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 <helper/time_support.h>
44 #include <jtag/jtag.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 static 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 static 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 static 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 static 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 static 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 static 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 },
217 const char *debug_reason_name(struct target
*t
)
221 cp
= Jim_Nvp_value2name_simple(nvp_target_debug_reason
,
222 t
->debug_reason
)->name
;
224 LOG_ERROR("Invalid debug reason: %d", (int)(t
->debug_reason
));
225 cp
= "(*BUG*unknown*BUG*)";
231 target_state_name( struct target
*t
)
234 cp
= Jim_Nvp_value2name_simple(nvp_target_state
, t
->state
)->name
;
236 LOG_ERROR("Invalid target state: %d", (int)(t
->state
));
237 cp
= "(*BUG*unknown*BUG*)";
242 /* determine the number of the new target */
243 static int new_target_number(void)
248 /* number is 0 based */
252 if (x
< t
->target_number
) {
253 x
= t
->target_number
;
260 /* read a uint32_t from a buffer in target memory endianness */
261 uint32_t target_buffer_get_u32(struct target
*target
, const uint8_t *buffer
)
263 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
264 return le_to_h_u32(buffer
);
266 return be_to_h_u32(buffer
);
269 /* read a uint16_t from a buffer in target memory endianness */
270 uint16_t target_buffer_get_u16(struct target
*target
, const uint8_t *buffer
)
272 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
273 return le_to_h_u16(buffer
);
275 return be_to_h_u16(buffer
);
278 /* read a uint8_t from a buffer in target memory endianness */
279 uint8_t target_buffer_get_u8(struct target
*target
, const uint8_t *buffer
)
281 return *buffer
& 0x0ff;
284 /* write a uint32_t to a buffer in target memory endianness */
285 void target_buffer_set_u32(struct target
*target
, uint8_t *buffer
, uint32_t value
)
287 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
288 h_u32_to_le(buffer
, value
);
290 h_u32_to_be(buffer
, value
);
293 /* write a uint16_t to a buffer in target memory endianness */
294 void target_buffer_set_u16(struct target
*target
, uint8_t *buffer
, uint16_t value
)
296 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
297 h_u16_to_le(buffer
, value
);
299 h_u16_to_be(buffer
, value
);
302 /* write a uint8_t to a buffer in target memory endianness */
303 void target_buffer_set_u8(struct target
*target
, uint8_t *buffer
, uint8_t value
)
308 /* return a pointer to a configured target; id is name or number */
309 struct target
*get_target(const char *id
)
311 struct target
*target
;
313 /* try as tcltarget name */
314 for (target
= all_targets
; target
; target
= target
->next
) {
315 if (target
->cmd_name
== NULL
)
317 if (strcmp(id
, target
->cmd_name
) == 0)
321 /* It's OK to remove this fallback sometime after August 2010 or so */
323 /* no match, try as number */
325 if (parse_uint(id
, &num
) != ERROR_OK
)
328 for (target
= all_targets
; target
; target
= target
->next
) {
329 if (target
->target_number
== (int)num
) {
330 LOG_WARNING("use '%s' as target identifier, not '%u'",
331 target
->cmd_name
, num
);
339 /* returns a pointer to the n-th configured target */
340 static struct target
*get_target_by_num(int num
)
342 struct target
*target
= all_targets
;
345 if (target
->target_number
== num
) {
348 target
= target
->next
;
354 struct target
* get_current_target(struct command_context
*cmd_ctx
)
356 struct target
*target
= get_target_by_num(cmd_ctx
->current_target
);
360 LOG_ERROR("BUG: current_target out of bounds");
367 int target_poll(struct target
*target
)
371 /* We can't poll until after examine */
372 if (!target_was_examined(target
))
374 /* Fail silently lest we pollute the log */
378 retval
= target
->type
->poll(target
);
379 if (retval
!= ERROR_OK
)
382 if (target
->halt_issued
)
384 if (target
->state
== TARGET_HALTED
)
386 target
->halt_issued
= false;
389 long long t
= timeval_ms() - target
->halt_issued_time
;
392 target
->halt_issued
= false;
393 LOG_INFO("Halt timed out, wake up GDB.");
394 target_call_event_callbacks(target
, TARGET_EVENT_GDB_HALT
);
402 int target_halt(struct target
*target
)
405 /* We can't poll until after examine */
406 if (!target_was_examined(target
))
408 LOG_ERROR("Target not examined yet");
412 retval
= target
->type
->halt(target
);
413 if (retval
!= ERROR_OK
)
416 target
->halt_issued
= true;
417 target
->halt_issued_time
= timeval_ms();
422 int target_resume(struct target
*target
, int current
, uint32_t address
, int handle_breakpoints
, int debug_execution
)
426 /* We can't poll until after examine */
427 if (!target_was_examined(target
))
429 LOG_ERROR("Target not examined yet");
433 /* note that resume *must* be asynchronous. The CPU can halt before we poll. The CPU can
434 * even halt at the current PC as a result of a software breakpoint being inserted by (a bug?)
437 if ((retval
= target
->type
->resume(target
, current
, address
, handle_breakpoints
, debug_execution
)) != ERROR_OK
)
443 int target_process_reset(struct command_context
*cmd_ctx
, enum target_reset_mode reset_mode
)
448 n
= Jim_Nvp_value2name_simple(nvp_reset_modes
, reset_mode
);
449 if (n
->name
== NULL
) {
450 LOG_ERROR("invalid reset mode");
454 /* disable polling during reset to make reset event scripts
455 * more predictable, i.e. dr/irscan & pathmove in events will
456 * not have JTAG operations injected into the middle of a sequence.
458 bool save_poll
= jtag_poll_get_enabled();
460 jtag_poll_set_enabled(false);
462 sprintf(buf
, "ocd_process_reset %s", n
->name
);
463 retval
= Jim_Eval(cmd_ctx
->interp
, buf
);
465 jtag_poll_set_enabled(save_poll
);
467 if (retval
!= JIM_OK
) {
468 Jim_PrintErrorMessage(cmd_ctx
->interp
);
472 /* We want any events to be processed before the prompt */
473 retval
= target_call_timer_callbacks_now();
478 static int identity_virt2phys(struct target
*target
,
479 uint32_t virtual, uint32_t *physical
)
485 static int no_mmu(struct target
*target
, int *enabled
)
491 static int default_examine(struct target
*target
)
493 target_set_examined(target
);
497 int target_examine_one(struct target
*target
)
499 return target
->type
->examine(target
);
502 static int jtag_enable_callback(enum jtag_event event
, void *priv
)
504 struct target
*target
= priv
;
506 if (event
!= JTAG_TAP_EVENT_ENABLE
|| !target
->tap
->enabled
)
509 jtag_unregister_event_callback(jtag_enable_callback
, target
);
510 return target_examine_one(target
);
514 /* Targets that correctly implement init + examine, i.e.
515 * no communication with target during init:
519 int target_examine(void)
521 int retval
= ERROR_OK
;
522 struct target
*target
;
524 for (target
= all_targets
; target
; target
= target
->next
)
526 /* defer examination, but don't skip it */
527 if (!target
->tap
->enabled
) {
528 jtag_register_event_callback(jtag_enable_callback
,
532 if ((retval
= target_examine_one(target
)) != ERROR_OK
)
537 const char *target_type_name(struct target
*target
)
539 return target
->type
->name
;
542 static int target_write_memory_imp(struct target
*target
, uint32_t address
, uint32_t size
, uint32_t count
, uint8_t *buffer
)
544 if (!target_was_examined(target
))
546 LOG_ERROR("Target not examined yet");
549 return target
->type
->write_memory_imp(target
, address
, size
, count
, buffer
);
552 static int target_read_memory_imp(struct target
*target
, uint32_t address
, uint32_t size
, uint32_t count
, uint8_t *buffer
)
554 if (!target_was_examined(target
))
556 LOG_ERROR("Target not examined yet");
559 return target
->type
->read_memory_imp(target
, address
, size
, count
, buffer
);
562 static int target_soft_reset_halt_imp(struct target
*target
)
564 if (!target_was_examined(target
))
566 LOG_ERROR("Target not examined yet");
569 if (!target
->type
->soft_reset_halt_imp
) {
570 LOG_ERROR("Target %s does not support soft_reset_halt",
571 target_name(target
));
574 return target
->type
->soft_reset_halt_imp(target
);
577 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
)
579 if (!target_was_examined(target
))
581 LOG_ERROR("Target not examined yet");
584 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
);
587 int target_read_memory(struct target
*target
,
588 uint32_t address
, uint32_t size
, uint32_t count
, uint8_t *buffer
)
590 return target
->type
->read_memory(target
, address
, size
, count
, buffer
);
593 int target_read_phys_memory(struct target
*target
,
594 uint32_t address
, uint32_t size
, uint32_t count
, uint8_t *buffer
)
596 return target
->type
->read_phys_memory(target
, address
, size
, count
, buffer
);
599 int target_write_memory(struct target
*target
,
600 uint32_t address
, uint32_t size
, uint32_t count
, uint8_t *buffer
)
602 return target
->type
->write_memory(target
, address
, size
, count
, buffer
);
605 int target_write_phys_memory(struct target
*target
,
606 uint32_t address
, uint32_t size
, uint32_t count
, uint8_t *buffer
)
608 return target
->type
->write_phys_memory(target
, address
, size
, count
, buffer
);
611 int target_bulk_write_memory(struct target
*target
,
612 uint32_t address
, uint32_t count
, uint8_t *buffer
)
614 return target
->type
->bulk_write_memory(target
, address
, count
, buffer
);
617 int target_add_breakpoint(struct target
*target
,
618 struct breakpoint
*breakpoint
)
620 if (target
->state
!= TARGET_HALTED
) {
621 LOG_WARNING("target %s is not halted", target
->cmd_name
);
622 return ERROR_TARGET_NOT_HALTED
;
624 return target
->type
->add_breakpoint(target
, breakpoint
);
626 int target_remove_breakpoint(struct target
*target
,
627 struct breakpoint
*breakpoint
)
629 return target
->type
->remove_breakpoint(target
, breakpoint
);
632 int target_add_watchpoint(struct target
*target
,
633 struct watchpoint
*watchpoint
)
635 if (target
->state
!= TARGET_HALTED
) {
636 LOG_WARNING("target %s is not halted", target
->cmd_name
);
637 return ERROR_TARGET_NOT_HALTED
;
639 return target
->type
->add_watchpoint(target
, watchpoint
);
641 int target_remove_watchpoint(struct target
*target
,
642 struct watchpoint
*watchpoint
)
644 return target
->type
->remove_watchpoint(target
, watchpoint
);
647 int target_get_gdb_reg_list(struct target
*target
,
648 struct reg
**reg_list
[], int *reg_list_size
)
650 return target
->type
->get_gdb_reg_list(target
, reg_list
, reg_list_size
);
652 int target_step(struct target
*target
,
653 int current
, uint32_t address
, int handle_breakpoints
)
655 return target
->type
->step(target
, current
, address
, handle_breakpoints
);
659 int target_run_algorithm(struct target
*target
,
660 int num_mem_params
, struct mem_param
*mem_params
,
661 int num_reg_params
, struct reg_param
*reg_param
,
662 uint32_t entry_point
, uint32_t exit_point
,
663 int timeout_ms
, void *arch_info
)
665 return target
->type
->run_algorithm(target
,
666 num_mem_params
, mem_params
, num_reg_params
, reg_param
,
667 entry_point
, exit_point
, timeout_ms
, arch_info
);
671 * Reset the @c examined flag for the given target.
672 * Pure paranoia -- targets are zeroed on allocation.
674 static void target_reset_examined(struct target
*target
)
676 target
->examined
= false;
680 err_read_phys_memory(struct target
*target
, uint32_t address
,
681 uint32_t size
, uint32_t count
, uint8_t *buffer
)
683 LOG_ERROR("Not implemented: %s", __func__
);
688 err_write_phys_memory(struct target
*target
, uint32_t address
,
689 uint32_t size
, uint32_t count
, uint8_t *buffer
)
691 LOG_ERROR("Not implemented: %s", __func__
);
695 static int handle_target(void *priv
);
697 static int target_init_one(struct command_context
*cmd_ctx
,
698 struct target
*target
)
700 target_reset_examined(target
);
702 struct target_type
*type
= target
->type
;
703 if (type
->examine
== NULL
)
704 type
->examine
= default_examine
;
706 int retval
= type
->init_target(cmd_ctx
, target
);
707 if (ERROR_OK
!= retval
)
709 LOG_ERROR("target '%s' init failed", target_name(target
));
714 * @todo get rid of those *memory_imp() methods, now that all
715 * callers are using target_*_memory() accessors ... and make
716 * sure the "physical" paths handle the same issues.
718 /* a non-invasive way(in terms of patches) to add some code that
719 * runs before the type->write/read_memory implementation
721 type
->write_memory_imp
= target
->type
->write_memory
;
722 type
->write_memory
= target_write_memory_imp
;
724 type
->read_memory_imp
= target
->type
->read_memory
;
725 type
->read_memory
= target_read_memory_imp
;
727 type
->soft_reset_halt_imp
= target
->type
->soft_reset_halt
;
728 type
->soft_reset_halt
= target_soft_reset_halt_imp
;
730 type
->run_algorithm_imp
= target
->type
->run_algorithm
;
731 type
->run_algorithm
= target_run_algorithm_imp
;
733 /* Sanity-check MMU support ... stub in what we must, to help
734 * implement it in stages, but warn if we need to do so.
738 if (type
->write_phys_memory
== NULL
)
740 LOG_ERROR("type '%s' is missing write_phys_memory",
742 type
->write_phys_memory
= err_write_phys_memory
;
744 if (type
->read_phys_memory
== NULL
)
746 LOG_ERROR("type '%s' is missing read_phys_memory",
748 type
->read_phys_memory
= err_read_phys_memory
;
750 if (type
->virt2phys
== NULL
)
752 LOG_ERROR("type '%s' is missing virt2phys", type
->name
);
753 type
->virt2phys
= identity_virt2phys
;
758 /* Make sure no-MMU targets all behave the same: make no
759 * distinction between physical and virtual addresses, and
760 * ensure that virt2phys() is always an identity mapping.
762 if (type
->write_phys_memory
|| type
->read_phys_memory
765 LOG_WARNING("type '%s' has bad MMU hooks", type
->name
);
769 type
->write_phys_memory
= type
->write_memory
;
770 type
->read_phys_memory
= type
->read_memory
;
771 type
->virt2phys
= identity_virt2phys
;
776 int target_init(struct command_context
*cmd_ctx
)
778 struct target
*target
;
781 for (target
= all_targets
; target
; target
= target
->next
)
783 retval
= target_init_one(cmd_ctx
, target
);
784 if (ERROR_OK
!= retval
)
791 retval
= target_register_user_commands(cmd_ctx
);
792 if (ERROR_OK
!= retval
)
795 retval
= target_register_timer_callback(&handle_target
,
796 100, 1, cmd_ctx
->interp
);
797 if (ERROR_OK
!= retval
)
803 COMMAND_HANDLER(handle_target_init_command
)
806 return ERROR_COMMAND_SYNTAX_ERROR
;
808 static bool target_initialized
= false;
809 if (target_initialized
)
811 LOG_INFO("'target init' has already been called");
814 target_initialized
= true;
816 LOG_DEBUG("Initializing targets...");
817 return target_init(CMD_CTX
);
820 int target_register_event_callback(int (*callback
)(struct target
*target
, enum target_event event
, void *priv
), void *priv
)
822 struct target_event_callback
**callbacks_p
= &target_event_callbacks
;
824 if (callback
== NULL
)
826 return ERROR_INVALID_ARGUMENTS
;
831 while ((*callbacks_p
)->next
)
832 callbacks_p
= &((*callbacks_p
)->next
);
833 callbacks_p
= &((*callbacks_p
)->next
);
836 (*callbacks_p
) = malloc(sizeof(struct target_event_callback
));
837 (*callbacks_p
)->callback
= callback
;
838 (*callbacks_p
)->priv
= priv
;
839 (*callbacks_p
)->next
= NULL
;
844 int target_register_timer_callback(int (*callback
)(void *priv
), int time_ms
, int periodic
, void *priv
)
846 struct target_timer_callback
**callbacks_p
= &target_timer_callbacks
;
849 if (callback
== NULL
)
851 return ERROR_INVALID_ARGUMENTS
;
856 while ((*callbacks_p
)->next
)
857 callbacks_p
= &((*callbacks_p
)->next
);
858 callbacks_p
= &((*callbacks_p
)->next
);
861 (*callbacks_p
) = malloc(sizeof(struct target_timer_callback
));
862 (*callbacks_p
)->callback
= callback
;
863 (*callbacks_p
)->periodic
= periodic
;
864 (*callbacks_p
)->time_ms
= time_ms
;
866 gettimeofday(&now
, NULL
);
867 (*callbacks_p
)->when
.tv_usec
= now
.tv_usec
+ (time_ms
% 1000) * 1000;
868 time_ms
-= (time_ms
% 1000);
869 (*callbacks_p
)->when
.tv_sec
= now
.tv_sec
+ (time_ms
/ 1000);
870 if ((*callbacks_p
)->when
.tv_usec
> 1000000)
872 (*callbacks_p
)->when
.tv_usec
= (*callbacks_p
)->when
.tv_usec
- 1000000;
873 (*callbacks_p
)->when
.tv_sec
+= 1;
876 (*callbacks_p
)->priv
= priv
;
877 (*callbacks_p
)->next
= NULL
;
882 int target_unregister_event_callback(int (*callback
)(struct target
*target
, enum target_event event
, void *priv
), void *priv
)
884 struct target_event_callback
**p
= &target_event_callbacks
;
885 struct target_event_callback
*c
= target_event_callbacks
;
887 if (callback
== NULL
)
889 return ERROR_INVALID_ARGUMENTS
;
894 struct target_event_callback
*next
= c
->next
;
895 if ((c
->callback
== callback
) && (c
->priv
== priv
))
909 int target_unregister_timer_callback(int (*callback
)(void *priv
), void *priv
)
911 struct target_timer_callback
**p
= &target_timer_callbacks
;
912 struct target_timer_callback
*c
= target_timer_callbacks
;
914 if (callback
== NULL
)
916 return ERROR_INVALID_ARGUMENTS
;
921 struct target_timer_callback
*next
= c
->next
;
922 if ((c
->callback
== callback
) && (c
->priv
== priv
))
936 int target_call_event_callbacks(struct target
*target
, enum target_event event
)
938 struct target_event_callback
*callback
= target_event_callbacks
;
939 struct target_event_callback
*next_callback
;
941 if (event
== TARGET_EVENT_HALTED
)
943 /* execute early halted first */
944 target_call_event_callbacks(target
, TARGET_EVENT_GDB_HALT
);
947 LOG_DEBUG("target event %i (%s)",
949 Jim_Nvp_value2name_simple(nvp_target_event
, event
)->name
);
951 target_handle_event(target
, event
);
955 next_callback
= callback
->next
;
956 callback
->callback(target
, event
, callback
->priv
);
957 callback
= next_callback
;
963 static int target_timer_callback_periodic_restart(
964 struct target_timer_callback
*cb
, struct timeval
*now
)
966 int time_ms
= cb
->time_ms
;
967 cb
->when
.tv_usec
= now
->tv_usec
+ (time_ms
% 1000) * 1000;
968 time_ms
-= (time_ms
% 1000);
969 cb
->when
.tv_sec
= now
->tv_sec
+ time_ms
/ 1000;
970 if (cb
->when
.tv_usec
> 1000000)
972 cb
->when
.tv_usec
= cb
->when
.tv_usec
- 1000000;
973 cb
->when
.tv_sec
+= 1;
978 static int target_call_timer_callback(struct target_timer_callback
*cb
,
981 cb
->callback(cb
->priv
);
984 return target_timer_callback_periodic_restart(cb
, now
);
986 return target_unregister_timer_callback(cb
->callback
, cb
->priv
);
989 static int target_call_timer_callbacks_check_time(int checktime
)
994 gettimeofday(&now
, NULL
);
996 struct target_timer_callback
*callback
= target_timer_callbacks
;
999 // cleaning up may unregister and free this callback
1000 struct target_timer_callback
*next_callback
= callback
->next
;
1002 bool call_it
= callback
->callback
&&
1003 ((!checktime
&& callback
->periodic
) ||
1004 now
.tv_sec
> callback
->when
.tv_sec
||
1005 (now
.tv_sec
== callback
->when
.tv_sec
&&
1006 now
.tv_usec
>= callback
->when
.tv_usec
));
1010 int retval
= target_call_timer_callback(callback
, &now
);
1011 if (retval
!= ERROR_OK
)
1015 callback
= next_callback
;
1021 int target_call_timer_callbacks(void)
1023 return target_call_timer_callbacks_check_time(1);
1026 /* invoke periodic callbacks immediately */
1027 int target_call_timer_callbacks_now(void)
1029 return target_call_timer_callbacks_check_time(0);
1032 int target_alloc_working_area(struct target
*target
, uint32_t size
, struct working_area
**area
)
1034 struct working_area
*c
= target
->working_areas
;
1035 struct working_area
*new_wa
= NULL
;
1037 /* Reevaluate working area address based on MMU state*/
1038 if (target
->working_areas
== NULL
)
1043 retval
= target
->type
->mmu(target
, &enabled
);
1044 if (retval
!= ERROR_OK
)
1050 if (target
->working_area_phys_spec
) {
1051 LOG_DEBUG("MMU disabled, using physical "
1052 "address for working memory 0x%08x",
1053 (unsigned)target
->working_area_phys
);
1054 target
->working_area
= target
->working_area_phys
;
1056 LOG_ERROR("No working memory available. "
1057 "Specify -work-area-phys to target.");
1058 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
1061 if (target
->working_area_virt_spec
) {
1062 LOG_DEBUG("MMU enabled, using virtual "
1063 "address for working memory 0x%08x",
1064 (unsigned)target
->working_area_virt
);
1065 target
->working_area
= target
->working_area_virt
;
1067 LOG_ERROR("No working memory available. "
1068 "Specify -work-area-virt to target.");
1069 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
1074 /* only allocate multiples of 4 byte */
1077 LOG_ERROR("BUG: code tried to allocate unaligned number of bytes (0x%08x), padding", ((unsigned)(size
)));
1078 size
= (size
+ 3) & (~3);
1081 /* see if there's already a matching working area */
1084 if ((c
->free
) && (c
->size
== size
))
1092 /* if not, allocate a new one */
1095 struct working_area
**p
= &target
->working_areas
;
1096 uint32_t first_free
= target
->working_area
;
1097 uint32_t free_size
= target
->working_area_size
;
1099 c
= target
->working_areas
;
1102 first_free
+= c
->size
;
1103 free_size
-= c
->size
;
1108 if (free_size
< size
)
1110 LOG_WARNING("not enough working area available(requested %u, free %u)",
1111 (unsigned)(size
), (unsigned)(free_size
));
1112 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
1115 LOG_DEBUG("allocated new working area at address 0x%08x", (unsigned)first_free
);
1117 new_wa
= malloc(sizeof(struct working_area
));
1118 new_wa
->next
= NULL
;
1119 new_wa
->size
= size
;
1120 new_wa
->address
= first_free
;
1122 if (target
->backup_working_area
)
1125 new_wa
->backup
= malloc(new_wa
->size
);
1126 if ((retval
= target_read_memory(target
, new_wa
->address
, 4, new_wa
->size
/ 4, new_wa
->backup
)) != ERROR_OK
)
1128 free(new_wa
->backup
);
1135 new_wa
->backup
= NULL
;
1138 /* put new entry in list */
1142 /* mark as used, and return the new (reused) area */
1147 new_wa
->user
= area
;
1152 int target_free_working_area_restore(struct target
*target
, struct working_area
*area
, int restore
)
1157 if (restore
&& target
->backup_working_area
)
1160 if ((retval
= target_write_memory(target
, area
->address
, 4, area
->size
/ 4, area
->backup
)) != ERROR_OK
)
1166 /* mark user pointer invalid */
1173 int target_free_working_area(struct target
*target
, struct working_area
*area
)
1175 return target_free_working_area_restore(target
, area
, 1);
1178 /* free resources and restore memory, if restoring memory fails,
1179 * free up resources anyway
1181 void target_free_all_working_areas_restore(struct target
*target
, int restore
)
1183 struct working_area
*c
= target
->working_areas
;
1187 struct working_area
*next
= c
->next
;
1188 target_free_working_area_restore(target
, c
, restore
);
1198 target
->working_areas
= NULL
;
1201 void target_free_all_working_areas(struct target
*target
)
1203 target_free_all_working_areas_restore(target
, 1);
1206 int target_arch_state(struct target
*target
)
1211 LOG_USER("No target has been configured");
1215 LOG_USER("target state: %s", target_state_name( target
));
1217 if (target
->state
!= TARGET_HALTED
)
1220 retval
= target
->type
->arch_state(target
);
1224 /* Single aligned words are guaranteed to use 16 or 32 bit access
1225 * mode respectively, otherwise data is handled as quickly as
1228 int target_write_buffer(struct target
*target
, uint32_t address
, uint32_t size
, uint8_t *buffer
)
1231 LOG_DEBUG("writing buffer of %i byte at 0x%8.8x",
1232 (int)size
, (unsigned)address
);
1234 if (!target_was_examined(target
))
1236 LOG_ERROR("Target not examined yet");
1244 if ((address
+ size
- 1) < address
)
1246 /* GDB can request this when e.g. PC is 0xfffffffc*/
1247 LOG_ERROR("address + size wrapped(0x%08x, 0x%08x)",
1253 if (((address
% 2) == 0) && (size
== 2))
1255 return target_write_memory(target
, address
, 2, 1, buffer
);
1258 /* handle unaligned head bytes */
1261 uint32_t unaligned
= 4 - (address
% 4);
1263 if (unaligned
> size
)
1266 if ((retval
= target_write_memory(target
, address
, 1, unaligned
, buffer
)) != ERROR_OK
)
1269 buffer
+= unaligned
;
1270 address
+= unaligned
;
1274 /* handle aligned words */
1277 int aligned
= size
- (size
% 4);
1279 /* use bulk writes above a certain limit. This may have to be changed */
1282 if ((retval
= target
->type
->bulk_write_memory(target
, address
, aligned
/ 4, buffer
)) != ERROR_OK
)
1287 if ((retval
= target_write_memory(target
, address
, 4, aligned
/ 4, buffer
)) != ERROR_OK
)
1296 /* handle tail writes of less than 4 bytes */
1299 if ((retval
= target_write_memory(target
, address
, 1, size
, buffer
)) != ERROR_OK
)
1306 /* Single aligned words are guaranteed to use 16 or 32 bit access
1307 * mode respectively, otherwise data is handled as quickly as
1310 int target_read_buffer(struct target
*target
, uint32_t address
, uint32_t size
, uint8_t *buffer
)
1313 LOG_DEBUG("reading buffer of %i byte at 0x%8.8x",
1314 (int)size
, (unsigned)address
);
1316 if (!target_was_examined(target
))
1318 LOG_ERROR("Target not examined yet");
1326 if ((address
+ size
- 1) < address
)
1328 /* GDB can request this when e.g. PC is 0xfffffffc*/
1329 LOG_ERROR("address + size wrapped(0x%08" PRIx32
", 0x%08" PRIx32
")",
1335 if (((address
% 2) == 0) && (size
== 2))
1337 return target_read_memory(target
, address
, 2, 1, buffer
);
1340 /* handle unaligned head bytes */
1343 uint32_t unaligned
= 4 - (address
% 4);
1345 if (unaligned
> size
)
1348 if ((retval
= target_read_memory(target
, address
, 1, unaligned
, buffer
)) != ERROR_OK
)
1351 buffer
+= unaligned
;
1352 address
+= unaligned
;
1356 /* handle aligned words */
1359 int aligned
= size
- (size
% 4);
1361 if ((retval
= target_read_memory(target
, address
, 4, aligned
/ 4, buffer
)) != ERROR_OK
)
1369 /*prevent byte access when possible (avoid AHB access limitations in some cases)*/
1372 int aligned
= size
- (size
%2);
1373 retval
= target_read_memory(target
, address
, 2, aligned
/ 2, buffer
);
1374 if (retval
!= ERROR_OK
)
1381 /* handle tail writes of less than 4 bytes */
1384 if ((retval
= target_read_memory(target
, address
, 1, size
, buffer
)) != ERROR_OK
)
1391 int target_checksum_memory(struct target
*target
, uint32_t address
, uint32_t size
, uint32_t* crc
)
1396 uint32_t checksum
= 0;
1397 if (!target_was_examined(target
))
1399 LOG_ERROR("Target not examined yet");
1403 if ((retval
= target
->type
->checksum_memory(target
, address
,
1404 size
, &checksum
)) != ERROR_OK
)
1406 buffer
= malloc(size
);
1409 LOG_ERROR("error allocating buffer for section (%d bytes)", (int)size
);
1410 return ERROR_INVALID_ARGUMENTS
;
1412 retval
= target_read_buffer(target
, address
, size
, buffer
);
1413 if (retval
!= ERROR_OK
)
1419 /* convert to target endianess */
1420 for (i
= 0; i
< (size
/sizeof(uint32_t)); i
++)
1422 uint32_t target_data
;
1423 target_data
= target_buffer_get_u32(target
, &buffer
[i
*sizeof(uint32_t)]);
1424 target_buffer_set_u32(target
, &buffer
[i
*sizeof(uint32_t)], target_data
);
1427 retval
= image_calculate_checksum(buffer
, size
, &checksum
);
1436 int target_blank_check_memory(struct target
*target
, uint32_t address
, uint32_t size
, uint32_t* blank
)
1439 if (!target_was_examined(target
))
1441 LOG_ERROR("Target not examined yet");
1445 if (target
->type
->blank_check_memory
== 0)
1446 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
1448 retval
= target
->type
->blank_check_memory(target
, address
, size
, blank
);
1453 int target_read_u32(struct target
*target
, uint32_t address
, uint32_t *value
)
1455 uint8_t value_buf
[4];
1456 if (!target_was_examined(target
))
1458 LOG_ERROR("Target not examined yet");
1462 int retval
= target_read_memory(target
, address
, 4, 1, value_buf
);
1464 if (retval
== ERROR_OK
)
1466 *value
= target_buffer_get_u32(target
, value_buf
);
1467 LOG_DEBUG("address: 0x%8.8" PRIx32
", value: 0x%8.8" PRIx32
"",
1474 LOG_DEBUG("address: 0x%8.8" PRIx32
" failed",
1481 int target_read_u16(struct target
*target
, uint32_t address
, uint16_t *value
)
1483 uint8_t value_buf
[2];
1484 if (!target_was_examined(target
))
1486 LOG_ERROR("Target not examined yet");
1490 int retval
= target_read_memory(target
, address
, 2, 1, value_buf
);
1492 if (retval
== ERROR_OK
)
1494 *value
= target_buffer_get_u16(target
, value_buf
);
1495 LOG_DEBUG("address: 0x%8.8" PRIx32
", value: 0x%4.4x",
1502 LOG_DEBUG("address: 0x%8.8" PRIx32
" failed",
1509 int target_read_u8(struct target
*target
, uint32_t address
, uint8_t *value
)
1511 int retval
= target_read_memory(target
, address
, 1, 1, value
);
1512 if (!target_was_examined(target
))
1514 LOG_ERROR("Target not examined yet");
1518 if (retval
== ERROR_OK
)
1520 LOG_DEBUG("address: 0x%8.8" PRIx32
", value: 0x%2.2x",
1527 LOG_DEBUG("address: 0x%8.8" PRIx32
" failed",
1534 int target_write_u32(struct target
*target
, uint32_t address
, uint32_t value
)
1537 uint8_t value_buf
[4];
1538 if (!target_was_examined(target
))
1540 LOG_ERROR("Target not examined yet");
1544 LOG_DEBUG("address: 0x%8.8" PRIx32
", value: 0x%8.8" PRIx32
"",
1548 target_buffer_set_u32(target
, value_buf
, value
);
1549 if ((retval
= target_write_memory(target
, address
, 4, 1, value_buf
)) != ERROR_OK
)
1551 LOG_DEBUG("failed: %i", retval
);
1557 int target_write_u16(struct target
*target
, uint32_t address
, uint16_t value
)
1560 uint8_t value_buf
[2];
1561 if (!target_was_examined(target
))
1563 LOG_ERROR("Target not examined yet");
1567 LOG_DEBUG("address: 0x%8.8" PRIx32
", value: 0x%8.8x",
1571 target_buffer_set_u16(target
, value_buf
, value
);
1572 if ((retval
= target_write_memory(target
, address
, 2, 1, value_buf
)) != ERROR_OK
)
1574 LOG_DEBUG("failed: %i", retval
);
1580 int target_write_u8(struct target
*target
, uint32_t address
, uint8_t value
)
1583 if (!target_was_examined(target
))
1585 LOG_ERROR("Target not examined yet");
1589 LOG_DEBUG("address: 0x%8.8" PRIx32
", value: 0x%2.2x",
1592 if ((retval
= target_write_memory(target
, address
, 1, 1, &value
)) != ERROR_OK
)
1594 LOG_DEBUG("failed: %i", retval
);
1600 COMMAND_HANDLER(handle_targets_command
)
1602 struct target
*target
= all_targets
;
1606 target
= get_target(CMD_ARGV
[0]);
1607 if (target
== NULL
) {
1608 command_print(CMD_CTX
,"Target: %s is unknown, try one of:\n", CMD_ARGV
[0]);
1611 if (!target
->tap
->enabled
) {
1612 command_print(CMD_CTX
,"Target: TAP %s is disabled, "
1613 "can't be the current target\n",
1614 target
->tap
->dotted_name
);
1618 CMD_CTX
->current_target
= target
->target_number
;
1623 target
= all_targets
;
1624 command_print(CMD_CTX
, " TargetName Type Endian TapName State ");
1625 command_print(CMD_CTX
, "-- ------------------ ---------- ------ ------------------ ------------");
1631 if (target
->tap
->enabled
)
1632 state
= target_state_name( target
);
1634 state
= "tap-disabled";
1636 if (CMD_CTX
->current_target
== target
->target_number
)
1639 /* keep columns lined up to match the headers above */
1640 command_print(CMD_CTX
, "%2d%c %-18s %-10s %-6s %-18s %s",
1641 target
->target_number
,
1643 target_name(target
),
1644 target_type_name(target
),
1645 Jim_Nvp_value2name_simple(nvp_target_endian
,
1646 target
->endianness
)->name
,
1647 target
->tap
->dotted_name
,
1649 target
= target
->next
;
1655 /* every 300ms we check for reset & powerdropout and issue a "reset halt" if so. */
1657 static int powerDropout
;
1658 static int srstAsserted
;
1660 static int runPowerRestore
;
1661 static int runPowerDropout
;
1662 static int runSrstAsserted
;
1663 static int runSrstDeasserted
;
1665 static int sense_handler(void)
1667 static int prevSrstAsserted
= 0;
1668 static int prevPowerdropout
= 0;
1671 if ((retval
= jtag_power_dropout(&powerDropout
)) != ERROR_OK
)
1675 powerRestored
= prevPowerdropout
&& !powerDropout
;
1678 runPowerRestore
= 1;
1681 long long current
= timeval_ms();
1682 static long long lastPower
= 0;
1683 int waitMore
= lastPower
+ 2000 > current
;
1684 if (powerDropout
&& !waitMore
)
1686 runPowerDropout
= 1;
1687 lastPower
= current
;
1690 if ((retval
= jtag_srst_asserted(&srstAsserted
)) != ERROR_OK
)
1694 srstDeasserted
= prevSrstAsserted
&& !srstAsserted
;
1696 static long long lastSrst
= 0;
1697 waitMore
= lastSrst
+ 2000 > current
;
1698 if (srstDeasserted
&& !waitMore
)
1700 runSrstDeasserted
= 1;
1704 if (!prevSrstAsserted
&& srstAsserted
)
1706 runSrstAsserted
= 1;
1709 prevSrstAsserted
= srstAsserted
;
1710 prevPowerdropout
= powerDropout
;
1712 if (srstDeasserted
|| powerRestored
)
1714 /* Other than logging the event we can't do anything here.
1715 * Issuing a reset is a particularly bad idea as we might
1716 * be inside a reset already.
1723 static void target_call_event_callbacks_all(enum target_event e
) {
1724 struct target
*target
;
1725 target
= all_targets
;
1727 target_call_event_callbacks(target
, e
);
1728 target
= target
->next
;
1732 /* process target state changes */
1733 static int handle_target(void *priv
)
1735 Jim_Interp
*interp
= (Jim_Interp
*)priv
;
1736 int retval
= ERROR_OK
;
1738 /* we do not want to recurse here... */
1739 static int recursive
= 0;
1744 /* danger! running these procedures can trigger srst assertions and power dropouts.
1745 * We need to avoid an infinite loop/recursion here and we do that by
1746 * clearing the flags after running these events.
1748 int did_something
= 0;
1749 if (runSrstAsserted
)
1751 target_call_event_callbacks_all(TARGET_EVENT_GDB_HALT
);
1752 Jim_Eval(interp
, "srst_asserted");
1755 if (runSrstDeasserted
)
1757 Jim_Eval(interp
, "srst_deasserted");
1760 if (runPowerDropout
)
1762 target_call_event_callbacks_all(TARGET_EVENT_GDB_HALT
);
1763 Jim_Eval(interp
, "power_dropout");
1766 if (runPowerRestore
)
1768 Jim_Eval(interp
, "power_restore");
1774 /* clear detect flags */
1778 /* clear action flags */
1780 runSrstAsserted
= 0;
1781 runSrstDeasserted
= 0;
1782 runPowerRestore
= 0;
1783 runPowerDropout
= 0;
1788 /* Poll targets for state changes unless that's globally disabled.
1789 * Skip targets that are currently disabled.
1791 for (struct target
*target
= all_targets
;
1792 is_jtag_poll_safe() && target
;
1793 target
= target
->next
)
1795 if (!target
->tap
->enabled
)
1798 /* only poll target if we've got power and srst isn't asserted */
1799 if (!powerDropout
&& !srstAsserted
)
1801 /* polling may fail silently until the target has been examined */
1802 if ((retval
= target_poll(target
)) != ERROR_OK
)
1804 target_call_event_callbacks(target
, TARGET_EVENT_GDB_HALT
);
1813 COMMAND_HANDLER(handle_reg_command
)
1815 struct target
*target
;
1816 struct reg
*reg
= NULL
;
1822 target
= get_current_target(CMD_CTX
);
1824 /* list all available registers for the current target */
1827 struct reg_cache
*cache
= target
->reg_cache
;
1834 command_print(CMD_CTX
, "===== %s", cache
->name
);
1836 for (i
= 0, reg
= cache
->reg_list
;
1837 i
< cache
->num_regs
;
1838 i
++, reg
++, count
++)
1840 /* only print cached values if they are valid */
1842 value
= buf_to_str(reg
->value
,
1844 command_print(CMD_CTX
,
1845 "(%i) %s (/%" PRIu32
"): 0x%s%s",
1853 command_print(CMD_CTX
, "(%i) %s (/%" PRIu32
")",
1858 cache
= cache
->next
;
1864 /* access a single register by its ordinal number */
1865 if ((CMD_ARGV
[0][0] >= '0') && (CMD_ARGV
[0][0] <= '9'))
1868 COMMAND_PARSE_NUMBER(uint
, CMD_ARGV
[0], num
);
1870 struct reg_cache
*cache
= target
->reg_cache
;
1875 for (i
= 0; i
< cache
->num_regs
; i
++)
1879 reg
= &cache
->reg_list
[i
];
1885 cache
= cache
->next
;
1890 command_print(CMD_CTX
, "%i is out of bounds, the current target has only %i registers (0 - %i)", num
, count
, count
- 1);
1893 } else /* access a single register by its name */
1895 reg
= register_get_by_name(target
->reg_cache
, CMD_ARGV
[0], 1);
1899 command_print(CMD_CTX
, "register %s not found in current target", CMD_ARGV
[0]);
1904 /* display a register */
1905 if ((CMD_ARGC
== 1) || ((CMD_ARGC
== 2) && !((CMD_ARGV
[1][0] >= '0') && (CMD_ARGV
[1][0] <= '9'))))
1907 if ((CMD_ARGC
== 2) && (strcmp(CMD_ARGV
[1], "force") == 0))
1910 if (reg
->valid
== 0)
1912 reg
->type
->get(reg
);
1914 value
= buf_to_str(reg
->value
, reg
->size
, 16);
1915 command_print(CMD_CTX
, "%s (/%i): 0x%s", reg
->name
, (int)(reg
->size
), value
);
1920 /* set register value */
1923 uint8_t *buf
= malloc(DIV_ROUND_UP(reg
->size
, 8));
1924 str_to_buf(CMD_ARGV
[1], strlen(CMD_ARGV
[1]), buf
, reg
->size
, 0);
1926 reg
->type
->set(reg
, buf
);
1928 value
= buf_to_str(reg
->value
, reg
->size
, 16);
1929 command_print(CMD_CTX
, "%s (/%i): 0x%s", reg
->name
, (int)(reg
->size
), value
);
1937 command_print(CMD_CTX
, "usage: reg <#|name> [value]");
1942 COMMAND_HANDLER(handle_poll_command
)
1944 int retval
= ERROR_OK
;
1945 struct target
*target
= get_current_target(CMD_CTX
);
1949 command_print(CMD_CTX
, "background polling: %s",
1950 jtag_poll_get_enabled() ? "on" : "off");
1951 command_print(CMD_CTX
, "TAP: %s (%s)",
1952 target
->tap
->dotted_name
,
1953 target
->tap
->enabled
? "enabled" : "disabled");
1954 if (!target
->tap
->enabled
)
1956 if ((retval
= target_poll(target
)) != ERROR_OK
)
1958 if ((retval
= target_arch_state(target
)) != ERROR_OK
)
1961 else if (CMD_ARGC
== 1)
1964 COMMAND_PARSE_ON_OFF(CMD_ARGV
[0], enable
);
1965 jtag_poll_set_enabled(enable
);
1969 return ERROR_COMMAND_SYNTAX_ERROR
;
1975 COMMAND_HANDLER(handle_wait_halt_command
)
1978 return ERROR_COMMAND_SYNTAX_ERROR
;
1983 int retval
= parse_uint(CMD_ARGV
[0], &ms
);
1984 if (ERROR_OK
!= retval
)
1986 command_print(CMD_CTX
, "usage: %s [seconds]", CMD_NAME
);
1987 return ERROR_COMMAND_SYNTAX_ERROR
;
1989 // convert seconds (given) to milliseconds (needed)
1993 struct target
*target
= get_current_target(CMD_CTX
);
1994 return target_wait_state(target
, TARGET_HALTED
, ms
);
1997 /* wait for target state to change. The trick here is to have a low
1998 * latency for short waits and not to suck up all the CPU time
2001 * After 500ms, keep_alive() is invoked
2003 int target_wait_state(struct target
*target
, enum target_state state
, int ms
)
2006 long long then
= 0, cur
;
2011 if ((retval
= target_poll(target
)) != ERROR_OK
)
2013 if (target
->state
== state
)
2021 then
= timeval_ms();
2022 LOG_DEBUG("waiting for target %s...",
2023 Jim_Nvp_value2name_simple(nvp_target_state
,state
)->name
);
2031 if ((cur
-then
) > ms
)
2033 LOG_ERROR("timed out while waiting for target %s",
2034 Jim_Nvp_value2name_simple(nvp_target_state
,state
)->name
);
2042 COMMAND_HANDLER(handle_halt_command
)
2046 struct target
*target
= get_current_target(CMD_CTX
);
2047 int retval
= target_halt(target
);
2048 if (ERROR_OK
!= retval
)
2054 retval
= parse_uint(CMD_ARGV
[0], &wait
);
2055 if (ERROR_OK
!= retval
)
2056 return ERROR_COMMAND_SYNTAX_ERROR
;
2061 return CALL_COMMAND_HANDLER(handle_wait_halt_command
);
2064 COMMAND_HANDLER(handle_soft_reset_halt_command
)
2066 struct target
*target
= get_current_target(CMD_CTX
);
2068 LOG_USER("requesting target halt and executing a soft reset");
2070 target
->type
->soft_reset_halt(target
);
2075 COMMAND_HANDLER(handle_reset_command
)
2078 return ERROR_COMMAND_SYNTAX_ERROR
;
2080 enum target_reset_mode reset_mode
= RESET_RUN
;
2084 n
= Jim_Nvp_name2value_simple(nvp_reset_modes
, CMD_ARGV
[0]);
2085 if ((n
->name
== NULL
) || (n
->value
== RESET_UNKNOWN
)) {
2086 return ERROR_COMMAND_SYNTAX_ERROR
;
2088 reset_mode
= n
->value
;
2091 /* reset *all* targets */
2092 return target_process_reset(CMD_CTX
, reset_mode
);
2096 COMMAND_HANDLER(handle_resume_command
)
2100 return ERROR_COMMAND_SYNTAX_ERROR
;
2102 struct target
*target
= get_current_target(CMD_CTX
);
2103 target_handle_event(target
, TARGET_EVENT_OLD_pre_resume
);
2105 /* with no CMD_ARGV, resume from current pc, addr = 0,
2106 * with one arguments, addr = CMD_ARGV[0],
2107 * handle breakpoints, not debugging */
2111 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], addr
);
2115 return target_resume(target
, current
, addr
, 1, 0);
2118 COMMAND_HANDLER(handle_step_command
)
2121 return ERROR_COMMAND_SYNTAX_ERROR
;
2125 /* with no CMD_ARGV, step from current pc, addr = 0,
2126 * with one argument addr = CMD_ARGV[0],
2127 * handle breakpoints, debugging */
2132 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], addr
);
2136 struct target
*target
= get_current_target(CMD_CTX
);
2138 return target
->type
->step(target
, current_pc
, addr
, 1);
2141 static void handle_md_output(struct command_context
*cmd_ctx
,
2142 struct target
*target
, uint32_t address
, unsigned size
,
2143 unsigned count
, const uint8_t *buffer
)
2145 const unsigned line_bytecnt
= 32;
2146 unsigned line_modulo
= line_bytecnt
/ size
;
2148 char output
[line_bytecnt
* 4 + 1];
2149 unsigned output_len
= 0;
2151 const char *value_fmt
;
2153 case 4: value_fmt
= "%8.8x "; break;
2154 case 2: value_fmt
= "%4.2x "; break;
2155 case 1: value_fmt
= "%2.2x "; break;
2157 LOG_ERROR("invalid memory read size: %u", size
);
2161 for (unsigned i
= 0; i
< count
; i
++)
2163 if (i
% line_modulo
== 0)
2165 output_len
+= snprintf(output
+ output_len
,
2166 sizeof(output
) - output_len
,
2168 (unsigned)(address
+ (i
*size
)));
2172 const uint8_t *value_ptr
= buffer
+ i
* size
;
2174 case 4: value
= target_buffer_get_u32(target
, value_ptr
); break;
2175 case 2: value
= target_buffer_get_u16(target
, value_ptr
); break;
2176 case 1: value
= *value_ptr
;
2178 output_len
+= snprintf(output
+ output_len
,
2179 sizeof(output
) - output_len
,
2182 if ((i
% line_modulo
== line_modulo
- 1) || (i
== count
- 1))
2184 command_print(cmd_ctx
, "%s", output
);
2190 COMMAND_HANDLER(handle_md_command
)
2193 return ERROR_COMMAND_SYNTAX_ERROR
;
2196 switch (CMD_NAME
[2]) {
2197 case 'w': size
= 4; break;
2198 case 'h': size
= 2; break;
2199 case 'b': size
= 1; break;
2200 default: return ERROR_COMMAND_SYNTAX_ERROR
;
2203 bool physical
=strcmp(CMD_ARGV
[0], "phys")==0;
2204 int (*fn
)(struct target
*target
,
2205 uint32_t address
, uint32_t size
, uint32_t count
, uint8_t *buffer
);
2210 fn
=target_read_phys_memory
;
2213 fn
=target_read_memory
;
2215 if ((CMD_ARGC
< 1) || (CMD_ARGC
> 2))
2217 return ERROR_COMMAND_SYNTAX_ERROR
;
2221 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], address
);
2225 COMMAND_PARSE_NUMBER(uint
, CMD_ARGV
[1], count
);
2227 uint8_t *buffer
= calloc(count
, size
);
2229 struct target
*target
= get_current_target(CMD_CTX
);
2230 int retval
= fn(target
, address
, size
, count
, buffer
);
2231 if (ERROR_OK
== retval
)
2232 handle_md_output(CMD_CTX
, target
, address
, size
, count
, buffer
);
2239 COMMAND_HANDLER(handle_mw_command
)
2243 return ERROR_COMMAND_SYNTAX_ERROR
;
2245 bool physical
=strcmp(CMD_ARGV
[0], "phys")==0;
2246 int (*fn
)(struct target
*target
,
2247 uint32_t address
, uint32_t size
, uint32_t count
, uint8_t *buffer
);
2252 fn
=target_write_phys_memory
;
2255 fn
=target_write_memory
;
2257 if ((CMD_ARGC
< 2) || (CMD_ARGC
> 3))
2258 return ERROR_COMMAND_SYNTAX_ERROR
;
2261 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], address
);
2264 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], value
);
2268 COMMAND_PARSE_NUMBER(uint
, CMD_ARGV
[2], count
);
2270 struct target
*target
= get_current_target(CMD_CTX
);
2272 uint8_t value_buf
[4];
2273 switch (CMD_NAME
[2])
2277 target_buffer_set_u32(target
, value_buf
, value
);
2281 target_buffer_set_u16(target
, value_buf
, value
);
2285 value_buf
[0] = value
;
2288 return ERROR_COMMAND_SYNTAX_ERROR
;
2290 for (unsigned i
= 0; i
< count
; i
++)
2292 int retval
= fn(target
,
2293 address
+ i
* wordsize
, wordsize
, 1, value_buf
);
2294 if (ERROR_OK
!= retval
)
2303 static COMMAND_HELPER(parse_load_image_command_CMD_ARGV
, struct image
*image
,
2304 uint32_t *min_address
, uint32_t *max_address
)
2306 if (CMD_ARGC
< 1 || CMD_ARGC
> 5)
2307 return ERROR_COMMAND_SYNTAX_ERROR
;
2309 /* a base address isn't always necessary,
2310 * default to 0x0 (i.e. don't relocate) */
2314 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], addr
);
2315 image
->base_address
= addr
;
2316 image
->base_address_set
= 1;
2319 image
->base_address_set
= 0;
2321 image
->start_address_set
= 0;
2325 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[3], *min_address
);
2329 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[4], *max_address
);
2330 // use size (given) to find max (required)
2331 *max_address
+= *min_address
;
2334 if (*min_address
> *max_address
)
2335 return ERROR_COMMAND_SYNTAX_ERROR
;
2340 COMMAND_HANDLER(handle_load_image_command
)
2344 uint32_t image_size
;
2345 uint32_t min_address
= 0;
2346 uint32_t max_address
= 0xffffffff;
2350 int retval
= CALL_COMMAND_HANDLER(parse_load_image_command_CMD_ARGV
,
2351 &image
, &min_address
, &max_address
);
2352 if (ERROR_OK
!= retval
)
2355 struct target
*target
= get_current_target(CMD_CTX
);
2357 struct duration bench
;
2358 duration_start(&bench
);
2360 if (image_open(&image
, CMD_ARGV
[0], (CMD_ARGC
>= 3) ? CMD_ARGV
[2] : NULL
) != ERROR_OK
)
2367 for (i
= 0; i
< image
.num_sections
; i
++)
2369 buffer
= malloc(image
.sections
[i
].size
);
2372 command_print(CMD_CTX
,
2373 "error allocating buffer for section (%d bytes)",
2374 (int)(image
.sections
[i
].size
));
2378 if ((retval
= image_read_section(&image
, i
, 0x0, image
.sections
[i
].size
, buffer
, &buf_cnt
)) != ERROR_OK
)
2384 uint32_t offset
= 0;
2385 uint32_t length
= buf_cnt
;
2387 /* DANGER!!! beware of unsigned comparision here!!! */
2389 if ((image
.sections
[i
].base_address
+ buf_cnt
>= min_address
)&&
2390 (image
.sections
[i
].base_address
< max_address
))
2392 if (image
.sections
[i
].base_address
< min_address
)
2394 /* clip addresses below */
2395 offset
+= min_address
-image
.sections
[i
].base_address
;
2399 if (image
.sections
[i
].base_address
+ buf_cnt
> max_address
)
2401 length
-= (image
.sections
[i
].base_address
+ buf_cnt
)-max_address
;
2404 if ((retval
= target_write_buffer(target
, image
.sections
[i
].base_address
+ offset
, length
, buffer
+ offset
)) != ERROR_OK
)
2409 image_size
+= length
;
2410 command_print(CMD_CTX
, "%u bytes written at address 0x%8.8" PRIx32
"",
2411 (unsigned int)length
,
2412 image
.sections
[i
].base_address
+ offset
);
2418 if ((ERROR_OK
== retval
) && (duration_measure(&bench
) == ERROR_OK
))
2420 command_print(CMD_CTX
, "downloaded %" PRIu32
" bytes "
2421 "in %fs (%0.3f kb/s)", image_size
,
2422 duration_elapsed(&bench
), duration_kbps(&bench
, image_size
));
2425 image_close(&image
);
2431 COMMAND_HANDLER(handle_dump_image_command
)
2433 struct fileio fileio
;
2435 uint8_t buffer
[560];
2439 struct target
*target
= get_current_target(CMD_CTX
);
2443 command_print(CMD_CTX
, "usage: dump_image <filename> <address> <size>");
2448 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], address
);
2450 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[2], size
);
2452 if (fileio_open(&fileio
, CMD_ARGV
[0], FILEIO_WRITE
, FILEIO_BINARY
) != ERROR_OK
)
2457 struct duration bench
;
2458 duration_start(&bench
);
2460 int retval
= ERROR_OK
;
2463 size_t size_written
;
2464 uint32_t this_run_size
= (size
> 560) ? 560 : size
;
2465 retval
= target_read_buffer(target
, address
, this_run_size
, buffer
);
2466 if (retval
!= ERROR_OK
)
2471 retval
= fileio_write(&fileio
, this_run_size
, buffer
, &size_written
);
2472 if (retval
!= ERROR_OK
)
2477 size
-= this_run_size
;
2478 address
+= this_run_size
;
2481 if ((retvaltemp
= fileio_close(&fileio
)) != ERROR_OK
)
2484 if ((ERROR_OK
== retval
) && (duration_measure(&bench
) == ERROR_OK
))
2486 command_print(CMD_CTX
,
2487 "dumped %zu bytes in %fs (%0.3f kb/s)", fileio
.size
,
2488 duration_elapsed(&bench
), duration_kbps(&bench
, fileio
.size
));
2494 static COMMAND_HELPER(handle_verify_image_command_internal
, int verify
)
2498 uint32_t image_size
;
2501 uint32_t checksum
= 0;
2502 uint32_t mem_checksum
= 0;
2506 struct target
*target
= get_current_target(CMD_CTX
);
2510 return ERROR_COMMAND_SYNTAX_ERROR
;
2515 LOG_ERROR("no target selected");
2519 struct duration bench
;
2520 duration_start(&bench
);
2525 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], addr
);
2526 image
.base_address
= addr
;
2527 image
.base_address_set
= 1;
2531 image
.base_address_set
= 0;
2532 image
.base_address
= 0x0;
2535 image
.start_address_set
= 0;
2537 if ((retval
= image_open(&image
, CMD_ARGV
[0], (CMD_ARGC
== 3) ? CMD_ARGV
[2] : NULL
)) != ERROR_OK
)
2544 for (i
= 0; i
< image
.num_sections
; i
++)
2546 buffer
= malloc(image
.sections
[i
].size
);
2549 command_print(CMD_CTX
,
2550 "error allocating buffer for section (%d bytes)",
2551 (int)(image
.sections
[i
].size
));
2554 if ((retval
= image_read_section(&image
, i
, 0x0, image
.sections
[i
].size
, buffer
, &buf_cnt
)) != ERROR_OK
)
2562 /* calculate checksum of image */
2563 image_calculate_checksum(buffer
, buf_cnt
, &checksum
);
2565 retval
= target_checksum_memory(target
, image
.sections
[i
].base_address
, buf_cnt
, &mem_checksum
);
2566 if (retval
!= ERROR_OK
)
2572 if (checksum
!= mem_checksum
)
2574 /* failed crc checksum, fall back to a binary compare */
2577 command_print(CMD_CTX
, "checksum mismatch - attempting binary compare");
2579 data
= (uint8_t*)malloc(buf_cnt
);
2581 /* Can we use 32bit word accesses? */
2583 int count
= buf_cnt
;
2584 if ((count
% 4) == 0)
2589 retval
= target_read_memory(target
, image
.sections
[i
].base_address
, size
, count
, data
);
2590 if (retval
== ERROR_OK
)
2593 for (t
= 0; t
< buf_cnt
; t
++)
2595 if (data
[t
] != buffer
[t
])
2597 command_print(CMD_CTX
,
2598 "Verify operation failed address 0x%08x. Was 0x%02x instead of 0x%02x\n",
2599 (unsigned)(t
+ image
.sections
[i
].base_address
),
2604 retval
= ERROR_FAIL
;
2618 command_print(CMD_CTX
, "address 0x%08" PRIx32
" length 0x%08zx",
2619 image
.sections
[i
].base_address
,
2624 image_size
+= buf_cnt
;
2627 if ((ERROR_OK
== retval
) && (duration_measure(&bench
) == ERROR_OK
))
2629 command_print(CMD_CTX
, "verified %" PRIu32
" bytes "
2630 "in %fs (%0.3f kb/s)", image_size
,
2631 duration_elapsed(&bench
), duration_kbps(&bench
, image_size
));
2634 image_close(&image
);
2639 COMMAND_HANDLER(handle_verify_image_command
)
2641 return CALL_COMMAND_HANDLER(handle_verify_image_command_internal
, 1);
2644 COMMAND_HANDLER(handle_test_image_command
)
2646 return CALL_COMMAND_HANDLER(handle_verify_image_command_internal
, 0);
2649 static int handle_bp_command_list(struct command_context
*cmd_ctx
)
2651 struct target
*target
= get_current_target(cmd_ctx
);
2652 struct breakpoint
*breakpoint
= target
->breakpoints
;
2655 if (breakpoint
->type
== BKPT_SOFT
)
2657 char* buf
= buf_to_str(breakpoint
->orig_instr
,
2658 breakpoint
->length
, 16);
2659 command_print(cmd_ctx
, "0x%8.8" PRIx32
", 0x%x, %i, 0x%s",
2660 breakpoint
->address
,
2662 breakpoint
->set
, buf
);
2667 command_print(cmd_ctx
, "0x%8.8" PRIx32
", 0x%x, %i",
2668 breakpoint
->address
,
2669 breakpoint
->length
, breakpoint
->set
);
2672 breakpoint
= breakpoint
->next
;
2677 static int handle_bp_command_set(struct command_context
*cmd_ctx
,
2678 uint32_t addr
, uint32_t length
, int hw
)
2680 struct target
*target
= get_current_target(cmd_ctx
);
2681 int retval
= breakpoint_add(target
, addr
, length
, hw
);
2682 if (ERROR_OK
== retval
)
2683 command_print(cmd_ctx
, "breakpoint set at 0x%8.8" PRIx32
"", addr
);
2685 LOG_ERROR("Failure setting breakpoint");
2689 COMMAND_HANDLER(handle_bp_command
)
2692 return handle_bp_command_list(CMD_CTX
);
2694 if (CMD_ARGC
< 2 || CMD_ARGC
> 3)
2696 command_print(CMD_CTX
, "usage: bp <address> <length> ['hw']");
2697 return ERROR_COMMAND_SYNTAX_ERROR
;
2701 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], addr
);
2703 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], length
);
2708 if (strcmp(CMD_ARGV
[2], "hw") == 0)
2711 return ERROR_COMMAND_SYNTAX_ERROR
;
2714 return handle_bp_command_set(CMD_CTX
, addr
, length
, hw
);
2717 COMMAND_HANDLER(handle_rbp_command
)
2720 return ERROR_COMMAND_SYNTAX_ERROR
;
2723 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], addr
);
2725 struct target
*target
= get_current_target(CMD_CTX
);
2726 breakpoint_remove(target
, addr
);
2731 COMMAND_HANDLER(handle_wp_command
)
2733 struct target
*target
= get_current_target(CMD_CTX
);
2737 struct watchpoint
*watchpoint
= target
->watchpoints
;
2741 command_print(CMD_CTX
, "address: 0x%8.8" PRIx32
2742 ", len: 0x%8.8" PRIx32
2743 ", r/w/a: %i, value: 0x%8.8" PRIx32
2744 ", mask: 0x%8.8" PRIx32
,
2745 watchpoint
->address
,
2747 (int)watchpoint
->rw
,
2750 watchpoint
= watchpoint
->next
;
2755 enum watchpoint_rw type
= WPT_ACCESS
;
2757 uint32_t length
= 0;
2758 uint32_t data_value
= 0x0;
2759 uint32_t data_mask
= 0xffffffff;
2764 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[4], data_mask
);
2767 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[3], data_value
);
2770 switch (CMD_ARGV
[2][0])
2782 LOG_ERROR("invalid watchpoint mode ('%c')", CMD_ARGV
[2][0]);
2783 return ERROR_COMMAND_SYNTAX_ERROR
;
2787 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], length
);
2788 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], addr
);
2792 command_print(CMD_CTX
, "usage: wp [address length "
2793 "[(r|w|a) [value [mask]]]]");
2794 return ERROR_COMMAND_SYNTAX_ERROR
;
2797 int retval
= watchpoint_add(target
, addr
, length
, type
,
2798 data_value
, data_mask
);
2799 if (ERROR_OK
!= retval
)
2800 LOG_ERROR("Failure setting watchpoints");
2805 COMMAND_HANDLER(handle_rwp_command
)
2808 return ERROR_COMMAND_SYNTAX_ERROR
;
2811 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], addr
);
2813 struct target
*target
= get_current_target(CMD_CTX
);
2814 watchpoint_remove(target
, addr
);
2821 * Translate a virtual address to a physical address.
2823 * The low-level target implementation must have logged a detailed error
2824 * which is forwarded to telnet/GDB session.
2826 COMMAND_HANDLER(handle_virt2phys_command
)
2829 return ERROR_COMMAND_SYNTAX_ERROR
;
2832 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], va
);
2835 struct target
*target
= get_current_target(CMD_CTX
);
2836 int retval
= target
->type
->virt2phys(target
, va
, &pa
);
2837 if (retval
== ERROR_OK
)
2838 command_print(CMD_CTX
, "Physical address 0x%08" PRIx32
"", pa
);
2843 static void writeData(FILE *f
, const void *data
, size_t len
)
2845 size_t written
= fwrite(data
, 1, len
, f
);
2847 LOG_ERROR("failed to write %zu bytes: %s", len
, strerror(errno
));
2850 static void writeLong(FILE *f
, int l
)
2853 for (i
= 0; i
< 4; i
++)
2855 char c
= (l
>> (i
*8))&0xff;
2856 writeData(f
, &c
, 1);
2861 static void writeString(FILE *f
, char *s
)
2863 writeData(f
, s
, strlen(s
));
2866 /* Dump a gmon.out histogram file. */
2867 static void writeGmon(uint32_t *samples
, uint32_t sampleNum
, const char *filename
)
2870 FILE *f
= fopen(filename
, "w");
2873 writeString(f
, "gmon");
2874 writeLong(f
, 0x00000001); /* Version */
2875 writeLong(f
, 0); /* padding */
2876 writeLong(f
, 0); /* padding */
2877 writeLong(f
, 0); /* padding */
2879 uint8_t zero
= 0; /* GMON_TAG_TIME_HIST */
2880 writeData(f
, &zero
, 1);
2882 /* figure out bucket size */
2883 uint32_t min
= samples
[0];
2884 uint32_t max
= samples
[0];
2885 for (i
= 0; i
< sampleNum
; i
++)
2887 if (min
> samples
[i
])
2891 if (max
< samples
[i
])
2897 int addressSpace
= (max
-min
+ 1);
2899 static const uint32_t maxBuckets
= 256 * 1024; /* maximum buckets. */
2900 uint32_t length
= addressSpace
;
2901 if (length
> maxBuckets
)
2903 length
= maxBuckets
;
2905 int *buckets
= malloc(sizeof(int)*length
);
2906 if (buckets
== NULL
)
2911 memset(buckets
, 0, sizeof(int)*length
);
2912 for (i
= 0; i
< sampleNum
;i
++)
2914 uint32_t address
= samples
[i
];
2915 long long a
= address
-min
;
2916 long long b
= length
-1;
2917 long long c
= addressSpace
-1;
2918 int index
= (a
*b
)/c
; /* danger!!!! int32 overflows */
2922 /* append binary memory gmon.out &profile_hist_hdr ((char*)&profile_hist_hdr + sizeof(struct gmon_hist_hdr)) */
2923 writeLong(f
, min
); /* low_pc */
2924 writeLong(f
, max
); /* high_pc */
2925 writeLong(f
, length
); /* # of samples */
2926 writeLong(f
, 64000000); /* 64MHz */
2927 writeString(f
, "seconds");
2928 for (i
= 0; i
< (15-strlen("seconds")); i
++)
2929 writeData(f
, &zero
, 1);
2930 writeString(f
, "s");
2932 /*append binary memory gmon.out profile_hist_data (profile_hist_data + profile_hist_hdr.hist_size) */
2934 char *data
= malloc(2*length
);
2937 for (i
= 0; i
< length
;i
++)
2946 data
[i
*2 + 1]=(val
>> 8)&0xff;
2949 writeData(f
, data
, length
* 2);
2959 /* profiling samples the CPU PC as quickly as OpenOCD is able, which will be used as a random sampling of PC */
2960 COMMAND_HANDLER(handle_profile_command
)
2962 struct target
*target
= get_current_target(CMD_CTX
);
2963 struct timeval timeout
, now
;
2965 gettimeofday(&timeout
, NULL
);
2968 return ERROR_COMMAND_SYNTAX_ERROR
;
2971 COMMAND_PARSE_NUMBER(uint
, CMD_ARGV
[0], offset
);
2973 timeval_add_time(&timeout
, offset
, 0);
2975 command_print(CMD_CTX
, "Starting profiling. Halting and resuming the target as often as we can...");
2977 static const int maxSample
= 10000;
2978 uint32_t *samples
= malloc(sizeof(uint32_t)*maxSample
);
2979 if (samples
== NULL
)
2983 /* hopefully it is safe to cache! We want to stop/restart as quickly as possible. */
2984 struct reg
*reg
= register_get_by_name(target
->reg_cache
, "pc", 1);
2989 target_poll(target
);
2990 if (target
->state
== TARGET_HALTED
)
2992 uint32_t t
=*((uint32_t *)reg
->value
);
2993 samples
[numSamples
++]=t
;
2994 retval
= target_resume(target
, 1, 0, 0, 0); /* current pc, addr = 0, do not handle breakpoints, not debugging */
2995 target_poll(target
);
2996 alive_sleep(10); /* sleep 10ms, i.e. <100 samples/second. */
2997 } else if (target
->state
== TARGET_RUNNING
)
2999 /* We want to quickly sample the PC. */
3000 if ((retval
= target_halt(target
)) != ERROR_OK
)
3007 command_print(CMD_CTX
, "Target not halted or running");
3011 if (retval
!= ERROR_OK
)
3016 gettimeofday(&now
, NULL
);
3017 if ((numSamples
>= maxSample
) || ((now
.tv_sec
>= timeout
.tv_sec
) && (now
.tv_usec
>= timeout
.tv_usec
)))
3019 command_print(CMD_CTX
, "Profiling completed. %d samples.", numSamples
);
3020 if ((retval
= target_poll(target
)) != ERROR_OK
)
3025 if (target
->state
== TARGET_HALTED
)
3027 target_resume(target
, 1, 0, 0, 0); /* current pc, addr = 0, do not handle breakpoints, not debugging */
3029 if ((retval
= target_poll(target
)) != ERROR_OK
)
3034 writeGmon(samples
, numSamples
, CMD_ARGV
[1]);
3035 command_print(CMD_CTX
, "Wrote %s", CMD_ARGV
[1]);
3044 static int new_int_array_element(Jim_Interp
* interp
, const char *varname
, int idx
, uint32_t val
)
3047 Jim_Obj
*nameObjPtr
, *valObjPtr
;
3050 namebuf
= alloc_printf("%s(%d)", varname
, idx
);
3054 nameObjPtr
= Jim_NewStringObj(interp
, namebuf
, -1);
3055 valObjPtr
= Jim_NewIntObj(interp
, val
);
3056 if (!nameObjPtr
|| !valObjPtr
)
3062 Jim_IncrRefCount(nameObjPtr
);
3063 Jim_IncrRefCount(valObjPtr
);
3064 result
= Jim_SetVariable(interp
, nameObjPtr
, valObjPtr
);
3065 Jim_DecrRefCount(interp
, nameObjPtr
);
3066 Jim_DecrRefCount(interp
, valObjPtr
);
3068 /* printf("%s(%d) <= 0%08x\n", varname, idx, val); */
3072 static int jim_mem2array(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
3074 struct command_context
*context
;
3075 struct target
*target
;
3077 context
= Jim_GetAssocData(interp
, "context");
3078 if (context
== NULL
)
3080 LOG_ERROR("mem2array: no command context");
3083 target
= get_current_target(context
);
3086 LOG_ERROR("mem2array: no current target");
3090 return target_mem2array(interp
, target
, argc
-1, argv
+ 1);
3093 static int target_mem2array(Jim_Interp
*interp
, struct target
*target
, int argc
, Jim_Obj
*const *argv
)
3101 const char *varname
;
3105 /* argv[1] = name of array to receive the data
3106 * argv[2] = desired width
3107 * argv[3] = memory address
3108 * argv[4] = count of times to read
3111 Jim_WrongNumArgs(interp
, 1, argv
, "varname width addr nelems");
3114 varname
= Jim_GetString(argv
[0], &len
);
3115 /* given "foo" get space for worse case "foo(%d)" .. add 20 */
3117 e
= Jim_GetLong(interp
, argv
[1], &l
);
3123 e
= Jim_GetLong(interp
, argv
[2], &l
);
3128 e
= Jim_GetLong(interp
, argv
[3], &l
);
3144 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3145 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "Invalid width param, must be 8/16/32", NULL
);
3149 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3150 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "mem2array: zero width read?", NULL
);
3153 if ((addr
+ (len
* width
)) < addr
) {
3154 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3155 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "mem2array: addr + len - wraps to zero?", NULL
);
3158 /* absurd transfer size? */
3160 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3161 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "mem2array: absurd > 64K item request", NULL
);
3166 ((width
== 2) && ((addr
& 1) == 0)) ||
3167 ((width
== 4) && ((addr
& 3) == 0))) {
3171 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3172 sprintf(buf
, "mem2array address: 0x%08" PRIx32
" is not aligned for %" PRId32
" byte reads",
3175 Jim_AppendStrings(interp
, Jim_GetResult(interp
), buf
, NULL
);
3184 size_t buffersize
= 4096;
3185 uint8_t *buffer
= malloc(buffersize
);
3192 /* Slurp... in buffer size chunks */
3194 count
= len
; /* in objects.. */
3195 if (count
> (buffersize
/width
)) {
3196 count
= (buffersize
/width
);
3199 retval
= target_read_memory(target
, addr
, width
, count
, buffer
);
3200 if (retval
!= ERROR_OK
) {
3202 LOG_ERROR("mem2array: Read @ 0x%08x, w=%d, cnt=%d, failed",
3206 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3207 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "mem2array: cannot read memory", NULL
);
3211 v
= 0; /* shut up gcc */
3212 for (i
= 0 ;i
< count
;i
++, n
++) {
3215 v
= target_buffer_get_u32(target
, &buffer
[i
*width
]);
3218 v
= target_buffer_get_u16(target
, &buffer
[i
*width
]);
3221 v
= buffer
[i
] & 0x0ff;
3224 new_int_array_element(interp
, varname
, n
, v
);
3232 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3237 static int get_int_array_element(Jim_Interp
* interp
, const char *varname
, int idx
, uint32_t *val
)
3240 Jim_Obj
*nameObjPtr
, *valObjPtr
;
3244 namebuf
= alloc_printf("%s(%d)", varname
, idx
);
3248 nameObjPtr
= Jim_NewStringObj(interp
, namebuf
, -1);
3255 Jim_IncrRefCount(nameObjPtr
);
3256 valObjPtr
= Jim_GetVariable(interp
, nameObjPtr
, JIM_ERRMSG
);
3257 Jim_DecrRefCount(interp
, nameObjPtr
);
3259 if (valObjPtr
== NULL
)
3262 result
= Jim_GetLong(interp
, valObjPtr
, &l
);
3263 /* printf("%s(%d) => 0%08x\n", varname, idx, val); */
3268 static int jim_array2mem(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
3270 struct command_context
*context
;
3271 struct target
*target
;
3273 context
= Jim_GetAssocData(interp
, "context");
3274 if (context
== NULL
) {
3275 LOG_ERROR("array2mem: no command context");
3278 target
= get_current_target(context
);
3279 if (target
== NULL
) {
3280 LOG_ERROR("array2mem: no current target");
3284 return target_array2mem(interp
,target
, argc
-1, argv
+ 1);
3286 static int target_array2mem(Jim_Interp
*interp
, struct target
*target
, int argc
, Jim_Obj
*const *argv
)
3294 const char *varname
;
3298 /* argv[1] = name of array to get the data
3299 * argv[2] = desired width
3300 * argv[3] = memory address
3301 * argv[4] = count to write
3304 Jim_WrongNumArgs(interp
, 0, argv
, "varname width addr nelems");
3307 varname
= Jim_GetString(argv
[0], &len
);
3308 /* given "foo" get space for worse case "foo(%d)" .. add 20 */
3310 e
= Jim_GetLong(interp
, argv
[1], &l
);
3316 e
= Jim_GetLong(interp
, argv
[2], &l
);
3321 e
= Jim_GetLong(interp
, argv
[3], &l
);
3337 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3338 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "Invalid width param, must be 8/16/32", NULL
);
3342 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3343 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "array2mem: zero width read?", NULL
);
3346 if ((addr
+ (len
* width
)) < addr
) {
3347 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3348 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "array2mem: addr + len - wraps to zero?", NULL
);
3351 /* absurd transfer size? */
3353 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3354 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "array2mem: absurd > 64K item request", NULL
);
3359 ((width
== 2) && ((addr
& 1) == 0)) ||
3360 ((width
== 4) && ((addr
& 3) == 0))) {
3364 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3365 sprintf(buf
, "array2mem address: 0x%08x is not aligned for %d byte reads",
3368 Jim_AppendStrings(interp
, Jim_GetResult(interp
), buf
, NULL
);
3379 size_t buffersize
= 4096;
3380 uint8_t *buffer
= malloc(buffersize
);
3385 /* Slurp... in buffer size chunks */
3387 count
= len
; /* in objects.. */
3388 if (count
> (buffersize
/width
)) {
3389 count
= (buffersize
/width
);
3392 v
= 0; /* shut up gcc */
3393 for (i
= 0 ;i
< count
;i
++, n
++) {
3394 get_int_array_element(interp
, varname
, n
, &v
);
3397 target_buffer_set_u32(target
, &buffer
[i
*width
], v
);
3400 target_buffer_set_u16(target
, &buffer
[i
*width
], v
);
3403 buffer
[i
] = v
& 0x0ff;
3409 retval
= target_write_memory(target
, addr
, width
, count
, buffer
);
3410 if (retval
!= ERROR_OK
) {
3412 LOG_ERROR("array2mem: Write @ 0x%08x, w=%d, cnt=%d, failed",
3416 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3417 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "array2mem: cannot read memory", NULL
);
3425 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3430 void target_all_handle_event(enum target_event e
)
3432 struct target
*target
;
3434 LOG_DEBUG("**all*targets: event: %d, %s",
3436 Jim_Nvp_value2name_simple(nvp_target_event
, e
)->name
);
3438 target
= all_targets
;
3440 target_handle_event(target
, e
);
3441 target
= target
->next
;
3446 /* FIX? should we propagate errors here rather than printing them
3449 void target_handle_event(struct target
*target
, enum target_event e
)
3451 struct target_event_action
*teap
;
3453 for (teap
= target
->event_action
; teap
!= NULL
; teap
= teap
->next
) {
3454 if (teap
->event
== e
) {
3455 LOG_DEBUG("target: (%d) %s (%s) event: %d (%s) action: %s",
3456 target
->target_number
,
3457 target_name(target
),
3458 target_type_name(target
),
3460 Jim_Nvp_value2name_simple(nvp_target_event
, e
)->name
,
3461 Jim_GetString(teap
->body
, NULL
));
3462 if (Jim_EvalObj(teap
->interp
, teap
->body
) != JIM_OK
)
3464 Jim_PrintErrorMessage(teap
->interp
);
3471 * Returns true only if the target has a handler for the specified event.
3473 bool target_has_event_action(struct target
*target
, enum target_event event
)
3475 struct target_event_action
*teap
;
3477 for (teap
= target
->event_action
; teap
!= NULL
; teap
= teap
->next
) {
3478 if (teap
->event
== event
)
3484 enum target_cfg_param
{
3487 TCFG_WORK_AREA_VIRT
,
3488 TCFG_WORK_AREA_PHYS
,
3489 TCFG_WORK_AREA_SIZE
,
3490 TCFG_WORK_AREA_BACKUP
,
3493 TCFG_CHAIN_POSITION
,
3496 static Jim_Nvp nvp_config_opts
[] = {
3497 { .name
= "-type", .value
= TCFG_TYPE
},
3498 { .name
= "-event", .value
= TCFG_EVENT
},
3499 { .name
= "-work-area-virt", .value
= TCFG_WORK_AREA_VIRT
},
3500 { .name
= "-work-area-phys", .value
= TCFG_WORK_AREA_PHYS
},
3501 { .name
= "-work-area-size", .value
= TCFG_WORK_AREA_SIZE
},
3502 { .name
= "-work-area-backup", .value
= TCFG_WORK_AREA_BACKUP
},
3503 { .name
= "-endian" , .value
= TCFG_ENDIAN
},
3504 { .name
= "-variant", .value
= TCFG_VARIANT
},
3505 { .name
= "-chain-position", .value
= TCFG_CHAIN_POSITION
},
3507 { .name
= NULL
, .value
= -1 }
3510 static int target_configure(Jim_GetOptInfo
*goi
, struct target
*target
)
3518 /* parse config or cget options ... */
3519 while (goi
->argc
> 0) {
3520 Jim_SetEmptyResult(goi
->interp
);
3521 /* Jim_GetOpt_Debug(goi); */
3523 if (target
->type
->target_jim_configure
) {
3524 /* target defines a configure function */
3525 /* target gets first dibs on parameters */
3526 e
= (*(target
->type
->target_jim_configure
))(target
, goi
);
3535 /* otherwise we 'continue' below */
3537 e
= Jim_GetOpt_Nvp(goi
, nvp_config_opts
, &n
);
3539 Jim_GetOpt_NvpUnknown(goi
, nvp_config_opts
, 0);
3545 if (goi
->isconfigure
) {
3546 Jim_SetResult_sprintf(goi
->interp
,
3547 "not settable: %s", n
->name
);
3551 if (goi
->argc
!= 0) {
3552 Jim_WrongNumArgs(goi
->interp
,
3553 goi
->argc
, goi
->argv
,
3558 Jim_SetResultString(goi
->interp
,
3559 target_type_name(target
), -1);
3563 if (goi
->argc
== 0) {
3564 Jim_WrongNumArgs(goi
->interp
, goi
->argc
, goi
->argv
, "-event ?event-name? ...");
3568 e
= Jim_GetOpt_Nvp(goi
, nvp_target_event
, &n
);
3570 Jim_GetOpt_NvpUnknown(goi
, nvp_target_event
, 1);
3574 if (goi
->isconfigure
) {
3575 if (goi
->argc
!= 1) {
3576 Jim_WrongNumArgs(goi
->interp
, goi
->argc
, goi
->argv
, "-event ?event-name? ?EVENT-BODY?");
3580 if (goi
->argc
!= 0) {
3581 Jim_WrongNumArgs(goi
->interp
, goi
->argc
, goi
->argv
, "-event ?event-name?");
3587 struct target_event_action
*teap
;
3589 teap
= target
->event_action
;
3590 /* replace existing? */
3592 if (teap
->event
== (enum target_event
)n
->value
) {
3598 if (goi
->isconfigure
) {
3599 bool replace
= true;
3602 teap
= calloc(1, sizeof(*teap
));
3605 teap
->event
= n
->value
;
3606 teap
->interp
= goi
->interp
;
3607 Jim_GetOpt_Obj(goi
, &o
);
3609 Jim_DecrRefCount(teap
->interp
, teap
->body
);
3611 teap
->body
= Jim_DuplicateObj(goi
->interp
, o
);
3614 * Tcl/TK - "tk events" have a nice feature.
3615 * See the "BIND" command.
3616 * We should support that here.
3617 * You can specify %X and %Y in the event code.
3618 * The idea is: %T - target name.
3619 * The idea is: %N - target number
3620 * The idea is: %E - event name.
3622 Jim_IncrRefCount(teap
->body
);
3626 /* add to head of event list */
3627 teap
->next
= target
->event_action
;
3628 target
->event_action
= teap
;
3630 Jim_SetEmptyResult(goi
->interp
);
3634 Jim_SetEmptyResult(goi
->interp
);
3636 Jim_SetResult(goi
->interp
, Jim_DuplicateObj(goi
->interp
, teap
->body
));
3643 case TCFG_WORK_AREA_VIRT
:
3644 if (goi
->isconfigure
) {
3645 target_free_all_working_areas(target
);
3646 e
= Jim_GetOpt_Wide(goi
, &w
);
3650 target
->working_area_virt
= w
;
3651 target
->working_area_virt_spec
= true;
3653 if (goi
->argc
!= 0) {
3657 Jim_SetResult(goi
->interp
, Jim_NewIntObj(goi
->interp
, target
->working_area_virt
));
3661 case TCFG_WORK_AREA_PHYS
:
3662 if (goi
->isconfigure
) {
3663 target_free_all_working_areas(target
);
3664 e
= Jim_GetOpt_Wide(goi
, &w
);
3668 target
->working_area_phys
= w
;
3669 target
->working_area_phys_spec
= true;
3671 if (goi
->argc
!= 0) {
3675 Jim_SetResult(goi
->interp
, Jim_NewIntObj(goi
->interp
, target
->working_area_phys
));
3679 case TCFG_WORK_AREA_SIZE
:
3680 if (goi
->isconfigure
) {
3681 target_free_all_working_areas(target
);
3682 e
= Jim_GetOpt_Wide(goi
, &w
);
3686 target
->working_area_size
= w
;
3688 if (goi
->argc
!= 0) {
3692 Jim_SetResult(goi
->interp
, Jim_NewIntObj(goi
->interp
, target
->working_area_size
));
3696 case TCFG_WORK_AREA_BACKUP
:
3697 if (goi
->isconfigure
) {
3698 target_free_all_working_areas(target
);
3699 e
= Jim_GetOpt_Wide(goi
, &w
);
3703 /* make this exactly 1 or 0 */
3704 target
->backup_working_area
= (!!w
);
3706 if (goi
->argc
!= 0) {
3710 Jim_SetResult(goi
->interp
, Jim_NewIntObj(goi
->interp
, target
->backup_working_area
));
3711 /* loop for more e*/
3715 if (goi
->isconfigure
) {
3716 e
= Jim_GetOpt_Nvp(goi
, nvp_target_endian
, &n
);
3718 Jim_GetOpt_NvpUnknown(goi
, nvp_target_endian
, 1);
3721 target
->endianness
= n
->value
;
3723 if (goi
->argc
!= 0) {
3727 n
= Jim_Nvp_value2name_simple(nvp_target_endian
, target
->endianness
);
3728 if (n
->name
== NULL
) {
3729 target
->endianness
= TARGET_LITTLE_ENDIAN
;
3730 n
= Jim_Nvp_value2name_simple(nvp_target_endian
, target
->endianness
);
3732 Jim_SetResultString(goi
->interp
, n
->name
, -1);
3737 if (goi
->isconfigure
) {
3738 if (goi
->argc
< 1) {
3739 Jim_SetResult_sprintf(goi
->interp
,
3744 if (target
->variant
) {
3745 free((void *)(target
->variant
));
3747 e
= Jim_GetOpt_String(goi
, &cp
, NULL
);
3748 target
->variant
= strdup(cp
);
3750 if (goi
->argc
!= 0) {
3754 Jim_SetResultString(goi
->interp
, target
->variant
,-1);
3757 case TCFG_CHAIN_POSITION
:
3758 if (goi
->isconfigure
) {
3760 struct jtag_tap
*tap
;
3761 target_free_all_working_areas(target
);
3762 e
= Jim_GetOpt_Obj(goi
, &o
);
3766 tap
= jtag_tap_by_jim_obj(goi
->interp
, o
);
3770 /* make this exactly 1 or 0 */
3773 if (goi
->argc
!= 0) {
3777 Jim_SetResultString(goi
->interp
, target
->tap
->dotted_name
, -1);
3778 /* loop for more e*/
3781 } /* while (goi->argc) */
3784 /* done - we return */
3788 static int jim_target_configure(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
3791 Jim_GetOpt_Setup(&goi
, interp
, argc
- 1, argv
+ 1);
3792 goi
.isconfigure
= strcmp(Jim_GetString(argv
[0], NULL
), "configure") == 0;
3793 int need_args
= 1 + goi
.isconfigure
;
3794 if (goi
.argc
< need_args
)
3796 Jim_WrongNumArgs(goi
.interp
, goi
.argc
, goi
.argv
,
3798 ? "missing: -option VALUE ..."
3799 : "missing: -option ...");
3802 struct target
*target
= Jim_CmdPrivData(goi
.interp
);
3803 return target_configure(&goi
, target
);
3806 static int jim_target_mw(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
3808 const char *cmd_name
= Jim_GetString(argv
[0], NULL
);
3811 Jim_GetOpt_Setup(&goi
, interp
, argc
- 1, argv
+ 1);
3813 if (goi
.argc
!= 2 && goi
.argc
!= 3)
3815 Jim_SetResult_sprintf(goi
.interp
,
3816 "usage: %s <address> <data> [<count>]", cmd_name
);
3821 int e
= Jim_GetOpt_Wide(&goi
, &a
);
3826 e
= Jim_GetOpt_Wide(&goi
, &b
);
3833 e
= Jim_GetOpt_Wide(&goi
, &c
);
3838 struct target
*target
= Jim_CmdPrivData(goi
.interp
);
3839 uint8_t target_buf
[32];
3840 if (strcasecmp(cmd_name
, "mww") == 0) {
3841 target_buffer_set_u32(target
, target_buf
, b
);
3844 else if (strcasecmp(cmd_name
, "mwh") == 0) {
3845 target_buffer_set_u16(target
, target_buf
, b
);
3848 else if (strcasecmp(cmd_name
, "mwb") == 0) {
3849 target_buffer_set_u8(target
, target_buf
, b
);
3852 LOG_ERROR("command '%s' unknown: ", cmd_name
);
3856 for (jim_wide x
= 0; x
< c
; x
++)
3858 e
= target_write_memory(target
, a
, b
, 1, target_buf
);
3861 Jim_SetResult_sprintf(interp
,
3862 "Error writing @ 0x%08x: %d\n", (int)(a
), e
);
3871 static int jim_target_md(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
3873 const char *cmd_name
= Jim_GetString(argv
[0], NULL
);
3876 Jim_GetOpt_Setup(&goi
, interp
, argc
- 1, argv
+ 1);
3878 if ((goi
.argc
== 2) || (goi
.argc
== 3))
3880 Jim_SetResult_sprintf(goi
.interp
,
3881 "usage: %s <address> [<count>]", cmd_name
);
3886 int e
= Jim_GetOpt_Wide(&goi
, &a
);
3892 e
= Jim_GetOpt_Wide(&goi
, &c
);
3899 jim_wide b
= 1; /* shut up gcc */
3900 if (strcasecmp(cmd_name
, "mdw") == 0)
3902 else if (strcasecmp(cmd_name
, "mdh") == 0)
3904 else if (strcasecmp(cmd_name
, "mdb") == 0)
3907 LOG_ERROR("command '%s' unknown: ", cmd_name
);
3911 /* convert count to "bytes" */
3914 struct target
*target
= Jim_CmdPrivData(goi
.interp
);
3915 uint8_t target_buf
[32];
3922 e
= target_read_memory(target
, a
, b
, y
/ b
, target_buf
);
3923 if (e
!= ERROR_OK
) {
3924 Jim_SetResult_sprintf(interp
, "error reading target @ 0x%08lx", (int)(a
));
3928 Jim_fprintf(interp
, interp
->cookie_stdout
, "0x%08x ", (int)(a
));
3931 for (x
= 0; x
< 16 && x
< y
; x
+= 4)
3933 z
= target_buffer_get_u32(target
, &(target_buf
[ x
* 4 ]));
3934 Jim_fprintf(interp
, interp
->cookie_stdout
, "%08x ", (int)(z
));
3936 for (; (x
< 16) ; x
+= 4) {
3937 Jim_fprintf(interp
, interp
->cookie_stdout
, " ");
3941 for (x
= 0; x
< 16 && x
< y
; x
+= 2)
3943 z
= target_buffer_get_u16(target
, &(target_buf
[ x
* 2 ]));
3944 Jim_fprintf(interp
, interp
->cookie_stdout
, "%04x ", (int)(z
));
3946 for (; (x
< 16) ; x
+= 2) {
3947 Jim_fprintf(interp
, interp
->cookie_stdout
, " ");
3952 for (x
= 0 ; (x
< 16) && (x
< y
) ; x
+= 1) {
3953 z
= target_buffer_get_u8(target
, &(target_buf
[ x
* 4 ]));
3954 Jim_fprintf(interp
, interp
->cookie_stdout
, "%02x ", (int)(z
));
3956 for (; (x
< 16) ; x
+= 1) {
3957 Jim_fprintf(interp
, interp
->cookie_stdout
, " ");
3961 /* ascii-ify the bytes */
3962 for (x
= 0 ; x
< y
; x
++) {
3963 if ((target_buf
[x
] >= 0x20) &&
3964 (target_buf
[x
] <= 0x7e)) {
3968 target_buf
[x
] = '.';
3973 target_buf
[x
] = ' ';
3978 /* print - with a newline */
3979 Jim_fprintf(interp
, interp
->cookie_stdout
, "%s\n", target_buf
);
3987 static int jim_target_mem2array(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
3989 struct target
*target
= Jim_CmdPrivData(interp
);
3990 return target_mem2array(interp
, target
, argc
- 1, argv
+ 1);
3993 static int jim_target_array2mem(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
3995 struct target
*target
= Jim_CmdPrivData(interp
);
3996 return target_array2mem(interp
, target
, argc
- 1, argv
+ 1);
3999 static int jim_target_tap_disabled(Jim_Interp
*interp
)
4001 Jim_SetResult_sprintf(interp
, "[TAP is disabled]");
4005 static int jim_target_examine(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4009 Jim_WrongNumArgs(interp
, 1, argv
, "[no parameters]");
4012 struct target
*target
= Jim_CmdPrivData(interp
);
4013 if (!target
->tap
->enabled
)
4014 return jim_target_tap_disabled(interp
);
4016 int e
= target
->type
->examine(target
);
4019 Jim_SetResult_sprintf(interp
, "examine-fails: %d", e
);
4025 static int jim_target_poll(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4029 Jim_WrongNumArgs(interp
, 1, argv
, "[no parameters]");
4032 struct target
*target
= Jim_CmdPrivData(interp
);
4033 if (!target
->tap
->enabled
)
4034 return jim_target_tap_disabled(interp
);
4037 if (!(target_was_examined(target
))) {
4038 e
= ERROR_TARGET_NOT_EXAMINED
;
4040 e
= target
->type
->poll(target
);
4044 Jim_SetResult_sprintf(interp
, "poll-fails: %d", e
);
4050 static int jim_target_reset(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4053 Jim_GetOpt_Setup(&goi
, interp
, argc
- 1, argv
+ 1);
4057 Jim_WrongNumArgs(interp
, 0, argv
,
4058 "([tT]|[fF]|assert|deassert) BOOL");
4063 int e
= Jim_GetOpt_Nvp(&goi
, nvp_assert
, &n
);
4066 Jim_GetOpt_NvpUnknown(&goi
, nvp_assert
, 1);
4069 /* the halt or not param */
4071 e
= Jim_GetOpt_Wide(&goi
, &a
);
4075 struct target
*target
= Jim_CmdPrivData(goi
.interp
);
4076 if (!target
->tap
->enabled
)
4077 return jim_target_tap_disabled(interp
);
4078 if (!target
->type
->assert_reset
|| !target
->type
->deassert_reset
)
4080 Jim_SetResult_sprintf(interp
,
4081 "No target-specific reset for %s",
4082 target_name(target
));
4085 /* determine if we should halt or not. */
4086 target
->reset_halt
= !!a
;
4087 /* When this happens - all workareas are invalid. */
4088 target_free_all_working_areas_restore(target
, 0);
4091 if (n
->value
== NVP_ASSERT
) {
4092 e
= target
->type
->assert_reset(target
);
4094 e
= target
->type
->deassert_reset(target
);
4096 return (e
== ERROR_OK
) ? JIM_OK
: JIM_ERR
;
4099 static int jim_target_halt(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4102 Jim_WrongNumArgs(interp
, 1, argv
, "[no parameters]");
4105 struct target
*target
= Jim_CmdPrivData(interp
);
4106 if (!target
->tap
->enabled
)
4107 return jim_target_tap_disabled(interp
);
4108 int e
= target
->type
->halt(target
);
4109 return (e
== ERROR_OK
) ? JIM_OK
: JIM_ERR
;
4112 static int jim_target_wait_state(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4115 Jim_GetOpt_Setup(&goi
, interp
, argc
- 1, argv
+ 1);
4117 /* params: <name> statename timeoutmsecs */
4120 const char *cmd_name
= Jim_GetString(argv
[0], NULL
);
4121 Jim_SetResult_sprintf(goi
.interp
,
4122 "%s <state_name> <timeout_in_msec>", cmd_name
);
4127 int e
= Jim_GetOpt_Nvp(&goi
, nvp_target_state
, &n
);
4129 Jim_GetOpt_NvpUnknown(&goi
, nvp_target_state
,1);
4133 e
= Jim_GetOpt_Wide(&goi
, &a
);
4137 struct target
*target
= Jim_CmdPrivData(interp
);
4138 if (!target
->tap
->enabled
)
4139 return jim_target_tap_disabled(interp
);
4141 e
= target_wait_state(target
, n
->value
, a
);
4144 Jim_SetResult_sprintf(goi
.interp
,
4145 "target: %s wait %s fails (%d) %s",
4146 target_name(target
), n
->name
,
4147 e
, target_strerror_safe(e
));
4152 /* List for human, Events defined for this target.
4153 * scripts/programs should use 'name cget -event NAME'
4155 static int jim_target_event_list(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4157 struct command_context
*cmd_ctx
= Jim_GetAssocData(interp
, "context");
4158 struct target
*target
= Jim_CmdPrivData(interp
);
4159 struct target_event_action
*teap
= target
->event_action
;
4160 command_print(cmd_ctx
, "Event actions for target (%d) %s\n",
4161 target
->target_number
,
4162 target_name(target
));
4163 command_print(cmd_ctx
, "%-25s | Body", "Event");
4164 command_print(cmd_ctx
, "------------------------- | "
4165 "----------------------------------------");
4168 Jim_Nvp
*opt
= Jim_Nvp_value2name_simple(nvp_target_event
, teap
->event
);
4169 command_print(cmd_ctx
, "%-25s | %s",
4170 opt
->name
, Jim_GetString(teap
->body
, NULL
));
4173 command_print(cmd_ctx
, "***END***");
4176 static int jim_target_current_state(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4180 Jim_WrongNumArgs(interp
, 1, argv
, "[no parameters]");
4183 struct target
*target
= Jim_CmdPrivData(interp
);
4184 Jim_SetResultString(interp
, target_state_name(target
), -1);
4187 static int jim_target_invoke_event(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4190 Jim_GetOpt_Setup(&goi
, interp
, argc
- 1, argv
+ 1);
4193 const char *cmd_name
= Jim_GetString(argv
[0], NULL
);
4194 Jim_SetResult_sprintf(goi
.interp
, "%s <eventname>", cmd_name
);
4198 int e
= Jim_GetOpt_Nvp(&goi
, nvp_target_event
, &n
);
4201 Jim_GetOpt_NvpUnknown(&goi
, nvp_target_event
, 1);
4204 struct target
*target
= Jim_CmdPrivData(interp
);
4205 target_handle_event(target
, n
->value
);
4209 static const struct command_registration target_instance_command_handlers
[] = {
4211 .name
= "configure",
4212 .mode
= COMMAND_CONFIG
,
4213 .jim_handler
= &jim_target_configure
,
4214 .usage
= "[<target_options> ...]",
4215 .help
= "configure a new target for use",
4219 .mode
= COMMAND_ANY
,
4220 .jim_handler
= &jim_target_configure
,
4221 .usage
= "<target_type> [<target_options> ...]",
4222 .help
= "configure a new target for use",
4226 .mode
= COMMAND_EXEC
,
4227 .jim_handler
= &jim_target_mw
,
4228 .usage
= "<address> <data> [<count>]",
4229 .help
= "Write 32-bit word(s) to target memory",
4233 .mode
= COMMAND_EXEC
,
4234 .jim_handler
= &jim_target_mw
,
4235 .usage
= "<address> <data> [<count>]",
4236 .help
= "Write 16-bit half-word(s) to target memory",
4240 .mode
= COMMAND_EXEC
,
4241 .jim_handler
= &jim_target_mw
,
4242 .usage
= "<address> <data> [<count>]",
4243 .help
= "Write byte(s) to target memory",
4247 .mode
= COMMAND_EXEC
,
4248 .jim_handler
= &jim_target_md
,
4249 .usage
= "<address> [<count>]",
4250 .help
= "Display target memory as 32-bit words",
4254 .mode
= COMMAND_EXEC
,
4255 .jim_handler
= &jim_target_md
,
4256 .usage
= "<address> [<count>]",
4257 .help
= "Display target memory as 16-bit half-words",
4261 .mode
= COMMAND_EXEC
,
4262 .jim_handler
= &jim_target_md
,
4263 .usage
= "<address> [<count>]",
4264 .help
= "Display target memory as 8-bit bytes",
4267 .name
= "array2mem",
4268 .mode
= COMMAND_EXEC
,
4269 .jim_handler
= &jim_target_array2mem
,
4272 .name
= "mem2array",
4273 .mode
= COMMAND_EXEC
,
4274 .jim_handler
= &jim_target_mem2array
,
4277 .name
= "eventlist",
4278 .mode
= COMMAND_EXEC
,
4279 .jim_handler
= &jim_target_event_list
,
4283 .mode
= COMMAND_EXEC
,
4284 .jim_handler
= &jim_target_current_state
,
4287 .name
= "arp_examine",
4288 .mode
= COMMAND_EXEC
,
4289 .jim_handler
= &jim_target_examine
,
4293 .mode
= COMMAND_EXEC
,
4294 .jim_handler
= &jim_target_poll
,
4297 .name
= "arp_reset",
4298 .mode
= COMMAND_EXEC
,
4299 .jim_handler
= &jim_target_reset
,
4303 .mode
= COMMAND_EXEC
,
4304 .jim_handler
= &jim_target_halt
,
4307 .name
= "arp_waitstate",
4308 .mode
= COMMAND_EXEC
,
4309 .jim_handler
= &jim_target_wait_state
,
4312 .name
= "invoke-event",
4313 .mode
= COMMAND_EXEC
,
4314 .jim_handler
= &jim_target_invoke_event
,
4316 COMMAND_REGISTRATION_DONE
4319 static int target_create(Jim_GetOptInfo
*goi
)
4327 struct target
*target
;
4328 struct command_context
*cmd_ctx
;
4330 cmd_ctx
= Jim_GetAssocData(goi
->interp
, "context");
4331 if (goi
->argc
< 3) {
4332 Jim_WrongNumArgs(goi
->interp
, 1, goi
->argv
, "?name? ?type? ..options...");
4337 Jim_GetOpt_Obj(goi
, &new_cmd
);
4338 /* does this command exist? */
4339 cmd
= Jim_GetCommand(goi
->interp
, new_cmd
, JIM_ERRMSG
);
4341 cp
= Jim_GetString(new_cmd
, NULL
);
4342 Jim_SetResult_sprintf(goi
->interp
, "Command/target: %s Exists", cp
);
4347 e
= Jim_GetOpt_String(goi
, &cp2
, NULL
);
4349 /* now does target type exist */
4350 for (x
= 0 ; target_types
[x
] ; x
++) {
4351 if (0 == strcmp(cp
, target_types
[x
]->name
)) {
4356 if (target_types
[x
] == NULL
) {
4357 Jim_SetResult_sprintf(goi
->interp
, "Unknown target type %s, try one of ", cp
);
4358 for (x
= 0 ; target_types
[x
] ; x
++) {
4359 if (target_types
[x
+ 1]) {
4360 Jim_AppendStrings(goi
->interp
,
4361 Jim_GetResult(goi
->interp
),
4362 target_types
[x
]->name
,
4365 Jim_AppendStrings(goi
->interp
,
4366 Jim_GetResult(goi
->interp
),
4368 target_types
[x
]->name
,NULL
);
4375 target
= calloc(1,sizeof(struct target
));
4376 /* set target number */
4377 target
->target_number
= new_target_number();
4379 /* allocate memory for each unique target type */
4380 target
->type
= (struct target_type
*)calloc(1,sizeof(struct target_type
));
4382 memcpy(target
->type
, target_types
[x
], sizeof(struct target_type
));
4384 /* will be set by "-endian" */
4385 target
->endianness
= TARGET_ENDIAN_UNKNOWN
;
4387 target
->working_area
= 0x0;
4388 target
->working_area_size
= 0x0;
4389 target
->working_areas
= NULL
;
4390 target
->backup_working_area
= 0;
4392 target
->state
= TARGET_UNKNOWN
;
4393 target
->debug_reason
= DBG_REASON_UNDEFINED
;
4394 target
->reg_cache
= NULL
;
4395 target
->breakpoints
= NULL
;
4396 target
->watchpoints
= NULL
;
4397 target
->next
= NULL
;
4398 target
->arch_info
= NULL
;
4400 target
->display
= 1;
4402 target
->halt_issued
= false;
4404 /* initialize trace information */
4405 target
->trace_info
= malloc(sizeof(struct trace
));
4406 target
->trace_info
->num_trace_points
= 0;
4407 target
->trace_info
->trace_points_size
= 0;
4408 target
->trace_info
->trace_points
= NULL
;
4409 target
->trace_info
->trace_history_size
= 0;
4410 target
->trace_info
->trace_history
= NULL
;
4411 target
->trace_info
->trace_history_pos
= 0;
4412 target
->trace_info
->trace_history_overflowed
= 0;
4414 target
->dbgmsg
= NULL
;
4415 target
->dbg_msg_enabled
= 0;
4417 target
->endianness
= TARGET_ENDIAN_UNKNOWN
;
4419 /* Do the rest as "configure" options */
4420 goi
->isconfigure
= 1;
4421 e
= target_configure(goi
, target
);
4423 if (target
->tap
== NULL
)
4425 Jim_SetResultString(goi
->interp
, "-chain-position required when creating target", -1);
4435 if (target
->endianness
== TARGET_ENDIAN_UNKNOWN
) {
4436 /* default endian to little if not specified */
4437 target
->endianness
= TARGET_LITTLE_ENDIAN
;
4440 /* incase variant is not set */
4441 if (!target
->variant
)
4442 target
->variant
= strdup("");
4444 cp
= Jim_GetString(new_cmd
, NULL
);
4445 target
->cmd_name
= strdup(cp
);
4447 /* create the target specific commands */
4448 if (target
->type
->commands
) {
4449 e
= register_commands(cmd_ctx
, NULL
, target
->type
->commands
);
4451 LOG_ERROR("unable to register '%s' commands", cp
);
4453 if (target
->type
->target_create
) {
4454 (*(target
->type
->target_create
))(target
, goi
->interp
);
4457 /* append to end of list */
4459 struct target
**tpp
;
4460 tpp
= &(all_targets
);
4462 tpp
= &((*tpp
)->next
);
4467 /* now - create the new target name command */
4468 const const struct command_registration target_subcommands
[] = {
4470 .chain
= target_instance_command_handlers
,
4473 .chain
= target
->type
->commands
,
4475 COMMAND_REGISTRATION_DONE
4477 const const struct command_registration target_commands
[] = {
4480 .mode
= COMMAND_ANY
,
4481 .help
= "target command group",
4482 .chain
= target_subcommands
,
4484 COMMAND_REGISTRATION_DONE
4486 e
= register_commands(cmd_ctx
, NULL
, target_commands
);
4490 struct command
*c
= command_find_in_context(cmd_ctx
, cp
);
4492 command_set_handler_data(c
, target
);
4494 return (ERROR_OK
== e
) ? JIM_OK
: JIM_ERR
;
4497 static int jim_target_current(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4501 Jim_WrongNumArgs(interp
, 1, argv
, "Too many parameters");
4504 struct command_context
*cmd_ctx
= Jim_GetAssocData(interp
, "context");
4505 Jim_SetResultString(interp
, get_current_target(cmd_ctx
)->cmd_name
, -1);
4509 static int jim_target_types(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4513 Jim_WrongNumArgs(interp
, 1, argv
, "Too many parameters");
4516 Jim_SetResult(interp
, Jim_NewListObj(interp
, NULL
, 0));
4517 for (unsigned x
= 0; NULL
!= target_types
[x
]; x
++)
4519 Jim_ListAppendElement(interp
, Jim_GetResult(interp
),
4520 Jim_NewStringObj(interp
, target_types
[x
]->name
, -1));
4525 static int jim_target_names(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4529 Jim_WrongNumArgs(interp
, 1, argv
, "Too many parameters");
4532 Jim_SetResult(interp
, Jim_NewListObj(interp
, NULL
, 0));
4533 struct target
*target
= all_targets
;
4536 Jim_ListAppendElement(interp
, Jim_GetResult(interp
),
4537 Jim_NewStringObj(interp
, target_name(target
), -1));
4538 target
= target
->next
;
4543 static int jim_target_create(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4546 Jim_GetOpt_Setup(&goi
, interp
, argc
- 1, argv
+ 1);
4549 Jim_WrongNumArgs(goi
.interp
, goi
.argc
, goi
.argv
,
4550 "<name> <target_type> [<target_options> ...]");
4553 return target_create(&goi
);
4556 static int jim_target_number(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4559 Jim_GetOpt_Setup(&goi
, interp
, argc
- 1, argv
+ 1);
4561 /* It's OK to remove this mechanism sometime after August 2010 or so */
4562 LOG_WARNING("don't use numbers as target identifiers; use names");
4565 Jim_SetResult_sprintf(goi
.interp
, "usage: target number <number>");
4569 int e
= Jim_GetOpt_Wide(&goi
, &w
);
4573 struct target
*target
;
4574 for (target
= all_targets
; NULL
!= target
; target
= target
->next
)
4576 if (target
->target_number
!= w
)
4579 Jim_SetResultString(goi
.interp
, target_name(target
), -1);
4582 Jim_SetResult_sprintf(goi
.interp
,
4583 "Target: number %d does not exist", (int)(w
));
4587 static int jim_target_count(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4591 Jim_WrongNumArgs(interp
, 1, argv
, "<no parameters>");
4595 struct target
*target
= all_targets
;
4596 while (NULL
!= target
)
4598 target
= target
->next
;
4601 Jim_SetResult(interp
, Jim_NewIntObj(interp
, count
));
4605 static const struct command_registration target_subcommand_handlers
[] = {
4608 .mode
= COMMAND_CONFIG
,
4609 .handler
= &handle_target_init_command
,
4610 .help
= "initialize targets",
4614 .mode
= COMMAND_ANY
,
4615 .jim_handler
= &jim_target_create
,
4616 .usage
= "<name> <type> ...",
4617 .help
= "Returns the currently selected target",
4621 .mode
= COMMAND_ANY
,
4622 .jim_handler
= &jim_target_current
,
4623 .help
= "Returns the currently selected target",
4627 .mode
= COMMAND_ANY
,
4628 .jim_handler
= &jim_target_types
,
4629 .help
= "Returns the available target types as a list of strings",
4633 .mode
= COMMAND_ANY
,
4634 .jim_handler
= &jim_target_names
,
4635 .help
= "Returns the names of all targets as a list of strings",
4639 .mode
= COMMAND_ANY
,
4640 .jim_handler
= &jim_target_number
,
4641 .usage
= "<number>",
4642 .help
= "Returns the name of target <n>",
4646 .mode
= COMMAND_ANY
,
4647 .jim_handler
= &jim_target_count
,
4648 .help
= "Returns the number of targets as an integer",
4650 COMMAND_REGISTRATION_DONE
4662 static int fastload_num
;
4663 static struct FastLoad
*fastload
;
4665 static void free_fastload(void)
4667 if (fastload
!= NULL
)
4670 for (i
= 0; i
< fastload_num
; i
++)
4672 if (fastload
[i
].data
)
4673 free(fastload
[i
].data
);
4683 COMMAND_HANDLER(handle_fast_load_image_command
)
4687 uint32_t image_size
;
4688 uint32_t min_address
= 0;
4689 uint32_t max_address
= 0xffffffff;
4694 int retval
= CALL_COMMAND_HANDLER(parse_load_image_command_CMD_ARGV
,
4695 &image
, &min_address
, &max_address
);
4696 if (ERROR_OK
!= retval
)
4699 struct duration bench
;
4700 duration_start(&bench
);
4702 if (image_open(&image
, CMD_ARGV
[0], (CMD_ARGC
>= 3) ? CMD_ARGV
[2] : NULL
) != ERROR_OK
)
4709 fastload_num
= image
.num_sections
;
4710 fastload
= (struct FastLoad
*)malloc(sizeof(struct FastLoad
)*image
.num_sections
);
4711 if (fastload
== NULL
)
4713 image_close(&image
);
4716 memset(fastload
, 0, sizeof(struct FastLoad
)*image
.num_sections
);
4717 for (i
= 0; i
< image
.num_sections
; i
++)
4719 buffer
= malloc(image
.sections
[i
].size
);
4722 command_print(CMD_CTX
, "error allocating buffer for section (%d bytes)",
4723 (int)(image
.sections
[i
].size
));
4727 if ((retval
= image_read_section(&image
, i
, 0x0, image
.sections
[i
].size
, buffer
, &buf_cnt
)) != ERROR_OK
)
4733 uint32_t offset
= 0;
4734 uint32_t length
= buf_cnt
;
4737 /* DANGER!!! beware of unsigned comparision here!!! */
4739 if ((image
.sections
[i
].base_address
+ buf_cnt
>= min_address
)&&
4740 (image
.sections
[i
].base_address
< max_address
))
4742 if (image
.sections
[i
].base_address
< min_address
)
4744 /* clip addresses below */
4745 offset
+= min_address
-image
.sections
[i
].base_address
;
4749 if (image
.sections
[i
].base_address
+ buf_cnt
> max_address
)
4751 length
-= (image
.sections
[i
].base_address
+ buf_cnt
)-max_address
;
4754 fastload
[i
].address
= image
.sections
[i
].base_address
+ offset
;
4755 fastload
[i
].data
= malloc(length
);
4756 if (fastload
[i
].data
== NULL
)
4761 memcpy(fastload
[i
].data
, buffer
+ offset
, length
);
4762 fastload
[i
].length
= length
;
4764 image_size
+= length
;
4765 command_print(CMD_CTX
, "%u bytes written at address 0x%8.8x",
4766 (unsigned int)length
,
4767 ((unsigned int)(image
.sections
[i
].base_address
+ offset
)));
4773 if ((ERROR_OK
== retval
) && (duration_measure(&bench
) == ERROR_OK
))
4775 command_print(CMD_CTX
, "Loaded %" PRIu32
" bytes "
4776 "in %fs (%0.3f kb/s)", image_size
,
4777 duration_elapsed(&bench
), duration_kbps(&bench
, image_size
));
4779 command_print(CMD_CTX
,
4780 "WARNING: image has not been loaded to target!"
4781 "You can issue a 'fast_load' to finish loading.");
4784 image_close(&image
);
4786 if (retval
!= ERROR_OK
)
4794 COMMAND_HANDLER(handle_fast_load_command
)
4797 return ERROR_COMMAND_SYNTAX_ERROR
;
4798 if (fastload
== NULL
)
4800 LOG_ERROR("No image in memory");
4804 int ms
= timeval_ms();
4806 int retval
= ERROR_OK
;
4807 for (i
= 0; i
< fastload_num
;i
++)
4809 struct target
*target
= get_current_target(CMD_CTX
);
4810 command_print(CMD_CTX
, "Write to 0x%08x, length 0x%08x",
4811 (unsigned int)(fastload
[i
].address
),
4812 (unsigned int)(fastload
[i
].length
));
4813 if (retval
== ERROR_OK
)
4815 retval
= target_write_buffer(target
, fastload
[i
].address
, fastload
[i
].length
, fastload
[i
].data
);
4817 size
+= fastload
[i
].length
;
4819 int after
= timeval_ms();
4820 command_print(CMD_CTX
, "Loaded image %f kBytes/s", (float)(size
/1024.0)/((float)(after
-ms
)/1000.0));
4824 static const struct command_registration target_command_handlers
[] = {
4827 .handler
= &handle_targets_command
,
4828 .mode
= COMMAND_ANY
,
4829 .help
= "change current command line target (one parameter) "
4830 "or list targets (no parameters)",
4831 .usage
= "[<new_current_target>]",
4835 .mode
= COMMAND_CONFIG
,
4836 .help
= "configure target",
4838 .chain
= target_subcommand_handlers
,
4840 COMMAND_REGISTRATION_DONE
4843 int target_register_commands(struct command_context
*cmd_ctx
)
4845 return register_commands(cmd_ctx
, NULL
, target_command_handlers
);
4848 static const struct command_registration target_exec_command_handlers
[] = {
4850 .name
= "fast_load_image",
4851 .handler
= &handle_fast_load_image_command
,
4852 .mode
= COMMAND_ANY
,
4853 .help
= "Load image into memory, mainly for profiling purposes",
4854 .usage
= "<file> <address> ['bin'|'ihex'|'elf'|'s19'] "
4855 "[min_address] [max_length]",
4858 .name
= "fast_load",
4859 .handler
= &handle_fast_load_command
,
4860 .mode
= COMMAND_ANY
,
4861 .help
= "loads active fast load image to current target "
4862 "- mainly for profiling purposes",
4866 .handler
= &handle_profile_command
,
4867 .mode
= COMMAND_EXEC
,
4868 .help
= "profiling samples the CPU PC",
4870 /** @todo don't register virt2phys() unless target supports it */
4872 .name
= "virt2phys",
4873 .handler
= &handle_virt2phys_command
,
4874 .mode
= COMMAND_ANY
,
4875 .help
= "translate a virtual address into a physical address",
4880 .handler
= &handle_reg_command
,
4881 .mode
= COMMAND_EXEC
,
4882 .help
= "display or set a register",
4887 .handler
= &handle_poll_command
,
4888 .mode
= COMMAND_EXEC
,
4889 .help
= "poll target state",
4892 .name
= "wait_halt",
4893 .handler
= &handle_wait_halt_command
,
4894 .mode
= COMMAND_EXEC
,
4895 .help
= "wait for target halt",
4896 .usage
= "[time (s)]",
4900 .handler
= &handle_halt_command
,
4901 .mode
= COMMAND_EXEC
,
4902 .help
= "halt target",
4906 .handler
= &handle_resume_command
,
4907 .mode
= COMMAND_EXEC
,
4908 .help
= "resume target",
4909 .usage
= "[<address>]",
4913 .handler
= &handle_reset_command
,
4914 .mode
= COMMAND_EXEC
,
4915 .usage
= "[run|halt|init]",
4916 .help
= "Reset all targets into the specified mode."
4917 "Default reset mode is run, if not given.",
4920 .name
= "soft_reset_halt",
4921 .handler
= &handle_soft_reset_halt_command
,
4922 .mode
= COMMAND_EXEC
,
4923 .help
= "halt the target and do a soft reset",
4928 .handler
= &handle_step_command
,
4929 .mode
= COMMAND_EXEC
,
4930 .help
= "step one instruction from current PC or [addr]",
4931 .usage
= "[<address>]",
4936 .handler
= &handle_md_command
,
4937 .mode
= COMMAND_EXEC
,
4938 .help
= "display memory words",
4939 .usage
= "[phys] <addr> [count]",
4943 .handler
= &handle_md_command
,
4944 .mode
= COMMAND_EXEC
,
4945 .help
= "display memory half-words",
4946 .usage
= "[phys] <addr> [count]",
4950 .handler
= &handle_md_command
,
4951 .mode
= COMMAND_EXEC
,
4952 .help
= "display memory bytes",
4953 .usage
= "[phys] <addr> [count]",
4958 .handler
= &handle_mw_command
,
4959 .mode
= COMMAND_EXEC
,
4960 .help
= "write memory word",
4961 .usage
= "[phys] <addr> <value> [count]",
4965 .handler
= &handle_mw_command
,
4966 .mode
= COMMAND_EXEC
,
4967 .help
= "write memory half-word",
4968 .usage
= "[phys] <addr> <value> [count]",
4972 .handler
= &handle_mw_command
,
4973 .mode
= COMMAND_EXEC
,
4974 .help
= "write memory byte",
4975 .usage
= "[phys] <addr> <value> [count]",
4980 .handler
= &handle_bp_command
,
4981 .mode
= COMMAND_EXEC
,
4982 .help
= "list or set breakpoint",
4983 .usage
= "[<address> <length> [hw]]",
4987 .handler
= &handle_rbp_command
,
4988 .mode
= COMMAND_EXEC
,
4989 .help
= "remove breakpoint",
4990 .usage
= "<address>",
4995 .handler
= &handle_wp_command
,
4996 .mode
= COMMAND_EXEC
,
4997 .help
= "list or set watchpoint",
4998 .usage
= "[<address> <length> <r/w/a> [value] [mask]]",
5002 .handler
= &handle_rwp_command
,
5003 .mode
= COMMAND_EXEC
,
5004 .help
= "remove watchpoint",
5005 .usage
= "<address>",
5009 .name
= "load_image",
5010 .handler
= &handle_load_image_command
,
5011 .mode
= COMMAND_EXEC
,
5012 .usage
= "<file> <address> ['bin'|'ihex'|'elf'|'s19'] "
5013 "[min_address] [max_length]",
5016 .name
= "dump_image",
5017 .handler
= &handle_dump_image_command
,
5018 .mode
= COMMAND_EXEC
,
5019 .usage
= "<file> <address> <size>",
5022 .name
= "verify_image",
5023 .handler
= &handle_verify_image_command
,
5024 .mode
= COMMAND_EXEC
,
5025 .usage
= "<file> [offset] [type]",
5028 .name
= "test_image",
5029 .handler
= &handle_test_image_command
,
5030 .mode
= COMMAND_EXEC
,
5031 .usage
= "<file> [offset] [type]",
5034 .name
= "ocd_mem2array",
5035 .mode
= COMMAND_EXEC
,
5036 .jim_handler
= &jim_mem2array
,
5037 .help
= "read memory and return as a TCL array "
5038 "for script processing",
5039 .usage
= "<arrayname> <width=32|16|8> <address> <count>",
5042 .name
= "ocd_array2mem",
5043 .mode
= COMMAND_EXEC
,
5044 .jim_handler
= &jim_array2mem
,
5045 .help
= "convert a TCL array to memory locations "
5046 "and write the values",
5047 .usage
= "<arrayname> <width=32|16|8> <address> <count>",
5049 COMMAND_REGISTRATION_DONE
5051 int target_register_user_commands(struct command_context
*cmd_ctx
)
5053 int retval
= ERROR_OK
;
5054 if ((retval
= target_request_register_commands(cmd_ctx
)) != ERROR_OK
)
5057 if ((retval
= trace_register_commands(cmd_ctx
)) != ERROR_OK
)
5061 return register_commands(cmd_ctx
, NULL
, target_exec_command_handlers
);