1 /***************************************************************************
2 * Copyright (C) 2005 by Dominic Rath *
3 * Dominic.Rath@gmx.de *
5 * Copyright (C) 2007-2009 Ø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 * This program is free software; you can redistribute it and/or modify *
18 * it under the terms of the GNU General Public License as published by *
19 * the Free Software Foundation; either version 2 of the License, or *
20 * (at your option) any later version. *
22 * This program is distributed in the hope that it will be useful, *
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
25 * GNU General Public License for more details. *
27 * You should have received a copy of the GNU General Public License *
28 * along with this program; if not, write to the *
29 * Free Software Foundation, Inc., *
30 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
31 ***************************************************************************/
37 #include "target_type.h"
38 #include "target_request.h"
39 #include "breakpoints.h"
40 #include "time_support.h"
47 static int target_array2mem(Jim_Interp
*interp
, struct target
*target
, int argc
, Jim_Obj
*const *argv
);
48 static int target_mem2array(Jim_Interp
*interp
, struct target
*target
, int argc
, Jim_Obj
*const *argv
);
51 extern struct target_type arm7tdmi_target
;
52 extern struct target_type arm720t_target
;
53 extern struct target_type arm9tdmi_target
;
54 extern struct target_type arm920t_target
;
55 extern struct target_type arm966e_target
;
56 extern struct target_type arm926ejs_target
;
57 extern struct target_type fa526_target
;
58 extern struct target_type feroceon_target
;
59 extern struct target_type dragonite_target
;
60 extern struct target_type xscale_target
;
61 extern struct target_type cortexm3_target
;
62 extern struct target_type cortexa8_target
;
63 extern struct target_type arm11_target
;
64 extern struct target_type mips_m4k_target
;
65 extern struct target_type avr_target
;
66 extern struct target_type testee_target
;
68 struct target_type
*target_types
[] =
89 struct target
*all_targets
= NULL
;
90 struct target_event_callback
*target_event_callbacks
= NULL
;
91 struct target_timer_callback
*target_timer_callbacks
= NULL
;
93 const Jim_Nvp nvp_assert
[] = {
94 { .name
= "assert", NVP_ASSERT
},
95 { .name
= "deassert", NVP_DEASSERT
},
96 { .name
= "T", NVP_ASSERT
},
97 { .name
= "F", NVP_DEASSERT
},
98 { .name
= "t", NVP_ASSERT
},
99 { .name
= "f", NVP_DEASSERT
},
100 { .name
= NULL
, .value
= -1 }
103 const Jim_Nvp nvp_error_target
[] = {
104 { .value
= ERROR_TARGET_INVALID
, .name
= "err-invalid" },
105 { .value
= ERROR_TARGET_INIT_FAILED
, .name
= "err-init-failed" },
106 { .value
= ERROR_TARGET_TIMEOUT
, .name
= "err-timeout" },
107 { .value
= ERROR_TARGET_NOT_HALTED
, .name
= "err-not-halted" },
108 { .value
= ERROR_TARGET_FAILURE
, .name
= "err-failure" },
109 { .value
= ERROR_TARGET_UNALIGNED_ACCESS
, .name
= "err-unaligned-access" },
110 { .value
= ERROR_TARGET_DATA_ABORT
, .name
= "err-data-abort" },
111 { .value
= ERROR_TARGET_RESOURCE_NOT_AVAILABLE
, .name
= "err-resource-not-available" },
112 { .value
= ERROR_TARGET_TRANSLATION_FAULT
, .name
= "err-translation-fault" },
113 { .value
= ERROR_TARGET_NOT_RUNNING
, .name
= "err-not-running" },
114 { .value
= ERROR_TARGET_NOT_EXAMINED
, .name
= "err-not-examined" },
115 { .value
= -1, .name
= NULL
}
118 const char *target_strerror_safe(int err
)
122 n
= Jim_Nvp_value2name_simple(nvp_error_target
, err
);
123 if (n
->name
== NULL
) {
130 static const Jim_Nvp nvp_target_event
[] = {
131 { .value
= TARGET_EVENT_OLD_gdb_program_config
, .name
= "old-gdb_program_config" },
132 { .value
= TARGET_EVENT_OLD_pre_resume
, .name
= "old-pre_resume" },
134 { .value
= TARGET_EVENT_GDB_HALT
, .name
= "gdb-halt" },
135 { .value
= TARGET_EVENT_HALTED
, .name
= "halted" },
136 { .value
= TARGET_EVENT_RESUMED
, .name
= "resumed" },
137 { .value
= TARGET_EVENT_RESUME_START
, .name
= "resume-start" },
138 { .value
= TARGET_EVENT_RESUME_END
, .name
= "resume-end" },
140 { .name
= "gdb-start", .value
= TARGET_EVENT_GDB_START
},
141 { .name
= "gdb-end", .value
= TARGET_EVENT_GDB_END
},
143 /* historical name */
145 { .value
= TARGET_EVENT_RESET_START
, .name
= "reset-start" },
147 { .value
= TARGET_EVENT_RESET_ASSERT_PRE
, .name
= "reset-assert-pre" },
148 { .value
= TARGET_EVENT_RESET_ASSERT
, .name
= "reset-assert" },
149 { .value
= TARGET_EVENT_RESET_ASSERT_POST
, .name
= "reset-assert-post" },
150 { .value
= TARGET_EVENT_RESET_DEASSERT_PRE
, .name
= "reset-deassert-pre" },
151 { .value
= TARGET_EVENT_RESET_DEASSERT_POST
, .name
= "reset-deassert-post" },
152 { .value
= TARGET_EVENT_RESET_HALT_PRE
, .name
= "reset-halt-pre" },
153 { .value
= TARGET_EVENT_RESET_HALT_POST
, .name
= "reset-halt-post" },
154 { .value
= TARGET_EVENT_RESET_WAIT_PRE
, .name
= "reset-wait-pre" },
155 { .value
= TARGET_EVENT_RESET_WAIT_POST
, .name
= "reset-wait-post" },
156 { .value
= TARGET_EVENT_RESET_INIT
, .name
= "reset-init" },
157 { .value
= TARGET_EVENT_RESET_END
, .name
= "reset-end" },
159 { .value
= TARGET_EVENT_EXAMINE_START
, .name
= "examine-start" },
160 { .value
= TARGET_EVENT_EXAMINE_END
, .name
= "examine-end" },
162 { .value
= TARGET_EVENT_DEBUG_HALTED
, .name
= "debug-halted" },
163 { .value
= TARGET_EVENT_DEBUG_RESUMED
, .name
= "debug-resumed" },
165 { .value
= TARGET_EVENT_GDB_ATTACH
, .name
= "gdb-attach" },
166 { .value
= TARGET_EVENT_GDB_DETACH
, .name
= "gdb-detach" },
168 { .value
= TARGET_EVENT_GDB_FLASH_WRITE_START
, .name
= "gdb-flash-write-start" },
169 { .value
= TARGET_EVENT_GDB_FLASH_WRITE_END
, .name
= "gdb-flash-write-end" },
171 { .value
= TARGET_EVENT_GDB_FLASH_ERASE_START
, .name
= "gdb-flash-erase-start" },
172 { .value
= TARGET_EVENT_GDB_FLASH_ERASE_END
, .name
= "gdb-flash-erase-end" },
174 { .value
= TARGET_EVENT_RESUME_START
, .name
= "resume-start" },
175 { .value
= TARGET_EVENT_RESUMED
, .name
= "resume-ok" },
176 { .value
= TARGET_EVENT_RESUME_END
, .name
= "resume-end" },
178 { .name
= NULL
, .value
= -1 }
181 const Jim_Nvp nvp_target_state
[] = {
182 { .name
= "unknown", .value
= TARGET_UNKNOWN
},
183 { .name
= "running", .value
= TARGET_RUNNING
},
184 { .name
= "halted", .value
= TARGET_HALTED
},
185 { .name
= "reset", .value
= TARGET_RESET
},
186 { .name
= "debug-running", .value
= TARGET_DEBUG_RUNNING
},
187 { .name
= NULL
, .value
= -1 },
190 const Jim_Nvp nvp_target_debug_reason
[] = {
191 { .name
= "debug-request" , .value
= DBG_REASON_DBGRQ
},
192 { .name
= "breakpoint" , .value
= DBG_REASON_BREAKPOINT
},
193 { .name
= "watchpoint" , .value
= DBG_REASON_WATCHPOINT
},
194 { .name
= "watchpoint-and-breakpoint", .value
= DBG_REASON_WPTANDBKPT
},
195 { .name
= "single-step" , .value
= DBG_REASON_SINGLESTEP
},
196 { .name
= "target-not-halted" , .value
= DBG_REASON_NOTHALTED
},
197 { .name
= "undefined" , .value
= DBG_REASON_UNDEFINED
},
198 { .name
= NULL
, .value
= -1 },
201 const Jim_Nvp nvp_target_endian
[] = {
202 { .name
= "big", .value
= TARGET_BIG_ENDIAN
},
203 { .name
= "little", .value
= TARGET_LITTLE_ENDIAN
},
204 { .name
= "be", .value
= TARGET_BIG_ENDIAN
},
205 { .name
= "le", .value
= TARGET_LITTLE_ENDIAN
},
206 { .name
= NULL
, .value
= -1 },
209 const Jim_Nvp nvp_reset_modes
[] = {
210 { .name
= "unknown", .value
= RESET_UNKNOWN
},
211 { .name
= "run" , .value
= RESET_RUN
},
212 { .name
= "halt" , .value
= RESET_HALT
},
213 { .name
= "init" , .value
= RESET_INIT
},
214 { .name
= NULL
, .value
= -1 },
218 target_state_name( struct target
*t
)
221 cp
= Jim_Nvp_value2name_simple(nvp_target_state
, t
->state
)->name
;
223 LOG_ERROR("Invalid target state: %d", (int)(t
->state
));
224 cp
= "(*BUG*unknown*BUG*)";
229 /* determine the number of the new target */
230 static int new_target_number(void)
235 /* number is 0 based */
239 if (x
< t
->target_number
) {
240 x
= t
->target_number
;
247 /* read a uint32_t from a buffer in target memory endianness */
248 uint32_t target_buffer_get_u32(struct target
*target
, const uint8_t *buffer
)
250 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
251 return le_to_h_u32(buffer
);
253 return be_to_h_u32(buffer
);
256 /* read a uint16_t from a buffer in target memory endianness */
257 uint16_t target_buffer_get_u16(struct target
*target
, const uint8_t *buffer
)
259 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
260 return le_to_h_u16(buffer
);
262 return be_to_h_u16(buffer
);
265 /* read a uint8_t from a buffer in target memory endianness */
266 uint8_t target_buffer_get_u8(struct target
*target
, const uint8_t *buffer
)
268 return *buffer
& 0x0ff;
271 /* write a uint32_t to a buffer in target memory endianness */
272 void target_buffer_set_u32(struct target
*target
, uint8_t *buffer
, uint32_t value
)
274 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
275 h_u32_to_le(buffer
, value
);
277 h_u32_to_be(buffer
, value
);
280 /* write a uint16_t to a buffer in target memory endianness */
281 void target_buffer_set_u16(struct target
*target
, uint8_t *buffer
, uint16_t value
)
283 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
284 h_u16_to_le(buffer
, value
);
286 h_u16_to_be(buffer
, value
);
289 /* write a uint8_t to a buffer in target memory endianness */
290 void target_buffer_set_u8(struct target
*target
, uint8_t *buffer
, uint8_t value
)
295 /* return a pointer to a configured target; id is name or number */
296 struct target
*get_target(const char *id
)
298 struct target
*target
;
300 /* try as tcltarget name */
301 for (target
= all_targets
; target
; target
= target
->next
) {
302 if (target
->cmd_name
== NULL
)
304 if (strcmp(id
, target
->cmd_name
) == 0)
308 /* It's OK to remove this fallback sometime after August 2010 or so */
310 /* no match, try as number */
312 if (parse_uint(id
, &num
) != ERROR_OK
)
315 for (target
= all_targets
; target
; target
= target
->next
) {
316 if (target
->target_number
== (int)num
) {
317 LOG_WARNING("use '%s' as target identifier, not '%u'",
318 target
->cmd_name
, num
);
326 /* returns a pointer to the n-th configured target */
327 static struct target
*get_target_by_num(int num
)
329 struct target
*target
= all_targets
;
332 if (target
->target_number
== num
) {
335 target
= target
->next
;
341 struct target
* get_current_target(struct command_context
*cmd_ctx
)
343 struct target
*target
= get_target_by_num(cmd_ctx
->current_target
);
347 LOG_ERROR("BUG: current_target out of bounds");
354 int target_poll(struct target
*target
)
358 /* We can't poll until after examine */
359 if (!target_was_examined(target
))
361 /* Fail silently lest we pollute the log */
365 retval
= target
->type
->poll(target
);
366 if (retval
!= ERROR_OK
)
369 if (target
->halt_issued
)
371 if (target
->state
== TARGET_HALTED
)
373 target
->halt_issued
= false;
376 long long t
= timeval_ms() - target
->halt_issued_time
;
379 target
->halt_issued
= false;
380 LOG_INFO("Halt timed out, wake up GDB.");
381 target_call_event_callbacks(target
, TARGET_EVENT_GDB_HALT
);
389 int target_halt(struct target
*target
)
392 /* We can't poll until after examine */
393 if (!target_was_examined(target
))
395 LOG_ERROR("Target not examined yet");
399 retval
= target
->type
->halt(target
);
400 if (retval
!= ERROR_OK
)
403 target
->halt_issued
= true;
404 target
->halt_issued_time
= timeval_ms();
409 int target_resume(struct target
*target
, int current
, uint32_t address
, int handle_breakpoints
, int debug_execution
)
413 /* We can't poll until after examine */
414 if (!target_was_examined(target
))
416 LOG_ERROR("Target not examined yet");
420 /* note that resume *must* be asynchronous. The CPU can halt before we poll. The CPU can
421 * even halt at the current PC as a result of a software breakpoint being inserted by (a bug?)
424 if ((retval
= target
->type
->resume(target
, current
, address
, handle_breakpoints
, debug_execution
)) != ERROR_OK
)
430 int target_process_reset(struct command_context
*cmd_ctx
, enum target_reset_mode reset_mode
)
435 n
= Jim_Nvp_value2name_simple(nvp_reset_modes
, reset_mode
);
436 if (n
->name
== NULL
) {
437 LOG_ERROR("invalid reset mode");
441 /* disable polling during reset to make reset event scripts
442 * more predictable, i.e. dr/irscan & pathmove in events will
443 * not have JTAG operations injected into the middle of a sequence.
445 bool save_poll
= jtag_poll_get_enabled();
447 jtag_poll_set_enabled(false);
449 sprintf(buf
, "ocd_process_reset %s", n
->name
);
450 retval
= Jim_Eval(cmd_ctx
->interp
, buf
);
452 jtag_poll_set_enabled(save_poll
);
454 if (retval
!= JIM_OK
) {
455 Jim_PrintErrorMessage(cmd_ctx
->interp
);
459 /* We want any events to be processed before the prompt */
460 retval
= target_call_timer_callbacks_now();
465 static int identity_virt2phys(struct target
*target
,
466 uint32_t virtual, uint32_t *physical
)
472 static int no_mmu(struct target
*target
, int *enabled
)
478 static int default_examine(struct target
*target
)
480 target_set_examined(target
);
484 int target_examine_one(struct target
*target
)
486 return target
->type
->examine(target
);
489 static int jtag_enable_callback(enum jtag_event event
, void *priv
)
491 struct target
*target
= priv
;
493 if (event
!= JTAG_TAP_EVENT_ENABLE
|| !target
->tap
->enabled
)
496 jtag_unregister_event_callback(jtag_enable_callback
, target
);
497 return target_examine_one(target
);
501 /* Targets that correctly implement init + examine, i.e.
502 * no communication with target during init:
506 int target_examine(void)
508 int retval
= ERROR_OK
;
509 struct target
*target
;
511 for (target
= all_targets
; target
; target
= target
->next
)
513 /* defer examination, but don't skip it */
514 if (!target
->tap
->enabled
) {
515 jtag_register_event_callback(jtag_enable_callback
,
519 if ((retval
= target_examine_one(target
)) != ERROR_OK
)
524 const char *target_type_name(struct target
*target
)
526 return target
->type
->name
;
529 static int target_write_memory_imp(struct target
*target
, uint32_t address
, uint32_t size
, uint32_t count
, uint8_t *buffer
)
531 if (!target_was_examined(target
))
533 LOG_ERROR("Target not examined yet");
536 return target
->type
->write_memory_imp(target
, address
, size
, count
, buffer
);
539 static int target_read_memory_imp(struct target
*target
, uint32_t address
, uint32_t size
, uint32_t count
, uint8_t *buffer
)
541 if (!target_was_examined(target
))
543 LOG_ERROR("Target not examined yet");
546 return target
->type
->read_memory_imp(target
, address
, size
, count
, buffer
);
549 static int target_soft_reset_halt_imp(struct target
*target
)
551 if (!target_was_examined(target
))
553 LOG_ERROR("Target not examined yet");
556 if (!target
->type
->soft_reset_halt_imp
) {
557 LOG_ERROR("Target %s does not support soft_reset_halt",
558 target_name(target
));
561 return target
->type
->soft_reset_halt_imp(target
);
564 static int target_run_algorithm_imp(struct target
*target
, int num_mem_params
, struct mem_param
*mem_params
, int num_reg_params
, struct reg_param
*reg_param
, uint32_t entry_point
, uint32_t exit_point
, int timeout_ms
, void *arch_info
)
566 if (!target_was_examined(target
))
568 LOG_ERROR("Target not examined yet");
571 return target
->type
->run_algorithm_imp(target
, num_mem_params
, mem_params
, num_reg_params
, reg_param
, entry_point
, exit_point
, timeout_ms
, arch_info
);
574 int target_read_memory(struct target
*target
,
575 uint32_t address
, uint32_t size
, uint32_t count
, uint8_t *buffer
)
577 return target
->type
->read_memory(target
, address
, size
, count
, buffer
);
580 int target_read_phys_memory(struct target
*target
,
581 uint32_t address
, uint32_t size
, uint32_t count
, uint8_t *buffer
)
583 return target
->type
->read_phys_memory(target
, address
, size
, count
, buffer
);
586 int target_write_memory(struct target
*target
,
587 uint32_t address
, uint32_t size
, uint32_t count
, uint8_t *buffer
)
589 return target
->type
->write_memory(target
, address
, size
, count
, buffer
);
592 int target_write_phys_memory(struct target
*target
,
593 uint32_t address
, uint32_t size
, uint32_t count
, uint8_t *buffer
)
595 return target
->type
->write_phys_memory(target
, address
, size
, count
, buffer
);
598 int target_bulk_write_memory(struct target
*target
,
599 uint32_t address
, uint32_t count
, uint8_t *buffer
)
601 return target
->type
->bulk_write_memory(target
, address
, count
, buffer
);
604 int target_add_breakpoint(struct target
*target
,
605 struct breakpoint
*breakpoint
)
607 if (target
->state
!= TARGET_HALTED
) {
608 LOG_WARNING("target %s is not halted", target
->cmd_name
);
609 return ERROR_TARGET_NOT_HALTED
;
611 return target
->type
->add_breakpoint(target
, breakpoint
);
613 int target_remove_breakpoint(struct target
*target
,
614 struct breakpoint
*breakpoint
)
616 return target
->type
->remove_breakpoint(target
, breakpoint
);
619 int target_add_watchpoint(struct target
*target
,
620 struct watchpoint
*watchpoint
)
622 if (target
->state
!= TARGET_HALTED
) {
623 LOG_WARNING("target %s is not halted", target
->cmd_name
);
624 return ERROR_TARGET_NOT_HALTED
;
626 return target
->type
->add_watchpoint(target
, watchpoint
);
628 int target_remove_watchpoint(struct target
*target
,
629 struct watchpoint
*watchpoint
)
631 return target
->type
->remove_watchpoint(target
, watchpoint
);
634 int target_get_gdb_reg_list(struct target
*target
,
635 struct reg
**reg_list
[], int *reg_list_size
)
637 return target
->type
->get_gdb_reg_list(target
, reg_list
, reg_list_size
);
639 int target_step(struct target
*target
,
640 int current
, uint32_t address
, int handle_breakpoints
)
642 return target
->type
->step(target
, current
, address
, handle_breakpoints
);
646 int target_run_algorithm(struct target
*target
,
647 int num_mem_params
, struct mem_param
*mem_params
,
648 int num_reg_params
, struct reg_param
*reg_param
,
649 uint32_t entry_point
, uint32_t exit_point
,
650 int timeout_ms
, void *arch_info
)
652 return target
->type
->run_algorithm(target
,
653 num_mem_params
, mem_params
, num_reg_params
, reg_param
,
654 entry_point
, exit_point
, timeout_ms
, arch_info
);
658 * Reset the @c examined flag for the given target.
659 * Pure paranoia -- targets are zeroed on allocation.
661 static void target_reset_examined(struct target
*target
)
663 target
->examined
= false;
667 err_read_phys_memory(struct target
*target
, uint32_t address
,
668 uint32_t size
, uint32_t count
, uint8_t *buffer
)
670 LOG_ERROR("Not implemented: %s", __func__
);
675 err_write_phys_memory(struct target
*target
, uint32_t address
,
676 uint32_t size
, uint32_t count
, uint8_t *buffer
)
678 LOG_ERROR("Not implemented: %s", __func__
);
682 static int handle_target(void *priv
);
684 static int target_init_one(struct command_context
*cmd_ctx
,
685 struct target
*target
)
687 target_reset_examined(target
);
689 struct target_type
*type
= target
->type
;
690 if (type
->examine
== NULL
)
691 type
->examine
= default_examine
;
693 int retval
= type
->init_target(cmd_ctx
, target
);
694 if (ERROR_OK
!= retval
)
696 LOG_ERROR("target '%s' init failed", target_name(target
));
701 * @todo get rid of those *memory_imp() methods, now that all
702 * callers are using target_*_memory() accessors ... and make
703 * sure the "physical" paths handle the same issues.
705 /* a non-invasive way(in terms of patches) to add some code that
706 * runs before the type->write/read_memory implementation
708 type
->write_memory_imp
= target
->type
->write_memory
;
709 type
->write_memory
= target_write_memory_imp
;
711 type
->read_memory_imp
= target
->type
->read_memory
;
712 type
->read_memory
= target_read_memory_imp
;
714 type
->soft_reset_halt_imp
= target
->type
->soft_reset_halt
;
715 type
->soft_reset_halt
= target_soft_reset_halt_imp
;
717 type
->run_algorithm_imp
= target
->type
->run_algorithm
;
718 type
->run_algorithm
= target_run_algorithm_imp
;
720 /* Sanity-check MMU support ... stub in what we must, to help
721 * implement it in stages, but warn if we need to do so.
725 if (type
->write_phys_memory
== NULL
)
727 LOG_ERROR("type '%s' is missing write_phys_memory",
729 type
->write_phys_memory
= err_write_phys_memory
;
731 if (type
->read_phys_memory
== NULL
)
733 LOG_ERROR("type '%s' is missing read_phys_memory",
735 type
->read_phys_memory
= err_read_phys_memory
;
737 if (type
->virt2phys
== NULL
)
739 LOG_ERROR("type '%s' is missing virt2phys", type
->name
);
740 type
->virt2phys
= identity_virt2phys
;
745 /* Make sure no-MMU targets all behave the same: make no
746 * distinction between physical and virtual addresses, and
747 * ensure that virt2phys() is always an identity mapping.
749 if (type
->write_phys_memory
|| type
->read_phys_memory
752 LOG_WARNING("type '%s' has bad MMU hooks", type
->name
);
756 type
->write_phys_memory
= type
->write_memory
;
757 type
->read_phys_memory
= type
->read_memory
;
758 type
->virt2phys
= identity_virt2phys
;
763 int target_init(struct command_context
*cmd_ctx
)
765 struct target
*target
;
768 for (target
= all_targets
; target
; target
= target
->next
)
770 retval
= target_init_one(cmd_ctx
, target
);
771 if (ERROR_OK
!= retval
)
778 retval
= target_register_user_commands(cmd_ctx
);
779 if (ERROR_OK
!= retval
)
782 retval
= target_register_timer_callback(&handle_target
,
783 100, 1, cmd_ctx
->interp
);
784 if (ERROR_OK
!= retval
)
790 COMMAND_HANDLER(handle_target_init_command
)
793 return ERROR_COMMAND_SYNTAX_ERROR
;
795 static bool target_initialized
= false;
796 if (target_initialized
)
798 LOG_INFO("'target init' has already been called");
801 target_initialized
= true;
803 LOG_DEBUG("Initializing targets...");
804 return target_init(CMD_CTX
);
807 int target_register_event_callback(int (*callback
)(struct target
*target
, enum target_event event
, void *priv
), void *priv
)
809 struct target_event_callback
**callbacks_p
= &target_event_callbacks
;
811 if (callback
== NULL
)
813 return ERROR_INVALID_ARGUMENTS
;
818 while ((*callbacks_p
)->next
)
819 callbacks_p
= &((*callbacks_p
)->next
);
820 callbacks_p
= &((*callbacks_p
)->next
);
823 (*callbacks_p
) = malloc(sizeof(struct target_event_callback
));
824 (*callbacks_p
)->callback
= callback
;
825 (*callbacks_p
)->priv
= priv
;
826 (*callbacks_p
)->next
= NULL
;
831 int target_register_timer_callback(int (*callback
)(void *priv
), int time_ms
, int periodic
, void *priv
)
833 struct target_timer_callback
**callbacks_p
= &target_timer_callbacks
;
836 if (callback
== NULL
)
838 return ERROR_INVALID_ARGUMENTS
;
843 while ((*callbacks_p
)->next
)
844 callbacks_p
= &((*callbacks_p
)->next
);
845 callbacks_p
= &((*callbacks_p
)->next
);
848 (*callbacks_p
) = malloc(sizeof(struct target_timer_callback
));
849 (*callbacks_p
)->callback
= callback
;
850 (*callbacks_p
)->periodic
= periodic
;
851 (*callbacks_p
)->time_ms
= time_ms
;
853 gettimeofday(&now
, NULL
);
854 (*callbacks_p
)->when
.tv_usec
= now
.tv_usec
+ (time_ms
% 1000) * 1000;
855 time_ms
-= (time_ms
% 1000);
856 (*callbacks_p
)->when
.tv_sec
= now
.tv_sec
+ (time_ms
/ 1000);
857 if ((*callbacks_p
)->when
.tv_usec
> 1000000)
859 (*callbacks_p
)->when
.tv_usec
= (*callbacks_p
)->when
.tv_usec
- 1000000;
860 (*callbacks_p
)->when
.tv_sec
+= 1;
863 (*callbacks_p
)->priv
= priv
;
864 (*callbacks_p
)->next
= NULL
;
869 int target_unregister_event_callback(int (*callback
)(struct target
*target
, enum target_event event
, void *priv
), void *priv
)
871 struct target_event_callback
**p
= &target_event_callbacks
;
872 struct target_event_callback
*c
= target_event_callbacks
;
874 if (callback
== NULL
)
876 return ERROR_INVALID_ARGUMENTS
;
881 struct target_event_callback
*next
= c
->next
;
882 if ((c
->callback
== callback
) && (c
->priv
== priv
))
896 int target_unregister_timer_callback(int (*callback
)(void *priv
), void *priv
)
898 struct target_timer_callback
**p
= &target_timer_callbacks
;
899 struct target_timer_callback
*c
= target_timer_callbacks
;
901 if (callback
== NULL
)
903 return ERROR_INVALID_ARGUMENTS
;
908 struct target_timer_callback
*next
= c
->next
;
909 if ((c
->callback
== callback
) && (c
->priv
== priv
))
923 int target_call_event_callbacks(struct target
*target
, enum target_event event
)
925 struct target_event_callback
*callback
= target_event_callbacks
;
926 struct target_event_callback
*next_callback
;
928 if (event
== TARGET_EVENT_HALTED
)
930 /* execute early halted first */
931 target_call_event_callbacks(target
, TARGET_EVENT_GDB_HALT
);
934 LOG_DEBUG("target event %i (%s)",
936 Jim_Nvp_value2name_simple(nvp_target_event
, event
)->name
);
938 target_handle_event(target
, event
);
942 next_callback
= callback
->next
;
943 callback
->callback(target
, event
, callback
->priv
);
944 callback
= next_callback
;
950 static int target_timer_callback_periodic_restart(
951 struct target_timer_callback
*cb
, struct timeval
*now
)
953 int time_ms
= cb
->time_ms
;
954 cb
->when
.tv_usec
= now
->tv_usec
+ (time_ms
% 1000) * 1000;
955 time_ms
-= (time_ms
% 1000);
956 cb
->when
.tv_sec
= now
->tv_sec
+ time_ms
/ 1000;
957 if (cb
->when
.tv_usec
> 1000000)
959 cb
->when
.tv_usec
= cb
->when
.tv_usec
- 1000000;
960 cb
->when
.tv_sec
+= 1;
965 static int target_call_timer_callback(struct target_timer_callback
*cb
,
968 cb
->callback(cb
->priv
);
971 return target_timer_callback_periodic_restart(cb
, now
);
973 return target_unregister_timer_callback(cb
->callback
, cb
->priv
);
976 static int target_call_timer_callbacks_check_time(int checktime
)
981 gettimeofday(&now
, NULL
);
983 struct target_timer_callback
*callback
= target_timer_callbacks
;
986 // cleaning up may unregister and free this callback
987 struct target_timer_callback
*next_callback
= callback
->next
;
989 bool call_it
= callback
->callback
&&
990 ((!checktime
&& callback
->periodic
) ||
991 now
.tv_sec
> callback
->when
.tv_sec
||
992 (now
.tv_sec
== callback
->when
.tv_sec
&&
993 now
.tv_usec
>= callback
->when
.tv_usec
));
997 int retval
= target_call_timer_callback(callback
, &now
);
998 if (retval
!= ERROR_OK
)
1002 callback
= next_callback
;
1008 int target_call_timer_callbacks(void)
1010 return target_call_timer_callbacks_check_time(1);
1013 /* invoke periodic callbacks immediately */
1014 int target_call_timer_callbacks_now(void)
1016 return target_call_timer_callbacks_check_time(0);
1019 int target_alloc_working_area(struct target
*target
, uint32_t size
, struct working_area
**area
)
1021 struct working_area
*c
= target
->working_areas
;
1022 struct working_area
*new_wa
= NULL
;
1024 /* Reevaluate working area address based on MMU state*/
1025 if (target
->working_areas
== NULL
)
1030 retval
= target
->type
->mmu(target
, &enabled
);
1031 if (retval
!= ERROR_OK
)
1037 if (target
->working_area_phys_spec
) {
1038 LOG_DEBUG("MMU disabled, using physical "
1039 "address for working memory 0x%08x",
1040 (unsigned)target
->working_area_phys
);
1041 target
->working_area
= target
->working_area_phys
;
1043 LOG_ERROR("No working memory available. "
1044 "Specify -work-area-phys to target.");
1045 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
1048 if (target
->working_area_virt_spec
) {
1049 LOG_DEBUG("MMU enabled, using virtual "
1050 "address for working memory 0x%08x",
1051 (unsigned)target
->working_area_virt
);
1052 target
->working_area
= target
->working_area_virt
;
1054 LOG_ERROR("No working memory available. "
1055 "Specify -work-area-virt to target.");
1056 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
1061 /* only allocate multiples of 4 byte */
1064 LOG_ERROR("BUG: code tried to allocate unaligned number of bytes (0x%08x), padding", ((unsigned)(size
)));
1065 size
= (size
+ 3) & (~3);
1068 /* see if there's already a matching working area */
1071 if ((c
->free
) && (c
->size
== size
))
1079 /* if not, allocate a new one */
1082 struct working_area
**p
= &target
->working_areas
;
1083 uint32_t first_free
= target
->working_area
;
1084 uint32_t free_size
= target
->working_area_size
;
1086 c
= target
->working_areas
;
1089 first_free
+= c
->size
;
1090 free_size
-= c
->size
;
1095 if (free_size
< size
)
1097 LOG_WARNING("not enough working area available(requested %u, free %u)",
1098 (unsigned)(size
), (unsigned)(free_size
));
1099 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
1102 LOG_DEBUG("allocated new working area at address 0x%08x", (unsigned)first_free
);
1104 new_wa
= malloc(sizeof(struct working_area
));
1105 new_wa
->next
= NULL
;
1106 new_wa
->size
= size
;
1107 new_wa
->address
= first_free
;
1109 if (target
->backup_working_area
)
1112 new_wa
->backup
= malloc(new_wa
->size
);
1113 if ((retval
= target_read_memory(target
, new_wa
->address
, 4, new_wa
->size
/ 4, new_wa
->backup
)) != ERROR_OK
)
1115 free(new_wa
->backup
);
1122 new_wa
->backup
= NULL
;
1125 /* put new entry in list */
1129 /* mark as used, and return the new (reused) area */
1134 new_wa
->user
= area
;
1139 int target_free_working_area_restore(struct target
*target
, struct working_area
*area
, int restore
)
1144 if (restore
&& target
->backup_working_area
)
1147 if ((retval
= target_write_memory(target
, area
->address
, 4, area
->size
/ 4, area
->backup
)) != ERROR_OK
)
1153 /* mark user pointer invalid */
1160 int target_free_working_area(struct target
*target
, struct working_area
*area
)
1162 return target_free_working_area_restore(target
, area
, 1);
1165 /* free resources and restore memory, if restoring memory fails,
1166 * free up resources anyway
1168 void target_free_all_working_areas_restore(struct target
*target
, int restore
)
1170 struct working_area
*c
= target
->working_areas
;
1174 struct working_area
*next
= c
->next
;
1175 target_free_working_area_restore(target
, c
, restore
);
1185 target
->working_areas
= NULL
;
1188 void target_free_all_working_areas(struct target
*target
)
1190 target_free_all_working_areas_restore(target
, 1);
1193 int target_arch_state(struct target
*target
)
1198 LOG_USER("No target has been configured");
1202 LOG_USER("target state: %s", target_state_name( target
));
1204 if (target
->state
!= TARGET_HALTED
)
1207 retval
= target
->type
->arch_state(target
);
1211 /* Single aligned words are guaranteed to use 16 or 32 bit access
1212 * mode respectively, otherwise data is handled as quickly as
1215 int target_write_buffer(struct target
*target
, uint32_t address
, uint32_t size
, uint8_t *buffer
)
1218 LOG_DEBUG("writing buffer of %i byte at 0x%8.8x",
1219 (int)size
, (unsigned)address
);
1221 if (!target_was_examined(target
))
1223 LOG_ERROR("Target not examined yet");
1231 if ((address
+ size
- 1) < address
)
1233 /* GDB can request this when e.g. PC is 0xfffffffc*/
1234 LOG_ERROR("address + size wrapped(0x%08x, 0x%08x)",
1240 if (((address
% 2) == 0) && (size
== 2))
1242 return target_write_memory(target
, address
, 2, 1, buffer
);
1245 /* handle unaligned head bytes */
1248 uint32_t unaligned
= 4 - (address
% 4);
1250 if (unaligned
> size
)
1253 if ((retval
= target_write_memory(target
, address
, 1, unaligned
, buffer
)) != ERROR_OK
)
1256 buffer
+= unaligned
;
1257 address
+= unaligned
;
1261 /* handle aligned words */
1264 int aligned
= size
- (size
% 4);
1266 /* use bulk writes above a certain limit. This may have to be changed */
1269 if ((retval
= target
->type
->bulk_write_memory(target
, address
, aligned
/ 4, buffer
)) != ERROR_OK
)
1274 if ((retval
= target_write_memory(target
, address
, 4, aligned
/ 4, buffer
)) != ERROR_OK
)
1283 /* handle tail writes of less than 4 bytes */
1286 if ((retval
= target_write_memory(target
, address
, 1, size
, buffer
)) != ERROR_OK
)
1293 /* Single aligned words are guaranteed to use 16 or 32 bit access
1294 * mode respectively, otherwise data is handled as quickly as
1297 int target_read_buffer(struct target
*target
, uint32_t address
, uint32_t size
, uint8_t *buffer
)
1300 LOG_DEBUG("reading buffer of %i byte at 0x%8.8x",
1301 (int)size
, (unsigned)address
);
1303 if (!target_was_examined(target
))
1305 LOG_ERROR("Target not examined yet");
1313 if ((address
+ size
- 1) < address
)
1315 /* GDB can request this when e.g. PC is 0xfffffffc*/
1316 LOG_ERROR("address + size wrapped(0x%08" PRIx32
", 0x%08" PRIx32
")",
1322 if (((address
% 2) == 0) && (size
== 2))
1324 return target_read_memory(target
, address
, 2, 1, buffer
);
1327 /* handle unaligned head bytes */
1330 uint32_t unaligned
= 4 - (address
% 4);
1332 if (unaligned
> size
)
1335 if ((retval
= target_read_memory(target
, address
, 1, unaligned
, buffer
)) != ERROR_OK
)
1338 buffer
+= unaligned
;
1339 address
+= unaligned
;
1343 /* handle aligned words */
1346 int aligned
= size
- (size
% 4);
1348 if ((retval
= target_read_memory(target
, address
, 4, aligned
/ 4, buffer
)) != ERROR_OK
)
1356 /*prevent byte access when possible (avoid AHB access limitations in some cases)*/
1359 int aligned
= size
- (size
%2);
1360 retval
= target_read_memory(target
, address
, 2, aligned
/ 2, buffer
);
1361 if (retval
!= ERROR_OK
)
1368 /* handle tail writes of less than 4 bytes */
1371 if ((retval
= target_read_memory(target
, address
, 1, size
, buffer
)) != ERROR_OK
)
1378 int target_checksum_memory(struct target
*target
, uint32_t address
, uint32_t size
, uint32_t* crc
)
1383 uint32_t checksum
= 0;
1384 if (!target_was_examined(target
))
1386 LOG_ERROR("Target not examined yet");
1390 if ((retval
= target
->type
->checksum_memory(target
, address
,
1391 size
, &checksum
)) != ERROR_OK
)
1393 buffer
= malloc(size
);
1396 LOG_ERROR("error allocating buffer for section (%d bytes)", (int)size
);
1397 return ERROR_INVALID_ARGUMENTS
;
1399 retval
= target_read_buffer(target
, address
, size
, buffer
);
1400 if (retval
!= ERROR_OK
)
1406 /* convert to target endianess */
1407 for (i
= 0; i
< (size
/sizeof(uint32_t)); i
++)
1409 uint32_t target_data
;
1410 target_data
= target_buffer_get_u32(target
, &buffer
[i
*sizeof(uint32_t)]);
1411 target_buffer_set_u32(target
, &buffer
[i
*sizeof(uint32_t)], target_data
);
1414 retval
= image_calculate_checksum(buffer
, size
, &checksum
);
1423 int target_blank_check_memory(struct target
*target
, uint32_t address
, uint32_t size
, uint32_t* blank
)
1426 if (!target_was_examined(target
))
1428 LOG_ERROR("Target not examined yet");
1432 if (target
->type
->blank_check_memory
== 0)
1433 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
1435 retval
= target
->type
->blank_check_memory(target
, address
, size
, blank
);
1440 int target_read_u32(struct target
*target
, uint32_t address
, uint32_t *value
)
1442 uint8_t value_buf
[4];
1443 if (!target_was_examined(target
))
1445 LOG_ERROR("Target not examined yet");
1449 int retval
= target_read_memory(target
, address
, 4, 1, value_buf
);
1451 if (retval
== ERROR_OK
)
1453 *value
= target_buffer_get_u32(target
, value_buf
);
1454 LOG_DEBUG("address: 0x%8.8" PRIx32
", value: 0x%8.8" PRIx32
"",
1461 LOG_DEBUG("address: 0x%8.8" PRIx32
" failed",
1468 int target_read_u16(struct target
*target
, uint32_t address
, uint16_t *value
)
1470 uint8_t value_buf
[2];
1471 if (!target_was_examined(target
))
1473 LOG_ERROR("Target not examined yet");
1477 int retval
= target_read_memory(target
, address
, 2, 1, value_buf
);
1479 if (retval
== ERROR_OK
)
1481 *value
= target_buffer_get_u16(target
, value_buf
);
1482 LOG_DEBUG("address: 0x%8.8" PRIx32
", value: 0x%4.4x",
1489 LOG_DEBUG("address: 0x%8.8" PRIx32
" failed",
1496 int target_read_u8(struct target
*target
, uint32_t address
, uint8_t *value
)
1498 int retval
= target_read_memory(target
, address
, 1, 1, value
);
1499 if (!target_was_examined(target
))
1501 LOG_ERROR("Target not examined yet");
1505 if (retval
== ERROR_OK
)
1507 LOG_DEBUG("address: 0x%8.8" PRIx32
", value: 0x%2.2x",
1514 LOG_DEBUG("address: 0x%8.8" PRIx32
" failed",
1521 int target_write_u32(struct target
*target
, uint32_t address
, uint32_t value
)
1524 uint8_t value_buf
[4];
1525 if (!target_was_examined(target
))
1527 LOG_ERROR("Target not examined yet");
1531 LOG_DEBUG("address: 0x%8.8" PRIx32
", value: 0x%8.8" PRIx32
"",
1535 target_buffer_set_u32(target
, value_buf
, value
);
1536 if ((retval
= target_write_memory(target
, address
, 4, 1, value_buf
)) != ERROR_OK
)
1538 LOG_DEBUG("failed: %i", retval
);
1544 int target_write_u16(struct target
*target
, uint32_t address
, uint16_t value
)
1547 uint8_t value_buf
[2];
1548 if (!target_was_examined(target
))
1550 LOG_ERROR("Target not examined yet");
1554 LOG_DEBUG("address: 0x%8.8" PRIx32
", value: 0x%8.8x",
1558 target_buffer_set_u16(target
, value_buf
, value
);
1559 if ((retval
= target_write_memory(target
, address
, 2, 1, value_buf
)) != ERROR_OK
)
1561 LOG_DEBUG("failed: %i", retval
);
1567 int target_write_u8(struct target
*target
, uint32_t address
, uint8_t value
)
1570 if (!target_was_examined(target
))
1572 LOG_ERROR("Target not examined yet");
1576 LOG_DEBUG("address: 0x%8.8" PRIx32
", value: 0x%2.2x",
1579 if ((retval
= target_write_memory(target
, address
, 1, 1, &value
)) != ERROR_OK
)
1581 LOG_DEBUG("failed: %i", retval
);
1587 COMMAND_HANDLER(handle_targets_command
)
1589 struct target
*target
= all_targets
;
1593 target
= get_target(CMD_ARGV
[0]);
1594 if (target
== NULL
) {
1595 command_print(CMD_CTX
,"Target: %s is unknown, try one of:\n", CMD_ARGV
[0]);
1598 if (!target
->tap
->enabled
) {
1599 command_print(CMD_CTX
,"Target: TAP %s is disabled, "
1600 "can't be the current target\n",
1601 target
->tap
->dotted_name
);
1605 CMD_CTX
->current_target
= target
->target_number
;
1610 target
= all_targets
;
1611 command_print(CMD_CTX
, " TargetName Type Endian TapName State ");
1612 command_print(CMD_CTX
, "-- ------------------ ---------- ------ ------------------ ------------");
1618 if (target
->tap
->enabled
)
1619 state
= target_state_name( target
);
1621 state
= "tap-disabled";
1623 if (CMD_CTX
->current_target
== target
->target_number
)
1626 /* keep columns lined up to match the headers above */
1627 command_print(CMD_CTX
, "%2d%c %-18s %-10s %-6s %-18s %s",
1628 target
->target_number
,
1630 target_name(target
),
1631 target_type_name(target
),
1632 Jim_Nvp_value2name_simple(nvp_target_endian
,
1633 target
->endianness
)->name
,
1634 target
->tap
->dotted_name
,
1636 target
= target
->next
;
1642 /* every 300ms we check for reset & powerdropout and issue a "reset halt" if so. */
1644 static int powerDropout
;
1645 static int srstAsserted
;
1647 static int runPowerRestore
;
1648 static int runPowerDropout
;
1649 static int runSrstAsserted
;
1650 static int runSrstDeasserted
;
1652 static int sense_handler(void)
1654 static int prevSrstAsserted
= 0;
1655 static int prevPowerdropout
= 0;
1658 if ((retval
= jtag_power_dropout(&powerDropout
)) != ERROR_OK
)
1662 powerRestored
= prevPowerdropout
&& !powerDropout
;
1665 runPowerRestore
= 1;
1668 long long current
= timeval_ms();
1669 static long long lastPower
= 0;
1670 int waitMore
= lastPower
+ 2000 > current
;
1671 if (powerDropout
&& !waitMore
)
1673 runPowerDropout
= 1;
1674 lastPower
= current
;
1677 if ((retval
= jtag_srst_asserted(&srstAsserted
)) != ERROR_OK
)
1681 srstDeasserted
= prevSrstAsserted
&& !srstAsserted
;
1683 static long long lastSrst
= 0;
1684 waitMore
= lastSrst
+ 2000 > current
;
1685 if (srstDeasserted
&& !waitMore
)
1687 runSrstDeasserted
= 1;
1691 if (!prevSrstAsserted
&& srstAsserted
)
1693 runSrstAsserted
= 1;
1696 prevSrstAsserted
= srstAsserted
;
1697 prevPowerdropout
= powerDropout
;
1699 if (srstDeasserted
|| powerRestored
)
1701 /* Other than logging the event we can't do anything here.
1702 * Issuing a reset is a particularly bad idea as we might
1703 * be inside a reset already.
1710 static void target_call_event_callbacks_all(enum target_event e
) {
1711 struct target
*target
;
1712 target
= all_targets
;
1714 target_call_event_callbacks(target
, e
);
1715 target
= target
->next
;
1719 /* process target state changes */
1720 static int handle_target(void *priv
)
1722 Jim_Interp
*interp
= (Jim_Interp
*)priv
;
1723 int retval
= ERROR_OK
;
1725 /* we do not want to recurse here... */
1726 static int recursive
= 0;
1731 /* danger! running these procedures can trigger srst assertions and power dropouts.
1732 * We need to avoid an infinite loop/recursion here and we do that by
1733 * clearing the flags after running these events.
1735 int did_something
= 0;
1736 if (runSrstAsserted
)
1738 target_call_event_callbacks_all(TARGET_EVENT_GDB_HALT
);
1739 Jim_Eval(interp
, "srst_asserted");
1742 if (runSrstDeasserted
)
1744 Jim_Eval(interp
, "srst_deasserted");
1747 if (runPowerDropout
)
1749 target_call_event_callbacks_all(TARGET_EVENT_GDB_HALT
);
1750 Jim_Eval(interp
, "power_dropout");
1753 if (runPowerRestore
)
1755 Jim_Eval(interp
, "power_restore");
1761 /* clear detect flags */
1765 /* clear action flags */
1767 runSrstAsserted
= 0;
1768 runSrstDeasserted
= 0;
1769 runPowerRestore
= 0;
1770 runPowerDropout
= 0;
1775 /* Poll targets for state changes unless that's globally disabled.
1776 * Skip targets that are currently disabled.
1778 for (struct target
*target
= all_targets
;
1779 is_jtag_poll_safe() && target
;
1780 target
= target
->next
)
1782 if (!target
->tap
->enabled
)
1785 /* only poll target if we've got power and srst isn't asserted */
1786 if (!powerDropout
&& !srstAsserted
)
1788 /* polling may fail silently until the target has been examined */
1789 if ((retval
= target_poll(target
)) != ERROR_OK
)
1791 target_call_event_callbacks(target
, TARGET_EVENT_GDB_HALT
);
1800 COMMAND_HANDLER(handle_reg_command
)
1802 struct target
*target
;
1803 struct reg
*reg
= NULL
;
1809 target
= get_current_target(CMD_CTX
);
1811 /* list all available registers for the current target */
1814 struct reg_cache
*cache
= target
->reg_cache
;
1821 command_print(CMD_CTX
, "===== %s", cache
->name
);
1823 for (i
= 0, reg
= cache
->reg_list
;
1824 i
< cache
->num_regs
;
1825 i
++, reg
++, count
++)
1827 /* only print cached values if they are valid */
1829 value
= buf_to_str(reg
->value
,
1831 command_print(CMD_CTX
,
1832 "(%i) %s (/%" PRIu32
"): 0x%s%s",
1840 command_print(CMD_CTX
, "(%i) %s (/%" PRIu32
")",
1845 cache
= cache
->next
;
1851 /* access a single register by its ordinal number */
1852 if ((CMD_ARGV
[0][0] >= '0') && (CMD_ARGV
[0][0] <= '9'))
1855 COMMAND_PARSE_NUMBER(uint
, CMD_ARGV
[0], num
);
1857 struct reg_cache
*cache
= target
->reg_cache
;
1862 for (i
= 0; i
< cache
->num_regs
; i
++)
1866 reg
= &cache
->reg_list
[i
];
1872 cache
= cache
->next
;
1877 command_print(CMD_CTX
, "%i is out of bounds, the current target has only %i registers (0 - %i)", num
, count
, count
- 1);
1880 } else /* access a single register by its name */
1882 reg
= register_get_by_name(target
->reg_cache
, CMD_ARGV
[0], 1);
1886 command_print(CMD_CTX
, "register %s not found in current target", CMD_ARGV
[0]);
1891 /* display a register */
1892 if ((CMD_ARGC
== 1) || ((CMD_ARGC
== 2) && !((CMD_ARGV
[1][0] >= '0') && (CMD_ARGV
[1][0] <= '9'))))
1894 if ((CMD_ARGC
== 2) && (strcmp(CMD_ARGV
[1], "force") == 0))
1897 if (reg
->valid
== 0)
1899 reg
->type
->get(reg
);
1901 value
= buf_to_str(reg
->value
, reg
->size
, 16);
1902 command_print(CMD_CTX
, "%s (/%i): 0x%s", reg
->name
, (int)(reg
->size
), value
);
1907 /* set register value */
1910 uint8_t *buf
= malloc(DIV_ROUND_UP(reg
->size
, 8));
1911 str_to_buf(CMD_ARGV
[1], strlen(CMD_ARGV
[1]), buf
, reg
->size
, 0);
1913 reg
->type
->set(reg
, buf
);
1915 value
= buf_to_str(reg
->value
, reg
->size
, 16);
1916 command_print(CMD_CTX
, "%s (/%i): 0x%s", reg
->name
, (int)(reg
->size
), value
);
1924 command_print(CMD_CTX
, "usage: reg <#|name> [value]");
1929 COMMAND_HANDLER(handle_poll_command
)
1931 int retval
= ERROR_OK
;
1932 struct target
*target
= get_current_target(CMD_CTX
);
1936 command_print(CMD_CTX
, "background polling: %s",
1937 jtag_poll_get_enabled() ? "on" : "off");
1938 command_print(CMD_CTX
, "TAP: %s (%s)",
1939 target
->tap
->dotted_name
,
1940 target
->tap
->enabled
? "enabled" : "disabled");
1941 if (!target
->tap
->enabled
)
1943 if ((retval
= target_poll(target
)) != ERROR_OK
)
1945 if ((retval
= target_arch_state(target
)) != ERROR_OK
)
1948 else if (CMD_ARGC
== 1)
1951 COMMAND_PARSE_ON_OFF(CMD_ARGV
[0], enable
);
1952 jtag_poll_set_enabled(enable
);
1956 return ERROR_COMMAND_SYNTAX_ERROR
;
1962 COMMAND_HANDLER(handle_wait_halt_command
)
1965 return ERROR_COMMAND_SYNTAX_ERROR
;
1970 int retval
= parse_uint(CMD_ARGV
[0], &ms
);
1971 if (ERROR_OK
!= retval
)
1973 command_print(CMD_CTX
, "usage: %s [seconds]", CMD_NAME
);
1974 return ERROR_COMMAND_SYNTAX_ERROR
;
1976 // convert seconds (given) to milliseconds (needed)
1980 struct target
*target
= get_current_target(CMD_CTX
);
1981 return target_wait_state(target
, TARGET_HALTED
, ms
);
1984 /* wait for target state to change. The trick here is to have a low
1985 * latency for short waits and not to suck up all the CPU time
1988 * After 500ms, keep_alive() is invoked
1990 int target_wait_state(struct target
*target
, enum target_state state
, int ms
)
1993 long long then
= 0, cur
;
1998 if ((retval
= target_poll(target
)) != ERROR_OK
)
2000 if (target
->state
== state
)
2008 then
= timeval_ms();
2009 LOG_DEBUG("waiting for target %s...",
2010 Jim_Nvp_value2name_simple(nvp_target_state
,state
)->name
);
2018 if ((cur
-then
) > ms
)
2020 LOG_ERROR("timed out while waiting for target %s",
2021 Jim_Nvp_value2name_simple(nvp_target_state
,state
)->name
);
2029 COMMAND_HANDLER(handle_halt_command
)
2033 struct target
*target
= get_current_target(CMD_CTX
);
2034 int retval
= target_halt(target
);
2035 if (ERROR_OK
!= retval
)
2041 retval
= parse_uint(CMD_ARGV
[0], &wait
);
2042 if (ERROR_OK
!= retval
)
2043 return ERROR_COMMAND_SYNTAX_ERROR
;
2048 return CALL_COMMAND_HANDLER(handle_wait_halt_command
);
2051 COMMAND_HANDLER(handle_soft_reset_halt_command
)
2053 struct target
*target
= get_current_target(CMD_CTX
);
2055 LOG_USER("requesting target halt and executing a soft reset");
2057 target
->type
->soft_reset_halt(target
);
2062 COMMAND_HANDLER(handle_reset_command
)
2065 return ERROR_COMMAND_SYNTAX_ERROR
;
2067 enum target_reset_mode reset_mode
= RESET_RUN
;
2071 n
= Jim_Nvp_name2value_simple(nvp_reset_modes
, CMD_ARGV
[0]);
2072 if ((n
->name
== NULL
) || (n
->value
== RESET_UNKNOWN
)) {
2073 return ERROR_COMMAND_SYNTAX_ERROR
;
2075 reset_mode
= n
->value
;
2078 /* reset *all* targets */
2079 return target_process_reset(CMD_CTX
, reset_mode
);
2083 COMMAND_HANDLER(handle_resume_command
)
2087 return ERROR_COMMAND_SYNTAX_ERROR
;
2089 struct target
*target
= get_current_target(CMD_CTX
);
2090 target_handle_event(target
, TARGET_EVENT_OLD_pre_resume
);
2092 /* with no CMD_ARGV, resume from current pc, addr = 0,
2093 * with one arguments, addr = CMD_ARGV[0],
2094 * handle breakpoints, not debugging */
2098 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], addr
);
2102 return target_resume(target
, current
, addr
, 1, 0);
2105 COMMAND_HANDLER(handle_step_command
)
2108 return ERROR_COMMAND_SYNTAX_ERROR
;
2112 /* with no CMD_ARGV, step from current pc, addr = 0,
2113 * with one argument addr = CMD_ARGV[0],
2114 * handle breakpoints, debugging */
2119 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], addr
);
2123 struct target
*target
= get_current_target(CMD_CTX
);
2125 return target
->type
->step(target
, current_pc
, addr
, 1);
2128 static void handle_md_output(struct command_context
*cmd_ctx
,
2129 struct target
*target
, uint32_t address
, unsigned size
,
2130 unsigned count
, const uint8_t *buffer
)
2132 const unsigned line_bytecnt
= 32;
2133 unsigned line_modulo
= line_bytecnt
/ size
;
2135 char output
[line_bytecnt
* 4 + 1];
2136 unsigned output_len
= 0;
2138 const char *value_fmt
;
2140 case 4: value_fmt
= "%8.8x "; break;
2141 case 2: value_fmt
= "%4.2x "; break;
2142 case 1: value_fmt
= "%2.2x "; break;
2144 LOG_ERROR("invalid memory read size: %u", size
);
2148 for (unsigned i
= 0; i
< count
; i
++)
2150 if (i
% line_modulo
== 0)
2152 output_len
+= snprintf(output
+ output_len
,
2153 sizeof(output
) - output_len
,
2155 (unsigned)(address
+ (i
*size
)));
2159 const uint8_t *value_ptr
= buffer
+ i
* size
;
2161 case 4: value
= target_buffer_get_u32(target
, value_ptr
); break;
2162 case 2: value
= target_buffer_get_u16(target
, value_ptr
); break;
2163 case 1: value
= *value_ptr
;
2165 output_len
+= snprintf(output
+ output_len
,
2166 sizeof(output
) - output_len
,
2169 if ((i
% line_modulo
== line_modulo
- 1) || (i
== count
- 1))
2171 command_print(cmd_ctx
, "%s", output
);
2177 COMMAND_HANDLER(handle_md_command
)
2180 return ERROR_COMMAND_SYNTAX_ERROR
;
2183 switch (CMD_NAME
[2]) {
2184 case 'w': size
= 4; break;
2185 case 'h': size
= 2; break;
2186 case 'b': size
= 1; break;
2187 default: return ERROR_COMMAND_SYNTAX_ERROR
;
2190 bool physical
=strcmp(CMD_ARGV
[0], "phys")==0;
2191 int (*fn
)(struct target
*target
,
2192 uint32_t address
, uint32_t size
, uint32_t count
, uint8_t *buffer
);
2197 fn
=target_read_phys_memory
;
2200 fn
=target_read_memory
;
2202 if ((CMD_ARGC
< 1) || (CMD_ARGC
> 2))
2204 return ERROR_COMMAND_SYNTAX_ERROR
;
2208 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], address
);
2212 COMMAND_PARSE_NUMBER(uint
, CMD_ARGV
[1], count
);
2214 uint8_t *buffer
= calloc(count
, size
);
2216 struct target
*target
= get_current_target(CMD_CTX
);
2217 int retval
= fn(target
, address
, size
, count
, buffer
);
2218 if (ERROR_OK
== retval
)
2219 handle_md_output(CMD_CTX
, target
, address
, size
, count
, buffer
);
2226 COMMAND_HANDLER(handle_mw_command
)
2230 return ERROR_COMMAND_SYNTAX_ERROR
;
2232 bool physical
=strcmp(CMD_ARGV
[0], "phys")==0;
2233 int (*fn
)(struct target
*target
,
2234 uint32_t address
, uint32_t size
, uint32_t count
, uint8_t *buffer
);
2239 fn
=target_write_phys_memory
;
2242 fn
=target_write_memory
;
2244 if ((CMD_ARGC
< 2) || (CMD_ARGC
> 3))
2245 return ERROR_COMMAND_SYNTAX_ERROR
;
2248 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], address
);
2251 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], value
);
2255 COMMAND_PARSE_NUMBER(uint
, CMD_ARGV
[2], count
);
2257 struct target
*target
= get_current_target(CMD_CTX
);
2259 uint8_t value_buf
[4];
2260 switch (CMD_NAME
[2])
2264 target_buffer_set_u32(target
, value_buf
, value
);
2268 target_buffer_set_u16(target
, value_buf
, value
);
2272 value_buf
[0] = value
;
2275 return ERROR_COMMAND_SYNTAX_ERROR
;
2277 for (unsigned i
= 0; i
< count
; i
++)
2279 int retval
= fn(target
,
2280 address
+ i
* wordsize
, wordsize
, 1, value_buf
);
2281 if (ERROR_OK
!= retval
)
2290 static COMMAND_HELPER(parse_load_image_command_CMD_ARGV
, struct image
*image
,
2291 uint32_t *min_address
, uint32_t *max_address
)
2293 if (CMD_ARGC
< 1 || CMD_ARGC
> 5)
2294 return ERROR_COMMAND_SYNTAX_ERROR
;
2296 /* a base address isn't always necessary,
2297 * default to 0x0 (i.e. don't relocate) */
2301 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], addr
);
2302 image
->base_address
= addr
;
2303 image
->base_address_set
= 1;
2306 image
->base_address_set
= 0;
2308 image
->start_address_set
= 0;
2312 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[3], *min_address
);
2316 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[4], *max_address
);
2317 // use size (given) to find max (required)
2318 *max_address
+= *min_address
;
2321 if (*min_address
> *max_address
)
2322 return ERROR_COMMAND_SYNTAX_ERROR
;
2327 COMMAND_HANDLER(handle_load_image_command
)
2331 uint32_t image_size
;
2332 uint32_t min_address
= 0;
2333 uint32_t max_address
= 0xffffffff;
2337 int retval
= CALL_COMMAND_HANDLER(parse_load_image_command_CMD_ARGV
,
2338 &image
, &min_address
, &max_address
);
2339 if (ERROR_OK
!= retval
)
2342 struct target
*target
= get_current_target(CMD_CTX
);
2344 struct duration bench
;
2345 duration_start(&bench
);
2347 if (image_open(&image
, CMD_ARGV
[0], (CMD_ARGC
>= 3) ? CMD_ARGV
[2] : NULL
) != ERROR_OK
)
2354 for (i
= 0; i
< image
.num_sections
; i
++)
2356 buffer
= malloc(image
.sections
[i
].size
);
2359 command_print(CMD_CTX
,
2360 "error allocating buffer for section (%d bytes)",
2361 (int)(image
.sections
[i
].size
));
2365 if ((retval
= image_read_section(&image
, i
, 0x0, image
.sections
[i
].size
, buffer
, &buf_cnt
)) != ERROR_OK
)
2371 uint32_t offset
= 0;
2372 uint32_t length
= buf_cnt
;
2374 /* DANGER!!! beware of unsigned comparision here!!! */
2376 if ((image
.sections
[i
].base_address
+ buf_cnt
>= min_address
)&&
2377 (image
.sections
[i
].base_address
< max_address
))
2379 if (image
.sections
[i
].base_address
< min_address
)
2381 /* clip addresses below */
2382 offset
+= min_address
-image
.sections
[i
].base_address
;
2386 if (image
.sections
[i
].base_address
+ buf_cnt
> max_address
)
2388 length
-= (image
.sections
[i
].base_address
+ buf_cnt
)-max_address
;
2391 if ((retval
= target_write_buffer(target
, image
.sections
[i
].base_address
+ offset
, length
, buffer
+ offset
)) != ERROR_OK
)
2396 image_size
+= length
;
2397 command_print(CMD_CTX
, "%u bytes written at address 0x%8.8" PRIx32
"",
2398 (unsigned int)length
,
2399 image
.sections
[i
].base_address
+ offset
);
2405 if ((ERROR_OK
== retval
) && (duration_measure(&bench
) == ERROR_OK
))
2407 command_print(CMD_CTX
, "downloaded %" PRIu32
" bytes "
2408 "in %fs (%0.3f kb/s)", image_size
,
2409 duration_elapsed(&bench
), duration_kbps(&bench
, image_size
));
2412 image_close(&image
);
2418 COMMAND_HANDLER(handle_dump_image_command
)
2420 struct fileio fileio
;
2422 uint8_t buffer
[560];
2426 struct target
*target
= get_current_target(CMD_CTX
);
2430 command_print(CMD_CTX
, "usage: dump_image <filename> <address> <size>");
2435 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], address
);
2437 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[2], size
);
2439 if (fileio_open(&fileio
, CMD_ARGV
[0], FILEIO_WRITE
, FILEIO_BINARY
) != ERROR_OK
)
2444 struct duration bench
;
2445 duration_start(&bench
);
2447 int retval
= ERROR_OK
;
2450 size_t size_written
;
2451 uint32_t this_run_size
= (size
> 560) ? 560 : size
;
2452 retval
= target_read_buffer(target
, address
, this_run_size
, buffer
);
2453 if (retval
!= ERROR_OK
)
2458 retval
= fileio_write(&fileio
, this_run_size
, buffer
, &size_written
);
2459 if (retval
!= ERROR_OK
)
2464 size
-= this_run_size
;
2465 address
+= this_run_size
;
2468 if ((retvaltemp
= fileio_close(&fileio
)) != ERROR_OK
)
2471 if ((ERROR_OK
== retval
) && (duration_measure(&bench
) == ERROR_OK
))
2473 command_print(CMD_CTX
,
2474 "dumped %zu bytes in %fs (%0.3f kb/s)", fileio
.size
,
2475 duration_elapsed(&bench
), duration_kbps(&bench
, fileio
.size
));
2481 static COMMAND_HELPER(handle_verify_image_command_internal
, int verify
)
2485 uint32_t image_size
;
2488 uint32_t checksum
= 0;
2489 uint32_t mem_checksum
= 0;
2493 struct target
*target
= get_current_target(CMD_CTX
);
2497 return ERROR_COMMAND_SYNTAX_ERROR
;
2502 LOG_ERROR("no target selected");
2506 struct duration bench
;
2507 duration_start(&bench
);
2512 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], addr
);
2513 image
.base_address
= addr
;
2514 image
.base_address_set
= 1;
2518 image
.base_address_set
= 0;
2519 image
.base_address
= 0x0;
2522 image
.start_address_set
= 0;
2524 if ((retval
= image_open(&image
, CMD_ARGV
[0], (CMD_ARGC
== 3) ? CMD_ARGV
[2] : NULL
)) != ERROR_OK
)
2531 for (i
= 0; i
< image
.num_sections
; i
++)
2533 buffer
= malloc(image
.sections
[i
].size
);
2536 command_print(CMD_CTX
,
2537 "error allocating buffer for section (%d bytes)",
2538 (int)(image
.sections
[i
].size
));
2541 if ((retval
= image_read_section(&image
, i
, 0x0, image
.sections
[i
].size
, buffer
, &buf_cnt
)) != ERROR_OK
)
2549 /* calculate checksum of image */
2550 image_calculate_checksum(buffer
, buf_cnt
, &checksum
);
2552 retval
= target_checksum_memory(target
, image
.sections
[i
].base_address
, buf_cnt
, &mem_checksum
);
2553 if (retval
!= ERROR_OK
)
2559 if (checksum
!= mem_checksum
)
2561 /* failed crc checksum, fall back to a binary compare */
2564 command_print(CMD_CTX
, "checksum mismatch - attempting binary compare");
2566 data
= (uint8_t*)malloc(buf_cnt
);
2568 /* Can we use 32bit word accesses? */
2570 int count
= buf_cnt
;
2571 if ((count
% 4) == 0)
2576 retval
= target_read_memory(target
, image
.sections
[i
].base_address
, size
, count
, data
);
2577 if (retval
== ERROR_OK
)
2580 for (t
= 0; t
< buf_cnt
; t
++)
2582 if (data
[t
] != buffer
[t
])
2584 command_print(CMD_CTX
,
2585 "Verify operation failed address 0x%08x. Was 0x%02x instead of 0x%02x\n",
2586 (unsigned)(t
+ image
.sections
[i
].base_address
),
2591 retval
= ERROR_FAIL
;
2605 command_print(CMD_CTX
, "address 0x%08" PRIx32
" length 0x%08zx",
2606 image
.sections
[i
].base_address
,
2611 image_size
+= buf_cnt
;
2614 if ((ERROR_OK
== retval
) && (duration_measure(&bench
) == ERROR_OK
))
2616 command_print(CMD_CTX
, "verified %" PRIu32
" bytes "
2617 "in %fs (%0.3f kb/s)", image_size
,
2618 duration_elapsed(&bench
), duration_kbps(&bench
, image_size
));
2621 image_close(&image
);
2626 COMMAND_HANDLER(handle_verify_image_command
)
2628 return CALL_COMMAND_HANDLER(handle_verify_image_command_internal
, 1);
2631 COMMAND_HANDLER(handle_test_image_command
)
2633 return CALL_COMMAND_HANDLER(handle_verify_image_command_internal
, 0);
2636 static int handle_bp_command_list(struct command_context
*cmd_ctx
)
2638 struct target
*target
= get_current_target(cmd_ctx
);
2639 struct breakpoint
*breakpoint
= target
->breakpoints
;
2642 if (breakpoint
->type
== BKPT_SOFT
)
2644 char* buf
= buf_to_str(breakpoint
->orig_instr
,
2645 breakpoint
->length
, 16);
2646 command_print(cmd_ctx
, "0x%8.8" PRIx32
", 0x%x, %i, 0x%s",
2647 breakpoint
->address
,
2649 breakpoint
->set
, buf
);
2654 command_print(cmd_ctx
, "0x%8.8" PRIx32
", 0x%x, %i",
2655 breakpoint
->address
,
2656 breakpoint
->length
, breakpoint
->set
);
2659 breakpoint
= breakpoint
->next
;
2664 static int handle_bp_command_set(struct command_context
*cmd_ctx
,
2665 uint32_t addr
, uint32_t length
, int hw
)
2667 struct target
*target
= get_current_target(cmd_ctx
);
2668 int retval
= breakpoint_add(target
, addr
, length
, hw
);
2669 if (ERROR_OK
== retval
)
2670 command_print(cmd_ctx
, "breakpoint set at 0x%8.8" PRIx32
"", addr
);
2672 LOG_ERROR("Failure setting breakpoint");
2676 COMMAND_HANDLER(handle_bp_command
)
2679 return handle_bp_command_list(CMD_CTX
);
2681 if (CMD_ARGC
< 2 || CMD_ARGC
> 3)
2683 command_print(CMD_CTX
, "usage: bp <address> <length> ['hw']");
2684 return ERROR_COMMAND_SYNTAX_ERROR
;
2688 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], addr
);
2690 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], length
);
2695 if (strcmp(CMD_ARGV
[2], "hw") == 0)
2698 return ERROR_COMMAND_SYNTAX_ERROR
;
2701 return handle_bp_command_set(CMD_CTX
, addr
, length
, hw
);
2704 COMMAND_HANDLER(handle_rbp_command
)
2707 return ERROR_COMMAND_SYNTAX_ERROR
;
2710 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], addr
);
2712 struct target
*target
= get_current_target(CMD_CTX
);
2713 breakpoint_remove(target
, addr
);
2718 COMMAND_HANDLER(handle_wp_command
)
2720 struct target
*target
= get_current_target(CMD_CTX
);
2724 struct watchpoint
*watchpoint
= target
->watchpoints
;
2728 command_print(CMD_CTX
, "address: 0x%8.8" PRIx32
2729 ", len: 0x%8.8" PRIx32
2730 ", r/w/a: %i, value: 0x%8.8" PRIx32
2731 ", mask: 0x%8.8" PRIx32
,
2732 watchpoint
->address
,
2734 (int)watchpoint
->rw
,
2737 watchpoint
= watchpoint
->next
;
2742 enum watchpoint_rw type
= WPT_ACCESS
;
2744 uint32_t length
= 0;
2745 uint32_t data_value
= 0x0;
2746 uint32_t data_mask
= 0xffffffff;
2751 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[4], data_mask
);
2754 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[3], data_value
);
2757 switch (CMD_ARGV
[2][0])
2769 LOG_ERROR("invalid watchpoint mode ('%c')", CMD_ARGV
[2][0]);
2770 return ERROR_COMMAND_SYNTAX_ERROR
;
2774 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], length
);
2775 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], addr
);
2779 command_print(CMD_CTX
, "usage: wp [address length "
2780 "[(r|w|a) [value [mask]]]]");
2781 return ERROR_COMMAND_SYNTAX_ERROR
;
2784 int retval
= watchpoint_add(target
, addr
, length
, type
,
2785 data_value
, data_mask
);
2786 if (ERROR_OK
!= retval
)
2787 LOG_ERROR("Failure setting watchpoints");
2792 COMMAND_HANDLER(handle_rwp_command
)
2795 return ERROR_COMMAND_SYNTAX_ERROR
;
2798 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], addr
);
2800 struct target
*target
= get_current_target(CMD_CTX
);
2801 watchpoint_remove(target
, addr
);
2808 * Translate a virtual address to a physical address.
2810 * The low-level target implementation must have logged a detailed error
2811 * which is forwarded to telnet/GDB session.
2813 COMMAND_HANDLER(handle_virt2phys_command
)
2816 return ERROR_COMMAND_SYNTAX_ERROR
;
2819 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], va
);
2822 struct target
*target
= get_current_target(CMD_CTX
);
2823 int retval
= target
->type
->virt2phys(target
, va
, &pa
);
2824 if (retval
== ERROR_OK
)
2825 command_print(CMD_CTX
, "Physical address 0x%08" PRIx32
"", pa
);
2830 static void writeData(FILE *f
, const void *data
, size_t len
)
2832 size_t written
= fwrite(data
, 1, len
, f
);
2834 LOG_ERROR("failed to write %zu bytes: %s", len
, strerror(errno
));
2837 static void writeLong(FILE *f
, int l
)
2840 for (i
= 0; i
< 4; i
++)
2842 char c
= (l
>> (i
*8))&0xff;
2843 writeData(f
, &c
, 1);
2848 static void writeString(FILE *f
, char *s
)
2850 writeData(f
, s
, strlen(s
));
2853 /* Dump a gmon.out histogram file. */
2854 static void writeGmon(uint32_t *samples
, uint32_t sampleNum
, const char *filename
)
2857 FILE *f
= fopen(filename
, "w");
2860 writeString(f
, "gmon");
2861 writeLong(f
, 0x00000001); /* Version */
2862 writeLong(f
, 0); /* padding */
2863 writeLong(f
, 0); /* padding */
2864 writeLong(f
, 0); /* padding */
2866 uint8_t zero
= 0; /* GMON_TAG_TIME_HIST */
2867 writeData(f
, &zero
, 1);
2869 /* figure out bucket size */
2870 uint32_t min
= samples
[0];
2871 uint32_t max
= samples
[0];
2872 for (i
= 0; i
< sampleNum
; i
++)
2874 if (min
> samples
[i
])
2878 if (max
< samples
[i
])
2884 int addressSpace
= (max
-min
+ 1);
2886 static const uint32_t maxBuckets
= 256 * 1024; /* maximum buckets. */
2887 uint32_t length
= addressSpace
;
2888 if (length
> maxBuckets
)
2890 length
= maxBuckets
;
2892 int *buckets
= malloc(sizeof(int)*length
);
2893 if (buckets
== NULL
)
2898 memset(buckets
, 0, sizeof(int)*length
);
2899 for (i
= 0; i
< sampleNum
;i
++)
2901 uint32_t address
= samples
[i
];
2902 long long a
= address
-min
;
2903 long long b
= length
-1;
2904 long long c
= addressSpace
-1;
2905 int index
= (a
*b
)/c
; /* danger!!!! int32 overflows */
2909 /* append binary memory gmon.out &profile_hist_hdr ((char*)&profile_hist_hdr + sizeof(struct gmon_hist_hdr)) */
2910 writeLong(f
, min
); /* low_pc */
2911 writeLong(f
, max
); /* high_pc */
2912 writeLong(f
, length
); /* # of samples */
2913 writeLong(f
, 64000000); /* 64MHz */
2914 writeString(f
, "seconds");
2915 for (i
= 0; i
< (15-strlen("seconds")); i
++)
2916 writeData(f
, &zero
, 1);
2917 writeString(f
, "s");
2919 /*append binary memory gmon.out profile_hist_data (profile_hist_data + profile_hist_hdr.hist_size) */
2921 char *data
= malloc(2*length
);
2924 for (i
= 0; i
< length
;i
++)
2933 data
[i
*2 + 1]=(val
>> 8)&0xff;
2936 writeData(f
, data
, length
* 2);
2946 /* profiling samples the CPU PC as quickly as OpenOCD is able, which will be used as a random sampling of PC */
2947 COMMAND_HANDLER(handle_profile_command
)
2949 struct target
*target
= get_current_target(CMD_CTX
);
2950 struct timeval timeout
, now
;
2952 gettimeofday(&timeout
, NULL
);
2955 return ERROR_COMMAND_SYNTAX_ERROR
;
2958 COMMAND_PARSE_NUMBER(uint
, CMD_ARGV
[0], offset
);
2960 timeval_add_time(&timeout
, offset
, 0);
2962 command_print(CMD_CTX
, "Starting profiling. Halting and resuming the target as often as we can...");
2964 static const int maxSample
= 10000;
2965 uint32_t *samples
= malloc(sizeof(uint32_t)*maxSample
);
2966 if (samples
== NULL
)
2970 /* hopefully it is safe to cache! We want to stop/restart as quickly as possible. */
2971 struct reg
*reg
= register_get_by_name(target
->reg_cache
, "pc", 1);
2976 target_poll(target
);
2977 if (target
->state
== TARGET_HALTED
)
2979 uint32_t t
=*((uint32_t *)reg
->value
);
2980 samples
[numSamples
++]=t
;
2981 retval
= target_resume(target
, 1, 0, 0, 0); /* current pc, addr = 0, do not handle breakpoints, not debugging */
2982 target_poll(target
);
2983 alive_sleep(10); /* sleep 10ms, i.e. <100 samples/second. */
2984 } else if (target
->state
== TARGET_RUNNING
)
2986 /* We want to quickly sample the PC. */
2987 if ((retval
= target_halt(target
)) != ERROR_OK
)
2994 command_print(CMD_CTX
, "Target not halted or running");
2998 if (retval
!= ERROR_OK
)
3003 gettimeofday(&now
, NULL
);
3004 if ((numSamples
>= maxSample
) || ((now
.tv_sec
>= timeout
.tv_sec
) && (now
.tv_usec
>= timeout
.tv_usec
)))
3006 command_print(CMD_CTX
, "Profiling completed. %d samples.", numSamples
);
3007 if ((retval
= target_poll(target
)) != ERROR_OK
)
3012 if (target
->state
== TARGET_HALTED
)
3014 target_resume(target
, 1, 0, 0, 0); /* current pc, addr = 0, do not handle breakpoints, not debugging */
3016 if ((retval
= target_poll(target
)) != ERROR_OK
)
3021 writeGmon(samples
, numSamples
, CMD_ARGV
[1]);
3022 command_print(CMD_CTX
, "Wrote %s", CMD_ARGV
[1]);
3031 static int new_int_array_element(Jim_Interp
* interp
, const char *varname
, int idx
, uint32_t val
)
3034 Jim_Obj
*nameObjPtr
, *valObjPtr
;
3037 namebuf
= alloc_printf("%s(%d)", varname
, idx
);
3041 nameObjPtr
= Jim_NewStringObj(interp
, namebuf
, -1);
3042 valObjPtr
= Jim_NewIntObj(interp
, val
);
3043 if (!nameObjPtr
|| !valObjPtr
)
3049 Jim_IncrRefCount(nameObjPtr
);
3050 Jim_IncrRefCount(valObjPtr
);
3051 result
= Jim_SetVariable(interp
, nameObjPtr
, valObjPtr
);
3052 Jim_DecrRefCount(interp
, nameObjPtr
);
3053 Jim_DecrRefCount(interp
, valObjPtr
);
3055 /* printf("%s(%d) <= 0%08x\n", varname, idx, val); */
3059 static int jim_mem2array(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
3061 struct command_context
*context
;
3062 struct target
*target
;
3064 context
= Jim_GetAssocData(interp
, "context");
3065 if (context
== NULL
)
3067 LOG_ERROR("mem2array: no command context");
3070 target
= get_current_target(context
);
3073 LOG_ERROR("mem2array: no current target");
3077 return target_mem2array(interp
, target
, argc
-1, argv
+ 1);
3080 static int target_mem2array(Jim_Interp
*interp
, struct target
*target
, int argc
, Jim_Obj
*const *argv
)
3088 const char *varname
;
3092 /* argv[1] = name of array to receive the data
3093 * argv[2] = desired width
3094 * argv[3] = memory address
3095 * argv[4] = count of times to read
3098 Jim_WrongNumArgs(interp
, 1, argv
, "varname width addr nelems");
3101 varname
= Jim_GetString(argv
[0], &len
);
3102 /* given "foo" get space for worse case "foo(%d)" .. add 20 */
3104 e
= Jim_GetLong(interp
, argv
[1], &l
);
3110 e
= Jim_GetLong(interp
, argv
[2], &l
);
3115 e
= Jim_GetLong(interp
, argv
[3], &l
);
3131 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3132 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "Invalid width param, must be 8/16/32", NULL
);
3136 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3137 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "mem2array: zero width read?", NULL
);
3140 if ((addr
+ (len
* width
)) < addr
) {
3141 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3142 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "mem2array: addr + len - wraps to zero?", NULL
);
3145 /* absurd transfer size? */
3147 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3148 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "mem2array: absurd > 64K item request", NULL
);
3153 ((width
== 2) && ((addr
& 1) == 0)) ||
3154 ((width
== 4) && ((addr
& 3) == 0))) {
3158 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3159 sprintf(buf
, "mem2array address: 0x%08" PRIx32
" is not aligned for %" PRId32
" byte reads",
3162 Jim_AppendStrings(interp
, Jim_GetResult(interp
), buf
, NULL
);
3171 size_t buffersize
= 4096;
3172 uint8_t *buffer
= malloc(buffersize
);
3179 /* Slurp... in buffer size chunks */
3181 count
= len
; /* in objects.. */
3182 if (count
> (buffersize
/width
)) {
3183 count
= (buffersize
/width
);
3186 retval
= target_read_memory(target
, addr
, width
, count
, buffer
);
3187 if (retval
!= ERROR_OK
) {
3189 LOG_ERROR("mem2array: Read @ 0x%08x, w=%d, cnt=%d, failed",
3193 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3194 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "mem2array: cannot read memory", NULL
);
3198 v
= 0; /* shut up gcc */
3199 for (i
= 0 ;i
< count
;i
++, n
++) {
3202 v
= target_buffer_get_u32(target
, &buffer
[i
*width
]);
3205 v
= target_buffer_get_u16(target
, &buffer
[i
*width
]);
3208 v
= buffer
[i
] & 0x0ff;
3211 new_int_array_element(interp
, varname
, n
, v
);
3219 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3224 static int get_int_array_element(Jim_Interp
* interp
, const char *varname
, int idx
, uint32_t *val
)
3227 Jim_Obj
*nameObjPtr
, *valObjPtr
;
3231 namebuf
= alloc_printf("%s(%d)", varname
, idx
);
3235 nameObjPtr
= Jim_NewStringObj(interp
, namebuf
, -1);
3242 Jim_IncrRefCount(nameObjPtr
);
3243 valObjPtr
= Jim_GetVariable(interp
, nameObjPtr
, JIM_ERRMSG
);
3244 Jim_DecrRefCount(interp
, nameObjPtr
);
3246 if (valObjPtr
== NULL
)
3249 result
= Jim_GetLong(interp
, valObjPtr
, &l
);
3250 /* printf("%s(%d) => 0%08x\n", varname, idx, val); */
3255 static int jim_array2mem(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
3257 struct command_context
*context
;
3258 struct target
*target
;
3260 context
= Jim_GetAssocData(interp
, "context");
3261 if (context
== NULL
) {
3262 LOG_ERROR("array2mem: no command context");
3265 target
= get_current_target(context
);
3266 if (target
== NULL
) {
3267 LOG_ERROR("array2mem: no current target");
3271 return target_array2mem(interp
,target
, argc
-1, argv
+ 1);
3273 static int target_array2mem(Jim_Interp
*interp
, struct target
*target
, int argc
, Jim_Obj
*const *argv
)
3281 const char *varname
;
3285 /* argv[1] = name of array to get the data
3286 * argv[2] = desired width
3287 * argv[3] = memory address
3288 * argv[4] = count to write
3291 Jim_WrongNumArgs(interp
, 0, argv
, "varname width addr nelems");
3294 varname
= Jim_GetString(argv
[0], &len
);
3295 /* given "foo" get space for worse case "foo(%d)" .. add 20 */
3297 e
= Jim_GetLong(interp
, argv
[1], &l
);
3303 e
= Jim_GetLong(interp
, argv
[2], &l
);
3308 e
= Jim_GetLong(interp
, argv
[3], &l
);
3324 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3325 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "Invalid width param, must be 8/16/32", NULL
);
3329 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3330 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "array2mem: zero width read?", NULL
);
3333 if ((addr
+ (len
* width
)) < addr
) {
3334 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3335 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "array2mem: addr + len - wraps to zero?", NULL
);
3338 /* absurd transfer size? */
3340 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3341 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "array2mem: absurd > 64K item request", NULL
);
3346 ((width
== 2) && ((addr
& 1) == 0)) ||
3347 ((width
== 4) && ((addr
& 3) == 0))) {
3351 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3352 sprintf(buf
, "array2mem address: 0x%08x is not aligned for %d byte reads",
3355 Jim_AppendStrings(interp
, Jim_GetResult(interp
), buf
, NULL
);
3366 size_t buffersize
= 4096;
3367 uint8_t *buffer
= malloc(buffersize
);
3372 /* Slurp... in buffer size chunks */
3374 count
= len
; /* in objects.. */
3375 if (count
> (buffersize
/width
)) {
3376 count
= (buffersize
/width
);
3379 v
= 0; /* shut up gcc */
3380 for (i
= 0 ;i
< count
;i
++, n
++) {
3381 get_int_array_element(interp
, varname
, n
, &v
);
3384 target_buffer_set_u32(target
, &buffer
[i
*width
], v
);
3387 target_buffer_set_u16(target
, &buffer
[i
*width
], v
);
3390 buffer
[i
] = v
& 0x0ff;
3396 retval
= target_write_memory(target
, addr
, width
, count
, buffer
);
3397 if (retval
!= ERROR_OK
) {
3399 LOG_ERROR("array2mem: Write @ 0x%08x, w=%d, cnt=%d, failed",
3403 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3404 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "array2mem: cannot read memory", NULL
);
3412 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3417 void target_all_handle_event(enum target_event e
)
3419 struct target
*target
;
3421 LOG_DEBUG("**all*targets: event: %d, %s",
3423 Jim_Nvp_value2name_simple(nvp_target_event
, e
)->name
);
3425 target
= all_targets
;
3427 target_handle_event(target
, e
);
3428 target
= target
->next
;
3433 /* FIX? should we propagate errors here rather than printing them
3436 void target_handle_event(struct target
*target
, enum target_event e
)
3438 struct target_event_action
*teap
;
3440 for (teap
= target
->event_action
; teap
!= NULL
; teap
= teap
->next
) {
3441 if (teap
->event
== e
) {
3442 LOG_DEBUG("target: (%d) %s (%s) event: %d (%s) action: %s",
3443 target
->target_number
,
3444 target_name(target
),
3445 target_type_name(target
),
3447 Jim_Nvp_value2name_simple(nvp_target_event
, e
)->name
,
3448 Jim_GetString(teap
->body
, NULL
));
3449 if (Jim_EvalObj(teap
->interp
, teap
->body
) != JIM_OK
)
3451 Jim_PrintErrorMessage(teap
->interp
);
3458 * Returns true only if the target has a handler for the specified event.
3460 bool target_has_event_action(struct target
*target
, enum target_event event
)
3462 struct target_event_action
*teap
;
3464 for (teap
= target
->event_action
; teap
!= NULL
; teap
= teap
->next
) {
3465 if (teap
->event
== event
)
3471 enum target_cfg_param
{
3474 TCFG_WORK_AREA_VIRT
,
3475 TCFG_WORK_AREA_PHYS
,
3476 TCFG_WORK_AREA_SIZE
,
3477 TCFG_WORK_AREA_BACKUP
,
3480 TCFG_CHAIN_POSITION
,
3483 static Jim_Nvp nvp_config_opts
[] = {
3484 { .name
= "-type", .value
= TCFG_TYPE
},
3485 { .name
= "-event", .value
= TCFG_EVENT
},
3486 { .name
= "-work-area-virt", .value
= TCFG_WORK_AREA_VIRT
},
3487 { .name
= "-work-area-phys", .value
= TCFG_WORK_AREA_PHYS
},
3488 { .name
= "-work-area-size", .value
= TCFG_WORK_AREA_SIZE
},
3489 { .name
= "-work-area-backup", .value
= TCFG_WORK_AREA_BACKUP
},
3490 { .name
= "-endian" , .value
= TCFG_ENDIAN
},
3491 { .name
= "-variant", .value
= TCFG_VARIANT
},
3492 { .name
= "-chain-position", .value
= TCFG_CHAIN_POSITION
},
3494 { .name
= NULL
, .value
= -1 }
3497 static int target_configure(Jim_GetOptInfo
*goi
, struct target
*target
)
3505 /* parse config or cget options ... */
3506 while (goi
->argc
> 0) {
3507 Jim_SetEmptyResult(goi
->interp
);
3508 /* Jim_GetOpt_Debug(goi); */
3510 if (target
->type
->target_jim_configure
) {
3511 /* target defines a configure function */
3512 /* target gets first dibs on parameters */
3513 e
= (*(target
->type
->target_jim_configure
))(target
, goi
);
3522 /* otherwise we 'continue' below */
3524 e
= Jim_GetOpt_Nvp(goi
, nvp_config_opts
, &n
);
3526 Jim_GetOpt_NvpUnknown(goi
, nvp_config_opts
, 0);
3532 if (goi
->isconfigure
) {
3533 Jim_SetResult_sprintf(goi
->interp
,
3534 "not settable: %s", n
->name
);
3538 if (goi
->argc
!= 0) {
3539 Jim_WrongNumArgs(goi
->interp
,
3540 goi
->argc
, goi
->argv
,
3545 Jim_SetResultString(goi
->interp
,
3546 target_type_name(target
), -1);
3550 if (goi
->argc
== 0) {
3551 Jim_WrongNumArgs(goi
->interp
, goi
->argc
, goi
->argv
, "-event ?event-name? ...");
3555 e
= Jim_GetOpt_Nvp(goi
, nvp_target_event
, &n
);
3557 Jim_GetOpt_NvpUnknown(goi
, nvp_target_event
, 1);
3561 if (goi
->isconfigure
) {
3562 if (goi
->argc
!= 1) {
3563 Jim_WrongNumArgs(goi
->interp
, goi
->argc
, goi
->argv
, "-event ?event-name? ?EVENT-BODY?");
3567 if (goi
->argc
!= 0) {
3568 Jim_WrongNumArgs(goi
->interp
, goi
->argc
, goi
->argv
, "-event ?event-name?");
3574 struct target_event_action
*teap
;
3576 teap
= target
->event_action
;
3577 /* replace existing? */
3579 if (teap
->event
== (enum target_event
)n
->value
) {
3585 if (goi
->isconfigure
) {
3586 bool replace
= true;
3589 teap
= calloc(1, sizeof(*teap
));
3592 teap
->event
= n
->value
;
3593 teap
->interp
= goi
->interp
;
3594 Jim_GetOpt_Obj(goi
, &o
);
3596 Jim_DecrRefCount(teap
->interp
, teap
->body
);
3598 teap
->body
= Jim_DuplicateObj(goi
->interp
, o
);
3601 * Tcl/TK - "tk events" have a nice feature.
3602 * See the "BIND" command.
3603 * We should support that here.
3604 * You can specify %X and %Y in the event code.
3605 * The idea is: %T - target name.
3606 * The idea is: %N - target number
3607 * The idea is: %E - event name.
3609 Jim_IncrRefCount(teap
->body
);
3613 /* add to head of event list */
3614 teap
->next
= target
->event_action
;
3615 target
->event_action
= teap
;
3617 Jim_SetEmptyResult(goi
->interp
);
3621 Jim_SetEmptyResult(goi
->interp
);
3623 Jim_SetResult(goi
->interp
, Jim_DuplicateObj(goi
->interp
, teap
->body
));
3630 case TCFG_WORK_AREA_VIRT
:
3631 if (goi
->isconfigure
) {
3632 target_free_all_working_areas(target
);
3633 e
= Jim_GetOpt_Wide(goi
, &w
);
3637 target
->working_area_virt
= w
;
3638 target
->working_area_virt_spec
= true;
3640 if (goi
->argc
!= 0) {
3644 Jim_SetResult(goi
->interp
, Jim_NewIntObj(goi
->interp
, target
->working_area_virt
));
3648 case TCFG_WORK_AREA_PHYS
:
3649 if (goi
->isconfigure
) {
3650 target_free_all_working_areas(target
);
3651 e
= Jim_GetOpt_Wide(goi
, &w
);
3655 target
->working_area_phys
= w
;
3656 target
->working_area_phys_spec
= true;
3658 if (goi
->argc
!= 0) {
3662 Jim_SetResult(goi
->interp
, Jim_NewIntObj(goi
->interp
, target
->working_area_phys
));
3666 case TCFG_WORK_AREA_SIZE
:
3667 if (goi
->isconfigure
) {
3668 target_free_all_working_areas(target
);
3669 e
= Jim_GetOpt_Wide(goi
, &w
);
3673 target
->working_area_size
= w
;
3675 if (goi
->argc
!= 0) {
3679 Jim_SetResult(goi
->interp
, Jim_NewIntObj(goi
->interp
, target
->working_area_size
));
3683 case TCFG_WORK_AREA_BACKUP
:
3684 if (goi
->isconfigure
) {
3685 target_free_all_working_areas(target
);
3686 e
= Jim_GetOpt_Wide(goi
, &w
);
3690 /* make this exactly 1 or 0 */
3691 target
->backup_working_area
= (!!w
);
3693 if (goi
->argc
!= 0) {
3697 Jim_SetResult(goi
->interp
, Jim_NewIntObj(goi
->interp
, target
->backup_working_area
));
3698 /* loop for more e*/
3702 if (goi
->isconfigure
) {
3703 e
= Jim_GetOpt_Nvp(goi
, nvp_target_endian
, &n
);
3705 Jim_GetOpt_NvpUnknown(goi
, nvp_target_endian
, 1);
3708 target
->endianness
= n
->value
;
3710 if (goi
->argc
!= 0) {
3714 n
= Jim_Nvp_value2name_simple(nvp_target_endian
, target
->endianness
);
3715 if (n
->name
== NULL
) {
3716 target
->endianness
= TARGET_LITTLE_ENDIAN
;
3717 n
= Jim_Nvp_value2name_simple(nvp_target_endian
, target
->endianness
);
3719 Jim_SetResultString(goi
->interp
, n
->name
, -1);
3724 if (goi
->isconfigure
) {
3725 if (goi
->argc
< 1) {
3726 Jim_SetResult_sprintf(goi
->interp
,
3731 if (target
->variant
) {
3732 free((void *)(target
->variant
));
3734 e
= Jim_GetOpt_String(goi
, &cp
, NULL
);
3735 target
->variant
= strdup(cp
);
3737 if (goi
->argc
!= 0) {
3741 Jim_SetResultString(goi
->interp
, target
->variant
,-1);
3744 case TCFG_CHAIN_POSITION
:
3745 if (goi
->isconfigure
) {
3747 struct jtag_tap
*tap
;
3748 target_free_all_working_areas(target
);
3749 e
= Jim_GetOpt_Obj(goi
, &o
);
3753 tap
= jtag_tap_by_jim_obj(goi
->interp
, o
);
3757 /* make this exactly 1 or 0 */
3760 if (goi
->argc
!= 0) {
3764 Jim_SetResultString(goi
->interp
, target
->tap
->dotted_name
, -1);
3765 /* loop for more e*/
3768 } /* while (goi->argc) */
3771 /* done - we return */
3775 static int jim_target_configure(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
3778 Jim_GetOpt_Setup(&goi
, interp
, argc
- 1, argv
+ 1);
3779 goi
.isconfigure
= strcmp(Jim_GetString(argv
[0], NULL
), "configure") == 0;
3780 int need_args
= 1 + goi
.isconfigure
;
3781 if (goi
.argc
< need_args
)
3783 Jim_WrongNumArgs(goi
.interp
, goi
.argc
, goi
.argv
,
3785 ? "missing: -option VALUE ..."
3786 : "missing: -option ...");
3789 struct target
*target
= Jim_CmdPrivData(goi
.interp
);
3790 return target_configure(&goi
, target
);
3793 static int jim_target_mw(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
3795 const char *cmd_name
= Jim_GetString(argv
[0], NULL
);
3798 Jim_GetOpt_Setup(&goi
, interp
, argc
- 1, argv
+ 1);
3800 if (goi
.argc
!= 2 && goi
.argc
!= 3)
3802 Jim_SetResult_sprintf(goi
.interp
,
3803 "usage: %s <address> <data> [<count>]", cmd_name
);
3808 int e
= Jim_GetOpt_Wide(&goi
, &a
);
3813 e
= Jim_GetOpt_Wide(&goi
, &b
);
3820 e
= Jim_GetOpt_Wide(&goi
, &c
);
3825 struct target
*target
= Jim_CmdPrivData(goi
.interp
);
3826 uint8_t target_buf
[32];
3827 if (strcasecmp(cmd_name
, "mww") == 0) {
3828 target_buffer_set_u32(target
, target_buf
, b
);
3831 else if (strcasecmp(cmd_name
, "mwh") == 0) {
3832 target_buffer_set_u16(target
, target_buf
, b
);
3835 else if (strcasecmp(cmd_name
, "mwb") == 0) {
3836 target_buffer_set_u8(target
, target_buf
, b
);
3839 LOG_ERROR("command '%s' unknown: ", cmd_name
);
3843 for (jim_wide x
= 0; x
< c
; x
++)
3845 e
= target_write_memory(target
, a
, b
, 1, target_buf
);
3848 Jim_SetResult_sprintf(interp
,
3849 "Error writing @ 0x%08x: %d\n", (int)(a
), e
);
3858 static int jim_target_md(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
3860 const char *cmd_name
= Jim_GetString(argv
[0], NULL
);
3863 Jim_GetOpt_Setup(&goi
, interp
, argc
- 1, argv
+ 1);
3865 if ((goi
.argc
== 2) || (goi
.argc
== 3))
3867 Jim_SetResult_sprintf(goi
.interp
,
3868 "usage: %s <address> [<count>]", cmd_name
);
3873 int e
= Jim_GetOpt_Wide(&goi
, &a
);
3879 e
= Jim_GetOpt_Wide(&goi
, &c
);
3886 jim_wide b
= 1; /* shut up gcc */
3887 if (strcasecmp(cmd_name
, "mdw") == 0)
3889 else if (strcasecmp(cmd_name
, "mdh") == 0)
3891 else if (strcasecmp(cmd_name
, "mdb") == 0)
3894 LOG_ERROR("command '%s' unknown: ", cmd_name
);
3898 /* convert count to "bytes" */
3901 struct target
*target
= Jim_CmdPrivData(goi
.interp
);
3902 uint8_t target_buf
[32];
3909 e
= target_read_memory(target
, a
, b
, y
/ b
, target_buf
);
3910 if (e
!= ERROR_OK
) {
3911 Jim_SetResult_sprintf(interp
, "error reading target @ 0x%08lx", (int)(a
));
3915 Jim_fprintf(interp
, interp
->cookie_stdout
, "0x%08x ", (int)(a
));
3918 for (x
= 0; x
< 16 && x
< y
; x
+= 4)
3920 z
= target_buffer_get_u32(target
, &(target_buf
[ x
* 4 ]));
3921 Jim_fprintf(interp
, interp
->cookie_stdout
, "%08x ", (int)(z
));
3923 for (; (x
< 16) ; x
+= 4) {
3924 Jim_fprintf(interp
, interp
->cookie_stdout
, " ");
3928 for (x
= 0; x
< 16 && x
< y
; x
+= 2)
3930 z
= target_buffer_get_u16(target
, &(target_buf
[ x
* 2 ]));
3931 Jim_fprintf(interp
, interp
->cookie_stdout
, "%04x ", (int)(z
));
3933 for (; (x
< 16) ; x
+= 2) {
3934 Jim_fprintf(interp
, interp
->cookie_stdout
, " ");
3939 for (x
= 0 ; (x
< 16) && (x
< y
) ; x
+= 1) {
3940 z
= target_buffer_get_u8(target
, &(target_buf
[ x
* 4 ]));
3941 Jim_fprintf(interp
, interp
->cookie_stdout
, "%02x ", (int)(z
));
3943 for (; (x
< 16) ; x
+= 1) {
3944 Jim_fprintf(interp
, interp
->cookie_stdout
, " ");
3948 /* ascii-ify the bytes */
3949 for (x
= 0 ; x
< y
; x
++) {
3950 if ((target_buf
[x
] >= 0x20) &&
3951 (target_buf
[x
] <= 0x7e)) {
3955 target_buf
[x
] = '.';
3960 target_buf
[x
] = ' ';
3965 /* print - with a newline */
3966 Jim_fprintf(interp
, interp
->cookie_stdout
, "%s\n", target_buf
);
3974 static int jim_target_mem2array(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
3976 struct target
*target
= Jim_CmdPrivData(interp
);
3977 return target_mem2array(interp
, target
, argc
- 1, argv
+ 1);
3980 static int jim_target_array2mem(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
3982 struct target
*target
= Jim_CmdPrivData(interp
);
3983 return target_array2mem(interp
, target
, argc
- 1, argv
+ 1);
3986 static int jim_target_tap_disabled(Jim_Interp
*interp
)
3988 Jim_SetResult_sprintf(interp
, "[TAP is disabled]");
3992 static int jim_target_examine(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
3996 Jim_WrongNumArgs(interp
, 1, argv
, "[no parameters]");
3999 struct target
*target
= Jim_CmdPrivData(interp
);
4000 if (!target
->tap
->enabled
)
4001 return jim_target_tap_disabled(interp
);
4003 int e
= target
->type
->examine(target
);
4006 Jim_SetResult_sprintf(interp
, "examine-fails: %d", e
);
4012 static int jim_target_poll(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4016 Jim_WrongNumArgs(interp
, 1, argv
, "[no parameters]");
4019 struct target
*target
= Jim_CmdPrivData(interp
);
4020 if (!target
->tap
->enabled
)
4021 return jim_target_tap_disabled(interp
);
4024 if (!(target_was_examined(target
))) {
4025 e
= ERROR_TARGET_NOT_EXAMINED
;
4027 e
= target
->type
->poll(target
);
4031 Jim_SetResult_sprintf(interp
, "poll-fails: %d", e
);
4037 static int jim_target_reset(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4040 Jim_GetOpt_Setup(&goi
, interp
, argc
- 1, argv
+ 1);
4044 Jim_WrongNumArgs(interp
, 0, argv
,
4045 "([tT]|[fF]|assert|deassert) BOOL");
4050 int e
= Jim_GetOpt_Nvp(&goi
, nvp_assert
, &n
);
4053 Jim_GetOpt_NvpUnknown(&goi
, nvp_assert
, 1);
4056 /* the halt or not param */
4058 e
= Jim_GetOpt_Wide(&goi
, &a
);
4062 struct target
*target
= Jim_CmdPrivData(goi
.interp
);
4063 if (!target
->tap
->enabled
)
4064 return jim_target_tap_disabled(interp
);
4065 if (!target
->type
->assert_reset
|| !target
->type
->deassert_reset
)
4067 Jim_SetResult_sprintf(interp
,
4068 "No target-specific reset for %s",
4069 target_name(target
));
4072 /* determine if we should halt or not. */
4073 target
->reset_halt
= !!a
;
4074 /* When this happens - all workareas are invalid. */
4075 target_free_all_working_areas_restore(target
, 0);
4078 if (n
->value
== NVP_ASSERT
) {
4079 e
= target
->type
->assert_reset(target
);
4081 e
= target
->type
->deassert_reset(target
);
4083 return (e
== ERROR_OK
) ? JIM_OK
: JIM_ERR
;
4086 static int jim_target_halt(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4089 Jim_WrongNumArgs(interp
, 1, argv
, "[no parameters]");
4092 struct target
*target
= Jim_CmdPrivData(interp
);
4093 if (!target
->tap
->enabled
)
4094 return jim_target_tap_disabled(interp
);
4095 int e
= target
->type
->halt(target
);
4096 return (e
== ERROR_OK
) ? JIM_OK
: JIM_ERR
;
4099 static int jim_target_wait_state(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4102 Jim_GetOpt_Setup(&goi
, interp
, argc
- 1, argv
+ 1);
4104 /* params: <name> statename timeoutmsecs */
4107 const char *cmd_name
= Jim_GetString(argv
[0], NULL
);
4108 Jim_SetResult_sprintf(goi
.interp
,
4109 "%s <state_name> <timeout_in_msec>", cmd_name
);
4114 int e
= Jim_GetOpt_Nvp(&goi
, nvp_target_state
, &n
);
4116 Jim_GetOpt_NvpUnknown(&goi
, nvp_target_state
,1);
4120 e
= Jim_GetOpt_Wide(&goi
, &a
);
4124 struct target
*target
= Jim_CmdPrivData(interp
);
4125 if (!target
->tap
->enabled
)
4126 return jim_target_tap_disabled(interp
);
4128 e
= target_wait_state(target
, n
->value
, a
);
4131 Jim_SetResult_sprintf(goi
.interp
,
4132 "target: %s wait %s fails (%d) %s",
4133 target_name(target
), n
->name
,
4134 e
, target_strerror_safe(e
));
4139 /* List for human, Events defined for this target.
4140 * scripts/programs should use 'name cget -event NAME'
4142 static int jim_target_event_list(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4144 struct command_context
*cmd_ctx
= Jim_GetAssocData(interp
, "context");
4145 struct target
*target
= Jim_CmdPrivData(interp
);
4146 struct target_event_action
*teap
= target
->event_action
;
4147 command_print(cmd_ctx
, "Event actions for target (%d) %s\n",
4148 target
->target_number
,
4149 target_name(target
));
4150 command_print(cmd_ctx
, "%-25s | Body", "Event");
4151 command_print(cmd_ctx
, "------------------------- | "
4152 "----------------------------------------");
4155 Jim_Nvp
*opt
= Jim_Nvp_value2name_simple(nvp_target_event
, teap
->event
);
4156 command_print(cmd_ctx
, "%-25s | %s",
4157 opt
->name
, Jim_GetString(teap
->body
, NULL
));
4160 command_print(cmd_ctx
, "***END***");
4163 static int jim_target_current_state(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4167 Jim_WrongNumArgs(interp
, 1, argv
, "[no parameters]");
4170 struct target
*target
= Jim_CmdPrivData(interp
);
4171 Jim_SetResultString(interp
, target_state_name(target
), -1);
4174 static int jim_target_invoke_event(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4177 Jim_GetOpt_Setup(&goi
, interp
, argc
- 1, argv
+ 1);
4180 const char *cmd_name
= Jim_GetString(argv
[0], NULL
);
4181 Jim_SetResult_sprintf(goi
.interp
, "%s <eventname>", cmd_name
);
4185 int e
= Jim_GetOpt_Nvp(&goi
, nvp_target_event
, &n
);
4188 Jim_GetOpt_NvpUnknown(&goi
, nvp_target_event
, 1);
4191 struct target
*target
= Jim_CmdPrivData(interp
);
4192 target_handle_event(target
, n
->value
);
4196 static const struct command_registration target_instance_command_handlers
[] = {
4198 .name
= "configure",
4199 .mode
= COMMAND_CONFIG
,
4200 .jim_handler
= &jim_target_configure
,
4201 .usage
= "[<target_options> ...]",
4202 .help
= "configure a new target for use",
4206 .mode
= COMMAND_ANY
,
4207 .jim_handler
= &jim_target_configure
,
4208 .usage
= "<target_type> [<target_options> ...]",
4209 .help
= "configure a new target for use",
4213 .mode
= COMMAND_EXEC
,
4214 .jim_handler
= &jim_target_mw
,
4215 .usage
= "<address> <data> [<count>]",
4216 .help
= "Write 32-bit word(s) to target memory",
4220 .mode
= COMMAND_EXEC
,
4221 .jim_handler
= &jim_target_mw
,
4222 .usage
= "<address> <data> [<count>]",
4223 .help
= "Write 16-bit half-word(s) to target memory",
4227 .mode
= COMMAND_EXEC
,
4228 .jim_handler
= &jim_target_mw
,
4229 .usage
= "<address> <data> [<count>]",
4230 .help
= "Write byte(s) to target memory",
4234 .mode
= COMMAND_EXEC
,
4235 .jim_handler
= &jim_target_md
,
4236 .usage
= "<address> [<count>]",
4237 .help
= "Display target memory as 32-bit words",
4241 .mode
= COMMAND_EXEC
,
4242 .jim_handler
= &jim_target_md
,
4243 .usage
= "<address> [<count>]",
4244 .help
= "Display target memory as 16-bit half-words",
4248 .mode
= COMMAND_EXEC
,
4249 .jim_handler
= &jim_target_md
,
4250 .usage
= "<address> [<count>]",
4251 .help
= "Display target memory as 8-bit bytes",
4254 .name
= "array2mem",
4255 .mode
= COMMAND_EXEC
,
4256 .jim_handler
= &jim_target_array2mem
,
4259 .name
= "mem2array",
4260 .mode
= COMMAND_EXEC
,
4261 .jim_handler
= &jim_target_mem2array
,
4264 .name
= "eventlist",
4265 .mode
= COMMAND_EXEC
,
4266 .jim_handler
= &jim_target_event_list
,
4270 .mode
= COMMAND_EXEC
,
4271 .jim_handler
= &jim_target_current_state
,
4274 .name
= "arp_examine",
4275 .mode
= COMMAND_EXEC
,
4276 .jim_handler
= &jim_target_examine
,
4280 .mode
= COMMAND_EXEC
,
4281 .jim_handler
= &jim_target_poll
,
4284 .name
= "arp_reset",
4285 .mode
= COMMAND_EXEC
,
4286 .jim_handler
= &jim_target_reset
,
4290 .mode
= COMMAND_EXEC
,
4291 .jim_handler
= &jim_target_halt
,
4294 .name
= "arp_waitstate",
4295 .mode
= COMMAND_EXEC
,
4296 .jim_handler
= &jim_target_wait_state
,
4299 .name
= "invoke-event",
4300 .mode
= COMMAND_EXEC
,
4301 .jim_handler
= &jim_target_invoke_event
,
4303 COMMAND_REGISTRATION_DONE
4306 static int target_create(Jim_GetOptInfo
*goi
)
4314 struct target
*target
;
4315 struct command_context
*cmd_ctx
;
4317 cmd_ctx
= Jim_GetAssocData(goi
->interp
, "context");
4318 if (goi
->argc
< 3) {
4319 Jim_WrongNumArgs(goi
->interp
, 1, goi
->argv
, "?name? ?type? ..options...");
4324 Jim_GetOpt_Obj(goi
, &new_cmd
);
4325 /* does this command exist? */
4326 cmd
= Jim_GetCommand(goi
->interp
, new_cmd
, JIM_ERRMSG
);
4328 cp
= Jim_GetString(new_cmd
, NULL
);
4329 Jim_SetResult_sprintf(goi
->interp
, "Command/target: %s Exists", cp
);
4334 e
= Jim_GetOpt_String(goi
, &cp2
, NULL
);
4336 /* now does target type exist */
4337 for (x
= 0 ; target_types
[x
] ; x
++) {
4338 if (0 == strcmp(cp
, target_types
[x
]->name
)) {
4343 if (target_types
[x
] == NULL
) {
4344 Jim_SetResult_sprintf(goi
->interp
, "Unknown target type %s, try one of ", cp
);
4345 for (x
= 0 ; target_types
[x
] ; x
++) {
4346 if (target_types
[x
+ 1]) {
4347 Jim_AppendStrings(goi
->interp
,
4348 Jim_GetResult(goi
->interp
),
4349 target_types
[x
]->name
,
4352 Jim_AppendStrings(goi
->interp
,
4353 Jim_GetResult(goi
->interp
),
4355 target_types
[x
]->name
,NULL
);
4362 target
= calloc(1,sizeof(struct target
));
4363 /* set target number */
4364 target
->target_number
= new_target_number();
4366 /* allocate memory for each unique target type */
4367 target
->type
= (struct target_type
*)calloc(1,sizeof(struct target_type
));
4369 memcpy(target
->type
, target_types
[x
], sizeof(struct target_type
));
4371 /* will be set by "-endian" */
4372 target
->endianness
= TARGET_ENDIAN_UNKNOWN
;
4374 target
->working_area
= 0x0;
4375 target
->working_area_size
= 0x0;
4376 target
->working_areas
= NULL
;
4377 target
->backup_working_area
= 0;
4379 target
->state
= TARGET_UNKNOWN
;
4380 target
->debug_reason
= DBG_REASON_UNDEFINED
;
4381 target
->reg_cache
= NULL
;
4382 target
->breakpoints
= NULL
;
4383 target
->watchpoints
= NULL
;
4384 target
->next
= NULL
;
4385 target
->arch_info
= NULL
;
4387 target
->display
= 1;
4389 target
->halt_issued
= false;
4391 /* initialize trace information */
4392 target
->trace_info
= malloc(sizeof(struct trace
));
4393 target
->trace_info
->num_trace_points
= 0;
4394 target
->trace_info
->trace_points_size
= 0;
4395 target
->trace_info
->trace_points
= NULL
;
4396 target
->trace_info
->trace_history_size
= 0;
4397 target
->trace_info
->trace_history
= NULL
;
4398 target
->trace_info
->trace_history_pos
= 0;
4399 target
->trace_info
->trace_history_overflowed
= 0;
4401 target
->dbgmsg
= NULL
;
4402 target
->dbg_msg_enabled
= 0;
4404 target
->endianness
= TARGET_ENDIAN_UNKNOWN
;
4406 /* Do the rest as "configure" options */
4407 goi
->isconfigure
= 1;
4408 e
= target_configure(goi
, target
);
4410 if (target
->tap
== NULL
)
4412 Jim_SetResultString(goi
->interp
, "-chain-position required when creating target", -1);
4422 if (target
->endianness
== TARGET_ENDIAN_UNKNOWN
) {
4423 /* default endian to little if not specified */
4424 target
->endianness
= TARGET_LITTLE_ENDIAN
;
4427 /* incase variant is not set */
4428 if (!target
->variant
)
4429 target
->variant
= strdup("");
4431 cp
= Jim_GetString(new_cmd
, NULL
);
4432 target
->cmd_name
= strdup(cp
);
4434 /* create the target specific commands */
4435 if (target
->type
->commands
) {
4436 e
= register_commands(cmd_ctx
, NULL
, target
->type
->commands
);
4438 LOG_ERROR("unable to register '%s' commands", cp
);
4440 if (target
->type
->target_create
) {
4441 (*(target
->type
->target_create
))(target
, goi
->interp
);
4444 /* append to end of list */
4446 struct target
**tpp
;
4447 tpp
= &(all_targets
);
4449 tpp
= &((*tpp
)->next
);
4454 /* now - create the new target name command */
4455 const const struct command_registration target_subcommands
[] = {
4457 .chain
= target_instance_command_handlers
,
4460 .chain
= target
->type
->commands
,
4462 COMMAND_REGISTRATION_DONE
4464 const const struct command_registration target_commands
[] = {
4467 .mode
= COMMAND_ANY
,
4468 .help
= "target command group",
4469 .chain
= target_subcommands
,
4471 COMMAND_REGISTRATION_DONE
4473 e
= register_commands(cmd_ctx
, NULL
, target_commands
);
4477 struct command
*c
= command_find_in_context(cmd_ctx
, cp
);
4479 command_set_handler_data(c
, target
);
4481 return (ERROR_OK
== e
) ? JIM_OK
: JIM_ERR
;
4484 static int jim_target_current(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4488 Jim_WrongNumArgs(interp
, 1, argv
, "Too many parameters");
4491 struct command_context
*cmd_ctx
= Jim_GetAssocData(interp
, "context");
4492 Jim_SetResultString(interp
, get_current_target(cmd_ctx
)->cmd_name
, -1);
4496 static int jim_target_types(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4500 Jim_WrongNumArgs(interp
, 1, argv
, "Too many parameters");
4503 Jim_SetResult(interp
, Jim_NewListObj(interp
, NULL
, 0));
4504 for (unsigned x
= 0; NULL
!= target_types
[x
]; x
++)
4506 Jim_ListAppendElement(interp
, Jim_GetResult(interp
),
4507 Jim_NewStringObj(interp
, target_types
[x
]->name
, -1));
4512 static int jim_target_names(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4516 Jim_WrongNumArgs(interp
, 1, argv
, "Too many parameters");
4519 Jim_SetResult(interp
, Jim_NewListObj(interp
, NULL
, 0));
4520 struct target
*target
= all_targets
;
4523 Jim_ListAppendElement(interp
, Jim_GetResult(interp
),
4524 Jim_NewStringObj(interp
, target_name(target
), -1));
4525 target
= target
->next
;
4530 static int jim_target_create(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4533 Jim_GetOpt_Setup(&goi
, interp
, argc
- 1, argv
+ 1);
4536 Jim_WrongNumArgs(goi
.interp
, goi
.argc
, goi
.argv
,
4537 "<name> <target_type> [<target_options> ...]");
4540 return target_create(&goi
);
4543 static int jim_target_number(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4546 Jim_GetOpt_Setup(&goi
, interp
, argc
- 1, argv
+ 1);
4548 /* It's OK to remove this mechanism sometime after August 2010 or so */
4549 LOG_WARNING("don't use numbers as target identifiers; use names");
4552 Jim_SetResult_sprintf(goi
.interp
, "usage: target number <number>");
4556 int e
= Jim_GetOpt_Wide(&goi
, &w
);
4560 struct target
*target
;
4561 for (target
= all_targets
; NULL
!= target
; target
= target
->next
)
4563 if (target
->target_number
!= w
)
4566 Jim_SetResultString(goi
.interp
, target_name(target
), -1);
4569 Jim_SetResult_sprintf(goi
.interp
,
4570 "Target: number %d does not exist", (int)(w
));
4574 static int jim_target_count(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4578 Jim_WrongNumArgs(interp
, 1, argv
, "<no parameters>");
4582 struct target
*target
= all_targets
;
4583 while (NULL
!= target
)
4585 target
= target
->next
;
4588 Jim_SetResult(interp
, Jim_NewIntObj(interp
, count
));
4592 static const struct command_registration target_subcommand_handlers
[] = {
4595 .mode
= COMMAND_CONFIG
,
4596 .handler
= &handle_target_init_command
,
4597 .help
= "initialize targets",
4601 .mode
= COMMAND_ANY
,
4602 .jim_handler
= &jim_target_create
,
4603 .usage
= "<name> <type> ...",
4604 .help
= "Returns the currently selected target",
4608 .mode
= COMMAND_ANY
,
4609 .jim_handler
= &jim_target_current
,
4610 .help
= "Returns the currently selected target",
4614 .mode
= COMMAND_ANY
,
4615 .jim_handler
= &jim_target_types
,
4616 .help
= "Returns the available target types as a list of strings",
4620 .mode
= COMMAND_ANY
,
4621 .jim_handler
= &jim_target_names
,
4622 .help
= "Returns the names of all targets as a list of strings",
4626 .mode
= COMMAND_ANY
,
4627 .jim_handler
= &jim_target_number
,
4628 .usage
= "<number>",
4629 .help
= "Returns the name of target <n>",
4633 .mode
= COMMAND_ANY
,
4634 .jim_handler
= &jim_target_count
,
4635 .help
= "Returns the number of targets as an integer",
4637 COMMAND_REGISTRATION_DONE
4649 static int fastload_num
;
4650 static struct FastLoad
*fastload
;
4652 static void free_fastload(void)
4654 if (fastload
!= NULL
)
4657 for (i
= 0; i
< fastload_num
; i
++)
4659 if (fastload
[i
].data
)
4660 free(fastload
[i
].data
);
4670 COMMAND_HANDLER(handle_fast_load_image_command
)
4674 uint32_t image_size
;
4675 uint32_t min_address
= 0;
4676 uint32_t max_address
= 0xffffffff;
4681 int retval
= CALL_COMMAND_HANDLER(parse_load_image_command_CMD_ARGV
,
4682 &image
, &min_address
, &max_address
);
4683 if (ERROR_OK
!= retval
)
4686 struct duration bench
;
4687 duration_start(&bench
);
4689 if (image_open(&image
, CMD_ARGV
[0], (CMD_ARGC
>= 3) ? CMD_ARGV
[2] : NULL
) != ERROR_OK
)
4696 fastload_num
= image
.num_sections
;
4697 fastload
= (struct FastLoad
*)malloc(sizeof(struct FastLoad
)*image
.num_sections
);
4698 if (fastload
== NULL
)
4700 image_close(&image
);
4703 memset(fastload
, 0, sizeof(struct FastLoad
)*image
.num_sections
);
4704 for (i
= 0; i
< image
.num_sections
; i
++)
4706 buffer
= malloc(image
.sections
[i
].size
);
4709 command_print(CMD_CTX
, "error allocating buffer for section (%d bytes)",
4710 (int)(image
.sections
[i
].size
));
4714 if ((retval
= image_read_section(&image
, i
, 0x0, image
.sections
[i
].size
, buffer
, &buf_cnt
)) != ERROR_OK
)
4720 uint32_t offset
= 0;
4721 uint32_t length
= buf_cnt
;
4724 /* DANGER!!! beware of unsigned comparision here!!! */
4726 if ((image
.sections
[i
].base_address
+ buf_cnt
>= min_address
)&&
4727 (image
.sections
[i
].base_address
< max_address
))
4729 if (image
.sections
[i
].base_address
< min_address
)
4731 /* clip addresses below */
4732 offset
+= min_address
-image
.sections
[i
].base_address
;
4736 if (image
.sections
[i
].base_address
+ buf_cnt
> max_address
)
4738 length
-= (image
.sections
[i
].base_address
+ buf_cnt
)-max_address
;
4741 fastload
[i
].address
= image
.sections
[i
].base_address
+ offset
;
4742 fastload
[i
].data
= malloc(length
);
4743 if (fastload
[i
].data
== NULL
)
4748 memcpy(fastload
[i
].data
, buffer
+ offset
, length
);
4749 fastload
[i
].length
= length
;
4751 image_size
+= length
;
4752 command_print(CMD_CTX
, "%u bytes written at address 0x%8.8x",
4753 (unsigned int)length
,
4754 ((unsigned int)(image
.sections
[i
].base_address
+ offset
)));
4760 if ((ERROR_OK
== retval
) && (duration_measure(&bench
) == ERROR_OK
))
4762 command_print(CMD_CTX
, "Loaded %" PRIu32
" bytes "
4763 "in %fs (%0.3f kb/s)", image_size
,
4764 duration_elapsed(&bench
), duration_kbps(&bench
, image_size
));
4766 command_print(CMD_CTX
,
4767 "WARNING: image has not been loaded to target!"
4768 "You can issue a 'fast_load' to finish loading.");
4771 image_close(&image
);
4773 if (retval
!= ERROR_OK
)
4781 COMMAND_HANDLER(handle_fast_load_command
)
4784 return ERROR_COMMAND_SYNTAX_ERROR
;
4785 if (fastload
== NULL
)
4787 LOG_ERROR("No image in memory");
4791 int ms
= timeval_ms();
4793 int retval
= ERROR_OK
;
4794 for (i
= 0; i
< fastload_num
;i
++)
4796 struct target
*target
= get_current_target(CMD_CTX
);
4797 command_print(CMD_CTX
, "Write to 0x%08x, length 0x%08x",
4798 (unsigned int)(fastload
[i
].address
),
4799 (unsigned int)(fastload
[i
].length
));
4800 if (retval
== ERROR_OK
)
4802 retval
= target_write_buffer(target
, fastload
[i
].address
, fastload
[i
].length
, fastload
[i
].data
);
4804 size
+= fastload
[i
].length
;
4806 int after
= timeval_ms();
4807 command_print(CMD_CTX
, "Loaded image %f kBytes/s", (float)(size
/1024.0)/((float)(after
-ms
)/1000.0));
4811 static const struct command_registration target_command_handlers
[] = {
4814 .handler
= &handle_targets_command
,
4815 .mode
= COMMAND_ANY
,
4816 .help
= "change current command line target (one parameter) "
4817 "or list targets (no parameters)",
4818 .usage
= "[<new_current_target>]",
4822 .mode
= COMMAND_CONFIG
,
4823 .help
= "configure target",
4825 .chain
= target_subcommand_handlers
,
4827 COMMAND_REGISTRATION_DONE
4830 int target_register_commands(struct command_context
*cmd_ctx
)
4832 return register_commands(cmd_ctx
, NULL
, target_command_handlers
);
4835 static const struct command_registration target_exec_command_handlers
[] = {
4837 .name
= "fast_load_image",
4838 .handler
= &handle_fast_load_image_command
,
4839 .mode
= COMMAND_ANY
,
4840 .help
= "Load image into memory, mainly for profiling purposes",
4841 .usage
= "<file> <address> ['bin'|'ihex'|'elf'|'s19'] "
4842 "[min_address] [max_length]",
4845 .name
= "fast_load",
4846 .handler
= &handle_fast_load_command
,
4847 .mode
= COMMAND_ANY
,
4848 .help
= "loads active fast load image to current target "
4849 "- mainly for profiling purposes",
4853 .handler
= &handle_profile_command
,
4854 .mode
= COMMAND_EXEC
,
4855 .help
= "profiling samples the CPU PC",
4857 /** @todo don't register virt2phys() unless target supports it */
4859 .name
= "virt2phys",
4860 .handler
= &handle_virt2phys_command
,
4861 .mode
= COMMAND_ANY
,
4862 .help
= "translate a virtual address into a physical address",
4867 .handler
= &handle_reg_command
,
4868 .mode
= COMMAND_EXEC
,
4869 .help
= "display or set a register",
4874 .handler
= &handle_poll_command
,
4875 .mode
= COMMAND_EXEC
,
4876 .help
= "poll target state",
4879 .name
= "wait_halt",
4880 .handler
= &handle_wait_halt_command
,
4881 .mode
= COMMAND_EXEC
,
4882 .help
= "wait for target halt",
4883 .usage
= "[time (s)]",
4887 .handler
= &handle_halt_command
,
4888 .mode
= COMMAND_EXEC
,
4889 .help
= "halt target",
4893 .handler
= &handle_resume_command
,
4894 .mode
= COMMAND_EXEC
,
4895 .help
= "resume target",
4896 .usage
= "[<address>]",
4900 .handler
= &handle_reset_command
,
4901 .mode
= COMMAND_EXEC
,
4902 .usage
= "[run|halt|init]",
4903 .help
= "Reset all targets into the specified mode."
4904 "Default reset mode is run, if not given.",
4907 .name
= "soft_reset_halt",
4908 .handler
= &handle_soft_reset_halt_command
,
4909 .mode
= COMMAND_EXEC
,
4910 .help
= "halt the target and do a soft reset",
4915 .handler
= &handle_step_command
,
4916 .mode
= COMMAND_EXEC
,
4917 .help
= "step one instruction from current PC or [addr]",
4918 .usage
= "[<address>]",
4923 .handler
= &handle_md_command
,
4924 .mode
= COMMAND_EXEC
,
4925 .help
= "display memory words",
4926 .usage
= "[phys] <addr> [count]",
4930 .handler
= &handle_md_command
,
4931 .mode
= COMMAND_EXEC
,
4932 .help
= "display memory half-words",
4933 .usage
= "[phys] <addr> [count]",
4937 .handler
= &handle_md_command
,
4938 .mode
= COMMAND_EXEC
,
4939 .help
= "display memory bytes",
4940 .usage
= "[phys] <addr> [count]",
4945 .handler
= &handle_mw_command
,
4946 .mode
= COMMAND_EXEC
,
4947 .help
= "write memory word",
4948 .usage
= "[phys] <addr> <value> [count]",
4952 .handler
= &handle_mw_command
,
4953 .mode
= COMMAND_EXEC
,
4954 .help
= "write memory half-word",
4955 .usage
= "[phys] <addr> <value> [count]",
4959 .handler
= &handle_mw_command
,
4960 .mode
= COMMAND_EXEC
,
4961 .help
= "write memory byte",
4962 .usage
= "[phys] <addr> <value> [count]",
4967 .handler
= &handle_bp_command
,
4968 .mode
= COMMAND_EXEC
,
4969 .help
= "list or set breakpoint",
4970 .usage
= "[<address> <length> [hw]]",
4974 .handler
= &handle_rbp_command
,
4975 .mode
= COMMAND_EXEC
,
4976 .help
= "remove breakpoint",
4977 .usage
= "<address>",
4982 .handler
= &handle_wp_command
,
4983 .mode
= COMMAND_EXEC
,
4984 .help
= "list or set watchpoint",
4985 .usage
= "[<address> <length> <r/w/a> [value] [mask]]",
4989 .handler
= &handle_rwp_command
,
4990 .mode
= COMMAND_EXEC
,
4991 .help
= "remove watchpoint",
4992 .usage
= "<address>",
4996 .name
= "load_image",
4997 .handler
= &handle_load_image_command
,
4998 .mode
= COMMAND_EXEC
,
4999 .usage
= "<file> <address> ['bin'|'ihex'|'elf'|'s19'] "
5000 "[min_address] [max_length]",
5003 .name
= "dump_image",
5004 .handler
= &handle_dump_image_command
,
5005 .mode
= COMMAND_EXEC
,
5006 .usage
= "<file> <address> <size>",
5009 .name
= "verify_image",
5010 .handler
= &handle_verify_image_command
,
5011 .mode
= COMMAND_EXEC
,
5012 .usage
= "<file> [offset] [type]",
5015 .name
= "test_image",
5016 .handler
= &handle_test_image_command
,
5017 .mode
= COMMAND_EXEC
,
5018 .usage
= "<file> [offset] [type]",
5021 .name
= "ocd_mem2array",
5022 .mode
= COMMAND_EXEC
,
5023 .jim_handler
= &jim_mem2array
,
5024 .help
= "read memory and return as a TCL array "
5025 "for script processing",
5026 .usage
= "<arrayname> <width=32|16|8> <address> <count>",
5029 .name
= "ocd_array2mem",
5030 .mode
= COMMAND_EXEC
,
5031 .jim_handler
= &jim_array2mem
,
5032 .help
= "convert a TCL array to memory locations "
5033 "and write the values",
5034 .usage
= "<arrayname> <width=32|16|8> <address> <count>",
5036 COMMAND_REGISTRATION_DONE
5038 int target_register_user_commands(struct command_context
*cmd_ctx
)
5040 int retval
= ERROR_OK
;
5041 if ((retval
= target_request_register_commands(cmd_ctx
)) != ERROR_OK
)
5044 if ((retval
= trace_register_commands(cmd_ctx
)) != ERROR_OK
)
5048 return register_commands(cmd_ctx
, NULL
, target_exec_command_handlers
);