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
)
480 static int target_process_reset(struct command_context
*cmd_ctx
, enum target_reset_mode reset_mode
)
485 n
= Jim_Nvp_value2name_simple(nvp_reset_modes
, reset_mode
);
486 if (n
->name
== NULL
) {
487 LOG_ERROR("invalid reset mode");
491 /* disable polling during reset to make reset event scripts
492 * more predictable, i.e. dr/irscan & pathmove in events will
493 * not have JTAG operations injected into the middle of a sequence.
495 bool save_poll
= jtag_poll_get_enabled();
497 jtag_poll_set_enabled(false);
499 sprintf(buf
, "ocd_process_reset %s", n
->name
);
500 retval
= Jim_Eval(cmd_ctx
->interp
, buf
);
502 jtag_poll_set_enabled(save_poll
);
504 if (retval
!= JIM_OK
) {
505 Jim_PrintErrorMessage(cmd_ctx
->interp
);
509 /* We want any events to be processed before the prompt */
510 retval
= target_call_timer_callbacks_now();
512 struct target
*target
;
513 for (target
= all_targets
; target
; target
= target
->next
) {
514 target
->type
->check_reset(target
);
520 static int identity_virt2phys(struct target
*target
,
521 uint32_t virtual, uint32_t *physical
)
527 static int no_mmu(struct target
*target
, int *enabled
)
533 static int default_examine(struct target
*target
)
535 target_set_examined(target
);
539 /* no check by default */
540 static int default_check_reset(struct target
*target
)
545 int target_examine_one(struct target
*target
)
547 return target
->type
->examine(target
);
550 static int jtag_enable_callback(enum jtag_event event
, void *priv
)
552 struct target
*target
= priv
;
554 if (event
!= JTAG_TAP_EVENT_ENABLE
|| !target
->tap
->enabled
)
557 jtag_unregister_event_callback(jtag_enable_callback
, target
);
558 return target_examine_one(target
);
562 /* Targets that correctly implement init + examine, i.e.
563 * no communication with target during init:
567 int target_examine(void)
569 int retval
= ERROR_OK
;
570 struct target
*target
;
572 for (target
= all_targets
; target
; target
= target
->next
)
574 /* defer examination, but don't skip it */
575 if (!target
->tap
->enabled
) {
576 jtag_register_event_callback(jtag_enable_callback
,
580 if ((retval
= target_examine_one(target
)) != ERROR_OK
)
585 const char *target_type_name(struct target
*target
)
587 return target
->type
->name
;
590 static int target_write_memory_imp(struct target
*target
, uint32_t address
, uint32_t size
, uint32_t count
, uint8_t *buffer
)
592 if (!target_was_examined(target
))
594 LOG_ERROR("Target not examined yet");
597 return target
->type
->write_memory_imp(target
, address
, size
, count
, buffer
);
600 static int target_read_memory_imp(struct target
*target
, uint32_t address
, uint32_t size
, uint32_t count
, uint8_t *buffer
)
602 if (!target_was_examined(target
))
604 LOG_ERROR("Target not examined yet");
607 return target
->type
->read_memory_imp(target
, address
, size
, count
, buffer
);
610 static int target_soft_reset_halt_imp(struct target
*target
)
612 if (!target_was_examined(target
))
614 LOG_ERROR("Target not examined yet");
617 if (!target
->type
->soft_reset_halt_imp
) {
618 LOG_ERROR("Target %s does not support soft_reset_halt",
619 target_name(target
));
622 return target
->type
->soft_reset_halt_imp(target
);
626 * Downloads a target-specific native code algorithm to the target,
627 * and executes it. * Note that some targets may need to set up, enable,
628 * and tear down a breakpoint (hard or * soft) to detect algorithm
629 * termination, while others may support lower overhead schemes where
630 * soft breakpoints embedded in the algorithm automatically terminate the
633 * @param target used to run the algorithm
634 * @param arch_info target-specific description of the algorithm.
636 int target_run_algorithm(struct target
*target
,
637 int num_mem_params
, struct mem_param
*mem_params
,
638 int num_reg_params
, struct reg_param
*reg_param
,
639 uint32_t entry_point
, uint32_t exit_point
,
640 int timeout_ms
, void *arch_info
)
642 int retval
= ERROR_FAIL
;
644 if (!target_was_examined(target
))
646 LOG_ERROR("Target not examined yet");
649 if (!target
->type
->run_algorithm
) {
650 LOG_ERROR("Target type '%s' does not support %s",
651 target_type_name(target
), __func__
);
655 target
->running_alg
= true;
656 retval
= target
->type
->run_algorithm(target
,
657 num_mem_params
, mem_params
,
658 num_reg_params
, reg_param
,
659 entry_point
, exit_point
, timeout_ms
, arch_info
);
660 target
->running_alg
= false;
667 int target_read_memory(struct target
*target
,
668 uint32_t address
, uint32_t size
, uint32_t count
, uint8_t *buffer
)
670 return target
->type
->read_memory(target
, address
, size
, count
, buffer
);
673 static int target_read_phys_memory(struct target
*target
,
674 uint32_t address
, uint32_t size
, uint32_t count
, uint8_t *buffer
)
676 return target
->type
->read_phys_memory(target
, address
, size
, count
, buffer
);
679 int target_write_memory(struct target
*target
,
680 uint32_t address
, uint32_t size
, uint32_t count
, uint8_t *buffer
)
682 return target
->type
->write_memory(target
, address
, size
, count
, buffer
);
685 static int target_write_phys_memory(struct target
*target
,
686 uint32_t address
, uint32_t size
, uint32_t count
, uint8_t *buffer
)
688 return target
->type
->write_phys_memory(target
, address
, size
, count
, buffer
);
691 int target_bulk_write_memory(struct target
*target
,
692 uint32_t address
, uint32_t count
, uint8_t *buffer
)
694 return target
->type
->bulk_write_memory(target
, address
, count
, buffer
);
697 int target_add_breakpoint(struct target
*target
,
698 struct breakpoint
*breakpoint
)
700 if (target
->state
!= TARGET_HALTED
) {
701 LOG_WARNING("target %s is not halted", target
->cmd_name
);
702 return ERROR_TARGET_NOT_HALTED
;
704 return target
->type
->add_breakpoint(target
, breakpoint
);
706 int target_remove_breakpoint(struct target
*target
,
707 struct breakpoint
*breakpoint
)
709 return target
->type
->remove_breakpoint(target
, breakpoint
);
712 int target_add_watchpoint(struct target
*target
,
713 struct watchpoint
*watchpoint
)
715 if (target
->state
!= TARGET_HALTED
) {
716 LOG_WARNING("target %s is not halted", target
->cmd_name
);
717 return ERROR_TARGET_NOT_HALTED
;
719 return target
->type
->add_watchpoint(target
, watchpoint
);
721 int target_remove_watchpoint(struct target
*target
,
722 struct watchpoint
*watchpoint
)
724 return target
->type
->remove_watchpoint(target
, watchpoint
);
727 int target_get_gdb_reg_list(struct target
*target
,
728 struct reg
**reg_list
[], int *reg_list_size
)
730 return target
->type
->get_gdb_reg_list(target
, reg_list
, reg_list_size
);
732 int target_step(struct target
*target
,
733 int current
, uint32_t address
, int handle_breakpoints
)
735 return target
->type
->step(target
, current
, address
, handle_breakpoints
);
740 * Reset the @c examined flag for the given target.
741 * Pure paranoia -- targets are zeroed on allocation.
743 static void target_reset_examined(struct target
*target
)
745 target
->examined
= false;
749 err_read_phys_memory(struct target
*target
, uint32_t address
,
750 uint32_t size
, uint32_t count
, uint8_t *buffer
)
752 LOG_ERROR("Not implemented: %s", __func__
);
757 err_write_phys_memory(struct target
*target
, uint32_t address
,
758 uint32_t size
, uint32_t count
, uint8_t *buffer
)
760 LOG_ERROR("Not implemented: %s", __func__
);
764 static int handle_target(void *priv
);
766 static int target_init_one(struct command_context
*cmd_ctx
,
767 struct target
*target
)
769 target_reset_examined(target
);
771 struct target_type
*type
= target
->type
;
772 if (type
->examine
== NULL
)
773 type
->examine
= default_examine
;
775 if (type
->check_reset
== NULL
)
776 type
->check_reset
= default_check_reset
;
778 int retval
= type
->init_target(cmd_ctx
, target
);
779 if (ERROR_OK
!= retval
)
781 LOG_ERROR("target '%s' init failed", target_name(target
));
786 * @todo get rid of those *memory_imp() methods, now that all
787 * callers are using target_*_memory() accessors ... and make
788 * sure the "physical" paths handle the same issues.
790 /* a non-invasive way(in terms of patches) to add some code that
791 * runs before the type->write/read_memory implementation
793 type
->write_memory_imp
= target
->type
->write_memory
;
794 type
->write_memory
= target_write_memory_imp
;
796 type
->read_memory_imp
= target
->type
->read_memory
;
797 type
->read_memory
= target_read_memory_imp
;
799 type
->soft_reset_halt_imp
= target
->type
->soft_reset_halt
;
800 type
->soft_reset_halt
= target_soft_reset_halt_imp
;
802 /* Sanity-check MMU support ... stub in what we must, to help
803 * implement it in stages, but warn if we need to do so.
807 if (type
->write_phys_memory
== NULL
)
809 LOG_ERROR("type '%s' is missing write_phys_memory",
811 type
->write_phys_memory
= err_write_phys_memory
;
813 if (type
->read_phys_memory
== NULL
)
815 LOG_ERROR("type '%s' is missing read_phys_memory",
817 type
->read_phys_memory
= err_read_phys_memory
;
819 if (type
->virt2phys
== NULL
)
821 LOG_ERROR("type '%s' is missing virt2phys", type
->name
);
822 type
->virt2phys
= identity_virt2phys
;
827 /* Make sure no-MMU targets all behave the same: make no
828 * distinction between physical and virtual addresses, and
829 * ensure that virt2phys() is always an identity mapping.
831 if (type
->write_phys_memory
|| type
->read_phys_memory
834 LOG_WARNING("type '%s' has bad MMU hooks", type
->name
);
838 type
->write_phys_memory
= type
->write_memory
;
839 type
->read_phys_memory
= type
->read_memory
;
840 type
->virt2phys
= identity_virt2phys
;
845 static int target_init(struct command_context
*cmd_ctx
)
847 struct target
*target
;
850 for (target
= all_targets
; target
; target
= target
->next
)
852 retval
= target_init_one(cmd_ctx
, target
);
853 if (ERROR_OK
!= retval
)
860 retval
= target_register_user_commands(cmd_ctx
);
861 if (ERROR_OK
!= retval
)
864 retval
= target_register_timer_callback(&handle_target
,
865 100, 1, cmd_ctx
->interp
);
866 if (ERROR_OK
!= retval
)
872 COMMAND_HANDLER(handle_target_init_command
)
875 return ERROR_COMMAND_SYNTAX_ERROR
;
877 static bool target_initialized
= false;
878 if (target_initialized
)
880 LOG_INFO("'target init' has already been called");
883 target_initialized
= true;
885 LOG_DEBUG("Initializing targets...");
886 return target_init(CMD_CTX
);
889 int target_register_event_callback(int (*callback
)(struct target
*target
, enum target_event event
, void *priv
), void *priv
)
891 struct target_event_callback
**callbacks_p
= &target_event_callbacks
;
893 if (callback
== NULL
)
895 return ERROR_INVALID_ARGUMENTS
;
900 while ((*callbacks_p
)->next
)
901 callbacks_p
= &((*callbacks_p
)->next
);
902 callbacks_p
= &((*callbacks_p
)->next
);
905 (*callbacks_p
) = malloc(sizeof(struct target_event_callback
));
906 (*callbacks_p
)->callback
= callback
;
907 (*callbacks_p
)->priv
= priv
;
908 (*callbacks_p
)->next
= NULL
;
913 int target_register_timer_callback(int (*callback
)(void *priv
), int time_ms
, int periodic
, void *priv
)
915 struct target_timer_callback
**callbacks_p
= &target_timer_callbacks
;
918 if (callback
== NULL
)
920 return ERROR_INVALID_ARGUMENTS
;
925 while ((*callbacks_p
)->next
)
926 callbacks_p
= &((*callbacks_p
)->next
);
927 callbacks_p
= &((*callbacks_p
)->next
);
930 (*callbacks_p
) = malloc(sizeof(struct target_timer_callback
));
931 (*callbacks_p
)->callback
= callback
;
932 (*callbacks_p
)->periodic
= periodic
;
933 (*callbacks_p
)->time_ms
= time_ms
;
935 gettimeofday(&now
, NULL
);
936 (*callbacks_p
)->when
.tv_usec
= now
.tv_usec
+ (time_ms
% 1000) * 1000;
937 time_ms
-= (time_ms
% 1000);
938 (*callbacks_p
)->when
.tv_sec
= now
.tv_sec
+ (time_ms
/ 1000);
939 if ((*callbacks_p
)->when
.tv_usec
> 1000000)
941 (*callbacks_p
)->when
.tv_usec
= (*callbacks_p
)->when
.tv_usec
- 1000000;
942 (*callbacks_p
)->when
.tv_sec
+= 1;
945 (*callbacks_p
)->priv
= priv
;
946 (*callbacks_p
)->next
= NULL
;
951 int target_unregister_event_callback(int (*callback
)(struct target
*target
, enum target_event event
, void *priv
), void *priv
)
953 struct target_event_callback
**p
= &target_event_callbacks
;
954 struct target_event_callback
*c
= target_event_callbacks
;
956 if (callback
== NULL
)
958 return ERROR_INVALID_ARGUMENTS
;
963 struct target_event_callback
*next
= c
->next
;
964 if ((c
->callback
== callback
) && (c
->priv
== priv
))
978 static int target_unregister_timer_callback(int (*callback
)(void *priv
), void *priv
)
980 struct target_timer_callback
**p
= &target_timer_callbacks
;
981 struct target_timer_callback
*c
= target_timer_callbacks
;
983 if (callback
== NULL
)
985 return ERROR_INVALID_ARGUMENTS
;
990 struct target_timer_callback
*next
= c
->next
;
991 if ((c
->callback
== callback
) && (c
->priv
== priv
))
1005 int target_call_event_callbacks(struct target
*target
, enum target_event event
)
1007 struct target_event_callback
*callback
= target_event_callbacks
;
1008 struct target_event_callback
*next_callback
;
1010 if (event
== TARGET_EVENT_HALTED
)
1012 /* execute early halted first */
1013 target_call_event_callbacks(target
, TARGET_EVENT_GDB_HALT
);
1016 LOG_DEBUG("target event %i (%s)",
1018 Jim_Nvp_value2name_simple(nvp_target_event
, event
)->name
);
1020 target_handle_event(target
, event
);
1024 next_callback
= callback
->next
;
1025 callback
->callback(target
, event
, callback
->priv
);
1026 callback
= next_callback
;
1032 static int target_timer_callback_periodic_restart(
1033 struct target_timer_callback
*cb
, struct timeval
*now
)
1035 int time_ms
= cb
->time_ms
;
1036 cb
->when
.tv_usec
= now
->tv_usec
+ (time_ms
% 1000) * 1000;
1037 time_ms
-= (time_ms
% 1000);
1038 cb
->when
.tv_sec
= now
->tv_sec
+ time_ms
/ 1000;
1039 if (cb
->when
.tv_usec
> 1000000)
1041 cb
->when
.tv_usec
= cb
->when
.tv_usec
- 1000000;
1042 cb
->when
.tv_sec
+= 1;
1047 static int target_call_timer_callback(struct target_timer_callback
*cb
,
1048 struct timeval
*now
)
1050 cb
->callback(cb
->priv
);
1053 return target_timer_callback_periodic_restart(cb
, now
);
1055 return target_unregister_timer_callback(cb
->callback
, cb
->priv
);
1058 static int target_call_timer_callbacks_check_time(int checktime
)
1063 gettimeofday(&now
, NULL
);
1065 struct target_timer_callback
*callback
= target_timer_callbacks
;
1068 // cleaning up may unregister and free this callback
1069 struct target_timer_callback
*next_callback
= callback
->next
;
1071 bool call_it
= callback
->callback
&&
1072 ((!checktime
&& callback
->periodic
) ||
1073 now
.tv_sec
> callback
->when
.tv_sec
||
1074 (now
.tv_sec
== callback
->when
.tv_sec
&&
1075 now
.tv_usec
>= callback
->when
.tv_usec
));
1079 int retval
= target_call_timer_callback(callback
, &now
);
1080 if (retval
!= ERROR_OK
)
1084 callback
= next_callback
;
1090 int target_call_timer_callbacks(void)
1092 return target_call_timer_callbacks_check_time(1);
1095 /* invoke periodic callbacks immediately */
1096 int target_call_timer_callbacks_now(void)
1098 return target_call_timer_callbacks_check_time(0);
1101 int target_alloc_working_area_try(struct target
*target
, uint32_t size
, struct working_area
**area
)
1103 struct working_area
*c
= target
->working_areas
;
1104 struct working_area
*new_wa
= NULL
;
1106 /* Reevaluate working area address based on MMU state*/
1107 if (target
->working_areas
== NULL
)
1112 retval
= target
->type
->mmu(target
, &enabled
);
1113 if (retval
!= ERROR_OK
)
1119 if (target
->working_area_phys_spec
) {
1120 LOG_DEBUG("MMU disabled, using physical "
1121 "address for working memory 0x%08x",
1122 (unsigned)target
->working_area_phys
);
1123 target
->working_area
= target
->working_area_phys
;
1125 LOG_ERROR("No working memory available. "
1126 "Specify -work-area-phys to target.");
1127 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
1130 if (target
->working_area_virt_spec
) {
1131 LOG_DEBUG("MMU enabled, using virtual "
1132 "address for working memory 0x%08x",
1133 (unsigned)target
->working_area_virt
);
1134 target
->working_area
= target
->working_area_virt
;
1136 LOG_ERROR("No working memory available. "
1137 "Specify -work-area-virt to target.");
1138 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
1143 /* only allocate multiples of 4 byte */
1146 LOG_ERROR("BUG: code tried to allocate unaligned number of bytes (0x%08x), padding", ((unsigned)(size
)));
1147 size
= (size
+ 3) & (~3);
1150 /* see if there's already a matching working area */
1153 if ((c
->free
) && (c
->size
== size
))
1161 /* if not, allocate a new one */
1164 struct working_area
**p
= &target
->working_areas
;
1165 uint32_t first_free
= target
->working_area
;
1166 uint32_t free_size
= target
->working_area_size
;
1168 c
= target
->working_areas
;
1171 first_free
+= c
->size
;
1172 free_size
-= c
->size
;
1177 if (free_size
< size
)
1179 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
1182 LOG_DEBUG("allocated new working area at address 0x%08x", (unsigned)first_free
);
1184 new_wa
= malloc(sizeof(struct working_area
));
1185 new_wa
->next
= NULL
;
1186 new_wa
->size
= size
;
1187 new_wa
->address
= first_free
;
1189 if (target
->backup_working_area
)
1192 new_wa
->backup
= malloc(new_wa
->size
);
1193 if ((retval
= target_read_memory(target
, new_wa
->address
, 4, new_wa
->size
/ 4, new_wa
->backup
)) != ERROR_OK
)
1195 free(new_wa
->backup
);
1202 new_wa
->backup
= NULL
;
1205 /* put new entry in list */
1209 /* mark as used, and return the new (reused) area */
1214 new_wa
->user
= area
;
1219 int target_alloc_working_area(struct target
*target
, uint32_t size
, struct working_area
**area
)
1223 retval
= target_alloc_working_area_try(target
, size
, area
);
1224 if (retval
== ERROR_TARGET_RESOURCE_NOT_AVAILABLE
)
1226 LOG_WARNING("not enough working area available(requested %u)", (unsigned)(size
));
1232 static int target_free_working_area_restore(struct target
*target
, struct working_area
*area
, int restore
)
1237 if (restore
&& target
->backup_working_area
)
1240 if ((retval
= target_write_memory(target
, area
->address
, 4, area
->size
/ 4, area
->backup
)) != ERROR_OK
)
1246 /* mark user pointer invalid */
1253 int target_free_working_area(struct target
*target
, struct working_area
*area
)
1255 return target_free_working_area_restore(target
, area
, 1);
1258 /* free resources and restore memory, if restoring memory fails,
1259 * free up resources anyway
1261 static void target_free_all_working_areas_restore(struct target
*target
, int restore
)
1263 struct working_area
*c
= target
->working_areas
;
1267 struct working_area
*next
= c
->next
;
1268 target_free_working_area_restore(target
, c
, restore
);
1278 target
->working_areas
= NULL
;
1281 void target_free_all_working_areas(struct target
*target
)
1283 target_free_all_working_areas_restore(target
, 1);
1286 int target_arch_state(struct target
*target
)
1291 LOG_USER("No target has been configured");
1295 LOG_USER("target state: %s", target_state_name( target
));
1297 if (target
->state
!= TARGET_HALTED
)
1300 retval
= target
->type
->arch_state(target
);
1304 /* Single aligned words are guaranteed to use 16 or 32 bit access
1305 * mode respectively, otherwise data is handled as quickly as
1308 int target_write_buffer(struct target
*target
, uint32_t address
, uint32_t size
, uint8_t *buffer
)
1311 LOG_DEBUG("writing buffer of %i byte at 0x%8.8x",
1312 (int)size
, (unsigned)address
);
1314 if (!target_was_examined(target
))
1316 LOG_ERROR("Target not examined yet");
1324 if ((address
+ size
- 1) < address
)
1326 /* GDB can request this when e.g. PC is 0xfffffffc*/
1327 LOG_ERROR("address + size wrapped(0x%08x, 0x%08x)",
1333 if (((address
% 2) == 0) && (size
== 2))
1335 return target_write_memory(target
, address
, 2, 1, buffer
);
1338 /* handle unaligned head bytes */
1341 uint32_t unaligned
= 4 - (address
% 4);
1343 if (unaligned
> size
)
1346 if ((retval
= target_write_memory(target
, address
, 1, unaligned
, buffer
)) != ERROR_OK
)
1349 buffer
+= unaligned
;
1350 address
+= unaligned
;
1354 /* handle aligned words */
1357 int aligned
= size
- (size
% 4);
1359 /* use bulk writes above a certain limit. This may have to be changed */
1362 if ((retval
= target
->type
->bulk_write_memory(target
, address
, aligned
/ 4, buffer
)) != ERROR_OK
)
1367 if ((retval
= target_write_memory(target
, address
, 4, aligned
/ 4, buffer
)) != ERROR_OK
)
1376 /* handle tail writes of less than 4 bytes */
1379 if ((retval
= target_write_memory(target
, address
, 1, size
, buffer
)) != ERROR_OK
)
1386 /* Single aligned words are guaranteed to use 16 or 32 bit access
1387 * mode respectively, otherwise data is handled as quickly as
1390 int target_read_buffer(struct target
*target
, uint32_t address
, uint32_t size
, uint8_t *buffer
)
1393 LOG_DEBUG("reading buffer of %i byte at 0x%8.8x",
1394 (int)size
, (unsigned)address
);
1396 if (!target_was_examined(target
))
1398 LOG_ERROR("Target not examined yet");
1406 if ((address
+ size
- 1) < address
)
1408 /* GDB can request this when e.g. PC is 0xfffffffc*/
1409 LOG_ERROR("address + size wrapped(0x%08" PRIx32
", 0x%08" PRIx32
")",
1415 if (((address
% 2) == 0) && (size
== 2))
1417 return target_read_memory(target
, address
, 2, 1, buffer
);
1420 /* handle unaligned head bytes */
1423 uint32_t unaligned
= 4 - (address
% 4);
1425 if (unaligned
> size
)
1428 if ((retval
= target_read_memory(target
, address
, 1, unaligned
, buffer
)) != ERROR_OK
)
1431 buffer
+= unaligned
;
1432 address
+= unaligned
;
1436 /* handle aligned words */
1439 int aligned
= size
- (size
% 4);
1441 if ((retval
= target_read_memory(target
, address
, 4, aligned
/ 4, buffer
)) != ERROR_OK
)
1449 /*prevent byte access when possible (avoid AHB access limitations in some cases)*/
1452 int aligned
= size
- (size
%2);
1453 retval
= target_read_memory(target
, address
, 2, aligned
/ 2, buffer
);
1454 if (retval
!= ERROR_OK
)
1461 /* handle tail writes of less than 4 bytes */
1464 if ((retval
= target_read_memory(target
, address
, 1, size
, buffer
)) != ERROR_OK
)
1471 int target_checksum_memory(struct target
*target
, uint32_t address
, uint32_t size
, uint32_t* crc
)
1476 uint32_t checksum
= 0;
1477 if (!target_was_examined(target
))
1479 LOG_ERROR("Target not examined yet");
1483 if ((retval
= target
->type
->checksum_memory(target
, address
,
1484 size
, &checksum
)) != ERROR_OK
)
1486 buffer
= malloc(size
);
1489 LOG_ERROR("error allocating buffer for section (%d bytes)", (int)size
);
1490 return ERROR_INVALID_ARGUMENTS
;
1492 retval
= target_read_buffer(target
, address
, size
, buffer
);
1493 if (retval
!= ERROR_OK
)
1499 /* convert to target endianess */
1500 for (i
= 0; i
< (size
/sizeof(uint32_t)); i
++)
1502 uint32_t target_data
;
1503 target_data
= target_buffer_get_u32(target
, &buffer
[i
*sizeof(uint32_t)]);
1504 target_buffer_set_u32(target
, &buffer
[i
*sizeof(uint32_t)], target_data
);
1507 retval
= image_calculate_checksum(buffer
, size
, &checksum
);
1516 int target_blank_check_memory(struct target
*target
, uint32_t address
, uint32_t size
, uint32_t* blank
)
1519 if (!target_was_examined(target
))
1521 LOG_ERROR("Target not examined yet");
1525 if (target
->type
->blank_check_memory
== 0)
1526 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
1528 retval
= target
->type
->blank_check_memory(target
, address
, size
, blank
);
1533 int target_read_u32(struct target
*target
, uint32_t address
, uint32_t *value
)
1535 uint8_t value_buf
[4];
1536 if (!target_was_examined(target
))
1538 LOG_ERROR("Target not examined yet");
1542 int retval
= target_read_memory(target
, address
, 4, 1, value_buf
);
1544 if (retval
== ERROR_OK
)
1546 *value
= target_buffer_get_u32(target
, value_buf
);
1547 LOG_DEBUG("address: 0x%8.8" PRIx32
", value: 0x%8.8" PRIx32
"",
1554 LOG_DEBUG("address: 0x%8.8" PRIx32
" failed",
1561 int target_read_u16(struct target
*target
, uint32_t address
, uint16_t *value
)
1563 uint8_t value_buf
[2];
1564 if (!target_was_examined(target
))
1566 LOG_ERROR("Target not examined yet");
1570 int retval
= target_read_memory(target
, address
, 2, 1, value_buf
);
1572 if (retval
== ERROR_OK
)
1574 *value
= target_buffer_get_u16(target
, value_buf
);
1575 LOG_DEBUG("address: 0x%8.8" PRIx32
", value: 0x%4.4x",
1582 LOG_DEBUG("address: 0x%8.8" PRIx32
" failed",
1589 int target_read_u8(struct target
*target
, uint32_t address
, uint8_t *value
)
1591 int retval
= target_read_memory(target
, address
, 1, 1, value
);
1592 if (!target_was_examined(target
))
1594 LOG_ERROR("Target not examined yet");
1598 if (retval
== ERROR_OK
)
1600 LOG_DEBUG("address: 0x%8.8" PRIx32
", value: 0x%2.2x",
1607 LOG_DEBUG("address: 0x%8.8" PRIx32
" failed",
1614 int target_write_u32(struct target
*target
, uint32_t address
, uint32_t value
)
1617 uint8_t value_buf
[4];
1618 if (!target_was_examined(target
))
1620 LOG_ERROR("Target not examined yet");
1624 LOG_DEBUG("address: 0x%8.8" PRIx32
", value: 0x%8.8" PRIx32
"",
1628 target_buffer_set_u32(target
, value_buf
, value
);
1629 if ((retval
= target_write_memory(target
, address
, 4, 1, value_buf
)) != ERROR_OK
)
1631 LOG_DEBUG("failed: %i", retval
);
1637 int target_write_u16(struct target
*target
, uint32_t address
, uint16_t value
)
1640 uint8_t value_buf
[2];
1641 if (!target_was_examined(target
))
1643 LOG_ERROR("Target not examined yet");
1647 LOG_DEBUG("address: 0x%8.8" PRIx32
", value: 0x%8.8x",
1651 target_buffer_set_u16(target
, value_buf
, value
);
1652 if ((retval
= target_write_memory(target
, address
, 2, 1, value_buf
)) != ERROR_OK
)
1654 LOG_DEBUG("failed: %i", retval
);
1660 int target_write_u8(struct target
*target
, uint32_t address
, uint8_t value
)
1663 if (!target_was_examined(target
))
1665 LOG_ERROR("Target not examined yet");
1669 LOG_DEBUG("address: 0x%8.8" PRIx32
", value: 0x%2.2x",
1672 if ((retval
= target_write_memory(target
, address
, 1, 1, &value
)) != ERROR_OK
)
1674 LOG_DEBUG("failed: %i", retval
);
1680 COMMAND_HANDLER(handle_targets_command
)
1682 struct target
*target
= all_targets
;
1686 target
= get_target(CMD_ARGV
[0]);
1687 if (target
== NULL
) {
1688 command_print(CMD_CTX
,"Target: %s is unknown, try one of:\n", CMD_ARGV
[0]);
1691 if (!target
->tap
->enabled
) {
1692 command_print(CMD_CTX
,"Target: TAP %s is disabled, "
1693 "can't be the current target\n",
1694 target
->tap
->dotted_name
);
1698 CMD_CTX
->current_target
= target
->target_number
;
1703 target
= all_targets
;
1704 command_print(CMD_CTX
, " TargetName Type Endian TapName State ");
1705 command_print(CMD_CTX
, "-- ------------------ ---------- ------ ------------------ ------------");
1711 if (target
->tap
->enabled
)
1712 state
= target_state_name( target
);
1714 state
= "tap-disabled";
1716 if (CMD_CTX
->current_target
== target
->target_number
)
1719 /* keep columns lined up to match the headers above */
1720 command_print(CMD_CTX
, "%2d%c %-18s %-10s %-6s %-18s %s",
1721 target
->target_number
,
1723 target_name(target
),
1724 target_type_name(target
),
1725 Jim_Nvp_value2name_simple(nvp_target_endian
,
1726 target
->endianness
)->name
,
1727 target
->tap
->dotted_name
,
1729 target
= target
->next
;
1735 /* every 300ms we check for reset & powerdropout and issue a "reset halt" if so. */
1737 static int powerDropout
;
1738 static int srstAsserted
;
1740 static int runPowerRestore
;
1741 static int runPowerDropout
;
1742 static int runSrstAsserted
;
1743 static int runSrstDeasserted
;
1745 static int sense_handler(void)
1747 static int prevSrstAsserted
= 0;
1748 static int prevPowerdropout
= 0;
1751 if ((retval
= jtag_power_dropout(&powerDropout
)) != ERROR_OK
)
1755 powerRestored
= prevPowerdropout
&& !powerDropout
;
1758 runPowerRestore
= 1;
1761 long long current
= timeval_ms();
1762 static long long lastPower
= 0;
1763 int waitMore
= lastPower
+ 2000 > current
;
1764 if (powerDropout
&& !waitMore
)
1766 runPowerDropout
= 1;
1767 lastPower
= current
;
1770 if ((retval
= jtag_srst_asserted(&srstAsserted
)) != ERROR_OK
)
1774 srstDeasserted
= prevSrstAsserted
&& !srstAsserted
;
1776 static long long lastSrst
= 0;
1777 waitMore
= lastSrst
+ 2000 > current
;
1778 if (srstDeasserted
&& !waitMore
)
1780 runSrstDeasserted
= 1;
1784 if (!prevSrstAsserted
&& srstAsserted
)
1786 runSrstAsserted
= 1;
1789 prevSrstAsserted
= srstAsserted
;
1790 prevPowerdropout
= powerDropout
;
1792 if (srstDeasserted
|| powerRestored
)
1794 /* Other than logging the event we can't do anything here.
1795 * Issuing a reset is a particularly bad idea as we might
1796 * be inside a reset already.
1803 /* process target state changes */
1804 static int handle_target(void *priv
)
1806 Jim_Interp
*interp
= (Jim_Interp
*)priv
;
1807 int retval
= ERROR_OK
;
1809 if (!is_jtag_poll_safe())
1811 /* polling is disabled currently */
1815 /* we do not want to recurse here... */
1816 static int recursive
= 0;
1821 /* danger! running these procedures can trigger srst assertions and power dropouts.
1822 * We need to avoid an infinite loop/recursion here and we do that by
1823 * clearing the flags after running these events.
1825 int did_something
= 0;
1826 if (runSrstAsserted
)
1828 LOG_INFO("srst asserted detected, running srst_asserted proc.");
1829 Jim_Eval(interp
, "srst_asserted");
1832 if (runSrstDeasserted
)
1834 Jim_Eval(interp
, "srst_deasserted");
1837 if (runPowerDropout
)
1839 LOG_INFO("Power dropout detected, running power_dropout proc.");
1840 Jim_Eval(interp
, "power_dropout");
1843 if (runPowerRestore
)
1845 Jim_Eval(interp
, "power_restore");
1851 /* clear detect flags */
1855 /* clear action flags */
1857 runSrstAsserted
= 0;
1858 runSrstDeasserted
= 0;
1859 runPowerRestore
= 0;
1860 runPowerDropout
= 0;
1865 /* Poll targets for state changes unless that's globally disabled.
1866 * Skip targets that are currently disabled.
1868 for (struct target
*target
= all_targets
;
1869 is_jtag_poll_safe() && target
;
1870 target
= target
->next
)
1872 if (!target
->tap
->enabled
)
1875 /* only poll target if we've got power and srst isn't asserted */
1876 if (!powerDropout
&& !srstAsserted
)
1878 /* polling may fail silently until the target has been examined */
1879 if ((retval
= target_poll(target
)) != ERROR_OK
)
1881 /* FIX!!!!! If we add a LOG_INFO() here to output a line in GDB
1882 * *why* we are aborting GDB, then we'll spam telnet when the
1883 * poll is failing persistently.
1885 * If we could implement an event that detected the
1886 * target going from non-pollable to pollable, we could issue
1887 * an error only upon the transition.
1889 target_call_event_callbacks(target
, TARGET_EVENT_GDB_HALT
);
1898 COMMAND_HANDLER(handle_reg_command
)
1900 struct target
*target
;
1901 struct reg
*reg
= NULL
;
1907 target
= get_current_target(CMD_CTX
);
1909 /* list all available registers for the current target */
1912 struct reg_cache
*cache
= target
->reg_cache
;
1919 command_print(CMD_CTX
, "===== %s", cache
->name
);
1921 for (i
= 0, reg
= cache
->reg_list
;
1922 i
< cache
->num_regs
;
1923 i
++, reg
++, count
++)
1925 /* only print cached values if they are valid */
1927 value
= buf_to_str(reg
->value
,
1929 command_print(CMD_CTX
,
1930 "(%i) %s (/%" PRIu32
"): 0x%s%s",
1938 command_print(CMD_CTX
, "(%i) %s (/%" PRIu32
")",
1943 cache
= cache
->next
;
1949 /* access a single register by its ordinal number */
1950 if ((CMD_ARGV
[0][0] >= '0') && (CMD_ARGV
[0][0] <= '9'))
1953 COMMAND_PARSE_NUMBER(uint
, CMD_ARGV
[0], num
);
1955 struct reg_cache
*cache
= target
->reg_cache
;
1960 for (i
= 0; i
< cache
->num_regs
; i
++)
1964 reg
= &cache
->reg_list
[i
];
1970 cache
= cache
->next
;
1975 command_print(CMD_CTX
, "%i is out of bounds, the current target has only %i registers (0 - %i)", num
, count
, count
- 1);
1978 } else /* access a single register by its name */
1980 reg
= register_get_by_name(target
->reg_cache
, CMD_ARGV
[0], 1);
1984 command_print(CMD_CTX
, "register %s not found in current target", CMD_ARGV
[0]);
1989 /* display a register */
1990 if ((CMD_ARGC
== 1) || ((CMD_ARGC
== 2) && !((CMD_ARGV
[1][0] >= '0') && (CMD_ARGV
[1][0] <= '9'))))
1992 if ((CMD_ARGC
== 2) && (strcmp(CMD_ARGV
[1], "force") == 0))
1995 if (reg
->valid
== 0)
1997 reg
->type
->get(reg
);
1999 value
= buf_to_str(reg
->value
, reg
->size
, 16);
2000 command_print(CMD_CTX
, "%s (/%i): 0x%s", reg
->name
, (int)(reg
->size
), value
);
2005 /* set register value */
2008 uint8_t *buf
= malloc(DIV_ROUND_UP(reg
->size
, 8));
2009 str_to_buf(CMD_ARGV
[1], strlen(CMD_ARGV
[1]), buf
, reg
->size
, 0);
2011 reg
->type
->set(reg
, buf
);
2013 value
= buf_to_str(reg
->value
, reg
->size
, 16);
2014 command_print(CMD_CTX
, "%s (/%i): 0x%s", reg
->name
, (int)(reg
->size
), value
);
2022 command_print(CMD_CTX
, "usage: reg <#|name> [value]");
2027 COMMAND_HANDLER(handle_poll_command
)
2029 int retval
= ERROR_OK
;
2030 struct target
*target
= get_current_target(CMD_CTX
);
2034 command_print(CMD_CTX
, "background polling: %s",
2035 jtag_poll_get_enabled() ? "on" : "off");
2036 command_print(CMD_CTX
, "TAP: %s (%s)",
2037 target
->tap
->dotted_name
,
2038 target
->tap
->enabled
? "enabled" : "disabled");
2039 if (!target
->tap
->enabled
)
2041 if ((retval
= target_poll(target
)) != ERROR_OK
)
2043 if ((retval
= target_arch_state(target
)) != ERROR_OK
)
2046 else if (CMD_ARGC
== 1)
2049 COMMAND_PARSE_ON_OFF(CMD_ARGV
[0], enable
);
2050 jtag_poll_set_enabled(enable
);
2054 return ERROR_COMMAND_SYNTAX_ERROR
;
2060 COMMAND_HANDLER(handle_wait_halt_command
)
2063 return ERROR_COMMAND_SYNTAX_ERROR
;
2068 int retval
= parse_uint(CMD_ARGV
[0], &ms
);
2069 if (ERROR_OK
!= retval
)
2071 command_print(CMD_CTX
, "usage: %s [seconds]", CMD_NAME
);
2072 return ERROR_COMMAND_SYNTAX_ERROR
;
2074 // convert seconds (given) to milliseconds (needed)
2078 struct target
*target
= get_current_target(CMD_CTX
);
2079 return target_wait_state(target
, TARGET_HALTED
, ms
);
2082 /* wait for target state to change. The trick here is to have a low
2083 * latency for short waits and not to suck up all the CPU time
2086 * After 500ms, keep_alive() is invoked
2088 int target_wait_state(struct target
*target
, enum target_state state
, int ms
)
2091 long long then
= 0, cur
;
2096 if ((retval
= target_poll(target
)) != ERROR_OK
)
2098 if (target
->state
== state
)
2106 then
= timeval_ms();
2107 LOG_DEBUG("waiting for target %s...",
2108 Jim_Nvp_value2name_simple(nvp_target_state
,state
)->name
);
2116 if ((cur
-then
) > ms
)
2118 LOG_ERROR("timed out while waiting for target %s",
2119 Jim_Nvp_value2name_simple(nvp_target_state
,state
)->name
);
2127 COMMAND_HANDLER(handle_halt_command
)
2131 struct target
*target
= get_current_target(CMD_CTX
);
2132 int retval
= target_halt(target
);
2133 if (ERROR_OK
!= retval
)
2139 retval
= parse_uint(CMD_ARGV
[0], &wait
);
2140 if (ERROR_OK
!= retval
)
2141 return ERROR_COMMAND_SYNTAX_ERROR
;
2146 return CALL_COMMAND_HANDLER(handle_wait_halt_command
);
2149 COMMAND_HANDLER(handle_soft_reset_halt_command
)
2151 struct target
*target
= get_current_target(CMD_CTX
);
2153 LOG_USER("requesting target halt and executing a soft reset");
2155 target
->type
->soft_reset_halt(target
);
2160 COMMAND_HANDLER(handle_reset_command
)
2163 return ERROR_COMMAND_SYNTAX_ERROR
;
2165 enum target_reset_mode reset_mode
= RESET_RUN
;
2169 n
= Jim_Nvp_name2value_simple(nvp_reset_modes
, CMD_ARGV
[0]);
2170 if ((n
->name
== NULL
) || (n
->value
== RESET_UNKNOWN
)) {
2171 return ERROR_COMMAND_SYNTAX_ERROR
;
2173 reset_mode
= n
->value
;
2176 /* reset *all* targets */
2177 return target_process_reset(CMD_CTX
, reset_mode
);
2181 COMMAND_HANDLER(handle_resume_command
)
2185 return ERROR_COMMAND_SYNTAX_ERROR
;
2187 struct target
*target
= get_current_target(CMD_CTX
);
2188 target_handle_event(target
, TARGET_EVENT_OLD_pre_resume
);
2190 /* with no CMD_ARGV, resume from current pc, addr = 0,
2191 * with one arguments, addr = CMD_ARGV[0],
2192 * handle breakpoints, not debugging */
2196 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], addr
);
2200 return target_resume(target
, current
, addr
, 1, 0);
2203 COMMAND_HANDLER(handle_step_command
)
2206 return ERROR_COMMAND_SYNTAX_ERROR
;
2210 /* with no CMD_ARGV, step from current pc, addr = 0,
2211 * with one argument addr = CMD_ARGV[0],
2212 * handle breakpoints, debugging */
2217 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], addr
);
2221 struct target
*target
= get_current_target(CMD_CTX
);
2223 return target
->type
->step(target
, current_pc
, addr
, 1);
2226 static void handle_md_output(struct command_context
*cmd_ctx
,
2227 struct target
*target
, uint32_t address
, unsigned size
,
2228 unsigned count
, const uint8_t *buffer
)
2230 const unsigned line_bytecnt
= 32;
2231 unsigned line_modulo
= line_bytecnt
/ size
;
2233 char output
[line_bytecnt
* 4 + 1];
2234 unsigned output_len
= 0;
2236 const char *value_fmt
;
2238 case 4: value_fmt
= "%8.8x "; break;
2239 case 2: value_fmt
= "%4.4x "; break;
2240 case 1: value_fmt
= "%2.2x "; break;
2242 /* "can't happen", caller checked */
2243 LOG_ERROR("invalid memory read size: %u", size
);
2247 for (unsigned i
= 0; i
< count
; i
++)
2249 if (i
% line_modulo
== 0)
2251 output_len
+= snprintf(output
+ output_len
,
2252 sizeof(output
) - output_len
,
2254 (unsigned)(address
+ (i
*size
)));
2258 const uint8_t *value_ptr
= buffer
+ i
* size
;
2260 case 4: value
= target_buffer_get_u32(target
, value_ptr
); break;
2261 case 2: value
= target_buffer_get_u16(target
, value_ptr
); break;
2262 case 1: value
= *value_ptr
;
2264 output_len
+= snprintf(output
+ output_len
,
2265 sizeof(output
) - output_len
,
2268 if ((i
% line_modulo
== line_modulo
- 1) || (i
== count
- 1))
2270 command_print(cmd_ctx
, "%s", output
);
2276 COMMAND_HANDLER(handle_md_command
)
2279 return ERROR_COMMAND_SYNTAX_ERROR
;
2282 switch (CMD_NAME
[2]) {
2283 case 'w': size
= 4; break;
2284 case 'h': size
= 2; break;
2285 case 'b': size
= 1; break;
2286 default: return ERROR_COMMAND_SYNTAX_ERROR
;
2289 bool physical
=strcmp(CMD_ARGV
[0], "phys")==0;
2290 int (*fn
)(struct target
*target
,
2291 uint32_t address
, uint32_t size
, uint32_t count
, uint8_t *buffer
);
2296 fn
=target_read_phys_memory
;
2299 fn
=target_read_memory
;
2301 if ((CMD_ARGC
< 1) || (CMD_ARGC
> 2))
2303 return ERROR_COMMAND_SYNTAX_ERROR
;
2307 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], address
);
2311 COMMAND_PARSE_NUMBER(uint
, CMD_ARGV
[1], count
);
2313 uint8_t *buffer
= calloc(count
, size
);
2315 struct target
*target
= get_current_target(CMD_CTX
);
2316 int retval
= fn(target
, address
, size
, count
, buffer
);
2317 if (ERROR_OK
== retval
)
2318 handle_md_output(CMD_CTX
, target
, address
, size
, count
, buffer
);
2325 typedef int (*target_write_fn
)(struct target
*target
,
2326 uint32_t address
, uint32_t size
, uint32_t count
, uint8_t *buffer
);
2328 static int target_write_memory_fast(struct target
*target
,
2329 uint32_t address
, uint32_t size
, uint32_t count
, uint8_t *buffer
)
2331 return target_write_buffer(target
, address
, size
* count
, buffer
);
2334 static int target_fill_mem(struct target
*target
,
2343 /* We have to write in reasonably large chunks to be able
2344 * to fill large memory areas with any sane speed */
2345 const unsigned chunk_size
= 16384;
2346 uint8_t *target_buf
= malloc(chunk_size
* data_size
);
2347 if (target_buf
== NULL
)
2349 LOG_ERROR("Out of memory");
2353 for (unsigned i
= 0; i
< chunk_size
; i
++)
2358 target_buffer_set_u32(target
, target_buf
+ i
*data_size
, b
);
2361 target_buffer_set_u16(target
, target_buf
+ i
*data_size
, b
);
2364 target_buffer_set_u8(target
, target_buf
+ i
*data_size
, b
);
2371 int retval
= ERROR_OK
;
2373 for (unsigned x
= 0; x
< c
; x
+= chunk_size
)
2377 if (current
> chunk_size
)
2379 current
= chunk_size
;
2381 retval
= fn(target
, address
+ x
* data_size
, data_size
, current
, target_buf
);
2382 if (retval
!= ERROR_OK
)
2386 /* avoid GDB timeouts */
2395 COMMAND_HANDLER(handle_mw_command
)
2399 return ERROR_COMMAND_SYNTAX_ERROR
;
2401 bool physical
=strcmp(CMD_ARGV
[0], "phys")==0;
2407 fn
=target_write_phys_memory
;
2410 fn
= target_write_memory_fast
;
2412 if ((CMD_ARGC
< 2) || (CMD_ARGC
> 3))
2413 return ERROR_COMMAND_SYNTAX_ERROR
;
2416 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], address
);
2419 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], value
);
2423 COMMAND_PARSE_NUMBER(uint
, CMD_ARGV
[2], count
);
2425 struct target
*target
= get_current_target(CMD_CTX
);
2427 switch (CMD_NAME
[2])
2439 return ERROR_COMMAND_SYNTAX_ERROR
;
2442 return target_fill_mem(target
, address
, fn
, wordsize
, value
, count
);
2445 static COMMAND_HELPER(parse_load_image_command_CMD_ARGV
, struct image
*image
,
2446 uint32_t *min_address
, uint32_t *max_address
)
2448 if (CMD_ARGC
< 1 || CMD_ARGC
> 5)
2449 return ERROR_COMMAND_SYNTAX_ERROR
;
2451 /* a base address isn't always necessary,
2452 * default to 0x0 (i.e. don't relocate) */
2456 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], addr
);
2457 image
->base_address
= addr
;
2458 image
->base_address_set
= 1;
2461 image
->base_address_set
= 0;
2463 image
->start_address_set
= 0;
2467 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[3], *min_address
);
2471 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[4], *max_address
);
2472 // use size (given) to find max (required)
2473 *max_address
+= *min_address
;
2476 if (*min_address
> *max_address
)
2477 return ERROR_COMMAND_SYNTAX_ERROR
;
2482 COMMAND_HANDLER(handle_load_image_command
)
2486 uint32_t image_size
;
2487 uint32_t min_address
= 0;
2488 uint32_t max_address
= 0xffffffff;
2492 int retval
= CALL_COMMAND_HANDLER(parse_load_image_command_CMD_ARGV
,
2493 &image
, &min_address
, &max_address
);
2494 if (ERROR_OK
!= retval
)
2497 struct target
*target
= get_current_target(CMD_CTX
);
2499 struct duration bench
;
2500 duration_start(&bench
);
2502 if (image_open(&image
, CMD_ARGV
[0], (CMD_ARGC
>= 3) ? CMD_ARGV
[2] : NULL
) != ERROR_OK
)
2509 for (i
= 0; i
< image
.num_sections
; i
++)
2511 buffer
= malloc(image
.sections
[i
].size
);
2514 command_print(CMD_CTX
,
2515 "error allocating buffer for section (%d bytes)",
2516 (int)(image
.sections
[i
].size
));
2520 if ((retval
= image_read_section(&image
, i
, 0x0, image
.sections
[i
].size
, buffer
, &buf_cnt
)) != ERROR_OK
)
2526 uint32_t offset
= 0;
2527 uint32_t length
= buf_cnt
;
2529 /* DANGER!!! beware of unsigned comparision here!!! */
2531 if ((image
.sections
[i
].base_address
+ buf_cnt
>= min_address
)&&
2532 (image
.sections
[i
].base_address
< max_address
))
2534 if (image
.sections
[i
].base_address
< min_address
)
2536 /* clip addresses below */
2537 offset
+= min_address
-image
.sections
[i
].base_address
;
2541 if (image
.sections
[i
].base_address
+ buf_cnt
> max_address
)
2543 length
-= (image
.sections
[i
].base_address
+ buf_cnt
)-max_address
;
2546 if ((retval
= target_write_buffer(target
, image
.sections
[i
].base_address
+ offset
, length
, buffer
+ offset
)) != ERROR_OK
)
2551 image_size
+= length
;
2552 command_print(CMD_CTX
, "%u bytes written at address 0x%8.8" PRIx32
"",
2553 (unsigned int)length
,
2554 image
.sections
[i
].base_address
+ offset
);
2560 if ((ERROR_OK
== retval
) && (duration_measure(&bench
) == ERROR_OK
))
2562 command_print(CMD_CTX
, "downloaded %" PRIu32
" bytes "
2563 "in %fs (%0.3f KiB/s)", image_size
,
2564 duration_elapsed(&bench
), duration_kbps(&bench
, image_size
));
2567 image_close(&image
);
2573 COMMAND_HANDLER(handle_dump_image_command
)
2575 struct fileio fileio
;
2577 uint8_t buffer
[560];
2581 struct target
*target
= get_current_target(CMD_CTX
);
2585 command_print(CMD_CTX
, "usage: dump_image <filename> <address> <size>");
2590 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], address
);
2592 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[2], size
);
2594 if (fileio_open(&fileio
, CMD_ARGV
[0], FILEIO_WRITE
, FILEIO_BINARY
) != ERROR_OK
)
2599 struct duration bench
;
2600 duration_start(&bench
);
2602 int retval
= ERROR_OK
;
2605 size_t size_written
;
2606 uint32_t this_run_size
= (size
> 560) ? 560 : size
;
2607 retval
= target_read_buffer(target
, address
, this_run_size
, buffer
);
2608 if (retval
!= ERROR_OK
)
2613 retval
= fileio_write(&fileio
, this_run_size
, buffer
, &size_written
);
2614 if (retval
!= ERROR_OK
)
2619 size
-= this_run_size
;
2620 address
+= this_run_size
;
2623 if ((retvaltemp
= fileio_close(&fileio
)) != ERROR_OK
)
2626 if ((ERROR_OK
== retval
) && (duration_measure(&bench
) == ERROR_OK
))
2628 command_print(CMD_CTX
,
2629 "dumped %ld bytes in %fs (%0.3f KiB/s)", (long)fileio
.size
,
2630 duration_elapsed(&bench
), duration_kbps(&bench
, fileio
.size
));
2636 static COMMAND_HELPER(handle_verify_image_command_internal
, int verify
)
2640 uint32_t image_size
;
2643 uint32_t checksum
= 0;
2644 uint32_t mem_checksum
= 0;
2648 struct target
*target
= get_current_target(CMD_CTX
);
2652 return ERROR_COMMAND_SYNTAX_ERROR
;
2657 LOG_ERROR("no target selected");
2661 struct duration bench
;
2662 duration_start(&bench
);
2667 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], addr
);
2668 image
.base_address
= addr
;
2669 image
.base_address_set
= 1;
2673 image
.base_address_set
= 0;
2674 image
.base_address
= 0x0;
2677 image
.start_address_set
= 0;
2679 if ((retval
= image_open(&image
, CMD_ARGV
[0], (CMD_ARGC
== 3) ? CMD_ARGV
[2] : NULL
)) != ERROR_OK
)
2687 for (i
= 0; i
< image
.num_sections
; i
++)
2689 buffer
= malloc(image
.sections
[i
].size
);
2692 command_print(CMD_CTX
,
2693 "error allocating buffer for section (%d bytes)",
2694 (int)(image
.sections
[i
].size
));
2697 if ((retval
= image_read_section(&image
, i
, 0x0, image
.sections
[i
].size
, buffer
, &buf_cnt
)) != ERROR_OK
)
2705 /* calculate checksum of image */
2706 image_calculate_checksum(buffer
, buf_cnt
, &checksum
);
2708 retval
= target_checksum_memory(target
, image
.sections
[i
].base_address
, buf_cnt
, &mem_checksum
);
2709 if (retval
!= ERROR_OK
)
2715 if (checksum
!= mem_checksum
)
2717 /* failed crc checksum, fall back to a binary compare */
2722 LOG_ERROR("checksum mismatch - attempting binary compare");
2725 data
= (uint8_t*)malloc(buf_cnt
);
2727 /* Can we use 32bit word accesses? */
2729 int count
= buf_cnt
;
2730 if ((count
% 4) == 0)
2735 retval
= target_read_memory(target
, image
.sections
[i
].base_address
, size
, count
, data
);
2736 if (retval
== ERROR_OK
)
2739 for (t
= 0; t
< buf_cnt
; t
++)
2741 if (data
[t
] != buffer
[t
])
2743 command_print(CMD_CTX
,
2744 "diff %d address 0x%08x. Was 0x%02x instead of 0x%02x",
2746 (unsigned)(t
+ image
.sections
[i
].base_address
),
2751 command_print(CMD_CTX
, "More than 128 errors, the rest are not printed.");
2764 command_print(CMD_CTX
, "address 0x%08" PRIx32
" length 0x%08zx",
2765 image
.sections
[i
].base_address
,
2770 image_size
+= buf_cnt
;
2775 retval
= ERROR_FAIL
;
2777 if ((ERROR_OK
== retval
) && (duration_measure(&bench
) == ERROR_OK
))
2779 command_print(CMD_CTX
, "verified %" PRIu32
" bytes "
2780 "in %fs (%0.3f KiB/s)", image_size
,
2781 duration_elapsed(&bench
), duration_kbps(&bench
, image_size
));
2784 image_close(&image
);
2789 COMMAND_HANDLER(handle_verify_image_command
)
2791 return CALL_COMMAND_HANDLER(handle_verify_image_command_internal
, 1);
2794 COMMAND_HANDLER(handle_test_image_command
)
2796 return CALL_COMMAND_HANDLER(handle_verify_image_command_internal
, 0);
2799 static int handle_bp_command_list(struct command_context
*cmd_ctx
)
2801 struct target
*target
= get_current_target(cmd_ctx
);
2802 struct breakpoint
*breakpoint
= target
->breakpoints
;
2805 if (breakpoint
->type
== BKPT_SOFT
)
2807 char* buf
= buf_to_str(breakpoint
->orig_instr
,
2808 breakpoint
->length
, 16);
2809 command_print(cmd_ctx
, "0x%8.8" PRIx32
", 0x%x, %i, 0x%s",
2810 breakpoint
->address
,
2812 breakpoint
->set
, buf
);
2817 command_print(cmd_ctx
, "0x%8.8" PRIx32
", 0x%x, %i",
2818 breakpoint
->address
,
2819 breakpoint
->length
, breakpoint
->set
);
2822 breakpoint
= breakpoint
->next
;
2827 static int handle_bp_command_set(struct command_context
*cmd_ctx
,
2828 uint32_t addr
, uint32_t length
, int hw
)
2830 struct target
*target
= get_current_target(cmd_ctx
);
2831 int retval
= breakpoint_add(target
, addr
, length
, hw
);
2832 if (ERROR_OK
== retval
)
2833 command_print(cmd_ctx
, "breakpoint set at 0x%8.8" PRIx32
"", addr
);
2835 LOG_ERROR("Failure setting breakpoint");
2839 COMMAND_HANDLER(handle_bp_command
)
2842 return handle_bp_command_list(CMD_CTX
);
2844 if (CMD_ARGC
< 2 || CMD_ARGC
> 3)
2846 command_print(CMD_CTX
, "usage: bp <address> <length> ['hw']");
2847 return ERROR_COMMAND_SYNTAX_ERROR
;
2851 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], addr
);
2853 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], length
);
2858 if (strcmp(CMD_ARGV
[2], "hw") == 0)
2861 return ERROR_COMMAND_SYNTAX_ERROR
;
2864 return handle_bp_command_set(CMD_CTX
, addr
, length
, hw
);
2867 COMMAND_HANDLER(handle_rbp_command
)
2870 return ERROR_COMMAND_SYNTAX_ERROR
;
2873 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], addr
);
2875 struct target
*target
= get_current_target(CMD_CTX
);
2876 breakpoint_remove(target
, addr
);
2881 COMMAND_HANDLER(handle_wp_command
)
2883 struct target
*target
= get_current_target(CMD_CTX
);
2887 struct watchpoint
*watchpoint
= target
->watchpoints
;
2891 command_print(CMD_CTX
, "address: 0x%8.8" PRIx32
2892 ", len: 0x%8.8" PRIx32
2893 ", r/w/a: %i, value: 0x%8.8" PRIx32
2894 ", mask: 0x%8.8" PRIx32
,
2895 watchpoint
->address
,
2897 (int)watchpoint
->rw
,
2900 watchpoint
= watchpoint
->next
;
2905 enum watchpoint_rw type
= WPT_ACCESS
;
2907 uint32_t length
= 0;
2908 uint32_t data_value
= 0x0;
2909 uint32_t data_mask
= 0xffffffff;
2914 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[4], data_mask
);
2917 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[3], data_value
);
2920 switch (CMD_ARGV
[2][0])
2932 LOG_ERROR("invalid watchpoint mode ('%c')", CMD_ARGV
[2][0]);
2933 return ERROR_COMMAND_SYNTAX_ERROR
;
2937 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], length
);
2938 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], addr
);
2942 command_print(CMD_CTX
, "usage: wp [address length "
2943 "[(r|w|a) [value [mask]]]]");
2944 return ERROR_COMMAND_SYNTAX_ERROR
;
2947 int retval
= watchpoint_add(target
, addr
, length
, type
,
2948 data_value
, data_mask
);
2949 if (ERROR_OK
!= retval
)
2950 LOG_ERROR("Failure setting watchpoints");
2955 COMMAND_HANDLER(handle_rwp_command
)
2958 return ERROR_COMMAND_SYNTAX_ERROR
;
2961 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], addr
);
2963 struct target
*target
= get_current_target(CMD_CTX
);
2964 watchpoint_remove(target
, addr
);
2971 * Translate a virtual address to a physical address.
2973 * The low-level target implementation must have logged a detailed error
2974 * which is forwarded to telnet/GDB session.
2976 COMMAND_HANDLER(handle_virt2phys_command
)
2979 return ERROR_COMMAND_SYNTAX_ERROR
;
2982 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], va
);
2985 struct target
*target
= get_current_target(CMD_CTX
);
2986 int retval
= target
->type
->virt2phys(target
, va
, &pa
);
2987 if (retval
== ERROR_OK
)
2988 command_print(CMD_CTX
, "Physical address 0x%08" PRIx32
"", pa
);
2993 static void writeData(FILE *f
, const void *data
, size_t len
)
2995 size_t written
= fwrite(data
, 1, len
, f
);
2997 LOG_ERROR("failed to write %zu bytes: %s", len
, strerror(errno
));
3000 static void writeLong(FILE *f
, int l
)
3003 for (i
= 0; i
< 4; i
++)
3005 char c
= (l
>> (i
*8))&0xff;
3006 writeData(f
, &c
, 1);
3011 static void writeString(FILE *f
, char *s
)
3013 writeData(f
, s
, strlen(s
));
3016 /* Dump a gmon.out histogram file. */
3017 static void writeGmon(uint32_t *samples
, uint32_t sampleNum
, const char *filename
)
3020 FILE *f
= fopen(filename
, "w");
3023 writeString(f
, "gmon");
3024 writeLong(f
, 0x00000001); /* Version */
3025 writeLong(f
, 0); /* padding */
3026 writeLong(f
, 0); /* padding */
3027 writeLong(f
, 0); /* padding */
3029 uint8_t zero
= 0; /* GMON_TAG_TIME_HIST */
3030 writeData(f
, &zero
, 1);
3032 /* figure out bucket size */
3033 uint32_t min
= samples
[0];
3034 uint32_t max
= samples
[0];
3035 for (i
= 0; i
< sampleNum
; i
++)
3037 if (min
> samples
[i
])
3041 if (max
< samples
[i
])
3047 int addressSpace
= (max
-min
+ 1);
3049 static const uint32_t maxBuckets
= 256 * 1024; /* maximum buckets. */
3050 uint32_t length
= addressSpace
;
3051 if (length
> maxBuckets
)
3053 length
= maxBuckets
;
3055 int *buckets
= malloc(sizeof(int)*length
);
3056 if (buckets
== NULL
)
3061 memset(buckets
, 0, sizeof(int)*length
);
3062 for (i
= 0; i
< sampleNum
;i
++)
3064 uint32_t address
= samples
[i
];
3065 long long a
= address
-min
;
3066 long long b
= length
-1;
3067 long long c
= addressSpace
-1;
3068 int index_t
= (a
*b
)/c
; /* danger!!!! int32 overflows */
3072 /* append binary memory gmon.out &profile_hist_hdr ((char*)&profile_hist_hdr + sizeof(struct gmon_hist_hdr)) */
3073 writeLong(f
, min
); /* low_pc */
3074 writeLong(f
, max
); /* high_pc */
3075 writeLong(f
, length
); /* # of samples */
3076 writeLong(f
, 64000000); /* 64MHz */
3077 writeString(f
, "seconds");
3078 for (i
= 0; i
< (15-strlen("seconds")); i
++)
3079 writeData(f
, &zero
, 1);
3080 writeString(f
, "s");
3082 /*append binary memory gmon.out profile_hist_data (profile_hist_data + profile_hist_hdr.hist_size) */
3084 char *data
= malloc(2*length
);
3087 for (i
= 0; i
< length
;i
++)
3096 data
[i
*2 + 1]=(val
>> 8)&0xff;
3099 writeData(f
, data
, length
* 2);
3109 /* profiling samples the CPU PC as quickly as OpenOCD is able,
3110 * which will be used as a random sampling of PC */
3111 COMMAND_HANDLER(handle_profile_command
)
3113 struct target
*target
= get_current_target(CMD_CTX
);
3114 struct timeval timeout
, now
;
3116 gettimeofday(&timeout
, NULL
);
3119 return ERROR_COMMAND_SYNTAX_ERROR
;
3122 COMMAND_PARSE_NUMBER(uint
, CMD_ARGV
[0], offset
);
3124 timeval_add_time(&timeout
, offset
, 0);
3127 * @todo: Some cores let us sample the PC without the
3128 * annoying halt/resume step; for example, ARMv7 PCSR.
3129 * Provide a way to use that more efficient mechanism.
3132 command_print(CMD_CTX
, "Starting profiling. Halting and resuming the target as often as we can...");
3134 static const int maxSample
= 10000;
3135 uint32_t *samples
= malloc(sizeof(uint32_t)*maxSample
);
3136 if (samples
== NULL
)
3140 /* hopefully it is safe to cache! We want to stop/restart as quickly as possible. */
3141 struct reg
*reg
= register_get_by_name(target
->reg_cache
, "pc", 1);
3146 target_poll(target
);
3147 if (target
->state
== TARGET_HALTED
)
3149 uint32_t t
=*((uint32_t *)reg
->value
);
3150 samples
[numSamples
++]=t
;
3151 retval
= target_resume(target
, 1, 0, 0, 0); /* current pc, addr = 0, do not handle breakpoints, not debugging */
3152 target_poll(target
);
3153 alive_sleep(10); /* sleep 10ms, i.e. <100 samples/second. */
3154 } else if (target
->state
== TARGET_RUNNING
)
3156 /* We want to quickly sample the PC. */
3157 if ((retval
= target_halt(target
)) != ERROR_OK
)
3164 command_print(CMD_CTX
, "Target not halted or running");
3168 if (retval
!= ERROR_OK
)
3173 gettimeofday(&now
, NULL
);
3174 if ((numSamples
>= maxSample
) || ((now
.tv_sec
>= timeout
.tv_sec
) && (now
.tv_usec
>= timeout
.tv_usec
)))
3176 command_print(CMD_CTX
, "Profiling completed. %d samples.", numSamples
);
3177 if ((retval
= target_poll(target
)) != ERROR_OK
)
3182 if (target
->state
== TARGET_HALTED
)
3184 target_resume(target
, 1, 0, 0, 0); /* current pc, addr = 0, do not handle breakpoints, not debugging */
3186 if ((retval
= target_poll(target
)) != ERROR_OK
)
3191 writeGmon(samples
, numSamples
, CMD_ARGV
[1]);
3192 command_print(CMD_CTX
, "Wrote %s", CMD_ARGV
[1]);
3201 static int new_int_array_element(Jim_Interp
* interp
, const char *varname
, int idx
, uint32_t val
)
3204 Jim_Obj
*nameObjPtr
, *valObjPtr
;
3207 namebuf
= alloc_printf("%s(%d)", varname
, idx
);
3211 nameObjPtr
= Jim_NewStringObj(interp
, namebuf
, -1);
3212 valObjPtr
= Jim_NewIntObj(interp
, val
);
3213 if (!nameObjPtr
|| !valObjPtr
)
3219 Jim_IncrRefCount(nameObjPtr
);
3220 Jim_IncrRefCount(valObjPtr
);
3221 result
= Jim_SetVariable(interp
, nameObjPtr
, valObjPtr
);
3222 Jim_DecrRefCount(interp
, nameObjPtr
);
3223 Jim_DecrRefCount(interp
, valObjPtr
);
3225 /* printf("%s(%d) <= 0%08x\n", varname, idx, val); */
3229 static int jim_mem2array(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
3231 struct command_context
*context
;
3232 struct target
*target
;
3234 context
= current_command_context(interp
);
3235 assert (context
!= NULL
);
3237 target
= get_current_target(context
);
3240 LOG_ERROR("mem2array: no current target");
3244 return target_mem2array(interp
, target
, argc
-1, argv
+ 1);
3247 static int target_mem2array(Jim_Interp
*interp
, struct target
*target
, int argc
, Jim_Obj
*const *argv
)
3255 const char *varname
;
3259 /* argv[1] = name of array to receive the data
3260 * argv[2] = desired width
3261 * argv[3] = memory address
3262 * argv[4] = count of times to read
3265 Jim_WrongNumArgs(interp
, 1, argv
, "varname width addr nelems");
3268 varname
= Jim_GetString(argv
[0], &len
);
3269 /* given "foo" get space for worse case "foo(%d)" .. add 20 */
3271 e
= Jim_GetLong(interp
, argv
[1], &l
);
3277 e
= Jim_GetLong(interp
, argv
[2], &l
);
3282 e
= Jim_GetLong(interp
, argv
[3], &l
);
3298 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3299 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "Invalid width param, must be 8/16/32", NULL
);
3303 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3304 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "mem2array: zero width read?", NULL
);
3307 if ((addr
+ (len
* width
)) < addr
) {
3308 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3309 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "mem2array: addr + len - wraps to zero?", NULL
);
3312 /* absurd transfer size? */
3314 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3315 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "mem2array: absurd > 64K item request", NULL
);
3320 ((width
== 2) && ((addr
& 1) == 0)) ||
3321 ((width
== 4) && ((addr
& 3) == 0))) {
3325 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3326 sprintf(buf
, "mem2array address: 0x%08" PRIx32
" is not aligned for %" PRId32
" byte reads",
3329 Jim_AppendStrings(interp
, Jim_GetResult(interp
), buf
, NULL
);
3338 size_t buffersize
= 4096;
3339 uint8_t *buffer
= malloc(buffersize
);
3346 /* Slurp... in buffer size chunks */
3348 count
= len
; /* in objects.. */
3349 if (count
> (buffersize
/width
)) {
3350 count
= (buffersize
/width
);
3353 retval
= target_read_memory(target
, addr
, width
, count
, buffer
);
3354 if (retval
!= ERROR_OK
) {
3356 LOG_ERROR("mem2array: Read @ 0x%08x, w=%d, cnt=%d, failed",
3360 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3361 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "mem2array: cannot read memory", NULL
);
3365 v
= 0; /* shut up gcc */
3366 for (i
= 0 ;i
< count
;i
++, n
++) {
3369 v
= target_buffer_get_u32(target
, &buffer
[i
*width
]);
3372 v
= target_buffer_get_u16(target
, &buffer
[i
*width
]);
3375 v
= buffer
[i
] & 0x0ff;
3378 new_int_array_element(interp
, varname
, n
, v
);
3386 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3391 static int get_int_array_element(Jim_Interp
* interp
, const char *varname
, int idx
, uint32_t *val
)
3394 Jim_Obj
*nameObjPtr
, *valObjPtr
;
3398 namebuf
= alloc_printf("%s(%d)", varname
, idx
);
3402 nameObjPtr
= Jim_NewStringObj(interp
, namebuf
, -1);
3409 Jim_IncrRefCount(nameObjPtr
);
3410 valObjPtr
= Jim_GetVariable(interp
, nameObjPtr
, JIM_ERRMSG
);
3411 Jim_DecrRefCount(interp
, nameObjPtr
);
3413 if (valObjPtr
== NULL
)
3416 result
= Jim_GetLong(interp
, valObjPtr
, &l
);
3417 /* printf("%s(%d) => 0%08x\n", varname, idx, val); */
3422 static int jim_array2mem(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
3424 struct command_context
*context
;
3425 struct target
*target
;
3427 context
= current_command_context(interp
);
3428 assert (context
!= NULL
);
3430 target
= get_current_target(context
);
3431 if (target
== NULL
) {
3432 LOG_ERROR("array2mem: no current target");
3436 return target_array2mem(interp
,target
, argc
-1, argv
+ 1);
3439 static int target_array2mem(Jim_Interp
*interp
, struct target
*target
,
3440 int argc
, Jim_Obj
*const *argv
)
3448 const char *varname
;
3452 /* argv[1] = name of array to get the data
3453 * argv[2] = desired width
3454 * argv[3] = memory address
3455 * argv[4] = count to write
3458 Jim_WrongNumArgs(interp
, 0, argv
, "varname width addr nelems");
3461 varname
= Jim_GetString(argv
[0], &len
);
3462 /* given "foo" get space for worse case "foo(%d)" .. add 20 */
3464 e
= Jim_GetLong(interp
, argv
[1], &l
);
3470 e
= Jim_GetLong(interp
, argv
[2], &l
);
3475 e
= Jim_GetLong(interp
, argv
[3], &l
);
3491 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3492 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "Invalid width param, must be 8/16/32", NULL
);
3496 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3497 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "array2mem: zero width read?", NULL
);
3500 if ((addr
+ (len
* width
)) < addr
) {
3501 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3502 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "array2mem: addr + len - wraps to zero?", NULL
);
3505 /* absurd transfer size? */
3507 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3508 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "array2mem: absurd > 64K item request", NULL
);
3513 ((width
== 2) && ((addr
& 1) == 0)) ||
3514 ((width
== 4) && ((addr
& 3) == 0))) {
3518 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3519 sprintf(buf
, "array2mem address: 0x%08x is not aligned for %d byte reads",
3522 Jim_AppendStrings(interp
, Jim_GetResult(interp
), buf
, NULL
);
3533 size_t buffersize
= 4096;
3534 uint8_t *buffer
= malloc(buffersize
);
3539 /* Slurp... in buffer size chunks */
3541 count
= len
; /* in objects.. */
3542 if (count
> (buffersize
/width
)) {
3543 count
= (buffersize
/width
);
3546 v
= 0; /* shut up gcc */
3547 for (i
= 0 ;i
< count
;i
++, n
++) {
3548 get_int_array_element(interp
, varname
, n
, &v
);
3551 target_buffer_set_u32(target
, &buffer
[i
*width
], v
);
3554 target_buffer_set_u16(target
, &buffer
[i
*width
], v
);
3557 buffer
[i
] = v
& 0x0ff;
3563 retval
= target_write_memory(target
, addr
, width
, count
, buffer
);
3564 if (retval
!= ERROR_OK
) {
3566 LOG_ERROR("array2mem: Write @ 0x%08x, w=%d, cnt=%d, failed",
3570 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3571 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "array2mem: cannot read memory", NULL
);
3579 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3584 /* FIX? should we propagate errors here rather than printing them
3587 void target_handle_event(struct target
*target
, enum target_event e
)
3589 struct target_event_action
*teap
;
3591 for (teap
= target
->event_action
; teap
!= NULL
; teap
= teap
->next
) {
3592 if (teap
->event
== e
) {
3593 LOG_DEBUG("target: (%d) %s (%s) event: %d (%s) action: %s",
3594 target
->target_number
,
3595 target_name(target
),
3596 target_type_name(target
),
3598 Jim_Nvp_value2name_simple(nvp_target_event
, e
)->name
,
3599 Jim_GetString(teap
->body
, NULL
));
3600 if (Jim_EvalObj(teap
->interp
, teap
->body
) != JIM_OK
)
3602 Jim_PrintErrorMessage(teap
->interp
);
3609 * Returns true only if the target has a handler for the specified event.
3611 bool target_has_event_action(struct target
*target
, enum target_event event
)
3613 struct target_event_action
*teap
;
3615 for (teap
= target
->event_action
; teap
!= NULL
; teap
= teap
->next
) {
3616 if (teap
->event
== event
)
3622 enum target_cfg_param
{
3625 TCFG_WORK_AREA_VIRT
,
3626 TCFG_WORK_AREA_PHYS
,
3627 TCFG_WORK_AREA_SIZE
,
3628 TCFG_WORK_AREA_BACKUP
,
3631 TCFG_CHAIN_POSITION
,
3634 static Jim_Nvp nvp_config_opts
[] = {
3635 { .name
= "-type", .value
= TCFG_TYPE
},
3636 { .name
= "-event", .value
= TCFG_EVENT
},
3637 { .name
= "-work-area-virt", .value
= TCFG_WORK_AREA_VIRT
},
3638 { .name
= "-work-area-phys", .value
= TCFG_WORK_AREA_PHYS
},
3639 { .name
= "-work-area-size", .value
= TCFG_WORK_AREA_SIZE
},
3640 { .name
= "-work-area-backup", .value
= TCFG_WORK_AREA_BACKUP
},
3641 { .name
= "-endian" , .value
= TCFG_ENDIAN
},
3642 { .name
= "-variant", .value
= TCFG_VARIANT
},
3643 { .name
= "-chain-position", .value
= TCFG_CHAIN_POSITION
},
3645 { .name
= NULL
, .value
= -1 }
3648 static int target_configure(Jim_GetOptInfo
*goi
, struct target
*target
)
3656 /* parse config or cget options ... */
3657 while (goi
->argc
> 0) {
3658 Jim_SetEmptyResult(goi
->interp
);
3659 /* Jim_GetOpt_Debug(goi); */
3661 if (target
->type
->target_jim_configure
) {
3662 /* target defines a configure function */
3663 /* target gets first dibs on parameters */
3664 e
= (*(target
->type
->target_jim_configure
))(target
, goi
);
3673 /* otherwise we 'continue' below */
3675 e
= Jim_GetOpt_Nvp(goi
, nvp_config_opts
, &n
);
3677 Jim_GetOpt_NvpUnknown(goi
, nvp_config_opts
, 0);
3683 if (goi
->isconfigure
) {
3684 Jim_SetResult_sprintf(goi
->interp
,
3685 "not settable: %s", n
->name
);
3689 if (goi
->argc
!= 0) {
3690 Jim_WrongNumArgs(goi
->interp
,
3691 goi
->argc
, goi
->argv
,
3696 Jim_SetResultString(goi
->interp
,
3697 target_type_name(target
), -1);
3701 if (goi
->argc
== 0) {
3702 Jim_WrongNumArgs(goi
->interp
, goi
->argc
, goi
->argv
, "-event ?event-name? ...");
3706 e
= Jim_GetOpt_Nvp(goi
, nvp_target_event
, &n
);
3708 Jim_GetOpt_NvpUnknown(goi
, nvp_target_event
, 1);
3712 if (goi
->isconfigure
) {
3713 if (goi
->argc
!= 1) {
3714 Jim_WrongNumArgs(goi
->interp
, goi
->argc
, goi
->argv
, "-event ?event-name? ?EVENT-BODY?");
3718 if (goi
->argc
!= 0) {
3719 Jim_WrongNumArgs(goi
->interp
, goi
->argc
, goi
->argv
, "-event ?event-name?");
3725 struct target_event_action
*teap
;
3727 teap
= target
->event_action
;
3728 /* replace existing? */
3730 if (teap
->event
== (enum target_event
)n
->value
) {
3736 if (goi
->isconfigure
) {
3737 bool replace
= true;
3740 teap
= calloc(1, sizeof(*teap
));
3743 teap
->event
= n
->value
;
3744 teap
->interp
= goi
->interp
;
3745 Jim_GetOpt_Obj(goi
, &o
);
3747 Jim_DecrRefCount(teap
->interp
, teap
->body
);
3749 teap
->body
= Jim_DuplicateObj(goi
->interp
, o
);
3752 * Tcl/TK - "tk events" have a nice feature.
3753 * See the "BIND" command.
3754 * We should support that here.
3755 * You can specify %X and %Y in the event code.
3756 * The idea is: %T - target name.
3757 * The idea is: %N - target number
3758 * The idea is: %E - event name.
3760 Jim_IncrRefCount(teap
->body
);
3764 /* add to head of event list */
3765 teap
->next
= target
->event_action
;
3766 target
->event_action
= teap
;
3768 Jim_SetEmptyResult(goi
->interp
);
3772 Jim_SetEmptyResult(goi
->interp
);
3774 Jim_SetResult(goi
->interp
, Jim_DuplicateObj(goi
->interp
, teap
->body
));
3781 case TCFG_WORK_AREA_VIRT
:
3782 if (goi
->isconfigure
) {
3783 target_free_all_working_areas(target
);
3784 e
= Jim_GetOpt_Wide(goi
, &w
);
3788 target
->working_area_virt
= w
;
3789 target
->working_area_virt_spec
= true;
3791 if (goi
->argc
!= 0) {
3795 Jim_SetResult(goi
->interp
, Jim_NewIntObj(goi
->interp
, target
->working_area_virt
));
3799 case TCFG_WORK_AREA_PHYS
:
3800 if (goi
->isconfigure
) {
3801 target_free_all_working_areas(target
);
3802 e
= Jim_GetOpt_Wide(goi
, &w
);
3806 target
->working_area_phys
= w
;
3807 target
->working_area_phys_spec
= true;
3809 if (goi
->argc
!= 0) {
3813 Jim_SetResult(goi
->interp
, Jim_NewIntObj(goi
->interp
, target
->working_area_phys
));
3817 case TCFG_WORK_AREA_SIZE
:
3818 if (goi
->isconfigure
) {
3819 target_free_all_working_areas(target
);
3820 e
= Jim_GetOpt_Wide(goi
, &w
);
3824 target
->working_area_size
= w
;
3826 if (goi
->argc
!= 0) {
3830 Jim_SetResult(goi
->interp
, Jim_NewIntObj(goi
->interp
, target
->working_area_size
));
3834 case TCFG_WORK_AREA_BACKUP
:
3835 if (goi
->isconfigure
) {
3836 target_free_all_working_areas(target
);
3837 e
= Jim_GetOpt_Wide(goi
, &w
);
3841 /* make this exactly 1 or 0 */
3842 target
->backup_working_area
= (!!w
);
3844 if (goi
->argc
!= 0) {
3848 Jim_SetResult(goi
->interp
, Jim_NewIntObj(goi
->interp
, target
->backup_working_area
));
3849 /* loop for more e*/
3853 if (goi
->isconfigure
) {
3854 e
= Jim_GetOpt_Nvp(goi
, nvp_target_endian
, &n
);
3856 Jim_GetOpt_NvpUnknown(goi
, nvp_target_endian
, 1);
3859 target
->endianness
= n
->value
;
3861 if (goi
->argc
!= 0) {
3865 n
= Jim_Nvp_value2name_simple(nvp_target_endian
, target
->endianness
);
3866 if (n
->name
== NULL
) {
3867 target
->endianness
= TARGET_LITTLE_ENDIAN
;
3868 n
= Jim_Nvp_value2name_simple(nvp_target_endian
, target
->endianness
);
3870 Jim_SetResultString(goi
->interp
, n
->name
, -1);
3875 if (goi
->isconfigure
) {
3876 if (goi
->argc
< 1) {
3877 Jim_SetResult_sprintf(goi
->interp
,
3882 if (target
->variant
) {
3883 free((void *)(target
->variant
));
3885 e
= Jim_GetOpt_String(goi
, &cp
, NULL
);
3886 target
->variant
= strdup(cp
);
3888 if (goi
->argc
!= 0) {
3892 Jim_SetResultString(goi
->interp
, target
->variant
,-1);
3895 case TCFG_CHAIN_POSITION
:
3896 if (goi
->isconfigure
) {
3898 struct jtag_tap
*tap
;
3899 target_free_all_working_areas(target
);
3900 e
= Jim_GetOpt_Obj(goi
, &o_t
);
3904 tap
= jtag_tap_by_jim_obj(goi
->interp
, o_t
);
3908 /* make this exactly 1 or 0 */
3911 if (goi
->argc
!= 0) {
3915 Jim_SetResultString(goi
->interp
, target
->tap
->dotted_name
, -1);
3916 /* loop for more e*/
3919 } /* while (goi->argc) */
3922 /* done - we return */
3927 jim_target_configure(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
3931 Jim_GetOpt_Setup(&goi
, interp
, argc
- 1, argv
+ 1);
3932 goi
.isconfigure
= !strcmp(Jim_GetString(argv
[0], NULL
), "configure");
3933 int need_args
= 1 + goi
.isconfigure
;
3934 if (goi
.argc
< need_args
)
3936 Jim_WrongNumArgs(goi
.interp
, goi
.argc
, goi
.argv
,
3938 ? "missing: -option VALUE ..."
3939 : "missing: -option ...");
3942 struct target
*target
= Jim_CmdPrivData(goi
.interp
);
3943 return target_configure(&goi
, target
);
3946 static int jim_target_mw(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
3948 const char *cmd_name
= Jim_GetString(argv
[0], NULL
);
3951 Jim_GetOpt_Setup(&goi
, interp
, argc
- 1, argv
+ 1);
3953 /* danger! goi.argc will be modified below! */
3956 if (argc
!= 2 && argc
!= 3)
3958 Jim_SetResult_sprintf(goi
.interp
,
3959 "usage: %s <address> <data> [<count>]", cmd_name
);
3965 int e
= Jim_GetOpt_Wide(&goi
, &a
);
3970 e
= Jim_GetOpt_Wide(&goi
, &b
);
3977 e
= Jim_GetOpt_Wide(&goi
, &c
);
3982 struct target
*target
= Jim_CmdPrivData(goi
.interp
);
3984 if (strcasecmp(cmd_name
, "mww") == 0) {
3987 else if (strcasecmp(cmd_name
, "mwh") == 0) {
3990 else if (strcasecmp(cmd_name
, "mwb") == 0) {
3993 LOG_ERROR("command '%s' unknown: ", cmd_name
);
3997 return (target_fill_mem(target
, a
, target_write_memory_fast
, data_size
, b
, c
) == ERROR_OK
) ? JIM_OK
: JIM_ERR
;
4000 static int jim_target_md(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4002 const char *cmd_name
= Jim_GetString(argv
[0], NULL
);
4005 Jim_GetOpt_Setup(&goi
, interp
, argc
- 1, argv
+ 1);
4007 /* danger! goi.argc will be modified below! */
4010 if ((argc
!= 1) && (argc
!= 2))
4012 Jim_SetResult_sprintf(goi
.interp
,
4013 "usage: %s <address> [<count>]", cmd_name
);
4018 int e
= Jim_GetOpt_Wide(&goi
, &a
);
4024 e
= Jim_GetOpt_Wide(&goi
, &c
);
4031 jim_wide b
= 1; /* shut up gcc */
4032 if (strcasecmp(cmd_name
, "mdw") == 0)
4034 else if (strcasecmp(cmd_name
, "mdh") == 0)
4036 else if (strcasecmp(cmd_name
, "mdb") == 0)
4039 LOG_ERROR("command '%s' unknown: ", cmd_name
);
4043 /* convert count to "bytes" */
4046 struct target
*target
= Jim_CmdPrivData(goi
.interp
);
4047 uint8_t target_buf
[32];
4054 e
= target_read_memory(target
, a
, b
, y
/ b
, target_buf
);
4055 if (e
!= ERROR_OK
) {
4056 Jim_SetResult_sprintf(interp
, "error reading target @ 0x%08lx", (int)(a
));
4060 Jim_fprintf(interp
, interp
->cookie_stdout
, "0x%08x ", (int)(a
));
4063 for (x
= 0; x
< 16 && x
< y
; x
+= 4)
4065 z
= target_buffer_get_u32(target
, &(target_buf
[ x
]));
4066 Jim_fprintf(interp
, interp
->cookie_stdout
, "%08x ", (int)(z
));
4068 for (; (x
< 16) ; x
+= 4) {
4069 Jim_fprintf(interp
, interp
->cookie_stdout
, " ");
4073 for (x
= 0; x
< 16 && x
< y
; x
+= 2)
4075 z
= target_buffer_get_u16(target
, &(target_buf
[ x
]));
4076 Jim_fprintf(interp
, interp
->cookie_stdout
, "%04x ", (int)(z
));
4078 for (; (x
< 16) ; x
+= 2) {
4079 Jim_fprintf(interp
, interp
->cookie_stdout
, " ");
4084 for (x
= 0 ; (x
< 16) && (x
< y
) ; x
+= 1) {
4085 z
= target_buffer_get_u8(target
, &(target_buf
[ x
]));
4086 Jim_fprintf(interp
, interp
->cookie_stdout
, "%02x ", (int)(z
));
4088 for (; (x
< 16) ; x
+= 1) {
4089 Jim_fprintf(interp
, interp
->cookie_stdout
, " ");
4093 /* ascii-ify the bytes */
4094 for (x
= 0 ; x
< y
; x
++) {
4095 if ((target_buf
[x
] >= 0x20) &&
4096 (target_buf
[x
] <= 0x7e)) {
4100 target_buf
[x
] = '.';
4105 target_buf
[x
] = ' ';
4110 /* print - with a newline */
4111 Jim_fprintf(interp
, interp
->cookie_stdout
, "%s\n", target_buf
);
4119 static int jim_target_mem2array(Jim_Interp
*interp
,
4120 int argc
, Jim_Obj
*const *argv
)
4122 struct target
*target
= Jim_CmdPrivData(interp
);
4123 return target_mem2array(interp
, target
, argc
- 1, argv
+ 1);
4126 static int jim_target_array2mem(Jim_Interp
*interp
,
4127 int argc
, Jim_Obj
*const *argv
)
4129 struct target
*target
= Jim_CmdPrivData(interp
);
4130 return target_array2mem(interp
, target
, argc
- 1, argv
+ 1);
4133 static int jim_target_tap_disabled(Jim_Interp
*interp
)
4135 Jim_SetResult_sprintf(interp
, "[TAP is disabled]");
4139 static int jim_target_examine(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4143 Jim_WrongNumArgs(interp
, 1, argv
, "[no parameters]");
4146 struct target
*target
= Jim_CmdPrivData(interp
);
4147 if (!target
->tap
->enabled
)
4148 return jim_target_tap_disabled(interp
);
4150 int e
= target
->type
->examine(target
);
4153 Jim_SetResult_sprintf(interp
, "examine-fails: %d", e
);
4159 static int jim_target_halt_gdb(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4163 Jim_WrongNumArgs(interp
, 1, argv
, "[no parameters]");
4166 struct target
*target
= Jim_CmdPrivData(interp
);
4168 if (target_call_event_callbacks(target
, TARGET_EVENT_GDB_HALT
) != ERROR_OK
)
4174 static int jim_target_poll(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4178 Jim_WrongNumArgs(interp
, 1, argv
, "[no parameters]");
4181 struct target
*target
= Jim_CmdPrivData(interp
);
4182 if (!target
->tap
->enabled
)
4183 return jim_target_tap_disabled(interp
);
4186 if (!(target_was_examined(target
))) {
4187 e
= ERROR_TARGET_NOT_EXAMINED
;
4189 e
= target
->type
->poll(target
);
4193 Jim_SetResult_sprintf(interp
, "poll-fails: %d", e
);
4199 static int jim_target_reset(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4202 Jim_GetOpt_Setup(&goi
, interp
, argc
- 1, argv
+ 1);
4206 Jim_WrongNumArgs(interp
, 0, argv
,
4207 "([tT]|[fF]|assert|deassert) BOOL");
4212 int e
= Jim_GetOpt_Nvp(&goi
, nvp_assert
, &n
);
4215 Jim_GetOpt_NvpUnknown(&goi
, nvp_assert
, 1);
4218 /* the halt or not param */
4220 e
= Jim_GetOpt_Wide(&goi
, &a
);
4224 struct target
*target
= Jim_CmdPrivData(goi
.interp
);
4225 if (!target
->tap
->enabled
)
4226 return jim_target_tap_disabled(interp
);
4227 if (!(target_was_examined(target
)))
4229 LOG_ERROR("Target not examined yet");
4230 return ERROR_TARGET_NOT_EXAMINED
;
4232 if (!target
->type
->assert_reset
|| !target
->type
->deassert_reset
)
4234 Jim_SetResult_sprintf(interp
,
4235 "No target-specific reset for %s",
4236 target_name(target
));
4239 /* determine if we should halt or not. */
4240 target
->reset_halt
= !!a
;
4241 /* When this happens - all workareas are invalid. */
4242 target_free_all_working_areas_restore(target
, 0);
4245 if (n
->value
== NVP_ASSERT
) {
4246 e
= target
->type
->assert_reset(target
);
4248 e
= target
->type
->deassert_reset(target
);
4250 return (e
== ERROR_OK
) ? JIM_OK
: JIM_ERR
;
4253 static int jim_target_halt(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4256 Jim_WrongNumArgs(interp
, 1, argv
, "[no parameters]");
4259 struct target
*target
= Jim_CmdPrivData(interp
);
4260 if (!target
->tap
->enabled
)
4261 return jim_target_tap_disabled(interp
);
4262 int e
= target
->type
->halt(target
);
4263 return (e
== ERROR_OK
) ? JIM_OK
: JIM_ERR
;
4266 static int jim_target_wait_state(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4269 Jim_GetOpt_Setup(&goi
, interp
, argc
- 1, argv
+ 1);
4271 /* params: <name> statename timeoutmsecs */
4274 const char *cmd_name
= Jim_GetString(argv
[0], NULL
);
4275 Jim_SetResult_sprintf(goi
.interp
,
4276 "%s <state_name> <timeout_in_msec>", cmd_name
);
4281 int e
= Jim_GetOpt_Nvp(&goi
, nvp_target_state
, &n
);
4283 Jim_GetOpt_NvpUnknown(&goi
, nvp_target_state
,1);
4287 e
= Jim_GetOpt_Wide(&goi
, &a
);
4291 struct target
*target
= Jim_CmdPrivData(interp
);
4292 if (!target
->tap
->enabled
)
4293 return jim_target_tap_disabled(interp
);
4295 e
= target_wait_state(target
, n
->value
, a
);
4298 Jim_SetResult_sprintf(goi
.interp
,
4299 "target: %s wait %s fails (%d) %s",
4300 target_name(target
), n
->name
,
4301 e
, target_strerror_safe(e
));
4306 /* List for human, Events defined for this target.
4307 * scripts/programs should use 'name cget -event NAME'
4309 static int jim_target_event_list(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4311 struct command_context
*cmd_ctx
= current_command_context(interp
);
4312 assert (cmd_ctx
!= NULL
);
4314 struct target
*target
= Jim_CmdPrivData(interp
);
4315 struct target_event_action
*teap
= target
->event_action
;
4316 command_print(cmd_ctx
, "Event actions for target (%d) %s\n",
4317 target
->target_number
,
4318 target_name(target
));
4319 command_print(cmd_ctx
, "%-25s | Body", "Event");
4320 command_print(cmd_ctx
, "------------------------- | "
4321 "----------------------------------------");
4324 Jim_Nvp
*opt
= Jim_Nvp_value2name_simple(nvp_target_event
, teap
->event
);
4325 command_print(cmd_ctx
, "%-25s | %s",
4326 opt
->name
, Jim_GetString(teap
->body
, NULL
));
4329 command_print(cmd_ctx
, "***END***");
4332 static int jim_target_current_state(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4336 Jim_WrongNumArgs(interp
, 1, argv
, "[no parameters]");
4339 struct target
*target
= Jim_CmdPrivData(interp
);
4340 Jim_SetResultString(interp
, target_state_name(target
), -1);
4343 static int jim_target_invoke_event(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4346 Jim_GetOpt_Setup(&goi
, interp
, argc
- 1, argv
+ 1);
4349 const char *cmd_name
= Jim_GetString(argv
[0], NULL
);
4350 Jim_SetResult_sprintf(goi
.interp
, "%s <eventname>", cmd_name
);
4354 int e
= Jim_GetOpt_Nvp(&goi
, nvp_target_event
, &n
);
4357 Jim_GetOpt_NvpUnknown(&goi
, nvp_target_event
, 1);
4360 struct target
*target
= Jim_CmdPrivData(interp
);
4361 target_handle_event(target
, n
->value
);
4365 static const struct command_registration target_instance_command_handlers
[] = {
4367 .name
= "configure",
4368 .mode
= COMMAND_CONFIG
,
4369 .jim_handler
= jim_target_configure
,
4370 .help
= "configure a new target for use",
4371 .usage
= "[target_attribute ...]",
4375 .mode
= COMMAND_ANY
,
4376 .jim_handler
= jim_target_configure
,
4377 .help
= "returns the specified target attribute",
4378 .usage
= "target_attribute",
4382 .mode
= COMMAND_EXEC
,
4383 .jim_handler
= jim_target_mw
,
4384 .help
= "Write 32-bit word(s) to target memory",
4385 .usage
= "address data [count]",
4389 .mode
= COMMAND_EXEC
,
4390 .jim_handler
= jim_target_mw
,
4391 .help
= "Write 16-bit half-word(s) to target memory",
4392 .usage
= "address data [count]",
4396 .mode
= COMMAND_EXEC
,
4397 .jim_handler
= jim_target_mw
,
4398 .help
= "Write byte(s) to target memory",
4399 .usage
= "address data [count]",
4403 .mode
= COMMAND_EXEC
,
4404 .jim_handler
= jim_target_md
,
4405 .help
= "Display target memory as 32-bit words",
4406 .usage
= "address [count]",
4410 .mode
= COMMAND_EXEC
,
4411 .jim_handler
= jim_target_md
,
4412 .help
= "Display target memory as 16-bit half-words",
4413 .usage
= "address [count]",
4417 .mode
= COMMAND_EXEC
,
4418 .jim_handler
= jim_target_md
,
4419 .help
= "Display target memory as 8-bit bytes",
4420 .usage
= "address [count]",
4423 .name
= "array2mem",
4424 .mode
= COMMAND_EXEC
,
4425 .jim_handler
= jim_target_array2mem
,
4426 .help
= "Writes Tcl array of 8/16/32 bit numbers "
4428 .usage
= "arrayname bitwidth address count",
4431 .name
= "mem2array",
4432 .mode
= COMMAND_EXEC
,
4433 .jim_handler
= jim_target_mem2array
,
4434 .help
= "Loads Tcl array of 8/16/32 bit numbers "
4435 "from target memory",
4436 .usage
= "arrayname bitwidth address count",
4439 .name
= "eventlist",
4440 .mode
= COMMAND_EXEC
,
4441 .jim_handler
= jim_target_event_list
,
4442 .help
= "displays a table of events defined for this target",
4446 .mode
= COMMAND_EXEC
,
4447 .jim_handler
= jim_target_current_state
,
4448 .help
= "displays the current state of this target",
4451 .name
= "arp_examine",
4452 .mode
= COMMAND_EXEC
,
4453 .jim_handler
= jim_target_examine
,
4454 .help
= "used internally for reset processing",
4457 .name
= "arp_halt_gdb",
4458 .mode
= COMMAND_EXEC
,
4459 .jim_handler
= jim_target_halt_gdb
,
4460 .help
= "used internally for reset processing to halt GDB",
4464 .mode
= COMMAND_EXEC
,
4465 .jim_handler
= jim_target_poll
,
4466 .help
= "used internally for reset processing",
4469 .name
= "arp_reset",
4470 .mode
= COMMAND_EXEC
,
4471 .jim_handler
= jim_target_reset
,
4472 .help
= "used internally for reset processing",
4476 .mode
= COMMAND_EXEC
,
4477 .jim_handler
= jim_target_halt
,
4478 .help
= "used internally for reset processing",
4481 .name
= "arp_waitstate",
4482 .mode
= COMMAND_EXEC
,
4483 .jim_handler
= jim_target_wait_state
,
4484 .help
= "used internally for reset processing",
4487 .name
= "invoke-event",
4488 .mode
= COMMAND_EXEC
,
4489 .jim_handler
= jim_target_invoke_event
,
4490 .help
= "invoke handler for specified event",
4491 .usage
= "event_name",
4493 COMMAND_REGISTRATION_DONE
4496 static int target_create(Jim_GetOptInfo
*goi
)
4504 struct target
*target
;
4505 struct command_context
*cmd_ctx
;
4507 cmd_ctx
= current_command_context(goi
->interp
);
4508 assert (cmd_ctx
!= NULL
);
4510 if (goi
->argc
< 3) {
4511 Jim_WrongNumArgs(goi
->interp
, 1, goi
->argv
, "?name? ?type? ..options...");
4516 Jim_GetOpt_Obj(goi
, &new_cmd
);
4517 /* does this command exist? */
4518 cmd
= Jim_GetCommand(goi
->interp
, new_cmd
, JIM_ERRMSG
);
4520 cp
= Jim_GetString(new_cmd
, NULL
);
4521 Jim_SetResult_sprintf(goi
->interp
, "Command/target: %s Exists", cp
);
4526 e
= Jim_GetOpt_String(goi
, &cp2
, NULL
);
4528 /* now does target type exist */
4529 for (x
= 0 ; target_types
[x
] ; x
++) {
4530 if (0 == strcmp(cp
, target_types
[x
]->name
)) {
4535 if (target_types
[x
] == NULL
) {
4536 Jim_SetResult_sprintf(goi
->interp
, "Unknown target type %s, try one of ", cp
);
4537 for (x
= 0 ; target_types
[x
] ; x
++) {
4538 if (target_types
[x
+ 1]) {
4539 Jim_AppendStrings(goi
->interp
,
4540 Jim_GetResult(goi
->interp
),
4541 target_types
[x
]->name
,
4544 Jim_AppendStrings(goi
->interp
,
4545 Jim_GetResult(goi
->interp
),
4547 target_types
[x
]->name
,NULL
);
4554 target
= calloc(1,sizeof(struct target
));
4555 /* set target number */
4556 target
->target_number
= new_target_number();
4558 /* allocate memory for each unique target type */
4559 target
->type
= (struct target_type
*)calloc(1,sizeof(struct target_type
));
4561 memcpy(target
->type
, target_types
[x
], sizeof(struct target_type
));
4563 /* will be set by "-endian" */
4564 target
->endianness
= TARGET_ENDIAN_UNKNOWN
;
4566 target
->working_area
= 0x0;
4567 target
->working_area_size
= 0x0;
4568 target
->working_areas
= NULL
;
4569 target
->backup_working_area
= 0;
4571 target
->state
= TARGET_UNKNOWN
;
4572 target
->debug_reason
= DBG_REASON_UNDEFINED
;
4573 target
->reg_cache
= NULL
;
4574 target
->breakpoints
= NULL
;
4575 target
->watchpoints
= NULL
;
4576 target
->next
= NULL
;
4577 target
->arch_info
= NULL
;
4579 target
->display
= 1;
4581 target
->halt_issued
= false;
4583 /* initialize trace information */
4584 target
->trace_info
= malloc(sizeof(struct trace
));
4585 target
->trace_info
->num_trace_points
= 0;
4586 target
->trace_info
->trace_points_size
= 0;
4587 target
->trace_info
->trace_points
= NULL
;
4588 target
->trace_info
->trace_history_size
= 0;
4589 target
->trace_info
->trace_history
= NULL
;
4590 target
->trace_info
->trace_history_pos
= 0;
4591 target
->trace_info
->trace_history_overflowed
= 0;
4593 target
->dbgmsg
= NULL
;
4594 target
->dbg_msg_enabled
= 0;
4596 target
->endianness
= TARGET_ENDIAN_UNKNOWN
;
4598 /* Do the rest as "configure" options */
4599 goi
->isconfigure
= 1;
4600 e
= target_configure(goi
, target
);
4602 if (target
->tap
== NULL
)
4604 Jim_SetResultString(goi
->interp
, "-chain-position required when creating target", -1);
4614 if (target
->endianness
== TARGET_ENDIAN_UNKNOWN
) {
4615 /* default endian to little if not specified */
4616 target
->endianness
= TARGET_LITTLE_ENDIAN
;
4619 /* incase variant is not set */
4620 if (!target
->variant
)
4621 target
->variant
= strdup("");
4623 cp
= Jim_GetString(new_cmd
, NULL
);
4624 target
->cmd_name
= strdup(cp
);
4626 /* create the target specific commands */
4627 if (target
->type
->commands
) {
4628 e
= register_commands(cmd_ctx
, NULL
, target
->type
->commands
);
4630 LOG_ERROR("unable to register '%s' commands", cp
);
4632 if (target
->type
->target_create
) {
4633 (*(target
->type
->target_create
))(target
, goi
->interp
);
4636 /* append to end of list */
4638 struct target
**tpp
;
4639 tpp
= &(all_targets
);
4641 tpp
= &((*tpp
)->next
);
4646 /* now - create the new target name command */
4647 const const struct command_registration target_subcommands
[] = {
4649 .chain
= target_instance_command_handlers
,
4652 .chain
= target
->type
->commands
,
4654 COMMAND_REGISTRATION_DONE
4656 const const struct command_registration target_commands
[] = {
4659 .mode
= COMMAND_ANY
,
4660 .help
= "target command group",
4661 .chain
= target_subcommands
,
4663 COMMAND_REGISTRATION_DONE
4665 e
= register_commands(cmd_ctx
, NULL
, target_commands
);
4669 struct command
*c
= command_find_in_context(cmd_ctx
, cp
);
4671 command_set_handler_data(c
, target
);
4673 return (ERROR_OK
== e
) ? JIM_OK
: JIM_ERR
;
4676 static int jim_target_current(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4680 Jim_WrongNumArgs(interp
, 1, argv
, "Too many parameters");
4683 struct command_context
*cmd_ctx
= current_command_context(interp
);
4684 assert (cmd_ctx
!= NULL
);
4686 Jim_SetResultString(interp
, get_current_target(cmd_ctx
)->cmd_name
, -1);
4690 static int jim_target_types(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4694 Jim_WrongNumArgs(interp
, 1, argv
, "Too many parameters");
4697 Jim_SetResult(interp
, Jim_NewListObj(interp
, NULL
, 0));
4698 for (unsigned x
= 0; NULL
!= target_types
[x
]; x
++)
4700 Jim_ListAppendElement(interp
, Jim_GetResult(interp
),
4701 Jim_NewStringObj(interp
, target_types
[x
]->name
, -1));
4706 static int jim_target_names(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4710 Jim_WrongNumArgs(interp
, 1, argv
, "Too many parameters");
4713 Jim_SetResult(interp
, Jim_NewListObj(interp
, NULL
, 0));
4714 struct target
*target
= all_targets
;
4717 Jim_ListAppendElement(interp
, Jim_GetResult(interp
),
4718 Jim_NewStringObj(interp
, target_name(target
), -1));
4719 target
= target
->next
;
4724 static int jim_target_create(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4727 Jim_GetOpt_Setup(&goi
, interp
, argc
- 1, argv
+ 1);
4730 Jim_WrongNumArgs(goi
.interp
, goi
.argc
, goi
.argv
,
4731 "<name> <target_type> [<target_options> ...]");
4734 return target_create(&goi
);
4737 static int jim_target_number(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4740 Jim_GetOpt_Setup(&goi
, interp
, argc
- 1, argv
+ 1);
4742 /* It's OK to remove this mechanism sometime after August 2010 or so */
4743 LOG_WARNING("don't use numbers as target identifiers; use names");
4746 Jim_SetResult_sprintf(goi
.interp
, "usage: target number <number>");
4750 int e
= Jim_GetOpt_Wide(&goi
, &w
);
4754 struct target
*target
;
4755 for (target
= all_targets
; NULL
!= target
; target
= target
->next
)
4757 if (target
->target_number
!= w
)
4760 Jim_SetResultString(goi
.interp
, target_name(target
), -1);
4763 Jim_SetResult_sprintf(goi
.interp
,
4764 "Target: number %d does not exist", (int)(w
));
4768 static int jim_target_count(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4772 Jim_WrongNumArgs(interp
, 1, argv
, "<no parameters>");
4776 struct target
*target
= all_targets
;
4777 while (NULL
!= target
)
4779 target
= target
->next
;
4782 Jim_SetResult(interp
, Jim_NewIntObj(interp
, count
));
4786 static const struct command_registration target_subcommand_handlers
[] = {
4789 .mode
= COMMAND_CONFIG
,
4790 .handler
= handle_target_init_command
,
4791 .help
= "initialize targets",
4795 /* REVISIT this should be COMMAND_CONFIG ... */
4796 .mode
= COMMAND_ANY
,
4797 .jim_handler
= jim_target_create
,
4798 .usage
= "name type '-chain-position' name [options ...]",
4799 .help
= "Creates and selects a new target",
4803 .mode
= COMMAND_ANY
,
4804 .jim_handler
= jim_target_current
,
4805 .help
= "Returns the currently selected target",
4809 .mode
= COMMAND_ANY
,
4810 .jim_handler
= jim_target_types
,
4811 .help
= "Returns the available target types as "
4812 "a list of strings",
4816 .mode
= COMMAND_ANY
,
4817 .jim_handler
= jim_target_names
,
4818 .help
= "Returns the names of all targets as a list of strings",
4822 .mode
= COMMAND_ANY
,
4823 .jim_handler
= jim_target_number
,
4825 .help
= "Returns the name of the numbered target "
4830 .mode
= COMMAND_ANY
,
4831 .jim_handler
= jim_target_count
,
4832 .help
= "Returns the number of targets as an integer "
4835 COMMAND_REGISTRATION_DONE
4846 static int fastload_num
;
4847 static struct FastLoad
*fastload
;
4849 static void free_fastload(void)
4851 if (fastload
!= NULL
)
4854 for (i
= 0; i
< fastload_num
; i
++)
4856 if (fastload
[i
].data
)
4857 free(fastload
[i
].data
);
4867 COMMAND_HANDLER(handle_fast_load_image_command
)
4871 uint32_t image_size
;
4872 uint32_t min_address
= 0;
4873 uint32_t max_address
= 0xffffffff;
4878 int retval
= CALL_COMMAND_HANDLER(parse_load_image_command_CMD_ARGV
,
4879 &image
, &min_address
, &max_address
);
4880 if (ERROR_OK
!= retval
)
4883 struct duration bench
;
4884 duration_start(&bench
);
4886 if (image_open(&image
, CMD_ARGV
[0], (CMD_ARGC
>= 3) ? CMD_ARGV
[2] : NULL
) != ERROR_OK
)
4893 fastload_num
= image
.num_sections
;
4894 fastload
= (struct FastLoad
*)malloc(sizeof(struct FastLoad
)*image
.num_sections
);
4895 if (fastload
== NULL
)
4897 image_close(&image
);
4900 memset(fastload
, 0, sizeof(struct FastLoad
)*image
.num_sections
);
4901 for (i
= 0; i
< image
.num_sections
; i
++)
4903 buffer
= malloc(image
.sections
[i
].size
);
4906 command_print(CMD_CTX
, "error allocating buffer for section (%d bytes)",
4907 (int)(image
.sections
[i
].size
));
4911 if ((retval
= image_read_section(&image
, i
, 0x0, image
.sections
[i
].size
, buffer
, &buf_cnt
)) != ERROR_OK
)
4917 uint32_t offset
= 0;
4918 uint32_t length
= buf_cnt
;
4921 /* DANGER!!! beware of unsigned comparision here!!! */
4923 if ((image
.sections
[i
].base_address
+ buf_cnt
>= min_address
)&&
4924 (image
.sections
[i
].base_address
< max_address
))
4926 if (image
.sections
[i
].base_address
< min_address
)
4928 /* clip addresses below */
4929 offset
+= min_address
-image
.sections
[i
].base_address
;
4933 if (image
.sections
[i
].base_address
+ buf_cnt
> max_address
)
4935 length
-= (image
.sections
[i
].base_address
+ buf_cnt
)-max_address
;
4938 fastload
[i
].address
= image
.sections
[i
].base_address
+ offset
;
4939 fastload
[i
].data
= malloc(length
);
4940 if (fastload
[i
].data
== NULL
)
4945 memcpy(fastload
[i
].data
, buffer
+ offset
, length
);
4946 fastload
[i
].length
= length
;
4948 image_size
+= length
;
4949 command_print(CMD_CTX
, "%u bytes written at address 0x%8.8x",
4950 (unsigned int)length
,
4951 ((unsigned int)(image
.sections
[i
].base_address
+ offset
)));
4957 if ((ERROR_OK
== retval
) && (duration_measure(&bench
) == ERROR_OK
))
4959 command_print(CMD_CTX
, "Loaded %" PRIu32
" bytes "
4960 "in %fs (%0.3f KiB/s)", image_size
,
4961 duration_elapsed(&bench
), duration_kbps(&bench
, image_size
));
4963 command_print(CMD_CTX
,
4964 "WARNING: image has not been loaded to target!"
4965 "You can issue a 'fast_load' to finish loading.");
4968 image_close(&image
);
4970 if (retval
!= ERROR_OK
)
4978 COMMAND_HANDLER(handle_fast_load_command
)
4981 return ERROR_COMMAND_SYNTAX_ERROR
;
4982 if (fastload
== NULL
)
4984 LOG_ERROR("No image in memory");
4988 int ms
= timeval_ms();
4990 int retval
= ERROR_OK
;
4991 for (i
= 0; i
< fastload_num
;i
++)
4993 struct target
*target
= get_current_target(CMD_CTX
);
4994 command_print(CMD_CTX
, "Write to 0x%08x, length 0x%08x",
4995 (unsigned int)(fastload
[i
].address
),
4996 (unsigned int)(fastload
[i
].length
));
4997 if (retval
== ERROR_OK
)
4999 retval
= target_write_buffer(target
, fastload
[i
].address
, fastload
[i
].length
, fastload
[i
].data
);
5001 size
+= fastload
[i
].length
;
5003 int after
= timeval_ms();
5004 command_print(CMD_CTX
, "Loaded image %f kBytes/s", (float)(size
/1024.0)/((float)(after
-ms
)/1000.0));
5008 static const struct command_registration target_command_handlers
[] = {
5011 .handler
= handle_targets_command
,
5012 .mode
= COMMAND_ANY
,
5013 .help
= "change current default target (one parameter) "
5014 "or prints table of all targets (no parameters)",
5015 .usage
= "[target]",
5019 .mode
= COMMAND_CONFIG
,
5020 .help
= "configure target",
5022 .chain
= target_subcommand_handlers
,
5024 COMMAND_REGISTRATION_DONE
5027 int target_register_commands(struct command_context
*cmd_ctx
)
5029 return register_commands(cmd_ctx
, NULL
, target_command_handlers
);
5032 static bool target_reset_nag
= true;
5034 bool get_target_reset_nag(void)
5036 return target_reset_nag
;
5039 COMMAND_HANDLER(handle_target_reset_nag
)
5041 return CALL_COMMAND_HANDLER(handle_command_parse_bool
,
5042 &target_reset_nag
, "Nag after each reset about options to improve "
5046 static const struct command_registration target_exec_command_handlers
[] = {
5048 .name
= "fast_load_image",
5049 .handler
= handle_fast_load_image_command
,
5050 .mode
= COMMAND_ANY
,
5051 .help
= "Load image into server memory for later use by "
5052 "fast_load; primarily for profiling",
5053 .usage
= "filename address ['bin'|'ihex'|'elf'|'s19'] "
5054 "[min_address [max_length]]",
5057 .name
= "fast_load",
5058 .handler
= handle_fast_load_command
,
5059 .mode
= COMMAND_EXEC
,
5060 .help
= "loads active fast load image to current target "
5061 "- mainly for profiling purposes",
5065 .handler
= handle_profile_command
,
5066 .mode
= COMMAND_EXEC
,
5067 .help
= "profiling samples the CPU PC",
5069 /** @todo don't register virt2phys() unless target supports it */
5071 .name
= "virt2phys",
5072 .handler
= handle_virt2phys_command
,
5073 .mode
= COMMAND_ANY
,
5074 .help
= "translate a virtual address into a physical address",
5075 .usage
= "virtual_address",
5079 .handler
= handle_reg_command
,
5080 .mode
= COMMAND_EXEC
,
5081 .help
= "display or set a register; with no arguments, "
5082 "displays all registers and their values",
5083 .usage
= "[(register_name|register_number) [value]]",
5087 .handler
= handle_poll_command
,
5088 .mode
= COMMAND_EXEC
,
5089 .help
= "poll target state; or reconfigure background polling",
5090 .usage
= "['on'|'off']",
5093 .name
= "wait_halt",
5094 .handler
= handle_wait_halt_command
,
5095 .mode
= COMMAND_EXEC
,
5096 .help
= "wait up to the specified number of milliseconds "
5097 "(default 5) for a previously requested halt",
5098 .usage
= "[milliseconds]",
5102 .handler
= handle_halt_command
,
5103 .mode
= COMMAND_EXEC
,
5104 .help
= "request target to halt, then wait up to the specified"
5105 "number of milliseconds (default 5) for it to complete",
5106 .usage
= "[milliseconds]",
5110 .handler
= handle_resume_command
,
5111 .mode
= COMMAND_EXEC
,
5112 .help
= "resume target execution from current PC or address",
5113 .usage
= "[address]",
5117 .handler
= handle_reset_command
,
5118 .mode
= COMMAND_EXEC
,
5119 .usage
= "[run|halt|init]",
5120 .help
= "Reset all targets into the specified mode."
5121 "Default reset mode is run, if not given.",
5124 .name
= "soft_reset_halt",
5125 .handler
= handle_soft_reset_halt_command
,
5126 .mode
= COMMAND_EXEC
,
5127 .help
= "halt the target and do a soft reset",
5131 .handler
= handle_step_command
,
5132 .mode
= COMMAND_EXEC
,
5133 .help
= "step one instruction from current PC or address",
5134 .usage
= "[address]",
5138 .handler
= handle_md_command
,
5139 .mode
= COMMAND_EXEC
,
5140 .help
= "display memory words",
5141 .usage
= "['phys'] address [count]",
5145 .handler
= handle_md_command
,
5146 .mode
= COMMAND_EXEC
,
5147 .help
= "display memory half-words",
5148 .usage
= "['phys'] address [count]",
5152 .handler
= handle_md_command
,
5153 .mode
= COMMAND_EXEC
,
5154 .help
= "display memory bytes",
5155 .usage
= "['phys'] address [count]",
5159 .handler
= handle_mw_command
,
5160 .mode
= COMMAND_EXEC
,
5161 .help
= "write memory word",
5162 .usage
= "['phys'] address value [count]",
5166 .handler
= handle_mw_command
,
5167 .mode
= COMMAND_EXEC
,
5168 .help
= "write memory half-word",
5169 .usage
= "['phys'] address value [count]",
5173 .handler
= handle_mw_command
,
5174 .mode
= COMMAND_EXEC
,
5175 .help
= "write memory byte",
5176 .usage
= "['phys'] address value [count]",
5180 .handler
= handle_bp_command
,
5181 .mode
= COMMAND_EXEC
,
5182 .help
= "list or set hardware or software breakpoint",
5183 .usage
= "[address length ['hw']]",
5187 .handler
= handle_rbp_command
,
5188 .mode
= COMMAND_EXEC
,
5189 .help
= "remove breakpoint",
5194 .handler
= handle_wp_command
,
5195 .mode
= COMMAND_EXEC
,
5196 .help
= "list (no params) or create watchpoints",
5197 .usage
= "[address length [('r'|'w'|'a') value [mask]]]",
5201 .handler
= handle_rwp_command
,
5202 .mode
= COMMAND_EXEC
,
5203 .help
= "remove watchpoint",
5207 .name
= "load_image",
5208 .handler
= handle_load_image_command
,
5209 .mode
= COMMAND_EXEC
,
5210 .usage
= "filename address ['bin'|'ihex'|'elf'|'s19'] "
5211 "[min_address] [max_length]",
5214 .name
= "dump_image",
5215 .handler
= handle_dump_image_command
,
5216 .mode
= COMMAND_EXEC
,
5217 .usage
= "filename address size",
5220 .name
= "verify_image",
5221 .handler
= handle_verify_image_command
,
5222 .mode
= COMMAND_EXEC
,
5223 .usage
= "filename [offset [type]]",
5226 .name
= "test_image",
5227 .handler
= handle_test_image_command
,
5228 .mode
= COMMAND_EXEC
,
5229 .usage
= "filename [offset [type]]",
5232 .name
= "ocd_mem2array",
5233 .mode
= COMMAND_EXEC
,
5234 .jim_handler
= jim_mem2array
,
5235 .help
= "read 8/16/32 bit memory and return as a TCL array "
5236 "for script processing",
5237 .usage
= "arrayname bitwidth address count",
5240 .name
= "ocd_array2mem",
5241 .mode
= COMMAND_EXEC
,
5242 .jim_handler
= jim_array2mem
,
5243 .help
= "convert a TCL array to memory locations "
5244 "and write the 8/16/32 bit values",
5245 .usage
= "arrayname bitwidth address count",
5248 .name
= "reset_nag",
5249 .handler
= handle_target_reset_nag
,
5250 .mode
= COMMAND_ANY
,
5251 .help
= "Nag after each reset about options that could have been "
5252 "enabled to improve performance. ",
5253 .usage
= "['enable'|'disable']",
5255 COMMAND_REGISTRATION_DONE
5257 static int target_register_user_commands(struct command_context
*cmd_ctx
)
5259 int retval
= ERROR_OK
;
5260 if ((retval
= target_request_register_commands(cmd_ctx
)) != ERROR_OK
)
5263 if ((retval
= trace_register_commands(cmd_ctx
)) != ERROR_OK
)
5267 return register_commands(cmd_ctx
, NULL
, target_exec_command_handlers
);