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 retval
= command_run_line(CMD_CTX
, "init_board");
1037 if (ERROR_OK
!= retval
)
1040 LOG_DEBUG("Initializing targets...");
1041 return target_init(CMD_CTX
);
1044 int target_register_event_callback(int (*callback
)(struct target
*target
,
1045 enum target_event event
, void *priv
), void *priv
)
1047 struct target_event_callback
**callbacks_p
= &target_event_callbacks
;
1049 if (callback
== NULL
)
1050 return ERROR_COMMAND_SYNTAX_ERROR
;
1053 while ((*callbacks_p
)->next
)
1054 callbacks_p
= &((*callbacks_p
)->next
);
1055 callbacks_p
= &((*callbacks_p
)->next
);
1058 (*callbacks_p
) = malloc(sizeof(struct target_event_callback
));
1059 (*callbacks_p
)->callback
= callback
;
1060 (*callbacks_p
)->priv
= priv
;
1061 (*callbacks_p
)->next
= NULL
;
1066 int target_register_timer_callback(int (*callback
)(void *priv
), int time_ms
, int periodic
, void *priv
)
1068 struct target_timer_callback
**callbacks_p
= &target_timer_callbacks
;
1071 if (callback
== NULL
)
1072 return ERROR_COMMAND_SYNTAX_ERROR
;
1075 while ((*callbacks_p
)->next
)
1076 callbacks_p
= &((*callbacks_p
)->next
);
1077 callbacks_p
= &((*callbacks_p
)->next
);
1080 (*callbacks_p
) = malloc(sizeof(struct target_timer_callback
));
1081 (*callbacks_p
)->callback
= callback
;
1082 (*callbacks_p
)->periodic
= periodic
;
1083 (*callbacks_p
)->time_ms
= time_ms
;
1085 gettimeofday(&now
, NULL
);
1086 (*callbacks_p
)->when
.tv_usec
= now
.tv_usec
+ (time_ms
% 1000) * 1000;
1087 time_ms
-= (time_ms
% 1000);
1088 (*callbacks_p
)->when
.tv_sec
= now
.tv_sec
+ (time_ms
/ 1000);
1089 if ((*callbacks_p
)->when
.tv_usec
> 1000000) {
1090 (*callbacks_p
)->when
.tv_usec
= (*callbacks_p
)->when
.tv_usec
- 1000000;
1091 (*callbacks_p
)->when
.tv_sec
+= 1;
1094 (*callbacks_p
)->priv
= priv
;
1095 (*callbacks_p
)->next
= NULL
;
1100 int target_unregister_event_callback(int (*callback
)(struct target
*target
,
1101 enum target_event event
, void *priv
), void *priv
)
1103 struct target_event_callback
**p
= &target_event_callbacks
;
1104 struct target_event_callback
*c
= target_event_callbacks
;
1106 if (callback
== NULL
)
1107 return ERROR_COMMAND_SYNTAX_ERROR
;
1110 struct target_event_callback
*next
= c
->next
;
1111 if ((c
->callback
== callback
) && (c
->priv
== priv
)) {
1123 static int target_unregister_timer_callback(int (*callback
)(void *priv
), void *priv
)
1125 struct target_timer_callback
**p
= &target_timer_callbacks
;
1126 struct target_timer_callback
*c
= target_timer_callbacks
;
1128 if (callback
== NULL
)
1129 return ERROR_COMMAND_SYNTAX_ERROR
;
1132 struct target_timer_callback
*next
= c
->next
;
1133 if ((c
->callback
== callback
) && (c
->priv
== priv
)) {
1145 int target_call_event_callbacks(struct target
*target
, enum target_event event
)
1147 struct target_event_callback
*callback
= target_event_callbacks
;
1148 struct target_event_callback
*next_callback
;
1150 if (event
== TARGET_EVENT_HALTED
) {
1151 /* execute early halted first */
1152 target_call_event_callbacks(target
, TARGET_EVENT_GDB_HALT
);
1155 LOG_DEBUG("target event %i (%s)", event
,
1156 Jim_Nvp_value2name_simple(nvp_target_event
, event
)->name
);
1158 target_handle_event(target
, event
);
1161 next_callback
= callback
->next
;
1162 callback
->callback(target
, event
, callback
->priv
);
1163 callback
= next_callback
;
1169 static int target_timer_callback_periodic_restart(
1170 struct target_timer_callback
*cb
, struct timeval
*now
)
1172 int time_ms
= cb
->time_ms
;
1173 cb
->when
.tv_usec
= now
->tv_usec
+ (time_ms
% 1000) * 1000;
1174 time_ms
-= (time_ms
% 1000);
1175 cb
->when
.tv_sec
= now
->tv_sec
+ time_ms
/ 1000;
1176 if (cb
->when
.tv_usec
> 1000000) {
1177 cb
->when
.tv_usec
= cb
->when
.tv_usec
- 1000000;
1178 cb
->when
.tv_sec
+= 1;
1183 static int target_call_timer_callback(struct target_timer_callback
*cb
,
1184 struct timeval
*now
)
1186 cb
->callback(cb
->priv
);
1189 return target_timer_callback_periodic_restart(cb
, now
);
1191 return target_unregister_timer_callback(cb
->callback
, cb
->priv
);
1194 static int target_call_timer_callbacks_check_time(int checktime
)
1199 gettimeofday(&now
, NULL
);
1201 struct target_timer_callback
*callback
= target_timer_callbacks
;
1203 /* cleaning up may unregister and free this callback */
1204 struct target_timer_callback
*next_callback
= callback
->next
;
1206 bool call_it
= callback
->callback
&&
1207 ((!checktime
&& callback
->periodic
) ||
1208 now
.tv_sec
> callback
->when
.tv_sec
||
1209 (now
.tv_sec
== callback
->when
.tv_sec
&&
1210 now
.tv_usec
>= callback
->when
.tv_usec
));
1213 int retval
= target_call_timer_callback(callback
, &now
);
1214 if (retval
!= ERROR_OK
)
1218 callback
= next_callback
;
1224 int target_call_timer_callbacks(void)
1226 return target_call_timer_callbacks_check_time(1);
1229 /* invoke periodic callbacks immediately */
1230 int target_call_timer_callbacks_now(void)
1232 return target_call_timer_callbacks_check_time(0);
1235 int target_alloc_working_area_try(struct target
*target
, uint32_t size
, struct working_area
**area
)
1237 struct working_area
*c
= target
->working_areas
;
1238 struct working_area
*new_wa
= NULL
;
1240 /* Reevaluate working area address based on MMU state*/
1241 if (target
->working_areas
== NULL
) {
1245 retval
= target
->type
->mmu(target
, &enabled
);
1246 if (retval
!= ERROR_OK
)
1250 if (target
->working_area_phys_spec
) {
1251 LOG_DEBUG("MMU disabled, using physical "
1252 "address for working memory 0x%08x",
1253 (unsigned)target
->working_area_phys
);
1254 target
->working_area
= target
->working_area_phys
;
1256 LOG_ERROR("No working memory available. "
1257 "Specify -work-area-phys to target.");
1258 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
1261 if (target
->working_area_virt_spec
) {
1262 LOG_DEBUG("MMU enabled, using virtual "
1263 "address for working memory 0x%08x",
1264 (unsigned)target
->working_area_virt
);
1265 target
->working_area
= target
->working_area_virt
;
1267 LOG_ERROR("No working memory available. "
1268 "Specify -work-area-virt to target.");
1269 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
1274 /* only allocate multiples of 4 byte */
1276 LOG_ERROR("BUG: code tried to allocate unaligned number of bytes (0x%08x), padding", ((unsigned)(size
)));
1277 size
= (size
+ 3) & (~3);
1280 /* see if there's already a matching working area */
1282 if ((c
->free
) && (c
->size
== size
)) {
1289 /* if not, allocate a new one */
1291 struct working_area
**p
= &target
->working_areas
;
1292 uint32_t first_free
= target
->working_area
;
1293 uint32_t free_size
= target
->working_area_size
;
1295 c
= target
->working_areas
;
1297 first_free
+= c
->size
;
1298 free_size
-= c
->size
;
1303 if (free_size
< size
)
1304 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
1306 LOG_DEBUG("allocated new working area at address 0x%08x", (unsigned)first_free
);
1308 new_wa
= malloc(sizeof(struct working_area
));
1309 new_wa
->next
= NULL
;
1310 new_wa
->size
= size
;
1311 new_wa
->address
= first_free
;
1313 if (target
->backup_working_area
) {
1315 new_wa
->backup
= malloc(new_wa
->size
);
1316 retval
= target_read_memory(target
, new_wa
->address
, 4,
1317 new_wa
->size
/ 4, new_wa
->backup
);
1318 if (retval
!= ERROR_OK
) {
1319 free(new_wa
->backup
);
1324 new_wa
->backup
= NULL
;
1326 /* put new entry in list */
1330 /* mark as used, and return the new (reused) area */
1331 new_wa
->free
= false;
1335 new_wa
->user
= area
;
1340 int target_alloc_working_area(struct target
*target
, uint32_t size
, struct working_area
**area
)
1344 retval
= target_alloc_working_area_try(target
, size
, area
);
1345 if (retval
== ERROR_TARGET_RESOURCE_NOT_AVAILABLE
)
1346 LOG_WARNING("not enough working area available(requested %u)", (unsigned)(size
));
1351 static int target_free_working_area_restore(struct target
*target
, struct working_area
*area
, int restore
)
1356 if (restore
&& target
->backup_working_area
) {
1357 int retval
= target_write_memory(target
,
1358 area
->address
, 4, area
->size
/ 4, area
->backup
);
1359 if (retval
!= ERROR_OK
)
1365 /* mark user pointer invalid */
1372 int target_free_working_area(struct target
*target
, struct working_area
*area
)
1374 return target_free_working_area_restore(target
, area
, 1);
1377 /* free resources and restore memory, if restoring memory fails,
1378 * free up resources anyway
1380 static void target_free_all_working_areas_restore(struct target
*target
, int restore
)
1382 struct working_area
*c
= target
->working_areas
;
1385 struct working_area
*next
= c
->next
;
1386 target_free_working_area_restore(target
, c
, restore
);
1396 target
->working_areas
= NULL
;
1399 void target_free_all_working_areas(struct target
*target
)
1401 target_free_all_working_areas_restore(target
, 1);
1404 int target_arch_state(struct target
*target
)
1407 if (target
== NULL
) {
1408 LOG_USER("No target has been configured");
1412 LOG_USER("target state: %s", target_state_name(target
));
1414 if (target
->state
!= TARGET_HALTED
)
1417 retval
= target
->type
->arch_state(target
);
1421 /* Single aligned words are guaranteed to use 16 or 32 bit access
1422 * mode respectively, otherwise data is handled as quickly as
1425 int target_write_buffer(struct target
*target
, uint32_t address
, uint32_t size
, const uint8_t *buffer
)
1427 LOG_DEBUG("writing buffer of %i byte at 0x%8.8x",
1428 (int)size
, (unsigned)address
);
1430 if (!target_was_examined(target
)) {
1431 LOG_ERROR("Target not examined yet");
1438 if ((address
+ size
- 1) < address
) {
1439 /* GDB can request this when e.g. PC is 0xfffffffc*/
1440 LOG_ERROR("address + size wrapped(0x%08x, 0x%08x)",
1446 return target
->type
->write_buffer(target
, address
, size
, buffer
);
1449 static int target_write_buffer_default(struct target
*target
, uint32_t address
, uint32_t size
, const uint8_t *buffer
)
1451 int retval
= ERROR_OK
;
1453 if (((address
% 2) == 0) && (size
== 2))
1454 return target_write_memory(target
, address
, 2, 1, buffer
);
1456 /* handle unaligned head bytes */
1458 uint32_t unaligned
= 4 - (address
% 4);
1460 if (unaligned
> size
)
1463 retval
= target_write_memory(target
, address
, 1, unaligned
, buffer
);
1464 if (retval
!= ERROR_OK
)
1467 buffer
+= unaligned
;
1468 address
+= unaligned
;
1472 /* handle aligned words */
1474 int aligned
= size
- (size
% 4);
1476 /* use bulk writes above a certain limit. This may have to be changed */
1477 if (aligned
> 128) {
1478 retval
= target
->type
->bulk_write_memory(target
, address
, aligned
/ 4, buffer
);
1479 if (retval
!= ERROR_OK
)
1482 retval
= target_write_memory(target
, address
, 4, aligned
/ 4, buffer
);
1483 if (retval
!= ERROR_OK
)
1492 /* handle tail writes of less than 4 bytes */
1494 retval
= target_write_memory(target
, address
, 1, size
, buffer
);
1495 if (retval
!= ERROR_OK
)
1502 /* Single aligned words are guaranteed to use 16 or 32 bit access
1503 * mode respectively, otherwise data is handled as quickly as
1506 int target_read_buffer(struct target
*target
, uint32_t address
, uint32_t size
, uint8_t *buffer
)
1508 LOG_DEBUG("reading buffer of %i byte at 0x%8.8x",
1509 (int)size
, (unsigned)address
);
1511 if (!target_was_examined(target
)) {
1512 LOG_ERROR("Target not examined yet");
1519 if ((address
+ size
- 1) < address
) {
1520 /* GDB can request this when e.g. PC is 0xfffffffc*/
1521 LOG_ERROR("address + size wrapped(0x%08" PRIx32
", 0x%08" PRIx32
")",
1527 return target
->type
->read_buffer(target
, address
, size
, buffer
);
1530 static int target_read_buffer_default(struct target
*target
, uint32_t address
, uint32_t size
, uint8_t *buffer
)
1532 int retval
= ERROR_OK
;
1534 if (((address
% 2) == 0) && (size
== 2))
1535 return target_read_memory(target
, address
, 2, 1, buffer
);
1537 /* handle unaligned head bytes */
1539 uint32_t unaligned
= 4 - (address
% 4);
1541 if (unaligned
> size
)
1544 retval
= target_read_memory(target
, address
, 1, unaligned
, buffer
);
1545 if (retval
!= ERROR_OK
)
1548 buffer
+= unaligned
;
1549 address
+= unaligned
;
1553 /* handle aligned words */
1555 int aligned
= size
- (size
% 4);
1557 retval
= target_read_memory(target
, address
, 4, aligned
/ 4, buffer
);
1558 if (retval
!= ERROR_OK
)
1566 /*prevent byte access when possible (avoid AHB access limitations in some cases)*/
1568 int aligned
= size
- (size
% 2);
1569 retval
= target_read_memory(target
, address
, 2, aligned
/ 2, buffer
);
1570 if (retval
!= ERROR_OK
)
1577 /* handle tail writes of less than 4 bytes */
1579 retval
= target_read_memory(target
, address
, 1, size
, buffer
);
1580 if (retval
!= ERROR_OK
)
1587 int target_checksum_memory(struct target
*target
, uint32_t address
, uint32_t size
, uint32_t* crc
)
1592 uint32_t checksum
= 0;
1593 if (!target_was_examined(target
)) {
1594 LOG_ERROR("Target not examined yet");
1598 retval
= target
->type
->checksum_memory(target
, address
, size
, &checksum
);
1599 if (retval
!= ERROR_OK
) {
1600 buffer
= malloc(size
);
1601 if (buffer
== NULL
) {
1602 LOG_ERROR("error allocating buffer for section (%d bytes)", (int)size
);
1603 return ERROR_COMMAND_SYNTAX_ERROR
;
1605 retval
= target_read_buffer(target
, address
, size
, buffer
);
1606 if (retval
!= ERROR_OK
) {
1611 /* convert to target endianness */
1612 for (i
= 0; i
< (size
/sizeof(uint32_t)); i
++) {
1613 uint32_t target_data
;
1614 target_data
= target_buffer_get_u32(target
, &buffer
[i
*sizeof(uint32_t)]);
1615 target_buffer_set_u32(target
, &buffer
[i
*sizeof(uint32_t)], target_data
);
1618 retval
= image_calculate_checksum(buffer
, size
, &checksum
);
1627 int target_blank_check_memory(struct target
*target
, uint32_t address
, uint32_t size
, uint32_t* blank
)
1630 if (!target_was_examined(target
)) {
1631 LOG_ERROR("Target not examined yet");
1635 if (target
->type
->blank_check_memory
== 0)
1636 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
1638 retval
= target
->type
->blank_check_memory(target
, address
, size
, blank
);
1643 int target_read_u32(struct target
*target
, uint32_t address
, uint32_t *value
)
1645 uint8_t value_buf
[4];
1646 if (!target_was_examined(target
)) {
1647 LOG_ERROR("Target not examined yet");
1651 int retval
= target_read_memory(target
, address
, 4, 1, value_buf
);
1653 if (retval
== ERROR_OK
) {
1654 *value
= target_buffer_get_u32(target
, value_buf
);
1655 LOG_DEBUG("address: 0x%8.8" PRIx32
", value: 0x%8.8" PRIx32
"",
1660 LOG_DEBUG("address: 0x%8.8" PRIx32
" failed",
1667 int target_read_u16(struct target
*target
, uint32_t address
, uint16_t *value
)
1669 uint8_t value_buf
[2];
1670 if (!target_was_examined(target
)) {
1671 LOG_ERROR("Target not examined yet");
1675 int retval
= target_read_memory(target
, address
, 2, 1, value_buf
);
1677 if (retval
== ERROR_OK
) {
1678 *value
= target_buffer_get_u16(target
, value_buf
);
1679 LOG_DEBUG("address: 0x%8.8" PRIx32
", value: 0x%4.4x",
1684 LOG_DEBUG("address: 0x%8.8" PRIx32
" failed",
1691 int target_read_u8(struct target
*target
, uint32_t address
, uint8_t *value
)
1693 int retval
= target_read_memory(target
, address
, 1, 1, value
);
1694 if (!target_was_examined(target
)) {
1695 LOG_ERROR("Target not examined yet");
1699 if (retval
== ERROR_OK
) {
1700 LOG_DEBUG("address: 0x%8.8" PRIx32
", value: 0x%2.2x",
1705 LOG_DEBUG("address: 0x%8.8" PRIx32
" failed",
1712 int target_write_u32(struct target
*target
, uint32_t address
, uint32_t value
)
1715 uint8_t value_buf
[4];
1716 if (!target_was_examined(target
)) {
1717 LOG_ERROR("Target not examined yet");
1721 LOG_DEBUG("address: 0x%8.8" PRIx32
", value: 0x%8.8" PRIx32
"",
1725 target_buffer_set_u32(target
, value_buf
, value
);
1726 retval
= target_write_memory(target
, address
, 4, 1, value_buf
);
1727 if (retval
!= ERROR_OK
)
1728 LOG_DEBUG("failed: %i", retval
);
1733 int target_write_u16(struct target
*target
, uint32_t address
, uint16_t value
)
1736 uint8_t value_buf
[2];
1737 if (!target_was_examined(target
)) {
1738 LOG_ERROR("Target not examined yet");
1742 LOG_DEBUG("address: 0x%8.8" PRIx32
", value: 0x%8.8x",
1746 target_buffer_set_u16(target
, value_buf
, value
);
1747 retval
= target_write_memory(target
, address
, 2, 1, value_buf
);
1748 if (retval
!= ERROR_OK
)
1749 LOG_DEBUG("failed: %i", retval
);
1754 int target_write_u8(struct target
*target
, uint32_t address
, uint8_t value
)
1757 if (!target_was_examined(target
)) {
1758 LOG_ERROR("Target not examined yet");
1762 LOG_DEBUG("address: 0x%8.8" PRIx32
", value: 0x%2.2x",
1765 retval
= target_write_memory(target
, address
, 1, 1, &value
);
1766 if (retval
!= ERROR_OK
)
1767 LOG_DEBUG("failed: %i", retval
);
1772 static int find_target(struct command_context
*cmd_ctx
, const char *name
)
1774 struct target
*target
= get_target(name
);
1775 if (target
== NULL
) {
1776 LOG_ERROR("Target: %s is unknown, try one of:\n", name
);
1779 if (!target
->tap
->enabled
) {
1780 LOG_USER("Target: TAP %s is disabled, "
1781 "can't be the current target\n",
1782 target
->tap
->dotted_name
);
1786 cmd_ctx
->current_target
= target
->target_number
;
1791 COMMAND_HANDLER(handle_targets_command
)
1793 int retval
= ERROR_OK
;
1794 if (CMD_ARGC
== 1) {
1795 retval
= find_target(CMD_CTX
, CMD_ARGV
[0]);
1796 if (retval
== ERROR_OK
) {
1802 struct target
*target
= all_targets
;
1803 command_print(CMD_CTX
, " TargetName Type Endian TapName State ");
1804 command_print(CMD_CTX
, "-- ------------------ ---------- ------ ------------------ ------------");
1809 if (target
->tap
->enabled
)
1810 state
= target_state_name(target
);
1812 state
= "tap-disabled";
1814 if (CMD_CTX
->current_target
== target
->target_number
)
1817 /* keep columns lined up to match the headers above */
1818 command_print(CMD_CTX
,
1819 "%2d%c %-18s %-10s %-6s %-18s %s",
1820 target
->target_number
,
1822 target_name(target
),
1823 target_type_name(target
),
1824 Jim_Nvp_value2name_simple(nvp_target_endian
,
1825 target
->endianness
)->name
,
1826 target
->tap
->dotted_name
,
1828 target
= target
->next
;
1834 /* every 300ms we check for reset & powerdropout and issue a "reset halt" if so. */
1836 static int powerDropout
;
1837 static int srstAsserted
;
1839 static int runPowerRestore
;
1840 static int runPowerDropout
;
1841 static int runSrstAsserted
;
1842 static int runSrstDeasserted
;
1844 static int sense_handler(void)
1846 static int prevSrstAsserted
;
1847 static int prevPowerdropout
;
1849 int retval
= jtag_power_dropout(&powerDropout
);
1850 if (retval
!= ERROR_OK
)
1854 powerRestored
= prevPowerdropout
&& !powerDropout
;
1856 runPowerRestore
= 1;
1858 long long current
= timeval_ms();
1859 static long long lastPower
;
1860 int waitMore
= lastPower
+ 2000 > current
;
1861 if (powerDropout
&& !waitMore
) {
1862 runPowerDropout
= 1;
1863 lastPower
= current
;
1866 retval
= jtag_srst_asserted(&srstAsserted
);
1867 if (retval
!= ERROR_OK
)
1871 srstDeasserted
= prevSrstAsserted
&& !srstAsserted
;
1873 static long long lastSrst
;
1874 waitMore
= lastSrst
+ 2000 > current
;
1875 if (srstDeasserted
&& !waitMore
) {
1876 runSrstDeasserted
= 1;
1880 if (!prevSrstAsserted
&& srstAsserted
)
1881 runSrstAsserted
= 1;
1883 prevSrstAsserted
= srstAsserted
;
1884 prevPowerdropout
= powerDropout
;
1886 if (srstDeasserted
|| powerRestored
) {
1887 /* Other than logging the event we can't do anything here.
1888 * Issuing a reset is a particularly bad idea as we might
1889 * be inside a reset already.
1896 static int backoff_times
;
1897 static int backoff_count
;
1899 /* process target state changes */
1900 static int handle_target(void *priv
)
1902 Jim_Interp
*interp
= (Jim_Interp
*)priv
;
1903 int retval
= ERROR_OK
;
1905 if (!is_jtag_poll_safe()) {
1906 /* polling is disabled currently */
1910 /* we do not want to recurse here... */
1911 static int recursive
;
1915 /* danger! running these procedures can trigger srst assertions and power dropouts.
1916 * We need to avoid an infinite loop/recursion here and we do that by
1917 * clearing the flags after running these events.
1919 int did_something
= 0;
1920 if (runSrstAsserted
) {
1921 LOG_INFO("srst asserted detected, running srst_asserted proc.");
1922 Jim_Eval(interp
, "srst_asserted");
1925 if (runSrstDeasserted
) {
1926 Jim_Eval(interp
, "srst_deasserted");
1929 if (runPowerDropout
) {
1930 LOG_INFO("Power dropout detected, running power_dropout proc.");
1931 Jim_Eval(interp
, "power_dropout");
1934 if (runPowerRestore
) {
1935 Jim_Eval(interp
, "power_restore");
1939 if (did_something
) {
1940 /* clear detect flags */
1944 /* clear action flags */
1946 runSrstAsserted
= 0;
1947 runSrstDeasserted
= 0;
1948 runPowerRestore
= 0;
1949 runPowerDropout
= 0;
1954 if (backoff_times
> backoff_count
) {
1955 /* do not poll this time as we failed previously */
1961 /* Poll targets for state changes unless that's globally disabled.
1962 * Skip targets that are currently disabled.
1964 for (struct target
*target
= all_targets
;
1965 is_jtag_poll_safe() && target
;
1966 target
= target
->next
) {
1967 if (!target
->tap
->enabled
)
1970 /* only poll target if we've got power and srst isn't asserted */
1971 if (!powerDropout
&& !srstAsserted
) {
1972 /* polling may fail silently until the target has been examined */
1973 retval
= target_poll(target
);
1974 if (retval
!= ERROR_OK
) {
1975 /* 100ms polling interval. Increase interval between polling up to 5000ms */
1976 if (backoff_times
* polling_interval
< 5000) {
1980 LOG_USER("Polling target failed, GDB will be halted. Polling again in %dms",
1981 backoff_times
* polling_interval
);
1983 /* Tell GDB to halt the debugger. This allows the user to
1984 * run monitor commands to handle the situation.
1986 target_call_event_callbacks(target
, TARGET_EVENT_GDB_HALT
);
1989 /* Since we succeeded, we reset backoff count */
1990 if (backoff_times
> 0)
1991 LOG_USER("Polling succeeded again");
1999 COMMAND_HANDLER(handle_reg_command
)
2001 struct target
*target
;
2002 struct reg
*reg
= NULL
;
2008 target
= get_current_target(CMD_CTX
);
2010 /* list all available registers for the current target */
2011 if (CMD_ARGC
== 0) {
2012 struct reg_cache
*cache
= target
->reg_cache
;
2018 command_print(CMD_CTX
, "===== %s", cache
->name
);
2020 for (i
= 0, reg
= cache
->reg_list
;
2021 i
< cache
->num_regs
;
2022 i
++, reg
++, count
++) {
2023 /* only print cached values if they are valid */
2025 value
= buf_to_str(reg
->value
,
2027 command_print(CMD_CTX
,
2028 "(%i) %s (/%" PRIu32
"): 0x%s%s",
2036 command_print(CMD_CTX
, "(%i) %s (/%" PRIu32
")",
2041 cache
= cache
->next
;
2047 /* access a single register by its ordinal number */
2048 if ((CMD_ARGV
[0][0] >= '0') && (CMD_ARGV
[0][0] <= '9')) {
2050 COMMAND_PARSE_NUMBER(uint
, CMD_ARGV
[0], num
);
2052 struct reg_cache
*cache
= target
->reg_cache
;
2056 for (i
= 0; i
< cache
->num_regs
; i
++) {
2057 if (count
++ == num
) {
2058 reg
= &cache
->reg_list
[i
];
2064 cache
= cache
->next
;
2068 command_print(CMD_CTX
, "%i is out of bounds, the current target "
2069 "has only %i registers (0 - %i)", num
, count
, count
- 1);
2073 /* access a single register by its name */
2074 reg
= register_get_by_name(target
->reg_cache
, CMD_ARGV
[0], 1);
2077 command_print(CMD_CTX
, "register %s not found in current target", CMD_ARGV
[0]);
2082 assert(reg
!= NULL
); /* give clang a hint that we *know* reg is != NULL here */
2084 /* display a register */
2085 if ((CMD_ARGC
== 1) || ((CMD_ARGC
== 2) && !((CMD_ARGV
[1][0] >= '0')
2086 && (CMD_ARGV
[1][0] <= '9')))) {
2087 if ((CMD_ARGC
== 2) && (strcmp(CMD_ARGV
[1], "force") == 0))
2090 if (reg
->valid
== 0)
2091 reg
->type
->get(reg
);
2092 value
= buf_to_str(reg
->value
, reg
->size
, 16);
2093 command_print(CMD_CTX
, "%s (/%i): 0x%s", reg
->name
, (int)(reg
->size
), value
);
2098 /* set register value */
2099 if (CMD_ARGC
== 2) {
2100 uint8_t *buf
= malloc(DIV_ROUND_UP(reg
->size
, 8));
2103 str_to_buf(CMD_ARGV
[1], strlen(CMD_ARGV
[1]), buf
, reg
->size
, 0);
2105 reg
->type
->set(reg
, buf
);
2107 value
= buf_to_str(reg
->value
, reg
->size
, 16);
2108 command_print(CMD_CTX
, "%s (/%i): 0x%s", reg
->name
, (int)(reg
->size
), value
);
2116 return ERROR_COMMAND_SYNTAX_ERROR
;
2119 COMMAND_HANDLER(handle_poll_command
)
2121 int retval
= ERROR_OK
;
2122 struct target
*target
= get_current_target(CMD_CTX
);
2124 if (CMD_ARGC
== 0) {
2125 command_print(CMD_CTX
, "background polling: %s",
2126 jtag_poll_get_enabled() ? "on" : "off");
2127 command_print(CMD_CTX
, "TAP: %s (%s)",
2128 target
->tap
->dotted_name
,
2129 target
->tap
->enabled
? "enabled" : "disabled");
2130 if (!target
->tap
->enabled
)
2132 retval
= target_poll(target
);
2133 if (retval
!= ERROR_OK
)
2135 retval
= target_arch_state(target
);
2136 if (retval
!= ERROR_OK
)
2138 } else if (CMD_ARGC
== 1) {
2140 COMMAND_PARSE_ON_OFF(CMD_ARGV
[0], enable
);
2141 jtag_poll_set_enabled(enable
);
2143 return ERROR_COMMAND_SYNTAX_ERROR
;
2148 COMMAND_HANDLER(handle_wait_halt_command
)
2151 return ERROR_COMMAND_SYNTAX_ERROR
;
2154 if (1 == CMD_ARGC
) {
2155 int retval
= parse_uint(CMD_ARGV
[0], &ms
);
2156 if (ERROR_OK
!= retval
)
2157 return ERROR_COMMAND_SYNTAX_ERROR
;
2158 /* convert seconds (given) to milliseconds (needed) */
2162 struct target
*target
= get_current_target(CMD_CTX
);
2163 return target_wait_state(target
, TARGET_HALTED
, ms
);
2166 /* wait for target state to change. The trick here is to have a low
2167 * latency for short waits and not to suck up all the CPU time
2170 * After 500ms, keep_alive() is invoked
2172 int target_wait_state(struct target
*target
, enum target_state state
, int ms
)
2175 long long then
= 0, cur
;
2179 retval
= target_poll(target
);
2180 if (retval
!= ERROR_OK
)
2182 if (target
->state
== state
)
2187 then
= timeval_ms();
2188 LOG_DEBUG("waiting for target %s...",
2189 Jim_Nvp_value2name_simple(nvp_target_state
, state
)->name
);
2195 if ((cur
-then
) > ms
) {
2196 LOG_ERROR("timed out while waiting for target %s",
2197 Jim_Nvp_value2name_simple(nvp_target_state
, state
)->name
);
2205 COMMAND_HANDLER(handle_halt_command
)
2209 struct target
*target
= get_current_target(CMD_CTX
);
2210 int retval
= target_halt(target
);
2211 if (ERROR_OK
!= retval
)
2214 if (CMD_ARGC
== 1) {
2215 unsigned wait_local
;
2216 retval
= parse_uint(CMD_ARGV
[0], &wait_local
);
2217 if (ERROR_OK
!= retval
)
2218 return ERROR_COMMAND_SYNTAX_ERROR
;
2223 return CALL_COMMAND_HANDLER(handle_wait_halt_command
);
2226 COMMAND_HANDLER(handle_soft_reset_halt_command
)
2228 struct target
*target
= get_current_target(CMD_CTX
);
2230 LOG_USER("requesting target halt and executing a soft reset");
2232 target
->type
->soft_reset_halt(target
);
2237 COMMAND_HANDLER(handle_reset_command
)
2240 return ERROR_COMMAND_SYNTAX_ERROR
;
2242 enum target_reset_mode reset_mode
= RESET_RUN
;
2243 if (CMD_ARGC
== 1) {
2245 n
= Jim_Nvp_name2value_simple(nvp_reset_modes
, CMD_ARGV
[0]);
2246 if ((n
->name
== NULL
) || (n
->value
== RESET_UNKNOWN
))
2247 return ERROR_COMMAND_SYNTAX_ERROR
;
2248 reset_mode
= n
->value
;
2251 /* reset *all* targets */
2252 return target_process_reset(CMD_CTX
, reset_mode
);
2256 COMMAND_HANDLER(handle_resume_command
)
2260 return ERROR_COMMAND_SYNTAX_ERROR
;
2262 struct target
*target
= get_current_target(CMD_CTX
);
2263 target_handle_event(target
, TARGET_EVENT_OLD_pre_resume
);
2265 /* with no CMD_ARGV, resume from current pc, addr = 0,
2266 * with one arguments, addr = CMD_ARGV[0],
2267 * handle breakpoints, not debugging */
2269 if (CMD_ARGC
== 1) {
2270 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], addr
);
2274 return target_resume(target
, current
, addr
, 1, 0);
2277 COMMAND_HANDLER(handle_step_command
)
2280 return ERROR_COMMAND_SYNTAX_ERROR
;
2284 /* with no CMD_ARGV, step from current pc, addr = 0,
2285 * with one argument addr = CMD_ARGV[0],
2286 * handle breakpoints, debugging */
2289 if (CMD_ARGC
== 1) {
2290 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], addr
);
2294 struct target
*target
= get_current_target(CMD_CTX
);
2296 return target
->type
->step(target
, current_pc
, addr
, 1);
2299 static void handle_md_output(struct command_context
*cmd_ctx
,
2300 struct target
*target
, uint32_t address
, unsigned size
,
2301 unsigned count
, const uint8_t *buffer
)
2303 const unsigned line_bytecnt
= 32;
2304 unsigned line_modulo
= line_bytecnt
/ size
;
2306 char output
[line_bytecnt
* 4 + 1];
2307 unsigned output_len
= 0;
2309 const char *value_fmt
;
2312 value_fmt
= "%8.8x ";
2315 value_fmt
= "%4.4x ";
2318 value_fmt
= "%2.2x ";
2321 /* "can't happen", caller checked */
2322 LOG_ERROR("invalid memory read size: %u", size
);
2326 for (unsigned i
= 0; i
< count
; i
++) {
2327 if (i
% line_modulo
== 0) {
2328 output_len
+= snprintf(output
+ output_len
,
2329 sizeof(output
) - output_len
,
2331 (unsigned)(address
+ (i
*size
)));
2335 const uint8_t *value_ptr
= buffer
+ i
* size
;
2338 value
= target_buffer_get_u32(target
, value_ptr
);
2341 value
= target_buffer_get_u16(target
, value_ptr
);
2346 output_len
+= snprintf(output
+ output_len
,
2347 sizeof(output
) - output_len
,
2350 if ((i
% line_modulo
== line_modulo
- 1) || (i
== count
- 1)) {
2351 command_print(cmd_ctx
, "%s", output
);
2357 COMMAND_HANDLER(handle_md_command
)
2360 return ERROR_COMMAND_SYNTAX_ERROR
;
2363 switch (CMD_NAME
[2]) {
2374 return ERROR_COMMAND_SYNTAX_ERROR
;
2377 bool physical
= strcmp(CMD_ARGV
[0], "phys") == 0;
2378 int (*fn
)(struct target
*target
,
2379 uint32_t address
, uint32_t size_value
, uint32_t count
, uint8_t *buffer
);
2383 fn
= target_read_phys_memory
;
2385 fn
= target_read_memory
;
2386 if ((CMD_ARGC
< 1) || (CMD_ARGC
> 2))
2387 return ERROR_COMMAND_SYNTAX_ERROR
;
2390 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], address
);
2394 COMMAND_PARSE_NUMBER(uint
, CMD_ARGV
[1], count
);
2396 uint8_t *buffer
= calloc(count
, size
);
2398 struct target
*target
= get_current_target(CMD_CTX
);
2399 int retval
= fn(target
, address
, size
, count
, buffer
);
2400 if (ERROR_OK
== retval
)
2401 handle_md_output(CMD_CTX
, target
, address
, size
, count
, buffer
);
2408 typedef int (*target_write_fn
)(struct target
*target
,
2409 uint32_t address
, uint32_t size
, uint32_t count
, const uint8_t *buffer
);
2411 static int target_write_memory_fast(struct target
*target
,
2412 uint32_t address
, uint32_t size
, uint32_t count
, const uint8_t *buffer
)
2414 return target_write_buffer(target
, address
, size
* count
, buffer
);
2417 static int target_fill_mem(struct target
*target
,
2426 /* We have to write in reasonably large chunks to be able
2427 * to fill large memory areas with any sane speed */
2428 const unsigned chunk_size
= 16384;
2429 uint8_t *target_buf
= malloc(chunk_size
* data_size
);
2430 if (target_buf
== NULL
) {
2431 LOG_ERROR("Out of memory");
2435 for (unsigned i
= 0; i
< chunk_size
; i
++) {
2436 switch (data_size
) {
2438 target_buffer_set_u32(target
, target_buf
+ i
* data_size
, b
);
2441 target_buffer_set_u16(target
, target_buf
+ i
* data_size
, b
);
2444 target_buffer_set_u8(target
, target_buf
+ i
* data_size
, b
);
2451 int retval
= ERROR_OK
;
2453 for (unsigned x
= 0; x
< c
; x
+= chunk_size
) {
2456 if (current
> chunk_size
)
2457 current
= chunk_size
;
2458 retval
= fn(target
, address
+ x
* data_size
, data_size
, current
, target_buf
);
2459 if (retval
!= ERROR_OK
)
2461 /* avoid GDB timeouts */
2470 COMMAND_HANDLER(handle_mw_command
)
2473 return ERROR_COMMAND_SYNTAX_ERROR
;
2474 bool physical
= strcmp(CMD_ARGV
[0], "phys") == 0;
2479 fn
= target_write_phys_memory
;
2481 fn
= target_write_memory_fast
;
2482 if ((CMD_ARGC
< 2) || (CMD_ARGC
> 3))
2483 return ERROR_COMMAND_SYNTAX_ERROR
;
2486 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], address
);
2489 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], value
);
2493 COMMAND_PARSE_NUMBER(uint
, CMD_ARGV
[2], count
);
2495 struct target
*target
= get_current_target(CMD_CTX
);
2497 switch (CMD_NAME
[2]) {
2508 return ERROR_COMMAND_SYNTAX_ERROR
;
2511 return target_fill_mem(target
, address
, fn
, wordsize
, value
, count
);
2514 static COMMAND_HELPER(parse_load_image_command_CMD_ARGV
, struct image
*image
,
2515 uint32_t *min_address
, uint32_t *max_address
)
2517 if (CMD_ARGC
< 1 || CMD_ARGC
> 5)
2518 return ERROR_COMMAND_SYNTAX_ERROR
;
2520 /* a base address isn't always necessary,
2521 * default to 0x0 (i.e. don't relocate) */
2522 if (CMD_ARGC
>= 2) {
2524 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], addr
);
2525 image
->base_address
= addr
;
2526 image
->base_address_set
= 1;
2528 image
->base_address_set
= 0;
2530 image
->start_address_set
= 0;
2533 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[3], *min_address
);
2534 if (CMD_ARGC
== 5) {
2535 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[4], *max_address
);
2536 /* use size (given) to find max (required) */
2537 *max_address
+= *min_address
;
2540 if (*min_address
> *max_address
)
2541 return ERROR_COMMAND_SYNTAX_ERROR
;
2546 COMMAND_HANDLER(handle_load_image_command
)
2550 uint32_t image_size
;
2551 uint32_t min_address
= 0;
2552 uint32_t max_address
= 0xffffffff;
2556 int retval
= CALL_COMMAND_HANDLER(parse_load_image_command_CMD_ARGV
,
2557 &image
, &min_address
, &max_address
);
2558 if (ERROR_OK
!= retval
)
2561 struct target
*target
= get_current_target(CMD_CTX
);
2563 struct duration bench
;
2564 duration_start(&bench
);
2566 if (image_open(&image
, CMD_ARGV
[0], (CMD_ARGC
>= 3) ? CMD_ARGV
[2] : NULL
) != ERROR_OK
)
2571 for (i
= 0; i
< image
.num_sections
; i
++) {
2572 buffer
= malloc(image
.sections
[i
].size
);
2573 if (buffer
== NULL
) {
2574 command_print(CMD_CTX
,
2575 "error allocating buffer for section (%d bytes)",
2576 (int)(image
.sections
[i
].size
));
2580 retval
= image_read_section(&image
, i
, 0x0, image
.sections
[i
].size
, buffer
, &buf_cnt
);
2581 if (retval
!= ERROR_OK
) {
2586 uint32_t offset
= 0;
2587 uint32_t length
= buf_cnt
;
2589 /* DANGER!!! beware of unsigned comparision here!!! */
2591 if ((image
.sections
[i
].base_address
+ buf_cnt
>= min_address
) &&
2592 (image
.sections
[i
].base_address
< max_address
)) {
2594 if (image
.sections
[i
].base_address
< min_address
) {
2595 /* clip addresses below */
2596 offset
+= min_address
-image
.sections
[i
].base_address
;
2600 if (image
.sections
[i
].base_address
+ buf_cnt
> max_address
)
2601 length
-= (image
.sections
[i
].base_address
+ buf_cnt
)-max_address
;
2603 retval
= target_write_buffer(target
,
2604 image
.sections
[i
].base_address
+ offset
, length
, buffer
+ offset
);
2605 if (retval
!= ERROR_OK
) {
2609 image_size
+= length
;
2610 command_print(CMD_CTX
, "%u bytes written at address 0x%8.8" PRIx32
"",
2611 (unsigned int)length
,
2612 image
.sections
[i
].base_address
+ offset
);
2618 if ((ERROR_OK
== retval
) && (duration_measure(&bench
) == ERROR_OK
)) {
2619 command_print(CMD_CTX
, "downloaded %" PRIu32
" bytes "
2620 "in %fs (%0.3f KiB/s)", image_size
,
2621 duration_elapsed(&bench
), duration_kbps(&bench
, image_size
));
2624 image_close(&image
);
2630 COMMAND_HANDLER(handle_dump_image_command
)
2632 struct fileio fileio
;
2634 int retval
, retvaltemp
;
2635 uint32_t address
, size
;
2636 struct duration bench
;
2637 struct target
*target
= get_current_target(CMD_CTX
);
2640 return ERROR_COMMAND_SYNTAX_ERROR
;
2642 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], address
);
2643 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[2], size
);
2645 uint32_t buf_size
= (size
> 4096) ? 4096 : size
;
2646 buffer
= malloc(buf_size
);
2650 retval
= fileio_open(&fileio
, CMD_ARGV
[0], FILEIO_WRITE
, FILEIO_BINARY
);
2651 if (retval
!= ERROR_OK
) {
2656 duration_start(&bench
);
2659 size_t size_written
;
2660 uint32_t this_run_size
= (size
> buf_size
) ? buf_size
: size
;
2661 retval
= target_read_buffer(target
, address
, this_run_size
, buffer
);
2662 if (retval
!= ERROR_OK
)
2665 retval
= fileio_write(&fileio
, this_run_size
, buffer
, &size_written
);
2666 if (retval
!= ERROR_OK
)
2669 size
-= this_run_size
;
2670 address
+= this_run_size
;
2675 if ((ERROR_OK
== retval
) && (duration_measure(&bench
) == ERROR_OK
)) {
2677 retval
= fileio_size(&fileio
, &filesize
);
2678 if (retval
!= ERROR_OK
)
2680 command_print(CMD_CTX
,
2681 "dumped %ld bytes in %fs (%0.3f KiB/s)", (long)filesize
,
2682 duration_elapsed(&bench
), duration_kbps(&bench
, filesize
));
2685 retvaltemp
= fileio_close(&fileio
);
2686 if (retvaltemp
!= ERROR_OK
)
2692 static COMMAND_HELPER(handle_verify_image_command_internal
, int verify
)
2696 uint32_t image_size
;
2699 uint32_t checksum
= 0;
2700 uint32_t mem_checksum
= 0;
2704 struct target
*target
= get_current_target(CMD_CTX
);
2707 return ERROR_COMMAND_SYNTAX_ERROR
;
2710 LOG_ERROR("no target selected");
2714 struct duration bench
;
2715 duration_start(&bench
);
2717 if (CMD_ARGC
>= 2) {
2719 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], addr
);
2720 image
.base_address
= addr
;
2721 image
.base_address_set
= 1;
2723 image
.base_address_set
= 0;
2724 image
.base_address
= 0x0;
2727 image
.start_address_set
= 0;
2729 retval
= image_open(&image
, CMD_ARGV
[0], (CMD_ARGC
== 3) ? CMD_ARGV
[2] : NULL
);
2730 if (retval
!= ERROR_OK
)
2736 for (i
= 0; i
< image
.num_sections
; i
++) {
2737 buffer
= malloc(image
.sections
[i
].size
);
2738 if (buffer
== NULL
) {
2739 command_print(CMD_CTX
,
2740 "error allocating buffer for section (%d bytes)",
2741 (int)(image
.sections
[i
].size
));
2744 retval
= image_read_section(&image
, i
, 0x0, image
.sections
[i
].size
, buffer
, &buf_cnt
);
2745 if (retval
!= ERROR_OK
) {
2751 /* calculate checksum of image */
2752 retval
= image_calculate_checksum(buffer
, buf_cnt
, &checksum
);
2753 if (retval
!= ERROR_OK
) {
2758 retval
= target_checksum_memory(target
, image
.sections
[i
].base_address
, buf_cnt
, &mem_checksum
);
2759 if (retval
!= ERROR_OK
) {
2764 if (checksum
!= mem_checksum
) {
2765 /* failed crc checksum, fall back to a binary compare */
2769 LOG_ERROR("checksum mismatch - attempting binary compare");
2771 data
= (uint8_t *)malloc(buf_cnt
);
2773 /* Can we use 32bit word accesses? */
2775 int count
= buf_cnt
;
2776 if ((count
% 4) == 0) {
2780 retval
= target_read_memory(target
, image
.sections
[i
].base_address
, size
, count
, data
);
2781 if (retval
== ERROR_OK
) {
2783 for (t
= 0; t
< buf_cnt
; t
++) {
2784 if (data
[t
] != buffer
[t
]) {
2785 command_print(CMD_CTX
,
2786 "diff %d address 0x%08x. Was 0x%02x instead of 0x%02x",
2788 (unsigned)(t
+ image
.sections
[i
].base_address
),
2791 if (diffs
++ >= 127) {
2792 command_print(CMD_CTX
, "More than 128 errors, the rest are not printed.");
2804 command_print(CMD_CTX
, "address 0x%08" PRIx32
" length 0x%08zx",
2805 image
.sections
[i
].base_address
,
2810 image_size
+= buf_cnt
;
2813 command_print(CMD_CTX
, "No more differences found.");
2816 retval
= ERROR_FAIL
;
2817 if ((ERROR_OK
== retval
) && (duration_measure(&bench
) == ERROR_OK
)) {
2818 command_print(CMD_CTX
, "verified %" PRIu32
" bytes "
2819 "in %fs (%0.3f KiB/s)", image_size
,
2820 duration_elapsed(&bench
), duration_kbps(&bench
, image_size
));
2823 image_close(&image
);
2828 COMMAND_HANDLER(handle_verify_image_command
)
2830 return CALL_COMMAND_HANDLER(handle_verify_image_command_internal
, 1);
2833 COMMAND_HANDLER(handle_test_image_command
)
2835 return CALL_COMMAND_HANDLER(handle_verify_image_command_internal
, 0);
2838 static int handle_bp_command_list(struct command_context
*cmd_ctx
)
2840 struct target
*target
= get_current_target(cmd_ctx
);
2841 struct breakpoint
*breakpoint
= target
->breakpoints
;
2842 while (breakpoint
) {
2843 if (breakpoint
->type
== BKPT_SOFT
) {
2844 char *buf
= buf_to_str(breakpoint
->orig_instr
,
2845 breakpoint
->length
, 16);
2846 command_print(cmd_ctx
, "IVA breakpoint: 0x%8.8" PRIx32
", 0x%x, %i, 0x%s",
2847 breakpoint
->address
,
2849 breakpoint
->set
, buf
);
2852 if ((breakpoint
->address
== 0) && (breakpoint
->asid
!= 0))
2853 command_print(cmd_ctx
, "Context breakpoint: 0x%8.8" PRIx32
", 0x%x, %i",
2855 breakpoint
->length
, breakpoint
->set
);
2856 else if ((breakpoint
->address
!= 0) && (breakpoint
->asid
!= 0)) {
2857 command_print(cmd_ctx
, "Hybrid breakpoint(IVA): 0x%8.8" PRIx32
", 0x%x, %i",
2858 breakpoint
->address
,
2859 breakpoint
->length
, breakpoint
->set
);
2860 command_print(cmd_ctx
, "\t|--->linked with ContextID: 0x%8.8" PRIx32
,
2863 command_print(cmd_ctx
, "Breakpoint(IVA): 0x%8.8" PRIx32
", 0x%x, %i",
2864 breakpoint
->address
,
2865 breakpoint
->length
, breakpoint
->set
);
2868 breakpoint
= breakpoint
->next
;
2873 static int handle_bp_command_set(struct command_context
*cmd_ctx
,
2874 uint32_t addr
, uint32_t asid
, uint32_t length
, int hw
)
2876 struct target
*target
= get_current_target(cmd_ctx
);
2879 int retval
= breakpoint_add(target
, addr
, length
, hw
);
2880 if (ERROR_OK
== retval
)
2881 command_print(cmd_ctx
, "breakpoint set at 0x%8.8" PRIx32
"", addr
);
2883 LOG_ERROR("Failure setting breakpoint, the same address(IVA) is already used");
2886 } else if (addr
== 0) {
2887 int retval
= context_breakpoint_add(target
, asid
, length
, hw
);
2888 if (ERROR_OK
== retval
)
2889 command_print(cmd_ctx
, "Context breakpoint set at 0x%8.8" PRIx32
"", asid
);
2891 LOG_ERROR("Failure setting breakpoint, the same address(CONTEXTID) is already used");
2895 int retval
= hybrid_breakpoint_add(target
, addr
, asid
, length
, hw
);
2896 if (ERROR_OK
== retval
)
2897 command_print(cmd_ctx
, "Hybrid breakpoint set at 0x%8.8" PRIx32
"", asid
);
2899 LOG_ERROR("Failure setting breakpoint, the same address is already used");
2906 COMMAND_HANDLER(handle_bp_command
)
2915 return handle_bp_command_list(CMD_CTX
);
2919 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], addr
);
2920 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], length
);
2921 return handle_bp_command_set(CMD_CTX
, addr
, asid
, length
, hw
);
2924 if (strcmp(CMD_ARGV
[2], "hw") == 0) {
2926 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], addr
);
2928 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], length
);
2931 return handle_bp_command_set(CMD_CTX
, addr
, asid
, length
, hw
);
2932 } else if (strcmp(CMD_ARGV
[2], "hw_ctx") == 0) {
2934 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], asid
);
2935 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], length
);
2937 return handle_bp_command_set(CMD_CTX
, addr
, asid
, length
, hw
);
2942 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], addr
);
2943 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], asid
);
2944 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[2], length
);
2945 return handle_bp_command_set(CMD_CTX
, addr
, asid
, length
, hw
);
2948 return ERROR_COMMAND_SYNTAX_ERROR
;
2952 COMMAND_HANDLER(handle_rbp_command
)
2955 return ERROR_COMMAND_SYNTAX_ERROR
;
2958 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], addr
);
2960 struct target
*target
= get_current_target(CMD_CTX
);
2961 breakpoint_remove(target
, addr
);
2966 COMMAND_HANDLER(handle_wp_command
)
2968 struct target
*target
= get_current_target(CMD_CTX
);
2970 if (CMD_ARGC
== 0) {
2971 struct watchpoint
*watchpoint
= target
->watchpoints
;
2973 while (watchpoint
) {
2974 command_print(CMD_CTX
, "address: 0x%8.8" PRIx32
2975 ", len: 0x%8.8" PRIx32
2976 ", r/w/a: %i, value: 0x%8.8" PRIx32
2977 ", mask: 0x%8.8" PRIx32
,
2978 watchpoint
->address
,
2980 (int)watchpoint
->rw
,
2983 watchpoint
= watchpoint
->next
;
2988 enum watchpoint_rw type
= WPT_ACCESS
;
2990 uint32_t length
= 0;
2991 uint32_t data_value
= 0x0;
2992 uint32_t data_mask
= 0xffffffff;
2996 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[4], data_mask
);
2999 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[3], data_value
);
3002 switch (CMD_ARGV
[2][0]) {
3013 LOG_ERROR("invalid watchpoint mode ('%c')", CMD_ARGV
[2][0]);
3014 return ERROR_COMMAND_SYNTAX_ERROR
;
3018 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], length
);
3019 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], addr
);
3023 return ERROR_COMMAND_SYNTAX_ERROR
;
3026 int retval
= watchpoint_add(target
, addr
, length
, type
,
3027 data_value
, data_mask
);
3028 if (ERROR_OK
!= retval
)
3029 LOG_ERROR("Failure setting watchpoints");
3034 COMMAND_HANDLER(handle_rwp_command
)
3037 return ERROR_COMMAND_SYNTAX_ERROR
;
3040 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], addr
);
3042 struct target
*target
= get_current_target(CMD_CTX
);
3043 watchpoint_remove(target
, addr
);
3049 * Translate a virtual address to a physical address.
3051 * The low-level target implementation must have logged a detailed error
3052 * which is forwarded to telnet/GDB session.
3054 COMMAND_HANDLER(handle_virt2phys_command
)
3057 return ERROR_COMMAND_SYNTAX_ERROR
;
3060 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], va
);
3063 struct target
*target
= get_current_target(CMD_CTX
);
3064 int retval
= target
->type
->virt2phys(target
, va
, &pa
);
3065 if (retval
== ERROR_OK
)
3066 command_print(CMD_CTX
, "Physical address 0x%08" PRIx32
"", pa
);
3071 static void writeData(FILE *f
, const void *data
, size_t len
)
3073 size_t written
= fwrite(data
, 1, len
, f
);
3075 LOG_ERROR("failed to write %zu bytes: %s", len
, strerror(errno
));
3078 static void writeLong(FILE *f
, int l
)
3081 for (i
= 0; i
< 4; i
++) {
3082 char c
= (l
>> (i
*8))&0xff;
3083 writeData(f
, &c
, 1);
3088 static void writeString(FILE *f
, char *s
)
3090 writeData(f
, s
, strlen(s
));
3093 /* Dump a gmon.out histogram file. */
3094 static void writeGmon(uint32_t *samples
, uint32_t sampleNum
, const char *filename
)
3097 FILE *f
= fopen(filename
, "w");
3100 writeString(f
, "gmon");
3101 writeLong(f
, 0x00000001); /* Version */
3102 writeLong(f
, 0); /* padding */
3103 writeLong(f
, 0); /* padding */
3104 writeLong(f
, 0); /* padding */
3106 uint8_t zero
= 0; /* GMON_TAG_TIME_HIST */
3107 writeData(f
, &zero
, 1);
3109 /* figure out bucket size */
3110 uint32_t min
= samples
[0];
3111 uint32_t max
= samples
[0];
3112 for (i
= 0; i
< sampleNum
; i
++) {
3113 if (min
> samples
[i
])
3115 if (max
< samples
[i
])
3119 int addressSpace
= (max
- min
+ 1);
3120 assert(addressSpace
>= 2);
3122 static const uint32_t maxBuckets
= 16 * 1024; /* maximum buckets. */
3123 uint32_t length
= addressSpace
;
3124 if (length
> maxBuckets
)
3125 length
= maxBuckets
;
3126 int *buckets
= malloc(sizeof(int)*length
);
3127 if (buckets
== NULL
) {
3131 memset(buckets
, 0, sizeof(int) * length
);
3132 for (i
= 0; i
< sampleNum
; i
++) {
3133 uint32_t address
= samples
[i
];
3134 long long a
= address
- min
;
3135 long long b
= length
- 1;
3136 long long c
= addressSpace
- 1;
3137 int index_t
= (a
* b
) / c
; /* danger!!!! int32 overflows */
3141 /* append binary memory gmon.out &profile_hist_hdr ((char*)&profile_hist_hdr + sizeof(struct gmon_hist_hdr)) */
3142 writeLong(f
, min
); /* low_pc */
3143 writeLong(f
, max
); /* high_pc */
3144 writeLong(f
, length
); /* # of samples */
3145 writeLong(f
, 100); /* KLUDGE! We lie, ca. 100Hz best case. */
3146 writeString(f
, "seconds");
3147 for (i
= 0; i
< (15-strlen("seconds")); i
++)
3148 writeData(f
, &zero
, 1);
3149 writeString(f
, "s");
3151 /*append binary memory gmon.out profile_hist_data (profile_hist_data + profile_hist_hdr.hist_size) */
3153 char *data
= malloc(2 * length
);
3155 for (i
= 0; i
< length
; i
++) {
3160 data
[i
* 2] = val
&0xff;
3161 data
[i
* 2 + 1] = (val
>> 8) & 0xff;
3164 writeData(f
, data
, length
* 2);
3172 /* profiling samples the CPU PC as quickly as OpenOCD is able,
3173 * which will be used as a random sampling of PC */
3174 COMMAND_HANDLER(handle_profile_command
)
3176 struct target
*target
= get_current_target(CMD_CTX
);
3177 struct timeval timeout
, now
;
3179 gettimeofday(&timeout
, NULL
);
3181 return ERROR_COMMAND_SYNTAX_ERROR
;
3183 COMMAND_PARSE_NUMBER(uint
, CMD_ARGV
[0], offset
);
3185 timeval_add_time(&timeout
, offset
, 0);
3188 * @todo: Some cores let us sample the PC without the
3189 * annoying halt/resume step; for example, ARMv7 PCSR.
3190 * Provide a way to use that more efficient mechanism.
3193 command_print(CMD_CTX
, "Starting profiling. Halting and resuming the target as often as we can...");
3195 static const int maxSample
= 10000;
3196 uint32_t *samples
= malloc(sizeof(uint32_t)*maxSample
);
3197 if (samples
== NULL
)
3201 /* hopefully it is safe to cache! We want to stop/restart as quickly as possible. */
3202 struct reg
*reg
= register_get_by_name(target
->reg_cache
, "pc", 1);
3204 int retval
= ERROR_OK
;
3206 target_poll(target
);
3207 if (target
->state
== TARGET_HALTED
) {
3208 uint32_t t
= *((uint32_t *)reg
->value
);
3209 samples
[numSamples
++] = t
;
3210 /* current pc, addr = 0, do not handle breakpoints, not debugging */
3211 retval
= target_resume(target
, 1, 0, 0, 0);
3212 target_poll(target
);
3213 alive_sleep(10); /* sleep 10ms, i.e. <100 samples/second. */
3214 } else if (target
->state
== TARGET_RUNNING
) {
3215 /* We want to quickly sample the PC. */
3216 retval
= target_halt(target
);
3217 if (retval
!= ERROR_OK
) {
3222 command_print(CMD_CTX
, "Target not halted or running");
3226 if (retval
!= ERROR_OK
)
3229 gettimeofday(&now
, NULL
);
3230 if ((numSamples
>= maxSample
) || ((now
.tv_sec
>= timeout
.tv_sec
)
3231 && (now
.tv_usec
>= timeout
.tv_usec
))) {
3232 command_print(CMD_CTX
, "Profiling completed. %d samples.", numSamples
);
3233 retval
= target_poll(target
);
3234 if (retval
!= ERROR_OK
) {
3238 if (target
->state
== TARGET_HALTED
) {
3239 /* current pc, addr = 0, do not handle
3240 * breakpoints, not debugging */
3241 target_resume(target
, 1, 0, 0, 0);
3243 retval
= target_poll(target
);
3244 if (retval
!= ERROR_OK
) {
3248 writeGmon(samples
, numSamples
, CMD_ARGV
[1]);
3249 command_print(CMD_CTX
, "Wrote %s", CMD_ARGV
[1]);
3258 static int new_int_array_element(Jim_Interp
*interp
, const char *varname
, int idx
, uint32_t val
)
3261 Jim_Obj
*nameObjPtr
, *valObjPtr
;
3264 namebuf
= alloc_printf("%s(%d)", varname
, idx
);
3268 nameObjPtr
= Jim_NewStringObj(interp
, namebuf
, -1);
3269 valObjPtr
= Jim_NewIntObj(interp
, val
);
3270 if (!nameObjPtr
|| !valObjPtr
) {
3275 Jim_IncrRefCount(nameObjPtr
);
3276 Jim_IncrRefCount(valObjPtr
);
3277 result
= Jim_SetVariable(interp
, nameObjPtr
, valObjPtr
);
3278 Jim_DecrRefCount(interp
, nameObjPtr
);
3279 Jim_DecrRefCount(interp
, valObjPtr
);
3281 /* printf("%s(%d) <= 0%08x\n", varname, idx, val); */
3285 static int jim_mem2array(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
3287 struct command_context
*context
;
3288 struct target
*target
;
3290 context
= current_command_context(interp
);
3291 assert(context
!= NULL
);
3293 target
= get_current_target(context
);
3294 if (target
== NULL
) {
3295 LOG_ERROR("mem2array: no current target");
3299 return target_mem2array(interp
, target
, argc
- 1, argv
+ 1);
3302 static int target_mem2array(Jim_Interp
*interp
, struct target
*target
, int argc
, Jim_Obj
*const *argv
)
3310 const char *varname
;
3314 /* argv[1] = name of array to receive the data
3315 * argv[2] = desired width
3316 * argv[3] = memory address
3317 * argv[4] = count of times to read
3320 Jim_WrongNumArgs(interp
, 1, argv
, "varname width addr nelems");
3323 varname
= Jim_GetString(argv
[0], &len
);
3324 /* given "foo" get space for worse case "foo(%d)" .. add 20 */
3326 e
= Jim_GetLong(interp
, argv
[1], &l
);
3331 e
= Jim_GetLong(interp
, argv
[2], &l
);
3335 e
= Jim_GetLong(interp
, argv
[3], &l
);
3350 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3351 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "Invalid width param, must be 8/16/32", NULL
);
3355 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3356 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "mem2array: zero width read?", NULL
);
3359 if ((addr
+ (len
* width
)) < addr
) {
3360 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3361 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "mem2array: addr + len - wraps to zero?", NULL
);
3364 /* absurd transfer size? */
3366 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3367 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "mem2array: absurd > 64K item request", NULL
);
3372 ((width
== 2) && ((addr
& 1) == 0)) ||
3373 ((width
== 4) && ((addr
& 3) == 0))) {
3377 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3378 sprintf(buf
, "mem2array address: 0x%08" PRIx32
" is not aligned for %" PRId32
" byte reads",
3381 Jim_AppendStrings(interp
, Jim_GetResult(interp
), buf
, NULL
);
3390 size_t buffersize
= 4096;
3391 uint8_t *buffer
= malloc(buffersize
);
3398 /* Slurp... in buffer size chunks */
3400 count
= len
; /* in objects.. */
3401 if (count
> (buffersize
/ width
))
3402 count
= (buffersize
/ width
);
3404 retval
= target_read_memory(target
, addr
, width
, count
, buffer
);
3405 if (retval
!= ERROR_OK
) {
3407 LOG_ERROR("mem2array: Read @ 0x%08x, w=%d, cnt=%d, failed",
3411 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3412 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "mem2array: cannot read memory", NULL
);
3416 v
= 0; /* shut up gcc */
3417 for (i
= 0; i
< count
; i
++, n
++) {
3420 v
= target_buffer_get_u32(target
, &buffer
[i
*width
]);
3423 v
= target_buffer_get_u16(target
, &buffer
[i
*width
]);
3426 v
= buffer
[i
] & 0x0ff;
3429 new_int_array_element(interp
, varname
, n
, v
);
3437 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3442 static int get_int_array_element(Jim_Interp
*interp
, const char *varname
, int idx
, uint32_t *val
)
3445 Jim_Obj
*nameObjPtr
, *valObjPtr
;
3449 namebuf
= alloc_printf("%s(%d)", varname
, idx
);
3453 nameObjPtr
= Jim_NewStringObj(interp
, namebuf
, -1);
3459 Jim_IncrRefCount(nameObjPtr
);
3460 valObjPtr
= Jim_GetVariable(interp
, nameObjPtr
, JIM_ERRMSG
);
3461 Jim_DecrRefCount(interp
, nameObjPtr
);
3463 if (valObjPtr
== NULL
)
3466 result
= Jim_GetLong(interp
, valObjPtr
, &l
);
3467 /* printf("%s(%d) => 0%08x\n", varname, idx, val); */
3472 static int jim_array2mem(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
3474 struct command_context
*context
;
3475 struct target
*target
;
3477 context
= current_command_context(interp
);
3478 assert(context
!= NULL
);
3480 target
= get_current_target(context
);
3481 if (target
== NULL
) {
3482 LOG_ERROR("array2mem: no current target");
3486 return target_array2mem(interp
, target
, argc
-1, argv
+ 1);
3489 static int target_array2mem(Jim_Interp
*interp
, struct target
*target
,
3490 int argc
, Jim_Obj
*const *argv
)
3498 const char *varname
;
3502 /* argv[1] = name of array to get the data
3503 * argv[2] = desired width
3504 * argv[3] = memory address
3505 * argv[4] = count to write
3508 Jim_WrongNumArgs(interp
, 0, argv
, "varname width addr nelems");
3511 varname
= Jim_GetString(argv
[0], &len
);
3512 /* given "foo" get space for worse case "foo(%d)" .. add 20 */
3514 e
= Jim_GetLong(interp
, argv
[1], &l
);
3519 e
= Jim_GetLong(interp
, argv
[2], &l
);
3523 e
= Jim_GetLong(interp
, argv
[3], &l
);
3538 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3539 Jim_AppendStrings(interp
, Jim_GetResult(interp
),
3540 "Invalid width param, must be 8/16/32", NULL
);
3544 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3545 Jim_AppendStrings(interp
, Jim_GetResult(interp
),
3546 "array2mem: zero width read?", NULL
);
3549 if ((addr
+ (len
* width
)) < addr
) {
3550 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3551 Jim_AppendStrings(interp
, Jim_GetResult(interp
),
3552 "array2mem: addr + len - wraps to zero?", NULL
);
3555 /* absurd transfer size? */
3557 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3558 Jim_AppendStrings(interp
, Jim_GetResult(interp
),
3559 "array2mem: absurd > 64K item request", NULL
);
3564 ((width
== 2) && ((addr
& 1) == 0)) ||
3565 ((width
== 4) && ((addr
& 3) == 0))) {
3569 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3570 sprintf(buf
, "array2mem address: 0x%08x is not aligned for %d byte reads",
3573 Jim_AppendStrings(interp
, Jim_GetResult(interp
), buf
, NULL
);
3584 size_t buffersize
= 4096;
3585 uint8_t *buffer
= malloc(buffersize
);
3590 /* Slurp... in buffer size chunks */
3592 count
= len
; /* in objects.. */
3593 if (count
> (buffersize
/ width
))
3594 count
= (buffersize
/ width
);
3596 v
= 0; /* shut up gcc */
3597 for (i
= 0; i
< count
; i
++, n
++) {
3598 get_int_array_element(interp
, varname
, n
, &v
);
3601 target_buffer_set_u32(target
, &buffer
[i
* width
], v
);
3604 target_buffer_set_u16(target
, &buffer
[i
* width
], v
);
3607 buffer
[i
] = v
& 0x0ff;
3613 retval
= target_write_memory(target
, addr
, width
, count
, buffer
);
3614 if (retval
!= ERROR_OK
) {
3616 LOG_ERROR("array2mem: Write @ 0x%08x, w=%d, cnt=%d, failed",
3620 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3621 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "array2mem: cannot read memory", NULL
);
3629 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3634 /* FIX? should we propagate errors here rather than printing them
3637 void target_handle_event(struct target
*target
, enum target_event e
)
3639 struct target_event_action
*teap
;
3641 for (teap
= target
->event_action
; teap
!= NULL
; teap
= teap
->next
) {
3642 if (teap
->event
== e
) {
3643 LOG_DEBUG("target: (%d) %s (%s) event: %d (%s) action: %s",
3644 target
->target_number
,
3645 target_name(target
),
3646 target_type_name(target
),
3648 Jim_Nvp_value2name_simple(nvp_target_event
, e
)->name
,
3649 Jim_GetString(teap
->body
, NULL
));
3650 if (Jim_EvalObj(teap
->interp
, teap
->body
) != JIM_OK
) {
3651 Jim_MakeErrorMessage(teap
->interp
);
3652 command_print(NULL
, "%s\n", Jim_GetString(Jim_GetResult(teap
->interp
), NULL
));
3659 * Returns true only if the target has a handler for the specified event.
3661 bool target_has_event_action(struct target
*target
, enum target_event event
)
3663 struct target_event_action
*teap
;
3665 for (teap
= target
->event_action
; teap
!= NULL
; teap
= teap
->next
) {
3666 if (teap
->event
== event
)
3672 enum target_cfg_param
{
3675 TCFG_WORK_AREA_VIRT
,
3676 TCFG_WORK_AREA_PHYS
,
3677 TCFG_WORK_AREA_SIZE
,
3678 TCFG_WORK_AREA_BACKUP
,
3682 TCFG_CHAIN_POSITION
,
3687 static Jim_Nvp nvp_config_opts
[] = {
3688 { .name
= "-type", .value
= TCFG_TYPE
},
3689 { .name
= "-event", .value
= TCFG_EVENT
},
3690 { .name
= "-work-area-virt", .value
= TCFG_WORK_AREA_VIRT
},
3691 { .name
= "-work-area-phys", .value
= TCFG_WORK_AREA_PHYS
},
3692 { .name
= "-work-area-size", .value
= TCFG_WORK_AREA_SIZE
},
3693 { .name
= "-work-area-backup", .value
= TCFG_WORK_AREA_BACKUP
},
3694 { .name
= "-endian" , .value
= TCFG_ENDIAN
},
3695 { .name
= "-variant", .value
= TCFG_VARIANT
},
3696 { .name
= "-coreid", .value
= TCFG_COREID
},
3697 { .name
= "-chain-position", .value
= TCFG_CHAIN_POSITION
},
3698 { .name
= "-dbgbase", .value
= TCFG_DBGBASE
},
3699 { .name
= "-rtos", .value
= TCFG_RTOS
},
3700 { .name
= NULL
, .value
= -1 }
3703 static int target_configure(Jim_GetOptInfo
*goi
, struct target
*target
)
3711 /* parse config or cget options ... */
3712 while (goi
->argc
> 0) {
3713 Jim_SetEmptyResult(goi
->interp
);
3714 /* Jim_GetOpt_Debug(goi); */
3716 if (target
->type
->target_jim_configure
) {
3717 /* target defines a configure function */
3718 /* target gets first dibs on parameters */
3719 e
= (*(target
->type
->target_jim_configure
))(target
, goi
);
3728 /* otherwise we 'continue' below */
3730 e
= Jim_GetOpt_Nvp(goi
, nvp_config_opts
, &n
);
3732 Jim_GetOpt_NvpUnknown(goi
, nvp_config_opts
, 0);
3738 if (goi
->isconfigure
) {
3739 Jim_SetResultFormatted(goi
->interp
,
3740 "not settable: %s", n
->name
);
3744 if (goi
->argc
!= 0) {
3745 Jim_WrongNumArgs(goi
->interp
,
3746 goi
->argc
, goi
->argv
,
3751 Jim_SetResultString(goi
->interp
,
3752 target_type_name(target
), -1);
3756 if (goi
->argc
== 0) {
3757 Jim_WrongNumArgs(goi
->interp
, goi
->argc
, goi
->argv
, "-event ?event-name? ...");
3761 e
= Jim_GetOpt_Nvp(goi
, nvp_target_event
, &n
);
3763 Jim_GetOpt_NvpUnknown(goi
, nvp_target_event
, 1);
3767 if (goi
->isconfigure
) {
3768 if (goi
->argc
!= 1) {
3769 Jim_WrongNumArgs(goi
->interp
, goi
->argc
, goi
->argv
, "-event ?event-name? ?EVENT-BODY?");
3773 if (goi
->argc
!= 0) {
3774 Jim_WrongNumArgs(goi
->interp
, goi
->argc
, goi
->argv
, "-event ?event-name?");
3780 struct target_event_action
*teap
;
3782 teap
= target
->event_action
;
3783 /* replace existing? */
3785 if (teap
->event
== (enum target_event
)n
->value
)
3790 if (goi
->isconfigure
) {
3791 bool replace
= true;
3794 teap
= calloc(1, sizeof(*teap
));
3797 teap
->event
= n
->value
;
3798 teap
->interp
= goi
->interp
;
3799 Jim_GetOpt_Obj(goi
, &o
);
3801 Jim_DecrRefCount(teap
->interp
, teap
->body
);
3802 teap
->body
= Jim_DuplicateObj(goi
->interp
, o
);
3805 * Tcl/TK - "tk events" have a nice feature.
3806 * See the "BIND" command.
3807 * We should support that here.
3808 * You can specify %X and %Y in the event code.
3809 * The idea is: %T - target name.
3810 * The idea is: %N - target number
3811 * The idea is: %E - event name.
3813 Jim_IncrRefCount(teap
->body
);
3816 /* add to head of event list */
3817 teap
->next
= target
->event_action
;
3818 target
->event_action
= teap
;
3820 Jim_SetEmptyResult(goi
->interp
);
3824 Jim_SetEmptyResult(goi
->interp
);
3826 Jim_SetResult(goi
->interp
, Jim_DuplicateObj(goi
->interp
, teap
->body
));
3832 case TCFG_WORK_AREA_VIRT
:
3833 if (goi
->isconfigure
) {
3834 target_free_all_working_areas(target
);
3835 e
= Jim_GetOpt_Wide(goi
, &w
);
3838 target
->working_area_virt
= w
;
3839 target
->working_area_virt_spec
= true;
3844 Jim_SetResult(goi
->interp
, Jim_NewIntObj(goi
->interp
, target
->working_area_virt
));
3848 case TCFG_WORK_AREA_PHYS
:
3849 if (goi
->isconfigure
) {
3850 target_free_all_working_areas(target
);
3851 e
= Jim_GetOpt_Wide(goi
, &w
);
3854 target
->working_area_phys
= w
;
3855 target
->working_area_phys_spec
= true;
3860 Jim_SetResult(goi
->interp
, Jim_NewIntObj(goi
->interp
, target
->working_area_phys
));
3864 case TCFG_WORK_AREA_SIZE
:
3865 if (goi
->isconfigure
) {
3866 target_free_all_working_areas(target
);
3867 e
= Jim_GetOpt_Wide(goi
, &w
);
3870 target
->working_area_size
= w
;
3875 Jim_SetResult(goi
->interp
, Jim_NewIntObj(goi
->interp
, target
->working_area_size
));
3879 case TCFG_WORK_AREA_BACKUP
:
3880 if (goi
->isconfigure
) {
3881 target_free_all_working_areas(target
);
3882 e
= Jim_GetOpt_Wide(goi
, &w
);
3885 /* make this exactly 1 or 0 */
3886 target
->backup_working_area
= (!!w
);
3891 Jim_SetResult(goi
->interp
, Jim_NewIntObj(goi
->interp
, target
->backup_working_area
));
3892 /* loop for more e*/
3897 if (goi
->isconfigure
) {
3898 e
= Jim_GetOpt_Nvp(goi
, nvp_target_endian
, &n
);
3900 Jim_GetOpt_NvpUnknown(goi
, nvp_target_endian
, 1);
3903 target
->endianness
= n
->value
;
3908 n
= Jim_Nvp_value2name_simple(nvp_target_endian
, target
->endianness
);
3909 if (n
->name
== NULL
) {
3910 target
->endianness
= TARGET_LITTLE_ENDIAN
;
3911 n
= Jim_Nvp_value2name_simple(nvp_target_endian
, target
->endianness
);
3913 Jim_SetResultString(goi
->interp
, n
->name
, -1);
3918 if (goi
->isconfigure
) {
3919 if (goi
->argc
< 1) {
3920 Jim_SetResultFormatted(goi
->interp
,
3925 if (target
->variant
)
3926 free((void *)(target
->variant
));
3927 e
= Jim_GetOpt_String(goi
, &cp
, NULL
);
3930 target
->variant
= strdup(cp
);
3935 Jim_SetResultString(goi
->interp
, target
->variant
, -1);
3940 if (goi
->isconfigure
) {
3941 e
= Jim_GetOpt_Wide(goi
, &w
);
3944 target
->coreid
= (int32_t)w
;
3949 Jim_SetResult(goi
->interp
, Jim_NewIntObj(goi
->interp
, target
->working_area_size
));
3953 case TCFG_CHAIN_POSITION
:
3954 if (goi
->isconfigure
) {
3956 struct jtag_tap
*tap
;
3957 target_free_all_working_areas(target
);
3958 e
= Jim_GetOpt_Obj(goi
, &o_t
);
3961 tap
= jtag_tap_by_jim_obj(goi
->interp
, o_t
);
3964 /* make this exactly 1 or 0 */
3970 Jim_SetResultString(goi
->interp
, target
->tap
->dotted_name
, -1);
3971 /* loop for more e*/
3974 if (goi
->isconfigure
) {
3975 e
= Jim_GetOpt_Wide(goi
, &w
);
3978 target
->dbgbase
= (uint32_t)w
;
3979 target
->dbgbase_set
= true;
3984 Jim_SetResult(goi
->interp
, Jim_NewIntObj(goi
->interp
, target
->dbgbase
));
3991 int result
= rtos_create(goi
, target
);
3992 if (result
!= JIM_OK
)
3998 } /* while (goi->argc) */
4001 /* done - we return */
4005 static int jim_target_configure(Jim_Interp
*interp
, int argc
, Jim_Obj
* const *argv
)
4009 Jim_GetOpt_Setup(&goi
, interp
, argc
- 1, argv
+ 1);
4010 goi
.isconfigure
= !strcmp(Jim_GetString(argv
[0], NULL
), "configure");
4011 int need_args
= 1 + goi
.isconfigure
;
4012 if (goi
.argc
< need_args
) {
4013 Jim_WrongNumArgs(goi
.interp
, goi
.argc
, goi
.argv
,
4015 ? "missing: -option VALUE ..."
4016 : "missing: -option ...");
4019 struct target
*target
= Jim_CmdPrivData(goi
.interp
);
4020 return target_configure(&goi
, target
);
4023 static int jim_target_mw(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4025 const char *cmd_name
= Jim_GetString(argv
[0], NULL
);
4028 Jim_GetOpt_Setup(&goi
, interp
, argc
- 1, argv
+ 1);
4030 if (goi
.argc
< 2 || goi
.argc
> 4) {
4031 Jim_SetResultFormatted(goi
.interp
,
4032 "usage: %s [phys] <address> <data> [<count>]", cmd_name
);
4037 fn
= target_write_memory_fast
;
4040 if (strcmp(Jim_GetString(argv
[1], NULL
), "phys") == 0) {
4042 struct Jim_Obj
*obj
;
4043 e
= Jim_GetOpt_Obj(&goi
, &obj
);
4047 fn
= target_write_phys_memory
;
4051 e
= Jim_GetOpt_Wide(&goi
, &a
);
4056 e
= Jim_GetOpt_Wide(&goi
, &b
);
4061 if (goi
.argc
== 1) {
4062 e
= Jim_GetOpt_Wide(&goi
, &c
);
4067 /* all args must be consumed */
4071 struct target
*target
= Jim_CmdPrivData(goi
.interp
);
4073 if (strcasecmp(cmd_name
, "mww") == 0)
4075 else if (strcasecmp(cmd_name
, "mwh") == 0)
4077 else if (strcasecmp(cmd_name
, "mwb") == 0)
4080 LOG_ERROR("command '%s' unknown: ", cmd_name
);
4084 return (target_fill_mem(target
, a
, fn
, data_size
, b
, c
) == ERROR_OK
) ? JIM_OK
: JIM_ERR
;
4087 static int jim_target_md(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4089 const char *cmd_name
= Jim_GetString(argv
[0], NULL
);
4092 Jim_GetOpt_Setup(&goi
, interp
, argc
- 1, argv
+ 1);
4094 if ((goi
.argc
< 1) || (goi
.argc
> 3)) {
4095 Jim_SetResultFormatted(goi
.interp
,
4096 "usage: %s [phys] <address> [<count>]", cmd_name
);
4100 int (*fn
)(struct target
*target
,
4101 uint32_t address
, uint32_t size
, uint32_t count
, uint8_t *buffer
);
4102 fn
= target_read_memory
;
4105 if (strcmp(Jim_GetString(argv
[1], NULL
), "phys") == 0) {
4107 struct Jim_Obj
*obj
;
4108 e
= Jim_GetOpt_Obj(&goi
, &obj
);
4112 fn
= target_read_phys_memory
;
4116 e
= Jim_GetOpt_Wide(&goi
, &a
);
4120 if (goi
.argc
== 1) {
4121 e
= Jim_GetOpt_Wide(&goi
, &c
);
4127 /* all args must be consumed */
4131 jim_wide b
= 1; /* shut up gcc */
4132 if (strcasecmp(cmd_name
, "mdw") == 0)
4134 else if (strcasecmp(cmd_name
, "mdh") == 0)
4136 else if (strcasecmp(cmd_name
, "mdb") == 0)
4139 LOG_ERROR("command '%s' unknown: ", cmd_name
);
4143 /* convert count to "bytes" */
4146 struct target
*target
= Jim_CmdPrivData(goi
.interp
);
4147 uint8_t target_buf
[32];
4153 e
= fn(target
, a
, b
, y
/ b
, target_buf
);
4154 if (e
!= ERROR_OK
) {
4156 snprintf(tmp
, sizeof(tmp
), "%08lx", (long)a
);
4157 Jim_SetResultFormatted(interp
, "error reading target @ 0x%s", tmp
);
4161 command_print(NULL
, "0x%08x ", (int)(a
));
4164 for (x
= 0; x
< 16 && x
< y
; x
+= 4) {
4165 z
= target_buffer_get_u32(target
, &(target_buf
[x
]));
4166 command_print(NULL
, "%08x ", (int)(z
));
4168 for (; (x
< 16) ; x
+= 4)
4169 command_print(NULL
, " ");
4172 for (x
= 0; x
< 16 && x
< y
; x
+= 2) {
4173 z
= target_buffer_get_u16(target
, &(target_buf
[x
]));
4174 command_print(NULL
, "%04x ", (int)(z
));
4176 for (; (x
< 16) ; x
+= 2)
4177 command_print(NULL
, " ");
4181 for (x
= 0 ; (x
< 16) && (x
< y
) ; x
+= 1) {
4182 z
= target_buffer_get_u8(target
, &(target_buf
[x
]));
4183 command_print(NULL
, "%02x ", (int)(z
));
4185 for (; (x
< 16) ; x
+= 1)
4186 command_print(NULL
, " ");
4189 /* ascii-ify the bytes */
4190 for (x
= 0 ; x
< y
; x
++) {
4191 if ((target_buf
[x
] >= 0x20) &&
4192 (target_buf
[x
] <= 0x7e)) {
4196 target_buf
[x
] = '.';
4201 target_buf
[x
] = ' ';
4206 /* print - with a newline */
4207 command_print(NULL
, "%s\n", target_buf
);
4215 static int jim_target_mem2array(Jim_Interp
*interp
,
4216 int argc
, Jim_Obj
*const *argv
)
4218 struct target
*target
= Jim_CmdPrivData(interp
);
4219 return target_mem2array(interp
, target
, argc
- 1, argv
+ 1);
4222 static int jim_target_array2mem(Jim_Interp
*interp
,
4223 int argc
, Jim_Obj
*const *argv
)
4225 struct target
*target
= Jim_CmdPrivData(interp
);
4226 return target_array2mem(interp
, target
, argc
- 1, argv
+ 1);
4229 static int jim_target_tap_disabled(Jim_Interp
*interp
)
4231 Jim_SetResultFormatted(interp
, "[TAP is disabled]");
4235 static int jim_target_examine(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4238 Jim_WrongNumArgs(interp
, 1, argv
, "[no parameters]");
4241 struct target
*target
= Jim_CmdPrivData(interp
);
4242 if (!target
->tap
->enabled
)
4243 return jim_target_tap_disabled(interp
);
4245 int e
= target
->type
->examine(target
);
4251 static int jim_target_halt_gdb(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4254 Jim_WrongNumArgs(interp
, 1, argv
, "[no parameters]");
4257 struct target
*target
= Jim_CmdPrivData(interp
);
4259 if (target_call_event_callbacks(target
, TARGET_EVENT_GDB_HALT
) != ERROR_OK
)
4265 static int jim_target_poll(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4268 Jim_WrongNumArgs(interp
, 1, argv
, "[no parameters]");
4271 struct target
*target
= Jim_CmdPrivData(interp
);
4272 if (!target
->tap
->enabled
)
4273 return jim_target_tap_disabled(interp
);
4276 if (!(target_was_examined(target
)))
4277 e
= ERROR_TARGET_NOT_EXAMINED
;
4279 e
= target
->type
->poll(target
);
4285 static int jim_target_reset(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4288 Jim_GetOpt_Setup(&goi
, interp
, argc
- 1, argv
+ 1);
4290 if (goi
.argc
!= 2) {
4291 Jim_WrongNumArgs(interp
, 0, argv
,
4292 "([tT]|[fF]|assert|deassert) BOOL");
4297 int e
= Jim_GetOpt_Nvp(&goi
, nvp_assert
, &n
);
4299 Jim_GetOpt_NvpUnknown(&goi
, nvp_assert
, 1);
4302 /* the halt or not param */
4304 e
= Jim_GetOpt_Wide(&goi
, &a
);
4308 struct target
*target
= Jim_CmdPrivData(goi
.interp
);
4309 if (!target
->tap
->enabled
)
4310 return jim_target_tap_disabled(interp
);
4311 if (!(target_was_examined(target
))) {
4312 LOG_ERROR("Target not examined yet");
4313 return ERROR_TARGET_NOT_EXAMINED
;
4315 if (!target
->type
->assert_reset
|| !target
->type
->deassert_reset
) {
4316 Jim_SetResultFormatted(interp
,
4317 "No target-specific reset for %s",
4318 target_name(target
));
4321 /* determine if we should halt or not. */
4322 target
->reset_halt
= !!a
;
4323 /* When this happens - all workareas are invalid. */
4324 target_free_all_working_areas_restore(target
, 0);
4327 if (n
->value
== NVP_ASSERT
)
4328 e
= target
->type
->assert_reset(target
);
4330 e
= target
->type
->deassert_reset(target
);
4331 return (e
== ERROR_OK
) ? JIM_OK
: JIM_ERR
;
4334 static int jim_target_halt(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4337 Jim_WrongNumArgs(interp
, 1, argv
, "[no parameters]");
4340 struct target
*target
= Jim_CmdPrivData(interp
);
4341 if (!target
->tap
->enabled
)
4342 return jim_target_tap_disabled(interp
);
4343 int e
= target
->type
->halt(target
);
4344 return (e
== ERROR_OK
) ? JIM_OK
: JIM_ERR
;
4347 static int jim_target_wait_state(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4350 Jim_GetOpt_Setup(&goi
, interp
, argc
- 1, argv
+ 1);
4352 /* params: <name> statename timeoutmsecs */
4353 if (goi
.argc
!= 2) {
4354 const char *cmd_name
= Jim_GetString(argv
[0], NULL
);
4355 Jim_SetResultFormatted(goi
.interp
,
4356 "%s <state_name> <timeout_in_msec>", cmd_name
);
4361 int e
= Jim_GetOpt_Nvp(&goi
, nvp_target_state
, &n
);
4363 Jim_GetOpt_NvpUnknown(&goi
, nvp_target_state
, 1);
4367 e
= Jim_GetOpt_Wide(&goi
, &a
);
4370 struct target
*target
= Jim_CmdPrivData(interp
);
4371 if (!target
->tap
->enabled
)
4372 return jim_target_tap_disabled(interp
);
4374 e
= target_wait_state(target
, n
->value
, a
);
4375 if (e
!= ERROR_OK
) {
4376 Jim_Obj
*eObj
= Jim_NewIntObj(interp
, e
);
4377 Jim_SetResultFormatted(goi
.interp
,
4378 "target: %s wait %s fails (%#s) %s",
4379 target_name(target
), n
->name
,
4380 eObj
, target_strerror_safe(e
));
4381 Jim_FreeNewObj(interp
, eObj
);
4386 /* List for human, Events defined for this target.
4387 * scripts/programs should use 'name cget -event NAME'
4389 static int jim_target_event_list(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4391 struct command_context
*cmd_ctx
= current_command_context(interp
);
4392 assert(cmd_ctx
!= NULL
);
4394 struct target
*target
= Jim_CmdPrivData(interp
);
4395 struct target_event_action
*teap
= target
->event_action
;
4396 command_print(cmd_ctx
, "Event actions for target (%d) %s\n",
4397 target
->target_number
,
4398 target_name(target
));
4399 command_print(cmd_ctx
, "%-25s | Body", "Event");
4400 command_print(cmd_ctx
, "------------------------- | "
4401 "----------------------------------------");
4403 Jim_Nvp
*opt
= Jim_Nvp_value2name_simple(nvp_target_event
, teap
->event
);
4404 command_print(cmd_ctx
, "%-25s | %s",
4405 opt
->name
, Jim_GetString(teap
->body
, NULL
));
4408 command_print(cmd_ctx
, "***END***");
4411 static int jim_target_current_state(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4414 Jim_WrongNumArgs(interp
, 1, argv
, "[no parameters]");
4417 struct target
*target
= Jim_CmdPrivData(interp
);
4418 Jim_SetResultString(interp
, target_state_name(target
), -1);
4421 static int jim_target_invoke_event(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4424 Jim_GetOpt_Setup(&goi
, interp
, argc
- 1, argv
+ 1);
4425 if (goi
.argc
!= 1) {
4426 const char *cmd_name
= Jim_GetString(argv
[0], NULL
);
4427 Jim_SetResultFormatted(goi
.interp
, "%s <eventname>", cmd_name
);
4431 int e
= Jim_GetOpt_Nvp(&goi
, nvp_target_event
, &n
);
4433 Jim_GetOpt_NvpUnknown(&goi
, nvp_target_event
, 1);
4436 struct target
*target
= Jim_CmdPrivData(interp
);
4437 target_handle_event(target
, n
->value
);
4441 static const struct command_registration target_instance_command_handlers
[] = {
4443 .name
= "configure",
4444 .mode
= COMMAND_CONFIG
,
4445 .jim_handler
= jim_target_configure
,
4446 .help
= "configure a new target for use",
4447 .usage
= "[target_attribute ...]",
4451 .mode
= COMMAND_ANY
,
4452 .jim_handler
= jim_target_configure
,
4453 .help
= "returns the specified target attribute",
4454 .usage
= "target_attribute",
4458 .mode
= COMMAND_EXEC
,
4459 .jim_handler
= jim_target_mw
,
4460 .help
= "Write 32-bit word(s) to target memory",
4461 .usage
= "address data [count]",
4465 .mode
= COMMAND_EXEC
,
4466 .jim_handler
= jim_target_mw
,
4467 .help
= "Write 16-bit half-word(s) to target memory",
4468 .usage
= "address data [count]",
4472 .mode
= COMMAND_EXEC
,
4473 .jim_handler
= jim_target_mw
,
4474 .help
= "Write byte(s) to target memory",
4475 .usage
= "address data [count]",
4479 .mode
= COMMAND_EXEC
,
4480 .jim_handler
= jim_target_md
,
4481 .help
= "Display target memory as 32-bit words",
4482 .usage
= "address [count]",
4486 .mode
= COMMAND_EXEC
,
4487 .jim_handler
= jim_target_md
,
4488 .help
= "Display target memory as 16-bit half-words",
4489 .usage
= "address [count]",
4493 .mode
= COMMAND_EXEC
,
4494 .jim_handler
= jim_target_md
,
4495 .help
= "Display target memory as 8-bit bytes",
4496 .usage
= "address [count]",
4499 .name
= "array2mem",
4500 .mode
= COMMAND_EXEC
,
4501 .jim_handler
= jim_target_array2mem
,
4502 .help
= "Writes Tcl array of 8/16/32 bit numbers "
4504 .usage
= "arrayname bitwidth address count",
4507 .name
= "mem2array",
4508 .mode
= COMMAND_EXEC
,
4509 .jim_handler
= jim_target_mem2array
,
4510 .help
= "Loads Tcl array of 8/16/32 bit numbers "
4511 "from target memory",
4512 .usage
= "arrayname bitwidth address count",
4515 .name
= "eventlist",
4516 .mode
= COMMAND_EXEC
,
4517 .jim_handler
= jim_target_event_list
,
4518 .help
= "displays a table of events defined for this target",
4522 .mode
= COMMAND_EXEC
,
4523 .jim_handler
= jim_target_current_state
,
4524 .help
= "displays the current state of this target",
4527 .name
= "arp_examine",
4528 .mode
= COMMAND_EXEC
,
4529 .jim_handler
= jim_target_examine
,
4530 .help
= "used internally for reset processing",
4533 .name
= "arp_halt_gdb",
4534 .mode
= COMMAND_EXEC
,
4535 .jim_handler
= jim_target_halt_gdb
,
4536 .help
= "used internally for reset processing to halt GDB",
4540 .mode
= COMMAND_EXEC
,
4541 .jim_handler
= jim_target_poll
,
4542 .help
= "used internally for reset processing",
4545 .name
= "arp_reset",
4546 .mode
= COMMAND_EXEC
,
4547 .jim_handler
= jim_target_reset
,
4548 .help
= "used internally for reset processing",
4552 .mode
= COMMAND_EXEC
,
4553 .jim_handler
= jim_target_halt
,
4554 .help
= "used internally for reset processing",
4557 .name
= "arp_waitstate",
4558 .mode
= COMMAND_EXEC
,
4559 .jim_handler
= jim_target_wait_state
,
4560 .help
= "used internally for reset processing",
4563 .name
= "invoke-event",
4564 .mode
= COMMAND_EXEC
,
4565 .jim_handler
= jim_target_invoke_event
,
4566 .help
= "invoke handler for specified event",
4567 .usage
= "event_name",
4569 COMMAND_REGISTRATION_DONE
4572 static int target_create(Jim_GetOptInfo
*goi
)
4580 struct target
*target
;
4581 struct command_context
*cmd_ctx
;
4583 cmd_ctx
= current_command_context(goi
->interp
);
4584 assert(cmd_ctx
!= NULL
);
4586 if (goi
->argc
< 3) {
4587 Jim_WrongNumArgs(goi
->interp
, 1, goi
->argv
, "?name? ?type? ..options...");
4592 Jim_GetOpt_Obj(goi
, &new_cmd
);
4593 /* does this command exist? */
4594 cmd
= Jim_GetCommand(goi
->interp
, new_cmd
, JIM_ERRMSG
);
4596 cp
= Jim_GetString(new_cmd
, NULL
);
4597 Jim_SetResultFormatted(goi
->interp
, "Command/target: %s Exists", cp
);
4602 e
= Jim_GetOpt_String(goi
, &cp2
, NULL
);
4606 /* now does target type exist */
4607 for (x
= 0 ; target_types
[x
] ; x
++) {
4608 if (0 == strcmp(cp
, target_types
[x
]->name
)) {
4613 if (target_types
[x
] == NULL
) {
4614 Jim_SetResultFormatted(goi
->interp
, "Unknown target type %s, try one of ", cp
);
4615 for (x
= 0 ; target_types
[x
] ; x
++) {
4616 if (target_types
[x
+ 1]) {
4617 Jim_AppendStrings(goi
->interp
,
4618 Jim_GetResult(goi
->interp
),
4619 target_types
[x
]->name
,
4622 Jim_AppendStrings(goi
->interp
,
4623 Jim_GetResult(goi
->interp
),
4625 target_types
[x
]->name
, NULL
);
4632 target
= calloc(1, sizeof(struct target
));
4633 /* set target number */
4634 target
->target_number
= new_target_number();
4636 /* allocate memory for each unique target type */
4637 target
->type
= (struct target_type
*)calloc(1, sizeof(struct target_type
));
4639 memcpy(target
->type
, target_types
[x
], sizeof(struct target_type
));
4641 /* will be set by "-endian" */
4642 target
->endianness
= TARGET_ENDIAN_UNKNOWN
;
4644 /* default to first core, override with -coreid */
4647 target
->working_area
= 0x0;
4648 target
->working_area_size
= 0x0;
4649 target
->working_areas
= NULL
;
4650 target
->backup_working_area
= 0;
4652 target
->state
= TARGET_UNKNOWN
;
4653 target
->debug_reason
= DBG_REASON_UNDEFINED
;
4654 target
->reg_cache
= NULL
;
4655 target
->breakpoints
= NULL
;
4656 target
->watchpoints
= NULL
;
4657 target
->next
= NULL
;
4658 target
->arch_info
= NULL
;
4660 target
->display
= 1;
4662 target
->halt_issued
= false;
4664 /* initialize trace information */
4665 target
->trace_info
= malloc(sizeof(struct trace
));
4666 target
->trace_info
->num_trace_points
= 0;
4667 target
->trace_info
->trace_points_size
= 0;
4668 target
->trace_info
->trace_points
= NULL
;
4669 target
->trace_info
->trace_history_size
= 0;
4670 target
->trace_info
->trace_history
= NULL
;
4671 target
->trace_info
->trace_history_pos
= 0;
4672 target
->trace_info
->trace_history_overflowed
= 0;
4674 target
->dbgmsg
= NULL
;
4675 target
->dbg_msg_enabled
= 0;
4677 target
->endianness
= TARGET_ENDIAN_UNKNOWN
;
4679 target
->rtos
= NULL
;
4680 target
->rtos_auto_detect
= false;
4682 /* Do the rest as "configure" options */
4683 goi
->isconfigure
= 1;
4684 e
= target_configure(goi
, target
);
4686 if (target
->tap
== NULL
) {
4687 Jim_SetResultString(goi
->interp
, "-chain-position required when creating target", -1);
4697 if (target
->endianness
== TARGET_ENDIAN_UNKNOWN
) {
4698 /* default endian to little if not specified */
4699 target
->endianness
= TARGET_LITTLE_ENDIAN
;
4702 /* incase variant is not set */
4703 if (!target
->variant
)
4704 target
->variant
= strdup("");
4706 cp
= Jim_GetString(new_cmd
, NULL
);
4707 target
->cmd_name
= strdup(cp
);
4709 /* create the target specific commands */
4710 if (target
->type
->commands
) {
4711 e
= register_commands(cmd_ctx
, NULL
, target
->type
->commands
);
4713 LOG_ERROR("unable to register '%s' commands", cp
);
4715 if (target
->type
->target_create
)
4716 (*(target
->type
->target_create
))(target
, goi
->interp
);
4718 /* append to end of list */
4720 struct target
**tpp
;
4721 tpp
= &(all_targets
);
4723 tpp
= &((*tpp
)->next
);
4727 /* now - create the new target name command */
4728 const const struct command_registration target_subcommands
[] = {
4730 .chain
= target_instance_command_handlers
,
4733 .chain
= target
->type
->commands
,
4735 COMMAND_REGISTRATION_DONE
4737 const const struct command_registration target_commands
[] = {
4740 .mode
= COMMAND_ANY
,
4741 .help
= "target command group",
4743 .chain
= target_subcommands
,
4745 COMMAND_REGISTRATION_DONE
4747 e
= register_commands(cmd_ctx
, NULL
, target_commands
);
4751 struct command
*c
= command_find_in_context(cmd_ctx
, cp
);
4753 command_set_handler_data(c
, target
);
4755 return (ERROR_OK
== e
) ? JIM_OK
: JIM_ERR
;
4758 static int jim_target_current(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4761 Jim_WrongNumArgs(interp
, 1, argv
, "Too many parameters");
4764 struct command_context
*cmd_ctx
= current_command_context(interp
);
4765 assert(cmd_ctx
!= NULL
);
4767 Jim_SetResultString(interp
, get_current_target(cmd_ctx
)->cmd_name
, -1);
4771 static int jim_target_types(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4774 Jim_WrongNumArgs(interp
, 1, argv
, "Too many parameters");
4777 Jim_SetResult(interp
, Jim_NewListObj(interp
, NULL
, 0));
4778 for (unsigned x
= 0; NULL
!= target_types
[x
]; x
++) {
4779 Jim_ListAppendElement(interp
, Jim_GetResult(interp
),
4780 Jim_NewStringObj(interp
, target_types
[x
]->name
, -1));
4785 static int jim_target_names(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4788 Jim_WrongNumArgs(interp
, 1, argv
, "Too many parameters");
4791 Jim_SetResult(interp
, Jim_NewListObj(interp
, NULL
, 0));
4792 struct target
*target
= all_targets
;
4794 Jim_ListAppendElement(interp
, Jim_GetResult(interp
),
4795 Jim_NewStringObj(interp
, target_name(target
), -1));
4796 target
= target
->next
;
4801 static int jim_target_smp(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4804 const char *targetname
;
4806 struct target
*target
= (struct target
*) NULL
;
4807 struct target_list
*head
, *curr
, *new;
4808 curr
= (struct target_list
*) NULL
;
4809 head
= (struct target_list
*) NULL
;
4810 new = (struct target_list
*) NULL
;
4813 LOG_DEBUG("%d", argc
);
4814 /* argv[1] = target to associate in smp
4815 * argv[2] = target to assoicate in smp
4819 for (i
= 1; i
< argc
; i
++) {
4821 targetname
= Jim_GetString(argv
[i
], &len
);
4822 target
= get_target(targetname
);
4823 LOG_DEBUG("%s ", targetname
);
4825 new = malloc(sizeof(struct target_list
));
4826 new->target
= target
;
4827 new->next
= (struct target_list
*)NULL
;
4828 if (head
== (struct target_list
*)NULL
) {
4837 /* now parse the list of cpu and put the target in smp mode*/
4840 while (curr
!= (struct target_list
*)NULL
) {
4841 target
= curr
->target
;
4843 target
->head
= head
;
4847 retval
= rtos_smp_init(head
->target
);
4852 static int jim_target_create(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4855 Jim_GetOpt_Setup(&goi
, interp
, argc
- 1, argv
+ 1);
4857 Jim_WrongNumArgs(goi
.interp
, goi
.argc
, goi
.argv
,
4858 "<name> <target_type> [<target_options> ...]");
4861 return target_create(&goi
);
4864 static int jim_target_number(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4867 Jim_GetOpt_Setup(&goi
, interp
, argc
- 1, argv
+ 1);
4869 /* It's OK to remove this mechanism sometime after August 2010 or so */
4870 LOG_WARNING("don't use numbers as target identifiers; use names");
4871 if (goi
.argc
!= 1) {
4872 Jim_SetResultFormatted(goi
.interp
, "usage: target number <number>");
4876 int e
= Jim_GetOpt_Wide(&goi
, &w
);
4880 struct target
*target
;
4881 for (target
= all_targets
; NULL
!= target
; target
= target
->next
) {
4882 if (target
->target_number
!= w
)
4885 Jim_SetResultString(goi
.interp
, target_name(target
), -1);
4889 Jim_Obj
*wObj
= Jim_NewIntObj(goi
.interp
, w
);
4890 Jim_SetResultFormatted(goi
.interp
,
4891 "Target: number %#s does not exist", wObj
);
4892 Jim_FreeNewObj(interp
, wObj
);
4897 static int jim_target_count(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4900 Jim_WrongNumArgs(interp
, 1, argv
, "<no parameters>");
4904 struct target
*target
= all_targets
;
4905 while (NULL
!= target
) {
4906 target
= target
->next
;
4909 Jim_SetResult(interp
, Jim_NewIntObj(interp
, count
));
4913 static const struct command_registration target_subcommand_handlers
[] = {
4916 .mode
= COMMAND_CONFIG
,
4917 .handler
= handle_target_init_command
,
4918 .help
= "initialize targets",
4922 /* REVISIT this should be COMMAND_CONFIG ... */
4923 .mode
= COMMAND_ANY
,
4924 .jim_handler
= jim_target_create
,
4925 .usage
= "name type '-chain-position' name [options ...]",
4926 .help
= "Creates and selects a new target",
4930 .mode
= COMMAND_ANY
,
4931 .jim_handler
= jim_target_current
,
4932 .help
= "Returns the currently selected target",
4936 .mode
= COMMAND_ANY
,
4937 .jim_handler
= jim_target_types
,
4938 .help
= "Returns the available target types as "
4939 "a list of strings",
4943 .mode
= COMMAND_ANY
,
4944 .jim_handler
= jim_target_names
,
4945 .help
= "Returns the names of all targets as a list of strings",
4949 .mode
= COMMAND_ANY
,
4950 .jim_handler
= jim_target_number
,
4952 .help
= "Returns the name of the numbered target "
4957 .mode
= COMMAND_ANY
,
4958 .jim_handler
= jim_target_count
,
4959 .help
= "Returns the number of targets as an integer "
4964 .mode
= COMMAND_ANY
,
4965 .jim_handler
= jim_target_smp
,
4966 .usage
= "targetname1 targetname2 ...",
4967 .help
= "gather several target in a smp list"
4970 COMMAND_REGISTRATION_DONE
4980 static int fastload_num
;
4981 static struct FastLoad
*fastload
;
4983 static void free_fastload(void)
4985 if (fastload
!= NULL
) {
4987 for (i
= 0; i
< fastload_num
; i
++) {
4988 if (fastload
[i
].data
)
4989 free(fastload
[i
].data
);
4996 COMMAND_HANDLER(handle_fast_load_image_command
)
5000 uint32_t image_size
;
5001 uint32_t min_address
= 0;
5002 uint32_t max_address
= 0xffffffff;
5007 int retval
= CALL_COMMAND_HANDLER(parse_load_image_command_CMD_ARGV
,
5008 &image
, &min_address
, &max_address
);
5009 if (ERROR_OK
!= retval
)
5012 struct duration bench
;
5013 duration_start(&bench
);
5015 retval
= image_open(&image
, CMD_ARGV
[0], (CMD_ARGC
>= 3) ? CMD_ARGV
[2] : NULL
);
5016 if (retval
!= ERROR_OK
)
5021 fastload_num
= image
.num_sections
;
5022 fastload
= (struct FastLoad
*)malloc(sizeof(struct FastLoad
)*image
.num_sections
);
5023 if (fastload
== NULL
) {
5024 command_print(CMD_CTX
, "out of memory");
5025 image_close(&image
);
5028 memset(fastload
, 0, sizeof(struct FastLoad
)*image
.num_sections
);
5029 for (i
= 0; i
< image
.num_sections
; i
++) {
5030 buffer
= malloc(image
.sections
[i
].size
);
5031 if (buffer
== NULL
) {
5032 command_print(CMD_CTX
, "error allocating buffer for section (%d bytes)",
5033 (int)(image
.sections
[i
].size
));
5034 retval
= ERROR_FAIL
;
5038 retval
= image_read_section(&image
, i
, 0x0, image
.sections
[i
].size
, buffer
, &buf_cnt
);
5039 if (retval
!= ERROR_OK
) {
5044 uint32_t offset
= 0;
5045 uint32_t length
= buf_cnt
;
5047 /* DANGER!!! beware of unsigned comparision here!!! */
5049 if ((image
.sections
[i
].base_address
+ buf_cnt
>= min_address
) &&
5050 (image
.sections
[i
].base_address
< max_address
)) {
5051 if (image
.sections
[i
].base_address
< min_address
) {
5052 /* clip addresses below */
5053 offset
+= min_address
-image
.sections
[i
].base_address
;
5057 if (image
.sections
[i
].base_address
+ buf_cnt
> max_address
)
5058 length
-= (image
.sections
[i
].base_address
+ buf_cnt
)-max_address
;
5060 fastload
[i
].address
= image
.sections
[i
].base_address
+ offset
;
5061 fastload
[i
].data
= malloc(length
);
5062 if (fastload
[i
].data
== NULL
) {
5064 command_print(CMD_CTX
, "error allocating buffer for section (%d bytes)",
5066 retval
= ERROR_FAIL
;
5069 memcpy(fastload
[i
].data
, buffer
+ offset
, length
);
5070 fastload
[i
].length
= length
;
5072 image_size
+= length
;
5073 command_print(CMD_CTX
, "%u bytes written at address 0x%8.8x",
5074 (unsigned int)length
,
5075 ((unsigned int)(image
.sections
[i
].base_address
+ offset
)));
5081 if ((ERROR_OK
== retval
) && (duration_measure(&bench
) == ERROR_OK
)) {
5082 command_print(CMD_CTX
, "Loaded %" PRIu32
" bytes "
5083 "in %fs (%0.3f KiB/s)", image_size
,
5084 duration_elapsed(&bench
), duration_kbps(&bench
, image_size
));
5086 command_print(CMD_CTX
,
5087 "WARNING: image has not been loaded to target!"
5088 "You can issue a 'fast_load' to finish loading.");
5091 image_close(&image
);
5093 if (retval
!= ERROR_OK
)
5099 COMMAND_HANDLER(handle_fast_load_command
)
5102 return ERROR_COMMAND_SYNTAX_ERROR
;
5103 if (fastload
== NULL
) {
5104 LOG_ERROR("No image in memory");
5108 int ms
= timeval_ms();
5110 int retval
= ERROR_OK
;
5111 for (i
= 0; i
< fastload_num
; i
++) {
5112 struct target
*target
= get_current_target(CMD_CTX
);
5113 command_print(CMD_CTX
, "Write to 0x%08x, length 0x%08x",
5114 (unsigned int)(fastload
[i
].address
),
5115 (unsigned int)(fastload
[i
].length
));
5116 retval
= target_write_buffer(target
, fastload
[i
].address
, fastload
[i
].length
, fastload
[i
].data
);
5117 if (retval
!= ERROR_OK
)
5119 size
+= fastload
[i
].length
;
5121 if (retval
== ERROR_OK
) {
5122 int after
= timeval_ms();
5123 command_print(CMD_CTX
, "Loaded image %f kBytes/s", (float)(size
/1024.0)/((float)(after
-ms
)/1000.0));
5128 static const struct command_registration target_command_handlers
[] = {
5131 .handler
= handle_targets_command
,
5132 .mode
= COMMAND_ANY
,
5133 .help
= "change current default target (one parameter) "
5134 "or prints table of all targets (no parameters)",
5135 .usage
= "[target]",
5139 .mode
= COMMAND_CONFIG
,
5140 .help
= "configure target",
5142 .chain
= target_subcommand_handlers
,
5144 COMMAND_REGISTRATION_DONE
5147 int target_register_commands(struct command_context
*cmd_ctx
)
5149 return register_commands(cmd_ctx
, NULL
, target_command_handlers
);
5152 static bool target_reset_nag
= true;
5154 bool get_target_reset_nag(void)
5156 return target_reset_nag
;
5159 COMMAND_HANDLER(handle_target_reset_nag
)
5161 return CALL_COMMAND_HANDLER(handle_command_parse_bool
,
5162 &target_reset_nag
, "Nag after each reset about options to improve "
5166 COMMAND_HANDLER(handle_ps_command
)
5168 struct target
*target
= get_current_target(CMD_CTX
);
5170 if (target
->state
!= TARGET_HALTED
) {
5171 LOG_INFO("target not halted !!");
5175 if ((target
->rtos
) && (target
->rtos
->type
)
5176 && (target
->rtos
->type
->ps_command
)) {
5177 display
= target
->rtos
->type
->ps_command(target
);
5178 command_print(CMD_CTX
, "%s", display
);
5183 return ERROR_TARGET_FAILURE
;
5187 static const struct command_registration target_exec_command_handlers
[] = {
5189 .name
= "fast_load_image",
5190 .handler
= handle_fast_load_image_command
,
5191 .mode
= COMMAND_ANY
,
5192 .help
= "Load image into server memory for later use by "
5193 "fast_load; primarily for profiling",
5194 .usage
= "filename address ['bin'|'ihex'|'elf'|'s19'] "
5195 "[min_address [max_length]]",
5198 .name
= "fast_load",
5199 .handler
= handle_fast_load_command
,
5200 .mode
= COMMAND_EXEC
,
5201 .help
= "loads active fast load image to current target "
5202 "- mainly for profiling purposes",
5207 .handler
= handle_profile_command
,
5208 .mode
= COMMAND_EXEC
,
5209 .usage
= "seconds filename",
5210 .help
= "profiling samples the CPU PC",
5212 /** @todo don't register virt2phys() unless target supports it */
5214 .name
= "virt2phys",
5215 .handler
= handle_virt2phys_command
,
5216 .mode
= COMMAND_ANY
,
5217 .help
= "translate a virtual address into a physical address",
5218 .usage
= "virtual_address",
5222 .handler
= handle_reg_command
,
5223 .mode
= COMMAND_EXEC
,
5224 .help
= "display or set a register; with no arguments, "
5225 "displays all registers and their values",
5226 .usage
= "[(register_name|register_number) [value]]",
5230 .handler
= handle_poll_command
,
5231 .mode
= COMMAND_EXEC
,
5232 .help
= "poll target state; or reconfigure background polling",
5233 .usage
= "['on'|'off']",
5236 .name
= "wait_halt",
5237 .handler
= handle_wait_halt_command
,
5238 .mode
= COMMAND_EXEC
,
5239 .help
= "wait up to the specified number of milliseconds "
5240 "(default 5) for a previously requested halt",
5241 .usage
= "[milliseconds]",
5245 .handler
= handle_halt_command
,
5246 .mode
= COMMAND_EXEC
,
5247 .help
= "request target to halt, then wait up to the specified"
5248 "number of milliseconds (default 5) for it to complete",
5249 .usage
= "[milliseconds]",
5253 .handler
= handle_resume_command
,
5254 .mode
= COMMAND_EXEC
,
5255 .help
= "resume target execution from current PC or address",
5256 .usage
= "[address]",
5260 .handler
= handle_reset_command
,
5261 .mode
= COMMAND_EXEC
,
5262 .usage
= "[run|halt|init]",
5263 .help
= "Reset all targets into the specified mode."
5264 "Default reset mode is run, if not given.",
5267 .name
= "soft_reset_halt",
5268 .handler
= handle_soft_reset_halt_command
,
5269 .mode
= COMMAND_EXEC
,
5271 .help
= "halt the target and do a soft reset",
5275 .handler
= handle_step_command
,
5276 .mode
= COMMAND_EXEC
,
5277 .help
= "step one instruction from current PC or address",
5278 .usage
= "[address]",
5282 .handler
= handle_md_command
,
5283 .mode
= COMMAND_EXEC
,
5284 .help
= "display memory words",
5285 .usage
= "['phys'] address [count]",
5289 .handler
= handle_md_command
,
5290 .mode
= COMMAND_EXEC
,
5291 .help
= "display memory half-words",
5292 .usage
= "['phys'] address [count]",
5296 .handler
= handle_md_command
,
5297 .mode
= COMMAND_EXEC
,
5298 .help
= "display memory bytes",
5299 .usage
= "['phys'] address [count]",
5303 .handler
= handle_mw_command
,
5304 .mode
= COMMAND_EXEC
,
5305 .help
= "write memory word",
5306 .usage
= "['phys'] address value [count]",
5310 .handler
= handle_mw_command
,
5311 .mode
= COMMAND_EXEC
,
5312 .help
= "write memory half-word",
5313 .usage
= "['phys'] address value [count]",
5317 .handler
= handle_mw_command
,
5318 .mode
= COMMAND_EXEC
,
5319 .help
= "write memory byte",
5320 .usage
= "['phys'] address value [count]",
5324 .handler
= handle_bp_command
,
5325 .mode
= COMMAND_EXEC
,
5326 .help
= "list or set hardware or software breakpoint",
5327 .usage
= "<address> [<asid>]<length> ['hw'|'hw_ctx']",
5331 .handler
= handle_rbp_command
,
5332 .mode
= COMMAND_EXEC
,
5333 .help
= "remove breakpoint",
5338 .handler
= handle_wp_command
,
5339 .mode
= COMMAND_EXEC
,
5340 .help
= "list (no params) or create watchpoints",
5341 .usage
= "[address length [('r'|'w'|'a') value [mask]]]",
5345 .handler
= handle_rwp_command
,
5346 .mode
= COMMAND_EXEC
,
5347 .help
= "remove watchpoint",
5351 .name
= "load_image",
5352 .handler
= handle_load_image_command
,
5353 .mode
= COMMAND_EXEC
,
5354 .usage
= "filename address ['bin'|'ihex'|'elf'|'s19'] "
5355 "[min_address] [max_length]",
5358 .name
= "dump_image",
5359 .handler
= handle_dump_image_command
,
5360 .mode
= COMMAND_EXEC
,
5361 .usage
= "filename address size",
5364 .name
= "verify_image",
5365 .handler
= handle_verify_image_command
,
5366 .mode
= COMMAND_EXEC
,
5367 .usage
= "filename [offset [type]]",
5370 .name
= "test_image",
5371 .handler
= handle_test_image_command
,
5372 .mode
= COMMAND_EXEC
,
5373 .usage
= "filename [offset [type]]",
5376 .name
= "mem2array",
5377 .mode
= COMMAND_EXEC
,
5378 .jim_handler
= jim_mem2array
,
5379 .help
= "read 8/16/32 bit memory and return as a TCL array "
5380 "for script processing",
5381 .usage
= "arrayname bitwidth address count",
5384 .name
= "array2mem",
5385 .mode
= COMMAND_EXEC
,
5386 .jim_handler
= jim_array2mem
,
5387 .help
= "convert a TCL array to memory locations "
5388 "and write the 8/16/32 bit values",
5389 .usage
= "arrayname bitwidth address count",
5392 .name
= "reset_nag",
5393 .handler
= handle_target_reset_nag
,
5394 .mode
= COMMAND_ANY
,
5395 .help
= "Nag after each reset about options that could have been "
5396 "enabled to improve performance. ",
5397 .usage
= "['enable'|'disable']",
5401 .handler
= handle_ps_command
,
5402 .mode
= COMMAND_EXEC
,
5403 .help
= "list all tasks ",
5407 COMMAND_REGISTRATION_DONE
5409 static int target_register_user_commands(struct command_context
*cmd_ctx
)
5411 int retval
= ERROR_OK
;
5412 retval
= target_request_register_commands(cmd_ctx
);
5413 if (retval
!= ERROR_OK
)
5416 retval
= trace_register_commands(cmd_ctx
);
5417 if (retval
!= ERROR_OK
)
5421 return register_commands(cmd_ctx
, NULL
, target_exec_command_handlers
);