1 /***************************************************************************
2 * Copyright (C) 2005 by Dominic Rath *
3 * Dominic.Rath@gmx.de *
5 * Copyright (C) 2007-2009 Øyvind Harboe *
6 * oyvind.harboe@zylin.com *
8 * Copyright (C) 2008, Duane Ellis *
9 * openocd@duaneeellis.com *
11 * Copyright (C) 2008 by Spencer Oliver *
12 * spen@spen-soft.co.uk *
14 * Copyright (C) 2008 by Rick Altherr *
15 * kc8apf@kc8apf.net> *
17 * This program is free software; you can redistribute it and/or modify *
18 * it under the terms of the GNU General Public License as published by *
19 * the Free Software Foundation; either version 2 of the License, or *
20 * (at your option) any later version. *
22 * This program is distributed in the hope that it will be useful, *
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
25 * GNU General Public License for more details. *
27 * You should have received a copy of the GNU General Public License *
28 * along with this program; if not, write to the *
29 * Free Software Foundation, Inc., *
30 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
31 ***************************************************************************/
37 #include "target_type.h"
38 #include "target_request.h"
39 #include "breakpoints.h"
40 #include <helper/time_support.h>
44 #include <jtag/jtag.h>
47 static int target_array2mem(Jim_Interp
*interp
, struct target
*target
, int argc
, Jim_Obj
*const *argv
);
48 static int target_mem2array(Jim_Interp
*interp
, struct target
*target
, int argc
, Jim_Obj
*const *argv
);
51 extern struct target_type arm7tdmi_target
;
52 extern struct target_type arm720t_target
;
53 extern struct target_type arm9tdmi_target
;
54 extern struct target_type arm920t_target
;
55 extern struct target_type arm966e_target
;
56 extern struct target_type arm926ejs_target
;
57 extern struct target_type fa526_target
;
58 extern struct target_type feroceon_target
;
59 extern struct target_type dragonite_target
;
60 extern struct target_type xscale_target
;
61 extern struct target_type cortexm3_target
;
62 extern struct target_type cortexa8_target
;
63 extern struct target_type arm11_target
;
64 extern struct target_type mips_m4k_target
;
65 extern struct target_type avr_target
;
66 extern struct target_type testee_target
;
68 struct target_type
*target_types
[] =
89 struct target
*all_targets
= NULL
;
90 struct target_event_callback
*target_event_callbacks
= NULL
;
91 struct target_timer_callback
*target_timer_callbacks
= NULL
;
93 static const Jim_Nvp nvp_assert
[] = {
94 { .name
= "assert", NVP_ASSERT
},
95 { .name
= "deassert", NVP_DEASSERT
},
96 { .name
= "T", NVP_ASSERT
},
97 { .name
= "F", NVP_DEASSERT
},
98 { .name
= "t", NVP_ASSERT
},
99 { .name
= "f", NVP_DEASSERT
},
100 { .name
= NULL
, .value
= -1 }
103 static const Jim_Nvp nvp_error_target
[] = {
104 { .value
= ERROR_TARGET_INVALID
, .name
= "err-invalid" },
105 { .value
= ERROR_TARGET_INIT_FAILED
, .name
= "err-init-failed" },
106 { .value
= ERROR_TARGET_TIMEOUT
, .name
= "err-timeout" },
107 { .value
= ERROR_TARGET_NOT_HALTED
, .name
= "err-not-halted" },
108 { .value
= ERROR_TARGET_FAILURE
, .name
= "err-failure" },
109 { .value
= ERROR_TARGET_UNALIGNED_ACCESS
, .name
= "err-unaligned-access" },
110 { .value
= ERROR_TARGET_DATA_ABORT
, .name
= "err-data-abort" },
111 { .value
= ERROR_TARGET_RESOURCE_NOT_AVAILABLE
, .name
= "err-resource-not-available" },
112 { .value
= ERROR_TARGET_TRANSLATION_FAULT
, .name
= "err-translation-fault" },
113 { .value
= ERROR_TARGET_NOT_RUNNING
, .name
= "err-not-running" },
114 { .value
= ERROR_TARGET_NOT_EXAMINED
, .name
= "err-not-examined" },
115 { .value
= -1, .name
= NULL
}
118 const char *target_strerror_safe(int err
)
122 n
= Jim_Nvp_value2name_simple(nvp_error_target
, err
);
123 if (n
->name
== NULL
) {
130 static const Jim_Nvp nvp_target_event
[] = {
131 { .value
= TARGET_EVENT_OLD_gdb_program_config
, .name
= "old-gdb_program_config" },
132 { .value
= TARGET_EVENT_OLD_pre_resume
, .name
= "old-pre_resume" },
134 { .value
= TARGET_EVENT_GDB_HALT
, .name
= "gdb-halt" },
135 { .value
= TARGET_EVENT_HALTED
, .name
= "halted" },
136 { .value
= TARGET_EVENT_RESUMED
, .name
= "resumed" },
137 { .value
= TARGET_EVENT_RESUME_START
, .name
= "resume-start" },
138 { .value
= TARGET_EVENT_RESUME_END
, .name
= "resume-end" },
140 { .name
= "gdb-start", .value
= TARGET_EVENT_GDB_START
},
141 { .name
= "gdb-end", .value
= TARGET_EVENT_GDB_END
},
143 /* historical name */
145 { .value
= TARGET_EVENT_RESET_START
, .name
= "reset-start" },
147 { .value
= TARGET_EVENT_RESET_ASSERT_PRE
, .name
= "reset-assert-pre" },
148 { .value
= TARGET_EVENT_RESET_ASSERT
, .name
= "reset-assert" },
149 { .value
= TARGET_EVENT_RESET_ASSERT_POST
, .name
= "reset-assert-post" },
150 { .value
= TARGET_EVENT_RESET_DEASSERT_PRE
, .name
= "reset-deassert-pre" },
151 { .value
= TARGET_EVENT_RESET_DEASSERT_POST
, .name
= "reset-deassert-post" },
152 { .value
= TARGET_EVENT_RESET_HALT_PRE
, .name
= "reset-halt-pre" },
153 { .value
= TARGET_EVENT_RESET_HALT_POST
, .name
= "reset-halt-post" },
154 { .value
= TARGET_EVENT_RESET_WAIT_PRE
, .name
= "reset-wait-pre" },
155 { .value
= TARGET_EVENT_RESET_WAIT_POST
, .name
= "reset-wait-post" },
156 { .value
= TARGET_EVENT_RESET_INIT
, .name
= "reset-init" },
157 { .value
= TARGET_EVENT_RESET_END
, .name
= "reset-end" },
159 { .value
= TARGET_EVENT_EXAMINE_START
, .name
= "examine-start" },
160 { .value
= TARGET_EVENT_EXAMINE_END
, .name
= "examine-end" },
162 { .value
= TARGET_EVENT_DEBUG_HALTED
, .name
= "debug-halted" },
163 { .value
= TARGET_EVENT_DEBUG_RESUMED
, .name
= "debug-resumed" },
165 { .value
= TARGET_EVENT_GDB_ATTACH
, .name
= "gdb-attach" },
166 { .value
= TARGET_EVENT_GDB_DETACH
, .name
= "gdb-detach" },
168 { .value
= TARGET_EVENT_GDB_FLASH_WRITE_START
, .name
= "gdb-flash-write-start" },
169 { .value
= TARGET_EVENT_GDB_FLASH_WRITE_END
, .name
= "gdb-flash-write-end" },
171 { .value
= TARGET_EVENT_GDB_FLASH_ERASE_START
, .name
= "gdb-flash-erase-start" },
172 { .value
= TARGET_EVENT_GDB_FLASH_ERASE_END
, .name
= "gdb-flash-erase-end" },
174 { .value
= TARGET_EVENT_RESUME_START
, .name
= "resume-start" },
175 { .value
= TARGET_EVENT_RESUMED
, .name
= "resume-ok" },
176 { .value
= TARGET_EVENT_RESUME_END
, .name
= "resume-end" },
178 { .name
= NULL
, .value
= -1 }
181 static const Jim_Nvp nvp_target_state
[] = {
182 { .name
= "unknown", .value
= TARGET_UNKNOWN
},
183 { .name
= "running", .value
= TARGET_RUNNING
},
184 { .name
= "halted", .value
= TARGET_HALTED
},
185 { .name
= "reset", .value
= TARGET_RESET
},
186 { .name
= "debug-running", .value
= TARGET_DEBUG_RUNNING
},
187 { .name
= NULL
, .value
= -1 },
190 static const Jim_Nvp nvp_target_debug_reason
[] = {
191 { .name
= "debug-request" , .value
= DBG_REASON_DBGRQ
},
192 { .name
= "breakpoint" , .value
= DBG_REASON_BREAKPOINT
},
193 { .name
= "watchpoint" , .value
= DBG_REASON_WATCHPOINT
},
194 { .name
= "watchpoint-and-breakpoint", .value
= DBG_REASON_WPTANDBKPT
},
195 { .name
= "single-step" , .value
= DBG_REASON_SINGLESTEP
},
196 { .name
= "target-not-halted" , .value
= DBG_REASON_NOTHALTED
},
197 { .name
= "undefined" , .value
= DBG_REASON_UNDEFINED
},
198 { .name
= NULL
, .value
= -1 },
201 static const Jim_Nvp nvp_target_endian
[] = {
202 { .name
= "big", .value
= TARGET_BIG_ENDIAN
},
203 { .name
= "little", .value
= TARGET_LITTLE_ENDIAN
},
204 { .name
= "be", .value
= TARGET_BIG_ENDIAN
},
205 { .name
= "le", .value
= TARGET_LITTLE_ENDIAN
},
206 { .name
= NULL
, .value
= -1 },
209 static const Jim_Nvp nvp_reset_modes
[] = {
210 { .name
= "unknown", .value
= RESET_UNKNOWN
},
211 { .name
= "run" , .value
= RESET_RUN
},
212 { .name
= "halt" , .value
= RESET_HALT
},
213 { .name
= "init" , .value
= RESET_INIT
},
214 { .name
= NULL
, .value
= -1 },
217 const char *debug_reason_name(struct target
*t
)
221 cp
= Jim_Nvp_value2name_simple(nvp_target_debug_reason
,
222 t
->debug_reason
)->name
;
224 LOG_ERROR("Invalid debug reason: %d", (int)(t
->debug_reason
));
225 cp
= "(*BUG*unknown*BUG*)";
231 target_state_name( struct target
*t
)
234 cp
= Jim_Nvp_value2name_simple(nvp_target_state
, t
->state
)->name
;
236 LOG_ERROR("Invalid target state: %d", (int)(t
->state
));
237 cp
= "(*BUG*unknown*BUG*)";
242 /* determine the number of the new target */
243 static int new_target_number(void)
248 /* number is 0 based */
252 if (x
< t
->target_number
) {
253 x
= t
->target_number
;
260 /* read a uint32_t from a buffer in target memory endianness */
261 uint32_t target_buffer_get_u32(struct target
*target
, const uint8_t *buffer
)
263 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
264 return le_to_h_u32(buffer
);
266 return be_to_h_u32(buffer
);
269 /* read a uint16_t from a buffer in target memory endianness */
270 uint16_t target_buffer_get_u16(struct target
*target
, const uint8_t *buffer
)
272 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
273 return le_to_h_u16(buffer
);
275 return be_to_h_u16(buffer
);
278 /* read a uint8_t from a buffer in target memory endianness */
279 uint8_t target_buffer_get_u8(struct target
*target
, const uint8_t *buffer
)
281 return *buffer
& 0x0ff;
284 /* write a uint32_t to a buffer in target memory endianness */
285 void target_buffer_set_u32(struct target
*target
, uint8_t *buffer
, uint32_t value
)
287 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
288 h_u32_to_le(buffer
, value
);
290 h_u32_to_be(buffer
, value
);
293 /* write a uint16_t to a buffer in target memory endianness */
294 void target_buffer_set_u16(struct target
*target
, uint8_t *buffer
, uint16_t value
)
296 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
297 h_u16_to_le(buffer
, value
);
299 h_u16_to_be(buffer
, value
);
302 /* write a uint8_t to a buffer in target memory endianness */
303 void target_buffer_set_u8(struct target
*target
, uint8_t *buffer
, uint8_t value
)
308 /* return a pointer to a configured target; id is name or number */
309 struct target
*get_target(const char *id
)
311 struct target
*target
;
313 /* try as tcltarget name */
314 for (target
= all_targets
; target
; target
= target
->next
) {
315 if (target
->cmd_name
== NULL
)
317 if (strcmp(id
, target
->cmd_name
) == 0)
321 /* It's OK to remove this fallback sometime after August 2010 or so */
323 /* no match, try as number */
325 if (parse_uint(id
, &num
) != ERROR_OK
)
328 for (target
= all_targets
; target
; target
= target
->next
) {
329 if (target
->target_number
== (int)num
) {
330 LOG_WARNING("use '%s' as target identifier, not '%u'",
331 target
->cmd_name
, num
);
339 /* returns a pointer to the n-th configured target */
340 static struct target
*get_target_by_num(int num
)
342 struct target
*target
= all_targets
;
345 if (target
->target_number
== num
) {
348 target
= target
->next
;
354 struct target
* get_current_target(struct command_context
*cmd_ctx
)
356 struct target
*target
= get_target_by_num(cmd_ctx
->current_target
);
360 LOG_ERROR("BUG: current_target out of bounds");
367 int target_poll(struct target
*target
)
371 /* We can't poll until after examine */
372 if (!target_was_examined(target
))
374 /* Fail silently lest we pollute the log */
378 retval
= target
->type
->poll(target
);
379 if (retval
!= ERROR_OK
)
382 if (target
->halt_issued
)
384 if (target
->state
== TARGET_HALTED
)
386 target
->halt_issued
= false;
389 long long t
= timeval_ms() - target
->halt_issued_time
;
392 target
->halt_issued
= false;
393 LOG_INFO("Halt timed out, wake up GDB.");
394 target_call_event_callbacks(target
, TARGET_EVENT_GDB_HALT
);
402 int target_halt(struct target
*target
)
405 /* We can't poll until after examine */
406 if (!target_was_examined(target
))
408 LOG_ERROR("Target not examined yet");
412 retval
= target
->type
->halt(target
);
413 if (retval
!= ERROR_OK
)
416 target
->halt_issued
= true;
417 target
->halt_issued_time
= timeval_ms();
422 int target_resume(struct target
*target
, int current
, uint32_t address
, int handle_breakpoints
, int debug_execution
)
426 /* We can't poll until after examine */
427 if (!target_was_examined(target
))
429 LOG_ERROR("Target not examined yet");
433 /* note that resume *must* be asynchronous. The CPU can halt before we poll. The CPU can
434 * even halt at the current PC as a result of a software breakpoint being inserted by (a bug?)
437 if ((retval
= target
->type
->resume(target
, current
, address
, handle_breakpoints
, debug_execution
)) != ERROR_OK
)
443 int target_process_reset(struct command_context
*cmd_ctx
, enum target_reset_mode reset_mode
)
448 n
= Jim_Nvp_value2name_simple(nvp_reset_modes
, reset_mode
);
449 if (n
->name
== NULL
) {
450 LOG_ERROR("invalid reset mode");
454 /* disable polling during reset to make reset event scripts
455 * more predictable, i.e. dr/irscan & pathmove in events will
456 * not have JTAG operations injected into the middle of a sequence.
458 bool save_poll
= jtag_poll_get_enabled();
460 jtag_poll_set_enabled(false);
462 sprintf(buf
, "ocd_process_reset %s", n
->name
);
463 retval
= Jim_Eval(cmd_ctx
->interp
, buf
);
465 jtag_poll_set_enabled(save_poll
);
467 if (retval
!= JIM_OK
) {
468 Jim_PrintErrorMessage(cmd_ctx
->interp
);
472 /* We want any events to be processed before the prompt */
473 retval
= target_call_timer_callbacks_now();
478 static int identity_virt2phys(struct target
*target
,
479 uint32_t virtual, uint32_t *physical
)
485 static int no_mmu(struct target
*target
, int *enabled
)
491 static int default_examine(struct target
*target
)
493 target_set_examined(target
);
497 int target_examine_one(struct target
*target
)
499 return target
->type
->examine(target
);
502 static int jtag_enable_callback(enum jtag_event event
, void *priv
)
504 struct target
*target
= priv
;
506 if (event
!= JTAG_TAP_EVENT_ENABLE
|| !target
->tap
->enabled
)
509 jtag_unregister_event_callback(jtag_enable_callback
, target
);
510 return target_examine_one(target
);
514 /* Targets that correctly implement init + examine, i.e.
515 * no communication with target during init:
519 int target_examine(void)
521 int retval
= ERROR_OK
;
522 struct target
*target
;
524 for (target
= all_targets
; target
; target
= target
->next
)
526 /* defer examination, but don't skip it */
527 if (!target
->tap
->enabled
) {
528 jtag_register_event_callback(jtag_enable_callback
,
532 if ((retval
= target_examine_one(target
)) != ERROR_OK
)
537 const char *target_type_name(struct target
*target
)
539 return target
->type
->name
;
542 static int target_write_memory_imp(struct target
*target
, uint32_t address
, uint32_t size
, uint32_t count
, uint8_t *buffer
)
544 if (!target_was_examined(target
))
546 LOG_ERROR("Target not examined yet");
549 return target
->type
->write_memory_imp(target
, address
, size
, count
, buffer
);
552 static int target_read_memory_imp(struct target
*target
, uint32_t address
, uint32_t size
, uint32_t count
, uint8_t *buffer
)
554 if (!target_was_examined(target
))
556 LOG_ERROR("Target not examined yet");
559 return target
->type
->read_memory_imp(target
, address
, size
, count
, buffer
);
562 static int target_soft_reset_halt_imp(struct target
*target
)
564 if (!target_was_examined(target
))
566 LOG_ERROR("Target not examined yet");
569 if (!target
->type
->soft_reset_halt_imp
) {
570 LOG_ERROR("Target %s does not support soft_reset_halt",
571 target_name(target
));
574 return target
->type
->soft_reset_halt_imp(target
);
577 static int target_run_algorithm_imp(struct target
*target
, int num_mem_params
, struct mem_param
*mem_params
, int num_reg_params
, struct reg_param
*reg_param
, uint32_t entry_point
, uint32_t exit_point
, int timeout_ms
, void *arch_info
)
579 if (!target_was_examined(target
))
581 LOG_ERROR("Target not examined yet");
584 return target
->type
->run_algorithm_imp(target
, num_mem_params
, mem_params
, num_reg_params
, reg_param
, entry_point
, exit_point
, timeout_ms
, arch_info
);
587 int target_read_memory(struct target
*target
,
588 uint32_t address
, uint32_t size
, uint32_t count
, uint8_t *buffer
)
590 return target
->type
->read_memory(target
, address
, size
, count
, buffer
);
593 int target_read_phys_memory(struct target
*target
,
594 uint32_t address
, uint32_t size
, uint32_t count
, uint8_t *buffer
)
596 return target
->type
->read_phys_memory(target
, address
, size
, count
, buffer
);
599 int target_write_memory(struct target
*target
,
600 uint32_t address
, uint32_t size
, uint32_t count
, uint8_t *buffer
)
602 return target
->type
->write_memory(target
, address
, size
, count
, buffer
);
605 int target_write_phys_memory(struct target
*target
,
606 uint32_t address
, uint32_t size
, uint32_t count
, uint8_t *buffer
)
608 return target
->type
->write_phys_memory(target
, address
, size
, count
, buffer
);
611 int target_bulk_write_memory(struct target
*target
,
612 uint32_t address
, uint32_t count
, uint8_t *buffer
)
614 return target
->type
->bulk_write_memory(target
, address
, count
, buffer
);
617 int target_add_breakpoint(struct target
*target
,
618 struct breakpoint
*breakpoint
)
620 if (target
->state
!= TARGET_HALTED
) {
621 LOG_WARNING("target %s is not halted", target
->cmd_name
);
622 return ERROR_TARGET_NOT_HALTED
;
624 return target
->type
->add_breakpoint(target
, breakpoint
);
626 int target_remove_breakpoint(struct target
*target
,
627 struct breakpoint
*breakpoint
)
629 return target
->type
->remove_breakpoint(target
, breakpoint
);
632 int target_add_watchpoint(struct target
*target
,
633 struct watchpoint
*watchpoint
)
635 if (target
->state
!= TARGET_HALTED
) {
636 LOG_WARNING("target %s is not halted", target
->cmd_name
);
637 return ERROR_TARGET_NOT_HALTED
;
639 return target
->type
->add_watchpoint(target
, watchpoint
);
641 int target_remove_watchpoint(struct target
*target
,
642 struct watchpoint
*watchpoint
)
644 return target
->type
->remove_watchpoint(target
, watchpoint
);
647 int target_get_gdb_reg_list(struct target
*target
,
648 struct reg
**reg_list
[], int *reg_list_size
)
650 return target
->type
->get_gdb_reg_list(target
, reg_list
, reg_list_size
);
652 int target_step(struct target
*target
,
653 int current
, uint32_t address
, int handle_breakpoints
)
655 return target
->type
->step(target
, current
, address
, handle_breakpoints
);
659 int target_run_algorithm(struct target
*target
,
660 int num_mem_params
, struct mem_param
*mem_params
,
661 int num_reg_params
, struct reg_param
*reg_param
,
662 uint32_t entry_point
, uint32_t exit_point
,
663 int timeout_ms
, void *arch_info
)
665 return target
->type
->run_algorithm(target
,
666 num_mem_params
, mem_params
, num_reg_params
, reg_param
,
667 entry_point
, exit_point
, timeout_ms
, arch_info
);
671 * Reset the @c examined flag for the given target.
672 * Pure paranoia -- targets are zeroed on allocation.
674 static void target_reset_examined(struct target
*target
)
676 target
->examined
= false;
680 err_read_phys_memory(struct target
*target
, uint32_t address
,
681 uint32_t size
, uint32_t count
, uint8_t *buffer
)
683 LOG_ERROR("Not implemented: %s", __func__
);
688 err_write_phys_memory(struct target
*target
, uint32_t address
,
689 uint32_t size
, uint32_t count
, uint8_t *buffer
)
691 LOG_ERROR("Not implemented: %s", __func__
);
695 static int handle_target(void *priv
);
697 static int target_init_one(struct command_context
*cmd_ctx
,
698 struct target
*target
)
700 target_reset_examined(target
);
702 struct target_type
*type
= target
->type
;
703 if (type
->examine
== NULL
)
704 type
->examine
= default_examine
;
706 int retval
= type
->init_target(cmd_ctx
, target
);
707 if (ERROR_OK
!= retval
)
709 LOG_ERROR("target '%s' init failed", target_name(target
));
714 * @todo get rid of those *memory_imp() methods, now that all
715 * callers are using target_*_memory() accessors ... and make
716 * sure the "physical" paths handle the same issues.
718 /* a non-invasive way(in terms of patches) to add some code that
719 * runs before the type->write/read_memory implementation
721 type
->write_memory_imp
= target
->type
->write_memory
;
722 type
->write_memory
= target_write_memory_imp
;
724 type
->read_memory_imp
= target
->type
->read_memory
;
725 type
->read_memory
= target_read_memory_imp
;
727 type
->soft_reset_halt_imp
= target
->type
->soft_reset_halt
;
728 type
->soft_reset_halt
= target_soft_reset_halt_imp
;
730 type
->run_algorithm_imp
= target
->type
->run_algorithm
;
731 type
->run_algorithm
= target_run_algorithm_imp
;
733 /* Sanity-check MMU support ... stub in what we must, to help
734 * implement it in stages, but warn if we need to do so.
738 if (type
->write_phys_memory
== NULL
)
740 LOG_ERROR("type '%s' is missing write_phys_memory",
742 type
->write_phys_memory
= err_write_phys_memory
;
744 if (type
->read_phys_memory
== NULL
)
746 LOG_ERROR("type '%s' is missing read_phys_memory",
748 type
->read_phys_memory
= err_read_phys_memory
;
750 if (type
->virt2phys
== NULL
)
752 LOG_ERROR("type '%s' is missing virt2phys", type
->name
);
753 type
->virt2phys
= identity_virt2phys
;
758 /* Make sure no-MMU targets all behave the same: make no
759 * distinction between physical and virtual addresses, and
760 * ensure that virt2phys() is always an identity mapping.
762 if (type
->write_phys_memory
|| type
->read_phys_memory
765 LOG_WARNING("type '%s' has bad MMU hooks", type
->name
);
769 type
->write_phys_memory
= type
->write_memory
;
770 type
->read_phys_memory
= type
->read_memory
;
771 type
->virt2phys
= identity_virt2phys
;
776 int target_init(struct command_context
*cmd_ctx
)
778 struct target
*target
;
781 for (target
= all_targets
; target
; target
= target
->next
)
783 retval
= target_init_one(cmd_ctx
, target
);
784 if (ERROR_OK
!= retval
)
791 retval
= target_register_user_commands(cmd_ctx
);
792 if (ERROR_OK
!= retval
)
795 retval
= target_register_timer_callback(&handle_target
,
796 100, 1, cmd_ctx
->interp
);
797 if (ERROR_OK
!= retval
)
803 COMMAND_HANDLER(handle_target_init_command
)
806 return ERROR_COMMAND_SYNTAX_ERROR
;
808 static bool target_initialized
= false;
809 if (target_initialized
)
811 LOG_INFO("'target init' has already been called");
814 target_initialized
= true;
816 LOG_DEBUG("Initializing targets...");
817 return target_init(CMD_CTX
);
820 int target_register_event_callback(int (*callback
)(struct target
*target
, enum target_event event
, void *priv
), void *priv
)
822 struct target_event_callback
**callbacks_p
= &target_event_callbacks
;
824 if (callback
== NULL
)
826 return ERROR_INVALID_ARGUMENTS
;
831 while ((*callbacks_p
)->next
)
832 callbacks_p
= &((*callbacks_p
)->next
);
833 callbacks_p
= &((*callbacks_p
)->next
);
836 (*callbacks_p
) = malloc(sizeof(struct target_event_callback
));
837 (*callbacks_p
)->callback
= callback
;
838 (*callbacks_p
)->priv
= priv
;
839 (*callbacks_p
)->next
= NULL
;
844 int target_register_timer_callback(int (*callback
)(void *priv
), int time_ms
, int periodic
, void *priv
)
846 struct target_timer_callback
**callbacks_p
= &target_timer_callbacks
;
849 if (callback
== NULL
)
851 return ERROR_INVALID_ARGUMENTS
;
856 while ((*callbacks_p
)->next
)
857 callbacks_p
= &((*callbacks_p
)->next
);
858 callbacks_p
= &((*callbacks_p
)->next
);
861 (*callbacks_p
) = malloc(sizeof(struct target_timer_callback
));
862 (*callbacks_p
)->callback
= callback
;
863 (*callbacks_p
)->periodic
= periodic
;
864 (*callbacks_p
)->time_ms
= time_ms
;
866 gettimeofday(&now
, NULL
);
867 (*callbacks_p
)->when
.tv_usec
= now
.tv_usec
+ (time_ms
% 1000) * 1000;
868 time_ms
-= (time_ms
% 1000);
869 (*callbacks_p
)->when
.tv_sec
= now
.tv_sec
+ (time_ms
/ 1000);
870 if ((*callbacks_p
)->when
.tv_usec
> 1000000)
872 (*callbacks_p
)->when
.tv_usec
= (*callbacks_p
)->when
.tv_usec
- 1000000;
873 (*callbacks_p
)->when
.tv_sec
+= 1;
876 (*callbacks_p
)->priv
= priv
;
877 (*callbacks_p
)->next
= NULL
;
882 int target_unregister_event_callback(int (*callback
)(struct target
*target
, enum target_event event
, void *priv
), void *priv
)
884 struct target_event_callback
**p
= &target_event_callbacks
;
885 struct target_event_callback
*c
= target_event_callbacks
;
887 if (callback
== NULL
)
889 return ERROR_INVALID_ARGUMENTS
;
894 struct target_event_callback
*next
= c
->next
;
895 if ((c
->callback
== callback
) && (c
->priv
== priv
))
909 int target_unregister_timer_callback(int (*callback
)(void *priv
), void *priv
)
911 struct target_timer_callback
**p
= &target_timer_callbacks
;
912 struct target_timer_callback
*c
= target_timer_callbacks
;
914 if (callback
== NULL
)
916 return ERROR_INVALID_ARGUMENTS
;
921 struct target_timer_callback
*next
= c
->next
;
922 if ((c
->callback
== callback
) && (c
->priv
== priv
))
936 int target_call_event_callbacks(struct target
*target
, enum target_event event
)
938 struct target_event_callback
*callback
= target_event_callbacks
;
939 struct target_event_callback
*next_callback
;
941 if (event
== TARGET_EVENT_HALTED
)
943 /* execute early halted first */
944 target_call_event_callbacks(target
, TARGET_EVENT_GDB_HALT
);
947 LOG_DEBUG("target event %i (%s)",
949 Jim_Nvp_value2name_simple(nvp_target_event
, event
)->name
);
951 target_handle_event(target
, event
);
955 next_callback
= callback
->next
;
956 callback
->callback(target
, event
, callback
->priv
);
957 callback
= next_callback
;
963 static int target_timer_callback_periodic_restart(
964 struct target_timer_callback
*cb
, struct timeval
*now
)
966 int time_ms
= cb
->time_ms
;
967 cb
->when
.tv_usec
= now
->tv_usec
+ (time_ms
% 1000) * 1000;
968 time_ms
-= (time_ms
% 1000);
969 cb
->when
.tv_sec
= now
->tv_sec
+ time_ms
/ 1000;
970 if (cb
->when
.tv_usec
> 1000000)
972 cb
->when
.tv_usec
= cb
->when
.tv_usec
- 1000000;
973 cb
->when
.tv_sec
+= 1;
978 static int target_call_timer_callback(struct target_timer_callback
*cb
,
981 cb
->callback(cb
->priv
);
984 return target_timer_callback_periodic_restart(cb
, now
);
986 return target_unregister_timer_callback(cb
->callback
, cb
->priv
);
989 static int target_call_timer_callbacks_check_time(int checktime
)
994 gettimeofday(&now
, NULL
);
996 struct target_timer_callback
*callback
= target_timer_callbacks
;
999 // cleaning up may unregister and free this callback
1000 struct target_timer_callback
*next_callback
= callback
->next
;
1002 bool call_it
= callback
->callback
&&
1003 ((!checktime
&& callback
->periodic
) ||
1004 now
.tv_sec
> callback
->when
.tv_sec
||
1005 (now
.tv_sec
== callback
->when
.tv_sec
&&
1006 now
.tv_usec
>= callback
->when
.tv_usec
));
1010 int retval
= target_call_timer_callback(callback
, &now
);
1011 if (retval
!= ERROR_OK
)
1015 callback
= next_callback
;
1021 int target_call_timer_callbacks(void)
1023 return target_call_timer_callbacks_check_time(1);
1026 /* invoke periodic callbacks immediately */
1027 int target_call_timer_callbacks_now(void)
1029 return target_call_timer_callbacks_check_time(0);
1032 int target_alloc_working_area(struct target
*target
, uint32_t size
, struct working_area
**area
)
1034 struct working_area
*c
= target
->working_areas
;
1035 struct working_area
*new_wa
= NULL
;
1037 /* Reevaluate working area address based on MMU state*/
1038 if (target
->working_areas
== NULL
)
1043 retval
= target
->type
->mmu(target
, &enabled
);
1044 if (retval
!= ERROR_OK
)
1050 if (target
->working_area_phys_spec
) {
1051 LOG_DEBUG("MMU disabled, using physical "
1052 "address for working memory 0x%08x",
1053 (unsigned)target
->working_area_phys
);
1054 target
->working_area
= target
->working_area_phys
;
1056 LOG_ERROR("No working memory available. "
1057 "Specify -work-area-phys to target.");
1058 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
1061 if (target
->working_area_virt_spec
) {
1062 LOG_DEBUG("MMU enabled, using virtual "
1063 "address for working memory 0x%08x",
1064 (unsigned)target
->working_area_virt
);
1065 target
->working_area
= target
->working_area_virt
;
1067 LOG_ERROR("No working memory available. "
1068 "Specify -work-area-virt to target.");
1069 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
1074 /* only allocate multiples of 4 byte */
1077 LOG_ERROR("BUG: code tried to allocate unaligned number of bytes (0x%08x), padding", ((unsigned)(size
)));
1078 size
= (size
+ 3) & (~3);
1081 /* see if there's already a matching working area */
1084 if ((c
->free
) && (c
->size
== size
))
1092 /* if not, allocate a new one */
1095 struct working_area
**p
= &target
->working_areas
;
1096 uint32_t first_free
= target
->working_area
;
1097 uint32_t free_size
= target
->working_area_size
;
1099 c
= target
->working_areas
;
1102 first_free
+= c
->size
;
1103 free_size
-= c
->size
;
1108 if (free_size
< size
)
1110 LOG_WARNING("not enough working area available(requested %u, free %u)",
1111 (unsigned)(size
), (unsigned)(free_size
));
1112 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
1115 LOG_DEBUG("allocated new working area at address 0x%08x", (unsigned)first_free
);
1117 new_wa
= malloc(sizeof(struct working_area
));
1118 new_wa
->next
= NULL
;
1119 new_wa
->size
= size
;
1120 new_wa
->address
= first_free
;
1122 if (target
->backup_working_area
)
1125 new_wa
->backup
= malloc(new_wa
->size
);
1126 if ((retval
= target_read_memory(target
, new_wa
->address
, 4, new_wa
->size
/ 4, new_wa
->backup
)) != ERROR_OK
)
1128 free(new_wa
->backup
);
1135 new_wa
->backup
= NULL
;
1138 /* put new entry in list */
1142 /* mark as used, and return the new (reused) area */
1147 new_wa
->user
= area
;
1152 int target_free_working_area_restore(struct target
*target
, struct working_area
*area
, int restore
)
1157 if (restore
&& target
->backup_working_area
)
1160 if ((retval
= target_write_memory(target
, area
->address
, 4, area
->size
/ 4, area
->backup
)) != ERROR_OK
)
1166 /* mark user pointer invalid */
1173 int target_free_working_area(struct target
*target
, struct working_area
*area
)
1175 return target_free_working_area_restore(target
, area
, 1);
1178 /* free resources and restore memory, if restoring memory fails,
1179 * free up resources anyway
1181 void target_free_all_working_areas_restore(struct target
*target
, int restore
)
1183 struct working_area
*c
= target
->working_areas
;
1187 struct working_area
*next
= c
->next
;
1188 target_free_working_area_restore(target
, c
, restore
);
1198 target
->working_areas
= NULL
;
1201 void target_free_all_working_areas(struct target
*target
)
1203 target_free_all_working_areas_restore(target
, 1);
1206 int target_arch_state(struct target
*target
)
1211 LOG_USER("No target has been configured");
1215 LOG_USER("target state: %s", target_state_name( target
));
1217 if (target
->state
!= TARGET_HALTED
)
1220 retval
= target
->type
->arch_state(target
);
1224 /* Single aligned words are guaranteed to use 16 or 32 bit access
1225 * mode respectively, otherwise data is handled as quickly as
1228 int target_write_buffer(struct target
*target
, uint32_t address
, uint32_t size
, uint8_t *buffer
)
1231 LOG_DEBUG("writing buffer of %i byte at 0x%8.8x",
1232 (int)size
, (unsigned)address
);
1234 if (!target_was_examined(target
))
1236 LOG_ERROR("Target not examined yet");
1244 if ((address
+ size
- 1) < address
)
1246 /* GDB can request this when e.g. PC is 0xfffffffc*/
1247 LOG_ERROR("address + size wrapped(0x%08x, 0x%08x)",
1253 if (((address
% 2) == 0) && (size
== 2))
1255 return target_write_memory(target
, address
, 2, 1, buffer
);
1258 /* handle unaligned head bytes */
1261 uint32_t unaligned
= 4 - (address
% 4);
1263 if (unaligned
> size
)
1266 if ((retval
= target_write_memory(target
, address
, 1, unaligned
, buffer
)) != ERROR_OK
)
1269 buffer
+= unaligned
;
1270 address
+= unaligned
;
1274 /* handle aligned words */
1277 int aligned
= size
- (size
% 4);
1279 /* use bulk writes above a certain limit. This may have to be changed */
1282 if ((retval
= target
->type
->bulk_write_memory(target
, address
, aligned
/ 4, buffer
)) != ERROR_OK
)
1287 if ((retval
= target_write_memory(target
, address
, 4, aligned
/ 4, buffer
)) != ERROR_OK
)
1296 /* handle tail writes of less than 4 bytes */
1299 if ((retval
= target_write_memory(target
, address
, 1, size
, buffer
)) != ERROR_OK
)
1306 /* Single aligned words are guaranteed to use 16 or 32 bit access
1307 * mode respectively, otherwise data is handled as quickly as
1310 int target_read_buffer(struct target
*target
, uint32_t address
, uint32_t size
, uint8_t *buffer
)
1313 LOG_DEBUG("reading buffer of %i byte at 0x%8.8x",
1314 (int)size
, (unsigned)address
);
1316 if (!target_was_examined(target
))
1318 LOG_ERROR("Target not examined yet");
1326 if ((address
+ size
- 1) < address
)
1328 /* GDB can request this when e.g. PC is 0xfffffffc*/
1329 LOG_ERROR("address + size wrapped(0x%08" PRIx32
", 0x%08" PRIx32
")",
1335 if (((address
% 2) == 0) && (size
== 2))
1337 return target_read_memory(target
, address
, 2, 1, buffer
);
1340 /* handle unaligned head bytes */
1343 uint32_t unaligned
= 4 - (address
% 4);
1345 if (unaligned
> size
)
1348 if ((retval
= target_read_memory(target
, address
, 1, unaligned
, buffer
)) != ERROR_OK
)
1351 buffer
+= unaligned
;
1352 address
+= unaligned
;
1356 /* handle aligned words */
1359 int aligned
= size
- (size
% 4);
1361 if ((retval
= target_read_memory(target
, address
, 4, aligned
/ 4, buffer
)) != ERROR_OK
)
1369 /*prevent byte access when possible (avoid AHB access limitations in some cases)*/
1372 int aligned
= size
- (size
%2);
1373 retval
= target_read_memory(target
, address
, 2, aligned
/ 2, buffer
);
1374 if (retval
!= ERROR_OK
)
1381 /* handle tail writes of less than 4 bytes */
1384 if ((retval
= target_read_memory(target
, address
, 1, size
, buffer
)) != ERROR_OK
)
1391 int target_checksum_memory(struct target
*target
, uint32_t address
, uint32_t size
, uint32_t* crc
)
1396 uint32_t checksum
= 0;
1397 if (!target_was_examined(target
))
1399 LOG_ERROR("Target not examined yet");
1403 if ((retval
= target
->type
->checksum_memory(target
, address
,
1404 size
, &checksum
)) != ERROR_OK
)
1406 buffer
= malloc(size
);
1409 LOG_ERROR("error allocating buffer for section (%d bytes)", (int)size
);
1410 return ERROR_INVALID_ARGUMENTS
;
1412 retval
= target_read_buffer(target
, address
, size
, buffer
);
1413 if (retval
!= ERROR_OK
)
1419 /* convert to target endianess */
1420 for (i
= 0; i
< (size
/sizeof(uint32_t)); i
++)
1422 uint32_t target_data
;
1423 target_data
= target_buffer_get_u32(target
, &buffer
[i
*sizeof(uint32_t)]);
1424 target_buffer_set_u32(target
, &buffer
[i
*sizeof(uint32_t)], target_data
);
1427 retval
= image_calculate_checksum(buffer
, size
, &checksum
);
1436 int target_blank_check_memory(struct target
*target
, uint32_t address
, uint32_t size
, uint32_t* blank
)
1439 if (!target_was_examined(target
))
1441 LOG_ERROR("Target not examined yet");
1445 if (target
->type
->blank_check_memory
== 0)
1446 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
1448 retval
= target
->type
->blank_check_memory(target
, address
, size
, blank
);
1453 int target_read_u32(struct target
*target
, uint32_t address
, uint32_t *value
)
1455 uint8_t value_buf
[4];
1456 if (!target_was_examined(target
))
1458 LOG_ERROR("Target not examined yet");
1462 int retval
= target_read_memory(target
, address
, 4, 1, value_buf
);
1464 if (retval
== ERROR_OK
)
1466 *value
= target_buffer_get_u32(target
, value_buf
);
1467 LOG_DEBUG("address: 0x%8.8" PRIx32
", value: 0x%8.8" PRIx32
"",
1474 LOG_DEBUG("address: 0x%8.8" PRIx32
" failed",
1481 int target_read_u16(struct target
*target
, uint32_t address
, uint16_t *value
)
1483 uint8_t value_buf
[2];
1484 if (!target_was_examined(target
))
1486 LOG_ERROR("Target not examined yet");
1490 int retval
= target_read_memory(target
, address
, 2, 1, value_buf
);
1492 if (retval
== ERROR_OK
)
1494 *value
= target_buffer_get_u16(target
, value_buf
);
1495 LOG_DEBUG("address: 0x%8.8" PRIx32
", value: 0x%4.4x",
1502 LOG_DEBUG("address: 0x%8.8" PRIx32
" failed",
1509 int target_read_u8(struct target
*target
, uint32_t address
, uint8_t *value
)
1511 int retval
= target_read_memory(target
, address
, 1, 1, value
);
1512 if (!target_was_examined(target
))
1514 LOG_ERROR("Target not examined yet");
1518 if (retval
== ERROR_OK
)
1520 LOG_DEBUG("address: 0x%8.8" PRIx32
", value: 0x%2.2x",
1527 LOG_DEBUG("address: 0x%8.8" PRIx32
" failed",
1534 int target_write_u32(struct target
*target
, uint32_t address
, uint32_t value
)
1537 uint8_t value_buf
[4];
1538 if (!target_was_examined(target
))
1540 LOG_ERROR("Target not examined yet");
1544 LOG_DEBUG("address: 0x%8.8" PRIx32
", value: 0x%8.8" PRIx32
"",
1548 target_buffer_set_u32(target
, value_buf
, value
);
1549 if ((retval
= target_write_memory(target
, address
, 4, 1, value_buf
)) != ERROR_OK
)
1551 LOG_DEBUG("failed: %i", retval
);
1557 int target_write_u16(struct target
*target
, uint32_t address
, uint16_t value
)
1560 uint8_t value_buf
[2];
1561 if (!target_was_examined(target
))
1563 LOG_ERROR("Target not examined yet");
1567 LOG_DEBUG("address: 0x%8.8" PRIx32
", value: 0x%8.8x",
1571 target_buffer_set_u16(target
, value_buf
, value
);
1572 if ((retval
= target_write_memory(target
, address
, 2, 1, value_buf
)) != ERROR_OK
)
1574 LOG_DEBUG("failed: %i", retval
);
1580 int target_write_u8(struct target
*target
, uint32_t address
, uint8_t value
)
1583 if (!target_was_examined(target
))
1585 LOG_ERROR("Target not examined yet");
1589 LOG_DEBUG("address: 0x%8.8" PRIx32
", value: 0x%2.2x",
1592 if ((retval
= target_write_memory(target
, address
, 1, 1, &value
)) != ERROR_OK
)
1594 LOG_DEBUG("failed: %i", retval
);
1600 COMMAND_HANDLER(handle_targets_command
)
1602 struct target
*target
= all_targets
;
1606 target
= get_target(CMD_ARGV
[0]);
1607 if (target
== NULL
) {
1608 command_print(CMD_CTX
,"Target: %s is unknown, try one of:\n", CMD_ARGV
[0]);
1611 if (!target
->tap
->enabled
) {
1612 command_print(CMD_CTX
,"Target: TAP %s is disabled, "
1613 "can't be the current target\n",
1614 target
->tap
->dotted_name
);
1618 CMD_CTX
->current_target
= target
->target_number
;
1623 target
= all_targets
;
1624 command_print(CMD_CTX
, " TargetName Type Endian TapName State ");
1625 command_print(CMD_CTX
, "-- ------------------ ---------- ------ ------------------ ------------");
1631 if (target
->tap
->enabled
)
1632 state
= target_state_name( target
);
1634 state
= "tap-disabled";
1636 if (CMD_CTX
->current_target
== target
->target_number
)
1639 /* keep columns lined up to match the headers above */
1640 command_print(CMD_CTX
, "%2d%c %-18s %-10s %-6s %-18s %s",
1641 target
->target_number
,
1643 target_name(target
),
1644 target_type_name(target
),
1645 Jim_Nvp_value2name_simple(nvp_target_endian
,
1646 target
->endianness
)->name
,
1647 target
->tap
->dotted_name
,
1649 target
= target
->next
;
1655 /* every 300ms we check for reset & powerdropout and issue a "reset halt" if so. */
1657 static int powerDropout
;
1658 static int srstAsserted
;
1660 static int runPowerRestore
;
1661 static int runPowerDropout
;
1662 static int runSrstAsserted
;
1663 static int runSrstDeasserted
;
1665 static int sense_handler(void)
1667 static int prevSrstAsserted
= 0;
1668 static int prevPowerdropout
= 0;
1671 if ((retval
= jtag_power_dropout(&powerDropout
)) != ERROR_OK
)
1675 powerRestored
= prevPowerdropout
&& !powerDropout
;
1678 runPowerRestore
= 1;
1681 long long current
= timeval_ms();
1682 static long long lastPower
= 0;
1683 int waitMore
= lastPower
+ 2000 > current
;
1684 if (powerDropout
&& !waitMore
)
1686 runPowerDropout
= 1;
1687 lastPower
= current
;
1690 if ((retval
= jtag_srst_asserted(&srstAsserted
)) != ERROR_OK
)
1694 srstDeasserted
= prevSrstAsserted
&& !srstAsserted
;
1696 static long long lastSrst
= 0;
1697 waitMore
= lastSrst
+ 2000 > current
;
1698 if (srstDeasserted
&& !waitMore
)
1700 runSrstDeasserted
= 1;
1704 if (!prevSrstAsserted
&& srstAsserted
)
1706 runSrstAsserted
= 1;
1709 prevSrstAsserted
= srstAsserted
;
1710 prevPowerdropout
= powerDropout
;
1712 if (srstDeasserted
|| powerRestored
)
1714 /* Other than logging the event we can't do anything here.
1715 * Issuing a reset is a particularly bad idea as we might
1716 * be inside a reset already.
1723 static void target_call_event_callbacks_all(enum target_event e
) {
1724 struct target
*target
;
1725 target
= all_targets
;
1727 target_call_event_callbacks(target
, e
);
1728 target
= target
->next
;
1732 /* process target state changes */
1733 static int handle_target(void *priv
)
1735 Jim_Interp
*interp
= (Jim_Interp
*)priv
;
1736 int retval
= ERROR_OK
;
1738 /* we do not want to recurse here... */
1739 static int recursive
= 0;
1744 /* danger! running these procedures can trigger srst assertions and power dropouts.
1745 * We need to avoid an infinite loop/recursion here and we do that by
1746 * clearing the flags after running these events.
1748 int did_something
= 0;
1749 if (runSrstAsserted
)
1751 target_call_event_callbacks_all(TARGET_EVENT_GDB_HALT
);
1752 Jim_Eval(interp
, "srst_asserted");
1755 if (runSrstDeasserted
)
1757 Jim_Eval(interp
, "srst_deasserted");
1760 if (runPowerDropout
)
1762 target_call_event_callbacks_all(TARGET_EVENT_GDB_HALT
);
1763 Jim_Eval(interp
, "power_dropout");
1766 if (runPowerRestore
)
1768 Jim_Eval(interp
, "power_restore");
1774 /* clear detect flags */
1778 /* clear action flags */
1780 runSrstAsserted
= 0;
1781 runSrstDeasserted
= 0;
1782 runPowerRestore
= 0;
1783 runPowerDropout
= 0;
1788 /* Poll targets for state changes unless that's globally disabled.
1789 * Skip targets that are currently disabled.
1791 for (struct target
*target
= all_targets
;
1792 is_jtag_poll_safe() && target
;
1793 target
= target
->next
)
1795 if (!target
->tap
->enabled
)
1798 /* only poll target if we've got power and srst isn't asserted */
1799 if (!powerDropout
&& !srstAsserted
)
1801 /* polling may fail silently until the target has been examined */
1802 if ((retval
= target_poll(target
)) != ERROR_OK
)
1804 target_call_event_callbacks(target
, TARGET_EVENT_GDB_HALT
);
1813 COMMAND_HANDLER(handle_reg_command
)
1815 struct target
*target
;
1816 struct reg
*reg
= NULL
;
1822 target
= get_current_target(CMD_CTX
);
1824 /* list all available registers for the current target */
1827 struct reg_cache
*cache
= target
->reg_cache
;
1834 command_print(CMD_CTX
, "===== %s", cache
->name
);
1836 for (i
= 0, reg
= cache
->reg_list
;
1837 i
< cache
->num_regs
;
1838 i
++, reg
++, count
++)
1840 /* only print cached values if they are valid */
1842 value
= buf_to_str(reg
->value
,
1844 command_print(CMD_CTX
,
1845 "(%i) %s (/%" PRIu32
"): 0x%s%s",
1853 command_print(CMD_CTX
, "(%i) %s (/%" PRIu32
")",
1858 cache
= cache
->next
;
1864 /* access a single register by its ordinal number */
1865 if ((CMD_ARGV
[0][0] >= '0') && (CMD_ARGV
[0][0] <= '9'))
1868 COMMAND_PARSE_NUMBER(uint
, CMD_ARGV
[0], num
);
1870 struct reg_cache
*cache
= target
->reg_cache
;
1875 for (i
= 0; i
< cache
->num_regs
; i
++)
1879 reg
= &cache
->reg_list
[i
];
1885 cache
= cache
->next
;
1890 command_print(CMD_CTX
, "%i is out of bounds, the current target has only %i registers (0 - %i)", num
, count
, count
- 1);
1893 } else /* access a single register by its name */
1895 reg
= register_get_by_name(target
->reg_cache
, CMD_ARGV
[0], 1);
1899 command_print(CMD_CTX
, "register %s not found in current target", CMD_ARGV
[0]);
1904 /* display a register */
1905 if ((CMD_ARGC
== 1) || ((CMD_ARGC
== 2) && !((CMD_ARGV
[1][0] >= '0') && (CMD_ARGV
[1][0] <= '9'))))
1907 if ((CMD_ARGC
== 2) && (strcmp(CMD_ARGV
[1], "force") == 0))
1910 if (reg
->valid
== 0)
1912 reg
->type
->get(reg
);
1914 value
= buf_to_str(reg
->value
, reg
->size
, 16);
1915 command_print(CMD_CTX
, "%s (/%i): 0x%s", reg
->name
, (int)(reg
->size
), value
);
1920 /* set register value */
1923 uint8_t *buf
= malloc(DIV_ROUND_UP(reg
->size
, 8));
1924 str_to_buf(CMD_ARGV
[1], strlen(CMD_ARGV
[1]), buf
, reg
->size
, 0);
1926 reg
->type
->set(reg
, buf
);
1928 value
= buf_to_str(reg
->value
, reg
->size
, 16);
1929 command_print(CMD_CTX
, "%s (/%i): 0x%s", reg
->name
, (int)(reg
->size
), value
);
1937 command_print(CMD_CTX
, "usage: reg <#|name> [value]");
1942 COMMAND_HANDLER(handle_poll_command
)
1944 int retval
= ERROR_OK
;
1945 struct target
*target
= get_current_target(CMD_CTX
);
1949 command_print(CMD_CTX
, "background polling: %s",
1950 jtag_poll_get_enabled() ? "on" : "off");
1951 command_print(CMD_CTX
, "TAP: %s (%s)",
1952 target
->tap
->dotted_name
,
1953 target
->tap
->enabled
? "enabled" : "disabled");
1954 if (!target
->tap
->enabled
)
1956 if ((retval
= target_poll(target
)) != ERROR_OK
)
1958 if ((retval
= target_arch_state(target
)) != ERROR_OK
)
1961 else if (CMD_ARGC
== 1)
1964 COMMAND_PARSE_ON_OFF(CMD_ARGV
[0], enable
);
1965 jtag_poll_set_enabled(enable
);
1969 return ERROR_COMMAND_SYNTAX_ERROR
;
1975 COMMAND_HANDLER(handle_wait_halt_command
)
1978 return ERROR_COMMAND_SYNTAX_ERROR
;
1983 int retval
= parse_uint(CMD_ARGV
[0], &ms
);
1984 if (ERROR_OK
!= retval
)
1986 command_print(CMD_CTX
, "usage: %s [seconds]", CMD_NAME
);
1987 return ERROR_COMMAND_SYNTAX_ERROR
;
1989 // convert seconds (given) to milliseconds (needed)
1993 struct target
*target
= get_current_target(CMD_CTX
);
1994 return target_wait_state(target
, TARGET_HALTED
, ms
);
1997 /* wait for target state to change. The trick here is to have a low
1998 * latency for short waits and not to suck up all the CPU time
2001 * After 500ms, keep_alive() is invoked
2003 int target_wait_state(struct target
*target
, enum target_state state
, int ms
)
2006 long long then
= 0, cur
;
2011 if ((retval
= target_poll(target
)) != ERROR_OK
)
2013 if (target
->state
== state
)
2021 then
= timeval_ms();
2022 LOG_DEBUG("waiting for target %s...",
2023 Jim_Nvp_value2name_simple(nvp_target_state
,state
)->name
);
2031 if ((cur
-then
) > ms
)
2033 LOG_ERROR("timed out while waiting for target %s",
2034 Jim_Nvp_value2name_simple(nvp_target_state
,state
)->name
);
2042 COMMAND_HANDLER(handle_halt_command
)
2046 struct target
*target
= get_current_target(CMD_CTX
);
2047 int retval
= target_halt(target
);
2048 if (ERROR_OK
!= retval
)
2054 retval
= parse_uint(CMD_ARGV
[0], &wait
);
2055 if (ERROR_OK
!= retval
)
2056 return ERROR_COMMAND_SYNTAX_ERROR
;
2061 return CALL_COMMAND_HANDLER(handle_wait_halt_command
);
2064 COMMAND_HANDLER(handle_soft_reset_halt_command
)
2066 struct target
*target
= get_current_target(CMD_CTX
);
2068 LOG_USER("requesting target halt and executing a soft reset");
2070 target
->type
->soft_reset_halt(target
);
2075 COMMAND_HANDLER(handle_reset_command
)
2078 return ERROR_COMMAND_SYNTAX_ERROR
;
2080 enum target_reset_mode reset_mode
= RESET_RUN
;
2084 n
= Jim_Nvp_name2value_simple(nvp_reset_modes
, CMD_ARGV
[0]);
2085 if ((n
->name
== NULL
) || (n
->value
== RESET_UNKNOWN
)) {
2086 return ERROR_COMMAND_SYNTAX_ERROR
;
2088 reset_mode
= n
->value
;
2091 /* reset *all* targets */
2092 return target_process_reset(CMD_CTX
, reset_mode
);
2096 COMMAND_HANDLER(handle_resume_command
)
2100 return ERROR_COMMAND_SYNTAX_ERROR
;
2102 struct target
*target
= get_current_target(CMD_CTX
);
2103 target_handle_event(target
, TARGET_EVENT_OLD_pre_resume
);
2105 /* with no CMD_ARGV, resume from current pc, addr = 0,
2106 * with one arguments, addr = CMD_ARGV[0],
2107 * handle breakpoints, not debugging */
2111 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], addr
);
2115 return target_resume(target
, current
, addr
, 1, 0);
2118 COMMAND_HANDLER(handle_step_command
)
2121 return ERROR_COMMAND_SYNTAX_ERROR
;
2125 /* with no CMD_ARGV, step from current pc, addr = 0,
2126 * with one argument addr = CMD_ARGV[0],
2127 * handle breakpoints, debugging */
2132 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], addr
);
2136 struct target
*target
= get_current_target(CMD_CTX
);
2138 return target
->type
->step(target
, current_pc
, addr
, 1);
2141 static void handle_md_output(struct command_context
*cmd_ctx
,
2142 struct target
*target
, uint32_t address
, unsigned size
,
2143 unsigned count
, const uint8_t *buffer
)
2145 const unsigned line_bytecnt
= 32;
2146 unsigned line_modulo
= line_bytecnt
/ size
;
2148 char output
[line_bytecnt
* 4 + 1];
2149 unsigned output_len
= 0;
2151 const char *value_fmt
;
2153 case 4: value_fmt
= "%8.8x "; break;
2154 case 2: value_fmt
= "%4.2x "; break;
2155 case 1: value_fmt
= "%2.2x "; break;
2157 /* "can't happen", caller checked */
2158 LOG_ERROR("invalid memory read size: %u", size
);
2162 for (unsigned i
= 0; i
< count
; i
++)
2164 if (i
% line_modulo
== 0)
2166 output_len
+= snprintf(output
+ output_len
,
2167 sizeof(output
) - output_len
,
2169 (unsigned)(address
+ (i
*size
)));
2173 const uint8_t *value_ptr
= buffer
+ i
* size
;
2175 case 4: value
= target_buffer_get_u32(target
, value_ptr
); break;
2176 case 2: value
= target_buffer_get_u16(target
, value_ptr
); break;
2177 case 1: value
= *value_ptr
;
2179 output_len
+= snprintf(output
+ output_len
,
2180 sizeof(output
) - output_len
,
2183 if ((i
% line_modulo
== line_modulo
- 1) || (i
== count
- 1))
2185 command_print(cmd_ctx
, "%s", output
);
2191 COMMAND_HANDLER(handle_md_command
)
2194 return ERROR_COMMAND_SYNTAX_ERROR
;
2197 switch (CMD_NAME
[2]) {
2198 case 'w': size
= 4; break;
2199 case 'h': size
= 2; break;
2200 case 'b': size
= 1; break;
2201 default: return ERROR_COMMAND_SYNTAX_ERROR
;
2204 bool physical
=strcmp(CMD_ARGV
[0], "phys")==0;
2205 int (*fn
)(struct target
*target
,
2206 uint32_t address
, uint32_t size
, uint32_t count
, uint8_t *buffer
);
2211 fn
=target_read_phys_memory
;
2214 fn
=target_read_memory
;
2216 if ((CMD_ARGC
< 1) || (CMD_ARGC
> 2))
2218 return ERROR_COMMAND_SYNTAX_ERROR
;
2222 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], address
);
2226 COMMAND_PARSE_NUMBER(uint
, CMD_ARGV
[1], count
);
2228 uint8_t *buffer
= calloc(count
, size
);
2230 struct target
*target
= get_current_target(CMD_CTX
);
2231 int retval
= fn(target
, address
, size
, count
, buffer
);
2232 if (ERROR_OK
== retval
)
2233 handle_md_output(CMD_CTX
, target
, address
, size
, count
, buffer
);
2240 COMMAND_HANDLER(handle_mw_command
)
2244 return ERROR_COMMAND_SYNTAX_ERROR
;
2246 bool physical
=strcmp(CMD_ARGV
[0], "phys")==0;
2247 int (*fn
)(struct target
*target
,
2248 uint32_t address
, uint32_t size
, uint32_t count
, uint8_t *buffer
);
2253 fn
=target_write_phys_memory
;
2256 fn
=target_write_memory
;
2258 if ((CMD_ARGC
< 2) || (CMD_ARGC
> 3))
2259 return ERROR_COMMAND_SYNTAX_ERROR
;
2262 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], address
);
2265 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], value
);
2269 COMMAND_PARSE_NUMBER(uint
, CMD_ARGV
[2], count
);
2271 struct target
*target
= get_current_target(CMD_CTX
);
2273 uint8_t value_buf
[4];
2274 switch (CMD_NAME
[2])
2278 target_buffer_set_u32(target
, value_buf
, value
);
2282 target_buffer_set_u16(target
, value_buf
, value
);
2286 value_buf
[0] = value
;
2289 return ERROR_COMMAND_SYNTAX_ERROR
;
2291 for (unsigned i
= 0; i
< count
; i
++)
2293 int retval
= fn(target
,
2294 address
+ i
* wordsize
, wordsize
, 1, value_buf
);
2295 if (ERROR_OK
!= retval
)
2304 static COMMAND_HELPER(parse_load_image_command_CMD_ARGV
, struct image
*image
,
2305 uint32_t *min_address
, uint32_t *max_address
)
2307 if (CMD_ARGC
< 1 || CMD_ARGC
> 5)
2308 return ERROR_COMMAND_SYNTAX_ERROR
;
2310 /* a base address isn't always necessary,
2311 * default to 0x0 (i.e. don't relocate) */
2315 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], addr
);
2316 image
->base_address
= addr
;
2317 image
->base_address_set
= 1;
2320 image
->base_address_set
= 0;
2322 image
->start_address_set
= 0;
2326 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[3], *min_address
);
2330 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[4], *max_address
);
2331 // use size (given) to find max (required)
2332 *max_address
+= *min_address
;
2335 if (*min_address
> *max_address
)
2336 return ERROR_COMMAND_SYNTAX_ERROR
;
2341 COMMAND_HANDLER(handle_load_image_command
)
2345 uint32_t image_size
;
2346 uint32_t min_address
= 0;
2347 uint32_t max_address
= 0xffffffff;
2351 int retval
= CALL_COMMAND_HANDLER(parse_load_image_command_CMD_ARGV
,
2352 &image
, &min_address
, &max_address
);
2353 if (ERROR_OK
!= retval
)
2356 struct target
*target
= get_current_target(CMD_CTX
);
2358 struct duration bench
;
2359 duration_start(&bench
);
2361 if (image_open(&image
, CMD_ARGV
[0], (CMD_ARGC
>= 3) ? CMD_ARGV
[2] : NULL
) != ERROR_OK
)
2368 for (i
= 0; i
< image
.num_sections
; i
++)
2370 buffer
= malloc(image
.sections
[i
].size
);
2373 command_print(CMD_CTX
,
2374 "error allocating buffer for section (%d bytes)",
2375 (int)(image
.sections
[i
].size
));
2379 if ((retval
= image_read_section(&image
, i
, 0x0, image
.sections
[i
].size
, buffer
, &buf_cnt
)) != ERROR_OK
)
2385 uint32_t offset
= 0;
2386 uint32_t length
= buf_cnt
;
2388 /* DANGER!!! beware of unsigned comparision here!!! */
2390 if ((image
.sections
[i
].base_address
+ buf_cnt
>= min_address
)&&
2391 (image
.sections
[i
].base_address
< max_address
))
2393 if (image
.sections
[i
].base_address
< min_address
)
2395 /* clip addresses below */
2396 offset
+= min_address
-image
.sections
[i
].base_address
;
2400 if (image
.sections
[i
].base_address
+ buf_cnt
> max_address
)
2402 length
-= (image
.sections
[i
].base_address
+ buf_cnt
)-max_address
;
2405 if ((retval
= target_write_buffer(target
, image
.sections
[i
].base_address
+ offset
, length
, buffer
+ offset
)) != ERROR_OK
)
2410 image_size
+= length
;
2411 command_print(CMD_CTX
, "%u bytes written at address 0x%8.8" PRIx32
"",
2412 (unsigned int)length
,
2413 image
.sections
[i
].base_address
+ offset
);
2419 if ((ERROR_OK
== retval
) && (duration_measure(&bench
) == ERROR_OK
))
2421 command_print(CMD_CTX
, "downloaded %" PRIu32
" bytes "
2422 "in %fs (%0.3f kb/s)", image_size
,
2423 duration_elapsed(&bench
), duration_kbps(&bench
, image_size
));
2426 image_close(&image
);
2432 COMMAND_HANDLER(handle_dump_image_command
)
2434 struct fileio fileio
;
2436 uint8_t buffer
[560];
2440 struct target
*target
= get_current_target(CMD_CTX
);
2444 command_print(CMD_CTX
, "usage: dump_image <filename> <address> <size>");
2449 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], address
);
2451 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[2], size
);
2453 if (fileio_open(&fileio
, CMD_ARGV
[0], FILEIO_WRITE
, FILEIO_BINARY
) != ERROR_OK
)
2458 struct duration bench
;
2459 duration_start(&bench
);
2461 int retval
= ERROR_OK
;
2464 size_t size_written
;
2465 uint32_t this_run_size
= (size
> 560) ? 560 : size
;
2466 retval
= target_read_buffer(target
, address
, this_run_size
, buffer
);
2467 if (retval
!= ERROR_OK
)
2472 retval
= fileio_write(&fileio
, this_run_size
, buffer
, &size_written
);
2473 if (retval
!= ERROR_OK
)
2478 size
-= this_run_size
;
2479 address
+= this_run_size
;
2482 if ((retvaltemp
= fileio_close(&fileio
)) != ERROR_OK
)
2485 if ((ERROR_OK
== retval
) && (duration_measure(&bench
) == ERROR_OK
))
2487 command_print(CMD_CTX
,
2488 "dumped %zu bytes in %fs (%0.3f kb/s)", fileio
.size
,
2489 duration_elapsed(&bench
), duration_kbps(&bench
, fileio
.size
));
2495 static COMMAND_HELPER(handle_verify_image_command_internal
, int verify
)
2499 uint32_t image_size
;
2502 uint32_t checksum
= 0;
2503 uint32_t mem_checksum
= 0;
2507 struct target
*target
= get_current_target(CMD_CTX
);
2511 return ERROR_COMMAND_SYNTAX_ERROR
;
2516 LOG_ERROR("no target selected");
2520 struct duration bench
;
2521 duration_start(&bench
);
2526 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], addr
);
2527 image
.base_address
= addr
;
2528 image
.base_address_set
= 1;
2532 image
.base_address_set
= 0;
2533 image
.base_address
= 0x0;
2536 image
.start_address_set
= 0;
2538 if ((retval
= image_open(&image
, CMD_ARGV
[0], (CMD_ARGC
== 3) ? CMD_ARGV
[2] : NULL
)) != ERROR_OK
)
2545 for (i
= 0; i
< image
.num_sections
; i
++)
2547 buffer
= malloc(image
.sections
[i
].size
);
2550 command_print(CMD_CTX
,
2551 "error allocating buffer for section (%d bytes)",
2552 (int)(image
.sections
[i
].size
));
2555 if ((retval
= image_read_section(&image
, i
, 0x0, image
.sections
[i
].size
, buffer
, &buf_cnt
)) != ERROR_OK
)
2563 /* calculate checksum of image */
2564 image_calculate_checksum(buffer
, buf_cnt
, &checksum
);
2566 retval
= target_checksum_memory(target
, image
.sections
[i
].base_address
, buf_cnt
, &mem_checksum
);
2567 if (retval
!= ERROR_OK
)
2573 if (checksum
!= mem_checksum
)
2575 /* failed crc checksum, fall back to a binary compare */
2578 command_print(CMD_CTX
, "checksum mismatch - attempting binary compare");
2580 data
= (uint8_t*)malloc(buf_cnt
);
2582 /* Can we use 32bit word accesses? */
2584 int count
= buf_cnt
;
2585 if ((count
% 4) == 0)
2590 retval
= target_read_memory(target
, image
.sections
[i
].base_address
, size
, count
, data
);
2591 if (retval
== ERROR_OK
)
2594 for (t
= 0; t
< buf_cnt
; t
++)
2596 if (data
[t
] != buffer
[t
])
2598 command_print(CMD_CTX
,
2599 "Verify operation failed address 0x%08x. Was 0x%02x instead of 0x%02x\n",
2600 (unsigned)(t
+ image
.sections
[i
].base_address
),
2605 retval
= ERROR_FAIL
;
2619 command_print(CMD_CTX
, "address 0x%08" PRIx32
" length 0x%08zx",
2620 image
.sections
[i
].base_address
,
2625 image_size
+= buf_cnt
;
2628 if ((ERROR_OK
== retval
) && (duration_measure(&bench
) == ERROR_OK
))
2630 command_print(CMD_CTX
, "verified %" PRIu32
" bytes "
2631 "in %fs (%0.3f kb/s)", image_size
,
2632 duration_elapsed(&bench
), duration_kbps(&bench
, image_size
));
2635 image_close(&image
);
2640 COMMAND_HANDLER(handle_verify_image_command
)
2642 return CALL_COMMAND_HANDLER(handle_verify_image_command_internal
, 1);
2645 COMMAND_HANDLER(handle_test_image_command
)
2647 return CALL_COMMAND_HANDLER(handle_verify_image_command_internal
, 0);
2650 static int handle_bp_command_list(struct command_context
*cmd_ctx
)
2652 struct target
*target
= get_current_target(cmd_ctx
);
2653 struct breakpoint
*breakpoint
= target
->breakpoints
;
2656 if (breakpoint
->type
== BKPT_SOFT
)
2658 char* buf
= buf_to_str(breakpoint
->orig_instr
,
2659 breakpoint
->length
, 16);
2660 command_print(cmd_ctx
, "0x%8.8" PRIx32
", 0x%x, %i, 0x%s",
2661 breakpoint
->address
,
2663 breakpoint
->set
, buf
);
2668 command_print(cmd_ctx
, "0x%8.8" PRIx32
", 0x%x, %i",
2669 breakpoint
->address
,
2670 breakpoint
->length
, breakpoint
->set
);
2673 breakpoint
= breakpoint
->next
;
2678 static int handle_bp_command_set(struct command_context
*cmd_ctx
,
2679 uint32_t addr
, uint32_t length
, int hw
)
2681 struct target
*target
= get_current_target(cmd_ctx
);
2682 int retval
= breakpoint_add(target
, addr
, length
, hw
);
2683 if (ERROR_OK
== retval
)
2684 command_print(cmd_ctx
, "breakpoint set at 0x%8.8" PRIx32
"", addr
);
2686 LOG_ERROR("Failure setting breakpoint");
2690 COMMAND_HANDLER(handle_bp_command
)
2693 return handle_bp_command_list(CMD_CTX
);
2695 if (CMD_ARGC
< 2 || CMD_ARGC
> 3)
2697 command_print(CMD_CTX
, "usage: bp <address> <length> ['hw']");
2698 return ERROR_COMMAND_SYNTAX_ERROR
;
2702 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], addr
);
2704 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], length
);
2709 if (strcmp(CMD_ARGV
[2], "hw") == 0)
2712 return ERROR_COMMAND_SYNTAX_ERROR
;
2715 return handle_bp_command_set(CMD_CTX
, addr
, length
, hw
);
2718 COMMAND_HANDLER(handle_rbp_command
)
2721 return ERROR_COMMAND_SYNTAX_ERROR
;
2724 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], addr
);
2726 struct target
*target
= get_current_target(CMD_CTX
);
2727 breakpoint_remove(target
, addr
);
2732 COMMAND_HANDLER(handle_wp_command
)
2734 struct target
*target
= get_current_target(CMD_CTX
);
2738 struct watchpoint
*watchpoint
= target
->watchpoints
;
2742 command_print(CMD_CTX
, "address: 0x%8.8" PRIx32
2743 ", len: 0x%8.8" PRIx32
2744 ", r/w/a: %i, value: 0x%8.8" PRIx32
2745 ", mask: 0x%8.8" PRIx32
,
2746 watchpoint
->address
,
2748 (int)watchpoint
->rw
,
2751 watchpoint
= watchpoint
->next
;
2756 enum watchpoint_rw type
= WPT_ACCESS
;
2758 uint32_t length
= 0;
2759 uint32_t data_value
= 0x0;
2760 uint32_t data_mask
= 0xffffffff;
2765 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[4], data_mask
);
2768 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[3], data_value
);
2771 switch (CMD_ARGV
[2][0])
2783 LOG_ERROR("invalid watchpoint mode ('%c')", CMD_ARGV
[2][0]);
2784 return ERROR_COMMAND_SYNTAX_ERROR
;
2788 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], length
);
2789 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], addr
);
2793 command_print(CMD_CTX
, "usage: wp [address length "
2794 "[(r|w|a) [value [mask]]]]");
2795 return ERROR_COMMAND_SYNTAX_ERROR
;
2798 int retval
= watchpoint_add(target
, addr
, length
, type
,
2799 data_value
, data_mask
);
2800 if (ERROR_OK
!= retval
)
2801 LOG_ERROR("Failure setting watchpoints");
2806 COMMAND_HANDLER(handle_rwp_command
)
2809 return ERROR_COMMAND_SYNTAX_ERROR
;
2812 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], addr
);
2814 struct target
*target
= get_current_target(CMD_CTX
);
2815 watchpoint_remove(target
, addr
);
2822 * Translate a virtual address to a physical address.
2824 * The low-level target implementation must have logged a detailed error
2825 * which is forwarded to telnet/GDB session.
2827 COMMAND_HANDLER(handle_virt2phys_command
)
2830 return ERROR_COMMAND_SYNTAX_ERROR
;
2833 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], va
);
2836 struct target
*target
= get_current_target(CMD_CTX
);
2837 int retval
= target
->type
->virt2phys(target
, va
, &pa
);
2838 if (retval
== ERROR_OK
)
2839 command_print(CMD_CTX
, "Physical address 0x%08" PRIx32
"", pa
);
2844 static void writeData(FILE *f
, const void *data
, size_t len
)
2846 size_t written
= fwrite(data
, 1, len
, f
);
2848 LOG_ERROR("failed to write %zu bytes: %s", len
, strerror(errno
));
2851 static void writeLong(FILE *f
, int l
)
2854 for (i
= 0; i
< 4; i
++)
2856 char c
= (l
>> (i
*8))&0xff;
2857 writeData(f
, &c
, 1);
2862 static void writeString(FILE *f
, char *s
)
2864 writeData(f
, s
, strlen(s
));
2867 /* Dump a gmon.out histogram file. */
2868 static void writeGmon(uint32_t *samples
, uint32_t sampleNum
, const char *filename
)
2871 FILE *f
= fopen(filename
, "w");
2874 writeString(f
, "gmon");
2875 writeLong(f
, 0x00000001); /* Version */
2876 writeLong(f
, 0); /* padding */
2877 writeLong(f
, 0); /* padding */
2878 writeLong(f
, 0); /* padding */
2880 uint8_t zero
= 0; /* GMON_TAG_TIME_HIST */
2881 writeData(f
, &zero
, 1);
2883 /* figure out bucket size */
2884 uint32_t min
= samples
[0];
2885 uint32_t max
= samples
[0];
2886 for (i
= 0; i
< sampleNum
; i
++)
2888 if (min
> samples
[i
])
2892 if (max
< samples
[i
])
2898 int addressSpace
= (max
-min
+ 1);
2900 static const uint32_t maxBuckets
= 256 * 1024; /* maximum buckets. */
2901 uint32_t length
= addressSpace
;
2902 if (length
> maxBuckets
)
2904 length
= maxBuckets
;
2906 int *buckets
= malloc(sizeof(int)*length
);
2907 if (buckets
== NULL
)
2912 memset(buckets
, 0, sizeof(int)*length
);
2913 for (i
= 0; i
< sampleNum
;i
++)
2915 uint32_t address
= samples
[i
];
2916 long long a
= address
-min
;
2917 long long b
= length
-1;
2918 long long c
= addressSpace
-1;
2919 int index
= (a
*b
)/c
; /* danger!!!! int32 overflows */
2923 /* append binary memory gmon.out &profile_hist_hdr ((char*)&profile_hist_hdr + sizeof(struct gmon_hist_hdr)) */
2924 writeLong(f
, min
); /* low_pc */
2925 writeLong(f
, max
); /* high_pc */
2926 writeLong(f
, length
); /* # of samples */
2927 writeLong(f
, 64000000); /* 64MHz */
2928 writeString(f
, "seconds");
2929 for (i
= 0; i
< (15-strlen("seconds")); i
++)
2930 writeData(f
, &zero
, 1);
2931 writeString(f
, "s");
2933 /*append binary memory gmon.out profile_hist_data (profile_hist_data + profile_hist_hdr.hist_size) */
2935 char *data
= malloc(2*length
);
2938 for (i
= 0; i
< length
;i
++)
2947 data
[i
*2 + 1]=(val
>> 8)&0xff;
2950 writeData(f
, data
, length
* 2);
2960 /* profiling samples the CPU PC as quickly as OpenOCD is able, which will be used as a random sampling of PC */
2961 COMMAND_HANDLER(handle_profile_command
)
2963 struct target
*target
= get_current_target(CMD_CTX
);
2964 struct timeval timeout
, now
;
2966 gettimeofday(&timeout
, NULL
);
2969 return ERROR_COMMAND_SYNTAX_ERROR
;
2972 COMMAND_PARSE_NUMBER(uint
, CMD_ARGV
[0], offset
);
2974 timeval_add_time(&timeout
, offset
, 0);
2976 command_print(CMD_CTX
, "Starting profiling. Halting and resuming the target as often as we can...");
2978 static const int maxSample
= 10000;
2979 uint32_t *samples
= malloc(sizeof(uint32_t)*maxSample
);
2980 if (samples
== NULL
)
2984 /* hopefully it is safe to cache! We want to stop/restart as quickly as possible. */
2985 struct reg
*reg
= register_get_by_name(target
->reg_cache
, "pc", 1);
2990 target_poll(target
);
2991 if (target
->state
== TARGET_HALTED
)
2993 uint32_t t
=*((uint32_t *)reg
->value
);
2994 samples
[numSamples
++]=t
;
2995 retval
= target_resume(target
, 1, 0, 0, 0); /* current pc, addr = 0, do not handle breakpoints, not debugging */
2996 target_poll(target
);
2997 alive_sleep(10); /* sleep 10ms, i.e. <100 samples/second. */
2998 } else if (target
->state
== TARGET_RUNNING
)
3000 /* We want to quickly sample the PC. */
3001 if ((retval
= target_halt(target
)) != ERROR_OK
)
3008 command_print(CMD_CTX
, "Target not halted or running");
3012 if (retval
!= ERROR_OK
)
3017 gettimeofday(&now
, NULL
);
3018 if ((numSamples
>= maxSample
) || ((now
.tv_sec
>= timeout
.tv_sec
) && (now
.tv_usec
>= timeout
.tv_usec
)))
3020 command_print(CMD_CTX
, "Profiling completed. %d samples.", numSamples
);
3021 if ((retval
= target_poll(target
)) != ERROR_OK
)
3026 if (target
->state
== TARGET_HALTED
)
3028 target_resume(target
, 1, 0, 0, 0); /* current pc, addr = 0, do not handle breakpoints, not debugging */
3030 if ((retval
= target_poll(target
)) != ERROR_OK
)
3035 writeGmon(samples
, numSamples
, CMD_ARGV
[1]);
3036 command_print(CMD_CTX
, "Wrote %s", CMD_ARGV
[1]);
3045 static int new_int_array_element(Jim_Interp
* interp
, const char *varname
, int idx
, uint32_t val
)
3048 Jim_Obj
*nameObjPtr
, *valObjPtr
;
3051 namebuf
= alloc_printf("%s(%d)", varname
, idx
);
3055 nameObjPtr
= Jim_NewStringObj(interp
, namebuf
, -1);
3056 valObjPtr
= Jim_NewIntObj(interp
, val
);
3057 if (!nameObjPtr
|| !valObjPtr
)
3063 Jim_IncrRefCount(nameObjPtr
);
3064 Jim_IncrRefCount(valObjPtr
);
3065 result
= Jim_SetVariable(interp
, nameObjPtr
, valObjPtr
);
3066 Jim_DecrRefCount(interp
, nameObjPtr
);
3067 Jim_DecrRefCount(interp
, valObjPtr
);
3069 /* printf("%s(%d) <= 0%08x\n", varname, idx, val); */
3073 static int jim_mem2array(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
3075 struct command_context
*context
;
3076 struct target
*target
;
3078 context
= Jim_GetAssocData(interp
, "context");
3079 if (context
== NULL
)
3081 LOG_ERROR("mem2array: no command context");
3084 target
= get_current_target(context
);
3087 LOG_ERROR("mem2array: no current target");
3091 return target_mem2array(interp
, target
, argc
-1, argv
+ 1);
3094 static int target_mem2array(Jim_Interp
*interp
, struct target
*target
, int argc
, Jim_Obj
*const *argv
)
3102 const char *varname
;
3106 /* argv[1] = name of array to receive the data
3107 * argv[2] = desired width
3108 * argv[3] = memory address
3109 * argv[4] = count of times to read
3112 Jim_WrongNumArgs(interp
, 1, argv
, "varname width addr nelems");
3115 varname
= Jim_GetString(argv
[0], &len
);
3116 /* given "foo" get space for worse case "foo(%d)" .. add 20 */
3118 e
= Jim_GetLong(interp
, argv
[1], &l
);
3124 e
= Jim_GetLong(interp
, argv
[2], &l
);
3129 e
= Jim_GetLong(interp
, argv
[3], &l
);
3145 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3146 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "Invalid width param, must be 8/16/32", NULL
);
3150 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3151 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "mem2array: zero width read?", NULL
);
3154 if ((addr
+ (len
* width
)) < addr
) {
3155 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3156 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "mem2array: addr + len - wraps to zero?", NULL
);
3159 /* absurd transfer size? */
3161 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3162 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "mem2array: absurd > 64K item request", NULL
);
3167 ((width
== 2) && ((addr
& 1) == 0)) ||
3168 ((width
== 4) && ((addr
& 3) == 0))) {
3172 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3173 sprintf(buf
, "mem2array address: 0x%08" PRIx32
" is not aligned for %" PRId32
" byte reads",
3176 Jim_AppendStrings(interp
, Jim_GetResult(interp
), buf
, NULL
);
3185 size_t buffersize
= 4096;
3186 uint8_t *buffer
= malloc(buffersize
);
3193 /* Slurp... in buffer size chunks */
3195 count
= len
; /* in objects.. */
3196 if (count
> (buffersize
/width
)) {
3197 count
= (buffersize
/width
);
3200 retval
= target_read_memory(target
, addr
, width
, count
, buffer
);
3201 if (retval
!= ERROR_OK
) {
3203 LOG_ERROR("mem2array: Read @ 0x%08x, w=%d, cnt=%d, failed",
3207 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3208 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "mem2array: cannot read memory", NULL
);
3212 v
= 0; /* shut up gcc */
3213 for (i
= 0 ;i
< count
;i
++, n
++) {
3216 v
= target_buffer_get_u32(target
, &buffer
[i
*width
]);
3219 v
= target_buffer_get_u16(target
, &buffer
[i
*width
]);
3222 v
= buffer
[i
] & 0x0ff;
3225 new_int_array_element(interp
, varname
, n
, v
);
3233 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3238 static int get_int_array_element(Jim_Interp
* interp
, const char *varname
, int idx
, uint32_t *val
)
3241 Jim_Obj
*nameObjPtr
, *valObjPtr
;
3245 namebuf
= alloc_printf("%s(%d)", varname
, idx
);
3249 nameObjPtr
= Jim_NewStringObj(interp
, namebuf
, -1);
3256 Jim_IncrRefCount(nameObjPtr
);
3257 valObjPtr
= Jim_GetVariable(interp
, nameObjPtr
, JIM_ERRMSG
);
3258 Jim_DecrRefCount(interp
, nameObjPtr
);
3260 if (valObjPtr
== NULL
)
3263 result
= Jim_GetLong(interp
, valObjPtr
, &l
);
3264 /* printf("%s(%d) => 0%08x\n", varname, idx, val); */
3269 static int jim_array2mem(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
3271 struct command_context
*context
;
3272 struct target
*target
;
3274 context
= Jim_GetAssocData(interp
, "context");
3275 if (context
== NULL
) {
3276 LOG_ERROR("array2mem: no command context");
3279 target
= get_current_target(context
);
3280 if (target
== NULL
) {
3281 LOG_ERROR("array2mem: no current target");
3285 return target_array2mem(interp
,target
, argc
-1, argv
+ 1);
3287 static int target_array2mem(Jim_Interp
*interp
, struct target
*target
, int argc
, Jim_Obj
*const *argv
)
3295 const char *varname
;
3299 /* argv[1] = name of array to get the data
3300 * argv[2] = desired width
3301 * argv[3] = memory address
3302 * argv[4] = count to write
3305 Jim_WrongNumArgs(interp
, 0, argv
, "varname width addr nelems");
3308 varname
= Jim_GetString(argv
[0], &len
);
3309 /* given "foo" get space for worse case "foo(%d)" .. add 20 */
3311 e
= Jim_GetLong(interp
, argv
[1], &l
);
3317 e
= Jim_GetLong(interp
, argv
[2], &l
);
3322 e
= Jim_GetLong(interp
, argv
[3], &l
);
3338 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3339 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "Invalid width param, must be 8/16/32", NULL
);
3343 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3344 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "array2mem: zero width read?", NULL
);
3347 if ((addr
+ (len
* width
)) < addr
) {
3348 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3349 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "array2mem: addr + len - wraps to zero?", NULL
);
3352 /* absurd transfer size? */
3354 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3355 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "array2mem: absurd > 64K item request", NULL
);
3360 ((width
== 2) && ((addr
& 1) == 0)) ||
3361 ((width
== 4) && ((addr
& 3) == 0))) {
3365 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3366 sprintf(buf
, "array2mem address: 0x%08x is not aligned for %d byte reads",
3369 Jim_AppendStrings(interp
, Jim_GetResult(interp
), buf
, NULL
);
3380 size_t buffersize
= 4096;
3381 uint8_t *buffer
= malloc(buffersize
);
3386 /* Slurp... in buffer size chunks */
3388 count
= len
; /* in objects.. */
3389 if (count
> (buffersize
/width
)) {
3390 count
= (buffersize
/width
);
3393 v
= 0; /* shut up gcc */
3394 for (i
= 0 ;i
< count
;i
++, n
++) {
3395 get_int_array_element(interp
, varname
, n
, &v
);
3398 target_buffer_set_u32(target
, &buffer
[i
*width
], v
);
3401 target_buffer_set_u16(target
, &buffer
[i
*width
], v
);
3404 buffer
[i
] = v
& 0x0ff;
3410 retval
= target_write_memory(target
, addr
, width
, count
, buffer
);
3411 if (retval
!= ERROR_OK
) {
3413 LOG_ERROR("array2mem: Write @ 0x%08x, w=%d, cnt=%d, failed",
3417 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3418 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "array2mem: cannot read memory", NULL
);
3426 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3431 void target_all_handle_event(enum target_event e
)
3433 struct target
*target
;
3435 LOG_DEBUG("**all*targets: event: %d, %s",
3437 Jim_Nvp_value2name_simple(nvp_target_event
, e
)->name
);
3439 target
= all_targets
;
3441 target_handle_event(target
, e
);
3442 target
= target
->next
;
3447 /* FIX? should we propagate errors here rather than printing them
3450 void target_handle_event(struct target
*target
, enum target_event e
)
3452 struct target_event_action
*teap
;
3454 for (teap
= target
->event_action
; teap
!= NULL
; teap
= teap
->next
) {
3455 if (teap
->event
== e
) {
3456 LOG_DEBUG("target: (%d) %s (%s) event: %d (%s) action: %s",
3457 target
->target_number
,
3458 target_name(target
),
3459 target_type_name(target
),
3461 Jim_Nvp_value2name_simple(nvp_target_event
, e
)->name
,
3462 Jim_GetString(teap
->body
, NULL
));
3463 if (Jim_EvalObj(teap
->interp
, teap
->body
) != JIM_OK
)
3465 Jim_PrintErrorMessage(teap
->interp
);
3472 * Returns true only if the target has a handler for the specified event.
3474 bool target_has_event_action(struct target
*target
, enum target_event event
)
3476 struct target_event_action
*teap
;
3478 for (teap
= target
->event_action
; teap
!= NULL
; teap
= teap
->next
) {
3479 if (teap
->event
== event
)
3485 enum target_cfg_param
{
3488 TCFG_WORK_AREA_VIRT
,
3489 TCFG_WORK_AREA_PHYS
,
3490 TCFG_WORK_AREA_SIZE
,
3491 TCFG_WORK_AREA_BACKUP
,
3494 TCFG_CHAIN_POSITION
,
3497 static Jim_Nvp nvp_config_opts
[] = {
3498 { .name
= "-type", .value
= TCFG_TYPE
},
3499 { .name
= "-event", .value
= TCFG_EVENT
},
3500 { .name
= "-work-area-virt", .value
= TCFG_WORK_AREA_VIRT
},
3501 { .name
= "-work-area-phys", .value
= TCFG_WORK_AREA_PHYS
},
3502 { .name
= "-work-area-size", .value
= TCFG_WORK_AREA_SIZE
},
3503 { .name
= "-work-area-backup", .value
= TCFG_WORK_AREA_BACKUP
},
3504 { .name
= "-endian" , .value
= TCFG_ENDIAN
},
3505 { .name
= "-variant", .value
= TCFG_VARIANT
},
3506 { .name
= "-chain-position", .value
= TCFG_CHAIN_POSITION
},
3508 { .name
= NULL
, .value
= -1 }
3511 static int target_configure(Jim_GetOptInfo
*goi
, struct target
*target
)
3519 /* parse config or cget options ... */
3520 while (goi
->argc
> 0) {
3521 Jim_SetEmptyResult(goi
->interp
);
3522 /* Jim_GetOpt_Debug(goi); */
3524 if (target
->type
->target_jim_configure
) {
3525 /* target defines a configure function */
3526 /* target gets first dibs on parameters */
3527 e
= (*(target
->type
->target_jim_configure
))(target
, goi
);
3536 /* otherwise we 'continue' below */
3538 e
= Jim_GetOpt_Nvp(goi
, nvp_config_opts
, &n
);
3540 Jim_GetOpt_NvpUnknown(goi
, nvp_config_opts
, 0);
3546 if (goi
->isconfigure
) {
3547 Jim_SetResult_sprintf(goi
->interp
,
3548 "not settable: %s", n
->name
);
3552 if (goi
->argc
!= 0) {
3553 Jim_WrongNumArgs(goi
->interp
,
3554 goi
->argc
, goi
->argv
,
3559 Jim_SetResultString(goi
->interp
,
3560 target_type_name(target
), -1);
3564 if (goi
->argc
== 0) {
3565 Jim_WrongNumArgs(goi
->interp
, goi
->argc
, goi
->argv
, "-event ?event-name? ...");
3569 e
= Jim_GetOpt_Nvp(goi
, nvp_target_event
, &n
);
3571 Jim_GetOpt_NvpUnknown(goi
, nvp_target_event
, 1);
3575 if (goi
->isconfigure
) {
3576 if (goi
->argc
!= 1) {
3577 Jim_WrongNumArgs(goi
->interp
, goi
->argc
, goi
->argv
, "-event ?event-name? ?EVENT-BODY?");
3581 if (goi
->argc
!= 0) {
3582 Jim_WrongNumArgs(goi
->interp
, goi
->argc
, goi
->argv
, "-event ?event-name?");
3588 struct target_event_action
*teap
;
3590 teap
= target
->event_action
;
3591 /* replace existing? */
3593 if (teap
->event
== (enum target_event
)n
->value
) {
3599 if (goi
->isconfigure
) {
3600 bool replace
= true;
3603 teap
= calloc(1, sizeof(*teap
));
3606 teap
->event
= n
->value
;
3607 teap
->interp
= goi
->interp
;
3608 Jim_GetOpt_Obj(goi
, &o
);
3610 Jim_DecrRefCount(teap
->interp
, teap
->body
);
3612 teap
->body
= Jim_DuplicateObj(goi
->interp
, o
);
3615 * Tcl/TK - "tk events" have a nice feature.
3616 * See the "BIND" command.
3617 * We should support that here.
3618 * You can specify %X and %Y in the event code.
3619 * The idea is: %T - target name.
3620 * The idea is: %N - target number
3621 * The idea is: %E - event name.
3623 Jim_IncrRefCount(teap
->body
);
3627 /* add to head of event list */
3628 teap
->next
= target
->event_action
;
3629 target
->event_action
= teap
;
3631 Jim_SetEmptyResult(goi
->interp
);
3635 Jim_SetEmptyResult(goi
->interp
);
3637 Jim_SetResult(goi
->interp
, Jim_DuplicateObj(goi
->interp
, teap
->body
));
3644 case TCFG_WORK_AREA_VIRT
:
3645 if (goi
->isconfigure
) {
3646 target_free_all_working_areas(target
);
3647 e
= Jim_GetOpt_Wide(goi
, &w
);
3651 target
->working_area_virt
= w
;
3652 target
->working_area_virt_spec
= true;
3654 if (goi
->argc
!= 0) {
3658 Jim_SetResult(goi
->interp
, Jim_NewIntObj(goi
->interp
, target
->working_area_virt
));
3662 case TCFG_WORK_AREA_PHYS
:
3663 if (goi
->isconfigure
) {
3664 target_free_all_working_areas(target
);
3665 e
= Jim_GetOpt_Wide(goi
, &w
);
3669 target
->working_area_phys
= w
;
3670 target
->working_area_phys_spec
= true;
3672 if (goi
->argc
!= 0) {
3676 Jim_SetResult(goi
->interp
, Jim_NewIntObj(goi
->interp
, target
->working_area_phys
));
3680 case TCFG_WORK_AREA_SIZE
:
3681 if (goi
->isconfigure
) {
3682 target_free_all_working_areas(target
);
3683 e
= Jim_GetOpt_Wide(goi
, &w
);
3687 target
->working_area_size
= w
;
3689 if (goi
->argc
!= 0) {
3693 Jim_SetResult(goi
->interp
, Jim_NewIntObj(goi
->interp
, target
->working_area_size
));
3697 case TCFG_WORK_AREA_BACKUP
:
3698 if (goi
->isconfigure
) {
3699 target_free_all_working_areas(target
);
3700 e
= Jim_GetOpt_Wide(goi
, &w
);
3704 /* make this exactly 1 or 0 */
3705 target
->backup_working_area
= (!!w
);
3707 if (goi
->argc
!= 0) {
3711 Jim_SetResult(goi
->interp
, Jim_NewIntObj(goi
->interp
, target
->backup_working_area
));
3712 /* loop for more e*/
3716 if (goi
->isconfigure
) {
3717 e
= Jim_GetOpt_Nvp(goi
, nvp_target_endian
, &n
);
3719 Jim_GetOpt_NvpUnknown(goi
, nvp_target_endian
, 1);
3722 target
->endianness
= n
->value
;
3724 if (goi
->argc
!= 0) {
3728 n
= Jim_Nvp_value2name_simple(nvp_target_endian
, target
->endianness
);
3729 if (n
->name
== NULL
) {
3730 target
->endianness
= TARGET_LITTLE_ENDIAN
;
3731 n
= Jim_Nvp_value2name_simple(nvp_target_endian
, target
->endianness
);
3733 Jim_SetResultString(goi
->interp
, n
->name
, -1);
3738 if (goi
->isconfigure
) {
3739 if (goi
->argc
< 1) {
3740 Jim_SetResult_sprintf(goi
->interp
,
3745 if (target
->variant
) {
3746 free((void *)(target
->variant
));
3748 e
= Jim_GetOpt_String(goi
, &cp
, NULL
);
3749 target
->variant
= strdup(cp
);
3751 if (goi
->argc
!= 0) {
3755 Jim_SetResultString(goi
->interp
, target
->variant
,-1);
3758 case TCFG_CHAIN_POSITION
:
3759 if (goi
->isconfigure
) {
3761 struct jtag_tap
*tap
;
3762 target_free_all_working_areas(target
);
3763 e
= Jim_GetOpt_Obj(goi
, &o
);
3767 tap
= jtag_tap_by_jim_obj(goi
->interp
, o
);
3771 /* make this exactly 1 or 0 */
3774 if (goi
->argc
!= 0) {
3778 Jim_SetResultString(goi
->interp
, target
->tap
->dotted_name
, -1);
3779 /* loop for more e*/
3782 } /* while (goi->argc) */
3785 /* done - we return */
3789 static int jim_target_configure(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
3792 Jim_GetOpt_Setup(&goi
, interp
, argc
- 1, argv
+ 1);
3793 goi
.isconfigure
= strcmp(Jim_GetString(argv
[0], NULL
), "configure") == 0;
3794 int need_args
= 1 + goi
.isconfigure
;
3795 if (goi
.argc
< need_args
)
3797 Jim_WrongNumArgs(goi
.interp
, goi
.argc
, goi
.argv
,
3799 ? "missing: -option VALUE ..."
3800 : "missing: -option ...");
3803 struct target
*target
= Jim_CmdPrivData(goi
.interp
);
3804 return target_configure(&goi
, target
);
3807 static int jim_target_mw(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
3809 const char *cmd_name
= Jim_GetString(argv
[0], NULL
);
3812 Jim_GetOpt_Setup(&goi
, interp
, argc
- 1, argv
+ 1);
3814 if (goi
.argc
!= 2 && goi
.argc
!= 3)
3816 Jim_SetResult_sprintf(goi
.interp
,
3817 "usage: %s <address> <data> [<count>]", cmd_name
);
3822 int e
= Jim_GetOpt_Wide(&goi
, &a
);
3827 e
= Jim_GetOpt_Wide(&goi
, &b
);
3834 e
= Jim_GetOpt_Wide(&goi
, &c
);
3839 struct target
*target
= Jim_CmdPrivData(goi
.interp
);
3840 uint8_t target_buf
[32];
3841 if (strcasecmp(cmd_name
, "mww") == 0) {
3842 target_buffer_set_u32(target
, target_buf
, b
);
3845 else if (strcasecmp(cmd_name
, "mwh") == 0) {
3846 target_buffer_set_u16(target
, target_buf
, b
);
3849 else if (strcasecmp(cmd_name
, "mwb") == 0) {
3850 target_buffer_set_u8(target
, target_buf
, b
);
3853 LOG_ERROR("command '%s' unknown: ", cmd_name
);
3857 for (jim_wide x
= 0; x
< c
; x
++)
3859 e
= target_write_memory(target
, a
, b
, 1, target_buf
);
3862 Jim_SetResult_sprintf(interp
,
3863 "Error writing @ 0x%08x: %d\n", (int)(a
), e
);
3872 static int jim_target_md(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
3874 const char *cmd_name
= Jim_GetString(argv
[0], NULL
);
3877 Jim_GetOpt_Setup(&goi
, interp
, argc
- 1, argv
+ 1);
3879 if ((goi
.argc
== 2) || (goi
.argc
== 3))
3881 Jim_SetResult_sprintf(goi
.interp
,
3882 "usage: %s <address> [<count>]", cmd_name
);
3887 int e
= Jim_GetOpt_Wide(&goi
, &a
);
3893 e
= Jim_GetOpt_Wide(&goi
, &c
);
3900 jim_wide b
= 1; /* shut up gcc */
3901 if (strcasecmp(cmd_name
, "mdw") == 0)
3903 else if (strcasecmp(cmd_name
, "mdh") == 0)
3905 else if (strcasecmp(cmd_name
, "mdb") == 0)
3908 LOG_ERROR("command '%s' unknown: ", cmd_name
);
3912 /* convert count to "bytes" */
3915 struct target
*target
= Jim_CmdPrivData(goi
.interp
);
3916 uint8_t target_buf
[32];
3923 e
= target_read_memory(target
, a
, b
, y
/ b
, target_buf
);
3924 if (e
!= ERROR_OK
) {
3925 Jim_SetResult_sprintf(interp
, "error reading target @ 0x%08lx", (int)(a
));
3929 Jim_fprintf(interp
, interp
->cookie_stdout
, "0x%08x ", (int)(a
));
3932 for (x
= 0; x
< 16 && x
< y
; x
+= 4)
3934 z
= target_buffer_get_u32(target
, &(target_buf
[ x
* 4 ]));
3935 Jim_fprintf(interp
, interp
->cookie_stdout
, "%08x ", (int)(z
));
3937 for (; (x
< 16) ; x
+= 4) {
3938 Jim_fprintf(interp
, interp
->cookie_stdout
, " ");
3942 for (x
= 0; x
< 16 && x
< y
; x
+= 2)
3944 z
= target_buffer_get_u16(target
, &(target_buf
[ x
* 2 ]));
3945 Jim_fprintf(interp
, interp
->cookie_stdout
, "%04x ", (int)(z
));
3947 for (; (x
< 16) ; x
+= 2) {
3948 Jim_fprintf(interp
, interp
->cookie_stdout
, " ");
3953 for (x
= 0 ; (x
< 16) && (x
< y
) ; x
+= 1) {
3954 z
= target_buffer_get_u8(target
, &(target_buf
[ x
* 4 ]));
3955 Jim_fprintf(interp
, interp
->cookie_stdout
, "%02x ", (int)(z
));
3957 for (; (x
< 16) ; x
+= 1) {
3958 Jim_fprintf(interp
, interp
->cookie_stdout
, " ");
3962 /* ascii-ify the bytes */
3963 for (x
= 0 ; x
< y
; x
++) {
3964 if ((target_buf
[x
] >= 0x20) &&
3965 (target_buf
[x
] <= 0x7e)) {
3969 target_buf
[x
] = '.';
3974 target_buf
[x
] = ' ';
3979 /* print - with a newline */
3980 Jim_fprintf(interp
, interp
->cookie_stdout
, "%s\n", target_buf
);
3988 static int jim_target_mem2array(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
3990 struct target
*target
= Jim_CmdPrivData(interp
);
3991 return target_mem2array(interp
, target
, argc
- 1, argv
+ 1);
3994 static int jim_target_array2mem(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
3996 struct target
*target
= Jim_CmdPrivData(interp
);
3997 return target_array2mem(interp
, target
, argc
- 1, argv
+ 1);
4000 static int jim_target_tap_disabled(Jim_Interp
*interp
)
4002 Jim_SetResult_sprintf(interp
, "[TAP is disabled]");
4006 static int jim_target_examine(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4010 Jim_WrongNumArgs(interp
, 1, argv
, "[no parameters]");
4013 struct target
*target
= Jim_CmdPrivData(interp
);
4014 if (!target
->tap
->enabled
)
4015 return jim_target_tap_disabled(interp
);
4017 int e
= target
->type
->examine(target
);
4020 Jim_SetResult_sprintf(interp
, "examine-fails: %d", e
);
4026 static int jim_target_poll(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4030 Jim_WrongNumArgs(interp
, 1, argv
, "[no parameters]");
4033 struct target
*target
= Jim_CmdPrivData(interp
);
4034 if (!target
->tap
->enabled
)
4035 return jim_target_tap_disabled(interp
);
4038 if (!(target_was_examined(target
))) {
4039 e
= ERROR_TARGET_NOT_EXAMINED
;
4041 e
= target
->type
->poll(target
);
4045 Jim_SetResult_sprintf(interp
, "poll-fails: %d", e
);
4051 static int jim_target_reset(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4054 Jim_GetOpt_Setup(&goi
, interp
, argc
- 1, argv
+ 1);
4058 Jim_WrongNumArgs(interp
, 0, argv
,
4059 "([tT]|[fF]|assert|deassert) BOOL");
4064 int e
= Jim_GetOpt_Nvp(&goi
, nvp_assert
, &n
);
4067 Jim_GetOpt_NvpUnknown(&goi
, nvp_assert
, 1);
4070 /* the halt or not param */
4072 e
= Jim_GetOpt_Wide(&goi
, &a
);
4076 struct target
*target
= Jim_CmdPrivData(goi
.interp
);
4077 if (!target
->tap
->enabled
)
4078 return jim_target_tap_disabled(interp
);
4079 if (!target
->type
->assert_reset
|| !target
->type
->deassert_reset
)
4081 Jim_SetResult_sprintf(interp
,
4082 "No target-specific reset for %s",
4083 target_name(target
));
4086 /* determine if we should halt or not. */
4087 target
->reset_halt
= !!a
;
4088 /* When this happens - all workareas are invalid. */
4089 target_free_all_working_areas_restore(target
, 0);
4092 if (n
->value
== NVP_ASSERT
) {
4093 e
= target
->type
->assert_reset(target
);
4095 e
= target
->type
->deassert_reset(target
);
4097 return (e
== ERROR_OK
) ? JIM_OK
: JIM_ERR
;
4100 static int jim_target_halt(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4103 Jim_WrongNumArgs(interp
, 1, argv
, "[no parameters]");
4106 struct target
*target
= Jim_CmdPrivData(interp
);
4107 if (!target
->tap
->enabled
)
4108 return jim_target_tap_disabled(interp
);
4109 int e
= target
->type
->halt(target
);
4110 return (e
== ERROR_OK
) ? JIM_OK
: JIM_ERR
;
4113 static int jim_target_wait_state(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4116 Jim_GetOpt_Setup(&goi
, interp
, argc
- 1, argv
+ 1);
4118 /* params: <name> statename timeoutmsecs */
4121 const char *cmd_name
= Jim_GetString(argv
[0], NULL
);
4122 Jim_SetResult_sprintf(goi
.interp
,
4123 "%s <state_name> <timeout_in_msec>", cmd_name
);
4128 int e
= Jim_GetOpt_Nvp(&goi
, nvp_target_state
, &n
);
4130 Jim_GetOpt_NvpUnknown(&goi
, nvp_target_state
,1);
4134 e
= Jim_GetOpt_Wide(&goi
, &a
);
4138 struct target
*target
= Jim_CmdPrivData(interp
);
4139 if (!target
->tap
->enabled
)
4140 return jim_target_tap_disabled(interp
);
4142 e
= target_wait_state(target
, n
->value
, a
);
4145 Jim_SetResult_sprintf(goi
.interp
,
4146 "target: %s wait %s fails (%d) %s",
4147 target_name(target
), n
->name
,
4148 e
, target_strerror_safe(e
));
4153 /* List for human, Events defined for this target.
4154 * scripts/programs should use 'name cget -event NAME'
4156 static int jim_target_event_list(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4158 struct command_context
*cmd_ctx
= Jim_GetAssocData(interp
, "context");
4159 struct target
*target
= Jim_CmdPrivData(interp
);
4160 struct target_event_action
*teap
= target
->event_action
;
4161 command_print(cmd_ctx
, "Event actions for target (%d) %s\n",
4162 target
->target_number
,
4163 target_name(target
));
4164 command_print(cmd_ctx
, "%-25s | Body", "Event");
4165 command_print(cmd_ctx
, "------------------------- | "
4166 "----------------------------------------");
4169 Jim_Nvp
*opt
= Jim_Nvp_value2name_simple(nvp_target_event
, teap
->event
);
4170 command_print(cmd_ctx
, "%-25s | %s",
4171 opt
->name
, Jim_GetString(teap
->body
, NULL
));
4174 command_print(cmd_ctx
, "***END***");
4177 static int jim_target_current_state(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4181 Jim_WrongNumArgs(interp
, 1, argv
, "[no parameters]");
4184 struct target
*target
= Jim_CmdPrivData(interp
);
4185 Jim_SetResultString(interp
, target_state_name(target
), -1);
4188 static int jim_target_invoke_event(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4191 Jim_GetOpt_Setup(&goi
, interp
, argc
- 1, argv
+ 1);
4194 const char *cmd_name
= Jim_GetString(argv
[0], NULL
);
4195 Jim_SetResult_sprintf(goi
.interp
, "%s <eventname>", cmd_name
);
4199 int e
= Jim_GetOpt_Nvp(&goi
, nvp_target_event
, &n
);
4202 Jim_GetOpt_NvpUnknown(&goi
, nvp_target_event
, 1);
4205 struct target
*target
= Jim_CmdPrivData(interp
);
4206 target_handle_event(target
, n
->value
);
4210 static const struct command_registration target_instance_command_handlers
[] = {
4212 .name
= "configure",
4213 .mode
= COMMAND_CONFIG
,
4214 .jim_handler
= &jim_target_configure
,
4215 .usage
= "[<target_options> ...]",
4216 .help
= "configure a new target for use",
4220 .mode
= COMMAND_ANY
,
4221 .jim_handler
= &jim_target_configure
,
4222 .usage
= "<target_type> [<target_options> ...]",
4223 .help
= "configure a new target for use",
4227 .mode
= COMMAND_EXEC
,
4228 .jim_handler
= &jim_target_mw
,
4229 .usage
= "<address> <data> [<count>]",
4230 .help
= "Write 32-bit word(s) to target memory",
4234 .mode
= COMMAND_EXEC
,
4235 .jim_handler
= &jim_target_mw
,
4236 .usage
= "<address> <data> [<count>]",
4237 .help
= "Write 16-bit half-word(s) to target memory",
4241 .mode
= COMMAND_EXEC
,
4242 .jim_handler
= &jim_target_mw
,
4243 .usage
= "<address> <data> [<count>]",
4244 .help
= "Write byte(s) to target memory",
4248 .mode
= COMMAND_EXEC
,
4249 .jim_handler
= &jim_target_md
,
4250 .usage
= "<address> [<count>]",
4251 .help
= "Display target memory as 32-bit words",
4255 .mode
= COMMAND_EXEC
,
4256 .jim_handler
= &jim_target_md
,
4257 .usage
= "<address> [<count>]",
4258 .help
= "Display target memory as 16-bit half-words",
4262 .mode
= COMMAND_EXEC
,
4263 .jim_handler
= &jim_target_md
,
4264 .usage
= "<address> [<count>]",
4265 .help
= "Display target memory as 8-bit bytes",
4268 .name
= "array2mem",
4269 .mode
= COMMAND_EXEC
,
4270 .jim_handler
= &jim_target_array2mem
,
4273 .name
= "mem2array",
4274 .mode
= COMMAND_EXEC
,
4275 .jim_handler
= &jim_target_mem2array
,
4278 .name
= "eventlist",
4279 .mode
= COMMAND_EXEC
,
4280 .jim_handler
= &jim_target_event_list
,
4284 .mode
= COMMAND_EXEC
,
4285 .jim_handler
= &jim_target_current_state
,
4288 .name
= "arp_examine",
4289 .mode
= COMMAND_EXEC
,
4290 .jim_handler
= &jim_target_examine
,
4294 .mode
= COMMAND_EXEC
,
4295 .jim_handler
= &jim_target_poll
,
4298 .name
= "arp_reset",
4299 .mode
= COMMAND_EXEC
,
4300 .jim_handler
= &jim_target_reset
,
4304 .mode
= COMMAND_EXEC
,
4305 .jim_handler
= &jim_target_halt
,
4308 .name
= "arp_waitstate",
4309 .mode
= COMMAND_EXEC
,
4310 .jim_handler
= &jim_target_wait_state
,
4313 .name
= "invoke-event",
4314 .mode
= COMMAND_EXEC
,
4315 .jim_handler
= &jim_target_invoke_event
,
4317 COMMAND_REGISTRATION_DONE
4320 static int target_create(Jim_GetOptInfo
*goi
)
4328 struct target
*target
;
4329 struct command_context
*cmd_ctx
;
4331 cmd_ctx
= Jim_GetAssocData(goi
->interp
, "context");
4332 if (goi
->argc
< 3) {
4333 Jim_WrongNumArgs(goi
->interp
, 1, goi
->argv
, "?name? ?type? ..options...");
4338 Jim_GetOpt_Obj(goi
, &new_cmd
);
4339 /* does this command exist? */
4340 cmd
= Jim_GetCommand(goi
->interp
, new_cmd
, JIM_ERRMSG
);
4342 cp
= Jim_GetString(new_cmd
, NULL
);
4343 Jim_SetResult_sprintf(goi
->interp
, "Command/target: %s Exists", cp
);
4348 e
= Jim_GetOpt_String(goi
, &cp2
, NULL
);
4350 /* now does target type exist */
4351 for (x
= 0 ; target_types
[x
] ; x
++) {
4352 if (0 == strcmp(cp
, target_types
[x
]->name
)) {
4357 if (target_types
[x
] == NULL
) {
4358 Jim_SetResult_sprintf(goi
->interp
, "Unknown target type %s, try one of ", cp
);
4359 for (x
= 0 ; target_types
[x
] ; x
++) {
4360 if (target_types
[x
+ 1]) {
4361 Jim_AppendStrings(goi
->interp
,
4362 Jim_GetResult(goi
->interp
),
4363 target_types
[x
]->name
,
4366 Jim_AppendStrings(goi
->interp
,
4367 Jim_GetResult(goi
->interp
),
4369 target_types
[x
]->name
,NULL
);
4376 target
= calloc(1,sizeof(struct target
));
4377 /* set target number */
4378 target
->target_number
= new_target_number();
4380 /* allocate memory for each unique target type */
4381 target
->type
= (struct target_type
*)calloc(1,sizeof(struct target_type
));
4383 memcpy(target
->type
, target_types
[x
], sizeof(struct target_type
));
4385 /* will be set by "-endian" */
4386 target
->endianness
= TARGET_ENDIAN_UNKNOWN
;
4388 target
->working_area
= 0x0;
4389 target
->working_area_size
= 0x0;
4390 target
->working_areas
= NULL
;
4391 target
->backup_working_area
= 0;
4393 target
->state
= TARGET_UNKNOWN
;
4394 target
->debug_reason
= DBG_REASON_UNDEFINED
;
4395 target
->reg_cache
= NULL
;
4396 target
->breakpoints
= NULL
;
4397 target
->watchpoints
= NULL
;
4398 target
->next
= NULL
;
4399 target
->arch_info
= NULL
;
4401 target
->display
= 1;
4403 target
->halt_issued
= false;
4405 /* initialize trace information */
4406 target
->trace_info
= malloc(sizeof(struct trace
));
4407 target
->trace_info
->num_trace_points
= 0;
4408 target
->trace_info
->trace_points_size
= 0;
4409 target
->trace_info
->trace_points
= NULL
;
4410 target
->trace_info
->trace_history_size
= 0;
4411 target
->trace_info
->trace_history
= NULL
;
4412 target
->trace_info
->trace_history_pos
= 0;
4413 target
->trace_info
->trace_history_overflowed
= 0;
4415 target
->dbgmsg
= NULL
;
4416 target
->dbg_msg_enabled
= 0;
4418 target
->endianness
= TARGET_ENDIAN_UNKNOWN
;
4420 /* Do the rest as "configure" options */
4421 goi
->isconfigure
= 1;
4422 e
= target_configure(goi
, target
);
4424 if (target
->tap
== NULL
)
4426 Jim_SetResultString(goi
->interp
, "-chain-position required when creating target", -1);
4436 if (target
->endianness
== TARGET_ENDIAN_UNKNOWN
) {
4437 /* default endian to little if not specified */
4438 target
->endianness
= TARGET_LITTLE_ENDIAN
;
4441 /* incase variant is not set */
4442 if (!target
->variant
)
4443 target
->variant
= strdup("");
4445 cp
= Jim_GetString(new_cmd
, NULL
);
4446 target
->cmd_name
= strdup(cp
);
4448 /* create the target specific commands */
4449 if (target
->type
->commands
) {
4450 e
= register_commands(cmd_ctx
, NULL
, target
->type
->commands
);
4452 LOG_ERROR("unable to register '%s' commands", cp
);
4454 if (target
->type
->target_create
) {
4455 (*(target
->type
->target_create
))(target
, goi
->interp
);
4458 /* append to end of list */
4460 struct target
**tpp
;
4461 tpp
= &(all_targets
);
4463 tpp
= &((*tpp
)->next
);
4468 /* now - create the new target name command */
4469 const const struct command_registration target_subcommands
[] = {
4471 .chain
= target_instance_command_handlers
,
4474 .chain
= target
->type
->commands
,
4476 COMMAND_REGISTRATION_DONE
4478 const const struct command_registration target_commands
[] = {
4481 .mode
= COMMAND_ANY
,
4482 .help
= "target command group",
4483 .chain
= target_subcommands
,
4485 COMMAND_REGISTRATION_DONE
4487 e
= register_commands(cmd_ctx
, NULL
, target_commands
);
4491 struct command
*c
= command_find_in_context(cmd_ctx
, cp
);
4493 command_set_handler_data(c
, target
);
4495 return (ERROR_OK
== e
) ? JIM_OK
: JIM_ERR
;
4498 static int jim_target_current(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4502 Jim_WrongNumArgs(interp
, 1, argv
, "Too many parameters");
4505 struct command_context
*cmd_ctx
= Jim_GetAssocData(interp
, "context");
4506 Jim_SetResultString(interp
, get_current_target(cmd_ctx
)->cmd_name
, -1);
4510 static int jim_target_types(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4514 Jim_WrongNumArgs(interp
, 1, argv
, "Too many parameters");
4517 Jim_SetResult(interp
, Jim_NewListObj(interp
, NULL
, 0));
4518 for (unsigned x
= 0; NULL
!= target_types
[x
]; x
++)
4520 Jim_ListAppendElement(interp
, Jim_GetResult(interp
),
4521 Jim_NewStringObj(interp
, target_types
[x
]->name
, -1));
4526 static int jim_target_names(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4530 Jim_WrongNumArgs(interp
, 1, argv
, "Too many parameters");
4533 Jim_SetResult(interp
, Jim_NewListObj(interp
, NULL
, 0));
4534 struct target
*target
= all_targets
;
4537 Jim_ListAppendElement(interp
, Jim_GetResult(interp
),
4538 Jim_NewStringObj(interp
, target_name(target
), -1));
4539 target
= target
->next
;
4544 static int jim_target_create(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4547 Jim_GetOpt_Setup(&goi
, interp
, argc
- 1, argv
+ 1);
4550 Jim_WrongNumArgs(goi
.interp
, goi
.argc
, goi
.argv
,
4551 "<name> <target_type> [<target_options> ...]");
4554 return target_create(&goi
);
4557 static int jim_target_number(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4560 Jim_GetOpt_Setup(&goi
, interp
, argc
- 1, argv
+ 1);
4562 /* It's OK to remove this mechanism sometime after August 2010 or so */
4563 LOG_WARNING("don't use numbers as target identifiers; use names");
4566 Jim_SetResult_sprintf(goi
.interp
, "usage: target number <number>");
4570 int e
= Jim_GetOpt_Wide(&goi
, &w
);
4574 struct target
*target
;
4575 for (target
= all_targets
; NULL
!= target
; target
= target
->next
)
4577 if (target
->target_number
!= w
)
4580 Jim_SetResultString(goi
.interp
, target_name(target
), -1);
4583 Jim_SetResult_sprintf(goi
.interp
,
4584 "Target: number %d does not exist", (int)(w
));
4588 static int jim_target_count(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4592 Jim_WrongNumArgs(interp
, 1, argv
, "<no parameters>");
4596 struct target
*target
= all_targets
;
4597 while (NULL
!= target
)
4599 target
= target
->next
;
4602 Jim_SetResult(interp
, Jim_NewIntObj(interp
, count
));
4606 static const struct command_registration target_subcommand_handlers
[] = {
4609 .mode
= COMMAND_CONFIG
,
4610 .handler
= &handle_target_init_command
,
4611 .help
= "initialize targets",
4615 .mode
= COMMAND_ANY
,
4616 .jim_handler
= &jim_target_create
,
4617 .usage
= "<name> <type> ...",
4618 .help
= "Returns the currently selected target",
4622 .mode
= COMMAND_ANY
,
4623 .jim_handler
= &jim_target_current
,
4624 .help
= "Returns the currently selected target",
4628 .mode
= COMMAND_ANY
,
4629 .jim_handler
= &jim_target_types
,
4630 .help
= "Returns the available target types as a list of strings",
4634 .mode
= COMMAND_ANY
,
4635 .jim_handler
= &jim_target_names
,
4636 .help
= "Returns the names of all targets as a list of strings",
4640 .mode
= COMMAND_ANY
,
4641 .jim_handler
= &jim_target_number
,
4642 .usage
= "<number>",
4643 .help
= "Returns the name of target <n>",
4647 .mode
= COMMAND_ANY
,
4648 .jim_handler
= &jim_target_count
,
4649 .help
= "Returns the number of targets as an integer",
4651 COMMAND_REGISTRATION_DONE
4663 static int fastload_num
;
4664 static struct FastLoad
*fastload
;
4666 static void free_fastload(void)
4668 if (fastload
!= NULL
)
4671 for (i
= 0; i
< fastload_num
; i
++)
4673 if (fastload
[i
].data
)
4674 free(fastload
[i
].data
);
4684 COMMAND_HANDLER(handle_fast_load_image_command
)
4688 uint32_t image_size
;
4689 uint32_t min_address
= 0;
4690 uint32_t max_address
= 0xffffffff;
4695 int retval
= CALL_COMMAND_HANDLER(parse_load_image_command_CMD_ARGV
,
4696 &image
, &min_address
, &max_address
);
4697 if (ERROR_OK
!= retval
)
4700 struct duration bench
;
4701 duration_start(&bench
);
4703 if (image_open(&image
, CMD_ARGV
[0], (CMD_ARGC
>= 3) ? CMD_ARGV
[2] : NULL
) != ERROR_OK
)
4710 fastload_num
= image
.num_sections
;
4711 fastload
= (struct FastLoad
*)malloc(sizeof(struct FastLoad
)*image
.num_sections
);
4712 if (fastload
== NULL
)
4714 image_close(&image
);
4717 memset(fastload
, 0, sizeof(struct FastLoad
)*image
.num_sections
);
4718 for (i
= 0; i
< image
.num_sections
; i
++)
4720 buffer
= malloc(image
.sections
[i
].size
);
4723 command_print(CMD_CTX
, "error allocating buffer for section (%d bytes)",
4724 (int)(image
.sections
[i
].size
));
4728 if ((retval
= image_read_section(&image
, i
, 0x0, image
.sections
[i
].size
, buffer
, &buf_cnt
)) != ERROR_OK
)
4734 uint32_t offset
= 0;
4735 uint32_t length
= buf_cnt
;
4738 /* DANGER!!! beware of unsigned comparision here!!! */
4740 if ((image
.sections
[i
].base_address
+ buf_cnt
>= min_address
)&&
4741 (image
.sections
[i
].base_address
< max_address
))
4743 if (image
.sections
[i
].base_address
< min_address
)
4745 /* clip addresses below */
4746 offset
+= min_address
-image
.sections
[i
].base_address
;
4750 if (image
.sections
[i
].base_address
+ buf_cnt
> max_address
)
4752 length
-= (image
.sections
[i
].base_address
+ buf_cnt
)-max_address
;
4755 fastload
[i
].address
= image
.sections
[i
].base_address
+ offset
;
4756 fastload
[i
].data
= malloc(length
);
4757 if (fastload
[i
].data
== NULL
)
4762 memcpy(fastload
[i
].data
, buffer
+ offset
, length
);
4763 fastload
[i
].length
= length
;
4765 image_size
+= length
;
4766 command_print(CMD_CTX
, "%u bytes written at address 0x%8.8x",
4767 (unsigned int)length
,
4768 ((unsigned int)(image
.sections
[i
].base_address
+ offset
)));
4774 if ((ERROR_OK
== retval
) && (duration_measure(&bench
) == ERROR_OK
))
4776 command_print(CMD_CTX
, "Loaded %" PRIu32
" bytes "
4777 "in %fs (%0.3f kb/s)", image_size
,
4778 duration_elapsed(&bench
), duration_kbps(&bench
, image_size
));
4780 command_print(CMD_CTX
,
4781 "WARNING: image has not been loaded to target!"
4782 "You can issue a 'fast_load' to finish loading.");
4785 image_close(&image
);
4787 if (retval
!= ERROR_OK
)
4795 COMMAND_HANDLER(handle_fast_load_command
)
4798 return ERROR_COMMAND_SYNTAX_ERROR
;
4799 if (fastload
== NULL
)
4801 LOG_ERROR("No image in memory");
4805 int ms
= timeval_ms();
4807 int retval
= ERROR_OK
;
4808 for (i
= 0; i
< fastload_num
;i
++)
4810 struct target
*target
= get_current_target(CMD_CTX
);
4811 command_print(CMD_CTX
, "Write to 0x%08x, length 0x%08x",
4812 (unsigned int)(fastload
[i
].address
),
4813 (unsigned int)(fastload
[i
].length
));
4814 if (retval
== ERROR_OK
)
4816 retval
= target_write_buffer(target
, fastload
[i
].address
, fastload
[i
].length
, fastload
[i
].data
);
4818 size
+= fastload
[i
].length
;
4820 int after
= timeval_ms();
4821 command_print(CMD_CTX
, "Loaded image %f kBytes/s", (float)(size
/1024.0)/((float)(after
-ms
)/1000.0));
4825 static const struct command_registration target_command_handlers
[] = {
4828 .handler
= &handle_targets_command
,
4829 .mode
= COMMAND_ANY
,
4830 .help
= "change current command line target (one parameter) "
4831 "or list targets (no parameters)",
4832 .usage
= "[<new_current_target>]",
4836 .mode
= COMMAND_CONFIG
,
4837 .help
= "configure target",
4839 .chain
= target_subcommand_handlers
,
4841 COMMAND_REGISTRATION_DONE
4844 int target_register_commands(struct command_context
*cmd_ctx
)
4846 return register_commands(cmd_ctx
, NULL
, target_command_handlers
);
4849 static const struct command_registration target_exec_command_handlers
[] = {
4851 .name
= "fast_load_image",
4852 .handler
= &handle_fast_load_image_command
,
4853 .mode
= COMMAND_ANY
,
4854 .help
= "Load image into memory, mainly for profiling purposes",
4855 .usage
= "<file> <address> ['bin'|'ihex'|'elf'|'s19'] "
4856 "[min_address] [max_length]",
4859 .name
= "fast_load",
4860 .handler
= &handle_fast_load_command
,
4861 .mode
= COMMAND_ANY
,
4862 .help
= "loads active fast load image to current target "
4863 "- mainly for profiling purposes",
4867 .handler
= &handle_profile_command
,
4868 .mode
= COMMAND_EXEC
,
4869 .help
= "profiling samples the CPU PC",
4871 /** @todo don't register virt2phys() unless target supports it */
4873 .name
= "virt2phys",
4874 .handler
= &handle_virt2phys_command
,
4875 .mode
= COMMAND_ANY
,
4876 .help
= "translate a virtual address into a physical address",
4881 .handler
= &handle_reg_command
,
4882 .mode
= COMMAND_EXEC
,
4883 .help
= "display or set a register",
4888 .handler
= &handle_poll_command
,
4889 .mode
= COMMAND_EXEC
,
4890 .help
= "poll target state",
4893 .name
= "wait_halt",
4894 .handler
= &handle_wait_halt_command
,
4895 .mode
= COMMAND_EXEC
,
4896 .help
= "wait for target halt",
4897 .usage
= "[time (s)]",
4901 .handler
= &handle_halt_command
,
4902 .mode
= COMMAND_EXEC
,
4903 .help
= "halt target",
4907 .handler
= &handle_resume_command
,
4908 .mode
= COMMAND_EXEC
,
4909 .help
= "resume target",
4910 .usage
= "[<address>]",
4914 .handler
= &handle_reset_command
,
4915 .mode
= COMMAND_EXEC
,
4916 .usage
= "[run|halt|init]",
4917 .help
= "Reset all targets into the specified mode."
4918 "Default reset mode is run, if not given.",
4921 .name
= "soft_reset_halt",
4922 .handler
= &handle_soft_reset_halt_command
,
4923 .mode
= COMMAND_EXEC
,
4924 .help
= "halt the target and do a soft reset",
4929 .handler
= &handle_step_command
,
4930 .mode
= COMMAND_EXEC
,
4931 .help
= "step one instruction from current PC or [addr]",
4932 .usage
= "[<address>]",
4937 .handler
= &handle_md_command
,
4938 .mode
= COMMAND_EXEC
,
4939 .help
= "display memory words",
4940 .usage
= "[phys] <addr> [count]",
4944 .handler
= &handle_md_command
,
4945 .mode
= COMMAND_EXEC
,
4946 .help
= "display memory half-words",
4947 .usage
= "[phys] <addr> [count]",
4951 .handler
= &handle_md_command
,
4952 .mode
= COMMAND_EXEC
,
4953 .help
= "display memory bytes",
4954 .usage
= "[phys] <addr> [count]",
4959 .handler
= &handle_mw_command
,
4960 .mode
= COMMAND_EXEC
,
4961 .help
= "write memory word",
4962 .usage
= "[phys] <addr> <value> [count]",
4966 .handler
= &handle_mw_command
,
4967 .mode
= COMMAND_EXEC
,
4968 .help
= "write memory half-word",
4969 .usage
= "[phys] <addr> <value> [count]",
4973 .handler
= &handle_mw_command
,
4974 .mode
= COMMAND_EXEC
,
4975 .help
= "write memory byte",
4976 .usage
= "[phys] <addr> <value> [count]",
4981 .handler
= &handle_bp_command
,
4982 .mode
= COMMAND_EXEC
,
4983 .help
= "list or set breakpoint",
4984 .usage
= "[<address> <length> [hw]]",
4988 .handler
= &handle_rbp_command
,
4989 .mode
= COMMAND_EXEC
,
4990 .help
= "remove breakpoint",
4991 .usage
= "<address>",
4996 .handler
= &handle_wp_command
,
4997 .mode
= COMMAND_EXEC
,
4998 .help
= "list or set watchpoint",
4999 .usage
= "[<address> <length> <r/w/a> [value] [mask]]",
5003 .handler
= &handle_rwp_command
,
5004 .mode
= COMMAND_EXEC
,
5005 .help
= "remove watchpoint",
5006 .usage
= "<address>",
5010 .name
= "load_image",
5011 .handler
= &handle_load_image_command
,
5012 .mode
= COMMAND_EXEC
,
5013 .usage
= "<file> <address> ['bin'|'ihex'|'elf'|'s19'] "
5014 "[min_address] [max_length]",
5017 .name
= "dump_image",
5018 .handler
= &handle_dump_image_command
,
5019 .mode
= COMMAND_EXEC
,
5020 .usage
= "<file> <address> <size>",
5023 .name
= "verify_image",
5024 .handler
= &handle_verify_image_command
,
5025 .mode
= COMMAND_EXEC
,
5026 .usage
= "<file> [offset] [type]",
5029 .name
= "test_image",
5030 .handler
= &handle_test_image_command
,
5031 .mode
= COMMAND_EXEC
,
5032 .usage
= "<file> [offset] [type]",
5035 .name
= "ocd_mem2array",
5036 .mode
= COMMAND_EXEC
,
5037 .jim_handler
= &jim_mem2array
,
5038 .help
= "read memory and return as a TCL array "
5039 "for script processing",
5040 .usage
= "<arrayname> <width=32|16|8> <address> <count>",
5043 .name
= "ocd_array2mem",
5044 .mode
= COMMAND_EXEC
,
5045 .jim_handler
= &jim_array2mem
,
5046 .help
= "convert a TCL array to memory locations "
5047 "and write the values",
5048 .usage
= "<arrayname> <width=32|16|8> <address> <count>",
5050 COMMAND_REGISTRATION_DONE
5052 int target_register_user_commands(struct command_context
*cmd_ctx
)
5054 int retval
= ERROR_OK
;
5055 if ((retval
= target_request_register_commands(cmd_ctx
)) != ERROR_OK
)
5058 if ((retval
= trace_register_commands(cmd_ctx
)) != ERROR_OK
)
5062 return register_commands(cmd_ctx
, NULL
, target_exec_command_handlers
);