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"
59 /* default halt wait timeout (ms) */
60 #define DEFAULT_HALT_TIMEOUT 5000
62 static int target_read_buffer_default(struct target
*target
, target_addr_t address
,
63 uint32_t count
, uint8_t *buffer
);
64 static int target_write_buffer_default(struct target
*target
, target_addr_t address
,
65 uint32_t count
, const uint8_t *buffer
);
66 static int target_array2mem(Jim_Interp
*interp
, struct target
*target
,
67 int argc
, Jim_Obj
* const *argv
);
68 static int target_mem2array(Jim_Interp
*interp
, struct target
*target
,
69 int argc
, Jim_Obj
* const *argv
);
70 static int target_register_user_commands(struct command_context
*cmd_ctx
);
71 static int target_get_gdb_fileio_info_default(struct target
*target
,
72 struct gdb_fileio_info
*fileio_info
);
73 static int target_gdb_fileio_end_default(struct target
*target
, int retcode
,
74 int fileio_errno
, bool ctrl_c
);
75 static int target_profiling_default(struct target
*target
, uint32_t *samples
,
76 uint32_t max_num_samples
, uint32_t *num_samples
, uint32_t seconds
);
79 extern struct target_type arm7tdmi_target
;
80 extern struct target_type arm720t_target
;
81 extern struct target_type arm9tdmi_target
;
82 extern struct target_type arm920t_target
;
83 extern struct target_type arm966e_target
;
84 extern struct target_type arm946e_target
;
85 extern struct target_type arm926ejs_target
;
86 extern struct target_type fa526_target
;
87 extern struct target_type feroceon_target
;
88 extern struct target_type dragonite_target
;
89 extern struct target_type xscale_target
;
90 extern struct target_type cortexm_target
;
91 extern struct target_type cortexa_target
;
92 extern struct target_type aarch64_target
;
93 extern struct target_type cortexr4_target
;
94 extern struct target_type arm11_target
;
95 extern struct target_type ls1_sap_target
;
96 extern struct target_type mips_m4k_target
;
97 extern struct target_type avr_target
;
98 extern struct target_type dsp563xx_target
;
99 extern struct target_type dsp5680xx_target
;
100 extern struct target_type testee_target
;
101 extern struct target_type avr32_ap7k_target
;
102 extern struct target_type hla_target
;
103 extern struct target_type nds32_v2_target
;
104 extern struct target_type nds32_v3_target
;
105 extern struct target_type nds32_v3m_target
;
106 extern struct target_type or1k_target
;
107 extern struct target_type quark_x10xx_target
;
108 extern struct target_type quark_d20xx_target
;
109 extern struct target_type stm8_target
;
111 static struct target_type
*target_types
[] = {
148 struct target
*all_targets
;
149 static struct target_event_callback
*target_event_callbacks
;
150 static struct target_timer_callback
*target_timer_callbacks
;
151 LIST_HEAD(target_reset_callback_list
);
152 LIST_HEAD(target_trace_callback_list
);
153 static const int polling_interval
= 100;
155 static const Jim_Nvp nvp_assert
[] = {
156 { .name
= "assert", NVP_ASSERT
},
157 { .name
= "deassert", NVP_DEASSERT
},
158 { .name
= "T", NVP_ASSERT
},
159 { .name
= "F", NVP_DEASSERT
},
160 { .name
= "t", NVP_ASSERT
},
161 { .name
= "f", NVP_DEASSERT
},
162 { .name
= NULL
, .value
= -1 }
165 static const Jim_Nvp nvp_error_target
[] = {
166 { .value
= ERROR_TARGET_INVALID
, .name
= "err-invalid" },
167 { .value
= ERROR_TARGET_INIT_FAILED
, .name
= "err-init-failed" },
168 { .value
= ERROR_TARGET_TIMEOUT
, .name
= "err-timeout" },
169 { .value
= ERROR_TARGET_NOT_HALTED
, .name
= "err-not-halted" },
170 { .value
= ERROR_TARGET_FAILURE
, .name
= "err-failure" },
171 { .value
= ERROR_TARGET_UNALIGNED_ACCESS
, .name
= "err-unaligned-access" },
172 { .value
= ERROR_TARGET_DATA_ABORT
, .name
= "err-data-abort" },
173 { .value
= ERROR_TARGET_RESOURCE_NOT_AVAILABLE
, .name
= "err-resource-not-available" },
174 { .value
= ERROR_TARGET_TRANSLATION_FAULT
, .name
= "err-translation-fault" },
175 { .value
= ERROR_TARGET_NOT_RUNNING
, .name
= "err-not-running" },
176 { .value
= ERROR_TARGET_NOT_EXAMINED
, .name
= "err-not-examined" },
177 { .value
= -1, .name
= NULL
}
180 static const char *target_strerror_safe(int err
)
184 n
= Jim_Nvp_value2name_simple(nvp_error_target
, err
);
191 static const Jim_Nvp nvp_target_event
[] = {
193 { .value
= TARGET_EVENT_GDB_HALT
, .name
= "gdb-halt" },
194 { .value
= TARGET_EVENT_HALTED
, .name
= "halted" },
195 { .value
= TARGET_EVENT_RESUMED
, .name
= "resumed" },
196 { .value
= TARGET_EVENT_RESUME_START
, .name
= "resume-start" },
197 { .value
= TARGET_EVENT_RESUME_END
, .name
= "resume-end" },
199 { .name
= "gdb-start", .value
= TARGET_EVENT_GDB_START
},
200 { .name
= "gdb-end", .value
= TARGET_EVENT_GDB_END
},
202 { .value
= TARGET_EVENT_RESET_START
, .name
= "reset-start" },
203 { .value
= TARGET_EVENT_RESET_ASSERT_PRE
, .name
= "reset-assert-pre" },
204 { .value
= TARGET_EVENT_RESET_ASSERT
, .name
= "reset-assert" },
205 { .value
= TARGET_EVENT_RESET_ASSERT_POST
, .name
= "reset-assert-post" },
206 { .value
= TARGET_EVENT_RESET_DEASSERT_PRE
, .name
= "reset-deassert-pre" },
207 { .value
= TARGET_EVENT_RESET_DEASSERT_POST
, .name
= "reset-deassert-post" },
208 { .value
= TARGET_EVENT_RESET_INIT
, .name
= "reset-init" },
209 { .value
= TARGET_EVENT_RESET_END
, .name
= "reset-end" },
211 { .value
= TARGET_EVENT_EXAMINE_START
, .name
= "examine-start" },
212 { .value
= TARGET_EVENT_EXAMINE_END
, .name
= "examine-end" },
214 { .value
= TARGET_EVENT_DEBUG_HALTED
, .name
= "debug-halted" },
215 { .value
= TARGET_EVENT_DEBUG_RESUMED
, .name
= "debug-resumed" },
217 { .value
= TARGET_EVENT_GDB_ATTACH
, .name
= "gdb-attach" },
218 { .value
= TARGET_EVENT_GDB_DETACH
, .name
= "gdb-detach" },
220 { .value
= TARGET_EVENT_GDB_FLASH_WRITE_START
, .name
= "gdb-flash-write-start" },
221 { .value
= TARGET_EVENT_GDB_FLASH_WRITE_END
, .name
= "gdb-flash-write-end" },
223 { .value
= TARGET_EVENT_GDB_FLASH_ERASE_START
, .name
= "gdb-flash-erase-start" },
224 { .value
= TARGET_EVENT_GDB_FLASH_ERASE_END
, .name
= "gdb-flash-erase-end" },
226 { .value
= TARGET_EVENT_TRACE_CONFIG
, .name
= "trace-config" },
228 { .name
= NULL
, .value
= -1 }
231 static const Jim_Nvp nvp_target_state
[] = {
232 { .name
= "unknown", .value
= TARGET_UNKNOWN
},
233 { .name
= "running", .value
= TARGET_RUNNING
},
234 { .name
= "halted", .value
= TARGET_HALTED
},
235 { .name
= "reset", .value
= TARGET_RESET
},
236 { .name
= "debug-running", .value
= TARGET_DEBUG_RUNNING
},
237 { .name
= NULL
, .value
= -1 },
240 static const Jim_Nvp nvp_target_debug_reason
[] = {
241 { .name
= "debug-request" , .value
= DBG_REASON_DBGRQ
},
242 { .name
= "breakpoint" , .value
= DBG_REASON_BREAKPOINT
},
243 { .name
= "watchpoint" , .value
= DBG_REASON_WATCHPOINT
},
244 { .name
= "watchpoint-and-breakpoint", .value
= DBG_REASON_WPTANDBKPT
},
245 { .name
= "single-step" , .value
= DBG_REASON_SINGLESTEP
},
246 { .name
= "target-not-halted" , .value
= DBG_REASON_NOTHALTED
},
247 { .name
= "program-exit" , .value
= DBG_REASON_EXIT
},
248 { .name
= "undefined" , .value
= DBG_REASON_UNDEFINED
},
249 { .name
= NULL
, .value
= -1 },
252 static const Jim_Nvp nvp_target_endian
[] = {
253 { .name
= "big", .value
= TARGET_BIG_ENDIAN
},
254 { .name
= "little", .value
= TARGET_LITTLE_ENDIAN
},
255 { .name
= "be", .value
= TARGET_BIG_ENDIAN
},
256 { .name
= "le", .value
= TARGET_LITTLE_ENDIAN
},
257 { .name
= NULL
, .value
= -1 },
260 static const Jim_Nvp nvp_reset_modes
[] = {
261 { .name
= "unknown", .value
= RESET_UNKNOWN
},
262 { .name
= "run" , .value
= RESET_RUN
},
263 { .name
= "halt" , .value
= RESET_HALT
},
264 { .name
= "init" , .value
= RESET_INIT
},
265 { .name
= NULL
, .value
= -1 },
268 const char *debug_reason_name(struct target
*t
)
272 cp
= Jim_Nvp_value2name_simple(nvp_target_debug_reason
,
273 t
->debug_reason
)->name
;
275 LOG_ERROR("Invalid debug reason: %d", (int)(t
->debug_reason
));
276 cp
= "(*BUG*unknown*BUG*)";
281 const char *target_state_name(struct target
*t
)
284 cp
= Jim_Nvp_value2name_simple(nvp_target_state
, t
->state
)->name
;
286 LOG_ERROR("Invalid target state: %d", (int)(t
->state
));
287 cp
= "(*BUG*unknown*BUG*)";
290 if (!target_was_examined(t
) && t
->defer_examine
)
291 cp
= "examine deferred";
296 const char *target_event_name(enum target_event event
)
299 cp
= Jim_Nvp_value2name_simple(nvp_target_event
, event
)->name
;
301 LOG_ERROR("Invalid target event: %d", (int)(event
));
302 cp
= "(*BUG*unknown*BUG*)";
307 const char *target_reset_mode_name(enum target_reset_mode reset_mode
)
310 cp
= Jim_Nvp_value2name_simple(nvp_reset_modes
, reset_mode
)->name
;
312 LOG_ERROR("Invalid target reset mode: %d", (int)(reset_mode
));
313 cp
= "(*BUG*unknown*BUG*)";
318 /* determine the number of the new target */
319 static int new_target_number(void)
324 /* number is 0 based */
328 if (x
< t
->target_number
)
329 x
= t
->target_number
;
335 /* read a uint64_t from a buffer in target memory endianness */
336 uint64_t target_buffer_get_u64(struct target
*target
, const uint8_t *buffer
)
338 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
339 return le_to_h_u64(buffer
);
341 return be_to_h_u64(buffer
);
344 /* read a uint32_t from a buffer in target memory endianness */
345 uint32_t target_buffer_get_u32(struct target
*target
, const uint8_t *buffer
)
347 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
348 return le_to_h_u32(buffer
);
350 return be_to_h_u32(buffer
);
353 /* read a uint24_t from a buffer in target memory endianness */
354 uint32_t target_buffer_get_u24(struct target
*target
, const uint8_t *buffer
)
356 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
357 return le_to_h_u24(buffer
);
359 return be_to_h_u24(buffer
);
362 /* read a uint16_t from a buffer in target memory endianness */
363 uint16_t target_buffer_get_u16(struct target
*target
, const uint8_t *buffer
)
365 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
366 return le_to_h_u16(buffer
);
368 return be_to_h_u16(buffer
);
371 /* read a uint8_t from a buffer in target memory endianness */
372 static uint8_t target_buffer_get_u8(struct target
*target
, const uint8_t *buffer
)
374 return *buffer
& 0x0ff;
377 /* write a uint64_t to a buffer in target memory endianness */
378 void target_buffer_set_u64(struct target
*target
, uint8_t *buffer
, uint64_t value
)
380 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
381 h_u64_to_le(buffer
, value
);
383 h_u64_to_be(buffer
, value
);
386 /* write a uint32_t to a buffer in target memory endianness */
387 void target_buffer_set_u32(struct target
*target
, uint8_t *buffer
, uint32_t value
)
389 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
390 h_u32_to_le(buffer
, value
);
392 h_u32_to_be(buffer
, value
);
395 /* write a uint24_t to a buffer in target memory endianness */
396 void target_buffer_set_u24(struct target
*target
, uint8_t *buffer
, uint32_t value
)
398 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
399 h_u24_to_le(buffer
, value
);
401 h_u24_to_be(buffer
, value
);
404 /* write a uint16_t to a buffer in target memory endianness */
405 void target_buffer_set_u16(struct target
*target
, uint8_t *buffer
, uint16_t value
)
407 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
408 h_u16_to_le(buffer
, value
);
410 h_u16_to_be(buffer
, value
);
413 /* write a uint8_t to a buffer in target memory endianness */
414 static void target_buffer_set_u8(struct target
*target
, uint8_t *buffer
, uint8_t value
)
419 /* write a uint64_t array to a buffer in target memory endianness */
420 void target_buffer_get_u64_array(struct target
*target
, const uint8_t *buffer
, uint32_t count
, uint64_t *dstbuf
)
423 for (i
= 0; i
< count
; i
++)
424 dstbuf
[i
] = target_buffer_get_u64(target
, &buffer
[i
* 8]);
427 /* write a uint32_t array to a buffer in target memory endianness */
428 void target_buffer_get_u32_array(struct target
*target
, const uint8_t *buffer
, uint32_t count
, uint32_t *dstbuf
)
431 for (i
= 0; i
< count
; i
++)
432 dstbuf
[i
] = target_buffer_get_u32(target
, &buffer
[i
* 4]);
435 /* write a uint16_t array to a buffer in target memory endianness */
436 void target_buffer_get_u16_array(struct target
*target
, const uint8_t *buffer
, uint32_t count
, uint16_t *dstbuf
)
439 for (i
= 0; i
< count
; i
++)
440 dstbuf
[i
] = target_buffer_get_u16(target
, &buffer
[i
* 2]);
443 /* write a uint64_t array to a buffer in target memory endianness */
444 void target_buffer_set_u64_array(struct target
*target
, uint8_t *buffer
, uint32_t count
, const uint64_t *srcbuf
)
447 for (i
= 0; i
< count
; i
++)
448 target_buffer_set_u64(target
, &buffer
[i
* 8], srcbuf
[i
]);
451 /* write a uint32_t array to a buffer in target memory endianness */
452 void target_buffer_set_u32_array(struct target
*target
, uint8_t *buffer
, uint32_t count
, const uint32_t *srcbuf
)
455 for (i
= 0; i
< count
; i
++)
456 target_buffer_set_u32(target
, &buffer
[i
* 4], srcbuf
[i
]);
459 /* write a uint16_t array to a buffer in target memory endianness */
460 void target_buffer_set_u16_array(struct target
*target
, uint8_t *buffer
, uint32_t count
, const uint16_t *srcbuf
)
463 for (i
= 0; i
< count
; i
++)
464 target_buffer_set_u16(target
, &buffer
[i
* 2], srcbuf
[i
]);
467 /* return a pointer to a configured target; id is name or number */
468 struct target
*get_target(const char *id
)
470 struct target
*target
;
472 /* try as tcltarget name */
473 for (target
= all_targets
; target
; target
= target
->next
) {
474 if (target_name(target
) == NULL
)
476 if (strcmp(id
, target_name(target
)) == 0)
480 /* It's OK to remove this fallback sometime after August 2010 or so */
482 /* no match, try as number */
484 if (parse_uint(id
, &num
) != ERROR_OK
)
487 for (target
= all_targets
; target
; target
= target
->next
) {
488 if (target
->target_number
== (int)num
) {
489 LOG_WARNING("use '%s' as target identifier, not '%u'",
490 target_name(target
), num
);
498 /* returns a pointer to the n-th configured target */
499 struct target
*get_target_by_num(int num
)
501 struct target
*target
= all_targets
;
504 if (target
->target_number
== num
)
506 target
= target
->next
;
512 struct target
*get_current_target(struct command_context
*cmd_ctx
)
514 struct target
*target
= cmd_ctx
->current_target_override
515 ? cmd_ctx
->current_target_override
516 : cmd_ctx
->current_target
;
518 if (target
== NULL
) {
519 LOG_ERROR("BUG: current_target out of bounds");
526 int target_poll(struct target
*target
)
530 /* We can't poll until after examine */
531 if (!target_was_examined(target
)) {
532 /* Fail silently lest we pollute the log */
536 retval
= target
->type
->poll(target
);
537 if (retval
!= ERROR_OK
)
540 if (target
->halt_issued
) {
541 if (target
->state
== TARGET_HALTED
)
542 target
->halt_issued
= false;
544 int64_t t
= timeval_ms() - target
->halt_issued_time
;
545 if (t
> DEFAULT_HALT_TIMEOUT
) {
546 target
->halt_issued
= false;
547 LOG_INFO("Halt timed out, wake up GDB.");
548 target_call_event_callbacks(target
, TARGET_EVENT_GDB_HALT
);
556 int target_halt(struct target
*target
)
559 /* We can't poll until after examine */
560 if (!target_was_examined(target
)) {
561 LOG_ERROR("Target not examined yet");
565 retval
= target
->type
->halt(target
);
566 if (retval
!= ERROR_OK
)
569 target
->halt_issued
= true;
570 target
->halt_issued_time
= timeval_ms();
576 * Make the target (re)start executing using its saved execution
577 * context (possibly with some modifications).
579 * @param target Which target should start executing.
580 * @param current True to use the target's saved program counter instead
581 * of the address parameter
582 * @param address Optionally used as the program counter.
583 * @param handle_breakpoints True iff breakpoints at the resumption PC
584 * should be skipped. (For example, maybe execution was stopped by
585 * such a breakpoint, in which case it would be counterprodutive to
587 * @param debug_execution False if all working areas allocated by OpenOCD
588 * should be released and/or restored to their original contents.
589 * (This would for example be true to run some downloaded "helper"
590 * algorithm code, which resides in one such working buffer and uses
591 * another for data storage.)
593 * @todo Resolve the ambiguity about what the "debug_execution" flag
594 * signifies. For example, Target implementations don't agree on how
595 * it relates to invalidation of the register cache, or to whether
596 * breakpoints and watchpoints should be enabled. (It would seem wrong
597 * to enable breakpoints when running downloaded "helper" algorithms
598 * (debug_execution true), since the breakpoints would be set to match
599 * target firmware being debugged, not the helper algorithm.... and
600 * enabling them could cause such helpers to malfunction (for example,
601 * by overwriting data with a breakpoint instruction. On the other
602 * hand the infrastructure for running such helpers might use this
603 * procedure but rely on hardware breakpoint to detect termination.)
605 int target_resume(struct target
*target
, int current
, target_addr_t address
,
606 int handle_breakpoints
, int debug_execution
)
610 /* We can't poll until after examine */
611 if (!target_was_examined(target
)) {
612 LOG_ERROR("Target not examined yet");
616 target_call_event_callbacks(target
, TARGET_EVENT_RESUME_START
);
618 /* note that resume *must* be asynchronous. The CPU can halt before
619 * we poll. The CPU can even halt at the current PC as a result of
620 * a software breakpoint being inserted by (a bug?) the application.
622 retval
= target
->type
->resume(target
, current
, address
, handle_breakpoints
, debug_execution
);
623 if (retval
!= ERROR_OK
)
626 target_call_event_callbacks(target
, TARGET_EVENT_RESUME_END
);
631 static int target_process_reset(struct command_context
*cmd_ctx
, enum target_reset_mode reset_mode
)
636 n
= Jim_Nvp_value2name_simple(nvp_reset_modes
, reset_mode
);
637 if (n
->name
== NULL
) {
638 LOG_ERROR("invalid reset mode");
642 struct target
*target
;
643 for (target
= all_targets
; target
; target
= target
->next
)
644 target_call_reset_callbacks(target
, reset_mode
);
646 /* disable polling during reset to make reset event scripts
647 * more predictable, i.e. dr/irscan & pathmove in events will
648 * not have JTAG operations injected into the middle of a sequence.
650 bool save_poll
= jtag_poll_get_enabled();
652 jtag_poll_set_enabled(false);
654 sprintf(buf
, "ocd_process_reset %s", n
->name
);
655 retval
= Jim_Eval(cmd_ctx
->interp
, buf
);
657 jtag_poll_set_enabled(save_poll
);
659 if (retval
!= JIM_OK
) {
660 Jim_MakeErrorMessage(cmd_ctx
->interp
);
661 command_print(NULL
, "%s\n", Jim_GetString(Jim_GetResult(cmd_ctx
->interp
), NULL
));
665 /* We want any events to be processed before the prompt */
666 retval
= target_call_timer_callbacks_now();
668 for (target
= all_targets
; target
; target
= target
->next
) {
669 target
->type
->check_reset(target
);
670 target
->running_alg
= false;
676 static int identity_virt2phys(struct target
*target
,
677 target_addr_t
virtual, target_addr_t
*physical
)
683 static int no_mmu(struct target
*target
, int *enabled
)
689 static int default_examine(struct target
*target
)
691 target_set_examined(target
);
695 /* no check by default */
696 static int default_check_reset(struct target
*target
)
701 int target_examine_one(struct target
*target
)
703 target_call_event_callbacks(target
, TARGET_EVENT_EXAMINE_START
);
705 int retval
= target
->type
->examine(target
);
706 if (retval
!= ERROR_OK
)
709 target_call_event_callbacks(target
, TARGET_EVENT_EXAMINE_END
);
714 static int jtag_enable_callback(enum jtag_event event
, void *priv
)
716 struct target
*target
= priv
;
718 if (event
!= JTAG_TAP_EVENT_ENABLE
|| !target
->tap
->enabled
)
721 jtag_unregister_event_callback(jtag_enable_callback
, target
);
723 return target_examine_one(target
);
726 /* Targets that correctly implement init + examine, i.e.
727 * no communication with target during init:
731 int target_examine(void)
733 int retval
= ERROR_OK
;
734 struct target
*target
;
736 for (target
= all_targets
; target
; target
= target
->next
) {
737 /* defer examination, but don't skip it */
738 if (!target
->tap
->enabled
) {
739 jtag_register_event_callback(jtag_enable_callback
,
744 if (target
->defer_examine
)
747 retval
= target_examine_one(target
);
748 if (retval
!= ERROR_OK
)
754 const char *target_type_name(struct target
*target
)
756 return target
->type
->name
;
759 static int target_soft_reset_halt(struct target
*target
)
761 if (!target_was_examined(target
)) {
762 LOG_ERROR("Target not examined yet");
765 if (!target
->type
->soft_reset_halt
) {
766 LOG_ERROR("Target %s does not support soft_reset_halt",
767 target_name(target
));
770 return target
->type
->soft_reset_halt(target
);
774 * Downloads a target-specific native code algorithm to the target,
775 * and executes it. * Note that some targets may need to set up, enable,
776 * and tear down a breakpoint (hard or * soft) to detect algorithm
777 * termination, while others may support lower overhead schemes where
778 * soft breakpoints embedded in the algorithm automatically terminate the
781 * @param target used to run the algorithm
782 * @param arch_info target-specific description of the algorithm.
784 int target_run_algorithm(struct target
*target
,
785 int num_mem_params
, struct mem_param
*mem_params
,
786 int num_reg_params
, struct reg_param
*reg_param
,
787 uint32_t entry_point
, uint32_t exit_point
,
788 int timeout_ms
, void *arch_info
)
790 int retval
= ERROR_FAIL
;
792 if (!target_was_examined(target
)) {
793 LOG_ERROR("Target not examined yet");
796 if (!target
->type
->run_algorithm
) {
797 LOG_ERROR("Target type '%s' does not support %s",
798 target_type_name(target
), __func__
);
802 target
->running_alg
= true;
803 retval
= target
->type
->run_algorithm(target
,
804 num_mem_params
, mem_params
,
805 num_reg_params
, reg_param
,
806 entry_point
, exit_point
, timeout_ms
, arch_info
);
807 target
->running_alg
= false;
814 * Executes a target-specific native code algorithm and leaves it running.
816 * @param target used to run the algorithm
817 * @param arch_info target-specific description of the algorithm.
819 int target_start_algorithm(struct target
*target
,
820 int num_mem_params
, struct mem_param
*mem_params
,
821 int num_reg_params
, struct reg_param
*reg_params
,
822 uint32_t entry_point
, uint32_t exit_point
,
825 int retval
= ERROR_FAIL
;
827 if (!target_was_examined(target
)) {
828 LOG_ERROR("Target not examined yet");
831 if (!target
->type
->start_algorithm
) {
832 LOG_ERROR("Target type '%s' does not support %s",
833 target_type_name(target
), __func__
);
836 if (target
->running_alg
) {
837 LOG_ERROR("Target is already running an algorithm");
841 target
->running_alg
= true;
842 retval
= target
->type
->start_algorithm(target
,
843 num_mem_params
, mem_params
,
844 num_reg_params
, reg_params
,
845 entry_point
, exit_point
, arch_info
);
852 * Waits for an algorithm started with target_start_algorithm() to complete.
854 * @param target used to run the algorithm
855 * @param arch_info target-specific description of the algorithm.
857 int target_wait_algorithm(struct target
*target
,
858 int num_mem_params
, struct mem_param
*mem_params
,
859 int num_reg_params
, struct reg_param
*reg_params
,
860 uint32_t exit_point
, int timeout_ms
,
863 int retval
= ERROR_FAIL
;
865 if (!target
->type
->wait_algorithm
) {
866 LOG_ERROR("Target type '%s' does not support %s",
867 target_type_name(target
), __func__
);
870 if (!target
->running_alg
) {
871 LOG_ERROR("Target is not running an algorithm");
875 retval
= target
->type
->wait_algorithm(target
,
876 num_mem_params
, mem_params
,
877 num_reg_params
, reg_params
,
878 exit_point
, timeout_ms
, arch_info
);
879 if (retval
!= ERROR_TARGET_TIMEOUT
)
880 target
->running_alg
= false;
887 * Streams data to a circular buffer on target intended for consumption by code
888 * running asynchronously on target.
890 * This is intended for applications where target-specific native code runs
891 * on the target, receives data from the circular buffer, does something with
892 * it (most likely writing it to a flash memory), and advances the circular
895 * This assumes that the helper algorithm has already been loaded to the target,
896 * but has not been started yet. Given memory and register parameters are passed
899 * The buffer is defined by (buffer_start, buffer_size) arguments and has the
902 * [buffer_start + 0, buffer_start + 4):
903 * Write Pointer address (aka head). Written and updated by this
904 * routine when new data is written to the circular buffer.
905 * [buffer_start + 4, buffer_start + 8):
906 * Read Pointer address (aka tail). Updated by code running on the
907 * target after it consumes data.
908 * [buffer_start + 8, buffer_start + buffer_size):
909 * Circular buffer contents.
911 * See contrib/loaders/flash/stm32f1x.S for an example.
913 * @param target used to run the algorithm
914 * @param buffer address on the host where data to be sent is located
915 * @param count number of blocks to send
916 * @param block_size size in bytes of each block
917 * @param num_mem_params count of memory-based params to pass to algorithm
918 * @param mem_params memory-based params to pass to algorithm
919 * @param num_reg_params count of register-based params to pass to algorithm
920 * @param reg_params memory-based params to pass to algorithm
921 * @param buffer_start address on the target of the circular buffer structure
922 * @param buffer_size size of the circular buffer structure
923 * @param entry_point address on the target to execute to start the algorithm
924 * @param exit_point address at which to set a breakpoint to catch the
925 * end of the algorithm; can be 0 if target triggers a breakpoint itself
928 int target_run_flash_async_algorithm(struct target
*target
,
929 const uint8_t *buffer
, uint32_t count
, int block_size
,
930 int num_mem_params
, struct mem_param
*mem_params
,
931 int num_reg_params
, struct reg_param
*reg_params
,
932 uint32_t buffer_start
, uint32_t buffer_size
,
933 uint32_t entry_point
, uint32_t exit_point
, void *arch_info
)
938 const uint8_t *buffer_orig
= buffer
;
940 /* Set up working area. First word is write pointer, second word is read pointer,
941 * rest is fifo data area. */
942 uint32_t wp_addr
= buffer_start
;
943 uint32_t rp_addr
= buffer_start
+ 4;
944 uint32_t fifo_start_addr
= buffer_start
+ 8;
945 uint32_t fifo_end_addr
= buffer_start
+ buffer_size
;
947 uint32_t wp
= fifo_start_addr
;
948 uint32_t rp
= fifo_start_addr
;
950 /* validate block_size is 2^n */
951 assert(!block_size
|| !(block_size
& (block_size
- 1)));
953 retval
= target_write_u32(target
, wp_addr
, wp
);
954 if (retval
!= ERROR_OK
)
956 retval
= target_write_u32(target
, rp_addr
, rp
);
957 if (retval
!= ERROR_OK
)
960 /* Start up algorithm on target and let it idle while writing the first chunk */
961 retval
= target_start_algorithm(target
, num_mem_params
, mem_params
,
962 num_reg_params
, reg_params
,
967 if (retval
!= ERROR_OK
) {
968 LOG_ERROR("error starting target flash write algorithm");
974 retval
= target_read_u32(target
, rp_addr
, &rp
);
975 if (retval
!= ERROR_OK
) {
976 LOG_ERROR("failed to get read pointer");
980 LOG_DEBUG("offs 0x%zx count 0x%" PRIx32
" wp 0x%" PRIx32
" rp 0x%" PRIx32
,
981 (size_t) (buffer
- buffer_orig
), count
, wp
, rp
);
984 LOG_ERROR("flash write algorithm aborted by target");
985 retval
= ERROR_FLASH_OPERATION_FAILED
;
989 if (((rp
- fifo_start_addr
) & (block_size
- 1)) || rp
< fifo_start_addr
|| rp
>= fifo_end_addr
) {
990 LOG_ERROR("corrupted fifo read pointer 0x%" PRIx32
, rp
);
994 /* Count the number of bytes available in the fifo without
995 * crossing the wrap around. Make sure to not fill it completely,
996 * because that would make wp == rp and that's the empty condition. */
997 uint32_t thisrun_bytes
;
999 thisrun_bytes
= rp
- wp
- block_size
;
1000 else if (rp
> fifo_start_addr
)
1001 thisrun_bytes
= fifo_end_addr
- wp
;
1003 thisrun_bytes
= fifo_end_addr
- wp
- block_size
;
1005 if (thisrun_bytes
== 0) {
1006 /* Throttle polling a bit if transfer is (much) faster than flash
1007 * programming. The exact delay shouldn't matter as long as it's
1008 * less than buffer size / flash speed. This is very unlikely to
1009 * run when using high latency connections such as USB. */
1012 /* to stop an infinite loop on some targets check and increment a timeout
1013 * this issue was observed on a stellaris using the new ICDI interface */
1014 if (timeout
++ >= 500) {
1015 LOG_ERROR("timeout waiting for algorithm, a target reset is recommended");
1016 return ERROR_FLASH_OPERATION_FAILED
;
1021 /* reset our timeout */
1024 /* Limit to the amount of data we actually want to write */
1025 if (thisrun_bytes
> count
* block_size
)
1026 thisrun_bytes
= count
* block_size
;
1028 /* Write data to fifo */
1029 retval
= target_write_buffer(target
, wp
, thisrun_bytes
, buffer
);
1030 if (retval
!= ERROR_OK
)
1033 /* Update counters and wrap write pointer */
1034 buffer
+= thisrun_bytes
;
1035 count
-= thisrun_bytes
/ block_size
;
1036 wp
+= thisrun_bytes
;
1037 if (wp
>= fifo_end_addr
)
1038 wp
= fifo_start_addr
;
1040 /* Store updated write pointer to target */
1041 retval
= target_write_u32(target
, wp_addr
, wp
);
1042 if (retval
!= ERROR_OK
)
1046 if (retval
!= ERROR_OK
) {
1047 /* abort flash write algorithm on target */
1048 target_write_u32(target
, wp_addr
, 0);
1051 int retval2
= target_wait_algorithm(target
, num_mem_params
, mem_params
,
1052 num_reg_params
, reg_params
,
1057 if (retval2
!= ERROR_OK
) {
1058 LOG_ERROR("error waiting for target flash write algorithm");
1062 if (retval
== ERROR_OK
) {
1063 /* check if algorithm set rp = 0 after fifo writer loop finished */
1064 retval
= target_read_u32(target
, rp_addr
, &rp
);
1065 if (retval
== ERROR_OK
&& rp
== 0) {
1066 LOG_ERROR("flash write algorithm aborted by target");
1067 retval
= ERROR_FLASH_OPERATION_FAILED
;
1074 int target_read_memory(struct target
*target
,
1075 target_addr_t address
, uint32_t size
, uint32_t count
, uint8_t *buffer
)
1077 if (!target_was_examined(target
)) {
1078 LOG_ERROR("Target not examined yet");
1081 if (!target
->type
->read_memory
) {
1082 LOG_ERROR("Target %s doesn't support read_memory", target_name(target
));
1085 return target
->type
->read_memory(target
, address
, size
, count
, buffer
);
1088 int target_read_phys_memory(struct target
*target
,
1089 target_addr_t address
, uint32_t size
, uint32_t count
, uint8_t *buffer
)
1091 if (!target_was_examined(target
)) {
1092 LOG_ERROR("Target not examined yet");
1095 if (!target
->type
->read_phys_memory
) {
1096 LOG_ERROR("Target %s doesn't support read_phys_memory", target_name(target
));
1099 return target
->type
->read_phys_memory(target
, address
, size
, count
, buffer
);
1102 int target_write_memory(struct target
*target
,
1103 target_addr_t address
, uint32_t size
, uint32_t count
, const uint8_t *buffer
)
1105 if (!target_was_examined(target
)) {
1106 LOG_ERROR("Target not examined yet");
1109 if (!target
->type
->write_memory
) {
1110 LOG_ERROR("Target %s doesn't support write_memory", target_name(target
));
1113 return target
->type
->write_memory(target
, address
, size
, count
, buffer
);
1116 int target_write_phys_memory(struct target
*target
,
1117 target_addr_t address
, uint32_t size
, uint32_t count
, const uint8_t *buffer
)
1119 if (!target_was_examined(target
)) {
1120 LOG_ERROR("Target not examined yet");
1123 if (!target
->type
->write_phys_memory
) {
1124 LOG_ERROR("Target %s doesn't support write_phys_memory", target_name(target
));
1127 return target
->type
->write_phys_memory(target
, address
, size
, count
, buffer
);
1130 int target_add_breakpoint(struct target
*target
,
1131 struct breakpoint
*breakpoint
)
1133 if ((target
->state
!= TARGET_HALTED
) && (breakpoint
->type
!= BKPT_HARD
)) {
1134 LOG_WARNING("target %s is not halted (add breakpoint)", target_name(target
));
1135 return ERROR_TARGET_NOT_HALTED
;
1137 return target
->type
->add_breakpoint(target
, breakpoint
);
1140 int target_add_context_breakpoint(struct target
*target
,
1141 struct breakpoint
*breakpoint
)
1143 if (target
->state
!= TARGET_HALTED
) {
1144 LOG_WARNING("target %s is not halted (add context breakpoint)", target_name(target
));
1145 return ERROR_TARGET_NOT_HALTED
;
1147 return target
->type
->add_context_breakpoint(target
, breakpoint
);
1150 int target_add_hybrid_breakpoint(struct target
*target
,
1151 struct breakpoint
*breakpoint
)
1153 if (target
->state
!= TARGET_HALTED
) {
1154 LOG_WARNING("target %s is not halted (add hybrid breakpoint)", target_name(target
));
1155 return ERROR_TARGET_NOT_HALTED
;
1157 return target
->type
->add_hybrid_breakpoint(target
, breakpoint
);
1160 int target_remove_breakpoint(struct target
*target
,
1161 struct breakpoint
*breakpoint
)
1163 return target
->type
->remove_breakpoint(target
, breakpoint
);
1166 int target_add_watchpoint(struct target
*target
,
1167 struct watchpoint
*watchpoint
)
1169 if (target
->state
!= TARGET_HALTED
) {
1170 LOG_WARNING("target %s is not halted (add watchpoint)", target_name(target
));
1171 return ERROR_TARGET_NOT_HALTED
;
1173 return target
->type
->add_watchpoint(target
, watchpoint
);
1175 int target_remove_watchpoint(struct target
*target
,
1176 struct watchpoint
*watchpoint
)
1178 return target
->type
->remove_watchpoint(target
, watchpoint
);
1180 int target_hit_watchpoint(struct target
*target
,
1181 struct watchpoint
**hit_watchpoint
)
1183 if (target
->state
!= TARGET_HALTED
) {
1184 LOG_WARNING("target %s is not halted (hit watchpoint)", target
->cmd_name
);
1185 return ERROR_TARGET_NOT_HALTED
;
1188 if (target
->type
->hit_watchpoint
== NULL
) {
1189 /* For backward compatible, if hit_watchpoint is not implemented,
1190 * return ERROR_FAIL such that gdb_server will not take the nonsense
1195 return target
->type
->hit_watchpoint(target
, hit_watchpoint
);
1198 int target_get_gdb_reg_list(struct target
*target
,
1199 struct reg
**reg_list
[], int *reg_list_size
,
1200 enum target_register_class reg_class
)
1202 return target
->type
->get_gdb_reg_list(target
, reg_list
, reg_list_size
, reg_class
);
1204 int target_step(struct target
*target
,
1205 int current
, target_addr_t address
, int handle_breakpoints
)
1207 return target
->type
->step(target
, current
, address
, handle_breakpoints
);
1210 int target_get_gdb_fileio_info(struct target
*target
, struct gdb_fileio_info
*fileio_info
)
1212 if (target
->state
!= TARGET_HALTED
) {
1213 LOG_WARNING("target %s is not halted (gdb fileio)", target
->cmd_name
);
1214 return ERROR_TARGET_NOT_HALTED
;
1216 return target
->type
->get_gdb_fileio_info(target
, fileio_info
);
1219 int target_gdb_fileio_end(struct target
*target
, int retcode
, int fileio_errno
, bool ctrl_c
)
1221 if (target
->state
!= TARGET_HALTED
) {
1222 LOG_WARNING("target %s is not halted (gdb fileio end)", target
->cmd_name
);
1223 return ERROR_TARGET_NOT_HALTED
;
1225 return target
->type
->gdb_fileio_end(target
, retcode
, fileio_errno
, ctrl_c
);
1228 int target_profiling(struct target
*target
, uint32_t *samples
,
1229 uint32_t max_num_samples
, uint32_t *num_samples
, uint32_t seconds
)
1231 if (target
->state
!= TARGET_HALTED
) {
1232 LOG_WARNING("target %s is not halted (profiling)", target
->cmd_name
);
1233 return ERROR_TARGET_NOT_HALTED
;
1235 return target
->type
->profiling(target
, samples
, max_num_samples
,
1236 num_samples
, seconds
);
1240 * Reset the @c examined flag for the given target.
1241 * Pure paranoia -- targets are zeroed on allocation.
1243 static void target_reset_examined(struct target
*target
)
1245 target
->examined
= false;
1248 static int handle_target(void *priv
);
1250 static int target_init_one(struct command_context
*cmd_ctx
,
1251 struct target
*target
)
1253 target_reset_examined(target
);
1255 struct target_type
*type
= target
->type
;
1256 if (type
->examine
== NULL
)
1257 type
->examine
= default_examine
;
1259 if (type
->check_reset
== NULL
)
1260 type
->check_reset
= default_check_reset
;
1262 assert(type
->init_target
!= NULL
);
1264 int retval
= type
->init_target(cmd_ctx
, target
);
1265 if (ERROR_OK
!= retval
) {
1266 LOG_ERROR("target '%s' init failed", target_name(target
));
1270 /* Sanity-check MMU support ... stub in what we must, to help
1271 * implement it in stages, but warn if we need to do so.
1274 if (type
->virt2phys
== NULL
) {
1275 LOG_ERROR("type '%s' is missing virt2phys", type
->name
);
1276 type
->virt2phys
= identity_virt2phys
;
1279 /* Make sure no-MMU targets all behave the same: make no
1280 * distinction between physical and virtual addresses, and
1281 * ensure that virt2phys() is always an identity mapping.
1283 if (type
->write_phys_memory
|| type
->read_phys_memory
|| type
->virt2phys
)
1284 LOG_WARNING("type '%s' has bad MMU hooks", type
->name
);
1287 type
->write_phys_memory
= type
->write_memory
;
1288 type
->read_phys_memory
= type
->read_memory
;
1289 type
->virt2phys
= identity_virt2phys
;
1292 if (target
->type
->read_buffer
== NULL
)
1293 target
->type
->read_buffer
= target_read_buffer_default
;
1295 if (target
->type
->write_buffer
== NULL
)
1296 target
->type
->write_buffer
= target_write_buffer_default
;
1298 if (target
->type
->get_gdb_fileio_info
== NULL
)
1299 target
->type
->get_gdb_fileio_info
= target_get_gdb_fileio_info_default
;
1301 if (target
->type
->gdb_fileio_end
== NULL
)
1302 target
->type
->gdb_fileio_end
= target_gdb_fileio_end_default
;
1304 if (target
->type
->profiling
== NULL
)
1305 target
->type
->profiling
= target_profiling_default
;
1310 static int target_init(struct command_context
*cmd_ctx
)
1312 struct target
*target
;
1315 for (target
= all_targets
; target
; target
= target
->next
) {
1316 retval
= target_init_one(cmd_ctx
, target
);
1317 if (ERROR_OK
!= retval
)
1324 retval
= target_register_user_commands(cmd_ctx
);
1325 if (ERROR_OK
!= retval
)
1328 retval
= target_register_timer_callback(&handle_target
,
1329 polling_interval
, 1, cmd_ctx
->interp
);
1330 if (ERROR_OK
!= retval
)
1336 COMMAND_HANDLER(handle_target_init_command
)
1341 return ERROR_COMMAND_SYNTAX_ERROR
;
1343 static bool target_initialized
;
1344 if (target_initialized
) {
1345 LOG_INFO("'target init' has already been called");
1348 target_initialized
= true;
1350 retval
= command_run_line(CMD_CTX
, "init_targets");
1351 if (ERROR_OK
!= retval
)
1354 retval
= command_run_line(CMD_CTX
, "init_target_events");
1355 if (ERROR_OK
!= retval
)
1358 retval
= command_run_line(CMD_CTX
, "init_board");
1359 if (ERROR_OK
!= retval
)
1362 LOG_DEBUG("Initializing targets...");
1363 return target_init(CMD_CTX
);
1366 int target_register_event_callback(int (*callback
)(struct target
*target
,
1367 enum target_event event
, void *priv
), void *priv
)
1369 struct target_event_callback
**callbacks_p
= &target_event_callbacks
;
1371 if (callback
== NULL
)
1372 return ERROR_COMMAND_SYNTAX_ERROR
;
1375 while ((*callbacks_p
)->next
)
1376 callbacks_p
= &((*callbacks_p
)->next
);
1377 callbacks_p
= &((*callbacks_p
)->next
);
1380 (*callbacks_p
) = malloc(sizeof(struct target_event_callback
));
1381 (*callbacks_p
)->callback
= callback
;
1382 (*callbacks_p
)->priv
= priv
;
1383 (*callbacks_p
)->next
= NULL
;
1388 int target_register_reset_callback(int (*callback
)(struct target
*target
,
1389 enum target_reset_mode reset_mode
, void *priv
), void *priv
)
1391 struct target_reset_callback
*entry
;
1393 if (callback
== NULL
)
1394 return ERROR_COMMAND_SYNTAX_ERROR
;
1396 entry
= malloc(sizeof(struct target_reset_callback
));
1397 if (entry
== NULL
) {
1398 LOG_ERROR("error allocating buffer for reset callback entry");
1399 return ERROR_COMMAND_SYNTAX_ERROR
;
1402 entry
->callback
= callback
;
1404 list_add(&entry
->list
, &target_reset_callback_list
);
1410 int target_register_trace_callback(int (*callback
)(struct target
*target
,
1411 size_t len
, uint8_t *data
, void *priv
), void *priv
)
1413 struct target_trace_callback
*entry
;
1415 if (callback
== NULL
)
1416 return ERROR_COMMAND_SYNTAX_ERROR
;
1418 entry
= malloc(sizeof(struct target_trace_callback
));
1419 if (entry
== NULL
) {
1420 LOG_ERROR("error allocating buffer for trace callback entry");
1421 return ERROR_COMMAND_SYNTAX_ERROR
;
1424 entry
->callback
= callback
;
1426 list_add(&entry
->list
, &target_trace_callback_list
);
1432 int target_register_timer_callback(int (*callback
)(void *priv
), int time_ms
, int periodic
, void *priv
)
1434 struct target_timer_callback
**callbacks_p
= &target_timer_callbacks
;
1436 if (callback
== NULL
)
1437 return ERROR_COMMAND_SYNTAX_ERROR
;
1440 while ((*callbacks_p
)->next
)
1441 callbacks_p
= &((*callbacks_p
)->next
);
1442 callbacks_p
= &((*callbacks_p
)->next
);
1445 (*callbacks_p
) = malloc(sizeof(struct target_timer_callback
));
1446 (*callbacks_p
)->callback
= callback
;
1447 (*callbacks_p
)->periodic
= periodic
;
1448 (*callbacks_p
)->time_ms
= time_ms
;
1449 (*callbacks_p
)->removed
= false;
1451 gettimeofday(&(*callbacks_p
)->when
, NULL
);
1452 timeval_add_time(&(*callbacks_p
)->when
, 0, time_ms
* 1000);
1454 (*callbacks_p
)->priv
= priv
;
1455 (*callbacks_p
)->next
= NULL
;
1460 int target_unregister_event_callback(int (*callback
)(struct target
*target
,
1461 enum target_event event
, void *priv
), void *priv
)
1463 struct target_event_callback
**p
= &target_event_callbacks
;
1464 struct target_event_callback
*c
= target_event_callbacks
;
1466 if (callback
== NULL
)
1467 return ERROR_COMMAND_SYNTAX_ERROR
;
1470 struct target_event_callback
*next
= c
->next
;
1471 if ((c
->callback
== callback
) && (c
->priv
== priv
)) {
1483 int target_unregister_reset_callback(int (*callback
)(struct target
*target
,
1484 enum target_reset_mode reset_mode
, void *priv
), void *priv
)
1486 struct target_reset_callback
*entry
;
1488 if (callback
== NULL
)
1489 return ERROR_COMMAND_SYNTAX_ERROR
;
1491 list_for_each_entry(entry
, &target_reset_callback_list
, list
) {
1492 if (entry
->callback
== callback
&& entry
->priv
== priv
) {
1493 list_del(&entry
->list
);
1502 int target_unregister_trace_callback(int (*callback
)(struct target
*target
,
1503 size_t len
, uint8_t *data
, void *priv
), void *priv
)
1505 struct target_trace_callback
*entry
;
1507 if (callback
== NULL
)
1508 return ERROR_COMMAND_SYNTAX_ERROR
;
1510 list_for_each_entry(entry
, &target_trace_callback_list
, list
) {
1511 if (entry
->callback
== callback
&& entry
->priv
== priv
) {
1512 list_del(&entry
->list
);
1521 int target_unregister_timer_callback(int (*callback
)(void *priv
), void *priv
)
1523 if (callback
== NULL
)
1524 return ERROR_COMMAND_SYNTAX_ERROR
;
1526 for (struct target_timer_callback
*c
= target_timer_callbacks
;
1528 if ((c
->callback
== callback
) && (c
->priv
== priv
)) {
1537 int target_call_event_callbacks(struct target
*target
, enum target_event event
)
1539 struct target_event_callback
*callback
= target_event_callbacks
;
1540 struct target_event_callback
*next_callback
;
1542 if (event
== TARGET_EVENT_HALTED
) {
1543 /* execute early halted first */
1544 target_call_event_callbacks(target
, TARGET_EVENT_GDB_HALT
);
1547 LOG_DEBUG("target event %i (%s)", event
,
1548 Jim_Nvp_value2name_simple(nvp_target_event
, event
)->name
);
1550 target_handle_event(target
, event
);
1553 next_callback
= callback
->next
;
1554 callback
->callback(target
, event
, callback
->priv
);
1555 callback
= next_callback
;
1561 int target_call_reset_callbacks(struct target
*target
, enum target_reset_mode reset_mode
)
1563 struct target_reset_callback
*callback
;
1565 LOG_DEBUG("target reset %i (%s)", reset_mode
,
1566 Jim_Nvp_value2name_simple(nvp_reset_modes
, reset_mode
)->name
);
1568 list_for_each_entry(callback
, &target_reset_callback_list
, list
)
1569 callback
->callback(target
, reset_mode
, callback
->priv
);
1574 int target_call_trace_callbacks(struct target
*target
, size_t len
, uint8_t *data
)
1576 struct target_trace_callback
*callback
;
1578 list_for_each_entry(callback
, &target_trace_callback_list
, list
)
1579 callback
->callback(target
, len
, data
, callback
->priv
);
1584 static int target_timer_callback_periodic_restart(
1585 struct target_timer_callback
*cb
, struct timeval
*now
)
1588 timeval_add_time(&cb
->when
, 0, cb
->time_ms
* 1000L);
1592 static int target_call_timer_callback(struct target_timer_callback
*cb
,
1593 struct timeval
*now
)
1595 cb
->callback(cb
->priv
);
1598 return target_timer_callback_periodic_restart(cb
, now
);
1600 return target_unregister_timer_callback(cb
->callback
, cb
->priv
);
1603 static int target_call_timer_callbacks_check_time(int checktime
)
1605 static bool callback_processing
;
1607 /* Do not allow nesting */
1608 if (callback_processing
)
1611 callback_processing
= true;
1616 gettimeofday(&now
, NULL
);
1618 /* Store an address of the place containing a pointer to the
1619 * next item; initially, that's a standalone "root of the
1620 * list" variable. */
1621 struct target_timer_callback
**callback
= &target_timer_callbacks
;
1623 if ((*callback
)->removed
) {
1624 struct target_timer_callback
*p
= *callback
;
1625 *callback
= (*callback
)->next
;
1630 bool call_it
= (*callback
)->callback
&&
1631 ((!checktime
&& (*callback
)->periodic
) ||
1632 timeval_compare(&now
, &(*callback
)->when
) >= 0);
1635 target_call_timer_callback(*callback
, &now
);
1637 callback
= &(*callback
)->next
;
1640 callback_processing
= false;
1644 int target_call_timer_callbacks(void)
1646 return target_call_timer_callbacks_check_time(1);
1649 /* invoke periodic callbacks immediately */
1650 int target_call_timer_callbacks_now(void)
1652 return target_call_timer_callbacks_check_time(0);
1655 /* Prints the working area layout for debug purposes */
1656 static void print_wa_layout(struct target
*target
)
1658 struct working_area
*c
= target
->working_areas
;
1661 LOG_DEBUG("%c%c " TARGET_ADDR_FMT
"-" TARGET_ADDR_FMT
" (%" PRIu32
" bytes)",
1662 c
->backup
? 'b' : ' ', c
->free
? ' ' : '*',
1663 c
->address
, c
->address
+ c
->size
- 1, c
->size
);
1668 /* Reduce area to size bytes, create a new free area from the remaining bytes, if any. */
1669 static void target_split_working_area(struct working_area
*area
, uint32_t size
)
1671 assert(area
->free
); /* Shouldn't split an allocated area */
1672 assert(size
<= area
->size
); /* Caller should guarantee this */
1674 /* Split only if not already the right size */
1675 if (size
< area
->size
) {
1676 struct working_area
*new_wa
= malloc(sizeof(*new_wa
));
1681 new_wa
->next
= area
->next
;
1682 new_wa
->size
= area
->size
- size
;
1683 new_wa
->address
= area
->address
+ size
;
1684 new_wa
->backup
= NULL
;
1685 new_wa
->user
= NULL
;
1686 new_wa
->free
= true;
1688 area
->next
= new_wa
;
1691 /* If backup memory was allocated to this area, it has the wrong size
1692 * now so free it and it will be reallocated if/when needed */
1695 area
->backup
= NULL
;
1700 /* Merge all adjacent free areas into one */
1701 static void target_merge_working_areas(struct target
*target
)
1703 struct working_area
*c
= target
->working_areas
;
1705 while (c
&& c
->next
) {
1706 assert(c
->next
->address
== c
->address
+ c
->size
); /* This is an invariant */
1708 /* Find two adjacent free areas */
1709 if (c
->free
&& c
->next
->free
) {
1710 /* Merge the last into the first */
1711 c
->size
+= c
->next
->size
;
1713 /* Remove the last */
1714 struct working_area
*to_be_freed
= c
->next
;
1715 c
->next
= c
->next
->next
;
1716 if (to_be_freed
->backup
)
1717 free(to_be_freed
->backup
);
1720 /* If backup memory was allocated to the remaining area, it's has
1721 * the wrong size now */
1732 int target_alloc_working_area_try(struct target
*target
, uint32_t size
, struct working_area
**area
)
1734 /* Reevaluate working area address based on MMU state*/
1735 if (target
->working_areas
== NULL
) {
1739 retval
= target
->type
->mmu(target
, &enabled
);
1740 if (retval
!= ERROR_OK
)
1744 if (target
->working_area_phys_spec
) {
1745 LOG_DEBUG("MMU disabled, using physical "
1746 "address for working memory " TARGET_ADDR_FMT
,
1747 target
->working_area_phys
);
1748 target
->working_area
= target
->working_area_phys
;
1750 LOG_ERROR("No working memory available. "
1751 "Specify -work-area-phys to target.");
1752 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
1755 if (target
->working_area_virt_spec
) {
1756 LOG_DEBUG("MMU enabled, using virtual "
1757 "address for working memory " TARGET_ADDR_FMT
,
1758 target
->working_area_virt
);
1759 target
->working_area
= target
->working_area_virt
;
1761 LOG_ERROR("No working memory available. "
1762 "Specify -work-area-virt to target.");
1763 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
1767 /* Set up initial working area on first call */
1768 struct working_area
*new_wa
= malloc(sizeof(*new_wa
));
1770 new_wa
->next
= NULL
;
1771 new_wa
->size
= target
->working_area_size
& ~3UL; /* 4-byte align */
1772 new_wa
->address
= target
->working_area
;
1773 new_wa
->backup
= NULL
;
1774 new_wa
->user
= NULL
;
1775 new_wa
->free
= true;
1778 target
->working_areas
= new_wa
;
1781 /* only allocate multiples of 4 byte */
1783 size
= (size
+ 3) & (~3UL);
1785 struct working_area
*c
= target
->working_areas
;
1787 /* Find the first large enough working area */
1789 if (c
->free
&& c
->size
>= size
)
1795 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
1797 /* Split the working area into the requested size */
1798 target_split_working_area(c
, size
);
1800 LOG_DEBUG("allocated new working area of %" PRIu32
" bytes at address " TARGET_ADDR_FMT
,
1803 if (target
->backup_working_area
) {
1804 if (c
->backup
== NULL
) {
1805 c
->backup
= malloc(c
->size
);
1806 if (c
->backup
== NULL
)
1810 int retval
= target_read_memory(target
, c
->address
, 4, c
->size
/ 4, c
->backup
);
1811 if (retval
!= ERROR_OK
)
1815 /* mark as used, and return the new (reused) area */
1822 print_wa_layout(target
);
1827 int target_alloc_working_area(struct target
*target
, uint32_t size
, struct working_area
**area
)
1831 retval
= target_alloc_working_area_try(target
, size
, area
);
1832 if (retval
== ERROR_TARGET_RESOURCE_NOT_AVAILABLE
)
1833 LOG_WARNING("not enough working area available(requested %"PRIu32
")", size
);
1838 static int target_restore_working_area(struct target
*target
, struct working_area
*area
)
1840 int retval
= ERROR_OK
;
1842 if (target
->backup_working_area
&& area
->backup
!= NULL
) {
1843 retval
= target_write_memory(target
, area
->address
, 4, area
->size
/ 4, area
->backup
);
1844 if (retval
!= ERROR_OK
)
1845 LOG_ERROR("failed to restore %" PRIu32
" bytes of working area at address " TARGET_ADDR_FMT
,
1846 area
->size
, area
->address
);
1852 /* Restore the area's backup memory, if any, and return the area to the allocation pool */
1853 static int target_free_working_area_restore(struct target
*target
, struct working_area
*area
, int restore
)
1855 int retval
= ERROR_OK
;
1861 retval
= target_restore_working_area(target
, area
);
1862 /* REVISIT: Perhaps the area should be freed even if restoring fails. */
1863 if (retval
!= ERROR_OK
)
1869 LOG_DEBUG("freed %" PRIu32
" bytes of working area at address " TARGET_ADDR_FMT
,
1870 area
->size
, area
->address
);
1872 /* mark user pointer invalid */
1873 /* TODO: Is this really safe? It points to some previous caller's memory.
1874 * How could we know that the area pointer is still in that place and not
1875 * some other vital data? What's the purpose of this, anyway? */
1879 target_merge_working_areas(target
);
1881 print_wa_layout(target
);
1886 int target_free_working_area(struct target
*target
, struct working_area
*area
)
1888 return target_free_working_area_restore(target
, area
, 1);
1891 static void target_destroy(struct target
*target
)
1893 if (target
->type
->deinit_target
)
1894 target
->type
->deinit_target(target
);
1896 struct target_event_action
*teap
= target
->event_action
;
1898 struct target_event_action
*next
= teap
->next
;
1899 Jim_DecrRefCount(teap
->interp
, teap
->body
);
1904 target_free_all_working_areas(target
);
1905 /* Now we have none or only one working area marked as free */
1906 if (target
->working_areas
) {
1907 free(target
->working_areas
->backup
);
1908 free(target
->working_areas
);
1912 free(target
->trace_info
);
1913 free(target
->fileio_info
);
1914 free(target
->cmd_name
);
1918 void target_quit(void)
1920 struct target_event_callback
*pe
= target_event_callbacks
;
1922 struct target_event_callback
*t
= pe
->next
;
1926 target_event_callbacks
= NULL
;
1928 struct target_timer_callback
*pt
= target_timer_callbacks
;
1930 struct target_timer_callback
*t
= pt
->next
;
1934 target_timer_callbacks
= NULL
;
1936 for (struct target
*target
= all_targets
; target
;) {
1940 target_destroy(target
);
1947 /* free resources and restore memory, if restoring memory fails,
1948 * free up resources anyway
1950 static void target_free_all_working_areas_restore(struct target
*target
, int restore
)
1952 struct working_area
*c
= target
->working_areas
;
1954 LOG_DEBUG("freeing all working areas");
1956 /* Loop through all areas, restoring the allocated ones and marking them as free */
1960 target_restore_working_area(target
, c
);
1962 *c
->user
= NULL
; /* Same as above */
1968 /* Run a merge pass to combine all areas into one */
1969 target_merge_working_areas(target
);
1971 print_wa_layout(target
);
1974 void target_free_all_working_areas(struct target
*target
)
1976 target_free_all_working_areas_restore(target
, 1);
1979 /* Find the largest number of bytes that can be allocated */
1980 uint32_t target_get_working_area_avail(struct target
*target
)
1982 struct working_area
*c
= target
->working_areas
;
1983 uint32_t max_size
= 0;
1986 return target
->working_area_size
;
1989 if (c
->free
&& max_size
< c
->size
)
1998 int target_arch_state(struct target
*target
)
2001 if (target
== NULL
) {
2002 LOG_WARNING("No target has been configured");
2006 if (target
->state
!= TARGET_HALTED
)
2009 retval
= target
->type
->arch_state(target
);
2013 static int target_get_gdb_fileio_info_default(struct target
*target
,
2014 struct gdb_fileio_info
*fileio_info
)
2016 /* If target does not support semi-hosting function, target
2017 has no need to provide .get_gdb_fileio_info callback.
2018 It just return ERROR_FAIL and gdb_server will return "Txx"
2019 as target halted every time. */
2023 static int target_gdb_fileio_end_default(struct target
*target
,
2024 int retcode
, int fileio_errno
, bool ctrl_c
)
2029 static int target_profiling_default(struct target
*target
, uint32_t *samples
,
2030 uint32_t max_num_samples
, uint32_t *num_samples
, uint32_t seconds
)
2032 struct timeval timeout
, now
;
2034 gettimeofday(&timeout
, NULL
);
2035 timeval_add_time(&timeout
, seconds
, 0);
2037 LOG_INFO("Starting profiling. Halting and resuming the"
2038 " target as often as we can...");
2040 uint32_t sample_count
= 0;
2041 /* hopefully it is safe to cache! We want to stop/restart as quickly as possible. */
2042 struct reg
*reg
= register_get_by_name(target
->reg_cache
, "pc", 1);
2044 int retval
= ERROR_OK
;
2046 target_poll(target
);
2047 if (target
->state
== TARGET_HALTED
) {
2048 uint32_t t
= buf_get_u32(reg
->value
, 0, 32);
2049 samples
[sample_count
++] = t
;
2050 /* current pc, addr = 0, do not handle breakpoints, not debugging */
2051 retval
= target_resume(target
, 1, 0, 0, 0);
2052 target_poll(target
);
2053 alive_sleep(10); /* sleep 10ms, i.e. <100 samples/second. */
2054 } else if (target
->state
== TARGET_RUNNING
) {
2055 /* We want to quickly sample the PC. */
2056 retval
= target_halt(target
);
2058 LOG_INFO("Target not halted or running");
2063 if (retval
!= ERROR_OK
)
2066 gettimeofday(&now
, NULL
);
2067 if ((sample_count
>= max_num_samples
) || timeval_compare(&now
, &timeout
) >= 0) {
2068 LOG_INFO("Profiling completed. %" PRIu32
" samples.", sample_count
);
2073 *num_samples
= sample_count
;
2077 /* Single aligned words are guaranteed to use 16 or 32 bit access
2078 * mode respectively, otherwise data is handled as quickly as
2081 int target_write_buffer(struct target
*target
, target_addr_t address
, uint32_t size
, const uint8_t *buffer
)
2083 LOG_DEBUG("writing buffer of %" PRIi32
" byte at " TARGET_ADDR_FMT
,
2086 if (!target_was_examined(target
)) {
2087 LOG_ERROR("Target not examined yet");
2094 if ((address
+ size
- 1) < address
) {
2095 /* GDB can request this when e.g. PC is 0xfffffffc */
2096 LOG_ERROR("address + size wrapped (" TARGET_ADDR_FMT
", 0x%08" PRIx32
")",
2102 return target
->type
->write_buffer(target
, address
, size
, buffer
);
2105 static int target_write_buffer_default(struct target
*target
,
2106 target_addr_t address
, uint32_t count
, const uint8_t *buffer
)
2110 /* Align up to maximum 4 bytes. The loop condition makes sure the next pass
2111 * will have something to do with the size we leave to it. */
2112 for (size
= 1; size
< 4 && count
>= size
* 2 + (address
& size
); size
*= 2) {
2113 if (address
& size
) {
2114 int retval
= target_write_memory(target
, address
, size
, 1, buffer
);
2115 if (retval
!= ERROR_OK
)
2123 /* Write the data with as large access size as possible. */
2124 for (; size
> 0; size
/= 2) {
2125 uint32_t aligned
= count
- count
% size
;
2127 int retval
= target_write_memory(target
, address
, size
, aligned
/ size
, buffer
);
2128 if (retval
!= ERROR_OK
)
2139 /* Single aligned words are guaranteed to use 16 or 32 bit access
2140 * mode respectively, otherwise data is handled as quickly as
2143 int target_read_buffer(struct target
*target
, target_addr_t address
, uint32_t size
, uint8_t *buffer
)
2145 LOG_DEBUG("reading buffer of %" PRIi32
" byte at " TARGET_ADDR_FMT
,
2148 if (!target_was_examined(target
)) {
2149 LOG_ERROR("Target not examined yet");
2156 if ((address
+ size
- 1) < address
) {
2157 /* GDB can request this when e.g. PC is 0xfffffffc */
2158 LOG_ERROR("address + size wrapped (" TARGET_ADDR_FMT
", 0x%08" PRIx32
")",
2164 return target
->type
->read_buffer(target
, address
, size
, buffer
);
2167 static int target_read_buffer_default(struct target
*target
, target_addr_t address
, uint32_t count
, uint8_t *buffer
)
2171 /* Align up to maximum 4 bytes. The loop condition makes sure the next pass
2172 * will have something to do with the size we leave to it. */
2173 for (size
= 1; size
< 4 && count
>= size
* 2 + (address
& size
); size
*= 2) {
2174 if (address
& size
) {
2175 int retval
= target_read_memory(target
, address
, size
, 1, buffer
);
2176 if (retval
!= ERROR_OK
)
2184 /* Read the data with as large access size as possible. */
2185 for (; size
> 0; size
/= 2) {
2186 uint32_t aligned
= count
- count
% size
;
2188 int retval
= target_read_memory(target
, address
, size
, aligned
/ size
, buffer
);
2189 if (retval
!= ERROR_OK
)
2200 int target_checksum_memory(struct target
*target
, target_addr_t address
, uint32_t size
, uint32_t* crc
)
2205 uint32_t checksum
= 0;
2206 if (!target_was_examined(target
)) {
2207 LOG_ERROR("Target not examined yet");
2211 retval
= target
->type
->checksum_memory(target
, address
, size
, &checksum
);
2212 if (retval
!= ERROR_OK
) {
2213 buffer
= malloc(size
);
2214 if (buffer
== NULL
) {
2215 LOG_ERROR("error allocating buffer for section (%" PRId32
" bytes)", size
);
2216 return ERROR_COMMAND_SYNTAX_ERROR
;
2218 retval
= target_read_buffer(target
, address
, size
, buffer
);
2219 if (retval
!= ERROR_OK
) {
2224 /* convert to target endianness */
2225 for (i
= 0; i
< (size
/sizeof(uint32_t)); i
++) {
2226 uint32_t target_data
;
2227 target_data
= target_buffer_get_u32(target
, &buffer
[i
*sizeof(uint32_t)]);
2228 target_buffer_set_u32(target
, &buffer
[i
*sizeof(uint32_t)], target_data
);
2231 retval
= image_calculate_checksum(buffer
, size
, &checksum
);
2240 int target_blank_check_memory(struct target
*target
, target_addr_t address
, uint32_t size
, uint32_t* blank
,
2241 uint8_t erased_value
)
2244 if (!target_was_examined(target
)) {
2245 LOG_ERROR("Target not examined yet");
2249 if (target
->type
->blank_check_memory
== 0)
2250 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
2252 retval
= target
->type
->blank_check_memory(target
, address
, size
, blank
, erased_value
);
2257 int target_read_u64(struct target
*target
, target_addr_t address
, uint64_t *value
)
2259 uint8_t value_buf
[8];
2260 if (!target_was_examined(target
)) {
2261 LOG_ERROR("Target not examined yet");
2265 int retval
= target_read_memory(target
, address
, 8, 1, value_buf
);
2267 if (retval
== ERROR_OK
) {
2268 *value
= target_buffer_get_u64(target
, value_buf
);
2269 LOG_DEBUG("address: " TARGET_ADDR_FMT
", value: 0x%16.16" PRIx64
"",
2274 LOG_DEBUG("address: " TARGET_ADDR_FMT
" failed",
2281 int target_read_u32(struct target
*target
, target_addr_t address
, uint32_t *value
)
2283 uint8_t value_buf
[4];
2284 if (!target_was_examined(target
)) {
2285 LOG_ERROR("Target not examined yet");
2289 int retval
= target_read_memory(target
, address
, 4, 1, value_buf
);
2291 if (retval
== ERROR_OK
) {
2292 *value
= target_buffer_get_u32(target
, value_buf
);
2293 LOG_DEBUG("address: " TARGET_ADDR_FMT
", value: 0x%8.8" PRIx32
"",
2298 LOG_DEBUG("address: " TARGET_ADDR_FMT
" failed",
2305 int target_read_u16(struct target
*target
, target_addr_t address
, uint16_t *value
)
2307 uint8_t value_buf
[2];
2308 if (!target_was_examined(target
)) {
2309 LOG_ERROR("Target not examined yet");
2313 int retval
= target_read_memory(target
, address
, 2, 1, value_buf
);
2315 if (retval
== ERROR_OK
) {
2316 *value
= target_buffer_get_u16(target
, value_buf
);
2317 LOG_DEBUG("address: " TARGET_ADDR_FMT
", value: 0x%4.4" PRIx16
,
2322 LOG_DEBUG("address: " TARGET_ADDR_FMT
" failed",
2329 int target_read_u8(struct target
*target
, target_addr_t address
, uint8_t *value
)
2331 if (!target_was_examined(target
)) {
2332 LOG_ERROR("Target not examined yet");
2336 int retval
= target_read_memory(target
, address
, 1, 1, value
);
2338 if (retval
== ERROR_OK
) {
2339 LOG_DEBUG("address: " TARGET_ADDR_FMT
", value: 0x%2.2" PRIx8
,
2344 LOG_DEBUG("address: " TARGET_ADDR_FMT
" failed",
2351 int target_write_u64(struct target
*target
, target_addr_t address
, uint64_t value
)
2354 uint8_t value_buf
[8];
2355 if (!target_was_examined(target
)) {
2356 LOG_ERROR("Target not examined yet");
2360 LOG_DEBUG("address: " TARGET_ADDR_FMT
", value: 0x%16.16" PRIx64
"",
2364 target_buffer_set_u64(target
, value_buf
, value
);
2365 retval
= target_write_memory(target
, address
, 8, 1, value_buf
);
2366 if (retval
!= ERROR_OK
)
2367 LOG_DEBUG("failed: %i", retval
);
2372 int target_write_u32(struct target
*target
, target_addr_t address
, uint32_t value
)
2375 uint8_t value_buf
[4];
2376 if (!target_was_examined(target
)) {
2377 LOG_ERROR("Target not examined yet");
2381 LOG_DEBUG("address: " TARGET_ADDR_FMT
", value: 0x%8.8" PRIx32
"",
2385 target_buffer_set_u32(target
, value_buf
, value
);
2386 retval
= target_write_memory(target
, address
, 4, 1, value_buf
);
2387 if (retval
!= ERROR_OK
)
2388 LOG_DEBUG("failed: %i", retval
);
2393 int target_write_u16(struct target
*target
, target_addr_t address
, uint16_t value
)
2396 uint8_t value_buf
[2];
2397 if (!target_was_examined(target
)) {
2398 LOG_ERROR("Target not examined yet");
2402 LOG_DEBUG("address: " TARGET_ADDR_FMT
", value: 0x%8.8" PRIx16
,
2406 target_buffer_set_u16(target
, value_buf
, value
);
2407 retval
= target_write_memory(target
, address
, 2, 1, value_buf
);
2408 if (retval
!= ERROR_OK
)
2409 LOG_DEBUG("failed: %i", retval
);
2414 int target_write_u8(struct target
*target
, target_addr_t address
, uint8_t value
)
2417 if (!target_was_examined(target
)) {
2418 LOG_ERROR("Target not examined yet");
2422 LOG_DEBUG("address: " TARGET_ADDR_FMT
", value: 0x%2.2" PRIx8
,
2425 retval
= target_write_memory(target
, address
, 1, 1, &value
);
2426 if (retval
!= ERROR_OK
)
2427 LOG_DEBUG("failed: %i", retval
);
2432 int target_write_phys_u64(struct target
*target
, target_addr_t address
, uint64_t value
)
2435 uint8_t value_buf
[8];
2436 if (!target_was_examined(target
)) {
2437 LOG_ERROR("Target not examined yet");
2441 LOG_DEBUG("address: " TARGET_ADDR_FMT
", value: 0x%16.16" PRIx64
"",
2445 target_buffer_set_u64(target
, value_buf
, value
);
2446 retval
= target_write_phys_memory(target
, address
, 8, 1, value_buf
);
2447 if (retval
!= ERROR_OK
)
2448 LOG_DEBUG("failed: %i", retval
);
2453 int target_write_phys_u32(struct target
*target
, target_addr_t address
, uint32_t value
)
2456 uint8_t value_buf
[4];
2457 if (!target_was_examined(target
)) {
2458 LOG_ERROR("Target not examined yet");
2462 LOG_DEBUG("address: " TARGET_ADDR_FMT
", value: 0x%8.8" PRIx32
"",
2466 target_buffer_set_u32(target
, value_buf
, value
);
2467 retval
= target_write_phys_memory(target
, address
, 4, 1, value_buf
);
2468 if (retval
!= ERROR_OK
)
2469 LOG_DEBUG("failed: %i", retval
);
2474 int target_write_phys_u16(struct target
*target
, target_addr_t address
, uint16_t value
)
2477 uint8_t value_buf
[2];
2478 if (!target_was_examined(target
)) {
2479 LOG_ERROR("Target not examined yet");
2483 LOG_DEBUG("address: " TARGET_ADDR_FMT
", value: 0x%8.8" PRIx16
,
2487 target_buffer_set_u16(target
, value_buf
, value
);
2488 retval
= target_write_phys_memory(target
, address
, 2, 1, value_buf
);
2489 if (retval
!= ERROR_OK
)
2490 LOG_DEBUG("failed: %i", retval
);
2495 int target_write_phys_u8(struct target
*target
, target_addr_t address
, uint8_t value
)
2498 if (!target_was_examined(target
)) {
2499 LOG_ERROR("Target not examined yet");
2503 LOG_DEBUG("address: " TARGET_ADDR_FMT
", value: 0x%2.2" PRIx8
,
2506 retval
= target_write_phys_memory(target
, address
, 1, 1, &value
);
2507 if (retval
!= ERROR_OK
)
2508 LOG_DEBUG("failed: %i", retval
);
2513 static int find_target(struct command_context
*cmd_ctx
, const char *name
)
2515 struct target
*target
= get_target(name
);
2516 if (target
== NULL
) {
2517 LOG_ERROR("Target: %s is unknown, try one of:\n", name
);
2520 if (!target
->tap
->enabled
) {
2521 LOG_USER("Target: TAP %s is disabled, "
2522 "can't be the current target\n",
2523 target
->tap
->dotted_name
);
2527 cmd_ctx
->current_target
= target
;
2528 if (cmd_ctx
->current_target_override
)
2529 cmd_ctx
->current_target_override
= target
;
2535 COMMAND_HANDLER(handle_targets_command
)
2537 int retval
= ERROR_OK
;
2538 if (CMD_ARGC
== 1) {
2539 retval
= find_target(CMD_CTX
, CMD_ARGV
[0]);
2540 if (retval
== ERROR_OK
) {
2546 struct target
*target
= all_targets
;
2547 command_print(CMD_CTX
, " TargetName Type Endian TapName State ");
2548 command_print(CMD_CTX
, "-- ------------------ ---------- ------ ------------------ ------------");
2553 if (target
->tap
->enabled
)
2554 state
= target_state_name(target
);
2556 state
= "tap-disabled";
2558 if (CMD_CTX
->current_target
== target
)
2561 /* keep columns lined up to match the headers above */
2562 command_print(CMD_CTX
,
2563 "%2d%c %-18s %-10s %-6s %-18s %s",
2564 target
->target_number
,
2566 target_name(target
),
2567 target_type_name(target
),
2568 Jim_Nvp_value2name_simple(nvp_target_endian
,
2569 target
->endianness
)->name
,
2570 target
->tap
->dotted_name
,
2572 target
= target
->next
;
2578 /* every 300ms we check for reset & powerdropout and issue a "reset halt" if so. */
2580 static int powerDropout
;
2581 static int srstAsserted
;
2583 static int runPowerRestore
;
2584 static int runPowerDropout
;
2585 static int runSrstAsserted
;
2586 static int runSrstDeasserted
;
2588 static int sense_handler(void)
2590 static int prevSrstAsserted
;
2591 static int prevPowerdropout
;
2593 int retval
= jtag_power_dropout(&powerDropout
);
2594 if (retval
!= ERROR_OK
)
2598 powerRestored
= prevPowerdropout
&& !powerDropout
;
2600 runPowerRestore
= 1;
2602 int64_t current
= timeval_ms();
2603 static int64_t lastPower
;
2604 bool waitMore
= lastPower
+ 2000 > current
;
2605 if (powerDropout
&& !waitMore
) {
2606 runPowerDropout
= 1;
2607 lastPower
= current
;
2610 retval
= jtag_srst_asserted(&srstAsserted
);
2611 if (retval
!= ERROR_OK
)
2615 srstDeasserted
= prevSrstAsserted
&& !srstAsserted
;
2617 static int64_t lastSrst
;
2618 waitMore
= lastSrst
+ 2000 > current
;
2619 if (srstDeasserted
&& !waitMore
) {
2620 runSrstDeasserted
= 1;
2624 if (!prevSrstAsserted
&& srstAsserted
)
2625 runSrstAsserted
= 1;
2627 prevSrstAsserted
= srstAsserted
;
2628 prevPowerdropout
= powerDropout
;
2630 if (srstDeasserted
|| powerRestored
) {
2631 /* Other than logging the event we can't do anything here.
2632 * Issuing a reset is a particularly bad idea as we might
2633 * be inside a reset already.
2640 /* process target state changes */
2641 static int handle_target(void *priv
)
2643 Jim_Interp
*interp
= (Jim_Interp
*)priv
;
2644 int retval
= ERROR_OK
;
2646 if (!is_jtag_poll_safe()) {
2647 /* polling is disabled currently */
2651 /* we do not want to recurse here... */
2652 static int recursive
;
2656 /* danger! running these procedures can trigger srst assertions and power dropouts.
2657 * We need to avoid an infinite loop/recursion here and we do that by
2658 * clearing the flags after running these events.
2660 int did_something
= 0;
2661 if (runSrstAsserted
) {
2662 LOG_INFO("srst asserted detected, running srst_asserted proc.");
2663 Jim_Eval(interp
, "srst_asserted");
2666 if (runSrstDeasserted
) {
2667 Jim_Eval(interp
, "srst_deasserted");
2670 if (runPowerDropout
) {
2671 LOG_INFO("Power dropout detected, running power_dropout proc.");
2672 Jim_Eval(interp
, "power_dropout");
2675 if (runPowerRestore
) {
2676 Jim_Eval(interp
, "power_restore");
2680 if (did_something
) {
2681 /* clear detect flags */
2685 /* clear action flags */
2687 runSrstAsserted
= 0;
2688 runSrstDeasserted
= 0;
2689 runPowerRestore
= 0;
2690 runPowerDropout
= 0;
2695 /* Poll targets for state changes unless that's globally disabled.
2696 * Skip targets that are currently disabled.
2698 for (struct target
*target
= all_targets
;
2699 is_jtag_poll_safe() && target
;
2700 target
= target
->next
) {
2702 if (!target_was_examined(target
))
2705 if (!target
->tap
->enabled
)
2708 if (target
->backoff
.times
> target
->backoff
.count
) {
2709 /* do not poll this time as we failed previously */
2710 target
->backoff
.count
++;
2713 target
->backoff
.count
= 0;
2715 /* only poll target if we've got power and srst isn't asserted */
2716 if (!powerDropout
&& !srstAsserted
) {
2717 /* polling may fail silently until the target has been examined */
2718 retval
= target_poll(target
);
2719 if (retval
!= ERROR_OK
) {
2720 /* 100ms polling interval. Increase interval between polling up to 5000ms */
2721 if (target
->backoff
.times
* polling_interval
< 5000) {
2722 target
->backoff
.times
*= 2;
2723 target
->backoff
.times
++;
2726 /* Tell GDB to halt the debugger. This allows the user to
2727 * run monitor commands to handle the situation.
2729 target_call_event_callbacks(target
, TARGET_EVENT_GDB_HALT
);
2731 if (target
->backoff
.times
> 0) {
2732 LOG_USER("Polling target %s failed, trying to reexamine", target_name(target
));
2733 target_reset_examined(target
);
2734 retval
= target_examine_one(target
);
2735 /* Target examination could have failed due to unstable connection,
2736 * but we set the examined flag anyway to repoll it later */
2737 if (retval
!= ERROR_OK
) {
2738 target
->examined
= true;
2739 LOG_USER("Examination failed, GDB will be halted. Polling again in %dms",
2740 target
->backoff
.times
* polling_interval
);
2745 /* Since we succeeded, we reset backoff count */
2746 target
->backoff
.times
= 0;
2753 COMMAND_HANDLER(handle_reg_command
)
2755 struct target
*target
;
2756 struct reg
*reg
= NULL
;
2762 target
= get_current_target(CMD_CTX
);
2764 /* list all available registers for the current target */
2765 if (CMD_ARGC
== 0) {
2766 struct reg_cache
*cache
= target
->reg_cache
;
2772 command_print(CMD_CTX
, "===== %s", cache
->name
);
2774 for (i
= 0, reg
= cache
->reg_list
;
2775 i
< cache
->num_regs
;
2776 i
++, reg
++, count
++) {
2777 /* only print cached values if they are valid */
2779 value
= buf_to_str(reg
->value
,
2781 command_print(CMD_CTX
,
2782 "(%i) %s (/%" PRIu32
"): 0x%s%s",
2790 command_print(CMD_CTX
, "(%i) %s (/%" PRIu32
")",
2795 cache
= cache
->next
;
2801 /* access a single register by its ordinal number */
2802 if ((CMD_ARGV
[0][0] >= '0') && (CMD_ARGV
[0][0] <= '9')) {
2804 COMMAND_PARSE_NUMBER(uint
, CMD_ARGV
[0], num
);
2806 struct reg_cache
*cache
= target
->reg_cache
;
2810 for (i
= 0; i
< cache
->num_regs
; i
++) {
2811 if (count
++ == num
) {
2812 reg
= &cache
->reg_list
[i
];
2818 cache
= cache
->next
;
2822 command_print(CMD_CTX
, "%i is out of bounds, the current target "
2823 "has only %i registers (0 - %i)", num
, count
, count
- 1);
2827 /* access a single register by its name */
2828 reg
= register_get_by_name(target
->reg_cache
, CMD_ARGV
[0], 1);
2831 command_print(CMD_CTX
, "register %s not found in current target", CMD_ARGV
[0]);
2836 assert(reg
!= NULL
); /* give clang a hint that we *know* reg is != NULL here */
2838 /* display a register */
2839 if ((CMD_ARGC
== 1) || ((CMD_ARGC
== 2) && !((CMD_ARGV
[1][0] >= '0')
2840 && (CMD_ARGV
[1][0] <= '9')))) {
2841 if ((CMD_ARGC
== 2) && (strcmp(CMD_ARGV
[1], "force") == 0))
2844 if (reg
->valid
== 0)
2845 reg
->type
->get(reg
);
2846 value
= buf_to_str(reg
->value
, reg
->size
, 16);
2847 command_print(CMD_CTX
, "%s (/%i): 0x%s", reg
->name
, (int)(reg
->size
), value
);
2852 /* set register value */
2853 if (CMD_ARGC
== 2) {
2854 uint8_t *buf
= malloc(DIV_ROUND_UP(reg
->size
, 8));
2857 str_to_buf(CMD_ARGV
[1], strlen(CMD_ARGV
[1]), buf
, reg
->size
, 0);
2859 reg
->type
->set(reg
, buf
);
2861 value
= buf_to_str(reg
->value
, reg
->size
, 16);
2862 command_print(CMD_CTX
, "%s (/%i): 0x%s", reg
->name
, (int)(reg
->size
), value
);
2870 return ERROR_COMMAND_SYNTAX_ERROR
;
2873 COMMAND_HANDLER(handle_poll_command
)
2875 int retval
= ERROR_OK
;
2876 struct target
*target
= get_current_target(CMD_CTX
);
2878 if (CMD_ARGC
== 0) {
2879 command_print(CMD_CTX
, "background polling: %s",
2880 jtag_poll_get_enabled() ? "on" : "off");
2881 command_print(CMD_CTX
, "TAP: %s (%s)",
2882 target
->tap
->dotted_name
,
2883 target
->tap
->enabled
? "enabled" : "disabled");
2884 if (!target
->tap
->enabled
)
2886 retval
= target_poll(target
);
2887 if (retval
!= ERROR_OK
)
2889 retval
= target_arch_state(target
);
2890 if (retval
!= ERROR_OK
)
2892 } else if (CMD_ARGC
== 1) {
2894 COMMAND_PARSE_ON_OFF(CMD_ARGV
[0], enable
);
2895 jtag_poll_set_enabled(enable
);
2897 return ERROR_COMMAND_SYNTAX_ERROR
;
2902 COMMAND_HANDLER(handle_wait_halt_command
)
2905 return ERROR_COMMAND_SYNTAX_ERROR
;
2907 unsigned ms
= DEFAULT_HALT_TIMEOUT
;
2908 if (1 == CMD_ARGC
) {
2909 int retval
= parse_uint(CMD_ARGV
[0], &ms
);
2910 if (ERROR_OK
!= retval
)
2911 return ERROR_COMMAND_SYNTAX_ERROR
;
2914 struct target
*target
= get_current_target(CMD_CTX
);
2915 return target_wait_state(target
, TARGET_HALTED
, ms
);
2918 /* wait for target state to change. The trick here is to have a low
2919 * latency for short waits and not to suck up all the CPU time
2922 * After 500ms, keep_alive() is invoked
2924 int target_wait_state(struct target
*target
, enum target_state state
, int ms
)
2927 int64_t then
= 0, cur
;
2931 retval
= target_poll(target
);
2932 if (retval
!= ERROR_OK
)
2934 if (target
->state
== state
)
2939 then
= timeval_ms();
2940 LOG_DEBUG("waiting for target %s...",
2941 Jim_Nvp_value2name_simple(nvp_target_state
, state
)->name
);
2947 if ((cur
-then
) > ms
) {
2948 LOG_ERROR("timed out while waiting for target %s",
2949 Jim_Nvp_value2name_simple(nvp_target_state
, state
)->name
);
2957 COMMAND_HANDLER(handle_halt_command
)
2961 struct target
*target
= get_current_target(CMD_CTX
);
2962 int retval
= target_halt(target
);
2963 if (ERROR_OK
!= retval
)
2966 if (CMD_ARGC
== 1) {
2967 unsigned wait_local
;
2968 retval
= parse_uint(CMD_ARGV
[0], &wait_local
);
2969 if (ERROR_OK
!= retval
)
2970 return ERROR_COMMAND_SYNTAX_ERROR
;
2975 return CALL_COMMAND_HANDLER(handle_wait_halt_command
);
2978 COMMAND_HANDLER(handle_soft_reset_halt_command
)
2980 struct target
*target
= get_current_target(CMD_CTX
);
2982 LOG_USER("requesting target halt and executing a soft reset");
2984 target_soft_reset_halt(target
);
2989 COMMAND_HANDLER(handle_reset_command
)
2992 return ERROR_COMMAND_SYNTAX_ERROR
;
2994 enum target_reset_mode reset_mode
= RESET_RUN
;
2995 if (CMD_ARGC
== 1) {
2997 n
= Jim_Nvp_name2value_simple(nvp_reset_modes
, CMD_ARGV
[0]);
2998 if ((n
->name
== NULL
) || (n
->value
== RESET_UNKNOWN
))
2999 return ERROR_COMMAND_SYNTAX_ERROR
;
3000 reset_mode
= n
->value
;
3003 /* reset *all* targets */
3004 return target_process_reset(CMD_CTX
, reset_mode
);
3008 COMMAND_HANDLER(handle_resume_command
)
3012 return ERROR_COMMAND_SYNTAX_ERROR
;
3014 struct target
*target
= get_current_target(CMD_CTX
);
3016 /* with no CMD_ARGV, resume from current pc, addr = 0,
3017 * with one arguments, addr = CMD_ARGV[0],
3018 * handle breakpoints, not debugging */
3019 target_addr_t addr
= 0;
3020 if (CMD_ARGC
== 1) {
3021 COMMAND_PARSE_ADDRESS(CMD_ARGV
[0], addr
);
3025 return target_resume(target
, current
, addr
, 1, 0);
3028 COMMAND_HANDLER(handle_step_command
)
3031 return ERROR_COMMAND_SYNTAX_ERROR
;
3035 /* with no CMD_ARGV, step from current pc, addr = 0,
3036 * with one argument addr = CMD_ARGV[0],
3037 * handle breakpoints, debugging */
3038 target_addr_t addr
= 0;
3040 if (CMD_ARGC
== 1) {
3041 COMMAND_PARSE_ADDRESS(CMD_ARGV
[0], addr
);
3045 struct target
*target
= get_current_target(CMD_CTX
);
3047 return target
->type
->step(target
, current_pc
, addr
, 1);
3050 static void handle_md_output(struct command_context
*cmd_ctx
,
3051 struct target
*target
, target_addr_t address
, unsigned size
,
3052 unsigned count
, const uint8_t *buffer
)
3054 const unsigned line_bytecnt
= 32;
3055 unsigned line_modulo
= line_bytecnt
/ size
;
3057 char output
[line_bytecnt
* 4 + 1];
3058 unsigned output_len
= 0;
3060 const char *value_fmt
;
3063 value_fmt
= "%16.16"PRIx64
" ";
3066 value_fmt
= "%8.8"PRIx64
" ";
3069 value_fmt
= "%4.4"PRIx64
" ";
3072 value_fmt
= "%2.2"PRIx64
" ";
3075 /* "can't happen", caller checked */
3076 LOG_ERROR("invalid memory read size: %u", size
);
3080 for (unsigned i
= 0; i
< count
; i
++) {
3081 if (i
% line_modulo
== 0) {
3082 output_len
+= snprintf(output
+ output_len
,
3083 sizeof(output
) - output_len
,
3084 TARGET_ADDR_FMT
": ",
3085 (address
+ (i
* size
)));
3089 const uint8_t *value_ptr
= buffer
+ i
* size
;
3092 value
= target_buffer_get_u64(target
, value_ptr
);
3095 value
= target_buffer_get_u32(target
, value_ptr
);
3098 value
= target_buffer_get_u16(target
, value_ptr
);
3103 output_len
+= snprintf(output
+ output_len
,
3104 sizeof(output
) - output_len
,
3107 if ((i
% line_modulo
== line_modulo
- 1) || (i
== count
- 1)) {
3108 command_print(cmd_ctx
, "%s", output
);
3114 COMMAND_HANDLER(handle_md_command
)
3117 return ERROR_COMMAND_SYNTAX_ERROR
;
3120 switch (CMD_NAME
[2]) {
3134 return ERROR_COMMAND_SYNTAX_ERROR
;
3137 bool physical
= strcmp(CMD_ARGV
[0], "phys") == 0;
3138 int (*fn
)(struct target
*target
,
3139 target_addr_t address
, uint32_t size_value
, uint32_t count
, uint8_t *buffer
);
3143 fn
= target_read_phys_memory
;
3145 fn
= target_read_memory
;
3146 if ((CMD_ARGC
< 1) || (CMD_ARGC
> 2))
3147 return ERROR_COMMAND_SYNTAX_ERROR
;
3149 target_addr_t address
;
3150 COMMAND_PARSE_ADDRESS(CMD_ARGV
[0], address
);
3154 COMMAND_PARSE_NUMBER(uint
, CMD_ARGV
[1], count
);
3156 uint8_t *buffer
= calloc(count
, size
);
3157 if (buffer
== NULL
) {
3158 LOG_ERROR("Failed to allocate md read buffer");
3162 struct target
*target
= get_current_target(CMD_CTX
);
3163 int retval
= fn(target
, address
, size
, count
, buffer
);
3164 if (ERROR_OK
== retval
)
3165 handle_md_output(CMD_CTX
, target
, address
, size
, count
, buffer
);
3172 typedef int (*target_write_fn
)(struct target
*target
,
3173 target_addr_t address
, uint32_t size
, uint32_t count
, const uint8_t *buffer
);
3175 static int target_fill_mem(struct target
*target
,
3176 target_addr_t address
,
3184 /* We have to write in reasonably large chunks to be able
3185 * to fill large memory areas with any sane speed */
3186 const unsigned chunk_size
= 16384;
3187 uint8_t *target_buf
= malloc(chunk_size
* data_size
);
3188 if (target_buf
== NULL
) {
3189 LOG_ERROR("Out of memory");
3193 for (unsigned i
= 0; i
< chunk_size
; i
++) {
3194 switch (data_size
) {
3196 target_buffer_set_u64(target
, target_buf
+ i
* data_size
, b
);
3199 target_buffer_set_u32(target
, target_buf
+ i
* data_size
, b
);
3202 target_buffer_set_u16(target
, target_buf
+ i
* data_size
, b
);
3205 target_buffer_set_u8(target
, target_buf
+ i
* data_size
, b
);
3212 int retval
= ERROR_OK
;
3214 for (unsigned x
= 0; x
< c
; x
+= chunk_size
) {
3217 if (current
> chunk_size
)
3218 current
= chunk_size
;
3219 retval
= fn(target
, address
+ x
* data_size
, data_size
, current
, target_buf
);
3220 if (retval
!= ERROR_OK
)
3222 /* avoid GDB timeouts */
3231 COMMAND_HANDLER(handle_mw_command
)
3234 return ERROR_COMMAND_SYNTAX_ERROR
;
3235 bool physical
= strcmp(CMD_ARGV
[0], "phys") == 0;
3240 fn
= target_write_phys_memory
;
3242 fn
= target_write_memory
;
3243 if ((CMD_ARGC
< 2) || (CMD_ARGC
> 3))
3244 return ERROR_COMMAND_SYNTAX_ERROR
;
3246 target_addr_t address
;
3247 COMMAND_PARSE_ADDRESS(CMD_ARGV
[0], address
);
3249 target_addr_t value
;
3250 COMMAND_PARSE_ADDRESS(CMD_ARGV
[1], value
);
3254 COMMAND_PARSE_NUMBER(uint
, CMD_ARGV
[2], count
);
3256 struct target
*target
= get_current_target(CMD_CTX
);
3258 switch (CMD_NAME
[2]) {
3272 return ERROR_COMMAND_SYNTAX_ERROR
;
3275 return target_fill_mem(target
, address
, fn
, wordsize
, value
, count
);
3278 static COMMAND_HELPER(parse_load_image_command_CMD_ARGV
, struct image
*image
,
3279 target_addr_t
*min_address
, target_addr_t
*max_address
)
3281 if (CMD_ARGC
< 1 || CMD_ARGC
> 5)
3282 return ERROR_COMMAND_SYNTAX_ERROR
;
3284 /* a base address isn't always necessary,
3285 * default to 0x0 (i.e. don't relocate) */
3286 if (CMD_ARGC
>= 2) {
3288 COMMAND_PARSE_ADDRESS(CMD_ARGV
[1], addr
);
3289 image
->base_address
= addr
;
3290 image
->base_address_set
= 1;
3292 image
->base_address_set
= 0;
3294 image
->start_address_set
= 0;
3297 COMMAND_PARSE_ADDRESS(CMD_ARGV
[3], *min_address
);
3298 if (CMD_ARGC
== 5) {
3299 COMMAND_PARSE_ADDRESS(CMD_ARGV
[4], *max_address
);
3300 /* use size (given) to find max (required) */
3301 *max_address
+= *min_address
;
3304 if (*min_address
> *max_address
)
3305 return ERROR_COMMAND_SYNTAX_ERROR
;
3310 COMMAND_HANDLER(handle_load_image_command
)
3314 uint32_t image_size
;
3315 target_addr_t min_address
= 0;
3316 target_addr_t max_address
= -1;
3320 int retval
= CALL_COMMAND_HANDLER(parse_load_image_command_CMD_ARGV
,
3321 &image
, &min_address
, &max_address
);
3322 if (ERROR_OK
!= retval
)
3325 struct target
*target
= get_current_target(CMD_CTX
);
3327 struct duration bench
;
3328 duration_start(&bench
);
3330 if (image_open(&image
, CMD_ARGV
[0], (CMD_ARGC
>= 3) ? CMD_ARGV
[2] : NULL
) != ERROR_OK
)
3335 for (i
= 0; i
< image
.num_sections
; i
++) {
3336 buffer
= malloc(image
.sections
[i
].size
);
3337 if (buffer
== NULL
) {
3338 command_print(CMD_CTX
,
3339 "error allocating buffer for section (%d bytes)",
3340 (int)(image
.sections
[i
].size
));
3341 retval
= ERROR_FAIL
;
3345 retval
= image_read_section(&image
, i
, 0x0, image
.sections
[i
].size
, buffer
, &buf_cnt
);
3346 if (retval
!= ERROR_OK
) {
3351 uint32_t offset
= 0;
3352 uint32_t length
= buf_cnt
;
3354 /* DANGER!!! beware of unsigned comparision here!!! */
3356 if ((image
.sections
[i
].base_address
+ buf_cnt
>= min_address
) &&
3357 (image
.sections
[i
].base_address
< max_address
)) {
3359 if (image
.sections
[i
].base_address
< min_address
) {
3360 /* clip addresses below */
3361 offset
+= min_address
-image
.sections
[i
].base_address
;
3365 if (image
.sections
[i
].base_address
+ buf_cnt
> max_address
)
3366 length
-= (image
.sections
[i
].base_address
+ buf_cnt
)-max_address
;
3368 retval
= target_write_buffer(target
,
3369 image
.sections
[i
].base_address
+ offset
, length
, buffer
+ offset
);
3370 if (retval
!= ERROR_OK
) {
3374 image_size
+= length
;
3375 command_print(CMD_CTX
, "%u bytes written at address " TARGET_ADDR_FMT
"",
3376 (unsigned int)length
,
3377 image
.sections
[i
].base_address
+ offset
);
3383 if ((ERROR_OK
== retval
) && (duration_measure(&bench
) == ERROR_OK
)) {
3384 command_print(CMD_CTX
, "downloaded %" PRIu32
" bytes "
3385 "in %fs (%0.3f KiB/s)", image_size
,
3386 duration_elapsed(&bench
), duration_kbps(&bench
, image_size
));
3389 image_close(&image
);
3395 COMMAND_HANDLER(handle_dump_image_command
)
3397 struct fileio
*fileio
;
3399 int retval
, retvaltemp
;
3400 target_addr_t address
, size
;
3401 struct duration bench
;
3402 struct target
*target
= get_current_target(CMD_CTX
);
3405 return ERROR_COMMAND_SYNTAX_ERROR
;
3407 COMMAND_PARSE_ADDRESS(CMD_ARGV
[1], address
);
3408 COMMAND_PARSE_ADDRESS(CMD_ARGV
[2], size
);
3410 uint32_t buf_size
= (size
> 4096) ? 4096 : size
;
3411 buffer
= malloc(buf_size
);
3415 retval
= fileio_open(&fileio
, CMD_ARGV
[0], FILEIO_WRITE
, FILEIO_BINARY
);
3416 if (retval
!= ERROR_OK
) {
3421 duration_start(&bench
);
3424 size_t size_written
;
3425 uint32_t this_run_size
= (size
> buf_size
) ? buf_size
: size
;
3426 retval
= target_read_buffer(target
, address
, this_run_size
, buffer
);
3427 if (retval
!= ERROR_OK
)
3430 retval
= fileio_write(fileio
, this_run_size
, buffer
, &size_written
);
3431 if (retval
!= ERROR_OK
)
3434 size
-= this_run_size
;
3435 address
+= this_run_size
;
3440 if ((ERROR_OK
== retval
) && (duration_measure(&bench
) == ERROR_OK
)) {
3442 retval
= fileio_size(fileio
, &filesize
);
3443 if (retval
!= ERROR_OK
)
3445 command_print(CMD_CTX
,
3446 "dumped %zu bytes in %fs (%0.3f KiB/s)", filesize
,
3447 duration_elapsed(&bench
), duration_kbps(&bench
, filesize
));
3450 retvaltemp
= fileio_close(fileio
);
3451 if (retvaltemp
!= ERROR_OK
)
3460 IMAGE_CHECKSUM_ONLY
= 2
3463 static COMMAND_HELPER(handle_verify_image_command_internal
, enum verify_mode verify
)
3467 uint32_t image_size
;
3470 uint32_t checksum
= 0;
3471 uint32_t mem_checksum
= 0;
3475 struct target
*target
= get_current_target(CMD_CTX
);
3478 return ERROR_COMMAND_SYNTAX_ERROR
;
3481 LOG_ERROR("no target selected");
3485 struct duration bench
;
3486 duration_start(&bench
);
3488 if (CMD_ARGC
>= 2) {
3490 COMMAND_PARSE_ADDRESS(CMD_ARGV
[1], addr
);
3491 image
.base_address
= addr
;
3492 image
.base_address_set
= 1;
3494 image
.base_address_set
= 0;
3495 image
.base_address
= 0x0;
3498 image
.start_address_set
= 0;
3500 retval
= image_open(&image
, CMD_ARGV
[0], (CMD_ARGC
== 3) ? CMD_ARGV
[2] : NULL
);
3501 if (retval
!= ERROR_OK
)
3507 for (i
= 0; i
< image
.num_sections
; i
++) {
3508 buffer
= malloc(image
.sections
[i
].size
);
3509 if (buffer
== NULL
) {
3510 command_print(CMD_CTX
,
3511 "error allocating buffer for section (%d bytes)",
3512 (int)(image
.sections
[i
].size
));
3515 retval
= image_read_section(&image
, i
, 0x0, image
.sections
[i
].size
, buffer
, &buf_cnt
);
3516 if (retval
!= ERROR_OK
) {
3521 if (verify
>= IMAGE_VERIFY
) {
3522 /* calculate checksum of image */
3523 retval
= image_calculate_checksum(buffer
, buf_cnt
, &checksum
);
3524 if (retval
!= ERROR_OK
) {
3529 retval
= target_checksum_memory(target
, image
.sections
[i
].base_address
, buf_cnt
, &mem_checksum
);
3530 if (retval
!= ERROR_OK
) {
3534 if ((checksum
!= mem_checksum
) && (verify
== IMAGE_CHECKSUM_ONLY
)) {
3535 LOG_ERROR("checksum mismatch");
3537 retval
= ERROR_FAIL
;
3540 if (checksum
!= mem_checksum
) {
3541 /* failed crc checksum, fall back to a binary compare */
3545 LOG_ERROR("checksum mismatch - attempting binary compare");
3547 data
= malloc(buf_cnt
);
3549 /* Can we use 32bit word accesses? */
3551 int count
= buf_cnt
;
3552 if ((count
% 4) == 0) {
3556 retval
= target_read_memory(target
, image
.sections
[i
].base_address
, size
, count
, data
);
3557 if (retval
== ERROR_OK
) {
3559 for (t
= 0; t
< buf_cnt
; t
++) {
3560 if (data
[t
] != buffer
[t
]) {
3561 command_print(CMD_CTX
,
3562 "diff %d address 0x%08x. Was 0x%02x instead of 0x%02x",
3564 (unsigned)(t
+ image
.sections
[i
].base_address
),
3567 if (diffs
++ >= 127) {
3568 command_print(CMD_CTX
, "More than 128 errors, the rest are not printed.");
3580 command_print(CMD_CTX
, "address " TARGET_ADDR_FMT
" length 0x%08zx",
3581 image
.sections
[i
].base_address
,
3586 image_size
+= buf_cnt
;
3589 command_print(CMD_CTX
, "No more differences found.");
3592 retval
= ERROR_FAIL
;
3593 if ((ERROR_OK
== retval
) && (duration_measure(&bench
) == ERROR_OK
)) {
3594 command_print(CMD_CTX
, "verified %" PRIu32
" bytes "
3595 "in %fs (%0.3f KiB/s)", image_size
,
3596 duration_elapsed(&bench
), duration_kbps(&bench
, image_size
));
3599 image_close(&image
);
3604 COMMAND_HANDLER(handle_verify_image_checksum_command
)
3606 return CALL_COMMAND_HANDLER(handle_verify_image_command_internal
, IMAGE_CHECKSUM_ONLY
);
3609 COMMAND_HANDLER(handle_verify_image_command
)
3611 return CALL_COMMAND_HANDLER(handle_verify_image_command_internal
, IMAGE_VERIFY
);
3614 COMMAND_HANDLER(handle_test_image_command
)
3616 return CALL_COMMAND_HANDLER(handle_verify_image_command_internal
, IMAGE_TEST
);
3619 static int handle_bp_command_list(struct command_context
*cmd_ctx
)
3621 struct target
*target
= get_current_target(cmd_ctx
);
3622 struct breakpoint
*breakpoint
= target
->breakpoints
;
3623 while (breakpoint
) {
3624 if (breakpoint
->type
== BKPT_SOFT
) {
3625 char *buf
= buf_to_str(breakpoint
->orig_instr
,
3626 breakpoint
->length
, 16);
3627 command_print(cmd_ctx
, "IVA breakpoint: " TARGET_ADDR_FMT
", 0x%x, %i, 0x%s",
3628 breakpoint
->address
,
3630 breakpoint
->set
, buf
);
3633 if ((breakpoint
->address
== 0) && (breakpoint
->asid
!= 0))
3634 command_print(cmd_ctx
, "Context breakpoint: 0x%8.8" PRIx32
", 0x%x, %i",
3636 breakpoint
->length
, breakpoint
->set
);
3637 else if ((breakpoint
->address
!= 0) && (breakpoint
->asid
!= 0)) {
3638 command_print(cmd_ctx
, "Hybrid breakpoint(IVA): " TARGET_ADDR_FMT
", 0x%x, %i",
3639 breakpoint
->address
,
3640 breakpoint
->length
, breakpoint
->set
);
3641 command_print(cmd_ctx
, "\t|--->linked with ContextID: 0x%8.8" PRIx32
,
3644 command_print(cmd_ctx
, "Breakpoint(IVA): " TARGET_ADDR_FMT
", 0x%x, %i",
3645 breakpoint
->address
,
3646 breakpoint
->length
, breakpoint
->set
);
3649 breakpoint
= breakpoint
->next
;
3654 static int handle_bp_command_set(struct command_context
*cmd_ctx
,
3655 target_addr_t addr
, uint32_t asid
, uint32_t length
, int hw
)
3657 struct target
*target
= get_current_target(cmd_ctx
);
3661 retval
= breakpoint_add(target
, addr
, length
, hw
);
3662 if (ERROR_OK
== retval
)
3663 command_print(cmd_ctx
, "breakpoint set at " TARGET_ADDR_FMT
"", addr
);
3665 LOG_ERROR("Failure setting breakpoint, the same address(IVA) is already used");
3668 } else if (addr
== 0) {
3669 if (target
->type
->add_context_breakpoint
== NULL
) {
3670 LOG_WARNING("Context breakpoint not available");
3673 retval
= context_breakpoint_add(target
, asid
, length
, hw
);
3674 if (ERROR_OK
== retval
)
3675 command_print(cmd_ctx
, "Context breakpoint set at 0x%8.8" PRIx32
"", asid
);
3677 LOG_ERROR("Failure setting breakpoint, the same address(CONTEXTID) is already used");
3681 if (target
->type
->add_hybrid_breakpoint
== NULL
) {
3682 LOG_WARNING("Hybrid breakpoint not available");
3685 retval
= hybrid_breakpoint_add(target
, addr
, asid
, length
, hw
);
3686 if (ERROR_OK
== retval
)
3687 command_print(cmd_ctx
, "Hybrid breakpoint set at 0x%8.8" PRIx32
"", asid
);
3689 LOG_ERROR("Failure setting breakpoint, the same address is already used");
3696 COMMAND_HANDLER(handle_bp_command
)
3705 return handle_bp_command_list(CMD_CTX
);
3709 COMMAND_PARSE_ADDRESS(CMD_ARGV
[0], addr
);
3710 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], length
);
3711 return handle_bp_command_set(CMD_CTX
, addr
, asid
, length
, hw
);
3714 if (strcmp(CMD_ARGV
[2], "hw") == 0) {
3716 COMMAND_PARSE_ADDRESS(CMD_ARGV
[0], addr
);
3717 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], length
);
3719 return handle_bp_command_set(CMD_CTX
, addr
, asid
, length
, hw
);
3720 } else if (strcmp(CMD_ARGV
[2], "hw_ctx") == 0) {
3722 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], asid
);
3723 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], length
);
3725 return handle_bp_command_set(CMD_CTX
, addr
, asid
, length
, hw
);
3730 COMMAND_PARSE_ADDRESS(CMD_ARGV
[0], addr
);
3731 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], asid
);
3732 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[2], length
);
3733 return handle_bp_command_set(CMD_CTX
, addr
, asid
, length
, hw
);
3736 return ERROR_COMMAND_SYNTAX_ERROR
;
3740 COMMAND_HANDLER(handle_rbp_command
)
3743 return ERROR_COMMAND_SYNTAX_ERROR
;
3746 COMMAND_PARSE_ADDRESS(CMD_ARGV
[0], addr
);
3748 struct target
*target
= get_current_target(CMD_CTX
);
3749 breakpoint_remove(target
, addr
);
3754 COMMAND_HANDLER(handle_wp_command
)
3756 struct target
*target
= get_current_target(CMD_CTX
);
3758 if (CMD_ARGC
== 0) {
3759 struct watchpoint
*watchpoint
= target
->watchpoints
;
3761 while (watchpoint
) {
3762 command_print(CMD_CTX
, "address: " TARGET_ADDR_FMT
3763 ", len: 0x%8.8" PRIx32
3764 ", r/w/a: %i, value: 0x%8.8" PRIx32
3765 ", mask: 0x%8.8" PRIx32
,
3766 watchpoint
->address
,
3768 (int)watchpoint
->rw
,
3771 watchpoint
= watchpoint
->next
;
3776 enum watchpoint_rw type
= WPT_ACCESS
;
3778 uint32_t length
= 0;
3779 uint32_t data_value
= 0x0;
3780 uint32_t data_mask
= 0xffffffff;
3784 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[4], data_mask
);
3787 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[3], data_value
);
3790 switch (CMD_ARGV
[2][0]) {
3801 LOG_ERROR("invalid watchpoint mode ('%c')", CMD_ARGV
[2][0]);
3802 return ERROR_COMMAND_SYNTAX_ERROR
;
3806 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], length
);
3807 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], addr
);
3811 return ERROR_COMMAND_SYNTAX_ERROR
;
3814 int retval
= watchpoint_add(target
, addr
, length
, type
,
3815 data_value
, data_mask
);
3816 if (ERROR_OK
!= retval
)
3817 LOG_ERROR("Failure setting watchpoints");
3822 COMMAND_HANDLER(handle_rwp_command
)
3825 return ERROR_COMMAND_SYNTAX_ERROR
;
3828 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], addr
);
3830 struct target
*target
= get_current_target(CMD_CTX
);
3831 watchpoint_remove(target
, addr
);
3837 * Translate a virtual address to a physical address.
3839 * The low-level target implementation must have logged a detailed error
3840 * which is forwarded to telnet/GDB session.
3842 COMMAND_HANDLER(handle_virt2phys_command
)
3845 return ERROR_COMMAND_SYNTAX_ERROR
;
3848 COMMAND_PARSE_ADDRESS(CMD_ARGV
[0], va
);
3851 struct target
*target
= get_current_target(CMD_CTX
);
3852 int retval
= target
->type
->virt2phys(target
, va
, &pa
);
3853 if (retval
== ERROR_OK
)
3854 command_print(CMD_CTX
, "Physical address " TARGET_ADDR_FMT
"", pa
);
3859 static void writeData(FILE *f
, const void *data
, size_t len
)
3861 size_t written
= fwrite(data
, 1, len
, f
);
3863 LOG_ERROR("failed to write %zu bytes: %s", len
, strerror(errno
));
3866 static void writeLong(FILE *f
, int l
, struct target
*target
)
3870 target_buffer_set_u32(target
, val
, l
);
3871 writeData(f
, val
, 4);
3874 static void writeString(FILE *f
, char *s
)
3876 writeData(f
, s
, strlen(s
));
3879 typedef unsigned char UNIT
[2]; /* unit of profiling */
3881 /* Dump a gmon.out histogram file. */
3882 static void write_gmon(uint32_t *samples
, uint32_t sampleNum
, const char *filename
, bool with_range
,
3883 uint32_t start_address
, uint32_t end_address
, struct target
*target
, uint32_t duration_ms
)
3886 FILE *f
= fopen(filename
, "w");
3889 writeString(f
, "gmon");
3890 writeLong(f
, 0x00000001, target
); /* Version */
3891 writeLong(f
, 0, target
); /* padding */
3892 writeLong(f
, 0, target
); /* padding */
3893 writeLong(f
, 0, target
); /* padding */
3895 uint8_t zero
= 0; /* GMON_TAG_TIME_HIST */
3896 writeData(f
, &zero
, 1);
3898 /* figure out bucket size */
3902 min
= start_address
;
3907 for (i
= 0; i
< sampleNum
; i
++) {
3908 if (min
> samples
[i
])
3910 if (max
< samples
[i
])
3914 /* max should be (largest sample + 1)
3915 * Refer to binutils/gprof/hist.c (find_histogram_for_pc) */
3919 int addressSpace
= max
- min
;
3920 assert(addressSpace
>= 2);
3922 /* FIXME: What is the reasonable number of buckets?
3923 * The profiling result will be more accurate if there are enough buckets. */
3924 static const uint32_t maxBuckets
= 128 * 1024; /* maximum buckets. */
3925 uint32_t numBuckets
= addressSpace
/ sizeof(UNIT
);
3926 if (numBuckets
> maxBuckets
)
3927 numBuckets
= maxBuckets
;
3928 int *buckets
= malloc(sizeof(int) * numBuckets
);
3929 if (buckets
== NULL
) {
3933 memset(buckets
, 0, sizeof(int) * numBuckets
);
3934 for (i
= 0; i
< sampleNum
; i
++) {
3935 uint32_t address
= samples
[i
];
3937 if ((address
< min
) || (max
<= address
))
3940 long long a
= address
- min
;
3941 long long b
= numBuckets
;
3942 long long c
= addressSpace
;
3943 int index_t
= (a
* b
) / c
; /* danger!!!! int32 overflows */
3947 /* append binary memory gmon.out &profile_hist_hdr ((char*)&profile_hist_hdr + sizeof(struct gmon_hist_hdr)) */
3948 writeLong(f
, min
, target
); /* low_pc */
3949 writeLong(f
, max
, target
); /* high_pc */
3950 writeLong(f
, numBuckets
, target
); /* # of buckets */
3951 float sample_rate
= sampleNum
/ (duration_ms
/ 1000.0);
3952 writeLong(f
, sample_rate
, target
);
3953 writeString(f
, "seconds");
3954 for (i
= 0; i
< (15-strlen("seconds")); i
++)
3955 writeData(f
, &zero
, 1);
3956 writeString(f
, "s");
3958 /*append binary memory gmon.out profile_hist_data (profile_hist_data + profile_hist_hdr.hist_size) */
3960 char *data
= malloc(2 * numBuckets
);
3962 for (i
= 0; i
< numBuckets
; i
++) {
3967 data
[i
* 2] = val
&0xff;
3968 data
[i
* 2 + 1] = (val
>> 8) & 0xff;
3971 writeData(f
, data
, numBuckets
* 2);
3979 /* profiling samples the CPU PC as quickly as OpenOCD is able,
3980 * which will be used as a random sampling of PC */
3981 COMMAND_HANDLER(handle_profile_command
)
3983 struct target
*target
= get_current_target(CMD_CTX
);
3985 if ((CMD_ARGC
!= 2) && (CMD_ARGC
!= 4))
3986 return ERROR_COMMAND_SYNTAX_ERROR
;
3988 const uint32_t MAX_PROFILE_SAMPLE_NUM
= 10000;
3990 uint32_t num_of_samples
;
3991 int retval
= ERROR_OK
;
3993 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], offset
);
3995 uint32_t *samples
= malloc(sizeof(uint32_t) * MAX_PROFILE_SAMPLE_NUM
);
3996 if (samples
== NULL
) {
3997 LOG_ERROR("No memory to store samples.");
4001 uint64_t timestart_ms
= timeval_ms();
4003 * Some cores let us sample the PC without the
4004 * annoying halt/resume step; for example, ARMv7 PCSR.
4005 * Provide a way to use that more efficient mechanism.
4007 retval
= target_profiling(target
, samples
, MAX_PROFILE_SAMPLE_NUM
,
4008 &num_of_samples
, offset
);
4009 if (retval
!= ERROR_OK
) {
4013 uint32_t duration_ms
= timeval_ms() - timestart_ms
;
4015 assert(num_of_samples
<= MAX_PROFILE_SAMPLE_NUM
);
4017 retval
= target_poll(target
);
4018 if (retval
!= ERROR_OK
) {
4022 if (target
->state
== TARGET_RUNNING
) {
4023 retval
= target_halt(target
);
4024 if (retval
!= ERROR_OK
) {
4030 retval
= target_poll(target
);
4031 if (retval
!= ERROR_OK
) {
4036 uint32_t start_address
= 0;
4037 uint32_t end_address
= 0;
4038 bool with_range
= false;
4039 if (CMD_ARGC
== 4) {
4041 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[2], start_address
);
4042 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[3], end_address
);
4045 write_gmon(samples
, num_of_samples
, CMD_ARGV
[1],
4046 with_range
, start_address
, end_address
, target
, duration_ms
);
4047 command_print(CMD_CTX
, "Wrote %s", CMD_ARGV
[1]);
4053 static int new_int_array_element(Jim_Interp
*interp
, const char *varname
, int idx
, uint32_t val
)
4056 Jim_Obj
*nameObjPtr
, *valObjPtr
;
4059 namebuf
= alloc_printf("%s(%d)", varname
, idx
);
4063 nameObjPtr
= Jim_NewStringObj(interp
, namebuf
, -1);
4064 valObjPtr
= Jim_NewIntObj(interp
, val
);
4065 if (!nameObjPtr
|| !valObjPtr
) {
4070 Jim_IncrRefCount(nameObjPtr
);
4071 Jim_IncrRefCount(valObjPtr
);
4072 result
= Jim_SetVariable(interp
, nameObjPtr
, valObjPtr
);
4073 Jim_DecrRefCount(interp
, nameObjPtr
);
4074 Jim_DecrRefCount(interp
, valObjPtr
);
4076 /* printf("%s(%d) <= 0%08x\n", varname, idx, val); */
4080 static int jim_mem2array(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4082 struct command_context
*context
;
4083 struct target
*target
;
4085 context
= current_command_context(interp
);
4086 assert(context
!= NULL
);
4088 target
= get_current_target(context
);
4089 if (target
== NULL
) {
4090 LOG_ERROR("mem2array: no current target");
4094 return target_mem2array(interp
, target
, argc
- 1, argv
+ 1);
4097 static int target_mem2array(Jim_Interp
*interp
, struct target
*target
, int argc
, Jim_Obj
*const *argv
)
4105 const char *varname
;
4111 /* argv[1] = name of array to receive the data
4112 * argv[2] = desired width
4113 * argv[3] = memory address
4114 * argv[4] = count of times to read
4116 if (argc
< 4 || argc
> 5) {
4117 Jim_WrongNumArgs(interp
, 1, argv
, "varname width addr nelems [phys]");
4120 varname
= Jim_GetString(argv
[0], &len
);
4121 /* given "foo" get space for worse case "foo(%d)" .. add 20 */
4123 e
= Jim_GetLong(interp
, argv
[1], &l
);
4128 e
= Jim_GetLong(interp
, argv
[2], &l
);
4132 e
= Jim_GetLong(interp
, argv
[3], &l
);
4138 phys
= Jim_GetString(argv
[4], &n
);
4139 if (!strncmp(phys
, "phys", n
))
4155 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
4156 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "Invalid width param, must be 8/16/32", NULL
);
4160 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
4161 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "mem2array: zero width read?", NULL
);
4164 if ((addr
+ (len
* width
)) < addr
) {
4165 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
4166 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "mem2array: addr + len - wraps to zero?", NULL
);
4169 /* absurd transfer size? */
4171 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
4172 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "mem2array: absurd > 64K item request", NULL
);
4177 ((width
== 2) && ((addr
& 1) == 0)) ||
4178 ((width
== 4) && ((addr
& 3) == 0))) {
4182 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
4183 sprintf(buf
, "mem2array address: 0x%08" PRIx32
" is not aligned for %" PRId32
" byte reads",
4186 Jim_AppendStrings(interp
, Jim_GetResult(interp
), buf
, NULL
);
4195 size_t buffersize
= 4096;
4196 uint8_t *buffer
= malloc(buffersize
);
4203 /* Slurp... in buffer size chunks */
4205 count
= len
; /* in objects.. */
4206 if (count
> (buffersize
/ width
))
4207 count
= (buffersize
/ width
);
4210 retval
= target_read_phys_memory(target
, addr
, width
, count
, buffer
);
4212 retval
= target_read_memory(target
, addr
, width
, count
, buffer
);
4213 if (retval
!= ERROR_OK
) {
4215 LOG_ERROR("mem2array: Read @ 0x%08" PRIx32
", w=%" PRId32
", cnt=%" PRId32
", failed",
4219 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
4220 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "mem2array: cannot read memory", NULL
);
4224 v
= 0; /* shut up gcc */
4225 for (i
= 0; i
< count
; i
++, n
++) {
4228 v
= target_buffer_get_u32(target
, &buffer
[i
*width
]);
4231 v
= target_buffer_get_u16(target
, &buffer
[i
*width
]);
4234 v
= buffer
[i
] & 0x0ff;
4237 new_int_array_element(interp
, varname
, n
, v
);
4240 addr
+= count
* width
;
4246 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
4251 static int get_int_array_element(Jim_Interp
*interp
, const char *varname
, int idx
, uint32_t *val
)
4254 Jim_Obj
*nameObjPtr
, *valObjPtr
;
4258 namebuf
= alloc_printf("%s(%d)", varname
, idx
);
4262 nameObjPtr
= Jim_NewStringObj(interp
, namebuf
, -1);
4268 Jim_IncrRefCount(nameObjPtr
);
4269 valObjPtr
= Jim_GetVariable(interp
, nameObjPtr
, JIM_ERRMSG
);
4270 Jim_DecrRefCount(interp
, nameObjPtr
);
4272 if (valObjPtr
== NULL
)
4275 result
= Jim_GetLong(interp
, valObjPtr
, &l
);
4276 /* printf("%s(%d) => 0%08x\n", varname, idx, val); */
4281 static int jim_array2mem(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4283 struct command_context
*context
;
4284 struct target
*target
;
4286 context
= current_command_context(interp
);
4287 assert(context
!= NULL
);
4289 target
= get_current_target(context
);
4290 if (target
== NULL
) {
4291 LOG_ERROR("array2mem: no current target");
4295 return target_array2mem(interp
, target
, argc
-1, argv
+ 1);
4298 static int target_array2mem(Jim_Interp
*interp
, struct target
*target
,
4299 int argc
, Jim_Obj
*const *argv
)
4307 const char *varname
;
4313 /* argv[1] = name of array to get the data
4314 * argv[2] = desired width
4315 * argv[3] = memory address
4316 * argv[4] = count to write
4318 if (argc
< 4 || argc
> 5) {
4319 Jim_WrongNumArgs(interp
, 0, argv
, "varname width addr nelems [phys]");
4322 varname
= Jim_GetString(argv
[0], &len
);
4323 /* given "foo" get space for worse case "foo(%d)" .. add 20 */
4325 e
= Jim_GetLong(interp
, argv
[1], &l
);
4330 e
= Jim_GetLong(interp
, argv
[2], &l
);
4334 e
= Jim_GetLong(interp
, argv
[3], &l
);
4340 phys
= Jim_GetString(argv
[4], &n
);
4341 if (!strncmp(phys
, "phys", n
))
4357 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
4358 Jim_AppendStrings(interp
, Jim_GetResult(interp
),
4359 "Invalid width param, must be 8/16/32", NULL
);
4363 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
4364 Jim_AppendStrings(interp
, Jim_GetResult(interp
),
4365 "array2mem: zero width read?", NULL
);
4368 if ((addr
+ (len
* width
)) < addr
) {
4369 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
4370 Jim_AppendStrings(interp
, Jim_GetResult(interp
),
4371 "array2mem: addr + len - wraps to zero?", NULL
);
4374 /* absurd transfer size? */
4376 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
4377 Jim_AppendStrings(interp
, Jim_GetResult(interp
),
4378 "array2mem: absurd > 64K item request", NULL
);
4383 ((width
== 2) && ((addr
& 1) == 0)) ||
4384 ((width
== 4) && ((addr
& 3) == 0))) {
4388 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
4389 sprintf(buf
, "array2mem address: 0x%08" PRIx32
" is not aligned for %" PRId32
" byte reads",
4392 Jim_AppendStrings(interp
, Jim_GetResult(interp
), buf
, NULL
);
4403 size_t buffersize
= 4096;
4404 uint8_t *buffer
= malloc(buffersize
);
4409 /* Slurp... in buffer size chunks */
4411 count
= len
; /* in objects.. */
4412 if (count
> (buffersize
/ width
))
4413 count
= (buffersize
/ width
);
4415 v
= 0; /* shut up gcc */
4416 for (i
= 0; i
< count
; i
++, n
++) {
4417 get_int_array_element(interp
, varname
, n
, &v
);
4420 target_buffer_set_u32(target
, &buffer
[i
* width
], v
);
4423 target_buffer_set_u16(target
, &buffer
[i
* width
], v
);
4426 buffer
[i
] = v
& 0x0ff;
4433 retval
= target_write_phys_memory(target
, addr
, width
, count
, buffer
);
4435 retval
= target_write_memory(target
, addr
, width
, count
, buffer
);
4436 if (retval
!= ERROR_OK
) {
4438 LOG_ERROR("array2mem: Write @ 0x%08" PRIx32
", w=%" PRId32
", cnt=%" PRId32
", failed",
4442 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
4443 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "array2mem: cannot read memory", NULL
);
4447 addr
+= count
* width
;
4452 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
4457 /* FIX? should we propagate errors here rather than printing them
4460 void target_handle_event(struct target
*target
, enum target_event e
)
4462 struct target_event_action
*teap
;
4464 for (teap
= target
->event_action
; teap
!= NULL
; teap
= teap
->next
) {
4465 if (teap
->event
== e
) {
4466 LOG_DEBUG("target(%d): %s (%s) event: %d (%s) action: %s",
4467 target
->target_number
,
4468 target_name(target
),
4469 target_type_name(target
),
4471 Jim_Nvp_value2name_simple(nvp_target_event
, e
)->name
,
4472 Jim_GetString(teap
->body
, NULL
));
4474 /* Override current target by the target an event
4475 * is issued from (lot of scripts need it).
4476 * Return back to previous override as soon
4477 * as the handler processing is done */
4478 struct command_context
*cmd_ctx
= current_command_context(teap
->interp
);
4479 struct target
*saved_target_override
= cmd_ctx
->current_target_override
;
4480 cmd_ctx
->current_target_override
= target
;
4482 if (Jim_EvalObj(teap
->interp
, teap
->body
) != JIM_OK
) {
4483 Jim_MakeErrorMessage(teap
->interp
);
4484 command_print(NULL
, "%s\n", Jim_GetString(Jim_GetResult(teap
->interp
), NULL
));
4487 cmd_ctx
->current_target_override
= saved_target_override
;
4493 * Returns true only if the target has a handler for the specified event.
4495 bool target_has_event_action(struct target
*target
, enum target_event event
)
4497 struct target_event_action
*teap
;
4499 for (teap
= target
->event_action
; teap
!= NULL
; teap
= teap
->next
) {
4500 if (teap
->event
== event
)
4506 enum target_cfg_param
{
4509 TCFG_WORK_AREA_VIRT
,
4510 TCFG_WORK_AREA_PHYS
,
4511 TCFG_WORK_AREA_SIZE
,
4512 TCFG_WORK_AREA_BACKUP
,
4515 TCFG_CHAIN_POSITION
,
4521 static Jim_Nvp nvp_config_opts
[] = {
4522 { .name
= "-type", .value
= TCFG_TYPE
},
4523 { .name
= "-event", .value
= TCFG_EVENT
},
4524 { .name
= "-work-area-virt", .value
= TCFG_WORK_AREA_VIRT
},
4525 { .name
= "-work-area-phys", .value
= TCFG_WORK_AREA_PHYS
},
4526 { .name
= "-work-area-size", .value
= TCFG_WORK_AREA_SIZE
},
4527 { .name
= "-work-area-backup", .value
= TCFG_WORK_AREA_BACKUP
},
4528 { .name
= "-endian" , .value
= TCFG_ENDIAN
},
4529 { .name
= "-coreid", .value
= TCFG_COREID
},
4530 { .name
= "-chain-position", .value
= TCFG_CHAIN_POSITION
},
4531 { .name
= "-dbgbase", .value
= TCFG_DBGBASE
},
4532 { .name
= "-rtos", .value
= TCFG_RTOS
},
4533 { .name
= "-defer-examine", .value
= TCFG_DEFER_EXAMINE
},
4534 { .name
= NULL
, .value
= -1 }
4537 static int target_configure(Jim_GetOptInfo
*goi
, struct target
*target
)
4544 /* parse config or cget options ... */
4545 while (goi
->argc
> 0) {
4546 Jim_SetEmptyResult(goi
->interp
);
4547 /* Jim_GetOpt_Debug(goi); */
4549 if (target
->type
->target_jim_configure
) {
4550 /* target defines a configure function */
4551 /* target gets first dibs on parameters */
4552 e
= (*(target
->type
->target_jim_configure
))(target
, goi
);
4561 /* otherwise we 'continue' below */
4563 e
= Jim_GetOpt_Nvp(goi
, nvp_config_opts
, &n
);
4565 Jim_GetOpt_NvpUnknown(goi
, nvp_config_opts
, 0);
4571 if (goi
->isconfigure
) {
4572 Jim_SetResultFormatted(goi
->interp
,
4573 "not settable: %s", n
->name
);
4577 if (goi
->argc
!= 0) {
4578 Jim_WrongNumArgs(goi
->interp
,
4579 goi
->argc
, goi
->argv
,
4584 Jim_SetResultString(goi
->interp
,
4585 target_type_name(target
), -1);
4589 if (goi
->argc
== 0) {
4590 Jim_WrongNumArgs(goi
->interp
, goi
->argc
, goi
->argv
, "-event ?event-name? ...");
4594 e
= Jim_GetOpt_Nvp(goi
, nvp_target_event
, &n
);
4596 Jim_GetOpt_NvpUnknown(goi
, nvp_target_event
, 1);
4600 if (goi
->isconfigure
) {
4601 if (goi
->argc
!= 1) {
4602 Jim_WrongNumArgs(goi
->interp
, goi
->argc
, goi
->argv
, "-event ?event-name? ?EVENT-BODY?");
4606 if (goi
->argc
!= 0) {
4607 Jim_WrongNumArgs(goi
->interp
, goi
->argc
, goi
->argv
, "-event ?event-name?");
4613 struct target_event_action
*teap
;
4615 teap
= target
->event_action
;
4616 /* replace existing? */
4618 if (teap
->event
== (enum target_event
)n
->value
)
4623 if (goi
->isconfigure
) {
4624 bool replace
= true;
4627 teap
= calloc(1, sizeof(*teap
));
4630 teap
->event
= n
->value
;
4631 teap
->interp
= goi
->interp
;
4632 Jim_GetOpt_Obj(goi
, &o
);
4634 Jim_DecrRefCount(teap
->interp
, teap
->body
);
4635 teap
->body
= Jim_DuplicateObj(goi
->interp
, o
);
4638 * Tcl/TK - "tk events" have a nice feature.
4639 * See the "BIND" command.
4640 * We should support that here.
4641 * You can specify %X and %Y in the event code.
4642 * The idea is: %T - target name.
4643 * The idea is: %N - target number
4644 * The idea is: %E - event name.
4646 Jim_IncrRefCount(teap
->body
);
4649 /* add to head of event list */
4650 teap
->next
= target
->event_action
;
4651 target
->event_action
= teap
;
4653 Jim_SetEmptyResult(goi
->interp
);
4657 Jim_SetEmptyResult(goi
->interp
);
4659 Jim_SetResult(goi
->interp
, Jim_DuplicateObj(goi
->interp
, teap
->body
));
4665 case TCFG_WORK_AREA_VIRT
:
4666 if (goi
->isconfigure
) {
4667 target_free_all_working_areas(target
);
4668 e
= Jim_GetOpt_Wide(goi
, &w
);
4671 target
->working_area_virt
= w
;
4672 target
->working_area_virt_spec
= true;
4677 Jim_SetResult(goi
->interp
, Jim_NewIntObj(goi
->interp
, target
->working_area_virt
));
4681 case TCFG_WORK_AREA_PHYS
:
4682 if (goi
->isconfigure
) {
4683 target_free_all_working_areas(target
);
4684 e
= Jim_GetOpt_Wide(goi
, &w
);
4687 target
->working_area_phys
= w
;
4688 target
->working_area_phys_spec
= true;
4693 Jim_SetResult(goi
->interp
, Jim_NewIntObj(goi
->interp
, target
->working_area_phys
));
4697 case TCFG_WORK_AREA_SIZE
:
4698 if (goi
->isconfigure
) {
4699 target_free_all_working_areas(target
);
4700 e
= Jim_GetOpt_Wide(goi
, &w
);
4703 target
->working_area_size
= w
;
4708 Jim_SetResult(goi
->interp
, Jim_NewIntObj(goi
->interp
, target
->working_area_size
));
4712 case TCFG_WORK_AREA_BACKUP
:
4713 if (goi
->isconfigure
) {
4714 target_free_all_working_areas(target
);
4715 e
= Jim_GetOpt_Wide(goi
, &w
);
4718 /* make this exactly 1 or 0 */
4719 target
->backup_working_area
= (!!w
);
4724 Jim_SetResult(goi
->interp
, Jim_NewIntObj(goi
->interp
, target
->backup_working_area
));
4725 /* loop for more e*/
4730 if (goi
->isconfigure
) {
4731 e
= Jim_GetOpt_Nvp(goi
, nvp_target_endian
, &n
);
4733 Jim_GetOpt_NvpUnknown(goi
, nvp_target_endian
, 1);
4736 target
->endianness
= n
->value
;
4741 n
= Jim_Nvp_value2name_simple(nvp_target_endian
, target
->endianness
);
4742 if (n
->name
== NULL
) {
4743 target
->endianness
= TARGET_LITTLE_ENDIAN
;
4744 n
= Jim_Nvp_value2name_simple(nvp_target_endian
, target
->endianness
);
4746 Jim_SetResultString(goi
->interp
, n
->name
, -1);
4751 if (goi
->isconfigure
) {
4752 e
= Jim_GetOpt_Wide(goi
, &w
);
4755 target
->coreid
= (int32_t)w
;
4760 Jim_SetResult(goi
->interp
, Jim_NewIntObj(goi
->interp
, target
->working_area_size
));
4764 case TCFG_CHAIN_POSITION
:
4765 if (goi
->isconfigure
) {
4767 struct jtag_tap
*tap
;
4769 if (target
->has_dap
) {
4770 Jim_SetResultString(goi
->interp
,
4771 "target requires -dap parameter instead of -chain-position!", -1);
4775 target_free_all_working_areas(target
);
4776 e
= Jim_GetOpt_Obj(goi
, &o_t
);
4779 tap
= jtag_tap_by_jim_obj(goi
->interp
, o_t
);
4783 target
->tap_configured
= true;
4788 Jim_SetResultString(goi
->interp
, target
->tap
->dotted_name
, -1);
4789 /* loop for more e*/
4792 if (goi
->isconfigure
) {
4793 e
= Jim_GetOpt_Wide(goi
, &w
);
4796 target
->dbgbase
= (uint32_t)w
;
4797 target
->dbgbase_set
= true;
4802 Jim_SetResult(goi
->interp
, Jim_NewIntObj(goi
->interp
, target
->dbgbase
));
4808 int result
= rtos_create(goi
, target
);
4809 if (result
!= JIM_OK
)
4815 case TCFG_DEFER_EXAMINE
:
4817 target
->defer_examine
= true;
4822 } /* while (goi->argc) */
4825 /* done - we return */
4829 static int jim_target_configure(Jim_Interp
*interp
, int argc
, Jim_Obj
* const *argv
)
4833 Jim_GetOpt_Setup(&goi
, interp
, argc
- 1, argv
+ 1);
4834 goi
.isconfigure
= !strcmp(Jim_GetString(argv
[0], NULL
), "configure");
4836 Jim_WrongNumArgs(goi
.interp
, goi
.argc
, goi
.argv
,
4837 "missing: -option ...");
4840 struct target
*target
= Jim_CmdPrivData(goi
.interp
);
4841 return target_configure(&goi
, target
);
4844 static int jim_target_mw(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4846 const char *cmd_name
= Jim_GetString(argv
[0], NULL
);
4849 Jim_GetOpt_Setup(&goi
, interp
, argc
- 1, argv
+ 1);
4851 if (goi
.argc
< 2 || goi
.argc
> 4) {
4852 Jim_SetResultFormatted(goi
.interp
,
4853 "usage: %s [phys] <address> <data> [<count>]", cmd_name
);
4858 fn
= target_write_memory
;
4861 if (strcmp(Jim_GetString(argv
[1], NULL
), "phys") == 0) {
4863 struct Jim_Obj
*obj
;
4864 e
= Jim_GetOpt_Obj(&goi
, &obj
);
4868 fn
= target_write_phys_memory
;
4872 e
= Jim_GetOpt_Wide(&goi
, &a
);
4877 e
= Jim_GetOpt_Wide(&goi
, &b
);
4882 if (goi
.argc
== 1) {
4883 e
= Jim_GetOpt_Wide(&goi
, &c
);
4888 /* all args must be consumed */
4892 struct target
*target
= Jim_CmdPrivData(goi
.interp
);
4894 if (strcasecmp(cmd_name
, "mww") == 0)
4896 else if (strcasecmp(cmd_name
, "mwh") == 0)
4898 else if (strcasecmp(cmd_name
, "mwb") == 0)
4901 LOG_ERROR("command '%s' unknown: ", cmd_name
);
4905 return (target_fill_mem(target
, a
, fn
, data_size
, b
, c
) == ERROR_OK
) ? JIM_OK
: JIM_ERR
;
4909 * @brief Reads an array of words/halfwords/bytes from target memory starting at specified address.
4911 * Usage: mdw [phys] <address> [<count>] - for 32 bit reads
4912 * mdh [phys] <address> [<count>] - for 16 bit reads
4913 * mdb [phys] <address> [<count>] - for 8 bit reads
4915 * Count defaults to 1.
4917 * Calls target_read_memory or target_read_phys_memory depending on
4918 * the presence of the "phys" argument
4919 * Reads the target memory in blocks of max. 32 bytes, and returns an array of ints formatted
4920 * to int representation in base16.
4921 * Also outputs read data in a human readable form using command_print
4923 * @param phys if present target_read_phys_memory will be used instead of target_read_memory
4924 * @param address address where to start the read. May be specified in decimal or hex using the standard "0x" prefix
4925 * @param count optional count parameter to read an array of values. If not specified, defaults to 1.
4926 * @returns: JIM_ERR on error or JIM_OK on success and sets the result string to an array of ascii formatted numbers
4927 * on success, with [<count>] number of elements.
4929 * In case of little endian target:
4930 * Example1: "mdw 0x00000000" returns "10123456"
4931 * Exmaple2: "mdh 0x00000000 1" returns "3456"
4932 * Example3: "mdb 0x00000000" returns "56"
4933 * Example4: "mdh 0x00000000 2" returns "3456 1012"
4934 * Example5: "mdb 0x00000000 3" returns "56 34 12"
4936 static int jim_target_md(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4938 const char *cmd_name
= Jim_GetString(argv
[0], NULL
);
4941 Jim_GetOpt_Setup(&goi
, interp
, argc
- 1, argv
+ 1);
4943 if ((goi
.argc
< 1) || (goi
.argc
> 3)) {
4944 Jim_SetResultFormatted(goi
.interp
,
4945 "usage: %s [phys] <address> [<count>]", cmd_name
);
4949 int (*fn
)(struct target
*target
,
4950 target_addr_t address
, uint32_t size
, uint32_t count
, uint8_t *buffer
);
4951 fn
= target_read_memory
;
4954 if (strcmp(Jim_GetString(argv
[1], NULL
), "phys") == 0) {
4956 struct Jim_Obj
*obj
;
4957 e
= Jim_GetOpt_Obj(&goi
, &obj
);
4961 fn
= target_read_phys_memory
;
4964 /* Read address parameter */
4966 e
= Jim_GetOpt_Wide(&goi
, &addr
);
4970 /* If next parameter exists, read it out as the count parameter, if not, set it to 1 (default) */
4972 if (goi
.argc
== 1) {
4973 e
= Jim_GetOpt_Wide(&goi
, &count
);
4979 /* all args must be consumed */
4983 jim_wide dwidth
= 1; /* shut up gcc */
4984 if (strcasecmp(cmd_name
, "mdw") == 0)
4986 else if (strcasecmp(cmd_name
, "mdh") == 0)
4988 else if (strcasecmp(cmd_name
, "mdb") == 0)
4991 LOG_ERROR("command '%s' unknown: ", cmd_name
);
4995 /* convert count to "bytes" */
4996 int bytes
= count
* dwidth
;
4998 struct target
*target
= Jim_CmdPrivData(goi
.interp
);
4999 uint8_t target_buf
[32];
5002 y
= (bytes
< 16) ? bytes
: 16; /* y = min(bytes, 16); */
5004 /* Try to read out next block */
5005 e
= fn(target
, addr
, dwidth
, y
/ dwidth
, target_buf
);
5007 if (e
!= ERROR_OK
) {
5008 Jim_SetResultFormatted(interp
, "error reading target @ 0x%08lx", (long)addr
);
5012 command_print_sameline(NULL
, "0x%08x ", (int)(addr
));
5015 for (x
= 0; x
< 16 && x
< y
; x
+= 4) {
5016 z
= target_buffer_get_u32(target
, &(target_buf
[x
]));
5017 command_print_sameline(NULL
, "%08x ", (int)(z
));
5019 for (; (x
< 16) ; x
+= 4)
5020 command_print_sameline(NULL
, " ");
5023 for (x
= 0; x
< 16 && x
< y
; x
+= 2) {
5024 z
= target_buffer_get_u16(target
, &(target_buf
[x
]));
5025 command_print_sameline(NULL
, "%04x ", (int)(z
));
5027 for (; (x
< 16) ; x
+= 2)
5028 command_print_sameline(NULL
, " ");
5032 for (x
= 0 ; (x
< 16) && (x
< y
) ; x
+= 1) {
5033 z
= target_buffer_get_u8(target
, &(target_buf
[x
]));
5034 command_print_sameline(NULL
, "%02x ", (int)(z
));
5036 for (; (x
< 16) ; x
+= 1)
5037 command_print_sameline(NULL
, " ");
5040 /* ascii-ify the bytes */
5041 for (x
= 0 ; x
< y
; x
++) {
5042 if ((target_buf
[x
] >= 0x20) &&
5043 (target_buf
[x
] <= 0x7e)) {
5047 target_buf
[x
] = '.';
5052 target_buf
[x
] = ' ';
5057 /* print - with a newline */
5058 command_print_sameline(NULL
, "%s\n", target_buf
);
5066 static int jim_target_mem2array(Jim_Interp
*interp
,
5067 int argc
, Jim_Obj
*const *argv
)
5069 struct target
*target
= Jim_CmdPrivData(interp
);
5070 return target_mem2array(interp
, target
, argc
- 1, argv
+ 1);
5073 static int jim_target_array2mem(Jim_Interp
*interp
,
5074 int argc
, Jim_Obj
*const *argv
)
5076 struct target
*target
= Jim_CmdPrivData(interp
);
5077 return target_array2mem(interp
, target
, argc
- 1, argv
+ 1);
5080 static int jim_target_tap_disabled(Jim_Interp
*interp
)
5082 Jim_SetResultFormatted(interp
, "[TAP is disabled]");
5086 static int jim_target_examine(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
5088 bool allow_defer
= false;
5091 Jim_GetOpt_Setup(&goi
, interp
, argc
- 1, argv
+ 1);
5093 const char *cmd_name
= Jim_GetString(argv
[0], NULL
);
5094 Jim_SetResultFormatted(goi
.interp
,
5095 "usage: %s ['allow-defer']", cmd_name
);
5099 strcmp(Jim_GetString(argv
[1], NULL
), "allow-defer") == 0) {
5101 struct Jim_Obj
*obj
;
5102 int e
= Jim_GetOpt_Obj(&goi
, &obj
);
5108 struct target
*target
= Jim_CmdPrivData(interp
);
5109 if (!target
->tap
->enabled
)
5110 return jim_target_tap_disabled(interp
);
5112 if (allow_defer
&& target
->defer_examine
) {
5113 LOG_INFO("Deferring arp_examine of %s", target_name(target
));
5114 LOG_INFO("Use arp_examine command to examine it manually!");
5118 int e
= target
->type
->examine(target
);
5124 static int jim_target_was_examined(Jim_Interp
*interp
, int argc
, Jim_Obj
* const *argv
)
5126 struct target
*target
= Jim_CmdPrivData(interp
);
5128 Jim_SetResultBool(interp
, target_was_examined(target
));
5132 static int jim_target_examine_deferred(Jim_Interp
*interp
, int argc
, Jim_Obj
* const *argv
)
5134 struct target
*target
= Jim_CmdPrivData(interp
);
5136 Jim_SetResultBool(interp
, target
->defer_examine
);
5140 static int jim_target_halt_gdb(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
5143 Jim_WrongNumArgs(interp
, 1, argv
, "[no parameters]");
5146 struct target
*target
= Jim_CmdPrivData(interp
);
5148 if (target_call_event_callbacks(target
, TARGET_EVENT_GDB_HALT
) != ERROR_OK
)
5154 static int jim_target_poll(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
5157 Jim_WrongNumArgs(interp
, 1, argv
, "[no parameters]");
5160 struct target
*target
= Jim_CmdPrivData(interp
);
5161 if (!target
->tap
->enabled
)
5162 return jim_target_tap_disabled(interp
);
5165 if (!(target_was_examined(target
)))
5166 e
= ERROR_TARGET_NOT_EXAMINED
;
5168 e
= target
->type
->poll(target
);
5174 static int jim_target_reset(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
5177 Jim_GetOpt_Setup(&goi
, interp
, argc
- 1, argv
+ 1);
5179 if (goi
.argc
!= 2) {
5180 Jim_WrongNumArgs(interp
, 0, argv
,
5181 "([tT]|[fF]|assert|deassert) BOOL");
5186 int e
= Jim_GetOpt_Nvp(&goi
, nvp_assert
, &n
);
5188 Jim_GetOpt_NvpUnknown(&goi
, nvp_assert
, 1);
5191 /* the halt or not param */
5193 e
= Jim_GetOpt_Wide(&goi
, &a
);
5197 struct target
*target
= Jim_CmdPrivData(goi
.interp
);
5198 if (!target
->tap
->enabled
)
5199 return jim_target_tap_disabled(interp
);
5201 if (!target
->type
->assert_reset
|| !target
->type
->deassert_reset
) {
5202 Jim_SetResultFormatted(interp
,
5203 "No target-specific reset for %s",
5204 target_name(target
));
5208 if (target
->defer_examine
)
5209 target_reset_examined(target
);
5211 /* determine if we should halt or not. */
5212 target
->reset_halt
= !!a
;
5213 /* When this happens - all workareas are invalid. */
5214 target_free_all_working_areas_restore(target
, 0);
5217 if (n
->value
== NVP_ASSERT
)
5218 e
= target
->type
->assert_reset(target
);
5220 e
= target
->type
->deassert_reset(target
);
5221 return (e
== ERROR_OK
) ? JIM_OK
: JIM_ERR
;
5224 static int jim_target_halt(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
5227 Jim_WrongNumArgs(interp
, 1, argv
, "[no parameters]");
5230 struct target
*target
= Jim_CmdPrivData(interp
);
5231 if (!target
->tap
->enabled
)
5232 return jim_target_tap_disabled(interp
);
5233 int e
= target
->type
->halt(target
);
5234 return (e
== ERROR_OK
) ? JIM_OK
: JIM_ERR
;
5237 static int jim_target_wait_state(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
5240 Jim_GetOpt_Setup(&goi
, interp
, argc
- 1, argv
+ 1);
5242 /* params: <name> statename timeoutmsecs */
5243 if (goi
.argc
!= 2) {
5244 const char *cmd_name
= Jim_GetString(argv
[0], NULL
);
5245 Jim_SetResultFormatted(goi
.interp
,
5246 "%s <state_name> <timeout_in_msec>", cmd_name
);
5251 int e
= Jim_GetOpt_Nvp(&goi
, nvp_target_state
, &n
);
5253 Jim_GetOpt_NvpUnknown(&goi
, nvp_target_state
, 1);
5257 e
= Jim_GetOpt_Wide(&goi
, &a
);
5260 struct target
*target
= Jim_CmdPrivData(interp
);
5261 if (!target
->tap
->enabled
)
5262 return jim_target_tap_disabled(interp
);
5264 e
= target_wait_state(target
, n
->value
, a
);
5265 if (e
!= ERROR_OK
) {
5266 Jim_Obj
*eObj
= Jim_NewIntObj(interp
, e
);
5267 Jim_SetResultFormatted(goi
.interp
,
5268 "target: %s wait %s fails (%#s) %s",
5269 target_name(target
), n
->name
,
5270 eObj
, target_strerror_safe(e
));
5271 Jim_FreeNewObj(interp
, eObj
);
5276 /* List for human, Events defined for this target.
5277 * scripts/programs should use 'name cget -event NAME'
5279 static int jim_target_event_list(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
5281 struct command_context
*cmd_ctx
= current_command_context(interp
);
5282 assert(cmd_ctx
!= NULL
);
5284 struct target
*target
= Jim_CmdPrivData(interp
);
5285 struct target_event_action
*teap
= target
->event_action
;
5286 command_print(cmd_ctx
, "Event actions for target (%d) %s\n",
5287 target
->target_number
,
5288 target_name(target
));
5289 command_print(cmd_ctx
, "%-25s | Body", "Event");
5290 command_print(cmd_ctx
, "------------------------- | "
5291 "----------------------------------------");
5293 Jim_Nvp
*opt
= Jim_Nvp_value2name_simple(nvp_target_event
, teap
->event
);
5294 command_print(cmd_ctx
, "%-25s | %s",
5295 opt
->name
, Jim_GetString(teap
->body
, NULL
));
5298 command_print(cmd_ctx
, "***END***");
5301 static int jim_target_current_state(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
5304 Jim_WrongNumArgs(interp
, 1, argv
, "[no parameters]");
5307 struct target
*target
= Jim_CmdPrivData(interp
);
5308 Jim_SetResultString(interp
, target_state_name(target
), -1);
5311 static int jim_target_invoke_event(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
5314 Jim_GetOpt_Setup(&goi
, interp
, argc
- 1, argv
+ 1);
5315 if (goi
.argc
!= 1) {
5316 const char *cmd_name
= Jim_GetString(argv
[0], NULL
);
5317 Jim_SetResultFormatted(goi
.interp
, "%s <eventname>", cmd_name
);
5321 int e
= Jim_GetOpt_Nvp(&goi
, nvp_target_event
, &n
);
5323 Jim_GetOpt_NvpUnknown(&goi
, nvp_target_event
, 1);
5326 struct target
*target
= Jim_CmdPrivData(interp
);
5327 target_handle_event(target
, n
->value
);
5331 static const struct command_registration target_instance_command_handlers
[] = {
5333 .name
= "configure",
5334 .mode
= COMMAND_CONFIG
,
5335 .jim_handler
= jim_target_configure
,
5336 .help
= "configure a new target for use",
5337 .usage
= "[target_attribute ...]",
5341 .mode
= COMMAND_ANY
,
5342 .jim_handler
= jim_target_configure
,
5343 .help
= "returns the specified target attribute",
5344 .usage
= "target_attribute",
5348 .mode
= COMMAND_EXEC
,
5349 .jim_handler
= jim_target_mw
,
5350 .help
= "Write 32-bit word(s) to target memory",
5351 .usage
= "address data [count]",
5355 .mode
= COMMAND_EXEC
,
5356 .jim_handler
= jim_target_mw
,
5357 .help
= "Write 16-bit half-word(s) to target memory",
5358 .usage
= "address data [count]",
5362 .mode
= COMMAND_EXEC
,
5363 .jim_handler
= jim_target_mw
,
5364 .help
= "Write byte(s) to target memory",
5365 .usage
= "address data [count]",
5369 .mode
= COMMAND_EXEC
,
5370 .jim_handler
= jim_target_md
,
5371 .help
= "Display target memory as 32-bit words",
5372 .usage
= "address [count]",
5376 .mode
= COMMAND_EXEC
,
5377 .jim_handler
= jim_target_md
,
5378 .help
= "Display target memory as 16-bit half-words",
5379 .usage
= "address [count]",
5383 .mode
= COMMAND_EXEC
,
5384 .jim_handler
= jim_target_md
,
5385 .help
= "Display target memory as 8-bit bytes",
5386 .usage
= "address [count]",
5389 .name
= "array2mem",
5390 .mode
= COMMAND_EXEC
,
5391 .jim_handler
= jim_target_array2mem
,
5392 .help
= "Writes Tcl array of 8/16/32 bit numbers "
5394 .usage
= "arrayname bitwidth address count",
5397 .name
= "mem2array",
5398 .mode
= COMMAND_EXEC
,
5399 .jim_handler
= jim_target_mem2array
,
5400 .help
= "Loads Tcl array of 8/16/32 bit numbers "
5401 "from target memory",
5402 .usage
= "arrayname bitwidth address count",
5405 .name
= "eventlist",
5406 .mode
= COMMAND_EXEC
,
5407 .jim_handler
= jim_target_event_list
,
5408 .help
= "displays a table of events defined for this target",
5412 .mode
= COMMAND_EXEC
,
5413 .jim_handler
= jim_target_current_state
,
5414 .help
= "displays the current state of this target",
5417 .name
= "arp_examine",
5418 .mode
= COMMAND_EXEC
,
5419 .jim_handler
= jim_target_examine
,
5420 .help
= "used internally for reset processing",
5421 .usage
= "arp_examine ['allow-defer']",
5424 .name
= "was_examined",
5425 .mode
= COMMAND_EXEC
,
5426 .jim_handler
= jim_target_was_examined
,
5427 .help
= "used internally for reset processing",
5428 .usage
= "was_examined",
5431 .name
= "examine_deferred",
5432 .mode
= COMMAND_EXEC
,
5433 .jim_handler
= jim_target_examine_deferred
,
5434 .help
= "used internally for reset processing",
5435 .usage
= "examine_deferred",
5438 .name
= "arp_halt_gdb",
5439 .mode
= COMMAND_EXEC
,
5440 .jim_handler
= jim_target_halt_gdb
,
5441 .help
= "used internally for reset processing to halt GDB",
5445 .mode
= COMMAND_EXEC
,
5446 .jim_handler
= jim_target_poll
,
5447 .help
= "used internally for reset processing",
5450 .name
= "arp_reset",
5451 .mode
= COMMAND_EXEC
,
5452 .jim_handler
= jim_target_reset
,
5453 .help
= "used internally for reset processing",
5457 .mode
= COMMAND_EXEC
,
5458 .jim_handler
= jim_target_halt
,
5459 .help
= "used internally for reset processing",
5462 .name
= "arp_waitstate",
5463 .mode
= COMMAND_EXEC
,
5464 .jim_handler
= jim_target_wait_state
,
5465 .help
= "used internally for reset processing",
5468 .name
= "invoke-event",
5469 .mode
= COMMAND_EXEC
,
5470 .jim_handler
= jim_target_invoke_event
,
5471 .help
= "invoke handler for specified event",
5472 .usage
= "event_name",
5474 COMMAND_REGISTRATION_DONE
5477 static int target_create(Jim_GetOptInfo
*goi
)
5484 struct target
*target
;
5485 struct command_context
*cmd_ctx
;
5487 cmd_ctx
= current_command_context(goi
->interp
);
5488 assert(cmd_ctx
!= NULL
);
5490 if (goi
->argc
< 3) {
5491 Jim_WrongNumArgs(goi
->interp
, 1, goi
->argv
, "?name? ?type? ..options...");
5496 Jim_GetOpt_Obj(goi
, &new_cmd
);
5497 /* does this command exist? */
5498 cmd
= Jim_GetCommand(goi
->interp
, new_cmd
, JIM_ERRMSG
);
5500 cp
= Jim_GetString(new_cmd
, NULL
);
5501 Jim_SetResultFormatted(goi
->interp
, "Command/target: %s Exists", cp
);
5506 e
= Jim_GetOpt_String(goi
, &cp
, NULL
);
5509 struct transport
*tr
= get_current_transport();
5510 if (tr
->override_target
) {
5511 e
= tr
->override_target(&cp
);
5512 if (e
!= ERROR_OK
) {
5513 LOG_ERROR("The selected transport doesn't support this target");
5516 LOG_INFO("The selected transport took over low-level target control. The results might differ compared to plain JTAG/SWD");
5518 /* now does target type exist */
5519 for (x
= 0 ; target_types
[x
] ; x
++) {
5520 if (0 == strcmp(cp
, target_types
[x
]->name
)) {
5525 /* check for deprecated name */
5526 if (target_types
[x
]->deprecated_name
) {
5527 if (0 == strcmp(cp
, target_types
[x
]->deprecated_name
)) {
5529 LOG_WARNING("target name is deprecated use: \'%s\'", target_types
[x
]->name
);
5534 if (target_types
[x
] == NULL
) {
5535 Jim_SetResultFormatted(goi
->interp
, "Unknown target type %s, try one of ", cp
);
5536 for (x
= 0 ; target_types
[x
] ; x
++) {
5537 if (target_types
[x
+ 1]) {
5538 Jim_AppendStrings(goi
->interp
,
5539 Jim_GetResult(goi
->interp
),
5540 target_types
[x
]->name
,
5543 Jim_AppendStrings(goi
->interp
,
5544 Jim_GetResult(goi
->interp
),
5546 target_types
[x
]->name
, NULL
);
5553 target
= calloc(1, sizeof(struct target
));
5554 /* set target number */
5555 target
->target_number
= new_target_number();
5556 cmd_ctx
->current_target
= target
;
5558 /* allocate memory for each unique target type */
5559 target
->type
= calloc(1, sizeof(struct target_type
));
5561 memcpy(target
->type
, target_types
[x
], sizeof(struct target_type
));
5563 /* will be set by "-endian" */
5564 target
->endianness
= TARGET_ENDIAN_UNKNOWN
;
5566 /* default to first core, override with -coreid */
5569 target
->working_area
= 0x0;
5570 target
->working_area_size
= 0x0;
5571 target
->working_areas
= NULL
;
5572 target
->backup_working_area
= 0;
5574 target
->state
= TARGET_UNKNOWN
;
5575 target
->debug_reason
= DBG_REASON_UNDEFINED
;
5576 target
->reg_cache
= NULL
;
5577 target
->breakpoints
= NULL
;
5578 target
->watchpoints
= NULL
;
5579 target
->next
= NULL
;
5580 target
->arch_info
= NULL
;
5582 target
->display
= 1;
5584 target
->halt_issued
= false;
5586 /* initialize trace information */
5587 target
->trace_info
= calloc(1, sizeof(struct trace
));
5589 target
->dbgmsg
= NULL
;
5590 target
->dbg_msg_enabled
= 0;
5592 target
->endianness
= TARGET_ENDIAN_UNKNOWN
;
5594 target
->rtos
= NULL
;
5595 target
->rtos_auto_detect
= false;
5597 /* Do the rest as "configure" options */
5598 goi
->isconfigure
= 1;
5599 e
= target_configure(goi
, target
);
5602 if (target
->has_dap
) {
5603 if (!target
->dap_configured
) {
5604 Jim_SetResultString(goi
->interp
, "-dap ?name? required when creating target", -1);
5608 if (!target
->tap_configured
) {
5609 Jim_SetResultString(goi
->interp
, "-chain-position ?name? required when creating target", -1);
5613 /* tap must be set after target was configured */
5614 if (target
->tap
== NULL
)
5624 if (target
->endianness
== TARGET_ENDIAN_UNKNOWN
) {
5625 /* default endian to little if not specified */
5626 target
->endianness
= TARGET_LITTLE_ENDIAN
;
5629 cp
= Jim_GetString(new_cmd
, NULL
);
5630 target
->cmd_name
= strdup(cp
);
5632 if (target
->type
->target_create
) {
5633 e
= (*(target
->type
->target_create
))(target
, goi
->interp
);
5634 if (e
!= ERROR_OK
) {
5635 LOG_DEBUG("target_create failed");
5637 free(target
->cmd_name
);
5643 /* create the target specific commands */
5644 if (target
->type
->commands
) {
5645 e
= register_commands(cmd_ctx
, NULL
, target
->type
->commands
);
5647 LOG_ERROR("unable to register '%s' commands", cp
);
5650 /* append to end of list */
5652 struct target
**tpp
;
5653 tpp
= &(all_targets
);
5655 tpp
= &((*tpp
)->next
);
5659 /* now - create the new target name command */
5660 const struct command_registration target_subcommands
[] = {
5662 .chain
= target_instance_command_handlers
,
5665 .chain
= target
->type
->commands
,
5667 COMMAND_REGISTRATION_DONE
5669 const struct command_registration target_commands
[] = {
5672 .mode
= COMMAND_ANY
,
5673 .help
= "target command group",
5675 .chain
= target_subcommands
,
5677 COMMAND_REGISTRATION_DONE
5679 e
= register_commands(cmd_ctx
, NULL
, target_commands
);
5683 struct command
*c
= command_find_in_context(cmd_ctx
, cp
);
5685 command_set_handler_data(c
, target
);
5687 return (ERROR_OK
== e
) ? JIM_OK
: JIM_ERR
;
5690 static int jim_target_current(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
5693 Jim_WrongNumArgs(interp
, 1, argv
, "Too many parameters");
5696 struct command_context
*cmd_ctx
= current_command_context(interp
);
5697 assert(cmd_ctx
!= NULL
);
5699 Jim_SetResultString(interp
, target_name(get_current_target(cmd_ctx
)), -1);
5703 static int jim_target_types(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
5706 Jim_WrongNumArgs(interp
, 1, argv
, "Too many parameters");
5709 Jim_SetResult(interp
, Jim_NewListObj(interp
, NULL
, 0));
5710 for (unsigned x
= 0; NULL
!= target_types
[x
]; x
++) {
5711 Jim_ListAppendElement(interp
, Jim_GetResult(interp
),
5712 Jim_NewStringObj(interp
, target_types
[x
]->name
, -1));
5717 static int jim_target_names(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
5720 Jim_WrongNumArgs(interp
, 1, argv
, "Too many parameters");
5723 Jim_SetResult(interp
, Jim_NewListObj(interp
, NULL
, 0));
5724 struct target
*target
= all_targets
;
5726 Jim_ListAppendElement(interp
, Jim_GetResult(interp
),
5727 Jim_NewStringObj(interp
, target_name(target
), -1));
5728 target
= target
->next
;
5733 static int jim_target_smp(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
5736 const char *targetname
;
5738 struct target
*target
= (struct target
*) NULL
;
5739 struct target_list
*head
, *curr
, *new;
5740 curr
= (struct target_list
*) NULL
;
5741 head
= (struct target_list
*) NULL
;
5744 LOG_DEBUG("%d", argc
);
5745 /* argv[1] = target to associate in smp
5746 * argv[2] = target to assoicate in smp
5750 for (i
= 1; i
< argc
; i
++) {
5752 targetname
= Jim_GetString(argv
[i
], &len
);
5753 target
= get_target(targetname
);
5754 LOG_DEBUG("%s ", targetname
);
5756 new = malloc(sizeof(struct target_list
));
5757 new->target
= target
;
5758 new->next
= (struct target_list
*)NULL
;
5759 if (head
== (struct target_list
*)NULL
) {
5768 /* now parse the list of cpu and put the target in smp mode*/
5771 while (curr
!= (struct target_list
*)NULL
) {
5772 target
= curr
->target
;
5774 target
->head
= head
;
5778 if (target
&& target
->rtos
)
5779 retval
= rtos_smp_init(head
->target
);
5785 static int jim_target_create(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
5788 Jim_GetOpt_Setup(&goi
, interp
, argc
- 1, argv
+ 1);
5790 Jim_WrongNumArgs(goi
.interp
, goi
.argc
, goi
.argv
,
5791 "<name> <target_type> [<target_options> ...]");
5794 return target_create(&goi
);
5797 static const struct command_registration target_subcommand_handlers
[] = {
5800 .mode
= COMMAND_CONFIG
,
5801 .handler
= handle_target_init_command
,
5802 .help
= "initialize targets",
5806 /* REVISIT this should be COMMAND_CONFIG ... */
5807 .mode
= COMMAND_ANY
,
5808 .jim_handler
= jim_target_create
,
5809 .usage
= "name type '-chain-position' name [options ...]",
5810 .help
= "Creates and selects a new target",
5814 .mode
= COMMAND_ANY
,
5815 .jim_handler
= jim_target_current
,
5816 .help
= "Returns the currently selected target",
5820 .mode
= COMMAND_ANY
,
5821 .jim_handler
= jim_target_types
,
5822 .help
= "Returns the available target types as "
5823 "a list of strings",
5827 .mode
= COMMAND_ANY
,
5828 .jim_handler
= jim_target_names
,
5829 .help
= "Returns the names of all targets as a list of strings",
5833 .mode
= COMMAND_ANY
,
5834 .jim_handler
= jim_target_smp
,
5835 .usage
= "targetname1 targetname2 ...",
5836 .help
= "gather several target in a smp list"
5839 COMMAND_REGISTRATION_DONE
5843 target_addr_t address
;
5849 static int fastload_num
;
5850 static struct FastLoad
*fastload
;
5852 static void free_fastload(void)
5854 if (fastload
!= NULL
) {
5856 for (i
= 0; i
< fastload_num
; i
++) {
5857 if (fastload
[i
].data
)
5858 free(fastload
[i
].data
);
5865 COMMAND_HANDLER(handle_fast_load_image_command
)
5869 uint32_t image_size
;
5870 target_addr_t min_address
= 0;
5871 target_addr_t max_address
= -1;
5876 int retval
= CALL_COMMAND_HANDLER(parse_load_image_command_CMD_ARGV
,
5877 &image
, &min_address
, &max_address
);
5878 if (ERROR_OK
!= retval
)
5881 struct duration bench
;
5882 duration_start(&bench
);
5884 retval
= image_open(&image
, CMD_ARGV
[0], (CMD_ARGC
>= 3) ? CMD_ARGV
[2] : NULL
);
5885 if (retval
!= ERROR_OK
)
5890 fastload_num
= image
.num_sections
;
5891 fastload
= malloc(sizeof(struct FastLoad
)*image
.num_sections
);
5892 if (fastload
== NULL
) {
5893 command_print(CMD_CTX
, "out of memory");
5894 image_close(&image
);
5897 memset(fastload
, 0, sizeof(struct FastLoad
)*image
.num_sections
);
5898 for (i
= 0; i
< image
.num_sections
; i
++) {
5899 buffer
= malloc(image
.sections
[i
].size
);
5900 if (buffer
== NULL
) {
5901 command_print(CMD_CTX
, "error allocating buffer for section (%d bytes)",
5902 (int)(image
.sections
[i
].size
));
5903 retval
= ERROR_FAIL
;
5907 retval
= image_read_section(&image
, i
, 0x0, image
.sections
[i
].size
, buffer
, &buf_cnt
);
5908 if (retval
!= ERROR_OK
) {
5913 uint32_t offset
= 0;
5914 uint32_t length
= buf_cnt
;
5916 /* DANGER!!! beware of unsigned comparision here!!! */
5918 if ((image
.sections
[i
].base_address
+ buf_cnt
>= min_address
) &&
5919 (image
.sections
[i
].base_address
< max_address
)) {
5920 if (image
.sections
[i
].base_address
< min_address
) {
5921 /* clip addresses below */
5922 offset
+= min_address
-image
.sections
[i
].base_address
;
5926 if (image
.sections
[i
].base_address
+ buf_cnt
> max_address
)
5927 length
-= (image
.sections
[i
].base_address
+ buf_cnt
)-max_address
;
5929 fastload
[i
].address
= image
.sections
[i
].base_address
+ offset
;
5930 fastload
[i
].data
= malloc(length
);
5931 if (fastload
[i
].data
== NULL
) {
5933 command_print(CMD_CTX
, "error allocating buffer for section (%" PRIu32
" bytes)",
5935 retval
= ERROR_FAIL
;
5938 memcpy(fastload
[i
].data
, buffer
+ offset
, length
);
5939 fastload
[i
].length
= length
;
5941 image_size
+= length
;
5942 command_print(CMD_CTX
, "%u bytes written at address 0x%8.8x",
5943 (unsigned int)length
,
5944 ((unsigned int)(image
.sections
[i
].base_address
+ offset
)));
5950 if ((ERROR_OK
== retval
) && (duration_measure(&bench
) == ERROR_OK
)) {
5951 command_print(CMD_CTX
, "Loaded %" PRIu32
" bytes "
5952 "in %fs (%0.3f KiB/s)", image_size
,
5953 duration_elapsed(&bench
), duration_kbps(&bench
, image_size
));
5955 command_print(CMD_CTX
,
5956 "WARNING: image has not been loaded to target!"
5957 "You can issue a 'fast_load' to finish loading.");
5960 image_close(&image
);
5962 if (retval
!= ERROR_OK
)
5968 COMMAND_HANDLER(handle_fast_load_command
)
5971 return ERROR_COMMAND_SYNTAX_ERROR
;
5972 if (fastload
== NULL
) {
5973 LOG_ERROR("No image in memory");
5977 int64_t ms
= timeval_ms();
5979 int retval
= ERROR_OK
;
5980 for (i
= 0; i
< fastload_num
; i
++) {
5981 struct target
*target
= get_current_target(CMD_CTX
);
5982 command_print(CMD_CTX
, "Write to 0x%08x, length 0x%08x",
5983 (unsigned int)(fastload
[i
].address
),
5984 (unsigned int)(fastload
[i
].length
));
5985 retval
= target_write_buffer(target
, fastload
[i
].address
, fastload
[i
].length
, fastload
[i
].data
);
5986 if (retval
!= ERROR_OK
)
5988 size
+= fastload
[i
].length
;
5990 if (retval
== ERROR_OK
) {
5991 int64_t after
= timeval_ms();
5992 command_print(CMD_CTX
, "Loaded image %f kBytes/s", (float)(size
/1024.0)/((float)(after
-ms
)/1000.0));
5997 static const struct command_registration target_command_handlers
[] = {
6000 .handler
= handle_targets_command
,
6001 .mode
= COMMAND_ANY
,
6002 .help
= "change current default target (one parameter) "
6003 "or prints table of all targets (no parameters)",
6004 .usage
= "[target]",
6008 .mode
= COMMAND_CONFIG
,
6009 .help
= "configure target",
6011 .chain
= target_subcommand_handlers
,
6013 COMMAND_REGISTRATION_DONE
6016 int target_register_commands(struct command_context
*cmd_ctx
)
6018 return register_commands(cmd_ctx
, NULL
, target_command_handlers
);
6021 static bool target_reset_nag
= true;
6023 bool get_target_reset_nag(void)
6025 return target_reset_nag
;
6028 COMMAND_HANDLER(handle_target_reset_nag
)
6030 return CALL_COMMAND_HANDLER(handle_command_parse_bool
,
6031 &target_reset_nag
, "Nag after each reset about options to improve "
6035 COMMAND_HANDLER(handle_ps_command
)
6037 struct target
*target
= get_current_target(CMD_CTX
);
6039 if (target
->state
!= TARGET_HALTED
) {
6040 LOG_INFO("target not halted !!");
6044 if ((target
->rtos
) && (target
->rtos
->type
)
6045 && (target
->rtos
->type
->ps_command
)) {
6046 display
= target
->rtos
->type
->ps_command(target
);
6047 command_print(CMD_CTX
, "%s", display
);
6052 return ERROR_TARGET_FAILURE
;
6056 static void binprint(struct command_context
*cmd_ctx
, const char *text
, const uint8_t *buf
, int size
)
6059 command_print_sameline(cmd_ctx
, "%s", text
);
6060 for (int i
= 0; i
< size
; i
++)
6061 command_print_sameline(cmd_ctx
, " %02x", buf
[i
]);
6062 command_print(cmd_ctx
, " ");
6065 COMMAND_HANDLER(handle_test_mem_access_command
)
6067 struct target
*target
= get_current_target(CMD_CTX
);
6069 int retval
= ERROR_OK
;
6071 if (target
->state
!= TARGET_HALTED
) {
6072 LOG_INFO("target not halted !!");
6077 return ERROR_COMMAND_SYNTAX_ERROR
;
6079 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], test_size
);
6082 size_t num_bytes
= test_size
+ 4;
6084 struct working_area
*wa
= NULL
;
6085 retval
= target_alloc_working_area(target
, num_bytes
, &wa
);
6086 if (retval
!= ERROR_OK
) {
6087 LOG_ERROR("Not enough working area");
6091 uint8_t *test_pattern
= malloc(num_bytes
);
6093 for (size_t i
= 0; i
< num_bytes
; i
++)
6094 test_pattern
[i
] = rand();
6096 retval
= target_write_memory(target
, wa
->address
, 1, num_bytes
, test_pattern
);
6097 if (retval
!= ERROR_OK
) {
6098 LOG_ERROR("Test pattern write failed");
6102 for (int host_offset
= 0; host_offset
<= 1; host_offset
++) {
6103 for (int size
= 1; size
<= 4; size
*= 2) {
6104 for (int offset
= 0; offset
< 4; offset
++) {
6105 uint32_t count
= test_size
/ size
;
6106 size_t host_bufsiz
= (count
+ 2) * size
+ host_offset
;
6107 uint8_t *read_ref
= malloc(host_bufsiz
);
6108 uint8_t *read_buf
= malloc(host_bufsiz
);
6110 for (size_t i
= 0; i
< host_bufsiz
; i
++) {
6111 read_ref
[i
] = rand();
6112 read_buf
[i
] = read_ref
[i
];
6114 command_print_sameline(CMD_CTX
,
6115 "Test read %" PRIu32
" x %d @ %d to %saligned buffer: ", count
,
6116 size
, offset
, host_offset
? "un" : "");
6118 struct duration bench
;
6119 duration_start(&bench
);
6121 retval
= target_read_memory(target
, wa
->address
+ offset
, size
, count
,
6122 read_buf
+ size
+ host_offset
);
6124 duration_measure(&bench
);
6126 if (retval
== ERROR_TARGET_UNALIGNED_ACCESS
) {
6127 command_print(CMD_CTX
, "Unsupported alignment");
6129 } else if (retval
!= ERROR_OK
) {
6130 command_print(CMD_CTX
, "Memory read failed");
6134 /* replay on host */
6135 memcpy(read_ref
+ size
+ host_offset
, test_pattern
+ offset
, count
* size
);
6138 int result
= memcmp(read_ref
, read_buf
, host_bufsiz
);
6140 command_print(CMD_CTX
, "Pass in %fs (%0.3f KiB/s)",
6141 duration_elapsed(&bench
),
6142 duration_kbps(&bench
, count
* size
));
6144 command_print(CMD_CTX
, "Compare failed");
6145 binprint(CMD_CTX
, "ref:", read_ref
, host_bufsiz
);
6146 binprint(CMD_CTX
, "buf:", read_buf
, host_bufsiz
);
6159 target_free_working_area(target
, wa
);
6162 num_bytes
= test_size
+ 4 + 4 + 4;
6164 retval
= target_alloc_working_area(target
, num_bytes
, &wa
);
6165 if (retval
!= ERROR_OK
) {
6166 LOG_ERROR("Not enough working area");
6170 test_pattern
= malloc(num_bytes
);
6172 for (size_t i
= 0; i
< num_bytes
; i
++)
6173 test_pattern
[i
] = rand();
6175 for (int host_offset
= 0; host_offset
<= 1; host_offset
++) {
6176 for (int size
= 1; size
<= 4; size
*= 2) {
6177 for (int offset
= 0; offset
< 4; offset
++) {
6178 uint32_t count
= test_size
/ size
;
6179 size_t host_bufsiz
= count
* size
+ host_offset
;
6180 uint8_t *read_ref
= malloc(num_bytes
);
6181 uint8_t *read_buf
= malloc(num_bytes
);
6182 uint8_t *write_buf
= malloc(host_bufsiz
);
6184 for (size_t i
= 0; i
< host_bufsiz
; i
++)
6185 write_buf
[i
] = rand();
6186 command_print_sameline(CMD_CTX
,
6187 "Test write %" PRIu32
" x %d @ %d from %saligned buffer: ", count
,
6188 size
, offset
, host_offset
? "un" : "");
6190 retval
= target_write_memory(target
, wa
->address
, 1, num_bytes
, test_pattern
);
6191 if (retval
!= ERROR_OK
) {
6192 command_print(CMD_CTX
, "Test pattern write failed");
6196 /* replay on host */
6197 memcpy(read_ref
, test_pattern
, num_bytes
);
6198 memcpy(read_ref
+ size
+ offset
, write_buf
+ host_offset
, count
* size
);
6200 struct duration bench
;
6201 duration_start(&bench
);
6203 retval
= target_write_memory(target
, wa
->address
+ size
+ offset
, size
, count
,
6204 write_buf
+ host_offset
);
6206 duration_measure(&bench
);
6208 if (retval
== ERROR_TARGET_UNALIGNED_ACCESS
) {
6209 command_print(CMD_CTX
, "Unsupported alignment");
6211 } else if (retval
!= ERROR_OK
) {
6212 command_print(CMD_CTX
, "Memory write failed");
6217 retval
= target_read_memory(target
, wa
->address
, 1, num_bytes
, read_buf
);
6218 if (retval
!= ERROR_OK
) {
6219 command_print(CMD_CTX
, "Test pattern write failed");
6224 int result
= memcmp(read_ref
, read_buf
, num_bytes
);
6226 command_print(CMD_CTX
, "Pass in %fs (%0.3f KiB/s)",
6227 duration_elapsed(&bench
),
6228 duration_kbps(&bench
, count
* size
));
6230 command_print(CMD_CTX
, "Compare failed");
6231 binprint(CMD_CTX
, "ref:", read_ref
, num_bytes
);
6232 binprint(CMD_CTX
, "buf:", read_buf
, num_bytes
);
6244 target_free_working_area(target
, wa
);
6248 static const struct command_registration target_exec_command_handlers
[] = {
6250 .name
= "fast_load_image",
6251 .handler
= handle_fast_load_image_command
,
6252 .mode
= COMMAND_ANY
,
6253 .help
= "Load image into server memory for later use by "
6254 "fast_load; primarily for profiling",
6255 .usage
= "filename address ['bin'|'ihex'|'elf'|'s19'] "
6256 "[min_address [max_length]]",
6259 .name
= "fast_load",
6260 .handler
= handle_fast_load_command
,
6261 .mode
= COMMAND_EXEC
,
6262 .help
= "loads active fast load image to current target "
6263 "- mainly for profiling purposes",
6268 .handler
= handle_profile_command
,
6269 .mode
= COMMAND_EXEC
,
6270 .usage
= "seconds filename [start end]",
6271 .help
= "profiling samples the CPU PC",
6273 /** @todo don't register virt2phys() unless target supports it */
6275 .name
= "virt2phys",
6276 .handler
= handle_virt2phys_command
,
6277 .mode
= COMMAND_ANY
,
6278 .help
= "translate a virtual address into a physical address",
6279 .usage
= "virtual_address",
6283 .handler
= handle_reg_command
,
6284 .mode
= COMMAND_EXEC
,
6285 .help
= "display (reread from target with \"force\") or set a register; "
6286 "with no arguments, displays all registers and their values",
6287 .usage
= "[(register_number|register_name) [(value|'force')]]",
6291 .handler
= handle_poll_command
,
6292 .mode
= COMMAND_EXEC
,
6293 .help
= "poll target state; or reconfigure background polling",
6294 .usage
= "['on'|'off']",
6297 .name
= "wait_halt",
6298 .handler
= handle_wait_halt_command
,
6299 .mode
= COMMAND_EXEC
,
6300 .help
= "wait up to the specified number of milliseconds "
6301 "(default 5000) for a previously requested halt",
6302 .usage
= "[milliseconds]",
6306 .handler
= handle_halt_command
,
6307 .mode
= COMMAND_EXEC
,
6308 .help
= "request target to halt, then wait up to the specified"
6309 "number of milliseconds (default 5000) for it to complete",
6310 .usage
= "[milliseconds]",
6314 .handler
= handle_resume_command
,
6315 .mode
= COMMAND_EXEC
,
6316 .help
= "resume target execution from current PC or address",
6317 .usage
= "[address]",
6321 .handler
= handle_reset_command
,
6322 .mode
= COMMAND_EXEC
,
6323 .usage
= "[run|halt|init]",
6324 .help
= "Reset all targets into the specified mode."
6325 "Default reset mode is run, if not given.",
6328 .name
= "soft_reset_halt",
6329 .handler
= handle_soft_reset_halt_command
,
6330 .mode
= COMMAND_EXEC
,
6332 .help
= "halt the target and do a soft reset",
6336 .handler
= handle_step_command
,
6337 .mode
= COMMAND_EXEC
,
6338 .help
= "step one instruction from current PC or address",
6339 .usage
= "[address]",
6343 .handler
= handle_md_command
,
6344 .mode
= COMMAND_EXEC
,
6345 .help
= "display memory words",
6346 .usage
= "['phys'] address [count]",
6350 .handler
= handle_md_command
,
6351 .mode
= COMMAND_EXEC
,
6352 .help
= "display memory words",
6353 .usage
= "['phys'] address [count]",
6357 .handler
= handle_md_command
,
6358 .mode
= COMMAND_EXEC
,
6359 .help
= "display memory half-words",
6360 .usage
= "['phys'] address [count]",
6364 .handler
= handle_md_command
,
6365 .mode
= COMMAND_EXEC
,
6366 .help
= "display memory bytes",
6367 .usage
= "['phys'] address [count]",
6371 .handler
= handle_mw_command
,
6372 .mode
= COMMAND_EXEC
,
6373 .help
= "write memory word",
6374 .usage
= "['phys'] address value [count]",
6378 .handler
= handle_mw_command
,
6379 .mode
= COMMAND_EXEC
,
6380 .help
= "write memory word",
6381 .usage
= "['phys'] address value [count]",
6385 .handler
= handle_mw_command
,
6386 .mode
= COMMAND_EXEC
,
6387 .help
= "write memory half-word",
6388 .usage
= "['phys'] address value [count]",
6392 .handler
= handle_mw_command
,
6393 .mode
= COMMAND_EXEC
,
6394 .help
= "write memory byte",
6395 .usage
= "['phys'] address value [count]",
6399 .handler
= handle_bp_command
,
6400 .mode
= COMMAND_EXEC
,
6401 .help
= "list or set hardware or software breakpoint",
6402 .usage
= "<address> [<asid>]<length> ['hw'|'hw_ctx']",
6406 .handler
= handle_rbp_command
,
6407 .mode
= COMMAND_EXEC
,
6408 .help
= "remove breakpoint",
6413 .handler
= handle_wp_command
,
6414 .mode
= COMMAND_EXEC
,
6415 .help
= "list (no params) or create watchpoints",
6416 .usage
= "[address length [('r'|'w'|'a') value [mask]]]",
6420 .handler
= handle_rwp_command
,
6421 .mode
= COMMAND_EXEC
,
6422 .help
= "remove watchpoint",
6426 .name
= "load_image",
6427 .handler
= handle_load_image_command
,
6428 .mode
= COMMAND_EXEC
,
6429 .usage
= "filename address ['bin'|'ihex'|'elf'|'s19'] "
6430 "[min_address] [max_length]",
6433 .name
= "dump_image",
6434 .handler
= handle_dump_image_command
,
6435 .mode
= COMMAND_EXEC
,
6436 .usage
= "filename address size",
6439 .name
= "verify_image_checksum",
6440 .handler
= handle_verify_image_checksum_command
,
6441 .mode
= COMMAND_EXEC
,
6442 .usage
= "filename [offset [type]]",
6445 .name
= "verify_image",
6446 .handler
= handle_verify_image_command
,
6447 .mode
= COMMAND_EXEC
,
6448 .usage
= "filename [offset [type]]",
6451 .name
= "test_image",
6452 .handler
= handle_test_image_command
,
6453 .mode
= COMMAND_EXEC
,
6454 .usage
= "filename [offset [type]]",
6457 .name
= "mem2array",
6458 .mode
= COMMAND_EXEC
,
6459 .jim_handler
= jim_mem2array
,
6460 .help
= "read 8/16/32 bit memory and return as a TCL array "
6461 "for script processing",
6462 .usage
= "arrayname bitwidth address count",
6465 .name
= "array2mem",
6466 .mode
= COMMAND_EXEC
,
6467 .jim_handler
= jim_array2mem
,
6468 .help
= "convert a TCL array to memory locations "
6469 "and write the 8/16/32 bit values",
6470 .usage
= "arrayname bitwidth address count",
6473 .name
= "reset_nag",
6474 .handler
= handle_target_reset_nag
,
6475 .mode
= COMMAND_ANY
,
6476 .help
= "Nag after each reset about options that could have been "
6477 "enabled to improve performance. ",
6478 .usage
= "['enable'|'disable']",
6482 .handler
= handle_ps_command
,
6483 .mode
= COMMAND_EXEC
,
6484 .help
= "list all tasks ",
6488 .name
= "test_mem_access",
6489 .handler
= handle_test_mem_access_command
,
6490 .mode
= COMMAND_EXEC
,
6491 .help
= "Test the target's memory access functions",
6495 COMMAND_REGISTRATION_DONE
6497 static int target_register_user_commands(struct command_context
*cmd_ctx
)
6499 int retval
= ERROR_OK
;
6500 retval
= target_request_register_commands(cmd_ctx
);
6501 if (retval
!= ERROR_OK
)
6504 retval
= trace_register_commands(cmd_ctx
);
6505 if (retval
!= ERROR_OK
)
6509 return register_commands(cmd_ctx
, NULL
, target_exec_command_handlers
);