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 uint24_t from a buffer in target memory endianness */
284 uint32_t target_buffer_get_u24(struct target
*target
, const uint8_t *buffer
)
286 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
287 return le_to_h_u24(buffer
);
289 return be_to_h_u24(buffer
);
292 /* read a uint16_t from a buffer in target memory endianness */
293 uint16_t target_buffer_get_u16(struct target
*target
, const uint8_t *buffer
)
295 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
296 return le_to_h_u16(buffer
);
298 return be_to_h_u16(buffer
);
301 /* read a uint8_t from a buffer in target memory endianness */
302 static uint8_t target_buffer_get_u8(struct target
*target
, const uint8_t *buffer
)
304 return *buffer
& 0x0ff;
307 /* write a uint32_t to a buffer in target memory endianness */
308 void target_buffer_set_u32(struct target
*target
, uint8_t *buffer
, uint32_t value
)
310 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
311 h_u32_to_le(buffer
, value
);
313 h_u32_to_be(buffer
, value
);
316 /* write a uint24_t to a buffer in target memory endianness */
317 void target_buffer_set_u24(struct target
*target
, uint8_t *buffer
, uint32_t value
)
319 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
320 h_u24_to_le(buffer
, value
);
322 h_u24_to_be(buffer
, value
);
325 /* write a uint16_t to a buffer in target memory endianness */
326 void target_buffer_set_u16(struct target
*target
, uint8_t *buffer
, uint16_t value
)
328 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
329 h_u16_to_le(buffer
, value
);
331 h_u16_to_be(buffer
, value
);
334 /* write a uint8_t to a buffer in target memory endianness */
335 static void target_buffer_set_u8(struct target
*target
, uint8_t *buffer
, uint8_t value
)
340 /* return a pointer to a configured target; id is name or number */
341 struct target
*get_target(const char *id
)
343 struct target
*target
;
345 /* try as tcltarget name */
346 for (target
= all_targets
; target
; target
= target
->next
) {
347 if (target
->cmd_name
== NULL
)
349 if (strcmp(id
, target
->cmd_name
) == 0)
353 /* It's OK to remove this fallback sometime after August 2010 or so */
355 /* no match, try as number */
357 if (parse_uint(id
, &num
) != ERROR_OK
)
360 for (target
= all_targets
; target
; target
= target
->next
) {
361 if (target
->target_number
== (int)num
) {
362 LOG_WARNING("use '%s' as target identifier, not '%u'",
363 target
->cmd_name
, num
);
371 /* returns a pointer to the n-th configured target */
372 static struct target
*get_target_by_num(int num
)
374 struct target
*target
= all_targets
;
377 if (target
->target_number
== num
) {
380 target
= target
->next
;
386 struct target
* get_current_target(struct command_context
*cmd_ctx
)
388 struct target
*target
= get_target_by_num(cmd_ctx
->current_target
);
392 LOG_ERROR("BUG: current_target out of bounds");
399 int target_poll(struct target
*target
)
403 /* We can't poll until after examine */
404 if (!target_was_examined(target
))
406 /* Fail silently lest we pollute the log */
410 retval
= target
->type
->poll(target
);
411 if (retval
!= ERROR_OK
)
414 if (target
->halt_issued
)
416 if (target
->state
== TARGET_HALTED
)
418 target
->halt_issued
= false;
421 long long t
= timeval_ms() - target
->halt_issued_time
;
424 target
->halt_issued
= false;
425 LOG_INFO("Halt timed out, wake up GDB.");
426 target_call_event_callbacks(target
, TARGET_EVENT_GDB_HALT
);
434 int target_halt(struct target
*target
)
437 /* We can't poll until after examine */
438 if (!target_was_examined(target
))
440 LOG_ERROR("Target not examined yet");
444 retval
= target
->type
->halt(target
);
445 if (retval
!= ERROR_OK
)
448 target
->halt_issued
= true;
449 target
->halt_issued_time
= timeval_ms();
455 * Make the target (re)start executing using its saved execution
456 * context (possibly with some modifications).
458 * @param target Which target should start executing.
459 * @param current True to use the target's saved program counter instead
460 * of the address parameter
461 * @param address Optionally used as the program counter.
462 * @param handle_breakpoints True iff breakpoints at the resumption PC
463 * should be skipped. (For example, maybe execution was stopped by
464 * such a breakpoint, in which case it would be counterprodutive to
466 * @param debug_execution False if all working areas allocated by OpenOCD
467 * should be released and/or restored to their original contents.
468 * (This would for example be true to run some downloaded "helper"
469 * algorithm code, which resides in one such working buffer and uses
470 * another for data storage.)
472 * @todo Resolve the ambiguity about what the "debug_execution" flag
473 * signifies. For example, Target implementations don't agree on how
474 * it relates to invalidation of the register cache, or to whether
475 * breakpoints and watchpoints should be enabled. (It would seem wrong
476 * to enable breakpoints when running downloaded "helper" algorithms
477 * (debug_execution true), since the breakpoints would be set to match
478 * target firmware being debugged, not the helper algorithm.... and
479 * enabling them could cause such helpers to malfunction (for example,
480 * by overwriting data with a breakpoint instruction. On the other
481 * hand the infrastructure for running such helpers might use this
482 * procedure but rely on hardware breakpoint to detect termination.)
484 int target_resume(struct target
*target
, int current
, uint32_t address
, int handle_breakpoints
, int debug_execution
)
488 /* We can't poll until after examine */
489 if (!target_was_examined(target
))
491 LOG_ERROR("Target not examined yet");
495 /* note that resume *must* be asynchronous. The CPU can halt before
496 * we poll. The CPU can even halt at the current PC as a result of
497 * a software breakpoint being inserted by (a bug?) the application.
499 if ((retval
= target
->type
->resume(target
, current
, address
, handle_breakpoints
, debug_execution
)) != ERROR_OK
)
505 static int target_process_reset(struct command_context
*cmd_ctx
, enum target_reset_mode reset_mode
)
510 n
= Jim_Nvp_value2name_simple(nvp_reset_modes
, reset_mode
);
511 if (n
->name
== NULL
) {
512 LOG_ERROR("invalid reset mode");
516 /* disable polling during reset to make reset event scripts
517 * more predictable, i.e. dr/irscan & pathmove in events will
518 * not have JTAG operations injected into the middle of a sequence.
520 bool save_poll
= jtag_poll_get_enabled();
522 jtag_poll_set_enabled(false);
524 sprintf(buf
, "ocd_process_reset %s", n
->name
);
525 retval
= Jim_Eval(cmd_ctx
->interp
, buf
);
527 jtag_poll_set_enabled(save_poll
);
529 if (retval
!= JIM_OK
) {
530 Jim_MakeErrorMessage(cmd_ctx
->interp
);
531 command_print(NULL
,"%s\n", Jim_GetString(Jim_GetResult(cmd_ctx
->interp
), NULL
));
535 /* We want any events to be processed before the prompt */
536 retval
= target_call_timer_callbacks_now();
538 struct target
*target
;
539 for (target
= all_targets
; target
; target
= target
->next
) {
540 target
->type
->check_reset(target
);
546 static int identity_virt2phys(struct target
*target
,
547 uint32_t virtual, uint32_t *physical
)
553 static int no_mmu(struct target
*target
, int *enabled
)
559 static int default_examine(struct target
*target
)
561 target_set_examined(target
);
565 /* no check by default */
566 static int default_check_reset(struct target
*target
)
571 int target_examine_one(struct target
*target
)
573 return target
->type
->examine(target
);
576 static int jtag_enable_callback(enum jtag_event event
, void *priv
)
578 struct target
*target
= priv
;
580 if (event
!= JTAG_TAP_EVENT_ENABLE
|| !target
->tap
->enabled
)
583 jtag_unregister_event_callback(jtag_enable_callback
, target
);
584 return target_examine_one(target
);
588 /* Targets that correctly implement init + examine, i.e.
589 * no communication with target during init:
593 int target_examine(void)
595 int retval
= ERROR_OK
;
596 struct target
*target
;
598 for (target
= all_targets
; target
; target
= target
->next
)
600 /* defer examination, but don't skip it */
601 if (!target
->tap
->enabled
) {
602 jtag_register_event_callback(jtag_enable_callback
,
606 if ((retval
= target_examine_one(target
)) != ERROR_OK
)
611 const char *target_type_name(struct target
*target
)
613 return target
->type
->name
;
616 static int target_write_memory_imp(struct target
*target
, uint32_t address
, uint32_t size
, uint32_t count
, uint8_t *buffer
)
618 if (!target_was_examined(target
))
620 LOG_ERROR("Target not examined yet");
623 return target
->type
->write_memory_imp(target
, address
, size
, count
, buffer
);
626 static int target_read_memory_imp(struct target
*target
, uint32_t address
, uint32_t size
, uint32_t count
, uint8_t *buffer
)
628 if (!target_was_examined(target
))
630 LOG_ERROR("Target not examined yet");
633 return target
->type
->read_memory_imp(target
, address
, size
, count
, buffer
);
636 static int target_soft_reset_halt_imp(struct target
*target
)
638 if (!target_was_examined(target
))
640 LOG_ERROR("Target not examined yet");
643 if (!target
->type
->soft_reset_halt_imp
) {
644 LOG_ERROR("Target %s does not support soft_reset_halt",
645 target_name(target
));
648 return target
->type
->soft_reset_halt_imp(target
);
652 * Downloads a target-specific native code algorithm to the target,
653 * and executes it. * Note that some targets may need to set up, enable,
654 * and tear down a breakpoint (hard or * soft) to detect algorithm
655 * termination, while others may support lower overhead schemes where
656 * soft breakpoints embedded in the algorithm automatically terminate the
659 * @param target used to run the algorithm
660 * @param arch_info target-specific description of the algorithm.
662 int target_run_algorithm(struct target
*target
,
663 int num_mem_params
, struct mem_param
*mem_params
,
664 int num_reg_params
, struct reg_param
*reg_param
,
665 uint32_t entry_point
, uint32_t exit_point
,
666 int timeout_ms
, void *arch_info
)
668 int retval
= ERROR_FAIL
;
670 if (!target_was_examined(target
))
672 LOG_ERROR("Target not examined yet");
675 if (!target
->type
->run_algorithm
) {
676 LOG_ERROR("Target type '%s' does not support %s",
677 target_type_name(target
), __func__
);
681 target
->running_alg
= true;
682 retval
= target
->type
->run_algorithm(target
,
683 num_mem_params
, mem_params
,
684 num_reg_params
, reg_param
,
685 entry_point
, exit_point
, timeout_ms
, arch_info
);
686 target
->running_alg
= false;
693 int target_read_memory(struct target
*target
,
694 uint32_t address
, uint32_t size
, uint32_t count
, uint8_t *buffer
)
696 return target
->type
->read_memory(target
, address
, size
, count
, buffer
);
699 static int target_read_phys_memory(struct target
*target
,
700 uint32_t address
, uint32_t size
, uint32_t count
, uint8_t *buffer
)
702 return target
->type
->read_phys_memory(target
, address
, size
, count
, buffer
);
705 int target_write_memory(struct target
*target
,
706 uint32_t address
, uint32_t size
, uint32_t count
, uint8_t *buffer
)
708 return target
->type
->write_memory(target
, address
, size
, count
, buffer
);
711 static int target_write_phys_memory(struct target
*target
,
712 uint32_t address
, uint32_t size
, uint32_t count
, uint8_t *buffer
)
714 return target
->type
->write_phys_memory(target
, address
, size
, count
, buffer
);
717 int target_bulk_write_memory(struct target
*target
,
718 uint32_t address
, uint32_t count
, uint8_t *buffer
)
720 return target
->type
->bulk_write_memory(target
, address
, count
, buffer
);
723 int target_add_breakpoint(struct target
*target
,
724 struct breakpoint
*breakpoint
)
726 if (target
->state
!= TARGET_HALTED
) {
727 LOG_WARNING("target %s is not halted", target
->cmd_name
);
728 return ERROR_TARGET_NOT_HALTED
;
730 return target
->type
->add_breakpoint(target
, breakpoint
);
732 int target_remove_breakpoint(struct target
*target
,
733 struct breakpoint
*breakpoint
)
735 return target
->type
->remove_breakpoint(target
, breakpoint
);
738 int target_add_watchpoint(struct target
*target
,
739 struct watchpoint
*watchpoint
)
741 if (target
->state
!= TARGET_HALTED
) {
742 LOG_WARNING("target %s is not halted", target
->cmd_name
);
743 return ERROR_TARGET_NOT_HALTED
;
745 return target
->type
->add_watchpoint(target
, watchpoint
);
747 int target_remove_watchpoint(struct target
*target
,
748 struct watchpoint
*watchpoint
)
750 return target
->type
->remove_watchpoint(target
, watchpoint
);
753 int target_get_gdb_reg_list(struct target
*target
,
754 struct reg
**reg_list
[], int *reg_list_size
)
756 return target
->type
->get_gdb_reg_list(target
, reg_list
, reg_list_size
);
758 int target_step(struct target
*target
,
759 int current
, uint32_t address
, int handle_breakpoints
)
761 return target
->type
->step(target
, current
, address
, handle_breakpoints
);
766 * Reset the @c examined flag for the given target.
767 * Pure paranoia -- targets are zeroed on allocation.
769 static void target_reset_examined(struct target
*target
)
771 target
->examined
= false;
775 err_read_phys_memory(struct target
*target
, uint32_t address
,
776 uint32_t size
, uint32_t count
, uint8_t *buffer
)
778 LOG_ERROR("Not implemented: %s", __func__
);
783 err_write_phys_memory(struct target
*target
, uint32_t address
,
784 uint32_t size
, uint32_t count
, uint8_t *buffer
)
786 LOG_ERROR("Not implemented: %s", __func__
);
790 static int handle_target(void *priv
);
792 static int target_init_one(struct command_context
*cmd_ctx
,
793 struct target
*target
)
795 target_reset_examined(target
);
797 struct target_type
*type
= target
->type
;
798 if (type
->examine
== NULL
)
799 type
->examine
= default_examine
;
801 if (type
->check_reset
== NULL
)
802 type
->check_reset
= default_check_reset
;
804 int retval
= type
->init_target(cmd_ctx
, target
);
805 if (ERROR_OK
!= retval
)
807 LOG_ERROR("target '%s' init failed", target_name(target
));
812 * @todo get rid of those *memory_imp() methods, now that all
813 * callers are using target_*_memory() accessors ... and make
814 * sure the "physical" paths handle the same issues.
816 /* a non-invasive way(in terms of patches) to add some code that
817 * runs before the type->write/read_memory implementation
819 type
->write_memory_imp
= target
->type
->write_memory
;
820 type
->write_memory
= target_write_memory_imp
;
822 type
->read_memory_imp
= target
->type
->read_memory
;
823 type
->read_memory
= target_read_memory_imp
;
825 type
->soft_reset_halt_imp
= target
->type
->soft_reset_halt
;
826 type
->soft_reset_halt
= target_soft_reset_halt_imp
;
828 /* Sanity-check MMU support ... stub in what we must, to help
829 * implement it in stages, but warn if we need to do so.
833 if (type
->write_phys_memory
== NULL
)
835 LOG_ERROR("type '%s' is missing write_phys_memory",
837 type
->write_phys_memory
= err_write_phys_memory
;
839 if (type
->read_phys_memory
== NULL
)
841 LOG_ERROR("type '%s' is missing read_phys_memory",
843 type
->read_phys_memory
= err_read_phys_memory
;
845 if (type
->virt2phys
== NULL
)
847 LOG_ERROR("type '%s' is missing virt2phys", type
->name
);
848 type
->virt2phys
= identity_virt2phys
;
853 /* Make sure no-MMU targets all behave the same: make no
854 * distinction between physical and virtual addresses, and
855 * ensure that virt2phys() is always an identity mapping.
857 if (type
->write_phys_memory
|| type
->read_phys_memory
860 LOG_WARNING("type '%s' has bad MMU hooks", type
->name
);
864 type
->write_phys_memory
= type
->write_memory
;
865 type
->read_phys_memory
= type
->read_memory
;
866 type
->virt2phys
= identity_virt2phys
;
871 static int target_init(struct command_context
*cmd_ctx
)
873 struct target
*target
;
876 for (target
= all_targets
; target
; target
= target
->next
)
878 retval
= target_init_one(cmd_ctx
, target
);
879 if (ERROR_OK
!= retval
)
886 retval
= target_register_user_commands(cmd_ctx
);
887 if (ERROR_OK
!= retval
)
890 retval
= target_register_timer_callback(&handle_target
,
891 polling_interval
, 1, cmd_ctx
->interp
);
892 if (ERROR_OK
!= retval
)
898 COMMAND_HANDLER(handle_target_init_command
)
901 return ERROR_COMMAND_SYNTAX_ERROR
;
903 static bool target_initialized
= false;
904 if (target_initialized
)
906 LOG_INFO("'target init' has already been called");
909 target_initialized
= true;
911 LOG_DEBUG("Initializing targets...");
912 return target_init(CMD_CTX
);
915 int target_register_event_callback(int (*callback
)(struct target
*target
, enum target_event event
, void *priv
), void *priv
)
917 struct target_event_callback
**callbacks_p
= &target_event_callbacks
;
919 if (callback
== NULL
)
921 return ERROR_INVALID_ARGUMENTS
;
926 while ((*callbacks_p
)->next
)
927 callbacks_p
= &((*callbacks_p
)->next
);
928 callbacks_p
= &((*callbacks_p
)->next
);
931 (*callbacks_p
) = malloc(sizeof(struct target_event_callback
));
932 (*callbacks_p
)->callback
= callback
;
933 (*callbacks_p
)->priv
= priv
;
934 (*callbacks_p
)->next
= NULL
;
939 int target_register_timer_callback(int (*callback
)(void *priv
), int time_ms
, int periodic
, void *priv
)
941 struct target_timer_callback
**callbacks_p
= &target_timer_callbacks
;
944 if (callback
== NULL
)
946 return ERROR_INVALID_ARGUMENTS
;
951 while ((*callbacks_p
)->next
)
952 callbacks_p
= &((*callbacks_p
)->next
);
953 callbacks_p
= &((*callbacks_p
)->next
);
956 (*callbacks_p
) = malloc(sizeof(struct target_timer_callback
));
957 (*callbacks_p
)->callback
= callback
;
958 (*callbacks_p
)->periodic
= periodic
;
959 (*callbacks_p
)->time_ms
= time_ms
;
961 gettimeofday(&now
, NULL
);
962 (*callbacks_p
)->when
.tv_usec
= now
.tv_usec
+ (time_ms
% 1000) * 1000;
963 time_ms
-= (time_ms
% 1000);
964 (*callbacks_p
)->when
.tv_sec
= now
.tv_sec
+ (time_ms
/ 1000);
965 if ((*callbacks_p
)->when
.tv_usec
> 1000000)
967 (*callbacks_p
)->when
.tv_usec
= (*callbacks_p
)->when
.tv_usec
- 1000000;
968 (*callbacks_p
)->when
.tv_sec
+= 1;
971 (*callbacks_p
)->priv
= priv
;
972 (*callbacks_p
)->next
= NULL
;
977 int target_unregister_event_callback(int (*callback
)(struct target
*target
, enum target_event event
, void *priv
), void *priv
)
979 struct target_event_callback
**p
= &target_event_callbacks
;
980 struct target_event_callback
*c
= target_event_callbacks
;
982 if (callback
== NULL
)
984 return ERROR_INVALID_ARGUMENTS
;
989 struct target_event_callback
*next
= c
->next
;
990 if ((c
->callback
== callback
) && (c
->priv
== priv
))
1004 static int target_unregister_timer_callback(int (*callback
)(void *priv
), void *priv
)
1006 struct target_timer_callback
**p
= &target_timer_callbacks
;
1007 struct target_timer_callback
*c
= target_timer_callbacks
;
1009 if (callback
== NULL
)
1011 return ERROR_INVALID_ARGUMENTS
;
1016 struct target_timer_callback
*next
= c
->next
;
1017 if ((c
->callback
== callback
) && (c
->priv
== priv
))
1031 int target_call_event_callbacks(struct target
*target
, enum target_event event
)
1033 struct target_event_callback
*callback
= target_event_callbacks
;
1034 struct target_event_callback
*next_callback
;
1036 if (event
== TARGET_EVENT_HALTED
)
1038 /* execute early halted first */
1039 target_call_event_callbacks(target
, TARGET_EVENT_GDB_HALT
);
1042 LOG_DEBUG("target event %i (%s)",
1044 Jim_Nvp_value2name_simple(nvp_target_event
, event
)->name
);
1046 target_handle_event(target
, event
);
1050 next_callback
= callback
->next
;
1051 callback
->callback(target
, event
, callback
->priv
);
1052 callback
= next_callback
;
1058 static int target_timer_callback_periodic_restart(
1059 struct target_timer_callback
*cb
, struct timeval
*now
)
1061 int time_ms
= cb
->time_ms
;
1062 cb
->when
.tv_usec
= now
->tv_usec
+ (time_ms
% 1000) * 1000;
1063 time_ms
-= (time_ms
% 1000);
1064 cb
->when
.tv_sec
= now
->tv_sec
+ time_ms
/ 1000;
1065 if (cb
->when
.tv_usec
> 1000000)
1067 cb
->when
.tv_usec
= cb
->when
.tv_usec
- 1000000;
1068 cb
->when
.tv_sec
+= 1;
1073 static int target_call_timer_callback(struct target_timer_callback
*cb
,
1074 struct timeval
*now
)
1076 cb
->callback(cb
->priv
);
1079 return target_timer_callback_periodic_restart(cb
, now
);
1081 return target_unregister_timer_callback(cb
->callback
, cb
->priv
);
1084 static int target_call_timer_callbacks_check_time(int checktime
)
1089 gettimeofday(&now
, NULL
);
1091 struct target_timer_callback
*callback
= target_timer_callbacks
;
1094 // cleaning up may unregister and free this callback
1095 struct target_timer_callback
*next_callback
= callback
->next
;
1097 bool call_it
= callback
->callback
&&
1098 ((!checktime
&& callback
->periodic
) ||
1099 now
.tv_sec
> callback
->when
.tv_sec
||
1100 (now
.tv_sec
== callback
->when
.tv_sec
&&
1101 now
.tv_usec
>= callback
->when
.tv_usec
));
1105 int retval
= target_call_timer_callback(callback
, &now
);
1106 if (retval
!= ERROR_OK
)
1110 callback
= next_callback
;
1116 int target_call_timer_callbacks(void)
1118 return target_call_timer_callbacks_check_time(1);
1121 /* invoke periodic callbacks immediately */
1122 int target_call_timer_callbacks_now(void)
1124 return target_call_timer_callbacks_check_time(0);
1127 int target_alloc_working_area_try(struct target
*target
, uint32_t size
, struct working_area
**area
)
1129 struct working_area
*c
= target
->working_areas
;
1130 struct working_area
*new_wa
= NULL
;
1132 /* Reevaluate working area address based on MMU state*/
1133 if (target
->working_areas
== NULL
)
1138 retval
= target
->type
->mmu(target
, &enabled
);
1139 if (retval
!= ERROR_OK
)
1145 if (target
->working_area_phys_spec
) {
1146 LOG_DEBUG("MMU disabled, using physical "
1147 "address for working memory 0x%08x",
1148 (unsigned)target
->working_area_phys
);
1149 target
->working_area
= target
->working_area_phys
;
1151 LOG_ERROR("No working memory available. "
1152 "Specify -work-area-phys to target.");
1153 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
1156 if (target
->working_area_virt_spec
) {
1157 LOG_DEBUG("MMU enabled, using virtual "
1158 "address for working memory 0x%08x",
1159 (unsigned)target
->working_area_virt
);
1160 target
->working_area
= target
->working_area_virt
;
1162 LOG_ERROR("No working memory available. "
1163 "Specify -work-area-virt to target.");
1164 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
1169 /* only allocate multiples of 4 byte */
1172 LOG_ERROR("BUG: code tried to allocate unaligned number of bytes (0x%08x), padding", ((unsigned)(size
)));
1173 size
= (size
+ 3) & (~3);
1176 /* see if there's already a matching working area */
1179 if ((c
->free
) && (c
->size
== size
))
1187 /* if not, allocate a new one */
1190 struct working_area
**p
= &target
->working_areas
;
1191 uint32_t first_free
= target
->working_area
;
1192 uint32_t free_size
= target
->working_area_size
;
1194 c
= target
->working_areas
;
1197 first_free
+= c
->size
;
1198 free_size
-= c
->size
;
1203 if (free_size
< size
)
1205 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
1208 LOG_DEBUG("allocated new working area at address 0x%08x", (unsigned)first_free
);
1210 new_wa
= malloc(sizeof(struct working_area
));
1211 new_wa
->next
= NULL
;
1212 new_wa
->size
= size
;
1213 new_wa
->address
= first_free
;
1215 if (target
->backup_working_area
)
1218 new_wa
->backup
= malloc(new_wa
->size
);
1219 if ((retval
= target_read_memory(target
, new_wa
->address
, 4, new_wa
->size
/ 4, new_wa
->backup
)) != ERROR_OK
)
1221 free(new_wa
->backup
);
1228 new_wa
->backup
= NULL
;
1231 /* put new entry in list */
1235 /* mark as used, and return the new (reused) area */
1236 new_wa
->free
= false;
1240 new_wa
->user
= area
;
1245 int target_alloc_working_area(struct target
*target
, uint32_t size
, struct working_area
**area
)
1249 retval
= target_alloc_working_area_try(target
, size
, area
);
1250 if (retval
== ERROR_TARGET_RESOURCE_NOT_AVAILABLE
)
1252 LOG_WARNING("not enough working area available(requested %u)", (unsigned)(size
));
1258 static int target_free_working_area_restore(struct target
*target
, struct working_area
*area
, int restore
)
1263 if (restore
&& target
->backup_working_area
)
1266 if ((retval
= target_write_memory(target
, area
->address
, 4, area
->size
/ 4, area
->backup
)) != ERROR_OK
)
1272 /* mark user pointer invalid */
1279 int target_free_working_area(struct target
*target
, struct working_area
*area
)
1281 return target_free_working_area_restore(target
, area
, 1);
1284 /* free resources and restore memory, if restoring memory fails,
1285 * free up resources anyway
1287 static void target_free_all_working_areas_restore(struct target
*target
, int restore
)
1289 struct working_area
*c
= target
->working_areas
;
1293 struct working_area
*next
= c
->next
;
1294 target_free_working_area_restore(target
, c
, restore
);
1304 target
->working_areas
= NULL
;
1307 void target_free_all_working_areas(struct target
*target
)
1309 target_free_all_working_areas_restore(target
, 1);
1312 int target_arch_state(struct target
*target
)
1317 LOG_USER("No target has been configured");
1321 LOG_USER("target state: %s", target_state_name( target
));
1323 if (target
->state
!= TARGET_HALTED
)
1326 retval
= target
->type
->arch_state(target
);
1330 /* Single aligned words are guaranteed to use 16 or 32 bit access
1331 * mode respectively, otherwise data is handled as quickly as
1334 int target_write_buffer(struct target
*target
, uint32_t address
, uint32_t size
, uint8_t *buffer
)
1337 LOG_DEBUG("writing buffer of %i byte at 0x%8.8x",
1338 (int)size
, (unsigned)address
);
1340 if (!target_was_examined(target
))
1342 LOG_ERROR("Target not examined yet");
1350 if ((address
+ size
- 1) < address
)
1352 /* GDB can request this when e.g. PC is 0xfffffffc*/
1353 LOG_ERROR("address + size wrapped(0x%08x, 0x%08x)",
1359 if (((address
% 2) == 0) && (size
== 2))
1361 return target_write_memory(target
, address
, 2, 1, buffer
);
1364 /* handle unaligned head bytes */
1367 uint32_t unaligned
= 4 - (address
% 4);
1369 if (unaligned
> size
)
1372 if ((retval
= target_write_memory(target
, address
, 1, unaligned
, buffer
)) != ERROR_OK
)
1375 buffer
+= unaligned
;
1376 address
+= unaligned
;
1380 /* handle aligned words */
1383 int aligned
= size
- (size
% 4);
1385 /* use bulk writes above a certain limit. This may have to be changed */
1388 if ((retval
= target
->type
->bulk_write_memory(target
, address
, aligned
/ 4, buffer
)) != ERROR_OK
)
1393 if ((retval
= target_write_memory(target
, address
, 4, aligned
/ 4, buffer
)) != ERROR_OK
)
1402 /* handle tail writes of less than 4 bytes */
1405 if ((retval
= target_write_memory(target
, address
, 1, size
, buffer
)) != ERROR_OK
)
1412 /* Single aligned words are guaranteed to use 16 or 32 bit access
1413 * mode respectively, otherwise data is handled as quickly as
1416 int target_read_buffer(struct target
*target
, uint32_t address
, uint32_t size
, uint8_t *buffer
)
1419 LOG_DEBUG("reading buffer of %i byte at 0x%8.8x",
1420 (int)size
, (unsigned)address
);
1422 if (!target_was_examined(target
))
1424 LOG_ERROR("Target not examined yet");
1432 if ((address
+ size
- 1) < address
)
1434 /* GDB can request this when e.g. PC is 0xfffffffc*/
1435 LOG_ERROR("address + size wrapped(0x%08" PRIx32
", 0x%08" PRIx32
")",
1441 if (((address
% 2) == 0) && (size
== 2))
1443 return target_read_memory(target
, address
, 2, 1, buffer
);
1446 /* handle unaligned head bytes */
1449 uint32_t unaligned
= 4 - (address
% 4);
1451 if (unaligned
> size
)
1454 if ((retval
= target_read_memory(target
, address
, 1, unaligned
, buffer
)) != ERROR_OK
)
1457 buffer
+= unaligned
;
1458 address
+= unaligned
;
1462 /* handle aligned words */
1465 int aligned
= size
- (size
% 4);
1467 if ((retval
= target_read_memory(target
, address
, 4, aligned
/ 4, buffer
)) != ERROR_OK
)
1475 /*prevent byte access when possible (avoid AHB access limitations in some cases)*/
1478 int aligned
= size
- (size
%2);
1479 retval
= target_read_memory(target
, address
, 2, aligned
/ 2, buffer
);
1480 if (retval
!= ERROR_OK
)
1487 /* handle tail writes of less than 4 bytes */
1490 if ((retval
= target_read_memory(target
, address
, 1, size
, buffer
)) != ERROR_OK
)
1497 int target_checksum_memory(struct target
*target
, uint32_t address
, uint32_t size
, uint32_t* crc
)
1502 uint32_t checksum
= 0;
1503 if (!target_was_examined(target
))
1505 LOG_ERROR("Target not examined yet");
1509 if ((retval
= target
->type
->checksum_memory(target
, address
,
1510 size
, &checksum
)) != ERROR_OK
)
1512 buffer
= malloc(size
);
1515 LOG_ERROR("error allocating buffer for section (%d bytes)", (int)size
);
1516 return ERROR_INVALID_ARGUMENTS
;
1518 retval
= target_read_buffer(target
, address
, size
, buffer
);
1519 if (retval
!= ERROR_OK
)
1525 /* convert to target endianess */
1526 for (i
= 0; i
< (size
/sizeof(uint32_t)); i
++)
1528 uint32_t target_data
;
1529 target_data
= target_buffer_get_u32(target
, &buffer
[i
*sizeof(uint32_t)]);
1530 target_buffer_set_u32(target
, &buffer
[i
*sizeof(uint32_t)], target_data
);
1533 retval
= image_calculate_checksum(buffer
, size
, &checksum
);
1542 int target_blank_check_memory(struct target
*target
, uint32_t address
, uint32_t size
, uint32_t* blank
)
1545 if (!target_was_examined(target
))
1547 LOG_ERROR("Target not examined yet");
1551 if (target
->type
->blank_check_memory
== 0)
1552 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
1554 retval
= target
->type
->blank_check_memory(target
, address
, size
, blank
);
1559 int target_read_u32(struct target
*target
, uint32_t address
, uint32_t *value
)
1561 uint8_t value_buf
[4];
1562 if (!target_was_examined(target
))
1564 LOG_ERROR("Target not examined yet");
1568 int retval
= target_read_memory(target
, address
, 4, 1, value_buf
);
1570 if (retval
== ERROR_OK
)
1572 *value
= target_buffer_get_u32(target
, value_buf
);
1573 LOG_DEBUG("address: 0x%8.8" PRIx32
", value: 0x%8.8" PRIx32
"",
1580 LOG_DEBUG("address: 0x%8.8" PRIx32
" failed",
1587 int target_read_u16(struct target
*target
, uint32_t address
, uint16_t *value
)
1589 uint8_t value_buf
[2];
1590 if (!target_was_examined(target
))
1592 LOG_ERROR("Target not examined yet");
1596 int retval
= target_read_memory(target
, address
, 2, 1, value_buf
);
1598 if (retval
== ERROR_OK
)
1600 *value
= target_buffer_get_u16(target
, value_buf
);
1601 LOG_DEBUG("address: 0x%8.8" PRIx32
", value: 0x%4.4x",
1608 LOG_DEBUG("address: 0x%8.8" PRIx32
" failed",
1615 int target_read_u8(struct target
*target
, uint32_t address
, uint8_t *value
)
1617 int retval
= target_read_memory(target
, address
, 1, 1, value
);
1618 if (!target_was_examined(target
))
1620 LOG_ERROR("Target not examined yet");
1624 if (retval
== ERROR_OK
)
1626 LOG_DEBUG("address: 0x%8.8" PRIx32
", value: 0x%2.2x",
1633 LOG_DEBUG("address: 0x%8.8" PRIx32
" failed",
1640 int target_write_u32(struct target
*target
, uint32_t address
, uint32_t value
)
1643 uint8_t value_buf
[4];
1644 if (!target_was_examined(target
))
1646 LOG_ERROR("Target not examined yet");
1650 LOG_DEBUG("address: 0x%8.8" PRIx32
", value: 0x%8.8" PRIx32
"",
1654 target_buffer_set_u32(target
, value_buf
, value
);
1655 if ((retval
= target_write_memory(target
, address
, 4, 1, value_buf
)) != ERROR_OK
)
1657 LOG_DEBUG("failed: %i", retval
);
1663 int target_write_u16(struct target
*target
, uint32_t address
, uint16_t value
)
1666 uint8_t value_buf
[2];
1667 if (!target_was_examined(target
))
1669 LOG_ERROR("Target not examined yet");
1673 LOG_DEBUG("address: 0x%8.8" PRIx32
", value: 0x%8.8x",
1677 target_buffer_set_u16(target
, value_buf
, value
);
1678 if ((retval
= target_write_memory(target
, address
, 2, 1, value_buf
)) != ERROR_OK
)
1680 LOG_DEBUG("failed: %i", retval
);
1686 int target_write_u8(struct target
*target
, uint32_t address
, uint8_t value
)
1689 if (!target_was_examined(target
))
1691 LOG_ERROR("Target not examined yet");
1695 LOG_DEBUG("address: 0x%8.8" PRIx32
", value: 0x%2.2x",
1698 if ((retval
= target_write_memory(target
, address
, 1, 1, &value
)) != ERROR_OK
)
1700 LOG_DEBUG("failed: %i", retval
);
1706 COMMAND_HANDLER(handle_targets_command
)
1708 struct target
*target
= all_targets
;
1712 target
= get_target(CMD_ARGV
[0]);
1713 if (target
== NULL
) {
1714 command_print(CMD_CTX
,"Target: %s is unknown, try one of:\n", CMD_ARGV
[0]);
1717 if (!target
->tap
->enabled
) {
1718 command_print(CMD_CTX
,"Target: TAP %s is disabled, "
1719 "can't be the current target\n",
1720 target
->tap
->dotted_name
);
1724 CMD_CTX
->current_target
= target
->target_number
;
1729 target
= all_targets
;
1730 command_print(CMD_CTX
, " TargetName Type Endian TapName State ");
1731 command_print(CMD_CTX
, "-- ------------------ ---------- ------ ------------------ ------------");
1737 if (target
->tap
->enabled
)
1738 state
= target_state_name( target
);
1740 state
= "tap-disabled";
1742 if (CMD_CTX
->current_target
== target
->target_number
)
1745 /* keep columns lined up to match the headers above */
1746 command_print(CMD_CTX
, "%2d%c %-18s %-10s %-6s %-18s %s",
1747 target
->target_number
,
1749 target_name(target
),
1750 target_type_name(target
),
1751 Jim_Nvp_value2name_simple(nvp_target_endian
,
1752 target
->endianness
)->name
,
1753 target
->tap
->dotted_name
,
1755 target
= target
->next
;
1761 /* every 300ms we check for reset & powerdropout and issue a "reset halt" if so. */
1763 static int powerDropout
;
1764 static int srstAsserted
;
1766 static int runPowerRestore
;
1767 static int runPowerDropout
;
1768 static int runSrstAsserted
;
1769 static int runSrstDeasserted
;
1771 static int sense_handler(void)
1773 static int prevSrstAsserted
= 0;
1774 static int prevPowerdropout
= 0;
1777 if ((retval
= jtag_power_dropout(&powerDropout
)) != ERROR_OK
)
1781 powerRestored
= prevPowerdropout
&& !powerDropout
;
1784 runPowerRestore
= 1;
1787 long long current
= timeval_ms();
1788 static long long lastPower
= 0;
1789 int waitMore
= lastPower
+ 2000 > current
;
1790 if (powerDropout
&& !waitMore
)
1792 runPowerDropout
= 1;
1793 lastPower
= current
;
1796 if ((retval
= jtag_srst_asserted(&srstAsserted
)) != ERROR_OK
)
1800 srstDeasserted
= prevSrstAsserted
&& !srstAsserted
;
1802 static long long lastSrst
= 0;
1803 waitMore
= lastSrst
+ 2000 > current
;
1804 if (srstDeasserted
&& !waitMore
)
1806 runSrstDeasserted
= 1;
1810 if (!prevSrstAsserted
&& srstAsserted
)
1812 runSrstAsserted
= 1;
1815 prevSrstAsserted
= srstAsserted
;
1816 prevPowerdropout
= powerDropout
;
1818 if (srstDeasserted
|| powerRestored
)
1820 /* Other than logging the event we can't do anything here.
1821 * Issuing a reset is a particularly bad idea as we might
1822 * be inside a reset already.
1829 static int backoff_times
= 0;
1830 static int backoff_count
= 0;
1832 /* process target state changes */
1833 static int handle_target(void *priv
)
1835 Jim_Interp
*interp
= (Jim_Interp
*)priv
;
1836 int retval
= ERROR_OK
;
1838 if (!is_jtag_poll_safe())
1840 /* polling is disabled currently */
1844 /* we do not want to recurse here... */
1845 static int recursive
= 0;
1850 /* danger! running these procedures can trigger srst assertions and power dropouts.
1851 * We need to avoid an infinite loop/recursion here and we do that by
1852 * clearing the flags after running these events.
1854 int did_something
= 0;
1855 if (runSrstAsserted
)
1857 LOG_INFO("srst asserted detected, running srst_asserted proc.");
1858 Jim_Eval(interp
, "srst_asserted");
1861 if (runSrstDeasserted
)
1863 Jim_Eval(interp
, "srst_deasserted");
1866 if (runPowerDropout
)
1868 LOG_INFO("Power dropout detected, running power_dropout proc.");
1869 Jim_Eval(interp
, "power_dropout");
1872 if (runPowerRestore
)
1874 Jim_Eval(interp
, "power_restore");
1880 /* clear detect flags */
1884 /* clear action flags */
1886 runSrstAsserted
= 0;
1887 runSrstDeasserted
= 0;
1888 runPowerRestore
= 0;
1889 runPowerDropout
= 0;
1894 if (backoff_times
> backoff_count
)
1896 /* do not poll this time as we failed previously */
1902 /* Poll targets for state changes unless that's globally disabled.
1903 * Skip targets that are currently disabled.
1905 for (struct target
*target
= all_targets
;
1906 is_jtag_poll_safe() && target
;
1907 target
= target
->next
)
1909 if (!target
->tap
->enabled
)
1912 /* only poll target if we've got power and srst isn't asserted */
1913 if (!powerDropout
&& !srstAsserted
)
1915 /* polling may fail silently until the target has been examined */
1916 if ((retval
= target_poll(target
)) != ERROR_OK
)
1918 /* 100ms polling interval. Increase interval between polling up to 5000ms */
1919 if (backoff_times
* polling_interval
< 5000)
1924 LOG_USER("Polling target failed, GDB will be halted. Polling again in %dms", backoff_times
* polling_interval
);
1926 /* Tell GDB to halt the debugger. This allows the user to
1927 * run monitor commands to handle the situation.
1929 target_call_event_callbacks(target
, TARGET_EVENT_GDB_HALT
);
1932 /* Since we succeeded, we reset backoff count */
1933 if (backoff_times
> 0)
1935 LOG_USER("Polling succeeded again");
1944 COMMAND_HANDLER(handle_reg_command
)
1946 struct target
*target
;
1947 struct reg
*reg
= NULL
;
1953 target
= get_current_target(CMD_CTX
);
1955 /* list all available registers for the current target */
1958 struct reg_cache
*cache
= target
->reg_cache
;
1965 command_print(CMD_CTX
, "===== %s", cache
->name
);
1967 for (i
= 0, reg
= cache
->reg_list
;
1968 i
< cache
->num_regs
;
1969 i
++, reg
++, count
++)
1971 /* only print cached values if they are valid */
1973 value
= buf_to_str(reg
->value
,
1975 command_print(CMD_CTX
,
1976 "(%i) %s (/%" PRIu32
"): 0x%s%s",
1984 command_print(CMD_CTX
, "(%i) %s (/%" PRIu32
")",
1989 cache
= cache
->next
;
1995 /* access a single register by its ordinal number */
1996 if ((CMD_ARGV
[0][0] >= '0') && (CMD_ARGV
[0][0] <= '9'))
1999 COMMAND_PARSE_NUMBER(uint
, CMD_ARGV
[0], num
);
2001 struct reg_cache
*cache
= target
->reg_cache
;
2006 for (i
= 0; i
< cache
->num_regs
; i
++)
2010 reg
= &cache
->reg_list
[i
];
2016 cache
= cache
->next
;
2021 command_print(CMD_CTX
, "%i is out of bounds, the current target has only %i registers (0 - %i)", num
, count
, count
- 1);
2024 } else /* access a single register by its name */
2026 reg
= register_get_by_name(target
->reg_cache
, CMD_ARGV
[0], 1);
2030 command_print(CMD_CTX
, "register %s not found in current target", CMD_ARGV
[0]);
2035 /* display a register */
2036 if ((CMD_ARGC
== 1) || ((CMD_ARGC
== 2) && !((CMD_ARGV
[1][0] >= '0') && (CMD_ARGV
[1][0] <= '9'))))
2038 if ((CMD_ARGC
== 2) && (strcmp(CMD_ARGV
[1], "force") == 0))
2041 if (reg
->valid
== 0)
2043 reg
->type
->get(reg
);
2045 value
= buf_to_str(reg
->value
, reg
->size
, 16);
2046 command_print(CMD_CTX
, "%s (/%i): 0x%s", reg
->name
, (int)(reg
->size
), value
);
2051 /* set register value */
2054 uint8_t *buf
= malloc(DIV_ROUND_UP(reg
->size
, 8));
2055 str_to_buf(CMD_ARGV
[1], strlen(CMD_ARGV
[1]), buf
, reg
->size
, 0);
2057 reg
->type
->set(reg
, buf
);
2059 value
= buf_to_str(reg
->value
, reg
->size
, 16);
2060 command_print(CMD_CTX
, "%s (/%i): 0x%s", reg
->name
, (int)(reg
->size
), value
);
2068 command_print(CMD_CTX
, "usage: reg <#|name> [value]");
2073 COMMAND_HANDLER(handle_poll_command
)
2075 int retval
= ERROR_OK
;
2076 struct target
*target
= get_current_target(CMD_CTX
);
2080 command_print(CMD_CTX
, "background polling: %s",
2081 jtag_poll_get_enabled() ? "on" : "off");
2082 command_print(CMD_CTX
, "TAP: %s (%s)",
2083 target
->tap
->dotted_name
,
2084 target
->tap
->enabled
? "enabled" : "disabled");
2085 if (!target
->tap
->enabled
)
2087 if ((retval
= target_poll(target
)) != ERROR_OK
)
2089 if ((retval
= target_arch_state(target
)) != ERROR_OK
)
2092 else if (CMD_ARGC
== 1)
2095 COMMAND_PARSE_ON_OFF(CMD_ARGV
[0], enable
);
2096 jtag_poll_set_enabled(enable
);
2100 return ERROR_COMMAND_SYNTAX_ERROR
;
2106 COMMAND_HANDLER(handle_wait_halt_command
)
2109 return ERROR_COMMAND_SYNTAX_ERROR
;
2114 int retval
= parse_uint(CMD_ARGV
[0], &ms
);
2115 if (ERROR_OK
!= retval
)
2117 command_print(CMD_CTX
, "usage: %s [seconds]", CMD_NAME
);
2118 return ERROR_COMMAND_SYNTAX_ERROR
;
2120 // convert seconds (given) to milliseconds (needed)
2124 struct target
*target
= get_current_target(CMD_CTX
);
2125 return target_wait_state(target
, TARGET_HALTED
, ms
);
2128 /* wait for target state to change. The trick here is to have a low
2129 * latency for short waits and not to suck up all the CPU time
2132 * After 500ms, keep_alive() is invoked
2134 int target_wait_state(struct target
*target
, enum target_state state
, int ms
)
2137 long long then
= 0, cur
;
2142 if ((retval
= target_poll(target
)) != ERROR_OK
)
2144 if (target
->state
== state
)
2152 then
= timeval_ms();
2153 LOG_DEBUG("waiting for target %s...",
2154 Jim_Nvp_value2name_simple(nvp_target_state
,state
)->name
);
2162 if ((cur
-then
) > ms
)
2164 LOG_ERROR("timed out while waiting for target %s",
2165 Jim_Nvp_value2name_simple(nvp_target_state
,state
)->name
);
2173 COMMAND_HANDLER(handle_halt_command
)
2177 struct target
*target
= get_current_target(CMD_CTX
);
2178 int retval
= target_halt(target
);
2179 if (ERROR_OK
!= retval
)
2184 unsigned wait_local
;
2185 retval
= parse_uint(CMD_ARGV
[0], &wait_local
);
2186 if (ERROR_OK
!= retval
)
2187 return ERROR_COMMAND_SYNTAX_ERROR
;
2192 return CALL_COMMAND_HANDLER(handle_wait_halt_command
);
2195 COMMAND_HANDLER(handle_soft_reset_halt_command
)
2197 struct target
*target
= get_current_target(CMD_CTX
);
2199 LOG_USER("requesting target halt and executing a soft reset");
2201 target
->type
->soft_reset_halt(target
);
2206 COMMAND_HANDLER(handle_reset_command
)
2209 return ERROR_COMMAND_SYNTAX_ERROR
;
2211 enum target_reset_mode reset_mode
= RESET_RUN
;
2215 n
= Jim_Nvp_name2value_simple(nvp_reset_modes
, CMD_ARGV
[0]);
2216 if ((n
->name
== NULL
) || (n
->value
== RESET_UNKNOWN
)) {
2217 return ERROR_COMMAND_SYNTAX_ERROR
;
2219 reset_mode
= n
->value
;
2222 /* reset *all* targets */
2223 return target_process_reset(CMD_CTX
, reset_mode
);
2227 COMMAND_HANDLER(handle_resume_command
)
2231 return ERROR_COMMAND_SYNTAX_ERROR
;
2233 struct target
*target
= get_current_target(CMD_CTX
);
2234 target_handle_event(target
, TARGET_EVENT_OLD_pre_resume
);
2236 /* with no CMD_ARGV, resume from current pc, addr = 0,
2237 * with one arguments, addr = CMD_ARGV[0],
2238 * handle breakpoints, not debugging */
2242 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], addr
);
2246 return target_resume(target
, current
, addr
, 1, 0);
2249 COMMAND_HANDLER(handle_step_command
)
2252 return ERROR_COMMAND_SYNTAX_ERROR
;
2256 /* with no CMD_ARGV, step from current pc, addr = 0,
2257 * with one argument addr = CMD_ARGV[0],
2258 * handle breakpoints, debugging */
2263 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], addr
);
2267 struct target
*target
= get_current_target(CMD_CTX
);
2269 return target
->type
->step(target
, current_pc
, addr
, 1);
2272 static void handle_md_output(struct command_context
*cmd_ctx
,
2273 struct target
*target
, uint32_t address
, unsigned size
,
2274 unsigned count
, const uint8_t *buffer
)
2276 const unsigned line_bytecnt
= 32;
2277 unsigned line_modulo
= line_bytecnt
/ size
;
2279 char output
[line_bytecnt
* 4 + 1];
2280 unsigned output_len
= 0;
2282 const char *value_fmt
;
2284 case 4: value_fmt
= "%8.8x "; break;
2285 case 2: value_fmt
= "%4.4x "; break;
2286 case 1: value_fmt
= "%2.2x "; break;
2288 /* "can't happen", caller checked */
2289 LOG_ERROR("invalid memory read size: %u", size
);
2293 for (unsigned i
= 0; i
< count
; i
++)
2295 if (i
% line_modulo
== 0)
2297 output_len
+= snprintf(output
+ output_len
,
2298 sizeof(output
) - output_len
,
2300 (unsigned)(address
+ (i
*size
)));
2304 const uint8_t *value_ptr
= buffer
+ i
* size
;
2306 case 4: value
= target_buffer_get_u32(target
, value_ptr
); break;
2307 case 2: value
= target_buffer_get_u16(target
, value_ptr
); break;
2308 case 1: value
= *value_ptr
;
2310 output_len
+= snprintf(output
+ output_len
,
2311 sizeof(output
) - output_len
,
2314 if ((i
% line_modulo
== line_modulo
- 1) || (i
== count
- 1))
2316 command_print(cmd_ctx
, "%s", output
);
2322 COMMAND_HANDLER(handle_md_command
)
2325 return ERROR_COMMAND_SYNTAX_ERROR
;
2328 switch (CMD_NAME
[2]) {
2329 case 'w': size
= 4; break;
2330 case 'h': size
= 2; break;
2331 case 'b': size
= 1; break;
2332 default: return ERROR_COMMAND_SYNTAX_ERROR
;
2335 bool physical
=strcmp(CMD_ARGV
[0], "phys")==0;
2336 int (*fn
)(struct target
*target
,
2337 uint32_t address
, uint32_t size_value
, uint32_t count
, uint8_t *buffer
);
2342 fn
=target_read_phys_memory
;
2345 fn
=target_read_memory
;
2347 if ((CMD_ARGC
< 1) || (CMD_ARGC
> 2))
2349 return ERROR_COMMAND_SYNTAX_ERROR
;
2353 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], address
);
2357 COMMAND_PARSE_NUMBER(uint
, CMD_ARGV
[1], count
);
2359 uint8_t *buffer
= calloc(count
, size
);
2361 struct target
*target
= get_current_target(CMD_CTX
);
2362 int retval
= fn(target
, address
, size
, count
, buffer
);
2363 if (ERROR_OK
== retval
)
2364 handle_md_output(CMD_CTX
, target
, address
, size
, count
, buffer
);
2371 typedef int (*target_write_fn
)(struct target
*target
,
2372 uint32_t address
, uint32_t size
, uint32_t count
, uint8_t *buffer
);
2374 static int target_write_memory_fast(struct target
*target
,
2375 uint32_t address
, uint32_t size
, uint32_t count
, uint8_t *buffer
)
2377 return target_write_buffer(target
, address
, size
* count
, buffer
);
2380 static int target_fill_mem(struct target
*target
,
2389 /* We have to write in reasonably large chunks to be able
2390 * to fill large memory areas with any sane speed */
2391 const unsigned chunk_size
= 16384;
2392 uint8_t *target_buf
= malloc(chunk_size
* data_size
);
2393 if (target_buf
== NULL
)
2395 LOG_ERROR("Out of memory");
2399 for (unsigned i
= 0; i
< chunk_size
; i
++)
2404 target_buffer_set_u32(target
, target_buf
+ i
*data_size
, b
);
2407 target_buffer_set_u16(target
, target_buf
+ i
*data_size
, b
);
2410 target_buffer_set_u8(target
, target_buf
+ i
*data_size
, b
);
2417 int retval
= ERROR_OK
;
2419 for (unsigned x
= 0; x
< c
; x
+= chunk_size
)
2423 if (current
> chunk_size
)
2425 current
= chunk_size
;
2427 retval
= fn(target
, address
+ x
* data_size
, data_size
, current
, target_buf
);
2428 if (retval
!= ERROR_OK
)
2432 /* avoid GDB timeouts */
2441 COMMAND_HANDLER(handle_mw_command
)
2445 return ERROR_COMMAND_SYNTAX_ERROR
;
2447 bool physical
=strcmp(CMD_ARGV
[0], "phys")==0;
2453 fn
=target_write_phys_memory
;
2456 fn
= target_write_memory_fast
;
2458 if ((CMD_ARGC
< 2) || (CMD_ARGC
> 3))
2459 return ERROR_COMMAND_SYNTAX_ERROR
;
2462 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], address
);
2465 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], value
);
2469 COMMAND_PARSE_NUMBER(uint
, CMD_ARGV
[2], count
);
2471 struct target
*target
= get_current_target(CMD_CTX
);
2473 switch (CMD_NAME
[2])
2485 return ERROR_COMMAND_SYNTAX_ERROR
;
2488 return target_fill_mem(target
, address
, fn
, wordsize
, value
, count
);
2491 static COMMAND_HELPER(parse_load_image_command_CMD_ARGV
, struct image
*image
,
2492 uint32_t *min_address
, uint32_t *max_address
)
2494 if (CMD_ARGC
< 1 || CMD_ARGC
> 5)
2495 return ERROR_COMMAND_SYNTAX_ERROR
;
2497 /* a base address isn't always necessary,
2498 * default to 0x0 (i.e. don't relocate) */
2502 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], addr
);
2503 image
->base_address
= addr
;
2504 image
->base_address_set
= 1;
2507 image
->base_address_set
= 0;
2509 image
->start_address_set
= 0;
2513 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[3], *min_address
);
2517 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[4], *max_address
);
2518 // use size (given) to find max (required)
2519 *max_address
+= *min_address
;
2522 if (*min_address
> *max_address
)
2523 return ERROR_COMMAND_SYNTAX_ERROR
;
2528 COMMAND_HANDLER(handle_load_image_command
)
2532 uint32_t image_size
;
2533 uint32_t min_address
= 0;
2534 uint32_t max_address
= 0xffffffff;
2538 int retval
= CALL_COMMAND_HANDLER(parse_load_image_command_CMD_ARGV
,
2539 &image
, &min_address
, &max_address
);
2540 if (ERROR_OK
!= retval
)
2543 struct target
*target
= get_current_target(CMD_CTX
);
2545 struct duration bench
;
2546 duration_start(&bench
);
2548 if (image_open(&image
, CMD_ARGV
[0], (CMD_ARGC
>= 3) ? CMD_ARGV
[2] : NULL
) != ERROR_OK
)
2555 for (i
= 0; i
< image
.num_sections
; i
++)
2557 buffer
= malloc(image
.sections
[i
].size
);
2560 command_print(CMD_CTX
,
2561 "error allocating buffer for section (%d bytes)",
2562 (int)(image
.sections
[i
].size
));
2566 if ((retval
= image_read_section(&image
, i
, 0x0, image
.sections
[i
].size
, buffer
, &buf_cnt
)) != ERROR_OK
)
2572 uint32_t offset
= 0;
2573 uint32_t length
= buf_cnt
;
2575 /* DANGER!!! beware of unsigned comparision here!!! */
2577 if ((image
.sections
[i
].base_address
+ buf_cnt
>= min_address
)&&
2578 (image
.sections
[i
].base_address
< max_address
))
2580 if (image
.sections
[i
].base_address
< min_address
)
2582 /* clip addresses below */
2583 offset
+= min_address
-image
.sections
[i
].base_address
;
2587 if (image
.sections
[i
].base_address
+ buf_cnt
> max_address
)
2589 length
-= (image
.sections
[i
].base_address
+ buf_cnt
)-max_address
;
2592 if ((retval
= target_write_buffer(target
, image
.sections
[i
].base_address
+ offset
, length
, buffer
+ offset
)) != ERROR_OK
)
2597 image_size
+= length
;
2598 command_print(CMD_CTX
, "%u bytes written at address 0x%8.8" PRIx32
"",
2599 (unsigned int)length
,
2600 image
.sections
[i
].base_address
+ offset
);
2606 if ((ERROR_OK
== retval
) && (duration_measure(&bench
) == ERROR_OK
))
2608 command_print(CMD_CTX
, "downloaded %" PRIu32
" bytes "
2609 "in %fs (%0.3f KiB/s)", image_size
,
2610 duration_elapsed(&bench
), duration_kbps(&bench
, image_size
));
2613 image_close(&image
);
2619 COMMAND_HANDLER(handle_dump_image_command
)
2621 struct fileio fileio
;
2622 uint8_t buffer
[560];
2623 int retval
, retvaltemp
;
2624 uint32_t address
, size
;
2625 struct duration bench
;
2626 struct target
*target
= get_current_target(CMD_CTX
);
2629 return ERROR_COMMAND_SYNTAX_ERROR
;
2631 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], address
);
2632 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[2], size
);
2634 retval
= fileio_open(&fileio
, CMD_ARGV
[0], FILEIO_WRITE
, FILEIO_BINARY
);
2635 if (retval
!= ERROR_OK
)
2638 duration_start(&bench
);
2643 size_t size_written
;
2644 uint32_t this_run_size
= (size
> 560) ? 560 : size
;
2645 retval
= target_read_buffer(target
, address
, this_run_size
, buffer
);
2646 if (retval
!= ERROR_OK
)
2651 retval
= fileio_write(&fileio
, this_run_size
, buffer
, &size_written
);
2652 if (retval
!= ERROR_OK
)
2657 size
-= this_run_size
;
2658 address
+= this_run_size
;
2661 if ((ERROR_OK
== retval
) && (duration_measure(&bench
) == ERROR_OK
))
2664 retval
= fileio_size(&fileio
, &filesize
);
2665 if (retval
!= ERROR_OK
)
2667 command_print(CMD_CTX
,
2668 "dumped %ld bytes in %fs (%0.3f KiB/s)", (long)filesize
,
2669 duration_elapsed(&bench
), duration_kbps(&bench
, filesize
));
2672 if ((retvaltemp
= fileio_close(&fileio
)) != ERROR_OK
)
2678 static COMMAND_HELPER(handle_verify_image_command_internal
, int verify
)
2682 uint32_t image_size
;
2685 uint32_t checksum
= 0;
2686 uint32_t mem_checksum
= 0;
2690 struct target
*target
= get_current_target(CMD_CTX
);
2694 return ERROR_COMMAND_SYNTAX_ERROR
;
2699 LOG_ERROR("no target selected");
2703 struct duration bench
;
2704 duration_start(&bench
);
2709 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], addr
);
2710 image
.base_address
= addr
;
2711 image
.base_address_set
= 1;
2715 image
.base_address_set
= 0;
2716 image
.base_address
= 0x0;
2719 image
.start_address_set
= 0;
2721 if ((retval
= image_open(&image
, CMD_ARGV
[0], (CMD_ARGC
== 3) ? CMD_ARGV
[2] : NULL
)) != ERROR_OK
)
2729 for (i
= 0; i
< image
.num_sections
; i
++)
2731 buffer
= malloc(image
.sections
[i
].size
);
2734 command_print(CMD_CTX
,
2735 "error allocating buffer for section (%d bytes)",
2736 (int)(image
.sections
[i
].size
));
2739 if ((retval
= image_read_section(&image
, i
, 0x0, image
.sections
[i
].size
, buffer
, &buf_cnt
)) != ERROR_OK
)
2747 /* calculate checksum of image */
2748 retval
= image_calculate_checksum(buffer
, buf_cnt
, &checksum
);
2749 if (retval
!= ERROR_OK
)
2755 retval
= target_checksum_memory(target
, image
.sections
[i
].base_address
, buf_cnt
, &mem_checksum
);
2756 if (retval
!= ERROR_OK
)
2762 if (checksum
!= mem_checksum
)
2764 /* failed crc checksum, fall back to a binary compare */
2769 LOG_ERROR("checksum mismatch - attempting binary compare");
2772 data
= (uint8_t*)malloc(buf_cnt
);
2774 /* Can we use 32bit word accesses? */
2776 int count
= buf_cnt
;
2777 if ((count
% 4) == 0)
2782 retval
= target_read_memory(target
, image
.sections
[i
].base_address
, size
, count
, data
);
2783 if (retval
== ERROR_OK
)
2786 for (t
= 0; t
< buf_cnt
; t
++)
2788 if (data
[t
] != buffer
[t
])
2790 command_print(CMD_CTX
,
2791 "diff %d address 0x%08x. Was 0x%02x instead of 0x%02x",
2793 (unsigned)(t
+ image
.sections
[i
].base_address
),
2798 command_print(CMD_CTX
, "More than 128 errors, the rest are not printed.");
2811 command_print(CMD_CTX
, "address 0x%08" PRIx32
" length 0x%08zx",
2812 image
.sections
[i
].base_address
,
2817 image_size
+= buf_cnt
;
2821 command_print(CMD_CTX
, "No more differences found.");
2826 retval
= ERROR_FAIL
;
2828 if ((ERROR_OK
== retval
) && (duration_measure(&bench
) == ERROR_OK
))
2830 command_print(CMD_CTX
, "verified %" PRIu32
" bytes "
2831 "in %fs (%0.3f KiB/s)", image_size
,
2832 duration_elapsed(&bench
), duration_kbps(&bench
, image_size
));
2835 image_close(&image
);
2840 COMMAND_HANDLER(handle_verify_image_command
)
2842 return CALL_COMMAND_HANDLER(handle_verify_image_command_internal
, 1);
2845 COMMAND_HANDLER(handle_test_image_command
)
2847 return CALL_COMMAND_HANDLER(handle_verify_image_command_internal
, 0);
2850 static int handle_bp_command_list(struct command_context
*cmd_ctx
)
2852 struct target
*target
= get_current_target(cmd_ctx
);
2853 struct breakpoint
*breakpoint
= target
->breakpoints
;
2856 if (breakpoint
->type
== BKPT_SOFT
)
2858 char* buf
= buf_to_str(breakpoint
->orig_instr
,
2859 breakpoint
->length
, 16);
2860 command_print(cmd_ctx
, "0x%8.8" PRIx32
", 0x%x, %i, 0x%s",
2861 breakpoint
->address
,
2863 breakpoint
->set
, buf
);
2868 command_print(cmd_ctx
, "0x%8.8" PRIx32
", 0x%x, %i",
2869 breakpoint
->address
,
2870 breakpoint
->length
, breakpoint
->set
);
2873 breakpoint
= breakpoint
->next
;
2878 static int handle_bp_command_set(struct command_context
*cmd_ctx
,
2879 uint32_t addr
, uint32_t length
, int hw
)
2881 struct target
*target
= get_current_target(cmd_ctx
);
2882 int retval
= breakpoint_add(target
, addr
, length
, hw
);
2883 if (ERROR_OK
== retval
)
2884 command_print(cmd_ctx
, "breakpoint set at 0x%8.8" PRIx32
"", addr
);
2886 LOG_ERROR("Failure setting breakpoint");
2890 COMMAND_HANDLER(handle_bp_command
)
2893 return handle_bp_command_list(CMD_CTX
);
2895 if (CMD_ARGC
< 2 || CMD_ARGC
> 3)
2897 command_print(CMD_CTX
, "usage: bp <address> <length> ['hw']");
2898 return ERROR_COMMAND_SYNTAX_ERROR
;
2902 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], addr
);
2904 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], length
);
2909 if (strcmp(CMD_ARGV
[2], "hw") == 0)
2912 return ERROR_COMMAND_SYNTAX_ERROR
;
2915 return handle_bp_command_set(CMD_CTX
, addr
, length
, hw
);
2918 COMMAND_HANDLER(handle_rbp_command
)
2921 return ERROR_COMMAND_SYNTAX_ERROR
;
2924 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], addr
);
2926 struct target
*target
= get_current_target(CMD_CTX
);
2927 breakpoint_remove(target
, addr
);
2932 COMMAND_HANDLER(handle_wp_command
)
2934 struct target
*target
= get_current_target(CMD_CTX
);
2938 struct watchpoint
*watchpoint
= target
->watchpoints
;
2942 command_print(CMD_CTX
, "address: 0x%8.8" PRIx32
2943 ", len: 0x%8.8" PRIx32
2944 ", r/w/a: %i, value: 0x%8.8" PRIx32
2945 ", mask: 0x%8.8" PRIx32
,
2946 watchpoint
->address
,
2948 (int)watchpoint
->rw
,
2951 watchpoint
= watchpoint
->next
;
2956 enum watchpoint_rw type
= WPT_ACCESS
;
2958 uint32_t length
= 0;
2959 uint32_t data_value
= 0x0;
2960 uint32_t data_mask
= 0xffffffff;
2965 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[4], data_mask
);
2968 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[3], data_value
);
2971 switch (CMD_ARGV
[2][0])
2983 LOG_ERROR("invalid watchpoint mode ('%c')", CMD_ARGV
[2][0]);
2984 return ERROR_COMMAND_SYNTAX_ERROR
;
2988 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], length
);
2989 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], addr
);
2993 command_print(CMD_CTX
, "usage: wp [address length "
2994 "[(r|w|a) [value [mask]]]]");
2995 return ERROR_COMMAND_SYNTAX_ERROR
;
2998 int retval
= watchpoint_add(target
, addr
, length
, type
,
2999 data_value
, data_mask
);
3000 if (ERROR_OK
!= retval
)
3001 LOG_ERROR("Failure setting watchpoints");
3006 COMMAND_HANDLER(handle_rwp_command
)
3009 return ERROR_COMMAND_SYNTAX_ERROR
;
3012 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], addr
);
3014 struct target
*target
= get_current_target(CMD_CTX
);
3015 watchpoint_remove(target
, addr
);
3022 * Translate a virtual address to a physical address.
3024 * The low-level target implementation must have logged a detailed error
3025 * which is forwarded to telnet/GDB session.
3027 COMMAND_HANDLER(handle_virt2phys_command
)
3030 return ERROR_COMMAND_SYNTAX_ERROR
;
3033 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], va
);
3036 struct target
*target
= get_current_target(CMD_CTX
);
3037 int retval
= target
->type
->virt2phys(target
, va
, &pa
);
3038 if (retval
== ERROR_OK
)
3039 command_print(CMD_CTX
, "Physical address 0x%08" PRIx32
"", pa
);
3044 static void writeData(FILE *f
, const void *data
, size_t len
)
3046 size_t written
= fwrite(data
, 1, len
, f
);
3048 LOG_ERROR("failed to write %zu bytes: %s", len
, strerror(errno
));
3051 static void writeLong(FILE *f
, int l
)
3054 for (i
= 0; i
< 4; i
++)
3056 char c
= (l
>> (i
*8))&0xff;
3057 writeData(f
, &c
, 1);
3062 static void writeString(FILE *f
, char *s
)
3064 writeData(f
, s
, strlen(s
));
3067 /* Dump a gmon.out histogram file. */
3068 static void writeGmon(uint32_t *samples
, uint32_t sampleNum
, const char *filename
)
3071 FILE *f
= fopen(filename
, "w");
3074 writeString(f
, "gmon");
3075 writeLong(f
, 0x00000001); /* Version */
3076 writeLong(f
, 0); /* padding */
3077 writeLong(f
, 0); /* padding */
3078 writeLong(f
, 0); /* padding */
3080 uint8_t zero
= 0; /* GMON_TAG_TIME_HIST */
3081 writeData(f
, &zero
, 1);
3083 /* figure out bucket size */
3084 uint32_t min
= samples
[0];
3085 uint32_t max
= samples
[0];
3086 for (i
= 0; i
< sampleNum
; i
++)
3088 if (min
> samples
[i
])
3092 if (max
< samples
[i
])
3098 int addressSpace
= (max
-min
+ 1);
3100 static const uint32_t maxBuckets
= 16 * 1024; /* maximum buckets. */
3101 uint32_t length
= addressSpace
;
3102 if (length
> maxBuckets
)
3104 length
= maxBuckets
;
3106 int *buckets
= malloc(sizeof(int)*length
);
3107 if (buckets
== NULL
)
3112 memset(buckets
, 0, sizeof(int)*length
);
3113 for (i
= 0; i
< sampleNum
;i
++)
3115 uint32_t address
= samples
[i
];
3116 long long a
= address
-min
;
3117 long long b
= length
-1;
3118 long long c
= addressSpace
-1;
3119 int index_t
= (a
*b
)/c
; /* danger!!!! int32 overflows */
3123 /* append binary memory gmon.out &profile_hist_hdr ((char*)&profile_hist_hdr + sizeof(struct gmon_hist_hdr)) */
3124 writeLong(f
, min
); /* low_pc */
3125 writeLong(f
, max
); /* high_pc */
3126 writeLong(f
, length
); /* # of samples */
3127 writeLong(f
, 100); /* KLUDGE! We lie, ca. 100Hz best case. */
3128 writeString(f
, "seconds");
3129 for (i
= 0; i
< (15-strlen("seconds")); i
++)
3130 writeData(f
, &zero
, 1);
3131 writeString(f
, "s");
3133 /*append binary memory gmon.out profile_hist_data (profile_hist_data + profile_hist_hdr.hist_size) */
3135 char *data
= malloc(2*length
);
3138 for (i
= 0; i
< length
;i
++)
3147 data
[i
*2 + 1]=(val
>> 8)&0xff;
3150 writeData(f
, data
, length
* 2);
3160 /* profiling samples the CPU PC as quickly as OpenOCD is able,
3161 * which will be used as a random sampling of PC */
3162 COMMAND_HANDLER(handle_profile_command
)
3164 struct target
*target
= get_current_target(CMD_CTX
);
3165 struct timeval timeout
, now
;
3167 gettimeofday(&timeout
, NULL
);
3170 return ERROR_COMMAND_SYNTAX_ERROR
;
3173 COMMAND_PARSE_NUMBER(uint
, CMD_ARGV
[0], offset
);
3175 timeval_add_time(&timeout
, offset
, 0);
3178 * @todo: Some cores let us sample the PC without the
3179 * annoying halt/resume step; for example, ARMv7 PCSR.
3180 * Provide a way to use that more efficient mechanism.
3183 command_print(CMD_CTX
, "Starting profiling. Halting and resuming the target as often as we can...");
3185 static const int maxSample
= 10000;
3186 uint32_t *samples
= malloc(sizeof(uint32_t)*maxSample
);
3187 if (samples
== NULL
)
3191 /* hopefully it is safe to cache! We want to stop/restart as quickly as possible. */
3192 struct reg
*reg
= register_get_by_name(target
->reg_cache
, "pc", 1);
3197 target_poll(target
);
3198 if (target
->state
== TARGET_HALTED
)
3200 uint32_t t
=*((uint32_t *)reg
->value
);
3201 samples
[numSamples
++]=t
;
3202 retval
= target_resume(target
, 1, 0, 0, 0); /* current pc, addr = 0, do not handle breakpoints, not debugging */
3203 target_poll(target
);
3204 alive_sleep(10); /* sleep 10ms, i.e. <100 samples/second. */
3205 } else if (target
->state
== TARGET_RUNNING
)
3207 /* We want to quickly sample the PC. */
3208 if ((retval
= target_halt(target
)) != ERROR_OK
)
3215 command_print(CMD_CTX
, "Target not halted or running");
3219 if (retval
!= ERROR_OK
)
3224 gettimeofday(&now
, NULL
);
3225 if ((numSamples
>= maxSample
) || ((now
.tv_sec
>= timeout
.tv_sec
) && (now
.tv_usec
>= timeout
.tv_usec
)))
3227 command_print(CMD_CTX
, "Profiling completed. %d samples.", numSamples
);
3228 if ((retval
= target_poll(target
)) != ERROR_OK
)
3233 if (target
->state
== TARGET_HALTED
)
3235 target_resume(target
, 1, 0, 0, 0); /* current pc, addr = 0, do not handle breakpoints, not debugging */
3237 if ((retval
= target_poll(target
)) != ERROR_OK
)
3242 writeGmon(samples
, numSamples
, CMD_ARGV
[1]);
3243 command_print(CMD_CTX
, "Wrote %s", CMD_ARGV
[1]);
3252 static int new_int_array_element(Jim_Interp
* interp
, const char *varname
, int idx
, uint32_t val
)
3255 Jim_Obj
*nameObjPtr
, *valObjPtr
;
3258 namebuf
= alloc_printf("%s(%d)", varname
, idx
);
3262 nameObjPtr
= Jim_NewStringObj(interp
, namebuf
, -1);
3263 valObjPtr
= Jim_NewIntObj(interp
, val
);
3264 if (!nameObjPtr
|| !valObjPtr
)
3270 Jim_IncrRefCount(nameObjPtr
);
3271 Jim_IncrRefCount(valObjPtr
);
3272 result
= Jim_SetVariable(interp
, nameObjPtr
, valObjPtr
);
3273 Jim_DecrRefCount(interp
, nameObjPtr
);
3274 Jim_DecrRefCount(interp
, valObjPtr
);
3276 /* printf("%s(%d) <= 0%08x\n", varname, idx, val); */
3280 static int jim_mem2array(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
3282 struct command_context
*context
;
3283 struct target
*target
;
3285 context
= current_command_context(interp
);
3286 assert (context
!= NULL
);
3288 target
= get_current_target(context
);
3291 LOG_ERROR("mem2array: no current target");
3295 return target_mem2array(interp
, target
, argc
-1, argv
+ 1);
3298 static int target_mem2array(Jim_Interp
*interp
, struct target
*target
, int argc
, Jim_Obj
*const *argv
)
3306 const char *varname
;
3310 /* argv[1] = name of array to receive the data
3311 * argv[2] = desired width
3312 * argv[3] = memory address
3313 * argv[4] = count of times to read
3316 Jim_WrongNumArgs(interp
, 1, argv
, "varname width addr nelems");
3319 varname
= Jim_GetString(argv
[0], &len
);
3320 /* given "foo" get space for worse case "foo(%d)" .. add 20 */
3322 e
= Jim_GetLong(interp
, argv
[1], &l
);
3328 e
= Jim_GetLong(interp
, argv
[2], &l
);
3333 e
= Jim_GetLong(interp
, argv
[3], &l
);
3349 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3350 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "Invalid width param, must be 8/16/32", NULL
);
3354 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3355 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "mem2array: zero width read?", NULL
);
3358 if ((addr
+ (len
* width
)) < addr
) {
3359 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3360 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "mem2array: addr + len - wraps to zero?", NULL
);
3363 /* absurd transfer size? */
3365 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3366 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "mem2array: absurd > 64K item request", NULL
);
3371 ((width
== 2) && ((addr
& 1) == 0)) ||
3372 ((width
== 4) && ((addr
& 3) == 0))) {
3376 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3377 sprintf(buf
, "mem2array address: 0x%08" PRIx32
" is not aligned for %" PRId32
" byte reads",
3380 Jim_AppendStrings(interp
, Jim_GetResult(interp
), buf
, NULL
);
3389 size_t buffersize
= 4096;
3390 uint8_t *buffer
= malloc(buffersize
);
3397 /* Slurp... in buffer size chunks */
3399 count
= len
; /* in objects.. */
3400 if (count
> (buffersize
/width
)) {
3401 count
= (buffersize
/width
);
3404 retval
= target_read_memory(target
, addr
, width
, count
, buffer
);
3405 if (retval
!= ERROR_OK
) {
3407 LOG_ERROR("mem2array: Read @ 0x%08x, w=%d, cnt=%d, failed",
3411 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3412 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "mem2array: cannot read memory", NULL
);
3416 v
= 0; /* shut up gcc */
3417 for (i
= 0 ;i
< count
;i
++, n
++) {
3420 v
= target_buffer_get_u32(target
, &buffer
[i
*width
]);
3423 v
= target_buffer_get_u16(target
, &buffer
[i
*width
]);
3426 v
= buffer
[i
] & 0x0ff;
3429 new_int_array_element(interp
, varname
, n
, v
);
3437 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3442 static int get_int_array_element(Jim_Interp
* interp
, const char *varname
, int idx
, uint32_t *val
)
3445 Jim_Obj
*nameObjPtr
, *valObjPtr
;
3449 namebuf
= alloc_printf("%s(%d)", varname
, idx
);
3453 nameObjPtr
= Jim_NewStringObj(interp
, namebuf
, -1);
3460 Jim_IncrRefCount(nameObjPtr
);
3461 valObjPtr
= Jim_GetVariable(interp
, nameObjPtr
, JIM_ERRMSG
);
3462 Jim_DecrRefCount(interp
, nameObjPtr
);
3464 if (valObjPtr
== NULL
)
3467 result
= Jim_GetLong(interp
, valObjPtr
, &l
);
3468 /* printf("%s(%d) => 0%08x\n", varname, idx, val); */
3473 static int jim_array2mem(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
3475 struct command_context
*context
;
3476 struct target
*target
;
3478 context
= current_command_context(interp
);
3479 assert (context
!= NULL
);
3481 target
= get_current_target(context
);
3482 if (target
== NULL
) {
3483 LOG_ERROR("array2mem: no current target");
3487 return target_array2mem(interp
,target
, argc
-1, argv
+ 1);
3490 static int target_array2mem(Jim_Interp
*interp
, struct target
*target
,
3491 int argc
, Jim_Obj
*const *argv
)
3499 const char *varname
;
3503 /* argv[1] = name of array to get the data
3504 * argv[2] = desired width
3505 * argv[3] = memory address
3506 * argv[4] = count to write
3509 Jim_WrongNumArgs(interp
, 0, argv
, "varname width addr nelems");
3512 varname
= Jim_GetString(argv
[0], &len
);
3513 /* given "foo" get space for worse case "foo(%d)" .. add 20 */
3515 e
= Jim_GetLong(interp
, argv
[1], &l
);
3521 e
= Jim_GetLong(interp
, argv
[2], &l
);
3526 e
= Jim_GetLong(interp
, argv
[3], &l
);
3542 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3543 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "Invalid width param, must be 8/16/32", NULL
);
3547 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3548 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "array2mem: zero width read?", NULL
);
3551 if ((addr
+ (len
* width
)) < addr
) {
3552 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3553 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "array2mem: addr + len - wraps to zero?", NULL
);
3556 /* absurd transfer size? */
3558 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3559 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "array2mem: absurd > 64K item request", NULL
);
3564 ((width
== 2) && ((addr
& 1) == 0)) ||
3565 ((width
== 4) && ((addr
& 3) == 0))) {
3569 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3570 sprintf(buf
, "array2mem address: 0x%08x is not aligned for %d byte reads",
3573 Jim_AppendStrings(interp
, Jim_GetResult(interp
), buf
, NULL
);
3584 size_t buffersize
= 4096;
3585 uint8_t *buffer
= malloc(buffersize
);
3590 /* Slurp... in buffer size chunks */
3592 count
= len
; /* in objects.. */
3593 if (count
> (buffersize
/width
)) {
3594 count
= (buffersize
/width
);
3597 v
= 0; /* shut up gcc */
3598 for (i
= 0 ;i
< count
;i
++, n
++) {
3599 get_int_array_element(interp
, varname
, n
, &v
);
3602 target_buffer_set_u32(target
, &buffer
[i
*width
], v
);
3605 target_buffer_set_u16(target
, &buffer
[i
*width
], v
);
3608 buffer
[i
] = v
& 0x0ff;
3614 retval
= target_write_memory(target
, addr
, width
, count
, buffer
);
3615 if (retval
!= ERROR_OK
) {
3617 LOG_ERROR("array2mem: Write @ 0x%08x, w=%d, cnt=%d, failed",
3621 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3622 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "array2mem: cannot read memory", NULL
);
3630 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3635 /* FIX? should we propagate errors here rather than printing them
3638 void target_handle_event(struct target
*target
, enum target_event e
)
3640 struct target_event_action
*teap
;
3642 for (teap
= target
->event_action
; teap
!= NULL
; teap
= teap
->next
) {
3643 if (teap
->event
== e
) {
3644 LOG_DEBUG("target: (%d) %s (%s) event: %d (%s) action: %s",
3645 target
->target_number
,
3646 target_name(target
),
3647 target_type_name(target
),
3649 Jim_Nvp_value2name_simple(nvp_target_event
, e
)->name
,
3650 Jim_GetString(teap
->body
, NULL
));
3651 if (Jim_EvalObj(teap
->interp
, teap
->body
) != JIM_OK
)
3653 Jim_MakeErrorMessage(teap
->interp
);
3654 command_print(NULL
,"%s\n", Jim_GetString(Jim_GetResult(teap
->interp
), NULL
));
3661 * Returns true only if the target has a handler for the specified event.
3663 bool target_has_event_action(struct target
*target
, enum target_event event
)
3665 struct target_event_action
*teap
;
3667 for (teap
= target
->event_action
; teap
!= NULL
; teap
= teap
->next
) {
3668 if (teap
->event
== event
)
3674 enum target_cfg_param
{
3677 TCFG_WORK_AREA_VIRT
,
3678 TCFG_WORK_AREA_PHYS
,
3679 TCFG_WORK_AREA_SIZE
,
3680 TCFG_WORK_AREA_BACKUP
,
3684 TCFG_CHAIN_POSITION
,
3687 static Jim_Nvp nvp_config_opts
[] = {
3688 { .name
= "-type", .value
= TCFG_TYPE
},
3689 { .name
= "-event", .value
= TCFG_EVENT
},
3690 { .name
= "-work-area-virt", .value
= TCFG_WORK_AREA_VIRT
},
3691 { .name
= "-work-area-phys", .value
= TCFG_WORK_AREA_PHYS
},
3692 { .name
= "-work-area-size", .value
= TCFG_WORK_AREA_SIZE
},
3693 { .name
= "-work-area-backup", .value
= TCFG_WORK_AREA_BACKUP
},
3694 { .name
= "-endian" , .value
= TCFG_ENDIAN
},
3695 { .name
= "-variant", .value
= TCFG_VARIANT
},
3696 { .name
= "-coreid", .value
= TCFG_COREID
},
3697 { .name
= "-chain-position", .value
= TCFG_CHAIN_POSITION
},
3699 { .name
= NULL
, .value
= -1 }
3702 static int target_configure(Jim_GetOptInfo
*goi
, struct target
*target
)
3710 /* parse config or cget options ... */
3711 while (goi
->argc
> 0) {
3712 Jim_SetEmptyResult(goi
->interp
);
3713 /* Jim_GetOpt_Debug(goi); */
3715 if (target
->type
->target_jim_configure
) {
3716 /* target defines a configure function */
3717 /* target gets first dibs on parameters */
3718 e
= (*(target
->type
->target_jim_configure
))(target
, goi
);
3727 /* otherwise we 'continue' below */
3729 e
= Jim_GetOpt_Nvp(goi
, nvp_config_opts
, &n
);
3731 Jim_GetOpt_NvpUnknown(goi
, nvp_config_opts
, 0);
3737 if (goi
->isconfigure
) {
3738 Jim_SetResultFormatted(goi
->interp
,
3739 "not settable: %s", n
->name
);
3743 if (goi
->argc
!= 0) {
3744 Jim_WrongNumArgs(goi
->interp
,
3745 goi
->argc
, goi
->argv
,
3750 Jim_SetResultString(goi
->interp
,
3751 target_type_name(target
), -1);
3755 if (goi
->argc
== 0) {
3756 Jim_WrongNumArgs(goi
->interp
, goi
->argc
, goi
->argv
, "-event ?event-name? ...");
3760 e
= Jim_GetOpt_Nvp(goi
, nvp_target_event
, &n
);
3762 Jim_GetOpt_NvpUnknown(goi
, nvp_target_event
, 1);
3766 if (goi
->isconfigure
) {
3767 if (goi
->argc
!= 1) {
3768 Jim_WrongNumArgs(goi
->interp
, goi
->argc
, goi
->argv
, "-event ?event-name? ?EVENT-BODY?");
3772 if (goi
->argc
!= 0) {
3773 Jim_WrongNumArgs(goi
->interp
, goi
->argc
, goi
->argv
, "-event ?event-name?");
3779 struct target_event_action
*teap
;
3781 teap
= target
->event_action
;
3782 /* replace existing? */
3784 if (teap
->event
== (enum target_event
)n
->value
) {
3790 if (goi
->isconfigure
) {
3791 bool replace
= true;
3794 teap
= calloc(1, sizeof(*teap
));
3797 teap
->event
= n
->value
;
3798 teap
->interp
= goi
->interp
;
3799 Jim_GetOpt_Obj(goi
, &o
);
3801 Jim_DecrRefCount(teap
->interp
, teap
->body
);
3803 teap
->body
= Jim_DuplicateObj(goi
->interp
, o
);
3806 * Tcl/TK - "tk events" have a nice feature.
3807 * See the "BIND" command.
3808 * We should support that here.
3809 * You can specify %X and %Y in the event code.
3810 * The idea is: %T - target name.
3811 * The idea is: %N - target number
3812 * The idea is: %E - event name.
3814 Jim_IncrRefCount(teap
->body
);
3818 /* add to head of event list */
3819 teap
->next
= target
->event_action
;
3820 target
->event_action
= teap
;
3822 Jim_SetEmptyResult(goi
->interp
);
3826 Jim_SetEmptyResult(goi
->interp
);
3828 Jim_SetResult(goi
->interp
, Jim_DuplicateObj(goi
->interp
, teap
->body
));
3835 case TCFG_WORK_AREA_VIRT
:
3836 if (goi
->isconfigure
) {
3837 target_free_all_working_areas(target
);
3838 e
= Jim_GetOpt_Wide(goi
, &w
);
3842 target
->working_area_virt
= w
;
3843 target
->working_area_virt_spec
= true;
3845 if (goi
->argc
!= 0) {
3849 Jim_SetResult(goi
->interp
, Jim_NewIntObj(goi
->interp
, target
->working_area_virt
));
3853 case TCFG_WORK_AREA_PHYS
:
3854 if (goi
->isconfigure
) {
3855 target_free_all_working_areas(target
);
3856 e
= Jim_GetOpt_Wide(goi
, &w
);
3860 target
->working_area_phys
= w
;
3861 target
->working_area_phys_spec
= true;
3863 if (goi
->argc
!= 0) {
3867 Jim_SetResult(goi
->interp
, Jim_NewIntObj(goi
->interp
, target
->working_area_phys
));
3871 case TCFG_WORK_AREA_SIZE
:
3872 if (goi
->isconfigure
) {
3873 target_free_all_working_areas(target
);
3874 e
= Jim_GetOpt_Wide(goi
, &w
);
3878 target
->working_area_size
= w
;
3880 if (goi
->argc
!= 0) {
3884 Jim_SetResult(goi
->interp
, Jim_NewIntObj(goi
->interp
, target
->working_area_size
));
3888 case TCFG_WORK_AREA_BACKUP
:
3889 if (goi
->isconfigure
) {
3890 target_free_all_working_areas(target
);
3891 e
= Jim_GetOpt_Wide(goi
, &w
);
3895 /* make this exactly 1 or 0 */
3896 target
->backup_working_area
= (!!w
);
3898 if (goi
->argc
!= 0) {
3902 Jim_SetResult(goi
->interp
, Jim_NewIntObj(goi
->interp
, target
->backup_working_area
));
3903 /* loop for more e*/
3907 if (goi
->isconfigure
) {
3908 e
= Jim_GetOpt_Nvp(goi
, nvp_target_endian
, &n
);
3910 Jim_GetOpt_NvpUnknown(goi
, nvp_target_endian
, 1);
3913 target
->endianness
= n
->value
;
3915 if (goi
->argc
!= 0) {
3919 n
= Jim_Nvp_value2name_simple(nvp_target_endian
, target
->endianness
);
3920 if (n
->name
== NULL
) {
3921 target
->endianness
= TARGET_LITTLE_ENDIAN
;
3922 n
= Jim_Nvp_value2name_simple(nvp_target_endian
, target
->endianness
);
3924 Jim_SetResultString(goi
->interp
, n
->name
, -1);
3929 if (goi
->isconfigure
) {
3930 if (goi
->argc
< 1) {
3931 Jim_SetResultFormatted(goi
->interp
,
3936 if (target
->variant
) {
3937 free((void *)(target
->variant
));
3939 e
= Jim_GetOpt_String(goi
, &cp
, NULL
);
3940 target
->variant
= strdup(cp
);
3942 if (goi
->argc
!= 0) {
3946 Jim_SetResultString(goi
->interp
, target
->variant
,-1);
3951 if (goi
->isconfigure
) {
3952 e
= Jim_GetOpt_Wide(goi
, &w
);
3956 target
->coreid
= (int)w
;
3958 if (goi
->argc
!= 0) {
3962 Jim_SetResult(goi
->interp
, Jim_NewIntObj(goi
->interp
, target
->working_area_size
));
3966 case TCFG_CHAIN_POSITION
:
3967 if (goi
->isconfigure
) {
3969 struct jtag_tap
*tap
;
3970 target_free_all_working_areas(target
);
3971 e
= Jim_GetOpt_Obj(goi
, &o_t
);
3975 tap
= jtag_tap_by_jim_obj(goi
->interp
, o_t
);
3979 /* make this exactly 1 or 0 */
3982 if (goi
->argc
!= 0) {
3986 Jim_SetResultString(goi
->interp
, target
->tap
->dotted_name
, -1);
3987 /* loop for more e*/
3990 } /* while (goi->argc) */
3993 /* done - we return */
3998 jim_target_configure(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4002 Jim_GetOpt_Setup(&goi
, interp
, argc
- 1, argv
+ 1);
4003 goi
.isconfigure
= !strcmp(Jim_GetString(argv
[0], NULL
), "configure");
4004 int need_args
= 1 + goi
.isconfigure
;
4005 if (goi
.argc
< need_args
)
4007 Jim_WrongNumArgs(goi
.interp
, goi
.argc
, goi
.argv
,
4009 ? "missing: -option VALUE ..."
4010 : "missing: -option ...");
4013 struct target
*target
= Jim_CmdPrivData(goi
.interp
);
4014 return target_configure(&goi
, target
);
4017 static int jim_target_mw(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4019 const char *cmd_name
= Jim_GetString(argv
[0], NULL
);
4022 Jim_GetOpt_Setup(&goi
, interp
, argc
- 1, argv
+ 1);
4024 if (goi
.argc
< 2 || goi
.argc
> 4)
4026 Jim_SetResultFormatted(goi
.interp
,
4027 "usage: %s [phys] <address> <data> [<count>]", cmd_name
);
4032 fn
= target_write_memory_fast
;
4035 if (strcmp(Jim_GetString(argv
[1], NULL
), "phys") == 0)
4038 struct Jim_Obj
*obj
;
4039 e
= Jim_GetOpt_Obj(&goi
, &obj
);
4043 fn
= target_write_phys_memory
;
4047 e
= Jim_GetOpt_Wide(&goi
, &a
);
4052 e
= Jim_GetOpt_Wide(&goi
, &b
);
4059 e
= Jim_GetOpt_Wide(&goi
, &c
);
4064 /* all args must be consumed */
4070 struct target
*target
= Jim_CmdPrivData(goi
.interp
);
4072 if (strcasecmp(cmd_name
, "mww") == 0) {
4075 else if (strcasecmp(cmd_name
, "mwh") == 0) {
4078 else if (strcasecmp(cmd_name
, "mwb") == 0) {
4081 LOG_ERROR("command '%s' unknown: ", cmd_name
);
4085 return (target_fill_mem(target
, a
, fn
, data_size
, b
, c
) == ERROR_OK
) ? JIM_OK
: JIM_ERR
;
4088 static int jim_target_md(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4090 const char *cmd_name
= Jim_GetString(argv
[0], NULL
);
4093 Jim_GetOpt_Setup(&goi
, interp
, argc
- 1, argv
+ 1);
4095 if ((goi
.argc
< 1) || (goi
.argc
> 3))
4097 Jim_SetResultFormatted(goi
.interp
,
4098 "usage: %s [phys] <address> [<count>]", cmd_name
);
4102 int (*fn
)(struct target
*target
,
4103 uint32_t address
, uint32_t size
, uint32_t count
, uint8_t *buffer
);
4104 fn
=target_read_memory
;
4107 if (strcmp(Jim_GetString(argv
[1], NULL
), "phys") == 0)
4110 struct Jim_Obj
*obj
;
4111 e
= Jim_GetOpt_Obj(&goi
, &obj
);
4115 fn
=target_read_phys_memory
;
4119 e
= Jim_GetOpt_Wide(&goi
, &a
);
4124 if (goi
.argc
== 1) {
4125 e
= Jim_GetOpt_Wide(&goi
, &c
);
4133 /* all args must be consumed */
4139 jim_wide b
= 1; /* shut up gcc */
4140 if (strcasecmp(cmd_name
, "mdw") == 0)
4142 else if (strcasecmp(cmd_name
, "mdh") == 0)
4144 else if (strcasecmp(cmd_name
, "mdb") == 0)
4147 LOG_ERROR("command '%s' unknown: ", cmd_name
);
4151 /* convert count to "bytes" */
4154 struct target
*target
= Jim_CmdPrivData(goi
.interp
);
4155 uint8_t target_buf
[32];
4162 e
= fn(target
, a
, b
, y
/ b
, target_buf
);
4163 if (e
!= ERROR_OK
) {
4165 snprintf(tmp
, sizeof(tmp
), "%08lx", (long)a
);
4166 Jim_SetResultFormatted(interp
, "error reading target @ 0x%s", tmp
);
4170 command_print(NULL
, "0x%08x ", (int)(a
));
4173 for (x
= 0; x
< 16 && x
< y
; x
+= 4)
4175 z
= target_buffer_get_u32(target
, &(target_buf
[ x
]));
4176 command_print(NULL
, "%08x ", (int)(z
));
4178 for (; (x
< 16) ; x
+= 4) {
4179 command_print(NULL
, " ");
4183 for (x
= 0; x
< 16 && x
< y
; x
+= 2)
4185 z
= target_buffer_get_u16(target
, &(target_buf
[ x
]));
4186 command_print(NULL
, "%04x ", (int)(z
));
4188 for (; (x
< 16) ; x
+= 2) {
4189 command_print(NULL
, " ");
4194 for (x
= 0 ; (x
< 16) && (x
< y
) ; x
+= 1) {
4195 z
= target_buffer_get_u8(target
, &(target_buf
[ x
]));
4196 command_print(NULL
, "%02x ", (int)(z
));
4198 for (; (x
< 16) ; x
+= 1) {
4199 command_print(NULL
, " ");
4203 /* ascii-ify the bytes */
4204 for (x
= 0 ; x
< y
; x
++) {
4205 if ((target_buf
[x
] >= 0x20) &&
4206 (target_buf
[x
] <= 0x7e)) {
4210 target_buf
[x
] = '.';
4215 target_buf
[x
] = ' ';
4220 /* print - with a newline */
4221 command_print(NULL
, "%s\n", target_buf
);
4229 static int jim_target_mem2array(Jim_Interp
*interp
,
4230 int argc
, Jim_Obj
*const *argv
)
4232 struct target
*target
= Jim_CmdPrivData(interp
);
4233 return target_mem2array(interp
, target
, argc
- 1, argv
+ 1);
4236 static int jim_target_array2mem(Jim_Interp
*interp
,
4237 int argc
, Jim_Obj
*const *argv
)
4239 struct target
*target
= Jim_CmdPrivData(interp
);
4240 return target_array2mem(interp
, target
, argc
- 1, argv
+ 1);
4243 static int jim_target_tap_disabled(Jim_Interp
*interp
)
4245 Jim_SetResultFormatted(interp
, "[TAP is disabled]");
4249 static int jim_target_examine(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4253 Jim_WrongNumArgs(interp
, 1, argv
, "[no parameters]");
4256 struct target
*target
= Jim_CmdPrivData(interp
);
4257 if (!target
->tap
->enabled
)
4258 return jim_target_tap_disabled(interp
);
4260 int e
= target
->type
->examine(target
);
4268 static int jim_target_halt_gdb(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4272 Jim_WrongNumArgs(interp
, 1, argv
, "[no parameters]");
4275 struct target
*target
= Jim_CmdPrivData(interp
);
4277 if (target_call_event_callbacks(target
, TARGET_EVENT_GDB_HALT
) != ERROR_OK
)
4283 static int jim_target_poll(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4287 Jim_WrongNumArgs(interp
, 1, argv
, "[no parameters]");
4290 struct target
*target
= Jim_CmdPrivData(interp
);
4291 if (!target
->tap
->enabled
)
4292 return jim_target_tap_disabled(interp
);
4295 if (!(target_was_examined(target
))) {
4296 e
= ERROR_TARGET_NOT_EXAMINED
;
4298 e
= target
->type
->poll(target
);
4307 static int jim_target_reset(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4310 Jim_GetOpt_Setup(&goi
, interp
, argc
- 1, argv
+ 1);
4314 Jim_WrongNumArgs(interp
, 0, argv
,
4315 "([tT]|[fF]|assert|deassert) BOOL");
4320 int e
= Jim_GetOpt_Nvp(&goi
, nvp_assert
, &n
);
4323 Jim_GetOpt_NvpUnknown(&goi
, nvp_assert
, 1);
4326 /* the halt or not param */
4328 e
= Jim_GetOpt_Wide(&goi
, &a
);
4332 struct target
*target
= Jim_CmdPrivData(goi
.interp
);
4333 if (!target
->tap
->enabled
)
4334 return jim_target_tap_disabled(interp
);
4335 if (!(target_was_examined(target
)))
4337 LOG_ERROR("Target not examined yet");
4338 return ERROR_TARGET_NOT_EXAMINED
;
4340 if (!target
->type
->assert_reset
|| !target
->type
->deassert_reset
)
4342 Jim_SetResultFormatted(interp
,
4343 "No target-specific reset for %s",
4344 target_name(target
));
4347 /* determine if we should halt or not. */
4348 target
->reset_halt
= !!a
;
4349 /* When this happens - all workareas are invalid. */
4350 target_free_all_working_areas_restore(target
, 0);
4353 if (n
->value
== NVP_ASSERT
) {
4354 e
= target
->type
->assert_reset(target
);
4356 e
= target
->type
->deassert_reset(target
);
4358 return (e
== ERROR_OK
) ? JIM_OK
: JIM_ERR
;
4361 static int jim_target_halt(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4364 Jim_WrongNumArgs(interp
, 1, argv
, "[no parameters]");
4367 struct target
*target
= Jim_CmdPrivData(interp
);
4368 if (!target
->tap
->enabled
)
4369 return jim_target_tap_disabled(interp
);
4370 int e
= target
->type
->halt(target
);
4371 return (e
== ERROR_OK
) ? JIM_OK
: JIM_ERR
;
4374 static int jim_target_wait_state(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4377 Jim_GetOpt_Setup(&goi
, interp
, argc
- 1, argv
+ 1);
4379 /* params: <name> statename timeoutmsecs */
4382 const char *cmd_name
= Jim_GetString(argv
[0], NULL
);
4383 Jim_SetResultFormatted(goi
.interp
,
4384 "%s <state_name> <timeout_in_msec>", cmd_name
);
4389 int e
= Jim_GetOpt_Nvp(&goi
, nvp_target_state
, &n
);
4391 Jim_GetOpt_NvpUnknown(&goi
, nvp_target_state
,1);
4395 e
= Jim_GetOpt_Wide(&goi
, &a
);
4399 struct target
*target
= Jim_CmdPrivData(interp
);
4400 if (!target
->tap
->enabled
)
4401 return jim_target_tap_disabled(interp
);
4403 e
= target_wait_state(target
, n
->value
, a
);
4406 Jim_Obj
*eObj
= Jim_NewIntObj(interp
, e
);
4407 Jim_SetResultFormatted(goi
.interp
,
4408 "target: %s wait %s fails (%#s) %s",
4409 target_name(target
), n
->name
,
4410 eObj
, target_strerror_safe(e
));
4411 Jim_FreeNewObj(interp
, eObj
);
4416 /* List for human, Events defined for this target.
4417 * scripts/programs should use 'name cget -event NAME'
4419 static int jim_target_event_list(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4421 struct command_context
*cmd_ctx
= current_command_context(interp
);
4422 assert (cmd_ctx
!= NULL
);
4424 struct target
*target
= Jim_CmdPrivData(interp
);
4425 struct target_event_action
*teap
= target
->event_action
;
4426 command_print(cmd_ctx
, "Event actions for target (%d) %s\n",
4427 target
->target_number
,
4428 target_name(target
));
4429 command_print(cmd_ctx
, "%-25s | Body", "Event");
4430 command_print(cmd_ctx
, "------------------------- | "
4431 "----------------------------------------");
4434 Jim_Nvp
*opt
= Jim_Nvp_value2name_simple(nvp_target_event
, teap
->event
);
4435 command_print(cmd_ctx
, "%-25s | %s",
4436 opt
->name
, Jim_GetString(teap
->body
, NULL
));
4439 command_print(cmd_ctx
, "***END***");
4442 static int jim_target_current_state(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4446 Jim_WrongNumArgs(interp
, 1, argv
, "[no parameters]");
4449 struct target
*target
= Jim_CmdPrivData(interp
);
4450 Jim_SetResultString(interp
, target_state_name(target
), -1);
4453 static int jim_target_invoke_event(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4456 Jim_GetOpt_Setup(&goi
, interp
, argc
- 1, argv
+ 1);
4459 const char *cmd_name
= Jim_GetString(argv
[0], NULL
);
4460 Jim_SetResultFormatted(goi
.interp
, "%s <eventname>", cmd_name
);
4464 int e
= Jim_GetOpt_Nvp(&goi
, nvp_target_event
, &n
);
4467 Jim_GetOpt_NvpUnknown(&goi
, nvp_target_event
, 1);
4470 struct target
*target
= Jim_CmdPrivData(interp
);
4471 target_handle_event(target
, n
->value
);
4475 static const struct command_registration target_instance_command_handlers
[] = {
4477 .name
= "configure",
4478 .mode
= COMMAND_CONFIG
,
4479 .jim_handler
= jim_target_configure
,
4480 .help
= "configure a new target for use",
4481 .usage
= "[target_attribute ...]",
4485 .mode
= COMMAND_ANY
,
4486 .jim_handler
= jim_target_configure
,
4487 .help
= "returns the specified target attribute",
4488 .usage
= "target_attribute",
4492 .mode
= COMMAND_EXEC
,
4493 .jim_handler
= jim_target_mw
,
4494 .help
= "Write 32-bit word(s) to target memory",
4495 .usage
= "address data [count]",
4499 .mode
= COMMAND_EXEC
,
4500 .jim_handler
= jim_target_mw
,
4501 .help
= "Write 16-bit half-word(s) to target memory",
4502 .usage
= "address data [count]",
4506 .mode
= COMMAND_EXEC
,
4507 .jim_handler
= jim_target_mw
,
4508 .help
= "Write byte(s) to target memory",
4509 .usage
= "address data [count]",
4513 .mode
= COMMAND_EXEC
,
4514 .jim_handler
= jim_target_md
,
4515 .help
= "Display target memory as 32-bit words",
4516 .usage
= "address [count]",
4520 .mode
= COMMAND_EXEC
,
4521 .jim_handler
= jim_target_md
,
4522 .help
= "Display target memory as 16-bit half-words",
4523 .usage
= "address [count]",
4527 .mode
= COMMAND_EXEC
,
4528 .jim_handler
= jim_target_md
,
4529 .help
= "Display target memory as 8-bit bytes",
4530 .usage
= "address [count]",
4533 .name
= "array2mem",
4534 .mode
= COMMAND_EXEC
,
4535 .jim_handler
= jim_target_array2mem
,
4536 .help
= "Writes Tcl array of 8/16/32 bit numbers "
4538 .usage
= "arrayname bitwidth address count",
4541 .name
= "mem2array",
4542 .mode
= COMMAND_EXEC
,
4543 .jim_handler
= jim_target_mem2array
,
4544 .help
= "Loads Tcl array of 8/16/32 bit numbers "
4545 "from target memory",
4546 .usage
= "arrayname bitwidth address count",
4549 .name
= "eventlist",
4550 .mode
= COMMAND_EXEC
,
4551 .jim_handler
= jim_target_event_list
,
4552 .help
= "displays a table of events defined for this target",
4556 .mode
= COMMAND_EXEC
,
4557 .jim_handler
= jim_target_current_state
,
4558 .help
= "displays the current state of this target",
4561 .name
= "arp_examine",
4562 .mode
= COMMAND_EXEC
,
4563 .jim_handler
= jim_target_examine
,
4564 .help
= "used internally for reset processing",
4567 .name
= "arp_halt_gdb",
4568 .mode
= COMMAND_EXEC
,
4569 .jim_handler
= jim_target_halt_gdb
,
4570 .help
= "used internally for reset processing to halt GDB",
4574 .mode
= COMMAND_EXEC
,
4575 .jim_handler
= jim_target_poll
,
4576 .help
= "used internally for reset processing",
4579 .name
= "arp_reset",
4580 .mode
= COMMAND_EXEC
,
4581 .jim_handler
= jim_target_reset
,
4582 .help
= "used internally for reset processing",
4586 .mode
= COMMAND_EXEC
,
4587 .jim_handler
= jim_target_halt
,
4588 .help
= "used internally for reset processing",
4591 .name
= "arp_waitstate",
4592 .mode
= COMMAND_EXEC
,
4593 .jim_handler
= jim_target_wait_state
,
4594 .help
= "used internally for reset processing",
4597 .name
= "invoke-event",
4598 .mode
= COMMAND_EXEC
,
4599 .jim_handler
= jim_target_invoke_event
,
4600 .help
= "invoke handler for specified event",
4601 .usage
= "event_name",
4603 COMMAND_REGISTRATION_DONE
4606 static int target_create(Jim_GetOptInfo
*goi
)
4614 struct target
*target
;
4615 struct command_context
*cmd_ctx
;
4617 cmd_ctx
= current_command_context(goi
->interp
);
4618 assert (cmd_ctx
!= NULL
);
4620 if (goi
->argc
< 3) {
4621 Jim_WrongNumArgs(goi
->interp
, 1, goi
->argv
, "?name? ?type? ..options...");
4626 Jim_GetOpt_Obj(goi
, &new_cmd
);
4627 /* does this command exist? */
4628 cmd
= Jim_GetCommand(goi
->interp
, new_cmd
, JIM_ERRMSG
);
4630 cp
= Jim_GetString(new_cmd
, NULL
);
4631 Jim_SetResultFormatted(goi
->interp
, "Command/target: %s Exists", cp
);
4636 e
= Jim_GetOpt_String(goi
, &cp2
, NULL
);
4638 /* now does target type exist */
4639 for (x
= 0 ; target_types
[x
] ; x
++) {
4640 if (0 == strcmp(cp
, target_types
[x
]->name
)) {
4645 if (target_types
[x
] == NULL
) {
4646 Jim_SetResultFormatted(goi
->interp
, "Unknown target type %s, try one of ", cp
);
4647 for (x
= 0 ; target_types
[x
] ; x
++) {
4648 if (target_types
[x
+ 1]) {
4649 Jim_AppendStrings(goi
->interp
,
4650 Jim_GetResult(goi
->interp
),
4651 target_types
[x
]->name
,
4654 Jim_AppendStrings(goi
->interp
,
4655 Jim_GetResult(goi
->interp
),
4657 target_types
[x
]->name
,NULL
);
4664 target
= calloc(1,sizeof(struct target
));
4665 /* set target number */
4666 target
->target_number
= new_target_number();
4668 /* allocate memory for each unique target type */
4669 target
->type
= (struct target_type
*)calloc(1,sizeof(struct target_type
));
4671 memcpy(target
->type
, target_types
[x
], sizeof(struct target_type
));
4673 /* will be set by "-endian" */
4674 target
->endianness
= TARGET_ENDIAN_UNKNOWN
;
4676 /* default to first core, override with -coreid */
4679 target
->working_area
= 0x0;
4680 target
->working_area_size
= 0x0;
4681 target
->working_areas
= NULL
;
4682 target
->backup_working_area
= 0;
4684 target
->state
= TARGET_UNKNOWN
;
4685 target
->debug_reason
= DBG_REASON_UNDEFINED
;
4686 target
->reg_cache
= NULL
;
4687 target
->breakpoints
= NULL
;
4688 target
->watchpoints
= NULL
;
4689 target
->next
= NULL
;
4690 target
->arch_info
= NULL
;
4692 target
->display
= 1;
4694 target
->halt_issued
= false;
4696 /* initialize trace information */
4697 target
->trace_info
= malloc(sizeof(struct trace
));
4698 target
->trace_info
->num_trace_points
= 0;
4699 target
->trace_info
->trace_points_size
= 0;
4700 target
->trace_info
->trace_points
= NULL
;
4701 target
->trace_info
->trace_history_size
= 0;
4702 target
->trace_info
->trace_history
= NULL
;
4703 target
->trace_info
->trace_history_pos
= 0;
4704 target
->trace_info
->trace_history_overflowed
= 0;
4706 target
->dbgmsg
= NULL
;
4707 target
->dbg_msg_enabled
= 0;
4709 target
->endianness
= TARGET_ENDIAN_UNKNOWN
;
4711 /* Do the rest as "configure" options */
4712 goi
->isconfigure
= 1;
4713 e
= target_configure(goi
, target
);
4715 if (target
->tap
== NULL
)
4717 Jim_SetResultString(goi
->interp
, "-chain-position required when creating target", -1);
4727 if (target
->endianness
== TARGET_ENDIAN_UNKNOWN
) {
4728 /* default endian to little if not specified */
4729 target
->endianness
= TARGET_LITTLE_ENDIAN
;
4732 /* incase variant is not set */
4733 if (!target
->variant
)
4734 target
->variant
= strdup("");
4736 cp
= Jim_GetString(new_cmd
, NULL
);
4737 target
->cmd_name
= strdup(cp
);
4739 /* create the target specific commands */
4740 if (target
->type
->commands
) {
4741 e
= register_commands(cmd_ctx
, NULL
, target
->type
->commands
);
4743 LOG_ERROR("unable to register '%s' commands", cp
);
4745 if (target
->type
->target_create
) {
4746 (*(target
->type
->target_create
))(target
, goi
->interp
);
4749 /* append to end of list */
4751 struct target
**tpp
;
4752 tpp
= &(all_targets
);
4754 tpp
= &((*tpp
)->next
);
4759 /* now - create the new target name command */
4760 const const struct command_registration target_subcommands
[] = {
4762 .chain
= target_instance_command_handlers
,
4765 .chain
= target
->type
->commands
,
4767 COMMAND_REGISTRATION_DONE
4769 const const struct command_registration target_commands
[] = {
4772 .mode
= COMMAND_ANY
,
4773 .help
= "target command group",
4774 .chain
= target_subcommands
,
4776 COMMAND_REGISTRATION_DONE
4778 e
= register_commands(cmd_ctx
, NULL
, target_commands
);
4782 struct command
*c
= command_find_in_context(cmd_ctx
, cp
);
4784 command_set_handler_data(c
, target
);
4786 return (ERROR_OK
== e
) ? JIM_OK
: JIM_ERR
;
4789 static int jim_target_current(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4793 Jim_WrongNumArgs(interp
, 1, argv
, "Too many parameters");
4796 struct command_context
*cmd_ctx
= current_command_context(interp
);
4797 assert (cmd_ctx
!= NULL
);
4799 Jim_SetResultString(interp
, get_current_target(cmd_ctx
)->cmd_name
, -1);
4803 static int jim_target_types(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4807 Jim_WrongNumArgs(interp
, 1, argv
, "Too many parameters");
4810 Jim_SetResult(interp
, Jim_NewListObj(interp
, NULL
, 0));
4811 for (unsigned x
= 0; NULL
!= target_types
[x
]; x
++)
4813 Jim_ListAppendElement(interp
, Jim_GetResult(interp
),
4814 Jim_NewStringObj(interp
, target_types
[x
]->name
, -1));
4819 static int jim_target_names(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4823 Jim_WrongNumArgs(interp
, 1, argv
, "Too many parameters");
4826 Jim_SetResult(interp
, Jim_NewListObj(interp
, NULL
, 0));
4827 struct target
*target
= all_targets
;
4830 Jim_ListAppendElement(interp
, Jim_GetResult(interp
),
4831 Jim_NewStringObj(interp
, target_name(target
), -1));
4832 target
= target
->next
;
4837 static int jim_target_create(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4840 Jim_GetOpt_Setup(&goi
, interp
, argc
- 1, argv
+ 1);
4843 Jim_WrongNumArgs(goi
.interp
, goi
.argc
, goi
.argv
,
4844 "<name> <target_type> [<target_options> ...]");
4847 return target_create(&goi
);
4850 static int jim_target_number(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4853 Jim_GetOpt_Setup(&goi
, interp
, argc
- 1, argv
+ 1);
4855 /* It's OK to remove this mechanism sometime after August 2010 or so */
4856 LOG_WARNING("don't use numbers as target identifiers; use names");
4859 Jim_SetResultFormatted(goi
.interp
, "usage: target number <number>");
4863 int e
= Jim_GetOpt_Wide(&goi
, &w
);
4867 struct target
*target
;
4868 for (target
= all_targets
; NULL
!= target
; target
= target
->next
)
4870 if (target
->target_number
!= w
)
4873 Jim_SetResultString(goi
.interp
, target_name(target
), -1);
4877 Jim_Obj
*wObj
= Jim_NewIntObj(goi
.interp
, w
);
4878 Jim_SetResultFormatted(goi
.interp
,
4879 "Target: number %#s does not exist", wObj
);
4880 Jim_FreeNewObj(interp
, wObj
);
4885 static int jim_target_count(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4889 Jim_WrongNumArgs(interp
, 1, argv
, "<no parameters>");
4893 struct target
*target
= all_targets
;
4894 while (NULL
!= target
)
4896 target
= target
->next
;
4899 Jim_SetResult(interp
, Jim_NewIntObj(interp
, count
));
4903 static const struct command_registration target_subcommand_handlers
[] = {
4906 .mode
= COMMAND_CONFIG
,
4907 .handler
= handle_target_init_command
,
4908 .help
= "initialize targets",
4912 /* REVISIT this should be COMMAND_CONFIG ... */
4913 .mode
= COMMAND_ANY
,
4914 .jim_handler
= jim_target_create
,
4915 .usage
= "name type '-chain-position' name [options ...]",
4916 .help
= "Creates and selects a new target",
4920 .mode
= COMMAND_ANY
,
4921 .jim_handler
= jim_target_current
,
4922 .help
= "Returns the currently selected target",
4926 .mode
= COMMAND_ANY
,
4927 .jim_handler
= jim_target_types
,
4928 .help
= "Returns the available target types as "
4929 "a list of strings",
4933 .mode
= COMMAND_ANY
,
4934 .jim_handler
= jim_target_names
,
4935 .help
= "Returns the names of all targets as a list of strings",
4939 .mode
= COMMAND_ANY
,
4940 .jim_handler
= jim_target_number
,
4942 .help
= "Returns the name of the numbered target "
4947 .mode
= COMMAND_ANY
,
4948 .jim_handler
= jim_target_count
,
4949 .help
= "Returns the number of targets as an integer "
4952 COMMAND_REGISTRATION_DONE
4963 static int fastload_num
;
4964 static struct FastLoad
*fastload
;
4966 static void free_fastload(void)
4968 if (fastload
!= NULL
)
4971 for (i
= 0; i
< fastload_num
; i
++)
4973 if (fastload
[i
].data
)
4974 free(fastload
[i
].data
);
4984 COMMAND_HANDLER(handle_fast_load_image_command
)
4988 uint32_t image_size
;
4989 uint32_t min_address
= 0;
4990 uint32_t max_address
= 0xffffffff;
4995 int retval
= CALL_COMMAND_HANDLER(parse_load_image_command_CMD_ARGV
,
4996 &image
, &min_address
, &max_address
);
4997 if (ERROR_OK
!= retval
)
5000 struct duration bench
;
5001 duration_start(&bench
);
5003 retval
= image_open(&image
, CMD_ARGV
[0], (CMD_ARGC
>= 3) ? CMD_ARGV
[2] : NULL
);
5004 if (retval
!= ERROR_OK
)
5011 fastload_num
= image
.num_sections
;
5012 fastload
= (struct FastLoad
*)malloc(sizeof(struct FastLoad
)*image
.num_sections
);
5013 if (fastload
== NULL
)
5015 command_print(CMD_CTX
, "out of memory");
5016 image_close(&image
);
5019 memset(fastload
, 0, sizeof(struct FastLoad
)*image
.num_sections
);
5020 for (i
= 0; i
< image
.num_sections
; i
++)
5022 buffer
= malloc(image
.sections
[i
].size
);
5025 command_print(CMD_CTX
, "error allocating buffer for section (%d bytes)",
5026 (int)(image
.sections
[i
].size
));
5027 retval
= ERROR_FAIL
;
5031 if ((retval
= image_read_section(&image
, i
, 0x0, image
.sections
[i
].size
, buffer
, &buf_cnt
)) != ERROR_OK
)
5037 uint32_t offset
= 0;
5038 uint32_t length
= buf_cnt
;
5041 /* DANGER!!! beware of unsigned comparision here!!! */
5043 if ((image
.sections
[i
].base_address
+ buf_cnt
>= min_address
)&&
5044 (image
.sections
[i
].base_address
< max_address
))
5046 if (image
.sections
[i
].base_address
< min_address
)
5048 /* clip addresses below */
5049 offset
+= min_address
-image
.sections
[i
].base_address
;
5053 if (image
.sections
[i
].base_address
+ buf_cnt
> max_address
)
5055 length
-= (image
.sections
[i
].base_address
+ buf_cnt
)-max_address
;
5058 fastload
[i
].address
= image
.sections
[i
].base_address
+ offset
;
5059 fastload
[i
].data
= malloc(length
);
5060 if (fastload
[i
].data
== NULL
)
5063 command_print(CMD_CTX
, "error allocating buffer for section (%d bytes)",
5065 retval
= ERROR_FAIL
;
5068 memcpy(fastload
[i
].data
, buffer
+ offset
, length
);
5069 fastload
[i
].length
= length
;
5071 image_size
+= length
;
5072 command_print(CMD_CTX
, "%u bytes written at address 0x%8.8x",
5073 (unsigned int)length
,
5074 ((unsigned int)(image
.sections
[i
].base_address
+ offset
)));
5080 if ((ERROR_OK
== retval
) && (duration_measure(&bench
) == ERROR_OK
))
5082 command_print(CMD_CTX
, "Loaded %" PRIu32
" bytes "
5083 "in %fs (%0.3f KiB/s)", image_size
,
5084 duration_elapsed(&bench
), duration_kbps(&bench
, image_size
));
5086 command_print(CMD_CTX
,
5087 "WARNING: image has not been loaded to target!"
5088 "You can issue a 'fast_load' to finish loading.");
5091 image_close(&image
);
5093 if (retval
!= ERROR_OK
)
5101 COMMAND_HANDLER(handle_fast_load_command
)
5104 return ERROR_COMMAND_SYNTAX_ERROR
;
5105 if (fastload
== NULL
)
5107 LOG_ERROR("No image in memory");
5111 int ms
= timeval_ms();
5113 int retval
= ERROR_OK
;
5114 for (i
= 0; i
< fastload_num
;i
++)
5116 struct target
*target
= get_current_target(CMD_CTX
);
5117 command_print(CMD_CTX
, "Write to 0x%08x, length 0x%08x",
5118 (unsigned int)(fastload
[i
].address
),
5119 (unsigned int)(fastload
[i
].length
));
5120 retval
= target_write_buffer(target
, fastload
[i
].address
, fastload
[i
].length
, fastload
[i
].data
);
5121 if (retval
!= ERROR_OK
)
5125 size
+= fastload
[i
].length
;
5127 if (retval
== ERROR_OK
)
5129 int after
= timeval_ms();
5130 command_print(CMD_CTX
, "Loaded image %f kBytes/s", (float)(size
/1024.0)/((float)(after
-ms
)/1000.0));
5135 static const struct command_registration target_command_handlers
[] = {
5138 .handler
= handle_targets_command
,
5139 .mode
= COMMAND_ANY
,
5140 .help
= "change current default target (one parameter) "
5141 "or prints table of all targets (no parameters)",
5142 .usage
= "[target]",
5146 .mode
= COMMAND_CONFIG
,
5147 .help
= "configure target",
5149 .chain
= target_subcommand_handlers
,
5151 COMMAND_REGISTRATION_DONE
5154 int target_register_commands(struct command_context
*cmd_ctx
)
5156 return register_commands(cmd_ctx
, NULL
, target_command_handlers
);
5159 static bool target_reset_nag
= true;
5161 bool get_target_reset_nag(void)
5163 return target_reset_nag
;
5166 COMMAND_HANDLER(handle_target_reset_nag
)
5168 return CALL_COMMAND_HANDLER(handle_command_parse_bool
,
5169 &target_reset_nag
, "Nag after each reset about options to improve "
5173 static const struct command_registration target_exec_command_handlers
[] = {
5175 .name
= "fast_load_image",
5176 .handler
= handle_fast_load_image_command
,
5177 .mode
= COMMAND_ANY
,
5178 .help
= "Load image into server memory for later use by "
5179 "fast_load; primarily for profiling",
5180 .usage
= "filename address ['bin'|'ihex'|'elf'|'s19'] "
5181 "[min_address [max_length]]",
5184 .name
= "fast_load",
5185 .handler
= handle_fast_load_command
,
5186 .mode
= COMMAND_EXEC
,
5187 .help
= "loads active fast load image to current target "
5188 "- mainly for profiling purposes",
5192 .handler
= handle_profile_command
,
5193 .mode
= COMMAND_EXEC
,
5194 .help
= "profiling samples the CPU PC",
5196 /** @todo don't register virt2phys() unless target supports it */
5198 .name
= "virt2phys",
5199 .handler
= handle_virt2phys_command
,
5200 .mode
= COMMAND_ANY
,
5201 .help
= "translate a virtual address into a physical address",
5202 .usage
= "virtual_address",
5206 .handler
= handle_reg_command
,
5207 .mode
= COMMAND_EXEC
,
5208 .help
= "display or set a register; with no arguments, "
5209 "displays all registers and their values",
5210 .usage
= "[(register_name|register_number) [value]]",
5214 .handler
= handle_poll_command
,
5215 .mode
= COMMAND_EXEC
,
5216 .help
= "poll target state; or reconfigure background polling",
5217 .usage
= "['on'|'off']",
5220 .name
= "wait_halt",
5221 .handler
= handle_wait_halt_command
,
5222 .mode
= COMMAND_EXEC
,
5223 .help
= "wait up to the specified number of milliseconds "
5224 "(default 5) for a previously requested halt",
5225 .usage
= "[milliseconds]",
5229 .handler
= handle_halt_command
,
5230 .mode
= COMMAND_EXEC
,
5231 .help
= "request target to halt, then wait up to the specified"
5232 "number of milliseconds (default 5) for it to complete",
5233 .usage
= "[milliseconds]",
5237 .handler
= handle_resume_command
,
5238 .mode
= COMMAND_EXEC
,
5239 .help
= "resume target execution from current PC or address",
5240 .usage
= "[address]",
5244 .handler
= handle_reset_command
,
5245 .mode
= COMMAND_EXEC
,
5246 .usage
= "[run|halt|init]",
5247 .help
= "Reset all targets into the specified mode."
5248 "Default reset mode is run, if not given.",
5251 .name
= "soft_reset_halt",
5252 .handler
= handle_soft_reset_halt_command
,
5253 .mode
= COMMAND_EXEC
,
5254 .help
= "halt the target and do a soft reset",
5258 .handler
= handle_step_command
,
5259 .mode
= COMMAND_EXEC
,
5260 .help
= "step one instruction from current PC or address",
5261 .usage
= "[address]",
5265 .handler
= handle_md_command
,
5266 .mode
= COMMAND_EXEC
,
5267 .help
= "display memory words",
5268 .usage
= "['phys'] address [count]",
5272 .handler
= handle_md_command
,
5273 .mode
= COMMAND_EXEC
,
5274 .help
= "display memory half-words",
5275 .usage
= "['phys'] address [count]",
5279 .handler
= handle_md_command
,
5280 .mode
= COMMAND_EXEC
,
5281 .help
= "display memory bytes",
5282 .usage
= "['phys'] address [count]",
5286 .handler
= handle_mw_command
,
5287 .mode
= COMMAND_EXEC
,
5288 .help
= "write memory word",
5289 .usage
= "['phys'] address value [count]",
5293 .handler
= handle_mw_command
,
5294 .mode
= COMMAND_EXEC
,
5295 .help
= "write memory half-word",
5296 .usage
= "['phys'] address value [count]",
5300 .handler
= handle_mw_command
,
5301 .mode
= COMMAND_EXEC
,
5302 .help
= "write memory byte",
5303 .usage
= "['phys'] address value [count]",
5307 .handler
= handle_bp_command
,
5308 .mode
= COMMAND_EXEC
,
5309 .help
= "list or set hardware or software breakpoint",
5310 .usage
= "[address length ['hw']]",
5314 .handler
= handle_rbp_command
,
5315 .mode
= COMMAND_EXEC
,
5316 .help
= "remove breakpoint",
5321 .handler
= handle_wp_command
,
5322 .mode
= COMMAND_EXEC
,
5323 .help
= "list (no params) or create watchpoints",
5324 .usage
= "[address length [('r'|'w'|'a') value [mask]]]",
5328 .handler
= handle_rwp_command
,
5329 .mode
= COMMAND_EXEC
,
5330 .help
= "remove watchpoint",
5334 .name
= "load_image",
5335 .handler
= handle_load_image_command
,
5336 .mode
= COMMAND_EXEC
,
5337 .usage
= "filename address ['bin'|'ihex'|'elf'|'s19'] "
5338 "[min_address] [max_length]",
5341 .name
= "dump_image",
5342 .handler
= handle_dump_image_command
,
5343 .mode
= COMMAND_EXEC
,
5344 .usage
= "filename address size",
5347 .name
= "verify_image",
5348 .handler
= handle_verify_image_command
,
5349 .mode
= COMMAND_EXEC
,
5350 .usage
= "filename [offset [type]]",
5353 .name
= "test_image",
5354 .handler
= handle_test_image_command
,
5355 .mode
= COMMAND_EXEC
,
5356 .usage
= "filename [offset [type]]",
5359 .name
= "mem2array",
5360 .mode
= COMMAND_EXEC
,
5361 .jim_handler
= jim_mem2array
,
5362 .help
= "read 8/16/32 bit memory and return as a TCL array "
5363 "for script processing",
5364 .usage
= "arrayname bitwidth address count",
5367 .name
= "array2mem",
5368 .mode
= COMMAND_EXEC
,
5369 .jim_handler
= jim_array2mem
,
5370 .help
= "convert a TCL array to memory locations "
5371 "and write the 8/16/32 bit values",
5372 .usage
= "arrayname bitwidth address count",
5375 .name
= "reset_nag",
5376 .handler
= handle_target_reset_nag
,
5377 .mode
= COMMAND_ANY
,
5378 .help
= "Nag after each reset about options that could have been "
5379 "enabled to improve performance. ",
5380 .usage
= "['enable'|'disable']",
5382 COMMAND_REGISTRATION_DONE
5384 static int target_register_user_commands(struct command_context
*cmd_ctx
)
5386 int retval
= ERROR_OK
;
5387 if ((retval
= target_request_register_commands(cmd_ctx
)) != ERROR_OK
)
5390 if ((retval
= trace_register_commands(cmd_ctx
)) != ERROR_OK
)
5394 return register_commands(cmd_ctx
, NULL
, target_exec_command_handlers
);