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 * Streams data to a circular buffer on target intended for consumption by code
886 * running asynchronously on target.
888 * This is intended for applications where target-specific native code runs
889 * on the target, receives data from the circular buffer, does something with
890 * it (most likely writing it to a flash memory), and advances the circular
893 * This assumes that the helper algorithm has already been loaded to the target,
894 * but has not been started yet. Given memory and register parameters are passed
897 * The buffer is defined by (buffer_start, buffer_size) arguments and has the
900 * [buffer_start + 0, buffer_start + 4):
901 * Write Pointer address (aka head). Written and updated by this
902 * routine when new data is written to the circular buffer.
903 * [buffer_start + 4, buffer_start + 8):
904 * Read Pointer address (aka tail). Updated by code running on the
905 * target after it consumes data.
906 * [buffer_start + 8, buffer_start + buffer_size):
907 * Circular buffer contents.
909 * See contrib/loaders/flash/stm32f1x.S for an example.
911 * @param target used to run the algorithm
912 * @param buffer address on the host where data to be sent is located
913 * @param count number of blocks to send
914 * @param block_size size in bytes of each block
915 * @param num_mem_params count of memory-based params to pass to algorithm
916 * @param mem_params memory-based params to pass to algorithm
917 * @param num_reg_params count of register-based params to pass to algorithm
918 * @param reg_params memory-based params to pass to algorithm
919 * @param buffer_start address on the target of the circular buffer structure
920 * @param buffer_size size of the circular buffer structure
921 * @param entry_point address on the target to execute to start the algorithm
922 * @param exit_point address at which to set a breakpoint to catch the
923 * end of the algorithm; can be 0 if target triggers a breakpoint itself
926 int target_run_flash_async_algorithm(struct target
*target
,
927 const uint8_t *buffer
, uint32_t count
, int block_size
,
928 int num_mem_params
, struct mem_param
*mem_params
,
929 int num_reg_params
, struct reg_param
*reg_params
,
930 uint32_t buffer_start
, uint32_t buffer_size
,
931 uint32_t entry_point
, uint32_t exit_point
, void *arch_info
)
936 const uint8_t *buffer_orig
= buffer
;
938 /* Set up working area. First word is write pointer, second word is read pointer,
939 * rest is fifo data area. */
940 uint32_t wp_addr
= buffer_start
;
941 uint32_t rp_addr
= buffer_start
+ 4;
942 uint32_t fifo_start_addr
= buffer_start
+ 8;
943 uint32_t fifo_end_addr
= buffer_start
+ buffer_size
;
945 uint32_t wp
= fifo_start_addr
;
946 uint32_t rp
= fifo_start_addr
;
948 /* validate block_size is 2^n */
949 assert(!block_size
|| !(block_size
& (block_size
- 1)));
951 retval
= target_write_u32(target
, wp_addr
, wp
);
952 if (retval
!= ERROR_OK
)
954 retval
= target_write_u32(target
, rp_addr
, rp
);
955 if (retval
!= ERROR_OK
)
958 /* Start up algorithm on target and let it idle while writing the first chunk */
959 retval
= target_start_algorithm(target
, num_mem_params
, mem_params
,
960 num_reg_params
, reg_params
,
965 if (retval
!= ERROR_OK
) {
966 LOG_ERROR("error starting target flash write algorithm");
972 retval
= target_read_u32(target
, rp_addr
, &rp
);
973 if (retval
!= ERROR_OK
) {
974 LOG_ERROR("failed to get read pointer");
978 LOG_DEBUG("offs 0x%zx count 0x%" PRIx32
" wp 0x%" PRIx32
" rp 0x%" PRIx32
,
979 (size_t) (buffer
- buffer_orig
), count
, wp
, rp
);
982 LOG_ERROR("flash write algorithm aborted by target");
983 retval
= ERROR_FLASH_OPERATION_FAILED
;
987 if (((rp
- fifo_start_addr
) & (block_size
- 1)) || rp
< fifo_start_addr
|| rp
>= fifo_end_addr
) {
988 LOG_ERROR("corrupted fifo read pointer 0x%" PRIx32
, rp
);
992 /* Count the number of bytes available in the fifo without
993 * crossing the wrap around. Make sure to not fill it completely,
994 * because that would make wp == rp and that's the empty condition. */
995 uint32_t thisrun_bytes
;
997 thisrun_bytes
= rp
- wp
- block_size
;
998 else if (rp
> fifo_start_addr
)
999 thisrun_bytes
= fifo_end_addr
- wp
;
1001 thisrun_bytes
= fifo_end_addr
- wp
- block_size
;
1003 if (thisrun_bytes
== 0) {
1004 /* Throttle polling a bit if transfer is (much) faster than flash
1005 * programming. The exact delay shouldn't matter as long as it's
1006 * less than buffer size / flash speed. This is very unlikely to
1007 * run when using high latency connections such as USB. */
1010 /* to stop an infinite loop on some targets check and increment a timeout
1011 * this issue was observed on a stellaris using the new ICDI interface */
1012 if (timeout
++ >= 500) {
1013 LOG_ERROR("timeout waiting for algorithm, a target reset is recommended");
1014 return ERROR_FLASH_OPERATION_FAILED
;
1019 /* reset our timeout */
1022 /* Limit to the amount of data we actually want to write */
1023 if (thisrun_bytes
> count
* block_size
)
1024 thisrun_bytes
= count
* block_size
;
1026 /* Write data to fifo */
1027 retval
= target_write_buffer(target
, wp
, thisrun_bytes
, buffer
);
1028 if (retval
!= ERROR_OK
)
1031 /* Update counters and wrap write pointer */
1032 buffer
+= thisrun_bytes
;
1033 count
-= thisrun_bytes
/ block_size
;
1034 wp
+= thisrun_bytes
;
1035 if (wp
>= fifo_end_addr
)
1036 wp
= fifo_start_addr
;
1038 /* Store updated write pointer to target */
1039 retval
= target_write_u32(target
, wp_addr
, wp
);
1040 if (retval
!= ERROR_OK
)
1044 if (retval
!= ERROR_OK
) {
1045 /* abort flash write algorithm on target */
1046 target_write_u32(target
, wp_addr
, 0);
1049 int retval2
= target_wait_algorithm(target
, num_mem_params
, mem_params
,
1050 num_reg_params
, reg_params
,
1055 if (retval2
!= ERROR_OK
) {
1056 LOG_ERROR("error waiting for target flash write algorithm");
1060 if (retval
== ERROR_OK
) {
1061 /* check if algorithm set rp = 0 after fifo writer loop finished */
1062 retval
= target_read_u32(target
, rp_addr
, &rp
);
1063 if (retval
== ERROR_OK
&& rp
== 0) {
1064 LOG_ERROR("flash write algorithm aborted by target");
1065 retval
= ERROR_FLASH_OPERATION_FAILED
;
1072 int target_read_memory(struct target
*target
,
1073 target_addr_t address
, uint32_t size
, uint32_t count
, uint8_t *buffer
)
1075 if (!target_was_examined(target
)) {
1076 LOG_ERROR("Target not examined yet");
1079 if (!target
->type
->read_memory
) {
1080 LOG_ERROR("Target %s doesn't support read_memory", target_name(target
));
1083 return target
->type
->read_memory(target
, address
, size
, count
, buffer
);
1086 int target_read_phys_memory(struct target
*target
,
1087 target_addr_t address
, uint32_t size
, uint32_t count
, uint8_t *buffer
)
1089 if (!target_was_examined(target
)) {
1090 LOG_ERROR("Target not examined yet");
1093 if (!target
->type
->read_phys_memory
) {
1094 LOG_ERROR("Target %s doesn't support read_phys_memory", target_name(target
));
1097 return target
->type
->read_phys_memory(target
, address
, size
, count
, buffer
);
1100 int target_write_memory(struct target
*target
,
1101 target_addr_t address
, uint32_t size
, uint32_t count
, const uint8_t *buffer
)
1103 if (!target_was_examined(target
)) {
1104 LOG_ERROR("Target not examined yet");
1107 if (!target
->type
->write_memory
) {
1108 LOG_ERROR("Target %s doesn't support write_memory", target_name(target
));
1111 return target
->type
->write_memory(target
, address
, size
, count
, buffer
);
1114 int target_write_phys_memory(struct target
*target
,
1115 target_addr_t address
, uint32_t size
, uint32_t count
, const uint8_t *buffer
)
1117 if (!target_was_examined(target
)) {
1118 LOG_ERROR("Target not examined yet");
1121 if (!target
->type
->write_phys_memory
) {
1122 LOG_ERROR("Target %s doesn't support write_phys_memory", target_name(target
));
1125 return target
->type
->write_phys_memory(target
, address
, size
, count
, buffer
);
1128 int target_add_breakpoint(struct target
*target
,
1129 struct breakpoint
*breakpoint
)
1131 if ((target
->state
!= TARGET_HALTED
) && (breakpoint
->type
!= BKPT_HARD
)) {
1132 LOG_WARNING("target %s is not halted (add breakpoint)", target_name(target
));
1133 return ERROR_TARGET_NOT_HALTED
;
1135 return target
->type
->add_breakpoint(target
, breakpoint
);
1138 int target_add_context_breakpoint(struct target
*target
,
1139 struct breakpoint
*breakpoint
)
1141 if (target
->state
!= TARGET_HALTED
) {
1142 LOG_WARNING("target %s is not halted (add context breakpoint)", target_name(target
));
1143 return ERROR_TARGET_NOT_HALTED
;
1145 return target
->type
->add_context_breakpoint(target
, breakpoint
);
1148 int target_add_hybrid_breakpoint(struct target
*target
,
1149 struct breakpoint
*breakpoint
)
1151 if (target
->state
!= TARGET_HALTED
) {
1152 LOG_WARNING("target %s is not halted (add hybrid breakpoint)", target_name(target
));
1153 return ERROR_TARGET_NOT_HALTED
;
1155 return target
->type
->add_hybrid_breakpoint(target
, breakpoint
);
1158 int target_remove_breakpoint(struct target
*target
,
1159 struct breakpoint
*breakpoint
)
1161 return target
->type
->remove_breakpoint(target
, breakpoint
);
1164 int target_add_watchpoint(struct target
*target
,
1165 struct watchpoint
*watchpoint
)
1167 if (target
->state
!= TARGET_HALTED
) {
1168 LOG_WARNING("target %s is not halted (add watchpoint)", target_name(target
));
1169 return ERROR_TARGET_NOT_HALTED
;
1171 return target
->type
->add_watchpoint(target
, watchpoint
);
1173 int target_remove_watchpoint(struct target
*target
,
1174 struct watchpoint
*watchpoint
)
1176 return target
->type
->remove_watchpoint(target
, watchpoint
);
1178 int target_hit_watchpoint(struct target
*target
,
1179 struct watchpoint
**hit_watchpoint
)
1181 if (target
->state
!= TARGET_HALTED
) {
1182 LOG_WARNING("target %s is not halted (hit watchpoint)", target
->cmd_name
);
1183 return ERROR_TARGET_NOT_HALTED
;
1186 if (target
->type
->hit_watchpoint
== NULL
) {
1187 /* For backward compatible, if hit_watchpoint is not implemented,
1188 * return ERROR_FAIL such that gdb_server will not take the nonsense
1193 return target
->type
->hit_watchpoint(target
, hit_watchpoint
);
1196 int target_get_gdb_reg_list(struct target
*target
,
1197 struct reg
**reg_list
[], int *reg_list_size
,
1198 enum target_register_class reg_class
)
1200 return target
->type
->get_gdb_reg_list(target
, reg_list
, reg_list_size
, reg_class
);
1202 int target_step(struct target
*target
,
1203 int current
, target_addr_t address
, int handle_breakpoints
)
1205 return target
->type
->step(target
, current
, address
, handle_breakpoints
);
1208 int target_get_gdb_fileio_info(struct target
*target
, struct gdb_fileio_info
*fileio_info
)
1210 if (target
->state
!= TARGET_HALTED
) {
1211 LOG_WARNING("target %s is not halted (gdb fileio)", target
->cmd_name
);
1212 return ERROR_TARGET_NOT_HALTED
;
1214 return target
->type
->get_gdb_fileio_info(target
, fileio_info
);
1217 int target_gdb_fileio_end(struct target
*target
, int retcode
, int fileio_errno
, bool ctrl_c
)
1219 if (target
->state
!= TARGET_HALTED
) {
1220 LOG_WARNING("target %s is not halted (gdb fileio end)", target
->cmd_name
);
1221 return ERROR_TARGET_NOT_HALTED
;
1223 return target
->type
->gdb_fileio_end(target
, retcode
, fileio_errno
, ctrl_c
);
1226 int target_profiling(struct target
*target
, uint32_t *samples
,
1227 uint32_t max_num_samples
, uint32_t *num_samples
, uint32_t seconds
)
1229 if (target
->state
!= TARGET_HALTED
) {
1230 LOG_WARNING("target %s is not halted (profiling)", target
->cmd_name
);
1231 return ERROR_TARGET_NOT_HALTED
;
1233 return target
->type
->profiling(target
, samples
, max_num_samples
,
1234 num_samples
, seconds
);
1238 * Reset the @c examined flag for the given target.
1239 * Pure paranoia -- targets are zeroed on allocation.
1241 static void target_reset_examined(struct target
*target
)
1243 target
->examined
= false;
1246 static int handle_target(void *priv
);
1248 static int target_init_one(struct command_context
*cmd_ctx
,
1249 struct target
*target
)
1251 target_reset_examined(target
);
1253 struct target_type
*type
= target
->type
;
1254 if (type
->examine
== NULL
)
1255 type
->examine
= default_examine
;
1257 if (type
->check_reset
== NULL
)
1258 type
->check_reset
= default_check_reset
;
1260 assert(type
->init_target
!= NULL
);
1262 int retval
= type
->init_target(cmd_ctx
, target
);
1263 if (ERROR_OK
!= retval
) {
1264 LOG_ERROR("target '%s' init failed", target_name(target
));
1268 /* Sanity-check MMU support ... stub in what we must, to help
1269 * implement it in stages, but warn if we need to do so.
1272 if (type
->virt2phys
== NULL
) {
1273 LOG_ERROR("type '%s' is missing virt2phys", type
->name
);
1274 type
->virt2phys
= identity_virt2phys
;
1277 /* Make sure no-MMU targets all behave the same: make no
1278 * distinction between physical and virtual addresses, and
1279 * ensure that virt2phys() is always an identity mapping.
1281 if (type
->write_phys_memory
|| type
->read_phys_memory
|| type
->virt2phys
)
1282 LOG_WARNING("type '%s' has bad MMU hooks", type
->name
);
1285 type
->write_phys_memory
= type
->write_memory
;
1286 type
->read_phys_memory
= type
->read_memory
;
1287 type
->virt2phys
= identity_virt2phys
;
1290 if (target
->type
->read_buffer
== NULL
)
1291 target
->type
->read_buffer
= target_read_buffer_default
;
1293 if (target
->type
->write_buffer
== NULL
)
1294 target
->type
->write_buffer
= target_write_buffer_default
;
1296 if (target
->type
->get_gdb_fileio_info
== NULL
)
1297 target
->type
->get_gdb_fileio_info
= target_get_gdb_fileio_info_default
;
1299 if (target
->type
->gdb_fileio_end
== NULL
)
1300 target
->type
->gdb_fileio_end
= target_gdb_fileio_end_default
;
1302 if (target
->type
->profiling
== NULL
)
1303 target
->type
->profiling
= target_profiling_default
;
1308 static int target_init(struct command_context
*cmd_ctx
)
1310 struct target
*target
;
1313 for (target
= all_targets
; target
; target
= target
->next
) {
1314 retval
= target_init_one(cmd_ctx
, target
);
1315 if (ERROR_OK
!= retval
)
1322 retval
= target_register_user_commands(cmd_ctx
);
1323 if (ERROR_OK
!= retval
)
1326 retval
= target_register_timer_callback(&handle_target
,
1327 polling_interval
, 1, cmd_ctx
->interp
);
1328 if (ERROR_OK
!= retval
)
1334 COMMAND_HANDLER(handle_target_init_command
)
1339 return ERROR_COMMAND_SYNTAX_ERROR
;
1341 static bool target_initialized
;
1342 if (target_initialized
) {
1343 LOG_INFO("'target init' has already been called");
1346 target_initialized
= true;
1348 retval
= command_run_line(CMD_CTX
, "init_targets");
1349 if (ERROR_OK
!= retval
)
1352 retval
= command_run_line(CMD_CTX
, "init_target_events");
1353 if (ERROR_OK
!= retval
)
1356 retval
= command_run_line(CMD_CTX
, "init_board");
1357 if (ERROR_OK
!= retval
)
1360 LOG_DEBUG("Initializing targets...");
1361 return target_init(CMD_CTX
);
1364 int target_register_event_callback(int (*callback
)(struct target
*target
,
1365 enum target_event event
, void *priv
), void *priv
)
1367 struct target_event_callback
**callbacks_p
= &target_event_callbacks
;
1369 if (callback
== NULL
)
1370 return ERROR_COMMAND_SYNTAX_ERROR
;
1373 while ((*callbacks_p
)->next
)
1374 callbacks_p
= &((*callbacks_p
)->next
);
1375 callbacks_p
= &((*callbacks_p
)->next
);
1378 (*callbacks_p
) = malloc(sizeof(struct target_event_callback
));
1379 (*callbacks_p
)->callback
= callback
;
1380 (*callbacks_p
)->priv
= priv
;
1381 (*callbacks_p
)->next
= NULL
;
1386 int target_register_reset_callback(int (*callback
)(struct target
*target
,
1387 enum target_reset_mode reset_mode
, void *priv
), void *priv
)
1389 struct target_reset_callback
*entry
;
1391 if (callback
== NULL
)
1392 return ERROR_COMMAND_SYNTAX_ERROR
;
1394 entry
= malloc(sizeof(struct target_reset_callback
));
1395 if (entry
== NULL
) {
1396 LOG_ERROR("error allocating buffer for reset callback entry");
1397 return ERROR_COMMAND_SYNTAX_ERROR
;
1400 entry
->callback
= callback
;
1402 list_add(&entry
->list
, &target_reset_callback_list
);
1408 int target_register_trace_callback(int (*callback
)(struct target
*target
,
1409 size_t len
, uint8_t *data
, void *priv
), void *priv
)
1411 struct target_trace_callback
*entry
;
1413 if (callback
== NULL
)
1414 return ERROR_COMMAND_SYNTAX_ERROR
;
1416 entry
= malloc(sizeof(struct target_trace_callback
));
1417 if (entry
== NULL
) {
1418 LOG_ERROR("error allocating buffer for trace callback entry");
1419 return ERROR_COMMAND_SYNTAX_ERROR
;
1422 entry
->callback
= callback
;
1424 list_add(&entry
->list
, &target_trace_callback_list
);
1430 int target_register_timer_callback(int (*callback
)(void *priv
), int time_ms
, int periodic
, void *priv
)
1432 struct target_timer_callback
**callbacks_p
= &target_timer_callbacks
;
1434 if (callback
== NULL
)
1435 return ERROR_COMMAND_SYNTAX_ERROR
;
1438 while ((*callbacks_p
)->next
)
1439 callbacks_p
= &((*callbacks_p
)->next
);
1440 callbacks_p
= &((*callbacks_p
)->next
);
1443 (*callbacks_p
) = malloc(sizeof(struct target_timer_callback
));
1444 (*callbacks_p
)->callback
= callback
;
1445 (*callbacks_p
)->periodic
= periodic
;
1446 (*callbacks_p
)->time_ms
= time_ms
;
1447 (*callbacks_p
)->removed
= false;
1449 gettimeofday(&(*callbacks_p
)->when
, NULL
);
1450 timeval_add_time(&(*callbacks_p
)->when
, 0, time_ms
* 1000);
1452 (*callbacks_p
)->priv
= priv
;
1453 (*callbacks_p
)->next
= NULL
;
1458 int target_unregister_event_callback(int (*callback
)(struct target
*target
,
1459 enum target_event event
, void *priv
), void *priv
)
1461 struct target_event_callback
**p
= &target_event_callbacks
;
1462 struct target_event_callback
*c
= target_event_callbacks
;
1464 if (callback
== NULL
)
1465 return ERROR_COMMAND_SYNTAX_ERROR
;
1468 struct target_event_callback
*next
= c
->next
;
1469 if ((c
->callback
== callback
) && (c
->priv
== priv
)) {
1481 int target_unregister_reset_callback(int (*callback
)(struct target
*target
,
1482 enum target_reset_mode reset_mode
, void *priv
), void *priv
)
1484 struct target_reset_callback
*entry
;
1486 if (callback
== NULL
)
1487 return ERROR_COMMAND_SYNTAX_ERROR
;
1489 list_for_each_entry(entry
, &target_reset_callback_list
, list
) {
1490 if (entry
->callback
== callback
&& entry
->priv
== priv
) {
1491 list_del(&entry
->list
);
1500 int target_unregister_trace_callback(int (*callback
)(struct target
*target
,
1501 size_t len
, uint8_t *data
, void *priv
), void *priv
)
1503 struct target_trace_callback
*entry
;
1505 if (callback
== NULL
)
1506 return ERROR_COMMAND_SYNTAX_ERROR
;
1508 list_for_each_entry(entry
, &target_trace_callback_list
, list
) {
1509 if (entry
->callback
== callback
&& entry
->priv
== priv
) {
1510 list_del(&entry
->list
);
1519 int target_unregister_timer_callback(int (*callback
)(void *priv
), void *priv
)
1521 if (callback
== NULL
)
1522 return ERROR_COMMAND_SYNTAX_ERROR
;
1524 for (struct target_timer_callback
*c
= target_timer_callbacks
;
1526 if ((c
->callback
== callback
) && (c
->priv
== priv
)) {
1535 int target_call_event_callbacks(struct target
*target
, enum target_event event
)
1537 struct target_event_callback
*callback
= target_event_callbacks
;
1538 struct target_event_callback
*next_callback
;
1540 if (event
== TARGET_EVENT_HALTED
) {
1541 /* execute early halted first */
1542 target_call_event_callbacks(target
, TARGET_EVENT_GDB_HALT
);
1545 LOG_DEBUG("target event %i (%s)", event
,
1546 Jim_Nvp_value2name_simple(nvp_target_event
, event
)->name
);
1548 target_handle_event(target
, event
);
1551 next_callback
= callback
->next
;
1552 callback
->callback(target
, event
, callback
->priv
);
1553 callback
= next_callback
;
1559 int target_call_reset_callbacks(struct target
*target
, enum target_reset_mode reset_mode
)
1561 struct target_reset_callback
*callback
;
1563 LOG_DEBUG("target reset %i (%s)", reset_mode
,
1564 Jim_Nvp_value2name_simple(nvp_reset_modes
, reset_mode
)->name
);
1566 list_for_each_entry(callback
, &target_reset_callback_list
, list
)
1567 callback
->callback(target
, reset_mode
, callback
->priv
);
1572 int target_call_trace_callbacks(struct target
*target
, size_t len
, uint8_t *data
)
1574 struct target_trace_callback
*callback
;
1576 list_for_each_entry(callback
, &target_trace_callback_list
, list
)
1577 callback
->callback(target
, len
, data
, callback
->priv
);
1582 static int target_timer_callback_periodic_restart(
1583 struct target_timer_callback
*cb
, struct timeval
*now
)
1586 timeval_add_time(&cb
->when
, 0, cb
->time_ms
* 1000L);
1590 static int target_call_timer_callback(struct target_timer_callback
*cb
,
1591 struct timeval
*now
)
1593 cb
->callback(cb
->priv
);
1596 return target_timer_callback_periodic_restart(cb
, now
);
1598 return target_unregister_timer_callback(cb
->callback
, cb
->priv
);
1601 static int target_call_timer_callbacks_check_time(int checktime
)
1603 static bool callback_processing
;
1605 /* Do not allow nesting */
1606 if (callback_processing
)
1609 callback_processing
= true;
1614 gettimeofday(&now
, NULL
);
1616 /* Store an address of the place containing a pointer to the
1617 * next item; initially, that's a standalone "root of the
1618 * list" variable. */
1619 struct target_timer_callback
**callback
= &target_timer_callbacks
;
1621 if ((*callback
)->removed
) {
1622 struct target_timer_callback
*p
= *callback
;
1623 *callback
= (*callback
)->next
;
1628 bool call_it
= (*callback
)->callback
&&
1629 ((!checktime
&& (*callback
)->periodic
) ||
1630 timeval_compare(&now
, &(*callback
)->when
) >= 0);
1633 target_call_timer_callback(*callback
, &now
);
1635 callback
= &(*callback
)->next
;
1638 callback_processing
= false;
1642 int target_call_timer_callbacks(void)
1644 return target_call_timer_callbacks_check_time(1);
1647 /* invoke periodic callbacks immediately */
1648 int target_call_timer_callbacks_now(void)
1650 return target_call_timer_callbacks_check_time(0);
1653 /* Prints the working area layout for debug purposes */
1654 static void print_wa_layout(struct target
*target
)
1656 struct working_area
*c
= target
->working_areas
;
1659 LOG_DEBUG("%c%c " TARGET_ADDR_FMT
"-" TARGET_ADDR_FMT
" (%" PRIu32
" bytes)",
1660 c
->backup
? 'b' : ' ', c
->free
? ' ' : '*',
1661 c
->address
, c
->address
+ c
->size
- 1, c
->size
);
1666 /* Reduce area to size bytes, create a new free area from the remaining bytes, if any. */
1667 static void target_split_working_area(struct working_area
*area
, uint32_t size
)
1669 assert(area
->free
); /* Shouldn't split an allocated area */
1670 assert(size
<= area
->size
); /* Caller should guarantee this */
1672 /* Split only if not already the right size */
1673 if (size
< area
->size
) {
1674 struct working_area
*new_wa
= malloc(sizeof(*new_wa
));
1679 new_wa
->next
= area
->next
;
1680 new_wa
->size
= area
->size
- size
;
1681 new_wa
->address
= area
->address
+ size
;
1682 new_wa
->backup
= NULL
;
1683 new_wa
->user
= NULL
;
1684 new_wa
->free
= true;
1686 area
->next
= new_wa
;
1689 /* If backup memory was allocated to this area, it has the wrong size
1690 * now so free it and it will be reallocated if/when needed */
1693 area
->backup
= NULL
;
1698 /* Merge all adjacent free areas into one */
1699 static void target_merge_working_areas(struct target
*target
)
1701 struct working_area
*c
= target
->working_areas
;
1703 while (c
&& c
->next
) {
1704 assert(c
->next
->address
== c
->address
+ c
->size
); /* This is an invariant */
1706 /* Find two adjacent free areas */
1707 if (c
->free
&& c
->next
->free
) {
1708 /* Merge the last into the first */
1709 c
->size
+= c
->next
->size
;
1711 /* Remove the last */
1712 struct working_area
*to_be_freed
= c
->next
;
1713 c
->next
= c
->next
->next
;
1714 if (to_be_freed
->backup
)
1715 free(to_be_freed
->backup
);
1718 /* If backup memory was allocated to the remaining area, it's has
1719 * the wrong size now */
1730 int target_alloc_working_area_try(struct target
*target
, uint32_t size
, struct working_area
**area
)
1732 /* Reevaluate working area address based on MMU state*/
1733 if (target
->working_areas
== NULL
) {
1737 retval
= target
->type
->mmu(target
, &enabled
);
1738 if (retval
!= ERROR_OK
)
1742 if (target
->working_area_phys_spec
) {
1743 LOG_DEBUG("MMU disabled, using physical "
1744 "address for working memory " TARGET_ADDR_FMT
,
1745 target
->working_area_phys
);
1746 target
->working_area
= target
->working_area_phys
;
1748 LOG_ERROR("No working memory available. "
1749 "Specify -work-area-phys to target.");
1750 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
1753 if (target
->working_area_virt_spec
) {
1754 LOG_DEBUG("MMU enabled, using virtual "
1755 "address for working memory " TARGET_ADDR_FMT
,
1756 target
->working_area_virt
);
1757 target
->working_area
= target
->working_area_virt
;
1759 LOG_ERROR("No working memory available. "
1760 "Specify -work-area-virt to target.");
1761 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
1765 /* Set up initial working area on first call */
1766 struct working_area
*new_wa
= malloc(sizeof(*new_wa
));
1768 new_wa
->next
= NULL
;
1769 new_wa
->size
= target
->working_area_size
& ~3UL; /* 4-byte align */
1770 new_wa
->address
= target
->working_area
;
1771 new_wa
->backup
= NULL
;
1772 new_wa
->user
= NULL
;
1773 new_wa
->free
= true;
1776 target
->working_areas
= new_wa
;
1779 /* only allocate multiples of 4 byte */
1781 size
= (size
+ 3) & (~3UL);
1783 struct working_area
*c
= target
->working_areas
;
1785 /* Find the first large enough working area */
1787 if (c
->free
&& c
->size
>= size
)
1793 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
1795 /* Split the working area into the requested size */
1796 target_split_working_area(c
, size
);
1798 LOG_DEBUG("allocated new working area of %" PRIu32
" bytes at address " TARGET_ADDR_FMT
,
1801 if (target
->backup_working_area
) {
1802 if (c
->backup
== NULL
) {
1803 c
->backup
= malloc(c
->size
);
1804 if (c
->backup
== NULL
)
1808 int retval
= target_read_memory(target
, c
->address
, 4, c
->size
/ 4, c
->backup
);
1809 if (retval
!= ERROR_OK
)
1813 /* mark as used, and return the new (reused) area */
1820 print_wa_layout(target
);
1825 int target_alloc_working_area(struct target
*target
, uint32_t size
, struct working_area
**area
)
1829 retval
= target_alloc_working_area_try(target
, size
, area
);
1830 if (retval
== ERROR_TARGET_RESOURCE_NOT_AVAILABLE
)
1831 LOG_WARNING("not enough working area available(requested %"PRIu32
")", size
);
1836 static int target_restore_working_area(struct target
*target
, struct working_area
*area
)
1838 int retval
= ERROR_OK
;
1840 if (target
->backup_working_area
&& area
->backup
!= NULL
) {
1841 retval
= target_write_memory(target
, area
->address
, 4, area
->size
/ 4, area
->backup
);
1842 if (retval
!= ERROR_OK
)
1843 LOG_ERROR("failed to restore %" PRIu32
" bytes of working area at address " TARGET_ADDR_FMT
,
1844 area
->size
, area
->address
);
1850 /* Restore the area's backup memory, if any, and return the area to the allocation pool */
1851 static int target_free_working_area_restore(struct target
*target
, struct working_area
*area
, int restore
)
1853 int retval
= ERROR_OK
;
1859 retval
= target_restore_working_area(target
, area
);
1860 /* REVISIT: Perhaps the area should be freed even if restoring fails. */
1861 if (retval
!= ERROR_OK
)
1867 LOG_DEBUG("freed %" PRIu32
" bytes of working area at address " TARGET_ADDR_FMT
,
1868 area
->size
, area
->address
);
1870 /* mark user pointer invalid */
1871 /* TODO: Is this really safe? It points to some previous caller's memory.
1872 * How could we know that the area pointer is still in that place and not
1873 * some other vital data? What's the purpose of this, anyway? */
1877 target_merge_working_areas(target
);
1879 print_wa_layout(target
);
1884 int target_free_working_area(struct target
*target
, struct working_area
*area
)
1886 return target_free_working_area_restore(target
, area
, 1);
1889 static void target_destroy(struct target
*target
)
1891 if (target
->type
->deinit_target
)
1892 target
->type
->deinit_target(target
);
1895 free(target
->trace_info
);
1896 free(target
->cmd_name
);
1900 void target_quit(void)
1902 struct target_event_callback
*pe
= target_event_callbacks
;
1904 struct target_event_callback
*t
= pe
->next
;
1908 target_event_callbacks
= NULL
;
1910 struct target_timer_callback
*pt
= target_timer_callbacks
;
1912 struct target_timer_callback
*t
= pt
->next
;
1916 target_timer_callbacks
= NULL
;
1918 for (struct target
*target
= all_targets
; target
;) {
1922 target_destroy(target
);
1929 /* free resources and restore memory, if restoring memory fails,
1930 * free up resources anyway
1932 static void target_free_all_working_areas_restore(struct target
*target
, int restore
)
1934 struct working_area
*c
= target
->working_areas
;
1936 LOG_DEBUG("freeing all working areas");
1938 /* Loop through all areas, restoring the allocated ones and marking them as free */
1942 target_restore_working_area(target
, c
);
1944 *c
->user
= NULL
; /* Same as above */
1950 /* Run a merge pass to combine all areas into one */
1951 target_merge_working_areas(target
);
1953 print_wa_layout(target
);
1956 void target_free_all_working_areas(struct target
*target
)
1958 target_free_all_working_areas_restore(target
, 1);
1961 /* Find the largest number of bytes that can be allocated */
1962 uint32_t target_get_working_area_avail(struct target
*target
)
1964 struct working_area
*c
= target
->working_areas
;
1965 uint32_t max_size
= 0;
1968 return target
->working_area_size
;
1971 if (c
->free
&& max_size
< c
->size
)
1980 int target_arch_state(struct target
*target
)
1983 if (target
== NULL
) {
1984 LOG_WARNING("No target has been configured");
1988 if (target
->state
!= TARGET_HALTED
)
1991 retval
= target
->type
->arch_state(target
);
1995 static int target_get_gdb_fileio_info_default(struct target
*target
,
1996 struct gdb_fileio_info
*fileio_info
)
1998 /* If target does not support semi-hosting function, target
1999 has no need to provide .get_gdb_fileio_info callback.
2000 It just return ERROR_FAIL and gdb_server will return "Txx"
2001 as target halted every time. */
2005 static int target_gdb_fileio_end_default(struct target
*target
,
2006 int retcode
, int fileio_errno
, bool ctrl_c
)
2011 static int target_profiling_default(struct target
*target
, uint32_t *samples
,
2012 uint32_t max_num_samples
, uint32_t *num_samples
, uint32_t seconds
)
2014 struct timeval timeout
, now
;
2016 gettimeofday(&timeout
, NULL
);
2017 timeval_add_time(&timeout
, seconds
, 0);
2019 LOG_INFO("Starting profiling. Halting and resuming the"
2020 " target as often as we can...");
2022 uint32_t sample_count
= 0;
2023 /* hopefully it is safe to cache! We want to stop/restart as quickly as possible. */
2024 struct reg
*reg
= register_get_by_name(target
->reg_cache
, "pc", 1);
2026 int retval
= ERROR_OK
;
2028 target_poll(target
);
2029 if (target
->state
== TARGET_HALTED
) {
2030 uint32_t t
= buf_get_u32(reg
->value
, 0, 32);
2031 samples
[sample_count
++] = t
;
2032 /* current pc, addr = 0, do not handle breakpoints, not debugging */
2033 retval
= target_resume(target
, 1, 0, 0, 0);
2034 target_poll(target
);
2035 alive_sleep(10); /* sleep 10ms, i.e. <100 samples/second. */
2036 } else if (target
->state
== TARGET_RUNNING
) {
2037 /* We want to quickly sample the PC. */
2038 retval
= target_halt(target
);
2040 LOG_INFO("Target not halted or running");
2045 if (retval
!= ERROR_OK
)
2048 gettimeofday(&now
, NULL
);
2049 if ((sample_count
>= max_num_samples
) || timeval_compare(&now
, &timeout
) >= 0) {
2050 LOG_INFO("Profiling completed. %" PRIu32
" samples.", sample_count
);
2055 *num_samples
= sample_count
;
2059 /* Single aligned words are guaranteed to use 16 or 32 bit access
2060 * mode respectively, otherwise data is handled as quickly as
2063 int target_write_buffer(struct target
*target
, target_addr_t address
, uint32_t size
, const uint8_t *buffer
)
2065 LOG_DEBUG("writing buffer of %" PRIi32
" byte at " TARGET_ADDR_FMT
,
2068 if (!target_was_examined(target
)) {
2069 LOG_ERROR("Target not examined yet");
2076 if ((address
+ size
- 1) < address
) {
2077 /* GDB can request this when e.g. PC is 0xfffffffc */
2078 LOG_ERROR("address + size wrapped (" TARGET_ADDR_FMT
", 0x%08" PRIx32
")",
2084 return target
->type
->write_buffer(target
, address
, size
, buffer
);
2087 static int target_write_buffer_default(struct target
*target
,
2088 target_addr_t address
, uint32_t count
, const uint8_t *buffer
)
2092 /* Align up to maximum 4 bytes. The loop condition makes sure the next pass
2093 * will have something to do with the size we leave to it. */
2094 for (size
= 1; size
< 4 && count
>= size
* 2 + (address
& size
); size
*= 2) {
2095 if (address
& size
) {
2096 int retval
= target_write_memory(target
, address
, size
, 1, buffer
);
2097 if (retval
!= ERROR_OK
)
2105 /* Write the data with as large access size as possible. */
2106 for (; size
> 0; size
/= 2) {
2107 uint32_t aligned
= count
- count
% size
;
2109 int retval
= target_write_memory(target
, address
, size
, aligned
/ size
, buffer
);
2110 if (retval
!= ERROR_OK
)
2121 /* Single aligned words are guaranteed to use 16 or 32 bit access
2122 * mode respectively, otherwise data is handled as quickly as
2125 int target_read_buffer(struct target
*target
, target_addr_t address
, uint32_t size
, uint8_t *buffer
)
2127 LOG_DEBUG("reading buffer of %" PRIi32
" byte at " TARGET_ADDR_FMT
,
2130 if (!target_was_examined(target
)) {
2131 LOG_ERROR("Target not examined yet");
2138 if ((address
+ size
- 1) < address
) {
2139 /* GDB can request this when e.g. PC is 0xfffffffc */
2140 LOG_ERROR("address + size wrapped (" TARGET_ADDR_FMT
", 0x%08" PRIx32
")",
2146 return target
->type
->read_buffer(target
, address
, size
, buffer
);
2149 static int target_read_buffer_default(struct target
*target
, target_addr_t address
, uint32_t count
, uint8_t *buffer
)
2153 /* Align up to maximum 4 bytes. The loop condition makes sure the next pass
2154 * will have something to do with the size we leave to it. */
2155 for (size
= 1; size
< 4 && count
>= size
* 2 + (address
& size
); size
*= 2) {
2156 if (address
& size
) {
2157 int retval
= target_read_memory(target
, address
, size
, 1, buffer
);
2158 if (retval
!= ERROR_OK
)
2166 /* Read the data with as large access size as possible. */
2167 for (; size
> 0; size
/= 2) {
2168 uint32_t aligned
= count
- count
% size
;
2170 int retval
= target_read_memory(target
, address
, size
, aligned
/ size
, buffer
);
2171 if (retval
!= ERROR_OK
)
2182 int target_checksum_memory(struct target
*target
, target_addr_t address
, uint32_t size
, uint32_t* crc
)
2187 uint32_t checksum
= 0;
2188 if (!target_was_examined(target
)) {
2189 LOG_ERROR("Target not examined yet");
2193 retval
= target
->type
->checksum_memory(target
, address
, size
, &checksum
);
2194 if (retval
!= ERROR_OK
) {
2195 buffer
= malloc(size
);
2196 if (buffer
== NULL
) {
2197 LOG_ERROR("error allocating buffer for section (%" PRId32
" bytes)", size
);
2198 return ERROR_COMMAND_SYNTAX_ERROR
;
2200 retval
= target_read_buffer(target
, address
, size
, buffer
);
2201 if (retval
!= ERROR_OK
) {
2206 /* convert to target endianness */
2207 for (i
= 0; i
< (size
/sizeof(uint32_t)); i
++) {
2208 uint32_t target_data
;
2209 target_data
= target_buffer_get_u32(target
, &buffer
[i
*sizeof(uint32_t)]);
2210 target_buffer_set_u32(target
, &buffer
[i
*sizeof(uint32_t)], target_data
);
2213 retval
= image_calculate_checksum(buffer
, size
, &checksum
);
2222 int target_blank_check_memory(struct target
*target
, target_addr_t address
, uint32_t size
, uint32_t* blank
,
2223 uint8_t erased_value
)
2226 if (!target_was_examined(target
)) {
2227 LOG_ERROR("Target not examined yet");
2231 if (target
->type
->blank_check_memory
== 0)
2232 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
2234 retval
= target
->type
->blank_check_memory(target
, address
, size
, blank
, erased_value
);
2239 int target_read_u64(struct target
*target
, target_addr_t address
, uint64_t *value
)
2241 uint8_t value_buf
[8];
2242 if (!target_was_examined(target
)) {
2243 LOG_ERROR("Target not examined yet");
2247 int retval
= target_read_memory(target
, address
, 8, 1, value_buf
);
2249 if (retval
== ERROR_OK
) {
2250 *value
= target_buffer_get_u64(target
, value_buf
);
2251 LOG_DEBUG("address: " TARGET_ADDR_FMT
", value: 0x%16.16" PRIx64
"",
2256 LOG_DEBUG("address: " TARGET_ADDR_FMT
" failed",
2263 int target_read_u32(struct target
*target
, target_addr_t address
, uint32_t *value
)
2265 uint8_t value_buf
[4];
2266 if (!target_was_examined(target
)) {
2267 LOG_ERROR("Target not examined yet");
2271 int retval
= target_read_memory(target
, address
, 4, 1, value_buf
);
2273 if (retval
== ERROR_OK
) {
2274 *value
= target_buffer_get_u32(target
, value_buf
);
2275 LOG_DEBUG("address: " TARGET_ADDR_FMT
", value: 0x%8.8" PRIx32
"",
2280 LOG_DEBUG("address: " TARGET_ADDR_FMT
" failed",
2287 int target_read_u16(struct target
*target
, target_addr_t address
, uint16_t *value
)
2289 uint8_t value_buf
[2];
2290 if (!target_was_examined(target
)) {
2291 LOG_ERROR("Target not examined yet");
2295 int retval
= target_read_memory(target
, address
, 2, 1, value_buf
);
2297 if (retval
== ERROR_OK
) {
2298 *value
= target_buffer_get_u16(target
, value_buf
);
2299 LOG_DEBUG("address: " TARGET_ADDR_FMT
", value: 0x%4.4" PRIx16
,
2304 LOG_DEBUG("address: " TARGET_ADDR_FMT
" failed",
2311 int target_read_u8(struct target
*target
, target_addr_t address
, uint8_t *value
)
2313 if (!target_was_examined(target
)) {
2314 LOG_ERROR("Target not examined yet");
2318 int retval
= target_read_memory(target
, address
, 1, 1, value
);
2320 if (retval
== ERROR_OK
) {
2321 LOG_DEBUG("address: " TARGET_ADDR_FMT
", value: 0x%2.2" PRIx8
,
2326 LOG_DEBUG("address: " TARGET_ADDR_FMT
" failed",
2333 int target_write_u64(struct target
*target
, target_addr_t address
, uint64_t value
)
2336 uint8_t value_buf
[8];
2337 if (!target_was_examined(target
)) {
2338 LOG_ERROR("Target not examined yet");
2342 LOG_DEBUG("address: " TARGET_ADDR_FMT
", value: 0x%16.16" PRIx64
"",
2346 target_buffer_set_u64(target
, value_buf
, value
);
2347 retval
= target_write_memory(target
, address
, 8, 1, value_buf
);
2348 if (retval
!= ERROR_OK
)
2349 LOG_DEBUG("failed: %i", retval
);
2354 int target_write_u32(struct target
*target
, target_addr_t address
, uint32_t value
)
2357 uint8_t value_buf
[4];
2358 if (!target_was_examined(target
)) {
2359 LOG_ERROR("Target not examined yet");
2363 LOG_DEBUG("address: " TARGET_ADDR_FMT
", value: 0x%8.8" PRIx32
"",
2367 target_buffer_set_u32(target
, value_buf
, value
);
2368 retval
= target_write_memory(target
, address
, 4, 1, value_buf
);
2369 if (retval
!= ERROR_OK
)
2370 LOG_DEBUG("failed: %i", retval
);
2375 int target_write_u16(struct target
*target
, target_addr_t address
, uint16_t value
)
2378 uint8_t value_buf
[2];
2379 if (!target_was_examined(target
)) {
2380 LOG_ERROR("Target not examined yet");
2384 LOG_DEBUG("address: " TARGET_ADDR_FMT
", value: 0x%8.8" PRIx16
,
2388 target_buffer_set_u16(target
, value_buf
, value
);
2389 retval
= target_write_memory(target
, address
, 2, 1, value_buf
);
2390 if (retval
!= ERROR_OK
)
2391 LOG_DEBUG("failed: %i", retval
);
2396 int target_write_u8(struct target
*target
, target_addr_t address
, uint8_t value
)
2399 if (!target_was_examined(target
)) {
2400 LOG_ERROR("Target not examined yet");
2404 LOG_DEBUG("address: " TARGET_ADDR_FMT
", value: 0x%2.2" PRIx8
,
2407 retval
= target_write_memory(target
, address
, 1, 1, &value
);
2408 if (retval
!= ERROR_OK
)
2409 LOG_DEBUG("failed: %i", retval
);
2414 int target_write_phys_u64(struct target
*target
, target_addr_t address
, uint64_t value
)
2417 uint8_t value_buf
[8];
2418 if (!target_was_examined(target
)) {
2419 LOG_ERROR("Target not examined yet");
2423 LOG_DEBUG("address: " TARGET_ADDR_FMT
", value: 0x%16.16" PRIx64
"",
2427 target_buffer_set_u64(target
, value_buf
, value
);
2428 retval
= target_write_phys_memory(target
, address
, 8, 1, value_buf
);
2429 if (retval
!= ERROR_OK
)
2430 LOG_DEBUG("failed: %i", retval
);
2435 int target_write_phys_u32(struct target
*target
, target_addr_t address
, uint32_t value
)
2438 uint8_t value_buf
[4];
2439 if (!target_was_examined(target
)) {
2440 LOG_ERROR("Target not examined yet");
2444 LOG_DEBUG("address: " TARGET_ADDR_FMT
", value: 0x%8.8" PRIx32
"",
2448 target_buffer_set_u32(target
, value_buf
, value
);
2449 retval
= target_write_phys_memory(target
, address
, 4, 1, value_buf
);
2450 if (retval
!= ERROR_OK
)
2451 LOG_DEBUG("failed: %i", retval
);
2456 int target_write_phys_u16(struct target
*target
, target_addr_t address
, uint16_t value
)
2459 uint8_t value_buf
[2];
2460 if (!target_was_examined(target
)) {
2461 LOG_ERROR("Target not examined yet");
2465 LOG_DEBUG("address: " TARGET_ADDR_FMT
", value: 0x%8.8" PRIx16
,
2469 target_buffer_set_u16(target
, value_buf
, value
);
2470 retval
= target_write_phys_memory(target
, address
, 2, 1, value_buf
);
2471 if (retval
!= ERROR_OK
)
2472 LOG_DEBUG("failed: %i", retval
);
2477 int target_write_phys_u8(struct target
*target
, target_addr_t address
, uint8_t value
)
2480 if (!target_was_examined(target
)) {
2481 LOG_ERROR("Target not examined yet");
2485 LOG_DEBUG("address: " TARGET_ADDR_FMT
", value: 0x%2.2" PRIx8
,
2488 retval
= target_write_phys_memory(target
, address
, 1, 1, &value
);
2489 if (retval
!= ERROR_OK
)
2490 LOG_DEBUG("failed: %i", retval
);
2495 static int find_target(struct command_context
*cmd_ctx
, const char *name
)
2497 struct target
*target
= get_target(name
);
2498 if (target
== NULL
) {
2499 LOG_ERROR("Target: %s is unknown, try one of:\n", name
);
2502 if (!target
->tap
->enabled
) {
2503 LOG_USER("Target: TAP %s is disabled, "
2504 "can't be the current target\n",
2505 target
->tap
->dotted_name
);
2509 cmd_ctx
->current_target
= target
->target_number
;
2514 COMMAND_HANDLER(handle_targets_command
)
2516 int retval
= ERROR_OK
;
2517 if (CMD_ARGC
== 1) {
2518 retval
= find_target(CMD_CTX
, CMD_ARGV
[0]);
2519 if (retval
== ERROR_OK
) {
2525 struct target
*target
= all_targets
;
2526 command_print(CMD_CTX
, " TargetName Type Endian TapName State ");
2527 command_print(CMD_CTX
, "-- ------------------ ---------- ------ ------------------ ------------");
2532 if (target
->tap
->enabled
)
2533 state
= target_state_name(target
);
2535 state
= "tap-disabled";
2537 if (CMD_CTX
->current_target
== target
->target_number
)
2540 /* keep columns lined up to match the headers above */
2541 command_print(CMD_CTX
,
2542 "%2d%c %-18s %-10s %-6s %-18s %s",
2543 target
->target_number
,
2545 target_name(target
),
2546 target_type_name(target
),
2547 Jim_Nvp_value2name_simple(nvp_target_endian
,
2548 target
->endianness
)->name
,
2549 target
->tap
->dotted_name
,
2551 target
= target
->next
;
2557 /* every 300ms we check for reset & powerdropout and issue a "reset halt" if so. */
2559 static int powerDropout
;
2560 static int srstAsserted
;
2562 static int runPowerRestore
;
2563 static int runPowerDropout
;
2564 static int runSrstAsserted
;
2565 static int runSrstDeasserted
;
2567 static int sense_handler(void)
2569 static int prevSrstAsserted
;
2570 static int prevPowerdropout
;
2572 int retval
= jtag_power_dropout(&powerDropout
);
2573 if (retval
!= ERROR_OK
)
2577 powerRestored
= prevPowerdropout
&& !powerDropout
;
2579 runPowerRestore
= 1;
2581 int64_t current
= timeval_ms();
2582 static int64_t lastPower
;
2583 bool waitMore
= lastPower
+ 2000 > current
;
2584 if (powerDropout
&& !waitMore
) {
2585 runPowerDropout
= 1;
2586 lastPower
= current
;
2589 retval
= jtag_srst_asserted(&srstAsserted
);
2590 if (retval
!= ERROR_OK
)
2594 srstDeasserted
= prevSrstAsserted
&& !srstAsserted
;
2596 static int64_t lastSrst
;
2597 waitMore
= lastSrst
+ 2000 > current
;
2598 if (srstDeasserted
&& !waitMore
) {
2599 runSrstDeasserted
= 1;
2603 if (!prevSrstAsserted
&& srstAsserted
)
2604 runSrstAsserted
= 1;
2606 prevSrstAsserted
= srstAsserted
;
2607 prevPowerdropout
= powerDropout
;
2609 if (srstDeasserted
|| powerRestored
) {
2610 /* Other than logging the event we can't do anything here.
2611 * Issuing a reset is a particularly bad idea as we might
2612 * be inside a reset already.
2619 /* process target state changes */
2620 static int handle_target(void *priv
)
2622 Jim_Interp
*interp
= (Jim_Interp
*)priv
;
2623 int retval
= ERROR_OK
;
2625 if (!is_jtag_poll_safe()) {
2626 /* polling is disabled currently */
2630 /* we do not want to recurse here... */
2631 static int recursive
;
2635 /* danger! running these procedures can trigger srst assertions and power dropouts.
2636 * We need to avoid an infinite loop/recursion here and we do that by
2637 * clearing the flags after running these events.
2639 int did_something
= 0;
2640 if (runSrstAsserted
) {
2641 LOG_INFO("srst asserted detected, running srst_asserted proc.");
2642 Jim_Eval(interp
, "srst_asserted");
2645 if (runSrstDeasserted
) {
2646 Jim_Eval(interp
, "srst_deasserted");
2649 if (runPowerDropout
) {
2650 LOG_INFO("Power dropout detected, running power_dropout proc.");
2651 Jim_Eval(interp
, "power_dropout");
2654 if (runPowerRestore
) {
2655 Jim_Eval(interp
, "power_restore");
2659 if (did_something
) {
2660 /* clear detect flags */
2664 /* clear action flags */
2666 runSrstAsserted
= 0;
2667 runSrstDeasserted
= 0;
2668 runPowerRestore
= 0;
2669 runPowerDropout
= 0;
2674 /* Poll targets for state changes unless that's globally disabled.
2675 * Skip targets that are currently disabled.
2677 for (struct target
*target
= all_targets
;
2678 is_jtag_poll_safe() && target
;
2679 target
= target
->next
) {
2681 if (!target_was_examined(target
))
2684 if (!target
->tap
->enabled
)
2687 if (target
->backoff
.times
> target
->backoff
.count
) {
2688 /* do not poll this time as we failed previously */
2689 target
->backoff
.count
++;
2692 target
->backoff
.count
= 0;
2694 /* only poll target if we've got power and srst isn't asserted */
2695 if (!powerDropout
&& !srstAsserted
) {
2696 /* polling may fail silently until the target has been examined */
2697 retval
= target_poll(target
);
2698 if (retval
!= ERROR_OK
) {
2699 /* 100ms polling interval. Increase interval between polling up to 5000ms */
2700 if (target
->backoff
.times
* polling_interval
< 5000) {
2701 target
->backoff
.times
*= 2;
2702 target
->backoff
.times
++;
2705 /* Tell GDB to halt the debugger. This allows the user to
2706 * run monitor commands to handle the situation.
2708 target_call_event_callbacks(target
, TARGET_EVENT_GDB_HALT
);
2710 if (target
->backoff
.times
> 0) {
2711 LOG_USER("Polling target %s failed, trying to reexamine", target_name(target
));
2712 target_reset_examined(target
);
2713 retval
= target_examine_one(target
);
2714 /* Target examination could have failed due to unstable connection,
2715 * but we set the examined flag anyway to repoll it later */
2716 if (retval
!= ERROR_OK
) {
2717 target
->examined
= true;
2718 LOG_USER("Examination failed, GDB will be halted. Polling again in %dms",
2719 target
->backoff
.times
* polling_interval
);
2724 /* Since we succeeded, we reset backoff count */
2725 target
->backoff
.times
= 0;
2732 COMMAND_HANDLER(handle_reg_command
)
2734 struct target
*target
;
2735 struct reg
*reg
= NULL
;
2741 target
= get_current_target(CMD_CTX
);
2743 /* list all available registers for the current target */
2744 if (CMD_ARGC
== 0) {
2745 struct reg_cache
*cache
= target
->reg_cache
;
2751 command_print(CMD_CTX
, "===== %s", cache
->name
);
2753 for (i
= 0, reg
= cache
->reg_list
;
2754 i
< cache
->num_regs
;
2755 i
++, reg
++, count
++) {
2756 /* only print cached values if they are valid */
2758 value
= buf_to_str(reg
->value
,
2760 command_print(CMD_CTX
,
2761 "(%i) %s (/%" PRIu32
"): 0x%s%s",
2769 command_print(CMD_CTX
, "(%i) %s (/%" PRIu32
")",
2774 cache
= cache
->next
;
2780 /* access a single register by its ordinal number */
2781 if ((CMD_ARGV
[0][0] >= '0') && (CMD_ARGV
[0][0] <= '9')) {
2783 COMMAND_PARSE_NUMBER(uint
, CMD_ARGV
[0], num
);
2785 struct reg_cache
*cache
= target
->reg_cache
;
2789 for (i
= 0; i
< cache
->num_regs
; i
++) {
2790 if (count
++ == num
) {
2791 reg
= &cache
->reg_list
[i
];
2797 cache
= cache
->next
;
2801 command_print(CMD_CTX
, "%i is out of bounds, the current target "
2802 "has only %i registers (0 - %i)", num
, count
, count
- 1);
2806 /* access a single register by its name */
2807 reg
= register_get_by_name(target
->reg_cache
, CMD_ARGV
[0], 1);
2810 command_print(CMD_CTX
, "register %s not found in current target", CMD_ARGV
[0]);
2815 assert(reg
!= NULL
); /* give clang a hint that we *know* reg is != NULL here */
2817 /* display a register */
2818 if ((CMD_ARGC
== 1) || ((CMD_ARGC
== 2) && !((CMD_ARGV
[1][0] >= '0')
2819 && (CMD_ARGV
[1][0] <= '9')))) {
2820 if ((CMD_ARGC
== 2) && (strcmp(CMD_ARGV
[1], "force") == 0))
2823 if (reg
->valid
== 0)
2824 reg
->type
->get(reg
);
2825 value
= buf_to_str(reg
->value
, reg
->size
, 16);
2826 command_print(CMD_CTX
, "%s (/%i): 0x%s", reg
->name
, (int)(reg
->size
), value
);
2831 /* set register value */
2832 if (CMD_ARGC
== 2) {
2833 uint8_t *buf
= malloc(DIV_ROUND_UP(reg
->size
, 8));
2836 str_to_buf(CMD_ARGV
[1], strlen(CMD_ARGV
[1]), buf
, reg
->size
, 0);
2838 reg
->type
->set(reg
, buf
);
2840 value
= buf_to_str(reg
->value
, reg
->size
, 16);
2841 command_print(CMD_CTX
, "%s (/%i): 0x%s", reg
->name
, (int)(reg
->size
), value
);
2849 return ERROR_COMMAND_SYNTAX_ERROR
;
2852 COMMAND_HANDLER(handle_poll_command
)
2854 int retval
= ERROR_OK
;
2855 struct target
*target
= get_current_target(CMD_CTX
);
2857 if (CMD_ARGC
== 0) {
2858 command_print(CMD_CTX
, "background polling: %s",
2859 jtag_poll_get_enabled() ? "on" : "off");
2860 command_print(CMD_CTX
, "TAP: %s (%s)",
2861 target
->tap
->dotted_name
,
2862 target
->tap
->enabled
? "enabled" : "disabled");
2863 if (!target
->tap
->enabled
)
2865 retval
= target_poll(target
);
2866 if (retval
!= ERROR_OK
)
2868 retval
= target_arch_state(target
);
2869 if (retval
!= ERROR_OK
)
2871 } else if (CMD_ARGC
== 1) {
2873 COMMAND_PARSE_ON_OFF(CMD_ARGV
[0], enable
);
2874 jtag_poll_set_enabled(enable
);
2876 return ERROR_COMMAND_SYNTAX_ERROR
;
2881 COMMAND_HANDLER(handle_wait_halt_command
)
2884 return ERROR_COMMAND_SYNTAX_ERROR
;
2886 unsigned ms
= DEFAULT_HALT_TIMEOUT
;
2887 if (1 == CMD_ARGC
) {
2888 int retval
= parse_uint(CMD_ARGV
[0], &ms
);
2889 if (ERROR_OK
!= retval
)
2890 return ERROR_COMMAND_SYNTAX_ERROR
;
2893 struct target
*target
= get_current_target(CMD_CTX
);
2894 return target_wait_state(target
, TARGET_HALTED
, ms
);
2897 /* wait for target state to change. The trick here is to have a low
2898 * latency for short waits and not to suck up all the CPU time
2901 * After 500ms, keep_alive() is invoked
2903 int target_wait_state(struct target
*target
, enum target_state state
, int ms
)
2906 int64_t then
= 0, cur
;
2910 retval
= target_poll(target
);
2911 if (retval
!= ERROR_OK
)
2913 if (target
->state
== state
)
2918 then
= timeval_ms();
2919 LOG_DEBUG("waiting for target %s...",
2920 Jim_Nvp_value2name_simple(nvp_target_state
, state
)->name
);
2926 if ((cur
-then
) > ms
) {
2927 LOG_ERROR("timed out while waiting for target %s",
2928 Jim_Nvp_value2name_simple(nvp_target_state
, state
)->name
);
2936 COMMAND_HANDLER(handle_halt_command
)
2940 struct target
*target
= get_current_target(CMD_CTX
);
2941 int retval
= target_halt(target
);
2942 if (ERROR_OK
!= retval
)
2945 if (CMD_ARGC
== 1) {
2946 unsigned wait_local
;
2947 retval
= parse_uint(CMD_ARGV
[0], &wait_local
);
2948 if (ERROR_OK
!= retval
)
2949 return ERROR_COMMAND_SYNTAX_ERROR
;
2954 return CALL_COMMAND_HANDLER(handle_wait_halt_command
);
2957 COMMAND_HANDLER(handle_soft_reset_halt_command
)
2959 struct target
*target
= get_current_target(CMD_CTX
);
2961 LOG_USER("requesting target halt and executing a soft reset");
2963 target_soft_reset_halt(target
);
2968 COMMAND_HANDLER(handle_reset_command
)
2971 return ERROR_COMMAND_SYNTAX_ERROR
;
2973 enum target_reset_mode reset_mode
= RESET_RUN
;
2974 if (CMD_ARGC
== 1) {
2976 n
= Jim_Nvp_name2value_simple(nvp_reset_modes
, CMD_ARGV
[0]);
2977 if ((n
->name
== NULL
) || (n
->value
== RESET_UNKNOWN
))
2978 return ERROR_COMMAND_SYNTAX_ERROR
;
2979 reset_mode
= n
->value
;
2982 /* reset *all* targets */
2983 return target_process_reset(CMD_CTX
, reset_mode
);
2987 COMMAND_HANDLER(handle_resume_command
)
2991 return ERROR_COMMAND_SYNTAX_ERROR
;
2993 struct target
*target
= get_current_target(CMD_CTX
);
2995 /* with no CMD_ARGV, resume from current pc, addr = 0,
2996 * with one arguments, addr = CMD_ARGV[0],
2997 * handle breakpoints, not debugging */
2998 target_addr_t addr
= 0;
2999 if (CMD_ARGC
== 1) {
3000 COMMAND_PARSE_ADDRESS(CMD_ARGV
[0], addr
);
3004 return target_resume(target
, current
, addr
, 1, 0);
3007 COMMAND_HANDLER(handle_step_command
)
3010 return ERROR_COMMAND_SYNTAX_ERROR
;
3014 /* with no CMD_ARGV, step from current pc, addr = 0,
3015 * with one argument addr = CMD_ARGV[0],
3016 * handle breakpoints, debugging */
3017 target_addr_t addr
= 0;
3019 if (CMD_ARGC
== 1) {
3020 COMMAND_PARSE_ADDRESS(CMD_ARGV
[0], addr
);
3024 struct target
*target
= get_current_target(CMD_CTX
);
3026 return target
->type
->step(target
, current_pc
, addr
, 1);
3029 static void handle_md_output(struct command_context
*cmd_ctx
,
3030 struct target
*target
, target_addr_t address
, unsigned size
,
3031 unsigned count
, const uint8_t *buffer
)
3033 const unsigned line_bytecnt
= 32;
3034 unsigned line_modulo
= line_bytecnt
/ size
;
3036 char output
[line_bytecnt
* 4 + 1];
3037 unsigned output_len
= 0;
3039 const char *value_fmt
;
3042 value_fmt
= "%16.16"PRIx64
" ";
3045 value_fmt
= "%8.8"PRIx64
" ";
3048 value_fmt
= "%4.4"PRIx64
" ";
3051 value_fmt
= "%2.2"PRIx64
" ";
3054 /* "can't happen", caller checked */
3055 LOG_ERROR("invalid memory read size: %u", size
);
3059 for (unsigned i
= 0; i
< count
; i
++) {
3060 if (i
% line_modulo
== 0) {
3061 output_len
+= snprintf(output
+ output_len
,
3062 sizeof(output
) - output_len
,
3063 TARGET_ADDR_FMT
": ",
3064 (address
+ (i
* size
)));
3068 const uint8_t *value_ptr
= buffer
+ i
* size
;
3071 value
= target_buffer_get_u64(target
, value_ptr
);
3074 value
= target_buffer_get_u32(target
, value_ptr
);
3077 value
= target_buffer_get_u16(target
, value_ptr
);
3082 output_len
+= snprintf(output
+ output_len
,
3083 sizeof(output
) - output_len
,
3086 if ((i
% line_modulo
== line_modulo
- 1) || (i
== count
- 1)) {
3087 command_print(cmd_ctx
, "%s", output
);
3093 COMMAND_HANDLER(handle_md_command
)
3096 return ERROR_COMMAND_SYNTAX_ERROR
;
3099 switch (CMD_NAME
[2]) {
3113 return ERROR_COMMAND_SYNTAX_ERROR
;
3116 bool physical
= strcmp(CMD_ARGV
[0], "phys") == 0;
3117 int (*fn
)(struct target
*target
,
3118 target_addr_t address
, uint32_t size_value
, uint32_t count
, uint8_t *buffer
);
3122 fn
= target_read_phys_memory
;
3124 fn
= target_read_memory
;
3125 if ((CMD_ARGC
< 1) || (CMD_ARGC
> 2))
3126 return ERROR_COMMAND_SYNTAX_ERROR
;
3128 target_addr_t address
;
3129 COMMAND_PARSE_ADDRESS(CMD_ARGV
[0], address
);
3133 COMMAND_PARSE_NUMBER(uint
, CMD_ARGV
[1], count
);
3135 uint8_t *buffer
= calloc(count
, size
);
3136 if (buffer
== NULL
) {
3137 LOG_ERROR("Failed to allocate md read buffer");
3141 struct target
*target
= get_current_target(CMD_CTX
);
3142 int retval
= fn(target
, address
, size
, count
, buffer
);
3143 if (ERROR_OK
== retval
)
3144 handle_md_output(CMD_CTX
, target
, address
, size
, count
, buffer
);
3151 typedef int (*target_write_fn
)(struct target
*target
,
3152 target_addr_t address
, uint32_t size
, uint32_t count
, const uint8_t *buffer
);
3154 static int target_fill_mem(struct target
*target
,
3155 target_addr_t address
,
3163 /* We have to write in reasonably large chunks to be able
3164 * to fill large memory areas with any sane speed */
3165 const unsigned chunk_size
= 16384;
3166 uint8_t *target_buf
= malloc(chunk_size
* data_size
);
3167 if (target_buf
== NULL
) {
3168 LOG_ERROR("Out of memory");
3172 for (unsigned i
= 0; i
< chunk_size
; i
++) {
3173 switch (data_size
) {
3175 target_buffer_set_u64(target
, target_buf
+ i
* data_size
, b
);
3178 target_buffer_set_u32(target
, target_buf
+ i
* data_size
, b
);
3181 target_buffer_set_u16(target
, target_buf
+ i
* data_size
, b
);
3184 target_buffer_set_u8(target
, target_buf
+ i
* data_size
, b
);
3191 int retval
= ERROR_OK
;
3193 for (unsigned x
= 0; x
< c
; x
+= chunk_size
) {
3196 if (current
> chunk_size
)
3197 current
= chunk_size
;
3198 retval
= fn(target
, address
+ x
* data_size
, data_size
, current
, target_buf
);
3199 if (retval
!= ERROR_OK
)
3201 /* avoid GDB timeouts */
3210 COMMAND_HANDLER(handle_mw_command
)
3213 return ERROR_COMMAND_SYNTAX_ERROR
;
3214 bool physical
= strcmp(CMD_ARGV
[0], "phys") == 0;
3219 fn
= target_write_phys_memory
;
3221 fn
= target_write_memory
;
3222 if ((CMD_ARGC
< 2) || (CMD_ARGC
> 3))
3223 return ERROR_COMMAND_SYNTAX_ERROR
;
3225 target_addr_t address
;
3226 COMMAND_PARSE_ADDRESS(CMD_ARGV
[0], address
);
3228 target_addr_t value
;
3229 COMMAND_PARSE_ADDRESS(CMD_ARGV
[1], value
);
3233 COMMAND_PARSE_NUMBER(uint
, CMD_ARGV
[2], count
);
3235 struct target
*target
= get_current_target(CMD_CTX
);
3237 switch (CMD_NAME
[2]) {
3251 return ERROR_COMMAND_SYNTAX_ERROR
;
3254 return target_fill_mem(target
, address
, fn
, wordsize
, value
, count
);
3257 static COMMAND_HELPER(parse_load_image_command_CMD_ARGV
, struct image
*image
,
3258 target_addr_t
*min_address
, target_addr_t
*max_address
)
3260 if (CMD_ARGC
< 1 || CMD_ARGC
> 5)
3261 return ERROR_COMMAND_SYNTAX_ERROR
;
3263 /* a base address isn't always necessary,
3264 * default to 0x0 (i.e. don't relocate) */
3265 if (CMD_ARGC
>= 2) {
3267 COMMAND_PARSE_ADDRESS(CMD_ARGV
[1], addr
);
3268 image
->base_address
= addr
;
3269 image
->base_address_set
= 1;
3271 image
->base_address_set
= 0;
3273 image
->start_address_set
= 0;
3276 COMMAND_PARSE_ADDRESS(CMD_ARGV
[3], *min_address
);
3277 if (CMD_ARGC
== 5) {
3278 COMMAND_PARSE_ADDRESS(CMD_ARGV
[4], *max_address
);
3279 /* use size (given) to find max (required) */
3280 *max_address
+= *min_address
;
3283 if (*min_address
> *max_address
)
3284 return ERROR_COMMAND_SYNTAX_ERROR
;
3289 COMMAND_HANDLER(handle_load_image_command
)
3293 uint32_t image_size
;
3294 target_addr_t min_address
= 0;
3295 target_addr_t max_address
= -1;
3299 int retval
= CALL_COMMAND_HANDLER(parse_load_image_command_CMD_ARGV
,
3300 &image
, &min_address
, &max_address
);
3301 if (ERROR_OK
!= retval
)
3304 struct target
*target
= get_current_target(CMD_CTX
);
3306 struct duration bench
;
3307 duration_start(&bench
);
3309 if (image_open(&image
, CMD_ARGV
[0], (CMD_ARGC
>= 3) ? CMD_ARGV
[2] : NULL
) != ERROR_OK
)
3314 for (i
= 0; i
< image
.num_sections
; i
++) {
3315 buffer
= malloc(image
.sections
[i
].size
);
3316 if (buffer
== NULL
) {
3317 command_print(CMD_CTX
,
3318 "error allocating buffer for section (%d bytes)",
3319 (int)(image
.sections
[i
].size
));
3320 retval
= ERROR_FAIL
;
3324 retval
= image_read_section(&image
, i
, 0x0, image
.sections
[i
].size
, buffer
, &buf_cnt
);
3325 if (retval
!= ERROR_OK
) {
3330 uint32_t offset
= 0;
3331 uint32_t length
= buf_cnt
;
3333 /* DANGER!!! beware of unsigned comparision here!!! */
3335 if ((image
.sections
[i
].base_address
+ buf_cnt
>= min_address
) &&
3336 (image
.sections
[i
].base_address
< max_address
)) {
3338 if (image
.sections
[i
].base_address
< min_address
) {
3339 /* clip addresses below */
3340 offset
+= min_address
-image
.sections
[i
].base_address
;
3344 if (image
.sections
[i
].base_address
+ buf_cnt
> max_address
)
3345 length
-= (image
.sections
[i
].base_address
+ buf_cnt
)-max_address
;
3347 retval
= target_write_buffer(target
,
3348 image
.sections
[i
].base_address
+ offset
, length
, buffer
+ offset
);
3349 if (retval
!= ERROR_OK
) {
3353 image_size
+= length
;
3354 command_print(CMD_CTX
, "%u bytes written at address " TARGET_ADDR_FMT
"",
3355 (unsigned int)length
,
3356 image
.sections
[i
].base_address
+ offset
);
3362 if ((ERROR_OK
== retval
) && (duration_measure(&bench
) == ERROR_OK
)) {
3363 command_print(CMD_CTX
, "downloaded %" PRIu32
" bytes "
3364 "in %fs (%0.3f KiB/s)", image_size
,
3365 duration_elapsed(&bench
), duration_kbps(&bench
, image_size
));
3368 image_close(&image
);
3374 COMMAND_HANDLER(handle_dump_image_command
)
3376 struct fileio
*fileio
;
3378 int retval
, retvaltemp
;
3379 target_addr_t address
, size
;
3380 struct duration bench
;
3381 struct target
*target
= get_current_target(CMD_CTX
);
3384 return ERROR_COMMAND_SYNTAX_ERROR
;
3386 COMMAND_PARSE_ADDRESS(CMD_ARGV
[1], address
);
3387 COMMAND_PARSE_ADDRESS(CMD_ARGV
[2], size
);
3389 uint32_t buf_size
= (size
> 4096) ? 4096 : size
;
3390 buffer
= malloc(buf_size
);
3394 retval
= fileio_open(&fileio
, CMD_ARGV
[0], FILEIO_WRITE
, FILEIO_BINARY
);
3395 if (retval
!= ERROR_OK
) {
3400 duration_start(&bench
);
3403 size_t size_written
;
3404 uint32_t this_run_size
= (size
> buf_size
) ? buf_size
: size
;
3405 retval
= target_read_buffer(target
, address
, this_run_size
, buffer
);
3406 if (retval
!= ERROR_OK
)
3409 retval
= fileio_write(fileio
, this_run_size
, buffer
, &size_written
);
3410 if (retval
!= ERROR_OK
)
3413 size
-= this_run_size
;
3414 address
+= this_run_size
;
3419 if ((ERROR_OK
== retval
) && (duration_measure(&bench
) == ERROR_OK
)) {
3421 retval
= fileio_size(fileio
, &filesize
);
3422 if (retval
!= ERROR_OK
)
3424 command_print(CMD_CTX
,
3425 "dumped %zu bytes in %fs (%0.3f KiB/s)", filesize
,
3426 duration_elapsed(&bench
), duration_kbps(&bench
, filesize
));
3429 retvaltemp
= fileio_close(fileio
);
3430 if (retvaltemp
!= ERROR_OK
)
3439 IMAGE_CHECKSUM_ONLY
= 2
3442 static COMMAND_HELPER(handle_verify_image_command_internal
, enum verify_mode verify
)
3446 uint32_t image_size
;
3449 uint32_t checksum
= 0;
3450 uint32_t mem_checksum
= 0;
3454 struct target
*target
= get_current_target(CMD_CTX
);
3457 return ERROR_COMMAND_SYNTAX_ERROR
;
3460 LOG_ERROR("no target selected");
3464 struct duration bench
;
3465 duration_start(&bench
);
3467 if (CMD_ARGC
>= 2) {
3469 COMMAND_PARSE_ADDRESS(CMD_ARGV
[1], addr
);
3470 image
.base_address
= addr
;
3471 image
.base_address_set
= 1;
3473 image
.base_address_set
= 0;
3474 image
.base_address
= 0x0;
3477 image
.start_address_set
= 0;
3479 retval
= image_open(&image
, CMD_ARGV
[0], (CMD_ARGC
== 3) ? CMD_ARGV
[2] : NULL
);
3480 if (retval
!= ERROR_OK
)
3486 for (i
= 0; i
< image
.num_sections
; i
++) {
3487 buffer
= malloc(image
.sections
[i
].size
);
3488 if (buffer
== NULL
) {
3489 command_print(CMD_CTX
,
3490 "error allocating buffer for section (%d bytes)",
3491 (int)(image
.sections
[i
].size
));
3494 retval
= image_read_section(&image
, i
, 0x0, image
.sections
[i
].size
, buffer
, &buf_cnt
);
3495 if (retval
!= ERROR_OK
) {
3500 if (verify
>= IMAGE_VERIFY
) {
3501 /* calculate checksum of image */
3502 retval
= image_calculate_checksum(buffer
, buf_cnt
, &checksum
);
3503 if (retval
!= ERROR_OK
) {
3508 retval
= target_checksum_memory(target
, image
.sections
[i
].base_address
, buf_cnt
, &mem_checksum
);
3509 if (retval
!= ERROR_OK
) {
3513 if ((checksum
!= mem_checksum
) && (verify
== IMAGE_CHECKSUM_ONLY
)) {
3514 LOG_ERROR("checksum mismatch");
3516 retval
= ERROR_FAIL
;
3519 if (checksum
!= mem_checksum
) {
3520 /* failed crc checksum, fall back to a binary compare */
3524 LOG_ERROR("checksum mismatch - attempting binary compare");
3526 data
= malloc(buf_cnt
);
3528 /* Can we use 32bit word accesses? */
3530 int count
= buf_cnt
;
3531 if ((count
% 4) == 0) {
3535 retval
= target_read_memory(target
, image
.sections
[i
].base_address
, size
, count
, data
);
3536 if (retval
== ERROR_OK
) {
3538 for (t
= 0; t
< buf_cnt
; t
++) {
3539 if (data
[t
] != buffer
[t
]) {
3540 command_print(CMD_CTX
,
3541 "diff %d address 0x%08x. Was 0x%02x instead of 0x%02x",
3543 (unsigned)(t
+ image
.sections
[i
].base_address
),
3546 if (diffs
++ >= 127) {
3547 command_print(CMD_CTX
, "More than 128 errors, the rest are not printed.");
3559 command_print(CMD_CTX
, "address " TARGET_ADDR_FMT
" length 0x%08zx",
3560 image
.sections
[i
].base_address
,
3565 image_size
+= buf_cnt
;
3568 command_print(CMD_CTX
, "No more differences found.");
3571 retval
= ERROR_FAIL
;
3572 if ((ERROR_OK
== retval
) && (duration_measure(&bench
) == ERROR_OK
)) {
3573 command_print(CMD_CTX
, "verified %" PRIu32
" bytes "
3574 "in %fs (%0.3f KiB/s)", image_size
,
3575 duration_elapsed(&bench
), duration_kbps(&bench
, image_size
));
3578 image_close(&image
);
3583 COMMAND_HANDLER(handle_verify_image_checksum_command
)
3585 return CALL_COMMAND_HANDLER(handle_verify_image_command_internal
, IMAGE_CHECKSUM_ONLY
);
3588 COMMAND_HANDLER(handle_verify_image_command
)
3590 return CALL_COMMAND_HANDLER(handle_verify_image_command_internal
, IMAGE_VERIFY
);
3593 COMMAND_HANDLER(handle_test_image_command
)
3595 return CALL_COMMAND_HANDLER(handle_verify_image_command_internal
, IMAGE_TEST
);
3598 static int handle_bp_command_list(struct command_context
*cmd_ctx
)
3600 struct target
*target
= get_current_target(cmd_ctx
);
3601 struct breakpoint
*breakpoint
= target
->breakpoints
;
3602 while (breakpoint
) {
3603 if (breakpoint
->type
== BKPT_SOFT
) {
3604 char *buf
= buf_to_str(breakpoint
->orig_instr
,
3605 breakpoint
->length
, 16);
3606 command_print(cmd_ctx
, "IVA breakpoint: " TARGET_ADDR_FMT
", 0x%x, %i, 0x%s",
3607 breakpoint
->address
,
3609 breakpoint
->set
, buf
);
3612 if ((breakpoint
->address
== 0) && (breakpoint
->asid
!= 0))
3613 command_print(cmd_ctx
, "Context breakpoint: 0x%8.8" PRIx32
", 0x%x, %i",
3615 breakpoint
->length
, breakpoint
->set
);
3616 else if ((breakpoint
->address
!= 0) && (breakpoint
->asid
!= 0)) {
3617 command_print(cmd_ctx
, "Hybrid breakpoint(IVA): " TARGET_ADDR_FMT
", 0x%x, %i",
3618 breakpoint
->address
,
3619 breakpoint
->length
, breakpoint
->set
);
3620 command_print(cmd_ctx
, "\t|--->linked with ContextID: 0x%8.8" PRIx32
,
3623 command_print(cmd_ctx
, "Breakpoint(IVA): " TARGET_ADDR_FMT
", 0x%x, %i",
3624 breakpoint
->address
,
3625 breakpoint
->length
, breakpoint
->set
);
3628 breakpoint
= breakpoint
->next
;
3633 static int handle_bp_command_set(struct command_context
*cmd_ctx
,
3634 target_addr_t addr
, uint32_t asid
, uint32_t length
, int hw
)
3636 struct target
*target
= get_current_target(cmd_ctx
);
3640 retval
= breakpoint_add(target
, addr
, length
, hw
);
3641 if (ERROR_OK
== retval
)
3642 command_print(cmd_ctx
, "breakpoint set at " TARGET_ADDR_FMT
"", addr
);
3644 LOG_ERROR("Failure setting breakpoint, the same address(IVA) is already used");
3647 } else if (addr
== 0) {
3648 if (target
->type
->add_context_breakpoint
== NULL
) {
3649 LOG_WARNING("Context breakpoint not available");
3652 retval
= context_breakpoint_add(target
, asid
, length
, hw
);
3653 if (ERROR_OK
== retval
)
3654 command_print(cmd_ctx
, "Context breakpoint set at 0x%8.8" PRIx32
"", asid
);
3656 LOG_ERROR("Failure setting breakpoint, the same address(CONTEXTID) is already used");
3660 if (target
->type
->add_hybrid_breakpoint
== NULL
) {
3661 LOG_WARNING("Hybrid breakpoint not available");
3664 retval
= hybrid_breakpoint_add(target
, addr
, asid
, length
, hw
);
3665 if (ERROR_OK
== retval
)
3666 command_print(cmd_ctx
, "Hybrid breakpoint set at 0x%8.8" PRIx32
"", asid
);
3668 LOG_ERROR("Failure setting breakpoint, the same address is already used");
3675 COMMAND_HANDLER(handle_bp_command
)
3684 return handle_bp_command_list(CMD_CTX
);
3688 COMMAND_PARSE_ADDRESS(CMD_ARGV
[0], addr
);
3689 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], length
);
3690 return handle_bp_command_set(CMD_CTX
, addr
, asid
, length
, hw
);
3693 if (strcmp(CMD_ARGV
[2], "hw") == 0) {
3695 COMMAND_PARSE_ADDRESS(CMD_ARGV
[0], addr
);
3696 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], length
);
3698 return handle_bp_command_set(CMD_CTX
, addr
, asid
, length
, hw
);
3699 } else if (strcmp(CMD_ARGV
[2], "hw_ctx") == 0) {
3701 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], asid
);
3702 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], length
);
3704 return handle_bp_command_set(CMD_CTX
, addr
, asid
, length
, hw
);
3709 COMMAND_PARSE_ADDRESS(CMD_ARGV
[0], addr
);
3710 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], asid
);
3711 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[2], length
);
3712 return handle_bp_command_set(CMD_CTX
, addr
, asid
, length
, hw
);
3715 return ERROR_COMMAND_SYNTAX_ERROR
;
3719 COMMAND_HANDLER(handle_rbp_command
)
3722 return ERROR_COMMAND_SYNTAX_ERROR
;
3725 COMMAND_PARSE_ADDRESS(CMD_ARGV
[0], addr
);
3727 struct target
*target
= get_current_target(CMD_CTX
);
3728 breakpoint_remove(target
, addr
);
3733 COMMAND_HANDLER(handle_wp_command
)
3735 struct target
*target
= get_current_target(CMD_CTX
);
3737 if (CMD_ARGC
== 0) {
3738 struct watchpoint
*watchpoint
= target
->watchpoints
;
3740 while (watchpoint
) {
3741 command_print(CMD_CTX
, "address: " TARGET_ADDR_FMT
3742 ", len: 0x%8.8" PRIx32
3743 ", r/w/a: %i, value: 0x%8.8" PRIx32
3744 ", mask: 0x%8.8" PRIx32
,
3745 watchpoint
->address
,
3747 (int)watchpoint
->rw
,
3750 watchpoint
= watchpoint
->next
;
3755 enum watchpoint_rw type
= WPT_ACCESS
;
3757 uint32_t length
= 0;
3758 uint32_t data_value
= 0x0;
3759 uint32_t data_mask
= 0xffffffff;
3763 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[4], data_mask
);
3766 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[3], data_value
);
3769 switch (CMD_ARGV
[2][0]) {
3780 LOG_ERROR("invalid watchpoint mode ('%c')", CMD_ARGV
[2][0]);
3781 return ERROR_COMMAND_SYNTAX_ERROR
;
3785 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], length
);
3786 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], addr
);
3790 return ERROR_COMMAND_SYNTAX_ERROR
;
3793 int retval
= watchpoint_add(target
, addr
, length
, type
,
3794 data_value
, data_mask
);
3795 if (ERROR_OK
!= retval
)
3796 LOG_ERROR("Failure setting watchpoints");
3801 COMMAND_HANDLER(handle_rwp_command
)
3804 return ERROR_COMMAND_SYNTAX_ERROR
;
3807 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], addr
);
3809 struct target
*target
= get_current_target(CMD_CTX
);
3810 watchpoint_remove(target
, addr
);
3816 * Translate a virtual address to a physical address.
3818 * The low-level target implementation must have logged a detailed error
3819 * which is forwarded to telnet/GDB session.
3821 COMMAND_HANDLER(handle_virt2phys_command
)
3824 return ERROR_COMMAND_SYNTAX_ERROR
;
3827 COMMAND_PARSE_ADDRESS(CMD_ARGV
[0], va
);
3830 struct target
*target
= get_current_target(CMD_CTX
);
3831 int retval
= target
->type
->virt2phys(target
, va
, &pa
);
3832 if (retval
== ERROR_OK
)
3833 command_print(CMD_CTX
, "Physical address " TARGET_ADDR_FMT
"", pa
);
3838 static void writeData(FILE *f
, const void *data
, size_t len
)
3840 size_t written
= fwrite(data
, 1, len
, f
);
3842 LOG_ERROR("failed to write %zu bytes: %s", len
, strerror(errno
));
3845 static void writeLong(FILE *f
, int l
, struct target
*target
)
3849 target_buffer_set_u32(target
, val
, l
);
3850 writeData(f
, val
, 4);
3853 static void writeString(FILE *f
, char *s
)
3855 writeData(f
, s
, strlen(s
));
3858 typedef unsigned char UNIT
[2]; /* unit of profiling */
3860 /* Dump a gmon.out histogram file. */
3861 static void write_gmon(uint32_t *samples
, uint32_t sampleNum
, const char *filename
, bool with_range
,
3862 uint32_t start_address
, uint32_t end_address
, struct target
*target
, uint32_t duration_ms
)
3865 FILE *f
= fopen(filename
, "w");
3868 writeString(f
, "gmon");
3869 writeLong(f
, 0x00000001, target
); /* Version */
3870 writeLong(f
, 0, target
); /* padding */
3871 writeLong(f
, 0, target
); /* padding */
3872 writeLong(f
, 0, target
); /* padding */
3874 uint8_t zero
= 0; /* GMON_TAG_TIME_HIST */
3875 writeData(f
, &zero
, 1);
3877 /* figure out bucket size */
3881 min
= start_address
;
3886 for (i
= 0; i
< sampleNum
; i
++) {
3887 if (min
> samples
[i
])
3889 if (max
< samples
[i
])
3893 /* max should be (largest sample + 1)
3894 * Refer to binutils/gprof/hist.c (find_histogram_for_pc) */
3898 int addressSpace
= max
- min
;
3899 assert(addressSpace
>= 2);
3901 /* FIXME: What is the reasonable number of buckets?
3902 * The profiling result will be more accurate if there are enough buckets. */
3903 static const uint32_t maxBuckets
= 128 * 1024; /* maximum buckets. */
3904 uint32_t numBuckets
= addressSpace
/ sizeof(UNIT
);
3905 if (numBuckets
> maxBuckets
)
3906 numBuckets
= maxBuckets
;
3907 int *buckets
= malloc(sizeof(int) * numBuckets
);
3908 if (buckets
== NULL
) {
3912 memset(buckets
, 0, sizeof(int) * numBuckets
);
3913 for (i
= 0; i
< sampleNum
; i
++) {
3914 uint32_t address
= samples
[i
];
3916 if ((address
< min
) || (max
<= address
))
3919 long long a
= address
- min
;
3920 long long b
= numBuckets
;
3921 long long c
= addressSpace
;
3922 int index_t
= (a
* b
) / c
; /* danger!!!! int32 overflows */
3926 /* append binary memory gmon.out &profile_hist_hdr ((char*)&profile_hist_hdr + sizeof(struct gmon_hist_hdr)) */
3927 writeLong(f
, min
, target
); /* low_pc */
3928 writeLong(f
, max
, target
); /* high_pc */
3929 writeLong(f
, numBuckets
, target
); /* # of buckets */
3930 float sample_rate
= sampleNum
/ (duration_ms
/ 1000.0);
3931 writeLong(f
, sample_rate
, target
);
3932 writeString(f
, "seconds");
3933 for (i
= 0; i
< (15-strlen("seconds")); i
++)
3934 writeData(f
, &zero
, 1);
3935 writeString(f
, "s");
3937 /*append binary memory gmon.out profile_hist_data (profile_hist_data + profile_hist_hdr.hist_size) */
3939 char *data
= malloc(2 * numBuckets
);
3941 for (i
= 0; i
< numBuckets
; i
++) {
3946 data
[i
* 2] = val
&0xff;
3947 data
[i
* 2 + 1] = (val
>> 8) & 0xff;
3950 writeData(f
, data
, numBuckets
* 2);
3958 /* profiling samples the CPU PC as quickly as OpenOCD is able,
3959 * which will be used as a random sampling of PC */
3960 COMMAND_HANDLER(handle_profile_command
)
3962 struct target
*target
= get_current_target(CMD_CTX
);
3964 if ((CMD_ARGC
!= 2) && (CMD_ARGC
!= 4))
3965 return ERROR_COMMAND_SYNTAX_ERROR
;
3967 const uint32_t MAX_PROFILE_SAMPLE_NUM
= 10000;
3969 uint32_t num_of_samples
;
3970 int retval
= ERROR_OK
;
3972 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], offset
);
3974 uint32_t *samples
= malloc(sizeof(uint32_t) * MAX_PROFILE_SAMPLE_NUM
);
3975 if (samples
== NULL
) {
3976 LOG_ERROR("No memory to store samples.");
3980 uint64_t timestart_ms
= timeval_ms();
3982 * Some cores let us sample the PC without the
3983 * annoying halt/resume step; for example, ARMv7 PCSR.
3984 * Provide a way to use that more efficient mechanism.
3986 retval
= target_profiling(target
, samples
, MAX_PROFILE_SAMPLE_NUM
,
3987 &num_of_samples
, offset
);
3988 if (retval
!= ERROR_OK
) {
3992 uint32_t duration_ms
= timeval_ms() - timestart_ms
;
3994 assert(num_of_samples
<= MAX_PROFILE_SAMPLE_NUM
);
3996 retval
= target_poll(target
);
3997 if (retval
!= ERROR_OK
) {
4001 if (target
->state
== TARGET_RUNNING
) {
4002 retval
= target_halt(target
);
4003 if (retval
!= ERROR_OK
) {
4009 retval
= target_poll(target
);
4010 if (retval
!= ERROR_OK
) {
4015 uint32_t start_address
= 0;
4016 uint32_t end_address
= 0;
4017 bool with_range
= false;
4018 if (CMD_ARGC
== 4) {
4020 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[2], start_address
);
4021 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[3], end_address
);
4024 write_gmon(samples
, num_of_samples
, CMD_ARGV
[1],
4025 with_range
, start_address
, end_address
, target
, duration_ms
);
4026 command_print(CMD_CTX
, "Wrote %s", CMD_ARGV
[1]);
4032 static int new_int_array_element(Jim_Interp
*interp
, const char *varname
, int idx
, uint32_t val
)
4035 Jim_Obj
*nameObjPtr
, *valObjPtr
;
4038 namebuf
= alloc_printf("%s(%d)", varname
, idx
);
4042 nameObjPtr
= Jim_NewStringObj(interp
, namebuf
, -1);
4043 valObjPtr
= Jim_NewIntObj(interp
, val
);
4044 if (!nameObjPtr
|| !valObjPtr
) {
4049 Jim_IncrRefCount(nameObjPtr
);
4050 Jim_IncrRefCount(valObjPtr
);
4051 result
= Jim_SetVariable(interp
, nameObjPtr
, valObjPtr
);
4052 Jim_DecrRefCount(interp
, nameObjPtr
);
4053 Jim_DecrRefCount(interp
, valObjPtr
);
4055 /* printf("%s(%d) <= 0%08x\n", varname, idx, val); */
4059 static int jim_mem2array(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4061 struct command_context
*context
;
4062 struct target
*target
;
4064 context
= current_command_context(interp
);
4065 assert(context
!= NULL
);
4067 target
= get_current_target(context
);
4068 if (target
== NULL
) {
4069 LOG_ERROR("mem2array: no current target");
4073 return target_mem2array(interp
, target
, argc
- 1, argv
+ 1);
4076 static int target_mem2array(Jim_Interp
*interp
, struct target
*target
, int argc
, Jim_Obj
*const *argv
)
4084 const char *varname
;
4090 /* argv[1] = name of array to receive the data
4091 * argv[2] = desired width
4092 * argv[3] = memory address
4093 * argv[4] = count of times to read
4095 if (argc
< 4 || argc
> 5) {
4096 Jim_WrongNumArgs(interp
, 1, argv
, "varname width addr nelems [phys]");
4099 varname
= Jim_GetString(argv
[0], &len
);
4100 /* given "foo" get space for worse case "foo(%d)" .. add 20 */
4102 e
= Jim_GetLong(interp
, argv
[1], &l
);
4107 e
= Jim_GetLong(interp
, argv
[2], &l
);
4111 e
= Jim_GetLong(interp
, argv
[3], &l
);
4117 phys
= Jim_GetString(argv
[4], &n
);
4118 if (!strncmp(phys
, "phys", n
))
4134 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
4135 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "Invalid width param, must be 8/16/32", NULL
);
4139 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
4140 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "mem2array: zero width read?", NULL
);
4143 if ((addr
+ (len
* width
)) < addr
) {
4144 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
4145 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "mem2array: addr + len - wraps to zero?", NULL
);
4148 /* absurd transfer size? */
4150 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
4151 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "mem2array: absurd > 64K item request", NULL
);
4156 ((width
== 2) && ((addr
& 1) == 0)) ||
4157 ((width
== 4) && ((addr
& 3) == 0))) {
4161 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
4162 sprintf(buf
, "mem2array address: 0x%08" PRIx32
" is not aligned for %" PRId32
" byte reads",
4165 Jim_AppendStrings(interp
, Jim_GetResult(interp
), buf
, NULL
);
4174 size_t buffersize
= 4096;
4175 uint8_t *buffer
= malloc(buffersize
);
4182 /* Slurp... in buffer size chunks */
4184 count
= len
; /* in objects.. */
4185 if (count
> (buffersize
/ width
))
4186 count
= (buffersize
/ width
);
4189 retval
= target_read_phys_memory(target
, addr
, width
, count
, buffer
);
4191 retval
= target_read_memory(target
, addr
, width
, count
, buffer
);
4192 if (retval
!= ERROR_OK
) {
4194 LOG_ERROR("mem2array: Read @ 0x%08" PRIx32
", w=%" PRId32
", cnt=%" PRId32
", failed",
4198 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
4199 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "mem2array: cannot read memory", NULL
);
4203 v
= 0; /* shut up gcc */
4204 for (i
= 0; i
< count
; i
++, n
++) {
4207 v
= target_buffer_get_u32(target
, &buffer
[i
*width
]);
4210 v
= target_buffer_get_u16(target
, &buffer
[i
*width
]);
4213 v
= buffer
[i
] & 0x0ff;
4216 new_int_array_element(interp
, varname
, n
, v
);
4219 addr
+= count
* width
;
4225 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
4230 static int get_int_array_element(Jim_Interp
*interp
, const char *varname
, int idx
, uint32_t *val
)
4233 Jim_Obj
*nameObjPtr
, *valObjPtr
;
4237 namebuf
= alloc_printf("%s(%d)", varname
, idx
);
4241 nameObjPtr
= Jim_NewStringObj(interp
, namebuf
, -1);
4247 Jim_IncrRefCount(nameObjPtr
);
4248 valObjPtr
= Jim_GetVariable(interp
, nameObjPtr
, JIM_ERRMSG
);
4249 Jim_DecrRefCount(interp
, nameObjPtr
);
4251 if (valObjPtr
== NULL
)
4254 result
= Jim_GetLong(interp
, valObjPtr
, &l
);
4255 /* printf("%s(%d) => 0%08x\n", varname, idx, val); */
4260 static int jim_array2mem(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4262 struct command_context
*context
;
4263 struct target
*target
;
4265 context
= current_command_context(interp
);
4266 assert(context
!= NULL
);
4268 target
= get_current_target(context
);
4269 if (target
== NULL
) {
4270 LOG_ERROR("array2mem: no current target");
4274 return target_array2mem(interp
, target
, argc
-1, argv
+ 1);
4277 static int target_array2mem(Jim_Interp
*interp
, struct target
*target
,
4278 int argc
, Jim_Obj
*const *argv
)
4286 const char *varname
;
4292 /* argv[1] = name of array to get the data
4293 * argv[2] = desired width
4294 * argv[3] = memory address
4295 * argv[4] = count to write
4297 if (argc
< 4 || argc
> 5) {
4298 Jim_WrongNumArgs(interp
, 0, argv
, "varname width addr nelems [phys]");
4301 varname
= Jim_GetString(argv
[0], &len
);
4302 /* given "foo" get space for worse case "foo(%d)" .. add 20 */
4304 e
= Jim_GetLong(interp
, argv
[1], &l
);
4309 e
= Jim_GetLong(interp
, argv
[2], &l
);
4313 e
= Jim_GetLong(interp
, argv
[3], &l
);
4319 phys
= Jim_GetString(argv
[4], &n
);
4320 if (!strncmp(phys
, "phys", n
))
4336 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
4337 Jim_AppendStrings(interp
, Jim_GetResult(interp
),
4338 "Invalid width param, must be 8/16/32", NULL
);
4342 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
4343 Jim_AppendStrings(interp
, Jim_GetResult(interp
),
4344 "array2mem: zero width read?", NULL
);
4347 if ((addr
+ (len
* width
)) < addr
) {
4348 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
4349 Jim_AppendStrings(interp
, Jim_GetResult(interp
),
4350 "array2mem: addr + len - wraps to zero?", NULL
);
4353 /* absurd transfer size? */
4355 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
4356 Jim_AppendStrings(interp
, Jim_GetResult(interp
),
4357 "array2mem: absurd > 64K item request", NULL
);
4362 ((width
== 2) && ((addr
& 1) == 0)) ||
4363 ((width
== 4) && ((addr
& 3) == 0))) {
4367 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
4368 sprintf(buf
, "array2mem address: 0x%08" PRIx32
" is not aligned for %" PRId32
" byte reads",
4371 Jim_AppendStrings(interp
, Jim_GetResult(interp
), buf
, NULL
);
4382 size_t buffersize
= 4096;
4383 uint8_t *buffer
= malloc(buffersize
);
4388 /* Slurp... in buffer size chunks */
4390 count
= len
; /* in objects.. */
4391 if (count
> (buffersize
/ width
))
4392 count
= (buffersize
/ width
);
4394 v
= 0; /* shut up gcc */
4395 for (i
= 0; i
< count
; i
++, n
++) {
4396 get_int_array_element(interp
, varname
, n
, &v
);
4399 target_buffer_set_u32(target
, &buffer
[i
* width
], v
);
4402 target_buffer_set_u16(target
, &buffer
[i
* width
], v
);
4405 buffer
[i
] = v
& 0x0ff;
4412 retval
= target_write_phys_memory(target
, addr
, width
, count
, buffer
);
4414 retval
= target_write_memory(target
, addr
, width
, count
, buffer
);
4415 if (retval
!= ERROR_OK
) {
4417 LOG_ERROR("array2mem: Write @ 0x%08" PRIx32
", w=%" PRId32
", cnt=%" PRId32
", failed",
4421 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
4422 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "array2mem: cannot read memory", NULL
);
4426 addr
+= count
* width
;
4431 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
4436 /* FIX? should we propagate errors here rather than printing them
4439 void target_handle_event(struct target
*target
, enum target_event e
)
4441 struct target_event_action
*teap
;
4443 for (teap
= target
->event_action
; teap
!= NULL
; teap
= teap
->next
) {
4444 if (teap
->event
== e
) {
4445 LOG_DEBUG("target: (%d) %s (%s) event: %d (%s) action: %s",
4446 target
->target_number
,
4447 target_name(target
),
4448 target_type_name(target
),
4450 Jim_Nvp_value2name_simple(nvp_target_event
, e
)->name
,
4451 Jim_GetString(teap
->body
, NULL
));
4452 if (Jim_EvalObj(teap
->interp
, teap
->body
) != JIM_OK
) {
4453 Jim_MakeErrorMessage(teap
->interp
);
4454 command_print(NULL
, "%s\n", Jim_GetString(Jim_GetResult(teap
->interp
), NULL
));
4461 * Returns true only if the target has a handler for the specified event.
4463 bool target_has_event_action(struct target
*target
, enum target_event event
)
4465 struct target_event_action
*teap
;
4467 for (teap
= target
->event_action
; teap
!= NULL
; teap
= teap
->next
) {
4468 if (teap
->event
== event
)
4474 enum target_cfg_param
{
4477 TCFG_WORK_AREA_VIRT
,
4478 TCFG_WORK_AREA_PHYS
,
4479 TCFG_WORK_AREA_SIZE
,
4480 TCFG_WORK_AREA_BACKUP
,
4483 TCFG_CHAIN_POSITION
,
4490 static Jim_Nvp nvp_config_opts
[] = {
4491 { .name
= "-type", .value
= TCFG_TYPE
},
4492 { .name
= "-event", .value
= TCFG_EVENT
},
4493 { .name
= "-work-area-virt", .value
= TCFG_WORK_AREA_VIRT
},
4494 { .name
= "-work-area-phys", .value
= TCFG_WORK_AREA_PHYS
},
4495 { .name
= "-work-area-size", .value
= TCFG_WORK_AREA_SIZE
},
4496 { .name
= "-work-area-backup", .value
= TCFG_WORK_AREA_BACKUP
},
4497 { .name
= "-endian" , .value
= TCFG_ENDIAN
},
4498 { .name
= "-coreid", .value
= TCFG_COREID
},
4499 { .name
= "-chain-position", .value
= TCFG_CHAIN_POSITION
},
4500 { .name
= "-dbgbase", .value
= TCFG_DBGBASE
},
4501 { .name
= "-ctibase", .value
= TCFG_CTIBASE
},
4502 { .name
= "-rtos", .value
= TCFG_RTOS
},
4503 { .name
= "-defer-examine", .value
= TCFG_DEFER_EXAMINE
},
4504 { .name
= NULL
, .value
= -1 }
4507 static int target_configure(Jim_GetOptInfo
*goi
, struct target
*target
)
4514 /* parse config or cget options ... */
4515 while (goi
->argc
> 0) {
4516 Jim_SetEmptyResult(goi
->interp
);
4517 /* Jim_GetOpt_Debug(goi); */
4519 if (target
->type
->target_jim_configure
) {
4520 /* target defines a configure function */
4521 /* target gets first dibs on parameters */
4522 e
= (*(target
->type
->target_jim_configure
))(target
, goi
);
4531 /* otherwise we 'continue' below */
4533 e
= Jim_GetOpt_Nvp(goi
, nvp_config_opts
, &n
);
4535 Jim_GetOpt_NvpUnknown(goi
, nvp_config_opts
, 0);
4541 if (goi
->isconfigure
) {
4542 Jim_SetResultFormatted(goi
->interp
,
4543 "not settable: %s", n
->name
);
4547 if (goi
->argc
!= 0) {
4548 Jim_WrongNumArgs(goi
->interp
,
4549 goi
->argc
, goi
->argv
,
4554 Jim_SetResultString(goi
->interp
,
4555 target_type_name(target
), -1);
4559 if (goi
->argc
== 0) {
4560 Jim_WrongNumArgs(goi
->interp
, goi
->argc
, goi
->argv
, "-event ?event-name? ...");
4564 e
= Jim_GetOpt_Nvp(goi
, nvp_target_event
, &n
);
4566 Jim_GetOpt_NvpUnknown(goi
, nvp_target_event
, 1);
4570 if (goi
->isconfigure
) {
4571 if (goi
->argc
!= 1) {
4572 Jim_WrongNumArgs(goi
->interp
, goi
->argc
, goi
->argv
, "-event ?event-name? ?EVENT-BODY?");
4576 if (goi
->argc
!= 0) {
4577 Jim_WrongNumArgs(goi
->interp
, goi
->argc
, goi
->argv
, "-event ?event-name?");
4583 struct target_event_action
*teap
;
4585 teap
= target
->event_action
;
4586 /* replace existing? */
4588 if (teap
->event
== (enum target_event
)n
->value
)
4593 if (goi
->isconfigure
) {
4594 bool replace
= true;
4597 teap
= calloc(1, sizeof(*teap
));
4600 teap
->event
= n
->value
;
4601 teap
->interp
= goi
->interp
;
4602 Jim_GetOpt_Obj(goi
, &o
);
4604 Jim_DecrRefCount(teap
->interp
, teap
->body
);
4605 teap
->body
= Jim_DuplicateObj(goi
->interp
, o
);
4608 * Tcl/TK - "tk events" have a nice feature.
4609 * See the "BIND" command.
4610 * We should support that here.
4611 * You can specify %X and %Y in the event code.
4612 * The idea is: %T - target name.
4613 * The idea is: %N - target number
4614 * The idea is: %E - event name.
4616 Jim_IncrRefCount(teap
->body
);
4619 /* add to head of event list */
4620 teap
->next
= target
->event_action
;
4621 target
->event_action
= teap
;
4623 Jim_SetEmptyResult(goi
->interp
);
4627 Jim_SetEmptyResult(goi
->interp
);
4629 Jim_SetResult(goi
->interp
, Jim_DuplicateObj(goi
->interp
, teap
->body
));
4635 case TCFG_WORK_AREA_VIRT
:
4636 if (goi
->isconfigure
) {
4637 target_free_all_working_areas(target
);
4638 e
= Jim_GetOpt_Wide(goi
, &w
);
4641 target
->working_area_virt
= w
;
4642 target
->working_area_virt_spec
= true;
4647 Jim_SetResult(goi
->interp
, Jim_NewIntObj(goi
->interp
, target
->working_area_virt
));
4651 case TCFG_WORK_AREA_PHYS
:
4652 if (goi
->isconfigure
) {
4653 target_free_all_working_areas(target
);
4654 e
= Jim_GetOpt_Wide(goi
, &w
);
4657 target
->working_area_phys
= w
;
4658 target
->working_area_phys_spec
= true;
4663 Jim_SetResult(goi
->interp
, Jim_NewIntObj(goi
->interp
, target
->working_area_phys
));
4667 case TCFG_WORK_AREA_SIZE
:
4668 if (goi
->isconfigure
) {
4669 target_free_all_working_areas(target
);
4670 e
= Jim_GetOpt_Wide(goi
, &w
);
4673 target
->working_area_size
= w
;
4678 Jim_SetResult(goi
->interp
, Jim_NewIntObj(goi
->interp
, target
->working_area_size
));
4682 case TCFG_WORK_AREA_BACKUP
:
4683 if (goi
->isconfigure
) {
4684 target_free_all_working_areas(target
);
4685 e
= Jim_GetOpt_Wide(goi
, &w
);
4688 /* make this exactly 1 or 0 */
4689 target
->backup_working_area
= (!!w
);
4694 Jim_SetResult(goi
->interp
, Jim_NewIntObj(goi
->interp
, target
->backup_working_area
));
4695 /* loop for more e*/
4700 if (goi
->isconfigure
) {
4701 e
= Jim_GetOpt_Nvp(goi
, nvp_target_endian
, &n
);
4703 Jim_GetOpt_NvpUnknown(goi
, nvp_target_endian
, 1);
4706 target
->endianness
= n
->value
;
4711 n
= Jim_Nvp_value2name_simple(nvp_target_endian
, target
->endianness
);
4712 if (n
->name
== NULL
) {
4713 target
->endianness
= TARGET_LITTLE_ENDIAN
;
4714 n
= Jim_Nvp_value2name_simple(nvp_target_endian
, target
->endianness
);
4716 Jim_SetResultString(goi
->interp
, n
->name
, -1);
4721 if (goi
->isconfigure
) {
4722 e
= Jim_GetOpt_Wide(goi
, &w
);
4725 target
->coreid
= (int32_t)w
;
4730 Jim_SetResult(goi
->interp
, Jim_NewIntObj(goi
->interp
, target
->working_area_size
));
4734 case TCFG_CHAIN_POSITION
:
4735 if (goi
->isconfigure
) {
4737 struct jtag_tap
*tap
;
4738 target_free_all_working_areas(target
);
4739 e
= Jim_GetOpt_Obj(goi
, &o_t
);
4742 tap
= jtag_tap_by_jim_obj(goi
->interp
, o_t
);
4745 /* make this exactly 1 or 0 */
4751 Jim_SetResultString(goi
->interp
, target
->tap
->dotted_name
, -1);
4752 /* loop for more e*/
4755 if (goi
->isconfigure
) {
4756 e
= Jim_GetOpt_Wide(goi
, &w
);
4759 target
->dbgbase
= (uint32_t)w
;
4760 target
->dbgbase_set
= true;
4765 Jim_SetResult(goi
->interp
, Jim_NewIntObj(goi
->interp
, target
->dbgbase
));
4769 if (goi
->isconfigure
) {
4770 e
= Jim_GetOpt_Wide(goi
, &w
);
4773 target
->ctibase
= (uint32_t)w
;
4774 target
->ctibase_set
= true;
4779 Jim_SetResult(goi
->interp
, Jim_NewIntObj(goi
->interp
, target
->ctibase
));
4785 int result
= rtos_create(goi
, target
);
4786 if (result
!= JIM_OK
)
4792 case TCFG_DEFER_EXAMINE
:
4794 target
->defer_examine
= true;
4799 } /* while (goi->argc) */
4802 /* done - we return */
4806 static int jim_target_configure(Jim_Interp
*interp
, int argc
, Jim_Obj
* const *argv
)
4810 Jim_GetOpt_Setup(&goi
, interp
, argc
- 1, argv
+ 1);
4811 goi
.isconfigure
= !strcmp(Jim_GetString(argv
[0], NULL
), "configure");
4813 Jim_WrongNumArgs(goi
.interp
, goi
.argc
, goi
.argv
,
4814 "missing: -option ...");
4817 struct target
*target
= Jim_CmdPrivData(goi
.interp
);
4818 return target_configure(&goi
, target
);
4821 static int jim_target_mw(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4823 const char *cmd_name
= Jim_GetString(argv
[0], NULL
);
4826 Jim_GetOpt_Setup(&goi
, interp
, argc
- 1, argv
+ 1);
4828 if (goi
.argc
< 2 || goi
.argc
> 4) {
4829 Jim_SetResultFormatted(goi
.interp
,
4830 "usage: %s [phys] <address> <data> [<count>]", cmd_name
);
4835 fn
= target_write_memory
;
4838 if (strcmp(Jim_GetString(argv
[1], NULL
), "phys") == 0) {
4840 struct Jim_Obj
*obj
;
4841 e
= Jim_GetOpt_Obj(&goi
, &obj
);
4845 fn
= target_write_phys_memory
;
4849 e
= Jim_GetOpt_Wide(&goi
, &a
);
4854 e
= Jim_GetOpt_Wide(&goi
, &b
);
4859 if (goi
.argc
== 1) {
4860 e
= Jim_GetOpt_Wide(&goi
, &c
);
4865 /* all args must be consumed */
4869 struct target
*target
= Jim_CmdPrivData(goi
.interp
);
4871 if (strcasecmp(cmd_name
, "mww") == 0)
4873 else if (strcasecmp(cmd_name
, "mwh") == 0)
4875 else if (strcasecmp(cmd_name
, "mwb") == 0)
4878 LOG_ERROR("command '%s' unknown: ", cmd_name
);
4882 return (target_fill_mem(target
, a
, fn
, data_size
, b
, c
) == ERROR_OK
) ? JIM_OK
: JIM_ERR
;
4886 * @brief Reads an array of words/halfwords/bytes from target memory starting at specified address.
4888 * Usage: mdw [phys] <address> [<count>] - for 32 bit reads
4889 * mdh [phys] <address> [<count>] - for 16 bit reads
4890 * mdb [phys] <address> [<count>] - for 8 bit reads
4892 * Count defaults to 1.
4894 * Calls target_read_memory or target_read_phys_memory depending on
4895 * the presence of the "phys" argument
4896 * Reads the target memory in blocks of max. 32 bytes, and returns an array of ints formatted
4897 * to int representation in base16.
4898 * Also outputs read data in a human readable form using command_print
4900 * @param phys if present target_read_phys_memory will be used instead of target_read_memory
4901 * @param address address where to start the read. May be specified in decimal or hex using the standard "0x" prefix
4902 * @param count optional count parameter to read an array of values. If not specified, defaults to 1.
4903 * @returns: JIM_ERR on error or JIM_OK on success and sets the result string to an array of ascii formatted numbers
4904 * on success, with [<count>] number of elements.
4906 * In case of little endian target:
4907 * Example1: "mdw 0x00000000" returns "10123456"
4908 * Exmaple2: "mdh 0x00000000 1" returns "3456"
4909 * Example3: "mdb 0x00000000" returns "56"
4910 * Example4: "mdh 0x00000000 2" returns "3456 1012"
4911 * Example5: "mdb 0x00000000 3" returns "56 34 12"
4913 static int jim_target_md(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4915 const char *cmd_name
= Jim_GetString(argv
[0], NULL
);
4918 Jim_GetOpt_Setup(&goi
, interp
, argc
- 1, argv
+ 1);
4920 if ((goi
.argc
< 1) || (goi
.argc
> 3)) {
4921 Jim_SetResultFormatted(goi
.interp
,
4922 "usage: %s [phys] <address> [<count>]", cmd_name
);
4926 int (*fn
)(struct target
*target
,
4927 target_addr_t address
, uint32_t size
, uint32_t count
, uint8_t *buffer
);
4928 fn
= target_read_memory
;
4931 if (strcmp(Jim_GetString(argv
[1], NULL
), "phys") == 0) {
4933 struct Jim_Obj
*obj
;
4934 e
= Jim_GetOpt_Obj(&goi
, &obj
);
4938 fn
= target_read_phys_memory
;
4941 /* Read address parameter */
4943 e
= Jim_GetOpt_Wide(&goi
, &addr
);
4947 /* If next parameter exists, read it out as the count parameter, if not, set it to 1 (default) */
4949 if (goi
.argc
== 1) {
4950 e
= Jim_GetOpt_Wide(&goi
, &count
);
4956 /* all args must be consumed */
4960 jim_wide dwidth
= 1; /* shut up gcc */
4961 if (strcasecmp(cmd_name
, "mdw") == 0)
4963 else if (strcasecmp(cmd_name
, "mdh") == 0)
4965 else if (strcasecmp(cmd_name
, "mdb") == 0)
4968 LOG_ERROR("command '%s' unknown: ", cmd_name
);
4972 /* convert count to "bytes" */
4973 int bytes
= count
* dwidth
;
4975 struct target
*target
= Jim_CmdPrivData(goi
.interp
);
4976 uint8_t target_buf
[32];
4979 y
= (bytes
< 16) ? bytes
: 16; /* y = min(bytes, 16); */
4981 /* Try to read out next block */
4982 e
= fn(target
, addr
, dwidth
, y
/ dwidth
, target_buf
);
4984 if (e
!= ERROR_OK
) {
4985 Jim_SetResultFormatted(interp
, "error reading target @ 0x%08lx", (long)addr
);
4989 command_print_sameline(NULL
, "0x%08x ", (int)(addr
));
4992 for (x
= 0; x
< 16 && x
< y
; x
+= 4) {
4993 z
= target_buffer_get_u32(target
, &(target_buf
[x
]));
4994 command_print_sameline(NULL
, "%08x ", (int)(z
));
4996 for (; (x
< 16) ; x
+= 4)
4997 command_print_sameline(NULL
, " ");
5000 for (x
= 0; x
< 16 && x
< y
; x
+= 2) {
5001 z
= target_buffer_get_u16(target
, &(target_buf
[x
]));
5002 command_print_sameline(NULL
, "%04x ", (int)(z
));
5004 for (; (x
< 16) ; x
+= 2)
5005 command_print_sameline(NULL
, " ");
5009 for (x
= 0 ; (x
< 16) && (x
< y
) ; x
+= 1) {
5010 z
= target_buffer_get_u8(target
, &(target_buf
[x
]));
5011 command_print_sameline(NULL
, "%02x ", (int)(z
));
5013 for (; (x
< 16) ; x
+= 1)
5014 command_print_sameline(NULL
, " ");
5017 /* ascii-ify the bytes */
5018 for (x
= 0 ; x
< y
; x
++) {
5019 if ((target_buf
[x
] >= 0x20) &&
5020 (target_buf
[x
] <= 0x7e)) {
5024 target_buf
[x
] = '.';
5029 target_buf
[x
] = ' ';
5034 /* print - with a newline */
5035 command_print_sameline(NULL
, "%s\n", target_buf
);
5043 static int jim_target_mem2array(Jim_Interp
*interp
,
5044 int argc
, Jim_Obj
*const *argv
)
5046 struct target
*target
= Jim_CmdPrivData(interp
);
5047 return target_mem2array(interp
, target
, argc
- 1, argv
+ 1);
5050 static int jim_target_array2mem(Jim_Interp
*interp
,
5051 int argc
, Jim_Obj
*const *argv
)
5053 struct target
*target
= Jim_CmdPrivData(interp
);
5054 return target_array2mem(interp
, target
, argc
- 1, argv
+ 1);
5057 static int jim_target_tap_disabled(Jim_Interp
*interp
)
5059 Jim_SetResultFormatted(interp
, "[TAP is disabled]");
5063 static int jim_target_examine(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
5065 bool allow_defer
= false;
5068 Jim_GetOpt_Setup(&goi
, interp
, argc
- 1, argv
+ 1);
5070 const char *cmd_name
= Jim_GetString(argv
[0], NULL
);
5071 Jim_SetResultFormatted(goi
.interp
,
5072 "usage: %s ['allow-defer']", cmd_name
);
5076 strcmp(Jim_GetString(argv
[1], NULL
), "allow-defer") == 0) {
5078 struct Jim_Obj
*obj
;
5079 int e
= Jim_GetOpt_Obj(&goi
, &obj
);
5085 struct target
*target
= Jim_CmdPrivData(interp
);
5086 if (!target
->tap
->enabled
)
5087 return jim_target_tap_disabled(interp
);
5089 if (allow_defer
&& target
->defer_examine
) {
5090 LOG_INFO("Deferring arp_examine of %s", target_name(target
));
5091 LOG_INFO("Use arp_examine command to examine it manually!");
5095 int e
= target
->type
->examine(target
);
5101 static int jim_target_was_examined(Jim_Interp
*interp
, int argc
, Jim_Obj
* const *argv
)
5103 struct target
*target
= Jim_CmdPrivData(interp
);
5105 Jim_SetResultBool(interp
, target_was_examined(target
));
5109 static int jim_target_examine_deferred(Jim_Interp
*interp
, int argc
, Jim_Obj
* const *argv
)
5111 struct target
*target
= Jim_CmdPrivData(interp
);
5113 Jim_SetResultBool(interp
, target
->defer_examine
);
5117 static int jim_target_halt_gdb(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
5120 Jim_WrongNumArgs(interp
, 1, argv
, "[no parameters]");
5123 struct target
*target
= Jim_CmdPrivData(interp
);
5125 if (target_call_event_callbacks(target
, TARGET_EVENT_GDB_HALT
) != ERROR_OK
)
5131 static int jim_target_poll(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
5134 Jim_WrongNumArgs(interp
, 1, argv
, "[no parameters]");
5137 struct target
*target
= Jim_CmdPrivData(interp
);
5138 if (!target
->tap
->enabled
)
5139 return jim_target_tap_disabled(interp
);
5142 if (!(target_was_examined(target
)))
5143 e
= ERROR_TARGET_NOT_EXAMINED
;
5145 e
= target
->type
->poll(target
);
5151 static int jim_target_reset(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
5154 Jim_GetOpt_Setup(&goi
, interp
, argc
- 1, argv
+ 1);
5156 if (goi
.argc
!= 2) {
5157 Jim_WrongNumArgs(interp
, 0, argv
,
5158 "([tT]|[fF]|assert|deassert) BOOL");
5163 int e
= Jim_GetOpt_Nvp(&goi
, nvp_assert
, &n
);
5165 Jim_GetOpt_NvpUnknown(&goi
, nvp_assert
, 1);
5168 /* the halt or not param */
5170 e
= Jim_GetOpt_Wide(&goi
, &a
);
5174 struct target
*target
= Jim_CmdPrivData(goi
.interp
);
5175 if (!target
->tap
->enabled
)
5176 return jim_target_tap_disabled(interp
);
5178 if (!target
->type
->assert_reset
|| !target
->type
->deassert_reset
) {
5179 Jim_SetResultFormatted(interp
,
5180 "No target-specific reset for %s",
5181 target_name(target
));
5185 if (target
->defer_examine
)
5186 target_reset_examined(target
);
5188 /* determine if we should halt or not. */
5189 target
->reset_halt
= !!a
;
5190 /* When this happens - all workareas are invalid. */
5191 target_free_all_working_areas_restore(target
, 0);
5194 if (n
->value
== NVP_ASSERT
)
5195 e
= target
->type
->assert_reset(target
);
5197 e
= target
->type
->deassert_reset(target
);
5198 return (e
== ERROR_OK
) ? JIM_OK
: JIM_ERR
;
5201 static int jim_target_halt(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
5204 Jim_WrongNumArgs(interp
, 1, argv
, "[no parameters]");
5207 struct target
*target
= Jim_CmdPrivData(interp
);
5208 if (!target
->tap
->enabled
)
5209 return jim_target_tap_disabled(interp
);
5210 int e
= target
->type
->halt(target
);
5211 return (e
== ERROR_OK
) ? JIM_OK
: JIM_ERR
;
5214 static int jim_target_wait_state(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
5217 Jim_GetOpt_Setup(&goi
, interp
, argc
- 1, argv
+ 1);
5219 /* params: <name> statename timeoutmsecs */
5220 if (goi
.argc
!= 2) {
5221 const char *cmd_name
= Jim_GetString(argv
[0], NULL
);
5222 Jim_SetResultFormatted(goi
.interp
,
5223 "%s <state_name> <timeout_in_msec>", cmd_name
);
5228 int e
= Jim_GetOpt_Nvp(&goi
, nvp_target_state
, &n
);
5230 Jim_GetOpt_NvpUnknown(&goi
, nvp_target_state
, 1);
5234 e
= Jim_GetOpt_Wide(&goi
, &a
);
5237 struct target
*target
= Jim_CmdPrivData(interp
);
5238 if (!target
->tap
->enabled
)
5239 return jim_target_tap_disabled(interp
);
5241 e
= target_wait_state(target
, n
->value
, a
);
5242 if (e
!= ERROR_OK
) {
5243 Jim_Obj
*eObj
= Jim_NewIntObj(interp
, e
);
5244 Jim_SetResultFormatted(goi
.interp
,
5245 "target: %s wait %s fails (%#s) %s",
5246 target_name(target
), n
->name
,
5247 eObj
, target_strerror_safe(e
));
5248 Jim_FreeNewObj(interp
, eObj
);
5253 /* List for human, Events defined for this target.
5254 * scripts/programs should use 'name cget -event NAME'
5256 static int jim_target_event_list(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
5258 struct command_context
*cmd_ctx
= current_command_context(interp
);
5259 assert(cmd_ctx
!= NULL
);
5261 struct target
*target
= Jim_CmdPrivData(interp
);
5262 struct target_event_action
*teap
= target
->event_action
;
5263 command_print(cmd_ctx
, "Event actions for target (%d) %s\n",
5264 target
->target_number
,
5265 target_name(target
));
5266 command_print(cmd_ctx
, "%-25s | Body", "Event");
5267 command_print(cmd_ctx
, "------------------------- | "
5268 "----------------------------------------");
5270 Jim_Nvp
*opt
= Jim_Nvp_value2name_simple(nvp_target_event
, teap
->event
);
5271 command_print(cmd_ctx
, "%-25s | %s",
5272 opt
->name
, Jim_GetString(teap
->body
, NULL
));
5275 command_print(cmd_ctx
, "***END***");
5278 static int jim_target_current_state(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
5281 Jim_WrongNumArgs(interp
, 1, argv
, "[no parameters]");
5284 struct target
*target
= Jim_CmdPrivData(interp
);
5285 Jim_SetResultString(interp
, target_state_name(target
), -1);
5288 static int jim_target_invoke_event(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
5291 Jim_GetOpt_Setup(&goi
, interp
, argc
- 1, argv
+ 1);
5292 if (goi
.argc
!= 1) {
5293 const char *cmd_name
= Jim_GetString(argv
[0], NULL
);
5294 Jim_SetResultFormatted(goi
.interp
, "%s <eventname>", cmd_name
);
5298 int e
= Jim_GetOpt_Nvp(&goi
, nvp_target_event
, &n
);
5300 Jim_GetOpt_NvpUnknown(&goi
, nvp_target_event
, 1);
5303 struct target
*target
= Jim_CmdPrivData(interp
);
5304 target_handle_event(target
, n
->value
);
5308 static const struct command_registration target_instance_command_handlers
[] = {
5310 .name
= "configure",
5311 .mode
= COMMAND_CONFIG
,
5312 .jim_handler
= jim_target_configure
,
5313 .help
= "configure a new target for use",
5314 .usage
= "[target_attribute ...]",
5318 .mode
= COMMAND_ANY
,
5319 .jim_handler
= jim_target_configure
,
5320 .help
= "returns the specified target attribute",
5321 .usage
= "target_attribute",
5325 .mode
= COMMAND_EXEC
,
5326 .jim_handler
= jim_target_mw
,
5327 .help
= "Write 32-bit word(s) to target memory",
5328 .usage
= "address data [count]",
5332 .mode
= COMMAND_EXEC
,
5333 .jim_handler
= jim_target_mw
,
5334 .help
= "Write 16-bit half-word(s) to target memory",
5335 .usage
= "address data [count]",
5339 .mode
= COMMAND_EXEC
,
5340 .jim_handler
= jim_target_mw
,
5341 .help
= "Write byte(s) to target memory",
5342 .usage
= "address data [count]",
5346 .mode
= COMMAND_EXEC
,
5347 .jim_handler
= jim_target_md
,
5348 .help
= "Display target memory as 32-bit words",
5349 .usage
= "address [count]",
5353 .mode
= COMMAND_EXEC
,
5354 .jim_handler
= jim_target_md
,
5355 .help
= "Display target memory as 16-bit half-words",
5356 .usage
= "address [count]",
5360 .mode
= COMMAND_EXEC
,
5361 .jim_handler
= jim_target_md
,
5362 .help
= "Display target memory as 8-bit bytes",
5363 .usage
= "address [count]",
5366 .name
= "array2mem",
5367 .mode
= COMMAND_EXEC
,
5368 .jim_handler
= jim_target_array2mem
,
5369 .help
= "Writes Tcl array of 8/16/32 bit numbers "
5371 .usage
= "arrayname bitwidth address count",
5374 .name
= "mem2array",
5375 .mode
= COMMAND_EXEC
,
5376 .jim_handler
= jim_target_mem2array
,
5377 .help
= "Loads Tcl array of 8/16/32 bit numbers "
5378 "from target memory",
5379 .usage
= "arrayname bitwidth address count",
5382 .name
= "eventlist",
5383 .mode
= COMMAND_EXEC
,
5384 .jim_handler
= jim_target_event_list
,
5385 .help
= "displays a table of events defined for this target",
5389 .mode
= COMMAND_EXEC
,
5390 .jim_handler
= jim_target_current_state
,
5391 .help
= "displays the current state of this target",
5394 .name
= "arp_examine",
5395 .mode
= COMMAND_EXEC
,
5396 .jim_handler
= jim_target_examine
,
5397 .help
= "used internally for reset processing",
5398 .usage
= "arp_examine ['allow-defer']",
5401 .name
= "was_examined",
5402 .mode
= COMMAND_EXEC
,
5403 .jim_handler
= jim_target_was_examined
,
5404 .help
= "used internally for reset processing",
5405 .usage
= "was_examined",
5408 .name
= "examine_deferred",
5409 .mode
= COMMAND_EXEC
,
5410 .jim_handler
= jim_target_examine_deferred
,
5411 .help
= "used internally for reset processing",
5412 .usage
= "examine_deferred",
5415 .name
= "arp_halt_gdb",
5416 .mode
= COMMAND_EXEC
,
5417 .jim_handler
= jim_target_halt_gdb
,
5418 .help
= "used internally for reset processing to halt GDB",
5422 .mode
= COMMAND_EXEC
,
5423 .jim_handler
= jim_target_poll
,
5424 .help
= "used internally for reset processing",
5427 .name
= "arp_reset",
5428 .mode
= COMMAND_EXEC
,
5429 .jim_handler
= jim_target_reset
,
5430 .help
= "used internally for reset processing",
5434 .mode
= COMMAND_EXEC
,
5435 .jim_handler
= jim_target_halt
,
5436 .help
= "used internally for reset processing",
5439 .name
= "arp_waitstate",
5440 .mode
= COMMAND_EXEC
,
5441 .jim_handler
= jim_target_wait_state
,
5442 .help
= "used internally for reset processing",
5445 .name
= "invoke-event",
5446 .mode
= COMMAND_EXEC
,
5447 .jim_handler
= jim_target_invoke_event
,
5448 .help
= "invoke handler for specified event",
5449 .usage
= "event_name",
5451 COMMAND_REGISTRATION_DONE
5454 static int target_create(Jim_GetOptInfo
*goi
)
5461 struct target
*target
;
5462 struct command_context
*cmd_ctx
;
5464 cmd_ctx
= current_command_context(goi
->interp
);
5465 assert(cmd_ctx
!= NULL
);
5467 if (goi
->argc
< 3) {
5468 Jim_WrongNumArgs(goi
->interp
, 1, goi
->argv
, "?name? ?type? ..options...");
5473 Jim_GetOpt_Obj(goi
, &new_cmd
);
5474 /* does this command exist? */
5475 cmd
= Jim_GetCommand(goi
->interp
, new_cmd
, JIM_ERRMSG
);
5477 cp
= Jim_GetString(new_cmd
, NULL
);
5478 Jim_SetResultFormatted(goi
->interp
, "Command/target: %s Exists", cp
);
5483 e
= Jim_GetOpt_String(goi
, &cp
, NULL
);
5486 struct transport
*tr
= get_current_transport();
5487 if (tr
->override_target
) {
5488 e
= tr
->override_target(&cp
);
5489 if (e
!= ERROR_OK
) {
5490 LOG_ERROR("The selected transport doesn't support this target");
5493 LOG_INFO("The selected transport took over low-level target control. The results might differ compared to plain JTAG/SWD");
5495 /* now does target type exist */
5496 for (x
= 0 ; target_types
[x
] ; x
++) {
5497 if (0 == strcmp(cp
, target_types
[x
]->name
)) {
5502 /* check for deprecated name */
5503 if (target_types
[x
]->deprecated_name
) {
5504 if (0 == strcmp(cp
, target_types
[x
]->deprecated_name
)) {
5506 LOG_WARNING("target name is deprecated use: \'%s\'", target_types
[x
]->name
);
5511 if (target_types
[x
] == NULL
) {
5512 Jim_SetResultFormatted(goi
->interp
, "Unknown target type %s, try one of ", cp
);
5513 for (x
= 0 ; target_types
[x
] ; x
++) {
5514 if (target_types
[x
+ 1]) {
5515 Jim_AppendStrings(goi
->interp
,
5516 Jim_GetResult(goi
->interp
),
5517 target_types
[x
]->name
,
5520 Jim_AppendStrings(goi
->interp
,
5521 Jim_GetResult(goi
->interp
),
5523 target_types
[x
]->name
, NULL
);
5530 target
= calloc(1, sizeof(struct target
));
5531 /* set target number */
5532 target
->target_number
= new_target_number();
5533 cmd_ctx
->current_target
= target
->target_number
;
5535 /* allocate memory for each unique target type */
5536 target
->type
= calloc(1, sizeof(struct target_type
));
5538 memcpy(target
->type
, target_types
[x
], sizeof(struct target_type
));
5540 /* will be set by "-endian" */
5541 target
->endianness
= TARGET_ENDIAN_UNKNOWN
;
5543 /* default to first core, override with -coreid */
5546 target
->working_area
= 0x0;
5547 target
->working_area_size
= 0x0;
5548 target
->working_areas
= NULL
;
5549 target
->backup_working_area
= 0;
5551 target
->state
= TARGET_UNKNOWN
;
5552 target
->debug_reason
= DBG_REASON_UNDEFINED
;
5553 target
->reg_cache
= NULL
;
5554 target
->breakpoints
= NULL
;
5555 target
->watchpoints
= NULL
;
5556 target
->next
= NULL
;
5557 target
->arch_info
= NULL
;
5559 target
->display
= 1;
5561 target
->halt_issued
= false;
5563 /* initialize trace information */
5564 target
->trace_info
= calloc(1, sizeof(struct trace
));
5566 target
->dbgmsg
= NULL
;
5567 target
->dbg_msg_enabled
= 0;
5569 target
->endianness
= TARGET_ENDIAN_UNKNOWN
;
5571 target
->rtos
= NULL
;
5572 target
->rtos_auto_detect
= false;
5574 /* Do the rest as "configure" options */
5575 goi
->isconfigure
= 1;
5576 e
= target_configure(goi
, target
);
5578 if (target
->tap
== NULL
) {
5579 Jim_SetResultString(goi
->interp
, "-chain-position required when creating target", -1);
5589 if (target
->endianness
== TARGET_ENDIAN_UNKNOWN
) {
5590 /* default endian to little if not specified */
5591 target
->endianness
= TARGET_LITTLE_ENDIAN
;
5594 cp
= Jim_GetString(new_cmd
, NULL
);
5595 target
->cmd_name
= strdup(cp
);
5597 /* create the target specific commands */
5598 if (target
->type
->commands
) {
5599 e
= register_commands(cmd_ctx
, NULL
, target
->type
->commands
);
5601 LOG_ERROR("unable to register '%s' commands", cp
);
5603 if (target
->type
->target_create
)
5604 (*(target
->type
->target_create
))(target
, goi
->interp
);
5606 /* append to end of list */
5608 struct target
**tpp
;
5609 tpp
= &(all_targets
);
5611 tpp
= &((*tpp
)->next
);
5615 /* now - create the new target name command */
5616 const struct command_registration target_subcommands
[] = {
5618 .chain
= target_instance_command_handlers
,
5621 .chain
= target
->type
->commands
,
5623 COMMAND_REGISTRATION_DONE
5625 const struct command_registration target_commands
[] = {
5628 .mode
= COMMAND_ANY
,
5629 .help
= "target command group",
5631 .chain
= target_subcommands
,
5633 COMMAND_REGISTRATION_DONE
5635 e
= register_commands(cmd_ctx
, NULL
, target_commands
);
5639 struct command
*c
= command_find_in_context(cmd_ctx
, cp
);
5641 command_set_handler_data(c
, target
);
5643 return (ERROR_OK
== e
) ? JIM_OK
: JIM_ERR
;
5646 static int jim_target_current(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
5649 Jim_WrongNumArgs(interp
, 1, argv
, "Too many parameters");
5652 struct command_context
*cmd_ctx
= current_command_context(interp
);
5653 assert(cmd_ctx
!= NULL
);
5655 Jim_SetResultString(interp
, target_name(get_current_target(cmd_ctx
)), -1);
5659 static int jim_target_types(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
5662 Jim_WrongNumArgs(interp
, 1, argv
, "Too many parameters");
5665 Jim_SetResult(interp
, Jim_NewListObj(interp
, NULL
, 0));
5666 for (unsigned x
= 0; NULL
!= target_types
[x
]; x
++) {
5667 Jim_ListAppendElement(interp
, Jim_GetResult(interp
),
5668 Jim_NewStringObj(interp
, target_types
[x
]->name
, -1));
5673 static int jim_target_names(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
5676 Jim_WrongNumArgs(interp
, 1, argv
, "Too many parameters");
5679 Jim_SetResult(interp
, Jim_NewListObj(interp
, NULL
, 0));
5680 struct target
*target
= all_targets
;
5682 Jim_ListAppendElement(interp
, Jim_GetResult(interp
),
5683 Jim_NewStringObj(interp
, target_name(target
), -1));
5684 target
= target
->next
;
5689 static int jim_target_smp(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
5692 const char *targetname
;
5694 struct target
*target
= (struct target
*) NULL
;
5695 struct target_list
*head
, *curr
, *new;
5696 curr
= (struct target_list
*) NULL
;
5697 head
= (struct target_list
*) NULL
;
5700 LOG_DEBUG("%d", argc
);
5701 /* argv[1] = target to associate in smp
5702 * argv[2] = target to assoicate in smp
5706 for (i
= 1; i
< argc
; i
++) {
5708 targetname
= Jim_GetString(argv
[i
], &len
);
5709 target
= get_target(targetname
);
5710 LOG_DEBUG("%s ", targetname
);
5712 new = malloc(sizeof(struct target_list
));
5713 new->target
= target
;
5714 new->next
= (struct target_list
*)NULL
;
5715 if (head
== (struct target_list
*)NULL
) {
5724 /* now parse the list of cpu and put the target in smp mode*/
5727 while (curr
!= (struct target_list
*)NULL
) {
5728 target
= curr
->target
;
5730 target
->head
= head
;
5734 if (target
&& target
->rtos
)
5735 retval
= rtos_smp_init(head
->target
);
5741 static int jim_target_create(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
5744 Jim_GetOpt_Setup(&goi
, interp
, argc
- 1, argv
+ 1);
5746 Jim_WrongNumArgs(goi
.interp
, goi
.argc
, goi
.argv
,
5747 "<name> <target_type> [<target_options> ...]");
5750 return target_create(&goi
);
5753 static const struct command_registration target_subcommand_handlers
[] = {
5756 .mode
= COMMAND_CONFIG
,
5757 .handler
= handle_target_init_command
,
5758 .help
= "initialize targets",
5762 /* REVISIT this should be COMMAND_CONFIG ... */
5763 .mode
= COMMAND_ANY
,
5764 .jim_handler
= jim_target_create
,
5765 .usage
= "name type '-chain-position' name [options ...]",
5766 .help
= "Creates and selects a new target",
5770 .mode
= COMMAND_ANY
,
5771 .jim_handler
= jim_target_current
,
5772 .help
= "Returns the currently selected target",
5776 .mode
= COMMAND_ANY
,
5777 .jim_handler
= jim_target_types
,
5778 .help
= "Returns the available target types as "
5779 "a list of strings",
5783 .mode
= COMMAND_ANY
,
5784 .jim_handler
= jim_target_names
,
5785 .help
= "Returns the names of all targets as a list of strings",
5789 .mode
= COMMAND_ANY
,
5790 .jim_handler
= jim_target_smp
,
5791 .usage
= "targetname1 targetname2 ...",
5792 .help
= "gather several target in a smp list"
5795 COMMAND_REGISTRATION_DONE
5799 target_addr_t address
;
5805 static int fastload_num
;
5806 static struct FastLoad
*fastload
;
5808 static void free_fastload(void)
5810 if (fastload
!= NULL
) {
5812 for (i
= 0; i
< fastload_num
; i
++) {
5813 if (fastload
[i
].data
)
5814 free(fastload
[i
].data
);
5821 COMMAND_HANDLER(handle_fast_load_image_command
)
5825 uint32_t image_size
;
5826 target_addr_t min_address
= 0;
5827 target_addr_t max_address
= -1;
5832 int retval
= CALL_COMMAND_HANDLER(parse_load_image_command_CMD_ARGV
,
5833 &image
, &min_address
, &max_address
);
5834 if (ERROR_OK
!= retval
)
5837 struct duration bench
;
5838 duration_start(&bench
);
5840 retval
= image_open(&image
, CMD_ARGV
[0], (CMD_ARGC
>= 3) ? CMD_ARGV
[2] : NULL
);
5841 if (retval
!= ERROR_OK
)
5846 fastload_num
= image
.num_sections
;
5847 fastload
= malloc(sizeof(struct FastLoad
)*image
.num_sections
);
5848 if (fastload
== NULL
) {
5849 command_print(CMD_CTX
, "out of memory");
5850 image_close(&image
);
5853 memset(fastload
, 0, sizeof(struct FastLoad
)*image
.num_sections
);
5854 for (i
= 0; i
< image
.num_sections
; i
++) {
5855 buffer
= malloc(image
.sections
[i
].size
);
5856 if (buffer
== NULL
) {
5857 command_print(CMD_CTX
, "error allocating buffer for section (%d bytes)",
5858 (int)(image
.sections
[i
].size
));
5859 retval
= ERROR_FAIL
;
5863 retval
= image_read_section(&image
, i
, 0x0, image
.sections
[i
].size
, buffer
, &buf_cnt
);
5864 if (retval
!= ERROR_OK
) {
5869 uint32_t offset
= 0;
5870 uint32_t length
= buf_cnt
;
5872 /* DANGER!!! beware of unsigned comparision here!!! */
5874 if ((image
.sections
[i
].base_address
+ buf_cnt
>= min_address
) &&
5875 (image
.sections
[i
].base_address
< max_address
)) {
5876 if (image
.sections
[i
].base_address
< min_address
) {
5877 /* clip addresses below */
5878 offset
+= min_address
-image
.sections
[i
].base_address
;
5882 if (image
.sections
[i
].base_address
+ buf_cnt
> max_address
)
5883 length
-= (image
.sections
[i
].base_address
+ buf_cnt
)-max_address
;
5885 fastload
[i
].address
= image
.sections
[i
].base_address
+ offset
;
5886 fastload
[i
].data
= malloc(length
);
5887 if (fastload
[i
].data
== NULL
) {
5889 command_print(CMD_CTX
, "error allocating buffer for section (%" PRIu32
" bytes)",
5891 retval
= ERROR_FAIL
;
5894 memcpy(fastload
[i
].data
, buffer
+ offset
, length
);
5895 fastload
[i
].length
= length
;
5897 image_size
+= length
;
5898 command_print(CMD_CTX
, "%u bytes written at address 0x%8.8x",
5899 (unsigned int)length
,
5900 ((unsigned int)(image
.sections
[i
].base_address
+ offset
)));
5906 if ((ERROR_OK
== retval
) && (duration_measure(&bench
) == ERROR_OK
)) {
5907 command_print(CMD_CTX
, "Loaded %" PRIu32
" bytes "
5908 "in %fs (%0.3f KiB/s)", image_size
,
5909 duration_elapsed(&bench
), duration_kbps(&bench
, image_size
));
5911 command_print(CMD_CTX
,
5912 "WARNING: image has not been loaded to target!"
5913 "You can issue a 'fast_load' to finish loading.");
5916 image_close(&image
);
5918 if (retval
!= ERROR_OK
)
5924 COMMAND_HANDLER(handle_fast_load_command
)
5927 return ERROR_COMMAND_SYNTAX_ERROR
;
5928 if (fastload
== NULL
) {
5929 LOG_ERROR("No image in memory");
5933 int64_t ms
= timeval_ms();
5935 int retval
= ERROR_OK
;
5936 for (i
= 0; i
< fastload_num
; i
++) {
5937 struct target
*target
= get_current_target(CMD_CTX
);
5938 command_print(CMD_CTX
, "Write to 0x%08x, length 0x%08x",
5939 (unsigned int)(fastload
[i
].address
),
5940 (unsigned int)(fastload
[i
].length
));
5941 retval
= target_write_buffer(target
, fastload
[i
].address
, fastload
[i
].length
, fastload
[i
].data
);
5942 if (retval
!= ERROR_OK
)
5944 size
+= fastload
[i
].length
;
5946 if (retval
== ERROR_OK
) {
5947 int64_t after
= timeval_ms();
5948 command_print(CMD_CTX
, "Loaded image %f kBytes/s", (float)(size
/1024.0)/((float)(after
-ms
)/1000.0));
5953 static const struct command_registration target_command_handlers
[] = {
5956 .handler
= handle_targets_command
,
5957 .mode
= COMMAND_ANY
,
5958 .help
= "change current default target (one parameter) "
5959 "or prints table of all targets (no parameters)",
5960 .usage
= "[target]",
5964 .mode
= COMMAND_CONFIG
,
5965 .help
= "configure target",
5967 .chain
= target_subcommand_handlers
,
5969 COMMAND_REGISTRATION_DONE
5972 int target_register_commands(struct command_context
*cmd_ctx
)
5974 return register_commands(cmd_ctx
, NULL
, target_command_handlers
);
5977 static bool target_reset_nag
= true;
5979 bool get_target_reset_nag(void)
5981 return target_reset_nag
;
5984 COMMAND_HANDLER(handle_target_reset_nag
)
5986 return CALL_COMMAND_HANDLER(handle_command_parse_bool
,
5987 &target_reset_nag
, "Nag after each reset about options to improve "
5991 COMMAND_HANDLER(handle_ps_command
)
5993 struct target
*target
= get_current_target(CMD_CTX
);
5995 if (target
->state
!= TARGET_HALTED
) {
5996 LOG_INFO("target not halted !!");
6000 if ((target
->rtos
) && (target
->rtos
->type
)
6001 && (target
->rtos
->type
->ps_command
)) {
6002 display
= target
->rtos
->type
->ps_command(target
);
6003 command_print(CMD_CTX
, "%s", display
);
6008 return ERROR_TARGET_FAILURE
;
6012 static void binprint(struct command_context
*cmd_ctx
, const char *text
, const uint8_t *buf
, int size
)
6015 command_print_sameline(cmd_ctx
, "%s", text
);
6016 for (int i
= 0; i
< size
; i
++)
6017 command_print_sameline(cmd_ctx
, " %02x", buf
[i
]);
6018 command_print(cmd_ctx
, " ");
6021 COMMAND_HANDLER(handle_test_mem_access_command
)
6023 struct target
*target
= get_current_target(CMD_CTX
);
6025 int retval
= ERROR_OK
;
6027 if (target
->state
!= TARGET_HALTED
) {
6028 LOG_INFO("target not halted !!");
6033 return ERROR_COMMAND_SYNTAX_ERROR
;
6035 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], test_size
);
6038 size_t num_bytes
= test_size
+ 4;
6040 struct working_area
*wa
= NULL
;
6041 retval
= target_alloc_working_area(target
, num_bytes
, &wa
);
6042 if (retval
!= ERROR_OK
) {
6043 LOG_ERROR("Not enough working area");
6047 uint8_t *test_pattern
= malloc(num_bytes
);
6049 for (size_t i
= 0; i
< num_bytes
; i
++)
6050 test_pattern
[i
] = rand();
6052 retval
= target_write_memory(target
, wa
->address
, 1, num_bytes
, test_pattern
);
6053 if (retval
!= ERROR_OK
) {
6054 LOG_ERROR("Test pattern write failed");
6058 for (int host_offset
= 0; host_offset
<= 1; host_offset
++) {
6059 for (int size
= 1; size
<= 4; size
*= 2) {
6060 for (int offset
= 0; offset
< 4; offset
++) {
6061 uint32_t count
= test_size
/ size
;
6062 size_t host_bufsiz
= (count
+ 2) * size
+ host_offset
;
6063 uint8_t *read_ref
= malloc(host_bufsiz
);
6064 uint8_t *read_buf
= malloc(host_bufsiz
);
6066 for (size_t i
= 0; i
< host_bufsiz
; i
++) {
6067 read_ref
[i
] = rand();
6068 read_buf
[i
] = read_ref
[i
];
6070 command_print_sameline(CMD_CTX
,
6071 "Test read %" PRIu32
" x %d @ %d to %saligned buffer: ", count
,
6072 size
, offset
, host_offset
? "un" : "");
6074 struct duration bench
;
6075 duration_start(&bench
);
6077 retval
= target_read_memory(target
, wa
->address
+ offset
, size
, count
,
6078 read_buf
+ size
+ host_offset
);
6080 duration_measure(&bench
);
6082 if (retval
== ERROR_TARGET_UNALIGNED_ACCESS
) {
6083 command_print(CMD_CTX
, "Unsupported alignment");
6085 } else if (retval
!= ERROR_OK
) {
6086 command_print(CMD_CTX
, "Memory read failed");
6090 /* replay on host */
6091 memcpy(read_ref
+ size
+ host_offset
, test_pattern
+ offset
, count
* size
);
6094 int result
= memcmp(read_ref
, read_buf
, host_bufsiz
);
6096 command_print(CMD_CTX
, "Pass in %fs (%0.3f KiB/s)",
6097 duration_elapsed(&bench
),
6098 duration_kbps(&bench
, count
* size
));
6100 command_print(CMD_CTX
, "Compare failed");
6101 binprint(CMD_CTX
, "ref:", read_ref
, host_bufsiz
);
6102 binprint(CMD_CTX
, "buf:", read_buf
, host_bufsiz
);
6115 target_free_working_area(target
, wa
);
6118 num_bytes
= test_size
+ 4 + 4 + 4;
6120 retval
= target_alloc_working_area(target
, num_bytes
, &wa
);
6121 if (retval
!= ERROR_OK
) {
6122 LOG_ERROR("Not enough working area");
6126 test_pattern
= malloc(num_bytes
);
6128 for (size_t i
= 0; i
< num_bytes
; i
++)
6129 test_pattern
[i
] = rand();
6131 for (int host_offset
= 0; host_offset
<= 1; host_offset
++) {
6132 for (int size
= 1; size
<= 4; size
*= 2) {
6133 for (int offset
= 0; offset
< 4; offset
++) {
6134 uint32_t count
= test_size
/ size
;
6135 size_t host_bufsiz
= count
* size
+ host_offset
;
6136 uint8_t *read_ref
= malloc(num_bytes
);
6137 uint8_t *read_buf
= malloc(num_bytes
);
6138 uint8_t *write_buf
= malloc(host_bufsiz
);
6140 for (size_t i
= 0; i
< host_bufsiz
; i
++)
6141 write_buf
[i
] = rand();
6142 command_print_sameline(CMD_CTX
,
6143 "Test write %" PRIu32
" x %d @ %d from %saligned buffer: ", count
,
6144 size
, offset
, host_offset
? "un" : "");
6146 retval
= target_write_memory(target
, wa
->address
, 1, num_bytes
, test_pattern
);
6147 if (retval
!= ERROR_OK
) {
6148 command_print(CMD_CTX
, "Test pattern write failed");
6152 /* replay on host */
6153 memcpy(read_ref
, test_pattern
, num_bytes
);
6154 memcpy(read_ref
+ size
+ offset
, write_buf
+ host_offset
, count
* size
);
6156 struct duration bench
;
6157 duration_start(&bench
);
6159 retval
= target_write_memory(target
, wa
->address
+ size
+ offset
, size
, count
,
6160 write_buf
+ host_offset
);
6162 duration_measure(&bench
);
6164 if (retval
== ERROR_TARGET_UNALIGNED_ACCESS
) {
6165 command_print(CMD_CTX
, "Unsupported alignment");
6167 } else if (retval
!= ERROR_OK
) {
6168 command_print(CMD_CTX
, "Memory write failed");
6173 retval
= target_read_memory(target
, wa
->address
, 1, num_bytes
, read_buf
);
6174 if (retval
!= ERROR_OK
) {
6175 command_print(CMD_CTX
, "Test pattern write failed");
6180 int result
= memcmp(read_ref
, read_buf
, num_bytes
);
6182 command_print(CMD_CTX
, "Pass in %fs (%0.3f KiB/s)",
6183 duration_elapsed(&bench
),
6184 duration_kbps(&bench
, count
* size
));
6186 command_print(CMD_CTX
, "Compare failed");
6187 binprint(CMD_CTX
, "ref:", read_ref
, num_bytes
);
6188 binprint(CMD_CTX
, "buf:", read_buf
, num_bytes
);
6200 target_free_working_area(target
, wa
);
6204 static const struct command_registration target_exec_command_handlers
[] = {
6206 .name
= "fast_load_image",
6207 .handler
= handle_fast_load_image_command
,
6208 .mode
= COMMAND_ANY
,
6209 .help
= "Load image into server memory for later use by "
6210 "fast_load; primarily for profiling",
6211 .usage
= "filename address ['bin'|'ihex'|'elf'|'s19'] "
6212 "[min_address [max_length]]",
6215 .name
= "fast_load",
6216 .handler
= handle_fast_load_command
,
6217 .mode
= COMMAND_EXEC
,
6218 .help
= "loads active fast load image to current target "
6219 "- mainly for profiling purposes",
6224 .handler
= handle_profile_command
,
6225 .mode
= COMMAND_EXEC
,
6226 .usage
= "seconds filename [start end]",
6227 .help
= "profiling samples the CPU PC",
6229 /** @todo don't register virt2phys() unless target supports it */
6231 .name
= "virt2phys",
6232 .handler
= handle_virt2phys_command
,
6233 .mode
= COMMAND_ANY
,
6234 .help
= "translate a virtual address into a physical address",
6235 .usage
= "virtual_address",
6239 .handler
= handle_reg_command
,
6240 .mode
= COMMAND_EXEC
,
6241 .help
= "display (reread from target with \"force\") or set a register; "
6242 "with no arguments, displays all registers and their values",
6243 .usage
= "[(register_number|register_name) [(value|'force')]]",
6247 .handler
= handle_poll_command
,
6248 .mode
= COMMAND_EXEC
,
6249 .help
= "poll target state; or reconfigure background polling",
6250 .usage
= "['on'|'off']",
6253 .name
= "wait_halt",
6254 .handler
= handle_wait_halt_command
,
6255 .mode
= COMMAND_EXEC
,
6256 .help
= "wait up to the specified number of milliseconds "
6257 "(default 5000) for a previously requested halt",
6258 .usage
= "[milliseconds]",
6262 .handler
= handle_halt_command
,
6263 .mode
= COMMAND_EXEC
,
6264 .help
= "request target to halt, then wait up to the specified"
6265 "number of milliseconds (default 5000) for it to complete",
6266 .usage
= "[milliseconds]",
6270 .handler
= handle_resume_command
,
6271 .mode
= COMMAND_EXEC
,
6272 .help
= "resume target execution from current PC or address",
6273 .usage
= "[address]",
6277 .handler
= handle_reset_command
,
6278 .mode
= COMMAND_EXEC
,
6279 .usage
= "[run|halt|init]",
6280 .help
= "Reset all targets into the specified mode."
6281 "Default reset mode is run, if not given.",
6284 .name
= "soft_reset_halt",
6285 .handler
= handle_soft_reset_halt_command
,
6286 .mode
= COMMAND_EXEC
,
6288 .help
= "halt the target and do a soft reset",
6292 .handler
= handle_step_command
,
6293 .mode
= COMMAND_EXEC
,
6294 .help
= "step one instruction from current PC or address",
6295 .usage
= "[address]",
6299 .handler
= handle_md_command
,
6300 .mode
= COMMAND_EXEC
,
6301 .help
= "display memory words",
6302 .usage
= "['phys'] address [count]",
6306 .handler
= handle_md_command
,
6307 .mode
= COMMAND_EXEC
,
6308 .help
= "display memory words",
6309 .usage
= "['phys'] address [count]",
6313 .handler
= handle_md_command
,
6314 .mode
= COMMAND_EXEC
,
6315 .help
= "display memory half-words",
6316 .usage
= "['phys'] address [count]",
6320 .handler
= handle_md_command
,
6321 .mode
= COMMAND_EXEC
,
6322 .help
= "display memory bytes",
6323 .usage
= "['phys'] address [count]",
6327 .handler
= handle_mw_command
,
6328 .mode
= COMMAND_EXEC
,
6329 .help
= "write memory word",
6330 .usage
= "['phys'] address value [count]",
6334 .handler
= handle_mw_command
,
6335 .mode
= COMMAND_EXEC
,
6336 .help
= "write memory word",
6337 .usage
= "['phys'] address value [count]",
6341 .handler
= handle_mw_command
,
6342 .mode
= COMMAND_EXEC
,
6343 .help
= "write memory half-word",
6344 .usage
= "['phys'] address value [count]",
6348 .handler
= handle_mw_command
,
6349 .mode
= COMMAND_EXEC
,
6350 .help
= "write memory byte",
6351 .usage
= "['phys'] address value [count]",
6355 .handler
= handle_bp_command
,
6356 .mode
= COMMAND_EXEC
,
6357 .help
= "list or set hardware or software breakpoint",
6358 .usage
= "<address> [<asid>]<length> ['hw'|'hw_ctx']",
6362 .handler
= handle_rbp_command
,
6363 .mode
= COMMAND_EXEC
,
6364 .help
= "remove breakpoint",
6369 .handler
= handle_wp_command
,
6370 .mode
= COMMAND_EXEC
,
6371 .help
= "list (no params) or create watchpoints",
6372 .usage
= "[address length [('r'|'w'|'a') value [mask]]]",
6376 .handler
= handle_rwp_command
,
6377 .mode
= COMMAND_EXEC
,
6378 .help
= "remove watchpoint",
6382 .name
= "load_image",
6383 .handler
= handle_load_image_command
,
6384 .mode
= COMMAND_EXEC
,
6385 .usage
= "filename address ['bin'|'ihex'|'elf'|'s19'] "
6386 "[min_address] [max_length]",
6389 .name
= "dump_image",
6390 .handler
= handle_dump_image_command
,
6391 .mode
= COMMAND_EXEC
,
6392 .usage
= "filename address size",
6395 .name
= "verify_image_checksum",
6396 .handler
= handle_verify_image_checksum_command
,
6397 .mode
= COMMAND_EXEC
,
6398 .usage
= "filename [offset [type]]",
6401 .name
= "verify_image",
6402 .handler
= handle_verify_image_command
,
6403 .mode
= COMMAND_EXEC
,
6404 .usage
= "filename [offset [type]]",
6407 .name
= "test_image",
6408 .handler
= handle_test_image_command
,
6409 .mode
= COMMAND_EXEC
,
6410 .usage
= "filename [offset [type]]",
6413 .name
= "mem2array",
6414 .mode
= COMMAND_EXEC
,
6415 .jim_handler
= jim_mem2array
,
6416 .help
= "read 8/16/32 bit memory and return as a TCL array "
6417 "for script processing",
6418 .usage
= "arrayname bitwidth address count",
6421 .name
= "array2mem",
6422 .mode
= COMMAND_EXEC
,
6423 .jim_handler
= jim_array2mem
,
6424 .help
= "convert a TCL array to memory locations "
6425 "and write the 8/16/32 bit values",
6426 .usage
= "arrayname bitwidth address count",
6429 .name
= "reset_nag",
6430 .handler
= handle_target_reset_nag
,
6431 .mode
= COMMAND_ANY
,
6432 .help
= "Nag after each reset about options that could have been "
6433 "enabled to improve performance. ",
6434 .usage
= "['enable'|'disable']",
6438 .handler
= handle_ps_command
,
6439 .mode
= COMMAND_EXEC
,
6440 .help
= "list all tasks ",
6444 .name
= "test_mem_access",
6445 .handler
= handle_test_mem_access_command
,
6446 .mode
= COMMAND_EXEC
,
6447 .help
= "Test the target's memory access functions",
6451 COMMAND_REGISTRATION_DONE
6453 static int target_register_user_commands(struct command_context
*cmd_ctx
)
6455 int retval
= ERROR_OK
;
6456 retval
= target_request_register_commands(cmd_ctx
);
6457 if (retval
!= ERROR_OK
)
6460 retval
= trace_register_commands(cmd_ctx
);
6461 if (retval
!= ERROR_OK
)
6465 return register_commands(cmd_ctx
, NULL
, target_exec_command_handlers
);