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 arm946e_target
;
62 extern struct target_type arm926ejs_target
;
63 extern struct target_type fa526_target
;
64 extern struct target_type feroceon_target
;
65 extern struct target_type dragonite_target
;
66 extern struct target_type xscale_target
;
67 extern struct target_type cortexm3_target
;
68 extern struct target_type cortexa8_target
;
69 extern struct target_type arm11_target
;
70 extern struct target_type mips_m4k_target
;
71 extern struct target_type avr_target
;
72 extern struct target_type dsp563xx_target
;
73 extern struct target_type testee_target
;
74 extern struct target_type avr32_ap7k_target
;
76 static struct target_type
*target_types
[] =
100 struct target
*all_targets
= NULL
;
101 static struct target_event_callback
*target_event_callbacks
= NULL
;
102 static struct target_timer_callback
*target_timer_callbacks
= NULL
;
103 static const int polling_interval
= 100;
105 static const Jim_Nvp nvp_assert
[] = {
106 { .name
= "assert", NVP_ASSERT
},
107 { .name
= "deassert", NVP_DEASSERT
},
108 { .name
= "T", NVP_ASSERT
},
109 { .name
= "F", NVP_DEASSERT
},
110 { .name
= "t", NVP_ASSERT
},
111 { .name
= "f", NVP_DEASSERT
},
112 { .name
= NULL
, .value
= -1 }
115 static const Jim_Nvp nvp_error_target
[] = {
116 { .value
= ERROR_TARGET_INVALID
, .name
= "err-invalid" },
117 { .value
= ERROR_TARGET_INIT_FAILED
, .name
= "err-init-failed" },
118 { .value
= ERROR_TARGET_TIMEOUT
, .name
= "err-timeout" },
119 { .value
= ERROR_TARGET_NOT_HALTED
, .name
= "err-not-halted" },
120 { .value
= ERROR_TARGET_FAILURE
, .name
= "err-failure" },
121 { .value
= ERROR_TARGET_UNALIGNED_ACCESS
, .name
= "err-unaligned-access" },
122 { .value
= ERROR_TARGET_DATA_ABORT
, .name
= "err-data-abort" },
123 { .value
= ERROR_TARGET_RESOURCE_NOT_AVAILABLE
, .name
= "err-resource-not-available" },
124 { .value
= ERROR_TARGET_TRANSLATION_FAULT
, .name
= "err-translation-fault" },
125 { .value
= ERROR_TARGET_NOT_RUNNING
, .name
= "err-not-running" },
126 { .value
= ERROR_TARGET_NOT_EXAMINED
, .name
= "err-not-examined" },
127 { .value
= -1, .name
= NULL
}
130 static const char *target_strerror_safe(int err
)
134 n
= Jim_Nvp_value2name_simple(nvp_error_target
, err
);
135 if (n
->name
== NULL
) {
142 static const Jim_Nvp nvp_target_event
[] = {
143 { .value
= TARGET_EVENT_OLD_gdb_program_config
, .name
= "old-gdb_program_config" },
144 { .value
= TARGET_EVENT_OLD_pre_resume
, .name
= "old-pre_resume" },
146 { .value
= TARGET_EVENT_GDB_HALT
, .name
= "gdb-halt" },
147 { .value
= TARGET_EVENT_HALTED
, .name
= "halted" },
148 { .value
= TARGET_EVENT_RESUMED
, .name
= "resumed" },
149 { .value
= TARGET_EVENT_RESUME_START
, .name
= "resume-start" },
150 { .value
= TARGET_EVENT_RESUME_END
, .name
= "resume-end" },
152 { .name
= "gdb-start", .value
= TARGET_EVENT_GDB_START
},
153 { .name
= "gdb-end", .value
= TARGET_EVENT_GDB_END
},
155 /* historical name */
157 { .value
= TARGET_EVENT_RESET_START
, .name
= "reset-start" },
159 { .value
= TARGET_EVENT_RESET_ASSERT_PRE
, .name
= "reset-assert-pre" },
160 { .value
= TARGET_EVENT_RESET_ASSERT
, .name
= "reset-assert" },
161 { .value
= TARGET_EVENT_RESET_ASSERT_POST
, .name
= "reset-assert-post" },
162 { .value
= TARGET_EVENT_RESET_DEASSERT_PRE
, .name
= "reset-deassert-pre" },
163 { .value
= TARGET_EVENT_RESET_DEASSERT_POST
, .name
= "reset-deassert-post" },
164 { .value
= TARGET_EVENT_RESET_HALT_PRE
, .name
= "reset-halt-pre" },
165 { .value
= TARGET_EVENT_RESET_HALT_POST
, .name
= "reset-halt-post" },
166 { .value
= TARGET_EVENT_RESET_WAIT_PRE
, .name
= "reset-wait-pre" },
167 { .value
= TARGET_EVENT_RESET_WAIT_POST
, .name
= "reset-wait-post" },
168 { .value
= TARGET_EVENT_RESET_INIT
, .name
= "reset-init" },
169 { .value
= TARGET_EVENT_RESET_END
, .name
= "reset-end" },
171 { .value
= TARGET_EVENT_EXAMINE_START
, .name
= "examine-start" },
172 { .value
= TARGET_EVENT_EXAMINE_END
, .name
= "examine-end" },
174 { .value
= TARGET_EVENT_DEBUG_HALTED
, .name
= "debug-halted" },
175 { .value
= TARGET_EVENT_DEBUG_RESUMED
, .name
= "debug-resumed" },
177 { .value
= TARGET_EVENT_GDB_ATTACH
, .name
= "gdb-attach" },
178 { .value
= TARGET_EVENT_GDB_DETACH
, .name
= "gdb-detach" },
180 { .value
= TARGET_EVENT_GDB_FLASH_WRITE_START
, .name
= "gdb-flash-write-start" },
181 { .value
= TARGET_EVENT_GDB_FLASH_WRITE_END
, .name
= "gdb-flash-write-end" },
183 { .value
= TARGET_EVENT_GDB_FLASH_ERASE_START
, .name
= "gdb-flash-erase-start" },
184 { .value
= TARGET_EVENT_GDB_FLASH_ERASE_END
, .name
= "gdb-flash-erase-end" },
186 { .value
= TARGET_EVENT_RESUME_START
, .name
= "resume-start" },
187 { .value
= TARGET_EVENT_RESUMED
, .name
= "resume-ok" },
188 { .value
= TARGET_EVENT_RESUME_END
, .name
= "resume-end" },
190 { .name
= NULL
, .value
= -1 }
193 static const Jim_Nvp nvp_target_state
[] = {
194 { .name
= "unknown", .value
= TARGET_UNKNOWN
},
195 { .name
= "running", .value
= TARGET_RUNNING
},
196 { .name
= "halted", .value
= TARGET_HALTED
},
197 { .name
= "reset", .value
= TARGET_RESET
},
198 { .name
= "debug-running", .value
= TARGET_DEBUG_RUNNING
},
199 { .name
= NULL
, .value
= -1 },
202 static const Jim_Nvp nvp_target_debug_reason
[] = {
203 { .name
= "debug-request" , .value
= DBG_REASON_DBGRQ
},
204 { .name
= "breakpoint" , .value
= DBG_REASON_BREAKPOINT
},
205 { .name
= "watchpoint" , .value
= DBG_REASON_WATCHPOINT
},
206 { .name
= "watchpoint-and-breakpoint", .value
= DBG_REASON_WPTANDBKPT
},
207 { .name
= "single-step" , .value
= DBG_REASON_SINGLESTEP
},
208 { .name
= "target-not-halted" , .value
= DBG_REASON_NOTHALTED
},
209 { .name
= "undefined" , .value
= DBG_REASON_UNDEFINED
},
210 { .name
= NULL
, .value
= -1 },
213 static const Jim_Nvp nvp_target_endian
[] = {
214 { .name
= "big", .value
= TARGET_BIG_ENDIAN
},
215 { .name
= "little", .value
= TARGET_LITTLE_ENDIAN
},
216 { .name
= "be", .value
= TARGET_BIG_ENDIAN
},
217 { .name
= "le", .value
= TARGET_LITTLE_ENDIAN
},
218 { .name
= NULL
, .value
= -1 },
221 static const Jim_Nvp nvp_reset_modes
[] = {
222 { .name
= "unknown", .value
= RESET_UNKNOWN
},
223 { .name
= "run" , .value
= RESET_RUN
},
224 { .name
= "halt" , .value
= RESET_HALT
},
225 { .name
= "init" , .value
= RESET_INIT
},
226 { .name
= NULL
, .value
= -1 },
229 const char *debug_reason_name(struct target
*t
)
233 cp
= Jim_Nvp_value2name_simple(nvp_target_debug_reason
,
234 t
->debug_reason
)->name
;
236 LOG_ERROR("Invalid debug reason: %d", (int)(t
->debug_reason
));
237 cp
= "(*BUG*unknown*BUG*)";
243 target_state_name( struct target
*t
)
246 cp
= Jim_Nvp_value2name_simple(nvp_target_state
, t
->state
)->name
;
248 LOG_ERROR("Invalid target state: %d", (int)(t
->state
));
249 cp
= "(*BUG*unknown*BUG*)";
254 /* determine the number of the new target */
255 static int new_target_number(void)
260 /* number is 0 based */
264 if (x
< t
->target_number
) {
265 x
= t
->target_number
;
272 /* read a uint32_t from a buffer in target memory endianness */
273 uint32_t target_buffer_get_u32(struct target
*target
, const uint8_t *buffer
)
275 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
276 return le_to_h_u32(buffer
);
278 return be_to_h_u32(buffer
);
281 /* read a uint16_t from a buffer in target memory endianness */
282 uint16_t target_buffer_get_u16(struct target
*target
, const uint8_t *buffer
)
284 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
285 return le_to_h_u16(buffer
);
287 return be_to_h_u16(buffer
);
290 /* read a uint8_t from a buffer in target memory endianness */
291 static uint8_t target_buffer_get_u8(struct target
*target
, const uint8_t *buffer
)
293 return *buffer
& 0x0ff;
296 /* write a uint32_t to a buffer in target memory endianness */
297 void target_buffer_set_u32(struct target
*target
, uint8_t *buffer
, uint32_t value
)
299 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
300 h_u32_to_le(buffer
, value
);
302 h_u32_to_be(buffer
, value
);
305 /* write a uint16_t to a buffer in target memory endianness */
306 void target_buffer_set_u16(struct target
*target
, uint8_t *buffer
, uint16_t value
)
308 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
309 h_u16_to_le(buffer
, value
);
311 h_u16_to_be(buffer
, value
);
314 /* write a uint8_t to a buffer in target memory endianness */
315 static void target_buffer_set_u8(struct target
*target
, uint8_t *buffer
, uint8_t value
)
320 /* return a pointer to a configured target; id is name or number */
321 struct target
*get_target(const char *id
)
323 struct target
*target
;
325 /* try as tcltarget name */
326 for (target
= all_targets
; target
; target
= target
->next
) {
327 if (target
->cmd_name
== NULL
)
329 if (strcmp(id
, target
->cmd_name
) == 0)
333 /* It's OK to remove this fallback sometime after August 2010 or so */
335 /* no match, try as number */
337 if (parse_uint(id
, &num
) != ERROR_OK
)
340 for (target
= all_targets
; target
; target
= target
->next
) {
341 if (target
->target_number
== (int)num
) {
342 LOG_WARNING("use '%s' as target identifier, not '%u'",
343 target
->cmd_name
, num
);
351 /* returns a pointer to the n-th configured target */
352 static struct target
*get_target_by_num(int num
)
354 struct target
*target
= all_targets
;
357 if (target
->target_number
== num
) {
360 target
= target
->next
;
366 struct target
* get_current_target(struct command_context
*cmd_ctx
)
368 struct target
*target
= get_target_by_num(cmd_ctx
->current_target
);
372 LOG_ERROR("BUG: current_target out of bounds");
379 int target_poll(struct target
*target
)
383 /* We can't poll until after examine */
384 if (!target_was_examined(target
))
386 /* Fail silently lest we pollute the log */
390 retval
= target
->type
->poll(target
);
391 if (retval
!= ERROR_OK
)
394 if (target
->halt_issued
)
396 if (target
->state
== TARGET_HALTED
)
398 target
->halt_issued
= false;
401 long long t
= timeval_ms() - target
->halt_issued_time
;
404 target
->halt_issued
= false;
405 LOG_INFO("Halt timed out, wake up GDB.");
406 target_call_event_callbacks(target
, TARGET_EVENT_GDB_HALT
);
414 int target_halt(struct target
*target
)
417 /* We can't poll until after examine */
418 if (!target_was_examined(target
))
420 LOG_ERROR("Target not examined yet");
424 retval
= target
->type
->halt(target
);
425 if (retval
!= ERROR_OK
)
428 target
->halt_issued
= true;
429 target
->halt_issued_time
= timeval_ms();
435 * Make the target (re)start executing using its saved execution
436 * context (possibly with some modifications).
438 * @param target Which target should start executing.
439 * @param current True to use the target's saved program counter instead
440 * of the address parameter
441 * @param address Optionally used as the program counter.
442 * @param handle_breakpoints True iff breakpoints at the resumption PC
443 * should be skipped. (For example, maybe execution was stopped by
444 * such a breakpoint, in which case it would be counterprodutive to
446 * @param debug_execution False if all working areas allocated by OpenOCD
447 * should be released and/or restored to their original contents.
448 * (This would for example be true to run some downloaded "helper"
449 * algorithm code, which resides in one such working buffer and uses
450 * another for data storage.)
452 * @todo Resolve the ambiguity about what the "debug_execution" flag
453 * signifies. For example, Target implementations don't agree on how
454 * it relates to invalidation of the register cache, or to whether
455 * breakpoints and watchpoints should be enabled. (It would seem wrong
456 * to enable breakpoints when running downloaded "helper" algorithms
457 * (debug_execution true), since the breakpoints would be set to match
458 * target firmware being debugged, not the helper algorithm.... and
459 * enabling them could cause such helpers to malfunction (for example,
460 * by overwriting data with a breakpoint instruction. On the other
461 * hand the infrastructure for running such helpers might use this
462 * procedure but rely on hardware breakpoint to detect termination.)
464 int target_resume(struct target
*target
, int current
, uint32_t address
, int handle_breakpoints
, int debug_execution
)
468 /* We can't poll until after examine */
469 if (!target_was_examined(target
))
471 LOG_ERROR("Target not examined yet");
475 /* note that resume *must* be asynchronous. The CPU can halt before
476 * we poll. The CPU can even halt at the current PC as a result of
477 * a software breakpoint being inserted by (a bug?) the application.
479 if ((retval
= target
->type
->resume(target
, current
, address
, handle_breakpoints
, debug_execution
)) != ERROR_OK
)
485 static int target_process_reset(struct command_context
*cmd_ctx
, enum target_reset_mode reset_mode
)
490 n
= Jim_Nvp_value2name_simple(nvp_reset_modes
, reset_mode
);
491 if (n
->name
== NULL
) {
492 LOG_ERROR("invalid reset mode");
496 /* disable polling during reset to make reset event scripts
497 * more predictable, i.e. dr/irscan & pathmove in events will
498 * not have JTAG operations injected into the middle of a sequence.
500 bool save_poll
= jtag_poll_get_enabled();
502 jtag_poll_set_enabled(false);
504 sprintf(buf
, "ocd_process_reset %s", n
->name
);
505 retval
= Jim_Eval(cmd_ctx
->interp
, buf
);
507 jtag_poll_set_enabled(save_poll
);
509 if (retval
!= JIM_OK
) {
510 Jim_MakeErrorMessage(cmd_ctx
->interp
);
511 command_print(NULL
,"%s\n", Jim_GetString(Jim_GetResult(cmd_ctx
->interp
), NULL
));
515 /* We want any events to be processed before the prompt */
516 retval
= target_call_timer_callbacks_now();
518 struct target
*target
;
519 for (target
= all_targets
; target
; target
= target
->next
) {
520 target
->type
->check_reset(target
);
526 static int identity_virt2phys(struct target
*target
,
527 uint32_t virtual, uint32_t *physical
)
533 static int no_mmu(struct target
*target
, int *enabled
)
539 static int default_examine(struct target
*target
)
541 target_set_examined(target
);
545 /* no check by default */
546 static int default_check_reset(struct target
*target
)
551 int target_examine_one(struct target
*target
)
553 return target
->type
->examine(target
);
556 static int jtag_enable_callback(enum jtag_event event
, void *priv
)
558 struct target
*target
= priv
;
560 if (event
!= JTAG_TAP_EVENT_ENABLE
|| !target
->tap
->enabled
)
563 jtag_unregister_event_callback(jtag_enable_callback
, target
);
564 return target_examine_one(target
);
568 /* Targets that correctly implement init + examine, i.e.
569 * no communication with target during init:
573 int target_examine(void)
575 int retval
= ERROR_OK
;
576 struct target
*target
;
578 for (target
= all_targets
; target
; target
= target
->next
)
580 /* defer examination, but don't skip it */
581 if (!target
->tap
->enabled
) {
582 jtag_register_event_callback(jtag_enable_callback
,
586 if ((retval
= target_examine_one(target
)) != ERROR_OK
)
591 const char *target_type_name(struct target
*target
)
593 return target
->type
->name
;
596 static int target_write_memory_imp(struct target
*target
, uint32_t address
, uint32_t size
, uint32_t count
, uint8_t *buffer
)
598 if (!target_was_examined(target
))
600 LOG_ERROR("Target not examined yet");
603 return target
->type
->write_memory_imp(target
, address
, size
, count
, buffer
);
606 static int target_read_memory_imp(struct target
*target
, uint32_t address
, uint32_t size
, uint32_t count
, uint8_t *buffer
)
608 if (!target_was_examined(target
))
610 LOG_ERROR("Target not examined yet");
613 return target
->type
->read_memory_imp(target
, address
, size
, count
, buffer
);
616 static int target_soft_reset_halt_imp(struct target
*target
)
618 if (!target_was_examined(target
))
620 LOG_ERROR("Target not examined yet");
623 if (!target
->type
->soft_reset_halt_imp
) {
624 LOG_ERROR("Target %s does not support soft_reset_halt",
625 target_name(target
));
628 return target
->type
->soft_reset_halt_imp(target
);
632 * Downloads a target-specific native code algorithm to the target,
633 * and executes it. * Note that some targets may need to set up, enable,
634 * and tear down a breakpoint (hard or * soft) to detect algorithm
635 * termination, while others may support lower overhead schemes where
636 * soft breakpoints embedded in the algorithm automatically terminate the
639 * @param target used to run the algorithm
640 * @param arch_info target-specific description of the algorithm.
642 int target_run_algorithm(struct target
*target
,
643 int num_mem_params
, struct mem_param
*mem_params
,
644 int num_reg_params
, struct reg_param
*reg_param
,
645 uint32_t entry_point
, uint32_t exit_point
,
646 int timeout_ms
, void *arch_info
)
648 int retval
= ERROR_FAIL
;
650 if (!target_was_examined(target
))
652 LOG_ERROR("Target not examined yet");
655 if (!target
->type
->run_algorithm
) {
656 LOG_ERROR("Target type '%s' does not support %s",
657 target_type_name(target
), __func__
);
661 target
->running_alg
= true;
662 retval
= target
->type
->run_algorithm(target
,
663 num_mem_params
, mem_params
,
664 num_reg_params
, reg_param
,
665 entry_point
, exit_point
, timeout_ms
, arch_info
);
666 target
->running_alg
= false;
673 int target_read_memory(struct target
*target
,
674 uint32_t address
, uint32_t size
, uint32_t count
, uint8_t *buffer
)
676 return target
->type
->read_memory(target
, address
, size
, count
, buffer
);
679 static int target_read_phys_memory(struct target
*target
,
680 uint32_t address
, uint32_t size
, uint32_t count
, uint8_t *buffer
)
682 return target
->type
->read_phys_memory(target
, address
, size
, count
, buffer
);
685 int target_write_memory(struct target
*target
,
686 uint32_t address
, uint32_t size
, uint32_t count
, uint8_t *buffer
)
688 return target
->type
->write_memory(target
, address
, size
, count
, buffer
);
691 static int target_write_phys_memory(struct target
*target
,
692 uint32_t address
, uint32_t size
, uint32_t count
, uint8_t *buffer
)
694 return target
->type
->write_phys_memory(target
, address
, size
, count
, buffer
);
697 int target_bulk_write_memory(struct target
*target
,
698 uint32_t address
, uint32_t count
, uint8_t *buffer
)
700 return target
->type
->bulk_write_memory(target
, address
, count
, buffer
);
703 int target_add_breakpoint(struct target
*target
,
704 struct breakpoint
*breakpoint
)
706 if (target
->state
!= TARGET_HALTED
) {
707 LOG_WARNING("target %s is not halted", target
->cmd_name
);
708 return ERROR_TARGET_NOT_HALTED
;
710 return target
->type
->add_breakpoint(target
, breakpoint
);
712 int target_remove_breakpoint(struct target
*target
,
713 struct breakpoint
*breakpoint
)
715 return target
->type
->remove_breakpoint(target
, breakpoint
);
718 int target_add_watchpoint(struct target
*target
,
719 struct watchpoint
*watchpoint
)
721 if (target
->state
!= TARGET_HALTED
) {
722 LOG_WARNING("target %s is not halted", target
->cmd_name
);
723 return ERROR_TARGET_NOT_HALTED
;
725 return target
->type
->add_watchpoint(target
, watchpoint
);
727 int target_remove_watchpoint(struct target
*target
,
728 struct watchpoint
*watchpoint
)
730 return target
->type
->remove_watchpoint(target
, watchpoint
);
733 int target_get_gdb_reg_list(struct target
*target
,
734 struct reg
**reg_list
[], int *reg_list_size
)
736 return target
->type
->get_gdb_reg_list(target
, reg_list
, reg_list_size
);
738 int target_step(struct target
*target
,
739 int current
, uint32_t address
, int handle_breakpoints
)
741 return target
->type
->step(target
, current
, address
, handle_breakpoints
);
746 * Reset the @c examined flag for the given target.
747 * Pure paranoia -- targets are zeroed on allocation.
749 static void target_reset_examined(struct target
*target
)
751 target
->examined
= false;
755 err_read_phys_memory(struct target
*target
, uint32_t address
,
756 uint32_t size
, uint32_t count
, uint8_t *buffer
)
758 LOG_ERROR("Not implemented: %s", __func__
);
763 err_write_phys_memory(struct target
*target
, uint32_t address
,
764 uint32_t size
, uint32_t count
, uint8_t *buffer
)
766 LOG_ERROR("Not implemented: %s", __func__
);
770 static int handle_target(void *priv
);
772 static int target_init_one(struct command_context
*cmd_ctx
,
773 struct target
*target
)
775 target_reset_examined(target
);
777 struct target_type
*type
= target
->type
;
778 if (type
->examine
== NULL
)
779 type
->examine
= default_examine
;
781 if (type
->check_reset
== NULL
)
782 type
->check_reset
= default_check_reset
;
784 int retval
= type
->init_target(cmd_ctx
, target
);
785 if (ERROR_OK
!= retval
)
787 LOG_ERROR("target '%s' init failed", target_name(target
));
792 * @todo get rid of those *memory_imp() methods, now that all
793 * callers are using target_*_memory() accessors ... and make
794 * sure the "physical" paths handle the same issues.
796 /* a non-invasive way(in terms of patches) to add some code that
797 * runs before the type->write/read_memory implementation
799 type
->write_memory_imp
= target
->type
->write_memory
;
800 type
->write_memory
= target_write_memory_imp
;
802 type
->read_memory_imp
= target
->type
->read_memory
;
803 type
->read_memory
= target_read_memory_imp
;
805 type
->soft_reset_halt_imp
= target
->type
->soft_reset_halt
;
806 type
->soft_reset_halt
= target_soft_reset_halt_imp
;
808 /* Sanity-check MMU support ... stub in what we must, to help
809 * implement it in stages, but warn if we need to do so.
813 if (type
->write_phys_memory
== NULL
)
815 LOG_ERROR("type '%s' is missing write_phys_memory",
817 type
->write_phys_memory
= err_write_phys_memory
;
819 if (type
->read_phys_memory
== NULL
)
821 LOG_ERROR("type '%s' is missing read_phys_memory",
823 type
->read_phys_memory
= err_read_phys_memory
;
825 if (type
->virt2phys
== NULL
)
827 LOG_ERROR("type '%s' is missing virt2phys", type
->name
);
828 type
->virt2phys
= identity_virt2phys
;
833 /* Make sure no-MMU targets all behave the same: make no
834 * distinction between physical and virtual addresses, and
835 * ensure that virt2phys() is always an identity mapping.
837 if (type
->write_phys_memory
|| type
->read_phys_memory
840 LOG_WARNING("type '%s' has bad MMU hooks", type
->name
);
844 type
->write_phys_memory
= type
->write_memory
;
845 type
->read_phys_memory
= type
->read_memory
;
846 type
->virt2phys
= identity_virt2phys
;
851 static int target_init(struct command_context
*cmd_ctx
)
853 struct target
*target
;
856 for (target
= all_targets
; target
; target
= target
->next
)
858 retval
= target_init_one(cmd_ctx
, target
);
859 if (ERROR_OK
!= retval
)
866 retval
= target_register_user_commands(cmd_ctx
);
867 if (ERROR_OK
!= retval
)
870 retval
= target_register_timer_callback(&handle_target
,
871 polling_interval
, 1, cmd_ctx
->interp
);
872 if (ERROR_OK
!= retval
)
878 COMMAND_HANDLER(handle_target_init_command
)
881 return ERROR_COMMAND_SYNTAX_ERROR
;
883 static bool target_initialized
= false;
884 if (target_initialized
)
886 LOG_INFO("'target init' has already been called");
889 target_initialized
= true;
891 LOG_DEBUG("Initializing targets...");
892 return target_init(CMD_CTX
);
895 int target_register_event_callback(int (*callback
)(struct target
*target
, enum target_event event
, void *priv
), void *priv
)
897 struct target_event_callback
**callbacks_p
= &target_event_callbacks
;
899 if (callback
== NULL
)
901 return ERROR_INVALID_ARGUMENTS
;
906 while ((*callbacks_p
)->next
)
907 callbacks_p
= &((*callbacks_p
)->next
);
908 callbacks_p
= &((*callbacks_p
)->next
);
911 (*callbacks_p
) = malloc(sizeof(struct target_event_callback
));
912 (*callbacks_p
)->callback
= callback
;
913 (*callbacks_p
)->priv
= priv
;
914 (*callbacks_p
)->next
= NULL
;
919 int target_register_timer_callback(int (*callback
)(void *priv
), int time_ms
, int periodic
, void *priv
)
921 struct target_timer_callback
**callbacks_p
= &target_timer_callbacks
;
924 if (callback
== NULL
)
926 return ERROR_INVALID_ARGUMENTS
;
931 while ((*callbacks_p
)->next
)
932 callbacks_p
= &((*callbacks_p
)->next
);
933 callbacks_p
= &((*callbacks_p
)->next
);
936 (*callbacks_p
) = malloc(sizeof(struct target_timer_callback
));
937 (*callbacks_p
)->callback
= callback
;
938 (*callbacks_p
)->periodic
= periodic
;
939 (*callbacks_p
)->time_ms
= time_ms
;
941 gettimeofday(&now
, NULL
);
942 (*callbacks_p
)->when
.tv_usec
= now
.tv_usec
+ (time_ms
% 1000) * 1000;
943 time_ms
-= (time_ms
% 1000);
944 (*callbacks_p
)->when
.tv_sec
= now
.tv_sec
+ (time_ms
/ 1000);
945 if ((*callbacks_p
)->when
.tv_usec
> 1000000)
947 (*callbacks_p
)->when
.tv_usec
= (*callbacks_p
)->when
.tv_usec
- 1000000;
948 (*callbacks_p
)->when
.tv_sec
+= 1;
951 (*callbacks_p
)->priv
= priv
;
952 (*callbacks_p
)->next
= NULL
;
957 int target_unregister_event_callback(int (*callback
)(struct target
*target
, enum target_event event
, void *priv
), void *priv
)
959 struct target_event_callback
**p
= &target_event_callbacks
;
960 struct target_event_callback
*c
= target_event_callbacks
;
962 if (callback
== NULL
)
964 return ERROR_INVALID_ARGUMENTS
;
969 struct target_event_callback
*next
= c
->next
;
970 if ((c
->callback
== callback
) && (c
->priv
== priv
))
984 static int target_unregister_timer_callback(int (*callback
)(void *priv
), void *priv
)
986 struct target_timer_callback
**p
= &target_timer_callbacks
;
987 struct target_timer_callback
*c
= target_timer_callbacks
;
989 if (callback
== NULL
)
991 return ERROR_INVALID_ARGUMENTS
;
996 struct target_timer_callback
*next
= c
->next
;
997 if ((c
->callback
== callback
) && (c
->priv
== priv
))
1011 int target_call_event_callbacks(struct target
*target
, enum target_event event
)
1013 struct target_event_callback
*callback
= target_event_callbacks
;
1014 struct target_event_callback
*next_callback
;
1016 if (event
== TARGET_EVENT_HALTED
)
1018 /* execute early halted first */
1019 target_call_event_callbacks(target
, TARGET_EVENT_GDB_HALT
);
1022 LOG_DEBUG("target event %i (%s)",
1024 Jim_Nvp_value2name_simple(nvp_target_event
, event
)->name
);
1026 target_handle_event(target
, event
);
1030 next_callback
= callback
->next
;
1031 callback
->callback(target
, event
, callback
->priv
);
1032 callback
= next_callback
;
1038 static int target_timer_callback_periodic_restart(
1039 struct target_timer_callback
*cb
, struct timeval
*now
)
1041 int time_ms
= cb
->time_ms
;
1042 cb
->when
.tv_usec
= now
->tv_usec
+ (time_ms
% 1000) * 1000;
1043 time_ms
-= (time_ms
% 1000);
1044 cb
->when
.tv_sec
= now
->tv_sec
+ time_ms
/ 1000;
1045 if (cb
->when
.tv_usec
> 1000000)
1047 cb
->when
.tv_usec
= cb
->when
.tv_usec
- 1000000;
1048 cb
->when
.tv_sec
+= 1;
1053 static int target_call_timer_callback(struct target_timer_callback
*cb
,
1054 struct timeval
*now
)
1056 cb
->callback(cb
->priv
);
1059 return target_timer_callback_periodic_restart(cb
, now
);
1061 return target_unregister_timer_callback(cb
->callback
, cb
->priv
);
1064 static int target_call_timer_callbacks_check_time(int checktime
)
1069 gettimeofday(&now
, NULL
);
1071 struct target_timer_callback
*callback
= target_timer_callbacks
;
1074 // cleaning up may unregister and free this callback
1075 struct target_timer_callback
*next_callback
= callback
->next
;
1077 bool call_it
= callback
->callback
&&
1078 ((!checktime
&& callback
->periodic
) ||
1079 now
.tv_sec
> callback
->when
.tv_sec
||
1080 (now
.tv_sec
== callback
->when
.tv_sec
&&
1081 now
.tv_usec
>= callback
->when
.tv_usec
));
1085 int retval
= target_call_timer_callback(callback
, &now
);
1086 if (retval
!= ERROR_OK
)
1090 callback
= next_callback
;
1096 int target_call_timer_callbacks(void)
1098 return target_call_timer_callbacks_check_time(1);
1101 /* invoke periodic callbacks immediately */
1102 int target_call_timer_callbacks_now(void)
1104 return target_call_timer_callbacks_check_time(0);
1107 int target_alloc_working_area_try(struct target
*target
, uint32_t size
, struct working_area
**area
)
1109 struct working_area
*c
= target
->working_areas
;
1110 struct working_area
*new_wa
= NULL
;
1112 /* Reevaluate working area address based on MMU state*/
1113 if (target
->working_areas
== NULL
)
1118 retval
= target
->type
->mmu(target
, &enabled
);
1119 if (retval
!= ERROR_OK
)
1125 if (target
->working_area_phys_spec
) {
1126 LOG_DEBUG("MMU disabled, using physical "
1127 "address for working memory 0x%08x",
1128 (unsigned)target
->working_area_phys
);
1129 target
->working_area
= target
->working_area_phys
;
1131 LOG_ERROR("No working memory available. "
1132 "Specify -work-area-phys to target.");
1133 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
1136 if (target
->working_area_virt_spec
) {
1137 LOG_DEBUG("MMU enabled, using virtual "
1138 "address for working memory 0x%08x",
1139 (unsigned)target
->working_area_virt
);
1140 target
->working_area
= target
->working_area_virt
;
1142 LOG_ERROR("No working memory available. "
1143 "Specify -work-area-virt to target.");
1144 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
1149 /* only allocate multiples of 4 byte */
1152 LOG_ERROR("BUG: code tried to allocate unaligned number of bytes (0x%08x), padding", ((unsigned)(size
)));
1153 size
= (size
+ 3) & (~3);
1156 /* see if there's already a matching working area */
1159 if ((c
->free
) && (c
->size
== size
))
1167 /* if not, allocate a new one */
1170 struct working_area
**p
= &target
->working_areas
;
1171 uint32_t first_free
= target
->working_area
;
1172 uint32_t free_size
= target
->working_area_size
;
1174 c
= target
->working_areas
;
1177 first_free
+= c
->size
;
1178 free_size
-= c
->size
;
1183 if (free_size
< size
)
1185 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
1188 LOG_DEBUG("allocated new working area at address 0x%08x", (unsigned)first_free
);
1190 new_wa
= malloc(sizeof(struct working_area
));
1191 new_wa
->next
= NULL
;
1192 new_wa
->size
= size
;
1193 new_wa
->address
= first_free
;
1195 if (target
->backup_working_area
)
1198 new_wa
->backup
= malloc(new_wa
->size
);
1199 if ((retval
= target_read_memory(target
, new_wa
->address
, 4, new_wa
->size
/ 4, new_wa
->backup
)) != ERROR_OK
)
1201 free(new_wa
->backup
);
1208 new_wa
->backup
= NULL
;
1211 /* put new entry in list */
1215 /* mark as used, and return the new (reused) area */
1216 new_wa
->free
= false;
1220 new_wa
->user
= area
;
1225 int target_alloc_working_area(struct target
*target
, uint32_t size
, struct working_area
**area
)
1229 retval
= target_alloc_working_area_try(target
, size
, area
);
1230 if (retval
== ERROR_TARGET_RESOURCE_NOT_AVAILABLE
)
1232 LOG_WARNING("not enough working area available(requested %u)", (unsigned)(size
));
1238 static int target_free_working_area_restore(struct target
*target
, struct working_area
*area
, int restore
)
1243 if (restore
&& target
->backup_working_area
)
1246 if ((retval
= target_write_memory(target
, area
->address
, 4, area
->size
/ 4, area
->backup
)) != ERROR_OK
)
1252 /* mark user pointer invalid */
1259 int target_free_working_area(struct target
*target
, struct working_area
*area
)
1261 return target_free_working_area_restore(target
, area
, 1);
1264 /* free resources and restore memory, if restoring memory fails,
1265 * free up resources anyway
1267 static void target_free_all_working_areas_restore(struct target
*target
, int restore
)
1269 struct working_area
*c
= target
->working_areas
;
1273 struct working_area
*next
= c
->next
;
1274 target_free_working_area_restore(target
, c
, restore
);
1284 target
->working_areas
= NULL
;
1287 void target_free_all_working_areas(struct target
*target
)
1289 target_free_all_working_areas_restore(target
, 1);
1292 int target_arch_state(struct target
*target
)
1297 LOG_USER("No target has been configured");
1301 LOG_USER("target state: %s", target_state_name( target
));
1303 if (target
->state
!= TARGET_HALTED
)
1306 retval
= target
->type
->arch_state(target
);
1310 /* Single aligned words are guaranteed to use 16 or 32 bit access
1311 * mode respectively, otherwise data is handled as quickly as
1314 int target_write_buffer(struct target
*target
, uint32_t address
, uint32_t size
, uint8_t *buffer
)
1317 LOG_DEBUG("writing buffer of %i byte at 0x%8.8x",
1318 (int)size
, (unsigned)address
);
1320 if (!target_was_examined(target
))
1322 LOG_ERROR("Target not examined yet");
1330 if ((address
+ size
- 1) < address
)
1332 /* GDB can request this when e.g. PC is 0xfffffffc*/
1333 LOG_ERROR("address + size wrapped(0x%08x, 0x%08x)",
1339 if (((address
% 2) == 0) && (size
== 2))
1341 return target_write_memory(target
, address
, 2, 1, buffer
);
1344 /* handle unaligned head bytes */
1347 uint32_t unaligned
= 4 - (address
% 4);
1349 if (unaligned
> size
)
1352 if ((retval
= target_write_memory(target
, address
, 1, unaligned
, buffer
)) != ERROR_OK
)
1355 buffer
+= unaligned
;
1356 address
+= unaligned
;
1360 /* handle aligned words */
1363 int aligned
= size
- (size
% 4);
1365 /* use bulk writes above a certain limit. This may have to be changed */
1368 if ((retval
= target
->type
->bulk_write_memory(target
, address
, aligned
/ 4, buffer
)) != ERROR_OK
)
1373 if ((retval
= target_write_memory(target
, address
, 4, aligned
/ 4, buffer
)) != ERROR_OK
)
1382 /* handle tail writes of less than 4 bytes */
1385 if ((retval
= target_write_memory(target
, address
, 1, size
, buffer
)) != ERROR_OK
)
1392 /* Single aligned words are guaranteed to use 16 or 32 bit access
1393 * mode respectively, otherwise data is handled as quickly as
1396 int target_read_buffer(struct target
*target
, uint32_t address
, uint32_t size
, uint8_t *buffer
)
1399 LOG_DEBUG("reading buffer of %i byte at 0x%8.8x",
1400 (int)size
, (unsigned)address
);
1402 if (!target_was_examined(target
))
1404 LOG_ERROR("Target not examined yet");
1412 if ((address
+ size
- 1) < address
)
1414 /* GDB can request this when e.g. PC is 0xfffffffc*/
1415 LOG_ERROR("address + size wrapped(0x%08" PRIx32
", 0x%08" PRIx32
")",
1421 if (((address
% 2) == 0) && (size
== 2))
1423 return target_read_memory(target
, address
, 2, 1, buffer
);
1426 /* handle unaligned head bytes */
1429 uint32_t unaligned
= 4 - (address
% 4);
1431 if (unaligned
> size
)
1434 if ((retval
= target_read_memory(target
, address
, 1, unaligned
, buffer
)) != ERROR_OK
)
1437 buffer
+= unaligned
;
1438 address
+= unaligned
;
1442 /* handle aligned words */
1445 int aligned
= size
- (size
% 4);
1447 if ((retval
= target_read_memory(target
, address
, 4, aligned
/ 4, buffer
)) != ERROR_OK
)
1455 /*prevent byte access when possible (avoid AHB access limitations in some cases)*/
1458 int aligned
= size
- (size
%2);
1459 retval
= target_read_memory(target
, address
, 2, aligned
/ 2, buffer
);
1460 if (retval
!= ERROR_OK
)
1467 /* handle tail writes of less than 4 bytes */
1470 if ((retval
= target_read_memory(target
, address
, 1, size
, buffer
)) != ERROR_OK
)
1477 int target_checksum_memory(struct target
*target
, uint32_t address
, uint32_t size
, uint32_t* crc
)
1482 uint32_t checksum
= 0;
1483 if (!target_was_examined(target
))
1485 LOG_ERROR("Target not examined yet");
1489 if ((retval
= target
->type
->checksum_memory(target
, address
,
1490 size
, &checksum
)) != ERROR_OK
)
1492 buffer
= malloc(size
);
1495 LOG_ERROR("error allocating buffer for section (%d bytes)", (int)size
);
1496 return ERROR_INVALID_ARGUMENTS
;
1498 retval
= target_read_buffer(target
, address
, size
, buffer
);
1499 if (retval
!= ERROR_OK
)
1505 /* convert to target endianess */
1506 for (i
= 0; i
< (size
/sizeof(uint32_t)); i
++)
1508 uint32_t target_data
;
1509 target_data
= target_buffer_get_u32(target
, &buffer
[i
*sizeof(uint32_t)]);
1510 target_buffer_set_u32(target
, &buffer
[i
*sizeof(uint32_t)], target_data
);
1513 retval
= image_calculate_checksum(buffer
, size
, &checksum
);
1522 int target_blank_check_memory(struct target
*target
, uint32_t address
, uint32_t size
, uint32_t* blank
)
1525 if (!target_was_examined(target
))
1527 LOG_ERROR("Target not examined yet");
1531 if (target
->type
->blank_check_memory
== 0)
1532 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
1534 retval
= target
->type
->blank_check_memory(target
, address
, size
, blank
);
1539 int target_read_u32(struct target
*target
, uint32_t address
, uint32_t *value
)
1541 uint8_t value_buf
[4];
1542 if (!target_was_examined(target
))
1544 LOG_ERROR("Target not examined yet");
1548 int retval
= target_read_memory(target
, address
, 4, 1, value_buf
);
1550 if (retval
== ERROR_OK
)
1552 *value
= target_buffer_get_u32(target
, value_buf
);
1553 LOG_DEBUG("address: 0x%8.8" PRIx32
", value: 0x%8.8" PRIx32
"",
1560 LOG_DEBUG("address: 0x%8.8" PRIx32
" failed",
1567 int target_read_u16(struct target
*target
, uint32_t address
, uint16_t *value
)
1569 uint8_t value_buf
[2];
1570 if (!target_was_examined(target
))
1572 LOG_ERROR("Target not examined yet");
1576 int retval
= target_read_memory(target
, address
, 2, 1, value_buf
);
1578 if (retval
== ERROR_OK
)
1580 *value
= target_buffer_get_u16(target
, value_buf
);
1581 LOG_DEBUG("address: 0x%8.8" PRIx32
", value: 0x%4.4x",
1588 LOG_DEBUG("address: 0x%8.8" PRIx32
" failed",
1595 int target_read_u8(struct target
*target
, uint32_t address
, uint8_t *value
)
1597 int retval
= target_read_memory(target
, address
, 1, 1, value
);
1598 if (!target_was_examined(target
))
1600 LOG_ERROR("Target not examined yet");
1604 if (retval
== ERROR_OK
)
1606 LOG_DEBUG("address: 0x%8.8" PRIx32
", value: 0x%2.2x",
1613 LOG_DEBUG("address: 0x%8.8" PRIx32
" failed",
1620 int target_write_u32(struct target
*target
, uint32_t address
, uint32_t value
)
1623 uint8_t value_buf
[4];
1624 if (!target_was_examined(target
))
1626 LOG_ERROR("Target not examined yet");
1630 LOG_DEBUG("address: 0x%8.8" PRIx32
", value: 0x%8.8" PRIx32
"",
1634 target_buffer_set_u32(target
, value_buf
, value
);
1635 if ((retval
= target_write_memory(target
, address
, 4, 1, value_buf
)) != ERROR_OK
)
1637 LOG_DEBUG("failed: %i", retval
);
1643 int target_write_u16(struct target
*target
, uint32_t address
, uint16_t value
)
1646 uint8_t value_buf
[2];
1647 if (!target_was_examined(target
))
1649 LOG_ERROR("Target not examined yet");
1653 LOG_DEBUG("address: 0x%8.8" PRIx32
", value: 0x%8.8x",
1657 target_buffer_set_u16(target
, value_buf
, value
);
1658 if ((retval
= target_write_memory(target
, address
, 2, 1, value_buf
)) != ERROR_OK
)
1660 LOG_DEBUG("failed: %i", retval
);
1666 int target_write_u8(struct target
*target
, uint32_t address
, uint8_t value
)
1669 if (!target_was_examined(target
))
1671 LOG_ERROR("Target not examined yet");
1675 LOG_DEBUG("address: 0x%8.8" PRIx32
", value: 0x%2.2x",
1678 if ((retval
= target_write_memory(target
, address
, 1, 1, &value
)) != ERROR_OK
)
1680 LOG_DEBUG("failed: %i", retval
);
1686 COMMAND_HANDLER(handle_targets_command
)
1688 struct target
*target
= all_targets
;
1692 target
= get_target(CMD_ARGV
[0]);
1693 if (target
== NULL
) {
1694 command_print(CMD_CTX
,"Target: %s is unknown, try one of:\n", CMD_ARGV
[0]);
1697 if (!target
->tap
->enabled
) {
1698 command_print(CMD_CTX
,"Target: TAP %s is disabled, "
1699 "can't be the current target\n",
1700 target
->tap
->dotted_name
);
1704 CMD_CTX
->current_target
= target
->target_number
;
1709 target
= all_targets
;
1710 command_print(CMD_CTX
, " TargetName Type Endian TapName State ");
1711 command_print(CMD_CTX
, "-- ------------------ ---------- ------ ------------------ ------------");
1717 if (target
->tap
->enabled
)
1718 state
= target_state_name( target
);
1720 state
= "tap-disabled";
1722 if (CMD_CTX
->current_target
== target
->target_number
)
1725 /* keep columns lined up to match the headers above */
1726 command_print(CMD_CTX
, "%2d%c %-18s %-10s %-6s %-18s %s",
1727 target
->target_number
,
1729 target_name(target
),
1730 target_type_name(target
),
1731 Jim_Nvp_value2name_simple(nvp_target_endian
,
1732 target
->endianness
)->name
,
1733 target
->tap
->dotted_name
,
1735 target
= target
->next
;
1741 /* every 300ms we check for reset & powerdropout and issue a "reset halt" if so. */
1743 static int powerDropout
;
1744 static int srstAsserted
;
1746 static int runPowerRestore
;
1747 static int runPowerDropout
;
1748 static int runSrstAsserted
;
1749 static int runSrstDeasserted
;
1751 static int sense_handler(void)
1753 static int prevSrstAsserted
= 0;
1754 static int prevPowerdropout
= 0;
1757 if ((retval
= jtag_power_dropout(&powerDropout
)) != ERROR_OK
)
1761 powerRestored
= prevPowerdropout
&& !powerDropout
;
1764 runPowerRestore
= 1;
1767 long long current
= timeval_ms();
1768 static long long lastPower
= 0;
1769 int waitMore
= lastPower
+ 2000 > current
;
1770 if (powerDropout
&& !waitMore
)
1772 runPowerDropout
= 1;
1773 lastPower
= current
;
1776 if ((retval
= jtag_srst_asserted(&srstAsserted
)) != ERROR_OK
)
1780 srstDeasserted
= prevSrstAsserted
&& !srstAsserted
;
1782 static long long lastSrst
= 0;
1783 waitMore
= lastSrst
+ 2000 > current
;
1784 if (srstDeasserted
&& !waitMore
)
1786 runSrstDeasserted
= 1;
1790 if (!prevSrstAsserted
&& srstAsserted
)
1792 runSrstAsserted
= 1;
1795 prevSrstAsserted
= srstAsserted
;
1796 prevPowerdropout
= powerDropout
;
1798 if (srstDeasserted
|| powerRestored
)
1800 /* Other than logging the event we can't do anything here.
1801 * Issuing a reset is a particularly bad idea as we might
1802 * be inside a reset already.
1809 static int backoff_times
= 0;
1810 static int backoff_count
= 0;
1812 /* process target state changes */
1813 static int handle_target(void *priv
)
1815 Jim_Interp
*interp
= (Jim_Interp
*)priv
;
1816 int retval
= ERROR_OK
;
1818 if (!is_jtag_poll_safe())
1820 /* polling is disabled currently */
1824 /* we do not want to recurse here... */
1825 static int recursive
= 0;
1830 /* danger! running these procedures can trigger srst assertions and power dropouts.
1831 * We need to avoid an infinite loop/recursion here and we do that by
1832 * clearing the flags after running these events.
1834 int did_something
= 0;
1835 if (runSrstAsserted
)
1837 LOG_INFO("srst asserted detected, running srst_asserted proc.");
1838 Jim_Eval(interp
, "srst_asserted");
1841 if (runSrstDeasserted
)
1843 Jim_Eval(interp
, "srst_deasserted");
1846 if (runPowerDropout
)
1848 LOG_INFO("Power dropout detected, running power_dropout proc.");
1849 Jim_Eval(interp
, "power_dropout");
1852 if (runPowerRestore
)
1854 Jim_Eval(interp
, "power_restore");
1860 /* clear detect flags */
1864 /* clear action flags */
1866 runSrstAsserted
= 0;
1867 runSrstDeasserted
= 0;
1868 runPowerRestore
= 0;
1869 runPowerDropout
= 0;
1874 if (backoff_times
> backoff_count
)
1876 /* do not poll this time as we failed previously */
1882 /* Poll targets for state changes unless that's globally disabled.
1883 * Skip targets that are currently disabled.
1885 for (struct target
*target
= all_targets
;
1886 is_jtag_poll_safe() && target
;
1887 target
= target
->next
)
1889 if (!target
->tap
->enabled
)
1892 /* only poll target if we've got power and srst isn't asserted */
1893 if (!powerDropout
&& !srstAsserted
)
1895 /* polling may fail silently until the target has been examined */
1896 if ((retval
= target_poll(target
)) != ERROR_OK
)
1898 /* 100ms polling interval. Increase interval between polling up to 5000ms */
1899 if (backoff_times
* polling_interval
< 5000)
1904 LOG_USER("Polling target failed, GDB will be halted. Polling again in %dms", backoff_times
* polling_interval
);
1906 /* Tell GDB to halt the debugger. This allows the user to
1907 * run monitor commands to handle the situation.
1909 target_call_event_callbacks(target
, TARGET_EVENT_GDB_HALT
);
1912 /* Since we succeeded, we reset backoff count */
1913 if (backoff_times
> 0)
1915 LOG_USER("Polling succeeded again");
1924 COMMAND_HANDLER(handle_reg_command
)
1926 struct target
*target
;
1927 struct reg
*reg
= NULL
;
1933 target
= get_current_target(CMD_CTX
);
1935 /* list all available registers for the current target */
1938 struct reg_cache
*cache
= target
->reg_cache
;
1945 command_print(CMD_CTX
, "===== %s", cache
->name
);
1947 for (i
= 0, reg
= cache
->reg_list
;
1948 i
< cache
->num_regs
;
1949 i
++, reg
++, count
++)
1951 /* only print cached values if they are valid */
1953 value
= buf_to_str(reg
->value
,
1955 command_print(CMD_CTX
,
1956 "(%i) %s (/%" PRIu32
"): 0x%s%s",
1964 command_print(CMD_CTX
, "(%i) %s (/%" PRIu32
")",
1969 cache
= cache
->next
;
1975 /* access a single register by its ordinal number */
1976 if ((CMD_ARGV
[0][0] >= '0') && (CMD_ARGV
[0][0] <= '9'))
1979 COMMAND_PARSE_NUMBER(uint
, CMD_ARGV
[0], num
);
1981 struct reg_cache
*cache
= target
->reg_cache
;
1986 for (i
= 0; i
< cache
->num_regs
; i
++)
1990 reg
= &cache
->reg_list
[i
];
1996 cache
= cache
->next
;
2001 command_print(CMD_CTX
, "%i is out of bounds, the current target has only %i registers (0 - %i)", num
, count
, count
- 1);
2004 } else /* access a single register by its name */
2006 reg
= register_get_by_name(target
->reg_cache
, CMD_ARGV
[0], 1);
2010 command_print(CMD_CTX
, "register %s not found in current target", CMD_ARGV
[0]);
2015 /* display a register */
2016 if ((CMD_ARGC
== 1) || ((CMD_ARGC
== 2) && !((CMD_ARGV
[1][0] >= '0') && (CMD_ARGV
[1][0] <= '9'))))
2018 if ((CMD_ARGC
== 2) && (strcmp(CMD_ARGV
[1], "force") == 0))
2021 if (reg
->valid
== 0)
2023 reg
->type
->get(reg
);
2025 value
= buf_to_str(reg
->value
, reg
->size
, 16);
2026 command_print(CMD_CTX
, "%s (/%i): 0x%s", reg
->name
, (int)(reg
->size
), value
);
2031 /* set register value */
2034 uint8_t *buf
= malloc(DIV_ROUND_UP(reg
->size
, 8));
2035 str_to_buf(CMD_ARGV
[1], strlen(CMD_ARGV
[1]), buf
, reg
->size
, 0);
2037 reg
->type
->set(reg
, buf
);
2039 value
= buf_to_str(reg
->value
, reg
->size
, 16);
2040 command_print(CMD_CTX
, "%s (/%i): 0x%s", reg
->name
, (int)(reg
->size
), value
);
2048 command_print(CMD_CTX
, "usage: reg <#|name> [value]");
2053 COMMAND_HANDLER(handle_poll_command
)
2055 int retval
= ERROR_OK
;
2056 struct target
*target
= get_current_target(CMD_CTX
);
2060 command_print(CMD_CTX
, "background polling: %s",
2061 jtag_poll_get_enabled() ? "on" : "off");
2062 command_print(CMD_CTX
, "TAP: %s (%s)",
2063 target
->tap
->dotted_name
,
2064 target
->tap
->enabled
? "enabled" : "disabled");
2065 if (!target
->tap
->enabled
)
2067 if ((retval
= target_poll(target
)) != ERROR_OK
)
2069 if ((retval
= target_arch_state(target
)) != ERROR_OK
)
2072 else if (CMD_ARGC
== 1)
2075 COMMAND_PARSE_ON_OFF(CMD_ARGV
[0], enable
);
2076 jtag_poll_set_enabled(enable
);
2080 return ERROR_COMMAND_SYNTAX_ERROR
;
2086 COMMAND_HANDLER(handle_wait_halt_command
)
2089 return ERROR_COMMAND_SYNTAX_ERROR
;
2094 int retval
= parse_uint(CMD_ARGV
[0], &ms
);
2095 if (ERROR_OK
!= retval
)
2097 command_print(CMD_CTX
, "usage: %s [seconds]", CMD_NAME
);
2098 return ERROR_COMMAND_SYNTAX_ERROR
;
2100 // convert seconds (given) to milliseconds (needed)
2104 struct target
*target
= get_current_target(CMD_CTX
);
2105 return target_wait_state(target
, TARGET_HALTED
, ms
);
2108 /* wait for target state to change. The trick here is to have a low
2109 * latency for short waits and not to suck up all the CPU time
2112 * After 500ms, keep_alive() is invoked
2114 int target_wait_state(struct target
*target
, enum target_state state
, int ms
)
2117 long long then
= 0, cur
;
2122 if ((retval
= target_poll(target
)) != ERROR_OK
)
2124 if (target
->state
== state
)
2132 then
= timeval_ms();
2133 LOG_DEBUG("waiting for target %s...",
2134 Jim_Nvp_value2name_simple(nvp_target_state
,state
)->name
);
2142 if ((cur
-then
) > ms
)
2144 LOG_ERROR("timed out while waiting for target %s",
2145 Jim_Nvp_value2name_simple(nvp_target_state
,state
)->name
);
2153 COMMAND_HANDLER(handle_halt_command
)
2157 struct target
*target
= get_current_target(CMD_CTX
);
2158 int retval
= target_halt(target
);
2159 if (ERROR_OK
!= retval
)
2164 unsigned wait_local
;
2165 retval
= parse_uint(CMD_ARGV
[0], &wait_local
);
2166 if (ERROR_OK
!= retval
)
2167 return ERROR_COMMAND_SYNTAX_ERROR
;
2172 return CALL_COMMAND_HANDLER(handle_wait_halt_command
);
2175 COMMAND_HANDLER(handle_soft_reset_halt_command
)
2177 struct target
*target
= get_current_target(CMD_CTX
);
2179 LOG_USER("requesting target halt and executing a soft reset");
2181 target
->type
->soft_reset_halt(target
);
2186 COMMAND_HANDLER(handle_reset_command
)
2189 return ERROR_COMMAND_SYNTAX_ERROR
;
2191 enum target_reset_mode reset_mode
= RESET_RUN
;
2195 n
= Jim_Nvp_name2value_simple(nvp_reset_modes
, CMD_ARGV
[0]);
2196 if ((n
->name
== NULL
) || (n
->value
== RESET_UNKNOWN
)) {
2197 return ERROR_COMMAND_SYNTAX_ERROR
;
2199 reset_mode
= n
->value
;
2202 /* reset *all* targets */
2203 return target_process_reset(CMD_CTX
, reset_mode
);
2207 COMMAND_HANDLER(handle_resume_command
)
2211 return ERROR_COMMAND_SYNTAX_ERROR
;
2213 struct target
*target
= get_current_target(CMD_CTX
);
2214 target_handle_event(target
, TARGET_EVENT_OLD_pre_resume
);
2216 /* with no CMD_ARGV, resume from current pc, addr = 0,
2217 * with one arguments, addr = CMD_ARGV[0],
2218 * handle breakpoints, not debugging */
2222 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], addr
);
2226 return target_resume(target
, current
, addr
, 1, 0);
2229 COMMAND_HANDLER(handle_step_command
)
2232 return ERROR_COMMAND_SYNTAX_ERROR
;
2236 /* with no CMD_ARGV, step from current pc, addr = 0,
2237 * with one argument addr = CMD_ARGV[0],
2238 * handle breakpoints, debugging */
2243 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], addr
);
2247 struct target
*target
= get_current_target(CMD_CTX
);
2249 return target
->type
->step(target
, current_pc
, addr
, 1);
2252 static void handle_md_output(struct command_context
*cmd_ctx
,
2253 struct target
*target
, uint32_t address
, unsigned size
,
2254 unsigned count
, const uint8_t *buffer
)
2256 const unsigned line_bytecnt
= 32;
2257 unsigned line_modulo
= line_bytecnt
/ size
;
2259 char output
[line_bytecnt
* 4 + 1];
2260 unsigned output_len
= 0;
2262 const char *value_fmt
;
2264 case 4: value_fmt
= "%8.8x "; break;
2265 case 2: value_fmt
= "%4.4x "; break;
2266 case 1: value_fmt
= "%2.2x "; break;
2268 /* "can't happen", caller checked */
2269 LOG_ERROR("invalid memory read size: %u", size
);
2273 for (unsigned i
= 0; i
< count
; i
++)
2275 if (i
% line_modulo
== 0)
2277 output_len
+= snprintf(output
+ output_len
,
2278 sizeof(output
) - output_len
,
2280 (unsigned)(address
+ (i
*size
)));
2284 const uint8_t *value_ptr
= buffer
+ i
* size
;
2286 case 4: value
= target_buffer_get_u32(target
, value_ptr
); break;
2287 case 2: value
= target_buffer_get_u16(target
, value_ptr
); break;
2288 case 1: value
= *value_ptr
;
2290 output_len
+= snprintf(output
+ output_len
,
2291 sizeof(output
) - output_len
,
2294 if ((i
% line_modulo
== line_modulo
- 1) || (i
== count
- 1))
2296 command_print(cmd_ctx
, "%s", output
);
2302 COMMAND_HANDLER(handle_md_command
)
2305 return ERROR_COMMAND_SYNTAX_ERROR
;
2308 switch (CMD_NAME
[2]) {
2309 case 'w': size
= 4; break;
2310 case 'h': size
= 2; break;
2311 case 'b': size
= 1; break;
2312 default: return ERROR_COMMAND_SYNTAX_ERROR
;
2315 bool physical
=strcmp(CMD_ARGV
[0], "phys")==0;
2316 int (*fn
)(struct target
*target
,
2317 uint32_t address
, uint32_t size_value
, uint32_t count
, uint8_t *buffer
);
2322 fn
=target_read_phys_memory
;
2325 fn
=target_read_memory
;
2327 if ((CMD_ARGC
< 1) || (CMD_ARGC
> 2))
2329 return ERROR_COMMAND_SYNTAX_ERROR
;
2333 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], address
);
2337 COMMAND_PARSE_NUMBER(uint
, CMD_ARGV
[1], count
);
2339 uint8_t *buffer
= calloc(count
, size
);
2341 struct target
*target
= get_current_target(CMD_CTX
);
2342 int retval
= fn(target
, address
, size
, count
, buffer
);
2343 if (ERROR_OK
== retval
)
2344 handle_md_output(CMD_CTX
, target
, address
, size
, count
, buffer
);
2351 typedef int (*target_write_fn
)(struct target
*target
,
2352 uint32_t address
, uint32_t size
, uint32_t count
, uint8_t *buffer
);
2354 static int target_write_memory_fast(struct target
*target
,
2355 uint32_t address
, uint32_t size
, uint32_t count
, uint8_t *buffer
)
2357 return target_write_buffer(target
, address
, size
* count
, buffer
);
2360 static int target_fill_mem(struct target
*target
,
2369 /* We have to write in reasonably large chunks to be able
2370 * to fill large memory areas with any sane speed */
2371 const unsigned chunk_size
= 16384;
2372 uint8_t *target_buf
= malloc(chunk_size
* data_size
);
2373 if (target_buf
== NULL
)
2375 LOG_ERROR("Out of memory");
2379 for (unsigned i
= 0; i
< chunk_size
; i
++)
2384 target_buffer_set_u32(target
, target_buf
+ i
*data_size
, b
);
2387 target_buffer_set_u16(target
, target_buf
+ i
*data_size
, b
);
2390 target_buffer_set_u8(target
, target_buf
+ i
*data_size
, b
);
2397 int retval
= ERROR_OK
;
2399 for (unsigned x
= 0; x
< c
; x
+= chunk_size
)
2403 if (current
> chunk_size
)
2405 current
= chunk_size
;
2407 retval
= fn(target
, address
+ x
* data_size
, data_size
, current
, target_buf
);
2408 if (retval
!= ERROR_OK
)
2412 /* avoid GDB timeouts */
2421 COMMAND_HANDLER(handle_mw_command
)
2425 return ERROR_COMMAND_SYNTAX_ERROR
;
2427 bool physical
=strcmp(CMD_ARGV
[0], "phys")==0;
2433 fn
=target_write_phys_memory
;
2436 fn
= target_write_memory_fast
;
2438 if ((CMD_ARGC
< 2) || (CMD_ARGC
> 3))
2439 return ERROR_COMMAND_SYNTAX_ERROR
;
2442 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], address
);
2445 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], value
);
2449 COMMAND_PARSE_NUMBER(uint
, CMD_ARGV
[2], count
);
2451 struct target
*target
= get_current_target(CMD_CTX
);
2453 switch (CMD_NAME
[2])
2465 return ERROR_COMMAND_SYNTAX_ERROR
;
2468 return target_fill_mem(target
, address
, fn
, wordsize
, value
, count
);
2471 static COMMAND_HELPER(parse_load_image_command_CMD_ARGV
, struct image
*image
,
2472 uint32_t *min_address
, uint32_t *max_address
)
2474 if (CMD_ARGC
< 1 || CMD_ARGC
> 5)
2475 return ERROR_COMMAND_SYNTAX_ERROR
;
2477 /* a base address isn't always necessary,
2478 * default to 0x0 (i.e. don't relocate) */
2482 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], addr
);
2483 image
->base_address
= addr
;
2484 image
->base_address_set
= 1;
2487 image
->base_address_set
= 0;
2489 image
->start_address_set
= 0;
2493 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[3], *min_address
);
2497 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[4], *max_address
);
2498 // use size (given) to find max (required)
2499 *max_address
+= *min_address
;
2502 if (*min_address
> *max_address
)
2503 return ERROR_COMMAND_SYNTAX_ERROR
;
2508 COMMAND_HANDLER(handle_load_image_command
)
2512 uint32_t image_size
;
2513 uint32_t min_address
= 0;
2514 uint32_t max_address
= 0xffffffff;
2518 int retval
= CALL_COMMAND_HANDLER(parse_load_image_command_CMD_ARGV
,
2519 &image
, &min_address
, &max_address
);
2520 if (ERROR_OK
!= retval
)
2523 struct target
*target
= get_current_target(CMD_CTX
);
2525 struct duration bench
;
2526 duration_start(&bench
);
2528 if (image_open(&image
, CMD_ARGV
[0], (CMD_ARGC
>= 3) ? CMD_ARGV
[2] : NULL
) != ERROR_OK
)
2535 for (i
= 0; i
< image
.num_sections
; i
++)
2537 buffer
= malloc(image
.sections
[i
].size
);
2540 command_print(CMD_CTX
,
2541 "error allocating buffer for section (%d bytes)",
2542 (int)(image
.sections
[i
].size
));
2546 if ((retval
= image_read_section(&image
, i
, 0x0, image
.sections
[i
].size
, buffer
, &buf_cnt
)) != ERROR_OK
)
2552 uint32_t offset
= 0;
2553 uint32_t length
= buf_cnt
;
2555 /* DANGER!!! beware of unsigned comparision here!!! */
2557 if ((image
.sections
[i
].base_address
+ buf_cnt
>= min_address
)&&
2558 (image
.sections
[i
].base_address
< max_address
))
2560 if (image
.sections
[i
].base_address
< min_address
)
2562 /* clip addresses below */
2563 offset
+= min_address
-image
.sections
[i
].base_address
;
2567 if (image
.sections
[i
].base_address
+ buf_cnt
> max_address
)
2569 length
-= (image
.sections
[i
].base_address
+ buf_cnt
)-max_address
;
2572 if ((retval
= target_write_buffer(target
, image
.sections
[i
].base_address
+ offset
, length
, buffer
+ offset
)) != ERROR_OK
)
2577 image_size
+= length
;
2578 command_print(CMD_CTX
, "%u bytes written at address 0x%8.8" PRIx32
"",
2579 (unsigned int)length
,
2580 image
.sections
[i
].base_address
+ offset
);
2586 if ((ERROR_OK
== retval
) && (duration_measure(&bench
) == ERROR_OK
))
2588 command_print(CMD_CTX
, "downloaded %" PRIu32
" bytes "
2589 "in %fs (%0.3f KiB/s)", image_size
,
2590 duration_elapsed(&bench
), duration_kbps(&bench
, image_size
));
2593 image_close(&image
);
2599 COMMAND_HANDLER(handle_dump_image_command
)
2601 struct fileio fileio
;
2602 uint8_t buffer
[560];
2603 int retval
, retvaltemp
;
2604 uint32_t address
, size
;
2605 struct duration bench
;
2606 struct target
*target
= get_current_target(CMD_CTX
);
2609 return ERROR_COMMAND_SYNTAX_ERROR
;
2611 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], address
);
2612 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[2], size
);
2614 retval
= fileio_open(&fileio
, CMD_ARGV
[0], FILEIO_WRITE
, FILEIO_BINARY
);
2615 if (retval
!= ERROR_OK
)
2618 duration_start(&bench
);
2623 size_t size_written
;
2624 uint32_t this_run_size
= (size
> 560) ? 560 : size
;
2625 retval
= target_read_buffer(target
, address
, this_run_size
, buffer
);
2626 if (retval
!= ERROR_OK
)
2631 retval
= fileio_write(&fileio
, this_run_size
, buffer
, &size_written
);
2632 if (retval
!= ERROR_OK
)
2637 size
-= this_run_size
;
2638 address
+= this_run_size
;
2641 if ((ERROR_OK
== retval
) && (duration_measure(&bench
) == ERROR_OK
))
2644 retval
= fileio_size(&fileio
, &filesize
);
2645 if (retval
!= ERROR_OK
)
2647 command_print(CMD_CTX
,
2648 "dumped %ld bytes in %fs (%0.3f KiB/s)", (long)filesize
,
2649 duration_elapsed(&bench
), duration_kbps(&bench
, filesize
));
2652 if ((retvaltemp
= fileio_close(&fileio
)) != ERROR_OK
)
2658 static COMMAND_HELPER(handle_verify_image_command_internal
, int verify
)
2662 uint32_t image_size
;
2665 uint32_t checksum
= 0;
2666 uint32_t mem_checksum
= 0;
2670 struct target
*target
= get_current_target(CMD_CTX
);
2674 return ERROR_COMMAND_SYNTAX_ERROR
;
2679 LOG_ERROR("no target selected");
2683 struct duration bench
;
2684 duration_start(&bench
);
2689 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], addr
);
2690 image
.base_address
= addr
;
2691 image
.base_address_set
= 1;
2695 image
.base_address_set
= 0;
2696 image
.base_address
= 0x0;
2699 image
.start_address_set
= 0;
2701 if ((retval
= image_open(&image
, CMD_ARGV
[0], (CMD_ARGC
== 3) ? CMD_ARGV
[2] : NULL
)) != ERROR_OK
)
2709 for (i
= 0; i
< image
.num_sections
; i
++)
2711 buffer
= malloc(image
.sections
[i
].size
);
2714 command_print(CMD_CTX
,
2715 "error allocating buffer for section (%d bytes)",
2716 (int)(image
.sections
[i
].size
));
2719 if ((retval
= image_read_section(&image
, i
, 0x0, image
.sections
[i
].size
, buffer
, &buf_cnt
)) != ERROR_OK
)
2727 /* calculate checksum of image */
2728 retval
= image_calculate_checksum(buffer
, buf_cnt
, &checksum
);
2729 if (retval
!= ERROR_OK
)
2735 retval
= target_checksum_memory(target
, image
.sections
[i
].base_address
, buf_cnt
, &mem_checksum
);
2736 if (retval
!= ERROR_OK
)
2742 if (checksum
!= mem_checksum
)
2744 /* failed crc checksum, fall back to a binary compare */
2749 LOG_ERROR("checksum mismatch - attempting binary compare");
2752 data
= (uint8_t*)malloc(buf_cnt
);
2754 /* Can we use 32bit word accesses? */
2756 int count
= buf_cnt
;
2757 if ((count
% 4) == 0)
2762 retval
= target_read_memory(target
, image
.sections
[i
].base_address
, size
, count
, data
);
2763 if (retval
== ERROR_OK
)
2766 for (t
= 0; t
< buf_cnt
; t
++)
2768 if (data
[t
] != buffer
[t
])
2770 command_print(CMD_CTX
,
2771 "diff %d address 0x%08x. Was 0x%02x instead of 0x%02x",
2773 (unsigned)(t
+ image
.sections
[i
].base_address
),
2778 command_print(CMD_CTX
, "More than 128 errors, the rest are not printed.");
2791 command_print(CMD_CTX
, "address 0x%08" PRIx32
" length 0x%08zx",
2792 image
.sections
[i
].base_address
,
2797 image_size
+= buf_cnt
;
2801 command_print(CMD_CTX
, "No more differences found.");
2806 retval
= ERROR_FAIL
;
2808 if ((ERROR_OK
== retval
) && (duration_measure(&bench
) == ERROR_OK
))
2810 command_print(CMD_CTX
, "verified %" PRIu32
" bytes "
2811 "in %fs (%0.3f KiB/s)", image_size
,
2812 duration_elapsed(&bench
), duration_kbps(&bench
, image_size
));
2815 image_close(&image
);
2820 COMMAND_HANDLER(handle_verify_image_command
)
2822 return CALL_COMMAND_HANDLER(handle_verify_image_command_internal
, 1);
2825 COMMAND_HANDLER(handle_test_image_command
)
2827 return CALL_COMMAND_HANDLER(handle_verify_image_command_internal
, 0);
2830 static int handle_bp_command_list(struct command_context
*cmd_ctx
)
2832 struct target
*target
= get_current_target(cmd_ctx
);
2833 struct breakpoint
*breakpoint
= target
->breakpoints
;
2836 if (breakpoint
->type
== BKPT_SOFT
)
2838 char* buf
= buf_to_str(breakpoint
->orig_instr
,
2839 breakpoint
->length
, 16);
2840 command_print(cmd_ctx
, "0x%8.8" PRIx32
", 0x%x, %i, 0x%s",
2841 breakpoint
->address
,
2843 breakpoint
->set
, buf
);
2848 command_print(cmd_ctx
, "0x%8.8" PRIx32
", 0x%x, %i",
2849 breakpoint
->address
,
2850 breakpoint
->length
, breakpoint
->set
);
2853 breakpoint
= breakpoint
->next
;
2858 static int handle_bp_command_set(struct command_context
*cmd_ctx
,
2859 uint32_t addr
, uint32_t length
, int hw
)
2861 struct target
*target
= get_current_target(cmd_ctx
);
2862 int retval
= breakpoint_add(target
, addr
, length
, hw
);
2863 if (ERROR_OK
== retval
)
2864 command_print(cmd_ctx
, "breakpoint set at 0x%8.8" PRIx32
"", addr
);
2866 LOG_ERROR("Failure setting breakpoint");
2870 COMMAND_HANDLER(handle_bp_command
)
2873 return handle_bp_command_list(CMD_CTX
);
2875 if (CMD_ARGC
< 2 || CMD_ARGC
> 3)
2877 command_print(CMD_CTX
, "usage: bp <address> <length> ['hw']");
2878 return ERROR_COMMAND_SYNTAX_ERROR
;
2882 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], addr
);
2884 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], length
);
2889 if (strcmp(CMD_ARGV
[2], "hw") == 0)
2892 return ERROR_COMMAND_SYNTAX_ERROR
;
2895 return handle_bp_command_set(CMD_CTX
, addr
, length
, hw
);
2898 COMMAND_HANDLER(handle_rbp_command
)
2901 return ERROR_COMMAND_SYNTAX_ERROR
;
2904 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], addr
);
2906 struct target
*target
= get_current_target(CMD_CTX
);
2907 breakpoint_remove(target
, addr
);
2912 COMMAND_HANDLER(handle_wp_command
)
2914 struct target
*target
= get_current_target(CMD_CTX
);
2918 struct watchpoint
*watchpoint
= target
->watchpoints
;
2922 command_print(CMD_CTX
, "address: 0x%8.8" PRIx32
2923 ", len: 0x%8.8" PRIx32
2924 ", r/w/a: %i, value: 0x%8.8" PRIx32
2925 ", mask: 0x%8.8" PRIx32
,
2926 watchpoint
->address
,
2928 (int)watchpoint
->rw
,
2931 watchpoint
= watchpoint
->next
;
2936 enum watchpoint_rw type
= WPT_ACCESS
;
2938 uint32_t length
= 0;
2939 uint32_t data_value
= 0x0;
2940 uint32_t data_mask
= 0xffffffff;
2945 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[4], data_mask
);
2948 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[3], data_value
);
2951 switch (CMD_ARGV
[2][0])
2963 LOG_ERROR("invalid watchpoint mode ('%c')", CMD_ARGV
[2][0]);
2964 return ERROR_COMMAND_SYNTAX_ERROR
;
2968 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], length
);
2969 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], addr
);
2973 command_print(CMD_CTX
, "usage: wp [address length "
2974 "[(r|w|a) [value [mask]]]]");
2975 return ERROR_COMMAND_SYNTAX_ERROR
;
2978 int retval
= watchpoint_add(target
, addr
, length
, type
,
2979 data_value
, data_mask
);
2980 if (ERROR_OK
!= retval
)
2981 LOG_ERROR("Failure setting watchpoints");
2986 COMMAND_HANDLER(handle_rwp_command
)
2989 return ERROR_COMMAND_SYNTAX_ERROR
;
2992 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], addr
);
2994 struct target
*target
= get_current_target(CMD_CTX
);
2995 watchpoint_remove(target
, addr
);
3002 * Translate a virtual address to a physical address.
3004 * The low-level target implementation must have logged a detailed error
3005 * which is forwarded to telnet/GDB session.
3007 COMMAND_HANDLER(handle_virt2phys_command
)
3010 return ERROR_COMMAND_SYNTAX_ERROR
;
3013 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], va
);
3016 struct target
*target
= get_current_target(CMD_CTX
);
3017 int retval
= target
->type
->virt2phys(target
, va
, &pa
);
3018 if (retval
== ERROR_OK
)
3019 command_print(CMD_CTX
, "Physical address 0x%08" PRIx32
"", pa
);
3024 static void writeData(FILE *f
, const void *data
, size_t len
)
3026 size_t written
= fwrite(data
, 1, len
, f
);
3028 LOG_ERROR("failed to write %zu bytes: %s", len
, strerror(errno
));
3031 static void writeLong(FILE *f
, int l
)
3034 for (i
= 0; i
< 4; i
++)
3036 char c
= (l
>> (i
*8))&0xff;
3037 writeData(f
, &c
, 1);
3042 static void writeString(FILE *f
, char *s
)
3044 writeData(f
, s
, strlen(s
));
3047 /* Dump a gmon.out histogram file. */
3048 static void writeGmon(uint32_t *samples
, uint32_t sampleNum
, const char *filename
)
3051 FILE *f
= fopen(filename
, "w");
3054 writeString(f
, "gmon");
3055 writeLong(f
, 0x00000001); /* Version */
3056 writeLong(f
, 0); /* padding */
3057 writeLong(f
, 0); /* padding */
3058 writeLong(f
, 0); /* padding */
3060 uint8_t zero
= 0; /* GMON_TAG_TIME_HIST */
3061 writeData(f
, &zero
, 1);
3063 /* figure out bucket size */
3064 uint32_t min
= samples
[0];
3065 uint32_t max
= samples
[0];
3066 for (i
= 0; i
< sampleNum
; i
++)
3068 if (min
> samples
[i
])
3072 if (max
< samples
[i
])
3078 int addressSpace
= (max
-min
+ 1);
3080 static const uint32_t maxBuckets
= 16 * 1024; /* maximum buckets. */
3081 uint32_t length
= addressSpace
;
3082 if (length
> maxBuckets
)
3084 length
= maxBuckets
;
3086 int *buckets
= malloc(sizeof(int)*length
);
3087 if (buckets
== NULL
)
3092 memset(buckets
, 0, sizeof(int)*length
);
3093 for (i
= 0; i
< sampleNum
;i
++)
3095 uint32_t address
= samples
[i
];
3096 long long a
= address
-min
;
3097 long long b
= length
-1;
3098 long long c
= addressSpace
-1;
3099 int index_t
= (a
*b
)/c
; /* danger!!!! int32 overflows */
3103 /* append binary memory gmon.out &profile_hist_hdr ((char*)&profile_hist_hdr + sizeof(struct gmon_hist_hdr)) */
3104 writeLong(f
, min
); /* low_pc */
3105 writeLong(f
, max
); /* high_pc */
3106 writeLong(f
, length
); /* # of samples */
3107 writeLong(f
, 100); /* KLUDGE! We lie, ca. 100Hz best case. */
3108 writeString(f
, "seconds");
3109 for (i
= 0; i
< (15-strlen("seconds")); i
++)
3110 writeData(f
, &zero
, 1);
3111 writeString(f
, "s");
3113 /*append binary memory gmon.out profile_hist_data (profile_hist_data + profile_hist_hdr.hist_size) */
3115 char *data
= malloc(2*length
);
3118 for (i
= 0; i
< length
;i
++)
3127 data
[i
*2 + 1]=(val
>> 8)&0xff;
3130 writeData(f
, data
, length
* 2);
3140 /* profiling samples the CPU PC as quickly as OpenOCD is able,
3141 * which will be used as a random sampling of PC */
3142 COMMAND_HANDLER(handle_profile_command
)
3144 struct target
*target
= get_current_target(CMD_CTX
);
3145 struct timeval timeout
, now
;
3147 gettimeofday(&timeout
, NULL
);
3150 return ERROR_COMMAND_SYNTAX_ERROR
;
3153 COMMAND_PARSE_NUMBER(uint
, CMD_ARGV
[0], offset
);
3155 timeval_add_time(&timeout
, offset
, 0);
3158 * @todo: Some cores let us sample the PC without the
3159 * annoying halt/resume step; for example, ARMv7 PCSR.
3160 * Provide a way to use that more efficient mechanism.
3163 command_print(CMD_CTX
, "Starting profiling. Halting and resuming the target as often as we can...");
3165 static const int maxSample
= 10000;
3166 uint32_t *samples
= malloc(sizeof(uint32_t)*maxSample
);
3167 if (samples
== NULL
)
3171 /* hopefully it is safe to cache! We want to stop/restart as quickly as possible. */
3172 struct reg
*reg
= register_get_by_name(target
->reg_cache
, "pc", 1);
3177 target_poll(target
);
3178 if (target
->state
== TARGET_HALTED
)
3180 uint32_t t
=*((uint32_t *)reg
->value
);
3181 samples
[numSamples
++]=t
;
3182 retval
= target_resume(target
, 1, 0, 0, 0); /* current pc, addr = 0, do not handle breakpoints, not debugging */
3183 target_poll(target
);
3184 alive_sleep(10); /* sleep 10ms, i.e. <100 samples/second. */
3185 } else if (target
->state
== TARGET_RUNNING
)
3187 /* We want to quickly sample the PC. */
3188 if ((retval
= target_halt(target
)) != ERROR_OK
)
3195 command_print(CMD_CTX
, "Target not halted or running");
3199 if (retval
!= ERROR_OK
)
3204 gettimeofday(&now
, NULL
);
3205 if ((numSamples
>= maxSample
) || ((now
.tv_sec
>= timeout
.tv_sec
) && (now
.tv_usec
>= timeout
.tv_usec
)))
3207 command_print(CMD_CTX
, "Profiling completed. %d samples.", numSamples
);
3208 if ((retval
= target_poll(target
)) != ERROR_OK
)
3213 if (target
->state
== TARGET_HALTED
)
3215 target_resume(target
, 1, 0, 0, 0); /* current pc, addr = 0, do not handle breakpoints, not debugging */
3217 if ((retval
= target_poll(target
)) != ERROR_OK
)
3222 writeGmon(samples
, numSamples
, CMD_ARGV
[1]);
3223 command_print(CMD_CTX
, "Wrote %s", CMD_ARGV
[1]);
3232 static int new_int_array_element(Jim_Interp
* interp
, const char *varname
, int idx
, uint32_t val
)
3235 Jim_Obj
*nameObjPtr
, *valObjPtr
;
3238 namebuf
= alloc_printf("%s(%d)", varname
, idx
);
3242 nameObjPtr
= Jim_NewStringObj(interp
, namebuf
, -1);
3243 valObjPtr
= Jim_NewIntObj(interp
, val
);
3244 if (!nameObjPtr
|| !valObjPtr
)
3250 Jim_IncrRefCount(nameObjPtr
);
3251 Jim_IncrRefCount(valObjPtr
);
3252 result
= Jim_SetVariable(interp
, nameObjPtr
, valObjPtr
);
3253 Jim_DecrRefCount(interp
, nameObjPtr
);
3254 Jim_DecrRefCount(interp
, valObjPtr
);
3256 /* printf("%s(%d) <= 0%08x\n", varname, idx, val); */
3260 static int jim_mem2array(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
3262 struct command_context
*context
;
3263 struct target
*target
;
3265 context
= current_command_context(interp
);
3266 assert (context
!= NULL
);
3268 target
= get_current_target(context
);
3271 LOG_ERROR("mem2array: no current target");
3275 return target_mem2array(interp
, target
, argc
-1, argv
+ 1);
3278 static int target_mem2array(Jim_Interp
*interp
, struct target
*target
, int argc
, Jim_Obj
*const *argv
)
3286 const char *varname
;
3290 /* argv[1] = name of array to receive the data
3291 * argv[2] = desired width
3292 * argv[3] = memory address
3293 * argv[4] = count of times to read
3296 Jim_WrongNumArgs(interp
, 1, argv
, "varname width addr nelems");
3299 varname
= Jim_GetString(argv
[0], &len
);
3300 /* given "foo" get space for worse case "foo(%d)" .. add 20 */
3302 e
= Jim_GetLong(interp
, argv
[1], &l
);
3308 e
= Jim_GetLong(interp
, argv
[2], &l
);
3313 e
= Jim_GetLong(interp
, argv
[3], &l
);
3329 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3330 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "Invalid width param, must be 8/16/32", NULL
);
3334 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3335 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "mem2array: zero width read?", NULL
);
3338 if ((addr
+ (len
* width
)) < addr
) {
3339 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3340 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "mem2array: addr + len - wraps to zero?", NULL
);
3343 /* absurd transfer size? */
3345 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3346 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "mem2array: absurd > 64K item request", NULL
);
3351 ((width
== 2) && ((addr
& 1) == 0)) ||
3352 ((width
== 4) && ((addr
& 3) == 0))) {
3356 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3357 sprintf(buf
, "mem2array address: 0x%08" PRIx32
" is not aligned for %" PRId32
" byte reads",
3360 Jim_AppendStrings(interp
, Jim_GetResult(interp
), buf
, NULL
);
3369 size_t buffersize
= 4096;
3370 uint8_t *buffer
= malloc(buffersize
);
3377 /* Slurp... in buffer size chunks */
3379 count
= len
; /* in objects.. */
3380 if (count
> (buffersize
/width
)) {
3381 count
= (buffersize
/width
);
3384 retval
= target_read_memory(target
, addr
, width
, count
, buffer
);
3385 if (retval
!= ERROR_OK
) {
3387 LOG_ERROR("mem2array: Read @ 0x%08x, w=%d, cnt=%d, failed",
3391 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3392 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "mem2array: cannot read memory", NULL
);
3396 v
= 0; /* shut up gcc */
3397 for (i
= 0 ;i
< count
;i
++, n
++) {
3400 v
= target_buffer_get_u32(target
, &buffer
[i
*width
]);
3403 v
= target_buffer_get_u16(target
, &buffer
[i
*width
]);
3406 v
= buffer
[i
] & 0x0ff;
3409 new_int_array_element(interp
, varname
, n
, v
);
3417 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3422 static int get_int_array_element(Jim_Interp
* interp
, const char *varname
, int idx
, uint32_t *val
)
3425 Jim_Obj
*nameObjPtr
, *valObjPtr
;
3429 namebuf
= alloc_printf("%s(%d)", varname
, idx
);
3433 nameObjPtr
= Jim_NewStringObj(interp
, namebuf
, -1);
3440 Jim_IncrRefCount(nameObjPtr
);
3441 valObjPtr
= Jim_GetVariable(interp
, nameObjPtr
, JIM_ERRMSG
);
3442 Jim_DecrRefCount(interp
, nameObjPtr
);
3444 if (valObjPtr
== NULL
)
3447 result
= Jim_GetLong(interp
, valObjPtr
, &l
);
3448 /* printf("%s(%d) => 0%08x\n", varname, idx, val); */
3453 static int jim_array2mem(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
3455 struct command_context
*context
;
3456 struct target
*target
;
3458 context
= current_command_context(interp
);
3459 assert (context
!= NULL
);
3461 target
= get_current_target(context
);
3462 if (target
== NULL
) {
3463 LOG_ERROR("array2mem: no current target");
3467 return target_array2mem(interp
,target
, argc
-1, argv
+ 1);
3470 static int target_array2mem(Jim_Interp
*interp
, struct target
*target
,
3471 int argc
, Jim_Obj
*const *argv
)
3479 const char *varname
;
3483 /* argv[1] = name of array to get the data
3484 * argv[2] = desired width
3485 * argv[3] = memory address
3486 * argv[4] = count to write
3489 Jim_WrongNumArgs(interp
, 0, argv
, "varname width addr nelems");
3492 varname
= Jim_GetString(argv
[0], &len
);
3493 /* given "foo" get space for worse case "foo(%d)" .. add 20 */
3495 e
= Jim_GetLong(interp
, argv
[1], &l
);
3501 e
= Jim_GetLong(interp
, argv
[2], &l
);
3506 e
= Jim_GetLong(interp
, argv
[3], &l
);
3522 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3523 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "Invalid width param, must be 8/16/32", NULL
);
3527 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3528 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "array2mem: zero width read?", NULL
);
3531 if ((addr
+ (len
* width
)) < addr
) {
3532 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3533 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "array2mem: addr + len - wraps to zero?", NULL
);
3536 /* absurd transfer size? */
3538 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3539 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "array2mem: absurd > 64K item request", NULL
);
3544 ((width
== 2) && ((addr
& 1) == 0)) ||
3545 ((width
== 4) && ((addr
& 3) == 0))) {
3549 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3550 sprintf(buf
, "array2mem address: 0x%08x is not aligned for %d byte reads",
3553 Jim_AppendStrings(interp
, Jim_GetResult(interp
), buf
, NULL
);
3564 size_t buffersize
= 4096;
3565 uint8_t *buffer
= malloc(buffersize
);
3570 /* Slurp... in buffer size chunks */
3572 count
= len
; /* in objects.. */
3573 if (count
> (buffersize
/width
)) {
3574 count
= (buffersize
/width
);
3577 v
= 0; /* shut up gcc */
3578 for (i
= 0 ;i
< count
;i
++, n
++) {
3579 get_int_array_element(interp
, varname
, n
, &v
);
3582 target_buffer_set_u32(target
, &buffer
[i
*width
], v
);
3585 target_buffer_set_u16(target
, &buffer
[i
*width
], v
);
3588 buffer
[i
] = v
& 0x0ff;
3594 retval
= target_write_memory(target
, addr
, width
, count
, buffer
);
3595 if (retval
!= ERROR_OK
) {
3597 LOG_ERROR("array2mem: Write @ 0x%08x, w=%d, cnt=%d, failed",
3601 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3602 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "array2mem: cannot read memory", NULL
);
3610 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3615 /* FIX? should we propagate errors here rather than printing them
3618 void target_handle_event(struct target
*target
, enum target_event e
)
3620 struct target_event_action
*teap
;
3622 for (teap
= target
->event_action
; teap
!= NULL
; teap
= teap
->next
) {
3623 if (teap
->event
== e
) {
3624 LOG_DEBUG("target: (%d) %s (%s) event: %d (%s) action: %s",
3625 target
->target_number
,
3626 target_name(target
),
3627 target_type_name(target
),
3629 Jim_Nvp_value2name_simple(nvp_target_event
, e
)->name
,
3630 Jim_GetString(teap
->body
, NULL
));
3631 if (Jim_EvalObj(teap
->interp
, teap
->body
) != JIM_OK
)
3633 Jim_MakeErrorMessage(teap
->interp
);
3634 command_print(NULL
,"%s\n", Jim_GetString(Jim_GetResult(teap
->interp
), NULL
));
3641 * Returns true only if the target has a handler for the specified event.
3643 bool target_has_event_action(struct target
*target
, enum target_event event
)
3645 struct target_event_action
*teap
;
3647 for (teap
= target
->event_action
; teap
!= NULL
; teap
= teap
->next
) {
3648 if (teap
->event
== event
)
3654 enum target_cfg_param
{
3657 TCFG_WORK_AREA_VIRT
,
3658 TCFG_WORK_AREA_PHYS
,
3659 TCFG_WORK_AREA_SIZE
,
3660 TCFG_WORK_AREA_BACKUP
,
3663 TCFG_CHAIN_POSITION
,
3666 static Jim_Nvp nvp_config_opts
[] = {
3667 { .name
= "-type", .value
= TCFG_TYPE
},
3668 { .name
= "-event", .value
= TCFG_EVENT
},
3669 { .name
= "-work-area-virt", .value
= TCFG_WORK_AREA_VIRT
},
3670 { .name
= "-work-area-phys", .value
= TCFG_WORK_AREA_PHYS
},
3671 { .name
= "-work-area-size", .value
= TCFG_WORK_AREA_SIZE
},
3672 { .name
= "-work-area-backup", .value
= TCFG_WORK_AREA_BACKUP
},
3673 { .name
= "-endian" , .value
= TCFG_ENDIAN
},
3674 { .name
= "-variant", .value
= TCFG_VARIANT
},
3675 { .name
= "-chain-position", .value
= TCFG_CHAIN_POSITION
},
3677 { .name
= NULL
, .value
= -1 }
3680 static int target_configure(Jim_GetOptInfo
*goi
, struct target
*target
)
3688 /* parse config or cget options ... */
3689 while (goi
->argc
> 0) {
3690 Jim_SetEmptyResult(goi
->interp
);
3691 /* Jim_GetOpt_Debug(goi); */
3693 if (target
->type
->target_jim_configure
) {
3694 /* target defines a configure function */
3695 /* target gets first dibs on parameters */
3696 e
= (*(target
->type
->target_jim_configure
))(target
, goi
);
3705 /* otherwise we 'continue' below */
3707 e
= Jim_GetOpt_Nvp(goi
, nvp_config_opts
, &n
);
3709 Jim_GetOpt_NvpUnknown(goi
, nvp_config_opts
, 0);
3715 if (goi
->isconfigure
) {
3716 Jim_SetResultFormatted(goi
->interp
,
3717 "not settable: %s", n
->name
);
3721 if (goi
->argc
!= 0) {
3722 Jim_WrongNumArgs(goi
->interp
,
3723 goi
->argc
, goi
->argv
,
3728 Jim_SetResultString(goi
->interp
,
3729 target_type_name(target
), -1);
3733 if (goi
->argc
== 0) {
3734 Jim_WrongNumArgs(goi
->interp
, goi
->argc
, goi
->argv
, "-event ?event-name? ...");
3738 e
= Jim_GetOpt_Nvp(goi
, nvp_target_event
, &n
);
3740 Jim_GetOpt_NvpUnknown(goi
, nvp_target_event
, 1);
3744 if (goi
->isconfigure
) {
3745 if (goi
->argc
!= 1) {
3746 Jim_WrongNumArgs(goi
->interp
, goi
->argc
, goi
->argv
, "-event ?event-name? ?EVENT-BODY?");
3750 if (goi
->argc
!= 0) {
3751 Jim_WrongNumArgs(goi
->interp
, goi
->argc
, goi
->argv
, "-event ?event-name?");
3757 struct target_event_action
*teap
;
3759 teap
= target
->event_action
;
3760 /* replace existing? */
3762 if (teap
->event
== (enum target_event
)n
->value
) {
3768 if (goi
->isconfigure
) {
3769 bool replace
= true;
3772 teap
= calloc(1, sizeof(*teap
));
3775 teap
->event
= n
->value
;
3776 teap
->interp
= goi
->interp
;
3777 Jim_GetOpt_Obj(goi
, &o
);
3779 Jim_DecrRefCount(teap
->interp
, teap
->body
);
3781 teap
->body
= Jim_DuplicateObj(goi
->interp
, o
);
3784 * Tcl/TK - "tk events" have a nice feature.
3785 * See the "BIND" command.
3786 * We should support that here.
3787 * You can specify %X and %Y in the event code.
3788 * The idea is: %T - target name.
3789 * The idea is: %N - target number
3790 * The idea is: %E - event name.
3792 Jim_IncrRefCount(teap
->body
);
3796 /* add to head of event list */
3797 teap
->next
= target
->event_action
;
3798 target
->event_action
= teap
;
3800 Jim_SetEmptyResult(goi
->interp
);
3804 Jim_SetEmptyResult(goi
->interp
);
3806 Jim_SetResult(goi
->interp
, Jim_DuplicateObj(goi
->interp
, teap
->body
));
3813 case TCFG_WORK_AREA_VIRT
:
3814 if (goi
->isconfigure
) {
3815 target_free_all_working_areas(target
);
3816 e
= Jim_GetOpt_Wide(goi
, &w
);
3820 target
->working_area_virt
= w
;
3821 target
->working_area_virt_spec
= true;
3823 if (goi
->argc
!= 0) {
3827 Jim_SetResult(goi
->interp
, Jim_NewIntObj(goi
->interp
, target
->working_area_virt
));
3831 case TCFG_WORK_AREA_PHYS
:
3832 if (goi
->isconfigure
) {
3833 target_free_all_working_areas(target
);
3834 e
= Jim_GetOpt_Wide(goi
, &w
);
3838 target
->working_area_phys
= w
;
3839 target
->working_area_phys_spec
= true;
3841 if (goi
->argc
!= 0) {
3845 Jim_SetResult(goi
->interp
, Jim_NewIntObj(goi
->interp
, target
->working_area_phys
));
3849 case TCFG_WORK_AREA_SIZE
:
3850 if (goi
->isconfigure
) {
3851 target_free_all_working_areas(target
);
3852 e
= Jim_GetOpt_Wide(goi
, &w
);
3856 target
->working_area_size
= w
;
3858 if (goi
->argc
!= 0) {
3862 Jim_SetResult(goi
->interp
, Jim_NewIntObj(goi
->interp
, target
->working_area_size
));
3866 case TCFG_WORK_AREA_BACKUP
:
3867 if (goi
->isconfigure
) {
3868 target_free_all_working_areas(target
);
3869 e
= Jim_GetOpt_Wide(goi
, &w
);
3873 /* make this exactly 1 or 0 */
3874 target
->backup_working_area
= (!!w
);
3876 if (goi
->argc
!= 0) {
3880 Jim_SetResult(goi
->interp
, Jim_NewIntObj(goi
->interp
, target
->backup_working_area
));
3881 /* loop for more e*/
3885 if (goi
->isconfigure
) {
3886 e
= Jim_GetOpt_Nvp(goi
, nvp_target_endian
, &n
);
3888 Jim_GetOpt_NvpUnknown(goi
, nvp_target_endian
, 1);
3891 target
->endianness
= n
->value
;
3893 if (goi
->argc
!= 0) {
3897 n
= Jim_Nvp_value2name_simple(nvp_target_endian
, target
->endianness
);
3898 if (n
->name
== NULL
) {
3899 target
->endianness
= TARGET_LITTLE_ENDIAN
;
3900 n
= Jim_Nvp_value2name_simple(nvp_target_endian
, target
->endianness
);
3902 Jim_SetResultString(goi
->interp
, n
->name
, -1);
3907 if (goi
->isconfigure
) {
3908 if (goi
->argc
< 1) {
3909 Jim_SetResultFormatted(goi
->interp
,
3914 if (target
->variant
) {
3915 free((void *)(target
->variant
));
3917 e
= Jim_GetOpt_String(goi
, &cp
, NULL
);
3918 target
->variant
= strdup(cp
);
3920 if (goi
->argc
!= 0) {
3924 Jim_SetResultString(goi
->interp
, target
->variant
,-1);
3927 case TCFG_CHAIN_POSITION
:
3928 if (goi
->isconfigure
) {
3930 struct jtag_tap
*tap
;
3931 target_free_all_working_areas(target
);
3932 e
= Jim_GetOpt_Obj(goi
, &o_t
);
3936 tap
= jtag_tap_by_jim_obj(goi
->interp
, o_t
);
3940 /* make this exactly 1 or 0 */
3943 if (goi
->argc
!= 0) {
3947 Jim_SetResultString(goi
->interp
, target
->tap
->dotted_name
, -1);
3948 /* loop for more e*/
3951 } /* while (goi->argc) */
3954 /* done - we return */
3959 jim_target_configure(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
3963 Jim_GetOpt_Setup(&goi
, interp
, argc
- 1, argv
+ 1);
3964 goi
.isconfigure
= !strcmp(Jim_GetString(argv
[0], NULL
), "configure");
3965 int need_args
= 1 + goi
.isconfigure
;
3966 if (goi
.argc
< need_args
)
3968 Jim_WrongNumArgs(goi
.interp
, goi
.argc
, goi
.argv
,
3970 ? "missing: -option VALUE ..."
3971 : "missing: -option ...");
3974 struct target
*target
= Jim_CmdPrivData(goi
.interp
);
3975 return target_configure(&goi
, target
);
3978 static int jim_target_mw(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
3980 const char *cmd_name
= Jim_GetString(argv
[0], NULL
);
3983 Jim_GetOpt_Setup(&goi
, interp
, argc
- 1, argv
+ 1);
3985 if (goi
.argc
< 2 || goi
.argc
> 4)
3987 Jim_SetResultFormatted(goi
.interp
,
3988 "usage: %s [phys] <address> <data> [<count>]", cmd_name
);
3993 fn
= target_write_memory_fast
;
3996 if (strcmp(Jim_GetString(argv
[1], NULL
), "phys") == 0)
3999 struct Jim_Obj
*obj
;
4000 e
= Jim_GetOpt_Obj(&goi
, &obj
);
4004 fn
= target_write_phys_memory
;
4008 e
= Jim_GetOpt_Wide(&goi
, &a
);
4013 e
= Jim_GetOpt_Wide(&goi
, &b
);
4020 e
= Jim_GetOpt_Wide(&goi
, &c
);
4025 /* all args must be consumed */
4031 struct target
*target
= Jim_CmdPrivData(goi
.interp
);
4033 if (strcasecmp(cmd_name
, "mww") == 0) {
4036 else if (strcasecmp(cmd_name
, "mwh") == 0) {
4039 else if (strcasecmp(cmd_name
, "mwb") == 0) {
4042 LOG_ERROR("command '%s' unknown: ", cmd_name
);
4046 return (target_fill_mem(target
, a
, fn
, data_size
, b
, c
) == ERROR_OK
) ? JIM_OK
: JIM_ERR
;
4049 static int jim_target_md(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4051 const char *cmd_name
= Jim_GetString(argv
[0], NULL
);
4054 Jim_GetOpt_Setup(&goi
, interp
, argc
- 1, argv
+ 1);
4056 if ((goi
.argc
< 1) || (goi
.argc
> 3))
4058 Jim_SetResultFormatted(goi
.interp
,
4059 "usage: %s [phys] <address> [<count>]", cmd_name
);
4063 int (*fn
)(struct target
*target
,
4064 uint32_t address
, uint32_t size
, uint32_t count
, uint8_t *buffer
);
4065 fn
=target_read_memory
;
4068 if (strcmp(Jim_GetString(argv
[1], NULL
), "phys") == 0)
4071 struct Jim_Obj
*obj
;
4072 e
= Jim_GetOpt_Obj(&goi
, &obj
);
4076 fn
=target_read_phys_memory
;
4080 e
= Jim_GetOpt_Wide(&goi
, &a
);
4085 if (goi
.argc
== 1) {
4086 e
= Jim_GetOpt_Wide(&goi
, &c
);
4094 /* all args must be consumed */
4100 jim_wide b
= 1; /* shut up gcc */
4101 if (strcasecmp(cmd_name
, "mdw") == 0)
4103 else if (strcasecmp(cmd_name
, "mdh") == 0)
4105 else if (strcasecmp(cmd_name
, "mdb") == 0)
4108 LOG_ERROR("command '%s' unknown: ", cmd_name
);
4112 /* convert count to "bytes" */
4115 struct target
*target
= Jim_CmdPrivData(goi
.interp
);
4116 uint8_t target_buf
[32];
4123 e
= fn(target
, a
, b
, y
/ b
, target_buf
);
4124 if (e
!= ERROR_OK
) {
4126 snprintf(tmp
, sizeof(tmp
), "%08lx", (long)a
);
4127 Jim_SetResultFormatted(interp
, "error reading target @ 0x%s", tmp
);
4131 command_print(NULL
, "0x%08x ", (int)(a
));
4134 for (x
= 0; x
< 16 && x
< y
; x
+= 4)
4136 z
= target_buffer_get_u32(target
, &(target_buf
[ x
]));
4137 command_print(NULL
, "%08x ", (int)(z
));
4139 for (; (x
< 16) ; x
+= 4) {
4140 command_print(NULL
, " ");
4144 for (x
= 0; x
< 16 && x
< y
; x
+= 2)
4146 z
= target_buffer_get_u16(target
, &(target_buf
[ x
]));
4147 command_print(NULL
, "%04x ", (int)(z
));
4149 for (; (x
< 16) ; x
+= 2) {
4150 command_print(NULL
, " ");
4155 for (x
= 0 ; (x
< 16) && (x
< y
) ; x
+= 1) {
4156 z
= target_buffer_get_u8(target
, &(target_buf
[ x
]));
4157 command_print(NULL
, "%02x ", (int)(z
));
4159 for (; (x
< 16) ; x
+= 1) {
4160 command_print(NULL
, " ");
4164 /* ascii-ify the bytes */
4165 for (x
= 0 ; x
< y
; x
++) {
4166 if ((target_buf
[x
] >= 0x20) &&
4167 (target_buf
[x
] <= 0x7e)) {
4171 target_buf
[x
] = '.';
4176 target_buf
[x
] = ' ';
4181 /* print - with a newline */
4182 command_print(NULL
, "%s\n", target_buf
);
4190 static int jim_target_mem2array(Jim_Interp
*interp
,
4191 int argc
, Jim_Obj
*const *argv
)
4193 struct target
*target
= Jim_CmdPrivData(interp
);
4194 return target_mem2array(interp
, target
, argc
- 1, argv
+ 1);
4197 static int jim_target_array2mem(Jim_Interp
*interp
,
4198 int argc
, Jim_Obj
*const *argv
)
4200 struct target
*target
= Jim_CmdPrivData(interp
);
4201 return target_array2mem(interp
, target
, argc
- 1, argv
+ 1);
4204 static int jim_target_tap_disabled(Jim_Interp
*interp
)
4206 Jim_SetResultFormatted(interp
, "[TAP is disabled]");
4210 static int jim_target_examine(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4214 Jim_WrongNumArgs(interp
, 1, argv
, "[no parameters]");
4217 struct target
*target
= Jim_CmdPrivData(interp
);
4218 if (!target
->tap
->enabled
)
4219 return jim_target_tap_disabled(interp
);
4221 int e
= target
->type
->examine(target
);
4229 static int jim_target_halt_gdb(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4233 Jim_WrongNumArgs(interp
, 1, argv
, "[no parameters]");
4236 struct target
*target
= Jim_CmdPrivData(interp
);
4238 if (target_call_event_callbacks(target
, TARGET_EVENT_GDB_HALT
) != ERROR_OK
)
4244 static int jim_target_poll(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4248 Jim_WrongNumArgs(interp
, 1, argv
, "[no parameters]");
4251 struct target
*target
= Jim_CmdPrivData(interp
);
4252 if (!target
->tap
->enabled
)
4253 return jim_target_tap_disabled(interp
);
4256 if (!(target_was_examined(target
))) {
4257 e
= ERROR_TARGET_NOT_EXAMINED
;
4259 e
= target
->type
->poll(target
);
4268 static int jim_target_reset(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4271 Jim_GetOpt_Setup(&goi
, interp
, argc
- 1, argv
+ 1);
4275 Jim_WrongNumArgs(interp
, 0, argv
,
4276 "([tT]|[fF]|assert|deassert) BOOL");
4281 int e
= Jim_GetOpt_Nvp(&goi
, nvp_assert
, &n
);
4284 Jim_GetOpt_NvpUnknown(&goi
, nvp_assert
, 1);
4287 /* the halt or not param */
4289 e
= Jim_GetOpt_Wide(&goi
, &a
);
4293 struct target
*target
= Jim_CmdPrivData(goi
.interp
);
4294 if (!target
->tap
->enabled
)
4295 return jim_target_tap_disabled(interp
);
4296 if (!(target_was_examined(target
)))
4298 LOG_ERROR("Target not examined yet");
4299 return ERROR_TARGET_NOT_EXAMINED
;
4301 if (!target
->type
->assert_reset
|| !target
->type
->deassert_reset
)
4303 Jim_SetResultFormatted(interp
,
4304 "No target-specific reset for %s",
4305 target_name(target
));
4308 /* determine if we should halt or not. */
4309 target
->reset_halt
= !!a
;
4310 /* When this happens - all workareas are invalid. */
4311 target_free_all_working_areas_restore(target
, 0);
4314 if (n
->value
== NVP_ASSERT
) {
4315 e
= target
->type
->assert_reset(target
);
4317 e
= target
->type
->deassert_reset(target
);
4319 return (e
== ERROR_OK
) ? JIM_OK
: JIM_ERR
;
4322 static int jim_target_halt(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4325 Jim_WrongNumArgs(interp
, 1, argv
, "[no parameters]");
4328 struct target
*target
= Jim_CmdPrivData(interp
);
4329 if (!target
->tap
->enabled
)
4330 return jim_target_tap_disabled(interp
);
4331 int e
= target
->type
->halt(target
);
4332 return (e
== ERROR_OK
) ? JIM_OK
: JIM_ERR
;
4335 static int jim_target_wait_state(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4338 Jim_GetOpt_Setup(&goi
, interp
, argc
- 1, argv
+ 1);
4340 /* params: <name> statename timeoutmsecs */
4343 const char *cmd_name
= Jim_GetString(argv
[0], NULL
);
4344 Jim_SetResultFormatted(goi
.interp
,
4345 "%s <state_name> <timeout_in_msec>", cmd_name
);
4350 int e
= Jim_GetOpt_Nvp(&goi
, nvp_target_state
, &n
);
4352 Jim_GetOpt_NvpUnknown(&goi
, nvp_target_state
,1);
4356 e
= Jim_GetOpt_Wide(&goi
, &a
);
4360 struct target
*target
= Jim_CmdPrivData(interp
);
4361 if (!target
->tap
->enabled
)
4362 return jim_target_tap_disabled(interp
);
4364 e
= target_wait_state(target
, n
->value
, a
);
4367 Jim_Obj
*eObj
= Jim_NewIntObj(interp
, e
);
4368 Jim_SetResultFormatted(goi
.interp
,
4369 "target: %s wait %s fails (%#s) %s",
4370 target_name(target
), n
->name
,
4371 eObj
, target_strerror_safe(e
));
4372 Jim_FreeNewObj(interp
, eObj
);
4377 /* List for human, Events defined for this target.
4378 * scripts/programs should use 'name cget -event NAME'
4380 static int jim_target_event_list(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4382 struct command_context
*cmd_ctx
= current_command_context(interp
);
4383 assert (cmd_ctx
!= NULL
);
4385 struct target
*target
= Jim_CmdPrivData(interp
);
4386 struct target_event_action
*teap
= target
->event_action
;
4387 command_print(cmd_ctx
, "Event actions for target (%d) %s\n",
4388 target
->target_number
,
4389 target_name(target
));
4390 command_print(cmd_ctx
, "%-25s | Body", "Event");
4391 command_print(cmd_ctx
, "------------------------- | "
4392 "----------------------------------------");
4395 Jim_Nvp
*opt
= Jim_Nvp_value2name_simple(nvp_target_event
, teap
->event
);
4396 command_print(cmd_ctx
, "%-25s | %s",
4397 opt
->name
, Jim_GetString(teap
->body
, NULL
));
4400 command_print(cmd_ctx
, "***END***");
4403 static int jim_target_current_state(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4407 Jim_WrongNumArgs(interp
, 1, argv
, "[no parameters]");
4410 struct target
*target
= Jim_CmdPrivData(interp
);
4411 Jim_SetResultString(interp
, target_state_name(target
), -1);
4414 static int jim_target_invoke_event(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4417 Jim_GetOpt_Setup(&goi
, interp
, argc
- 1, argv
+ 1);
4420 const char *cmd_name
= Jim_GetString(argv
[0], NULL
);
4421 Jim_SetResultFormatted(goi
.interp
, "%s <eventname>", cmd_name
);
4425 int e
= Jim_GetOpt_Nvp(&goi
, nvp_target_event
, &n
);
4428 Jim_GetOpt_NvpUnknown(&goi
, nvp_target_event
, 1);
4431 struct target
*target
= Jim_CmdPrivData(interp
);
4432 target_handle_event(target
, n
->value
);
4436 static const struct command_registration target_instance_command_handlers
[] = {
4438 .name
= "configure",
4439 .mode
= COMMAND_CONFIG
,
4440 .jim_handler
= jim_target_configure
,
4441 .help
= "configure a new target for use",
4442 .usage
= "[target_attribute ...]",
4446 .mode
= COMMAND_ANY
,
4447 .jim_handler
= jim_target_configure
,
4448 .help
= "returns the specified target attribute",
4449 .usage
= "target_attribute",
4453 .mode
= COMMAND_EXEC
,
4454 .jim_handler
= jim_target_mw
,
4455 .help
= "Write 32-bit word(s) to target memory",
4456 .usage
= "address data [count]",
4460 .mode
= COMMAND_EXEC
,
4461 .jim_handler
= jim_target_mw
,
4462 .help
= "Write 16-bit half-word(s) to target memory",
4463 .usage
= "address data [count]",
4467 .mode
= COMMAND_EXEC
,
4468 .jim_handler
= jim_target_mw
,
4469 .help
= "Write byte(s) to target memory",
4470 .usage
= "address data [count]",
4474 .mode
= COMMAND_EXEC
,
4475 .jim_handler
= jim_target_md
,
4476 .help
= "Display target memory as 32-bit words",
4477 .usage
= "address [count]",
4481 .mode
= COMMAND_EXEC
,
4482 .jim_handler
= jim_target_md
,
4483 .help
= "Display target memory as 16-bit half-words",
4484 .usage
= "address [count]",
4488 .mode
= COMMAND_EXEC
,
4489 .jim_handler
= jim_target_md
,
4490 .help
= "Display target memory as 8-bit bytes",
4491 .usage
= "address [count]",
4494 .name
= "array2mem",
4495 .mode
= COMMAND_EXEC
,
4496 .jim_handler
= jim_target_array2mem
,
4497 .help
= "Writes Tcl array of 8/16/32 bit numbers "
4499 .usage
= "arrayname bitwidth address count",
4502 .name
= "mem2array",
4503 .mode
= COMMAND_EXEC
,
4504 .jim_handler
= jim_target_mem2array
,
4505 .help
= "Loads Tcl array of 8/16/32 bit numbers "
4506 "from target memory",
4507 .usage
= "arrayname bitwidth address count",
4510 .name
= "eventlist",
4511 .mode
= COMMAND_EXEC
,
4512 .jim_handler
= jim_target_event_list
,
4513 .help
= "displays a table of events defined for this target",
4517 .mode
= COMMAND_EXEC
,
4518 .jim_handler
= jim_target_current_state
,
4519 .help
= "displays the current state of this target",
4522 .name
= "arp_examine",
4523 .mode
= COMMAND_EXEC
,
4524 .jim_handler
= jim_target_examine
,
4525 .help
= "used internally for reset processing",
4528 .name
= "arp_halt_gdb",
4529 .mode
= COMMAND_EXEC
,
4530 .jim_handler
= jim_target_halt_gdb
,
4531 .help
= "used internally for reset processing to halt GDB",
4535 .mode
= COMMAND_EXEC
,
4536 .jim_handler
= jim_target_poll
,
4537 .help
= "used internally for reset processing",
4540 .name
= "arp_reset",
4541 .mode
= COMMAND_EXEC
,
4542 .jim_handler
= jim_target_reset
,
4543 .help
= "used internally for reset processing",
4547 .mode
= COMMAND_EXEC
,
4548 .jim_handler
= jim_target_halt
,
4549 .help
= "used internally for reset processing",
4552 .name
= "arp_waitstate",
4553 .mode
= COMMAND_EXEC
,
4554 .jim_handler
= jim_target_wait_state
,
4555 .help
= "used internally for reset processing",
4558 .name
= "invoke-event",
4559 .mode
= COMMAND_EXEC
,
4560 .jim_handler
= jim_target_invoke_event
,
4561 .help
= "invoke handler for specified event",
4562 .usage
= "event_name",
4564 COMMAND_REGISTRATION_DONE
4567 static int target_create(Jim_GetOptInfo
*goi
)
4575 struct target
*target
;
4576 struct command_context
*cmd_ctx
;
4578 cmd_ctx
= current_command_context(goi
->interp
);
4579 assert (cmd_ctx
!= NULL
);
4581 if (goi
->argc
< 3) {
4582 Jim_WrongNumArgs(goi
->interp
, 1, goi
->argv
, "?name? ?type? ..options...");
4587 Jim_GetOpt_Obj(goi
, &new_cmd
);
4588 /* does this command exist? */
4589 cmd
= Jim_GetCommand(goi
->interp
, new_cmd
, JIM_ERRMSG
);
4591 cp
= Jim_GetString(new_cmd
, NULL
);
4592 Jim_SetResultFormatted(goi
->interp
, "Command/target: %s Exists", cp
);
4597 e
= Jim_GetOpt_String(goi
, &cp2
, NULL
);
4599 /* now does target type exist */
4600 for (x
= 0 ; target_types
[x
] ; x
++) {
4601 if (0 == strcmp(cp
, target_types
[x
]->name
)) {
4606 if (target_types
[x
] == NULL
) {
4607 Jim_SetResultFormatted(goi
->interp
, "Unknown target type %s, try one of ", cp
);
4608 for (x
= 0 ; target_types
[x
] ; x
++) {
4609 if (target_types
[x
+ 1]) {
4610 Jim_AppendStrings(goi
->interp
,
4611 Jim_GetResult(goi
->interp
),
4612 target_types
[x
]->name
,
4615 Jim_AppendStrings(goi
->interp
,
4616 Jim_GetResult(goi
->interp
),
4618 target_types
[x
]->name
,NULL
);
4625 target
= calloc(1,sizeof(struct target
));
4626 /* set target number */
4627 target
->target_number
= new_target_number();
4629 /* allocate memory for each unique target type */
4630 target
->type
= (struct target_type
*)calloc(1,sizeof(struct target_type
));
4632 memcpy(target
->type
, target_types
[x
], sizeof(struct target_type
));
4634 /* will be set by "-endian" */
4635 target
->endianness
= TARGET_ENDIAN_UNKNOWN
;
4637 target
->working_area
= 0x0;
4638 target
->working_area_size
= 0x0;
4639 target
->working_areas
= NULL
;
4640 target
->backup_working_area
= 0;
4642 target
->state
= TARGET_UNKNOWN
;
4643 target
->debug_reason
= DBG_REASON_UNDEFINED
;
4644 target
->reg_cache
= NULL
;
4645 target
->breakpoints
= NULL
;
4646 target
->watchpoints
= NULL
;
4647 target
->next
= NULL
;
4648 target
->arch_info
= NULL
;
4650 target
->display
= 1;
4652 target
->halt_issued
= false;
4654 /* initialize trace information */
4655 target
->trace_info
= malloc(sizeof(struct trace
));
4656 target
->trace_info
->num_trace_points
= 0;
4657 target
->trace_info
->trace_points_size
= 0;
4658 target
->trace_info
->trace_points
= NULL
;
4659 target
->trace_info
->trace_history_size
= 0;
4660 target
->trace_info
->trace_history
= NULL
;
4661 target
->trace_info
->trace_history_pos
= 0;
4662 target
->trace_info
->trace_history_overflowed
= 0;
4664 target
->dbgmsg
= NULL
;
4665 target
->dbg_msg_enabled
= 0;
4667 target
->endianness
= TARGET_ENDIAN_UNKNOWN
;
4669 /* Do the rest as "configure" options */
4670 goi
->isconfigure
= 1;
4671 e
= target_configure(goi
, target
);
4673 if (target
->tap
== NULL
)
4675 Jim_SetResultString(goi
->interp
, "-chain-position required when creating target", -1);
4685 if (target
->endianness
== TARGET_ENDIAN_UNKNOWN
) {
4686 /* default endian to little if not specified */
4687 target
->endianness
= TARGET_LITTLE_ENDIAN
;
4690 /* incase variant is not set */
4691 if (!target
->variant
)
4692 target
->variant
= strdup("");
4694 cp
= Jim_GetString(new_cmd
, NULL
);
4695 target
->cmd_name
= strdup(cp
);
4697 /* create the target specific commands */
4698 if (target
->type
->commands
) {
4699 e
= register_commands(cmd_ctx
, NULL
, target
->type
->commands
);
4701 LOG_ERROR("unable to register '%s' commands", cp
);
4703 if (target
->type
->target_create
) {
4704 (*(target
->type
->target_create
))(target
, goi
->interp
);
4707 /* append to end of list */
4709 struct target
**tpp
;
4710 tpp
= &(all_targets
);
4712 tpp
= &((*tpp
)->next
);
4717 /* now - create the new target name command */
4718 const const struct command_registration target_subcommands
[] = {
4720 .chain
= target_instance_command_handlers
,
4723 .chain
= target
->type
->commands
,
4725 COMMAND_REGISTRATION_DONE
4727 const const struct command_registration target_commands
[] = {
4730 .mode
= COMMAND_ANY
,
4731 .help
= "target command group",
4732 .chain
= target_subcommands
,
4734 COMMAND_REGISTRATION_DONE
4736 e
= register_commands(cmd_ctx
, NULL
, target_commands
);
4740 struct command
*c
= command_find_in_context(cmd_ctx
, cp
);
4742 command_set_handler_data(c
, target
);
4744 return (ERROR_OK
== e
) ? JIM_OK
: JIM_ERR
;
4747 static int jim_target_current(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4751 Jim_WrongNumArgs(interp
, 1, argv
, "Too many parameters");
4754 struct command_context
*cmd_ctx
= current_command_context(interp
);
4755 assert (cmd_ctx
!= NULL
);
4757 Jim_SetResultString(interp
, get_current_target(cmd_ctx
)->cmd_name
, -1);
4761 static int jim_target_types(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4765 Jim_WrongNumArgs(interp
, 1, argv
, "Too many parameters");
4768 Jim_SetResult(interp
, Jim_NewListObj(interp
, NULL
, 0));
4769 for (unsigned x
= 0; NULL
!= target_types
[x
]; x
++)
4771 Jim_ListAppendElement(interp
, Jim_GetResult(interp
),
4772 Jim_NewStringObj(interp
, target_types
[x
]->name
, -1));
4777 static int jim_target_names(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4781 Jim_WrongNumArgs(interp
, 1, argv
, "Too many parameters");
4784 Jim_SetResult(interp
, Jim_NewListObj(interp
, NULL
, 0));
4785 struct target
*target
= all_targets
;
4788 Jim_ListAppendElement(interp
, Jim_GetResult(interp
),
4789 Jim_NewStringObj(interp
, target_name(target
), -1));
4790 target
= target
->next
;
4795 static int jim_target_create(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4798 Jim_GetOpt_Setup(&goi
, interp
, argc
- 1, argv
+ 1);
4801 Jim_WrongNumArgs(goi
.interp
, goi
.argc
, goi
.argv
,
4802 "<name> <target_type> [<target_options> ...]");
4805 return target_create(&goi
);
4808 static int jim_target_number(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4811 Jim_GetOpt_Setup(&goi
, interp
, argc
- 1, argv
+ 1);
4813 /* It's OK to remove this mechanism sometime after August 2010 or so */
4814 LOG_WARNING("don't use numbers as target identifiers; use names");
4817 Jim_SetResultFormatted(goi
.interp
, "usage: target number <number>");
4821 int e
= Jim_GetOpt_Wide(&goi
, &w
);
4825 struct target
*target
;
4826 for (target
= all_targets
; NULL
!= target
; target
= target
->next
)
4828 if (target
->target_number
!= w
)
4831 Jim_SetResultString(goi
.interp
, target_name(target
), -1);
4835 Jim_Obj
*wObj
= Jim_NewIntObj(goi
.interp
, w
);
4836 Jim_SetResultFormatted(goi
.interp
,
4837 "Target: number %#s does not exist", wObj
);
4838 Jim_FreeNewObj(interp
, wObj
);
4843 static int jim_target_count(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4847 Jim_WrongNumArgs(interp
, 1, argv
, "<no parameters>");
4851 struct target
*target
= all_targets
;
4852 while (NULL
!= target
)
4854 target
= target
->next
;
4857 Jim_SetResult(interp
, Jim_NewIntObj(interp
, count
));
4861 static const struct command_registration target_subcommand_handlers
[] = {
4864 .mode
= COMMAND_CONFIG
,
4865 .handler
= handle_target_init_command
,
4866 .help
= "initialize targets",
4870 /* REVISIT this should be COMMAND_CONFIG ... */
4871 .mode
= COMMAND_ANY
,
4872 .jim_handler
= jim_target_create
,
4873 .usage
= "name type '-chain-position' name [options ...]",
4874 .help
= "Creates and selects a new target",
4878 .mode
= COMMAND_ANY
,
4879 .jim_handler
= jim_target_current
,
4880 .help
= "Returns the currently selected target",
4884 .mode
= COMMAND_ANY
,
4885 .jim_handler
= jim_target_types
,
4886 .help
= "Returns the available target types as "
4887 "a list of strings",
4891 .mode
= COMMAND_ANY
,
4892 .jim_handler
= jim_target_names
,
4893 .help
= "Returns the names of all targets as a list of strings",
4897 .mode
= COMMAND_ANY
,
4898 .jim_handler
= jim_target_number
,
4900 .help
= "Returns the name of the numbered target "
4905 .mode
= COMMAND_ANY
,
4906 .jim_handler
= jim_target_count
,
4907 .help
= "Returns the number of targets as an integer "
4910 COMMAND_REGISTRATION_DONE
4921 static int fastload_num
;
4922 static struct FastLoad
*fastload
;
4924 static void free_fastload(void)
4926 if (fastload
!= NULL
)
4929 for (i
= 0; i
< fastload_num
; i
++)
4931 if (fastload
[i
].data
)
4932 free(fastload
[i
].data
);
4942 COMMAND_HANDLER(handle_fast_load_image_command
)
4946 uint32_t image_size
;
4947 uint32_t min_address
= 0;
4948 uint32_t max_address
= 0xffffffff;
4953 int retval
= CALL_COMMAND_HANDLER(parse_load_image_command_CMD_ARGV
,
4954 &image
, &min_address
, &max_address
);
4955 if (ERROR_OK
!= retval
)
4958 struct duration bench
;
4959 duration_start(&bench
);
4961 retval
= image_open(&image
, CMD_ARGV
[0], (CMD_ARGC
>= 3) ? CMD_ARGV
[2] : NULL
);
4962 if (retval
!= ERROR_OK
)
4969 fastload_num
= image
.num_sections
;
4970 fastload
= (struct FastLoad
*)malloc(sizeof(struct FastLoad
)*image
.num_sections
);
4971 if (fastload
== NULL
)
4973 command_print(CMD_CTX
, "out of memory");
4974 image_close(&image
);
4977 memset(fastload
, 0, sizeof(struct FastLoad
)*image
.num_sections
);
4978 for (i
= 0; i
< image
.num_sections
; i
++)
4980 buffer
= malloc(image
.sections
[i
].size
);
4983 command_print(CMD_CTX
, "error allocating buffer for section (%d bytes)",
4984 (int)(image
.sections
[i
].size
));
4985 retval
= ERROR_FAIL
;
4989 if ((retval
= image_read_section(&image
, i
, 0x0, image
.sections
[i
].size
, buffer
, &buf_cnt
)) != ERROR_OK
)
4995 uint32_t offset
= 0;
4996 uint32_t length
= buf_cnt
;
4999 /* DANGER!!! beware of unsigned comparision here!!! */
5001 if ((image
.sections
[i
].base_address
+ buf_cnt
>= min_address
)&&
5002 (image
.sections
[i
].base_address
< max_address
))
5004 if (image
.sections
[i
].base_address
< min_address
)
5006 /* clip addresses below */
5007 offset
+= min_address
-image
.sections
[i
].base_address
;
5011 if (image
.sections
[i
].base_address
+ buf_cnt
> max_address
)
5013 length
-= (image
.sections
[i
].base_address
+ buf_cnt
)-max_address
;
5016 fastload
[i
].address
= image
.sections
[i
].base_address
+ offset
;
5017 fastload
[i
].data
= malloc(length
);
5018 if (fastload
[i
].data
== NULL
)
5021 command_print(CMD_CTX
, "error allocating buffer for section (%d bytes)",
5023 retval
= ERROR_FAIL
;
5026 memcpy(fastload
[i
].data
, buffer
+ offset
, length
);
5027 fastload
[i
].length
= length
;
5029 image_size
+= length
;
5030 command_print(CMD_CTX
, "%u bytes written at address 0x%8.8x",
5031 (unsigned int)length
,
5032 ((unsigned int)(image
.sections
[i
].base_address
+ offset
)));
5038 if ((ERROR_OK
== retval
) && (duration_measure(&bench
) == ERROR_OK
))
5040 command_print(CMD_CTX
, "Loaded %" PRIu32
" bytes "
5041 "in %fs (%0.3f KiB/s)", image_size
,
5042 duration_elapsed(&bench
), duration_kbps(&bench
, image_size
));
5044 command_print(CMD_CTX
,
5045 "WARNING: image has not been loaded to target!"
5046 "You can issue a 'fast_load' to finish loading.");
5049 image_close(&image
);
5051 if (retval
!= ERROR_OK
)
5059 COMMAND_HANDLER(handle_fast_load_command
)
5062 return ERROR_COMMAND_SYNTAX_ERROR
;
5063 if (fastload
== NULL
)
5065 LOG_ERROR("No image in memory");
5069 int ms
= timeval_ms();
5071 int retval
= ERROR_OK
;
5072 for (i
= 0; i
< fastload_num
;i
++)
5074 struct target
*target
= get_current_target(CMD_CTX
);
5075 command_print(CMD_CTX
, "Write to 0x%08x, length 0x%08x",
5076 (unsigned int)(fastload
[i
].address
),
5077 (unsigned int)(fastload
[i
].length
));
5078 retval
= target_write_buffer(target
, fastload
[i
].address
, fastload
[i
].length
, fastload
[i
].data
);
5079 if (retval
!= ERROR_OK
)
5083 size
+= fastload
[i
].length
;
5085 if (retval
== ERROR_OK
)
5087 int after
= timeval_ms();
5088 command_print(CMD_CTX
, "Loaded image %f kBytes/s", (float)(size
/1024.0)/((float)(after
-ms
)/1000.0));
5093 static const struct command_registration target_command_handlers
[] = {
5096 .handler
= handle_targets_command
,
5097 .mode
= COMMAND_ANY
,
5098 .help
= "change current default target (one parameter) "
5099 "or prints table of all targets (no parameters)",
5100 .usage
= "[target]",
5104 .mode
= COMMAND_CONFIG
,
5105 .help
= "configure target",
5107 .chain
= target_subcommand_handlers
,
5109 COMMAND_REGISTRATION_DONE
5112 int target_register_commands(struct command_context
*cmd_ctx
)
5114 return register_commands(cmd_ctx
, NULL
, target_command_handlers
);
5117 static bool target_reset_nag
= true;
5119 bool get_target_reset_nag(void)
5121 return target_reset_nag
;
5124 COMMAND_HANDLER(handle_target_reset_nag
)
5126 return CALL_COMMAND_HANDLER(handle_command_parse_bool
,
5127 &target_reset_nag
, "Nag after each reset about options to improve "
5131 static const struct command_registration target_exec_command_handlers
[] = {
5133 .name
= "fast_load_image",
5134 .handler
= handle_fast_load_image_command
,
5135 .mode
= COMMAND_ANY
,
5136 .help
= "Load image into server memory for later use by "
5137 "fast_load; primarily for profiling",
5138 .usage
= "filename address ['bin'|'ihex'|'elf'|'s19'] "
5139 "[min_address [max_length]]",
5142 .name
= "fast_load",
5143 .handler
= handle_fast_load_command
,
5144 .mode
= COMMAND_EXEC
,
5145 .help
= "loads active fast load image to current target "
5146 "- mainly for profiling purposes",
5150 .handler
= handle_profile_command
,
5151 .mode
= COMMAND_EXEC
,
5152 .help
= "profiling samples the CPU PC",
5154 /** @todo don't register virt2phys() unless target supports it */
5156 .name
= "virt2phys",
5157 .handler
= handle_virt2phys_command
,
5158 .mode
= COMMAND_ANY
,
5159 .help
= "translate a virtual address into a physical address",
5160 .usage
= "virtual_address",
5164 .handler
= handle_reg_command
,
5165 .mode
= COMMAND_EXEC
,
5166 .help
= "display or set a register; with no arguments, "
5167 "displays all registers and their values",
5168 .usage
= "[(register_name|register_number) [value]]",
5172 .handler
= handle_poll_command
,
5173 .mode
= COMMAND_EXEC
,
5174 .help
= "poll target state; or reconfigure background polling",
5175 .usage
= "['on'|'off']",
5178 .name
= "wait_halt",
5179 .handler
= handle_wait_halt_command
,
5180 .mode
= COMMAND_EXEC
,
5181 .help
= "wait up to the specified number of milliseconds "
5182 "(default 5) for a previously requested halt",
5183 .usage
= "[milliseconds]",
5187 .handler
= handle_halt_command
,
5188 .mode
= COMMAND_EXEC
,
5189 .help
= "request target to halt, then wait up to the specified"
5190 "number of milliseconds (default 5) for it to complete",
5191 .usage
= "[milliseconds]",
5195 .handler
= handle_resume_command
,
5196 .mode
= COMMAND_EXEC
,
5197 .help
= "resume target execution from current PC or address",
5198 .usage
= "[address]",
5202 .handler
= handle_reset_command
,
5203 .mode
= COMMAND_EXEC
,
5204 .usage
= "[run|halt|init]",
5205 .help
= "Reset all targets into the specified mode."
5206 "Default reset mode is run, if not given.",
5209 .name
= "soft_reset_halt",
5210 .handler
= handle_soft_reset_halt_command
,
5211 .mode
= COMMAND_EXEC
,
5212 .help
= "halt the target and do a soft reset",
5216 .handler
= handle_step_command
,
5217 .mode
= COMMAND_EXEC
,
5218 .help
= "step one instruction from current PC or address",
5219 .usage
= "[address]",
5223 .handler
= handle_md_command
,
5224 .mode
= COMMAND_EXEC
,
5225 .help
= "display memory words",
5226 .usage
= "['phys'] address [count]",
5230 .handler
= handle_md_command
,
5231 .mode
= COMMAND_EXEC
,
5232 .help
= "display memory half-words",
5233 .usage
= "['phys'] address [count]",
5237 .handler
= handle_md_command
,
5238 .mode
= COMMAND_EXEC
,
5239 .help
= "display memory bytes",
5240 .usage
= "['phys'] address [count]",
5244 .handler
= handle_mw_command
,
5245 .mode
= COMMAND_EXEC
,
5246 .help
= "write memory word",
5247 .usage
= "['phys'] address value [count]",
5251 .handler
= handle_mw_command
,
5252 .mode
= COMMAND_EXEC
,
5253 .help
= "write memory half-word",
5254 .usage
= "['phys'] address value [count]",
5258 .handler
= handle_mw_command
,
5259 .mode
= COMMAND_EXEC
,
5260 .help
= "write memory byte",
5261 .usage
= "['phys'] address value [count]",
5265 .handler
= handle_bp_command
,
5266 .mode
= COMMAND_EXEC
,
5267 .help
= "list or set hardware or software breakpoint",
5268 .usage
= "[address length ['hw']]",
5272 .handler
= handle_rbp_command
,
5273 .mode
= COMMAND_EXEC
,
5274 .help
= "remove breakpoint",
5279 .handler
= handle_wp_command
,
5280 .mode
= COMMAND_EXEC
,
5281 .help
= "list (no params) or create watchpoints",
5282 .usage
= "[address length [('r'|'w'|'a') value [mask]]]",
5286 .handler
= handle_rwp_command
,
5287 .mode
= COMMAND_EXEC
,
5288 .help
= "remove watchpoint",
5292 .name
= "load_image",
5293 .handler
= handle_load_image_command
,
5294 .mode
= COMMAND_EXEC
,
5295 .usage
= "filename address ['bin'|'ihex'|'elf'|'s19'] "
5296 "[min_address] [max_length]",
5299 .name
= "dump_image",
5300 .handler
= handle_dump_image_command
,
5301 .mode
= COMMAND_EXEC
,
5302 .usage
= "filename address size",
5305 .name
= "verify_image",
5306 .handler
= handle_verify_image_command
,
5307 .mode
= COMMAND_EXEC
,
5308 .usage
= "filename [offset [type]]",
5311 .name
= "test_image",
5312 .handler
= handle_test_image_command
,
5313 .mode
= COMMAND_EXEC
,
5314 .usage
= "filename [offset [type]]",
5317 .name
= "mem2array",
5318 .mode
= COMMAND_EXEC
,
5319 .jim_handler
= jim_mem2array
,
5320 .help
= "read 8/16/32 bit memory and return as a TCL array "
5321 "for script processing",
5322 .usage
= "arrayname bitwidth address count",
5325 .name
= "array2mem",
5326 .mode
= COMMAND_EXEC
,
5327 .jim_handler
= jim_array2mem
,
5328 .help
= "convert a TCL array to memory locations "
5329 "and write the 8/16/32 bit values",
5330 .usage
= "arrayname bitwidth address count",
5333 .name
= "reset_nag",
5334 .handler
= handle_target_reset_nag
,
5335 .mode
= COMMAND_ANY
,
5336 .help
= "Nag after each reset about options that could have been "
5337 "enabled to improve performance. ",
5338 .usage
= "['enable'|'disable']",
5340 COMMAND_REGISTRATION_DONE
5342 static int target_register_user_commands(struct command_context
*cmd_ctx
)
5344 int retval
= ERROR_OK
;
5345 if ((retval
= target_request_register_commands(cmd_ctx
)) != ERROR_OK
)
5348 if ((retval
= trace_register_commands(cmd_ctx
)) != ERROR_OK
)
5352 return register_commands(cmd_ctx
, NULL
, target_exec_command_handlers
);