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 int target_register_event_callback(int (*callback
)(struct target
*target
, enum target_event event
, void *priv
), void *priv
)
792 struct target_event_callback
**callbacks_p
= &target_event_callbacks
;
794 if (callback
== NULL
)
796 return ERROR_INVALID_ARGUMENTS
;
801 while ((*callbacks_p
)->next
)
802 callbacks_p
= &((*callbacks_p
)->next
);
803 callbacks_p
= &((*callbacks_p
)->next
);
806 (*callbacks_p
) = malloc(sizeof(struct target_event_callback
));
807 (*callbacks_p
)->callback
= callback
;
808 (*callbacks_p
)->priv
= priv
;
809 (*callbacks_p
)->next
= NULL
;
814 int target_register_timer_callback(int (*callback
)(void *priv
), int time_ms
, int periodic
, void *priv
)
816 struct target_timer_callback
**callbacks_p
= &target_timer_callbacks
;
819 if (callback
== NULL
)
821 return ERROR_INVALID_ARGUMENTS
;
826 while ((*callbacks_p
)->next
)
827 callbacks_p
= &((*callbacks_p
)->next
);
828 callbacks_p
= &((*callbacks_p
)->next
);
831 (*callbacks_p
) = malloc(sizeof(struct target_timer_callback
));
832 (*callbacks_p
)->callback
= callback
;
833 (*callbacks_p
)->periodic
= periodic
;
834 (*callbacks_p
)->time_ms
= time_ms
;
836 gettimeofday(&now
, NULL
);
837 (*callbacks_p
)->when
.tv_usec
= now
.tv_usec
+ (time_ms
% 1000) * 1000;
838 time_ms
-= (time_ms
% 1000);
839 (*callbacks_p
)->when
.tv_sec
= now
.tv_sec
+ (time_ms
/ 1000);
840 if ((*callbacks_p
)->when
.tv_usec
> 1000000)
842 (*callbacks_p
)->when
.tv_usec
= (*callbacks_p
)->when
.tv_usec
- 1000000;
843 (*callbacks_p
)->when
.tv_sec
+= 1;
846 (*callbacks_p
)->priv
= priv
;
847 (*callbacks_p
)->next
= NULL
;
852 int target_unregister_event_callback(int (*callback
)(struct target
*target
, enum target_event event
, void *priv
), void *priv
)
854 struct target_event_callback
**p
= &target_event_callbacks
;
855 struct target_event_callback
*c
= target_event_callbacks
;
857 if (callback
== NULL
)
859 return ERROR_INVALID_ARGUMENTS
;
864 struct target_event_callback
*next
= c
->next
;
865 if ((c
->callback
== callback
) && (c
->priv
== priv
))
879 int target_unregister_timer_callback(int (*callback
)(void *priv
), void *priv
)
881 struct target_timer_callback
**p
= &target_timer_callbacks
;
882 struct target_timer_callback
*c
= target_timer_callbacks
;
884 if (callback
== NULL
)
886 return ERROR_INVALID_ARGUMENTS
;
891 struct target_timer_callback
*next
= c
->next
;
892 if ((c
->callback
== callback
) && (c
->priv
== priv
))
906 int target_call_event_callbacks(struct target
*target
, enum target_event event
)
908 struct target_event_callback
*callback
= target_event_callbacks
;
909 struct target_event_callback
*next_callback
;
911 if (event
== TARGET_EVENT_HALTED
)
913 /* execute early halted first */
914 target_call_event_callbacks(target
, TARGET_EVENT_GDB_HALT
);
917 LOG_DEBUG("target event %i (%s)",
919 Jim_Nvp_value2name_simple(nvp_target_event
, event
)->name
);
921 target_handle_event(target
, event
);
925 next_callback
= callback
->next
;
926 callback
->callback(target
, event
, callback
->priv
);
927 callback
= next_callback
;
933 static int target_timer_callback_periodic_restart(
934 struct target_timer_callback
*cb
, struct timeval
*now
)
936 int time_ms
= cb
->time_ms
;
937 cb
->when
.tv_usec
= now
->tv_usec
+ (time_ms
% 1000) * 1000;
938 time_ms
-= (time_ms
% 1000);
939 cb
->when
.tv_sec
= now
->tv_sec
+ time_ms
/ 1000;
940 if (cb
->when
.tv_usec
> 1000000)
942 cb
->when
.tv_usec
= cb
->when
.tv_usec
- 1000000;
943 cb
->when
.tv_sec
+= 1;
948 static int target_call_timer_callback(struct target_timer_callback
*cb
,
951 cb
->callback(cb
->priv
);
954 return target_timer_callback_periodic_restart(cb
, now
);
956 return target_unregister_timer_callback(cb
->callback
, cb
->priv
);
959 static int target_call_timer_callbacks_check_time(int checktime
)
964 gettimeofday(&now
, NULL
);
966 struct target_timer_callback
*callback
= target_timer_callbacks
;
969 // cleaning up may unregister and free this callback
970 struct target_timer_callback
*next_callback
= callback
->next
;
972 bool call_it
= callback
->callback
&&
973 ((!checktime
&& callback
->periodic
) ||
974 now
.tv_sec
> callback
->when
.tv_sec
||
975 (now
.tv_sec
== callback
->when
.tv_sec
&&
976 now
.tv_usec
>= callback
->when
.tv_usec
));
980 int retval
= target_call_timer_callback(callback
, &now
);
981 if (retval
!= ERROR_OK
)
985 callback
= next_callback
;
991 int target_call_timer_callbacks(void)
993 return target_call_timer_callbacks_check_time(1);
996 /* invoke periodic callbacks immediately */
997 int target_call_timer_callbacks_now(void)
999 return target_call_timer_callbacks_check_time(0);
1002 int target_alloc_working_area(struct target
*target
, uint32_t size
, struct working_area
**area
)
1004 struct working_area
*c
= target
->working_areas
;
1005 struct working_area
*new_wa
= NULL
;
1007 /* Reevaluate working area address based on MMU state*/
1008 if (target
->working_areas
== NULL
)
1013 retval
= target
->type
->mmu(target
, &enabled
);
1014 if (retval
!= ERROR_OK
)
1020 if (target
->working_area_phys_spec
) {
1021 LOG_DEBUG("MMU disabled, using physical "
1022 "address for working memory 0x%08x",
1023 (unsigned)target
->working_area_phys
);
1024 target
->working_area
= target
->working_area_phys
;
1026 LOG_ERROR("No working memory available. "
1027 "Specify -work-area-phys to target.");
1028 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
1031 if (target
->working_area_virt_spec
) {
1032 LOG_DEBUG("MMU enabled, using virtual "
1033 "address for working memory 0x%08x",
1034 (unsigned)target
->working_area_virt
);
1035 target
->working_area
= target
->working_area_virt
;
1037 LOG_ERROR("No working memory available. "
1038 "Specify -work-area-virt to target.");
1039 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
1044 /* only allocate multiples of 4 byte */
1047 LOG_ERROR("BUG: code tried to allocate unaligned number of bytes (0x%08x), padding", ((unsigned)(size
)));
1048 size
= (size
+ 3) & (~3);
1051 /* see if there's already a matching working area */
1054 if ((c
->free
) && (c
->size
== size
))
1062 /* if not, allocate a new one */
1065 struct working_area
**p
= &target
->working_areas
;
1066 uint32_t first_free
= target
->working_area
;
1067 uint32_t free_size
= target
->working_area_size
;
1069 c
= target
->working_areas
;
1072 first_free
+= c
->size
;
1073 free_size
-= c
->size
;
1078 if (free_size
< size
)
1080 LOG_WARNING("not enough working area available(requested %u, free %u)",
1081 (unsigned)(size
), (unsigned)(free_size
));
1082 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
1085 LOG_DEBUG("allocated new working area at address 0x%08x", (unsigned)first_free
);
1087 new_wa
= malloc(sizeof(struct working_area
));
1088 new_wa
->next
= NULL
;
1089 new_wa
->size
= size
;
1090 new_wa
->address
= first_free
;
1092 if (target
->backup_working_area
)
1095 new_wa
->backup
= malloc(new_wa
->size
);
1096 if ((retval
= target_read_memory(target
, new_wa
->address
, 4, new_wa
->size
/ 4, new_wa
->backup
)) != ERROR_OK
)
1098 free(new_wa
->backup
);
1105 new_wa
->backup
= NULL
;
1108 /* put new entry in list */
1112 /* mark as used, and return the new (reused) area */
1117 new_wa
->user
= area
;
1122 int target_free_working_area_restore(struct target
*target
, struct working_area
*area
, int restore
)
1127 if (restore
&& target
->backup_working_area
)
1130 if ((retval
= target_write_memory(target
, area
->address
, 4, area
->size
/ 4, area
->backup
)) != ERROR_OK
)
1136 /* mark user pointer invalid */
1143 int target_free_working_area(struct target
*target
, struct working_area
*area
)
1145 return target_free_working_area_restore(target
, area
, 1);
1148 /* free resources and restore memory, if restoring memory fails,
1149 * free up resources anyway
1151 void target_free_all_working_areas_restore(struct target
*target
, int restore
)
1153 struct working_area
*c
= target
->working_areas
;
1157 struct working_area
*next
= c
->next
;
1158 target_free_working_area_restore(target
, c
, restore
);
1168 target
->working_areas
= NULL
;
1171 void target_free_all_working_areas(struct target
*target
)
1173 target_free_all_working_areas_restore(target
, 1);
1176 int target_arch_state(struct target
*target
)
1181 LOG_USER("No target has been configured");
1185 LOG_USER("target state: %s", target_state_name( target
));
1187 if (target
->state
!= TARGET_HALTED
)
1190 retval
= target
->type
->arch_state(target
);
1194 /* Single aligned words are guaranteed to use 16 or 32 bit access
1195 * mode respectively, otherwise data is handled as quickly as
1198 int target_write_buffer(struct target
*target
, uint32_t address
, uint32_t size
, uint8_t *buffer
)
1201 LOG_DEBUG("writing buffer of %i byte at 0x%8.8x",
1202 (int)size
, (unsigned)address
);
1204 if (!target_was_examined(target
))
1206 LOG_ERROR("Target not examined yet");
1214 if ((address
+ size
- 1) < address
)
1216 /* GDB can request this when e.g. PC is 0xfffffffc*/
1217 LOG_ERROR("address + size wrapped(0x%08x, 0x%08x)",
1223 if (((address
% 2) == 0) && (size
== 2))
1225 return target_write_memory(target
, address
, 2, 1, buffer
);
1228 /* handle unaligned head bytes */
1231 uint32_t unaligned
= 4 - (address
% 4);
1233 if (unaligned
> size
)
1236 if ((retval
= target_write_memory(target
, address
, 1, unaligned
, buffer
)) != ERROR_OK
)
1239 buffer
+= unaligned
;
1240 address
+= unaligned
;
1244 /* handle aligned words */
1247 int aligned
= size
- (size
% 4);
1249 /* use bulk writes above a certain limit. This may have to be changed */
1252 if ((retval
= target
->type
->bulk_write_memory(target
, address
, aligned
/ 4, buffer
)) != ERROR_OK
)
1257 if ((retval
= target_write_memory(target
, address
, 4, aligned
/ 4, buffer
)) != ERROR_OK
)
1266 /* handle tail writes of less than 4 bytes */
1269 if ((retval
= target_write_memory(target
, address
, 1, size
, buffer
)) != ERROR_OK
)
1276 /* Single aligned words are guaranteed to use 16 or 32 bit access
1277 * mode respectively, otherwise data is handled as quickly as
1280 int target_read_buffer(struct target
*target
, uint32_t address
, uint32_t size
, uint8_t *buffer
)
1283 LOG_DEBUG("reading buffer of %i byte at 0x%8.8x",
1284 (int)size
, (unsigned)address
);
1286 if (!target_was_examined(target
))
1288 LOG_ERROR("Target not examined yet");
1296 if ((address
+ size
- 1) < address
)
1298 /* GDB can request this when e.g. PC is 0xfffffffc*/
1299 LOG_ERROR("address + size wrapped(0x%08" PRIx32
", 0x%08" PRIx32
")",
1305 if (((address
% 2) == 0) && (size
== 2))
1307 return target_read_memory(target
, address
, 2, 1, buffer
);
1310 /* handle unaligned head bytes */
1313 uint32_t unaligned
= 4 - (address
% 4);
1315 if (unaligned
> size
)
1318 if ((retval
= target_read_memory(target
, address
, 1, unaligned
, buffer
)) != ERROR_OK
)
1321 buffer
+= unaligned
;
1322 address
+= unaligned
;
1326 /* handle aligned words */
1329 int aligned
= size
- (size
% 4);
1331 if ((retval
= target_read_memory(target
, address
, 4, aligned
/ 4, buffer
)) != ERROR_OK
)
1339 /*prevent byte access when possible (avoid AHB access limitations in some cases)*/
1342 int aligned
= size
- (size
%2);
1343 retval
= target_read_memory(target
, address
, 2, aligned
/ 2, buffer
);
1344 if (retval
!= ERROR_OK
)
1351 /* handle tail writes of less than 4 bytes */
1354 if ((retval
= target_read_memory(target
, address
, 1, size
, buffer
)) != ERROR_OK
)
1361 int target_checksum_memory(struct target
*target
, uint32_t address
, uint32_t size
, uint32_t* crc
)
1366 uint32_t checksum
= 0;
1367 if (!target_was_examined(target
))
1369 LOG_ERROR("Target not examined yet");
1373 if ((retval
= target
->type
->checksum_memory(target
, address
,
1374 size
, &checksum
)) != ERROR_OK
)
1376 buffer
= malloc(size
);
1379 LOG_ERROR("error allocating buffer for section (%d bytes)", (int)size
);
1380 return ERROR_INVALID_ARGUMENTS
;
1382 retval
= target_read_buffer(target
, address
, size
, buffer
);
1383 if (retval
!= ERROR_OK
)
1389 /* convert to target endianess */
1390 for (i
= 0; i
< (size
/sizeof(uint32_t)); i
++)
1392 uint32_t target_data
;
1393 target_data
= target_buffer_get_u32(target
, &buffer
[i
*sizeof(uint32_t)]);
1394 target_buffer_set_u32(target
, &buffer
[i
*sizeof(uint32_t)], target_data
);
1397 retval
= image_calculate_checksum(buffer
, size
, &checksum
);
1406 int target_blank_check_memory(struct target
*target
, uint32_t address
, uint32_t size
, uint32_t* blank
)
1409 if (!target_was_examined(target
))
1411 LOG_ERROR("Target not examined yet");
1415 if (target
->type
->blank_check_memory
== 0)
1416 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
1418 retval
= target
->type
->blank_check_memory(target
, address
, size
, blank
);
1423 int target_read_u32(struct target
*target
, uint32_t address
, uint32_t *value
)
1425 uint8_t value_buf
[4];
1426 if (!target_was_examined(target
))
1428 LOG_ERROR("Target not examined yet");
1432 int retval
= target_read_memory(target
, address
, 4, 1, value_buf
);
1434 if (retval
== ERROR_OK
)
1436 *value
= target_buffer_get_u32(target
, value_buf
);
1437 LOG_DEBUG("address: 0x%8.8" PRIx32
", value: 0x%8.8" PRIx32
"",
1444 LOG_DEBUG("address: 0x%8.8" PRIx32
" failed",
1451 int target_read_u16(struct target
*target
, uint32_t address
, uint16_t *value
)
1453 uint8_t value_buf
[2];
1454 if (!target_was_examined(target
))
1456 LOG_ERROR("Target not examined yet");
1460 int retval
= target_read_memory(target
, address
, 2, 1, value_buf
);
1462 if (retval
== ERROR_OK
)
1464 *value
= target_buffer_get_u16(target
, value_buf
);
1465 LOG_DEBUG("address: 0x%8.8" PRIx32
", value: 0x%4.4x",
1472 LOG_DEBUG("address: 0x%8.8" PRIx32
" failed",
1479 int target_read_u8(struct target
*target
, uint32_t address
, uint8_t *value
)
1481 int retval
= target_read_memory(target
, address
, 1, 1, value
);
1482 if (!target_was_examined(target
))
1484 LOG_ERROR("Target not examined yet");
1488 if (retval
== ERROR_OK
)
1490 LOG_DEBUG("address: 0x%8.8" PRIx32
", value: 0x%2.2x",
1497 LOG_DEBUG("address: 0x%8.8" PRIx32
" failed",
1504 int target_write_u32(struct target
*target
, uint32_t address
, uint32_t value
)
1507 uint8_t value_buf
[4];
1508 if (!target_was_examined(target
))
1510 LOG_ERROR("Target not examined yet");
1514 LOG_DEBUG("address: 0x%8.8" PRIx32
", value: 0x%8.8" PRIx32
"",
1518 target_buffer_set_u32(target
, value_buf
, value
);
1519 if ((retval
= target_write_memory(target
, address
, 4, 1, value_buf
)) != ERROR_OK
)
1521 LOG_DEBUG("failed: %i", retval
);
1527 int target_write_u16(struct target
*target
, uint32_t address
, uint16_t value
)
1530 uint8_t value_buf
[2];
1531 if (!target_was_examined(target
))
1533 LOG_ERROR("Target not examined yet");
1537 LOG_DEBUG("address: 0x%8.8" PRIx32
", value: 0x%8.8x",
1541 target_buffer_set_u16(target
, value_buf
, value
);
1542 if ((retval
= target_write_memory(target
, address
, 2, 1, value_buf
)) != ERROR_OK
)
1544 LOG_DEBUG("failed: %i", retval
);
1550 int target_write_u8(struct target
*target
, uint32_t address
, uint8_t value
)
1553 if (!target_was_examined(target
))
1555 LOG_ERROR("Target not examined yet");
1559 LOG_DEBUG("address: 0x%8.8" PRIx32
", value: 0x%2.2x",
1562 if ((retval
= target_write_memory(target
, address
, 1, 1, &value
)) != ERROR_OK
)
1564 LOG_DEBUG("failed: %i", retval
);
1570 COMMAND_HANDLER(handle_targets_command
)
1572 struct target
*target
= all_targets
;
1576 target
= get_target(CMD_ARGV
[0]);
1577 if (target
== NULL
) {
1578 command_print(CMD_CTX
,"Target: %s is unknown, try one of:\n", CMD_ARGV
[0]);
1581 if (!target
->tap
->enabled
) {
1582 command_print(CMD_CTX
,"Target: TAP %s is disabled, "
1583 "can't be the current target\n",
1584 target
->tap
->dotted_name
);
1588 CMD_CTX
->current_target
= target
->target_number
;
1593 target
= all_targets
;
1594 command_print(CMD_CTX
, " TargetName Type Endian TapName State ");
1595 command_print(CMD_CTX
, "-- ------------------ ---------- ------ ------------------ ------------");
1601 if (target
->tap
->enabled
)
1602 state
= target_state_name( target
);
1604 state
= "tap-disabled";
1606 if (CMD_CTX
->current_target
== target
->target_number
)
1609 /* keep columns lined up to match the headers above */
1610 command_print(CMD_CTX
, "%2d%c %-18s %-10s %-6s %-18s %s",
1611 target
->target_number
,
1613 target_name(target
),
1614 target_type_name(target
),
1615 Jim_Nvp_value2name_simple(nvp_target_endian
,
1616 target
->endianness
)->name
,
1617 target
->tap
->dotted_name
,
1619 target
= target
->next
;
1625 /* every 300ms we check for reset & powerdropout and issue a "reset halt" if so. */
1627 static int powerDropout
;
1628 static int srstAsserted
;
1630 static int runPowerRestore
;
1631 static int runPowerDropout
;
1632 static int runSrstAsserted
;
1633 static int runSrstDeasserted
;
1635 static int sense_handler(void)
1637 static int prevSrstAsserted
= 0;
1638 static int prevPowerdropout
= 0;
1641 if ((retval
= jtag_power_dropout(&powerDropout
)) != ERROR_OK
)
1645 powerRestored
= prevPowerdropout
&& !powerDropout
;
1648 runPowerRestore
= 1;
1651 long long current
= timeval_ms();
1652 static long long lastPower
= 0;
1653 int waitMore
= lastPower
+ 2000 > current
;
1654 if (powerDropout
&& !waitMore
)
1656 runPowerDropout
= 1;
1657 lastPower
= current
;
1660 if ((retval
= jtag_srst_asserted(&srstAsserted
)) != ERROR_OK
)
1664 srstDeasserted
= prevSrstAsserted
&& !srstAsserted
;
1666 static long long lastSrst
= 0;
1667 waitMore
= lastSrst
+ 2000 > current
;
1668 if (srstDeasserted
&& !waitMore
)
1670 runSrstDeasserted
= 1;
1674 if (!prevSrstAsserted
&& srstAsserted
)
1676 runSrstAsserted
= 1;
1679 prevSrstAsserted
= srstAsserted
;
1680 prevPowerdropout
= powerDropout
;
1682 if (srstDeasserted
|| powerRestored
)
1684 /* Other than logging the event we can't do anything here.
1685 * Issuing a reset is a particularly bad idea as we might
1686 * be inside a reset already.
1693 static void target_call_event_callbacks_all(enum target_event e
) {
1694 struct target
*target
;
1695 target
= all_targets
;
1697 target_call_event_callbacks(target
, e
);
1698 target
= target
->next
;
1702 /* process target state changes */
1703 static int handle_target(void *priv
)
1705 Jim_Interp
*interp
= (Jim_Interp
*)priv
;
1706 int retval
= ERROR_OK
;
1708 /* we do not want to recurse here... */
1709 static int recursive
= 0;
1714 /* danger! running these procedures can trigger srst assertions and power dropouts.
1715 * We need to avoid an infinite loop/recursion here and we do that by
1716 * clearing the flags after running these events.
1718 int did_something
= 0;
1719 if (runSrstAsserted
)
1721 target_call_event_callbacks_all(TARGET_EVENT_GDB_HALT
);
1722 Jim_Eval(interp
, "srst_asserted");
1725 if (runSrstDeasserted
)
1727 Jim_Eval(interp
, "srst_deasserted");
1730 if (runPowerDropout
)
1732 target_call_event_callbacks_all(TARGET_EVENT_GDB_HALT
);
1733 Jim_Eval(interp
, "power_dropout");
1736 if (runPowerRestore
)
1738 Jim_Eval(interp
, "power_restore");
1744 /* clear detect flags */
1748 /* clear action flags */
1750 runSrstAsserted
= 0;
1751 runSrstDeasserted
= 0;
1752 runPowerRestore
= 0;
1753 runPowerDropout
= 0;
1758 /* Poll targets for state changes unless that's globally disabled.
1759 * Skip targets that are currently disabled.
1761 for (struct target
*target
= all_targets
;
1762 is_jtag_poll_safe() && target
;
1763 target
= target
->next
)
1765 if (!target
->tap
->enabled
)
1768 /* only poll target if we've got power and srst isn't asserted */
1769 if (!powerDropout
&& !srstAsserted
)
1771 /* polling may fail silently until the target has been examined */
1772 if ((retval
= target_poll(target
)) != ERROR_OK
)
1774 target_call_event_callbacks(target
, TARGET_EVENT_GDB_HALT
);
1783 COMMAND_HANDLER(handle_reg_command
)
1785 struct target
*target
;
1786 struct reg
*reg
= NULL
;
1792 target
= get_current_target(CMD_CTX
);
1794 /* list all available registers for the current target */
1797 struct reg_cache
*cache
= target
->reg_cache
;
1804 command_print(CMD_CTX
, "===== %s", cache
->name
);
1806 for (i
= 0, reg
= cache
->reg_list
;
1807 i
< cache
->num_regs
;
1808 i
++, reg
++, count
++)
1810 /* only print cached values if they are valid */
1812 value
= buf_to_str(reg
->value
,
1814 command_print(CMD_CTX
,
1815 "(%i) %s (/%" PRIu32
"): 0x%s%s",
1823 command_print(CMD_CTX
, "(%i) %s (/%" PRIu32
")",
1828 cache
= cache
->next
;
1834 /* access a single register by its ordinal number */
1835 if ((CMD_ARGV
[0][0] >= '0') && (CMD_ARGV
[0][0] <= '9'))
1838 COMMAND_PARSE_NUMBER(uint
, CMD_ARGV
[0], num
);
1840 struct reg_cache
*cache
= target
->reg_cache
;
1845 for (i
= 0; i
< cache
->num_regs
; i
++)
1849 reg
= &cache
->reg_list
[i
];
1855 cache
= cache
->next
;
1860 command_print(CMD_CTX
, "%i is out of bounds, the current target has only %i registers (0 - %i)", num
, count
, count
- 1);
1863 } else /* access a single register by its name */
1865 reg
= register_get_by_name(target
->reg_cache
, CMD_ARGV
[0], 1);
1869 command_print(CMD_CTX
, "register %s not found in current target", CMD_ARGV
[0]);
1874 /* display a register */
1875 if ((CMD_ARGC
== 1) || ((CMD_ARGC
== 2) && !((CMD_ARGV
[1][0] >= '0') && (CMD_ARGV
[1][0] <= '9'))))
1877 if ((CMD_ARGC
== 2) && (strcmp(CMD_ARGV
[1], "force") == 0))
1880 if (reg
->valid
== 0)
1882 reg
->type
->get(reg
);
1884 value
= buf_to_str(reg
->value
, reg
->size
, 16);
1885 command_print(CMD_CTX
, "%s (/%i): 0x%s", reg
->name
, (int)(reg
->size
), value
);
1890 /* set register value */
1893 uint8_t *buf
= malloc(DIV_ROUND_UP(reg
->size
, 8));
1894 str_to_buf(CMD_ARGV
[1], strlen(CMD_ARGV
[1]), buf
, reg
->size
, 0);
1896 reg
->type
->set(reg
, buf
);
1898 value
= buf_to_str(reg
->value
, reg
->size
, 16);
1899 command_print(CMD_CTX
, "%s (/%i): 0x%s", reg
->name
, (int)(reg
->size
), value
);
1907 command_print(CMD_CTX
, "usage: reg <#|name> [value]");
1912 COMMAND_HANDLER(handle_poll_command
)
1914 int retval
= ERROR_OK
;
1915 struct target
*target
= get_current_target(CMD_CTX
);
1919 command_print(CMD_CTX
, "background polling: %s",
1920 jtag_poll_get_enabled() ? "on" : "off");
1921 command_print(CMD_CTX
, "TAP: %s (%s)",
1922 target
->tap
->dotted_name
,
1923 target
->tap
->enabled
? "enabled" : "disabled");
1924 if (!target
->tap
->enabled
)
1926 if ((retval
= target_poll(target
)) != ERROR_OK
)
1928 if ((retval
= target_arch_state(target
)) != ERROR_OK
)
1931 else if (CMD_ARGC
== 1)
1934 COMMAND_PARSE_ON_OFF(CMD_ARGV
[0], enable
);
1935 jtag_poll_set_enabled(enable
);
1939 return ERROR_COMMAND_SYNTAX_ERROR
;
1945 COMMAND_HANDLER(handle_wait_halt_command
)
1948 return ERROR_COMMAND_SYNTAX_ERROR
;
1953 int retval
= parse_uint(CMD_ARGV
[0], &ms
);
1954 if (ERROR_OK
!= retval
)
1956 command_print(CMD_CTX
, "usage: %s [seconds]", CMD_NAME
);
1957 return ERROR_COMMAND_SYNTAX_ERROR
;
1959 // convert seconds (given) to milliseconds (needed)
1963 struct target
*target
= get_current_target(CMD_CTX
);
1964 return target_wait_state(target
, TARGET_HALTED
, ms
);
1967 /* wait for target state to change. The trick here is to have a low
1968 * latency for short waits and not to suck up all the CPU time
1971 * After 500ms, keep_alive() is invoked
1973 int target_wait_state(struct target
*target
, enum target_state state
, int ms
)
1976 long long then
= 0, cur
;
1981 if ((retval
= target_poll(target
)) != ERROR_OK
)
1983 if (target
->state
== state
)
1991 then
= timeval_ms();
1992 LOG_DEBUG("waiting for target %s...",
1993 Jim_Nvp_value2name_simple(nvp_target_state
,state
)->name
);
2001 if ((cur
-then
) > ms
)
2003 LOG_ERROR("timed out while waiting for target %s",
2004 Jim_Nvp_value2name_simple(nvp_target_state
,state
)->name
);
2012 COMMAND_HANDLER(handle_halt_command
)
2016 struct target
*target
= get_current_target(CMD_CTX
);
2017 int retval
= target_halt(target
);
2018 if (ERROR_OK
!= retval
)
2024 retval
= parse_uint(CMD_ARGV
[0], &wait
);
2025 if (ERROR_OK
!= retval
)
2026 return ERROR_COMMAND_SYNTAX_ERROR
;
2031 return CALL_COMMAND_HANDLER(handle_wait_halt_command
);
2034 COMMAND_HANDLER(handle_soft_reset_halt_command
)
2036 struct target
*target
= get_current_target(CMD_CTX
);
2038 LOG_USER("requesting target halt and executing a soft reset");
2040 target
->type
->soft_reset_halt(target
);
2045 COMMAND_HANDLER(handle_reset_command
)
2048 return ERROR_COMMAND_SYNTAX_ERROR
;
2050 enum target_reset_mode reset_mode
= RESET_RUN
;
2054 n
= Jim_Nvp_name2value_simple(nvp_reset_modes
, CMD_ARGV
[0]);
2055 if ((n
->name
== NULL
) || (n
->value
== RESET_UNKNOWN
)) {
2056 return ERROR_COMMAND_SYNTAX_ERROR
;
2058 reset_mode
= n
->value
;
2061 /* reset *all* targets */
2062 return target_process_reset(CMD_CTX
, reset_mode
);
2066 COMMAND_HANDLER(handle_resume_command
)
2070 return ERROR_COMMAND_SYNTAX_ERROR
;
2072 struct target
*target
= get_current_target(CMD_CTX
);
2073 target_handle_event(target
, TARGET_EVENT_OLD_pre_resume
);
2075 /* with no CMD_ARGV, resume from current pc, addr = 0,
2076 * with one arguments, addr = CMD_ARGV[0],
2077 * handle breakpoints, not debugging */
2081 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], addr
);
2085 return target_resume(target
, current
, addr
, 1, 0);
2088 COMMAND_HANDLER(handle_step_command
)
2091 return ERROR_COMMAND_SYNTAX_ERROR
;
2095 /* with no CMD_ARGV, step from current pc, addr = 0,
2096 * with one argument addr = CMD_ARGV[0],
2097 * handle breakpoints, debugging */
2102 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], addr
);
2106 struct target
*target
= get_current_target(CMD_CTX
);
2108 return target
->type
->step(target
, current_pc
, addr
, 1);
2111 static void handle_md_output(struct command_context
*cmd_ctx
,
2112 struct target
*target
, uint32_t address
, unsigned size
,
2113 unsigned count
, const uint8_t *buffer
)
2115 const unsigned line_bytecnt
= 32;
2116 unsigned line_modulo
= line_bytecnt
/ size
;
2118 char output
[line_bytecnt
* 4 + 1];
2119 unsigned output_len
= 0;
2121 const char *value_fmt
;
2123 case 4: value_fmt
= "%8.8x "; break;
2124 case 2: value_fmt
= "%4.2x "; break;
2125 case 1: value_fmt
= "%2.2x "; break;
2127 LOG_ERROR("invalid memory read size: %u", size
);
2131 for (unsigned i
= 0; i
< count
; i
++)
2133 if (i
% line_modulo
== 0)
2135 output_len
+= snprintf(output
+ output_len
,
2136 sizeof(output
) - output_len
,
2138 (unsigned)(address
+ (i
*size
)));
2142 const uint8_t *value_ptr
= buffer
+ i
* size
;
2144 case 4: value
= target_buffer_get_u32(target
, value_ptr
); break;
2145 case 2: value
= target_buffer_get_u16(target
, value_ptr
); break;
2146 case 1: value
= *value_ptr
;
2148 output_len
+= snprintf(output
+ output_len
,
2149 sizeof(output
) - output_len
,
2152 if ((i
% line_modulo
== line_modulo
- 1) || (i
== count
- 1))
2154 command_print(cmd_ctx
, "%s", output
);
2160 COMMAND_HANDLER(handle_md_command
)
2163 return ERROR_COMMAND_SYNTAX_ERROR
;
2166 switch (CMD_NAME
[2]) {
2167 case 'w': size
= 4; break;
2168 case 'h': size
= 2; break;
2169 case 'b': size
= 1; break;
2170 default: return ERROR_COMMAND_SYNTAX_ERROR
;
2173 bool physical
=strcmp(CMD_ARGV
[0], "phys")==0;
2174 int (*fn
)(struct target
*target
,
2175 uint32_t address
, uint32_t size
, uint32_t count
, uint8_t *buffer
);
2180 fn
=target_read_phys_memory
;
2183 fn
=target_read_memory
;
2185 if ((CMD_ARGC
< 1) || (CMD_ARGC
> 2))
2187 return ERROR_COMMAND_SYNTAX_ERROR
;
2191 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], address
);
2195 COMMAND_PARSE_NUMBER(uint
, CMD_ARGV
[1], count
);
2197 uint8_t *buffer
= calloc(count
, size
);
2199 struct target
*target
= get_current_target(CMD_CTX
);
2200 int retval
= fn(target
, address
, size
, count
, buffer
);
2201 if (ERROR_OK
== retval
)
2202 handle_md_output(CMD_CTX
, target
, address
, size
, count
, buffer
);
2209 COMMAND_HANDLER(handle_mw_command
)
2213 return ERROR_COMMAND_SYNTAX_ERROR
;
2215 bool physical
=strcmp(CMD_ARGV
[0], "phys")==0;
2216 int (*fn
)(struct target
*target
,
2217 uint32_t address
, uint32_t size
, uint32_t count
, uint8_t *buffer
);
2222 fn
=target_write_phys_memory
;
2225 fn
=target_write_memory
;
2227 if ((CMD_ARGC
< 2) || (CMD_ARGC
> 3))
2228 return ERROR_COMMAND_SYNTAX_ERROR
;
2231 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], address
);
2234 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], value
);
2238 COMMAND_PARSE_NUMBER(uint
, CMD_ARGV
[2], count
);
2240 struct target
*target
= get_current_target(CMD_CTX
);
2242 uint8_t value_buf
[4];
2243 switch (CMD_NAME
[2])
2247 target_buffer_set_u32(target
, value_buf
, value
);
2251 target_buffer_set_u16(target
, value_buf
, value
);
2255 value_buf
[0] = value
;
2258 return ERROR_COMMAND_SYNTAX_ERROR
;
2260 for (unsigned i
= 0; i
< count
; i
++)
2262 int retval
= fn(target
,
2263 address
+ i
* wordsize
, wordsize
, 1, value_buf
);
2264 if (ERROR_OK
!= retval
)
2273 static COMMAND_HELPER(parse_load_image_command_CMD_ARGV
, struct image
*image
,
2274 uint32_t *min_address
, uint32_t *max_address
)
2276 if (CMD_ARGC
< 1 || CMD_ARGC
> 5)
2277 return ERROR_COMMAND_SYNTAX_ERROR
;
2279 /* a base address isn't always necessary,
2280 * default to 0x0 (i.e. don't relocate) */
2284 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], addr
);
2285 image
->base_address
= addr
;
2286 image
->base_address_set
= 1;
2289 image
->base_address_set
= 0;
2291 image
->start_address_set
= 0;
2295 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[3], *min_address
);
2299 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[4], *max_address
);
2300 // use size (given) to find max (required)
2301 *max_address
+= *min_address
;
2304 if (*min_address
> *max_address
)
2305 return ERROR_COMMAND_SYNTAX_ERROR
;
2310 COMMAND_HANDLER(handle_load_image_command
)
2314 uint32_t image_size
;
2315 uint32_t min_address
= 0;
2316 uint32_t max_address
= 0xffffffff;
2320 int retval
= CALL_COMMAND_HANDLER(parse_load_image_command_CMD_ARGV
,
2321 &image
, &min_address
, &max_address
);
2322 if (ERROR_OK
!= retval
)
2325 struct target
*target
= get_current_target(CMD_CTX
);
2327 struct duration bench
;
2328 duration_start(&bench
);
2330 if (image_open(&image
, CMD_ARGV
[0], (CMD_ARGC
>= 3) ? CMD_ARGV
[2] : NULL
) != ERROR_OK
)
2337 for (i
= 0; i
< image
.num_sections
; i
++)
2339 buffer
= malloc(image
.sections
[i
].size
);
2342 command_print(CMD_CTX
,
2343 "error allocating buffer for section (%d bytes)",
2344 (int)(image
.sections
[i
].size
));
2348 if ((retval
= image_read_section(&image
, i
, 0x0, image
.sections
[i
].size
, buffer
, &buf_cnt
)) != ERROR_OK
)
2354 uint32_t offset
= 0;
2355 uint32_t length
= buf_cnt
;
2357 /* DANGER!!! beware of unsigned comparision here!!! */
2359 if ((image
.sections
[i
].base_address
+ buf_cnt
>= min_address
)&&
2360 (image
.sections
[i
].base_address
< max_address
))
2362 if (image
.sections
[i
].base_address
< min_address
)
2364 /* clip addresses below */
2365 offset
+= min_address
-image
.sections
[i
].base_address
;
2369 if (image
.sections
[i
].base_address
+ buf_cnt
> max_address
)
2371 length
-= (image
.sections
[i
].base_address
+ buf_cnt
)-max_address
;
2374 if ((retval
= target_write_buffer(target
, image
.sections
[i
].base_address
+ offset
, length
, buffer
+ offset
)) != ERROR_OK
)
2379 image_size
+= length
;
2380 command_print(CMD_CTX
, "%u bytes written at address 0x%8.8" PRIx32
"",
2381 (unsigned int)length
,
2382 image
.sections
[i
].base_address
+ offset
);
2388 if ((ERROR_OK
== retval
) && (duration_measure(&bench
) == ERROR_OK
))
2390 command_print(CMD_CTX
, "downloaded %" PRIu32
" bytes "
2391 "in %fs (%0.3f kb/s)", image_size
,
2392 duration_elapsed(&bench
), duration_kbps(&bench
, image_size
));
2395 image_close(&image
);
2401 COMMAND_HANDLER(handle_dump_image_command
)
2403 struct fileio fileio
;
2405 uint8_t buffer
[560];
2409 struct target
*target
= get_current_target(CMD_CTX
);
2413 command_print(CMD_CTX
, "usage: dump_image <filename> <address> <size>");
2418 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], address
);
2420 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[2], size
);
2422 if (fileio_open(&fileio
, CMD_ARGV
[0], FILEIO_WRITE
, FILEIO_BINARY
) != ERROR_OK
)
2427 struct duration bench
;
2428 duration_start(&bench
);
2430 int retval
= ERROR_OK
;
2433 size_t size_written
;
2434 uint32_t this_run_size
= (size
> 560) ? 560 : size
;
2435 retval
= target_read_buffer(target
, address
, this_run_size
, buffer
);
2436 if (retval
!= ERROR_OK
)
2441 retval
= fileio_write(&fileio
, this_run_size
, buffer
, &size_written
);
2442 if (retval
!= ERROR_OK
)
2447 size
-= this_run_size
;
2448 address
+= this_run_size
;
2451 if ((retvaltemp
= fileio_close(&fileio
)) != ERROR_OK
)
2454 if ((ERROR_OK
== retval
) && (duration_measure(&bench
) == ERROR_OK
))
2456 command_print(CMD_CTX
,
2457 "dumped %zu bytes in %fs (%0.3f kb/s)", fileio
.size
,
2458 duration_elapsed(&bench
), duration_kbps(&bench
, fileio
.size
));
2464 static COMMAND_HELPER(handle_verify_image_command_internal
, int verify
)
2468 uint32_t image_size
;
2471 uint32_t checksum
= 0;
2472 uint32_t mem_checksum
= 0;
2476 struct target
*target
= get_current_target(CMD_CTX
);
2480 return ERROR_COMMAND_SYNTAX_ERROR
;
2485 LOG_ERROR("no target selected");
2489 struct duration bench
;
2490 duration_start(&bench
);
2495 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], addr
);
2496 image
.base_address
= addr
;
2497 image
.base_address_set
= 1;
2501 image
.base_address_set
= 0;
2502 image
.base_address
= 0x0;
2505 image
.start_address_set
= 0;
2507 if ((retval
= image_open(&image
, CMD_ARGV
[0], (CMD_ARGC
== 3) ? CMD_ARGV
[2] : NULL
)) != ERROR_OK
)
2514 for (i
= 0; i
< image
.num_sections
; i
++)
2516 buffer
= malloc(image
.sections
[i
].size
);
2519 command_print(CMD_CTX
,
2520 "error allocating buffer for section (%d bytes)",
2521 (int)(image
.sections
[i
].size
));
2524 if ((retval
= image_read_section(&image
, i
, 0x0, image
.sections
[i
].size
, buffer
, &buf_cnt
)) != ERROR_OK
)
2532 /* calculate checksum of image */
2533 image_calculate_checksum(buffer
, buf_cnt
, &checksum
);
2535 retval
= target_checksum_memory(target
, image
.sections
[i
].base_address
, buf_cnt
, &mem_checksum
);
2536 if (retval
!= ERROR_OK
)
2542 if (checksum
!= mem_checksum
)
2544 /* failed crc checksum, fall back to a binary compare */
2547 command_print(CMD_CTX
, "checksum mismatch - attempting binary compare");
2549 data
= (uint8_t*)malloc(buf_cnt
);
2551 /* Can we use 32bit word accesses? */
2553 int count
= buf_cnt
;
2554 if ((count
% 4) == 0)
2559 retval
= target_read_memory(target
, image
.sections
[i
].base_address
, size
, count
, data
);
2560 if (retval
== ERROR_OK
)
2563 for (t
= 0; t
< buf_cnt
; t
++)
2565 if (data
[t
] != buffer
[t
])
2567 command_print(CMD_CTX
,
2568 "Verify operation failed address 0x%08x. Was 0x%02x instead of 0x%02x\n",
2569 (unsigned)(t
+ image
.sections
[i
].base_address
),
2574 retval
= ERROR_FAIL
;
2588 command_print(CMD_CTX
, "address 0x%08" PRIx32
" length 0x%08zx",
2589 image
.sections
[i
].base_address
,
2594 image_size
+= buf_cnt
;
2597 if ((ERROR_OK
== retval
) && (duration_measure(&bench
) == ERROR_OK
))
2599 command_print(CMD_CTX
, "verified %" PRIu32
" bytes "
2600 "in %fs (%0.3f kb/s)", image_size
,
2601 duration_elapsed(&bench
), duration_kbps(&bench
, image_size
));
2604 image_close(&image
);
2609 COMMAND_HANDLER(handle_verify_image_command
)
2611 return CALL_COMMAND_HANDLER(handle_verify_image_command_internal
, 1);
2614 COMMAND_HANDLER(handle_test_image_command
)
2616 return CALL_COMMAND_HANDLER(handle_verify_image_command_internal
, 0);
2619 static int handle_bp_command_list(struct command_context
*cmd_ctx
)
2621 struct target
*target
= get_current_target(cmd_ctx
);
2622 struct breakpoint
*breakpoint
= target
->breakpoints
;
2625 if (breakpoint
->type
== BKPT_SOFT
)
2627 char* buf
= buf_to_str(breakpoint
->orig_instr
,
2628 breakpoint
->length
, 16);
2629 command_print(cmd_ctx
, "0x%8.8" PRIx32
", 0x%x, %i, 0x%s",
2630 breakpoint
->address
,
2632 breakpoint
->set
, buf
);
2637 command_print(cmd_ctx
, "0x%8.8" PRIx32
", 0x%x, %i",
2638 breakpoint
->address
,
2639 breakpoint
->length
, breakpoint
->set
);
2642 breakpoint
= breakpoint
->next
;
2647 static int handle_bp_command_set(struct command_context
*cmd_ctx
,
2648 uint32_t addr
, uint32_t length
, int hw
)
2650 struct target
*target
= get_current_target(cmd_ctx
);
2651 int retval
= breakpoint_add(target
, addr
, length
, hw
);
2652 if (ERROR_OK
== retval
)
2653 command_print(cmd_ctx
, "breakpoint set at 0x%8.8" PRIx32
"", addr
);
2655 LOG_ERROR("Failure setting breakpoint");
2659 COMMAND_HANDLER(handle_bp_command
)
2662 return handle_bp_command_list(CMD_CTX
);
2664 if (CMD_ARGC
< 2 || CMD_ARGC
> 3)
2666 command_print(CMD_CTX
, "usage: bp <address> <length> ['hw']");
2667 return ERROR_COMMAND_SYNTAX_ERROR
;
2671 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], addr
);
2673 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], length
);
2678 if (strcmp(CMD_ARGV
[2], "hw") == 0)
2681 return ERROR_COMMAND_SYNTAX_ERROR
;
2684 return handle_bp_command_set(CMD_CTX
, addr
, length
, hw
);
2687 COMMAND_HANDLER(handle_rbp_command
)
2690 return ERROR_COMMAND_SYNTAX_ERROR
;
2693 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], addr
);
2695 struct target
*target
= get_current_target(CMD_CTX
);
2696 breakpoint_remove(target
, addr
);
2701 COMMAND_HANDLER(handle_wp_command
)
2703 struct target
*target
= get_current_target(CMD_CTX
);
2707 struct watchpoint
*watchpoint
= target
->watchpoints
;
2711 command_print(CMD_CTX
, "address: 0x%8.8" PRIx32
2712 ", len: 0x%8.8" PRIx32
2713 ", r/w/a: %i, value: 0x%8.8" PRIx32
2714 ", mask: 0x%8.8" PRIx32
,
2715 watchpoint
->address
,
2717 (int)watchpoint
->rw
,
2720 watchpoint
= watchpoint
->next
;
2725 enum watchpoint_rw type
= WPT_ACCESS
;
2727 uint32_t length
= 0;
2728 uint32_t data_value
= 0x0;
2729 uint32_t data_mask
= 0xffffffff;
2734 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[4], data_mask
);
2737 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[3], data_value
);
2740 switch (CMD_ARGV
[2][0])
2752 LOG_ERROR("invalid watchpoint mode ('%c')", CMD_ARGV
[2][0]);
2753 return ERROR_COMMAND_SYNTAX_ERROR
;
2757 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], length
);
2758 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], addr
);
2762 command_print(CMD_CTX
, "usage: wp [address length "
2763 "[(r|w|a) [value [mask]]]]");
2764 return ERROR_COMMAND_SYNTAX_ERROR
;
2767 int retval
= watchpoint_add(target
, addr
, length
, type
,
2768 data_value
, data_mask
);
2769 if (ERROR_OK
!= retval
)
2770 LOG_ERROR("Failure setting watchpoints");
2775 COMMAND_HANDLER(handle_rwp_command
)
2778 return ERROR_COMMAND_SYNTAX_ERROR
;
2781 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], addr
);
2783 struct target
*target
= get_current_target(CMD_CTX
);
2784 watchpoint_remove(target
, addr
);
2791 * Translate a virtual address to a physical address.
2793 * The low-level target implementation must have logged a detailed error
2794 * which is forwarded to telnet/GDB session.
2796 COMMAND_HANDLER(handle_virt2phys_command
)
2799 return ERROR_COMMAND_SYNTAX_ERROR
;
2802 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], va
);
2805 struct target
*target
= get_current_target(CMD_CTX
);
2806 int retval
= target
->type
->virt2phys(target
, va
, &pa
);
2807 if (retval
== ERROR_OK
)
2808 command_print(CMD_CTX
, "Physical address 0x%08" PRIx32
"", pa
);
2813 static void writeData(FILE *f
, const void *data
, size_t len
)
2815 size_t written
= fwrite(data
, 1, len
, f
);
2817 LOG_ERROR("failed to write %zu bytes: %s", len
, strerror(errno
));
2820 static void writeLong(FILE *f
, int l
)
2823 for (i
= 0; i
< 4; i
++)
2825 char c
= (l
>> (i
*8))&0xff;
2826 writeData(f
, &c
, 1);
2831 static void writeString(FILE *f
, char *s
)
2833 writeData(f
, s
, strlen(s
));
2836 /* Dump a gmon.out histogram file. */
2837 static void writeGmon(uint32_t *samples
, uint32_t sampleNum
, const char *filename
)
2840 FILE *f
= fopen(filename
, "w");
2843 writeString(f
, "gmon");
2844 writeLong(f
, 0x00000001); /* Version */
2845 writeLong(f
, 0); /* padding */
2846 writeLong(f
, 0); /* padding */
2847 writeLong(f
, 0); /* padding */
2849 uint8_t zero
= 0; /* GMON_TAG_TIME_HIST */
2850 writeData(f
, &zero
, 1);
2852 /* figure out bucket size */
2853 uint32_t min
= samples
[0];
2854 uint32_t max
= samples
[0];
2855 for (i
= 0; i
< sampleNum
; i
++)
2857 if (min
> samples
[i
])
2861 if (max
< samples
[i
])
2867 int addressSpace
= (max
-min
+ 1);
2869 static const uint32_t maxBuckets
= 256 * 1024; /* maximum buckets. */
2870 uint32_t length
= addressSpace
;
2871 if (length
> maxBuckets
)
2873 length
= maxBuckets
;
2875 int *buckets
= malloc(sizeof(int)*length
);
2876 if (buckets
== NULL
)
2881 memset(buckets
, 0, sizeof(int)*length
);
2882 for (i
= 0; i
< sampleNum
;i
++)
2884 uint32_t address
= samples
[i
];
2885 long long a
= address
-min
;
2886 long long b
= length
-1;
2887 long long c
= addressSpace
-1;
2888 int index
= (a
*b
)/c
; /* danger!!!! int32 overflows */
2892 /* append binary memory gmon.out &profile_hist_hdr ((char*)&profile_hist_hdr + sizeof(struct gmon_hist_hdr)) */
2893 writeLong(f
, min
); /* low_pc */
2894 writeLong(f
, max
); /* high_pc */
2895 writeLong(f
, length
); /* # of samples */
2896 writeLong(f
, 64000000); /* 64MHz */
2897 writeString(f
, "seconds");
2898 for (i
= 0; i
< (15-strlen("seconds")); i
++)
2899 writeData(f
, &zero
, 1);
2900 writeString(f
, "s");
2902 /*append binary memory gmon.out profile_hist_data (profile_hist_data + profile_hist_hdr.hist_size) */
2904 char *data
= malloc(2*length
);
2907 for (i
= 0; i
< length
;i
++)
2916 data
[i
*2 + 1]=(val
>> 8)&0xff;
2919 writeData(f
, data
, length
* 2);
2929 /* profiling samples the CPU PC as quickly as OpenOCD is able, which will be used as a random sampling of PC */
2930 COMMAND_HANDLER(handle_profile_command
)
2932 struct target
*target
= get_current_target(CMD_CTX
);
2933 struct timeval timeout
, now
;
2935 gettimeofday(&timeout
, NULL
);
2938 return ERROR_COMMAND_SYNTAX_ERROR
;
2941 COMMAND_PARSE_NUMBER(uint
, CMD_ARGV
[0], offset
);
2943 timeval_add_time(&timeout
, offset
, 0);
2945 command_print(CMD_CTX
, "Starting profiling. Halting and resuming the target as often as we can...");
2947 static const int maxSample
= 10000;
2948 uint32_t *samples
= malloc(sizeof(uint32_t)*maxSample
);
2949 if (samples
== NULL
)
2953 /* hopefully it is safe to cache! We want to stop/restart as quickly as possible. */
2954 struct reg
*reg
= register_get_by_name(target
->reg_cache
, "pc", 1);
2959 target_poll(target
);
2960 if (target
->state
== TARGET_HALTED
)
2962 uint32_t t
=*((uint32_t *)reg
->value
);
2963 samples
[numSamples
++]=t
;
2964 retval
= target_resume(target
, 1, 0, 0, 0); /* current pc, addr = 0, do not handle breakpoints, not debugging */
2965 target_poll(target
);
2966 alive_sleep(10); /* sleep 10ms, i.e. <100 samples/second. */
2967 } else if (target
->state
== TARGET_RUNNING
)
2969 /* We want to quickly sample the PC. */
2970 if ((retval
= target_halt(target
)) != ERROR_OK
)
2977 command_print(CMD_CTX
, "Target not halted or running");
2981 if (retval
!= ERROR_OK
)
2986 gettimeofday(&now
, NULL
);
2987 if ((numSamples
>= maxSample
) || ((now
.tv_sec
>= timeout
.tv_sec
) && (now
.tv_usec
>= timeout
.tv_usec
)))
2989 command_print(CMD_CTX
, "Profiling completed. %d samples.", numSamples
);
2990 if ((retval
= target_poll(target
)) != ERROR_OK
)
2995 if (target
->state
== TARGET_HALTED
)
2997 target_resume(target
, 1, 0, 0, 0); /* current pc, addr = 0, do not handle breakpoints, not debugging */
2999 if ((retval
= target_poll(target
)) != ERROR_OK
)
3004 writeGmon(samples
, numSamples
, CMD_ARGV
[1]);
3005 command_print(CMD_CTX
, "Wrote %s", CMD_ARGV
[1]);
3014 static int new_int_array_element(Jim_Interp
* interp
, const char *varname
, int idx
, uint32_t val
)
3017 Jim_Obj
*nameObjPtr
, *valObjPtr
;
3020 namebuf
= alloc_printf("%s(%d)", varname
, idx
);
3024 nameObjPtr
= Jim_NewStringObj(interp
, namebuf
, -1);
3025 valObjPtr
= Jim_NewIntObj(interp
, val
);
3026 if (!nameObjPtr
|| !valObjPtr
)
3032 Jim_IncrRefCount(nameObjPtr
);
3033 Jim_IncrRefCount(valObjPtr
);
3034 result
= Jim_SetVariable(interp
, nameObjPtr
, valObjPtr
);
3035 Jim_DecrRefCount(interp
, nameObjPtr
);
3036 Jim_DecrRefCount(interp
, valObjPtr
);
3038 /* printf("%s(%d) <= 0%08x\n", varname, idx, val); */
3042 static int jim_mem2array(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
3044 struct command_context
*context
;
3045 struct target
*target
;
3047 context
= Jim_GetAssocData(interp
, "context");
3048 if (context
== NULL
)
3050 LOG_ERROR("mem2array: no command context");
3053 target
= get_current_target(context
);
3056 LOG_ERROR("mem2array: no current target");
3060 return target_mem2array(interp
, target
, argc
-1, argv
+ 1);
3063 static int target_mem2array(Jim_Interp
*interp
, struct target
*target
, int argc
, Jim_Obj
*const *argv
)
3071 const char *varname
;
3075 /* argv[1] = name of array to receive the data
3076 * argv[2] = desired width
3077 * argv[3] = memory address
3078 * argv[4] = count of times to read
3081 Jim_WrongNumArgs(interp
, 1, argv
, "varname width addr nelems");
3084 varname
= Jim_GetString(argv
[0], &len
);
3085 /* given "foo" get space for worse case "foo(%d)" .. add 20 */
3087 e
= Jim_GetLong(interp
, argv
[1], &l
);
3093 e
= Jim_GetLong(interp
, argv
[2], &l
);
3098 e
= Jim_GetLong(interp
, argv
[3], &l
);
3114 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3115 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "Invalid width param, must be 8/16/32", NULL
);
3119 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3120 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "mem2array: zero width read?", NULL
);
3123 if ((addr
+ (len
* width
)) < addr
) {
3124 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3125 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "mem2array: addr + len - wraps to zero?", NULL
);
3128 /* absurd transfer size? */
3130 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3131 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "mem2array: absurd > 64K item request", NULL
);
3136 ((width
== 2) && ((addr
& 1) == 0)) ||
3137 ((width
== 4) && ((addr
& 3) == 0))) {
3141 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3142 sprintf(buf
, "mem2array address: 0x%08" PRIx32
" is not aligned for %" PRId32
" byte reads",
3145 Jim_AppendStrings(interp
, Jim_GetResult(interp
), buf
, NULL
);
3154 size_t buffersize
= 4096;
3155 uint8_t *buffer
= malloc(buffersize
);
3162 /* Slurp... in buffer size chunks */
3164 count
= len
; /* in objects.. */
3165 if (count
> (buffersize
/width
)) {
3166 count
= (buffersize
/width
);
3169 retval
= target_read_memory(target
, addr
, width
, count
, buffer
);
3170 if (retval
!= ERROR_OK
) {
3172 LOG_ERROR("mem2array: Read @ 0x%08x, w=%d, cnt=%d, failed",
3176 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3177 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "mem2array: cannot read memory", NULL
);
3181 v
= 0; /* shut up gcc */
3182 for (i
= 0 ;i
< count
;i
++, n
++) {
3185 v
= target_buffer_get_u32(target
, &buffer
[i
*width
]);
3188 v
= target_buffer_get_u16(target
, &buffer
[i
*width
]);
3191 v
= buffer
[i
] & 0x0ff;
3194 new_int_array_element(interp
, varname
, n
, v
);
3202 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3207 static int get_int_array_element(Jim_Interp
* interp
, const char *varname
, int idx
, uint32_t *val
)
3210 Jim_Obj
*nameObjPtr
, *valObjPtr
;
3214 namebuf
= alloc_printf("%s(%d)", varname
, idx
);
3218 nameObjPtr
= Jim_NewStringObj(interp
, namebuf
, -1);
3225 Jim_IncrRefCount(nameObjPtr
);
3226 valObjPtr
= Jim_GetVariable(interp
, nameObjPtr
, JIM_ERRMSG
);
3227 Jim_DecrRefCount(interp
, nameObjPtr
);
3229 if (valObjPtr
== NULL
)
3232 result
= Jim_GetLong(interp
, valObjPtr
, &l
);
3233 /* printf("%s(%d) => 0%08x\n", varname, idx, val); */
3238 static int jim_array2mem(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
3240 struct command_context
*context
;
3241 struct target
*target
;
3243 context
= Jim_GetAssocData(interp
, "context");
3244 if (context
== NULL
) {
3245 LOG_ERROR("array2mem: no command context");
3248 target
= get_current_target(context
);
3249 if (target
== NULL
) {
3250 LOG_ERROR("array2mem: no current target");
3254 return target_array2mem(interp
,target
, argc
-1, argv
+ 1);
3256 static int target_array2mem(Jim_Interp
*interp
, struct target
*target
, int argc
, Jim_Obj
*const *argv
)
3264 const char *varname
;
3268 /* argv[1] = name of array to get the data
3269 * argv[2] = desired width
3270 * argv[3] = memory address
3271 * argv[4] = count to write
3274 Jim_WrongNumArgs(interp
, 0, argv
, "varname width addr nelems");
3277 varname
= Jim_GetString(argv
[0], &len
);
3278 /* given "foo" get space for worse case "foo(%d)" .. add 20 */
3280 e
= Jim_GetLong(interp
, argv
[1], &l
);
3286 e
= Jim_GetLong(interp
, argv
[2], &l
);
3291 e
= Jim_GetLong(interp
, argv
[3], &l
);
3307 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3308 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "Invalid width param, must be 8/16/32", NULL
);
3312 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3313 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "array2mem: zero width read?", NULL
);
3316 if ((addr
+ (len
* width
)) < addr
) {
3317 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3318 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "array2mem: addr + len - wraps to zero?", NULL
);
3321 /* absurd transfer size? */
3323 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3324 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "array2mem: absurd > 64K item request", NULL
);
3329 ((width
== 2) && ((addr
& 1) == 0)) ||
3330 ((width
== 4) && ((addr
& 3) == 0))) {
3334 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3335 sprintf(buf
, "array2mem address: 0x%08x is not aligned for %d byte reads",
3338 Jim_AppendStrings(interp
, Jim_GetResult(interp
), buf
, NULL
);
3349 size_t buffersize
= 4096;
3350 uint8_t *buffer
= malloc(buffersize
);
3355 /* Slurp... in buffer size chunks */
3357 count
= len
; /* in objects.. */
3358 if (count
> (buffersize
/width
)) {
3359 count
= (buffersize
/width
);
3362 v
= 0; /* shut up gcc */
3363 for (i
= 0 ;i
< count
;i
++, n
++) {
3364 get_int_array_element(interp
, varname
, n
, &v
);
3367 target_buffer_set_u32(target
, &buffer
[i
*width
], v
);
3370 target_buffer_set_u16(target
, &buffer
[i
*width
], v
);
3373 buffer
[i
] = v
& 0x0ff;
3379 retval
= target_write_memory(target
, addr
, width
, count
, buffer
);
3380 if (retval
!= ERROR_OK
) {
3382 LOG_ERROR("array2mem: Write @ 0x%08x, w=%d, cnt=%d, failed",
3386 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3387 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "array2mem: cannot read memory", NULL
);
3395 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3400 void target_all_handle_event(enum target_event e
)
3402 struct target
*target
;
3404 LOG_DEBUG("**all*targets: event: %d, %s",
3406 Jim_Nvp_value2name_simple(nvp_target_event
, e
)->name
);
3408 target
= all_targets
;
3410 target_handle_event(target
, e
);
3411 target
= target
->next
;
3416 /* FIX? should we propagate errors here rather than printing them
3419 void target_handle_event(struct target
*target
, enum target_event e
)
3421 struct target_event_action
*teap
;
3423 for (teap
= target
->event_action
; teap
!= NULL
; teap
= teap
->next
) {
3424 if (teap
->event
== e
) {
3425 LOG_DEBUG("target: (%d) %s (%s) event: %d (%s) action: %s",
3426 target
->target_number
,
3427 target_name(target
),
3428 target_type_name(target
),
3430 Jim_Nvp_value2name_simple(nvp_target_event
, e
)->name
,
3431 Jim_GetString(teap
->body
, NULL
));
3432 if (Jim_EvalObj(teap
->interp
, teap
->body
) != JIM_OK
)
3434 Jim_PrintErrorMessage(teap
->interp
);
3441 * Returns true only if the target has a handler for the specified event.
3443 bool target_has_event_action(struct target
*target
, enum target_event event
)
3445 struct target_event_action
*teap
;
3447 for (teap
= target
->event_action
; teap
!= NULL
; teap
= teap
->next
) {
3448 if (teap
->event
== event
)
3454 enum target_cfg_param
{
3457 TCFG_WORK_AREA_VIRT
,
3458 TCFG_WORK_AREA_PHYS
,
3459 TCFG_WORK_AREA_SIZE
,
3460 TCFG_WORK_AREA_BACKUP
,
3463 TCFG_CHAIN_POSITION
,
3466 static Jim_Nvp nvp_config_opts
[] = {
3467 { .name
= "-type", .value
= TCFG_TYPE
},
3468 { .name
= "-event", .value
= TCFG_EVENT
},
3469 { .name
= "-work-area-virt", .value
= TCFG_WORK_AREA_VIRT
},
3470 { .name
= "-work-area-phys", .value
= TCFG_WORK_AREA_PHYS
},
3471 { .name
= "-work-area-size", .value
= TCFG_WORK_AREA_SIZE
},
3472 { .name
= "-work-area-backup", .value
= TCFG_WORK_AREA_BACKUP
},
3473 { .name
= "-endian" , .value
= TCFG_ENDIAN
},
3474 { .name
= "-variant", .value
= TCFG_VARIANT
},
3475 { .name
= "-chain-position", .value
= TCFG_CHAIN_POSITION
},
3477 { .name
= NULL
, .value
= -1 }
3480 static int target_configure(Jim_GetOptInfo
*goi
, struct target
*target
)
3488 /* parse config or cget options ... */
3489 while (goi
->argc
> 0) {
3490 Jim_SetEmptyResult(goi
->interp
);
3491 /* Jim_GetOpt_Debug(goi); */
3493 if (target
->type
->target_jim_configure
) {
3494 /* target defines a configure function */
3495 /* target gets first dibs on parameters */
3496 e
= (*(target
->type
->target_jim_configure
))(target
, goi
);
3505 /* otherwise we 'continue' below */
3507 e
= Jim_GetOpt_Nvp(goi
, nvp_config_opts
, &n
);
3509 Jim_GetOpt_NvpUnknown(goi
, nvp_config_opts
, 0);
3515 if (goi
->isconfigure
) {
3516 Jim_SetResult_sprintf(goi
->interp
,
3517 "not settable: %s", n
->name
);
3521 if (goi
->argc
!= 0) {
3522 Jim_WrongNumArgs(goi
->interp
,
3523 goi
->argc
, goi
->argv
,
3528 Jim_SetResultString(goi
->interp
,
3529 target_type_name(target
), -1);
3533 if (goi
->argc
== 0) {
3534 Jim_WrongNumArgs(goi
->interp
, goi
->argc
, goi
->argv
, "-event ?event-name? ...");
3538 e
= Jim_GetOpt_Nvp(goi
, nvp_target_event
, &n
);
3540 Jim_GetOpt_NvpUnknown(goi
, nvp_target_event
, 1);
3544 if (goi
->isconfigure
) {
3545 if (goi
->argc
!= 1) {
3546 Jim_WrongNumArgs(goi
->interp
, goi
->argc
, goi
->argv
, "-event ?event-name? ?EVENT-BODY?");
3550 if (goi
->argc
!= 0) {
3551 Jim_WrongNumArgs(goi
->interp
, goi
->argc
, goi
->argv
, "-event ?event-name?");
3557 struct target_event_action
*teap
;
3559 teap
= target
->event_action
;
3560 /* replace existing? */
3562 if (teap
->event
== (enum target_event
)n
->value
) {
3568 if (goi
->isconfigure
) {
3569 bool replace
= true;
3572 teap
= calloc(1, sizeof(*teap
));
3575 teap
->event
= n
->value
;
3576 teap
->interp
= goi
->interp
;
3577 Jim_GetOpt_Obj(goi
, &o
);
3579 Jim_DecrRefCount(teap
->interp
, teap
->body
);
3581 teap
->body
= Jim_DuplicateObj(goi
->interp
, o
);
3584 * Tcl/TK - "tk events" have a nice feature.
3585 * See the "BIND" command.
3586 * We should support that here.
3587 * You can specify %X and %Y in the event code.
3588 * The idea is: %T - target name.
3589 * The idea is: %N - target number
3590 * The idea is: %E - event name.
3592 Jim_IncrRefCount(teap
->body
);
3596 /* add to head of event list */
3597 teap
->next
= target
->event_action
;
3598 target
->event_action
= teap
;
3600 Jim_SetEmptyResult(goi
->interp
);
3604 Jim_SetEmptyResult(goi
->interp
);
3606 Jim_SetResult(goi
->interp
, Jim_DuplicateObj(goi
->interp
, teap
->body
));
3613 case TCFG_WORK_AREA_VIRT
:
3614 if (goi
->isconfigure
) {
3615 target_free_all_working_areas(target
);
3616 e
= Jim_GetOpt_Wide(goi
, &w
);
3620 target
->working_area_virt
= w
;
3621 target
->working_area_virt_spec
= true;
3623 if (goi
->argc
!= 0) {
3627 Jim_SetResult(goi
->interp
, Jim_NewIntObj(goi
->interp
, target
->working_area_virt
));
3631 case TCFG_WORK_AREA_PHYS
:
3632 if (goi
->isconfigure
) {
3633 target_free_all_working_areas(target
);
3634 e
= Jim_GetOpt_Wide(goi
, &w
);
3638 target
->working_area_phys
= w
;
3639 target
->working_area_phys_spec
= true;
3641 if (goi
->argc
!= 0) {
3645 Jim_SetResult(goi
->interp
, Jim_NewIntObj(goi
->interp
, target
->working_area_phys
));
3649 case TCFG_WORK_AREA_SIZE
:
3650 if (goi
->isconfigure
) {
3651 target_free_all_working_areas(target
);
3652 e
= Jim_GetOpt_Wide(goi
, &w
);
3656 target
->working_area_size
= w
;
3658 if (goi
->argc
!= 0) {
3662 Jim_SetResult(goi
->interp
, Jim_NewIntObj(goi
->interp
, target
->working_area_size
));
3666 case TCFG_WORK_AREA_BACKUP
:
3667 if (goi
->isconfigure
) {
3668 target_free_all_working_areas(target
);
3669 e
= Jim_GetOpt_Wide(goi
, &w
);
3673 /* make this exactly 1 or 0 */
3674 target
->backup_working_area
= (!!w
);
3676 if (goi
->argc
!= 0) {
3680 Jim_SetResult(goi
->interp
, Jim_NewIntObj(goi
->interp
, target
->backup_working_area
));
3681 /* loop for more e*/
3685 if (goi
->isconfigure
) {
3686 e
= Jim_GetOpt_Nvp(goi
, nvp_target_endian
, &n
);
3688 Jim_GetOpt_NvpUnknown(goi
, nvp_target_endian
, 1);
3691 target
->endianness
= n
->value
;
3693 if (goi
->argc
!= 0) {
3697 n
= Jim_Nvp_value2name_simple(nvp_target_endian
, target
->endianness
);
3698 if (n
->name
== NULL
) {
3699 target
->endianness
= TARGET_LITTLE_ENDIAN
;
3700 n
= Jim_Nvp_value2name_simple(nvp_target_endian
, target
->endianness
);
3702 Jim_SetResultString(goi
->interp
, n
->name
, -1);
3707 if (goi
->isconfigure
) {
3708 if (goi
->argc
< 1) {
3709 Jim_SetResult_sprintf(goi
->interp
,
3714 if (target
->variant
) {
3715 free((void *)(target
->variant
));
3717 e
= Jim_GetOpt_String(goi
, &cp
, NULL
);
3718 target
->variant
= strdup(cp
);
3720 if (goi
->argc
!= 0) {
3724 Jim_SetResultString(goi
->interp
, target
->variant
,-1);
3727 case TCFG_CHAIN_POSITION
:
3728 if (goi
->isconfigure
) {
3730 struct jtag_tap
*tap
;
3731 target_free_all_working_areas(target
);
3732 e
= Jim_GetOpt_Obj(goi
, &o
);
3736 tap
= jtag_tap_by_jim_obj(goi
->interp
, o
);
3740 /* make this exactly 1 or 0 */
3743 if (goi
->argc
!= 0) {
3747 Jim_SetResultString(goi
->interp
, target
->tap
->dotted_name
, -1);
3748 /* loop for more e*/
3751 } /* while (goi->argc) */
3754 /* done - we return */
3758 static int jim_target_configure(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
3761 Jim_GetOpt_Setup(&goi
, interp
, argc
- 1, argv
+ 1);
3762 goi
.isconfigure
= strcmp(Jim_GetString(argv
[0], NULL
), "configure") == 0;
3763 int need_args
= 1 + goi
.isconfigure
;
3764 if (goi
.argc
< need_args
)
3766 Jim_WrongNumArgs(goi
.interp
, goi
.argc
, goi
.argv
,
3768 ? "missing: -option VALUE ..."
3769 : "missing: -option ...");
3772 struct target
*target
= Jim_CmdPrivData(goi
.interp
);
3773 return target_configure(&goi
, target
);
3776 static int jim_target_mw(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
3778 const char *cmd_name
= Jim_GetString(argv
[0], NULL
);
3781 Jim_GetOpt_Setup(&goi
, interp
, argc
- 1, argv
+ 1);
3783 if (goi
.argc
!= 2 && goi
.argc
!= 3)
3785 Jim_SetResult_sprintf(goi
.interp
,
3786 "usage: %s <address> <data> [<count>]", cmd_name
);
3791 int e
= Jim_GetOpt_Wide(&goi
, &a
);
3796 e
= Jim_GetOpt_Wide(&goi
, &b
);
3803 e
= Jim_GetOpt_Wide(&goi
, &c
);
3808 struct target
*target
= Jim_CmdPrivData(goi
.interp
);
3809 uint8_t target_buf
[32];
3810 if (strcasecmp(cmd_name
, "mww") == 0) {
3811 target_buffer_set_u32(target
, target_buf
, b
);
3814 else if (strcasecmp(cmd_name
, "mwh") == 0) {
3815 target_buffer_set_u16(target
, target_buf
, b
);
3818 else if (strcasecmp(cmd_name
, "mwb") == 0) {
3819 target_buffer_set_u8(target
, target_buf
, b
);
3822 LOG_ERROR("command '%s' unknown: ", cmd_name
);
3826 for (jim_wide x
= 0; x
< c
; x
++)
3828 e
= target_write_memory(target
, a
, b
, 1, target_buf
);
3831 Jim_SetResult_sprintf(interp
,
3832 "Error writing @ 0x%08x: %d\n", (int)(a
), e
);
3841 static int jim_target_md(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
3843 const char *cmd_name
= Jim_GetString(argv
[0], NULL
);
3846 Jim_GetOpt_Setup(&goi
, interp
, argc
- 1, argv
+ 1);
3848 if ((goi
.argc
== 2) || (goi
.argc
== 3))
3850 Jim_SetResult_sprintf(goi
.interp
,
3851 "usage: %s <address> [<count>]", cmd_name
);
3856 int e
= Jim_GetOpt_Wide(&goi
, &a
);
3862 e
= Jim_GetOpt_Wide(&goi
, &c
);
3869 jim_wide b
= 1; /* shut up gcc */
3870 if (strcasecmp(cmd_name
, "mdw") == 0)
3872 else if (strcasecmp(cmd_name
, "mdh") == 0)
3874 else if (strcasecmp(cmd_name
, "mdb") == 0)
3877 LOG_ERROR("command '%s' unknown: ", cmd_name
);
3881 /* convert count to "bytes" */
3884 struct target
*target
= Jim_CmdPrivData(goi
.interp
);
3885 uint8_t target_buf
[32];
3892 e
= target_read_memory(target
, a
, b
, y
/ b
, target_buf
);
3893 if (e
!= ERROR_OK
) {
3894 Jim_SetResult_sprintf(interp
, "error reading target @ 0x%08lx", (int)(a
));
3898 Jim_fprintf(interp
, interp
->cookie_stdout
, "0x%08x ", (int)(a
));
3901 for (x
= 0; x
< 16 && x
< y
; x
+= 4)
3903 z
= target_buffer_get_u32(target
, &(target_buf
[ x
* 4 ]));
3904 Jim_fprintf(interp
, interp
->cookie_stdout
, "%08x ", (int)(z
));
3906 for (; (x
< 16) ; x
+= 4) {
3907 Jim_fprintf(interp
, interp
->cookie_stdout
, " ");
3911 for (x
= 0; x
< 16 && x
< y
; x
+= 2)
3913 z
= target_buffer_get_u16(target
, &(target_buf
[ x
* 2 ]));
3914 Jim_fprintf(interp
, interp
->cookie_stdout
, "%04x ", (int)(z
));
3916 for (; (x
< 16) ; x
+= 2) {
3917 Jim_fprintf(interp
, interp
->cookie_stdout
, " ");
3922 for (x
= 0 ; (x
< 16) && (x
< y
) ; x
+= 1) {
3923 z
= target_buffer_get_u8(target
, &(target_buf
[ x
* 4 ]));
3924 Jim_fprintf(interp
, interp
->cookie_stdout
, "%02x ", (int)(z
));
3926 for (; (x
< 16) ; x
+= 1) {
3927 Jim_fprintf(interp
, interp
->cookie_stdout
, " ");
3931 /* ascii-ify the bytes */
3932 for (x
= 0 ; x
< y
; x
++) {
3933 if ((target_buf
[x
] >= 0x20) &&
3934 (target_buf
[x
] <= 0x7e)) {
3938 target_buf
[x
] = '.';
3943 target_buf
[x
] = ' ';
3948 /* print - with a newline */
3949 Jim_fprintf(interp
, interp
->cookie_stdout
, "%s\n", target_buf
);
3957 static int jim_target_mem2array(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
3959 struct target
*target
= Jim_CmdPrivData(interp
);
3960 return target_mem2array(interp
, target
, argc
- 1, argv
+ 1);
3963 static int jim_target_array2mem(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
3965 struct target
*target
= Jim_CmdPrivData(interp
);
3966 return target_array2mem(interp
, target
, argc
- 1, argv
+ 1);
3969 static int jim_target_tap_disabled(Jim_Interp
*interp
)
3971 Jim_SetResult_sprintf(interp
, "[TAP is disabled]");
3975 static int jim_target_examine(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
3979 Jim_WrongNumArgs(interp
, 1, argv
, "[no parameters]");
3982 struct target
*target
= Jim_CmdPrivData(interp
);
3983 if (!target
->tap
->enabled
)
3984 return jim_target_tap_disabled(interp
);
3986 int e
= target
->type
->examine(target
);
3989 Jim_SetResult_sprintf(interp
, "examine-fails: %d", e
);
3995 static int jim_target_poll(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
3999 Jim_WrongNumArgs(interp
, 1, argv
, "[no parameters]");
4002 struct target
*target
= Jim_CmdPrivData(interp
);
4003 if (!target
->tap
->enabled
)
4004 return jim_target_tap_disabled(interp
);
4007 if (!(target_was_examined(target
))) {
4008 e
= ERROR_TARGET_NOT_EXAMINED
;
4010 e
= target
->type
->poll(target
);
4014 Jim_SetResult_sprintf(interp
, "poll-fails: %d", e
);
4020 static int jim_target_reset(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4023 Jim_GetOpt_Setup(&goi
, interp
, argc
- 1, argv
+ 1);
4027 Jim_WrongNumArgs(interp
, 0, argv
,
4028 "([tT]|[fF]|assert|deassert) BOOL");
4033 int e
= Jim_GetOpt_Nvp(&goi
, nvp_assert
, &n
);
4036 Jim_GetOpt_NvpUnknown(&goi
, nvp_assert
, 1);
4039 /* the halt or not param */
4041 e
= Jim_GetOpt_Wide(&goi
, &a
);
4045 struct target
*target
= Jim_CmdPrivData(goi
.interp
);
4046 if (!target
->tap
->enabled
)
4047 return jim_target_tap_disabled(interp
);
4048 if (!target
->type
->assert_reset
|| !target
->type
->deassert_reset
)
4050 Jim_SetResult_sprintf(interp
,
4051 "No target-specific reset for %s",
4052 target_name(target
));
4055 /* determine if we should halt or not. */
4056 target
->reset_halt
= !!a
;
4057 /* When this happens - all workareas are invalid. */
4058 target_free_all_working_areas_restore(target
, 0);
4061 if (n
->value
== NVP_ASSERT
) {
4062 e
= target
->type
->assert_reset(target
);
4064 e
= target
->type
->deassert_reset(target
);
4066 return (e
== ERROR_OK
) ? JIM_OK
: JIM_ERR
;
4069 static int jim_target_halt(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4072 Jim_WrongNumArgs(interp
, 1, argv
, "[no parameters]");
4075 struct target
*target
= Jim_CmdPrivData(interp
);
4076 if (!target
->tap
->enabled
)
4077 return jim_target_tap_disabled(interp
);
4078 int e
= target
->type
->halt(target
);
4079 return (e
== ERROR_OK
) ? JIM_OK
: JIM_ERR
;
4082 static int jim_target_wait_state(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4085 Jim_GetOpt_Setup(&goi
, interp
, argc
- 1, argv
+ 1);
4087 /* params: <name> statename timeoutmsecs */
4090 const char *cmd_name
= Jim_GetString(argv
[0], NULL
);
4091 Jim_SetResult_sprintf(goi
.interp
,
4092 "%s <state_name> <timeout_in_msec>", cmd_name
);
4097 int e
= Jim_GetOpt_Nvp(&goi
, nvp_target_state
, &n
);
4099 Jim_GetOpt_NvpUnknown(&goi
, nvp_target_state
,1);
4103 e
= Jim_GetOpt_Wide(&goi
, &a
);
4107 struct target
*target
= Jim_CmdPrivData(interp
);
4108 if (!target
->tap
->enabled
)
4109 return jim_target_tap_disabled(interp
);
4111 e
= target_wait_state(target
, n
->value
, a
);
4114 Jim_SetResult_sprintf(goi
.interp
,
4115 "target: %s wait %s fails (%d) %s",
4116 target_name(target
), n
->name
,
4117 e
, target_strerror_safe(e
));
4122 /* List for human, Events defined for this target.
4123 * scripts/programs should use 'name cget -event NAME'
4125 static int jim_target_event_list(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4127 struct command_context
*cmd_ctx
= Jim_GetAssocData(interp
, "context");
4128 struct target
*target
= Jim_CmdPrivData(interp
);
4129 struct target_event_action
*teap
= target
->event_action
;
4130 command_print(cmd_ctx
, "Event actions for target (%d) %s\n",
4131 target
->target_number
,
4132 target_name(target
));
4133 command_print(cmd_ctx
, "%-25s | Body", "Event");
4134 command_print(cmd_ctx
, "------------------------- | "
4135 "----------------------------------------");
4138 Jim_Nvp
*opt
= Jim_Nvp_value2name_simple(nvp_target_event
, teap
->event
);
4139 command_print(cmd_ctx
, "%-25s | %s",
4140 opt
->name
, Jim_GetString(teap
->body
, NULL
));
4143 command_print(cmd_ctx
, "***END***");
4146 static int jim_target_current_state(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4150 Jim_WrongNumArgs(interp
, 1, argv
, "[no parameters]");
4153 struct target
*target
= Jim_CmdPrivData(interp
);
4154 Jim_SetResultString(interp
, target_state_name(target
), -1);
4157 static int jim_target_invoke_event(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4160 Jim_GetOpt_Setup(&goi
, interp
, argc
- 1, argv
+ 1);
4163 const char *cmd_name
= Jim_GetString(argv
[0], NULL
);
4164 Jim_SetResult_sprintf(goi
.interp
, "%s <eventname>", cmd_name
);
4168 int e
= Jim_GetOpt_Nvp(&goi
, nvp_target_event
, &n
);
4171 Jim_GetOpt_NvpUnknown(&goi
, nvp_target_event
, 1);
4174 struct target
*target
= Jim_CmdPrivData(interp
);
4175 target_handle_event(target
, n
->value
);
4179 static const struct command_registration target_instance_command_handlers
[] = {
4181 .name
= "configure",
4182 .mode
= COMMAND_CONFIG
,
4183 .jim_handler
= &jim_target_configure
,
4184 .usage
= "[<target_options> ...]",
4185 .help
= "configure a new target for use",
4189 .mode
= COMMAND_ANY
,
4190 .jim_handler
= &jim_target_configure
,
4191 .usage
= "<target_type> [<target_options> ...]",
4192 .help
= "configure a new target for use",
4196 .mode
= COMMAND_EXEC
,
4197 .jim_handler
= &jim_target_mw
,
4198 .usage
= "<address> <data> [<count>]",
4199 .help
= "Write 32-bit word(s) to target memory",
4203 .mode
= COMMAND_EXEC
,
4204 .jim_handler
= &jim_target_mw
,
4205 .usage
= "<address> <data> [<count>]",
4206 .help
= "Write 16-bit half-word(s) to target memory",
4210 .mode
= COMMAND_EXEC
,
4211 .jim_handler
= &jim_target_mw
,
4212 .usage
= "<address> <data> [<count>]",
4213 .help
= "Write byte(s) to target memory",
4217 .mode
= COMMAND_EXEC
,
4218 .jim_handler
= &jim_target_md
,
4219 .usage
= "<address> [<count>]",
4220 .help
= "Display target memory as 32-bit words",
4224 .mode
= COMMAND_EXEC
,
4225 .jim_handler
= &jim_target_md
,
4226 .usage
= "<address> [<count>]",
4227 .help
= "Display target memory as 16-bit half-words",
4231 .mode
= COMMAND_EXEC
,
4232 .jim_handler
= &jim_target_md
,
4233 .usage
= "<address> [<count>]",
4234 .help
= "Display target memory as 8-bit bytes",
4237 .name
= "array2mem",
4238 .mode
= COMMAND_EXEC
,
4239 .jim_handler
= &jim_target_array2mem
,
4242 .name
= "mem2array",
4243 .mode
= COMMAND_EXEC
,
4244 .jim_handler
= &jim_target_mem2array
,
4247 .name
= "eventlist",
4248 .mode
= COMMAND_EXEC
,
4249 .jim_handler
= &jim_target_event_list
,
4253 .mode
= COMMAND_EXEC
,
4254 .jim_handler
= &jim_target_current_state
,
4257 .name
= "arp_examine",
4258 .mode
= COMMAND_EXEC
,
4259 .jim_handler
= &jim_target_examine
,
4263 .mode
= COMMAND_EXEC
,
4264 .jim_handler
= &jim_target_poll
,
4267 .name
= "arp_reset",
4268 .mode
= COMMAND_EXEC
,
4269 .jim_handler
= &jim_target_reset
,
4273 .mode
= COMMAND_EXEC
,
4274 .jim_handler
= &jim_target_halt
,
4277 .name
= "arp_waitstate",
4278 .mode
= COMMAND_EXEC
,
4279 .jim_handler
= &jim_target_wait_state
,
4282 .name
= "invoke-event",
4283 .mode
= COMMAND_EXEC
,
4284 .jim_handler
= &jim_target_invoke_event
,
4286 COMMAND_REGISTRATION_DONE
4289 static int target_create(Jim_GetOptInfo
*goi
)
4297 struct target
*target
;
4298 struct command_context
*cmd_ctx
;
4300 cmd_ctx
= Jim_GetAssocData(goi
->interp
, "context");
4301 if (goi
->argc
< 3) {
4302 Jim_WrongNumArgs(goi
->interp
, 1, goi
->argv
, "?name? ?type? ..options...");
4307 Jim_GetOpt_Obj(goi
, &new_cmd
);
4308 /* does this command exist? */
4309 cmd
= Jim_GetCommand(goi
->interp
, new_cmd
, JIM_ERRMSG
);
4311 cp
= Jim_GetString(new_cmd
, NULL
);
4312 Jim_SetResult_sprintf(goi
->interp
, "Command/target: %s Exists", cp
);
4317 e
= Jim_GetOpt_String(goi
, &cp2
, NULL
);
4319 /* now does target type exist */
4320 for (x
= 0 ; target_types
[x
] ; x
++) {
4321 if (0 == strcmp(cp
, target_types
[x
]->name
)) {
4326 if (target_types
[x
] == NULL
) {
4327 Jim_SetResult_sprintf(goi
->interp
, "Unknown target type %s, try one of ", cp
);
4328 for (x
= 0 ; target_types
[x
] ; x
++) {
4329 if (target_types
[x
+ 1]) {
4330 Jim_AppendStrings(goi
->interp
,
4331 Jim_GetResult(goi
->interp
),
4332 target_types
[x
]->name
,
4335 Jim_AppendStrings(goi
->interp
,
4336 Jim_GetResult(goi
->interp
),
4338 target_types
[x
]->name
,NULL
);
4345 target
= calloc(1,sizeof(struct target
));
4346 /* set target number */
4347 target
->target_number
= new_target_number();
4349 /* allocate memory for each unique target type */
4350 target
->type
= (struct target_type
*)calloc(1,sizeof(struct target_type
));
4352 memcpy(target
->type
, target_types
[x
], sizeof(struct target_type
));
4354 /* will be set by "-endian" */
4355 target
->endianness
= TARGET_ENDIAN_UNKNOWN
;
4357 target
->working_area
= 0x0;
4358 target
->working_area_size
= 0x0;
4359 target
->working_areas
= NULL
;
4360 target
->backup_working_area
= 0;
4362 target
->state
= TARGET_UNKNOWN
;
4363 target
->debug_reason
= DBG_REASON_UNDEFINED
;
4364 target
->reg_cache
= NULL
;
4365 target
->breakpoints
= NULL
;
4366 target
->watchpoints
= NULL
;
4367 target
->next
= NULL
;
4368 target
->arch_info
= NULL
;
4370 target
->display
= 1;
4372 target
->halt_issued
= false;
4374 /* initialize trace information */
4375 target
->trace_info
= malloc(sizeof(struct trace
));
4376 target
->trace_info
->num_trace_points
= 0;
4377 target
->trace_info
->trace_points_size
= 0;
4378 target
->trace_info
->trace_points
= NULL
;
4379 target
->trace_info
->trace_history_size
= 0;
4380 target
->trace_info
->trace_history
= NULL
;
4381 target
->trace_info
->trace_history_pos
= 0;
4382 target
->trace_info
->trace_history_overflowed
= 0;
4384 target
->dbgmsg
= NULL
;
4385 target
->dbg_msg_enabled
= 0;
4387 target
->endianness
= TARGET_ENDIAN_UNKNOWN
;
4389 /* Do the rest as "configure" options */
4390 goi
->isconfigure
= 1;
4391 e
= target_configure(goi
, target
);
4393 if (target
->tap
== NULL
)
4395 Jim_SetResultString(goi
->interp
, "-chain-position required when creating target", -1);
4405 if (target
->endianness
== TARGET_ENDIAN_UNKNOWN
) {
4406 /* default endian to little if not specified */
4407 target
->endianness
= TARGET_LITTLE_ENDIAN
;
4410 /* incase variant is not set */
4411 if (!target
->variant
)
4412 target
->variant
= strdup("");
4414 cp
= Jim_GetString(new_cmd
, NULL
);
4415 target
->cmd_name
= strdup(cp
);
4417 /* create the target specific commands */
4418 if (target
->type
->commands
) {
4419 e
= register_commands(cmd_ctx
, NULL
, target
->type
->commands
);
4421 LOG_ERROR("unable to register '%s' commands", cp
);
4423 if (target
->type
->target_create
) {
4424 (*(target
->type
->target_create
))(target
, goi
->interp
);
4427 /* append to end of list */
4429 struct target
**tpp
;
4430 tpp
= &(all_targets
);
4432 tpp
= &((*tpp
)->next
);
4437 /* now - create the new target name command */
4438 const const struct command_registration target_subcommands
[] = {
4440 .chain
= target_instance_command_handlers
,
4443 .chain
= target
->type
->commands
,
4445 COMMAND_REGISTRATION_DONE
4447 const const struct command_registration target_commands
[] = {
4450 .mode
= COMMAND_ANY
,
4451 .help
= "target command group",
4452 .chain
= target_subcommands
,
4454 COMMAND_REGISTRATION_DONE
4456 e
= register_commands(cmd_ctx
, NULL
, target_commands
);
4460 struct command
*c
= command_find_in_context(cmd_ctx
, cp
);
4462 command_set_handler_data(c
, target
);
4464 return (ERROR_OK
== e
) ? JIM_OK
: JIM_ERR
;
4467 static int jim_target_current(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4471 Jim_WrongNumArgs(interp
, 1, argv
, "Too many parameters");
4474 struct command_context
*cmd_ctx
= Jim_GetAssocData(interp
, "context");
4475 Jim_SetResultString(interp
, get_current_target(cmd_ctx
)->cmd_name
, -1);
4479 static int jim_target_types(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4483 Jim_WrongNumArgs(interp
, 1, argv
, "Too many parameters");
4486 Jim_SetResult(interp
, Jim_NewListObj(interp
, NULL
, 0));
4487 for (unsigned x
= 0; NULL
!= target_types
[x
]; x
++)
4489 Jim_ListAppendElement(interp
, Jim_GetResult(interp
),
4490 Jim_NewStringObj(interp
, target_types
[x
]->name
, -1));
4495 static int jim_target_names(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4499 Jim_WrongNumArgs(interp
, 1, argv
, "Too many parameters");
4502 Jim_SetResult(interp
, Jim_NewListObj(interp
, NULL
, 0));
4503 struct target
*target
= all_targets
;
4506 Jim_ListAppendElement(interp
, Jim_GetResult(interp
),
4507 Jim_NewStringObj(interp
, target_name(target
), -1));
4508 target
= target
->next
;
4513 static int jim_target_create(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4516 Jim_GetOpt_Setup(&goi
, interp
, argc
- 1, argv
+ 1);
4519 Jim_WrongNumArgs(goi
.interp
, goi
.argc
, goi
.argv
,
4520 "<name> <target_type> [<target_options> ...]");
4523 return target_create(&goi
);
4526 static int jim_target_number(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4529 Jim_GetOpt_Setup(&goi
, interp
, argc
- 1, argv
+ 1);
4531 /* It's OK to remove this mechanism sometime after August 2010 or so */
4532 LOG_WARNING("don't use numbers as target identifiers; use names");
4535 Jim_SetResult_sprintf(goi
.interp
, "usage: target number <number>");
4539 int e
= Jim_GetOpt_Wide(&goi
, &w
);
4543 struct target
*target
;
4544 for (target
= all_targets
; NULL
!= target
; target
= target
->next
)
4546 if (target
->target_number
!= w
)
4549 Jim_SetResultString(goi
.interp
, target_name(target
), -1);
4552 Jim_SetResult_sprintf(goi
.interp
,
4553 "Target: number %d does not exist", (int)(w
));
4557 static int jim_target_count(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4561 Jim_WrongNumArgs(interp
, 1, argv
, "<no parameters>");
4565 struct target
*target
= all_targets
;
4566 while (NULL
!= target
)
4568 target
= target
->next
;
4571 Jim_SetResult(interp
, Jim_NewIntObj(interp
, count
));
4575 static const struct command_registration target_subcommand_handlers
[] = {
4578 .mode
= COMMAND_ANY
,
4579 .jim_handler
= &jim_target_create
,
4580 .usage
= "<name> <type> ...",
4581 .help
= "Returns the currently selected target",
4585 .mode
= COMMAND_ANY
,
4586 .jim_handler
= &jim_target_current
,
4587 .help
= "Returns the currently selected target",
4591 .mode
= COMMAND_ANY
,
4592 .jim_handler
= &jim_target_types
,
4593 .help
= "Returns the available target types as a list of strings",
4597 .mode
= COMMAND_ANY
,
4598 .jim_handler
= &jim_target_names
,
4599 .help
= "Returns the names of all targets as a list of strings",
4603 .mode
= COMMAND_ANY
,
4604 .jim_handler
= &jim_target_number
,
4605 .usage
= "<number>",
4606 .help
= "Returns the name of target <n>",
4610 .mode
= COMMAND_ANY
,
4611 .jim_handler
= &jim_target_count
,
4612 .help
= "Returns the number of targets as an integer",
4614 COMMAND_REGISTRATION_DONE
4626 static int fastload_num
;
4627 static struct FastLoad
*fastload
;
4629 static void free_fastload(void)
4631 if (fastload
!= NULL
)
4634 for (i
= 0; i
< fastload_num
; i
++)
4636 if (fastload
[i
].data
)
4637 free(fastload
[i
].data
);
4647 COMMAND_HANDLER(handle_fast_load_image_command
)
4651 uint32_t image_size
;
4652 uint32_t min_address
= 0;
4653 uint32_t max_address
= 0xffffffff;
4658 int retval
= CALL_COMMAND_HANDLER(parse_load_image_command_CMD_ARGV
,
4659 &image
, &min_address
, &max_address
);
4660 if (ERROR_OK
!= retval
)
4663 struct duration bench
;
4664 duration_start(&bench
);
4666 if (image_open(&image
, CMD_ARGV
[0], (CMD_ARGC
>= 3) ? CMD_ARGV
[2] : NULL
) != ERROR_OK
)
4673 fastload_num
= image
.num_sections
;
4674 fastload
= (struct FastLoad
*)malloc(sizeof(struct FastLoad
)*image
.num_sections
);
4675 if (fastload
== NULL
)
4677 image_close(&image
);
4680 memset(fastload
, 0, sizeof(struct FastLoad
)*image
.num_sections
);
4681 for (i
= 0; i
< image
.num_sections
; i
++)
4683 buffer
= malloc(image
.sections
[i
].size
);
4686 command_print(CMD_CTX
, "error allocating buffer for section (%d bytes)",
4687 (int)(image
.sections
[i
].size
));
4691 if ((retval
= image_read_section(&image
, i
, 0x0, image
.sections
[i
].size
, buffer
, &buf_cnt
)) != ERROR_OK
)
4697 uint32_t offset
= 0;
4698 uint32_t length
= buf_cnt
;
4701 /* DANGER!!! beware of unsigned comparision here!!! */
4703 if ((image
.sections
[i
].base_address
+ buf_cnt
>= min_address
)&&
4704 (image
.sections
[i
].base_address
< max_address
))
4706 if (image
.sections
[i
].base_address
< min_address
)
4708 /* clip addresses below */
4709 offset
+= min_address
-image
.sections
[i
].base_address
;
4713 if (image
.sections
[i
].base_address
+ buf_cnt
> max_address
)
4715 length
-= (image
.sections
[i
].base_address
+ buf_cnt
)-max_address
;
4718 fastload
[i
].address
= image
.sections
[i
].base_address
+ offset
;
4719 fastload
[i
].data
= malloc(length
);
4720 if (fastload
[i
].data
== NULL
)
4725 memcpy(fastload
[i
].data
, buffer
+ offset
, length
);
4726 fastload
[i
].length
= length
;
4728 image_size
+= length
;
4729 command_print(CMD_CTX
, "%u bytes written at address 0x%8.8x",
4730 (unsigned int)length
,
4731 ((unsigned int)(image
.sections
[i
].base_address
+ offset
)));
4737 if ((ERROR_OK
== retval
) && (duration_measure(&bench
) == ERROR_OK
))
4739 command_print(CMD_CTX
, "Loaded %" PRIu32
" bytes "
4740 "in %fs (%0.3f kb/s)", image_size
,
4741 duration_elapsed(&bench
), duration_kbps(&bench
, image_size
));
4743 command_print(CMD_CTX
,
4744 "WARNING: image has not been loaded to target!"
4745 "You can issue a 'fast_load' to finish loading.");
4748 image_close(&image
);
4750 if (retval
!= ERROR_OK
)
4758 COMMAND_HANDLER(handle_fast_load_command
)
4761 return ERROR_COMMAND_SYNTAX_ERROR
;
4762 if (fastload
== NULL
)
4764 LOG_ERROR("No image in memory");
4768 int ms
= timeval_ms();
4770 int retval
= ERROR_OK
;
4771 for (i
= 0; i
< fastload_num
;i
++)
4773 struct target
*target
= get_current_target(CMD_CTX
);
4774 command_print(CMD_CTX
, "Write to 0x%08x, length 0x%08x",
4775 (unsigned int)(fastload
[i
].address
),
4776 (unsigned int)(fastload
[i
].length
));
4777 if (retval
== ERROR_OK
)
4779 retval
= target_write_buffer(target
, fastload
[i
].address
, fastload
[i
].length
, fastload
[i
].data
);
4781 size
+= fastload
[i
].length
;
4783 int after
= timeval_ms();
4784 command_print(CMD_CTX
, "Loaded image %f kBytes/s", (float)(size
/1024.0)/((float)(after
-ms
)/1000.0));
4788 static const struct command_registration target_command_handlers
[] = {
4791 .handler
= &handle_targets_command
,
4792 .mode
= COMMAND_ANY
,
4793 .help
= "change current command line target (one parameter) "
4794 "or list targets (no parameters)",
4795 .usage
= "[<new_current_target>]",
4799 .mode
= COMMAND_CONFIG
,
4800 .help
= "configure target",
4802 .chain
= target_subcommand_handlers
,
4804 COMMAND_REGISTRATION_DONE
4807 int target_register_commands(struct command_context
*cmd_ctx
)
4809 return register_commands(cmd_ctx
, NULL
, target_command_handlers
);
4812 static const struct command_registration target_exec_command_handlers
[] = {
4814 .name
= "fast_load_image",
4815 .handler
= &handle_fast_load_image_command
,
4816 .mode
= COMMAND_ANY
,
4817 .help
= "Load image into memory, mainly for profiling purposes",
4818 .usage
= "<file> <address> ['bin'|'ihex'|'elf'|'s19'] "
4819 "[min_address] [max_length]",
4822 .name
= "fast_load",
4823 .handler
= &handle_fast_load_command
,
4824 .mode
= COMMAND_ANY
,
4825 .help
= "loads active fast load image to current target "
4826 "- mainly for profiling purposes",
4830 .handler
= &handle_profile_command
,
4831 .mode
= COMMAND_EXEC
,
4832 .help
= "profiling samples the CPU PC",
4834 /** @todo don't register virt2phys() unless target supports it */
4836 .name
= "virt2phys",
4837 .handler
= &handle_virt2phys_command
,
4838 .mode
= COMMAND_ANY
,
4839 .help
= "translate a virtual address into a physical address",
4844 .handler
= &handle_reg_command
,
4845 .mode
= COMMAND_EXEC
,
4846 .help
= "display or set a register",
4851 .handler
= &handle_poll_command
,
4852 .mode
= COMMAND_EXEC
,
4853 .help
= "poll target state",
4856 .name
= "wait_halt",
4857 .handler
= &handle_wait_halt_command
,
4858 .mode
= COMMAND_EXEC
,
4859 .help
= "wait for target halt",
4860 .usage
= "[time (s)]",
4864 .handler
= &handle_halt_command
,
4865 .mode
= COMMAND_EXEC
,
4866 .help
= "halt target",
4870 .handler
= &handle_resume_command
,
4871 .mode
= COMMAND_EXEC
,
4872 .help
= "resume target",
4873 .usage
= "[<address>]",
4877 .handler
= &handle_reset_command
,
4878 .mode
= COMMAND_EXEC
,
4879 .usage
= "[run|halt|init]",
4880 .help
= "Reset all targets into the specified mode."
4881 "Default reset mode is run, if not given.",
4884 .name
= "soft_reset_halt",
4885 .handler
= &handle_soft_reset_halt_command
,
4886 .mode
= COMMAND_EXEC
,
4887 .help
= "halt the target and do a soft reset",
4892 .handler
= &handle_step_command
,
4893 .mode
= COMMAND_EXEC
,
4894 .help
= "step one instruction from current PC or [addr]",
4895 .usage
= "[<address>]",
4900 .handler
= &handle_md_command
,
4901 .mode
= COMMAND_EXEC
,
4902 .help
= "display memory words",
4903 .usage
= "[phys] <addr> [count]",
4907 .handler
= &handle_md_command
,
4908 .mode
= COMMAND_EXEC
,
4909 .help
= "display memory half-words",
4910 .usage
= "[phys] <addr> [count]",
4914 .handler
= &handle_md_command
,
4915 .mode
= COMMAND_EXEC
,
4916 .help
= "display memory bytes",
4917 .usage
= "[phys] <addr> [count]",
4922 .handler
= &handle_mw_command
,
4923 .mode
= COMMAND_EXEC
,
4924 .help
= "write memory word",
4925 .usage
= "[phys] <addr> <value> [count]",
4929 .handler
= &handle_mw_command
,
4930 .mode
= COMMAND_EXEC
,
4931 .help
= "write memory half-word",
4932 .usage
= "[phys] <addr> <value> [count]",
4936 .handler
= &handle_mw_command
,
4937 .mode
= COMMAND_EXEC
,
4938 .help
= "write memory byte",
4939 .usage
= "[phys] <addr> <value> [count]",
4944 .handler
= &handle_bp_command
,
4945 .mode
= COMMAND_EXEC
,
4946 .help
= "list or set breakpoint",
4947 .usage
= "[<address> <length> [hw]]",
4951 .handler
= &handle_rbp_command
,
4952 .mode
= COMMAND_EXEC
,
4953 .help
= "remove breakpoint",
4954 .usage
= "<address>",
4959 .handler
= &handle_wp_command
,
4960 .mode
= COMMAND_EXEC
,
4961 .help
= "list or set watchpoint",
4962 .usage
= "[<address> <length> <r/w/a> [value] [mask]]",
4966 .handler
= &handle_rwp_command
,
4967 .mode
= COMMAND_EXEC
,
4968 .help
= "remove watchpoint",
4969 .usage
= "<address>",
4973 .name
= "load_image",
4974 .handler
= &handle_load_image_command
,
4975 .mode
= COMMAND_EXEC
,
4976 .usage
= "<file> <address> ['bin'|'ihex'|'elf'|'s19'] "
4977 "[min_address] [max_length]",
4980 .name
= "dump_image",
4981 .handler
= &handle_dump_image_command
,
4982 .mode
= COMMAND_EXEC
,
4983 .usage
= "<file> <address> <size>",
4986 .name
= "verify_image",
4987 .handler
= &handle_verify_image_command
,
4988 .mode
= COMMAND_EXEC
,
4989 .usage
= "<file> [offset] [type]",
4992 .name
= "test_image",
4993 .handler
= &handle_test_image_command
,
4994 .mode
= COMMAND_EXEC
,
4995 .usage
= "<file> [offset] [type]",
4998 .name
= "ocd_mem2array",
4999 .mode
= COMMAND_EXEC
,
5000 .jim_handler
= &jim_mem2array
,
5001 .help
= "read memory and return as a TCL array "
5002 "for script processing",
5003 .usage
= "<arrayname> <width=32|16|8> <address> <count>",
5006 .name
= "ocd_array2mem",
5007 .mode
= COMMAND_EXEC
,
5008 .jim_handler
= &jim_array2mem
,
5009 .help
= "convert a TCL array to memory locations "
5010 "and write the values",
5011 .usage
= "<arrayname> <width=32|16|8> <address> <count>",
5013 COMMAND_REGISTRATION_DONE
5015 int target_register_user_commands(struct command_context
*cmd_ctx
)
5017 int retval
= ERROR_OK
;
5018 if ((retval
= target_request_register_commands(cmd_ctx
)) != ERROR_OK
)
5021 if ((retval
= trace_register_commands(cmd_ctx
)) != ERROR_OK
)
5025 return register_commands(cmd_ctx
, NULL
, target_exec_command_handlers
);