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 cortexa9_target
;
70 extern struct target_type arm11_target
;
71 extern struct target_type mips_m4k_target
;
72 extern struct target_type avr_target
;
73 extern struct target_type dsp563xx_target
;
74 extern struct target_type testee_target
;
75 extern struct target_type avr32_ap7k_target
;
77 static struct target_type
*target_types
[] =
102 struct target
*all_targets
= NULL
;
103 static struct target_event_callback
*target_event_callbacks
= NULL
;
104 static struct target_timer_callback
*target_timer_callbacks
= NULL
;
105 static const int polling_interval
= 100;
107 static const Jim_Nvp nvp_assert
[] = {
108 { .name
= "assert", NVP_ASSERT
},
109 { .name
= "deassert", NVP_DEASSERT
},
110 { .name
= "T", NVP_ASSERT
},
111 { .name
= "F", NVP_DEASSERT
},
112 { .name
= "t", NVP_ASSERT
},
113 { .name
= "f", NVP_DEASSERT
},
114 { .name
= NULL
, .value
= -1 }
117 static const Jim_Nvp nvp_error_target
[] = {
118 { .value
= ERROR_TARGET_INVALID
, .name
= "err-invalid" },
119 { .value
= ERROR_TARGET_INIT_FAILED
, .name
= "err-init-failed" },
120 { .value
= ERROR_TARGET_TIMEOUT
, .name
= "err-timeout" },
121 { .value
= ERROR_TARGET_NOT_HALTED
, .name
= "err-not-halted" },
122 { .value
= ERROR_TARGET_FAILURE
, .name
= "err-failure" },
123 { .value
= ERROR_TARGET_UNALIGNED_ACCESS
, .name
= "err-unaligned-access" },
124 { .value
= ERROR_TARGET_DATA_ABORT
, .name
= "err-data-abort" },
125 { .value
= ERROR_TARGET_RESOURCE_NOT_AVAILABLE
, .name
= "err-resource-not-available" },
126 { .value
= ERROR_TARGET_TRANSLATION_FAULT
, .name
= "err-translation-fault" },
127 { .value
= ERROR_TARGET_NOT_RUNNING
, .name
= "err-not-running" },
128 { .value
= ERROR_TARGET_NOT_EXAMINED
, .name
= "err-not-examined" },
129 { .value
= -1, .name
= NULL
}
132 static const char *target_strerror_safe(int err
)
136 n
= Jim_Nvp_value2name_simple(nvp_error_target
, err
);
137 if (n
->name
== NULL
) {
144 static const Jim_Nvp nvp_target_event
[] = {
145 { .value
= TARGET_EVENT_OLD_gdb_program_config
, .name
= "old-gdb_program_config" },
146 { .value
= TARGET_EVENT_OLD_pre_resume
, .name
= "old-pre_resume" },
148 { .value
= TARGET_EVENT_GDB_HALT
, .name
= "gdb-halt" },
149 { .value
= TARGET_EVENT_HALTED
, .name
= "halted" },
150 { .value
= TARGET_EVENT_RESUMED
, .name
= "resumed" },
151 { .value
= TARGET_EVENT_RESUME_START
, .name
= "resume-start" },
152 { .value
= TARGET_EVENT_RESUME_END
, .name
= "resume-end" },
154 { .name
= "gdb-start", .value
= TARGET_EVENT_GDB_START
},
155 { .name
= "gdb-end", .value
= TARGET_EVENT_GDB_END
},
157 /* historical name */
159 { .value
= TARGET_EVENT_RESET_START
, .name
= "reset-start" },
161 { .value
= TARGET_EVENT_RESET_ASSERT_PRE
, .name
= "reset-assert-pre" },
162 { .value
= TARGET_EVENT_RESET_ASSERT
, .name
= "reset-assert" },
163 { .value
= TARGET_EVENT_RESET_ASSERT_POST
, .name
= "reset-assert-post" },
164 { .value
= TARGET_EVENT_RESET_DEASSERT_PRE
, .name
= "reset-deassert-pre" },
165 { .value
= TARGET_EVENT_RESET_DEASSERT_POST
, .name
= "reset-deassert-post" },
166 { .value
= TARGET_EVENT_RESET_HALT_PRE
, .name
= "reset-halt-pre" },
167 { .value
= TARGET_EVENT_RESET_HALT_POST
, .name
= "reset-halt-post" },
168 { .value
= TARGET_EVENT_RESET_WAIT_PRE
, .name
= "reset-wait-pre" },
169 { .value
= TARGET_EVENT_RESET_WAIT_POST
, .name
= "reset-wait-post" },
170 { .value
= TARGET_EVENT_RESET_INIT
, .name
= "reset-init" },
171 { .value
= TARGET_EVENT_RESET_END
, .name
= "reset-end" },
173 { .value
= TARGET_EVENT_EXAMINE_START
, .name
= "examine-start" },
174 { .value
= TARGET_EVENT_EXAMINE_END
, .name
= "examine-end" },
176 { .value
= TARGET_EVENT_DEBUG_HALTED
, .name
= "debug-halted" },
177 { .value
= TARGET_EVENT_DEBUG_RESUMED
, .name
= "debug-resumed" },
179 { .value
= TARGET_EVENT_GDB_ATTACH
, .name
= "gdb-attach" },
180 { .value
= TARGET_EVENT_GDB_DETACH
, .name
= "gdb-detach" },
182 { .value
= TARGET_EVENT_GDB_FLASH_WRITE_START
, .name
= "gdb-flash-write-start" },
183 { .value
= TARGET_EVENT_GDB_FLASH_WRITE_END
, .name
= "gdb-flash-write-end" },
185 { .value
= TARGET_EVENT_GDB_FLASH_ERASE_START
, .name
= "gdb-flash-erase-start" },
186 { .value
= TARGET_EVENT_GDB_FLASH_ERASE_END
, .name
= "gdb-flash-erase-end" },
188 { .value
= TARGET_EVENT_RESUME_START
, .name
= "resume-start" },
189 { .value
= TARGET_EVENT_RESUMED
, .name
= "resume-ok" },
190 { .value
= TARGET_EVENT_RESUME_END
, .name
= "resume-end" },
192 { .name
= NULL
, .value
= -1 }
195 static const Jim_Nvp nvp_target_state
[] = {
196 { .name
= "unknown", .value
= TARGET_UNKNOWN
},
197 { .name
= "running", .value
= TARGET_RUNNING
},
198 { .name
= "halted", .value
= TARGET_HALTED
},
199 { .name
= "reset", .value
= TARGET_RESET
},
200 { .name
= "debug-running", .value
= TARGET_DEBUG_RUNNING
},
201 { .name
= NULL
, .value
= -1 },
204 static const Jim_Nvp nvp_target_debug_reason
[] = {
205 { .name
= "debug-request" , .value
= DBG_REASON_DBGRQ
},
206 { .name
= "breakpoint" , .value
= DBG_REASON_BREAKPOINT
},
207 { .name
= "watchpoint" , .value
= DBG_REASON_WATCHPOINT
},
208 { .name
= "watchpoint-and-breakpoint", .value
= DBG_REASON_WPTANDBKPT
},
209 { .name
= "single-step" , .value
= DBG_REASON_SINGLESTEP
},
210 { .name
= "target-not-halted" , .value
= DBG_REASON_NOTHALTED
},
211 { .name
= "undefined" , .value
= DBG_REASON_UNDEFINED
},
212 { .name
= NULL
, .value
= -1 },
215 static const Jim_Nvp nvp_target_endian
[] = {
216 { .name
= "big", .value
= TARGET_BIG_ENDIAN
},
217 { .name
= "little", .value
= TARGET_LITTLE_ENDIAN
},
218 { .name
= "be", .value
= TARGET_BIG_ENDIAN
},
219 { .name
= "le", .value
= TARGET_LITTLE_ENDIAN
},
220 { .name
= NULL
, .value
= -1 },
223 static const Jim_Nvp nvp_reset_modes
[] = {
224 { .name
= "unknown", .value
= RESET_UNKNOWN
},
225 { .name
= "run" , .value
= RESET_RUN
},
226 { .name
= "halt" , .value
= RESET_HALT
},
227 { .name
= "init" , .value
= RESET_INIT
},
228 { .name
= NULL
, .value
= -1 },
231 const char *debug_reason_name(struct target
*t
)
235 cp
= Jim_Nvp_value2name_simple(nvp_target_debug_reason
,
236 t
->debug_reason
)->name
;
238 LOG_ERROR("Invalid debug reason: %d", (int)(t
->debug_reason
));
239 cp
= "(*BUG*unknown*BUG*)";
245 target_state_name( struct target
*t
)
248 cp
= Jim_Nvp_value2name_simple(nvp_target_state
, t
->state
)->name
;
250 LOG_ERROR("Invalid target state: %d", (int)(t
->state
));
251 cp
= "(*BUG*unknown*BUG*)";
256 /* determine the number of the new target */
257 static int new_target_number(void)
262 /* number is 0 based */
266 if (x
< t
->target_number
) {
267 x
= t
->target_number
;
274 /* read a uint32_t from a buffer in target memory endianness */
275 uint32_t target_buffer_get_u32(struct target
*target
, const uint8_t *buffer
)
277 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
278 return le_to_h_u32(buffer
);
280 return be_to_h_u32(buffer
);
283 /* read a uint16_t from a buffer in target memory endianness */
284 uint16_t target_buffer_get_u16(struct target
*target
, const uint8_t *buffer
)
286 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
287 return le_to_h_u16(buffer
);
289 return be_to_h_u16(buffer
);
292 /* read a uint8_t from a buffer in target memory endianness */
293 static uint8_t target_buffer_get_u8(struct target
*target
, const uint8_t *buffer
)
295 return *buffer
& 0x0ff;
298 /* write a uint32_t to a buffer in target memory endianness */
299 void target_buffer_set_u32(struct target
*target
, uint8_t *buffer
, uint32_t value
)
301 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
302 h_u32_to_le(buffer
, value
);
304 h_u32_to_be(buffer
, value
);
307 /* write a uint16_t to a buffer in target memory endianness */
308 void target_buffer_set_u16(struct target
*target
, uint8_t *buffer
, uint16_t value
)
310 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
311 h_u16_to_le(buffer
, value
);
313 h_u16_to_be(buffer
, value
);
316 /* write a uint8_t to a buffer in target memory endianness */
317 static void target_buffer_set_u8(struct target
*target
, uint8_t *buffer
, uint8_t value
)
322 /* return a pointer to a configured target; id is name or number */
323 struct target
*get_target(const char *id
)
325 struct target
*target
;
327 /* try as tcltarget name */
328 for (target
= all_targets
; target
; target
= target
->next
) {
329 if (target
->cmd_name
== NULL
)
331 if (strcmp(id
, target
->cmd_name
) == 0)
335 /* It's OK to remove this fallback sometime after August 2010 or so */
337 /* no match, try as number */
339 if (parse_uint(id
, &num
) != ERROR_OK
)
342 for (target
= all_targets
; target
; target
= target
->next
) {
343 if (target
->target_number
== (int)num
) {
344 LOG_WARNING("use '%s' as target identifier, not '%u'",
345 target
->cmd_name
, num
);
353 /* returns a pointer to the n-th configured target */
354 static struct target
*get_target_by_num(int num
)
356 struct target
*target
= all_targets
;
359 if (target
->target_number
== num
) {
362 target
= target
->next
;
368 struct target
* get_current_target(struct command_context
*cmd_ctx
)
370 struct target
*target
= get_target_by_num(cmd_ctx
->current_target
);
374 LOG_ERROR("BUG: current_target out of bounds");
381 int target_poll(struct target
*target
)
385 /* We can't poll until after examine */
386 if (!target_was_examined(target
))
388 /* Fail silently lest we pollute the log */
392 retval
= target
->type
->poll(target
);
393 if (retval
!= ERROR_OK
)
396 if (target
->halt_issued
)
398 if (target
->state
== TARGET_HALTED
)
400 target
->halt_issued
= false;
403 long long t
= timeval_ms() - target
->halt_issued_time
;
406 target
->halt_issued
= false;
407 LOG_INFO("Halt timed out, wake up GDB.");
408 target_call_event_callbacks(target
, TARGET_EVENT_GDB_HALT
);
416 int target_halt(struct target
*target
)
419 /* We can't poll until after examine */
420 if (!target_was_examined(target
))
422 LOG_ERROR("Target not examined yet");
426 retval
= target
->type
->halt(target
);
427 if (retval
!= ERROR_OK
)
430 target
->halt_issued
= true;
431 target
->halt_issued_time
= timeval_ms();
437 * Make the target (re)start executing using its saved execution
438 * context (possibly with some modifications).
440 * @param target Which target should start executing.
441 * @param current True to use the target's saved program counter instead
442 * of the address parameter
443 * @param address Optionally used as the program counter.
444 * @param handle_breakpoints True iff breakpoints at the resumption PC
445 * should be skipped. (For example, maybe execution was stopped by
446 * such a breakpoint, in which case it would be counterprodutive to
448 * @param debug_execution False if all working areas allocated by OpenOCD
449 * should be released and/or restored to their original contents.
450 * (This would for example be true to run some downloaded "helper"
451 * algorithm code, which resides in one such working buffer and uses
452 * another for data storage.)
454 * @todo Resolve the ambiguity about what the "debug_execution" flag
455 * signifies. For example, Target implementations don't agree on how
456 * it relates to invalidation of the register cache, or to whether
457 * breakpoints and watchpoints should be enabled. (It would seem wrong
458 * to enable breakpoints when running downloaded "helper" algorithms
459 * (debug_execution true), since the breakpoints would be set to match
460 * target firmware being debugged, not the helper algorithm.... and
461 * enabling them could cause such helpers to malfunction (for example,
462 * by overwriting data with a breakpoint instruction. On the other
463 * hand the infrastructure for running such helpers might use this
464 * procedure but rely on hardware breakpoint to detect termination.)
466 int target_resume(struct target
*target
, int current
, uint32_t address
, int handle_breakpoints
, int debug_execution
)
470 /* We can't poll until after examine */
471 if (!target_was_examined(target
))
473 LOG_ERROR("Target not examined yet");
477 /* note that resume *must* be asynchronous. The CPU can halt before
478 * we poll. The CPU can even halt at the current PC as a result of
479 * a software breakpoint being inserted by (a bug?) the application.
481 if ((retval
= target
->type
->resume(target
, current
, address
, handle_breakpoints
, debug_execution
)) != ERROR_OK
)
487 static int target_process_reset(struct command_context
*cmd_ctx
, enum target_reset_mode reset_mode
)
492 n
= Jim_Nvp_value2name_simple(nvp_reset_modes
, reset_mode
);
493 if (n
->name
== NULL
) {
494 LOG_ERROR("invalid reset mode");
498 /* disable polling during reset to make reset event scripts
499 * more predictable, i.e. dr/irscan & pathmove in events will
500 * not have JTAG operations injected into the middle of a sequence.
502 bool save_poll
= jtag_poll_get_enabled();
504 jtag_poll_set_enabled(false);
506 sprintf(buf
, "ocd_process_reset %s", n
->name
);
507 retval
= Jim_Eval(cmd_ctx
->interp
, buf
);
509 jtag_poll_set_enabled(save_poll
);
511 if (retval
!= JIM_OK
) {
512 Jim_MakeErrorMessage(cmd_ctx
->interp
);
513 command_print(NULL
,"%s\n", Jim_GetString(Jim_GetResult(cmd_ctx
->interp
), NULL
));
517 /* We want any events to be processed before the prompt */
518 retval
= target_call_timer_callbacks_now();
520 struct target
*target
;
521 for (target
= all_targets
; target
; target
= target
->next
) {
522 target
->type
->check_reset(target
);
528 static int identity_virt2phys(struct target
*target
,
529 uint32_t virtual, uint32_t *physical
)
535 static int no_mmu(struct target
*target
, int *enabled
)
541 static int default_examine(struct target
*target
)
543 target_set_examined(target
);
547 /* no check by default */
548 static int default_check_reset(struct target
*target
)
553 int target_examine_one(struct target
*target
)
555 return target
->type
->examine(target
);
558 static int jtag_enable_callback(enum jtag_event event
, void *priv
)
560 struct target
*target
= priv
;
562 if (event
!= JTAG_TAP_EVENT_ENABLE
|| !target
->tap
->enabled
)
565 jtag_unregister_event_callback(jtag_enable_callback
, target
);
566 return target_examine_one(target
);
570 /* Targets that correctly implement init + examine, i.e.
571 * no communication with target during init:
575 int target_examine(void)
577 int retval
= ERROR_OK
;
578 struct target
*target
;
580 for (target
= all_targets
; target
; target
= target
->next
)
582 /* defer examination, but don't skip it */
583 if (!target
->tap
->enabled
) {
584 jtag_register_event_callback(jtag_enable_callback
,
588 if ((retval
= target_examine_one(target
)) != ERROR_OK
)
593 const char *target_type_name(struct target
*target
)
595 return target
->type
->name
;
598 static int target_write_memory_imp(struct target
*target
, uint32_t address
, uint32_t size
, uint32_t count
, uint8_t *buffer
)
600 if (!target_was_examined(target
))
602 LOG_ERROR("Target not examined yet");
605 return target
->type
->write_memory_imp(target
, address
, size
, count
, buffer
);
608 static int target_read_memory_imp(struct target
*target
, uint32_t address
, uint32_t size
, uint32_t count
, uint8_t *buffer
)
610 if (!target_was_examined(target
))
612 LOG_ERROR("Target not examined yet");
615 return target
->type
->read_memory_imp(target
, address
, size
, count
, buffer
);
618 static int target_soft_reset_halt_imp(struct target
*target
)
620 if (!target_was_examined(target
))
622 LOG_ERROR("Target not examined yet");
625 if (!target
->type
->soft_reset_halt_imp
) {
626 LOG_ERROR("Target %s does not support soft_reset_halt",
627 target_name(target
));
630 return target
->type
->soft_reset_halt_imp(target
);
634 * Downloads a target-specific native code algorithm to the target,
635 * and executes it. * Note that some targets may need to set up, enable,
636 * and tear down a breakpoint (hard or * soft) to detect algorithm
637 * termination, while others may support lower overhead schemes where
638 * soft breakpoints embedded in the algorithm automatically terminate the
641 * @param target used to run the algorithm
642 * @param arch_info target-specific description of the algorithm.
644 int target_run_algorithm(struct target
*target
,
645 int num_mem_params
, struct mem_param
*mem_params
,
646 int num_reg_params
, struct reg_param
*reg_param
,
647 uint32_t entry_point
, uint32_t exit_point
,
648 int timeout_ms
, void *arch_info
)
650 int retval
= ERROR_FAIL
;
652 if (!target_was_examined(target
))
654 LOG_ERROR("Target not examined yet");
657 if (!target
->type
->run_algorithm
) {
658 LOG_ERROR("Target type '%s' does not support %s",
659 target_type_name(target
), __func__
);
663 target
->running_alg
= true;
664 retval
= target
->type
->run_algorithm(target
,
665 num_mem_params
, mem_params
,
666 num_reg_params
, reg_param
,
667 entry_point
, exit_point
, timeout_ms
, arch_info
);
668 target
->running_alg
= false;
675 int target_read_memory(struct target
*target
,
676 uint32_t address
, uint32_t size
, uint32_t count
, uint8_t *buffer
)
678 return target
->type
->read_memory(target
, address
, size
, count
, buffer
);
681 static int target_read_phys_memory(struct target
*target
,
682 uint32_t address
, uint32_t size
, uint32_t count
, uint8_t *buffer
)
684 return target
->type
->read_phys_memory(target
, address
, size
, count
, buffer
);
687 int target_write_memory(struct target
*target
,
688 uint32_t address
, uint32_t size
, uint32_t count
, uint8_t *buffer
)
690 return target
->type
->write_memory(target
, address
, size
, count
, buffer
);
693 static int target_write_phys_memory(struct target
*target
,
694 uint32_t address
, uint32_t size
, uint32_t count
, uint8_t *buffer
)
696 return target
->type
->write_phys_memory(target
, address
, size
, count
, buffer
);
699 int target_bulk_write_memory(struct target
*target
,
700 uint32_t address
, uint32_t count
, uint8_t *buffer
)
702 return target
->type
->bulk_write_memory(target
, address
, count
, buffer
);
705 int target_add_breakpoint(struct target
*target
,
706 struct breakpoint
*breakpoint
)
708 if (target
->state
!= TARGET_HALTED
) {
709 LOG_WARNING("target %s is not halted", target
->cmd_name
);
710 return ERROR_TARGET_NOT_HALTED
;
712 return target
->type
->add_breakpoint(target
, breakpoint
);
714 int target_remove_breakpoint(struct target
*target
,
715 struct breakpoint
*breakpoint
)
717 return target
->type
->remove_breakpoint(target
, breakpoint
);
720 int target_add_watchpoint(struct target
*target
,
721 struct watchpoint
*watchpoint
)
723 if (target
->state
!= TARGET_HALTED
) {
724 LOG_WARNING("target %s is not halted", target
->cmd_name
);
725 return ERROR_TARGET_NOT_HALTED
;
727 return target
->type
->add_watchpoint(target
, watchpoint
);
729 int target_remove_watchpoint(struct target
*target
,
730 struct watchpoint
*watchpoint
)
732 return target
->type
->remove_watchpoint(target
, watchpoint
);
735 int target_get_gdb_reg_list(struct target
*target
,
736 struct reg
**reg_list
[], int *reg_list_size
)
738 return target
->type
->get_gdb_reg_list(target
, reg_list
, reg_list_size
);
740 int target_step(struct target
*target
,
741 int current
, uint32_t address
, int handle_breakpoints
)
743 return target
->type
->step(target
, current
, address
, handle_breakpoints
);
748 * Reset the @c examined flag for the given target.
749 * Pure paranoia -- targets are zeroed on allocation.
751 static void target_reset_examined(struct target
*target
)
753 target
->examined
= false;
757 err_read_phys_memory(struct target
*target
, uint32_t address
,
758 uint32_t size
, uint32_t count
, uint8_t *buffer
)
760 LOG_ERROR("Not implemented: %s", __func__
);
765 err_write_phys_memory(struct target
*target
, uint32_t address
,
766 uint32_t size
, uint32_t count
, uint8_t *buffer
)
768 LOG_ERROR("Not implemented: %s", __func__
);
772 static int handle_target(void *priv
);
774 static int target_init_one(struct command_context
*cmd_ctx
,
775 struct target
*target
)
777 target_reset_examined(target
);
779 struct target_type
*type
= target
->type
;
780 if (type
->examine
== NULL
)
781 type
->examine
= default_examine
;
783 if (type
->check_reset
== NULL
)
784 type
->check_reset
= default_check_reset
;
786 int retval
= type
->init_target(cmd_ctx
, target
);
787 if (ERROR_OK
!= retval
)
789 LOG_ERROR("target '%s' init failed", target_name(target
));
794 * @todo get rid of those *memory_imp() methods, now that all
795 * callers are using target_*_memory() accessors ... and make
796 * sure the "physical" paths handle the same issues.
798 /* a non-invasive way(in terms of patches) to add some code that
799 * runs before the type->write/read_memory implementation
801 type
->write_memory_imp
= target
->type
->write_memory
;
802 type
->write_memory
= target_write_memory_imp
;
804 type
->read_memory_imp
= target
->type
->read_memory
;
805 type
->read_memory
= target_read_memory_imp
;
807 type
->soft_reset_halt_imp
= target
->type
->soft_reset_halt
;
808 type
->soft_reset_halt
= target_soft_reset_halt_imp
;
810 /* Sanity-check MMU support ... stub in what we must, to help
811 * implement it in stages, but warn if we need to do so.
815 if (type
->write_phys_memory
== NULL
)
817 LOG_ERROR("type '%s' is missing write_phys_memory",
819 type
->write_phys_memory
= err_write_phys_memory
;
821 if (type
->read_phys_memory
== NULL
)
823 LOG_ERROR("type '%s' is missing read_phys_memory",
825 type
->read_phys_memory
= err_read_phys_memory
;
827 if (type
->virt2phys
== NULL
)
829 LOG_ERROR("type '%s' is missing virt2phys", type
->name
);
830 type
->virt2phys
= identity_virt2phys
;
835 /* Make sure no-MMU targets all behave the same: make no
836 * distinction between physical and virtual addresses, and
837 * ensure that virt2phys() is always an identity mapping.
839 if (type
->write_phys_memory
|| type
->read_phys_memory
842 LOG_WARNING("type '%s' has bad MMU hooks", type
->name
);
846 type
->write_phys_memory
= type
->write_memory
;
847 type
->read_phys_memory
= type
->read_memory
;
848 type
->virt2phys
= identity_virt2phys
;
853 static int target_init(struct command_context
*cmd_ctx
)
855 struct target
*target
;
858 for (target
= all_targets
; target
; target
= target
->next
)
860 retval
= target_init_one(cmd_ctx
, target
);
861 if (ERROR_OK
!= retval
)
868 retval
= target_register_user_commands(cmd_ctx
);
869 if (ERROR_OK
!= retval
)
872 retval
= target_register_timer_callback(&handle_target
,
873 polling_interval
, 1, cmd_ctx
->interp
);
874 if (ERROR_OK
!= retval
)
880 COMMAND_HANDLER(handle_target_init_command
)
883 return ERROR_COMMAND_SYNTAX_ERROR
;
885 static bool target_initialized
= false;
886 if (target_initialized
)
888 LOG_INFO("'target init' has already been called");
891 target_initialized
= true;
893 LOG_DEBUG("Initializing targets...");
894 return target_init(CMD_CTX
);
897 int target_register_event_callback(int (*callback
)(struct target
*target
, enum target_event event
, void *priv
), void *priv
)
899 struct target_event_callback
**callbacks_p
= &target_event_callbacks
;
901 if (callback
== NULL
)
903 return ERROR_INVALID_ARGUMENTS
;
908 while ((*callbacks_p
)->next
)
909 callbacks_p
= &((*callbacks_p
)->next
);
910 callbacks_p
= &((*callbacks_p
)->next
);
913 (*callbacks_p
) = malloc(sizeof(struct target_event_callback
));
914 (*callbacks_p
)->callback
= callback
;
915 (*callbacks_p
)->priv
= priv
;
916 (*callbacks_p
)->next
= NULL
;
921 int target_register_timer_callback(int (*callback
)(void *priv
), int time_ms
, int periodic
, void *priv
)
923 struct target_timer_callback
**callbacks_p
= &target_timer_callbacks
;
926 if (callback
== NULL
)
928 return ERROR_INVALID_ARGUMENTS
;
933 while ((*callbacks_p
)->next
)
934 callbacks_p
= &((*callbacks_p
)->next
);
935 callbacks_p
= &((*callbacks_p
)->next
);
938 (*callbacks_p
) = malloc(sizeof(struct target_timer_callback
));
939 (*callbacks_p
)->callback
= callback
;
940 (*callbacks_p
)->periodic
= periodic
;
941 (*callbacks_p
)->time_ms
= time_ms
;
943 gettimeofday(&now
, NULL
);
944 (*callbacks_p
)->when
.tv_usec
= now
.tv_usec
+ (time_ms
% 1000) * 1000;
945 time_ms
-= (time_ms
% 1000);
946 (*callbacks_p
)->when
.tv_sec
= now
.tv_sec
+ (time_ms
/ 1000);
947 if ((*callbacks_p
)->when
.tv_usec
> 1000000)
949 (*callbacks_p
)->when
.tv_usec
= (*callbacks_p
)->when
.tv_usec
- 1000000;
950 (*callbacks_p
)->when
.tv_sec
+= 1;
953 (*callbacks_p
)->priv
= priv
;
954 (*callbacks_p
)->next
= NULL
;
959 int target_unregister_event_callback(int (*callback
)(struct target
*target
, enum target_event event
, void *priv
), void *priv
)
961 struct target_event_callback
**p
= &target_event_callbacks
;
962 struct target_event_callback
*c
= target_event_callbacks
;
964 if (callback
== NULL
)
966 return ERROR_INVALID_ARGUMENTS
;
971 struct target_event_callback
*next
= c
->next
;
972 if ((c
->callback
== callback
) && (c
->priv
== priv
))
986 static int target_unregister_timer_callback(int (*callback
)(void *priv
), void *priv
)
988 struct target_timer_callback
**p
= &target_timer_callbacks
;
989 struct target_timer_callback
*c
= target_timer_callbacks
;
991 if (callback
== NULL
)
993 return ERROR_INVALID_ARGUMENTS
;
998 struct target_timer_callback
*next
= c
->next
;
999 if ((c
->callback
== callback
) && (c
->priv
== priv
))
1013 int target_call_event_callbacks(struct target
*target
, enum target_event event
)
1015 struct target_event_callback
*callback
= target_event_callbacks
;
1016 struct target_event_callback
*next_callback
;
1018 if (event
== TARGET_EVENT_HALTED
)
1020 /* execute early halted first */
1021 target_call_event_callbacks(target
, TARGET_EVENT_GDB_HALT
);
1024 LOG_DEBUG("target event %i (%s)",
1026 Jim_Nvp_value2name_simple(nvp_target_event
, event
)->name
);
1028 target_handle_event(target
, event
);
1032 next_callback
= callback
->next
;
1033 callback
->callback(target
, event
, callback
->priv
);
1034 callback
= next_callback
;
1040 static int target_timer_callback_periodic_restart(
1041 struct target_timer_callback
*cb
, struct timeval
*now
)
1043 int time_ms
= cb
->time_ms
;
1044 cb
->when
.tv_usec
= now
->tv_usec
+ (time_ms
% 1000) * 1000;
1045 time_ms
-= (time_ms
% 1000);
1046 cb
->when
.tv_sec
= now
->tv_sec
+ time_ms
/ 1000;
1047 if (cb
->when
.tv_usec
> 1000000)
1049 cb
->when
.tv_usec
= cb
->when
.tv_usec
- 1000000;
1050 cb
->when
.tv_sec
+= 1;
1055 static int target_call_timer_callback(struct target_timer_callback
*cb
,
1056 struct timeval
*now
)
1058 cb
->callback(cb
->priv
);
1061 return target_timer_callback_periodic_restart(cb
, now
);
1063 return target_unregister_timer_callback(cb
->callback
, cb
->priv
);
1066 static int target_call_timer_callbacks_check_time(int checktime
)
1071 gettimeofday(&now
, NULL
);
1073 struct target_timer_callback
*callback
= target_timer_callbacks
;
1076 // cleaning up may unregister and free this callback
1077 struct target_timer_callback
*next_callback
= callback
->next
;
1079 bool call_it
= callback
->callback
&&
1080 ((!checktime
&& callback
->periodic
) ||
1081 now
.tv_sec
> callback
->when
.tv_sec
||
1082 (now
.tv_sec
== callback
->when
.tv_sec
&&
1083 now
.tv_usec
>= callback
->when
.tv_usec
));
1087 int retval
= target_call_timer_callback(callback
, &now
);
1088 if (retval
!= ERROR_OK
)
1092 callback
= next_callback
;
1098 int target_call_timer_callbacks(void)
1100 return target_call_timer_callbacks_check_time(1);
1103 /* invoke periodic callbacks immediately */
1104 int target_call_timer_callbacks_now(void)
1106 return target_call_timer_callbacks_check_time(0);
1109 int target_alloc_working_area_try(struct target
*target
, uint32_t size
, struct working_area
**area
)
1111 struct working_area
*c
= target
->working_areas
;
1112 struct working_area
*new_wa
= NULL
;
1114 /* Reevaluate working area address based on MMU state*/
1115 if (target
->working_areas
== NULL
)
1120 retval
= target
->type
->mmu(target
, &enabled
);
1121 if (retval
!= ERROR_OK
)
1127 if (target
->working_area_phys_spec
) {
1128 LOG_DEBUG("MMU disabled, using physical "
1129 "address for working memory 0x%08x",
1130 (unsigned)target
->working_area_phys
);
1131 target
->working_area
= target
->working_area_phys
;
1133 LOG_ERROR("No working memory available. "
1134 "Specify -work-area-phys to target.");
1135 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
1138 if (target
->working_area_virt_spec
) {
1139 LOG_DEBUG("MMU enabled, using virtual "
1140 "address for working memory 0x%08x",
1141 (unsigned)target
->working_area_virt
);
1142 target
->working_area
= target
->working_area_virt
;
1144 LOG_ERROR("No working memory available. "
1145 "Specify -work-area-virt to target.");
1146 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
1151 /* only allocate multiples of 4 byte */
1154 LOG_ERROR("BUG: code tried to allocate unaligned number of bytes (0x%08x), padding", ((unsigned)(size
)));
1155 size
= (size
+ 3) & (~3);
1158 /* see if there's already a matching working area */
1161 if ((c
->free
) && (c
->size
== size
))
1169 /* if not, allocate a new one */
1172 struct working_area
**p
= &target
->working_areas
;
1173 uint32_t first_free
= target
->working_area
;
1174 uint32_t free_size
= target
->working_area_size
;
1176 c
= target
->working_areas
;
1179 first_free
+= c
->size
;
1180 free_size
-= c
->size
;
1185 if (free_size
< size
)
1187 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
1190 LOG_DEBUG("allocated new working area at address 0x%08x", (unsigned)first_free
);
1192 new_wa
= malloc(sizeof(struct working_area
));
1193 new_wa
->next
= NULL
;
1194 new_wa
->size
= size
;
1195 new_wa
->address
= first_free
;
1197 if (target
->backup_working_area
)
1200 new_wa
->backup
= malloc(new_wa
->size
);
1201 if ((retval
= target_read_memory(target
, new_wa
->address
, 4, new_wa
->size
/ 4, new_wa
->backup
)) != ERROR_OK
)
1203 free(new_wa
->backup
);
1210 new_wa
->backup
= NULL
;
1213 /* put new entry in list */
1217 /* mark as used, and return the new (reused) area */
1218 new_wa
->free
= false;
1222 new_wa
->user
= area
;
1227 int target_alloc_working_area(struct target
*target
, uint32_t size
, struct working_area
**area
)
1231 retval
= target_alloc_working_area_try(target
, size
, area
);
1232 if (retval
== ERROR_TARGET_RESOURCE_NOT_AVAILABLE
)
1234 LOG_WARNING("not enough working area available(requested %u)", (unsigned)(size
));
1240 static int target_free_working_area_restore(struct target
*target
, struct working_area
*area
, int restore
)
1245 if (restore
&& target
->backup_working_area
)
1248 if ((retval
= target_write_memory(target
, area
->address
, 4, area
->size
/ 4, area
->backup
)) != ERROR_OK
)
1254 /* mark user pointer invalid */
1261 int target_free_working_area(struct target
*target
, struct working_area
*area
)
1263 return target_free_working_area_restore(target
, area
, 1);
1266 /* free resources and restore memory, if restoring memory fails,
1267 * free up resources anyway
1269 static void target_free_all_working_areas_restore(struct target
*target
, int restore
)
1271 struct working_area
*c
= target
->working_areas
;
1275 struct working_area
*next
= c
->next
;
1276 target_free_working_area_restore(target
, c
, restore
);
1286 target
->working_areas
= NULL
;
1289 void target_free_all_working_areas(struct target
*target
)
1291 target_free_all_working_areas_restore(target
, 1);
1294 int target_arch_state(struct target
*target
)
1299 LOG_USER("No target has been configured");
1303 LOG_USER("target state: %s", target_state_name( target
));
1305 if (target
->state
!= TARGET_HALTED
)
1308 retval
= target
->type
->arch_state(target
);
1312 /* Single aligned words are guaranteed to use 16 or 32 bit access
1313 * mode respectively, otherwise data is handled as quickly as
1316 int target_write_buffer(struct target
*target
, uint32_t address
, uint32_t size
, uint8_t *buffer
)
1319 LOG_DEBUG("writing buffer of %i byte at 0x%8.8x",
1320 (int)size
, (unsigned)address
);
1322 if (!target_was_examined(target
))
1324 LOG_ERROR("Target not examined yet");
1332 if ((address
+ size
- 1) < address
)
1334 /* GDB can request this when e.g. PC is 0xfffffffc*/
1335 LOG_ERROR("address + size wrapped(0x%08x, 0x%08x)",
1341 if (((address
% 2) == 0) && (size
== 2))
1343 return target_write_memory(target
, address
, 2, 1, buffer
);
1346 /* handle unaligned head bytes */
1349 uint32_t unaligned
= 4 - (address
% 4);
1351 if (unaligned
> size
)
1354 if ((retval
= target_write_memory(target
, address
, 1, unaligned
, buffer
)) != ERROR_OK
)
1357 buffer
+= unaligned
;
1358 address
+= unaligned
;
1362 /* handle aligned words */
1365 int aligned
= size
- (size
% 4);
1367 /* use bulk writes above a certain limit. This may have to be changed */
1370 if ((retval
= target
->type
->bulk_write_memory(target
, address
, aligned
/ 4, buffer
)) != ERROR_OK
)
1375 if ((retval
= target_write_memory(target
, address
, 4, aligned
/ 4, buffer
)) != ERROR_OK
)
1384 /* handle tail writes of less than 4 bytes */
1387 if ((retval
= target_write_memory(target
, address
, 1, size
, buffer
)) != ERROR_OK
)
1394 /* Single aligned words are guaranteed to use 16 or 32 bit access
1395 * mode respectively, otherwise data is handled as quickly as
1398 int target_read_buffer(struct target
*target
, uint32_t address
, uint32_t size
, uint8_t *buffer
)
1401 LOG_DEBUG("reading buffer of %i byte at 0x%8.8x",
1402 (int)size
, (unsigned)address
);
1404 if (!target_was_examined(target
))
1406 LOG_ERROR("Target not examined yet");
1414 if ((address
+ size
- 1) < address
)
1416 /* GDB can request this when e.g. PC is 0xfffffffc*/
1417 LOG_ERROR("address + size wrapped(0x%08" PRIx32
", 0x%08" PRIx32
")",
1423 if (((address
% 2) == 0) && (size
== 2))
1425 return target_read_memory(target
, address
, 2, 1, buffer
);
1428 /* handle unaligned head bytes */
1431 uint32_t unaligned
= 4 - (address
% 4);
1433 if (unaligned
> size
)
1436 if ((retval
= target_read_memory(target
, address
, 1, unaligned
, buffer
)) != ERROR_OK
)
1439 buffer
+= unaligned
;
1440 address
+= unaligned
;
1444 /* handle aligned words */
1447 int aligned
= size
- (size
% 4);
1449 if ((retval
= target_read_memory(target
, address
, 4, aligned
/ 4, buffer
)) != ERROR_OK
)
1457 /*prevent byte access when possible (avoid AHB access limitations in some cases)*/
1460 int aligned
= size
- (size
%2);
1461 retval
= target_read_memory(target
, address
, 2, aligned
/ 2, buffer
);
1462 if (retval
!= ERROR_OK
)
1469 /* handle tail writes of less than 4 bytes */
1472 if ((retval
= target_read_memory(target
, address
, 1, size
, buffer
)) != ERROR_OK
)
1479 int target_checksum_memory(struct target
*target
, uint32_t address
, uint32_t size
, uint32_t* crc
)
1484 uint32_t checksum
= 0;
1485 if (!target_was_examined(target
))
1487 LOG_ERROR("Target not examined yet");
1491 if ((retval
= target
->type
->checksum_memory(target
, address
,
1492 size
, &checksum
)) != ERROR_OK
)
1494 buffer
= malloc(size
);
1497 LOG_ERROR("error allocating buffer for section (%d bytes)", (int)size
);
1498 return ERROR_INVALID_ARGUMENTS
;
1500 retval
= target_read_buffer(target
, address
, size
, buffer
);
1501 if (retval
!= ERROR_OK
)
1507 /* convert to target endianess */
1508 for (i
= 0; i
< (size
/sizeof(uint32_t)); i
++)
1510 uint32_t target_data
;
1511 target_data
= target_buffer_get_u32(target
, &buffer
[i
*sizeof(uint32_t)]);
1512 target_buffer_set_u32(target
, &buffer
[i
*sizeof(uint32_t)], target_data
);
1515 retval
= image_calculate_checksum(buffer
, size
, &checksum
);
1524 int target_blank_check_memory(struct target
*target
, uint32_t address
, uint32_t size
, uint32_t* blank
)
1527 if (!target_was_examined(target
))
1529 LOG_ERROR("Target not examined yet");
1533 if (target
->type
->blank_check_memory
== 0)
1534 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
1536 retval
= target
->type
->blank_check_memory(target
, address
, size
, blank
);
1541 int target_read_u32(struct target
*target
, uint32_t address
, uint32_t *value
)
1543 uint8_t value_buf
[4];
1544 if (!target_was_examined(target
))
1546 LOG_ERROR("Target not examined yet");
1550 int retval
= target_read_memory(target
, address
, 4, 1, value_buf
);
1552 if (retval
== ERROR_OK
)
1554 *value
= target_buffer_get_u32(target
, value_buf
);
1555 LOG_DEBUG("address: 0x%8.8" PRIx32
", value: 0x%8.8" PRIx32
"",
1562 LOG_DEBUG("address: 0x%8.8" PRIx32
" failed",
1569 int target_read_u16(struct target
*target
, uint32_t address
, uint16_t *value
)
1571 uint8_t value_buf
[2];
1572 if (!target_was_examined(target
))
1574 LOG_ERROR("Target not examined yet");
1578 int retval
= target_read_memory(target
, address
, 2, 1, value_buf
);
1580 if (retval
== ERROR_OK
)
1582 *value
= target_buffer_get_u16(target
, value_buf
);
1583 LOG_DEBUG("address: 0x%8.8" PRIx32
", value: 0x%4.4x",
1590 LOG_DEBUG("address: 0x%8.8" PRIx32
" failed",
1597 int target_read_u8(struct target
*target
, uint32_t address
, uint8_t *value
)
1599 int retval
= target_read_memory(target
, address
, 1, 1, value
);
1600 if (!target_was_examined(target
))
1602 LOG_ERROR("Target not examined yet");
1606 if (retval
== ERROR_OK
)
1608 LOG_DEBUG("address: 0x%8.8" PRIx32
", value: 0x%2.2x",
1615 LOG_DEBUG("address: 0x%8.8" PRIx32
" failed",
1622 int target_write_u32(struct target
*target
, uint32_t address
, uint32_t value
)
1625 uint8_t value_buf
[4];
1626 if (!target_was_examined(target
))
1628 LOG_ERROR("Target not examined yet");
1632 LOG_DEBUG("address: 0x%8.8" PRIx32
", value: 0x%8.8" PRIx32
"",
1636 target_buffer_set_u32(target
, value_buf
, value
);
1637 if ((retval
= target_write_memory(target
, address
, 4, 1, value_buf
)) != ERROR_OK
)
1639 LOG_DEBUG("failed: %i", retval
);
1645 int target_write_u16(struct target
*target
, uint32_t address
, uint16_t value
)
1648 uint8_t value_buf
[2];
1649 if (!target_was_examined(target
))
1651 LOG_ERROR("Target not examined yet");
1655 LOG_DEBUG("address: 0x%8.8" PRIx32
", value: 0x%8.8x",
1659 target_buffer_set_u16(target
, value_buf
, value
);
1660 if ((retval
= target_write_memory(target
, address
, 2, 1, value_buf
)) != ERROR_OK
)
1662 LOG_DEBUG("failed: %i", retval
);
1668 int target_write_u8(struct target
*target
, uint32_t address
, uint8_t value
)
1671 if (!target_was_examined(target
))
1673 LOG_ERROR("Target not examined yet");
1677 LOG_DEBUG("address: 0x%8.8" PRIx32
", value: 0x%2.2x",
1680 if ((retval
= target_write_memory(target
, address
, 1, 1, &value
)) != ERROR_OK
)
1682 LOG_DEBUG("failed: %i", retval
);
1688 COMMAND_HANDLER(handle_targets_command
)
1690 struct target
*target
= all_targets
;
1694 target
= get_target(CMD_ARGV
[0]);
1695 if (target
== NULL
) {
1696 command_print(CMD_CTX
,"Target: %s is unknown, try one of:\n", CMD_ARGV
[0]);
1699 if (!target
->tap
->enabled
) {
1700 command_print(CMD_CTX
,"Target: TAP %s is disabled, "
1701 "can't be the current target\n",
1702 target
->tap
->dotted_name
);
1706 CMD_CTX
->current_target
= target
->target_number
;
1711 target
= all_targets
;
1712 command_print(CMD_CTX
, " TargetName Type Endian TapName State ");
1713 command_print(CMD_CTX
, "-- ------------------ ---------- ------ ------------------ ------------");
1719 if (target
->tap
->enabled
)
1720 state
= target_state_name( target
);
1722 state
= "tap-disabled";
1724 if (CMD_CTX
->current_target
== target
->target_number
)
1727 /* keep columns lined up to match the headers above */
1728 command_print(CMD_CTX
, "%2d%c %-18s %-10s %-6s %-18s %s",
1729 target
->target_number
,
1731 target_name(target
),
1732 target_type_name(target
),
1733 Jim_Nvp_value2name_simple(nvp_target_endian
,
1734 target
->endianness
)->name
,
1735 target
->tap
->dotted_name
,
1737 target
= target
->next
;
1743 /* every 300ms we check for reset & powerdropout and issue a "reset halt" if so. */
1745 static int powerDropout
;
1746 static int srstAsserted
;
1748 static int runPowerRestore
;
1749 static int runPowerDropout
;
1750 static int runSrstAsserted
;
1751 static int runSrstDeasserted
;
1753 static int sense_handler(void)
1755 static int prevSrstAsserted
= 0;
1756 static int prevPowerdropout
= 0;
1759 if ((retval
= jtag_power_dropout(&powerDropout
)) != ERROR_OK
)
1763 powerRestored
= prevPowerdropout
&& !powerDropout
;
1766 runPowerRestore
= 1;
1769 long long current
= timeval_ms();
1770 static long long lastPower
= 0;
1771 int waitMore
= lastPower
+ 2000 > current
;
1772 if (powerDropout
&& !waitMore
)
1774 runPowerDropout
= 1;
1775 lastPower
= current
;
1778 if ((retval
= jtag_srst_asserted(&srstAsserted
)) != ERROR_OK
)
1782 srstDeasserted
= prevSrstAsserted
&& !srstAsserted
;
1784 static long long lastSrst
= 0;
1785 waitMore
= lastSrst
+ 2000 > current
;
1786 if (srstDeasserted
&& !waitMore
)
1788 runSrstDeasserted
= 1;
1792 if (!prevSrstAsserted
&& srstAsserted
)
1794 runSrstAsserted
= 1;
1797 prevSrstAsserted
= srstAsserted
;
1798 prevPowerdropout
= powerDropout
;
1800 if (srstDeasserted
|| powerRestored
)
1802 /* Other than logging the event we can't do anything here.
1803 * Issuing a reset is a particularly bad idea as we might
1804 * be inside a reset already.
1811 static int backoff_times
= 0;
1812 static int backoff_count
= 0;
1814 /* process target state changes */
1815 static int handle_target(void *priv
)
1817 Jim_Interp
*interp
= (Jim_Interp
*)priv
;
1818 int retval
= ERROR_OK
;
1820 if (!is_jtag_poll_safe())
1822 /* polling is disabled currently */
1826 /* we do not want to recurse here... */
1827 static int recursive
= 0;
1832 /* danger! running these procedures can trigger srst assertions and power dropouts.
1833 * We need to avoid an infinite loop/recursion here and we do that by
1834 * clearing the flags after running these events.
1836 int did_something
= 0;
1837 if (runSrstAsserted
)
1839 LOG_INFO("srst asserted detected, running srst_asserted proc.");
1840 Jim_Eval(interp
, "srst_asserted");
1843 if (runSrstDeasserted
)
1845 Jim_Eval(interp
, "srst_deasserted");
1848 if (runPowerDropout
)
1850 LOG_INFO("Power dropout detected, running power_dropout proc.");
1851 Jim_Eval(interp
, "power_dropout");
1854 if (runPowerRestore
)
1856 Jim_Eval(interp
, "power_restore");
1862 /* clear detect flags */
1866 /* clear action flags */
1868 runSrstAsserted
= 0;
1869 runSrstDeasserted
= 0;
1870 runPowerRestore
= 0;
1871 runPowerDropout
= 0;
1876 if (backoff_times
> backoff_count
)
1878 /* do not poll this time as we failed previously */
1884 /* Poll targets for state changes unless that's globally disabled.
1885 * Skip targets that are currently disabled.
1887 for (struct target
*target
= all_targets
;
1888 is_jtag_poll_safe() && target
;
1889 target
= target
->next
)
1891 if (!target
->tap
->enabled
)
1894 /* only poll target if we've got power and srst isn't asserted */
1895 if (!powerDropout
&& !srstAsserted
)
1897 /* polling may fail silently until the target has been examined */
1898 if ((retval
= target_poll(target
)) != ERROR_OK
)
1900 /* 100ms polling interval. Increase interval between polling up to 5000ms */
1901 if (backoff_times
* polling_interval
< 5000)
1906 LOG_USER("Polling target failed, GDB will be halted. Polling again in %dms", backoff_times
* polling_interval
);
1908 /* Tell GDB to halt the debugger. This allows the user to
1909 * run monitor commands to handle the situation.
1911 target_call_event_callbacks(target
, TARGET_EVENT_GDB_HALT
);
1914 /* Since we succeeded, we reset backoff count */
1915 if (backoff_times
> 0)
1917 LOG_USER("Polling succeeded again");
1926 COMMAND_HANDLER(handle_reg_command
)
1928 struct target
*target
;
1929 struct reg
*reg
= NULL
;
1935 target
= get_current_target(CMD_CTX
);
1937 /* list all available registers for the current target */
1940 struct reg_cache
*cache
= target
->reg_cache
;
1947 command_print(CMD_CTX
, "===== %s", cache
->name
);
1949 for (i
= 0, reg
= cache
->reg_list
;
1950 i
< cache
->num_regs
;
1951 i
++, reg
++, count
++)
1953 /* only print cached values if they are valid */
1955 value
= buf_to_str(reg
->value
,
1957 command_print(CMD_CTX
,
1958 "(%i) %s (/%" PRIu32
"): 0x%s%s",
1966 command_print(CMD_CTX
, "(%i) %s (/%" PRIu32
")",
1971 cache
= cache
->next
;
1977 /* access a single register by its ordinal number */
1978 if ((CMD_ARGV
[0][0] >= '0') && (CMD_ARGV
[0][0] <= '9'))
1981 COMMAND_PARSE_NUMBER(uint
, CMD_ARGV
[0], num
);
1983 struct reg_cache
*cache
= target
->reg_cache
;
1988 for (i
= 0; i
< cache
->num_regs
; i
++)
1992 reg
= &cache
->reg_list
[i
];
1998 cache
= cache
->next
;
2003 command_print(CMD_CTX
, "%i is out of bounds, the current target has only %i registers (0 - %i)", num
, count
, count
- 1);
2006 } else /* access a single register by its name */
2008 reg
= register_get_by_name(target
->reg_cache
, CMD_ARGV
[0], 1);
2012 command_print(CMD_CTX
, "register %s not found in current target", CMD_ARGV
[0]);
2017 /* display a register */
2018 if ((CMD_ARGC
== 1) || ((CMD_ARGC
== 2) && !((CMD_ARGV
[1][0] >= '0') && (CMD_ARGV
[1][0] <= '9'))))
2020 if ((CMD_ARGC
== 2) && (strcmp(CMD_ARGV
[1], "force") == 0))
2023 if (reg
->valid
== 0)
2025 reg
->type
->get(reg
);
2027 value
= buf_to_str(reg
->value
, reg
->size
, 16);
2028 command_print(CMD_CTX
, "%s (/%i): 0x%s", reg
->name
, (int)(reg
->size
), value
);
2033 /* set register value */
2036 uint8_t *buf
= malloc(DIV_ROUND_UP(reg
->size
, 8));
2037 str_to_buf(CMD_ARGV
[1], strlen(CMD_ARGV
[1]), buf
, reg
->size
, 0);
2039 reg
->type
->set(reg
, buf
);
2041 value
= buf_to_str(reg
->value
, reg
->size
, 16);
2042 command_print(CMD_CTX
, "%s (/%i): 0x%s", reg
->name
, (int)(reg
->size
), value
);
2050 command_print(CMD_CTX
, "usage: reg <#|name> [value]");
2055 COMMAND_HANDLER(handle_poll_command
)
2057 int retval
= ERROR_OK
;
2058 struct target
*target
= get_current_target(CMD_CTX
);
2062 command_print(CMD_CTX
, "background polling: %s",
2063 jtag_poll_get_enabled() ? "on" : "off");
2064 command_print(CMD_CTX
, "TAP: %s (%s)",
2065 target
->tap
->dotted_name
,
2066 target
->tap
->enabled
? "enabled" : "disabled");
2067 if (!target
->tap
->enabled
)
2069 if ((retval
= target_poll(target
)) != ERROR_OK
)
2071 if ((retval
= target_arch_state(target
)) != ERROR_OK
)
2074 else if (CMD_ARGC
== 1)
2077 COMMAND_PARSE_ON_OFF(CMD_ARGV
[0], enable
);
2078 jtag_poll_set_enabled(enable
);
2082 return ERROR_COMMAND_SYNTAX_ERROR
;
2088 COMMAND_HANDLER(handle_wait_halt_command
)
2091 return ERROR_COMMAND_SYNTAX_ERROR
;
2096 int retval
= parse_uint(CMD_ARGV
[0], &ms
);
2097 if (ERROR_OK
!= retval
)
2099 command_print(CMD_CTX
, "usage: %s [seconds]", CMD_NAME
);
2100 return ERROR_COMMAND_SYNTAX_ERROR
;
2102 // convert seconds (given) to milliseconds (needed)
2106 struct target
*target
= get_current_target(CMD_CTX
);
2107 return target_wait_state(target
, TARGET_HALTED
, ms
);
2110 /* wait for target state to change. The trick here is to have a low
2111 * latency for short waits and not to suck up all the CPU time
2114 * After 500ms, keep_alive() is invoked
2116 int target_wait_state(struct target
*target
, enum target_state state
, int ms
)
2119 long long then
= 0, cur
;
2124 if ((retval
= target_poll(target
)) != ERROR_OK
)
2126 if (target
->state
== state
)
2134 then
= timeval_ms();
2135 LOG_DEBUG("waiting for target %s...",
2136 Jim_Nvp_value2name_simple(nvp_target_state
,state
)->name
);
2144 if ((cur
-then
) > ms
)
2146 LOG_ERROR("timed out while waiting for target %s",
2147 Jim_Nvp_value2name_simple(nvp_target_state
,state
)->name
);
2155 COMMAND_HANDLER(handle_halt_command
)
2159 struct target
*target
= get_current_target(CMD_CTX
);
2160 int retval
= target_halt(target
);
2161 if (ERROR_OK
!= retval
)
2166 unsigned wait_local
;
2167 retval
= parse_uint(CMD_ARGV
[0], &wait_local
);
2168 if (ERROR_OK
!= retval
)
2169 return ERROR_COMMAND_SYNTAX_ERROR
;
2174 return CALL_COMMAND_HANDLER(handle_wait_halt_command
);
2177 COMMAND_HANDLER(handle_soft_reset_halt_command
)
2179 struct target
*target
= get_current_target(CMD_CTX
);
2181 LOG_USER("requesting target halt and executing a soft reset");
2183 target
->type
->soft_reset_halt(target
);
2188 COMMAND_HANDLER(handle_reset_command
)
2191 return ERROR_COMMAND_SYNTAX_ERROR
;
2193 enum target_reset_mode reset_mode
= RESET_RUN
;
2197 n
= Jim_Nvp_name2value_simple(nvp_reset_modes
, CMD_ARGV
[0]);
2198 if ((n
->name
== NULL
) || (n
->value
== RESET_UNKNOWN
)) {
2199 return ERROR_COMMAND_SYNTAX_ERROR
;
2201 reset_mode
= n
->value
;
2204 /* reset *all* targets */
2205 return target_process_reset(CMD_CTX
, reset_mode
);
2209 COMMAND_HANDLER(handle_resume_command
)
2213 return ERROR_COMMAND_SYNTAX_ERROR
;
2215 struct target
*target
= get_current_target(CMD_CTX
);
2216 target_handle_event(target
, TARGET_EVENT_OLD_pre_resume
);
2218 /* with no CMD_ARGV, resume from current pc, addr = 0,
2219 * with one arguments, addr = CMD_ARGV[0],
2220 * handle breakpoints, not debugging */
2224 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], addr
);
2228 return target_resume(target
, current
, addr
, 1, 0);
2231 COMMAND_HANDLER(handle_step_command
)
2234 return ERROR_COMMAND_SYNTAX_ERROR
;
2238 /* with no CMD_ARGV, step from current pc, addr = 0,
2239 * with one argument addr = CMD_ARGV[0],
2240 * handle breakpoints, debugging */
2245 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], addr
);
2249 struct target
*target
= get_current_target(CMD_CTX
);
2251 return target
->type
->step(target
, current_pc
, addr
, 1);
2254 static void handle_md_output(struct command_context
*cmd_ctx
,
2255 struct target
*target
, uint32_t address
, unsigned size
,
2256 unsigned count
, const uint8_t *buffer
)
2258 const unsigned line_bytecnt
= 32;
2259 unsigned line_modulo
= line_bytecnt
/ size
;
2261 char output
[line_bytecnt
* 4 + 1];
2262 unsigned output_len
= 0;
2264 const char *value_fmt
;
2266 case 4: value_fmt
= "%8.8x "; break;
2267 case 2: value_fmt
= "%4.4x "; break;
2268 case 1: value_fmt
= "%2.2x "; break;
2270 /* "can't happen", caller checked */
2271 LOG_ERROR("invalid memory read size: %u", size
);
2275 for (unsigned i
= 0; i
< count
; i
++)
2277 if (i
% line_modulo
== 0)
2279 output_len
+= snprintf(output
+ output_len
,
2280 sizeof(output
) - output_len
,
2282 (unsigned)(address
+ (i
*size
)));
2286 const uint8_t *value_ptr
= buffer
+ i
* size
;
2288 case 4: value
= target_buffer_get_u32(target
, value_ptr
); break;
2289 case 2: value
= target_buffer_get_u16(target
, value_ptr
); break;
2290 case 1: value
= *value_ptr
;
2292 output_len
+= snprintf(output
+ output_len
,
2293 sizeof(output
) - output_len
,
2296 if ((i
% line_modulo
== line_modulo
- 1) || (i
== count
- 1))
2298 command_print(cmd_ctx
, "%s", output
);
2304 COMMAND_HANDLER(handle_md_command
)
2307 return ERROR_COMMAND_SYNTAX_ERROR
;
2310 switch (CMD_NAME
[2]) {
2311 case 'w': size
= 4; break;
2312 case 'h': size
= 2; break;
2313 case 'b': size
= 1; break;
2314 default: return ERROR_COMMAND_SYNTAX_ERROR
;
2317 bool physical
=strcmp(CMD_ARGV
[0], "phys")==0;
2318 int (*fn
)(struct target
*target
,
2319 uint32_t address
, uint32_t size_value
, uint32_t count
, uint8_t *buffer
);
2324 fn
=target_read_phys_memory
;
2327 fn
=target_read_memory
;
2329 if ((CMD_ARGC
< 1) || (CMD_ARGC
> 2))
2331 return ERROR_COMMAND_SYNTAX_ERROR
;
2335 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], address
);
2339 COMMAND_PARSE_NUMBER(uint
, CMD_ARGV
[1], count
);
2341 uint8_t *buffer
= calloc(count
, size
);
2343 struct target
*target
= get_current_target(CMD_CTX
);
2344 int retval
= fn(target
, address
, size
, count
, buffer
);
2345 if (ERROR_OK
== retval
)
2346 handle_md_output(CMD_CTX
, target
, address
, size
, count
, buffer
);
2353 typedef int (*target_write_fn
)(struct target
*target
,
2354 uint32_t address
, uint32_t size
, uint32_t count
, uint8_t *buffer
);
2356 static int target_write_memory_fast(struct target
*target
,
2357 uint32_t address
, uint32_t size
, uint32_t count
, uint8_t *buffer
)
2359 return target_write_buffer(target
, address
, size
* count
, buffer
);
2362 static int target_fill_mem(struct target
*target
,
2371 /* We have to write in reasonably large chunks to be able
2372 * to fill large memory areas with any sane speed */
2373 const unsigned chunk_size
= 16384;
2374 uint8_t *target_buf
= malloc(chunk_size
* data_size
);
2375 if (target_buf
== NULL
)
2377 LOG_ERROR("Out of memory");
2381 for (unsigned i
= 0; i
< chunk_size
; i
++)
2386 target_buffer_set_u32(target
, target_buf
+ i
*data_size
, b
);
2389 target_buffer_set_u16(target
, target_buf
+ i
*data_size
, b
);
2392 target_buffer_set_u8(target
, target_buf
+ i
*data_size
, b
);
2399 int retval
= ERROR_OK
;
2401 for (unsigned x
= 0; x
< c
; x
+= chunk_size
)
2405 if (current
> chunk_size
)
2407 current
= chunk_size
;
2409 retval
= fn(target
, address
+ x
* data_size
, data_size
, current
, target_buf
);
2410 if (retval
!= ERROR_OK
)
2414 /* avoid GDB timeouts */
2423 COMMAND_HANDLER(handle_mw_command
)
2427 return ERROR_COMMAND_SYNTAX_ERROR
;
2429 bool physical
=strcmp(CMD_ARGV
[0], "phys")==0;
2435 fn
=target_write_phys_memory
;
2438 fn
= target_write_memory_fast
;
2440 if ((CMD_ARGC
< 2) || (CMD_ARGC
> 3))
2441 return ERROR_COMMAND_SYNTAX_ERROR
;
2444 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], address
);
2447 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], value
);
2451 COMMAND_PARSE_NUMBER(uint
, CMD_ARGV
[2], count
);
2453 struct target
*target
= get_current_target(CMD_CTX
);
2455 switch (CMD_NAME
[2])
2467 return ERROR_COMMAND_SYNTAX_ERROR
;
2470 return target_fill_mem(target
, address
, fn
, wordsize
, value
, count
);
2473 static COMMAND_HELPER(parse_load_image_command_CMD_ARGV
, struct image
*image
,
2474 uint32_t *min_address
, uint32_t *max_address
)
2476 if (CMD_ARGC
< 1 || CMD_ARGC
> 5)
2477 return ERROR_COMMAND_SYNTAX_ERROR
;
2479 /* a base address isn't always necessary,
2480 * default to 0x0 (i.e. don't relocate) */
2484 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], addr
);
2485 image
->base_address
= addr
;
2486 image
->base_address_set
= 1;
2489 image
->base_address_set
= 0;
2491 image
->start_address_set
= 0;
2495 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[3], *min_address
);
2499 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[4], *max_address
);
2500 // use size (given) to find max (required)
2501 *max_address
+= *min_address
;
2504 if (*min_address
> *max_address
)
2505 return ERROR_COMMAND_SYNTAX_ERROR
;
2510 COMMAND_HANDLER(handle_load_image_command
)
2514 uint32_t image_size
;
2515 uint32_t min_address
= 0;
2516 uint32_t max_address
= 0xffffffff;
2520 int retval
= CALL_COMMAND_HANDLER(parse_load_image_command_CMD_ARGV
,
2521 &image
, &min_address
, &max_address
);
2522 if (ERROR_OK
!= retval
)
2525 struct target
*target
= get_current_target(CMD_CTX
);
2527 struct duration bench
;
2528 duration_start(&bench
);
2530 if (image_open(&image
, CMD_ARGV
[0], (CMD_ARGC
>= 3) ? CMD_ARGV
[2] : NULL
) != ERROR_OK
)
2537 for (i
= 0; i
< image
.num_sections
; i
++)
2539 buffer
= malloc(image
.sections
[i
].size
);
2542 command_print(CMD_CTX
,
2543 "error allocating buffer for section (%d bytes)",
2544 (int)(image
.sections
[i
].size
));
2548 if ((retval
= image_read_section(&image
, i
, 0x0, image
.sections
[i
].size
, buffer
, &buf_cnt
)) != ERROR_OK
)
2554 uint32_t offset
= 0;
2555 uint32_t length
= buf_cnt
;
2557 /* DANGER!!! beware of unsigned comparision here!!! */
2559 if ((image
.sections
[i
].base_address
+ buf_cnt
>= min_address
)&&
2560 (image
.sections
[i
].base_address
< max_address
))
2562 if (image
.sections
[i
].base_address
< min_address
)
2564 /* clip addresses below */
2565 offset
+= min_address
-image
.sections
[i
].base_address
;
2569 if (image
.sections
[i
].base_address
+ buf_cnt
> max_address
)
2571 length
-= (image
.sections
[i
].base_address
+ buf_cnt
)-max_address
;
2574 if ((retval
= target_write_buffer(target
, image
.sections
[i
].base_address
+ offset
, length
, buffer
+ offset
)) != ERROR_OK
)
2579 image_size
+= length
;
2580 command_print(CMD_CTX
, "%u bytes written at address 0x%8.8" PRIx32
"",
2581 (unsigned int)length
,
2582 image
.sections
[i
].base_address
+ offset
);
2588 if ((ERROR_OK
== retval
) && (duration_measure(&bench
) == ERROR_OK
))
2590 command_print(CMD_CTX
, "downloaded %" PRIu32
" bytes "
2591 "in %fs (%0.3f KiB/s)", image_size
,
2592 duration_elapsed(&bench
), duration_kbps(&bench
, image_size
));
2595 image_close(&image
);
2601 COMMAND_HANDLER(handle_dump_image_command
)
2603 struct fileio fileio
;
2604 uint8_t buffer
[560];
2605 int retval
, retvaltemp
;
2606 uint32_t address
, size
;
2607 struct duration bench
;
2608 struct target
*target
= get_current_target(CMD_CTX
);
2611 return ERROR_COMMAND_SYNTAX_ERROR
;
2613 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], address
);
2614 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[2], size
);
2616 retval
= fileio_open(&fileio
, CMD_ARGV
[0], FILEIO_WRITE
, FILEIO_BINARY
);
2617 if (retval
!= ERROR_OK
)
2620 duration_start(&bench
);
2625 size_t size_written
;
2626 uint32_t this_run_size
= (size
> 560) ? 560 : size
;
2627 retval
= target_read_buffer(target
, address
, this_run_size
, buffer
);
2628 if (retval
!= ERROR_OK
)
2633 retval
= fileio_write(&fileio
, this_run_size
, buffer
, &size_written
);
2634 if (retval
!= ERROR_OK
)
2639 size
-= this_run_size
;
2640 address
+= this_run_size
;
2643 if ((ERROR_OK
== retval
) && (duration_measure(&bench
) == ERROR_OK
))
2646 retval
= fileio_size(&fileio
, &filesize
);
2647 if (retval
!= ERROR_OK
)
2649 command_print(CMD_CTX
,
2650 "dumped %ld bytes in %fs (%0.3f KiB/s)", (long)filesize
,
2651 duration_elapsed(&bench
), duration_kbps(&bench
, filesize
));
2654 if ((retvaltemp
= fileio_close(&fileio
)) != ERROR_OK
)
2660 static COMMAND_HELPER(handle_verify_image_command_internal
, int verify
)
2664 uint32_t image_size
;
2667 uint32_t checksum
= 0;
2668 uint32_t mem_checksum
= 0;
2672 struct target
*target
= get_current_target(CMD_CTX
);
2676 return ERROR_COMMAND_SYNTAX_ERROR
;
2681 LOG_ERROR("no target selected");
2685 struct duration bench
;
2686 duration_start(&bench
);
2691 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], addr
);
2692 image
.base_address
= addr
;
2693 image
.base_address_set
= 1;
2697 image
.base_address_set
= 0;
2698 image
.base_address
= 0x0;
2701 image
.start_address_set
= 0;
2703 if ((retval
= image_open(&image
, CMD_ARGV
[0], (CMD_ARGC
== 3) ? CMD_ARGV
[2] : NULL
)) != ERROR_OK
)
2711 for (i
= 0; i
< image
.num_sections
; i
++)
2713 buffer
= malloc(image
.sections
[i
].size
);
2716 command_print(CMD_CTX
,
2717 "error allocating buffer for section (%d bytes)",
2718 (int)(image
.sections
[i
].size
));
2721 if ((retval
= image_read_section(&image
, i
, 0x0, image
.sections
[i
].size
, buffer
, &buf_cnt
)) != ERROR_OK
)
2729 /* calculate checksum of image */
2730 retval
= image_calculate_checksum(buffer
, buf_cnt
, &checksum
);
2731 if (retval
!= ERROR_OK
)
2737 retval
= target_checksum_memory(target
, image
.sections
[i
].base_address
, buf_cnt
, &mem_checksum
);
2738 if (retval
!= ERROR_OK
)
2744 if (checksum
!= mem_checksum
)
2746 /* failed crc checksum, fall back to a binary compare */
2751 LOG_ERROR("checksum mismatch - attempting binary compare");
2754 data
= (uint8_t*)malloc(buf_cnt
);
2756 /* Can we use 32bit word accesses? */
2758 int count
= buf_cnt
;
2759 if ((count
% 4) == 0)
2764 retval
= target_read_memory(target
, image
.sections
[i
].base_address
, size
, count
, data
);
2765 if (retval
== ERROR_OK
)
2768 for (t
= 0; t
< buf_cnt
; t
++)
2770 if (data
[t
] != buffer
[t
])
2772 command_print(CMD_CTX
,
2773 "diff %d address 0x%08x. Was 0x%02x instead of 0x%02x",
2775 (unsigned)(t
+ image
.sections
[i
].base_address
),
2780 command_print(CMD_CTX
, "More than 128 errors, the rest are not printed.");
2793 command_print(CMD_CTX
, "address 0x%08" PRIx32
" length 0x%08zx",
2794 image
.sections
[i
].base_address
,
2799 image_size
+= buf_cnt
;
2803 command_print(CMD_CTX
, "No more differences found.");
2808 retval
= ERROR_FAIL
;
2810 if ((ERROR_OK
== retval
) && (duration_measure(&bench
) == ERROR_OK
))
2812 command_print(CMD_CTX
, "verified %" PRIu32
" bytes "
2813 "in %fs (%0.3f KiB/s)", image_size
,
2814 duration_elapsed(&bench
), duration_kbps(&bench
, image_size
));
2817 image_close(&image
);
2822 COMMAND_HANDLER(handle_verify_image_command
)
2824 return CALL_COMMAND_HANDLER(handle_verify_image_command_internal
, 1);
2827 COMMAND_HANDLER(handle_test_image_command
)
2829 return CALL_COMMAND_HANDLER(handle_verify_image_command_internal
, 0);
2832 static int handle_bp_command_list(struct command_context
*cmd_ctx
)
2834 struct target
*target
= get_current_target(cmd_ctx
);
2835 struct breakpoint
*breakpoint
= target
->breakpoints
;
2838 if (breakpoint
->type
== BKPT_SOFT
)
2840 char* buf
= buf_to_str(breakpoint
->orig_instr
,
2841 breakpoint
->length
, 16);
2842 command_print(cmd_ctx
, "0x%8.8" PRIx32
", 0x%x, %i, 0x%s",
2843 breakpoint
->address
,
2845 breakpoint
->set
, buf
);
2850 command_print(cmd_ctx
, "0x%8.8" PRIx32
", 0x%x, %i",
2851 breakpoint
->address
,
2852 breakpoint
->length
, breakpoint
->set
);
2855 breakpoint
= breakpoint
->next
;
2860 static int handle_bp_command_set(struct command_context
*cmd_ctx
,
2861 uint32_t addr
, uint32_t length
, int hw
)
2863 struct target
*target
= get_current_target(cmd_ctx
);
2864 int retval
= breakpoint_add(target
, addr
, length
, hw
);
2865 if (ERROR_OK
== retval
)
2866 command_print(cmd_ctx
, "breakpoint set at 0x%8.8" PRIx32
"", addr
);
2868 LOG_ERROR("Failure setting breakpoint");
2872 COMMAND_HANDLER(handle_bp_command
)
2875 return handle_bp_command_list(CMD_CTX
);
2877 if (CMD_ARGC
< 2 || CMD_ARGC
> 3)
2879 command_print(CMD_CTX
, "usage: bp <address> <length> ['hw']");
2880 return ERROR_COMMAND_SYNTAX_ERROR
;
2884 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], addr
);
2886 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], length
);
2891 if (strcmp(CMD_ARGV
[2], "hw") == 0)
2894 return ERROR_COMMAND_SYNTAX_ERROR
;
2897 return handle_bp_command_set(CMD_CTX
, addr
, length
, hw
);
2900 COMMAND_HANDLER(handle_rbp_command
)
2903 return ERROR_COMMAND_SYNTAX_ERROR
;
2906 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], addr
);
2908 struct target
*target
= get_current_target(CMD_CTX
);
2909 breakpoint_remove(target
, addr
);
2914 COMMAND_HANDLER(handle_wp_command
)
2916 struct target
*target
= get_current_target(CMD_CTX
);
2920 struct watchpoint
*watchpoint
= target
->watchpoints
;
2924 command_print(CMD_CTX
, "address: 0x%8.8" PRIx32
2925 ", len: 0x%8.8" PRIx32
2926 ", r/w/a: %i, value: 0x%8.8" PRIx32
2927 ", mask: 0x%8.8" PRIx32
,
2928 watchpoint
->address
,
2930 (int)watchpoint
->rw
,
2933 watchpoint
= watchpoint
->next
;
2938 enum watchpoint_rw type
= WPT_ACCESS
;
2940 uint32_t length
= 0;
2941 uint32_t data_value
= 0x0;
2942 uint32_t data_mask
= 0xffffffff;
2947 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[4], data_mask
);
2950 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[3], data_value
);
2953 switch (CMD_ARGV
[2][0])
2965 LOG_ERROR("invalid watchpoint mode ('%c')", CMD_ARGV
[2][0]);
2966 return ERROR_COMMAND_SYNTAX_ERROR
;
2970 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], length
);
2971 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], addr
);
2975 command_print(CMD_CTX
, "usage: wp [address length "
2976 "[(r|w|a) [value [mask]]]]");
2977 return ERROR_COMMAND_SYNTAX_ERROR
;
2980 int retval
= watchpoint_add(target
, addr
, length
, type
,
2981 data_value
, data_mask
);
2982 if (ERROR_OK
!= retval
)
2983 LOG_ERROR("Failure setting watchpoints");
2988 COMMAND_HANDLER(handle_rwp_command
)
2991 return ERROR_COMMAND_SYNTAX_ERROR
;
2994 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], addr
);
2996 struct target
*target
= get_current_target(CMD_CTX
);
2997 watchpoint_remove(target
, addr
);
3004 * Translate a virtual address to a physical address.
3006 * The low-level target implementation must have logged a detailed error
3007 * which is forwarded to telnet/GDB session.
3009 COMMAND_HANDLER(handle_virt2phys_command
)
3012 return ERROR_COMMAND_SYNTAX_ERROR
;
3015 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], va
);
3018 struct target
*target
= get_current_target(CMD_CTX
);
3019 int retval
= target
->type
->virt2phys(target
, va
, &pa
);
3020 if (retval
== ERROR_OK
)
3021 command_print(CMD_CTX
, "Physical address 0x%08" PRIx32
"", pa
);
3026 static void writeData(FILE *f
, const void *data
, size_t len
)
3028 size_t written
= fwrite(data
, 1, len
, f
);
3030 LOG_ERROR("failed to write %zu bytes: %s", len
, strerror(errno
));
3033 static void writeLong(FILE *f
, int l
)
3036 for (i
= 0; i
< 4; i
++)
3038 char c
= (l
>> (i
*8))&0xff;
3039 writeData(f
, &c
, 1);
3044 static void writeString(FILE *f
, char *s
)
3046 writeData(f
, s
, strlen(s
));
3049 /* Dump a gmon.out histogram file. */
3050 static void writeGmon(uint32_t *samples
, uint32_t sampleNum
, const char *filename
)
3053 FILE *f
= fopen(filename
, "w");
3056 writeString(f
, "gmon");
3057 writeLong(f
, 0x00000001); /* Version */
3058 writeLong(f
, 0); /* padding */
3059 writeLong(f
, 0); /* padding */
3060 writeLong(f
, 0); /* padding */
3062 uint8_t zero
= 0; /* GMON_TAG_TIME_HIST */
3063 writeData(f
, &zero
, 1);
3065 /* figure out bucket size */
3066 uint32_t min
= samples
[0];
3067 uint32_t max
= samples
[0];
3068 for (i
= 0; i
< sampleNum
; i
++)
3070 if (min
> samples
[i
])
3074 if (max
< samples
[i
])
3080 int addressSpace
= (max
-min
+ 1);
3082 static const uint32_t maxBuckets
= 16 * 1024; /* maximum buckets. */
3083 uint32_t length
= addressSpace
;
3084 if (length
> maxBuckets
)
3086 length
= maxBuckets
;
3088 int *buckets
= malloc(sizeof(int)*length
);
3089 if (buckets
== NULL
)
3094 memset(buckets
, 0, sizeof(int)*length
);
3095 for (i
= 0; i
< sampleNum
;i
++)
3097 uint32_t address
= samples
[i
];
3098 long long a
= address
-min
;
3099 long long b
= length
-1;
3100 long long c
= addressSpace
-1;
3101 int index_t
= (a
*b
)/c
; /* danger!!!! int32 overflows */
3105 /* append binary memory gmon.out &profile_hist_hdr ((char*)&profile_hist_hdr + sizeof(struct gmon_hist_hdr)) */
3106 writeLong(f
, min
); /* low_pc */
3107 writeLong(f
, max
); /* high_pc */
3108 writeLong(f
, length
); /* # of samples */
3109 writeLong(f
, 100); /* KLUDGE! We lie, ca. 100Hz best case. */
3110 writeString(f
, "seconds");
3111 for (i
= 0; i
< (15-strlen("seconds")); i
++)
3112 writeData(f
, &zero
, 1);
3113 writeString(f
, "s");
3115 /*append binary memory gmon.out profile_hist_data (profile_hist_data + profile_hist_hdr.hist_size) */
3117 char *data
= malloc(2*length
);
3120 for (i
= 0; i
< length
;i
++)
3129 data
[i
*2 + 1]=(val
>> 8)&0xff;
3132 writeData(f
, data
, length
* 2);
3142 /* profiling samples the CPU PC as quickly as OpenOCD is able,
3143 * which will be used as a random sampling of PC */
3144 COMMAND_HANDLER(handle_profile_command
)
3146 struct target
*target
= get_current_target(CMD_CTX
);
3147 struct timeval timeout
, now
;
3149 gettimeofday(&timeout
, NULL
);
3152 return ERROR_COMMAND_SYNTAX_ERROR
;
3155 COMMAND_PARSE_NUMBER(uint
, CMD_ARGV
[0], offset
);
3157 timeval_add_time(&timeout
, offset
, 0);
3160 * @todo: Some cores let us sample the PC without the
3161 * annoying halt/resume step; for example, ARMv7 PCSR.
3162 * Provide a way to use that more efficient mechanism.
3165 command_print(CMD_CTX
, "Starting profiling. Halting and resuming the target as often as we can...");
3167 static const int maxSample
= 10000;
3168 uint32_t *samples
= malloc(sizeof(uint32_t)*maxSample
);
3169 if (samples
== NULL
)
3173 /* hopefully it is safe to cache! We want to stop/restart as quickly as possible. */
3174 struct reg
*reg
= register_get_by_name(target
->reg_cache
, "pc", 1);
3179 target_poll(target
);
3180 if (target
->state
== TARGET_HALTED
)
3182 uint32_t t
=*((uint32_t *)reg
->value
);
3183 samples
[numSamples
++]=t
;
3184 retval
= target_resume(target
, 1, 0, 0, 0); /* current pc, addr = 0, do not handle breakpoints, not debugging */
3185 target_poll(target
);
3186 alive_sleep(10); /* sleep 10ms, i.e. <100 samples/second. */
3187 } else if (target
->state
== TARGET_RUNNING
)
3189 /* We want to quickly sample the PC. */
3190 if ((retval
= target_halt(target
)) != ERROR_OK
)
3197 command_print(CMD_CTX
, "Target not halted or running");
3201 if (retval
!= ERROR_OK
)
3206 gettimeofday(&now
, NULL
);
3207 if ((numSamples
>= maxSample
) || ((now
.tv_sec
>= timeout
.tv_sec
) && (now
.tv_usec
>= timeout
.tv_usec
)))
3209 command_print(CMD_CTX
, "Profiling completed. %d samples.", numSamples
);
3210 if ((retval
= target_poll(target
)) != ERROR_OK
)
3215 if (target
->state
== TARGET_HALTED
)
3217 target_resume(target
, 1, 0, 0, 0); /* current pc, addr = 0, do not handle breakpoints, not debugging */
3219 if ((retval
= target_poll(target
)) != ERROR_OK
)
3224 writeGmon(samples
, numSamples
, CMD_ARGV
[1]);
3225 command_print(CMD_CTX
, "Wrote %s", CMD_ARGV
[1]);
3234 static int new_int_array_element(Jim_Interp
* interp
, const char *varname
, int idx
, uint32_t val
)
3237 Jim_Obj
*nameObjPtr
, *valObjPtr
;
3240 namebuf
= alloc_printf("%s(%d)", varname
, idx
);
3244 nameObjPtr
= Jim_NewStringObj(interp
, namebuf
, -1);
3245 valObjPtr
= Jim_NewIntObj(interp
, val
);
3246 if (!nameObjPtr
|| !valObjPtr
)
3252 Jim_IncrRefCount(nameObjPtr
);
3253 Jim_IncrRefCount(valObjPtr
);
3254 result
= Jim_SetVariable(interp
, nameObjPtr
, valObjPtr
);
3255 Jim_DecrRefCount(interp
, nameObjPtr
);
3256 Jim_DecrRefCount(interp
, valObjPtr
);
3258 /* printf("%s(%d) <= 0%08x\n", varname, idx, val); */
3262 static int jim_mem2array(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
3264 struct command_context
*context
;
3265 struct target
*target
;
3267 context
= current_command_context(interp
);
3268 assert (context
!= NULL
);
3270 target
= get_current_target(context
);
3273 LOG_ERROR("mem2array: no current target");
3277 return target_mem2array(interp
, target
, argc
-1, argv
+ 1);
3280 static int target_mem2array(Jim_Interp
*interp
, struct target
*target
, int argc
, Jim_Obj
*const *argv
)
3288 const char *varname
;
3292 /* argv[1] = name of array to receive the data
3293 * argv[2] = desired width
3294 * argv[3] = memory address
3295 * argv[4] = count of times to read
3298 Jim_WrongNumArgs(interp
, 1, argv
, "varname width addr nelems");
3301 varname
= Jim_GetString(argv
[0], &len
);
3302 /* given "foo" get space for worse case "foo(%d)" .. add 20 */
3304 e
= Jim_GetLong(interp
, argv
[1], &l
);
3310 e
= Jim_GetLong(interp
, argv
[2], &l
);
3315 e
= Jim_GetLong(interp
, argv
[3], &l
);
3331 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3332 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "Invalid width param, must be 8/16/32", NULL
);
3336 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3337 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "mem2array: zero width read?", NULL
);
3340 if ((addr
+ (len
* width
)) < addr
) {
3341 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3342 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "mem2array: addr + len - wraps to zero?", NULL
);
3345 /* absurd transfer size? */
3347 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3348 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "mem2array: absurd > 64K item request", NULL
);
3353 ((width
== 2) && ((addr
& 1) == 0)) ||
3354 ((width
== 4) && ((addr
& 3) == 0))) {
3358 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3359 sprintf(buf
, "mem2array address: 0x%08" PRIx32
" is not aligned for %" PRId32
" byte reads",
3362 Jim_AppendStrings(interp
, Jim_GetResult(interp
), buf
, NULL
);
3371 size_t buffersize
= 4096;
3372 uint8_t *buffer
= malloc(buffersize
);
3379 /* Slurp... in buffer size chunks */
3381 count
= len
; /* in objects.. */
3382 if (count
> (buffersize
/width
)) {
3383 count
= (buffersize
/width
);
3386 retval
= target_read_memory(target
, addr
, width
, count
, buffer
);
3387 if (retval
!= ERROR_OK
) {
3389 LOG_ERROR("mem2array: Read @ 0x%08x, w=%d, cnt=%d, failed",
3393 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3394 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "mem2array: cannot read memory", NULL
);
3398 v
= 0; /* shut up gcc */
3399 for (i
= 0 ;i
< count
;i
++, n
++) {
3402 v
= target_buffer_get_u32(target
, &buffer
[i
*width
]);
3405 v
= target_buffer_get_u16(target
, &buffer
[i
*width
]);
3408 v
= buffer
[i
] & 0x0ff;
3411 new_int_array_element(interp
, varname
, n
, v
);
3419 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3424 static int get_int_array_element(Jim_Interp
* interp
, const char *varname
, int idx
, uint32_t *val
)
3427 Jim_Obj
*nameObjPtr
, *valObjPtr
;
3431 namebuf
= alloc_printf("%s(%d)", varname
, idx
);
3435 nameObjPtr
= Jim_NewStringObj(interp
, namebuf
, -1);
3442 Jim_IncrRefCount(nameObjPtr
);
3443 valObjPtr
= Jim_GetVariable(interp
, nameObjPtr
, JIM_ERRMSG
);
3444 Jim_DecrRefCount(interp
, nameObjPtr
);
3446 if (valObjPtr
== NULL
)
3449 result
= Jim_GetLong(interp
, valObjPtr
, &l
);
3450 /* printf("%s(%d) => 0%08x\n", varname, idx, val); */
3455 static int jim_array2mem(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
3457 struct command_context
*context
;
3458 struct target
*target
;
3460 context
= current_command_context(interp
);
3461 assert (context
!= NULL
);
3463 target
= get_current_target(context
);
3464 if (target
== NULL
) {
3465 LOG_ERROR("array2mem: no current target");
3469 return target_array2mem(interp
,target
, argc
-1, argv
+ 1);
3472 static int target_array2mem(Jim_Interp
*interp
, struct target
*target
,
3473 int argc
, Jim_Obj
*const *argv
)
3481 const char *varname
;
3485 /* argv[1] = name of array to get the data
3486 * argv[2] = desired width
3487 * argv[3] = memory address
3488 * argv[4] = count to write
3491 Jim_WrongNumArgs(interp
, 0, argv
, "varname width addr nelems");
3494 varname
= Jim_GetString(argv
[0], &len
);
3495 /* given "foo" get space for worse case "foo(%d)" .. add 20 */
3497 e
= Jim_GetLong(interp
, argv
[1], &l
);
3503 e
= Jim_GetLong(interp
, argv
[2], &l
);
3508 e
= Jim_GetLong(interp
, argv
[3], &l
);
3524 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3525 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "Invalid width param, must be 8/16/32", NULL
);
3529 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3530 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "array2mem: zero width read?", NULL
);
3533 if ((addr
+ (len
* width
)) < addr
) {
3534 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3535 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "array2mem: addr + len - wraps to zero?", NULL
);
3538 /* absurd transfer size? */
3540 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3541 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "array2mem: absurd > 64K item request", NULL
);
3546 ((width
== 2) && ((addr
& 1) == 0)) ||
3547 ((width
== 4) && ((addr
& 3) == 0))) {
3551 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3552 sprintf(buf
, "array2mem address: 0x%08x is not aligned for %d byte reads",
3555 Jim_AppendStrings(interp
, Jim_GetResult(interp
), buf
, NULL
);
3566 size_t buffersize
= 4096;
3567 uint8_t *buffer
= malloc(buffersize
);
3572 /* Slurp... in buffer size chunks */
3574 count
= len
; /* in objects.. */
3575 if (count
> (buffersize
/width
)) {
3576 count
= (buffersize
/width
);
3579 v
= 0; /* shut up gcc */
3580 for (i
= 0 ;i
< count
;i
++, n
++) {
3581 get_int_array_element(interp
, varname
, n
, &v
);
3584 target_buffer_set_u32(target
, &buffer
[i
*width
], v
);
3587 target_buffer_set_u16(target
, &buffer
[i
*width
], v
);
3590 buffer
[i
] = v
& 0x0ff;
3596 retval
= target_write_memory(target
, addr
, width
, count
, buffer
);
3597 if (retval
!= ERROR_OK
) {
3599 LOG_ERROR("array2mem: Write @ 0x%08x, w=%d, cnt=%d, failed",
3603 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3604 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "array2mem: cannot read memory", NULL
);
3612 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3617 /* FIX? should we propagate errors here rather than printing them
3620 void target_handle_event(struct target
*target
, enum target_event e
)
3622 struct target_event_action
*teap
;
3624 for (teap
= target
->event_action
; teap
!= NULL
; teap
= teap
->next
) {
3625 if (teap
->event
== e
) {
3626 LOG_DEBUG("target: (%d) %s (%s) event: %d (%s) action: %s",
3627 target
->target_number
,
3628 target_name(target
),
3629 target_type_name(target
),
3631 Jim_Nvp_value2name_simple(nvp_target_event
, e
)->name
,
3632 Jim_GetString(teap
->body
, NULL
));
3633 if (Jim_EvalObj(teap
->interp
, teap
->body
) != JIM_OK
)
3635 Jim_MakeErrorMessage(teap
->interp
);
3636 command_print(NULL
,"%s\n", Jim_GetString(Jim_GetResult(teap
->interp
), NULL
));
3643 * Returns true only if the target has a handler for the specified event.
3645 bool target_has_event_action(struct target
*target
, enum target_event event
)
3647 struct target_event_action
*teap
;
3649 for (teap
= target
->event_action
; teap
!= NULL
; teap
= teap
->next
) {
3650 if (teap
->event
== event
)
3656 enum target_cfg_param
{
3659 TCFG_WORK_AREA_VIRT
,
3660 TCFG_WORK_AREA_PHYS
,
3661 TCFG_WORK_AREA_SIZE
,
3662 TCFG_WORK_AREA_BACKUP
,
3666 TCFG_CHAIN_POSITION
,
3669 static Jim_Nvp nvp_config_opts
[] = {
3670 { .name
= "-type", .value
= TCFG_TYPE
},
3671 { .name
= "-event", .value
= TCFG_EVENT
},
3672 { .name
= "-work-area-virt", .value
= TCFG_WORK_AREA_VIRT
},
3673 { .name
= "-work-area-phys", .value
= TCFG_WORK_AREA_PHYS
},
3674 { .name
= "-work-area-size", .value
= TCFG_WORK_AREA_SIZE
},
3675 { .name
= "-work-area-backup", .value
= TCFG_WORK_AREA_BACKUP
},
3676 { .name
= "-endian" , .value
= TCFG_ENDIAN
},
3677 { .name
= "-variant", .value
= TCFG_VARIANT
},
3678 { .name
= "-coreid", .value
= TCFG_COREID
},
3679 { .name
= "-chain-position", .value
= TCFG_CHAIN_POSITION
},
3681 { .name
= NULL
, .value
= -1 }
3684 static int target_configure(Jim_GetOptInfo
*goi
, struct target
*target
)
3692 /* parse config or cget options ... */
3693 while (goi
->argc
> 0) {
3694 Jim_SetEmptyResult(goi
->interp
);
3695 /* Jim_GetOpt_Debug(goi); */
3697 if (target
->type
->target_jim_configure
) {
3698 /* target defines a configure function */
3699 /* target gets first dibs on parameters */
3700 e
= (*(target
->type
->target_jim_configure
))(target
, goi
);
3709 /* otherwise we 'continue' below */
3711 e
= Jim_GetOpt_Nvp(goi
, nvp_config_opts
, &n
);
3713 Jim_GetOpt_NvpUnknown(goi
, nvp_config_opts
, 0);
3719 if (goi
->isconfigure
) {
3720 Jim_SetResultFormatted(goi
->interp
,
3721 "not settable: %s", n
->name
);
3725 if (goi
->argc
!= 0) {
3726 Jim_WrongNumArgs(goi
->interp
,
3727 goi
->argc
, goi
->argv
,
3732 Jim_SetResultString(goi
->interp
,
3733 target_type_name(target
), -1);
3737 if (goi
->argc
== 0) {
3738 Jim_WrongNumArgs(goi
->interp
, goi
->argc
, goi
->argv
, "-event ?event-name? ...");
3742 e
= Jim_GetOpt_Nvp(goi
, nvp_target_event
, &n
);
3744 Jim_GetOpt_NvpUnknown(goi
, nvp_target_event
, 1);
3748 if (goi
->isconfigure
) {
3749 if (goi
->argc
!= 1) {
3750 Jim_WrongNumArgs(goi
->interp
, goi
->argc
, goi
->argv
, "-event ?event-name? ?EVENT-BODY?");
3754 if (goi
->argc
!= 0) {
3755 Jim_WrongNumArgs(goi
->interp
, goi
->argc
, goi
->argv
, "-event ?event-name?");
3761 struct target_event_action
*teap
;
3763 teap
= target
->event_action
;
3764 /* replace existing? */
3766 if (teap
->event
== (enum target_event
)n
->value
) {
3772 if (goi
->isconfigure
) {
3773 bool replace
= true;
3776 teap
= calloc(1, sizeof(*teap
));
3779 teap
->event
= n
->value
;
3780 teap
->interp
= goi
->interp
;
3781 Jim_GetOpt_Obj(goi
, &o
);
3783 Jim_DecrRefCount(teap
->interp
, teap
->body
);
3785 teap
->body
= Jim_DuplicateObj(goi
->interp
, o
);
3788 * Tcl/TK - "tk events" have a nice feature.
3789 * See the "BIND" command.
3790 * We should support that here.
3791 * You can specify %X and %Y in the event code.
3792 * The idea is: %T - target name.
3793 * The idea is: %N - target number
3794 * The idea is: %E - event name.
3796 Jim_IncrRefCount(teap
->body
);
3800 /* add to head of event list */
3801 teap
->next
= target
->event_action
;
3802 target
->event_action
= teap
;
3804 Jim_SetEmptyResult(goi
->interp
);
3808 Jim_SetEmptyResult(goi
->interp
);
3810 Jim_SetResult(goi
->interp
, Jim_DuplicateObj(goi
->interp
, teap
->body
));
3817 case TCFG_WORK_AREA_VIRT
:
3818 if (goi
->isconfigure
) {
3819 target_free_all_working_areas(target
);
3820 e
= Jim_GetOpt_Wide(goi
, &w
);
3824 target
->working_area_virt
= w
;
3825 target
->working_area_virt_spec
= true;
3827 if (goi
->argc
!= 0) {
3831 Jim_SetResult(goi
->interp
, Jim_NewIntObj(goi
->interp
, target
->working_area_virt
));
3835 case TCFG_WORK_AREA_PHYS
:
3836 if (goi
->isconfigure
) {
3837 target_free_all_working_areas(target
);
3838 e
= Jim_GetOpt_Wide(goi
, &w
);
3842 target
->working_area_phys
= w
;
3843 target
->working_area_phys_spec
= true;
3845 if (goi
->argc
!= 0) {
3849 Jim_SetResult(goi
->interp
, Jim_NewIntObj(goi
->interp
, target
->working_area_phys
));
3853 case TCFG_WORK_AREA_SIZE
:
3854 if (goi
->isconfigure
) {
3855 target_free_all_working_areas(target
);
3856 e
= Jim_GetOpt_Wide(goi
, &w
);
3860 target
->working_area_size
= w
;
3862 if (goi
->argc
!= 0) {
3866 Jim_SetResult(goi
->interp
, Jim_NewIntObj(goi
->interp
, target
->working_area_size
));
3870 case TCFG_WORK_AREA_BACKUP
:
3871 if (goi
->isconfigure
) {
3872 target_free_all_working_areas(target
);
3873 e
= Jim_GetOpt_Wide(goi
, &w
);
3877 /* make this exactly 1 or 0 */
3878 target
->backup_working_area
= (!!w
);
3880 if (goi
->argc
!= 0) {
3884 Jim_SetResult(goi
->interp
, Jim_NewIntObj(goi
->interp
, target
->backup_working_area
));
3885 /* loop for more e*/
3889 if (goi
->isconfigure
) {
3890 e
= Jim_GetOpt_Nvp(goi
, nvp_target_endian
, &n
);
3892 Jim_GetOpt_NvpUnknown(goi
, nvp_target_endian
, 1);
3895 target
->endianness
= n
->value
;
3897 if (goi
->argc
!= 0) {
3901 n
= Jim_Nvp_value2name_simple(nvp_target_endian
, target
->endianness
);
3902 if (n
->name
== NULL
) {
3903 target
->endianness
= TARGET_LITTLE_ENDIAN
;
3904 n
= Jim_Nvp_value2name_simple(nvp_target_endian
, target
->endianness
);
3906 Jim_SetResultString(goi
->interp
, n
->name
, -1);
3911 if (goi
->isconfigure
) {
3912 if (goi
->argc
< 1) {
3913 Jim_SetResultFormatted(goi
->interp
,
3918 if (target
->variant
) {
3919 free((void *)(target
->variant
));
3921 e
= Jim_GetOpt_String(goi
, &cp
, NULL
);
3922 target
->variant
= strdup(cp
);
3924 if (goi
->argc
!= 0) {
3928 Jim_SetResultString(goi
->interp
, target
->variant
,-1);
3933 if (goi
->isconfigure
) {
3934 e
= Jim_GetOpt_Wide(goi
, &w
);
3938 target
->coreid
= (int)w
;
3940 if (goi
->argc
!= 0) {
3944 Jim_SetResult(goi
->interp
, Jim_NewIntObj(goi
->interp
, target
->working_area_size
));
3948 case TCFG_CHAIN_POSITION
:
3949 if (goi
->isconfigure
) {
3951 struct jtag_tap
*tap
;
3952 target_free_all_working_areas(target
);
3953 e
= Jim_GetOpt_Obj(goi
, &o_t
);
3957 tap
= jtag_tap_by_jim_obj(goi
->interp
, o_t
);
3961 /* make this exactly 1 or 0 */
3964 if (goi
->argc
!= 0) {
3968 Jim_SetResultString(goi
->interp
, target
->tap
->dotted_name
, -1);
3969 /* loop for more e*/
3972 } /* while (goi->argc) */
3975 /* done - we return */
3980 jim_target_configure(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
3984 Jim_GetOpt_Setup(&goi
, interp
, argc
- 1, argv
+ 1);
3985 goi
.isconfigure
= !strcmp(Jim_GetString(argv
[0], NULL
), "configure");
3986 int need_args
= 1 + goi
.isconfigure
;
3987 if (goi
.argc
< need_args
)
3989 Jim_WrongNumArgs(goi
.interp
, goi
.argc
, goi
.argv
,
3991 ? "missing: -option VALUE ..."
3992 : "missing: -option ...");
3995 struct target
*target
= Jim_CmdPrivData(goi
.interp
);
3996 return target_configure(&goi
, target
);
3999 static int jim_target_mw(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4001 const char *cmd_name
= Jim_GetString(argv
[0], NULL
);
4004 Jim_GetOpt_Setup(&goi
, interp
, argc
- 1, argv
+ 1);
4006 if (goi
.argc
< 2 || goi
.argc
> 4)
4008 Jim_SetResultFormatted(goi
.interp
,
4009 "usage: %s [phys] <address> <data> [<count>]", cmd_name
);
4014 fn
= target_write_memory_fast
;
4017 if (strcmp(Jim_GetString(argv
[1], NULL
), "phys") == 0)
4020 struct Jim_Obj
*obj
;
4021 e
= Jim_GetOpt_Obj(&goi
, &obj
);
4025 fn
= target_write_phys_memory
;
4029 e
= Jim_GetOpt_Wide(&goi
, &a
);
4034 e
= Jim_GetOpt_Wide(&goi
, &b
);
4041 e
= Jim_GetOpt_Wide(&goi
, &c
);
4046 /* all args must be consumed */
4052 struct target
*target
= Jim_CmdPrivData(goi
.interp
);
4054 if (strcasecmp(cmd_name
, "mww") == 0) {
4057 else if (strcasecmp(cmd_name
, "mwh") == 0) {
4060 else if (strcasecmp(cmd_name
, "mwb") == 0) {
4063 LOG_ERROR("command '%s' unknown: ", cmd_name
);
4067 return (target_fill_mem(target
, a
, fn
, data_size
, b
, c
) == ERROR_OK
) ? JIM_OK
: JIM_ERR
;
4070 static int jim_target_md(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4072 const char *cmd_name
= Jim_GetString(argv
[0], NULL
);
4075 Jim_GetOpt_Setup(&goi
, interp
, argc
- 1, argv
+ 1);
4077 if ((goi
.argc
< 1) || (goi
.argc
> 3))
4079 Jim_SetResultFormatted(goi
.interp
,
4080 "usage: %s [phys] <address> [<count>]", cmd_name
);
4084 int (*fn
)(struct target
*target
,
4085 uint32_t address
, uint32_t size
, uint32_t count
, uint8_t *buffer
);
4086 fn
=target_read_memory
;
4089 if (strcmp(Jim_GetString(argv
[1], NULL
), "phys") == 0)
4092 struct Jim_Obj
*obj
;
4093 e
= Jim_GetOpt_Obj(&goi
, &obj
);
4097 fn
=target_read_phys_memory
;
4101 e
= Jim_GetOpt_Wide(&goi
, &a
);
4106 if (goi
.argc
== 1) {
4107 e
= Jim_GetOpt_Wide(&goi
, &c
);
4115 /* all args must be consumed */
4121 jim_wide b
= 1; /* shut up gcc */
4122 if (strcasecmp(cmd_name
, "mdw") == 0)
4124 else if (strcasecmp(cmd_name
, "mdh") == 0)
4126 else if (strcasecmp(cmd_name
, "mdb") == 0)
4129 LOG_ERROR("command '%s' unknown: ", cmd_name
);
4133 /* convert count to "bytes" */
4136 struct target
*target
= Jim_CmdPrivData(goi
.interp
);
4137 uint8_t target_buf
[32];
4144 e
= fn(target
, a
, b
, y
/ b
, target_buf
);
4145 if (e
!= ERROR_OK
) {
4147 snprintf(tmp
, sizeof(tmp
), "%08lx", (long)a
);
4148 Jim_SetResultFormatted(interp
, "error reading target @ 0x%s", tmp
);
4152 command_print(NULL
, "0x%08x ", (int)(a
));
4155 for (x
= 0; x
< 16 && x
< y
; x
+= 4)
4157 z
= target_buffer_get_u32(target
, &(target_buf
[ x
]));
4158 command_print(NULL
, "%08x ", (int)(z
));
4160 for (; (x
< 16) ; x
+= 4) {
4161 command_print(NULL
, " ");
4165 for (x
= 0; x
< 16 && x
< y
; x
+= 2)
4167 z
= target_buffer_get_u16(target
, &(target_buf
[ x
]));
4168 command_print(NULL
, "%04x ", (int)(z
));
4170 for (; (x
< 16) ; x
+= 2) {
4171 command_print(NULL
, " ");
4176 for (x
= 0 ; (x
< 16) && (x
< y
) ; x
+= 1) {
4177 z
= target_buffer_get_u8(target
, &(target_buf
[ x
]));
4178 command_print(NULL
, "%02x ", (int)(z
));
4180 for (; (x
< 16) ; x
+= 1) {
4181 command_print(NULL
, " ");
4185 /* ascii-ify the bytes */
4186 for (x
= 0 ; x
< y
; x
++) {
4187 if ((target_buf
[x
] >= 0x20) &&
4188 (target_buf
[x
] <= 0x7e)) {
4192 target_buf
[x
] = '.';
4197 target_buf
[x
] = ' ';
4202 /* print - with a newline */
4203 command_print(NULL
, "%s\n", target_buf
);
4211 static int jim_target_mem2array(Jim_Interp
*interp
,
4212 int argc
, Jim_Obj
*const *argv
)
4214 struct target
*target
= Jim_CmdPrivData(interp
);
4215 return target_mem2array(interp
, target
, argc
- 1, argv
+ 1);
4218 static int jim_target_array2mem(Jim_Interp
*interp
,
4219 int argc
, Jim_Obj
*const *argv
)
4221 struct target
*target
= Jim_CmdPrivData(interp
);
4222 return target_array2mem(interp
, target
, argc
- 1, argv
+ 1);
4225 static int jim_target_tap_disabled(Jim_Interp
*interp
)
4227 Jim_SetResultFormatted(interp
, "[TAP is disabled]");
4231 static int jim_target_examine(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4235 Jim_WrongNumArgs(interp
, 1, argv
, "[no parameters]");
4238 struct target
*target
= Jim_CmdPrivData(interp
);
4239 if (!target
->tap
->enabled
)
4240 return jim_target_tap_disabled(interp
);
4242 int e
= target
->type
->examine(target
);
4250 static int jim_target_halt_gdb(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4254 Jim_WrongNumArgs(interp
, 1, argv
, "[no parameters]");
4257 struct target
*target
= Jim_CmdPrivData(interp
);
4259 if (target_call_event_callbacks(target
, TARGET_EVENT_GDB_HALT
) != ERROR_OK
)
4265 static int jim_target_poll(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4269 Jim_WrongNumArgs(interp
, 1, argv
, "[no parameters]");
4272 struct target
*target
= Jim_CmdPrivData(interp
);
4273 if (!target
->tap
->enabled
)
4274 return jim_target_tap_disabled(interp
);
4277 if (!(target_was_examined(target
))) {
4278 e
= ERROR_TARGET_NOT_EXAMINED
;
4280 e
= target
->type
->poll(target
);
4289 static int jim_target_reset(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4292 Jim_GetOpt_Setup(&goi
, interp
, argc
- 1, argv
+ 1);
4296 Jim_WrongNumArgs(interp
, 0, argv
,
4297 "([tT]|[fF]|assert|deassert) BOOL");
4302 int e
= Jim_GetOpt_Nvp(&goi
, nvp_assert
, &n
);
4305 Jim_GetOpt_NvpUnknown(&goi
, nvp_assert
, 1);
4308 /* the halt or not param */
4310 e
= Jim_GetOpt_Wide(&goi
, &a
);
4314 struct target
*target
= Jim_CmdPrivData(goi
.interp
);
4315 if (!target
->tap
->enabled
)
4316 return jim_target_tap_disabled(interp
);
4317 if (!(target_was_examined(target
)))
4319 LOG_ERROR("Target not examined yet");
4320 return ERROR_TARGET_NOT_EXAMINED
;
4322 if (!target
->type
->assert_reset
|| !target
->type
->deassert_reset
)
4324 Jim_SetResultFormatted(interp
,
4325 "No target-specific reset for %s",
4326 target_name(target
));
4329 /* determine if we should halt or not. */
4330 target
->reset_halt
= !!a
;
4331 /* When this happens - all workareas are invalid. */
4332 target_free_all_working_areas_restore(target
, 0);
4335 if (n
->value
== NVP_ASSERT
) {
4336 e
= target
->type
->assert_reset(target
);
4338 e
= target
->type
->deassert_reset(target
);
4340 return (e
== ERROR_OK
) ? JIM_OK
: JIM_ERR
;
4343 static int jim_target_halt(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4346 Jim_WrongNumArgs(interp
, 1, argv
, "[no parameters]");
4349 struct target
*target
= Jim_CmdPrivData(interp
);
4350 if (!target
->tap
->enabled
)
4351 return jim_target_tap_disabled(interp
);
4352 int e
= target
->type
->halt(target
);
4353 return (e
== ERROR_OK
) ? JIM_OK
: JIM_ERR
;
4356 static int jim_target_wait_state(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4359 Jim_GetOpt_Setup(&goi
, interp
, argc
- 1, argv
+ 1);
4361 /* params: <name> statename timeoutmsecs */
4364 const char *cmd_name
= Jim_GetString(argv
[0], NULL
);
4365 Jim_SetResultFormatted(goi
.interp
,
4366 "%s <state_name> <timeout_in_msec>", cmd_name
);
4371 int e
= Jim_GetOpt_Nvp(&goi
, nvp_target_state
, &n
);
4373 Jim_GetOpt_NvpUnknown(&goi
, nvp_target_state
,1);
4377 e
= Jim_GetOpt_Wide(&goi
, &a
);
4381 struct target
*target
= Jim_CmdPrivData(interp
);
4382 if (!target
->tap
->enabled
)
4383 return jim_target_tap_disabled(interp
);
4385 e
= target_wait_state(target
, n
->value
, a
);
4388 Jim_Obj
*eObj
= Jim_NewIntObj(interp
, e
);
4389 Jim_SetResultFormatted(goi
.interp
,
4390 "target: %s wait %s fails (%#s) %s",
4391 target_name(target
), n
->name
,
4392 eObj
, target_strerror_safe(e
));
4393 Jim_FreeNewObj(interp
, eObj
);
4398 /* List for human, Events defined for this target.
4399 * scripts/programs should use 'name cget -event NAME'
4401 static int jim_target_event_list(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4403 struct command_context
*cmd_ctx
= current_command_context(interp
);
4404 assert (cmd_ctx
!= NULL
);
4406 struct target
*target
= Jim_CmdPrivData(interp
);
4407 struct target_event_action
*teap
= target
->event_action
;
4408 command_print(cmd_ctx
, "Event actions for target (%d) %s\n",
4409 target
->target_number
,
4410 target_name(target
));
4411 command_print(cmd_ctx
, "%-25s | Body", "Event");
4412 command_print(cmd_ctx
, "------------------------- | "
4413 "----------------------------------------");
4416 Jim_Nvp
*opt
= Jim_Nvp_value2name_simple(nvp_target_event
, teap
->event
);
4417 command_print(cmd_ctx
, "%-25s | %s",
4418 opt
->name
, Jim_GetString(teap
->body
, NULL
));
4421 command_print(cmd_ctx
, "***END***");
4424 static int jim_target_current_state(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4428 Jim_WrongNumArgs(interp
, 1, argv
, "[no parameters]");
4431 struct target
*target
= Jim_CmdPrivData(interp
);
4432 Jim_SetResultString(interp
, target_state_name(target
), -1);
4435 static int jim_target_invoke_event(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4438 Jim_GetOpt_Setup(&goi
, interp
, argc
- 1, argv
+ 1);
4441 const char *cmd_name
= Jim_GetString(argv
[0], NULL
);
4442 Jim_SetResultFormatted(goi
.interp
, "%s <eventname>", cmd_name
);
4446 int e
= Jim_GetOpt_Nvp(&goi
, nvp_target_event
, &n
);
4449 Jim_GetOpt_NvpUnknown(&goi
, nvp_target_event
, 1);
4452 struct target
*target
= Jim_CmdPrivData(interp
);
4453 target_handle_event(target
, n
->value
);
4457 static const struct command_registration target_instance_command_handlers
[] = {
4459 .name
= "configure",
4460 .mode
= COMMAND_CONFIG
,
4461 .jim_handler
= jim_target_configure
,
4462 .help
= "configure a new target for use",
4463 .usage
= "[target_attribute ...]",
4467 .mode
= COMMAND_ANY
,
4468 .jim_handler
= jim_target_configure
,
4469 .help
= "returns the specified target attribute",
4470 .usage
= "target_attribute",
4474 .mode
= COMMAND_EXEC
,
4475 .jim_handler
= jim_target_mw
,
4476 .help
= "Write 32-bit word(s) to target memory",
4477 .usage
= "address data [count]",
4481 .mode
= COMMAND_EXEC
,
4482 .jim_handler
= jim_target_mw
,
4483 .help
= "Write 16-bit half-word(s) to target memory",
4484 .usage
= "address data [count]",
4488 .mode
= COMMAND_EXEC
,
4489 .jim_handler
= jim_target_mw
,
4490 .help
= "Write byte(s) to target memory",
4491 .usage
= "address data [count]",
4495 .mode
= COMMAND_EXEC
,
4496 .jim_handler
= jim_target_md
,
4497 .help
= "Display target memory as 32-bit words",
4498 .usage
= "address [count]",
4502 .mode
= COMMAND_EXEC
,
4503 .jim_handler
= jim_target_md
,
4504 .help
= "Display target memory as 16-bit half-words",
4505 .usage
= "address [count]",
4509 .mode
= COMMAND_EXEC
,
4510 .jim_handler
= jim_target_md
,
4511 .help
= "Display target memory as 8-bit bytes",
4512 .usage
= "address [count]",
4515 .name
= "array2mem",
4516 .mode
= COMMAND_EXEC
,
4517 .jim_handler
= jim_target_array2mem
,
4518 .help
= "Writes Tcl array of 8/16/32 bit numbers "
4520 .usage
= "arrayname bitwidth address count",
4523 .name
= "mem2array",
4524 .mode
= COMMAND_EXEC
,
4525 .jim_handler
= jim_target_mem2array
,
4526 .help
= "Loads Tcl array of 8/16/32 bit numbers "
4527 "from target memory",
4528 .usage
= "arrayname bitwidth address count",
4531 .name
= "eventlist",
4532 .mode
= COMMAND_EXEC
,
4533 .jim_handler
= jim_target_event_list
,
4534 .help
= "displays a table of events defined for this target",
4538 .mode
= COMMAND_EXEC
,
4539 .jim_handler
= jim_target_current_state
,
4540 .help
= "displays the current state of this target",
4543 .name
= "arp_examine",
4544 .mode
= COMMAND_EXEC
,
4545 .jim_handler
= jim_target_examine
,
4546 .help
= "used internally for reset processing",
4549 .name
= "arp_halt_gdb",
4550 .mode
= COMMAND_EXEC
,
4551 .jim_handler
= jim_target_halt_gdb
,
4552 .help
= "used internally for reset processing to halt GDB",
4556 .mode
= COMMAND_EXEC
,
4557 .jim_handler
= jim_target_poll
,
4558 .help
= "used internally for reset processing",
4561 .name
= "arp_reset",
4562 .mode
= COMMAND_EXEC
,
4563 .jim_handler
= jim_target_reset
,
4564 .help
= "used internally for reset processing",
4568 .mode
= COMMAND_EXEC
,
4569 .jim_handler
= jim_target_halt
,
4570 .help
= "used internally for reset processing",
4573 .name
= "arp_waitstate",
4574 .mode
= COMMAND_EXEC
,
4575 .jim_handler
= jim_target_wait_state
,
4576 .help
= "used internally for reset processing",
4579 .name
= "invoke-event",
4580 .mode
= COMMAND_EXEC
,
4581 .jim_handler
= jim_target_invoke_event
,
4582 .help
= "invoke handler for specified event",
4583 .usage
= "event_name",
4585 COMMAND_REGISTRATION_DONE
4588 static int target_create(Jim_GetOptInfo
*goi
)
4596 struct target
*target
;
4597 struct command_context
*cmd_ctx
;
4599 cmd_ctx
= current_command_context(goi
->interp
);
4600 assert (cmd_ctx
!= NULL
);
4602 if (goi
->argc
< 3) {
4603 Jim_WrongNumArgs(goi
->interp
, 1, goi
->argv
, "?name? ?type? ..options...");
4608 Jim_GetOpt_Obj(goi
, &new_cmd
);
4609 /* does this command exist? */
4610 cmd
= Jim_GetCommand(goi
->interp
, new_cmd
, JIM_ERRMSG
);
4612 cp
= Jim_GetString(new_cmd
, NULL
);
4613 Jim_SetResultFormatted(goi
->interp
, "Command/target: %s Exists", cp
);
4618 e
= Jim_GetOpt_String(goi
, &cp2
, NULL
);
4620 /* now does target type exist */
4621 for (x
= 0 ; target_types
[x
] ; x
++) {
4622 if (0 == strcmp(cp
, target_types
[x
]->name
)) {
4627 if (target_types
[x
] == NULL
) {
4628 Jim_SetResultFormatted(goi
->interp
, "Unknown target type %s, try one of ", cp
);
4629 for (x
= 0 ; target_types
[x
] ; x
++) {
4630 if (target_types
[x
+ 1]) {
4631 Jim_AppendStrings(goi
->interp
,
4632 Jim_GetResult(goi
->interp
),
4633 target_types
[x
]->name
,
4636 Jim_AppendStrings(goi
->interp
,
4637 Jim_GetResult(goi
->interp
),
4639 target_types
[x
]->name
,NULL
);
4646 target
= calloc(1,sizeof(struct target
));
4647 /* set target number */
4648 target
->target_number
= new_target_number();
4650 /* allocate memory for each unique target type */
4651 target
->type
= (struct target_type
*)calloc(1,sizeof(struct target_type
));
4653 memcpy(target
->type
, target_types
[x
], sizeof(struct target_type
));
4655 /* will be set by "-endian" */
4656 target
->endianness
= TARGET_ENDIAN_UNKNOWN
;
4658 /* default to first core, override with -coreid */
4661 target
->working_area
= 0x0;
4662 target
->working_area_size
= 0x0;
4663 target
->working_areas
= NULL
;
4664 target
->backup_working_area
= 0;
4666 target
->state
= TARGET_UNKNOWN
;
4667 target
->debug_reason
= DBG_REASON_UNDEFINED
;
4668 target
->reg_cache
= NULL
;
4669 target
->breakpoints
= NULL
;
4670 target
->watchpoints
= NULL
;
4671 target
->next
= NULL
;
4672 target
->arch_info
= NULL
;
4674 target
->display
= 1;
4676 target
->halt_issued
= false;
4678 /* initialize trace information */
4679 target
->trace_info
= malloc(sizeof(struct trace
));
4680 target
->trace_info
->num_trace_points
= 0;
4681 target
->trace_info
->trace_points_size
= 0;
4682 target
->trace_info
->trace_points
= NULL
;
4683 target
->trace_info
->trace_history_size
= 0;
4684 target
->trace_info
->trace_history
= NULL
;
4685 target
->trace_info
->trace_history_pos
= 0;
4686 target
->trace_info
->trace_history_overflowed
= 0;
4688 target
->dbgmsg
= NULL
;
4689 target
->dbg_msg_enabled
= 0;
4691 target
->endianness
= TARGET_ENDIAN_UNKNOWN
;
4693 /* Do the rest as "configure" options */
4694 goi
->isconfigure
= 1;
4695 e
= target_configure(goi
, target
);
4697 if (target
->tap
== NULL
)
4699 Jim_SetResultString(goi
->interp
, "-chain-position required when creating target", -1);
4709 if (target
->endianness
== TARGET_ENDIAN_UNKNOWN
) {
4710 /* default endian to little if not specified */
4711 target
->endianness
= TARGET_LITTLE_ENDIAN
;
4714 /* incase variant is not set */
4715 if (!target
->variant
)
4716 target
->variant
= strdup("");
4718 cp
= Jim_GetString(new_cmd
, NULL
);
4719 target
->cmd_name
= strdup(cp
);
4721 /* create the target specific commands */
4722 if (target
->type
->commands
) {
4723 e
= register_commands(cmd_ctx
, NULL
, target
->type
->commands
);
4725 LOG_ERROR("unable to register '%s' commands", cp
);
4727 if (target
->type
->target_create
) {
4728 (*(target
->type
->target_create
))(target
, goi
->interp
);
4731 /* append to end of list */
4733 struct target
**tpp
;
4734 tpp
= &(all_targets
);
4736 tpp
= &((*tpp
)->next
);
4741 /* now - create the new target name command */
4742 const const struct command_registration target_subcommands
[] = {
4744 .chain
= target_instance_command_handlers
,
4747 .chain
= target
->type
->commands
,
4749 COMMAND_REGISTRATION_DONE
4751 const const struct command_registration target_commands
[] = {
4754 .mode
= COMMAND_ANY
,
4755 .help
= "target command group",
4756 .chain
= target_subcommands
,
4758 COMMAND_REGISTRATION_DONE
4760 e
= register_commands(cmd_ctx
, NULL
, target_commands
);
4764 struct command
*c
= command_find_in_context(cmd_ctx
, cp
);
4766 command_set_handler_data(c
, target
);
4768 return (ERROR_OK
== e
) ? JIM_OK
: JIM_ERR
;
4771 static int jim_target_current(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4775 Jim_WrongNumArgs(interp
, 1, argv
, "Too many parameters");
4778 struct command_context
*cmd_ctx
= current_command_context(interp
);
4779 assert (cmd_ctx
!= NULL
);
4781 Jim_SetResultString(interp
, get_current_target(cmd_ctx
)->cmd_name
, -1);
4785 static int jim_target_types(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4789 Jim_WrongNumArgs(interp
, 1, argv
, "Too many parameters");
4792 Jim_SetResult(interp
, Jim_NewListObj(interp
, NULL
, 0));
4793 for (unsigned x
= 0; NULL
!= target_types
[x
]; x
++)
4795 Jim_ListAppendElement(interp
, Jim_GetResult(interp
),
4796 Jim_NewStringObj(interp
, target_types
[x
]->name
, -1));
4801 static int jim_target_names(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4805 Jim_WrongNumArgs(interp
, 1, argv
, "Too many parameters");
4808 Jim_SetResult(interp
, Jim_NewListObj(interp
, NULL
, 0));
4809 struct target
*target
= all_targets
;
4812 Jim_ListAppendElement(interp
, Jim_GetResult(interp
),
4813 Jim_NewStringObj(interp
, target_name(target
), -1));
4814 target
= target
->next
;
4819 static int jim_target_create(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4822 Jim_GetOpt_Setup(&goi
, interp
, argc
- 1, argv
+ 1);
4825 Jim_WrongNumArgs(goi
.interp
, goi
.argc
, goi
.argv
,
4826 "<name> <target_type> [<target_options> ...]");
4829 return target_create(&goi
);
4832 static int jim_target_number(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4835 Jim_GetOpt_Setup(&goi
, interp
, argc
- 1, argv
+ 1);
4837 /* It's OK to remove this mechanism sometime after August 2010 or so */
4838 LOG_WARNING("don't use numbers as target identifiers; use names");
4841 Jim_SetResultFormatted(goi
.interp
, "usage: target number <number>");
4845 int e
= Jim_GetOpt_Wide(&goi
, &w
);
4849 struct target
*target
;
4850 for (target
= all_targets
; NULL
!= target
; target
= target
->next
)
4852 if (target
->target_number
!= w
)
4855 Jim_SetResultString(goi
.interp
, target_name(target
), -1);
4859 Jim_Obj
*wObj
= Jim_NewIntObj(goi
.interp
, w
);
4860 Jim_SetResultFormatted(goi
.interp
,
4861 "Target: number %#s does not exist", wObj
);
4862 Jim_FreeNewObj(interp
, wObj
);
4867 static int jim_target_count(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4871 Jim_WrongNumArgs(interp
, 1, argv
, "<no parameters>");
4875 struct target
*target
= all_targets
;
4876 while (NULL
!= target
)
4878 target
= target
->next
;
4881 Jim_SetResult(interp
, Jim_NewIntObj(interp
, count
));
4885 static const struct command_registration target_subcommand_handlers
[] = {
4888 .mode
= COMMAND_CONFIG
,
4889 .handler
= handle_target_init_command
,
4890 .help
= "initialize targets",
4894 /* REVISIT this should be COMMAND_CONFIG ... */
4895 .mode
= COMMAND_ANY
,
4896 .jim_handler
= jim_target_create
,
4897 .usage
= "name type '-chain-position' name [options ...]",
4898 .help
= "Creates and selects a new target",
4902 .mode
= COMMAND_ANY
,
4903 .jim_handler
= jim_target_current
,
4904 .help
= "Returns the currently selected target",
4908 .mode
= COMMAND_ANY
,
4909 .jim_handler
= jim_target_types
,
4910 .help
= "Returns the available target types as "
4911 "a list of strings",
4915 .mode
= COMMAND_ANY
,
4916 .jim_handler
= jim_target_names
,
4917 .help
= "Returns the names of all targets as a list of strings",
4921 .mode
= COMMAND_ANY
,
4922 .jim_handler
= jim_target_number
,
4924 .help
= "Returns the name of the numbered target "
4929 .mode
= COMMAND_ANY
,
4930 .jim_handler
= jim_target_count
,
4931 .help
= "Returns the number of targets as an integer "
4934 COMMAND_REGISTRATION_DONE
4945 static int fastload_num
;
4946 static struct FastLoad
*fastload
;
4948 static void free_fastload(void)
4950 if (fastload
!= NULL
)
4953 for (i
= 0; i
< fastload_num
; i
++)
4955 if (fastload
[i
].data
)
4956 free(fastload
[i
].data
);
4966 COMMAND_HANDLER(handle_fast_load_image_command
)
4970 uint32_t image_size
;
4971 uint32_t min_address
= 0;
4972 uint32_t max_address
= 0xffffffff;
4977 int retval
= CALL_COMMAND_HANDLER(parse_load_image_command_CMD_ARGV
,
4978 &image
, &min_address
, &max_address
);
4979 if (ERROR_OK
!= retval
)
4982 struct duration bench
;
4983 duration_start(&bench
);
4985 retval
= image_open(&image
, CMD_ARGV
[0], (CMD_ARGC
>= 3) ? CMD_ARGV
[2] : NULL
);
4986 if (retval
!= ERROR_OK
)
4993 fastload_num
= image
.num_sections
;
4994 fastload
= (struct FastLoad
*)malloc(sizeof(struct FastLoad
)*image
.num_sections
);
4995 if (fastload
== NULL
)
4997 command_print(CMD_CTX
, "out of memory");
4998 image_close(&image
);
5001 memset(fastload
, 0, sizeof(struct FastLoad
)*image
.num_sections
);
5002 for (i
= 0; i
< image
.num_sections
; i
++)
5004 buffer
= malloc(image
.sections
[i
].size
);
5007 command_print(CMD_CTX
, "error allocating buffer for section (%d bytes)",
5008 (int)(image
.sections
[i
].size
));
5009 retval
= ERROR_FAIL
;
5013 if ((retval
= image_read_section(&image
, i
, 0x0, image
.sections
[i
].size
, buffer
, &buf_cnt
)) != ERROR_OK
)
5019 uint32_t offset
= 0;
5020 uint32_t length
= buf_cnt
;
5023 /* DANGER!!! beware of unsigned comparision here!!! */
5025 if ((image
.sections
[i
].base_address
+ buf_cnt
>= min_address
)&&
5026 (image
.sections
[i
].base_address
< max_address
))
5028 if (image
.sections
[i
].base_address
< min_address
)
5030 /* clip addresses below */
5031 offset
+= min_address
-image
.sections
[i
].base_address
;
5035 if (image
.sections
[i
].base_address
+ buf_cnt
> max_address
)
5037 length
-= (image
.sections
[i
].base_address
+ buf_cnt
)-max_address
;
5040 fastload
[i
].address
= image
.sections
[i
].base_address
+ offset
;
5041 fastload
[i
].data
= malloc(length
);
5042 if (fastload
[i
].data
== NULL
)
5045 command_print(CMD_CTX
, "error allocating buffer for section (%d bytes)",
5047 retval
= ERROR_FAIL
;
5050 memcpy(fastload
[i
].data
, buffer
+ offset
, length
);
5051 fastload
[i
].length
= length
;
5053 image_size
+= length
;
5054 command_print(CMD_CTX
, "%u bytes written at address 0x%8.8x",
5055 (unsigned int)length
,
5056 ((unsigned int)(image
.sections
[i
].base_address
+ offset
)));
5062 if ((ERROR_OK
== retval
) && (duration_measure(&bench
) == ERROR_OK
))
5064 command_print(CMD_CTX
, "Loaded %" PRIu32
" bytes "
5065 "in %fs (%0.3f KiB/s)", image_size
,
5066 duration_elapsed(&bench
), duration_kbps(&bench
, image_size
));
5068 command_print(CMD_CTX
,
5069 "WARNING: image has not been loaded to target!"
5070 "You can issue a 'fast_load' to finish loading.");
5073 image_close(&image
);
5075 if (retval
!= ERROR_OK
)
5083 COMMAND_HANDLER(handle_fast_load_command
)
5086 return ERROR_COMMAND_SYNTAX_ERROR
;
5087 if (fastload
== NULL
)
5089 LOG_ERROR("No image in memory");
5093 int ms
= timeval_ms();
5095 int retval
= ERROR_OK
;
5096 for (i
= 0; i
< fastload_num
;i
++)
5098 struct target
*target
= get_current_target(CMD_CTX
);
5099 command_print(CMD_CTX
, "Write to 0x%08x, length 0x%08x",
5100 (unsigned int)(fastload
[i
].address
),
5101 (unsigned int)(fastload
[i
].length
));
5102 retval
= target_write_buffer(target
, fastload
[i
].address
, fastload
[i
].length
, fastload
[i
].data
);
5103 if (retval
!= ERROR_OK
)
5107 size
+= fastload
[i
].length
;
5109 if (retval
== ERROR_OK
)
5111 int after
= timeval_ms();
5112 command_print(CMD_CTX
, "Loaded image %f kBytes/s", (float)(size
/1024.0)/((float)(after
-ms
)/1000.0));
5117 static const struct command_registration target_command_handlers
[] = {
5120 .handler
= handle_targets_command
,
5121 .mode
= COMMAND_ANY
,
5122 .help
= "change current default target (one parameter) "
5123 "or prints table of all targets (no parameters)",
5124 .usage
= "[target]",
5128 .mode
= COMMAND_CONFIG
,
5129 .help
= "configure target",
5131 .chain
= target_subcommand_handlers
,
5133 COMMAND_REGISTRATION_DONE
5136 int target_register_commands(struct command_context
*cmd_ctx
)
5138 return register_commands(cmd_ctx
, NULL
, target_command_handlers
);
5141 static bool target_reset_nag
= true;
5143 bool get_target_reset_nag(void)
5145 return target_reset_nag
;
5148 COMMAND_HANDLER(handle_target_reset_nag
)
5150 return CALL_COMMAND_HANDLER(handle_command_parse_bool
,
5151 &target_reset_nag
, "Nag after each reset about options to improve "
5155 static const struct command_registration target_exec_command_handlers
[] = {
5157 .name
= "fast_load_image",
5158 .handler
= handle_fast_load_image_command
,
5159 .mode
= COMMAND_ANY
,
5160 .help
= "Load image into server memory for later use by "
5161 "fast_load; primarily for profiling",
5162 .usage
= "filename address ['bin'|'ihex'|'elf'|'s19'] "
5163 "[min_address [max_length]]",
5166 .name
= "fast_load",
5167 .handler
= handle_fast_load_command
,
5168 .mode
= COMMAND_EXEC
,
5169 .help
= "loads active fast load image to current target "
5170 "- mainly for profiling purposes",
5174 .handler
= handle_profile_command
,
5175 .mode
= COMMAND_EXEC
,
5176 .help
= "profiling samples the CPU PC",
5178 /** @todo don't register virt2phys() unless target supports it */
5180 .name
= "virt2phys",
5181 .handler
= handle_virt2phys_command
,
5182 .mode
= COMMAND_ANY
,
5183 .help
= "translate a virtual address into a physical address",
5184 .usage
= "virtual_address",
5188 .handler
= handle_reg_command
,
5189 .mode
= COMMAND_EXEC
,
5190 .help
= "display or set a register; with no arguments, "
5191 "displays all registers and their values",
5192 .usage
= "[(register_name|register_number) [value]]",
5196 .handler
= handle_poll_command
,
5197 .mode
= COMMAND_EXEC
,
5198 .help
= "poll target state; or reconfigure background polling",
5199 .usage
= "['on'|'off']",
5202 .name
= "wait_halt",
5203 .handler
= handle_wait_halt_command
,
5204 .mode
= COMMAND_EXEC
,
5205 .help
= "wait up to the specified number of milliseconds "
5206 "(default 5) for a previously requested halt",
5207 .usage
= "[milliseconds]",
5211 .handler
= handle_halt_command
,
5212 .mode
= COMMAND_EXEC
,
5213 .help
= "request target to halt, then wait up to the specified"
5214 "number of milliseconds (default 5) for it to complete",
5215 .usage
= "[milliseconds]",
5219 .handler
= handle_resume_command
,
5220 .mode
= COMMAND_EXEC
,
5221 .help
= "resume target execution from current PC or address",
5222 .usage
= "[address]",
5226 .handler
= handle_reset_command
,
5227 .mode
= COMMAND_EXEC
,
5228 .usage
= "[run|halt|init]",
5229 .help
= "Reset all targets into the specified mode."
5230 "Default reset mode is run, if not given.",
5233 .name
= "soft_reset_halt",
5234 .handler
= handle_soft_reset_halt_command
,
5235 .mode
= COMMAND_EXEC
,
5236 .help
= "halt the target and do a soft reset",
5240 .handler
= handle_step_command
,
5241 .mode
= COMMAND_EXEC
,
5242 .help
= "step one instruction from current PC or address",
5243 .usage
= "[address]",
5247 .handler
= handle_md_command
,
5248 .mode
= COMMAND_EXEC
,
5249 .help
= "display memory words",
5250 .usage
= "['phys'] address [count]",
5254 .handler
= handle_md_command
,
5255 .mode
= COMMAND_EXEC
,
5256 .help
= "display memory half-words",
5257 .usage
= "['phys'] address [count]",
5261 .handler
= handle_md_command
,
5262 .mode
= COMMAND_EXEC
,
5263 .help
= "display memory bytes",
5264 .usage
= "['phys'] address [count]",
5268 .handler
= handle_mw_command
,
5269 .mode
= COMMAND_EXEC
,
5270 .help
= "write memory word",
5271 .usage
= "['phys'] address value [count]",
5275 .handler
= handle_mw_command
,
5276 .mode
= COMMAND_EXEC
,
5277 .help
= "write memory half-word",
5278 .usage
= "['phys'] address value [count]",
5282 .handler
= handle_mw_command
,
5283 .mode
= COMMAND_EXEC
,
5284 .help
= "write memory byte",
5285 .usage
= "['phys'] address value [count]",
5289 .handler
= handle_bp_command
,
5290 .mode
= COMMAND_EXEC
,
5291 .help
= "list or set hardware or software breakpoint",
5292 .usage
= "[address length ['hw']]",
5296 .handler
= handle_rbp_command
,
5297 .mode
= COMMAND_EXEC
,
5298 .help
= "remove breakpoint",
5303 .handler
= handle_wp_command
,
5304 .mode
= COMMAND_EXEC
,
5305 .help
= "list (no params) or create watchpoints",
5306 .usage
= "[address length [('r'|'w'|'a') value [mask]]]",
5310 .handler
= handle_rwp_command
,
5311 .mode
= COMMAND_EXEC
,
5312 .help
= "remove watchpoint",
5316 .name
= "load_image",
5317 .handler
= handle_load_image_command
,
5318 .mode
= COMMAND_EXEC
,
5319 .usage
= "filename address ['bin'|'ihex'|'elf'|'s19'] "
5320 "[min_address] [max_length]",
5323 .name
= "dump_image",
5324 .handler
= handle_dump_image_command
,
5325 .mode
= COMMAND_EXEC
,
5326 .usage
= "filename address size",
5329 .name
= "verify_image",
5330 .handler
= handle_verify_image_command
,
5331 .mode
= COMMAND_EXEC
,
5332 .usage
= "filename [offset [type]]",
5335 .name
= "test_image",
5336 .handler
= handle_test_image_command
,
5337 .mode
= COMMAND_EXEC
,
5338 .usage
= "filename [offset [type]]",
5341 .name
= "mem2array",
5342 .mode
= COMMAND_EXEC
,
5343 .jim_handler
= jim_mem2array
,
5344 .help
= "read 8/16/32 bit memory and return as a TCL array "
5345 "for script processing",
5346 .usage
= "arrayname bitwidth address count",
5349 .name
= "array2mem",
5350 .mode
= COMMAND_EXEC
,
5351 .jim_handler
= jim_array2mem
,
5352 .help
= "convert a TCL array to memory locations "
5353 "and write the 8/16/32 bit values",
5354 .usage
= "arrayname bitwidth address count",
5357 .name
= "reset_nag",
5358 .handler
= handle_target_reset_nag
,
5359 .mode
= COMMAND_ANY
,
5360 .help
= "Nag after each reset about options that could have been "
5361 "enabled to improve performance. ",
5362 .usage
= "['enable'|'disable']",
5364 COMMAND_REGISTRATION_DONE
5366 static int target_register_user_commands(struct command_context
*cmd_ctx
)
5368 int retval
= ERROR_OK
;
5369 if ((retval
= target_request_register_commands(cmd_ctx
)) != ERROR_OK
)
5372 if ((retval
= trace_register_commands(cmd_ctx
)) != ERROR_OK
)
5376 return register_commands(cmd_ctx
, NULL
, target_exec_command_handlers
);