1 /***************************************************************************
2 * Copyright (C) 2005 by Dominic Rath *
3 * Dominic.Rath@gmx.de *
5 * Copyright (C) 2007-2010 Øyvind Harboe *
6 * oyvind.harboe@zylin.com *
8 * Copyright (C) 2008, Duane Ellis *
9 * openocd@duaneeellis.com *
11 * Copyright (C) 2008 by Spencer Oliver *
12 * spen@spen-soft.co.uk *
14 * Copyright (C) 2008 by Rick Altherr *
15 * kc8apf@kc8apf.net> *
17 * Copyright (C) 2011 by Broadcom Corporation *
18 * Evan Hunter - ehunter@broadcom.com *
20 * Copyright (C) ST-Ericsson SA 2011 *
21 * michel.jaouen@stericsson.com : smp minimum support *
23 * Copyright (C) 2011 Andreas Fritiofson *
24 * andreas.fritiofson@gmail.com *
26 * This program is free software; you can redistribute it and/or modify *
27 * it under the terms of the GNU General Public License as published by *
28 * the Free Software Foundation; either version 2 of the License, or *
29 * (at your option) any later version. *
31 * This program is distributed in the hope that it will be useful, *
32 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
33 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
34 * GNU General Public License for more details. *
36 * You should have received a copy of the GNU General Public License *
37 * along with this program. If not, see <http://www.gnu.org/licenses/>. *
38 ***************************************************************************/
44 #include <helper/time_support.h>
45 #include <jtag/jtag.h>
46 #include <flash/nor/core.h>
49 #include "target_type.h"
50 #include "target_request.h"
51 #include "breakpoints.h"
55 #include "rtos/rtos.h"
56 #include "transport/transport.h"
58 /* default halt wait timeout (ms) */
59 #define DEFAULT_HALT_TIMEOUT 5000
61 static int target_read_buffer_default(struct target
*target
, target_addr_t address
,
62 uint32_t count
, uint8_t *buffer
);
63 static int target_write_buffer_default(struct target
*target
, target_addr_t address
,
64 uint32_t count
, const uint8_t *buffer
);
65 static int target_array2mem(Jim_Interp
*interp
, struct target
*target
,
66 int argc
, Jim_Obj
* const *argv
);
67 static int target_mem2array(Jim_Interp
*interp
, struct target
*target
,
68 int argc
, Jim_Obj
* const *argv
);
69 static int target_register_user_commands(struct command_context
*cmd_ctx
);
70 static int target_get_gdb_fileio_info_default(struct target
*target
,
71 struct gdb_fileio_info
*fileio_info
);
72 static int target_gdb_fileio_end_default(struct target
*target
, int retcode
,
73 int fileio_errno
, bool ctrl_c
);
74 static int target_profiling_default(struct target
*target
, uint32_t *samples
,
75 uint32_t max_num_samples
, uint32_t *num_samples
, uint32_t seconds
);
78 extern struct target_type arm7tdmi_target
;
79 extern struct target_type arm720t_target
;
80 extern struct target_type arm9tdmi_target
;
81 extern struct target_type arm920t_target
;
82 extern struct target_type arm966e_target
;
83 extern struct target_type arm946e_target
;
84 extern struct target_type arm926ejs_target
;
85 extern struct target_type fa526_target
;
86 extern struct target_type feroceon_target
;
87 extern struct target_type dragonite_target
;
88 extern struct target_type xscale_target
;
89 extern struct target_type cortexm_target
;
90 extern struct target_type cortexa_target
;
91 extern struct target_type aarch64_target
;
92 extern struct target_type cortexr4_target
;
93 extern struct target_type arm11_target
;
94 extern struct target_type ls1_sap_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
;
108 extern struct target_type stm8_target
;
110 static struct target_type
*target_types
[] = {
147 struct target
*all_targets
;
148 static struct target_event_callback
*target_event_callbacks
;
149 static struct target_timer_callback
*target_timer_callbacks
;
150 LIST_HEAD(target_reset_callback_list
);
151 LIST_HEAD(target_trace_callback_list
);
152 static const int polling_interval
= 100;
154 static const Jim_Nvp nvp_assert
[] = {
155 { .name
= "assert", NVP_ASSERT
},
156 { .name
= "deassert", NVP_DEASSERT
},
157 { .name
= "T", NVP_ASSERT
},
158 { .name
= "F", NVP_DEASSERT
},
159 { .name
= "t", NVP_ASSERT
},
160 { .name
= "f", NVP_DEASSERT
},
161 { .name
= NULL
, .value
= -1 }
164 static const Jim_Nvp nvp_error_target
[] = {
165 { .value
= ERROR_TARGET_INVALID
, .name
= "err-invalid" },
166 { .value
= ERROR_TARGET_INIT_FAILED
, .name
= "err-init-failed" },
167 { .value
= ERROR_TARGET_TIMEOUT
, .name
= "err-timeout" },
168 { .value
= ERROR_TARGET_NOT_HALTED
, .name
= "err-not-halted" },
169 { .value
= ERROR_TARGET_FAILURE
, .name
= "err-failure" },
170 { .value
= ERROR_TARGET_UNALIGNED_ACCESS
, .name
= "err-unaligned-access" },
171 { .value
= ERROR_TARGET_DATA_ABORT
, .name
= "err-data-abort" },
172 { .value
= ERROR_TARGET_RESOURCE_NOT_AVAILABLE
, .name
= "err-resource-not-available" },
173 { .value
= ERROR_TARGET_TRANSLATION_FAULT
, .name
= "err-translation-fault" },
174 { .value
= ERROR_TARGET_NOT_RUNNING
, .name
= "err-not-running" },
175 { .value
= ERROR_TARGET_NOT_EXAMINED
, .name
= "err-not-examined" },
176 { .value
= -1, .name
= NULL
}
179 static const char *target_strerror_safe(int err
)
183 n
= Jim_Nvp_value2name_simple(nvp_error_target
, err
);
190 static const Jim_Nvp nvp_target_event
[] = {
192 { .value
= TARGET_EVENT_GDB_HALT
, .name
= "gdb-halt" },
193 { .value
= TARGET_EVENT_HALTED
, .name
= "halted" },
194 { .value
= TARGET_EVENT_RESUMED
, .name
= "resumed" },
195 { .value
= TARGET_EVENT_RESUME_START
, .name
= "resume-start" },
196 { .value
= TARGET_EVENT_RESUME_END
, .name
= "resume-end" },
198 { .name
= "gdb-start", .value
= TARGET_EVENT_GDB_START
},
199 { .name
= "gdb-end", .value
= TARGET_EVENT_GDB_END
},
201 { .value
= TARGET_EVENT_RESET_START
, .name
= "reset-start" },
202 { .value
= TARGET_EVENT_RESET_ASSERT_PRE
, .name
= "reset-assert-pre" },
203 { .value
= TARGET_EVENT_RESET_ASSERT
, .name
= "reset-assert" },
204 { .value
= TARGET_EVENT_RESET_ASSERT_POST
, .name
= "reset-assert-post" },
205 { .value
= TARGET_EVENT_RESET_DEASSERT_PRE
, .name
= "reset-deassert-pre" },
206 { .value
= TARGET_EVENT_RESET_DEASSERT_POST
, .name
= "reset-deassert-post" },
207 { .value
= TARGET_EVENT_RESET_INIT
, .name
= "reset-init" },
208 { .value
= TARGET_EVENT_RESET_END
, .name
= "reset-end" },
210 { .value
= TARGET_EVENT_EXAMINE_START
, .name
= "examine-start" },
211 { .value
= TARGET_EVENT_EXAMINE_END
, .name
= "examine-end" },
213 { .value
= TARGET_EVENT_DEBUG_HALTED
, .name
= "debug-halted" },
214 { .value
= TARGET_EVENT_DEBUG_RESUMED
, .name
= "debug-resumed" },
216 { .value
= TARGET_EVENT_GDB_ATTACH
, .name
= "gdb-attach" },
217 { .value
= TARGET_EVENT_GDB_DETACH
, .name
= "gdb-detach" },
219 { .value
= TARGET_EVENT_GDB_FLASH_WRITE_START
, .name
= "gdb-flash-write-start" },
220 { .value
= TARGET_EVENT_GDB_FLASH_WRITE_END
, .name
= "gdb-flash-write-end" },
222 { .value
= TARGET_EVENT_GDB_FLASH_ERASE_START
, .name
= "gdb-flash-erase-start" },
223 { .value
= TARGET_EVENT_GDB_FLASH_ERASE_END
, .name
= "gdb-flash-erase-end" },
225 { .value
= TARGET_EVENT_TRACE_CONFIG
, .name
= "trace-config" },
227 { .name
= NULL
, .value
= -1 }
230 static const Jim_Nvp nvp_target_state
[] = {
231 { .name
= "unknown", .value
= TARGET_UNKNOWN
},
232 { .name
= "running", .value
= TARGET_RUNNING
},
233 { .name
= "halted", .value
= TARGET_HALTED
},
234 { .name
= "reset", .value
= TARGET_RESET
},
235 { .name
= "debug-running", .value
= TARGET_DEBUG_RUNNING
},
236 { .name
= NULL
, .value
= -1 },
239 static const Jim_Nvp nvp_target_debug_reason
[] = {
240 { .name
= "debug-request" , .value
= DBG_REASON_DBGRQ
},
241 { .name
= "breakpoint" , .value
= DBG_REASON_BREAKPOINT
},
242 { .name
= "watchpoint" , .value
= DBG_REASON_WATCHPOINT
},
243 { .name
= "watchpoint-and-breakpoint", .value
= DBG_REASON_WPTANDBKPT
},
244 { .name
= "single-step" , .value
= DBG_REASON_SINGLESTEP
},
245 { .name
= "target-not-halted" , .value
= DBG_REASON_NOTHALTED
},
246 { .name
= "program-exit" , .value
= DBG_REASON_EXIT
},
247 { .name
= "undefined" , .value
= DBG_REASON_UNDEFINED
},
248 { .name
= NULL
, .value
= -1 },
251 static const Jim_Nvp nvp_target_endian
[] = {
252 { .name
= "big", .value
= TARGET_BIG_ENDIAN
},
253 { .name
= "little", .value
= TARGET_LITTLE_ENDIAN
},
254 { .name
= "be", .value
= TARGET_BIG_ENDIAN
},
255 { .name
= "le", .value
= TARGET_LITTLE_ENDIAN
},
256 { .name
= NULL
, .value
= -1 },
259 static const Jim_Nvp nvp_reset_modes
[] = {
260 { .name
= "unknown", .value
= RESET_UNKNOWN
},
261 { .name
= "run" , .value
= RESET_RUN
},
262 { .name
= "halt" , .value
= RESET_HALT
},
263 { .name
= "init" , .value
= RESET_INIT
},
264 { .name
= NULL
, .value
= -1 },
267 const char *debug_reason_name(struct target
*t
)
271 cp
= Jim_Nvp_value2name_simple(nvp_target_debug_reason
,
272 t
->debug_reason
)->name
;
274 LOG_ERROR("Invalid debug reason: %d", (int)(t
->debug_reason
));
275 cp
= "(*BUG*unknown*BUG*)";
280 const char *target_state_name(struct target
*t
)
283 cp
= Jim_Nvp_value2name_simple(nvp_target_state
, t
->state
)->name
;
285 LOG_ERROR("Invalid target state: %d", (int)(t
->state
));
286 cp
= "(*BUG*unknown*BUG*)";
289 if (!target_was_examined(t
) && t
->defer_examine
)
290 cp
= "examine deferred";
295 const char *target_event_name(enum target_event event
)
298 cp
= Jim_Nvp_value2name_simple(nvp_target_event
, event
)->name
;
300 LOG_ERROR("Invalid target event: %d", (int)(event
));
301 cp
= "(*BUG*unknown*BUG*)";
306 const char *target_reset_mode_name(enum target_reset_mode reset_mode
)
309 cp
= Jim_Nvp_value2name_simple(nvp_reset_modes
, reset_mode
)->name
;
311 LOG_ERROR("Invalid target reset mode: %d", (int)(reset_mode
));
312 cp
= "(*BUG*unknown*BUG*)";
317 /* determine the number of the new target */
318 static int new_target_number(void)
323 /* number is 0 based */
327 if (x
< t
->target_number
)
328 x
= t
->target_number
;
334 /* read a uint64_t from a buffer in target memory endianness */
335 uint64_t target_buffer_get_u64(struct target
*target
, const uint8_t *buffer
)
337 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
338 return le_to_h_u64(buffer
);
340 return be_to_h_u64(buffer
);
343 /* read a uint32_t from a buffer in target memory endianness */
344 uint32_t target_buffer_get_u32(struct target
*target
, const uint8_t *buffer
)
346 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
347 return le_to_h_u32(buffer
);
349 return be_to_h_u32(buffer
);
352 /* read a uint24_t from a buffer in target memory endianness */
353 uint32_t target_buffer_get_u24(struct target
*target
, const uint8_t *buffer
)
355 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
356 return le_to_h_u24(buffer
);
358 return be_to_h_u24(buffer
);
361 /* read a uint16_t from a buffer in target memory endianness */
362 uint16_t target_buffer_get_u16(struct target
*target
, const uint8_t *buffer
)
364 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
365 return le_to_h_u16(buffer
);
367 return be_to_h_u16(buffer
);
370 /* read a uint8_t from a buffer in target memory endianness */
371 static uint8_t target_buffer_get_u8(struct target
*target
, const uint8_t *buffer
)
373 return *buffer
& 0x0ff;
376 /* write a uint64_t to a buffer in target memory endianness */
377 void target_buffer_set_u64(struct target
*target
, uint8_t *buffer
, uint64_t value
)
379 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
380 h_u64_to_le(buffer
, value
);
382 h_u64_to_be(buffer
, value
);
385 /* write a uint32_t to a buffer in target memory endianness */
386 void target_buffer_set_u32(struct target
*target
, uint8_t *buffer
, uint32_t value
)
388 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
389 h_u32_to_le(buffer
, value
);
391 h_u32_to_be(buffer
, value
);
394 /* write a uint24_t to a buffer in target memory endianness */
395 void target_buffer_set_u24(struct target
*target
, uint8_t *buffer
, uint32_t value
)
397 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
398 h_u24_to_le(buffer
, value
);
400 h_u24_to_be(buffer
, value
);
403 /* write a uint16_t to a buffer in target memory endianness */
404 void target_buffer_set_u16(struct target
*target
, uint8_t *buffer
, uint16_t value
)
406 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
407 h_u16_to_le(buffer
, value
);
409 h_u16_to_be(buffer
, value
);
412 /* write a uint8_t to a buffer in target memory endianness */
413 static void target_buffer_set_u8(struct target
*target
, uint8_t *buffer
, uint8_t value
)
418 /* write a uint64_t array to a buffer in target memory endianness */
419 void target_buffer_get_u64_array(struct target
*target
, const uint8_t *buffer
, uint32_t count
, uint64_t *dstbuf
)
422 for (i
= 0; i
< count
; i
++)
423 dstbuf
[i
] = target_buffer_get_u64(target
, &buffer
[i
* 8]);
426 /* write a uint32_t array to a buffer in target memory endianness */
427 void target_buffer_get_u32_array(struct target
*target
, const uint8_t *buffer
, uint32_t count
, uint32_t *dstbuf
)
430 for (i
= 0; i
< count
; i
++)
431 dstbuf
[i
] = target_buffer_get_u32(target
, &buffer
[i
* 4]);
434 /* write a uint16_t array to a buffer in target memory endianness */
435 void target_buffer_get_u16_array(struct target
*target
, const uint8_t *buffer
, uint32_t count
, uint16_t *dstbuf
)
438 for (i
= 0; i
< count
; i
++)
439 dstbuf
[i
] = target_buffer_get_u16(target
, &buffer
[i
* 2]);
442 /* write a uint64_t array to a buffer in target memory endianness */
443 void target_buffer_set_u64_array(struct target
*target
, uint8_t *buffer
, uint32_t count
, const uint64_t *srcbuf
)
446 for (i
= 0; i
< count
; i
++)
447 target_buffer_set_u64(target
, &buffer
[i
* 8], srcbuf
[i
]);
450 /* write a uint32_t array to a buffer in target memory endianness */
451 void target_buffer_set_u32_array(struct target
*target
, uint8_t *buffer
, uint32_t count
, const uint32_t *srcbuf
)
454 for (i
= 0; i
< count
; i
++)
455 target_buffer_set_u32(target
, &buffer
[i
* 4], srcbuf
[i
]);
458 /* write a uint16_t array to a buffer in target memory endianness */
459 void target_buffer_set_u16_array(struct target
*target
, uint8_t *buffer
, uint32_t count
, const uint16_t *srcbuf
)
462 for (i
= 0; i
< count
; i
++)
463 target_buffer_set_u16(target
, &buffer
[i
* 2], srcbuf
[i
]);
466 /* return a pointer to a configured target; id is name or number */
467 struct target
*get_target(const char *id
)
469 struct target
*target
;
471 /* try as tcltarget name */
472 for (target
= all_targets
; target
; target
= target
->next
) {
473 if (target_name(target
) == NULL
)
475 if (strcmp(id
, target_name(target
)) == 0)
479 /* It's OK to remove this fallback sometime after August 2010 or so */
481 /* no match, try as number */
483 if (parse_uint(id
, &num
) != ERROR_OK
)
486 for (target
= all_targets
; target
; target
= target
->next
) {
487 if (target
->target_number
== (int)num
) {
488 LOG_WARNING("use '%s' as target identifier, not '%u'",
489 target_name(target
), num
);
497 /* returns a pointer to the n-th configured target */
498 struct target
*get_target_by_num(int num
)
500 struct target
*target
= all_targets
;
503 if (target
->target_number
== num
)
505 target
= target
->next
;
511 struct target
*get_current_target(struct command_context
*cmd_ctx
)
513 struct target
*target
= get_target_by_num(cmd_ctx
->current_target
);
515 if (target
== NULL
) {
516 LOG_ERROR("BUG: current_target out of bounds");
523 int target_poll(struct target
*target
)
527 /* We can't poll until after examine */
528 if (!target_was_examined(target
)) {
529 /* Fail silently lest we pollute the log */
533 retval
= target
->type
->poll(target
);
534 if (retval
!= ERROR_OK
)
537 if (target
->halt_issued
) {
538 if (target
->state
== TARGET_HALTED
)
539 target
->halt_issued
= false;
541 int64_t t
= timeval_ms() - target
->halt_issued_time
;
542 if (t
> DEFAULT_HALT_TIMEOUT
) {
543 target
->halt_issued
= false;
544 LOG_INFO("Halt timed out, wake up GDB.");
545 target_call_event_callbacks(target
, TARGET_EVENT_GDB_HALT
);
553 int target_halt(struct target
*target
)
556 /* We can't poll until after examine */
557 if (!target_was_examined(target
)) {
558 LOG_ERROR("Target not examined yet");
562 retval
= target
->type
->halt(target
);
563 if (retval
!= ERROR_OK
)
566 target
->halt_issued
= true;
567 target
->halt_issued_time
= timeval_ms();
573 * Make the target (re)start executing using its saved execution
574 * context (possibly with some modifications).
576 * @param target Which target should start executing.
577 * @param current True to use the target's saved program counter instead
578 * of the address parameter
579 * @param address Optionally used as the program counter.
580 * @param handle_breakpoints True iff breakpoints at the resumption PC
581 * should be skipped. (For example, maybe execution was stopped by
582 * such a breakpoint, in which case it would be counterprodutive to
584 * @param debug_execution False if all working areas allocated by OpenOCD
585 * should be released and/or restored to their original contents.
586 * (This would for example be true to run some downloaded "helper"
587 * algorithm code, which resides in one such working buffer and uses
588 * another for data storage.)
590 * @todo Resolve the ambiguity about what the "debug_execution" flag
591 * signifies. For example, Target implementations don't agree on how
592 * it relates to invalidation of the register cache, or to whether
593 * breakpoints and watchpoints should be enabled. (It would seem wrong
594 * to enable breakpoints when running downloaded "helper" algorithms
595 * (debug_execution true), since the breakpoints would be set to match
596 * target firmware being debugged, not the helper algorithm.... and
597 * enabling them could cause such helpers to malfunction (for example,
598 * by overwriting data with a breakpoint instruction. On the other
599 * hand the infrastructure for running such helpers might use this
600 * procedure but rely on hardware breakpoint to detect termination.)
602 int target_resume(struct target
*target
, int current
, target_addr_t address
,
603 int handle_breakpoints
, int debug_execution
)
607 /* We can't poll until after examine */
608 if (!target_was_examined(target
)) {
609 LOG_ERROR("Target not examined yet");
613 target_call_event_callbacks(target
, TARGET_EVENT_RESUME_START
);
615 /* note that resume *must* be asynchronous. The CPU can halt before
616 * we poll. The CPU can even halt at the current PC as a result of
617 * a software breakpoint being inserted by (a bug?) the application.
619 retval
= target
->type
->resume(target
, current
, address
, handle_breakpoints
, debug_execution
);
620 if (retval
!= ERROR_OK
)
623 target_call_event_callbacks(target
, TARGET_EVENT_RESUME_END
);
628 static int target_process_reset(struct command_context
*cmd_ctx
, enum target_reset_mode reset_mode
)
633 n
= Jim_Nvp_value2name_simple(nvp_reset_modes
, reset_mode
);
634 if (n
->name
== NULL
) {
635 LOG_ERROR("invalid reset mode");
639 struct target
*target
;
640 for (target
= all_targets
; target
; target
= target
->next
)
641 target_call_reset_callbacks(target
, reset_mode
);
643 /* disable polling during reset to make reset event scripts
644 * more predictable, i.e. dr/irscan & pathmove in events will
645 * not have JTAG operations injected into the middle of a sequence.
647 bool save_poll
= jtag_poll_get_enabled();
649 jtag_poll_set_enabled(false);
651 sprintf(buf
, "ocd_process_reset %s", n
->name
);
652 retval
= Jim_Eval(cmd_ctx
->interp
, buf
);
654 jtag_poll_set_enabled(save_poll
);
656 if (retval
!= JIM_OK
) {
657 Jim_MakeErrorMessage(cmd_ctx
->interp
);
658 command_print(NULL
, "%s\n", Jim_GetString(Jim_GetResult(cmd_ctx
->interp
), NULL
));
662 /* We want any events to be processed before the prompt */
663 retval
= target_call_timer_callbacks_now();
665 for (target
= all_targets
; target
; target
= target
->next
) {
666 target
->type
->check_reset(target
);
667 target
->running_alg
= false;
673 static int identity_virt2phys(struct target
*target
,
674 target_addr_t
virtual, target_addr_t
*physical
)
680 static int no_mmu(struct target
*target
, int *enabled
)
686 static int default_examine(struct target
*target
)
688 target_set_examined(target
);
692 /* no check by default */
693 static int default_check_reset(struct target
*target
)
698 int target_examine_one(struct target
*target
)
700 target_call_event_callbacks(target
, TARGET_EVENT_EXAMINE_START
);
702 int retval
= target
->type
->examine(target
);
703 if (retval
!= ERROR_OK
)
706 target_call_event_callbacks(target
, TARGET_EVENT_EXAMINE_END
);
711 static int jtag_enable_callback(enum jtag_event event
, void *priv
)
713 struct target
*target
= priv
;
715 if (event
!= JTAG_TAP_EVENT_ENABLE
|| !target
->tap
->enabled
)
718 jtag_unregister_event_callback(jtag_enable_callback
, target
);
720 return target_examine_one(target
);
723 /* Targets that correctly implement init + examine, i.e.
724 * no communication with target during init:
728 int target_examine(void)
730 int retval
= ERROR_OK
;
731 struct target
*target
;
733 for (target
= all_targets
; target
; target
= target
->next
) {
734 /* defer examination, but don't skip it */
735 if (!target
->tap
->enabled
) {
736 jtag_register_event_callback(jtag_enable_callback
,
741 if (target
->defer_examine
)
744 retval
= target_examine_one(target
);
745 if (retval
!= ERROR_OK
)
751 const char *target_type_name(struct target
*target
)
753 return target
->type
->name
;
756 static int target_soft_reset_halt(struct target
*target
)
758 if (!target_was_examined(target
)) {
759 LOG_ERROR("Target not examined yet");
762 if (!target
->type
->soft_reset_halt
) {
763 LOG_ERROR("Target %s does not support soft_reset_halt",
764 target_name(target
));
767 return target
->type
->soft_reset_halt(target
);
771 * Downloads a target-specific native code algorithm to the target,
772 * and executes it. * Note that some targets may need to set up, enable,
773 * and tear down a breakpoint (hard or * soft) to detect algorithm
774 * termination, while others may support lower overhead schemes where
775 * soft breakpoints embedded in the algorithm automatically terminate the
778 * @param target used to run the algorithm
779 * @param arch_info target-specific description of the algorithm.
781 int target_run_algorithm(struct target
*target
,
782 int num_mem_params
, struct mem_param
*mem_params
,
783 int num_reg_params
, struct reg_param
*reg_param
,
784 uint32_t entry_point
, uint32_t exit_point
,
785 int timeout_ms
, void *arch_info
)
787 int retval
= ERROR_FAIL
;
789 if (!target_was_examined(target
)) {
790 LOG_ERROR("Target not examined yet");
793 if (!target
->type
->run_algorithm
) {
794 LOG_ERROR("Target type '%s' does not support %s",
795 target_type_name(target
), __func__
);
799 target
->running_alg
= true;
800 retval
= target
->type
->run_algorithm(target
,
801 num_mem_params
, mem_params
,
802 num_reg_params
, reg_param
,
803 entry_point
, exit_point
, timeout_ms
, arch_info
);
804 target
->running_alg
= false;
811 * Downloads a target-specific native code algorithm to the target,
812 * executes and leaves it running.
814 * @param target used to run the algorithm
815 * @param arch_info target-specific description of the algorithm.
817 int target_start_algorithm(struct target
*target
,
818 int num_mem_params
, struct mem_param
*mem_params
,
819 int num_reg_params
, struct reg_param
*reg_params
,
820 uint32_t entry_point
, uint32_t exit_point
,
823 int retval
= ERROR_FAIL
;
825 if (!target_was_examined(target
)) {
826 LOG_ERROR("Target not examined yet");
829 if (!target
->type
->start_algorithm
) {
830 LOG_ERROR("Target type '%s' does not support %s",
831 target_type_name(target
), __func__
);
834 if (target
->running_alg
) {
835 LOG_ERROR("Target is already running an algorithm");
839 target
->running_alg
= true;
840 retval
= target
->type
->start_algorithm(target
,
841 num_mem_params
, mem_params
,
842 num_reg_params
, reg_params
,
843 entry_point
, exit_point
, arch_info
);
850 * Waits for an algorithm started with target_start_algorithm() to complete.
852 * @param target used to run the algorithm
853 * @param arch_info target-specific description of the algorithm.
855 int target_wait_algorithm(struct target
*target
,
856 int num_mem_params
, struct mem_param
*mem_params
,
857 int num_reg_params
, struct reg_param
*reg_params
,
858 uint32_t exit_point
, int timeout_ms
,
861 int retval
= ERROR_FAIL
;
863 if (!target
->type
->wait_algorithm
) {
864 LOG_ERROR("Target type '%s' does not support %s",
865 target_type_name(target
), __func__
);
868 if (!target
->running_alg
) {
869 LOG_ERROR("Target is not running an algorithm");
873 retval
= target
->type
->wait_algorithm(target
,
874 num_mem_params
, mem_params
,
875 num_reg_params
, reg_params
,
876 exit_point
, timeout_ms
, arch_info
);
877 if (retval
!= ERROR_TARGET_TIMEOUT
)
878 target
->running_alg
= false;
885 * Executes a target-specific native code algorithm in the target.
886 * It differs from target_run_algorithm in that the algorithm is asynchronous.
887 * Because of this it requires an compliant algorithm:
888 * see contrib/loaders/flash/stm32f1x.S for example.
890 * @param target used to run the algorithm
893 int target_run_flash_async_algorithm(struct target
*target
,
894 const uint8_t *buffer
, uint32_t count
, int block_size
,
895 int num_mem_params
, struct mem_param
*mem_params
,
896 int num_reg_params
, struct reg_param
*reg_params
,
897 uint32_t buffer_start
, uint32_t buffer_size
,
898 uint32_t entry_point
, uint32_t exit_point
, void *arch_info
)
903 const uint8_t *buffer_orig
= buffer
;
905 /* Set up working area. First word is write pointer, second word is read pointer,
906 * rest is fifo data area. */
907 uint32_t wp_addr
= buffer_start
;
908 uint32_t rp_addr
= buffer_start
+ 4;
909 uint32_t fifo_start_addr
= buffer_start
+ 8;
910 uint32_t fifo_end_addr
= buffer_start
+ buffer_size
;
912 uint32_t wp
= fifo_start_addr
;
913 uint32_t rp
= fifo_start_addr
;
915 /* validate block_size is 2^n */
916 assert(!block_size
|| !(block_size
& (block_size
- 1)));
918 retval
= target_write_u32(target
, wp_addr
, wp
);
919 if (retval
!= ERROR_OK
)
921 retval
= target_write_u32(target
, rp_addr
, rp
);
922 if (retval
!= ERROR_OK
)
925 /* Start up algorithm on target and let it idle while writing the first chunk */
926 retval
= target_start_algorithm(target
, num_mem_params
, mem_params
,
927 num_reg_params
, reg_params
,
932 if (retval
!= ERROR_OK
) {
933 LOG_ERROR("error starting target flash write algorithm");
939 retval
= target_read_u32(target
, rp_addr
, &rp
);
940 if (retval
!= ERROR_OK
) {
941 LOG_ERROR("failed to get read pointer");
945 LOG_DEBUG("offs 0x%zx count 0x%" PRIx32
" wp 0x%" PRIx32
" rp 0x%" PRIx32
,
946 (size_t) (buffer
- buffer_orig
), count
, wp
, rp
);
949 LOG_ERROR("flash write algorithm aborted by target");
950 retval
= ERROR_FLASH_OPERATION_FAILED
;
954 if (((rp
- fifo_start_addr
) & (block_size
- 1)) || rp
< fifo_start_addr
|| rp
>= fifo_end_addr
) {
955 LOG_ERROR("corrupted fifo read pointer 0x%" PRIx32
, rp
);
959 /* Count the number of bytes available in the fifo without
960 * crossing the wrap around. Make sure to not fill it completely,
961 * because that would make wp == rp and that's the empty condition. */
962 uint32_t thisrun_bytes
;
964 thisrun_bytes
= rp
- wp
- block_size
;
965 else if (rp
> fifo_start_addr
)
966 thisrun_bytes
= fifo_end_addr
- wp
;
968 thisrun_bytes
= fifo_end_addr
- wp
- block_size
;
970 if (thisrun_bytes
== 0) {
971 /* Throttle polling a bit if transfer is (much) faster than flash
972 * programming. The exact delay shouldn't matter as long as it's
973 * less than buffer size / flash speed. This is very unlikely to
974 * run when using high latency connections such as USB. */
977 /* to stop an infinite loop on some targets check and increment a timeout
978 * this issue was observed on a stellaris using the new ICDI interface */
979 if (timeout
++ >= 500) {
980 LOG_ERROR("timeout waiting for algorithm, a target reset is recommended");
981 return ERROR_FLASH_OPERATION_FAILED
;
986 /* reset our timeout */
989 /* Limit to the amount of data we actually want to write */
990 if (thisrun_bytes
> count
* block_size
)
991 thisrun_bytes
= count
* block_size
;
993 /* Write data to fifo */
994 retval
= target_write_buffer(target
, wp
, thisrun_bytes
, buffer
);
995 if (retval
!= ERROR_OK
)
998 /* Update counters and wrap write pointer */
999 buffer
+= thisrun_bytes
;
1000 count
-= thisrun_bytes
/ block_size
;
1001 wp
+= thisrun_bytes
;
1002 if (wp
>= fifo_end_addr
)
1003 wp
= fifo_start_addr
;
1005 /* Store updated write pointer to target */
1006 retval
= target_write_u32(target
, wp_addr
, wp
);
1007 if (retval
!= ERROR_OK
)
1011 if (retval
!= ERROR_OK
) {
1012 /* abort flash write algorithm on target */
1013 target_write_u32(target
, wp_addr
, 0);
1016 int retval2
= target_wait_algorithm(target
, num_mem_params
, mem_params
,
1017 num_reg_params
, reg_params
,
1022 if (retval2
!= ERROR_OK
) {
1023 LOG_ERROR("error waiting for target flash write algorithm");
1027 if (retval
== ERROR_OK
) {
1028 /* check if algorithm set rp = 0 after fifo writer loop finished */
1029 retval
= target_read_u32(target
, rp_addr
, &rp
);
1030 if (retval
== ERROR_OK
&& rp
== 0) {
1031 LOG_ERROR("flash write algorithm aborted by target");
1032 retval
= ERROR_FLASH_OPERATION_FAILED
;
1039 int target_read_memory(struct target
*target
,
1040 target_addr_t address
, uint32_t size
, uint32_t count
, uint8_t *buffer
)
1042 if (!target_was_examined(target
)) {
1043 LOG_ERROR("Target not examined yet");
1046 if (!target
->type
->read_memory
) {
1047 LOG_ERROR("Target %s doesn't support read_memory", target_name(target
));
1050 return target
->type
->read_memory(target
, address
, size
, count
, buffer
);
1053 int target_read_phys_memory(struct target
*target
,
1054 target_addr_t address
, uint32_t size
, uint32_t count
, uint8_t *buffer
)
1056 if (!target_was_examined(target
)) {
1057 LOG_ERROR("Target not examined yet");
1060 if (!target
->type
->read_phys_memory
) {
1061 LOG_ERROR("Target %s doesn't support read_phys_memory", target_name(target
));
1064 return target
->type
->read_phys_memory(target
, address
, size
, count
, buffer
);
1067 int target_write_memory(struct target
*target
,
1068 target_addr_t address
, uint32_t size
, uint32_t count
, const uint8_t *buffer
)
1070 if (!target_was_examined(target
)) {
1071 LOG_ERROR("Target not examined yet");
1074 if (!target
->type
->write_memory
) {
1075 LOG_ERROR("Target %s doesn't support write_memory", target_name(target
));
1078 return target
->type
->write_memory(target
, address
, size
, count
, buffer
);
1081 int target_write_phys_memory(struct target
*target
,
1082 target_addr_t address
, uint32_t size
, uint32_t count
, const uint8_t *buffer
)
1084 if (!target_was_examined(target
)) {
1085 LOG_ERROR("Target not examined yet");
1088 if (!target
->type
->write_phys_memory
) {
1089 LOG_ERROR("Target %s doesn't support write_phys_memory", target_name(target
));
1092 return target
->type
->write_phys_memory(target
, address
, size
, count
, buffer
);
1095 int target_add_breakpoint(struct target
*target
,
1096 struct breakpoint
*breakpoint
)
1098 if ((target
->state
!= TARGET_HALTED
) && (breakpoint
->type
!= BKPT_HARD
)) {
1099 LOG_WARNING("target %s is not halted (add breakpoint)", target_name(target
));
1100 return ERROR_TARGET_NOT_HALTED
;
1102 return target
->type
->add_breakpoint(target
, breakpoint
);
1105 int target_add_context_breakpoint(struct target
*target
,
1106 struct breakpoint
*breakpoint
)
1108 if (target
->state
!= TARGET_HALTED
) {
1109 LOG_WARNING("target %s is not halted (add context breakpoint)", target_name(target
));
1110 return ERROR_TARGET_NOT_HALTED
;
1112 return target
->type
->add_context_breakpoint(target
, breakpoint
);
1115 int target_add_hybrid_breakpoint(struct target
*target
,
1116 struct breakpoint
*breakpoint
)
1118 if (target
->state
!= TARGET_HALTED
) {
1119 LOG_WARNING("target %s is not halted (add hybrid breakpoint)", target_name(target
));
1120 return ERROR_TARGET_NOT_HALTED
;
1122 return target
->type
->add_hybrid_breakpoint(target
, breakpoint
);
1125 int target_remove_breakpoint(struct target
*target
,
1126 struct breakpoint
*breakpoint
)
1128 return target
->type
->remove_breakpoint(target
, breakpoint
);
1131 int target_add_watchpoint(struct target
*target
,
1132 struct watchpoint
*watchpoint
)
1134 if (target
->state
!= TARGET_HALTED
) {
1135 LOG_WARNING("target %s is not halted (add watchpoint)", target_name(target
));
1136 return ERROR_TARGET_NOT_HALTED
;
1138 return target
->type
->add_watchpoint(target
, watchpoint
);
1140 int target_remove_watchpoint(struct target
*target
,
1141 struct watchpoint
*watchpoint
)
1143 return target
->type
->remove_watchpoint(target
, watchpoint
);
1145 int target_hit_watchpoint(struct target
*target
,
1146 struct watchpoint
**hit_watchpoint
)
1148 if (target
->state
!= TARGET_HALTED
) {
1149 LOG_WARNING("target %s is not halted (hit watchpoint)", target
->cmd_name
);
1150 return ERROR_TARGET_NOT_HALTED
;
1153 if (target
->type
->hit_watchpoint
== NULL
) {
1154 /* For backward compatible, if hit_watchpoint is not implemented,
1155 * return ERROR_FAIL such that gdb_server will not take the nonsense
1160 return target
->type
->hit_watchpoint(target
, hit_watchpoint
);
1163 int target_get_gdb_reg_list(struct target
*target
,
1164 struct reg
**reg_list
[], int *reg_list_size
,
1165 enum target_register_class reg_class
)
1167 return target
->type
->get_gdb_reg_list(target
, reg_list
, reg_list_size
, reg_class
);
1169 int target_step(struct target
*target
,
1170 int current
, target_addr_t address
, int handle_breakpoints
)
1172 return target
->type
->step(target
, current
, address
, handle_breakpoints
);
1175 int target_get_gdb_fileio_info(struct target
*target
, struct gdb_fileio_info
*fileio_info
)
1177 if (target
->state
!= TARGET_HALTED
) {
1178 LOG_WARNING("target %s is not halted (gdb fileio)", target
->cmd_name
);
1179 return ERROR_TARGET_NOT_HALTED
;
1181 return target
->type
->get_gdb_fileio_info(target
, fileio_info
);
1184 int target_gdb_fileio_end(struct target
*target
, int retcode
, int fileio_errno
, bool ctrl_c
)
1186 if (target
->state
!= TARGET_HALTED
) {
1187 LOG_WARNING("target %s is not halted (gdb fileio end)", target
->cmd_name
);
1188 return ERROR_TARGET_NOT_HALTED
;
1190 return target
->type
->gdb_fileio_end(target
, retcode
, fileio_errno
, ctrl_c
);
1193 int target_profiling(struct target
*target
, uint32_t *samples
,
1194 uint32_t max_num_samples
, uint32_t *num_samples
, uint32_t seconds
)
1196 if (target
->state
!= TARGET_HALTED
) {
1197 LOG_WARNING("target %s is not halted (profiling)", target
->cmd_name
);
1198 return ERROR_TARGET_NOT_HALTED
;
1200 return target
->type
->profiling(target
, samples
, max_num_samples
,
1201 num_samples
, seconds
);
1205 * Reset the @c examined flag for the given target.
1206 * Pure paranoia -- targets are zeroed on allocation.
1208 static void target_reset_examined(struct target
*target
)
1210 target
->examined
= false;
1213 static int handle_target(void *priv
);
1215 static int target_init_one(struct command_context
*cmd_ctx
,
1216 struct target
*target
)
1218 target_reset_examined(target
);
1220 struct target_type
*type
= target
->type
;
1221 if (type
->examine
== NULL
)
1222 type
->examine
= default_examine
;
1224 if (type
->check_reset
== NULL
)
1225 type
->check_reset
= default_check_reset
;
1227 assert(type
->init_target
!= NULL
);
1229 int retval
= type
->init_target(cmd_ctx
, target
);
1230 if (ERROR_OK
!= retval
) {
1231 LOG_ERROR("target '%s' init failed", target_name(target
));
1235 /* Sanity-check MMU support ... stub in what we must, to help
1236 * implement it in stages, but warn if we need to do so.
1239 if (type
->virt2phys
== NULL
) {
1240 LOG_ERROR("type '%s' is missing virt2phys", type
->name
);
1241 type
->virt2phys
= identity_virt2phys
;
1244 /* Make sure no-MMU targets all behave the same: make no
1245 * distinction between physical and virtual addresses, and
1246 * ensure that virt2phys() is always an identity mapping.
1248 if (type
->write_phys_memory
|| type
->read_phys_memory
|| type
->virt2phys
)
1249 LOG_WARNING("type '%s' has bad MMU hooks", type
->name
);
1252 type
->write_phys_memory
= type
->write_memory
;
1253 type
->read_phys_memory
= type
->read_memory
;
1254 type
->virt2phys
= identity_virt2phys
;
1257 if (target
->type
->read_buffer
== NULL
)
1258 target
->type
->read_buffer
= target_read_buffer_default
;
1260 if (target
->type
->write_buffer
== NULL
)
1261 target
->type
->write_buffer
= target_write_buffer_default
;
1263 if (target
->type
->get_gdb_fileio_info
== NULL
)
1264 target
->type
->get_gdb_fileio_info
= target_get_gdb_fileio_info_default
;
1266 if (target
->type
->gdb_fileio_end
== NULL
)
1267 target
->type
->gdb_fileio_end
= target_gdb_fileio_end_default
;
1269 if (target
->type
->profiling
== NULL
)
1270 target
->type
->profiling
= target_profiling_default
;
1275 static int target_init(struct command_context
*cmd_ctx
)
1277 struct target
*target
;
1280 for (target
= all_targets
; target
; target
= target
->next
) {
1281 retval
= target_init_one(cmd_ctx
, target
);
1282 if (ERROR_OK
!= retval
)
1289 retval
= target_register_user_commands(cmd_ctx
);
1290 if (ERROR_OK
!= retval
)
1293 retval
= target_register_timer_callback(&handle_target
,
1294 polling_interval
, 1, cmd_ctx
->interp
);
1295 if (ERROR_OK
!= retval
)
1301 COMMAND_HANDLER(handle_target_init_command
)
1306 return ERROR_COMMAND_SYNTAX_ERROR
;
1308 static bool target_initialized
;
1309 if (target_initialized
) {
1310 LOG_INFO("'target init' has already been called");
1313 target_initialized
= true;
1315 retval
= command_run_line(CMD_CTX
, "init_targets");
1316 if (ERROR_OK
!= retval
)
1319 retval
= command_run_line(CMD_CTX
, "init_target_events");
1320 if (ERROR_OK
!= retval
)
1323 retval
= command_run_line(CMD_CTX
, "init_board");
1324 if (ERROR_OK
!= retval
)
1327 LOG_DEBUG("Initializing targets...");
1328 return target_init(CMD_CTX
);
1331 int target_register_event_callback(int (*callback
)(struct target
*target
,
1332 enum target_event event
, void *priv
), void *priv
)
1334 struct target_event_callback
**callbacks_p
= &target_event_callbacks
;
1336 if (callback
== NULL
)
1337 return ERROR_COMMAND_SYNTAX_ERROR
;
1340 while ((*callbacks_p
)->next
)
1341 callbacks_p
= &((*callbacks_p
)->next
);
1342 callbacks_p
= &((*callbacks_p
)->next
);
1345 (*callbacks_p
) = malloc(sizeof(struct target_event_callback
));
1346 (*callbacks_p
)->callback
= callback
;
1347 (*callbacks_p
)->priv
= priv
;
1348 (*callbacks_p
)->next
= NULL
;
1353 int target_register_reset_callback(int (*callback
)(struct target
*target
,
1354 enum target_reset_mode reset_mode
, void *priv
), void *priv
)
1356 struct target_reset_callback
*entry
;
1358 if (callback
== NULL
)
1359 return ERROR_COMMAND_SYNTAX_ERROR
;
1361 entry
= malloc(sizeof(struct target_reset_callback
));
1362 if (entry
== NULL
) {
1363 LOG_ERROR("error allocating buffer for reset callback entry");
1364 return ERROR_COMMAND_SYNTAX_ERROR
;
1367 entry
->callback
= callback
;
1369 list_add(&entry
->list
, &target_reset_callback_list
);
1375 int target_register_trace_callback(int (*callback
)(struct target
*target
,
1376 size_t len
, uint8_t *data
, void *priv
), void *priv
)
1378 struct target_trace_callback
*entry
;
1380 if (callback
== NULL
)
1381 return ERROR_COMMAND_SYNTAX_ERROR
;
1383 entry
= malloc(sizeof(struct target_trace_callback
));
1384 if (entry
== NULL
) {
1385 LOG_ERROR("error allocating buffer for trace callback entry");
1386 return ERROR_COMMAND_SYNTAX_ERROR
;
1389 entry
->callback
= callback
;
1391 list_add(&entry
->list
, &target_trace_callback_list
);
1397 int target_register_timer_callback(int (*callback
)(void *priv
), int time_ms
, int periodic
, void *priv
)
1399 struct target_timer_callback
**callbacks_p
= &target_timer_callbacks
;
1401 if (callback
== NULL
)
1402 return ERROR_COMMAND_SYNTAX_ERROR
;
1405 while ((*callbacks_p
)->next
)
1406 callbacks_p
= &((*callbacks_p
)->next
);
1407 callbacks_p
= &((*callbacks_p
)->next
);
1410 (*callbacks_p
) = malloc(sizeof(struct target_timer_callback
));
1411 (*callbacks_p
)->callback
= callback
;
1412 (*callbacks_p
)->periodic
= periodic
;
1413 (*callbacks_p
)->time_ms
= time_ms
;
1414 (*callbacks_p
)->removed
= false;
1416 gettimeofday(&(*callbacks_p
)->when
, NULL
);
1417 timeval_add_time(&(*callbacks_p
)->when
, 0, time_ms
* 1000);
1419 (*callbacks_p
)->priv
= priv
;
1420 (*callbacks_p
)->next
= NULL
;
1425 int target_unregister_event_callback(int (*callback
)(struct target
*target
,
1426 enum target_event event
, void *priv
), void *priv
)
1428 struct target_event_callback
**p
= &target_event_callbacks
;
1429 struct target_event_callback
*c
= target_event_callbacks
;
1431 if (callback
== NULL
)
1432 return ERROR_COMMAND_SYNTAX_ERROR
;
1435 struct target_event_callback
*next
= c
->next
;
1436 if ((c
->callback
== callback
) && (c
->priv
== priv
)) {
1448 int target_unregister_reset_callback(int (*callback
)(struct target
*target
,
1449 enum target_reset_mode reset_mode
, void *priv
), void *priv
)
1451 struct target_reset_callback
*entry
;
1453 if (callback
== NULL
)
1454 return ERROR_COMMAND_SYNTAX_ERROR
;
1456 list_for_each_entry(entry
, &target_reset_callback_list
, list
) {
1457 if (entry
->callback
== callback
&& entry
->priv
== priv
) {
1458 list_del(&entry
->list
);
1467 int target_unregister_trace_callback(int (*callback
)(struct target
*target
,
1468 size_t len
, uint8_t *data
, void *priv
), void *priv
)
1470 struct target_trace_callback
*entry
;
1472 if (callback
== NULL
)
1473 return ERROR_COMMAND_SYNTAX_ERROR
;
1475 list_for_each_entry(entry
, &target_trace_callback_list
, list
) {
1476 if (entry
->callback
== callback
&& entry
->priv
== priv
) {
1477 list_del(&entry
->list
);
1486 int target_unregister_timer_callback(int (*callback
)(void *priv
), void *priv
)
1488 if (callback
== NULL
)
1489 return ERROR_COMMAND_SYNTAX_ERROR
;
1491 for (struct target_timer_callback
*c
= target_timer_callbacks
;
1493 if ((c
->callback
== callback
) && (c
->priv
== priv
)) {
1502 int target_call_event_callbacks(struct target
*target
, enum target_event event
)
1504 struct target_event_callback
*callback
= target_event_callbacks
;
1505 struct target_event_callback
*next_callback
;
1507 if (event
== TARGET_EVENT_HALTED
) {
1508 /* execute early halted first */
1509 target_call_event_callbacks(target
, TARGET_EVENT_GDB_HALT
);
1512 LOG_DEBUG("target event %i (%s)", event
,
1513 Jim_Nvp_value2name_simple(nvp_target_event
, event
)->name
);
1515 target_handle_event(target
, event
);
1518 next_callback
= callback
->next
;
1519 callback
->callback(target
, event
, callback
->priv
);
1520 callback
= next_callback
;
1526 int target_call_reset_callbacks(struct target
*target
, enum target_reset_mode reset_mode
)
1528 struct target_reset_callback
*callback
;
1530 LOG_DEBUG("target reset %i (%s)", reset_mode
,
1531 Jim_Nvp_value2name_simple(nvp_reset_modes
, reset_mode
)->name
);
1533 list_for_each_entry(callback
, &target_reset_callback_list
, list
)
1534 callback
->callback(target
, reset_mode
, callback
->priv
);
1539 int target_call_trace_callbacks(struct target
*target
, size_t len
, uint8_t *data
)
1541 struct target_trace_callback
*callback
;
1543 list_for_each_entry(callback
, &target_trace_callback_list
, list
)
1544 callback
->callback(target
, len
, data
, callback
->priv
);
1549 static int target_timer_callback_periodic_restart(
1550 struct target_timer_callback
*cb
, struct timeval
*now
)
1553 timeval_add_time(&cb
->when
, 0, cb
->time_ms
* 1000L);
1557 static int target_call_timer_callback(struct target_timer_callback
*cb
,
1558 struct timeval
*now
)
1560 cb
->callback(cb
->priv
);
1563 return target_timer_callback_periodic_restart(cb
, now
);
1565 return target_unregister_timer_callback(cb
->callback
, cb
->priv
);
1568 static int target_call_timer_callbacks_check_time(int checktime
)
1570 static bool callback_processing
;
1572 /* Do not allow nesting */
1573 if (callback_processing
)
1576 callback_processing
= true;
1581 gettimeofday(&now
, NULL
);
1583 /* Store an address of the place containing a pointer to the
1584 * next item; initially, that's a standalone "root of the
1585 * list" variable. */
1586 struct target_timer_callback
**callback
= &target_timer_callbacks
;
1588 if ((*callback
)->removed
) {
1589 struct target_timer_callback
*p
= *callback
;
1590 *callback
= (*callback
)->next
;
1595 bool call_it
= (*callback
)->callback
&&
1596 ((!checktime
&& (*callback
)->periodic
) ||
1597 timeval_compare(&now
, &(*callback
)->when
) >= 0);
1600 target_call_timer_callback(*callback
, &now
);
1602 callback
= &(*callback
)->next
;
1605 callback_processing
= false;
1609 int target_call_timer_callbacks(void)
1611 return target_call_timer_callbacks_check_time(1);
1614 /* invoke periodic callbacks immediately */
1615 int target_call_timer_callbacks_now(void)
1617 return target_call_timer_callbacks_check_time(0);
1620 /* Prints the working area layout for debug purposes */
1621 static void print_wa_layout(struct target
*target
)
1623 struct working_area
*c
= target
->working_areas
;
1626 LOG_DEBUG("%c%c " TARGET_ADDR_FMT
"-" TARGET_ADDR_FMT
" (%" PRIu32
" bytes)",
1627 c
->backup
? 'b' : ' ', c
->free
? ' ' : '*',
1628 c
->address
, c
->address
+ c
->size
- 1, c
->size
);
1633 /* Reduce area to size bytes, create a new free area from the remaining bytes, if any. */
1634 static void target_split_working_area(struct working_area
*area
, uint32_t size
)
1636 assert(area
->free
); /* Shouldn't split an allocated area */
1637 assert(size
<= area
->size
); /* Caller should guarantee this */
1639 /* Split only if not already the right size */
1640 if (size
< area
->size
) {
1641 struct working_area
*new_wa
= malloc(sizeof(*new_wa
));
1646 new_wa
->next
= area
->next
;
1647 new_wa
->size
= area
->size
- size
;
1648 new_wa
->address
= area
->address
+ size
;
1649 new_wa
->backup
= NULL
;
1650 new_wa
->user
= NULL
;
1651 new_wa
->free
= true;
1653 area
->next
= new_wa
;
1656 /* If backup memory was allocated to this area, it has the wrong size
1657 * now so free it and it will be reallocated if/when needed */
1660 area
->backup
= NULL
;
1665 /* Merge all adjacent free areas into one */
1666 static void target_merge_working_areas(struct target
*target
)
1668 struct working_area
*c
= target
->working_areas
;
1670 while (c
&& c
->next
) {
1671 assert(c
->next
->address
== c
->address
+ c
->size
); /* This is an invariant */
1673 /* Find two adjacent free areas */
1674 if (c
->free
&& c
->next
->free
) {
1675 /* Merge the last into the first */
1676 c
->size
+= c
->next
->size
;
1678 /* Remove the last */
1679 struct working_area
*to_be_freed
= c
->next
;
1680 c
->next
= c
->next
->next
;
1681 if (to_be_freed
->backup
)
1682 free(to_be_freed
->backup
);
1685 /* If backup memory was allocated to the remaining area, it's has
1686 * the wrong size now */
1697 int target_alloc_working_area_try(struct target
*target
, uint32_t size
, struct working_area
**area
)
1699 /* Reevaluate working area address based on MMU state*/
1700 if (target
->working_areas
== NULL
) {
1704 retval
= target
->type
->mmu(target
, &enabled
);
1705 if (retval
!= ERROR_OK
)
1709 if (target
->working_area_phys_spec
) {
1710 LOG_DEBUG("MMU disabled, using physical "
1711 "address for working memory " TARGET_ADDR_FMT
,
1712 target
->working_area_phys
);
1713 target
->working_area
= target
->working_area_phys
;
1715 LOG_ERROR("No working memory available. "
1716 "Specify -work-area-phys to target.");
1717 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
1720 if (target
->working_area_virt_spec
) {
1721 LOG_DEBUG("MMU enabled, using virtual "
1722 "address for working memory " TARGET_ADDR_FMT
,
1723 target
->working_area_virt
);
1724 target
->working_area
= target
->working_area_virt
;
1726 LOG_ERROR("No working memory available. "
1727 "Specify -work-area-virt to target.");
1728 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
1732 /* Set up initial working area on first call */
1733 struct working_area
*new_wa
= malloc(sizeof(*new_wa
));
1735 new_wa
->next
= NULL
;
1736 new_wa
->size
= target
->working_area_size
& ~3UL; /* 4-byte align */
1737 new_wa
->address
= target
->working_area
;
1738 new_wa
->backup
= NULL
;
1739 new_wa
->user
= NULL
;
1740 new_wa
->free
= true;
1743 target
->working_areas
= new_wa
;
1746 /* only allocate multiples of 4 byte */
1748 size
= (size
+ 3) & (~3UL);
1750 struct working_area
*c
= target
->working_areas
;
1752 /* Find the first large enough working area */
1754 if (c
->free
&& c
->size
>= size
)
1760 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
1762 /* Split the working area into the requested size */
1763 target_split_working_area(c
, size
);
1765 LOG_DEBUG("allocated new working area of %" PRIu32
" bytes at address " TARGET_ADDR_FMT
,
1768 if (target
->backup_working_area
) {
1769 if (c
->backup
== NULL
) {
1770 c
->backup
= malloc(c
->size
);
1771 if (c
->backup
== NULL
)
1775 int retval
= target_read_memory(target
, c
->address
, 4, c
->size
/ 4, c
->backup
);
1776 if (retval
!= ERROR_OK
)
1780 /* mark as used, and return the new (reused) area */
1787 print_wa_layout(target
);
1792 int target_alloc_working_area(struct target
*target
, uint32_t size
, struct working_area
**area
)
1796 retval
= target_alloc_working_area_try(target
, size
, area
);
1797 if (retval
== ERROR_TARGET_RESOURCE_NOT_AVAILABLE
)
1798 LOG_WARNING("not enough working area available(requested %"PRIu32
")", size
);
1803 static int target_restore_working_area(struct target
*target
, struct working_area
*area
)
1805 int retval
= ERROR_OK
;
1807 if (target
->backup_working_area
&& area
->backup
!= NULL
) {
1808 retval
= target_write_memory(target
, area
->address
, 4, area
->size
/ 4, area
->backup
);
1809 if (retval
!= ERROR_OK
)
1810 LOG_ERROR("failed to restore %" PRIu32
" bytes of working area at address " TARGET_ADDR_FMT
,
1811 area
->size
, area
->address
);
1817 /* Restore the area's backup memory, if any, and return the area to the allocation pool */
1818 static int target_free_working_area_restore(struct target
*target
, struct working_area
*area
, int restore
)
1820 int retval
= ERROR_OK
;
1826 retval
= target_restore_working_area(target
, area
);
1827 /* REVISIT: Perhaps the area should be freed even if restoring fails. */
1828 if (retval
!= ERROR_OK
)
1834 LOG_DEBUG("freed %" PRIu32
" bytes of working area at address " TARGET_ADDR_FMT
,
1835 area
->size
, area
->address
);
1837 /* mark user pointer invalid */
1838 /* TODO: Is this really safe? It points to some previous caller's memory.
1839 * How could we know that the area pointer is still in that place and not
1840 * some other vital data? What's the purpose of this, anyway? */
1844 target_merge_working_areas(target
);
1846 print_wa_layout(target
);
1851 int target_free_working_area(struct target
*target
, struct working_area
*area
)
1853 return target_free_working_area_restore(target
, area
, 1);
1856 static void target_destroy(struct target
*target
)
1858 if (target
->type
->deinit_target
)
1859 target
->type
->deinit_target(target
);
1862 free(target
->trace_info
);
1863 free(target
->cmd_name
);
1867 void target_quit(void)
1869 struct target_event_callback
*pe
= target_event_callbacks
;
1871 struct target_event_callback
*t
= pe
->next
;
1875 target_event_callbacks
= NULL
;
1877 struct target_timer_callback
*pt
= target_timer_callbacks
;
1879 struct target_timer_callback
*t
= pt
->next
;
1883 target_timer_callbacks
= NULL
;
1885 for (struct target
*target
= all_targets
; target
;) {
1889 target_destroy(target
);
1896 /* free resources and restore memory, if restoring memory fails,
1897 * free up resources anyway
1899 static void target_free_all_working_areas_restore(struct target
*target
, int restore
)
1901 struct working_area
*c
= target
->working_areas
;
1903 LOG_DEBUG("freeing all working areas");
1905 /* Loop through all areas, restoring the allocated ones and marking them as free */
1909 target_restore_working_area(target
, c
);
1911 *c
->user
= NULL
; /* Same as above */
1917 /* Run a merge pass to combine all areas into one */
1918 target_merge_working_areas(target
);
1920 print_wa_layout(target
);
1923 void target_free_all_working_areas(struct target
*target
)
1925 target_free_all_working_areas_restore(target
, 1);
1928 /* Find the largest number of bytes that can be allocated */
1929 uint32_t target_get_working_area_avail(struct target
*target
)
1931 struct working_area
*c
= target
->working_areas
;
1932 uint32_t max_size
= 0;
1935 return target
->working_area_size
;
1938 if (c
->free
&& max_size
< c
->size
)
1947 int target_arch_state(struct target
*target
)
1950 if (target
== NULL
) {
1951 LOG_WARNING("No target has been configured");
1955 if (target
->state
!= TARGET_HALTED
)
1958 retval
= target
->type
->arch_state(target
);
1962 static int target_get_gdb_fileio_info_default(struct target
*target
,
1963 struct gdb_fileio_info
*fileio_info
)
1965 /* If target does not support semi-hosting function, target
1966 has no need to provide .get_gdb_fileio_info callback.
1967 It just return ERROR_FAIL and gdb_server will return "Txx"
1968 as target halted every time. */
1972 static int target_gdb_fileio_end_default(struct target
*target
,
1973 int retcode
, int fileio_errno
, bool ctrl_c
)
1978 static int target_profiling_default(struct target
*target
, uint32_t *samples
,
1979 uint32_t max_num_samples
, uint32_t *num_samples
, uint32_t seconds
)
1981 struct timeval timeout
, now
;
1983 gettimeofday(&timeout
, NULL
);
1984 timeval_add_time(&timeout
, seconds
, 0);
1986 LOG_INFO("Starting profiling. Halting and resuming the"
1987 " target as often as we can...");
1989 uint32_t sample_count
= 0;
1990 /* hopefully it is safe to cache! We want to stop/restart as quickly as possible. */
1991 struct reg
*reg
= register_get_by_name(target
->reg_cache
, "pc", 1);
1993 int retval
= ERROR_OK
;
1995 target_poll(target
);
1996 if (target
->state
== TARGET_HALTED
) {
1997 uint32_t t
= buf_get_u32(reg
->value
, 0, 32);
1998 samples
[sample_count
++] = t
;
1999 /* current pc, addr = 0, do not handle breakpoints, not debugging */
2000 retval
= target_resume(target
, 1, 0, 0, 0);
2001 target_poll(target
);
2002 alive_sleep(10); /* sleep 10ms, i.e. <100 samples/second. */
2003 } else if (target
->state
== TARGET_RUNNING
) {
2004 /* We want to quickly sample the PC. */
2005 retval
= target_halt(target
);
2007 LOG_INFO("Target not halted or running");
2012 if (retval
!= ERROR_OK
)
2015 gettimeofday(&now
, NULL
);
2016 if ((sample_count
>= max_num_samples
) || timeval_compare(&now
, &timeout
) >= 0) {
2017 LOG_INFO("Profiling completed. %" PRIu32
" samples.", sample_count
);
2022 *num_samples
= sample_count
;
2026 /* Single aligned words are guaranteed to use 16 or 32 bit access
2027 * mode respectively, otherwise data is handled as quickly as
2030 int target_write_buffer(struct target
*target
, target_addr_t address
, uint32_t size
, const uint8_t *buffer
)
2032 LOG_DEBUG("writing buffer of %" PRIi32
" byte at " TARGET_ADDR_FMT
,
2035 if (!target_was_examined(target
)) {
2036 LOG_ERROR("Target not examined yet");
2043 if ((address
+ size
- 1) < address
) {
2044 /* GDB can request this when e.g. PC is 0xfffffffc */
2045 LOG_ERROR("address + size wrapped (" TARGET_ADDR_FMT
", 0x%08" PRIx32
")",
2051 return target
->type
->write_buffer(target
, address
, size
, buffer
);
2054 static int target_write_buffer_default(struct target
*target
,
2055 target_addr_t address
, uint32_t count
, const uint8_t *buffer
)
2059 /* Align up to maximum 4 bytes. The loop condition makes sure the next pass
2060 * will have something to do with the size we leave to it. */
2061 for (size
= 1; size
< 4 && count
>= size
* 2 + (address
& size
); size
*= 2) {
2062 if (address
& size
) {
2063 int retval
= target_write_memory(target
, address
, size
, 1, buffer
);
2064 if (retval
!= ERROR_OK
)
2072 /* Write the data with as large access size as possible. */
2073 for (; size
> 0; size
/= 2) {
2074 uint32_t aligned
= count
- count
% size
;
2076 int retval
= target_write_memory(target
, address
, size
, aligned
/ size
, buffer
);
2077 if (retval
!= ERROR_OK
)
2088 /* Single aligned words are guaranteed to use 16 or 32 bit access
2089 * mode respectively, otherwise data is handled as quickly as
2092 int target_read_buffer(struct target
*target
, target_addr_t address
, uint32_t size
, uint8_t *buffer
)
2094 LOG_DEBUG("reading buffer of %" PRIi32
" byte at " TARGET_ADDR_FMT
,
2097 if (!target_was_examined(target
)) {
2098 LOG_ERROR("Target not examined yet");
2105 if ((address
+ size
- 1) < address
) {
2106 /* GDB can request this when e.g. PC is 0xfffffffc */
2107 LOG_ERROR("address + size wrapped (" TARGET_ADDR_FMT
", 0x%08" PRIx32
")",
2113 return target
->type
->read_buffer(target
, address
, size
, buffer
);
2116 static int target_read_buffer_default(struct target
*target
, target_addr_t address
, uint32_t count
, uint8_t *buffer
)
2120 /* Align up to maximum 4 bytes. The loop condition makes sure the next pass
2121 * will have something to do with the size we leave to it. */
2122 for (size
= 1; size
< 4 && count
>= size
* 2 + (address
& size
); size
*= 2) {
2123 if (address
& size
) {
2124 int retval
= target_read_memory(target
, address
, size
, 1, buffer
);
2125 if (retval
!= ERROR_OK
)
2133 /* Read the data with as large access size as possible. */
2134 for (; size
> 0; size
/= 2) {
2135 uint32_t aligned
= count
- count
% size
;
2137 int retval
= target_read_memory(target
, address
, size
, aligned
/ size
, buffer
);
2138 if (retval
!= ERROR_OK
)
2149 int target_checksum_memory(struct target
*target
, target_addr_t address
, uint32_t size
, uint32_t* crc
)
2154 uint32_t checksum
= 0;
2155 if (!target_was_examined(target
)) {
2156 LOG_ERROR("Target not examined yet");
2160 retval
= target
->type
->checksum_memory(target
, address
, size
, &checksum
);
2161 if (retval
!= ERROR_OK
) {
2162 buffer
= malloc(size
);
2163 if (buffer
== NULL
) {
2164 LOG_ERROR("error allocating buffer for section (%" PRId32
" bytes)", size
);
2165 return ERROR_COMMAND_SYNTAX_ERROR
;
2167 retval
= target_read_buffer(target
, address
, size
, buffer
);
2168 if (retval
!= ERROR_OK
) {
2173 /* convert to target endianness */
2174 for (i
= 0; i
< (size
/sizeof(uint32_t)); i
++) {
2175 uint32_t target_data
;
2176 target_data
= target_buffer_get_u32(target
, &buffer
[i
*sizeof(uint32_t)]);
2177 target_buffer_set_u32(target
, &buffer
[i
*sizeof(uint32_t)], target_data
);
2180 retval
= image_calculate_checksum(buffer
, size
, &checksum
);
2189 int target_blank_check_memory(struct target
*target
, target_addr_t address
, uint32_t size
, uint32_t* blank
,
2190 uint8_t erased_value
)
2193 if (!target_was_examined(target
)) {
2194 LOG_ERROR("Target not examined yet");
2198 if (target
->type
->blank_check_memory
== 0)
2199 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
2201 retval
= target
->type
->blank_check_memory(target
, address
, size
, blank
, erased_value
);
2206 int target_read_u64(struct target
*target
, target_addr_t address
, uint64_t *value
)
2208 uint8_t value_buf
[8];
2209 if (!target_was_examined(target
)) {
2210 LOG_ERROR("Target not examined yet");
2214 int retval
= target_read_memory(target
, address
, 8, 1, value_buf
);
2216 if (retval
== ERROR_OK
) {
2217 *value
= target_buffer_get_u64(target
, value_buf
);
2218 LOG_DEBUG("address: " TARGET_ADDR_FMT
", value: 0x%16.16" PRIx64
"",
2223 LOG_DEBUG("address: " TARGET_ADDR_FMT
" failed",
2230 int target_read_u32(struct target
*target
, target_addr_t address
, uint32_t *value
)
2232 uint8_t value_buf
[4];
2233 if (!target_was_examined(target
)) {
2234 LOG_ERROR("Target not examined yet");
2238 int retval
= target_read_memory(target
, address
, 4, 1, value_buf
);
2240 if (retval
== ERROR_OK
) {
2241 *value
= target_buffer_get_u32(target
, value_buf
);
2242 LOG_DEBUG("address: " TARGET_ADDR_FMT
", value: 0x%8.8" PRIx32
"",
2247 LOG_DEBUG("address: " TARGET_ADDR_FMT
" failed",
2254 int target_read_u16(struct target
*target
, target_addr_t address
, uint16_t *value
)
2256 uint8_t value_buf
[2];
2257 if (!target_was_examined(target
)) {
2258 LOG_ERROR("Target not examined yet");
2262 int retval
= target_read_memory(target
, address
, 2, 1, value_buf
);
2264 if (retval
== ERROR_OK
) {
2265 *value
= target_buffer_get_u16(target
, value_buf
);
2266 LOG_DEBUG("address: " TARGET_ADDR_FMT
", value: 0x%4.4" PRIx16
,
2271 LOG_DEBUG("address: " TARGET_ADDR_FMT
" failed",
2278 int target_read_u8(struct target
*target
, target_addr_t address
, uint8_t *value
)
2280 if (!target_was_examined(target
)) {
2281 LOG_ERROR("Target not examined yet");
2285 int retval
= target_read_memory(target
, address
, 1, 1, value
);
2287 if (retval
== ERROR_OK
) {
2288 LOG_DEBUG("address: " TARGET_ADDR_FMT
", value: 0x%2.2" PRIx8
,
2293 LOG_DEBUG("address: " TARGET_ADDR_FMT
" failed",
2300 int target_write_u64(struct target
*target
, target_addr_t address
, uint64_t value
)
2303 uint8_t value_buf
[8];
2304 if (!target_was_examined(target
)) {
2305 LOG_ERROR("Target not examined yet");
2309 LOG_DEBUG("address: " TARGET_ADDR_FMT
", value: 0x%16.16" PRIx64
"",
2313 target_buffer_set_u64(target
, value_buf
, value
);
2314 retval
= target_write_memory(target
, address
, 8, 1, value_buf
);
2315 if (retval
!= ERROR_OK
)
2316 LOG_DEBUG("failed: %i", retval
);
2321 int target_write_u32(struct target
*target
, target_addr_t address
, uint32_t value
)
2324 uint8_t value_buf
[4];
2325 if (!target_was_examined(target
)) {
2326 LOG_ERROR("Target not examined yet");
2330 LOG_DEBUG("address: " TARGET_ADDR_FMT
", value: 0x%8.8" PRIx32
"",
2334 target_buffer_set_u32(target
, value_buf
, value
);
2335 retval
= target_write_memory(target
, address
, 4, 1, value_buf
);
2336 if (retval
!= ERROR_OK
)
2337 LOG_DEBUG("failed: %i", retval
);
2342 int target_write_u16(struct target
*target
, target_addr_t address
, uint16_t value
)
2345 uint8_t value_buf
[2];
2346 if (!target_was_examined(target
)) {
2347 LOG_ERROR("Target not examined yet");
2351 LOG_DEBUG("address: " TARGET_ADDR_FMT
", value: 0x%8.8" PRIx16
,
2355 target_buffer_set_u16(target
, value_buf
, value
);
2356 retval
= target_write_memory(target
, address
, 2, 1, value_buf
);
2357 if (retval
!= ERROR_OK
)
2358 LOG_DEBUG("failed: %i", retval
);
2363 int target_write_u8(struct target
*target
, target_addr_t address
, uint8_t value
)
2366 if (!target_was_examined(target
)) {
2367 LOG_ERROR("Target not examined yet");
2371 LOG_DEBUG("address: " TARGET_ADDR_FMT
", value: 0x%2.2" PRIx8
,
2374 retval
= target_write_memory(target
, address
, 1, 1, &value
);
2375 if (retval
!= ERROR_OK
)
2376 LOG_DEBUG("failed: %i", retval
);
2381 int target_write_phys_u64(struct target
*target
, target_addr_t address
, uint64_t value
)
2384 uint8_t value_buf
[8];
2385 if (!target_was_examined(target
)) {
2386 LOG_ERROR("Target not examined yet");
2390 LOG_DEBUG("address: " TARGET_ADDR_FMT
", value: 0x%16.16" PRIx64
"",
2394 target_buffer_set_u64(target
, value_buf
, value
);
2395 retval
= target_write_phys_memory(target
, address
, 8, 1, value_buf
);
2396 if (retval
!= ERROR_OK
)
2397 LOG_DEBUG("failed: %i", retval
);
2402 int target_write_phys_u32(struct target
*target
, target_addr_t address
, uint32_t value
)
2405 uint8_t value_buf
[4];
2406 if (!target_was_examined(target
)) {
2407 LOG_ERROR("Target not examined yet");
2411 LOG_DEBUG("address: " TARGET_ADDR_FMT
", value: 0x%8.8" PRIx32
"",
2415 target_buffer_set_u32(target
, value_buf
, value
);
2416 retval
= target_write_phys_memory(target
, address
, 4, 1, value_buf
);
2417 if (retval
!= ERROR_OK
)
2418 LOG_DEBUG("failed: %i", retval
);
2423 int target_write_phys_u16(struct target
*target
, target_addr_t address
, uint16_t value
)
2426 uint8_t value_buf
[2];
2427 if (!target_was_examined(target
)) {
2428 LOG_ERROR("Target not examined yet");
2432 LOG_DEBUG("address: " TARGET_ADDR_FMT
", value: 0x%8.8" PRIx16
,
2436 target_buffer_set_u16(target
, value_buf
, value
);
2437 retval
= target_write_phys_memory(target
, address
, 2, 1, value_buf
);
2438 if (retval
!= ERROR_OK
)
2439 LOG_DEBUG("failed: %i", retval
);
2444 int target_write_phys_u8(struct target
*target
, target_addr_t address
, uint8_t value
)
2447 if (!target_was_examined(target
)) {
2448 LOG_ERROR("Target not examined yet");
2452 LOG_DEBUG("address: " TARGET_ADDR_FMT
", value: 0x%2.2" PRIx8
,
2455 retval
= target_write_phys_memory(target
, address
, 1, 1, &value
);
2456 if (retval
!= ERROR_OK
)
2457 LOG_DEBUG("failed: %i", retval
);
2462 static int find_target(struct command_context
*cmd_ctx
, const char *name
)
2464 struct target
*target
= get_target(name
);
2465 if (target
== NULL
) {
2466 LOG_ERROR("Target: %s is unknown, try one of:\n", name
);
2469 if (!target
->tap
->enabled
) {
2470 LOG_USER("Target: TAP %s is disabled, "
2471 "can't be the current target\n",
2472 target
->tap
->dotted_name
);
2476 cmd_ctx
->current_target
= target
->target_number
;
2481 COMMAND_HANDLER(handle_targets_command
)
2483 int retval
= ERROR_OK
;
2484 if (CMD_ARGC
== 1) {
2485 retval
= find_target(CMD_CTX
, CMD_ARGV
[0]);
2486 if (retval
== ERROR_OK
) {
2492 struct target
*target
= all_targets
;
2493 command_print(CMD_CTX
, " TargetName Type Endian TapName State ");
2494 command_print(CMD_CTX
, "-- ------------------ ---------- ------ ------------------ ------------");
2499 if (target
->tap
->enabled
)
2500 state
= target_state_name(target
);
2502 state
= "tap-disabled";
2504 if (CMD_CTX
->current_target
== target
->target_number
)
2507 /* keep columns lined up to match the headers above */
2508 command_print(CMD_CTX
,
2509 "%2d%c %-18s %-10s %-6s %-18s %s",
2510 target
->target_number
,
2512 target_name(target
),
2513 target_type_name(target
),
2514 Jim_Nvp_value2name_simple(nvp_target_endian
,
2515 target
->endianness
)->name
,
2516 target
->tap
->dotted_name
,
2518 target
= target
->next
;
2524 /* every 300ms we check for reset & powerdropout and issue a "reset halt" if so. */
2526 static int powerDropout
;
2527 static int srstAsserted
;
2529 static int runPowerRestore
;
2530 static int runPowerDropout
;
2531 static int runSrstAsserted
;
2532 static int runSrstDeasserted
;
2534 static int sense_handler(void)
2536 static int prevSrstAsserted
;
2537 static int prevPowerdropout
;
2539 int retval
= jtag_power_dropout(&powerDropout
);
2540 if (retval
!= ERROR_OK
)
2544 powerRestored
= prevPowerdropout
&& !powerDropout
;
2546 runPowerRestore
= 1;
2548 int64_t current
= timeval_ms();
2549 static int64_t lastPower
;
2550 bool waitMore
= lastPower
+ 2000 > current
;
2551 if (powerDropout
&& !waitMore
) {
2552 runPowerDropout
= 1;
2553 lastPower
= current
;
2556 retval
= jtag_srst_asserted(&srstAsserted
);
2557 if (retval
!= ERROR_OK
)
2561 srstDeasserted
= prevSrstAsserted
&& !srstAsserted
;
2563 static int64_t lastSrst
;
2564 waitMore
= lastSrst
+ 2000 > current
;
2565 if (srstDeasserted
&& !waitMore
) {
2566 runSrstDeasserted
= 1;
2570 if (!prevSrstAsserted
&& srstAsserted
)
2571 runSrstAsserted
= 1;
2573 prevSrstAsserted
= srstAsserted
;
2574 prevPowerdropout
= powerDropout
;
2576 if (srstDeasserted
|| powerRestored
) {
2577 /* Other than logging the event we can't do anything here.
2578 * Issuing a reset is a particularly bad idea as we might
2579 * be inside a reset already.
2586 /* process target state changes */
2587 static int handle_target(void *priv
)
2589 Jim_Interp
*interp
= (Jim_Interp
*)priv
;
2590 int retval
= ERROR_OK
;
2592 if (!is_jtag_poll_safe()) {
2593 /* polling is disabled currently */
2597 /* we do not want to recurse here... */
2598 static int recursive
;
2602 /* danger! running these procedures can trigger srst assertions and power dropouts.
2603 * We need to avoid an infinite loop/recursion here and we do that by
2604 * clearing the flags after running these events.
2606 int did_something
= 0;
2607 if (runSrstAsserted
) {
2608 LOG_INFO("srst asserted detected, running srst_asserted proc.");
2609 Jim_Eval(interp
, "srst_asserted");
2612 if (runSrstDeasserted
) {
2613 Jim_Eval(interp
, "srst_deasserted");
2616 if (runPowerDropout
) {
2617 LOG_INFO("Power dropout detected, running power_dropout proc.");
2618 Jim_Eval(interp
, "power_dropout");
2621 if (runPowerRestore
) {
2622 Jim_Eval(interp
, "power_restore");
2626 if (did_something
) {
2627 /* clear detect flags */
2631 /* clear action flags */
2633 runSrstAsserted
= 0;
2634 runSrstDeasserted
= 0;
2635 runPowerRestore
= 0;
2636 runPowerDropout
= 0;
2641 /* Poll targets for state changes unless that's globally disabled.
2642 * Skip targets that are currently disabled.
2644 for (struct target
*target
= all_targets
;
2645 is_jtag_poll_safe() && target
;
2646 target
= target
->next
) {
2648 if (!target_was_examined(target
))
2651 if (!target
->tap
->enabled
)
2654 if (target
->backoff
.times
> target
->backoff
.count
) {
2655 /* do not poll this time as we failed previously */
2656 target
->backoff
.count
++;
2659 target
->backoff
.count
= 0;
2661 /* only poll target if we've got power and srst isn't asserted */
2662 if (!powerDropout
&& !srstAsserted
) {
2663 /* polling may fail silently until the target has been examined */
2664 retval
= target_poll(target
);
2665 if (retval
!= ERROR_OK
) {
2666 /* 100ms polling interval. Increase interval between polling up to 5000ms */
2667 if (target
->backoff
.times
* polling_interval
< 5000) {
2668 target
->backoff
.times
*= 2;
2669 target
->backoff
.times
++;
2672 /* Tell GDB to halt the debugger. This allows the user to
2673 * run monitor commands to handle the situation.
2675 target_call_event_callbacks(target
, TARGET_EVENT_GDB_HALT
);
2677 if (target
->backoff
.times
> 0) {
2678 LOG_USER("Polling target %s failed, trying to reexamine", target_name(target
));
2679 target_reset_examined(target
);
2680 retval
= target_examine_one(target
);
2681 /* Target examination could have failed due to unstable connection,
2682 * but we set the examined flag anyway to repoll it later */
2683 if (retval
!= ERROR_OK
) {
2684 target
->examined
= true;
2685 LOG_USER("Examination failed, GDB will be halted. Polling again in %dms",
2686 target
->backoff
.times
* polling_interval
);
2691 /* Since we succeeded, we reset backoff count */
2692 target
->backoff
.times
= 0;
2699 COMMAND_HANDLER(handle_reg_command
)
2701 struct target
*target
;
2702 struct reg
*reg
= NULL
;
2708 target
= get_current_target(CMD_CTX
);
2710 /* list all available registers for the current target */
2711 if (CMD_ARGC
== 0) {
2712 struct reg_cache
*cache
= target
->reg_cache
;
2718 command_print(CMD_CTX
, "===== %s", cache
->name
);
2720 for (i
= 0, reg
= cache
->reg_list
;
2721 i
< cache
->num_regs
;
2722 i
++, reg
++, count
++) {
2723 /* only print cached values if they are valid */
2725 value
= buf_to_str(reg
->value
,
2727 command_print(CMD_CTX
,
2728 "(%i) %s (/%" PRIu32
"): 0x%s%s",
2736 command_print(CMD_CTX
, "(%i) %s (/%" PRIu32
")",
2741 cache
= cache
->next
;
2747 /* access a single register by its ordinal number */
2748 if ((CMD_ARGV
[0][0] >= '0') && (CMD_ARGV
[0][0] <= '9')) {
2750 COMMAND_PARSE_NUMBER(uint
, CMD_ARGV
[0], num
);
2752 struct reg_cache
*cache
= target
->reg_cache
;
2756 for (i
= 0; i
< cache
->num_regs
; i
++) {
2757 if (count
++ == num
) {
2758 reg
= &cache
->reg_list
[i
];
2764 cache
= cache
->next
;
2768 command_print(CMD_CTX
, "%i is out of bounds, the current target "
2769 "has only %i registers (0 - %i)", num
, count
, count
- 1);
2773 /* access a single register by its name */
2774 reg
= register_get_by_name(target
->reg_cache
, CMD_ARGV
[0], 1);
2777 command_print(CMD_CTX
, "register %s not found in current target", CMD_ARGV
[0]);
2782 assert(reg
!= NULL
); /* give clang a hint that we *know* reg is != NULL here */
2784 /* display a register */
2785 if ((CMD_ARGC
== 1) || ((CMD_ARGC
== 2) && !((CMD_ARGV
[1][0] >= '0')
2786 && (CMD_ARGV
[1][0] <= '9')))) {
2787 if ((CMD_ARGC
== 2) && (strcmp(CMD_ARGV
[1], "force") == 0))
2790 if (reg
->valid
== 0)
2791 reg
->type
->get(reg
);
2792 value
= buf_to_str(reg
->value
, reg
->size
, 16);
2793 command_print(CMD_CTX
, "%s (/%i): 0x%s", reg
->name
, (int)(reg
->size
), value
);
2798 /* set register value */
2799 if (CMD_ARGC
== 2) {
2800 uint8_t *buf
= malloc(DIV_ROUND_UP(reg
->size
, 8));
2803 str_to_buf(CMD_ARGV
[1], strlen(CMD_ARGV
[1]), buf
, reg
->size
, 0);
2805 reg
->type
->set(reg
, buf
);
2807 value
= buf_to_str(reg
->value
, reg
->size
, 16);
2808 command_print(CMD_CTX
, "%s (/%i): 0x%s", reg
->name
, (int)(reg
->size
), value
);
2816 return ERROR_COMMAND_SYNTAX_ERROR
;
2819 COMMAND_HANDLER(handle_poll_command
)
2821 int retval
= ERROR_OK
;
2822 struct target
*target
= get_current_target(CMD_CTX
);
2824 if (CMD_ARGC
== 0) {
2825 command_print(CMD_CTX
, "background polling: %s",
2826 jtag_poll_get_enabled() ? "on" : "off");
2827 command_print(CMD_CTX
, "TAP: %s (%s)",
2828 target
->tap
->dotted_name
,
2829 target
->tap
->enabled
? "enabled" : "disabled");
2830 if (!target
->tap
->enabled
)
2832 retval
= target_poll(target
);
2833 if (retval
!= ERROR_OK
)
2835 retval
= target_arch_state(target
);
2836 if (retval
!= ERROR_OK
)
2838 } else if (CMD_ARGC
== 1) {
2840 COMMAND_PARSE_ON_OFF(CMD_ARGV
[0], enable
);
2841 jtag_poll_set_enabled(enable
);
2843 return ERROR_COMMAND_SYNTAX_ERROR
;
2848 COMMAND_HANDLER(handle_wait_halt_command
)
2851 return ERROR_COMMAND_SYNTAX_ERROR
;
2853 unsigned ms
= DEFAULT_HALT_TIMEOUT
;
2854 if (1 == CMD_ARGC
) {
2855 int retval
= parse_uint(CMD_ARGV
[0], &ms
);
2856 if (ERROR_OK
!= retval
)
2857 return ERROR_COMMAND_SYNTAX_ERROR
;
2860 struct target
*target
= get_current_target(CMD_CTX
);
2861 return target_wait_state(target
, TARGET_HALTED
, ms
);
2864 /* wait for target state to change. The trick here is to have a low
2865 * latency for short waits and not to suck up all the CPU time
2868 * After 500ms, keep_alive() is invoked
2870 int target_wait_state(struct target
*target
, enum target_state state
, int ms
)
2873 int64_t then
= 0, cur
;
2877 retval
= target_poll(target
);
2878 if (retval
!= ERROR_OK
)
2880 if (target
->state
== state
)
2885 then
= timeval_ms();
2886 LOG_DEBUG("waiting for target %s...",
2887 Jim_Nvp_value2name_simple(nvp_target_state
, state
)->name
);
2893 if ((cur
-then
) > ms
) {
2894 LOG_ERROR("timed out while waiting for target %s",
2895 Jim_Nvp_value2name_simple(nvp_target_state
, state
)->name
);
2903 COMMAND_HANDLER(handle_halt_command
)
2907 struct target
*target
= get_current_target(CMD_CTX
);
2908 int retval
= target_halt(target
);
2909 if (ERROR_OK
!= retval
)
2912 if (CMD_ARGC
== 1) {
2913 unsigned wait_local
;
2914 retval
= parse_uint(CMD_ARGV
[0], &wait_local
);
2915 if (ERROR_OK
!= retval
)
2916 return ERROR_COMMAND_SYNTAX_ERROR
;
2921 return CALL_COMMAND_HANDLER(handle_wait_halt_command
);
2924 COMMAND_HANDLER(handle_soft_reset_halt_command
)
2926 struct target
*target
= get_current_target(CMD_CTX
);
2928 LOG_USER("requesting target halt and executing a soft reset");
2930 target_soft_reset_halt(target
);
2935 COMMAND_HANDLER(handle_reset_command
)
2938 return ERROR_COMMAND_SYNTAX_ERROR
;
2940 enum target_reset_mode reset_mode
= RESET_RUN
;
2941 if (CMD_ARGC
== 1) {
2943 n
= Jim_Nvp_name2value_simple(nvp_reset_modes
, CMD_ARGV
[0]);
2944 if ((n
->name
== NULL
) || (n
->value
== RESET_UNKNOWN
))
2945 return ERROR_COMMAND_SYNTAX_ERROR
;
2946 reset_mode
= n
->value
;
2949 /* reset *all* targets */
2950 return target_process_reset(CMD_CTX
, reset_mode
);
2954 COMMAND_HANDLER(handle_resume_command
)
2958 return ERROR_COMMAND_SYNTAX_ERROR
;
2960 struct target
*target
= get_current_target(CMD_CTX
);
2962 /* with no CMD_ARGV, resume from current pc, addr = 0,
2963 * with one arguments, addr = CMD_ARGV[0],
2964 * handle breakpoints, not debugging */
2965 target_addr_t addr
= 0;
2966 if (CMD_ARGC
== 1) {
2967 COMMAND_PARSE_ADDRESS(CMD_ARGV
[0], addr
);
2971 return target_resume(target
, current
, addr
, 1, 0);
2974 COMMAND_HANDLER(handle_step_command
)
2977 return ERROR_COMMAND_SYNTAX_ERROR
;
2981 /* with no CMD_ARGV, step from current pc, addr = 0,
2982 * with one argument addr = CMD_ARGV[0],
2983 * handle breakpoints, debugging */
2984 target_addr_t addr
= 0;
2986 if (CMD_ARGC
== 1) {
2987 COMMAND_PARSE_ADDRESS(CMD_ARGV
[0], addr
);
2991 struct target
*target
= get_current_target(CMD_CTX
);
2993 return target
->type
->step(target
, current_pc
, addr
, 1);
2996 static void handle_md_output(struct command_context
*cmd_ctx
,
2997 struct target
*target
, target_addr_t address
, unsigned size
,
2998 unsigned count
, const uint8_t *buffer
)
3000 const unsigned line_bytecnt
= 32;
3001 unsigned line_modulo
= line_bytecnt
/ size
;
3003 char output
[line_bytecnt
* 4 + 1];
3004 unsigned output_len
= 0;
3006 const char *value_fmt
;
3009 value_fmt
= "%16.16"PRIx64
" ";
3012 value_fmt
= "%8.8"PRIx64
" ";
3015 value_fmt
= "%4.4"PRIx64
" ";
3018 value_fmt
= "%2.2"PRIx64
" ";
3021 /* "can't happen", caller checked */
3022 LOG_ERROR("invalid memory read size: %u", size
);
3026 for (unsigned i
= 0; i
< count
; i
++) {
3027 if (i
% line_modulo
== 0) {
3028 output_len
+= snprintf(output
+ output_len
,
3029 sizeof(output
) - output_len
,
3030 TARGET_ADDR_FMT
": ",
3031 (address
+ (i
* size
)));
3035 const uint8_t *value_ptr
= buffer
+ i
* size
;
3038 value
= target_buffer_get_u64(target
, value_ptr
);
3041 value
= target_buffer_get_u32(target
, value_ptr
);
3044 value
= target_buffer_get_u16(target
, value_ptr
);
3049 output_len
+= snprintf(output
+ output_len
,
3050 sizeof(output
) - output_len
,
3053 if ((i
% line_modulo
== line_modulo
- 1) || (i
== count
- 1)) {
3054 command_print(cmd_ctx
, "%s", output
);
3060 COMMAND_HANDLER(handle_md_command
)
3063 return ERROR_COMMAND_SYNTAX_ERROR
;
3066 switch (CMD_NAME
[2]) {
3080 return ERROR_COMMAND_SYNTAX_ERROR
;
3083 bool physical
= strcmp(CMD_ARGV
[0], "phys") == 0;
3084 int (*fn
)(struct target
*target
,
3085 target_addr_t address
, uint32_t size_value
, uint32_t count
, uint8_t *buffer
);
3089 fn
= target_read_phys_memory
;
3091 fn
= target_read_memory
;
3092 if ((CMD_ARGC
< 1) || (CMD_ARGC
> 2))
3093 return ERROR_COMMAND_SYNTAX_ERROR
;
3095 target_addr_t address
;
3096 COMMAND_PARSE_ADDRESS(CMD_ARGV
[0], address
);
3100 COMMAND_PARSE_NUMBER(uint
, CMD_ARGV
[1], count
);
3102 uint8_t *buffer
= calloc(count
, size
);
3103 if (buffer
== NULL
) {
3104 LOG_ERROR("Failed to allocate md read buffer");
3108 struct target
*target
= get_current_target(CMD_CTX
);
3109 int retval
= fn(target
, address
, size
, count
, buffer
);
3110 if (ERROR_OK
== retval
)
3111 handle_md_output(CMD_CTX
, target
, address
, size
, count
, buffer
);
3118 typedef int (*target_write_fn
)(struct target
*target
,
3119 target_addr_t address
, uint32_t size
, uint32_t count
, const uint8_t *buffer
);
3121 static int target_fill_mem(struct target
*target
,
3122 target_addr_t address
,
3130 /* We have to write in reasonably large chunks to be able
3131 * to fill large memory areas with any sane speed */
3132 const unsigned chunk_size
= 16384;
3133 uint8_t *target_buf
= malloc(chunk_size
* data_size
);
3134 if (target_buf
== NULL
) {
3135 LOG_ERROR("Out of memory");
3139 for (unsigned i
= 0; i
< chunk_size
; i
++) {
3140 switch (data_size
) {
3142 target_buffer_set_u64(target
, target_buf
+ i
* data_size
, b
);
3145 target_buffer_set_u32(target
, target_buf
+ i
* data_size
, b
);
3148 target_buffer_set_u16(target
, target_buf
+ i
* data_size
, b
);
3151 target_buffer_set_u8(target
, target_buf
+ i
* data_size
, b
);
3158 int retval
= ERROR_OK
;
3160 for (unsigned x
= 0; x
< c
; x
+= chunk_size
) {
3163 if (current
> chunk_size
)
3164 current
= chunk_size
;
3165 retval
= fn(target
, address
+ x
* data_size
, data_size
, current
, target_buf
);
3166 if (retval
!= ERROR_OK
)
3168 /* avoid GDB timeouts */
3177 COMMAND_HANDLER(handle_mw_command
)
3180 return ERROR_COMMAND_SYNTAX_ERROR
;
3181 bool physical
= strcmp(CMD_ARGV
[0], "phys") == 0;
3186 fn
= target_write_phys_memory
;
3188 fn
= target_write_memory
;
3189 if ((CMD_ARGC
< 2) || (CMD_ARGC
> 3))
3190 return ERROR_COMMAND_SYNTAX_ERROR
;
3192 target_addr_t address
;
3193 COMMAND_PARSE_ADDRESS(CMD_ARGV
[0], address
);
3195 target_addr_t value
;
3196 COMMAND_PARSE_ADDRESS(CMD_ARGV
[1], value
);
3200 COMMAND_PARSE_NUMBER(uint
, CMD_ARGV
[2], count
);
3202 struct target
*target
= get_current_target(CMD_CTX
);
3204 switch (CMD_NAME
[2]) {
3218 return ERROR_COMMAND_SYNTAX_ERROR
;
3221 return target_fill_mem(target
, address
, fn
, wordsize
, value
, count
);
3224 static COMMAND_HELPER(parse_load_image_command_CMD_ARGV
, struct image
*image
,
3225 target_addr_t
*min_address
, target_addr_t
*max_address
)
3227 if (CMD_ARGC
< 1 || CMD_ARGC
> 5)
3228 return ERROR_COMMAND_SYNTAX_ERROR
;
3230 /* a base address isn't always necessary,
3231 * default to 0x0 (i.e. don't relocate) */
3232 if (CMD_ARGC
>= 2) {
3234 COMMAND_PARSE_ADDRESS(CMD_ARGV
[1], addr
);
3235 image
->base_address
= addr
;
3236 image
->base_address_set
= 1;
3238 image
->base_address_set
= 0;
3240 image
->start_address_set
= 0;
3243 COMMAND_PARSE_ADDRESS(CMD_ARGV
[3], *min_address
);
3244 if (CMD_ARGC
== 5) {
3245 COMMAND_PARSE_ADDRESS(CMD_ARGV
[4], *max_address
);
3246 /* use size (given) to find max (required) */
3247 *max_address
+= *min_address
;
3250 if (*min_address
> *max_address
)
3251 return ERROR_COMMAND_SYNTAX_ERROR
;
3256 COMMAND_HANDLER(handle_load_image_command
)
3260 uint32_t image_size
;
3261 target_addr_t min_address
= 0;
3262 target_addr_t max_address
= -1;
3266 int retval
= CALL_COMMAND_HANDLER(parse_load_image_command_CMD_ARGV
,
3267 &image
, &min_address
, &max_address
);
3268 if (ERROR_OK
!= retval
)
3271 struct target
*target
= get_current_target(CMD_CTX
);
3273 struct duration bench
;
3274 duration_start(&bench
);
3276 if (image_open(&image
, CMD_ARGV
[0], (CMD_ARGC
>= 3) ? CMD_ARGV
[2] : NULL
) != ERROR_OK
)
3281 for (i
= 0; i
< image
.num_sections
; i
++) {
3282 buffer
= malloc(image
.sections
[i
].size
);
3283 if (buffer
== NULL
) {
3284 command_print(CMD_CTX
,
3285 "error allocating buffer for section (%d bytes)",
3286 (int)(image
.sections
[i
].size
));
3287 retval
= ERROR_FAIL
;
3291 retval
= image_read_section(&image
, i
, 0x0, image
.sections
[i
].size
, buffer
, &buf_cnt
);
3292 if (retval
!= ERROR_OK
) {
3297 uint32_t offset
= 0;
3298 uint32_t length
= buf_cnt
;
3300 /* DANGER!!! beware of unsigned comparision here!!! */
3302 if ((image
.sections
[i
].base_address
+ buf_cnt
>= min_address
) &&
3303 (image
.sections
[i
].base_address
< max_address
)) {
3305 if (image
.sections
[i
].base_address
< min_address
) {
3306 /* clip addresses below */
3307 offset
+= min_address
-image
.sections
[i
].base_address
;
3311 if (image
.sections
[i
].base_address
+ buf_cnt
> max_address
)
3312 length
-= (image
.sections
[i
].base_address
+ buf_cnt
)-max_address
;
3314 retval
= target_write_buffer(target
,
3315 image
.sections
[i
].base_address
+ offset
, length
, buffer
+ offset
);
3316 if (retval
!= ERROR_OK
) {
3320 image_size
+= length
;
3321 command_print(CMD_CTX
, "%u bytes written at address " TARGET_ADDR_FMT
"",
3322 (unsigned int)length
,
3323 image
.sections
[i
].base_address
+ offset
);
3329 if ((ERROR_OK
== retval
) && (duration_measure(&bench
) == ERROR_OK
)) {
3330 command_print(CMD_CTX
, "downloaded %" PRIu32
" bytes "
3331 "in %fs (%0.3f KiB/s)", image_size
,
3332 duration_elapsed(&bench
), duration_kbps(&bench
, image_size
));
3335 image_close(&image
);
3341 COMMAND_HANDLER(handle_dump_image_command
)
3343 struct fileio
*fileio
;
3345 int retval
, retvaltemp
;
3346 target_addr_t address
, size
;
3347 struct duration bench
;
3348 struct target
*target
= get_current_target(CMD_CTX
);
3351 return ERROR_COMMAND_SYNTAX_ERROR
;
3353 COMMAND_PARSE_ADDRESS(CMD_ARGV
[1], address
);
3354 COMMAND_PARSE_ADDRESS(CMD_ARGV
[2], size
);
3356 uint32_t buf_size
= (size
> 4096) ? 4096 : size
;
3357 buffer
= malloc(buf_size
);
3361 retval
= fileio_open(&fileio
, CMD_ARGV
[0], FILEIO_WRITE
, FILEIO_BINARY
);
3362 if (retval
!= ERROR_OK
) {
3367 duration_start(&bench
);
3370 size_t size_written
;
3371 uint32_t this_run_size
= (size
> buf_size
) ? buf_size
: size
;
3372 retval
= target_read_buffer(target
, address
, this_run_size
, buffer
);
3373 if (retval
!= ERROR_OK
)
3376 retval
= fileio_write(fileio
, this_run_size
, buffer
, &size_written
);
3377 if (retval
!= ERROR_OK
)
3380 size
-= this_run_size
;
3381 address
+= this_run_size
;
3386 if ((ERROR_OK
== retval
) && (duration_measure(&bench
) == ERROR_OK
)) {
3388 retval
= fileio_size(fileio
, &filesize
);
3389 if (retval
!= ERROR_OK
)
3391 command_print(CMD_CTX
,
3392 "dumped %zu bytes in %fs (%0.3f KiB/s)", filesize
,
3393 duration_elapsed(&bench
), duration_kbps(&bench
, filesize
));
3396 retvaltemp
= fileio_close(fileio
);
3397 if (retvaltemp
!= ERROR_OK
)
3406 IMAGE_CHECKSUM_ONLY
= 2
3409 static COMMAND_HELPER(handle_verify_image_command_internal
, enum verify_mode verify
)
3413 uint32_t image_size
;
3416 uint32_t checksum
= 0;
3417 uint32_t mem_checksum
= 0;
3421 struct target
*target
= get_current_target(CMD_CTX
);
3424 return ERROR_COMMAND_SYNTAX_ERROR
;
3427 LOG_ERROR("no target selected");
3431 struct duration bench
;
3432 duration_start(&bench
);
3434 if (CMD_ARGC
>= 2) {
3436 COMMAND_PARSE_ADDRESS(CMD_ARGV
[1], addr
);
3437 image
.base_address
= addr
;
3438 image
.base_address_set
= 1;
3440 image
.base_address_set
= 0;
3441 image
.base_address
= 0x0;
3444 image
.start_address_set
= 0;
3446 retval
= image_open(&image
, CMD_ARGV
[0], (CMD_ARGC
== 3) ? CMD_ARGV
[2] : NULL
);
3447 if (retval
!= ERROR_OK
)
3453 for (i
= 0; i
< image
.num_sections
; i
++) {
3454 buffer
= malloc(image
.sections
[i
].size
);
3455 if (buffer
== NULL
) {
3456 command_print(CMD_CTX
,
3457 "error allocating buffer for section (%d bytes)",
3458 (int)(image
.sections
[i
].size
));
3461 retval
= image_read_section(&image
, i
, 0x0, image
.sections
[i
].size
, buffer
, &buf_cnt
);
3462 if (retval
!= ERROR_OK
) {
3467 if (verify
>= IMAGE_VERIFY
) {
3468 /* calculate checksum of image */
3469 retval
= image_calculate_checksum(buffer
, buf_cnt
, &checksum
);
3470 if (retval
!= ERROR_OK
) {
3475 retval
= target_checksum_memory(target
, image
.sections
[i
].base_address
, buf_cnt
, &mem_checksum
);
3476 if (retval
!= ERROR_OK
) {
3480 if ((checksum
!= mem_checksum
) && (verify
== IMAGE_CHECKSUM_ONLY
)) {
3481 LOG_ERROR("checksum mismatch");
3483 retval
= ERROR_FAIL
;
3486 if (checksum
!= mem_checksum
) {
3487 /* failed crc checksum, fall back to a binary compare */
3491 LOG_ERROR("checksum mismatch - attempting binary compare");
3493 data
= malloc(buf_cnt
);
3495 /* Can we use 32bit word accesses? */
3497 int count
= buf_cnt
;
3498 if ((count
% 4) == 0) {
3502 retval
= target_read_memory(target
, image
.sections
[i
].base_address
, size
, count
, data
);
3503 if (retval
== ERROR_OK
) {
3505 for (t
= 0; t
< buf_cnt
; t
++) {
3506 if (data
[t
] != buffer
[t
]) {
3507 command_print(CMD_CTX
,
3508 "diff %d address 0x%08x. Was 0x%02x instead of 0x%02x",
3510 (unsigned)(t
+ image
.sections
[i
].base_address
),
3513 if (diffs
++ >= 127) {
3514 command_print(CMD_CTX
, "More than 128 errors, the rest are not printed.");
3526 command_print(CMD_CTX
, "address " TARGET_ADDR_FMT
" length 0x%08zx",
3527 image
.sections
[i
].base_address
,
3532 image_size
+= buf_cnt
;
3535 command_print(CMD_CTX
, "No more differences found.");
3538 retval
= ERROR_FAIL
;
3539 if ((ERROR_OK
== retval
) && (duration_measure(&bench
) == ERROR_OK
)) {
3540 command_print(CMD_CTX
, "verified %" PRIu32
" bytes "
3541 "in %fs (%0.3f KiB/s)", image_size
,
3542 duration_elapsed(&bench
), duration_kbps(&bench
, image_size
));
3545 image_close(&image
);
3550 COMMAND_HANDLER(handle_verify_image_checksum_command
)
3552 return CALL_COMMAND_HANDLER(handle_verify_image_command_internal
, IMAGE_CHECKSUM_ONLY
);
3555 COMMAND_HANDLER(handle_verify_image_command
)
3557 return CALL_COMMAND_HANDLER(handle_verify_image_command_internal
, IMAGE_VERIFY
);
3560 COMMAND_HANDLER(handle_test_image_command
)
3562 return CALL_COMMAND_HANDLER(handle_verify_image_command_internal
, IMAGE_TEST
);
3565 static int handle_bp_command_list(struct command_context
*cmd_ctx
)
3567 struct target
*target
= get_current_target(cmd_ctx
);
3568 struct breakpoint
*breakpoint
= target
->breakpoints
;
3569 while (breakpoint
) {
3570 if (breakpoint
->type
== BKPT_SOFT
) {
3571 char *buf
= buf_to_str(breakpoint
->orig_instr
,
3572 breakpoint
->length
, 16);
3573 command_print(cmd_ctx
, "IVA breakpoint: " TARGET_ADDR_FMT
", 0x%x, %i, 0x%s",
3574 breakpoint
->address
,
3576 breakpoint
->set
, buf
);
3579 if ((breakpoint
->address
== 0) && (breakpoint
->asid
!= 0))
3580 command_print(cmd_ctx
, "Context breakpoint: 0x%8.8" PRIx32
", 0x%x, %i",
3582 breakpoint
->length
, breakpoint
->set
);
3583 else if ((breakpoint
->address
!= 0) && (breakpoint
->asid
!= 0)) {
3584 command_print(cmd_ctx
, "Hybrid breakpoint(IVA): " TARGET_ADDR_FMT
", 0x%x, %i",
3585 breakpoint
->address
,
3586 breakpoint
->length
, breakpoint
->set
);
3587 command_print(cmd_ctx
, "\t|--->linked with ContextID: 0x%8.8" PRIx32
,
3590 command_print(cmd_ctx
, "Breakpoint(IVA): " TARGET_ADDR_FMT
", 0x%x, %i",
3591 breakpoint
->address
,
3592 breakpoint
->length
, breakpoint
->set
);
3595 breakpoint
= breakpoint
->next
;
3600 static int handle_bp_command_set(struct command_context
*cmd_ctx
,
3601 target_addr_t addr
, uint32_t asid
, uint32_t length
, int hw
)
3603 struct target
*target
= get_current_target(cmd_ctx
);
3607 retval
= breakpoint_add(target
, addr
, length
, hw
);
3608 if (ERROR_OK
== retval
)
3609 command_print(cmd_ctx
, "breakpoint set at " TARGET_ADDR_FMT
"", addr
);
3611 LOG_ERROR("Failure setting breakpoint, the same address(IVA) is already used");
3614 } else if (addr
== 0) {
3615 if (target
->type
->add_context_breakpoint
== NULL
) {
3616 LOG_WARNING("Context breakpoint not available");
3619 retval
= context_breakpoint_add(target
, asid
, length
, hw
);
3620 if (ERROR_OK
== retval
)
3621 command_print(cmd_ctx
, "Context breakpoint set at 0x%8.8" PRIx32
"", asid
);
3623 LOG_ERROR("Failure setting breakpoint, the same address(CONTEXTID) is already used");
3627 if (target
->type
->add_hybrid_breakpoint
== NULL
) {
3628 LOG_WARNING("Hybrid breakpoint not available");
3631 retval
= hybrid_breakpoint_add(target
, addr
, asid
, length
, hw
);
3632 if (ERROR_OK
== retval
)
3633 command_print(cmd_ctx
, "Hybrid breakpoint set at 0x%8.8" PRIx32
"", asid
);
3635 LOG_ERROR("Failure setting breakpoint, the same address is already used");
3642 COMMAND_HANDLER(handle_bp_command
)
3651 return handle_bp_command_list(CMD_CTX
);
3655 COMMAND_PARSE_ADDRESS(CMD_ARGV
[0], addr
);
3656 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], length
);
3657 return handle_bp_command_set(CMD_CTX
, addr
, asid
, length
, hw
);
3660 if (strcmp(CMD_ARGV
[2], "hw") == 0) {
3662 COMMAND_PARSE_ADDRESS(CMD_ARGV
[0], addr
);
3663 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], length
);
3665 return handle_bp_command_set(CMD_CTX
, addr
, asid
, length
, hw
);
3666 } else if (strcmp(CMD_ARGV
[2], "hw_ctx") == 0) {
3668 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], asid
);
3669 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], length
);
3671 return handle_bp_command_set(CMD_CTX
, addr
, asid
, length
, hw
);
3676 COMMAND_PARSE_ADDRESS(CMD_ARGV
[0], addr
);
3677 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], asid
);
3678 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[2], length
);
3679 return handle_bp_command_set(CMD_CTX
, addr
, asid
, length
, hw
);
3682 return ERROR_COMMAND_SYNTAX_ERROR
;
3686 COMMAND_HANDLER(handle_rbp_command
)
3689 return ERROR_COMMAND_SYNTAX_ERROR
;
3692 COMMAND_PARSE_ADDRESS(CMD_ARGV
[0], addr
);
3694 struct target
*target
= get_current_target(CMD_CTX
);
3695 breakpoint_remove(target
, addr
);
3700 COMMAND_HANDLER(handle_wp_command
)
3702 struct target
*target
= get_current_target(CMD_CTX
);
3704 if (CMD_ARGC
== 0) {
3705 struct watchpoint
*watchpoint
= target
->watchpoints
;
3707 while (watchpoint
) {
3708 command_print(CMD_CTX
, "address: " TARGET_ADDR_FMT
3709 ", len: 0x%8.8" PRIx32
3710 ", r/w/a: %i, value: 0x%8.8" PRIx32
3711 ", mask: 0x%8.8" PRIx32
,
3712 watchpoint
->address
,
3714 (int)watchpoint
->rw
,
3717 watchpoint
= watchpoint
->next
;
3722 enum watchpoint_rw type
= WPT_ACCESS
;
3724 uint32_t length
= 0;
3725 uint32_t data_value
= 0x0;
3726 uint32_t data_mask
= 0xffffffff;
3730 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[4], data_mask
);
3733 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[3], data_value
);
3736 switch (CMD_ARGV
[2][0]) {
3747 LOG_ERROR("invalid watchpoint mode ('%c')", CMD_ARGV
[2][0]);
3748 return ERROR_COMMAND_SYNTAX_ERROR
;
3752 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], length
);
3753 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], addr
);
3757 return ERROR_COMMAND_SYNTAX_ERROR
;
3760 int retval
= watchpoint_add(target
, addr
, length
, type
,
3761 data_value
, data_mask
);
3762 if (ERROR_OK
!= retval
)
3763 LOG_ERROR("Failure setting watchpoints");
3768 COMMAND_HANDLER(handle_rwp_command
)
3771 return ERROR_COMMAND_SYNTAX_ERROR
;
3774 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], addr
);
3776 struct target
*target
= get_current_target(CMD_CTX
);
3777 watchpoint_remove(target
, addr
);
3783 * Translate a virtual address to a physical address.
3785 * The low-level target implementation must have logged a detailed error
3786 * which is forwarded to telnet/GDB session.
3788 COMMAND_HANDLER(handle_virt2phys_command
)
3791 return ERROR_COMMAND_SYNTAX_ERROR
;
3794 COMMAND_PARSE_ADDRESS(CMD_ARGV
[0], va
);
3797 struct target
*target
= get_current_target(CMD_CTX
);
3798 int retval
= target
->type
->virt2phys(target
, va
, &pa
);
3799 if (retval
== ERROR_OK
)
3800 command_print(CMD_CTX
, "Physical address " TARGET_ADDR_FMT
"", pa
);
3805 static void writeData(FILE *f
, const void *data
, size_t len
)
3807 size_t written
= fwrite(data
, 1, len
, f
);
3809 LOG_ERROR("failed to write %zu bytes: %s", len
, strerror(errno
));
3812 static void writeLong(FILE *f
, int l
, struct target
*target
)
3816 target_buffer_set_u32(target
, val
, l
);
3817 writeData(f
, val
, 4);
3820 static void writeString(FILE *f
, char *s
)
3822 writeData(f
, s
, strlen(s
));
3825 typedef unsigned char UNIT
[2]; /* unit of profiling */
3827 /* Dump a gmon.out histogram file. */
3828 static void write_gmon(uint32_t *samples
, uint32_t sampleNum
, const char *filename
, bool with_range
,
3829 uint32_t start_address
, uint32_t end_address
, struct target
*target
, uint32_t duration_ms
)
3832 FILE *f
= fopen(filename
, "w");
3835 writeString(f
, "gmon");
3836 writeLong(f
, 0x00000001, target
); /* Version */
3837 writeLong(f
, 0, target
); /* padding */
3838 writeLong(f
, 0, target
); /* padding */
3839 writeLong(f
, 0, target
); /* padding */
3841 uint8_t zero
= 0; /* GMON_TAG_TIME_HIST */
3842 writeData(f
, &zero
, 1);
3844 /* figure out bucket size */
3848 min
= start_address
;
3853 for (i
= 0; i
< sampleNum
; i
++) {
3854 if (min
> samples
[i
])
3856 if (max
< samples
[i
])
3860 /* max should be (largest sample + 1)
3861 * Refer to binutils/gprof/hist.c (find_histogram_for_pc) */
3865 int addressSpace
= max
- min
;
3866 assert(addressSpace
>= 2);
3868 /* FIXME: What is the reasonable number of buckets?
3869 * The profiling result will be more accurate if there are enough buckets. */
3870 static const uint32_t maxBuckets
= 128 * 1024; /* maximum buckets. */
3871 uint32_t numBuckets
= addressSpace
/ sizeof(UNIT
);
3872 if (numBuckets
> maxBuckets
)
3873 numBuckets
= maxBuckets
;
3874 int *buckets
= malloc(sizeof(int) * numBuckets
);
3875 if (buckets
== NULL
) {
3879 memset(buckets
, 0, sizeof(int) * numBuckets
);
3880 for (i
= 0; i
< sampleNum
; i
++) {
3881 uint32_t address
= samples
[i
];
3883 if ((address
< min
) || (max
<= address
))
3886 long long a
= address
- min
;
3887 long long b
= numBuckets
;
3888 long long c
= addressSpace
;
3889 int index_t
= (a
* b
) / c
; /* danger!!!! int32 overflows */
3893 /* append binary memory gmon.out &profile_hist_hdr ((char*)&profile_hist_hdr + sizeof(struct gmon_hist_hdr)) */
3894 writeLong(f
, min
, target
); /* low_pc */
3895 writeLong(f
, max
, target
); /* high_pc */
3896 writeLong(f
, numBuckets
, target
); /* # of buckets */
3897 float sample_rate
= sampleNum
/ (duration_ms
/ 1000.0);
3898 writeLong(f
, sample_rate
, target
);
3899 writeString(f
, "seconds");
3900 for (i
= 0; i
< (15-strlen("seconds")); i
++)
3901 writeData(f
, &zero
, 1);
3902 writeString(f
, "s");
3904 /*append binary memory gmon.out profile_hist_data (profile_hist_data + profile_hist_hdr.hist_size) */
3906 char *data
= malloc(2 * numBuckets
);
3908 for (i
= 0; i
< numBuckets
; i
++) {
3913 data
[i
* 2] = val
&0xff;
3914 data
[i
* 2 + 1] = (val
>> 8) & 0xff;
3917 writeData(f
, data
, numBuckets
* 2);
3925 /* profiling samples the CPU PC as quickly as OpenOCD is able,
3926 * which will be used as a random sampling of PC */
3927 COMMAND_HANDLER(handle_profile_command
)
3929 struct target
*target
= get_current_target(CMD_CTX
);
3931 if ((CMD_ARGC
!= 2) && (CMD_ARGC
!= 4))
3932 return ERROR_COMMAND_SYNTAX_ERROR
;
3934 const uint32_t MAX_PROFILE_SAMPLE_NUM
= 10000;
3936 uint32_t num_of_samples
;
3937 int retval
= ERROR_OK
;
3939 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], offset
);
3941 uint32_t *samples
= malloc(sizeof(uint32_t) * MAX_PROFILE_SAMPLE_NUM
);
3942 if (samples
== NULL
) {
3943 LOG_ERROR("No memory to store samples.");
3947 uint64_t timestart_ms
= timeval_ms();
3949 * Some cores let us sample the PC without the
3950 * annoying halt/resume step; for example, ARMv7 PCSR.
3951 * Provide a way to use that more efficient mechanism.
3953 retval
= target_profiling(target
, samples
, MAX_PROFILE_SAMPLE_NUM
,
3954 &num_of_samples
, offset
);
3955 if (retval
!= ERROR_OK
) {
3959 uint32_t duration_ms
= timeval_ms() - timestart_ms
;
3961 assert(num_of_samples
<= MAX_PROFILE_SAMPLE_NUM
);
3963 retval
= target_poll(target
);
3964 if (retval
!= ERROR_OK
) {
3968 if (target
->state
== TARGET_RUNNING
) {
3969 retval
= target_halt(target
);
3970 if (retval
!= ERROR_OK
) {
3976 retval
= target_poll(target
);
3977 if (retval
!= ERROR_OK
) {
3982 uint32_t start_address
= 0;
3983 uint32_t end_address
= 0;
3984 bool with_range
= false;
3985 if (CMD_ARGC
== 4) {
3987 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[2], start_address
);
3988 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[3], end_address
);
3991 write_gmon(samples
, num_of_samples
, CMD_ARGV
[1],
3992 with_range
, start_address
, end_address
, target
, duration_ms
);
3993 command_print(CMD_CTX
, "Wrote %s", CMD_ARGV
[1]);
3999 static int new_int_array_element(Jim_Interp
*interp
, const char *varname
, int idx
, uint32_t val
)
4002 Jim_Obj
*nameObjPtr
, *valObjPtr
;
4005 namebuf
= alloc_printf("%s(%d)", varname
, idx
);
4009 nameObjPtr
= Jim_NewStringObj(interp
, namebuf
, -1);
4010 valObjPtr
= Jim_NewIntObj(interp
, val
);
4011 if (!nameObjPtr
|| !valObjPtr
) {
4016 Jim_IncrRefCount(nameObjPtr
);
4017 Jim_IncrRefCount(valObjPtr
);
4018 result
= Jim_SetVariable(interp
, nameObjPtr
, valObjPtr
);
4019 Jim_DecrRefCount(interp
, nameObjPtr
);
4020 Jim_DecrRefCount(interp
, valObjPtr
);
4022 /* printf("%s(%d) <= 0%08x\n", varname, idx, val); */
4026 static int jim_mem2array(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4028 struct command_context
*context
;
4029 struct target
*target
;
4031 context
= current_command_context(interp
);
4032 assert(context
!= NULL
);
4034 target
= get_current_target(context
);
4035 if (target
== NULL
) {
4036 LOG_ERROR("mem2array: no current target");
4040 return target_mem2array(interp
, target
, argc
- 1, argv
+ 1);
4043 static int target_mem2array(Jim_Interp
*interp
, struct target
*target
, int argc
, Jim_Obj
*const *argv
)
4051 const char *varname
;
4057 /* argv[1] = name of array to receive the data
4058 * argv[2] = desired width
4059 * argv[3] = memory address
4060 * argv[4] = count of times to read
4062 if (argc
< 4 || argc
> 5) {
4063 Jim_WrongNumArgs(interp
, 1, argv
, "varname width addr nelems [phys]");
4066 varname
= Jim_GetString(argv
[0], &len
);
4067 /* given "foo" get space for worse case "foo(%d)" .. add 20 */
4069 e
= Jim_GetLong(interp
, argv
[1], &l
);
4074 e
= Jim_GetLong(interp
, argv
[2], &l
);
4078 e
= Jim_GetLong(interp
, argv
[3], &l
);
4084 phys
= Jim_GetString(argv
[4], &n
);
4085 if (!strncmp(phys
, "phys", n
))
4101 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
4102 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "Invalid width param, must be 8/16/32", NULL
);
4106 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
4107 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "mem2array: zero width read?", NULL
);
4110 if ((addr
+ (len
* width
)) < addr
) {
4111 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
4112 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "mem2array: addr + len - wraps to zero?", NULL
);
4115 /* absurd transfer size? */
4117 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
4118 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "mem2array: absurd > 64K item request", NULL
);
4123 ((width
== 2) && ((addr
& 1) == 0)) ||
4124 ((width
== 4) && ((addr
& 3) == 0))) {
4128 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
4129 sprintf(buf
, "mem2array address: 0x%08" PRIx32
" is not aligned for %" PRId32
" byte reads",
4132 Jim_AppendStrings(interp
, Jim_GetResult(interp
), buf
, NULL
);
4141 size_t buffersize
= 4096;
4142 uint8_t *buffer
= malloc(buffersize
);
4149 /* Slurp... in buffer size chunks */
4151 count
= len
; /* in objects.. */
4152 if (count
> (buffersize
/ width
))
4153 count
= (buffersize
/ width
);
4156 retval
= target_read_phys_memory(target
, addr
, width
, count
, buffer
);
4158 retval
= target_read_memory(target
, addr
, width
, count
, buffer
);
4159 if (retval
!= ERROR_OK
) {
4161 LOG_ERROR("mem2array: Read @ 0x%08" PRIx32
", w=%" PRId32
", cnt=%" PRId32
", failed",
4165 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
4166 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "mem2array: cannot read memory", NULL
);
4170 v
= 0; /* shut up gcc */
4171 for (i
= 0; i
< count
; i
++, n
++) {
4174 v
= target_buffer_get_u32(target
, &buffer
[i
*width
]);
4177 v
= target_buffer_get_u16(target
, &buffer
[i
*width
]);
4180 v
= buffer
[i
] & 0x0ff;
4183 new_int_array_element(interp
, varname
, n
, v
);
4186 addr
+= count
* width
;
4192 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
4197 static int get_int_array_element(Jim_Interp
*interp
, const char *varname
, int idx
, uint32_t *val
)
4200 Jim_Obj
*nameObjPtr
, *valObjPtr
;
4204 namebuf
= alloc_printf("%s(%d)", varname
, idx
);
4208 nameObjPtr
= Jim_NewStringObj(interp
, namebuf
, -1);
4214 Jim_IncrRefCount(nameObjPtr
);
4215 valObjPtr
= Jim_GetVariable(interp
, nameObjPtr
, JIM_ERRMSG
);
4216 Jim_DecrRefCount(interp
, nameObjPtr
);
4218 if (valObjPtr
== NULL
)
4221 result
= Jim_GetLong(interp
, valObjPtr
, &l
);
4222 /* printf("%s(%d) => 0%08x\n", varname, idx, val); */
4227 static int jim_array2mem(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4229 struct command_context
*context
;
4230 struct target
*target
;
4232 context
= current_command_context(interp
);
4233 assert(context
!= NULL
);
4235 target
= get_current_target(context
);
4236 if (target
== NULL
) {
4237 LOG_ERROR("array2mem: no current target");
4241 return target_array2mem(interp
, target
, argc
-1, argv
+ 1);
4244 static int target_array2mem(Jim_Interp
*interp
, struct target
*target
,
4245 int argc
, Jim_Obj
*const *argv
)
4253 const char *varname
;
4259 /* argv[1] = name of array to get the data
4260 * argv[2] = desired width
4261 * argv[3] = memory address
4262 * argv[4] = count to write
4264 if (argc
< 4 || argc
> 5) {
4265 Jim_WrongNumArgs(interp
, 0, argv
, "varname width addr nelems [phys]");
4268 varname
= Jim_GetString(argv
[0], &len
);
4269 /* given "foo" get space for worse case "foo(%d)" .. add 20 */
4271 e
= Jim_GetLong(interp
, argv
[1], &l
);
4276 e
= Jim_GetLong(interp
, argv
[2], &l
);
4280 e
= Jim_GetLong(interp
, argv
[3], &l
);
4286 phys
= Jim_GetString(argv
[4], &n
);
4287 if (!strncmp(phys
, "phys", n
))
4303 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
4304 Jim_AppendStrings(interp
, Jim_GetResult(interp
),
4305 "Invalid width param, must be 8/16/32", NULL
);
4309 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
4310 Jim_AppendStrings(interp
, Jim_GetResult(interp
),
4311 "array2mem: zero width read?", NULL
);
4314 if ((addr
+ (len
* width
)) < addr
) {
4315 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
4316 Jim_AppendStrings(interp
, Jim_GetResult(interp
),
4317 "array2mem: addr + len - wraps to zero?", NULL
);
4320 /* absurd transfer size? */
4322 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
4323 Jim_AppendStrings(interp
, Jim_GetResult(interp
),
4324 "array2mem: absurd > 64K item request", NULL
);
4329 ((width
== 2) && ((addr
& 1) == 0)) ||
4330 ((width
== 4) && ((addr
& 3) == 0))) {
4334 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
4335 sprintf(buf
, "array2mem address: 0x%08" PRIx32
" is not aligned for %" PRId32
" byte reads",
4338 Jim_AppendStrings(interp
, Jim_GetResult(interp
), buf
, NULL
);
4349 size_t buffersize
= 4096;
4350 uint8_t *buffer
= malloc(buffersize
);
4355 /* Slurp... in buffer size chunks */
4357 count
= len
; /* in objects.. */
4358 if (count
> (buffersize
/ width
))
4359 count
= (buffersize
/ width
);
4361 v
= 0; /* shut up gcc */
4362 for (i
= 0; i
< count
; i
++, n
++) {
4363 get_int_array_element(interp
, varname
, n
, &v
);
4366 target_buffer_set_u32(target
, &buffer
[i
* width
], v
);
4369 target_buffer_set_u16(target
, &buffer
[i
* width
], v
);
4372 buffer
[i
] = v
& 0x0ff;
4379 retval
= target_write_phys_memory(target
, addr
, width
, count
, buffer
);
4381 retval
= target_write_memory(target
, addr
, width
, count
, buffer
);
4382 if (retval
!= ERROR_OK
) {
4384 LOG_ERROR("array2mem: Write @ 0x%08" PRIx32
", w=%" PRId32
", cnt=%" PRId32
", failed",
4388 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
4389 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "array2mem: cannot read memory", NULL
);
4393 addr
+= count
* width
;
4398 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
4403 /* FIX? should we propagate errors here rather than printing them
4406 void target_handle_event(struct target
*target
, enum target_event e
)
4408 struct target_event_action
*teap
;
4410 for (teap
= target
->event_action
; teap
!= NULL
; teap
= teap
->next
) {
4411 if (teap
->event
== e
) {
4412 LOG_DEBUG("target: (%d) %s (%s) event: %d (%s) action: %s",
4413 target
->target_number
,
4414 target_name(target
),
4415 target_type_name(target
),
4417 Jim_Nvp_value2name_simple(nvp_target_event
, e
)->name
,
4418 Jim_GetString(teap
->body
, NULL
));
4419 if (Jim_EvalObj(teap
->interp
, teap
->body
) != JIM_OK
) {
4420 Jim_MakeErrorMessage(teap
->interp
);
4421 command_print(NULL
, "%s\n", Jim_GetString(Jim_GetResult(teap
->interp
), NULL
));
4428 * Returns true only if the target has a handler for the specified event.
4430 bool target_has_event_action(struct target
*target
, enum target_event event
)
4432 struct target_event_action
*teap
;
4434 for (teap
= target
->event_action
; teap
!= NULL
; teap
= teap
->next
) {
4435 if (teap
->event
== event
)
4441 enum target_cfg_param
{
4444 TCFG_WORK_AREA_VIRT
,
4445 TCFG_WORK_AREA_PHYS
,
4446 TCFG_WORK_AREA_SIZE
,
4447 TCFG_WORK_AREA_BACKUP
,
4450 TCFG_CHAIN_POSITION
,
4457 static Jim_Nvp nvp_config_opts
[] = {
4458 { .name
= "-type", .value
= TCFG_TYPE
},
4459 { .name
= "-event", .value
= TCFG_EVENT
},
4460 { .name
= "-work-area-virt", .value
= TCFG_WORK_AREA_VIRT
},
4461 { .name
= "-work-area-phys", .value
= TCFG_WORK_AREA_PHYS
},
4462 { .name
= "-work-area-size", .value
= TCFG_WORK_AREA_SIZE
},
4463 { .name
= "-work-area-backup", .value
= TCFG_WORK_AREA_BACKUP
},
4464 { .name
= "-endian" , .value
= TCFG_ENDIAN
},
4465 { .name
= "-coreid", .value
= TCFG_COREID
},
4466 { .name
= "-chain-position", .value
= TCFG_CHAIN_POSITION
},
4467 { .name
= "-dbgbase", .value
= TCFG_DBGBASE
},
4468 { .name
= "-ctibase", .value
= TCFG_CTIBASE
},
4469 { .name
= "-rtos", .value
= TCFG_RTOS
},
4470 { .name
= "-defer-examine", .value
= TCFG_DEFER_EXAMINE
},
4471 { .name
= NULL
, .value
= -1 }
4474 static int target_configure(Jim_GetOptInfo
*goi
, struct target
*target
)
4481 /* parse config or cget options ... */
4482 while (goi
->argc
> 0) {
4483 Jim_SetEmptyResult(goi
->interp
);
4484 /* Jim_GetOpt_Debug(goi); */
4486 if (target
->type
->target_jim_configure
) {
4487 /* target defines a configure function */
4488 /* target gets first dibs on parameters */
4489 e
= (*(target
->type
->target_jim_configure
))(target
, goi
);
4498 /* otherwise we 'continue' below */
4500 e
= Jim_GetOpt_Nvp(goi
, nvp_config_opts
, &n
);
4502 Jim_GetOpt_NvpUnknown(goi
, nvp_config_opts
, 0);
4508 if (goi
->isconfigure
) {
4509 Jim_SetResultFormatted(goi
->interp
,
4510 "not settable: %s", n
->name
);
4514 if (goi
->argc
!= 0) {
4515 Jim_WrongNumArgs(goi
->interp
,
4516 goi
->argc
, goi
->argv
,
4521 Jim_SetResultString(goi
->interp
,
4522 target_type_name(target
), -1);
4526 if (goi
->argc
== 0) {
4527 Jim_WrongNumArgs(goi
->interp
, goi
->argc
, goi
->argv
, "-event ?event-name? ...");
4531 e
= Jim_GetOpt_Nvp(goi
, nvp_target_event
, &n
);
4533 Jim_GetOpt_NvpUnknown(goi
, nvp_target_event
, 1);
4537 if (goi
->isconfigure
) {
4538 if (goi
->argc
!= 1) {
4539 Jim_WrongNumArgs(goi
->interp
, goi
->argc
, goi
->argv
, "-event ?event-name? ?EVENT-BODY?");
4543 if (goi
->argc
!= 0) {
4544 Jim_WrongNumArgs(goi
->interp
, goi
->argc
, goi
->argv
, "-event ?event-name?");
4550 struct target_event_action
*teap
;
4552 teap
= target
->event_action
;
4553 /* replace existing? */
4555 if (teap
->event
== (enum target_event
)n
->value
)
4560 if (goi
->isconfigure
) {
4561 bool replace
= true;
4564 teap
= calloc(1, sizeof(*teap
));
4567 teap
->event
= n
->value
;
4568 teap
->interp
= goi
->interp
;
4569 Jim_GetOpt_Obj(goi
, &o
);
4571 Jim_DecrRefCount(teap
->interp
, teap
->body
);
4572 teap
->body
= Jim_DuplicateObj(goi
->interp
, o
);
4575 * Tcl/TK - "tk events" have a nice feature.
4576 * See the "BIND" command.
4577 * We should support that here.
4578 * You can specify %X and %Y in the event code.
4579 * The idea is: %T - target name.
4580 * The idea is: %N - target number
4581 * The idea is: %E - event name.
4583 Jim_IncrRefCount(teap
->body
);
4586 /* add to head of event list */
4587 teap
->next
= target
->event_action
;
4588 target
->event_action
= teap
;
4590 Jim_SetEmptyResult(goi
->interp
);
4594 Jim_SetEmptyResult(goi
->interp
);
4596 Jim_SetResult(goi
->interp
, Jim_DuplicateObj(goi
->interp
, teap
->body
));
4602 case TCFG_WORK_AREA_VIRT
:
4603 if (goi
->isconfigure
) {
4604 target_free_all_working_areas(target
);
4605 e
= Jim_GetOpt_Wide(goi
, &w
);
4608 target
->working_area_virt
= w
;
4609 target
->working_area_virt_spec
= true;
4614 Jim_SetResult(goi
->interp
, Jim_NewIntObj(goi
->interp
, target
->working_area_virt
));
4618 case TCFG_WORK_AREA_PHYS
:
4619 if (goi
->isconfigure
) {
4620 target_free_all_working_areas(target
);
4621 e
= Jim_GetOpt_Wide(goi
, &w
);
4624 target
->working_area_phys
= w
;
4625 target
->working_area_phys_spec
= true;
4630 Jim_SetResult(goi
->interp
, Jim_NewIntObj(goi
->interp
, target
->working_area_phys
));
4634 case TCFG_WORK_AREA_SIZE
:
4635 if (goi
->isconfigure
) {
4636 target_free_all_working_areas(target
);
4637 e
= Jim_GetOpt_Wide(goi
, &w
);
4640 target
->working_area_size
= w
;
4645 Jim_SetResult(goi
->interp
, Jim_NewIntObj(goi
->interp
, target
->working_area_size
));
4649 case TCFG_WORK_AREA_BACKUP
:
4650 if (goi
->isconfigure
) {
4651 target_free_all_working_areas(target
);
4652 e
= Jim_GetOpt_Wide(goi
, &w
);
4655 /* make this exactly 1 or 0 */
4656 target
->backup_working_area
= (!!w
);
4661 Jim_SetResult(goi
->interp
, Jim_NewIntObj(goi
->interp
, target
->backup_working_area
));
4662 /* loop for more e*/
4667 if (goi
->isconfigure
) {
4668 e
= Jim_GetOpt_Nvp(goi
, nvp_target_endian
, &n
);
4670 Jim_GetOpt_NvpUnknown(goi
, nvp_target_endian
, 1);
4673 target
->endianness
= n
->value
;
4678 n
= Jim_Nvp_value2name_simple(nvp_target_endian
, target
->endianness
);
4679 if (n
->name
== NULL
) {
4680 target
->endianness
= TARGET_LITTLE_ENDIAN
;
4681 n
= Jim_Nvp_value2name_simple(nvp_target_endian
, target
->endianness
);
4683 Jim_SetResultString(goi
->interp
, n
->name
, -1);
4688 if (goi
->isconfigure
) {
4689 e
= Jim_GetOpt_Wide(goi
, &w
);
4692 target
->coreid
= (int32_t)w
;
4697 Jim_SetResult(goi
->interp
, Jim_NewIntObj(goi
->interp
, target
->working_area_size
));
4701 case TCFG_CHAIN_POSITION
:
4702 if (goi
->isconfigure
) {
4704 struct jtag_tap
*tap
;
4705 target_free_all_working_areas(target
);
4706 e
= Jim_GetOpt_Obj(goi
, &o_t
);
4709 tap
= jtag_tap_by_jim_obj(goi
->interp
, o_t
);
4712 /* make this exactly 1 or 0 */
4718 Jim_SetResultString(goi
->interp
, target
->tap
->dotted_name
, -1);
4719 /* loop for more e*/
4722 if (goi
->isconfigure
) {
4723 e
= Jim_GetOpt_Wide(goi
, &w
);
4726 target
->dbgbase
= (uint32_t)w
;
4727 target
->dbgbase_set
= true;
4732 Jim_SetResult(goi
->interp
, Jim_NewIntObj(goi
->interp
, target
->dbgbase
));
4736 if (goi
->isconfigure
) {
4737 e
= Jim_GetOpt_Wide(goi
, &w
);
4740 target
->ctibase
= (uint32_t)w
;
4741 target
->ctibase_set
= true;
4746 Jim_SetResult(goi
->interp
, Jim_NewIntObj(goi
->interp
, target
->ctibase
));
4752 int result
= rtos_create(goi
, target
);
4753 if (result
!= JIM_OK
)
4759 case TCFG_DEFER_EXAMINE
:
4761 target
->defer_examine
= true;
4766 } /* while (goi->argc) */
4769 /* done - we return */
4773 static int jim_target_configure(Jim_Interp
*interp
, int argc
, Jim_Obj
* const *argv
)
4777 Jim_GetOpt_Setup(&goi
, interp
, argc
- 1, argv
+ 1);
4778 goi
.isconfigure
= !strcmp(Jim_GetString(argv
[0], NULL
), "configure");
4780 Jim_WrongNumArgs(goi
.interp
, goi
.argc
, goi
.argv
,
4781 "missing: -option ...");
4784 struct target
*target
= Jim_CmdPrivData(goi
.interp
);
4785 return target_configure(&goi
, target
);
4788 static int jim_target_mw(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4790 const char *cmd_name
= Jim_GetString(argv
[0], NULL
);
4793 Jim_GetOpt_Setup(&goi
, interp
, argc
- 1, argv
+ 1);
4795 if (goi
.argc
< 2 || goi
.argc
> 4) {
4796 Jim_SetResultFormatted(goi
.interp
,
4797 "usage: %s [phys] <address> <data> [<count>]", cmd_name
);
4802 fn
= target_write_memory
;
4805 if (strcmp(Jim_GetString(argv
[1], NULL
), "phys") == 0) {
4807 struct Jim_Obj
*obj
;
4808 e
= Jim_GetOpt_Obj(&goi
, &obj
);
4812 fn
= target_write_phys_memory
;
4816 e
= Jim_GetOpt_Wide(&goi
, &a
);
4821 e
= Jim_GetOpt_Wide(&goi
, &b
);
4826 if (goi
.argc
== 1) {
4827 e
= Jim_GetOpt_Wide(&goi
, &c
);
4832 /* all args must be consumed */
4836 struct target
*target
= Jim_CmdPrivData(goi
.interp
);
4838 if (strcasecmp(cmd_name
, "mww") == 0)
4840 else if (strcasecmp(cmd_name
, "mwh") == 0)
4842 else if (strcasecmp(cmd_name
, "mwb") == 0)
4845 LOG_ERROR("command '%s' unknown: ", cmd_name
);
4849 return (target_fill_mem(target
, a
, fn
, data_size
, b
, c
) == ERROR_OK
) ? JIM_OK
: JIM_ERR
;
4853 * @brief Reads an array of words/halfwords/bytes from target memory starting at specified address.
4855 * Usage: mdw [phys] <address> [<count>] - for 32 bit reads
4856 * mdh [phys] <address> [<count>] - for 16 bit reads
4857 * mdb [phys] <address> [<count>] - for 8 bit reads
4859 * Count defaults to 1.
4861 * Calls target_read_memory or target_read_phys_memory depending on
4862 * the presence of the "phys" argument
4863 * Reads the target memory in blocks of max. 32 bytes, and returns an array of ints formatted
4864 * to int representation in base16.
4865 * Also outputs read data in a human readable form using command_print
4867 * @param phys if present target_read_phys_memory will be used instead of target_read_memory
4868 * @param address address where to start the read. May be specified in decimal or hex using the standard "0x" prefix
4869 * @param count optional count parameter to read an array of values. If not specified, defaults to 1.
4870 * @returns: JIM_ERR on error or JIM_OK on success and sets the result string to an array of ascii formatted numbers
4871 * on success, with [<count>] number of elements.
4873 * In case of little endian target:
4874 * Example1: "mdw 0x00000000" returns "10123456"
4875 * Exmaple2: "mdh 0x00000000 1" returns "3456"
4876 * Example3: "mdb 0x00000000" returns "56"
4877 * Example4: "mdh 0x00000000 2" returns "3456 1012"
4878 * Example5: "mdb 0x00000000 3" returns "56 34 12"
4880 static int jim_target_md(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4882 const char *cmd_name
= Jim_GetString(argv
[0], NULL
);
4885 Jim_GetOpt_Setup(&goi
, interp
, argc
- 1, argv
+ 1);
4887 if ((goi
.argc
< 1) || (goi
.argc
> 3)) {
4888 Jim_SetResultFormatted(goi
.interp
,
4889 "usage: %s [phys] <address> [<count>]", cmd_name
);
4893 int (*fn
)(struct target
*target
,
4894 target_addr_t address
, uint32_t size
, uint32_t count
, uint8_t *buffer
);
4895 fn
= target_read_memory
;
4898 if (strcmp(Jim_GetString(argv
[1], NULL
), "phys") == 0) {
4900 struct Jim_Obj
*obj
;
4901 e
= Jim_GetOpt_Obj(&goi
, &obj
);
4905 fn
= target_read_phys_memory
;
4908 /* Read address parameter */
4910 e
= Jim_GetOpt_Wide(&goi
, &addr
);
4914 /* If next parameter exists, read it out as the count parameter, if not, set it to 1 (default) */
4916 if (goi
.argc
== 1) {
4917 e
= Jim_GetOpt_Wide(&goi
, &count
);
4923 /* all args must be consumed */
4927 jim_wide dwidth
= 1; /* shut up gcc */
4928 if (strcasecmp(cmd_name
, "mdw") == 0)
4930 else if (strcasecmp(cmd_name
, "mdh") == 0)
4932 else if (strcasecmp(cmd_name
, "mdb") == 0)
4935 LOG_ERROR("command '%s' unknown: ", cmd_name
);
4939 /* convert count to "bytes" */
4940 int bytes
= count
* dwidth
;
4942 struct target
*target
= Jim_CmdPrivData(goi
.interp
);
4943 uint8_t target_buf
[32];
4946 y
= (bytes
< 16) ? bytes
: 16; /* y = min(bytes, 16); */
4948 /* Try to read out next block */
4949 e
= fn(target
, addr
, dwidth
, y
/ dwidth
, target_buf
);
4951 if (e
!= ERROR_OK
) {
4952 Jim_SetResultFormatted(interp
, "error reading target @ 0x%08lx", (long)addr
);
4956 command_print_sameline(NULL
, "0x%08x ", (int)(addr
));
4959 for (x
= 0; x
< 16 && x
< y
; x
+= 4) {
4960 z
= target_buffer_get_u32(target
, &(target_buf
[x
]));
4961 command_print_sameline(NULL
, "%08x ", (int)(z
));
4963 for (; (x
< 16) ; x
+= 4)
4964 command_print_sameline(NULL
, " ");
4967 for (x
= 0; x
< 16 && x
< y
; x
+= 2) {
4968 z
= target_buffer_get_u16(target
, &(target_buf
[x
]));
4969 command_print_sameline(NULL
, "%04x ", (int)(z
));
4971 for (; (x
< 16) ; x
+= 2)
4972 command_print_sameline(NULL
, " ");
4976 for (x
= 0 ; (x
< 16) && (x
< y
) ; x
+= 1) {
4977 z
= target_buffer_get_u8(target
, &(target_buf
[x
]));
4978 command_print_sameline(NULL
, "%02x ", (int)(z
));
4980 for (; (x
< 16) ; x
+= 1)
4981 command_print_sameline(NULL
, " ");
4984 /* ascii-ify the bytes */
4985 for (x
= 0 ; x
< y
; x
++) {
4986 if ((target_buf
[x
] >= 0x20) &&
4987 (target_buf
[x
] <= 0x7e)) {
4991 target_buf
[x
] = '.';
4996 target_buf
[x
] = ' ';
5001 /* print - with a newline */
5002 command_print_sameline(NULL
, "%s\n", target_buf
);
5010 static int jim_target_mem2array(Jim_Interp
*interp
,
5011 int argc
, Jim_Obj
*const *argv
)
5013 struct target
*target
= Jim_CmdPrivData(interp
);
5014 return target_mem2array(interp
, target
, argc
- 1, argv
+ 1);
5017 static int jim_target_array2mem(Jim_Interp
*interp
,
5018 int argc
, Jim_Obj
*const *argv
)
5020 struct target
*target
= Jim_CmdPrivData(interp
);
5021 return target_array2mem(interp
, target
, argc
- 1, argv
+ 1);
5024 static int jim_target_tap_disabled(Jim_Interp
*interp
)
5026 Jim_SetResultFormatted(interp
, "[TAP is disabled]");
5030 static int jim_target_examine(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
5032 bool allow_defer
= false;
5035 Jim_GetOpt_Setup(&goi
, interp
, argc
- 1, argv
+ 1);
5037 const char *cmd_name
= Jim_GetString(argv
[0], NULL
);
5038 Jim_SetResultFormatted(goi
.interp
,
5039 "usage: %s ['allow-defer']", cmd_name
);
5043 strcmp(Jim_GetString(argv
[1], NULL
), "allow-defer") == 0) {
5045 struct Jim_Obj
*obj
;
5046 int e
= Jim_GetOpt_Obj(&goi
, &obj
);
5052 struct target
*target
= Jim_CmdPrivData(interp
);
5053 if (!target
->tap
->enabled
)
5054 return jim_target_tap_disabled(interp
);
5056 if (allow_defer
&& target
->defer_examine
) {
5057 LOG_INFO("Deferring arp_examine of %s", target_name(target
));
5058 LOG_INFO("Use arp_examine command to examine it manually!");
5062 int e
= target
->type
->examine(target
);
5068 static int jim_target_was_examined(Jim_Interp
*interp
, int argc
, Jim_Obj
* const *argv
)
5070 struct target
*target
= Jim_CmdPrivData(interp
);
5072 Jim_SetResultBool(interp
, target_was_examined(target
));
5076 static int jim_target_examine_deferred(Jim_Interp
*interp
, int argc
, Jim_Obj
* const *argv
)
5078 struct target
*target
= Jim_CmdPrivData(interp
);
5080 Jim_SetResultBool(interp
, target
->defer_examine
);
5084 static int jim_target_halt_gdb(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
5087 Jim_WrongNumArgs(interp
, 1, argv
, "[no parameters]");
5090 struct target
*target
= Jim_CmdPrivData(interp
);
5092 if (target_call_event_callbacks(target
, TARGET_EVENT_GDB_HALT
) != ERROR_OK
)
5098 static int jim_target_poll(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
5101 Jim_WrongNumArgs(interp
, 1, argv
, "[no parameters]");
5104 struct target
*target
= Jim_CmdPrivData(interp
);
5105 if (!target
->tap
->enabled
)
5106 return jim_target_tap_disabled(interp
);
5109 if (!(target_was_examined(target
)))
5110 e
= ERROR_TARGET_NOT_EXAMINED
;
5112 e
= target
->type
->poll(target
);
5118 static int jim_target_reset(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
5121 Jim_GetOpt_Setup(&goi
, interp
, argc
- 1, argv
+ 1);
5123 if (goi
.argc
!= 2) {
5124 Jim_WrongNumArgs(interp
, 0, argv
,
5125 "([tT]|[fF]|assert|deassert) BOOL");
5130 int e
= Jim_GetOpt_Nvp(&goi
, nvp_assert
, &n
);
5132 Jim_GetOpt_NvpUnknown(&goi
, nvp_assert
, 1);
5135 /* the halt or not param */
5137 e
= Jim_GetOpt_Wide(&goi
, &a
);
5141 struct target
*target
= Jim_CmdPrivData(goi
.interp
);
5142 if (!target
->tap
->enabled
)
5143 return jim_target_tap_disabled(interp
);
5145 if (!target
->type
->assert_reset
|| !target
->type
->deassert_reset
) {
5146 Jim_SetResultFormatted(interp
,
5147 "No target-specific reset for %s",
5148 target_name(target
));
5152 if (target
->defer_examine
)
5153 target_reset_examined(target
);
5155 /* determine if we should halt or not. */
5156 target
->reset_halt
= !!a
;
5157 /* When this happens - all workareas are invalid. */
5158 target_free_all_working_areas_restore(target
, 0);
5161 if (n
->value
== NVP_ASSERT
)
5162 e
= target
->type
->assert_reset(target
);
5164 e
= target
->type
->deassert_reset(target
);
5165 return (e
== ERROR_OK
) ? JIM_OK
: JIM_ERR
;
5168 static int jim_target_halt(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
5171 Jim_WrongNumArgs(interp
, 1, argv
, "[no parameters]");
5174 struct target
*target
= Jim_CmdPrivData(interp
);
5175 if (!target
->tap
->enabled
)
5176 return jim_target_tap_disabled(interp
);
5177 int e
= target
->type
->halt(target
);
5178 return (e
== ERROR_OK
) ? JIM_OK
: JIM_ERR
;
5181 static int jim_target_wait_state(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
5184 Jim_GetOpt_Setup(&goi
, interp
, argc
- 1, argv
+ 1);
5186 /* params: <name> statename timeoutmsecs */
5187 if (goi
.argc
!= 2) {
5188 const char *cmd_name
= Jim_GetString(argv
[0], NULL
);
5189 Jim_SetResultFormatted(goi
.interp
,
5190 "%s <state_name> <timeout_in_msec>", cmd_name
);
5195 int e
= Jim_GetOpt_Nvp(&goi
, nvp_target_state
, &n
);
5197 Jim_GetOpt_NvpUnknown(&goi
, nvp_target_state
, 1);
5201 e
= Jim_GetOpt_Wide(&goi
, &a
);
5204 struct target
*target
= Jim_CmdPrivData(interp
);
5205 if (!target
->tap
->enabled
)
5206 return jim_target_tap_disabled(interp
);
5208 e
= target_wait_state(target
, n
->value
, a
);
5209 if (e
!= ERROR_OK
) {
5210 Jim_Obj
*eObj
= Jim_NewIntObj(interp
, e
);
5211 Jim_SetResultFormatted(goi
.interp
,
5212 "target: %s wait %s fails (%#s) %s",
5213 target_name(target
), n
->name
,
5214 eObj
, target_strerror_safe(e
));
5215 Jim_FreeNewObj(interp
, eObj
);
5220 /* List for human, Events defined for this target.
5221 * scripts/programs should use 'name cget -event NAME'
5223 static int jim_target_event_list(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
5225 struct command_context
*cmd_ctx
= current_command_context(interp
);
5226 assert(cmd_ctx
!= NULL
);
5228 struct target
*target
= Jim_CmdPrivData(interp
);
5229 struct target_event_action
*teap
= target
->event_action
;
5230 command_print(cmd_ctx
, "Event actions for target (%d) %s\n",
5231 target
->target_number
,
5232 target_name(target
));
5233 command_print(cmd_ctx
, "%-25s | Body", "Event");
5234 command_print(cmd_ctx
, "------------------------- | "
5235 "----------------------------------------");
5237 Jim_Nvp
*opt
= Jim_Nvp_value2name_simple(nvp_target_event
, teap
->event
);
5238 command_print(cmd_ctx
, "%-25s | %s",
5239 opt
->name
, Jim_GetString(teap
->body
, NULL
));
5242 command_print(cmd_ctx
, "***END***");
5245 static int jim_target_current_state(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
5248 Jim_WrongNumArgs(interp
, 1, argv
, "[no parameters]");
5251 struct target
*target
= Jim_CmdPrivData(interp
);
5252 Jim_SetResultString(interp
, target_state_name(target
), -1);
5255 static int jim_target_invoke_event(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
5258 Jim_GetOpt_Setup(&goi
, interp
, argc
- 1, argv
+ 1);
5259 if (goi
.argc
!= 1) {
5260 const char *cmd_name
= Jim_GetString(argv
[0], NULL
);
5261 Jim_SetResultFormatted(goi
.interp
, "%s <eventname>", cmd_name
);
5265 int e
= Jim_GetOpt_Nvp(&goi
, nvp_target_event
, &n
);
5267 Jim_GetOpt_NvpUnknown(&goi
, nvp_target_event
, 1);
5270 struct target
*target
= Jim_CmdPrivData(interp
);
5271 target_handle_event(target
, n
->value
);
5275 static const struct command_registration target_instance_command_handlers
[] = {
5277 .name
= "configure",
5278 .mode
= COMMAND_CONFIG
,
5279 .jim_handler
= jim_target_configure
,
5280 .help
= "configure a new target for use",
5281 .usage
= "[target_attribute ...]",
5285 .mode
= COMMAND_ANY
,
5286 .jim_handler
= jim_target_configure
,
5287 .help
= "returns the specified target attribute",
5288 .usage
= "target_attribute",
5292 .mode
= COMMAND_EXEC
,
5293 .jim_handler
= jim_target_mw
,
5294 .help
= "Write 32-bit word(s) to target memory",
5295 .usage
= "address data [count]",
5299 .mode
= COMMAND_EXEC
,
5300 .jim_handler
= jim_target_mw
,
5301 .help
= "Write 16-bit half-word(s) to target memory",
5302 .usage
= "address data [count]",
5306 .mode
= COMMAND_EXEC
,
5307 .jim_handler
= jim_target_mw
,
5308 .help
= "Write byte(s) to target memory",
5309 .usage
= "address data [count]",
5313 .mode
= COMMAND_EXEC
,
5314 .jim_handler
= jim_target_md
,
5315 .help
= "Display target memory as 32-bit words",
5316 .usage
= "address [count]",
5320 .mode
= COMMAND_EXEC
,
5321 .jim_handler
= jim_target_md
,
5322 .help
= "Display target memory as 16-bit half-words",
5323 .usage
= "address [count]",
5327 .mode
= COMMAND_EXEC
,
5328 .jim_handler
= jim_target_md
,
5329 .help
= "Display target memory as 8-bit bytes",
5330 .usage
= "address [count]",
5333 .name
= "array2mem",
5334 .mode
= COMMAND_EXEC
,
5335 .jim_handler
= jim_target_array2mem
,
5336 .help
= "Writes Tcl array of 8/16/32 bit numbers "
5338 .usage
= "arrayname bitwidth address count",
5341 .name
= "mem2array",
5342 .mode
= COMMAND_EXEC
,
5343 .jim_handler
= jim_target_mem2array
,
5344 .help
= "Loads Tcl array of 8/16/32 bit numbers "
5345 "from target memory",
5346 .usage
= "arrayname bitwidth address count",
5349 .name
= "eventlist",
5350 .mode
= COMMAND_EXEC
,
5351 .jim_handler
= jim_target_event_list
,
5352 .help
= "displays a table of events defined for this target",
5356 .mode
= COMMAND_EXEC
,
5357 .jim_handler
= jim_target_current_state
,
5358 .help
= "displays the current state of this target",
5361 .name
= "arp_examine",
5362 .mode
= COMMAND_EXEC
,
5363 .jim_handler
= jim_target_examine
,
5364 .help
= "used internally for reset processing",
5365 .usage
= "arp_examine ['allow-defer']",
5368 .name
= "was_examined",
5369 .mode
= COMMAND_EXEC
,
5370 .jim_handler
= jim_target_was_examined
,
5371 .help
= "used internally for reset processing",
5372 .usage
= "was_examined",
5375 .name
= "examine_deferred",
5376 .mode
= COMMAND_EXEC
,
5377 .jim_handler
= jim_target_examine_deferred
,
5378 .help
= "used internally for reset processing",
5379 .usage
= "examine_deferred",
5382 .name
= "arp_halt_gdb",
5383 .mode
= COMMAND_EXEC
,
5384 .jim_handler
= jim_target_halt_gdb
,
5385 .help
= "used internally for reset processing to halt GDB",
5389 .mode
= COMMAND_EXEC
,
5390 .jim_handler
= jim_target_poll
,
5391 .help
= "used internally for reset processing",
5394 .name
= "arp_reset",
5395 .mode
= COMMAND_EXEC
,
5396 .jim_handler
= jim_target_reset
,
5397 .help
= "used internally for reset processing",
5401 .mode
= COMMAND_EXEC
,
5402 .jim_handler
= jim_target_halt
,
5403 .help
= "used internally for reset processing",
5406 .name
= "arp_waitstate",
5407 .mode
= COMMAND_EXEC
,
5408 .jim_handler
= jim_target_wait_state
,
5409 .help
= "used internally for reset processing",
5412 .name
= "invoke-event",
5413 .mode
= COMMAND_EXEC
,
5414 .jim_handler
= jim_target_invoke_event
,
5415 .help
= "invoke handler for specified event",
5416 .usage
= "event_name",
5418 COMMAND_REGISTRATION_DONE
5421 static int target_create(Jim_GetOptInfo
*goi
)
5428 struct target
*target
;
5429 struct command_context
*cmd_ctx
;
5431 cmd_ctx
= current_command_context(goi
->interp
);
5432 assert(cmd_ctx
!= NULL
);
5434 if (goi
->argc
< 3) {
5435 Jim_WrongNumArgs(goi
->interp
, 1, goi
->argv
, "?name? ?type? ..options...");
5440 Jim_GetOpt_Obj(goi
, &new_cmd
);
5441 /* does this command exist? */
5442 cmd
= Jim_GetCommand(goi
->interp
, new_cmd
, JIM_ERRMSG
);
5444 cp
= Jim_GetString(new_cmd
, NULL
);
5445 Jim_SetResultFormatted(goi
->interp
, "Command/target: %s Exists", cp
);
5450 e
= Jim_GetOpt_String(goi
, &cp
, NULL
);
5453 struct transport
*tr
= get_current_transport();
5454 if (tr
->override_target
) {
5455 e
= tr
->override_target(&cp
);
5456 if (e
!= ERROR_OK
) {
5457 LOG_ERROR("The selected transport doesn't support this target");
5460 LOG_INFO("The selected transport took over low-level target control. The results might differ compared to plain JTAG/SWD");
5462 /* now does target type exist */
5463 for (x
= 0 ; target_types
[x
] ; x
++) {
5464 if (0 == strcmp(cp
, target_types
[x
]->name
)) {
5469 /* check for deprecated name */
5470 if (target_types
[x
]->deprecated_name
) {
5471 if (0 == strcmp(cp
, target_types
[x
]->deprecated_name
)) {
5473 LOG_WARNING("target name is deprecated use: \'%s\'", target_types
[x
]->name
);
5478 if (target_types
[x
] == NULL
) {
5479 Jim_SetResultFormatted(goi
->interp
, "Unknown target type %s, try one of ", cp
);
5480 for (x
= 0 ; target_types
[x
] ; x
++) {
5481 if (target_types
[x
+ 1]) {
5482 Jim_AppendStrings(goi
->interp
,
5483 Jim_GetResult(goi
->interp
),
5484 target_types
[x
]->name
,
5487 Jim_AppendStrings(goi
->interp
,
5488 Jim_GetResult(goi
->interp
),
5490 target_types
[x
]->name
, NULL
);
5497 target
= calloc(1, sizeof(struct target
));
5498 /* set target number */
5499 target
->target_number
= new_target_number();
5500 cmd_ctx
->current_target
= target
->target_number
;
5502 /* allocate memory for each unique target type */
5503 target
->type
= calloc(1, sizeof(struct target_type
));
5505 memcpy(target
->type
, target_types
[x
], sizeof(struct target_type
));
5507 /* will be set by "-endian" */
5508 target
->endianness
= TARGET_ENDIAN_UNKNOWN
;
5510 /* default to first core, override with -coreid */
5513 target
->working_area
= 0x0;
5514 target
->working_area_size
= 0x0;
5515 target
->working_areas
= NULL
;
5516 target
->backup_working_area
= 0;
5518 target
->state
= TARGET_UNKNOWN
;
5519 target
->debug_reason
= DBG_REASON_UNDEFINED
;
5520 target
->reg_cache
= NULL
;
5521 target
->breakpoints
= NULL
;
5522 target
->watchpoints
= NULL
;
5523 target
->next
= NULL
;
5524 target
->arch_info
= NULL
;
5526 target
->display
= 1;
5528 target
->halt_issued
= false;
5530 /* initialize trace information */
5531 target
->trace_info
= calloc(1, sizeof(struct trace
));
5533 target
->dbgmsg
= NULL
;
5534 target
->dbg_msg_enabled
= 0;
5536 target
->endianness
= TARGET_ENDIAN_UNKNOWN
;
5538 target
->rtos
= NULL
;
5539 target
->rtos_auto_detect
= false;
5541 /* Do the rest as "configure" options */
5542 goi
->isconfigure
= 1;
5543 e
= target_configure(goi
, target
);
5545 if (target
->tap
== NULL
) {
5546 Jim_SetResultString(goi
->interp
, "-chain-position required when creating target", -1);
5556 if (target
->endianness
== TARGET_ENDIAN_UNKNOWN
) {
5557 /* default endian to little if not specified */
5558 target
->endianness
= TARGET_LITTLE_ENDIAN
;
5561 cp
= Jim_GetString(new_cmd
, NULL
);
5562 target
->cmd_name
= strdup(cp
);
5564 /* create the target specific commands */
5565 if (target
->type
->commands
) {
5566 e
= register_commands(cmd_ctx
, NULL
, target
->type
->commands
);
5568 LOG_ERROR("unable to register '%s' commands", cp
);
5570 if (target
->type
->target_create
)
5571 (*(target
->type
->target_create
))(target
, goi
->interp
);
5573 /* append to end of list */
5575 struct target
**tpp
;
5576 tpp
= &(all_targets
);
5578 tpp
= &((*tpp
)->next
);
5582 /* now - create the new target name command */
5583 const struct command_registration target_subcommands
[] = {
5585 .chain
= target_instance_command_handlers
,
5588 .chain
= target
->type
->commands
,
5590 COMMAND_REGISTRATION_DONE
5592 const struct command_registration target_commands
[] = {
5595 .mode
= COMMAND_ANY
,
5596 .help
= "target command group",
5598 .chain
= target_subcommands
,
5600 COMMAND_REGISTRATION_DONE
5602 e
= register_commands(cmd_ctx
, NULL
, target_commands
);
5606 struct command
*c
= command_find_in_context(cmd_ctx
, cp
);
5608 command_set_handler_data(c
, target
);
5610 return (ERROR_OK
== e
) ? JIM_OK
: JIM_ERR
;
5613 static int jim_target_current(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
5616 Jim_WrongNumArgs(interp
, 1, argv
, "Too many parameters");
5619 struct command_context
*cmd_ctx
= current_command_context(interp
);
5620 assert(cmd_ctx
!= NULL
);
5622 Jim_SetResultString(interp
, target_name(get_current_target(cmd_ctx
)), -1);
5626 static int jim_target_types(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
5629 Jim_WrongNumArgs(interp
, 1, argv
, "Too many parameters");
5632 Jim_SetResult(interp
, Jim_NewListObj(interp
, NULL
, 0));
5633 for (unsigned x
= 0; NULL
!= target_types
[x
]; x
++) {
5634 Jim_ListAppendElement(interp
, Jim_GetResult(interp
),
5635 Jim_NewStringObj(interp
, target_types
[x
]->name
, -1));
5640 static int jim_target_names(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
5643 Jim_WrongNumArgs(interp
, 1, argv
, "Too many parameters");
5646 Jim_SetResult(interp
, Jim_NewListObj(interp
, NULL
, 0));
5647 struct target
*target
= all_targets
;
5649 Jim_ListAppendElement(interp
, Jim_GetResult(interp
),
5650 Jim_NewStringObj(interp
, target_name(target
), -1));
5651 target
= target
->next
;
5656 static int jim_target_smp(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
5659 const char *targetname
;
5661 struct target
*target
= (struct target
*) NULL
;
5662 struct target_list
*head
, *curr
, *new;
5663 curr
= (struct target_list
*) NULL
;
5664 head
= (struct target_list
*) NULL
;
5667 LOG_DEBUG("%d", argc
);
5668 /* argv[1] = target to associate in smp
5669 * argv[2] = target to assoicate in smp
5673 for (i
= 1; i
< argc
; i
++) {
5675 targetname
= Jim_GetString(argv
[i
], &len
);
5676 target
= get_target(targetname
);
5677 LOG_DEBUG("%s ", targetname
);
5679 new = malloc(sizeof(struct target_list
));
5680 new->target
= target
;
5681 new->next
= (struct target_list
*)NULL
;
5682 if (head
== (struct target_list
*)NULL
) {
5691 /* now parse the list of cpu and put the target in smp mode*/
5694 while (curr
!= (struct target_list
*)NULL
) {
5695 target
= curr
->target
;
5697 target
->head
= head
;
5701 if (target
&& target
->rtos
)
5702 retval
= rtos_smp_init(head
->target
);
5708 static int jim_target_create(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
5711 Jim_GetOpt_Setup(&goi
, interp
, argc
- 1, argv
+ 1);
5713 Jim_WrongNumArgs(goi
.interp
, goi
.argc
, goi
.argv
,
5714 "<name> <target_type> [<target_options> ...]");
5717 return target_create(&goi
);
5720 static const struct command_registration target_subcommand_handlers
[] = {
5723 .mode
= COMMAND_CONFIG
,
5724 .handler
= handle_target_init_command
,
5725 .help
= "initialize targets",
5729 /* REVISIT this should be COMMAND_CONFIG ... */
5730 .mode
= COMMAND_ANY
,
5731 .jim_handler
= jim_target_create
,
5732 .usage
= "name type '-chain-position' name [options ...]",
5733 .help
= "Creates and selects a new target",
5737 .mode
= COMMAND_ANY
,
5738 .jim_handler
= jim_target_current
,
5739 .help
= "Returns the currently selected target",
5743 .mode
= COMMAND_ANY
,
5744 .jim_handler
= jim_target_types
,
5745 .help
= "Returns the available target types as "
5746 "a list of strings",
5750 .mode
= COMMAND_ANY
,
5751 .jim_handler
= jim_target_names
,
5752 .help
= "Returns the names of all targets as a list of strings",
5756 .mode
= COMMAND_ANY
,
5757 .jim_handler
= jim_target_smp
,
5758 .usage
= "targetname1 targetname2 ...",
5759 .help
= "gather several target in a smp list"
5762 COMMAND_REGISTRATION_DONE
5766 target_addr_t address
;
5772 static int fastload_num
;
5773 static struct FastLoad
*fastload
;
5775 static void free_fastload(void)
5777 if (fastload
!= NULL
) {
5779 for (i
= 0; i
< fastload_num
; i
++) {
5780 if (fastload
[i
].data
)
5781 free(fastload
[i
].data
);
5788 COMMAND_HANDLER(handle_fast_load_image_command
)
5792 uint32_t image_size
;
5793 target_addr_t min_address
= 0;
5794 target_addr_t max_address
= -1;
5799 int retval
= CALL_COMMAND_HANDLER(parse_load_image_command_CMD_ARGV
,
5800 &image
, &min_address
, &max_address
);
5801 if (ERROR_OK
!= retval
)
5804 struct duration bench
;
5805 duration_start(&bench
);
5807 retval
= image_open(&image
, CMD_ARGV
[0], (CMD_ARGC
>= 3) ? CMD_ARGV
[2] : NULL
);
5808 if (retval
!= ERROR_OK
)
5813 fastload_num
= image
.num_sections
;
5814 fastload
= malloc(sizeof(struct FastLoad
)*image
.num_sections
);
5815 if (fastload
== NULL
) {
5816 command_print(CMD_CTX
, "out of memory");
5817 image_close(&image
);
5820 memset(fastload
, 0, sizeof(struct FastLoad
)*image
.num_sections
);
5821 for (i
= 0; i
< image
.num_sections
; i
++) {
5822 buffer
= malloc(image
.sections
[i
].size
);
5823 if (buffer
== NULL
) {
5824 command_print(CMD_CTX
, "error allocating buffer for section (%d bytes)",
5825 (int)(image
.sections
[i
].size
));
5826 retval
= ERROR_FAIL
;
5830 retval
= image_read_section(&image
, i
, 0x0, image
.sections
[i
].size
, buffer
, &buf_cnt
);
5831 if (retval
!= ERROR_OK
) {
5836 uint32_t offset
= 0;
5837 uint32_t length
= buf_cnt
;
5839 /* DANGER!!! beware of unsigned comparision here!!! */
5841 if ((image
.sections
[i
].base_address
+ buf_cnt
>= min_address
) &&
5842 (image
.sections
[i
].base_address
< max_address
)) {
5843 if (image
.sections
[i
].base_address
< min_address
) {
5844 /* clip addresses below */
5845 offset
+= min_address
-image
.sections
[i
].base_address
;
5849 if (image
.sections
[i
].base_address
+ buf_cnt
> max_address
)
5850 length
-= (image
.sections
[i
].base_address
+ buf_cnt
)-max_address
;
5852 fastload
[i
].address
= image
.sections
[i
].base_address
+ offset
;
5853 fastload
[i
].data
= malloc(length
);
5854 if (fastload
[i
].data
== NULL
) {
5856 command_print(CMD_CTX
, "error allocating buffer for section (%" PRIu32
" bytes)",
5858 retval
= ERROR_FAIL
;
5861 memcpy(fastload
[i
].data
, buffer
+ offset
, length
);
5862 fastload
[i
].length
= length
;
5864 image_size
+= length
;
5865 command_print(CMD_CTX
, "%u bytes written at address 0x%8.8x",
5866 (unsigned int)length
,
5867 ((unsigned int)(image
.sections
[i
].base_address
+ offset
)));
5873 if ((ERROR_OK
== retval
) && (duration_measure(&bench
) == ERROR_OK
)) {
5874 command_print(CMD_CTX
, "Loaded %" PRIu32
" bytes "
5875 "in %fs (%0.3f KiB/s)", image_size
,
5876 duration_elapsed(&bench
), duration_kbps(&bench
, image_size
));
5878 command_print(CMD_CTX
,
5879 "WARNING: image has not been loaded to target!"
5880 "You can issue a 'fast_load' to finish loading.");
5883 image_close(&image
);
5885 if (retval
!= ERROR_OK
)
5891 COMMAND_HANDLER(handle_fast_load_command
)
5894 return ERROR_COMMAND_SYNTAX_ERROR
;
5895 if (fastload
== NULL
) {
5896 LOG_ERROR("No image in memory");
5900 int64_t ms
= timeval_ms();
5902 int retval
= ERROR_OK
;
5903 for (i
= 0; i
< fastload_num
; i
++) {
5904 struct target
*target
= get_current_target(CMD_CTX
);
5905 command_print(CMD_CTX
, "Write to 0x%08x, length 0x%08x",
5906 (unsigned int)(fastload
[i
].address
),
5907 (unsigned int)(fastload
[i
].length
));
5908 retval
= target_write_buffer(target
, fastload
[i
].address
, fastload
[i
].length
, fastload
[i
].data
);
5909 if (retval
!= ERROR_OK
)
5911 size
+= fastload
[i
].length
;
5913 if (retval
== ERROR_OK
) {
5914 int64_t after
= timeval_ms();
5915 command_print(CMD_CTX
, "Loaded image %f kBytes/s", (float)(size
/1024.0)/((float)(after
-ms
)/1000.0));
5920 static const struct command_registration target_command_handlers
[] = {
5923 .handler
= handle_targets_command
,
5924 .mode
= COMMAND_ANY
,
5925 .help
= "change current default target (one parameter) "
5926 "or prints table of all targets (no parameters)",
5927 .usage
= "[target]",
5931 .mode
= COMMAND_CONFIG
,
5932 .help
= "configure target",
5934 .chain
= target_subcommand_handlers
,
5936 COMMAND_REGISTRATION_DONE
5939 int target_register_commands(struct command_context
*cmd_ctx
)
5941 return register_commands(cmd_ctx
, NULL
, target_command_handlers
);
5944 static bool target_reset_nag
= true;
5946 bool get_target_reset_nag(void)
5948 return target_reset_nag
;
5951 COMMAND_HANDLER(handle_target_reset_nag
)
5953 return CALL_COMMAND_HANDLER(handle_command_parse_bool
,
5954 &target_reset_nag
, "Nag after each reset about options to improve "
5958 COMMAND_HANDLER(handle_ps_command
)
5960 struct target
*target
= get_current_target(CMD_CTX
);
5962 if (target
->state
!= TARGET_HALTED
) {
5963 LOG_INFO("target not halted !!");
5967 if ((target
->rtos
) && (target
->rtos
->type
)
5968 && (target
->rtos
->type
->ps_command
)) {
5969 display
= target
->rtos
->type
->ps_command(target
);
5970 command_print(CMD_CTX
, "%s", display
);
5975 return ERROR_TARGET_FAILURE
;
5979 static void binprint(struct command_context
*cmd_ctx
, const char *text
, const uint8_t *buf
, int size
)
5982 command_print_sameline(cmd_ctx
, "%s", text
);
5983 for (int i
= 0; i
< size
; i
++)
5984 command_print_sameline(cmd_ctx
, " %02x", buf
[i
]);
5985 command_print(cmd_ctx
, " ");
5988 COMMAND_HANDLER(handle_test_mem_access_command
)
5990 struct target
*target
= get_current_target(CMD_CTX
);
5992 int retval
= ERROR_OK
;
5994 if (target
->state
!= TARGET_HALTED
) {
5995 LOG_INFO("target not halted !!");
6000 return ERROR_COMMAND_SYNTAX_ERROR
;
6002 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], test_size
);
6005 size_t num_bytes
= test_size
+ 4;
6007 struct working_area
*wa
= NULL
;
6008 retval
= target_alloc_working_area(target
, num_bytes
, &wa
);
6009 if (retval
!= ERROR_OK
) {
6010 LOG_ERROR("Not enough working area");
6014 uint8_t *test_pattern
= malloc(num_bytes
);
6016 for (size_t i
= 0; i
< num_bytes
; i
++)
6017 test_pattern
[i
] = rand();
6019 retval
= target_write_memory(target
, wa
->address
, 1, num_bytes
, test_pattern
);
6020 if (retval
!= ERROR_OK
) {
6021 LOG_ERROR("Test pattern write failed");
6025 for (int host_offset
= 0; host_offset
<= 1; host_offset
++) {
6026 for (int size
= 1; size
<= 4; size
*= 2) {
6027 for (int offset
= 0; offset
< 4; offset
++) {
6028 uint32_t count
= test_size
/ size
;
6029 size_t host_bufsiz
= (count
+ 2) * size
+ host_offset
;
6030 uint8_t *read_ref
= malloc(host_bufsiz
);
6031 uint8_t *read_buf
= malloc(host_bufsiz
);
6033 for (size_t i
= 0; i
< host_bufsiz
; i
++) {
6034 read_ref
[i
] = rand();
6035 read_buf
[i
] = read_ref
[i
];
6037 command_print_sameline(CMD_CTX
,
6038 "Test read %" PRIu32
" x %d @ %d to %saligned buffer: ", count
,
6039 size
, offset
, host_offset
? "un" : "");
6041 struct duration bench
;
6042 duration_start(&bench
);
6044 retval
= target_read_memory(target
, wa
->address
+ offset
, size
, count
,
6045 read_buf
+ size
+ host_offset
);
6047 duration_measure(&bench
);
6049 if (retval
== ERROR_TARGET_UNALIGNED_ACCESS
) {
6050 command_print(CMD_CTX
, "Unsupported alignment");
6052 } else if (retval
!= ERROR_OK
) {
6053 command_print(CMD_CTX
, "Memory read failed");
6057 /* replay on host */
6058 memcpy(read_ref
+ size
+ host_offset
, test_pattern
+ offset
, count
* size
);
6061 int result
= memcmp(read_ref
, read_buf
, host_bufsiz
);
6063 command_print(CMD_CTX
, "Pass in %fs (%0.3f KiB/s)",
6064 duration_elapsed(&bench
),
6065 duration_kbps(&bench
, count
* size
));
6067 command_print(CMD_CTX
, "Compare failed");
6068 binprint(CMD_CTX
, "ref:", read_ref
, host_bufsiz
);
6069 binprint(CMD_CTX
, "buf:", read_buf
, host_bufsiz
);
6082 target_free_working_area(target
, wa
);
6085 num_bytes
= test_size
+ 4 + 4 + 4;
6087 retval
= target_alloc_working_area(target
, num_bytes
, &wa
);
6088 if (retval
!= ERROR_OK
) {
6089 LOG_ERROR("Not enough working area");
6093 test_pattern
= malloc(num_bytes
);
6095 for (size_t i
= 0; i
< num_bytes
; i
++)
6096 test_pattern
[i
] = rand();
6098 for (int host_offset
= 0; host_offset
<= 1; host_offset
++) {
6099 for (int size
= 1; size
<= 4; size
*= 2) {
6100 for (int offset
= 0; offset
< 4; offset
++) {
6101 uint32_t count
= test_size
/ size
;
6102 size_t host_bufsiz
= count
* size
+ host_offset
;
6103 uint8_t *read_ref
= malloc(num_bytes
);
6104 uint8_t *read_buf
= malloc(num_bytes
);
6105 uint8_t *write_buf
= malloc(host_bufsiz
);
6107 for (size_t i
= 0; i
< host_bufsiz
; i
++)
6108 write_buf
[i
] = rand();
6109 command_print_sameline(CMD_CTX
,
6110 "Test write %" PRIu32
" x %d @ %d from %saligned buffer: ", count
,
6111 size
, offset
, host_offset
? "un" : "");
6113 retval
= target_write_memory(target
, wa
->address
, 1, num_bytes
, test_pattern
);
6114 if (retval
!= ERROR_OK
) {
6115 command_print(CMD_CTX
, "Test pattern write failed");
6119 /* replay on host */
6120 memcpy(read_ref
, test_pattern
, num_bytes
);
6121 memcpy(read_ref
+ size
+ offset
, write_buf
+ host_offset
, count
* size
);
6123 struct duration bench
;
6124 duration_start(&bench
);
6126 retval
= target_write_memory(target
, wa
->address
+ size
+ offset
, size
, count
,
6127 write_buf
+ host_offset
);
6129 duration_measure(&bench
);
6131 if (retval
== ERROR_TARGET_UNALIGNED_ACCESS
) {
6132 command_print(CMD_CTX
, "Unsupported alignment");
6134 } else if (retval
!= ERROR_OK
) {
6135 command_print(CMD_CTX
, "Memory write failed");
6140 retval
= target_read_memory(target
, wa
->address
, 1, num_bytes
, read_buf
);
6141 if (retval
!= ERROR_OK
) {
6142 command_print(CMD_CTX
, "Test pattern write failed");
6147 int result
= memcmp(read_ref
, read_buf
, num_bytes
);
6149 command_print(CMD_CTX
, "Pass in %fs (%0.3f KiB/s)",
6150 duration_elapsed(&bench
),
6151 duration_kbps(&bench
, count
* size
));
6153 command_print(CMD_CTX
, "Compare failed");
6154 binprint(CMD_CTX
, "ref:", read_ref
, num_bytes
);
6155 binprint(CMD_CTX
, "buf:", read_buf
, num_bytes
);
6167 target_free_working_area(target
, wa
);
6171 static const struct command_registration target_exec_command_handlers
[] = {
6173 .name
= "fast_load_image",
6174 .handler
= handle_fast_load_image_command
,
6175 .mode
= COMMAND_ANY
,
6176 .help
= "Load image into server memory for later use by "
6177 "fast_load; primarily for profiling",
6178 .usage
= "filename address ['bin'|'ihex'|'elf'|'s19'] "
6179 "[min_address [max_length]]",
6182 .name
= "fast_load",
6183 .handler
= handle_fast_load_command
,
6184 .mode
= COMMAND_EXEC
,
6185 .help
= "loads active fast load image to current target "
6186 "- mainly for profiling purposes",
6191 .handler
= handle_profile_command
,
6192 .mode
= COMMAND_EXEC
,
6193 .usage
= "seconds filename [start end]",
6194 .help
= "profiling samples the CPU PC",
6196 /** @todo don't register virt2phys() unless target supports it */
6198 .name
= "virt2phys",
6199 .handler
= handle_virt2phys_command
,
6200 .mode
= COMMAND_ANY
,
6201 .help
= "translate a virtual address into a physical address",
6202 .usage
= "virtual_address",
6206 .handler
= handle_reg_command
,
6207 .mode
= COMMAND_EXEC
,
6208 .help
= "display (reread from target with \"force\") or set a register; "
6209 "with no arguments, displays all registers and their values",
6210 .usage
= "[(register_number|register_name) [(value|'force')]]",
6214 .handler
= handle_poll_command
,
6215 .mode
= COMMAND_EXEC
,
6216 .help
= "poll target state; or reconfigure background polling",
6217 .usage
= "['on'|'off']",
6220 .name
= "wait_halt",
6221 .handler
= handle_wait_halt_command
,
6222 .mode
= COMMAND_EXEC
,
6223 .help
= "wait up to the specified number of milliseconds "
6224 "(default 5000) for a previously requested halt",
6225 .usage
= "[milliseconds]",
6229 .handler
= handle_halt_command
,
6230 .mode
= COMMAND_EXEC
,
6231 .help
= "request target to halt, then wait up to the specified"
6232 "number of milliseconds (default 5000) for it to complete",
6233 .usage
= "[milliseconds]",
6237 .handler
= handle_resume_command
,
6238 .mode
= COMMAND_EXEC
,
6239 .help
= "resume target execution from current PC or address",
6240 .usage
= "[address]",
6244 .handler
= handle_reset_command
,
6245 .mode
= COMMAND_EXEC
,
6246 .usage
= "[run|halt|init]",
6247 .help
= "Reset all targets into the specified mode."
6248 "Default reset mode is run, if not given.",
6251 .name
= "soft_reset_halt",
6252 .handler
= handle_soft_reset_halt_command
,
6253 .mode
= COMMAND_EXEC
,
6255 .help
= "halt the target and do a soft reset",
6259 .handler
= handle_step_command
,
6260 .mode
= COMMAND_EXEC
,
6261 .help
= "step one instruction from current PC or address",
6262 .usage
= "[address]",
6266 .handler
= handle_md_command
,
6267 .mode
= COMMAND_EXEC
,
6268 .help
= "display memory words",
6269 .usage
= "['phys'] address [count]",
6273 .handler
= handle_md_command
,
6274 .mode
= COMMAND_EXEC
,
6275 .help
= "display memory words",
6276 .usage
= "['phys'] address [count]",
6280 .handler
= handle_md_command
,
6281 .mode
= COMMAND_EXEC
,
6282 .help
= "display memory half-words",
6283 .usage
= "['phys'] address [count]",
6287 .handler
= handle_md_command
,
6288 .mode
= COMMAND_EXEC
,
6289 .help
= "display memory bytes",
6290 .usage
= "['phys'] address [count]",
6294 .handler
= handle_mw_command
,
6295 .mode
= COMMAND_EXEC
,
6296 .help
= "write memory word",
6297 .usage
= "['phys'] address value [count]",
6301 .handler
= handle_mw_command
,
6302 .mode
= COMMAND_EXEC
,
6303 .help
= "write memory word",
6304 .usage
= "['phys'] address value [count]",
6308 .handler
= handle_mw_command
,
6309 .mode
= COMMAND_EXEC
,
6310 .help
= "write memory half-word",
6311 .usage
= "['phys'] address value [count]",
6315 .handler
= handle_mw_command
,
6316 .mode
= COMMAND_EXEC
,
6317 .help
= "write memory byte",
6318 .usage
= "['phys'] address value [count]",
6322 .handler
= handle_bp_command
,
6323 .mode
= COMMAND_EXEC
,
6324 .help
= "list or set hardware or software breakpoint",
6325 .usage
= "<address> [<asid>]<length> ['hw'|'hw_ctx']",
6329 .handler
= handle_rbp_command
,
6330 .mode
= COMMAND_EXEC
,
6331 .help
= "remove breakpoint",
6336 .handler
= handle_wp_command
,
6337 .mode
= COMMAND_EXEC
,
6338 .help
= "list (no params) or create watchpoints",
6339 .usage
= "[address length [('r'|'w'|'a') value [mask]]]",
6343 .handler
= handle_rwp_command
,
6344 .mode
= COMMAND_EXEC
,
6345 .help
= "remove watchpoint",
6349 .name
= "load_image",
6350 .handler
= handle_load_image_command
,
6351 .mode
= COMMAND_EXEC
,
6352 .usage
= "filename address ['bin'|'ihex'|'elf'|'s19'] "
6353 "[min_address] [max_length]",
6356 .name
= "dump_image",
6357 .handler
= handle_dump_image_command
,
6358 .mode
= COMMAND_EXEC
,
6359 .usage
= "filename address size",
6362 .name
= "verify_image_checksum",
6363 .handler
= handle_verify_image_checksum_command
,
6364 .mode
= COMMAND_EXEC
,
6365 .usage
= "filename [offset [type]]",
6368 .name
= "verify_image",
6369 .handler
= handle_verify_image_command
,
6370 .mode
= COMMAND_EXEC
,
6371 .usage
= "filename [offset [type]]",
6374 .name
= "test_image",
6375 .handler
= handle_test_image_command
,
6376 .mode
= COMMAND_EXEC
,
6377 .usage
= "filename [offset [type]]",
6380 .name
= "mem2array",
6381 .mode
= COMMAND_EXEC
,
6382 .jim_handler
= jim_mem2array
,
6383 .help
= "read 8/16/32 bit memory and return as a TCL array "
6384 "for script processing",
6385 .usage
= "arrayname bitwidth address count",
6388 .name
= "array2mem",
6389 .mode
= COMMAND_EXEC
,
6390 .jim_handler
= jim_array2mem
,
6391 .help
= "convert a TCL array to memory locations "
6392 "and write the 8/16/32 bit values",
6393 .usage
= "arrayname bitwidth address count",
6396 .name
= "reset_nag",
6397 .handler
= handle_target_reset_nag
,
6398 .mode
= COMMAND_ANY
,
6399 .help
= "Nag after each reset about options that could have been "
6400 "enabled to improve performance. ",
6401 .usage
= "['enable'|'disable']",
6405 .handler
= handle_ps_command
,
6406 .mode
= COMMAND_EXEC
,
6407 .help
= "list all tasks ",
6411 .name
= "test_mem_access",
6412 .handler
= handle_test_mem_access_command
,
6413 .mode
= COMMAND_EXEC
,
6414 .help
= "Test the target's memory access functions",
6418 COMMAND_REGISTRATION_DONE
6420 static int target_register_user_commands(struct command_context
*cmd_ctx
)
6422 int retval
= ERROR_OK
;
6423 retval
= target_request_register_commands(cmd_ctx
);
6424 if (retval
!= ERROR_OK
)
6427 retval
= trace_register_commands(cmd_ctx
);
6428 if (retval
!= ERROR_OK
)
6432 return register_commands(cmd_ctx
, NULL
, target_exec_command_handlers
);