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 dsp563xx_target
;
68 extern struct target_type testee_target
;
70 struct target_type
*target_types
[] =
92 struct target
*all_targets
= NULL
;
93 struct target_event_callback
*target_event_callbacks
= NULL
;
94 struct target_timer_callback
*target_timer_callbacks
= NULL
;
96 static const Jim_Nvp nvp_assert
[] = {
97 { .name
= "assert", NVP_ASSERT
},
98 { .name
= "deassert", NVP_DEASSERT
},
99 { .name
= "T", NVP_ASSERT
},
100 { .name
= "F", NVP_DEASSERT
},
101 { .name
= "t", NVP_ASSERT
},
102 { .name
= "f", NVP_DEASSERT
},
103 { .name
= NULL
, .value
= -1 }
106 static const Jim_Nvp nvp_error_target
[] = {
107 { .value
= ERROR_TARGET_INVALID
, .name
= "err-invalid" },
108 { .value
= ERROR_TARGET_INIT_FAILED
, .name
= "err-init-failed" },
109 { .value
= ERROR_TARGET_TIMEOUT
, .name
= "err-timeout" },
110 { .value
= ERROR_TARGET_NOT_HALTED
, .name
= "err-not-halted" },
111 { .value
= ERROR_TARGET_FAILURE
, .name
= "err-failure" },
112 { .value
= ERROR_TARGET_UNALIGNED_ACCESS
, .name
= "err-unaligned-access" },
113 { .value
= ERROR_TARGET_DATA_ABORT
, .name
= "err-data-abort" },
114 { .value
= ERROR_TARGET_RESOURCE_NOT_AVAILABLE
, .name
= "err-resource-not-available" },
115 { .value
= ERROR_TARGET_TRANSLATION_FAULT
, .name
= "err-translation-fault" },
116 { .value
= ERROR_TARGET_NOT_RUNNING
, .name
= "err-not-running" },
117 { .value
= ERROR_TARGET_NOT_EXAMINED
, .name
= "err-not-examined" },
118 { .value
= -1, .name
= NULL
}
121 const char *target_strerror_safe(int err
)
125 n
= Jim_Nvp_value2name_simple(nvp_error_target
, err
);
126 if (n
->name
== NULL
) {
133 static const Jim_Nvp nvp_target_event
[] = {
134 { .value
= TARGET_EVENT_OLD_gdb_program_config
, .name
= "old-gdb_program_config" },
135 { .value
= TARGET_EVENT_OLD_pre_resume
, .name
= "old-pre_resume" },
137 { .value
= TARGET_EVENT_GDB_HALT
, .name
= "gdb-halt" },
138 { .value
= TARGET_EVENT_HALTED
, .name
= "halted" },
139 { .value
= TARGET_EVENT_RESUMED
, .name
= "resumed" },
140 { .value
= TARGET_EVENT_RESUME_START
, .name
= "resume-start" },
141 { .value
= TARGET_EVENT_RESUME_END
, .name
= "resume-end" },
143 { .name
= "gdb-start", .value
= TARGET_EVENT_GDB_START
},
144 { .name
= "gdb-end", .value
= TARGET_EVENT_GDB_END
},
146 /* historical name */
148 { .value
= TARGET_EVENT_RESET_START
, .name
= "reset-start" },
150 { .value
= TARGET_EVENT_RESET_ASSERT_PRE
, .name
= "reset-assert-pre" },
151 { .value
= TARGET_EVENT_RESET_ASSERT
, .name
= "reset-assert" },
152 { .value
= TARGET_EVENT_RESET_ASSERT_POST
, .name
= "reset-assert-post" },
153 { .value
= TARGET_EVENT_RESET_DEASSERT_PRE
, .name
= "reset-deassert-pre" },
154 { .value
= TARGET_EVENT_RESET_DEASSERT_POST
, .name
= "reset-deassert-post" },
155 { .value
= TARGET_EVENT_RESET_HALT_PRE
, .name
= "reset-halt-pre" },
156 { .value
= TARGET_EVENT_RESET_HALT_POST
, .name
= "reset-halt-post" },
157 { .value
= TARGET_EVENT_RESET_WAIT_PRE
, .name
= "reset-wait-pre" },
158 { .value
= TARGET_EVENT_RESET_WAIT_POST
, .name
= "reset-wait-post" },
159 { .value
= TARGET_EVENT_RESET_INIT
, .name
= "reset-init" },
160 { .value
= TARGET_EVENT_RESET_END
, .name
= "reset-end" },
162 { .value
= TARGET_EVENT_EXAMINE_START
, .name
= "examine-start" },
163 { .value
= TARGET_EVENT_EXAMINE_END
, .name
= "examine-end" },
165 { .value
= TARGET_EVENT_DEBUG_HALTED
, .name
= "debug-halted" },
166 { .value
= TARGET_EVENT_DEBUG_RESUMED
, .name
= "debug-resumed" },
168 { .value
= TARGET_EVENT_GDB_ATTACH
, .name
= "gdb-attach" },
169 { .value
= TARGET_EVENT_GDB_DETACH
, .name
= "gdb-detach" },
171 { .value
= TARGET_EVENT_GDB_FLASH_WRITE_START
, .name
= "gdb-flash-write-start" },
172 { .value
= TARGET_EVENT_GDB_FLASH_WRITE_END
, .name
= "gdb-flash-write-end" },
174 { .value
= TARGET_EVENT_GDB_FLASH_ERASE_START
, .name
= "gdb-flash-erase-start" },
175 { .value
= TARGET_EVENT_GDB_FLASH_ERASE_END
, .name
= "gdb-flash-erase-end" },
177 { .value
= TARGET_EVENT_RESUME_START
, .name
= "resume-start" },
178 { .value
= TARGET_EVENT_RESUMED
, .name
= "resume-ok" },
179 { .value
= TARGET_EVENT_RESUME_END
, .name
= "resume-end" },
181 { .name
= NULL
, .value
= -1 }
184 static const Jim_Nvp nvp_target_state
[] = {
185 { .name
= "unknown", .value
= TARGET_UNKNOWN
},
186 { .name
= "running", .value
= TARGET_RUNNING
},
187 { .name
= "halted", .value
= TARGET_HALTED
},
188 { .name
= "reset", .value
= TARGET_RESET
},
189 { .name
= "debug-running", .value
= TARGET_DEBUG_RUNNING
},
190 { .name
= NULL
, .value
= -1 },
193 static const Jim_Nvp nvp_target_debug_reason
[] = {
194 { .name
= "debug-request" , .value
= DBG_REASON_DBGRQ
},
195 { .name
= "breakpoint" , .value
= DBG_REASON_BREAKPOINT
},
196 { .name
= "watchpoint" , .value
= DBG_REASON_WATCHPOINT
},
197 { .name
= "watchpoint-and-breakpoint", .value
= DBG_REASON_WPTANDBKPT
},
198 { .name
= "single-step" , .value
= DBG_REASON_SINGLESTEP
},
199 { .name
= "target-not-halted" , .value
= DBG_REASON_NOTHALTED
},
200 { .name
= "undefined" , .value
= DBG_REASON_UNDEFINED
},
201 { .name
= NULL
, .value
= -1 },
204 static const Jim_Nvp nvp_target_endian
[] = {
205 { .name
= "big", .value
= TARGET_BIG_ENDIAN
},
206 { .name
= "little", .value
= TARGET_LITTLE_ENDIAN
},
207 { .name
= "be", .value
= TARGET_BIG_ENDIAN
},
208 { .name
= "le", .value
= TARGET_LITTLE_ENDIAN
},
209 { .name
= NULL
, .value
= -1 },
212 static const Jim_Nvp nvp_reset_modes
[] = {
213 { .name
= "unknown", .value
= RESET_UNKNOWN
},
214 { .name
= "run" , .value
= RESET_RUN
},
215 { .name
= "halt" , .value
= RESET_HALT
},
216 { .name
= "init" , .value
= RESET_INIT
},
217 { .name
= NULL
, .value
= -1 },
220 const char *debug_reason_name(struct target
*t
)
224 cp
= Jim_Nvp_value2name_simple(nvp_target_debug_reason
,
225 t
->debug_reason
)->name
;
227 LOG_ERROR("Invalid debug reason: %d", (int)(t
->debug_reason
));
228 cp
= "(*BUG*unknown*BUG*)";
234 target_state_name( struct target
*t
)
237 cp
= Jim_Nvp_value2name_simple(nvp_target_state
, t
->state
)->name
;
239 LOG_ERROR("Invalid target state: %d", (int)(t
->state
));
240 cp
= "(*BUG*unknown*BUG*)";
245 /* determine the number of the new target */
246 static int new_target_number(void)
251 /* number is 0 based */
255 if (x
< t
->target_number
) {
256 x
= t
->target_number
;
263 /* read a uint32_t from a buffer in target memory endianness */
264 uint32_t target_buffer_get_u32(struct target
*target
, const uint8_t *buffer
)
266 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
267 return le_to_h_u32(buffer
);
269 return be_to_h_u32(buffer
);
272 /* read a uint16_t from a buffer in target memory endianness */
273 uint16_t target_buffer_get_u16(struct target
*target
, const uint8_t *buffer
)
275 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
276 return le_to_h_u16(buffer
);
278 return be_to_h_u16(buffer
);
281 /* read a uint8_t from a buffer in target memory endianness */
282 uint8_t target_buffer_get_u8(struct target
*target
, const uint8_t *buffer
)
284 return *buffer
& 0x0ff;
287 /* write a uint32_t to a buffer in target memory endianness */
288 void target_buffer_set_u32(struct target
*target
, uint8_t *buffer
, uint32_t value
)
290 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
291 h_u32_to_le(buffer
, value
);
293 h_u32_to_be(buffer
, value
);
296 /* write a uint16_t to a buffer in target memory endianness */
297 void target_buffer_set_u16(struct target
*target
, uint8_t *buffer
, uint16_t value
)
299 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
300 h_u16_to_le(buffer
, value
);
302 h_u16_to_be(buffer
, value
);
305 /* write a uint8_t to a buffer in target memory endianness */
306 void target_buffer_set_u8(struct target
*target
, uint8_t *buffer
, uint8_t value
)
311 /* return a pointer to a configured target; id is name or number */
312 struct target
*get_target(const char *id
)
314 struct target
*target
;
316 /* try as tcltarget name */
317 for (target
= all_targets
; target
; target
= target
->next
) {
318 if (target
->cmd_name
== NULL
)
320 if (strcmp(id
, target
->cmd_name
) == 0)
324 /* It's OK to remove this fallback sometime after August 2010 or so */
326 /* no match, try as number */
328 if (parse_uint(id
, &num
) != ERROR_OK
)
331 for (target
= all_targets
; target
; target
= target
->next
) {
332 if (target
->target_number
== (int)num
) {
333 LOG_WARNING("use '%s' as target identifier, not '%u'",
334 target
->cmd_name
, num
);
342 /* returns a pointer to the n-th configured target */
343 static struct target
*get_target_by_num(int num
)
345 struct target
*target
= all_targets
;
348 if (target
->target_number
== num
) {
351 target
= target
->next
;
357 struct target
* get_current_target(struct command_context
*cmd_ctx
)
359 struct target
*target
= get_target_by_num(cmd_ctx
->current_target
);
363 LOG_ERROR("BUG: current_target out of bounds");
370 int target_poll(struct target
*target
)
374 /* We can't poll until after examine */
375 if (!target_was_examined(target
))
377 /* Fail silently lest we pollute the log */
381 retval
= target
->type
->poll(target
);
382 if (retval
!= ERROR_OK
)
385 if (target
->halt_issued
)
387 if (target
->state
== TARGET_HALTED
)
389 target
->halt_issued
= false;
392 long long t
= timeval_ms() - target
->halt_issued_time
;
395 target
->halt_issued
= false;
396 LOG_INFO("Halt timed out, wake up GDB.");
397 target_call_event_callbacks(target
, TARGET_EVENT_GDB_HALT
);
405 int target_halt(struct target
*target
)
408 /* We can't poll until after examine */
409 if (!target_was_examined(target
))
411 LOG_ERROR("Target not examined yet");
415 retval
= target
->type
->halt(target
);
416 if (retval
!= ERROR_OK
)
419 target
->halt_issued
= true;
420 target
->halt_issued_time
= timeval_ms();
425 int target_resume(struct target
*target
, int current
, uint32_t address
, int handle_breakpoints
, int debug_execution
)
429 /* We can't poll until after examine */
430 if (!target_was_examined(target
))
432 LOG_ERROR("Target not examined yet");
436 /* note that resume *must* be asynchronous. The CPU can halt before we poll. The CPU can
437 * even halt at the current PC as a result of a software breakpoint being inserted by (a bug?)
440 if ((retval
= target
->type
->resume(target
, current
, address
, handle_breakpoints
, debug_execution
)) != ERROR_OK
)
446 int target_process_reset(struct command_context
*cmd_ctx
, enum target_reset_mode reset_mode
)
451 n
= Jim_Nvp_value2name_simple(nvp_reset_modes
, reset_mode
);
452 if (n
->name
== NULL
) {
453 LOG_ERROR("invalid reset mode");
457 /* disable polling during reset to make reset event scripts
458 * more predictable, i.e. dr/irscan & pathmove in events will
459 * not have JTAG operations injected into the middle of a sequence.
461 bool save_poll
= jtag_poll_get_enabled();
463 jtag_poll_set_enabled(false);
465 sprintf(buf
, "ocd_process_reset %s", n
->name
);
466 retval
= Jim_Eval(cmd_ctx
->interp
, buf
);
468 jtag_poll_set_enabled(save_poll
);
470 if (retval
!= JIM_OK
) {
471 Jim_PrintErrorMessage(cmd_ctx
->interp
);
475 /* We want any events to be processed before the prompt */
476 retval
= target_call_timer_callbacks_now();
481 static int identity_virt2phys(struct target
*target
,
482 uint32_t virtual, uint32_t *physical
)
488 static int no_mmu(struct target
*target
, int *enabled
)
494 static int default_examine(struct target
*target
)
496 target_set_examined(target
);
500 int target_examine_one(struct target
*target
)
502 return target
->type
->examine(target
);
505 static int jtag_enable_callback(enum jtag_event event
, void *priv
)
507 struct target
*target
= priv
;
509 if (event
!= JTAG_TAP_EVENT_ENABLE
|| !target
->tap
->enabled
)
512 jtag_unregister_event_callback(jtag_enable_callback
, target
);
513 return target_examine_one(target
);
517 /* Targets that correctly implement init + examine, i.e.
518 * no communication with target during init:
522 int target_examine(void)
524 int retval
= ERROR_OK
;
525 struct target
*target
;
527 for (target
= all_targets
; target
; target
= target
->next
)
529 /* defer examination, but don't skip it */
530 if (!target
->tap
->enabled
) {
531 jtag_register_event_callback(jtag_enable_callback
,
535 if ((retval
= target_examine_one(target
)) != ERROR_OK
)
540 const char *target_type_name(struct target
*target
)
542 return target
->type
->name
;
545 static int target_write_memory_imp(struct target
*target
, uint32_t address
, uint32_t size
, uint32_t count
, uint8_t *buffer
)
547 if (!target_was_examined(target
))
549 LOG_ERROR("Target not examined yet");
552 return target
->type
->write_memory_imp(target
, address
, size
, count
, buffer
);
555 static int target_read_memory_imp(struct target
*target
, uint32_t address
, uint32_t size
, uint32_t count
, uint8_t *buffer
)
557 if (!target_was_examined(target
))
559 LOG_ERROR("Target not examined yet");
562 return target
->type
->read_memory_imp(target
, address
, size
, count
, buffer
);
565 static int target_soft_reset_halt_imp(struct target
*target
)
567 if (!target_was_examined(target
))
569 LOG_ERROR("Target not examined yet");
572 if (!target
->type
->soft_reset_halt_imp
) {
573 LOG_ERROR("Target %s does not support soft_reset_halt",
574 target_name(target
));
577 return target
->type
->soft_reset_halt_imp(target
);
580 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
)
582 if (!target_was_examined(target
))
584 LOG_ERROR("Target not examined yet");
587 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
);
590 int target_read_memory(struct target
*target
,
591 uint32_t address
, uint32_t size
, uint32_t count
, uint8_t *buffer
)
593 return target
->type
->read_memory(target
, address
, size
, count
, buffer
);
596 int target_read_phys_memory(struct target
*target
,
597 uint32_t address
, uint32_t size
, uint32_t count
, uint8_t *buffer
)
599 return target
->type
->read_phys_memory(target
, address
, size
, count
, buffer
);
602 int target_write_memory(struct target
*target
,
603 uint32_t address
, uint32_t size
, uint32_t count
, uint8_t *buffer
)
605 return target
->type
->write_memory(target
, address
, size
, count
, buffer
);
608 int target_write_phys_memory(struct target
*target
,
609 uint32_t address
, uint32_t size
, uint32_t count
, uint8_t *buffer
)
611 return target
->type
->write_phys_memory(target
, address
, size
, count
, buffer
);
614 int target_bulk_write_memory(struct target
*target
,
615 uint32_t address
, uint32_t count
, uint8_t *buffer
)
617 return target
->type
->bulk_write_memory(target
, address
, count
, buffer
);
620 int target_add_breakpoint(struct target
*target
,
621 struct breakpoint
*breakpoint
)
623 if (target
->state
!= TARGET_HALTED
) {
624 LOG_WARNING("target %s is not halted", target
->cmd_name
);
625 return ERROR_TARGET_NOT_HALTED
;
627 return target
->type
->add_breakpoint(target
, breakpoint
);
629 int target_remove_breakpoint(struct target
*target
,
630 struct breakpoint
*breakpoint
)
632 return target
->type
->remove_breakpoint(target
, breakpoint
);
635 int target_add_watchpoint(struct target
*target
,
636 struct watchpoint
*watchpoint
)
638 if (target
->state
!= TARGET_HALTED
) {
639 LOG_WARNING("target %s is not halted", target
->cmd_name
);
640 return ERROR_TARGET_NOT_HALTED
;
642 return target
->type
->add_watchpoint(target
, watchpoint
);
644 int target_remove_watchpoint(struct target
*target
,
645 struct watchpoint
*watchpoint
)
647 return target
->type
->remove_watchpoint(target
, watchpoint
);
650 int target_get_gdb_reg_list(struct target
*target
,
651 struct reg
**reg_list
[], int *reg_list_size
)
653 return target
->type
->get_gdb_reg_list(target
, reg_list
, reg_list_size
);
655 int target_step(struct target
*target
,
656 int current
, uint32_t address
, int handle_breakpoints
)
658 return target
->type
->step(target
, current
, address
, handle_breakpoints
);
662 int target_run_algorithm(struct target
*target
,
663 int num_mem_params
, struct mem_param
*mem_params
,
664 int num_reg_params
, struct reg_param
*reg_param
,
665 uint32_t entry_point
, uint32_t exit_point
,
666 int timeout_ms
, void *arch_info
)
668 return target
->type
->run_algorithm(target
,
669 num_mem_params
, mem_params
, num_reg_params
, reg_param
,
670 entry_point
, exit_point
, timeout_ms
, arch_info
);
674 * Reset the @c examined flag for the given target.
675 * Pure paranoia -- targets are zeroed on allocation.
677 static void target_reset_examined(struct target
*target
)
679 target
->examined
= false;
683 err_read_phys_memory(struct target
*target
, uint32_t address
,
684 uint32_t size
, uint32_t count
, uint8_t *buffer
)
686 LOG_ERROR("Not implemented: %s", __func__
);
691 err_write_phys_memory(struct target
*target
, uint32_t address
,
692 uint32_t size
, uint32_t count
, uint8_t *buffer
)
694 LOG_ERROR("Not implemented: %s", __func__
);
698 static int handle_target(void *priv
);
700 static int target_init_one(struct command_context
*cmd_ctx
,
701 struct target
*target
)
703 target_reset_examined(target
);
705 struct target_type
*type
= target
->type
;
706 if (type
->examine
== NULL
)
707 type
->examine
= default_examine
;
709 int retval
= type
->init_target(cmd_ctx
, target
);
710 if (ERROR_OK
!= retval
)
712 LOG_ERROR("target '%s' init failed", target_name(target
));
717 * @todo get rid of those *memory_imp() methods, now that all
718 * callers are using target_*_memory() accessors ... and make
719 * sure the "physical" paths handle the same issues.
721 /* a non-invasive way(in terms of patches) to add some code that
722 * runs before the type->write/read_memory implementation
724 type
->write_memory_imp
= target
->type
->write_memory
;
725 type
->write_memory
= target_write_memory_imp
;
727 type
->read_memory_imp
= target
->type
->read_memory
;
728 type
->read_memory
= target_read_memory_imp
;
730 type
->soft_reset_halt_imp
= target
->type
->soft_reset_halt
;
731 type
->soft_reset_halt
= target_soft_reset_halt_imp
;
733 type
->run_algorithm_imp
= target
->type
->run_algorithm
;
734 type
->run_algorithm
= target_run_algorithm_imp
;
736 /* Sanity-check MMU support ... stub in what we must, to help
737 * implement it in stages, but warn if we need to do so.
741 if (type
->write_phys_memory
== NULL
)
743 LOG_ERROR("type '%s' is missing write_phys_memory",
745 type
->write_phys_memory
= err_write_phys_memory
;
747 if (type
->read_phys_memory
== NULL
)
749 LOG_ERROR("type '%s' is missing read_phys_memory",
751 type
->read_phys_memory
= err_read_phys_memory
;
753 if (type
->virt2phys
== NULL
)
755 LOG_ERROR("type '%s' is missing virt2phys", type
->name
);
756 type
->virt2phys
= identity_virt2phys
;
761 /* Make sure no-MMU targets all behave the same: make no
762 * distinction between physical and virtual addresses, and
763 * ensure that virt2phys() is always an identity mapping.
765 if (type
->write_phys_memory
|| type
->read_phys_memory
768 LOG_WARNING("type '%s' has bad MMU hooks", type
->name
);
772 type
->write_phys_memory
= type
->write_memory
;
773 type
->read_phys_memory
= type
->read_memory
;
774 type
->virt2phys
= identity_virt2phys
;
779 int target_init(struct command_context
*cmd_ctx
)
781 struct target
*target
;
784 for (target
= all_targets
; target
; target
= target
->next
)
786 retval
= target_init_one(cmd_ctx
, target
);
787 if (ERROR_OK
!= retval
)
794 retval
= target_register_user_commands(cmd_ctx
);
795 if (ERROR_OK
!= retval
)
798 retval
= target_register_timer_callback(&handle_target
,
799 100, 1, cmd_ctx
->interp
);
800 if (ERROR_OK
!= retval
)
806 COMMAND_HANDLER(handle_target_init_command
)
809 return ERROR_COMMAND_SYNTAX_ERROR
;
811 static bool target_initialized
= false;
812 if (target_initialized
)
814 LOG_INFO("'target init' has already been called");
817 target_initialized
= true;
819 LOG_DEBUG("Initializing targets...");
820 return target_init(CMD_CTX
);
823 int target_register_event_callback(int (*callback
)(struct target
*target
, enum target_event event
, void *priv
), void *priv
)
825 struct target_event_callback
**callbacks_p
= &target_event_callbacks
;
827 if (callback
== NULL
)
829 return ERROR_INVALID_ARGUMENTS
;
834 while ((*callbacks_p
)->next
)
835 callbacks_p
= &((*callbacks_p
)->next
);
836 callbacks_p
= &((*callbacks_p
)->next
);
839 (*callbacks_p
) = malloc(sizeof(struct target_event_callback
));
840 (*callbacks_p
)->callback
= callback
;
841 (*callbacks_p
)->priv
= priv
;
842 (*callbacks_p
)->next
= NULL
;
847 int target_register_timer_callback(int (*callback
)(void *priv
), int time_ms
, int periodic
, void *priv
)
849 struct target_timer_callback
**callbacks_p
= &target_timer_callbacks
;
852 if (callback
== NULL
)
854 return ERROR_INVALID_ARGUMENTS
;
859 while ((*callbacks_p
)->next
)
860 callbacks_p
= &((*callbacks_p
)->next
);
861 callbacks_p
= &((*callbacks_p
)->next
);
864 (*callbacks_p
) = malloc(sizeof(struct target_timer_callback
));
865 (*callbacks_p
)->callback
= callback
;
866 (*callbacks_p
)->periodic
= periodic
;
867 (*callbacks_p
)->time_ms
= time_ms
;
869 gettimeofday(&now
, NULL
);
870 (*callbacks_p
)->when
.tv_usec
= now
.tv_usec
+ (time_ms
% 1000) * 1000;
871 time_ms
-= (time_ms
% 1000);
872 (*callbacks_p
)->when
.tv_sec
= now
.tv_sec
+ (time_ms
/ 1000);
873 if ((*callbacks_p
)->when
.tv_usec
> 1000000)
875 (*callbacks_p
)->when
.tv_usec
= (*callbacks_p
)->when
.tv_usec
- 1000000;
876 (*callbacks_p
)->when
.tv_sec
+= 1;
879 (*callbacks_p
)->priv
= priv
;
880 (*callbacks_p
)->next
= NULL
;
885 int target_unregister_event_callback(int (*callback
)(struct target
*target
, enum target_event event
, void *priv
), void *priv
)
887 struct target_event_callback
**p
= &target_event_callbacks
;
888 struct target_event_callback
*c
= target_event_callbacks
;
890 if (callback
== NULL
)
892 return ERROR_INVALID_ARGUMENTS
;
897 struct target_event_callback
*next
= c
->next
;
898 if ((c
->callback
== callback
) && (c
->priv
== priv
))
912 int target_unregister_timer_callback(int (*callback
)(void *priv
), void *priv
)
914 struct target_timer_callback
**p
= &target_timer_callbacks
;
915 struct target_timer_callback
*c
= target_timer_callbacks
;
917 if (callback
== NULL
)
919 return ERROR_INVALID_ARGUMENTS
;
924 struct target_timer_callback
*next
= c
->next
;
925 if ((c
->callback
== callback
) && (c
->priv
== priv
))
939 int target_call_event_callbacks(struct target
*target
, enum target_event event
)
941 struct target_event_callback
*callback
= target_event_callbacks
;
942 struct target_event_callback
*next_callback
;
944 if (event
== TARGET_EVENT_HALTED
)
946 /* execute early halted first */
947 target_call_event_callbacks(target
, TARGET_EVENT_GDB_HALT
);
950 LOG_DEBUG("target event %i (%s)",
952 Jim_Nvp_value2name_simple(nvp_target_event
, event
)->name
);
954 target_handle_event(target
, event
);
958 next_callback
= callback
->next
;
959 callback
->callback(target
, event
, callback
->priv
);
960 callback
= next_callback
;
966 static int target_timer_callback_periodic_restart(
967 struct target_timer_callback
*cb
, struct timeval
*now
)
969 int time_ms
= cb
->time_ms
;
970 cb
->when
.tv_usec
= now
->tv_usec
+ (time_ms
% 1000) * 1000;
971 time_ms
-= (time_ms
% 1000);
972 cb
->when
.tv_sec
= now
->tv_sec
+ time_ms
/ 1000;
973 if (cb
->when
.tv_usec
> 1000000)
975 cb
->when
.tv_usec
= cb
->when
.tv_usec
- 1000000;
976 cb
->when
.tv_sec
+= 1;
981 static int target_call_timer_callback(struct target_timer_callback
*cb
,
984 cb
->callback(cb
->priv
);
987 return target_timer_callback_periodic_restart(cb
, now
);
989 return target_unregister_timer_callback(cb
->callback
, cb
->priv
);
992 static int target_call_timer_callbacks_check_time(int checktime
)
997 gettimeofday(&now
, NULL
);
999 struct target_timer_callback
*callback
= target_timer_callbacks
;
1002 // cleaning up may unregister and free this callback
1003 struct target_timer_callback
*next_callback
= callback
->next
;
1005 bool call_it
= callback
->callback
&&
1006 ((!checktime
&& callback
->periodic
) ||
1007 now
.tv_sec
> callback
->when
.tv_sec
||
1008 (now
.tv_sec
== callback
->when
.tv_sec
&&
1009 now
.tv_usec
>= callback
->when
.tv_usec
));
1013 int retval
= target_call_timer_callback(callback
, &now
);
1014 if (retval
!= ERROR_OK
)
1018 callback
= next_callback
;
1024 int target_call_timer_callbacks(void)
1026 return target_call_timer_callbacks_check_time(1);
1029 /* invoke periodic callbacks immediately */
1030 int target_call_timer_callbacks_now(void)
1032 return target_call_timer_callbacks_check_time(0);
1035 int target_alloc_working_area(struct target
*target
, uint32_t size
, struct working_area
**area
)
1037 struct working_area
*c
= target
->working_areas
;
1038 struct working_area
*new_wa
= NULL
;
1040 /* Reevaluate working area address based on MMU state*/
1041 if (target
->working_areas
== NULL
)
1046 retval
= target
->type
->mmu(target
, &enabled
);
1047 if (retval
!= ERROR_OK
)
1053 if (target
->working_area_phys_spec
) {
1054 LOG_DEBUG("MMU disabled, using physical "
1055 "address for working memory 0x%08x",
1056 (unsigned)target
->working_area_phys
);
1057 target
->working_area
= target
->working_area_phys
;
1059 LOG_ERROR("No working memory available. "
1060 "Specify -work-area-phys to target.");
1061 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
1064 if (target
->working_area_virt_spec
) {
1065 LOG_DEBUG("MMU enabled, using virtual "
1066 "address for working memory 0x%08x",
1067 (unsigned)target
->working_area_virt
);
1068 target
->working_area
= target
->working_area_virt
;
1070 LOG_ERROR("No working memory available. "
1071 "Specify -work-area-virt to target.");
1072 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
1077 /* only allocate multiples of 4 byte */
1080 LOG_ERROR("BUG: code tried to allocate unaligned number of bytes (0x%08x), padding", ((unsigned)(size
)));
1081 size
= (size
+ 3) & (~3);
1084 /* see if there's already a matching working area */
1087 if ((c
->free
) && (c
->size
== size
))
1095 /* if not, allocate a new one */
1098 struct working_area
**p
= &target
->working_areas
;
1099 uint32_t first_free
= target
->working_area
;
1100 uint32_t free_size
= target
->working_area_size
;
1102 c
= target
->working_areas
;
1105 first_free
+= c
->size
;
1106 free_size
-= c
->size
;
1111 if (free_size
< size
)
1113 LOG_WARNING("not enough working area available(requested %u, free %u)",
1114 (unsigned)(size
), (unsigned)(free_size
));
1115 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
1118 LOG_DEBUG("allocated new working area at address 0x%08x", (unsigned)first_free
);
1120 new_wa
= malloc(sizeof(struct working_area
));
1121 new_wa
->next
= NULL
;
1122 new_wa
->size
= size
;
1123 new_wa
->address
= first_free
;
1125 if (target
->backup_working_area
)
1128 new_wa
->backup
= malloc(new_wa
->size
);
1129 if ((retval
= target_read_memory(target
, new_wa
->address
, 4, new_wa
->size
/ 4, new_wa
->backup
)) != ERROR_OK
)
1131 free(new_wa
->backup
);
1138 new_wa
->backup
= NULL
;
1141 /* put new entry in list */
1145 /* mark as used, and return the new (reused) area */
1150 new_wa
->user
= area
;
1155 int target_free_working_area_restore(struct target
*target
, struct working_area
*area
, int restore
)
1160 if (restore
&& target
->backup_working_area
)
1163 if ((retval
= target_write_memory(target
, area
->address
, 4, area
->size
/ 4, area
->backup
)) != ERROR_OK
)
1169 /* mark user pointer invalid */
1176 int target_free_working_area(struct target
*target
, struct working_area
*area
)
1178 return target_free_working_area_restore(target
, area
, 1);
1181 /* free resources and restore memory, if restoring memory fails,
1182 * free up resources anyway
1184 void target_free_all_working_areas_restore(struct target
*target
, int restore
)
1186 struct working_area
*c
= target
->working_areas
;
1190 struct working_area
*next
= c
->next
;
1191 target_free_working_area_restore(target
, c
, restore
);
1201 target
->working_areas
= NULL
;
1204 void target_free_all_working_areas(struct target
*target
)
1206 target_free_all_working_areas_restore(target
, 1);
1209 int target_arch_state(struct target
*target
)
1214 LOG_USER("No target has been configured");
1218 LOG_USER("target state: %s", target_state_name( target
));
1220 if (target
->state
!= TARGET_HALTED
)
1223 retval
= target
->type
->arch_state(target
);
1227 /* Single aligned words are guaranteed to use 16 or 32 bit access
1228 * mode respectively, otherwise data is handled as quickly as
1231 int target_write_buffer(struct target
*target
, uint32_t address
, uint32_t size
, uint8_t *buffer
)
1234 LOG_DEBUG("writing buffer of %i byte at 0x%8.8x",
1235 (int)size
, (unsigned)address
);
1237 if (!target_was_examined(target
))
1239 LOG_ERROR("Target not examined yet");
1247 if ((address
+ size
- 1) < address
)
1249 /* GDB can request this when e.g. PC is 0xfffffffc*/
1250 LOG_ERROR("address + size wrapped(0x%08x, 0x%08x)",
1256 if (((address
% 2) == 0) && (size
== 2))
1258 return target_write_memory(target
, address
, 2, 1, buffer
);
1261 /* handle unaligned head bytes */
1264 uint32_t unaligned
= 4 - (address
% 4);
1266 if (unaligned
> size
)
1269 if ((retval
= target_write_memory(target
, address
, 1, unaligned
, buffer
)) != ERROR_OK
)
1272 buffer
+= unaligned
;
1273 address
+= unaligned
;
1277 /* handle aligned words */
1280 int aligned
= size
- (size
% 4);
1282 /* use bulk writes above a certain limit. This may have to be changed */
1285 if ((retval
= target
->type
->bulk_write_memory(target
, address
, aligned
/ 4, buffer
)) != ERROR_OK
)
1290 if ((retval
= target_write_memory(target
, address
, 4, aligned
/ 4, buffer
)) != ERROR_OK
)
1299 /* handle tail writes of less than 4 bytes */
1302 if ((retval
= target_write_memory(target
, address
, 1, size
, buffer
)) != ERROR_OK
)
1309 /* Single aligned words are guaranteed to use 16 or 32 bit access
1310 * mode respectively, otherwise data is handled as quickly as
1313 int target_read_buffer(struct target
*target
, uint32_t address
, uint32_t size
, uint8_t *buffer
)
1316 LOG_DEBUG("reading buffer of %i byte at 0x%8.8x",
1317 (int)size
, (unsigned)address
);
1319 if (!target_was_examined(target
))
1321 LOG_ERROR("Target not examined yet");
1329 if ((address
+ size
- 1) < address
)
1331 /* GDB can request this when e.g. PC is 0xfffffffc*/
1332 LOG_ERROR("address + size wrapped(0x%08" PRIx32
", 0x%08" PRIx32
")",
1338 if (((address
% 2) == 0) && (size
== 2))
1340 return target_read_memory(target
, address
, 2, 1, buffer
);
1343 /* handle unaligned head bytes */
1346 uint32_t unaligned
= 4 - (address
% 4);
1348 if (unaligned
> size
)
1351 if ((retval
= target_read_memory(target
, address
, 1, unaligned
, buffer
)) != ERROR_OK
)
1354 buffer
+= unaligned
;
1355 address
+= unaligned
;
1359 /* handle aligned words */
1362 int aligned
= size
- (size
% 4);
1364 if ((retval
= target_read_memory(target
, address
, 4, aligned
/ 4, buffer
)) != ERROR_OK
)
1372 /*prevent byte access when possible (avoid AHB access limitations in some cases)*/
1375 int aligned
= size
- (size
%2);
1376 retval
= target_read_memory(target
, address
, 2, aligned
/ 2, buffer
);
1377 if (retval
!= ERROR_OK
)
1384 /* handle tail writes of less than 4 bytes */
1387 if ((retval
= target_read_memory(target
, address
, 1, size
, buffer
)) != ERROR_OK
)
1394 int target_checksum_memory(struct target
*target
, uint32_t address
, uint32_t size
, uint32_t* crc
)
1399 uint32_t checksum
= 0;
1400 if (!target_was_examined(target
))
1402 LOG_ERROR("Target not examined yet");
1406 if ((retval
= target
->type
->checksum_memory(target
, address
,
1407 size
, &checksum
)) != ERROR_OK
)
1409 buffer
= malloc(size
);
1412 LOG_ERROR("error allocating buffer for section (%d bytes)", (int)size
);
1413 return ERROR_INVALID_ARGUMENTS
;
1415 retval
= target_read_buffer(target
, address
, size
, buffer
);
1416 if (retval
!= ERROR_OK
)
1422 /* convert to target endianess */
1423 for (i
= 0; i
< (size
/sizeof(uint32_t)); i
++)
1425 uint32_t target_data
;
1426 target_data
= target_buffer_get_u32(target
, &buffer
[i
*sizeof(uint32_t)]);
1427 target_buffer_set_u32(target
, &buffer
[i
*sizeof(uint32_t)], target_data
);
1430 retval
= image_calculate_checksum(buffer
, size
, &checksum
);
1439 int target_blank_check_memory(struct target
*target
, uint32_t address
, uint32_t size
, uint32_t* blank
)
1442 if (!target_was_examined(target
))
1444 LOG_ERROR("Target not examined yet");
1448 if (target
->type
->blank_check_memory
== 0)
1449 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
1451 retval
= target
->type
->blank_check_memory(target
, address
, size
, blank
);
1456 int target_read_u32(struct target
*target
, uint32_t address
, uint32_t *value
)
1458 uint8_t value_buf
[4];
1459 if (!target_was_examined(target
))
1461 LOG_ERROR("Target not examined yet");
1465 int retval
= target_read_memory(target
, address
, 4, 1, value_buf
);
1467 if (retval
== ERROR_OK
)
1469 *value
= target_buffer_get_u32(target
, value_buf
);
1470 LOG_DEBUG("address: 0x%8.8" PRIx32
", value: 0x%8.8" PRIx32
"",
1477 LOG_DEBUG("address: 0x%8.8" PRIx32
" failed",
1484 int target_read_u16(struct target
*target
, uint32_t address
, uint16_t *value
)
1486 uint8_t value_buf
[2];
1487 if (!target_was_examined(target
))
1489 LOG_ERROR("Target not examined yet");
1493 int retval
= target_read_memory(target
, address
, 2, 1, value_buf
);
1495 if (retval
== ERROR_OK
)
1497 *value
= target_buffer_get_u16(target
, value_buf
);
1498 LOG_DEBUG("address: 0x%8.8" PRIx32
", value: 0x%4.4x",
1505 LOG_DEBUG("address: 0x%8.8" PRIx32
" failed",
1512 int target_read_u8(struct target
*target
, uint32_t address
, uint8_t *value
)
1514 int retval
= target_read_memory(target
, address
, 1, 1, value
);
1515 if (!target_was_examined(target
))
1517 LOG_ERROR("Target not examined yet");
1521 if (retval
== ERROR_OK
)
1523 LOG_DEBUG("address: 0x%8.8" PRIx32
", value: 0x%2.2x",
1530 LOG_DEBUG("address: 0x%8.8" PRIx32
" failed",
1537 int target_write_u32(struct target
*target
, uint32_t address
, uint32_t value
)
1540 uint8_t value_buf
[4];
1541 if (!target_was_examined(target
))
1543 LOG_ERROR("Target not examined yet");
1547 LOG_DEBUG("address: 0x%8.8" PRIx32
", value: 0x%8.8" PRIx32
"",
1551 target_buffer_set_u32(target
, value_buf
, value
);
1552 if ((retval
= target_write_memory(target
, address
, 4, 1, value_buf
)) != ERROR_OK
)
1554 LOG_DEBUG("failed: %i", retval
);
1560 int target_write_u16(struct target
*target
, uint32_t address
, uint16_t value
)
1563 uint8_t value_buf
[2];
1564 if (!target_was_examined(target
))
1566 LOG_ERROR("Target not examined yet");
1570 LOG_DEBUG("address: 0x%8.8" PRIx32
", value: 0x%8.8x",
1574 target_buffer_set_u16(target
, value_buf
, value
);
1575 if ((retval
= target_write_memory(target
, address
, 2, 1, value_buf
)) != ERROR_OK
)
1577 LOG_DEBUG("failed: %i", retval
);
1583 int target_write_u8(struct target
*target
, uint32_t address
, uint8_t value
)
1586 if (!target_was_examined(target
))
1588 LOG_ERROR("Target not examined yet");
1592 LOG_DEBUG("address: 0x%8.8" PRIx32
", value: 0x%2.2x",
1595 if ((retval
= target_write_memory(target
, address
, 1, 1, &value
)) != ERROR_OK
)
1597 LOG_DEBUG("failed: %i", retval
);
1603 COMMAND_HANDLER(handle_targets_command
)
1605 struct target
*target
= all_targets
;
1609 target
= get_target(CMD_ARGV
[0]);
1610 if (target
== NULL
) {
1611 command_print(CMD_CTX
,"Target: %s is unknown, try one of:\n", CMD_ARGV
[0]);
1614 if (!target
->tap
->enabled
) {
1615 command_print(CMD_CTX
,"Target: TAP %s is disabled, "
1616 "can't be the current target\n",
1617 target
->tap
->dotted_name
);
1621 CMD_CTX
->current_target
= target
->target_number
;
1626 target
= all_targets
;
1627 command_print(CMD_CTX
, " TargetName Type Endian TapName State ");
1628 command_print(CMD_CTX
, "-- ------------------ ---------- ------ ------------------ ------------");
1634 if (target
->tap
->enabled
)
1635 state
= target_state_name( target
);
1637 state
= "tap-disabled";
1639 if (CMD_CTX
->current_target
== target
->target_number
)
1642 /* keep columns lined up to match the headers above */
1643 command_print(CMD_CTX
, "%2d%c %-18s %-10s %-6s %-18s %s",
1644 target
->target_number
,
1646 target_name(target
),
1647 target_type_name(target
),
1648 Jim_Nvp_value2name_simple(nvp_target_endian
,
1649 target
->endianness
)->name
,
1650 target
->tap
->dotted_name
,
1652 target
= target
->next
;
1658 /* every 300ms we check for reset & powerdropout and issue a "reset halt" if so. */
1660 static int powerDropout
;
1661 static int srstAsserted
;
1663 static int runPowerRestore
;
1664 static int runPowerDropout
;
1665 static int runSrstAsserted
;
1666 static int runSrstDeasserted
;
1668 static int sense_handler(void)
1670 static int prevSrstAsserted
= 0;
1671 static int prevPowerdropout
= 0;
1674 if ((retval
= jtag_power_dropout(&powerDropout
)) != ERROR_OK
)
1678 powerRestored
= prevPowerdropout
&& !powerDropout
;
1681 runPowerRestore
= 1;
1684 long long current
= timeval_ms();
1685 static long long lastPower
= 0;
1686 int waitMore
= lastPower
+ 2000 > current
;
1687 if (powerDropout
&& !waitMore
)
1689 runPowerDropout
= 1;
1690 lastPower
= current
;
1693 if ((retval
= jtag_srst_asserted(&srstAsserted
)) != ERROR_OK
)
1697 srstDeasserted
= prevSrstAsserted
&& !srstAsserted
;
1699 static long long lastSrst
= 0;
1700 waitMore
= lastSrst
+ 2000 > current
;
1701 if (srstDeasserted
&& !waitMore
)
1703 runSrstDeasserted
= 1;
1707 if (!prevSrstAsserted
&& srstAsserted
)
1709 runSrstAsserted
= 1;
1712 prevSrstAsserted
= srstAsserted
;
1713 prevPowerdropout
= powerDropout
;
1715 if (srstDeasserted
|| powerRestored
)
1717 /* Other than logging the event we can't do anything here.
1718 * Issuing a reset is a particularly bad idea as we might
1719 * be inside a reset already.
1726 static void target_call_event_callbacks_all(enum target_event e
) {
1727 struct target
*target
;
1728 target
= all_targets
;
1730 target_call_event_callbacks(target
, e
);
1731 target
= target
->next
;
1735 /* process target state changes */
1736 static int handle_target(void *priv
)
1738 Jim_Interp
*interp
= (Jim_Interp
*)priv
;
1739 int retval
= ERROR_OK
;
1741 /* we do not want to recurse here... */
1742 static int recursive
= 0;
1747 /* danger! running these procedures can trigger srst assertions and power dropouts.
1748 * We need to avoid an infinite loop/recursion here and we do that by
1749 * clearing the flags after running these events.
1751 int did_something
= 0;
1752 if (runSrstAsserted
)
1754 target_call_event_callbacks_all(TARGET_EVENT_GDB_HALT
);
1755 Jim_Eval(interp
, "srst_asserted");
1758 if (runSrstDeasserted
)
1760 Jim_Eval(interp
, "srst_deasserted");
1763 if (runPowerDropout
)
1765 target_call_event_callbacks_all(TARGET_EVENT_GDB_HALT
);
1766 Jim_Eval(interp
, "power_dropout");
1769 if (runPowerRestore
)
1771 Jim_Eval(interp
, "power_restore");
1777 /* clear detect flags */
1781 /* clear action flags */
1783 runSrstAsserted
= 0;
1784 runSrstDeasserted
= 0;
1785 runPowerRestore
= 0;
1786 runPowerDropout
= 0;
1791 /* Poll targets for state changes unless that's globally disabled.
1792 * Skip targets that are currently disabled.
1794 for (struct target
*target
= all_targets
;
1795 is_jtag_poll_safe() && target
;
1796 target
= target
->next
)
1798 if (!target
->tap
->enabled
)
1801 /* only poll target if we've got power and srst isn't asserted */
1802 if (!powerDropout
&& !srstAsserted
)
1804 /* polling may fail silently until the target has been examined */
1805 if ((retval
= target_poll(target
)) != ERROR_OK
)
1807 target_call_event_callbacks(target
, TARGET_EVENT_GDB_HALT
);
1816 COMMAND_HANDLER(handle_reg_command
)
1818 struct target
*target
;
1819 struct reg
*reg
= NULL
;
1825 target
= get_current_target(CMD_CTX
);
1827 /* list all available registers for the current target */
1830 struct reg_cache
*cache
= target
->reg_cache
;
1837 command_print(CMD_CTX
, "===== %s", cache
->name
);
1839 for (i
= 0, reg
= cache
->reg_list
;
1840 i
< cache
->num_regs
;
1841 i
++, reg
++, count
++)
1843 /* only print cached values if they are valid */
1845 value
= buf_to_str(reg
->value
,
1847 command_print(CMD_CTX
,
1848 "(%i) %s (/%" PRIu32
"): 0x%s%s",
1856 command_print(CMD_CTX
, "(%i) %s (/%" PRIu32
")",
1861 cache
= cache
->next
;
1867 /* access a single register by its ordinal number */
1868 if ((CMD_ARGV
[0][0] >= '0') && (CMD_ARGV
[0][0] <= '9'))
1871 COMMAND_PARSE_NUMBER(uint
, CMD_ARGV
[0], num
);
1873 struct reg_cache
*cache
= target
->reg_cache
;
1878 for (i
= 0; i
< cache
->num_regs
; i
++)
1882 reg
= &cache
->reg_list
[i
];
1888 cache
= cache
->next
;
1893 command_print(CMD_CTX
, "%i is out of bounds, the current target has only %i registers (0 - %i)", num
, count
, count
- 1);
1896 } else /* access a single register by its name */
1898 reg
= register_get_by_name(target
->reg_cache
, CMD_ARGV
[0], 1);
1902 command_print(CMD_CTX
, "register %s not found in current target", CMD_ARGV
[0]);
1907 /* display a register */
1908 if ((CMD_ARGC
== 1) || ((CMD_ARGC
== 2) && !((CMD_ARGV
[1][0] >= '0') && (CMD_ARGV
[1][0] <= '9'))))
1910 if ((CMD_ARGC
== 2) && (strcmp(CMD_ARGV
[1], "force") == 0))
1913 if (reg
->valid
== 0)
1915 reg
->type
->get(reg
);
1917 value
= buf_to_str(reg
->value
, reg
->size
, 16);
1918 command_print(CMD_CTX
, "%s (/%i): 0x%s", reg
->name
, (int)(reg
->size
), value
);
1923 /* set register value */
1926 uint8_t *buf
= malloc(DIV_ROUND_UP(reg
->size
, 8));
1927 str_to_buf(CMD_ARGV
[1], strlen(CMD_ARGV
[1]), buf
, reg
->size
, 0);
1929 reg
->type
->set(reg
, buf
);
1931 value
= buf_to_str(reg
->value
, reg
->size
, 16);
1932 command_print(CMD_CTX
, "%s (/%i): 0x%s", reg
->name
, (int)(reg
->size
), value
);
1940 command_print(CMD_CTX
, "usage: reg <#|name> [value]");
1945 COMMAND_HANDLER(handle_poll_command
)
1947 int retval
= ERROR_OK
;
1948 struct target
*target
= get_current_target(CMD_CTX
);
1952 command_print(CMD_CTX
, "background polling: %s",
1953 jtag_poll_get_enabled() ? "on" : "off");
1954 command_print(CMD_CTX
, "TAP: %s (%s)",
1955 target
->tap
->dotted_name
,
1956 target
->tap
->enabled
? "enabled" : "disabled");
1957 if (!target
->tap
->enabled
)
1959 if ((retval
= target_poll(target
)) != ERROR_OK
)
1961 if ((retval
= target_arch_state(target
)) != ERROR_OK
)
1964 else if (CMD_ARGC
== 1)
1967 COMMAND_PARSE_ON_OFF(CMD_ARGV
[0], enable
);
1968 jtag_poll_set_enabled(enable
);
1972 return ERROR_COMMAND_SYNTAX_ERROR
;
1978 COMMAND_HANDLER(handle_wait_halt_command
)
1981 return ERROR_COMMAND_SYNTAX_ERROR
;
1986 int retval
= parse_uint(CMD_ARGV
[0], &ms
);
1987 if (ERROR_OK
!= retval
)
1989 command_print(CMD_CTX
, "usage: %s [seconds]", CMD_NAME
);
1990 return ERROR_COMMAND_SYNTAX_ERROR
;
1992 // convert seconds (given) to milliseconds (needed)
1996 struct target
*target
= get_current_target(CMD_CTX
);
1997 return target_wait_state(target
, TARGET_HALTED
, ms
);
2000 /* wait for target state to change. The trick here is to have a low
2001 * latency for short waits and not to suck up all the CPU time
2004 * After 500ms, keep_alive() is invoked
2006 int target_wait_state(struct target
*target
, enum target_state state
, int ms
)
2009 long long then
= 0, cur
;
2014 if ((retval
= target_poll(target
)) != ERROR_OK
)
2016 if (target
->state
== state
)
2024 then
= timeval_ms();
2025 LOG_DEBUG("waiting for target %s...",
2026 Jim_Nvp_value2name_simple(nvp_target_state
,state
)->name
);
2034 if ((cur
-then
) > ms
)
2036 LOG_ERROR("timed out while waiting for target %s",
2037 Jim_Nvp_value2name_simple(nvp_target_state
,state
)->name
);
2045 COMMAND_HANDLER(handle_halt_command
)
2049 struct target
*target
= get_current_target(CMD_CTX
);
2050 int retval
= target_halt(target
);
2051 if (ERROR_OK
!= retval
)
2057 retval
= parse_uint(CMD_ARGV
[0], &wait
);
2058 if (ERROR_OK
!= retval
)
2059 return ERROR_COMMAND_SYNTAX_ERROR
;
2064 return CALL_COMMAND_HANDLER(handle_wait_halt_command
);
2067 COMMAND_HANDLER(handle_soft_reset_halt_command
)
2069 struct target
*target
= get_current_target(CMD_CTX
);
2071 LOG_USER("requesting target halt and executing a soft reset");
2073 target
->type
->soft_reset_halt(target
);
2078 COMMAND_HANDLER(handle_reset_command
)
2081 return ERROR_COMMAND_SYNTAX_ERROR
;
2083 enum target_reset_mode reset_mode
= RESET_RUN
;
2087 n
= Jim_Nvp_name2value_simple(nvp_reset_modes
, CMD_ARGV
[0]);
2088 if ((n
->name
== NULL
) || (n
->value
== RESET_UNKNOWN
)) {
2089 return ERROR_COMMAND_SYNTAX_ERROR
;
2091 reset_mode
= n
->value
;
2094 /* reset *all* targets */
2095 return target_process_reset(CMD_CTX
, reset_mode
);
2099 COMMAND_HANDLER(handle_resume_command
)
2103 return ERROR_COMMAND_SYNTAX_ERROR
;
2105 struct target
*target
= get_current_target(CMD_CTX
);
2106 target_handle_event(target
, TARGET_EVENT_OLD_pre_resume
);
2108 /* with no CMD_ARGV, resume from current pc, addr = 0,
2109 * with one arguments, addr = CMD_ARGV[0],
2110 * handle breakpoints, not debugging */
2114 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], addr
);
2118 return target_resume(target
, current
, addr
, 1, 0);
2121 COMMAND_HANDLER(handle_step_command
)
2124 return ERROR_COMMAND_SYNTAX_ERROR
;
2128 /* with no CMD_ARGV, step from current pc, addr = 0,
2129 * with one argument addr = CMD_ARGV[0],
2130 * handle breakpoints, debugging */
2135 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], addr
);
2139 struct target
*target
= get_current_target(CMD_CTX
);
2141 return target
->type
->step(target
, current_pc
, addr
, 1);
2144 static void handle_md_output(struct command_context
*cmd_ctx
,
2145 struct target
*target
, uint32_t address
, unsigned size
,
2146 unsigned count
, const uint8_t *buffer
)
2148 const unsigned line_bytecnt
= 32;
2149 unsigned line_modulo
= line_bytecnt
/ size
;
2151 char output
[line_bytecnt
* 4 + 1];
2152 unsigned output_len
= 0;
2154 const char *value_fmt
;
2156 case 4: value_fmt
= "%8.8x "; break;
2157 case 2: value_fmt
= "%4.2x "; break;
2158 case 1: value_fmt
= "%2.2x "; break;
2160 /* "can't happen", caller checked */
2161 LOG_ERROR("invalid memory read size: %u", size
);
2165 for (unsigned i
= 0; i
< count
; i
++)
2167 if (i
% line_modulo
== 0)
2169 output_len
+= snprintf(output
+ output_len
,
2170 sizeof(output
) - output_len
,
2172 (unsigned)(address
+ (i
*size
)));
2176 const uint8_t *value_ptr
= buffer
+ i
* size
;
2178 case 4: value
= target_buffer_get_u32(target
, value_ptr
); break;
2179 case 2: value
= target_buffer_get_u16(target
, value_ptr
); break;
2180 case 1: value
= *value_ptr
;
2182 output_len
+= snprintf(output
+ output_len
,
2183 sizeof(output
) - output_len
,
2186 if ((i
% line_modulo
== line_modulo
- 1) || (i
== count
- 1))
2188 command_print(cmd_ctx
, "%s", output
);
2194 COMMAND_HANDLER(handle_md_command
)
2197 return ERROR_COMMAND_SYNTAX_ERROR
;
2200 switch (CMD_NAME
[2]) {
2201 case 'w': size
= 4; break;
2202 case 'h': size
= 2; break;
2203 case 'b': size
= 1; break;
2204 default: return ERROR_COMMAND_SYNTAX_ERROR
;
2207 bool physical
=strcmp(CMD_ARGV
[0], "phys")==0;
2208 int (*fn
)(struct target
*target
,
2209 uint32_t address
, uint32_t size
, uint32_t count
, uint8_t *buffer
);
2214 fn
=target_read_phys_memory
;
2217 fn
=target_read_memory
;
2219 if ((CMD_ARGC
< 1) || (CMD_ARGC
> 2))
2221 return ERROR_COMMAND_SYNTAX_ERROR
;
2225 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], address
);
2229 COMMAND_PARSE_NUMBER(uint
, CMD_ARGV
[1], count
);
2231 uint8_t *buffer
= calloc(count
, size
);
2233 struct target
*target
= get_current_target(CMD_CTX
);
2234 int retval
= fn(target
, address
, size
, count
, buffer
);
2235 if (ERROR_OK
== retval
)
2236 handle_md_output(CMD_CTX
, target
, address
, size
, count
, buffer
);
2243 COMMAND_HANDLER(handle_mw_command
)
2247 return ERROR_COMMAND_SYNTAX_ERROR
;
2249 bool physical
=strcmp(CMD_ARGV
[0], "phys")==0;
2250 int (*fn
)(struct target
*target
,
2251 uint32_t address
, uint32_t size
, uint32_t count
, uint8_t *buffer
);
2256 fn
=target_write_phys_memory
;
2259 fn
=target_write_memory
;
2261 if ((CMD_ARGC
< 2) || (CMD_ARGC
> 3))
2262 return ERROR_COMMAND_SYNTAX_ERROR
;
2265 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], address
);
2268 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], value
);
2272 COMMAND_PARSE_NUMBER(uint
, CMD_ARGV
[2], count
);
2274 struct target
*target
= get_current_target(CMD_CTX
);
2276 uint8_t value_buf
[4];
2277 switch (CMD_NAME
[2])
2281 target_buffer_set_u32(target
, value_buf
, value
);
2285 target_buffer_set_u16(target
, value_buf
, value
);
2289 value_buf
[0] = value
;
2292 return ERROR_COMMAND_SYNTAX_ERROR
;
2294 for (unsigned i
= 0; i
< count
; i
++)
2296 int retval
= fn(target
,
2297 address
+ i
* wordsize
, wordsize
, 1, value_buf
);
2298 if (ERROR_OK
!= retval
)
2307 static COMMAND_HELPER(parse_load_image_command_CMD_ARGV
, struct image
*image
,
2308 uint32_t *min_address
, uint32_t *max_address
)
2310 if (CMD_ARGC
< 1 || CMD_ARGC
> 5)
2311 return ERROR_COMMAND_SYNTAX_ERROR
;
2313 /* a base address isn't always necessary,
2314 * default to 0x0 (i.e. don't relocate) */
2318 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], addr
);
2319 image
->base_address
= addr
;
2320 image
->base_address_set
= 1;
2323 image
->base_address_set
= 0;
2325 image
->start_address_set
= 0;
2329 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[3], *min_address
);
2333 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[4], *max_address
);
2334 // use size (given) to find max (required)
2335 *max_address
+= *min_address
;
2338 if (*min_address
> *max_address
)
2339 return ERROR_COMMAND_SYNTAX_ERROR
;
2344 COMMAND_HANDLER(handle_load_image_command
)
2348 uint32_t image_size
;
2349 uint32_t min_address
= 0;
2350 uint32_t max_address
= 0xffffffff;
2354 int retval
= CALL_COMMAND_HANDLER(parse_load_image_command_CMD_ARGV
,
2355 &image
, &min_address
, &max_address
);
2356 if (ERROR_OK
!= retval
)
2359 struct target
*target
= get_current_target(CMD_CTX
);
2361 struct duration bench
;
2362 duration_start(&bench
);
2364 if (image_open(&image
, CMD_ARGV
[0], (CMD_ARGC
>= 3) ? CMD_ARGV
[2] : NULL
) != ERROR_OK
)
2371 for (i
= 0; i
< image
.num_sections
; i
++)
2373 buffer
= malloc(image
.sections
[i
].size
);
2376 command_print(CMD_CTX
,
2377 "error allocating buffer for section (%d bytes)",
2378 (int)(image
.sections
[i
].size
));
2382 if ((retval
= image_read_section(&image
, i
, 0x0, image
.sections
[i
].size
, buffer
, &buf_cnt
)) != ERROR_OK
)
2388 uint32_t offset
= 0;
2389 uint32_t length
= buf_cnt
;
2391 /* DANGER!!! beware of unsigned comparision here!!! */
2393 if ((image
.sections
[i
].base_address
+ buf_cnt
>= min_address
)&&
2394 (image
.sections
[i
].base_address
< max_address
))
2396 if (image
.sections
[i
].base_address
< min_address
)
2398 /* clip addresses below */
2399 offset
+= min_address
-image
.sections
[i
].base_address
;
2403 if (image
.sections
[i
].base_address
+ buf_cnt
> max_address
)
2405 length
-= (image
.sections
[i
].base_address
+ buf_cnt
)-max_address
;
2408 if ((retval
= target_write_buffer(target
, image
.sections
[i
].base_address
+ offset
, length
, buffer
+ offset
)) != ERROR_OK
)
2413 image_size
+= length
;
2414 command_print(CMD_CTX
, "%u bytes written at address 0x%8.8" PRIx32
"",
2415 (unsigned int)length
,
2416 image
.sections
[i
].base_address
+ offset
);
2422 if ((ERROR_OK
== retval
) && (duration_measure(&bench
) == ERROR_OK
))
2424 command_print(CMD_CTX
, "downloaded %" PRIu32
" bytes "
2425 "in %fs (%0.3f kb/s)", image_size
,
2426 duration_elapsed(&bench
), duration_kbps(&bench
, image_size
));
2429 image_close(&image
);
2435 COMMAND_HANDLER(handle_dump_image_command
)
2437 struct fileio fileio
;
2439 uint8_t buffer
[560];
2443 struct target
*target
= get_current_target(CMD_CTX
);
2447 command_print(CMD_CTX
, "usage: dump_image <filename> <address> <size>");
2452 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], address
);
2454 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[2], size
);
2456 if (fileio_open(&fileio
, CMD_ARGV
[0], FILEIO_WRITE
, FILEIO_BINARY
) != ERROR_OK
)
2461 struct duration bench
;
2462 duration_start(&bench
);
2464 int retval
= ERROR_OK
;
2467 size_t size_written
;
2468 uint32_t this_run_size
= (size
> 560) ? 560 : size
;
2469 retval
= target_read_buffer(target
, address
, this_run_size
, buffer
);
2470 if (retval
!= ERROR_OK
)
2475 retval
= fileio_write(&fileio
, this_run_size
, buffer
, &size_written
);
2476 if (retval
!= ERROR_OK
)
2481 size
-= this_run_size
;
2482 address
+= this_run_size
;
2485 if ((retvaltemp
= fileio_close(&fileio
)) != ERROR_OK
)
2488 if ((ERROR_OK
== retval
) && (duration_measure(&bench
) == ERROR_OK
))
2490 command_print(CMD_CTX
,
2491 "dumped %zu bytes in %fs (%0.3f kb/s)", fileio
.size
,
2492 duration_elapsed(&bench
), duration_kbps(&bench
, fileio
.size
));
2498 static COMMAND_HELPER(handle_verify_image_command_internal
, int verify
)
2502 uint32_t image_size
;
2505 uint32_t checksum
= 0;
2506 uint32_t mem_checksum
= 0;
2510 struct target
*target
= get_current_target(CMD_CTX
);
2514 return ERROR_COMMAND_SYNTAX_ERROR
;
2519 LOG_ERROR("no target selected");
2523 struct duration bench
;
2524 duration_start(&bench
);
2529 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], addr
);
2530 image
.base_address
= addr
;
2531 image
.base_address_set
= 1;
2535 image
.base_address_set
= 0;
2536 image
.base_address
= 0x0;
2539 image
.start_address_set
= 0;
2541 if ((retval
= image_open(&image
, CMD_ARGV
[0], (CMD_ARGC
== 3) ? CMD_ARGV
[2] : NULL
)) != ERROR_OK
)
2548 for (i
= 0; i
< image
.num_sections
; i
++)
2550 buffer
= malloc(image
.sections
[i
].size
);
2553 command_print(CMD_CTX
,
2554 "error allocating buffer for section (%d bytes)",
2555 (int)(image
.sections
[i
].size
));
2558 if ((retval
= image_read_section(&image
, i
, 0x0, image
.sections
[i
].size
, buffer
, &buf_cnt
)) != ERROR_OK
)
2566 /* calculate checksum of image */
2567 image_calculate_checksum(buffer
, buf_cnt
, &checksum
);
2569 retval
= target_checksum_memory(target
, image
.sections
[i
].base_address
, buf_cnt
, &mem_checksum
);
2570 if (retval
!= ERROR_OK
)
2576 if (checksum
!= mem_checksum
)
2578 /* failed crc checksum, fall back to a binary compare */
2581 command_print(CMD_CTX
, "checksum mismatch - attempting binary compare");
2583 data
= (uint8_t*)malloc(buf_cnt
);
2585 /* Can we use 32bit word accesses? */
2587 int count
= buf_cnt
;
2588 if ((count
% 4) == 0)
2593 retval
= target_read_memory(target
, image
.sections
[i
].base_address
, size
, count
, data
);
2594 if (retval
== ERROR_OK
)
2597 for (t
= 0; t
< buf_cnt
; t
++)
2599 if (data
[t
] != buffer
[t
])
2601 command_print(CMD_CTX
,
2602 "Verify operation failed address 0x%08x. Was 0x%02x instead of 0x%02x\n",
2603 (unsigned)(t
+ image
.sections
[i
].base_address
),
2608 retval
= ERROR_FAIL
;
2622 command_print(CMD_CTX
, "address 0x%08" PRIx32
" length 0x%08zx",
2623 image
.sections
[i
].base_address
,
2628 image_size
+= buf_cnt
;
2631 if ((ERROR_OK
== retval
) && (duration_measure(&bench
) == ERROR_OK
))
2633 command_print(CMD_CTX
, "verified %" PRIu32
" bytes "
2634 "in %fs (%0.3f kb/s)", image_size
,
2635 duration_elapsed(&bench
), duration_kbps(&bench
, image_size
));
2638 image_close(&image
);
2643 COMMAND_HANDLER(handle_verify_image_command
)
2645 return CALL_COMMAND_HANDLER(handle_verify_image_command_internal
, 1);
2648 COMMAND_HANDLER(handle_test_image_command
)
2650 return CALL_COMMAND_HANDLER(handle_verify_image_command_internal
, 0);
2653 static int handle_bp_command_list(struct command_context
*cmd_ctx
)
2655 struct target
*target
= get_current_target(cmd_ctx
);
2656 struct breakpoint
*breakpoint
= target
->breakpoints
;
2659 if (breakpoint
->type
== BKPT_SOFT
)
2661 char* buf
= buf_to_str(breakpoint
->orig_instr
,
2662 breakpoint
->length
, 16);
2663 command_print(cmd_ctx
, "0x%8.8" PRIx32
", 0x%x, %i, 0x%s",
2664 breakpoint
->address
,
2666 breakpoint
->set
, buf
);
2671 command_print(cmd_ctx
, "0x%8.8" PRIx32
", 0x%x, %i",
2672 breakpoint
->address
,
2673 breakpoint
->length
, breakpoint
->set
);
2676 breakpoint
= breakpoint
->next
;
2681 static int handle_bp_command_set(struct command_context
*cmd_ctx
,
2682 uint32_t addr
, uint32_t length
, int hw
)
2684 struct target
*target
= get_current_target(cmd_ctx
);
2685 int retval
= breakpoint_add(target
, addr
, length
, hw
);
2686 if (ERROR_OK
== retval
)
2687 command_print(cmd_ctx
, "breakpoint set at 0x%8.8" PRIx32
"", addr
);
2689 LOG_ERROR("Failure setting breakpoint");
2693 COMMAND_HANDLER(handle_bp_command
)
2696 return handle_bp_command_list(CMD_CTX
);
2698 if (CMD_ARGC
< 2 || CMD_ARGC
> 3)
2700 command_print(CMD_CTX
, "usage: bp <address> <length> ['hw']");
2701 return ERROR_COMMAND_SYNTAX_ERROR
;
2705 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], addr
);
2707 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], length
);
2712 if (strcmp(CMD_ARGV
[2], "hw") == 0)
2715 return ERROR_COMMAND_SYNTAX_ERROR
;
2718 return handle_bp_command_set(CMD_CTX
, addr
, length
, hw
);
2721 COMMAND_HANDLER(handle_rbp_command
)
2724 return ERROR_COMMAND_SYNTAX_ERROR
;
2727 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], addr
);
2729 struct target
*target
= get_current_target(CMD_CTX
);
2730 breakpoint_remove(target
, addr
);
2735 COMMAND_HANDLER(handle_wp_command
)
2737 struct target
*target
= get_current_target(CMD_CTX
);
2741 struct watchpoint
*watchpoint
= target
->watchpoints
;
2745 command_print(CMD_CTX
, "address: 0x%8.8" PRIx32
2746 ", len: 0x%8.8" PRIx32
2747 ", r/w/a: %i, value: 0x%8.8" PRIx32
2748 ", mask: 0x%8.8" PRIx32
,
2749 watchpoint
->address
,
2751 (int)watchpoint
->rw
,
2754 watchpoint
= watchpoint
->next
;
2759 enum watchpoint_rw type
= WPT_ACCESS
;
2761 uint32_t length
= 0;
2762 uint32_t data_value
= 0x0;
2763 uint32_t data_mask
= 0xffffffff;
2768 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[4], data_mask
);
2771 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[3], data_value
);
2774 switch (CMD_ARGV
[2][0])
2786 LOG_ERROR("invalid watchpoint mode ('%c')", CMD_ARGV
[2][0]);
2787 return ERROR_COMMAND_SYNTAX_ERROR
;
2791 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], length
);
2792 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], addr
);
2796 command_print(CMD_CTX
, "usage: wp [address length "
2797 "[(r|w|a) [value [mask]]]]");
2798 return ERROR_COMMAND_SYNTAX_ERROR
;
2801 int retval
= watchpoint_add(target
, addr
, length
, type
,
2802 data_value
, data_mask
);
2803 if (ERROR_OK
!= retval
)
2804 LOG_ERROR("Failure setting watchpoints");
2809 COMMAND_HANDLER(handle_rwp_command
)
2812 return ERROR_COMMAND_SYNTAX_ERROR
;
2815 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], addr
);
2817 struct target
*target
= get_current_target(CMD_CTX
);
2818 watchpoint_remove(target
, addr
);
2825 * Translate a virtual address to a physical address.
2827 * The low-level target implementation must have logged a detailed error
2828 * which is forwarded to telnet/GDB session.
2830 COMMAND_HANDLER(handle_virt2phys_command
)
2833 return ERROR_COMMAND_SYNTAX_ERROR
;
2836 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], va
);
2839 struct target
*target
= get_current_target(CMD_CTX
);
2840 int retval
= target
->type
->virt2phys(target
, va
, &pa
);
2841 if (retval
== ERROR_OK
)
2842 command_print(CMD_CTX
, "Physical address 0x%08" PRIx32
"", pa
);
2847 static void writeData(FILE *f
, const void *data
, size_t len
)
2849 size_t written
= fwrite(data
, 1, len
, f
);
2851 LOG_ERROR("failed to write %zu bytes: %s", len
, strerror(errno
));
2854 static void writeLong(FILE *f
, int l
)
2857 for (i
= 0; i
< 4; i
++)
2859 char c
= (l
>> (i
*8))&0xff;
2860 writeData(f
, &c
, 1);
2865 static void writeString(FILE *f
, char *s
)
2867 writeData(f
, s
, strlen(s
));
2870 /* Dump a gmon.out histogram file. */
2871 static void writeGmon(uint32_t *samples
, uint32_t sampleNum
, const char *filename
)
2874 FILE *f
= fopen(filename
, "w");
2877 writeString(f
, "gmon");
2878 writeLong(f
, 0x00000001); /* Version */
2879 writeLong(f
, 0); /* padding */
2880 writeLong(f
, 0); /* padding */
2881 writeLong(f
, 0); /* padding */
2883 uint8_t zero
= 0; /* GMON_TAG_TIME_HIST */
2884 writeData(f
, &zero
, 1);
2886 /* figure out bucket size */
2887 uint32_t min
= samples
[0];
2888 uint32_t max
= samples
[0];
2889 for (i
= 0; i
< sampleNum
; i
++)
2891 if (min
> samples
[i
])
2895 if (max
< samples
[i
])
2901 int addressSpace
= (max
-min
+ 1);
2903 static const uint32_t maxBuckets
= 256 * 1024; /* maximum buckets. */
2904 uint32_t length
= addressSpace
;
2905 if (length
> maxBuckets
)
2907 length
= maxBuckets
;
2909 int *buckets
= malloc(sizeof(int)*length
);
2910 if (buckets
== NULL
)
2915 memset(buckets
, 0, sizeof(int)*length
);
2916 for (i
= 0; i
< sampleNum
;i
++)
2918 uint32_t address
= samples
[i
];
2919 long long a
= address
-min
;
2920 long long b
= length
-1;
2921 long long c
= addressSpace
-1;
2922 int index
= (a
*b
)/c
; /* danger!!!! int32 overflows */
2926 /* append binary memory gmon.out &profile_hist_hdr ((char*)&profile_hist_hdr + sizeof(struct gmon_hist_hdr)) */
2927 writeLong(f
, min
); /* low_pc */
2928 writeLong(f
, max
); /* high_pc */
2929 writeLong(f
, length
); /* # of samples */
2930 writeLong(f
, 64000000); /* 64MHz */
2931 writeString(f
, "seconds");
2932 for (i
= 0; i
< (15-strlen("seconds")); i
++)
2933 writeData(f
, &zero
, 1);
2934 writeString(f
, "s");
2936 /*append binary memory gmon.out profile_hist_data (profile_hist_data + profile_hist_hdr.hist_size) */
2938 char *data
= malloc(2*length
);
2941 for (i
= 0; i
< length
;i
++)
2950 data
[i
*2 + 1]=(val
>> 8)&0xff;
2953 writeData(f
, data
, length
* 2);
2963 /* profiling samples the CPU PC as quickly as OpenOCD is able, which will be used as a random sampling of PC */
2964 COMMAND_HANDLER(handle_profile_command
)
2966 struct target
*target
= get_current_target(CMD_CTX
);
2967 struct timeval timeout
, now
;
2969 gettimeofday(&timeout
, NULL
);
2972 return ERROR_COMMAND_SYNTAX_ERROR
;
2975 COMMAND_PARSE_NUMBER(uint
, CMD_ARGV
[0], offset
);
2977 timeval_add_time(&timeout
, offset
, 0);
2979 command_print(CMD_CTX
, "Starting profiling. Halting and resuming the target as often as we can...");
2981 static const int maxSample
= 10000;
2982 uint32_t *samples
= malloc(sizeof(uint32_t)*maxSample
);
2983 if (samples
== NULL
)
2987 /* hopefully it is safe to cache! We want to stop/restart as quickly as possible. */
2988 struct reg
*reg
= register_get_by_name(target
->reg_cache
, "pc", 1);
2993 target_poll(target
);
2994 if (target
->state
== TARGET_HALTED
)
2996 uint32_t t
=*((uint32_t *)reg
->value
);
2997 samples
[numSamples
++]=t
;
2998 retval
= target_resume(target
, 1, 0, 0, 0); /* current pc, addr = 0, do not handle breakpoints, not debugging */
2999 target_poll(target
);
3000 alive_sleep(10); /* sleep 10ms, i.e. <100 samples/second. */
3001 } else if (target
->state
== TARGET_RUNNING
)
3003 /* We want to quickly sample the PC. */
3004 if ((retval
= target_halt(target
)) != ERROR_OK
)
3011 command_print(CMD_CTX
, "Target not halted or running");
3015 if (retval
!= ERROR_OK
)
3020 gettimeofday(&now
, NULL
);
3021 if ((numSamples
>= maxSample
) || ((now
.tv_sec
>= timeout
.tv_sec
) && (now
.tv_usec
>= timeout
.tv_usec
)))
3023 command_print(CMD_CTX
, "Profiling completed. %d samples.", numSamples
);
3024 if ((retval
= target_poll(target
)) != ERROR_OK
)
3029 if (target
->state
== TARGET_HALTED
)
3031 target_resume(target
, 1, 0, 0, 0); /* current pc, addr = 0, do not handle breakpoints, not debugging */
3033 if ((retval
= target_poll(target
)) != ERROR_OK
)
3038 writeGmon(samples
, numSamples
, CMD_ARGV
[1]);
3039 command_print(CMD_CTX
, "Wrote %s", CMD_ARGV
[1]);
3048 static int new_int_array_element(Jim_Interp
* interp
, const char *varname
, int idx
, uint32_t val
)
3051 Jim_Obj
*nameObjPtr
, *valObjPtr
;
3054 namebuf
= alloc_printf("%s(%d)", varname
, idx
);
3058 nameObjPtr
= Jim_NewStringObj(interp
, namebuf
, -1);
3059 valObjPtr
= Jim_NewIntObj(interp
, val
);
3060 if (!nameObjPtr
|| !valObjPtr
)
3066 Jim_IncrRefCount(nameObjPtr
);
3067 Jim_IncrRefCount(valObjPtr
);
3068 result
= Jim_SetVariable(interp
, nameObjPtr
, valObjPtr
);
3069 Jim_DecrRefCount(interp
, nameObjPtr
);
3070 Jim_DecrRefCount(interp
, valObjPtr
);
3072 /* printf("%s(%d) <= 0%08x\n", varname, idx, val); */
3076 static int jim_mem2array(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
3078 struct command_context
*context
;
3079 struct target
*target
;
3081 context
= Jim_GetAssocData(interp
, "context");
3082 if (context
== NULL
)
3084 LOG_ERROR("mem2array: no command context");
3087 target
= get_current_target(context
);
3090 LOG_ERROR("mem2array: no current target");
3094 return target_mem2array(interp
, target
, argc
-1, argv
+ 1);
3097 static int target_mem2array(Jim_Interp
*interp
, struct target
*target
, int argc
, Jim_Obj
*const *argv
)
3105 const char *varname
;
3109 /* argv[1] = name of array to receive the data
3110 * argv[2] = desired width
3111 * argv[3] = memory address
3112 * argv[4] = count of times to read
3115 Jim_WrongNumArgs(interp
, 1, argv
, "varname width addr nelems");
3118 varname
= Jim_GetString(argv
[0], &len
);
3119 /* given "foo" get space for worse case "foo(%d)" .. add 20 */
3121 e
= Jim_GetLong(interp
, argv
[1], &l
);
3127 e
= Jim_GetLong(interp
, argv
[2], &l
);
3132 e
= Jim_GetLong(interp
, argv
[3], &l
);
3148 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3149 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "Invalid width param, must be 8/16/32", NULL
);
3153 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3154 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "mem2array: zero width read?", NULL
);
3157 if ((addr
+ (len
* width
)) < addr
) {
3158 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3159 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "mem2array: addr + len - wraps to zero?", NULL
);
3162 /* absurd transfer size? */
3164 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3165 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "mem2array: absurd > 64K item request", NULL
);
3170 ((width
== 2) && ((addr
& 1) == 0)) ||
3171 ((width
== 4) && ((addr
& 3) == 0))) {
3175 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3176 sprintf(buf
, "mem2array address: 0x%08" PRIx32
" is not aligned for %" PRId32
" byte reads",
3179 Jim_AppendStrings(interp
, Jim_GetResult(interp
), buf
, NULL
);
3188 size_t buffersize
= 4096;
3189 uint8_t *buffer
= malloc(buffersize
);
3196 /* Slurp... in buffer size chunks */
3198 count
= len
; /* in objects.. */
3199 if (count
> (buffersize
/width
)) {
3200 count
= (buffersize
/width
);
3203 retval
= target_read_memory(target
, addr
, width
, count
, buffer
);
3204 if (retval
!= ERROR_OK
) {
3206 LOG_ERROR("mem2array: Read @ 0x%08x, w=%d, cnt=%d, failed",
3210 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3211 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "mem2array: cannot read memory", NULL
);
3215 v
= 0; /* shut up gcc */
3216 for (i
= 0 ;i
< count
;i
++, n
++) {
3219 v
= target_buffer_get_u32(target
, &buffer
[i
*width
]);
3222 v
= target_buffer_get_u16(target
, &buffer
[i
*width
]);
3225 v
= buffer
[i
] & 0x0ff;
3228 new_int_array_element(interp
, varname
, n
, v
);
3236 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3241 static int get_int_array_element(Jim_Interp
* interp
, const char *varname
, int idx
, uint32_t *val
)
3244 Jim_Obj
*nameObjPtr
, *valObjPtr
;
3248 namebuf
= alloc_printf("%s(%d)", varname
, idx
);
3252 nameObjPtr
= Jim_NewStringObj(interp
, namebuf
, -1);
3259 Jim_IncrRefCount(nameObjPtr
);
3260 valObjPtr
= Jim_GetVariable(interp
, nameObjPtr
, JIM_ERRMSG
);
3261 Jim_DecrRefCount(interp
, nameObjPtr
);
3263 if (valObjPtr
== NULL
)
3266 result
= Jim_GetLong(interp
, valObjPtr
, &l
);
3267 /* printf("%s(%d) => 0%08x\n", varname, idx, val); */
3272 static int jim_array2mem(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
3274 struct command_context
*context
;
3275 struct target
*target
;
3277 context
= Jim_GetAssocData(interp
, "context");
3278 if (context
== NULL
) {
3279 LOG_ERROR("array2mem: no command context");
3282 target
= get_current_target(context
);
3283 if (target
== NULL
) {
3284 LOG_ERROR("array2mem: no current target");
3288 return target_array2mem(interp
,target
, argc
-1, argv
+ 1);
3290 static int target_array2mem(Jim_Interp
*interp
, struct target
*target
, int argc
, Jim_Obj
*const *argv
)
3298 const char *varname
;
3302 /* argv[1] = name of array to get the data
3303 * argv[2] = desired width
3304 * argv[3] = memory address
3305 * argv[4] = count to write
3308 Jim_WrongNumArgs(interp
, 0, argv
, "varname width addr nelems");
3311 varname
= Jim_GetString(argv
[0], &len
);
3312 /* given "foo" get space for worse case "foo(%d)" .. add 20 */
3314 e
= Jim_GetLong(interp
, argv
[1], &l
);
3320 e
= Jim_GetLong(interp
, argv
[2], &l
);
3325 e
= Jim_GetLong(interp
, argv
[3], &l
);
3341 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3342 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "Invalid width param, must be 8/16/32", NULL
);
3346 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3347 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "array2mem: zero width read?", NULL
);
3350 if ((addr
+ (len
* width
)) < addr
) {
3351 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3352 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "array2mem: addr + len - wraps to zero?", NULL
);
3355 /* absurd transfer size? */
3357 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3358 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "array2mem: absurd > 64K item request", NULL
);
3363 ((width
== 2) && ((addr
& 1) == 0)) ||
3364 ((width
== 4) && ((addr
& 3) == 0))) {
3368 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3369 sprintf(buf
, "array2mem address: 0x%08x is not aligned for %d byte reads",
3372 Jim_AppendStrings(interp
, Jim_GetResult(interp
), buf
, NULL
);
3383 size_t buffersize
= 4096;
3384 uint8_t *buffer
= malloc(buffersize
);
3389 /* Slurp... in buffer size chunks */
3391 count
= len
; /* in objects.. */
3392 if (count
> (buffersize
/width
)) {
3393 count
= (buffersize
/width
);
3396 v
= 0; /* shut up gcc */
3397 for (i
= 0 ;i
< count
;i
++, n
++) {
3398 get_int_array_element(interp
, varname
, n
, &v
);
3401 target_buffer_set_u32(target
, &buffer
[i
*width
], v
);
3404 target_buffer_set_u16(target
, &buffer
[i
*width
], v
);
3407 buffer
[i
] = v
& 0x0ff;
3413 retval
= target_write_memory(target
, addr
, width
, count
, buffer
);
3414 if (retval
!= ERROR_OK
) {
3416 LOG_ERROR("array2mem: Write @ 0x%08x, w=%d, cnt=%d, failed",
3420 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3421 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "array2mem: cannot read memory", NULL
);
3429 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3434 void target_all_handle_event(enum target_event e
)
3436 struct target
*target
;
3438 LOG_DEBUG("**all*targets: event: %d, %s",
3440 Jim_Nvp_value2name_simple(nvp_target_event
, e
)->name
);
3442 target
= all_targets
;
3444 target_handle_event(target
, e
);
3445 target
= target
->next
;
3450 /* FIX? should we propagate errors here rather than printing them
3453 void target_handle_event(struct target
*target
, enum target_event e
)
3455 struct target_event_action
*teap
;
3457 for (teap
= target
->event_action
; teap
!= NULL
; teap
= teap
->next
) {
3458 if (teap
->event
== e
) {
3459 LOG_DEBUG("target: (%d) %s (%s) event: %d (%s) action: %s",
3460 target
->target_number
,
3461 target_name(target
),
3462 target_type_name(target
),
3464 Jim_Nvp_value2name_simple(nvp_target_event
, e
)->name
,
3465 Jim_GetString(teap
->body
, NULL
));
3466 if (Jim_EvalObj(teap
->interp
, teap
->body
) != JIM_OK
)
3468 Jim_PrintErrorMessage(teap
->interp
);
3475 * Returns true only if the target has a handler for the specified event.
3477 bool target_has_event_action(struct target
*target
, enum target_event event
)
3479 struct target_event_action
*teap
;
3481 for (teap
= target
->event_action
; teap
!= NULL
; teap
= teap
->next
) {
3482 if (teap
->event
== event
)
3488 enum target_cfg_param
{
3491 TCFG_WORK_AREA_VIRT
,
3492 TCFG_WORK_AREA_PHYS
,
3493 TCFG_WORK_AREA_SIZE
,
3494 TCFG_WORK_AREA_BACKUP
,
3497 TCFG_CHAIN_POSITION
,
3500 static Jim_Nvp nvp_config_opts
[] = {
3501 { .name
= "-type", .value
= TCFG_TYPE
},
3502 { .name
= "-event", .value
= TCFG_EVENT
},
3503 { .name
= "-work-area-virt", .value
= TCFG_WORK_AREA_VIRT
},
3504 { .name
= "-work-area-phys", .value
= TCFG_WORK_AREA_PHYS
},
3505 { .name
= "-work-area-size", .value
= TCFG_WORK_AREA_SIZE
},
3506 { .name
= "-work-area-backup", .value
= TCFG_WORK_AREA_BACKUP
},
3507 { .name
= "-endian" , .value
= TCFG_ENDIAN
},
3508 { .name
= "-variant", .value
= TCFG_VARIANT
},
3509 { .name
= "-chain-position", .value
= TCFG_CHAIN_POSITION
},
3511 { .name
= NULL
, .value
= -1 }
3514 static int target_configure(Jim_GetOptInfo
*goi
, struct target
*target
)
3522 /* parse config or cget options ... */
3523 while (goi
->argc
> 0) {
3524 Jim_SetEmptyResult(goi
->interp
);
3525 /* Jim_GetOpt_Debug(goi); */
3527 if (target
->type
->target_jim_configure
) {
3528 /* target defines a configure function */
3529 /* target gets first dibs on parameters */
3530 e
= (*(target
->type
->target_jim_configure
))(target
, goi
);
3539 /* otherwise we 'continue' below */
3541 e
= Jim_GetOpt_Nvp(goi
, nvp_config_opts
, &n
);
3543 Jim_GetOpt_NvpUnknown(goi
, nvp_config_opts
, 0);
3549 if (goi
->isconfigure
) {
3550 Jim_SetResult_sprintf(goi
->interp
,
3551 "not settable: %s", n
->name
);
3555 if (goi
->argc
!= 0) {
3556 Jim_WrongNumArgs(goi
->interp
,
3557 goi
->argc
, goi
->argv
,
3562 Jim_SetResultString(goi
->interp
,
3563 target_type_name(target
), -1);
3567 if (goi
->argc
== 0) {
3568 Jim_WrongNumArgs(goi
->interp
, goi
->argc
, goi
->argv
, "-event ?event-name? ...");
3572 e
= Jim_GetOpt_Nvp(goi
, nvp_target_event
, &n
);
3574 Jim_GetOpt_NvpUnknown(goi
, nvp_target_event
, 1);
3578 if (goi
->isconfigure
) {
3579 if (goi
->argc
!= 1) {
3580 Jim_WrongNumArgs(goi
->interp
, goi
->argc
, goi
->argv
, "-event ?event-name? ?EVENT-BODY?");
3584 if (goi
->argc
!= 0) {
3585 Jim_WrongNumArgs(goi
->interp
, goi
->argc
, goi
->argv
, "-event ?event-name?");
3591 struct target_event_action
*teap
;
3593 teap
= target
->event_action
;
3594 /* replace existing? */
3596 if (teap
->event
== (enum target_event
)n
->value
) {
3602 if (goi
->isconfigure
) {
3603 bool replace
= true;
3606 teap
= calloc(1, sizeof(*teap
));
3609 teap
->event
= n
->value
;
3610 teap
->interp
= goi
->interp
;
3611 Jim_GetOpt_Obj(goi
, &o
);
3613 Jim_DecrRefCount(teap
->interp
, teap
->body
);
3615 teap
->body
= Jim_DuplicateObj(goi
->interp
, o
);
3618 * Tcl/TK - "tk events" have a nice feature.
3619 * See the "BIND" command.
3620 * We should support that here.
3621 * You can specify %X and %Y in the event code.
3622 * The idea is: %T - target name.
3623 * The idea is: %N - target number
3624 * The idea is: %E - event name.
3626 Jim_IncrRefCount(teap
->body
);
3630 /* add to head of event list */
3631 teap
->next
= target
->event_action
;
3632 target
->event_action
= teap
;
3634 Jim_SetEmptyResult(goi
->interp
);
3638 Jim_SetEmptyResult(goi
->interp
);
3640 Jim_SetResult(goi
->interp
, Jim_DuplicateObj(goi
->interp
, teap
->body
));
3647 case TCFG_WORK_AREA_VIRT
:
3648 if (goi
->isconfigure
) {
3649 target_free_all_working_areas(target
);
3650 e
= Jim_GetOpt_Wide(goi
, &w
);
3654 target
->working_area_virt
= w
;
3655 target
->working_area_virt_spec
= true;
3657 if (goi
->argc
!= 0) {
3661 Jim_SetResult(goi
->interp
, Jim_NewIntObj(goi
->interp
, target
->working_area_virt
));
3665 case TCFG_WORK_AREA_PHYS
:
3666 if (goi
->isconfigure
) {
3667 target_free_all_working_areas(target
);
3668 e
= Jim_GetOpt_Wide(goi
, &w
);
3672 target
->working_area_phys
= w
;
3673 target
->working_area_phys_spec
= true;
3675 if (goi
->argc
!= 0) {
3679 Jim_SetResult(goi
->interp
, Jim_NewIntObj(goi
->interp
, target
->working_area_phys
));
3683 case TCFG_WORK_AREA_SIZE
:
3684 if (goi
->isconfigure
) {
3685 target_free_all_working_areas(target
);
3686 e
= Jim_GetOpt_Wide(goi
, &w
);
3690 target
->working_area_size
= w
;
3692 if (goi
->argc
!= 0) {
3696 Jim_SetResult(goi
->interp
, Jim_NewIntObj(goi
->interp
, target
->working_area_size
));
3700 case TCFG_WORK_AREA_BACKUP
:
3701 if (goi
->isconfigure
) {
3702 target_free_all_working_areas(target
);
3703 e
= Jim_GetOpt_Wide(goi
, &w
);
3707 /* make this exactly 1 or 0 */
3708 target
->backup_working_area
= (!!w
);
3710 if (goi
->argc
!= 0) {
3714 Jim_SetResult(goi
->interp
, Jim_NewIntObj(goi
->interp
, target
->backup_working_area
));
3715 /* loop for more e*/
3719 if (goi
->isconfigure
) {
3720 e
= Jim_GetOpt_Nvp(goi
, nvp_target_endian
, &n
);
3722 Jim_GetOpt_NvpUnknown(goi
, nvp_target_endian
, 1);
3725 target
->endianness
= n
->value
;
3727 if (goi
->argc
!= 0) {
3731 n
= Jim_Nvp_value2name_simple(nvp_target_endian
, target
->endianness
);
3732 if (n
->name
== NULL
) {
3733 target
->endianness
= TARGET_LITTLE_ENDIAN
;
3734 n
= Jim_Nvp_value2name_simple(nvp_target_endian
, target
->endianness
);
3736 Jim_SetResultString(goi
->interp
, n
->name
, -1);
3741 if (goi
->isconfigure
) {
3742 if (goi
->argc
< 1) {
3743 Jim_SetResult_sprintf(goi
->interp
,
3748 if (target
->variant
) {
3749 free((void *)(target
->variant
));
3751 e
= Jim_GetOpt_String(goi
, &cp
, NULL
);
3752 target
->variant
= strdup(cp
);
3754 if (goi
->argc
!= 0) {
3758 Jim_SetResultString(goi
->interp
, target
->variant
,-1);
3761 case TCFG_CHAIN_POSITION
:
3762 if (goi
->isconfigure
) {
3764 struct jtag_tap
*tap
;
3765 target_free_all_working_areas(target
);
3766 e
= Jim_GetOpt_Obj(goi
, &o
);
3770 tap
= jtag_tap_by_jim_obj(goi
->interp
, o
);
3774 /* make this exactly 1 or 0 */
3777 if (goi
->argc
!= 0) {
3781 Jim_SetResultString(goi
->interp
, target
->tap
->dotted_name
, -1);
3782 /* loop for more e*/
3785 } /* while (goi->argc) */
3788 /* done - we return */
3792 static int jim_target_configure(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
3795 Jim_GetOpt_Setup(&goi
, interp
, argc
- 1, argv
+ 1);
3796 goi
.isconfigure
= strcmp(Jim_GetString(argv
[0], NULL
), "configure") == 0;
3797 int need_args
= 1 + goi
.isconfigure
;
3798 if (goi
.argc
< need_args
)
3800 Jim_WrongNumArgs(goi
.interp
, goi
.argc
, goi
.argv
,
3802 ? "missing: -option VALUE ..."
3803 : "missing: -option ...");
3806 struct target
*target
= Jim_CmdPrivData(goi
.interp
);
3807 return target_configure(&goi
, target
);
3810 static int jim_target_mw(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
3812 const char *cmd_name
= Jim_GetString(argv
[0], NULL
);
3815 Jim_GetOpt_Setup(&goi
, interp
, argc
- 1, argv
+ 1);
3817 if (goi
.argc
!= 2 && goi
.argc
!= 3)
3819 Jim_SetResult_sprintf(goi
.interp
,
3820 "usage: %s <address> <data> [<count>]", cmd_name
);
3825 int e
= Jim_GetOpt_Wide(&goi
, &a
);
3830 e
= Jim_GetOpt_Wide(&goi
, &b
);
3837 e
= Jim_GetOpt_Wide(&goi
, &c
);
3842 struct target
*target
= Jim_CmdPrivData(goi
.interp
);
3843 uint8_t target_buf
[32];
3844 if (strcasecmp(cmd_name
, "mww") == 0) {
3845 target_buffer_set_u32(target
, target_buf
, b
);
3848 else if (strcasecmp(cmd_name
, "mwh") == 0) {
3849 target_buffer_set_u16(target
, target_buf
, b
);
3852 else if (strcasecmp(cmd_name
, "mwb") == 0) {
3853 target_buffer_set_u8(target
, target_buf
, b
);
3856 LOG_ERROR("command '%s' unknown: ", cmd_name
);
3860 for (jim_wide x
= 0; x
< c
; x
++)
3862 e
= target_write_memory(target
, a
, b
, 1, target_buf
);
3865 Jim_SetResult_sprintf(interp
,
3866 "Error writing @ 0x%08x: %d\n", (int)(a
), e
);
3875 static int jim_target_md(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
3877 const char *cmd_name
= Jim_GetString(argv
[0], NULL
);
3880 Jim_GetOpt_Setup(&goi
, interp
, argc
- 1, argv
+ 1);
3882 if ((goi
.argc
== 2) || (goi
.argc
== 3))
3884 Jim_SetResult_sprintf(goi
.interp
,
3885 "usage: %s <address> [<count>]", cmd_name
);
3890 int e
= Jim_GetOpt_Wide(&goi
, &a
);
3896 e
= Jim_GetOpt_Wide(&goi
, &c
);
3903 jim_wide b
= 1; /* shut up gcc */
3904 if (strcasecmp(cmd_name
, "mdw") == 0)
3906 else if (strcasecmp(cmd_name
, "mdh") == 0)
3908 else if (strcasecmp(cmd_name
, "mdb") == 0)
3911 LOG_ERROR("command '%s' unknown: ", cmd_name
);
3915 /* convert count to "bytes" */
3918 struct target
*target
= Jim_CmdPrivData(goi
.interp
);
3919 uint8_t target_buf
[32];
3926 e
= target_read_memory(target
, a
, b
, y
/ b
, target_buf
);
3927 if (e
!= ERROR_OK
) {
3928 Jim_SetResult_sprintf(interp
, "error reading target @ 0x%08lx", (int)(a
));
3932 Jim_fprintf(interp
, interp
->cookie_stdout
, "0x%08x ", (int)(a
));
3935 for (x
= 0; x
< 16 && x
< y
; x
+= 4)
3937 z
= target_buffer_get_u32(target
, &(target_buf
[ x
* 4 ]));
3938 Jim_fprintf(interp
, interp
->cookie_stdout
, "%08x ", (int)(z
));
3940 for (; (x
< 16) ; x
+= 4) {
3941 Jim_fprintf(interp
, interp
->cookie_stdout
, " ");
3945 for (x
= 0; x
< 16 && x
< y
; x
+= 2)
3947 z
= target_buffer_get_u16(target
, &(target_buf
[ x
* 2 ]));
3948 Jim_fprintf(interp
, interp
->cookie_stdout
, "%04x ", (int)(z
));
3950 for (; (x
< 16) ; x
+= 2) {
3951 Jim_fprintf(interp
, interp
->cookie_stdout
, " ");
3956 for (x
= 0 ; (x
< 16) && (x
< y
) ; x
+= 1) {
3957 z
= target_buffer_get_u8(target
, &(target_buf
[ x
* 4 ]));
3958 Jim_fprintf(interp
, interp
->cookie_stdout
, "%02x ", (int)(z
));
3960 for (; (x
< 16) ; x
+= 1) {
3961 Jim_fprintf(interp
, interp
->cookie_stdout
, " ");
3965 /* ascii-ify the bytes */
3966 for (x
= 0 ; x
< y
; x
++) {
3967 if ((target_buf
[x
] >= 0x20) &&
3968 (target_buf
[x
] <= 0x7e)) {
3972 target_buf
[x
] = '.';
3977 target_buf
[x
] = ' ';
3982 /* print - with a newline */
3983 Jim_fprintf(interp
, interp
->cookie_stdout
, "%s\n", target_buf
);
3991 static int jim_target_mem2array(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
3993 struct target
*target
= Jim_CmdPrivData(interp
);
3994 return target_mem2array(interp
, target
, argc
- 1, argv
+ 1);
3997 static int jim_target_array2mem(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
3999 struct target
*target
= Jim_CmdPrivData(interp
);
4000 return target_array2mem(interp
, target
, argc
- 1, argv
+ 1);
4003 static int jim_target_tap_disabled(Jim_Interp
*interp
)
4005 Jim_SetResult_sprintf(interp
, "[TAP is disabled]");
4009 static int jim_target_examine(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4013 Jim_WrongNumArgs(interp
, 1, argv
, "[no parameters]");
4016 struct target
*target
= Jim_CmdPrivData(interp
);
4017 if (!target
->tap
->enabled
)
4018 return jim_target_tap_disabled(interp
);
4020 int e
= target
->type
->examine(target
);
4023 Jim_SetResult_sprintf(interp
, "examine-fails: %d", e
);
4029 static int jim_target_poll(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4033 Jim_WrongNumArgs(interp
, 1, argv
, "[no parameters]");
4036 struct target
*target
= Jim_CmdPrivData(interp
);
4037 if (!target
->tap
->enabled
)
4038 return jim_target_tap_disabled(interp
);
4041 if (!(target_was_examined(target
))) {
4042 e
= ERROR_TARGET_NOT_EXAMINED
;
4044 e
= target
->type
->poll(target
);
4048 Jim_SetResult_sprintf(interp
, "poll-fails: %d", e
);
4054 static int jim_target_reset(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4057 Jim_GetOpt_Setup(&goi
, interp
, argc
- 1, argv
+ 1);
4061 Jim_WrongNumArgs(interp
, 0, argv
,
4062 "([tT]|[fF]|assert|deassert) BOOL");
4067 int e
= Jim_GetOpt_Nvp(&goi
, nvp_assert
, &n
);
4070 Jim_GetOpt_NvpUnknown(&goi
, nvp_assert
, 1);
4073 /* the halt or not param */
4075 e
= Jim_GetOpt_Wide(&goi
, &a
);
4079 struct target
*target
= Jim_CmdPrivData(goi
.interp
);
4080 if (!target
->tap
->enabled
)
4081 return jim_target_tap_disabled(interp
);
4082 if (!target
->type
->assert_reset
|| !target
->type
->deassert_reset
)
4084 Jim_SetResult_sprintf(interp
,
4085 "No target-specific reset for %s",
4086 target_name(target
));
4089 /* determine if we should halt or not. */
4090 target
->reset_halt
= !!a
;
4091 /* When this happens - all workareas are invalid. */
4092 target_free_all_working_areas_restore(target
, 0);
4095 if (n
->value
== NVP_ASSERT
) {
4096 e
= target
->type
->assert_reset(target
);
4098 e
= target
->type
->deassert_reset(target
);
4100 return (e
== ERROR_OK
) ? JIM_OK
: JIM_ERR
;
4103 static int jim_target_halt(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4106 Jim_WrongNumArgs(interp
, 1, argv
, "[no parameters]");
4109 struct target
*target
= Jim_CmdPrivData(interp
);
4110 if (!target
->tap
->enabled
)
4111 return jim_target_tap_disabled(interp
);
4112 int e
= target
->type
->halt(target
);
4113 return (e
== ERROR_OK
) ? JIM_OK
: JIM_ERR
;
4116 static int jim_target_wait_state(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4119 Jim_GetOpt_Setup(&goi
, interp
, argc
- 1, argv
+ 1);
4121 /* params: <name> statename timeoutmsecs */
4124 const char *cmd_name
= Jim_GetString(argv
[0], NULL
);
4125 Jim_SetResult_sprintf(goi
.interp
,
4126 "%s <state_name> <timeout_in_msec>", cmd_name
);
4131 int e
= Jim_GetOpt_Nvp(&goi
, nvp_target_state
, &n
);
4133 Jim_GetOpt_NvpUnknown(&goi
, nvp_target_state
,1);
4137 e
= Jim_GetOpt_Wide(&goi
, &a
);
4141 struct target
*target
= Jim_CmdPrivData(interp
);
4142 if (!target
->tap
->enabled
)
4143 return jim_target_tap_disabled(interp
);
4145 e
= target_wait_state(target
, n
->value
, a
);
4148 Jim_SetResult_sprintf(goi
.interp
,
4149 "target: %s wait %s fails (%d) %s",
4150 target_name(target
), n
->name
,
4151 e
, target_strerror_safe(e
));
4156 /* List for human, Events defined for this target.
4157 * scripts/programs should use 'name cget -event NAME'
4159 static int jim_target_event_list(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4161 struct command_context
*cmd_ctx
= Jim_GetAssocData(interp
, "context");
4162 struct target
*target
= Jim_CmdPrivData(interp
);
4163 struct target_event_action
*teap
= target
->event_action
;
4164 command_print(cmd_ctx
, "Event actions for target (%d) %s\n",
4165 target
->target_number
,
4166 target_name(target
));
4167 command_print(cmd_ctx
, "%-25s | Body", "Event");
4168 command_print(cmd_ctx
, "------------------------- | "
4169 "----------------------------------------");
4172 Jim_Nvp
*opt
= Jim_Nvp_value2name_simple(nvp_target_event
, teap
->event
);
4173 command_print(cmd_ctx
, "%-25s | %s",
4174 opt
->name
, Jim_GetString(teap
->body
, NULL
));
4177 command_print(cmd_ctx
, "***END***");
4180 static int jim_target_current_state(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4184 Jim_WrongNumArgs(interp
, 1, argv
, "[no parameters]");
4187 struct target
*target
= Jim_CmdPrivData(interp
);
4188 Jim_SetResultString(interp
, target_state_name(target
), -1);
4191 static int jim_target_invoke_event(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4194 Jim_GetOpt_Setup(&goi
, interp
, argc
- 1, argv
+ 1);
4197 const char *cmd_name
= Jim_GetString(argv
[0], NULL
);
4198 Jim_SetResult_sprintf(goi
.interp
, "%s <eventname>", cmd_name
);
4202 int e
= Jim_GetOpt_Nvp(&goi
, nvp_target_event
, &n
);
4205 Jim_GetOpt_NvpUnknown(&goi
, nvp_target_event
, 1);
4208 struct target
*target
= Jim_CmdPrivData(interp
);
4209 target_handle_event(target
, n
->value
);
4213 static const struct command_registration target_instance_command_handlers
[] = {
4215 .name
= "configure",
4216 .mode
= COMMAND_CONFIG
,
4217 .jim_handler
= &jim_target_configure
,
4218 .usage
= "[<target_options> ...]",
4219 .help
= "configure a new target for use",
4223 .mode
= COMMAND_ANY
,
4224 .jim_handler
= &jim_target_configure
,
4225 .usage
= "<target_type> [<target_options> ...]",
4226 .help
= "configure a new target for use",
4230 .mode
= COMMAND_EXEC
,
4231 .jim_handler
= &jim_target_mw
,
4232 .usage
= "<address> <data> [<count>]",
4233 .help
= "Write 32-bit word(s) to target memory",
4237 .mode
= COMMAND_EXEC
,
4238 .jim_handler
= &jim_target_mw
,
4239 .usage
= "<address> <data> [<count>]",
4240 .help
= "Write 16-bit half-word(s) to target memory",
4244 .mode
= COMMAND_EXEC
,
4245 .jim_handler
= &jim_target_mw
,
4246 .usage
= "<address> <data> [<count>]",
4247 .help
= "Write byte(s) to target memory",
4251 .mode
= COMMAND_EXEC
,
4252 .jim_handler
= &jim_target_md
,
4253 .usage
= "<address> [<count>]",
4254 .help
= "Display target memory as 32-bit words",
4258 .mode
= COMMAND_EXEC
,
4259 .jim_handler
= &jim_target_md
,
4260 .usage
= "<address> [<count>]",
4261 .help
= "Display target memory as 16-bit half-words",
4265 .mode
= COMMAND_EXEC
,
4266 .jim_handler
= &jim_target_md
,
4267 .usage
= "<address> [<count>]",
4268 .help
= "Display target memory as 8-bit bytes",
4271 .name
= "array2mem",
4272 .mode
= COMMAND_EXEC
,
4273 .jim_handler
= &jim_target_array2mem
,
4276 .name
= "mem2array",
4277 .mode
= COMMAND_EXEC
,
4278 .jim_handler
= &jim_target_mem2array
,
4281 .name
= "eventlist",
4282 .mode
= COMMAND_EXEC
,
4283 .jim_handler
= &jim_target_event_list
,
4287 .mode
= COMMAND_EXEC
,
4288 .jim_handler
= &jim_target_current_state
,
4291 .name
= "arp_examine",
4292 .mode
= COMMAND_EXEC
,
4293 .jim_handler
= &jim_target_examine
,
4297 .mode
= COMMAND_EXEC
,
4298 .jim_handler
= &jim_target_poll
,
4301 .name
= "arp_reset",
4302 .mode
= COMMAND_EXEC
,
4303 .jim_handler
= &jim_target_reset
,
4307 .mode
= COMMAND_EXEC
,
4308 .jim_handler
= &jim_target_halt
,
4311 .name
= "arp_waitstate",
4312 .mode
= COMMAND_EXEC
,
4313 .jim_handler
= &jim_target_wait_state
,
4316 .name
= "invoke-event",
4317 .mode
= COMMAND_EXEC
,
4318 .jim_handler
= &jim_target_invoke_event
,
4320 COMMAND_REGISTRATION_DONE
4323 static int target_create(Jim_GetOptInfo
*goi
)
4331 struct target
*target
;
4332 struct command_context
*cmd_ctx
;
4334 cmd_ctx
= Jim_GetAssocData(goi
->interp
, "context");
4335 if (goi
->argc
< 3) {
4336 Jim_WrongNumArgs(goi
->interp
, 1, goi
->argv
, "?name? ?type? ..options...");
4341 Jim_GetOpt_Obj(goi
, &new_cmd
);
4342 /* does this command exist? */
4343 cmd
= Jim_GetCommand(goi
->interp
, new_cmd
, JIM_ERRMSG
);
4345 cp
= Jim_GetString(new_cmd
, NULL
);
4346 Jim_SetResult_sprintf(goi
->interp
, "Command/target: %s Exists", cp
);
4351 e
= Jim_GetOpt_String(goi
, &cp2
, NULL
);
4353 /* now does target type exist */
4354 for (x
= 0 ; target_types
[x
] ; x
++) {
4355 if (0 == strcmp(cp
, target_types
[x
]->name
)) {
4360 if (target_types
[x
] == NULL
) {
4361 Jim_SetResult_sprintf(goi
->interp
, "Unknown target type %s, try one of ", cp
);
4362 for (x
= 0 ; target_types
[x
] ; x
++) {
4363 if (target_types
[x
+ 1]) {
4364 Jim_AppendStrings(goi
->interp
,
4365 Jim_GetResult(goi
->interp
),
4366 target_types
[x
]->name
,
4369 Jim_AppendStrings(goi
->interp
,
4370 Jim_GetResult(goi
->interp
),
4372 target_types
[x
]->name
,NULL
);
4379 target
= calloc(1,sizeof(struct target
));
4380 /* set target number */
4381 target
->target_number
= new_target_number();
4383 /* allocate memory for each unique target type */
4384 target
->type
= (struct target_type
*)calloc(1,sizeof(struct target_type
));
4386 memcpy(target
->type
, target_types
[x
], sizeof(struct target_type
));
4388 /* will be set by "-endian" */
4389 target
->endianness
= TARGET_ENDIAN_UNKNOWN
;
4391 target
->working_area
= 0x0;
4392 target
->working_area_size
= 0x0;
4393 target
->working_areas
= NULL
;
4394 target
->backup_working_area
= 0;
4396 target
->state
= TARGET_UNKNOWN
;
4397 target
->debug_reason
= DBG_REASON_UNDEFINED
;
4398 target
->reg_cache
= NULL
;
4399 target
->breakpoints
= NULL
;
4400 target
->watchpoints
= NULL
;
4401 target
->next
= NULL
;
4402 target
->arch_info
= NULL
;
4404 target
->display
= 1;
4406 target
->halt_issued
= false;
4408 /* initialize trace information */
4409 target
->trace_info
= malloc(sizeof(struct trace
));
4410 target
->trace_info
->num_trace_points
= 0;
4411 target
->trace_info
->trace_points_size
= 0;
4412 target
->trace_info
->trace_points
= NULL
;
4413 target
->trace_info
->trace_history_size
= 0;
4414 target
->trace_info
->trace_history
= NULL
;
4415 target
->trace_info
->trace_history_pos
= 0;
4416 target
->trace_info
->trace_history_overflowed
= 0;
4418 target
->dbgmsg
= NULL
;
4419 target
->dbg_msg_enabled
= 0;
4421 target
->endianness
= TARGET_ENDIAN_UNKNOWN
;
4423 /* Do the rest as "configure" options */
4424 goi
->isconfigure
= 1;
4425 e
= target_configure(goi
, target
);
4427 if (target
->tap
== NULL
)
4429 Jim_SetResultString(goi
->interp
, "-chain-position required when creating target", -1);
4439 if (target
->endianness
== TARGET_ENDIAN_UNKNOWN
) {
4440 /* default endian to little if not specified */
4441 target
->endianness
= TARGET_LITTLE_ENDIAN
;
4444 /* incase variant is not set */
4445 if (!target
->variant
)
4446 target
->variant
= strdup("");
4448 cp
= Jim_GetString(new_cmd
, NULL
);
4449 target
->cmd_name
= strdup(cp
);
4451 /* create the target specific commands */
4452 if (target
->type
->commands
) {
4453 e
= register_commands(cmd_ctx
, NULL
, target
->type
->commands
);
4455 LOG_ERROR("unable to register '%s' commands", cp
);
4457 if (target
->type
->target_create
) {
4458 (*(target
->type
->target_create
))(target
, goi
->interp
);
4461 /* append to end of list */
4463 struct target
**tpp
;
4464 tpp
= &(all_targets
);
4466 tpp
= &((*tpp
)->next
);
4471 /* now - create the new target name command */
4472 const const struct command_registration target_subcommands
[] = {
4474 .chain
= target_instance_command_handlers
,
4477 .chain
= target
->type
->commands
,
4479 COMMAND_REGISTRATION_DONE
4481 const const struct command_registration target_commands
[] = {
4484 .mode
= COMMAND_ANY
,
4485 .help
= "target command group",
4486 .chain
= target_subcommands
,
4488 COMMAND_REGISTRATION_DONE
4490 e
= register_commands(cmd_ctx
, NULL
, target_commands
);
4494 struct command
*c
= command_find_in_context(cmd_ctx
, cp
);
4496 command_set_handler_data(c
, target
);
4498 return (ERROR_OK
== e
) ? JIM_OK
: JIM_ERR
;
4501 static int jim_target_current(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4505 Jim_WrongNumArgs(interp
, 1, argv
, "Too many parameters");
4508 struct command_context
*cmd_ctx
= Jim_GetAssocData(interp
, "context");
4509 Jim_SetResultString(interp
, get_current_target(cmd_ctx
)->cmd_name
, -1);
4513 static int jim_target_types(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4517 Jim_WrongNumArgs(interp
, 1, argv
, "Too many parameters");
4520 Jim_SetResult(interp
, Jim_NewListObj(interp
, NULL
, 0));
4521 for (unsigned x
= 0; NULL
!= target_types
[x
]; x
++)
4523 Jim_ListAppendElement(interp
, Jim_GetResult(interp
),
4524 Jim_NewStringObj(interp
, target_types
[x
]->name
, -1));
4529 static int jim_target_names(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4533 Jim_WrongNumArgs(interp
, 1, argv
, "Too many parameters");
4536 Jim_SetResult(interp
, Jim_NewListObj(interp
, NULL
, 0));
4537 struct target
*target
= all_targets
;
4540 Jim_ListAppendElement(interp
, Jim_GetResult(interp
),
4541 Jim_NewStringObj(interp
, target_name(target
), -1));
4542 target
= target
->next
;
4547 static int jim_target_create(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4550 Jim_GetOpt_Setup(&goi
, interp
, argc
- 1, argv
+ 1);
4553 Jim_WrongNumArgs(goi
.interp
, goi
.argc
, goi
.argv
,
4554 "<name> <target_type> [<target_options> ...]");
4557 return target_create(&goi
);
4560 static int jim_target_number(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4563 Jim_GetOpt_Setup(&goi
, interp
, argc
- 1, argv
+ 1);
4565 /* It's OK to remove this mechanism sometime after August 2010 or so */
4566 LOG_WARNING("don't use numbers as target identifiers; use names");
4569 Jim_SetResult_sprintf(goi
.interp
, "usage: target number <number>");
4573 int e
= Jim_GetOpt_Wide(&goi
, &w
);
4577 struct target
*target
;
4578 for (target
= all_targets
; NULL
!= target
; target
= target
->next
)
4580 if (target
->target_number
!= w
)
4583 Jim_SetResultString(goi
.interp
, target_name(target
), -1);
4586 Jim_SetResult_sprintf(goi
.interp
,
4587 "Target: number %d does not exist", (int)(w
));
4591 static int jim_target_count(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4595 Jim_WrongNumArgs(interp
, 1, argv
, "<no parameters>");
4599 struct target
*target
= all_targets
;
4600 while (NULL
!= target
)
4602 target
= target
->next
;
4605 Jim_SetResult(interp
, Jim_NewIntObj(interp
, count
));
4609 static const struct command_registration target_subcommand_handlers
[] = {
4612 .mode
= COMMAND_CONFIG
,
4613 .handler
= &handle_target_init_command
,
4614 .help
= "initialize targets",
4618 .mode
= COMMAND_ANY
,
4619 .jim_handler
= &jim_target_create
,
4620 .usage
= "<name> <type> ...",
4621 .help
= "Returns the currently selected target",
4625 .mode
= COMMAND_ANY
,
4626 .jim_handler
= &jim_target_current
,
4627 .help
= "Returns the currently selected target",
4631 .mode
= COMMAND_ANY
,
4632 .jim_handler
= &jim_target_types
,
4633 .help
= "Returns the available target types as a list of strings",
4637 .mode
= COMMAND_ANY
,
4638 .jim_handler
= &jim_target_names
,
4639 .help
= "Returns the names of all targets as a list of strings",
4643 .mode
= COMMAND_ANY
,
4644 .jim_handler
= &jim_target_number
,
4645 .usage
= "<number>",
4646 .help
= "Returns the name of target <n>",
4650 .mode
= COMMAND_ANY
,
4651 .jim_handler
= &jim_target_count
,
4652 .help
= "Returns the number of targets as an integer",
4654 COMMAND_REGISTRATION_DONE
4666 static int fastload_num
;
4667 static struct FastLoad
*fastload
;
4669 static void free_fastload(void)
4671 if (fastload
!= NULL
)
4674 for (i
= 0; i
< fastload_num
; i
++)
4676 if (fastload
[i
].data
)
4677 free(fastload
[i
].data
);
4687 COMMAND_HANDLER(handle_fast_load_image_command
)
4691 uint32_t image_size
;
4692 uint32_t min_address
= 0;
4693 uint32_t max_address
= 0xffffffff;
4698 int retval
= CALL_COMMAND_HANDLER(parse_load_image_command_CMD_ARGV
,
4699 &image
, &min_address
, &max_address
);
4700 if (ERROR_OK
!= retval
)
4703 struct duration bench
;
4704 duration_start(&bench
);
4706 if (image_open(&image
, CMD_ARGV
[0], (CMD_ARGC
>= 3) ? CMD_ARGV
[2] : NULL
) != ERROR_OK
)
4713 fastload_num
= image
.num_sections
;
4714 fastload
= (struct FastLoad
*)malloc(sizeof(struct FastLoad
)*image
.num_sections
);
4715 if (fastload
== NULL
)
4717 image_close(&image
);
4720 memset(fastload
, 0, sizeof(struct FastLoad
)*image
.num_sections
);
4721 for (i
= 0; i
< image
.num_sections
; i
++)
4723 buffer
= malloc(image
.sections
[i
].size
);
4726 command_print(CMD_CTX
, "error allocating buffer for section (%d bytes)",
4727 (int)(image
.sections
[i
].size
));
4731 if ((retval
= image_read_section(&image
, i
, 0x0, image
.sections
[i
].size
, buffer
, &buf_cnt
)) != ERROR_OK
)
4737 uint32_t offset
= 0;
4738 uint32_t length
= buf_cnt
;
4741 /* DANGER!!! beware of unsigned comparision here!!! */
4743 if ((image
.sections
[i
].base_address
+ buf_cnt
>= min_address
)&&
4744 (image
.sections
[i
].base_address
< max_address
))
4746 if (image
.sections
[i
].base_address
< min_address
)
4748 /* clip addresses below */
4749 offset
+= min_address
-image
.sections
[i
].base_address
;
4753 if (image
.sections
[i
].base_address
+ buf_cnt
> max_address
)
4755 length
-= (image
.sections
[i
].base_address
+ buf_cnt
)-max_address
;
4758 fastload
[i
].address
= image
.sections
[i
].base_address
+ offset
;
4759 fastload
[i
].data
= malloc(length
);
4760 if (fastload
[i
].data
== NULL
)
4765 memcpy(fastload
[i
].data
, buffer
+ offset
, length
);
4766 fastload
[i
].length
= length
;
4768 image_size
+= length
;
4769 command_print(CMD_CTX
, "%u bytes written at address 0x%8.8x",
4770 (unsigned int)length
,
4771 ((unsigned int)(image
.sections
[i
].base_address
+ offset
)));
4777 if ((ERROR_OK
== retval
) && (duration_measure(&bench
) == ERROR_OK
))
4779 command_print(CMD_CTX
, "Loaded %" PRIu32
" bytes "
4780 "in %fs (%0.3f kb/s)", image_size
,
4781 duration_elapsed(&bench
), duration_kbps(&bench
, image_size
));
4783 command_print(CMD_CTX
,
4784 "WARNING: image has not been loaded to target!"
4785 "You can issue a 'fast_load' to finish loading.");
4788 image_close(&image
);
4790 if (retval
!= ERROR_OK
)
4798 COMMAND_HANDLER(handle_fast_load_command
)
4801 return ERROR_COMMAND_SYNTAX_ERROR
;
4802 if (fastload
== NULL
)
4804 LOG_ERROR("No image in memory");
4808 int ms
= timeval_ms();
4810 int retval
= ERROR_OK
;
4811 for (i
= 0; i
< fastload_num
;i
++)
4813 struct target
*target
= get_current_target(CMD_CTX
);
4814 command_print(CMD_CTX
, "Write to 0x%08x, length 0x%08x",
4815 (unsigned int)(fastload
[i
].address
),
4816 (unsigned int)(fastload
[i
].length
));
4817 if (retval
== ERROR_OK
)
4819 retval
= target_write_buffer(target
, fastload
[i
].address
, fastload
[i
].length
, fastload
[i
].data
);
4821 size
+= fastload
[i
].length
;
4823 int after
= timeval_ms();
4824 command_print(CMD_CTX
, "Loaded image %f kBytes/s", (float)(size
/1024.0)/((float)(after
-ms
)/1000.0));
4828 static const struct command_registration target_command_handlers
[] = {
4831 .handler
= &handle_targets_command
,
4832 .mode
= COMMAND_ANY
,
4833 .help
= "change current command line target (one parameter) "
4834 "or list targets (no parameters)",
4835 .usage
= "[<new_current_target>]",
4839 .mode
= COMMAND_CONFIG
,
4840 .help
= "configure target",
4842 .chain
= target_subcommand_handlers
,
4844 COMMAND_REGISTRATION_DONE
4847 int target_register_commands(struct command_context
*cmd_ctx
)
4849 return register_commands(cmd_ctx
, NULL
, target_command_handlers
);
4852 static const struct command_registration target_exec_command_handlers
[] = {
4854 .name
= "fast_load_image",
4855 .handler
= &handle_fast_load_image_command
,
4856 .mode
= COMMAND_ANY
,
4857 .help
= "Load image into memory, mainly for profiling purposes",
4858 .usage
= "<file> <address> ['bin'|'ihex'|'elf'|'s19'] "
4859 "[min_address] [max_length]",
4862 .name
= "fast_load",
4863 .handler
= &handle_fast_load_command
,
4864 .mode
= COMMAND_ANY
,
4865 .help
= "loads active fast load image to current target "
4866 "- mainly for profiling purposes",
4870 .handler
= &handle_profile_command
,
4871 .mode
= COMMAND_EXEC
,
4872 .help
= "profiling samples the CPU PC",
4874 /** @todo don't register virt2phys() unless target supports it */
4876 .name
= "virt2phys",
4877 .handler
= &handle_virt2phys_command
,
4878 .mode
= COMMAND_ANY
,
4879 .help
= "translate a virtual address into a physical address",
4884 .handler
= &handle_reg_command
,
4885 .mode
= COMMAND_EXEC
,
4886 .help
= "display or set a register",
4891 .handler
= &handle_poll_command
,
4892 .mode
= COMMAND_EXEC
,
4893 .help
= "poll target state",
4896 .name
= "wait_halt",
4897 .handler
= &handle_wait_halt_command
,
4898 .mode
= COMMAND_EXEC
,
4899 .help
= "wait for target halt",
4900 .usage
= "[time (s)]",
4904 .handler
= &handle_halt_command
,
4905 .mode
= COMMAND_EXEC
,
4906 .help
= "halt target",
4910 .handler
= &handle_resume_command
,
4911 .mode
= COMMAND_EXEC
,
4912 .help
= "resume target",
4913 .usage
= "[<address>]",
4917 .handler
= &handle_reset_command
,
4918 .mode
= COMMAND_EXEC
,
4919 .usage
= "[run|halt|init]",
4920 .help
= "Reset all targets into the specified mode."
4921 "Default reset mode is run, if not given.",
4924 .name
= "soft_reset_halt",
4925 .handler
= &handle_soft_reset_halt_command
,
4926 .mode
= COMMAND_EXEC
,
4927 .help
= "halt the target and do a soft reset",
4932 .handler
= &handle_step_command
,
4933 .mode
= COMMAND_EXEC
,
4934 .help
= "step one instruction from current PC or [addr]",
4935 .usage
= "[<address>]",
4940 .handler
= &handle_md_command
,
4941 .mode
= COMMAND_EXEC
,
4942 .help
= "display memory words",
4943 .usage
= "[phys] <addr> [count]",
4947 .handler
= &handle_md_command
,
4948 .mode
= COMMAND_EXEC
,
4949 .help
= "display memory half-words",
4950 .usage
= "[phys] <addr> [count]",
4954 .handler
= &handle_md_command
,
4955 .mode
= COMMAND_EXEC
,
4956 .help
= "display memory bytes",
4957 .usage
= "[phys] <addr> [count]",
4962 .handler
= &handle_mw_command
,
4963 .mode
= COMMAND_EXEC
,
4964 .help
= "write memory word",
4965 .usage
= "[phys] <addr> <value> [count]",
4969 .handler
= &handle_mw_command
,
4970 .mode
= COMMAND_EXEC
,
4971 .help
= "write memory half-word",
4972 .usage
= "[phys] <addr> <value> [count]",
4976 .handler
= &handle_mw_command
,
4977 .mode
= COMMAND_EXEC
,
4978 .help
= "write memory byte",
4979 .usage
= "[phys] <addr> <value> [count]",
4984 .handler
= &handle_bp_command
,
4985 .mode
= COMMAND_EXEC
,
4986 .help
= "list or set breakpoint",
4987 .usage
= "[<address> <length> [hw]]",
4991 .handler
= &handle_rbp_command
,
4992 .mode
= COMMAND_EXEC
,
4993 .help
= "remove breakpoint",
4994 .usage
= "<address>",
4999 .handler
= &handle_wp_command
,
5000 .mode
= COMMAND_EXEC
,
5001 .help
= "list or set watchpoint",
5002 .usage
= "[<address> <length> <r/w/a> [value] [mask]]",
5006 .handler
= &handle_rwp_command
,
5007 .mode
= COMMAND_EXEC
,
5008 .help
= "remove watchpoint",
5009 .usage
= "<address>",
5013 .name
= "load_image",
5014 .handler
= &handle_load_image_command
,
5015 .mode
= COMMAND_EXEC
,
5016 .usage
= "<file> <address> ['bin'|'ihex'|'elf'|'s19'] "
5017 "[min_address] [max_length]",
5020 .name
= "dump_image",
5021 .handler
= &handle_dump_image_command
,
5022 .mode
= COMMAND_EXEC
,
5023 .usage
= "<file> <address> <size>",
5026 .name
= "verify_image",
5027 .handler
= &handle_verify_image_command
,
5028 .mode
= COMMAND_EXEC
,
5029 .usage
= "<file> [offset] [type]",
5032 .name
= "test_image",
5033 .handler
= &handle_test_image_command
,
5034 .mode
= COMMAND_EXEC
,
5035 .usage
= "<file> [offset] [type]",
5038 .name
= "ocd_mem2array",
5039 .mode
= COMMAND_EXEC
,
5040 .jim_handler
= &jim_mem2array
,
5041 .help
= "read memory and return as a TCL array "
5042 "for script processing",
5043 .usage
= "<arrayname> <width=32|16|8> <address> <count>",
5046 .name
= "ocd_array2mem",
5047 .mode
= COMMAND_EXEC
,
5048 .jim_handler
= &jim_array2mem
,
5049 .help
= "convert a TCL array to memory locations "
5050 "and write the values",
5051 .usage
= "<arrayname> <width=32|16|8> <address> <count>",
5053 COMMAND_REGISTRATION_DONE
5055 int target_register_user_commands(struct command_context
*cmd_ctx
)
5057 int retval
= ERROR_OK
;
5058 if ((retval
= target_request_register_commands(cmd_ctx
)) != ERROR_OK
)
5061 if ((retval
= trace_register_commands(cmd_ctx
)) != ERROR_OK
)
5065 return register_commands(cmd_ctx
, NULL
, target_exec_command_handlers
);