1 /***************************************************************************
2 * Copyright (C) 2005 by Dominic Rath *
3 * Dominic.Rath@gmx.de *
5 * Copyright (C) 2007-2009 Øyvind Harboe *
6 * oyvind.harboe@zylin.com *
8 * Copyright (C) 2008, Duane Ellis *
9 * openocd@duaneeellis.com *
11 * Copyright (C) 2008 by Spencer Oliver *
12 * spen@spen-soft.co.uk *
14 * Copyright (C) 2008 by Rick Altherr *
15 * kc8apf@kc8apf.net> *
17 * This program is free software; you can redistribute it and/or modify *
18 * it under the terms of the GNU General Public License as published by *
19 * the Free Software Foundation; either version 2 of the License, or *
20 * (at your option) any later version. *
22 * This program is distributed in the hope that it will be useful, *
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
25 * GNU General Public License for more details. *
27 * You should have received a copy of the GNU General Public License *
28 * along with this program; if not, write to the *
29 * Free Software Foundation, Inc., *
30 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
31 ***************************************************************************/
36 #include <helper/time_support.h>
37 #include <jtag/jtag.h>
40 #include "target_type.h"
41 #include "target_request.h"
42 #include "breakpoints.h"
48 static int target_array2mem(Jim_Interp
*interp
, struct target
*target
, int argc
, Jim_Obj
*const *argv
);
49 static int target_mem2array(Jim_Interp
*interp
, struct target
*target
, int argc
, Jim_Obj
*const *argv
);
52 extern struct target_type arm7tdmi_target
;
53 extern struct target_type arm720t_target
;
54 extern struct target_type arm9tdmi_target
;
55 extern struct target_type arm920t_target
;
56 extern struct target_type arm966e_target
;
57 extern struct target_type arm926ejs_target
;
58 extern struct target_type fa526_target
;
59 extern struct target_type feroceon_target
;
60 extern struct target_type dragonite_target
;
61 extern struct target_type xscale_target
;
62 extern struct target_type cortexm3_target
;
63 extern struct target_type cortexa8_target
;
64 extern struct target_type arm11_target
;
65 extern struct target_type mips_m4k_target
;
66 extern struct target_type avr_target
;
67 extern struct target_type testee_target
;
69 struct target_type
*target_types
[] =
90 struct target
*all_targets
= NULL
;
91 struct target_event_callback
*target_event_callbacks
= NULL
;
92 struct target_timer_callback
*target_timer_callbacks
= NULL
;
94 static const Jim_Nvp nvp_assert
[] = {
95 { .name
= "assert", NVP_ASSERT
},
96 { .name
= "deassert", NVP_DEASSERT
},
97 { .name
= "T", NVP_ASSERT
},
98 { .name
= "F", NVP_DEASSERT
},
99 { .name
= "t", NVP_ASSERT
},
100 { .name
= "f", NVP_DEASSERT
},
101 { .name
= NULL
, .value
= -1 }
104 static const Jim_Nvp nvp_error_target
[] = {
105 { .value
= ERROR_TARGET_INVALID
, .name
= "err-invalid" },
106 { .value
= ERROR_TARGET_INIT_FAILED
, .name
= "err-init-failed" },
107 { .value
= ERROR_TARGET_TIMEOUT
, .name
= "err-timeout" },
108 { .value
= ERROR_TARGET_NOT_HALTED
, .name
= "err-not-halted" },
109 { .value
= ERROR_TARGET_FAILURE
, .name
= "err-failure" },
110 { .value
= ERROR_TARGET_UNALIGNED_ACCESS
, .name
= "err-unaligned-access" },
111 { .value
= ERROR_TARGET_DATA_ABORT
, .name
= "err-data-abort" },
112 { .value
= ERROR_TARGET_RESOURCE_NOT_AVAILABLE
, .name
= "err-resource-not-available" },
113 { .value
= ERROR_TARGET_TRANSLATION_FAULT
, .name
= "err-translation-fault" },
114 { .value
= ERROR_TARGET_NOT_RUNNING
, .name
= "err-not-running" },
115 { .value
= ERROR_TARGET_NOT_EXAMINED
, .name
= "err-not-examined" },
116 { .value
= -1, .name
= NULL
}
119 const char *target_strerror_safe(int err
)
123 n
= Jim_Nvp_value2name_simple(nvp_error_target
, err
);
124 if (n
->name
== NULL
) {
131 static const Jim_Nvp nvp_target_event
[] = {
132 { .value
= TARGET_EVENT_OLD_gdb_program_config
, .name
= "old-gdb_program_config" },
133 { .value
= TARGET_EVENT_OLD_pre_resume
, .name
= "old-pre_resume" },
135 { .value
= TARGET_EVENT_GDB_HALT
, .name
= "gdb-halt" },
136 { .value
= TARGET_EVENT_HALTED
, .name
= "halted" },
137 { .value
= TARGET_EVENT_RESUMED
, .name
= "resumed" },
138 { .value
= TARGET_EVENT_RESUME_START
, .name
= "resume-start" },
139 { .value
= TARGET_EVENT_RESUME_END
, .name
= "resume-end" },
141 { .name
= "gdb-start", .value
= TARGET_EVENT_GDB_START
},
142 { .name
= "gdb-end", .value
= TARGET_EVENT_GDB_END
},
144 /* historical name */
146 { .value
= TARGET_EVENT_RESET_START
, .name
= "reset-start" },
148 { .value
= TARGET_EVENT_RESET_ASSERT_PRE
, .name
= "reset-assert-pre" },
149 { .value
= TARGET_EVENT_RESET_ASSERT
, .name
= "reset-assert" },
150 { .value
= TARGET_EVENT_RESET_ASSERT_POST
, .name
= "reset-assert-post" },
151 { .value
= TARGET_EVENT_RESET_DEASSERT_PRE
, .name
= "reset-deassert-pre" },
152 { .value
= TARGET_EVENT_RESET_DEASSERT_POST
, .name
= "reset-deassert-post" },
153 { .value
= TARGET_EVENT_RESET_HALT_PRE
, .name
= "reset-halt-pre" },
154 { .value
= TARGET_EVENT_RESET_HALT_POST
, .name
= "reset-halt-post" },
155 { .value
= TARGET_EVENT_RESET_WAIT_PRE
, .name
= "reset-wait-pre" },
156 { .value
= TARGET_EVENT_RESET_WAIT_POST
, .name
= "reset-wait-post" },
157 { .value
= TARGET_EVENT_RESET_INIT
, .name
= "reset-init" },
158 { .value
= TARGET_EVENT_RESET_END
, .name
= "reset-end" },
160 { .value
= TARGET_EVENT_EXAMINE_START
, .name
= "examine-start" },
161 { .value
= TARGET_EVENT_EXAMINE_END
, .name
= "examine-end" },
163 { .value
= TARGET_EVENT_DEBUG_HALTED
, .name
= "debug-halted" },
164 { .value
= TARGET_EVENT_DEBUG_RESUMED
, .name
= "debug-resumed" },
166 { .value
= TARGET_EVENT_GDB_ATTACH
, .name
= "gdb-attach" },
167 { .value
= TARGET_EVENT_GDB_DETACH
, .name
= "gdb-detach" },
169 { .value
= TARGET_EVENT_GDB_FLASH_WRITE_START
, .name
= "gdb-flash-write-start" },
170 { .value
= TARGET_EVENT_GDB_FLASH_WRITE_END
, .name
= "gdb-flash-write-end" },
172 { .value
= TARGET_EVENT_GDB_FLASH_ERASE_START
, .name
= "gdb-flash-erase-start" },
173 { .value
= TARGET_EVENT_GDB_FLASH_ERASE_END
, .name
= "gdb-flash-erase-end" },
175 { .value
= TARGET_EVENT_RESUME_START
, .name
= "resume-start" },
176 { .value
= TARGET_EVENT_RESUMED
, .name
= "resume-ok" },
177 { .value
= TARGET_EVENT_RESUME_END
, .name
= "resume-end" },
179 { .name
= NULL
, .value
= -1 }
182 static const Jim_Nvp nvp_target_state
[] = {
183 { .name
= "unknown", .value
= TARGET_UNKNOWN
},
184 { .name
= "running", .value
= TARGET_RUNNING
},
185 { .name
= "halted", .value
= TARGET_HALTED
},
186 { .name
= "reset", .value
= TARGET_RESET
},
187 { .name
= "debug-running", .value
= TARGET_DEBUG_RUNNING
},
188 { .name
= NULL
, .value
= -1 },
191 static const Jim_Nvp nvp_target_debug_reason
[] = {
192 { .name
= "debug-request" , .value
= DBG_REASON_DBGRQ
},
193 { .name
= "breakpoint" , .value
= DBG_REASON_BREAKPOINT
},
194 { .name
= "watchpoint" , .value
= DBG_REASON_WATCHPOINT
},
195 { .name
= "watchpoint-and-breakpoint", .value
= DBG_REASON_WPTANDBKPT
},
196 { .name
= "single-step" , .value
= DBG_REASON_SINGLESTEP
},
197 { .name
= "target-not-halted" , .value
= DBG_REASON_NOTHALTED
},
198 { .name
= "undefined" , .value
= DBG_REASON_UNDEFINED
},
199 { .name
= NULL
, .value
= -1 },
202 static const Jim_Nvp nvp_target_endian
[] = {
203 { .name
= "big", .value
= TARGET_BIG_ENDIAN
},
204 { .name
= "little", .value
= TARGET_LITTLE_ENDIAN
},
205 { .name
= "be", .value
= TARGET_BIG_ENDIAN
},
206 { .name
= "le", .value
= TARGET_LITTLE_ENDIAN
},
207 { .name
= NULL
, .value
= -1 },
210 static const Jim_Nvp nvp_reset_modes
[] = {
211 { .name
= "unknown", .value
= RESET_UNKNOWN
},
212 { .name
= "run" , .value
= RESET_RUN
},
213 { .name
= "halt" , .value
= RESET_HALT
},
214 { .name
= "init" , .value
= RESET_INIT
},
215 { .name
= NULL
, .value
= -1 },
218 const char *debug_reason_name(struct target
*t
)
222 cp
= Jim_Nvp_value2name_simple(nvp_target_debug_reason
,
223 t
->debug_reason
)->name
;
225 LOG_ERROR("Invalid debug reason: %d", (int)(t
->debug_reason
));
226 cp
= "(*BUG*unknown*BUG*)";
232 target_state_name( struct target
*t
)
235 cp
= Jim_Nvp_value2name_simple(nvp_target_state
, t
->state
)->name
;
237 LOG_ERROR("Invalid target state: %d", (int)(t
->state
));
238 cp
= "(*BUG*unknown*BUG*)";
243 /* determine the number of the new target */
244 static int new_target_number(void)
249 /* number is 0 based */
253 if (x
< t
->target_number
) {
254 x
= t
->target_number
;
261 /* read a uint32_t from a buffer in target memory endianness */
262 uint32_t target_buffer_get_u32(struct target
*target
, const uint8_t *buffer
)
264 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
265 return le_to_h_u32(buffer
);
267 return be_to_h_u32(buffer
);
270 /* read a uint16_t from a buffer in target memory endianness */
271 uint16_t target_buffer_get_u16(struct target
*target
, const uint8_t *buffer
)
273 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
274 return le_to_h_u16(buffer
);
276 return be_to_h_u16(buffer
);
279 /* read a uint8_t from a buffer in target memory endianness */
280 uint8_t target_buffer_get_u8(struct target
*target
, const uint8_t *buffer
)
282 return *buffer
& 0x0ff;
285 /* write a uint32_t to a buffer in target memory endianness */
286 void target_buffer_set_u32(struct target
*target
, uint8_t *buffer
, uint32_t value
)
288 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
289 h_u32_to_le(buffer
, value
);
291 h_u32_to_be(buffer
, value
);
294 /* write a uint16_t to a buffer in target memory endianness */
295 void target_buffer_set_u16(struct target
*target
, uint8_t *buffer
, uint16_t value
)
297 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
298 h_u16_to_le(buffer
, value
);
300 h_u16_to_be(buffer
, value
);
303 /* write a uint8_t to a buffer in target memory endianness */
304 void target_buffer_set_u8(struct target
*target
, uint8_t *buffer
, uint8_t value
)
309 /* return a pointer to a configured target; id is name or number */
310 struct target
*get_target(const char *id
)
312 struct target
*target
;
314 /* try as tcltarget name */
315 for (target
= all_targets
; target
; target
= target
->next
) {
316 if (target
->cmd_name
== NULL
)
318 if (strcmp(id
, target
->cmd_name
) == 0)
322 /* It's OK to remove this fallback sometime after August 2010 or so */
324 /* no match, try as number */
326 if (parse_uint(id
, &num
) != ERROR_OK
)
329 for (target
= all_targets
; target
; target
= target
->next
) {
330 if (target
->target_number
== (int)num
) {
331 LOG_WARNING("use '%s' as target identifier, not '%u'",
332 target
->cmd_name
, num
);
340 /* returns a pointer to the n-th configured target */
341 static struct target
*get_target_by_num(int num
)
343 struct target
*target
= all_targets
;
346 if (target
->target_number
== num
) {
349 target
= target
->next
;
355 struct target
* get_current_target(struct command_context
*cmd_ctx
)
357 struct target
*target
= get_target_by_num(cmd_ctx
->current_target
);
361 LOG_ERROR("BUG: current_target out of bounds");
368 int target_poll(struct target
*target
)
372 /* We can't poll until after examine */
373 if (!target_was_examined(target
))
375 /* Fail silently lest we pollute the log */
379 retval
= target
->type
->poll(target
);
380 if (retval
!= ERROR_OK
)
383 if (target
->halt_issued
)
385 if (target
->state
== TARGET_HALTED
)
387 target
->halt_issued
= false;
390 long long t
= timeval_ms() - target
->halt_issued_time
;
393 target
->halt_issued
= false;
394 LOG_INFO("Halt timed out, wake up GDB.");
395 target_call_event_callbacks(target
, TARGET_EVENT_GDB_HALT
);
403 int target_halt(struct target
*target
)
406 /* We can't poll until after examine */
407 if (!target_was_examined(target
))
409 LOG_ERROR("Target not examined yet");
413 retval
= target
->type
->halt(target
);
414 if (retval
!= ERROR_OK
)
417 target
->halt_issued
= true;
418 target
->halt_issued_time
= timeval_ms();
423 int target_resume(struct target
*target
, int current
, uint32_t address
, int handle_breakpoints
, int debug_execution
)
427 /* We can't poll until after examine */
428 if (!target_was_examined(target
))
430 LOG_ERROR("Target not examined yet");
434 /* note that resume *must* be asynchronous. The CPU can halt before we poll. The CPU can
435 * even halt at the current PC as a result of a software breakpoint being inserted by (a bug?)
438 if ((retval
= target
->type
->resume(target
, current
, address
, handle_breakpoints
, debug_execution
)) != ERROR_OK
)
444 int target_process_reset(struct command_context
*cmd_ctx
, enum target_reset_mode reset_mode
)
449 n
= Jim_Nvp_value2name_simple(nvp_reset_modes
, reset_mode
);
450 if (n
->name
== NULL
) {
451 LOG_ERROR("invalid reset mode");
455 /* disable polling during reset to make reset event scripts
456 * more predictable, i.e. dr/irscan & pathmove in events will
457 * not have JTAG operations injected into the middle of a sequence.
459 bool save_poll
= jtag_poll_get_enabled();
461 jtag_poll_set_enabled(false);
463 sprintf(buf
, "ocd_process_reset %s", n
->name
);
464 retval
= Jim_Eval(cmd_ctx
->interp
, buf
);
466 jtag_poll_set_enabled(save_poll
);
468 if (retval
!= JIM_OK
) {
469 Jim_PrintErrorMessage(cmd_ctx
->interp
);
473 /* We want any events to be processed before the prompt */
474 retval
= target_call_timer_callbacks_now();
479 static int identity_virt2phys(struct target
*target
,
480 uint32_t virtual, uint32_t *physical
)
486 static int no_mmu(struct target
*target
, int *enabled
)
492 static int default_examine(struct target
*target
)
494 target_set_examined(target
);
498 int target_examine_one(struct target
*target
)
500 return target
->type
->examine(target
);
503 static int jtag_enable_callback(enum jtag_event event
, void *priv
)
505 struct target
*target
= priv
;
507 if (event
!= JTAG_TAP_EVENT_ENABLE
|| !target
->tap
->enabled
)
510 jtag_unregister_event_callback(jtag_enable_callback
, target
);
511 return target_examine_one(target
);
515 /* Targets that correctly implement init + examine, i.e.
516 * no communication with target during init:
520 int target_examine(void)
522 int retval
= ERROR_OK
;
523 struct target
*target
;
525 for (target
= all_targets
; target
; target
= target
->next
)
527 /* defer examination, but don't skip it */
528 if (!target
->tap
->enabled
) {
529 jtag_register_event_callback(jtag_enable_callback
,
533 if ((retval
= target_examine_one(target
)) != ERROR_OK
)
538 const char *target_type_name(struct target
*target
)
540 return target
->type
->name
;
543 static int target_write_memory_imp(struct target
*target
, uint32_t address
, uint32_t size
, uint32_t count
, uint8_t *buffer
)
545 if (!target_was_examined(target
))
547 LOG_ERROR("Target not examined yet");
550 return target
->type
->write_memory_imp(target
, address
, size
, count
, buffer
);
553 static int target_read_memory_imp(struct target
*target
, uint32_t address
, uint32_t size
, uint32_t count
, uint8_t *buffer
)
555 if (!target_was_examined(target
))
557 LOG_ERROR("Target not examined yet");
560 return target
->type
->read_memory_imp(target
, address
, size
, count
, buffer
);
563 static int target_soft_reset_halt_imp(struct target
*target
)
565 if (!target_was_examined(target
))
567 LOG_ERROR("Target not examined yet");
570 if (!target
->type
->soft_reset_halt_imp
) {
571 LOG_ERROR("Target %s does not support soft_reset_halt",
572 target_name(target
));
575 return target
->type
->soft_reset_halt_imp(target
);
578 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
)
580 if (!target_was_examined(target
))
582 LOG_ERROR("Target not examined yet");
585 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
);
588 int target_read_memory(struct target
*target
,
589 uint32_t address
, uint32_t size
, uint32_t count
, uint8_t *buffer
)
591 return target
->type
->read_memory(target
, address
, size
, count
, buffer
);
594 int target_read_phys_memory(struct target
*target
,
595 uint32_t address
, uint32_t size
, uint32_t count
, uint8_t *buffer
)
597 return target
->type
->read_phys_memory(target
, address
, size
, count
, buffer
);
600 int target_write_memory(struct target
*target
,
601 uint32_t address
, uint32_t size
, uint32_t count
, uint8_t *buffer
)
603 return target
->type
->write_memory(target
, address
, size
, count
, buffer
);
606 int target_write_phys_memory(struct target
*target
,
607 uint32_t address
, uint32_t size
, uint32_t count
, uint8_t *buffer
)
609 return target
->type
->write_phys_memory(target
, address
, size
, count
, buffer
);
612 int target_bulk_write_memory(struct target
*target
,
613 uint32_t address
, uint32_t count
, uint8_t *buffer
)
615 return target
->type
->bulk_write_memory(target
, address
, count
, buffer
);
618 int target_add_breakpoint(struct target
*target
,
619 struct breakpoint
*breakpoint
)
621 if (target
->state
!= TARGET_HALTED
) {
622 LOG_WARNING("target %s is not halted", target
->cmd_name
);
623 return ERROR_TARGET_NOT_HALTED
;
625 return target
->type
->add_breakpoint(target
, breakpoint
);
627 int target_remove_breakpoint(struct target
*target
,
628 struct breakpoint
*breakpoint
)
630 return target
->type
->remove_breakpoint(target
, breakpoint
);
633 int target_add_watchpoint(struct target
*target
,
634 struct watchpoint
*watchpoint
)
636 if (target
->state
!= TARGET_HALTED
) {
637 LOG_WARNING("target %s is not halted", target
->cmd_name
);
638 return ERROR_TARGET_NOT_HALTED
;
640 return target
->type
->add_watchpoint(target
, watchpoint
);
642 int target_remove_watchpoint(struct target
*target
,
643 struct watchpoint
*watchpoint
)
645 return target
->type
->remove_watchpoint(target
, watchpoint
);
648 int target_get_gdb_reg_list(struct target
*target
,
649 struct reg
**reg_list
[], int *reg_list_size
)
651 return target
->type
->get_gdb_reg_list(target
, reg_list
, reg_list_size
);
653 int target_step(struct target
*target
,
654 int current
, uint32_t address
, int handle_breakpoints
)
656 return target
->type
->step(target
, current
, address
, handle_breakpoints
);
660 int target_run_algorithm(struct target
*target
,
661 int num_mem_params
, struct mem_param
*mem_params
,
662 int num_reg_params
, struct reg_param
*reg_param
,
663 uint32_t entry_point
, uint32_t exit_point
,
664 int timeout_ms
, void *arch_info
)
666 return target
->type
->run_algorithm(target
,
667 num_mem_params
, mem_params
, num_reg_params
, reg_param
,
668 entry_point
, exit_point
, timeout_ms
, arch_info
);
672 * Reset the @c examined flag for the given target.
673 * Pure paranoia -- targets are zeroed on allocation.
675 static void target_reset_examined(struct target
*target
)
677 target
->examined
= false;
681 err_read_phys_memory(struct target
*target
, uint32_t address
,
682 uint32_t size
, uint32_t count
, uint8_t *buffer
)
684 LOG_ERROR("Not implemented: %s", __func__
);
689 err_write_phys_memory(struct target
*target
, uint32_t address
,
690 uint32_t size
, uint32_t count
, uint8_t *buffer
)
692 LOG_ERROR("Not implemented: %s", __func__
);
696 static int handle_target(void *priv
);
698 static int target_init_one(struct command_context
*cmd_ctx
,
699 struct target
*target
)
701 target_reset_examined(target
);
703 struct target_type
*type
= target
->type
;
704 if (type
->examine
== NULL
)
705 type
->examine
= default_examine
;
707 int retval
= type
->init_target(cmd_ctx
, target
);
708 if (ERROR_OK
!= retval
)
710 LOG_ERROR("target '%s' init failed", target_name(target
));
715 * @todo get rid of those *memory_imp() methods, now that all
716 * callers are using target_*_memory() accessors ... and make
717 * sure the "physical" paths handle the same issues.
719 /* a non-invasive way(in terms of patches) to add some code that
720 * runs before the type->write/read_memory implementation
722 type
->write_memory_imp
= target
->type
->write_memory
;
723 type
->write_memory
= target_write_memory_imp
;
725 type
->read_memory_imp
= target
->type
->read_memory
;
726 type
->read_memory
= target_read_memory_imp
;
728 type
->soft_reset_halt_imp
= target
->type
->soft_reset_halt
;
729 type
->soft_reset_halt
= target_soft_reset_halt_imp
;
731 type
->run_algorithm_imp
= target
->type
->run_algorithm
;
732 type
->run_algorithm
= target_run_algorithm_imp
;
734 /* Sanity-check MMU support ... stub in what we must, to help
735 * implement it in stages, but warn if we need to do so.
739 if (type
->write_phys_memory
== NULL
)
741 LOG_ERROR("type '%s' is missing write_phys_memory",
743 type
->write_phys_memory
= err_write_phys_memory
;
745 if (type
->read_phys_memory
== NULL
)
747 LOG_ERROR("type '%s' is missing read_phys_memory",
749 type
->read_phys_memory
= err_read_phys_memory
;
751 if (type
->virt2phys
== NULL
)
753 LOG_ERROR("type '%s' is missing virt2phys", type
->name
);
754 type
->virt2phys
= identity_virt2phys
;
759 /* Make sure no-MMU targets all behave the same: make no
760 * distinction between physical and virtual addresses, and
761 * ensure that virt2phys() is always an identity mapping.
763 if (type
->write_phys_memory
|| type
->read_phys_memory
766 LOG_WARNING("type '%s' has bad MMU hooks", type
->name
);
770 type
->write_phys_memory
= type
->write_memory
;
771 type
->read_phys_memory
= type
->read_memory
;
772 type
->virt2phys
= identity_virt2phys
;
777 int target_init(struct command_context
*cmd_ctx
)
779 struct target
*target
;
782 for (target
= all_targets
; target
; target
= target
->next
)
784 retval
= target_init_one(cmd_ctx
, target
);
785 if (ERROR_OK
!= retval
)
792 retval
= target_register_user_commands(cmd_ctx
);
793 if (ERROR_OK
!= retval
)
796 retval
= target_register_timer_callback(&handle_target
,
797 100, 1, cmd_ctx
->interp
);
798 if (ERROR_OK
!= retval
)
804 COMMAND_HANDLER(handle_target_init_command
)
807 return ERROR_COMMAND_SYNTAX_ERROR
;
809 static bool target_initialized
= false;
810 if (target_initialized
)
812 LOG_INFO("'target init' has already been called");
815 target_initialized
= true;
817 LOG_DEBUG("Initializing targets...");
818 return target_init(CMD_CTX
);
821 int target_register_event_callback(int (*callback
)(struct target
*target
, enum target_event event
, void *priv
), void *priv
)
823 struct target_event_callback
**callbacks_p
= &target_event_callbacks
;
825 if (callback
== NULL
)
827 return ERROR_INVALID_ARGUMENTS
;
832 while ((*callbacks_p
)->next
)
833 callbacks_p
= &((*callbacks_p
)->next
);
834 callbacks_p
= &((*callbacks_p
)->next
);
837 (*callbacks_p
) = malloc(sizeof(struct target_event_callback
));
838 (*callbacks_p
)->callback
= callback
;
839 (*callbacks_p
)->priv
= priv
;
840 (*callbacks_p
)->next
= NULL
;
845 int target_register_timer_callback(int (*callback
)(void *priv
), int time_ms
, int periodic
, void *priv
)
847 struct target_timer_callback
**callbacks_p
= &target_timer_callbacks
;
850 if (callback
== NULL
)
852 return ERROR_INVALID_ARGUMENTS
;
857 while ((*callbacks_p
)->next
)
858 callbacks_p
= &((*callbacks_p
)->next
);
859 callbacks_p
= &((*callbacks_p
)->next
);
862 (*callbacks_p
) = malloc(sizeof(struct target_timer_callback
));
863 (*callbacks_p
)->callback
= callback
;
864 (*callbacks_p
)->periodic
= periodic
;
865 (*callbacks_p
)->time_ms
= time_ms
;
867 gettimeofday(&now
, NULL
);
868 (*callbacks_p
)->when
.tv_usec
= now
.tv_usec
+ (time_ms
% 1000) * 1000;
869 time_ms
-= (time_ms
% 1000);
870 (*callbacks_p
)->when
.tv_sec
= now
.tv_sec
+ (time_ms
/ 1000);
871 if ((*callbacks_p
)->when
.tv_usec
> 1000000)
873 (*callbacks_p
)->when
.tv_usec
= (*callbacks_p
)->when
.tv_usec
- 1000000;
874 (*callbacks_p
)->when
.tv_sec
+= 1;
877 (*callbacks_p
)->priv
= priv
;
878 (*callbacks_p
)->next
= NULL
;
883 int target_unregister_event_callback(int (*callback
)(struct target
*target
, enum target_event event
, void *priv
), void *priv
)
885 struct target_event_callback
**p
= &target_event_callbacks
;
886 struct target_event_callback
*c
= target_event_callbacks
;
888 if (callback
== NULL
)
890 return ERROR_INVALID_ARGUMENTS
;
895 struct target_event_callback
*next
= c
->next
;
896 if ((c
->callback
== callback
) && (c
->priv
== priv
))
910 int target_unregister_timer_callback(int (*callback
)(void *priv
), void *priv
)
912 struct target_timer_callback
**p
= &target_timer_callbacks
;
913 struct target_timer_callback
*c
= target_timer_callbacks
;
915 if (callback
== NULL
)
917 return ERROR_INVALID_ARGUMENTS
;
922 struct target_timer_callback
*next
= c
->next
;
923 if ((c
->callback
== callback
) && (c
->priv
== priv
))
937 int target_call_event_callbacks(struct target
*target
, enum target_event event
)
939 struct target_event_callback
*callback
= target_event_callbacks
;
940 struct target_event_callback
*next_callback
;
942 if (event
== TARGET_EVENT_HALTED
)
944 /* execute early halted first */
945 target_call_event_callbacks(target
, TARGET_EVENT_GDB_HALT
);
948 LOG_DEBUG("target event %i (%s)",
950 Jim_Nvp_value2name_simple(nvp_target_event
, event
)->name
);
952 target_handle_event(target
, event
);
956 next_callback
= callback
->next
;
957 callback
->callback(target
, event
, callback
->priv
);
958 callback
= next_callback
;
964 static int target_timer_callback_periodic_restart(
965 struct target_timer_callback
*cb
, struct timeval
*now
)
967 int time_ms
= cb
->time_ms
;
968 cb
->when
.tv_usec
= now
->tv_usec
+ (time_ms
% 1000) * 1000;
969 time_ms
-= (time_ms
% 1000);
970 cb
->when
.tv_sec
= now
->tv_sec
+ time_ms
/ 1000;
971 if (cb
->when
.tv_usec
> 1000000)
973 cb
->when
.tv_usec
= cb
->when
.tv_usec
- 1000000;
974 cb
->when
.tv_sec
+= 1;
979 static int target_call_timer_callback(struct target_timer_callback
*cb
,
982 cb
->callback(cb
->priv
);
985 return target_timer_callback_periodic_restart(cb
, now
);
987 return target_unregister_timer_callback(cb
->callback
, cb
->priv
);
990 static int target_call_timer_callbacks_check_time(int checktime
)
995 gettimeofday(&now
, NULL
);
997 struct target_timer_callback
*callback
= target_timer_callbacks
;
1000 // cleaning up may unregister and free this callback
1001 struct target_timer_callback
*next_callback
= callback
->next
;
1003 bool call_it
= callback
->callback
&&
1004 ((!checktime
&& callback
->periodic
) ||
1005 now
.tv_sec
> callback
->when
.tv_sec
||
1006 (now
.tv_sec
== callback
->when
.tv_sec
&&
1007 now
.tv_usec
>= callback
->when
.tv_usec
));
1011 int retval
= target_call_timer_callback(callback
, &now
);
1012 if (retval
!= ERROR_OK
)
1016 callback
= next_callback
;
1022 int target_call_timer_callbacks(void)
1024 return target_call_timer_callbacks_check_time(1);
1027 /* invoke periodic callbacks immediately */
1028 int target_call_timer_callbacks_now(void)
1030 return target_call_timer_callbacks_check_time(0);
1033 int target_alloc_working_area(struct target
*target
, uint32_t size
, struct working_area
**area
)
1035 struct working_area
*c
= target
->working_areas
;
1036 struct working_area
*new_wa
= NULL
;
1038 /* Reevaluate working area address based on MMU state*/
1039 if (target
->working_areas
== NULL
)
1044 retval
= target
->type
->mmu(target
, &enabled
);
1045 if (retval
!= ERROR_OK
)
1051 if (target
->working_area_phys_spec
) {
1052 LOG_DEBUG("MMU disabled, using physical "
1053 "address for working memory 0x%08x",
1054 (unsigned)target
->working_area_phys
);
1055 target
->working_area
= target
->working_area_phys
;
1057 LOG_ERROR("No working memory available. "
1058 "Specify -work-area-phys to target.");
1059 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
1062 if (target
->working_area_virt_spec
) {
1063 LOG_DEBUG("MMU enabled, using virtual "
1064 "address for working memory 0x%08x",
1065 (unsigned)target
->working_area_virt
);
1066 target
->working_area
= target
->working_area_virt
;
1068 LOG_ERROR("No working memory available. "
1069 "Specify -work-area-virt to target.");
1070 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
1075 /* only allocate multiples of 4 byte */
1078 LOG_ERROR("BUG: code tried to allocate unaligned number of bytes (0x%08x), padding", ((unsigned)(size
)));
1079 size
= (size
+ 3) & (~3);
1082 /* see if there's already a matching working area */
1085 if ((c
->free
) && (c
->size
== size
))
1093 /* if not, allocate a new one */
1096 struct working_area
**p
= &target
->working_areas
;
1097 uint32_t first_free
= target
->working_area
;
1098 uint32_t free_size
= target
->working_area_size
;
1100 c
= target
->working_areas
;
1103 first_free
+= c
->size
;
1104 free_size
-= c
->size
;
1109 if (free_size
< size
)
1111 LOG_WARNING("not enough working area available(requested %u, free %u)",
1112 (unsigned)(size
), (unsigned)(free_size
));
1113 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
1116 LOG_DEBUG("allocated new working area at address 0x%08x", (unsigned)first_free
);
1118 new_wa
= malloc(sizeof(struct working_area
));
1119 new_wa
->next
= NULL
;
1120 new_wa
->size
= size
;
1121 new_wa
->address
= first_free
;
1123 if (target
->backup_working_area
)
1126 new_wa
->backup
= malloc(new_wa
->size
);
1127 if ((retval
= target_read_memory(target
, new_wa
->address
, 4, new_wa
->size
/ 4, new_wa
->backup
)) != ERROR_OK
)
1129 free(new_wa
->backup
);
1136 new_wa
->backup
= NULL
;
1139 /* put new entry in list */
1143 /* mark as used, and return the new (reused) area */
1148 new_wa
->user
= area
;
1153 int target_free_working_area_restore(struct target
*target
, struct working_area
*area
, int restore
)
1158 if (restore
&& target
->backup_working_area
)
1161 if ((retval
= target_write_memory(target
, area
->address
, 4, area
->size
/ 4, area
->backup
)) != ERROR_OK
)
1167 /* mark user pointer invalid */
1174 int target_free_working_area(struct target
*target
, struct working_area
*area
)
1176 return target_free_working_area_restore(target
, area
, 1);
1179 /* free resources and restore memory, if restoring memory fails,
1180 * free up resources anyway
1182 void target_free_all_working_areas_restore(struct target
*target
, int restore
)
1184 struct working_area
*c
= target
->working_areas
;
1188 struct working_area
*next
= c
->next
;
1189 target_free_working_area_restore(target
, c
, restore
);
1199 target
->working_areas
= NULL
;
1202 void target_free_all_working_areas(struct target
*target
)
1204 target_free_all_working_areas_restore(target
, 1);
1207 int target_arch_state(struct target
*target
)
1212 LOG_USER("No target has been configured");
1216 LOG_USER("target state: %s", target_state_name( target
));
1218 if (target
->state
!= TARGET_HALTED
)
1221 retval
= target
->type
->arch_state(target
);
1225 /* Single aligned words are guaranteed to use 16 or 32 bit access
1226 * mode respectively, otherwise data is handled as quickly as
1229 int target_write_buffer(struct target
*target
, uint32_t address
, uint32_t size
, uint8_t *buffer
)
1232 LOG_DEBUG("writing buffer of %i byte at 0x%8.8x",
1233 (int)size
, (unsigned)address
);
1235 if (!target_was_examined(target
))
1237 LOG_ERROR("Target not examined yet");
1245 if ((address
+ size
- 1) < address
)
1247 /* GDB can request this when e.g. PC is 0xfffffffc*/
1248 LOG_ERROR("address + size wrapped(0x%08x, 0x%08x)",
1254 if (((address
% 2) == 0) && (size
== 2))
1256 return target_write_memory(target
, address
, 2, 1, buffer
);
1259 /* handle unaligned head bytes */
1262 uint32_t unaligned
= 4 - (address
% 4);
1264 if (unaligned
> size
)
1267 if ((retval
= target_write_memory(target
, address
, 1, unaligned
, buffer
)) != ERROR_OK
)
1270 buffer
+= unaligned
;
1271 address
+= unaligned
;
1275 /* handle aligned words */
1278 int aligned
= size
- (size
% 4);
1280 /* use bulk writes above a certain limit. This may have to be changed */
1283 if ((retval
= target
->type
->bulk_write_memory(target
, address
, aligned
/ 4, buffer
)) != ERROR_OK
)
1288 if ((retval
= target_write_memory(target
, address
, 4, aligned
/ 4, buffer
)) != ERROR_OK
)
1297 /* handle tail writes of less than 4 bytes */
1300 if ((retval
= target_write_memory(target
, address
, 1, size
, buffer
)) != ERROR_OK
)
1307 /* Single aligned words are guaranteed to use 16 or 32 bit access
1308 * mode respectively, otherwise data is handled as quickly as
1311 int target_read_buffer(struct target
*target
, uint32_t address
, uint32_t size
, uint8_t *buffer
)
1314 LOG_DEBUG("reading buffer of %i byte at 0x%8.8x",
1315 (int)size
, (unsigned)address
);
1317 if (!target_was_examined(target
))
1319 LOG_ERROR("Target not examined yet");
1327 if ((address
+ size
- 1) < address
)
1329 /* GDB can request this when e.g. PC is 0xfffffffc*/
1330 LOG_ERROR("address + size wrapped(0x%08" PRIx32
", 0x%08" PRIx32
")",
1336 if (((address
% 2) == 0) && (size
== 2))
1338 return target_read_memory(target
, address
, 2, 1, buffer
);
1341 /* handle unaligned head bytes */
1344 uint32_t unaligned
= 4 - (address
% 4);
1346 if (unaligned
> size
)
1349 if ((retval
= target_read_memory(target
, address
, 1, unaligned
, buffer
)) != ERROR_OK
)
1352 buffer
+= unaligned
;
1353 address
+= unaligned
;
1357 /* handle aligned words */
1360 int aligned
= size
- (size
% 4);
1362 if ((retval
= target_read_memory(target
, address
, 4, aligned
/ 4, buffer
)) != ERROR_OK
)
1370 /*prevent byte access when possible (avoid AHB access limitations in some cases)*/
1373 int aligned
= size
- (size
%2);
1374 retval
= target_read_memory(target
, address
, 2, aligned
/ 2, buffer
);
1375 if (retval
!= ERROR_OK
)
1382 /* handle tail writes of less than 4 bytes */
1385 if ((retval
= target_read_memory(target
, address
, 1, size
, buffer
)) != ERROR_OK
)
1392 int target_checksum_memory(struct target
*target
, uint32_t address
, uint32_t size
, uint32_t* crc
)
1397 uint32_t checksum
= 0;
1398 if (!target_was_examined(target
))
1400 LOG_ERROR("Target not examined yet");
1404 if ((retval
= target
->type
->checksum_memory(target
, address
,
1405 size
, &checksum
)) != ERROR_OK
)
1407 buffer
= malloc(size
);
1410 LOG_ERROR("error allocating buffer for section (%d bytes)", (int)size
);
1411 return ERROR_INVALID_ARGUMENTS
;
1413 retval
= target_read_buffer(target
, address
, size
, buffer
);
1414 if (retval
!= ERROR_OK
)
1420 /* convert to target endianess */
1421 for (i
= 0; i
< (size
/sizeof(uint32_t)); i
++)
1423 uint32_t target_data
;
1424 target_data
= target_buffer_get_u32(target
, &buffer
[i
*sizeof(uint32_t)]);
1425 target_buffer_set_u32(target
, &buffer
[i
*sizeof(uint32_t)], target_data
);
1428 retval
= image_calculate_checksum(buffer
, size
, &checksum
);
1437 int target_blank_check_memory(struct target
*target
, uint32_t address
, uint32_t size
, uint32_t* blank
)
1440 if (!target_was_examined(target
))
1442 LOG_ERROR("Target not examined yet");
1446 if (target
->type
->blank_check_memory
== 0)
1447 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
1449 retval
= target
->type
->blank_check_memory(target
, address
, size
, blank
);
1454 int target_read_u32(struct target
*target
, uint32_t address
, uint32_t *value
)
1456 uint8_t value_buf
[4];
1457 if (!target_was_examined(target
))
1459 LOG_ERROR("Target not examined yet");
1463 int retval
= target_read_memory(target
, address
, 4, 1, value_buf
);
1465 if (retval
== ERROR_OK
)
1467 *value
= target_buffer_get_u32(target
, value_buf
);
1468 LOG_DEBUG("address: 0x%8.8" PRIx32
", value: 0x%8.8" PRIx32
"",
1475 LOG_DEBUG("address: 0x%8.8" PRIx32
" failed",
1482 int target_read_u16(struct target
*target
, uint32_t address
, uint16_t *value
)
1484 uint8_t value_buf
[2];
1485 if (!target_was_examined(target
))
1487 LOG_ERROR("Target not examined yet");
1491 int retval
= target_read_memory(target
, address
, 2, 1, value_buf
);
1493 if (retval
== ERROR_OK
)
1495 *value
= target_buffer_get_u16(target
, value_buf
);
1496 LOG_DEBUG("address: 0x%8.8" PRIx32
", value: 0x%4.4x",
1503 LOG_DEBUG("address: 0x%8.8" PRIx32
" failed",
1510 int target_read_u8(struct target
*target
, uint32_t address
, uint8_t *value
)
1512 int retval
= target_read_memory(target
, address
, 1, 1, value
);
1513 if (!target_was_examined(target
))
1515 LOG_ERROR("Target not examined yet");
1519 if (retval
== ERROR_OK
)
1521 LOG_DEBUG("address: 0x%8.8" PRIx32
", value: 0x%2.2x",
1528 LOG_DEBUG("address: 0x%8.8" PRIx32
" failed",
1535 int target_write_u32(struct target
*target
, uint32_t address
, uint32_t value
)
1538 uint8_t value_buf
[4];
1539 if (!target_was_examined(target
))
1541 LOG_ERROR("Target not examined yet");
1545 LOG_DEBUG("address: 0x%8.8" PRIx32
", value: 0x%8.8" PRIx32
"",
1549 target_buffer_set_u32(target
, value_buf
, value
);
1550 if ((retval
= target_write_memory(target
, address
, 4, 1, value_buf
)) != ERROR_OK
)
1552 LOG_DEBUG("failed: %i", retval
);
1558 int target_write_u16(struct target
*target
, uint32_t address
, uint16_t value
)
1561 uint8_t value_buf
[2];
1562 if (!target_was_examined(target
))
1564 LOG_ERROR("Target not examined yet");
1568 LOG_DEBUG("address: 0x%8.8" PRIx32
", value: 0x%8.8x",
1572 target_buffer_set_u16(target
, value_buf
, value
);
1573 if ((retval
= target_write_memory(target
, address
, 2, 1, value_buf
)) != ERROR_OK
)
1575 LOG_DEBUG("failed: %i", retval
);
1581 int target_write_u8(struct target
*target
, uint32_t address
, uint8_t value
)
1584 if (!target_was_examined(target
))
1586 LOG_ERROR("Target not examined yet");
1590 LOG_DEBUG("address: 0x%8.8" PRIx32
", value: 0x%2.2x",
1593 if ((retval
= target_write_memory(target
, address
, 1, 1, &value
)) != ERROR_OK
)
1595 LOG_DEBUG("failed: %i", retval
);
1601 COMMAND_HANDLER(handle_targets_command
)
1603 struct target
*target
= all_targets
;
1607 target
= get_target(CMD_ARGV
[0]);
1608 if (target
== NULL
) {
1609 command_print(CMD_CTX
,"Target: %s is unknown, try one of:\n", CMD_ARGV
[0]);
1612 if (!target
->tap
->enabled
) {
1613 command_print(CMD_CTX
,"Target: TAP %s is disabled, "
1614 "can't be the current target\n",
1615 target
->tap
->dotted_name
);
1619 CMD_CTX
->current_target
= target
->target_number
;
1624 target
= all_targets
;
1625 command_print(CMD_CTX
, " TargetName Type Endian TapName State ");
1626 command_print(CMD_CTX
, "-- ------------------ ---------- ------ ------------------ ------------");
1632 if (target
->tap
->enabled
)
1633 state
= target_state_name( target
);
1635 state
= "tap-disabled";
1637 if (CMD_CTX
->current_target
== target
->target_number
)
1640 /* keep columns lined up to match the headers above */
1641 command_print(CMD_CTX
, "%2d%c %-18s %-10s %-6s %-18s %s",
1642 target
->target_number
,
1644 target_name(target
),
1645 target_type_name(target
),
1646 Jim_Nvp_value2name_simple(nvp_target_endian
,
1647 target
->endianness
)->name
,
1648 target
->tap
->dotted_name
,
1650 target
= target
->next
;
1656 /* every 300ms we check for reset & powerdropout and issue a "reset halt" if so. */
1658 static int powerDropout
;
1659 static int srstAsserted
;
1661 static int runPowerRestore
;
1662 static int runPowerDropout
;
1663 static int runSrstAsserted
;
1664 static int runSrstDeasserted
;
1666 static int sense_handler(void)
1668 static int prevSrstAsserted
= 0;
1669 static int prevPowerdropout
= 0;
1672 if ((retval
= jtag_power_dropout(&powerDropout
)) != ERROR_OK
)
1676 powerRestored
= prevPowerdropout
&& !powerDropout
;
1679 runPowerRestore
= 1;
1682 long long current
= timeval_ms();
1683 static long long lastPower
= 0;
1684 int waitMore
= lastPower
+ 2000 > current
;
1685 if (powerDropout
&& !waitMore
)
1687 runPowerDropout
= 1;
1688 lastPower
= current
;
1691 if ((retval
= jtag_srst_asserted(&srstAsserted
)) != ERROR_OK
)
1695 srstDeasserted
= prevSrstAsserted
&& !srstAsserted
;
1697 static long long lastSrst
= 0;
1698 waitMore
= lastSrst
+ 2000 > current
;
1699 if (srstDeasserted
&& !waitMore
)
1701 runSrstDeasserted
= 1;
1705 if (!prevSrstAsserted
&& srstAsserted
)
1707 runSrstAsserted
= 1;
1710 prevSrstAsserted
= srstAsserted
;
1711 prevPowerdropout
= powerDropout
;
1713 if (srstDeasserted
|| powerRestored
)
1715 /* Other than logging the event we can't do anything here.
1716 * Issuing a reset is a particularly bad idea as we might
1717 * be inside a reset already.
1724 static void target_call_event_callbacks_all(enum target_event e
) {
1725 struct target
*target
;
1726 target
= all_targets
;
1728 target_call_event_callbacks(target
, e
);
1729 target
= target
->next
;
1733 /* process target state changes */
1734 static int handle_target(void *priv
)
1736 Jim_Interp
*interp
= (Jim_Interp
*)priv
;
1737 int retval
= ERROR_OK
;
1739 /* we do not want to recurse here... */
1740 static int recursive
= 0;
1745 /* danger! running these procedures can trigger srst assertions and power dropouts.
1746 * We need to avoid an infinite loop/recursion here and we do that by
1747 * clearing the flags after running these events.
1749 int did_something
= 0;
1750 if (runSrstAsserted
)
1752 target_call_event_callbacks_all(TARGET_EVENT_GDB_HALT
);
1753 Jim_Eval(interp
, "srst_asserted");
1756 if (runSrstDeasserted
)
1758 Jim_Eval(interp
, "srst_deasserted");
1761 if (runPowerDropout
)
1763 target_call_event_callbacks_all(TARGET_EVENT_GDB_HALT
);
1764 Jim_Eval(interp
, "power_dropout");
1767 if (runPowerRestore
)
1769 Jim_Eval(interp
, "power_restore");
1775 /* clear detect flags */
1779 /* clear action flags */
1781 runSrstAsserted
= 0;
1782 runSrstDeasserted
= 0;
1783 runPowerRestore
= 0;
1784 runPowerDropout
= 0;
1789 /* Poll targets for state changes unless that's globally disabled.
1790 * Skip targets that are currently disabled.
1792 for (struct target
*target
= all_targets
;
1793 is_jtag_poll_safe() && target
;
1794 target
= target
->next
)
1796 if (!target
->tap
->enabled
)
1799 /* only poll target if we've got power and srst isn't asserted */
1800 if (!powerDropout
&& !srstAsserted
)
1802 /* polling may fail silently until the target has been examined */
1803 if ((retval
= target_poll(target
)) != ERROR_OK
)
1805 target_call_event_callbacks(target
, TARGET_EVENT_GDB_HALT
);
1814 COMMAND_HANDLER(handle_reg_command
)
1816 struct target
*target
;
1817 struct reg
*reg
= NULL
;
1823 target
= get_current_target(CMD_CTX
);
1825 /* list all available registers for the current target */
1828 struct reg_cache
*cache
= target
->reg_cache
;
1835 command_print(CMD_CTX
, "===== %s", cache
->name
);
1837 for (i
= 0, reg
= cache
->reg_list
;
1838 i
< cache
->num_regs
;
1839 i
++, reg
++, count
++)
1841 /* only print cached values if they are valid */
1843 value
= buf_to_str(reg
->value
,
1845 command_print(CMD_CTX
,
1846 "(%i) %s (/%" PRIu32
"): 0x%s%s",
1854 command_print(CMD_CTX
, "(%i) %s (/%" PRIu32
")",
1859 cache
= cache
->next
;
1865 /* access a single register by its ordinal number */
1866 if ((CMD_ARGV
[0][0] >= '0') && (CMD_ARGV
[0][0] <= '9'))
1869 COMMAND_PARSE_NUMBER(uint
, CMD_ARGV
[0], num
);
1871 struct reg_cache
*cache
= target
->reg_cache
;
1876 for (i
= 0; i
< cache
->num_regs
; i
++)
1880 reg
= &cache
->reg_list
[i
];
1886 cache
= cache
->next
;
1891 command_print(CMD_CTX
, "%i is out of bounds, the current target has only %i registers (0 - %i)", num
, count
, count
- 1);
1894 } else /* access a single register by its name */
1896 reg
= register_get_by_name(target
->reg_cache
, CMD_ARGV
[0], 1);
1900 command_print(CMD_CTX
, "register %s not found in current target", CMD_ARGV
[0]);
1905 /* display a register */
1906 if ((CMD_ARGC
== 1) || ((CMD_ARGC
== 2) && !((CMD_ARGV
[1][0] >= '0') && (CMD_ARGV
[1][0] <= '9'))))
1908 if ((CMD_ARGC
== 2) && (strcmp(CMD_ARGV
[1], "force") == 0))
1911 if (reg
->valid
== 0)
1913 reg
->type
->get(reg
);
1915 value
= buf_to_str(reg
->value
, reg
->size
, 16);
1916 command_print(CMD_CTX
, "%s (/%i): 0x%s", reg
->name
, (int)(reg
->size
), value
);
1921 /* set register value */
1924 uint8_t *buf
= malloc(DIV_ROUND_UP(reg
->size
, 8));
1925 str_to_buf(CMD_ARGV
[1], strlen(CMD_ARGV
[1]), buf
, reg
->size
, 0);
1927 reg
->type
->set(reg
, buf
);
1929 value
= buf_to_str(reg
->value
, reg
->size
, 16);
1930 command_print(CMD_CTX
, "%s (/%i): 0x%s", reg
->name
, (int)(reg
->size
), value
);
1938 command_print(CMD_CTX
, "usage: reg <#|name> [value]");
1943 COMMAND_HANDLER(handle_poll_command
)
1945 int retval
= ERROR_OK
;
1946 struct target
*target
= get_current_target(CMD_CTX
);
1950 command_print(CMD_CTX
, "background polling: %s",
1951 jtag_poll_get_enabled() ? "on" : "off");
1952 command_print(CMD_CTX
, "TAP: %s (%s)",
1953 target
->tap
->dotted_name
,
1954 target
->tap
->enabled
? "enabled" : "disabled");
1955 if (!target
->tap
->enabled
)
1957 if ((retval
= target_poll(target
)) != ERROR_OK
)
1959 if ((retval
= target_arch_state(target
)) != ERROR_OK
)
1962 else if (CMD_ARGC
== 1)
1965 COMMAND_PARSE_ON_OFF(CMD_ARGV
[0], enable
);
1966 jtag_poll_set_enabled(enable
);
1970 return ERROR_COMMAND_SYNTAX_ERROR
;
1976 COMMAND_HANDLER(handle_wait_halt_command
)
1979 return ERROR_COMMAND_SYNTAX_ERROR
;
1984 int retval
= parse_uint(CMD_ARGV
[0], &ms
);
1985 if (ERROR_OK
!= retval
)
1987 command_print(CMD_CTX
, "usage: %s [seconds]", CMD_NAME
);
1988 return ERROR_COMMAND_SYNTAX_ERROR
;
1990 // convert seconds (given) to milliseconds (needed)
1994 struct target
*target
= get_current_target(CMD_CTX
);
1995 return target_wait_state(target
, TARGET_HALTED
, ms
);
1998 /* wait for target state to change. The trick here is to have a low
1999 * latency for short waits and not to suck up all the CPU time
2002 * After 500ms, keep_alive() is invoked
2004 int target_wait_state(struct target
*target
, enum target_state state
, int ms
)
2007 long long then
= 0, cur
;
2012 if ((retval
= target_poll(target
)) != ERROR_OK
)
2014 if (target
->state
== state
)
2022 then
= timeval_ms();
2023 LOG_DEBUG("waiting for target %s...",
2024 Jim_Nvp_value2name_simple(nvp_target_state
,state
)->name
);
2032 if ((cur
-then
) > ms
)
2034 LOG_ERROR("timed out while waiting for target %s",
2035 Jim_Nvp_value2name_simple(nvp_target_state
,state
)->name
);
2043 COMMAND_HANDLER(handle_halt_command
)
2047 struct target
*target
= get_current_target(CMD_CTX
);
2048 int retval
= target_halt(target
);
2049 if (ERROR_OK
!= retval
)
2055 retval
= parse_uint(CMD_ARGV
[0], &wait
);
2056 if (ERROR_OK
!= retval
)
2057 return ERROR_COMMAND_SYNTAX_ERROR
;
2062 return CALL_COMMAND_HANDLER(handle_wait_halt_command
);
2065 COMMAND_HANDLER(handle_soft_reset_halt_command
)
2067 struct target
*target
= get_current_target(CMD_CTX
);
2069 LOG_USER("requesting target halt and executing a soft reset");
2071 target
->type
->soft_reset_halt(target
);
2076 COMMAND_HANDLER(handle_reset_command
)
2079 return ERROR_COMMAND_SYNTAX_ERROR
;
2081 enum target_reset_mode reset_mode
= RESET_RUN
;
2085 n
= Jim_Nvp_name2value_simple(nvp_reset_modes
, CMD_ARGV
[0]);
2086 if ((n
->name
== NULL
) || (n
->value
== RESET_UNKNOWN
)) {
2087 return ERROR_COMMAND_SYNTAX_ERROR
;
2089 reset_mode
= n
->value
;
2092 /* reset *all* targets */
2093 return target_process_reset(CMD_CTX
, reset_mode
);
2097 COMMAND_HANDLER(handle_resume_command
)
2101 return ERROR_COMMAND_SYNTAX_ERROR
;
2103 struct target
*target
= get_current_target(CMD_CTX
);
2104 target_handle_event(target
, TARGET_EVENT_OLD_pre_resume
);
2106 /* with no CMD_ARGV, resume from current pc, addr = 0,
2107 * with one arguments, addr = CMD_ARGV[0],
2108 * handle breakpoints, not debugging */
2112 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], addr
);
2116 return target_resume(target
, current
, addr
, 1, 0);
2119 COMMAND_HANDLER(handle_step_command
)
2122 return ERROR_COMMAND_SYNTAX_ERROR
;
2126 /* with no CMD_ARGV, step from current pc, addr = 0,
2127 * with one argument addr = CMD_ARGV[0],
2128 * handle breakpoints, debugging */
2133 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], addr
);
2137 struct target
*target
= get_current_target(CMD_CTX
);
2139 return target
->type
->step(target
, current_pc
, addr
, 1);
2142 static void handle_md_output(struct command_context
*cmd_ctx
,
2143 struct target
*target
, uint32_t address
, unsigned size
,
2144 unsigned count
, const uint8_t *buffer
)
2146 const unsigned line_bytecnt
= 32;
2147 unsigned line_modulo
= line_bytecnt
/ size
;
2149 char output
[line_bytecnt
* 4 + 1];
2150 unsigned output_len
= 0;
2152 const char *value_fmt
;
2154 case 4: value_fmt
= "%8.8x "; break;
2155 case 2: value_fmt
= "%4.2x "; break;
2156 case 1: value_fmt
= "%2.2x "; break;
2158 /* "can't happen", caller checked */
2159 LOG_ERROR("invalid memory read size: %u", size
);
2163 for (unsigned i
= 0; i
< count
; i
++)
2165 if (i
% line_modulo
== 0)
2167 output_len
+= snprintf(output
+ output_len
,
2168 sizeof(output
) - output_len
,
2170 (unsigned)(address
+ (i
*size
)));
2174 const uint8_t *value_ptr
= buffer
+ i
* size
;
2176 case 4: value
= target_buffer_get_u32(target
, value_ptr
); break;
2177 case 2: value
= target_buffer_get_u16(target
, value_ptr
); break;
2178 case 1: value
= *value_ptr
;
2180 output_len
+= snprintf(output
+ output_len
,
2181 sizeof(output
) - output_len
,
2184 if ((i
% line_modulo
== line_modulo
- 1) || (i
== count
- 1))
2186 command_print(cmd_ctx
, "%s", output
);
2192 COMMAND_HANDLER(handle_md_command
)
2195 return ERROR_COMMAND_SYNTAX_ERROR
;
2198 switch (CMD_NAME
[2]) {
2199 case 'w': size
= 4; break;
2200 case 'h': size
= 2; break;
2201 case 'b': size
= 1; break;
2202 default: return ERROR_COMMAND_SYNTAX_ERROR
;
2205 bool physical
=strcmp(CMD_ARGV
[0], "phys")==0;
2206 int (*fn
)(struct target
*target
,
2207 uint32_t address
, uint32_t size
, uint32_t count
, uint8_t *buffer
);
2212 fn
=target_read_phys_memory
;
2215 fn
=target_read_memory
;
2217 if ((CMD_ARGC
< 1) || (CMD_ARGC
> 2))
2219 return ERROR_COMMAND_SYNTAX_ERROR
;
2223 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], address
);
2227 COMMAND_PARSE_NUMBER(uint
, CMD_ARGV
[1], count
);
2229 uint8_t *buffer
= calloc(count
, size
);
2231 struct target
*target
= get_current_target(CMD_CTX
);
2232 int retval
= fn(target
, address
, size
, count
, buffer
);
2233 if (ERROR_OK
== retval
)
2234 handle_md_output(CMD_CTX
, target
, address
, size
, count
, buffer
);
2241 COMMAND_HANDLER(handle_mw_command
)
2245 return ERROR_COMMAND_SYNTAX_ERROR
;
2247 bool physical
=strcmp(CMD_ARGV
[0], "phys")==0;
2248 int (*fn
)(struct target
*target
,
2249 uint32_t address
, uint32_t size
, uint32_t count
, uint8_t *buffer
);
2254 fn
=target_write_phys_memory
;
2257 fn
=target_write_memory
;
2259 if ((CMD_ARGC
< 2) || (CMD_ARGC
> 3))
2260 return ERROR_COMMAND_SYNTAX_ERROR
;
2263 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], address
);
2266 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], value
);
2270 COMMAND_PARSE_NUMBER(uint
, CMD_ARGV
[2], count
);
2272 struct target
*target
= get_current_target(CMD_CTX
);
2274 uint8_t value_buf
[4];
2275 switch (CMD_NAME
[2])
2279 target_buffer_set_u32(target
, value_buf
, value
);
2283 target_buffer_set_u16(target
, value_buf
, value
);
2287 value_buf
[0] = value
;
2290 return ERROR_COMMAND_SYNTAX_ERROR
;
2292 for (unsigned i
= 0; i
< count
; i
++)
2294 int retval
= fn(target
,
2295 address
+ i
* wordsize
, wordsize
, 1, value_buf
);
2296 if (ERROR_OK
!= retval
)
2305 static COMMAND_HELPER(parse_load_image_command_CMD_ARGV
, struct image
*image
,
2306 uint32_t *min_address
, uint32_t *max_address
)
2308 if (CMD_ARGC
< 1 || CMD_ARGC
> 5)
2309 return ERROR_COMMAND_SYNTAX_ERROR
;
2311 /* a base address isn't always necessary,
2312 * default to 0x0 (i.e. don't relocate) */
2316 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], addr
);
2317 image
->base_address
= addr
;
2318 image
->base_address_set
= 1;
2321 image
->base_address_set
= 0;
2323 image
->start_address_set
= 0;
2327 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[3], *min_address
);
2331 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[4], *max_address
);
2332 // use size (given) to find max (required)
2333 *max_address
+= *min_address
;
2336 if (*min_address
> *max_address
)
2337 return ERROR_COMMAND_SYNTAX_ERROR
;
2342 COMMAND_HANDLER(handle_load_image_command
)
2346 uint32_t image_size
;
2347 uint32_t min_address
= 0;
2348 uint32_t max_address
= 0xffffffff;
2352 int retval
= CALL_COMMAND_HANDLER(parse_load_image_command_CMD_ARGV
,
2353 &image
, &min_address
, &max_address
);
2354 if (ERROR_OK
!= retval
)
2357 struct target
*target
= get_current_target(CMD_CTX
);
2359 struct duration bench
;
2360 duration_start(&bench
);
2362 if (image_open(&image
, CMD_ARGV
[0], (CMD_ARGC
>= 3) ? CMD_ARGV
[2] : NULL
) != ERROR_OK
)
2369 for (i
= 0; i
< image
.num_sections
; i
++)
2371 buffer
= malloc(image
.sections
[i
].size
);
2374 command_print(CMD_CTX
,
2375 "error allocating buffer for section (%d bytes)",
2376 (int)(image
.sections
[i
].size
));
2380 if ((retval
= image_read_section(&image
, i
, 0x0, image
.sections
[i
].size
, buffer
, &buf_cnt
)) != ERROR_OK
)
2386 uint32_t offset
= 0;
2387 uint32_t length
= buf_cnt
;
2389 /* DANGER!!! beware of unsigned comparision here!!! */
2391 if ((image
.sections
[i
].base_address
+ buf_cnt
>= min_address
)&&
2392 (image
.sections
[i
].base_address
< max_address
))
2394 if (image
.sections
[i
].base_address
< min_address
)
2396 /* clip addresses below */
2397 offset
+= min_address
-image
.sections
[i
].base_address
;
2401 if (image
.sections
[i
].base_address
+ buf_cnt
> max_address
)
2403 length
-= (image
.sections
[i
].base_address
+ buf_cnt
)-max_address
;
2406 if ((retval
= target_write_buffer(target
, image
.sections
[i
].base_address
+ offset
, length
, buffer
+ offset
)) != ERROR_OK
)
2411 image_size
+= length
;
2412 command_print(CMD_CTX
, "%u bytes written at address 0x%8.8" PRIx32
"",
2413 (unsigned int)length
,
2414 image
.sections
[i
].base_address
+ offset
);
2420 if ((ERROR_OK
== retval
) && (duration_measure(&bench
) == ERROR_OK
))
2422 command_print(CMD_CTX
, "downloaded %" PRIu32
" bytes "
2423 "in %fs (%0.3f kb/s)", image_size
,
2424 duration_elapsed(&bench
), duration_kbps(&bench
, image_size
));
2427 image_close(&image
);
2433 COMMAND_HANDLER(handle_dump_image_command
)
2435 struct fileio fileio
;
2437 uint8_t buffer
[560];
2441 struct target
*target
= get_current_target(CMD_CTX
);
2445 command_print(CMD_CTX
, "usage: dump_image <filename> <address> <size>");
2450 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], address
);
2452 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[2], size
);
2454 if (fileio_open(&fileio
, CMD_ARGV
[0], FILEIO_WRITE
, FILEIO_BINARY
) != ERROR_OK
)
2459 struct duration bench
;
2460 duration_start(&bench
);
2462 int retval
= ERROR_OK
;
2465 size_t size_written
;
2466 uint32_t this_run_size
= (size
> 560) ? 560 : size
;
2467 retval
= target_read_buffer(target
, address
, this_run_size
, buffer
);
2468 if (retval
!= ERROR_OK
)
2473 retval
= fileio_write(&fileio
, this_run_size
, buffer
, &size_written
);
2474 if (retval
!= ERROR_OK
)
2479 size
-= this_run_size
;
2480 address
+= this_run_size
;
2483 if ((retvaltemp
= fileio_close(&fileio
)) != ERROR_OK
)
2486 if ((ERROR_OK
== retval
) && (duration_measure(&bench
) == ERROR_OK
))
2488 command_print(CMD_CTX
,
2489 "dumped %zu bytes in %fs (%0.3f kb/s)", fileio
.size
,
2490 duration_elapsed(&bench
), duration_kbps(&bench
, fileio
.size
));
2496 static COMMAND_HELPER(handle_verify_image_command_internal
, int verify
)
2500 uint32_t image_size
;
2503 uint32_t checksum
= 0;
2504 uint32_t mem_checksum
= 0;
2508 struct target
*target
= get_current_target(CMD_CTX
);
2512 return ERROR_COMMAND_SYNTAX_ERROR
;
2517 LOG_ERROR("no target selected");
2521 struct duration bench
;
2522 duration_start(&bench
);
2527 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], addr
);
2528 image
.base_address
= addr
;
2529 image
.base_address_set
= 1;
2533 image
.base_address_set
= 0;
2534 image
.base_address
= 0x0;
2537 image
.start_address_set
= 0;
2539 if ((retval
= image_open(&image
, CMD_ARGV
[0], (CMD_ARGC
== 3) ? CMD_ARGV
[2] : NULL
)) != ERROR_OK
)
2546 for (i
= 0; i
< image
.num_sections
; i
++)
2548 buffer
= malloc(image
.sections
[i
].size
);
2551 command_print(CMD_CTX
,
2552 "error allocating buffer for section (%d bytes)",
2553 (int)(image
.sections
[i
].size
));
2556 if ((retval
= image_read_section(&image
, i
, 0x0, image
.sections
[i
].size
, buffer
, &buf_cnt
)) != ERROR_OK
)
2564 /* calculate checksum of image */
2565 image_calculate_checksum(buffer
, buf_cnt
, &checksum
);
2567 retval
= target_checksum_memory(target
, image
.sections
[i
].base_address
, buf_cnt
, &mem_checksum
);
2568 if (retval
!= ERROR_OK
)
2574 if (checksum
!= mem_checksum
)
2576 /* failed crc checksum, fall back to a binary compare */
2579 command_print(CMD_CTX
, "checksum mismatch - attempting binary compare");
2581 data
= (uint8_t*)malloc(buf_cnt
);
2583 /* Can we use 32bit word accesses? */
2585 int count
= buf_cnt
;
2586 if ((count
% 4) == 0)
2591 retval
= target_read_memory(target
, image
.sections
[i
].base_address
, size
, count
, data
);
2592 if (retval
== ERROR_OK
)
2595 for (t
= 0; t
< buf_cnt
; t
++)
2597 if (data
[t
] != buffer
[t
])
2599 command_print(CMD_CTX
,
2600 "Verify operation failed address 0x%08x. Was 0x%02x instead of 0x%02x\n",
2601 (unsigned)(t
+ image
.sections
[i
].base_address
),
2606 retval
= ERROR_FAIL
;
2620 command_print(CMD_CTX
, "address 0x%08" PRIx32
" length 0x%08zx",
2621 image
.sections
[i
].base_address
,
2626 image_size
+= buf_cnt
;
2629 if ((ERROR_OK
== retval
) && (duration_measure(&bench
) == ERROR_OK
))
2631 command_print(CMD_CTX
, "verified %" PRIu32
" bytes "
2632 "in %fs (%0.3f kb/s)", image_size
,
2633 duration_elapsed(&bench
), duration_kbps(&bench
, image_size
));
2636 image_close(&image
);
2641 COMMAND_HANDLER(handle_verify_image_command
)
2643 return CALL_COMMAND_HANDLER(handle_verify_image_command_internal
, 1);
2646 COMMAND_HANDLER(handle_test_image_command
)
2648 return CALL_COMMAND_HANDLER(handle_verify_image_command_internal
, 0);
2651 static int handle_bp_command_list(struct command_context
*cmd_ctx
)
2653 struct target
*target
= get_current_target(cmd_ctx
);
2654 struct breakpoint
*breakpoint
= target
->breakpoints
;
2657 if (breakpoint
->type
== BKPT_SOFT
)
2659 char* buf
= buf_to_str(breakpoint
->orig_instr
,
2660 breakpoint
->length
, 16);
2661 command_print(cmd_ctx
, "0x%8.8" PRIx32
", 0x%x, %i, 0x%s",
2662 breakpoint
->address
,
2664 breakpoint
->set
, buf
);
2669 command_print(cmd_ctx
, "0x%8.8" PRIx32
", 0x%x, %i",
2670 breakpoint
->address
,
2671 breakpoint
->length
, breakpoint
->set
);
2674 breakpoint
= breakpoint
->next
;
2679 static int handle_bp_command_set(struct command_context
*cmd_ctx
,
2680 uint32_t addr
, uint32_t length
, int hw
)
2682 struct target
*target
= get_current_target(cmd_ctx
);
2683 int retval
= breakpoint_add(target
, addr
, length
, hw
);
2684 if (ERROR_OK
== retval
)
2685 command_print(cmd_ctx
, "breakpoint set at 0x%8.8" PRIx32
"", addr
);
2687 LOG_ERROR("Failure setting breakpoint");
2691 COMMAND_HANDLER(handle_bp_command
)
2694 return handle_bp_command_list(CMD_CTX
);
2696 if (CMD_ARGC
< 2 || CMD_ARGC
> 3)
2698 command_print(CMD_CTX
, "usage: bp <address> <length> ['hw']");
2699 return ERROR_COMMAND_SYNTAX_ERROR
;
2703 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], addr
);
2705 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], length
);
2710 if (strcmp(CMD_ARGV
[2], "hw") == 0)
2713 return ERROR_COMMAND_SYNTAX_ERROR
;
2716 return handle_bp_command_set(CMD_CTX
, addr
, length
, hw
);
2719 COMMAND_HANDLER(handle_rbp_command
)
2722 return ERROR_COMMAND_SYNTAX_ERROR
;
2725 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], addr
);
2727 struct target
*target
= get_current_target(CMD_CTX
);
2728 breakpoint_remove(target
, addr
);
2733 COMMAND_HANDLER(handle_wp_command
)
2735 struct target
*target
= get_current_target(CMD_CTX
);
2739 struct watchpoint
*watchpoint
= target
->watchpoints
;
2743 command_print(CMD_CTX
, "address: 0x%8.8" PRIx32
2744 ", len: 0x%8.8" PRIx32
2745 ", r/w/a: %i, value: 0x%8.8" PRIx32
2746 ", mask: 0x%8.8" PRIx32
,
2747 watchpoint
->address
,
2749 (int)watchpoint
->rw
,
2752 watchpoint
= watchpoint
->next
;
2757 enum watchpoint_rw type
= WPT_ACCESS
;
2759 uint32_t length
= 0;
2760 uint32_t data_value
= 0x0;
2761 uint32_t data_mask
= 0xffffffff;
2766 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[4], data_mask
);
2769 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[3], data_value
);
2772 switch (CMD_ARGV
[2][0])
2784 LOG_ERROR("invalid watchpoint mode ('%c')", CMD_ARGV
[2][0]);
2785 return ERROR_COMMAND_SYNTAX_ERROR
;
2789 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], length
);
2790 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], addr
);
2794 command_print(CMD_CTX
, "usage: wp [address length "
2795 "[(r|w|a) [value [mask]]]]");
2796 return ERROR_COMMAND_SYNTAX_ERROR
;
2799 int retval
= watchpoint_add(target
, addr
, length
, type
,
2800 data_value
, data_mask
);
2801 if (ERROR_OK
!= retval
)
2802 LOG_ERROR("Failure setting watchpoints");
2807 COMMAND_HANDLER(handle_rwp_command
)
2810 return ERROR_COMMAND_SYNTAX_ERROR
;
2813 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], addr
);
2815 struct target
*target
= get_current_target(CMD_CTX
);
2816 watchpoint_remove(target
, addr
);
2823 * Translate a virtual address to a physical address.
2825 * The low-level target implementation must have logged a detailed error
2826 * which is forwarded to telnet/GDB session.
2828 COMMAND_HANDLER(handle_virt2phys_command
)
2831 return ERROR_COMMAND_SYNTAX_ERROR
;
2834 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], va
);
2837 struct target
*target
= get_current_target(CMD_CTX
);
2838 int retval
= target
->type
->virt2phys(target
, va
, &pa
);
2839 if (retval
== ERROR_OK
)
2840 command_print(CMD_CTX
, "Physical address 0x%08" PRIx32
"", pa
);
2845 static void writeData(FILE *f
, const void *data
, size_t len
)
2847 size_t written
= fwrite(data
, 1, len
, f
);
2849 LOG_ERROR("failed to write %zu bytes: %s", len
, strerror(errno
));
2852 static void writeLong(FILE *f
, int l
)
2855 for (i
= 0; i
< 4; i
++)
2857 char c
= (l
>> (i
*8))&0xff;
2858 writeData(f
, &c
, 1);
2863 static void writeString(FILE *f
, char *s
)
2865 writeData(f
, s
, strlen(s
));
2868 /* Dump a gmon.out histogram file. */
2869 static void writeGmon(uint32_t *samples
, uint32_t sampleNum
, const char *filename
)
2872 FILE *f
= fopen(filename
, "w");
2875 writeString(f
, "gmon");
2876 writeLong(f
, 0x00000001); /* Version */
2877 writeLong(f
, 0); /* padding */
2878 writeLong(f
, 0); /* padding */
2879 writeLong(f
, 0); /* padding */
2881 uint8_t zero
= 0; /* GMON_TAG_TIME_HIST */
2882 writeData(f
, &zero
, 1);
2884 /* figure out bucket size */
2885 uint32_t min
= samples
[0];
2886 uint32_t max
= samples
[0];
2887 for (i
= 0; i
< sampleNum
; i
++)
2889 if (min
> samples
[i
])
2893 if (max
< samples
[i
])
2899 int addressSpace
= (max
-min
+ 1);
2901 static const uint32_t maxBuckets
= 256 * 1024; /* maximum buckets. */
2902 uint32_t length
= addressSpace
;
2903 if (length
> maxBuckets
)
2905 length
= maxBuckets
;
2907 int *buckets
= malloc(sizeof(int)*length
);
2908 if (buckets
== NULL
)
2913 memset(buckets
, 0, sizeof(int)*length
);
2914 for (i
= 0; i
< sampleNum
;i
++)
2916 uint32_t address
= samples
[i
];
2917 long long a
= address
-min
;
2918 long long b
= length
-1;
2919 long long c
= addressSpace
-1;
2920 int index
= (a
*b
)/c
; /* danger!!!! int32 overflows */
2924 /* append binary memory gmon.out &profile_hist_hdr ((char*)&profile_hist_hdr + sizeof(struct gmon_hist_hdr)) */
2925 writeLong(f
, min
); /* low_pc */
2926 writeLong(f
, max
); /* high_pc */
2927 writeLong(f
, length
); /* # of samples */
2928 writeLong(f
, 64000000); /* 64MHz */
2929 writeString(f
, "seconds");
2930 for (i
= 0; i
< (15-strlen("seconds")); i
++)
2931 writeData(f
, &zero
, 1);
2932 writeString(f
, "s");
2934 /*append binary memory gmon.out profile_hist_data (profile_hist_data + profile_hist_hdr.hist_size) */
2936 char *data
= malloc(2*length
);
2939 for (i
= 0; i
< length
;i
++)
2948 data
[i
*2 + 1]=(val
>> 8)&0xff;
2951 writeData(f
, data
, length
* 2);
2961 /* profiling samples the CPU PC as quickly as OpenOCD is able, which will be used as a random sampling of PC */
2962 COMMAND_HANDLER(handle_profile_command
)
2964 struct target
*target
= get_current_target(CMD_CTX
);
2965 struct timeval timeout
, now
;
2967 gettimeofday(&timeout
, NULL
);
2970 return ERROR_COMMAND_SYNTAX_ERROR
;
2973 COMMAND_PARSE_NUMBER(uint
, CMD_ARGV
[0], offset
);
2975 timeval_add_time(&timeout
, offset
, 0);
2977 command_print(CMD_CTX
, "Starting profiling. Halting and resuming the target as often as we can...");
2979 static const int maxSample
= 10000;
2980 uint32_t *samples
= malloc(sizeof(uint32_t)*maxSample
);
2981 if (samples
== NULL
)
2985 /* hopefully it is safe to cache! We want to stop/restart as quickly as possible. */
2986 struct reg
*reg
= register_get_by_name(target
->reg_cache
, "pc", 1);
2991 target_poll(target
);
2992 if (target
->state
== TARGET_HALTED
)
2994 uint32_t t
=*((uint32_t *)reg
->value
);
2995 samples
[numSamples
++]=t
;
2996 retval
= target_resume(target
, 1, 0, 0, 0); /* current pc, addr = 0, do not handle breakpoints, not debugging */
2997 target_poll(target
);
2998 alive_sleep(10); /* sleep 10ms, i.e. <100 samples/second. */
2999 } else if (target
->state
== TARGET_RUNNING
)
3001 /* We want to quickly sample the PC. */
3002 if ((retval
= target_halt(target
)) != ERROR_OK
)
3009 command_print(CMD_CTX
, "Target not halted or running");
3013 if (retval
!= ERROR_OK
)
3018 gettimeofday(&now
, NULL
);
3019 if ((numSamples
>= maxSample
) || ((now
.tv_sec
>= timeout
.tv_sec
) && (now
.tv_usec
>= timeout
.tv_usec
)))
3021 command_print(CMD_CTX
, "Profiling completed. %d samples.", numSamples
);
3022 if ((retval
= target_poll(target
)) != ERROR_OK
)
3027 if (target
->state
== TARGET_HALTED
)
3029 target_resume(target
, 1, 0, 0, 0); /* current pc, addr = 0, do not handle breakpoints, not debugging */
3031 if ((retval
= target_poll(target
)) != ERROR_OK
)
3036 writeGmon(samples
, numSamples
, CMD_ARGV
[1]);
3037 command_print(CMD_CTX
, "Wrote %s", CMD_ARGV
[1]);
3046 static int new_int_array_element(Jim_Interp
* interp
, const char *varname
, int idx
, uint32_t val
)
3049 Jim_Obj
*nameObjPtr
, *valObjPtr
;
3052 namebuf
= alloc_printf("%s(%d)", varname
, idx
);
3056 nameObjPtr
= Jim_NewStringObj(interp
, namebuf
, -1);
3057 valObjPtr
= Jim_NewIntObj(interp
, val
);
3058 if (!nameObjPtr
|| !valObjPtr
)
3064 Jim_IncrRefCount(nameObjPtr
);
3065 Jim_IncrRefCount(valObjPtr
);
3066 result
= Jim_SetVariable(interp
, nameObjPtr
, valObjPtr
);
3067 Jim_DecrRefCount(interp
, nameObjPtr
);
3068 Jim_DecrRefCount(interp
, valObjPtr
);
3070 /* printf("%s(%d) <= 0%08x\n", varname, idx, val); */
3074 static int jim_mem2array(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
3076 struct command_context
*context
;
3077 struct target
*target
;
3079 context
= Jim_GetAssocData(interp
, "context");
3080 if (context
== NULL
)
3082 LOG_ERROR("mem2array: no command context");
3085 target
= get_current_target(context
);
3088 LOG_ERROR("mem2array: no current target");
3092 return target_mem2array(interp
, target
, argc
-1, argv
+ 1);
3095 static int target_mem2array(Jim_Interp
*interp
, struct target
*target
, int argc
, Jim_Obj
*const *argv
)
3103 const char *varname
;
3107 /* argv[1] = name of array to receive the data
3108 * argv[2] = desired width
3109 * argv[3] = memory address
3110 * argv[4] = count of times to read
3113 Jim_WrongNumArgs(interp
, 1, argv
, "varname width addr nelems");
3116 varname
= Jim_GetString(argv
[0], &len
);
3117 /* given "foo" get space for worse case "foo(%d)" .. add 20 */
3119 e
= Jim_GetLong(interp
, argv
[1], &l
);
3125 e
= Jim_GetLong(interp
, argv
[2], &l
);
3130 e
= Jim_GetLong(interp
, argv
[3], &l
);
3146 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3147 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "Invalid width param, must be 8/16/32", NULL
);
3151 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3152 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "mem2array: zero width read?", NULL
);
3155 if ((addr
+ (len
* width
)) < addr
) {
3156 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3157 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "mem2array: addr + len - wraps to zero?", NULL
);
3160 /* absurd transfer size? */
3162 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3163 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "mem2array: absurd > 64K item request", NULL
);
3168 ((width
== 2) && ((addr
& 1) == 0)) ||
3169 ((width
== 4) && ((addr
& 3) == 0))) {
3173 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3174 sprintf(buf
, "mem2array address: 0x%08" PRIx32
" is not aligned for %" PRId32
" byte reads",
3177 Jim_AppendStrings(interp
, Jim_GetResult(interp
), buf
, NULL
);
3186 size_t buffersize
= 4096;
3187 uint8_t *buffer
= malloc(buffersize
);
3194 /* Slurp... in buffer size chunks */
3196 count
= len
; /* in objects.. */
3197 if (count
> (buffersize
/width
)) {
3198 count
= (buffersize
/width
);
3201 retval
= target_read_memory(target
, addr
, width
, count
, buffer
);
3202 if (retval
!= ERROR_OK
) {
3204 LOG_ERROR("mem2array: Read @ 0x%08x, w=%d, cnt=%d, failed",
3208 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3209 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "mem2array: cannot read memory", NULL
);
3213 v
= 0; /* shut up gcc */
3214 for (i
= 0 ;i
< count
;i
++, n
++) {
3217 v
= target_buffer_get_u32(target
, &buffer
[i
*width
]);
3220 v
= target_buffer_get_u16(target
, &buffer
[i
*width
]);
3223 v
= buffer
[i
] & 0x0ff;
3226 new_int_array_element(interp
, varname
, n
, v
);
3234 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3239 static int get_int_array_element(Jim_Interp
* interp
, const char *varname
, int idx
, uint32_t *val
)
3242 Jim_Obj
*nameObjPtr
, *valObjPtr
;
3246 namebuf
= alloc_printf("%s(%d)", varname
, idx
);
3250 nameObjPtr
= Jim_NewStringObj(interp
, namebuf
, -1);
3257 Jim_IncrRefCount(nameObjPtr
);
3258 valObjPtr
= Jim_GetVariable(interp
, nameObjPtr
, JIM_ERRMSG
);
3259 Jim_DecrRefCount(interp
, nameObjPtr
);
3261 if (valObjPtr
== NULL
)
3264 result
= Jim_GetLong(interp
, valObjPtr
, &l
);
3265 /* printf("%s(%d) => 0%08x\n", varname, idx, val); */
3270 static int jim_array2mem(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
3272 struct command_context
*context
;
3273 struct target
*target
;
3275 context
= Jim_GetAssocData(interp
, "context");
3276 if (context
== NULL
) {
3277 LOG_ERROR("array2mem: no command context");
3280 target
= get_current_target(context
);
3281 if (target
== NULL
) {
3282 LOG_ERROR("array2mem: no current target");
3286 return target_array2mem(interp
,target
, argc
-1, argv
+ 1);
3288 static int target_array2mem(Jim_Interp
*interp
, struct target
*target
, int argc
, Jim_Obj
*const *argv
)
3296 const char *varname
;
3300 /* argv[1] = name of array to get the data
3301 * argv[2] = desired width
3302 * argv[3] = memory address
3303 * argv[4] = count to write
3306 Jim_WrongNumArgs(interp
, 0, argv
, "varname width addr nelems");
3309 varname
= Jim_GetString(argv
[0], &len
);
3310 /* given "foo" get space for worse case "foo(%d)" .. add 20 */
3312 e
= Jim_GetLong(interp
, argv
[1], &l
);
3318 e
= Jim_GetLong(interp
, argv
[2], &l
);
3323 e
= Jim_GetLong(interp
, argv
[3], &l
);
3339 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3340 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "Invalid width param, must be 8/16/32", NULL
);
3344 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3345 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "array2mem: zero width read?", NULL
);
3348 if ((addr
+ (len
* width
)) < addr
) {
3349 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3350 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "array2mem: addr + len - wraps to zero?", NULL
);
3353 /* absurd transfer size? */
3355 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3356 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "array2mem: absurd > 64K item request", NULL
);
3361 ((width
== 2) && ((addr
& 1) == 0)) ||
3362 ((width
== 4) && ((addr
& 3) == 0))) {
3366 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3367 sprintf(buf
, "array2mem address: 0x%08x is not aligned for %d byte reads",
3370 Jim_AppendStrings(interp
, Jim_GetResult(interp
), buf
, NULL
);
3381 size_t buffersize
= 4096;
3382 uint8_t *buffer
= malloc(buffersize
);
3387 /* Slurp... in buffer size chunks */
3389 count
= len
; /* in objects.. */
3390 if (count
> (buffersize
/width
)) {
3391 count
= (buffersize
/width
);
3394 v
= 0; /* shut up gcc */
3395 for (i
= 0 ;i
< count
;i
++, n
++) {
3396 get_int_array_element(interp
, varname
, n
, &v
);
3399 target_buffer_set_u32(target
, &buffer
[i
*width
], v
);
3402 target_buffer_set_u16(target
, &buffer
[i
*width
], v
);
3405 buffer
[i
] = v
& 0x0ff;
3411 retval
= target_write_memory(target
, addr
, width
, count
, buffer
);
3412 if (retval
!= ERROR_OK
) {
3414 LOG_ERROR("array2mem: Write @ 0x%08x, w=%d, cnt=%d, failed",
3418 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3419 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "array2mem: cannot read memory", NULL
);
3427 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3432 void target_all_handle_event(enum target_event e
)
3434 struct target
*target
;
3436 LOG_DEBUG("**all*targets: event: %d, %s",
3438 Jim_Nvp_value2name_simple(nvp_target_event
, e
)->name
);
3440 target
= all_targets
;
3442 target_handle_event(target
, e
);
3443 target
= target
->next
;
3448 /* FIX? should we propagate errors here rather than printing them
3451 void target_handle_event(struct target
*target
, enum target_event e
)
3453 struct target_event_action
*teap
;
3455 for (teap
= target
->event_action
; teap
!= NULL
; teap
= teap
->next
) {
3456 if (teap
->event
== e
) {
3457 LOG_DEBUG("target: (%d) %s (%s) event: %d (%s) action: %s",
3458 target
->target_number
,
3459 target_name(target
),
3460 target_type_name(target
),
3462 Jim_Nvp_value2name_simple(nvp_target_event
, e
)->name
,
3463 Jim_GetString(teap
->body
, NULL
));
3464 if (Jim_EvalObj(teap
->interp
, teap
->body
) != JIM_OK
)
3466 Jim_PrintErrorMessage(teap
->interp
);
3473 * Returns true only if the target has a handler for the specified event.
3475 bool target_has_event_action(struct target
*target
, enum target_event event
)
3477 struct target_event_action
*teap
;
3479 for (teap
= target
->event_action
; teap
!= NULL
; teap
= teap
->next
) {
3480 if (teap
->event
== event
)
3486 enum target_cfg_param
{
3489 TCFG_WORK_AREA_VIRT
,
3490 TCFG_WORK_AREA_PHYS
,
3491 TCFG_WORK_AREA_SIZE
,
3492 TCFG_WORK_AREA_BACKUP
,
3495 TCFG_CHAIN_POSITION
,
3498 static Jim_Nvp nvp_config_opts
[] = {
3499 { .name
= "-type", .value
= TCFG_TYPE
},
3500 { .name
= "-event", .value
= TCFG_EVENT
},
3501 { .name
= "-work-area-virt", .value
= TCFG_WORK_AREA_VIRT
},
3502 { .name
= "-work-area-phys", .value
= TCFG_WORK_AREA_PHYS
},
3503 { .name
= "-work-area-size", .value
= TCFG_WORK_AREA_SIZE
},
3504 { .name
= "-work-area-backup", .value
= TCFG_WORK_AREA_BACKUP
},
3505 { .name
= "-endian" , .value
= TCFG_ENDIAN
},
3506 { .name
= "-variant", .value
= TCFG_VARIANT
},
3507 { .name
= "-chain-position", .value
= TCFG_CHAIN_POSITION
},
3509 { .name
= NULL
, .value
= -1 }
3512 static int target_configure(Jim_GetOptInfo
*goi
, struct target
*target
)
3520 /* parse config or cget options ... */
3521 while (goi
->argc
> 0) {
3522 Jim_SetEmptyResult(goi
->interp
);
3523 /* Jim_GetOpt_Debug(goi); */
3525 if (target
->type
->target_jim_configure
) {
3526 /* target defines a configure function */
3527 /* target gets first dibs on parameters */
3528 e
= (*(target
->type
->target_jim_configure
))(target
, goi
);
3537 /* otherwise we 'continue' below */
3539 e
= Jim_GetOpt_Nvp(goi
, nvp_config_opts
, &n
);
3541 Jim_GetOpt_NvpUnknown(goi
, nvp_config_opts
, 0);
3547 if (goi
->isconfigure
) {
3548 Jim_SetResult_sprintf(goi
->interp
,
3549 "not settable: %s", n
->name
);
3553 if (goi
->argc
!= 0) {
3554 Jim_WrongNumArgs(goi
->interp
,
3555 goi
->argc
, goi
->argv
,
3560 Jim_SetResultString(goi
->interp
,
3561 target_type_name(target
), -1);
3565 if (goi
->argc
== 0) {
3566 Jim_WrongNumArgs(goi
->interp
, goi
->argc
, goi
->argv
, "-event ?event-name? ...");
3570 e
= Jim_GetOpt_Nvp(goi
, nvp_target_event
, &n
);
3572 Jim_GetOpt_NvpUnknown(goi
, nvp_target_event
, 1);
3576 if (goi
->isconfigure
) {
3577 if (goi
->argc
!= 1) {
3578 Jim_WrongNumArgs(goi
->interp
, goi
->argc
, goi
->argv
, "-event ?event-name? ?EVENT-BODY?");
3582 if (goi
->argc
!= 0) {
3583 Jim_WrongNumArgs(goi
->interp
, goi
->argc
, goi
->argv
, "-event ?event-name?");
3589 struct target_event_action
*teap
;
3591 teap
= target
->event_action
;
3592 /* replace existing? */
3594 if (teap
->event
== (enum target_event
)n
->value
) {
3600 if (goi
->isconfigure
) {
3601 bool replace
= true;
3604 teap
= calloc(1, sizeof(*teap
));
3607 teap
->event
= n
->value
;
3608 teap
->interp
= goi
->interp
;
3609 Jim_GetOpt_Obj(goi
, &o
);
3611 Jim_DecrRefCount(teap
->interp
, teap
->body
);
3613 teap
->body
= Jim_DuplicateObj(goi
->interp
, o
);
3616 * Tcl/TK - "tk events" have a nice feature.
3617 * See the "BIND" command.
3618 * We should support that here.
3619 * You can specify %X and %Y in the event code.
3620 * The idea is: %T - target name.
3621 * The idea is: %N - target number
3622 * The idea is: %E - event name.
3624 Jim_IncrRefCount(teap
->body
);
3628 /* add to head of event list */
3629 teap
->next
= target
->event_action
;
3630 target
->event_action
= teap
;
3632 Jim_SetEmptyResult(goi
->interp
);
3636 Jim_SetEmptyResult(goi
->interp
);
3638 Jim_SetResult(goi
->interp
, Jim_DuplicateObj(goi
->interp
, teap
->body
));
3645 case TCFG_WORK_AREA_VIRT
:
3646 if (goi
->isconfigure
) {
3647 target_free_all_working_areas(target
);
3648 e
= Jim_GetOpt_Wide(goi
, &w
);
3652 target
->working_area_virt
= w
;
3653 target
->working_area_virt_spec
= true;
3655 if (goi
->argc
!= 0) {
3659 Jim_SetResult(goi
->interp
, Jim_NewIntObj(goi
->interp
, target
->working_area_virt
));
3663 case TCFG_WORK_AREA_PHYS
:
3664 if (goi
->isconfigure
) {
3665 target_free_all_working_areas(target
);
3666 e
= Jim_GetOpt_Wide(goi
, &w
);
3670 target
->working_area_phys
= w
;
3671 target
->working_area_phys_spec
= true;
3673 if (goi
->argc
!= 0) {
3677 Jim_SetResult(goi
->interp
, Jim_NewIntObj(goi
->interp
, target
->working_area_phys
));
3681 case TCFG_WORK_AREA_SIZE
:
3682 if (goi
->isconfigure
) {
3683 target_free_all_working_areas(target
);
3684 e
= Jim_GetOpt_Wide(goi
, &w
);
3688 target
->working_area_size
= w
;
3690 if (goi
->argc
!= 0) {
3694 Jim_SetResult(goi
->interp
, Jim_NewIntObj(goi
->interp
, target
->working_area_size
));
3698 case TCFG_WORK_AREA_BACKUP
:
3699 if (goi
->isconfigure
) {
3700 target_free_all_working_areas(target
);
3701 e
= Jim_GetOpt_Wide(goi
, &w
);
3705 /* make this exactly 1 or 0 */
3706 target
->backup_working_area
= (!!w
);
3708 if (goi
->argc
!= 0) {
3712 Jim_SetResult(goi
->interp
, Jim_NewIntObj(goi
->interp
, target
->backup_working_area
));
3713 /* loop for more e*/
3717 if (goi
->isconfigure
) {
3718 e
= Jim_GetOpt_Nvp(goi
, nvp_target_endian
, &n
);
3720 Jim_GetOpt_NvpUnknown(goi
, nvp_target_endian
, 1);
3723 target
->endianness
= n
->value
;
3725 if (goi
->argc
!= 0) {
3729 n
= Jim_Nvp_value2name_simple(nvp_target_endian
, target
->endianness
);
3730 if (n
->name
== NULL
) {
3731 target
->endianness
= TARGET_LITTLE_ENDIAN
;
3732 n
= Jim_Nvp_value2name_simple(nvp_target_endian
, target
->endianness
);
3734 Jim_SetResultString(goi
->interp
, n
->name
, -1);
3739 if (goi
->isconfigure
) {
3740 if (goi
->argc
< 1) {
3741 Jim_SetResult_sprintf(goi
->interp
,
3746 if (target
->variant
) {
3747 free((void *)(target
->variant
));
3749 e
= Jim_GetOpt_String(goi
, &cp
, NULL
);
3750 target
->variant
= strdup(cp
);
3752 if (goi
->argc
!= 0) {
3756 Jim_SetResultString(goi
->interp
, target
->variant
,-1);
3759 case TCFG_CHAIN_POSITION
:
3760 if (goi
->isconfigure
) {
3762 struct jtag_tap
*tap
;
3763 target_free_all_working_areas(target
);
3764 e
= Jim_GetOpt_Obj(goi
, &o
);
3768 tap
= jtag_tap_by_jim_obj(goi
->interp
, o
);
3772 /* make this exactly 1 or 0 */
3775 if (goi
->argc
!= 0) {
3779 Jim_SetResultString(goi
->interp
, target
->tap
->dotted_name
, -1);
3780 /* loop for more e*/
3783 } /* while (goi->argc) */
3786 /* done - we return */
3790 static int jim_target_configure(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
3793 Jim_GetOpt_Setup(&goi
, interp
, argc
- 1, argv
+ 1);
3794 goi
.isconfigure
= strcmp(Jim_GetString(argv
[0], NULL
), "configure") == 0;
3795 int need_args
= 1 + goi
.isconfigure
;
3796 if (goi
.argc
< need_args
)
3798 Jim_WrongNumArgs(goi
.interp
, goi
.argc
, goi
.argv
,
3800 ? "missing: -option VALUE ..."
3801 : "missing: -option ...");
3804 struct target
*target
= Jim_CmdPrivData(goi
.interp
);
3805 return target_configure(&goi
, target
);
3808 static int jim_target_mw(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
3810 const char *cmd_name
= Jim_GetString(argv
[0], NULL
);
3813 Jim_GetOpt_Setup(&goi
, interp
, argc
- 1, argv
+ 1);
3815 if (goi
.argc
!= 2 && goi
.argc
!= 3)
3817 Jim_SetResult_sprintf(goi
.interp
,
3818 "usage: %s <address> <data> [<count>]", cmd_name
);
3823 int e
= Jim_GetOpt_Wide(&goi
, &a
);
3828 e
= Jim_GetOpt_Wide(&goi
, &b
);
3835 e
= Jim_GetOpt_Wide(&goi
, &c
);
3840 struct target
*target
= Jim_CmdPrivData(goi
.interp
);
3841 uint8_t target_buf
[32];
3842 if (strcasecmp(cmd_name
, "mww") == 0) {
3843 target_buffer_set_u32(target
, target_buf
, b
);
3846 else if (strcasecmp(cmd_name
, "mwh") == 0) {
3847 target_buffer_set_u16(target
, target_buf
, b
);
3850 else if (strcasecmp(cmd_name
, "mwb") == 0) {
3851 target_buffer_set_u8(target
, target_buf
, b
);
3854 LOG_ERROR("command '%s' unknown: ", cmd_name
);
3858 for (jim_wide x
= 0; x
< c
; x
++)
3860 e
= target_write_memory(target
, a
, b
, 1, target_buf
);
3863 Jim_SetResult_sprintf(interp
,
3864 "Error writing @ 0x%08x: %d\n", (int)(a
), e
);
3873 static int jim_target_md(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
3875 const char *cmd_name
= Jim_GetString(argv
[0], NULL
);
3878 Jim_GetOpt_Setup(&goi
, interp
, argc
- 1, argv
+ 1);
3880 if ((goi
.argc
== 2) || (goi
.argc
== 3))
3882 Jim_SetResult_sprintf(goi
.interp
,
3883 "usage: %s <address> [<count>]", cmd_name
);
3888 int e
= Jim_GetOpt_Wide(&goi
, &a
);
3894 e
= Jim_GetOpt_Wide(&goi
, &c
);
3901 jim_wide b
= 1; /* shut up gcc */
3902 if (strcasecmp(cmd_name
, "mdw") == 0)
3904 else if (strcasecmp(cmd_name
, "mdh") == 0)
3906 else if (strcasecmp(cmd_name
, "mdb") == 0)
3909 LOG_ERROR("command '%s' unknown: ", cmd_name
);
3913 /* convert count to "bytes" */
3916 struct target
*target
= Jim_CmdPrivData(goi
.interp
);
3917 uint8_t target_buf
[32];
3924 e
= target_read_memory(target
, a
, b
, y
/ b
, target_buf
);
3925 if (e
!= ERROR_OK
) {
3926 Jim_SetResult_sprintf(interp
, "error reading target @ 0x%08lx", (int)(a
));
3930 Jim_fprintf(interp
, interp
->cookie_stdout
, "0x%08x ", (int)(a
));
3933 for (x
= 0; x
< 16 && x
< y
; x
+= 4)
3935 z
= target_buffer_get_u32(target
, &(target_buf
[ x
* 4 ]));
3936 Jim_fprintf(interp
, interp
->cookie_stdout
, "%08x ", (int)(z
));
3938 for (; (x
< 16) ; x
+= 4) {
3939 Jim_fprintf(interp
, interp
->cookie_stdout
, " ");
3943 for (x
= 0; x
< 16 && x
< y
; x
+= 2)
3945 z
= target_buffer_get_u16(target
, &(target_buf
[ x
* 2 ]));
3946 Jim_fprintf(interp
, interp
->cookie_stdout
, "%04x ", (int)(z
));
3948 for (; (x
< 16) ; x
+= 2) {
3949 Jim_fprintf(interp
, interp
->cookie_stdout
, " ");
3954 for (x
= 0 ; (x
< 16) && (x
< y
) ; x
+= 1) {
3955 z
= target_buffer_get_u8(target
, &(target_buf
[ x
* 4 ]));
3956 Jim_fprintf(interp
, interp
->cookie_stdout
, "%02x ", (int)(z
));
3958 for (; (x
< 16) ; x
+= 1) {
3959 Jim_fprintf(interp
, interp
->cookie_stdout
, " ");
3963 /* ascii-ify the bytes */
3964 for (x
= 0 ; x
< y
; x
++) {
3965 if ((target_buf
[x
] >= 0x20) &&
3966 (target_buf
[x
] <= 0x7e)) {
3970 target_buf
[x
] = '.';
3975 target_buf
[x
] = ' ';
3980 /* print - with a newline */
3981 Jim_fprintf(interp
, interp
->cookie_stdout
, "%s\n", target_buf
);
3989 static int jim_target_mem2array(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
3991 struct target
*target
= Jim_CmdPrivData(interp
);
3992 return target_mem2array(interp
, target
, argc
- 1, argv
+ 1);
3995 static int jim_target_array2mem(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
3997 struct target
*target
= Jim_CmdPrivData(interp
);
3998 return target_array2mem(interp
, target
, argc
- 1, argv
+ 1);
4001 static int jim_target_tap_disabled(Jim_Interp
*interp
)
4003 Jim_SetResult_sprintf(interp
, "[TAP is disabled]");
4007 static int jim_target_examine(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4011 Jim_WrongNumArgs(interp
, 1, argv
, "[no parameters]");
4014 struct target
*target
= Jim_CmdPrivData(interp
);
4015 if (!target
->tap
->enabled
)
4016 return jim_target_tap_disabled(interp
);
4018 int e
= target
->type
->examine(target
);
4021 Jim_SetResult_sprintf(interp
, "examine-fails: %d", e
);
4027 static int jim_target_poll(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4031 Jim_WrongNumArgs(interp
, 1, argv
, "[no parameters]");
4034 struct target
*target
= Jim_CmdPrivData(interp
);
4035 if (!target
->tap
->enabled
)
4036 return jim_target_tap_disabled(interp
);
4039 if (!(target_was_examined(target
))) {
4040 e
= ERROR_TARGET_NOT_EXAMINED
;
4042 e
= target
->type
->poll(target
);
4046 Jim_SetResult_sprintf(interp
, "poll-fails: %d", e
);
4052 static int jim_target_reset(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4055 Jim_GetOpt_Setup(&goi
, interp
, argc
- 1, argv
+ 1);
4059 Jim_WrongNumArgs(interp
, 0, argv
,
4060 "([tT]|[fF]|assert|deassert) BOOL");
4065 int e
= Jim_GetOpt_Nvp(&goi
, nvp_assert
, &n
);
4068 Jim_GetOpt_NvpUnknown(&goi
, nvp_assert
, 1);
4071 /* the halt or not param */
4073 e
= Jim_GetOpt_Wide(&goi
, &a
);
4077 struct target
*target
= Jim_CmdPrivData(goi
.interp
);
4078 if (!target
->tap
->enabled
)
4079 return jim_target_tap_disabled(interp
);
4080 if (!target
->type
->assert_reset
|| !target
->type
->deassert_reset
)
4082 Jim_SetResult_sprintf(interp
,
4083 "No target-specific reset for %s",
4084 target_name(target
));
4087 /* determine if we should halt or not. */
4088 target
->reset_halt
= !!a
;
4089 /* When this happens - all workareas are invalid. */
4090 target_free_all_working_areas_restore(target
, 0);
4093 if (n
->value
== NVP_ASSERT
) {
4094 e
= target
->type
->assert_reset(target
);
4096 e
= target
->type
->deassert_reset(target
);
4098 return (e
== ERROR_OK
) ? JIM_OK
: JIM_ERR
;
4101 static int jim_target_halt(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4104 Jim_WrongNumArgs(interp
, 1, argv
, "[no parameters]");
4107 struct target
*target
= Jim_CmdPrivData(interp
);
4108 if (!target
->tap
->enabled
)
4109 return jim_target_tap_disabled(interp
);
4110 int e
= target
->type
->halt(target
);
4111 return (e
== ERROR_OK
) ? JIM_OK
: JIM_ERR
;
4114 static int jim_target_wait_state(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4117 Jim_GetOpt_Setup(&goi
, interp
, argc
- 1, argv
+ 1);
4119 /* params: <name> statename timeoutmsecs */
4122 const char *cmd_name
= Jim_GetString(argv
[0], NULL
);
4123 Jim_SetResult_sprintf(goi
.interp
,
4124 "%s <state_name> <timeout_in_msec>", cmd_name
);
4129 int e
= Jim_GetOpt_Nvp(&goi
, nvp_target_state
, &n
);
4131 Jim_GetOpt_NvpUnknown(&goi
, nvp_target_state
,1);
4135 e
= Jim_GetOpt_Wide(&goi
, &a
);
4139 struct target
*target
= Jim_CmdPrivData(interp
);
4140 if (!target
->tap
->enabled
)
4141 return jim_target_tap_disabled(interp
);
4143 e
= target_wait_state(target
, n
->value
, a
);
4146 Jim_SetResult_sprintf(goi
.interp
,
4147 "target: %s wait %s fails (%d) %s",
4148 target_name(target
), n
->name
,
4149 e
, target_strerror_safe(e
));
4154 /* List for human, Events defined for this target.
4155 * scripts/programs should use 'name cget -event NAME'
4157 static int jim_target_event_list(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4159 struct command_context
*cmd_ctx
= Jim_GetAssocData(interp
, "context");
4160 struct target
*target
= Jim_CmdPrivData(interp
);
4161 struct target_event_action
*teap
= target
->event_action
;
4162 command_print(cmd_ctx
, "Event actions for target (%d) %s\n",
4163 target
->target_number
,
4164 target_name(target
));
4165 command_print(cmd_ctx
, "%-25s | Body", "Event");
4166 command_print(cmd_ctx
, "------------------------- | "
4167 "----------------------------------------");
4170 Jim_Nvp
*opt
= Jim_Nvp_value2name_simple(nvp_target_event
, teap
->event
);
4171 command_print(cmd_ctx
, "%-25s | %s",
4172 opt
->name
, Jim_GetString(teap
->body
, NULL
));
4175 command_print(cmd_ctx
, "***END***");
4178 static int jim_target_current_state(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4182 Jim_WrongNumArgs(interp
, 1, argv
, "[no parameters]");
4185 struct target
*target
= Jim_CmdPrivData(interp
);
4186 Jim_SetResultString(interp
, target_state_name(target
), -1);
4189 static int jim_target_invoke_event(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4192 Jim_GetOpt_Setup(&goi
, interp
, argc
- 1, argv
+ 1);
4195 const char *cmd_name
= Jim_GetString(argv
[0], NULL
);
4196 Jim_SetResult_sprintf(goi
.interp
, "%s <eventname>", cmd_name
);
4200 int e
= Jim_GetOpt_Nvp(&goi
, nvp_target_event
, &n
);
4203 Jim_GetOpt_NvpUnknown(&goi
, nvp_target_event
, 1);
4206 struct target
*target
= Jim_CmdPrivData(interp
);
4207 target_handle_event(target
, n
->value
);
4211 static const struct command_registration target_instance_command_handlers
[] = {
4213 .name
= "configure",
4214 .mode
= COMMAND_CONFIG
,
4215 .jim_handler
= &jim_target_configure
,
4216 .usage
= "[<target_options> ...]",
4217 .help
= "configure a new target for use",
4221 .mode
= COMMAND_ANY
,
4222 .jim_handler
= &jim_target_configure
,
4223 .usage
= "<target_type> [<target_options> ...]",
4224 .help
= "configure a new target for use",
4228 .mode
= COMMAND_EXEC
,
4229 .jim_handler
= &jim_target_mw
,
4230 .usage
= "<address> <data> [<count>]",
4231 .help
= "Write 32-bit word(s) to target memory",
4235 .mode
= COMMAND_EXEC
,
4236 .jim_handler
= &jim_target_mw
,
4237 .usage
= "<address> <data> [<count>]",
4238 .help
= "Write 16-bit half-word(s) to target memory",
4242 .mode
= COMMAND_EXEC
,
4243 .jim_handler
= &jim_target_mw
,
4244 .usage
= "<address> <data> [<count>]",
4245 .help
= "Write byte(s) to target memory",
4249 .mode
= COMMAND_EXEC
,
4250 .jim_handler
= &jim_target_md
,
4251 .usage
= "<address> [<count>]",
4252 .help
= "Display target memory as 32-bit words",
4256 .mode
= COMMAND_EXEC
,
4257 .jim_handler
= &jim_target_md
,
4258 .usage
= "<address> [<count>]",
4259 .help
= "Display target memory as 16-bit half-words",
4263 .mode
= COMMAND_EXEC
,
4264 .jim_handler
= &jim_target_md
,
4265 .usage
= "<address> [<count>]",
4266 .help
= "Display target memory as 8-bit bytes",
4269 .name
= "array2mem",
4270 .mode
= COMMAND_EXEC
,
4271 .jim_handler
= &jim_target_array2mem
,
4274 .name
= "mem2array",
4275 .mode
= COMMAND_EXEC
,
4276 .jim_handler
= &jim_target_mem2array
,
4279 .name
= "eventlist",
4280 .mode
= COMMAND_EXEC
,
4281 .jim_handler
= &jim_target_event_list
,
4285 .mode
= COMMAND_EXEC
,
4286 .jim_handler
= &jim_target_current_state
,
4289 .name
= "arp_examine",
4290 .mode
= COMMAND_EXEC
,
4291 .jim_handler
= &jim_target_examine
,
4295 .mode
= COMMAND_EXEC
,
4296 .jim_handler
= &jim_target_poll
,
4299 .name
= "arp_reset",
4300 .mode
= COMMAND_EXEC
,
4301 .jim_handler
= &jim_target_reset
,
4305 .mode
= COMMAND_EXEC
,
4306 .jim_handler
= &jim_target_halt
,
4309 .name
= "arp_waitstate",
4310 .mode
= COMMAND_EXEC
,
4311 .jim_handler
= &jim_target_wait_state
,
4314 .name
= "invoke-event",
4315 .mode
= COMMAND_EXEC
,
4316 .jim_handler
= &jim_target_invoke_event
,
4318 COMMAND_REGISTRATION_DONE
4321 static int target_create(Jim_GetOptInfo
*goi
)
4329 struct target
*target
;
4330 struct command_context
*cmd_ctx
;
4332 cmd_ctx
= Jim_GetAssocData(goi
->interp
, "context");
4333 if (goi
->argc
< 3) {
4334 Jim_WrongNumArgs(goi
->interp
, 1, goi
->argv
, "?name? ?type? ..options...");
4339 Jim_GetOpt_Obj(goi
, &new_cmd
);
4340 /* does this command exist? */
4341 cmd
= Jim_GetCommand(goi
->interp
, new_cmd
, JIM_ERRMSG
);
4343 cp
= Jim_GetString(new_cmd
, NULL
);
4344 Jim_SetResult_sprintf(goi
->interp
, "Command/target: %s Exists", cp
);
4349 e
= Jim_GetOpt_String(goi
, &cp2
, NULL
);
4351 /* now does target type exist */
4352 for (x
= 0 ; target_types
[x
] ; x
++) {
4353 if (0 == strcmp(cp
, target_types
[x
]->name
)) {
4358 if (target_types
[x
] == NULL
) {
4359 Jim_SetResult_sprintf(goi
->interp
, "Unknown target type %s, try one of ", cp
);
4360 for (x
= 0 ; target_types
[x
] ; x
++) {
4361 if (target_types
[x
+ 1]) {
4362 Jim_AppendStrings(goi
->interp
,
4363 Jim_GetResult(goi
->interp
),
4364 target_types
[x
]->name
,
4367 Jim_AppendStrings(goi
->interp
,
4368 Jim_GetResult(goi
->interp
),
4370 target_types
[x
]->name
,NULL
);
4377 target
= calloc(1,sizeof(struct target
));
4378 /* set target number */
4379 target
->target_number
= new_target_number();
4381 /* allocate memory for each unique target type */
4382 target
->type
= (struct target_type
*)calloc(1,sizeof(struct target_type
));
4384 memcpy(target
->type
, target_types
[x
], sizeof(struct target_type
));
4386 /* will be set by "-endian" */
4387 target
->endianness
= TARGET_ENDIAN_UNKNOWN
;
4389 target
->working_area
= 0x0;
4390 target
->working_area_size
= 0x0;
4391 target
->working_areas
= NULL
;
4392 target
->backup_working_area
= 0;
4394 target
->state
= TARGET_UNKNOWN
;
4395 target
->debug_reason
= DBG_REASON_UNDEFINED
;
4396 target
->reg_cache
= NULL
;
4397 target
->breakpoints
= NULL
;
4398 target
->watchpoints
= NULL
;
4399 target
->next
= NULL
;
4400 target
->arch_info
= NULL
;
4402 target
->display
= 1;
4404 target
->halt_issued
= false;
4406 /* initialize trace information */
4407 target
->trace_info
= malloc(sizeof(struct trace
));
4408 target
->trace_info
->num_trace_points
= 0;
4409 target
->trace_info
->trace_points_size
= 0;
4410 target
->trace_info
->trace_points
= NULL
;
4411 target
->trace_info
->trace_history_size
= 0;
4412 target
->trace_info
->trace_history
= NULL
;
4413 target
->trace_info
->trace_history_pos
= 0;
4414 target
->trace_info
->trace_history_overflowed
= 0;
4416 target
->dbgmsg
= NULL
;
4417 target
->dbg_msg_enabled
= 0;
4419 target
->endianness
= TARGET_ENDIAN_UNKNOWN
;
4421 /* Do the rest as "configure" options */
4422 goi
->isconfigure
= 1;
4423 e
= target_configure(goi
, target
);
4425 if (target
->tap
== NULL
)
4427 Jim_SetResultString(goi
->interp
, "-chain-position required when creating target", -1);
4437 if (target
->endianness
== TARGET_ENDIAN_UNKNOWN
) {
4438 /* default endian to little if not specified */
4439 target
->endianness
= TARGET_LITTLE_ENDIAN
;
4442 /* incase variant is not set */
4443 if (!target
->variant
)
4444 target
->variant
= strdup("");
4446 cp
= Jim_GetString(new_cmd
, NULL
);
4447 target
->cmd_name
= strdup(cp
);
4449 /* create the target specific commands */
4450 if (target
->type
->commands
) {
4451 e
= register_commands(cmd_ctx
, NULL
, target
->type
->commands
);
4453 LOG_ERROR("unable to register '%s' commands", cp
);
4455 if (target
->type
->target_create
) {
4456 (*(target
->type
->target_create
))(target
, goi
->interp
);
4459 /* append to end of list */
4461 struct target
**tpp
;
4462 tpp
= &(all_targets
);
4464 tpp
= &((*tpp
)->next
);
4469 /* now - create the new target name command */
4470 const const struct command_registration target_subcommands
[] = {
4472 .chain
= target_instance_command_handlers
,
4475 .chain
= target
->type
->commands
,
4477 COMMAND_REGISTRATION_DONE
4479 const const struct command_registration target_commands
[] = {
4482 .mode
= COMMAND_ANY
,
4483 .help
= "target command group",
4484 .chain
= target_subcommands
,
4486 COMMAND_REGISTRATION_DONE
4488 e
= register_commands(cmd_ctx
, NULL
, target_commands
);
4492 struct command
*c
= command_find_in_context(cmd_ctx
, cp
);
4494 command_set_handler_data(c
, target
);
4496 return (ERROR_OK
== e
) ? JIM_OK
: JIM_ERR
;
4499 static int jim_target_current(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4503 Jim_WrongNumArgs(interp
, 1, argv
, "Too many parameters");
4506 struct command_context
*cmd_ctx
= Jim_GetAssocData(interp
, "context");
4507 Jim_SetResultString(interp
, get_current_target(cmd_ctx
)->cmd_name
, -1);
4511 static int jim_target_types(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4515 Jim_WrongNumArgs(interp
, 1, argv
, "Too many parameters");
4518 Jim_SetResult(interp
, Jim_NewListObj(interp
, NULL
, 0));
4519 for (unsigned x
= 0; NULL
!= target_types
[x
]; x
++)
4521 Jim_ListAppendElement(interp
, Jim_GetResult(interp
),
4522 Jim_NewStringObj(interp
, target_types
[x
]->name
, -1));
4527 static int jim_target_names(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4531 Jim_WrongNumArgs(interp
, 1, argv
, "Too many parameters");
4534 Jim_SetResult(interp
, Jim_NewListObj(interp
, NULL
, 0));
4535 struct target
*target
= all_targets
;
4538 Jim_ListAppendElement(interp
, Jim_GetResult(interp
),
4539 Jim_NewStringObj(interp
, target_name(target
), -1));
4540 target
= target
->next
;
4545 static int jim_target_create(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4548 Jim_GetOpt_Setup(&goi
, interp
, argc
- 1, argv
+ 1);
4551 Jim_WrongNumArgs(goi
.interp
, goi
.argc
, goi
.argv
,
4552 "<name> <target_type> [<target_options> ...]");
4555 return target_create(&goi
);
4558 static int jim_target_number(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4561 Jim_GetOpt_Setup(&goi
, interp
, argc
- 1, argv
+ 1);
4563 /* It's OK to remove this mechanism sometime after August 2010 or so */
4564 LOG_WARNING("don't use numbers as target identifiers; use names");
4567 Jim_SetResult_sprintf(goi
.interp
, "usage: target number <number>");
4571 int e
= Jim_GetOpt_Wide(&goi
, &w
);
4575 struct target
*target
;
4576 for (target
= all_targets
; NULL
!= target
; target
= target
->next
)
4578 if (target
->target_number
!= w
)
4581 Jim_SetResultString(goi
.interp
, target_name(target
), -1);
4584 Jim_SetResult_sprintf(goi
.interp
,
4585 "Target: number %d does not exist", (int)(w
));
4589 static int jim_target_count(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4593 Jim_WrongNumArgs(interp
, 1, argv
, "<no parameters>");
4597 struct target
*target
= all_targets
;
4598 while (NULL
!= target
)
4600 target
= target
->next
;
4603 Jim_SetResult(interp
, Jim_NewIntObj(interp
, count
));
4607 static const struct command_registration target_subcommand_handlers
[] = {
4610 .mode
= COMMAND_CONFIG
,
4611 .handler
= &handle_target_init_command
,
4612 .help
= "initialize targets",
4616 .mode
= COMMAND_ANY
,
4617 .jim_handler
= &jim_target_create
,
4618 .usage
= "<name> <type> ...",
4619 .help
= "Returns the currently selected target",
4623 .mode
= COMMAND_ANY
,
4624 .jim_handler
= &jim_target_current
,
4625 .help
= "Returns the currently selected target",
4629 .mode
= COMMAND_ANY
,
4630 .jim_handler
= &jim_target_types
,
4631 .help
= "Returns the available target types as a list of strings",
4635 .mode
= COMMAND_ANY
,
4636 .jim_handler
= &jim_target_names
,
4637 .help
= "Returns the names of all targets as a list of strings",
4641 .mode
= COMMAND_ANY
,
4642 .jim_handler
= &jim_target_number
,
4643 .usage
= "<number>",
4644 .help
= "Returns the name of target <n>",
4648 .mode
= COMMAND_ANY
,
4649 .jim_handler
= &jim_target_count
,
4650 .help
= "Returns the number of targets as an integer",
4652 COMMAND_REGISTRATION_DONE
4664 static int fastload_num
;
4665 static struct FastLoad
*fastload
;
4667 static void free_fastload(void)
4669 if (fastload
!= NULL
)
4672 for (i
= 0; i
< fastload_num
; i
++)
4674 if (fastload
[i
].data
)
4675 free(fastload
[i
].data
);
4685 COMMAND_HANDLER(handle_fast_load_image_command
)
4689 uint32_t image_size
;
4690 uint32_t min_address
= 0;
4691 uint32_t max_address
= 0xffffffff;
4696 int retval
= CALL_COMMAND_HANDLER(parse_load_image_command_CMD_ARGV
,
4697 &image
, &min_address
, &max_address
);
4698 if (ERROR_OK
!= retval
)
4701 struct duration bench
;
4702 duration_start(&bench
);
4704 if (image_open(&image
, CMD_ARGV
[0], (CMD_ARGC
>= 3) ? CMD_ARGV
[2] : NULL
) != ERROR_OK
)
4711 fastload_num
= image
.num_sections
;
4712 fastload
= (struct FastLoad
*)malloc(sizeof(struct FastLoad
)*image
.num_sections
);
4713 if (fastload
== NULL
)
4715 image_close(&image
);
4718 memset(fastload
, 0, sizeof(struct FastLoad
)*image
.num_sections
);
4719 for (i
= 0; i
< image
.num_sections
; i
++)
4721 buffer
= malloc(image
.sections
[i
].size
);
4724 command_print(CMD_CTX
, "error allocating buffer for section (%d bytes)",
4725 (int)(image
.sections
[i
].size
));
4729 if ((retval
= image_read_section(&image
, i
, 0x0, image
.sections
[i
].size
, buffer
, &buf_cnt
)) != ERROR_OK
)
4735 uint32_t offset
= 0;
4736 uint32_t length
= buf_cnt
;
4739 /* DANGER!!! beware of unsigned comparision here!!! */
4741 if ((image
.sections
[i
].base_address
+ buf_cnt
>= min_address
)&&
4742 (image
.sections
[i
].base_address
< max_address
))
4744 if (image
.sections
[i
].base_address
< min_address
)
4746 /* clip addresses below */
4747 offset
+= min_address
-image
.sections
[i
].base_address
;
4751 if (image
.sections
[i
].base_address
+ buf_cnt
> max_address
)
4753 length
-= (image
.sections
[i
].base_address
+ buf_cnt
)-max_address
;
4756 fastload
[i
].address
= image
.sections
[i
].base_address
+ offset
;
4757 fastload
[i
].data
= malloc(length
);
4758 if (fastload
[i
].data
== NULL
)
4763 memcpy(fastload
[i
].data
, buffer
+ offset
, length
);
4764 fastload
[i
].length
= length
;
4766 image_size
+= length
;
4767 command_print(CMD_CTX
, "%u bytes written at address 0x%8.8x",
4768 (unsigned int)length
,
4769 ((unsigned int)(image
.sections
[i
].base_address
+ offset
)));
4775 if ((ERROR_OK
== retval
) && (duration_measure(&bench
) == ERROR_OK
))
4777 command_print(CMD_CTX
, "Loaded %" PRIu32
" bytes "
4778 "in %fs (%0.3f kb/s)", image_size
,
4779 duration_elapsed(&bench
), duration_kbps(&bench
, image_size
));
4781 command_print(CMD_CTX
,
4782 "WARNING: image has not been loaded to target!"
4783 "You can issue a 'fast_load' to finish loading.");
4786 image_close(&image
);
4788 if (retval
!= ERROR_OK
)
4796 COMMAND_HANDLER(handle_fast_load_command
)
4799 return ERROR_COMMAND_SYNTAX_ERROR
;
4800 if (fastload
== NULL
)
4802 LOG_ERROR("No image in memory");
4806 int ms
= timeval_ms();
4808 int retval
= ERROR_OK
;
4809 for (i
= 0; i
< fastload_num
;i
++)
4811 struct target
*target
= get_current_target(CMD_CTX
);
4812 command_print(CMD_CTX
, "Write to 0x%08x, length 0x%08x",
4813 (unsigned int)(fastload
[i
].address
),
4814 (unsigned int)(fastload
[i
].length
));
4815 if (retval
== ERROR_OK
)
4817 retval
= target_write_buffer(target
, fastload
[i
].address
, fastload
[i
].length
, fastload
[i
].data
);
4819 size
+= fastload
[i
].length
;
4821 int after
= timeval_ms();
4822 command_print(CMD_CTX
, "Loaded image %f kBytes/s", (float)(size
/1024.0)/((float)(after
-ms
)/1000.0));
4826 static const struct command_registration target_command_handlers
[] = {
4829 .handler
= &handle_targets_command
,
4830 .mode
= COMMAND_ANY
,
4831 .help
= "change current command line target (one parameter) "
4832 "or list targets (no parameters)",
4833 .usage
= "[<new_current_target>]",
4837 .mode
= COMMAND_CONFIG
,
4838 .help
= "configure target",
4840 .chain
= target_subcommand_handlers
,
4842 COMMAND_REGISTRATION_DONE
4845 int target_register_commands(struct command_context
*cmd_ctx
)
4847 return register_commands(cmd_ctx
, NULL
, target_command_handlers
);
4850 static const struct command_registration target_exec_command_handlers
[] = {
4852 .name
= "fast_load_image",
4853 .handler
= &handle_fast_load_image_command
,
4854 .mode
= COMMAND_ANY
,
4855 .help
= "Load image into memory, mainly for profiling purposes",
4856 .usage
= "<file> <address> ['bin'|'ihex'|'elf'|'s19'] "
4857 "[min_address] [max_length]",
4860 .name
= "fast_load",
4861 .handler
= &handle_fast_load_command
,
4862 .mode
= COMMAND_ANY
,
4863 .help
= "loads active fast load image to current target "
4864 "- mainly for profiling purposes",
4868 .handler
= &handle_profile_command
,
4869 .mode
= COMMAND_EXEC
,
4870 .help
= "profiling samples the CPU PC",
4872 /** @todo don't register virt2phys() unless target supports it */
4874 .name
= "virt2phys",
4875 .handler
= &handle_virt2phys_command
,
4876 .mode
= COMMAND_ANY
,
4877 .help
= "translate a virtual address into a physical address",
4882 .handler
= &handle_reg_command
,
4883 .mode
= COMMAND_EXEC
,
4884 .help
= "display or set a register",
4889 .handler
= &handle_poll_command
,
4890 .mode
= COMMAND_EXEC
,
4891 .help
= "poll target state",
4894 .name
= "wait_halt",
4895 .handler
= &handle_wait_halt_command
,
4896 .mode
= COMMAND_EXEC
,
4897 .help
= "wait for target halt",
4898 .usage
= "[time (s)]",
4902 .handler
= &handle_halt_command
,
4903 .mode
= COMMAND_EXEC
,
4904 .help
= "halt target",
4908 .handler
= &handle_resume_command
,
4909 .mode
= COMMAND_EXEC
,
4910 .help
= "resume target",
4911 .usage
= "[<address>]",
4915 .handler
= &handle_reset_command
,
4916 .mode
= COMMAND_EXEC
,
4917 .usage
= "[run|halt|init]",
4918 .help
= "Reset all targets into the specified mode."
4919 "Default reset mode is run, if not given.",
4922 .name
= "soft_reset_halt",
4923 .handler
= &handle_soft_reset_halt_command
,
4924 .mode
= COMMAND_EXEC
,
4925 .help
= "halt the target and do a soft reset",
4930 .handler
= &handle_step_command
,
4931 .mode
= COMMAND_EXEC
,
4932 .help
= "step one instruction from current PC or [addr]",
4933 .usage
= "[<address>]",
4938 .handler
= &handle_md_command
,
4939 .mode
= COMMAND_EXEC
,
4940 .help
= "display memory words",
4941 .usage
= "[phys] <addr> [count]",
4945 .handler
= &handle_md_command
,
4946 .mode
= COMMAND_EXEC
,
4947 .help
= "display memory half-words",
4948 .usage
= "[phys] <addr> [count]",
4952 .handler
= &handle_md_command
,
4953 .mode
= COMMAND_EXEC
,
4954 .help
= "display memory bytes",
4955 .usage
= "[phys] <addr> [count]",
4960 .handler
= &handle_mw_command
,
4961 .mode
= COMMAND_EXEC
,
4962 .help
= "write memory word",
4963 .usage
= "[phys] <addr> <value> [count]",
4967 .handler
= &handle_mw_command
,
4968 .mode
= COMMAND_EXEC
,
4969 .help
= "write memory half-word",
4970 .usage
= "[phys] <addr> <value> [count]",
4974 .handler
= &handle_mw_command
,
4975 .mode
= COMMAND_EXEC
,
4976 .help
= "write memory byte",
4977 .usage
= "[phys] <addr> <value> [count]",
4982 .handler
= &handle_bp_command
,
4983 .mode
= COMMAND_EXEC
,
4984 .help
= "list or set breakpoint",
4985 .usage
= "[<address> <length> [hw]]",
4989 .handler
= &handle_rbp_command
,
4990 .mode
= COMMAND_EXEC
,
4991 .help
= "remove breakpoint",
4992 .usage
= "<address>",
4997 .handler
= &handle_wp_command
,
4998 .mode
= COMMAND_EXEC
,
4999 .help
= "list or set watchpoint",
5000 .usage
= "[<address> <length> <r/w/a> [value] [mask]]",
5004 .handler
= &handle_rwp_command
,
5005 .mode
= COMMAND_EXEC
,
5006 .help
= "remove watchpoint",
5007 .usage
= "<address>",
5011 .name
= "load_image",
5012 .handler
= &handle_load_image_command
,
5013 .mode
= COMMAND_EXEC
,
5014 .usage
= "<file> <address> ['bin'|'ihex'|'elf'|'s19'] "
5015 "[min_address] [max_length]",
5018 .name
= "dump_image",
5019 .handler
= &handle_dump_image_command
,
5020 .mode
= COMMAND_EXEC
,
5021 .usage
= "<file> <address> <size>",
5024 .name
= "verify_image",
5025 .handler
= &handle_verify_image_command
,
5026 .mode
= COMMAND_EXEC
,
5027 .usage
= "<file> [offset] [type]",
5030 .name
= "test_image",
5031 .handler
= &handle_test_image_command
,
5032 .mode
= COMMAND_EXEC
,
5033 .usage
= "<file> [offset] [type]",
5036 .name
= "ocd_mem2array",
5037 .mode
= COMMAND_EXEC
,
5038 .jim_handler
= &jim_mem2array
,
5039 .help
= "read memory and return as a TCL array "
5040 "for script processing",
5041 .usage
= "<arrayname> <width=32|16|8> <address> <count>",
5044 .name
= "ocd_array2mem",
5045 .mode
= COMMAND_EXEC
,
5046 .jim_handler
= &jim_array2mem
,
5047 .help
= "convert a TCL array to memory locations "
5048 "and write the values",
5049 .usage
= "<arrayname> <width=32|16|8> <address> <count>",
5051 COMMAND_REGISTRATION_DONE
5053 int target_register_user_commands(struct command_context
*cmd_ctx
)
5055 int retval
= ERROR_OK
;
5056 if ((retval
= target_request_register_commands(cmd_ctx
)) != ERROR_OK
)
5059 if ((retval
= trace_register_commands(cmd_ctx
)) != ERROR_OK
)
5063 return register_commands(cmd_ctx
, NULL
, target_exec_command_handlers
);