1 /***************************************************************************
2 * Copyright (C) 2005 by Dominic Rath *
3 * Dominic.Rath@gmx.de *
5 * Copyright (C) 2007-2010 Øyvind Harboe *
6 * oyvind.harboe@zylin.com *
8 * Copyright (C) 2008, Duane Ellis *
9 * openocd@duaneeellis.com *
11 * Copyright (C) 2008 by Spencer Oliver *
12 * spen@spen-soft.co.uk *
14 * Copyright (C) 2008 by Rick Altherr *
15 * kc8apf@kc8apf.net> *
17 * This program is free software; you can redistribute it and/or modify *
18 * it under the terms of the GNU General Public License as published by *
19 * the Free Software Foundation; either version 2 of the License, or *
20 * (at your option) any later version. *
22 * This program is distributed in the hope that it will be useful, *
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
25 * GNU General Public License for more details. *
27 * You should have received a copy of the GNU General Public License *
28 * along with this program; if not, write to the *
29 * Free Software Foundation, Inc., *
30 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
31 ***************************************************************************/
36 #include <helper/time_support.h>
37 #include <jtag/jtag.h>
38 #include <flash/nor/core.h>
41 #include "target_type.h"
42 #include "target_request.h"
43 #include "breakpoints.h"
49 static int target_array2mem(Jim_Interp
*interp
, struct target
*target
,
50 int argc
, Jim_Obj
*const *argv
);
51 static int target_mem2array(Jim_Interp
*interp
, struct target
*target
,
52 int argc
, Jim_Obj
*const *argv
);
53 static int target_register_user_commands(struct command_context
*cmd_ctx
);
56 extern struct target_type arm7tdmi_target
;
57 extern struct target_type arm720t_target
;
58 extern struct target_type arm9tdmi_target
;
59 extern struct target_type arm920t_target
;
60 extern struct target_type arm966e_target
;
61 extern struct target_type arm926ejs_target
;
62 extern struct target_type fa526_target
;
63 extern struct target_type feroceon_target
;
64 extern struct target_type dragonite_target
;
65 extern struct target_type xscale_target
;
66 extern struct target_type cortexm3_target
;
67 extern struct target_type cortexa8_target
;
68 extern struct target_type arm11_target
;
69 extern struct target_type mips_m4k_target
;
70 extern struct target_type avr_target
;
71 extern struct target_type dsp563xx_target
;
72 extern struct target_type testee_target
;
73 extern struct target_type avr32_ap7k_target
;
75 static struct target_type
*target_types
[] =
98 struct target
*all_targets
= NULL
;
99 static struct target_event_callback
*target_event_callbacks
= NULL
;
100 static struct target_timer_callback
*target_timer_callbacks
= NULL
;
101 static const int polling_interval
= 100;
103 static const Jim_Nvp nvp_assert
[] = {
104 { .name
= "assert", NVP_ASSERT
},
105 { .name
= "deassert", NVP_DEASSERT
},
106 { .name
= "T", NVP_ASSERT
},
107 { .name
= "F", NVP_DEASSERT
},
108 { .name
= "t", NVP_ASSERT
},
109 { .name
= "f", NVP_DEASSERT
},
110 { .name
= NULL
, .value
= -1 }
113 static const Jim_Nvp nvp_error_target
[] = {
114 { .value
= ERROR_TARGET_INVALID
, .name
= "err-invalid" },
115 { .value
= ERROR_TARGET_INIT_FAILED
, .name
= "err-init-failed" },
116 { .value
= ERROR_TARGET_TIMEOUT
, .name
= "err-timeout" },
117 { .value
= ERROR_TARGET_NOT_HALTED
, .name
= "err-not-halted" },
118 { .value
= ERROR_TARGET_FAILURE
, .name
= "err-failure" },
119 { .value
= ERROR_TARGET_UNALIGNED_ACCESS
, .name
= "err-unaligned-access" },
120 { .value
= ERROR_TARGET_DATA_ABORT
, .name
= "err-data-abort" },
121 { .value
= ERROR_TARGET_RESOURCE_NOT_AVAILABLE
, .name
= "err-resource-not-available" },
122 { .value
= ERROR_TARGET_TRANSLATION_FAULT
, .name
= "err-translation-fault" },
123 { .value
= ERROR_TARGET_NOT_RUNNING
, .name
= "err-not-running" },
124 { .value
= ERROR_TARGET_NOT_EXAMINED
, .name
= "err-not-examined" },
125 { .value
= -1, .name
= NULL
}
128 static const char *target_strerror_safe(int err
)
132 n
= Jim_Nvp_value2name_simple(nvp_error_target
, err
);
133 if (n
->name
== NULL
) {
140 static const Jim_Nvp nvp_target_event
[] = {
141 { .value
= TARGET_EVENT_OLD_gdb_program_config
, .name
= "old-gdb_program_config" },
142 { .value
= TARGET_EVENT_OLD_pre_resume
, .name
= "old-pre_resume" },
144 { .value
= TARGET_EVENT_GDB_HALT
, .name
= "gdb-halt" },
145 { .value
= TARGET_EVENT_HALTED
, .name
= "halted" },
146 { .value
= TARGET_EVENT_RESUMED
, .name
= "resumed" },
147 { .value
= TARGET_EVENT_RESUME_START
, .name
= "resume-start" },
148 { .value
= TARGET_EVENT_RESUME_END
, .name
= "resume-end" },
150 { .name
= "gdb-start", .value
= TARGET_EVENT_GDB_START
},
151 { .name
= "gdb-end", .value
= TARGET_EVENT_GDB_END
},
153 /* historical name */
155 { .value
= TARGET_EVENT_RESET_START
, .name
= "reset-start" },
157 { .value
= TARGET_EVENT_RESET_ASSERT_PRE
, .name
= "reset-assert-pre" },
158 { .value
= TARGET_EVENT_RESET_ASSERT
, .name
= "reset-assert" },
159 { .value
= TARGET_EVENT_RESET_ASSERT_POST
, .name
= "reset-assert-post" },
160 { .value
= TARGET_EVENT_RESET_DEASSERT_PRE
, .name
= "reset-deassert-pre" },
161 { .value
= TARGET_EVENT_RESET_DEASSERT_POST
, .name
= "reset-deassert-post" },
162 { .value
= TARGET_EVENT_RESET_HALT_PRE
, .name
= "reset-halt-pre" },
163 { .value
= TARGET_EVENT_RESET_HALT_POST
, .name
= "reset-halt-post" },
164 { .value
= TARGET_EVENT_RESET_WAIT_PRE
, .name
= "reset-wait-pre" },
165 { .value
= TARGET_EVENT_RESET_WAIT_POST
, .name
= "reset-wait-post" },
166 { .value
= TARGET_EVENT_RESET_INIT
, .name
= "reset-init" },
167 { .value
= TARGET_EVENT_RESET_END
, .name
= "reset-end" },
169 { .value
= TARGET_EVENT_EXAMINE_START
, .name
= "examine-start" },
170 { .value
= TARGET_EVENT_EXAMINE_END
, .name
= "examine-end" },
172 { .value
= TARGET_EVENT_DEBUG_HALTED
, .name
= "debug-halted" },
173 { .value
= TARGET_EVENT_DEBUG_RESUMED
, .name
= "debug-resumed" },
175 { .value
= TARGET_EVENT_GDB_ATTACH
, .name
= "gdb-attach" },
176 { .value
= TARGET_EVENT_GDB_DETACH
, .name
= "gdb-detach" },
178 { .value
= TARGET_EVENT_GDB_FLASH_WRITE_START
, .name
= "gdb-flash-write-start" },
179 { .value
= TARGET_EVENT_GDB_FLASH_WRITE_END
, .name
= "gdb-flash-write-end" },
181 { .value
= TARGET_EVENT_GDB_FLASH_ERASE_START
, .name
= "gdb-flash-erase-start" },
182 { .value
= TARGET_EVENT_GDB_FLASH_ERASE_END
, .name
= "gdb-flash-erase-end" },
184 { .value
= TARGET_EVENT_RESUME_START
, .name
= "resume-start" },
185 { .value
= TARGET_EVENT_RESUMED
, .name
= "resume-ok" },
186 { .value
= TARGET_EVENT_RESUME_END
, .name
= "resume-end" },
188 { .name
= NULL
, .value
= -1 }
191 static const Jim_Nvp nvp_target_state
[] = {
192 { .name
= "unknown", .value
= TARGET_UNKNOWN
},
193 { .name
= "running", .value
= TARGET_RUNNING
},
194 { .name
= "halted", .value
= TARGET_HALTED
},
195 { .name
= "reset", .value
= TARGET_RESET
},
196 { .name
= "debug-running", .value
= TARGET_DEBUG_RUNNING
},
197 { .name
= NULL
, .value
= -1 },
200 static const Jim_Nvp nvp_target_debug_reason
[] = {
201 { .name
= "debug-request" , .value
= DBG_REASON_DBGRQ
},
202 { .name
= "breakpoint" , .value
= DBG_REASON_BREAKPOINT
},
203 { .name
= "watchpoint" , .value
= DBG_REASON_WATCHPOINT
},
204 { .name
= "watchpoint-and-breakpoint", .value
= DBG_REASON_WPTANDBKPT
},
205 { .name
= "single-step" , .value
= DBG_REASON_SINGLESTEP
},
206 { .name
= "target-not-halted" , .value
= DBG_REASON_NOTHALTED
},
207 { .name
= "undefined" , .value
= DBG_REASON_UNDEFINED
},
208 { .name
= NULL
, .value
= -1 },
211 static const Jim_Nvp nvp_target_endian
[] = {
212 { .name
= "big", .value
= TARGET_BIG_ENDIAN
},
213 { .name
= "little", .value
= TARGET_LITTLE_ENDIAN
},
214 { .name
= "be", .value
= TARGET_BIG_ENDIAN
},
215 { .name
= "le", .value
= TARGET_LITTLE_ENDIAN
},
216 { .name
= NULL
, .value
= -1 },
219 static const Jim_Nvp nvp_reset_modes
[] = {
220 { .name
= "unknown", .value
= RESET_UNKNOWN
},
221 { .name
= "run" , .value
= RESET_RUN
},
222 { .name
= "halt" , .value
= RESET_HALT
},
223 { .name
= "init" , .value
= RESET_INIT
},
224 { .name
= NULL
, .value
= -1 },
227 const char *debug_reason_name(struct target
*t
)
231 cp
= Jim_Nvp_value2name_simple(nvp_target_debug_reason
,
232 t
->debug_reason
)->name
;
234 LOG_ERROR("Invalid debug reason: %d", (int)(t
->debug_reason
));
235 cp
= "(*BUG*unknown*BUG*)";
241 target_state_name( struct target
*t
)
244 cp
= Jim_Nvp_value2name_simple(nvp_target_state
, t
->state
)->name
;
246 LOG_ERROR("Invalid target state: %d", (int)(t
->state
));
247 cp
= "(*BUG*unknown*BUG*)";
252 /* determine the number of the new target */
253 static int new_target_number(void)
258 /* number is 0 based */
262 if (x
< t
->target_number
) {
263 x
= t
->target_number
;
270 /* read a uint32_t from a buffer in target memory endianness */
271 uint32_t target_buffer_get_u32(struct target
*target
, const uint8_t *buffer
)
273 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
274 return le_to_h_u32(buffer
);
276 return be_to_h_u32(buffer
);
279 /* read a uint16_t from a buffer in target memory endianness */
280 uint16_t target_buffer_get_u16(struct target
*target
, const uint8_t *buffer
)
282 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
283 return le_to_h_u16(buffer
);
285 return be_to_h_u16(buffer
);
288 /* read a uint8_t from a buffer in target memory endianness */
289 static uint8_t target_buffer_get_u8(struct target
*target
, const uint8_t *buffer
)
291 return *buffer
& 0x0ff;
294 /* write a uint32_t to a buffer in target memory endianness */
295 void target_buffer_set_u32(struct target
*target
, uint8_t *buffer
, uint32_t value
)
297 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
298 h_u32_to_le(buffer
, value
);
300 h_u32_to_be(buffer
, value
);
303 /* write a uint16_t to a buffer in target memory endianness */
304 void target_buffer_set_u16(struct target
*target
, uint8_t *buffer
, uint16_t value
)
306 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
307 h_u16_to_le(buffer
, value
);
309 h_u16_to_be(buffer
, value
);
312 /* write a uint8_t to a buffer in target memory endianness */
313 static void target_buffer_set_u8(struct target
*target
, uint8_t *buffer
, uint8_t value
)
318 /* return a pointer to a configured target; id is name or number */
319 struct target
*get_target(const char *id
)
321 struct target
*target
;
323 /* try as tcltarget name */
324 for (target
= all_targets
; target
; target
= target
->next
) {
325 if (target
->cmd_name
== NULL
)
327 if (strcmp(id
, target
->cmd_name
) == 0)
331 /* It's OK to remove this fallback sometime after August 2010 or so */
333 /* no match, try as number */
335 if (parse_uint(id
, &num
) != ERROR_OK
)
338 for (target
= all_targets
; target
; target
= target
->next
) {
339 if (target
->target_number
== (int)num
) {
340 LOG_WARNING("use '%s' as target identifier, not '%u'",
341 target
->cmd_name
, num
);
349 /* returns a pointer to the n-th configured target */
350 static struct target
*get_target_by_num(int num
)
352 struct target
*target
= all_targets
;
355 if (target
->target_number
== num
) {
358 target
= target
->next
;
364 struct target
* get_current_target(struct command_context
*cmd_ctx
)
366 struct target
*target
= get_target_by_num(cmd_ctx
->current_target
);
370 LOG_ERROR("BUG: current_target out of bounds");
377 int target_poll(struct target
*target
)
381 /* We can't poll until after examine */
382 if (!target_was_examined(target
))
384 /* Fail silently lest we pollute the log */
388 retval
= target
->type
->poll(target
);
389 if (retval
!= ERROR_OK
)
392 if (target
->halt_issued
)
394 if (target
->state
== TARGET_HALTED
)
396 target
->halt_issued
= false;
399 long long t
= timeval_ms() - target
->halt_issued_time
;
402 target
->halt_issued
= false;
403 LOG_INFO("Halt timed out, wake up GDB.");
404 target_call_event_callbacks(target
, TARGET_EVENT_GDB_HALT
);
412 int target_halt(struct target
*target
)
415 /* We can't poll until after examine */
416 if (!target_was_examined(target
))
418 LOG_ERROR("Target not examined yet");
422 retval
= target
->type
->halt(target
);
423 if (retval
!= ERROR_OK
)
426 target
->halt_issued
= true;
427 target
->halt_issued_time
= timeval_ms();
433 * Make the target (re)start executing using its saved execution
434 * context (possibly with some modifications).
436 * @param target Which target should start executing.
437 * @param current True to use the target's saved program counter instead
438 * of the address parameter
439 * @param address Optionally used as the program counter.
440 * @param handle_breakpoints True iff breakpoints at the resumption PC
441 * should be skipped. (For example, maybe execution was stopped by
442 * such a breakpoint, in which case it would be counterprodutive to
444 * @param debug_execution False if all working areas allocated by OpenOCD
445 * should be released and/or restored to their original contents.
446 * (This would for example be true to run some downloaded "helper"
447 * algorithm code, which resides in one such working buffer and uses
448 * another for data storage.)
450 * @todo Resolve the ambiguity about what the "debug_execution" flag
451 * signifies. For example, Target implementations don't agree on how
452 * it relates to invalidation of the register cache, or to whether
453 * breakpoints and watchpoints should be enabled. (It would seem wrong
454 * to enable breakpoints when running downloaded "helper" algorithms
455 * (debug_execution true), since the breakpoints would be set to match
456 * target firmware being debugged, not the helper algorithm.... and
457 * enabling them could cause such helpers to malfunction (for example,
458 * by overwriting data with a breakpoint instruction. On the other
459 * hand the infrastructure for running such helpers might use this
460 * procedure but rely on hardware breakpoint to detect termination.)
462 int target_resume(struct target
*target
, int current
, uint32_t address
, int handle_breakpoints
, int debug_execution
)
466 /* We can't poll until after examine */
467 if (!target_was_examined(target
))
469 LOG_ERROR("Target not examined yet");
473 /* note that resume *must* be asynchronous. The CPU can halt before
474 * we poll. The CPU can even halt at the current PC as a result of
475 * a software breakpoint being inserted by (a bug?) the application.
477 if ((retval
= target
->type
->resume(target
, current
, address
, handle_breakpoints
, debug_execution
)) != ERROR_OK
)
483 static int target_process_reset(struct command_context
*cmd_ctx
, enum target_reset_mode reset_mode
)
488 n
= Jim_Nvp_value2name_simple(nvp_reset_modes
, reset_mode
);
489 if (n
->name
== NULL
) {
490 LOG_ERROR("invalid reset mode");
494 /* disable polling during reset to make reset event scripts
495 * more predictable, i.e. dr/irscan & pathmove in events will
496 * not have JTAG operations injected into the middle of a sequence.
498 bool save_poll
= jtag_poll_get_enabled();
500 jtag_poll_set_enabled(false);
502 sprintf(buf
, "ocd_process_reset %s", n
->name
);
503 retval
= Jim_Eval(cmd_ctx
->interp
, buf
);
505 jtag_poll_set_enabled(save_poll
);
507 if (retval
!= JIM_OK
) {
508 Jim_MakeErrorMessage(cmd_ctx
->interp
);
509 command_print(NULL
,"%s\n", Jim_GetString(Jim_GetResult(cmd_ctx
->interp
), NULL
));
513 /* We want any events to be processed before the prompt */
514 retval
= target_call_timer_callbacks_now();
516 struct target
*target
;
517 for (target
= all_targets
; target
; target
= target
->next
) {
518 target
->type
->check_reset(target
);
524 static int identity_virt2phys(struct target
*target
,
525 uint32_t virtual, uint32_t *physical
)
531 static int no_mmu(struct target
*target
, int *enabled
)
537 static int default_examine(struct target
*target
)
539 target_set_examined(target
);
543 /* no check by default */
544 static int default_check_reset(struct target
*target
)
549 int target_examine_one(struct target
*target
)
551 return target
->type
->examine(target
);
554 static int jtag_enable_callback(enum jtag_event event
, void *priv
)
556 struct target
*target
= priv
;
558 if (event
!= JTAG_TAP_EVENT_ENABLE
|| !target
->tap
->enabled
)
561 jtag_unregister_event_callback(jtag_enable_callback
, target
);
562 return target_examine_one(target
);
566 /* Targets that correctly implement init + examine, i.e.
567 * no communication with target during init:
571 int target_examine(void)
573 int retval
= ERROR_OK
;
574 struct target
*target
;
576 for (target
= all_targets
; target
; target
= target
->next
)
578 /* defer examination, but don't skip it */
579 if (!target
->tap
->enabled
) {
580 jtag_register_event_callback(jtag_enable_callback
,
584 if ((retval
= target_examine_one(target
)) != ERROR_OK
)
589 const char *target_type_name(struct target
*target
)
591 return target
->type
->name
;
594 static int target_write_memory_imp(struct target
*target
, uint32_t address
, uint32_t size
, uint32_t count
, uint8_t *buffer
)
596 if (!target_was_examined(target
))
598 LOG_ERROR("Target not examined yet");
601 return target
->type
->write_memory_imp(target
, address
, size
, count
, buffer
);
604 static int target_read_memory_imp(struct target
*target
, uint32_t address
, uint32_t size
, uint32_t count
, uint8_t *buffer
)
606 if (!target_was_examined(target
))
608 LOG_ERROR("Target not examined yet");
611 return target
->type
->read_memory_imp(target
, address
, size
, count
, buffer
);
614 static int target_soft_reset_halt_imp(struct target
*target
)
616 if (!target_was_examined(target
))
618 LOG_ERROR("Target not examined yet");
621 if (!target
->type
->soft_reset_halt_imp
) {
622 LOG_ERROR("Target %s does not support soft_reset_halt",
623 target_name(target
));
626 return target
->type
->soft_reset_halt_imp(target
);
630 * Downloads a target-specific native code algorithm to the target,
631 * and executes it. * Note that some targets may need to set up, enable,
632 * and tear down a breakpoint (hard or * soft) to detect algorithm
633 * termination, while others may support lower overhead schemes where
634 * soft breakpoints embedded in the algorithm automatically terminate the
637 * @param target used to run the algorithm
638 * @param arch_info target-specific description of the algorithm.
640 int target_run_algorithm(struct target
*target
,
641 int num_mem_params
, struct mem_param
*mem_params
,
642 int num_reg_params
, struct reg_param
*reg_param
,
643 uint32_t entry_point
, uint32_t exit_point
,
644 int timeout_ms
, void *arch_info
)
646 int retval
= ERROR_FAIL
;
648 if (!target_was_examined(target
))
650 LOG_ERROR("Target not examined yet");
653 if (!target
->type
->run_algorithm
) {
654 LOG_ERROR("Target type '%s' does not support %s",
655 target_type_name(target
), __func__
);
659 target
->running_alg
= true;
660 retval
= target
->type
->run_algorithm(target
,
661 num_mem_params
, mem_params
,
662 num_reg_params
, reg_param
,
663 entry_point
, exit_point
, timeout_ms
, arch_info
);
664 target
->running_alg
= false;
671 int target_read_memory(struct target
*target
,
672 uint32_t address
, uint32_t size
, uint32_t count
, uint8_t *buffer
)
674 return target
->type
->read_memory(target
, address
, size
, count
, buffer
);
677 static int target_read_phys_memory(struct target
*target
,
678 uint32_t address
, uint32_t size
, uint32_t count
, uint8_t *buffer
)
680 return target
->type
->read_phys_memory(target
, address
, size
, count
, buffer
);
683 int target_write_memory(struct target
*target
,
684 uint32_t address
, uint32_t size
, uint32_t count
, uint8_t *buffer
)
686 return target
->type
->write_memory(target
, address
, size
, count
, buffer
);
689 static int target_write_phys_memory(struct target
*target
,
690 uint32_t address
, uint32_t size
, uint32_t count
, uint8_t *buffer
)
692 return target
->type
->write_phys_memory(target
, address
, size
, count
, buffer
);
695 int target_bulk_write_memory(struct target
*target
,
696 uint32_t address
, uint32_t count
, uint8_t *buffer
)
698 return target
->type
->bulk_write_memory(target
, address
, count
, buffer
);
701 int target_add_breakpoint(struct target
*target
,
702 struct breakpoint
*breakpoint
)
704 if (target
->state
!= TARGET_HALTED
) {
705 LOG_WARNING("target %s is not halted", target
->cmd_name
);
706 return ERROR_TARGET_NOT_HALTED
;
708 return target
->type
->add_breakpoint(target
, breakpoint
);
710 int target_remove_breakpoint(struct target
*target
,
711 struct breakpoint
*breakpoint
)
713 return target
->type
->remove_breakpoint(target
, breakpoint
);
716 int target_add_watchpoint(struct target
*target
,
717 struct watchpoint
*watchpoint
)
719 if (target
->state
!= TARGET_HALTED
) {
720 LOG_WARNING("target %s is not halted", target
->cmd_name
);
721 return ERROR_TARGET_NOT_HALTED
;
723 return target
->type
->add_watchpoint(target
, watchpoint
);
725 int target_remove_watchpoint(struct target
*target
,
726 struct watchpoint
*watchpoint
)
728 return target
->type
->remove_watchpoint(target
, watchpoint
);
731 int target_get_gdb_reg_list(struct target
*target
,
732 struct reg
**reg_list
[], int *reg_list_size
)
734 return target
->type
->get_gdb_reg_list(target
, reg_list
, reg_list_size
);
736 int target_step(struct target
*target
,
737 int current
, uint32_t address
, int handle_breakpoints
)
739 return target
->type
->step(target
, current
, address
, handle_breakpoints
);
744 * Reset the @c examined flag for the given target.
745 * Pure paranoia -- targets are zeroed on allocation.
747 static void target_reset_examined(struct target
*target
)
749 target
->examined
= false;
753 err_read_phys_memory(struct target
*target
, uint32_t address
,
754 uint32_t size
, uint32_t count
, uint8_t *buffer
)
756 LOG_ERROR("Not implemented: %s", __func__
);
761 err_write_phys_memory(struct target
*target
, uint32_t address
,
762 uint32_t size
, uint32_t count
, uint8_t *buffer
)
764 LOG_ERROR("Not implemented: %s", __func__
);
768 static int handle_target(void *priv
);
770 static int target_init_one(struct command_context
*cmd_ctx
,
771 struct target
*target
)
773 target_reset_examined(target
);
775 struct target_type
*type
= target
->type
;
776 if (type
->examine
== NULL
)
777 type
->examine
= default_examine
;
779 if (type
->check_reset
== NULL
)
780 type
->check_reset
= default_check_reset
;
782 int retval
= type
->init_target(cmd_ctx
, target
);
783 if (ERROR_OK
!= retval
)
785 LOG_ERROR("target '%s' init failed", target_name(target
));
790 * @todo get rid of those *memory_imp() methods, now that all
791 * callers are using target_*_memory() accessors ... and make
792 * sure the "physical" paths handle the same issues.
794 /* a non-invasive way(in terms of patches) to add some code that
795 * runs before the type->write/read_memory implementation
797 type
->write_memory_imp
= target
->type
->write_memory
;
798 type
->write_memory
= target_write_memory_imp
;
800 type
->read_memory_imp
= target
->type
->read_memory
;
801 type
->read_memory
= target_read_memory_imp
;
803 type
->soft_reset_halt_imp
= target
->type
->soft_reset_halt
;
804 type
->soft_reset_halt
= target_soft_reset_halt_imp
;
806 /* Sanity-check MMU support ... stub in what we must, to help
807 * implement it in stages, but warn if we need to do so.
811 if (type
->write_phys_memory
== NULL
)
813 LOG_ERROR("type '%s' is missing write_phys_memory",
815 type
->write_phys_memory
= err_write_phys_memory
;
817 if (type
->read_phys_memory
== NULL
)
819 LOG_ERROR("type '%s' is missing read_phys_memory",
821 type
->read_phys_memory
= err_read_phys_memory
;
823 if (type
->virt2phys
== NULL
)
825 LOG_ERROR("type '%s' is missing virt2phys", type
->name
);
826 type
->virt2phys
= identity_virt2phys
;
831 /* Make sure no-MMU targets all behave the same: make no
832 * distinction between physical and virtual addresses, and
833 * ensure that virt2phys() is always an identity mapping.
835 if (type
->write_phys_memory
|| type
->read_phys_memory
838 LOG_WARNING("type '%s' has bad MMU hooks", type
->name
);
842 type
->write_phys_memory
= type
->write_memory
;
843 type
->read_phys_memory
= type
->read_memory
;
844 type
->virt2phys
= identity_virt2phys
;
849 static int target_init(struct command_context
*cmd_ctx
)
851 struct target
*target
;
854 for (target
= all_targets
; target
; target
= target
->next
)
856 retval
= target_init_one(cmd_ctx
, target
);
857 if (ERROR_OK
!= retval
)
864 retval
= target_register_user_commands(cmd_ctx
);
865 if (ERROR_OK
!= retval
)
868 retval
= target_register_timer_callback(&handle_target
,
869 polling_interval
, 1, cmd_ctx
->interp
);
870 if (ERROR_OK
!= retval
)
876 COMMAND_HANDLER(handle_target_init_command
)
879 return ERROR_COMMAND_SYNTAX_ERROR
;
881 static bool target_initialized
= false;
882 if (target_initialized
)
884 LOG_INFO("'target init' has already been called");
887 target_initialized
= true;
889 LOG_DEBUG("Initializing targets...");
890 return target_init(CMD_CTX
);
893 int target_register_event_callback(int (*callback
)(struct target
*target
, enum target_event event
, void *priv
), void *priv
)
895 struct target_event_callback
**callbacks_p
= &target_event_callbacks
;
897 if (callback
== NULL
)
899 return ERROR_INVALID_ARGUMENTS
;
904 while ((*callbacks_p
)->next
)
905 callbacks_p
= &((*callbacks_p
)->next
);
906 callbacks_p
= &((*callbacks_p
)->next
);
909 (*callbacks_p
) = malloc(sizeof(struct target_event_callback
));
910 (*callbacks_p
)->callback
= callback
;
911 (*callbacks_p
)->priv
= priv
;
912 (*callbacks_p
)->next
= NULL
;
917 int target_register_timer_callback(int (*callback
)(void *priv
), int time_ms
, int periodic
, void *priv
)
919 struct target_timer_callback
**callbacks_p
= &target_timer_callbacks
;
922 if (callback
== NULL
)
924 return ERROR_INVALID_ARGUMENTS
;
929 while ((*callbacks_p
)->next
)
930 callbacks_p
= &((*callbacks_p
)->next
);
931 callbacks_p
= &((*callbacks_p
)->next
);
934 (*callbacks_p
) = malloc(sizeof(struct target_timer_callback
));
935 (*callbacks_p
)->callback
= callback
;
936 (*callbacks_p
)->periodic
= periodic
;
937 (*callbacks_p
)->time_ms
= time_ms
;
939 gettimeofday(&now
, NULL
);
940 (*callbacks_p
)->when
.tv_usec
= now
.tv_usec
+ (time_ms
% 1000) * 1000;
941 time_ms
-= (time_ms
% 1000);
942 (*callbacks_p
)->when
.tv_sec
= now
.tv_sec
+ (time_ms
/ 1000);
943 if ((*callbacks_p
)->when
.tv_usec
> 1000000)
945 (*callbacks_p
)->when
.tv_usec
= (*callbacks_p
)->when
.tv_usec
- 1000000;
946 (*callbacks_p
)->when
.tv_sec
+= 1;
949 (*callbacks_p
)->priv
= priv
;
950 (*callbacks_p
)->next
= NULL
;
955 int target_unregister_event_callback(int (*callback
)(struct target
*target
, enum target_event event
, void *priv
), void *priv
)
957 struct target_event_callback
**p
= &target_event_callbacks
;
958 struct target_event_callback
*c
= target_event_callbacks
;
960 if (callback
== NULL
)
962 return ERROR_INVALID_ARGUMENTS
;
967 struct target_event_callback
*next
= c
->next
;
968 if ((c
->callback
== callback
) && (c
->priv
== priv
))
982 static int target_unregister_timer_callback(int (*callback
)(void *priv
), void *priv
)
984 struct target_timer_callback
**p
= &target_timer_callbacks
;
985 struct target_timer_callback
*c
= target_timer_callbacks
;
987 if (callback
== NULL
)
989 return ERROR_INVALID_ARGUMENTS
;
994 struct target_timer_callback
*next
= c
->next
;
995 if ((c
->callback
== callback
) && (c
->priv
== priv
))
1009 int target_call_event_callbacks(struct target
*target
, enum target_event event
)
1011 struct target_event_callback
*callback
= target_event_callbacks
;
1012 struct target_event_callback
*next_callback
;
1014 if (event
== TARGET_EVENT_HALTED
)
1016 /* execute early halted first */
1017 target_call_event_callbacks(target
, TARGET_EVENT_GDB_HALT
);
1020 LOG_DEBUG("target event %i (%s)",
1022 Jim_Nvp_value2name_simple(nvp_target_event
, event
)->name
);
1024 target_handle_event(target
, event
);
1028 next_callback
= callback
->next
;
1029 callback
->callback(target
, event
, callback
->priv
);
1030 callback
= next_callback
;
1036 static int target_timer_callback_periodic_restart(
1037 struct target_timer_callback
*cb
, struct timeval
*now
)
1039 int time_ms
= cb
->time_ms
;
1040 cb
->when
.tv_usec
= now
->tv_usec
+ (time_ms
% 1000) * 1000;
1041 time_ms
-= (time_ms
% 1000);
1042 cb
->when
.tv_sec
= now
->tv_sec
+ time_ms
/ 1000;
1043 if (cb
->when
.tv_usec
> 1000000)
1045 cb
->when
.tv_usec
= cb
->when
.tv_usec
- 1000000;
1046 cb
->when
.tv_sec
+= 1;
1051 static int target_call_timer_callback(struct target_timer_callback
*cb
,
1052 struct timeval
*now
)
1054 cb
->callback(cb
->priv
);
1057 return target_timer_callback_periodic_restart(cb
, now
);
1059 return target_unregister_timer_callback(cb
->callback
, cb
->priv
);
1062 static int target_call_timer_callbacks_check_time(int checktime
)
1067 gettimeofday(&now
, NULL
);
1069 struct target_timer_callback
*callback
= target_timer_callbacks
;
1072 // cleaning up may unregister and free this callback
1073 struct target_timer_callback
*next_callback
= callback
->next
;
1075 bool call_it
= callback
->callback
&&
1076 ((!checktime
&& callback
->periodic
) ||
1077 now
.tv_sec
> callback
->when
.tv_sec
||
1078 (now
.tv_sec
== callback
->when
.tv_sec
&&
1079 now
.tv_usec
>= callback
->when
.tv_usec
));
1083 int retval
= target_call_timer_callback(callback
, &now
);
1084 if (retval
!= ERROR_OK
)
1088 callback
= next_callback
;
1094 int target_call_timer_callbacks(void)
1096 return target_call_timer_callbacks_check_time(1);
1099 /* invoke periodic callbacks immediately */
1100 int target_call_timer_callbacks_now(void)
1102 return target_call_timer_callbacks_check_time(0);
1105 int target_alloc_working_area_try(struct target
*target
, uint32_t size
, struct working_area
**area
)
1107 struct working_area
*c
= target
->working_areas
;
1108 struct working_area
*new_wa
= NULL
;
1110 /* Reevaluate working area address based on MMU state*/
1111 if (target
->working_areas
== NULL
)
1116 retval
= target
->type
->mmu(target
, &enabled
);
1117 if (retval
!= ERROR_OK
)
1123 if (target
->working_area_phys_spec
) {
1124 LOG_DEBUG("MMU disabled, using physical "
1125 "address for working memory 0x%08x",
1126 (unsigned)target
->working_area_phys
);
1127 target
->working_area
= target
->working_area_phys
;
1129 LOG_ERROR("No working memory available. "
1130 "Specify -work-area-phys to target.");
1131 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
1134 if (target
->working_area_virt_spec
) {
1135 LOG_DEBUG("MMU enabled, using virtual "
1136 "address for working memory 0x%08x",
1137 (unsigned)target
->working_area_virt
);
1138 target
->working_area
= target
->working_area_virt
;
1140 LOG_ERROR("No working memory available. "
1141 "Specify -work-area-virt to target.");
1142 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
1147 /* only allocate multiples of 4 byte */
1150 LOG_ERROR("BUG: code tried to allocate unaligned number of bytes (0x%08x), padding", ((unsigned)(size
)));
1151 size
= (size
+ 3) & (~3);
1154 /* see if there's already a matching working area */
1157 if ((c
->free
) && (c
->size
== size
))
1165 /* if not, allocate a new one */
1168 struct working_area
**p
= &target
->working_areas
;
1169 uint32_t first_free
= target
->working_area
;
1170 uint32_t free_size
= target
->working_area_size
;
1172 c
= target
->working_areas
;
1175 first_free
+= c
->size
;
1176 free_size
-= c
->size
;
1181 if (free_size
< size
)
1183 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
1186 LOG_DEBUG("allocated new working area at address 0x%08x", (unsigned)first_free
);
1188 new_wa
= malloc(sizeof(struct working_area
));
1189 new_wa
->next
= NULL
;
1190 new_wa
->size
= size
;
1191 new_wa
->address
= first_free
;
1193 if (target
->backup_working_area
)
1196 new_wa
->backup
= malloc(new_wa
->size
);
1197 if ((retval
= target_read_memory(target
, new_wa
->address
, 4, new_wa
->size
/ 4, new_wa
->backup
)) != ERROR_OK
)
1199 free(new_wa
->backup
);
1206 new_wa
->backup
= NULL
;
1209 /* put new entry in list */
1213 /* mark as used, and return the new (reused) area */
1218 new_wa
->user
= area
;
1223 int target_alloc_working_area(struct target
*target
, uint32_t size
, struct working_area
**area
)
1227 retval
= target_alloc_working_area_try(target
, size
, area
);
1228 if (retval
== ERROR_TARGET_RESOURCE_NOT_AVAILABLE
)
1230 LOG_WARNING("not enough working area available(requested %u)", (unsigned)(size
));
1236 static int target_free_working_area_restore(struct target
*target
, struct working_area
*area
, int restore
)
1241 if (restore
&& target
->backup_working_area
)
1244 if ((retval
= target_write_memory(target
, area
->address
, 4, area
->size
/ 4, area
->backup
)) != ERROR_OK
)
1250 /* mark user pointer invalid */
1257 int target_free_working_area(struct target
*target
, struct working_area
*area
)
1259 return target_free_working_area_restore(target
, area
, 1);
1262 /* free resources and restore memory, if restoring memory fails,
1263 * free up resources anyway
1265 static void target_free_all_working_areas_restore(struct target
*target
, int restore
)
1267 struct working_area
*c
= target
->working_areas
;
1271 struct working_area
*next
= c
->next
;
1272 target_free_working_area_restore(target
, c
, restore
);
1282 target
->working_areas
= NULL
;
1285 void target_free_all_working_areas(struct target
*target
)
1287 target_free_all_working_areas_restore(target
, 1);
1290 int target_arch_state(struct target
*target
)
1295 LOG_USER("No target has been configured");
1299 LOG_USER("target state: %s", target_state_name( target
));
1301 if (target
->state
!= TARGET_HALTED
)
1304 retval
= target
->type
->arch_state(target
);
1308 /* Single aligned words are guaranteed to use 16 or 32 bit access
1309 * mode respectively, otherwise data is handled as quickly as
1312 int target_write_buffer(struct target
*target
, uint32_t address
, uint32_t size
, uint8_t *buffer
)
1315 LOG_DEBUG("writing buffer of %i byte at 0x%8.8x",
1316 (int)size
, (unsigned)address
);
1318 if (!target_was_examined(target
))
1320 LOG_ERROR("Target not examined yet");
1328 if ((address
+ size
- 1) < address
)
1330 /* GDB can request this when e.g. PC is 0xfffffffc*/
1331 LOG_ERROR("address + size wrapped(0x%08x, 0x%08x)",
1337 if (((address
% 2) == 0) && (size
== 2))
1339 return target_write_memory(target
, address
, 2, 1, buffer
);
1342 /* handle unaligned head bytes */
1345 uint32_t unaligned
= 4 - (address
% 4);
1347 if (unaligned
> size
)
1350 if ((retval
= target_write_memory(target
, address
, 1, unaligned
, buffer
)) != ERROR_OK
)
1353 buffer
+= unaligned
;
1354 address
+= unaligned
;
1358 /* handle aligned words */
1361 int aligned
= size
- (size
% 4);
1363 /* use bulk writes above a certain limit. This may have to be changed */
1366 if ((retval
= target
->type
->bulk_write_memory(target
, address
, aligned
/ 4, buffer
)) != ERROR_OK
)
1371 if ((retval
= target_write_memory(target
, address
, 4, aligned
/ 4, buffer
)) != ERROR_OK
)
1380 /* handle tail writes of less than 4 bytes */
1383 if ((retval
= target_write_memory(target
, address
, 1, size
, buffer
)) != ERROR_OK
)
1390 /* Single aligned words are guaranteed to use 16 or 32 bit access
1391 * mode respectively, otherwise data is handled as quickly as
1394 int target_read_buffer(struct target
*target
, uint32_t address
, uint32_t size
, uint8_t *buffer
)
1397 LOG_DEBUG("reading buffer of %i byte at 0x%8.8x",
1398 (int)size
, (unsigned)address
);
1400 if (!target_was_examined(target
))
1402 LOG_ERROR("Target not examined yet");
1410 if ((address
+ size
- 1) < address
)
1412 /* GDB can request this when e.g. PC is 0xfffffffc*/
1413 LOG_ERROR("address + size wrapped(0x%08" PRIx32
", 0x%08" PRIx32
")",
1419 if (((address
% 2) == 0) && (size
== 2))
1421 return target_read_memory(target
, address
, 2, 1, buffer
);
1424 /* handle unaligned head bytes */
1427 uint32_t unaligned
= 4 - (address
% 4);
1429 if (unaligned
> size
)
1432 if ((retval
= target_read_memory(target
, address
, 1, unaligned
, buffer
)) != ERROR_OK
)
1435 buffer
+= unaligned
;
1436 address
+= unaligned
;
1440 /* handle aligned words */
1443 int aligned
= size
- (size
% 4);
1445 if ((retval
= target_read_memory(target
, address
, 4, aligned
/ 4, buffer
)) != ERROR_OK
)
1453 /*prevent byte access when possible (avoid AHB access limitations in some cases)*/
1456 int aligned
= size
- (size
%2);
1457 retval
= target_read_memory(target
, address
, 2, aligned
/ 2, buffer
);
1458 if (retval
!= ERROR_OK
)
1465 /* handle tail writes of less than 4 bytes */
1468 if ((retval
= target_read_memory(target
, address
, 1, size
, buffer
)) != ERROR_OK
)
1475 int target_checksum_memory(struct target
*target
, uint32_t address
, uint32_t size
, uint32_t* crc
)
1480 uint32_t checksum
= 0;
1481 if (!target_was_examined(target
))
1483 LOG_ERROR("Target not examined yet");
1487 if ((retval
= target
->type
->checksum_memory(target
, address
,
1488 size
, &checksum
)) != ERROR_OK
)
1490 buffer
= malloc(size
);
1493 LOG_ERROR("error allocating buffer for section (%d bytes)", (int)size
);
1494 return ERROR_INVALID_ARGUMENTS
;
1496 retval
= target_read_buffer(target
, address
, size
, buffer
);
1497 if (retval
!= ERROR_OK
)
1503 /* convert to target endianess */
1504 for (i
= 0; i
< (size
/sizeof(uint32_t)); i
++)
1506 uint32_t target_data
;
1507 target_data
= target_buffer_get_u32(target
, &buffer
[i
*sizeof(uint32_t)]);
1508 target_buffer_set_u32(target
, &buffer
[i
*sizeof(uint32_t)], target_data
);
1511 retval
= image_calculate_checksum(buffer
, size
, &checksum
);
1520 int target_blank_check_memory(struct target
*target
, uint32_t address
, uint32_t size
, uint32_t* blank
)
1523 if (!target_was_examined(target
))
1525 LOG_ERROR("Target not examined yet");
1529 if (target
->type
->blank_check_memory
== 0)
1530 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
1532 retval
= target
->type
->blank_check_memory(target
, address
, size
, blank
);
1537 int target_read_u32(struct target
*target
, uint32_t address
, uint32_t *value
)
1539 uint8_t value_buf
[4];
1540 if (!target_was_examined(target
))
1542 LOG_ERROR("Target not examined yet");
1546 int retval
= target_read_memory(target
, address
, 4, 1, value_buf
);
1548 if (retval
== ERROR_OK
)
1550 *value
= target_buffer_get_u32(target
, value_buf
);
1551 LOG_DEBUG("address: 0x%8.8" PRIx32
", value: 0x%8.8" PRIx32
"",
1558 LOG_DEBUG("address: 0x%8.8" PRIx32
" failed",
1565 int target_read_u16(struct target
*target
, uint32_t address
, uint16_t *value
)
1567 uint8_t value_buf
[2];
1568 if (!target_was_examined(target
))
1570 LOG_ERROR("Target not examined yet");
1574 int retval
= target_read_memory(target
, address
, 2, 1, value_buf
);
1576 if (retval
== ERROR_OK
)
1578 *value
= target_buffer_get_u16(target
, value_buf
);
1579 LOG_DEBUG("address: 0x%8.8" PRIx32
", value: 0x%4.4x",
1586 LOG_DEBUG("address: 0x%8.8" PRIx32
" failed",
1593 int target_read_u8(struct target
*target
, uint32_t address
, uint8_t *value
)
1595 int retval
= target_read_memory(target
, address
, 1, 1, value
);
1596 if (!target_was_examined(target
))
1598 LOG_ERROR("Target not examined yet");
1602 if (retval
== ERROR_OK
)
1604 LOG_DEBUG("address: 0x%8.8" PRIx32
", value: 0x%2.2x",
1611 LOG_DEBUG("address: 0x%8.8" PRIx32
" failed",
1618 int target_write_u32(struct target
*target
, uint32_t address
, uint32_t value
)
1621 uint8_t value_buf
[4];
1622 if (!target_was_examined(target
))
1624 LOG_ERROR("Target not examined yet");
1628 LOG_DEBUG("address: 0x%8.8" PRIx32
", value: 0x%8.8" PRIx32
"",
1632 target_buffer_set_u32(target
, value_buf
, value
);
1633 if ((retval
= target_write_memory(target
, address
, 4, 1, value_buf
)) != ERROR_OK
)
1635 LOG_DEBUG("failed: %i", retval
);
1641 int target_write_u16(struct target
*target
, uint32_t address
, uint16_t value
)
1644 uint8_t value_buf
[2];
1645 if (!target_was_examined(target
))
1647 LOG_ERROR("Target not examined yet");
1651 LOG_DEBUG("address: 0x%8.8" PRIx32
", value: 0x%8.8x",
1655 target_buffer_set_u16(target
, value_buf
, value
);
1656 if ((retval
= target_write_memory(target
, address
, 2, 1, value_buf
)) != ERROR_OK
)
1658 LOG_DEBUG("failed: %i", retval
);
1664 int target_write_u8(struct target
*target
, uint32_t address
, uint8_t value
)
1667 if (!target_was_examined(target
))
1669 LOG_ERROR("Target not examined yet");
1673 LOG_DEBUG("address: 0x%8.8" PRIx32
", value: 0x%2.2x",
1676 if ((retval
= target_write_memory(target
, address
, 1, 1, &value
)) != ERROR_OK
)
1678 LOG_DEBUG("failed: %i", retval
);
1684 COMMAND_HANDLER(handle_targets_command
)
1686 struct target
*target
= all_targets
;
1690 target
= get_target(CMD_ARGV
[0]);
1691 if (target
== NULL
) {
1692 command_print(CMD_CTX
,"Target: %s is unknown, try one of:\n", CMD_ARGV
[0]);
1695 if (!target
->tap
->enabled
) {
1696 command_print(CMD_CTX
,"Target: TAP %s is disabled, "
1697 "can't be the current target\n",
1698 target
->tap
->dotted_name
);
1702 CMD_CTX
->current_target
= target
->target_number
;
1707 target
= all_targets
;
1708 command_print(CMD_CTX
, " TargetName Type Endian TapName State ");
1709 command_print(CMD_CTX
, "-- ------------------ ---------- ------ ------------------ ------------");
1715 if (target
->tap
->enabled
)
1716 state
= target_state_name( target
);
1718 state
= "tap-disabled";
1720 if (CMD_CTX
->current_target
== target
->target_number
)
1723 /* keep columns lined up to match the headers above */
1724 command_print(CMD_CTX
, "%2d%c %-18s %-10s %-6s %-18s %s",
1725 target
->target_number
,
1727 target_name(target
),
1728 target_type_name(target
),
1729 Jim_Nvp_value2name_simple(nvp_target_endian
,
1730 target
->endianness
)->name
,
1731 target
->tap
->dotted_name
,
1733 target
= target
->next
;
1739 /* every 300ms we check for reset & powerdropout and issue a "reset halt" if so. */
1741 static int powerDropout
;
1742 static int srstAsserted
;
1744 static int runPowerRestore
;
1745 static int runPowerDropout
;
1746 static int runSrstAsserted
;
1747 static int runSrstDeasserted
;
1749 static int sense_handler(void)
1751 static int prevSrstAsserted
= 0;
1752 static int prevPowerdropout
= 0;
1755 if ((retval
= jtag_power_dropout(&powerDropout
)) != ERROR_OK
)
1759 powerRestored
= prevPowerdropout
&& !powerDropout
;
1762 runPowerRestore
= 1;
1765 long long current
= timeval_ms();
1766 static long long lastPower
= 0;
1767 int waitMore
= lastPower
+ 2000 > current
;
1768 if (powerDropout
&& !waitMore
)
1770 runPowerDropout
= 1;
1771 lastPower
= current
;
1774 if ((retval
= jtag_srst_asserted(&srstAsserted
)) != ERROR_OK
)
1778 srstDeasserted
= prevSrstAsserted
&& !srstAsserted
;
1780 static long long lastSrst
= 0;
1781 waitMore
= lastSrst
+ 2000 > current
;
1782 if (srstDeasserted
&& !waitMore
)
1784 runSrstDeasserted
= 1;
1788 if (!prevSrstAsserted
&& srstAsserted
)
1790 runSrstAsserted
= 1;
1793 prevSrstAsserted
= srstAsserted
;
1794 prevPowerdropout
= powerDropout
;
1796 if (srstDeasserted
|| powerRestored
)
1798 /* Other than logging the event we can't do anything here.
1799 * Issuing a reset is a particularly bad idea as we might
1800 * be inside a reset already.
1807 static int backoff_times
= 0;
1808 static int backoff_count
= 0;
1810 /* process target state changes */
1811 static int handle_target(void *priv
)
1813 Jim_Interp
*interp
= (Jim_Interp
*)priv
;
1814 int retval
= ERROR_OK
;
1816 if (!is_jtag_poll_safe())
1818 /* polling is disabled currently */
1822 /* we do not want to recurse here... */
1823 static int recursive
= 0;
1828 /* danger! running these procedures can trigger srst assertions and power dropouts.
1829 * We need to avoid an infinite loop/recursion here and we do that by
1830 * clearing the flags after running these events.
1832 int did_something
= 0;
1833 if (runSrstAsserted
)
1835 LOG_INFO("srst asserted detected, running srst_asserted proc.");
1836 Jim_Eval(interp
, "srst_asserted");
1839 if (runSrstDeasserted
)
1841 Jim_Eval(interp
, "srst_deasserted");
1844 if (runPowerDropout
)
1846 LOG_INFO("Power dropout detected, running power_dropout proc.");
1847 Jim_Eval(interp
, "power_dropout");
1850 if (runPowerRestore
)
1852 Jim_Eval(interp
, "power_restore");
1858 /* clear detect flags */
1862 /* clear action flags */
1864 runSrstAsserted
= 0;
1865 runSrstDeasserted
= 0;
1866 runPowerRestore
= 0;
1867 runPowerDropout
= 0;
1872 if (backoff_times
> backoff_count
)
1874 /* do not poll this time as we failed previously */
1880 /* Poll targets for state changes unless that's globally disabled.
1881 * Skip targets that are currently disabled.
1883 for (struct target
*target
= all_targets
;
1884 is_jtag_poll_safe() && target
;
1885 target
= target
->next
)
1887 if (!target
->tap
->enabled
)
1890 /* only poll target if we've got power and srst isn't asserted */
1891 if (!powerDropout
&& !srstAsserted
)
1893 /* polling may fail silently until the target has been examined */
1894 if ((retval
= target_poll(target
)) != ERROR_OK
)
1896 /* 100ms polling interval. Increase interval between polling up to 5000ms */
1897 if (backoff_times
* polling_interval
< 5000)
1902 LOG_USER("Polling target failed, GDB will be halted. Polling again in %dms", backoff_times
* polling_interval
);
1904 /* Tell GDB to halt the debugger. This allows the user to
1905 * run monitor commands to handle the situation.
1907 target_call_event_callbacks(target
, TARGET_EVENT_GDB_HALT
);
1910 /* Since we succeeded, we reset backoff count */
1911 if (backoff_times
> 0)
1913 LOG_USER("Polling succeeded again");
1922 COMMAND_HANDLER(handle_reg_command
)
1924 struct target
*target
;
1925 struct reg
*reg
= NULL
;
1931 target
= get_current_target(CMD_CTX
);
1933 /* list all available registers for the current target */
1936 struct reg_cache
*cache
= target
->reg_cache
;
1943 command_print(CMD_CTX
, "===== %s", cache
->name
);
1945 for (i
= 0, reg
= cache
->reg_list
;
1946 i
< cache
->num_regs
;
1947 i
++, reg
++, count
++)
1949 /* only print cached values if they are valid */
1951 value
= buf_to_str(reg
->value
,
1953 command_print(CMD_CTX
,
1954 "(%i) %s (/%" PRIu32
"): 0x%s%s",
1962 command_print(CMD_CTX
, "(%i) %s (/%" PRIu32
")",
1967 cache
= cache
->next
;
1973 /* access a single register by its ordinal number */
1974 if ((CMD_ARGV
[0][0] >= '0') && (CMD_ARGV
[0][0] <= '9'))
1977 COMMAND_PARSE_NUMBER(uint
, CMD_ARGV
[0], num
);
1979 struct reg_cache
*cache
= target
->reg_cache
;
1984 for (i
= 0; i
< cache
->num_regs
; i
++)
1988 reg
= &cache
->reg_list
[i
];
1994 cache
= cache
->next
;
1999 command_print(CMD_CTX
, "%i is out of bounds, the current target has only %i registers (0 - %i)", num
, count
, count
- 1);
2002 } else /* access a single register by its name */
2004 reg
= register_get_by_name(target
->reg_cache
, CMD_ARGV
[0], 1);
2008 command_print(CMD_CTX
, "register %s not found in current target", CMD_ARGV
[0]);
2013 /* display a register */
2014 if ((CMD_ARGC
== 1) || ((CMD_ARGC
== 2) && !((CMD_ARGV
[1][0] >= '0') && (CMD_ARGV
[1][0] <= '9'))))
2016 if ((CMD_ARGC
== 2) && (strcmp(CMD_ARGV
[1], "force") == 0))
2019 if (reg
->valid
== 0)
2021 reg
->type
->get(reg
);
2023 value
= buf_to_str(reg
->value
, reg
->size
, 16);
2024 command_print(CMD_CTX
, "%s (/%i): 0x%s", reg
->name
, (int)(reg
->size
), value
);
2029 /* set register value */
2032 uint8_t *buf
= malloc(DIV_ROUND_UP(reg
->size
, 8));
2033 str_to_buf(CMD_ARGV
[1], strlen(CMD_ARGV
[1]), buf
, reg
->size
, 0);
2035 reg
->type
->set(reg
, buf
);
2037 value
= buf_to_str(reg
->value
, reg
->size
, 16);
2038 command_print(CMD_CTX
, "%s (/%i): 0x%s", reg
->name
, (int)(reg
->size
), value
);
2046 command_print(CMD_CTX
, "usage: reg <#|name> [value]");
2051 COMMAND_HANDLER(handle_poll_command
)
2053 int retval
= ERROR_OK
;
2054 struct target
*target
= get_current_target(CMD_CTX
);
2058 command_print(CMD_CTX
, "background polling: %s",
2059 jtag_poll_get_enabled() ? "on" : "off");
2060 command_print(CMD_CTX
, "TAP: %s (%s)",
2061 target
->tap
->dotted_name
,
2062 target
->tap
->enabled
? "enabled" : "disabled");
2063 if (!target
->tap
->enabled
)
2065 if ((retval
= target_poll(target
)) != ERROR_OK
)
2067 if ((retval
= target_arch_state(target
)) != ERROR_OK
)
2070 else if (CMD_ARGC
== 1)
2073 COMMAND_PARSE_ON_OFF(CMD_ARGV
[0], enable
);
2074 jtag_poll_set_enabled(enable
);
2078 return ERROR_COMMAND_SYNTAX_ERROR
;
2084 COMMAND_HANDLER(handle_wait_halt_command
)
2087 return ERROR_COMMAND_SYNTAX_ERROR
;
2092 int retval
= parse_uint(CMD_ARGV
[0], &ms
);
2093 if (ERROR_OK
!= retval
)
2095 command_print(CMD_CTX
, "usage: %s [seconds]", CMD_NAME
);
2096 return ERROR_COMMAND_SYNTAX_ERROR
;
2098 // convert seconds (given) to milliseconds (needed)
2102 struct target
*target
= get_current_target(CMD_CTX
);
2103 return target_wait_state(target
, TARGET_HALTED
, ms
);
2106 /* wait for target state to change. The trick here is to have a low
2107 * latency for short waits and not to suck up all the CPU time
2110 * After 500ms, keep_alive() is invoked
2112 int target_wait_state(struct target
*target
, enum target_state state
, int ms
)
2115 long long then
= 0, cur
;
2120 if ((retval
= target_poll(target
)) != ERROR_OK
)
2122 if (target
->state
== state
)
2130 then
= timeval_ms();
2131 LOG_DEBUG("waiting for target %s...",
2132 Jim_Nvp_value2name_simple(nvp_target_state
,state
)->name
);
2140 if ((cur
-then
) > ms
)
2142 LOG_ERROR("timed out while waiting for target %s",
2143 Jim_Nvp_value2name_simple(nvp_target_state
,state
)->name
);
2151 COMMAND_HANDLER(handle_halt_command
)
2155 struct target
*target
= get_current_target(CMD_CTX
);
2156 int retval
= target_halt(target
);
2157 if (ERROR_OK
!= retval
)
2162 unsigned wait_local
;
2163 retval
= parse_uint(CMD_ARGV
[0], &wait_local
);
2164 if (ERROR_OK
!= retval
)
2165 return ERROR_COMMAND_SYNTAX_ERROR
;
2170 return CALL_COMMAND_HANDLER(handle_wait_halt_command
);
2173 COMMAND_HANDLER(handle_soft_reset_halt_command
)
2175 struct target
*target
= get_current_target(CMD_CTX
);
2177 LOG_USER("requesting target halt and executing a soft reset");
2179 target
->type
->soft_reset_halt(target
);
2184 COMMAND_HANDLER(handle_reset_command
)
2187 return ERROR_COMMAND_SYNTAX_ERROR
;
2189 enum target_reset_mode reset_mode
= RESET_RUN
;
2193 n
= Jim_Nvp_name2value_simple(nvp_reset_modes
, CMD_ARGV
[0]);
2194 if ((n
->name
== NULL
) || (n
->value
== RESET_UNKNOWN
)) {
2195 return ERROR_COMMAND_SYNTAX_ERROR
;
2197 reset_mode
= n
->value
;
2200 /* reset *all* targets */
2201 return target_process_reset(CMD_CTX
, reset_mode
);
2205 COMMAND_HANDLER(handle_resume_command
)
2209 return ERROR_COMMAND_SYNTAX_ERROR
;
2211 struct target
*target
= get_current_target(CMD_CTX
);
2212 target_handle_event(target
, TARGET_EVENT_OLD_pre_resume
);
2214 /* with no CMD_ARGV, resume from current pc, addr = 0,
2215 * with one arguments, addr = CMD_ARGV[0],
2216 * handle breakpoints, not debugging */
2220 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], addr
);
2224 return target_resume(target
, current
, addr
, 1, 0);
2227 COMMAND_HANDLER(handle_step_command
)
2230 return ERROR_COMMAND_SYNTAX_ERROR
;
2234 /* with no CMD_ARGV, step from current pc, addr = 0,
2235 * with one argument addr = CMD_ARGV[0],
2236 * handle breakpoints, debugging */
2241 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], addr
);
2245 struct target
*target
= get_current_target(CMD_CTX
);
2247 return target
->type
->step(target
, current_pc
, addr
, 1);
2250 static void handle_md_output(struct command_context
*cmd_ctx
,
2251 struct target
*target
, uint32_t address
, unsigned size
,
2252 unsigned count
, const uint8_t *buffer
)
2254 const unsigned line_bytecnt
= 32;
2255 unsigned line_modulo
= line_bytecnt
/ size
;
2257 char output
[line_bytecnt
* 4 + 1];
2258 unsigned output_len
= 0;
2260 const char *value_fmt
;
2262 case 4: value_fmt
= "%8.8x "; break;
2263 case 2: value_fmt
= "%4.4x "; break;
2264 case 1: value_fmt
= "%2.2x "; break;
2266 /* "can't happen", caller checked */
2267 LOG_ERROR("invalid memory read size: %u", size
);
2271 for (unsigned i
= 0; i
< count
; i
++)
2273 if (i
% line_modulo
== 0)
2275 output_len
+= snprintf(output
+ output_len
,
2276 sizeof(output
) - output_len
,
2278 (unsigned)(address
+ (i
*size
)));
2282 const uint8_t *value_ptr
= buffer
+ i
* size
;
2284 case 4: value
= target_buffer_get_u32(target
, value_ptr
); break;
2285 case 2: value
= target_buffer_get_u16(target
, value_ptr
); break;
2286 case 1: value
= *value_ptr
;
2288 output_len
+= snprintf(output
+ output_len
,
2289 sizeof(output
) - output_len
,
2292 if ((i
% line_modulo
== line_modulo
- 1) || (i
== count
- 1))
2294 command_print(cmd_ctx
, "%s", output
);
2300 COMMAND_HANDLER(handle_md_command
)
2303 return ERROR_COMMAND_SYNTAX_ERROR
;
2306 switch (CMD_NAME
[2]) {
2307 case 'w': size
= 4; break;
2308 case 'h': size
= 2; break;
2309 case 'b': size
= 1; break;
2310 default: return ERROR_COMMAND_SYNTAX_ERROR
;
2313 bool physical
=strcmp(CMD_ARGV
[0], "phys")==0;
2314 int (*fn
)(struct target
*target
,
2315 uint32_t address
, uint32_t size
, uint32_t count
, uint8_t *buffer
);
2320 fn
=target_read_phys_memory
;
2323 fn
=target_read_memory
;
2325 if ((CMD_ARGC
< 1) || (CMD_ARGC
> 2))
2327 return ERROR_COMMAND_SYNTAX_ERROR
;
2331 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], address
);
2335 COMMAND_PARSE_NUMBER(uint
, CMD_ARGV
[1], count
);
2337 uint8_t *buffer
= calloc(count
, size
);
2339 struct target
*target
= get_current_target(CMD_CTX
);
2340 int retval
= fn(target
, address
, size
, count
, buffer
);
2341 if (ERROR_OK
== retval
)
2342 handle_md_output(CMD_CTX
, target
, address
, size
, count
, buffer
);
2349 typedef int (*target_write_fn
)(struct target
*target
,
2350 uint32_t address
, uint32_t size
, uint32_t count
, uint8_t *buffer
);
2352 static int target_write_memory_fast(struct target
*target
,
2353 uint32_t address
, uint32_t size
, uint32_t count
, uint8_t *buffer
)
2355 return target_write_buffer(target
, address
, size
* count
, buffer
);
2358 static int target_fill_mem(struct target
*target
,
2367 /* We have to write in reasonably large chunks to be able
2368 * to fill large memory areas with any sane speed */
2369 const unsigned chunk_size
= 16384;
2370 uint8_t *target_buf
= malloc(chunk_size
* data_size
);
2371 if (target_buf
== NULL
)
2373 LOG_ERROR("Out of memory");
2377 for (unsigned i
= 0; i
< chunk_size
; i
++)
2382 target_buffer_set_u32(target
, target_buf
+ i
*data_size
, b
);
2385 target_buffer_set_u16(target
, target_buf
+ i
*data_size
, b
);
2388 target_buffer_set_u8(target
, target_buf
+ i
*data_size
, b
);
2395 int retval
= ERROR_OK
;
2397 for (unsigned x
= 0; x
< c
; x
+= chunk_size
)
2401 if (current
> chunk_size
)
2403 current
= chunk_size
;
2405 retval
= fn(target
, address
+ x
* data_size
, data_size
, current
, target_buf
);
2406 if (retval
!= ERROR_OK
)
2410 /* avoid GDB timeouts */
2419 COMMAND_HANDLER(handle_mw_command
)
2423 return ERROR_COMMAND_SYNTAX_ERROR
;
2425 bool physical
=strcmp(CMD_ARGV
[0], "phys")==0;
2431 fn
=target_write_phys_memory
;
2434 fn
= target_write_memory_fast
;
2436 if ((CMD_ARGC
< 2) || (CMD_ARGC
> 3))
2437 return ERROR_COMMAND_SYNTAX_ERROR
;
2440 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], address
);
2443 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], value
);
2447 COMMAND_PARSE_NUMBER(uint
, CMD_ARGV
[2], count
);
2449 struct target
*target
= get_current_target(CMD_CTX
);
2451 switch (CMD_NAME
[2])
2463 return ERROR_COMMAND_SYNTAX_ERROR
;
2466 return target_fill_mem(target
, address
, fn
, wordsize
, value
, count
);
2469 static COMMAND_HELPER(parse_load_image_command_CMD_ARGV
, struct image
*image
,
2470 uint32_t *min_address
, uint32_t *max_address
)
2472 if (CMD_ARGC
< 1 || CMD_ARGC
> 5)
2473 return ERROR_COMMAND_SYNTAX_ERROR
;
2475 /* a base address isn't always necessary,
2476 * default to 0x0 (i.e. don't relocate) */
2480 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], addr
);
2481 image
->base_address
= addr
;
2482 image
->base_address_set
= 1;
2485 image
->base_address_set
= 0;
2487 image
->start_address_set
= 0;
2491 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[3], *min_address
);
2495 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[4], *max_address
);
2496 // use size (given) to find max (required)
2497 *max_address
+= *min_address
;
2500 if (*min_address
> *max_address
)
2501 return ERROR_COMMAND_SYNTAX_ERROR
;
2506 COMMAND_HANDLER(handle_load_image_command
)
2510 uint32_t image_size
;
2511 uint32_t min_address
= 0;
2512 uint32_t max_address
= 0xffffffff;
2516 int retval
= CALL_COMMAND_HANDLER(parse_load_image_command_CMD_ARGV
,
2517 &image
, &min_address
, &max_address
);
2518 if (ERROR_OK
!= retval
)
2521 struct target
*target
= get_current_target(CMD_CTX
);
2523 struct duration bench
;
2524 duration_start(&bench
);
2526 if (image_open(&image
, CMD_ARGV
[0], (CMD_ARGC
>= 3) ? CMD_ARGV
[2] : NULL
) != ERROR_OK
)
2533 for (i
= 0; i
< image
.num_sections
; i
++)
2535 buffer
= malloc(image
.sections
[i
].size
);
2538 command_print(CMD_CTX
,
2539 "error allocating buffer for section (%d bytes)",
2540 (int)(image
.sections
[i
].size
));
2544 if ((retval
= image_read_section(&image
, i
, 0x0, image
.sections
[i
].size
, buffer
, &buf_cnt
)) != ERROR_OK
)
2550 uint32_t offset
= 0;
2551 uint32_t length
= buf_cnt
;
2553 /* DANGER!!! beware of unsigned comparision here!!! */
2555 if ((image
.sections
[i
].base_address
+ buf_cnt
>= min_address
)&&
2556 (image
.sections
[i
].base_address
< max_address
))
2558 if (image
.sections
[i
].base_address
< min_address
)
2560 /* clip addresses below */
2561 offset
+= min_address
-image
.sections
[i
].base_address
;
2565 if (image
.sections
[i
].base_address
+ buf_cnt
> max_address
)
2567 length
-= (image
.sections
[i
].base_address
+ buf_cnt
)-max_address
;
2570 if ((retval
= target_write_buffer(target
, image
.sections
[i
].base_address
+ offset
, length
, buffer
+ offset
)) != ERROR_OK
)
2575 image_size
+= length
;
2576 command_print(CMD_CTX
, "%u bytes written at address 0x%8.8" PRIx32
"",
2577 (unsigned int)length
,
2578 image
.sections
[i
].base_address
+ offset
);
2584 if ((ERROR_OK
== retval
) && (duration_measure(&bench
) == ERROR_OK
))
2586 command_print(CMD_CTX
, "downloaded %" PRIu32
" bytes "
2587 "in %fs (%0.3f KiB/s)", image_size
,
2588 duration_elapsed(&bench
), duration_kbps(&bench
, image_size
));
2591 image_close(&image
);
2597 COMMAND_HANDLER(handle_dump_image_command
)
2599 struct fileio fileio
;
2600 uint8_t buffer
[560];
2601 int retval
, retvaltemp
;
2602 uint32_t address
, size
;
2603 struct duration bench
;
2604 struct target
*target
= get_current_target(CMD_CTX
);
2607 return ERROR_COMMAND_SYNTAX_ERROR
;
2609 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], address
);
2610 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[2], size
);
2612 retval
= fileio_open(&fileio
, CMD_ARGV
[0], FILEIO_WRITE
, FILEIO_BINARY
);
2613 if (retval
!= ERROR_OK
)
2616 duration_start(&bench
);
2621 size_t size_written
;
2622 uint32_t this_run_size
= (size
> 560) ? 560 : size
;
2623 retval
= target_read_buffer(target
, address
, this_run_size
, buffer
);
2624 if (retval
!= ERROR_OK
)
2629 retval
= fileio_write(&fileio
, this_run_size
, buffer
, &size_written
);
2630 if (retval
!= ERROR_OK
)
2635 size
-= this_run_size
;
2636 address
+= this_run_size
;
2639 if ((ERROR_OK
== retval
) && (duration_measure(&bench
) == ERROR_OK
))
2642 retval
= fileio_size(&fileio
, &filesize
);
2643 if (retval
!= ERROR_OK
)
2645 command_print(CMD_CTX
,
2646 "dumped %ld bytes in %fs (%0.3f KiB/s)", (long)filesize
,
2647 duration_elapsed(&bench
), duration_kbps(&bench
, filesize
));
2650 if ((retvaltemp
= fileio_close(&fileio
)) != ERROR_OK
)
2656 static COMMAND_HELPER(handle_verify_image_command_internal
, int verify
)
2660 uint32_t image_size
;
2663 uint32_t checksum
= 0;
2664 uint32_t mem_checksum
= 0;
2668 struct target
*target
= get_current_target(CMD_CTX
);
2672 return ERROR_COMMAND_SYNTAX_ERROR
;
2677 LOG_ERROR("no target selected");
2681 struct duration bench
;
2682 duration_start(&bench
);
2687 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], addr
);
2688 image
.base_address
= addr
;
2689 image
.base_address_set
= 1;
2693 image
.base_address_set
= 0;
2694 image
.base_address
= 0x0;
2697 image
.start_address_set
= 0;
2699 if ((retval
= image_open(&image
, CMD_ARGV
[0], (CMD_ARGC
== 3) ? CMD_ARGV
[2] : NULL
)) != ERROR_OK
)
2707 for (i
= 0; i
< image
.num_sections
; i
++)
2709 buffer
= malloc(image
.sections
[i
].size
);
2712 command_print(CMD_CTX
,
2713 "error allocating buffer for section (%d bytes)",
2714 (int)(image
.sections
[i
].size
));
2717 if ((retval
= image_read_section(&image
, i
, 0x0, image
.sections
[i
].size
, buffer
, &buf_cnt
)) != ERROR_OK
)
2725 /* calculate checksum of image */
2726 retval
= image_calculate_checksum(buffer
, buf_cnt
, &checksum
);
2727 if (retval
!= ERROR_OK
)
2733 retval
= target_checksum_memory(target
, image
.sections
[i
].base_address
, buf_cnt
, &mem_checksum
);
2734 if (retval
!= ERROR_OK
)
2740 if (checksum
!= mem_checksum
)
2742 /* failed crc checksum, fall back to a binary compare */
2747 LOG_ERROR("checksum mismatch - attempting binary compare");
2750 data
= (uint8_t*)malloc(buf_cnt
);
2752 /* Can we use 32bit word accesses? */
2754 int count
= buf_cnt
;
2755 if ((count
% 4) == 0)
2760 retval
= target_read_memory(target
, image
.sections
[i
].base_address
, size
, count
, data
);
2761 if (retval
== ERROR_OK
)
2764 for (t
= 0; t
< buf_cnt
; t
++)
2766 if (data
[t
] != buffer
[t
])
2768 command_print(CMD_CTX
,
2769 "diff %d address 0x%08x. Was 0x%02x instead of 0x%02x",
2771 (unsigned)(t
+ image
.sections
[i
].base_address
),
2776 command_print(CMD_CTX
, "More than 128 errors, the rest are not printed.");
2789 command_print(CMD_CTX
, "address 0x%08" PRIx32
" length 0x%08zx",
2790 image
.sections
[i
].base_address
,
2795 image_size
+= buf_cnt
;
2799 command_print(CMD_CTX
, "No more differences found.");
2804 retval
= ERROR_FAIL
;
2806 if ((ERROR_OK
== retval
) && (duration_measure(&bench
) == ERROR_OK
))
2808 command_print(CMD_CTX
, "verified %" PRIu32
" bytes "
2809 "in %fs (%0.3f KiB/s)", image_size
,
2810 duration_elapsed(&bench
), duration_kbps(&bench
, image_size
));
2813 image_close(&image
);
2818 COMMAND_HANDLER(handle_verify_image_command
)
2820 return CALL_COMMAND_HANDLER(handle_verify_image_command_internal
, 1);
2823 COMMAND_HANDLER(handle_test_image_command
)
2825 return CALL_COMMAND_HANDLER(handle_verify_image_command_internal
, 0);
2828 static int handle_bp_command_list(struct command_context
*cmd_ctx
)
2830 struct target
*target
= get_current_target(cmd_ctx
);
2831 struct breakpoint
*breakpoint
= target
->breakpoints
;
2834 if (breakpoint
->type
== BKPT_SOFT
)
2836 char* buf
= buf_to_str(breakpoint
->orig_instr
,
2837 breakpoint
->length
, 16);
2838 command_print(cmd_ctx
, "0x%8.8" PRIx32
", 0x%x, %i, 0x%s",
2839 breakpoint
->address
,
2841 breakpoint
->set
, buf
);
2846 command_print(cmd_ctx
, "0x%8.8" PRIx32
", 0x%x, %i",
2847 breakpoint
->address
,
2848 breakpoint
->length
, breakpoint
->set
);
2851 breakpoint
= breakpoint
->next
;
2856 static int handle_bp_command_set(struct command_context
*cmd_ctx
,
2857 uint32_t addr
, uint32_t length
, int hw
)
2859 struct target
*target
= get_current_target(cmd_ctx
);
2860 int retval
= breakpoint_add(target
, addr
, length
, hw
);
2861 if (ERROR_OK
== retval
)
2862 command_print(cmd_ctx
, "breakpoint set at 0x%8.8" PRIx32
"", addr
);
2864 LOG_ERROR("Failure setting breakpoint");
2868 COMMAND_HANDLER(handle_bp_command
)
2871 return handle_bp_command_list(CMD_CTX
);
2873 if (CMD_ARGC
< 2 || CMD_ARGC
> 3)
2875 command_print(CMD_CTX
, "usage: bp <address> <length> ['hw']");
2876 return ERROR_COMMAND_SYNTAX_ERROR
;
2880 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], addr
);
2882 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], length
);
2887 if (strcmp(CMD_ARGV
[2], "hw") == 0)
2890 return ERROR_COMMAND_SYNTAX_ERROR
;
2893 return handle_bp_command_set(CMD_CTX
, addr
, length
, hw
);
2896 COMMAND_HANDLER(handle_rbp_command
)
2899 return ERROR_COMMAND_SYNTAX_ERROR
;
2902 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], addr
);
2904 struct target
*target
= get_current_target(CMD_CTX
);
2905 breakpoint_remove(target
, addr
);
2910 COMMAND_HANDLER(handle_wp_command
)
2912 struct target
*target
= get_current_target(CMD_CTX
);
2916 struct watchpoint
*watchpoint
= target
->watchpoints
;
2920 command_print(CMD_CTX
, "address: 0x%8.8" PRIx32
2921 ", len: 0x%8.8" PRIx32
2922 ", r/w/a: %i, value: 0x%8.8" PRIx32
2923 ", mask: 0x%8.8" PRIx32
,
2924 watchpoint
->address
,
2926 (int)watchpoint
->rw
,
2929 watchpoint
= watchpoint
->next
;
2934 enum watchpoint_rw type
= WPT_ACCESS
;
2936 uint32_t length
= 0;
2937 uint32_t data_value
= 0x0;
2938 uint32_t data_mask
= 0xffffffff;
2943 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[4], data_mask
);
2946 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[3], data_value
);
2949 switch (CMD_ARGV
[2][0])
2961 LOG_ERROR("invalid watchpoint mode ('%c')", CMD_ARGV
[2][0]);
2962 return ERROR_COMMAND_SYNTAX_ERROR
;
2966 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], length
);
2967 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], addr
);
2971 command_print(CMD_CTX
, "usage: wp [address length "
2972 "[(r|w|a) [value [mask]]]]");
2973 return ERROR_COMMAND_SYNTAX_ERROR
;
2976 int retval
= watchpoint_add(target
, addr
, length
, type
,
2977 data_value
, data_mask
);
2978 if (ERROR_OK
!= retval
)
2979 LOG_ERROR("Failure setting watchpoints");
2984 COMMAND_HANDLER(handle_rwp_command
)
2987 return ERROR_COMMAND_SYNTAX_ERROR
;
2990 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], addr
);
2992 struct target
*target
= get_current_target(CMD_CTX
);
2993 watchpoint_remove(target
, addr
);
3000 * Translate a virtual address to a physical address.
3002 * The low-level target implementation must have logged a detailed error
3003 * which is forwarded to telnet/GDB session.
3005 COMMAND_HANDLER(handle_virt2phys_command
)
3008 return ERROR_COMMAND_SYNTAX_ERROR
;
3011 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], va
);
3014 struct target
*target
= get_current_target(CMD_CTX
);
3015 int retval
= target
->type
->virt2phys(target
, va
, &pa
);
3016 if (retval
== ERROR_OK
)
3017 command_print(CMD_CTX
, "Physical address 0x%08" PRIx32
"", pa
);
3022 static void writeData(FILE *f
, const void *data
, size_t len
)
3024 size_t written
= fwrite(data
, 1, len
, f
);
3026 LOG_ERROR("failed to write %zu bytes: %s", len
, strerror(errno
));
3029 static void writeLong(FILE *f
, int l
)
3032 for (i
= 0; i
< 4; i
++)
3034 char c
= (l
>> (i
*8))&0xff;
3035 writeData(f
, &c
, 1);
3040 static void writeString(FILE *f
, char *s
)
3042 writeData(f
, s
, strlen(s
));
3045 /* Dump a gmon.out histogram file. */
3046 static void writeGmon(uint32_t *samples
, uint32_t sampleNum
, const char *filename
)
3049 FILE *f
= fopen(filename
, "w");
3052 writeString(f
, "gmon");
3053 writeLong(f
, 0x00000001); /* Version */
3054 writeLong(f
, 0); /* padding */
3055 writeLong(f
, 0); /* padding */
3056 writeLong(f
, 0); /* padding */
3058 uint8_t zero
= 0; /* GMON_TAG_TIME_HIST */
3059 writeData(f
, &zero
, 1);
3061 /* figure out bucket size */
3062 uint32_t min
= samples
[0];
3063 uint32_t max
= samples
[0];
3064 for (i
= 0; i
< sampleNum
; i
++)
3066 if (min
> samples
[i
])
3070 if (max
< samples
[i
])
3076 int addressSpace
= (max
-min
+ 1);
3078 static const uint32_t maxBuckets
= 256 * 1024; /* maximum buckets. */
3079 uint32_t length
= addressSpace
;
3080 if (length
> maxBuckets
)
3082 length
= maxBuckets
;
3084 int *buckets
= malloc(sizeof(int)*length
);
3085 if (buckets
== NULL
)
3090 memset(buckets
, 0, sizeof(int)*length
);
3091 for (i
= 0; i
< sampleNum
;i
++)
3093 uint32_t address
= samples
[i
];
3094 long long a
= address
-min
;
3095 long long b
= length
-1;
3096 long long c
= addressSpace
-1;
3097 int index_t
= (a
*b
)/c
; /* danger!!!! int32 overflows */
3101 /* append binary memory gmon.out &profile_hist_hdr ((char*)&profile_hist_hdr + sizeof(struct gmon_hist_hdr)) */
3102 writeLong(f
, min
); /* low_pc */
3103 writeLong(f
, max
); /* high_pc */
3104 writeLong(f
, length
); /* # of samples */
3105 writeLong(f
, 64000000); /* 64MHz */
3106 writeString(f
, "seconds");
3107 for (i
= 0; i
< (15-strlen("seconds")); i
++)
3108 writeData(f
, &zero
, 1);
3109 writeString(f
, "s");
3111 /*append binary memory gmon.out profile_hist_data (profile_hist_data + profile_hist_hdr.hist_size) */
3113 char *data
= malloc(2*length
);
3116 for (i
= 0; i
< length
;i
++)
3125 data
[i
*2 + 1]=(val
>> 8)&0xff;
3128 writeData(f
, data
, length
* 2);
3138 /* profiling samples the CPU PC as quickly as OpenOCD is able,
3139 * which will be used as a random sampling of PC */
3140 COMMAND_HANDLER(handle_profile_command
)
3142 struct target
*target
= get_current_target(CMD_CTX
);
3143 struct timeval timeout
, now
;
3145 gettimeofday(&timeout
, NULL
);
3148 return ERROR_COMMAND_SYNTAX_ERROR
;
3151 COMMAND_PARSE_NUMBER(uint
, CMD_ARGV
[0], offset
);
3153 timeval_add_time(&timeout
, offset
, 0);
3156 * @todo: Some cores let us sample the PC without the
3157 * annoying halt/resume step; for example, ARMv7 PCSR.
3158 * Provide a way to use that more efficient mechanism.
3161 command_print(CMD_CTX
, "Starting profiling. Halting and resuming the target as often as we can...");
3163 static const int maxSample
= 10000;
3164 uint32_t *samples
= malloc(sizeof(uint32_t)*maxSample
);
3165 if (samples
== NULL
)
3169 /* hopefully it is safe to cache! We want to stop/restart as quickly as possible. */
3170 struct reg
*reg
= register_get_by_name(target
->reg_cache
, "pc", 1);
3175 target_poll(target
);
3176 if (target
->state
== TARGET_HALTED
)
3178 uint32_t t
=*((uint32_t *)reg
->value
);
3179 samples
[numSamples
++]=t
;
3180 retval
= target_resume(target
, 1, 0, 0, 0); /* current pc, addr = 0, do not handle breakpoints, not debugging */
3181 target_poll(target
);
3182 alive_sleep(10); /* sleep 10ms, i.e. <100 samples/second. */
3183 } else if (target
->state
== TARGET_RUNNING
)
3185 /* We want to quickly sample the PC. */
3186 if ((retval
= target_halt(target
)) != ERROR_OK
)
3193 command_print(CMD_CTX
, "Target not halted or running");
3197 if (retval
!= ERROR_OK
)
3202 gettimeofday(&now
, NULL
);
3203 if ((numSamples
>= maxSample
) || ((now
.tv_sec
>= timeout
.tv_sec
) && (now
.tv_usec
>= timeout
.tv_usec
)))
3205 command_print(CMD_CTX
, "Profiling completed. %d samples.", numSamples
);
3206 if ((retval
= target_poll(target
)) != ERROR_OK
)
3211 if (target
->state
== TARGET_HALTED
)
3213 target_resume(target
, 1, 0, 0, 0); /* current pc, addr = 0, do not handle breakpoints, not debugging */
3215 if ((retval
= target_poll(target
)) != ERROR_OK
)
3220 writeGmon(samples
, numSamples
, CMD_ARGV
[1]);
3221 command_print(CMD_CTX
, "Wrote %s", CMD_ARGV
[1]);
3230 static int new_int_array_element(Jim_Interp
* interp
, const char *varname
, int idx
, uint32_t val
)
3233 Jim_Obj
*nameObjPtr
, *valObjPtr
;
3236 namebuf
= alloc_printf("%s(%d)", varname
, idx
);
3240 nameObjPtr
= Jim_NewStringObj(interp
, namebuf
, -1);
3241 valObjPtr
= Jim_NewIntObj(interp
, val
);
3242 if (!nameObjPtr
|| !valObjPtr
)
3248 Jim_IncrRefCount(nameObjPtr
);
3249 Jim_IncrRefCount(valObjPtr
);
3250 result
= Jim_SetVariable(interp
, nameObjPtr
, valObjPtr
);
3251 Jim_DecrRefCount(interp
, nameObjPtr
);
3252 Jim_DecrRefCount(interp
, valObjPtr
);
3254 /* printf("%s(%d) <= 0%08x\n", varname, idx, val); */
3258 static int jim_mem2array(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
3260 struct command_context
*context
;
3261 struct target
*target
;
3263 context
= current_command_context(interp
);
3264 assert (context
!= NULL
);
3266 target
= get_current_target(context
);
3269 LOG_ERROR("mem2array: no current target");
3273 return target_mem2array(interp
, target
, argc
-1, argv
+ 1);
3276 static int target_mem2array(Jim_Interp
*interp
, struct target
*target
, int argc
, Jim_Obj
*const *argv
)
3284 const char *varname
;
3288 /* argv[1] = name of array to receive the data
3289 * argv[2] = desired width
3290 * argv[3] = memory address
3291 * argv[4] = count of times to read
3294 Jim_WrongNumArgs(interp
, 1, argv
, "varname width addr nelems");
3297 varname
= Jim_GetString(argv
[0], &len
);
3298 /* given "foo" get space for worse case "foo(%d)" .. add 20 */
3300 e
= Jim_GetLong(interp
, argv
[1], &l
);
3306 e
= Jim_GetLong(interp
, argv
[2], &l
);
3311 e
= Jim_GetLong(interp
, argv
[3], &l
);
3327 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3328 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "Invalid width param, must be 8/16/32", NULL
);
3332 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3333 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "mem2array: zero width read?", NULL
);
3336 if ((addr
+ (len
* width
)) < addr
) {
3337 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3338 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "mem2array: addr + len - wraps to zero?", NULL
);
3341 /* absurd transfer size? */
3343 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3344 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "mem2array: absurd > 64K item request", NULL
);
3349 ((width
== 2) && ((addr
& 1) == 0)) ||
3350 ((width
== 4) && ((addr
& 3) == 0))) {
3354 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3355 sprintf(buf
, "mem2array address: 0x%08" PRIx32
" is not aligned for %" PRId32
" byte reads",
3358 Jim_AppendStrings(interp
, Jim_GetResult(interp
), buf
, NULL
);
3367 size_t buffersize
= 4096;
3368 uint8_t *buffer
= malloc(buffersize
);
3375 /* Slurp... in buffer size chunks */
3377 count
= len
; /* in objects.. */
3378 if (count
> (buffersize
/width
)) {
3379 count
= (buffersize
/width
);
3382 retval
= target_read_memory(target
, addr
, width
, count
, buffer
);
3383 if (retval
!= ERROR_OK
) {
3385 LOG_ERROR("mem2array: Read @ 0x%08x, w=%d, cnt=%d, failed",
3389 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3390 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "mem2array: cannot read memory", NULL
);
3394 v
= 0; /* shut up gcc */
3395 for (i
= 0 ;i
< count
;i
++, n
++) {
3398 v
= target_buffer_get_u32(target
, &buffer
[i
*width
]);
3401 v
= target_buffer_get_u16(target
, &buffer
[i
*width
]);
3404 v
= buffer
[i
] & 0x0ff;
3407 new_int_array_element(interp
, varname
, n
, v
);
3415 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3420 static int get_int_array_element(Jim_Interp
* interp
, const char *varname
, int idx
, uint32_t *val
)
3423 Jim_Obj
*nameObjPtr
, *valObjPtr
;
3427 namebuf
= alloc_printf("%s(%d)", varname
, idx
);
3431 nameObjPtr
= Jim_NewStringObj(interp
, namebuf
, -1);
3438 Jim_IncrRefCount(nameObjPtr
);
3439 valObjPtr
= Jim_GetVariable(interp
, nameObjPtr
, JIM_ERRMSG
);
3440 Jim_DecrRefCount(interp
, nameObjPtr
);
3442 if (valObjPtr
== NULL
)
3445 result
= Jim_GetLong(interp
, valObjPtr
, &l
);
3446 /* printf("%s(%d) => 0%08x\n", varname, idx, val); */
3451 static int jim_array2mem(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
3453 struct command_context
*context
;
3454 struct target
*target
;
3456 context
= current_command_context(interp
);
3457 assert (context
!= NULL
);
3459 target
= get_current_target(context
);
3460 if (target
== NULL
) {
3461 LOG_ERROR("array2mem: no current target");
3465 return target_array2mem(interp
,target
, argc
-1, argv
+ 1);
3468 static int target_array2mem(Jim_Interp
*interp
, struct target
*target
,
3469 int argc
, Jim_Obj
*const *argv
)
3477 const char *varname
;
3481 /* argv[1] = name of array to get the data
3482 * argv[2] = desired width
3483 * argv[3] = memory address
3484 * argv[4] = count to write
3487 Jim_WrongNumArgs(interp
, 0, argv
, "varname width addr nelems");
3490 varname
= Jim_GetString(argv
[0], &len
);
3491 /* given "foo" get space for worse case "foo(%d)" .. add 20 */
3493 e
= Jim_GetLong(interp
, argv
[1], &l
);
3499 e
= Jim_GetLong(interp
, argv
[2], &l
);
3504 e
= Jim_GetLong(interp
, argv
[3], &l
);
3520 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3521 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "Invalid width param, must be 8/16/32", NULL
);
3525 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3526 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "array2mem: zero width read?", NULL
);
3529 if ((addr
+ (len
* width
)) < addr
) {
3530 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3531 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "array2mem: addr + len - wraps to zero?", NULL
);
3534 /* absurd transfer size? */
3536 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3537 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "array2mem: absurd > 64K item request", NULL
);
3542 ((width
== 2) && ((addr
& 1) == 0)) ||
3543 ((width
== 4) && ((addr
& 3) == 0))) {
3547 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3548 sprintf(buf
, "array2mem address: 0x%08x is not aligned for %d byte reads",
3551 Jim_AppendStrings(interp
, Jim_GetResult(interp
), buf
, NULL
);
3562 size_t buffersize
= 4096;
3563 uint8_t *buffer
= malloc(buffersize
);
3568 /* Slurp... in buffer size chunks */
3570 count
= len
; /* in objects.. */
3571 if (count
> (buffersize
/width
)) {
3572 count
= (buffersize
/width
);
3575 v
= 0; /* shut up gcc */
3576 for (i
= 0 ;i
< count
;i
++, n
++) {
3577 get_int_array_element(interp
, varname
, n
, &v
);
3580 target_buffer_set_u32(target
, &buffer
[i
*width
], v
);
3583 target_buffer_set_u16(target
, &buffer
[i
*width
], v
);
3586 buffer
[i
] = v
& 0x0ff;
3592 retval
= target_write_memory(target
, addr
, width
, count
, buffer
);
3593 if (retval
!= ERROR_OK
) {
3595 LOG_ERROR("array2mem: Write @ 0x%08x, w=%d, cnt=%d, failed",
3599 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3600 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "array2mem: cannot read memory", NULL
);
3608 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3613 /* FIX? should we propagate errors here rather than printing them
3616 void target_handle_event(struct target
*target
, enum target_event e
)
3618 struct target_event_action
*teap
;
3620 for (teap
= target
->event_action
; teap
!= NULL
; teap
= teap
->next
) {
3621 if (teap
->event
== e
) {
3622 LOG_DEBUG("target: (%d) %s (%s) event: %d (%s) action: %s",
3623 target
->target_number
,
3624 target_name(target
),
3625 target_type_name(target
),
3627 Jim_Nvp_value2name_simple(nvp_target_event
, e
)->name
,
3628 Jim_GetString(teap
->body
, NULL
));
3629 if (Jim_EvalObj(teap
->interp
, teap
->body
) != JIM_OK
)
3631 Jim_MakeErrorMessage(teap
->interp
);
3632 command_print(NULL
,"%s\n", Jim_GetString(Jim_GetResult(teap
->interp
), NULL
));
3639 * Returns true only if the target has a handler for the specified event.
3641 bool target_has_event_action(struct target
*target
, enum target_event event
)
3643 struct target_event_action
*teap
;
3645 for (teap
= target
->event_action
; teap
!= NULL
; teap
= teap
->next
) {
3646 if (teap
->event
== event
)
3652 enum target_cfg_param
{
3655 TCFG_WORK_AREA_VIRT
,
3656 TCFG_WORK_AREA_PHYS
,
3657 TCFG_WORK_AREA_SIZE
,
3658 TCFG_WORK_AREA_BACKUP
,
3661 TCFG_CHAIN_POSITION
,
3664 static Jim_Nvp nvp_config_opts
[] = {
3665 { .name
= "-type", .value
= TCFG_TYPE
},
3666 { .name
= "-event", .value
= TCFG_EVENT
},
3667 { .name
= "-work-area-virt", .value
= TCFG_WORK_AREA_VIRT
},
3668 { .name
= "-work-area-phys", .value
= TCFG_WORK_AREA_PHYS
},
3669 { .name
= "-work-area-size", .value
= TCFG_WORK_AREA_SIZE
},
3670 { .name
= "-work-area-backup", .value
= TCFG_WORK_AREA_BACKUP
},
3671 { .name
= "-endian" , .value
= TCFG_ENDIAN
},
3672 { .name
= "-variant", .value
= TCFG_VARIANT
},
3673 { .name
= "-chain-position", .value
= TCFG_CHAIN_POSITION
},
3675 { .name
= NULL
, .value
= -1 }
3678 static int target_configure(Jim_GetOptInfo
*goi
, struct target
*target
)
3686 /* parse config or cget options ... */
3687 while (goi
->argc
> 0) {
3688 Jim_SetEmptyResult(goi
->interp
);
3689 /* Jim_GetOpt_Debug(goi); */
3691 if (target
->type
->target_jim_configure
) {
3692 /* target defines a configure function */
3693 /* target gets first dibs on parameters */
3694 e
= (*(target
->type
->target_jim_configure
))(target
, goi
);
3703 /* otherwise we 'continue' below */
3705 e
= Jim_GetOpt_Nvp(goi
, nvp_config_opts
, &n
);
3707 Jim_GetOpt_NvpUnknown(goi
, nvp_config_opts
, 0);
3713 if (goi
->isconfigure
) {