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
);
55 extern struct target_type arm7tdmi_target
;
56 extern struct target_type arm720t_target
;
57 extern struct target_type arm9tdmi_target
;
58 extern struct target_type arm920t_target
;
59 extern struct target_type arm966e_target
;
60 extern struct target_type arm926ejs_target
;
61 extern struct target_type fa526_target
;
62 extern struct target_type feroceon_target
;
63 extern struct target_type dragonite_target
;
64 extern struct target_type xscale_target
;
65 extern struct target_type cortexm3_target
;
66 extern struct target_type cortexa8_target
;
67 extern struct target_type arm11_target
;
68 extern struct target_type mips_m4k_target
;
69 extern struct target_type avr_target
;
70 extern struct target_type dsp563xx_target
;
71 extern struct target_type testee_target
;
73 struct target_type
*target_types
[] =
95 struct target
*all_targets
= NULL
;
96 struct target_event_callback
*target_event_callbacks
= NULL
;
97 struct target_timer_callback
*target_timer_callbacks
= NULL
;
99 static const Jim_Nvp nvp_assert
[] = {
100 { .name
= "assert", NVP_ASSERT
},
101 { .name
= "deassert", NVP_DEASSERT
},
102 { .name
= "T", NVP_ASSERT
},
103 { .name
= "F", NVP_DEASSERT
},
104 { .name
= "t", NVP_ASSERT
},
105 { .name
= "f", NVP_DEASSERT
},
106 { .name
= NULL
, .value
= -1 }
109 static const Jim_Nvp nvp_error_target
[] = {
110 { .value
= ERROR_TARGET_INVALID
, .name
= "err-invalid" },
111 { .value
= ERROR_TARGET_INIT_FAILED
, .name
= "err-init-failed" },
112 { .value
= ERROR_TARGET_TIMEOUT
, .name
= "err-timeout" },
113 { .value
= ERROR_TARGET_NOT_HALTED
, .name
= "err-not-halted" },
114 { .value
= ERROR_TARGET_FAILURE
, .name
= "err-failure" },
115 { .value
= ERROR_TARGET_UNALIGNED_ACCESS
, .name
= "err-unaligned-access" },
116 { .value
= ERROR_TARGET_DATA_ABORT
, .name
= "err-data-abort" },
117 { .value
= ERROR_TARGET_RESOURCE_NOT_AVAILABLE
, .name
= "err-resource-not-available" },
118 { .value
= ERROR_TARGET_TRANSLATION_FAULT
, .name
= "err-translation-fault" },
119 { .value
= ERROR_TARGET_NOT_RUNNING
, .name
= "err-not-running" },
120 { .value
= ERROR_TARGET_NOT_EXAMINED
, .name
= "err-not-examined" },
121 { .value
= -1, .name
= NULL
}
124 const char *target_strerror_safe(int err
)
128 n
= Jim_Nvp_value2name_simple(nvp_error_target
, err
);
129 if (n
->name
== NULL
) {
136 static const Jim_Nvp nvp_target_event
[] = {
137 { .value
= TARGET_EVENT_OLD_gdb_program_config
, .name
= "old-gdb_program_config" },
138 { .value
= TARGET_EVENT_OLD_pre_resume
, .name
= "old-pre_resume" },
140 { .value
= TARGET_EVENT_GDB_HALT
, .name
= "gdb-halt" },
141 { .value
= TARGET_EVENT_HALTED
, .name
= "halted" },
142 { .value
= TARGET_EVENT_RESUMED
, .name
= "resumed" },
143 { .value
= TARGET_EVENT_RESUME_START
, .name
= "resume-start" },
144 { .value
= TARGET_EVENT_RESUME_END
, .name
= "resume-end" },
146 { .name
= "gdb-start", .value
= TARGET_EVENT_GDB_START
},
147 { .name
= "gdb-end", .value
= TARGET_EVENT_GDB_END
},
149 /* historical name */
151 { .value
= TARGET_EVENT_RESET_START
, .name
= "reset-start" },
153 { .value
= TARGET_EVENT_RESET_ASSERT_PRE
, .name
= "reset-assert-pre" },
154 { .value
= TARGET_EVENT_RESET_ASSERT
, .name
= "reset-assert" },
155 { .value
= TARGET_EVENT_RESET_ASSERT_POST
, .name
= "reset-assert-post" },
156 { .value
= TARGET_EVENT_RESET_DEASSERT_PRE
, .name
= "reset-deassert-pre" },
157 { .value
= TARGET_EVENT_RESET_DEASSERT_POST
, .name
= "reset-deassert-post" },
158 { .value
= TARGET_EVENT_RESET_HALT_PRE
, .name
= "reset-halt-pre" },
159 { .value
= TARGET_EVENT_RESET_HALT_POST
, .name
= "reset-halt-post" },
160 { .value
= TARGET_EVENT_RESET_WAIT_PRE
, .name
= "reset-wait-pre" },
161 { .value
= TARGET_EVENT_RESET_WAIT_POST
, .name
= "reset-wait-post" },
162 { .value
= TARGET_EVENT_RESET_INIT
, .name
= "reset-init" },
163 { .value
= TARGET_EVENT_RESET_END
, .name
= "reset-end" },
165 { .value
= TARGET_EVENT_EXAMINE_START
, .name
= "examine-start" },
166 { .value
= TARGET_EVENT_EXAMINE_END
, .name
= "examine-end" },
168 { .value
= TARGET_EVENT_DEBUG_HALTED
, .name
= "debug-halted" },
169 { .value
= TARGET_EVENT_DEBUG_RESUMED
, .name
= "debug-resumed" },
171 { .value
= TARGET_EVENT_GDB_ATTACH
, .name
= "gdb-attach" },
172 { .value
= TARGET_EVENT_GDB_DETACH
, .name
= "gdb-detach" },
174 { .value
= TARGET_EVENT_GDB_FLASH_WRITE_START
, .name
= "gdb-flash-write-start" },
175 { .value
= TARGET_EVENT_GDB_FLASH_WRITE_END
, .name
= "gdb-flash-write-end" },
177 { .value
= TARGET_EVENT_GDB_FLASH_ERASE_START
, .name
= "gdb-flash-erase-start" },
178 { .value
= TARGET_EVENT_GDB_FLASH_ERASE_END
, .name
= "gdb-flash-erase-end" },
180 { .value
= TARGET_EVENT_RESUME_START
, .name
= "resume-start" },
181 { .value
= TARGET_EVENT_RESUMED
, .name
= "resume-ok" },
182 { .value
= TARGET_EVENT_RESUME_END
, .name
= "resume-end" },
184 { .name
= NULL
, .value
= -1 }
187 static const Jim_Nvp nvp_target_state
[] = {
188 { .name
= "unknown", .value
= TARGET_UNKNOWN
},
189 { .name
= "running", .value
= TARGET_RUNNING
},
190 { .name
= "halted", .value
= TARGET_HALTED
},
191 { .name
= "reset", .value
= TARGET_RESET
},
192 { .name
= "debug-running", .value
= TARGET_DEBUG_RUNNING
},
193 { .name
= NULL
, .value
= -1 },
196 static const Jim_Nvp nvp_target_debug_reason
[] = {
197 { .name
= "debug-request" , .value
= DBG_REASON_DBGRQ
},
198 { .name
= "breakpoint" , .value
= DBG_REASON_BREAKPOINT
},
199 { .name
= "watchpoint" , .value
= DBG_REASON_WATCHPOINT
},
200 { .name
= "watchpoint-and-breakpoint", .value
= DBG_REASON_WPTANDBKPT
},
201 { .name
= "single-step" , .value
= DBG_REASON_SINGLESTEP
},
202 { .name
= "target-not-halted" , .value
= DBG_REASON_NOTHALTED
},
203 { .name
= "undefined" , .value
= DBG_REASON_UNDEFINED
},
204 { .name
= NULL
, .value
= -1 },
207 static const Jim_Nvp nvp_target_endian
[] = {
208 { .name
= "big", .value
= TARGET_BIG_ENDIAN
},
209 { .name
= "little", .value
= TARGET_LITTLE_ENDIAN
},
210 { .name
= "be", .value
= TARGET_BIG_ENDIAN
},
211 { .name
= "le", .value
= TARGET_LITTLE_ENDIAN
},
212 { .name
= NULL
, .value
= -1 },
215 static const Jim_Nvp nvp_reset_modes
[] = {
216 { .name
= "unknown", .value
= RESET_UNKNOWN
},
217 { .name
= "run" , .value
= RESET_RUN
},
218 { .name
= "halt" , .value
= RESET_HALT
},
219 { .name
= "init" , .value
= RESET_INIT
},
220 { .name
= NULL
, .value
= -1 },
223 const char *debug_reason_name(struct target
*t
)
227 cp
= Jim_Nvp_value2name_simple(nvp_target_debug_reason
,
228 t
->debug_reason
)->name
;
230 LOG_ERROR("Invalid debug reason: %d", (int)(t
->debug_reason
));
231 cp
= "(*BUG*unknown*BUG*)";
237 target_state_name( struct target
*t
)
240 cp
= Jim_Nvp_value2name_simple(nvp_target_state
, t
->state
)->name
;
242 LOG_ERROR("Invalid target state: %d", (int)(t
->state
));
243 cp
= "(*BUG*unknown*BUG*)";
248 /* determine the number of the new target */
249 static int new_target_number(void)
254 /* number is 0 based */
258 if (x
< t
->target_number
) {
259 x
= t
->target_number
;
266 /* read a uint32_t from a buffer in target memory endianness */
267 uint32_t target_buffer_get_u32(struct target
*target
, const uint8_t *buffer
)
269 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
270 return le_to_h_u32(buffer
);
272 return be_to_h_u32(buffer
);
275 /* read a uint16_t from a buffer in target memory endianness */
276 uint16_t target_buffer_get_u16(struct target
*target
, const uint8_t *buffer
)
278 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
279 return le_to_h_u16(buffer
);
281 return be_to_h_u16(buffer
);
284 /* read a uint8_t from a buffer in target memory endianness */
285 uint8_t target_buffer_get_u8(struct target
*target
, const uint8_t *buffer
)
287 return *buffer
& 0x0ff;
290 /* write a uint32_t to a buffer in target memory endianness */
291 void target_buffer_set_u32(struct target
*target
, uint8_t *buffer
, uint32_t value
)
293 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
294 h_u32_to_le(buffer
, value
);
296 h_u32_to_be(buffer
, value
);
299 /* write a uint16_t to a buffer in target memory endianness */
300 void target_buffer_set_u16(struct target
*target
, uint8_t *buffer
, uint16_t value
)
302 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
303 h_u16_to_le(buffer
, value
);
305 h_u16_to_be(buffer
, value
);
308 /* write a uint8_t to a buffer in target memory endianness */
309 void target_buffer_set_u8(struct target
*target
, uint8_t *buffer
, uint8_t value
)
314 /* return a pointer to a configured target; id is name or number */
315 struct target
*get_target(const char *id
)
317 struct target
*target
;
319 /* try as tcltarget name */
320 for (target
= all_targets
; target
; target
= target
->next
) {
321 if (target
->cmd_name
== NULL
)
323 if (strcmp(id
, target
->cmd_name
) == 0)
327 /* It's OK to remove this fallback sometime after August 2010 or so */
329 /* no match, try as number */
331 if (parse_uint(id
, &num
) != ERROR_OK
)
334 for (target
= all_targets
; target
; target
= target
->next
) {
335 if (target
->target_number
== (int)num
) {
336 LOG_WARNING("use '%s' as target identifier, not '%u'",
337 target
->cmd_name
, num
);
345 /* returns a pointer to the n-th configured target */
346 static struct target
*get_target_by_num(int num
)
348 struct target
*target
= all_targets
;
351 if (target
->target_number
== num
) {
354 target
= target
->next
;
360 struct target
* get_current_target(struct command_context
*cmd_ctx
)
362 struct target
*target
= get_target_by_num(cmd_ctx
->current_target
);
366 LOG_ERROR("BUG: current_target out of bounds");
373 int target_poll(struct target
*target
)
377 /* We can't poll until after examine */
378 if (!target_was_examined(target
))
380 /* Fail silently lest we pollute the log */
384 retval
= target
->type
->poll(target
);
385 if (retval
!= ERROR_OK
)
388 if (target
->halt_issued
)
390 if (target
->state
== TARGET_HALTED
)
392 target
->halt_issued
= false;
395 long long t
= timeval_ms() - target
->halt_issued_time
;
398 target
->halt_issued
= false;
399 LOG_INFO("Halt timed out, wake up GDB.");
400 target_call_event_callbacks(target
, TARGET_EVENT_GDB_HALT
);
408 int target_halt(struct target
*target
)
411 /* We can't poll until after examine */
412 if (!target_was_examined(target
))
414 LOG_ERROR("Target not examined yet");
418 retval
= target
->type
->halt(target
);
419 if (retval
!= ERROR_OK
)
422 target
->halt_issued
= true;
423 target
->halt_issued_time
= timeval_ms();
429 * Make the target (re)start executing using its saved execution
430 * context (possibly with some modifications).
432 * @param target Which target should start executing.
433 * @param current True to use the target's saved program counter instead
434 * of the address parameter
435 * @param address Optionally used as the program counter.
436 * @param handle_breakpoints True iff breakpoints at the resumption PC
437 * should be skipped. (For example, maybe execution was stopped by
438 * such a breakpoint, in which case it would be counterprodutive to
440 * @param debug_execution False if all working areas allocated by OpenOCD
441 * should be released and/or restored to their original contents.
442 * (This would for example be true to run some downloaded "helper"
443 * algorithm code, which resides in one such working buffer and uses
444 * another for data storage.)
446 * @todo Resolve the ambiguity about what the "debug_execution" flag
447 * signifies. For example, Target implementations don't agree on how
448 * it relates to invalidation of the register cache, or to whether
449 * breakpoints and watchpoints should be enabled. (It would seem wrong
450 * to enable breakpoints when running downloaded "helper" algorithms
451 * (debug_execution true), since the breakpoints would be set to match
452 * target firmware being debugged, not the helper algorithm.... and
453 * enabling them could cause such helpers to malfunction (for example,
454 * by overwriting data with a breakpoint instruction. On the other
455 * hand the infrastructure for running such helpers might use this
456 * procedure but rely on hardware breakpoint to detect termination.)
458 int target_resume(struct target
*target
, int current
, uint32_t address
, int handle_breakpoints
, int debug_execution
)
462 /* We can't poll until after examine */
463 if (!target_was_examined(target
))
465 LOG_ERROR("Target not examined yet");
469 /* note that resume *must* be asynchronous. The CPU can halt before
470 * we poll. The CPU can even halt at the current PC as a result of
471 * a software breakpoint being inserted by (a bug?) the application.
473 if ((retval
= target
->type
->resume(target
, current
, address
, handle_breakpoints
, debug_execution
)) != ERROR_OK
)
476 /* Invalidate any cached protect/erase/... flash status, since
477 * almost all targets will now be able modify the flash by
478 * themselves. We want flash drivers and infrastructure to
479 * be able to rely on (non-invalidated) cached state.
481 * For now we require that algorithms provided by OpenOCD are
482 * used only by code which properly maintains that cached state.
485 * REVISIT do the same for NAND ; maybe other flash flavors too...
487 if (!target
->running_alg
)
492 int target_process_reset(struct command_context
*cmd_ctx
, enum target_reset_mode reset_mode
)
497 n
= Jim_Nvp_value2name_simple(nvp_reset_modes
, reset_mode
);
498 if (n
->name
== NULL
) {
499 LOG_ERROR("invalid reset mode");
503 /* disable polling during reset to make reset event scripts
504 * more predictable, i.e. dr/irscan & pathmove in events will
505 * not have JTAG operations injected into the middle of a sequence.
507 bool save_poll
= jtag_poll_get_enabled();
509 jtag_poll_set_enabled(false);
511 sprintf(buf
, "ocd_process_reset %s", n
->name
);
512 retval
= Jim_Eval(cmd_ctx
->interp
, buf
);
514 jtag_poll_set_enabled(save_poll
);
516 if (retval
!= JIM_OK
) {
517 Jim_PrintErrorMessage(cmd_ctx
->interp
);
521 /* We want any events to be processed before the prompt */
522 retval
= target_call_timer_callbacks_now();
524 struct target
*target
;
525 for (target
= all_targets
; target
; target
= target
->next
) {
526 target
->type
->check_reset(target
);
532 static int identity_virt2phys(struct target
*target
,
533 uint32_t virtual, uint32_t *physical
)
539 static int no_mmu(struct target
*target
, int *enabled
)
545 static int default_examine(struct target
*target
)
547 target_set_examined(target
);
551 /* no check by default */
552 static int default_check_reset(struct target
*target
)
557 int target_examine_one(struct target
*target
)
559 return target
->type
->examine(target
);
562 static int jtag_enable_callback(enum jtag_event event
, void *priv
)
564 struct target
*target
= priv
;
566 if (event
!= JTAG_TAP_EVENT_ENABLE
|| !target
->tap
->enabled
)
569 jtag_unregister_event_callback(jtag_enable_callback
, target
);
570 return target_examine_one(target
);
574 /* Targets that correctly implement init + examine, i.e.
575 * no communication with target during init:
579 int target_examine(void)
581 int retval
= ERROR_OK
;
582 struct target
*target
;
584 for (target
= all_targets
; target
; target
= target
->next
)
586 /* defer examination, but don't skip it */
587 if (!target
->tap
->enabled
) {
588 jtag_register_event_callback(jtag_enable_callback
,
592 if ((retval
= target_examine_one(target
)) != ERROR_OK
)
597 const char *target_type_name(struct target
*target
)
599 return target
->type
->name
;
602 static int target_write_memory_imp(struct target
*target
, uint32_t address
, uint32_t size
, uint32_t count
, uint8_t *buffer
)
604 if (!target_was_examined(target
))
606 LOG_ERROR("Target not examined yet");
609 return target
->type
->write_memory_imp(target
, address
, size
, count
, buffer
);
612 static int target_read_memory_imp(struct target
*target
, uint32_t address
, uint32_t size
, uint32_t count
, uint8_t *buffer
)
614 if (!target_was_examined(target
))
616 LOG_ERROR("Target not examined yet");
619 return target
->type
->read_memory_imp(target
, address
, size
, count
, buffer
);
622 static int target_soft_reset_halt_imp(struct target
*target
)
624 if (!target_was_examined(target
))
626 LOG_ERROR("Target not examined yet");
629 if (!target
->type
->soft_reset_halt_imp
) {
630 LOG_ERROR("Target %s does not support soft_reset_halt",
631 target_name(target
));
634 return target
->type
->soft_reset_halt_imp(target
);
638 * Downloads a target-specific native code algorithm to the target,
639 * and executes it. * Note that some targets may need to set up, enable,
640 * and tear down a breakpoint (hard or * soft) to detect algorithm
641 * termination, while others may support lower overhead schemes where
642 * soft breakpoints embedded in the algorithm automatically terminate the
645 * @param target used to run the algorithm
646 * @param arch_info target-specific description of the algorithm.
648 int target_run_algorithm(struct target
*target
,
649 int num_mem_params
, struct mem_param
*mem_params
,
650 int num_reg_params
, struct reg_param
*reg_param
,
651 uint32_t entry_point
, uint32_t exit_point
,
652 int timeout_ms
, void *arch_info
)
654 int retval
= ERROR_FAIL
;
656 if (!target_was_examined(target
))
658 LOG_ERROR("Target not examined yet");
661 if (target
->type
->run_algorithm
) {
662 LOG_ERROR("Target type '%s' does not support %s",
663 target_type_name(target
), __func__
);
667 target
->running_alg
= true;
668 retval
= target
->type
->run_algorithm(target
,
669 num_mem_params
, mem_params
,
670 num_reg_params
, reg_param
,
671 entry_point
, exit_point
, timeout_ms
, arch_info
);
672 target
->running_alg
= false;
679 int target_read_memory(struct target
*target
,
680 uint32_t address
, uint32_t size
, uint32_t count
, uint8_t *buffer
)
682 return target
->type
->read_memory(target
, address
, size
, count
, buffer
);
685 int target_read_phys_memory(struct target
*target
,
686 uint32_t address
, uint32_t size
, uint32_t count
, uint8_t *buffer
)
688 return target
->type
->read_phys_memory(target
, address
, size
, count
, buffer
);
691 int target_write_memory(struct target
*target
,
692 uint32_t address
, uint32_t size
, uint32_t count
, uint8_t *buffer
)
694 return target
->type
->write_memory(target
, address
, size
, count
, buffer
);
697 int target_write_phys_memory(struct target
*target
,
698 uint32_t address
, uint32_t size
, uint32_t count
, uint8_t *buffer
)
700 return target
->type
->write_phys_memory(target
, address
, size
, count
, buffer
);
703 int target_bulk_write_memory(struct target
*target
,
704 uint32_t address
, uint32_t count
, uint8_t *buffer
)
706 return target
->type
->bulk_write_memory(target
, address
, count
, buffer
);
709 int target_add_breakpoint(struct target
*target
,
710 struct breakpoint
*breakpoint
)
712 if (target
->state
!= TARGET_HALTED
) {
713 LOG_WARNING("target %s is not halted", target
->cmd_name
);
714 return ERROR_TARGET_NOT_HALTED
;
716 return target
->type
->add_breakpoint(target
, breakpoint
);
718 int target_remove_breakpoint(struct target
*target
,
719 struct breakpoint
*breakpoint
)
721 return target
->type
->remove_breakpoint(target
, breakpoint
);
724 int target_add_watchpoint(struct target
*target
,
725 struct watchpoint
*watchpoint
)
727 if (target
->state
!= TARGET_HALTED
) {
728 LOG_WARNING("target %s is not halted", target
->cmd_name
);
729 return ERROR_TARGET_NOT_HALTED
;
731 return target
->type
->add_watchpoint(target
, watchpoint
);
733 int target_remove_watchpoint(struct target
*target
,
734 struct watchpoint
*watchpoint
)
736 return target
->type
->remove_watchpoint(target
, watchpoint
);
739 int target_get_gdb_reg_list(struct target
*target
,
740 struct reg
**reg_list
[], int *reg_list_size
)
742 return target
->type
->get_gdb_reg_list(target
, reg_list
, reg_list_size
);
744 int target_step(struct target
*target
,
745 int current
, uint32_t address
, int handle_breakpoints
)
747 return target
->type
->step(target
, current
, address
, handle_breakpoints
);
752 * Reset the @c examined flag for the given target.
753 * Pure paranoia -- targets are zeroed on allocation.
755 static void target_reset_examined(struct target
*target
)
757 target
->examined
= false;
761 err_read_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__
);
769 err_write_phys_memory(struct target
*target
, uint32_t address
,
770 uint32_t size
, uint32_t count
, uint8_t *buffer
)
772 LOG_ERROR("Not implemented: %s", __func__
);
776 static int handle_target(void *priv
);
778 static int target_init_one(struct command_context
*cmd_ctx
,
779 struct target
*target
)
781 target_reset_examined(target
);
783 struct target_type
*type
= target
->type
;
784 if (type
->examine
== NULL
)
785 type
->examine
= default_examine
;
787 if (type
->check_reset
== NULL
)
788 type
->check_reset
= default_check_reset
;
790 int retval
= type
->init_target(cmd_ctx
, target
);
791 if (ERROR_OK
!= retval
)
793 LOG_ERROR("target '%s' init failed", target_name(target
));
798 * @todo get rid of those *memory_imp() methods, now that all
799 * callers are using target_*_memory() accessors ... and make
800 * sure the "physical" paths handle the same issues.
802 /* a non-invasive way(in terms of patches) to add some code that
803 * runs before the type->write/read_memory implementation
805 type
->write_memory_imp
= target
->type
->write_memory
;
806 type
->write_memory
= target_write_memory_imp
;
808 type
->read_memory_imp
= target
->type
->read_memory
;
809 type
->read_memory
= target_read_memory_imp
;
811 type
->soft_reset_halt_imp
= target
->type
->soft_reset_halt
;
812 type
->soft_reset_halt
= target_soft_reset_halt_imp
;
814 /* Sanity-check MMU support ... stub in what we must, to help
815 * implement it in stages, but warn if we need to do so.
819 if (type
->write_phys_memory
== NULL
)
821 LOG_ERROR("type '%s' is missing write_phys_memory",
823 type
->write_phys_memory
= err_write_phys_memory
;
825 if (type
->read_phys_memory
== NULL
)
827 LOG_ERROR("type '%s' is missing read_phys_memory",
829 type
->read_phys_memory
= err_read_phys_memory
;
831 if (type
->virt2phys
== NULL
)
833 LOG_ERROR("type '%s' is missing virt2phys", type
->name
);
834 type
->virt2phys
= identity_virt2phys
;
839 /* Make sure no-MMU targets all behave the same: make no
840 * distinction between physical and virtual addresses, and
841 * ensure that virt2phys() is always an identity mapping.
843 if (type
->write_phys_memory
|| type
->read_phys_memory
846 LOG_WARNING("type '%s' has bad MMU hooks", type
->name
);
850 type
->write_phys_memory
= type
->write_memory
;
851 type
->read_phys_memory
= type
->read_memory
;
852 type
->virt2phys
= identity_virt2phys
;
857 int target_init(struct command_context
*cmd_ctx
)
859 struct target
*target
;
862 for (target
= all_targets
; target
; target
= target
->next
)
864 retval
= target_init_one(cmd_ctx
, target
);
865 if (ERROR_OK
!= retval
)
872 retval
= target_register_user_commands(cmd_ctx
);
873 if (ERROR_OK
!= retval
)
876 retval
= target_register_timer_callback(&handle_target
,
877 100, 1, cmd_ctx
->interp
);
878 if (ERROR_OK
!= retval
)
884 COMMAND_HANDLER(handle_target_init_command
)
887 return ERROR_COMMAND_SYNTAX_ERROR
;
889 static bool target_initialized
= false;
890 if (target_initialized
)
892 LOG_INFO("'target init' has already been called");
895 target_initialized
= true;
897 LOG_DEBUG("Initializing targets...");
898 return target_init(CMD_CTX
);
901 int target_register_event_callback(int (*callback
)(struct target
*target
, enum target_event event
, void *priv
), void *priv
)
903 struct target_event_callback
**callbacks_p
= &target_event_callbacks
;
905 if (callback
== NULL
)
907 return ERROR_INVALID_ARGUMENTS
;
912 while ((*callbacks_p
)->next
)
913 callbacks_p
= &((*callbacks_p
)->next
);
914 callbacks_p
= &((*callbacks_p
)->next
);
917 (*callbacks_p
) = malloc(sizeof(struct target_event_callback
));
918 (*callbacks_p
)->callback
= callback
;
919 (*callbacks_p
)->priv
= priv
;
920 (*callbacks_p
)->next
= NULL
;
925 int target_register_timer_callback(int (*callback
)(void *priv
), int time_ms
, int periodic
, void *priv
)
927 struct target_timer_callback
**callbacks_p
= &target_timer_callbacks
;
930 if (callback
== NULL
)
932 return ERROR_INVALID_ARGUMENTS
;
937 while ((*callbacks_p
)->next
)
938 callbacks_p
= &((*callbacks_p
)->next
);
939 callbacks_p
= &((*callbacks_p
)->next
);
942 (*callbacks_p
) = malloc(sizeof(struct target_timer_callback
));
943 (*callbacks_p
)->callback
= callback
;
944 (*callbacks_p
)->periodic
= periodic
;
945 (*callbacks_p
)->time_ms
= time_ms
;
947 gettimeofday(&now
, NULL
);
948 (*callbacks_p
)->when
.tv_usec
= now
.tv_usec
+ (time_ms
% 1000) * 1000;
949 time_ms
-= (time_ms
% 1000);
950 (*callbacks_p
)->when
.tv_sec
= now
.tv_sec
+ (time_ms
/ 1000);
951 if ((*callbacks_p
)->when
.tv_usec
> 1000000)
953 (*callbacks_p
)->when
.tv_usec
= (*callbacks_p
)->when
.tv_usec
- 1000000;
954 (*callbacks_p
)->when
.tv_sec
+= 1;
957 (*callbacks_p
)->priv
= priv
;
958 (*callbacks_p
)->next
= NULL
;
963 int target_unregister_event_callback(int (*callback
)(struct target
*target
, enum target_event event
, void *priv
), void *priv
)
965 struct target_event_callback
**p
= &target_event_callbacks
;
966 struct target_event_callback
*c
= target_event_callbacks
;
968 if (callback
== NULL
)
970 return ERROR_INVALID_ARGUMENTS
;
975 struct target_event_callback
*next
= c
->next
;
976 if ((c
->callback
== callback
) && (c
->priv
== priv
))
990 int target_unregister_timer_callback(int (*callback
)(void *priv
), void *priv
)
992 struct target_timer_callback
**p
= &target_timer_callbacks
;
993 struct target_timer_callback
*c
= target_timer_callbacks
;
995 if (callback
== NULL
)
997 return ERROR_INVALID_ARGUMENTS
;
1002 struct target_timer_callback
*next
= c
->next
;
1003 if ((c
->callback
== callback
) && (c
->priv
== priv
))
1017 int target_call_event_callbacks(struct target
*target
, enum target_event event
)
1019 struct target_event_callback
*callback
= target_event_callbacks
;
1020 struct target_event_callback
*next_callback
;
1022 if (event
== TARGET_EVENT_HALTED
)
1024 /* execute early halted first */
1025 target_call_event_callbacks(target
, TARGET_EVENT_GDB_HALT
);
1028 LOG_DEBUG("target event %i (%s)",
1030 Jim_Nvp_value2name_simple(nvp_target_event
, event
)->name
);
1032 target_handle_event(target
, event
);
1036 next_callback
= callback
->next
;
1037 callback
->callback(target
, event
, callback
->priv
);
1038 callback
= next_callback
;
1044 static int target_timer_callback_periodic_restart(
1045 struct target_timer_callback
*cb
, struct timeval
*now
)
1047 int time_ms
= cb
->time_ms
;
1048 cb
->when
.tv_usec
= now
->tv_usec
+ (time_ms
% 1000) * 1000;
1049 time_ms
-= (time_ms
% 1000);
1050 cb
->when
.tv_sec
= now
->tv_sec
+ time_ms
/ 1000;
1051 if (cb
->when
.tv_usec
> 1000000)
1053 cb
->when
.tv_usec
= cb
->when
.tv_usec
- 1000000;
1054 cb
->when
.tv_sec
+= 1;
1059 static int target_call_timer_callback(struct target_timer_callback
*cb
,
1060 struct timeval
*now
)
1062 cb
->callback(cb
->priv
);
1065 return target_timer_callback_periodic_restart(cb
, now
);
1067 return target_unregister_timer_callback(cb
->callback
, cb
->priv
);
1070 static int target_call_timer_callbacks_check_time(int checktime
)
1075 gettimeofday(&now
, NULL
);
1077 struct target_timer_callback
*callback
= target_timer_callbacks
;
1080 // cleaning up may unregister and free this callback
1081 struct target_timer_callback
*next_callback
= callback
->next
;
1083 bool call_it
= callback
->callback
&&
1084 ((!checktime
&& callback
->periodic
) ||
1085 now
.tv_sec
> callback
->when
.tv_sec
||
1086 (now
.tv_sec
== callback
->when
.tv_sec
&&
1087 now
.tv_usec
>= callback
->when
.tv_usec
));
1091 int retval
= target_call_timer_callback(callback
, &now
);
1092 if (retval
!= ERROR_OK
)
1096 callback
= next_callback
;
1102 int target_call_timer_callbacks(void)
1104 return target_call_timer_callbacks_check_time(1);
1107 /* invoke periodic callbacks immediately */
1108 int target_call_timer_callbacks_now(void)
1110 return target_call_timer_callbacks_check_time(0);
1113 int target_alloc_working_area(struct target
*target
, uint32_t size
, struct working_area
**area
)
1115 struct working_area
*c
= target
->working_areas
;
1116 struct working_area
*new_wa
= NULL
;
1118 /* Reevaluate working area address based on MMU state*/
1119 if (target
->working_areas
== NULL
)
1124 retval
= target
->type
->mmu(target
, &enabled
);
1125 if (retval
!= ERROR_OK
)
1131 if (target
->working_area_phys_spec
) {
1132 LOG_DEBUG("MMU disabled, using physical "
1133 "address for working memory 0x%08x",
1134 (unsigned)target
->working_area_phys
);
1135 target
->working_area
= target
->working_area_phys
;
1137 LOG_ERROR("No working memory available. "
1138 "Specify -work-area-phys to target.");
1139 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
1142 if (target
->working_area_virt_spec
) {
1143 LOG_DEBUG("MMU enabled, using virtual "
1144 "address for working memory 0x%08x",
1145 (unsigned)target
->working_area_virt
);
1146 target
->working_area
= target
->working_area_virt
;
1148 LOG_ERROR("No working memory available. "
1149 "Specify -work-area-virt to target.");
1150 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
1155 /* only allocate multiples of 4 byte */
1158 LOG_ERROR("BUG: code tried to allocate unaligned number of bytes (0x%08x), padding", ((unsigned)(size
)));
1159 size
= (size
+ 3) & (~3);
1162 /* see if there's already a matching working area */
1165 if ((c
->free
) && (c
->size
== size
))
1173 /* if not, allocate a new one */
1176 struct working_area
**p
= &target
->working_areas
;
1177 uint32_t first_free
= target
->working_area
;
1178 uint32_t free_size
= target
->working_area_size
;
1180 c
= target
->working_areas
;
1183 first_free
+= c
->size
;
1184 free_size
-= c
->size
;
1189 if (free_size
< size
)
1191 LOG_WARNING("not enough working area available(requested %u, free %u)",
1192 (unsigned)(size
), (unsigned)(free_size
));
1193 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
1196 LOG_DEBUG("allocated new working area at address 0x%08x", (unsigned)first_free
);
1198 new_wa
= malloc(sizeof(struct working_area
));
1199 new_wa
->next
= NULL
;
1200 new_wa
->size
= size
;
1201 new_wa
->address
= first_free
;
1203 if (target
->backup_working_area
)
1206 new_wa
->backup
= malloc(new_wa
->size
);
1207 if ((retval
= target_read_memory(target
, new_wa
->address
, 4, new_wa
->size
/ 4, new_wa
->backup
)) != ERROR_OK
)
1209 free(new_wa
->backup
);
1216 new_wa
->backup
= NULL
;
1219 /* put new entry in list */
1223 /* mark as used, and return the new (reused) area */
1228 new_wa
->user
= area
;
1233 int target_free_working_area_restore(struct target
*target
, struct working_area
*area
, int restore
)
1238 if (restore
&& target
->backup_working_area
)
1241 if ((retval
= target_write_memory(target
, area
->address
, 4, area
->size
/ 4, area
->backup
)) != ERROR_OK
)
1247 /* mark user pointer invalid */
1254 int target_free_working_area(struct target
*target
, struct working_area
*area
)
1256 return target_free_working_area_restore(target
, area
, 1);
1259 /* free resources and restore memory, if restoring memory fails,
1260 * free up resources anyway
1262 void target_free_all_working_areas_restore(struct target
*target
, int restore
)
1264 struct working_area
*c
= target
->working_areas
;
1268 struct working_area
*next
= c
->next
;
1269 target_free_working_area_restore(target
, c
, restore
);
1279 target
->working_areas
= NULL
;
1282 void target_free_all_working_areas(struct target
*target
)
1284 target_free_all_working_areas_restore(target
, 1);
1287 int target_arch_state(struct target
*target
)
1292 LOG_USER("No target has been configured");
1296 LOG_USER("target state: %s", target_state_name( target
));
1298 if (target
->state
!= TARGET_HALTED
)
1301 retval
= target
->type
->arch_state(target
);
1305 /* Single aligned words are guaranteed to use 16 or 32 bit access
1306 * mode respectively, otherwise data is handled as quickly as
1309 int target_write_buffer(struct target
*target
, uint32_t address
, uint32_t size
, uint8_t *buffer
)
1312 LOG_DEBUG("writing buffer of %i byte at 0x%8.8x",
1313 (int)size
, (unsigned)address
);
1315 if (!target_was_examined(target
))
1317 LOG_ERROR("Target not examined yet");
1325 if ((address
+ size
- 1) < address
)
1327 /* GDB can request this when e.g. PC is 0xfffffffc*/
1328 LOG_ERROR("address + size wrapped(0x%08x, 0x%08x)",
1334 if (((address
% 2) == 0) && (size
== 2))
1336 return target_write_memory(target
, address
, 2, 1, buffer
);
1339 /* handle unaligned head bytes */
1342 uint32_t unaligned
= 4 - (address
% 4);
1344 if (unaligned
> size
)
1347 if ((retval
= target_write_memory(target
, address
, 1, unaligned
, buffer
)) != ERROR_OK
)
1350 buffer
+= unaligned
;
1351 address
+= unaligned
;
1355 /* handle aligned words */
1358 int aligned
= size
- (size
% 4);
1360 /* use bulk writes above a certain limit. This may have to be changed */
1363 if ((retval
= target
->type
->bulk_write_memory(target
, address
, aligned
/ 4, buffer
)) != ERROR_OK
)
1368 if ((retval
= target_write_memory(target
, address
, 4, aligned
/ 4, buffer
)) != ERROR_OK
)
1377 /* handle tail writes of less than 4 bytes */
1380 if ((retval
= target_write_memory(target
, address
, 1, size
, buffer
)) != ERROR_OK
)
1387 /* Single aligned words are guaranteed to use 16 or 32 bit access
1388 * mode respectively, otherwise data is handled as quickly as
1391 int target_read_buffer(struct target
*target
, uint32_t address
, uint32_t size
, uint8_t *buffer
)
1394 LOG_DEBUG("reading buffer of %i byte at 0x%8.8x",
1395 (int)size
, (unsigned)address
);
1397 if (!target_was_examined(target
))
1399 LOG_ERROR("Target not examined yet");
1407 if ((address
+ size
- 1) < address
)
1409 /* GDB can request this when e.g. PC is 0xfffffffc*/
1410 LOG_ERROR("address + size wrapped(0x%08" PRIx32
", 0x%08" PRIx32
")",
1416 if (((address
% 2) == 0) && (size
== 2))
1418 return target_read_memory(target
, address
, 2, 1, buffer
);
1421 /* handle unaligned head bytes */
1424 uint32_t unaligned
= 4 - (address
% 4);
1426 if (unaligned
> size
)
1429 if ((retval
= target_read_memory(target
, address
, 1, unaligned
, buffer
)) != ERROR_OK
)
1432 buffer
+= unaligned
;
1433 address
+= unaligned
;
1437 /* handle aligned words */
1440 int aligned
= size
- (size
% 4);
1442 if ((retval
= target_read_memory(target
, address
, 4, aligned
/ 4, buffer
)) != ERROR_OK
)
1450 /*prevent byte access when possible (avoid AHB access limitations in some cases)*/
1453 int aligned
= size
- (size
%2);
1454 retval
= target_read_memory(target
, address
, 2, aligned
/ 2, buffer
);
1455 if (retval
!= ERROR_OK
)
1462 /* handle tail writes of less than 4 bytes */
1465 if ((retval
= target_read_memory(target
, address
, 1, size
, buffer
)) != ERROR_OK
)
1472 int target_checksum_memory(struct target
*target
, uint32_t address
, uint32_t size
, uint32_t* crc
)
1477 uint32_t checksum
= 0;
1478 if (!target_was_examined(target
))
1480 LOG_ERROR("Target not examined yet");
1484 if ((retval
= target
->type
->checksum_memory(target
, address
,
1485 size
, &checksum
)) != ERROR_OK
)
1487 buffer
= malloc(size
);
1490 LOG_ERROR("error allocating buffer for section (%d bytes)", (int)size
);
1491 return ERROR_INVALID_ARGUMENTS
;
1493 retval
= target_read_buffer(target
, address
, size
, buffer
);
1494 if (retval
!= ERROR_OK
)
1500 /* convert to target endianess */
1501 for (i
= 0; i
< (size
/sizeof(uint32_t)); i
++)
1503 uint32_t target_data
;
1504 target_data
= target_buffer_get_u32(target
, &buffer
[i
*sizeof(uint32_t)]);
1505 target_buffer_set_u32(target
, &buffer
[i
*sizeof(uint32_t)], target_data
);
1508 retval
= image_calculate_checksum(buffer
, size
, &checksum
);
1517 int target_blank_check_memory(struct target
*target
, uint32_t address
, uint32_t size
, uint32_t* blank
)
1520 if (!target_was_examined(target
))
1522 LOG_ERROR("Target not examined yet");
1526 if (target
->type
->blank_check_memory
== 0)
1527 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
1529 retval
= target
->type
->blank_check_memory(target
, address
, size
, blank
);
1534 int target_read_u32(struct target
*target
, uint32_t address
, uint32_t *value
)
1536 uint8_t value_buf
[4];
1537 if (!target_was_examined(target
))
1539 LOG_ERROR("Target not examined yet");
1543 int retval
= target_read_memory(target
, address
, 4, 1, value_buf
);
1545 if (retval
== ERROR_OK
)
1547 *value
= target_buffer_get_u32(target
, value_buf
);
1548 LOG_DEBUG("address: 0x%8.8" PRIx32
", value: 0x%8.8" PRIx32
"",
1555 LOG_DEBUG("address: 0x%8.8" PRIx32
" failed",
1562 int target_read_u16(struct target
*target
, uint32_t address
, uint16_t *value
)
1564 uint8_t value_buf
[2];
1565 if (!target_was_examined(target
))
1567 LOG_ERROR("Target not examined yet");
1571 int retval
= target_read_memory(target
, address
, 2, 1, value_buf
);
1573 if (retval
== ERROR_OK
)
1575 *value
= target_buffer_get_u16(target
, value_buf
);
1576 LOG_DEBUG("address: 0x%8.8" PRIx32
", value: 0x%4.4x",
1583 LOG_DEBUG("address: 0x%8.8" PRIx32
" failed",
1590 int target_read_u8(struct target
*target
, uint32_t address
, uint8_t *value
)
1592 int retval
= target_read_memory(target
, address
, 1, 1, value
);
1593 if (!target_was_examined(target
))
1595 LOG_ERROR("Target not examined yet");
1599 if (retval
== ERROR_OK
)
1601 LOG_DEBUG("address: 0x%8.8" PRIx32
", value: 0x%2.2x",
1608 LOG_DEBUG("address: 0x%8.8" PRIx32
" failed",
1615 int target_write_u32(struct target
*target
, uint32_t address
, uint32_t value
)
1618 uint8_t value_buf
[4];
1619 if (!target_was_examined(target
))
1621 LOG_ERROR("Target not examined yet");
1625 LOG_DEBUG("address: 0x%8.8" PRIx32
", value: 0x%8.8" PRIx32
"",
1629 target_buffer_set_u32(target
, value_buf
, value
);
1630 if ((retval
= target_write_memory(target
, address
, 4, 1, value_buf
)) != ERROR_OK
)
1632 LOG_DEBUG("failed: %i", retval
);
1638 int target_write_u16(struct target
*target
, uint32_t address
, uint16_t value
)
1641 uint8_t value_buf
[2];
1642 if (!target_was_examined(target
))
1644 LOG_ERROR("Target not examined yet");
1648 LOG_DEBUG("address: 0x%8.8" PRIx32
", value: 0x%8.8x",
1652 target_buffer_set_u16(target
, value_buf
, value
);
1653 if ((retval
= target_write_memory(target
, address
, 2, 1, value_buf
)) != ERROR_OK
)
1655 LOG_DEBUG("failed: %i", retval
);
1661 int target_write_u8(struct target
*target
, uint32_t address
, uint8_t value
)
1664 if (!target_was_examined(target
))
1666 LOG_ERROR("Target not examined yet");
1670 LOG_DEBUG("address: 0x%8.8" PRIx32
", value: 0x%2.2x",
1673 if ((retval
= target_write_memory(target
, address
, 1, 1, &value
)) != ERROR_OK
)
1675 LOG_DEBUG("failed: %i", retval
);
1681 COMMAND_HANDLER(handle_targets_command
)
1683 struct target
*target
= all_targets
;
1687 target
= get_target(CMD_ARGV
[0]);
1688 if (target
== NULL
) {
1689 command_print(CMD_CTX
,"Target: %s is unknown, try one of:\n", CMD_ARGV
[0]);
1692 if (!target
->tap
->enabled
) {
1693 command_print(CMD_CTX
,"Target: TAP %s is disabled, "
1694 "can't be the current target\n",
1695 target
->tap
->dotted_name
);
1699 CMD_CTX
->current_target
= target
->target_number
;
1704 target
= all_targets
;
1705 command_print(CMD_CTX
, " TargetName Type Endian TapName State ");
1706 command_print(CMD_CTX
, "-- ------------------ ---------- ------ ------------------ ------------");
1712 if (target
->tap
->enabled
)
1713 state
= target_state_name( target
);
1715 state
= "tap-disabled";
1717 if (CMD_CTX
->current_target
== target
->target_number
)
1720 /* keep columns lined up to match the headers above */
1721 command_print(CMD_CTX
, "%2d%c %-18s %-10s %-6s %-18s %s",
1722 target
->target_number
,
1724 target_name(target
),
1725 target_type_name(target
),
1726 Jim_Nvp_value2name_simple(nvp_target_endian
,
1727 target
->endianness
)->name
,
1728 target
->tap
->dotted_name
,
1730 target
= target
->next
;
1736 /* every 300ms we check for reset & powerdropout and issue a "reset halt" if so. */
1738 static int powerDropout
;
1739 static int srstAsserted
;
1741 static int runPowerRestore
;
1742 static int runPowerDropout
;
1743 static int runSrstAsserted
;
1744 static int runSrstDeasserted
;
1746 static int sense_handler(void)
1748 static int prevSrstAsserted
= 0;
1749 static int prevPowerdropout
= 0;
1752 if ((retval
= jtag_power_dropout(&powerDropout
)) != ERROR_OK
)
1756 powerRestored
= prevPowerdropout
&& !powerDropout
;
1759 runPowerRestore
= 1;
1762 long long current
= timeval_ms();
1763 static long long lastPower
= 0;
1764 int waitMore
= lastPower
+ 2000 > current
;
1765 if (powerDropout
&& !waitMore
)
1767 runPowerDropout
= 1;
1768 lastPower
= current
;
1771 if ((retval
= jtag_srst_asserted(&srstAsserted
)) != ERROR_OK
)
1775 srstDeasserted
= prevSrstAsserted
&& !srstAsserted
;
1777 static long long lastSrst
= 0;
1778 waitMore
= lastSrst
+ 2000 > current
;
1779 if (srstDeasserted
&& !waitMore
)
1781 runSrstDeasserted
= 1;
1785 if (!prevSrstAsserted
&& srstAsserted
)
1787 runSrstAsserted
= 1;
1790 prevSrstAsserted
= srstAsserted
;
1791 prevPowerdropout
= powerDropout
;
1793 if (srstDeasserted
|| powerRestored
)
1795 /* Other than logging the event we can't do anything here.
1796 * Issuing a reset is a particularly bad idea as we might
1797 * be inside a reset already.
1804 /* process target state changes */
1805 static int handle_target(void *priv
)
1807 Jim_Interp
*interp
= (Jim_Interp
*)priv
;
1808 int retval
= ERROR_OK
;
1810 if (!is_jtag_poll_safe())
1812 /* polling is disabled currently */
1816 /* we do not want to recurse here... */
1817 static int recursive
= 0;
1822 /* danger! running these procedures can trigger srst assertions and power dropouts.
1823 * We need to avoid an infinite loop/recursion here and we do that by
1824 * clearing the flags after running these events.
1826 int did_something
= 0;
1827 if (runSrstAsserted
)
1829 LOG_INFO("srst asserted detected, running srst_asserted proc.");
1830 Jim_Eval(interp
, "srst_asserted");
1833 if (runSrstDeasserted
)
1835 Jim_Eval(interp
, "srst_deasserted");
1838 if (runPowerDropout
)
1840 LOG_INFO("Power dropout detected, running power_dropout proc.");
1841 Jim_Eval(interp
, "power_dropout");
1844 if (runPowerRestore
)
1846 Jim_Eval(interp
, "power_restore");
1852 /* clear detect flags */
1856 /* clear action flags */
1858 runSrstAsserted
= 0;
1859 runSrstDeasserted
= 0;
1860 runPowerRestore
= 0;
1861 runPowerDropout
= 0;
1866 /* Poll targets for state changes unless that's globally disabled.
1867 * Skip targets that are currently disabled.
1869 for (struct target
*target
= all_targets
;
1870 is_jtag_poll_safe() && target
;
1871 target
= target
->next
)
1873 if (!target
->tap
->enabled
)
1876 /* only poll target if we've got power and srst isn't asserted */
1877 if (!powerDropout
&& !srstAsserted
)
1879 /* polling may fail silently until the target has been examined */
1880 if ((retval
= target_poll(target
)) != ERROR_OK
)
1882 /* FIX!!!!! If we add a LOG_INFO() here to output a line in GDB
1883 * *why* we are aborting GDB, then we'll spam telnet when the
1884 * poll is failing persistently.
1886 * If we could implement an event that detected the
1887 * target going from non-pollable to pollable, we could issue
1888 * an error only upon the transition.
1890 target_call_event_callbacks(target
, TARGET_EVENT_GDB_HALT
);
1899 COMMAND_HANDLER(handle_reg_command
)
1901 struct target
*target
;
1902 struct reg
*reg
= NULL
;
1908 target
= get_current_target(CMD_CTX
);
1910 /* list all available registers for the current target */
1913 struct reg_cache
*cache
= target
->reg_cache
;
1920 command_print(CMD_CTX
, "===== %s", cache
->name
);
1922 for (i
= 0, reg
= cache
->reg_list
;
1923 i
< cache
->num_regs
;
1924 i
++, reg
++, count
++)
1926 /* only print cached values if they are valid */
1928 value
= buf_to_str(reg
->value
,
1930 command_print(CMD_CTX
,
1931 "(%i) %s (/%" PRIu32
"): 0x%s%s",
1939 command_print(CMD_CTX
, "(%i) %s (/%" PRIu32
")",
1944 cache
= cache
->next
;
1950 /* access a single register by its ordinal number */
1951 if ((CMD_ARGV
[0][0] >= '0') && (CMD_ARGV
[0][0] <= '9'))
1954 COMMAND_PARSE_NUMBER(uint
, CMD_ARGV
[0], num
);
1956 struct reg_cache
*cache
= target
->reg_cache
;
1961 for (i
= 0; i
< cache
->num_regs
; i
++)
1965 reg
= &cache
->reg_list
[i
];
1971 cache
= cache
->next
;
1976 command_print(CMD_CTX
, "%i is out of bounds, the current target has only %i registers (0 - %i)", num
, count
, count
- 1);
1979 } else /* access a single register by its name */
1981 reg
= register_get_by_name(target
->reg_cache
, CMD_ARGV
[0], 1);
1985 command_print(CMD_CTX
, "register %s not found in current target", CMD_ARGV
[0]);
1990 /* display a register */
1991 if ((CMD_ARGC
== 1) || ((CMD_ARGC
== 2) && !((CMD_ARGV
[1][0] >= '0') && (CMD_ARGV
[1][0] <= '9'))))
1993 if ((CMD_ARGC
== 2) && (strcmp(CMD_ARGV
[1], "force") == 0))
1996 if (reg
->valid
== 0)
1998 reg
->type
->get(reg
);
2000 value
= buf_to_str(reg
->value
, reg
->size
, 16);
2001 command_print(CMD_CTX
, "%s (/%i): 0x%s", reg
->name
, (int)(reg
->size
), value
);
2006 /* set register value */
2009 uint8_t *buf
= malloc(DIV_ROUND_UP(reg
->size
, 8));
2010 str_to_buf(CMD_ARGV
[1], strlen(CMD_ARGV
[1]), buf
, reg
->size
, 0);
2012 reg
->type
->set(reg
, buf
);
2014 value
= buf_to_str(reg
->value
, reg
->size
, 16);
2015 command_print(CMD_CTX
, "%s (/%i): 0x%s", reg
->name
, (int)(reg
->size
), value
);
2023 command_print(CMD_CTX
, "usage: reg <#|name> [value]");
2028 COMMAND_HANDLER(handle_poll_command
)
2030 int retval
= ERROR_OK
;
2031 struct target
*target
= get_current_target(CMD_CTX
);
2035 command_print(CMD_CTX
, "background polling: %s",
2036 jtag_poll_get_enabled() ? "on" : "off");
2037 command_print(CMD_CTX
, "TAP: %s (%s)",
2038 target
->tap
->dotted_name
,
2039 target
->tap
->enabled
? "enabled" : "disabled");
2040 if (!target
->tap
->enabled
)
2042 if ((retval
= target_poll(target
)) != ERROR_OK
)
2044 if ((retval
= target_arch_state(target
)) != ERROR_OK
)
2047 else if (CMD_ARGC
== 1)
2050 COMMAND_PARSE_ON_OFF(CMD_ARGV
[0], enable
);
2051 jtag_poll_set_enabled(enable
);
2055 return ERROR_COMMAND_SYNTAX_ERROR
;
2061 COMMAND_HANDLER(handle_wait_halt_command
)
2064 return ERROR_COMMAND_SYNTAX_ERROR
;
2069 int retval
= parse_uint(CMD_ARGV
[0], &ms
);
2070 if (ERROR_OK
!= retval
)
2072 command_print(CMD_CTX
, "usage: %s [seconds]", CMD_NAME
);
2073 return ERROR_COMMAND_SYNTAX_ERROR
;
2075 // convert seconds (given) to milliseconds (needed)
2079 struct target
*target
= get_current_target(CMD_CTX
);
2080 return target_wait_state(target
, TARGET_HALTED
, ms
);
2083 /* wait for target state to change. The trick here is to have a low
2084 * latency for short waits and not to suck up all the CPU time
2087 * After 500ms, keep_alive() is invoked
2089 int target_wait_state(struct target
*target
, enum target_state state
, int ms
)
2092 long long then
= 0, cur
;
2097 if ((retval
= target_poll(target
)) != ERROR_OK
)
2099 if (target
->state
== state
)
2107 then
= timeval_ms();
2108 LOG_DEBUG("waiting for target %s...",
2109 Jim_Nvp_value2name_simple(nvp_target_state
,state
)->name
);
2117 if ((cur
-then
) > ms
)
2119 LOG_ERROR("timed out while waiting for target %s",
2120 Jim_Nvp_value2name_simple(nvp_target_state
,state
)->name
);
2128 COMMAND_HANDLER(handle_halt_command
)
2132 struct target
*target
= get_current_target(CMD_CTX
);
2133 int retval
= target_halt(target
);
2134 if (ERROR_OK
!= retval
)
2140 retval
= parse_uint(CMD_ARGV
[0], &wait
);
2141 if (ERROR_OK
!= retval
)
2142 return ERROR_COMMAND_SYNTAX_ERROR
;
2147 return CALL_COMMAND_HANDLER(handle_wait_halt_command
);
2150 COMMAND_HANDLER(handle_soft_reset_halt_command
)
2152 struct target
*target
= get_current_target(CMD_CTX
);
2154 LOG_USER("requesting target halt and executing a soft reset");
2156 target
->type
->soft_reset_halt(target
);
2161 COMMAND_HANDLER(handle_reset_command
)
2164 return ERROR_COMMAND_SYNTAX_ERROR
;
2166 enum target_reset_mode reset_mode
= RESET_RUN
;
2170 n
= Jim_Nvp_name2value_simple(nvp_reset_modes
, CMD_ARGV
[0]);
2171 if ((n
->name
== NULL
) || (n
->value
== RESET_UNKNOWN
)) {
2172 return ERROR_COMMAND_SYNTAX_ERROR
;
2174 reset_mode
= n
->value
;
2177 /* reset *all* targets */
2178 return target_process_reset(CMD_CTX
, reset_mode
);
2182 COMMAND_HANDLER(handle_resume_command
)
2186 return ERROR_COMMAND_SYNTAX_ERROR
;
2188 struct target
*target
= get_current_target(CMD_CTX
);
2189 target_handle_event(target
, TARGET_EVENT_OLD_pre_resume
);
2191 /* with no CMD_ARGV, resume from current pc, addr = 0,
2192 * with one arguments, addr = CMD_ARGV[0],
2193 * handle breakpoints, not debugging */
2197 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], addr
);
2201 return target_resume(target
, current
, addr
, 1, 0);
2204 COMMAND_HANDLER(handle_step_command
)
2207 return ERROR_COMMAND_SYNTAX_ERROR
;
2211 /* with no CMD_ARGV, step from current pc, addr = 0,
2212 * with one argument addr = CMD_ARGV[0],
2213 * handle breakpoints, debugging */
2218 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], addr
);
2222 struct target
*target
= get_current_target(CMD_CTX
);
2224 return target
->type
->step(target
, current_pc
, addr
, 1);
2227 static void handle_md_output(struct command_context
*cmd_ctx
,
2228 struct target
*target
, uint32_t address
, unsigned size
,
2229 unsigned count
, const uint8_t *buffer
)
2231 const unsigned line_bytecnt
= 32;
2232 unsigned line_modulo
= line_bytecnt
/ size
;
2234 char output
[line_bytecnt
* 4 + 1];
2235 unsigned output_len
= 0;
2237 const char *value_fmt
;
2239 case 4: value_fmt
= "%8.8x "; break;
2240 case 2: value_fmt
= "%4.4x "; break;
2241 case 1: value_fmt
= "%2.2x "; break;
2243 /* "can't happen", caller checked */
2244 LOG_ERROR("invalid memory read size: %u", size
);
2248 for (unsigned i
= 0; i
< count
; i
++)
2250 if (i
% line_modulo
== 0)
2252 output_len
+= snprintf(output
+ output_len
,
2253 sizeof(output
) - output_len
,
2255 (unsigned)(address
+ (i
*size
)));
2259 const uint8_t *value_ptr
= buffer
+ i
* size
;
2261 case 4: value
= target_buffer_get_u32(target
, value_ptr
); break;
2262 case 2: value
= target_buffer_get_u16(target
, value_ptr
); break;
2263 case 1: value
= *value_ptr
;
2265 output_len
+= snprintf(output
+ output_len
,
2266 sizeof(output
) - output_len
,
2269 if ((i
% line_modulo
== line_modulo
- 1) || (i
== count
- 1))
2271 command_print(cmd_ctx
, "%s", output
);
2277 COMMAND_HANDLER(handle_md_command
)
2280 return ERROR_COMMAND_SYNTAX_ERROR
;
2283 switch (CMD_NAME
[2]) {
2284 case 'w': size
= 4; break;
2285 case 'h': size
= 2; break;
2286 case 'b': size
= 1; break;
2287 default: return ERROR_COMMAND_SYNTAX_ERROR
;
2290 bool physical
=strcmp(CMD_ARGV
[0], "phys")==0;
2291 int (*fn
)(struct target
*target
,
2292 uint32_t address
, uint32_t size
, uint32_t count
, uint8_t *buffer
);
2297 fn
=target_read_phys_memory
;
2300 fn
=target_read_memory
;
2302 if ((CMD_ARGC
< 1) || (CMD_ARGC
> 2))
2304 return ERROR_COMMAND_SYNTAX_ERROR
;
2308 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], address
);
2312 COMMAND_PARSE_NUMBER(uint
, CMD_ARGV
[1], count
);
2314 uint8_t *buffer
= calloc(count
, size
);
2316 struct target
*target
= get_current_target(CMD_CTX
);
2317 int retval
= fn(target
, address
, size
, count
, buffer
);
2318 if (ERROR_OK
== retval
)
2319 handle_md_output(CMD_CTX
, target
, address
, size
, count
, buffer
);
2326 typedef int (*target_write_fn
)(struct target
*target
,
2327 uint32_t address
, uint32_t size
, uint32_t count
, uint8_t *buffer
);
2329 static int target_write_memory_fast(struct target
*target
,
2330 uint32_t address
, uint32_t size
, uint32_t count
, uint8_t *buffer
)
2332 return target_write_buffer(target
, address
, size
* count
, buffer
);
2335 static int target_fill_mem(struct target
*target
,
2344 /* We have to write in reasonably large chunks to be able
2345 * to fill large memory areas with any sane speed */
2346 const unsigned chunk_size
= 16384;
2347 uint8_t *target_buf
= malloc(chunk_size
* data_size
);
2348 if (target_buf
== NULL
)
2350 LOG_ERROR("Out of memory");
2354 for (unsigned i
= 0; i
< chunk_size
; i
++)
2359 target_buffer_set_u32(target
, target_buf
+ i
*data_size
, b
);
2362 target_buffer_set_u16(target
, target_buf
+ i
*data_size
, b
);
2365 target_buffer_set_u8(target
, target_buf
+ i
*data_size
, b
);
2372 int retval
= ERROR_OK
;
2374 for (unsigned x
= 0; x
< c
; x
+= chunk_size
)
2378 if (current
> chunk_size
)
2380 current
= chunk_size
;
2382 int retval
= fn(target
, address
+ x
* data_size
, data_size
, current
, target_buf
);
2383 if (retval
!= ERROR_OK
)
2387 /* avoid GDB timeouts */
2396 COMMAND_HANDLER(handle_mw_command
)
2400 return ERROR_COMMAND_SYNTAX_ERROR
;
2402 bool physical
=strcmp(CMD_ARGV
[0], "phys")==0;
2408 fn
=target_write_phys_memory
;
2411 fn
= target_write_memory_fast
;
2413 if ((CMD_ARGC
< 2) || (CMD_ARGC
> 3))
2414 return ERROR_COMMAND_SYNTAX_ERROR
;
2417 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], address
);
2420 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], value
);
2424 COMMAND_PARSE_NUMBER(uint
, CMD_ARGV
[2], count
);
2426 struct target
*target
= get_current_target(CMD_CTX
);
2428 switch (CMD_NAME
[2])
2440 return ERROR_COMMAND_SYNTAX_ERROR
;
2443 return target_fill_mem(target
, address
, fn
, wordsize
, value
, count
);
2446 static COMMAND_HELPER(parse_load_image_command_CMD_ARGV
, struct image
*image
,
2447 uint32_t *min_address
, uint32_t *max_address
)
2449 if (CMD_ARGC
< 1 || CMD_ARGC
> 5)
2450 return ERROR_COMMAND_SYNTAX_ERROR
;
2452 /* a base address isn't always necessary,
2453 * default to 0x0 (i.e. don't relocate) */
2457 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], addr
);
2458 image
->base_address
= addr
;
2459 image
->base_address_set
= 1;
2462 image
->base_address_set
= 0;
2464 image
->start_address_set
= 0;
2468 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[3], *min_address
);
2472 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[4], *max_address
);
2473 // use size (given) to find max (required)
2474 *max_address
+= *min_address
;
2477 if (*min_address
> *max_address
)
2478 return ERROR_COMMAND_SYNTAX_ERROR
;
2483 COMMAND_HANDLER(handle_load_image_command
)
2487 uint32_t image_size
;
2488 uint32_t min_address
= 0;
2489 uint32_t max_address
= 0xffffffff;
2493 int retval
= CALL_COMMAND_HANDLER(parse_load_image_command_CMD_ARGV
,
2494 &image
, &min_address
, &max_address
);
2495 if (ERROR_OK
!= retval
)
2498 struct target
*target
= get_current_target(CMD_CTX
);
2500 struct duration bench
;
2501 duration_start(&bench
);
2503 if (image_open(&image
, CMD_ARGV
[0], (CMD_ARGC
>= 3) ? CMD_ARGV
[2] : NULL
) != ERROR_OK
)
2510 for (i
= 0; i
< image
.num_sections
; i
++)
2512 buffer
= malloc(image
.sections
[i
].size
);
2515 command_print(CMD_CTX
,
2516 "error allocating buffer for section (%d bytes)",
2517 (int)(image
.sections
[i
].size
));
2521 if ((retval
= image_read_section(&image
, i
, 0x0, image
.sections
[i
].size
, buffer
, &buf_cnt
)) != ERROR_OK
)
2527 uint32_t offset
= 0;
2528 uint32_t length
= buf_cnt
;
2530 /* DANGER!!! beware of unsigned comparision here!!! */
2532 if ((image
.sections
[i
].base_address
+ buf_cnt
>= min_address
)&&
2533 (image
.sections
[i
].base_address
< max_address
))
2535 if (image
.sections
[i
].base_address
< min_address
)
2537 /* clip addresses below */
2538 offset
+= min_address
-image
.sections
[i
].base_address
;
2542 if (image
.sections
[i
].base_address
+ buf_cnt
> max_address
)
2544 length
-= (image
.sections
[i
].base_address
+ buf_cnt
)-max_address
;
2547 if ((retval
= target_write_buffer(target
, image
.sections
[i
].base_address
+ offset
, length
, buffer
+ offset
)) != ERROR_OK
)
2552 image_size
+= length
;
2553 command_print(CMD_CTX
, "%u bytes written at address 0x%8.8" PRIx32
"",
2554 (unsigned int)length
,
2555 image
.sections
[i
].base_address
+ offset
);
2561 if ((ERROR_OK
== retval
) && (duration_measure(&bench
) == ERROR_OK
))
2563 command_print(CMD_CTX
, "downloaded %" PRIu32
" bytes "
2564 "in %fs (%0.3f kb/s)", image_size
,
2565 duration_elapsed(&bench
), duration_kbps(&bench
, image_size
));
2568 image_close(&image
);
2574 COMMAND_HANDLER(handle_dump_image_command
)
2576 struct fileio fileio
;
2578 uint8_t buffer
[560];
2582 struct target
*target
= get_current_target(CMD_CTX
);
2586 command_print(CMD_CTX
, "usage: dump_image <filename> <address> <size>");
2591 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], address
);
2593 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[2], size
);
2595 if (fileio_open(&fileio
, CMD_ARGV
[0], FILEIO_WRITE
, FILEIO_BINARY
) != ERROR_OK
)
2600 struct duration bench
;
2601 duration_start(&bench
);
2603 int retval
= ERROR_OK
;
2606 size_t size_written
;
2607 uint32_t this_run_size
= (size
> 560) ? 560 : size
;
2608 retval
= target_read_buffer(target
, address
, this_run_size
, buffer
);
2609 if (retval
!= ERROR_OK
)
2614 retval
= fileio_write(&fileio
, this_run_size
, buffer
, &size_written
);
2615 if (retval
!= ERROR_OK
)
2620 size
-= this_run_size
;
2621 address
+= this_run_size
;
2624 if ((retvaltemp
= fileio_close(&fileio
)) != ERROR_OK
)
2627 if ((ERROR_OK
== retval
) && (duration_measure(&bench
) == ERROR_OK
))
2629 command_print(CMD_CTX
,
2630 "dumped %ld bytes in %fs (%0.3f kb/s)", (long)fileio
.size
,
2631 duration_elapsed(&bench
), duration_kbps(&bench
, fileio
.size
));
2637 static COMMAND_HELPER(handle_verify_image_command_internal
, int verify
)
2641 uint32_t image_size
;
2644 uint32_t checksum
= 0;
2645 uint32_t mem_checksum
= 0;
2649 struct target
*target
= get_current_target(CMD_CTX
);
2653 return ERROR_COMMAND_SYNTAX_ERROR
;
2658 LOG_ERROR("no target selected");
2662 struct duration bench
;
2663 duration_start(&bench
);
2668 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], addr
);
2669 image
.base_address
= addr
;
2670 image
.base_address_set
= 1;
2674 image
.base_address_set
= 0;
2675 image
.base_address
= 0x0;
2678 image
.start_address_set
= 0;
2680 if ((retval
= image_open(&image
, CMD_ARGV
[0], (CMD_ARGC
== 3) ? CMD_ARGV
[2] : NULL
)) != ERROR_OK
)
2687 for (i
= 0; i
< image
.num_sections
; i
++)
2689 buffer
= malloc(image
.sections
[i
].size
);
2692 command_print(CMD_CTX
,
2693 "error allocating buffer for section (%d bytes)",
2694 (int)(image
.sections
[i
].size
));
2697 if ((retval
= image_read_section(&image
, i
, 0x0, image
.sections
[i
].size
, buffer
, &buf_cnt
)) != ERROR_OK
)
2705 /* calculate checksum of image */
2706 image_calculate_checksum(buffer
, buf_cnt
, &checksum
);
2708 retval
= target_checksum_memory(target
, image
.sections
[i
].base_address
, buf_cnt
, &mem_checksum
);
2709 if (retval
!= ERROR_OK
)
2715 if (checksum
!= mem_checksum
)
2717 /* failed crc checksum, fall back to a binary compare */
2720 command_print(CMD_CTX
, "checksum mismatch - attempting binary compare");
2722 data
= (uint8_t*)malloc(buf_cnt
);
2724 /* Can we use 32bit word accesses? */
2726 int count
= buf_cnt
;
2727 if ((count
% 4) == 0)
2732 retval
= target_read_memory(target
, image
.sections
[i
].base_address
, size
, count
, data
);
2733 if (retval
== ERROR_OK
)
2736 for (t
= 0; t
< buf_cnt
; t
++)
2738 if (data
[t
] != buffer
[t
])
2740 command_print(CMD_CTX
,
2741 "Verify operation failed address 0x%08x. Was 0x%02x instead of 0x%02x\n",
2742 (unsigned)(t
+ image
.sections
[i
].base_address
),
2747 retval
= ERROR_FAIL
;
2761 command_print(CMD_CTX
, "address 0x%08" PRIx32
" length 0x%08zx",
2762 image
.sections
[i
].base_address
,
2767 image_size
+= buf_cnt
;
2770 if ((ERROR_OK
== retval
) && (duration_measure(&bench
) == ERROR_OK
))
2772 command_print(CMD_CTX
, "verified %" PRIu32
" bytes "
2773 "in %fs (%0.3f kb/s)", image_size
,
2774 duration_elapsed(&bench
), duration_kbps(&bench
, image_size
));
2777 image_close(&image
);
2782 COMMAND_HANDLER(handle_verify_image_command
)
2784 return CALL_COMMAND_HANDLER(handle_verify_image_command_internal
, 1);
2787 COMMAND_HANDLER(handle_test_image_command
)
2789 return CALL_COMMAND_HANDLER(handle_verify_image_command_internal
, 0);
2792 static int handle_bp_command_list(struct command_context
*cmd_ctx
)
2794 struct target
*target
= get_current_target(cmd_ctx
);
2795 struct breakpoint
*breakpoint
= target
->breakpoints
;
2798 if (breakpoint
->type
== BKPT_SOFT
)
2800 char* buf
= buf_to_str(breakpoint
->orig_instr
,
2801 breakpoint
->length
, 16);
2802 command_print(cmd_ctx
, "0x%8.8" PRIx32
", 0x%x, %i, 0x%s",
2803 breakpoint
->address
,
2805 breakpoint
->set
, buf
);
2810 command_print(cmd_ctx
, "0x%8.8" PRIx32
", 0x%x, %i",
2811 breakpoint
->address
,
2812 breakpoint
->length
, breakpoint
->set
);
2815 breakpoint
= breakpoint
->next
;
2820 static int handle_bp_command_set(struct command_context
*cmd_ctx
,
2821 uint32_t addr
, uint32_t length
, int hw
)
2823 struct target
*target
= get_current_target(cmd_ctx
);
2824 int retval
= breakpoint_add(target
, addr
, length
, hw
);
2825 if (ERROR_OK
== retval
)
2826 command_print(cmd_ctx
, "breakpoint set at 0x%8.8" PRIx32
"", addr
);
2828 LOG_ERROR("Failure setting breakpoint");
2832 COMMAND_HANDLER(handle_bp_command
)
2835 return handle_bp_command_list(CMD_CTX
);
2837 if (CMD_ARGC
< 2 || CMD_ARGC
> 3)
2839 command_print(CMD_CTX
, "usage: bp <address> <length> ['hw']");
2840 return ERROR_COMMAND_SYNTAX_ERROR
;
2844 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], addr
);
2846 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], length
);
2851 if (strcmp(CMD_ARGV
[2], "hw") == 0)
2854 return ERROR_COMMAND_SYNTAX_ERROR
;
2857 return handle_bp_command_set(CMD_CTX
, addr
, length
, hw
);
2860 COMMAND_HANDLER(handle_rbp_command
)
2863 return ERROR_COMMAND_SYNTAX_ERROR
;
2866 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], addr
);
2868 struct target
*target
= get_current_target(CMD_CTX
);
2869 breakpoint_remove(target
, addr
);
2874 COMMAND_HANDLER(handle_wp_command
)
2876 struct target
*target
= get_current_target(CMD_CTX
);
2880 struct watchpoint
*watchpoint
= target
->watchpoints
;
2884 command_print(CMD_CTX
, "address: 0x%8.8" PRIx32
2885 ", len: 0x%8.8" PRIx32
2886 ", r/w/a: %i, value: 0x%8.8" PRIx32
2887 ", mask: 0x%8.8" PRIx32
,
2888 watchpoint
->address
,
2890 (int)watchpoint
->rw
,
2893 watchpoint
= watchpoint
->next
;
2898 enum watchpoint_rw type
= WPT_ACCESS
;
2900 uint32_t length
= 0;
2901 uint32_t data_value
= 0x0;
2902 uint32_t data_mask
= 0xffffffff;
2907 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[4], data_mask
);
2910 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[3], data_value
);
2913 switch (CMD_ARGV
[2][0])
2925 LOG_ERROR("invalid watchpoint mode ('%c')", CMD_ARGV
[2][0]);
2926 return ERROR_COMMAND_SYNTAX_ERROR
;
2930 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], length
);
2931 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], addr
);
2935 command_print(CMD_CTX
, "usage: wp [address length "
2936 "[(r|w|a) [value [mask]]]]");
2937 return ERROR_COMMAND_SYNTAX_ERROR
;
2940 int retval
= watchpoint_add(target
, addr
, length
, type
,
2941 data_value
, data_mask
);
2942 if (ERROR_OK
!= retval
)
2943 LOG_ERROR("Failure setting watchpoints");
2948 COMMAND_HANDLER(handle_rwp_command
)
2951 return ERROR_COMMAND_SYNTAX_ERROR
;
2954 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], addr
);
2956 struct target
*target
= get_current_target(CMD_CTX
);
2957 watchpoint_remove(target
, addr
);
2964 * Translate a virtual address to a physical address.
2966 * The low-level target implementation must have logged a detailed error
2967 * which is forwarded to telnet/GDB session.
2969 COMMAND_HANDLER(handle_virt2phys_command
)
2972 return ERROR_COMMAND_SYNTAX_ERROR
;
2975 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], va
);
2978 struct target
*target
= get_current_target(CMD_CTX
);
2979 int retval
= target
->type
->virt2phys(target
, va
, &pa
);
2980 if (retval
== ERROR_OK
)
2981 command_print(CMD_CTX
, "Physical address 0x%08" PRIx32
"", pa
);
2986 static void writeData(FILE *f
, const void *data
, size_t len
)
2988 size_t written
= fwrite(data
, 1, len
, f
);
2990 LOG_ERROR("failed to write %zu bytes: %s", len
, strerror(errno
));
2993 static void writeLong(FILE *f
, int l
)
2996 for (i
= 0; i
< 4; i
++)
2998 char c
= (l
>> (i
*8))&0xff;
2999 writeData(f
, &c
, 1);
3004 static void writeString(FILE *f
, char *s
)
3006 writeData(f
, s
, strlen(s
));
3009 /* Dump a gmon.out histogram file. */
3010 static void writeGmon(uint32_t *samples
, uint32_t sampleNum
, const char *filename
)
3013 FILE *f
= fopen(filename
, "w");
3016 writeString(f
, "gmon");
3017 writeLong(f
, 0x00000001); /* Version */
3018 writeLong(f
, 0); /* padding */
3019 writeLong(f
, 0); /* padding */
3020 writeLong(f
, 0); /* padding */
3022 uint8_t zero
= 0; /* GMON_TAG_TIME_HIST */
3023 writeData(f
, &zero
, 1);
3025 /* figure out bucket size */
3026 uint32_t min
= samples
[0];
3027 uint32_t max
= samples
[0];
3028 for (i
= 0; i
< sampleNum
; i
++)
3030 if (min
> samples
[i
])
3034 if (max
< samples
[i
])
3040 int addressSpace
= (max
-min
+ 1);
3042 static const uint32_t maxBuckets
= 256 * 1024; /* maximum buckets. */
3043 uint32_t length
= addressSpace
;
3044 if (length
> maxBuckets
)
3046 length
= maxBuckets
;
3048 int *buckets
= malloc(sizeof(int)*length
);
3049 if (buckets
== NULL
)
3054 memset(buckets
, 0, sizeof(int)*length
);
3055 for (i
= 0; i
< sampleNum
;i
++)
3057 uint32_t address
= samples
[i
];
3058 long long a
= address
-min
;
3059 long long b
= length
-1;
3060 long long c
= addressSpace
-1;
3061 int index
= (a
*b
)/c
; /* danger!!!! int32 overflows */
3065 /* append binary memory gmon.out &profile_hist_hdr ((char*)&profile_hist_hdr + sizeof(struct gmon_hist_hdr)) */
3066 writeLong(f
, min
); /* low_pc */
3067 writeLong(f
, max
); /* high_pc */
3068 writeLong(f
, length
); /* # of samples */
3069 writeLong(f
, 64000000); /* 64MHz */
3070 writeString(f
, "seconds");
3071 for (i
= 0; i
< (15-strlen("seconds")); i
++)
3072 writeData(f
, &zero
, 1);
3073 writeString(f
, "s");
3075 /*append binary memory gmon.out profile_hist_data (profile_hist_data + profile_hist_hdr.hist_size) */
3077 char *data
= malloc(2*length
);
3080 for (i
= 0; i
< length
;i
++)
3089 data
[i
*2 + 1]=(val
>> 8)&0xff;
3092 writeData(f
, data
, length
* 2);
3102 /* profiling samples the CPU PC as quickly as OpenOCD is able,
3103 * which will be used as a random sampling of PC */
3104 COMMAND_HANDLER(handle_profile_command
)
3106 struct target
*target
= get_current_target(CMD_CTX
);
3107 struct timeval timeout
, now
;
3109 gettimeofday(&timeout
, NULL
);
3112 return ERROR_COMMAND_SYNTAX_ERROR
;
3115 COMMAND_PARSE_NUMBER(uint
, CMD_ARGV
[0], offset
);
3117 timeval_add_time(&timeout
, offset
, 0);
3120 * @todo: Some cores let us sample the PC without the
3121 * annoying halt/resume step; for example, ARMv7 PCSR.
3122 * Provide a way to use that more efficient mechanism.
3125 command_print(CMD_CTX
, "Starting profiling. Halting and resuming the target as often as we can...");
3127 static const int maxSample
= 10000;
3128 uint32_t *samples
= malloc(sizeof(uint32_t)*maxSample
);
3129 if (samples
== NULL
)
3133 /* hopefully it is safe to cache! We want to stop/restart as quickly as possible. */
3134 struct reg
*reg
= register_get_by_name(target
->reg_cache
, "pc", 1);
3139 target_poll(target
);
3140 if (target
->state
== TARGET_HALTED
)
3142 uint32_t t
=*((uint32_t *)reg
->value
);
3143 samples
[numSamples
++]=t
;
3144 retval
= target_resume(target
, 1, 0, 0, 0); /* current pc, addr = 0, do not handle breakpoints, not debugging */
3145 target_poll(target
);
3146 alive_sleep(10); /* sleep 10ms, i.e. <100 samples/second. */
3147 } else if (target
->state
== TARGET_RUNNING
)
3149 /* We want to quickly sample the PC. */
3150 if ((retval
= target_halt(target
)) != ERROR_OK
)
3157 command_print(CMD_CTX
, "Target not halted or running");
3161 if (retval
!= ERROR_OK
)
3166 gettimeofday(&now
, NULL
);
3167 if ((numSamples
>= maxSample
) || ((now
.tv_sec
>= timeout
.tv_sec
) && (now
.tv_usec
>= timeout
.tv_usec
)))
3169 command_print(CMD_CTX
, "Profiling completed. %d samples.", numSamples
);
3170 if ((retval
= target_poll(target
)) != ERROR_OK
)
3175 if (target
->state
== TARGET_HALTED
)
3177 target_resume(target
, 1, 0, 0, 0); /* current pc, addr = 0, do not handle breakpoints, not debugging */
3179 if ((retval
= target_poll(target
)) != ERROR_OK
)
3184 writeGmon(samples
, numSamples
, CMD_ARGV
[1]);
3185 command_print(CMD_CTX
, "Wrote %s", CMD_ARGV
[1]);
3194 static int new_int_array_element(Jim_Interp
* interp
, const char *varname
, int idx
, uint32_t val
)
3197 Jim_Obj
*nameObjPtr
, *valObjPtr
;
3200 namebuf
= alloc_printf("%s(%d)", varname
, idx
);
3204 nameObjPtr
= Jim_NewStringObj(interp
, namebuf
, -1);
3205 valObjPtr
= Jim_NewIntObj(interp
, val
);
3206 if (!nameObjPtr
|| !valObjPtr
)
3212 Jim_IncrRefCount(nameObjPtr
);
3213 Jim_IncrRefCount(valObjPtr
);
3214 result
= Jim_SetVariable(interp
, nameObjPtr
, valObjPtr
);
3215 Jim_DecrRefCount(interp
, nameObjPtr
);
3216 Jim_DecrRefCount(interp
, valObjPtr
);
3218 /* printf("%s(%d) <= 0%08x\n", varname, idx, val); */
3222 static int jim_mem2array(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
3224 struct command_context
*context
;
3225 struct target
*target
;
3227 context
= Jim_GetAssocData(interp
, "context");
3228 if (context
== NULL
)
3230 LOG_ERROR("mem2array: no command context");
3233 target
= get_current_target(context
);
3236 LOG_ERROR("mem2array: no current target");
3240 return target_mem2array(interp
, target
, argc
-1, argv
+ 1);
3243 static int target_mem2array(Jim_Interp
*interp
, struct target
*target
, int argc
, Jim_Obj
*const *argv
)
3251 const char *varname
;
3255 /* argv[1] = name of array to receive the data
3256 * argv[2] = desired width
3257 * argv[3] = memory address
3258 * argv[4] = count of times to read
3261 Jim_WrongNumArgs(interp
, 1, argv
, "varname width addr nelems");
3264 varname
= Jim_GetString(argv
[0], &len
);
3265 /* given "foo" get space for worse case "foo(%d)" .. add 20 */
3267 e
= Jim_GetLong(interp
, argv
[1], &l
);
3273 e
= Jim_GetLong(interp
, argv
[2], &l
);
3278 e
= Jim_GetLong(interp
, argv
[3], &l
);
3294 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3295 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "Invalid width param, must be 8/16/32", NULL
);
3299 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3300 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "mem2array: zero width read?", NULL
);
3303 if ((addr
+ (len
* width
)) < addr
) {
3304 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3305 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "mem2array: addr + len - wraps to zero?", NULL
);
3308 /* absurd transfer size? */
3310 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3311 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "mem2array: absurd > 64K item request", NULL
);
3316 ((width
== 2) && ((addr
& 1) == 0)) ||
3317 ((width
== 4) && ((addr
& 3) == 0))) {
3321 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3322 sprintf(buf
, "mem2array address: 0x%08" PRIx32
" is not aligned for %" PRId32
" byte reads",
3325 Jim_AppendStrings(interp
, Jim_GetResult(interp
), buf
, NULL
);
3334 size_t buffersize
= 4096;
3335 uint8_t *buffer
= malloc(buffersize
);
3342 /* Slurp... in buffer size chunks */
3344 count
= len
; /* in objects.. */
3345 if (count
> (buffersize
/width
)) {
3346 count
= (buffersize
/width
);
3349 retval
= target_read_memory(target
, addr
, width
, count
, buffer
);
3350 if (retval
!= ERROR_OK
) {
3352 LOG_ERROR("mem2array: Read @ 0x%08x, w=%d, cnt=%d, failed",
3356 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3357 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "mem2array: cannot read memory", NULL
);
3361 v
= 0; /* shut up gcc */
3362 for (i
= 0 ;i
< count
;i
++, n
++) {
3365 v
= target_buffer_get_u32(target
, &buffer
[i
*width
]);
3368 v
= target_buffer_get_u16(target
, &buffer
[i
*width
]);
3371 v
= buffer
[i
] & 0x0ff;
3374 new_int_array_element(interp
, varname
, n
, v
);
3382 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3387 static int get_int_array_element(Jim_Interp
* interp
, const char *varname
, int idx
, uint32_t *val
)
3390 Jim_Obj
*nameObjPtr
, *valObjPtr
;
3394 namebuf
= alloc_printf("%s(%d)", varname
, idx
);
3398 nameObjPtr
= Jim_NewStringObj(interp
, namebuf
, -1);
3405 Jim_IncrRefCount(nameObjPtr
);
3406 valObjPtr
= Jim_GetVariable(interp
, nameObjPtr
, JIM_ERRMSG
);
3407 Jim_DecrRefCount(interp
, nameObjPtr
);
3409 if (valObjPtr
== NULL
)
3412 result
= Jim_GetLong(interp
, valObjPtr
, &l
);
3413 /* printf("%s(%d) => 0%08x\n", varname, idx, val); */
3418 static int jim_array2mem(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
3420 struct command_context
*context
;
3421 struct target
*target
;
3423 context
= Jim_GetAssocData(interp
, "context");
3424 if (context
== NULL
) {
3425 LOG_ERROR("array2mem: no command context");
3428 target
= get_current_target(context
);
3429 if (target
== NULL
) {
3430 LOG_ERROR("array2mem: no current target");
3434 return target_array2mem(interp
,target
, argc
-1, argv
+ 1);
3437 static int target_array2mem(Jim_Interp
*interp
, struct target
*target
,
3438 int argc
, Jim_Obj
*const *argv
)
3446 const char *varname
;
3450 /* argv[1] = name of array to get the data
3451 * argv[2] = desired width
3452 * argv[3] = memory address
3453 * argv[4] = count to write
3456 Jim_WrongNumArgs(interp
, 0, argv
, "varname width addr nelems");
3459 varname
= Jim_GetString(argv
[0], &len
);
3460 /* given "foo" get space for worse case "foo(%d)" .. add 20 */
3462 e
= Jim_GetLong(interp
, argv
[1], &l
);
3468 e
= Jim_GetLong(interp
, argv
[2], &l
);
3473 e
= Jim_GetLong(interp
, argv
[3], &l
);
3489 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3490 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "Invalid width param, must be 8/16/32", NULL
);
3494 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3495 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "array2mem: zero width read?", NULL
);
3498 if ((addr
+ (len
* width
)) < addr
) {
3499 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3500 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "array2mem: addr + len - wraps to zero?", NULL
);
3503 /* absurd transfer size? */
3505 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3506 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "array2mem: absurd > 64K item request", NULL
);
3511 ((width
== 2) && ((addr
& 1) == 0)) ||
3512 ((width
== 4) && ((addr
& 3) == 0))) {
3516 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3517 sprintf(buf
, "array2mem address: 0x%08x is not aligned for %d byte reads",
3520 Jim_AppendStrings(interp
, Jim_GetResult(interp
), buf
, NULL
);
3531 size_t buffersize
= 4096;
3532 uint8_t *buffer
= malloc(buffersize
);
3537 /* Slurp... in buffer size chunks */
3539 count
= len
; /* in objects.. */
3540 if (count
> (buffersize
/width
)) {
3541 count
= (buffersize
/width
);
3544 v
= 0; /* shut up gcc */
3545 for (i
= 0 ;i
< count
;i
++, n
++) {
3546 get_int_array_element(interp
, varname
, n
, &v
);
3549 target_buffer_set_u32(target
, &buffer
[i
*width
], v
);
3552 target_buffer_set_u16(target
, &buffer
[i
*width
], v
);
3555 buffer
[i
] = v
& 0x0ff;
3561 retval
= target_write_memory(target
, addr
, width
, count
, buffer
);
3562 if (retval
!= ERROR_OK
) {
3564 LOG_ERROR("array2mem: Write @ 0x%08x, w=%d, cnt=%d, failed",
3568 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3569 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "array2mem: cannot read memory", NULL
);
3577 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3582 void target_all_handle_event(enum target_event e
)
3584 struct target
*target
;
3586 LOG_DEBUG("**all*targets: event: %d, %s",
3588 Jim_Nvp_value2name_simple(nvp_target_event
, e
)->name
);
3590 target
= all_targets
;
3592 target_handle_event(target
, e
);
3593 target
= target
->next
;
3598 /* FIX? should we propagate errors here rather than printing them
3601 void target_handle_event(struct target
*target
, enum target_event e
)
3603 struct target_event_action
*teap
;
3605 for (teap
= target
->event_action
; teap
!= NULL
; teap
= teap
->next
) {
3606 if (teap
->event
== e
) {
3607 LOG_DEBUG("target: (%d) %s (%s) event: %d (%s) action: %s",
3608 target
->target_number
,
3609 target_name(target
),
3610 target_type_name(target
),
3612 Jim_Nvp_value2name_simple(nvp_target_event
, e
)->name
,
3613 Jim_GetString(teap
->body
, NULL
));
3614 if (Jim_EvalObj(teap
->interp
, teap
->body
) != JIM_OK
)
3616 Jim_PrintErrorMessage(teap
->interp
);
3623 * Returns true only if the target has a handler for the specified event.
3625 bool target_has_event_action(struct target
*target
, enum target_event event
)
3627 struct target_event_action
*teap
;
3629 for (teap
= target
->event_action
; teap
!= NULL
; teap
= teap
->next
) {
3630 if (teap
->event
== event
)
3636 enum target_cfg_param
{
3639 TCFG_WORK_AREA_VIRT
,
3640 TCFG_WORK_AREA_PHYS
,
3641 TCFG_WORK_AREA_SIZE
,
3642 TCFG_WORK_AREA_BACKUP
,
3645 TCFG_CHAIN_POSITION
,
3648 static Jim_Nvp nvp_config_opts
[] = {
3649 { .name
= "-type", .value
= TCFG_TYPE
},
3650 { .name
= "-event", .value
= TCFG_EVENT
},
3651 { .name
= "-work-area-virt", .value
= TCFG_WORK_AREA_VIRT
},
3652 { .name
= "-work-area-phys", .value
= TCFG_WORK_AREA_PHYS
},
3653 { .name
= "-work-area-size", .value
= TCFG_WORK_AREA_SIZE
},
3654 { .name
= "-work-area-backup", .value
= TCFG_WORK_AREA_BACKUP
},
3655 { .name
= "-endian" , .value
= TCFG_ENDIAN
},
3656 { .name
= "-variant", .value
= TCFG_VARIANT
},
3657 { .name
= "-chain-position", .value
= TCFG_CHAIN_POSITION
},
3659 { .name
= NULL
, .value
= -1 }
3662 static int target_configure(Jim_GetOptInfo
*goi
, struct target
*target
)
3670 /* parse config or cget options ... */
3671 while (goi
->argc
> 0) {
3672 Jim_SetEmptyResult(goi
->interp
);
3673 /* Jim_GetOpt_Debug(goi); */
3675 if (target
->type
->target_jim_configure
) {
3676 /* target defines a configure function */
3677 /* target gets first dibs on parameters */
3678 e
= (*(target
->type
->target_jim_configure
))(target
, goi
);
3687 /* otherwise we 'continue' below */
3689 e
= Jim_GetOpt_Nvp(goi
, nvp_config_opts
, &n
);
3691 Jim_GetOpt_NvpUnknown(goi
, nvp_config_opts
, 0);
3697 if (goi
->isconfigure
) {
3698 Jim_SetResult_sprintf(goi
->interp
,
3699 "not settable: %s", n
->name
);
3703 if (goi
->argc
!= 0) {
3704 Jim_WrongNumArgs(goi
->interp
,
3705 goi
->argc
, goi
->argv
,
3710 Jim_SetResultString(goi
->interp
,
3711 target_type_name(target
), -1);
3715 if (goi
->argc
== 0) {
3716 Jim_WrongNumArgs(goi
->interp
, goi
->argc
, goi
->argv
, "-event ?event-name? ...");
3720 e
= Jim_GetOpt_Nvp(goi
, nvp_target_event
, &n
);
3722 Jim_GetOpt_NvpUnknown(goi
, nvp_target_event
, 1);
3726 if (goi
->isconfigure
) {
3727 if (goi
->argc
!= 1) {
3728 Jim_WrongNumArgs(goi
->interp
, goi
->argc
, goi
->argv
, "-event ?event-name? ?EVENT-BODY?");
3732 if (goi
->argc
!= 0) {
3733 Jim_WrongNumArgs(goi
->interp
, goi
->argc
, goi
->argv
, "-event ?event-name?");
3739 struct target_event_action
*teap
;
3741 teap
= target
->event_action
;
3742 /* replace existing? */
3744 if (teap
->event
== (enum target_event
)n
->value
) {
3750 if (goi
->isconfigure
) {
3751 bool replace
= true;
3754 teap
= calloc(1, sizeof(*teap
));
3757 teap
->event
= n
->value
;
3758 teap
->interp
= goi
->interp
;
3759 Jim_GetOpt_Obj(goi
, &o
);
3761 Jim_DecrRefCount(teap
->interp
, teap
->body
);
3763 teap
->body
= Jim_DuplicateObj(goi
->interp
, o
);
3766 * Tcl/TK - "tk events" have a nice feature.
3767 * See the "BIND" command.
3768 * We should support that here.
3769 * You can specify %X and %Y in the event code.
3770 * The idea is: %T - target name.
3771 * The idea is: %N - target number
3772 * The idea is: %E - event name.
3774 Jim_IncrRefCount(teap
->body
);
3778 /* add to head of event list */
3779 teap
->next
= target
->event_action
;
3780 target
->event_action
= teap
;
3782 Jim_SetEmptyResult(goi
->interp
);
3786 Jim_SetEmptyResult(goi
->interp
);
3788 Jim_SetResult(goi
->interp
, Jim_DuplicateObj(goi
->interp
, teap
->body
));
3795 case TCFG_WORK_AREA_VIRT
:
3796 if (goi
->isconfigure
) {
3797 target_free_all_working_areas(target
);
3798 e
= Jim_GetOpt_Wide(goi
, &w
);
3802 target
->working_area_virt
= w
;
3803 target
->working_area_virt_spec
= true;
3805 if (goi
->argc
!= 0) {
3809 Jim_SetResult(goi
->interp
, Jim_NewIntObj(goi
->interp
, target
->working_area_virt
));
3813 case TCFG_WORK_AREA_PHYS
:
3814 if (goi
->isconfigure
) {
3815 target_free_all_working_areas(target
);
3816 e
= Jim_GetOpt_Wide(goi
, &w
);
3820 target
->working_area_phys
= w
;
3821 target
->working_area_phys_spec
= true;
3823 if (goi
->argc
!= 0) {
3827 Jim_SetResult(goi
->interp
, Jim_NewIntObj(goi
->interp
, target
->working_area_phys
));
3831 case TCFG_WORK_AREA_SIZE
:
3832 if (goi
->isconfigure
) {
3833 target_free_all_working_areas(target
);
3834 e
= Jim_GetOpt_Wide(goi
, &w
);
3838 target
->working_area_size
= w
;
3840 if (goi
->argc
!= 0) {
3844 Jim_SetResult(goi
->interp
, Jim_NewIntObj(goi
->interp
, target
->working_area_size
));
3848 case TCFG_WORK_AREA_BACKUP
:
3849 if (goi
->isconfigure
) {
3850 target_free_all_working_areas(target
);
3851 e
= Jim_GetOpt_Wide(goi
, &w
);
3855 /* make this exactly 1 or 0 */
3856 target
->backup_working_area
= (!!w
);
3858 if (goi
->argc
!= 0) {
3862 Jim_SetResult(goi
->interp
, Jim_NewIntObj(goi
->interp
, target
->backup_working_area
));
3863 /* loop for more e*/
3867 if (goi
->isconfigure
) {
3868 e
= Jim_GetOpt_Nvp(goi
, nvp_target_endian
, &n
);
3870 Jim_GetOpt_NvpUnknown(goi
, nvp_target_endian
, 1);
3873 target
->endianness
= n
->value
;
3875 if (goi
->argc
!= 0) {
3879 n
= Jim_Nvp_value2name_simple(nvp_target_endian
, target
->endianness
);
3880 if (n
->name
== NULL
) {
3881 target
->endianness
= TARGET_LITTLE_ENDIAN
;
3882 n
= Jim_Nvp_value2name_simple(nvp_target_endian
, target
->endianness
);
3884 Jim_SetResultString(goi
->interp
, n
->name
, -1);
3889 if (goi
->isconfigure
) {
3890 if (goi
->argc
< 1) {
3891 Jim_SetResult_sprintf(goi
->interp
,
3896 if (target
->variant
) {
3897 free((void *)(target
->variant
));
3899 e
= Jim_GetOpt_String(goi
, &cp
, NULL
);
3900 target
->variant
= strdup(cp
);
3902 if (goi
->argc
!= 0) {
3906 Jim_SetResultString(goi
->interp
, target
->variant
,-1);
3909 case TCFG_CHAIN_POSITION
:
3910 if (goi
->isconfigure
) {
3912 struct jtag_tap
*tap
;
3913 target_free_all_working_areas(target
);
3914 e
= Jim_GetOpt_Obj(goi
, &o
);
3918 tap
= jtag_tap_by_jim_obj(goi
->interp
, o
);
3922 /* make this exactly 1 or 0 */
3925 if (goi
->argc
!= 0) {
3929 Jim_SetResultString(goi
->interp
, target
->tap
->dotted_name
, -1);
3930 /* loop for more e*/
3933 } /* while (goi->argc) */
3936 /* done - we return */
3941 jim_target_configure(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
3945 Jim_GetOpt_Setup(&goi
, interp
, argc
- 1, argv
+ 1);
3946 goi
.isconfigure
= !strcmp(Jim_GetString(argv
[0], NULL
), "configure");
3947 int need_args
= 1 + goi
.isconfigure
;
3948 if (goi
.argc
< need_args
)
3950 Jim_WrongNumArgs(goi
.interp
, goi
.argc
, goi
.argv
,
3952 ? "missing: -option VALUE ..."
3953 : "missing: -option ...");
3956 struct target
*target
= Jim_CmdPrivData(goi
.interp
);
3957 return target_configure(&goi
, target
);
3960 static int jim_target_mw(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
3962 const char *cmd_name
= Jim_GetString(argv
[0], NULL
);
3965 Jim_GetOpt_Setup(&goi
, interp
, argc
- 1, argv
+ 1);
3967 /* danger! goi.argc will be modified below! */
3970 if (argc
!= 2 && argc
!= 3)
3972 Jim_SetResult_sprintf(goi
.interp
,
3973 "usage: %s <address> <data> [<count>]", cmd_name
);
3979 int e
= Jim_GetOpt_Wide(&goi
, &a
);
3984 e
= Jim_GetOpt_Wide(&goi
, &b
);
3991 e
= Jim_GetOpt_Wide(&goi
, &c
);
3996 struct target
*target
= Jim_CmdPrivData(goi
.interp
);
3998 if (strcasecmp(cmd_name
, "mww") == 0) {
4001 else if (strcasecmp(cmd_name
, "mwh") == 0) {
4004 else if (strcasecmp(cmd_name
, "mwb") == 0) {
4007 LOG_ERROR("command '%s' unknown: ", cmd_name
);
4011 return (target_fill_mem(target
, a
, target_write_memory_fast
, data_size
, b
, c
) == ERROR_OK
) ? JIM_OK
: JIM_ERR
;
4014 static int jim_target_md(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4016 const char *cmd_name
= Jim_GetString(argv
[0], NULL
);
4019 Jim_GetOpt_Setup(&goi
, interp
, argc
- 1, argv
+ 1);
4021 /* danger! goi.argc will be modified below! */
4024 if ((argc
!= 1) && (argc
!= 2))
4026 Jim_SetResult_sprintf(goi
.interp
,
4027 "usage: %s <address> [<count>]", cmd_name
);
4032 int e
= Jim_GetOpt_Wide(&goi
, &a
);
4038 e
= Jim_GetOpt_Wide(&goi
, &c
);
4045 jim_wide b
= 1; /* shut up gcc */
4046 if (strcasecmp(cmd_name
, "mdw") == 0)
4048 else if (strcasecmp(cmd_name
, "mdh") == 0)
4050 else if (strcasecmp(cmd_name
, "mdb") == 0)
4053 LOG_ERROR("command '%s' unknown: ", cmd_name
);
4057 /* convert count to "bytes" */
4060 struct target
*target
= Jim_CmdPrivData(goi
.interp
);
4061 uint8_t target_buf
[32];
4068 e
= target_read_memory(target
, a
, b
, y
/ b
, target_buf
);
4069 if (e
!= ERROR_OK
) {
4070 Jim_SetResult_sprintf(interp
, "error reading target @ 0x%08lx", (int)(a
));
4074 Jim_fprintf(interp
, interp
->cookie_stdout
, "0x%08x ", (int)(a
));
4077 for (x
= 0; x
< 16 && x
< y
; x
+= 4)
4079 z
= target_buffer_get_u32(target
, &(target_buf
[ x
]));
4080 Jim_fprintf(interp
, interp
->cookie_stdout
, "%08x ", (int)(z
));
4082 for (; (x
< 16) ; x
+= 4) {
4083 Jim_fprintf(interp
, interp
->cookie_stdout
, " ");
4087 for (x
= 0; x
< 16 && x
< y
; x
+= 2)
4089 z
= target_buffer_get_u16(target
, &(target_buf
[ x
]));
4090 Jim_fprintf(interp
, interp
->cookie_stdout
, "%04x ", (int)(z
));
4092 for (; (x
< 16) ; x
+= 2) {
4093 Jim_fprintf(interp
, interp
->cookie_stdout
, " ");
4098 for (x
= 0 ; (x
< 16) && (x
< y
) ; x
+= 1) {
4099 z
= target_buffer_get_u8(target
, &(target_buf
[ x
]));
4100 Jim_fprintf(interp
, interp
->cookie_stdout
, "%02x ", (int)(z
));
4102 for (; (x
< 16) ; x
+= 1) {
4103 Jim_fprintf(interp
, interp
->cookie_stdout
, " ");
4107 /* ascii-ify the bytes */
4108 for (x
= 0 ; x
< y
; x
++) {
4109 if ((target_buf
[x
] >= 0x20) &&
4110 (target_buf
[x
] <= 0x7e)) {
4114 target_buf
[x
] = '.';
4119 target_buf
[x
] = ' ';
4124 /* print - with a newline */
4125 Jim_fprintf(interp
, interp
->cookie_stdout
, "%s\n", target_buf
);
4133 static int jim_target_mem2array(Jim_Interp
*interp
,
4134 int argc
, Jim_Obj
*const *argv
)
4136 struct target
*target
= Jim_CmdPrivData(interp
);
4137 return target_mem2array(interp
, target
, argc
- 1, argv
+ 1);
4140 static int jim_target_array2mem(Jim_Interp
*interp
,
4141 int argc
, Jim_Obj
*const *argv
)
4143 struct target
*target
= Jim_CmdPrivData(interp
);
4144 return target_array2mem(interp
, target
, argc
- 1, argv
+ 1);
4147 static int jim_target_tap_disabled(Jim_Interp
*interp
)
4149 Jim_SetResult_sprintf(interp
, "[TAP is disabled]");
4153 static int jim_target_examine(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4157 Jim_WrongNumArgs(interp
, 1, argv
, "[no parameters]");
4160 struct target
*target
= Jim_CmdPrivData(interp
);
4161 if (!target
->tap
->enabled
)
4162 return jim_target_tap_disabled(interp
);
4164 int e
= target
->type
->examine(target
);
4167 Jim_SetResult_sprintf(interp
, "examine-fails: %d", e
);
4173 static int jim_target_halt_gdb(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4177 Jim_WrongNumArgs(interp
, 1, argv
, "[no parameters]");
4180 struct target
*target
= Jim_CmdPrivData(interp
);
4182 if (target_call_event_callbacks(target
, TARGET_EVENT_GDB_HALT
) != ERROR_OK
)
4188 static int jim_target_poll(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4192 Jim_WrongNumArgs(interp
, 1, argv
, "[no parameters]");
4195 struct target
*target
= Jim_CmdPrivData(interp
);
4196 if (!target
->tap
->enabled
)
4197 return jim_target_tap_disabled(interp
);
4200 if (!(target_was_examined(target
))) {
4201 e
= ERROR_TARGET_NOT_EXAMINED
;
4203 e
= target
->type
->poll(target
);
4207 Jim_SetResult_sprintf(interp
, "poll-fails: %d", e
);
4213 static int jim_target_reset(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4216 Jim_GetOpt_Setup(&goi
, interp
, argc
- 1, argv
+ 1);
4220 Jim_WrongNumArgs(interp
, 0, argv
,
4221 "([tT]|[fF]|assert|deassert) BOOL");
4226 int e
= Jim_GetOpt_Nvp(&goi
, nvp_assert
, &n
);
4229 Jim_GetOpt_NvpUnknown(&goi
, nvp_assert
, 1);
4232 /* the halt or not param */
4234 e
= Jim_GetOpt_Wide(&goi
, &a
);
4238 struct target
*target
= Jim_CmdPrivData(goi
.interp
);
4239 if (!target
->tap
->enabled
)
4240 return jim_target_tap_disabled(interp
);
4241 if (!(target_was_examined(target
)))
4243 LOG_ERROR("Target not examined yet");
4244 return ERROR_TARGET_NOT_EXAMINED
;
4246 if (!target
->type
->assert_reset
|| !target
->type
->deassert_reset
)
4248 Jim_SetResult_sprintf(interp
,
4249 "No target-specific reset for %s",
4250 target_name(target
));
4253 /* determine if we should halt or not. */
4254 target
->reset_halt
= !!a
;
4255 /* When this happens - all workareas are invalid. */
4256 target_free_all_working_areas_restore(target
, 0);
4259 if (n
->value
== NVP_ASSERT
) {
4260 e
= target
->type
->assert_reset(target
);
4262 e
= target
->type
->deassert_reset(target
);
4264 return (e
== ERROR_OK
) ? JIM_OK
: JIM_ERR
;
4267 static int jim_target_halt(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4270 Jim_WrongNumArgs(interp
, 1, argv
, "[no parameters]");
4273 struct target
*target
= Jim_CmdPrivData(interp
);
4274 if (!target
->tap
->enabled
)
4275 return jim_target_tap_disabled(interp
);
4276 int e
= target
->type
->halt(target
);
4277 return (e
== ERROR_OK
) ? JIM_OK
: JIM_ERR
;
4280 static int jim_target_wait_state(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4283 Jim_GetOpt_Setup(&goi
, interp
, argc
- 1, argv
+ 1);
4285 /* params: <name> statename timeoutmsecs */
4288 const char *cmd_name
= Jim_GetString(argv
[0], NULL
);
4289 Jim_SetResult_sprintf(goi
.interp
,
4290 "%s <state_name> <timeout_in_msec>", cmd_name
);
4295 int e
= Jim_GetOpt_Nvp(&goi
, nvp_target_state
, &n
);
4297 Jim_GetOpt_NvpUnknown(&goi
, nvp_target_state
,1);
4301 e
= Jim_GetOpt_Wide(&goi
, &a
);
4305 struct target
*target
= Jim_CmdPrivData(interp
);
4306 if (!target
->tap
->enabled
)
4307 return jim_target_tap_disabled(interp
);
4309 e
= target_wait_state(target
, n
->value
, a
);
4312 Jim_SetResult_sprintf(goi
.interp
,
4313 "target: %s wait %s fails (%d) %s",
4314 target_name(target
), n
->name
,
4315 e
, target_strerror_safe(e
));
4320 /* List for human, Events defined for this target.
4321 * scripts/programs should use 'name cget -event NAME'
4323 static int jim_target_event_list(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4325 struct command_context
*cmd_ctx
= Jim_GetAssocData(interp
, "context");
4326 struct target
*target
= Jim_CmdPrivData(interp
);
4327 struct target_event_action
*teap
= target
->event_action
;
4328 command_print(cmd_ctx
, "Event actions for target (%d) %s\n",
4329 target
->target_number
,
4330 target_name(target
));
4331 command_print(cmd_ctx
, "%-25s | Body", "Event");
4332 command_print(cmd_ctx
, "------------------------- | "
4333 "----------------------------------------");
4336 Jim_Nvp
*opt
= Jim_Nvp_value2name_simple(nvp_target_event
, teap
->event
);
4337 command_print(cmd_ctx
, "%-25s | %s",
4338 opt
->name
, Jim_GetString(teap
->body
, NULL
));
4341 command_print(cmd_ctx
, "***END***");
4344 static int jim_target_current_state(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4348 Jim_WrongNumArgs(interp
, 1, argv
, "[no parameters]");
4351 struct target
*target
= Jim_CmdPrivData(interp
);
4352 Jim_SetResultString(interp
, target_state_name(target
), -1);
4355 static int jim_target_invoke_event(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4358 Jim_GetOpt_Setup(&goi
, interp
, argc
- 1, argv
+ 1);
4361 const char *cmd_name
= Jim_GetString(argv
[0], NULL
);
4362 Jim_SetResult_sprintf(goi
.interp
, "%s <eventname>", cmd_name
);
4366 int e
= Jim_GetOpt_Nvp(&goi
, nvp_target_event
, &n
);
4369 Jim_GetOpt_NvpUnknown(&goi
, nvp_target_event
, 1);
4372 struct target
*target
= Jim_CmdPrivData(interp
);
4373 target_handle_event(target
, n
->value
);
4377 static const struct command_registration target_instance_command_handlers
[] = {
4379 .name
= "configure",
4380 .mode
= COMMAND_CONFIG
,
4381 .jim_handler
= jim_target_configure
,
4382 .help
= "configure a new target for use",
4383 .usage
= "[target_attribute ...]",
4387 .mode
= COMMAND_ANY
,
4388 .jim_handler
= jim_target_configure
,
4389 .help
= "returns the specified target attribute",
4390 .usage
= "target_attribute",
4394 .mode
= COMMAND_EXEC
,
4395 .jim_handler
= jim_target_mw
,
4396 .help
= "Write 32-bit word(s) to target memory",
4397 .usage
= "address data [count]",
4401 .mode
= COMMAND_EXEC
,
4402 .jim_handler
= jim_target_mw
,
4403 .help
= "Write 16-bit half-word(s) to target memory",
4404 .usage
= "address data [count]",
4408 .mode
= COMMAND_EXEC
,
4409 .jim_handler
= jim_target_mw
,
4410 .help
= "Write byte(s) to target memory",
4411 .usage
= "address data [count]",
4415 .mode
= COMMAND_EXEC
,
4416 .jim_handler
= jim_target_md
,
4417 .help
= "Display target memory as 32-bit words",
4418 .usage
= "address [count]",
4422 .mode
= COMMAND_EXEC
,
4423 .jim_handler
= jim_target_md
,
4424 .help
= "Display target memory as 16-bit half-words",
4425 .usage
= "address [count]",
4429 .mode
= COMMAND_EXEC
,
4430 .jim_handler
= jim_target_md
,
4431 .help
= "Display target memory as 8-bit bytes",
4432 .usage
= "address [count]",
4435 .name
= "array2mem",
4436 .mode
= COMMAND_EXEC
,
4437 .jim_handler
= jim_target_array2mem
,
4438 .help
= "Writes Tcl array of 8/16/32 bit numbers "
4440 .usage
= "arrayname bitwidth address count",
4443 .name
= "mem2array",
4444 .mode
= COMMAND_EXEC
,
4445 .jim_handler
= jim_target_mem2array
,
4446 .help
= "Loads Tcl array of 8/16/32 bit numbers "
4447 "from target memory",
4448 .usage
= "arrayname bitwidth address count",
4451 .name
= "eventlist",
4452 .mode
= COMMAND_EXEC
,
4453 .jim_handler
= jim_target_event_list
,
4454 .help
= "displays a table of events defined for this target",
4458 .mode
= COMMAND_EXEC
,
4459 .jim_handler
= jim_target_current_state
,
4460 .help
= "displays the current state of this target",
4463 .name
= "arp_examine",
4464 .mode
= COMMAND_EXEC
,
4465 .jim_handler
= jim_target_examine
,
4466 .help
= "used internally for reset processing",
4469 .name
= "arp_halt_gdb",
4470 .mode
= COMMAND_EXEC
,
4471 .jim_handler
= jim_target_halt_gdb
,
4472 .help
= "used internally for reset processing to halt GDB",
4476 .mode
= COMMAND_EXEC
,
4477 .jim_handler
= jim_target_poll
,
4478 .help
= "used internally for reset processing",
4481 .name
= "arp_reset",
4482 .mode
= COMMAND_EXEC
,
4483 .jim_handler
= jim_target_reset
,
4484 .help
= "used internally for reset processing",
4488 .mode
= COMMAND_EXEC
,
4489 .jim_handler
= jim_target_halt
,
4490 .help
= "used internally for reset processing",
4493 .name
= "arp_waitstate",
4494 .mode
= COMMAND_EXEC
,
4495 .jim_handler
= jim_target_wait_state
,
4496 .help
= "used internally for reset processing",
4499 .name
= "invoke-event",
4500 .mode
= COMMAND_EXEC
,
4501 .jim_handler
= jim_target_invoke_event
,
4502 .help
= "invoke handler for specified event",
4503 .usage
= "event_name",
4505 COMMAND_REGISTRATION_DONE
4508 static int target_create(Jim_GetOptInfo
*goi
)
4516 struct target
*target
;
4517 struct command_context
*cmd_ctx
;
4519 cmd_ctx
= Jim_GetAssocData(goi
->interp
, "context");
4520 if (goi
->argc
< 3) {
4521 Jim_WrongNumArgs(goi
->interp
, 1, goi
->argv
, "?name? ?type? ..options...");
4526 Jim_GetOpt_Obj(goi
, &new_cmd
);
4527 /* does this command exist? */
4528 cmd
= Jim_GetCommand(goi
->interp
, new_cmd
, JIM_ERRMSG
);
4530 cp
= Jim_GetString(new_cmd
, NULL
);
4531 Jim_SetResult_sprintf(goi
->interp
, "Command/target: %s Exists", cp
);
4536 e
= Jim_GetOpt_String(goi
, &cp2
, NULL
);
4538 /* now does target type exist */
4539 for (x
= 0 ; target_types
[x
] ; x
++) {
4540 if (0 == strcmp(cp
, target_types
[x
]->name
)) {
4545 if (target_types
[x
] == NULL
) {
4546 Jim_SetResult_sprintf(goi
->interp
, "Unknown target type %s, try one of ", cp
);
4547 for (x
= 0 ; target_types
[x
] ; x
++) {
4548 if (target_types
[x
+ 1]) {
4549 Jim_AppendStrings(goi
->interp
,
4550 Jim_GetResult(goi
->interp
),
4551 target_types
[x
]->name
,
4554 Jim_AppendStrings(goi
->interp
,
4555 Jim_GetResult(goi
->interp
),
4557 target_types
[x
]->name
,NULL
);
4564 target
= calloc(1,sizeof(struct target
));
4565 /* set target number */
4566 target
->target_number
= new_target_number();
4568 /* allocate memory for each unique target type */
4569 target
->type
= (struct target_type
*)calloc(1,sizeof(struct target_type
));
4571 memcpy(target
->type
, target_types
[x
], sizeof(struct target_type
));
4573 /* will be set by "-endian" */
4574 target
->endianness
= TARGET_ENDIAN_UNKNOWN
;
4576 target
->working_area
= 0x0;
4577 target
->working_area_size
= 0x0;
4578 target
->working_areas
= NULL
;
4579 target
->backup_working_area
= 0;
4581 target
->state
= TARGET_UNKNOWN
;
4582 target
->debug_reason
= DBG_REASON_UNDEFINED
;
4583 target
->reg_cache
= NULL
;
4584 target
->breakpoints
= NULL
;
4585 target
->watchpoints
= NULL
;
4586 target
->next
= NULL
;
4587 target
->arch_info
= NULL
;
4589 target
->display
= 1;
4591 target
->halt_issued
= false;
4593 /* initialize trace information */
4594 target
->trace_info
= malloc(sizeof(struct trace
));
4595 target
->trace_info
->num_trace_points
= 0;
4596 target
->trace_info
->trace_points_size
= 0;
4597 target
->trace_info
->trace_points
= NULL
;
4598 target
->trace_info
->trace_history_size
= 0;
4599 target
->trace_info
->trace_history
= NULL
;
4600 target
->trace_info
->trace_history_pos
= 0;
4601 target
->trace_info
->trace_history_overflowed
= 0;
4603 target
->dbgmsg
= NULL
;
4604 target
->dbg_msg_enabled
= 0;
4606 target
->endianness
= TARGET_ENDIAN_UNKNOWN
;
4608 /* Do the rest as "configure" options */
4609 goi
->isconfigure
= 1;
4610 e
= target_configure(goi
, target
);
4612 if (target
->tap
== NULL
)
4614 Jim_SetResultString(goi
->interp
, "-chain-position required when creating target", -1);
4624 if (target
->endianness
== TARGET_ENDIAN_UNKNOWN
) {
4625 /* default endian to little if not specified */
4626 target
->endianness
= TARGET_LITTLE_ENDIAN
;
4629 /* incase variant is not set */
4630 if (!target
->variant
)
4631 target
->variant
= strdup("");
4633 cp
= Jim_GetString(new_cmd
, NULL
);
4634 target
->cmd_name
= strdup(cp
);
4636 /* create the target specific commands */
4637 if (target
->type
->commands
) {
4638 e
= register_commands(cmd_ctx
, NULL
, target
->type
->commands
);
4640 LOG_ERROR("unable to register '%s' commands", cp
);
4642 if (target
->type
->target_create
) {
4643 (*(target
->type
->target_create
))(target
, goi
->interp
);
4646 /* append to end of list */
4648 struct target
**tpp
;
4649 tpp
= &(all_targets
);
4651 tpp
= &((*tpp
)->next
);
4656 /* now - create the new target name command */
4657 const const struct command_registration target_subcommands
[] = {
4659 .chain
= target_instance_command_handlers
,
4662 .chain
= target
->type
->commands
,
4664 COMMAND_REGISTRATION_DONE
4666 const const struct command_registration target_commands
[] = {
4669 .mode
= COMMAND_ANY
,
4670 .help
= "target command group",
4671 .chain
= target_subcommands
,
4673 COMMAND_REGISTRATION_DONE
4675 e
= register_commands(cmd_ctx
, NULL
, target_commands
);
4679 struct command
*c
= command_find_in_context(cmd_ctx
, cp
);
4681 command_set_handler_data(c
, target
);
4683 return (ERROR_OK
== e
) ? JIM_OK
: JIM_ERR
;
4686 static int jim_target_current(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4690 Jim_WrongNumArgs(interp
, 1, argv
, "Too many parameters");
4693 struct command_context
*cmd_ctx
= Jim_GetAssocData(interp
, "context");
4694 Jim_SetResultString(interp
, get_current_target(cmd_ctx
)->cmd_name
, -1);
4698 static int jim_target_types(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4702 Jim_WrongNumArgs(interp
, 1, argv
, "Too many parameters");
4705 Jim_SetResult(interp
, Jim_NewListObj(interp
, NULL
, 0));
4706 for (unsigned x
= 0; NULL
!= target_types
[x
]; x
++)
4708 Jim_ListAppendElement(interp
, Jim_GetResult(interp
),
4709 Jim_NewStringObj(interp
, target_types
[x
]->name
, -1));
4714 static int jim_target_names(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4718 Jim_WrongNumArgs(interp
, 1, argv
, "Too many parameters");
4721 Jim_SetResult(interp
, Jim_NewListObj(interp
, NULL
, 0));
4722 struct target
*target
= all_targets
;
4725 Jim_ListAppendElement(interp
, Jim_GetResult(interp
),
4726 Jim_NewStringObj(interp
, target_name(target
), -1));
4727 target
= target
->next
;
4732 static int jim_target_create(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4735 Jim_GetOpt_Setup(&goi
, interp
, argc
- 1, argv
+ 1);
4738 Jim_WrongNumArgs(goi
.interp
, goi
.argc
, goi
.argv
,
4739 "<name> <target_type> [<target_options> ...]");
4742 return target_create(&goi
);
4745 static int jim_target_number(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4748 Jim_GetOpt_Setup(&goi
, interp
, argc
- 1, argv
+ 1);
4750 /* It's OK to remove this mechanism sometime after August 2010 or so */
4751 LOG_WARNING("don't use numbers as target identifiers; use names");
4754 Jim_SetResult_sprintf(goi
.interp
, "usage: target number <number>");
4758 int e
= Jim_GetOpt_Wide(&goi
, &w
);
4762 struct target
*target
;
4763 for (target
= all_targets
; NULL
!= target
; target
= target
->next
)
4765 if (target
->target_number
!= w
)
4768 Jim_SetResultString(goi
.interp
, target_name(target
), -1);
4771 Jim_SetResult_sprintf(goi
.interp
,
4772 "Target: number %d does not exist", (int)(w
));
4776 static int jim_target_count(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4780 Jim_WrongNumArgs(interp
, 1, argv
, "<no parameters>");
4784 struct target
*target
= all_targets
;
4785 while (NULL
!= target
)
4787 target
= target
->next
;
4790 Jim_SetResult(interp
, Jim_NewIntObj(interp
, count
));
4794 static const struct command_registration target_subcommand_handlers
[] = {
4797 .mode
= COMMAND_CONFIG
,
4798 .handler
= handle_target_init_command
,
4799 .help
= "initialize targets",
4803 /* REVISIT this should be COMMAND_CONFIG ... */
4804 .mode
= COMMAND_ANY
,
4805 .jim_handler
= jim_target_create
,
4806 .usage
= "name type '-chain-position' name [options ...]",
4807 .help
= "Creates and selects a new target",
4811 .mode
= COMMAND_ANY
,
4812 .jim_handler
= jim_target_current
,
4813 .help
= "Returns the currently selected target",
4817 .mode
= COMMAND_ANY
,
4818 .jim_handler
= jim_target_types
,
4819 .help
= "Returns the available target types as "
4820 "a list of strings",
4824 .mode
= COMMAND_ANY
,
4825 .jim_handler
= jim_target_names
,
4826 .help
= "Returns the names of all targets as a list of strings",
4830 .mode
= COMMAND_ANY
,
4831 .jim_handler
= jim_target_number
,
4833 .help
= "Returns the name of the numbered target "
4838 .mode
= COMMAND_ANY
,
4839 .jim_handler
= jim_target_count
,
4840 .help
= "Returns the number of targets as an integer "
4843 COMMAND_REGISTRATION_DONE
4854 static int fastload_num
;
4855 static struct FastLoad
*fastload
;
4857 static void free_fastload(void)
4859 if (fastload
!= NULL
)
4862 for (i
= 0; i
< fastload_num
; i
++)
4864 if (fastload
[i
].data
)
4865 free(fastload
[i
].data
);
4875 COMMAND_HANDLER(handle_fast_load_image_command
)
4879 uint32_t image_size
;
4880 uint32_t min_address
= 0;
4881 uint32_t max_address
= 0xffffffff;
4886 int retval
= CALL_COMMAND_HANDLER(parse_load_image_command_CMD_ARGV
,
4887 &image
, &min_address
, &max_address
);
4888 if (ERROR_OK
!= retval
)
4891 struct duration bench
;
4892 duration_start(&bench
);
4894 if (image_open(&image
, CMD_ARGV
[0], (CMD_ARGC
>= 3) ? CMD_ARGV
[2] : NULL
) != ERROR_OK
)
4901 fastload_num
= image
.num_sections
;
4902 fastload
= (struct FastLoad
*)malloc(sizeof(struct FastLoad
)*image
.num_sections
);
4903 if (fastload
== NULL
)
4905 image_close(&image
);
4908 memset(fastload
, 0, sizeof(struct FastLoad
)*image
.num_sections
);
4909 for (i
= 0; i
< image
.num_sections
; i
++)
4911 buffer
= malloc(image
.sections
[i
].size
);
4914 command_print(CMD_CTX
, "error allocating buffer for section (%d bytes)",
4915 (int)(image
.sections
[i
].size
));
4919 if ((retval
= image_read_section(&image
, i
, 0x0, image
.sections
[i
].size
, buffer
, &buf_cnt
)) != ERROR_OK
)
4925 uint32_t offset
= 0;
4926 uint32_t length
= buf_cnt
;
4929 /* DANGER!!! beware of unsigned comparision here!!! */
4931 if ((image
.sections
[i
].base_address
+ buf_cnt
>= min_address
)&&
4932 (image
.sections
[i
].base_address
< max_address
))
4934 if (image
.sections
[i
].base_address
< min_address
)
4936 /* clip addresses below */
4937 offset
+= min_address
-image
.sections
[i
].base_address
;
4941 if (image
.sections
[i
].base_address
+ buf_cnt
> max_address
)
4943 length
-= (image
.sections
[i
].base_address
+ buf_cnt
)-max_address
;
4946 fastload
[i
].address
= image
.sections
[i
].base_address
+ offset
;
4947 fastload
[i
].data
= malloc(length
);
4948 if (fastload
[i
].data
== NULL
)
4953 memcpy(fastload
[i
].data
, buffer
+ offset
, length
);
4954 fastload
[i
].length
= length
;
4956 image_size
+= length
;
4957 command_print(CMD_CTX
, "%u bytes written at address 0x%8.8x",
4958 (unsigned int)length
,
4959 ((unsigned int)(image
.sections
[i
].base_address
+ offset
)));
4965 if ((ERROR_OK
== retval
) && (duration_measure(&bench
) == ERROR_OK
))
4967 command_print(CMD_CTX
, "Loaded %" PRIu32
" bytes "
4968 "in %fs (%0.3f kb/s)", image_size
,
4969 duration_elapsed(&bench
), duration_kbps(&bench
, image_size
));
4971 command_print(CMD_CTX
,
4972 "WARNING: image has not been loaded to target!"
4973 "You can issue a 'fast_load' to finish loading.");
4976 image_close(&image
);
4978 if (retval
!= ERROR_OK
)
4986 COMMAND_HANDLER(handle_fast_load_command
)
4989 return ERROR_COMMAND_SYNTAX_ERROR
;
4990 if (fastload
== NULL
)
4992 LOG_ERROR("No image in memory");
4996 int ms
= timeval_ms();
4998 int retval
= ERROR_OK
;
4999 for (i
= 0; i
< fastload_num
;i
++)
5001 struct target
*target
= get_current_target(CMD_CTX
);
5002 command_print(CMD_CTX
, "Write to 0x%08x, length 0x%08x",
5003 (unsigned int)(fastload
[i
].address
),
5004 (unsigned int)(fastload
[i
].length
));
5005 if (retval
== ERROR_OK
)
5007 retval
= target_write_buffer(target
, fastload
[i
].address
, fastload
[i
].length
, fastload
[i
].data
);
5009 size
+= fastload
[i
].length
;
5011 int after
= timeval_ms();
5012 command_print(CMD_CTX
, "Loaded image %f kBytes/s", (float)(size
/1024.0)/((float)(after
-ms
)/1000.0));
5016 static const struct command_registration target_command_handlers
[] = {
5019 .handler
= handle_targets_command
,
5020 .mode
= COMMAND_ANY
,
5021 .help
= "change current default target (one parameter) "
5022 "or prints table of all targets (no parameters)",
5023 .usage
= "[target]",
5027 .mode
= COMMAND_CONFIG
,
5028 .help
= "configure target",
5030 .chain
= target_subcommand_handlers
,
5032 COMMAND_REGISTRATION_DONE
5035 int target_register_commands(struct command_context
*cmd_ctx
)
5037 return register_commands(cmd_ctx
, NULL
, target_command_handlers
);
5040 static bool target_reset_nag
= true;
5042 bool get_target_reset_nag(void)
5044 return target_reset_nag
;
5047 COMMAND_HANDLER(handle_target_reset_nag
)
5049 return CALL_COMMAND_HANDLER(handle_command_parse_bool
,
5050 &target_reset_nag
, "Nag after each reset about options to improve "
5054 static const struct command_registration target_exec_command_handlers
[] = {
5056 .name
= "fast_load_image",
5057 .handler
= handle_fast_load_image_command
,
5058 .mode
= COMMAND_ANY
,
5059 .help
= "Load image into server memory for later use by "
5060 "fast_load; primarily for profiling",
5061 .usage
= "filename address ['bin'|'ihex'|'elf'|'s19'] "
5062 "[min_address [max_length]]",
5065 .name
= "fast_load",
5066 .handler
= handle_fast_load_command
,
5067 .mode
= COMMAND_EXEC
,
5068 .help
= "loads active fast load image to current target "
5069 "- mainly for profiling purposes",
5073 .handler
= handle_profile_command
,
5074 .mode
= COMMAND_EXEC
,
5075 .help
= "profiling samples the CPU PC",
5077 /** @todo don't register virt2phys() unless target supports it */
5079 .name
= "virt2phys",
5080 .handler
= handle_virt2phys_command
,
5081 .mode
= COMMAND_ANY
,
5082 .help
= "translate a virtual address into a physical address",
5083 .usage
= "virtual_address",
5087 .handler
= handle_reg_command
,
5088 .mode
= COMMAND_EXEC
,
5089 .help
= "display or set a register; with no arguments, "
5090 "displays all registers and their values",
5091 .usage
= "[(register_name|register_number) [value]]",
5095 .handler
= handle_poll_command
,
5096 .mode
= COMMAND_EXEC
,
5097 .help
= "poll target state; or reconfigure background polling",
5098 .usage
= "['on'|'off']",
5101 .name
= "wait_halt",
5102 .handler
= handle_wait_halt_command
,
5103 .mode
= COMMAND_EXEC
,
5104 .help
= "wait up to the specified number of milliseconds "
5105 "(default 5) for a previously requested halt",
5106 .usage
= "[milliseconds]",
5110 .handler
= handle_halt_command
,
5111 .mode
= COMMAND_EXEC
,
5112 .help
= "request target to halt, then wait up to the specified"
5113 "number of milliseconds (default 5) for it to complete",
5114 .usage
= "[milliseconds]",
5118 .handler
= handle_resume_command
,
5119 .mode
= COMMAND_EXEC
,
5120 .help
= "resume target execution from current PC or address",
5121 .usage
= "[address]",
5125 .handler
= handle_reset_command
,
5126 .mode
= COMMAND_EXEC
,
5127 .usage
= "[run|halt|init]",
5128 .help
= "Reset all targets into the specified mode."
5129 "Default reset mode is run, if not given.",
5132 .name
= "soft_reset_halt",
5133 .handler
= handle_soft_reset_halt_command
,
5134 .mode
= COMMAND_EXEC
,
5135 .help
= "halt the target and do a soft reset",
5139 .handler
= handle_step_command
,
5140 .mode
= COMMAND_EXEC
,
5141 .help
= "step one instruction from current PC or address",
5142 .usage
= "[address]",
5146 .handler
= handle_md_command
,
5147 .mode
= COMMAND_EXEC
,
5148 .help
= "display memory words",
5149 .usage
= "['phys'] address [count]",
5153 .handler
= handle_md_command
,
5154 .mode
= COMMAND_EXEC
,
5155 .help
= "display memory half-words",
5156 .usage
= "['phys'] address [count]",
5160 .handler
= handle_md_command
,
5161 .mode
= COMMAND_EXEC
,
5162 .help
= "display memory bytes",
5163 .usage
= "['phys'] address [count]",
5167 .handler
= handle_mw_command
,
5168 .mode
= COMMAND_EXEC
,
5169 .help
= "write memory word",
5170 .usage
= "['phys'] address value [count]",
5174 .handler
= handle_mw_command
,
5175 .mode
= COMMAND_EXEC
,
5176 .help
= "write memory half-word",
5177 .usage
= "['phys'] address value [count]",
5181 .handler
= handle_mw_command
,
5182 .mode
= COMMAND_EXEC
,
5183 .help
= "write memory byte",
5184 .usage
= "['phys'] address value [count]",
5188 .handler
= handle_bp_command
,
5189 .mode
= COMMAND_EXEC
,
5190 .help
= "list or set hardware or software breakpoint",
5191 .usage
= "[address length ['hw']]",
5195 .handler
= handle_rbp_command
,
5196 .mode
= COMMAND_EXEC
,
5197 .help
= "remove breakpoint",
5202 .handler
= handle_wp_command
,
5203 .mode
= COMMAND_EXEC
,
5204 .help
= "list (no params) or create watchpoints",
5205 .usage
= "[address length [('r'|'w'|'a') value [mask]]]",
5209 .handler
= handle_rwp_command
,
5210 .mode
= COMMAND_EXEC
,
5211 .help
= "remove watchpoint",
5215 .name
= "load_image",
5216 .handler
= handle_load_image_command
,
5217 .mode
= COMMAND_EXEC
,
5218 .usage
= "filename address ['bin'|'ihex'|'elf'|'s19'] "
5219 "[min_address] [max_length]",
5222 .name
= "dump_image",
5223 .handler
= handle_dump_image_command
,
5224 .mode
= COMMAND_EXEC
,
5225 .usage
= "filename address size",
5228 .name
= "verify_image",
5229 .handler
= handle_verify_image_command
,
5230 .mode
= COMMAND_EXEC
,
5231 .usage
= "filename [offset [type]]",
5234 .name
= "test_image",
5235 .handler
= handle_test_image_command
,
5236 .mode
= COMMAND_EXEC
,
5237 .usage
= "filename [offset [type]]",
5240 .name
= "ocd_mem2array",
5241 .mode
= COMMAND_EXEC
,
5242 .jim_handler
= jim_mem2array
,
5243 .help
= "read 8/16/32 bit memory and return as a TCL array "
5244 "for script processing",
5245 .usage
= "arrayname bitwidth address count",
5248 .name
= "ocd_array2mem",
5249 .mode
= COMMAND_EXEC
,
5250 .jim_handler
= jim_array2mem
,
5251 .help
= "convert a TCL array to memory locations "
5252 "and write the 8/16/32 bit values",
5253 .usage
= "arrayname bitwidth address count",
5256 .name
= "reset_nag",
5257 .handler
= handle_target_reset_nag
,
5258 .mode
= COMMAND_ANY
,
5259 .help
= "Nag after each reset about options that could have been "
5260 "enabled to improve performance. ",
5261 .usage
= "['enable'|'disable']",
5263 COMMAND_REGISTRATION_DONE
5265 int target_register_user_commands(struct command_context
*cmd_ctx
)
5267 int retval
= ERROR_OK
;
5268 if ((retval
= target_request_register_commands(cmd_ctx
)) != ERROR_OK
)
5271 if ((retval
= trace_register_commands(cmd_ctx
)) != ERROR_OK
)
5275 return register_commands(cmd_ctx
, NULL
, target_exec_command_handlers
);