1 /***************************************************************************
2 * Copyright (C) 2005 by Dominic Rath *
3 * Dominic.Rath@gmx.de *
5 * Copyright (C) 2007-2010 Ø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>
38 #include <flash/nor/core.h>
41 #include "target_type.h"
42 #include "target_request.h"
43 #include "breakpoints.h"
49 static int target_array2mem(Jim_Interp
*interp
, struct target
*target
,
50 int argc
, Jim_Obj
*const *argv
);
51 static int target_mem2array(Jim_Interp
*interp
, struct target
*target
,
52 int argc
, Jim_Obj
*const *argv
);
53 static int target_register_user_commands(struct command_context
*cmd_ctx
);
56 extern struct target_type arm7tdmi_target
;
57 extern struct target_type arm720t_target
;
58 extern struct target_type arm9tdmi_target
;
59 extern struct target_type arm920t_target
;
60 extern struct target_type arm966e_target
;
61 extern struct target_type arm926ejs_target
;
62 extern struct target_type fa526_target
;
63 extern struct target_type feroceon_target
;
64 extern struct target_type dragonite_target
;
65 extern struct target_type xscale_target
;
66 extern struct target_type cortexm3_target
;
67 extern struct target_type cortexa8_target
;
68 extern struct target_type arm11_target
;
69 extern struct target_type mips_m4k_target
;
70 extern struct target_type avr_target
;
71 extern struct target_type dsp563xx_target
;
72 extern struct target_type testee_target
;
74 static struct target_type
*target_types
[] =
96 struct target
*all_targets
= NULL
;
97 static struct target_event_callback
*target_event_callbacks
= NULL
;
98 static struct target_timer_callback
*target_timer_callbacks
= NULL
;
100 static const Jim_Nvp nvp_assert
[] = {
101 { .name
= "assert", NVP_ASSERT
},
102 { .name
= "deassert", NVP_DEASSERT
},
103 { .name
= "T", NVP_ASSERT
},
104 { .name
= "F", NVP_DEASSERT
},
105 { .name
= "t", NVP_ASSERT
},
106 { .name
= "f", NVP_DEASSERT
},
107 { .name
= NULL
, .value
= -1 }
110 static const Jim_Nvp nvp_error_target
[] = {
111 { .value
= ERROR_TARGET_INVALID
, .name
= "err-invalid" },
112 { .value
= ERROR_TARGET_INIT_FAILED
, .name
= "err-init-failed" },
113 { .value
= ERROR_TARGET_TIMEOUT
, .name
= "err-timeout" },
114 { .value
= ERROR_TARGET_NOT_HALTED
, .name
= "err-not-halted" },
115 { .value
= ERROR_TARGET_FAILURE
, .name
= "err-failure" },
116 { .value
= ERROR_TARGET_UNALIGNED_ACCESS
, .name
= "err-unaligned-access" },
117 { .value
= ERROR_TARGET_DATA_ABORT
, .name
= "err-data-abort" },
118 { .value
= ERROR_TARGET_RESOURCE_NOT_AVAILABLE
, .name
= "err-resource-not-available" },
119 { .value
= ERROR_TARGET_TRANSLATION_FAULT
, .name
= "err-translation-fault" },
120 { .value
= ERROR_TARGET_NOT_RUNNING
, .name
= "err-not-running" },
121 { .value
= ERROR_TARGET_NOT_EXAMINED
, .name
= "err-not-examined" },
122 { .value
= -1, .name
= NULL
}
125 static const char *target_strerror_safe(int err
)
129 n
= Jim_Nvp_value2name_simple(nvp_error_target
, err
);
130 if (n
->name
== NULL
) {
137 static const Jim_Nvp nvp_target_event
[] = {
138 { .value
= TARGET_EVENT_OLD_gdb_program_config
, .name
= "old-gdb_program_config" },
139 { .value
= TARGET_EVENT_OLD_pre_resume
, .name
= "old-pre_resume" },
141 { .value
= TARGET_EVENT_GDB_HALT
, .name
= "gdb-halt" },
142 { .value
= TARGET_EVENT_HALTED
, .name
= "halted" },
143 { .value
= TARGET_EVENT_RESUMED
, .name
= "resumed" },
144 { .value
= TARGET_EVENT_RESUME_START
, .name
= "resume-start" },
145 { .value
= TARGET_EVENT_RESUME_END
, .name
= "resume-end" },
147 { .name
= "gdb-start", .value
= TARGET_EVENT_GDB_START
},
148 { .name
= "gdb-end", .value
= TARGET_EVENT_GDB_END
},
150 /* historical name */
152 { .value
= TARGET_EVENT_RESET_START
, .name
= "reset-start" },
154 { .value
= TARGET_EVENT_RESET_ASSERT_PRE
, .name
= "reset-assert-pre" },
155 { .value
= TARGET_EVENT_RESET_ASSERT
, .name
= "reset-assert" },
156 { .value
= TARGET_EVENT_RESET_ASSERT_POST
, .name
= "reset-assert-post" },
157 { .value
= TARGET_EVENT_RESET_DEASSERT_PRE
, .name
= "reset-deassert-pre" },
158 { .value
= TARGET_EVENT_RESET_DEASSERT_POST
, .name
= "reset-deassert-post" },
159 { .value
= TARGET_EVENT_RESET_HALT_PRE
, .name
= "reset-halt-pre" },
160 { .value
= TARGET_EVENT_RESET_HALT_POST
, .name
= "reset-halt-post" },
161 { .value
= TARGET_EVENT_RESET_WAIT_PRE
, .name
= "reset-wait-pre" },
162 { .value
= TARGET_EVENT_RESET_WAIT_POST
, .name
= "reset-wait-post" },
163 { .value
= TARGET_EVENT_RESET_INIT
, .name
= "reset-init" },
164 { .value
= TARGET_EVENT_RESET_END
, .name
= "reset-end" },
166 { .value
= TARGET_EVENT_EXAMINE_START
, .name
= "examine-start" },
167 { .value
= TARGET_EVENT_EXAMINE_END
, .name
= "examine-end" },
169 { .value
= TARGET_EVENT_DEBUG_HALTED
, .name
= "debug-halted" },
170 { .value
= TARGET_EVENT_DEBUG_RESUMED
, .name
= "debug-resumed" },
172 { .value
= TARGET_EVENT_GDB_ATTACH
, .name
= "gdb-attach" },
173 { .value
= TARGET_EVENT_GDB_DETACH
, .name
= "gdb-detach" },
175 { .value
= TARGET_EVENT_GDB_FLASH_WRITE_START
, .name
= "gdb-flash-write-start" },
176 { .value
= TARGET_EVENT_GDB_FLASH_WRITE_END
, .name
= "gdb-flash-write-end" },
178 { .value
= TARGET_EVENT_GDB_FLASH_ERASE_START
, .name
= "gdb-flash-erase-start" },
179 { .value
= TARGET_EVENT_GDB_FLASH_ERASE_END
, .name
= "gdb-flash-erase-end" },
181 { .value
= TARGET_EVENT_RESUME_START
, .name
= "resume-start" },
182 { .value
= TARGET_EVENT_RESUMED
, .name
= "resume-ok" },
183 { .value
= TARGET_EVENT_RESUME_END
, .name
= "resume-end" },
185 { .name
= NULL
, .value
= -1 }
188 static const Jim_Nvp nvp_target_state
[] = {
189 { .name
= "unknown", .value
= TARGET_UNKNOWN
},
190 { .name
= "running", .value
= TARGET_RUNNING
},
191 { .name
= "halted", .value
= TARGET_HALTED
},
192 { .name
= "reset", .value
= TARGET_RESET
},
193 { .name
= "debug-running", .value
= TARGET_DEBUG_RUNNING
},
194 { .name
= NULL
, .value
= -1 },
197 static const Jim_Nvp nvp_target_debug_reason
[] = {
198 { .name
= "debug-request" , .value
= DBG_REASON_DBGRQ
},
199 { .name
= "breakpoint" , .value
= DBG_REASON_BREAKPOINT
},
200 { .name
= "watchpoint" , .value
= DBG_REASON_WATCHPOINT
},
201 { .name
= "watchpoint-and-breakpoint", .value
= DBG_REASON_WPTANDBKPT
},
202 { .name
= "single-step" , .value
= DBG_REASON_SINGLESTEP
},
203 { .name
= "target-not-halted" , .value
= DBG_REASON_NOTHALTED
},
204 { .name
= "undefined" , .value
= DBG_REASON_UNDEFINED
},
205 { .name
= NULL
, .value
= -1 },
208 static const Jim_Nvp nvp_target_endian
[] = {
209 { .name
= "big", .value
= TARGET_BIG_ENDIAN
},
210 { .name
= "little", .value
= TARGET_LITTLE_ENDIAN
},
211 { .name
= "be", .value
= TARGET_BIG_ENDIAN
},
212 { .name
= "le", .value
= TARGET_LITTLE_ENDIAN
},
213 { .name
= NULL
, .value
= -1 },
216 static const Jim_Nvp nvp_reset_modes
[] = {
217 { .name
= "unknown", .value
= RESET_UNKNOWN
},
218 { .name
= "run" , .value
= RESET_RUN
},
219 { .name
= "halt" , .value
= RESET_HALT
},
220 { .name
= "init" , .value
= RESET_INIT
},
221 { .name
= NULL
, .value
= -1 },
224 const char *debug_reason_name(struct target
*t
)
228 cp
= Jim_Nvp_value2name_simple(nvp_target_debug_reason
,
229 t
->debug_reason
)->name
;
231 LOG_ERROR("Invalid debug reason: %d", (int)(t
->debug_reason
));
232 cp
= "(*BUG*unknown*BUG*)";
238 target_state_name( struct target
*t
)
241 cp
= Jim_Nvp_value2name_simple(nvp_target_state
, t
->state
)->name
;
243 LOG_ERROR("Invalid target state: %d", (int)(t
->state
));
244 cp
= "(*BUG*unknown*BUG*)";
249 /* determine the number of the new target */
250 static int new_target_number(void)
255 /* number is 0 based */
259 if (x
< t
->target_number
) {
260 x
= t
->target_number
;
267 /* read a uint32_t from a buffer in target memory endianness */
268 uint32_t target_buffer_get_u32(struct target
*target
, const uint8_t *buffer
)
270 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
271 return le_to_h_u32(buffer
);
273 return be_to_h_u32(buffer
);
276 /* read a uint16_t from a buffer in target memory endianness */
277 uint16_t target_buffer_get_u16(struct target
*target
, const uint8_t *buffer
)
279 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
280 return le_to_h_u16(buffer
);
282 return be_to_h_u16(buffer
);
285 /* read a uint8_t from a buffer in target memory endianness */
286 static uint8_t target_buffer_get_u8(struct target
*target
, const uint8_t *buffer
)
288 return *buffer
& 0x0ff;
291 /* write a uint32_t to a buffer in target memory endianness */
292 void target_buffer_set_u32(struct target
*target
, uint8_t *buffer
, uint32_t value
)
294 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
295 h_u32_to_le(buffer
, value
);
297 h_u32_to_be(buffer
, value
);
300 /* write a uint16_t to a buffer in target memory endianness */
301 void target_buffer_set_u16(struct target
*target
, uint8_t *buffer
, uint16_t value
)
303 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
304 h_u16_to_le(buffer
, value
);
306 h_u16_to_be(buffer
, value
);
309 /* write a uint8_t to a buffer in target memory endianness */
310 static void target_buffer_set_u8(struct target
*target
, uint8_t *buffer
, uint8_t value
)
315 /* return a pointer to a configured target; id is name or number */
316 struct target
*get_target(const char *id
)
318 struct target
*target
;
320 /* try as tcltarget name */
321 for (target
= all_targets
; target
; target
= target
->next
) {
322 if (target
->cmd_name
== NULL
)
324 if (strcmp(id
, target
->cmd_name
) == 0)
328 /* It's OK to remove this fallback sometime after August 2010 or so */
330 /* no match, try as number */
332 if (parse_uint(id
, &num
) != ERROR_OK
)
335 for (target
= all_targets
; target
; target
= target
->next
) {
336 if (target
->target_number
== (int)num
) {
337 LOG_WARNING("use '%s' as target identifier, not '%u'",
338 target
->cmd_name
, num
);
346 /* returns a pointer to the n-th configured target */
347 static struct target
*get_target_by_num(int num
)
349 struct target
*target
= all_targets
;
352 if (target
->target_number
== num
) {
355 target
= target
->next
;
361 struct target
* get_current_target(struct command_context
*cmd_ctx
)
363 struct target
*target
= get_target_by_num(cmd_ctx
->current_target
);
367 LOG_ERROR("BUG: current_target out of bounds");
374 int target_poll(struct target
*target
)
378 /* We can't poll until after examine */
379 if (!target_was_examined(target
))
381 /* Fail silently lest we pollute the log */
385 retval
= target
->type
->poll(target
);
386 if (retval
!= ERROR_OK
)
389 if (target
->halt_issued
)
391 if (target
->state
== TARGET_HALTED
)
393 target
->halt_issued
= false;
396 long long t
= timeval_ms() - target
->halt_issued_time
;
399 target
->halt_issued
= false;
400 LOG_INFO("Halt timed out, wake up GDB.");
401 target_call_event_callbacks(target
, TARGET_EVENT_GDB_HALT
);
409 int target_halt(struct target
*target
)
412 /* We can't poll until after examine */
413 if (!target_was_examined(target
))
415 LOG_ERROR("Target not examined yet");
419 retval
= target
->type
->halt(target
);
420 if (retval
!= ERROR_OK
)
423 target
->halt_issued
= true;
424 target
->halt_issued_time
= timeval_ms();
430 * Make the target (re)start executing using its saved execution
431 * context (possibly with some modifications).
433 * @param target Which target should start executing.
434 * @param current True to use the target's saved program counter instead
435 * of the address parameter
436 * @param address Optionally used as the program counter.
437 * @param handle_breakpoints True iff breakpoints at the resumption PC
438 * should be skipped. (For example, maybe execution was stopped by
439 * such a breakpoint, in which case it would be counterprodutive to
441 * @param debug_execution False if all working areas allocated by OpenOCD
442 * should be released and/or restored to their original contents.
443 * (This would for example be true to run some downloaded "helper"
444 * algorithm code, which resides in one such working buffer and uses
445 * another for data storage.)
447 * @todo Resolve the ambiguity about what the "debug_execution" flag
448 * signifies. For example, Target implementations don't agree on how
449 * it relates to invalidation of the register cache, or to whether
450 * breakpoints and watchpoints should be enabled. (It would seem wrong
451 * to enable breakpoints when running downloaded "helper" algorithms
452 * (debug_execution true), since the breakpoints would be set to match
453 * target firmware being debugged, not the helper algorithm.... and
454 * enabling them could cause such helpers to malfunction (for example,
455 * by overwriting data with a breakpoint instruction. On the other
456 * hand the infrastructure for running such helpers might use this
457 * procedure but rely on hardware breakpoint to detect termination.)
459 int target_resume(struct target
*target
, int current
, uint32_t address
, int handle_breakpoints
, int debug_execution
)
463 /* We can't poll until after examine */
464 if (!target_was_examined(target
))
466 LOG_ERROR("Target not examined yet");
470 /* note that resume *must* be asynchronous. The CPU can halt before
471 * we poll. The CPU can even halt at the current PC as a result of
472 * a software breakpoint being inserted by (a bug?) the application.
474 if ((retval
= target
->type
->resume(target
, current
, address
, handle_breakpoints
, debug_execution
)) != ERROR_OK
)
477 /* Invalidate any cached protect/erase/... flash status, since
478 * almost all targets will now be able modify the flash by
479 * themselves. We want flash drivers and infrastructure to
480 * be able to rely on (non-invalidated) cached state.
482 * For now we require that algorithms provided by OpenOCD are
483 * used only by code which properly maintains that cached state.
486 * REVISIT do the same for NAND ; maybe other flash flavors too...
488 if (!target
->running_alg
)
493 static int target_process_reset(struct command_context
*cmd_ctx
, enum target_reset_mode reset_mode
)
498 n
= Jim_Nvp_value2name_simple(nvp_reset_modes
, reset_mode
);
499 if (n
->name
== NULL
) {
500 LOG_ERROR("invalid reset mode");
504 /* disable polling during reset to make reset event scripts
505 * more predictable, i.e. dr/irscan & pathmove in events will
506 * not have JTAG operations injected into the middle of a sequence.
508 bool save_poll
= jtag_poll_get_enabled();
510 jtag_poll_set_enabled(false);
512 sprintf(buf
, "ocd_process_reset %s", n
->name
);
513 retval
= Jim_Eval(cmd_ctx
->interp
, buf
);
515 jtag_poll_set_enabled(save_poll
);
517 if (retval
!= JIM_OK
) {
518 Jim_PrintErrorMessage(cmd_ctx
->interp
);
522 /* We want any events to be processed before the prompt */
523 retval
= target_call_timer_callbacks_now();
525 struct target
*target
;
526 for (target
= all_targets
; target
; target
= target
->next
) {
527 target
->type
->check_reset(target
);
533 static int identity_virt2phys(struct target
*target
,
534 uint32_t virtual, uint32_t *physical
)
540 static int no_mmu(struct target
*target
, int *enabled
)
546 static int default_examine(struct target
*target
)
548 target_set_examined(target
);
552 /* no check by default */
553 static int default_check_reset(struct target
*target
)
558 int target_examine_one(struct target
*target
)
560 return target
->type
->examine(target
);
563 static int jtag_enable_callback(enum jtag_event event
, void *priv
)
565 struct target
*target
= priv
;
567 if (event
!= JTAG_TAP_EVENT_ENABLE
|| !target
->tap
->enabled
)
570 jtag_unregister_event_callback(jtag_enable_callback
, target
);
571 return target_examine_one(target
);
575 /* Targets that correctly implement init + examine, i.e.
576 * no communication with target during init:
580 int target_examine(void)
582 int retval
= ERROR_OK
;
583 struct target
*target
;
585 for (target
= all_targets
; target
; target
= target
->next
)
587 /* defer examination, but don't skip it */
588 if (!target
->tap
->enabled
) {
589 jtag_register_event_callback(jtag_enable_callback
,
593 if ((retval
= target_examine_one(target
)) != ERROR_OK
)
598 const char *target_type_name(struct target
*target
)
600 return target
->type
->name
;
603 static int target_write_memory_imp(struct target
*target
, uint32_t address
, uint32_t size
, uint32_t count
, uint8_t *buffer
)
605 if (!target_was_examined(target
))
607 LOG_ERROR("Target not examined yet");
610 return target
->type
->write_memory_imp(target
, address
, size
, count
, buffer
);
613 static int target_read_memory_imp(struct target
*target
, uint32_t address
, uint32_t size
, uint32_t count
, uint8_t *buffer
)
615 if (!target_was_examined(target
))
617 LOG_ERROR("Target not examined yet");
620 return target
->type
->read_memory_imp(target
, address
, size
, count
, buffer
);
623 static int target_soft_reset_halt_imp(struct target
*target
)
625 if (!target_was_examined(target
))
627 LOG_ERROR("Target not examined yet");
630 if (!target
->type
->soft_reset_halt_imp
) {
631 LOG_ERROR("Target %s does not support soft_reset_halt",
632 target_name(target
));
635 return target
->type
->soft_reset_halt_imp(target
);
639 * Downloads a target-specific native code algorithm to the target,
640 * and executes it. * Note that some targets may need to set up, enable,
641 * and tear down a breakpoint (hard or * soft) to detect algorithm
642 * termination, while others may support lower overhead schemes where
643 * soft breakpoints embedded in the algorithm automatically terminate the
646 * @param target used to run the algorithm
647 * @param arch_info target-specific description of the algorithm.
649 int target_run_algorithm(struct target
*target
,
650 int num_mem_params
, struct mem_param
*mem_params
,
651 int num_reg_params
, struct reg_param
*reg_param
,
652 uint32_t entry_point
, uint32_t exit_point
,
653 int timeout_ms
, void *arch_info
)
655 int retval
= ERROR_FAIL
;
657 if (!target_was_examined(target
))
659 LOG_ERROR("Target not examined yet");
662 if (!target
->type
->run_algorithm
) {
663 LOG_ERROR("Target type '%s' does not support %s",
664 target_type_name(target
), __func__
);
668 target
->running_alg
= true;
669 retval
= target
->type
->run_algorithm(target
,
670 num_mem_params
, mem_params
,
671 num_reg_params
, reg_param
,
672 entry_point
, exit_point
, timeout_ms
, arch_info
);
673 target
->running_alg
= false;
680 int target_read_memory(struct target
*target
,
681 uint32_t address
, uint32_t size
, uint32_t count
, uint8_t *buffer
)
683 return target
->type
->read_memory(target
, address
, size
, count
, buffer
);
686 static int target_read_phys_memory(struct target
*target
,
687 uint32_t address
, uint32_t size
, uint32_t count
, uint8_t *buffer
)
689 return target
->type
->read_phys_memory(target
, address
, size
, count
, buffer
);
692 int target_write_memory(struct target
*target
,
693 uint32_t address
, uint32_t size
, uint32_t count
, uint8_t *buffer
)
695 return target
->type
->write_memory(target
, address
, size
, count
, buffer
);
698 static int target_write_phys_memory(struct target
*target
,
699 uint32_t address
, uint32_t size
, uint32_t count
, uint8_t *buffer
)
701 return target
->type
->write_phys_memory(target
, address
, size
, count
, buffer
);
704 int target_bulk_write_memory(struct target
*target
,
705 uint32_t address
, uint32_t count
, uint8_t *buffer
)
707 return target
->type
->bulk_write_memory(target
, address
, count
, buffer
);
710 int target_add_breakpoint(struct target
*target
,
711 struct breakpoint
*breakpoint
)
713 if (target
->state
!= TARGET_HALTED
) {
714 LOG_WARNING("target %s is not halted", target
->cmd_name
);
715 return ERROR_TARGET_NOT_HALTED
;
717 return target
->type
->add_breakpoint(target
, breakpoint
);
719 int target_remove_breakpoint(struct target
*target
,
720 struct breakpoint
*breakpoint
)
722 return target
->type
->remove_breakpoint(target
, breakpoint
);
725 int target_add_watchpoint(struct target
*target
,
726 struct watchpoint
*watchpoint
)
728 if (target
->state
!= TARGET_HALTED
) {
729 LOG_WARNING("target %s is not halted", target
->cmd_name
);
730 return ERROR_TARGET_NOT_HALTED
;
732 return target
->type
->add_watchpoint(target
, watchpoint
);
734 int target_remove_watchpoint(struct target
*target
,
735 struct watchpoint
*watchpoint
)
737 return target
->type
->remove_watchpoint(target
, watchpoint
);
740 int target_get_gdb_reg_list(struct target
*target
,
741 struct reg
**reg_list
[], int *reg_list_size
)
743 return target
->type
->get_gdb_reg_list(target
, reg_list
, reg_list_size
);
745 int target_step(struct target
*target
,
746 int current
, uint32_t address
, int handle_breakpoints
)
748 return target
->type
->step(target
, current
, address
, handle_breakpoints
);
753 * Reset the @c examined flag for the given target.
754 * Pure paranoia -- targets are zeroed on allocation.
756 static void target_reset_examined(struct target
*target
)
758 target
->examined
= false;
762 err_read_phys_memory(struct target
*target
, uint32_t address
,
763 uint32_t size
, uint32_t count
, uint8_t *buffer
)
765 LOG_ERROR("Not implemented: %s", __func__
);
770 err_write_phys_memory(struct target
*target
, uint32_t address
,
771 uint32_t size
, uint32_t count
, uint8_t *buffer
)
773 LOG_ERROR("Not implemented: %s", __func__
);
777 static int handle_target(void *priv
);
779 static int target_init_one(struct command_context
*cmd_ctx
,
780 struct target
*target
)
782 target_reset_examined(target
);
784 struct target_type
*type
= target
->type
;
785 if (type
->examine
== NULL
)
786 type
->examine
= default_examine
;
788 if (type
->check_reset
== NULL
)
789 type
->check_reset
= default_check_reset
;
791 int retval
= type
->init_target(cmd_ctx
, target
);
792 if (ERROR_OK
!= retval
)
794 LOG_ERROR("target '%s' init failed", target_name(target
));
799 * @todo get rid of those *memory_imp() methods, now that all
800 * callers are using target_*_memory() accessors ... and make
801 * sure the "physical" paths handle the same issues.
803 /* a non-invasive way(in terms of patches) to add some code that
804 * runs before the type->write/read_memory implementation
806 type
->write_memory_imp
= target
->type
->write_memory
;
807 type
->write_memory
= target_write_memory_imp
;
809 type
->read_memory_imp
= target
->type
->read_memory
;
810 type
->read_memory
= target_read_memory_imp
;
812 type
->soft_reset_halt_imp
= target
->type
->soft_reset_halt
;
813 type
->soft_reset_halt
= target_soft_reset_halt_imp
;
815 /* Sanity-check MMU support ... stub in what we must, to help
816 * implement it in stages, but warn if we need to do so.
820 if (type
->write_phys_memory
== NULL
)
822 LOG_ERROR("type '%s' is missing write_phys_memory",
824 type
->write_phys_memory
= err_write_phys_memory
;
826 if (type
->read_phys_memory
== NULL
)
828 LOG_ERROR("type '%s' is missing read_phys_memory",
830 type
->read_phys_memory
= err_read_phys_memory
;
832 if (type
->virt2phys
== NULL
)
834 LOG_ERROR("type '%s' is missing virt2phys", type
->name
);
835 type
->virt2phys
= identity_virt2phys
;
840 /* Make sure no-MMU targets all behave the same: make no
841 * distinction between physical and virtual addresses, and
842 * ensure that virt2phys() is always an identity mapping.
844 if (type
->write_phys_memory
|| type
->read_phys_memory
847 LOG_WARNING("type '%s' has bad MMU hooks", type
->name
);
851 type
->write_phys_memory
= type
->write_memory
;
852 type
->read_phys_memory
= type
->read_memory
;
853 type
->virt2phys
= identity_virt2phys
;
858 static int target_init(struct command_context
*cmd_ctx
)
860 struct target
*target
;
863 for (target
= all_targets
; target
; target
= target
->next
)
865 retval
= target_init_one(cmd_ctx
, target
);
866 if (ERROR_OK
!= retval
)
873 retval
= target_register_user_commands(cmd_ctx
);
874 if (ERROR_OK
!= retval
)
877 retval
= target_register_timer_callback(&handle_target
,
878 100, 1, cmd_ctx
->interp
);
879 if (ERROR_OK
!= retval
)
885 COMMAND_HANDLER(handle_target_init_command
)
888 return ERROR_COMMAND_SYNTAX_ERROR
;
890 static bool target_initialized
= false;
891 if (target_initialized
)
893 LOG_INFO("'target init' has already been called");
896 target_initialized
= true;
898 LOG_DEBUG("Initializing targets...");
899 return target_init(CMD_CTX
);
902 int target_register_event_callback(int (*callback
)(struct target
*target
, enum target_event event
, void *priv
), void *priv
)
904 struct target_event_callback
**callbacks_p
= &target_event_callbacks
;
906 if (callback
== NULL
)
908 return ERROR_INVALID_ARGUMENTS
;
913 while ((*callbacks_p
)->next
)
914 callbacks_p
= &((*callbacks_p
)->next
);
915 callbacks_p
= &((*callbacks_p
)->next
);
918 (*callbacks_p
) = malloc(sizeof(struct target_event_callback
));
919 (*callbacks_p
)->callback
= callback
;
920 (*callbacks_p
)->priv
= priv
;
921 (*callbacks_p
)->next
= NULL
;
926 int target_register_timer_callback(int (*callback
)(void *priv
), int time_ms
, int periodic
, void *priv
)
928 struct target_timer_callback
**callbacks_p
= &target_timer_callbacks
;
931 if (callback
== NULL
)
933 return ERROR_INVALID_ARGUMENTS
;
938 while ((*callbacks_p
)->next
)
939 callbacks_p
= &((*callbacks_p
)->next
);
940 callbacks_p
= &((*callbacks_p
)->next
);
943 (*callbacks_p
) = malloc(sizeof(struct target_timer_callback
));
944 (*callbacks_p
)->callback
= callback
;
945 (*callbacks_p
)->periodic
= periodic
;
946 (*callbacks_p
)->time_ms
= time_ms
;
948 gettimeofday(&now
, NULL
);
949 (*callbacks_p
)->when
.tv_usec
= now
.tv_usec
+ (time_ms
% 1000) * 1000;
950 time_ms
-= (time_ms
% 1000);
951 (*callbacks_p
)->when
.tv_sec
= now
.tv_sec
+ (time_ms
/ 1000);
952 if ((*callbacks_p
)->when
.tv_usec
> 1000000)
954 (*callbacks_p
)->when
.tv_usec
= (*callbacks_p
)->when
.tv_usec
- 1000000;
955 (*callbacks_p
)->when
.tv_sec
+= 1;
958 (*callbacks_p
)->priv
= priv
;
959 (*callbacks_p
)->next
= NULL
;
964 int target_unregister_event_callback(int (*callback
)(struct target
*target
, enum target_event event
, void *priv
), void *priv
)
966 struct target_event_callback
**p
= &target_event_callbacks
;
967 struct target_event_callback
*c
= target_event_callbacks
;
969 if (callback
== NULL
)
971 return ERROR_INVALID_ARGUMENTS
;
976 struct target_event_callback
*next
= c
->next
;
977 if ((c
->callback
== callback
) && (c
->priv
== priv
))
991 static int target_unregister_timer_callback(int (*callback
)(void *priv
), void *priv
)
993 struct target_timer_callback
**p
= &target_timer_callbacks
;
994 struct target_timer_callback
*c
= target_timer_callbacks
;
996 if (callback
== NULL
)
998 return ERROR_INVALID_ARGUMENTS
;
1003 struct target_timer_callback
*next
= c
->next
;
1004 if ((c
->callback
== callback
) && (c
->priv
== priv
))
1018 int target_call_event_callbacks(struct target
*target
, enum target_event event
)
1020 struct target_event_callback
*callback
= target_event_callbacks
;
1021 struct target_event_callback
*next_callback
;
1023 if (event
== TARGET_EVENT_HALTED
)
1025 /* execute early halted first */
1026 target_call_event_callbacks(target
, TARGET_EVENT_GDB_HALT
);
1029 LOG_DEBUG("target event %i (%s)",
1031 Jim_Nvp_value2name_simple(nvp_target_event
, event
)->name
);
1033 target_handle_event(target
, event
);
1037 next_callback
= callback
->next
;
1038 callback
->callback(target
, event
, callback
->priv
);
1039 callback
= next_callback
;
1045 static int target_timer_callback_periodic_restart(
1046 struct target_timer_callback
*cb
, struct timeval
*now
)
1048 int time_ms
= cb
->time_ms
;
1049 cb
->when
.tv_usec
= now
->tv_usec
+ (time_ms
% 1000) * 1000;
1050 time_ms
-= (time_ms
% 1000);
1051 cb
->when
.tv_sec
= now
->tv_sec
+ time_ms
/ 1000;
1052 if (cb
->when
.tv_usec
> 1000000)
1054 cb
->when
.tv_usec
= cb
->when
.tv_usec
- 1000000;
1055 cb
->when
.tv_sec
+= 1;
1060 static int target_call_timer_callback(struct target_timer_callback
*cb
,
1061 struct timeval
*now
)
1063 cb
->callback(cb
->priv
);
1066 return target_timer_callback_periodic_restart(cb
, now
);
1068 return target_unregister_timer_callback(cb
->callback
, cb
->priv
);
1071 static int target_call_timer_callbacks_check_time(int checktime
)
1076 gettimeofday(&now
, NULL
);
1078 struct target_timer_callback
*callback
= target_timer_callbacks
;
1081 // cleaning up may unregister and free this callback
1082 struct target_timer_callback
*next_callback
= callback
->next
;
1084 bool call_it
= callback
->callback
&&
1085 ((!checktime
&& callback
->periodic
) ||
1086 now
.tv_sec
> callback
->when
.tv_sec
||
1087 (now
.tv_sec
== callback
->when
.tv_sec
&&
1088 now
.tv_usec
>= callback
->when
.tv_usec
));
1092 int retval
= target_call_timer_callback(callback
, &now
);
1093 if (retval
!= ERROR_OK
)
1097 callback
= next_callback
;
1103 int target_call_timer_callbacks(void)
1105 return target_call_timer_callbacks_check_time(1);
1108 /* invoke periodic callbacks immediately */
1109 int target_call_timer_callbacks_now(void)
1111 return target_call_timer_callbacks_check_time(0);
1114 int target_alloc_working_area(struct target
*target
, uint32_t size
, struct working_area
**area
)
1116 struct working_area
*c
= target
->working_areas
;
1117 struct working_area
*new_wa
= NULL
;
1119 /* Reevaluate working area address based on MMU state*/
1120 if (target
->working_areas
== NULL
)
1125 retval
= target
->type
->mmu(target
, &enabled
);
1126 if (retval
!= ERROR_OK
)
1132 if (target
->working_area_phys_spec
) {
1133 LOG_DEBUG("MMU disabled, using physical "
1134 "address for working memory 0x%08x",
1135 (unsigned)target
->working_area_phys
);
1136 target
->working_area
= target
->working_area_phys
;
1138 LOG_ERROR("No working memory available. "
1139 "Specify -work-area-phys to target.");
1140 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
1143 if (target
->working_area_virt_spec
) {
1144 LOG_DEBUG("MMU enabled, using virtual "
1145 "address for working memory 0x%08x",
1146 (unsigned)target
->working_area_virt
);
1147 target
->working_area
= target
->working_area_virt
;
1149 LOG_ERROR("No working memory available. "
1150 "Specify -work-area-virt to target.");
1151 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
1156 /* only allocate multiples of 4 byte */
1159 LOG_ERROR("BUG: code tried to allocate unaligned number of bytes (0x%08x), padding", ((unsigned)(size
)));
1160 size
= (size
+ 3) & (~3);
1163 /* see if there's already a matching working area */
1166 if ((c
->free
) && (c
->size
== size
))
1174 /* if not, allocate a new one */
1177 struct working_area
**p
= &target
->working_areas
;
1178 uint32_t first_free
= target
->working_area
;
1179 uint32_t free_size
= target
->working_area_size
;
1181 c
= target
->working_areas
;
1184 first_free
+= c
->size
;
1185 free_size
-= c
->size
;
1190 if (free_size
< size
)
1192 LOG_WARNING("not enough working area available(requested %u, free %u)",
1193 (unsigned)(size
), (unsigned)(free_size
));
1194 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
1197 LOG_DEBUG("allocated new working area at address 0x%08x", (unsigned)first_free
);
1199 new_wa
= malloc(sizeof(struct working_area
));
1200 new_wa
->next
= NULL
;
1201 new_wa
->size
= size
;
1202 new_wa
->address
= first_free
;
1204 if (target
->backup_working_area
)
1207 new_wa
->backup
= malloc(new_wa
->size
);
1208 if ((retval
= target_read_memory(target
, new_wa
->address
, 4, new_wa
->size
/ 4, new_wa
->backup
)) != ERROR_OK
)
1210 free(new_wa
->backup
);
1217 new_wa
->backup
= NULL
;
1220 /* put new entry in list */
1224 /* mark as used, and return the new (reused) area */
1229 new_wa
->user
= area
;
1234 static int target_free_working_area_restore(struct target
*target
, struct working_area
*area
, int restore
)
1239 if (restore
&& target
->backup_working_area
)
1242 if ((retval
= target_write_memory(target
, area
->address
, 4, area
->size
/ 4, area
->backup
)) != ERROR_OK
)
1248 /* mark user pointer invalid */
1255 int target_free_working_area(struct target
*target
, struct working_area
*area
)
1257 return target_free_working_area_restore(target
, area
, 1);
1260 /* free resources and restore memory, if restoring memory fails,
1261 * free up resources anyway
1263 static void target_free_all_working_areas_restore(struct target
*target
, int restore
)
1265 struct working_area
*c
= target
->working_areas
;
1269 struct working_area
*next
= c
->next
;
1270 target_free_working_area_restore(target
, c
, restore
);
1280 target
->working_areas
= NULL
;
1283 void target_free_all_working_areas(struct target
*target
)
1285 target_free_all_working_areas_restore(target
, 1);
1288 int target_arch_state(struct target
*target
)
1293 LOG_USER("No target has been configured");
1297 LOG_USER("target state: %s", target_state_name( target
));
1299 if (target
->state
!= TARGET_HALTED
)
1302 retval
= target
->type
->arch_state(target
);
1306 /* Single aligned words are guaranteed to use 16 or 32 bit access
1307 * mode respectively, otherwise data is handled as quickly as
1310 int target_write_buffer(struct target
*target
, uint32_t address
, uint32_t size
, uint8_t *buffer
)
1313 LOG_DEBUG("writing buffer of %i byte at 0x%8.8x",
1314 (int)size
, (unsigned)address
);
1316 if (!target_was_examined(target
))
1318 LOG_ERROR("Target not examined yet");
1326 if ((address
+ size
- 1) < address
)
1328 /* GDB can request this when e.g. PC is 0xfffffffc*/
1329 LOG_ERROR("address + size wrapped(0x%08x, 0x%08x)",
1335 if (((address
% 2) == 0) && (size
== 2))
1337 return target_write_memory(target
, address
, 2, 1, buffer
);
1340 /* handle unaligned head bytes */
1343 uint32_t unaligned
= 4 - (address
% 4);
1345 if (unaligned
> size
)
1348 if ((retval
= target_write_memory(target
, address
, 1, unaligned
, buffer
)) != ERROR_OK
)
1351 buffer
+= unaligned
;
1352 address
+= unaligned
;
1356 /* handle aligned words */
1359 int aligned
= size
- (size
% 4);
1361 /* use bulk writes above a certain limit. This may have to be changed */
1364 if ((retval
= target
->type
->bulk_write_memory(target
, address
, aligned
/ 4, buffer
)) != ERROR_OK
)
1369 if ((retval
= target_write_memory(target
, address
, 4, aligned
/ 4, buffer
)) != ERROR_OK
)
1378 /* handle tail writes of less than 4 bytes */
1381 if ((retval
= target_write_memory(target
, address
, 1, size
, buffer
)) != ERROR_OK
)
1388 /* Single aligned words are guaranteed to use 16 or 32 bit access
1389 * mode respectively, otherwise data is handled as quickly as
1392 int target_read_buffer(struct target
*target
, uint32_t address
, uint32_t size
, uint8_t *buffer
)
1395 LOG_DEBUG("reading buffer of %i byte at 0x%8.8x",
1396 (int)size
, (unsigned)address
);
1398 if (!target_was_examined(target
))
1400 LOG_ERROR("Target not examined yet");
1408 if ((address
+ size
- 1) < address
)
1410 /* GDB can request this when e.g. PC is 0xfffffffc*/
1411 LOG_ERROR("address + size wrapped(0x%08" PRIx32
", 0x%08" PRIx32
")",
1417 if (((address
% 2) == 0) && (size
== 2))
1419 return target_read_memory(target
, address
, 2, 1, buffer
);
1422 /* handle unaligned head bytes */
1425 uint32_t unaligned
= 4 - (address
% 4);
1427 if (unaligned
> size
)
1430 if ((retval
= target_read_memory(target
, address
, 1, unaligned
, buffer
)) != ERROR_OK
)
1433 buffer
+= unaligned
;
1434 address
+= unaligned
;
1438 /* handle aligned words */
1441 int aligned
= size
- (size
% 4);
1443 if ((retval
= target_read_memory(target
, address
, 4, aligned
/ 4, buffer
)) != ERROR_OK
)
1451 /*prevent byte access when possible (avoid AHB access limitations in some cases)*/
1454 int aligned
= size
- (size
%2);
1455 retval
= target_read_memory(target
, address
, 2, aligned
/ 2, buffer
);
1456 if (retval
!= ERROR_OK
)
1463 /* handle tail writes of less than 4 bytes */
1466 if ((retval
= target_read_memory(target
, address
, 1, size
, buffer
)) != ERROR_OK
)
1473 int target_checksum_memory(struct target
*target
, uint32_t address
, uint32_t size
, uint32_t* crc
)
1478 uint32_t checksum
= 0;
1479 if (!target_was_examined(target
))
1481 LOG_ERROR("Target not examined yet");
1485 if ((retval
= target
->type
->checksum_memory(target
, address
,
1486 size
, &checksum
)) != ERROR_OK
)
1488 buffer
= malloc(size
);
1491 LOG_ERROR("error allocating buffer for section (%d bytes)", (int)size
);
1492 return ERROR_INVALID_ARGUMENTS
;
1494 retval
= target_read_buffer(target
, address
, size
, buffer
);
1495 if (retval
!= ERROR_OK
)
1501 /* convert to target endianess */
1502 for (i
= 0; i
< (size
/sizeof(uint32_t)); i
++)
1504 uint32_t target_data
;
1505 target_data
= target_buffer_get_u32(target
, &buffer
[i
*sizeof(uint32_t)]);
1506 target_buffer_set_u32(target
, &buffer
[i
*sizeof(uint32_t)], target_data
);
1509 retval
= image_calculate_checksum(buffer
, size
, &checksum
);
1518 int target_blank_check_memory(struct target
*target
, uint32_t address
, uint32_t size
, uint32_t* blank
)
1521 if (!target_was_examined(target
))
1523 LOG_ERROR("Target not examined yet");
1527 if (target
->type
->blank_check_memory
== 0)
1528 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
1530 retval
= target
->type
->blank_check_memory(target
, address
, size
, blank
);
1535 int target_read_u32(struct target
*target
, uint32_t address
, uint32_t *value
)
1537 uint8_t value_buf
[4];
1538 if (!target_was_examined(target
))
1540 LOG_ERROR("Target not examined yet");
1544 int retval
= target_read_memory(target
, address
, 4, 1, value_buf
);
1546 if (retval
== ERROR_OK
)
1548 *value
= target_buffer_get_u32(target
, value_buf
);
1549 LOG_DEBUG("address: 0x%8.8" PRIx32
", value: 0x%8.8" PRIx32
"",
1556 LOG_DEBUG("address: 0x%8.8" PRIx32
" failed",
1563 int target_read_u16(struct target
*target
, uint32_t address
, uint16_t *value
)
1565 uint8_t value_buf
[2];
1566 if (!target_was_examined(target
))
1568 LOG_ERROR("Target not examined yet");
1572 int retval
= target_read_memory(target
, address
, 2, 1, value_buf
);
1574 if (retval
== ERROR_OK
)
1576 *value
= target_buffer_get_u16(target
, value_buf
);
1577 LOG_DEBUG("address: 0x%8.8" PRIx32
", value: 0x%4.4x",
1584 LOG_DEBUG("address: 0x%8.8" PRIx32
" failed",
1591 int target_read_u8(struct target
*target
, uint32_t address
, uint8_t *value
)
1593 int retval
= target_read_memory(target
, address
, 1, 1, value
);
1594 if (!target_was_examined(target
))
1596 LOG_ERROR("Target not examined yet");
1600 if (retval
== ERROR_OK
)
1602 LOG_DEBUG("address: 0x%8.8" PRIx32
", value: 0x%2.2x",
1609 LOG_DEBUG("address: 0x%8.8" PRIx32
" failed",
1616 int target_write_u32(struct target
*target
, uint32_t address
, uint32_t value
)
1619 uint8_t value_buf
[4];
1620 if (!target_was_examined(target
))
1622 LOG_ERROR("Target not examined yet");
1626 LOG_DEBUG("address: 0x%8.8" PRIx32
", value: 0x%8.8" PRIx32
"",
1630 target_buffer_set_u32(target
, value_buf
, value
);
1631 if ((retval
= target_write_memory(target
, address
, 4, 1, value_buf
)) != ERROR_OK
)
1633 LOG_DEBUG("failed: %i", retval
);
1639 int target_write_u16(struct target
*target
, uint32_t address
, uint16_t value
)
1642 uint8_t value_buf
[2];
1643 if (!target_was_examined(target
))
1645 LOG_ERROR("Target not examined yet");
1649 LOG_DEBUG("address: 0x%8.8" PRIx32
", value: 0x%8.8x",
1653 target_buffer_set_u16(target
, value_buf
, value
);
1654 if ((retval
= target_write_memory(target
, address
, 2, 1, value_buf
)) != ERROR_OK
)
1656 LOG_DEBUG("failed: %i", retval
);
1662 int target_write_u8(struct target
*target
, uint32_t address
, uint8_t value
)
1665 if (!target_was_examined(target
))
1667 LOG_ERROR("Target not examined yet");
1671 LOG_DEBUG("address: 0x%8.8" PRIx32
", value: 0x%2.2x",
1674 if ((retval
= target_write_memory(target
, address
, 1, 1, &value
)) != ERROR_OK
)
1676 LOG_DEBUG("failed: %i", retval
);
1682 COMMAND_HANDLER(handle_targets_command
)
1684 struct target
*target
= all_targets
;
1688 target
= get_target(CMD_ARGV
[0]);
1689 if (target
== NULL
) {
1690 command_print(CMD_CTX
,"Target: %s is unknown, try one of:\n", CMD_ARGV
[0]);
1693 if (!target
->tap
->enabled
) {
1694 command_print(CMD_CTX
,"Target: TAP %s is disabled, "
1695 "can't be the current target\n",
1696 target
->tap
->dotted_name
);
1700 CMD_CTX
->current_target
= target
->target_number
;
1705 target
= all_targets
;
1706 command_print(CMD_CTX
, " TargetName Type Endian TapName State ");
1707 command_print(CMD_CTX
, "-- ------------------ ---------- ------ ------------------ ------------");
1713 if (target
->tap
->enabled
)
1714 state
= target_state_name( target
);
1716 state
= "tap-disabled";
1718 if (CMD_CTX
->current_target
== target
->target_number
)
1721 /* keep columns lined up to match the headers above */
1722 command_print(CMD_CTX
, "%2d%c %-18s %-10s %-6s %-18s %s",
1723 target
->target_number
,
1725 target_name(target
),
1726 target_type_name(target
),
1727 Jim_Nvp_value2name_simple(nvp_target_endian
,
1728 target
->endianness
)->name
,
1729 target
->tap
->dotted_name
,
1731 target
= target
->next
;
1737 /* every 300ms we check for reset & powerdropout and issue a "reset halt" if so. */
1739 static int powerDropout
;
1740 static int srstAsserted
;
1742 static int runPowerRestore
;
1743 static int runPowerDropout
;
1744 static int runSrstAsserted
;
1745 static int runSrstDeasserted
;
1747 static int sense_handler(void)
1749 static int prevSrstAsserted
= 0;
1750 static int prevPowerdropout
= 0;
1753 if ((retval
= jtag_power_dropout(&powerDropout
)) != ERROR_OK
)
1757 powerRestored
= prevPowerdropout
&& !powerDropout
;
1760 runPowerRestore
= 1;
1763 long long current
= timeval_ms();
1764 static long long lastPower
= 0;
1765 int waitMore
= lastPower
+ 2000 > current
;
1766 if (powerDropout
&& !waitMore
)
1768 runPowerDropout
= 1;
1769 lastPower
= current
;
1772 if ((retval
= jtag_srst_asserted(&srstAsserted
)) != ERROR_OK
)
1776 srstDeasserted
= prevSrstAsserted
&& !srstAsserted
;
1778 static long long lastSrst
= 0;
1779 waitMore
= lastSrst
+ 2000 > current
;
1780 if (srstDeasserted
&& !waitMore
)
1782 runSrstDeasserted
= 1;
1786 if (!prevSrstAsserted
&& srstAsserted
)
1788 runSrstAsserted
= 1;
1791 prevSrstAsserted
= srstAsserted
;
1792 prevPowerdropout
= powerDropout
;
1794 if (srstDeasserted
|| powerRestored
)
1796 /* Other than logging the event we can't do anything here.
1797 * Issuing a reset is a particularly bad idea as we might
1798 * be inside a reset already.
1805 /* process target state changes */
1806 static int handle_target(void *priv
)
1808 Jim_Interp
*interp
= (Jim_Interp
*)priv
;
1809 int retval
= ERROR_OK
;
1811 if (!is_jtag_poll_safe())
1813 /* polling is disabled currently */
1817 /* we do not want to recurse here... */
1818 static int recursive
= 0;
1823 /* danger! running these procedures can trigger srst assertions and power dropouts.
1824 * We need to avoid an infinite loop/recursion here and we do that by
1825 * clearing the flags after running these events.
1827 int did_something
= 0;
1828 if (runSrstAsserted
)
1830 LOG_INFO("srst asserted detected, running srst_asserted proc.");
1831 Jim_Eval(interp
, "srst_asserted");
1834 if (runSrstDeasserted
)
1836 Jim_Eval(interp
, "srst_deasserted");
1839 if (runPowerDropout
)
1841 LOG_INFO("Power dropout detected, running power_dropout proc.");
1842 Jim_Eval(interp
, "power_dropout");
1845 if (runPowerRestore
)
1847 Jim_Eval(interp
, "power_restore");
1853 /* clear detect flags */
1857 /* clear action flags */
1859 runSrstAsserted
= 0;
1860 runSrstDeasserted
= 0;
1861 runPowerRestore
= 0;
1862 runPowerDropout
= 0;
1867 /* Poll targets for state changes unless that's globally disabled.
1868 * Skip targets that are currently disabled.
1870 for (struct target
*target
= all_targets
;
1871 is_jtag_poll_safe() && target
;
1872 target
= target
->next
)
1874 if (!target
->tap
->enabled
)
1877 /* only poll target if we've got power and srst isn't asserted */
1878 if (!powerDropout
&& !srstAsserted
)
1880 /* polling may fail silently until the target has been examined */
1881 if ((retval
= target_poll(target
)) != ERROR_OK
)
1883 /* FIX!!!!! If we add a LOG_INFO() here to output a line in GDB
1884 * *why* we are aborting GDB, then we'll spam telnet when the
1885 * poll is failing persistently.
1887 * If we could implement an event that detected the
1888 * target going from non-pollable to pollable, we could issue
1889 * an error only upon the transition.
1891 target_call_event_callbacks(target
, TARGET_EVENT_GDB_HALT
);
1900 COMMAND_HANDLER(handle_reg_command
)
1902 struct target
*target
;
1903 struct reg
*reg
= NULL
;
1909 target
= get_current_target(CMD_CTX
);
1911 /* list all available registers for the current target */
1914 struct reg_cache
*cache
= target
->reg_cache
;
1921 command_print(CMD_CTX
, "===== %s", cache
->name
);
1923 for (i
= 0, reg
= cache
->reg_list
;
1924 i
< cache
->num_regs
;
1925 i
++, reg
++, count
++)
1927 /* only print cached values if they are valid */
1929 value
= buf_to_str(reg
->value
,
1931 command_print(CMD_CTX
,
1932 "(%i) %s (/%" PRIu32
"): 0x%s%s",
1940 command_print(CMD_CTX
, "(%i) %s (/%" PRIu32
")",
1945 cache
= cache
->next
;
1951 /* access a single register by its ordinal number */
1952 if ((CMD_ARGV
[0][0] >= '0') && (CMD_ARGV
[0][0] <= '9'))
1955 COMMAND_PARSE_NUMBER(uint
, CMD_ARGV
[0], num
);
1957 struct reg_cache
*cache
= target
->reg_cache
;
1962 for (i
= 0; i
< cache
->num_regs
; i
++)
1966 reg
= &cache
->reg_list
[i
];
1972 cache
= cache
->next
;
1977 command_print(CMD_CTX
, "%i is out of bounds, the current target has only %i registers (0 - %i)", num
, count
, count
- 1);
1980 } else /* access a single register by its name */
1982 reg
= register_get_by_name(target
->reg_cache
, CMD_ARGV
[0], 1);
1986 command_print(CMD_CTX
, "register %s not found in current target", CMD_ARGV
[0]);
1991 /* display a register */
1992 if ((CMD_ARGC
== 1) || ((CMD_ARGC
== 2) && !((CMD_ARGV
[1][0] >= '0') && (CMD_ARGV
[1][0] <= '9'))))
1994 if ((CMD_ARGC
== 2) && (strcmp(CMD_ARGV
[1], "force") == 0))
1997 if (reg
->valid
== 0)
1999 reg
->type
->get(reg
);
2001 value
= buf_to_str(reg
->value
, reg
->size
, 16);
2002 command_print(CMD_CTX
, "%s (/%i): 0x%s", reg
->name
, (int)(reg
->size
), value
);
2007 /* set register value */
2010 uint8_t *buf
= malloc(DIV_ROUND_UP(reg
->size
, 8));
2011 str_to_buf(CMD_ARGV
[1], strlen(CMD_ARGV
[1]), buf
, reg
->size
, 0);
2013 reg
->type
->set(reg
, buf
);
2015 value
= buf_to_str(reg
->value
, reg
->size
, 16);
2016 command_print(CMD_CTX
, "%s (/%i): 0x%s", reg
->name
, (int)(reg
->size
), value
);
2024 command_print(CMD_CTX
, "usage: reg <#|name> [value]");
2029 COMMAND_HANDLER(handle_poll_command
)
2031 int retval
= ERROR_OK
;
2032 struct target
*target
= get_current_target(CMD_CTX
);
2036 command_print(CMD_CTX
, "background polling: %s",
2037 jtag_poll_get_enabled() ? "on" : "off");
2038 command_print(CMD_CTX
, "TAP: %s (%s)",
2039 target
->tap
->dotted_name
,
2040 target
->tap
->enabled
? "enabled" : "disabled");
2041 if (!target
->tap
->enabled
)
2043 if ((retval
= target_poll(target
)) != ERROR_OK
)
2045 if ((retval
= target_arch_state(target
)) != ERROR_OK
)
2048 else if (CMD_ARGC
== 1)
2051 COMMAND_PARSE_ON_OFF(CMD_ARGV
[0], enable
);
2052 jtag_poll_set_enabled(enable
);
2056 return ERROR_COMMAND_SYNTAX_ERROR
;
2062 COMMAND_HANDLER(handle_wait_halt_command
)
2065 return ERROR_COMMAND_SYNTAX_ERROR
;
2070 int retval
= parse_uint(CMD_ARGV
[0], &ms
);
2071 if (ERROR_OK
!= retval
)
2073 command_print(CMD_CTX
, "usage: %s [seconds]", CMD_NAME
);
2074 return ERROR_COMMAND_SYNTAX_ERROR
;
2076 // convert seconds (given) to milliseconds (needed)
2080 struct target
*target
= get_current_target(CMD_CTX
);
2081 return target_wait_state(target
, TARGET_HALTED
, ms
);
2084 /* wait for target state to change. The trick here is to have a low
2085 * latency for short waits and not to suck up all the CPU time
2088 * After 500ms, keep_alive() is invoked
2090 int target_wait_state(struct target
*target
, enum target_state state
, int ms
)
2093 long long then
= 0, cur
;
2098 if ((retval
= target_poll(target
)) != ERROR_OK
)
2100 if (target
->state
== state
)
2108 then
= timeval_ms();
2109 LOG_DEBUG("waiting for target %s...",
2110 Jim_Nvp_value2name_simple(nvp_target_state
,state
)->name
);
2118 if ((cur
-then
) > ms
)
2120 LOG_ERROR("timed out while waiting for target %s",
2121 Jim_Nvp_value2name_simple(nvp_target_state
,state
)->name
);
2129 COMMAND_HANDLER(handle_halt_command
)
2133 struct target
*target
= get_current_target(CMD_CTX
);
2134 int retval
= target_halt(target
);
2135 if (ERROR_OK
!= retval
)
2141 retval
= parse_uint(CMD_ARGV
[0], &wait
);
2142 if (ERROR_OK
!= retval
)
2143 return ERROR_COMMAND_SYNTAX_ERROR
;
2148 return CALL_COMMAND_HANDLER(handle_wait_halt_command
);
2151 COMMAND_HANDLER(handle_soft_reset_halt_command
)
2153 struct target
*target
= get_current_target(CMD_CTX
);
2155 LOG_USER("requesting target halt and executing a soft reset");
2157 target
->type
->soft_reset_halt(target
);
2162 COMMAND_HANDLER(handle_reset_command
)
2165 return ERROR_COMMAND_SYNTAX_ERROR
;
2167 enum target_reset_mode reset_mode
= RESET_RUN
;
2171 n
= Jim_Nvp_name2value_simple(nvp_reset_modes
, CMD_ARGV
[0]);
2172 if ((n
->name
== NULL
) || (n
->value
== RESET_UNKNOWN
)) {
2173 return ERROR_COMMAND_SYNTAX_ERROR
;
2175 reset_mode
= n
->value
;
2178 /* reset *all* targets */
2179 return target_process_reset(CMD_CTX
, reset_mode
);
2183 COMMAND_HANDLER(handle_resume_command
)
2187 return ERROR_COMMAND_SYNTAX_ERROR
;
2189 struct target
*target
= get_current_target(CMD_CTX
);
2190 target_handle_event(target
, TARGET_EVENT_OLD_pre_resume
);
2192 /* with no CMD_ARGV, resume from current pc, addr = 0,
2193 * with one arguments, addr = CMD_ARGV[0],
2194 * handle breakpoints, not debugging */
2198 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], addr
);
2202 return target_resume(target
, current
, addr
, 1, 0);
2205 COMMAND_HANDLER(handle_step_command
)
2208 return ERROR_COMMAND_SYNTAX_ERROR
;
2212 /* with no CMD_ARGV, step from current pc, addr = 0,
2213 * with one argument addr = CMD_ARGV[0],
2214 * handle breakpoints, debugging */
2219 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], addr
);
2223 struct target
*target
= get_current_target(CMD_CTX
);
2225 return target
->type
->step(target
, current_pc
, addr
, 1);
2228 static void handle_md_output(struct command_context
*cmd_ctx
,
2229 struct target
*target
, uint32_t address
, unsigned size
,
2230 unsigned count
, const uint8_t *buffer
)
2232 const unsigned line_bytecnt
= 32;
2233 unsigned line_modulo
= line_bytecnt
/ size
;
2235 char output
[line_bytecnt
* 4 + 1];
2236 unsigned output_len
= 0;
2238 const char *value_fmt
;
2240 case 4: value_fmt
= "%8.8x "; break;
2241 case 2: value_fmt
= "%4.4x "; break;
2242 case 1: value_fmt
= "%2.2x "; break;
2244 /* "can't happen", caller checked */
2245 LOG_ERROR("invalid memory read size: %u", size
);
2249 for (unsigned i
= 0; i
< count
; i
++)
2251 if (i
% line_modulo
== 0)
2253 output_len
+= snprintf(output
+ output_len
,
2254 sizeof(output
) - output_len
,
2256 (unsigned)(address
+ (i
*size
)));
2260 const uint8_t *value_ptr
= buffer
+ i
* size
;
2262 case 4: value
= target_buffer_get_u32(target
, value_ptr
); break;
2263 case 2: value
= target_buffer_get_u16(target
, value_ptr
); break;
2264 case 1: value
= *value_ptr
;
2266 output_len
+= snprintf(output
+ output_len
,
2267 sizeof(output
) - output_len
,
2270 if ((i
% line_modulo
== line_modulo
- 1) || (i
== count
- 1))
2272 command_print(cmd_ctx
, "%s", output
);
2278 COMMAND_HANDLER(handle_md_command
)
2281 return ERROR_COMMAND_SYNTAX_ERROR
;
2284 switch (CMD_NAME
[2]) {
2285 case 'w': size
= 4; break;
2286 case 'h': size
= 2; break;
2287 case 'b': size
= 1; break;
2288 default: return ERROR_COMMAND_SYNTAX_ERROR
;
2291 bool physical
=strcmp(CMD_ARGV
[0], "phys")==0;
2292 int (*fn
)(struct target
*target
,
2293 uint32_t address
, uint32_t size
, uint32_t count
, uint8_t *buffer
);
2298 fn
=target_read_phys_memory
;
2301 fn
=target_read_memory
;
2303 if ((CMD_ARGC
< 1) || (CMD_ARGC
> 2))
2305 return ERROR_COMMAND_SYNTAX_ERROR
;
2309 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], address
);
2313 COMMAND_PARSE_NUMBER(uint
, CMD_ARGV
[1], count
);
2315 uint8_t *buffer
= calloc(count
, size
);
2317 struct target
*target
= get_current_target(CMD_CTX
);
2318 int retval
= fn(target
, address
, size
, count
, buffer
);
2319 if (ERROR_OK
== retval
)
2320 handle_md_output(CMD_CTX
, target
, address
, size
, count
, buffer
);
2327 typedef int (*target_write_fn
)(struct target
*target
,
2328 uint32_t address
, uint32_t size
, uint32_t count
, uint8_t *buffer
);
2330 static int target_write_memory_fast(struct target
*target
,
2331 uint32_t address
, uint32_t size
, uint32_t count
, uint8_t *buffer
)
2333 return target_write_buffer(target
, address
, size
* count
, buffer
);
2336 static int target_fill_mem(struct target
*target
,
2345 /* We have to write in reasonably large chunks to be able
2346 * to fill large memory areas with any sane speed */
2347 const unsigned chunk_size
= 16384;
2348 uint8_t *target_buf
= malloc(chunk_size
* data_size
);
2349 if (target_buf
== NULL
)
2351 LOG_ERROR("Out of memory");
2355 for (unsigned i
= 0; i
< chunk_size
; i
++)
2360 target_buffer_set_u32(target
, target_buf
+ i
*data_size
, b
);
2363 target_buffer_set_u16(target
, target_buf
+ i
*data_size
, b
);
2366 target_buffer_set_u8(target
, target_buf
+ i
*data_size
, b
);
2373 int retval
= ERROR_OK
;
2375 for (unsigned x
= 0; x
< c
; x
+= chunk_size
)
2379 if (current
> chunk_size
)
2381 current
= chunk_size
;
2383 int retval
= fn(target
, address
+ x
* data_size
, data_size
, current
, target_buf
);
2384 if (retval
!= ERROR_OK
)
2388 /* avoid GDB timeouts */
2397 COMMAND_HANDLER(handle_mw_command
)
2401 return ERROR_COMMAND_SYNTAX_ERROR
;
2403 bool physical
=strcmp(CMD_ARGV
[0], "phys")==0;
2409 fn
=target_write_phys_memory
;
2412 fn
= target_write_memory_fast
;
2414 if ((CMD_ARGC
< 2) || (CMD_ARGC
> 3))
2415 return ERROR_COMMAND_SYNTAX_ERROR
;
2418 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], address
);
2421 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], value
);
2425 COMMAND_PARSE_NUMBER(uint
, CMD_ARGV
[2], count
);
2427 struct target
*target
= get_current_target(CMD_CTX
);
2429 switch (CMD_NAME
[2])
2441 return ERROR_COMMAND_SYNTAX_ERROR
;
2444 return target_fill_mem(target
, address
, fn
, wordsize
, value
, count
);
2447 static COMMAND_HELPER(parse_load_image_command_CMD_ARGV
, struct image
*image
,
2448 uint32_t *min_address
, uint32_t *max_address
)
2450 if (CMD_ARGC
< 1 || CMD_ARGC
> 5)
2451 return ERROR_COMMAND_SYNTAX_ERROR
;
2453 /* a base address isn't always necessary,
2454 * default to 0x0 (i.e. don't relocate) */
2458 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], addr
);
2459 image
->base_address
= addr
;
2460 image
->base_address_set
= 1;
2463 image
->base_address_set
= 0;
2465 image
->start_address_set
= 0;
2469 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[3], *min_address
);
2473 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[4], *max_address
);
2474 // use size (given) to find max (required)
2475 *max_address
+= *min_address
;
2478 if (*min_address
> *max_address
)
2479 return ERROR_COMMAND_SYNTAX_ERROR
;
2484 COMMAND_HANDLER(handle_load_image_command
)
2488 uint32_t image_size
;
2489 uint32_t min_address
= 0;
2490 uint32_t max_address
= 0xffffffff;
2494 int retval
= CALL_COMMAND_HANDLER(parse_load_image_command_CMD_ARGV
,
2495 &image
, &min_address
, &max_address
);
2496 if (ERROR_OK
!= retval
)
2499 struct target
*target
= get_current_target(CMD_CTX
);
2501 struct duration bench
;
2502 duration_start(&bench
);
2504 if (image_open(&image
, CMD_ARGV
[0], (CMD_ARGC
>= 3) ? CMD_ARGV
[2] : NULL
) != ERROR_OK
)
2511 for (i
= 0; i
< image
.num_sections
; i
++)
2513 buffer
= malloc(image
.sections
[i
].size
);
2516 command_print(CMD_CTX
,
2517 "error allocating buffer for section (%d bytes)",
2518 (int)(image
.sections
[i
].size
));
2522 if ((retval
= image_read_section(&image
, i
, 0x0, image
.sections
[i
].size
, buffer
, &buf_cnt
)) != ERROR_OK
)
2528 uint32_t offset
= 0;
2529 uint32_t length
= buf_cnt
;
2531 /* DANGER!!! beware of unsigned comparision here!!! */
2533 if ((image
.sections
[i
].base_address
+ buf_cnt
>= min_address
)&&
2534 (image
.sections
[i
].base_address
< max_address
))
2536 if (image
.sections
[i
].base_address
< min_address
)
2538 /* clip addresses below */
2539 offset
+= min_address
-image
.sections
[i
].base_address
;
2543 if (image
.sections
[i
].base_address
+ buf_cnt
> max_address
)
2545 length
-= (image
.sections
[i
].base_address
+ buf_cnt
)-max_address
;
2548 if ((retval
= target_write_buffer(target
, image
.sections
[i
].base_address
+ offset
, length
, buffer
+ offset
)) != ERROR_OK
)
2553 image_size
+= length
;
2554 command_print(CMD_CTX
, "%u bytes written at address 0x%8.8" PRIx32
"",
2555 (unsigned int)length
,
2556 image
.sections
[i
].base_address
+ offset
);
2562 if ((ERROR_OK
== retval
) && (duration_measure(&bench
) == ERROR_OK
))
2564 command_print(CMD_CTX
, "downloaded %" PRIu32
" bytes "
2565 "in %fs (%0.3f kb/s)", image_size
,
2566 duration_elapsed(&bench
), duration_kbps(&bench
, image_size
));
2569 image_close(&image
);
2575 COMMAND_HANDLER(handle_dump_image_command
)
2577 struct fileio fileio
;
2579 uint8_t buffer
[560];
2583 struct target
*target
= get_current_target(CMD_CTX
);
2587 command_print(CMD_CTX
, "usage: dump_image <filename> <address> <size>");
2592 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], address
);
2594 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[2], size
);
2596 if (fileio_open(&fileio
, CMD_ARGV
[0], FILEIO_WRITE
, FILEIO_BINARY
) != ERROR_OK
)
2601 struct duration bench
;
2602 duration_start(&bench
);
2604 int retval
= ERROR_OK
;
2607 size_t size_written
;
2608 uint32_t this_run_size
= (size
> 560) ? 560 : size
;
2609 retval
= target_read_buffer(target
, address
, this_run_size
, buffer
);
2610 if (retval
!= ERROR_OK
)
2615 retval
= fileio_write(&fileio
, this_run_size
, buffer
, &size_written
);
2616 if (retval
!= ERROR_OK
)
2621 size
-= this_run_size
;
2622 address
+= this_run_size
;
2625 if ((retvaltemp
= fileio_close(&fileio
)) != ERROR_OK
)
2628 if ((ERROR_OK
== retval
) && (duration_measure(&bench
) == ERROR_OK
))
2630 command_print(CMD_CTX
,
2631 "dumped %ld bytes in %fs (%0.3f kb/s)", (long)fileio
.size
,
2632 duration_elapsed(&bench
), duration_kbps(&bench
, fileio
.size
));
2638 static COMMAND_HELPER(handle_verify_image_command_internal
, int verify
)
2642 uint32_t image_size
;
2645 uint32_t checksum
= 0;
2646 uint32_t mem_checksum
= 0;
2650 struct target
*target
= get_current_target(CMD_CTX
);
2654 return ERROR_COMMAND_SYNTAX_ERROR
;
2659 LOG_ERROR("no target selected");
2663 struct duration bench
;
2664 duration_start(&bench
);
2669 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], addr
);
2670 image
.base_address
= addr
;
2671 image
.base_address_set
= 1;
2675 image
.base_address_set
= 0;
2676 image
.base_address
= 0x0;
2679 image
.start_address_set
= 0;
2681 if ((retval
= image_open(&image
, CMD_ARGV
[0], (CMD_ARGC
== 3) ? CMD_ARGV
[2] : NULL
)) != ERROR_OK
)
2688 for (i
= 0; i
< image
.num_sections
; i
++)
2690 buffer
= malloc(image
.sections
[i
].size
);
2693 command_print(CMD_CTX
,
2694 "error allocating buffer for section (%d bytes)",
2695 (int)(image
.sections
[i
].size
));
2698 if ((retval
= image_read_section(&image
, i
, 0x0, image
.sections
[i
].size
, buffer
, &buf_cnt
)) != ERROR_OK
)
2706 /* calculate checksum of image */
2707 image_calculate_checksum(buffer
, buf_cnt
, &checksum
);
2709 retval
= target_checksum_memory(target
, image
.sections
[i
].base_address
, buf_cnt
, &mem_checksum
);
2710 if (retval
!= ERROR_OK
)
2716 if (checksum
!= mem_checksum
)
2718 /* failed crc checksum, fall back to a binary compare */
2721 command_print(CMD_CTX
, "checksum mismatch - attempting binary compare");
2723 data
= (uint8_t*)malloc(buf_cnt
);
2725 /* Can we use 32bit word accesses? */
2727 int count
= buf_cnt
;
2728 if ((count
% 4) == 0)
2733 retval
= target_read_memory(target
, image
.sections
[i
].base_address
, size
, count
, data
);
2734 if (retval
== ERROR_OK
)
2737 for (t
= 0; t
< buf_cnt
; t
++)
2739 if (data
[t
] != buffer
[t
])
2741 command_print(CMD_CTX
,
2742 "Verify operation failed address 0x%08x. Was 0x%02x instead of 0x%02x\n",
2743 (unsigned)(t
+ image
.sections
[i
].base_address
),
2748 retval
= ERROR_FAIL
;
2762 command_print(CMD_CTX
, "address 0x%08" PRIx32
" length 0x%08zx",
2763 image
.sections
[i
].base_address
,
2768 image_size
+= buf_cnt
;
2771 if ((ERROR_OK
== retval
) && (duration_measure(&bench
) == ERROR_OK
))
2773 command_print(CMD_CTX
, "verified %" PRIu32
" bytes "
2774 "in %fs (%0.3f kb/s)", image_size
,
2775 duration_elapsed(&bench
), duration_kbps(&bench
, image_size
));
2778 image_close(&image
);
2783 COMMAND_HANDLER(handle_verify_image_command
)
2785 return CALL_COMMAND_HANDLER(handle_verify_image_command_internal
, 1);
2788 COMMAND_HANDLER(handle_test_image_command
)
2790 return CALL_COMMAND_HANDLER(handle_verify_image_command_internal
, 0);
2793 static int handle_bp_command_list(struct command_context
*cmd_ctx
)
2795 struct target
*target
= get_current_target(cmd_ctx
);
2796 struct breakpoint
*breakpoint
= target
->breakpoints
;
2799 if (breakpoint
->type
== BKPT_SOFT
)
2801 char* buf
= buf_to_str(breakpoint
->orig_instr
,
2802 breakpoint
->length
, 16);
2803 command_print(cmd_ctx
, "0x%8.8" PRIx32
", 0x%x, %i, 0x%s",
2804 breakpoint
->address
,
2806 breakpoint
->set
, buf
);
2811 command_print(cmd_ctx
, "0x%8.8" PRIx32
", 0x%x, %i",
2812 breakpoint
->address
,
2813 breakpoint
->length
, breakpoint
->set
);
2816 breakpoint
= breakpoint
->next
;
2821 static int handle_bp_command_set(struct command_context
*cmd_ctx
,
2822 uint32_t addr
, uint32_t length
, int hw
)
2824 struct target
*target
= get_current_target(cmd_ctx
);
2825 int retval
= breakpoint_add(target
, addr
, length
, hw
);
2826 if (ERROR_OK
== retval
)
2827 command_print(cmd_ctx
, "breakpoint set at 0x%8.8" PRIx32
"", addr
);
2829 LOG_ERROR("Failure setting breakpoint");
2833 COMMAND_HANDLER(handle_bp_command
)
2836 return handle_bp_command_list(CMD_CTX
);
2838 if (CMD_ARGC
< 2 || CMD_ARGC
> 3)
2840 command_print(CMD_CTX
, "usage: bp <address> <length> ['hw']");
2841 return ERROR_COMMAND_SYNTAX_ERROR
;
2845 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], addr
);
2847 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], length
);
2852 if (strcmp(CMD_ARGV
[2], "hw") == 0)
2855 return ERROR_COMMAND_SYNTAX_ERROR
;
2858 return handle_bp_command_set(CMD_CTX
, addr
, length
, hw
);
2861 COMMAND_HANDLER(handle_rbp_command
)
2864 return ERROR_COMMAND_SYNTAX_ERROR
;
2867 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], addr
);
2869 struct target
*target
= get_current_target(CMD_CTX
);
2870 breakpoint_remove(target
, addr
);
2875 COMMAND_HANDLER(handle_wp_command
)
2877 struct target
*target
= get_current_target(CMD_CTX
);
2881 struct watchpoint
*watchpoint
= target
->watchpoints
;
2885 command_print(CMD_CTX
, "address: 0x%8.8" PRIx32
2886 ", len: 0x%8.8" PRIx32
2887 ", r/w/a: %i, value: 0x%8.8" PRIx32
2888 ", mask: 0x%8.8" PRIx32
,
2889 watchpoint
->address
,
2891 (int)watchpoint
->rw
,
2894 watchpoint
= watchpoint
->next
;
2899 enum watchpoint_rw type
= WPT_ACCESS
;
2901 uint32_t length
= 0;
2902 uint32_t data_value
= 0x0;
2903 uint32_t data_mask
= 0xffffffff;
2908 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[4], data_mask
);
2911 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[3], data_value
);
2914 switch (CMD_ARGV
[2][0])
2926 LOG_ERROR("invalid watchpoint mode ('%c')", CMD_ARGV
[2][0]);
2927 return ERROR_COMMAND_SYNTAX_ERROR
;
2931 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], length
);
2932 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], addr
);
2936 command_print(CMD_CTX
, "usage: wp [address length "
2937 "[(r|w|a) [value [mask]]]]");
2938 return ERROR_COMMAND_SYNTAX_ERROR
;
2941 int retval
= watchpoint_add(target
, addr
, length
, type
,
2942 data_value
, data_mask
);
2943 if (ERROR_OK
!= retval
)
2944 LOG_ERROR("Failure setting watchpoints");
2949 COMMAND_HANDLER(handle_rwp_command
)
2952 return ERROR_COMMAND_SYNTAX_ERROR
;
2955 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], addr
);
2957 struct target
*target
= get_current_target(CMD_CTX
);
2958 watchpoint_remove(target
, addr
);
2965 * Translate a virtual address to a physical address.
2967 * The low-level target implementation must have logged a detailed error
2968 * which is forwarded to telnet/GDB session.
2970 COMMAND_HANDLER(handle_virt2phys_command
)
2973 return ERROR_COMMAND_SYNTAX_ERROR
;
2976 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], va
);
2979 struct target
*target
= get_current_target(CMD_CTX
);
2980 int retval
= target
->type
->virt2phys(target
, va
, &pa
);
2981 if (retval
== ERROR_OK
)
2982 command_print(CMD_CTX
, "Physical address 0x%08" PRIx32
"", pa
);
2987 static void writeData(FILE *f
, const void *data
, size_t len
)
2989 size_t written
= fwrite(data
, 1, len
, f
);
2991 LOG_ERROR("failed to write %zu bytes: %s", len
, strerror(errno
));
2994 static void writeLong(FILE *f
, int l
)
2997 for (i
= 0; i
< 4; i
++)
2999 char c
= (l
>> (i
*8))&0xff;
3000 writeData(f
, &c
, 1);
3005 static void writeString(FILE *f
, char *s
)
3007 writeData(f
, s
, strlen(s
));
3010 /* Dump a gmon.out histogram file. */
3011 static void writeGmon(uint32_t *samples
, uint32_t sampleNum
, const char *filename
)
3014 FILE *f
= fopen(filename
, "w");
3017 writeString(f
, "gmon");
3018 writeLong(f
, 0x00000001); /* Version */
3019 writeLong(f
, 0); /* padding */
3020 writeLong(f
, 0); /* padding */
3021 writeLong(f
, 0); /* padding */
3023 uint8_t zero
= 0; /* GMON_TAG_TIME_HIST */
3024 writeData(f
, &zero
, 1);
3026 /* figure out bucket size */
3027 uint32_t min
= samples
[0];
3028 uint32_t max
= samples
[0];
3029 for (i
= 0; i
< sampleNum
; i
++)
3031 if (min
> samples
[i
])
3035 if (max
< samples
[i
])
3041 int addressSpace
= (max
-min
+ 1);
3043 static const uint32_t maxBuckets
= 256 * 1024; /* maximum buckets. */
3044 uint32_t length
= addressSpace
;
3045 if (length
> maxBuckets
)
3047 length
= maxBuckets
;
3049 int *buckets
= malloc(sizeof(int)*length
);
3050 if (buckets
== NULL
)
3055 memset(buckets
, 0, sizeof(int)*length
);
3056 for (i
= 0; i
< sampleNum
;i
++)
3058 uint32_t address
= samples
[i
];
3059 long long a
= address
-min
;
3060 long long b
= length
-1;
3061 long long c
= addressSpace
-1;
3062 int index
= (a
*b
)/c
; /* danger!!!! int32 overflows */
3066 /* append binary memory gmon.out &profile_hist_hdr ((char*)&profile_hist_hdr + sizeof(struct gmon_hist_hdr)) */
3067 writeLong(f
, min
); /* low_pc */
3068 writeLong(f
, max
); /* high_pc */
3069 writeLong(f
, length
); /* # of samples */
3070 writeLong(f
, 64000000); /* 64MHz */
3071 writeString(f
, "seconds");
3072 for (i
= 0; i
< (15-strlen("seconds")); i
++)
3073 writeData(f
, &zero
, 1);
3074 writeString(f
, "s");
3076 /*append binary memory gmon.out profile_hist_data (profile_hist_data + profile_hist_hdr.hist_size) */
3078 char *data
= malloc(2*length
);
3081 for (i
= 0; i
< length
;i
++)
3090 data
[i
*2 + 1]=(val
>> 8)&0xff;
3093 writeData(f
, data
, length
* 2);
3103 /* profiling samples the CPU PC as quickly as OpenOCD is able,
3104 * which will be used as a random sampling of PC */
3105 COMMAND_HANDLER(handle_profile_command
)
3107 struct target
*target
= get_current_target(CMD_CTX
);
3108 struct timeval timeout
, now
;
3110 gettimeofday(&timeout
, NULL
);
3113 return ERROR_COMMAND_SYNTAX_ERROR
;
3116 COMMAND_PARSE_NUMBER(uint
, CMD_ARGV
[0], offset
);
3118 timeval_add_time(&timeout
, offset
, 0);
3121 * @todo: Some cores let us sample the PC without the
3122 * annoying halt/resume step; for example, ARMv7 PCSR.
3123 * Provide a way to use that more efficient mechanism.
3126 command_print(CMD_CTX
, "Starting profiling. Halting and resuming the target as often as we can...");
3128 static const int maxSample
= 10000;
3129 uint32_t *samples
= malloc(sizeof(uint32_t)*maxSample
);
3130 if (samples
== NULL
)
3134 /* hopefully it is safe to cache! We want to stop/restart as quickly as possible. */
3135 struct reg
*reg
= register_get_by_name(target
->reg_cache
, "pc", 1);
3140 target_poll(target
);
3141 if (target
->state
== TARGET_HALTED
)
3143 uint32_t t
=*((uint32_t *)reg
->value
);
3144 samples
[numSamples
++]=t
;
3145 retval
= target_resume(target
, 1, 0, 0, 0); /* current pc, addr = 0, do not handle breakpoints, not debugging */
3146 target_poll(target
);
3147 alive_sleep(10); /* sleep 10ms, i.e. <100 samples/second. */
3148 } else if (target
->state
== TARGET_RUNNING
)
3150 /* We want to quickly sample the PC. */
3151 if ((retval
= target_halt(target
)) != ERROR_OK
)
3158 command_print(CMD_CTX
, "Target not halted or running");
3162 if (retval
!= ERROR_OK
)
3167 gettimeofday(&now
, NULL
);
3168 if ((numSamples
>= maxSample
) || ((now
.tv_sec
>= timeout
.tv_sec
) && (now
.tv_usec
>= timeout
.tv_usec
)))
3170 command_print(CMD_CTX
, "Profiling completed. %d samples.", numSamples
);
3171 if ((retval
= target_poll(target
)) != ERROR_OK
)
3176 if (target
->state
== TARGET_HALTED
)
3178 target_resume(target
, 1, 0, 0, 0); /* current pc, addr = 0, do not handle breakpoints, not debugging */
3180 if ((retval
= target_poll(target
)) != ERROR_OK
)
3185 writeGmon(samples
, numSamples
, CMD_ARGV
[1]);
3186 command_print(CMD_CTX
, "Wrote %s", CMD_ARGV
[1]);
3195 static int new_int_array_element(Jim_Interp
* interp
, const char *varname
, int idx
, uint32_t val
)
3198 Jim_Obj
*nameObjPtr
, *valObjPtr
;
3201 namebuf
= alloc_printf("%s(%d)", varname
, idx
);
3205 nameObjPtr
= Jim_NewStringObj(interp
, namebuf
, -1);
3206 valObjPtr
= Jim_NewIntObj(interp
, val
);
3207 if (!nameObjPtr
|| !valObjPtr
)
3213 Jim_IncrRefCount(nameObjPtr
);
3214 Jim_IncrRefCount(valObjPtr
);
3215 result
= Jim_SetVariable(interp
, nameObjPtr
, valObjPtr
);
3216 Jim_DecrRefCount(interp
, nameObjPtr
);
3217 Jim_DecrRefCount(interp
, valObjPtr
);
3219 /* printf("%s(%d) <= 0%08x\n", varname, idx, val); */
3223 static int jim_mem2array(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
3225 struct command_context
*context
;
3226 struct target
*target
;
3228 context
= Jim_GetAssocData(interp
, "context");
3229 if (context
== NULL
)
3231 LOG_ERROR("mem2array: no command context");
3234 target
= get_current_target(context
);
3237 LOG_ERROR("mem2array: no current target");
3241 return target_mem2array(interp
, target
, argc
-1, argv
+ 1);
3244 static int target_mem2array(Jim_Interp
*interp
, struct target
*target
, int argc
, Jim_Obj
*const *argv
)
3252 const char *varname
;
3256 /* argv[1] = name of array to receive the data
3257 * argv[2] = desired width
3258 * argv[3] = memory address
3259 * argv[4] = count of times to read
3262 Jim_WrongNumArgs(interp
, 1, argv
, "varname width addr nelems");
3265 varname
= Jim_GetString(argv
[0], &len
);
3266 /* given "foo" get space for worse case "foo(%d)" .. add 20 */
3268 e
= Jim_GetLong(interp
, argv
[1], &l
);
3274 e
= Jim_GetLong(interp
, argv
[2], &l
);
3279 e
= Jim_GetLong(interp
, argv
[3], &l
);
3295 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3296 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "Invalid width param, must be 8/16/32", NULL
);
3300 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3301 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "mem2array: zero width read?", NULL
);
3304 if ((addr
+ (len
* width
)) < addr
) {
3305 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3306 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "mem2array: addr + len - wraps to zero?", NULL
);
3309 /* absurd transfer size? */
3311 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3312 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "mem2array: absurd > 64K item request", NULL
);
3317 ((width
== 2) && ((addr
& 1) == 0)) ||
3318 ((width
== 4) && ((addr
& 3) == 0))) {
3322 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3323 sprintf(buf
, "mem2array address: 0x%08" PRIx32
" is not aligned for %" PRId32
" byte reads",
3326 Jim_AppendStrings(interp
, Jim_GetResult(interp
), buf
, NULL
);
3335 size_t buffersize
= 4096;
3336 uint8_t *buffer
= malloc(buffersize
);
3343 /* Slurp... in buffer size chunks */
3345 count
= len
; /* in objects.. */
3346 if (count
> (buffersize
/width
)) {
3347 count
= (buffersize
/width
);
3350 retval
= target_read_memory(target
, addr
, width
, count
, buffer
);
3351 if (retval
!= ERROR_OK
) {
3353 LOG_ERROR("mem2array: Read @ 0x%08x, w=%d, cnt=%d, failed",
3357 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3358 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "mem2array: cannot read memory", NULL
);
3362 v
= 0; /* shut up gcc */
3363 for (i
= 0 ;i
< count
;i
++, n
++) {
3366 v
= target_buffer_get_u32(target
, &buffer
[i
*width
]);
3369 v
= target_buffer_get_u16(target
, &buffer
[i
*width
]);
3372 v
= buffer
[i
] & 0x0ff;
3375 new_int_array_element(interp
, varname
, n
, v
);
3383 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3388 static int get_int_array_element(Jim_Interp
* interp
, const char *varname
, int idx
, uint32_t *val
)
3391 Jim_Obj
*nameObjPtr
, *valObjPtr
;
3395 namebuf
= alloc_printf("%s(%d)", varname
, idx
);
3399 nameObjPtr
= Jim_NewStringObj(interp
, namebuf
, -1);
3406 Jim_IncrRefCount(nameObjPtr
);
3407 valObjPtr
= Jim_GetVariable(interp
, nameObjPtr
, JIM_ERRMSG
);
3408 Jim_DecrRefCount(interp
, nameObjPtr
);
3410 if (valObjPtr
== NULL
)
3413 result
= Jim_GetLong(interp
, valObjPtr
, &l
);
3414 /* printf("%s(%d) => 0%08x\n", varname, idx, val); */
3419 static int jim_array2mem(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
3421 struct command_context
*context
;
3422 struct target
*target
;
3424 context
= Jim_GetAssocData(interp
, "context");
3425 if (context
== NULL
) {
3426 LOG_ERROR("array2mem: no command context");
3429 target
= get_current_target(context
);
3430 if (target
== NULL
) {
3431 LOG_ERROR("array2mem: no current target");
3435 return target_array2mem(interp
,target
, argc
-1, argv
+ 1);
3438 static int target_array2mem(Jim_Interp
*interp
, struct target
*target
,
3439 int argc
, Jim_Obj
*const *argv
)
3447 const char *varname
;
3451 /* argv[1] = name of array to get the data
3452 * argv[2] = desired width
3453 * argv[3] = memory address
3454 * argv[4] = count to write
3457 Jim_WrongNumArgs(interp
, 0, argv
, "varname width addr nelems");
3460 varname
= Jim_GetString(argv
[0], &len
);
3461 /* given "foo" get space for worse case "foo(%d)" .. add 20 */
3463 e
= Jim_GetLong(interp
, argv
[1], &l
);
3469 e
= Jim_GetLong(interp
, argv
[2], &l
);
3474 e
= Jim_GetLong(interp
, argv
[3], &l
);
3490 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3491 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "Invalid width param, must be 8/16/32", NULL
);
3495 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3496 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "array2mem: zero width read?", NULL
);
3499 if ((addr
+ (len
* width
)) < addr
) {
3500 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3501 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "array2mem: addr + len - wraps to zero?", NULL
);
3504 /* absurd transfer size? */
3506 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3507 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "array2mem: absurd > 64K item request", NULL
);
3512 ((width
== 2) && ((addr
& 1) == 0)) ||
3513 ((width
== 4) && ((addr
& 3) == 0))) {
3517 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3518 sprintf(buf
, "array2mem address: 0x%08x is not aligned for %d byte reads",
3521 Jim_AppendStrings(interp
, Jim_GetResult(interp
), buf
, NULL
);
3532 size_t buffersize
= 4096;
3533 uint8_t *buffer
= malloc(buffersize
);
3538 /* Slurp... in buffer size chunks */
3540 count
= len
; /* in objects.. */
3541 if (count
> (buffersize
/width
)) {
3542 count
= (buffersize
/width
);
3545 v
= 0; /* shut up gcc */
3546 for (i
= 0 ;i
< count
;i
++, n
++) {
3547 get_int_array_element(interp
, varname
, n
, &v
);
3550 target_buffer_set_u32(target
, &buffer
[i
*width
], v
);
3553 target_buffer_set_u16(target
, &buffer
[i
*width
], v
);
3556 buffer
[i
] = v
& 0x0ff;
3562 retval
= target_write_memory(target
, addr
, width
, count
, buffer
);
3563 if (retval
!= ERROR_OK
) {
3565 LOG_ERROR("array2mem: Write @ 0x%08x, w=%d, cnt=%d, failed",
3569 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3570 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "array2mem: cannot read memory", NULL
);
3578 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3583 void target_all_handle_event(enum target_event e
)
3585 struct target
*target
;
3587 LOG_DEBUG("**all*targets: event: %d, %s",
3589 Jim_Nvp_value2name_simple(nvp_target_event
, e
)->name
);
3591 target
= all_targets
;
3593 target_handle_event(target
, e
);
3594 target
= target
->next
;
3599 /* FIX? should we propagate errors here rather than printing them
3602 void target_handle_event(struct target
*target
, enum target_event e
)
3604 struct target_event_action
*teap
;
3606 for (teap
= target
->event_action
; teap
!= NULL
; teap
= teap
->next
) {
3607 if (teap
->event
== e
) {
3608 LOG_DEBUG("target: (%d) %s (%s) event: %d (%s) action: %s",
3609 target
->target_number
,
3610 target_name(target
),
3611 target_type_name(target
),
3613 Jim_Nvp_value2name_simple(nvp_target_event
, e
)->name
,
3614 Jim_GetString(teap
->body
, NULL
));
3615 if (Jim_EvalObj(teap
->interp
, teap
->body
) != JIM_OK
)
3617 Jim_PrintErrorMessage(teap
->interp
);
3624 * Returns true only if the target has a handler for the specified event.
3626 bool target_has_event_action(struct target
*target
, enum target_event event
)
3628 struct target_event_action
*teap
;
3630 for (teap
= target
->event_action
; teap
!= NULL
; teap
= teap
->next
) {
3631 if (teap
->event
== event
)
3637 enum target_cfg_param
{
3640 TCFG_WORK_AREA_VIRT
,
3641 TCFG_WORK_AREA_PHYS
,
3642 TCFG_WORK_AREA_SIZE
,
3643 TCFG_WORK_AREA_BACKUP
,
3646 TCFG_CHAIN_POSITION
,
3649 static Jim_Nvp nvp_config_opts
[] = {
3650 { .name
= "-type", .value
= TCFG_TYPE
},
3651 { .name
= "-event", .value
= TCFG_EVENT
},
3652 { .name
= "-work-area-virt", .value
= TCFG_WORK_AREA_VIRT
},
3653 { .name
= "-work-area-phys", .value
= TCFG_WORK_AREA_PHYS
},
3654 { .name
= "-work-area-size", .value
= TCFG_WORK_AREA_SIZE
},
3655 { .name
= "-work-area-backup", .value
= TCFG_WORK_AREA_BACKUP
},
3656 { .name
= "-endian" , .value
= TCFG_ENDIAN
},
3657 { .name
= "-variant", .value
= TCFG_VARIANT
},
3658 { .name
= "-chain-position", .value
= TCFG_CHAIN_POSITION
},
3660 { .name
= NULL
, .value
= -1 }
3663 static int target_configure(Jim_GetOptInfo
*goi
, struct target
*target
)
3671 /* parse config or cget options ... */
3672 while (goi
->argc
> 0) {
3673 Jim_SetEmptyResult(goi
->interp
);
3674 /* Jim_GetOpt_Debug(goi); */
3676 if (target
->type
->target_jim_configure
) {
3677 /* target defines a configure function */
3678 /* target gets first dibs on parameters */
3679 e
= (*(target
->type
->target_jim_configure
))(target
, goi
);
3688 /* otherwise we 'continue' below */
3690 e
= Jim_GetOpt_Nvp(goi
, nvp_config_opts
, &n
);
3692 Jim_GetOpt_NvpUnknown(goi
, nvp_config_opts
, 0);
3698 if (goi
->isconfigure
) {
3699 Jim_SetResult_sprintf(goi
->interp
,
3700 "not settable: %s", n
->name
);
3704 if (goi
->argc
!= 0) {
3705 Jim_WrongNumArgs(goi
->interp
,
3706 goi
->argc
, goi
->argv
,
3711 Jim_SetResultString(goi
->interp
,
3712 target_type_name(target
), -1);
3716 if (goi
->argc
== 0) {
3717 Jim_WrongNumArgs(goi
->interp
, goi
->argc
, goi
->argv
, "-event ?event-name? ...");
3721 e
= Jim_GetOpt_Nvp(goi
, nvp_target_event
, &n
);
3723 Jim_GetOpt_NvpUnknown(goi
, nvp_target_event
, 1);
3727 if (goi
->isconfigure
) {
3728 if (goi
->argc
!= 1) {
3729 Jim_WrongNumArgs(goi
->interp
, goi
->argc
, goi
->argv
, "-event ?event-name? ?EVENT-BODY?");
3733 if (goi
->argc
!= 0) {
3734 Jim_WrongNumArgs(goi
->interp
, goi
->argc
, goi
->argv
, "-event ?event-name?");
3740 struct target_event_action
*teap
;
3742 teap
= target
->event_action
;
3743 /* replace existing? */
3745 if (teap
->event
== (enum target_event
)n
->value
) {
3751 if (goi
->isconfigure
) {
3752 bool replace
= true;
3755 teap
= calloc(1, sizeof(*teap
));
3758 teap
->event
= n
->value
;
3759 teap
->interp
= goi
->interp
;
3760 Jim_GetOpt_Obj(goi
, &o
);
3762 Jim_DecrRefCount(teap
->interp
, teap
->body
);
3764 teap
->body
= Jim_DuplicateObj(goi
->interp
, o
);
3767 * Tcl/TK - "tk events" have a nice feature.
3768 * See the "BIND" command.
3769 * We should support that here.
3770 * You can specify %X and %Y in the event code.
3771 * The idea is: %T - target name.
3772 * The idea is: %N - target number
3773 * The idea is: %E - event name.
3775 Jim_IncrRefCount(teap
->body
);
3779 /* add to head of event list */
3780 teap
->next
= target
->event_action
;
3781 target
->event_action
= teap
;
3783 Jim_SetEmptyResult(goi
->interp
);
3787 Jim_SetEmptyResult(goi
->interp
);
3789 Jim_SetResult(goi
->interp
, Jim_DuplicateObj(goi
->interp
, teap
->body
));
3796 case TCFG_WORK_AREA_VIRT
:
3797 if (goi
->isconfigure
) {
3798 target_free_all_working_areas(target
);
3799 e
= Jim_GetOpt_Wide(goi
, &w
);
3803 target
->working_area_virt
= w
;
3804 target
->working_area_virt_spec
= true;
3806 if (goi
->argc
!= 0) {
3810 Jim_SetResult(goi
->interp
, Jim_NewIntObj(goi
->interp
, target
->working_area_virt
));
3814 case TCFG_WORK_AREA_PHYS
:
3815 if (goi
->isconfigure
) {
3816 target_free_all_working_areas(target
);
3817 e
= Jim_GetOpt_Wide(goi
, &w
);
3821 target
->working_area_phys
= w
;
3822 target
->working_area_phys_spec
= true;
3824 if (goi
->argc
!= 0) {
3828 Jim_SetResult(goi
->interp
, Jim_NewIntObj(goi
->interp
, target
->working_area_phys
));
3832 case TCFG_WORK_AREA_SIZE
:
3833 if (goi
->isconfigure
) {
3834 target_free_all_working_areas(target
);
3835 e
= Jim_GetOpt_Wide(goi
, &w
);
3839 target
->working_area_size
= w
;
3841 if (goi
->argc
!= 0) {
3845 Jim_SetResult(goi
->interp
, Jim_NewIntObj(goi
->interp
, target
->working_area_size
));
3849 case TCFG_WORK_AREA_BACKUP
:
3850 if (goi
->isconfigure
) {
3851 target_free_all_working_areas(target
);
3852 e
= Jim_GetOpt_Wide(goi
, &w
);
3856 /* make this exactly 1 or 0 */
3857 target
->backup_working_area
= (!!w
);
3859 if (goi
->argc
!= 0) {
3863 Jim_SetResult(goi
->interp
, Jim_NewIntObj(goi
->interp
, target
->backup_working_area
));
3864 /* loop for more e*/
3868 if (goi
->isconfigure
) {
3869 e
= Jim_GetOpt_Nvp(goi
, nvp_target_endian
, &n
);
3871 Jim_GetOpt_NvpUnknown(goi
, nvp_target_endian
, 1);
3874 target
->endianness
= n
->value
;
3876 if (goi
->argc
!= 0) {
3880 n
= Jim_Nvp_value2name_simple(nvp_target_endian
, target
->endianness
);
3881 if (n
->name
== NULL
) {
3882 target
->endianness
= TARGET_LITTLE_ENDIAN
;
3883 n
= Jim_Nvp_value2name_simple(nvp_target_endian
, target
->endianness
);
3885 Jim_SetResultString(goi
->interp
, n
->name
, -1);
3890 if (goi
->isconfigure
) {
3891 if (goi
->argc
< 1) {
3892 Jim_SetResult_sprintf(goi
->interp
,
3897 if (target
->variant
) {
3898 free((void *)(target
->variant
));
3900 e
= Jim_GetOpt_String(goi
, &cp
, NULL
);
3901 target
->variant
= strdup(cp
);
3903 if (goi
->argc
!= 0) {
3907 Jim_SetResultString(goi
->interp
, target
->variant
,-1);
3910 case TCFG_CHAIN_POSITION
:
3911 if (goi
->isconfigure
) {
3913 struct jtag_tap
*tap
;
3914 target_free_all_working_areas(target
);
3915 e
= Jim_GetOpt_Obj(goi
, &o
);
3919 tap
= jtag_tap_by_jim_obj(goi
->interp
, o
);
3923 /* make this exactly 1 or 0 */
3926 if (goi
->argc
!= 0) {
3930 Jim_SetResultString(goi
->interp
, target
->tap
->dotted_name
, -1);
3931 /* loop for more e*/
3934 } /* while (goi->argc) */
3937 /* done - we return */
3942 jim_target_configure(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
3946 Jim_GetOpt_Setup(&goi
, interp
, argc
- 1, argv
+ 1);
3947 goi
.isconfigure
= !strcmp(Jim_GetString(argv
[0], NULL
), "configure");
3948 int need_args
= 1 + goi
.isconfigure
;
3949 if (goi
.argc
< need_args
)
3951 Jim_WrongNumArgs(goi
.interp
, goi
.argc
, goi
.argv
,
3953 ? "missing: -option VALUE ..."
3954 : "missing: -option ...");
3957 struct target
*target
= Jim_CmdPrivData(goi
.interp
);
3958 return target_configure(&goi
, target
);
3961 static int jim_target_mw(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
3963 const char *cmd_name
= Jim_GetString(argv
[0], NULL
);
3966 Jim_GetOpt_Setup(&goi
, interp
, argc
- 1, argv
+ 1);
3968 /* danger! goi.argc will be modified below! */
3971 if (argc
!= 2 && argc
!= 3)
3973 Jim_SetResult_sprintf(goi
.interp
,
3974 "usage: %s <address> <data> [<count>]", cmd_name
);
3980 int e
= Jim_GetOpt_Wide(&goi
, &a
);
3985 e
= Jim_GetOpt_Wide(&goi
, &b
);
3992 e
= Jim_GetOpt_Wide(&goi
, &c
);
3997 struct target
*target
= Jim_CmdPrivData(goi
.interp
);
3999 if (strcasecmp(cmd_name
, "mww") == 0) {
4002 else if (strcasecmp(cmd_name
, "mwh") == 0) {
4005 else if (strcasecmp(cmd_name
, "mwb") == 0) {
4008 LOG_ERROR("command '%s' unknown: ", cmd_name
);
4012 return (target_fill_mem(target
, a
, target_write_memory_fast
, data_size
, b
, c
) == ERROR_OK
) ? JIM_OK
: JIM_ERR
;
4015 static int jim_target_md(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4017 const char *cmd_name
= Jim_GetString(argv
[0], NULL
);
4020 Jim_GetOpt_Setup(&goi
, interp
, argc
- 1, argv
+ 1);
4022 /* danger! goi.argc will be modified below! */
4025 if ((argc
!= 1) && (argc
!= 2))
4027 Jim_SetResult_sprintf(goi
.interp
,
4028 "usage: %s <address> [<count>]", cmd_name
);
4033 int e
= Jim_GetOpt_Wide(&goi
, &a
);
4039 e
= Jim_GetOpt_Wide(&goi
, &c
);
4046 jim_wide b
= 1; /* shut up gcc */
4047 if (strcasecmp(cmd_name
, "mdw") == 0)
4049 else if (strcasecmp(cmd_name
, "mdh") == 0)
4051 else if (strcasecmp(cmd_name
, "mdb") == 0)
4054 LOG_ERROR("command '%s' unknown: ", cmd_name
);
4058 /* convert count to "bytes" */
4061 struct target
*target
= Jim_CmdPrivData(goi
.interp
);
4062 uint8_t target_buf
[32];
4069 e
= target_read_memory(target
, a
, b
, y
/ b
, target_buf
);
4070 if (e
!= ERROR_OK
) {
4071 Jim_SetResult_sprintf(interp
, "error reading target @ 0x%08lx", (int)(a
));
4075 Jim_fprintf(interp
, interp
->cookie_stdout
, "0x%08x ", (int)(a
));
4078 for (x
= 0; x
< 16 && x
< y
; x
+= 4)
4080 z
= target_buffer_get_u32(target
, &(target_buf
[ x
]));
4081 Jim_fprintf(interp
, interp
->cookie_stdout
, "%08x ", (int)(z
));
4083 for (; (x
< 16) ; x
+= 4) {
4084 Jim_fprintf(interp
, interp
->cookie_stdout
, " ");
4088 for (x
= 0; x
< 16 && x
< y
; x
+= 2)
4090 z
= target_buffer_get_u16(target
, &(target_buf
[ x
]));
4091 Jim_fprintf(interp
, interp
->cookie_stdout
, "%04x ", (int)(z
));
4093 for (; (x
< 16) ; x
+= 2) {
4094 Jim_fprintf(interp
, interp
->cookie_stdout
, " ");
4099 for (x
= 0 ; (x
< 16) && (x
< y
) ; x
+= 1) {
4100 z
= target_buffer_get_u8(target
, &(target_buf
[ x
]));
4101 Jim_fprintf(interp
, interp
->cookie_stdout
, "%02x ", (int)(z
));
4103 for (; (x
< 16) ; x
+= 1) {
4104 Jim_fprintf(interp
, interp
->cookie_stdout
, " ");
4108 /* ascii-ify the bytes */
4109 for (x
= 0 ; x
< y
; x
++) {
4110 if ((target_buf
[x
] >= 0x20) &&
4111 (target_buf
[x
] <= 0x7e)) {
4115 target_buf
[x
] = '.';
4120 target_buf
[x
] = ' ';
4125 /* print - with a newline */
4126 Jim_fprintf(interp
, interp
->cookie_stdout
, "%s\n", target_buf
);
4134 static int jim_target_mem2array(Jim_Interp
*interp
,
4135 int argc
, Jim_Obj
*const *argv
)
4137 struct target
*target
= Jim_CmdPrivData(interp
);
4138 return target_mem2array(interp
, target
, argc
- 1, argv
+ 1);
4141 static int jim_target_array2mem(Jim_Interp
*interp
,
4142 int argc
, Jim_Obj
*const *argv
)
4144 struct target
*target
= Jim_CmdPrivData(interp
);
4145 return target_array2mem(interp
, target
, argc
- 1, argv
+ 1);
4148 static int jim_target_tap_disabled(Jim_Interp
*interp
)
4150 Jim_SetResult_sprintf(interp
, "[TAP is disabled]");
4154 static int jim_target_examine(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4158 Jim_WrongNumArgs(interp
, 1, argv
, "[no parameters]");
4161 struct target
*target
= Jim_CmdPrivData(interp
);
4162 if (!target
->tap
->enabled
)
4163 return jim_target_tap_disabled(interp
);
4165 int e
= target
->type
->examine(target
);
4168 Jim_SetResult_sprintf(interp
, "examine-fails: %d", e
);
4174 static int jim_target_halt_gdb(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4178 Jim_WrongNumArgs(interp
, 1, argv
, "[no parameters]");
4181 struct target
*target
= Jim_CmdPrivData(interp
);
4183 if (target_call_event_callbacks(target
, TARGET_EVENT_GDB_HALT
) != ERROR_OK
)
4189 static int jim_target_poll(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4193 Jim_WrongNumArgs(interp
, 1, argv
, "[no parameters]");
4196 struct target
*target
= Jim_CmdPrivData(interp
);
4197 if (!target
->tap
->enabled
)
4198 return jim_target_tap_disabled(interp
);
4201 if (!(target_was_examined(target
))) {
4202 e
= ERROR_TARGET_NOT_EXAMINED
;
4204 e
= target
->type
->poll(target
);
4208 Jim_SetResult_sprintf(interp
, "poll-fails: %d", e
);
4214 static int jim_target_reset(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4217 Jim_GetOpt_Setup(&goi
, interp
, argc
- 1, argv
+ 1);
4221 Jim_WrongNumArgs(interp
, 0, argv
,
4222 "([tT]|[fF]|assert|deassert) BOOL");
4227 int e
= Jim_GetOpt_Nvp(&goi
, nvp_assert
, &n
);
4230 Jim_GetOpt_NvpUnknown(&goi
, nvp_assert
, 1);
4233 /* the halt or not param */
4235 e
= Jim_GetOpt_Wide(&goi
, &a
);
4239 struct target
*target
= Jim_CmdPrivData(goi
.interp
);
4240 if (!target
->tap
->enabled
)
4241 return jim_target_tap_disabled(interp
);
4242 if (!(target_was_examined(target
)))
4244 LOG_ERROR("Target not examined yet");
4245 return ERROR_TARGET_NOT_EXAMINED
;
4247 if (!target
->type
->assert_reset
|| !target
->type
->deassert_reset
)
4249 Jim_SetResult_sprintf(interp
,
4250 "No target-specific reset for %s",
4251 target_name(target
));
4254 /* determine if we should halt or not. */
4255 target
->reset_halt
= !!a
;
4256 /* When this happens - all workareas are invalid. */
4257 target_free_all_working_areas_restore(target
, 0);
4260 if (n
->value
== NVP_ASSERT
) {
4261 e
= target
->type
->assert_reset(target
);
4263 e
= target
->type
->deassert_reset(target
);
4265 return (e
== ERROR_OK
) ? JIM_OK
: JIM_ERR
;
4268 static int jim_target_halt(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4271 Jim_WrongNumArgs(interp
, 1, argv
, "[no parameters]");
4274 struct target
*target
= Jim_CmdPrivData(interp
);
4275 if (!target
->tap
->enabled
)
4276 return jim_target_tap_disabled(interp
);
4277 int e
= target
->type
->halt(target
);
4278 return (e
== ERROR_OK
) ? JIM_OK
: JIM_ERR
;
4281 static int jim_target_wait_state(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4284 Jim_GetOpt_Setup(&goi
, interp
, argc
- 1, argv
+ 1);
4286 /* params: <name> statename timeoutmsecs */
4289 const char *cmd_name
= Jim_GetString(argv
[0], NULL
);
4290 Jim_SetResult_sprintf(goi
.interp
,
4291 "%s <state_name> <timeout_in_msec>", cmd_name
);
4296 int e
= Jim_GetOpt_Nvp(&goi
, nvp_target_state
, &n
);
4298 Jim_GetOpt_NvpUnknown(&goi
, nvp_target_state
,1);
4302 e
= Jim_GetOpt_Wide(&goi
, &a
);
4306 struct target
*target
= Jim_CmdPrivData(interp
);
4307 if (!target
->tap
->enabled
)
4308 return jim_target_tap_disabled(interp
);
4310 e
= target_wait_state(target
, n
->value
, a
);
4313 Jim_SetResult_sprintf(goi
.interp
,
4314 "target: %s wait %s fails (%d) %s",
4315 target_name(target
), n
->name
,
4316 e
, target_strerror_safe(e
));
4321 /* List for human, Events defined for this target.
4322 * scripts/programs should use 'name cget -event NAME'
4324 static int jim_target_event_list(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4326 struct command_context
*cmd_ctx
= Jim_GetAssocData(interp
, "context");
4327 struct target
*target
= Jim_CmdPrivData(interp
);
4328 struct target_event_action
*teap
= target
->event_action
;
4329 command_print(cmd_ctx
, "Event actions for target (%d) %s\n",
4330 target
->target_number
,
4331 target_name(target
));
4332 command_print(cmd_ctx
, "%-25s | Body", "Event");
4333 command_print(cmd_ctx
, "------------------------- | "
4334 "----------------------------------------");
4337 Jim_Nvp
*opt
= Jim_Nvp_value2name_simple(nvp_target_event
, teap
->event
);
4338 command_print(cmd_ctx
, "%-25s | %s",
4339 opt
->name
, Jim_GetString(teap
->body
, NULL
));
4342 command_print(cmd_ctx
, "***END***");
4345 static int jim_target_current_state(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4349 Jim_WrongNumArgs(interp
, 1, argv
, "[no parameters]");
4352 struct target
*target
= Jim_CmdPrivData(interp
);
4353 Jim_SetResultString(interp
, target_state_name(target
), -1);
4356 static int jim_target_invoke_event(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4359 Jim_GetOpt_Setup(&goi
, interp
, argc
- 1, argv
+ 1);
4362 const char *cmd_name
= Jim_GetString(argv
[0], NULL
);
4363 Jim_SetResult_sprintf(goi
.interp
, "%s <eventname>", cmd_name
);
4367 int e
= Jim_GetOpt_Nvp(&goi
, nvp_target_event
, &n
);
4370 Jim_GetOpt_NvpUnknown(&goi
, nvp_target_event
, 1);
4373 struct target
*target
= Jim_CmdPrivData(interp
);
4374 target_handle_event(target
, n
->value
);
4378 static const struct command_registration target_instance_command_handlers
[] = {
4380 .name
= "configure",
4381 .mode
= COMMAND_CONFIG
,
4382 .jim_handler
= jim_target_configure
,
4383 .help
= "configure a new target for use",
4384 .usage
= "[target_attribute ...]",
4388 .mode
= COMMAND_ANY
,
4389 .jim_handler
= jim_target_configure
,
4390 .help
= "returns the specified target attribute",
4391 .usage
= "target_attribute",
4395 .mode
= COMMAND_EXEC
,
4396 .jim_handler
= jim_target_mw
,
4397 .help
= "Write 32-bit word(s) to target memory",
4398 .usage
= "address data [count]",
4402 .mode
= COMMAND_EXEC
,
4403 .jim_handler
= jim_target_mw
,
4404 .help
= "Write 16-bit half-word(s) to target memory",
4405 .usage
= "address data [count]",
4409 .mode
= COMMAND_EXEC
,
4410 .jim_handler
= jim_target_mw
,
4411 .help
= "Write byte(s) to target memory",
4412 .usage
= "address data [count]",
4416 .mode
= COMMAND_EXEC
,
4417 .jim_handler
= jim_target_md
,
4418 .help
= "Display target memory as 32-bit words",
4419 .usage
= "address [count]",
4423 .mode
= COMMAND_EXEC
,
4424 .jim_handler
= jim_target_md
,
4425 .help
= "Display target memory as 16-bit half-words",
4426 .usage
= "address [count]",
4430 .mode
= COMMAND_EXEC
,
4431 .jim_handler
= jim_target_md
,
4432 .help
= "Display target memory as 8-bit bytes",
4433 .usage
= "address [count]",
4436 .name
= "array2mem",
4437 .mode
= COMMAND_EXEC
,
4438 .jim_handler
= jim_target_array2mem
,
4439 .help
= "Writes Tcl array of 8/16/32 bit numbers "
4441 .usage
= "arrayname bitwidth address count",
4444 .name
= "mem2array",
4445 .mode
= COMMAND_EXEC
,
4446 .jim_handler
= jim_target_mem2array
,
4447 .help
= "Loads Tcl array of 8/16/32 bit numbers "
4448 "from target memory",
4449 .usage
= "arrayname bitwidth address count",
4452 .name
= "eventlist",
4453 .mode
= COMMAND_EXEC
,
4454 .jim_handler
= jim_target_event_list
,
4455 .help
= "displays a table of events defined for this target",
4459 .mode
= COMMAND_EXEC
,
4460 .jim_handler
= jim_target_current_state
,
4461 .help
= "displays the current state of this target",
4464 .name
= "arp_examine",
4465 .mode
= COMMAND_EXEC
,
4466 .jim_handler
= jim_target_examine
,
4467 .help
= "used internally for reset processing",
4470 .name
= "arp_halt_gdb",
4471 .mode
= COMMAND_EXEC
,
4472 .jim_handler
= jim_target_halt_gdb
,
4473 .help
= "used internally for reset processing to halt GDB",
4477 .mode
= COMMAND_EXEC
,
4478 .jim_handler
= jim_target_poll
,
4479 .help
= "used internally for reset processing",
4482 .name
= "arp_reset",
4483 .mode
= COMMAND_EXEC
,
4484 .jim_handler
= jim_target_reset
,
4485 .help
= "used internally for reset processing",
4489 .mode
= COMMAND_EXEC
,
4490 .jim_handler
= jim_target_halt
,
4491 .help
= "used internally for reset processing",
4494 .name
= "arp_waitstate",
4495 .mode
= COMMAND_EXEC
,
4496 .jim_handler
= jim_target_wait_state
,
4497 .help
= "used internally for reset processing",
4500 .name
= "invoke-event",
4501 .mode
= COMMAND_EXEC
,
4502 .jim_handler
= jim_target_invoke_event
,
4503 .help
= "invoke handler for specified event",
4504 .usage
= "event_name",
4506 COMMAND_REGISTRATION_DONE
4509 static int target_create(Jim_GetOptInfo
*goi
)
4517 struct target
*target
;
4518 struct command_context
*cmd_ctx
;
4520 cmd_ctx
= Jim_GetAssocData(goi
->interp
, "context");
4521 if (goi
->argc
< 3) {
4522 Jim_WrongNumArgs(goi
->interp
, 1, goi
->argv
, "?name? ?type? ..options...");
4527 Jim_GetOpt_Obj(goi
, &new_cmd
);
4528 /* does this command exist? */
4529 cmd
= Jim_GetCommand(goi
->interp
, new_cmd
, JIM_ERRMSG
);
4531 cp
= Jim_GetString(new_cmd
, NULL
);
4532 Jim_SetResult_sprintf(goi
->interp
, "Command/target: %s Exists", cp
);
4537 e
= Jim_GetOpt_String(goi
, &cp2
, NULL
);
4539 /* now does target type exist */
4540 for (x
= 0 ; target_types
[x
] ; x
++) {
4541 if (0 == strcmp(cp
, target_types
[x
]->name
)) {
4546 if (target_types
[x
] == NULL
) {
4547 Jim_SetResult_sprintf(goi
->interp
, "Unknown target type %s, try one of ", cp
);
4548 for (x
= 0 ; target_types
[x
] ; x
++) {
4549 if (target_types
[x
+ 1]) {
4550 Jim_AppendStrings(goi
->interp
,
4551 Jim_GetResult(goi
->interp
),
4552 target_types
[x
]->name
,
4555 Jim_AppendStrings(goi
->interp
,
4556 Jim_GetResult(goi
->interp
),
4558 target_types
[x
]->name
,NULL
);
4565 target
= calloc(1,sizeof(struct target
));
4566 /* set target number */
4567 target
->target_number
= new_target_number();
4569 /* allocate memory for each unique target type */
4570 target
->type
= (struct target_type
*)calloc(1,sizeof(struct target_type
));
4572 memcpy(target
->type
, target_types
[x
], sizeof(struct target_type
));
4574 /* will be set by "-endian" */
4575 target
->endianness
= TARGET_ENDIAN_UNKNOWN
;
4577 target
->working_area
= 0x0;
4578 target
->working_area_size
= 0x0;
4579 target
->working_areas
= NULL
;
4580 target
->backup_working_area
= 0;
4582 target
->state
= TARGET_UNKNOWN
;
4583 target
->debug_reason
= DBG_REASON_UNDEFINED
;
4584 target
->reg_cache
= NULL
;
4585 target
->breakpoints
= NULL
;
4586 target
->watchpoints
= NULL
;
4587 target
->next
= NULL
;
4588 target
->arch_info
= NULL
;
4590 target
->display
= 1;
4592 target
->halt_issued
= false;
4594 /* initialize trace information */
4595 target
->trace_info
= malloc(sizeof(struct trace
));
4596 target
->trace_info
->num_trace_points
= 0;
4597 target
->trace_info
->trace_points_size
= 0;
4598 target
->trace_info
->trace_points
= NULL
;
4599 target
->trace_info
->trace_history_size
= 0;
4600 target
->trace_info
->trace_history
= NULL
;
4601 target
->trace_info
->trace_history_pos
= 0;
4602 target
->trace_info
->trace_history_overflowed
= 0;
4604 target
->dbgmsg
= NULL
;
4605 target
->dbg_msg_enabled
= 0;
4607 target
->endianness
= TARGET_ENDIAN_UNKNOWN
;
4609 /* Do the rest as "configure" options */
4610 goi
->isconfigure
= 1;
4611 e
= target_configure(goi
, target
);
4613 if (target
->tap
== NULL
)
4615 Jim_SetResultString(goi
->interp
, "-chain-position required when creating target", -1);
4625 if (target
->endianness
== TARGET_ENDIAN_UNKNOWN
) {
4626 /* default endian to little if not specified */
4627 target
->endianness
= TARGET_LITTLE_ENDIAN
;
4630 /* incase variant is not set */
4631 if (!target
->variant
)
4632 target
->variant
= strdup("");
4634 cp
= Jim_GetString(new_cmd
, NULL
);
4635 target
->cmd_name
= strdup(cp
);
4637 /* create the target specific commands */
4638 if (target
->type
->commands
) {
4639 e
= register_commands(cmd_ctx
, NULL
, target
->type
->commands
);
4641 LOG_ERROR("unable to register '%s' commands", cp
);
4643 if (target
->type
->target_create
) {
4644 (*(target
->type
->target_create
))(target
, goi
->interp
);
4647 /* append to end of list */
4649 struct target
**tpp
;
4650 tpp
= &(all_targets
);
4652 tpp
= &((*tpp
)->next
);
4657 /* now - create the new target name command */
4658 const const struct command_registration target_subcommands
[] = {
4660 .chain
= target_instance_command_handlers
,
4663 .chain
= target
->type
->commands
,
4665 COMMAND_REGISTRATION_DONE
4667 const const struct command_registration target_commands
[] = {
4670 .mode
= COMMAND_ANY
,
4671 .help
= "target command group",
4672 .chain
= target_subcommands
,
4674 COMMAND_REGISTRATION_DONE
4676 e
= register_commands(cmd_ctx
, NULL
, target_commands
);
4680 struct command
*c
= command_find_in_context(cmd_ctx
, cp
);
4682 command_set_handler_data(c
, target
);
4684 return (ERROR_OK
== e
) ? JIM_OK
: JIM_ERR
;
4687 static int jim_target_current(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4691 Jim_WrongNumArgs(interp
, 1, argv
, "Too many parameters");
4694 struct command_context
*cmd_ctx
= Jim_GetAssocData(interp
, "context");
4695 Jim_SetResultString(interp
, get_current_target(cmd_ctx
)->cmd_name
, -1);
4699 static int jim_target_types(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4703 Jim_WrongNumArgs(interp
, 1, argv
, "Too many parameters");
4706 Jim_SetResult(interp
, Jim_NewListObj(interp
, NULL
, 0));
4707 for (unsigned x
= 0; NULL
!= target_types
[x
]; x
++)
4709 Jim_ListAppendElement(interp
, Jim_GetResult(interp
),
4710 Jim_NewStringObj(interp
, target_types
[x
]->name
, -1));
4715 static int jim_target_names(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4719 Jim_WrongNumArgs(interp
, 1, argv
, "Too many parameters");
4722 Jim_SetResult(interp
, Jim_NewListObj(interp
, NULL
, 0));
4723 struct target
*target
= all_targets
;
4726 Jim_ListAppendElement(interp
, Jim_GetResult(interp
),
4727 Jim_NewStringObj(interp
, target_name(target
), -1));
4728 target
= target
->next
;
4733 static int jim_target_create(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4736 Jim_GetOpt_Setup(&goi
, interp
, argc
- 1, argv
+ 1);
4739 Jim_WrongNumArgs(goi
.interp
, goi
.argc
, goi
.argv
,
4740 "<name> <target_type> [<target_options> ...]");
4743 return target_create(&goi
);
4746 static int jim_target_number(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4749 Jim_GetOpt_Setup(&goi
, interp
, argc
- 1, argv
+ 1);
4751 /* It's OK to remove this mechanism sometime after August 2010 or so */
4752 LOG_WARNING("don't use numbers as target identifiers; use names");
4755 Jim_SetResult_sprintf(goi
.interp
, "usage: target number <number>");
4759 int e
= Jim_GetOpt_Wide(&goi
, &w
);
4763 struct target
*target
;
4764 for (target
= all_targets
; NULL
!= target
; target
= target
->next
)
4766 if (target
->target_number
!= w
)
4769 Jim_SetResultString(goi
.interp
, target_name(target
), -1);
4772 Jim_SetResult_sprintf(goi
.interp
,
4773 "Target: number %d does not exist", (int)(w
));
4777 static int jim_target_count(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4781 Jim_WrongNumArgs(interp
, 1, argv
, "<no parameters>");
4785 struct target
*target
= all_targets
;
4786 while (NULL
!= target
)
4788 target
= target
->next
;
4791 Jim_SetResult(interp
, Jim_NewIntObj(interp
, count
));
4795 static const struct command_registration target_subcommand_handlers
[] = {
4798 .mode
= COMMAND_CONFIG
,
4799 .handler
= handle_target_init_command
,
4800 .help
= "initialize targets",
4804 /* REVISIT this should be COMMAND_CONFIG ... */
4805 .mode
= COMMAND_ANY
,
4806 .jim_handler
= jim_target_create
,
4807 .usage
= "name type '-chain-position' name [options ...]",
4808 .help
= "Creates and selects a new target",
4812 .mode
= COMMAND_ANY
,
4813 .jim_handler
= jim_target_current
,
4814 .help
= "Returns the currently selected target",
4818 .mode
= COMMAND_ANY
,
4819 .jim_handler
= jim_target_types
,
4820 .help
= "Returns the available target types as "
4821 "a list of strings",
4825 .mode
= COMMAND_ANY
,
4826 .jim_handler
= jim_target_names
,
4827 .help
= "Returns the names of all targets as a list of strings",
4831 .mode
= COMMAND_ANY
,
4832 .jim_handler
= jim_target_number
,
4834 .help
= "Returns the name of the numbered target "
4839 .mode
= COMMAND_ANY
,
4840 .jim_handler
= jim_target_count
,
4841 .help
= "Returns the number of targets as an integer "
4844 COMMAND_REGISTRATION_DONE
4855 static int fastload_num
;
4856 static struct FastLoad
*fastload
;
4858 static void free_fastload(void)
4860 if (fastload
!= NULL
)
4863 for (i
= 0; i
< fastload_num
; i
++)
4865 if (fastload
[i
].data
)
4866 free(fastload
[i
].data
);
4876 COMMAND_HANDLER(handle_fast_load_image_command
)
4880 uint32_t image_size
;
4881 uint32_t min_address
= 0;
4882 uint32_t max_address
= 0xffffffff;
4887 int retval
= CALL_COMMAND_HANDLER(parse_load_image_command_CMD_ARGV
,
4888 &image
, &min_address
, &max_address
);
4889 if (ERROR_OK
!= retval
)
4892 struct duration bench
;
4893 duration_start(&bench
);
4895 if (image_open(&image
, CMD_ARGV
[0], (CMD_ARGC
>= 3) ? CMD_ARGV
[2] : NULL
) != ERROR_OK
)
4902 fastload_num
= image
.num_sections
;
4903 fastload
= (struct FastLoad
*)malloc(sizeof(struct FastLoad
)*image
.num_sections
);
4904 if (fastload
== NULL
)
4906 image_close(&image
);
4909 memset(fastload
, 0, sizeof(struct FastLoad
)*image
.num_sections
);
4910 for (i
= 0; i
< image
.num_sections
; i
++)
4912 buffer
= malloc(image
.sections
[i
].size
);
4915 command_print(CMD_CTX
, "error allocating buffer for section (%d bytes)",
4916 (int)(image
.sections
[i
].size
));
4920 if ((retval
= image_read_section(&image
, i
, 0x0, image
.sections
[i
].size
, buffer
, &buf_cnt
)) != ERROR_OK
)
4926 uint32_t offset
= 0;
4927 uint32_t length
= buf_cnt
;
4930 /* DANGER!!! beware of unsigned comparision here!!! */
4932 if ((image
.sections
[i
].base_address
+ buf_cnt
>= min_address
)&&
4933 (image
.sections
[i
].base_address
< max_address
))
4935 if (image
.sections
[i
].base_address
< min_address
)
4937 /* clip addresses below */
4938 offset
+= min_address
-image
.sections
[i
].base_address
;
4942 if (image
.sections
[i
].base_address
+ buf_cnt
> max_address
)
4944 length
-= (image
.sections
[i
].base_address
+ buf_cnt
)-max_address
;
4947 fastload
[i
].address
= image
.sections
[i
].base_address
+ offset
;
4948 fastload
[i
].data
= malloc(length
);
4949 if (fastload
[i
].data
== NULL
)
4954 memcpy(fastload
[i
].data
, buffer
+ offset
, length
);
4955 fastload
[i
].length
= length
;
4957 image_size
+= length
;
4958 command_print(CMD_CTX
, "%u bytes written at address 0x%8.8x",
4959 (unsigned int)length
,
4960 ((unsigned int)(image
.sections
[i
].base_address
+ offset
)));
4966 if ((ERROR_OK
== retval
) && (duration_measure(&bench
) == ERROR_OK
))
4968 command_print(CMD_CTX
, "Loaded %" PRIu32
" bytes "
4969 "in %fs (%0.3f kb/s)", image_size
,
4970 duration_elapsed(&bench
), duration_kbps(&bench
, image_size
));
4972 command_print(CMD_CTX
,
4973 "WARNING: image has not been loaded to target!"
4974 "You can issue a 'fast_load' to finish loading.");
4977 image_close(&image
);
4979 if (retval
!= ERROR_OK
)
4987 COMMAND_HANDLER(handle_fast_load_command
)
4990 return ERROR_COMMAND_SYNTAX_ERROR
;
4991 if (fastload
== NULL
)
4993 LOG_ERROR("No image in memory");
4997 int ms
= timeval_ms();
4999 int retval
= ERROR_OK
;
5000 for (i
= 0; i
< fastload_num
;i
++)
5002 struct target
*target
= get_current_target(CMD_CTX
);
5003 command_print(CMD_CTX
, "Write to 0x%08x, length 0x%08x",
5004 (unsigned int)(fastload
[i
].address
),
5005 (unsigned int)(fastload
[i
].length
));
5006 if (retval
== ERROR_OK
)
5008 retval
= target_write_buffer(target
, fastload
[i
].address
, fastload
[i
].length
, fastload
[i
].data
);
5010 size
+= fastload
[i
].length
;
5012 int after
= timeval_ms();
5013 command_print(CMD_CTX
, "Loaded image %f kBytes/s", (float)(size
/1024.0)/((float)(after
-ms
)/1000.0));
5017 static const struct command_registration target_command_handlers
[] = {
5020 .handler
= handle_targets_command
,
5021 .mode
= COMMAND_ANY
,
5022 .help
= "change current default target (one parameter) "
5023 "or prints table of all targets (no parameters)",
5024 .usage
= "[target]",
5028 .mode
= COMMAND_CONFIG
,
5029 .help
= "configure target",
5031 .chain
= target_subcommand_handlers
,
5033 COMMAND_REGISTRATION_DONE
5036 int target_register_commands(struct command_context
*cmd_ctx
)
5038 return register_commands(cmd_ctx
, NULL
, target_command_handlers
);
5041 static bool target_reset_nag
= true;
5043 bool get_target_reset_nag(void)
5045 return target_reset_nag
;
5048 COMMAND_HANDLER(handle_target_reset_nag
)
5050 return CALL_COMMAND_HANDLER(handle_command_parse_bool
,
5051 &target_reset_nag
, "Nag after each reset about options to improve "
5055 static const struct command_registration target_exec_command_handlers
[] = {
5057 .name
= "fast_load_image",
5058 .handler
= handle_fast_load_image_command
,
5059 .mode
= COMMAND_ANY
,
5060 .help
= "Load image into server memory for later use by "
5061 "fast_load; primarily for profiling",
5062 .usage
= "filename address ['bin'|'ihex'|'elf'|'s19'] "
5063 "[min_address [max_length]]",
5066 .name
= "fast_load",
5067 .handler
= handle_fast_load_command
,
5068 .mode
= COMMAND_EXEC
,
5069 .help
= "loads active fast load image to current target "
5070 "- mainly for profiling purposes",
5074 .handler
= handle_profile_command
,
5075 .mode
= COMMAND_EXEC
,
5076 .help
= "profiling samples the CPU PC",
5078 /** @todo don't register virt2phys() unless target supports it */
5080 .name
= "virt2phys",
5081 .handler
= handle_virt2phys_command
,
5082 .mode
= COMMAND_ANY
,
5083 .help
= "translate a virtual address into a physical address",
5084 .usage
= "virtual_address",
5088 .handler
= handle_reg_command
,
5089 .mode
= COMMAND_EXEC
,
5090 .help
= "display or set a register; with no arguments, "
5091 "displays all registers and their values",
5092 .usage
= "[(register_name|register_number) [value]]",
5096 .handler
= handle_poll_command
,
5097 .mode
= COMMAND_EXEC
,
5098 .help
= "poll target state; or reconfigure background polling",
5099 .usage
= "['on'|'off']",
5102 .name
= "wait_halt",
5103 .handler
= handle_wait_halt_command
,
5104 .mode
= COMMAND_EXEC
,
5105 .help
= "wait up to the specified number of milliseconds "
5106 "(default 5) for a previously requested halt",
5107 .usage
= "[milliseconds]",
5111 .handler
= handle_halt_command
,
5112 .mode
= COMMAND_EXEC
,
5113 .help
= "request target to halt, then wait up to the specified"
5114 "number of milliseconds (default 5) for it to complete",
5115 .usage
= "[milliseconds]",
5119 .handler
= handle_resume_command
,
5120 .mode
= COMMAND_EXEC
,
5121 .help
= "resume target execution from current PC or address",
5122 .usage
= "[address]",
5126 .handler
= handle_reset_command
,
5127 .mode
= COMMAND_EXEC
,
5128 .usage
= "[run|halt|init]",
5129 .help
= "Reset all targets into the specified mode."
5130 "Default reset mode is run, if not given.",
5133 .name
= "soft_reset_halt",
5134 .handler
= handle_soft_reset_halt_command
,
5135 .mode
= COMMAND_EXEC
,
5136 .help
= "halt the target and do a soft reset",
5140 .handler
= handle_step_command
,
5141 .mode
= COMMAND_EXEC
,
5142 .help
= "step one instruction from current PC or address",
5143 .usage
= "[address]",
5147 .handler
= handle_md_command
,
5148 .mode
= COMMAND_EXEC
,
5149 .help
= "display memory words",
5150 .usage
= "['phys'] address [count]",
5154 .handler
= handle_md_command
,
5155 .mode
= COMMAND_EXEC
,
5156 .help
= "display memory half-words",
5157 .usage
= "['phys'] address [count]",
5161 .handler
= handle_md_command
,
5162 .mode
= COMMAND_EXEC
,
5163 .help
= "display memory bytes",
5164 .usage
= "['phys'] address [count]",
5168 .handler
= handle_mw_command
,
5169 .mode
= COMMAND_EXEC
,
5170 .help
= "write memory word",
5171 .usage
= "['phys'] address value [count]",
5175 .handler
= handle_mw_command
,
5176 .mode
= COMMAND_EXEC
,
5177 .help
= "write memory half-word",
5178 .usage
= "['phys'] address value [count]",
5182 .handler
= handle_mw_command
,
5183 .mode
= COMMAND_EXEC
,
5184 .help
= "write memory byte",
5185 .usage
= "['phys'] address value [count]",
5189 .handler
= handle_bp_command
,
5190 .mode
= COMMAND_EXEC
,
5191 .help
= "list or set hardware or software breakpoint",
5192 .usage
= "[address length ['hw']]",
5196 .handler
= handle_rbp_command
,
5197 .mode
= COMMAND_EXEC
,
5198 .help
= "remove breakpoint",
5203 .handler
= handle_wp_command
,
5204 .mode
= COMMAND_EXEC
,
5205 .help
= "list (no params) or create watchpoints",
5206 .usage
= "[address length [('r'|'w'|'a') value [mask]]]",
5210 .handler
= handle_rwp_command
,
5211 .mode
= COMMAND_EXEC
,
5212 .help
= "remove watchpoint",
5216 .name
= "load_image",
5217 .handler
= handle_load_image_command
,
5218 .mode
= COMMAND_EXEC
,
5219 .usage
= "filename address ['bin'|'ihex'|'elf'|'s19'] "
5220 "[min_address] [max_length]",
5223 .name
= "dump_image",
5224 .handler
= handle_dump_image_command
,
5225 .mode
= COMMAND_EXEC
,
5226 .usage
= "filename address size",
5229 .name
= "verify_image",
5230 .handler
= handle_verify_image_command
,
5231 .mode
= COMMAND_EXEC
,
5232 .usage
= "filename [offset [type]]",
5235 .name
= "test_image",
5236 .handler
= handle_test_image_command
,
5237 .mode
= COMMAND_EXEC
,
5238 .usage
= "filename [offset [type]]",
5241 .name
= "ocd_mem2array",
5242 .mode
= COMMAND_EXEC
,
5243 .jim_handler
= jim_mem2array
,
5244 .help
= "read 8/16/32 bit memory and return as a TCL array "
5245 "for script processing",
5246 .usage
= "arrayname bitwidth address count",
5249 .name
= "ocd_array2mem",
5250 .mode
= COMMAND_EXEC
,
5251 .jim_handler
= jim_array2mem
,
5252 .help
= "convert a TCL array to memory locations "
5253 "and write the 8/16/32 bit values",
5254 .usage
= "arrayname bitwidth address count",
5257 .name
= "reset_nag",
5258 .handler
= handle_target_reset_nag
,
5259 .mode
= COMMAND_ANY
,
5260 .help
= "Nag after each reset about options that could have been "
5261 "enabled to improve performance. ",
5262 .usage
= "['enable'|'disable']",
5264 COMMAND_REGISTRATION_DONE
5266 static int target_register_user_commands(struct command_context
*cmd_ctx
)
5268 int retval
= ERROR_OK
;
5269 if ((retval
= target_request_register_commands(cmd_ctx
)) != ERROR_OK
)
5272 if ((retval
= trace_register_commands(cmd_ctx
)) != ERROR_OK
)
5276 return register_commands(cmd_ctx
, NULL
, target_exec_command_handlers
);