1 /***************************************************************************
2 * Copyright (C) 2005 by Dominic Rath *
3 * Dominic.Rath@gmx.de *
5 * Copyright (C) 2007-2010 Øyvind Harboe *
6 * oyvind.harboe@zylin.com *
8 * Copyright (C) 2008, Duane Ellis *
9 * openocd@duaneeellis.com *
11 * Copyright (C) 2008 by Spencer Oliver *
12 * spen@spen-soft.co.uk *
14 * Copyright (C) 2008 by Rick Altherr *
15 * kc8apf@kc8apf.net> *
17 * Copyright (C) 2011 by Broadcom Corporation *
18 * Evan Hunter - ehunter@broadcom.com *
20 * Copyright (C) ST-Ericsson SA 2011 *
21 * michel.jaouen@stericsson.com : smp minimum support *
23 * Copyright (C) 2011 Andreas Fritiofson *
24 * andreas.fritiofson@gmail.com *
26 * This program is free software; you can redistribute it and/or modify *
27 * it under the terms of the GNU General Public License as published by *
28 * the Free Software Foundation; either version 2 of the License, or *
29 * (at your option) any later version. *
31 * This program is distributed in the hope that it will be useful, *
32 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
33 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
34 * GNU General Public License for more details. *
36 * You should have received a copy of the GNU General Public License *
37 * along with this program. If not, see <http://www.gnu.org/licenses/>. *
38 ***************************************************************************/
44 #include <helper/time_support.h>
45 #include <jtag/jtag.h>
46 #include <flash/nor/core.h>
49 #include "target_type.h"
50 #include "target_request.h"
51 #include "breakpoints.h"
55 #include "rtos/rtos.h"
56 #include "transport/transport.h"
58 /* default halt wait timeout (ms) */
59 #define DEFAULT_HALT_TIMEOUT 5000
61 static int target_read_buffer_default(struct target
*target
, target_addr_t address
,
62 uint32_t count
, uint8_t *buffer
);
63 static int target_write_buffer_default(struct target
*target
, target_addr_t address
,
64 uint32_t count
, const uint8_t *buffer
);
65 static int target_array2mem(Jim_Interp
*interp
, struct target
*target
,
66 int argc
, Jim_Obj
* const *argv
);
67 static int target_mem2array(Jim_Interp
*interp
, struct target
*target
,
68 int argc
, Jim_Obj
* const *argv
);
69 static int target_register_user_commands(struct command_context
*cmd_ctx
);
70 static int target_get_gdb_fileio_info_default(struct target
*target
,
71 struct gdb_fileio_info
*fileio_info
);
72 static int target_gdb_fileio_end_default(struct target
*target
, int retcode
,
73 int fileio_errno
, bool ctrl_c
);
74 static int target_profiling_default(struct target
*target
, uint32_t *samples
,
75 uint32_t max_num_samples
, uint32_t *num_samples
, uint32_t seconds
);
78 extern struct target_type arm7tdmi_target
;
79 extern struct target_type arm720t_target
;
80 extern struct target_type arm9tdmi_target
;
81 extern struct target_type arm920t_target
;
82 extern struct target_type arm966e_target
;
83 extern struct target_type arm946e_target
;
84 extern struct target_type arm926ejs_target
;
85 extern struct target_type fa526_target
;
86 extern struct target_type feroceon_target
;
87 extern struct target_type dragonite_target
;
88 extern struct target_type xscale_target
;
89 extern struct target_type cortexm_target
;
90 extern struct target_type cortexa_target
;
91 extern struct target_type aarch64_target
;
92 extern struct target_type cortexr4_target
;
93 extern struct target_type arm11_target
;
94 extern struct target_type ls1_sap_target
;
95 extern struct target_type mips_m4k_target
;
96 extern struct target_type avr_target
;
97 extern struct target_type dsp563xx_target
;
98 extern struct target_type dsp5680xx_target
;
99 extern struct target_type testee_target
;
100 extern struct target_type avr32_ap7k_target
;
101 extern struct target_type hla_target
;
102 extern struct target_type nds32_v2_target
;
103 extern struct target_type nds32_v3_target
;
104 extern struct target_type nds32_v3m_target
;
105 extern struct target_type or1k_target
;
106 extern struct target_type quark_x10xx_target
;
107 extern struct target_type quark_d20xx_target
;
108 extern struct target_type stm8_target
;
110 static struct target_type
*target_types
[] = {
147 struct target
*all_targets
;
148 static struct target_event_callback
*target_event_callbacks
;
149 static struct target_timer_callback
*target_timer_callbacks
;
150 LIST_HEAD(target_reset_callback_list
);
151 LIST_HEAD(target_trace_callback_list
);
152 static const int polling_interval
= 100;
154 static const Jim_Nvp nvp_assert
[] = {
155 { .name
= "assert", NVP_ASSERT
},
156 { .name
= "deassert", NVP_DEASSERT
},
157 { .name
= "T", NVP_ASSERT
},
158 { .name
= "F", NVP_DEASSERT
},
159 { .name
= "t", NVP_ASSERT
},
160 { .name
= "f", NVP_DEASSERT
},
161 { .name
= NULL
, .value
= -1 }
164 static const Jim_Nvp nvp_error_target
[] = {
165 { .value
= ERROR_TARGET_INVALID
, .name
= "err-invalid" },
166 { .value
= ERROR_TARGET_INIT_FAILED
, .name
= "err-init-failed" },
167 { .value
= ERROR_TARGET_TIMEOUT
, .name
= "err-timeout" },
168 { .value
= ERROR_TARGET_NOT_HALTED
, .name
= "err-not-halted" },
169 { .value
= ERROR_TARGET_FAILURE
, .name
= "err-failure" },
170 { .value
= ERROR_TARGET_UNALIGNED_ACCESS
, .name
= "err-unaligned-access" },
171 { .value
= ERROR_TARGET_DATA_ABORT
, .name
= "err-data-abort" },
172 { .value
= ERROR_TARGET_RESOURCE_NOT_AVAILABLE
, .name
= "err-resource-not-available" },
173 { .value
= ERROR_TARGET_TRANSLATION_FAULT
, .name
= "err-translation-fault" },
174 { .value
= ERROR_TARGET_NOT_RUNNING
, .name
= "err-not-running" },
175 { .value
= ERROR_TARGET_NOT_EXAMINED
, .name
= "err-not-examined" },
176 { .value
= -1, .name
= NULL
}
179 static const char *target_strerror_safe(int err
)
183 n
= Jim_Nvp_value2name_simple(nvp_error_target
, err
);
190 static const Jim_Nvp nvp_target_event
[] = {
192 { .value
= TARGET_EVENT_GDB_HALT
, .name
= "gdb-halt" },
193 { .value
= TARGET_EVENT_HALTED
, .name
= "halted" },
194 { .value
= TARGET_EVENT_RESUMED
, .name
= "resumed" },
195 { .value
= TARGET_EVENT_RESUME_START
, .name
= "resume-start" },
196 { .value
= TARGET_EVENT_RESUME_END
, .name
= "resume-end" },
198 { .name
= "gdb-start", .value
= TARGET_EVENT_GDB_START
},
199 { .name
= "gdb-end", .value
= TARGET_EVENT_GDB_END
},
201 { .value
= TARGET_EVENT_RESET_START
, .name
= "reset-start" },
202 { .value
= TARGET_EVENT_RESET_ASSERT_PRE
, .name
= "reset-assert-pre" },
203 { .value
= TARGET_EVENT_RESET_ASSERT
, .name
= "reset-assert" },
204 { .value
= TARGET_EVENT_RESET_ASSERT_POST
, .name
= "reset-assert-post" },
205 { .value
= TARGET_EVENT_RESET_DEASSERT_PRE
, .name
= "reset-deassert-pre" },
206 { .value
= TARGET_EVENT_RESET_DEASSERT_POST
, .name
= "reset-deassert-post" },
207 { .value
= TARGET_EVENT_RESET_INIT
, .name
= "reset-init" },
208 { .value
= TARGET_EVENT_RESET_END
, .name
= "reset-end" },
210 { .value
= TARGET_EVENT_EXAMINE_START
, .name
= "examine-start" },
211 { .value
= TARGET_EVENT_EXAMINE_END
, .name
= "examine-end" },
213 { .value
= TARGET_EVENT_DEBUG_HALTED
, .name
= "debug-halted" },
214 { .value
= TARGET_EVENT_DEBUG_RESUMED
, .name
= "debug-resumed" },
216 { .value
= TARGET_EVENT_GDB_ATTACH
, .name
= "gdb-attach" },
217 { .value
= TARGET_EVENT_GDB_DETACH
, .name
= "gdb-detach" },
219 { .value
= TARGET_EVENT_GDB_FLASH_WRITE_START
, .name
= "gdb-flash-write-start" },
220 { .value
= TARGET_EVENT_GDB_FLASH_WRITE_END
, .name
= "gdb-flash-write-end" },
222 { .value
= TARGET_EVENT_GDB_FLASH_ERASE_START
, .name
= "gdb-flash-erase-start" },
223 { .value
= TARGET_EVENT_GDB_FLASH_ERASE_END
, .name
= "gdb-flash-erase-end" },
225 { .value
= TARGET_EVENT_TRACE_CONFIG
, .name
= "trace-config" },
227 { .name
= NULL
, .value
= -1 }
230 static const Jim_Nvp nvp_target_state
[] = {
231 { .name
= "unknown", .value
= TARGET_UNKNOWN
},
232 { .name
= "running", .value
= TARGET_RUNNING
},
233 { .name
= "halted", .value
= TARGET_HALTED
},
234 { .name
= "reset", .value
= TARGET_RESET
},
235 { .name
= "debug-running", .value
= TARGET_DEBUG_RUNNING
},
236 { .name
= NULL
, .value
= -1 },
239 static const Jim_Nvp nvp_target_debug_reason
[] = {
240 { .name
= "debug-request" , .value
= DBG_REASON_DBGRQ
},
241 { .name
= "breakpoint" , .value
= DBG_REASON_BREAKPOINT
},
242 { .name
= "watchpoint" , .value
= DBG_REASON_WATCHPOINT
},
243 { .name
= "watchpoint-and-breakpoint", .value
= DBG_REASON_WPTANDBKPT
},
244 { .name
= "single-step" , .value
= DBG_REASON_SINGLESTEP
},
245 { .name
= "target-not-halted" , .value
= DBG_REASON_NOTHALTED
},
246 { .name
= "program-exit" , .value
= DBG_REASON_EXIT
},
247 { .name
= "undefined" , .value
= DBG_REASON_UNDEFINED
},
248 { .name
= NULL
, .value
= -1 },
251 static const Jim_Nvp nvp_target_endian
[] = {
252 { .name
= "big", .value
= TARGET_BIG_ENDIAN
},
253 { .name
= "little", .value
= TARGET_LITTLE_ENDIAN
},
254 { .name
= "be", .value
= TARGET_BIG_ENDIAN
},
255 { .name
= "le", .value
= TARGET_LITTLE_ENDIAN
},
256 { .name
= NULL
, .value
= -1 },
259 static const Jim_Nvp nvp_reset_modes
[] = {
260 { .name
= "unknown", .value
= RESET_UNKNOWN
},
261 { .name
= "run" , .value
= RESET_RUN
},
262 { .name
= "halt" , .value
= RESET_HALT
},
263 { .name
= "init" , .value
= RESET_INIT
},
264 { .name
= NULL
, .value
= -1 },
267 const char *debug_reason_name(struct target
*t
)
271 cp
= Jim_Nvp_value2name_simple(nvp_target_debug_reason
,
272 t
->debug_reason
)->name
;
274 LOG_ERROR("Invalid debug reason: %d", (int)(t
->debug_reason
));
275 cp
= "(*BUG*unknown*BUG*)";
280 const char *target_state_name(struct target
*t
)
283 cp
= Jim_Nvp_value2name_simple(nvp_target_state
, t
->state
)->name
;
285 LOG_ERROR("Invalid target state: %d", (int)(t
->state
));
286 cp
= "(*BUG*unknown*BUG*)";
289 if (!target_was_examined(t
) && t
->defer_examine
)
290 cp
= "examine deferred";
295 const char *target_event_name(enum target_event event
)
298 cp
= Jim_Nvp_value2name_simple(nvp_target_event
, event
)->name
;
300 LOG_ERROR("Invalid target event: %d", (int)(event
));
301 cp
= "(*BUG*unknown*BUG*)";
306 const char *target_reset_mode_name(enum target_reset_mode reset_mode
)
309 cp
= Jim_Nvp_value2name_simple(nvp_reset_modes
, reset_mode
)->name
;
311 LOG_ERROR("Invalid target reset mode: %d", (int)(reset_mode
));
312 cp
= "(*BUG*unknown*BUG*)";
317 /* determine the number of the new target */
318 static int new_target_number(void)
323 /* number is 0 based */
327 if (x
< t
->target_number
)
328 x
= t
->target_number
;
334 /* read a uint64_t from a buffer in target memory endianness */
335 uint64_t target_buffer_get_u64(struct target
*target
, const uint8_t *buffer
)
337 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
338 return le_to_h_u64(buffer
);
340 return be_to_h_u64(buffer
);
343 /* read a uint32_t from a buffer in target memory endianness */
344 uint32_t target_buffer_get_u32(struct target
*target
, const uint8_t *buffer
)
346 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
347 return le_to_h_u32(buffer
);
349 return be_to_h_u32(buffer
);
352 /* read a uint24_t from a buffer in target memory endianness */
353 uint32_t target_buffer_get_u24(struct target
*target
, const uint8_t *buffer
)
355 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
356 return le_to_h_u24(buffer
);
358 return be_to_h_u24(buffer
);
361 /* read a uint16_t from a buffer in target memory endianness */
362 uint16_t target_buffer_get_u16(struct target
*target
, const uint8_t *buffer
)
364 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
365 return le_to_h_u16(buffer
);
367 return be_to_h_u16(buffer
);
370 /* read a uint8_t from a buffer in target memory endianness */
371 static uint8_t target_buffer_get_u8(struct target
*target
, const uint8_t *buffer
)
373 return *buffer
& 0x0ff;
376 /* write a uint64_t to a buffer in target memory endianness */
377 void target_buffer_set_u64(struct target
*target
, uint8_t *buffer
, uint64_t value
)
379 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
380 h_u64_to_le(buffer
, value
);
382 h_u64_to_be(buffer
, value
);
385 /* write a uint32_t to a buffer in target memory endianness */
386 void target_buffer_set_u32(struct target
*target
, uint8_t *buffer
, uint32_t value
)
388 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
389 h_u32_to_le(buffer
, value
);
391 h_u32_to_be(buffer
, value
);
394 /* write a uint24_t to a buffer in target memory endianness */
395 void target_buffer_set_u24(struct target
*target
, uint8_t *buffer
, uint32_t value
)
397 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
398 h_u24_to_le(buffer
, value
);
400 h_u24_to_be(buffer
, value
);
403 /* write a uint16_t to a buffer in target memory endianness */
404 void target_buffer_set_u16(struct target
*target
, uint8_t *buffer
, uint16_t value
)
406 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
407 h_u16_to_le(buffer
, value
);
409 h_u16_to_be(buffer
, value
);
412 /* write a uint8_t to a buffer in target memory endianness */
413 static void target_buffer_set_u8(struct target
*target
, uint8_t *buffer
, uint8_t value
)
418 /* write a uint64_t array to a buffer in target memory endianness */
419 void target_buffer_get_u64_array(struct target
*target
, const uint8_t *buffer
, uint32_t count
, uint64_t *dstbuf
)
422 for (i
= 0; i
< count
; i
++)
423 dstbuf
[i
] = target_buffer_get_u64(target
, &buffer
[i
* 8]);
426 /* write a uint32_t array to a buffer in target memory endianness */
427 void target_buffer_get_u32_array(struct target
*target
, const uint8_t *buffer
, uint32_t count
, uint32_t *dstbuf
)
430 for (i
= 0; i
< count
; i
++)
431 dstbuf
[i
] = target_buffer_get_u32(target
, &buffer
[i
* 4]);
434 /* write a uint16_t array to a buffer in target memory endianness */
435 void target_buffer_get_u16_array(struct target
*target
, const uint8_t *buffer
, uint32_t count
, uint16_t *dstbuf
)
438 for (i
= 0; i
< count
; i
++)
439 dstbuf
[i
] = target_buffer_get_u16(target
, &buffer
[i
* 2]);
442 /* write a uint64_t array to a buffer in target memory endianness */
443 void target_buffer_set_u64_array(struct target
*target
, uint8_t *buffer
, uint32_t count
, const uint64_t *srcbuf
)
446 for (i
= 0; i
< count
; i
++)
447 target_buffer_set_u64(target
, &buffer
[i
* 8], srcbuf
[i
]);
450 /* write a uint32_t array to a buffer in target memory endianness */
451 void target_buffer_set_u32_array(struct target
*target
, uint8_t *buffer
, uint32_t count
, const uint32_t *srcbuf
)
454 for (i
= 0; i
< count
; i
++)
455 target_buffer_set_u32(target
, &buffer
[i
* 4], srcbuf
[i
]);
458 /* write a uint16_t array to a buffer in target memory endianness */
459 void target_buffer_set_u16_array(struct target
*target
, uint8_t *buffer
, uint32_t count
, const uint16_t *srcbuf
)
462 for (i
= 0; i
< count
; i
++)
463 target_buffer_set_u16(target
, &buffer
[i
* 2], srcbuf
[i
]);
466 /* return a pointer to a configured target; id is name or number */
467 struct target
*get_target(const char *id
)
469 struct target
*target
;
471 /* try as tcltarget name */
472 for (target
= all_targets
; target
; target
= target
->next
) {
473 if (target_name(target
) == NULL
)
475 if (strcmp(id
, target_name(target
)) == 0)
479 /* It's OK to remove this fallback sometime after August 2010 or so */
481 /* no match, try as number */
483 if (parse_uint(id
, &num
) != ERROR_OK
)
486 for (target
= all_targets
; target
; target
= target
->next
) {
487 if (target
->target_number
== (int)num
) {
488 LOG_WARNING("use '%s' as target identifier, not '%u'",
489 target_name(target
), num
);
497 /* returns a pointer to the n-th configured target */
498 struct target
*get_target_by_num(int num
)
500 struct target
*target
= all_targets
;
503 if (target
->target_number
== num
)
505 target
= target
->next
;
511 struct target
*get_current_target(struct command_context
*cmd_ctx
)
513 struct target
*target
= get_target_by_num(cmd_ctx
->current_target
);
515 if (target
== NULL
) {
516 LOG_ERROR("BUG: current_target out of bounds");
523 int target_poll(struct target
*target
)
527 /* We can't poll until after examine */
528 if (!target_was_examined(target
)) {
529 /* Fail silently lest we pollute the log */
533 retval
= target
->type
->poll(target
);
534 if (retval
!= ERROR_OK
)
537 if (target
->halt_issued
) {
538 if (target
->state
== TARGET_HALTED
)
539 target
->halt_issued
= false;
541 int64_t t
= timeval_ms() - target
->halt_issued_time
;
542 if (t
> DEFAULT_HALT_TIMEOUT
) {
543 target
->halt_issued
= false;
544 LOG_INFO("Halt timed out, wake up GDB.");
545 target_call_event_callbacks(target
, TARGET_EVENT_GDB_HALT
);
553 int target_halt(struct target
*target
)
556 /* We can't poll until after examine */
557 if (!target_was_examined(target
)) {
558 LOG_ERROR("Target not examined yet");
562 retval
= target
->type
->halt(target
);
563 if (retval
!= ERROR_OK
)
566 target
->halt_issued
= true;
567 target
->halt_issued_time
= timeval_ms();
573 * Make the target (re)start executing using its saved execution
574 * context (possibly with some modifications).
576 * @param target Which target should start executing.
577 * @param current True to use the target's saved program counter instead
578 * of the address parameter
579 * @param address Optionally used as the program counter.
580 * @param handle_breakpoints True iff breakpoints at the resumption PC
581 * should be skipped. (For example, maybe execution was stopped by
582 * such a breakpoint, in which case it would be counterprodutive to
584 * @param debug_execution False if all working areas allocated by OpenOCD
585 * should be released and/or restored to their original contents.
586 * (This would for example be true to run some downloaded "helper"
587 * algorithm code, which resides in one such working buffer and uses
588 * another for data storage.)
590 * @todo Resolve the ambiguity about what the "debug_execution" flag
591 * signifies. For example, Target implementations don't agree on how
592 * it relates to invalidation of the register cache, or to whether
593 * breakpoints and watchpoints should be enabled. (It would seem wrong
594 * to enable breakpoints when running downloaded "helper" algorithms
595 * (debug_execution true), since the breakpoints would be set to match
596 * target firmware being debugged, not the helper algorithm.... and
597 * enabling them could cause such helpers to malfunction (for example,
598 * by overwriting data with a breakpoint instruction. On the other
599 * hand the infrastructure for running such helpers might use this
600 * procedure but rely on hardware breakpoint to detect termination.)
602 int target_resume(struct target
*target
, int current
, target_addr_t address
,
603 int handle_breakpoints
, int debug_execution
)
607 /* We can't poll until after examine */
608 if (!target_was_examined(target
)) {
609 LOG_ERROR("Target not examined yet");
613 target_call_event_callbacks(target
, TARGET_EVENT_RESUME_START
);
615 /* note that resume *must* be asynchronous. The CPU can halt before
616 * we poll. The CPU can even halt at the current PC as a result of
617 * a software breakpoint being inserted by (a bug?) the application.
619 retval
= target
->type
->resume(target
, current
, address
, handle_breakpoints
, debug_execution
);
620 if (retval
!= ERROR_OK
)
623 target_call_event_callbacks(target
, TARGET_EVENT_RESUME_END
);
628 static int target_process_reset(struct command_context
*cmd_ctx
, enum target_reset_mode reset_mode
)
633 n
= Jim_Nvp_value2name_simple(nvp_reset_modes
, reset_mode
);
634 if (n
->name
== NULL
) {
635 LOG_ERROR("invalid reset mode");
639 struct target
*target
;
640 for (target
= all_targets
; target
; target
= target
->next
)
641 target_call_reset_callbacks(target
, reset_mode
);
643 /* disable polling during reset to make reset event scripts
644 * more predictable, i.e. dr/irscan & pathmove in events will
645 * not have JTAG operations injected into the middle of a sequence.
647 bool save_poll
= jtag_poll_get_enabled();
649 jtag_poll_set_enabled(false);
651 sprintf(buf
, "ocd_process_reset %s", n
->name
);
652 retval
= Jim_Eval(cmd_ctx
->interp
, buf
);
654 jtag_poll_set_enabled(save_poll
);
656 if (retval
!= JIM_OK
) {
657 Jim_MakeErrorMessage(cmd_ctx
->interp
);
658 command_print(NULL
, "%s\n", Jim_GetString(Jim_GetResult(cmd_ctx
->interp
), NULL
));
662 /* We want any events to be processed before the prompt */
663 retval
= target_call_timer_callbacks_now();
665 for (target
= all_targets
; target
; target
= target
->next
) {
666 target
->type
->check_reset(target
);
667 target
->running_alg
= false;
673 static int identity_virt2phys(struct target
*target
,
674 target_addr_t
virtual, target_addr_t
*physical
)
680 static int no_mmu(struct target
*target
, int *enabled
)
686 static int default_examine(struct target
*target
)
688 target_set_examined(target
);
692 /* no check by default */
693 static int default_check_reset(struct target
*target
)
698 int target_examine_one(struct target
*target
)
700 target_call_event_callbacks(target
, TARGET_EVENT_EXAMINE_START
);
702 int retval
= target
->type
->examine(target
);
703 if (retval
!= ERROR_OK
)
706 target_call_event_callbacks(target
, TARGET_EVENT_EXAMINE_END
);
711 static int jtag_enable_callback(enum jtag_event event
, void *priv
)
713 struct target
*target
= priv
;
715 if (event
!= JTAG_TAP_EVENT_ENABLE
|| !target
->tap
->enabled
)
718 jtag_unregister_event_callback(jtag_enable_callback
, target
);
720 return target_examine_one(target
);
723 /* Targets that correctly implement init + examine, i.e.
724 * no communication with target during init:
728 int target_examine(void)
730 int retval
= ERROR_OK
;
731 struct target
*target
;
733 for (target
= all_targets
; target
; target
= target
->next
) {
734 /* defer examination, but don't skip it */
735 if (!target
->tap
->enabled
) {
736 jtag_register_event_callback(jtag_enable_callback
,
741 if (target
->defer_examine
)
744 retval
= target_examine_one(target
);
745 if (retval
!= ERROR_OK
)
751 const char *target_type_name(struct target
*target
)
753 return target
->type
->name
;
756 static int target_soft_reset_halt(struct target
*target
)
758 if (!target_was_examined(target
)) {
759 LOG_ERROR("Target not examined yet");
762 if (!target
->type
->soft_reset_halt
) {
763 LOG_ERROR("Target %s does not support soft_reset_halt",
764 target_name(target
));
767 return target
->type
->soft_reset_halt(target
);
771 * Downloads a target-specific native code algorithm to the target,
772 * and executes it. * Note that some targets may need to set up, enable,
773 * and tear down a breakpoint (hard or * soft) to detect algorithm
774 * termination, while others may support lower overhead schemes where
775 * soft breakpoints embedded in the algorithm automatically terminate the
778 * @param target used to run the algorithm
779 * @param arch_info target-specific description of the algorithm.
781 int target_run_algorithm(struct target
*target
,
782 int num_mem_params
, struct mem_param
*mem_params
,
783 int num_reg_params
, struct reg_param
*reg_param
,
784 uint32_t entry_point
, uint32_t exit_point
,
785 int timeout_ms
, void *arch_info
)
787 int retval
= ERROR_FAIL
;
789 if (!target_was_examined(target
)) {
790 LOG_ERROR("Target not examined yet");
793 if (!target
->type
->run_algorithm
) {
794 LOG_ERROR("Target type '%s' does not support %s",
795 target_type_name(target
), __func__
);
799 target
->running_alg
= true;
800 retval
= target
->type
->run_algorithm(target
,
801 num_mem_params
, mem_params
,
802 num_reg_params
, reg_param
,
803 entry_point
, exit_point
, timeout_ms
, arch_info
);
804 target
->running_alg
= false;
811 * Downloads a target-specific native code algorithm to the target,
812 * executes and leaves it running.
814 * @param target used to run the algorithm
815 * @param arch_info target-specific description of the algorithm.
817 int target_start_algorithm(struct target
*target
,
818 int num_mem_params
, struct mem_param
*mem_params
,
819 int num_reg_params
, struct reg_param
*reg_params
,
820 uint32_t entry_point
, uint32_t exit_point
,
823 int retval
= ERROR_FAIL
;
825 if (!target_was_examined(target
)) {
826 LOG_ERROR("Target not examined yet");
829 if (!target
->type
->start_algorithm
) {
830 LOG_ERROR("Target type '%s' does not support %s",
831 target_type_name(target
), __func__
);
834 if (target
->running_alg
) {
835 LOG_ERROR("Target is already running an algorithm");
839 target
->running_alg
= true;
840 retval
= target
->type
->start_algorithm(target
,
841 num_mem_params
, mem_params
,
842 num_reg_params
, reg_params
,
843 entry_point
, exit_point
, arch_info
);
850 * Waits for an algorithm started with target_start_algorithm() to complete.
852 * @param target used to run the algorithm
853 * @param arch_info target-specific description of the algorithm.
855 int target_wait_algorithm(struct target
*target
,
856 int num_mem_params
, struct mem_param
*mem_params
,
857 int num_reg_params
, struct reg_param
*reg_params
,
858 uint32_t exit_point
, int timeout_ms
,
861 int retval
= ERROR_FAIL
;
863 if (!target
->type
->wait_algorithm
) {
864 LOG_ERROR("Target type '%s' does not support %s",
865 target_type_name(target
), __func__
);
868 if (!target
->running_alg
) {
869 LOG_ERROR("Target is not running an algorithm");
873 retval
= target
->type
->wait_algorithm(target
,
874 num_mem_params
, mem_params
,
875 num_reg_params
, reg_params
,
876 exit_point
, timeout_ms
, arch_info
);
877 if (retval
!= ERROR_TARGET_TIMEOUT
)
878 target
->running_alg
= false;
885 * Executes a target-specific native code algorithm in the target.
886 * It differs from target_run_algorithm in that the algorithm is asynchronous.
887 * Because of this it requires an compliant algorithm:
888 * see contrib/loaders/flash/stm32f1x.S for example.
890 * @param target used to run the algorithm
893 int target_run_flash_async_algorithm(struct target
*target
,
894 const uint8_t *buffer
, uint32_t count
, int block_size
,
895 int num_mem_params
, struct mem_param
*mem_params
,
896 int num_reg_params
, struct reg_param
*reg_params
,
897 uint32_t buffer_start
, uint32_t buffer_size
,
898 uint32_t entry_point
, uint32_t exit_point
, void *arch_info
)
903 const uint8_t *buffer_orig
= buffer
;
905 /* Set up working area. First word is write pointer, second word is read pointer,
906 * rest is fifo data area. */
907 uint32_t wp_addr
= buffer_start
;
908 uint32_t rp_addr
= buffer_start
+ 4;
909 uint32_t fifo_start_addr
= buffer_start
+ 8;
910 uint32_t fifo_end_addr
= buffer_start
+ buffer_size
;
912 uint32_t wp
= fifo_start_addr
;
913 uint32_t rp
= fifo_start_addr
;
915 /* validate block_size is 2^n */
916 assert(!block_size
|| !(block_size
& (block_size
- 1)));
918 retval
= target_write_u32(target
, wp_addr
, wp
);
919 if (retval
!= ERROR_OK
)
921 retval
= target_write_u32(target
, rp_addr
, rp
);
922 if (retval
!= ERROR_OK
)
925 /* Start up algorithm on target and let it idle while writing the first chunk */
926 retval
= target_start_algorithm(target
, num_mem_params
, mem_params
,
927 num_reg_params
, reg_params
,
932 if (retval
!= ERROR_OK
) {
933 LOG_ERROR("error starting target flash write algorithm");
939 retval
= target_read_u32(target
, rp_addr
, &rp
);
940 if (retval
!= ERROR_OK
) {
941 LOG_ERROR("failed to get read pointer");
945 LOG_DEBUG("offs 0x%zx count 0x%" PRIx32
" wp 0x%" PRIx32
" rp 0x%" PRIx32
,
946 (size_t) (buffer
- buffer_orig
), count
, wp
, rp
);
949 LOG_ERROR("flash write algorithm aborted by target");
950 retval
= ERROR_FLASH_OPERATION_FAILED
;
954 if (((rp
- fifo_start_addr
) & (block_size
- 1)) || rp
< fifo_start_addr
|| rp
>= fifo_end_addr
) {
955 LOG_ERROR("corrupted fifo read pointer 0x%" PRIx32
, rp
);
959 /* Count the number of bytes available in the fifo without
960 * crossing the wrap around. Make sure to not fill it completely,
961 * because that would make wp == rp and that's the empty condition. */
962 uint32_t thisrun_bytes
;
964 thisrun_bytes
= rp
- wp
- block_size
;
965 else if (rp
> fifo_start_addr
)
966 thisrun_bytes
= fifo_end_addr
- wp
;
968 thisrun_bytes
= fifo_end_addr
- wp
- block_size
;
970 if (thisrun_bytes
== 0) {
971 /* Throttle polling a bit if transfer is (much) faster than flash
972 * programming. The exact delay shouldn't matter as long as it's
973 * less than buffer size / flash speed. This is very unlikely to
974 * run when using high latency connections such as USB. */
977 /* to stop an infinite loop on some targets check and increment a timeout
978 * this issue was observed on a stellaris using the new ICDI interface */
979 if (timeout
++ >= 500) {
980 LOG_ERROR("timeout waiting for algorithm, a target reset is recommended");
981 return ERROR_FLASH_OPERATION_FAILED
;
986 /* reset our timeout */
989 /* Limit to the amount of data we actually want to write */
990 if (thisrun_bytes
> count
* block_size
)
991 thisrun_bytes
= count
* block_size
;
993 /* Write data to fifo */
994 retval
= target_write_buffer(target
, wp
, thisrun_bytes
, buffer
);
995 if (retval
!= ERROR_OK
)
998 /* Update counters and wrap write pointer */
999 buffer
+= thisrun_bytes
;
1000 count
-= thisrun_bytes
/ block_size
;
1001 wp
+= thisrun_bytes
;
1002 if (wp
>= fifo_end_addr
)
1003 wp
= fifo_start_addr
;
1005 /* Store updated write pointer to target */
1006 retval
= target_write_u32(target
, wp_addr
, wp
);
1007 if (retval
!= ERROR_OK
)
1011 if (retval
!= ERROR_OK
) {
1012 /* abort flash write algorithm on target */
1013 target_write_u32(target
, wp_addr
, 0);
1016 int retval2
= target_wait_algorithm(target
, num_mem_params
, mem_params
,
1017 num_reg_params
, reg_params
,
1022 if (retval2
!= ERROR_OK
) {
1023 LOG_ERROR("error waiting for target flash write algorithm");
1027 if (retval
== ERROR_OK
) {
1028 /* check if algorithm set rp = 0 after fifo writer loop finished */
1029 retval
= target_read_u32(target
, rp_addr
, &rp
);
1030 if (retval
== ERROR_OK
&& rp
== 0) {
1031 LOG_ERROR("flash write algorithm aborted by target");
1032 retval
= ERROR_FLASH_OPERATION_FAILED
;
1039 int target_read_memory(struct target
*target
,
1040 target_addr_t address
, uint32_t size
, uint32_t count
, uint8_t *buffer
)
1042 if (!target_was_examined(target
)) {
1043 LOG_ERROR("Target not examined yet");
1046 if (!target
->type
->read_memory
) {
1047 LOG_ERROR("Target %s doesn't support read_memory", target_name(target
));
1050 return target
->type
->read_memory(target
, address
, size
, count
, buffer
);
1053 int target_read_phys_memory(struct target
*target
,
1054 target_addr_t address
, uint32_t size
, uint32_t count
, uint8_t *buffer
)
1056 if (!target_was_examined(target
)) {
1057 LOG_ERROR("Target not examined yet");
1060 if (!target
->type
->read_phys_memory
) {
1061 LOG_ERROR("Target %s doesn't support read_phys_memory", target_name(target
));
1064 return target
->type
->read_phys_memory(target
, address
, size
, count
, buffer
);
1067 int target_write_memory(struct target
*target
,
1068 target_addr_t address
, uint32_t size
, uint32_t count
, const uint8_t *buffer
)
1070 if (!target_was_examined(target
)) {
1071 LOG_ERROR("Target not examined yet");
1074 if (!target
->type
->write_memory
) {
1075 LOG_ERROR("Target %s doesn't support write_memory", target_name(target
));
1078 return target
->type
->write_memory(target
, address
, size
, count
, buffer
);
1081 int target_write_phys_memory(struct target
*target
,
1082 target_addr_t address
, uint32_t size
, uint32_t count
, const uint8_t *buffer
)
1084 if (!target_was_examined(target
)) {
1085 LOG_ERROR("Target not examined yet");
1088 if (!target
->type
->write_phys_memory
) {
1089 LOG_ERROR("Target %s doesn't support write_phys_memory", target_name(target
));
1092 return target
->type
->write_phys_memory(target
, address
, size
, count
, buffer
);
1095 int target_add_breakpoint(struct target
*target
,
1096 struct breakpoint
*breakpoint
)
1098 if ((target
->state
!= TARGET_HALTED
) && (breakpoint
->type
!= BKPT_HARD
)) {
1099 LOG_WARNING("target %s is not halted (add breakpoint)", target_name(target
));
1100 return ERROR_TARGET_NOT_HALTED
;
1102 return target
->type
->add_breakpoint(target
, breakpoint
);
1105 int target_add_context_breakpoint(struct target
*target
,
1106 struct breakpoint
*breakpoint
)
1108 if (target
->state
!= TARGET_HALTED
) {
1109 LOG_WARNING("target %s is not halted (add context breakpoint)", target_name(target
));
1110 return ERROR_TARGET_NOT_HALTED
;
1112 return target
->type
->add_context_breakpoint(target
, breakpoint
);
1115 int target_add_hybrid_breakpoint(struct target
*target
,
1116 struct breakpoint
*breakpoint
)
1118 if (target
->state
!= TARGET_HALTED
) {
1119 LOG_WARNING("target %s is not halted (add hybrid breakpoint)", target_name(target
));
1120 return ERROR_TARGET_NOT_HALTED
;
1122 return target
->type
->add_hybrid_breakpoint(target
, breakpoint
);
1125 int target_remove_breakpoint(struct target
*target
,
1126 struct breakpoint
*breakpoint
)
1128 return target
->type
->remove_breakpoint(target
, breakpoint
);
1131 int target_add_watchpoint(struct target
*target
,
1132 struct watchpoint
*watchpoint
)
1134 if (target
->state
!= TARGET_HALTED
) {
1135 LOG_WARNING("target %s is not halted (add watchpoint)", target_name(target
));
1136 return ERROR_TARGET_NOT_HALTED
;
1138 return target
->type
->add_watchpoint(target
, watchpoint
);
1140 int target_remove_watchpoint(struct target
*target
,
1141 struct watchpoint
*watchpoint
)
1143 return target
->type
->remove_watchpoint(target
, watchpoint
);
1145 int target_hit_watchpoint(struct target
*target
,
1146 struct watchpoint
**hit_watchpoint
)
1148 if (target
->state
!= TARGET_HALTED
) {
1149 LOG_WARNING("target %s is not halted (hit watchpoint)", target
->cmd_name
);
1150 return ERROR_TARGET_NOT_HALTED
;
1153 if (target
->type
->hit_watchpoint
== NULL
) {
1154 /* For backward compatible, if hit_watchpoint is not implemented,
1155 * return ERROR_FAIL such that gdb_server will not take the nonsense
1160 return target
->type
->hit_watchpoint(target
, hit_watchpoint
);
1163 int target_get_gdb_reg_list(struct target
*target
,
1164 struct reg
**reg_list
[], int *reg_list_size
,
1165 enum target_register_class reg_class
)
1167 return target
->type
->get_gdb_reg_list(target
, reg_list
, reg_list_size
, reg_class
);
1169 int target_step(struct target
*target
,
1170 int current
, target_addr_t address
, int handle_breakpoints
)
1172 return target
->type
->step(target
, current
, address
, handle_breakpoints
);
1175 int target_get_gdb_fileio_info(struct target
*target
, struct gdb_fileio_info
*fileio_info
)
1177 if (target
->state
!= TARGET_HALTED
) {
1178 LOG_WARNING("target %s is not halted (gdb fileio)", target
->cmd_name
);
1179 return ERROR_TARGET_NOT_HALTED
;
1181 return target
->type
->get_gdb_fileio_info(target
, fileio_info
);
1184 int target_gdb_fileio_end(struct target
*target
, int retcode
, int fileio_errno
, bool ctrl_c
)
1186 if (target
->state
!= TARGET_HALTED
) {
1187 LOG_WARNING("target %s is not halted (gdb fileio end)", target
->cmd_name
);
1188 return ERROR_TARGET_NOT_HALTED
;
1190 return target
->type
->gdb_fileio_end(target
, retcode
, fileio_errno
, ctrl_c
);
1193 int target_profiling(struct target
*target
, uint32_t *samples
,
1194 uint32_t max_num_samples
, uint32_t *num_samples
, uint32_t seconds
)
1196 if (target
->state
!= TARGET_HALTED
) {
1197 LOG_WARNING("target %s is not halted (profiling)", target
->cmd_name
);
1198 return ERROR_TARGET_NOT_HALTED
;
1200 return target
->type
->profiling(target
, samples
, max_num_samples
,
1201 num_samples
, seconds
);
1205 * Reset the @c examined flag for the given target.
1206 * Pure paranoia -- targets are zeroed on allocation.
1208 static void target_reset_examined(struct target
*target
)
1210 target
->examined
= false;
1213 static int handle_target(void *priv
);
1215 static int target_init_one(struct command_context
*cmd_ctx
,
1216 struct target
*target
)
1218 target_reset_examined(target
);
1220 struct target_type
*type
= target
->type
;
1221 if (type
->examine
== NULL
)
1222 type
->examine
= default_examine
;
1224 if (type
->check_reset
== NULL
)
1225 type
->check_reset
= default_check_reset
;
1227 assert(type
->init_target
!= NULL
);
1229 int retval
= type
->init_target(cmd_ctx
, target
);
1230 if (ERROR_OK
!= retval
) {
1231 LOG_ERROR("target '%s' init failed", target_name(target
));
1235 /* Sanity-check MMU support ... stub in what we must, to help
1236 * implement it in stages, but warn if we need to do so.
1239 if (type
->virt2phys
== NULL
) {
1240 LOG_ERROR("type '%s' is missing virt2phys", type
->name
);
1241 type
->virt2phys
= identity_virt2phys
;
1244 /* Make sure no-MMU targets all behave the same: make no
1245 * distinction between physical and virtual addresses, and
1246 * ensure that virt2phys() is always an identity mapping.
1248 if (type
->write_phys_memory
|| type
->read_phys_memory
|| type
->virt2phys
)
1249 LOG_WARNING("type '%s' has bad MMU hooks", type
->name
);
1252 type
->write_phys_memory
= type
->write_memory
;
1253 type
->read_phys_memory
= type
->read_memory
;
1254 type
->virt2phys
= identity_virt2phys
;
1257 if (target
->type
->read_buffer
== NULL
)
1258 target
->type
->read_buffer
= target_read_buffer_default
;
1260 if (target
->type
->write_buffer
== NULL
)
1261 target
->type
->write_buffer
= target_write_buffer_default
;
1263 if (target
->type
->get_gdb_fileio_info
== NULL
)
1264 target
->type
->get_gdb_fileio_info
= target_get_gdb_fileio_info_default
;
1266 if (target
->type
->gdb_fileio_end
== NULL
)
1267 target
->type
->gdb_fileio_end
= target_gdb_fileio_end_default
;
1269 if (target
->type
->profiling
== NULL
)
1270 target
->type
->profiling
= target_profiling_default
;
1275 static int target_init(struct command_context
*cmd_ctx
)
1277 struct target
*target
;
1280 for (target
= all_targets
; target
; target
= target
->next
) {
1281 retval
= target_init_one(cmd_ctx
, target
);
1282 if (ERROR_OK
!= retval
)
1289 retval
= target_register_user_commands(cmd_ctx
);
1290 if (ERROR_OK
!= retval
)
1293 retval
= target_register_timer_callback(&handle_target
,
1294 polling_interval
, 1, cmd_ctx
->interp
);
1295 if (ERROR_OK
!= retval
)
1301 COMMAND_HANDLER(handle_target_init_command
)
1306 return ERROR_COMMAND_SYNTAX_ERROR
;
1308 static bool target_initialized
;
1309 if (target_initialized
) {
1310 LOG_INFO("'target init' has already been called");
1313 target_initialized
= true;
1315 retval
= command_run_line(CMD_CTX
, "init_targets");
1316 if (ERROR_OK
!= retval
)
1319 retval
= command_run_line(CMD_CTX
, "init_target_events");
1320 if (ERROR_OK
!= retval
)
1323 retval
= command_run_line(CMD_CTX
, "init_board");
1324 if (ERROR_OK
!= retval
)
1327 LOG_DEBUG("Initializing targets...");
1328 return target_init(CMD_CTX
);
1331 int target_register_event_callback(int (*callback
)(struct target
*target
,
1332 enum target_event event
, void *priv
), void *priv
)
1334 struct target_event_callback
**callbacks_p
= &target_event_callbacks
;
1336 if (callback
== NULL
)
1337 return ERROR_COMMAND_SYNTAX_ERROR
;
1340 while ((*callbacks_p
)->next
)
1341 callbacks_p
= &((*callbacks_p
)->next
);
1342 callbacks_p
= &((*callbacks_p
)->next
);
1345 (*callbacks_p
) = malloc(sizeof(struct target_event_callback
));
1346 (*callbacks_p
)->callback
= callback
;
1347 (*callbacks_p
)->priv
= priv
;
1348 (*callbacks_p
)->next
= NULL
;
1353 int target_register_reset_callback(int (*callback
)(struct target
*target
,
1354 enum target_reset_mode reset_mode
, void *priv
), void *priv
)
1356 struct target_reset_callback
*entry
;
1358 if (callback
== NULL
)
1359 return ERROR_COMMAND_SYNTAX_ERROR
;
1361 entry
= malloc(sizeof(struct target_reset_callback
));
1362 if (entry
== NULL
) {
1363 LOG_ERROR("error allocating buffer for reset callback entry");
1364 return ERROR_COMMAND_SYNTAX_ERROR
;
1367 entry
->callback
= callback
;
1369 list_add(&entry
->list
, &target_reset_callback_list
);
1375 int target_register_trace_callback(int (*callback
)(struct target
*target
,
1376 size_t len
, uint8_t *data
, void *priv
), void *priv
)
1378 struct target_trace_callback
*entry
;
1380 if (callback
== NULL
)
1381 return ERROR_COMMAND_SYNTAX_ERROR
;
1383 entry
= malloc(sizeof(struct target_trace_callback
));
1384 if (entry
== NULL
) {
1385 LOG_ERROR("error allocating buffer for trace callback entry");
1386 return ERROR_COMMAND_SYNTAX_ERROR
;
1389 entry
->callback
= callback
;
1391 list_add(&entry
->list
, &target_trace_callback_list
);
1397 int target_register_timer_callback(int (*callback
)(void *priv
), int time_ms
, int periodic
, void *priv
)
1399 struct target_timer_callback
**callbacks_p
= &target_timer_callbacks
;
1402 if (callback
== NULL
)
1403 return ERROR_COMMAND_SYNTAX_ERROR
;
1406 while ((*callbacks_p
)->next
)
1407 callbacks_p
= &((*callbacks_p
)->next
);
1408 callbacks_p
= &((*callbacks_p
)->next
);
1411 (*callbacks_p
) = malloc(sizeof(struct target_timer_callback
));
1412 (*callbacks_p
)->callback
= callback
;
1413 (*callbacks_p
)->periodic
= periodic
;
1414 (*callbacks_p
)->time_ms
= time_ms
;
1415 (*callbacks_p
)->removed
= false;
1417 gettimeofday(&now
, NULL
);
1418 (*callbacks_p
)->when
.tv_usec
= now
.tv_usec
+ (time_ms
% 1000) * 1000;
1419 time_ms
-= (time_ms
% 1000);
1420 (*callbacks_p
)->when
.tv_sec
= now
.tv_sec
+ (time_ms
/ 1000);
1421 if ((*callbacks_p
)->when
.tv_usec
> 1000000) {
1422 (*callbacks_p
)->when
.tv_usec
= (*callbacks_p
)->when
.tv_usec
- 1000000;
1423 (*callbacks_p
)->when
.tv_sec
+= 1;
1426 (*callbacks_p
)->priv
= priv
;
1427 (*callbacks_p
)->next
= NULL
;
1432 int target_unregister_event_callback(int (*callback
)(struct target
*target
,
1433 enum target_event event
, void *priv
), void *priv
)
1435 struct target_event_callback
**p
= &target_event_callbacks
;
1436 struct target_event_callback
*c
= target_event_callbacks
;
1438 if (callback
== NULL
)
1439 return ERROR_COMMAND_SYNTAX_ERROR
;
1442 struct target_event_callback
*next
= c
->next
;
1443 if ((c
->callback
== callback
) && (c
->priv
== priv
)) {
1455 int target_unregister_reset_callback(int (*callback
)(struct target
*target
,
1456 enum target_reset_mode reset_mode
, void *priv
), void *priv
)
1458 struct target_reset_callback
*entry
;
1460 if (callback
== NULL
)
1461 return ERROR_COMMAND_SYNTAX_ERROR
;
1463 list_for_each_entry(entry
, &target_reset_callback_list
, list
) {
1464 if (entry
->callback
== callback
&& entry
->priv
== priv
) {
1465 list_del(&entry
->list
);
1474 int target_unregister_trace_callback(int (*callback
)(struct target
*target
,
1475 size_t len
, uint8_t *data
, void *priv
), void *priv
)
1477 struct target_trace_callback
*entry
;
1479 if (callback
== NULL
)
1480 return ERROR_COMMAND_SYNTAX_ERROR
;
1482 list_for_each_entry(entry
, &target_trace_callback_list
, list
) {
1483 if (entry
->callback
== callback
&& entry
->priv
== priv
) {
1484 list_del(&entry
->list
);
1493 int target_unregister_timer_callback(int (*callback
)(void *priv
), void *priv
)
1495 if (callback
== NULL
)
1496 return ERROR_COMMAND_SYNTAX_ERROR
;
1498 for (struct target_timer_callback
*c
= target_timer_callbacks
;
1500 if ((c
->callback
== callback
) && (c
->priv
== priv
)) {
1509 int target_call_event_callbacks(struct target
*target
, enum target_event event
)
1511 struct target_event_callback
*callback
= target_event_callbacks
;
1512 struct target_event_callback
*next_callback
;
1514 if (event
== TARGET_EVENT_HALTED
) {
1515 /* execute early halted first */
1516 target_call_event_callbacks(target
, TARGET_EVENT_GDB_HALT
);
1519 LOG_DEBUG("target event %i (%s)", event
,
1520 Jim_Nvp_value2name_simple(nvp_target_event
, event
)->name
);
1522 target_handle_event(target
, event
);
1525 next_callback
= callback
->next
;
1526 callback
->callback(target
, event
, callback
->priv
);
1527 callback
= next_callback
;
1533 int target_call_reset_callbacks(struct target
*target
, enum target_reset_mode reset_mode
)
1535 struct target_reset_callback
*callback
;
1537 LOG_DEBUG("target reset %i (%s)", reset_mode
,
1538 Jim_Nvp_value2name_simple(nvp_reset_modes
, reset_mode
)->name
);
1540 list_for_each_entry(callback
, &target_reset_callback_list
, list
)
1541 callback
->callback(target
, reset_mode
, callback
->priv
);
1546 int target_call_trace_callbacks(struct target
*target
, size_t len
, uint8_t *data
)
1548 struct target_trace_callback
*callback
;
1550 list_for_each_entry(callback
, &target_trace_callback_list
, list
)
1551 callback
->callback(target
, len
, data
, callback
->priv
);
1556 static int target_timer_callback_periodic_restart(
1557 struct target_timer_callback
*cb
, struct timeval
*now
)
1559 int time_ms
= cb
->time_ms
;
1560 cb
->when
.tv_usec
= now
->tv_usec
+ (time_ms
% 1000) * 1000;
1561 time_ms
-= (time_ms
% 1000);
1562 cb
->when
.tv_sec
= now
->tv_sec
+ time_ms
/ 1000;
1563 if (cb
->when
.tv_usec
> 1000000) {
1564 cb
->when
.tv_usec
= cb
->when
.tv_usec
- 1000000;
1565 cb
->when
.tv_sec
+= 1;
1570 static int target_call_timer_callback(struct target_timer_callback
*cb
,
1571 struct timeval
*now
)
1573 cb
->callback(cb
->priv
);
1576 return target_timer_callback_periodic_restart(cb
, now
);
1578 return target_unregister_timer_callback(cb
->callback
, cb
->priv
);
1581 static int target_call_timer_callbacks_check_time(int checktime
)
1583 static bool callback_processing
;
1585 /* Do not allow nesting */
1586 if (callback_processing
)
1589 callback_processing
= true;
1594 gettimeofday(&now
, NULL
);
1596 /* Store an address of the place containing a pointer to the
1597 * next item; initially, that's a standalone "root of the
1598 * list" variable. */
1599 struct target_timer_callback
**callback
= &target_timer_callbacks
;
1601 if ((*callback
)->removed
) {
1602 struct target_timer_callback
*p
= *callback
;
1603 *callback
= (*callback
)->next
;
1608 bool call_it
= (*callback
)->callback
&&
1609 ((!checktime
&& (*callback
)->periodic
) ||
1610 now
.tv_sec
> (*callback
)->when
.tv_sec
||
1611 (now
.tv_sec
== (*callback
)->when
.tv_sec
&&
1612 now
.tv_usec
>= (*callback
)->when
.tv_usec
));
1615 target_call_timer_callback(*callback
, &now
);
1617 callback
= &(*callback
)->next
;
1620 callback_processing
= false;
1624 int target_call_timer_callbacks(void)
1626 return target_call_timer_callbacks_check_time(1);
1629 /* invoke periodic callbacks immediately */
1630 int target_call_timer_callbacks_now(void)
1632 return target_call_timer_callbacks_check_time(0);
1635 /* Prints the working area layout for debug purposes */
1636 static void print_wa_layout(struct target
*target
)
1638 struct working_area
*c
= target
->working_areas
;
1641 LOG_DEBUG("%c%c " TARGET_ADDR_FMT
"-" TARGET_ADDR_FMT
" (%" PRIu32
" bytes)",
1642 c
->backup
? 'b' : ' ', c
->free
? ' ' : '*',
1643 c
->address
, c
->address
+ c
->size
- 1, c
->size
);
1648 /* Reduce area to size bytes, create a new free area from the remaining bytes, if any. */
1649 static void target_split_working_area(struct working_area
*area
, uint32_t size
)
1651 assert(area
->free
); /* Shouldn't split an allocated area */
1652 assert(size
<= area
->size
); /* Caller should guarantee this */
1654 /* Split only if not already the right size */
1655 if (size
< area
->size
) {
1656 struct working_area
*new_wa
= malloc(sizeof(*new_wa
));
1661 new_wa
->next
= area
->next
;
1662 new_wa
->size
= area
->size
- size
;
1663 new_wa
->address
= area
->address
+ size
;
1664 new_wa
->backup
= NULL
;
1665 new_wa
->user
= NULL
;
1666 new_wa
->free
= true;
1668 area
->next
= new_wa
;
1671 /* If backup memory was allocated to this area, it has the wrong size
1672 * now so free it and it will be reallocated if/when needed */
1675 area
->backup
= NULL
;
1680 /* Merge all adjacent free areas into one */
1681 static void target_merge_working_areas(struct target
*target
)
1683 struct working_area
*c
= target
->working_areas
;
1685 while (c
&& c
->next
) {
1686 assert(c
->next
->address
== c
->address
+ c
->size
); /* This is an invariant */
1688 /* Find two adjacent free areas */
1689 if (c
->free
&& c
->next
->free
) {
1690 /* Merge the last into the first */
1691 c
->size
+= c
->next
->size
;
1693 /* Remove the last */
1694 struct working_area
*to_be_freed
= c
->next
;
1695 c
->next
= c
->next
->next
;
1696 if (to_be_freed
->backup
)
1697 free(to_be_freed
->backup
);
1700 /* If backup memory was allocated to the remaining area, it's has
1701 * the wrong size now */
1712 int target_alloc_working_area_try(struct target
*target
, uint32_t size
, struct working_area
**area
)
1714 /* Reevaluate working area address based on MMU state*/
1715 if (target
->working_areas
== NULL
) {
1719 retval
= target
->type
->mmu(target
, &enabled
);
1720 if (retval
!= ERROR_OK
)
1724 if (target
->working_area_phys_spec
) {
1725 LOG_DEBUG("MMU disabled, using physical "
1726 "address for working memory " TARGET_ADDR_FMT
,
1727 target
->working_area_phys
);
1728 target
->working_area
= target
->working_area_phys
;
1730 LOG_ERROR("No working memory available. "
1731 "Specify -work-area-phys to target.");
1732 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
1735 if (target
->working_area_virt_spec
) {
1736 LOG_DEBUG("MMU enabled, using virtual "
1737 "address for working memory " TARGET_ADDR_FMT
,
1738 target
->working_area_virt
);
1739 target
->working_area
= target
->working_area_virt
;
1741 LOG_ERROR("No working memory available. "
1742 "Specify -work-area-virt to target.");
1743 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
1747 /* Set up initial working area on first call */
1748 struct working_area
*new_wa
= malloc(sizeof(*new_wa
));
1750 new_wa
->next
= NULL
;
1751 new_wa
->size
= target
->working_area_size
& ~3UL; /* 4-byte align */
1752 new_wa
->address
= target
->working_area
;
1753 new_wa
->backup
= NULL
;
1754 new_wa
->user
= NULL
;
1755 new_wa
->free
= true;
1758 target
->working_areas
= new_wa
;
1761 /* only allocate multiples of 4 byte */
1763 size
= (size
+ 3) & (~3UL);
1765 struct working_area
*c
= target
->working_areas
;
1767 /* Find the first large enough working area */
1769 if (c
->free
&& c
->size
>= size
)
1775 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
1777 /* Split the working area into the requested size */
1778 target_split_working_area(c
, size
);
1780 LOG_DEBUG("allocated new working area of %" PRIu32
" bytes at address " TARGET_ADDR_FMT
,
1783 if (target
->backup_working_area
) {
1784 if (c
->backup
== NULL
) {
1785 c
->backup
= malloc(c
->size
);
1786 if (c
->backup
== NULL
)
1790 int retval
= target_read_memory(target
, c
->address
, 4, c
->size
/ 4, c
->backup
);
1791 if (retval
!= ERROR_OK
)
1795 /* mark as used, and return the new (reused) area */
1802 print_wa_layout(target
);
1807 int target_alloc_working_area(struct target
*target
, uint32_t size
, struct working_area
**area
)
1811 retval
= target_alloc_working_area_try(target
, size
, area
);
1812 if (retval
== ERROR_TARGET_RESOURCE_NOT_AVAILABLE
)
1813 LOG_WARNING("not enough working area available(requested %"PRIu32
")", size
);
1818 static int target_restore_working_area(struct target
*target
, struct working_area
*area
)
1820 int retval
= ERROR_OK
;
1822 if (target
->backup_working_area
&& area
->backup
!= NULL
) {
1823 retval
= target_write_memory(target
, area
->address
, 4, area
->size
/ 4, area
->backup
);
1824 if (retval
!= ERROR_OK
)
1825 LOG_ERROR("failed to restore %" PRIu32
" bytes of working area at address " TARGET_ADDR_FMT
,
1826 area
->size
, area
->address
);
1832 /* Restore the area's backup memory, if any, and return the area to the allocation pool */
1833 static int target_free_working_area_restore(struct target
*target
, struct working_area
*area
, int restore
)
1835 int retval
= ERROR_OK
;
1841 retval
= target_restore_working_area(target
, area
);
1842 /* REVISIT: Perhaps the area should be freed even if restoring fails. */
1843 if (retval
!= ERROR_OK
)
1849 LOG_DEBUG("freed %" PRIu32
" bytes of working area at address " TARGET_ADDR_FMT
,
1850 area
->size
, area
->address
);
1852 /* mark user pointer invalid */
1853 /* TODO: Is this really safe? It points to some previous caller's memory.
1854 * How could we know that the area pointer is still in that place and not
1855 * some other vital data? What's the purpose of this, anyway? */
1859 target_merge_working_areas(target
);
1861 print_wa_layout(target
);
1866 int target_free_working_area(struct target
*target
, struct working_area
*area
)
1868 return target_free_working_area_restore(target
, area
, 1);
1871 static void target_destroy(struct target
*target
)
1873 if (target
->type
->deinit_target
)
1874 target
->type
->deinit_target(target
);
1877 free(target
->trace_info
);
1878 free(target
->cmd_name
);
1882 void target_quit(void)
1884 struct target_event_callback
*pe
= target_event_callbacks
;
1886 struct target_event_callback
*t
= pe
->next
;
1890 target_event_callbacks
= NULL
;
1892 struct target_timer_callback
*pt
= target_timer_callbacks
;
1894 struct target_timer_callback
*t
= pt
->next
;
1898 target_timer_callbacks
= NULL
;
1900 for (struct target
*target
= all_targets
; target
;) {
1904 target_destroy(target
);
1911 /* free resources and restore memory, if restoring memory fails,
1912 * free up resources anyway
1914 static void target_free_all_working_areas_restore(struct target
*target
, int restore
)
1916 struct working_area
*c
= target
->working_areas
;
1918 LOG_DEBUG("freeing all working areas");
1920 /* Loop through all areas, restoring the allocated ones and marking them as free */
1924 target_restore_working_area(target
, c
);
1926 *c
->user
= NULL
; /* Same as above */
1932 /* Run a merge pass to combine all areas into one */
1933 target_merge_working_areas(target
);
1935 print_wa_layout(target
);
1938 void target_free_all_working_areas(struct target
*target
)
1940 target_free_all_working_areas_restore(target
, 1);
1943 /* Find the largest number of bytes that can be allocated */
1944 uint32_t target_get_working_area_avail(struct target
*target
)
1946 struct working_area
*c
= target
->working_areas
;
1947 uint32_t max_size
= 0;
1950 return target
->working_area_size
;
1953 if (c
->free
&& max_size
< c
->size
)
1962 int target_arch_state(struct target
*target
)
1965 if (target
== NULL
) {
1966 LOG_WARNING("No target has been configured");
1970 if (target
->state
!= TARGET_HALTED
)
1973 retval
= target
->type
->arch_state(target
);
1977 static int target_get_gdb_fileio_info_default(struct target
*target
,
1978 struct gdb_fileio_info
*fileio_info
)
1980 /* If target does not support semi-hosting function, target
1981 has no need to provide .get_gdb_fileio_info callback.
1982 It just return ERROR_FAIL and gdb_server will return "Txx"
1983 as target halted every time. */
1987 static int target_gdb_fileio_end_default(struct target
*target
,
1988 int retcode
, int fileio_errno
, bool ctrl_c
)
1993 static int target_profiling_default(struct target
*target
, uint32_t *samples
,
1994 uint32_t max_num_samples
, uint32_t *num_samples
, uint32_t seconds
)
1996 struct timeval timeout
, now
;
1998 gettimeofday(&timeout
, NULL
);
1999 timeval_add_time(&timeout
, seconds
, 0);
2001 LOG_INFO("Starting profiling. Halting and resuming the"
2002 " target as often as we can...");
2004 uint32_t sample_count
= 0;
2005 /* hopefully it is safe to cache! We want to stop/restart as quickly as possible. */
2006 struct reg
*reg
= register_get_by_name(target
->reg_cache
, "pc", 1);
2008 int retval
= ERROR_OK
;
2010 target_poll(target
);
2011 if (target
->state
== TARGET_HALTED
) {
2012 uint32_t t
= buf_get_u32(reg
->value
, 0, 32);
2013 samples
[sample_count
++] = t
;
2014 /* current pc, addr = 0, do not handle breakpoints, not debugging */
2015 retval
= target_resume(target
, 1, 0, 0, 0);
2016 target_poll(target
);
2017 alive_sleep(10); /* sleep 10ms, i.e. <100 samples/second. */
2018 } else if (target
->state
== TARGET_RUNNING
) {
2019 /* We want to quickly sample the PC. */
2020 retval
= target_halt(target
);
2022 LOG_INFO("Target not halted or running");
2027 if (retval
!= ERROR_OK
)
2030 gettimeofday(&now
, NULL
);
2031 if ((sample_count
>= max_num_samples
) ||
2032 ((now
.tv_sec
>= timeout
.tv_sec
) && (now
.tv_usec
>= timeout
.tv_usec
))) {
2033 LOG_INFO("Profiling completed. %" PRIu32
" samples.", sample_count
);
2038 *num_samples
= sample_count
;
2042 /* Single aligned words are guaranteed to use 16 or 32 bit access
2043 * mode respectively, otherwise data is handled as quickly as
2046 int target_write_buffer(struct target
*target
, target_addr_t address
, uint32_t size
, const uint8_t *buffer
)
2048 LOG_DEBUG("writing buffer of %" PRIi32
" byte at " TARGET_ADDR_FMT
,
2051 if (!target_was_examined(target
)) {
2052 LOG_ERROR("Target not examined yet");
2059 if ((address
+ size
- 1) < address
) {
2060 /* GDB can request this when e.g. PC is 0xfffffffc */
2061 LOG_ERROR("address + size wrapped (" TARGET_ADDR_FMT
", 0x%08" PRIx32
")",
2067 return target
->type
->write_buffer(target
, address
, size
, buffer
);
2070 static int target_write_buffer_default(struct target
*target
,
2071 target_addr_t address
, uint32_t count
, const uint8_t *buffer
)
2075 /* Align up to maximum 4 bytes. The loop condition makes sure the next pass
2076 * will have something to do with the size we leave to it. */
2077 for (size
= 1; size
< 4 && count
>= size
* 2 + (address
& size
); size
*= 2) {
2078 if (address
& size
) {
2079 int retval
= target_write_memory(target
, address
, size
, 1, buffer
);
2080 if (retval
!= ERROR_OK
)
2088 /* Write the data with as large access size as possible. */
2089 for (; size
> 0; size
/= 2) {
2090 uint32_t aligned
= count
- count
% size
;
2092 int retval
= target_write_memory(target
, address
, size
, aligned
/ size
, buffer
);
2093 if (retval
!= ERROR_OK
)
2104 /* Single aligned words are guaranteed to use 16 or 32 bit access
2105 * mode respectively, otherwise data is handled as quickly as
2108 int target_read_buffer(struct target
*target
, target_addr_t address
, uint32_t size
, uint8_t *buffer
)
2110 LOG_DEBUG("reading buffer of %" PRIi32
" byte at " TARGET_ADDR_FMT
,
2113 if (!target_was_examined(target
)) {
2114 LOG_ERROR("Target not examined yet");
2121 if ((address
+ size
- 1) < address
) {
2122 /* GDB can request this when e.g. PC is 0xfffffffc */
2123 LOG_ERROR("address + size wrapped (" TARGET_ADDR_FMT
", 0x%08" PRIx32
")",
2129 return target
->type
->read_buffer(target
, address
, size
, buffer
);
2132 static int target_read_buffer_default(struct target
*target
, target_addr_t address
, uint32_t count
, uint8_t *buffer
)
2136 /* Align up to maximum 4 bytes. The loop condition makes sure the next pass
2137 * will have something to do with the size we leave to it. */
2138 for (size
= 1; size
< 4 && count
>= size
* 2 + (address
& size
); size
*= 2) {
2139 if (address
& size
) {
2140 int retval
= target_read_memory(target
, address
, size
, 1, buffer
);
2141 if (retval
!= ERROR_OK
)
2149 /* Read the data with as large access size as possible. */
2150 for (; size
> 0; size
/= 2) {
2151 uint32_t aligned
= count
- count
% size
;
2153 int retval
= target_read_memory(target
, address
, size
, aligned
/ size
, buffer
);
2154 if (retval
!= ERROR_OK
)
2165 int target_checksum_memory(struct target
*target
, target_addr_t address
, uint32_t size
, uint32_t* crc
)
2170 uint32_t checksum
= 0;
2171 if (!target_was_examined(target
)) {
2172 LOG_ERROR("Target not examined yet");
2176 retval
= target
->type
->checksum_memory(target
, address
, size
, &checksum
);
2177 if (retval
!= ERROR_OK
) {
2178 buffer
= malloc(size
);
2179 if (buffer
== NULL
) {
2180 LOG_ERROR("error allocating buffer for section (%" PRId32
" bytes)", size
);
2181 return ERROR_COMMAND_SYNTAX_ERROR
;
2183 retval
= target_read_buffer(target
, address
, size
, buffer
);
2184 if (retval
!= ERROR_OK
) {
2189 /* convert to target endianness */
2190 for (i
= 0; i
< (size
/sizeof(uint32_t)); i
++) {
2191 uint32_t target_data
;
2192 target_data
= target_buffer_get_u32(target
, &buffer
[i
*sizeof(uint32_t)]);
2193 target_buffer_set_u32(target
, &buffer
[i
*sizeof(uint32_t)], target_data
);
2196 retval
= image_calculate_checksum(buffer
, size
, &checksum
);
2205 int target_blank_check_memory(struct target
*target
, target_addr_t address
, uint32_t size
, uint32_t* blank
,
2206 uint8_t erased_value
)
2209 if (!target_was_examined(target
)) {
2210 LOG_ERROR("Target not examined yet");
2214 if (target
->type
->blank_check_memory
== 0)
2215 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
2217 retval
= target
->type
->blank_check_memory(target
, address
, size
, blank
, erased_value
);
2222 int target_read_u64(struct target
*target
, target_addr_t address
, uint64_t *value
)
2224 uint8_t value_buf
[8];
2225 if (!target_was_examined(target
)) {
2226 LOG_ERROR("Target not examined yet");
2230 int retval
= target_read_memory(target
, address
, 8, 1, value_buf
);
2232 if (retval
== ERROR_OK
) {
2233 *value
= target_buffer_get_u64(target
, value_buf
);
2234 LOG_DEBUG("address: " TARGET_ADDR_FMT
", value: 0x%16.16" PRIx64
"",
2239 LOG_DEBUG("address: " TARGET_ADDR_FMT
" failed",
2246 int target_read_u32(struct target
*target
, target_addr_t address
, uint32_t *value
)
2248 uint8_t value_buf
[4];
2249 if (!target_was_examined(target
)) {
2250 LOG_ERROR("Target not examined yet");
2254 int retval
= target_read_memory(target
, address
, 4, 1, value_buf
);
2256 if (retval
== ERROR_OK
) {
2257 *value
= target_buffer_get_u32(target
, value_buf
);
2258 LOG_DEBUG("address: " TARGET_ADDR_FMT
", value: 0x%8.8" PRIx32
"",
2263 LOG_DEBUG("address: " TARGET_ADDR_FMT
" failed",
2270 int target_read_u16(struct target
*target
, target_addr_t address
, uint16_t *value
)
2272 uint8_t value_buf
[2];
2273 if (!target_was_examined(target
)) {
2274 LOG_ERROR("Target not examined yet");
2278 int retval
= target_read_memory(target
, address
, 2, 1, value_buf
);
2280 if (retval
== ERROR_OK
) {
2281 *value
= target_buffer_get_u16(target
, value_buf
);
2282 LOG_DEBUG("address: " TARGET_ADDR_FMT
", value: 0x%4.4" PRIx16
,
2287 LOG_DEBUG("address: " TARGET_ADDR_FMT
" failed",
2294 int target_read_u8(struct target
*target
, target_addr_t address
, uint8_t *value
)
2296 if (!target_was_examined(target
)) {
2297 LOG_ERROR("Target not examined yet");
2301 int retval
= target_read_memory(target
, address
, 1, 1, value
);
2303 if (retval
== ERROR_OK
) {
2304 LOG_DEBUG("address: " TARGET_ADDR_FMT
", value: 0x%2.2" PRIx8
,
2309 LOG_DEBUG("address: " TARGET_ADDR_FMT
" failed",
2316 int target_write_u64(struct target
*target
, target_addr_t address
, uint64_t value
)
2319 uint8_t value_buf
[8];
2320 if (!target_was_examined(target
)) {
2321 LOG_ERROR("Target not examined yet");
2325 LOG_DEBUG("address: " TARGET_ADDR_FMT
", value: 0x%16.16" PRIx64
"",
2329 target_buffer_set_u64(target
, value_buf
, value
);
2330 retval
= target_write_memory(target
, address
, 8, 1, value_buf
);
2331 if (retval
!= ERROR_OK
)
2332 LOG_DEBUG("failed: %i", retval
);
2337 int target_write_u32(struct target
*target
, target_addr_t address
, uint32_t value
)
2340 uint8_t value_buf
[4];
2341 if (!target_was_examined(target
)) {
2342 LOG_ERROR("Target not examined yet");
2346 LOG_DEBUG("address: " TARGET_ADDR_FMT
", value: 0x%8.8" PRIx32
"",
2350 target_buffer_set_u32(target
, value_buf
, value
);
2351 retval
= target_write_memory(target
, address
, 4, 1, value_buf
);
2352 if (retval
!= ERROR_OK
)
2353 LOG_DEBUG("failed: %i", retval
);
2358 int target_write_u16(struct target
*target
, target_addr_t address
, uint16_t value
)
2361 uint8_t value_buf
[2];
2362 if (!target_was_examined(target
)) {
2363 LOG_ERROR("Target not examined yet");
2367 LOG_DEBUG("address: " TARGET_ADDR_FMT
", value: 0x%8.8" PRIx16
,
2371 target_buffer_set_u16(target
, value_buf
, value
);
2372 retval
= target_write_memory(target
, address
, 2, 1, value_buf
);
2373 if (retval
!= ERROR_OK
)
2374 LOG_DEBUG("failed: %i", retval
);
2379 int target_write_u8(struct target
*target
, target_addr_t address
, uint8_t value
)
2382 if (!target_was_examined(target
)) {
2383 LOG_ERROR("Target not examined yet");
2387 LOG_DEBUG("address: " TARGET_ADDR_FMT
", value: 0x%2.2" PRIx8
,
2390 retval
= target_write_memory(target
, address
, 1, 1, &value
);
2391 if (retval
!= ERROR_OK
)
2392 LOG_DEBUG("failed: %i", retval
);
2397 int target_write_phys_u64(struct target
*target
, target_addr_t address
, uint64_t value
)
2400 uint8_t value_buf
[8];
2401 if (!target_was_examined(target
)) {
2402 LOG_ERROR("Target not examined yet");
2406 LOG_DEBUG("address: " TARGET_ADDR_FMT
", value: 0x%16.16" PRIx64
"",
2410 target_buffer_set_u64(target
, value_buf
, value
);
2411 retval
= target_write_phys_memory(target
, address
, 8, 1, value_buf
);
2412 if (retval
!= ERROR_OK
)
2413 LOG_DEBUG("failed: %i", retval
);
2418 int target_write_phys_u32(struct target
*target
, target_addr_t address
, uint32_t value
)
2421 uint8_t value_buf
[4];
2422 if (!target_was_examined(target
)) {
2423 LOG_ERROR("Target not examined yet");
2427 LOG_DEBUG("address: " TARGET_ADDR_FMT
", value: 0x%8.8" PRIx32
"",
2431 target_buffer_set_u32(target
, value_buf
, value
);
2432 retval
= target_write_phys_memory(target
, address
, 4, 1, value_buf
);
2433 if (retval
!= ERROR_OK
)
2434 LOG_DEBUG("failed: %i", retval
);
2439 int target_write_phys_u16(struct target
*target
, target_addr_t address
, uint16_t value
)
2442 uint8_t value_buf
[2];
2443 if (!target_was_examined(target
)) {
2444 LOG_ERROR("Target not examined yet");
2448 LOG_DEBUG("address: " TARGET_ADDR_FMT
", value: 0x%8.8" PRIx16
,
2452 target_buffer_set_u16(target
, value_buf
, value
);
2453 retval
= target_write_phys_memory(target
, address
, 2, 1, value_buf
);
2454 if (retval
!= ERROR_OK
)
2455 LOG_DEBUG("failed: %i", retval
);
2460 int target_write_phys_u8(struct target
*target
, target_addr_t address
, uint8_t value
)
2463 if (!target_was_examined(target
)) {
2464 LOG_ERROR("Target not examined yet");
2468 LOG_DEBUG("address: " TARGET_ADDR_FMT
", value: 0x%2.2" PRIx8
,
2471 retval
= target_write_phys_memory(target
, address
, 1, 1, &value
);
2472 if (retval
!= ERROR_OK
)
2473 LOG_DEBUG("failed: %i", retval
);
2478 static int find_target(struct command_context
*cmd_ctx
, const char *name
)
2480 struct target
*target
= get_target(name
);
2481 if (target
== NULL
) {
2482 LOG_ERROR("Target: %s is unknown, try one of:\n", name
);
2485 if (!target
->tap
->enabled
) {
2486 LOG_USER("Target: TAP %s is disabled, "
2487 "can't be the current target\n",
2488 target
->tap
->dotted_name
);
2492 cmd_ctx
->current_target
= target
->target_number
;
2497 COMMAND_HANDLER(handle_targets_command
)
2499 int retval
= ERROR_OK
;
2500 if (CMD_ARGC
== 1) {
2501 retval
= find_target(CMD_CTX
, CMD_ARGV
[0]);
2502 if (retval
== ERROR_OK
) {
2508 struct target
*target
= all_targets
;
2509 command_print(CMD_CTX
, " TargetName Type Endian TapName State ");
2510 command_print(CMD_CTX
, "-- ------------------ ---------- ------ ------------------ ------------");
2515 if (target
->tap
->enabled
)
2516 state
= target_state_name(target
);
2518 state
= "tap-disabled";
2520 if (CMD_CTX
->current_target
== target
->target_number
)
2523 /* keep columns lined up to match the headers above */
2524 command_print(CMD_CTX
,
2525 "%2d%c %-18s %-10s %-6s %-18s %s",
2526 target
->target_number
,
2528 target_name(target
),
2529 target_type_name(target
),
2530 Jim_Nvp_value2name_simple(nvp_target_endian
,
2531 target
->endianness
)->name
,
2532 target
->tap
->dotted_name
,
2534 target
= target
->next
;
2540 /* every 300ms we check for reset & powerdropout and issue a "reset halt" if so. */
2542 static int powerDropout
;
2543 static int srstAsserted
;
2545 static int runPowerRestore
;
2546 static int runPowerDropout
;
2547 static int runSrstAsserted
;
2548 static int runSrstDeasserted
;
2550 static int sense_handler(void)
2552 static int prevSrstAsserted
;
2553 static int prevPowerdropout
;
2555 int retval
= jtag_power_dropout(&powerDropout
);
2556 if (retval
!= ERROR_OK
)
2560 powerRestored
= prevPowerdropout
&& !powerDropout
;
2562 runPowerRestore
= 1;
2564 int64_t current
= timeval_ms();
2565 static int64_t lastPower
;
2566 bool waitMore
= lastPower
+ 2000 > current
;
2567 if (powerDropout
&& !waitMore
) {
2568 runPowerDropout
= 1;
2569 lastPower
= current
;
2572 retval
= jtag_srst_asserted(&srstAsserted
);
2573 if (retval
!= ERROR_OK
)
2577 srstDeasserted
= prevSrstAsserted
&& !srstAsserted
;
2579 static int64_t lastSrst
;
2580 waitMore
= lastSrst
+ 2000 > current
;
2581 if (srstDeasserted
&& !waitMore
) {
2582 runSrstDeasserted
= 1;
2586 if (!prevSrstAsserted
&& srstAsserted
)
2587 runSrstAsserted
= 1;
2589 prevSrstAsserted
= srstAsserted
;
2590 prevPowerdropout
= powerDropout
;
2592 if (srstDeasserted
|| powerRestored
) {
2593 /* Other than logging the event we can't do anything here.
2594 * Issuing a reset is a particularly bad idea as we might
2595 * be inside a reset already.
2602 /* process target state changes */
2603 static int handle_target(void *priv
)
2605 Jim_Interp
*interp
= (Jim_Interp
*)priv
;
2606 int retval
= ERROR_OK
;
2608 if (!is_jtag_poll_safe()) {
2609 /* polling is disabled currently */
2613 /* we do not want to recurse here... */
2614 static int recursive
;
2618 /* danger! running these procedures can trigger srst assertions and power dropouts.
2619 * We need to avoid an infinite loop/recursion here and we do that by
2620 * clearing the flags after running these events.
2622 int did_something
= 0;
2623 if (runSrstAsserted
) {
2624 LOG_INFO("srst asserted detected, running srst_asserted proc.");
2625 Jim_Eval(interp
, "srst_asserted");
2628 if (runSrstDeasserted
) {
2629 Jim_Eval(interp
, "srst_deasserted");
2632 if (runPowerDropout
) {
2633 LOG_INFO("Power dropout detected, running power_dropout proc.");
2634 Jim_Eval(interp
, "power_dropout");
2637 if (runPowerRestore
) {
2638 Jim_Eval(interp
, "power_restore");
2642 if (did_something
) {
2643 /* clear detect flags */
2647 /* clear action flags */
2649 runSrstAsserted
= 0;
2650 runSrstDeasserted
= 0;
2651 runPowerRestore
= 0;
2652 runPowerDropout
= 0;
2657 /* Poll targets for state changes unless that's globally disabled.
2658 * Skip targets that are currently disabled.
2660 for (struct target
*target
= all_targets
;
2661 is_jtag_poll_safe() && target
;
2662 target
= target
->next
) {
2664 if (!target_was_examined(target
))
2667 if (!target
->tap
->enabled
)
2670 if (target
->backoff
.times
> target
->backoff
.count
) {
2671 /* do not poll this time as we failed previously */
2672 target
->backoff
.count
++;
2675 target
->backoff
.count
= 0;
2677 /* only poll target if we've got power and srst isn't asserted */
2678 if (!powerDropout
&& !srstAsserted
) {
2679 /* polling may fail silently until the target has been examined */
2680 retval
= target_poll(target
);
2681 if (retval
!= ERROR_OK
) {
2682 /* 100ms polling interval. Increase interval between polling up to 5000ms */
2683 if (target
->backoff
.times
* polling_interval
< 5000) {
2684 target
->backoff
.times
*= 2;
2685 target
->backoff
.times
++;
2688 /* Tell GDB to halt the debugger. This allows the user to
2689 * run monitor commands to handle the situation.
2691 target_call_event_callbacks(target
, TARGET_EVENT_GDB_HALT
);
2693 if (target
->backoff
.times
> 0) {
2694 LOG_USER("Polling target %s failed, trying to reexamine", target_name(target
));
2695 target_reset_examined(target
);
2696 retval
= target_examine_one(target
);
2697 /* Target examination could have failed due to unstable connection,
2698 * but we set the examined flag anyway to repoll it later */
2699 if (retval
!= ERROR_OK
) {
2700 target
->examined
= true;
2701 LOG_USER("Examination failed, GDB will be halted. Polling again in %dms",
2702 target
->backoff
.times
* polling_interval
);
2707 /* Since we succeeded, we reset backoff count */
2708 target
->backoff
.times
= 0;
2715 COMMAND_HANDLER(handle_reg_command
)
2717 struct target
*target
;
2718 struct reg
*reg
= NULL
;
2724 target
= get_current_target(CMD_CTX
);
2726 /* list all available registers for the current target */
2727 if (CMD_ARGC
== 0) {
2728 struct reg_cache
*cache
= target
->reg_cache
;
2734 command_print(CMD_CTX
, "===== %s", cache
->name
);
2736 for (i
= 0, reg
= cache
->reg_list
;
2737 i
< cache
->num_regs
;
2738 i
++, reg
++, count
++) {
2739 /* only print cached values if they are valid */
2741 value
= buf_to_str(reg
->value
,
2743 command_print(CMD_CTX
,
2744 "(%i) %s (/%" PRIu32
"): 0x%s%s",
2752 command_print(CMD_CTX
, "(%i) %s (/%" PRIu32
")",
2757 cache
= cache
->next
;
2763 /* access a single register by its ordinal number */
2764 if ((CMD_ARGV
[0][0] >= '0') && (CMD_ARGV
[0][0] <= '9')) {
2766 COMMAND_PARSE_NUMBER(uint
, CMD_ARGV
[0], num
);
2768 struct reg_cache
*cache
= target
->reg_cache
;
2772 for (i
= 0; i
< cache
->num_regs
; i
++) {
2773 if (count
++ == num
) {
2774 reg
= &cache
->reg_list
[i
];
2780 cache
= cache
->next
;
2784 command_print(CMD_CTX
, "%i is out of bounds, the current target "
2785 "has only %i registers (0 - %i)", num
, count
, count
- 1);
2789 /* access a single register by its name */
2790 reg
= register_get_by_name(target
->reg_cache
, CMD_ARGV
[0], 1);
2793 command_print(CMD_CTX
, "register %s not found in current target", CMD_ARGV
[0]);
2798 assert(reg
!= NULL
); /* give clang a hint that we *know* reg is != NULL here */
2800 /* display a register */
2801 if ((CMD_ARGC
== 1) || ((CMD_ARGC
== 2) && !((CMD_ARGV
[1][0] >= '0')
2802 && (CMD_ARGV
[1][0] <= '9')))) {
2803 if ((CMD_ARGC
== 2) && (strcmp(CMD_ARGV
[1], "force") == 0))
2806 if (reg
->valid
== 0)
2807 reg
->type
->get(reg
);
2808 value
= buf_to_str(reg
->value
, reg
->size
, 16);
2809 command_print(CMD_CTX
, "%s (/%i): 0x%s", reg
->name
, (int)(reg
->size
), value
);
2814 /* set register value */
2815 if (CMD_ARGC
== 2) {
2816 uint8_t *buf
= malloc(DIV_ROUND_UP(reg
->size
, 8));
2819 str_to_buf(CMD_ARGV
[1], strlen(CMD_ARGV
[1]), buf
, reg
->size
, 0);
2821 reg
->type
->set(reg
, buf
);
2823 value
= buf_to_str(reg
->value
, reg
->size
, 16);
2824 command_print(CMD_CTX
, "%s (/%i): 0x%s", reg
->name
, (int)(reg
->size
), value
);
2832 return ERROR_COMMAND_SYNTAX_ERROR
;
2835 COMMAND_HANDLER(handle_poll_command
)
2837 int retval
= ERROR_OK
;
2838 struct target
*target
= get_current_target(CMD_CTX
);
2840 if (CMD_ARGC
== 0) {
2841 command_print(CMD_CTX
, "background polling: %s",
2842 jtag_poll_get_enabled() ? "on" : "off");
2843 command_print(CMD_CTX
, "TAP: %s (%s)",
2844 target
->tap
->dotted_name
,
2845 target
->tap
->enabled
? "enabled" : "disabled");
2846 if (!target
->tap
->enabled
)
2848 retval
= target_poll(target
);
2849 if (retval
!= ERROR_OK
)
2851 retval
= target_arch_state(target
);
2852 if (retval
!= ERROR_OK
)
2854 } else if (CMD_ARGC
== 1) {
2856 COMMAND_PARSE_ON_OFF(CMD_ARGV
[0], enable
);
2857 jtag_poll_set_enabled(enable
);
2859 return ERROR_COMMAND_SYNTAX_ERROR
;
2864 COMMAND_HANDLER(handle_wait_halt_command
)
2867 return ERROR_COMMAND_SYNTAX_ERROR
;
2869 unsigned ms
= DEFAULT_HALT_TIMEOUT
;
2870 if (1 == CMD_ARGC
) {
2871 int retval
= parse_uint(CMD_ARGV
[0], &ms
);
2872 if (ERROR_OK
!= retval
)
2873 return ERROR_COMMAND_SYNTAX_ERROR
;
2876 struct target
*target
= get_current_target(CMD_CTX
);
2877 return target_wait_state(target
, TARGET_HALTED
, ms
);
2880 /* wait for target state to change. The trick here is to have a low
2881 * latency for short waits and not to suck up all the CPU time
2884 * After 500ms, keep_alive() is invoked
2886 int target_wait_state(struct target
*target
, enum target_state state
, int ms
)
2889 int64_t then
= 0, cur
;
2893 retval
= target_poll(target
);
2894 if (retval
!= ERROR_OK
)
2896 if (target
->state
== state
)
2901 then
= timeval_ms();
2902 LOG_DEBUG("waiting for target %s...",
2903 Jim_Nvp_value2name_simple(nvp_target_state
, state
)->name
);
2909 if ((cur
-then
) > ms
) {
2910 LOG_ERROR("timed out while waiting for target %s",
2911 Jim_Nvp_value2name_simple(nvp_target_state
, state
)->name
);
2919 COMMAND_HANDLER(handle_halt_command
)
2923 struct target
*target
= get_current_target(CMD_CTX
);
2924 int retval
= target_halt(target
);
2925 if (ERROR_OK
!= retval
)
2928 if (CMD_ARGC
== 1) {
2929 unsigned wait_local
;
2930 retval
= parse_uint(CMD_ARGV
[0], &wait_local
);
2931 if (ERROR_OK
!= retval
)
2932 return ERROR_COMMAND_SYNTAX_ERROR
;
2937 return CALL_COMMAND_HANDLER(handle_wait_halt_command
);
2940 COMMAND_HANDLER(handle_soft_reset_halt_command
)
2942 struct target
*target
= get_current_target(CMD_CTX
);
2944 LOG_USER("requesting target halt and executing a soft reset");
2946 target_soft_reset_halt(target
);
2951 COMMAND_HANDLER(handle_reset_command
)
2954 return ERROR_COMMAND_SYNTAX_ERROR
;
2956 enum target_reset_mode reset_mode
= RESET_RUN
;
2957 if (CMD_ARGC
== 1) {
2959 n
= Jim_Nvp_name2value_simple(nvp_reset_modes
, CMD_ARGV
[0]);
2960 if ((n
->name
== NULL
) || (n
->value
== RESET_UNKNOWN
))
2961 return ERROR_COMMAND_SYNTAX_ERROR
;
2962 reset_mode
= n
->value
;
2965 /* reset *all* targets */
2966 return target_process_reset(CMD_CTX
, reset_mode
);
2970 COMMAND_HANDLER(handle_resume_command
)
2974 return ERROR_COMMAND_SYNTAX_ERROR
;
2976 struct target
*target
= get_current_target(CMD_CTX
);
2978 /* with no CMD_ARGV, resume from current pc, addr = 0,
2979 * with one arguments, addr = CMD_ARGV[0],
2980 * handle breakpoints, not debugging */
2981 target_addr_t addr
= 0;
2982 if (CMD_ARGC
== 1) {
2983 COMMAND_PARSE_ADDRESS(CMD_ARGV
[0], addr
);
2987 return target_resume(target
, current
, addr
, 1, 0);
2990 COMMAND_HANDLER(handle_step_command
)
2993 return ERROR_COMMAND_SYNTAX_ERROR
;
2997 /* with no CMD_ARGV, step from current pc, addr = 0,
2998 * with one argument addr = CMD_ARGV[0],
2999 * handle breakpoints, debugging */
3000 target_addr_t addr
= 0;
3002 if (CMD_ARGC
== 1) {
3003 COMMAND_PARSE_ADDRESS(CMD_ARGV
[0], addr
);
3007 struct target
*target
= get_current_target(CMD_CTX
);
3009 return target
->type
->step(target
, current_pc
, addr
, 1);
3012 static void handle_md_output(struct command_context
*cmd_ctx
,
3013 struct target
*target
, target_addr_t address
, unsigned size
,
3014 unsigned count
, const uint8_t *buffer
)
3016 const unsigned line_bytecnt
= 32;
3017 unsigned line_modulo
= line_bytecnt
/ size
;
3019 char output
[line_bytecnt
* 4 + 1];
3020 unsigned output_len
= 0;
3022 const char *value_fmt
;
3025 value_fmt
= "%16.16"PRIx64
" ";
3028 value_fmt
= "%8.8"PRIx64
" ";
3031 value_fmt
= "%4.4"PRIx64
" ";
3034 value_fmt
= "%2.2"PRIx64
" ";
3037 /* "can't happen", caller checked */
3038 LOG_ERROR("invalid memory read size: %u", size
);
3042 for (unsigned i
= 0; i
< count
; i
++) {
3043 if (i
% line_modulo
== 0) {
3044 output_len
+= snprintf(output
+ output_len
,
3045 sizeof(output
) - output_len
,
3046 TARGET_ADDR_FMT
": ",
3047 (address
+ (i
* size
)));
3051 const uint8_t *value_ptr
= buffer
+ i
* size
;
3054 value
= target_buffer_get_u64(target
, value_ptr
);
3057 value
= target_buffer_get_u32(target
, value_ptr
);
3060 value
= target_buffer_get_u16(target
, value_ptr
);
3065 output_len
+= snprintf(output
+ output_len
,
3066 sizeof(output
) - output_len
,
3069 if ((i
% line_modulo
== line_modulo
- 1) || (i
== count
- 1)) {
3070 command_print(cmd_ctx
, "%s", output
);
3076 COMMAND_HANDLER(handle_md_command
)
3079 return ERROR_COMMAND_SYNTAX_ERROR
;
3082 switch (CMD_NAME
[2]) {
3096 return ERROR_COMMAND_SYNTAX_ERROR
;
3099 bool physical
= strcmp(CMD_ARGV
[0], "phys") == 0;
3100 int (*fn
)(struct target
*target
,
3101 target_addr_t address
, uint32_t size_value
, uint32_t count
, uint8_t *buffer
);
3105 fn
= target_read_phys_memory
;
3107 fn
= target_read_memory
;
3108 if ((CMD_ARGC
< 1) || (CMD_ARGC
> 2))
3109 return ERROR_COMMAND_SYNTAX_ERROR
;
3111 target_addr_t address
;
3112 COMMAND_PARSE_ADDRESS(CMD_ARGV
[0], address
);
3116 COMMAND_PARSE_NUMBER(uint
, CMD_ARGV
[1], count
);
3118 uint8_t *buffer
= calloc(count
, size
);
3119 if (buffer
== NULL
) {
3120 LOG_ERROR("Failed to allocate md read buffer");
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
, uint32_t duration_ms
)
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 float sample_rate
= sampleNum
/ (duration_ms
/ 1000.0);
3914 writeLong(f
, sample_rate
, target
);
3915 writeString(f
, "seconds");
3916 for (i
= 0; i
< (15-strlen("seconds")); i
++)
3917 writeData(f
, &zero
, 1);
3918 writeString(f
, "s");
3920 /*append binary memory gmon.out profile_hist_data (profile_hist_data + profile_hist_hdr.hist_size) */
3922 char *data
= malloc(2 * numBuckets
);
3924 for (i
= 0; i
< numBuckets
; i
++) {
3929 data
[i
* 2] = val
&0xff;
3930 data
[i
* 2 + 1] = (val
>> 8) & 0xff;
3933 writeData(f
, data
, numBuckets
* 2);
3941 /* profiling samples the CPU PC as quickly as OpenOCD is able,
3942 * which will be used as a random sampling of PC */
3943 COMMAND_HANDLER(handle_profile_command
)
3945 struct target
*target
= get_current_target(CMD_CTX
);
3947 if ((CMD_ARGC
!= 2) && (CMD_ARGC
!= 4))
3948 return ERROR_COMMAND_SYNTAX_ERROR
;
3950 const uint32_t MAX_PROFILE_SAMPLE_NUM
= 10000;
3952 uint32_t num_of_samples
;
3953 int retval
= ERROR_OK
;
3955 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], offset
);
3957 uint32_t *samples
= malloc(sizeof(uint32_t) * MAX_PROFILE_SAMPLE_NUM
);
3958 if (samples
== NULL
) {
3959 LOG_ERROR("No memory to store samples.");
3963 uint64_t timestart_ms
= timeval_ms();
3965 * Some cores let us sample the PC without the
3966 * annoying halt/resume step; for example, ARMv7 PCSR.
3967 * Provide a way to use that more efficient mechanism.
3969 retval
= target_profiling(target
, samples
, MAX_PROFILE_SAMPLE_NUM
,
3970 &num_of_samples
, offset
);
3971 if (retval
!= ERROR_OK
) {
3975 uint32_t duration_ms
= timeval_ms() - timestart_ms
;
3977 assert(num_of_samples
<= MAX_PROFILE_SAMPLE_NUM
);
3979 retval
= target_poll(target
);
3980 if (retval
!= ERROR_OK
) {
3984 if (target
->state
== TARGET_RUNNING
) {
3985 retval
= target_halt(target
);
3986 if (retval
!= ERROR_OK
) {
3992 retval
= target_poll(target
);
3993 if (retval
!= ERROR_OK
) {
3998 uint32_t start_address
= 0;
3999 uint32_t end_address
= 0;
4000 bool with_range
= false;
4001 if (CMD_ARGC
== 4) {
4003 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[2], start_address
);
4004 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[3], end_address
);
4007 write_gmon(samples
, num_of_samples
, CMD_ARGV
[1],
4008 with_range
, start_address
, end_address
, target
, duration_ms
);
4009 command_print(CMD_CTX
, "Wrote %s", CMD_ARGV
[1]);
4015 static int new_int_array_element(Jim_Interp
*interp
, const char *varname
, int idx
, uint32_t val
)
4018 Jim_Obj
*nameObjPtr
, *valObjPtr
;
4021 namebuf
= alloc_printf("%s(%d)", varname
, idx
);
4025 nameObjPtr
= Jim_NewStringObj(interp
, namebuf
, -1);
4026 valObjPtr
= Jim_NewIntObj(interp
, val
);
4027 if (!nameObjPtr
|| !valObjPtr
) {
4032 Jim_IncrRefCount(nameObjPtr
);
4033 Jim_IncrRefCount(valObjPtr
);
4034 result
= Jim_SetVariable(interp
, nameObjPtr
, valObjPtr
);
4035 Jim_DecrRefCount(interp
, nameObjPtr
);
4036 Jim_DecrRefCount(interp
, valObjPtr
);
4038 /* printf("%s(%d) <= 0%08x\n", varname, idx, val); */
4042 static int jim_mem2array(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4044 struct command_context
*context
;
4045 struct target
*target
;
4047 context
= current_command_context(interp
);
4048 assert(context
!= NULL
);
4050 target
= get_current_target(context
);
4051 if (target
== NULL
) {
4052 LOG_ERROR("mem2array: no current target");
4056 return target_mem2array(interp
, target
, argc
- 1, argv
+ 1);
4059 static int target_mem2array(Jim_Interp
*interp
, struct target
*target
, int argc
, Jim_Obj
*const *argv
)
4067 const char *varname
;
4073 /* argv[1] = name of array to receive the data
4074 * argv[2] = desired width
4075 * argv[3] = memory address
4076 * argv[4] = count of times to read
4078 if (argc
< 4 || argc
> 5) {
4079 Jim_WrongNumArgs(interp
, 1, argv
, "varname width addr nelems [phys]");
4082 varname
= Jim_GetString(argv
[0], &len
);
4083 /* given "foo" get space for worse case "foo(%d)" .. add 20 */
4085 e
= Jim_GetLong(interp
, argv
[1], &l
);
4090 e
= Jim_GetLong(interp
, argv
[2], &l
);
4094 e
= Jim_GetLong(interp
, argv
[3], &l
);
4100 phys
= Jim_GetString(argv
[4], &n
);
4101 if (!strncmp(phys
, "phys", n
))
4117 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
4118 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "Invalid width param, must be 8/16/32", NULL
);
4122 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
4123 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "mem2array: zero width read?", NULL
);
4126 if ((addr
+ (len
* width
)) < addr
) {
4127 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
4128 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "mem2array: addr + len - wraps to zero?", NULL
);
4131 /* absurd transfer size? */
4133 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
4134 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "mem2array: absurd > 64K item request", NULL
);
4139 ((width
== 2) && ((addr
& 1) == 0)) ||
4140 ((width
== 4) && ((addr
& 3) == 0))) {
4144 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
4145 sprintf(buf
, "mem2array address: 0x%08" PRIx32
" is not aligned for %" PRId32
" byte reads",
4148 Jim_AppendStrings(interp
, Jim_GetResult(interp
), buf
, NULL
);
4157 size_t buffersize
= 4096;
4158 uint8_t *buffer
= malloc(buffersize
);
4165 /* Slurp... in buffer size chunks */
4167 count
= len
; /* in objects.. */
4168 if (count
> (buffersize
/ width
))
4169 count
= (buffersize
/ width
);
4172 retval
= target_read_phys_memory(target
, addr
, width
, count
, buffer
);
4174 retval
= target_read_memory(target
, addr
, width
, count
, buffer
);
4175 if (retval
!= ERROR_OK
) {
4177 LOG_ERROR("mem2array: Read @ 0x%08" PRIx32
", w=%" PRId32
", cnt=%" PRId32
", failed",
4181 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
4182 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "mem2array: cannot read memory", NULL
);
4186 v
= 0; /* shut up gcc */
4187 for (i
= 0; i
< count
; i
++, n
++) {
4190 v
= target_buffer_get_u32(target
, &buffer
[i
*width
]);
4193 v
= target_buffer_get_u16(target
, &buffer
[i
*width
]);
4196 v
= buffer
[i
] & 0x0ff;
4199 new_int_array_element(interp
, varname
, n
, v
);
4202 addr
+= count
* width
;
4208 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
4213 static int get_int_array_element(Jim_Interp
*interp
, const char *varname
, int idx
, uint32_t *val
)
4216 Jim_Obj
*nameObjPtr
, *valObjPtr
;
4220 namebuf
= alloc_printf("%s(%d)", varname
, idx
);
4224 nameObjPtr
= Jim_NewStringObj(interp
, namebuf
, -1);
4230 Jim_IncrRefCount(nameObjPtr
);
4231 valObjPtr
= Jim_GetVariable(interp
, nameObjPtr
, JIM_ERRMSG
);
4232 Jim_DecrRefCount(interp
, nameObjPtr
);
4234 if (valObjPtr
== NULL
)
4237 result
= Jim_GetLong(interp
, valObjPtr
, &l
);
4238 /* printf("%s(%d) => 0%08x\n", varname, idx, val); */
4243 static int jim_array2mem(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4245 struct command_context
*context
;
4246 struct target
*target
;
4248 context
= current_command_context(interp
);
4249 assert(context
!= NULL
);
4251 target
= get_current_target(context
);
4252 if (target
== NULL
) {
4253 LOG_ERROR("array2mem: no current target");
4257 return target_array2mem(interp
, target
, argc
-1, argv
+ 1);
4260 static int target_array2mem(Jim_Interp
*interp
, struct target
*target
,
4261 int argc
, Jim_Obj
*const *argv
)
4269 const char *varname
;
4275 /* argv[1] = name of array to get the data
4276 * argv[2] = desired width
4277 * argv[3] = memory address
4278 * argv[4] = count to write
4280 if (argc
< 4 || argc
> 5) {
4281 Jim_WrongNumArgs(interp
, 0, argv
, "varname width addr nelems [phys]");
4284 varname
= Jim_GetString(argv
[0], &len
);
4285 /* given "foo" get space for worse case "foo(%d)" .. add 20 */
4287 e
= Jim_GetLong(interp
, argv
[1], &l
);
4292 e
= Jim_GetLong(interp
, argv
[2], &l
);
4296 e
= Jim_GetLong(interp
, argv
[3], &l
);
4302 phys
= Jim_GetString(argv
[4], &n
);
4303 if (!strncmp(phys
, "phys", n
))
4319 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
4320 Jim_AppendStrings(interp
, Jim_GetResult(interp
),
4321 "Invalid width param, must be 8/16/32", NULL
);
4325 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
4326 Jim_AppendStrings(interp
, Jim_GetResult(interp
),
4327 "array2mem: zero width read?", NULL
);
4330 if ((addr
+ (len
* width
)) < addr
) {
4331 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
4332 Jim_AppendStrings(interp
, Jim_GetResult(interp
),
4333 "array2mem: addr + len - wraps to zero?", NULL
);
4336 /* absurd transfer size? */
4338 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
4339 Jim_AppendStrings(interp
, Jim_GetResult(interp
),
4340 "array2mem: absurd > 64K item request", NULL
);
4345 ((width
== 2) && ((addr
& 1) == 0)) ||
4346 ((width
== 4) && ((addr
& 3) == 0))) {
4350 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
4351 sprintf(buf
, "array2mem address: 0x%08" PRIx32
" is not aligned for %" PRId32
" byte reads",
4354 Jim_AppendStrings(interp
, Jim_GetResult(interp
), buf
, NULL
);
4365 size_t buffersize
= 4096;
4366 uint8_t *buffer
= malloc(buffersize
);
4371 /* Slurp... in buffer size chunks */
4373 count
= len
; /* in objects.. */
4374 if (count
> (buffersize
/ width
))
4375 count
= (buffersize
/ width
);
4377 v
= 0; /* shut up gcc */
4378 for (i
= 0; i
< count
; i
++, n
++) {
4379 get_int_array_element(interp
, varname
, n
, &v
);
4382 target_buffer_set_u32(target
, &buffer
[i
* width
], v
);
4385 target_buffer_set_u16(target
, &buffer
[i
* width
], v
);
4388 buffer
[i
] = v
& 0x0ff;
4395 retval
= target_write_phys_memory(target
, addr
, width
, count
, buffer
);
4397 retval
= target_write_memory(target
, addr
, width
, count
, buffer
);
4398 if (retval
!= ERROR_OK
) {
4400 LOG_ERROR("array2mem: Write @ 0x%08" PRIx32
", w=%" PRId32
", cnt=%" PRId32
", failed",
4404 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
4405 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "array2mem: cannot read memory", NULL
);
4409 addr
+= count
* width
;
4414 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
4419 /* FIX? should we propagate errors here rather than printing them
4422 void target_handle_event(struct target
*target
, enum target_event e
)
4424 struct target_event_action
*teap
;
4426 for (teap
= target
->event_action
; teap
!= NULL
; teap
= teap
->next
) {
4427 if (teap
->event
== e
) {
4428 LOG_DEBUG("target: (%d) %s (%s) event: %d (%s) action: %s",
4429 target
->target_number
,
4430 target_name(target
),
4431 target_type_name(target
),
4433 Jim_Nvp_value2name_simple(nvp_target_event
, e
)->name
,
4434 Jim_GetString(teap
->body
, NULL
));
4435 if (Jim_EvalObj(teap
->interp
, teap
->body
) != JIM_OK
) {
4436 Jim_MakeErrorMessage(teap
->interp
);
4437 command_print(NULL
, "%s\n", Jim_GetString(Jim_GetResult(teap
->interp
), NULL
));
4444 * Returns true only if the target has a handler for the specified event.
4446 bool target_has_event_action(struct target
*target
, enum target_event event
)
4448 struct target_event_action
*teap
;
4450 for (teap
= target
->event_action
; teap
!= NULL
; teap
= teap
->next
) {
4451 if (teap
->event
== event
)
4457 enum target_cfg_param
{
4460 TCFG_WORK_AREA_VIRT
,
4461 TCFG_WORK_AREA_PHYS
,
4462 TCFG_WORK_AREA_SIZE
,
4463 TCFG_WORK_AREA_BACKUP
,
4466 TCFG_CHAIN_POSITION
,
4473 static Jim_Nvp nvp_config_opts
[] = {
4474 { .name
= "-type", .value
= TCFG_TYPE
},
4475 { .name
= "-event", .value
= TCFG_EVENT
},
4476 { .name
= "-work-area-virt", .value
= TCFG_WORK_AREA_VIRT
},
4477 { .name
= "-work-area-phys", .value
= TCFG_WORK_AREA_PHYS
},
4478 { .name
= "-work-area-size", .value
= TCFG_WORK_AREA_SIZE
},
4479 { .name
= "-work-area-backup", .value
= TCFG_WORK_AREA_BACKUP
},
4480 { .name
= "-endian" , .value
= TCFG_ENDIAN
},
4481 { .name
= "-coreid", .value
= TCFG_COREID
},
4482 { .name
= "-chain-position", .value
= TCFG_CHAIN_POSITION
},
4483 { .name
= "-dbgbase", .value
= TCFG_DBGBASE
},
4484 { .name
= "-ctibase", .value
= TCFG_CTIBASE
},
4485 { .name
= "-rtos", .value
= TCFG_RTOS
},
4486 { .name
= "-defer-examine", .value
= TCFG_DEFER_EXAMINE
},
4487 { .name
= NULL
, .value
= -1 }
4490 static int target_configure(Jim_GetOptInfo
*goi
, struct target
*target
)
4497 /* parse config or cget options ... */
4498 while (goi
->argc
> 0) {
4499 Jim_SetEmptyResult(goi
->interp
);
4500 /* Jim_GetOpt_Debug(goi); */
4502 if (target
->type
->target_jim_configure
) {
4503 /* target defines a configure function */
4504 /* target gets first dibs on parameters */
4505 e
= (*(target
->type
->target_jim_configure
))(target
, goi
);
4514 /* otherwise we 'continue' below */
4516 e
= Jim_GetOpt_Nvp(goi
, nvp_config_opts
, &n
);
4518 Jim_GetOpt_NvpUnknown(goi
, nvp_config_opts
, 0);
4524 if (goi
->isconfigure
) {
4525 Jim_SetResultFormatted(goi
->interp
,
4526 "not settable: %s", n
->name
);
4530 if (goi
->argc
!= 0) {
4531 Jim_WrongNumArgs(goi
->interp
,
4532 goi
->argc
, goi
->argv
,
4537 Jim_SetResultString(goi
->interp
,
4538 target_type_name(target
), -1);
4542 if (goi
->argc
== 0) {
4543 Jim_WrongNumArgs(goi
->interp
, goi
->argc
, goi
->argv
, "-event ?event-name? ...");
4547 e
= Jim_GetOpt_Nvp(goi
, nvp_target_event
, &n
);
4549 Jim_GetOpt_NvpUnknown(goi
, nvp_target_event
, 1);
4553 if (goi
->isconfigure
) {
4554 if (goi
->argc
!= 1) {
4555 Jim_WrongNumArgs(goi
->interp
, goi
->argc
, goi
->argv
, "-event ?event-name? ?EVENT-BODY?");
4559 if (goi
->argc
!= 0) {
4560 Jim_WrongNumArgs(goi
->interp
, goi
->argc
, goi
->argv
, "-event ?event-name?");
4566 struct target_event_action
*teap
;
4568 teap
= target
->event_action
;
4569 /* replace existing? */
4571 if (teap
->event
== (enum target_event
)n
->value
)
4576 if (goi
->isconfigure
) {
4577 bool replace
= true;
4580 teap
= calloc(1, sizeof(*teap
));
4583 teap
->event
= n
->value
;
4584 teap
->interp
= goi
->interp
;
4585 Jim_GetOpt_Obj(goi
, &o
);
4587 Jim_DecrRefCount(teap
->interp
, teap
->body
);
4588 teap
->body
= Jim_DuplicateObj(goi
->interp
, o
);
4591 * Tcl/TK - "tk events" have a nice feature.
4592 * See the "BIND" command.
4593 * We should support that here.
4594 * You can specify %X and %Y in the event code.
4595 * The idea is: %T - target name.
4596 * The idea is: %N - target number
4597 * The idea is: %E - event name.
4599 Jim_IncrRefCount(teap
->body
);
4602 /* add to head of event list */
4603 teap
->next
= target
->event_action
;
4604 target
->event_action
= teap
;
4606 Jim_SetEmptyResult(goi
->interp
);
4610 Jim_SetEmptyResult(goi
->interp
);
4612 Jim_SetResult(goi
->interp
, Jim_DuplicateObj(goi
->interp
, teap
->body
));
4618 case TCFG_WORK_AREA_VIRT
:
4619 if (goi
->isconfigure
) {
4620 target_free_all_working_areas(target
);
4621 e
= Jim_GetOpt_Wide(goi
, &w
);
4624 target
->working_area_virt
= w
;
4625 target
->working_area_virt_spec
= true;
4630 Jim_SetResult(goi
->interp
, Jim_NewIntObj(goi
->interp
, target
->working_area_virt
));
4634 case TCFG_WORK_AREA_PHYS
:
4635 if (goi
->isconfigure
) {
4636 target_free_all_working_areas(target
);
4637 e
= Jim_GetOpt_Wide(goi
, &w
);
4640 target
->working_area_phys
= w
;
4641 target
->working_area_phys_spec
= true;
4646 Jim_SetResult(goi
->interp
, Jim_NewIntObj(goi
->interp
, target
->working_area_phys
));
4650 case TCFG_WORK_AREA_SIZE
:
4651 if (goi
->isconfigure
) {
4652 target_free_all_working_areas(target
);
4653 e
= Jim_GetOpt_Wide(goi
, &w
);
4656 target
->working_area_size
= w
;
4661 Jim_SetResult(goi
->interp
, Jim_NewIntObj(goi
->interp
, target
->working_area_size
));
4665 case TCFG_WORK_AREA_BACKUP
:
4666 if (goi
->isconfigure
) {
4667 target_free_all_working_areas(target
);
4668 e
= Jim_GetOpt_Wide(goi
, &w
);
4671 /* make this exactly 1 or 0 */
4672 target
->backup_working_area
= (!!w
);
4677 Jim_SetResult(goi
->interp
, Jim_NewIntObj(goi
->interp
, target
->backup_working_area
));
4678 /* loop for more e*/
4683 if (goi
->isconfigure
) {
4684 e
= Jim_GetOpt_Nvp(goi
, nvp_target_endian
, &n
);
4686 Jim_GetOpt_NvpUnknown(goi
, nvp_target_endian
, 1);
4689 target
->endianness
= n
->value
;
4694 n
= Jim_Nvp_value2name_simple(nvp_target_endian
, target
->endianness
);
4695 if (n
->name
== NULL
) {
4696 target
->endianness
= TARGET_LITTLE_ENDIAN
;
4697 n
= Jim_Nvp_value2name_simple(nvp_target_endian
, target
->endianness
);
4699 Jim_SetResultString(goi
->interp
, n
->name
, -1);
4704 if (goi
->isconfigure
) {
4705 e
= Jim_GetOpt_Wide(goi
, &w
);
4708 target
->coreid
= (int32_t)w
;
4713 Jim_SetResult(goi
->interp
, Jim_NewIntObj(goi
->interp
, target
->working_area_size
));
4717 case TCFG_CHAIN_POSITION
:
4718 if (goi
->isconfigure
) {
4720 struct jtag_tap
*tap
;
4721 target_free_all_working_areas(target
);
4722 e
= Jim_GetOpt_Obj(goi
, &o_t
);
4725 tap
= jtag_tap_by_jim_obj(goi
->interp
, o_t
);
4728 /* make this exactly 1 or 0 */
4734 Jim_SetResultString(goi
->interp
, target
->tap
->dotted_name
, -1);
4735 /* loop for more e*/
4738 if (goi
->isconfigure
) {
4739 e
= Jim_GetOpt_Wide(goi
, &w
);
4742 target
->dbgbase
= (uint32_t)w
;
4743 target
->dbgbase_set
= true;
4748 Jim_SetResult(goi
->interp
, Jim_NewIntObj(goi
->interp
, target
->dbgbase
));
4752 if (goi
->isconfigure
) {
4753 e
= Jim_GetOpt_Wide(goi
, &w
);
4756 target
->ctibase
= (uint32_t)w
;
4757 target
->ctibase_set
= true;
4762 Jim_SetResult(goi
->interp
, Jim_NewIntObj(goi
->interp
, target
->ctibase
));
4768 int result
= rtos_create(goi
, target
);
4769 if (result
!= JIM_OK
)
4775 case TCFG_DEFER_EXAMINE
:
4777 target
->defer_examine
= true;
4782 } /* while (goi->argc) */
4785 /* done - we return */
4789 static int jim_target_configure(Jim_Interp
*interp
, int argc
, Jim_Obj
* const *argv
)
4793 Jim_GetOpt_Setup(&goi
, interp
, argc
- 1, argv
+ 1);
4794 goi
.isconfigure
= !strcmp(Jim_GetString(argv
[0], NULL
), "configure");
4796 Jim_WrongNumArgs(goi
.interp
, goi
.argc
, goi
.argv
,
4797 "missing: -option ...");
4800 struct target
*target
= Jim_CmdPrivData(goi
.interp
);
4801 return target_configure(&goi
, target
);
4804 static int jim_target_mw(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4806 const char *cmd_name
= Jim_GetString(argv
[0], NULL
);
4809 Jim_GetOpt_Setup(&goi
, interp
, argc
- 1, argv
+ 1);
4811 if (goi
.argc
< 2 || goi
.argc
> 4) {
4812 Jim_SetResultFormatted(goi
.interp
,
4813 "usage: %s [phys] <address> <data> [<count>]", cmd_name
);
4818 fn
= target_write_memory
;
4821 if (strcmp(Jim_GetString(argv
[1], NULL
), "phys") == 0) {
4823 struct Jim_Obj
*obj
;
4824 e
= Jim_GetOpt_Obj(&goi
, &obj
);
4828 fn
= target_write_phys_memory
;
4832 e
= Jim_GetOpt_Wide(&goi
, &a
);
4837 e
= Jim_GetOpt_Wide(&goi
, &b
);
4842 if (goi
.argc
== 1) {
4843 e
= Jim_GetOpt_Wide(&goi
, &c
);
4848 /* all args must be consumed */
4852 struct target
*target
= Jim_CmdPrivData(goi
.interp
);
4854 if (strcasecmp(cmd_name
, "mww") == 0)
4856 else if (strcasecmp(cmd_name
, "mwh") == 0)
4858 else if (strcasecmp(cmd_name
, "mwb") == 0)
4861 LOG_ERROR("command '%s' unknown: ", cmd_name
);
4865 return (target_fill_mem(target
, a
, fn
, data_size
, b
, c
) == ERROR_OK
) ? JIM_OK
: JIM_ERR
;
4869 * @brief Reads an array of words/halfwords/bytes from target memory starting at specified address.
4871 * Usage: mdw [phys] <address> [<count>] - for 32 bit reads
4872 * mdh [phys] <address> [<count>] - for 16 bit reads
4873 * mdb [phys] <address> [<count>] - for 8 bit reads
4875 * Count defaults to 1.
4877 * Calls target_read_memory or target_read_phys_memory depending on
4878 * the presence of the "phys" argument
4879 * Reads the target memory in blocks of max. 32 bytes, and returns an array of ints formatted
4880 * to int representation in base16.
4881 * Also outputs read data in a human readable form using command_print
4883 * @param phys if present target_read_phys_memory will be used instead of target_read_memory
4884 * @param address address where to start the read. May be specified in decimal or hex using the standard "0x" prefix
4885 * @param count optional count parameter to read an array of values. If not specified, defaults to 1.
4886 * @returns: JIM_ERR on error or JIM_OK on success and sets the result string to an array of ascii formatted numbers
4887 * on success, with [<count>] number of elements.
4889 * In case of little endian target:
4890 * Example1: "mdw 0x00000000" returns "10123456"
4891 * Exmaple2: "mdh 0x00000000 1" returns "3456"
4892 * Example3: "mdb 0x00000000" returns "56"
4893 * Example4: "mdh 0x00000000 2" returns "3456 1012"
4894 * Example5: "mdb 0x00000000 3" returns "56 34 12"
4896 static int jim_target_md(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4898 const char *cmd_name
= Jim_GetString(argv
[0], NULL
);
4901 Jim_GetOpt_Setup(&goi
, interp
, argc
- 1, argv
+ 1);
4903 if ((goi
.argc
< 1) || (goi
.argc
> 3)) {
4904 Jim_SetResultFormatted(goi
.interp
,
4905 "usage: %s [phys] <address> [<count>]", cmd_name
);
4909 int (*fn
)(struct target
*target
,
4910 target_addr_t address
, uint32_t size
, uint32_t count
, uint8_t *buffer
);
4911 fn
= target_read_memory
;
4914 if (strcmp(Jim_GetString(argv
[1], NULL
), "phys") == 0) {
4916 struct Jim_Obj
*obj
;
4917 e
= Jim_GetOpt_Obj(&goi
, &obj
);
4921 fn
= target_read_phys_memory
;
4924 /* Read address parameter */
4926 e
= Jim_GetOpt_Wide(&goi
, &addr
);
4930 /* If next parameter exists, read it out as the count parameter, if not, set it to 1 (default) */
4932 if (goi
.argc
== 1) {
4933 e
= Jim_GetOpt_Wide(&goi
, &count
);
4939 /* all args must be consumed */
4943 jim_wide dwidth
= 1; /* shut up gcc */
4944 if (strcasecmp(cmd_name
, "mdw") == 0)
4946 else if (strcasecmp(cmd_name
, "mdh") == 0)
4948 else if (strcasecmp(cmd_name
, "mdb") == 0)
4951 LOG_ERROR("command '%s' unknown: ", cmd_name
);
4955 /* convert count to "bytes" */
4956 int bytes
= count
* dwidth
;
4958 struct target
*target
= Jim_CmdPrivData(goi
.interp
);
4959 uint8_t target_buf
[32];
4962 y
= (bytes
< 16) ? bytes
: 16; /* y = min(bytes, 16); */
4964 /* Try to read out next block */
4965 e
= fn(target
, addr
, dwidth
, y
/ dwidth
, target_buf
);
4967 if (e
!= ERROR_OK
) {
4968 Jim_SetResultFormatted(interp
, "error reading target @ 0x%08lx", (long)addr
);
4972 command_print_sameline(NULL
, "0x%08x ", (int)(addr
));
4975 for (x
= 0; x
< 16 && x
< y
; x
+= 4) {
4976 z
= target_buffer_get_u32(target
, &(target_buf
[x
]));
4977 command_print_sameline(NULL
, "%08x ", (int)(z
));
4979 for (; (x
< 16) ; x
+= 4)
4980 command_print_sameline(NULL
, " ");
4983 for (x
= 0; x
< 16 && x
< y
; x
+= 2) {
4984 z
= target_buffer_get_u16(target
, &(target_buf
[x
]));
4985 command_print_sameline(NULL
, "%04x ", (int)(z
));
4987 for (; (x
< 16) ; x
+= 2)
4988 command_print_sameline(NULL
, " ");
4992 for (x
= 0 ; (x
< 16) && (x
< y
) ; x
+= 1) {
4993 z
= target_buffer_get_u8(target
, &(target_buf
[x
]));
4994 command_print_sameline(NULL
, "%02x ", (int)(z
));
4996 for (; (x
< 16) ; x
+= 1)
4997 command_print_sameline(NULL
, " ");
5000 /* ascii-ify the bytes */
5001 for (x
= 0 ; x
< y
; x
++) {
5002 if ((target_buf
[x
] >= 0x20) &&
5003 (target_buf
[x
] <= 0x7e)) {
5007 target_buf
[x
] = '.';
5012 target_buf
[x
] = ' ';
5017 /* print - with a newline */
5018 command_print_sameline(NULL
, "%s\n", target_buf
);
5026 static int jim_target_mem2array(Jim_Interp
*interp
,
5027 int argc
, Jim_Obj
*const *argv
)
5029 struct target
*target
= Jim_CmdPrivData(interp
);
5030 return target_mem2array(interp
, target
, argc
- 1, argv
+ 1);
5033 static int jim_target_array2mem(Jim_Interp
*interp
,
5034 int argc
, Jim_Obj
*const *argv
)
5036 struct target
*target
= Jim_CmdPrivData(interp
);
5037 return target_array2mem(interp
, target
, argc
- 1, argv
+ 1);
5040 static int jim_target_tap_disabled(Jim_Interp
*interp
)
5042 Jim_SetResultFormatted(interp
, "[TAP is disabled]");
5046 static int jim_target_examine(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
5048 bool allow_defer
= false;
5051 Jim_GetOpt_Setup(&goi
, interp
, argc
- 1, argv
+ 1);
5053 const char *cmd_name
= Jim_GetString(argv
[0], NULL
);
5054 Jim_SetResultFormatted(goi
.interp
,
5055 "usage: %s ['allow-defer']", cmd_name
);
5059 strcmp(Jim_GetString(argv
[1], NULL
), "allow-defer") == 0) {
5061 struct Jim_Obj
*obj
;
5062 int e
= Jim_GetOpt_Obj(&goi
, &obj
);
5068 struct target
*target
= Jim_CmdPrivData(interp
);
5069 if (!target
->tap
->enabled
)
5070 return jim_target_tap_disabled(interp
);
5072 if (allow_defer
&& target
->defer_examine
) {
5073 LOG_INFO("Deferring arp_examine of %s", target_name(target
));
5074 LOG_INFO("Use arp_examine command to examine it manually!");
5078 int e
= target
->type
->examine(target
);
5084 static int jim_target_was_examined(Jim_Interp
*interp
, int argc
, Jim_Obj
* const *argv
)
5086 struct target
*target
= Jim_CmdPrivData(interp
);
5088 Jim_SetResultBool(interp
, target_was_examined(target
));
5092 static int jim_target_examine_deferred(Jim_Interp
*interp
, int argc
, Jim_Obj
* const *argv
)
5094 struct target
*target
= Jim_CmdPrivData(interp
);
5096 Jim_SetResultBool(interp
, target
->defer_examine
);
5100 static int jim_target_halt_gdb(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
5103 Jim_WrongNumArgs(interp
, 1, argv
, "[no parameters]");
5106 struct target
*target
= Jim_CmdPrivData(interp
);
5108 if (target_call_event_callbacks(target
, TARGET_EVENT_GDB_HALT
) != ERROR_OK
)
5114 static int jim_target_poll(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
5117 Jim_WrongNumArgs(interp
, 1, argv
, "[no parameters]");
5120 struct target
*target
= Jim_CmdPrivData(interp
);
5121 if (!target
->tap
->enabled
)
5122 return jim_target_tap_disabled(interp
);
5125 if (!(target_was_examined(target
)))
5126 e
= ERROR_TARGET_NOT_EXAMINED
;
5128 e
= target
->type
->poll(target
);
5134 static int jim_target_reset(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
5137 Jim_GetOpt_Setup(&goi
, interp
, argc
- 1, argv
+ 1);
5139 if (goi
.argc
!= 2) {
5140 Jim_WrongNumArgs(interp
, 0, argv
,
5141 "([tT]|[fF]|assert|deassert) BOOL");
5146 int e
= Jim_GetOpt_Nvp(&goi
, nvp_assert
, &n
);
5148 Jim_GetOpt_NvpUnknown(&goi
, nvp_assert
, 1);
5151 /* the halt or not param */
5153 e
= Jim_GetOpt_Wide(&goi
, &a
);
5157 struct target
*target
= Jim_CmdPrivData(goi
.interp
);
5158 if (!target
->tap
->enabled
)
5159 return jim_target_tap_disabled(interp
);
5161 if (!target
->type
->assert_reset
|| !target
->type
->deassert_reset
) {
5162 Jim_SetResultFormatted(interp
,
5163 "No target-specific reset for %s",
5164 target_name(target
));
5168 if (target
->defer_examine
)
5169 target_reset_examined(target
);
5171 /* determine if we should halt or not. */
5172 target
->reset_halt
= !!a
;
5173 /* When this happens - all workareas are invalid. */
5174 target_free_all_working_areas_restore(target
, 0);
5177 if (n
->value
== NVP_ASSERT
)
5178 e
= target
->type
->assert_reset(target
);
5180 e
= target
->type
->deassert_reset(target
);
5181 return (e
== ERROR_OK
) ? JIM_OK
: JIM_ERR
;
5184 static int jim_target_halt(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
5187 Jim_WrongNumArgs(interp
, 1, argv
, "[no parameters]");
5190 struct target
*target
= Jim_CmdPrivData(interp
);
5191 if (!target
->tap
->enabled
)
5192 return jim_target_tap_disabled(interp
);
5193 int e
= target
->type
->halt(target
);
5194 return (e
== ERROR_OK
) ? JIM_OK
: JIM_ERR
;
5197 static int jim_target_wait_state(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
5200 Jim_GetOpt_Setup(&goi
, interp
, argc
- 1, argv
+ 1);
5202 /* params: <name> statename timeoutmsecs */
5203 if (goi
.argc
!= 2) {
5204 const char *cmd_name
= Jim_GetString(argv
[0], NULL
);
5205 Jim_SetResultFormatted(goi
.interp
,
5206 "%s <state_name> <timeout_in_msec>", cmd_name
);
5211 int e
= Jim_GetOpt_Nvp(&goi
, nvp_target_state
, &n
);
5213 Jim_GetOpt_NvpUnknown(&goi
, nvp_target_state
, 1);
5217 e
= Jim_GetOpt_Wide(&goi
, &a
);
5220 struct target
*target
= Jim_CmdPrivData(interp
);
5221 if (!target
->tap
->enabled
)
5222 return jim_target_tap_disabled(interp
);
5224 e
= target_wait_state(target
, n
->value
, a
);
5225 if (e
!= ERROR_OK
) {
5226 Jim_Obj
*eObj
= Jim_NewIntObj(interp
, e
);
5227 Jim_SetResultFormatted(goi
.interp
,
5228 "target: %s wait %s fails (%#s) %s",
5229 target_name(target
), n
->name
,
5230 eObj
, target_strerror_safe(e
));
5231 Jim_FreeNewObj(interp
, eObj
);
5236 /* List for human, Events defined for this target.
5237 * scripts/programs should use 'name cget -event NAME'
5239 static int jim_target_event_list(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
5241 struct command_context
*cmd_ctx
= current_command_context(interp
);
5242 assert(cmd_ctx
!= NULL
);
5244 struct target
*target
= Jim_CmdPrivData(interp
);
5245 struct target_event_action
*teap
= target
->event_action
;
5246 command_print(cmd_ctx
, "Event actions for target (%d) %s\n",
5247 target
->target_number
,
5248 target_name(target
));
5249 command_print(cmd_ctx
, "%-25s | Body", "Event");
5250 command_print(cmd_ctx
, "------------------------- | "
5251 "----------------------------------------");
5253 Jim_Nvp
*opt
= Jim_Nvp_value2name_simple(nvp_target_event
, teap
->event
);
5254 command_print(cmd_ctx
, "%-25s | %s",
5255 opt
->name
, Jim_GetString(teap
->body
, NULL
));
5258 command_print(cmd_ctx
, "***END***");
5261 static int jim_target_current_state(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
5264 Jim_WrongNumArgs(interp
, 1, argv
, "[no parameters]");
5267 struct target
*target
= Jim_CmdPrivData(interp
);
5268 Jim_SetResultString(interp
, target_state_name(target
), -1);
5271 static int jim_target_invoke_event(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
5274 Jim_GetOpt_Setup(&goi
, interp
, argc
- 1, argv
+ 1);
5275 if (goi
.argc
!= 1) {
5276 const char *cmd_name
= Jim_GetString(argv
[0], NULL
);
5277 Jim_SetResultFormatted(goi
.interp
, "%s <eventname>", cmd_name
);
5281 int e
= Jim_GetOpt_Nvp(&goi
, nvp_target_event
, &n
);
5283 Jim_GetOpt_NvpUnknown(&goi
, nvp_target_event
, 1);
5286 struct target
*target
= Jim_CmdPrivData(interp
);
5287 target_handle_event(target
, n
->value
);
5291 static const struct command_registration target_instance_command_handlers
[] = {
5293 .name
= "configure",
5294 .mode
= COMMAND_CONFIG
,
5295 .jim_handler
= jim_target_configure
,
5296 .help
= "configure a new target for use",
5297 .usage
= "[target_attribute ...]",
5301 .mode
= COMMAND_ANY
,
5302 .jim_handler
= jim_target_configure
,
5303 .help
= "returns the specified target attribute",
5304 .usage
= "target_attribute",
5308 .mode
= COMMAND_EXEC
,
5309 .jim_handler
= jim_target_mw
,
5310 .help
= "Write 32-bit word(s) to target memory",
5311 .usage
= "address data [count]",
5315 .mode
= COMMAND_EXEC
,
5316 .jim_handler
= jim_target_mw
,
5317 .help
= "Write 16-bit half-word(s) to target memory",
5318 .usage
= "address data [count]",
5322 .mode
= COMMAND_EXEC
,
5323 .jim_handler
= jim_target_mw
,
5324 .help
= "Write byte(s) to target memory",
5325 .usage
= "address data [count]",
5329 .mode
= COMMAND_EXEC
,
5330 .jim_handler
= jim_target_md
,
5331 .help
= "Display target memory as 32-bit words",
5332 .usage
= "address [count]",
5336 .mode
= COMMAND_EXEC
,
5337 .jim_handler
= jim_target_md
,
5338 .help
= "Display target memory as 16-bit half-words",
5339 .usage
= "address [count]",
5343 .mode
= COMMAND_EXEC
,
5344 .jim_handler
= jim_target_md
,
5345 .help
= "Display target memory as 8-bit bytes",
5346 .usage
= "address [count]",
5349 .name
= "array2mem",
5350 .mode
= COMMAND_EXEC
,
5351 .jim_handler
= jim_target_array2mem
,
5352 .help
= "Writes Tcl array of 8/16/32 bit numbers "
5354 .usage
= "arrayname bitwidth address count",
5357 .name
= "mem2array",
5358 .mode
= COMMAND_EXEC
,
5359 .jim_handler
= jim_target_mem2array
,
5360 .help
= "Loads Tcl array of 8/16/32 bit numbers "
5361 "from target memory",
5362 .usage
= "arrayname bitwidth address count",
5365 .name
= "eventlist",
5366 .mode
= COMMAND_EXEC
,
5367 .jim_handler
= jim_target_event_list
,
5368 .help
= "displays a table of events defined for this target",
5372 .mode
= COMMAND_EXEC
,
5373 .jim_handler
= jim_target_current_state
,
5374 .help
= "displays the current state of this target",
5377 .name
= "arp_examine",
5378 .mode
= COMMAND_EXEC
,
5379 .jim_handler
= jim_target_examine
,
5380 .help
= "used internally for reset processing",
5381 .usage
= "arp_examine ['allow-defer']",
5384 .name
= "was_examined",
5385 .mode
= COMMAND_EXEC
,
5386 .jim_handler
= jim_target_was_examined
,
5387 .help
= "used internally for reset processing",
5388 .usage
= "was_examined",
5391 .name
= "examine_deferred",
5392 .mode
= COMMAND_EXEC
,
5393 .jim_handler
= jim_target_examine_deferred
,
5394 .help
= "used internally for reset processing",
5395 .usage
= "examine_deferred",
5398 .name
= "arp_halt_gdb",
5399 .mode
= COMMAND_EXEC
,
5400 .jim_handler
= jim_target_halt_gdb
,
5401 .help
= "used internally for reset processing to halt GDB",
5405 .mode
= COMMAND_EXEC
,
5406 .jim_handler
= jim_target_poll
,
5407 .help
= "used internally for reset processing",
5410 .name
= "arp_reset",
5411 .mode
= COMMAND_EXEC
,
5412 .jim_handler
= jim_target_reset
,
5413 .help
= "used internally for reset processing",
5417 .mode
= COMMAND_EXEC
,
5418 .jim_handler
= jim_target_halt
,
5419 .help
= "used internally for reset processing",
5422 .name
= "arp_waitstate",
5423 .mode
= COMMAND_EXEC
,
5424 .jim_handler
= jim_target_wait_state
,
5425 .help
= "used internally for reset processing",
5428 .name
= "invoke-event",
5429 .mode
= COMMAND_EXEC
,
5430 .jim_handler
= jim_target_invoke_event
,
5431 .help
= "invoke handler for specified event",
5432 .usage
= "event_name",
5434 COMMAND_REGISTRATION_DONE
5437 static int target_create(Jim_GetOptInfo
*goi
)
5444 struct target
*target
;
5445 struct command_context
*cmd_ctx
;
5447 cmd_ctx
= current_command_context(goi
->interp
);
5448 assert(cmd_ctx
!= NULL
);
5450 if (goi
->argc
< 3) {
5451 Jim_WrongNumArgs(goi
->interp
, 1, goi
->argv
, "?name? ?type? ..options...");
5456 Jim_GetOpt_Obj(goi
, &new_cmd
);
5457 /* does this command exist? */
5458 cmd
= Jim_GetCommand(goi
->interp
, new_cmd
, JIM_ERRMSG
);
5460 cp
= Jim_GetString(new_cmd
, NULL
);
5461 Jim_SetResultFormatted(goi
->interp
, "Command/target: %s Exists", cp
);
5466 e
= Jim_GetOpt_String(goi
, &cp
, NULL
);
5469 struct transport
*tr
= get_current_transport();
5470 if (tr
->override_target
) {
5471 e
= tr
->override_target(&cp
);
5472 if (e
!= ERROR_OK
) {
5473 LOG_ERROR("The selected transport doesn't support this target");
5476 LOG_INFO("The selected transport took over low-level target control. The results might differ compared to plain JTAG/SWD");
5478 /* now does target type exist */
5479 for (x
= 0 ; target_types
[x
] ; x
++) {
5480 if (0 == strcmp(cp
, target_types
[x
]->name
)) {
5485 /* check for deprecated name */
5486 if (target_types
[x
]->deprecated_name
) {
5487 if (0 == strcmp(cp
, target_types
[x
]->deprecated_name
)) {
5489 LOG_WARNING("target name is deprecated use: \'%s\'", target_types
[x
]->name
);
5494 if (target_types
[x
] == NULL
) {
5495 Jim_SetResultFormatted(goi
->interp
, "Unknown target type %s, try one of ", cp
);
5496 for (x
= 0 ; target_types
[x
] ; x
++) {
5497 if (target_types
[x
+ 1]) {
5498 Jim_AppendStrings(goi
->interp
,
5499 Jim_GetResult(goi
->interp
),
5500 target_types
[x
]->name
,
5503 Jim_AppendStrings(goi
->interp
,
5504 Jim_GetResult(goi
->interp
),
5506 target_types
[x
]->name
, NULL
);
5513 target
= calloc(1, sizeof(struct target
));
5514 /* set target number */
5515 target
->target_number
= new_target_number();
5516 cmd_ctx
->current_target
= target
->target_number
;
5518 /* allocate memory for each unique target type */
5519 target
->type
= calloc(1, sizeof(struct target_type
));
5521 memcpy(target
->type
, target_types
[x
], sizeof(struct target_type
));
5523 /* will be set by "-endian" */
5524 target
->endianness
= TARGET_ENDIAN_UNKNOWN
;
5526 /* default to first core, override with -coreid */
5529 target
->working_area
= 0x0;
5530 target
->working_area_size
= 0x0;
5531 target
->working_areas
= NULL
;
5532 target
->backup_working_area
= 0;
5534 target
->state
= TARGET_UNKNOWN
;
5535 target
->debug_reason
= DBG_REASON_UNDEFINED
;
5536 target
->reg_cache
= NULL
;
5537 target
->breakpoints
= NULL
;
5538 target
->watchpoints
= NULL
;
5539 target
->next
= NULL
;
5540 target
->arch_info
= NULL
;
5542 target
->display
= 1;
5544 target
->halt_issued
= false;
5546 /* initialize trace information */
5547 target
->trace_info
= calloc(1, sizeof(struct trace
));
5549 target
->dbgmsg
= NULL
;
5550 target
->dbg_msg_enabled
= 0;
5552 target
->endianness
= TARGET_ENDIAN_UNKNOWN
;
5554 target
->rtos
= NULL
;
5555 target
->rtos_auto_detect
= false;
5557 /* Do the rest as "configure" options */
5558 goi
->isconfigure
= 1;
5559 e
= target_configure(goi
, target
);
5561 if (target
->tap
== NULL
) {
5562 Jim_SetResultString(goi
->interp
, "-chain-position required when creating target", -1);
5572 if (target
->endianness
== TARGET_ENDIAN_UNKNOWN
) {
5573 /* default endian to little if not specified */
5574 target
->endianness
= TARGET_LITTLE_ENDIAN
;
5577 cp
= Jim_GetString(new_cmd
, NULL
);
5578 target
->cmd_name
= strdup(cp
);
5580 /* create the target specific commands */
5581 if (target
->type
->commands
) {
5582 e
= register_commands(cmd_ctx
, NULL
, target
->type
->commands
);
5584 LOG_ERROR("unable to register '%s' commands", cp
);
5586 if (target
->type
->target_create
)
5587 (*(target
->type
->target_create
))(target
, goi
->interp
);
5589 /* append to end of list */
5591 struct target
**tpp
;
5592 tpp
= &(all_targets
);
5594 tpp
= &((*tpp
)->next
);
5598 /* now - create the new target name command */
5599 const struct command_registration target_subcommands
[] = {
5601 .chain
= target_instance_command_handlers
,
5604 .chain
= target
->type
->commands
,
5606 COMMAND_REGISTRATION_DONE
5608 const struct command_registration target_commands
[] = {
5611 .mode
= COMMAND_ANY
,
5612 .help
= "target command group",
5614 .chain
= target_subcommands
,
5616 COMMAND_REGISTRATION_DONE
5618 e
= register_commands(cmd_ctx
, NULL
, target_commands
);
5622 struct command
*c
= command_find_in_context(cmd_ctx
, cp
);
5624 command_set_handler_data(c
, target
);
5626 return (ERROR_OK
== e
) ? JIM_OK
: JIM_ERR
;
5629 static int jim_target_current(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
5632 Jim_WrongNumArgs(interp
, 1, argv
, "Too many parameters");
5635 struct command_context
*cmd_ctx
= current_command_context(interp
);
5636 assert(cmd_ctx
!= NULL
);
5638 Jim_SetResultString(interp
, target_name(get_current_target(cmd_ctx
)), -1);
5642 static int jim_target_types(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
5645 Jim_WrongNumArgs(interp
, 1, argv
, "Too many parameters");
5648 Jim_SetResult(interp
, Jim_NewListObj(interp
, NULL
, 0));
5649 for (unsigned x
= 0; NULL
!= target_types
[x
]; x
++) {
5650 Jim_ListAppendElement(interp
, Jim_GetResult(interp
),
5651 Jim_NewStringObj(interp
, target_types
[x
]->name
, -1));
5656 static int jim_target_names(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
5659 Jim_WrongNumArgs(interp
, 1, argv
, "Too many parameters");
5662 Jim_SetResult(interp
, Jim_NewListObj(interp
, NULL
, 0));
5663 struct target
*target
= all_targets
;
5665 Jim_ListAppendElement(interp
, Jim_GetResult(interp
),
5666 Jim_NewStringObj(interp
, target_name(target
), -1));
5667 target
= target
->next
;
5672 static int jim_target_smp(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
5675 const char *targetname
;
5677 struct target
*target
= (struct target
*) NULL
;
5678 struct target_list
*head
, *curr
, *new;
5679 curr
= (struct target_list
*) NULL
;
5680 head
= (struct target_list
*) NULL
;
5683 LOG_DEBUG("%d", argc
);
5684 /* argv[1] = target to associate in smp
5685 * argv[2] = target to assoicate in smp
5689 for (i
= 1; i
< argc
; i
++) {
5691 targetname
= Jim_GetString(argv
[i
], &len
);
5692 target
= get_target(targetname
);
5693 LOG_DEBUG("%s ", targetname
);
5695 new = malloc(sizeof(struct target_list
));
5696 new->target
= target
;
5697 new->next
= (struct target_list
*)NULL
;
5698 if (head
== (struct target_list
*)NULL
) {
5707 /* now parse the list of cpu and put the target in smp mode*/
5710 while (curr
!= (struct target_list
*)NULL
) {
5711 target
= curr
->target
;
5713 target
->head
= head
;
5717 if (target
&& target
->rtos
)
5718 retval
= rtos_smp_init(head
->target
);
5724 static int jim_target_create(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
5727 Jim_GetOpt_Setup(&goi
, interp
, argc
- 1, argv
+ 1);
5729 Jim_WrongNumArgs(goi
.interp
, goi
.argc
, goi
.argv
,
5730 "<name> <target_type> [<target_options> ...]");
5733 return target_create(&goi
);
5736 static const struct command_registration target_subcommand_handlers
[] = {
5739 .mode
= COMMAND_CONFIG
,
5740 .handler
= handle_target_init_command
,
5741 .help
= "initialize targets",
5745 /* REVISIT this should be COMMAND_CONFIG ... */
5746 .mode
= COMMAND_ANY
,
5747 .jim_handler
= jim_target_create
,
5748 .usage
= "name type '-chain-position' name [options ...]",
5749 .help
= "Creates and selects a new target",
5753 .mode
= COMMAND_ANY
,
5754 .jim_handler
= jim_target_current
,
5755 .help
= "Returns the currently selected target",
5759 .mode
= COMMAND_ANY
,
5760 .jim_handler
= jim_target_types
,
5761 .help
= "Returns the available target types as "
5762 "a list of strings",
5766 .mode
= COMMAND_ANY
,
5767 .jim_handler
= jim_target_names
,
5768 .help
= "Returns the names of all targets as a list of strings",
5772 .mode
= COMMAND_ANY
,
5773 .jim_handler
= jim_target_smp
,
5774 .usage
= "targetname1 targetname2 ...",
5775 .help
= "gather several target in a smp list"
5778 COMMAND_REGISTRATION_DONE
5782 target_addr_t address
;
5788 static int fastload_num
;
5789 static struct FastLoad
*fastload
;
5791 static void free_fastload(void)
5793 if (fastload
!= NULL
) {
5795 for (i
= 0; i
< fastload_num
; i
++) {
5796 if (fastload
[i
].data
)
5797 free(fastload
[i
].data
);
5804 COMMAND_HANDLER(handle_fast_load_image_command
)
5808 uint32_t image_size
;
5809 target_addr_t min_address
= 0;
5810 target_addr_t max_address
= -1;
5815 int retval
= CALL_COMMAND_HANDLER(parse_load_image_command_CMD_ARGV
,
5816 &image
, &min_address
, &max_address
);
5817 if (ERROR_OK
!= retval
)
5820 struct duration bench
;
5821 duration_start(&bench
);
5823 retval
= image_open(&image
, CMD_ARGV
[0], (CMD_ARGC
>= 3) ? CMD_ARGV
[2] : NULL
);
5824 if (retval
!= ERROR_OK
)
5829 fastload_num
= image
.num_sections
;
5830 fastload
= malloc(sizeof(struct FastLoad
)*image
.num_sections
);
5831 if (fastload
== NULL
) {
5832 command_print(CMD_CTX
, "out of memory");
5833 image_close(&image
);
5836 memset(fastload
, 0, sizeof(struct FastLoad
)*image
.num_sections
);
5837 for (i
= 0; i
< image
.num_sections
; i
++) {
5838 buffer
= malloc(image
.sections
[i
].size
);
5839 if (buffer
== NULL
) {
5840 command_print(CMD_CTX
, "error allocating buffer for section (%d bytes)",
5841 (int)(image
.sections
[i
].size
));
5842 retval
= ERROR_FAIL
;
5846 retval
= image_read_section(&image
, i
, 0x0, image
.sections
[i
].size
, buffer
, &buf_cnt
);
5847 if (retval
!= ERROR_OK
) {
5852 uint32_t offset
= 0;
5853 uint32_t length
= buf_cnt
;
5855 /* DANGER!!! beware of unsigned comparision here!!! */
5857 if ((image
.sections
[i
].base_address
+ buf_cnt
>= min_address
) &&
5858 (image
.sections
[i
].base_address
< max_address
)) {
5859 if (image
.sections
[i
].base_address
< min_address
) {
5860 /* clip addresses below */
5861 offset
+= min_address
-image
.sections
[i
].base_address
;
5865 if (image
.sections
[i
].base_address
+ buf_cnt
> max_address
)
5866 length
-= (image
.sections
[i
].base_address
+ buf_cnt
)-max_address
;
5868 fastload
[i
].address
= image
.sections
[i
].base_address
+ offset
;
5869 fastload
[i
].data
= malloc(length
);
5870 if (fastload
[i
].data
== NULL
) {
5872 command_print(CMD_CTX
, "error allocating buffer for section (%" PRIu32
" bytes)",
5874 retval
= ERROR_FAIL
;
5877 memcpy(fastload
[i
].data
, buffer
+ offset
, length
);
5878 fastload
[i
].length
= length
;
5880 image_size
+= length
;
5881 command_print(CMD_CTX
, "%u bytes written at address 0x%8.8x",
5882 (unsigned int)length
,
5883 ((unsigned int)(image
.sections
[i
].base_address
+ offset
)));
5889 if ((ERROR_OK
== retval
) && (duration_measure(&bench
) == ERROR_OK
)) {
5890 command_print(CMD_CTX
, "Loaded %" PRIu32
" bytes "
5891 "in %fs (%0.3f KiB/s)", image_size
,
5892 duration_elapsed(&bench
), duration_kbps(&bench
, image_size
));
5894 command_print(CMD_CTX
,
5895 "WARNING: image has not been loaded to target!"
5896 "You can issue a 'fast_load' to finish loading.");
5899 image_close(&image
);
5901 if (retval
!= ERROR_OK
)
5907 COMMAND_HANDLER(handle_fast_load_command
)
5910 return ERROR_COMMAND_SYNTAX_ERROR
;
5911 if (fastload
== NULL
) {
5912 LOG_ERROR("No image in memory");
5916 int64_t ms
= timeval_ms();
5918 int retval
= ERROR_OK
;
5919 for (i
= 0; i
< fastload_num
; i
++) {
5920 struct target
*target
= get_current_target(CMD_CTX
);
5921 command_print(CMD_CTX
, "Write to 0x%08x, length 0x%08x",
5922 (unsigned int)(fastload
[i
].address
),
5923 (unsigned int)(fastload
[i
].length
));
5924 retval
= target_write_buffer(target
, fastload
[i
].address
, fastload
[i
].length
, fastload
[i
].data
);
5925 if (retval
!= ERROR_OK
)
5927 size
+= fastload
[i
].length
;
5929 if (retval
== ERROR_OK
) {
5930 int64_t after
= timeval_ms();
5931 command_print(CMD_CTX
, "Loaded image %f kBytes/s", (float)(size
/1024.0)/((float)(after
-ms
)/1000.0));
5936 static const struct command_registration target_command_handlers
[] = {
5939 .handler
= handle_targets_command
,
5940 .mode
= COMMAND_ANY
,
5941 .help
= "change current default target (one parameter) "
5942 "or prints table of all targets (no parameters)",
5943 .usage
= "[target]",
5947 .mode
= COMMAND_CONFIG
,
5948 .help
= "configure target",
5950 .chain
= target_subcommand_handlers
,
5952 COMMAND_REGISTRATION_DONE
5955 int target_register_commands(struct command_context
*cmd_ctx
)
5957 return register_commands(cmd_ctx
, NULL
, target_command_handlers
);
5960 static bool target_reset_nag
= true;
5962 bool get_target_reset_nag(void)
5964 return target_reset_nag
;
5967 COMMAND_HANDLER(handle_target_reset_nag
)
5969 return CALL_COMMAND_HANDLER(handle_command_parse_bool
,
5970 &target_reset_nag
, "Nag after each reset about options to improve "
5974 COMMAND_HANDLER(handle_ps_command
)
5976 struct target
*target
= get_current_target(CMD_CTX
);
5978 if (target
->state
!= TARGET_HALTED
) {
5979 LOG_INFO("target not halted !!");
5983 if ((target
->rtos
) && (target
->rtos
->type
)
5984 && (target
->rtos
->type
->ps_command
)) {
5985 display
= target
->rtos
->type
->ps_command(target
);
5986 command_print(CMD_CTX
, "%s", display
);
5991 return ERROR_TARGET_FAILURE
;
5995 static void binprint(struct command_context
*cmd_ctx
, const char *text
, const uint8_t *buf
, int size
)
5998 command_print_sameline(cmd_ctx
, "%s", text
);
5999 for (int i
= 0; i
< size
; i
++)
6000 command_print_sameline(cmd_ctx
, " %02x", buf
[i
]);
6001 command_print(cmd_ctx
, " ");
6004 COMMAND_HANDLER(handle_test_mem_access_command
)
6006 struct target
*target
= get_current_target(CMD_CTX
);
6008 int retval
= ERROR_OK
;
6010 if (target
->state
!= TARGET_HALTED
) {
6011 LOG_INFO("target not halted !!");
6016 return ERROR_COMMAND_SYNTAX_ERROR
;
6018 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], test_size
);
6021 size_t num_bytes
= test_size
+ 4;
6023 struct working_area
*wa
= NULL
;
6024 retval
= target_alloc_working_area(target
, num_bytes
, &wa
);
6025 if (retval
!= ERROR_OK
) {
6026 LOG_ERROR("Not enough working area");
6030 uint8_t *test_pattern
= malloc(num_bytes
);
6032 for (size_t i
= 0; i
< num_bytes
; i
++)
6033 test_pattern
[i
] = rand();
6035 retval
= target_write_memory(target
, wa
->address
, 1, num_bytes
, test_pattern
);
6036 if (retval
!= ERROR_OK
) {
6037 LOG_ERROR("Test pattern write failed");
6041 for (int host_offset
= 0; host_offset
<= 1; host_offset
++) {
6042 for (int size
= 1; size
<= 4; size
*= 2) {
6043 for (int offset
= 0; offset
< 4; offset
++) {
6044 uint32_t count
= test_size
/ size
;
6045 size_t host_bufsiz
= (count
+ 2) * size
+ host_offset
;
6046 uint8_t *read_ref
= malloc(host_bufsiz
);
6047 uint8_t *read_buf
= malloc(host_bufsiz
);
6049 for (size_t i
= 0; i
< host_bufsiz
; i
++) {
6050 read_ref
[i
] = rand();
6051 read_buf
[i
] = read_ref
[i
];
6053 command_print_sameline(CMD_CTX
,
6054 "Test read %" PRIu32
" x %d @ %d to %saligned buffer: ", count
,
6055 size
, offset
, host_offset
? "un" : "");
6057 struct duration bench
;
6058 duration_start(&bench
);
6060 retval
= target_read_memory(target
, wa
->address
+ offset
, size
, count
,
6061 read_buf
+ size
+ host_offset
);
6063 duration_measure(&bench
);
6065 if (retval
== ERROR_TARGET_UNALIGNED_ACCESS
) {
6066 command_print(CMD_CTX
, "Unsupported alignment");
6068 } else if (retval
!= ERROR_OK
) {
6069 command_print(CMD_CTX
, "Memory read failed");
6073 /* replay on host */
6074 memcpy(read_ref
+ size
+ host_offset
, test_pattern
+ offset
, count
* size
);
6077 int result
= memcmp(read_ref
, read_buf
, host_bufsiz
);
6079 command_print(CMD_CTX
, "Pass in %fs (%0.3f KiB/s)",
6080 duration_elapsed(&bench
),
6081 duration_kbps(&bench
, count
* size
));
6083 command_print(CMD_CTX
, "Compare failed");
6084 binprint(CMD_CTX
, "ref:", read_ref
, host_bufsiz
);
6085 binprint(CMD_CTX
, "buf:", read_buf
, host_bufsiz
);
6098 target_free_working_area(target
, wa
);
6101 num_bytes
= test_size
+ 4 + 4 + 4;
6103 retval
= target_alloc_working_area(target
, num_bytes
, &wa
);
6104 if (retval
!= ERROR_OK
) {
6105 LOG_ERROR("Not enough working area");
6109 test_pattern
= malloc(num_bytes
);
6111 for (size_t i
= 0; i
< num_bytes
; i
++)
6112 test_pattern
[i
] = rand();
6114 for (int host_offset
= 0; host_offset
<= 1; host_offset
++) {
6115 for (int size
= 1; size
<= 4; size
*= 2) {
6116 for (int offset
= 0; offset
< 4; offset
++) {
6117 uint32_t count
= test_size
/ size
;
6118 size_t host_bufsiz
= count
* size
+ host_offset
;
6119 uint8_t *read_ref
= malloc(num_bytes
);
6120 uint8_t *read_buf
= malloc(num_bytes
);
6121 uint8_t *write_buf
= malloc(host_bufsiz
);
6123 for (size_t i
= 0; i
< host_bufsiz
; i
++)
6124 write_buf
[i
] = rand();
6125 command_print_sameline(CMD_CTX
,
6126 "Test write %" PRIu32
" x %d @ %d from %saligned buffer: ", count
,
6127 size
, offset
, host_offset
? "un" : "");
6129 retval
= target_write_memory(target
, wa
->address
, 1, num_bytes
, test_pattern
);
6130 if (retval
!= ERROR_OK
) {
6131 command_print(CMD_CTX
, "Test pattern write failed");
6135 /* replay on host */
6136 memcpy(read_ref
, test_pattern
, num_bytes
);
6137 memcpy(read_ref
+ size
+ offset
, write_buf
+ host_offset
, count
* size
);
6139 struct duration bench
;
6140 duration_start(&bench
);
6142 retval
= target_write_memory(target
, wa
->address
+ size
+ offset
, size
, count
,
6143 write_buf
+ host_offset
);
6145 duration_measure(&bench
);
6147 if (retval
== ERROR_TARGET_UNALIGNED_ACCESS
) {
6148 command_print(CMD_CTX
, "Unsupported alignment");
6150 } else if (retval
!= ERROR_OK
) {
6151 command_print(CMD_CTX
, "Memory write failed");
6156 retval
= target_read_memory(target
, wa
->address
, 1, num_bytes
, read_buf
);
6157 if (retval
!= ERROR_OK
) {
6158 command_print(CMD_CTX
, "Test pattern write failed");
6163 int result
= memcmp(read_ref
, read_buf
, num_bytes
);
6165 command_print(CMD_CTX
, "Pass in %fs (%0.3f KiB/s)",
6166 duration_elapsed(&bench
),
6167 duration_kbps(&bench
, count
* size
));
6169 command_print(CMD_CTX
, "Compare failed");
6170 binprint(CMD_CTX
, "ref:", read_ref
, num_bytes
);
6171 binprint(CMD_CTX
, "buf:", read_buf
, num_bytes
);
6183 target_free_working_area(target
, wa
);
6187 static const struct command_registration target_exec_command_handlers
[] = {
6189 .name
= "fast_load_image",
6190 .handler
= handle_fast_load_image_command
,
6191 .mode
= COMMAND_ANY
,
6192 .help
= "Load image into server memory for later use by "
6193 "fast_load; primarily for profiling",
6194 .usage
= "filename address ['bin'|'ihex'|'elf'|'s19'] "
6195 "[min_address [max_length]]",
6198 .name
= "fast_load",
6199 .handler
= handle_fast_load_command
,
6200 .mode
= COMMAND_EXEC
,
6201 .help
= "loads active fast load image to current target "
6202 "- mainly for profiling purposes",
6207 .handler
= handle_profile_command
,
6208 .mode
= COMMAND_EXEC
,
6209 .usage
= "seconds filename [start end]",
6210 .help
= "profiling samples the CPU PC",
6212 /** @todo don't register virt2phys() unless target supports it */
6214 .name
= "virt2phys",
6215 .handler
= handle_virt2phys_command
,
6216 .mode
= COMMAND_ANY
,
6217 .help
= "translate a virtual address into a physical address",
6218 .usage
= "virtual_address",
6222 .handler
= handle_reg_command
,
6223 .mode
= COMMAND_EXEC
,
6224 .help
= "display (reread from target with \"force\") or set a register; "
6225 "with no arguments, displays all registers and their values",
6226 .usage
= "[(register_number|register_name) [(value|'force')]]",
6230 .handler
= handle_poll_command
,
6231 .mode
= COMMAND_EXEC
,
6232 .help
= "poll target state; or reconfigure background polling",
6233 .usage
= "['on'|'off']",
6236 .name
= "wait_halt",
6237 .handler
= handle_wait_halt_command
,
6238 .mode
= COMMAND_EXEC
,
6239 .help
= "wait up to the specified number of milliseconds "
6240 "(default 5000) for a previously requested halt",
6241 .usage
= "[milliseconds]",
6245 .handler
= handle_halt_command
,
6246 .mode
= COMMAND_EXEC
,
6247 .help
= "request target to halt, then wait up to the specified"
6248 "number of milliseconds (default 5000) for it to complete",
6249 .usage
= "[milliseconds]",
6253 .handler
= handle_resume_command
,
6254 .mode
= COMMAND_EXEC
,
6255 .help
= "resume target execution from current PC or address",
6256 .usage
= "[address]",
6260 .handler
= handle_reset_command
,
6261 .mode
= COMMAND_EXEC
,
6262 .usage
= "[run|halt|init]",
6263 .help
= "Reset all targets into the specified mode."
6264 "Default reset mode is run, if not given.",
6267 .name
= "soft_reset_halt",
6268 .handler
= handle_soft_reset_halt_command
,
6269 .mode
= COMMAND_EXEC
,
6271 .help
= "halt the target and do a soft reset",
6275 .handler
= handle_step_command
,
6276 .mode
= COMMAND_EXEC
,
6277 .help
= "step one instruction from current PC or address",
6278 .usage
= "[address]",
6282 .handler
= handle_md_command
,
6283 .mode
= COMMAND_EXEC
,
6284 .help
= "display memory words",
6285 .usage
= "['phys'] address [count]",
6289 .handler
= handle_md_command
,
6290 .mode
= COMMAND_EXEC
,
6291 .help
= "display memory words",
6292 .usage
= "['phys'] address [count]",
6296 .handler
= handle_md_command
,
6297 .mode
= COMMAND_EXEC
,
6298 .help
= "display memory half-words",
6299 .usage
= "['phys'] address [count]",
6303 .handler
= handle_md_command
,
6304 .mode
= COMMAND_EXEC
,
6305 .help
= "display memory bytes",
6306 .usage
= "['phys'] address [count]",
6310 .handler
= handle_mw_command
,
6311 .mode
= COMMAND_EXEC
,
6312 .help
= "write memory word",
6313 .usage
= "['phys'] address value [count]",
6317 .handler
= handle_mw_command
,
6318 .mode
= COMMAND_EXEC
,
6319 .help
= "write memory word",
6320 .usage
= "['phys'] address value [count]",
6324 .handler
= handle_mw_command
,
6325 .mode
= COMMAND_EXEC
,
6326 .help
= "write memory half-word",
6327 .usage
= "['phys'] address value [count]",
6331 .handler
= handle_mw_command
,
6332 .mode
= COMMAND_EXEC
,
6333 .help
= "write memory byte",
6334 .usage
= "['phys'] address value [count]",
6338 .handler
= handle_bp_command
,
6339 .mode
= COMMAND_EXEC
,
6340 .help
= "list or set hardware or software breakpoint",
6341 .usage
= "<address> [<asid>]<length> ['hw'|'hw_ctx']",
6345 .handler
= handle_rbp_command
,
6346 .mode
= COMMAND_EXEC
,
6347 .help
= "remove breakpoint",
6352 .handler
= handle_wp_command
,
6353 .mode
= COMMAND_EXEC
,
6354 .help
= "list (no params) or create watchpoints",
6355 .usage
= "[address length [('r'|'w'|'a') value [mask]]]",
6359 .handler
= handle_rwp_command
,
6360 .mode
= COMMAND_EXEC
,
6361 .help
= "remove watchpoint",
6365 .name
= "load_image",
6366 .handler
= handle_load_image_command
,
6367 .mode
= COMMAND_EXEC
,
6368 .usage
= "filename address ['bin'|'ihex'|'elf'|'s19'] "
6369 "[min_address] [max_length]",
6372 .name
= "dump_image",
6373 .handler
= handle_dump_image_command
,
6374 .mode
= COMMAND_EXEC
,
6375 .usage
= "filename address size",
6378 .name
= "verify_image_checksum",
6379 .handler
= handle_verify_image_checksum_command
,
6380 .mode
= COMMAND_EXEC
,
6381 .usage
= "filename [offset [type]]",
6384 .name
= "verify_image",
6385 .handler
= handle_verify_image_command
,
6386 .mode
= COMMAND_EXEC
,
6387 .usage
= "filename [offset [type]]",
6390 .name
= "test_image",
6391 .handler
= handle_test_image_command
,
6392 .mode
= COMMAND_EXEC
,
6393 .usage
= "filename [offset [type]]",
6396 .name
= "mem2array",
6397 .mode
= COMMAND_EXEC
,
6398 .jim_handler
= jim_mem2array
,
6399 .help
= "read 8/16/32 bit memory and return as a TCL array "
6400 "for script processing",
6401 .usage
= "arrayname bitwidth address count",
6404 .name
= "array2mem",
6405 .mode
= COMMAND_EXEC
,
6406 .jim_handler
= jim_array2mem
,
6407 .help
= "convert a TCL array to memory locations "
6408 "and write the 8/16/32 bit values",
6409 .usage
= "arrayname bitwidth address count",
6412 .name
= "reset_nag",
6413 .handler
= handle_target_reset_nag
,
6414 .mode
= COMMAND_ANY
,
6415 .help
= "Nag after each reset about options that could have been "
6416 "enabled to improve performance. ",
6417 .usage
= "['enable'|'disable']",
6421 .handler
= handle_ps_command
,
6422 .mode
= COMMAND_EXEC
,
6423 .help
= "list all tasks ",
6427 .name
= "test_mem_access",
6428 .handler
= handle_test_mem_access_command
,
6429 .mode
= COMMAND_EXEC
,
6430 .help
= "Test the target's memory access functions",
6434 COMMAND_REGISTRATION_DONE
6436 static int target_register_user_commands(struct command_context
*cmd_ctx
)
6438 int retval
= ERROR_OK
;
6439 retval
= target_request_register_commands(cmd_ctx
);
6440 if (retval
!= ERROR_OK
)
6443 retval
= trace_register_commands(cmd_ctx
);
6444 if (retval
!= ERROR_OK
)
6448 return register_commands(cmd_ctx
, NULL
, target_exec_command_handlers
);