1 /***************************************************************************
2 * Copyright (C) 2005 by Dominic Rath *
3 * Dominic.Rath@gmx.de *
5 * Copyright (C) 2007-2010 Øyvind Harboe *
6 * oyvind.harboe@zylin.com *
8 * Copyright (C) 2008, Duane Ellis *
9 * openocd@duaneeellis.com *
11 * Copyright (C) 2008 by Spencer Oliver *
12 * spen@spen-soft.co.uk *
14 * Copyright (C) 2008 by Rick Altherr *
15 * kc8apf@kc8apf.net> *
17 * Copyright (C) 2011 by Broadcom Corporation *
18 * Evan Hunter - ehunter@broadcom.com *
20 * Copyright (C) ST-Ericsson SA 2011 *
21 * michel.jaouen@stericsson.com : smp minimum support *
23 * Copyright (C) 2011 Andreas Fritiofson *
24 * andreas.fritiofson@gmail.com *
26 * This program is free software; you can redistribute it and/or modify *
27 * it under the terms of the GNU General Public License as published by *
28 * the Free Software Foundation; either version 2 of the License, or *
29 * (at your option) any later version. *
31 * This program is distributed in the hope that it will be useful, *
32 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
33 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
34 * GNU General Public License for more details. *
36 * You should have received a copy of the GNU General Public License *
37 * along with this program. If not, see <http://www.gnu.org/licenses/>. *
38 ***************************************************************************/
44 #include <helper/time_support.h>
45 #include <jtag/jtag.h>
46 #include <flash/nor/core.h>
49 #include "target_type.h"
50 #include "target_request.h"
51 #include "breakpoints.h"
55 #include "rtos/rtos.h"
56 #include "transport/transport.h"
58 /* default halt wait timeout (ms) */
59 #define DEFAULT_HALT_TIMEOUT 5000
61 static int target_read_buffer_default(struct target
*target
, target_addr_t address
,
62 uint32_t count
, uint8_t *buffer
);
63 static int target_write_buffer_default(struct target
*target
, target_addr_t address
,
64 uint32_t count
, const uint8_t *buffer
);
65 static int target_array2mem(Jim_Interp
*interp
, struct target
*target
,
66 int argc
, Jim_Obj
* const *argv
);
67 static int target_mem2array(Jim_Interp
*interp
, struct target
*target
,
68 int argc
, Jim_Obj
* const *argv
);
69 static int target_register_user_commands(struct command_context
*cmd_ctx
);
70 static int target_get_gdb_fileio_info_default(struct target
*target
,
71 struct gdb_fileio_info
*fileio_info
);
72 static int target_gdb_fileio_end_default(struct target
*target
, int retcode
,
73 int fileio_errno
, bool ctrl_c
);
74 static int target_profiling_default(struct target
*target
, uint32_t *samples
,
75 uint32_t max_num_samples
, uint32_t *num_samples
, uint32_t seconds
);
78 extern struct target_type arm7tdmi_target
;
79 extern struct target_type arm720t_target
;
80 extern struct target_type arm9tdmi_target
;
81 extern struct target_type arm920t_target
;
82 extern struct target_type arm966e_target
;
83 extern struct target_type arm946e_target
;
84 extern struct target_type arm926ejs_target
;
85 extern struct target_type fa526_target
;
86 extern struct target_type feroceon_target
;
87 extern struct target_type dragonite_target
;
88 extern struct target_type xscale_target
;
89 extern struct target_type cortexm_target
;
90 extern struct target_type cortexa_target
;
91 extern struct target_type aarch64_target
;
92 extern struct target_type cortexr4_target
;
93 extern struct target_type arm11_target
;
94 extern struct target_type ls1_sap_target
;
95 extern struct target_type mips_m4k_target
;
96 extern struct target_type avr_target
;
97 extern struct target_type dsp563xx_target
;
98 extern struct target_type dsp5680xx_target
;
99 extern struct target_type testee_target
;
100 extern struct target_type avr32_ap7k_target
;
101 extern struct target_type hla_target
;
102 extern struct target_type nds32_v2_target
;
103 extern struct target_type nds32_v3_target
;
104 extern struct target_type nds32_v3m_target
;
105 extern struct target_type or1k_target
;
106 extern struct target_type quark_x10xx_target
;
107 extern struct target_type quark_d20xx_target
;
108 extern struct target_type stm8_target
;
110 static struct target_type
*target_types
[] = {
147 struct target
*all_targets
;
148 static struct target_event_callback
*target_event_callbacks
;
149 static struct target_timer_callback
*target_timer_callbacks
;
150 LIST_HEAD(target_reset_callback_list
);
151 LIST_HEAD(target_trace_callback_list
);
152 static const int polling_interval
= 100;
154 static const Jim_Nvp nvp_assert
[] = {
155 { .name
= "assert", NVP_ASSERT
},
156 { .name
= "deassert", NVP_DEASSERT
},
157 { .name
= "T", NVP_ASSERT
},
158 { .name
= "F", NVP_DEASSERT
},
159 { .name
= "t", NVP_ASSERT
},
160 { .name
= "f", NVP_DEASSERT
},
161 { .name
= NULL
, .value
= -1 }
164 static const Jim_Nvp nvp_error_target
[] = {
165 { .value
= ERROR_TARGET_INVALID
, .name
= "err-invalid" },
166 { .value
= ERROR_TARGET_INIT_FAILED
, .name
= "err-init-failed" },
167 { .value
= ERROR_TARGET_TIMEOUT
, .name
= "err-timeout" },
168 { .value
= ERROR_TARGET_NOT_HALTED
, .name
= "err-not-halted" },
169 { .value
= ERROR_TARGET_FAILURE
, .name
= "err-failure" },
170 { .value
= ERROR_TARGET_UNALIGNED_ACCESS
, .name
= "err-unaligned-access" },
171 { .value
= ERROR_TARGET_DATA_ABORT
, .name
= "err-data-abort" },
172 { .value
= ERROR_TARGET_RESOURCE_NOT_AVAILABLE
, .name
= "err-resource-not-available" },
173 { .value
= ERROR_TARGET_TRANSLATION_FAULT
, .name
= "err-translation-fault" },
174 { .value
= ERROR_TARGET_NOT_RUNNING
, .name
= "err-not-running" },
175 { .value
= ERROR_TARGET_NOT_EXAMINED
, .name
= "err-not-examined" },
176 { .value
= -1, .name
= NULL
}
179 static const char *target_strerror_safe(int err
)
183 n
= Jim_Nvp_value2name_simple(nvp_error_target
, err
);
190 static const Jim_Nvp nvp_target_event
[] = {
192 { .value
= TARGET_EVENT_GDB_HALT
, .name
= "gdb-halt" },
193 { .value
= TARGET_EVENT_HALTED
, .name
= "halted" },
194 { .value
= TARGET_EVENT_RESUMED
, .name
= "resumed" },
195 { .value
= TARGET_EVENT_RESUME_START
, .name
= "resume-start" },
196 { .value
= TARGET_EVENT_RESUME_END
, .name
= "resume-end" },
198 { .name
= "gdb-start", .value
= TARGET_EVENT_GDB_START
},
199 { .name
= "gdb-end", .value
= TARGET_EVENT_GDB_END
},
201 { .value
= TARGET_EVENT_RESET_START
, .name
= "reset-start" },
202 { .value
= TARGET_EVENT_RESET_ASSERT_PRE
, .name
= "reset-assert-pre" },
203 { .value
= TARGET_EVENT_RESET_ASSERT
, .name
= "reset-assert" },
204 { .value
= TARGET_EVENT_RESET_ASSERT_POST
, .name
= "reset-assert-post" },
205 { .value
= TARGET_EVENT_RESET_DEASSERT_PRE
, .name
= "reset-deassert-pre" },
206 { .value
= TARGET_EVENT_RESET_DEASSERT_POST
, .name
= "reset-deassert-post" },
207 { .value
= TARGET_EVENT_RESET_HALT_PRE
, .name
= "reset-halt-pre" },
208 { .value
= TARGET_EVENT_RESET_HALT_POST
, .name
= "reset-halt-post" },
209 { .value
= TARGET_EVENT_RESET_WAIT_PRE
, .name
= "reset-wait-pre" },
210 { .value
= TARGET_EVENT_RESET_WAIT_POST
, .name
= "reset-wait-post" },
211 { .value
= TARGET_EVENT_RESET_INIT
, .name
= "reset-init" },
212 { .value
= TARGET_EVENT_RESET_END
, .name
= "reset-end" },
214 { .value
= TARGET_EVENT_EXAMINE_START
, .name
= "examine-start" },
215 { .value
= TARGET_EVENT_EXAMINE_END
, .name
= "examine-end" },
217 { .value
= TARGET_EVENT_DEBUG_HALTED
, .name
= "debug-halted" },
218 { .value
= TARGET_EVENT_DEBUG_RESUMED
, .name
= "debug-resumed" },
220 { .value
= TARGET_EVENT_GDB_ATTACH
, .name
= "gdb-attach" },
221 { .value
= TARGET_EVENT_GDB_DETACH
, .name
= "gdb-detach" },
223 { .value
= TARGET_EVENT_GDB_FLASH_WRITE_START
, .name
= "gdb-flash-write-start" },
224 { .value
= TARGET_EVENT_GDB_FLASH_WRITE_END
, .name
= "gdb-flash-write-end" },
226 { .value
= TARGET_EVENT_GDB_FLASH_ERASE_START
, .name
= "gdb-flash-erase-start" },
227 { .value
= TARGET_EVENT_GDB_FLASH_ERASE_END
, .name
= "gdb-flash-erase-end" },
229 { .value
= TARGET_EVENT_TRACE_CONFIG
, .name
= "trace-config" },
231 { .name
= NULL
, .value
= -1 }
234 static const Jim_Nvp nvp_target_state
[] = {
235 { .name
= "unknown", .value
= TARGET_UNKNOWN
},
236 { .name
= "running", .value
= TARGET_RUNNING
},
237 { .name
= "halted", .value
= TARGET_HALTED
},
238 { .name
= "reset", .value
= TARGET_RESET
},
239 { .name
= "debug-running", .value
= TARGET_DEBUG_RUNNING
},
240 { .name
= NULL
, .value
= -1 },
243 static const Jim_Nvp nvp_target_debug_reason
[] = {
244 { .name
= "debug-request" , .value
= DBG_REASON_DBGRQ
},
245 { .name
= "breakpoint" , .value
= DBG_REASON_BREAKPOINT
},
246 { .name
= "watchpoint" , .value
= DBG_REASON_WATCHPOINT
},
247 { .name
= "watchpoint-and-breakpoint", .value
= DBG_REASON_WPTANDBKPT
},
248 { .name
= "single-step" , .value
= DBG_REASON_SINGLESTEP
},
249 { .name
= "target-not-halted" , .value
= DBG_REASON_NOTHALTED
},
250 { .name
= "program-exit" , .value
= DBG_REASON_EXIT
},
251 { .name
= "undefined" , .value
= DBG_REASON_UNDEFINED
},
252 { .name
= NULL
, .value
= -1 },
255 static const Jim_Nvp nvp_target_endian
[] = {
256 { .name
= "big", .value
= TARGET_BIG_ENDIAN
},
257 { .name
= "little", .value
= TARGET_LITTLE_ENDIAN
},
258 { .name
= "be", .value
= TARGET_BIG_ENDIAN
},
259 { .name
= "le", .value
= TARGET_LITTLE_ENDIAN
},
260 { .name
= NULL
, .value
= -1 },
263 static const Jim_Nvp nvp_reset_modes
[] = {
264 { .name
= "unknown", .value
= RESET_UNKNOWN
},
265 { .name
= "run" , .value
= RESET_RUN
},
266 { .name
= "halt" , .value
= RESET_HALT
},
267 { .name
= "init" , .value
= RESET_INIT
},
268 { .name
= NULL
, .value
= -1 },
271 const char *debug_reason_name(struct target
*t
)
275 cp
= Jim_Nvp_value2name_simple(nvp_target_debug_reason
,
276 t
->debug_reason
)->name
;
278 LOG_ERROR("Invalid debug reason: %d", (int)(t
->debug_reason
));
279 cp
= "(*BUG*unknown*BUG*)";
284 const char *target_state_name(struct target
*t
)
287 cp
= Jim_Nvp_value2name_simple(nvp_target_state
, t
->state
)->name
;
289 LOG_ERROR("Invalid target state: %d", (int)(t
->state
));
290 cp
= "(*BUG*unknown*BUG*)";
293 if (!target_was_examined(t
) && t
->defer_examine
)
294 cp
= "examine deferred";
299 const char *target_event_name(enum target_event event
)
302 cp
= Jim_Nvp_value2name_simple(nvp_target_event
, event
)->name
;
304 LOG_ERROR("Invalid target event: %d", (int)(event
));
305 cp
= "(*BUG*unknown*BUG*)";
310 const char *target_reset_mode_name(enum target_reset_mode reset_mode
)
313 cp
= Jim_Nvp_value2name_simple(nvp_reset_modes
, reset_mode
)->name
;
315 LOG_ERROR("Invalid target reset mode: %d", (int)(reset_mode
));
316 cp
= "(*BUG*unknown*BUG*)";
321 /* determine the number of the new target */
322 static int new_target_number(void)
327 /* number is 0 based */
331 if (x
< t
->target_number
)
332 x
= t
->target_number
;
338 /* read a uint64_t from a buffer in target memory endianness */
339 uint64_t target_buffer_get_u64(struct target
*target
, const uint8_t *buffer
)
341 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
342 return le_to_h_u64(buffer
);
344 return be_to_h_u64(buffer
);
347 /* read a uint32_t from a buffer in target memory endianness */
348 uint32_t target_buffer_get_u32(struct target
*target
, const uint8_t *buffer
)
350 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
351 return le_to_h_u32(buffer
);
353 return be_to_h_u32(buffer
);
356 /* read a uint24_t from a buffer in target memory endianness */
357 uint32_t target_buffer_get_u24(struct target
*target
, const uint8_t *buffer
)
359 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
360 return le_to_h_u24(buffer
);
362 return be_to_h_u24(buffer
);
365 /* read a uint16_t from a buffer in target memory endianness */
366 uint16_t target_buffer_get_u16(struct target
*target
, const uint8_t *buffer
)
368 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
369 return le_to_h_u16(buffer
);
371 return be_to_h_u16(buffer
);
374 /* read a uint8_t from a buffer in target memory endianness */
375 static uint8_t target_buffer_get_u8(struct target
*target
, const uint8_t *buffer
)
377 return *buffer
& 0x0ff;
380 /* write a uint64_t to a buffer in target memory endianness */
381 void target_buffer_set_u64(struct target
*target
, uint8_t *buffer
, uint64_t value
)
383 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
384 h_u64_to_le(buffer
, value
);
386 h_u64_to_be(buffer
, value
);
389 /* write a uint32_t to a buffer in target memory endianness */
390 void target_buffer_set_u32(struct target
*target
, uint8_t *buffer
, uint32_t value
)
392 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
393 h_u32_to_le(buffer
, value
);
395 h_u32_to_be(buffer
, value
);
398 /* write a uint24_t to a buffer in target memory endianness */
399 void target_buffer_set_u24(struct target
*target
, uint8_t *buffer
, uint32_t value
)
401 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
402 h_u24_to_le(buffer
, value
);
404 h_u24_to_be(buffer
, value
);
407 /* write a uint16_t to a buffer in target memory endianness */
408 void target_buffer_set_u16(struct target
*target
, uint8_t *buffer
, uint16_t value
)
410 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
411 h_u16_to_le(buffer
, value
);
413 h_u16_to_be(buffer
, value
);
416 /* write a uint8_t to a buffer in target memory endianness */
417 static void target_buffer_set_u8(struct target
*target
, uint8_t *buffer
, uint8_t value
)
422 /* write a uint64_t array to a buffer in target memory endianness */
423 void target_buffer_get_u64_array(struct target
*target
, const uint8_t *buffer
, uint32_t count
, uint64_t *dstbuf
)
426 for (i
= 0; i
< count
; i
++)
427 dstbuf
[i
] = target_buffer_get_u64(target
, &buffer
[i
* 8]);
430 /* write a uint32_t array to a buffer in target memory endianness */
431 void target_buffer_get_u32_array(struct target
*target
, const uint8_t *buffer
, uint32_t count
, uint32_t *dstbuf
)
434 for (i
= 0; i
< count
; i
++)
435 dstbuf
[i
] = target_buffer_get_u32(target
, &buffer
[i
* 4]);
438 /* write a uint16_t array to a buffer in target memory endianness */
439 void target_buffer_get_u16_array(struct target
*target
, const uint8_t *buffer
, uint32_t count
, uint16_t *dstbuf
)
442 for (i
= 0; i
< count
; i
++)
443 dstbuf
[i
] = target_buffer_get_u16(target
, &buffer
[i
* 2]);
446 /* write a uint64_t array to a buffer in target memory endianness */
447 void target_buffer_set_u64_array(struct target
*target
, uint8_t *buffer
, uint32_t count
, const uint64_t *srcbuf
)
450 for (i
= 0; i
< count
; i
++)
451 target_buffer_set_u64(target
, &buffer
[i
* 8], srcbuf
[i
]);
454 /* write a uint32_t array to a buffer in target memory endianness */
455 void target_buffer_set_u32_array(struct target
*target
, uint8_t *buffer
, uint32_t count
, const uint32_t *srcbuf
)
458 for (i
= 0; i
< count
; i
++)
459 target_buffer_set_u32(target
, &buffer
[i
* 4], srcbuf
[i
]);
462 /* write a uint16_t array to a buffer in target memory endianness */
463 void target_buffer_set_u16_array(struct target
*target
, uint8_t *buffer
, uint32_t count
, const uint16_t *srcbuf
)
466 for (i
= 0; i
< count
; i
++)
467 target_buffer_set_u16(target
, &buffer
[i
* 2], srcbuf
[i
]);
470 /* return a pointer to a configured target; id is name or number */
471 struct target
*get_target(const char *id
)
473 struct target
*target
;
475 /* try as tcltarget name */
476 for (target
= all_targets
; target
; target
= target
->next
) {
477 if (target_name(target
) == NULL
)
479 if (strcmp(id
, target_name(target
)) == 0)
483 /* It's OK to remove this fallback sometime after August 2010 or so */
485 /* no match, try as number */
487 if (parse_uint(id
, &num
) != ERROR_OK
)
490 for (target
= all_targets
; target
; target
= target
->next
) {
491 if (target
->target_number
== (int)num
) {
492 LOG_WARNING("use '%s' as target identifier, not '%u'",
493 target_name(target
), num
);
501 /* returns a pointer to the n-th configured target */
502 struct target
*get_target_by_num(int num
)
504 struct target
*target
= all_targets
;
507 if (target
->target_number
== num
)
509 target
= target
->next
;
515 struct target
*get_current_target(struct command_context
*cmd_ctx
)
517 struct target
*target
= get_target_by_num(cmd_ctx
->current_target
);
519 if (target
== NULL
) {
520 LOG_ERROR("BUG: current_target out of bounds");
527 int target_poll(struct target
*target
)
531 /* We can't poll until after examine */
532 if (!target_was_examined(target
)) {
533 /* Fail silently lest we pollute the log */
537 retval
= target
->type
->poll(target
);
538 if (retval
!= ERROR_OK
)
541 if (target
->halt_issued
) {
542 if (target
->state
== TARGET_HALTED
)
543 target
->halt_issued
= false;
545 int64_t t
= timeval_ms() - target
->halt_issued_time
;
546 if (t
> DEFAULT_HALT_TIMEOUT
) {
547 target
->halt_issued
= false;
548 LOG_INFO("Halt timed out, wake up GDB.");
549 target_call_event_callbacks(target
, TARGET_EVENT_GDB_HALT
);
557 int target_halt(struct target
*target
)
560 /* We can't poll until after examine */
561 if (!target_was_examined(target
)) {
562 LOG_ERROR("Target not examined yet");
566 retval
= target
->type
->halt(target
);
567 if (retval
!= ERROR_OK
)
570 target
->halt_issued
= true;
571 target
->halt_issued_time
= timeval_ms();
577 * Make the target (re)start executing using its saved execution
578 * context (possibly with some modifications).
580 * @param target Which target should start executing.
581 * @param current True to use the target's saved program counter instead
582 * of the address parameter
583 * @param address Optionally used as the program counter.
584 * @param handle_breakpoints True iff breakpoints at the resumption PC
585 * should be skipped. (For example, maybe execution was stopped by
586 * such a breakpoint, in which case it would be counterprodutive to
588 * @param debug_execution False if all working areas allocated by OpenOCD
589 * should be released and/or restored to their original contents.
590 * (This would for example be true to run some downloaded "helper"
591 * algorithm code, which resides in one such working buffer and uses
592 * another for data storage.)
594 * @todo Resolve the ambiguity about what the "debug_execution" flag
595 * signifies. For example, Target implementations don't agree on how
596 * it relates to invalidation of the register cache, or to whether
597 * breakpoints and watchpoints should be enabled. (It would seem wrong
598 * to enable breakpoints when running downloaded "helper" algorithms
599 * (debug_execution true), since the breakpoints would be set to match
600 * target firmware being debugged, not the helper algorithm.... and
601 * enabling them could cause such helpers to malfunction (for example,
602 * by overwriting data with a breakpoint instruction. On the other
603 * hand the infrastructure for running such helpers might use this
604 * procedure but rely on hardware breakpoint to detect termination.)
606 int target_resume(struct target
*target
, int current
, target_addr_t address
,
607 int handle_breakpoints
, int debug_execution
)
611 /* We can't poll until after examine */
612 if (!target_was_examined(target
)) {
613 LOG_ERROR("Target not examined yet");
617 target_call_event_callbacks(target
, TARGET_EVENT_RESUME_START
);
619 /* note that resume *must* be asynchronous. The CPU can halt before
620 * we poll. The CPU can even halt at the current PC as a result of
621 * a software breakpoint being inserted by (a bug?) the application.
623 retval
= target
->type
->resume(target
, current
, address
, handle_breakpoints
, debug_execution
);
624 if (retval
!= ERROR_OK
)
627 target_call_event_callbacks(target
, TARGET_EVENT_RESUME_END
);
632 static int target_process_reset(struct command_context
*cmd_ctx
, enum target_reset_mode reset_mode
)
637 n
= Jim_Nvp_value2name_simple(nvp_reset_modes
, reset_mode
);
638 if (n
->name
== NULL
) {
639 LOG_ERROR("invalid reset mode");
643 struct target
*target
;
644 for (target
= all_targets
; target
; target
= target
->next
)
645 target_call_reset_callbacks(target
, reset_mode
);
647 /* disable polling during reset to make reset event scripts
648 * more predictable, i.e. dr/irscan & pathmove in events will
649 * not have JTAG operations injected into the middle of a sequence.
651 bool save_poll
= jtag_poll_get_enabled();
653 jtag_poll_set_enabled(false);
655 sprintf(buf
, "ocd_process_reset %s", n
->name
);
656 retval
= Jim_Eval(cmd_ctx
->interp
, buf
);
658 jtag_poll_set_enabled(save_poll
);
660 if (retval
!= JIM_OK
) {
661 Jim_MakeErrorMessage(cmd_ctx
->interp
);
662 command_print(NULL
, "%s\n", Jim_GetString(Jim_GetResult(cmd_ctx
->interp
), NULL
));
666 /* We want any events to be processed before the prompt */
667 retval
= target_call_timer_callbacks_now();
669 for (target
= all_targets
; target
; target
= target
->next
) {
670 target
->type
->check_reset(target
);
671 target
->running_alg
= false;
677 static int identity_virt2phys(struct target
*target
,
678 target_addr_t
virtual, target_addr_t
*physical
)
684 static int no_mmu(struct target
*target
, int *enabled
)
690 static int default_examine(struct target
*target
)
692 target_set_examined(target
);
696 /* no check by default */
697 static int default_check_reset(struct target
*target
)
702 int target_examine_one(struct target
*target
)
704 target_call_event_callbacks(target
, TARGET_EVENT_EXAMINE_START
);
706 int retval
= target
->type
->examine(target
);
707 if (retval
!= ERROR_OK
)
710 target_call_event_callbacks(target
, TARGET_EVENT_EXAMINE_END
);
715 static int jtag_enable_callback(enum jtag_event event
, void *priv
)
717 struct target
*target
= priv
;
719 if (event
!= JTAG_TAP_EVENT_ENABLE
|| !target
->tap
->enabled
)
722 jtag_unregister_event_callback(jtag_enable_callback
, target
);
724 return target_examine_one(target
);
727 /* Targets that correctly implement init + examine, i.e.
728 * no communication with target during init:
732 int target_examine(void)
734 int retval
= ERROR_OK
;
735 struct target
*target
;
737 for (target
= all_targets
; target
; target
= target
->next
) {
738 /* defer examination, but don't skip it */
739 if (!target
->tap
->enabled
) {
740 jtag_register_event_callback(jtag_enable_callback
,
745 if (target
->defer_examine
)
748 retval
= target_examine_one(target
);
749 if (retval
!= ERROR_OK
)
755 const char *target_type_name(struct target
*target
)
757 return target
->type
->name
;
760 static int target_soft_reset_halt(struct target
*target
)
762 if (!target_was_examined(target
)) {
763 LOG_ERROR("Target not examined yet");
766 if (!target
->type
->soft_reset_halt
) {
767 LOG_ERROR("Target %s does not support soft_reset_halt",
768 target_name(target
));
771 return target
->type
->soft_reset_halt(target
);
775 * Downloads a target-specific native code algorithm to the target,
776 * and executes it. * Note that some targets may need to set up, enable,
777 * and tear down a breakpoint (hard or * soft) to detect algorithm
778 * termination, while others may support lower overhead schemes where
779 * soft breakpoints embedded in the algorithm automatically terminate the
782 * @param target used to run the algorithm
783 * @param arch_info target-specific description of the algorithm.
785 int target_run_algorithm(struct target
*target
,
786 int num_mem_params
, struct mem_param
*mem_params
,
787 int num_reg_params
, struct reg_param
*reg_param
,
788 uint32_t entry_point
, uint32_t exit_point
,
789 int timeout_ms
, void *arch_info
)
791 int retval
= ERROR_FAIL
;
793 if (!target_was_examined(target
)) {
794 LOG_ERROR("Target not examined yet");
797 if (!target
->type
->run_algorithm
) {
798 LOG_ERROR("Target type '%s' does not support %s",
799 target_type_name(target
), __func__
);
803 target
->running_alg
= true;
804 retval
= target
->type
->run_algorithm(target
,
805 num_mem_params
, mem_params
,
806 num_reg_params
, reg_param
,
807 entry_point
, exit_point
, timeout_ms
, arch_info
);
808 target
->running_alg
= false;
815 * Downloads a target-specific native code algorithm to the target,
816 * executes and leaves it running.
818 * @param target used to run the algorithm
819 * @param arch_info target-specific description of the algorithm.
821 int target_start_algorithm(struct target
*target
,
822 int num_mem_params
, struct mem_param
*mem_params
,
823 int num_reg_params
, struct reg_param
*reg_params
,
824 uint32_t entry_point
, uint32_t exit_point
,
827 int retval
= ERROR_FAIL
;
829 if (!target_was_examined(target
)) {
830 LOG_ERROR("Target not examined yet");
833 if (!target
->type
->start_algorithm
) {
834 LOG_ERROR("Target type '%s' does not support %s",
835 target_type_name(target
), __func__
);
838 if (target
->running_alg
) {
839 LOG_ERROR("Target is already running an algorithm");
843 target
->running_alg
= true;
844 retval
= target
->type
->start_algorithm(target
,
845 num_mem_params
, mem_params
,
846 num_reg_params
, reg_params
,
847 entry_point
, exit_point
, arch_info
);
854 * Waits for an algorithm started with target_start_algorithm() to complete.
856 * @param target used to run the algorithm
857 * @param arch_info target-specific description of the algorithm.
859 int target_wait_algorithm(struct target
*target
,
860 int num_mem_params
, struct mem_param
*mem_params
,
861 int num_reg_params
, struct reg_param
*reg_params
,
862 uint32_t exit_point
, int timeout_ms
,
865 int retval
= ERROR_FAIL
;
867 if (!target
->type
->wait_algorithm
) {
868 LOG_ERROR("Target type '%s' does not support %s",
869 target_type_name(target
), __func__
);
872 if (!target
->running_alg
) {
873 LOG_ERROR("Target is not running an algorithm");
877 retval
= target
->type
->wait_algorithm(target
,
878 num_mem_params
, mem_params
,
879 num_reg_params
, reg_params
,
880 exit_point
, timeout_ms
, arch_info
);
881 if (retval
!= ERROR_TARGET_TIMEOUT
)
882 target
->running_alg
= false;
889 * Executes a target-specific native code algorithm in the target.
890 * It differs from target_run_algorithm in that the algorithm is asynchronous.
891 * Because of this it requires an compliant algorithm:
892 * see contrib/loaders/flash/stm32f1x.S for example.
894 * @param target used to run the algorithm
897 int target_run_flash_async_algorithm(struct target
*target
,
898 const uint8_t *buffer
, uint32_t count
, int block_size
,
899 int num_mem_params
, struct mem_param
*mem_params
,
900 int num_reg_params
, struct reg_param
*reg_params
,
901 uint32_t buffer_start
, uint32_t buffer_size
,
902 uint32_t entry_point
, uint32_t exit_point
, void *arch_info
)
907 const uint8_t *buffer_orig
= buffer
;
909 /* Set up working area. First word is write pointer, second word is read pointer,
910 * rest is fifo data area. */
911 uint32_t wp_addr
= buffer_start
;
912 uint32_t rp_addr
= buffer_start
+ 4;
913 uint32_t fifo_start_addr
= buffer_start
+ 8;
914 uint32_t fifo_end_addr
= buffer_start
+ buffer_size
;
916 uint32_t wp
= fifo_start_addr
;
917 uint32_t rp
= fifo_start_addr
;
919 /* validate block_size is 2^n */
920 assert(!block_size
|| !(block_size
& (block_size
- 1)));
922 retval
= target_write_u32(target
, wp_addr
, wp
);
923 if (retval
!= ERROR_OK
)
925 retval
= target_write_u32(target
, rp_addr
, rp
);
926 if (retval
!= ERROR_OK
)
929 /* Start up algorithm on target and let it idle while writing the first chunk */
930 retval
= target_start_algorithm(target
, num_mem_params
, mem_params
,
931 num_reg_params
, reg_params
,
936 if (retval
!= ERROR_OK
) {
937 LOG_ERROR("error starting target flash write algorithm");
943 retval
= target_read_u32(target
, rp_addr
, &rp
);
944 if (retval
!= ERROR_OK
) {
945 LOG_ERROR("failed to get read pointer");
949 LOG_DEBUG("offs 0x%zx count 0x%" PRIx32
" wp 0x%" PRIx32
" rp 0x%" PRIx32
,
950 (size_t) (buffer
- buffer_orig
), count
, wp
, rp
);
953 LOG_ERROR("flash write algorithm aborted by target");
954 retval
= ERROR_FLASH_OPERATION_FAILED
;
958 if (((rp
- fifo_start_addr
) & (block_size
- 1)) || rp
< fifo_start_addr
|| rp
>= fifo_end_addr
) {
959 LOG_ERROR("corrupted fifo read pointer 0x%" PRIx32
, rp
);
963 /* Count the number of bytes available in the fifo without
964 * crossing the wrap around. Make sure to not fill it completely,
965 * because that would make wp == rp and that's the empty condition. */
966 uint32_t thisrun_bytes
;
968 thisrun_bytes
= rp
- wp
- block_size
;
969 else if (rp
> fifo_start_addr
)
970 thisrun_bytes
= fifo_end_addr
- wp
;
972 thisrun_bytes
= fifo_end_addr
- wp
- block_size
;
974 if (thisrun_bytes
== 0) {
975 /* Throttle polling a bit if transfer is (much) faster than flash
976 * programming. The exact delay shouldn't matter as long as it's
977 * less than buffer size / flash speed. This is very unlikely to
978 * run when using high latency connections such as USB. */
981 /* to stop an infinite loop on some targets check and increment a timeout
982 * this issue was observed on a stellaris using the new ICDI interface */
983 if (timeout
++ >= 500) {
984 LOG_ERROR("timeout waiting for algorithm, a target reset is recommended");
985 return ERROR_FLASH_OPERATION_FAILED
;
990 /* reset our timeout */
993 /* Limit to the amount of data we actually want to write */
994 if (thisrun_bytes
> count
* block_size
)
995 thisrun_bytes
= count
* block_size
;
997 /* Write data to fifo */
998 retval
= target_write_buffer(target
, wp
, thisrun_bytes
, buffer
);
999 if (retval
!= ERROR_OK
)
1002 /* Update counters and wrap write pointer */
1003 buffer
+= thisrun_bytes
;
1004 count
-= thisrun_bytes
/ block_size
;
1005 wp
+= thisrun_bytes
;
1006 if (wp
>= fifo_end_addr
)
1007 wp
= fifo_start_addr
;
1009 /* Store updated write pointer to target */
1010 retval
= target_write_u32(target
, wp_addr
, wp
);
1011 if (retval
!= ERROR_OK
)
1015 if (retval
!= ERROR_OK
) {
1016 /* abort flash write algorithm on target */
1017 target_write_u32(target
, wp_addr
, 0);
1020 int retval2
= target_wait_algorithm(target
, num_mem_params
, mem_params
,
1021 num_reg_params
, reg_params
,
1026 if (retval2
!= ERROR_OK
) {
1027 LOG_ERROR("error waiting for target flash write algorithm");
1031 if (retval
== ERROR_OK
) {
1032 /* check if algorithm set rp = 0 after fifo writer loop finished */
1033 retval
= target_read_u32(target
, rp_addr
, &rp
);
1034 if (retval
== ERROR_OK
&& rp
== 0) {
1035 LOG_ERROR("flash write algorithm aborted by target");
1036 retval
= ERROR_FLASH_OPERATION_FAILED
;
1043 int target_read_memory(struct target
*target
,
1044 target_addr_t address
, uint32_t size
, uint32_t count
, uint8_t *buffer
)
1046 if (!target_was_examined(target
)) {
1047 LOG_ERROR("Target not examined yet");
1050 if (!target
->type
->read_memory
) {
1051 LOG_ERROR("Target %s doesn't support read_memory", target_name(target
));
1054 return target
->type
->read_memory(target
, address
, size
, count
, buffer
);
1057 int target_read_phys_memory(struct target
*target
,
1058 target_addr_t address
, uint32_t size
, uint32_t count
, uint8_t *buffer
)
1060 if (!target_was_examined(target
)) {
1061 LOG_ERROR("Target not examined yet");
1064 if (!target
->type
->read_phys_memory
) {
1065 LOG_ERROR("Target %s doesn't support read_phys_memory", target_name(target
));
1068 return target
->type
->read_phys_memory(target
, address
, size
, count
, buffer
);
1071 int target_write_memory(struct target
*target
,
1072 target_addr_t address
, uint32_t size
, uint32_t count
, const uint8_t *buffer
)
1074 if (!target_was_examined(target
)) {
1075 LOG_ERROR("Target not examined yet");
1078 if (!target
->type
->write_memory
) {
1079 LOG_ERROR("Target %s doesn't support write_memory", target_name(target
));
1082 return target
->type
->write_memory(target
, address
, size
, count
, buffer
);
1085 int target_write_phys_memory(struct target
*target
,
1086 target_addr_t address
, uint32_t size
, uint32_t count
, const uint8_t *buffer
)
1088 if (!target_was_examined(target
)) {
1089 LOG_ERROR("Target not examined yet");
1092 if (!target
->type
->write_phys_memory
) {
1093 LOG_ERROR("Target %s doesn't support write_phys_memory", target_name(target
));
1096 return target
->type
->write_phys_memory(target
, address
, size
, count
, buffer
);
1099 int target_add_breakpoint(struct target
*target
,
1100 struct breakpoint
*breakpoint
)
1102 if ((target
->state
!= TARGET_HALTED
) && (breakpoint
->type
!= BKPT_HARD
)) {
1103 LOG_WARNING("target %s is not halted (add breakpoint)", target_name(target
));
1104 return ERROR_TARGET_NOT_HALTED
;
1106 return target
->type
->add_breakpoint(target
, breakpoint
);
1109 int target_add_context_breakpoint(struct target
*target
,
1110 struct breakpoint
*breakpoint
)
1112 if (target
->state
!= TARGET_HALTED
) {
1113 LOG_WARNING("target %s is not halted (add context breakpoint)", target_name(target
));
1114 return ERROR_TARGET_NOT_HALTED
;
1116 return target
->type
->add_context_breakpoint(target
, breakpoint
);
1119 int target_add_hybrid_breakpoint(struct target
*target
,
1120 struct breakpoint
*breakpoint
)
1122 if (target
->state
!= TARGET_HALTED
) {
1123 LOG_WARNING("target %s is not halted (add hybrid breakpoint)", target_name(target
));
1124 return ERROR_TARGET_NOT_HALTED
;
1126 return target
->type
->add_hybrid_breakpoint(target
, breakpoint
);
1129 int target_remove_breakpoint(struct target
*target
,
1130 struct breakpoint
*breakpoint
)
1132 return target
->type
->remove_breakpoint(target
, breakpoint
);
1135 int target_add_watchpoint(struct target
*target
,
1136 struct watchpoint
*watchpoint
)
1138 if (target
->state
!= TARGET_HALTED
) {
1139 LOG_WARNING("target %s is not halted (add watchpoint)", target_name(target
));
1140 return ERROR_TARGET_NOT_HALTED
;
1142 return target
->type
->add_watchpoint(target
, watchpoint
);
1144 int target_remove_watchpoint(struct target
*target
,
1145 struct watchpoint
*watchpoint
)
1147 return target
->type
->remove_watchpoint(target
, watchpoint
);
1149 int target_hit_watchpoint(struct target
*target
,
1150 struct watchpoint
**hit_watchpoint
)
1152 if (target
->state
!= TARGET_HALTED
) {
1153 LOG_WARNING("target %s is not halted (hit watchpoint)", target
->cmd_name
);
1154 return ERROR_TARGET_NOT_HALTED
;
1157 if (target
->type
->hit_watchpoint
== NULL
) {
1158 /* For backward compatible, if hit_watchpoint is not implemented,
1159 * return ERROR_FAIL such that gdb_server will not take the nonsense
1164 return target
->type
->hit_watchpoint(target
, hit_watchpoint
);
1167 int target_get_gdb_reg_list(struct target
*target
,
1168 struct reg
**reg_list
[], int *reg_list_size
,
1169 enum target_register_class reg_class
)
1171 return target
->type
->get_gdb_reg_list(target
, reg_list
, reg_list_size
, reg_class
);
1173 int target_step(struct target
*target
,
1174 int current
, target_addr_t address
, int handle_breakpoints
)
1176 return target
->type
->step(target
, current
, address
, handle_breakpoints
);
1179 int target_get_gdb_fileio_info(struct target
*target
, struct gdb_fileio_info
*fileio_info
)
1181 if (target
->state
!= TARGET_HALTED
) {
1182 LOG_WARNING("target %s is not halted (gdb fileio)", target
->cmd_name
);
1183 return ERROR_TARGET_NOT_HALTED
;
1185 return target
->type
->get_gdb_fileio_info(target
, fileio_info
);
1188 int target_gdb_fileio_end(struct target
*target
, int retcode
, int fileio_errno
, bool ctrl_c
)
1190 if (target
->state
!= TARGET_HALTED
) {
1191 LOG_WARNING("target %s is not halted (gdb fileio end)", target
->cmd_name
);
1192 return ERROR_TARGET_NOT_HALTED
;
1194 return target
->type
->gdb_fileio_end(target
, retcode
, fileio_errno
, ctrl_c
);
1197 int target_profiling(struct target
*target
, uint32_t *samples
,
1198 uint32_t max_num_samples
, uint32_t *num_samples
, uint32_t seconds
)
1200 if (target
->state
!= TARGET_HALTED
) {
1201 LOG_WARNING("target %s is not halted (profiling)", target
->cmd_name
);
1202 return ERROR_TARGET_NOT_HALTED
;
1204 return target
->type
->profiling(target
, samples
, max_num_samples
,
1205 num_samples
, seconds
);
1209 * Reset the @c examined flag for the given target.
1210 * Pure paranoia -- targets are zeroed on allocation.
1212 static void target_reset_examined(struct target
*target
)
1214 target
->examined
= false;
1217 static int handle_target(void *priv
);
1219 static int target_init_one(struct command_context
*cmd_ctx
,
1220 struct target
*target
)
1222 target_reset_examined(target
);
1224 struct target_type
*type
= target
->type
;
1225 if (type
->examine
== NULL
)
1226 type
->examine
= default_examine
;
1228 if (type
->check_reset
== NULL
)
1229 type
->check_reset
= default_check_reset
;
1231 assert(type
->init_target
!= NULL
);
1233 int retval
= type
->init_target(cmd_ctx
, target
);
1234 if (ERROR_OK
!= retval
) {
1235 LOG_ERROR("target '%s' init failed", target_name(target
));
1239 /* Sanity-check MMU support ... stub in what we must, to help
1240 * implement it in stages, but warn if we need to do so.
1243 if (type
->virt2phys
== NULL
) {
1244 LOG_ERROR("type '%s' is missing virt2phys", type
->name
);
1245 type
->virt2phys
= identity_virt2phys
;
1248 /* Make sure no-MMU targets all behave the same: make no
1249 * distinction between physical and virtual addresses, and
1250 * ensure that virt2phys() is always an identity mapping.
1252 if (type
->write_phys_memory
|| type
->read_phys_memory
|| type
->virt2phys
)
1253 LOG_WARNING("type '%s' has bad MMU hooks", type
->name
);
1256 type
->write_phys_memory
= type
->write_memory
;
1257 type
->read_phys_memory
= type
->read_memory
;
1258 type
->virt2phys
= identity_virt2phys
;
1261 if (target
->type
->read_buffer
== NULL
)
1262 target
->type
->read_buffer
= target_read_buffer_default
;
1264 if (target
->type
->write_buffer
== NULL
)
1265 target
->type
->write_buffer
= target_write_buffer_default
;
1267 if (target
->type
->get_gdb_fileio_info
== NULL
)
1268 target
->type
->get_gdb_fileio_info
= target_get_gdb_fileio_info_default
;
1270 if (target
->type
->gdb_fileio_end
== NULL
)
1271 target
->type
->gdb_fileio_end
= target_gdb_fileio_end_default
;
1273 if (target
->type
->profiling
== NULL
)
1274 target
->type
->profiling
= target_profiling_default
;
1279 static int target_init(struct command_context
*cmd_ctx
)
1281 struct target
*target
;
1284 for (target
= all_targets
; target
; target
= target
->next
) {
1285 retval
= target_init_one(cmd_ctx
, target
);
1286 if (ERROR_OK
!= retval
)
1293 retval
= target_register_user_commands(cmd_ctx
);
1294 if (ERROR_OK
!= retval
)
1297 retval
= target_register_timer_callback(&handle_target
,
1298 polling_interval
, 1, cmd_ctx
->interp
);
1299 if (ERROR_OK
!= retval
)
1305 COMMAND_HANDLER(handle_target_init_command
)
1310 return ERROR_COMMAND_SYNTAX_ERROR
;
1312 static bool target_initialized
;
1313 if (target_initialized
) {
1314 LOG_INFO("'target init' has already been called");
1317 target_initialized
= true;
1319 retval
= command_run_line(CMD_CTX
, "init_targets");
1320 if (ERROR_OK
!= retval
)
1323 retval
= command_run_line(CMD_CTX
, "init_target_events");
1324 if (ERROR_OK
!= retval
)
1327 retval
= command_run_line(CMD_CTX
, "init_board");
1328 if (ERROR_OK
!= retval
)
1331 LOG_DEBUG("Initializing targets...");
1332 return target_init(CMD_CTX
);
1335 int target_register_event_callback(int (*callback
)(struct target
*target
,
1336 enum target_event event
, void *priv
), void *priv
)
1338 struct target_event_callback
**callbacks_p
= &target_event_callbacks
;
1340 if (callback
== NULL
)
1341 return ERROR_COMMAND_SYNTAX_ERROR
;
1344 while ((*callbacks_p
)->next
)
1345 callbacks_p
= &((*callbacks_p
)->next
);
1346 callbacks_p
= &((*callbacks_p
)->next
);
1349 (*callbacks_p
) = malloc(sizeof(struct target_event_callback
));
1350 (*callbacks_p
)->callback
= callback
;
1351 (*callbacks_p
)->priv
= priv
;
1352 (*callbacks_p
)->next
= NULL
;
1357 int target_register_reset_callback(int (*callback
)(struct target
*target
,
1358 enum target_reset_mode reset_mode
, void *priv
), void *priv
)
1360 struct target_reset_callback
*entry
;
1362 if (callback
== NULL
)
1363 return ERROR_COMMAND_SYNTAX_ERROR
;
1365 entry
= malloc(sizeof(struct target_reset_callback
));
1366 if (entry
== NULL
) {
1367 LOG_ERROR("error allocating buffer for reset callback entry");
1368 return ERROR_COMMAND_SYNTAX_ERROR
;
1371 entry
->callback
= callback
;
1373 list_add(&entry
->list
, &target_reset_callback_list
);
1379 int target_register_trace_callback(int (*callback
)(struct target
*target
,
1380 size_t len
, uint8_t *data
, void *priv
), void *priv
)
1382 struct target_trace_callback
*entry
;
1384 if (callback
== NULL
)
1385 return ERROR_COMMAND_SYNTAX_ERROR
;
1387 entry
= malloc(sizeof(struct target_trace_callback
));
1388 if (entry
== NULL
) {
1389 LOG_ERROR("error allocating buffer for trace callback entry");
1390 return ERROR_COMMAND_SYNTAX_ERROR
;
1393 entry
->callback
= callback
;
1395 list_add(&entry
->list
, &target_trace_callback_list
);
1401 int target_register_timer_callback(int (*callback
)(void *priv
), int time_ms
, int periodic
, void *priv
)
1403 struct target_timer_callback
**callbacks_p
= &target_timer_callbacks
;
1406 if (callback
== NULL
)
1407 return ERROR_COMMAND_SYNTAX_ERROR
;
1410 while ((*callbacks_p
)->next
)
1411 callbacks_p
= &((*callbacks_p
)->next
);
1412 callbacks_p
= &((*callbacks_p
)->next
);
1415 (*callbacks_p
) = malloc(sizeof(struct target_timer_callback
));
1416 (*callbacks_p
)->callback
= callback
;
1417 (*callbacks_p
)->periodic
= periodic
;
1418 (*callbacks_p
)->time_ms
= time_ms
;
1419 (*callbacks_p
)->removed
= false;
1421 gettimeofday(&now
, NULL
);
1422 (*callbacks_p
)->when
.tv_usec
= now
.tv_usec
+ (time_ms
% 1000) * 1000;
1423 time_ms
-= (time_ms
% 1000);
1424 (*callbacks_p
)->when
.tv_sec
= now
.tv_sec
+ (time_ms
/ 1000);
1425 if ((*callbacks_p
)->when
.tv_usec
> 1000000) {
1426 (*callbacks_p
)->when
.tv_usec
= (*callbacks_p
)->when
.tv_usec
- 1000000;
1427 (*callbacks_p
)->when
.tv_sec
+= 1;
1430 (*callbacks_p
)->priv
= priv
;
1431 (*callbacks_p
)->next
= NULL
;
1436 int target_unregister_event_callback(int (*callback
)(struct target
*target
,
1437 enum target_event event
, void *priv
), void *priv
)
1439 struct target_event_callback
**p
= &target_event_callbacks
;
1440 struct target_event_callback
*c
= target_event_callbacks
;
1442 if (callback
== NULL
)
1443 return ERROR_COMMAND_SYNTAX_ERROR
;
1446 struct target_event_callback
*next
= c
->next
;
1447 if ((c
->callback
== callback
) && (c
->priv
== priv
)) {
1459 int target_unregister_reset_callback(int (*callback
)(struct target
*target
,
1460 enum target_reset_mode reset_mode
, void *priv
), void *priv
)
1462 struct target_reset_callback
*entry
;
1464 if (callback
== NULL
)
1465 return ERROR_COMMAND_SYNTAX_ERROR
;
1467 list_for_each_entry(entry
, &target_reset_callback_list
, list
) {
1468 if (entry
->callback
== callback
&& entry
->priv
== priv
) {
1469 list_del(&entry
->list
);
1478 int target_unregister_trace_callback(int (*callback
)(struct target
*target
,
1479 size_t len
, uint8_t *data
, void *priv
), void *priv
)
1481 struct target_trace_callback
*entry
;
1483 if (callback
== NULL
)
1484 return ERROR_COMMAND_SYNTAX_ERROR
;
1486 list_for_each_entry(entry
, &target_trace_callback_list
, list
) {
1487 if (entry
->callback
== callback
&& entry
->priv
== priv
) {
1488 list_del(&entry
->list
);
1497 int target_unregister_timer_callback(int (*callback
)(void *priv
), void *priv
)
1499 if (callback
== NULL
)
1500 return ERROR_COMMAND_SYNTAX_ERROR
;
1502 for (struct target_timer_callback
*c
= target_timer_callbacks
;
1504 if ((c
->callback
== callback
) && (c
->priv
== priv
)) {
1513 int target_call_event_callbacks(struct target
*target
, enum target_event event
)
1515 struct target_event_callback
*callback
= target_event_callbacks
;
1516 struct target_event_callback
*next_callback
;
1518 if (event
== TARGET_EVENT_HALTED
) {
1519 /* execute early halted first */
1520 target_call_event_callbacks(target
, TARGET_EVENT_GDB_HALT
);
1523 LOG_DEBUG("target event %i (%s)", event
,
1524 Jim_Nvp_value2name_simple(nvp_target_event
, event
)->name
);
1526 target_handle_event(target
, event
);
1529 next_callback
= callback
->next
;
1530 callback
->callback(target
, event
, callback
->priv
);
1531 callback
= next_callback
;
1537 int target_call_reset_callbacks(struct target
*target
, enum target_reset_mode reset_mode
)
1539 struct target_reset_callback
*callback
;
1541 LOG_DEBUG("target reset %i (%s)", reset_mode
,
1542 Jim_Nvp_value2name_simple(nvp_reset_modes
, reset_mode
)->name
);
1544 list_for_each_entry(callback
, &target_reset_callback_list
, list
)
1545 callback
->callback(target
, reset_mode
, callback
->priv
);
1550 int target_call_trace_callbacks(struct target
*target
, size_t len
, uint8_t *data
)
1552 struct target_trace_callback
*callback
;
1554 list_for_each_entry(callback
, &target_trace_callback_list
, list
)
1555 callback
->callback(target
, len
, data
, callback
->priv
);
1560 static int target_timer_callback_periodic_restart(
1561 struct target_timer_callback
*cb
, struct timeval
*now
)
1563 int time_ms
= cb
->time_ms
;
1564 cb
->when
.tv_usec
= now
->tv_usec
+ (time_ms
% 1000) * 1000;
1565 time_ms
-= (time_ms
% 1000);
1566 cb
->when
.tv_sec
= now
->tv_sec
+ time_ms
/ 1000;
1567 if (cb
->when
.tv_usec
> 1000000) {
1568 cb
->when
.tv_usec
= cb
->when
.tv_usec
- 1000000;
1569 cb
->when
.tv_sec
+= 1;
1574 static int target_call_timer_callback(struct target_timer_callback
*cb
,
1575 struct timeval
*now
)
1577 cb
->callback(cb
->priv
);
1580 return target_timer_callback_periodic_restart(cb
, now
);
1582 return target_unregister_timer_callback(cb
->callback
, cb
->priv
);
1585 static int target_call_timer_callbacks_check_time(int checktime
)
1587 static bool callback_processing
;
1589 /* Do not allow nesting */
1590 if (callback_processing
)
1593 callback_processing
= true;
1598 gettimeofday(&now
, NULL
);
1600 /* Store an address of the place containing a pointer to the
1601 * next item; initially, that's a standalone "root of the
1602 * list" variable. */
1603 struct target_timer_callback
**callback
= &target_timer_callbacks
;
1605 if ((*callback
)->removed
) {
1606 struct target_timer_callback
*p
= *callback
;
1607 *callback
= (*callback
)->next
;
1612 bool call_it
= (*callback
)->callback
&&
1613 ((!checktime
&& (*callback
)->periodic
) ||
1614 now
.tv_sec
> (*callback
)->when
.tv_sec
||
1615 (now
.tv_sec
== (*callback
)->when
.tv_sec
&&
1616 now
.tv_usec
>= (*callback
)->when
.tv_usec
));
1619 target_call_timer_callback(*callback
, &now
);
1621 callback
= &(*callback
)->next
;
1624 callback_processing
= false;
1628 int target_call_timer_callbacks(void)
1630 return target_call_timer_callbacks_check_time(1);
1633 /* invoke periodic callbacks immediately */
1634 int target_call_timer_callbacks_now(void)
1636 return target_call_timer_callbacks_check_time(0);
1639 /* Prints the working area layout for debug purposes */
1640 static void print_wa_layout(struct target
*target
)
1642 struct working_area
*c
= target
->working_areas
;
1645 LOG_DEBUG("%c%c " TARGET_ADDR_FMT
"-" TARGET_ADDR_FMT
" (%" PRIu32
" bytes)",
1646 c
->backup
? 'b' : ' ', c
->free
? ' ' : '*',
1647 c
->address
, c
->address
+ c
->size
- 1, c
->size
);
1652 /* Reduce area to size bytes, create a new free area from the remaining bytes, if any. */
1653 static void target_split_working_area(struct working_area
*area
, uint32_t size
)
1655 assert(area
->free
); /* Shouldn't split an allocated area */
1656 assert(size
<= area
->size
); /* Caller should guarantee this */
1658 /* Split only if not already the right size */
1659 if (size
< area
->size
) {
1660 struct working_area
*new_wa
= malloc(sizeof(*new_wa
));
1665 new_wa
->next
= area
->next
;
1666 new_wa
->size
= area
->size
- size
;
1667 new_wa
->address
= area
->address
+ size
;
1668 new_wa
->backup
= NULL
;
1669 new_wa
->user
= NULL
;
1670 new_wa
->free
= true;
1672 area
->next
= new_wa
;
1675 /* If backup memory was allocated to this area, it has the wrong size
1676 * now so free it and it will be reallocated if/when needed */
1679 area
->backup
= NULL
;
1684 /* Merge all adjacent free areas into one */
1685 static void target_merge_working_areas(struct target
*target
)
1687 struct working_area
*c
= target
->working_areas
;
1689 while (c
&& c
->next
) {
1690 assert(c
->next
->address
== c
->address
+ c
->size
); /* This is an invariant */
1692 /* Find two adjacent free areas */
1693 if (c
->free
&& c
->next
->free
) {
1694 /* Merge the last into the first */
1695 c
->size
+= c
->next
->size
;
1697 /* Remove the last */
1698 struct working_area
*to_be_freed
= c
->next
;
1699 c
->next
= c
->next
->next
;
1700 if (to_be_freed
->backup
)
1701 free(to_be_freed
->backup
);
1704 /* If backup memory was allocated to the remaining area, it's has
1705 * the wrong size now */
1716 int target_alloc_working_area_try(struct target
*target
, uint32_t size
, struct working_area
**area
)
1718 /* Reevaluate working area address based on MMU state*/
1719 if (target
->working_areas
== NULL
) {
1723 retval
= target
->type
->mmu(target
, &enabled
);
1724 if (retval
!= ERROR_OK
)
1728 if (target
->working_area_phys_spec
) {
1729 LOG_DEBUG("MMU disabled, using physical "
1730 "address for working memory " TARGET_ADDR_FMT
,
1731 target
->working_area_phys
);
1732 target
->working_area
= target
->working_area_phys
;
1734 LOG_ERROR("No working memory available. "
1735 "Specify -work-area-phys to target.");
1736 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
1739 if (target
->working_area_virt_spec
) {
1740 LOG_DEBUG("MMU enabled, using virtual "
1741 "address for working memory " TARGET_ADDR_FMT
,
1742 target
->working_area_virt
);
1743 target
->working_area
= target
->working_area_virt
;
1745 LOG_ERROR("No working memory available. "
1746 "Specify -work-area-virt to target.");
1747 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
1751 /* Set up initial working area on first call */
1752 struct working_area
*new_wa
= malloc(sizeof(*new_wa
));
1754 new_wa
->next
= NULL
;
1755 new_wa
->size
= target
->working_area_size
& ~3UL; /* 4-byte align */
1756 new_wa
->address
= target
->working_area
;
1757 new_wa
->backup
= NULL
;
1758 new_wa
->user
= NULL
;
1759 new_wa
->free
= true;
1762 target
->working_areas
= new_wa
;
1765 /* only allocate multiples of 4 byte */
1767 size
= (size
+ 3) & (~3UL);
1769 struct working_area
*c
= target
->working_areas
;
1771 /* Find the first large enough working area */
1773 if (c
->free
&& c
->size
>= size
)
1779 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
1781 /* Split the working area into the requested size */
1782 target_split_working_area(c
, size
);
1784 LOG_DEBUG("allocated new working area of %" PRIu32
" bytes at address " TARGET_ADDR_FMT
,
1787 if (target
->backup_working_area
) {
1788 if (c
->backup
== NULL
) {
1789 c
->backup
= malloc(c
->size
);
1790 if (c
->backup
== NULL
)
1794 int retval
= target_read_memory(target
, c
->address
, 4, c
->size
/ 4, c
->backup
);
1795 if (retval
!= ERROR_OK
)
1799 /* mark as used, and return the new (reused) area */
1806 print_wa_layout(target
);
1811 int target_alloc_working_area(struct target
*target
, uint32_t size
, struct working_area
**area
)
1815 retval
= target_alloc_working_area_try(target
, size
, area
);
1816 if (retval
== ERROR_TARGET_RESOURCE_NOT_AVAILABLE
)
1817 LOG_WARNING("not enough working area available(requested %"PRIu32
")", size
);
1822 static int target_restore_working_area(struct target
*target
, struct working_area
*area
)
1824 int retval
= ERROR_OK
;
1826 if (target
->backup_working_area
&& area
->backup
!= NULL
) {
1827 retval
= target_write_memory(target
, area
->address
, 4, area
->size
/ 4, area
->backup
);
1828 if (retval
!= ERROR_OK
)
1829 LOG_ERROR("failed to restore %" PRIu32
" bytes of working area at address " TARGET_ADDR_FMT
,
1830 area
->size
, area
->address
);
1836 /* Restore the area's backup memory, if any, and return the area to the allocation pool */
1837 static int target_free_working_area_restore(struct target
*target
, struct working_area
*area
, int restore
)
1839 int retval
= ERROR_OK
;
1845 retval
= target_restore_working_area(target
, area
);
1846 /* REVISIT: Perhaps the area should be freed even if restoring fails. */
1847 if (retval
!= ERROR_OK
)
1853 LOG_DEBUG("freed %" PRIu32
" bytes of working area at address " TARGET_ADDR_FMT
,
1854 area
->size
, area
->address
);
1856 /* mark user pointer invalid */
1857 /* TODO: Is this really safe? It points to some previous caller's memory.
1858 * How could we know that the area pointer is still in that place and not
1859 * some other vital data? What's the purpose of this, anyway? */
1863 target_merge_working_areas(target
);
1865 print_wa_layout(target
);
1870 int target_free_working_area(struct target
*target
, struct working_area
*area
)
1872 return target_free_working_area_restore(target
, area
, 1);
1875 static void target_destroy(struct target
*target
)
1877 if (target
->type
->deinit_target
)
1878 target
->type
->deinit_target(target
);
1881 free(target
->trace_info
);
1882 free(target
->cmd_name
);
1886 void target_quit(void)
1888 struct target_event_callback
*pe
= target_event_callbacks
;
1890 struct target_event_callback
*t
= pe
->next
;
1894 target_event_callbacks
= NULL
;
1896 struct target_timer_callback
*pt
= target_timer_callbacks
;
1898 struct target_timer_callback
*t
= pt
->next
;
1902 target_timer_callbacks
= NULL
;
1904 for (struct target
*target
= all_targets
; target
;) {
1908 target_destroy(target
);
1915 /* free resources and restore memory, if restoring memory fails,
1916 * free up resources anyway
1918 static void target_free_all_working_areas_restore(struct target
*target
, int restore
)
1920 struct working_area
*c
= target
->working_areas
;
1922 LOG_DEBUG("freeing all working areas");
1924 /* Loop through all areas, restoring the allocated ones and marking them as free */
1928 target_restore_working_area(target
, c
);
1930 *c
->user
= NULL
; /* Same as above */
1936 /* Run a merge pass to combine all areas into one */
1937 target_merge_working_areas(target
);
1939 print_wa_layout(target
);
1942 void target_free_all_working_areas(struct target
*target
)
1944 target_free_all_working_areas_restore(target
, 1);
1947 /* Find the largest number of bytes that can be allocated */
1948 uint32_t target_get_working_area_avail(struct target
*target
)
1950 struct working_area
*c
= target
->working_areas
;
1951 uint32_t max_size
= 0;
1954 return target
->working_area_size
;
1957 if (c
->free
&& max_size
< c
->size
)
1966 int target_arch_state(struct target
*target
)
1969 if (target
== NULL
) {
1970 LOG_WARNING("No target has been configured");
1974 if (target
->state
!= TARGET_HALTED
)
1977 retval
= target
->type
->arch_state(target
);
1981 static int target_get_gdb_fileio_info_default(struct target
*target
,
1982 struct gdb_fileio_info
*fileio_info
)
1984 /* If target does not support semi-hosting function, target
1985 has no need to provide .get_gdb_fileio_info callback.
1986 It just return ERROR_FAIL and gdb_server will return "Txx"
1987 as target halted every time. */
1991 static int target_gdb_fileio_end_default(struct target
*target
,
1992 int retcode
, int fileio_errno
, bool ctrl_c
)
1997 static int target_profiling_default(struct target
*target
, uint32_t *samples
,
1998 uint32_t max_num_samples
, uint32_t *num_samples
, uint32_t seconds
)
2000 struct timeval timeout
, now
;
2002 gettimeofday(&timeout
, NULL
);
2003 timeval_add_time(&timeout
, seconds
, 0);
2005 LOG_INFO("Starting profiling. Halting and resuming the"
2006 " target as often as we can...");
2008 uint32_t sample_count
= 0;
2009 /* hopefully it is safe to cache! We want to stop/restart as quickly as possible. */
2010 struct reg
*reg
= register_get_by_name(target
->reg_cache
, "pc", 1);
2012 int retval
= ERROR_OK
;
2014 target_poll(target
);
2015 if (target
->state
== TARGET_HALTED
) {
2016 uint32_t t
= buf_get_u32(reg
->value
, 0, 32);
2017 samples
[sample_count
++] = t
;
2018 /* current pc, addr = 0, do not handle breakpoints, not debugging */
2019 retval
= target_resume(target
, 1, 0, 0, 0);
2020 target_poll(target
);
2021 alive_sleep(10); /* sleep 10ms, i.e. <100 samples/second. */
2022 } else if (target
->state
== TARGET_RUNNING
) {
2023 /* We want to quickly sample the PC. */
2024 retval
= target_halt(target
);
2026 LOG_INFO("Target not halted or running");
2031 if (retval
!= ERROR_OK
)
2034 gettimeofday(&now
, NULL
);
2035 if ((sample_count
>= max_num_samples
) ||
2036 ((now
.tv_sec
>= timeout
.tv_sec
) && (now
.tv_usec
>= timeout
.tv_usec
))) {
2037 LOG_INFO("Profiling completed. %" PRIu32
" samples.", sample_count
);
2042 *num_samples
= sample_count
;
2046 /* Single aligned words are guaranteed to use 16 or 32 bit access
2047 * mode respectively, otherwise data is handled as quickly as
2050 int target_write_buffer(struct target
*target
, target_addr_t address
, uint32_t size
, const uint8_t *buffer
)
2052 LOG_DEBUG("writing buffer of %" PRIi32
" byte at " TARGET_ADDR_FMT
,
2055 if (!target_was_examined(target
)) {
2056 LOG_ERROR("Target not examined yet");
2063 if ((address
+ size
- 1) < address
) {
2064 /* GDB can request this when e.g. PC is 0xfffffffc */
2065 LOG_ERROR("address + size wrapped (" TARGET_ADDR_FMT
", 0x%08" PRIx32
")",
2071 return target
->type
->write_buffer(target
, address
, size
, buffer
);
2074 static int target_write_buffer_default(struct target
*target
,
2075 target_addr_t address
, uint32_t count
, const uint8_t *buffer
)
2079 /* Align up to maximum 4 bytes. The loop condition makes sure the next pass
2080 * will have something to do with the size we leave to it. */
2081 for (size
= 1; size
< 4 && count
>= size
* 2 + (address
& size
); size
*= 2) {
2082 if (address
& size
) {
2083 int retval
= target_write_memory(target
, address
, size
, 1, buffer
);
2084 if (retval
!= ERROR_OK
)
2092 /* Write the data with as large access size as possible. */
2093 for (; size
> 0; size
/= 2) {
2094 uint32_t aligned
= count
- count
% size
;
2096 int retval
= target_write_memory(target
, address
, size
, aligned
/ size
, buffer
);
2097 if (retval
!= ERROR_OK
)
2108 /* Single aligned words are guaranteed to use 16 or 32 bit access
2109 * mode respectively, otherwise data is handled as quickly as
2112 int target_read_buffer(struct target
*target
, target_addr_t address
, uint32_t size
, uint8_t *buffer
)
2114 LOG_DEBUG("reading buffer of %" PRIi32
" byte at " TARGET_ADDR_FMT
,
2117 if (!target_was_examined(target
)) {
2118 LOG_ERROR("Target not examined yet");
2125 if ((address
+ size
- 1) < address
) {
2126 /* GDB can request this when e.g. PC is 0xfffffffc */
2127 LOG_ERROR("address + size wrapped (" TARGET_ADDR_FMT
", 0x%08" PRIx32
")",
2133 return target
->type
->read_buffer(target
, address
, size
, buffer
);
2136 static int target_read_buffer_default(struct target
*target
, target_addr_t address
, uint32_t count
, uint8_t *buffer
)
2140 /* Align up to maximum 4 bytes. The loop condition makes sure the next pass
2141 * will have something to do with the size we leave to it. */
2142 for (size
= 1; size
< 4 && count
>= size
* 2 + (address
& size
); size
*= 2) {
2143 if (address
& size
) {
2144 int retval
= target_read_memory(target
, address
, size
, 1, buffer
);
2145 if (retval
!= ERROR_OK
)
2153 /* Read the data with as large access size as possible. */
2154 for (; size
> 0; size
/= 2) {
2155 uint32_t aligned
= count
- count
% size
;
2157 int retval
= target_read_memory(target
, address
, size
, aligned
/ size
, buffer
);
2158 if (retval
!= ERROR_OK
)
2169 int target_checksum_memory(struct target
*target
, target_addr_t address
, uint32_t size
, uint32_t* crc
)
2174 uint32_t checksum
= 0;
2175 if (!target_was_examined(target
)) {
2176 LOG_ERROR("Target not examined yet");
2180 retval
= target
->type
->checksum_memory(target
, address
, size
, &checksum
);
2181 if (retval
!= ERROR_OK
) {
2182 buffer
= malloc(size
);
2183 if (buffer
== NULL
) {
2184 LOG_ERROR("error allocating buffer for section (%" PRId32
" bytes)", size
);
2185 return ERROR_COMMAND_SYNTAX_ERROR
;
2187 retval
= target_read_buffer(target
, address
, size
, buffer
);
2188 if (retval
!= ERROR_OK
) {
2193 /* convert to target endianness */
2194 for (i
= 0; i
< (size
/sizeof(uint32_t)); i
++) {
2195 uint32_t target_data
;
2196 target_data
= target_buffer_get_u32(target
, &buffer
[i
*sizeof(uint32_t)]);
2197 target_buffer_set_u32(target
, &buffer
[i
*sizeof(uint32_t)], target_data
);
2200 retval
= image_calculate_checksum(buffer
, size
, &checksum
);
2209 int target_blank_check_memory(struct target
*target
, target_addr_t address
, uint32_t size
, uint32_t* blank
,
2210 uint8_t erased_value
)
2213 if (!target_was_examined(target
)) {
2214 LOG_ERROR("Target not examined yet");
2218 if (target
->type
->blank_check_memory
== 0)
2219 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
2221 retval
= target
->type
->blank_check_memory(target
, address
, size
, blank
, erased_value
);
2226 int target_read_u64(struct target
*target
, target_addr_t address
, uint64_t *value
)
2228 uint8_t value_buf
[8];
2229 if (!target_was_examined(target
)) {
2230 LOG_ERROR("Target not examined yet");
2234 int retval
= target_read_memory(target
, address
, 8, 1, value_buf
);
2236 if (retval
== ERROR_OK
) {
2237 *value
= target_buffer_get_u64(target
, value_buf
);
2238 LOG_DEBUG("address: " TARGET_ADDR_FMT
", value: 0x%16.16" PRIx64
"",
2243 LOG_DEBUG("address: " TARGET_ADDR_FMT
" failed",
2250 int target_read_u32(struct target
*target
, target_addr_t address
, uint32_t *value
)
2252 uint8_t value_buf
[4];
2253 if (!target_was_examined(target
)) {
2254 LOG_ERROR("Target not examined yet");
2258 int retval
= target_read_memory(target
, address
, 4, 1, value_buf
);
2260 if (retval
== ERROR_OK
) {
2261 *value
= target_buffer_get_u32(target
, value_buf
);
2262 LOG_DEBUG("address: " TARGET_ADDR_FMT
", value: 0x%8.8" PRIx32
"",
2267 LOG_DEBUG("address: " TARGET_ADDR_FMT
" failed",
2274 int target_read_u16(struct target
*target
, target_addr_t address
, uint16_t *value
)
2276 uint8_t value_buf
[2];
2277 if (!target_was_examined(target
)) {
2278 LOG_ERROR("Target not examined yet");
2282 int retval
= target_read_memory(target
, address
, 2, 1, value_buf
);
2284 if (retval
== ERROR_OK
) {
2285 *value
= target_buffer_get_u16(target
, value_buf
);
2286 LOG_DEBUG("address: " TARGET_ADDR_FMT
", value: 0x%4.4" PRIx16
,
2291 LOG_DEBUG("address: " TARGET_ADDR_FMT
" failed",
2298 int target_read_u8(struct target
*target
, target_addr_t address
, uint8_t *value
)
2300 if (!target_was_examined(target
)) {
2301 LOG_ERROR("Target not examined yet");
2305 int retval
= target_read_memory(target
, address
, 1, 1, value
);
2307 if (retval
== ERROR_OK
) {
2308 LOG_DEBUG("address: " TARGET_ADDR_FMT
", value: 0x%2.2" PRIx8
,
2313 LOG_DEBUG("address: " TARGET_ADDR_FMT
" failed",
2320 int target_write_u64(struct target
*target
, target_addr_t address
, uint64_t value
)
2323 uint8_t value_buf
[8];
2324 if (!target_was_examined(target
)) {
2325 LOG_ERROR("Target not examined yet");
2329 LOG_DEBUG("address: " TARGET_ADDR_FMT
", value: 0x%16.16" PRIx64
"",
2333 target_buffer_set_u64(target
, value_buf
, value
);
2334 retval
= target_write_memory(target
, address
, 8, 1, value_buf
);
2335 if (retval
!= ERROR_OK
)
2336 LOG_DEBUG("failed: %i", retval
);
2341 int target_write_u32(struct target
*target
, target_addr_t address
, uint32_t value
)
2344 uint8_t value_buf
[4];
2345 if (!target_was_examined(target
)) {
2346 LOG_ERROR("Target not examined yet");
2350 LOG_DEBUG("address: " TARGET_ADDR_FMT
", value: 0x%8.8" PRIx32
"",
2354 target_buffer_set_u32(target
, value_buf
, value
);
2355 retval
= target_write_memory(target
, address
, 4, 1, value_buf
);
2356 if (retval
!= ERROR_OK
)
2357 LOG_DEBUG("failed: %i", retval
);
2362 int target_write_u16(struct target
*target
, target_addr_t address
, uint16_t value
)
2365 uint8_t value_buf
[2];
2366 if (!target_was_examined(target
)) {
2367 LOG_ERROR("Target not examined yet");
2371 LOG_DEBUG("address: " TARGET_ADDR_FMT
", value: 0x%8.8" PRIx16
,
2375 target_buffer_set_u16(target
, value_buf
, value
);
2376 retval
= target_write_memory(target
, address
, 2, 1, value_buf
);
2377 if (retval
!= ERROR_OK
)
2378 LOG_DEBUG("failed: %i", retval
);
2383 int target_write_u8(struct target
*target
, target_addr_t address
, uint8_t value
)
2386 if (!target_was_examined(target
)) {
2387 LOG_ERROR("Target not examined yet");
2391 LOG_DEBUG("address: " TARGET_ADDR_FMT
", value: 0x%2.2" PRIx8
,
2394 retval
= target_write_memory(target
, address
, 1, 1, &value
);
2395 if (retval
!= ERROR_OK
)
2396 LOG_DEBUG("failed: %i", retval
);
2401 int target_write_phys_u64(struct target
*target
, target_addr_t address
, uint64_t value
)
2404 uint8_t value_buf
[8];
2405 if (!target_was_examined(target
)) {
2406 LOG_ERROR("Target not examined yet");
2410 LOG_DEBUG("address: " TARGET_ADDR_FMT
", value: 0x%16.16" PRIx64
"",
2414 target_buffer_set_u64(target
, value_buf
, value
);
2415 retval
= target_write_phys_memory(target
, address
, 8, 1, value_buf
);
2416 if (retval
!= ERROR_OK
)
2417 LOG_DEBUG("failed: %i", retval
);
2422 int target_write_phys_u32(struct target
*target
, target_addr_t address
, uint32_t value
)
2425 uint8_t value_buf
[4];
2426 if (!target_was_examined(target
)) {
2427 LOG_ERROR("Target not examined yet");
2431 LOG_DEBUG("address: " TARGET_ADDR_FMT
", value: 0x%8.8" PRIx32
"",
2435 target_buffer_set_u32(target
, value_buf
, value
);
2436 retval
= target_write_phys_memory(target
, address
, 4, 1, value_buf
);
2437 if (retval
!= ERROR_OK
)
2438 LOG_DEBUG("failed: %i", retval
);
2443 int target_write_phys_u16(struct target
*target
, target_addr_t address
, uint16_t value
)
2446 uint8_t value_buf
[2];
2447 if (!target_was_examined(target
)) {
2448 LOG_ERROR("Target not examined yet");
2452 LOG_DEBUG("address: " TARGET_ADDR_FMT
", value: 0x%8.8" PRIx16
,
2456 target_buffer_set_u16(target
, value_buf
, value
);
2457 retval
= target_write_phys_memory(target
, address
, 2, 1, value_buf
);
2458 if (retval
!= ERROR_OK
)
2459 LOG_DEBUG("failed: %i", retval
);
2464 int target_write_phys_u8(struct target
*target
, target_addr_t address
, uint8_t value
)
2467 if (!target_was_examined(target
)) {
2468 LOG_ERROR("Target not examined yet");
2472 LOG_DEBUG("address: " TARGET_ADDR_FMT
", value: 0x%2.2" PRIx8
,
2475 retval
= target_write_phys_memory(target
, address
, 1, 1, &value
);
2476 if (retval
!= ERROR_OK
)
2477 LOG_DEBUG("failed: %i", retval
);
2482 static int find_target(struct command_context
*cmd_ctx
, const char *name
)
2484 struct target
*target
= get_target(name
);
2485 if (target
== NULL
) {
2486 LOG_ERROR("Target: %s is unknown, try one of:\n", name
);
2489 if (!target
->tap
->enabled
) {
2490 LOG_USER("Target: TAP %s is disabled, "
2491 "can't be the current target\n",
2492 target
->tap
->dotted_name
);
2496 cmd_ctx
->current_target
= target
->target_number
;
2501 COMMAND_HANDLER(handle_targets_command
)
2503 int retval
= ERROR_OK
;
2504 if (CMD_ARGC
== 1) {
2505 retval
= find_target(CMD_CTX
, CMD_ARGV
[0]);
2506 if (retval
== ERROR_OK
) {
2512 struct target
*target
= all_targets
;
2513 command_print(CMD_CTX
, " TargetName Type Endian TapName State ");
2514 command_print(CMD_CTX
, "-- ------------------ ---------- ------ ------------------ ------------");
2519 if (target
->tap
->enabled
)
2520 state
= target_state_name(target
);
2522 state
= "tap-disabled";
2524 if (CMD_CTX
->current_target
== target
->target_number
)
2527 /* keep columns lined up to match the headers above */
2528 command_print(CMD_CTX
,
2529 "%2d%c %-18s %-10s %-6s %-18s %s",
2530 target
->target_number
,
2532 target_name(target
),
2533 target_type_name(target
),
2534 Jim_Nvp_value2name_simple(nvp_target_endian
,
2535 target
->endianness
)->name
,
2536 target
->tap
->dotted_name
,
2538 target
= target
->next
;
2544 /* every 300ms we check for reset & powerdropout and issue a "reset halt" if so. */
2546 static int powerDropout
;
2547 static int srstAsserted
;
2549 static int runPowerRestore
;
2550 static int runPowerDropout
;
2551 static int runSrstAsserted
;
2552 static int runSrstDeasserted
;
2554 static int sense_handler(void)
2556 static int prevSrstAsserted
;
2557 static int prevPowerdropout
;
2559 int retval
= jtag_power_dropout(&powerDropout
);
2560 if (retval
!= ERROR_OK
)
2564 powerRestored
= prevPowerdropout
&& !powerDropout
;
2566 runPowerRestore
= 1;
2568 int64_t current
= timeval_ms();
2569 static int64_t lastPower
;
2570 bool waitMore
= lastPower
+ 2000 > current
;
2571 if (powerDropout
&& !waitMore
) {
2572 runPowerDropout
= 1;
2573 lastPower
= current
;
2576 retval
= jtag_srst_asserted(&srstAsserted
);
2577 if (retval
!= ERROR_OK
)
2581 srstDeasserted
= prevSrstAsserted
&& !srstAsserted
;
2583 static int64_t lastSrst
;
2584 waitMore
= lastSrst
+ 2000 > current
;
2585 if (srstDeasserted
&& !waitMore
) {
2586 runSrstDeasserted
= 1;
2590 if (!prevSrstAsserted
&& srstAsserted
)
2591 runSrstAsserted
= 1;
2593 prevSrstAsserted
= srstAsserted
;
2594 prevPowerdropout
= powerDropout
;
2596 if (srstDeasserted
|| powerRestored
) {
2597 /* Other than logging the event we can't do anything here.
2598 * Issuing a reset is a particularly bad idea as we might
2599 * be inside a reset already.
2606 /* process target state changes */
2607 static int handle_target(void *priv
)
2609 Jim_Interp
*interp
= (Jim_Interp
*)priv
;
2610 int retval
= ERROR_OK
;
2612 if (!is_jtag_poll_safe()) {
2613 /* polling is disabled currently */
2617 /* we do not want to recurse here... */
2618 static int recursive
;
2622 /* danger! running these procedures can trigger srst assertions and power dropouts.
2623 * We need to avoid an infinite loop/recursion here and we do that by
2624 * clearing the flags after running these events.
2626 int did_something
= 0;
2627 if (runSrstAsserted
) {
2628 LOG_INFO("srst asserted detected, running srst_asserted proc.");
2629 Jim_Eval(interp
, "srst_asserted");
2632 if (runSrstDeasserted
) {
2633 Jim_Eval(interp
, "srst_deasserted");
2636 if (runPowerDropout
) {
2637 LOG_INFO("Power dropout detected, running power_dropout proc.");
2638 Jim_Eval(interp
, "power_dropout");
2641 if (runPowerRestore
) {
2642 Jim_Eval(interp
, "power_restore");
2646 if (did_something
) {
2647 /* clear detect flags */
2651 /* clear action flags */
2653 runSrstAsserted
= 0;
2654 runSrstDeasserted
= 0;
2655 runPowerRestore
= 0;
2656 runPowerDropout
= 0;
2661 /* Poll targets for state changes unless that's globally disabled.
2662 * Skip targets that are currently disabled.
2664 for (struct target
*target
= all_targets
;
2665 is_jtag_poll_safe() && target
;
2666 target
= target
->next
) {
2668 if (!target_was_examined(target
))
2671 if (!target
->tap
->enabled
)
2674 if (target
->backoff
.times
> target
->backoff
.count
) {
2675 /* do not poll this time as we failed previously */
2676 target
->backoff
.count
++;
2679 target
->backoff
.count
= 0;
2681 /* only poll target if we've got power and srst isn't asserted */
2682 if (!powerDropout
&& !srstAsserted
) {
2683 /* polling may fail silently until the target has been examined */
2684 retval
= target_poll(target
);
2685 if (retval
!= ERROR_OK
) {
2686 /* 100ms polling interval. Increase interval between polling up to 5000ms */
2687 if (target
->backoff
.times
* polling_interval
< 5000) {
2688 target
->backoff
.times
*= 2;
2689 target
->backoff
.times
++;
2692 /* Tell GDB to halt the debugger. This allows the user to
2693 * run monitor commands to handle the situation.
2695 target_call_event_callbacks(target
, TARGET_EVENT_GDB_HALT
);
2697 if (target
->backoff
.times
> 0) {
2698 LOG_USER("Polling target %s failed, trying to reexamine", target_name(target
));
2699 target_reset_examined(target
);
2700 retval
= target_examine_one(target
);
2701 /* Target examination could have failed due to unstable connection,
2702 * but we set the examined flag anyway to repoll it later */
2703 if (retval
!= ERROR_OK
) {
2704 target
->examined
= true;
2705 LOG_USER("Examination failed, GDB will be halted. Polling again in %dms",
2706 target
->backoff
.times
* polling_interval
);
2711 /* Since we succeeded, we reset backoff count */
2712 target
->backoff
.times
= 0;
2719 COMMAND_HANDLER(handle_reg_command
)
2721 struct target
*target
;
2722 struct reg
*reg
= NULL
;
2728 target
= get_current_target(CMD_CTX
);
2730 /* list all available registers for the current target */
2731 if (CMD_ARGC
== 0) {
2732 struct reg_cache
*cache
= target
->reg_cache
;
2738 command_print(CMD_CTX
, "===== %s", cache
->name
);
2740 for (i
= 0, reg
= cache
->reg_list
;
2741 i
< cache
->num_regs
;
2742 i
++, reg
++, count
++) {
2743 /* only print cached values if they are valid */
2745 value
= buf_to_str(reg
->value
,
2747 command_print(CMD_CTX
,
2748 "(%i) %s (/%" PRIu32
"): 0x%s%s",
2756 command_print(CMD_CTX
, "(%i) %s (/%" PRIu32
")",
2761 cache
= cache
->next
;
2767 /* access a single register by its ordinal number */
2768 if ((CMD_ARGV
[0][0] >= '0') && (CMD_ARGV
[0][0] <= '9')) {
2770 COMMAND_PARSE_NUMBER(uint
, CMD_ARGV
[0], num
);
2772 struct reg_cache
*cache
= target
->reg_cache
;
2776 for (i
= 0; i
< cache
->num_regs
; i
++) {
2777 if (count
++ == num
) {
2778 reg
= &cache
->reg_list
[i
];
2784 cache
= cache
->next
;
2788 command_print(CMD_CTX
, "%i is out of bounds, the current target "
2789 "has only %i registers (0 - %i)", num
, count
, count
- 1);
2793 /* access a single register by its name */
2794 reg
= register_get_by_name(target
->reg_cache
, CMD_ARGV
[0], 1);
2797 command_print(CMD_CTX
, "register %s not found in current target", CMD_ARGV
[0]);
2802 assert(reg
!= NULL
); /* give clang a hint that we *know* reg is != NULL here */
2804 /* display a register */
2805 if ((CMD_ARGC
== 1) || ((CMD_ARGC
== 2) && !((CMD_ARGV
[1][0] >= '0')
2806 && (CMD_ARGV
[1][0] <= '9')))) {
2807 if ((CMD_ARGC
== 2) && (strcmp(CMD_ARGV
[1], "force") == 0))
2810 if (reg
->valid
== 0)
2811 reg
->type
->get(reg
);
2812 value
= buf_to_str(reg
->value
, reg
->size
, 16);
2813 command_print(CMD_CTX
, "%s (/%i): 0x%s", reg
->name
, (int)(reg
->size
), value
);
2818 /* set register value */
2819 if (CMD_ARGC
== 2) {
2820 uint8_t *buf
= malloc(DIV_ROUND_UP(reg
->size
, 8));
2823 str_to_buf(CMD_ARGV
[1], strlen(CMD_ARGV
[1]), buf
, reg
->size
, 0);
2825 reg
->type
->set(reg
, buf
);
2827 value
= buf_to_str(reg
->value
, reg
->size
, 16);
2828 command_print(CMD_CTX
, "%s (/%i): 0x%s", reg
->name
, (int)(reg
->size
), value
);
2836 return ERROR_COMMAND_SYNTAX_ERROR
;
2839 COMMAND_HANDLER(handle_poll_command
)
2841 int retval
= ERROR_OK
;
2842 struct target
*target
= get_current_target(CMD_CTX
);
2844 if (CMD_ARGC
== 0) {
2845 command_print(CMD_CTX
, "background polling: %s",
2846 jtag_poll_get_enabled() ? "on" : "off");
2847 command_print(CMD_CTX
, "TAP: %s (%s)",
2848 target
->tap
->dotted_name
,
2849 target
->tap
->enabled
? "enabled" : "disabled");
2850 if (!target
->tap
->enabled
)
2852 retval
= target_poll(target
);
2853 if (retval
!= ERROR_OK
)
2855 retval
= target_arch_state(target
);
2856 if (retval
!= ERROR_OK
)
2858 } else if (CMD_ARGC
== 1) {
2860 COMMAND_PARSE_ON_OFF(CMD_ARGV
[0], enable
);
2861 jtag_poll_set_enabled(enable
);
2863 return ERROR_COMMAND_SYNTAX_ERROR
;
2868 COMMAND_HANDLER(handle_wait_halt_command
)
2871 return ERROR_COMMAND_SYNTAX_ERROR
;
2873 unsigned ms
= DEFAULT_HALT_TIMEOUT
;
2874 if (1 == CMD_ARGC
) {
2875 int retval
= parse_uint(CMD_ARGV
[0], &ms
);
2876 if (ERROR_OK
!= retval
)
2877 return ERROR_COMMAND_SYNTAX_ERROR
;
2880 struct target
*target
= get_current_target(CMD_CTX
);
2881 return target_wait_state(target
, TARGET_HALTED
, ms
);
2884 /* wait for target state to change. The trick here is to have a low
2885 * latency for short waits and not to suck up all the CPU time
2888 * After 500ms, keep_alive() is invoked
2890 int target_wait_state(struct target
*target
, enum target_state state
, int ms
)
2893 int64_t then
= 0, cur
;
2897 retval
= target_poll(target
);
2898 if (retval
!= ERROR_OK
)
2900 if (target
->state
== state
)
2905 then
= timeval_ms();
2906 LOG_DEBUG("waiting for target %s...",
2907 Jim_Nvp_value2name_simple(nvp_target_state
, state
)->name
);
2913 if ((cur
-then
) > ms
) {
2914 LOG_ERROR("timed out while waiting for target %s",
2915 Jim_Nvp_value2name_simple(nvp_target_state
, state
)->name
);
2923 COMMAND_HANDLER(handle_halt_command
)
2927 struct target
*target
= get_current_target(CMD_CTX
);
2928 int retval
= target_halt(target
);
2929 if (ERROR_OK
!= retval
)
2932 if (CMD_ARGC
== 1) {
2933 unsigned wait_local
;
2934 retval
= parse_uint(CMD_ARGV
[0], &wait_local
);
2935 if (ERROR_OK
!= retval
)
2936 return ERROR_COMMAND_SYNTAX_ERROR
;
2941 return CALL_COMMAND_HANDLER(handle_wait_halt_command
);
2944 COMMAND_HANDLER(handle_soft_reset_halt_command
)
2946 struct target
*target
= get_current_target(CMD_CTX
);
2948 LOG_USER("requesting target halt and executing a soft reset");
2950 target_soft_reset_halt(target
);
2955 COMMAND_HANDLER(handle_reset_command
)
2958 return ERROR_COMMAND_SYNTAX_ERROR
;
2960 enum target_reset_mode reset_mode
= RESET_RUN
;
2961 if (CMD_ARGC
== 1) {
2963 n
= Jim_Nvp_name2value_simple(nvp_reset_modes
, CMD_ARGV
[0]);
2964 if ((n
->name
== NULL
) || (n
->value
== RESET_UNKNOWN
))
2965 return ERROR_COMMAND_SYNTAX_ERROR
;
2966 reset_mode
= n
->value
;
2969 /* reset *all* targets */
2970 return target_process_reset(CMD_CTX
, reset_mode
);
2974 COMMAND_HANDLER(handle_resume_command
)
2978 return ERROR_COMMAND_SYNTAX_ERROR
;
2980 struct target
*target
= get_current_target(CMD_CTX
);
2982 /* with no CMD_ARGV, resume from current pc, addr = 0,
2983 * with one arguments, addr = CMD_ARGV[0],
2984 * handle breakpoints, not debugging */
2985 target_addr_t addr
= 0;
2986 if (CMD_ARGC
== 1) {
2987 COMMAND_PARSE_ADDRESS(CMD_ARGV
[0], addr
);
2991 return target_resume(target
, current
, addr
, 1, 0);
2994 COMMAND_HANDLER(handle_step_command
)
2997 return ERROR_COMMAND_SYNTAX_ERROR
;
3001 /* with no CMD_ARGV, step from current pc, addr = 0,
3002 * with one argument addr = CMD_ARGV[0],
3003 * handle breakpoints, debugging */
3004 target_addr_t addr
= 0;
3006 if (CMD_ARGC
== 1) {
3007 COMMAND_PARSE_ADDRESS(CMD_ARGV
[0], addr
);
3011 struct target
*target
= get_current_target(CMD_CTX
);
3013 return target
->type
->step(target
, current_pc
, addr
, 1);
3016 static void handle_md_output(struct command_context
*cmd_ctx
,
3017 struct target
*target
, target_addr_t address
, unsigned size
,
3018 unsigned count
, const uint8_t *buffer
)
3020 const unsigned line_bytecnt
= 32;
3021 unsigned line_modulo
= line_bytecnt
/ size
;
3023 char output
[line_bytecnt
* 4 + 1];
3024 unsigned output_len
= 0;
3026 const char *value_fmt
;
3029 value_fmt
= "%16.16"PRIx64
" ";
3032 value_fmt
= "%8.8"PRIx64
" ";
3035 value_fmt
= "%4.4"PRIx64
" ";
3038 value_fmt
= "%2.2"PRIx64
" ";
3041 /* "can't happen", caller checked */
3042 LOG_ERROR("invalid memory read size: %u", size
);
3046 for (unsigned i
= 0; i
< count
; i
++) {
3047 if (i
% line_modulo
== 0) {
3048 output_len
+= snprintf(output
+ output_len
,
3049 sizeof(output
) - output_len
,
3050 TARGET_ADDR_FMT
": ",
3051 (address
+ (i
* size
)));
3055 const uint8_t *value_ptr
= buffer
+ i
* size
;
3058 value
= target_buffer_get_u64(target
, value_ptr
);
3061 value
= target_buffer_get_u32(target
, value_ptr
);
3064 value
= target_buffer_get_u16(target
, value_ptr
);
3069 output_len
+= snprintf(output
+ output_len
,
3070 sizeof(output
) - output_len
,
3073 if ((i
% line_modulo
== line_modulo
- 1) || (i
== count
- 1)) {
3074 command_print(cmd_ctx
, "%s", output
);
3080 COMMAND_HANDLER(handle_md_command
)
3083 return ERROR_COMMAND_SYNTAX_ERROR
;
3086 switch (CMD_NAME
[2]) {
3100 return ERROR_COMMAND_SYNTAX_ERROR
;
3103 bool physical
= strcmp(CMD_ARGV
[0], "phys") == 0;
3104 int (*fn
)(struct target
*target
,
3105 target_addr_t address
, uint32_t size_value
, uint32_t count
, uint8_t *buffer
);
3109 fn
= target_read_phys_memory
;
3111 fn
= target_read_memory
;
3112 if ((CMD_ARGC
< 1) || (CMD_ARGC
> 2))
3113 return ERROR_COMMAND_SYNTAX_ERROR
;
3115 target_addr_t address
;
3116 COMMAND_PARSE_ADDRESS(CMD_ARGV
[0], address
);
3120 COMMAND_PARSE_NUMBER(uint
, CMD_ARGV
[1], count
);
3122 uint8_t *buffer
= calloc(count
, size
);
3124 struct target
*target
= get_current_target(CMD_CTX
);
3125 int retval
= fn(target
, address
, size
, count
, buffer
);
3126 if (ERROR_OK
== retval
)
3127 handle_md_output(CMD_CTX
, target
, address
, size
, count
, buffer
);
3134 typedef int (*target_write_fn
)(struct target
*target
,
3135 target_addr_t address
, uint32_t size
, uint32_t count
, const uint8_t *buffer
);
3137 static int target_fill_mem(struct target
*target
,
3138 target_addr_t address
,
3146 /* We have to write in reasonably large chunks to be able
3147 * to fill large memory areas with any sane speed */
3148 const unsigned chunk_size
= 16384;
3149 uint8_t *target_buf
= malloc(chunk_size
* data_size
);
3150 if (target_buf
== NULL
) {
3151 LOG_ERROR("Out of memory");
3155 for (unsigned i
= 0; i
< chunk_size
; i
++) {
3156 switch (data_size
) {
3158 target_buffer_set_u64(target
, target_buf
+ i
* data_size
, b
);
3161 target_buffer_set_u32(target
, target_buf
+ i
* data_size
, b
);
3164 target_buffer_set_u16(target
, target_buf
+ i
* data_size
, b
);
3167 target_buffer_set_u8(target
, target_buf
+ i
* data_size
, b
);
3174 int retval
= ERROR_OK
;
3176 for (unsigned x
= 0; x
< c
; x
+= chunk_size
) {
3179 if (current
> chunk_size
)
3180 current
= chunk_size
;
3181 retval
= fn(target
, address
+ x
* data_size
, data_size
, current
, target_buf
);
3182 if (retval
!= ERROR_OK
)
3184 /* avoid GDB timeouts */
3193 COMMAND_HANDLER(handle_mw_command
)
3196 return ERROR_COMMAND_SYNTAX_ERROR
;
3197 bool physical
= strcmp(CMD_ARGV
[0], "phys") == 0;
3202 fn
= target_write_phys_memory
;
3204 fn
= target_write_memory
;
3205 if ((CMD_ARGC
< 2) || (CMD_ARGC
> 3))
3206 return ERROR_COMMAND_SYNTAX_ERROR
;
3208 target_addr_t address
;
3209 COMMAND_PARSE_ADDRESS(CMD_ARGV
[0], address
);
3211 target_addr_t value
;
3212 COMMAND_PARSE_ADDRESS(CMD_ARGV
[1], value
);
3216 COMMAND_PARSE_NUMBER(uint
, CMD_ARGV
[2], count
);
3218 struct target
*target
= get_current_target(CMD_CTX
);
3220 switch (CMD_NAME
[2]) {
3234 return ERROR_COMMAND_SYNTAX_ERROR
;
3237 return target_fill_mem(target
, address
, fn
, wordsize
, value
, count
);
3240 static COMMAND_HELPER(parse_load_image_command_CMD_ARGV
, struct image
*image
,
3241 target_addr_t
*min_address
, target_addr_t
*max_address
)
3243 if (CMD_ARGC
< 1 || CMD_ARGC
> 5)
3244 return ERROR_COMMAND_SYNTAX_ERROR
;
3246 /* a base address isn't always necessary,
3247 * default to 0x0 (i.e. don't relocate) */
3248 if (CMD_ARGC
>= 2) {
3250 COMMAND_PARSE_ADDRESS(CMD_ARGV
[1], addr
);
3251 image
->base_address
= addr
;
3252 image
->base_address_set
= 1;
3254 image
->base_address_set
= 0;
3256 image
->start_address_set
= 0;
3259 COMMAND_PARSE_ADDRESS(CMD_ARGV
[3], *min_address
);
3260 if (CMD_ARGC
== 5) {
3261 COMMAND_PARSE_ADDRESS(CMD_ARGV
[4], *max_address
);
3262 /* use size (given) to find max (required) */
3263 *max_address
+= *min_address
;
3266 if (*min_address
> *max_address
)
3267 return ERROR_COMMAND_SYNTAX_ERROR
;
3272 COMMAND_HANDLER(handle_load_image_command
)
3276 uint32_t image_size
;
3277 target_addr_t min_address
= 0;
3278 target_addr_t max_address
= -1;
3282 int retval
= CALL_COMMAND_HANDLER(parse_load_image_command_CMD_ARGV
,
3283 &image
, &min_address
, &max_address
);
3284 if (ERROR_OK
!= retval
)
3287 struct target
*target
= get_current_target(CMD_CTX
);
3289 struct duration bench
;
3290 duration_start(&bench
);
3292 if (image_open(&image
, CMD_ARGV
[0], (CMD_ARGC
>= 3) ? CMD_ARGV
[2] : NULL
) != ERROR_OK
)
3297 for (i
= 0; i
< image
.num_sections
; i
++) {
3298 buffer
= malloc(image
.sections
[i
].size
);
3299 if (buffer
== NULL
) {
3300 command_print(CMD_CTX
,
3301 "error allocating buffer for section (%d bytes)",
3302 (int)(image
.sections
[i
].size
));
3303 retval
= ERROR_FAIL
;
3307 retval
= image_read_section(&image
, i
, 0x0, image
.sections
[i
].size
, buffer
, &buf_cnt
);
3308 if (retval
!= ERROR_OK
) {
3313 uint32_t offset
= 0;
3314 uint32_t length
= buf_cnt
;
3316 /* DANGER!!! beware of unsigned comparision here!!! */
3318 if ((image
.sections
[i
].base_address
+ buf_cnt
>= min_address
) &&
3319 (image
.sections
[i
].base_address
< max_address
)) {
3321 if (image
.sections
[i
].base_address
< min_address
) {
3322 /* clip addresses below */
3323 offset
+= min_address
-image
.sections
[i
].base_address
;
3327 if (image
.sections
[i
].base_address
+ buf_cnt
> max_address
)
3328 length
-= (image
.sections
[i
].base_address
+ buf_cnt
)-max_address
;
3330 retval
= target_write_buffer(target
,
3331 image
.sections
[i
].base_address
+ offset
, length
, buffer
+ offset
);
3332 if (retval
!= ERROR_OK
) {
3336 image_size
+= length
;
3337 command_print(CMD_CTX
, "%u bytes written at address " TARGET_ADDR_FMT
"",
3338 (unsigned int)length
,
3339 image
.sections
[i
].base_address
+ offset
);
3345 if ((ERROR_OK
== retval
) && (duration_measure(&bench
) == ERROR_OK
)) {
3346 command_print(CMD_CTX
, "downloaded %" PRIu32
" bytes "
3347 "in %fs (%0.3f KiB/s)", image_size
,
3348 duration_elapsed(&bench
), duration_kbps(&bench
, image_size
));
3351 image_close(&image
);
3357 COMMAND_HANDLER(handle_dump_image_command
)
3359 struct fileio
*fileio
;
3361 int retval
, retvaltemp
;
3362 target_addr_t address
, size
;
3363 struct duration bench
;
3364 struct target
*target
= get_current_target(CMD_CTX
);
3367 return ERROR_COMMAND_SYNTAX_ERROR
;
3369 COMMAND_PARSE_ADDRESS(CMD_ARGV
[1], address
);
3370 COMMAND_PARSE_ADDRESS(CMD_ARGV
[2], size
);
3372 uint32_t buf_size
= (size
> 4096) ? 4096 : size
;
3373 buffer
= malloc(buf_size
);
3377 retval
= fileio_open(&fileio
, CMD_ARGV
[0], FILEIO_WRITE
, FILEIO_BINARY
);
3378 if (retval
!= ERROR_OK
) {
3383 duration_start(&bench
);
3386 size_t size_written
;
3387 uint32_t this_run_size
= (size
> buf_size
) ? buf_size
: size
;
3388 retval
= target_read_buffer(target
, address
, this_run_size
, buffer
);
3389 if (retval
!= ERROR_OK
)
3392 retval
= fileio_write(fileio
, this_run_size
, buffer
, &size_written
);
3393 if (retval
!= ERROR_OK
)
3396 size
-= this_run_size
;
3397 address
+= this_run_size
;
3402 if ((ERROR_OK
== retval
) && (duration_measure(&bench
) == ERROR_OK
)) {
3404 retval
= fileio_size(fileio
, &filesize
);
3405 if (retval
!= ERROR_OK
)
3407 command_print(CMD_CTX
,
3408 "dumped %zu bytes in %fs (%0.3f KiB/s)", filesize
,
3409 duration_elapsed(&bench
), duration_kbps(&bench
, filesize
));
3412 retvaltemp
= fileio_close(fileio
);
3413 if (retvaltemp
!= ERROR_OK
)
3422 IMAGE_CHECKSUM_ONLY
= 2
3425 static COMMAND_HELPER(handle_verify_image_command_internal
, enum verify_mode verify
)
3429 uint32_t image_size
;
3432 uint32_t checksum
= 0;
3433 uint32_t mem_checksum
= 0;
3437 struct target
*target
= get_current_target(CMD_CTX
);
3440 return ERROR_COMMAND_SYNTAX_ERROR
;
3443 LOG_ERROR("no target selected");
3447 struct duration bench
;
3448 duration_start(&bench
);
3450 if (CMD_ARGC
>= 2) {
3452 COMMAND_PARSE_ADDRESS(CMD_ARGV
[1], addr
);
3453 image
.base_address
= addr
;
3454 image
.base_address_set
= 1;
3456 image
.base_address_set
= 0;
3457 image
.base_address
= 0x0;
3460 image
.start_address_set
= 0;
3462 retval
= image_open(&image
, CMD_ARGV
[0], (CMD_ARGC
== 3) ? CMD_ARGV
[2] : NULL
);
3463 if (retval
!= ERROR_OK
)
3469 for (i
= 0; i
< image
.num_sections
; i
++) {
3470 buffer
= malloc(image
.sections
[i
].size
);
3471 if (buffer
== NULL
) {
3472 command_print(CMD_CTX
,
3473 "error allocating buffer for section (%d bytes)",
3474 (int)(image
.sections
[i
].size
));
3477 retval
= image_read_section(&image
, i
, 0x0, image
.sections
[i
].size
, buffer
, &buf_cnt
);
3478 if (retval
!= ERROR_OK
) {
3483 if (verify
>= IMAGE_VERIFY
) {
3484 /* calculate checksum of image */
3485 retval
= image_calculate_checksum(buffer
, buf_cnt
, &checksum
);
3486 if (retval
!= ERROR_OK
) {
3491 retval
= target_checksum_memory(target
, image
.sections
[i
].base_address
, buf_cnt
, &mem_checksum
);
3492 if (retval
!= ERROR_OK
) {
3496 if ((checksum
!= mem_checksum
) && (verify
== IMAGE_CHECKSUM_ONLY
)) {
3497 LOG_ERROR("checksum mismatch");
3499 retval
= ERROR_FAIL
;
3502 if (checksum
!= mem_checksum
) {
3503 /* failed crc checksum, fall back to a binary compare */
3507 LOG_ERROR("checksum mismatch - attempting binary compare");
3509 data
= malloc(buf_cnt
);
3511 /* Can we use 32bit word accesses? */
3513 int count
= buf_cnt
;
3514 if ((count
% 4) == 0) {
3518 retval
= target_read_memory(target
, image
.sections
[i
].base_address
, size
, count
, data
);
3519 if (retval
== ERROR_OK
) {
3521 for (t
= 0; t
< buf_cnt
; t
++) {
3522 if (data
[t
] != buffer
[t
]) {
3523 command_print(CMD_CTX
,
3524 "diff %d address 0x%08x. Was 0x%02x instead of 0x%02x",
3526 (unsigned)(t
+ image
.sections
[i
].base_address
),
3529 if (diffs
++ >= 127) {
3530 command_print(CMD_CTX
, "More than 128 errors, the rest are not printed.");
3542 command_print(CMD_CTX
, "address " TARGET_ADDR_FMT
" length 0x%08zx",
3543 image
.sections
[i
].base_address
,
3548 image_size
+= buf_cnt
;
3551 command_print(CMD_CTX
, "No more differences found.");
3554 retval
= ERROR_FAIL
;
3555 if ((ERROR_OK
== retval
) && (duration_measure(&bench
) == ERROR_OK
)) {
3556 command_print(CMD_CTX
, "verified %" PRIu32
" bytes "
3557 "in %fs (%0.3f KiB/s)", image_size
,
3558 duration_elapsed(&bench
), duration_kbps(&bench
, image_size
));
3561 image_close(&image
);
3566 COMMAND_HANDLER(handle_verify_image_checksum_command
)
3568 return CALL_COMMAND_HANDLER(handle_verify_image_command_internal
, IMAGE_CHECKSUM_ONLY
);
3571 COMMAND_HANDLER(handle_verify_image_command
)
3573 return CALL_COMMAND_HANDLER(handle_verify_image_command_internal
, IMAGE_VERIFY
);
3576 COMMAND_HANDLER(handle_test_image_command
)
3578 return CALL_COMMAND_HANDLER(handle_verify_image_command_internal
, IMAGE_TEST
);
3581 static int handle_bp_command_list(struct command_context
*cmd_ctx
)
3583 struct target
*target
= get_current_target(cmd_ctx
);
3584 struct breakpoint
*breakpoint
= target
->breakpoints
;
3585 while (breakpoint
) {
3586 if (breakpoint
->type
== BKPT_SOFT
) {
3587 char *buf
= buf_to_str(breakpoint
->orig_instr
,
3588 breakpoint
->length
, 16);
3589 command_print(cmd_ctx
, "IVA breakpoint: " TARGET_ADDR_FMT
", 0x%x, %i, 0x%s",
3590 breakpoint
->address
,
3592 breakpoint
->set
, buf
);
3595 if ((breakpoint
->address
== 0) && (breakpoint
->asid
!= 0))
3596 command_print(cmd_ctx
, "Context breakpoint: 0x%8.8" PRIx32
", 0x%x, %i",
3598 breakpoint
->length
, breakpoint
->set
);
3599 else if ((breakpoint
->address
!= 0) && (breakpoint
->asid
!= 0)) {
3600 command_print(cmd_ctx
, "Hybrid breakpoint(IVA): " TARGET_ADDR_FMT
", 0x%x, %i",
3601 breakpoint
->address
,
3602 breakpoint
->length
, breakpoint
->set
);
3603 command_print(cmd_ctx
, "\t|--->linked with ContextID: 0x%8.8" PRIx32
,
3606 command_print(cmd_ctx
, "Breakpoint(IVA): " TARGET_ADDR_FMT
", 0x%x, %i",
3607 breakpoint
->address
,
3608 breakpoint
->length
, breakpoint
->set
);
3611 breakpoint
= breakpoint
->next
;
3616 static int handle_bp_command_set(struct command_context
*cmd_ctx
,
3617 target_addr_t addr
, uint32_t asid
, uint32_t length
, int hw
)
3619 struct target
*target
= get_current_target(cmd_ctx
);
3623 retval
= breakpoint_add(target
, addr
, length
, hw
);
3624 if (ERROR_OK
== retval
)
3625 command_print(cmd_ctx
, "breakpoint set at " TARGET_ADDR_FMT
"", addr
);
3627 LOG_ERROR("Failure setting breakpoint, the same address(IVA) is already used");
3630 } else if (addr
== 0) {
3631 if (target
->type
->add_context_breakpoint
== NULL
) {
3632 LOG_WARNING("Context breakpoint not available");
3635 retval
= context_breakpoint_add(target
, asid
, length
, hw
);
3636 if (ERROR_OK
== retval
)
3637 command_print(cmd_ctx
, "Context breakpoint set at 0x%8.8" PRIx32
"", asid
);
3639 LOG_ERROR("Failure setting breakpoint, the same address(CONTEXTID) is already used");
3643 if (target
->type
->add_hybrid_breakpoint
== NULL
) {
3644 LOG_WARNING("Hybrid breakpoint not available");
3647 retval
= hybrid_breakpoint_add(target
, addr
, asid
, length
, hw
);
3648 if (ERROR_OK
== retval
)
3649 command_print(cmd_ctx
, "Hybrid breakpoint set at 0x%8.8" PRIx32
"", asid
);
3651 LOG_ERROR("Failure setting breakpoint, the same address is already used");
3658 COMMAND_HANDLER(handle_bp_command
)
3667 return handle_bp_command_list(CMD_CTX
);
3671 COMMAND_PARSE_ADDRESS(CMD_ARGV
[0], addr
);
3672 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], length
);
3673 return handle_bp_command_set(CMD_CTX
, addr
, asid
, length
, hw
);
3676 if (strcmp(CMD_ARGV
[2], "hw") == 0) {
3678 COMMAND_PARSE_ADDRESS(CMD_ARGV
[0], addr
);
3679 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], length
);
3681 return handle_bp_command_set(CMD_CTX
, addr
, asid
, length
, hw
);
3682 } else if (strcmp(CMD_ARGV
[2], "hw_ctx") == 0) {
3684 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], asid
);
3685 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], length
);
3687 return handle_bp_command_set(CMD_CTX
, addr
, asid
, length
, hw
);
3692 COMMAND_PARSE_ADDRESS(CMD_ARGV
[0], addr
);
3693 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], asid
);
3694 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[2], length
);
3695 return handle_bp_command_set(CMD_CTX
, addr
, asid
, length
, hw
);
3698 return ERROR_COMMAND_SYNTAX_ERROR
;
3702 COMMAND_HANDLER(handle_rbp_command
)
3705 return ERROR_COMMAND_SYNTAX_ERROR
;
3708 COMMAND_PARSE_ADDRESS(CMD_ARGV
[0], addr
);
3710 struct target
*target
= get_current_target(CMD_CTX
);
3711 breakpoint_remove(target
, addr
);
3716 COMMAND_HANDLER(handle_wp_command
)
3718 struct target
*target
= get_current_target(CMD_CTX
);
3720 if (CMD_ARGC
== 0) {
3721 struct watchpoint
*watchpoint
= target
->watchpoints
;
3723 while (watchpoint
) {
3724 command_print(CMD_CTX
, "address: " TARGET_ADDR_FMT
3725 ", len: 0x%8.8" PRIx32
3726 ", r/w/a: %i, value: 0x%8.8" PRIx32
3727 ", mask: 0x%8.8" PRIx32
,
3728 watchpoint
->address
,
3730 (int)watchpoint
->rw
,
3733 watchpoint
= watchpoint
->next
;
3738 enum watchpoint_rw type
= WPT_ACCESS
;
3740 uint32_t length
= 0;
3741 uint32_t data_value
= 0x0;
3742 uint32_t data_mask
= 0xffffffff;
3746 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[4], data_mask
);
3749 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[3], data_value
);
3752 switch (CMD_ARGV
[2][0]) {
3763 LOG_ERROR("invalid watchpoint mode ('%c')", CMD_ARGV
[2][0]);
3764 return ERROR_COMMAND_SYNTAX_ERROR
;
3768 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], length
);
3769 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], addr
);
3773 return ERROR_COMMAND_SYNTAX_ERROR
;
3776 int retval
= watchpoint_add(target
, addr
, length
, type
,
3777 data_value
, data_mask
);
3778 if (ERROR_OK
!= retval
)
3779 LOG_ERROR("Failure setting watchpoints");
3784 COMMAND_HANDLER(handle_rwp_command
)
3787 return ERROR_COMMAND_SYNTAX_ERROR
;
3790 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], addr
);
3792 struct target
*target
= get_current_target(CMD_CTX
);
3793 watchpoint_remove(target
, addr
);
3799 * Translate a virtual address to a physical address.
3801 * The low-level target implementation must have logged a detailed error
3802 * which is forwarded to telnet/GDB session.
3804 COMMAND_HANDLER(handle_virt2phys_command
)
3807 return ERROR_COMMAND_SYNTAX_ERROR
;
3810 COMMAND_PARSE_ADDRESS(CMD_ARGV
[0], va
);
3813 struct target
*target
= get_current_target(CMD_CTX
);
3814 int retval
= target
->type
->virt2phys(target
, va
, &pa
);
3815 if (retval
== ERROR_OK
)
3816 command_print(CMD_CTX
, "Physical address " TARGET_ADDR_FMT
"", pa
);
3821 static void writeData(FILE *f
, const void *data
, size_t len
)
3823 size_t written
= fwrite(data
, 1, len
, f
);
3825 LOG_ERROR("failed to write %zu bytes: %s", len
, strerror(errno
));
3828 static void writeLong(FILE *f
, int l
, struct target
*target
)
3832 target_buffer_set_u32(target
, val
, l
);
3833 writeData(f
, val
, 4);
3836 static void writeString(FILE *f
, char *s
)
3838 writeData(f
, s
, strlen(s
));
3841 typedef unsigned char UNIT
[2]; /* unit of profiling */
3843 /* Dump a gmon.out histogram file. */
3844 static void write_gmon(uint32_t *samples
, uint32_t sampleNum
, const char *filename
, bool with_range
,
3845 uint32_t start_address
, uint32_t end_address
, struct target
*target
)
3848 FILE *f
= fopen(filename
, "w");
3851 writeString(f
, "gmon");
3852 writeLong(f
, 0x00000001, target
); /* Version */
3853 writeLong(f
, 0, target
); /* padding */
3854 writeLong(f
, 0, target
); /* padding */
3855 writeLong(f
, 0, target
); /* padding */
3857 uint8_t zero
= 0; /* GMON_TAG_TIME_HIST */
3858 writeData(f
, &zero
, 1);
3860 /* figure out bucket size */
3864 min
= start_address
;
3869 for (i
= 0; i
< sampleNum
; i
++) {
3870 if (min
> samples
[i
])
3872 if (max
< samples
[i
])
3876 /* max should be (largest sample + 1)
3877 * Refer to binutils/gprof/hist.c (find_histogram_for_pc) */
3881 int addressSpace
= max
- min
;
3882 assert(addressSpace
>= 2);
3884 /* FIXME: What is the reasonable number of buckets?
3885 * The profiling result will be more accurate if there are enough buckets. */
3886 static const uint32_t maxBuckets
= 128 * 1024; /* maximum buckets. */
3887 uint32_t numBuckets
= addressSpace
/ sizeof(UNIT
);
3888 if (numBuckets
> maxBuckets
)
3889 numBuckets
= maxBuckets
;
3890 int *buckets
= malloc(sizeof(int) * numBuckets
);
3891 if (buckets
== NULL
) {
3895 memset(buckets
, 0, sizeof(int) * numBuckets
);
3896 for (i
= 0; i
< sampleNum
; i
++) {
3897 uint32_t address
= samples
[i
];
3899 if ((address
< min
) || (max
<= address
))
3902 long long a
= address
- min
;
3903 long long b
= numBuckets
;
3904 long long c
= addressSpace
;
3905 int index_t
= (a
* b
) / c
; /* danger!!!! int32 overflows */
3909 /* append binary memory gmon.out &profile_hist_hdr ((char*)&profile_hist_hdr + sizeof(struct gmon_hist_hdr)) */
3910 writeLong(f
, min
, target
); /* low_pc */
3911 writeLong(f
, max
, target
); /* high_pc */
3912 writeLong(f
, numBuckets
, target
); /* # of buckets */
3913 writeLong(f
, 100, target
); /* KLUDGE! We lie, ca. 100Hz best case. */
3914 writeString(f
, "seconds");
3915 for (i
= 0; i
< (15-strlen("seconds")); i
++)
3916 writeData(f
, &zero
, 1);
3917 writeString(f
, "s");
3919 /*append binary memory gmon.out profile_hist_data (profile_hist_data + profile_hist_hdr.hist_size) */
3921 char *data
= malloc(2 * numBuckets
);
3923 for (i
= 0; i
< numBuckets
; i
++) {
3928 data
[i
* 2] = val
&0xff;
3929 data
[i
* 2 + 1] = (val
>> 8) & 0xff;
3932 writeData(f
, data
, numBuckets
* 2);
3940 /* profiling samples the CPU PC as quickly as OpenOCD is able,
3941 * which will be used as a random sampling of PC */
3942 COMMAND_HANDLER(handle_profile_command
)
3944 struct target
*target
= get_current_target(CMD_CTX
);
3946 if ((CMD_ARGC
!= 2) && (CMD_ARGC
!= 4))
3947 return ERROR_COMMAND_SYNTAX_ERROR
;
3949 const uint32_t MAX_PROFILE_SAMPLE_NUM
= 10000;
3951 uint32_t num_of_samples
;
3952 int retval
= ERROR_OK
;
3954 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], offset
);
3956 uint32_t *samples
= malloc(sizeof(uint32_t) * MAX_PROFILE_SAMPLE_NUM
);
3957 if (samples
== NULL
) {
3958 LOG_ERROR("No memory to store samples.");
3963 * Some cores let us sample the PC without the
3964 * annoying halt/resume step; for example, ARMv7 PCSR.
3965 * Provide a way to use that more efficient mechanism.
3967 retval
= target_profiling(target
, samples
, MAX_PROFILE_SAMPLE_NUM
,
3968 &num_of_samples
, offset
);
3969 if (retval
!= ERROR_OK
) {
3974 assert(num_of_samples
<= MAX_PROFILE_SAMPLE_NUM
);
3976 retval
= target_poll(target
);
3977 if (retval
!= ERROR_OK
) {
3981 if (target
->state
== TARGET_RUNNING
) {
3982 retval
= target_halt(target
);
3983 if (retval
!= ERROR_OK
) {
3989 retval
= target_poll(target
);
3990 if (retval
!= ERROR_OK
) {
3995 uint32_t start_address
= 0;
3996 uint32_t end_address
= 0;
3997 bool with_range
= false;
3998 if (CMD_ARGC
== 4) {
4000 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[2], start_address
);
4001 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[3], end_address
);
4004 write_gmon(samples
, num_of_samples
, CMD_ARGV
[1],
4005 with_range
, start_address
, end_address
, target
);
4006 command_print(CMD_CTX
, "Wrote %s", CMD_ARGV
[1]);
4012 static int new_int_array_element(Jim_Interp
*interp
, const char *varname
, int idx
, uint32_t val
)
4015 Jim_Obj
*nameObjPtr
, *valObjPtr
;
4018 namebuf
= alloc_printf("%s(%d)", varname
, idx
);
4022 nameObjPtr
= Jim_NewStringObj(interp
, namebuf
, -1);
4023 valObjPtr
= Jim_NewIntObj(interp
, val
);
4024 if (!nameObjPtr
|| !valObjPtr
) {
4029 Jim_IncrRefCount(nameObjPtr
);
4030 Jim_IncrRefCount(valObjPtr
);
4031 result
= Jim_SetVariable(interp
, nameObjPtr
, valObjPtr
);
4032 Jim_DecrRefCount(interp
, nameObjPtr
);
4033 Jim_DecrRefCount(interp
, valObjPtr
);
4035 /* printf("%s(%d) <= 0%08x\n", varname, idx, val); */
4039 static int jim_mem2array(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4041 struct command_context
*context
;
4042 struct target
*target
;
4044 context
= current_command_context(interp
);
4045 assert(context
!= NULL
);
4047 target
= get_current_target(context
);
4048 if (target
== NULL
) {
4049 LOG_ERROR("mem2array: no current target");
4053 return target_mem2array(interp
, target
, argc
- 1, argv
+ 1);
4056 static int target_mem2array(Jim_Interp
*interp
, struct target
*target
, int argc
, Jim_Obj
*const *argv
)
4064 const char *varname
;
4070 /* argv[1] = name of array to receive the data
4071 * argv[2] = desired width
4072 * argv[3] = memory address
4073 * argv[4] = count of times to read
4075 if (argc
< 4 || argc
> 5) {
4076 Jim_WrongNumArgs(interp
, 1, argv
, "varname width addr nelems [phys]");
4079 varname
= Jim_GetString(argv
[0], &len
);
4080 /* given "foo" get space for worse case "foo(%d)" .. add 20 */
4082 e
= Jim_GetLong(interp
, argv
[1], &l
);
4087 e
= Jim_GetLong(interp
, argv
[2], &l
);
4091 e
= Jim_GetLong(interp
, argv
[3], &l
);
4097 phys
= Jim_GetString(argv
[4], &n
);
4098 if (!strncmp(phys
, "phys", n
))
4114 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
4115 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "Invalid width param, must be 8/16/32", NULL
);
4119 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
4120 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "mem2array: zero width read?", NULL
);
4123 if ((addr
+ (len
* width
)) < addr
) {
4124 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
4125 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "mem2array: addr + len - wraps to zero?", NULL
);
4128 /* absurd transfer size? */
4130 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
4131 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "mem2array: absurd > 64K item request", NULL
);
4136 ((width
== 2) && ((addr
& 1) == 0)) ||
4137 ((width
== 4) && ((addr
& 3) == 0))) {
4141 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
4142 sprintf(buf
, "mem2array address: 0x%08" PRIx32
" is not aligned for %" PRId32
" byte reads",
4145 Jim_AppendStrings(interp
, Jim_GetResult(interp
), buf
, NULL
);
4154 size_t buffersize
= 4096;
4155 uint8_t *buffer
= malloc(buffersize
);
4162 /* Slurp... in buffer size chunks */
4164 count
= len
; /* in objects.. */
4165 if (count
> (buffersize
/ width
))
4166 count
= (buffersize
/ width
);
4169 retval
= target_read_phys_memory(target
, addr
, width
, count
, buffer
);
4171 retval
= target_read_memory(target
, addr
, width
, count
, buffer
);
4172 if (retval
!= ERROR_OK
) {
4174 LOG_ERROR("mem2array: Read @ 0x%08" PRIx32
", w=%" PRId32
", cnt=%" PRId32
", failed",
4178 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
4179 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "mem2array: cannot read memory", NULL
);
4183 v
= 0; /* shut up gcc */
4184 for (i
= 0; i
< count
; i
++, n
++) {
4187 v
= target_buffer_get_u32(target
, &buffer
[i
*width
]);
4190 v
= target_buffer_get_u16(target
, &buffer
[i
*width
]);
4193 v
= buffer
[i
] & 0x0ff;
4196 new_int_array_element(interp
, varname
, n
, v
);
4199 addr
+= count
* width
;
4205 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
4210 static int get_int_array_element(Jim_Interp
*interp
, const char *varname
, int idx
, uint32_t *val
)
4213 Jim_Obj
*nameObjPtr
, *valObjPtr
;
4217 namebuf
= alloc_printf("%s(%d)", varname
, idx
);
4221 nameObjPtr
= Jim_NewStringObj(interp
, namebuf
, -1);
4227 Jim_IncrRefCount(nameObjPtr
);
4228 valObjPtr
= Jim_GetVariable(interp
, nameObjPtr
, JIM_ERRMSG
);
4229 Jim_DecrRefCount(interp
, nameObjPtr
);
4231 if (valObjPtr
== NULL
)
4234 result
= Jim_GetLong(interp
, valObjPtr
, &l
);
4235 /* printf("%s(%d) => 0%08x\n", varname, idx, val); */
4240 static int jim_array2mem(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4242 struct command_context
*context
;
4243 struct target
*target
;
4245 context
= current_command_context(interp
);
4246 assert(context
!= NULL
);
4248 target
= get_current_target(context
);
4249 if (target
== NULL
) {
4250 LOG_ERROR("array2mem: no current target");
4254 return target_array2mem(interp
, target
, argc
-1, argv
+ 1);
4257 static int target_array2mem(Jim_Interp
*interp
, struct target
*target
,
4258 int argc
, Jim_Obj
*const *argv
)
4266 const char *varname
;
4272 /* argv[1] = name of array to get the data
4273 * argv[2] = desired width
4274 * argv[3] = memory address
4275 * argv[4] = count to write
4277 if (argc
< 4 || argc
> 5) {
4278 Jim_WrongNumArgs(interp
, 0, argv
, "varname width addr nelems [phys]");
4281 varname
= Jim_GetString(argv
[0], &len
);
4282 /* given "foo" get space for worse case "foo(%d)" .. add 20 */
4284 e
= Jim_GetLong(interp
, argv
[1], &l
);
4289 e
= Jim_GetLong(interp
, argv
[2], &l
);
4293 e
= Jim_GetLong(interp
, argv
[3], &l
);
4299 phys
= Jim_GetString(argv
[4], &n
);
4300 if (!strncmp(phys
, "phys", n
))
4316 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
4317 Jim_AppendStrings(interp
, Jim_GetResult(interp
),
4318 "Invalid width param, must be 8/16/32", NULL
);
4322 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
4323 Jim_AppendStrings(interp
, Jim_GetResult(interp
),
4324 "array2mem: zero width read?", NULL
);
4327 if ((addr
+ (len
* width
)) < addr
) {
4328 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
4329 Jim_AppendStrings(interp
, Jim_GetResult(interp
),
4330 "array2mem: addr + len - wraps to zero?", NULL
);
4333 /* absurd transfer size? */
4335 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
4336 Jim_AppendStrings(interp
, Jim_GetResult(interp
),
4337 "array2mem: absurd > 64K item request", NULL
);
4342 ((width
== 2) && ((addr
& 1) == 0)) ||
4343 ((width
== 4) && ((addr
& 3) == 0))) {
4347 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
4348 sprintf(buf
, "array2mem address: 0x%08" PRIx32
" is not aligned for %" PRId32
" byte reads",
4351 Jim_AppendStrings(interp
, Jim_GetResult(interp
), buf
, NULL
);
4362 size_t buffersize
= 4096;
4363 uint8_t *buffer
= malloc(buffersize
);
4368 /* Slurp... in buffer size chunks */
4370 count
= len
; /* in objects.. */
4371 if (count
> (buffersize
/ width
))
4372 count
= (buffersize
/ width
);
4374 v
= 0; /* shut up gcc */
4375 for (i
= 0; i
< count
; i
++, n
++) {
4376 get_int_array_element(interp
, varname
, n
, &v
);
4379 target_buffer_set_u32(target
, &buffer
[i
* width
], v
);
4382 target_buffer_set_u16(target
, &buffer
[i
* width
], v
);
4385 buffer
[i
] = v
& 0x0ff;
4392 retval
= target_write_phys_memory(target
, addr
, width
, count
, buffer
);
4394 retval
= target_write_memory(target
, addr
, width
, count
, buffer
);
4395 if (retval
!= ERROR_OK
) {
4397 LOG_ERROR("array2mem: Write @ 0x%08" PRIx32
", w=%" PRId32
", cnt=%" PRId32
", failed",
4401 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
4402 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "array2mem: cannot read memory", NULL
);
4406 addr
+= count
* width
;
4411 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
4416 /* FIX? should we propagate errors here rather than printing them
4419 void target_handle_event(struct target
*target
, enum target_event e
)
4421 struct target_event_action
*teap
;
4423 for (teap
= target
->event_action
; teap
!= NULL
; teap
= teap
->next
) {
4424 if (teap
->event
== e
) {
4425 LOG_DEBUG("target: (%d) %s (%s) event: %d (%s) action: %s",
4426 target
->target_number
,
4427 target_name(target
),
4428 target_type_name(target
),
4430 Jim_Nvp_value2name_simple(nvp_target_event
, e
)->name
,
4431 Jim_GetString(teap
->body
, NULL
));
4432 if (Jim_EvalObj(teap
->interp
, teap
->body
) != JIM_OK
) {
4433 Jim_MakeErrorMessage(teap
->interp
);
4434 command_print(NULL
, "%s\n", Jim_GetString(Jim_GetResult(teap
->interp
), NULL
));
4441 * Returns true only if the target has a handler for the specified event.
4443 bool target_has_event_action(struct target
*target
, enum target_event event
)
4445 struct target_event_action
*teap
;
4447 for (teap
= target
->event_action
; teap
!= NULL
; teap
= teap
->next
) {
4448 if (teap
->event
== event
)
4454 enum target_cfg_param
{
4457 TCFG_WORK_AREA_VIRT
,
4458 TCFG_WORK_AREA_PHYS
,
4459 TCFG_WORK_AREA_SIZE
,
4460 TCFG_WORK_AREA_BACKUP
,
4463 TCFG_CHAIN_POSITION
,
4470 static Jim_Nvp nvp_config_opts
[] = {
4471 { .name
= "-type", .value
= TCFG_TYPE
},
4472 { .name
= "-event", .value
= TCFG_EVENT
},
4473 { .name
= "-work-area-virt", .value
= TCFG_WORK_AREA_VIRT
},
4474 { .name
= "-work-area-phys", .value
= TCFG_WORK_AREA_PHYS
},
4475 { .name
= "-work-area-size", .value
= TCFG_WORK_AREA_SIZE
},
4476 { .name
= "-work-area-backup", .value
= TCFG_WORK_AREA_BACKUP
},
4477 { .name
= "-endian" , .value
= TCFG_ENDIAN
},
4478 { .name
= "-coreid", .value
= TCFG_COREID
},
4479 { .name
= "-chain-position", .value
= TCFG_CHAIN_POSITION
},
4480 { .name
= "-dbgbase", .value
= TCFG_DBGBASE
},
4481 { .name
= "-ctibase", .value
= TCFG_CTIBASE
},
4482 { .name
= "-rtos", .value
= TCFG_RTOS
},
4483 { .name
= "-defer-examine", .value
= TCFG_DEFER_EXAMINE
},
4484 { .name
= NULL
, .value
= -1 }
4487 static int target_configure(Jim_GetOptInfo
*goi
, struct target
*target
)
4494 /* parse config or cget options ... */
4495 while (goi
->argc
> 0) {
4496 Jim_SetEmptyResult(goi
->interp
);
4497 /* Jim_GetOpt_Debug(goi); */
4499 if (target
->type
->target_jim_configure
) {
4500 /* target defines a configure function */
4501 /* target gets first dibs on parameters */
4502 e
= (*(target
->type
->target_jim_configure
))(target
, goi
);
4511 /* otherwise we 'continue' below */
4513 e
= Jim_GetOpt_Nvp(goi
, nvp_config_opts
, &n
);
4515 Jim_GetOpt_NvpUnknown(goi
, nvp_config_opts
, 0);
4521 if (goi
->isconfigure
) {
4522 Jim_SetResultFormatted(goi
->interp
,
4523 "not settable: %s", n
->name
);
4527 if (goi
->argc
!= 0) {
4528 Jim_WrongNumArgs(goi
->interp
,
4529 goi
->argc
, goi
->argv
,
4534 Jim_SetResultString(goi
->interp
,
4535 target_type_name(target
), -1);
4539 if (goi
->argc
== 0) {
4540 Jim_WrongNumArgs(goi
->interp
, goi
->argc
, goi
->argv
, "-event ?event-name? ...");
4544 e
= Jim_GetOpt_Nvp(goi
, nvp_target_event
, &n
);
4546 Jim_GetOpt_NvpUnknown(goi
, nvp_target_event
, 1);
4550 if (goi
->isconfigure
) {
4551 if (goi
->argc
!= 1) {
4552 Jim_WrongNumArgs(goi
->interp
, goi
->argc
, goi
->argv
, "-event ?event-name? ?EVENT-BODY?");
4556 if (goi
->argc
!= 0) {
4557 Jim_WrongNumArgs(goi
->interp
, goi
->argc
, goi
->argv
, "-event ?event-name?");
4563 struct target_event_action
*teap
;
4565 teap
= target
->event_action
;
4566 /* replace existing? */
4568 if (teap
->event
== (enum target_event
)n
->value
)
4573 if (goi
->isconfigure
) {
4574 bool replace
= true;
4577 teap
= calloc(1, sizeof(*teap
));
4580 teap
->event
= n
->value
;
4581 teap
->interp
= goi
->interp
;
4582 Jim_GetOpt_Obj(goi
, &o
);
4584 Jim_DecrRefCount(teap
->interp
, teap
->body
);
4585 teap
->body
= Jim_DuplicateObj(goi
->interp
, o
);
4588 * Tcl/TK - "tk events" have a nice feature.
4589 * See the "BIND" command.
4590 * We should support that here.
4591 * You can specify %X and %Y in the event code.
4592 * The idea is: %T - target name.
4593 * The idea is: %N - target number
4594 * The idea is: %E - event name.
4596 Jim_IncrRefCount(teap
->body
);
4599 /* add to head of event list */
4600 teap
->next
= target
->event_action
;
4601 target
->event_action
= teap
;
4603 Jim_SetEmptyResult(goi
->interp
);
4607 Jim_SetEmptyResult(goi
->interp
);
4609 Jim_SetResult(goi
->interp
, Jim_DuplicateObj(goi
->interp
, teap
->body
));
4615 case TCFG_WORK_AREA_VIRT
:
4616 if (goi
->isconfigure
) {
4617 target_free_all_working_areas(target
);
4618 e
= Jim_GetOpt_Wide(goi
, &w
);
4621 target
->working_area_virt
= w
;
4622 target
->working_area_virt_spec
= true;
4627 Jim_SetResult(goi
->interp
, Jim_NewIntObj(goi
->interp
, target
->working_area_virt
));
4631 case TCFG_WORK_AREA_PHYS
:
4632 if (goi
->isconfigure
) {
4633 target_free_all_working_areas(target
);
4634 e
= Jim_GetOpt_Wide(goi
, &w
);
4637 target
->working_area_phys
= w
;
4638 target
->working_area_phys_spec
= true;
4643 Jim_SetResult(goi
->interp
, Jim_NewIntObj(goi
->interp
, target
->working_area_phys
));
4647 case TCFG_WORK_AREA_SIZE
:
4648 if (goi
->isconfigure
) {
4649 target_free_all_working_areas(target
);
4650 e
= Jim_GetOpt_Wide(goi
, &w
);
4653 target
->working_area_size
= w
;
4658 Jim_SetResult(goi
->interp
, Jim_NewIntObj(goi
->interp
, target
->working_area_size
));
4662 case TCFG_WORK_AREA_BACKUP
:
4663 if (goi
->isconfigure
) {
4664 target_free_all_working_areas(target
);
4665 e
= Jim_GetOpt_Wide(goi
, &w
);
4668 /* make this exactly 1 or 0 */
4669 target
->backup_working_area
= (!!w
);
4674 Jim_SetResult(goi
->interp
, Jim_NewIntObj(goi
->interp
, target
->backup_working_area
));
4675 /* loop for more e*/
4680 if (goi
->isconfigure
) {
4681 e
= Jim_GetOpt_Nvp(goi
, nvp_target_endian
, &n
);
4683 Jim_GetOpt_NvpUnknown(goi
, nvp_target_endian
, 1);
4686 target
->endianness
= n
->value
;
4691 n
= Jim_Nvp_value2name_simple(nvp_target_endian
, target
->endianness
);
4692 if (n
->name
== NULL
) {
4693 target
->endianness
= TARGET_LITTLE_ENDIAN
;
4694 n
= Jim_Nvp_value2name_simple(nvp_target_endian
, target
->endianness
);
4696 Jim_SetResultString(goi
->interp
, n
->name
, -1);
4701 if (goi
->isconfigure
) {
4702 e
= Jim_GetOpt_Wide(goi
, &w
);
4705 target
->coreid
= (int32_t)w
;
4710 Jim_SetResult(goi
->interp
, Jim_NewIntObj(goi
->interp
, target
->working_area_size
));
4714 case TCFG_CHAIN_POSITION
:
4715 if (goi
->isconfigure
) {
4717 struct jtag_tap
*tap
;
4718 target_free_all_working_areas(target
);
4719 e
= Jim_GetOpt_Obj(goi
, &o_t
);
4722 tap
= jtag_tap_by_jim_obj(goi
->interp
, o_t
);
4725 /* make this exactly 1 or 0 */
4731 Jim_SetResultString(goi
->interp
, target
->tap
->dotted_name
, -1);
4732 /* loop for more e*/
4735 if (goi
->isconfigure
) {
4736 e
= Jim_GetOpt_Wide(goi
, &w
);
4739 target
->dbgbase
= (uint32_t)w
;
4740 target
->dbgbase_set
= true;
4745 Jim_SetResult(goi
->interp
, Jim_NewIntObj(goi
->interp
, target
->dbgbase
));
4749 if (goi
->isconfigure
) {
4750 e
= Jim_GetOpt_Wide(goi
, &w
);
4753 target
->ctibase
= (uint32_t)w
;
4754 target
->ctibase_set
= true;
4759 Jim_SetResult(goi
->interp
, Jim_NewIntObj(goi
->interp
, target
->ctibase
));
4765 int result
= rtos_create(goi
, target
);
4766 if (result
!= JIM_OK
)
4772 case TCFG_DEFER_EXAMINE
:
4774 target
->defer_examine
= true;
4779 } /* while (goi->argc) */
4782 /* done - we return */
4786 static int jim_target_configure(Jim_Interp
*interp
, int argc
, Jim_Obj
* const *argv
)
4790 Jim_GetOpt_Setup(&goi
, interp
, argc
- 1, argv
+ 1);
4791 goi
.isconfigure
= !strcmp(Jim_GetString(argv
[0], NULL
), "configure");
4793 Jim_WrongNumArgs(goi
.interp
, goi
.argc
, goi
.argv
,
4794 "missing: -option ...");
4797 struct target
*target
= Jim_CmdPrivData(goi
.interp
);
4798 return target_configure(&goi
, target
);
4801 static int jim_target_mw(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4803 const char *cmd_name
= Jim_GetString(argv
[0], NULL
);
4806 Jim_GetOpt_Setup(&goi
, interp
, argc
- 1, argv
+ 1);
4808 if (goi
.argc
< 2 || goi
.argc
> 4) {
4809 Jim_SetResultFormatted(goi
.interp
,
4810 "usage: %s [phys] <address> <data> [<count>]", cmd_name
);
4815 fn
= target_write_memory
;
4818 if (strcmp(Jim_GetString(argv
[1], NULL
), "phys") == 0) {
4820 struct Jim_Obj
*obj
;
4821 e
= Jim_GetOpt_Obj(&goi
, &obj
);
4825 fn
= target_write_phys_memory
;
4829 e
= Jim_GetOpt_Wide(&goi
, &a
);
4834 e
= Jim_GetOpt_Wide(&goi
, &b
);
4839 if (goi
.argc
== 1) {
4840 e
= Jim_GetOpt_Wide(&goi
, &c
);
4845 /* all args must be consumed */
4849 struct target
*target
= Jim_CmdPrivData(goi
.interp
);
4851 if (strcasecmp(cmd_name
, "mww") == 0)
4853 else if (strcasecmp(cmd_name
, "mwh") == 0)
4855 else if (strcasecmp(cmd_name
, "mwb") == 0)
4858 LOG_ERROR("command '%s' unknown: ", cmd_name
);
4862 return (target_fill_mem(target
, a
, fn
, data_size
, b
, c
) == ERROR_OK
) ? JIM_OK
: JIM_ERR
;
4866 * @brief Reads an array of words/halfwords/bytes from target memory starting at specified address.
4868 * Usage: mdw [phys] <address> [<count>] - for 32 bit reads
4869 * mdh [phys] <address> [<count>] - for 16 bit reads
4870 * mdb [phys] <address> [<count>] - for 8 bit reads
4872 * Count defaults to 1.
4874 * Calls target_read_memory or target_read_phys_memory depending on
4875 * the presence of the "phys" argument
4876 * Reads the target memory in blocks of max. 32 bytes, and returns an array of ints formatted
4877 * to int representation in base16.
4878 * Also outputs read data in a human readable form using command_print
4880 * @param phys if present target_read_phys_memory will be used instead of target_read_memory
4881 * @param address address where to start the read. May be specified in decimal or hex using the standard "0x" prefix
4882 * @param count optional count parameter to read an array of values. If not specified, defaults to 1.
4883 * @returns: JIM_ERR on error or JIM_OK on success and sets the result string to an array of ascii formatted numbers
4884 * on success, with [<count>] number of elements.
4886 * In case of little endian target:
4887 * Example1: "mdw 0x00000000" returns "10123456"
4888 * Exmaple2: "mdh 0x00000000 1" returns "3456"
4889 * Example3: "mdb 0x00000000" returns "56"
4890 * Example4: "mdh 0x00000000 2" returns "3456 1012"
4891 * Example5: "mdb 0x00000000 3" returns "56 34 12"
4893 static int jim_target_md(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4895 const char *cmd_name
= Jim_GetString(argv
[0], NULL
);
4898 Jim_GetOpt_Setup(&goi
, interp
, argc
- 1, argv
+ 1);
4900 if ((goi
.argc
< 1) || (goi
.argc
> 3)) {
4901 Jim_SetResultFormatted(goi
.interp
,
4902 "usage: %s [phys] <address> [<count>]", cmd_name
);
4906 int (*fn
)(struct target
*target
,
4907 target_addr_t address
, uint32_t size
, uint32_t count
, uint8_t *buffer
);
4908 fn
= target_read_memory
;
4911 if (strcmp(Jim_GetString(argv
[1], NULL
), "phys") == 0) {
4913 struct Jim_Obj
*obj
;
4914 e
= Jim_GetOpt_Obj(&goi
, &obj
);
4918 fn
= target_read_phys_memory
;
4921 /* Read address parameter */
4923 e
= Jim_GetOpt_Wide(&goi
, &addr
);
4927 /* If next parameter exists, read it out as the count parameter, if not, set it to 1 (default) */
4929 if (goi
.argc
== 1) {
4930 e
= Jim_GetOpt_Wide(&goi
, &count
);
4936 /* all args must be consumed */
4940 jim_wide dwidth
= 1; /* shut up gcc */
4941 if (strcasecmp(cmd_name
, "mdw") == 0)
4943 else if (strcasecmp(cmd_name
, "mdh") == 0)
4945 else if (strcasecmp(cmd_name
, "mdb") == 0)
4948 LOG_ERROR("command '%s' unknown: ", cmd_name
);
4952 /* convert count to "bytes" */
4953 int bytes
= count
* dwidth
;
4955 struct target
*target
= Jim_CmdPrivData(goi
.interp
);
4956 uint8_t target_buf
[32];
4959 y
= (bytes
< 16) ? bytes
: 16; /* y = min(bytes, 16); */
4961 /* Try to read out next block */
4962 e
= fn(target
, addr
, dwidth
, y
/ dwidth
, target_buf
);
4964 if (e
!= ERROR_OK
) {
4965 Jim_SetResultFormatted(interp
, "error reading target @ 0x%08lx", (long)addr
);
4969 command_print_sameline(NULL
, "0x%08x ", (int)(addr
));
4972 for (x
= 0; x
< 16 && x
< y
; x
+= 4) {
4973 z
= target_buffer_get_u32(target
, &(target_buf
[x
]));
4974 command_print_sameline(NULL
, "%08x ", (int)(z
));
4976 for (; (x
< 16) ; x
+= 4)
4977 command_print_sameline(NULL
, " ");
4980 for (x
= 0; x
< 16 && x
< y
; x
+= 2) {
4981 z
= target_buffer_get_u16(target
, &(target_buf
[x
]));
4982 command_print_sameline(NULL
, "%04x ", (int)(z
));
4984 for (; (x
< 16) ; x
+= 2)
4985 command_print_sameline(NULL
, " ");
4989 for (x
= 0 ; (x
< 16) && (x
< y
) ; x
+= 1) {
4990 z
= target_buffer_get_u8(target
, &(target_buf
[x
]));
4991 command_print_sameline(NULL
, "%02x ", (int)(z
));
4993 for (; (x
< 16) ; x
+= 1)
4994 command_print_sameline(NULL
, " ");
4997 /* ascii-ify the bytes */
4998 for (x
= 0 ; x
< y
; x
++) {
4999 if ((target_buf
[x
] >= 0x20) &&
5000 (target_buf
[x
] <= 0x7e)) {
5004 target_buf
[x
] = '.';
5009 target_buf
[x
] = ' ';
5014 /* print - with a newline */
5015 command_print_sameline(NULL
, "%s\n", target_buf
);
5023 static int jim_target_mem2array(Jim_Interp
*interp
,
5024 int argc
, Jim_Obj
*const *argv
)
5026 struct target
*target
= Jim_CmdPrivData(interp
);
5027 return target_mem2array(interp
, target
, argc
- 1, argv
+ 1);
5030 static int jim_target_array2mem(Jim_Interp
*interp
,
5031 int argc
, Jim_Obj
*const *argv
)
5033 struct target
*target
= Jim_CmdPrivData(interp
);
5034 return target_array2mem(interp
, target
, argc
- 1, argv
+ 1);
5037 static int jim_target_tap_disabled(Jim_Interp
*interp
)
5039 Jim_SetResultFormatted(interp
, "[TAP is disabled]");
5043 static int jim_target_examine(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
5045 bool allow_defer
= false;
5048 Jim_GetOpt_Setup(&goi
, interp
, argc
- 1, argv
+ 1);
5050 const char *cmd_name
= Jim_GetString(argv
[0], NULL
);
5051 Jim_SetResultFormatted(goi
.interp
,
5052 "usage: %s ['allow-defer']", cmd_name
);
5056 strcmp(Jim_GetString(argv
[1], NULL
), "allow-defer") == 0) {
5058 struct Jim_Obj
*obj
;
5059 int e
= Jim_GetOpt_Obj(&goi
, &obj
);
5065 struct target
*target
= Jim_CmdPrivData(interp
);
5066 if (!target
->tap
->enabled
)
5067 return jim_target_tap_disabled(interp
);
5069 if (allow_defer
&& target
->defer_examine
) {
5070 LOG_INFO("Deferring arp_examine of %s", target_name(target
));
5071 LOG_INFO("Use arp_examine command to examine it manually!");
5075 int e
= target
->type
->examine(target
);
5081 static int jim_target_was_examined(Jim_Interp
*interp
, int argc
, Jim_Obj
* const *argv
)
5083 struct target
*target
= Jim_CmdPrivData(interp
);
5085 Jim_SetResultBool(interp
, target_was_examined(target
));
5089 static int jim_target_examine_deferred(Jim_Interp
*interp
, int argc
, Jim_Obj
* const *argv
)
5091 struct target
*target
= Jim_CmdPrivData(interp
);
5093 Jim_SetResultBool(interp
, target
->defer_examine
);
5097 static int jim_target_halt_gdb(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
5100 Jim_WrongNumArgs(interp
, 1, argv
, "[no parameters]");
5103 struct target
*target
= Jim_CmdPrivData(interp
);
5105 if (target_call_event_callbacks(target
, TARGET_EVENT_GDB_HALT
) != ERROR_OK
)
5111 static int jim_target_poll(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
5114 Jim_WrongNumArgs(interp
, 1, argv
, "[no parameters]");
5117 struct target
*target
= Jim_CmdPrivData(interp
);
5118 if (!target
->tap
->enabled
)
5119 return jim_target_tap_disabled(interp
);
5122 if (!(target_was_examined(target
)))
5123 e
= ERROR_TARGET_NOT_EXAMINED
;
5125 e
= target
->type
->poll(target
);
5131 static int jim_target_reset(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
5134 Jim_GetOpt_Setup(&goi
, interp
, argc
- 1, argv
+ 1);
5136 if (goi
.argc
!= 2) {
5137 Jim_WrongNumArgs(interp
, 0, argv
,
5138 "([tT]|[fF]|assert|deassert) BOOL");
5143 int e
= Jim_GetOpt_Nvp(&goi
, nvp_assert
, &n
);
5145 Jim_GetOpt_NvpUnknown(&goi
, nvp_assert
, 1);
5148 /* the halt or not param */
5150 e
= Jim_GetOpt_Wide(&goi
, &a
);
5154 struct target
*target
= Jim_CmdPrivData(goi
.interp
);
5155 if (!target
->tap
->enabled
)
5156 return jim_target_tap_disabled(interp
);
5158 if (!target
->type
->assert_reset
|| !target
->type
->deassert_reset
) {
5159 Jim_SetResultFormatted(interp
,
5160 "No target-specific reset for %s",
5161 target_name(target
));
5165 if (target
->defer_examine
)
5166 target_reset_examined(target
);
5168 /* determine if we should halt or not. */
5169 target
->reset_halt
= !!a
;
5170 /* When this happens - all workareas are invalid. */
5171 target_free_all_working_areas_restore(target
, 0);
5174 if (n
->value
== NVP_ASSERT
)
5175 e
= target
->type
->assert_reset(target
);
5177 e
= target
->type
->deassert_reset(target
);
5178 return (e
== ERROR_OK
) ? JIM_OK
: JIM_ERR
;
5181 static int jim_target_halt(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
5184 Jim_WrongNumArgs(interp
, 1, argv
, "[no parameters]");
5187 struct target
*target
= Jim_CmdPrivData(interp
);
5188 if (!target
->tap
->enabled
)
5189 return jim_target_tap_disabled(interp
);
5190 int e
= target
->type
->halt(target
);
5191 return (e
== ERROR_OK
) ? JIM_OK
: JIM_ERR
;
5194 static int jim_target_wait_state(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
5197 Jim_GetOpt_Setup(&goi
, interp
, argc
- 1, argv
+ 1);
5199 /* params: <name> statename timeoutmsecs */
5200 if (goi
.argc
!= 2) {
5201 const char *cmd_name
= Jim_GetString(argv
[0], NULL
);
5202 Jim_SetResultFormatted(goi
.interp
,
5203 "%s <state_name> <timeout_in_msec>", cmd_name
);
5208 int e
= Jim_GetOpt_Nvp(&goi
, nvp_target_state
, &n
);
5210 Jim_GetOpt_NvpUnknown(&goi
, nvp_target_state
, 1);
5214 e
= Jim_GetOpt_Wide(&goi
, &a
);
5217 struct target
*target
= Jim_CmdPrivData(interp
);
5218 if (!target
->tap
->enabled
)
5219 return jim_target_tap_disabled(interp
);
5221 e
= target_wait_state(target
, n
->value
, a
);
5222 if (e
!= ERROR_OK
) {
5223 Jim_Obj
*eObj
= Jim_NewIntObj(interp
, e
);
5224 Jim_SetResultFormatted(goi
.interp
,
5225 "target: %s wait %s fails (%#s) %s",
5226 target_name(target
), n
->name
,
5227 eObj
, target_strerror_safe(e
));
5228 Jim_FreeNewObj(interp
, eObj
);
5233 /* List for human, Events defined for this target.
5234 * scripts/programs should use 'name cget -event NAME'
5236 static int jim_target_event_list(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
5238 struct command_context
*cmd_ctx
= current_command_context(interp
);
5239 assert(cmd_ctx
!= NULL
);
5241 struct target
*target
= Jim_CmdPrivData(interp
);
5242 struct target_event_action
*teap
= target
->event_action
;
5243 command_print(cmd_ctx
, "Event actions for target (%d) %s\n",
5244 target
->target_number
,
5245 target_name(target
));
5246 command_print(cmd_ctx
, "%-25s | Body", "Event");
5247 command_print(cmd_ctx
, "------------------------- | "
5248 "----------------------------------------");
5250 Jim_Nvp
*opt
= Jim_Nvp_value2name_simple(nvp_target_event
, teap
->event
);
5251 command_print(cmd_ctx
, "%-25s | %s",
5252 opt
->name
, Jim_GetString(teap
->body
, NULL
));
5255 command_print(cmd_ctx
, "***END***");
5258 static int jim_target_current_state(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
5261 Jim_WrongNumArgs(interp
, 1, argv
, "[no parameters]");
5264 struct target
*target
= Jim_CmdPrivData(interp
);
5265 Jim_SetResultString(interp
, target_state_name(target
), -1);
5268 static int jim_target_invoke_event(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
5271 Jim_GetOpt_Setup(&goi
, interp
, argc
- 1, argv
+ 1);
5272 if (goi
.argc
!= 1) {
5273 const char *cmd_name
= Jim_GetString(argv
[0], NULL
);
5274 Jim_SetResultFormatted(goi
.interp
, "%s <eventname>", cmd_name
);
5278 int e
= Jim_GetOpt_Nvp(&goi
, nvp_target_event
, &n
);
5280 Jim_GetOpt_NvpUnknown(&goi
, nvp_target_event
, 1);
5283 struct target
*target
= Jim_CmdPrivData(interp
);
5284 target_handle_event(target
, n
->value
);
5288 static const struct command_registration target_instance_command_handlers
[] = {
5290 .name
= "configure",
5291 .mode
= COMMAND_CONFIG
,
5292 .jim_handler
= jim_target_configure
,
5293 .help
= "configure a new target for use",
5294 .usage
= "[target_attribute ...]",
5298 .mode
= COMMAND_ANY
,
5299 .jim_handler
= jim_target_configure
,
5300 .help
= "returns the specified target attribute",
5301 .usage
= "target_attribute",
5305 .mode
= COMMAND_EXEC
,
5306 .jim_handler
= jim_target_mw
,
5307 .help
= "Write 32-bit word(s) to target memory",
5308 .usage
= "address data [count]",
5312 .mode
= COMMAND_EXEC
,
5313 .jim_handler
= jim_target_mw
,
5314 .help
= "Write 16-bit half-word(s) to target memory",
5315 .usage
= "address data [count]",
5319 .mode
= COMMAND_EXEC
,
5320 .jim_handler
= jim_target_mw
,
5321 .help
= "Write byte(s) to target memory",
5322 .usage
= "address data [count]",
5326 .mode
= COMMAND_EXEC
,
5327 .jim_handler
= jim_target_md
,
5328 .help
= "Display target memory as 32-bit words",
5329 .usage
= "address [count]",
5333 .mode
= COMMAND_EXEC
,
5334 .jim_handler
= jim_target_md
,
5335 .help
= "Display target memory as 16-bit half-words",
5336 .usage
= "address [count]",
5340 .mode
= COMMAND_EXEC
,
5341 .jim_handler
= jim_target_md
,
5342 .help
= "Display target memory as 8-bit bytes",
5343 .usage
= "address [count]",
5346 .name
= "array2mem",
5347 .mode
= COMMAND_EXEC
,
5348 .jim_handler
= jim_target_array2mem
,
5349 .help
= "Writes Tcl array of 8/16/32 bit numbers "
5351 .usage
= "arrayname bitwidth address count",
5354 .name
= "mem2array",
5355 .mode
= COMMAND_EXEC
,
5356 .jim_handler
= jim_target_mem2array
,
5357 .help
= "Loads Tcl array of 8/16/32 bit numbers "
5358 "from target memory",
5359 .usage
= "arrayname bitwidth address count",
5362 .name
= "eventlist",
5363 .mode
= COMMAND_EXEC
,
5364 .jim_handler
= jim_target_event_list
,
5365 .help
= "displays a table of events defined for this target",
5369 .mode
= COMMAND_EXEC
,
5370 .jim_handler
= jim_target_current_state
,
5371 .help
= "displays the current state of this target",
5374 .name
= "arp_examine",
5375 .mode
= COMMAND_EXEC
,
5376 .jim_handler
= jim_target_examine
,
5377 .help
= "used internally for reset processing",
5378 .usage
= "arp_examine ['allow-defer']",
5381 .name
= "was_examined",
5382 .mode
= COMMAND_EXEC
,
5383 .jim_handler
= jim_target_was_examined
,
5384 .help
= "used internally for reset processing",
5385 .usage
= "was_examined",
5388 .name
= "examine_deferred",
5389 .mode
= COMMAND_EXEC
,
5390 .jim_handler
= jim_target_examine_deferred
,
5391 .help
= "used internally for reset processing",
5392 .usage
= "examine_deferred",
5395 .name
= "arp_halt_gdb",
5396 .mode
= COMMAND_EXEC
,
5397 .jim_handler
= jim_target_halt_gdb
,
5398 .help
= "used internally for reset processing to halt GDB",
5402 .mode
= COMMAND_EXEC
,
5403 .jim_handler
= jim_target_poll
,
5404 .help
= "used internally for reset processing",
5407 .name
= "arp_reset",
5408 .mode
= COMMAND_EXEC
,
5409 .jim_handler
= jim_target_reset
,
5410 .help
= "used internally for reset processing",
5414 .mode
= COMMAND_EXEC
,
5415 .jim_handler
= jim_target_halt
,
5416 .help
= "used internally for reset processing",
5419 .name
= "arp_waitstate",
5420 .mode
= COMMAND_EXEC
,
5421 .jim_handler
= jim_target_wait_state
,
5422 .help
= "used internally for reset processing",
5425 .name
= "invoke-event",
5426 .mode
= COMMAND_EXEC
,
5427 .jim_handler
= jim_target_invoke_event
,
5428 .help
= "invoke handler for specified event",
5429 .usage
= "event_name",
5431 COMMAND_REGISTRATION_DONE
5434 static int target_create(Jim_GetOptInfo
*goi
)
5441 struct target
*target
;
5442 struct command_context
*cmd_ctx
;
5444 cmd_ctx
= current_command_context(goi
->interp
);
5445 assert(cmd_ctx
!= NULL
);
5447 if (goi
->argc
< 3) {
5448 Jim_WrongNumArgs(goi
->interp
, 1, goi
->argv
, "?name? ?type? ..options...");
5453 Jim_GetOpt_Obj(goi
, &new_cmd
);
5454 /* does this command exist? */
5455 cmd
= Jim_GetCommand(goi
->interp
, new_cmd
, JIM_ERRMSG
);
5457 cp
= Jim_GetString(new_cmd
, NULL
);
5458 Jim_SetResultFormatted(goi
->interp
, "Command/target: %s Exists", cp
);
5463 e
= Jim_GetOpt_String(goi
, &cp
, NULL
);
5466 struct transport
*tr
= get_current_transport();
5467 if (tr
->override_target
) {
5468 e
= tr
->override_target(&cp
);
5469 if (e
!= ERROR_OK
) {
5470 LOG_ERROR("The selected transport doesn't support this target");
5473 LOG_INFO("The selected transport took over low-level target control. The results might differ compared to plain JTAG/SWD");
5475 /* now does target type exist */
5476 for (x
= 0 ; target_types
[x
] ; x
++) {
5477 if (0 == strcmp(cp
, target_types
[x
]->name
)) {
5482 /* check for deprecated name */
5483 if (target_types
[x
]->deprecated_name
) {
5484 if (0 == strcmp(cp
, target_types
[x
]->deprecated_name
)) {
5486 LOG_WARNING("target name is deprecated use: \'%s\'", target_types
[x
]->name
);
5491 if (target_types
[x
] == NULL
) {
5492 Jim_SetResultFormatted(goi
->interp
, "Unknown target type %s, try one of ", cp
);
5493 for (x
= 0 ; target_types
[x
] ; x
++) {
5494 if (target_types
[x
+ 1]) {
5495 Jim_AppendStrings(goi
->interp
,
5496 Jim_GetResult(goi
->interp
),
5497 target_types
[x
]->name
,
5500 Jim_AppendStrings(goi
->interp
,
5501 Jim_GetResult(goi
->interp
),
5503 target_types
[x
]->name
, NULL
);
5510 target
= calloc(1, sizeof(struct target
));
5511 /* set target number */
5512 target
->target_number
= new_target_number();
5513 cmd_ctx
->current_target
= target
->target_number
;
5515 /* allocate memory for each unique target type */
5516 target
->type
= calloc(1, sizeof(struct target_type
));
5518 memcpy(target
->type
, target_types
[x
], sizeof(struct target_type
));
5520 /* will be set by "-endian" */
5521 target
->endianness
= TARGET_ENDIAN_UNKNOWN
;
5523 /* default to first core, override with -coreid */
5526 target
->working_area
= 0x0;
5527 target
->working_area_size
= 0x0;
5528 target
->working_areas
= NULL
;
5529 target
->backup_working_area
= 0;
5531 target
->state
= TARGET_UNKNOWN
;
5532 target
->debug_reason
= DBG_REASON_UNDEFINED
;
5533 target
->reg_cache
= NULL
;
5534 target
->breakpoints
= NULL
;
5535 target
->watchpoints
= NULL
;
5536 target
->next
= NULL
;
5537 target
->arch_info
= NULL
;
5539 target
->display
= 1;
5541 target
->halt_issued
= false;
5543 /* initialize trace information */
5544 target
->trace_info
= calloc(1, sizeof(struct trace
));
5546 target
->dbgmsg
= NULL
;
5547 target
->dbg_msg_enabled
= 0;
5549 target
->endianness
= TARGET_ENDIAN_UNKNOWN
;
5551 target
->rtos
= NULL
;
5552 target
->rtos_auto_detect
= false;
5554 /* Do the rest as "configure" options */
5555 goi
->isconfigure
= 1;
5556 e
= target_configure(goi
, target
);
5558 if (target
->tap
== NULL
) {
5559 Jim_SetResultString(goi
->interp
, "-chain-position required when creating target", -1);
5569 if (target
->endianness
== TARGET_ENDIAN_UNKNOWN
) {
5570 /* default endian to little if not specified */
5571 target
->endianness
= TARGET_LITTLE_ENDIAN
;
5574 cp
= Jim_GetString(new_cmd
, NULL
);
5575 target
->cmd_name
= strdup(cp
);
5577 /* create the target specific commands */
5578 if (target
->type
->commands
) {
5579 e
= register_commands(cmd_ctx
, NULL
, target
->type
->commands
);
5581 LOG_ERROR("unable to register '%s' commands", cp
);
5583 if (target
->type
->target_create
)
5584 (*(target
->type
->target_create
))(target
, goi
->interp
);
5586 /* append to end of list */
5588 struct target
**tpp
;
5589 tpp
= &(all_targets
);
5591 tpp
= &((*tpp
)->next
);
5595 /* now - create the new target name command */
5596 const struct command_registration target_subcommands
[] = {
5598 .chain
= target_instance_command_handlers
,
5601 .chain
= target
->type
->commands
,
5603 COMMAND_REGISTRATION_DONE
5605 const struct command_registration target_commands
[] = {
5608 .mode
= COMMAND_ANY
,
5609 .help
= "target command group",
5611 .chain
= target_subcommands
,
5613 COMMAND_REGISTRATION_DONE
5615 e
= register_commands(cmd_ctx
, NULL
, target_commands
);
5619 struct command
*c
= command_find_in_context(cmd_ctx
, cp
);
5621 command_set_handler_data(c
, target
);
5623 return (ERROR_OK
== e
) ? JIM_OK
: JIM_ERR
;
5626 static int jim_target_current(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
5629 Jim_WrongNumArgs(interp
, 1, argv
, "Too many parameters");
5632 struct command_context
*cmd_ctx
= current_command_context(interp
);
5633 assert(cmd_ctx
!= NULL
);
5635 Jim_SetResultString(interp
, target_name(get_current_target(cmd_ctx
)), -1);
5639 static int jim_target_types(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
5642 Jim_WrongNumArgs(interp
, 1, argv
, "Too many parameters");
5645 Jim_SetResult(interp
, Jim_NewListObj(interp
, NULL
, 0));
5646 for (unsigned x
= 0; NULL
!= target_types
[x
]; x
++) {
5647 Jim_ListAppendElement(interp
, Jim_GetResult(interp
),
5648 Jim_NewStringObj(interp
, target_types
[x
]->name
, -1));
5653 static int jim_target_names(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
5656 Jim_WrongNumArgs(interp
, 1, argv
, "Too many parameters");
5659 Jim_SetResult(interp
, Jim_NewListObj(interp
, NULL
, 0));
5660 struct target
*target
= all_targets
;
5662 Jim_ListAppendElement(interp
, Jim_GetResult(interp
),
5663 Jim_NewStringObj(interp
, target_name(target
), -1));
5664 target
= target
->next
;
5669 static int jim_target_smp(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
5672 const char *targetname
;
5674 struct target
*target
= (struct target
*) NULL
;
5675 struct target_list
*head
, *curr
, *new;
5676 curr
= (struct target_list
*) NULL
;
5677 head
= (struct target_list
*) NULL
;
5680 LOG_DEBUG("%d", argc
);
5681 /* argv[1] = target to associate in smp
5682 * argv[2] = target to assoicate in smp
5686 for (i
= 1; i
< argc
; i
++) {
5688 targetname
= Jim_GetString(argv
[i
], &len
);
5689 target
= get_target(targetname
);
5690 LOG_DEBUG("%s ", targetname
);
5692 new = malloc(sizeof(struct target_list
));
5693 new->target
= target
;
5694 new->next
= (struct target_list
*)NULL
;
5695 if (head
== (struct target_list
*)NULL
) {
5704 /* now parse the list of cpu and put the target in smp mode*/
5707 while (curr
!= (struct target_list
*)NULL
) {
5708 target
= curr
->target
;
5710 target
->head
= head
;
5714 if (target
&& target
->rtos
)
5715 retval
= rtos_smp_init(head
->target
);
5721 static int jim_target_create(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
5724 Jim_GetOpt_Setup(&goi
, interp
, argc
- 1, argv
+ 1);
5726 Jim_WrongNumArgs(goi
.interp
, goi
.argc
, goi
.argv
,
5727 "<name> <target_type> [<target_options> ...]");
5730 return target_create(&goi
);
5733 static const struct command_registration target_subcommand_handlers
[] = {
5736 .mode
= COMMAND_CONFIG
,
5737 .handler
= handle_target_init_command
,
5738 .help
= "initialize targets",
5742 /* REVISIT this should be COMMAND_CONFIG ... */
5743 .mode
= COMMAND_ANY
,
5744 .jim_handler
= jim_target_create
,
5745 .usage
= "name type '-chain-position' name [options ...]",
5746 .help
= "Creates and selects a new target",
5750 .mode
= COMMAND_ANY
,
5751 .jim_handler
= jim_target_current
,
5752 .help
= "Returns the currently selected target",
5756 .mode
= COMMAND_ANY
,
5757 .jim_handler
= jim_target_types
,
5758 .help
= "Returns the available target types as "
5759 "a list of strings",
5763 .mode
= COMMAND_ANY
,
5764 .jim_handler
= jim_target_names
,
5765 .help
= "Returns the names of all targets as a list of strings",
5769 .mode
= COMMAND_ANY
,
5770 .jim_handler
= jim_target_smp
,
5771 .usage
= "targetname1 targetname2 ...",
5772 .help
= "gather several target in a smp list"
5775 COMMAND_REGISTRATION_DONE
5779 target_addr_t address
;
5785 static int fastload_num
;
5786 static struct FastLoad
*fastload
;
5788 static void free_fastload(void)
5790 if (fastload
!= NULL
) {
5792 for (i
= 0; i
< fastload_num
; i
++) {
5793 if (fastload
[i
].data
)
5794 free(fastload
[i
].data
);
5801 COMMAND_HANDLER(handle_fast_load_image_command
)
5805 uint32_t image_size
;
5806 target_addr_t min_address
= 0;
5807 target_addr_t max_address
= -1;
5812 int retval
= CALL_COMMAND_HANDLER(parse_load_image_command_CMD_ARGV
,
5813 &image
, &min_address
, &max_address
);
5814 if (ERROR_OK
!= retval
)
5817 struct duration bench
;
5818 duration_start(&bench
);
5820 retval
= image_open(&image
, CMD_ARGV
[0], (CMD_ARGC
>= 3) ? CMD_ARGV
[2] : NULL
);
5821 if (retval
!= ERROR_OK
)
5826 fastload_num
= image
.num_sections
;
5827 fastload
= malloc(sizeof(struct FastLoad
)*image
.num_sections
);
5828 if (fastload
== NULL
) {
5829 command_print(CMD_CTX
, "out of memory");
5830 image_close(&image
);
5833 memset(fastload
, 0, sizeof(struct FastLoad
)*image
.num_sections
);
5834 for (i
= 0; i
< image
.num_sections
; i
++) {
5835 buffer
= malloc(image
.sections
[i
].size
);
5836 if (buffer
== NULL
) {
5837 command_print(CMD_CTX
, "error allocating buffer for section (%d bytes)",
5838 (int)(image
.sections
[i
].size
));
5839 retval
= ERROR_FAIL
;
5843 retval
= image_read_section(&image
, i
, 0x0, image
.sections
[i
].size
, buffer
, &buf_cnt
);
5844 if (retval
!= ERROR_OK
) {
5849 uint32_t offset
= 0;
5850 uint32_t length
= buf_cnt
;
5852 /* DANGER!!! beware of unsigned comparision here!!! */
5854 if ((image
.sections
[i
].base_address
+ buf_cnt
>= min_address
) &&
5855 (image
.sections
[i
].base_address
< max_address
)) {
5856 if (image
.sections
[i
].base_address
< min_address
) {
5857 /* clip addresses below */
5858 offset
+= min_address
-image
.sections
[i
].base_address
;
5862 if (image
.sections
[i
].base_address
+ buf_cnt
> max_address
)
5863 length
-= (image
.sections
[i
].base_address
+ buf_cnt
)-max_address
;
5865 fastload
[i
].address
= image
.sections
[i
].base_address
+ offset
;
5866 fastload
[i
].data
= malloc(length
);
5867 if (fastload
[i
].data
== NULL
) {
5869 command_print(CMD_CTX
, "error allocating buffer for section (%" PRIu32
" bytes)",
5871 retval
= ERROR_FAIL
;
5874 memcpy(fastload
[i
].data
, buffer
+ offset
, length
);
5875 fastload
[i
].length
= length
;
5877 image_size
+= length
;
5878 command_print(CMD_CTX
, "%u bytes written at address 0x%8.8x",
5879 (unsigned int)length
,
5880 ((unsigned int)(image
.sections
[i
].base_address
+ offset
)));
5886 if ((ERROR_OK
== retval
) && (duration_measure(&bench
) == ERROR_OK
)) {
5887 command_print(CMD_CTX
, "Loaded %" PRIu32
" bytes "
5888 "in %fs (%0.3f KiB/s)", image_size
,
5889 duration_elapsed(&bench
), duration_kbps(&bench
, image_size
));
5891 command_print(CMD_CTX
,
5892 "WARNING: image has not been loaded to target!"
5893 "You can issue a 'fast_load' to finish loading.");
5896 image_close(&image
);
5898 if (retval
!= ERROR_OK
)
5904 COMMAND_HANDLER(handle_fast_load_command
)
5907 return ERROR_COMMAND_SYNTAX_ERROR
;
5908 if (fastload
== NULL
) {
5909 LOG_ERROR("No image in memory");
5913 int64_t ms
= timeval_ms();
5915 int retval
= ERROR_OK
;
5916 for (i
= 0; i
< fastload_num
; i
++) {
5917 struct target
*target
= get_current_target(CMD_CTX
);
5918 command_print(CMD_CTX
, "Write to 0x%08x, length 0x%08x",
5919 (unsigned int)(fastload
[i
].address
),
5920 (unsigned int)(fastload
[i
].length
));
5921 retval
= target_write_buffer(target
, fastload
[i
].address
, fastload
[i
].length
, fastload
[i
].data
);
5922 if (retval
!= ERROR_OK
)
5924 size
+= fastload
[i
].length
;
5926 if (retval
== ERROR_OK
) {
5927 int64_t after
= timeval_ms();
5928 command_print(CMD_CTX
, "Loaded image %f kBytes/s", (float)(size
/1024.0)/((float)(after
-ms
)/1000.0));
5933 static const struct command_registration target_command_handlers
[] = {
5936 .handler
= handle_targets_command
,
5937 .mode
= COMMAND_ANY
,
5938 .help
= "change current default target (one parameter) "
5939 "or prints table of all targets (no parameters)",
5940 .usage
= "[target]",
5944 .mode
= COMMAND_CONFIG
,
5945 .help
= "configure target",
5947 .chain
= target_subcommand_handlers
,
5949 COMMAND_REGISTRATION_DONE
5952 int target_register_commands(struct command_context
*cmd_ctx
)
5954 return register_commands(cmd_ctx
, NULL
, target_command_handlers
);
5957 static bool target_reset_nag
= true;
5959 bool get_target_reset_nag(void)
5961 return target_reset_nag
;
5964 COMMAND_HANDLER(handle_target_reset_nag
)
5966 return CALL_COMMAND_HANDLER(handle_command_parse_bool
,
5967 &target_reset_nag
, "Nag after each reset about options to improve "
5971 COMMAND_HANDLER(handle_ps_command
)
5973 struct target
*target
= get_current_target(CMD_CTX
);
5975 if (target
->state
!= TARGET_HALTED
) {
5976 LOG_INFO("target not halted !!");
5980 if ((target
->rtos
) && (target
->rtos
->type
)
5981 && (target
->rtos
->type
->ps_command
)) {
5982 display
= target
->rtos
->type
->ps_command(target
);
5983 command_print(CMD_CTX
, "%s", display
);
5988 return ERROR_TARGET_FAILURE
;
5992 static void binprint(struct command_context
*cmd_ctx
, const char *text
, const uint8_t *buf
, int size
)
5995 command_print_sameline(cmd_ctx
, "%s", text
);
5996 for (int i
= 0; i
< size
; i
++)
5997 command_print_sameline(cmd_ctx
, " %02x", buf
[i
]);
5998 command_print(cmd_ctx
, " ");
6001 COMMAND_HANDLER(handle_test_mem_access_command
)
6003 struct target
*target
= get_current_target(CMD_CTX
);
6005 int retval
= ERROR_OK
;
6007 if (target
->state
!= TARGET_HALTED
) {
6008 LOG_INFO("target not halted !!");
6013 return ERROR_COMMAND_SYNTAX_ERROR
;
6015 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], test_size
);
6018 size_t num_bytes
= test_size
+ 4;
6020 struct working_area
*wa
= NULL
;
6021 retval
= target_alloc_working_area(target
, num_bytes
, &wa
);
6022 if (retval
!= ERROR_OK
) {
6023 LOG_ERROR("Not enough working area");
6027 uint8_t *test_pattern
= malloc(num_bytes
);
6029 for (size_t i
= 0; i
< num_bytes
; i
++)
6030 test_pattern
[i
] = rand();
6032 retval
= target_write_memory(target
, wa
->address
, 1, num_bytes
, test_pattern
);
6033 if (retval
!= ERROR_OK
) {
6034 LOG_ERROR("Test pattern write failed");
6038 for (int host_offset
= 0; host_offset
<= 1; host_offset
++) {
6039 for (int size
= 1; size
<= 4; size
*= 2) {
6040 for (int offset
= 0; offset
< 4; offset
++) {
6041 uint32_t count
= test_size
/ size
;
6042 size_t host_bufsiz
= (count
+ 2) * size
+ host_offset
;
6043 uint8_t *read_ref
= malloc(host_bufsiz
);
6044 uint8_t *read_buf
= malloc(host_bufsiz
);
6046 for (size_t i
= 0; i
< host_bufsiz
; i
++) {
6047 read_ref
[i
] = rand();
6048 read_buf
[i
] = read_ref
[i
];
6050 command_print_sameline(CMD_CTX
,
6051 "Test read %" PRIu32
" x %d @ %d to %saligned buffer: ", count
,
6052 size
, offset
, host_offset
? "un" : "");
6054 struct duration bench
;
6055 duration_start(&bench
);
6057 retval
= target_read_memory(target
, wa
->address
+ offset
, size
, count
,
6058 read_buf
+ size
+ host_offset
);
6060 duration_measure(&bench
);
6062 if (retval
== ERROR_TARGET_UNALIGNED_ACCESS
) {
6063 command_print(CMD_CTX
, "Unsupported alignment");
6065 } else if (retval
!= ERROR_OK
) {
6066 command_print(CMD_CTX
, "Memory read failed");
6070 /* replay on host */
6071 memcpy(read_ref
+ size
+ host_offset
, test_pattern
+ offset
, count
* size
);
6074 int result
= memcmp(read_ref
, read_buf
, host_bufsiz
);
6076 command_print(CMD_CTX
, "Pass in %fs (%0.3f KiB/s)",
6077 duration_elapsed(&bench
),
6078 duration_kbps(&bench
, count
* size
));
6080 command_print(CMD_CTX
, "Compare failed");
6081 binprint(CMD_CTX
, "ref:", read_ref
, host_bufsiz
);
6082 binprint(CMD_CTX
, "buf:", read_buf
, host_bufsiz
);
6095 target_free_working_area(target
, wa
);
6098 num_bytes
= test_size
+ 4 + 4 + 4;
6100 retval
= target_alloc_working_area(target
, num_bytes
, &wa
);
6101 if (retval
!= ERROR_OK
) {
6102 LOG_ERROR("Not enough working area");
6106 test_pattern
= malloc(num_bytes
);
6108 for (size_t i
= 0; i
< num_bytes
; i
++)
6109 test_pattern
[i
] = rand();
6111 for (int host_offset
= 0; host_offset
<= 1; host_offset
++) {
6112 for (int size
= 1; size
<= 4; size
*= 2) {
6113 for (int offset
= 0; offset
< 4; offset
++) {
6114 uint32_t count
= test_size
/ size
;
6115 size_t host_bufsiz
= count
* size
+ host_offset
;
6116 uint8_t *read_ref
= malloc(num_bytes
);
6117 uint8_t *read_buf
= malloc(num_bytes
);
6118 uint8_t *write_buf
= malloc(host_bufsiz
);
6120 for (size_t i
= 0; i
< host_bufsiz
; i
++)
6121 write_buf
[i
] = rand();
6122 command_print_sameline(CMD_CTX
,
6123 "Test write %" PRIu32
" x %d @ %d from %saligned buffer: ", count
,
6124 size
, offset
, host_offset
? "un" : "");
6126 retval
= target_write_memory(target
, wa
->address
, 1, num_bytes
, test_pattern
);
6127 if (retval
!= ERROR_OK
) {
6128 command_print(CMD_CTX
, "Test pattern write failed");
6132 /* replay on host */
6133 memcpy(read_ref
, test_pattern
, num_bytes
);
6134 memcpy(read_ref
+ size
+ offset
, write_buf
+ host_offset
, count
* size
);
6136 struct duration bench
;
6137 duration_start(&bench
);
6139 retval
= target_write_memory(target
, wa
->address
+ size
+ offset
, size
, count
,
6140 write_buf
+ host_offset
);
6142 duration_measure(&bench
);
6144 if (retval
== ERROR_TARGET_UNALIGNED_ACCESS
) {
6145 command_print(CMD_CTX
, "Unsupported alignment");
6147 } else if (retval
!= ERROR_OK
) {
6148 command_print(CMD_CTX
, "Memory write failed");
6153 retval
= target_read_memory(target
, wa
->address
, 1, num_bytes
, read_buf
);
6154 if (retval
!= ERROR_OK
) {
6155 command_print(CMD_CTX
, "Test pattern write failed");
6160 int result
= memcmp(read_ref
, read_buf
, num_bytes
);
6162 command_print(CMD_CTX
, "Pass in %fs (%0.3f KiB/s)",
6163 duration_elapsed(&bench
),
6164 duration_kbps(&bench
, count
* size
));
6166 command_print(CMD_CTX
, "Compare failed");
6167 binprint(CMD_CTX
, "ref:", read_ref
, num_bytes
);
6168 binprint(CMD_CTX
, "buf:", read_buf
, num_bytes
);
6180 target_free_working_area(target
, wa
);
6184 static const struct command_registration target_exec_command_handlers
[] = {
6186 .name
= "fast_load_image",
6187 .handler
= handle_fast_load_image_command
,
6188 .mode
= COMMAND_ANY
,
6189 .help
= "Load image into server memory for later use by "
6190 "fast_load; primarily for profiling",
6191 .usage
= "filename address ['bin'|'ihex'|'elf'|'s19'] "
6192 "[min_address [max_length]]",
6195 .name
= "fast_load",
6196 .handler
= handle_fast_load_command
,
6197 .mode
= COMMAND_EXEC
,
6198 .help
= "loads active fast load image to current target "
6199 "- mainly for profiling purposes",
6204 .handler
= handle_profile_command
,
6205 .mode
= COMMAND_EXEC
,
6206 .usage
= "seconds filename [start end]",
6207 .help
= "profiling samples the CPU PC",
6209 /** @todo don't register virt2phys() unless target supports it */
6211 .name
= "virt2phys",
6212 .handler
= handle_virt2phys_command
,
6213 .mode
= COMMAND_ANY
,
6214 .help
= "translate a virtual address into a physical address",
6215 .usage
= "virtual_address",
6219 .handler
= handle_reg_command
,
6220 .mode
= COMMAND_EXEC
,
6221 .help
= "display (reread from target with \"force\") or set a register; "
6222 "with no arguments, displays all registers and their values",
6223 .usage
= "[(register_number|register_name) [(value|'force')]]",
6227 .handler
= handle_poll_command
,
6228 .mode
= COMMAND_EXEC
,
6229 .help
= "poll target state; or reconfigure background polling",
6230 .usage
= "['on'|'off']",
6233 .name
= "wait_halt",
6234 .handler
= handle_wait_halt_command
,
6235 .mode
= COMMAND_EXEC
,
6236 .help
= "wait up to the specified number of milliseconds "
6237 "(default 5000) for a previously requested halt",
6238 .usage
= "[milliseconds]",
6242 .handler
= handle_halt_command
,
6243 .mode
= COMMAND_EXEC
,
6244 .help
= "request target to halt, then wait up to the specified"
6245 "number of milliseconds (default 5000) for it to complete",
6246 .usage
= "[milliseconds]",
6250 .handler
= handle_resume_command
,
6251 .mode
= COMMAND_EXEC
,
6252 .help
= "resume target execution from current PC or address",
6253 .usage
= "[address]",
6257 .handler
= handle_reset_command
,
6258 .mode
= COMMAND_EXEC
,
6259 .usage
= "[run|halt|init]",
6260 .help
= "Reset all targets into the specified mode."
6261 "Default reset mode is run, if not given.",
6264 .name
= "soft_reset_halt",
6265 .handler
= handle_soft_reset_halt_command
,
6266 .mode
= COMMAND_EXEC
,
6268 .help
= "halt the target and do a soft reset",
6272 .handler
= handle_step_command
,
6273 .mode
= COMMAND_EXEC
,
6274 .help
= "step one instruction from current PC or address",
6275 .usage
= "[address]",
6279 .handler
= handle_md_command
,
6280 .mode
= COMMAND_EXEC
,
6281 .help
= "display memory words",
6282 .usage
= "['phys'] address [count]",
6286 .handler
= handle_md_command
,
6287 .mode
= COMMAND_EXEC
,
6288 .help
= "display memory words",
6289 .usage
= "['phys'] address [count]",
6293 .handler
= handle_md_command
,
6294 .mode
= COMMAND_EXEC
,
6295 .help
= "display memory half-words",
6296 .usage
= "['phys'] address [count]",
6300 .handler
= handle_md_command
,
6301 .mode
= COMMAND_EXEC
,
6302 .help
= "display memory bytes",
6303 .usage
= "['phys'] address [count]",
6307 .handler
= handle_mw_command
,
6308 .mode
= COMMAND_EXEC
,
6309 .help
= "write memory word",
6310 .usage
= "['phys'] address value [count]",
6314 .handler
= handle_mw_command
,
6315 .mode
= COMMAND_EXEC
,
6316 .help
= "write memory word",
6317 .usage
= "['phys'] address value [count]",
6321 .handler
= handle_mw_command
,
6322 .mode
= COMMAND_EXEC
,
6323 .help
= "write memory half-word",
6324 .usage
= "['phys'] address value [count]",
6328 .handler
= handle_mw_command
,
6329 .mode
= COMMAND_EXEC
,
6330 .help
= "write memory byte",
6331 .usage
= "['phys'] address value [count]",
6335 .handler
= handle_bp_command
,
6336 .mode
= COMMAND_EXEC
,
6337 .help
= "list or set hardware or software breakpoint",
6338 .usage
= "<address> [<asid>]<length> ['hw'|'hw_ctx']",
6342 .handler
= handle_rbp_command
,
6343 .mode
= COMMAND_EXEC
,
6344 .help
= "remove breakpoint",
6349 .handler
= handle_wp_command
,
6350 .mode
= COMMAND_EXEC
,
6351 .help
= "list (no params) or create watchpoints",
6352 .usage
= "[address length [('r'|'w'|'a') value [mask]]]",
6356 .handler
= handle_rwp_command
,
6357 .mode
= COMMAND_EXEC
,
6358 .help
= "remove watchpoint",
6362 .name
= "load_image",
6363 .handler
= handle_load_image_command
,
6364 .mode
= COMMAND_EXEC
,
6365 .usage
= "filename address ['bin'|'ihex'|'elf'|'s19'] "
6366 "[min_address] [max_length]",
6369 .name
= "dump_image",
6370 .handler
= handle_dump_image_command
,
6371 .mode
= COMMAND_EXEC
,
6372 .usage
= "filename address size",
6375 .name
= "verify_image_checksum",
6376 .handler
= handle_verify_image_checksum_command
,
6377 .mode
= COMMAND_EXEC
,
6378 .usage
= "filename [offset [type]]",
6381 .name
= "verify_image",
6382 .handler
= handle_verify_image_command
,
6383 .mode
= COMMAND_EXEC
,
6384 .usage
= "filename [offset [type]]",
6387 .name
= "test_image",
6388 .handler
= handle_test_image_command
,
6389 .mode
= COMMAND_EXEC
,
6390 .usage
= "filename [offset [type]]",
6393 .name
= "mem2array",
6394 .mode
= COMMAND_EXEC
,
6395 .jim_handler
= jim_mem2array
,
6396 .help
= "read 8/16/32 bit memory and return as a TCL array "
6397 "for script processing",
6398 .usage
= "arrayname bitwidth address count",
6401 .name
= "array2mem",
6402 .mode
= COMMAND_EXEC
,
6403 .jim_handler
= jim_array2mem
,
6404 .help
= "convert a TCL array to memory locations "
6405 "and write the 8/16/32 bit values",
6406 .usage
= "arrayname bitwidth address count",
6409 .name
= "reset_nag",
6410 .handler
= handle_target_reset_nag
,
6411 .mode
= COMMAND_ANY
,
6412 .help
= "Nag after each reset about options that could have been "
6413 "enabled to improve performance. ",
6414 .usage
= "['enable'|'disable']",
6418 .handler
= handle_ps_command
,
6419 .mode
= COMMAND_EXEC
,
6420 .help
= "list all tasks ",
6424 .name
= "test_mem_access",
6425 .handler
= handle_test_mem_access_command
,
6426 .mode
= COMMAND_EXEC
,
6427 .help
= "Test the target's memory access functions",
6431 COMMAND_REGISTRATION_DONE
6433 static int target_register_user_commands(struct command_context
*cmd_ctx
)
6435 int retval
= ERROR_OK
;
6436 retval
= target_request_register_commands(cmd_ctx
);
6437 if (retval
!= ERROR_OK
)
6440 retval
= trace_register_commands(cmd_ctx
);
6441 if (retval
!= ERROR_OK
)
6445 return register_commands(cmd_ctx
, NULL
, target_exec_command_handlers
);