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 ***************************************************************************/
36 #include <helper/time_support.h>
37 #include <jtag/jtag.h>
40 #include "target_type.h"
41 #include "target_request.h"
42 #include "breakpoints.h"
48 static int target_array2mem(Jim_Interp
*interp
, struct target
*target
,
49 int argc
, Jim_Obj
*const *argv
);
50 static int target_mem2array(Jim_Interp
*interp
, struct target
*target
,
51 int argc
, Jim_Obj
*const *argv
);
54 extern struct target_type arm7tdmi_target
;
55 extern struct target_type arm720t_target
;
56 extern struct target_type arm9tdmi_target
;
57 extern struct target_type arm920t_target
;
58 extern struct target_type arm966e_target
;
59 extern struct target_type arm926ejs_target
;
60 extern struct target_type fa526_target
;
61 extern struct target_type feroceon_target
;
62 extern struct target_type dragonite_target
;
63 extern struct target_type xscale_target
;
64 extern struct target_type cortexm3_target
;
65 extern struct target_type cortexa8_target
;
66 extern struct target_type arm11_target
;
67 extern struct target_type mips_m4k_target
;
68 extern struct target_type avr_target
;
69 extern struct target_type dsp563xx_target
;
70 extern struct target_type testee_target
;
72 struct target_type
*target_types
[] =
94 struct target
*all_targets
= NULL
;
95 struct target_event_callback
*target_event_callbacks
= NULL
;
96 struct target_timer_callback
*target_timer_callbacks
= NULL
;
98 static const Jim_Nvp nvp_assert
[] = {
99 { .name
= "assert", NVP_ASSERT
},
100 { .name
= "deassert", NVP_DEASSERT
},
101 { .name
= "T", NVP_ASSERT
},
102 { .name
= "F", NVP_DEASSERT
},
103 { .name
= "t", NVP_ASSERT
},
104 { .name
= "f", NVP_DEASSERT
},
105 { .name
= NULL
, .value
= -1 }
108 static const Jim_Nvp nvp_error_target
[] = {
109 { .value
= ERROR_TARGET_INVALID
, .name
= "err-invalid" },
110 { .value
= ERROR_TARGET_INIT_FAILED
, .name
= "err-init-failed" },
111 { .value
= ERROR_TARGET_TIMEOUT
, .name
= "err-timeout" },
112 { .value
= ERROR_TARGET_NOT_HALTED
, .name
= "err-not-halted" },
113 { .value
= ERROR_TARGET_FAILURE
, .name
= "err-failure" },
114 { .value
= ERROR_TARGET_UNALIGNED_ACCESS
, .name
= "err-unaligned-access" },
115 { .value
= ERROR_TARGET_DATA_ABORT
, .name
= "err-data-abort" },
116 { .value
= ERROR_TARGET_RESOURCE_NOT_AVAILABLE
, .name
= "err-resource-not-available" },
117 { .value
= ERROR_TARGET_TRANSLATION_FAULT
, .name
= "err-translation-fault" },
118 { .value
= ERROR_TARGET_NOT_RUNNING
, .name
= "err-not-running" },
119 { .value
= ERROR_TARGET_NOT_EXAMINED
, .name
= "err-not-examined" },
120 { .value
= -1, .name
= NULL
}
123 const char *target_strerror_safe(int err
)
127 n
= Jim_Nvp_value2name_simple(nvp_error_target
, err
);
128 if (n
->name
== NULL
) {
135 static const Jim_Nvp nvp_target_event
[] = {
136 { .value
= TARGET_EVENT_OLD_gdb_program_config
, .name
= "old-gdb_program_config" },
137 { .value
= TARGET_EVENT_OLD_pre_resume
, .name
= "old-pre_resume" },
139 { .value
= TARGET_EVENT_GDB_HALT
, .name
= "gdb-halt" },
140 { .value
= TARGET_EVENT_HALTED
, .name
= "halted" },
141 { .value
= TARGET_EVENT_RESUMED
, .name
= "resumed" },
142 { .value
= TARGET_EVENT_RESUME_START
, .name
= "resume-start" },
143 { .value
= TARGET_EVENT_RESUME_END
, .name
= "resume-end" },
145 { .name
= "gdb-start", .value
= TARGET_EVENT_GDB_START
},
146 { .name
= "gdb-end", .value
= TARGET_EVENT_GDB_END
},
148 /* historical name */
150 { .value
= TARGET_EVENT_RESET_START
, .name
= "reset-start" },
152 { .value
= TARGET_EVENT_RESET_ASSERT_PRE
, .name
= "reset-assert-pre" },
153 { .value
= TARGET_EVENT_RESET_ASSERT
, .name
= "reset-assert" },
154 { .value
= TARGET_EVENT_RESET_ASSERT_POST
, .name
= "reset-assert-post" },
155 { .value
= TARGET_EVENT_RESET_DEASSERT_PRE
, .name
= "reset-deassert-pre" },
156 { .value
= TARGET_EVENT_RESET_DEASSERT_POST
, .name
= "reset-deassert-post" },
157 { .value
= TARGET_EVENT_RESET_HALT_PRE
, .name
= "reset-halt-pre" },
158 { .value
= TARGET_EVENT_RESET_HALT_POST
, .name
= "reset-halt-post" },
159 { .value
= TARGET_EVENT_RESET_WAIT_PRE
, .name
= "reset-wait-pre" },
160 { .value
= TARGET_EVENT_RESET_WAIT_POST
, .name
= "reset-wait-post" },
161 { .value
= TARGET_EVENT_RESET_INIT
, .name
= "reset-init" },
162 { .value
= TARGET_EVENT_RESET_END
, .name
= "reset-end" },
164 { .value
= TARGET_EVENT_EXAMINE_START
, .name
= "examine-start" },
165 { .value
= TARGET_EVENT_EXAMINE_END
, .name
= "examine-end" },
167 { .value
= TARGET_EVENT_DEBUG_HALTED
, .name
= "debug-halted" },
168 { .value
= TARGET_EVENT_DEBUG_RESUMED
, .name
= "debug-resumed" },
170 { .value
= TARGET_EVENT_GDB_ATTACH
, .name
= "gdb-attach" },
171 { .value
= TARGET_EVENT_GDB_DETACH
, .name
= "gdb-detach" },
173 { .value
= TARGET_EVENT_GDB_FLASH_WRITE_START
, .name
= "gdb-flash-write-start" },
174 { .value
= TARGET_EVENT_GDB_FLASH_WRITE_END
, .name
= "gdb-flash-write-end" },
176 { .value
= TARGET_EVENT_GDB_FLASH_ERASE_START
, .name
= "gdb-flash-erase-start" },
177 { .value
= TARGET_EVENT_GDB_FLASH_ERASE_END
, .name
= "gdb-flash-erase-end" },
179 { .value
= TARGET_EVENT_RESUME_START
, .name
= "resume-start" },
180 { .value
= TARGET_EVENT_RESUMED
, .name
= "resume-ok" },
181 { .value
= TARGET_EVENT_RESUME_END
, .name
= "resume-end" },
183 { .name
= NULL
, .value
= -1 }
186 static const Jim_Nvp nvp_target_state
[] = {
187 { .name
= "unknown", .value
= TARGET_UNKNOWN
},
188 { .name
= "running", .value
= TARGET_RUNNING
},
189 { .name
= "halted", .value
= TARGET_HALTED
},
190 { .name
= "reset", .value
= TARGET_RESET
},
191 { .name
= "debug-running", .value
= TARGET_DEBUG_RUNNING
},
192 { .name
= NULL
, .value
= -1 },
195 static const Jim_Nvp nvp_target_debug_reason
[] = {
196 { .name
= "debug-request" , .value
= DBG_REASON_DBGRQ
},
197 { .name
= "breakpoint" , .value
= DBG_REASON_BREAKPOINT
},
198 { .name
= "watchpoint" , .value
= DBG_REASON_WATCHPOINT
},
199 { .name
= "watchpoint-and-breakpoint", .value
= DBG_REASON_WPTANDBKPT
},
200 { .name
= "single-step" , .value
= DBG_REASON_SINGLESTEP
},
201 { .name
= "target-not-halted" , .value
= DBG_REASON_NOTHALTED
},
202 { .name
= "undefined" , .value
= DBG_REASON_UNDEFINED
},
203 { .name
= NULL
, .value
= -1 },
206 static const Jim_Nvp nvp_target_endian
[] = {
207 { .name
= "big", .value
= TARGET_BIG_ENDIAN
},
208 { .name
= "little", .value
= TARGET_LITTLE_ENDIAN
},
209 { .name
= "be", .value
= TARGET_BIG_ENDIAN
},
210 { .name
= "le", .value
= TARGET_LITTLE_ENDIAN
},
211 { .name
= NULL
, .value
= -1 },
214 static const Jim_Nvp nvp_reset_modes
[] = {
215 { .name
= "unknown", .value
= RESET_UNKNOWN
},
216 { .name
= "run" , .value
= RESET_RUN
},
217 { .name
= "halt" , .value
= RESET_HALT
},
218 { .name
= "init" , .value
= RESET_INIT
},
219 { .name
= NULL
, .value
= -1 },
222 const char *debug_reason_name(struct target
*t
)
226 cp
= Jim_Nvp_value2name_simple(nvp_target_debug_reason
,
227 t
->debug_reason
)->name
;
229 LOG_ERROR("Invalid debug reason: %d", (int)(t
->debug_reason
));
230 cp
= "(*BUG*unknown*BUG*)";
236 target_state_name( struct target
*t
)
239 cp
= Jim_Nvp_value2name_simple(nvp_target_state
, t
->state
)->name
;
241 LOG_ERROR("Invalid target state: %d", (int)(t
->state
));
242 cp
= "(*BUG*unknown*BUG*)";
247 /* determine the number of the new target */
248 static int new_target_number(void)
253 /* number is 0 based */
257 if (x
< t
->target_number
) {
258 x
= t
->target_number
;
265 /* read a uint32_t from a buffer in target memory endianness */
266 uint32_t target_buffer_get_u32(struct target
*target
, const uint8_t *buffer
)
268 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
269 return le_to_h_u32(buffer
);
271 return be_to_h_u32(buffer
);
274 /* read a uint16_t from a buffer in target memory endianness */
275 uint16_t target_buffer_get_u16(struct target
*target
, const uint8_t *buffer
)
277 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
278 return le_to_h_u16(buffer
);
280 return be_to_h_u16(buffer
);
283 /* read a uint8_t from a buffer in target memory endianness */
284 uint8_t target_buffer_get_u8(struct target
*target
, const uint8_t *buffer
)
286 return *buffer
& 0x0ff;
289 /* write a uint32_t to a buffer in target memory endianness */
290 void target_buffer_set_u32(struct target
*target
, uint8_t *buffer
, uint32_t value
)
292 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
293 h_u32_to_le(buffer
, value
);
295 h_u32_to_be(buffer
, value
);
298 /* write a uint16_t to a buffer in target memory endianness */
299 void target_buffer_set_u16(struct target
*target
, uint8_t *buffer
, uint16_t value
)
301 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
302 h_u16_to_le(buffer
, value
);
304 h_u16_to_be(buffer
, value
);
307 /* write a uint8_t to a buffer in target memory endianness */
308 void target_buffer_set_u8(struct target
*target
, uint8_t *buffer
, uint8_t value
)
313 /* return a pointer to a configured target; id is name or number */
314 struct target
*get_target(const char *id
)
316 struct target
*target
;
318 /* try as tcltarget name */
319 for (target
= all_targets
; target
; target
= target
->next
) {
320 if (target
->cmd_name
== NULL
)
322 if (strcmp(id
, target
->cmd_name
) == 0)
326 /* It's OK to remove this fallback sometime after August 2010 or so */
328 /* no match, try as number */
330 if (parse_uint(id
, &num
) != ERROR_OK
)
333 for (target
= all_targets
; target
; target
= target
->next
) {
334 if (target
->target_number
== (int)num
) {
335 LOG_WARNING("use '%s' as target identifier, not '%u'",
336 target
->cmd_name
, num
);
344 /* returns a pointer to the n-th configured target */
345 static struct target
*get_target_by_num(int num
)
347 struct target
*target
= all_targets
;
350 if (target
->target_number
== num
) {
353 target
= target
->next
;
359 struct target
* get_current_target(struct command_context
*cmd_ctx
)
361 struct target
*target
= get_target_by_num(cmd_ctx
->current_target
);
365 LOG_ERROR("BUG: current_target out of bounds");
372 int target_poll(struct target
*target
)
376 /* We can't poll until after examine */
377 if (!target_was_examined(target
))
379 /* Fail silently lest we pollute the log */
383 retval
= target
->type
->poll(target
);
384 if (retval
!= ERROR_OK
)
387 if (target
->halt_issued
)
389 if (target
->state
== TARGET_HALTED
)
391 target
->halt_issued
= false;
394 long long t
= timeval_ms() - target
->halt_issued_time
;
397 target
->halt_issued
= false;
398 LOG_INFO("Halt timed out, wake up GDB.");
399 target_call_event_callbacks(target
, TARGET_EVENT_GDB_HALT
);
407 int target_halt(struct target
*target
)
410 /* We can't poll until after examine */
411 if (!target_was_examined(target
))
413 LOG_ERROR("Target not examined yet");
417 retval
= target
->type
->halt(target
);
418 if (retval
!= ERROR_OK
)
421 target
->halt_issued
= true;
422 target
->halt_issued_time
= timeval_ms();
427 int target_resume(struct target
*target
, int current
, uint32_t address
, int handle_breakpoints
, int debug_execution
)
431 /* We can't poll until after examine */
432 if (!target_was_examined(target
))
434 LOG_ERROR("Target not examined yet");
438 /* note that resume *must* be asynchronous. The CPU can halt before we poll. The CPU can
439 * even halt at the current PC as a result of a software breakpoint being inserted by (a bug?)
442 if ((retval
= target
->type
->resume(target
, current
, address
, handle_breakpoints
, debug_execution
)) != ERROR_OK
)
448 int target_process_reset(struct command_context
*cmd_ctx
, enum target_reset_mode reset_mode
)
453 n
= Jim_Nvp_value2name_simple(nvp_reset_modes
, reset_mode
);
454 if (n
->name
== NULL
) {
455 LOG_ERROR("invalid reset mode");
459 /* disable polling during reset to make reset event scripts
460 * more predictable, i.e. dr/irscan & pathmove in events will
461 * not have JTAG operations injected into the middle of a sequence.
463 bool save_poll
= jtag_poll_get_enabled();
465 jtag_poll_set_enabled(false);
467 sprintf(buf
, "ocd_process_reset %s", n
->name
);
468 retval
= Jim_Eval(cmd_ctx
->interp
, buf
);
470 jtag_poll_set_enabled(save_poll
);
472 if (retval
!= JIM_OK
) {
473 Jim_PrintErrorMessage(cmd_ctx
->interp
);
477 /* We want any events to be processed before the prompt */
478 retval
= target_call_timer_callbacks_now();
483 static int identity_virt2phys(struct target
*target
,
484 uint32_t virtual, uint32_t *physical
)
490 static int no_mmu(struct target
*target
, int *enabled
)
496 static int default_examine(struct target
*target
)
498 target_set_examined(target
);
502 int target_examine_one(struct target
*target
)
504 return target
->type
->examine(target
);
507 static int jtag_enable_callback(enum jtag_event event
, void *priv
)
509 struct target
*target
= priv
;
511 if (event
!= JTAG_TAP_EVENT_ENABLE
|| !target
->tap
->enabled
)
514 jtag_unregister_event_callback(jtag_enable_callback
, target
);
515 return target_examine_one(target
);
519 /* Targets that correctly implement init + examine, i.e.
520 * no communication with target during init:
524 int target_examine(void)
526 int retval
= ERROR_OK
;
527 struct target
*target
;
529 for (target
= all_targets
; target
; target
= target
->next
)
531 /* defer examination, but don't skip it */
532 if (!target
->tap
->enabled
) {
533 jtag_register_event_callback(jtag_enable_callback
,
537 if ((retval
= target_examine_one(target
)) != ERROR_OK
)
542 const char *target_type_name(struct target
*target
)
544 return target
->type
->name
;
547 static int target_write_memory_imp(struct target
*target
, uint32_t address
, uint32_t size
, uint32_t count
, uint8_t *buffer
)
549 if (!target_was_examined(target
))
551 LOG_ERROR("Target not examined yet");
554 return target
->type
->write_memory_imp(target
, address
, size
, count
, buffer
);
557 static int target_read_memory_imp(struct target
*target
, uint32_t address
, uint32_t size
, uint32_t count
, uint8_t *buffer
)
559 if (!target_was_examined(target
))
561 LOG_ERROR("Target not examined yet");
564 return target
->type
->read_memory_imp(target
, address
, size
, count
, buffer
);
567 static int target_soft_reset_halt_imp(struct target
*target
)
569 if (!target_was_examined(target
))
571 LOG_ERROR("Target not examined yet");
574 if (!target
->type
->soft_reset_halt_imp
) {
575 LOG_ERROR("Target %s does not support soft_reset_halt",
576 target_name(target
));
579 return target
->type
->soft_reset_halt_imp(target
);
582 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
)
584 if (!target_was_examined(target
))
586 LOG_ERROR("Target not examined yet");
589 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
);
592 int target_read_memory(struct target
*target
,
593 uint32_t address
, uint32_t size
, uint32_t count
, uint8_t *buffer
)
595 return target
->type
->read_memory(target
, address
, size
, count
, buffer
);
598 int target_read_phys_memory(struct target
*target
,
599 uint32_t address
, uint32_t size
, uint32_t count
, uint8_t *buffer
)
601 return target
->type
->read_phys_memory(target
, address
, size
, count
, buffer
);
604 int target_write_memory(struct target
*target
,
605 uint32_t address
, uint32_t size
, uint32_t count
, uint8_t *buffer
)
607 return target
->type
->write_memory(target
, address
, size
, count
, buffer
);
610 int target_write_phys_memory(struct target
*target
,
611 uint32_t address
, uint32_t size
, uint32_t count
, uint8_t *buffer
)
613 return target
->type
->write_phys_memory(target
, address
, size
, count
, buffer
);
616 int target_bulk_write_memory(struct target
*target
,
617 uint32_t address
, uint32_t count
, uint8_t *buffer
)
619 return target
->type
->bulk_write_memory(target
, address
, count
, buffer
);
622 int target_add_breakpoint(struct target
*target
,
623 struct breakpoint
*breakpoint
)
625 if (target
->state
!= TARGET_HALTED
) {
626 LOG_WARNING("target %s is not halted", target
->cmd_name
);
627 return ERROR_TARGET_NOT_HALTED
;
629 return target
->type
->add_breakpoint(target
, breakpoint
);
631 int target_remove_breakpoint(struct target
*target
,
632 struct breakpoint
*breakpoint
)
634 return target
->type
->remove_breakpoint(target
, breakpoint
);
637 int target_add_watchpoint(struct target
*target
,
638 struct watchpoint
*watchpoint
)
640 if (target
->state
!= TARGET_HALTED
) {
641 LOG_WARNING("target %s is not halted", target
->cmd_name
);
642 return ERROR_TARGET_NOT_HALTED
;
644 return target
->type
->add_watchpoint(target
, watchpoint
);
646 int target_remove_watchpoint(struct target
*target
,
647 struct watchpoint
*watchpoint
)
649 return target
->type
->remove_watchpoint(target
, watchpoint
);
652 int target_get_gdb_reg_list(struct target
*target
,
653 struct reg
**reg_list
[], int *reg_list_size
)
655 return target
->type
->get_gdb_reg_list(target
, reg_list
, reg_list_size
);
657 int target_step(struct target
*target
,
658 int current
, uint32_t address
, int handle_breakpoints
)
660 return target
->type
->step(target
, current
, address
, handle_breakpoints
);
664 int target_run_algorithm(struct target
*target
,
665 int num_mem_params
, struct mem_param
*mem_params
,
666 int num_reg_params
, struct reg_param
*reg_param
,
667 uint32_t entry_point
, uint32_t exit_point
,
668 int timeout_ms
, void *arch_info
)
670 return target
->type
->run_algorithm(target
,
671 num_mem_params
, mem_params
, num_reg_params
, reg_param
,
672 entry_point
, exit_point
, timeout_ms
, arch_info
);
676 * Reset the @c examined flag for the given target.
677 * Pure paranoia -- targets are zeroed on allocation.
679 static void target_reset_examined(struct target
*target
)
681 target
->examined
= false;
685 err_read_phys_memory(struct target
*target
, uint32_t address
,
686 uint32_t size
, uint32_t count
, uint8_t *buffer
)
688 LOG_ERROR("Not implemented: %s", __func__
);
693 err_write_phys_memory(struct target
*target
, uint32_t address
,
694 uint32_t size
, uint32_t count
, uint8_t *buffer
)
696 LOG_ERROR("Not implemented: %s", __func__
);
700 static int handle_target(void *priv
);
702 static int target_init_one(struct command_context
*cmd_ctx
,
703 struct target
*target
)
705 target_reset_examined(target
);
707 struct target_type
*type
= target
->type
;
708 if (type
->examine
== NULL
)
709 type
->examine
= default_examine
;
711 int retval
= type
->init_target(cmd_ctx
, target
);
712 if (ERROR_OK
!= retval
)
714 LOG_ERROR("target '%s' init failed", target_name(target
));
719 * @todo get rid of those *memory_imp() methods, now that all
720 * callers are using target_*_memory() accessors ... and make
721 * sure the "physical" paths handle the same issues.
723 /* a non-invasive way(in terms of patches) to add some code that
724 * runs before the type->write/read_memory implementation
726 type
->write_memory_imp
= target
->type
->write_memory
;
727 type
->write_memory
= target_write_memory_imp
;
729 type
->read_memory_imp
= target
->type
->read_memory
;
730 type
->read_memory
= target_read_memory_imp
;
732 type
->soft_reset_halt_imp
= target
->type
->soft_reset_halt
;
733 type
->soft_reset_halt
= target_soft_reset_halt_imp
;
735 type
->run_algorithm_imp
= target
->type
->run_algorithm
;
736 type
->run_algorithm
= target_run_algorithm_imp
;
738 /* Sanity-check MMU support ... stub in what we must, to help
739 * implement it in stages, but warn if we need to do so.
743 if (type
->write_phys_memory
== NULL
)
745 LOG_ERROR("type '%s' is missing write_phys_memory",
747 type
->write_phys_memory
= err_write_phys_memory
;
749 if (type
->read_phys_memory
== NULL
)
751 LOG_ERROR("type '%s' is missing read_phys_memory",
753 type
->read_phys_memory
= err_read_phys_memory
;
755 if (type
->virt2phys
== NULL
)
757 LOG_ERROR("type '%s' is missing virt2phys", type
->name
);
758 type
->virt2phys
= identity_virt2phys
;
763 /* Make sure no-MMU targets all behave the same: make no
764 * distinction between physical and virtual addresses, and
765 * ensure that virt2phys() is always an identity mapping.
767 if (type
->write_phys_memory
|| type
->read_phys_memory
770 LOG_WARNING("type '%s' has bad MMU hooks", type
->name
);
774 type
->write_phys_memory
= type
->write_memory
;
775 type
->read_phys_memory
= type
->read_memory
;
776 type
->virt2phys
= identity_virt2phys
;
781 int target_init(struct command_context
*cmd_ctx
)
783 struct target
*target
;
786 for (target
= all_targets
; target
; target
= target
->next
)
788 retval
= target_init_one(cmd_ctx
, target
);
789 if (ERROR_OK
!= retval
)
796 retval
= target_register_user_commands(cmd_ctx
);
797 if (ERROR_OK
!= retval
)
800 retval
= target_register_timer_callback(&handle_target
,
801 100, 1, cmd_ctx
->interp
);
802 if (ERROR_OK
!= retval
)
808 COMMAND_HANDLER(handle_target_init_command
)
811 return ERROR_COMMAND_SYNTAX_ERROR
;
813 static bool target_initialized
= false;
814 if (target_initialized
)
816 LOG_INFO("'target init' has already been called");
819 target_initialized
= true;
821 LOG_DEBUG("Initializing targets...");
822 return target_init(CMD_CTX
);
825 int target_register_event_callback(int (*callback
)(struct target
*target
, enum target_event event
, void *priv
), void *priv
)
827 struct target_event_callback
**callbacks_p
= &target_event_callbacks
;
829 if (callback
== NULL
)
831 return ERROR_INVALID_ARGUMENTS
;
836 while ((*callbacks_p
)->next
)
837 callbacks_p
= &((*callbacks_p
)->next
);
838 callbacks_p
= &((*callbacks_p
)->next
);
841 (*callbacks_p
) = malloc(sizeof(struct target_event_callback
));
842 (*callbacks_p
)->callback
= callback
;
843 (*callbacks_p
)->priv
= priv
;
844 (*callbacks_p
)->next
= NULL
;
849 int target_register_timer_callback(int (*callback
)(void *priv
), int time_ms
, int periodic
, void *priv
)
851 struct target_timer_callback
**callbacks_p
= &target_timer_callbacks
;
854 if (callback
== NULL
)
856 return ERROR_INVALID_ARGUMENTS
;
861 while ((*callbacks_p
)->next
)
862 callbacks_p
= &((*callbacks_p
)->next
);
863 callbacks_p
= &((*callbacks_p
)->next
);
866 (*callbacks_p
) = malloc(sizeof(struct target_timer_callback
));
867 (*callbacks_p
)->callback
= callback
;
868 (*callbacks_p
)->periodic
= periodic
;
869 (*callbacks_p
)->time_ms
= time_ms
;
871 gettimeofday(&now
, NULL
);
872 (*callbacks_p
)->when
.tv_usec
= now
.tv_usec
+ (time_ms
% 1000) * 1000;
873 time_ms
-= (time_ms
% 1000);
874 (*callbacks_p
)->when
.tv_sec
= now
.tv_sec
+ (time_ms
/ 1000);
875 if ((*callbacks_p
)->when
.tv_usec
> 1000000)
877 (*callbacks_p
)->when
.tv_usec
= (*callbacks_p
)->when
.tv_usec
- 1000000;
878 (*callbacks_p
)->when
.tv_sec
+= 1;
881 (*callbacks_p
)->priv
= priv
;
882 (*callbacks_p
)->next
= NULL
;
887 int target_unregister_event_callback(int (*callback
)(struct target
*target
, enum target_event event
, void *priv
), void *priv
)
889 struct target_event_callback
**p
= &target_event_callbacks
;
890 struct target_event_callback
*c
= target_event_callbacks
;
892 if (callback
== NULL
)
894 return ERROR_INVALID_ARGUMENTS
;
899 struct target_event_callback
*next
= c
->next
;
900 if ((c
->callback
== callback
) && (c
->priv
== priv
))
914 int target_unregister_timer_callback(int (*callback
)(void *priv
), void *priv
)
916 struct target_timer_callback
**p
= &target_timer_callbacks
;
917 struct target_timer_callback
*c
= target_timer_callbacks
;
919 if (callback
== NULL
)
921 return ERROR_INVALID_ARGUMENTS
;
926 struct target_timer_callback
*next
= c
->next
;
927 if ((c
->callback
== callback
) && (c
->priv
== priv
))
941 int target_call_event_callbacks(struct target
*target
, enum target_event event
)
943 struct target_event_callback
*callback
= target_event_callbacks
;
944 struct target_event_callback
*next_callback
;
946 if (event
== TARGET_EVENT_HALTED
)
948 /* execute early halted first */
949 target_call_event_callbacks(target
, TARGET_EVENT_GDB_HALT
);
952 LOG_DEBUG("target event %i (%s)",
954 Jim_Nvp_value2name_simple(nvp_target_event
, event
)->name
);
956 target_handle_event(target
, event
);
960 next_callback
= callback
->next
;
961 callback
->callback(target
, event
, callback
->priv
);
962 callback
= next_callback
;
968 static int target_timer_callback_periodic_restart(
969 struct target_timer_callback
*cb
, struct timeval
*now
)
971 int time_ms
= cb
->time_ms
;
972 cb
->when
.tv_usec
= now
->tv_usec
+ (time_ms
% 1000) * 1000;
973 time_ms
-= (time_ms
% 1000);
974 cb
->when
.tv_sec
= now
->tv_sec
+ time_ms
/ 1000;
975 if (cb
->when
.tv_usec
> 1000000)
977 cb
->when
.tv_usec
= cb
->when
.tv_usec
- 1000000;
978 cb
->when
.tv_sec
+= 1;
983 static int target_call_timer_callback(struct target_timer_callback
*cb
,
986 cb
->callback(cb
->priv
);
989 return target_timer_callback_periodic_restart(cb
, now
);
991 return target_unregister_timer_callback(cb
->callback
, cb
->priv
);
994 static int target_call_timer_callbacks_check_time(int checktime
)
999 gettimeofday(&now
, NULL
);
1001 struct target_timer_callback
*callback
= target_timer_callbacks
;
1004 // cleaning up may unregister and free this callback
1005 struct target_timer_callback
*next_callback
= callback
->next
;
1007 bool call_it
= callback
->callback
&&
1008 ((!checktime
&& callback
->periodic
) ||
1009 now
.tv_sec
> callback
->when
.tv_sec
||
1010 (now
.tv_sec
== callback
->when
.tv_sec
&&
1011 now
.tv_usec
>= callback
->when
.tv_usec
));
1015 int retval
= target_call_timer_callback(callback
, &now
);
1016 if (retval
!= ERROR_OK
)
1020 callback
= next_callback
;
1026 int target_call_timer_callbacks(void)
1028 return target_call_timer_callbacks_check_time(1);
1031 /* invoke periodic callbacks immediately */
1032 int target_call_timer_callbacks_now(void)
1034 return target_call_timer_callbacks_check_time(0);
1037 int target_alloc_working_area(struct target
*target
, uint32_t size
, struct working_area
**area
)
1039 struct working_area
*c
= target
->working_areas
;
1040 struct working_area
*new_wa
= NULL
;
1042 /* Reevaluate working area address based on MMU state*/
1043 if (target
->working_areas
== NULL
)
1048 retval
= target
->type
->mmu(target
, &enabled
);
1049 if (retval
!= ERROR_OK
)
1055 if (target
->working_area_phys_spec
) {
1056 LOG_DEBUG("MMU disabled, using physical "
1057 "address for working memory 0x%08x",
1058 (unsigned)target
->working_area_phys
);
1059 target
->working_area
= target
->working_area_phys
;
1061 LOG_ERROR("No working memory available. "
1062 "Specify -work-area-phys to target.");
1063 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
1066 if (target
->working_area_virt_spec
) {
1067 LOG_DEBUG("MMU enabled, using virtual "
1068 "address for working memory 0x%08x",
1069 (unsigned)target
->working_area_virt
);
1070 target
->working_area
= target
->working_area_virt
;
1072 LOG_ERROR("No working memory available. "
1073 "Specify -work-area-virt to target.");
1074 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
1079 /* only allocate multiples of 4 byte */
1082 LOG_ERROR("BUG: code tried to allocate unaligned number of bytes (0x%08x), padding", ((unsigned)(size
)));
1083 size
= (size
+ 3) & (~3);
1086 /* see if there's already a matching working area */
1089 if ((c
->free
) && (c
->size
== size
))
1097 /* if not, allocate a new one */
1100 struct working_area
**p
= &target
->working_areas
;
1101 uint32_t first_free
= target
->working_area
;
1102 uint32_t free_size
= target
->working_area_size
;
1104 c
= target
->working_areas
;
1107 first_free
+= c
->size
;
1108 free_size
-= c
->size
;
1113 if (free_size
< size
)
1115 LOG_WARNING("not enough working area available(requested %u, free %u)",
1116 (unsigned)(size
), (unsigned)(free_size
));
1117 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
1120 LOG_DEBUG("allocated new working area at address 0x%08x", (unsigned)first_free
);
1122 new_wa
= malloc(sizeof(struct working_area
));
1123 new_wa
->next
= NULL
;
1124 new_wa
->size
= size
;
1125 new_wa
->address
= first_free
;
1127 if (target
->backup_working_area
)
1130 new_wa
->backup
= malloc(new_wa
->size
);
1131 if ((retval
= target_read_memory(target
, new_wa
->address
, 4, new_wa
->size
/ 4, new_wa
->backup
)) != ERROR_OK
)
1133 free(new_wa
->backup
);
1140 new_wa
->backup
= NULL
;
1143 /* put new entry in list */
1147 /* mark as used, and return the new (reused) area */
1152 new_wa
->user
= area
;
1157 int target_free_working_area_restore(struct target
*target
, struct working_area
*area
, int restore
)
1162 if (restore
&& target
->backup_working_area
)
1165 if ((retval
= target_write_memory(target
, area
->address
, 4, area
->size
/ 4, area
->backup
)) != ERROR_OK
)
1171 /* mark user pointer invalid */
1178 int target_free_working_area(struct target
*target
, struct working_area
*area
)
1180 return target_free_working_area_restore(target
, area
, 1);
1183 /* free resources and restore memory, if restoring memory fails,
1184 * free up resources anyway
1186 void target_free_all_working_areas_restore(struct target
*target
, int restore
)
1188 struct working_area
*c
= target
->working_areas
;
1192 struct working_area
*next
= c
->next
;
1193 target_free_working_area_restore(target
, c
, restore
);
1203 target
->working_areas
= NULL
;
1206 void target_free_all_working_areas(struct target
*target
)
1208 target_free_all_working_areas_restore(target
, 1);
1211 int target_arch_state(struct target
*target
)
1216 LOG_USER("No target has been configured");
1220 LOG_USER("target state: %s", target_state_name( target
));
1222 if (target
->state
!= TARGET_HALTED
)
1225 retval
= target
->type
->arch_state(target
);
1229 /* Single aligned words are guaranteed to use 16 or 32 bit access
1230 * mode respectively, otherwise data is handled as quickly as
1233 int target_write_buffer(struct target
*target
, uint32_t address
, uint32_t size
, uint8_t *buffer
)
1236 LOG_DEBUG("writing buffer of %i byte at 0x%8.8x",
1237 (int)size
, (unsigned)address
);
1239 if (!target_was_examined(target
))
1241 LOG_ERROR("Target not examined yet");
1249 if ((address
+ size
- 1) < address
)
1251 /* GDB can request this when e.g. PC is 0xfffffffc*/
1252 LOG_ERROR("address + size wrapped(0x%08x, 0x%08x)",
1258 if (((address
% 2) == 0) && (size
== 2))
1260 return target_write_memory(target
, address
, 2, 1, buffer
);
1263 /* handle unaligned head bytes */
1266 uint32_t unaligned
= 4 - (address
% 4);
1268 if (unaligned
> size
)
1271 if ((retval
= target_write_memory(target
, address
, 1, unaligned
, buffer
)) != ERROR_OK
)
1274 buffer
+= unaligned
;
1275 address
+= unaligned
;
1279 /* handle aligned words */
1282 int aligned
= size
- (size
% 4);
1284 /* use bulk writes above a certain limit. This may have to be changed */
1287 if ((retval
= target
->type
->bulk_write_memory(target
, address
, aligned
/ 4, buffer
)) != ERROR_OK
)
1292 if ((retval
= target_write_memory(target
, address
, 4, aligned
/ 4, buffer
)) != ERROR_OK
)
1301 /* handle tail writes of less than 4 bytes */
1304 if ((retval
= target_write_memory(target
, address
, 1, size
, buffer
)) != ERROR_OK
)
1311 /* Single aligned words are guaranteed to use 16 or 32 bit access
1312 * mode respectively, otherwise data is handled as quickly as
1315 int target_read_buffer(struct target
*target
, uint32_t address
, uint32_t size
, uint8_t *buffer
)
1318 LOG_DEBUG("reading buffer of %i byte at 0x%8.8x",
1319 (int)size
, (unsigned)address
);
1321 if (!target_was_examined(target
))
1323 LOG_ERROR("Target not examined yet");
1331 if ((address
+ size
- 1) < address
)
1333 /* GDB can request this when e.g. PC is 0xfffffffc*/
1334 LOG_ERROR("address + size wrapped(0x%08" PRIx32
", 0x%08" PRIx32
")",
1340 if (((address
% 2) == 0) && (size
== 2))
1342 return target_read_memory(target
, address
, 2, 1, buffer
);
1345 /* handle unaligned head bytes */
1348 uint32_t unaligned
= 4 - (address
% 4);
1350 if (unaligned
> size
)
1353 if ((retval
= target_read_memory(target
, address
, 1, unaligned
, buffer
)) != ERROR_OK
)
1356 buffer
+= unaligned
;
1357 address
+= unaligned
;
1361 /* handle aligned words */
1364 int aligned
= size
- (size
% 4);
1366 if ((retval
= target_read_memory(target
, address
, 4, aligned
/ 4, buffer
)) != ERROR_OK
)
1374 /*prevent byte access when possible (avoid AHB access limitations in some cases)*/
1377 int aligned
= size
- (size
%2);
1378 retval
= target_read_memory(target
, address
, 2, aligned
/ 2, buffer
);
1379 if (retval
!= ERROR_OK
)
1386 /* handle tail writes of less than 4 bytes */
1389 if ((retval
= target_read_memory(target
, address
, 1, size
, buffer
)) != ERROR_OK
)
1396 int target_checksum_memory(struct target
*target
, uint32_t address
, uint32_t size
, uint32_t* crc
)
1401 uint32_t checksum
= 0;
1402 if (!target_was_examined(target
))
1404 LOG_ERROR("Target not examined yet");
1408 if ((retval
= target
->type
->checksum_memory(target
, address
,
1409 size
, &checksum
)) != ERROR_OK
)
1411 buffer
= malloc(size
);
1414 LOG_ERROR("error allocating buffer for section (%d bytes)", (int)size
);
1415 return ERROR_INVALID_ARGUMENTS
;
1417 retval
= target_read_buffer(target
, address
, size
, buffer
);
1418 if (retval
!= ERROR_OK
)
1424 /* convert to target endianess */
1425 for (i
= 0; i
< (size
/sizeof(uint32_t)); i
++)
1427 uint32_t target_data
;
1428 target_data
= target_buffer_get_u32(target
, &buffer
[i
*sizeof(uint32_t)]);
1429 target_buffer_set_u32(target
, &buffer
[i
*sizeof(uint32_t)], target_data
);
1432 retval
= image_calculate_checksum(buffer
, size
, &checksum
);
1441 int target_blank_check_memory(struct target
*target
, uint32_t address
, uint32_t size
, uint32_t* blank
)
1444 if (!target_was_examined(target
))
1446 LOG_ERROR("Target not examined yet");
1450 if (target
->type
->blank_check_memory
== 0)
1451 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
1453 retval
= target
->type
->blank_check_memory(target
, address
, size
, blank
);
1458 int target_read_u32(struct target
*target
, uint32_t address
, uint32_t *value
)
1460 uint8_t value_buf
[4];
1461 if (!target_was_examined(target
))
1463 LOG_ERROR("Target not examined yet");
1467 int retval
= target_read_memory(target
, address
, 4, 1, value_buf
);
1469 if (retval
== ERROR_OK
)
1471 *value
= target_buffer_get_u32(target
, value_buf
);
1472 LOG_DEBUG("address: 0x%8.8" PRIx32
", value: 0x%8.8" PRIx32
"",
1479 LOG_DEBUG("address: 0x%8.8" PRIx32
" failed",
1486 int target_read_u16(struct target
*target
, uint32_t address
, uint16_t *value
)
1488 uint8_t value_buf
[2];
1489 if (!target_was_examined(target
))
1491 LOG_ERROR("Target not examined yet");
1495 int retval
= target_read_memory(target
, address
, 2, 1, value_buf
);
1497 if (retval
== ERROR_OK
)
1499 *value
= target_buffer_get_u16(target
, value_buf
);
1500 LOG_DEBUG("address: 0x%8.8" PRIx32
", value: 0x%4.4x",
1507 LOG_DEBUG("address: 0x%8.8" PRIx32
" failed",
1514 int target_read_u8(struct target
*target
, uint32_t address
, uint8_t *value
)
1516 int retval
= target_read_memory(target
, address
, 1, 1, value
);
1517 if (!target_was_examined(target
))
1519 LOG_ERROR("Target not examined yet");
1523 if (retval
== ERROR_OK
)
1525 LOG_DEBUG("address: 0x%8.8" PRIx32
", value: 0x%2.2x",
1532 LOG_DEBUG("address: 0x%8.8" PRIx32
" failed",
1539 int target_write_u32(struct target
*target
, uint32_t address
, uint32_t value
)
1542 uint8_t value_buf
[4];
1543 if (!target_was_examined(target
))
1545 LOG_ERROR("Target not examined yet");
1549 LOG_DEBUG("address: 0x%8.8" PRIx32
", value: 0x%8.8" PRIx32
"",
1553 target_buffer_set_u32(target
, value_buf
, value
);
1554 if ((retval
= target_write_memory(target
, address
, 4, 1, value_buf
)) != ERROR_OK
)
1556 LOG_DEBUG("failed: %i", retval
);
1562 int target_write_u16(struct target
*target
, uint32_t address
, uint16_t value
)
1565 uint8_t value_buf
[2];
1566 if (!target_was_examined(target
))
1568 LOG_ERROR("Target not examined yet");
1572 LOG_DEBUG("address: 0x%8.8" PRIx32
", value: 0x%8.8x",
1576 target_buffer_set_u16(target
, value_buf
, value
);
1577 if ((retval
= target_write_memory(target
, address
, 2, 1, value_buf
)) != ERROR_OK
)
1579 LOG_DEBUG("failed: %i", retval
);
1585 int target_write_u8(struct target
*target
, uint32_t address
, uint8_t value
)
1588 if (!target_was_examined(target
))
1590 LOG_ERROR("Target not examined yet");
1594 LOG_DEBUG("address: 0x%8.8" PRIx32
", value: 0x%2.2x",
1597 if ((retval
= target_write_memory(target
, address
, 1, 1, &value
)) != ERROR_OK
)
1599 LOG_DEBUG("failed: %i", retval
);
1605 COMMAND_HANDLER(handle_targets_command
)
1607 struct target
*target
= all_targets
;
1611 target
= get_target(CMD_ARGV
[0]);
1612 if (target
== NULL
) {
1613 command_print(CMD_CTX
,"Target: %s is unknown, try one of:\n", CMD_ARGV
[0]);
1616 if (!target
->tap
->enabled
) {
1617 command_print(CMD_CTX
,"Target: TAP %s is disabled, "
1618 "can't be the current target\n",
1619 target
->tap
->dotted_name
);
1623 CMD_CTX
->current_target
= target
->target_number
;
1628 target
= all_targets
;
1629 command_print(CMD_CTX
, " TargetName Type Endian TapName State ");
1630 command_print(CMD_CTX
, "-- ------------------ ---------- ------ ------------------ ------------");
1636 if (target
->tap
->enabled
)
1637 state
= target_state_name( target
);
1639 state
= "tap-disabled";
1641 if (CMD_CTX
->current_target
== target
->target_number
)
1644 /* keep columns lined up to match the headers above */
1645 command_print(CMD_CTX
, "%2d%c %-18s %-10s %-6s %-18s %s",
1646 target
->target_number
,
1648 target_name(target
),
1649 target_type_name(target
),
1650 Jim_Nvp_value2name_simple(nvp_target_endian
,
1651 target
->endianness
)->name
,
1652 target
->tap
->dotted_name
,
1654 target
= target
->next
;
1660 /* every 300ms we check for reset & powerdropout and issue a "reset halt" if so. */
1662 static int powerDropout
;
1663 static int srstAsserted
;
1665 static int runPowerRestore
;
1666 static int runPowerDropout
;
1667 static int runSrstAsserted
;
1668 static int runSrstDeasserted
;
1670 static int sense_handler(void)
1672 static int prevSrstAsserted
= 0;
1673 static int prevPowerdropout
= 0;
1676 if ((retval
= jtag_power_dropout(&powerDropout
)) != ERROR_OK
)
1680 powerRestored
= prevPowerdropout
&& !powerDropout
;
1683 runPowerRestore
= 1;
1686 long long current
= timeval_ms();
1687 static long long lastPower
= 0;
1688 int waitMore
= lastPower
+ 2000 > current
;
1689 if (powerDropout
&& !waitMore
)
1691 runPowerDropout
= 1;
1692 lastPower
= current
;
1695 if ((retval
= jtag_srst_asserted(&srstAsserted
)) != ERROR_OK
)
1699 srstDeasserted
= prevSrstAsserted
&& !srstAsserted
;
1701 static long long lastSrst
= 0;
1702 waitMore
= lastSrst
+ 2000 > current
;
1703 if (srstDeasserted
&& !waitMore
)
1705 runSrstDeasserted
= 1;
1709 if (!prevSrstAsserted
&& srstAsserted
)
1711 runSrstAsserted
= 1;
1714 prevSrstAsserted
= srstAsserted
;
1715 prevPowerdropout
= powerDropout
;
1717 if (srstDeasserted
|| powerRestored
)
1719 /* Other than logging the event we can't do anything here.
1720 * Issuing a reset is a particularly bad idea as we might
1721 * be inside a reset already.
1728 static void target_call_event_callbacks_all(enum target_event e
) {
1729 struct target
*target
;
1730 target
= all_targets
;
1732 target_call_event_callbacks(target
, e
);
1733 target
= target
->next
;
1737 /* process target state changes */
1738 static int handle_target(void *priv
)
1740 Jim_Interp
*interp
= (Jim_Interp
*)priv
;
1741 int retval
= ERROR_OK
;
1743 /* we do not want to recurse here... */
1744 static int recursive
= 0;
1749 /* danger! running these procedures can trigger srst assertions and power dropouts.
1750 * We need to avoid an infinite loop/recursion here and we do that by
1751 * clearing the flags after running these events.
1753 int did_something
= 0;
1754 if (runSrstAsserted
)
1756 target_call_event_callbacks_all(TARGET_EVENT_GDB_HALT
);
1757 Jim_Eval(interp
, "srst_asserted");
1760 if (runSrstDeasserted
)
1762 Jim_Eval(interp
, "srst_deasserted");
1765 if (runPowerDropout
)
1767 target_call_event_callbacks_all(TARGET_EVENT_GDB_HALT
);
1768 Jim_Eval(interp
, "power_dropout");
1771 if (runPowerRestore
)
1773 Jim_Eval(interp
, "power_restore");
1779 /* clear detect flags */
1783 /* clear action flags */
1785 runSrstAsserted
= 0;
1786 runSrstDeasserted
= 0;
1787 runPowerRestore
= 0;
1788 runPowerDropout
= 0;
1793 /* Poll targets for state changes unless that's globally disabled.
1794 * Skip targets that are currently disabled.
1796 for (struct target
*target
= all_targets
;
1797 is_jtag_poll_safe() && target
;
1798 target
= target
->next
)
1800 if (!target
->tap
->enabled
)
1803 /* only poll target if we've got power and srst isn't asserted */
1804 if (!powerDropout
&& !srstAsserted
)
1806 /* polling may fail silently until the target has been examined */
1807 if ((retval
= target_poll(target
)) != ERROR_OK
)
1809 target_call_event_callbacks(target
, TARGET_EVENT_GDB_HALT
);
1818 COMMAND_HANDLER(handle_reg_command
)
1820 struct target
*target
;
1821 struct reg
*reg
= NULL
;
1827 target
= get_current_target(CMD_CTX
);
1829 /* list all available registers for the current target */
1832 struct reg_cache
*cache
= target
->reg_cache
;
1839 command_print(CMD_CTX
, "===== %s", cache
->name
);
1841 for (i
= 0, reg
= cache
->reg_list
;
1842 i
< cache
->num_regs
;
1843 i
++, reg
++, count
++)
1845 /* only print cached values if they are valid */
1847 value
= buf_to_str(reg
->value
,
1849 command_print(CMD_CTX
,
1850 "(%i) %s (/%" PRIu32
"): 0x%s%s",
1858 command_print(CMD_CTX
, "(%i) %s (/%" PRIu32
")",
1863 cache
= cache
->next
;
1869 /* access a single register by its ordinal number */
1870 if ((CMD_ARGV
[0][0] >= '0') && (CMD_ARGV
[0][0] <= '9'))
1873 COMMAND_PARSE_NUMBER(uint
, CMD_ARGV
[0], num
);
1875 struct reg_cache
*cache
= target
->reg_cache
;
1880 for (i
= 0; i
< cache
->num_regs
; i
++)
1884 reg
= &cache
->reg_list
[i
];
1890 cache
= cache
->next
;
1895 command_print(CMD_CTX
, "%i is out of bounds, the current target has only %i registers (0 - %i)", num
, count
, count
- 1);
1898 } else /* access a single register by its name */
1900 reg
= register_get_by_name(target
->reg_cache
, CMD_ARGV
[0], 1);
1904 command_print(CMD_CTX
, "register %s not found in current target", CMD_ARGV
[0]);
1909 /* display a register */
1910 if ((CMD_ARGC
== 1) || ((CMD_ARGC
== 2) && !((CMD_ARGV
[1][0] >= '0') && (CMD_ARGV
[1][0] <= '9'))))
1912 if ((CMD_ARGC
== 2) && (strcmp(CMD_ARGV
[1], "force") == 0))
1915 if (reg
->valid
== 0)
1917 reg
->type
->get(reg
);
1919 value
= buf_to_str(reg
->value
, reg
->size
, 16);
1920 command_print(CMD_CTX
, "%s (/%i): 0x%s", reg
->name
, (int)(reg
->size
), value
);
1925 /* set register value */
1928 uint8_t *buf
= malloc(DIV_ROUND_UP(reg
->size
, 8));
1929 str_to_buf(CMD_ARGV
[1], strlen(CMD_ARGV
[1]), buf
, reg
->size
, 0);
1931 reg
->type
->set(reg
, buf
);
1933 value
= buf_to_str(reg
->value
, reg
->size
, 16);
1934 command_print(CMD_CTX
, "%s (/%i): 0x%s", reg
->name
, (int)(reg
->size
), value
);
1942 command_print(CMD_CTX
, "usage: reg <#|name> [value]");
1947 COMMAND_HANDLER(handle_poll_command
)
1949 int retval
= ERROR_OK
;
1950 struct target
*target
= get_current_target(CMD_CTX
);
1954 command_print(CMD_CTX
, "background polling: %s",
1955 jtag_poll_get_enabled() ? "on" : "off");
1956 command_print(CMD_CTX
, "TAP: %s (%s)",
1957 target
->tap
->dotted_name
,
1958 target
->tap
->enabled
? "enabled" : "disabled");
1959 if (!target
->tap
->enabled
)
1961 if ((retval
= target_poll(target
)) != ERROR_OK
)
1963 if ((retval
= target_arch_state(target
)) != ERROR_OK
)
1966 else if (CMD_ARGC
== 1)
1969 COMMAND_PARSE_ON_OFF(CMD_ARGV
[0], enable
);
1970 jtag_poll_set_enabled(enable
);
1974 return ERROR_COMMAND_SYNTAX_ERROR
;
1980 COMMAND_HANDLER(handle_wait_halt_command
)
1983 return ERROR_COMMAND_SYNTAX_ERROR
;
1988 int retval
= parse_uint(CMD_ARGV
[0], &ms
);
1989 if (ERROR_OK
!= retval
)
1991 command_print(CMD_CTX
, "usage: %s [seconds]", CMD_NAME
);
1992 return ERROR_COMMAND_SYNTAX_ERROR
;
1994 // convert seconds (given) to milliseconds (needed)
1998 struct target
*target
= get_current_target(CMD_CTX
);
1999 return target_wait_state(target
, TARGET_HALTED
, ms
);
2002 /* wait for target state to change. The trick here is to have a low
2003 * latency for short waits and not to suck up all the CPU time
2006 * After 500ms, keep_alive() is invoked
2008 int target_wait_state(struct target
*target
, enum target_state state
, int ms
)
2011 long long then
= 0, cur
;
2016 if ((retval
= target_poll(target
)) != ERROR_OK
)
2018 if (target
->state
== state
)
2026 then
= timeval_ms();
2027 LOG_DEBUG("waiting for target %s...",
2028 Jim_Nvp_value2name_simple(nvp_target_state
,state
)->name
);
2036 if ((cur
-then
) > ms
)
2038 LOG_ERROR("timed out while waiting for target %s",
2039 Jim_Nvp_value2name_simple(nvp_target_state
,state
)->name
);
2047 COMMAND_HANDLER(handle_halt_command
)
2051 struct target
*target
= get_current_target(CMD_CTX
);
2052 int retval
= target_halt(target
);
2053 if (ERROR_OK
!= retval
)
2059 retval
= parse_uint(CMD_ARGV
[0], &wait
);
2060 if (ERROR_OK
!= retval
)
2061 return ERROR_COMMAND_SYNTAX_ERROR
;
2066 return CALL_COMMAND_HANDLER(handle_wait_halt_command
);
2069 COMMAND_HANDLER(handle_soft_reset_halt_command
)
2071 struct target
*target
= get_current_target(CMD_CTX
);
2073 LOG_USER("requesting target halt and executing a soft reset");
2075 target
->type
->soft_reset_halt(target
);
2080 COMMAND_HANDLER(handle_reset_command
)
2083 return ERROR_COMMAND_SYNTAX_ERROR
;
2085 enum target_reset_mode reset_mode
= RESET_RUN
;
2089 n
= Jim_Nvp_name2value_simple(nvp_reset_modes
, CMD_ARGV
[0]);
2090 if ((n
->name
== NULL
) || (n
->value
== RESET_UNKNOWN
)) {
2091 return ERROR_COMMAND_SYNTAX_ERROR
;
2093 reset_mode
= n
->value
;
2096 /* reset *all* targets */
2097 return target_process_reset(CMD_CTX
, reset_mode
);
2101 COMMAND_HANDLER(handle_resume_command
)
2105 return ERROR_COMMAND_SYNTAX_ERROR
;
2107 struct target
*target
= get_current_target(CMD_CTX
);
2108 target_handle_event(target
, TARGET_EVENT_OLD_pre_resume
);
2110 /* with no CMD_ARGV, resume from current pc, addr = 0,
2111 * with one arguments, addr = CMD_ARGV[0],
2112 * handle breakpoints, not debugging */
2116 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], addr
);
2120 return target_resume(target
, current
, addr
, 1, 0);
2123 COMMAND_HANDLER(handle_step_command
)
2126 return ERROR_COMMAND_SYNTAX_ERROR
;
2130 /* with no CMD_ARGV, step from current pc, addr = 0,
2131 * with one argument addr = CMD_ARGV[0],
2132 * handle breakpoints, debugging */
2137 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], addr
);
2141 struct target
*target
= get_current_target(CMD_CTX
);
2143 return target
->type
->step(target
, current_pc
, addr
, 1);
2146 static void handle_md_output(struct command_context
*cmd_ctx
,
2147 struct target
*target
, uint32_t address
, unsigned size
,
2148 unsigned count
, const uint8_t *buffer
)
2150 const unsigned line_bytecnt
= 32;
2151 unsigned line_modulo
= line_bytecnt
/ size
;
2153 char output
[line_bytecnt
* 4 + 1];
2154 unsigned output_len
= 0;
2156 const char *value_fmt
;
2158 case 4: value_fmt
= "%8.8x "; break;
2159 case 2: value_fmt
= "%4.2x "; break;
2160 case 1: value_fmt
= "%2.2x "; break;
2162 /* "can't happen", caller checked */
2163 LOG_ERROR("invalid memory read size: %u", size
);
2167 for (unsigned i
= 0; i
< count
; i
++)
2169 if (i
% line_modulo
== 0)
2171 output_len
+= snprintf(output
+ output_len
,
2172 sizeof(output
) - output_len
,
2174 (unsigned)(address
+ (i
*size
)));
2178 const uint8_t *value_ptr
= buffer
+ i
* size
;
2180 case 4: value
= target_buffer_get_u32(target
, value_ptr
); break;
2181 case 2: value
= target_buffer_get_u16(target
, value_ptr
); break;
2182 case 1: value
= *value_ptr
;
2184 output_len
+= snprintf(output
+ output_len
,
2185 sizeof(output
) - output_len
,
2188 if ((i
% line_modulo
== line_modulo
- 1) || (i
== count
- 1))
2190 command_print(cmd_ctx
, "%s", output
);
2196 COMMAND_HANDLER(handle_md_command
)
2199 return ERROR_COMMAND_SYNTAX_ERROR
;
2202 switch (CMD_NAME
[2]) {
2203 case 'w': size
= 4; break;
2204 case 'h': size
= 2; break;
2205 case 'b': size
= 1; break;
2206 default: return ERROR_COMMAND_SYNTAX_ERROR
;
2209 bool physical
=strcmp(CMD_ARGV
[0], "phys")==0;
2210 int (*fn
)(struct target
*target
,
2211 uint32_t address
, uint32_t size
, uint32_t count
, uint8_t *buffer
);
2216 fn
=target_read_phys_memory
;
2219 fn
=target_read_memory
;
2221 if ((CMD_ARGC
< 1) || (CMD_ARGC
> 2))
2223 return ERROR_COMMAND_SYNTAX_ERROR
;
2227 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], address
);
2231 COMMAND_PARSE_NUMBER(uint
, CMD_ARGV
[1], count
);
2233 uint8_t *buffer
= calloc(count
, size
);
2235 struct target
*target
= get_current_target(CMD_CTX
);
2236 int retval
= fn(target
, address
, size
, count
, buffer
);
2237 if (ERROR_OK
== retval
)
2238 handle_md_output(CMD_CTX
, target
, address
, size
, count
, buffer
);
2245 COMMAND_HANDLER(handle_mw_command
)
2249 return ERROR_COMMAND_SYNTAX_ERROR
;
2251 bool physical
=strcmp(CMD_ARGV
[0], "phys")==0;
2252 int (*fn
)(struct target
*target
,
2253 uint32_t address
, uint32_t size
, uint32_t count
, uint8_t *buffer
);
2258 fn
=target_write_phys_memory
;
2261 fn
=target_write_memory
;
2263 if ((CMD_ARGC
< 2) || (CMD_ARGC
> 3))
2264 return ERROR_COMMAND_SYNTAX_ERROR
;
2267 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], address
);
2270 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], value
);
2274 COMMAND_PARSE_NUMBER(uint
, CMD_ARGV
[2], count
);
2276 struct target
*target
= get_current_target(CMD_CTX
);
2278 uint8_t value_buf
[4];
2279 switch (CMD_NAME
[2])
2283 target_buffer_set_u32(target
, value_buf
, value
);
2287 target_buffer_set_u16(target
, value_buf
, value
);
2291 value_buf
[0] = value
;
2294 return ERROR_COMMAND_SYNTAX_ERROR
;
2296 for (unsigned i
= 0; i
< count
; i
++)
2298 int retval
= fn(target
,
2299 address
+ i
* wordsize
, wordsize
, 1, value_buf
);
2300 if (ERROR_OK
!= retval
)
2309 static COMMAND_HELPER(parse_load_image_command_CMD_ARGV
, struct image
*image
,
2310 uint32_t *min_address
, uint32_t *max_address
)
2312 if (CMD_ARGC
< 1 || CMD_ARGC
> 5)
2313 return ERROR_COMMAND_SYNTAX_ERROR
;
2315 /* a base address isn't always necessary,
2316 * default to 0x0 (i.e. don't relocate) */
2320 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], addr
);
2321 image
->base_address
= addr
;
2322 image
->base_address_set
= 1;
2325 image
->base_address_set
= 0;
2327 image
->start_address_set
= 0;
2331 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[3], *min_address
);
2335 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[4], *max_address
);
2336 // use size (given) to find max (required)
2337 *max_address
+= *min_address
;
2340 if (*min_address
> *max_address
)
2341 return ERROR_COMMAND_SYNTAX_ERROR
;
2346 COMMAND_HANDLER(handle_load_image_command
)
2350 uint32_t image_size
;
2351 uint32_t min_address
= 0;
2352 uint32_t max_address
= 0xffffffff;
2356 int retval
= CALL_COMMAND_HANDLER(parse_load_image_command_CMD_ARGV
,
2357 &image
, &min_address
, &max_address
);
2358 if (ERROR_OK
!= retval
)
2361 struct target
*target
= get_current_target(CMD_CTX
);
2363 struct duration bench
;
2364 duration_start(&bench
);
2366 if (image_open(&image
, CMD_ARGV
[0], (CMD_ARGC
>= 3) ? CMD_ARGV
[2] : NULL
) != ERROR_OK
)
2373 for (i
= 0; i
< image
.num_sections
; i
++)
2375 buffer
= malloc(image
.sections
[i
].size
);
2378 command_print(CMD_CTX
,
2379 "error allocating buffer for section (%d bytes)",
2380 (int)(image
.sections
[i
].size
));
2384 if ((retval
= image_read_section(&image
, i
, 0x0, image
.sections
[i
].size
, buffer
, &buf_cnt
)) != ERROR_OK
)
2390 uint32_t offset
= 0;
2391 uint32_t length
= buf_cnt
;
2393 /* DANGER!!! beware of unsigned comparision here!!! */
2395 if ((image
.sections
[i
].base_address
+ buf_cnt
>= min_address
)&&
2396 (image
.sections
[i
].base_address
< max_address
))
2398 if (image
.sections
[i
].base_address
< min_address
)
2400 /* clip addresses below */
2401 offset
+= min_address
-image
.sections
[i
].base_address
;
2405 if (image
.sections
[i
].base_address
+ buf_cnt
> max_address
)
2407 length
-= (image
.sections
[i
].base_address
+ buf_cnt
)-max_address
;
2410 if ((retval
= target_write_buffer(target
, image
.sections
[i
].base_address
+ offset
, length
, buffer
+ offset
)) != ERROR_OK
)
2415 image_size
+= length
;
2416 command_print(CMD_CTX
, "%u bytes written at address 0x%8.8" PRIx32
"",
2417 (unsigned int)length
,
2418 image
.sections
[i
].base_address
+ offset
);
2424 if ((ERROR_OK
== retval
) && (duration_measure(&bench
) == ERROR_OK
))
2426 command_print(CMD_CTX
, "downloaded %" PRIu32
" bytes "
2427 "in %fs (%0.3f kb/s)", image_size
,
2428 duration_elapsed(&bench
), duration_kbps(&bench
, image_size
));
2431 image_close(&image
);
2437 COMMAND_HANDLER(handle_dump_image_command
)
2439 struct fileio fileio
;
2441 uint8_t buffer
[560];
2445 struct target
*target
= get_current_target(CMD_CTX
);
2449 command_print(CMD_CTX
, "usage: dump_image <filename> <address> <size>");
2454 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], address
);
2456 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[2], size
);
2458 if (fileio_open(&fileio
, CMD_ARGV
[0], FILEIO_WRITE
, FILEIO_BINARY
) != ERROR_OK
)
2463 struct duration bench
;
2464 duration_start(&bench
);
2466 int retval
= ERROR_OK
;
2469 size_t size_written
;
2470 uint32_t this_run_size
= (size
> 560) ? 560 : size
;
2471 retval
= target_read_buffer(target
, address
, this_run_size
, buffer
);
2472 if (retval
!= ERROR_OK
)
2477 retval
= fileio_write(&fileio
, this_run_size
, buffer
, &size_written
);
2478 if (retval
!= ERROR_OK
)
2483 size
-= this_run_size
;
2484 address
+= this_run_size
;
2487 if ((retvaltemp
= fileio_close(&fileio
)) != ERROR_OK
)
2490 if ((ERROR_OK
== retval
) && (duration_measure(&bench
) == ERROR_OK
))
2492 command_print(CMD_CTX
,
2493 "dumped %ld bytes in %fs (%0.3f kb/s)", (long)fileio
.size
,
2494 duration_elapsed(&bench
), duration_kbps(&bench
, fileio
.size
));
2500 static COMMAND_HELPER(handle_verify_image_command_internal
, int verify
)
2504 uint32_t image_size
;
2507 uint32_t checksum
= 0;
2508 uint32_t mem_checksum
= 0;
2512 struct target
*target
= get_current_target(CMD_CTX
);
2516 return ERROR_COMMAND_SYNTAX_ERROR
;
2521 LOG_ERROR("no target selected");
2525 struct duration bench
;
2526 duration_start(&bench
);
2531 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], addr
);
2532 image
.base_address
= addr
;
2533 image
.base_address_set
= 1;
2537 image
.base_address_set
= 0;
2538 image
.base_address
= 0x0;
2541 image
.start_address_set
= 0;
2543 if ((retval
= image_open(&image
, CMD_ARGV
[0], (CMD_ARGC
== 3) ? CMD_ARGV
[2] : NULL
)) != ERROR_OK
)
2550 for (i
= 0; i
< image
.num_sections
; i
++)
2552 buffer
= malloc(image
.sections
[i
].size
);
2555 command_print(CMD_CTX
,
2556 "error allocating buffer for section (%d bytes)",
2557 (int)(image
.sections
[i
].size
));
2560 if ((retval
= image_read_section(&image
, i
, 0x0, image
.sections
[i
].size
, buffer
, &buf_cnt
)) != ERROR_OK
)
2568 /* calculate checksum of image */
2569 image_calculate_checksum(buffer
, buf_cnt
, &checksum
);
2571 retval
= target_checksum_memory(target
, image
.sections
[i
].base_address
, buf_cnt
, &mem_checksum
);
2572 if (retval
!= ERROR_OK
)
2578 if (checksum
!= mem_checksum
)
2580 /* failed crc checksum, fall back to a binary compare */
2583 command_print(CMD_CTX
, "checksum mismatch - attempting binary compare");
2585 data
= (uint8_t*)malloc(buf_cnt
);
2587 /* Can we use 32bit word accesses? */
2589 int count
= buf_cnt
;
2590 if ((count
% 4) == 0)
2595 retval
= target_read_memory(target
, image
.sections
[i
].base_address
, size
, count
, data
);
2596 if (retval
== ERROR_OK
)
2599 for (t
= 0; t
< buf_cnt
; t
++)
2601 if (data
[t
] != buffer
[t
])
2603 command_print(CMD_CTX
,
2604 "Verify operation failed address 0x%08x. Was 0x%02x instead of 0x%02x\n",
2605 (unsigned)(t
+ image
.sections
[i
].base_address
),
2610 retval
= ERROR_FAIL
;
2624 command_print(CMD_CTX
, "address 0x%08" PRIx32
" length 0x%08zx",
2625 image
.sections
[i
].base_address
,
2630 image_size
+= buf_cnt
;
2633 if ((ERROR_OK
== retval
) && (duration_measure(&bench
) == ERROR_OK
))
2635 command_print(CMD_CTX
, "verified %" PRIu32
" bytes "
2636 "in %fs (%0.3f kb/s)", image_size
,
2637 duration_elapsed(&bench
), duration_kbps(&bench
, image_size
));
2640 image_close(&image
);
2645 COMMAND_HANDLER(handle_verify_image_command
)
2647 return CALL_COMMAND_HANDLER(handle_verify_image_command_internal
, 1);
2650 COMMAND_HANDLER(handle_test_image_command
)
2652 return CALL_COMMAND_HANDLER(handle_verify_image_command_internal
, 0);
2655 static int handle_bp_command_list(struct command_context
*cmd_ctx
)
2657 struct target
*target
= get_current_target(cmd_ctx
);
2658 struct breakpoint
*breakpoint
= target
->breakpoints
;
2661 if (breakpoint
->type
== BKPT_SOFT
)
2663 char* buf
= buf_to_str(breakpoint
->orig_instr
,
2664 breakpoint
->length
, 16);
2665 command_print(cmd_ctx
, "0x%8.8" PRIx32
", 0x%x, %i, 0x%s",
2666 breakpoint
->address
,
2668 breakpoint
->set
, buf
);
2673 command_print(cmd_ctx
, "0x%8.8" PRIx32
", 0x%x, %i",
2674 breakpoint
->address
,
2675 breakpoint
->length
, breakpoint
->set
);
2678 breakpoint
= breakpoint
->next
;
2683 static int handle_bp_command_set(struct command_context
*cmd_ctx
,
2684 uint32_t addr
, uint32_t length
, int hw
)
2686 struct target
*target
= get_current_target(cmd_ctx
);
2687 int retval
= breakpoint_add(target
, addr
, length
, hw
);
2688 if (ERROR_OK
== retval
)
2689 command_print(cmd_ctx
, "breakpoint set at 0x%8.8" PRIx32
"", addr
);
2691 LOG_ERROR("Failure setting breakpoint");
2695 COMMAND_HANDLER(handle_bp_command
)
2698 return handle_bp_command_list(CMD_CTX
);
2700 if (CMD_ARGC
< 2 || CMD_ARGC
> 3)
2702 command_print(CMD_CTX
, "usage: bp <address> <length> ['hw']");
2703 return ERROR_COMMAND_SYNTAX_ERROR
;
2707 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], addr
);
2709 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], length
);
2714 if (strcmp(CMD_ARGV
[2], "hw") == 0)
2717 return ERROR_COMMAND_SYNTAX_ERROR
;
2720 return handle_bp_command_set(CMD_CTX
, addr
, length
, hw
);
2723 COMMAND_HANDLER(handle_rbp_command
)
2726 return ERROR_COMMAND_SYNTAX_ERROR
;
2729 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], addr
);
2731 struct target
*target
= get_current_target(CMD_CTX
);
2732 breakpoint_remove(target
, addr
);
2737 COMMAND_HANDLER(handle_wp_command
)
2739 struct target
*target
= get_current_target(CMD_CTX
);
2743 struct watchpoint
*watchpoint
= target
->watchpoints
;
2747 command_print(CMD_CTX
, "address: 0x%8.8" PRIx32
2748 ", len: 0x%8.8" PRIx32
2749 ", r/w/a: %i, value: 0x%8.8" PRIx32
2750 ", mask: 0x%8.8" PRIx32
,
2751 watchpoint
->address
,
2753 (int)watchpoint
->rw
,
2756 watchpoint
= watchpoint
->next
;
2761 enum watchpoint_rw type
= WPT_ACCESS
;
2763 uint32_t length
= 0;
2764 uint32_t data_value
= 0x0;
2765 uint32_t data_mask
= 0xffffffff;
2770 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[4], data_mask
);
2773 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[3], data_value
);
2776 switch (CMD_ARGV
[2][0])
2788 LOG_ERROR("invalid watchpoint mode ('%c')", CMD_ARGV
[2][0]);
2789 return ERROR_COMMAND_SYNTAX_ERROR
;
2793 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], length
);
2794 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], addr
);
2798 command_print(CMD_CTX
, "usage: wp [address length "
2799 "[(r|w|a) [value [mask]]]]");
2800 return ERROR_COMMAND_SYNTAX_ERROR
;
2803 int retval
= watchpoint_add(target
, addr
, length
, type
,
2804 data_value
, data_mask
);
2805 if (ERROR_OK
!= retval
)
2806 LOG_ERROR("Failure setting watchpoints");
2811 COMMAND_HANDLER(handle_rwp_command
)
2814 return ERROR_COMMAND_SYNTAX_ERROR
;
2817 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], addr
);
2819 struct target
*target
= get_current_target(CMD_CTX
);
2820 watchpoint_remove(target
, addr
);
2827 * Translate a virtual address to a physical address.
2829 * The low-level target implementation must have logged a detailed error
2830 * which is forwarded to telnet/GDB session.
2832 COMMAND_HANDLER(handle_virt2phys_command
)
2835 return ERROR_COMMAND_SYNTAX_ERROR
;
2838 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], va
);
2841 struct target
*target
= get_current_target(CMD_CTX
);
2842 int retval
= target
->type
->virt2phys(target
, va
, &pa
);
2843 if (retval
== ERROR_OK
)
2844 command_print(CMD_CTX
, "Physical address 0x%08" PRIx32
"", pa
);
2849 static void writeData(FILE *f
, const void *data
, size_t len
)
2851 size_t written
= fwrite(data
, 1, len
, f
);
2853 LOG_ERROR("failed to write %zu bytes: %s", len
, strerror(errno
));
2856 static void writeLong(FILE *f
, int l
)
2859 for (i
= 0; i
< 4; i
++)
2861 char c
= (l
>> (i
*8))&0xff;
2862 writeData(f
, &c
, 1);
2867 static void writeString(FILE *f
, char *s
)
2869 writeData(f
, s
, strlen(s
));
2872 /* Dump a gmon.out histogram file. */
2873 static void writeGmon(uint32_t *samples
, uint32_t sampleNum
, const char *filename
)
2876 FILE *f
= fopen(filename
, "w");
2879 writeString(f
, "gmon");
2880 writeLong(f
, 0x00000001); /* Version */
2881 writeLong(f
, 0); /* padding */
2882 writeLong(f
, 0); /* padding */
2883 writeLong(f
, 0); /* padding */
2885 uint8_t zero
= 0; /* GMON_TAG_TIME_HIST */
2886 writeData(f
, &zero
, 1);
2888 /* figure out bucket size */
2889 uint32_t min
= samples
[0];
2890 uint32_t max
= samples
[0];
2891 for (i
= 0; i
< sampleNum
; i
++)
2893 if (min
> samples
[i
])
2897 if (max
< samples
[i
])
2903 int addressSpace
= (max
-min
+ 1);
2905 static const uint32_t maxBuckets
= 256 * 1024; /* maximum buckets. */
2906 uint32_t length
= addressSpace
;
2907 if (length
> maxBuckets
)
2909 length
= maxBuckets
;
2911 int *buckets
= malloc(sizeof(int)*length
);
2912 if (buckets
== NULL
)
2917 memset(buckets
, 0, sizeof(int)*length
);
2918 for (i
= 0; i
< sampleNum
;i
++)
2920 uint32_t address
= samples
[i
];
2921 long long a
= address
-min
;
2922 long long b
= length
-1;
2923 long long c
= addressSpace
-1;
2924 int index
= (a
*b
)/c
; /* danger!!!! int32 overflows */
2928 /* append binary memory gmon.out &profile_hist_hdr ((char*)&profile_hist_hdr + sizeof(struct gmon_hist_hdr)) */
2929 writeLong(f
, min
); /* low_pc */
2930 writeLong(f
, max
); /* high_pc */
2931 writeLong(f
, length
); /* # of samples */
2932 writeLong(f
, 64000000); /* 64MHz */
2933 writeString(f
, "seconds");
2934 for (i
= 0; i
< (15-strlen("seconds")); i
++)
2935 writeData(f
, &zero
, 1);
2936 writeString(f
, "s");
2938 /*append binary memory gmon.out profile_hist_data (profile_hist_data + profile_hist_hdr.hist_size) */
2940 char *data
= malloc(2*length
);
2943 for (i
= 0; i
< length
;i
++)
2952 data
[i
*2 + 1]=(val
>> 8)&0xff;
2955 writeData(f
, data
, length
* 2);
2965 /* profiling samples the CPU PC as quickly as OpenOCD is able,
2966 * which will be used as a random sampling of PC */
2967 COMMAND_HANDLER(handle_profile_command
)
2969 struct target
*target
= get_current_target(CMD_CTX
);
2970 struct timeval timeout
, now
;
2972 gettimeofday(&timeout
, NULL
);
2975 return ERROR_COMMAND_SYNTAX_ERROR
;
2978 COMMAND_PARSE_NUMBER(uint
, CMD_ARGV
[0], offset
);
2980 timeval_add_time(&timeout
, offset
, 0);
2983 * @todo: Some cores let us sample the PC without the
2984 * annoying halt/resume step; for example, ARMv7 PCSR.
2985 * Provide a way to use that more efficient mechanism.
2988 command_print(CMD_CTX
, "Starting profiling. Halting and resuming the target as often as we can...");
2990 static const int maxSample
= 10000;
2991 uint32_t *samples
= malloc(sizeof(uint32_t)*maxSample
);
2992 if (samples
== NULL
)
2996 /* hopefully it is safe to cache! We want to stop/restart as quickly as possible. */
2997 struct reg
*reg
= register_get_by_name(target
->reg_cache
, "pc", 1);
3002 target_poll(target
);
3003 if (target
->state
== TARGET_HALTED
)
3005 uint32_t t
=*((uint32_t *)reg
->value
);
3006 samples
[numSamples
++]=t
;
3007 retval
= target_resume(target
, 1, 0, 0, 0); /* current pc, addr = 0, do not handle breakpoints, not debugging */
3008 target_poll(target
);
3009 alive_sleep(10); /* sleep 10ms, i.e. <100 samples/second. */
3010 } else if (target
->state
== TARGET_RUNNING
)
3012 /* We want to quickly sample the PC. */
3013 if ((retval
= target_halt(target
)) != ERROR_OK
)
3020 command_print(CMD_CTX
, "Target not halted or running");
3024 if (retval
!= ERROR_OK
)
3029 gettimeofday(&now
, NULL
);
3030 if ((numSamples
>= maxSample
) || ((now
.tv_sec
>= timeout
.tv_sec
) && (now
.tv_usec
>= timeout
.tv_usec
)))
3032 command_print(CMD_CTX
, "Profiling completed. %d samples.", numSamples
);
3033 if ((retval
= target_poll(target
)) != ERROR_OK
)
3038 if (target
->state
== TARGET_HALTED
)
3040 target_resume(target
, 1, 0, 0, 0); /* current pc, addr = 0, do not handle breakpoints, not debugging */
3042 if ((retval
= target_poll(target
)) != ERROR_OK
)
3047 writeGmon(samples
, numSamples
, CMD_ARGV
[1]);
3048 command_print(CMD_CTX
, "Wrote %s", CMD_ARGV
[1]);
3057 static int new_int_array_element(Jim_Interp
* interp
, const char *varname
, int idx
, uint32_t val
)
3060 Jim_Obj
*nameObjPtr
, *valObjPtr
;
3063 namebuf
= alloc_printf("%s(%d)", varname
, idx
);
3067 nameObjPtr
= Jim_NewStringObj(interp
, namebuf
, -1);
3068 valObjPtr
= Jim_NewIntObj(interp
, val
);
3069 if (!nameObjPtr
|| !valObjPtr
)
3075 Jim_IncrRefCount(nameObjPtr
);
3076 Jim_IncrRefCount(valObjPtr
);
3077 result
= Jim_SetVariable(interp
, nameObjPtr
, valObjPtr
);
3078 Jim_DecrRefCount(interp
, nameObjPtr
);
3079 Jim_DecrRefCount(interp
, valObjPtr
);
3081 /* printf("%s(%d) <= 0%08x\n", varname, idx, val); */
3085 static int jim_mem2array(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
3087 struct command_context
*context
;
3088 struct target
*target
;
3090 context
= Jim_GetAssocData(interp
, "context");
3091 if (context
== NULL
)
3093 LOG_ERROR("mem2array: no command context");
3096 target
= get_current_target(context
);
3099 LOG_ERROR("mem2array: no current target");
3103 return target_mem2array(interp
, target
, argc
-1, argv
+ 1);
3106 static int target_mem2array(Jim_Interp
*interp
, struct target
*target
, int argc
, Jim_Obj
*const *argv
)
3114 const char *varname
;
3118 /* argv[1] = name of array to receive the data
3119 * argv[2] = desired width
3120 * argv[3] = memory address
3121 * argv[4] = count of times to read
3124 Jim_WrongNumArgs(interp
, 1, argv
, "varname width addr nelems");
3127 varname
= Jim_GetString(argv
[0], &len
);
3128 /* given "foo" get space for worse case "foo(%d)" .. add 20 */
3130 e
= Jim_GetLong(interp
, argv
[1], &l
);
3136 e
= Jim_GetLong(interp
, argv
[2], &l
);
3141 e
= Jim_GetLong(interp
, argv
[3], &l
);
3157 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3158 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "Invalid width param, must be 8/16/32", NULL
);
3162 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3163 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "mem2array: zero width read?", NULL
);
3166 if ((addr
+ (len
* width
)) < addr
) {
3167 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3168 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "mem2array: addr + len - wraps to zero?", NULL
);
3171 /* absurd transfer size? */
3173 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3174 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "mem2array: absurd > 64K item request", NULL
);
3179 ((width
== 2) && ((addr
& 1) == 0)) ||
3180 ((width
== 4) && ((addr
& 3) == 0))) {
3184 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3185 sprintf(buf
, "mem2array address: 0x%08" PRIx32
" is not aligned for %" PRId32
" byte reads",
3188 Jim_AppendStrings(interp
, Jim_GetResult(interp
), buf
, NULL
);
3197 size_t buffersize
= 4096;
3198 uint8_t *buffer
= malloc(buffersize
);
3205 /* Slurp... in buffer size chunks */
3207 count
= len
; /* in objects.. */
3208 if (count
> (buffersize
/width
)) {
3209 count
= (buffersize
/width
);
3212 retval
= target_read_memory(target
, addr
, width
, count
, buffer
);
3213 if (retval
!= ERROR_OK
) {
3215 LOG_ERROR("mem2array: Read @ 0x%08x, w=%d, cnt=%d, failed",
3219 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3220 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "mem2array: cannot read memory", NULL
);
3224 v
= 0; /* shut up gcc */
3225 for (i
= 0 ;i
< count
;i
++, n
++) {
3228 v
= target_buffer_get_u32(target
, &buffer
[i
*width
]);
3231 v
= target_buffer_get_u16(target
, &buffer
[i
*width
]);
3234 v
= buffer
[i
] & 0x0ff;
3237 new_int_array_element(interp
, varname
, n
, v
);
3245 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3250 static int get_int_array_element(Jim_Interp
* interp
, const char *varname
, int idx
, uint32_t *val
)
3253 Jim_Obj
*nameObjPtr
, *valObjPtr
;
3257 namebuf
= alloc_printf("%s(%d)", varname
, idx
);
3261 nameObjPtr
= Jim_NewStringObj(interp
, namebuf
, -1);
3268 Jim_IncrRefCount(nameObjPtr
);
3269 valObjPtr
= Jim_GetVariable(interp
, nameObjPtr
, JIM_ERRMSG
);
3270 Jim_DecrRefCount(interp
, nameObjPtr
);
3272 if (valObjPtr
== NULL
)
3275 result
= Jim_GetLong(interp
, valObjPtr
, &l
);
3276 /* printf("%s(%d) => 0%08x\n", varname, idx, val); */
3281 static int jim_array2mem(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
3283 struct command_context
*context
;
3284 struct target
*target
;
3286 context
= Jim_GetAssocData(interp
, "context");
3287 if (context
== NULL
) {
3288 LOG_ERROR("array2mem: no command context");
3291 target
= get_current_target(context
);
3292 if (target
== NULL
) {
3293 LOG_ERROR("array2mem: no current target");
3297 return target_array2mem(interp
,target
, argc
-1, argv
+ 1);
3300 static int target_array2mem(Jim_Interp
*interp
, struct target
*target
,
3301 int argc
, Jim_Obj
*const *argv
)
3309 const char *varname
;
3313 /* argv[1] = name of array to get the data
3314 * argv[2] = desired width
3315 * argv[3] = memory address
3316 * argv[4] = count to write
3319 Jim_WrongNumArgs(interp
, 0, argv
, "varname width addr nelems");
3322 varname
= Jim_GetString(argv
[0], &len
);
3323 /* given "foo" get space for worse case "foo(%d)" .. add 20 */
3325 e
= Jim_GetLong(interp
, argv
[1], &l
);
3331 e
= Jim_GetLong(interp
, argv
[2], &l
);
3336 e
= Jim_GetLong(interp
, argv
[3], &l
);
3352 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3353 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "Invalid width param, must be 8/16/32", NULL
);
3357 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3358 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "array2mem: zero width read?", NULL
);
3361 if ((addr
+ (len
* width
)) < addr
) {
3362 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3363 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "array2mem: addr + len - wraps to zero?", NULL
);
3366 /* absurd transfer size? */
3368 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3369 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "array2mem: absurd > 64K item request", NULL
);
3374 ((width
== 2) && ((addr
& 1) == 0)) ||
3375 ((width
== 4) && ((addr
& 3) == 0))) {
3379 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3380 sprintf(buf
, "array2mem address: 0x%08x is not aligned for %d byte reads",
3383 Jim_AppendStrings(interp
, Jim_GetResult(interp
), buf
, NULL
);
3394 size_t buffersize
= 4096;
3395 uint8_t *buffer
= malloc(buffersize
);
3400 /* Slurp... in buffer size chunks */
3402 count
= len
; /* in objects.. */
3403 if (count
> (buffersize
/width
)) {
3404 count
= (buffersize
/width
);
3407 v
= 0; /* shut up gcc */
3408 for (i
= 0 ;i
< count
;i
++, n
++) {
3409 get_int_array_element(interp
, varname
, n
, &v
);
3412 target_buffer_set_u32(target
, &buffer
[i
*width
], v
);
3415 target_buffer_set_u16(target
, &buffer
[i
*width
], v
);
3418 buffer
[i
] = v
& 0x0ff;
3424 retval
= target_write_memory(target
, addr
, width
, count
, buffer
);
3425 if (retval
!= ERROR_OK
) {
3427 LOG_ERROR("array2mem: Write @ 0x%08x, w=%d, cnt=%d, failed",
3431 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3432 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "array2mem: cannot read memory", NULL
);
3440 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3445 void target_all_handle_event(enum target_event e
)
3447 struct target
*target
;
3449 LOG_DEBUG("**all*targets: event: %d, %s",
3451 Jim_Nvp_value2name_simple(nvp_target_event
, e
)->name
);
3453 target
= all_targets
;
3455 target_handle_event(target
, e
);
3456 target
= target
->next
;
3461 /* FIX? should we propagate errors here rather than printing them
3464 void target_handle_event(struct target
*target
, enum target_event e
)
3466 struct target_event_action
*teap
;
3468 for (teap
= target
->event_action
; teap
!= NULL
; teap
= teap
->next
) {
3469 if (teap
->event
== e
) {
3470 LOG_DEBUG("target: (%d) %s (%s) event: %d (%s) action: %s",
3471 target
->target_number
,
3472 target_name(target
),
3473 target_type_name(target
),
3475 Jim_Nvp_value2name_simple(nvp_target_event
, e
)->name
,
3476 Jim_GetString(teap
->body
, NULL
));
3477 if (Jim_EvalObj(teap
->interp
, teap
->body
) != JIM_OK
)
3479 Jim_PrintErrorMessage(teap
->interp
);
3486 * Returns true only if the target has a handler for the specified event.
3488 bool target_has_event_action(struct target
*target
, enum target_event event
)
3490 struct target_event_action
*teap
;
3492 for (teap
= target
->event_action
; teap
!= NULL
; teap
= teap
->next
) {
3493 if (teap
->event
== event
)
3499 enum target_cfg_param
{
3502 TCFG_WORK_AREA_VIRT
,
3503 TCFG_WORK_AREA_PHYS
,
3504 TCFG_WORK_AREA_SIZE
,
3505 TCFG_WORK_AREA_BACKUP
,
3508 TCFG_CHAIN_POSITION
,
3511 static Jim_Nvp nvp_config_opts
[] = {
3512 { .name
= "-type", .value
= TCFG_TYPE
},
3513 { .name
= "-event", .value
= TCFG_EVENT
},
3514 { .name
= "-work-area-virt", .value
= TCFG_WORK_AREA_VIRT
},
3515 { .name
= "-work-area-phys", .value
= TCFG_WORK_AREA_PHYS
},
3516 { .name
= "-work-area-size", .value
= TCFG_WORK_AREA_SIZE
},
3517 { .name
= "-work-area-backup", .value
= TCFG_WORK_AREA_BACKUP
},
3518 { .name
= "-endian" , .value
= TCFG_ENDIAN
},
3519 { .name
= "-variant", .value
= TCFG_VARIANT
},
3520 { .name
= "-chain-position", .value
= TCFG_CHAIN_POSITION
},
3522 { .name
= NULL
, .value
= -1 }
3525 static int target_configure(Jim_GetOptInfo
*goi
, struct target
*target
)
3533 /* parse config or cget options ... */
3534 while (goi
->argc
> 0) {
3535 Jim_SetEmptyResult(goi
->interp
);
3536 /* Jim_GetOpt_Debug(goi); */
3538 if (target
->type
->target_jim_configure
) {
3539 /* target defines a configure function */
3540 /* target gets first dibs on parameters */
3541 e
= (*(target
->type
->target_jim_configure
))(target
, goi
);
3550 /* otherwise we 'continue' below */
3552 e
= Jim_GetOpt_Nvp(goi
, nvp_config_opts
, &n
);
3554 Jim_GetOpt_NvpUnknown(goi
, nvp_config_opts
, 0);
3560 if (goi
->isconfigure
) {
3561 Jim_SetResult_sprintf(goi
->interp
,
3562 "not settable: %s", n
->name
);
3566 if (goi
->argc
!= 0) {
3567 Jim_WrongNumArgs(goi
->interp
,
3568 goi
->argc
, goi
->argv
,
3573 Jim_SetResultString(goi
->interp
,
3574 target_type_name(target
), -1);
3578 if (goi
->argc
== 0) {
3579 Jim_WrongNumArgs(goi
->interp
, goi
->argc
, goi
->argv
, "-event ?event-name? ...");
3583 e
= Jim_GetOpt_Nvp(goi
, nvp_target_event
, &n
);
3585 Jim_GetOpt_NvpUnknown(goi
, nvp_target_event
, 1);
3589 if (goi
->isconfigure
) {
3590 if (goi
->argc
!= 1) {
3591 Jim_WrongNumArgs(goi
->interp
, goi
->argc
, goi
->argv
, "-event ?event-name? ?EVENT-BODY?");
3595 if (goi
->argc
!= 0) {
3596 Jim_WrongNumArgs(goi
->interp
, goi
->argc
, goi
->argv
, "-event ?event-name?");
3602 struct target_event_action
*teap
;
3604 teap
= target
->event_action
;
3605 /* replace existing? */
3607 if (teap
->event
== (enum target_event
)n
->value
) {
3613 if (goi
->isconfigure
) {
3614 bool replace
= true;
3617 teap
= calloc(1, sizeof(*teap
));
3620 teap
->event
= n
->value
;
3621 teap
->interp
= goi
->interp
;
3622 Jim_GetOpt_Obj(goi
, &o
);
3624 Jim_DecrRefCount(teap
->interp
, teap
->body
);
3626 teap
->body
= Jim_DuplicateObj(goi
->interp
, o
);
3629 * Tcl/TK - "tk events" have a nice feature.
3630 * See the "BIND" command.
3631 * We should support that here.
3632 * You can specify %X and %Y in the event code.
3633 * The idea is: %T - target name.
3634 * The idea is: %N - target number
3635 * The idea is: %E - event name.
3637 Jim_IncrRefCount(teap
->body
);
3641 /* add to head of event list */
3642 teap
->next
= target
->event_action
;
3643 target
->event_action
= teap
;
3645 Jim_SetEmptyResult(goi
->interp
);
3649 Jim_SetEmptyResult(goi
->interp
);
3651 Jim_SetResult(goi
->interp
, Jim_DuplicateObj(goi
->interp
, teap
->body
));
3658 case TCFG_WORK_AREA_VIRT
:
3659 if (goi
->isconfigure
) {
3660 target_free_all_working_areas(target
);
3661 e
= Jim_GetOpt_Wide(goi
, &w
);
3665 target
->working_area_virt
= w
;
3666 target
->working_area_virt_spec
= true;
3668 if (goi
->argc
!= 0) {
3672 Jim_SetResult(goi
->interp
, Jim_NewIntObj(goi
->interp
, target
->working_area_virt
));
3676 case TCFG_WORK_AREA_PHYS
:
3677 if (goi
->isconfigure
) {
3678 target_free_all_working_areas(target
);
3679 e
= Jim_GetOpt_Wide(goi
, &w
);
3683 target
->working_area_phys
= w
;
3684 target
->working_area_phys_spec
= true;
3686 if (goi
->argc
!= 0) {
3690 Jim_SetResult(goi
->interp
, Jim_NewIntObj(goi
->interp
, target
->working_area_phys
));
3694 case TCFG_WORK_AREA_SIZE
:
3695 if (goi
->isconfigure
) {
3696 target_free_all_working_areas(target
);
3697 e
= Jim_GetOpt_Wide(goi
, &w
);
3701 target
->working_area_size
= w
;
3703 if (goi
->argc
!= 0) {
3707 Jim_SetResult(goi
->interp
, Jim_NewIntObj(goi
->interp
, target
->working_area_size
));
3711 case TCFG_WORK_AREA_BACKUP
:
3712 if (goi
->isconfigure
) {
3713 target_free_all_working_areas(target
);
3714 e
= Jim_GetOpt_Wide(goi
, &w
);
3718 /* make this exactly 1 or 0 */
3719 target
->backup_working_area
= (!!w
);
3721 if (goi
->argc
!= 0) {
3725 Jim_SetResult(goi
->interp
, Jim_NewIntObj(goi
->interp
, target
->backup_working_area
));
3726 /* loop for more e*/
3730 if (goi
->isconfigure
) {
3731 e
= Jim_GetOpt_Nvp(goi
, nvp_target_endian
, &n
);
3733 Jim_GetOpt_NvpUnknown(goi
, nvp_target_endian
, 1);
3736 target
->endianness
= n
->value
;
3738 if (goi
->argc
!= 0) {
3742 n
= Jim_Nvp_value2name_simple(nvp_target_endian
, target
->endianness
);
3743 if (n
->name
== NULL
) {
3744 target
->endianness
= TARGET_LITTLE_ENDIAN
;
3745 n
= Jim_Nvp_value2name_simple(nvp_target_endian
, target
->endianness
);
3747 Jim_SetResultString(goi
->interp
, n
->name
, -1);
3752 if (goi
->isconfigure
) {
3753 if (goi
->argc
< 1) {
3754 Jim_SetResult_sprintf(goi
->interp
,
3759 if (target
->variant
) {
3760 free((void *)(target
->variant
));
3762 e
= Jim_GetOpt_String(goi
, &cp
, NULL
);
3763 target
->variant
= strdup(cp
);
3765 if (goi
->argc
!= 0) {
3769 Jim_SetResultString(goi
->interp
, target
->variant
,-1);
3772 case TCFG_CHAIN_POSITION
:
3773 if (goi
->isconfigure
) {
3775 struct jtag_tap
*tap
;
3776 target_free_all_working_areas(target
);
3777 e
= Jim_GetOpt_Obj(goi
, &o
);
3781 tap
= jtag_tap_by_jim_obj(goi
->interp
, o
);
3785 /* make this exactly 1 or 0 */
3788 if (goi
->argc
!= 0) {
3792 Jim_SetResultString(goi
->interp
, target
->tap
->dotted_name
, -1);
3793 /* loop for more e*/
3796 } /* while (goi->argc) */
3799 /* done - we return */
3804 jim_target_configure(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
3808 Jim_GetOpt_Setup(&goi
, interp
, argc
- 1, argv
+ 1);
3809 goi
.isconfigure
= !strcmp(Jim_GetString(argv
[0], NULL
), "configure");
3810 int need_args
= 1 + goi
.isconfigure
;
3811 if (goi
.argc
< need_args
)
3813 Jim_WrongNumArgs(goi
.interp
, goi
.argc
, goi
.argv
,
3815 ? "missing: -option VALUE ..."
3816 : "missing: -option ...");
3819 struct target
*target
= Jim_CmdPrivData(goi
.interp
);
3820 return target_configure(&goi
, target
);
3823 static int jim_target_mw(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
3825 const char *cmd_name
= Jim_GetString(argv
[0], NULL
);
3828 Jim_GetOpt_Setup(&goi
, interp
, argc
- 1, argv
+ 1);
3830 if (goi
.argc
!= 2 && goi
.argc
!= 3)
3832 Jim_SetResult_sprintf(goi
.interp
,
3833 "usage: %s <address> <data> [<count>]", cmd_name
);
3838 int e
= Jim_GetOpt_Wide(&goi
, &a
);
3843 e
= Jim_GetOpt_Wide(&goi
, &b
);
3850 e
= Jim_GetOpt_Wide(&goi
, &c
);
3855 struct target
*target
= Jim_CmdPrivData(goi
.interp
);
3856 uint8_t target_buf
[32];
3857 if (strcasecmp(cmd_name
, "mww") == 0) {
3858 target_buffer_set_u32(target
, target_buf
, b
);
3861 else if (strcasecmp(cmd_name
, "mwh") == 0) {
3862 target_buffer_set_u16(target
, target_buf
, b
);
3865 else if (strcasecmp(cmd_name
, "mwb") == 0) {
3866 target_buffer_set_u8(target
, target_buf
, b
);
3869 LOG_ERROR("command '%s' unknown: ", cmd_name
);
3873 for (jim_wide x
= 0; x
< c
; x
++)
3875 e
= target_write_memory(target
, a
, b
, 1, target_buf
);
3878 Jim_SetResult_sprintf(interp
,
3879 "Error writing @ 0x%08x: %d\n", (int)(a
), e
);
3888 static int jim_target_md(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
3890 const char *cmd_name
= Jim_GetString(argv
[0], NULL
);
3893 Jim_GetOpt_Setup(&goi
, interp
, argc
- 1, argv
+ 1);
3895 if ((goi
.argc
== 2) || (goi
.argc
== 3))
3897 Jim_SetResult_sprintf(goi
.interp
,
3898 "usage: %s <address> [<count>]", cmd_name
);
3903 int e
= Jim_GetOpt_Wide(&goi
, &a
);
3909 e
= Jim_GetOpt_Wide(&goi
, &c
);
3916 jim_wide b
= 1; /* shut up gcc */
3917 if (strcasecmp(cmd_name
, "mdw") == 0)
3919 else if (strcasecmp(cmd_name
, "mdh") == 0)
3921 else if (strcasecmp(cmd_name
, "mdb") == 0)
3924 LOG_ERROR("command '%s' unknown: ", cmd_name
);
3928 /* convert count to "bytes" */
3931 struct target
*target
= Jim_CmdPrivData(goi
.interp
);
3932 uint8_t target_buf
[32];
3939 e
= target_read_memory(target
, a
, b
, y
/ b
, target_buf
);
3940 if (e
!= ERROR_OK
) {
3941 Jim_SetResult_sprintf(interp
, "error reading target @ 0x%08lx", (int)(a
));
3945 Jim_fprintf(interp
, interp
->cookie_stdout
, "0x%08x ", (int)(a
));
3948 for (x
= 0; x
< 16 && x
< y
; x
+= 4)
3950 z
= target_buffer_get_u32(target
, &(target_buf
[ x
* 4 ]));
3951 Jim_fprintf(interp
, interp
->cookie_stdout
, "%08x ", (int)(z
));
3953 for (; (x
< 16) ; x
+= 4) {
3954 Jim_fprintf(interp
, interp
->cookie_stdout
, " ");
3958 for (x
= 0; x
< 16 && x
< y
; x
+= 2)
3960 z
= target_buffer_get_u16(target
, &(target_buf
[ x
* 2 ]));
3961 Jim_fprintf(interp
, interp
->cookie_stdout
, "%04x ", (int)(z
));
3963 for (; (x
< 16) ; x
+= 2) {
3964 Jim_fprintf(interp
, interp
->cookie_stdout
, " ");
3969 for (x
= 0 ; (x
< 16) && (x
< y
) ; x
+= 1) {
3970 z
= target_buffer_get_u8(target
, &(target_buf
[ x
* 4 ]));
3971 Jim_fprintf(interp
, interp
->cookie_stdout
, "%02x ", (int)(z
));
3973 for (; (x
< 16) ; x
+= 1) {
3974 Jim_fprintf(interp
, interp
->cookie_stdout
, " ");
3978 /* ascii-ify the bytes */
3979 for (x
= 0 ; x
< y
; x
++) {
3980 if ((target_buf
[x
] >= 0x20) &&
3981 (target_buf
[x
] <= 0x7e)) {
3985 target_buf
[x
] = '.';
3990 target_buf
[x
] = ' ';
3995 /* print - with a newline */
3996 Jim_fprintf(interp
, interp
->cookie_stdout
, "%s\n", target_buf
);
4004 static int jim_target_mem2array(Jim_Interp
*interp
,
4005 int argc
, Jim_Obj
*const *argv
)
4007 struct target
*target
= Jim_CmdPrivData(interp
);
4008 return target_mem2array(interp
, target
, argc
- 1, argv
+ 1);
4011 static int jim_target_array2mem(Jim_Interp
*interp
,
4012 int argc
, Jim_Obj
*const *argv
)
4014 struct target
*target
= Jim_CmdPrivData(interp
);
4015 return target_array2mem(interp
, target
, argc
- 1, argv
+ 1);
4018 static int jim_target_tap_disabled(Jim_Interp
*interp
)
4020 Jim_SetResult_sprintf(interp
, "[TAP is disabled]");
4024 static int jim_target_examine(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4028 Jim_WrongNumArgs(interp
, 1, argv
, "[no parameters]");
4031 struct target
*target
= Jim_CmdPrivData(interp
);
4032 if (!target
->tap
->enabled
)
4033 return jim_target_tap_disabled(interp
);
4035 int e
= target
->type
->examine(target
);
4038 Jim_SetResult_sprintf(interp
, "examine-fails: %d", e
);
4044 static int jim_target_poll(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4048 Jim_WrongNumArgs(interp
, 1, argv
, "[no parameters]");
4051 struct target
*target
= Jim_CmdPrivData(interp
);
4052 if (!target
->tap
->enabled
)
4053 return jim_target_tap_disabled(interp
);
4056 if (!(target_was_examined(target
))) {
4057 e
= ERROR_TARGET_NOT_EXAMINED
;
4059 e
= target
->type
->poll(target
);
4063 Jim_SetResult_sprintf(interp
, "poll-fails: %d", e
);
4069 static int jim_target_reset(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4072 Jim_GetOpt_Setup(&goi
, interp
, argc
- 1, argv
+ 1);
4076 Jim_WrongNumArgs(interp
, 0, argv
,
4077 "([tT]|[fF]|assert|deassert) BOOL");
4082 int e
= Jim_GetOpt_Nvp(&goi
, nvp_assert
, &n
);
4085 Jim_GetOpt_NvpUnknown(&goi
, nvp_assert
, 1);
4088 /* the halt or not param */
4090 e
= Jim_GetOpt_Wide(&goi
, &a
);
4094 struct target
*target
= Jim_CmdPrivData(goi
.interp
);
4095 if (!target
->tap
->enabled
)
4096 return jim_target_tap_disabled(interp
);
4097 if (!(target_was_examined(target
)))
4099 LOG_ERROR("Target not examined yet");
4100 return ERROR_TARGET_NOT_EXAMINED
;
4102 if (!target
->type
->assert_reset
|| !target
->type
->deassert_reset
)
4104 Jim_SetResult_sprintf(interp
,
4105 "No target-specific reset for %s",
4106 target_name(target
));
4109 /* determine if we should halt or not. */
4110 target
->reset_halt
= !!a
;
4111 /* When this happens - all workareas are invalid. */
4112 target_free_all_working_areas_restore(target
, 0);
4115 if (n
->value
== NVP_ASSERT
) {
4116 e
= target
->type
->assert_reset(target
);
4118 e
= target
->type
->deassert_reset(target
);
4120 return (e
== ERROR_OK
) ? JIM_OK
: JIM_ERR
;
4123 static int jim_target_halt(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4126 Jim_WrongNumArgs(interp
, 1, argv
, "[no parameters]");
4129 struct target
*target
= Jim_CmdPrivData(interp
);
4130 if (!target
->tap
->enabled
)
4131 return jim_target_tap_disabled(interp
);
4132 int e
= target
->type
->halt(target
);
4133 return (e
== ERROR_OK
) ? JIM_OK
: JIM_ERR
;
4136 static int jim_target_wait_state(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4139 Jim_GetOpt_Setup(&goi
, interp
, argc
- 1, argv
+ 1);
4141 /* params: <name> statename timeoutmsecs */
4144 const char *cmd_name
= Jim_GetString(argv
[0], NULL
);
4145 Jim_SetResult_sprintf(goi
.interp
,
4146 "%s <state_name> <timeout_in_msec>", cmd_name
);
4151 int e
= Jim_GetOpt_Nvp(&goi
, nvp_target_state
, &n
);
4153 Jim_GetOpt_NvpUnknown(&goi
, nvp_target_state
,1);
4157 e
= Jim_GetOpt_Wide(&goi
, &a
);
4161 struct target
*target
= Jim_CmdPrivData(interp
);
4162 if (!target
->tap
->enabled
)
4163 return jim_target_tap_disabled(interp
);
4165 e
= target_wait_state(target
, n
->value
, a
);
4168 Jim_SetResult_sprintf(goi
.interp
,
4169 "target: %s wait %s fails (%d) %s",
4170 target_name(target
), n
->name
,
4171 e
, target_strerror_safe(e
));
4176 /* List for human, Events defined for this target.
4177 * scripts/programs should use 'name cget -event NAME'
4179 static int jim_target_event_list(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4181 struct command_context
*cmd_ctx
= Jim_GetAssocData(interp
, "context");
4182 struct target
*target
= Jim_CmdPrivData(interp
);
4183 struct target_event_action
*teap
= target
->event_action
;
4184 command_print(cmd_ctx
, "Event actions for target (%d) %s\n",
4185 target
->target_number
,
4186 target_name(target
));
4187 command_print(cmd_ctx
, "%-25s | Body", "Event");
4188 command_print(cmd_ctx
, "------------------------- | "
4189 "----------------------------------------");
4192 Jim_Nvp
*opt
= Jim_Nvp_value2name_simple(nvp_target_event
, teap
->event
);
4193 command_print(cmd_ctx
, "%-25s | %s",
4194 opt
->name
, Jim_GetString(teap
->body
, NULL
));
4197 command_print(cmd_ctx
, "***END***");
4200 static int jim_target_current_state(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4204 Jim_WrongNumArgs(interp
, 1, argv
, "[no parameters]");
4207 struct target
*target
= Jim_CmdPrivData(interp
);
4208 Jim_SetResultString(interp
, target_state_name(target
), -1);
4211 static int jim_target_invoke_event(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4214 Jim_GetOpt_Setup(&goi
, interp
, argc
- 1, argv
+ 1);
4217 const char *cmd_name
= Jim_GetString(argv
[0], NULL
);
4218 Jim_SetResult_sprintf(goi
.interp
, "%s <eventname>", cmd_name
);
4222 int e
= Jim_GetOpt_Nvp(&goi
, nvp_target_event
, &n
);
4225 Jim_GetOpt_NvpUnknown(&goi
, nvp_target_event
, 1);
4228 struct target
*target
= Jim_CmdPrivData(interp
);
4229 target_handle_event(target
, n
->value
);
4233 static const struct command_registration target_instance_command_handlers
[] = {
4235 .name
= "configure",
4236 .mode
= COMMAND_CONFIG
,
4237 .jim_handler
= jim_target_configure
,
4238 .help
= "configure a new target for use",
4239 .usage
= "[target_attribute ...]",
4243 .mode
= COMMAND_ANY
,
4244 .jim_handler
= jim_target_configure
,
4245 .help
= "returns the specified target attribute",
4246 .usage
= "target_attribute",
4250 .mode
= COMMAND_EXEC
,
4251 .jim_handler
= jim_target_mw
,
4252 .help
= "Write 32-bit word(s) to target memory",
4253 .usage
= "address data [count]",
4257 .mode
= COMMAND_EXEC
,
4258 .jim_handler
= jim_target_mw
,
4259 .help
= "Write 16-bit half-word(s) to target memory",
4260 .usage
= "address data [count]",
4264 .mode
= COMMAND_EXEC
,
4265 .jim_handler
= jim_target_mw
,
4266 .help
= "Write byte(s) to target memory",
4267 .usage
= "address data [count]",
4271 .mode
= COMMAND_EXEC
,
4272 .jim_handler
= jim_target_md
,
4273 .help
= "Display target memory as 32-bit words",
4274 .usage
= "address [count]",
4278 .mode
= COMMAND_EXEC
,
4279 .jim_handler
= jim_target_md
,
4280 .help
= "Display target memory as 16-bit half-words",
4281 .usage
= "address [count]",
4285 .mode
= COMMAND_EXEC
,
4286 .jim_handler
= jim_target_md
,
4287 .help
= "Display target memory as 8-bit bytes",
4288 .usage
= "address [count]",
4291 .name
= "array2mem",
4292 .mode
= COMMAND_EXEC
,
4293 .jim_handler
= jim_target_array2mem
,
4294 .help
= "Writes Tcl array of 8/16/32 bit numbers "
4296 .usage
= "arrayname bitwidth address count",
4299 .name
= "mem2array",
4300 .mode
= COMMAND_EXEC
,
4301 .jim_handler
= jim_target_mem2array
,
4302 .help
= "Loads Tcl array of 8/16/32 bit numbers "
4303 "from target memory",
4304 .usage
= "arrayname bitwidth address count",
4307 .name
= "eventlist",
4308 .mode
= COMMAND_EXEC
,
4309 .jim_handler
= jim_target_event_list
,
4310 .help
= "displays a table of events defined for this target",
4314 .mode
= COMMAND_EXEC
,
4315 .jim_handler
= jim_target_current_state
,
4316 .help
= "displays the current state of this target",
4319 .name
= "arp_examine",
4320 .mode
= COMMAND_EXEC
,
4321 .jim_handler
= jim_target_examine
,
4322 .help
= "used internally for reset processing",
4326 .mode
= COMMAND_EXEC
,
4327 .jim_handler
= jim_target_poll
,
4328 .help
= "used internally for reset processing",
4331 .name
= "arp_reset",
4332 .mode
= COMMAND_EXEC
,
4333 .jim_handler
= jim_target_reset
,
4334 .help
= "used internally for reset processing",
4338 .mode
= COMMAND_EXEC
,
4339 .jim_handler
= jim_target_halt
,
4340 .help
= "used internally for reset processing",
4343 .name
= "arp_waitstate",
4344 .mode
= COMMAND_EXEC
,
4345 .jim_handler
= jim_target_wait_state
,
4346 .help
= "used internally for reset processing",
4349 .name
= "invoke-event",
4350 .mode
= COMMAND_EXEC
,
4351 .jim_handler
= jim_target_invoke_event
,
4352 .help
= "invoke handler for specified event",
4353 .usage
= "event_name",
4355 COMMAND_REGISTRATION_DONE
4358 static int target_create(Jim_GetOptInfo
*goi
)
4366 struct target
*target
;
4367 struct command_context
*cmd_ctx
;
4369 cmd_ctx
= Jim_GetAssocData(goi
->interp
, "context");
4370 if (goi
->argc
< 3) {
4371 Jim_WrongNumArgs(goi
->interp
, 1, goi
->argv
, "?name? ?type? ..options...");
4376 Jim_GetOpt_Obj(goi
, &new_cmd
);
4377 /* does this command exist? */
4378 cmd
= Jim_GetCommand(goi
->interp
, new_cmd
, JIM_ERRMSG
);
4380 cp
= Jim_GetString(new_cmd
, NULL
);
4381 Jim_SetResult_sprintf(goi
->interp
, "Command/target: %s Exists", cp
);
4386 e
= Jim_GetOpt_String(goi
, &cp2
, NULL
);
4388 /* now does target type exist */
4389 for (x
= 0 ; target_types
[x
] ; x
++) {
4390 if (0 == strcmp(cp
, target_types
[x
]->name
)) {
4395 if (target_types
[x
] == NULL
) {
4396 Jim_SetResult_sprintf(goi
->interp
, "Unknown target type %s, try one of ", cp
);
4397 for (x
= 0 ; target_types
[x
] ; x
++) {
4398 if (target_types
[x
+ 1]) {
4399 Jim_AppendStrings(goi
->interp
,
4400 Jim_GetResult(goi
->interp
),
4401 target_types
[x
]->name
,
4404 Jim_AppendStrings(goi
->interp
,
4405 Jim_GetResult(goi
->interp
),
4407 target_types
[x
]->name
,NULL
);
4414 target
= calloc(1,sizeof(struct target
));
4415 /* set target number */
4416 target
->target_number
= new_target_number();
4418 /* allocate memory for each unique target type */
4419 target
->type
= (struct target_type
*)calloc(1,sizeof(struct target_type
));
4421 memcpy(target
->type
, target_types
[x
], sizeof(struct target_type
));
4423 /* will be set by "-endian" */
4424 target
->endianness
= TARGET_ENDIAN_UNKNOWN
;
4426 target
->working_area
= 0x0;
4427 target
->working_area_size
= 0x0;
4428 target
->working_areas
= NULL
;
4429 target
->backup_working_area
= 0;
4431 target
->state
= TARGET_UNKNOWN
;
4432 target
->debug_reason
= DBG_REASON_UNDEFINED
;
4433 target
->reg_cache
= NULL
;
4434 target
->breakpoints
= NULL
;
4435 target
->watchpoints
= NULL
;
4436 target
->next
= NULL
;
4437 target
->arch_info
= NULL
;
4439 target
->display
= 1;
4441 target
->halt_issued
= false;
4443 /* initialize trace information */
4444 target
->trace_info
= malloc(sizeof(struct trace
));
4445 target
->trace_info
->num_trace_points
= 0;
4446 target
->trace_info
->trace_points_size
= 0;
4447 target
->trace_info
->trace_points
= NULL
;
4448 target
->trace_info
->trace_history_size
= 0;
4449 target
->trace_info
->trace_history
= NULL
;
4450 target
->trace_info
->trace_history_pos
= 0;
4451 target
->trace_info
->trace_history_overflowed
= 0;
4453 target
->dbgmsg
= NULL
;
4454 target
->dbg_msg_enabled
= 0;
4456 target
->endianness
= TARGET_ENDIAN_UNKNOWN
;
4458 /* Do the rest as "configure" options */
4459 goi
->isconfigure
= 1;
4460 e
= target_configure(goi
, target
);
4462 if (target
->tap
== NULL
)
4464 Jim_SetResultString(goi
->interp
, "-chain-position required when creating target", -1);
4474 if (target
->endianness
== TARGET_ENDIAN_UNKNOWN
) {
4475 /* default endian to little if not specified */
4476 target
->endianness
= TARGET_LITTLE_ENDIAN
;
4479 /* incase variant is not set */
4480 if (!target
->variant
)
4481 target
->variant
= strdup("");
4483 cp
= Jim_GetString(new_cmd
, NULL
);
4484 target
->cmd_name
= strdup(cp
);
4486 /* create the target specific commands */
4487 if (target
->type
->commands
) {
4488 e
= register_commands(cmd_ctx
, NULL
, target
->type
->commands
);
4490 LOG_ERROR("unable to register '%s' commands", cp
);
4492 if (target
->type
->target_create
) {
4493 (*(target
->type
->target_create
))(target
, goi
->interp
);
4496 /* append to end of list */
4498 struct target
**tpp
;
4499 tpp
= &(all_targets
);
4501 tpp
= &((*tpp
)->next
);
4506 /* now - create the new target name command */
4507 const const struct command_registration target_subcommands
[] = {
4509 .chain
= target_instance_command_handlers
,
4512 .chain
= target
->type
->commands
,
4514 COMMAND_REGISTRATION_DONE
4516 const const struct command_registration target_commands
[] = {
4519 .mode
= COMMAND_ANY
,
4520 .help
= "target command group",
4521 .chain
= target_subcommands
,
4523 COMMAND_REGISTRATION_DONE
4525 e
= register_commands(cmd_ctx
, NULL
, target_commands
);
4529 struct command
*c
= command_find_in_context(cmd_ctx
, cp
);
4531 command_set_handler_data(c
, target
);
4533 return (ERROR_OK
== e
) ? JIM_OK
: JIM_ERR
;
4536 static int jim_target_current(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4540 Jim_WrongNumArgs(interp
, 1, argv
, "Too many parameters");
4543 struct command_context
*cmd_ctx
= Jim_GetAssocData(interp
, "context");
4544 Jim_SetResultString(interp
, get_current_target(cmd_ctx
)->cmd_name
, -1);
4548 static int jim_target_types(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4552 Jim_WrongNumArgs(interp
, 1, argv
, "Too many parameters");
4555 Jim_SetResult(interp
, Jim_NewListObj(interp
, NULL
, 0));
4556 for (unsigned x
= 0; NULL
!= target_types
[x
]; x
++)
4558 Jim_ListAppendElement(interp
, Jim_GetResult(interp
),
4559 Jim_NewStringObj(interp
, target_types
[x
]->name
, -1));
4564 static int jim_target_names(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4568 Jim_WrongNumArgs(interp
, 1, argv
, "Too many parameters");
4571 Jim_SetResult(interp
, Jim_NewListObj(interp
, NULL
, 0));
4572 struct target
*target
= all_targets
;
4575 Jim_ListAppendElement(interp
, Jim_GetResult(interp
),
4576 Jim_NewStringObj(interp
, target_name(target
), -1));
4577 target
= target
->next
;
4582 static int jim_target_create(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4585 Jim_GetOpt_Setup(&goi
, interp
, argc
- 1, argv
+ 1);
4588 Jim_WrongNumArgs(goi
.interp
, goi
.argc
, goi
.argv
,
4589 "<name> <target_type> [<target_options> ...]");
4592 return target_create(&goi
);
4595 static int jim_target_number(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4598 Jim_GetOpt_Setup(&goi
, interp
, argc
- 1, argv
+ 1);
4600 /* It's OK to remove this mechanism sometime after August 2010 or so */
4601 LOG_WARNING("don't use numbers as target identifiers; use names");
4604 Jim_SetResult_sprintf(goi
.interp
, "usage: target number <number>");
4608 int e
= Jim_GetOpt_Wide(&goi
, &w
);
4612 struct target
*target
;
4613 for (target
= all_targets
; NULL
!= target
; target
= target
->next
)
4615 if (target
->target_number
!= w
)
4618 Jim_SetResultString(goi
.interp
, target_name(target
), -1);
4621 Jim_SetResult_sprintf(goi
.interp
,
4622 "Target: number %d does not exist", (int)(w
));
4626 static int jim_target_count(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4630 Jim_WrongNumArgs(interp
, 1, argv
, "<no parameters>");
4634 struct target
*target
= all_targets
;
4635 while (NULL
!= target
)
4637 target
= target
->next
;
4640 Jim_SetResult(interp
, Jim_NewIntObj(interp
, count
));
4644 static const struct command_registration target_subcommand_handlers
[] = {
4647 .mode
= COMMAND_CONFIG
,
4648 .handler
= handle_target_init_command
,
4649 .help
= "initialize targets",
4653 /* REVISIT this should be COMMAND_CONFIG ... */
4654 .mode
= COMMAND_ANY
,
4655 .jim_handler
= jim_target_create
,
4656 .usage
= "name type '-chain-position' name [options ...]",
4657 .help
= "Creates and selects a new target",
4661 .mode
= COMMAND_ANY
,
4662 .jim_handler
= jim_target_current
,
4663 .help
= "Returns the currently selected target",
4667 .mode
= COMMAND_ANY
,
4668 .jim_handler
= jim_target_types
,
4669 .help
= "Returns the available target types as "
4670 "a list of strings",
4674 .mode
= COMMAND_ANY
,
4675 .jim_handler
= jim_target_names
,
4676 .help
= "Returns the names of all targets as a list of strings",
4680 .mode
= COMMAND_ANY
,
4681 .jim_handler
= jim_target_number
,
4683 .help
= "Returns the name of the numbered target "
4688 .mode
= COMMAND_ANY
,
4689 .jim_handler
= jim_target_count
,
4690 .help
= "Returns the number of targets as an integer "
4693 COMMAND_REGISTRATION_DONE
4704 static int fastload_num
;
4705 static struct FastLoad
*fastload
;
4707 static void free_fastload(void)
4709 if (fastload
!= NULL
)
4712 for (i
= 0; i
< fastload_num
; i
++)
4714 if (fastload
[i
].data
)
4715 free(fastload
[i
].data
);
4725 COMMAND_HANDLER(handle_fast_load_image_command
)
4729 uint32_t image_size
;
4730 uint32_t min_address
= 0;
4731 uint32_t max_address
= 0xffffffff;
4736 int retval
= CALL_COMMAND_HANDLER(parse_load_image_command_CMD_ARGV
,
4737 &image
, &min_address
, &max_address
);
4738 if (ERROR_OK
!= retval
)
4741 struct duration bench
;
4742 duration_start(&bench
);
4744 if (image_open(&image
, CMD_ARGV
[0], (CMD_ARGC
>= 3) ? CMD_ARGV
[2] : NULL
) != ERROR_OK
)
4751 fastload_num
= image
.num_sections
;
4752 fastload
= (struct FastLoad
*)malloc(sizeof(struct FastLoad
)*image
.num_sections
);
4753 if (fastload
== NULL
)
4755 image_close(&image
);
4758 memset(fastload
, 0, sizeof(struct FastLoad
)*image
.num_sections
);
4759 for (i
= 0; i
< image
.num_sections
; i
++)
4761 buffer
= malloc(image
.sections
[i
].size
);
4764 command_print(CMD_CTX
, "error allocating buffer for section (%d bytes)",
4765 (int)(image
.sections
[i
].size
));
4769 if ((retval
= image_read_section(&image
, i
, 0x0, image
.sections
[i
].size
, buffer
, &buf_cnt
)) != ERROR_OK
)
4775 uint32_t offset
= 0;
4776 uint32_t length
= buf_cnt
;
4779 /* DANGER!!! beware of unsigned comparision here!!! */
4781 if ((image
.sections
[i
].base_address
+ buf_cnt
>= min_address
)&&
4782 (image
.sections
[i
].base_address
< max_address
))
4784 if (image
.sections
[i
].base_address
< min_address
)
4786 /* clip addresses below */
4787 offset
+= min_address
-image
.sections
[i
].base_address
;
4791 if (image
.sections
[i
].base_address
+ buf_cnt
> max_address
)
4793 length
-= (image
.sections
[i
].base_address
+ buf_cnt
)-max_address
;
4796 fastload
[i
].address
= image
.sections
[i
].base_address
+ offset
;
4797 fastload
[i
].data
= malloc(length
);
4798 if (fastload
[i
].data
== NULL
)
4803 memcpy(fastload
[i
].data
, buffer
+ offset
, length
);
4804 fastload
[i
].length
= length
;
4806 image_size
+= length
;
4807 command_print(CMD_CTX
, "%u bytes written at address 0x%8.8x",
4808 (unsigned int)length
,
4809 ((unsigned int)(image
.sections
[i
].base_address
+ offset
)));
4815 if ((ERROR_OK
== retval
) && (duration_measure(&bench
) == ERROR_OK
))
4817 command_print(CMD_CTX
, "Loaded %" PRIu32
" bytes "
4818 "in %fs (%0.3f kb/s)", image_size
,
4819 duration_elapsed(&bench
), duration_kbps(&bench
, image_size
));
4821 command_print(CMD_CTX
,
4822 "WARNING: image has not been loaded to target!"
4823 "You can issue a 'fast_load' to finish loading.");
4826 image_close(&image
);
4828 if (retval
!= ERROR_OK
)
4836 COMMAND_HANDLER(handle_fast_load_command
)
4839 return ERROR_COMMAND_SYNTAX_ERROR
;
4840 if (fastload
== NULL
)
4842 LOG_ERROR("No image in memory");
4846 int ms
= timeval_ms();
4848 int retval
= ERROR_OK
;
4849 for (i
= 0; i
< fastload_num
;i
++)
4851 struct target
*target
= get_current_target(CMD_CTX
);
4852 command_print(CMD_CTX
, "Write to 0x%08x, length 0x%08x",
4853 (unsigned int)(fastload
[i
].address
),
4854 (unsigned int)(fastload
[i
].length
));
4855 if (retval
== ERROR_OK
)
4857 retval
= target_write_buffer(target
, fastload
[i
].address
, fastload
[i
].length
, fastload
[i
].data
);
4859 size
+= fastload
[i
].length
;
4861 int after
= timeval_ms();
4862 command_print(CMD_CTX
, "Loaded image %f kBytes/s", (float)(size
/1024.0)/((float)(after
-ms
)/1000.0));
4866 static const struct command_registration target_command_handlers
[] = {
4869 .handler
= handle_targets_command
,
4870 .mode
= COMMAND_ANY
,
4871 .help
= "change current default target (one parameter) "
4872 "or prints table of all targets (no parameters)",
4873 .usage
= "[target]",
4877 .mode
= COMMAND_CONFIG
,
4878 .help
= "configure target",
4880 .chain
= target_subcommand_handlers
,
4882 COMMAND_REGISTRATION_DONE
4885 int target_register_commands(struct command_context
*cmd_ctx
)
4887 return register_commands(cmd_ctx
, NULL
, target_command_handlers
);
4890 static const struct command_registration target_exec_command_handlers
[] = {
4892 .name
= "fast_load_image",
4893 .handler
= handle_fast_load_image_command
,
4894 .mode
= COMMAND_ANY
,
4895 .help
= "Load image into server memory for later use by "
4896 "fast_load; primarily for profiling",
4897 .usage
= "filename address ['bin'|'ihex'|'elf'|'s19'] "
4898 "[min_address [max_length]]",
4901 .name
= "fast_load",
4902 .handler
= handle_fast_load_command
,
4903 .mode
= COMMAND_EXEC
,
4904 .help
= "loads active fast load image to current target "
4905 "- mainly for profiling purposes",
4909 .handler
= handle_profile_command
,
4910 .mode
= COMMAND_EXEC
,
4911 .help
= "profiling samples the CPU PC",
4913 /** @todo don't register virt2phys() unless target supports it */
4915 .name
= "virt2phys",
4916 .handler
= handle_virt2phys_command
,
4917 .mode
= COMMAND_ANY
,
4918 .help
= "translate a virtual address into a physical address",
4919 .usage
= "virtual_address",
4923 .handler
= handle_reg_command
,
4924 .mode
= COMMAND_EXEC
,
4925 .help
= "display or set a register; with no arguments, "
4926 "displays all registers and their values",
4927 .usage
= "[(register_name|register_number) [value]]",
4931 .handler
= handle_poll_command
,
4932 .mode
= COMMAND_EXEC
,
4933 .help
= "poll target state; or reconfigure background polling",
4934 .usage
= "['on'|'off']",
4937 .name
= "wait_halt",
4938 .handler
= handle_wait_halt_command
,
4939 .mode
= COMMAND_EXEC
,
4940 .help
= "wait up to the specified number of milliseconds "
4941 "(default 5) for a previously requested halt",
4942 .usage
= "[milliseconds]",
4946 .handler
= handle_halt_command
,
4947 .mode
= COMMAND_EXEC
,
4948 .help
= "request target to halt, then wait up to the specified"
4949 "number of milliseconds (default 5) for it to complete",
4950 .usage
= "[milliseconds]",
4954 .handler
= handle_resume_command
,
4955 .mode
= COMMAND_EXEC
,
4956 .help
= "resume target execution from current PC or address",
4957 .usage
= "[address]",
4961 .handler
= handle_reset_command
,
4962 .mode
= COMMAND_EXEC
,
4963 .usage
= "[run|halt|init]",
4964 .help
= "Reset all targets into the specified mode."
4965 "Default reset mode is run, if not given.",
4968 .name
= "soft_reset_halt",
4969 .handler
= handle_soft_reset_halt_command
,
4970 .mode
= COMMAND_EXEC
,
4971 .help
= "halt the target and do a soft reset",
4975 .handler
= handle_step_command
,
4976 .mode
= COMMAND_EXEC
,
4977 .help
= "step one instruction from current PC or address",
4978 .usage
= "[address]",
4982 .handler
= handle_md_command
,
4983 .mode
= COMMAND_EXEC
,
4984 .help
= "display memory words",
4985 .usage
= "['phys'] address [count]",
4989 .handler
= handle_md_command
,
4990 .mode
= COMMAND_EXEC
,
4991 .help
= "display memory half-words",
4992 .usage
= "['phys'] address [count]",
4996 .handler
= handle_md_command
,
4997 .mode
= COMMAND_EXEC
,
4998 .help
= "display memory bytes",
4999 .usage
= "['phys'] address [count]",
5003 .handler
= handle_mw_command
,
5004 .mode
= COMMAND_EXEC
,
5005 .help
= "write memory word",
5006 .usage
= "['phys'] address value [count]",
5010 .handler
= handle_mw_command
,
5011 .mode
= COMMAND_EXEC
,
5012 .help
= "write memory half-word",
5013 .usage
= "['phys'] address value [count]",
5017 .handler
= handle_mw_command
,
5018 .mode
= COMMAND_EXEC
,
5019 .help
= "write memory byte",
5020 .usage
= "['phys'] address value [count]",
5024 .handler
= handle_bp_command
,
5025 .mode
= COMMAND_EXEC
,
5026 .help
= "list or set hardware or software breakpoint",
5027 .usage
= "[address length ['hw']]",
5031 .handler
= handle_rbp_command
,
5032 .mode
= COMMAND_EXEC
,
5033 .help
= "remove breakpoint",
5038 .handler
= handle_wp_command
,
5039 .mode
= COMMAND_EXEC
,
5040 .help
= "list (no params) or create watchpoints",
5041 .usage
= "[address length [('r'|'w'|'a') value [mask]]]",
5045 .handler
= handle_rwp_command
,
5046 .mode
= COMMAND_EXEC
,
5047 .help
= "remove watchpoint",
5051 .name
= "load_image",
5052 .handler
= handle_load_image_command
,
5053 .mode
= COMMAND_EXEC
,
5054 .usage
= "filename address ['bin'|'ihex'|'elf'|'s19'] "
5055 "[min_address] [max_length]",
5058 .name
= "dump_image",
5059 .handler
= handle_dump_image_command
,
5060 .mode
= COMMAND_EXEC
,
5061 .usage
= "filename address size",
5064 .name
= "verify_image",
5065 .handler
= handle_verify_image_command
,
5066 .mode
= COMMAND_EXEC
,
5067 .usage
= "filename [offset [type]]",
5070 .name
= "test_image",
5071 .handler
= handle_test_image_command
,
5072 .mode
= COMMAND_EXEC
,
5073 .usage
= "filename [offset [type]]",
5076 .name
= "ocd_mem2array",
5077 .mode
= COMMAND_EXEC
,
5078 .jim_handler
= jim_mem2array
,
5079 .help
= "read 8/16/32 bit memory and return as a TCL array "
5080 "for script processing",
5081 .usage
= "arrayname bitwidth address count",
5084 .name
= "ocd_array2mem",
5085 .mode
= COMMAND_EXEC
,
5086 .jim_handler
= jim_array2mem
,
5087 .help
= "convert a TCL array to memory locations "
5088 "and write the 8/16/32 bit values",
5089 .usage
= "arrayname bitwidth address count",
5091 COMMAND_REGISTRATION_DONE
5093 int target_register_user_commands(struct command_context
*cmd_ctx
)
5095 int retval
= ERROR_OK
;
5096 if ((retval
= target_request_register_commands(cmd_ctx
)) != ERROR_OK
)
5099 if ((retval
= trace_register_commands(cmd_ctx
)) != ERROR_OK
)
5103 return register_commands(cmd_ctx
, NULL
, target_exec_command_handlers
);