1 /***************************************************************************
2 * Copyright (C) 2005 by Dominic Rath *
3 * Dominic.Rath@gmx.de *
5 * Copyright (C) 2007-2009 Ø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 ***************************************************************************/
37 #include "target_type.h"
38 #include "target_request.h"
39 #include "breakpoints.h"
40 #include "time_support.h"
47 static int jim_mcrmrc(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
);
49 static int target_array2mem(Jim_Interp
*interp
, struct target
*target
, int argc
, Jim_Obj
*const *argv
);
50 static int target_mem2array(Jim_Interp
*interp
, struct target
*target
, int argc
, Jim_Obj
*const *argv
);
53 extern struct target_type arm7tdmi_target
;
54 extern struct target_type arm720t_target
;
55 extern struct target_type arm9tdmi_target
;
56 extern struct target_type arm920t_target
;
57 extern struct target_type arm966e_target
;
58 extern struct target_type arm926ejs_target
;
59 extern struct target_type fa526_target
;
60 extern struct target_type feroceon_target
;
61 extern struct target_type dragonite_target
;
62 extern struct target_type xscale_target
;
63 extern struct target_type cortexm3_target
;
64 extern struct target_type cortexa8_target
;
65 extern struct target_type arm11_target
;
66 extern struct target_type mips_m4k_target
;
67 extern struct target_type avr_target
;
68 extern struct target_type testee_target
;
70 struct target_type
*target_types
[] =
91 struct target
*all_targets
= NULL
;
92 struct target_event_callback
*target_event_callbacks
= NULL
;
93 struct target_timer_callback
*target_timer_callbacks
= NULL
;
95 const Jim_Nvp nvp_assert
[] = {
96 { .name
= "assert", NVP_ASSERT
},
97 { .name
= "deassert", NVP_DEASSERT
},
98 { .name
= "T", NVP_ASSERT
},
99 { .name
= "F", NVP_DEASSERT
},
100 { .name
= "t", NVP_ASSERT
},
101 { .name
= "f", NVP_DEASSERT
},
102 { .name
= NULL
, .value
= -1 }
105 const Jim_Nvp nvp_error_target
[] = {
106 { .value
= ERROR_TARGET_INVALID
, .name
= "err-invalid" },
107 { .value
= ERROR_TARGET_INIT_FAILED
, .name
= "err-init-failed" },
108 { .value
= ERROR_TARGET_TIMEOUT
, .name
= "err-timeout" },
109 { .value
= ERROR_TARGET_NOT_HALTED
, .name
= "err-not-halted" },
110 { .value
= ERROR_TARGET_FAILURE
, .name
= "err-failure" },
111 { .value
= ERROR_TARGET_UNALIGNED_ACCESS
, .name
= "err-unaligned-access" },
112 { .value
= ERROR_TARGET_DATA_ABORT
, .name
= "err-data-abort" },
113 { .value
= ERROR_TARGET_RESOURCE_NOT_AVAILABLE
, .name
= "err-resource-not-available" },
114 { .value
= ERROR_TARGET_TRANSLATION_FAULT
, .name
= "err-translation-fault" },
115 { .value
= ERROR_TARGET_NOT_RUNNING
, .name
= "err-not-running" },
116 { .value
= ERROR_TARGET_NOT_EXAMINED
, .name
= "err-not-examined" },
117 { .value
= -1, .name
= NULL
}
120 const char *target_strerror_safe(int err
)
124 n
= Jim_Nvp_value2name_simple(nvp_error_target
, err
);
125 if (n
->name
== NULL
) {
132 static const Jim_Nvp nvp_target_event
[] = {
133 { .value
= TARGET_EVENT_OLD_gdb_program_config
, .name
= "old-gdb_program_config" },
134 { .value
= TARGET_EVENT_OLD_pre_resume
, .name
= "old-pre_resume" },
136 { .value
= TARGET_EVENT_GDB_HALT
, .name
= "gdb-halt" },
137 { .value
= TARGET_EVENT_HALTED
, .name
= "halted" },
138 { .value
= TARGET_EVENT_RESUMED
, .name
= "resumed" },
139 { .value
= TARGET_EVENT_RESUME_START
, .name
= "resume-start" },
140 { .value
= TARGET_EVENT_RESUME_END
, .name
= "resume-end" },
142 { .name
= "gdb-start", .value
= TARGET_EVENT_GDB_START
},
143 { .name
= "gdb-end", .value
= TARGET_EVENT_GDB_END
},
145 /* historical name */
147 { .value
= TARGET_EVENT_RESET_START
, .name
= "reset-start" },
149 { .value
= TARGET_EVENT_RESET_ASSERT_PRE
, .name
= "reset-assert-pre" },
150 { .value
= TARGET_EVENT_RESET_ASSERT
, .name
= "reset-assert" },
151 { .value
= TARGET_EVENT_RESET_ASSERT_POST
, .name
= "reset-assert-post" },
152 { .value
= TARGET_EVENT_RESET_DEASSERT_PRE
, .name
= "reset-deassert-pre" },
153 { .value
= TARGET_EVENT_RESET_DEASSERT_POST
, .name
= "reset-deassert-post" },
154 { .value
= TARGET_EVENT_RESET_HALT_PRE
, .name
= "reset-halt-pre" },
155 { .value
= TARGET_EVENT_RESET_HALT_POST
, .name
= "reset-halt-post" },
156 { .value
= TARGET_EVENT_RESET_WAIT_PRE
, .name
= "reset-wait-pre" },
157 { .value
= TARGET_EVENT_RESET_WAIT_POST
, .name
= "reset-wait-post" },
158 { .value
= TARGET_EVENT_RESET_INIT
, .name
= "reset-init" },
159 { .value
= TARGET_EVENT_RESET_END
, .name
= "reset-end" },
161 { .value
= TARGET_EVENT_EXAMINE_START
, .name
= "examine-start" },
162 { .value
= TARGET_EVENT_EXAMINE_END
, .name
= "examine-end" },
164 { .value
= TARGET_EVENT_DEBUG_HALTED
, .name
= "debug-halted" },
165 { .value
= TARGET_EVENT_DEBUG_RESUMED
, .name
= "debug-resumed" },
167 { .value
= TARGET_EVENT_GDB_ATTACH
, .name
= "gdb-attach" },
168 { .value
= TARGET_EVENT_GDB_DETACH
, .name
= "gdb-detach" },
170 { .value
= TARGET_EVENT_GDB_FLASH_WRITE_START
, .name
= "gdb-flash-write-start" },
171 { .value
= TARGET_EVENT_GDB_FLASH_WRITE_END
, .name
= "gdb-flash-write-end" },
173 { .value
= TARGET_EVENT_GDB_FLASH_ERASE_START
, .name
= "gdb-flash-erase-start" },
174 { .value
= TARGET_EVENT_GDB_FLASH_ERASE_END
, .name
= "gdb-flash-erase-end" },
176 { .value
= TARGET_EVENT_RESUME_START
, .name
= "resume-start" },
177 { .value
= TARGET_EVENT_RESUMED
, .name
= "resume-ok" },
178 { .value
= TARGET_EVENT_RESUME_END
, .name
= "resume-end" },
180 { .name
= NULL
, .value
= -1 }
183 const Jim_Nvp nvp_target_state
[] = {
184 { .name
= "unknown", .value
= TARGET_UNKNOWN
},
185 { .name
= "running", .value
= TARGET_RUNNING
},
186 { .name
= "halted", .value
= TARGET_HALTED
},
187 { .name
= "reset", .value
= TARGET_RESET
},
188 { .name
= "debug-running", .value
= TARGET_DEBUG_RUNNING
},
189 { .name
= NULL
, .value
= -1 },
192 const Jim_Nvp nvp_target_debug_reason
[] = {
193 { .name
= "debug-request" , .value
= DBG_REASON_DBGRQ
},
194 { .name
= "breakpoint" , .value
= DBG_REASON_BREAKPOINT
},
195 { .name
= "watchpoint" , .value
= DBG_REASON_WATCHPOINT
},
196 { .name
= "watchpoint-and-breakpoint", .value
= DBG_REASON_WPTANDBKPT
},
197 { .name
= "single-step" , .value
= DBG_REASON_SINGLESTEP
},
198 { .name
= "target-not-halted" , .value
= DBG_REASON_NOTHALTED
},
199 { .name
= "undefined" , .value
= DBG_REASON_UNDEFINED
},
200 { .name
= NULL
, .value
= -1 },
203 const Jim_Nvp nvp_target_endian
[] = {
204 { .name
= "big", .value
= TARGET_BIG_ENDIAN
},
205 { .name
= "little", .value
= TARGET_LITTLE_ENDIAN
},
206 { .name
= "be", .value
= TARGET_BIG_ENDIAN
},
207 { .name
= "le", .value
= TARGET_LITTLE_ENDIAN
},
208 { .name
= NULL
, .value
= -1 },
211 const Jim_Nvp nvp_reset_modes
[] = {
212 { .name
= "unknown", .value
= RESET_UNKNOWN
},
213 { .name
= "run" , .value
= RESET_RUN
},
214 { .name
= "halt" , .value
= RESET_HALT
},
215 { .name
= "init" , .value
= RESET_INIT
},
216 { .name
= NULL
, .value
= -1 },
220 target_state_name( struct target
*t
)
223 cp
= Jim_Nvp_value2name_simple(nvp_target_state
, t
->state
)->name
;
225 LOG_ERROR("Invalid target state: %d", (int)(t
->state
));
226 cp
= "(*BUG*unknown*BUG*)";
231 /* determine the number of the new target */
232 static int new_target_number(void)
237 /* number is 0 based */
241 if (x
< t
->target_number
) {
242 x
= t
->target_number
;
249 /* read a uint32_t from a buffer in target memory endianness */
250 uint32_t target_buffer_get_u32(struct target
*target
, const uint8_t *buffer
)
252 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
253 return le_to_h_u32(buffer
);
255 return be_to_h_u32(buffer
);
258 /* read a uint16_t from a buffer in target memory endianness */
259 uint16_t target_buffer_get_u16(struct target
*target
, const uint8_t *buffer
)
261 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
262 return le_to_h_u16(buffer
);
264 return be_to_h_u16(buffer
);
267 /* read a uint8_t from a buffer in target memory endianness */
268 uint8_t target_buffer_get_u8(struct target
*target
, const uint8_t *buffer
)
270 return *buffer
& 0x0ff;
273 /* write a uint32_t to a buffer in target memory endianness */
274 void target_buffer_set_u32(struct target
*target
, uint8_t *buffer
, uint32_t value
)
276 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
277 h_u32_to_le(buffer
, value
);
279 h_u32_to_be(buffer
, value
);
282 /* write a uint16_t to a buffer in target memory endianness */
283 void target_buffer_set_u16(struct target
*target
, uint8_t *buffer
, uint16_t value
)
285 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
286 h_u16_to_le(buffer
, value
);
288 h_u16_to_be(buffer
, value
);
291 /* write a uint8_t to a buffer in target memory endianness */
292 void target_buffer_set_u8(struct target
*target
, uint8_t *buffer
, uint8_t value
)
297 /* return a pointer to a configured target; id is name or number */
298 struct target
*get_target(const char *id
)
300 struct target
*target
;
302 /* try as tcltarget name */
303 for (target
= all_targets
; target
; target
= target
->next
) {
304 if (target
->cmd_name
== NULL
)
306 if (strcmp(id
, target
->cmd_name
) == 0)
310 /* It's OK to remove this fallback sometime after August 2010 or so */
312 /* no match, try as number */
314 if (parse_uint(id
, &num
) != ERROR_OK
)
317 for (target
= all_targets
; target
; target
= target
->next
) {
318 if (target
->target_number
== (int)num
) {
319 LOG_WARNING("use '%s' as target identifier, not '%u'",
320 target
->cmd_name
, num
);
328 /* returns a pointer to the n-th configured target */
329 static struct target
*get_target_by_num(int num
)
331 struct target
*target
= all_targets
;
334 if (target
->target_number
== num
) {
337 target
= target
->next
;
343 struct target
* get_current_target(struct command_context
*cmd_ctx
)
345 struct target
*target
= get_target_by_num(cmd_ctx
->current_target
);
349 LOG_ERROR("BUG: current_target out of bounds");
356 int target_poll(struct target
*target
)
360 /* We can't poll until after examine */
361 if (!target_was_examined(target
))
363 /* Fail silently lest we pollute the log */
367 retval
= target
->type
->poll(target
);
368 if (retval
!= ERROR_OK
)
371 if (target
->halt_issued
)
373 if (target
->state
== TARGET_HALTED
)
375 target
->halt_issued
= false;
378 long long t
= timeval_ms() - target
->halt_issued_time
;
381 target
->halt_issued
= false;
382 LOG_INFO("Halt timed out, wake up GDB.");
383 target_call_event_callbacks(target
, TARGET_EVENT_GDB_HALT
);
391 int target_halt(struct target
*target
)
394 /* We can't poll until after examine */
395 if (!target_was_examined(target
))
397 LOG_ERROR("Target not examined yet");
401 retval
= target
->type
->halt(target
);
402 if (retval
!= ERROR_OK
)
405 target
->halt_issued
= true;
406 target
->halt_issued_time
= timeval_ms();
411 int target_resume(struct target
*target
, int current
, uint32_t address
, int handle_breakpoints
, int debug_execution
)
415 /* We can't poll until after examine */
416 if (!target_was_examined(target
))
418 LOG_ERROR("Target not examined yet");
422 /* note that resume *must* be asynchronous. The CPU can halt before we poll. The CPU can
423 * even halt at the current PC as a result of a software breakpoint being inserted by (a bug?)
426 if ((retval
= target
->type
->resume(target
, current
, address
, handle_breakpoints
, debug_execution
)) != ERROR_OK
)
432 int target_process_reset(struct command_context
*cmd_ctx
, enum target_reset_mode reset_mode
)
437 n
= Jim_Nvp_value2name_simple(nvp_reset_modes
, reset_mode
);
438 if (n
->name
== NULL
) {
439 LOG_ERROR("invalid reset mode");
443 /* disable polling during reset to make reset event scripts
444 * more predictable, i.e. dr/irscan & pathmove in events will
445 * not have JTAG operations injected into the middle of a sequence.
447 bool save_poll
= jtag_poll_get_enabled();
449 jtag_poll_set_enabled(false);
451 sprintf(buf
, "ocd_process_reset %s", n
->name
);
452 retval
= Jim_Eval(cmd_ctx
->interp
, buf
);
454 jtag_poll_set_enabled(save_poll
);
456 if (retval
!= JIM_OK
) {
457 Jim_PrintErrorMessage(cmd_ctx
->interp
);
461 /* We want any events to be processed before the prompt */
462 retval
= target_call_timer_callbacks_now();
467 static int identity_virt2phys(struct target
*target
,
468 uint32_t virtual, uint32_t *physical
)
474 static int no_mmu(struct target
*target
, int *enabled
)
480 static int default_examine(struct target
*target
)
482 target_set_examined(target
);
486 int target_examine_one(struct target
*target
)
488 return target
->type
->examine(target
);
491 static int jtag_enable_callback(enum jtag_event event
, void *priv
)
493 struct target
*target
= priv
;
495 if (event
!= JTAG_TAP_EVENT_ENABLE
|| !target
->tap
->enabled
)
498 jtag_unregister_event_callback(jtag_enable_callback
, target
);
499 return target_examine_one(target
);
503 /* Targets that correctly implement init + examine, i.e.
504 * no communication with target during init:
508 int target_examine(void)
510 int retval
= ERROR_OK
;
511 struct target
*target
;
513 for (target
= all_targets
; target
; target
= target
->next
)
515 /* defer examination, but don't skip it */
516 if (!target
->tap
->enabled
) {
517 jtag_register_event_callback(jtag_enable_callback
,
521 if ((retval
= target_examine_one(target
)) != ERROR_OK
)
526 const char *target_type_name(struct target
*target
)
528 return target
->type
->name
;
531 static int target_write_memory_imp(struct target
*target
, uint32_t address
, uint32_t size
, uint32_t count
, uint8_t *buffer
)
533 if (!target_was_examined(target
))
535 LOG_ERROR("Target not examined yet");
538 return target
->type
->write_memory_imp(target
, address
, size
, count
, buffer
);
541 static int target_read_memory_imp(struct target
*target
, uint32_t address
, uint32_t size
, uint32_t count
, uint8_t *buffer
)
543 if (!target_was_examined(target
))
545 LOG_ERROR("Target not examined yet");
548 return target
->type
->read_memory_imp(target
, address
, size
, count
, buffer
);
551 static int target_soft_reset_halt_imp(struct target
*target
)
553 if (!target_was_examined(target
))
555 LOG_ERROR("Target not examined yet");
558 if (!target
->type
->soft_reset_halt_imp
) {
559 LOG_ERROR("Target %s does not support soft_reset_halt",
560 target_name(target
));
563 return target
->type
->soft_reset_halt_imp(target
);
566 static int target_run_algorithm_imp(struct target
*target
, int num_mem_params
, struct mem_param
*mem_params
, int num_reg_params
, struct reg_param
*reg_param
, uint32_t entry_point
, uint32_t exit_point
, int timeout_ms
, void *arch_info
)
568 if (!target_was_examined(target
))
570 LOG_ERROR("Target not examined yet");
573 return target
->type
->run_algorithm_imp(target
, num_mem_params
, mem_params
, num_reg_params
, reg_param
, entry_point
, exit_point
, timeout_ms
, arch_info
);
576 int target_read_memory(struct target
*target
,
577 uint32_t address
, uint32_t size
, uint32_t count
, uint8_t *buffer
)
579 return target
->type
->read_memory(target
, address
, size
, count
, buffer
);
582 int target_read_phys_memory(struct target
*target
,
583 uint32_t address
, uint32_t size
, uint32_t count
, uint8_t *buffer
)
585 return target
->type
->read_phys_memory(target
, address
, size
, count
, buffer
);
588 int target_write_memory(struct target
*target
,
589 uint32_t address
, uint32_t size
, uint32_t count
, uint8_t *buffer
)
591 return target
->type
->write_memory(target
, address
, size
, count
, buffer
);
594 int target_write_phys_memory(struct target
*target
,
595 uint32_t address
, uint32_t size
, uint32_t count
, uint8_t *buffer
)
597 return target
->type
->write_phys_memory(target
, address
, size
, count
, buffer
);
600 int target_bulk_write_memory(struct target
*target
,
601 uint32_t address
, uint32_t count
, uint8_t *buffer
)
603 return target
->type
->bulk_write_memory(target
, address
, count
, buffer
);
606 int target_add_breakpoint(struct target
*target
,
607 struct breakpoint
*breakpoint
)
609 if (target
->state
!= TARGET_HALTED
) {
610 LOG_WARNING("target %s is not halted", target
->cmd_name
);
611 return ERROR_TARGET_NOT_HALTED
;
613 return target
->type
->add_breakpoint(target
, breakpoint
);
615 int target_remove_breakpoint(struct target
*target
,
616 struct breakpoint
*breakpoint
)
618 return target
->type
->remove_breakpoint(target
, breakpoint
);
621 int target_add_watchpoint(struct target
*target
,
622 struct watchpoint
*watchpoint
)
624 if (target
->state
!= TARGET_HALTED
) {
625 LOG_WARNING("target %s is not halted", target
->cmd_name
);
626 return ERROR_TARGET_NOT_HALTED
;
628 return target
->type
->add_watchpoint(target
, watchpoint
);
630 int target_remove_watchpoint(struct target
*target
,
631 struct watchpoint
*watchpoint
)
633 return target
->type
->remove_watchpoint(target
, watchpoint
);
636 int target_get_gdb_reg_list(struct target
*target
,
637 struct reg
**reg_list
[], int *reg_list_size
)
639 return target
->type
->get_gdb_reg_list(target
, reg_list
, reg_list_size
);
641 int target_step(struct target
*target
,
642 int current
, uint32_t address
, int handle_breakpoints
)
644 return target
->type
->step(target
, current
, address
, handle_breakpoints
);
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 return target
->type
->run_algorithm(target
,
655 num_mem_params
, mem_params
, num_reg_params
, reg_param
,
656 entry_point
, exit_point
, timeout_ms
, arch_info
);
660 * Reset the @c examined flag for the given target.
661 * Pure paranoia -- targets are zeroed on allocation.
663 static void target_reset_examined(struct target
*target
)
665 target
->examined
= false;
670 static int default_mrc(struct target
*target
, int cpnum
, uint32_t op1
, uint32_t op2
, uint32_t CRn
, uint32_t CRm
, uint32_t *value
)
672 LOG_ERROR("Not implemented: %s", __func__
);
676 static int default_mcr(struct target
*target
, int cpnum
, uint32_t op1
, uint32_t op2
, uint32_t CRn
, uint32_t CRm
, uint32_t value
)
678 LOG_ERROR("Not implemented: %s", __func__
);
682 static int arm_cp_check(struct target
*target
, int cpnum
, uint32_t op1
, uint32_t op2
, uint32_t CRn
, uint32_t CRm
)
685 if (!target_was_examined(target
))
687 LOG_ERROR("Target not examined yet");
691 if ((cpnum
<0) || (cpnum
> 15))
693 LOG_ERROR("Illegal co-processor %d", cpnum
);
699 LOG_ERROR("Illegal op1");
705 LOG_ERROR("Illegal op2");
711 LOG_ERROR("Illegal CRn");
717 LOG_ERROR("Illegal CRm");
724 int target_mrc(struct target
*target
, int cpnum
, uint32_t op1
, uint32_t op2
, uint32_t CRn
, uint32_t CRm
, uint32_t *value
)
728 retval
= arm_cp_check(target
, cpnum
, op1
, op2
, CRn
, CRm
);
729 if (retval
!= ERROR_OK
)
732 return target
->type
->mrc(target
, cpnum
, op1
, op2
, CRn
, CRm
, value
);
735 int target_mcr(struct target
*target
, int cpnum
, uint32_t op1
, uint32_t op2
, uint32_t CRn
, uint32_t CRm
, uint32_t value
)
739 retval
= arm_cp_check(target
, cpnum
, op1
, op2
, CRn
, CRm
);
740 if (retval
!= ERROR_OK
)
743 return target
->type
->mcr(target
, cpnum
, op1
, op2
, CRn
, CRm
, value
);
747 err_read_phys_memory(struct target
*target
, uint32_t address
,
748 uint32_t size
, uint32_t count
, uint8_t *buffer
)
750 LOG_ERROR("Not implemented: %s", __func__
);
755 err_write_phys_memory(struct target
*target
, uint32_t address
,
756 uint32_t size
, uint32_t count
, uint8_t *buffer
)
758 LOG_ERROR("Not implemented: %s", __func__
);
762 static int handle_target(void *priv
);
764 int target_init(struct command_context
*cmd_ctx
)
766 struct target
*target
;
769 for (target
= all_targets
; target
; target
= target
->next
) {
770 struct target_type
*type
= target
->type
;
772 target_reset_examined(target
);
773 if (target
->type
->examine
== NULL
)
775 target
->type
->examine
= default_examine
;
778 if ((retval
= target
->type
->init_target(cmd_ctx
, target
)) != ERROR_OK
)
780 LOG_ERROR("target '%s' init failed", target_name(target
));
785 * @todo MCR/MRC are ARM-specific; don't require them in
786 * all targets, or for ARMs without coprocessors.
788 if (target
->type
->mcr
== NULL
)
790 target
->type
->mcr
= default_mcr
;
793 const struct command_registration mcr_cmd
= {
795 .mode
= COMMAND_EXEC
,
796 .jim_handler
= &jim_mcrmrc
,
797 .help
= "write coprocessor",
798 .usage
= "<cpnum> <op1> <op2> <CRn> <CRm> <value>",
800 register_command(cmd_ctx
, NULL
, &mcr_cmd
);
803 if (target
->type
->mrc
== NULL
)
805 target
->type
->mrc
= default_mrc
;
808 const struct command_registration mrc_cmd
= {
810 .jim_handler
= &jim_mcrmrc
,
811 .help
= "read coprocessor",
812 .usage
= "<cpnum> <op1> <op2> <CRn> <CRm>",
814 register_command(cmd_ctx
, NULL
, &mrc_cmd
);
819 * @todo get rid of those *memory_imp() methods, now that all
820 * callers are using target_*_memory() accessors ... and make
821 * sure the "physical" paths handle the same issues.
824 /* a non-invasive way(in terms of patches) to add some code that
825 * runs before the type->write/read_memory implementation
827 target
->type
->write_memory_imp
= target
->type
->write_memory
;
828 target
->type
->write_memory
= target_write_memory_imp
;
829 target
->type
->read_memory_imp
= target
->type
->read_memory
;
830 target
->type
->read_memory
= target_read_memory_imp
;
831 target
->type
->soft_reset_halt_imp
= target
->type
->soft_reset_halt
;
832 target
->type
->soft_reset_halt
= target_soft_reset_halt_imp
;
833 target
->type
->run_algorithm_imp
= target
->type
->run_algorithm
;
834 target
->type
->run_algorithm
= target_run_algorithm_imp
;
836 /* Sanity-check MMU support ... stub in what we must, to help
837 * implement it in stages, but warn if we need to do so.
840 if (type
->write_phys_memory
== NULL
) {
841 LOG_ERROR("type '%s' is missing %s",
843 "write_phys_memory");
844 type
->write_phys_memory
= err_write_phys_memory
;
846 if (type
->read_phys_memory
== NULL
) {
847 LOG_ERROR("type '%s' is missing %s",
850 type
->read_phys_memory
= err_read_phys_memory
;
852 if (type
->virt2phys
== NULL
) {
853 LOG_ERROR("type '%s' is missing %s",
856 type
->virt2phys
= identity_virt2phys
;
859 /* Make sure no-MMU targets all behave the same: make no
860 * distinction between physical and virtual addresses, and
861 * ensure that virt2phys() is always an identity mapping.
864 if (type
->write_phys_memory
865 || type
->read_phys_memory
867 LOG_WARNING("type '%s' has broken MMU hooks",
871 type
->write_phys_memory
= type
->write_memory
;
872 type
->read_phys_memory
= type
->read_memory
;
873 type
->virt2phys
= identity_virt2phys
;
879 if ((retval
= target_register_user_commands(cmd_ctx
)) != ERROR_OK
)
881 if ((retval
= target_register_timer_callback(&handle_target
, 100, 1, cmd_ctx
->interp
)) != ERROR_OK
)
888 int target_register_event_callback(int (*callback
)(struct target
*target
, enum target_event event
, void *priv
), void *priv
)
890 struct target_event_callback
**callbacks_p
= &target_event_callbacks
;
892 if (callback
== NULL
)
894 return ERROR_INVALID_ARGUMENTS
;
899 while ((*callbacks_p
)->next
)
900 callbacks_p
= &((*callbacks_p
)->next
);
901 callbacks_p
= &((*callbacks_p
)->next
);
904 (*callbacks_p
) = malloc(sizeof(struct target_event_callback
));
905 (*callbacks_p
)->callback
= callback
;
906 (*callbacks_p
)->priv
= priv
;
907 (*callbacks_p
)->next
= NULL
;
912 int target_register_timer_callback(int (*callback
)(void *priv
), int time_ms
, int periodic
, void *priv
)
914 struct target_timer_callback
**callbacks_p
= &target_timer_callbacks
;
917 if (callback
== NULL
)
919 return ERROR_INVALID_ARGUMENTS
;
924 while ((*callbacks_p
)->next
)
925 callbacks_p
= &((*callbacks_p
)->next
);
926 callbacks_p
= &((*callbacks_p
)->next
);
929 (*callbacks_p
) = malloc(sizeof(struct target_timer_callback
));
930 (*callbacks_p
)->callback
= callback
;
931 (*callbacks_p
)->periodic
= periodic
;
932 (*callbacks_p
)->time_ms
= time_ms
;
934 gettimeofday(&now
, NULL
);
935 (*callbacks_p
)->when
.tv_usec
= now
.tv_usec
+ (time_ms
% 1000) * 1000;
936 time_ms
-= (time_ms
% 1000);
937 (*callbacks_p
)->when
.tv_sec
= now
.tv_sec
+ (time_ms
/ 1000);
938 if ((*callbacks_p
)->when
.tv_usec
> 1000000)
940 (*callbacks_p
)->when
.tv_usec
= (*callbacks_p
)->when
.tv_usec
- 1000000;
941 (*callbacks_p
)->when
.tv_sec
+= 1;
944 (*callbacks_p
)->priv
= priv
;
945 (*callbacks_p
)->next
= NULL
;
950 int target_unregister_event_callback(int (*callback
)(struct target
*target
, enum target_event event
, void *priv
), void *priv
)
952 struct target_event_callback
**p
= &target_event_callbacks
;
953 struct target_event_callback
*c
= target_event_callbacks
;
955 if (callback
== NULL
)
957 return ERROR_INVALID_ARGUMENTS
;
962 struct target_event_callback
*next
= c
->next
;
963 if ((c
->callback
== callback
) && (c
->priv
== priv
))
977 int target_unregister_timer_callback(int (*callback
)(void *priv
), void *priv
)
979 struct target_timer_callback
**p
= &target_timer_callbacks
;
980 struct target_timer_callback
*c
= target_timer_callbacks
;
982 if (callback
== NULL
)
984 return ERROR_INVALID_ARGUMENTS
;
989 struct target_timer_callback
*next
= c
->next
;
990 if ((c
->callback
== callback
) && (c
->priv
== priv
))
1004 int target_call_event_callbacks(struct target
*target
, enum target_event event
)
1006 struct target_event_callback
*callback
= target_event_callbacks
;
1007 struct target_event_callback
*next_callback
;
1009 if (event
== TARGET_EVENT_HALTED
)
1011 /* execute early halted first */
1012 target_call_event_callbacks(target
, TARGET_EVENT_GDB_HALT
);
1015 LOG_DEBUG("target event %i (%s)",
1017 Jim_Nvp_value2name_simple(nvp_target_event
, event
)->name
);
1019 target_handle_event(target
, event
);
1023 next_callback
= callback
->next
;
1024 callback
->callback(target
, event
, callback
->priv
);
1025 callback
= next_callback
;
1031 static int target_timer_callback_periodic_restart(
1032 struct target_timer_callback
*cb
, struct timeval
*now
)
1034 int time_ms
= cb
->time_ms
;
1035 cb
->when
.tv_usec
= now
->tv_usec
+ (time_ms
% 1000) * 1000;
1036 time_ms
-= (time_ms
% 1000);
1037 cb
->when
.tv_sec
= now
->tv_sec
+ time_ms
/ 1000;
1038 if (cb
->when
.tv_usec
> 1000000)
1040 cb
->when
.tv_usec
= cb
->when
.tv_usec
- 1000000;
1041 cb
->when
.tv_sec
+= 1;
1046 static int target_call_timer_callback(struct target_timer_callback
*cb
,
1047 struct timeval
*now
)
1049 cb
->callback(cb
->priv
);
1052 return target_timer_callback_periodic_restart(cb
, now
);
1054 return target_unregister_timer_callback(cb
->callback
, cb
->priv
);
1057 static int target_call_timer_callbacks_check_time(int checktime
)
1062 gettimeofday(&now
, NULL
);
1064 struct target_timer_callback
*callback
= target_timer_callbacks
;
1067 // cleaning up may unregister and free this callback
1068 struct target_timer_callback
*next_callback
= callback
->next
;
1070 bool call_it
= callback
->callback
&&
1071 ((!checktime
&& callback
->periodic
) ||
1072 now
.tv_sec
> callback
->when
.tv_sec
||
1073 (now
.tv_sec
== callback
->when
.tv_sec
&&
1074 now
.tv_usec
>= callback
->when
.tv_usec
));
1078 int retval
= target_call_timer_callback(callback
, &now
);
1079 if (retval
!= ERROR_OK
)
1083 callback
= next_callback
;
1089 int target_call_timer_callbacks(void)
1091 return target_call_timer_callbacks_check_time(1);
1094 /* invoke periodic callbacks immediately */
1095 int target_call_timer_callbacks_now(void)
1097 return target_call_timer_callbacks_check_time(0);
1100 int target_alloc_working_area(struct target
*target
, uint32_t size
, struct working_area
**area
)
1102 struct working_area
*c
= target
->working_areas
;
1103 struct working_area
*new_wa
= NULL
;
1105 /* Reevaluate working area address based on MMU state*/
1106 if (target
->working_areas
== NULL
)
1111 retval
= target
->type
->mmu(target
, &enabled
);
1112 if (retval
!= ERROR_OK
)
1118 if (target
->working_area_phys_spec
) {
1119 LOG_DEBUG("MMU disabled, using physical "
1120 "address for working memory 0x%08x",
1121 (unsigned)target
->working_area_phys
);
1122 target
->working_area
= target
->working_area_phys
;
1124 LOG_ERROR("No working memory available. "
1125 "Specify -work-area-phys to target.");
1126 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
1129 if (target
->working_area_virt_spec
) {
1130 LOG_DEBUG("MMU enabled, using virtual "
1131 "address for working memory 0x%08x",
1132 (unsigned)target
->working_area_virt
);
1133 target
->working_area
= target
->working_area_virt
;
1135 LOG_ERROR("No working memory available. "
1136 "Specify -work-area-virt to target.");
1137 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
1142 /* only allocate multiples of 4 byte */
1145 LOG_ERROR("BUG: code tried to allocate unaligned number of bytes (0x%08x), padding", ((unsigned)(size
)));
1146 size
= (size
+ 3) & (~3);
1149 /* see if there's already a matching working area */
1152 if ((c
->free
) && (c
->size
== size
))
1160 /* if not, allocate a new one */
1163 struct working_area
**p
= &target
->working_areas
;
1164 uint32_t first_free
= target
->working_area
;
1165 uint32_t free_size
= target
->working_area_size
;
1167 c
= target
->working_areas
;
1170 first_free
+= c
->size
;
1171 free_size
-= c
->size
;
1176 if (free_size
< size
)
1178 LOG_WARNING("not enough working area available(requested %u, free %u)",
1179 (unsigned)(size
), (unsigned)(free_size
));
1180 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
1183 LOG_DEBUG("allocated new working area at address 0x%08x", (unsigned)first_free
);
1185 new_wa
= malloc(sizeof(struct working_area
));
1186 new_wa
->next
= NULL
;
1187 new_wa
->size
= size
;
1188 new_wa
->address
= first_free
;
1190 if (target
->backup_working_area
)
1193 new_wa
->backup
= malloc(new_wa
->size
);
1194 if ((retval
= target_read_memory(target
, new_wa
->address
, 4, new_wa
->size
/ 4, new_wa
->backup
)) != ERROR_OK
)
1196 free(new_wa
->backup
);
1203 new_wa
->backup
= NULL
;
1206 /* put new entry in list */
1210 /* mark as used, and return the new (reused) area */
1215 new_wa
->user
= area
;
1220 int target_free_working_area_restore(struct target
*target
, struct working_area
*area
, int restore
)
1225 if (restore
&& target
->backup_working_area
)
1228 if ((retval
= target_write_memory(target
, area
->address
, 4, area
->size
/ 4, area
->backup
)) != ERROR_OK
)
1234 /* mark user pointer invalid */
1241 int target_free_working_area(struct target
*target
, struct working_area
*area
)
1243 return target_free_working_area_restore(target
, area
, 1);
1246 /* free resources and restore memory, if restoring memory fails,
1247 * free up resources anyway
1249 void target_free_all_working_areas_restore(struct target
*target
, int restore
)
1251 struct working_area
*c
= target
->working_areas
;
1255 struct working_area
*next
= c
->next
;
1256 target_free_working_area_restore(target
, c
, restore
);
1266 target
->working_areas
= NULL
;
1269 void target_free_all_working_areas(struct target
*target
)
1271 target_free_all_working_areas_restore(target
, 1);
1274 int target_arch_state(struct target
*target
)
1279 LOG_USER("No target has been configured");
1283 LOG_USER("target state: %s", target_state_name( target
));
1285 if (target
->state
!= TARGET_HALTED
)
1288 retval
= target
->type
->arch_state(target
);
1292 /* Single aligned words are guaranteed to use 16 or 32 bit access
1293 * mode respectively, otherwise data is handled as quickly as
1296 int target_write_buffer(struct target
*target
, uint32_t address
, uint32_t size
, uint8_t *buffer
)
1299 LOG_DEBUG("writing buffer of %i byte at 0x%8.8x",
1300 (int)size
, (unsigned)address
);
1302 if (!target_was_examined(target
))
1304 LOG_ERROR("Target not examined yet");
1312 if ((address
+ size
- 1) < address
)
1314 /* GDB can request this when e.g. PC is 0xfffffffc*/
1315 LOG_ERROR("address + size wrapped(0x%08x, 0x%08x)",
1321 if (((address
% 2) == 0) && (size
== 2))
1323 return target_write_memory(target
, address
, 2, 1, buffer
);
1326 /* handle unaligned head bytes */
1329 uint32_t unaligned
= 4 - (address
% 4);
1331 if (unaligned
> size
)
1334 if ((retval
= target_write_memory(target
, address
, 1, unaligned
, buffer
)) != ERROR_OK
)
1337 buffer
+= unaligned
;
1338 address
+= unaligned
;
1342 /* handle aligned words */
1345 int aligned
= size
- (size
% 4);
1347 /* use bulk writes above a certain limit. This may have to be changed */
1350 if ((retval
= target
->type
->bulk_write_memory(target
, address
, aligned
/ 4, buffer
)) != ERROR_OK
)
1355 if ((retval
= target_write_memory(target
, address
, 4, aligned
/ 4, buffer
)) != ERROR_OK
)
1364 /* handle tail writes of less than 4 bytes */
1367 if ((retval
= target_write_memory(target
, address
, 1, size
, buffer
)) != ERROR_OK
)
1374 /* Single aligned words are guaranteed to use 16 or 32 bit access
1375 * mode respectively, otherwise data is handled as quickly as
1378 int target_read_buffer(struct target
*target
, uint32_t address
, uint32_t size
, uint8_t *buffer
)
1381 LOG_DEBUG("reading buffer of %i byte at 0x%8.8x",
1382 (int)size
, (unsigned)address
);
1384 if (!target_was_examined(target
))
1386 LOG_ERROR("Target not examined yet");
1394 if ((address
+ size
- 1) < address
)
1396 /* GDB can request this when e.g. PC is 0xfffffffc*/
1397 LOG_ERROR("address + size wrapped(0x%08" PRIx32
", 0x%08" PRIx32
")",
1403 if (((address
% 2) == 0) && (size
== 2))
1405 return target_read_memory(target
, address
, 2, 1, buffer
);
1408 /* handle unaligned head bytes */
1411 uint32_t unaligned
= 4 - (address
% 4);
1413 if (unaligned
> size
)
1416 if ((retval
= target_read_memory(target
, address
, 1, unaligned
, buffer
)) != ERROR_OK
)
1419 buffer
+= unaligned
;
1420 address
+= unaligned
;
1424 /* handle aligned words */
1427 int aligned
= size
- (size
% 4);
1429 if ((retval
= target_read_memory(target
, address
, 4, aligned
/ 4, buffer
)) != ERROR_OK
)
1437 /*prevent byte access when possible (avoid AHB access limitations in some cases)*/
1440 int aligned
= size
- (size
%2);
1441 retval
= target_read_memory(target
, address
, 2, aligned
/ 2, buffer
);
1442 if (retval
!= ERROR_OK
)
1449 /* handle tail writes of less than 4 bytes */
1452 if ((retval
= target_read_memory(target
, address
, 1, size
, buffer
)) != ERROR_OK
)
1459 int target_checksum_memory(struct target
*target
, uint32_t address
, uint32_t size
, uint32_t* crc
)
1464 uint32_t checksum
= 0;
1465 if (!target_was_examined(target
))
1467 LOG_ERROR("Target not examined yet");
1471 if ((retval
= target
->type
->checksum_memory(target
, address
,
1472 size
, &checksum
)) != ERROR_OK
)
1474 buffer
= malloc(size
);
1477 LOG_ERROR("error allocating buffer for section (%d bytes)", (int)size
);
1478 return ERROR_INVALID_ARGUMENTS
;
1480 retval
= target_read_buffer(target
, address
, size
, buffer
);
1481 if (retval
!= ERROR_OK
)
1487 /* convert to target endianess */
1488 for (i
= 0; i
< (size
/sizeof(uint32_t)); i
++)
1490 uint32_t target_data
;
1491 target_data
= target_buffer_get_u32(target
, &buffer
[i
*sizeof(uint32_t)]);
1492 target_buffer_set_u32(target
, &buffer
[i
*sizeof(uint32_t)], target_data
);
1495 retval
= image_calculate_checksum(buffer
, size
, &checksum
);
1504 int target_blank_check_memory(struct target
*target
, uint32_t address
, uint32_t size
, uint32_t* blank
)
1507 if (!target_was_examined(target
))
1509 LOG_ERROR("Target not examined yet");
1513 if (target
->type
->blank_check_memory
== 0)
1514 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
1516 retval
= target
->type
->blank_check_memory(target
, address
, size
, blank
);
1521 int target_read_u32(struct target
*target
, uint32_t address
, uint32_t *value
)
1523 uint8_t value_buf
[4];
1524 if (!target_was_examined(target
))
1526 LOG_ERROR("Target not examined yet");
1530 int retval
= target_read_memory(target
, address
, 4, 1, value_buf
);
1532 if (retval
== ERROR_OK
)
1534 *value
= target_buffer_get_u32(target
, value_buf
);
1535 LOG_DEBUG("address: 0x%8.8" PRIx32
", value: 0x%8.8" PRIx32
"",
1542 LOG_DEBUG("address: 0x%8.8" PRIx32
" failed",
1549 int target_read_u16(struct target
*target
, uint32_t address
, uint16_t *value
)
1551 uint8_t value_buf
[2];
1552 if (!target_was_examined(target
))
1554 LOG_ERROR("Target not examined yet");
1558 int retval
= target_read_memory(target
, address
, 2, 1, value_buf
);
1560 if (retval
== ERROR_OK
)
1562 *value
= target_buffer_get_u16(target
, value_buf
);
1563 LOG_DEBUG("address: 0x%8.8" PRIx32
", value: 0x%4.4x",
1570 LOG_DEBUG("address: 0x%8.8" PRIx32
" failed",
1577 int target_read_u8(struct target
*target
, uint32_t address
, uint8_t *value
)
1579 int retval
= target_read_memory(target
, address
, 1, 1, value
);
1580 if (!target_was_examined(target
))
1582 LOG_ERROR("Target not examined yet");
1586 if (retval
== ERROR_OK
)
1588 LOG_DEBUG("address: 0x%8.8" PRIx32
", value: 0x%2.2x",
1595 LOG_DEBUG("address: 0x%8.8" PRIx32
" failed",
1602 int target_write_u32(struct target
*target
, uint32_t address
, uint32_t value
)
1605 uint8_t value_buf
[4];
1606 if (!target_was_examined(target
))
1608 LOG_ERROR("Target not examined yet");
1612 LOG_DEBUG("address: 0x%8.8" PRIx32
", value: 0x%8.8" PRIx32
"",
1616 target_buffer_set_u32(target
, value_buf
, value
);
1617 if ((retval
= target_write_memory(target
, address
, 4, 1, value_buf
)) != ERROR_OK
)
1619 LOG_DEBUG("failed: %i", retval
);
1625 int target_write_u16(struct target
*target
, uint32_t address
, uint16_t value
)
1628 uint8_t value_buf
[2];
1629 if (!target_was_examined(target
))
1631 LOG_ERROR("Target not examined yet");
1635 LOG_DEBUG("address: 0x%8.8" PRIx32
", value: 0x%8.8x",
1639 target_buffer_set_u16(target
, value_buf
, value
);
1640 if ((retval
= target_write_memory(target
, address
, 2, 1, value_buf
)) != ERROR_OK
)
1642 LOG_DEBUG("failed: %i", retval
);
1648 int target_write_u8(struct target
*target
, uint32_t address
, uint8_t value
)
1651 if (!target_was_examined(target
))
1653 LOG_ERROR("Target not examined yet");
1657 LOG_DEBUG("address: 0x%8.8" PRIx32
", value: 0x%2.2x",
1660 if ((retval
= target_write_memory(target
, address
, 1, 1, &value
)) != ERROR_OK
)
1662 LOG_DEBUG("failed: %i", retval
);
1668 COMMAND_HANDLER(handle_targets_command
)
1670 struct target
*target
= all_targets
;
1674 target
= get_target(CMD_ARGV
[0]);
1675 if (target
== NULL
) {
1676 command_print(CMD_CTX
,"Target: %s is unknown, try one of:\n", CMD_ARGV
[0]);
1679 if (!target
->tap
->enabled
) {
1680 command_print(CMD_CTX
,"Target: TAP %s is disabled, "
1681 "can't be the current target\n",
1682 target
->tap
->dotted_name
);
1686 CMD_CTX
->current_target
= target
->target_number
;
1691 target
= all_targets
;
1692 command_print(CMD_CTX
, " TargetName Type Endian TapName State ");
1693 command_print(CMD_CTX
, "-- ------------------ ---------- ------ ------------------ ------------");
1699 if (target
->tap
->enabled
)
1700 state
= target_state_name( target
);
1702 state
= "tap-disabled";
1704 if (CMD_CTX
->current_target
== target
->target_number
)
1707 /* keep columns lined up to match the headers above */
1708 command_print(CMD_CTX
, "%2d%c %-18s %-10s %-6s %-18s %s",
1709 target
->target_number
,
1711 target_name(target
),
1712 target_type_name(target
),
1713 Jim_Nvp_value2name_simple(nvp_target_endian
,
1714 target
->endianness
)->name
,
1715 target
->tap
->dotted_name
,
1717 target
= target
->next
;
1723 /* every 300ms we check for reset & powerdropout and issue a "reset halt" if so. */
1725 static int powerDropout
;
1726 static int srstAsserted
;
1728 static int runPowerRestore
;
1729 static int runPowerDropout
;
1730 static int runSrstAsserted
;
1731 static int runSrstDeasserted
;
1733 static int sense_handler(void)
1735 static int prevSrstAsserted
= 0;
1736 static int prevPowerdropout
= 0;
1739 if ((retval
= jtag_power_dropout(&powerDropout
)) != ERROR_OK
)
1743 powerRestored
= prevPowerdropout
&& !powerDropout
;
1746 runPowerRestore
= 1;
1749 long long current
= timeval_ms();
1750 static long long lastPower
= 0;
1751 int waitMore
= lastPower
+ 2000 > current
;
1752 if (powerDropout
&& !waitMore
)
1754 runPowerDropout
= 1;
1755 lastPower
= current
;
1758 if ((retval
= jtag_srst_asserted(&srstAsserted
)) != ERROR_OK
)
1762 srstDeasserted
= prevSrstAsserted
&& !srstAsserted
;
1764 static long long lastSrst
= 0;
1765 waitMore
= lastSrst
+ 2000 > current
;
1766 if (srstDeasserted
&& !waitMore
)
1768 runSrstDeasserted
= 1;
1772 if (!prevSrstAsserted
&& srstAsserted
)
1774 runSrstAsserted
= 1;
1777 prevSrstAsserted
= srstAsserted
;
1778 prevPowerdropout
= powerDropout
;
1780 if (srstDeasserted
|| powerRestored
)
1782 /* Other than logging the event we can't do anything here.
1783 * Issuing a reset is a particularly bad idea as we might
1784 * be inside a reset already.
1791 static void target_call_event_callbacks_all(enum target_event e
) {
1792 struct target
*target
;
1793 target
= all_targets
;
1795 target_call_event_callbacks(target
, e
);
1796 target
= target
->next
;
1800 /* process target state changes */
1801 static int handle_target(void *priv
)
1803 Jim_Interp
*interp
= (Jim_Interp
*)priv
;
1804 int retval
= ERROR_OK
;
1806 /* we do not want to recurse here... */
1807 static int recursive
= 0;
1812 /* danger! running these procedures can trigger srst assertions and power dropouts.
1813 * We need to avoid an infinite loop/recursion here and we do that by
1814 * clearing the flags after running these events.
1816 int did_something
= 0;
1817 if (runSrstAsserted
)
1819 target_call_event_callbacks_all(TARGET_EVENT_GDB_HALT
);
1820 Jim_Eval(interp
, "srst_asserted");
1823 if (runSrstDeasserted
)
1825 Jim_Eval(interp
, "srst_deasserted");
1828 if (runPowerDropout
)
1830 target_call_event_callbacks_all(TARGET_EVENT_GDB_HALT
);
1831 Jim_Eval(interp
, "power_dropout");
1834 if (runPowerRestore
)
1836 Jim_Eval(interp
, "power_restore");
1842 /* clear detect flags */
1846 /* clear action flags */
1848 runSrstAsserted
= 0;
1849 runSrstDeasserted
= 0;
1850 runPowerRestore
= 0;
1851 runPowerDropout
= 0;
1856 /* Poll targets for state changes unless that's globally disabled.
1857 * Skip targets that are currently disabled.
1859 for (struct target
*target
= all_targets
;
1860 is_jtag_poll_safe() && target
;
1861 target
= target
->next
)
1863 if (!target
->tap
->enabled
)
1866 /* only poll target if we've got power and srst isn't asserted */
1867 if (!powerDropout
&& !srstAsserted
)
1869 /* polling may fail silently until the target has been examined */
1870 if ((retval
= target_poll(target
)) != ERROR_OK
)
1872 target_call_event_callbacks(target
, TARGET_EVENT_GDB_HALT
);
1881 COMMAND_HANDLER(handle_reg_command
)
1883 struct target
*target
;
1884 struct reg
*reg
= NULL
;
1890 target
= get_current_target(CMD_CTX
);
1892 /* list all available registers for the current target */
1895 struct reg_cache
*cache
= target
->reg_cache
;
1902 command_print(CMD_CTX
, "===== %s", cache
->name
);
1904 for (i
= 0, reg
= cache
->reg_list
;
1905 i
< cache
->num_regs
;
1906 i
++, reg
++, count
++)
1908 /* only print cached values if they are valid */
1910 value
= buf_to_str(reg
->value
,
1912 command_print(CMD_CTX
,
1913 "(%i) %s (/%" PRIu32
"): 0x%s%s",
1921 command_print(CMD_CTX
, "(%i) %s (/%" PRIu32
")",
1926 cache
= cache
->next
;
1932 /* access a single register by its ordinal number */
1933 if ((CMD_ARGV
[0][0] >= '0') && (CMD_ARGV
[0][0] <= '9'))
1936 COMMAND_PARSE_NUMBER(uint
, CMD_ARGV
[0], num
);
1938 struct reg_cache
*cache
= target
->reg_cache
;
1943 for (i
= 0; i
< cache
->num_regs
; i
++)
1947 reg
= &cache
->reg_list
[i
];
1953 cache
= cache
->next
;
1958 command_print(CMD_CTX
, "%i is out of bounds, the current target has only %i registers (0 - %i)", num
, count
, count
- 1);
1961 } else /* access a single register by its name */
1963 reg
= register_get_by_name(target
->reg_cache
, CMD_ARGV
[0], 1);
1967 command_print(CMD_CTX
, "register %s not found in current target", CMD_ARGV
[0]);
1972 /* display a register */
1973 if ((CMD_ARGC
== 1) || ((CMD_ARGC
== 2) && !((CMD_ARGV
[1][0] >= '0') && (CMD_ARGV
[1][0] <= '9'))))
1975 if ((CMD_ARGC
== 2) && (strcmp(CMD_ARGV
[1], "force") == 0))
1978 if (reg
->valid
== 0)
1980 reg
->type
->get(reg
);
1982 value
= buf_to_str(reg
->value
, reg
->size
, 16);
1983 command_print(CMD_CTX
, "%s (/%i): 0x%s", reg
->name
, (int)(reg
->size
), value
);
1988 /* set register value */
1991 uint8_t *buf
= malloc(DIV_ROUND_UP(reg
->size
, 8));
1992 str_to_buf(CMD_ARGV
[1], strlen(CMD_ARGV
[1]), buf
, reg
->size
, 0);
1994 reg
->type
->set(reg
, buf
);
1996 value
= buf_to_str(reg
->value
, reg
->size
, 16);
1997 command_print(CMD_CTX
, "%s (/%i): 0x%s", reg
->name
, (int)(reg
->size
), value
);
2005 command_print(CMD_CTX
, "usage: reg <#|name> [value]");
2010 COMMAND_HANDLER(handle_poll_command
)
2012 int retval
= ERROR_OK
;
2013 struct target
*target
= get_current_target(CMD_CTX
);
2017 command_print(CMD_CTX
, "background polling: %s",
2018 jtag_poll_get_enabled() ? "on" : "off");
2019 command_print(CMD_CTX
, "TAP: %s (%s)",
2020 target
->tap
->dotted_name
,
2021 target
->tap
->enabled
? "enabled" : "disabled");
2022 if (!target
->tap
->enabled
)
2024 if ((retval
= target_poll(target
)) != ERROR_OK
)
2026 if ((retval
= target_arch_state(target
)) != ERROR_OK
)
2029 else if (CMD_ARGC
== 1)
2032 COMMAND_PARSE_ON_OFF(CMD_ARGV
[0], enable
);
2033 jtag_poll_set_enabled(enable
);
2037 return ERROR_COMMAND_SYNTAX_ERROR
;
2043 COMMAND_HANDLER(handle_wait_halt_command
)
2046 return ERROR_COMMAND_SYNTAX_ERROR
;
2051 int retval
= parse_uint(CMD_ARGV
[0], &ms
);
2052 if (ERROR_OK
!= retval
)
2054 command_print(CMD_CTX
, "usage: %s [seconds]", CMD_NAME
);
2055 return ERROR_COMMAND_SYNTAX_ERROR
;
2057 // convert seconds (given) to milliseconds (needed)
2061 struct target
*target
= get_current_target(CMD_CTX
);
2062 return target_wait_state(target
, TARGET_HALTED
, ms
);
2065 /* wait for target state to change. The trick here is to have a low
2066 * latency for short waits and not to suck up all the CPU time
2069 * After 500ms, keep_alive() is invoked
2071 int target_wait_state(struct target
*target
, enum target_state state
, int ms
)
2074 long long then
= 0, cur
;
2079 if ((retval
= target_poll(target
)) != ERROR_OK
)
2081 if (target
->state
== state
)
2089 then
= timeval_ms();
2090 LOG_DEBUG("waiting for target %s...",
2091 Jim_Nvp_value2name_simple(nvp_target_state
,state
)->name
);
2099 if ((cur
-then
) > ms
)
2101 LOG_ERROR("timed out while waiting for target %s",
2102 Jim_Nvp_value2name_simple(nvp_target_state
,state
)->name
);
2110 COMMAND_HANDLER(handle_halt_command
)
2114 struct target
*target
= get_current_target(CMD_CTX
);
2115 int retval
= target_halt(target
);
2116 if (ERROR_OK
!= retval
)
2122 retval
= parse_uint(CMD_ARGV
[0], &wait
);
2123 if (ERROR_OK
!= retval
)
2124 return ERROR_COMMAND_SYNTAX_ERROR
;
2129 return CALL_COMMAND_HANDLER(handle_wait_halt_command
);
2132 COMMAND_HANDLER(handle_soft_reset_halt_command
)
2134 struct target
*target
= get_current_target(CMD_CTX
);
2136 LOG_USER("requesting target halt and executing a soft reset");
2138 target
->type
->soft_reset_halt(target
);
2143 COMMAND_HANDLER(handle_reset_command
)
2146 return ERROR_COMMAND_SYNTAX_ERROR
;
2148 enum target_reset_mode reset_mode
= RESET_RUN
;
2152 n
= Jim_Nvp_name2value_simple(nvp_reset_modes
, CMD_ARGV
[0]);
2153 if ((n
->name
== NULL
) || (n
->value
== RESET_UNKNOWN
)) {
2154 return ERROR_COMMAND_SYNTAX_ERROR
;
2156 reset_mode
= n
->value
;
2159 /* reset *all* targets */
2160 return target_process_reset(CMD_CTX
, reset_mode
);
2164 COMMAND_HANDLER(handle_resume_command
)
2168 return ERROR_COMMAND_SYNTAX_ERROR
;
2170 struct target
*target
= get_current_target(CMD_CTX
);
2171 target_handle_event(target
, TARGET_EVENT_OLD_pre_resume
);
2173 /* with no CMD_ARGV, resume from current pc, addr = 0,
2174 * with one arguments, addr = CMD_ARGV[0],
2175 * handle breakpoints, not debugging */
2179 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], addr
);
2183 return target_resume(target
, current
, addr
, 1, 0);
2186 COMMAND_HANDLER(handle_step_command
)
2189 return ERROR_COMMAND_SYNTAX_ERROR
;
2193 /* with no CMD_ARGV, step from current pc, addr = 0,
2194 * with one argument addr = CMD_ARGV[0],
2195 * handle breakpoints, debugging */
2200 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], addr
);
2204 struct target
*target
= get_current_target(CMD_CTX
);
2206 return target
->type
->step(target
, current_pc
, addr
, 1);
2209 static void handle_md_output(struct command_context
*cmd_ctx
,
2210 struct target
*target
, uint32_t address
, unsigned size
,
2211 unsigned count
, const uint8_t *buffer
)
2213 const unsigned line_bytecnt
= 32;
2214 unsigned line_modulo
= line_bytecnt
/ size
;
2216 char output
[line_bytecnt
* 4 + 1];
2217 unsigned output_len
= 0;
2219 const char *value_fmt
;
2221 case 4: value_fmt
= "%8.8x "; break;
2222 case 2: value_fmt
= "%4.2x "; break;
2223 case 1: value_fmt
= "%2.2x "; break;
2225 LOG_ERROR("invalid memory read size: %u", size
);
2229 for (unsigned i
= 0; i
< count
; i
++)
2231 if (i
% line_modulo
== 0)
2233 output_len
+= snprintf(output
+ output_len
,
2234 sizeof(output
) - output_len
,
2236 (unsigned)(address
+ (i
*size
)));
2240 const uint8_t *value_ptr
= buffer
+ i
* size
;
2242 case 4: value
= target_buffer_get_u32(target
, value_ptr
); break;
2243 case 2: value
= target_buffer_get_u16(target
, value_ptr
); break;
2244 case 1: value
= *value_ptr
;
2246 output_len
+= snprintf(output
+ output_len
,
2247 sizeof(output
) - output_len
,
2250 if ((i
% line_modulo
== line_modulo
- 1) || (i
== count
- 1))
2252 command_print(cmd_ctx
, "%s", output
);
2258 COMMAND_HANDLER(handle_md_command
)
2261 return ERROR_COMMAND_SYNTAX_ERROR
;
2264 switch (CMD_NAME
[2]) {
2265 case 'w': size
= 4; break;
2266 case 'h': size
= 2; break;
2267 case 'b': size
= 1; break;
2268 default: return ERROR_COMMAND_SYNTAX_ERROR
;
2271 bool physical
=strcmp(CMD_ARGV
[0], "phys")==0;
2272 int (*fn
)(struct target
*target
,
2273 uint32_t address
, uint32_t size
, uint32_t count
, uint8_t *buffer
);
2278 fn
=target_read_phys_memory
;
2281 fn
=target_read_memory
;
2283 if ((CMD_ARGC
< 1) || (CMD_ARGC
> 2))
2285 return ERROR_COMMAND_SYNTAX_ERROR
;
2289 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], address
);
2293 COMMAND_PARSE_NUMBER(uint
, CMD_ARGV
[1], count
);
2295 uint8_t *buffer
= calloc(count
, size
);
2297 struct target
*target
= get_current_target(CMD_CTX
);
2298 int retval
= fn(target
, address
, size
, count
, buffer
);
2299 if (ERROR_OK
== retval
)
2300 handle_md_output(CMD_CTX
, target
, address
, size
, count
, buffer
);
2307 COMMAND_HANDLER(handle_mw_command
)
2311 return ERROR_COMMAND_SYNTAX_ERROR
;
2313 bool physical
=strcmp(CMD_ARGV
[0], "phys")==0;
2314 int (*fn
)(struct target
*target
,
2315 uint32_t address
, uint32_t size
, uint32_t count
, uint8_t *buffer
);
2320 fn
=target_write_phys_memory
;
2323 fn
=target_write_memory
;
2325 if ((CMD_ARGC
< 2) || (CMD_ARGC
> 3))
2326 return ERROR_COMMAND_SYNTAX_ERROR
;
2329 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], address
);
2332 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], value
);
2336 COMMAND_PARSE_NUMBER(uint
, CMD_ARGV
[2], count
);
2338 struct target
*target
= get_current_target(CMD_CTX
);
2340 uint8_t value_buf
[4];
2341 switch (CMD_NAME
[2])
2345 target_buffer_set_u32(target
, value_buf
, value
);
2349 target_buffer_set_u16(target
, value_buf
, value
);
2353 value_buf
[0] = value
;
2356 return ERROR_COMMAND_SYNTAX_ERROR
;
2358 for (unsigned i
= 0; i
< count
; i
++)
2360 int retval
= fn(target
,
2361 address
+ i
* wordsize
, wordsize
, 1, value_buf
);
2362 if (ERROR_OK
!= retval
)
2371 static COMMAND_HELPER(parse_load_image_command_CMD_ARGV
, struct image
*image
,
2372 uint32_t *min_address
, uint32_t *max_address
)
2374 if (CMD_ARGC
< 1 || CMD_ARGC
> 5)
2375 return ERROR_COMMAND_SYNTAX_ERROR
;
2377 /* a base address isn't always necessary,
2378 * default to 0x0 (i.e. don't relocate) */
2382 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], addr
);
2383 image
->base_address
= addr
;
2384 image
->base_address_set
= 1;
2387 image
->base_address_set
= 0;
2389 image
->start_address_set
= 0;
2393 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[3], *min_address
);
2397 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[4], *max_address
);
2398 // use size (given) to find max (required)
2399 *max_address
+= *min_address
;
2402 if (*min_address
> *max_address
)
2403 return ERROR_COMMAND_SYNTAX_ERROR
;
2408 COMMAND_HANDLER(handle_load_image_command
)
2412 uint32_t image_size
;
2413 uint32_t min_address
= 0;
2414 uint32_t max_address
= 0xffffffff;
2418 int retval
= CALL_COMMAND_HANDLER(parse_load_image_command_CMD_ARGV
,
2419 &image
, &min_address
, &max_address
);
2420 if (ERROR_OK
!= retval
)
2423 struct target
*target
= get_current_target(CMD_CTX
);
2425 struct duration bench
;
2426 duration_start(&bench
);
2428 if (image_open(&image
, CMD_ARGV
[0], (CMD_ARGC
>= 3) ? CMD_ARGV
[2] : NULL
) != ERROR_OK
)
2435 for (i
= 0; i
< image
.num_sections
; i
++)
2437 buffer
= malloc(image
.sections
[i
].size
);
2440 command_print(CMD_CTX
,
2441 "error allocating buffer for section (%d bytes)",
2442 (int)(image
.sections
[i
].size
));
2446 if ((retval
= image_read_section(&image
, i
, 0x0, image
.sections
[i
].size
, buffer
, &buf_cnt
)) != ERROR_OK
)
2452 uint32_t offset
= 0;
2453 uint32_t length
= buf_cnt
;
2455 /* DANGER!!! beware of unsigned comparision here!!! */
2457 if ((image
.sections
[i
].base_address
+ buf_cnt
>= min_address
)&&
2458 (image
.sections
[i
].base_address
< max_address
))
2460 if (image
.sections
[i
].base_address
< min_address
)
2462 /* clip addresses below */
2463 offset
+= min_address
-image
.sections
[i
].base_address
;
2467 if (image
.sections
[i
].base_address
+ buf_cnt
> max_address
)
2469 length
-= (image
.sections
[i
].base_address
+ buf_cnt
)-max_address
;
2472 if ((retval
= target_write_buffer(target
, image
.sections
[i
].base_address
+ offset
, length
, buffer
+ offset
)) != ERROR_OK
)
2477 image_size
+= length
;
2478 command_print(CMD_CTX
, "%u bytes written at address 0x%8.8" PRIx32
"",
2479 (unsigned int)length
,
2480 image
.sections
[i
].base_address
+ offset
);
2486 if ((ERROR_OK
== retval
) && (duration_measure(&bench
) == ERROR_OK
))
2488 command_print(CMD_CTX
, "downloaded %" PRIu32
" bytes "
2489 "in %fs (%0.3f kb/s)", image_size
,
2490 duration_elapsed(&bench
), duration_kbps(&bench
, image_size
));
2493 image_close(&image
);
2499 COMMAND_HANDLER(handle_dump_image_command
)
2501 struct fileio fileio
;
2503 uint8_t buffer
[560];
2507 struct target
*target
= get_current_target(CMD_CTX
);
2511 command_print(CMD_CTX
, "usage: dump_image <filename> <address> <size>");
2516 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], address
);
2518 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[2], size
);
2520 if (fileio_open(&fileio
, CMD_ARGV
[0], FILEIO_WRITE
, FILEIO_BINARY
) != ERROR_OK
)
2525 struct duration bench
;
2526 duration_start(&bench
);
2528 int retval
= ERROR_OK
;
2531 size_t size_written
;
2532 uint32_t this_run_size
= (size
> 560) ? 560 : size
;
2533 retval
= target_read_buffer(target
, address
, this_run_size
, buffer
);
2534 if (retval
!= ERROR_OK
)
2539 retval
= fileio_write(&fileio
, this_run_size
, buffer
, &size_written
);
2540 if (retval
!= ERROR_OK
)
2545 size
-= this_run_size
;
2546 address
+= this_run_size
;
2549 if ((retvaltemp
= fileio_close(&fileio
)) != ERROR_OK
)
2552 if ((ERROR_OK
== retval
) && (duration_measure(&bench
) == ERROR_OK
))
2554 command_print(CMD_CTX
,
2555 "dumped %zu bytes in %fs (%0.3f kb/s)", fileio
.size
,
2556 duration_elapsed(&bench
), duration_kbps(&bench
, fileio
.size
));
2562 static COMMAND_HELPER(handle_verify_image_command_internal
, int verify
)
2566 uint32_t image_size
;
2569 uint32_t checksum
= 0;
2570 uint32_t mem_checksum
= 0;
2574 struct target
*target
= get_current_target(CMD_CTX
);
2578 return ERROR_COMMAND_SYNTAX_ERROR
;
2583 LOG_ERROR("no target selected");
2587 struct duration bench
;
2588 duration_start(&bench
);
2593 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], addr
);
2594 image
.base_address
= addr
;
2595 image
.base_address_set
= 1;
2599 image
.base_address_set
= 0;
2600 image
.base_address
= 0x0;
2603 image
.start_address_set
= 0;
2605 if ((retval
= image_open(&image
, CMD_ARGV
[0], (CMD_ARGC
== 3) ? CMD_ARGV
[2] : NULL
)) != ERROR_OK
)
2612 for (i
= 0; i
< image
.num_sections
; i
++)
2614 buffer
= malloc(image
.sections
[i
].size
);
2617 command_print(CMD_CTX
,
2618 "error allocating buffer for section (%d bytes)",
2619 (int)(image
.sections
[i
].size
));
2622 if ((retval
= image_read_section(&image
, i
, 0x0, image
.sections
[i
].size
, buffer
, &buf_cnt
)) != ERROR_OK
)
2630 /* calculate checksum of image */
2631 image_calculate_checksum(buffer
, buf_cnt
, &checksum
);
2633 retval
= target_checksum_memory(target
, image
.sections
[i
].base_address
, buf_cnt
, &mem_checksum
);
2634 if (retval
!= ERROR_OK
)
2640 if (checksum
!= mem_checksum
)
2642 /* failed crc checksum, fall back to a binary compare */
2645 command_print(CMD_CTX
, "checksum mismatch - attempting binary compare");
2647 data
= (uint8_t*)malloc(buf_cnt
);
2649 /* Can we use 32bit word accesses? */
2651 int count
= buf_cnt
;
2652 if ((count
% 4) == 0)
2657 retval
= target_read_memory(target
, image
.sections
[i
].base_address
, size
, count
, data
);
2658 if (retval
== ERROR_OK
)
2661 for (t
= 0; t
< buf_cnt
; t
++)
2663 if (data
[t
] != buffer
[t
])
2665 command_print(CMD_CTX
,
2666 "Verify operation failed address 0x%08x. Was 0x%02x instead of 0x%02x\n",
2667 (unsigned)(t
+ image
.sections
[i
].base_address
),
2672 retval
= ERROR_FAIL
;
2686 command_print(CMD_CTX
, "address 0x%08" PRIx32
" length 0x%08zx",
2687 image
.sections
[i
].base_address
,
2692 image_size
+= buf_cnt
;
2695 if ((ERROR_OK
== retval
) && (duration_measure(&bench
) == ERROR_OK
))
2697 command_print(CMD_CTX
, "verified %" PRIu32
" bytes "
2698 "in %fs (%0.3f kb/s)", image_size
,
2699 duration_elapsed(&bench
), duration_kbps(&bench
, image_size
));
2702 image_close(&image
);
2707 COMMAND_HANDLER(handle_verify_image_command
)
2709 return CALL_COMMAND_HANDLER(handle_verify_image_command_internal
, 1);
2712 COMMAND_HANDLER(handle_test_image_command
)
2714 return CALL_COMMAND_HANDLER(handle_verify_image_command_internal
, 0);
2717 static int handle_bp_command_list(struct command_context
*cmd_ctx
)
2719 struct target
*target
= get_current_target(cmd_ctx
);
2720 struct breakpoint
*breakpoint
= target
->breakpoints
;
2723 if (breakpoint
->type
== BKPT_SOFT
)
2725 char* buf
= buf_to_str(breakpoint
->orig_instr
,
2726 breakpoint
->length
, 16);
2727 command_print(cmd_ctx
, "0x%8.8" PRIx32
", 0x%x, %i, 0x%s",
2728 breakpoint
->address
,
2730 breakpoint
->set
, buf
);
2735 command_print(cmd_ctx
, "0x%8.8" PRIx32
", 0x%x, %i",
2736 breakpoint
->address
,
2737 breakpoint
->length
, breakpoint
->set
);
2740 breakpoint
= breakpoint
->next
;
2745 static int handle_bp_command_set(struct command_context
*cmd_ctx
,
2746 uint32_t addr
, uint32_t length
, int hw
)
2748 struct target
*target
= get_current_target(cmd_ctx
);
2749 int retval
= breakpoint_add(target
, addr
, length
, hw
);
2750 if (ERROR_OK
== retval
)
2751 command_print(cmd_ctx
, "breakpoint set at 0x%8.8" PRIx32
"", addr
);
2753 LOG_ERROR("Failure setting breakpoint");
2757 COMMAND_HANDLER(handle_bp_command
)
2760 return handle_bp_command_list(CMD_CTX
);
2762 if (CMD_ARGC
< 2 || CMD_ARGC
> 3)
2764 command_print(CMD_CTX
, "usage: bp <address> <length> ['hw']");
2765 return ERROR_COMMAND_SYNTAX_ERROR
;
2769 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], addr
);
2771 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], length
);
2776 if (strcmp(CMD_ARGV
[2], "hw") == 0)
2779 return ERROR_COMMAND_SYNTAX_ERROR
;
2782 return handle_bp_command_set(CMD_CTX
, addr
, length
, hw
);
2785 COMMAND_HANDLER(handle_rbp_command
)
2788 return ERROR_COMMAND_SYNTAX_ERROR
;
2791 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], addr
);
2793 struct target
*target
= get_current_target(CMD_CTX
);
2794 breakpoint_remove(target
, addr
);
2799 COMMAND_HANDLER(handle_wp_command
)
2801 struct target
*target
= get_current_target(CMD_CTX
);
2805 struct watchpoint
*watchpoint
= target
->watchpoints
;
2809 command_print(CMD_CTX
, "address: 0x%8.8" PRIx32
2810 ", len: 0x%8.8" PRIx32
2811 ", r/w/a: %i, value: 0x%8.8" PRIx32
2812 ", mask: 0x%8.8" PRIx32
,
2813 watchpoint
->address
,
2815 (int)watchpoint
->rw
,
2818 watchpoint
= watchpoint
->next
;
2823 enum watchpoint_rw type
= WPT_ACCESS
;
2825 uint32_t length
= 0;
2826 uint32_t data_value
= 0x0;
2827 uint32_t data_mask
= 0xffffffff;
2832 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[4], data_mask
);
2835 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[3], data_value
);
2838 switch (CMD_ARGV
[2][0])
2850 LOG_ERROR("invalid watchpoint mode ('%c')", CMD_ARGV
[2][0]);
2851 return ERROR_COMMAND_SYNTAX_ERROR
;
2855 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], length
);
2856 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], addr
);
2860 command_print(CMD_CTX
, "usage: wp [address length "
2861 "[(r|w|a) [value [mask]]]]");
2862 return ERROR_COMMAND_SYNTAX_ERROR
;
2865 int retval
= watchpoint_add(target
, addr
, length
, type
,
2866 data_value
, data_mask
);
2867 if (ERROR_OK
!= retval
)
2868 LOG_ERROR("Failure setting watchpoints");
2873 COMMAND_HANDLER(handle_rwp_command
)
2876 return ERROR_COMMAND_SYNTAX_ERROR
;
2879 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], addr
);
2881 struct target
*target
= get_current_target(CMD_CTX
);
2882 watchpoint_remove(target
, addr
);
2889 * Translate a virtual address to a physical address.
2891 * The low-level target implementation must have logged a detailed error
2892 * which is forwarded to telnet/GDB session.
2894 COMMAND_HANDLER(handle_virt2phys_command
)
2897 return ERROR_COMMAND_SYNTAX_ERROR
;
2900 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], va
);
2903 struct target
*target
= get_current_target(CMD_CTX
);
2904 int retval
= target
->type
->virt2phys(target
, va
, &pa
);
2905 if (retval
== ERROR_OK
)
2906 command_print(CMD_CTX
, "Physical address 0x%08" PRIx32
"", pa
);
2911 static void writeData(FILE *f
, const void *data
, size_t len
)
2913 size_t written
= fwrite(data
, 1, len
, f
);
2915 LOG_ERROR("failed to write %zu bytes: %s", len
, strerror(errno
));
2918 static void writeLong(FILE *f
, int l
)
2921 for (i
= 0; i
< 4; i
++)
2923 char c
= (l
>> (i
*8))&0xff;
2924 writeData(f
, &c
, 1);
2929 static void writeString(FILE *f
, char *s
)
2931 writeData(f
, s
, strlen(s
));
2934 /* Dump a gmon.out histogram file. */
2935 static void writeGmon(uint32_t *samples
, uint32_t sampleNum
, const char *filename
)
2938 FILE *f
= fopen(filename
, "w");
2941 writeString(f
, "gmon");
2942 writeLong(f
, 0x00000001); /* Version */
2943 writeLong(f
, 0); /* padding */
2944 writeLong(f
, 0); /* padding */
2945 writeLong(f
, 0); /* padding */
2947 uint8_t zero
= 0; /* GMON_TAG_TIME_HIST */
2948 writeData(f
, &zero
, 1);
2950 /* figure out bucket size */
2951 uint32_t min
= samples
[0];
2952 uint32_t max
= samples
[0];
2953 for (i
= 0; i
< sampleNum
; i
++)
2955 if (min
> samples
[i
])
2959 if (max
< samples
[i
])
2965 int addressSpace
= (max
-min
+ 1);
2967 static const uint32_t maxBuckets
= 256 * 1024; /* maximum buckets. */
2968 uint32_t length
= addressSpace
;
2969 if (length
> maxBuckets
)
2971 length
= maxBuckets
;
2973 int *buckets
= malloc(sizeof(int)*length
);
2974 if (buckets
== NULL
)
2979 memset(buckets
, 0, sizeof(int)*length
);
2980 for (i
= 0; i
< sampleNum
;i
++)
2982 uint32_t address
= samples
[i
];
2983 long long a
= address
-min
;
2984 long long b
= length
-1;
2985 long long c
= addressSpace
-1;
2986 int index
= (a
*b
)/c
; /* danger!!!! int32 overflows */
2990 /* append binary memory gmon.out &profile_hist_hdr ((char*)&profile_hist_hdr + sizeof(struct gmon_hist_hdr)) */
2991 writeLong(f
, min
); /* low_pc */
2992 writeLong(f
, max
); /* high_pc */
2993 writeLong(f
, length
); /* # of samples */
2994 writeLong(f
, 64000000); /* 64MHz */
2995 writeString(f
, "seconds");
2996 for (i
= 0; i
< (15-strlen("seconds")); i
++)
2997 writeData(f
, &zero
, 1);
2998 writeString(f
, "s");
3000 /*append binary memory gmon.out profile_hist_data (profile_hist_data + profile_hist_hdr.hist_size) */
3002 char *data
= malloc(2*length
);
3005 for (i
= 0; i
< length
;i
++)
3014 data
[i
*2 + 1]=(val
>> 8)&0xff;
3017 writeData(f
, data
, length
* 2);
3027 /* profiling samples the CPU PC as quickly as OpenOCD is able, which will be used as a random sampling of PC */
3028 COMMAND_HANDLER(handle_profile_command
)
3030 struct target
*target
= get_current_target(CMD_CTX
);
3031 struct timeval timeout
, now
;
3033 gettimeofday(&timeout
, NULL
);
3036 return ERROR_COMMAND_SYNTAX_ERROR
;
3039 COMMAND_PARSE_NUMBER(uint
, CMD_ARGV
[0], offset
);
3041 timeval_add_time(&timeout
, offset
, 0);
3043 command_print(CMD_CTX
, "Starting profiling. Halting and resuming the target as often as we can...");
3045 static const int maxSample
= 10000;
3046 uint32_t *samples
= malloc(sizeof(uint32_t)*maxSample
);
3047 if (samples
== NULL
)
3051 /* hopefully it is safe to cache! We want to stop/restart as quickly as possible. */
3052 struct reg
*reg
= register_get_by_name(target
->reg_cache
, "pc", 1);
3057 target_poll(target
);
3058 if (target
->state
== TARGET_HALTED
)
3060 uint32_t t
=*((uint32_t *)reg
->value
);
3061 samples
[numSamples
++]=t
;
3062 retval
= target_resume(target
, 1, 0, 0, 0); /* current pc, addr = 0, do not handle breakpoints, not debugging */
3063 target_poll(target
);
3064 alive_sleep(10); /* sleep 10ms, i.e. <100 samples/second. */
3065 } else if (target
->state
== TARGET_RUNNING
)
3067 /* We want to quickly sample the PC. */
3068 if ((retval
= target_halt(target
)) != ERROR_OK
)
3075 command_print(CMD_CTX
, "Target not halted or running");
3079 if (retval
!= ERROR_OK
)
3084 gettimeofday(&now
, NULL
);
3085 if ((numSamples
>= maxSample
) || ((now
.tv_sec
>= timeout
.tv_sec
) && (now
.tv_usec
>= timeout
.tv_usec
)))
3087 command_print(CMD_CTX
, "Profiling completed. %d samples.", numSamples
);
3088 if ((retval
= target_poll(target
)) != ERROR_OK
)
3093 if (target
->state
== TARGET_HALTED
)
3095 target_resume(target
, 1, 0, 0, 0); /* current pc, addr = 0, do not handle breakpoints, not debugging */
3097 if ((retval
= target_poll(target
)) != ERROR_OK
)
3102 writeGmon(samples
, numSamples
, CMD_ARGV
[1]);
3103 command_print(CMD_CTX
, "Wrote %s", CMD_ARGV
[1]);
3112 static int new_int_array_element(Jim_Interp
* interp
, const char *varname
, int idx
, uint32_t val
)
3115 Jim_Obj
*nameObjPtr
, *valObjPtr
;
3118 namebuf
= alloc_printf("%s(%d)", varname
, idx
);
3122 nameObjPtr
= Jim_NewStringObj(interp
, namebuf
, -1);
3123 valObjPtr
= Jim_NewIntObj(interp
, val
);
3124 if (!nameObjPtr
|| !valObjPtr
)
3130 Jim_IncrRefCount(nameObjPtr
);
3131 Jim_IncrRefCount(valObjPtr
);
3132 result
= Jim_SetVariable(interp
, nameObjPtr
, valObjPtr
);
3133 Jim_DecrRefCount(interp
, nameObjPtr
);
3134 Jim_DecrRefCount(interp
, valObjPtr
);
3136 /* printf("%s(%d) <= 0%08x\n", varname, idx, val); */
3140 static int jim_mem2array(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
3142 struct command_context
*context
;
3143 struct target
*target
;
3145 context
= Jim_GetAssocData(interp
, "context");
3146 if (context
== NULL
)
3148 LOG_ERROR("mem2array: no command context");
3151 target
= get_current_target(context
);
3154 LOG_ERROR("mem2array: no current target");
3158 return target_mem2array(interp
, target
, argc
-1, argv
+ 1);
3161 static int target_mem2array(Jim_Interp
*interp
, struct target
*target
, int argc
, Jim_Obj
*const *argv
)
3169 const char *varname
;
3173 /* argv[1] = name of array to receive the data
3174 * argv[2] = desired width
3175 * argv[3] = memory address
3176 * argv[4] = count of times to read
3179 Jim_WrongNumArgs(interp
, 1, argv
, "varname width addr nelems");
3182 varname
= Jim_GetString(argv
[0], &len
);
3183 /* given "foo" get space for worse case "foo(%d)" .. add 20 */
3185 e
= Jim_GetLong(interp
, argv
[1], &l
);
3191 e
= Jim_GetLong(interp
, argv
[2], &l
);
3196 e
= Jim_GetLong(interp
, argv
[3], &l
);
3212 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3213 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "Invalid width param, must be 8/16/32", NULL
);
3217 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3218 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "mem2array: zero width read?", NULL
);
3221 if ((addr
+ (len
* width
)) < addr
) {
3222 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3223 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "mem2array: addr + len - wraps to zero?", NULL
);
3226 /* absurd transfer size? */
3228 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3229 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "mem2array: absurd > 64K item request", NULL
);
3234 ((width
== 2) && ((addr
& 1) == 0)) ||
3235 ((width
== 4) && ((addr
& 3) == 0))) {
3239 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3240 sprintf(buf
, "mem2array address: 0x%08" PRIx32
" is not aligned for %" PRId32
" byte reads",
3243 Jim_AppendStrings(interp
, Jim_GetResult(interp
), buf
, NULL
);
3252 size_t buffersize
= 4096;
3253 uint8_t *buffer
= malloc(buffersize
);
3260 /* Slurp... in buffer size chunks */
3262 count
= len
; /* in objects.. */
3263 if (count
> (buffersize
/width
)) {
3264 count
= (buffersize
/width
);
3267 retval
= target_read_memory(target
, addr
, width
, count
, buffer
);
3268 if (retval
!= ERROR_OK
) {
3270 LOG_ERROR("mem2array: Read @ 0x%08x, w=%d, cnt=%d, failed",
3274 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3275 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "mem2array: cannot read memory", NULL
);
3279 v
= 0; /* shut up gcc */
3280 for (i
= 0 ;i
< count
;i
++, n
++) {
3283 v
= target_buffer_get_u32(target
, &buffer
[i
*width
]);
3286 v
= target_buffer_get_u16(target
, &buffer
[i
*width
]);
3289 v
= buffer
[i
] & 0x0ff;
3292 new_int_array_element(interp
, varname
, n
, v
);
3300 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3305 static int get_int_array_element(Jim_Interp
* interp
, const char *varname
, int idx
, uint32_t *val
)
3308 Jim_Obj
*nameObjPtr
, *valObjPtr
;
3312 namebuf
= alloc_printf("%s(%d)", varname
, idx
);
3316 nameObjPtr
= Jim_NewStringObj(interp
, namebuf
, -1);
3323 Jim_IncrRefCount(nameObjPtr
);
3324 valObjPtr
= Jim_GetVariable(interp
, nameObjPtr
, JIM_ERRMSG
);
3325 Jim_DecrRefCount(interp
, nameObjPtr
);
3327 if (valObjPtr
== NULL
)
3330 result
= Jim_GetLong(interp
, valObjPtr
, &l
);
3331 /* printf("%s(%d) => 0%08x\n", varname, idx, val); */
3336 static int jim_array2mem(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
3338 struct command_context
*context
;
3339 struct target
*target
;
3341 context
= Jim_GetAssocData(interp
, "context");
3342 if (context
== NULL
) {
3343 LOG_ERROR("array2mem: no command context");
3346 target
= get_current_target(context
);
3347 if (target
== NULL
) {
3348 LOG_ERROR("array2mem: no current target");
3352 return target_array2mem(interp
,target
, argc
-1, argv
+ 1);
3354 static int target_array2mem(Jim_Interp
*interp
, struct target
*target
, int argc
, Jim_Obj
*const *argv
)
3362 const char *varname
;
3366 /* argv[1] = name of array to get the data
3367 * argv[2] = desired width
3368 * argv[3] = memory address
3369 * argv[4] = count to write
3372 Jim_WrongNumArgs(interp
, 0, argv
, "varname width addr nelems");
3375 varname
= Jim_GetString(argv
[0], &len
);
3376 /* given "foo" get space for worse case "foo(%d)" .. add 20 */
3378 e
= Jim_GetLong(interp
, argv
[1], &l
);
3384 e
= Jim_GetLong(interp
, argv
[2], &l
);
3389 e
= Jim_GetLong(interp
, argv
[3], &l
);
3405 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3406 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "Invalid width param, must be 8/16/32", NULL
);
3410 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3411 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "array2mem: zero width read?", NULL
);
3414 if ((addr
+ (len
* width
)) < addr
) {
3415 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3416 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "array2mem: addr + len - wraps to zero?", NULL
);
3419 /* absurd transfer size? */
3421 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3422 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "array2mem: absurd > 64K item request", NULL
);
3427 ((width
== 2) && ((addr
& 1) == 0)) ||
3428 ((width
== 4) && ((addr
& 3) == 0))) {
3432 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3433 sprintf(buf
, "array2mem address: 0x%08x is not aligned for %d byte reads",
3436 Jim_AppendStrings(interp
, Jim_GetResult(interp
), buf
, NULL
);
3447 size_t buffersize
= 4096;
3448 uint8_t *buffer
= malloc(buffersize
);
3453 /* Slurp... in buffer size chunks */
3455 count
= len
; /* in objects.. */
3456 if (count
> (buffersize
/width
)) {
3457 count
= (buffersize
/width
);
3460 v
= 0; /* shut up gcc */
3461 for (i
= 0 ;i
< count
;i
++, n
++) {
3462 get_int_array_element(interp
, varname
, n
, &v
);
3465 target_buffer_set_u32(target
, &buffer
[i
*width
], v
);
3468 target_buffer_set_u16(target
, &buffer
[i
*width
], v
);
3471 buffer
[i
] = v
& 0x0ff;
3477 retval
= target_write_memory(target
, addr
, width
, count
, buffer
);
3478 if (retval
!= ERROR_OK
) {
3480 LOG_ERROR("array2mem: Write @ 0x%08x, w=%d, cnt=%d, failed",
3484 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3485 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "array2mem: cannot read memory", NULL
);
3493 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3498 void target_all_handle_event(enum target_event e
)
3500 struct target
*target
;
3502 LOG_DEBUG("**all*targets: event: %d, %s",
3504 Jim_Nvp_value2name_simple(nvp_target_event
, e
)->name
);
3506 target
= all_targets
;
3508 target_handle_event(target
, e
);
3509 target
= target
->next
;
3514 /* FIX? should we propagate errors here rather than printing them
3517 void target_handle_event(struct target
*target
, enum target_event e
)
3519 struct target_event_action
*teap
;
3521 for (teap
= target
->event_action
; teap
!= NULL
; teap
= teap
->next
) {
3522 if (teap
->event
== e
) {
3523 LOG_DEBUG("target: (%d) %s (%s) event: %d (%s) action: %s",
3524 target
->target_number
,
3525 target_name(target
),
3526 target_type_name(target
),
3528 Jim_Nvp_value2name_simple(nvp_target_event
, e
)->name
,
3529 Jim_GetString(teap
->body
, NULL
));
3530 if (Jim_EvalObj(teap
->interp
, teap
->body
) != JIM_OK
)
3532 Jim_PrintErrorMessage(teap
->interp
);
3539 * Returns true only if the target has a handler for the specified event.
3541 bool target_has_event_action(struct target
*target
, enum target_event event
)
3543 struct target_event_action
*teap
;
3545 for (teap
= target
->event_action
; teap
!= NULL
; teap
= teap
->next
) {
3546 if (teap
->event
== event
)
3552 enum target_cfg_param
{
3555 TCFG_WORK_AREA_VIRT
,
3556 TCFG_WORK_AREA_PHYS
,
3557 TCFG_WORK_AREA_SIZE
,
3558 TCFG_WORK_AREA_BACKUP
,
3561 TCFG_CHAIN_POSITION
,
3564 static Jim_Nvp nvp_config_opts
[] = {
3565 { .name
= "-type", .value
= TCFG_TYPE
},
3566 { .name
= "-event", .value
= TCFG_EVENT
},
3567 { .name
= "-work-area-virt", .value
= TCFG_WORK_AREA_VIRT
},
3568 { .name
= "-work-area-phys", .value
= TCFG_WORK_AREA_PHYS
},
3569 { .name
= "-work-area-size", .value
= TCFG_WORK_AREA_SIZE
},
3570 { .name
= "-work-area-backup", .value
= TCFG_WORK_AREA_BACKUP
},
3571 { .name
= "-endian" , .value
= TCFG_ENDIAN
},
3572 { .name
= "-variant", .value
= TCFG_VARIANT
},
3573 { .name
= "-chain-position", .value
= TCFG_CHAIN_POSITION
},
3575 { .name
= NULL
, .value
= -1 }
3578 static int target_configure(Jim_GetOptInfo
*goi
, struct target
*target
)
3586 /* parse config or cget options ... */
3587 while (goi
->argc
> 0) {
3588 Jim_SetEmptyResult(goi
->interp
);
3589 /* Jim_GetOpt_Debug(goi); */
3591 if (target
->type
->target_jim_configure
) {
3592 /* target defines a configure function */
3593 /* target gets first dibs on parameters */
3594 e
= (*(target
->type
->target_jim_configure
))(target
, goi
);
3603 /* otherwise we 'continue' below */
3605 e
= Jim_GetOpt_Nvp(goi
, nvp_config_opts
, &n
);
3607 Jim_GetOpt_NvpUnknown(goi
, nvp_config_opts
, 0);
3613 if (goi
->isconfigure
) {
3614 Jim_SetResult_sprintf(goi
->interp
,
3615 "not settable: %s", n
->name
);
3619 if (goi
->argc
!= 0) {
3620 Jim_WrongNumArgs(goi
->interp
,
3621 goi
->argc
, goi
->argv
,
3626 Jim_SetResultString(goi
->interp
,
3627 target_type_name(target
), -1);
3631 if (goi
->argc
== 0) {
3632 Jim_WrongNumArgs(goi
->interp
, goi
->argc
, goi
->argv
, "-event ?event-name? ...");
3636 e
= Jim_GetOpt_Nvp(goi
, nvp_target_event
, &n
);
3638 Jim_GetOpt_NvpUnknown(goi
, nvp_target_event
, 1);
3642 if (goi
->isconfigure
) {
3643 if (goi
->argc
!= 1) {
3644 Jim_WrongNumArgs(goi
->interp
, goi
->argc
, goi
->argv
, "-event ?event-name? ?EVENT-BODY?");
3648 if (goi
->argc
!= 0) {
3649 Jim_WrongNumArgs(goi
->interp
, goi
->argc
, goi
->argv
, "-event ?event-name?");
3655 struct target_event_action
*teap
;
3657 teap
= target
->event_action
;
3658 /* replace existing? */
3660 if (teap
->event
== (enum target_event
)n
->value
) {
3666 if (goi
->isconfigure
) {
3667 bool replace
= true;
3670 teap
= calloc(1, sizeof(*teap
));
3673 teap
->event
= n
->value
;
3674 teap
->interp
= goi
->interp
;
3675 Jim_GetOpt_Obj(goi
, &o
);
3677 Jim_DecrRefCount(teap
->interp
, teap
->body
);
3679 teap
->body
= Jim_DuplicateObj(goi
->interp
, o
);
3682 * Tcl/TK - "tk events" have a nice feature.
3683 * See the "BIND" command.
3684 * We should support that here.
3685 * You can specify %X and %Y in the event code.
3686 * The idea is: %T - target name.
3687 * The idea is: %N - target number
3688 * The idea is: %E - event name.
3690 Jim_IncrRefCount(teap
->body
);
3694 /* add to head of event list */
3695 teap
->next
= target
->event_action
;
3696 target
->event_action
= teap
;
3698 Jim_SetEmptyResult(goi
->interp
);
3702 Jim_SetEmptyResult(goi
->interp
);
3704 Jim_SetResult(goi
->interp
, Jim_DuplicateObj(goi
->interp
, teap
->body
));
3711 case TCFG_WORK_AREA_VIRT
:
3712 if (goi
->isconfigure
) {
3713 target_free_all_working_areas(target
);
3714 e
= Jim_GetOpt_Wide(goi
, &w
);
3718 target
->working_area_virt
= w
;
3719 target
->working_area_virt_spec
= true;
3721 if (goi
->argc
!= 0) {
3725 Jim_SetResult(goi
->interp
, Jim_NewIntObj(goi
->interp
, target
->working_area_virt
));
3729 case TCFG_WORK_AREA_PHYS
:
3730 if (goi
->isconfigure
) {
3731 target_free_all_working_areas(target
);
3732 e
= Jim_GetOpt_Wide(goi
, &w
);
3736 target
->working_area_phys
= w
;
3737 target
->working_area_phys_spec
= true;
3739 if (goi
->argc
!= 0) {
3743 Jim_SetResult(goi
->interp
, Jim_NewIntObj(goi
->interp
, target
->working_area_phys
));
3747 case TCFG_WORK_AREA_SIZE
:
3748 if (goi
->isconfigure
) {
3749 target_free_all_working_areas(target
);
3750 e
= Jim_GetOpt_Wide(goi
, &w
);
3754 target
->working_area_size
= w
;
3756 if (goi
->argc
!= 0) {
3760 Jim_SetResult(goi
->interp
, Jim_NewIntObj(goi
->interp
, target
->working_area_size
));
3764 case TCFG_WORK_AREA_BACKUP
:
3765 if (goi
->isconfigure
) {
3766 target_free_all_working_areas(target
);
3767 e
= Jim_GetOpt_Wide(goi
, &w
);
3771 /* make this exactly 1 or 0 */
3772 target
->backup_working_area
= (!!w
);
3774 if (goi
->argc
!= 0) {
3778 Jim_SetResult(goi
->interp
, Jim_NewIntObj(goi
->interp
, target
->backup_working_area
));
3779 /* loop for more e*/
3783 if (goi
->isconfigure
) {
3784 e
= Jim_GetOpt_Nvp(goi
, nvp_target_endian
, &n
);
3786 Jim_GetOpt_NvpUnknown(goi
, nvp_target_endian
, 1);
3789 target
->endianness
= n
->value
;
3791 if (goi
->argc
!= 0) {
3795 n
= Jim_Nvp_value2name_simple(nvp_target_endian
, target
->endianness
);
3796 if (n
->name
== NULL
) {
3797 target
->endianness
= TARGET_LITTLE_ENDIAN
;
3798 n
= Jim_Nvp_value2name_simple(nvp_target_endian
, target
->endianness
);
3800 Jim_SetResultString(goi
->interp
, n
->name
, -1);
3805 if (goi
->isconfigure
) {
3806 if (goi
->argc
< 1) {
3807 Jim_SetResult_sprintf(goi
->interp
,
3812 if (target
->variant
) {
3813 free((void *)(target
->variant
));
3815 e
= Jim_GetOpt_String(goi
, &cp
, NULL
);
3816 target
->variant
= strdup(cp
);
3818 if (goi
->argc
!= 0) {
3822 Jim_SetResultString(goi
->interp
, target
->variant
,-1);
3825 case TCFG_CHAIN_POSITION
:
3826 if (goi
->isconfigure
) {
3828 struct jtag_tap
*tap
;
3829 target_free_all_working_areas(target
);
3830 e
= Jim_GetOpt_Obj(goi
, &o
);
3834 tap
= jtag_tap_by_jim_obj(goi
->interp
, o
);
3838 /* make this exactly 1 or 0 */
3841 if (goi
->argc
!= 0) {
3845 Jim_SetResultString(goi
->interp
, target
->tap
->dotted_name
, -1);
3846 /* loop for more e*/
3849 } /* while (goi->argc) */
3852 /* done - we return */
3856 static int jim_target_configure(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
3859 Jim_GetOpt_Setup(&goi
, interp
, argc
- 1, argv
+ 1);
3860 goi
.isconfigure
= strcmp(Jim_GetString(argv
[0], NULL
), "configure") == 0;
3861 int need_args
= 1 + goi
.isconfigure
;
3862 if (goi
.argc
< need_args
)
3864 Jim_WrongNumArgs(goi
.interp
, goi
.argc
, goi
.argv
,
3866 ? "missing: -option VALUE ..."
3867 : "missing: -option ...");
3870 struct target
*target
= Jim_CmdPrivData(goi
.interp
);
3871 return target_configure(&goi
, target
);
3874 static int jim_target_mw(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
3876 const char *cmd_name
= Jim_GetString(argv
[0], NULL
);
3879 Jim_GetOpt_Setup(&goi
, interp
, argc
- 1, argv
+ 1);
3881 if (goi
.argc
!= 2 && goi
.argc
!= 3)
3883 Jim_SetResult_sprintf(goi
.interp
,
3884 "usage: %s <address> <data> [<count>]", cmd_name
);
3889 int e
= Jim_GetOpt_Wide(&goi
, &a
);
3894 e
= Jim_GetOpt_Wide(&goi
, &b
);
3901 e
= Jim_GetOpt_Wide(&goi
, &c
);
3906 struct target
*target
= Jim_CmdPrivData(goi
.interp
);
3907 uint8_t target_buf
[32];
3908 if (strcasecmp(cmd_name
, "mww") == 0) {
3909 target_buffer_set_u32(target
, target_buf
, b
);
3912 else if (strcasecmp(cmd_name
, "mwh") == 0) {
3913 target_buffer_set_u16(target
, target_buf
, b
);
3916 else if (strcasecmp(cmd_name
, "mwb") == 0) {
3917 target_buffer_set_u8(target
, target_buf
, b
);
3920 LOG_ERROR("command '%s' unknown: ", cmd_name
);
3924 for (jim_wide x
= 0; x
< c
; x
++)
3926 e
= target_write_memory(target
, a
, b
, 1, target_buf
);
3929 Jim_SetResult_sprintf(interp
,
3930 "Error writing @ 0x%08x: %d\n", (int)(a
), e
);
3939 static int jim_target_md(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
3941 const char *cmd_name
= Jim_GetString(argv
[0], NULL
);
3944 Jim_GetOpt_Setup(&goi
, interp
, argc
- 1, argv
+ 1);
3946 if ((goi
.argc
== 2) || (goi
.argc
== 3))
3948 Jim_SetResult_sprintf(goi
.interp
,
3949 "usage: %s <address> [<count>]", cmd_name
);
3954 int e
= Jim_GetOpt_Wide(&goi
, &a
);
3960 e
= Jim_GetOpt_Wide(&goi
, &c
);
3967 jim_wide b
= 1; /* shut up gcc */
3968 if (strcasecmp(cmd_name
, "mdw") == 0)
3970 else if (strcasecmp(cmd_name
, "mdh") == 0)
3972 else if (strcasecmp(cmd_name
, "mdb") == 0)
3975 LOG_ERROR("command '%s' unknown: ", cmd_name
);
3979 /* convert count to "bytes" */
3982 struct target
*target
= Jim_CmdPrivData(goi
.interp
);
3983 uint8_t target_buf
[32];
3990 e
= target_read_memory(target
, a
, b
, y
/ b
, target_buf
);
3991 if (e
!= ERROR_OK
) {
3992 Jim_SetResult_sprintf(interp
, "error reading target @ 0x%08lx", (int)(a
));
3996 Jim_fprintf(interp
, interp
->cookie_stdout
, "0x%08x ", (int)(a
));
3999 for (x
= 0; x
< 16 && x
< y
; x
+= 4)
4001 z
= target_buffer_get_u32(target
, &(target_buf
[ x
* 4 ]));
4002 Jim_fprintf(interp
, interp
->cookie_stdout
, "%08x ", (int)(z
));
4004 for (; (x
< 16) ; x
+= 4) {
4005 Jim_fprintf(interp
, interp
->cookie_stdout
, " ");
4009 for (x
= 0; x
< 16 && x
< y
; x
+= 2)
4011 z
= target_buffer_get_u16(target
, &(target_buf
[ x
* 2 ]));
4012 Jim_fprintf(interp
, interp
->cookie_stdout
, "%04x ", (int)(z
));
4014 for (; (x
< 16) ; x
+= 2) {
4015 Jim_fprintf(interp
, interp
->cookie_stdout
, " ");
4020 for (x
= 0 ; (x
< 16) && (x
< y
) ; x
+= 1) {
4021 z
= target_buffer_get_u8(target
, &(target_buf
[ x
* 4 ]));
4022 Jim_fprintf(interp
, interp
->cookie_stdout
, "%02x ", (int)(z
));
4024 for (; (x
< 16) ; x
+= 1) {
4025 Jim_fprintf(interp
, interp
->cookie_stdout
, " ");
4029 /* ascii-ify the bytes */
4030 for (x
= 0 ; x
< y
; x
++) {
4031 if ((target_buf
[x
] >= 0x20) &&
4032 (target_buf
[x
] <= 0x7e)) {
4036 target_buf
[x
] = '.';
4041 target_buf
[x
] = ' ';
4046 /* print - with a newline */
4047 Jim_fprintf(interp
, interp
->cookie_stdout
, "%s\n", target_buf
);
4055 static int jim_target_mem2array(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4057 struct target
*target
= Jim_CmdPrivData(interp
);
4058 return target_mem2array(interp
, target
, argc
- 1, argv
+ 1);
4061 static int jim_target_array2mem(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4063 struct target
*target
= Jim_CmdPrivData(interp
);
4064 return target_array2mem(interp
, target
, argc
- 1, argv
+ 1);
4067 static int jim_target_tap_disabled(Jim_Interp
*interp
)
4069 Jim_SetResult_sprintf(interp
, "[TAP is disabled]");
4073 static int jim_target_examine(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4077 Jim_WrongNumArgs(interp
, 1, argv
, "[no parameters]");
4080 struct target
*target
= Jim_CmdPrivData(interp
);
4081 if (!target
->tap
->enabled
)
4082 return jim_target_tap_disabled(interp
);
4084 int e
= target
->type
->examine(target
);
4087 Jim_SetResult_sprintf(interp
, "examine-fails: %d", e
);
4093 static int jim_target_poll(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4097 Jim_WrongNumArgs(interp
, 1, argv
, "[no parameters]");
4100 struct target
*target
= Jim_CmdPrivData(interp
);
4101 if (!target
->tap
->enabled
)
4102 return jim_target_tap_disabled(interp
);
4105 if (!(target_was_examined(target
))) {
4106 e
= ERROR_TARGET_NOT_EXAMINED
;
4108 e
= target
->type
->poll(target
);
4112 Jim_SetResult_sprintf(interp
, "poll-fails: %d", e
);
4118 static int jim_target_reset(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4121 Jim_GetOpt_Setup(&goi
, interp
, argc
- 1, argv
+ 1);
4125 Jim_WrongNumArgs(interp
, 0, argv
,
4126 "([tT]|[fF]|assert|deassert) BOOL");
4131 int e
= Jim_GetOpt_Nvp(&goi
, nvp_assert
, &n
);
4134 Jim_GetOpt_NvpUnknown(&goi
, nvp_assert
, 1);
4137 /* the halt or not param */
4139 e
= Jim_GetOpt_Wide(&goi
, &a
);
4143 struct target
*target
= Jim_CmdPrivData(goi
.interp
);
4144 if (!target
->tap
->enabled
)
4145 return jim_target_tap_disabled(interp
);
4146 if (!target
->type
->assert_reset
|| !target
->type
->deassert_reset
)
4148 Jim_SetResult_sprintf(interp
,
4149 "No target-specific reset for %s",
4150 target_name(target
));
4153 /* determine if we should halt or not. */
4154 target
->reset_halt
= !!a
;
4155 /* When this happens - all workareas are invalid. */
4156 target_free_all_working_areas_restore(target
, 0);
4159 if (n
->value
== NVP_ASSERT
) {
4160 e
= target
->type
->assert_reset(target
);
4162 e
= target
->type
->deassert_reset(target
);
4164 return (e
== ERROR_OK
) ? JIM_OK
: JIM_ERR
;
4167 static int jim_target_halt(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4170 Jim_WrongNumArgs(interp
, 1, argv
, "[no parameters]");
4173 struct target
*target
= Jim_CmdPrivData(interp
);
4174 if (!target
->tap
->enabled
)
4175 return jim_target_tap_disabled(interp
);
4176 int e
= target
->type
->halt(target
);
4177 return (e
== ERROR_OK
) ? JIM_OK
: JIM_ERR
;
4180 static int jim_target_wait_state(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4183 Jim_GetOpt_Setup(&goi
, interp
, argc
- 1, argv
+ 1);
4185 /* params: <name> statename timeoutmsecs */
4188 const char *cmd_name
= Jim_GetString(argv
[0], NULL
);
4189 Jim_SetResult_sprintf(goi
.interp
,
4190 "%s <state_name> <timeout_in_msec>", cmd_name
);
4195 int e
= Jim_GetOpt_Nvp(&goi
, nvp_target_state
, &n
);
4197 Jim_GetOpt_NvpUnknown(&goi
, nvp_target_state
,1);
4201 e
= Jim_GetOpt_Wide(&goi
, &a
);
4205 struct target
*target
= Jim_CmdPrivData(interp
);
4206 if (!target
->tap
->enabled
)
4207 return jim_target_tap_disabled(interp
);
4209 e
= target_wait_state(target
, n
->value
, a
);
4212 Jim_SetResult_sprintf(goi
.interp
,
4213 "target: %s wait %s fails (%d) %s",
4214 target_name(target
), n
->name
,
4215 e
, target_strerror_safe(e
));
4220 /* List for human, Events defined for this target.
4221 * scripts/programs should use 'name cget -event NAME'
4223 static int jim_target_event_list(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4225 struct command_context
*cmd_ctx
= Jim_GetAssocData(interp
, "context");
4226 struct target
*target
= Jim_CmdPrivData(interp
);
4227 struct target_event_action
*teap
= target
->event_action
;
4228 command_print(cmd_ctx
, "Event actions for target (%d) %s\n",
4229 target
->target_number
,
4230 target_name(target
));
4231 command_print(cmd_ctx
, "%-25s | Body", "Event");
4232 command_print(cmd_ctx
, "------------------------- | "
4233 "----------------------------------------");
4236 Jim_Nvp
*opt
= Jim_Nvp_value2name_simple(nvp_target_event
, teap
->event
);
4237 command_print(cmd_ctx
, "%-25s | %s",
4238 opt
->name
, Jim_GetString(teap
->body
, NULL
));
4241 command_print(cmd_ctx
, "***END***");
4244 static int jim_target_current_state(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4248 Jim_WrongNumArgs(interp
, 1, argv
, "[no parameters]");
4251 struct target
*target
= Jim_CmdPrivData(interp
);
4252 Jim_SetResultString(interp
, target_state_name(target
), -1);
4255 static int jim_target_invoke_event(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4258 Jim_GetOpt_Setup(&goi
, interp
, argc
- 1, argv
+ 1);
4261 const char *cmd_name
= Jim_GetString(argv
[0], NULL
);
4262 Jim_SetResult_sprintf(goi
.interp
, "%s <eventname>", cmd_name
);
4266 int e
= Jim_GetOpt_Nvp(&goi
, nvp_target_event
, &n
);
4269 Jim_GetOpt_NvpUnknown(&goi
, nvp_target_event
, 1);
4272 struct target
*target
= Jim_CmdPrivData(interp
);
4273 target_handle_event(target
, n
->value
);
4277 static const struct command_registration target_instance_command_handlers
[] = {
4279 .name
= "configure",
4280 .mode
= COMMAND_CONFIG
,
4281 .jim_handler
= &jim_target_configure
,
4282 .usage
= "[<target_options> ...]",
4283 .help
= "configure a new target for use",
4287 .mode
= COMMAND_ANY
,
4288 .jim_handler
= &jim_target_configure
,
4289 .usage
= "<target_type> [<target_options> ...]",
4290 .help
= "configure a new target for use",
4294 .mode
= COMMAND_EXEC
,
4295 .jim_handler
= &jim_target_mw
,
4296 .usage
= "<address> <data> [<count>]",
4297 .help
= "Write 32-bit word(s) to target memory",
4301 .mode
= COMMAND_EXEC
,
4302 .jim_handler
= &jim_target_mw
,
4303 .usage
= "<address> <data> [<count>]",
4304 .help
= "Write 16-bit half-word(s) to target memory",
4308 .mode
= COMMAND_EXEC
,
4309 .jim_handler
= &jim_target_mw
,
4310 .usage
= "<address> <data> [<count>]",
4311 .help
= "Write byte(s) to target memory",
4315 .mode
= COMMAND_EXEC
,
4316 .jim_handler
= &jim_target_md
,
4317 .usage
= "<address> [<count>]",
4318 .help
= "Display target memory as 32-bit words",
4322 .mode
= COMMAND_EXEC
,
4323 .jim_handler
= &jim_target_md
,
4324 .usage
= "<address> [<count>]",
4325 .help
= "Display target memory as 16-bit half-words",
4329 .mode
= COMMAND_EXEC
,
4330 .jim_handler
= &jim_target_md
,
4331 .usage
= "<address> [<count>]",
4332 .help
= "Display target memory as 8-bit bytes",
4335 .name
= "array2mem",
4336 .mode
= COMMAND_EXEC
,
4337 .jim_handler
= &jim_target_array2mem
,
4340 .name
= "mem2array",
4341 .mode
= COMMAND_EXEC
,
4342 .jim_handler
= &jim_target_mem2array
,
4345 .name
= "eventlist",
4346 .mode
= COMMAND_EXEC
,
4347 .jim_handler
= &jim_target_event_list
,
4351 .mode
= COMMAND_EXEC
,
4352 .jim_handler
= &jim_target_current_state
,
4355 .name
= "arp_examine",
4356 .mode
= COMMAND_EXEC
,
4357 .jim_handler
= &jim_target_examine
,
4361 .mode
= COMMAND_EXEC
,
4362 .jim_handler
= &jim_target_poll
,
4365 .name
= "arp_reset",
4366 .mode
= COMMAND_EXEC
,
4367 .jim_handler
= &jim_target_reset
,
4371 .mode
= COMMAND_EXEC
,
4372 .jim_handler
= &jim_target_halt
,
4375 .name
= "arp_waitstate",
4376 .mode
= COMMAND_EXEC
,
4377 .jim_handler
= &jim_target_wait_state
,
4380 .name
= "invoke-event",
4381 .mode
= COMMAND_EXEC
,
4382 .jim_handler
= &jim_target_invoke_event
,
4384 COMMAND_REGISTRATION_DONE
4387 static int target_create(Jim_GetOptInfo
*goi
)
4395 struct target
*target
;
4396 struct command_context
*cmd_ctx
;
4398 cmd_ctx
= Jim_GetAssocData(goi
->interp
, "context");
4399 if (goi
->argc
< 3) {
4400 Jim_WrongNumArgs(goi
->interp
, 1, goi
->argv
, "?name? ?type? ..options...");
4405 Jim_GetOpt_Obj(goi
, &new_cmd
);
4406 /* does this command exist? */
4407 cmd
= Jim_GetCommand(goi
->interp
, new_cmd
, JIM_ERRMSG
);
4409 cp
= Jim_GetString(new_cmd
, NULL
);
4410 Jim_SetResult_sprintf(goi
->interp
, "Command/target: %s Exists", cp
);
4415 e
= Jim_GetOpt_String(goi
, &cp2
, NULL
);
4417 /* now does target type exist */
4418 for (x
= 0 ; target_types
[x
] ; x
++) {
4419 if (0 == strcmp(cp
, target_types
[x
]->name
)) {
4424 if (target_types
[x
] == NULL
) {
4425 Jim_SetResult_sprintf(goi
->interp
, "Unknown target type %s, try one of ", cp
);
4426 for (x
= 0 ; target_types
[x
] ; x
++) {
4427 if (target_types
[x
+ 1]) {
4428 Jim_AppendStrings(goi
->interp
,
4429 Jim_GetResult(goi
->interp
),
4430 target_types
[x
]->name
,
4433 Jim_AppendStrings(goi
->interp
,
4434 Jim_GetResult(goi
->interp
),
4436 target_types
[x
]->name
,NULL
);
4443 target
= calloc(1,sizeof(struct target
));
4444 /* set target number */
4445 target
->target_number
= new_target_number();
4447 /* allocate memory for each unique target type */
4448 target
->type
= (struct target_type
*)calloc(1,sizeof(struct target_type
));
4450 memcpy(target
->type
, target_types
[x
], sizeof(struct target_type
));
4452 /* will be set by "-endian" */
4453 target
->endianness
= TARGET_ENDIAN_UNKNOWN
;
4455 target
->working_area
= 0x0;
4456 target
->working_area_size
= 0x0;
4457 target
->working_areas
= NULL
;
4458 target
->backup_working_area
= 0;
4460 target
->state
= TARGET_UNKNOWN
;
4461 target
->debug_reason
= DBG_REASON_UNDEFINED
;
4462 target
->reg_cache
= NULL
;
4463 target
->breakpoints
= NULL
;
4464 target
->watchpoints
= NULL
;
4465 target
->next
= NULL
;
4466 target
->arch_info
= NULL
;
4468 target
->display
= 1;
4470 target
->halt_issued
= false;
4472 /* initialize trace information */
4473 target
->trace_info
= malloc(sizeof(struct trace
));
4474 target
->trace_info
->num_trace_points
= 0;
4475 target
->trace_info
->trace_points_size
= 0;
4476 target
->trace_info
->trace_points
= NULL
;
4477 target
->trace_info
->trace_history_size
= 0;
4478 target
->trace_info
->trace_history
= NULL
;
4479 target
->trace_info
->trace_history_pos
= 0;
4480 target
->trace_info
->trace_history_overflowed
= 0;
4482 target
->dbgmsg
= NULL
;
4483 target
->dbg_msg_enabled
= 0;
4485 target
->endianness
= TARGET_ENDIAN_UNKNOWN
;
4487 /* Do the rest as "configure" options */
4488 goi
->isconfigure
= 1;
4489 e
= target_configure(goi
, target
);
4491 if (target
->tap
== NULL
)
4493 Jim_SetResultString(goi
->interp
, "-chain-position required when creating target", -1);
4503 if (target
->endianness
== TARGET_ENDIAN_UNKNOWN
) {
4504 /* default endian to little if not specified */
4505 target
->endianness
= TARGET_LITTLE_ENDIAN
;
4508 /* incase variant is not set */
4509 if (!target
->variant
)
4510 target
->variant
= strdup("");
4512 cp
= Jim_GetString(new_cmd
, NULL
);
4513 target
->cmd_name
= strdup(cp
);
4515 /* create the target specific commands */
4516 if (target
->type
->commands
) {
4517 e
= register_commands(cmd_ctx
, NULL
, target
->type
->commands
);
4519 LOG_ERROR("unable to register '%s' commands", cp
);
4521 if (target
->type
->target_create
) {
4522 (*(target
->type
->target_create
))(target
, goi
->interp
);
4525 /* append to end of list */
4527 struct target
**tpp
;
4528 tpp
= &(all_targets
);
4530 tpp
= &((*tpp
)->next
);
4535 /* now - create the new target name command */
4536 const const struct command_registration target_subcommands
[] = {
4538 .chain
= target_instance_command_handlers
,
4541 .chain
= target
->type
->commands
,
4543 COMMAND_REGISTRATION_DONE
4545 const const struct command_registration target_commands
[] = {
4548 .mode
= COMMAND_ANY
,
4549 .help
= "target command group",
4550 .chain
= target_subcommands
,
4552 COMMAND_REGISTRATION_DONE
4554 e
= register_commands(cmd_ctx
, NULL
, target_commands
);
4558 struct command
*c
= command_find_in_context(cmd_ctx
, cp
);
4560 command_set_handler_data(c
, target
);
4562 return (ERROR_OK
== e
) ? JIM_OK
: JIM_ERR
;
4565 static int jim_target_current(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4569 Jim_WrongNumArgs(interp
, 1, argv
, "Too many parameters");
4572 struct command_context
*cmd_ctx
= Jim_GetAssocData(interp
, "context");
4573 Jim_SetResultString(interp
, get_current_target(cmd_ctx
)->cmd_name
, -1);
4577 static int jim_target_types(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4581 Jim_WrongNumArgs(interp
, 1, argv
, "Too many parameters");
4584 Jim_SetResult(interp
, Jim_NewListObj(interp
, NULL
, 0));
4585 for (unsigned x
= 0; NULL
!= target_types
[x
]; x
++)
4587 Jim_ListAppendElement(interp
, Jim_GetResult(interp
),
4588 Jim_NewStringObj(interp
, target_types
[x
]->name
, -1));
4593 static int jim_target_names(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4597 Jim_WrongNumArgs(interp
, 1, argv
, "Too many parameters");
4600 Jim_SetResult(interp
, Jim_NewListObj(interp
, NULL
, 0));
4601 struct target
*target
= all_targets
;
4604 Jim_ListAppendElement(interp
, Jim_GetResult(interp
),
4605 Jim_NewStringObj(interp
, target_name(target
), -1));
4606 target
= target
->next
;
4611 static int jim_target_create(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4614 Jim_GetOpt_Setup(&goi
, interp
, argc
- 1, argv
+ 1);
4617 Jim_WrongNumArgs(goi
.interp
, goi
.argc
, goi
.argv
,
4618 "<name> <target_type> [<target_options> ...]");
4621 return target_create(&goi
);
4624 static int jim_target_number(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4627 Jim_GetOpt_Setup(&goi
, interp
, argc
- 1, argv
+ 1);
4629 /* It's OK to remove this mechanism sometime after August 2010 or so */
4630 LOG_WARNING("don't use numbers as target identifiers; use names");
4633 Jim_SetResult_sprintf(goi
.interp
, "usage: target number <number>");
4637 int e
= Jim_GetOpt_Wide(&goi
, &w
);
4641 struct target
*target
;
4642 for (target
= all_targets
; NULL
!= target
; target
= target
->next
)
4644 if (target
->target_number
!= w
)
4647 Jim_SetResultString(goi
.interp
, target_name(target
), -1);
4650 Jim_SetResult_sprintf(goi
.interp
,
4651 "Target: number %d does not exist", (int)(w
));
4655 static int jim_target_count(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4659 Jim_WrongNumArgs(interp
, 1, argv
, "<no parameters>");
4663 struct target
*target
= all_targets
;
4664 while (NULL
!= target
)
4666 target
= target
->next
;
4669 Jim_SetResult(interp
, Jim_NewIntObj(interp
, count
));
4673 static const struct command_registration target_subcommand_handlers
[] = {
4676 .mode
= COMMAND_ANY
,
4677 .jim_handler
= &jim_target_create
,
4678 .usage
= "<name> <type> ...",
4679 .help
= "Returns the currently selected target",
4683 .mode
= COMMAND_ANY
,
4684 .jim_handler
= &jim_target_current
,
4685 .help
= "Returns the currently selected target",
4689 .mode
= COMMAND_ANY
,
4690 .jim_handler
= &jim_target_types
,
4691 .help
= "Returns the available target types as a list of strings",
4695 .mode
= COMMAND_ANY
,
4696 .jim_handler
= &jim_target_names
,
4697 .help
= "Returns the names of all targets as a list of strings",
4701 .mode
= COMMAND_ANY
,
4702 .jim_handler
= &jim_target_number
,
4703 .usage
= "<number>",
4704 .help
= "Returns the name of target <n>",
4708 .mode
= COMMAND_ANY
,
4709 .jim_handler
= &jim_target_count
,
4710 .help
= "Returns the number of targets as an integer",
4712 COMMAND_REGISTRATION_DONE
4724 static int fastload_num
;
4725 static struct FastLoad
*fastload
;
4727 static void free_fastload(void)
4729 if (fastload
!= NULL
)
4732 for (i
= 0; i
< fastload_num
; i
++)
4734 if (fastload
[i
].data
)
4735 free(fastload
[i
].data
);
4745 COMMAND_HANDLER(handle_fast_load_image_command
)
4749 uint32_t image_size
;
4750 uint32_t min_address
= 0;
4751 uint32_t max_address
= 0xffffffff;
4756 int retval
= CALL_COMMAND_HANDLER(parse_load_image_command_CMD_ARGV
,
4757 &image
, &min_address
, &max_address
);
4758 if (ERROR_OK
!= retval
)
4761 struct duration bench
;
4762 duration_start(&bench
);
4764 if (image_open(&image
, CMD_ARGV
[0], (CMD_ARGC
>= 3) ? CMD_ARGV
[2] : NULL
) != ERROR_OK
)
4771 fastload_num
= image
.num_sections
;
4772 fastload
= (struct FastLoad
*)malloc(sizeof(struct FastLoad
)*image
.num_sections
);
4773 if (fastload
== NULL
)
4775 image_close(&image
);
4778 memset(fastload
, 0, sizeof(struct FastLoad
)*image
.num_sections
);
4779 for (i
= 0; i
< image
.num_sections
; i
++)
4781 buffer
= malloc(image
.sections
[i
].size
);
4784 command_print(CMD_CTX
, "error allocating buffer for section (%d bytes)",
4785 (int)(image
.sections
[i
].size
));
4789 if ((retval
= image_read_section(&image
, i
, 0x0, image
.sections
[i
].size
, buffer
, &buf_cnt
)) != ERROR_OK
)
4795 uint32_t offset
= 0;
4796 uint32_t length
= buf_cnt
;
4799 /* DANGER!!! beware of unsigned comparision here!!! */
4801 if ((image
.sections
[i
].base_address
+ buf_cnt
>= min_address
)&&
4802 (image
.sections
[i
].base_address
< max_address
))
4804 if (image
.sections
[i
].base_address
< min_address
)
4806 /* clip addresses below */
4807 offset
+= min_address
-image
.sections
[i
].base_address
;
4811 if (image
.sections
[i
].base_address
+ buf_cnt
> max_address
)
4813 length
-= (image
.sections
[i
].base_address
+ buf_cnt
)-max_address
;
4816 fastload
[i
].address
= image
.sections
[i
].base_address
+ offset
;
4817 fastload
[i
].data
= malloc(length
);
4818 if (fastload
[i
].data
== NULL
)
4823 memcpy(fastload
[i
].data
, buffer
+ offset
, length
);
4824 fastload
[i
].length
= length
;
4826 image_size
+= length
;
4827 command_print(CMD_CTX
, "%u bytes written at address 0x%8.8x",
4828 (unsigned int)length
,
4829 ((unsigned int)(image
.sections
[i
].base_address
+ offset
)));
4835 if ((ERROR_OK
== retval
) && (duration_measure(&bench
) == ERROR_OK
))
4837 command_print(CMD_CTX
, "Loaded %" PRIu32
" bytes "
4838 "in %fs (%0.3f kb/s)", image_size
,
4839 duration_elapsed(&bench
), duration_kbps(&bench
, image_size
));
4841 command_print(CMD_CTX
,
4842 "WARNING: image has not been loaded to target!"
4843 "You can issue a 'fast_load' to finish loading.");
4846 image_close(&image
);
4848 if (retval
!= ERROR_OK
)
4856 COMMAND_HANDLER(handle_fast_load_command
)
4859 return ERROR_COMMAND_SYNTAX_ERROR
;
4860 if (fastload
== NULL
)
4862 LOG_ERROR("No image in memory");
4866 int ms
= timeval_ms();
4868 int retval
= ERROR_OK
;
4869 for (i
= 0; i
< fastload_num
;i
++)
4871 struct target
*target
= get_current_target(CMD_CTX
);
4872 command_print(CMD_CTX
, "Write to 0x%08x, length 0x%08x",
4873 (unsigned int)(fastload
[i
].address
),
4874 (unsigned int)(fastload
[i
].length
));
4875 if (retval
== ERROR_OK
)
4877 retval
= target_write_buffer(target
, fastload
[i
].address
, fastload
[i
].length
, fastload
[i
].data
);
4879 size
+= fastload
[i
].length
;
4881 int after
= timeval_ms();
4882 command_print(CMD_CTX
, "Loaded image %f kBytes/s", (float)(size
/1024.0)/((float)(after
-ms
)/1000.0));
4886 static int jim_mcrmrc(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4888 struct command_context
*context
;
4889 struct target
*target
;
4892 context
= Jim_GetAssocData(interp
, "context");
4893 if (context
== NULL
) {
4894 LOG_ERROR("array2mem: no command context");
4897 target
= get_current_target(context
);
4898 if (target
== NULL
) {
4899 LOG_ERROR("array2mem: no current target");
4903 if ((argc
< 6) || (argc
> 7))
4917 e
= Jim_GetLong(interp
, argv
[1], &l
);
4923 e
= Jim_GetLong(interp
, argv
[2], &l
);
4929 e
= Jim_GetLong(interp
, argv
[3], &l
);
4935 e
= Jim_GetLong(interp
, argv
[4], &l
);
4941 e
= Jim_GetLong(interp
, argv
[5], &l
);
4951 e
= Jim_GetLong(interp
, argv
[6], &l
);
4957 retval
= target_mcr(target
, cpnum
, op1
, op2
, CRn
, CRm
, value
);
4958 if (retval
!= ERROR_OK
)
4962 retval
= target_mrc(target
, cpnum
, op1
, op2
, CRn
, CRm
, &value
);
4963 if (retval
!= ERROR_OK
)
4966 Jim_SetResult(interp
, Jim_NewIntObj(interp
, value
));
4972 static const struct command_registration target_command_handlers
[] = {
4975 .handler
= &handle_targets_command
,
4976 .mode
= COMMAND_ANY
,
4977 .help
= "change current command line target (one parameter) "
4978 "or list targets (no parameters)",
4979 .usage
= "[<new_current_target>]",
4983 .mode
= COMMAND_CONFIG
,
4984 .help
= "configure target",
4986 .chain
= target_subcommand_handlers
,
4988 COMMAND_REGISTRATION_DONE
4991 int target_register_commands(struct command_context
*cmd_ctx
)
4993 return register_commands(cmd_ctx
, NULL
, target_command_handlers
);
4996 static const struct command_registration target_exec_command_handlers
[] = {
4998 .name
= "fast_load_image",
4999 .handler
= &handle_fast_load_image_command
,
5000 .mode
= COMMAND_ANY
,
5001 .help
= "Load image into memory, mainly for profiling purposes",
5002 .usage
= "<file> <address> ['bin'|'ihex'|'elf'|'s19'] "
5003 "[min_address] [max_length]",
5006 .name
= "fast_load",
5007 .handler
= &handle_fast_load_command
,
5008 .mode
= COMMAND_ANY
,
5009 .help
= "loads active fast load image to current target "
5010 "- mainly for profiling purposes",
5014 .handler
= &handle_profile_command
,
5015 .mode
= COMMAND_EXEC
,
5016 .help
= "profiling samples the CPU PC",
5018 /** @todo don't register virt2phys() unless target supports it */
5020 .name
= "virt2phys",
5021 .handler
= &handle_virt2phys_command
,
5022 .mode
= COMMAND_ANY
,
5023 .help
= "translate a virtual address into a physical address",
5028 .handler
= &handle_reg_command
,
5029 .mode
= COMMAND_EXEC
,
5030 .help
= "display or set a register",
5035 .handler
= &handle_poll_command
,
5036 .mode
= COMMAND_EXEC
,
5037 .help
= "poll target state",
5040 .name
= "wait_halt",
5041 .handler
= &handle_wait_halt_command
,
5042 .mode
= COMMAND_EXEC
,
5043 .help
= "wait for target halt",
5044 .usage
= "[time (s)]",
5048 .handler
= &handle_halt_command
,
5049 .mode
= COMMAND_EXEC
,
5050 .help
= "halt target",
5054 .handler
= &handle_resume_command
,
5055 .mode
= COMMAND_EXEC
,
5056 .help
= "resume target",
5057 .usage
= "[<address>]",
5061 .handler
= &handle_reset_command
,
5062 .mode
= COMMAND_EXEC
,
5063 .usage
= "[run|halt|init]",
5064 .help
= "Reset all targets into the specified mode."
5065 "Default reset mode is run, if not given.",
5068 .name
= "soft_reset_halt",
5069 .handler
= &handle_soft_reset_halt_command
,
5070 .mode
= COMMAND_EXEC
,
5071 .help
= "halt the target and do a soft reset",
5076 .handler
= &handle_step_command
,
5077 .mode
= COMMAND_EXEC
,
5078 .help
= "step one instruction from current PC or [addr]",
5079 .usage
= "[<address>]",
5084 .handler
= &handle_md_command
,
5085 .mode
= COMMAND_EXEC
,
5086 .help
= "display memory words",
5087 .usage
= "[phys] <addr> [count]",
5091 .handler
= &handle_md_command
,
5092 .mode
= COMMAND_EXEC
,
5093 .help
= "display memory half-words",
5094 .usage
= "[phys] <addr> [count]",
5098 .handler
= &handle_md_command
,
5099 .mode
= COMMAND_EXEC
,
5100 .help
= "display memory bytes",
5101 .usage
= "[phys] <addr> [count]",
5106 .handler
= &handle_mw_command
,
5107 .mode
= COMMAND_EXEC
,
5108 .help
= "write memory word",
5109 .usage
= "[phys] <addr> <value> [count]",
5113 .handler
= &handle_mw_command
,
5114 .mode
= COMMAND_EXEC
,
5115 .help
= "write memory half-word",
5116 .usage
= "[phys] <addr> <value> [count]",
5120 .handler
= &handle_mw_command
,
5121 .mode
= COMMAND_EXEC
,
5122 .help
= "write memory byte",
5123 .usage
= "[phys] <addr> <value> [count]",
5128 .handler
= &handle_bp_command
,
5129 .mode
= COMMAND_EXEC
,
5130 .help
= "list or set breakpoint",
5131 .usage
= "[<address> <length> [hw]]",
5135 .handler
= &handle_rbp_command
,
5136 .mode
= COMMAND_EXEC
,
5137 .help
= "remove breakpoint",
5138 .usage
= "<address>",
5143 .handler
= &handle_wp_command
,
5144 .mode
= COMMAND_EXEC
,
5145 .help
= "list or set watchpoint",
5146 .usage
= "[<address> <length> <r/w/a> [value] [mask]]",
5150 .handler
= &handle_rwp_command
,
5151 .mode
= COMMAND_EXEC
,
5152 .help
= "remove watchpoint",
5153 .usage
= "<address>",
5157 .name
= "load_image",
5158 .handler
= &handle_load_image_command
,
5159 .mode
= COMMAND_EXEC
,
5160 .usage
= "<file> <address> ['bin'|'ihex'|'elf'|'s19'] "
5161 "[min_address] [max_length]",
5164 .name
= "dump_image",
5165 .handler
= &handle_dump_image_command
,
5166 .mode
= COMMAND_EXEC
,
5167 .usage
= "<file> <address> <size>",
5170 .name
= "verify_image",
5171 .handler
= &handle_verify_image_command
,
5172 .mode
= COMMAND_EXEC
,
5173 .usage
= "<file> [offset] [type]",
5176 .name
= "test_image",
5177 .handler
= &handle_test_image_command
,
5178 .mode
= COMMAND_EXEC
,
5179 .usage
= "<file> [offset] [type]",
5182 .name
= "ocd_mem2array",
5183 .mode
= COMMAND_EXEC
,
5184 .jim_handler
= &jim_mem2array
,
5185 .help
= "read memory and return as a TCL array "
5186 "for script processing",
5187 .usage
= "<arrayname> <width=32|16|8> <address> <count>",
5190 .name
= "ocd_array2mem",
5191 .mode
= COMMAND_EXEC
,
5192 .jim_handler
= &jim_array2mem
,
5193 .help
= "convert a TCL array to memory locations "
5194 "and write the values",
5195 .usage
= "<arrayname> <width=32|16|8> <address> <count>",
5197 COMMAND_REGISTRATION_DONE
5199 int target_register_user_commands(struct command_context
*cmd_ctx
)
5201 int retval
= ERROR_OK
;
5202 if ((retval
= target_request_register_commands(cmd_ctx
)) != ERROR_OK
)
5205 if ((retval
= trace_register_commands(cmd_ctx
)) != ERROR_OK
)
5209 return register_commands(cmd_ctx
, NULL
, target_exec_command_handlers
);