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 * This program is free software; you can redistribute it and/or modify *
24 * it under the terms of the GNU General Public License as published by *
25 * the Free Software Foundation; either version 2 of the License, or *
26 * (at your option) any later version. *
28 * This program is distributed in the hope that it will be useful, *
29 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
30 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
31 * GNU General Public License for more details. *
33 * You should have received a copy of the GNU General Public License *
34 * along with this program; if not, write to the *
35 * Free Software Foundation, Inc., *
36 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
37 ***************************************************************************/
43 #include <helper/time_support.h>
44 #include <jtag/jtag.h>
45 #include <flash/nor/core.h>
48 #include "target_type.h"
49 #include "target_request.h"
50 #include "breakpoints.h"
54 #include "rtos/rtos.h"
56 static int target_read_buffer_default(struct target
*target
, uint32_t address
,
57 uint32_t size
, uint8_t *buffer
);
58 static int target_write_buffer_default(struct target
*target
, uint32_t address
,
59 uint32_t size
, const uint8_t *buffer
);
60 static int target_array2mem(Jim_Interp
*interp
, struct target
*target
,
61 int argc
, Jim_Obj
* const *argv
);
62 static int target_mem2array(Jim_Interp
*interp
, struct target
*target
,
63 int argc
, Jim_Obj
* const *argv
);
64 static int target_register_user_commands(struct command_context
*cmd_ctx
);
67 extern struct target_type arm7tdmi_target
;
68 extern struct target_type arm720t_target
;
69 extern struct target_type arm9tdmi_target
;
70 extern struct target_type arm920t_target
;
71 extern struct target_type arm966e_target
;
72 extern struct target_type arm946e_target
;
73 extern struct target_type arm926ejs_target
;
74 extern struct target_type fa526_target
;
75 extern struct target_type feroceon_target
;
76 extern struct target_type dragonite_target
;
77 extern struct target_type xscale_target
;
78 extern struct target_type cortexm3_target
;
79 extern struct target_type cortexa8_target
;
80 extern struct target_type arm11_target
;
81 extern struct target_type mips_m4k_target
;
82 extern struct target_type avr_target
;
83 extern struct target_type dsp563xx_target
;
84 extern struct target_type dsp5680xx_target
;
85 extern struct target_type testee_target
;
86 extern struct target_type avr32_ap7k_target
;
87 extern struct target_type stm32_stlink_target
;
89 static struct target_type
*target_types
[] = {
110 &stm32_stlink_target
,
114 struct target
*all_targets
;
115 static struct target_event_callback
*target_event_callbacks
;
116 static struct target_timer_callback
*target_timer_callbacks
;
117 static const int polling_interval
= 100;
119 static const Jim_Nvp nvp_assert
[] = {
120 { .name
= "assert", NVP_ASSERT
},
121 { .name
= "deassert", NVP_DEASSERT
},
122 { .name
= "T", NVP_ASSERT
},
123 { .name
= "F", NVP_DEASSERT
},
124 { .name
= "t", NVP_ASSERT
},
125 { .name
= "f", NVP_DEASSERT
},
126 { .name
= NULL
, .value
= -1 }
129 static const Jim_Nvp nvp_error_target
[] = {
130 { .value
= ERROR_TARGET_INVALID
, .name
= "err-invalid" },
131 { .value
= ERROR_TARGET_INIT_FAILED
, .name
= "err-init-failed" },
132 { .value
= ERROR_TARGET_TIMEOUT
, .name
= "err-timeout" },
133 { .value
= ERROR_TARGET_NOT_HALTED
, .name
= "err-not-halted" },
134 { .value
= ERROR_TARGET_FAILURE
, .name
= "err-failure" },
135 { .value
= ERROR_TARGET_UNALIGNED_ACCESS
, .name
= "err-unaligned-access" },
136 { .value
= ERROR_TARGET_DATA_ABORT
, .name
= "err-data-abort" },
137 { .value
= ERROR_TARGET_RESOURCE_NOT_AVAILABLE
, .name
= "err-resource-not-available" },
138 { .value
= ERROR_TARGET_TRANSLATION_FAULT
, .name
= "err-translation-fault" },
139 { .value
= ERROR_TARGET_NOT_RUNNING
, .name
= "err-not-running" },
140 { .value
= ERROR_TARGET_NOT_EXAMINED
, .name
= "err-not-examined" },
141 { .value
= -1, .name
= NULL
}
144 static const char *target_strerror_safe(int err
)
148 n
= Jim_Nvp_value2name_simple(nvp_error_target
, err
);
155 static const Jim_Nvp nvp_target_event
[] = {
156 { .value
= TARGET_EVENT_OLD_gdb_program_config
, .name
= "old-gdb_program_config" },
157 { .value
= TARGET_EVENT_OLD_pre_resume
, .name
= "old-pre_resume" },
159 { .value
= TARGET_EVENT_GDB_HALT
, .name
= "gdb-halt" },
160 { .value
= TARGET_EVENT_HALTED
, .name
= "halted" },
161 { .value
= TARGET_EVENT_RESUMED
, .name
= "resumed" },
162 { .value
= TARGET_EVENT_RESUME_START
, .name
= "resume-start" },
163 { .value
= TARGET_EVENT_RESUME_END
, .name
= "resume-end" },
165 { .name
= "gdb-start", .value
= TARGET_EVENT_GDB_START
},
166 { .name
= "gdb-end", .value
= TARGET_EVENT_GDB_END
},
168 /* historical name */
170 { .value
= TARGET_EVENT_RESET_START
, .name
= "reset-start" },
172 { .value
= TARGET_EVENT_RESET_ASSERT_PRE
, .name
= "reset-assert-pre" },
173 { .value
= TARGET_EVENT_RESET_ASSERT
, .name
= "reset-assert" },
174 { .value
= TARGET_EVENT_RESET_ASSERT_POST
, .name
= "reset-assert-post" },
175 { .value
= TARGET_EVENT_RESET_DEASSERT_PRE
, .name
= "reset-deassert-pre" },
176 { .value
= TARGET_EVENT_RESET_DEASSERT_POST
, .name
= "reset-deassert-post" },
177 { .value
= TARGET_EVENT_RESET_HALT_PRE
, .name
= "reset-halt-pre" },
178 { .value
= TARGET_EVENT_RESET_HALT_POST
, .name
= "reset-halt-post" },
179 { .value
= TARGET_EVENT_RESET_WAIT_PRE
, .name
= "reset-wait-pre" },
180 { .value
= TARGET_EVENT_RESET_WAIT_POST
, .name
= "reset-wait-post" },
181 { .value
= TARGET_EVENT_RESET_INIT
, .name
= "reset-init" },
182 { .value
= TARGET_EVENT_RESET_END
, .name
= "reset-end" },
184 { .value
= TARGET_EVENT_EXAMINE_START
, .name
= "examine-start" },
185 { .value
= TARGET_EVENT_EXAMINE_END
, .name
= "examine-end" },
187 { .value
= TARGET_EVENT_DEBUG_HALTED
, .name
= "debug-halted" },
188 { .value
= TARGET_EVENT_DEBUG_RESUMED
, .name
= "debug-resumed" },
190 { .value
= TARGET_EVENT_GDB_ATTACH
, .name
= "gdb-attach" },
191 { .value
= TARGET_EVENT_GDB_DETACH
, .name
= "gdb-detach" },
193 { .value
= TARGET_EVENT_GDB_FLASH_WRITE_START
, .name
= "gdb-flash-write-start" },
194 { .value
= TARGET_EVENT_GDB_FLASH_WRITE_END
, .name
= "gdb-flash-write-end" },
196 { .value
= TARGET_EVENT_GDB_FLASH_ERASE_START
, .name
= "gdb-flash-erase-start" },
197 { .value
= TARGET_EVENT_GDB_FLASH_ERASE_END
, .name
= "gdb-flash-erase-end" },
199 { .value
= TARGET_EVENT_RESUME_START
, .name
= "resume-start" },
200 { .value
= TARGET_EVENT_RESUMED
, .name
= "resume-ok" },
201 { .value
= TARGET_EVENT_RESUME_END
, .name
= "resume-end" },
203 { .name
= NULL
, .value
= -1 }
206 static const Jim_Nvp nvp_target_state
[] = {
207 { .name
= "unknown", .value
= TARGET_UNKNOWN
},
208 { .name
= "running", .value
= TARGET_RUNNING
},
209 { .name
= "halted", .value
= TARGET_HALTED
},
210 { .name
= "reset", .value
= TARGET_RESET
},
211 { .name
= "debug-running", .value
= TARGET_DEBUG_RUNNING
},
212 { .name
= NULL
, .value
= -1 },
215 static const Jim_Nvp nvp_target_debug_reason
[] = {
216 { .name
= "debug-request" , .value
= DBG_REASON_DBGRQ
},
217 { .name
= "breakpoint" , .value
= DBG_REASON_BREAKPOINT
},
218 { .name
= "watchpoint" , .value
= DBG_REASON_WATCHPOINT
},
219 { .name
= "watchpoint-and-breakpoint", .value
= DBG_REASON_WPTANDBKPT
},
220 { .name
= "single-step" , .value
= DBG_REASON_SINGLESTEP
},
221 { .name
= "target-not-halted" , .value
= DBG_REASON_NOTHALTED
},
222 { .name
= "undefined" , .value
= DBG_REASON_UNDEFINED
},
223 { .name
= NULL
, .value
= -1 },
226 static const Jim_Nvp nvp_target_endian
[] = {
227 { .name
= "big", .value
= TARGET_BIG_ENDIAN
},
228 { .name
= "little", .value
= TARGET_LITTLE_ENDIAN
},
229 { .name
= "be", .value
= TARGET_BIG_ENDIAN
},
230 { .name
= "le", .value
= TARGET_LITTLE_ENDIAN
},
231 { .name
= NULL
, .value
= -1 },
234 static const Jim_Nvp nvp_reset_modes
[] = {
235 { .name
= "unknown", .value
= RESET_UNKNOWN
},
236 { .name
= "run" , .value
= RESET_RUN
},
237 { .name
= "halt" , .value
= RESET_HALT
},
238 { .name
= "init" , .value
= RESET_INIT
},
239 { .name
= NULL
, .value
= -1 },
242 const char *debug_reason_name(struct target
*t
)
246 cp
= Jim_Nvp_value2name_simple(nvp_target_debug_reason
,
247 t
->debug_reason
)->name
;
249 LOG_ERROR("Invalid debug reason: %d", (int)(t
->debug_reason
));
250 cp
= "(*BUG*unknown*BUG*)";
255 const char *target_state_name(struct target
*t
)
258 cp
= Jim_Nvp_value2name_simple(nvp_target_state
, t
->state
)->name
;
260 LOG_ERROR("Invalid target state: %d", (int)(t
->state
));
261 cp
= "(*BUG*unknown*BUG*)";
266 /* determine the number of the new target */
267 static int new_target_number(void)
272 /* number is 0 based */
276 if (x
< t
->target_number
)
277 x
= t
->target_number
;
283 /* read a uint32_t from a buffer in target memory endianness */
284 uint32_t target_buffer_get_u32(struct target
*target
, const uint8_t *buffer
)
286 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
287 return le_to_h_u32(buffer
);
289 return be_to_h_u32(buffer
);
292 /* read a uint24_t from a buffer in target memory endianness */
293 uint32_t target_buffer_get_u24(struct target
*target
, const uint8_t *buffer
)
295 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
296 return le_to_h_u24(buffer
);
298 return be_to_h_u24(buffer
);
301 /* read a uint16_t from a buffer in target memory endianness */
302 uint16_t target_buffer_get_u16(struct target
*target
, const uint8_t *buffer
)
304 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
305 return le_to_h_u16(buffer
);
307 return be_to_h_u16(buffer
);
310 /* read a uint8_t from a buffer in target memory endianness */
311 static uint8_t target_buffer_get_u8(struct target
*target
, const uint8_t *buffer
)
313 return *buffer
& 0x0ff;
316 /* write a uint32_t to a buffer in target memory endianness */
317 void target_buffer_set_u32(struct target
*target
, uint8_t *buffer
, uint32_t value
)
319 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
320 h_u32_to_le(buffer
, value
);
322 h_u32_to_be(buffer
, value
);
325 /* write a uint24_t to a buffer in target memory endianness */
326 void target_buffer_set_u24(struct target
*target
, uint8_t *buffer
, uint32_t value
)
328 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
329 h_u24_to_le(buffer
, value
);
331 h_u24_to_be(buffer
, value
);
334 /* write a uint16_t to a buffer in target memory endianness */
335 void target_buffer_set_u16(struct target
*target
, uint8_t *buffer
, uint16_t value
)
337 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
338 h_u16_to_le(buffer
, value
);
340 h_u16_to_be(buffer
, value
);
343 /* write a uint8_t to a buffer in target memory endianness */
344 static void target_buffer_set_u8(struct target
*target
, uint8_t *buffer
, uint8_t value
)
349 /* write a uint32_t array to a buffer in target memory endianness */
350 void target_buffer_get_u32_array(struct target
*target
, const uint8_t *buffer
, uint32_t count
, uint32_t *dstbuf
)
353 for (i
= 0; i
< count
; i
++)
354 dstbuf
[i
] = target_buffer_get_u32(target
, &buffer
[i
* 4]);
357 /* write a uint16_t array to a buffer in target memory endianness */
358 void target_buffer_get_u16_array(struct target
*target
, const uint8_t *buffer
, uint32_t count
, uint16_t *dstbuf
)
361 for (i
= 0; i
< count
; i
++)
362 dstbuf
[i
] = target_buffer_get_u16(target
, &buffer
[i
* 2]);
365 /* write a uint32_t array to a buffer in target memory endianness */
366 void target_buffer_set_u32_array(struct target
*target
, uint8_t *buffer
, uint32_t count
, uint32_t *srcbuf
)
369 for (i
= 0; i
< count
; i
++)
370 target_buffer_set_u32(target
, &buffer
[i
* 4], srcbuf
[i
]);
373 /* write a uint16_t array to a buffer in target memory endianness */
374 void target_buffer_set_u16_array(struct target
*target
, uint8_t *buffer
, uint32_t count
, uint16_t *srcbuf
)
377 for (i
= 0; i
< count
; i
++)
378 target_buffer_set_u16(target
, &buffer
[i
* 2], srcbuf
[i
]);
381 /* return a pointer to a configured target; id is name or number */
382 struct target
*get_target(const char *id
)
384 struct target
*target
;
386 /* try as tcltarget name */
387 for (target
= all_targets
; target
; target
= target
->next
) {
388 if (target
->cmd_name
== NULL
)
390 if (strcmp(id
, target
->cmd_name
) == 0)
394 /* It's OK to remove this fallback sometime after August 2010 or so */
396 /* no match, try as number */
398 if (parse_uint(id
, &num
) != ERROR_OK
)
401 for (target
= all_targets
; target
; target
= target
->next
) {
402 if (target
->target_number
== (int)num
) {
403 LOG_WARNING("use '%s' as target identifier, not '%u'",
404 target
->cmd_name
, num
);
412 /* returns a pointer to the n-th configured target */
413 static struct target
*get_target_by_num(int num
)
415 struct target
*target
= all_targets
;
418 if (target
->target_number
== num
)
420 target
= target
->next
;
426 struct target
*get_current_target(struct command_context
*cmd_ctx
)
428 struct target
*target
= get_target_by_num(cmd_ctx
->current_target
);
430 if (target
== NULL
) {
431 LOG_ERROR("BUG: current_target out of bounds");
438 int target_poll(struct target
*target
)
442 /* We can't poll until after examine */
443 if (!target_was_examined(target
)) {
444 /* Fail silently lest we pollute the log */
448 retval
= target
->type
->poll(target
);
449 if (retval
!= ERROR_OK
)
452 if (target
->halt_issued
) {
453 if (target
->state
== TARGET_HALTED
)
454 target
->halt_issued
= false;
456 long long t
= timeval_ms() - target
->halt_issued_time
;
458 target
->halt_issued
= false;
459 LOG_INFO("Halt timed out, wake up GDB.");
460 target_call_event_callbacks(target
, TARGET_EVENT_GDB_HALT
);
468 int target_halt(struct target
*target
)
471 /* We can't poll until after examine */
472 if (!target_was_examined(target
)) {
473 LOG_ERROR("Target not examined yet");
477 retval
= target
->type
->halt(target
);
478 if (retval
!= ERROR_OK
)
481 target
->halt_issued
= true;
482 target
->halt_issued_time
= timeval_ms();
488 * Make the target (re)start executing using its saved execution
489 * context (possibly with some modifications).
491 * @param target Which target should start executing.
492 * @param current True to use the target's saved program counter instead
493 * of the address parameter
494 * @param address Optionally used as the program counter.
495 * @param handle_breakpoints True iff breakpoints at the resumption PC
496 * should be skipped. (For example, maybe execution was stopped by
497 * such a breakpoint, in which case it would be counterprodutive to
499 * @param debug_execution False if all working areas allocated by OpenOCD
500 * should be released and/or restored to their original contents.
501 * (This would for example be true to run some downloaded "helper"
502 * algorithm code, which resides in one such working buffer and uses
503 * another for data storage.)
505 * @todo Resolve the ambiguity about what the "debug_execution" flag
506 * signifies. For example, Target implementations don't agree on how
507 * it relates to invalidation of the register cache, or to whether
508 * breakpoints and watchpoints should be enabled. (It would seem wrong
509 * to enable breakpoints when running downloaded "helper" algorithms
510 * (debug_execution true), since the breakpoints would be set to match
511 * target firmware being debugged, not the helper algorithm.... and
512 * enabling them could cause such helpers to malfunction (for example,
513 * by overwriting data with a breakpoint instruction. On the other
514 * hand the infrastructure for running such helpers might use this
515 * procedure but rely on hardware breakpoint to detect termination.)
517 int target_resume(struct target
*target
, int current
, uint32_t address
, int handle_breakpoints
, int debug_execution
)
521 /* We can't poll until after examine */
522 if (!target_was_examined(target
)) {
523 LOG_ERROR("Target not examined yet");
527 /* note that resume *must* be asynchronous. The CPU can halt before
528 * we poll. The CPU can even halt at the current PC as a result of
529 * a software breakpoint being inserted by (a bug?) the application.
531 retval
= target
->type
->resume(target
, current
, address
, handle_breakpoints
, debug_execution
);
532 if (retval
!= ERROR_OK
)
538 static int target_process_reset(struct command_context
*cmd_ctx
, enum target_reset_mode reset_mode
)
543 n
= Jim_Nvp_value2name_simple(nvp_reset_modes
, reset_mode
);
544 if (n
->name
== NULL
) {
545 LOG_ERROR("invalid reset mode");
549 /* disable polling during reset to make reset event scripts
550 * more predictable, i.e. dr/irscan & pathmove in events will
551 * not have JTAG operations injected into the middle of a sequence.
553 bool save_poll
= jtag_poll_get_enabled();
555 jtag_poll_set_enabled(false);
557 sprintf(buf
, "ocd_process_reset %s", n
->name
);
558 retval
= Jim_Eval(cmd_ctx
->interp
, buf
);
560 jtag_poll_set_enabled(save_poll
);
562 if (retval
!= JIM_OK
) {
563 Jim_MakeErrorMessage(cmd_ctx
->interp
);
564 command_print(NULL
, "%s\n", Jim_GetString(Jim_GetResult(cmd_ctx
->interp
), NULL
));
568 /* We want any events to be processed before the prompt */
569 retval
= target_call_timer_callbacks_now();
571 struct target
*target
;
572 for (target
= all_targets
; target
; target
= target
->next
)
573 target
->type
->check_reset(target
);
578 static int identity_virt2phys(struct target
*target
,
579 uint32_t virtual, uint32_t *physical
)
585 static int no_mmu(struct target
*target
, int *enabled
)
591 static int default_examine(struct target
*target
)
593 target_set_examined(target
);
597 /* no check by default */
598 static int default_check_reset(struct target
*target
)
603 int target_examine_one(struct target
*target
)
605 return target
->type
->examine(target
);
608 static int jtag_enable_callback(enum jtag_event event
, void *priv
)
610 struct target
*target
= priv
;
612 if (event
!= JTAG_TAP_EVENT_ENABLE
|| !target
->tap
->enabled
)
615 jtag_unregister_event_callback(jtag_enable_callback
, target
);
616 return target_examine_one(target
);
620 /* Targets that correctly implement init + examine, i.e.
621 * no communication with target during init:
625 int target_examine(void)
627 int retval
= ERROR_OK
;
628 struct target
*target
;
630 for (target
= all_targets
; target
; target
= target
->next
) {
631 /* defer examination, but don't skip it */
632 if (!target
->tap
->enabled
) {
633 jtag_register_event_callback(jtag_enable_callback
,
637 retval
= target_examine_one(target
);
638 if (retval
!= ERROR_OK
)
643 const char *target_type_name(struct target
*target
)
645 return target
->type
->name
;
648 static int target_write_memory_imp(struct target
*target
, uint32_t address
,
649 uint32_t size
, uint32_t count
, const uint8_t *buffer
)
651 if (!target_was_examined(target
)) {
652 LOG_ERROR("Target not examined yet");
655 return target
->type
->write_memory_imp(target
, address
, size
, count
, buffer
);
658 static int target_read_memory_imp(struct target
*target
, uint32_t address
,
659 uint32_t size
, uint32_t count
, uint8_t *buffer
)
661 if (!target_was_examined(target
)) {
662 LOG_ERROR("Target not examined yet");
665 return target
->type
->read_memory_imp(target
, address
, size
, count
, buffer
);
668 static int target_soft_reset_halt_imp(struct target
*target
)
670 if (!target_was_examined(target
)) {
671 LOG_ERROR("Target not examined yet");
674 if (!target
->type
->soft_reset_halt_imp
) {
675 LOG_ERROR("Target %s does not support soft_reset_halt",
676 target_name(target
));
679 return target
->type
->soft_reset_halt_imp(target
);
683 * Downloads a target-specific native code algorithm to the target,
684 * and executes it. * Note that some targets may need to set up, enable,
685 * and tear down a breakpoint (hard or * soft) to detect algorithm
686 * termination, while others may support lower overhead schemes where
687 * soft breakpoints embedded in the algorithm automatically terminate the
690 * @param target used to run the algorithm
691 * @param arch_info target-specific description of the algorithm.
693 int target_run_algorithm(struct target
*target
,
694 int num_mem_params
, struct mem_param
*mem_params
,
695 int num_reg_params
, struct reg_param
*reg_param
,
696 uint32_t entry_point
, uint32_t exit_point
,
697 int timeout_ms
, void *arch_info
)
699 int retval
= ERROR_FAIL
;
701 if (!target_was_examined(target
)) {
702 LOG_ERROR("Target not examined yet");
705 if (!target
->type
->run_algorithm
) {
706 LOG_ERROR("Target type '%s' does not support %s",
707 target_type_name(target
), __func__
);
711 target
->running_alg
= true;
712 retval
= target
->type
->run_algorithm(target
,
713 num_mem_params
, mem_params
,
714 num_reg_params
, reg_param
,
715 entry_point
, exit_point
, timeout_ms
, arch_info
);
716 target
->running_alg
= false;
723 * Downloads a target-specific native code algorithm to the target,
724 * executes and leaves it running.
726 * @param target used to run the algorithm
727 * @param arch_info target-specific description of the algorithm.
729 int target_start_algorithm(struct target
*target
,
730 int num_mem_params
, struct mem_param
*mem_params
,
731 int num_reg_params
, struct reg_param
*reg_params
,
732 uint32_t entry_point
, uint32_t exit_point
,
735 int retval
= ERROR_FAIL
;
737 if (!target_was_examined(target
)) {
738 LOG_ERROR("Target not examined yet");
741 if (!target
->type
->start_algorithm
) {
742 LOG_ERROR("Target type '%s' does not support %s",
743 target_type_name(target
), __func__
);
746 if (target
->running_alg
) {
747 LOG_ERROR("Target is already running an algorithm");
751 target
->running_alg
= true;
752 retval
= target
->type
->start_algorithm(target
,
753 num_mem_params
, mem_params
,
754 num_reg_params
, reg_params
,
755 entry_point
, exit_point
, arch_info
);
762 * Waits for an algorithm started with target_start_algorithm() to complete.
764 * @param target used to run the algorithm
765 * @param arch_info target-specific description of the algorithm.
767 int target_wait_algorithm(struct target
*target
,
768 int num_mem_params
, struct mem_param
*mem_params
,
769 int num_reg_params
, struct reg_param
*reg_params
,
770 uint32_t exit_point
, int timeout_ms
,
773 int retval
= ERROR_FAIL
;
775 if (!target
->type
->wait_algorithm
) {
776 LOG_ERROR("Target type '%s' does not support %s",
777 target_type_name(target
), __func__
);
780 if (!target
->running_alg
) {
781 LOG_ERROR("Target is not running an algorithm");
785 retval
= target
->type
->wait_algorithm(target
,
786 num_mem_params
, mem_params
,
787 num_reg_params
, reg_params
,
788 exit_point
, timeout_ms
, arch_info
);
789 if (retval
!= ERROR_TARGET_TIMEOUT
)
790 target
->running_alg
= false;
797 int target_read_memory(struct target
*target
,
798 uint32_t address
, uint32_t size
, uint32_t count
, uint8_t *buffer
)
800 return target
->type
->read_memory(target
, address
, size
, count
, buffer
);
803 static int target_read_phys_memory(struct target
*target
,
804 uint32_t address
, uint32_t size
, uint32_t count
, uint8_t *buffer
)
806 return target
->type
->read_phys_memory(target
, address
, size
, count
, buffer
);
809 int target_write_memory(struct target
*target
,
810 uint32_t address
, uint32_t size
, uint32_t count
, const uint8_t *buffer
)
812 return target
->type
->write_memory(target
, address
, size
, count
, buffer
);
815 static int target_write_phys_memory(struct target
*target
,
816 uint32_t address
, uint32_t size
, uint32_t count
, const uint8_t *buffer
)
818 return target
->type
->write_phys_memory(target
, address
, size
, count
, buffer
);
821 int target_bulk_write_memory(struct target
*target
,
822 uint32_t address
, uint32_t count
, const uint8_t *buffer
)
824 return target
->type
->bulk_write_memory(target
, address
, count
, buffer
);
827 int target_add_breakpoint(struct target
*target
,
828 struct breakpoint
*breakpoint
)
830 if ((target
->state
!= TARGET_HALTED
) && (breakpoint
->type
!= BKPT_HARD
)) {
831 LOG_WARNING("target %s is not halted", target
->cmd_name
);
832 return ERROR_TARGET_NOT_HALTED
;
834 return target
->type
->add_breakpoint(target
, breakpoint
);
837 int target_add_context_breakpoint(struct target
*target
,
838 struct breakpoint
*breakpoint
)
840 if (target
->state
!= TARGET_HALTED
) {
841 LOG_WARNING("target %s is not halted", target
->cmd_name
);
842 return ERROR_TARGET_NOT_HALTED
;
844 return target
->type
->add_context_breakpoint(target
, breakpoint
);
847 int target_add_hybrid_breakpoint(struct target
*target
,
848 struct breakpoint
*breakpoint
)
850 if (target
->state
!= TARGET_HALTED
) {
851 LOG_WARNING("target %s is not halted", target
->cmd_name
);
852 return ERROR_TARGET_NOT_HALTED
;
854 return target
->type
->add_hybrid_breakpoint(target
, breakpoint
);
857 int target_remove_breakpoint(struct target
*target
,
858 struct breakpoint
*breakpoint
)
860 return target
->type
->remove_breakpoint(target
, breakpoint
);
863 int target_add_watchpoint(struct target
*target
,
864 struct watchpoint
*watchpoint
)
866 if (target
->state
!= TARGET_HALTED
) {
867 LOG_WARNING("target %s is not halted", target
->cmd_name
);
868 return ERROR_TARGET_NOT_HALTED
;
870 return target
->type
->add_watchpoint(target
, watchpoint
);
872 int target_remove_watchpoint(struct target
*target
,
873 struct watchpoint
*watchpoint
)
875 return target
->type
->remove_watchpoint(target
, watchpoint
);
878 int target_get_gdb_reg_list(struct target
*target
,
879 struct reg
**reg_list
[], int *reg_list_size
)
881 return target
->type
->get_gdb_reg_list(target
, reg_list
, reg_list_size
);
883 int target_step(struct target
*target
,
884 int current
, uint32_t address
, int handle_breakpoints
)
886 return target
->type
->step(target
, current
, address
, handle_breakpoints
);
890 * Reset the @c examined flag for the given target.
891 * Pure paranoia -- targets are zeroed on allocation.
893 static void target_reset_examined(struct target
*target
)
895 target
->examined
= false;
898 static int err_read_phys_memory(struct target
*target
, uint32_t address
,
899 uint32_t size
, uint32_t count
, uint8_t *buffer
)
901 LOG_ERROR("Not implemented: %s", __func__
);
905 static int err_write_phys_memory(struct target
*target
, uint32_t address
,
906 uint32_t size
, uint32_t count
, const uint8_t *buffer
)
908 LOG_ERROR("Not implemented: %s", __func__
);
912 static int handle_target(void *priv
);
914 static int target_init_one(struct command_context
*cmd_ctx
,
915 struct target
*target
)
917 target_reset_examined(target
);
919 struct target_type
*type
= target
->type
;
920 if (type
->examine
== NULL
)
921 type
->examine
= default_examine
;
923 if (type
->check_reset
== NULL
)
924 type
->check_reset
= default_check_reset
;
926 assert(type
->init_target
!= NULL
);
928 int retval
= type
->init_target(cmd_ctx
, target
);
929 if (ERROR_OK
!= retval
) {
930 LOG_ERROR("target '%s' init failed", target_name(target
));
935 * @todo get rid of those *memory_imp() methods, now that all
936 * callers are using target_*_memory() accessors ... and make
937 * sure the "physical" paths handle the same issues.
939 /* a non-invasive way(in terms of patches) to add some code that
940 * runs before the type->write/read_memory implementation
942 type
->write_memory_imp
= target
->type
->write_memory
;
943 type
->write_memory
= target_write_memory_imp
;
945 type
->read_memory_imp
= target
->type
->read_memory
;
946 type
->read_memory
= target_read_memory_imp
;
948 type
->soft_reset_halt_imp
= target
->type
->soft_reset_halt
;
949 type
->soft_reset_halt
= target_soft_reset_halt_imp
;
951 /* Sanity-check MMU support ... stub in what we must, to help
952 * implement it in stages, but warn if we need to do so.
955 if (type
->write_phys_memory
== NULL
) {
956 LOG_ERROR("type '%s' is missing write_phys_memory",
958 type
->write_phys_memory
= err_write_phys_memory
;
960 if (type
->read_phys_memory
== NULL
) {
961 LOG_ERROR("type '%s' is missing read_phys_memory",
963 type
->read_phys_memory
= err_read_phys_memory
;
965 if (type
->virt2phys
== NULL
) {
966 LOG_ERROR("type '%s' is missing virt2phys", type
->name
);
967 type
->virt2phys
= identity_virt2phys
;
970 /* Make sure no-MMU targets all behave the same: make no
971 * distinction between physical and virtual addresses, and
972 * ensure that virt2phys() is always an identity mapping.
974 if (type
->write_phys_memory
|| type
->read_phys_memory
|| type
->virt2phys
)
975 LOG_WARNING("type '%s' has bad MMU hooks", type
->name
);
978 type
->write_phys_memory
= type
->write_memory
;
979 type
->read_phys_memory
= type
->read_memory
;
980 type
->virt2phys
= identity_virt2phys
;
983 if (target
->type
->read_buffer
== NULL
)
984 target
->type
->read_buffer
= target_read_buffer_default
;
986 if (target
->type
->write_buffer
== NULL
)
987 target
->type
->write_buffer
= target_write_buffer_default
;
992 static int target_init(struct command_context
*cmd_ctx
)
994 struct target
*target
;
997 for (target
= all_targets
; target
; target
= target
->next
) {
998 retval
= target_init_one(cmd_ctx
, target
);
999 if (ERROR_OK
!= retval
)
1006 retval
= target_register_user_commands(cmd_ctx
);
1007 if (ERROR_OK
!= retval
)
1010 retval
= target_register_timer_callback(&handle_target
,
1011 polling_interval
, 1, cmd_ctx
->interp
);
1012 if (ERROR_OK
!= retval
)
1018 COMMAND_HANDLER(handle_target_init_command
)
1023 return ERROR_COMMAND_SYNTAX_ERROR
;
1025 static bool target_initialized
;
1026 if (target_initialized
) {
1027 LOG_INFO("'target init' has already been called");
1030 target_initialized
= true;
1032 retval
= command_run_line(CMD_CTX
, "init_targets");
1033 if (ERROR_OK
!= retval
)
1036 LOG_DEBUG("Initializing targets...");
1037 return target_init(CMD_CTX
);
1040 int target_register_event_callback(int (*callback
)(struct target
*target
,
1041 enum target_event event
, void *priv
), void *priv
)
1043 struct target_event_callback
**callbacks_p
= &target_event_callbacks
;
1045 if (callback
== NULL
)
1046 return ERROR_COMMAND_SYNTAX_ERROR
;
1049 while ((*callbacks_p
)->next
)
1050 callbacks_p
= &((*callbacks_p
)->next
);
1051 callbacks_p
= &((*callbacks_p
)->next
);
1054 (*callbacks_p
) = malloc(sizeof(struct target_event_callback
));
1055 (*callbacks_p
)->callback
= callback
;
1056 (*callbacks_p
)->priv
= priv
;
1057 (*callbacks_p
)->next
= NULL
;
1062 int target_register_timer_callback(int (*callback
)(void *priv
), int time_ms
, int periodic
, void *priv
)
1064 struct target_timer_callback
**callbacks_p
= &target_timer_callbacks
;
1067 if (callback
== NULL
)
1068 return ERROR_COMMAND_SYNTAX_ERROR
;
1071 while ((*callbacks_p
)->next
)
1072 callbacks_p
= &((*callbacks_p
)->next
);
1073 callbacks_p
= &((*callbacks_p
)->next
);
1076 (*callbacks_p
) = malloc(sizeof(struct target_timer_callback
));
1077 (*callbacks_p
)->callback
= callback
;
1078 (*callbacks_p
)->periodic
= periodic
;
1079 (*callbacks_p
)->time_ms
= time_ms
;
1081 gettimeofday(&now
, NULL
);
1082 (*callbacks_p
)->when
.tv_usec
= now
.tv_usec
+ (time_ms
% 1000) * 1000;
1083 time_ms
-= (time_ms
% 1000);
1084 (*callbacks_p
)->when
.tv_sec
= now
.tv_sec
+ (time_ms
/ 1000);
1085 if ((*callbacks_p
)->when
.tv_usec
> 1000000) {
1086 (*callbacks_p
)->when
.tv_usec
= (*callbacks_p
)->when
.tv_usec
- 1000000;
1087 (*callbacks_p
)->when
.tv_sec
+= 1;
1090 (*callbacks_p
)->priv
= priv
;
1091 (*callbacks_p
)->next
= NULL
;
1096 int target_unregister_event_callback(int (*callback
)(struct target
*target
,
1097 enum target_event event
, void *priv
), void *priv
)
1099 struct target_event_callback
**p
= &target_event_callbacks
;
1100 struct target_event_callback
*c
= target_event_callbacks
;
1102 if (callback
== NULL
)
1103 return ERROR_COMMAND_SYNTAX_ERROR
;
1106 struct target_event_callback
*next
= c
->next
;
1107 if ((c
->callback
== callback
) && (c
->priv
== priv
)) {
1119 static int target_unregister_timer_callback(int (*callback
)(void *priv
), void *priv
)
1121 struct target_timer_callback
**p
= &target_timer_callbacks
;
1122 struct target_timer_callback
*c
= target_timer_callbacks
;
1124 if (callback
== NULL
)
1125 return ERROR_COMMAND_SYNTAX_ERROR
;
1128 struct target_timer_callback
*next
= c
->next
;
1129 if ((c
->callback
== callback
) && (c
->priv
== priv
)) {
1141 int target_call_event_callbacks(struct target
*target
, enum target_event event
)
1143 struct target_event_callback
*callback
= target_event_callbacks
;
1144 struct target_event_callback
*next_callback
;
1146 if (event
== TARGET_EVENT_HALTED
) {
1147 /* execute early halted first */
1148 target_call_event_callbacks(target
, TARGET_EVENT_GDB_HALT
);
1151 LOG_DEBUG("target event %i (%s)", event
,
1152 Jim_Nvp_value2name_simple(nvp_target_event
, event
)->name
);
1154 target_handle_event(target
, event
);
1157 next_callback
= callback
->next
;
1158 callback
->callback(target
, event
, callback
->priv
);
1159 callback
= next_callback
;
1165 static int target_timer_callback_periodic_restart(
1166 struct target_timer_callback
*cb
, struct timeval
*now
)
1168 int time_ms
= cb
->time_ms
;
1169 cb
->when
.tv_usec
= now
->tv_usec
+ (time_ms
% 1000) * 1000;
1170 time_ms
-= (time_ms
% 1000);
1171 cb
->when
.tv_sec
= now
->tv_sec
+ time_ms
/ 1000;
1172 if (cb
->when
.tv_usec
> 1000000) {
1173 cb
->when
.tv_usec
= cb
->when
.tv_usec
- 1000000;
1174 cb
->when
.tv_sec
+= 1;
1179 static int target_call_timer_callback(struct target_timer_callback
*cb
,
1180 struct timeval
*now
)
1182 cb
->callback(cb
->priv
);
1185 return target_timer_callback_periodic_restart(cb
, now
);
1187 return target_unregister_timer_callback(cb
->callback
, cb
->priv
);
1190 static int target_call_timer_callbacks_check_time(int checktime
)
1195 gettimeofday(&now
, NULL
);
1197 struct target_timer_callback
*callback
= target_timer_callbacks
;
1199 /* cleaning up may unregister and free this callback */
1200 struct target_timer_callback
*next_callback
= callback
->next
;
1202 bool call_it
= callback
->callback
&&
1203 ((!checktime
&& callback
->periodic
) ||
1204 now
.tv_sec
> callback
->when
.tv_sec
||
1205 (now
.tv_sec
== callback
->when
.tv_sec
&&
1206 now
.tv_usec
>= callback
->when
.tv_usec
));
1209 int retval
= target_call_timer_callback(callback
, &now
);
1210 if (retval
!= ERROR_OK
)
1214 callback
= next_callback
;
1220 int target_call_timer_callbacks(void)
1222 return target_call_timer_callbacks_check_time(1);
1225 /* invoke periodic callbacks immediately */
1226 int target_call_timer_callbacks_now(void)
1228 return target_call_timer_callbacks_check_time(0);
1231 int target_alloc_working_area_try(struct target
*target
, uint32_t size
, struct working_area
**area
)
1233 struct working_area
*c
= target
->working_areas
;
1234 struct working_area
*new_wa
= NULL
;
1236 /* Reevaluate working area address based on MMU state*/
1237 if (target
->working_areas
== NULL
) {
1241 retval
= target
->type
->mmu(target
, &enabled
);
1242 if (retval
!= ERROR_OK
)
1246 if (target
->working_area_phys_spec
) {
1247 LOG_DEBUG("MMU disabled, using physical "
1248 "address for working memory 0x%08x",
1249 (unsigned)target
->working_area_phys
);
1250 target
->working_area
= target
->working_area_phys
;
1252 LOG_ERROR("No working memory available. "
1253 "Specify -work-area-phys to target.");
1254 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
1257 if (target
->working_area_virt_spec
) {
1258 LOG_DEBUG("MMU enabled, using virtual "
1259 "address for working memory 0x%08x",
1260 (unsigned)target
->working_area_virt
);
1261 target
->working_area
= target
->working_area_virt
;
1263 LOG_ERROR("No working memory available. "
1264 "Specify -work-area-virt to target.");
1265 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
1270 /* only allocate multiples of 4 byte */
1272 LOG_ERROR("BUG: code tried to allocate unaligned number of bytes (0x%08x), padding", ((unsigned)(size
)));
1273 size
= (size
+ 3) & (~3);
1276 /* see if there's already a matching working area */
1278 if ((c
->free
) && (c
->size
== size
)) {
1285 /* if not, allocate a new one */
1287 struct working_area
**p
= &target
->working_areas
;
1288 uint32_t first_free
= target
->working_area
;
1289 uint32_t free_size
= target
->working_area_size
;
1291 c
= target
->working_areas
;
1293 first_free
+= c
->size
;
1294 free_size
-= c
->size
;
1299 if (free_size
< size
)
1300 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
1302 LOG_DEBUG("allocated new working area at address 0x%08x", (unsigned)first_free
);
1304 new_wa
= malloc(sizeof(struct working_area
));
1305 new_wa
->next
= NULL
;
1306 new_wa
->size
= size
;
1307 new_wa
->address
= first_free
;
1309 if (target
->backup_working_area
) {
1311 new_wa
->backup
= malloc(new_wa
->size
);
1312 retval
= target_read_memory(target
, new_wa
->address
, 4,
1313 new_wa
->size
/ 4, new_wa
->backup
);
1314 if (retval
!= ERROR_OK
) {
1315 free(new_wa
->backup
);
1320 new_wa
->backup
= NULL
;
1322 /* put new entry in list */
1326 /* mark as used, and return the new (reused) area */
1327 new_wa
->free
= false;
1331 new_wa
->user
= area
;
1336 int target_alloc_working_area(struct target
*target
, uint32_t size
, struct working_area
**area
)
1340 retval
= target_alloc_working_area_try(target
, size
, area
);
1341 if (retval
== ERROR_TARGET_RESOURCE_NOT_AVAILABLE
)
1342 LOG_WARNING("not enough working area available(requested %u)", (unsigned)(size
));
1347 static int target_free_working_area_restore(struct target
*target
, struct working_area
*area
, int restore
)
1352 if (restore
&& target
->backup_working_area
) {
1353 int retval
= target_write_memory(target
,
1354 area
->address
, 4, area
->size
/ 4, area
->backup
);
1355 if (retval
!= ERROR_OK
)
1361 /* mark user pointer invalid */
1368 int target_free_working_area(struct target
*target
, struct working_area
*area
)
1370 return target_free_working_area_restore(target
, area
, 1);
1373 /* free resources and restore memory, if restoring memory fails,
1374 * free up resources anyway
1376 static void target_free_all_working_areas_restore(struct target
*target
, int restore
)
1378 struct working_area
*c
= target
->working_areas
;
1381 struct working_area
*next
= c
->next
;
1382 target_free_working_area_restore(target
, c
, restore
);
1392 target
->working_areas
= NULL
;
1395 void target_free_all_working_areas(struct target
*target
)
1397 target_free_all_working_areas_restore(target
, 1);
1400 int target_arch_state(struct target
*target
)
1403 if (target
== NULL
) {
1404 LOG_USER("No target has been configured");
1408 LOG_USER("target state: %s", target_state_name(target
));
1410 if (target
->state
!= TARGET_HALTED
)
1413 retval
= target
->type
->arch_state(target
);
1417 /* Single aligned words are guaranteed to use 16 or 32 bit access
1418 * mode respectively, otherwise data is handled as quickly as
1421 int target_write_buffer(struct target
*target
, uint32_t address
, uint32_t size
, const uint8_t *buffer
)
1423 LOG_DEBUG("writing buffer of %i byte at 0x%8.8x",
1424 (int)size
, (unsigned)address
);
1426 if (!target_was_examined(target
)) {
1427 LOG_ERROR("Target not examined yet");
1434 if ((address
+ size
- 1) < address
) {
1435 /* GDB can request this when e.g. PC is 0xfffffffc*/
1436 LOG_ERROR("address + size wrapped(0x%08x, 0x%08x)",
1442 return target
->type
->write_buffer(target
, address
, size
, buffer
);
1445 static int target_write_buffer_default(struct target
*target
, uint32_t address
, uint32_t size
, const uint8_t *buffer
)
1447 int retval
= ERROR_OK
;
1449 if (((address
% 2) == 0) && (size
== 2))
1450 return target_write_memory(target
, address
, 2, 1, buffer
);
1452 /* handle unaligned head bytes */
1454 uint32_t unaligned
= 4 - (address
% 4);
1456 if (unaligned
> size
)
1459 retval
= target_write_memory(target
, address
, 1, unaligned
, buffer
);
1460 if (retval
!= ERROR_OK
)
1463 buffer
+= unaligned
;
1464 address
+= unaligned
;
1468 /* handle aligned words */
1470 int aligned
= size
- (size
% 4);
1472 /* use bulk writes above a certain limit. This may have to be changed */
1473 if (aligned
> 128) {
1474 retval
= target
->type
->bulk_write_memory(target
, address
, aligned
/ 4, buffer
);
1475 if (retval
!= ERROR_OK
)
1478 retval
= target_write_memory(target
, address
, 4, aligned
/ 4, buffer
);
1479 if (retval
!= ERROR_OK
)
1488 /* handle tail writes of less than 4 bytes */
1490 retval
= target_write_memory(target
, address
, 1, size
, buffer
);
1491 if (retval
!= ERROR_OK
)
1498 /* Single aligned words are guaranteed to use 16 or 32 bit access
1499 * mode respectively, otherwise data is handled as quickly as
1502 int target_read_buffer(struct target
*target
, uint32_t address
, uint32_t size
, uint8_t *buffer
)
1504 LOG_DEBUG("reading buffer of %i byte at 0x%8.8x",
1505 (int)size
, (unsigned)address
);
1507 if (!target_was_examined(target
)) {
1508 LOG_ERROR("Target not examined yet");
1515 if ((address
+ size
- 1) < address
) {
1516 /* GDB can request this when e.g. PC is 0xfffffffc*/
1517 LOG_ERROR("address + size wrapped(0x%08" PRIx32
", 0x%08" PRIx32
")",
1523 return target
->type
->read_buffer(target
, address
, size
, buffer
);
1526 static int target_read_buffer_default(struct target
*target
, uint32_t address
, uint32_t size
, uint8_t *buffer
)
1528 int retval
= ERROR_OK
;
1530 if (((address
% 2) == 0) && (size
== 2))
1531 return target_read_memory(target
, address
, 2, 1, buffer
);
1533 /* handle unaligned head bytes */
1535 uint32_t unaligned
= 4 - (address
% 4);
1537 if (unaligned
> size
)
1540 retval
= target_read_memory(target
, address
, 1, unaligned
, buffer
);
1541 if (retval
!= ERROR_OK
)
1544 buffer
+= unaligned
;
1545 address
+= unaligned
;
1549 /* handle aligned words */
1551 int aligned
= size
- (size
% 4);
1553 retval
= target_read_memory(target
, address
, 4, aligned
/ 4, buffer
);
1554 if (retval
!= ERROR_OK
)
1562 /*prevent byte access when possible (avoid AHB access limitations in some cases)*/
1564 int aligned
= size
- (size
% 2);
1565 retval
= target_read_memory(target
, address
, 2, aligned
/ 2, buffer
);
1566 if (retval
!= ERROR_OK
)
1573 /* handle tail writes of less than 4 bytes */
1575 retval
= target_read_memory(target
, address
, 1, size
, buffer
);
1576 if (retval
!= ERROR_OK
)
1583 int target_checksum_memory(struct target
*target
, uint32_t address
, uint32_t size
, uint32_t* crc
)
1588 uint32_t checksum
= 0;
1589 if (!target_was_examined(target
)) {
1590 LOG_ERROR("Target not examined yet");
1594 retval
= target
->type
->checksum_memory(target
, address
, size
, &checksum
);
1595 if (retval
!= ERROR_OK
) {
1596 buffer
= malloc(size
);
1597 if (buffer
== NULL
) {
1598 LOG_ERROR("error allocating buffer for section (%d bytes)", (int)size
);
1599 return ERROR_COMMAND_SYNTAX_ERROR
;
1601 retval
= target_read_buffer(target
, address
, size
, buffer
);
1602 if (retval
!= ERROR_OK
) {
1607 /* convert to target endianness */
1608 for (i
= 0; i
< (size
/sizeof(uint32_t)); i
++) {
1609 uint32_t target_data
;
1610 target_data
= target_buffer_get_u32(target
, &buffer
[i
*sizeof(uint32_t)]);
1611 target_buffer_set_u32(target
, &buffer
[i
*sizeof(uint32_t)], target_data
);
1614 retval
= image_calculate_checksum(buffer
, size
, &checksum
);
1623 int target_blank_check_memory(struct target
*target
, uint32_t address
, uint32_t size
, uint32_t* blank
)
1626 if (!target_was_examined(target
)) {
1627 LOG_ERROR("Target not examined yet");
1631 if (target
->type
->blank_check_memory
== 0)
1632 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
1634 retval
= target
->type
->blank_check_memory(target
, address
, size
, blank
);
1639 int target_read_u32(struct target
*target
, uint32_t address
, uint32_t *value
)
1641 uint8_t value_buf
[4];
1642 if (!target_was_examined(target
)) {
1643 LOG_ERROR("Target not examined yet");
1647 int retval
= target_read_memory(target
, address
, 4, 1, value_buf
);
1649 if (retval
== ERROR_OK
) {
1650 *value
= target_buffer_get_u32(target
, value_buf
);
1651 LOG_DEBUG("address: 0x%8.8" PRIx32
", value: 0x%8.8" PRIx32
"",
1656 LOG_DEBUG("address: 0x%8.8" PRIx32
" failed",
1663 int target_read_u16(struct target
*target
, uint32_t address
, uint16_t *value
)
1665 uint8_t value_buf
[2];
1666 if (!target_was_examined(target
)) {
1667 LOG_ERROR("Target not examined yet");
1671 int retval
= target_read_memory(target
, address
, 2, 1, value_buf
);
1673 if (retval
== ERROR_OK
) {
1674 *value
= target_buffer_get_u16(target
, value_buf
);
1675 LOG_DEBUG("address: 0x%8.8" PRIx32
", value: 0x%4.4x",
1680 LOG_DEBUG("address: 0x%8.8" PRIx32
" failed",
1687 int target_read_u8(struct target
*target
, uint32_t address
, uint8_t *value
)
1689 int retval
= target_read_memory(target
, address
, 1, 1, value
);
1690 if (!target_was_examined(target
)) {
1691 LOG_ERROR("Target not examined yet");
1695 if (retval
== ERROR_OK
) {
1696 LOG_DEBUG("address: 0x%8.8" PRIx32
", value: 0x%2.2x",
1701 LOG_DEBUG("address: 0x%8.8" PRIx32
" failed",
1708 int target_write_u32(struct target
*target
, uint32_t address
, uint32_t value
)
1711 uint8_t value_buf
[4];
1712 if (!target_was_examined(target
)) {
1713 LOG_ERROR("Target not examined yet");
1717 LOG_DEBUG("address: 0x%8.8" PRIx32
", value: 0x%8.8" PRIx32
"",
1721 target_buffer_set_u32(target
, value_buf
, value
);
1722 retval
= target_write_memory(target
, address
, 4, 1, value_buf
);
1723 if (retval
!= ERROR_OK
)
1724 LOG_DEBUG("failed: %i", retval
);
1729 int target_write_u16(struct target
*target
, uint32_t address
, uint16_t value
)
1732 uint8_t value_buf
[2];
1733 if (!target_was_examined(target
)) {
1734 LOG_ERROR("Target not examined yet");
1738 LOG_DEBUG("address: 0x%8.8" PRIx32
", value: 0x%8.8x",
1742 target_buffer_set_u16(target
, value_buf
, value
);
1743 retval
= target_write_memory(target
, address
, 2, 1, value_buf
);
1744 if (retval
!= ERROR_OK
)
1745 LOG_DEBUG("failed: %i", retval
);
1750 int target_write_u8(struct target
*target
, uint32_t address
, uint8_t value
)
1753 if (!target_was_examined(target
)) {
1754 LOG_ERROR("Target not examined yet");
1758 LOG_DEBUG("address: 0x%8.8" PRIx32
", value: 0x%2.2x",
1761 retval
= target_write_memory(target
, address
, 1, 1, &value
);
1762 if (retval
!= ERROR_OK
)
1763 LOG_DEBUG("failed: %i", retval
);
1768 static int find_target(struct command_context
*cmd_ctx
, const char *name
)
1770 struct target
*target
= get_target(name
);
1771 if (target
== NULL
) {
1772 LOG_ERROR("Target: %s is unknown, try one of:\n", name
);
1775 if (!target
->tap
->enabled
) {
1776 LOG_USER("Target: TAP %s is disabled, "
1777 "can't be the current target\n",
1778 target
->tap
->dotted_name
);
1782 cmd_ctx
->current_target
= target
->target_number
;
1787 COMMAND_HANDLER(handle_targets_command
)
1789 int retval
= ERROR_OK
;
1790 if (CMD_ARGC
== 1) {
1791 retval
= find_target(CMD_CTX
, CMD_ARGV
[0]);
1792 if (retval
== ERROR_OK
) {
1798 struct target
*target
= all_targets
;
1799 command_print(CMD_CTX
, " TargetName Type Endian TapName State ");
1800 command_print(CMD_CTX
, "-- ------------------ ---------- ------ ------------------ ------------");
1805 if (target
->tap
->enabled
)
1806 state
= target_state_name(target
);
1808 state
= "tap-disabled";
1810 if (CMD_CTX
->current_target
== target
->target_number
)
1813 /* keep columns lined up to match the headers above */
1814 command_print(CMD_CTX
,
1815 "%2d%c %-18s %-10s %-6s %-18s %s",
1816 target
->target_number
,
1818 target_name(target
),
1819 target_type_name(target
),
1820 Jim_Nvp_value2name_simple(nvp_target_endian
,
1821 target
->endianness
)->name
,
1822 target
->tap
->dotted_name
,
1824 target
= target
->next
;
1830 /* every 300ms we check for reset & powerdropout and issue a "reset halt" if so. */
1832 static int powerDropout
;
1833 static int srstAsserted
;
1835 static int runPowerRestore
;
1836 static int runPowerDropout
;
1837 static int runSrstAsserted
;
1838 static int runSrstDeasserted
;
1840 static int sense_handler(void)
1842 static int prevSrstAsserted
;
1843 static int prevPowerdropout
;
1845 int retval
= jtag_power_dropout(&powerDropout
);
1846 if (retval
!= ERROR_OK
)
1850 powerRestored
= prevPowerdropout
&& !powerDropout
;
1852 runPowerRestore
= 1;
1854 long long current
= timeval_ms();
1855 static long long lastPower
;
1856 int waitMore
= lastPower
+ 2000 > current
;
1857 if (powerDropout
&& !waitMore
) {
1858 runPowerDropout
= 1;
1859 lastPower
= current
;
1862 retval
= jtag_srst_asserted(&srstAsserted
);
1863 if (retval
!= ERROR_OK
)
1867 srstDeasserted
= prevSrstAsserted
&& !srstAsserted
;
1869 static long long lastSrst
;
1870 waitMore
= lastSrst
+ 2000 > current
;
1871 if (srstDeasserted
&& !waitMore
) {
1872 runSrstDeasserted
= 1;
1876 if (!prevSrstAsserted
&& srstAsserted
)
1877 runSrstAsserted
= 1;
1879 prevSrstAsserted
= srstAsserted
;
1880 prevPowerdropout
= powerDropout
;
1882 if (srstDeasserted
|| powerRestored
) {
1883 /* Other than logging the event we can't do anything here.
1884 * Issuing a reset is a particularly bad idea as we might
1885 * be inside a reset already.
1892 static int backoff_times
;
1893 static int backoff_count
;
1895 /* process target state changes */
1896 static int handle_target(void *priv
)
1898 Jim_Interp
*interp
= (Jim_Interp
*)priv
;
1899 int retval
= ERROR_OK
;
1901 if (!is_jtag_poll_safe()) {
1902 /* polling is disabled currently */
1906 /* we do not want to recurse here... */
1907 static int recursive
;
1911 /* danger! running these procedures can trigger srst assertions and power dropouts.
1912 * We need to avoid an infinite loop/recursion here and we do that by
1913 * clearing the flags after running these events.
1915 int did_something
= 0;
1916 if (runSrstAsserted
) {
1917 LOG_INFO("srst asserted detected, running srst_asserted proc.");
1918 Jim_Eval(interp
, "srst_asserted");
1921 if (runSrstDeasserted
) {
1922 Jim_Eval(interp
, "srst_deasserted");
1925 if (runPowerDropout
) {
1926 LOG_INFO("Power dropout detected, running power_dropout proc.");
1927 Jim_Eval(interp
, "power_dropout");
1930 if (runPowerRestore
) {
1931 Jim_Eval(interp
, "power_restore");
1935 if (did_something
) {
1936 /* clear detect flags */
1940 /* clear action flags */
1942 runSrstAsserted
= 0;
1943 runSrstDeasserted
= 0;
1944 runPowerRestore
= 0;
1945 runPowerDropout
= 0;
1950 if (backoff_times
> backoff_count
) {
1951 /* do not poll this time as we failed previously */
1957 /* Poll targets for state changes unless that's globally disabled.
1958 * Skip targets that are currently disabled.
1960 for (struct target
*target
= all_targets
;
1961 is_jtag_poll_safe() && target
;
1962 target
= target
->next
) {
1963 if (!target
->tap
->enabled
)
1966 /* only poll target if we've got power and srst isn't asserted */
1967 if (!powerDropout
&& !srstAsserted
) {
1968 /* polling may fail silently until the target has been examined */
1969 retval
= target_poll(target
);
1970 if (retval
!= ERROR_OK
) {
1971 /* 100ms polling interval. Increase interval between polling up to 5000ms */
1972 if (backoff_times
* polling_interval
< 5000) {
1976 LOG_USER("Polling target failed, GDB will be halted. Polling again in %dms",
1977 backoff_times
* polling_interval
);
1979 /* Tell GDB to halt the debugger. This allows the user to
1980 * run monitor commands to handle the situation.
1982 target_call_event_callbacks(target
, TARGET_EVENT_GDB_HALT
);
1985 /* Since we succeeded, we reset backoff count */
1986 if (backoff_times
> 0)
1987 LOG_USER("Polling succeeded again");
1995 COMMAND_HANDLER(handle_reg_command
)
1997 struct target
*target
;
1998 struct reg
*reg
= NULL
;
2004 target
= get_current_target(CMD_CTX
);
2006 /* list all available registers for the current target */
2007 if (CMD_ARGC
== 0) {
2008 struct reg_cache
*cache
= target
->reg_cache
;
2014 command_print(CMD_CTX
, "===== %s", cache
->name
);
2016 for (i
= 0, reg
= cache
->reg_list
;
2017 i
< cache
->num_regs
;
2018 i
++, reg
++, count
++) {
2019 /* only print cached values if they are valid */
2021 value
= buf_to_str(reg
->value
,
2023 command_print(CMD_CTX
,
2024 "(%i) %s (/%" PRIu32
"): 0x%s%s",
2032 command_print(CMD_CTX
, "(%i) %s (/%" PRIu32
")",
2037 cache
= cache
->next
;
2043 /* access a single register by its ordinal number */
2044 if ((CMD_ARGV
[0][0] >= '0') && (CMD_ARGV
[0][0] <= '9')) {
2046 COMMAND_PARSE_NUMBER(uint
, CMD_ARGV
[0], num
);
2048 struct reg_cache
*cache
= target
->reg_cache
;
2052 for (i
= 0; i
< cache
->num_regs
; i
++) {
2053 if (count
++ == num
) {
2054 reg
= &cache
->reg_list
[i
];
2060 cache
= cache
->next
;
2064 command_print(CMD_CTX
, "%i is out of bounds, the current target "
2065 "has only %i registers (0 - %i)", num
, count
, count
- 1);
2069 /* access a single register by its name */
2070 reg
= register_get_by_name(target
->reg_cache
, CMD_ARGV
[0], 1);
2073 command_print(CMD_CTX
, "register %s not found in current target", CMD_ARGV
[0]);
2078 assert(reg
!= NULL
); /* give clang a hint that we *know* reg is != NULL here */
2080 /* display a register */
2081 if ((CMD_ARGC
== 1) || ((CMD_ARGC
== 2) && !((CMD_ARGV
[1][0] >= '0')
2082 && (CMD_ARGV
[1][0] <= '9')))) {
2083 if ((CMD_ARGC
== 2) && (strcmp(CMD_ARGV
[1], "force") == 0))
2086 if (reg
->valid
== 0)
2087 reg
->type
->get(reg
);
2088 value
= buf_to_str(reg
->value
, reg
->size
, 16);
2089 command_print(CMD_CTX
, "%s (/%i): 0x%s", reg
->name
, (int)(reg
->size
), value
);
2094 /* set register value */
2095 if (CMD_ARGC
== 2) {
2096 uint8_t *buf
= malloc(DIV_ROUND_UP(reg
->size
, 8));
2099 str_to_buf(CMD_ARGV
[1], strlen(CMD_ARGV
[1]), buf
, reg
->size
, 0);
2101 reg
->type
->set(reg
, buf
);
2103 value
= buf_to_str(reg
->value
, reg
->size
, 16);
2104 command_print(CMD_CTX
, "%s (/%i): 0x%s", reg
->name
, (int)(reg
->size
), value
);
2112 return ERROR_COMMAND_SYNTAX_ERROR
;
2115 COMMAND_HANDLER(handle_poll_command
)
2117 int retval
= ERROR_OK
;
2118 struct target
*target
= get_current_target(CMD_CTX
);
2120 if (CMD_ARGC
== 0) {
2121 command_print(CMD_CTX
, "background polling: %s",
2122 jtag_poll_get_enabled() ? "on" : "off");
2123 command_print(CMD_CTX
, "TAP: %s (%s)",
2124 target
->tap
->dotted_name
,
2125 target
->tap
->enabled
? "enabled" : "disabled");
2126 if (!target
->tap
->enabled
)
2128 retval
= target_poll(target
);
2129 if (retval
!= ERROR_OK
)
2131 retval
= target_arch_state(target
);
2132 if (retval
!= ERROR_OK
)
2134 } else if (CMD_ARGC
== 1) {
2136 COMMAND_PARSE_ON_OFF(CMD_ARGV
[0], enable
);
2137 jtag_poll_set_enabled(enable
);
2139 return ERROR_COMMAND_SYNTAX_ERROR
;
2144 COMMAND_HANDLER(handle_wait_halt_command
)
2147 return ERROR_COMMAND_SYNTAX_ERROR
;
2150 if (1 == CMD_ARGC
) {
2151 int retval
= parse_uint(CMD_ARGV
[0], &ms
);
2152 if (ERROR_OK
!= retval
)
2153 return ERROR_COMMAND_SYNTAX_ERROR
;
2154 /* convert seconds (given) to milliseconds (needed) */
2158 struct target
*target
= get_current_target(CMD_CTX
);
2159 return target_wait_state(target
, TARGET_HALTED
, ms
);
2162 /* wait for target state to change. The trick here is to have a low
2163 * latency for short waits and not to suck up all the CPU time
2166 * After 500ms, keep_alive() is invoked
2168 int target_wait_state(struct target
*target
, enum target_state state
, int ms
)
2171 long long then
= 0, cur
;
2175 retval
= target_poll(target
);
2176 if (retval
!= ERROR_OK
)
2178 if (target
->state
== state
)
2183 then
= timeval_ms();
2184 LOG_DEBUG("waiting for target %s...",
2185 Jim_Nvp_value2name_simple(nvp_target_state
, state
)->name
);
2191 if ((cur
-then
) > ms
) {
2192 LOG_ERROR("timed out while waiting for target %s",
2193 Jim_Nvp_value2name_simple(nvp_target_state
, state
)->name
);
2201 COMMAND_HANDLER(handle_halt_command
)
2205 struct target
*target
= get_current_target(CMD_CTX
);
2206 int retval
= target_halt(target
);
2207 if (ERROR_OK
!= retval
)
2210 if (CMD_ARGC
== 1) {
2211 unsigned wait_local
;
2212 retval
= parse_uint(CMD_ARGV
[0], &wait_local
);
2213 if (ERROR_OK
!= retval
)
2214 return ERROR_COMMAND_SYNTAX_ERROR
;
2219 return CALL_COMMAND_HANDLER(handle_wait_halt_command
);
2222 COMMAND_HANDLER(handle_soft_reset_halt_command
)
2224 struct target
*target
= get_current_target(CMD_CTX
);
2226 LOG_USER("requesting target halt and executing a soft reset");
2228 target
->type
->soft_reset_halt(target
);
2233 COMMAND_HANDLER(handle_reset_command
)
2236 return ERROR_COMMAND_SYNTAX_ERROR
;
2238 enum target_reset_mode reset_mode
= RESET_RUN
;
2239 if (CMD_ARGC
== 1) {
2241 n
= Jim_Nvp_name2value_simple(nvp_reset_modes
, CMD_ARGV
[0]);
2242 if ((n
->name
== NULL
) || (n
->value
== RESET_UNKNOWN
))
2243 return ERROR_COMMAND_SYNTAX_ERROR
;
2244 reset_mode
= n
->value
;
2247 /* reset *all* targets */
2248 return target_process_reset(CMD_CTX
, reset_mode
);
2252 COMMAND_HANDLER(handle_resume_command
)
2256 return ERROR_COMMAND_SYNTAX_ERROR
;
2258 struct target
*target
= get_current_target(CMD_CTX
);
2259 target_handle_event(target
, TARGET_EVENT_OLD_pre_resume
);
2261 /* with no CMD_ARGV, resume from current pc, addr = 0,
2262 * with one arguments, addr = CMD_ARGV[0],
2263 * handle breakpoints, not debugging */
2265 if (CMD_ARGC
== 1) {
2266 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], addr
);
2270 return target_resume(target
, current
, addr
, 1, 0);
2273 COMMAND_HANDLER(handle_step_command
)
2276 return ERROR_COMMAND_SYNTAX_ERROR
;
2280 /* with no CMD_ARGV, step from current pc, addr = 0,
2281 * with one argument addr = CMD_ARGV[0],
2282 * handle breakpoints, debugging */
2285 if (CMD_ARGC
== 1) {
2286 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], addr
);
2290 struct target
*target
= get_current_target(CMD_CTX
);
2292 return target
->type
->step(target
, current_pc
, addr
, 1);
2295 static void handle_md_output(struct command_context
*cmd_ctx
,
2296 struct target
*target
, uint32_t address
, unsigned size
,
2297 unsigned count
, const uint8_t *buffer
)
2299 const unsigned line_bytecnt
= 32;
2300 unsigned line_modulo
= line_bytecnt
/ size
;
2302 char output
[line_bytecnt
* 4 + 1];
2303 unsigned output_len
= 0;
2305 const char *value_fmt
;
2308 value_fmt
= "%8.8x ";
2311 value_fmt
= "%4.4x ";
2314 value_fmt
= "%2.2x ";
2317 /* "can't happen", caller checked */
2318 LOG_ERROR("invalid memory read size: %u", size
);
2322 for (unsigned i
= 0; i
< count
; i
++) {
2323 if (i
% line_modulo
== 0) {
2324 output_len
+= snprintf(output
+ output_len
,
2325 sizeof(output
) - output_len
,
2327 (unsigned)(address
+ (i
*size
)));
2331 const uint8_t *value_ptr
= buffer
+ i
* size
;
2334 value
= target_buffer_get_u32(target
, value_ptr
);
2337 value
= target_buffer_get_u16(target
, value_ptr
);
2342 output_len
+= snprintf(output
+ output_len
,
2343 sizeof(output
) - output_len
,
2346 if ((i
% line_modulo
== line_modulo
- 1) || (i
== count
- 1)) {
2347 command_print(cmd_ctx
, "%s", output
);
2353 COMMAND_HANDLER(handle_md_command
)
2356 return ERROR_COMMAND_SYNTAX_ERROR
;
2359 switch (CMD_NAME
[2]) {
2370 return ERROR_COMMAND_SYNTAX_ERROR
;
2373 bool physical
= strcmp(CMD_ARGV
[0], "phys") == 0;
2374 int (*fn
)(struct target
*target
,
2375 uint32_t address
, uint32_t size_value
, uint32_t count
, uint8_t *buffer
);
2379 fn
= target_read_phys_memory
;
2381 fn
= target_read_memory
;
2382 if ((CMD_ARGC
< 1) || (CMD_ARGC
> 2))
2383 return ERROR_COMMAND_SYNTAX_ERROR
;
2386 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], address
);
2390 COMMAND_PARSE_NUMBER(uint
, CMD_ARGV
[1], count
);
2392 uint8_t *buffer
= calloc(count
, size
);
2394 struct target
*target
= get_current_target(CMD_CTX
);
2395 int retval
= fn(target
, address
, size
, count
, buffer
);
2396 if (ERROR_OK
== retval
)
2397 handle_md_output(CMD_CTX
, target
, address
, size
, count
, buffer
);
2404 typedef int (*target_write_fn
)(struct target
*target
,
2405 uint32_t address
, uint32_t size
, uint32_t count
, const uint8_t *buffer
);
2407 static int target_write_memory_fast(struct target
*target
,
2408 uint32_t address
, uint32_t size
, uint32_t count
, const uint8_t *buffer
)
2410 return target_write_buffer(target
, address
, size
* count
, buffer
);
2413 static int target_fill_mem(struct target
*target
,
2422 /* We have to write in reasonably large chunks to be able
2423 * to fill large memory areas with any sane speed */
2424 const unsigned chunk_size
= 16384;
2425 uint8_t *target_buf
= malloc(chunk_size
* data_size
);
2426 if (target_buf
== NULL
) {
2427 LOG_ERROR("Out of memory");
2431 for (unsigned i
= 0; i
< chunk_size
; i
++) {
2432 switch (data_size
) {
2434 target_buffer_set_u32(target
, target_buf
+ i
* data_size
, b
);
2437 target_buffer_set_u16(target
, target_buf
+ i
* data_size
, b
);
2440 target_buffer_set_u8(target
, target_buf
+ i
* data_size
, b
);
2447 int retval
= ERROR_OK
;
2449 for (unsigned x
= 0; x
< c
; x
+= chunk_size
) {
2452 if (current
> chunk_size
)
2453 current
= chunk_size
;
2454 retval
= fn(target
, address
+ x
* data_size
, data_size
, current
, target_buf
);
2455 if (retval
!= ERROR_OK
)
2457 /* avoid GDB timeouts */
2466 COMMAND_HANDLER(handle_mw_command
)
2469 return ERROR_COMMAND_SYNTAX_ERROR
;
2470 bool physical
= strcmp(CMD_ARGV
[0], "phys") == 0;
2475 fn
= target_write_phys_memory
;
2477 fn
= target_write_memory_fast
;
2478 if ((CMD_ARGC
< 2) || (CMD_ARGC
> 3))
2479 return ERROR_COMMAND_SYNTAX_ERROR
;
2482 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], address
);
2485 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], value
);
2489 COMMAND_PARSE_NUMBER(uint
, CMD_ARGV
[2], count
);
2491 struct target
*target
= get_current_target(CMD_CTX
);
2493 switch (CMD_NAME
[2]) {
2504 return ERROR_COMMAND_SYNTAX_ERROR
;
2507 return target_fill_mem(target
, address
, fn
, wordsize
, value
, count
);
2510 static COMMAND_HELPER(parse_load_image_command_CMD_ARGV
, struct image
*image
,
2511 uint32_t *min_address
, uint32_t *max_address
)
2513 if (CMD_ARGC
< 1 || CMD_ARGC
> 5)
2514 return ERROR_COMMAND_SYNTAX_ERROR
;
2516 /* a base address isn't always necessary,
2517 * default to 0x0 (i.e. don't relocate) */
2518 if (CMD_ARGC
>= 2) {
2520 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], addr
);
2521 image
->base_address
= addr
;
2522 image
->base_address_set
= 1;
2524 image
->base_address_set
= 0;
2526 image
->start_address_set
= 0;
2529 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[3], *min_address
);
2530 if (CMD_ARGC
== 5) {
2531 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[4], *max_address
);
2532 /* use size (given) to find max (required) */
2533 *max_address
+= *min_address
;
2536 if (*min_address
> *max_address
)
2537 return ERROR_COMMAND_SYNTAX_ERROR
;
2542 COMMAND_HANDLER(handle_load_image_command
)
2546 uint32_t image_size
;
2547 uint32_t min_address
= 0;
2548 uint32_t max_address
= 0xffffffff;
2552 int retval
= CALL_COMMAND_HANDLER(parse_load_image_command_CMD_ARGV
,
2553 &image
, &min_address
, &max_address
);
2554 if (ERROR_OK
!= retval
)
2557 struct target
*target
= get_current_target(CMD_CTX
);
2559 struct duration bench
;
2560 duration_start(&bench
);
2562 if (image_open(&image
, CMD_ARGV
[0], (CMD_ARGC
>= 3) ? CMD_ARGV
[2] : NULL
) != ERROR_OK
)
2567 for (i
= 0; i
< image
.num_sections
; i
++) {
2568 buffer
= malloc(image
.sections
[i
].size
);
2569 if (buffer
== NULL
) {
2570 command_print(CMD_CTX
,
2571 "error allocating buffer for section (%d bytes)",
2572 (int)(image
.sections
[i
].size
));
2576 retval
= image_read_section(&image
, i
, 0x0, image
.sections
[i
].size
, buffer
, &buf_cnt
);
2577 if (retval
!= ERROR_OK
) {
2582 uint32_t offset
= 0;
2583 uint32_t length
= buf_cnt
;
2585 /* DANGER!!! beware of unsigned comparision here!!! */
2587 if ((image
.sections
[i
].base_address
+ buf_cnt
>= min_address
) &&
2588 (image
.sections
[i
].base_address
< max_address
)) {
2590 if (image
.sections
[i
].base_address
< min_address
) {
2591 /* clip addresses below */
2592 offset
+= min_address
-image
.sections
[i
].base_address
;
2596 if (image
.sections
[i
].base_address
+ buf_cnt
> max_address
)
2597 length
-= (image
.sections
[i
].base_address
+ buf_cnt
)-max_address
;
2599 retval
= target_write_buffer(target
,
2600 image
.sections
[i
].base_address
+ offset
, length
, buffer
+ offset
);
2601 if (retval
!= ERROR_OK
) {
2605 image_size
+= length
;
2606 command_print(CMD_CTX
, "%u bytes written at address 0x%8.8" PRIx32
"",
2607 (unsigned int)length
,
2608 image
.sections
[i
].base_address
+ offset
);
2614 if ((ERROR_OK
== retval
) && (duration_measure(&bench
) == ERROR_OK
)) {
2615 command_print(CMD_CTX
, "downloaded %" PRIu32
" bytes "
2616 "in %fs (%0.3f KiB/s)", image_size
,
2617 duration_elapsed(&bench
), duration_kbps(&bench
, image_size
));
2620 image_close(&image
);
2626 COMMAND_HANDLER(handle_dump_image_command
)
2628 struct fileio fileio
;
2630 int retval
, retvaltemp
;
2631 uint32_t address
, size
;
2632 struct duration bench
;
2633 struct target
*target
= get_current_target(CMD_CTX
);
2636 return ERROR_COMMAND_SYNTAX_ERROR
;
2638 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], address
);
2639 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[2], size
);
2641 uint32_t buf_size
= (size
> 4096) ? 4096 : size
;
2642 buffer
= malloc(buf_size
);
2646 retval
= fileio_open(&fileio
, CMD_ARGV
[0], FILEIO_WRITE
, FILEIO_BINARY
);
2647 if (retval
!= ERROR_OK
) {
2652 duration_start(&bench
);
2655 size_t size_written
;
2656 uint32_t this_run_size
= (size
> buf_size
) ? buf_size
: size
;
2657 retval
= target_read_buffer(target
, address
, this_run_size
, buffer
);
2658 if (retval
!= ERROR_OK
)
2661 retval
= fileio_write(&fileio
, this_run_size
, buffer
, &size_written
);
2662 if (retval
!= ERROR_OK
)
2665 size
-= this_run_size
;
2666 address
+= this_run_size
;
2671 if ((ERROR_OK
== retval
) && (duration_measure(&bench
) == ERROR_OK
)) {
2673 retval
= fileio_size(&fileio
, &filesize
);
2674 if (retval
!= ERROR_OK
)
2676 command_print(CMD_CTX
,
2677 "dumped %ld bytes in %fs (%0.3f KiB/s)", (long)filesize
,
2678 duration_elapsed(&bench
), duration_kbps(&bench
, filesize
));
2681 retvaltemp
= fileio_close(&fileio
);
2682 if (retvaltemp
!= ERROR_OK
)
2688 static COMMAND_HELPER(handle_verify_image_command_internal
, int verify
)
2692 uint32_t image_size
;
2695 uint32_t checksum
= 0;
2696 uint32_t mem_checksum
= 0;
2700 struct target
*target
= get_current_target(CMD_CTX
);
2703 return ERROR_COMMAND_SYNTAX_ERROR
;
2706 LOG_ERROR("no target selected");
2710 struct duration bench
;
2711 duration_start(&bench
);
2713 if (CMD_ARGC
>= 2) {
2715 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], addr
);
2716 image
.base_address
= addr
;
2717 image
.base_address_set
= 1;
2719 image
.base_address_set
= 0;
2720 image
.base_address
= 0x0;
2723 image
.start_address_set
= 0;
2725 retval
= image_open(&image
, CMD_ARGV
[0], (CMD_ARGC
== 3) ? CMD_ARGV
[2] : NULL
);
2726 if (retval
!= ERROR_OK
)
2732 for (i
= 0; i
< image
.num_sections
; i
++) {
2733 buffer
= malloc(image
.sections
[i
].size
);
2734 if (buffer
== NULL
) {
2735 command_print(CMD_CTX
,
2736 "error allocating buffer for section (%d bytes)",
2737 (int)(image
.sections
[i
].size
));
2740 retval
= image_read_section(&image
, i
, 0x0, image
.sections
[i
].size
, buffer
, &buf_cnt
);
2741 if (retval
!= ERROR_OK
) {
2747 /* calculate checksum of image */
2748 retval
= image_calculate_checksum(buffer
, buf_cnt
, &checksum
);
2749 if (retval
!= ERROR_OK
) {
2754 retval
= target_checksum_memory(target
, image
.sections
[i
].base_address
, buf_cnt
, &mem_checksum
);
2755 if (retval
!= ERROR_OK
) {
2760 if (checksum
!= mem_checksum
) {
2761 /* failed crc checksum, fall back to a binary compare */
2765 LOG_ERROR("checksum mismatch - attempting binary compare");
2767 data
= (uint8_t *)malloc(buf_cnt
);
2769 /* Can we use 32bit word accesses? */
2771 int count
= buf_cnt
;
2772 if ((count
% 4) == 0) {
2776 retval
= target_read_memory(target
, image
.sections
[i
].base_address
, size
, count
, data
);
2777 if (retval
== ERROR_OK
) {
2779 for (t
= 0; t
< buf_cnt
; t
++) {
2780 if (data
[t
] != buffer
[t
]) {
2781 command_print(CMD_CTX
,
2782 "diff %d address 0x%08x. Was 0x%02x instead of 0x%02x",
2784 (unsigned)(t
+ image
.sections
[i
].base_address
),
2787 if (diffs
++ >= 127) {
2788 command_print(CMD_CTX
, "More than 128 errors, the rest are not printed.");
2800 command_print(CMD_CTX
, "address 0x%08" PRIx32
" length 0x%08zx",
2801 image
.sections
[i
].base_address
,
2806 image_size
+= buf_cnt
;
2809 command_print(CMD_CTX
, "No more differences found.");
2812 retval
= ERROR_FAIL
;
2813 if ((ERROR_OK
== retval
) && (duration_measure(&bench
) == ERROR_OK
)) {
2814 command_print(CMD_CTX
, "verified %" PRIu32
" bytes "
2815 "in %fs (%0.3f KiB/s)", image_size
,
2816 duration_elapsed(&bench
), duration_kbps(&bench
, image_size
));
2819 image_close(&image
);
2824 COMMAND_HANDLER(handle_verify_image_command
)
2826 return CALL_COMMAND_HANDLER(handle_verify_image_command_internal
, 1);
2829 COMMAND_HANDLER(handle_test_image_command
)
2831 return CALL_COMMAND_HANDLER(handle_verify_image_command_internal
, 0);
2834 static int handle_bp_command_list(struct command_context
*cmd_ctx
)
2836 struct target
*target
= get_current_target(cmd_ctx
);
2837 struct breakpoint
*breakpoint
= target
->breakpoints
;
2838 while (breakpoint
) {
2839 if (breakpoint
->type
== BKPT_SOFT
) {
2840 char *buf
= buf_to_str(breakpoint
->orig_instr
,
2841 breakpoint
->length
, 16);
2842 command_print(cmd_ctx
, "IVA breakpoint: 0x%8.8" PRIx32
", 0x%x, %i, 0x%s",
2843 breakpoint
->address
,
2845 breakpoint
->set
, buf
);
2848 if ((breakpoint
->address
== 0) && (breakpoint
->asid
!= 0))
2849 command_print(cmd_ctx
, "Context breakpoint: 0x%8.8" PRIx32
", 0x%x, %i",
2851 breakpoint
->length
, breakpoint
->set
);
2852 else if ((breakpoint
->address
!= 0) && (breakpoint
->asid
!= 0)) {
2853 command_print(cmd_ctx
, "Hybrid breakpoint(IVA): 0x%8.8" PRIx32
", 0x%x, %i",
2854 breakpoint
->address
,
2855 breakpoint
->length
, breakpoint
->set
);
2856 command_print(cmd_ctx
, "\t|--->linked with ContextID: 0x%8.8" PRIx32
,
2859 command_print(cmd_ctx
, "Breakpoint(IVA): 0x%8.8" PRIx32
", 0x%x, %i",
2860 breakpoint
->address
,
2861 breakpoint
->length
, breakpoint
->set
);
2864 breakpoint
= breakpoint
->next
;
2869 static int handle_bp_command_set(struct command_context
*cmd_ctx
,
2870 uint32_t addr
, uint32_t asid
, uint32_t length
, int hw
)
2872 struct target
*target
= get_current_target(cmd_ctx
);
2875 int retval
= breakpoint_add(target
, addr
, length
, hw
);
2876 if (ERROR_OK
== retval
)
2877 command_print(cmd_ctx
, "breakpoint set at 0x%8.8" PRIx32
"", addr
);
2879 LOG_ERROR("Failure setting breakpoint, the same address(IVA) is already used");
2882 } else if (addr
== 0) {
2883 int retval
= context_breakpoint_add(target
, asid
, length
, hw
);
2884 if (ERROR_OK
== retval
)
2885 command_print(cmd_ctx
, "Context breakpoint set at 0x%8.8" PRIx32
"", asid
);
2887 LOG_ERROR("Failure setting breakpoint, the same address(CONTEXTID) is already used");
2891 int retval
= hybrid_breakpoint_add(target
, addr
, asid
, length
, hw
);
2892 if (ERROR_OK
== retval
)
2893 command_print(cmd_ctx
, "Hybrid breakpoint set at 0x%8.8" PRIx32
"", asid
);
2895 LOG_ERROR("Failure setting breakpoint, the same address is already used");
2902 COMMAND_HANDLER(handle_bp_command
)
2911 return handle_bp_command_list(CMD_CTX
);
2915 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], addr
);
2916 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], length
);
2917 return handle_bp_command_set(CMD_CTX
, addr
, asid
, length
, hw
);
2920 if (strcmp(CMD_ARGV
[2], "hw") == 0) {
2922 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], addr
);
2924 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], length
);
2927 return handle_bp_command_set(CMD_CTX
, addr
, asid
, length
, hw
);
2928 } else if (strcmp(CMD_ARGV
[2], "hw_ctx") == 0) {
2930 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], asid
);
2931 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], length
);
2933 return handle_bp_command_set(CMD_CTX
, addr
, asid
, length
, hw
);
2938 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], addr
);
2939 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], asid
);
2940 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[2], length
);
2941 return handle_bp_command_set(CMD_CTX
, addr
, asid
, length
, hw
);
2944 return ERROR_COMMAND_SYNTAX_ERROR
;
2948 COMMAND_HANDLER(handle_rbp_command
)
2951 return ERROR_COMMAND_SYNTAX_ERROR
;
2954 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], addr
);
2956 struct target
*target
= get_current_target(CMD_CTX
);
2957 breakpoint_remove(target
, addr
);
2962 COMMAND_HANDLER(handle_wp_command
)
2964 struct target
*target
= get_current_target(CMD_CTX
);
2966 if (CMD_ARGC
== 0) {
2967 struct watchpoint
*watchpoint
= target
->watchpoints
;
2969 while (watchpoint
) {
2970 command_print(CMD_CTX
, "address: 0x%8.8" PRIx32
2971 ", len: 0x%8.8" PRIx32
2972 ", r/w/a: %i, value: 0x%8.8" PRIx32
2973 ", mask: 0x%8.8" PRIx32
,
2974 watchpoint
->address
,
2976 (int)watchpoint
->rw
,
2979 watchpoint
= watchpoint
->next
;
2984 enum watchpoint_rw type
= WPT_ACCESS
;
2986 uint32_t length
= 0;
2987 uint32_t data_value
= 0x0;
2988 uint32_t data_mask
= 0xffffffff;
2992 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[4], data_mask
);
2995 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[3], data_value
);
2998 switch (CMD_ARGV
[2][0]) {
3009 LOG_ERROR("invalid watchpoint mode ('%c')", CMD_ARGV
[2][0]);
3010 return ERROR_COMMAND_SYNTAX_ERROR
;
3014 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], length
);
3015 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], addr
);
3019 return ERROR_COMMAND_SYNTAX_ERROR
;
3022 int retval
= watchpoint_add(target
, addr
, length
, type
,
3023 data_value
, data_mask
);
3024 if (ERROR_OK
!= retval
)
3025 LOG_ERROR("Failure setting watchpoints");
3030 COMMAND_HANDLER(handle_rwp_command
)
3033 return ERROR_COMMAND_SYNTAX_ERROR
;
3036 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], addr
);
3038 struct target
*target
= get_current_target(CMD_CTX
);
3039 watchpoint_remove(target
, addr
);
3045 * Translate a virtual address to a physical address.
3047 * The low-level target implementation must have logged a detailed error
3048 * which is forwarded to telnet/GDB session.
3050 COMMAND_HANDLER(handle_virt2phys_command
)
3053 return ERROR_COMMAND_SYNTAX_ERROR
;
3056 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], va
);
3059 struct target
*target
= get_current_target(CMD_CTX
);
3060 int retval
= target
->type
->virt2phys(target
, va
, &pa
);
3061 if (retval
== ERROR_OK
)
3062 command_print(CMD_CTX
, "Physical address 0x%08" PRIx32
"", pa
);
3067 static void writeData(FILE *f
, const void *data
, size_t len
)
3069 size_t written
= fwrite(data
, 1, len
, f
);
3071 LOG_ERROR("failed to write %zu bytes: %s", len
, strerror(errno
));
3074 static void writeLong(FILE *f
, int l
)
3077 for (i
= 0; i
< 4; i
++) {
3078 char c
= (l
>> (i
*8))&0xff;
3079 writeData(f
, &c
, 1);
3084 static void writeString(FILE *f
, char *s
)
3086 writeData(f
, s
, strlen(s
));
3089 /* Dump a gmon.out histogram file. */
3090 static void writeGmon(uint32_t *samples
, uint32_t sampleNum
, const char *filename
)
3093 FILE *f
= fopen(filename
, "w");
3096 writeString(f
, "gmon");
3097 writeLong(f
, 0x00000001); /* Version */
3098 writeLong(f
, 0); /* padding */
3099 writeLong(f
, 0); /* padding */
3100 writeLong(f
, 0); /* padding */
3102 uint8_t zero
= 0; /* GMON_TAG_TIME_HIST */
3103 writeData(f
, &zero
, 1);
3105 /* figure out bucket size */
3106 uint32_t min
= samples
[0];
3107 uint32_t max
= samples
[0];
3108 for (i
= 0; i
< sampleNum
; i
++) {
3109 if (min
> samples
[i
])
3111 if (max
< samples
[i
])
3115 int addressSpace
= (max
- min
+ 1);
3116 assert(addressSpace
>= 2);
3118 static const uint32_t maxBuckets
= 16 * 1024; /* maximum buckets. */
3119 uint32_t length
= addressSpace
;
3120 if (length
> maxBuckets
)
3121 length
= maxBuckets
;
3122 int *buckets
= malloc(sizeof(int)*length
);
3123 if (buckets
== NULL
) {
3127 memset(buckets
, 0, sizeof(int) * length
);
3128 for (i
= 0; i
< sampleNum
; i
++) {
3129 uint32_t address
= samples
[i
];
3130 long long a
= address
- min
;
3131 long long b
= length
- 1;
3132 long long c
= addressSpace
- 1;
3133 int index_t
= (a
* b
) / c
; /* danger!!!! int32 overflows */
3137 /* append binary memory gmon.out &profile_hist_hdr ((char*)&profile_hist_hdr + sizeof(struct gmon_hist_hdr)) */
3138 writeLong(f
, min
); /* low_pc */
3139 writeLong(f
, max
); /* high_pc */
3140 writeLong(f
, length
); /* # of samples */
3141 writeLong(f
, 100); /* KLUDGE! We lie, ca. 100Hz best case. */
3142 writeString(f
, "seconds");
3143 for (i
= 0; i
< (15-strlen("seconds")); i
++)
3144 writeData(f
, &zero
, 1);
3145 writeString(f
, "s");
3147 /*append binary memory gmon.out profile_hist_data (profile_hist_data + profile_hist_hdr.hist_size) */
3149 char *data
= malloc(2 * length
);
3151 for (i
= 0; i
< length
; i
++) {
3156 data
[i
* 2] = val
&0xff;
3157 data
[i
* 2 + 1] = (val
>> 8) & 0xff;
3160 writeData(f
, data
, length
* 2);
3168 /* profiling samples the CPU PC as quickly as OpenOCD is able,
3169 * which will be used as a random sampling of PC */
3170 COMMAND_HANDLER(handle_profile_command
)
3172 struct target
*target
= get_current_target(CMD_CTX
);
3173 struct timeval timeout
, now
;
3175 gettimeofday(&timeout
, NULL
);
3177 return ERROR_COMMAND_SYNTAX_ERROR
;
3179 COMMAND_PARSE_NUMBER(uint
, CMD_ARGV
[0], offset
);
3181 timeval_add_time(&timeout
, offset
, 0);
3184 * @todo: Some cores let us sample the PC without the
3185 * annoying halt/resume step; for example, ARMv7 PCSR.
3186 * Provide a way to use that more efficient mechanism.
3189 command_print(CMD_CTX
, "Starting profiling. Halting and resuming the target as often as we can...");
3191 static const int maxSample
= 10000;
3192 uint32_t *samples
= malloc(sizeof(uint32_t)*maxSample
);
3193 if (samples
== NULL
)
3197 /* hopefully it is safe to cache! We want to stop/restart as quickly as possible. */
3198 struct reg
*reg
= register_get_by_name(target
->reg_cache
, "pc", 1);
3200 int retval
= ERROR_OK
;
3202 target_poll(target
);
3203 if (target
->state
== TARGET_HALTED
) {
3204 uint32_t t
= *((uint32_t *)reg
->value
);
3205 samples
[numSamples
++] = t
;
3206 /* current pc, addr = 0, do not handle breakpoints, not debugging */
3207 retval
= target_resume(target
, 1, 0, 0, 0);
3208 target_poll(target
);
3209 alive_sleep(10); /* sleep 10ms, i.e. <100 samples/second. */
3210 } else if (target
->state
== TARGET_RUNNING
) {
3211 /* We want to quickly sample the PC. */
3212 retval
= target_halt(target
);
3213 if (retval
!= ERROR_OK
) {
3218 command_print(CMD_CTX
, "Target not halted or running");
3222 if (retval
!= ERROR_OK
)
3225 gettimeofday(&now
, NULL
);
3226 if ((numSamples
>= maxSample
) || ((now
.tv_sec
>= timeout
.tv_sec
)
3227 && (now
.tv_usec
>= timeout
.tv_usec
))) {
3228 command_print(CMD_CTX
, "Profiling completed. %d samples.", numSamples
);
3229 retval
= target_poll(target
);
3230 if (retval
!= ERROR_OK
) {
3234 if (target
->state
== TARGET_HALTED
) {
3235 /* current pc, addr = 0, do not handle
3236 * breakpoints, not debugging */
3237 target_resume(target
, 1, 0, 0, 0);
3239 retval
= target_poll(target
);
3240 if (retval
!= ERROR_OK
) {
3244 writeGmon(samples
, numSamples
, CMD_ARGV
[1]);
3245 command_print(CMD_CTX
, "Wrote %s", CMD_ARGV
[1]);
3254 static int new_int_array_element(Jim_Interp
*interp
, const char *varname
, int idx
, uint32_t val
)
3257 Jim_Obj
*nameObjPtr
, *valObjPtr
;
3260 namebuf
= alloc_printf("%s(%d)", varname
, idx
);
3264 nameObjPtr
= Jim_NewStringObj(interp
, namebuf
, -1);
3265 valObjPtr
= Jim_NewIntObj(interp
, val
);
3266 if (!nameObjPtr
|| !valObjPtr
) {
3271 Jim_IncrRefCount(nameObjPtr
);
3272 Jim_IncrRefCount(valObjPtr
);
3273 result
= Jim_SetVariable(interp
, nameObjPtr
, valObjPtr
);
3274 Jim_DecrRefCount(interp
, nameObjPtr
);
3275 Jim_DecrRefCount(interp
, valObjPtr
);
3277 /* printf("%s(%d) <= 0%08x\n", varname, idx, val); */
3281 static int jim_mem2array(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
3283 struct command_context
*context
;
3284 struct target
*target
;
3286 context
= current_command_context(interp
);
3287 assert(context
!= NULL
);
3289 target
= get_current_target(context
);
3290 if (target
== NULL
) {
3291 LOG_ERROR("mem2array: no current target");
3295 return target_mem2array(interp
, target
, argc
- 1, argv
+ 1);
3298 static int target_mem2array(Jim_Interp
*interp
, struct target
*target
, int argc
, Jim_Obj
*const *argv
)
3306 const char *varname
;
3310 /* argv[1] = name of array to receive the data
3311 * argv[2] = desired width
3312 * argv[3] = memory address
3313 * argv[4] = count of times to read
3316 Jim_WrongNumArgs(interp
, 1, argv
, "varname width addr nelems");
3319 varname
= Jim_GetString(argv
[0], &len
);
3320 /* given "foo" get space for worse case "foo(%d)" .. add 20 */
3322 e
= Jim_GetLong(interp
, argv
[1], &l
);
3327 e
= Jim_GetLong(interp
, argv
[2], &l
);
3331 e
= Jim_GetLong(interp
, argv
[3], &l
);
3346 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3347 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "Invalid width param, must be 8/16/32", NULL
);
3351 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3352 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "mem2array: zero width read?", NULL
);
3355 if ((addr
+ (len
* width
)) < addr
) {
3356 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3357 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "mem2array: addr + len - wraps to zero?", NULL
);
3360 /* absurd transfer size? */
3362 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3363 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "mem2array: absurd > 64K item request", NULL
);
3368 ((width
== 2) && ((addr
& 1) == 0)) ||
3369 ((width
== 4) && ((addr
& 3) == 0))) {
3373 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3374 sprintf(buf
, "mem2array address: 0x%08" PRIx32
" is not aligned for %" PRId32
" byte reads",
3377 Jim_AppendStrings(interp
, Jim_GetResult(interp
), buf
, NULL
);
3386 size_t buffersize
= 4096;
3387 uint8_t *buffer
= malloc(buffersize
);
3394 /* Slurp... in buffer size chunks */
3396 count
= len
; /* in objects.. */
3397 if (count
> (buffersize
/ width
))
3398 count
= (buffersize
/ width
);
3400 retval
= target_read_memory(target
, addr
, width
, count
, buffer
);
3401 if (retval
!= ERROR_OK
) {
3403 LOG_ERROR("mem2array: Read @ 0x%08x, w=%d, cnt=%d, failed",
3407 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3408 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "mem2array: cannot read memory", NULL
);
3412 v
= 0; /* shut up gcc */
3413 for (i
= 0; i
< count
; i
++, n
++) {
3416 v
= target_buffer_get_u32(target
, &buffer
[i
*width
]);
3419 v
= target_buffer_get_u16(target
, &buffer
[i
*width
]);
3422 v
= buffer
[i
] & 0x0ff;
3425 new_int_array_element(interp
, varname
, n
, v
);
3433 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3438 static int get_int_array_element(Jim_Interp
*interp
, const char *varname
, int idx
, uint32_t *val
)
3441 Jim_Obj
*nameObjPtr
, *valObjPtr
;
3445 namebuf
= alloc_printf("%s(%d)", varname
, idx
);
3449 nameObjPtr
= Jim_NewStringObj(interp
, namebuf
, -1);
3455 Jim_IncrRefCount(nameObjPtr
);
3456 valObjPtr
= Jim_GetVariable(interp
, nameObjPtr
, JIM_ERRMSG
);
3457 Jim_DecrRefCount(interp
, nameObjPtr
);
3459 if (valObjPtr
== NULL
)
3462 result
= Jim_GetLong(interp
, valObjPtr
, &l
);
3463 /* printf("%s(%d) => 0%08x\n", varname, idx, val); */
3468 static int jim_array2mem(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
3470 struct command_context
*context
;
3471 struct target
*target
;
3473 context
= current_command_context(interp
);
3474 assert(context
!= NULL
);
3476 target
= get_current_target(context
);
3477 if (target
== NULL
) {
3478 LOG_ERROR("array2mem: no current target");
3482 return target_array2mem(interp
, target
, argc
-1, argv
+ 1);
3485 static int target_array2mem(Jim_Interp
*interp
, struct target
*target
,
3486 int argc
, Jim_Obj
*const *argv
)
3494 const char *varname
;
3498 /* argv[1] = name of array to get the data
3499 * argv[2] = desired width
3500 * argv[3] = memory address
3501 * argv[4] = count to write
3504 Jim_WrongNumArgs(interp
, 0, argv
, "varname width addr nelems");
3507 varname
= Jim_GetString(argv
[0], &len
);
3508 /* given "foo" get space for worse case "foo(%d)" .. add 20 */
3510 e
= Jim_GetLong(interp
, argv
[1], &l
);
3515 e
= Jim_GetLong(interp
, argv
[2], &l
);
3519 e
= Jim_GetLong(interp
, argv
[3], &l
);
3534 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3535 Jim_AppendStrings(interp
, Jim_GetResult(interp
),
3536 "Invalid width param, must be 8/16/32", NULL
);
3540 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3541 Jim_AppendStrings(interp
, Jim_GetResult(interp
),
3542 "array2mem: zero width read?", NULL
);
3545 if ((addr
+ (len
* width
)) < addr
) {
3546 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3547 Jim_AppendStrings(interp
, Jim_GetResult(interp
),
3548 "array2mem: addr + len - wraps to zero?", NULL
);
3551 /* absurd transfer size? */
3553 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3554 Jim_AppendStrings(interp
, Jim_GetResult(interp
),
3555 "array2mem: absurd > 64K item request", NULL
);
3560 ((width
== 2) && ((addr
& 1) == 0)) ||
3561 ((width
== 4) && ((addr
& 3) == 0))) {
3565 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3566 sprintf(buf
, "array2mem address: 0x%08x is not aligned for %d byte reads",
3569 Jim_AppendStrings(interp
, Jim_GetResult(interp
), buf
, NULL
);
3580 size_t buffersize
= 4096;
3581 uint8_t *buffer
= malloc(buffersize
);
3586 /* Slurp... in buffer size chunks */
3588 count
= len
; /* in objects.. */
3589 if (count
> (buffersize
/ width
))
3590 count
= (buffersize
/ width
);
3592 v
= 0; /* shut up gcc */
3593 for (i
= 0; i
< count
; i
++, n
++) {
3594 get_int_array_element(interp
, varname
, n
, &v
);
3597 target_buffer_set_u32(target
, &buffer
[i
* width
], v
);
3600 target_buffer_set_u16(target
, &buffer
[i
* width
], v
);
3603 buffer
[i
] = v
& 0x0ff;
3609 retval
= target_write_memory(target
, addr
, width
, count
, buffer
);
3610 if (retval
!= ERROR_OK
) {
3612 LOG_ERROR("array2mem: Write @ 0x%08x, w=%d, cnt=%d, failed",
3616 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3617 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "array2mem: cannot read memory", NULL
);
3625 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3630 /* FIX? should we propagate errors here rather than printing them
3633 void target_handle_event(struct target
*target
, enum target_event e
)
3635 struct target_event_action
*teap
;
3637 for (teap
= target
->event_action
; teap
!= NULL
; teap
= teap
->next
) {
3638 if (teap
->event
== e
) {
3639 LOG_DEBUG("target: (%d) %s (%s) event: %d (%s) action: %s",
3640 target
->target_number
,
3641 target_name(target
),
3642 target_type_name(target
),
3644 Jim_Nvp_value2name_simple(nvp_target_event
, e
)->name
,
3645 Jim_GetString(teap
->body
, NULL
));
3646 if (Jim_EvalObj(teap
->interp
, teap
->body
) != JIM_OK
) {
3647 Jim_MakeErrorMessage(teap
->interp
);
3648 command_print(NULL
, "%s\n", Jim_GetString(Jim_GetResult(teap
->interp
), NULL
));
3655 * Returns true only if the target has a handler for the specified event.
3657 bool target_has_event_action(struct target
*target
, enum target_event event
)
3659 struct target_event_action
*teap
;
3661 for (teap
= target
->event_action
; teap
!= NULL
; teap
= teap
->next
) {
3662 if (teap
->event
== event
)
3668 enum target_cfg_param
{
3671 TCFG_WORK_AREA_VIRT
,
3672 TCFG_WORK_AREA_PHYS
,
3673 TCFG_WORK_AREA_SIZE
,
3674 TCFG_WORK_AREA_BACKUP
,
3678 TCFG_CHAIN_POSITION
,
3683 static Jim_Nvp nvp_config_opts
[] = {
3684 { .name
= "-type", .value
= TCFG_TYPE
},
3685 { .name
= "-event", .value
= TCFG_EVENT
},
3686 { .name
= "-work-area-virt", .value
= TCFG_WORK_AREA_VIRT
},
3687 { .name
= "-work-area-phys", .value
= TCFG_WORK_AREA_PHYS
},
3688 { .name
= "-work-area-size", .value
= TCFG_WORK_AREA_SIZE
},
3689 { .name
= "-work-area-backup", .value
= TCFG_WORK_AREA_BACKUP
},
3690 { .name
= "-endian" , .value
= TCFG_ENDIAN
},
3691 { .name
= "-variant", .value
= TCFG_VARIANT
},
3692 { .name
= "-coreid", .value
= TCFG_COREID
},
3693 { .name
= "-chain-position", .value
= TCFG_CHAIN_POSITION
},
3694 { .name
= "-dbgbase", .value
= TCFG_DBGBASE
},
3695 { .name
= "-rtos", .value
= TCFG_RTOS
},
3696 { .name
= NULL
, .value
= -1 }
3699 static int target_configure(Jim_GetOptInfo
*goi
, struct target
*target
)
3707 /* parse config or cget options ... */
3708 while (goi
->argc
> 0) {
3709 Jim_SetEmptyResult(goi
->interp
);
3710 /* Jim_GetOpt_Debug(goi); */
3712 if (target
->type
->target_jim_configure
) {
3713 /* target defines a configure function */
3714 /* target gets first dibs on parameters */
3715 e
= (*(target
->type
->target_jim_configure
))(target
, goi
);
3724 /* otherwise we 'continue' below */
3726 e
= Jim_GetOpt_Nvp(goi
, nvp_config_opts
, &n
);
3728 Jim_GetOpt_NvpUnknown(goi
, nvp_config_opts
, 0);
3734 if (goi
->isconfigure
) {
3735 Jim_SetResultFormatted(goi
->interp
,
3736 "not settable: %s", n
->name
);
3740 if (goi
->argc
!= 0) {
3741 Jim_WrongNumArgs(goi
->interp
,
3742 goi
->argc
, goi
->argv
,
3747 Jim_SetResultString(goi
->interp
,
3748 target_type_name(target
), -1);
3752 if (goi
->argc
== 0) {
3753 Jim_WrongNumArgs(goi
->interp
, goi
->argc
, goi
->argv
, "-event ?event-name? ...");
3757 e
= Jim_GetOpt_Nvp(goi
, nvp_target_event
, &n
);
3759 Jim_GetOpt_NvpUnknown(goi
, nvp_target_event
, 1);
3763 if (goi
->isconfigure
) {
3764 if (goi
->argc
!= 1) {
3765 Jim_WrongNumArgs(goi
->interp
, goi
->argc
, goi
->argv
, "-event ?event-name? ?EVENT-BODY?");
3769 if (goi
->argc
!= 0) {
3770 Jim_WrongNumArgs(goi
->interp
, goi
->argc
, goi
->argv
, "-event ?event-name?");
3776 struct target_event_action
*teap
;
3778 teap
= target
->event_action
;
3779 /* replace existing? */
3781 if (teap
->event
== (enum target_event
)n
->value
)
3786 if (goi
->isconfigure
) {
3787 bool replace
= true;
3790 teap
= calloc(1, sizeof(*teap
));
3793 teap
->event
= n
->value
;
3794 teap
->interp
= goi
->interp
;
3795 Jim_GetOpt_Obj(goi
, &o
);
3797 Jim_DecrRefCount(teap
->interp
, teap
->body
);
3798 teap
->body
= Jim_DuplicateObj(goi
->interp
, o
);
3801 * Tcl/TK - "tk events" have a nice feature.
3802 * See the "BIND" command.
3803 * We should support that here.
3804 * You can specify %X and %Y in the event code.
3805 * The idea is: %T - target name.
3806 * The idea is: %N - target number
3807 * The idea is: %E - event name.
3809 Jim_IncrRefCount(teap
->body
);
3812 /* add to head of event list */
3813 teap
->next
= target
->event_action
;
3814 target
->event_action
= teap
;
3816 Jim_SetEmptyResult(goi
->interp
);
3820 Jim_SetEmptyResult(goi
->interp
);
3822 Jim_SetResult(goi
->interp
, Jim_DuplicateObj(goi
->interp
, teap
->body
));
3828 case TCFG_WORK_AREA_VIRT
:
3829 if (goi
->isconfigure
) {
3830 target_free_all_working_areas(target
);
3831 e
= Jim_GetOpt_Wide(goi
, &w
);
3834 target
->working_area_virt
= w
;
3835 target
->working_area_virt_spec
= true;
3840 Jim_SetResult(goi
->interp
, Jim_NewIntObj(goi
->interp
, target
->working_area_virt
));
3844 case TCFG_WORK_AREA_PHYS
:
3845 if (goi
->isconfigure
) {
3846 target_free_all_working_areas(target
);
3847 e
= Jim_GetOpt_Wide(goi
, &w
);
3850 target
->working_area_phys
= w
;
3851 target
->working_area_phys_spec
= true;
3856 Jim_SetResult(goi
->interp
, Jim_NewIntObj(goi
->interp
, target
->working_area_phys
));
3860 case TCFG_WORK_AREA_SIZE
:
3861 if (goi
->isconfigure
) {
3862 target_free_all_working_areas(target
);
3863 e
= Jim_GetOpt_Wide(goi
, &w
);
3866 target
->working_area_size
= w
;
3871 Jim_SetResult(goi
->interp
, Jim_NewIntObj(goi
->interp
, target
->working_area_size
));
3875 case TCFG_WORK_AREA_BACKUP
:
3876 if (goi
->isconfigure
) {
3877 target_free_all_working_areas(target
);
3878 e
= Jim_GetOpt_Wide(goi
, &w
);
3881 /* make this exactly 1 or 0 */
3882 target
->backup_working_area
= (!!w
);
3887 Jim_SetResult(goi
->interp
, Jim_NewIntObj(goi
->interp
, target
->backup_working_area
));
3888 /* loop for more e*/
3893 if (goi
->isconfigure
) {
3894 e
= Jim_GetOpt_Nvp(goi
, nvp_target_endian
, &n
);
3896 Jim_GetOpt_NvpUnknown(goi
, nvp_target_endian
, 1);
3899 target
->endianness
= n
->value
;
3904 n
= Jim_Nvp_value2name_simple(nvp_target_endian
, target
->endianness
);
3905 if (n
->name
== NULL
) {
3906 target
->endianness
= TARGET_LITTLE_ENDIAN
;
3907 n
= Jim_Nvp_value2name_simple(nvp_target_endian
, target
->endianness
);
3909 Jim_SetResultString(goi
->interp
, n
->name
, -1);
3914 if (goi
->isconfigure
) {
3915 if (goi
->argc
< 1) {
3916 Jim_SetResultFormatted(goi
->interp
,
3921 if (target
->variant
)
3922 free((void *)(target
->variant
));
3923 e
= Jim_GetOpt_String(goi
, &cp
, NULL
);
3926 target
->variant
= strdup(cp
);
3931 Jim_SetResultString(goi
->interp
, target
->variant
, -1);
3936 if (goi
->isconfigure
) {
3937 e
= Jim_GetOpt_Wide(goi
, &w
);
3940 target
->coreid
= (int32_t)w
;
3945 Jim_SetResult(goi
->interp
, Jim_NewIntObj(goi
->interp
, target
->working_area_size
));
3949 case TCFG_CHAIN_POSITION
:
3950 if (goi
->isconfigure
) {
3952 struct jtag_tap
*tap
;
3953 target_free_all_working_areas(target
);
3954 e
= Jim_GetOpt_Obj(goi
, &o_t
);
3957 tap
= jtag_tap_by_jim_obj(goi
->interp
, o_t
);
3960 /* make this exactly 1 or 0 */
3966 Jim_SetResultString(goi
->interp
, target
->tap
->dotted_name
, -1);
3967 /* loop for more e*/
3970 if (goi
->isconfigure
) {
3971 e
= Jim_GetOpt_Wide(goi
, &w
);
3974 target
->dbgbase
= (uint32_t)w
;
3975 target
->dbgbase_set
= true;
3980 Jim_SetResult(goi
->interp
, Jim_NewIntObj(goi
->interp
, target
->dbgbase
));
3987 int result
= rtos_create(goi
, target
);
3988 if (result
!= JIM_OK
)
3994 } /* while (goi->argc) */
3997 /* done - we return */
4001 static int jim_target_configure(Jim_Interp
*interp
, int argc
, Jim_Obj
* const *argv
)
4005 Jim_GetOpt_Setup(&goi
, interp
, argc
- 1, argv
+ 1);
4006 goi
.isconfigure
= !strcmp(Jim_GetString(argv
[0], NULL
), "configure");
4007 int need_args
= 1 + goi
.isconfigure
;
4008 if (goi
.argc
< need_args
) {
4009 Jim_WrongNumArgs(goi
.interp
, goi
.argc
, goi
.argv
,
4011 ? "missing: -option VALUE ..."
4012 : "missing: -option ...");
4015 struct target
*target
= Jim_CmdPrivData(goi
.interp
);
4016 return target_configure(&goi
, target
);
4019 static int jim_target_mw(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4021 const char *cmd_name
= Jim_GetString(argv
[0], NULL
);
4024 Jim_GetOpt_Setup(&goi
, interp
, argc
- 1, argv
+ 1);
4026 if (goi
.argc
< 2 || goi
.argc
> 4) {
4027 Jim_SetResultFormatted(goi
.interp
,
4028 "usage: %s [phys] <address> <data> [<count>]", cmd_name
);
4033 fn
= target_write_memory_fast
;
4036 if (strcmp(Jim_GetString(argv
[1], NULL
), "phys") == 0) {
4038 struct Jim_Obj
*obj
;
4039 e
= Jim_GetOpt_Obj(&goi
, &obj
);
4043 fn
= target_write_phys_memory
;
4047 e
= Jim_GetOpt_Wide(&goi
, &a
);
4052 e
= Jim_GetOpt_Wide(&goi
, &b
);
4057 if (goi
.argc
== 1) {
4058 e
= Jim_GetOpt_Wide(&goi
, &c
);
4063 /* all args must be consumed */
4067 struct target
*target
= Jim_CmdPrivData(goi
.interp
);
4069 if (strcasecmp(cmd_name
, "mww") == 0)
4071 else if (strcasecmp(cmd_name
, "mwh") == 0)
4073 else if (strcasecmp(cmd_name
, "mwb") == 0)
4076 LOG_ERROR("command '%s' unknown: ", cmd_name
);
4080 return (target_fill_mem(target
, a
, fn
, data_size
, b
, c
) == ERROR_OK
) ? JIM_OK
: JIM_ERR
;
4083 static int jim_target_md(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4085 const char *cmd_name
= Jim_GetString(argv
[0], NULL
);
4088 Jim_GetOpt_Setup(&goi
, interp
, argc
- 1, argv
+ 1);
4090 if ((goi
.argc
< 1) || (goi
.argc
> 3)) {
4091 Jim_SetResultFormatted(goi
.interp
,
4092 "usage: %s [phys] <address> [<count>]", cmd_name
);
4096 int (*fn
)(struct target
*target
,
4097 uint32_t address
, uint32_t size
, uint32_t count
, uint8_t *buffer
);
4098 fn
= target_read_memory
;
4101 if (strcmp(Jim_GetString(argv
[1], NULL
), "phys") == 0) {
4103 struct Jim_Obj
*obj
;
4104 e
= Jim_GetOpt_Obj(&goi
, &obj
);
4108 fn
= target_read_phys_memory
;
4112 e
= Jim_GetOpt_Wide(&goi
, &a
);
4116 if (goi
.argc
== 1) {
4117 e
= Jim_GetOpt_Wide(&goi
, &c
);
4123 /* all args must be consumed */
4127 jim_wide b
= 1; /* shut up gcc */
4128 if (strcasecmp(cmd_name
, "mdw") == 0)
4130 else if (strcasecmp(cmd_name
, "mdh") == 0)
4132 else if (strcasecmp(cmd_name
, "mdb") == 0)
4135 LOG_ERROR("command '%s' unknown: ", cmd_name
);
4139 /* convert count to "bytes" */
4142 struct target
*target
= Jim_CmdPrivData(goi
.interp
);
4143 uint8_t target_buf
[32];
4149 e
= fn(target
, a
, b
, y
/ b
, target_buf
);
4150 if (e
!= ERROR_OK
) {
4152 snprintf(tmp
, sizeof(tmp
), "%08lx", (long)a
);
4153 Jim_SetResultFormatted(interp
, "error reading target @ 0x%s", tmp
);
4157 command_print(NULL
, "0x%08x ", (int)(a
));
4160 for (x
= 0; x
< 16 && x
< y
; x
+= 4) {
4161 z
= target_buffer_get_u32(target
, &(target_buf
[x
]));
4162 command_print(NULL
, "%08x ", (int)(z
));
4164 for (; (x
< 16) ; x
+= 4)
4165 command_print(NULL
, " ");
4168 for (x
= 0; x
< 16 && x
< y
; x
+= 2) {
4169 z
= target_buffer_get_u16(target
, &(target_buf
[x
]));
4170 command_print(NULL
, "%04x ", (int)(z
));
4172 for (; (x
< 16) ; x
+= 2)
4173 command_print(NULL
, " ");
4177 for (x
= 0 ; (x
< 16) && (x
< y
) ; x
+= 1) {
4178 z
= target_buffer_get_u8(target
, &(target_buf
[x
]));
4179 command_print(NULL
, "%02x ", (int)(z
));
4181 for (; (x
< 16) ; x
+= 1)
4182 command_print(NULL
, " ");
4185 /* ascii-ify the bytes */
4186 for (x
= 0 ; x
< y
; x
++) {
4187 if ((target_buf
[x
] >= 0x20) &&
4188 (target_buf
[x
] <= 0x7e)) {
4192 target_buf
[x
] = '.';
4197 target_buf
[x
] = ' ';
4202 /* print - with a newline */
4203 command_print(NULL
, "%s\n", target_buf
);
4211 static int jim_target_mem2array(Jim_Interp
*interp
,
4212 int argc
, Jim_Obj
*const *argv
)
4214 struct target
*target
= Jim_CmdPrivData(interp
);
4215 return target_mem2array(interp
, target
, argc
- 1, argv
+ 1);
4218 static int jim_target_array2mem(Jim_Interp
*interp
,
4219 int argc
, Jim_Obj
*const *argv
)
4221 struct target
*target
= Jim_CmdPrivData(interp
);
4222 return target_array2mem(interp
, target
, argc
- 1, argv
+ 1);
4225 static int jim_target_tap_disabled(Jim_Interp
*interp
)
4227 Jim_SetResultFormatted(interp
, "[TAP is disabled]");
4231 static int jim_target_examine(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4234 Jim_WrongNumArgs(interp
, 1, argv
, "[no parameters]");
4237 struct target
*target
= Jim_CmdPrivData(interp
);
4238 if (!target
->tap
->enabled
)
4239 return jim_target_tap_disabled(interp
);
4241 int e
= target
->type
->examine(target
);
4247 static int jim_target_halt_gdb(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4250 Jim_WrongNumArgs(interp
, 1, argv
, "[no parameters]");
4253 struct target
*target
= Jim_CmdPrivData(interp
);
4255 if (target_call_event_callbacks(target
, TARGET_EVENT_GDB_HALT
) != ERROR_OK
)
4261 static int jim_target_poll(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4264 Jim_WrongNumArgs(interp
, 1, argv
, "[no parameters]");
4267 struct target
*target
= Jim_CmdPrivData(interp
);
4268 if (!target
->tap
->enabled
)
4269 return jim_target_tap_disabled(interp
);
4272 if (!(target_was_examined(target
)))
4273 e
= ERROR_TARGET_NOT_EXAMINED
;
4275 e
= target
->type
->poll(target
);
4281 static int jim_target_reset(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4284 Jim_GetOpt_Setup(&goi
, interp
, argc
- 1, argv
+ 1);
4286 if (goi
.argc
!= 2) {
4287 Jim_WrongNumArgs(interp
, 0, argv
,
4288 "([tT]|[fF]|assert|deassert) BOOL");
4293 int e
= Jim_GetOpt_Nvp(&goi
, nvp_assert
, &n
);
4295 Jim_GetOpt_NvpUnknown(&goi
, nvp_assert
, 1);
4298 /* the halt or not param */
4300 e
= Jim_GetOpt_Wide(&goi
, &a
);
4304 struct target
*target
= Jim_CmdPrivData(goi
.interp
);
4305 if (!target
->tap
->enabled
)
4306 return jim_target_tap_disabled(interp
);
4307 if (!(target_was_examined(target
))) {
4308 LOG_ERROR("Target not examined yet");
4309 return ERROR_TARGET_NOT_EXAMINED
;
4311 if (!target
->type
->assert_reset
|| !target
->type
->deassert_reset
) {
4312 Jim_SetResultFormatted(interp
,
4313 "No target-specific reset for %s",
4314 target_name(target
));
4317 /* determine if we should halt or not. */
4318 target
->reset_halt
= !!a
;
4319 /* When this happens - all workareas are invalid. */
4320 target_free_all_working_areas_restore(target
, 0);
4323 if (n
->value
== NVP_ASSERT
)
4324 e
= target
->type
->assert_reset(target
);
4326 e
= target
->type
->deassert_reset(target
);
4327 return (e
== ERROR_OK
) ? JIM_OK
: JIM_ERR
;
4330 static int jim_target_halt(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4333 Jim_WrongNumArgs(interp
, 1, argv
, "[no parameters]");
4336 struct target
*target
= Jim_CmdPrivData(interp
);
4337 if (!target
->tap
->enabled
)
4338 return jim_target_tap_disabled(interp
);
4339 int e
= target
->type
->halt(target
);
4340 return (e
== ERROR_OK
) ? JIM_OK
: JIM_ERR
;
4343 static int jim_target_wait_state(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4346 Jim_GetOpt_Setup(&goi
, interp
, argc
- 1, argv
+ 1);
4348 /* params: <name> statename timeoutmsecs */
4349 if (goi
.argc
!= 2) {
4350 const char *cmd_name
= Jim_GetString(argv
[0], NULL
);
4351 Jim_SetResultFormatted(goi
.interp
,
4352 "%s <state_name> <timeout_in_msec>", cmd_name
);
4357 int e
= Jim_GetOpt_Nvp(&goi
, nvp_target_state
, &n
);
4359 Jim_GetOpt_NvpUnknown(&goi
, nvp_target_state
, 1);
4363 e
= Jim_GetOpt_Wide(&goi
, &a
);
4366 struct target
*target
= Jim_CmdPrivData(interp
);
4367 if (!target
->tap
->enabled
)
4368 return jim_target_tap_disabled(interp
);
4370 e
= target_wait_state(target
, n
->value
, a
);
4371 if (e
!= ERROR_OK
) {
4372 Jim_Obj
*eObj
= Jim_NewIntObj(interp
, e
);
4373 Jim_SetResultFormatted(goi
.interp
,
4374 "target: %s wait %s fails (%#s) %s",
4375 target_name(target
), n
->name
,
4376 eObj
, target_strerror_safe(e
));
4377 Jim_FreeNewObj(interp
, eObj
);
4382 /* List for human, Events defined for this target.
4383 * scripts/programs should use 'name cget -event NAME'
4385 static int jim_target_event_list(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4387 struct command_context
*cmd_ctx
= current_command_context(interp
);
4388 assert(cmd_ctx
!= NULL
);
4390 struct target
*target
= Jim_CmdPrivData(interp
);
4391 struct target_event_action
*teap
= target
->event_action
;
4392 command_print(cmd_ctx
, "Event actions for target (%d) %s\n",
4393 target
->target_number
,
4394 target_name(target
));
4395 command_print(cmd_ctx
, "%-25s | Body", "Event");
4396 command_print(cmd_ctx
, "------------------------- | "
4397 "----------------------------------------");
4399 Jim_Nvp
*opt
= Jim_Nvp_value2name_simple(nvp_target_event
, teap
->event
);
4400 command_print(cmd_ctx
, "%-25s | %s",
4401 opt
->name
, Jim_GetString(teap
->body
, NULL
));
4404 command_print(cmd_ctx
, "***END***");
4407 static int jim_target_current_state(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4410 Jim_WrongNumArgs(interp
, 1, argv
, "[no parameters]");
4413 struct target
*target
= Jim_CmdPrivData(interp
);
4414 Jim_SetResultString(interp
, target_state_name(target
), -1);
4417 static int jim_target_invoke_event(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4420 Jim_GetOpt_Setup(&goi
, interp
, argc
- 1, argv
+ 1);
4421 if (goi
.argc
!= 1) {
4422 const char *cmd_name
= Jim_GetString(argv
[0], NULL
);
4423 Jim_SetResultFormatted(goi
.interp
, "%s <eventname>", cmd_name
);
4427 int e
= Jim_GetOpt_Nvp(&goi
, nvp_target_event
, &n
);
4429 Jim_GetOpt_NvpUnknown(&goi
, nvp_target_event
, 1);
4432 struct target
*target
= Jim_CmdPrivData(interp
);
4433 target_handle_event(target
, n
->value
);
4437 static const struct command_registration target_instance_command_handlers
[] = {
4439 .name
= "configure",
4440 .mode
= COMMAND_CONFIG
,
4441 .jim_handler
= jim_target_configure
,
4442 .help
= "configure a new target for use",
4443 .usage
= "[target_attribute ...]",
4447 .mode
= COMMAND_ANY
,
4448 .jim_handler
= jim_target_configure
,
4449 .help
= "returns the specified target attribute",
4450 .usage
= "target_attribute",
4454 .mode
= COMMAND_EXEC
,
4455 .jim_handler
= jim_target_mw
,
4456 .help
= "Write 32-bit word(s) to target memory",
4457 .usage
= "address data [count]",
4461 .mode
= COMMAND_EXEC
,
4462 .jim_handler
= jim_target_mw
,
4463 .help
= "Write 16-bit half-word(s) to target memory",
4464 .usage
= "address data [count]",
4468 .mode
= COMMAND_EXEC
,
4469 .jim_handler
= jim_target_mw
,
4470 .help
= "Write byte(s) to target memory",
4471 .usage
= "address data [count]",
4475 .mode
= COMMAND_EXEC
,
4476 .jim_handler
= jim_target_md
,
4477 .help
= "Display target memory as 32-bit words",
4478 .usage
= "address [count]",
4482 .mode
= COMMAND_EXEC
,
4483 .jim_handler
= jim_target_md
,
4484 .help
= "Display target memory as 16-bit half-words",
4485 .usage
= "address [count]",
4489 .mode
= COMMAND_EXEC
,
4490 .jim_handler
= jim_target_md
,
4491 .help
= "Display target memory as 8-bit bytes",
4492 .usage
= "address [count]",
4495 .name
= "array2mem",
4496 .mode
= COMMAND_EXEC
,
4497 .jim_handler
= jim_target_array2mem
,
4498 .help
= "Writes Tcl array of 8/16/32 bit numbers "
4500 .usage
= "arrayname bitwidth address count",
4503 .name
= "mem2array",
4504 .mode
= COMMAND_EXEC
,
4505 .jim_handler
= jim_target_mem2array
,
4506 .help
= "Loads Tcl array of 8/16/32 bit numbers "
4507 "from target memory",
4508 .usage
= "arrayname bitwidth address count",
4511 .name
= "eventlist",
4512 .mode
= COMMAND_EXEC
,
4513 .jim_handler
= jim_target_event_list
,
4514 .help
= "displays a table of events defined for this target",
4518 .mode
= COMMAND_EXEC
,
4519 .jim_handler
= jim_target_current_state
,
4520 .help
= "displays the current state of this target",
4523 .name
= "arp_examine",
4524 .mode
= COMMAND_EXEC
,
4525 .jim_handler
= jim_target_examine
,
4526 .help
= "used internally for reset processing",
4529 .name
= "arp_halt_gdb",
4530 .mode
= COMMAND_EXEC
,
4531 .jim_handler
= jim_target_halt_gdb
,
4532 .help
= "used internally for reset processing to halt GDB",
4536 .mode
= COMMAND_EXEC
,
4537 .jim_handler
= jim_target_poll
,
4538 .help
= "used internally for reset processing",
4541 .name
= "arp_reset",
4542 .mode
= COMMAND_EXEC
,
4543 .jim_handler
= jim_target_reset
,
4544 .help
= "used internally for reset processing",
4548 .mode
= COMMAND_EXEC
,
4549 .jim_handler
= jim_target_halt
,
4550 .help
= "used internally for reset processing",
4553 .name
= "arp_waitstate",
4554 .mode
= COMMAND_EXEC
,
4555 .jim_handler
= jim_target_wait_state
,
4556 .help
= "used internally for reset processing",
4559 .name
= "invoke-event",
4560 .mode
= COMMAND_EXEC
,
4561 .jim_handler
= jim_target_invoke_event
,
4562 .help
= "invoke handler for specified event",
4563 .usage
= "event_name",
4565 COMMAND_REGISTRATION_DONE
4568 static int target_create(Jim_GetOptInfo
*goi
)
4576 struct target
*target
;
4577 struct command_context
*cmd_ctx
;
4579 cmd_ctx
= current_command_context(goi
->interp
);
4580 assert(cmd_ctx
!= NULL
);
4582 if (goi
->argc
< 3) {
4583 Jim_WrongNumArgs(goi
->interp
, 1, goi
->argv
, "?name? ?type? ..options...");
4588 Jim_GetOpt_Obj(goi
, &new_cmd
);
4589 /* does this command exist? */
4590 cmd
= Jim_GetCommand(goi
->interp
, new_cmd
, JIM_ERRMSG
);
4592 cp
= Jim_GetString(new_cmd
, NULL
);
4593 Jim_SetResultFormatted(goi
->interp
, "Command/target: %s Exists", cp
);
4598 e
= Jim_GetOpt_String(goi
, &cp2
, NULL
);
4602 /* now does target type exist */
4603 for (x
= 0 ; target_types
[x
] ; x
++) {
4604 if (0 == strcmp(cp
, target_types
[x
]->name
)) {
4609 if (target_types
[x
] == NULL
) {
4610 Jim_SetResultFormatted(goi
->interp
, "Unknown target type %s, try one of ", cp
);
4611 for (x
= 0 ; target_types
[x
] ; x
++) {
4612 if (target_types
[x
+ 1]) {
4613 Jim_AppendStrings(goi
->interp
,
4614 Jim_GetResult(goi
->interp
),
4615 target_types
[x
]->name
,
4618 Jim_AppendStrings(goi
->interp
,
4619 Jim_GetResult(goi
->interp
),
4621 target_types
[x
]->name
, NULL
);
4628 target
= calloc(1, sizeof(struct target
));
4629 /* set target number */
4630 target
->target_number
= new_target_number();
4632 /* allocate memory for each unique target type */
4633 target
->type
= (struct target_type
*)calloc(1, sizeof(struct target_type
));
4635 memcpy(target
->type
, target_types
[x
], sizeof(struct target_type
));
4637 /* will be set by "-endian" */
4638 target
->endianness
= TARGET_ENDIAN_UNKNOWN
;
4640 /* default to first core, override with -coreid */
4643 target
->working_area
= 0x0;
4644 target
->working_area_size
= 0x0;
4645 target
->working_areas
= NULL
;
4646 target
->backup_working_area
= 0;
4648 target
->state
= TARGET_UNKNOWN
;
4649 target
->debug_reason
= DBG_REASON_UNDEFINED
;
4650 target
->reg_cache
= NULL
;
4651 target
->breakpoints
= NULL
;
4652 target
->watchpoints
= NULL
;
4653 target
->next
= NULL
;
4654 target
->arch_info
= NULL
;
4656 target
->display
= 1;
4658 target
->halt_issued
= false;
4660 /* initialize trace information */
4661 target
->trace_info
= malloc(sizeof(struct trace
));
4662 target
->trace_info
->num_trace_points
= 0;
4663 target
->trace_info
->trace_points_size
= 0;
4664 target
->trace_info
->trace_points
= NULL
;
4665 target
->trace_info
->trace_history_size
= 0;
4666 target
->trace_info
->trace_history
= NULL
;
4667 target
->trace_info
->trace_history_pos
= 0;
4668 target
->trace_info
->trace_history_overflowed
= 0;
4670 target
->dbgmsg
= NULL
;
4671 target
->dbg_msg_enabled
= 0;
4673 target
->endianness
= TARGET_ENDIAN_UNKNOWN
;
4675 target
->rtos
= NULL
;
4676 target
->rtos_auto_detect
= false;
4678 /* Do the rest as "configure" options */
4679 goi
->isconfigure
= 1;
4680 e
= target_configure(goi
, target
);
4682 if (target
->tap
== NULL
) {
4683 Jim_SetResultString(goi
->interp
, "-chain-position required when creating target", -1);
4693 if (target
->endianness
== TARGET_ENDIAN_UNKNOWN
) {
4694 /* default endian to little if not specified */
4695 target
->endianness
= TARGET_LITTLE_ENDIAN
;
4698 /* incase variant is not set */
4699 if (!target
->variant
)
4700 target
->variant
= strdup("");
4702 cp
= Jim_GetString(new_cmd
, NULL
);
4703 target
->cmd_name
= strdup(cp
);
4705 /* create the target specific commands */
4706 if (target
->type
->commands
) {
4707 e
= register_commands(cmd_ctx
, NULL
, target
->type
->commands
);
4709 LOG_ERROR("unable to register '%s' commands", cp
);
4711 if (target
->type
->target_create
)
4712 (*(target
->type
->target_create
))(target
, goi
->interp
);
4714 /* append to end of list */
4716 struct target
**tpp
;
4717 tpp
= &(all_targets
);
4719 tpp
= &((*tpp
)->next
);
4723 /* now - create the new target name command */
4724 const const struct command_registration target_subcommands
[] = {
4726 .chain
= target_instance_command_handlers
,
4729 .chain
= target
->type
->commands
,
4731 COMMAND_REGISTRATION_DONE
4733 const const struct command_registration target_commands
[] = {
4736 .mode
= COMMAND_ANY
,
4737 .help
= "target command group",
4739 .chain
= target_subcommands
,
4741 COMMAND_REGISTRATION_DONE
4743 e
= register_commands(cmd_ctx
, NULL
, target_commands
);
4747 struct command
*c
= command_find_in_context(cmd_ctx
, cp
);
4749 command_set_handler_data(c
, target
);
4751 return (ERROR_OK
== e
) ? JIM_OK
: JIM_ERR
;
4754 static int jim_target_current(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4757 Jim_WrongNumArgs(interp
, 1, argv
, "Too many parameters");
4760 struct command_context
*cmd_ctx
= current_command_context(interp
);
4761 assert(cmd_ctx
!= NULL
);
4763 Jim_SetResultString(interp
, get_current_target(cmd_ctx
)->cmd_name
, -1);
4767 static int jim_target_types(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4770 Jim_WrongNumArgs(interp
, 1, argv
, "Too many parameters");
4773 Jim_SetResult(interp
, Jim_NewListObj(interp
, NULL
, 0));
4774 for (unsigned x
= 0; NULL
!= target_types
[x
]; x
++) {
4775 Jim_ListAppendElement(interp
, Jim_GetResult(interp
),
4776 Jim_NewStringObj(interp
, target_types
[x
]->name
, -1));
4781 static int jim_target_names(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4784 Jim_WrongNumArgs(interp
, 1, argv
, "Too many parameters");
4787 Jim_SetResult(interp
, Jim_NewListObj(interp
, NULL
, 0));
4788 struct target
*target
= all_targets
;
4790 Jim_ListAppendElement(interp
, Jim_GetResult(interp
),
4791 Jim_NewStringObj(interp
, target_name(target
), -1));
4792 target
= target
->next
;
4797 static int jim_target_smp(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4800 const char *targetname
;
4802 struct target
*target
= (struct target
*) NULL
;
4803 struct target_list
*head
, *curr
, *new;
4804 curr
= (struct target_list
*) NULL
;
4805 head
= (struct target_list
*) NULL
;
4806 new = (struct target_list
*) NULL
;
4809 LOG_DEBUG("%d", argc
);
4810 /* argv[1] = target to associate in smp
4811 * argv[2] = target to assoicate in smp
4815 for (i
= 1; i
< argc
; i
++) {
4817 targetname
= Jim_GetString(argv
[i
], &len
);
4818 target
= get_target(targetname
);
4819 LOG_DEBUG("%s ", targetname
);
4821 new = malloc(sizeof(struct target_list
));
4822 new->target
= target
;
4823 new->next
= (struct target_list
*)NULL
;
4824 if (head
== (struct target_list
*)NULL
) {
4833 /* now parse the list of cpu and put the target in smp mode*/
4836 while (curr
!= (struct target_list
*)NULL
) {
4837 target
= curr
->target
;
4839 target
->head
= head
;
4843 retval
= rtos_smp_init(head
->target
);
4848 static int jim_target_create(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4851 Jim_GetOpt_Setup(&goi
, interp
, argc
- 1, argv
+ 1);
4853 Jim_WrongNumArgs(goi
.interp
, goi
.argc
, goi
.argv
,
4854 "<name> <target_type> [<target_options> ...]");
4857 return target_create(&goi
);
4860 static int jim_target_number(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4863 Jim_GetOpt_Setup(&goi
, interp
, argc
- 1, argv
+ 1);
4865 /* It's OK to remove this mechanism sometime after August 2010 or so */
4866 LOG_WARNING("don't use numbers as target identifiers; use names");
4867 if (goi
.argc
!= 1) {
4868 Jim_SetResultFormatted(goi
.interp
, "usage: target number <number>");
4872 int e
= Jim_GetOpt_Wide(&goi
, &w
);
4876 struct target
*target
;
4877 for (target
= all_targets
; NULL
!= target
; target
= target
->next
) {
4878 if (target
->target_number
!= w
)
4881 Jim_SetResultString(goi
.interp
, target_name(target
), -1);
4885 Jim_Obj
*wObj
= Jim_NewIntObj(goi
.interp
, w
);
4886 Jim_SetResultFormatted(goi
.interp
,
4887 "Target: number %#s does not exist", wObj
);
4888 Jim_FreeNewObj(interp
, wObj
);
4893 static int jim_target_count(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4896 Jim_WrongNumArgs(interp
, 1, argv
, "<no parameters>");
4900 struct target
*target
= all_targets
;
4901 while (NULL
!= target
) {
4902 target
= target
->next
;
4905 Jim_SetResult(interp
, Jim_NewIntObj(interp
, count
));
4909 static const struct command_registration target_subcommand_handlers
[] = {
4912 .mode
= COMMAND_CONFIG
,
4913 .handler
= handle_target_init_command
,
4914 .help
= "initialize targets",
4918 /* REVISIT this should be COMMAND_CONFIG ... */
4919 .mode
= COMMAND_ANY
,
4920 .jim_handler
= jim_target_create
,
4921 .usage
= "name type '-chain-position' name [options ...]",
4922 .help
= "Creates and selects a new target",
4926 .mode
= COMMAND_ANY
,
4927 .jim_handler
= jim_target_current
,
4928 .help
= "Returns the currently selected target",
4932 .mode
= COMMAND_ANY
,
4933 .jim_handler
= jim_target_types
,
4934 .help
= "Returns the available target types as "
4935 "a list of strings",
4939 .mode
= COMMAND_ANY
,
4940 .jim_handler
= jim_target_names
,
4941 .help
= "Returns the names of all targets as a list of strings",
4945 .mode
= COMMAND_ANY
,
4946 .jim_handler
= jim_target_number
,
4948 .help
= "Returns the name of the numbered target "
4953 .mode
= COMMAND_ANY
,
4954 .jim_handler
= jim_target_count
,
4955 .help
= "Returns the number of targets as an integer "
4960 .mode
= COMMAND_ANY
,
4961 .jim_handler
= jim_target_smp
,
4962 .usage
= "targetname1 targetname2 ...",
4963 .help
= "gather several target in a smp list"
4966 COMMAND_REGISTRATION_DONE
4976 static int fastload_num
;
4977 static struct FastLoad
*fastload
;
4979 static void free_fastload(void)
4981 if (fastload
!= NULL
) {
4983 for (i
= 0; i
< fastload_num
; i
++) {
4984 if (fastload
[i
].data
)
4985 free(fastload
[i
].data
);
4992 COMMAND_HANDLER(handle_fast_load_image_command
)
4996 uint32_t image_size
;
4997 uint32_t min_address
= 0;
4998 uint32_t max_address
= 0xffffffff;
5003 int retval
= CALL_COMMAND_HANDLER(parse_load_image_command_CMD_ARGV
,
5004 &image
, &min_address
, &max_address
);
5005 if (ERROR_OK
!= retval
)
5008 struct duration bench
;
5009 duration_start(&bench
);
5011 retval
= image_open(&image
, CMD_ARGV
[0], (CMD_ARGC
>= 3) ? CMD_ARGV
[2] : NULL
);
5012 if (retval
!= ERROR_OK
)
5017 fastload_num
= image
.num_sections
;
5018 fastload
= (struct FastLoad
*)malloc(sizeof(struct FastLoad
)*image
.num_sections
);
5019 if (fastload
== NULL
) {
5020 command_print(CMD_CTX
, "out of memory");
5021 image_close(&image
);
5024 memset(fastload
, 0, sizeof(struct FastLoad
)*image
.num_sections
);
5025 for (i
= 0; i
< image
.num_sections
; i
++) {
5026 buffer
= malloc(image
.sections
[i
].size
);
5027 if (buffer
== NULL
) {
5028 command_print(CMD_CTX
, "error allocating buffer for section (%d bytes)",
5029 (int)(image
.sections
[i
].size
));
5030 retval
= ERROR_FAIL
;
5034 retval
= image_read_section(&image
, i
, 0x0, image
.sections
[i
].size
, buffer
, &buf_cnt
);
5035 if (retval
!= ERROR_OK
) {
5040 uint32_t offset
= 0;
5041 uint32_t length
= buf_cnt
;
5043 /* DANGER!!! beware of unsigned comparision here!!! */
5045 if ((image
.sections
[i
].base_address
+ buf_cnt
>= min_address
) &&
5046 (image
.sections
[i
].base_address
< max_address
)) {
5047 if (image
.sections
[i
].base_address
< min_address
) {
5048 /* clip addresses below */
5049 offset
+= min_address
-image
.sections
[i
].base_address
;
5053 if (image
.sections
[i
].base_address
+ buf_cnt
> max_address
)
5054 length
-= (image
.sections
[i
].base_address
+ buf_cnt
)-max_address
;
5056 fastload
[i
].address
= image
.sections
[i
].base_address
+ offset
;
5057 fastload
[i
].data
= malloc(length
);
5058 if (fastload
[i
].data
== NULL
) {
5060 command_print(CMD_CTX
, "error allocating buffer for section (%d bytes)",
5062 retval
= ERROR_FAIL
;
5065 memcpy(fastload
[i
].data
, buffer
+ offset
, length
);
5066 fastload
[i
].length
= length
;
5068 image_size
+= length
;
5069 command_print(CMD_CTX
, "%u bytes written at address 0x%8.8x",
5070 (unsigned int)length
,
5071 ((unsigned int)(image
.sections
[i
].base_address
+ offset
)));
5077 if ((ERROR_OK
== retval
) && (duration_measure(&bench
) == ERROR_OK
)) {
5078 command_print(CMD_CTX
, "Loaded %" PRIu32
" bytes "
5079 "in %fs (%0.3f KiB/s)", image_size
,
5080 duration_elapsed(&bench
), duration_kbps(&bench
, image_size
));
5082 command_print(CMD_CTX
,
5083 "WARNING: image has not been loaded to target!"
5084 "You can issue a 'fast_load' to finish loading.");
5087 image_close(&image
);
5089 if (retval
!= ERROR_OK
)
5095 COMMAND_HANDLER(handle_fast_load_command
)
5098 return ERROR_COMMAND_SYNTAX_ERROR
;
5099 if (fastload
== NULL
) {
5100 LOG_ERROR("No image in memory");
5104 int ms
= timeval_ms();
5106 int retval
= ERROR_OK
;
5107 for (i
= 0; i
< fastload_num
; i
++) {
5108 struct target
*target
= get_current_target(CMD_CTX
);
5109 command_print(CMD_CTX
, "Write to 0x%08x, length 0x%08x",
5110 (unsigned int)(fastload
[i
].address
),
5111 (unsigned int)(fastload
[i
].length
));
5112 retval
= target_write_buffer(target
, fastload
[i
].address
, fastload
[i
].length
, fastload
[i
].data
);
5113 if (retval
!= ERROR_OK
)
5115 size
+= fastload
[i
].length
;
5117 if (retval
== ERROR_OK
) {
5118 int after
= timeval_ms();
5119 command_print(CMD_CTX
, "Loaded image %f kBytes/s", (float)(size
/1024.0)/((float)(after
-ms
)/1000.0));
5124 static const struct command_registration target_command_handlers
[] = {
5127 .handler
= handle_targets_command
,
5128 .mode
= COMMAND_ANY
,
5129 .help
= "change current default target (one parameter) "
5130 "or prints table of all targets (no parameters)",
5131 .usage
= "[target]",
5135 .mode
= COMMAND_CONFIG
,
5136 .help
= "configure target",
5138 .chain
= target_subcommand_handlers
,
5140 COMMAND_REGISTRATION_DONE
5143 int target_register_commands(struct command_context
*cmd_ctx
)
5145 return register_commands(cmd_ctx
, NULL
, target_command_handlers
);
5148 static bool target_reset_nag
= true;
5150 bool get_target_reset_nag(void)
5152 return target_reset_nag
;
5155 COMMAND_HANDLER(handle_target_reset_nag
)
5157 return CALL_COMMAND_HANDLER(handle_command_parse_bool
,
5158 &target_reset_nag
, "Nag after each reset about options to improve "
5162 COMMAND_HANDLER(handle_ps_command
)
5164 struct target
*target
= get_current_target(CMD_CTX
);
5166 if (target
->state
!= TARGET_HALTED
) {
5167 LOG_INFO("target not halted !!");
5171 if ((target
->rtos
) && (target
->rtos
->type
)
5172 && (target
->rtos
->type
->ps_command
)) {
5173 display
= target
->rtos
->type
->ps_command(target
);
5174 command_print(CMD_CTX
, "%s", display
);
5179 return ERROR_TARGET_FAILURE
;
5183 static const struct command_registration target_exec_command_handlers
[] = {
5185 .name
= "fast_load_image",
5186 .handler
= handle_fast_load_image_command
,
5187 .mode
= COMMAND_ANY
,
5188 .help
= "Load image into server memory for later use by "
5189 "fast_load; primarily for profiling",
5190 .usage
= "filename address ['bin'|'ihex'|'elf'|'s19'] "
5191 "[min_address [max_length]]",
5194 .name
= "fast_load",
5195 .handler
= handle_fast_load_command
,
5196 .mode
= COMMAND_EXEC
,
5197 .help
= "loads active fast load image to current target "
5198 "- mainly for profiling purposes",
5203 .handler
= handle_profile_command
,
5204 .mode
= COMMAND_EXEC
,
5205 .usage
= "seconds filename",
5206 .help
= "profiling samples the CPU PC",
5208 /** @todo don't register virt2phys() unless target supports it */
5210 .name
= "virt2phys",
5211 .handler
= handle_virt2phys_command
,
5212 .mode
= COMMAND_ANY
,
5213 .help
= "translate a virtual address into a physical address",
5214 .usage
= "virtual_address",
5218 .handler
= handle_reg_command
,
5219 .mode
= COMMAND_EXEC
,
5220 .help
= "display or set a register; with no arguments, "
5221 "displays all registers and their values",
5222 .usage
= "[(register_name|register_number) [value]]",
5226 .handler
= handle_poll_command
,
5227 .mode
= COMMAND_EXEC
,
5228 .help
= "poll target state; or reconfigure background polling",
5229 .usage
= "['on'|'off']",
5232 .name
= "wait_halt",
5233 .handler
= handle_wait_halt_command
,
5234 .mode
= COMMAND_EXEC
,
5235 .help
= "wait up to the specified number of milliseconds "
5236 "(default 5) for a previously requested halt",
5237 .usage
= "[milliseconds]",
5241 .handler
= handle_halt_command
,
5242 .mode
= COMMAND_EXEC
,
5243 .help
= "request target to halt, then wait up to the specified"
5244 "number of milliseconds (default 5) for it to complete",
5245 .usage
= "[milliseconds]",
5249 .handler
= handle_resume_command
,
5250 .mode
= COMMAND_EXEC
,
5251 .help
= "resume target execution from current PC or address",
5252 .usage
= "[address]",
5256 .handler
= handle_reset_command
,
5257 .mode
= COMMAND_EXEC
,
5258 .usage
= "[run|halt|init]",
5259 .help
= "Reset all targets into the specified mode."
5260 "Default reset mode is run, if not given.",
5263 .name
= "soft_reset_halt",
5264 .handler
= handle_soft_reset_halt_command
,
5265 .mode
= COMMAND_EXEC
,
5267 .help
= "halt the target and do a soft reset",
5271 .handler
= handle_step_command
,
5272 .mode
= COMMAND_EXEC
,
5273 .help
= "step one instruction from current PC or address",
5274 .usage
= "[address]",
5278 .handler
= handle_md_command
,
5279 .mode
= COMMAND_EXEC
,
5280 .help
= "display memory words",
5281 .usage
= "['phys'] address [count]",
5285 .handler
= handle_md_command
,
5286 .mode
= COMMAND_EXEC
,
5287 .help
= "display memory half-words",
5288 .usage
= "['phys'] address [count]",
5292 .handler
= handle_md_command
,
5293 .mode
= COMMAND_EXEC
,
5294 .help
= "display memory bytes",
5295 .usage
= "['phys'] address [count]",
5299 .handler
= handle_mw_command
,
5300 .mode
= COMMAND_EXEC
,
5301 .help
= "write memory word",
5302 .usage
= "['phys'] address value [count]",
5306 .handler
= handle_mw_command
,
5307 .mode
= COMMAND_EXEC
,
5308 .help
= "write memory half-word",
5309 .usage
= "['phys'] address value [count]",
5313 .handler
= handle_mw_command
,
5314 .mode
= COMMAND_EXEC
,
5315 .help
= "write memory byte",
5316 .usage
= "['phys'] address value [count]",
5320 .handler
= handle_bp_command
,
5321 .mode
= COMMAND_EXEC
,
5322 .help
= "list or set hardware or software breakpoint",
5323 .usage
= "<address> [<asid>]<length> ['hw'|'hw_ctx']",
5327 .handler
= handle_rbp_command
,
5328 .mode
= COMMAND_EXEC
,
5329 .help
= "remove breakpoint",
5334 .handler
= handle_wp_command
,
5335 .mode
= COMMAND_EXEC
,
5336 .help
= "list (no params) or create watchpoints",
5337 .usage
= "[address length [('r'|'w'|'a') value [mask]]]",
5341 .handler
= handle_rwp_command
,
5342 .mode
= COMMAND_EXEC
,
5343 .help
= "remove watchpoint",
5347 .name
= "load_image",
5348 .handler
= handle_load_image_command
,
5349 .mode
= COMMAND_EXEC
,
5350 .usage
= "filename address ['bin'|'ihex'|'elf'|'s19'] "
5351 "[min_address] [max_length]",
5354 .name
= "dump_image",
5355 .handler
= handle_dump_image_command
,
5356 .mode
= COMMAND_EXEC
,
5357 .usage
= "filename address size",
5360 .name
= "verify_image",
5361 .handler
= handle_verify_image_command
,
5362 .mode
= COMMAND_EXEC
,
5363 .usage
= "filename [offset [type]]",
5366 .name
= "test_image",
5367 .handler
= handle_test_image_command
,
5368 .mode
= COMMAND_EXEC
,
5369 .usage
= "filename [offset [type]]",
5372 .name
= "mem2array",
5373 .mode
= COMMAND_EXEC
,
5374 .jim_handler
= jim_mem2array
,
5375 .help
= "read 8/16/32 bit memory and return as a TCL array "
5376 "for script processing",
5377 .usage
= "arrayname bitwidth address count",
5380 .name
= "array2mem",
5381 .mode
= COMMAND_EXEC
,
5382 .jim_handler
= jim_array2mem
,
5383 .help
= "convert a TCL array to memory locations "
5384 "and write the 8/16/32 bit values",
5385 .usage
= "arrayname bitwidth address count",
5388 .name
= "reset_nag",
5389 .handler
= handle_target_reset_nag
,
5390 .mode
= COMMAND_ANY
,
5391 .help
= "Nag after each reset about options that could have been "
5392 "enabled to improve performance. ",
5393 .usage
= "['enable'|'disable']",
5397 .handler
= handle_ps_command
,
5398 .mode
= COMMAND_EXEC
,
5399 .help
= "list all tasks ",
5403 COMMAND_REGISTRATION_DONE
5405 static int target_register_user_commands(struct command_context
*cmd_ctx
)
5407 int retval
= ERROR_OK
;
5408 retval
= target_request_register_commands(cmd_ctx
);
5409 if (retval
!= ERROR_OK
)
5412 retval
= trace_register_commands(cmd_ctx
);
5413 if (retval
!= ERROR_OK
)
5417 return register_commands(cmd_ctx
, NULL
, target_exec_command_handlers
);