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(interp
, buf
);
454 jtag_poll_set_enabled(save_poll
);
456 if (retval
!= JIM_OK
) {
457 Jim_PrintErrorMessage(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 int target_init(struct command_context
*cmd_ctx
)
764 struct target
*target
;
767 for (target
= all_targets
; target
; target
= target
->next
) {
768 struct target_type
*type
= target
->type
;
770 target_reset_examined(target
);
771 if (target
->type
->examine
== NULL
)
773 target
->type
->examine
= default_examine
;
776 if ((retval
= target
->type
->init_target(cmd_ctx
, target
)) != ERROR_OK
)
778 LOG_ERROR("target '%s' init failed", target_name(target
));
783 * @todo MCR/MRC are ARM-specific; don't require them in
784 * all targets, or for ARMs without coprocessors.
786 if (target
->type
->mcr
== NULL
)
788 target
->type
->mcr
= default_mcr
;
791 const struct command_registration mcr_cmd
= {
793 .mode
= COMMAND_EXEC
,
794 .jim_handler
= &jim_mcrmrc
,
795 .help
= "write coprocessor",
796 .usage
= "<cpnum> <op1> <op2> <CRn> <CRm> <value>",
798 register_command(cmd_ctx
, NULL
, &mcr_cmd
);
801 if (target
->type
->mrc
== NULL
)
803 target
->type
->mrc
= default_mrc
;
806 const struct command_registration mrc_cmd
= {
808 .jim_handler
= &jim_mcrmrc
,
809 .help
= "read coprocessor",
810 .usage
= "<cpnum> <op1> <op2> <CRn> <CRm>",
812 register_command(cmd_ctx
, NULL
, &mrc_cmd
);
817 * @todo get rid of those *memory_imp() methods, now that all
818 * callers are using target_*_memory() accessors ... and make
819 * sure the "physical" paths handle the same issues.
822 /* a non-invasive way(in terms of patches) to add some code that
823 * runs before the type->write/read_memory implementation
825 target
->type
->write_memory_imp
= target
->type
->write_memory
;
826 target
->type
->write_memory
= target_write_memory_imp
;
827 target
->type
->read_memory_imp
= target
->type
->read_memory
;
828 target
->type
->read_memory
= target_read_memory_imp
;
829 target
->type
->soft_reset_halt_imp
= target
->type
->soft_reset_halt
;
830 target
->type
->soft_reset_halt
= target_soft_reset_halt_imp
;
831 target
->type
->run_algorithm_imp
= target
->type
->run_algorithm
;
832 target
->type
->run_algorithm
= target_run_algorithm_imp
;
834 /* Sanity-check MMU support ... stub in what we must, to help
835 * implement it in stages, but warn if we need to do so.
838 if (type
->write_phys_memory
== NULL
) {
839 LOG_ERROR("type '%s' is missing %s",
841 "write_phys_memory");
842 type
->write_phys_memory
= err_write_phys_memory
;
844 if (type
->read_phys_memory
== NULL
) {
845 LOG_ERROR("type '%s' is missing %s",
848 type
->read_phys_memory
= err_read_phys_memory
;
850 if (type
->virt2phys
== NULL
) {
851 LOG_ERROR("type '%s' is missing %s",
854 type
->virt2phys
= identity_virt2phys
;
857 /* Make sure no-MMU targets all behave the same: make no
858 * distinction between physical and virtual addresses, and
859 * ensure that virt2phys() is always an identity mapping.
862 if (type
->write_phys_memory
863 || type
->read_phys_memory
865 LOG_WARNING("type '%s' has broken MMU hooks",
869 type
->write_phys_memory
= type
->write_memory
;
870 type
->read_phys_memory
= type
->read_memory
;
871 type
->virt2phys
= identity_virt2phys
;
877 if ((retval
= target_register_user_commands(cmd_ctx
)) != ERROR_OK
)
879 if ((retval
= target_register_timer_callback(handle_target
, 100, 1, NULL
)) != ERROR_OK
)
886 int target_register_event_callback(int (*callback
)(struct target
*target
, enum target_event event
, void *priv
), void *priv
)
888 struct target_event_callback
**callbacks_p
= &target_event_callbacks
;
890 if (callback
== NULL
)
892 return ERROR_INVALID_ARGUMENTS
;
897 while ((*callbacks_p
)->next
)
898 callbacks_p
= &((*callbacks_p
)->next
);
899 callbacks_p
= &((*callbacks_p
)->next
);
902 (*callbacks_p
) = malloc(sizeof(struct target_event_callback
));
903 (*callbacks_p
)->callback
= callback
;
904 (*callbacks_p
)->priv
= priv
;
905 (*callbacks_p
)->next
= NULL
;
910 int target_register_timer_callback(int (*callback
)(void *priv
), int time_ms
, int periodic
, void *priv
)
912 struct target_timer_callback
**callbacks_p
= &target_timer_callbacks
;
915 if (callback
== NULL
)
917 return ERROR_INVALID_ARGUMENTS
;
922 while ((*callbacks_p
)->next
)
923 callbacks_p
= &((*callbacks_p
)->next
);
924 callbacks_p
= &((*callbacks_p
)->next
);
927 (*callbacks_p
) = malloc(sizeof(struct target_timer_callback
));
928 (*callbacks_p
)->callback
= callback
;
929 (*callbacks_p
)->periodic
= periodic
;
930 (*callbacks_p
)->time_ms
= time_ms
;
932 gettimeofday(&now
, NULL
);
933 (*callbacks_p
)->when
.tv_usec
= now
.tv_usec
+ (time_ms
% 1000) * 1000;
934 time_ms
-= (time_ms
% 1000);
935 (*callbacks_p
)->when
.tv_sec
= now
.tv_sec
+ (time_ms
/ 1000);
936 if ((*callbacks_p
)->when
.tv_usec
> 1000000)
938 (*callbacks_p
)->when
.tv_usec
= (*callbacks_p
)->when
.tv_usec
- 1000000;
939 (*callbacks_p
)->when
.tv_sec
+= 1;
942 (*callbacks_p
)->priv
= priv
;
943 (*callbacks_p
)->next
= NULL
;
948 int target_unregister_event_callback(int (*callback
)(struct target
*target
, enum target_event event
, void *priv
), void *priv
)
950 struct target_event_callback
**p
= &target_event_callbacks
;
951 struct target_event_callback
*c
= target_event_callbacks
;
953 if (callback
== NULL
)
955 return ERROR_INVALID_ARGUMENTS
;
960 struct target_event_callback
*next
= c
->next
;
961 if ((c
->callback
== callback
) && (c
->priv
== priv
))
975 int target_unregister_timer_callback(int (*callback
)(void *priv
), void *priv
)
977 struct target_timer_callback
**p
= &target_timer_callbacks
;
978 struct target_timer_callback
*c
= target_timer_callbacks
;
980 if (callback
== NULL
)
982 return ERROR_INVALID_ARGUMENTS
;
987 struct target_timer_callback
*next
= c
->next
;
988 if ((c
->callback
== callback
) && (c
->priv
== priv
))
1002 int target_call_event_callbacks(struct target
*target
, enum target_event event
)
1004 struct target_event_callback
*callback
= target_event_callbacks
;
1005 struct target_event_callback
*next_callback
;
1007 if (event
== TARGET_EVENT_HALTED
)
1009 /* execute early halted first */
1010 target_call_event_callbacks(target
, TARGET_EVENT_GDB_HALT
);
1013 LOG_DEBUG("target event %i (%s)",
1015 Jim_Nvp_value2name_simple(nvp_target_event
, event
)->name
);
1017 target_handle_event(target
, event
);
1021 next_callback
= callback
->next
;
1022 callback
->callback(target
, event
, callback
->priv
);
1023 callback
= next_callback
;
1029 static int target_timer_callback_periodic_restart(
1030 struct target_timer_callback
*cb
, struct timeval
*now
)
1032 int time_ms
= cb
->time_ms
;
1033 cb
->when
.tv_usec
= now
->tv_usec
+ (time_ms
% 1000) * 1000;
1034 time_ms
-= (time_ms
% 1000);
1035 cb
->when
.tv_sec
= now
->tv_sec
+ time_ms
/ 1000;
1036 if (cb
->when
.tv_usec
> 1000000)
1038 cb
->when
.tv_usec
= cb
->when
.tv_usec
- 1000000;
1039 cb
->when
.tv_sec
+= 1;
1044 static int target_call_timer_callback(struct target_timer_callback
*cb
,
1045 struct timeval
*now
)
1047 cb
->callback(cb
->priv
);
1050 return target_timer_callback_periodic_restart(cb
, now
);
1052 return target_unregister_timer_callback(cb
->callback
, cb
->priv
);
1055 static int target_call_timer_callbacks_check_time(int checktime
)
1060 gettimeofday(&now
, NULL
);
1062 struct target_timer_callback
*callback
= target_timer_callbacks
;
1065 // cleaning up may unregister and free this callback
1066 struct target_timer_callback
*next_callback
= callback
->next
;
1068 bool call_it
= callback
->callback
&&
1069 ((!checktime
&& callback
->periodic
) ||
1070 now
.tv_sec
> callback
->when
.tv_sec
||
1071 (now
.tv_sec
== callback
->when
.tv_sec
&&
1072 now
.tv_usec
>= callback
->when
.tv_usec
));
1076 int retval
= target_call_timer_callback(callback
, &now
);
1077 if (retval
!= ERROR_OK
)
1081 callback
= next_callback
;
1087 int target_call_timer_callbacks(void)
1089 return target_call_timer_callbacks_check_time(1);
1092 /* invoke periodic callbacks immediately */
1093 int target_call_timer_callbacks_now(void)
1095 return target_call_timer_callbacks_check_time(0);
1098 int target_alloc_working_area(struct target
*target
, uint32_t size
, struct working_area
**area
)
1100 struct working_area
*c
= target
->working_areas
;
1101 struct working_area
*new_wa
= NULL
;
1103 /* Reevaluate working area address based on MMU state*/
1104 if (target
->working_areas
== NULL
)
1109 retval
= target
->type
->mmu(target
, &enabled
);
1110 if (retval
!= ERROR_OK
)
1116 if (target
->working_area_phys_spec
) {
1117 LOG_DEBUG("MMU disabled, using physical "
1118 "address for working memory 0x%08x",
1119 (unsigned)target
->working_area_phys
);
1120 target
->working_area
= target
->working_area_phys
;
1122 LOG_ERROR("No working memory available. "
1123 "Specify -work-area-phys to target.");
1124 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
1127 if (target
->working_area_virt_spec
) {
1128 LOG_DEBUG("MMU enabled, using virtual "
1129 "address for working memory 0x%08x",
1130 (unsigned)target
->working_area_virt
);
1131 target
->working_area
= target
->working_area_virt
;
1133 LOG_ERROR("No working memory available. "
1134 "Specify -work-area-virt to target.");
1135 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
1140 /* only allocate multiples of 4 byte */
1143 LOG_ERROR("BUG: code tried to allocate unaligned number of bytes (0x%08x), padding", ((unsigned)(size
)));
1144 size
= (size
+ 3) & (~3);
1147 /* see if there's already a matching working area */
1150 if ((c
->free
) && (c
->size
== size
))
1158 /* if not, allocate a new one */
1161 struct working_area
**p
= &target
->working_areas
;
1162 uint32_t first_free
= target
->working_area
;
1163 uint32_t free_size
= target
->working_area_size
;
1165 c
= target
->working_areas
;
1168 first_free
+= c
->size
;
1169 free_size
-= c
->size
;
1174 if (free_size
< size
)
1176 LOG_WARNING("not enough working area available(requested %u, free %u)",
1177 (unsigned)(size
), (unsigned)(free_size
));
1178 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
1181 LOG_DEBUG("allocated new working area at address 0x%08x", (unsigned)first_free
);
1183 new_wa
= malloc(sizeof(struct working_area
));
1184 new_wa
->next
= NULL
;
1185 new_wa
->size
= size
;
1186 new_wa
->address
= first_free
;
1188 if (target
->backup_working_area
)
1191 new_wa
->backup
= malloc(new_wa
->size
);
1192 if ((retval
= target_read_memory(target
, new_wa
->address
, 4, new_wa
->size
/ 4, new_wa
->backup
)) != ERROR_OK
)
1194 free(new_wa
->backup
);
1201 new_wa
->backup
= NULL
;
1204 /* put new entry in list */
1208 /* mark as used, and return the new (reused) area */
1213 new_wa
->user
= area
;
1218 int target_free_working_area_restore(struct target
*target
, struct working_area
*area
, int restore
)
1223 if (restore
&& target
->backup_working_area
)
1226 if ((retval
= target_write_memory(target
, area
->address
, 4, area
->size
/ 4, area
->backup
)) != ERROR_OK
)
1232 /* mark user pointer invalid */
1239 int target_free_working_area(struct target
*target
, struct working_area
*area
)
1241 return target_free_working_area_restore(target
, area
, 1);
1244 /* free resources and restore memory, if restoring memory fails,
1245 * free up resources anyway
1247 void target_free_all_working_areas_restore(struct target
*target
, int restore
)
1249 struct working_area
*c
= target
->working_areas
;
1253 struct working_area
*next
= c
->next
;
1254 target_free_working_area_restore(target
, c
, restore
);
1264 target
->working_areas
= NULL
;
1267 void target_free_all_working_areas(struct target
*target
)
1269 target_free_all_working_areas_restore(target
, 1);
1272 int target_arch_state(struct target
*target
)
1277 LOG_USER("No target has been configured");
1281 LOG_USER("target state: %s", target_state_name( target
));
1283 if (target
->state
!= TARGET_HALTED
)
1286 retval
= target
->type
->arch_state(target
);
1290 /* Single aligned words are guaranteed to use 16 or 32 bit access
1291 * mode respectively, otherwise data is handled as quickly as
1294 int target_write_buffer(struct target
*target
, uint32_t address
, uint32_t size
, uint8_t *buffer
)
1297 LOG_DEBUG("writing buffer of %i byte at 0x%8.8x",
1298 (int)size
, (unsigned)address
);
1300 if (!target_was_examined(target
))
1302 LOG_ERROR("Target not examined yet");
1310 if ((address
+ size
- 1) < address
)
1312 /* GDB can request this when e.g. PC is 0xfffffffc*/
1313 LOG_ERROR("address + size wrapped(0x%08x, 0x%08x)",
1319 if (((address
% 2) == 0) && (size
== 2))
1321 return target_write_memory(target
, address
, 2, 1, buffer
);
1324 /* handle unaligned head bytes */
1327 uint32_t unaligned
= 4 - (address
% 4);
1329 if (unaligned
> size
)
1332 if ((retval
= target_write_memory(target
, address
, 1, unaligned
, buffer
)) != ERROR_OK
)
1335 buffer
+= unaligned
;
1336 address
+= unaligned
;
1340 /* handle aligned words */
1343 int aligned
= size
- (size
% 4);
1345 /* use bulk writes above a certain limit. This may have to be changed */
1348 if ((retval
= target
->type
->bulk_write_memory(target
, address
, aligned
/ 4, buffer
)) != ERROR_OK
)
1353 if ((retval
= target_write_memory(target
, address
, 4, aligned
/ 4, buffer
)) != ERROR_OK
)
1362 /* handle tail writes of less than 4 bytes */
1365 if ((retval
= target_write_memory(target
, address
, 1, size
, buffer
)) != ERROR_OK
)
1372 /* Single aligned words are guaranteed to use 16 or 32 bit access
1373 * mode respectively, otherwise data is handled as quickly as
1376 int target_read_buffer(struct target
*target
, uint32_t address
, uint32_t size
, uint8_t *buffer
)
1379 LOG_DEBUG("reading buffer of %i byte at 0x%8.8x",
1380 (int)size
, (unsigned)address
);
1382 if (!target_was_examined(target
))
1384 LOG_ERROR("Target not examined yet");
1392 if ((address
+ size
- 1) < address
)
1394 /* GDB can request this when e.g. PC is 0xfffffffc*/
1395 LOG_ERROR("address + size wrapped(0x%08" PRIx32
", 0x%08" PRIx32
")",
1401 if (((address
% 2) == 0) && (size
== 2))
1403 return target_read_memory(target
, address
, 2, 1, buffer
);
1406 /* handle unaligned head bytes */
1409 uint32_t unaligned
= 4 - (address
% 4);
1411 if (unaligned
> size
)
1414 if ((retval
= target_read_memory(target
, address
, 1, unaligned
, buffer
)) != ERROR_OK
)
1417 buffer
+= unaligned
;
1418 address
+= unaligned
;
1422 /* handle aligned words */
1425 int aligned
= size
- (size
% 4);
1427 if ((retval
= target_read_memory(target
, address
, 4, aligned
/ 4, buffer
)) != ERROR_OK
)
1435 /*prevent byte access when possible (avoid AHB access limitations in some cases)*/
1438 int aligned
= size
- (size
%2);
1439 retval
= target_read_memory(target
, address
, 2, aligned
/ 2, buffer
);
1440 if (retval
!= ERROR_OK
)
1447 /* handle tail writes of less than 4 bytes */
1450 if ((retval
= target_read_memory(target
, address
, 1, size
, buffer
)) != ERROR_OK
)
1457 int target_checksum_memory(struct target
*target
, uint32_t address
, uint32_t size
, uint32_t* crc
)
1462 uint32_t checksum
= 0;
1463 if (!target_was_examined(target
))
1465 LOG_ERROR("Target not examined yet");
1469 if ((retval
= target
->type
->checksum_memory(target
, address
,
1470 size
, &checksum
)) != ERROR_OK
)
1472 buffer
= malloc(size
);
1475 LOG_ERROR("error allocating buffer for section (%d bytes)", (int)size
);
1476 return ERROR_INVALID_ARGUMENTS
;
1478 retval
= target_read_buffer(target
, address
, size
, buffer
);
1479 if (retval
!= ERROR_OK
)
1485 /* convert to target endianess */
1486 for (i
= 0; i
< (size
/sizeof(uint32_t)); i
++)
1488 uint32_t target_data
;
1489 target_data
= target_buffer_get_u32(target
, &buffer
[i
*sizeof(uint32_t)]);
1490 target_buffer_set_u32(target
, &buffer
[i
*sizeof(uint32_t)], target_data
);
1493 retval
= image_calculate_checksum(buffer
, size
, &checksum
);
1502 int target_blank_check_memory(struct target
*target
, uint32_t address
, uint32_t size
, uint32_t* blank
)
1505 if (!target_was_examined(target
))
1507 LOG_ERROR("Target not examined yet");
1511 if (target
->type
->blank_check_memory
== 0)
1512 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
1514 retval
= target
->type
->blank_check_memory(target
, address
, size
, blank
);
1519 int target_read_u32(struct target
*target
, uint32_t address
, uint32_t *value
)
1521 uint8_t value_buf
[4];
1522 if (!target_was_examined(target
))
1524 LOG_ERROR("Target not examined yet");
1528 int retval
= target_read_memory(target
, address
, 4, 1, value_buf
);
1530 if (retval
== ERROR_OK
)
1532 *value
= target_buffer_get_u32(target
, value_buf
);
1533 LOG_DEBUG("address: 0x%8.8" PRIx32
", value: 0x%8.8" PRIx32
"",
1540 LOG_DEBUG("address: 0x%8.8" PRIx32
" failed",
1547 int target_read_u16(struct target
*target
, uint32_t address
, uint16_t *value
)
1549 uint8_t value_buf
[2];
1550 if (!target_was_examined(target
))
1552 LOG_ERROR("Target not examined yet");
1556 int retval
= target_read_memory(target
, address
, 2, 1, value_buf
);
1558 if (retval
== ERROR_OK
)
1560 *value
= target_buffer_get_u16(target
, value_buf
);
1561 LOG_DEBUG("address: 0x%8.8" PRIx32
", value: 0x%4.4x",
1568 LOG_DEBUG("address: 0x%8.8" PRIx32
" failed",
1575 int target_read_u8(struct target
*target
, uint32_t address
, uint8_t *value
)
1577 int retval
= target_read_memory(target
, address
, 1, 1, value
);
1578 if (!target_was_examined(target
))
1580 LOG_ERROR("Target not examined yet");
1584 if (retval
== ERROR_OK
)
1586 LOG_DEBUG("address: 0x%8.8" PRIx32
", value: 0x%2.2x",
1593 LOG_DEBUG("address: 0x%8.8" PRIx32
" failed",
1600 int target_write_u32(struct target
*target
, uint32_t address
, uint32_t value
)
1603 uint8_t value_buf
[4];
1604 if (!target_was_examined(target
))
1606 LOG_ERROR("Target not examined yet");
1610 LOG_DEBUG("address: 0x%8.8" PRIx32
", value: 0x%8.8" PRIx32
"",
1614 target_buffer_set_u32(target
, value_buf
, value
);
1615 if ((retval
= target_write_memory(target
, address
, 4, 1, value_buf
)) != ERROR_OK
)
1617 LOG_DEBUG("failed: %i", retval
);
1623 int target_write_u16(struct target
*target
, uint32_t address
, uint16_t value
)
1626 uint8_t value_buf
[2];
1627 if (!target_was_examined(target
))
1629 LOG_ERROR("Target not examined yet");
1633 LOG_DEBUG("address: 0x%8.8" PRIx32
", value: 0x%8.8x",
1637 target_buffer_set_u16(target
, value_buf
, value
);
1638 if ((retval
= target_write_memory(target
, address
, 2, 1, value_buf
)) != ERROR_OK
)
1640 LOG_DEBUG("failed: %i", retval
);
1646 int target_write_u8(struct target
*target
, uint32_t address
, uint8_t value
)
1649 if (!target_was_examined(target
))
1651 LOG_ERROR("Target not examined yet");
1655 LOG_DEBUG("address: 0x%8.8" PRIx32
", value: 0x%2.2x",
1658 if ((retval
= target_write_memory(target
, address
, 1, 1, &value
)) != ERROR_OK
)
1660 LOG_DEBUG("failed: %i", retval
);
1666 COMMAND_HANDLER(handle_targets_command
)
1668 struct target
*target
= all_targets
;
1672 target
= get_target(CMD_ARGV
[0]);
1673 if (target
== NULL
) {
1674 command_print(CMD_CTX
,"Target: %s is unknown, try one of:\n", CMD_ARGV
[0]);
1677 if (!target
->tap
->enabled
) {
1678 command_print(CMD_CTX
,"Target: TAP %s is disabled, "
1679 "can't be the current target\n",
1680 target
->tap
->dotted_name
);
1684 CMD_CTX
->current_target
= target
->target_number
;
1689 target
= all_targets
;
1690 command_print(CMD_CTX
, " TargetName Type Endian TapName State ");
1691 command_print(CMD_CTX
, "-- ------------------ ---------- ------ ------------------ ------------");
1697 if (target
->tap
->enabled
)
1698 state
= target_state_name( target
);
1700 state
= "tap-disabled";
1702 if (CMD_CTX
->current_target
== target
->target_number
)
1705 /* keep columns lined up to match the headers above */
1706 command_print(CMD_CTX
, "%2d%c %-18s %-10s %-6s %-18s %s",
1707 target
->target_number
,
1709 target_name(target
),
1710 target_type_name(target
),
1711 Jim_Nvp_value2name_simple(nvp_target_endian
,
1712 target
->endianness
)->name
,
1713 target
->tap
->dotted_name
,
1715 target
= target
->next
;
1721 /* every 300ms we check for reset & powerdropout and issue a "reset halt" if so. */
1723 static int powerDropout
;
1724 static int srstAsserted
;
1726 static int runPowerRestore
;
1727 static int runPowerDropout
;
1728 static int runSrstAsserted
;
1729 static int runSrstDeasserted
;
1731 static int sense_handler(void)
1733 static int prevSrstAsserted
= 0;
1734 static int prevPowerdropout
= 0;
1737 if ((retval
= jtag_power_dropout(&powerDropout
)) != ERROR_OK
)
1741 powerRestored
= prevPowerdropout
&& !powerDropout
;
1744 runPowerRestore
= 1;
1747 long long current
= timeval_ms();
1748 static long long lastPower
= 0;
1749 int waitMore
= lastPower
+ 2000 > current
;
1750 if (powerDropout
&& !waitMore
)
1752 runPowerDropout
= 1;
1753 lastPower
= current
;
1756 if ((retval
= jtag_srst_asserted(&srstAsserted
)) != ERROR_OK
)
1760 srstDeasserted
= prevSrstAsserted
&& !srstAsserted
;
1762 static long long lastSrst
= 0;
1763 waitMore
= lastSrst
+ 2000 > current
;
1764 if (srstDeasserted
&& !waitMore
)
1766 runSrstDeasserted
= 1;
1770 if (!prevSrstAsserted
&& srstAsserted
)
1772 runSrstAsserted
= 1;
1775 prevSrstAsserted
= srstAsserted
;
1776 prevPowerdropout
= powerDropout
;
1778 if (srstDeasserted
|| powerRestored
)
1780 /* Other than logging the event we can't do anything here.
1781 * Issuing a reset is a particularly bad idea as we might
1782 * be inside a reset already.
1789 static void target_call_event_callbacks_all(enum target_event e
) {
1790 struct target
*target
;
1791 target
= all_targets
;
1793 target_call_event_callbacks(target
, e
);
1794 target
= target
->next
;
1798 /* process target state changes */
1799 int handle_target(void *priv
)
1801 int retval
= ERROR_OK
;
1803 /* we do not want to recurse here... */
1804 static int recursive
= 0;
1809 /* danger! running these procedures can trigger srst assertions and power dropouts.
1810 * We need to avoid an infinite loop/recursion here and we do that by
1811 * clearing the flags after running these events.
1813 int did_something
= 0;
1814 if (runSrstAsserted
)
1816 target_call_event_callbacks_all(TARGET_EVENT_GDB_HALT
);
1817 Jim_Eval(interp
, "srst_asserted");
1820 if (runSrstDeasserted
)
1822 Jim_Eval(interp
, "srst_deasserted");
1825 if (runPowerDropout
)
1827 target_call_event_callbacks_all(TARGET_EVENT_GDB_HALT
);
1828 Jim_Eval(interp
, "power_dropout");
1831 if (runPowerRestore
)
1833 Jim_Eval(interp
, "power_restore");
1839 /* clear detect flags */
1843 /* clear action flags */
1845 runSrstAsserted
= 0;
1846 runSrstDeasserted
= 0;
1847 runPowerRestore
= 0;
1848 runPowerDropout
= 0;
1853 /* Poll targets for state changes unless that's globally disabled.
1854 * Skip targets that are currently disabled.
1856 for (struct target
*target
= all_targets
;
1857 is_jtag_poll_safe() && target
;
1858 target
= target
->next
)
1860 if (!target
->tap
->enabled
)
1863 /* only poll target if we've got power and srst isn't asserted */
1864 if (!powerDropout
&& !srstAsserted
)
1866 /* polling may fail silently until the target has been examined */
1867 if ((retval
= target_poll(target
)) != ERROR_OK
)
1869 target_call_event_callbacks(target
, TARGET_EVENT_GDB_HALT
);
1878 COMMAND_HANDLER(handle_reg_command
)
1880 struct target
*target
;
1881 struct reg
*reg
= NULL
;
1887 target
= get_current_target(CMD_CTX
);
1889 /* list all available registers for the current target */
1892 struct reg_cache
*cache
= target
->reg_cache
;
1899 command_print(CMD_CTX
, "===== %s", cache
->name
);
1901 for (i
= 0, reg
= cache
->reg_list
;
1902 i
< cache
->num_regs
;
1903 i
++, reg
++, count
++)
1905 /* only print cached values if they are valid */
1907 value
= buf_to_str(reg
->value
,
1909 command_print(CMD_CTX
,
1910 "(%i) %s (/%" PRIu32
"): 0x%s%s",
1918 command_print(CMD_CTX
, "(%i) %s (/%" PRIu32
")",
1923 cache
= cache
->next
;
1929 /* access a single register by its ordinal number */
1930 if ((CMD_ARGV
[0][0] >= '0') && (CMD_ARGV
[0][0] <= '9'))
1933 COMMAND_PARSE_NUMBER(uint
, CMD_ARGV
[0], num
);
1935 struct reg_cache
*cache
= target
->reg_cache
;
1940 for (i
= 0; i
< cache
->num_regs
; i
++)
1944 reg
= &cache
->reg_list
[i
];
1950 cache
= cache
->next
;
1955 command_print(CMD_CTX
, "%i is out of bounds, the current target has only %i registers (0 - %i)", num
, count
, count
- 1);
1958 } else /* access a single register by its name */
1960 reg
= register_get_by_name(target
->reg_cache
, CMD_ARGV
[0], 1);
1964 command_print(CMD_CTX
, "register %s not found in current target", CMD_ARGV
[0]);
1969 /* display a register */
1970 if ((CMD_ARGC
== 1) || ((CMD_ARGC
== 2) && !((CMD_ARGV
[1][0] >= '0') && (CMD_ARGV
[1][0] <= '9'))))
1972 if ((CMD_ARGC
== 2) && (strcmp(CMD_ARGV
[1], "force") == 0))
1975 if (reg
->valid
== 0)
1977 reg
->type
->get(reg
);
1979 value
= buf_to_str(reg
->value
, reg
->size
, 16);
1980 command_print(CMD_CTX
, "%s (/%i): 0x%s", reg
->name
, (int)(reg
->size
), value
);
1985 /* set register value */
1988 uint8_t *buf
= malloc(DIV_ROUND_UP(reg
->size
, 8));
1989 str_to_buf(CMD_ARGV
[1], strlen(CMD_ARGV
[1]), buf
, reg
->size
, 0);
1991 reg
->type
->set(reg
, buf
);
1993 value
= buf_to_str(reg
->value
, reg
->size
, 16);
1994 command_print(CMD_CTX
, "%s (/%i): 0x%s", reg
->name
, (int)(reg
->size
), value
);
2002 command_print(CMD_CTX
, "usage: reg <#|name> [value]");
2007 COMMAND_HANDLER(handle_poll_command
)
2009 int retval
= ERROR_OK
;
2010 struct target
*target
= get_current_target(CMD_CTX
);
2014 command_print(CMD_CTX
, "background polling: %s",
2015 jtag_poll_get_enabled() ? "on" : "off");
2016 command_print(CMD_CTX
, "TAP: %s (%s)",
2017 target
->tap
->dotted_name
,
2018 target
->tap
->enabled
? "enabled" : "disabled");
2019 if (!target
->tap
->enabled
)
2021 if ((retval
= target_poll(target
)) != ERROR_OK
)
2023 if ((retval
= target_arch_state(target
)) != ERROR_OK
)
2026 else if (CMD_ARGC
== 1)
2029 COMMAND_PARSE_ON_OFF(CMD_ARGV
[0], enable
);
2030 jtag_poll_set_enabled(enable
);
2034 return ERROR_COMMAND_SYNTAX_ERROR
;
2040 COMMAND_HANDLER(handle_wait_halt_command
)
2043 return ERROR_COMMAND_SYNTAX_ERROR
;
2048 int retval
= parse_uint(CMD_ARGV
[0], &ms
);
2049 if (ERROR_OK
!= retval
)
2051 command_print(CMD_CTX
, "usage: %s [seconds]", CMD_NAME
);
2052 return ERROR_COMMAND_SYNTAX_ERROR
;
2054 // convert seconds (given) to milliseconds (needed)
2058 struct target
*target
= get_current_target(CMD_CTX
);
2059 return target_wait_state(target
, TARGET_HALTED
, ms
);
2062 /* wait for target state to change. The trick here is to have a low
2063 * latency for short waits and not to suck up all the CPU time
2066 * After 500ms, keep_alive() is invoked
2068 int target_wait_state(struct target
*target
, enum target_state state
, int ms
)
2071 long long then
= 0, cur
;
2076 if ((retval
= target_poll(target
)) != ERROR_OK
)
2078 if (target
->state
== state
)
2086 then
= timeval_ms();
2087 LOG_DEBUG("waiting for target %s...",
2088 Jim_Nvp_value2name_simple(nvp_target_state
,state
)->name
);
2096 if ((cur
-then
) > ms
)
2098 LOG_ERROR("timed out while waiting for target %s",
2099 Jim_Nvp_value2name_simple(nvp_target_state
,state
)->name
);
2107 COMMAND_HANDLER(handle_halt_command
)
2111 struct target
*target
= get_current_target(CMD_CTX
);
2112 int retval
= target_halt(target
);
2113 if (ERROR_OK
!= retval
)
2119 retval
= parse_uint(CMD_ARGV
[0], &wait
);
2120 if (ERROR_OK
!= retval
)
2121 return ERROR_COMMAND_SYNTAX_ERROR
;
2126 return CALL_COMMAND_HANDLER(handle_wait_halt_command
);
2129 COMMAND_HANDLER(handle_soft_reset_halt_command
)
2131 struct target
*target
= get_current_target(CMD_CTX
);
2133 LOG_USER("requesting target halt and executing a soft reset");
2135 target
->type
->soft_reset_halt(target
);
2140 COMMAND_HANDLER(handle_reset_command
)
2143 return ERROR_COMMAND_SYNTAX_ERROR
;
2145 enum target_reset_mode reset_mode
= RESET_RUN
;
2149 n
= Jim_Nvp_name2value_simple(nvp_reset_modes
, CMD_ARGV
[0]);
2150 if ((n
->name
== NULL
) || (n
->value
== RESET_UNKNOWN
)) {
2151 return ERROR_COMMAND_SYNTAX_ERROR
;
2153 reset_mode
= n
->value
;
2156 /* reset *all* targets */
2157 return target_process_reset(CMD_CTX
, reset_mode
);
2161 COMMAND_HANDLER(handle_resume_command
)
2165 return ERROR_COMMAND_SYNTAX_ERROR
;
2167 struct target
*target
= get_current_target(CMD_CTX
);
2168 target_handle_event(target
, TARGET_EVENT_OLD_pre_resume
);
2170 /* with no CMD_ARGV, resume from current pc, addr = 0,
2171 * with one arguments, addr = CMD_ARGV[0],
2172 * handle breakpoints, not debugging */
2176 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], addr
);
2180 return target_resume(target
, current
, addr
, 1, 0);
2183 COMMAND_HANDLER(handle_step_command
)
2186 return ERROR_COMMAND_SYNTAX_ERROR
;
2190 /* with no CMD_ARGV, step from current pc, addr = 0,
2191 * with one argument addr = CMD_ARGV[0],
2192 * handle breakpoints, debugging */
2197 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], addr
);
2201 struct target
*target
= get_current_target(CMD_CTX
);
2203 return target
->type
->step(target
, current_pc
, addr
, 1);
2206 static void handle_md_output(struct command_context
*cmd_ctx
,
2207 struct target
*target
, uint32_t address
, unsigned size
,
2208 unsigned count
, const uint8_t *buffer
)
2210 const unsigned line_bytecnt
= 32;
2211 unsigned line_modulo
= line_bytecnt
/ size
;
2213 char output
[line_bytecnt
* 4 + 1];
2214 unsigned output_len
= 0;
2216 const char *value_fmt
;
2218 case 4: value_fmt
= "%8.8x "; break;
2219 case 2: value_fmt
= "%4.2x "; break;
2220 case 1: value_fmt
= "%2.2x "; break;
2222 LOG_ERROR("invalid memory read size: %u", size
);
2226 for (unsigned i
= 0; i
< count
; i
++)
2228 if (i
% line_modulo
== 0)
2230 output_len
+= snprintf(output
+ output_len
,
2231 sizeof(output
) - output_len
,
2233 (unsigned)(address
+ (i
*size
)));
2237 const uint8_t *value_ptr
= buffer
+ i
* size
;
2239 case 4: value
= target_buffer_get_u32(target
, value_ptr
); break;
2240 case 2: value
= target_buffer_get_u16(target
, value_ptr
); break;
2241 case 1: value
= *value_ptr
;
2243 output_len
+= snprintf(output
+ output_len
,
2244 sizeof(output
) - output_len
,
2247 if ((i
% line_modulo
== line_modulo
- 1) || (i
== count
- 1))
2249 command_print(cmd_ctx
, "%s", output
);
2255 COMMAND_HANDLER(handle_md_command
)
2258 return ERROR_COMMAND_SYNTAX_ERROR
;
2261 switch (CMD_NAME
[2]) {
2262 case 'w': size
= 4; break;
2263 case 'h': size
= 2; break;
2264 case 'b': size
= 1; break;
2265 default: return ERROR_COMMAND_SYNTAX_ERROR
;
2268 bool physical
=strcmp(CMD_ARGV
[0], "phys")==0;
2269 int (*fn
)(struct target
*target
,
2270 uint32_t address
, uint32_t size
, uint32_t count
, uint8_t *buffer
);
2275 fn
=target_read_phys_memory
;
2278 fn
=target_read_memory
;
2280 if ((CMD_ARGC
< 1) || (CMD_ARGC
> 2))
2282 return ERROR_COMMAND_SYNTAX_ERROR
;
2286 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], address
);
2290 COMMAND_PARSE_NUMBER(uint
, CMD_ARGV
[1], count
);
2292 uint8_t *buffer
= calloc(count
, size
);
2294 struct target
*target
= get_current_target(CMD_CTX
);
2295 int retval
= fn(target
, address
, size
, count
, buffer
);
2296 if (ERROR_OK
== retval
)
2297 handle_md_output(CMD_CTX
, target
, address
, size
, count
, buffer
);
2304 COMMAND_HANDLER(handle_mw_command
)
2308 return ERROR_COMMAND_SYNTAX_ERROR
;
2310 bool physical
=strcmp(CMD_ARGV
[0], "phys")==0;
2311 int (*fn
)(struct target
*target
,
2312 uint32_t address
, uint32_t size
, uint32_t count
, uint8_t *buffer
);
2317 fn
=target_write_phys_memory
;
2320 fn
=target_write_memory
;
2322 if ((CMD_ARGC
< 2) || (CMD_ARGC
> 3))
2323 return ERROR_COMMAND_SYNTAX_ERROR
;
2326 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], address
);
2329 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], value
);
2333 COMMAND_PARSE_NUMBER(uint
, CMD_ARGV
[2], count
);
2335 struct target
*target
= get_current_target(CMD_CTX
);
2337 uint8_t value_buf
[4];
2338 switch (CMD_NAME
[2])
2342 target_buffer_set_u32(target
, value_buf
, value
);
2346 target_buffer_set_u16(target
, value_buf
, value
);
2350 value_buf
[0] = value
;
2353 return ERROR_COMMAND_SYNTAX_ERROR
;
2355 for (unsigned i
= 0; i
< count
; i
++)
2357 int retval
= fn(target
,
2358 address
+ i
* wordsize
, wordsize
, 1, value_buf
);
2359 if (ERROR_OK
!= retval
)
2368 static COMMAND_HELPER(parse_load_image_command_CMD_ARGV
, struct image
*image
,
2369 uint32_t *min_address
, uint32_t *max_address
)
2371 if (CMD_ARGC
< 1 || CMD_ARGC
> 5)
2372 return ERROR_COMMAND_SYNTAX_ERROR
;
2374 /* a base address isn't always necessary,
2375 * default to 0x0 (i.e. don't relocate) */
2379 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], addr
);
2380 image
->base_address
= addr
;
2381 image
->base_address_set
= 1;
2384 image
->base_address_set
= 0;
2386 image
->start_address_set
= 0;
2390 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[3], *min_address
);
2394 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[4], *max_address
);
2395 // use size (given) to find max (required)
2396 *max_address
+= *min_address
;
2399 if (*min_address
> *max_address
)
2400 return ERROR_COMMAND_SYNTAX_ERROR
;
2405 COMMAND_HANDLER(handle_load_image_command
)
2409 uint32_t image_size
;
2410 uint32_t min_address
= 0;
2411 uint32_t max_address
= 0xffffffff;
2415 int retval
= CALL_COMMAND_HANDLER(parse_load_image_command_CMD_ARGV
,
2416 &image
, &min_address
, &max_address
);
2417 if (ERROR_OK
!= retval
)
2420 struct target
*target
= get_current_target(CMD_CTX
);
2422 struct duration bench
;
2423 duration_start(&bench
);
2425 if (image_open(&image
, CMD_ARGV
[0], (CMD_ARGC
>= 3) ? CMD_ARGV
[2] : NULL
) != ERROR_OK
)
2432 for (i
= 0; i
< image
.num_sections
; i
++)
2434 buffer
= malloc(image
.sections
[i
].size
);
2437 command_print(CMD_CTX
,
2438 "error allocating buffer for section (%d bytes)",
2439 (int)(image
.sections
[i
].size
));
2443 if ((retval
= image_read_section(&image
, i
, 0x0, image
.sections
[i
].size
, buffer
, &buf_cnt
)) != ERROR_OK
)
2449 uint32_t offset
= 0;
2450 uint32_t length
= buf_cnt
;
2452 /* DANGER!!! beware of unsigned comparision here!!! */
2454 if ((image
.sections
[i
].base_address
+ buf_cnt
>= min_address
)&&
2455 (image
.sections
[i
].base_address
< max_address
))
2457 if (image
.sections
[i
].base_address
< min_address
)
2459 /* clip addresses below */
2460 offset
+= min_address
-image
.sections
[i
].base_address
;
2464 if (image
.sections
[i
].base_address
+ buf_cnt
> max_address
)
2466 length
-= (image
.sections
[i
].base_address
+ buf_cnt
)-max_address
;
2469 if ((retval
= target_write_buffer(target
, image
.sections
[i
].base_address
+ offset
, length
, buffer
+ offset
)) != ERROR_OK
)
2474 image_size
+= length
;
2475 command_print(CMD_CTX
, "%u bytes written at address 0x%8.8" PRIx32
"",
2476 (unsigned int)length
,
2477 image
.sections
[i
].base_address
+ offset
);
2483 if ((ERROR_OK
== retval
) && (duration_measure(&bench
) == ERROR_OK
))
2485 command_print(CMD_CTX
, "downloaded %" PRIu32
" bytes "
2486 "in %fs (%0.3f kb/s)", image_size
,
2487 duration_elapsed(&bench
), duration_kbps(&bench
, image_size
));
2490 image_close(&image
);
2496 COMMAND_HANDLER(handle_dump_image_command
)
2498 struct fileio fileio
;
2500 uint8_t buffer
[560];
2504 struct target
*target
= get_current_target(CMD_CTX
);
2508 command_print(CMD_CTX
, "usage: dump_image <filename> <address> <size>");
2513 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], address
);
2515 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[2], size
);
2517 if (fileio_open(&fileio
, CMD_ARGV
[0], FILEIO_WRITE
, FILEIO_BINARY
) != ERROR_OK
)
2522 struct duration bench
;
2523 duration_start(&bench
);
2525 int retval
= ERROR_OK
;
2528 size_t size_written
;
2529 uint32_t this_run_size
= (size
> 560) ? 560 : size
;
2530 retval
= target_read_buffer(target
, address
, this_run_size
, buffer
);
2531 if (retval
!= ERROR_OK
)
2536 retval
= fileio_write(&fileio
, this_run_size
, buffer
, &size_written
);
2537 if (retval
!= ERROR_OK
)
2542 size
-= this_run_size
;
2543 address
+= this_run_size
;
2546 if ((retvaltemp
= fileio_close(&fileio
)) != ERROR_OK
)
2549 if ((ERROR_OK
== retval
) && (duration_measure(&bench
) == ERROR_OK
))
2551 command_print(CMD_CTX
,
2552 "dumped %zu bytes in %fs (%0.3f kb/s)", fileio
.size
,
2553 duration_elapsed(&bench
), duration_kbps(&bench
, fileio
.size
));
2559 static COMMAND_HELPER(handle_verify_image_command_internal
, int verify
)
2563 uint32_t image_size
;
2566 uint32_t checksum
= 0;
2567 uint32_t mem_checksum
= 0;
2571 struct target
*target
= get_current_target(CMD_CTX
);
2575 return ERROR_COMMAND_SYNTAX_ERROR
;
2580 LOG_ERROR("no target selected");
2584 struct duration bench
;
2585 duration_start(&bench
);
2590 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], addr
);
2591 image
.base_address
= addr
;
2592 image
.base_address_set
= 1;
2596 image
.base_address_set
= 0;
2597 image
.base_address
= 0x0;
2600 image
.start_address_set
= 0;
2602 if ((retval
= image_open(&image
, CMD_ARGV
[0], (CMD_ARGC
== 3) ? CMD_ARGV
[2] : NULL
)) != ERROR_OK
)
2609 for (i
= 0; i
< image
.num_sections
; i
++)
2611 buffer
= malloc(image
.sections
[i
].size
);
2614 command_print(CMD_CTX
,
2615 "error allocating buffer for section (%d bytes)",
2616 (int)(image
.sections
[i
].size
));
2619 if ((retval
= image_read_section(&image
, i
, 0x0, image
.sections
[i
].size
, buffer
, &buf_cnt
)) != ERROR_OK
)
2627 /* calculate checksum of image */
2628 image_calculate_checksum(buffer
, buf_cnt
, &checksum
);
2630 retval
= target_checksum_memory(target
, image
.sections
[i
].base_address
, buf_cnt
, &mem_checksum
);
2631 if (retval
!= ERROR_OK
)
2637 if (checksum
!= mem_checksum
)
2639 /* failed crc checksum, fall back to a binary compare */
2642 command_print(CMD_CTX
, "checksum mismatch - attempting binary compare");
2644 data
= (uint8_t*)malloc(buf_cnt
);
2646 /* Can we use 32bit word accesses? */
2648 int count
= buf_cnt
;
2649 if ((count
% 4) == 0)
2654 retval
= target_read_memory(target
, image
.sections
[i
].base_address
, size
, count
, data
);
2655 if (retval
== ERROR_OK
)
2658 for (t
= 0; t
< buf_cnt
; t
++)
2660 if (data
[t
] != buffer
[t
])
2662 command_print(CMD_CTX
,
2663 "Verify operation failed address 0x%08x. Was 0x%02x instead of 0x%02x\n",
2664 (unsigned)(t
+ image
.sections
[i
].base_address
),
2669 retval
= ERROR_FAIL
;
2683 command_print(CMD_CTX
, "address 0x%08" PRIx32
" length 0x%08zx",
2684 image
.sections
[i
].base_address
,
2689 image_size
+= buf_cnt
;
2692 if ((ERROR_OK
== retval
) && (duration_measure(&bench
) == ERROR_OK
))
2694 command_print(CMD_CTX
, "verified %" PRIu32
" bytes "
2695 "in %fs (%0.3f kb/s)", image_size
,
2696 duration_elapsed(&bench
), duration_kbps(&bench
, image_size
));
2699 image_close(&image
);
2704 COMMAND_HANDLER(handle_verify_image_command
)
2706 return CALL_COMMAND_HANDLER(handle_verify_image_command_internal
, 1);
2709 COMMAND_HANDLER(handle_test_image_command
)
2711 return CALL_COMMAND_HANDLER(handle_verify_image_command_internal
, 0);
2714 static int handle_bp_command_list(struct command_context
*cmd_ctx
)
2716 struct target
*target
= get_current_target(cmd_ctx
);
2717 struct breakpoint
*breakpoint
= target
->breakpoints
;
2720 if (breakpoint
->type
== BKPT_SOFT
)
2722 char* buf
= buf_to_str(breakpoint
->orig_instr
,
2723 breakpoint
->length
, 16);
2724 command_print(cmd_ctx
, "0x%8.8" PRIx32
", 0x%x, %i, 0x%s",
2725 breakpoint
->address
,
2727 breakpoint
->set
, buf
);
2732 command_print(cmd_ctx
, "0x%8.8" PRIx32
", 0x%x, %i",
2733 breakpoint
->address
,
2734 breakpoint
->length
, breakpoint
->set
);
2737 breakpoint
= breakpoint
->next
;
2742 static int handle_bp_command_set(struct command_context
*cmd_ctx
,
2743 uint32_t addr
, uint32_t length
, int hw
)
2745 struct target
*target
= get_current_target(cmd_ctx
);
2746 int retval
= breakpoint_add(target
, addr
, length
, hw
);
2747 if (ERROR_OK
== retval
)
2748 command_print(cmd_ctx
, "breakpoint set at 0x%8.8" PRIx32
"", addr
);
2750 LOG_ERROR("Failure setting breakpoint");
2754 COMMAND_HANDLER(handle_bp_command
)
2757 return handle_bp_command_list(CMD_CTX
);
2759 if (CMD_ARGC
< 2 || CMD_ARGC
> 3)
2761 command_print(CMD_CTX
, "usage: bp <address> <length> ['hw']");
2762 return ERROR_COMMAND_SYNTAX_ERROR
;
2766 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], addr
);
2768 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], length
);
2773 if (strcmp(CMD_ARGV
[2], "hw") == 0)
2776 return ERROR_COMMAND_SYNTAX_ERROR
;
2779 return handle_bp_command_set(CMD_CTX
, addr
, length
, hw
);
2782 COMMAND_HANDLER(handle_rbp_command
)
2785 return ERROR_COMMAND_SYNTAX_ERROR
;
2788 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], addr
);
2790 struct target
*target
= get_current_target(CMD_CTX
);
2791 breakpoint_remove(target
, addr
);
2796 COMMAND_HANDLER(handle_wp_command
)
2798 struct target
*target
= get_current_target(CMD_CTX
);
2802 struct watchpoint
*watchpoint
= target
->watchpoints
;
2806 command_print(CMD_CTX
, "address: 0x%8.8" PRIx32
2807 ", len: 0x%8.8" PRIx32
2808 ", r/w/a: %i, value: 0x%8.8" PRIx32
2809 ", mask: 0x%8.8" PRIx32
,
2810 watchpoint
->address
,
2812 (int)watchpoint
->rw
,
2815 watchpoint
= watchpoint
->next
;
2820 enum watchpoint_rw type
= WPT_ACCESS
;
2822 uint32_t length
= 0;
2823 uint32_t data_value
= 0x0;
2824 uint32_t data_mask
= 0xffffffff;
2829 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[4], data_mask
);
2832 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[3], data_value
);
2835 switch (CMD_ARGV
[2][0])
2847 LOG_ERROR("invalid watchpoint mode ('%c')", CMD_ARGV
[2][0]);
2848 return ERROR_COMMAND_SYNTAX_ERROR
;
2852 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], length
);
2853 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], addr
);
2857 command_print(CMD_CTX
, "usage: wp [address length "
2858 "[(r|w|a) [value [mask]]]]");
2859 return ERROR_COMMAND_SYNTAX_ERROR
;
2862 int retval
= watchpoint_add(target
, addr
, length
, type
,
2863 data_value
, data_mask
);
2864 if (ERROR_OK
!= retval
)
2865 LOG_ERROR("Failure setting watchpoints");
2870 COMMAND_HANDLER(handle_rwp_command
)
2873 return ERROR_COMMAND_SYNTAX_ERROR
;
2876 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], addr
);
2878 struct target
*target
= get_current_target(CMD_CTX
);
2879 watchpoint_remove(target
, addr
);
2886 * Translate a virtual address to a physical address.
2888 * The low-level target implementation must have logged a detailed error
2889 * which is forwarded to telnet/GDB session.
2891 COMMAND_HANDLER(handle_virt2phys_command
)
2894 return ERROR_COMMAND_SYNTAX_ERROR
;
2897 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], va
);
2900 struct target
*target
= get_current_target(CMD_CTX
);
2901 int retval
= target
->type
->virt2phys(target
, va
, &pa
);
2902 if (retval
== ERROR_OK
)
2903 command_print(CMD_CTX
, "Physical address 0x%08" PRIx32
"", pa
);
2908 static void writeData(FILE *f
, const void *data
, size_t len
)
2910 size_t written
= fwrite(data
, 1, len
, f
);
2912 LOG_ERROR("failed to write %zu bytes: %s", len
, strerror(errno
));
2915 static void writeLong(FILE *f
, int l
)
2918 for (i
= 0; i
< 4; i
++)
2920 char c
= (l
>> (i
*8))&0xff;
2921 writeData(f
, &c
, 1);
2926 static void writeString(FILE *f
, char *s
)
2928 writeData(f
, s
, strlen(s
));
2931 /* Dump a gmon.out histogram file. */
2932 static void writeGmon(uint32_t *samples
, uint32_t sampleNum
, const char *filename
)
2935 FILE *f
= fopen(filename
, "w");
2938 writeString(f
, "gmon");
2939 writeLong(f
, 0x00000001); /* Version */
2940 writeLong(f
, 0); /* padding */
2941 writeLong(f
, 0); /* padding */
2942 writeLong(f
, 0); /* padding */
2944 uint8_t zero
= 0; /* GMON_TAG_TIME_HIST */
2945 writeData(f
, &zero
, 1);
2947 /* figure out bucket size */
2948 uint32_t min
= samples
[0];
2949 uint32_t max
= samples
[0];
2950 for (i
= 0; i
< sampleNum
; i
++)
2952 if (min
> samples
[i
])
2956 if (max
< samples
[i
])
2962 int addressSpace
= (max
-min
+ 1);
2964 static const uint32_t maxBuckets
= 256 * 1024; /* maximum buckets. */
2965 uint32_t length
= addressSpace
;
2966 if (length
> maxBuckets
)
2968 length
= maxBuckets
;
2970 int *buckets
= malloc(sizeof(int)*length
);
2971 if (buckets
== NULL
)
2976 memset(buckets
, 0, sizeof(int)*length
);
2977 for (i
= 0; i
< sampleNum
;i
++)
2979 uint32_t address
= samples
[i
];
2980 long long a
= address
-min
;
2981 long long b
= length
-1;
2982 long long c
= addressSpace
-1;
2983 int index
= (a
*b
)/c
; /* danger!!!! int32 overflows */
2987 /* append binary memory gmon.out &profile_hist_hdr ((char*)&profile_hist_hdr + sizeof(struct gmon_hist_hdr)) */
2988 writeLong(f
, min
); /* low_pc */
2989 writeLong(f
, max
); /* high_pc */
2990 writeLong(f
, length
); /* # of samples */
2991 writeLong(f
, 64000000); /* 64MHz */
2992 writeString(f
, "seconds");
2993 for (i
= 0; i
< (15-strlen("seconds")); i
++)
2994 writeData(f
, &zero
, 1);
2995 writeString(f
, "s");
2997 /*append binary memory gmon.out profile_hist_data (profile_hist_data + profile_hist_hdr.hist_size) */
2999 char *data
= malloc(2*length
);
3002 for (i
= 0; i
< length
;i
++)
3011 data
[i
*2 + 1]=(val
>> 8)&0xff;
3014 writeData(f
, data
, length
* 2);
3024 /* profiling samples the CPU PC as quickly as OpenOCD is able, which will be used as a random sampling of PC */
3025 COMMAND_HANDLER(handle_profile_command
)
3027 struct target
*target
= get_current_target(CMD_CTX
);
3028 struct timeval timeout
, now
;
3030 gettimeofday(&timeout
, NULL
);
3033 return ERROR_COMMAND_SYNTAX_ERROR
;
3036 COMMAND_PARSE_NUMBER(uint
, CMD_ARGV
[0], offset
);
3038 timeval_add_time(&timeout
, offset
, 0);
3040 command_print(CMD_CTX
, "Starting profiling. Halting and resuming the target as often as we can...");
3042 static const int maxSample
= 10000;
3043 uint32_t *samples
= malloc(sizeof(uint32_t)*maxSample
);
3044 if (samples
== NULL
)
3048 /* hopefully it is safe to cache! We want to stop/restart as quickly as possible. */
3049 struct reg
*reg
= register_get_by_name(target
->reg_cache
, "pc", 1);
3054 target_poll(target
);
3055 if (target
->state
== TARGET_HALTED
)
3057 uint32_t t
=*((uint32_t *)reg
->value
);
3058 samples
[numSamples
++]=t
;
3059 retval
= target_resume(target
, 1, 0, 0, 0); /* current pc, addr = 0, do not handle breakpoints, not debugging */
3060 target_poll(target
);
3061 alive_sleep(10); /* sleep 10ms, i.e. <100 samples/second. */
3062 } else if (target
->state
== TARGET_RUNNING
)
3064 /* We want to quickly sample the PC. */
3065 if ((retval
= target_halt(target
)) != ERROR_OK
)
3072 command_print(CMD_CTX
, "Target not halted or running");
3076 if (retval
!= ERROR_OK
)
3081 gettimeofday(&now
, NULL
);
3082 if ((numSamples
>= maxSample
) || ((now
.tv_sec
>= timeout
.tv_sec
) && (now
.tv_usec
>= timeout
.tv_usec
)))
3084 command_print(CMD_CTX
, "Profiling completed. %d samples.", numSamples
);
3085 if ((retval
= target_poll(target
)) != ERROR_OK
)
3090 if (target
->state
== TARGET_HALTED
)
3092 target_resume(target
, 1, 0, 0, 0); /* current pc, addr = 0, do not handle breakpoints, not debugging */
3094 if ((retval
= target_poll(target
)) != ERROR_OK
)
3099 writeGmon(samples
, numSamples
, CMD_ARGV
[1]);
3100 command_print(CMD_CTX
, "Wrote %s", CMD_ARGV
[1]);
3109 static int new_int_array_element(Jim_Interp
* interp
, const char *varname
, int idx
, uint32_t val
)
3112 Jim_Obj
*nameObjPtr
, *valObjPtr
;
3115 namebuf
= alloc_printf("%s(%d)", varname
, idx
);
3119 nameObjPtr
= Jim_NewStringObj(interp
, namebuf
, -1);
3120 valObjPtr
= Jim_NewIntObj(interp
, val
);
3121 if (!nameObjPtr
|| !valObjPtr
)
3127 Jim_IncrRefCount(nameObjPtr
);
3128 Jim_IncrRefCount(valObjPtr
);
3129 result
= Jim_SetVariable(interp
, nameObjPtr
, valObjPtr
);
3130 Jim_DecrRefCount(interp
, nameObjPtr
);
3131 Jim_DecrRefCount(interp
, valObjPtr
);
3133 /* printf("%s(%d) <= 0%08x\n", varname, idx, val); */
3137 static int jim_mem2array(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
3139 struct command_context
*context
;
3140 struct target
*target
;
3142 context
= Jim_GetAssocData(interp
, "context");
3143 if (context
== NULL
)
3145 LOG_ERROR("mem2array: no command context");
3148 target
= get_current_target(context
);
3151 LOG_ERROR("mem2array: no current target");
3155 return target_mem2array(interp
, target
, argc
-1, argv
+ 1);
3158 static int target_mem2array(Jim_Interp
*interp
, struct target
*target
, int argc
, Jim_Obj
*const *argv
)
3166 const char *varname
;
3170 /* argv[1] = name of array to receive the data
3171 * argv[2] = desired width
3172 * argv[3] = memory address
3173 * argv[4] = count of times to read
3176 Jim_WrongNumArgs(interp
, 1, argv
, "varname width addr nelems");
3179 varname
= Jim_GetString(argv
[0], &len
);
3180 /* given "foo" get space for worse case "foo(%d)" .. add 20 */
3182 e
= Jim_GetLong(interp
, argv
[1], &l
);
3188 e
= Jim_GetLong(interp
, argv
[2], &l
);
3193 e
= Jim_GetLong(interp
, argv
[3], &l
);
3209 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3210 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "Invalid width param, must be 8/16/32", NULL
);
3214 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3215 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "mem2array: zero width read?", NULL
);
3218 if ((addr
+ (len
* width
)) < addr
) {
3219 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3220 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "mem2array: addr + len - wraps to zero?", NULL
);
3223 /* absurd transfer size? */
3225 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3226 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "mem2array: absurd > 64K item request", NULL
);
3231 ((width
== 2) && ((addr
& 1) == 0)) ||
3232 ((width
== 4) && ((addr
& 3) == 0))) {
3236 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3237 sprintf(buf
, "mem2array address: 0x%08" PRIx32
" is not aligned for %" PRId32
" byte reads",
3240 Jim_AppendStrings(interp
, Jim_GetResult(interp
), buf
, NULL
);
3249 size_t buffersize
= 4096;
3250 uint8_t *buffer
= malloc(buffersize
);
3257 /* Slurp... in buffer size chunks */
3259 count
= len
; /* in objects.. */
3260 if (count
> (buffersize
/width
)) {
3261 count
= (buffersize
/width
);
3264 retval
= target_read_memory(target
, addr
, width
, count
, buffer
);
3265 if (retval
!= ERROR_OK
) {
3267 LOG_ERROR("mem2array: Read @ 0x%08x, w=%d, cnt=%d, failed",
3271 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3272 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "mem2array: cannot read memory", NULL
);
3276 v
= 0; /* shut up gcc */
3277 for (i
= 0 ;i
< count
;i
++, n
++) {
3280 v
= target_buffer_get_u32(target
, &buffer
[i
*width
]);
3283 v
= target_buffer_get_u16(target
, &buffer
[i
*width
]);
3286 v
= buffer
[i
] & 0x0ff;
3289 new_int_array_element(interp
, varname
, n
, v
);
3297 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3302 static int get_int_array_element(Jim_Interp
* interp
, const char *varname
, int idx
, uint32_t *val
)
3305 Jim_Obj
*nameObjPtr
, *valObjPtr
;
3309 namebuf
= alloc_printf("%s(%d)", varname
, idx
);
3313 nameObjPtr
= Jim_NewStringObj(interp
, namebuf
, -1);
3320 Jim_IncrRefCount(nameObjPtr
);
3321 valObjPtr
= Jim_GetVariable(interp
, nameObjPtr
, JIM_ERRMSG
);
3322 Jim_DecrRefCount(interp
, nameObjPtr
);
3324 if (valObjPtr
== NULL
)
3327 result
= Jim_GetLong(interp
, valObjPtr
, &l
);
3328 /* printf("%s(%d) => 0%08x\n", varname, idx, val); */
3333 static int jim_array2mem(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
3335 struct command_context
*context
;
3336 struct target
*target
;
3338 context
= Jim_GetAssocData(interp
, "context");
3339 if (context
== NULL
) {
3340 LOG_ERROR("array2mem: no command context");
3343 target
= get_current_target(context
);
3344 if (target
== NULL
) {
3345 LOG_ERROR("array2mem: no current target");
3349 return target_array2mem(interp
,target
, argc
-1, argv
+ 1);
3351 static int target_array2mem(Jim_Interp
*interp
, struct target
*target
, int argc
, Jim_Obj
*const *argv
)
3359 const char *varname
;
3363 /* argv[1] = name of array to get the data
3364 * argv[2] = desired width
3365 * argv[3] = memory address
3366 * argv[4] = count to write
3369 Jim_WrongNumArgs(interp
, 0, argv
, "varname width addr nelems");
3372 varname
= Jim_GetString(argv
[0], &len
);
3373 /* given "foo" get space for worse case "foo(%d)" .. add 20 */
3375 e
= Jim_GetLong(interp
, argv
[1], &l
);
3381 e
= Jim_GetLong(interp
, argv
[2], &l
);
3386 e
= Jim_GetLong(interp
, argv
[3], &l
);
3402 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3403 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "Invalid width param, must be 8/16/32", NULL
);
3407 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3408 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "array2mem: zero width read?", NULL
);
3411 if ((addr
+ (len
* width
)) < addr
) {
3412 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3413 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "array2mem: addr + len - wraps to zero?", NULL
);
3416 /* absurd transfer size? */
3418 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3419 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "array2mem: absurd > 64K item request", NULL
);
3424 ((width
== 2) && ((addr
& 1) == 0)) ||
3425 ((width
== 4) && ((addr
& 3) == 0))) {
3429 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3430 sprintf(buf
, "array2mem address: 0x%08x is not aligned for %d byte reads",
3433 Jim_AppendStrings(interp
, Jim_GetResult(interp
), buf
, NULL
);
3444 size_t buffersize
= 4096;
3445 uint8_t *buffer
= malloc(buffersize
);
3450 /* Slurp... in buffer size chunks */
3452 count
= len
; /* in objects.. */
3453 if (count
> (buffersize
/width
)) {
3454 count
= (buffersize
/width
);
3457 v
= 0; /* shut up gcc */
3458 for (i
= 0 ;i
< count
;i
++, n
++) {
3459 get_int_array_element(interp
, varname
, n
, &v
);
3462 target_buffer_set_u32(target
, &buffer
[i
*width
], v
);
3465 target_buffer_set_u16(target
, &buffer
[i
*width
], v
);
3468 buffer
[i
] = v
& 0x0ff;
3474 retval
= target_write_memory(target
, addr
, width
, count
, buffer
);
3475 if (retval
!= ERROR_OK
) {
3477 LOG_ERROR("array2mem: Write @ 0x%08x, w=%d, cnt=%d, failed",
3481 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3482 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "array2mem: cannot read memory", NULL
);
3490 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3495 void target_all_handle_event(enum target_event e
)
3497 struct target
*target
;
3499 LOG_DEBUG("**all*targets: event: %d, %s",
3501 Jim_Nvp_value2name_simple(nvp_target_event
, e
)->name
);
3503 target
= all_targets
;
3505 target_handle_event(target
, e
);
3506 target
= target
->next
;
3511 /* FIX? should we propagate errors here rather than printing them
3514 void target_handle_event(struct target
*target
, enum target_event e
)
3516 struct target_event_action
*teap
;
3518 for (teap
= target
->event_action
; teap
!= NULL
; teap
= teap
->next
) {
3519 if (teap
->event
== e
) {
3520 LOG_DEBUG("target: (%d) %s (%s) event: %d (%s) action: %s",
3521 target
->target_number
,
3522 target_name(target
),
3523 target_type_name(target
),
3525 Jim_Nvp_value2name_simple(nvp_target_event
, e
)->name
,
3526 Jim_GetString(teap
->body
, NULL
));
3527 if (Jim_EvalObj(interp
, teap
->body
) != JIM_OK
)
3529 Jim_PrintErrorMessage(interp
);
3536 * Returns true only if the target has a handler for the specified event.
3538 bool target_has_event_action(struct target
*target
, enum target_event event
)
3540 struct target_event_action
*teap
;
3542 for (teap
= target
->event_action
; teap
!= NULL
; teap
= teap
->next
) {
3543 if (teap
->event
== event
)
3549 enum target_cfg_param
{
3552 TCFG_WORK_AREA_VIRT
,
3553 TCFG_WORK_AREA_PHYS
,
3554 TCFG_WORK_AREA_SIZE
,
3555 TCFG_WORK_AREA_BACKUP
,
3558 TCFG_CHAIN_POSITION
,
3561 static Jim_Nvp nvp_config_opts
[] = {
3562 { .name
= "-type", .value
= TCFG_TYPE
},
3563 { .name
= "-event", .value
= TCFG_EVENT
},
3564 { .name
= "-work-area-virt", .value
= TCFG_WORK_AREA_VIRT
},
3565 { .name
= "-work-area-phys", .value
= TCFG_WORK_AREA_PHYS
},
3566 { .name
= "-work-area-size", .value
= TCFG_WORK_AREA_SIZE
},
3567 { .name
= "-work-area-backup", .value
= TCFG_WORK_AREA_BACKUP
},
3568 { .name
= "-endian" , .value
= TCFG_ENDIAN
},
3569 { .name
= "-variant", .value
= TCFG_VARIANT
},
3570 { .name
= "-chain-position", .value
= TCFG_CHAIN_POSITION
},
3572 { .name
= NULL
, .value
= -1 }
3575 static int target_configure(Jim_GetOptInfo
*goi
, struct target
*target
)
3583 /* parse config or cget options ... */
3584 while (goi
->argc
> 0) {
3585 Jim_SetEmptyResult(goi
->interp
);
3586 /* Jim_GetOpt_Debug(goi); */
3588 if (target
->type
->target_jim_configure
) {
3589 /* target defines a configure function */
3590 /* target gets first dibs on parameters */
3591 e
= (*(target
->type
->target_jim_configure
))(target
, goi
);
3600 /* otherwise we 'continue' below */
3602 e
= Jim_GetOpt_Nvp(goi
, nvp_config_opts
, &n
);
3604 Jim_GetOpt_NvpUnknown(goi
, nvp_config_opts
, 0);
3610 if (goi
->isconfigure
) {
3611 Jim_SetResult_sprintf(goi
->interp
,
3612 "not settable: %s", n
->name
);
3616 if (goi
->argc
!= 0) {
3617 Jim_WrongNumArgs(goi
->interp
,
3618 goi
->argc
, goi
->argv
,
3623 Jim_SetResultString(goi
->interp
,
3624 target_type_name(target
), -1);
3628 if (goi
->argc
== 0) {
3629 Jim_WrongNumArgs(goi
->interp
, goi
->argc
, goi
->argv
, "-event ?event-name? ...");
3633 e
= Jim_GetOpt_Nvp(goi
, nvp_target_event
, &n
);
3635 Jim_GetOpt_NvpUnknown(goi
, nvp_target_event
, 1);
3639 if (goi
->isconfigure
) {
3640 if (goi
->argc
!= 1) {
3641 Jim_WrongNumArgs(goi
->interp
, goi
->argc
, goi
->argv
, "-event ?event-name? ?EVENT-BODY?");
3645 if (goi
->argc
!= 0) {
3646 Jim_WrongNumArgs(goi
->interp
, goi
->argc
, goi
->argv
, "-event ?event-name?");
3652 struct target_event_action
*teap
;
3654 teap
= target
->event_action
;
3655 /* replace existing? */
3657 if (teap
->event
== (enum target_event
)n
->value
) {
3663 if (goi
->isconfigure
) {
3664 bool replace
= true;
3667 teap
= calloc(1, sizeof(*teap
));
3670 teap
->event
= n
->value
;
3671 Jim_GetOpt_Obj(goi
, &o
);
3673 Jim_DecrRefCount(interp
, teap
->body
);
3675 teap
->body
= Jim_DuplicateObj(goi
->interp
, o
);
3678 * Tcl/TK - "tk events" have a nice feature.
3679 * See the "BIND" command.
3680 * We should support that here.
3681 * You can specify %X and %Y in the event code.
3682 * The idea is: %T - target name.
3683 * The idea is: %N - target number
3684 * The idea is: %E - event name.
3686 Jim_IncrRefCount(teap
->body
);
3690 /* add to head of event list */
3691 teap
->next
= target
->event_action
;
3692 target
->event_action
= teap
;
3694 Jim_SetEmptyResult(goi
->interp
);
3698 Jim_SetEmptyResult(goi
->interp
);
3700 Jim_SetResult(goi
->interp
, Jim_DuplicateObj(goi
->interp
, teap
->body
));
3707 case TCFG_WORK_AREA_VIRT
:
3708 if (goi
->isconfigure
) {
3709 target_free_all_working_areas(target
);
3710 e
= Jim_GetOpt_Wide(goi
, &w
);
3714 target
->working_area_virt
= w
;
3715 target
->working_area_virt_spec
= true;
3717 if (goi
->argc
!= 0) {
3721 Jim_SetResult(interp
, Jim_NewIntObj(goi
->interp
, target
->working_area_virt
));
3725 case TCFG_WORK_AREA_PHYS
:
3726 if (goi
->isconfigure
) {
3727 target_free_all_working_areas(target
);
3728 e
= Jim_GetOpt_Wide(goi
, &w
);
3732 target
->working_area_phys
= w
;
3733 target
->working_area_phys_spec
= true;
3735 if (goi
->argc
!= 0) {
3739 Jim_SetResult(interp
, Jim_NewIntObj(goi
->interp
, target
->working_area_phys
));
3743 case TCFG_WORK_AREA_SIZE
:
3744 if (goi
->isconfigure
) {
3745 target_free_all_working_areas(target
);
3746 e
= Jim_GetOpt_Wide(goi
, &w
);
3750 target
->working_area_size
= w
;
3752 if (goi
->argc
!= 0) {
3756 Jim_SetResult(interp
, Jim_NewIntObj(goi
->interp
, target
->working_area_size
));
3760 case TCFG_WORK_AREA_BACKUP
:
3761 if (goi
->isconfigure
) {
3762 target_free_all_working_areas(target
);
3763 e
= Jim_GetOpt_Wide(goi
, &w
);
3767 /* make this exactly 1 or 0 */
3768 target
->backup_working_area
= (!!w
);
3770 if (goi
->argc
!= 0) {
3774 Jim_SetResult(interp
, Jim_NewIntObj(goi
->interp
, target
->backup_working_area
));
3775 /* loop for more e*/
3779 if (goi
->isconfigure
) {
3780 e
= Jim_GetOpt_Nvp(goi
, nvp_target_endian
, &n
);
3782 Jim_GetOpt_NvpUnknown(goi
, nvp_target_endian
, 1);
3785 target
->endianness
= n
->value
;
3787 if (goi
->argc
!= 0) {
3791 n
= Jim_Nvp_value2name_simple(nvp_target_endian
, target
->endianness
);
3792 if (n
->name
== NULL
) {
3793 target
->endianness
= TARGET_LITTLE_ENDIAN
;
3794 n
= Jim_Nvp_value2name_simple(nvp_target_endian
, target
->endianness
);
3796 Jim_SetResultString(goi
->interp
, n
->name
, -1);
3801 if (goi
->isconfigure
) {
3802 if (goi
->argc
< 1) {
3803 Jim_SetResult_sprintf(goi
->interp
,
3808 if (target
->variant
) {
3809 free((void *)(target
->variant
));
3811 e
= Jim_GetOpt_String(goi
, &cp
, NULL
);
3812 target
->variant
= strdup(cp
);
3814 if (goi
->argc
!= 0) {
3818 Jim_SetResultString(goi
->interp
, target
->variant
,-1);
3821 case TCFG_CHAIN_POSITION
:
3822 if (goi
->isconfigure
) {
3824 struct jtag_tap
*tap
;
3825 target_free_all_working_areas(target
);
3826 e
= Jim_GetOpt_Obj(goi
, &o
);
3830 tap
= jtag_tap_by_jim_obj(goi
->interp
, o
);
3834 /* make this exactly 1 or 0 */
3837 if (goi
->argc
!= 0) {
3841 Jim_SetResultString(interp
, target
->tap
->dotted_name
, -1);
3842 /* loop for more e*/
3845 } /* while (goi->argc) */
3848 /* done - we return */
3852 static int jim_target_configure(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
3855 Jim_GetOpt_Setup(&goi
, interp
, argc
- 1, argv
+ 1);
3856 goi
.isconfigure
= strcmp(Jim_GetString(argv
[0], NULL
), "configure") == 0;
3857 int need_args
= 1 + goi
.isconfigure
;
3858 if (goi
.argc
< need_args
)
3860 Jim_WrongNumArgs(goi
.interp
, goi
.argc
, goi
.argv
,
3862 ? "missing: -option VALUE ..."
3863 : "missing: -option ...");
3866 struct target
*target
= Jim_CmdPrivData(goi
.interp
);
3867 return target_configure(&goi
, target
);
3870 static int jim_target_mw(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
3872 const char *cmd_name
= Jim_GetString(argv
[0], NULL
);
3875 Jim_GetOpt_Setup(&goi
, interp
, argc
- 1, argv
+ 1);
3877 if (goi
.argc
!= 2 && goi
.argc
!= 3)
3879 Jim_SetResult_sprintf(goi
.interp
,
3880 "usage: %s <address> <data> [<count>]", cmd_name
);
3885 int e
= Jim_GetOpt_Wide(&goi
, &a
);
3890 e
= Jim_GetOpt_Wide(&goi
, &b
);
3897 e
= Jim_GetOpt_Wide(&goi
, &c
);
3902 struct target
*target
= Jim_CmdPrivData(goi
.interp
);
3903 uint8_t target_buf
[32];
3904 if (strcasecmp(cmd_name
, "mww") == 0) {
3905 target_buffer_set_u32(target
, target_buf
, b
);
3908 else if (strcasecmp(cmd_name
, "mwh") == 0) {
3909 target_buffer_set_u16(target
, target_buf
, b
);
3912 else if (strcasecmp(cmd_name
, "mwb") == 0) {
3913 target_buffer_set_u8(target
, target_buf
, b
);
3916 LOG_ERROR("command '%s' unknown: ", cmd_name
);
3920 for (jim_wide x
= 0; x
< c
; x
++)
3922 e
= target_write_memory(target
, a
, b
, 1, target_buf
);
3925 Jim_SetResult_sprintf(interp
,
3926 "Error writing @ 0x%08x: %d\n", (int)(a
), e
);
3935 static int jim_target_md(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
3937 const char *cmd_name
= Jim_GetString(argv
[0], NULL
);
3940 Jim_GetOpt_Setup(&goi
, interp
, argc
- 1, argv
+ 1);
3942 if ((goi
.argc
== 2) || (goi
.argc
== 3))
3944 Jim_SetResult_sprintf(goi
.interp
,
3945 "usage: %s <address> [<count>]", cmd_name
);
3950 int e
= Jim_GetOpt_Wide(&goi
, &a
);
3956 e
= Jim_GetOpt_Wide(&goi
, &c
);
3963 jim_wide b
= 1; /* shut up gcc */
3964 if (strcasecmp(cmd_name
, "mdw") == 0)
3966 else if (strcasecmp(cmd_name
, "mdh") == 0)
3968 else if (strcasecmp(cmd_name
, "mdb") == 0)
3971 LOG_ERROR("command '%s' unknown: ", cmd_name
);
3975 /* convert count to "bytes" */
3978 struct target
*target
= Jim_CmdPrivData(goi
.interp
);
3979 uint8_t target_buf
[32];
3986 e
= target_read_memory(target
, a
, b
, y
/ b
, target_buf
);
3987 if (e
!= ERROR_OK
) {
3988 Jim_SetResult_sprintf(interp
, "error reading target @ 0x%08lx", (int)(a
));
3992 Jim_fprintf(interp
, interp
->cookie_stdout
, "0x%08x ", (int)(a
));
3995 for (x
= 0; x
< 16 && x
< y
; x
+= 4)
3997 z
= target_buffer_get_u32(target
, &(target_buf
[ x
* 4 ]));
3998 Jim_fprintf(interp
, interp
->cookie_stdout
, "%08x ", (int)(z
));
4000 for (; (x
< 16) ; x
+= 4) {
4001 Jim_fprintf(interp
, interp
->cookie_stdout
, " ");
4005 for (x
= 0; x
< 16 && x
< y
; x
+= 2)
4007 z
= target_buffer_get_u16(target
, &(target_buf
[ x
* 2 ]));
4008 Jim_fprintf(interp
, interp
->cookie_stdout
, "%04x ", (int)(z
));
4010 for (; (x
< 16) ; x
+= 2) {
4011 Jim_fprintf(interp
, interp
->cookie_stdout
, " ");
4016 for (x
= 0 ; (x
< 16) && (x
< y
) ; x
+= 1) {
4017 z
= target_buffer_get_u8(target
, &(target_buf
[ x
* 4 ]));
4018 Jim_fprintf(interp
, interp
->cookie_stdout
, "%02x ", (int)(z
));
4020 for (; (x
< 16) ; x
+= 1) {
4021 Jim_fprintf(interp
, interp
->cookie_stdout
, " ");
4025 /* ascii-ify the bytes */
4026 for (x
= 0 ; x
< y
; x
++) {
4027 if ((target_buf
[x
] >= 0x20) &&
4028 (target_buf
[x
] <= 0x7e)) {
4032 target_buf
[x
] = '.';
4037 target_buf
[x
] = ' ';
4042 /* print - with a newline */
4043 Jim_fprintf(interp
, interp
->cookie_stdout
, "%s\n", target_buf
);
4051 static int jim_target_mem2array(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4053 struct target
*target
= Jim_CmdPrivData(interp
);
4054 return target_mem2array(interp
, target
, argc
- 1, argv
+ 1);
4057 static int jim_target_array2mem(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4059 struct target
*target
= Jim_CmdPrivData(interp
);
4060 return target_array2mem(interp
, target
, argc
- 1, argv
+ 1);
4063 static int jim_target_tap_disabled(Jim_Interp
*interp
)
4065 Jim_SetResult_sprintf(interp
, "[TAP is disabled]");
4069 static int jim_target_examine(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4073 Jim_WrongNumArgs(interp
, 1, argv
, "[no parameters]");
4076 struct target
*target
= Jim_CmdPrivData(interp
);
4077 if (!target
->tap
->enabled
)
4078 return jim_target_tap_disabled(interp
);
4080 int e
= target
->type
->examine(target
);
4083 Jim_SetResult_sprintf(interp
, "examine-fails: %d", e
);
4089 static int jim_target_poll(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4093 Jim_WrongNumArgs(interp
, 1, argv
, "[no parameters]");
4096 struct target
*target
= Jim_CmdPrivData(interp
);
4097 if (!target
->tap
->enabled
)
4098 return jim_target_tap_disabled(interp
);
4101 if (!(target_was_examined(target
))) {
4102 e
= ERROR_TARGET_NOT_EXAMINED
;
4104 e
= target
->type
->poll(target
);
4108 Jim_SetResult_sprintf(interp
, "poll-fails: %d", e
);
4114 static int jim_target_reset(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4117 Jim_GetOpt_Setup(&goi
, interp
, argc
- 1, argv
+ 1);
4121 Jim_WrongNumArgs(interp
, 0, argv
,
4122 "([tT]|[fF]|assert|deassert) BOOL");
4127 int e
= Jim_GetOpt_Nvp(&goi
, nvp_assert
, &n
);
4130 Jim_GetOpt_NvpUnknown(&goi
, nvp_assert
, 1);
4133 /* the halt or not param */
4135 e
= Jim_GetOpt_Wide(&goi
, &a
);
4139 struct target
*target
= Jim_CmdPrivData(goi
.interp
);
4140 if (!target
->tap
->enabled
)
4141 return jim_target_tap_disabled(interp
);
4142 if (!target
->type
->assert_reset
|| !target
->type
->deassert_reset
)
4144 Jim_SetResult_sprintf(interp
,
4145 "No target-specific reset for %s",
4146 target_name(target
));
4149 /* determine if we should halt or not. */
4150 target
->reset_halt
= !!a
;
4151 /* When this happens - all workareas are invalid. */
4152 target_free_all_working_areas_restore(target
, 0);
4155 if (n
->value
== NVP_ASSERT
) {
4156 e
= target
->type
->assert_reset(target
);
4158 e
= target
->type
->deassert_reset(target
);
4160 return (e
== ERROR_OK
) ? JIM_OK
: JIM_ERR
;
4163 static int jim_target_halt(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4166 Jim_WrongNumArgs(interp
, 1, argv
, "[no parameters]");
4169 struct target
*target
= Jim_CmdPrivData(interp
);
4170 if (!target
->tap
->enabled
)
4171 return jim_target_tap_disabled(interp
);
4172 int e
= target
->type
->halt(target
);
4173 return (e
== ERROR_OK
) ? JIM_OK
: JIM_ERR
;
4176 static int jim_target_wait_state(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4179 Jim_GetOpt_Setup(&goi
, interp
, argc
- 1, argv
+ 1);
4181 /* params: <name> statename timeoutmsecs */
4184 const char *cmd_name
= Jim_GetString(argv
[0], NULL
);
4185 Jim_SetResult_sprintf(goi
.interp
,
4186 "%s <state_name> <timeout_in_msec>", cmd_name
);
4191 int e
= Jim_GetOpt_Nvp(&goi
, nvp_target_state
, &n
);
4193 Jim_GetOpt_NvpUnknown(&goi
, nvp_target_state
,1);
4197 e
= Jim_GetOpt_Wide(&goi
, &a
);
4201 struct target
*target
= Jim_CmdPrivData(interp
);
4202 if (!target
->tap
->enabled
)
4203 return jim_target_tap_disabled(interp
);
4205 e
= target_wait_state(target
, n
->value
, a
);
4208 Jim_SetResult_sprintf(goi
.interp
,
4209 "target: %s wait %s fails (%d) %s",
4210 target_name(target
), n
->name
,
4211 e
, target_strerror_safe(e
));
4216 /* List for human, Events defined for this target.
4217 * scripts/programs should use 'name cget -event NAME'
4219 static int jim_target_event_list(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4221 struct command_context
*cmd_ctx
= Jim_GetAssocData(interp
, "context");
4222 struct target
*target
= Jim_CmdPrivData(interp
);
4223 struct target_event_action
*teap
= target
->event_action
;
4224 command_print(cmd_ctx
, "Event actions for target (%d) %s\n",
4225 target
->target_number
,
4226 target_name(target
));
4227 command_print(cmd_ctx
, "%-25s | Body", "Event");
4228 command_print(cmd_ctx
, "------------------------- | "
4229 "----------------------------------------");
4232 Jim_Nvp
*opt
= Jim_Nvp_value2name_simple(nvp_target_event
, teap
->event
);
4233 command_print(cmd_ctx
, "%-25s | %s",
4234 opt
->name
, Jim_GetString(teap
->body
, NULL
));
4237 command_print(cmd_ctx
, "***END***");
4240 static int jim_target_current_state(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4244 Jim_WrongNumArgs(interp
, 1, argv
, "[no parameters]");
4247 struct target
*target
= Jim_CmdPrivData(interp
);
4248 Jim_SetResultString(interp
, target_state_name(target
), -1);
4251 static int jim_target_invoke_event(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4254 Jim_GetOpt_Setup(&goi
, interp
, argc
- 1, argv
+ 1);
4257 const char *cmd_name
= Jim_GetString(argv
[0], NULL
);
4258 Jim_SetResult_sprintf(goi
.interp
, "%s <eventname>", cmd_name
);
4262 int e
= Jim_GetOpt_Nvp(&goi
, nvp_target_event
, &n
);
4265 Jim_GetOpt_NvpUnknown(&goi
, nvp_target_event
, 1);
4268 struct target
*target
= Jim_CmdPrivData(interp
);
4269 target_handle_event(target
, n
->value
);
4273 static const struct command_registration target_instance_command_handlers
[] = {
4275 .name
= "configure",
4276 .mode
= COMMAND_CONFIG
,
4277 .jim_handler
= &jim_target_configure
,
4278 .usage
= "[<target_options> ...]",
4279 .help
= "configure a new target for use",
4283 .mode
= COMMAND_ANY
,
4284 .jim_handler
= &jim_target_configure
,
4285 .usage
= "<target_type> [<target_options> ...]",
4286 .help
= "configure a new target for use",
4290 .mode
= COMMAND_EXEC
,
4291 .jim_handler
= &jim_target_mw
,
4292 .usage
= "<address> <data> [<count>]",
4293 .help
= "Write 32-bit word(s) to target memory",
4297 .mode
= COMMAND_EXEC
,
4298 .jim_handler
= &jim_target_mw
,
4299 .usage
= "<address> <data> [<count>]",
4300 .help
= "Write 16-bit half-word(s) to target memory",
4304 .mode
= COMMAND_EXEC
,
4305 .jim_handler
= &jim_target_mw
,
4306 .usage
= "<address> <data> [<count>]",
4307 .help
= "Write byte(s) to target memory",
4311 .mode
= COMMAND_EXEC
,
4312 .jim_handler
= &jim_target_md
,
4313 .usage
= "<address> [<count>]",
4314 .help
= "Display target memory as 32-bit words",
4318 .mode
= COMMAND_EXEC
,
4319 .jim_handler
= &jim_target_md
,
4320 .usage
= "<address> [<count>]",
4321 .help
= "Display target memory as 16-bit half-words",
4325 .mode
= COMMAND_EXEC
,
4326 .jim_handler
= &jim_target_md
,
4327 .usage
= "<address> [<count>]",
4328 .help
= "Display target memory as 8-bit bytes",
4331 .name
= "array2mem",
4332 .mode
= COMMAND_EXEC
,
4333 .jim_handler
= &jim_target_array2mem
,
4336 .name
= "mem2array",
4337 .mode
= COMMAND_EXEC
,
4338 .jim_handler
= &jim_target_mem2array
,
4341 .name
= "eventlist",
4342 .mode
= COMMAND_EXEC
,
4343 .jim_handler
= &jim_target_event_list
,
4347 .mode
= COMMAND_EXEC
,
4348 .jim_handler
= &jim_target_current_state
,
4351 .name
= "arp_examine",
4352 .mode
= COMMAND_EXEC
,
4353 .jim_handler
= &jim_target_examine
,
4357 .mode
= COMMAND_EXEC
,
4358 .jim_handler
= &jim_target_poll
,
4361 .name
= "arp_reset",
4362 .mode
= COMMAND_EXEC
,
4363 .jim_handler
= &jim_target_reset
,
4367 .mode
= COMMAND_EXEC
,
4368 .jim_handler
= &jim_target_halt
,
4371 .name
= "arp_waitstate",
4372 .mode
= COMMAND_EXEC
,
4373 .jim_handler
= &jim_target_wait_state
,
4376 .name
= "invoke-event",
4377 .mode
= COMMAND_EXEC
,
4378 .jim_handler
= &jim_target_invoke_event
,
4380 COMMAND_REGISTRATION_DONE
4383 static int target_create(Jim_GetOptInfo
*goi
)
4391 struct target
*target
;
4392 struct command_context
*cmd_ctx
;
4394 cmd_ctx
= Jim_GetAssocData(goi
->interp
, "context");
4395 if (goi
->argc
< 3) {
4396 Jim_WrongNumArgs(goi
->interp
, 1, goi
->argv
, "?name? ?type? ..options...");
4401 Jim_GetOpt_Obj(goi
, &new_cmd
);
4402 /* does this command exist? */
4403 cmd
= Jim_GetCommand(goi
->interp
, new_cmd
, JIM_ERRMSG
);
4405 cp
= Jim_GetString(new_cmd
, NULL
);
4406 Jim_SetResult_sprintf(goi
->interp
, "Command/target: %s Exists", cp
);
4411 e
= Jim_GetOpt_String(goi
, &cp2
, NULL
);
4413 /* now does target type exist */
4414 for (x
= 0 ; target_types
[x
] ; x
++) {
4415 if (0 == strcmp(cp
, target_types
[x
]->name
)) {
4420 if (target_types
[x
] == NULL
) {
4421 Jim_SetResult_sprintf(goi
->interp
, "Unknown target type %s, try one of ", cp
);
4422 for (x
= 0 ; target_types
[x
] ; x
++) {
4423 if (target_types
[x
+ 1]) {
4424 Jim_AppendStrings(goi
->interp
,
4425 Jim_GetResult(goi
->interp
),
4426 target_types
[x
]->name
,
4429 Jim_AppendStrings(goi
->interp
,
4430 Jim_GetResult(goi
->interp
),
4432 target_types
[x
]->name
,NULL
);
4439 target
= calloc(1,sizeof(struct target
));
4440 /* set target number */
4441 target
->target_number
= new_target_number();
4443 /* allocate memory for each unique target type */
4444 target
->type
= (struct target_type
*)calloc(1,sizeof(struct target_type
));
4446 memcpy(target
->type
, target_types
[x
], sizeof(struct target_type
));
4448 /* will be set by "-endian" */
4449 target
->endianness
= TARGET_ENDIAN_UNKNOWN
;
4451 target
->working_area
= 0x0;
4452 target
->working_area_size
= 0x0;
4453 target
->working_areas
= NULL
;
4454 target
->backup_working_area
= 0;
4456 target
->state
= TARGET_UNKNOWN
;
4457 target
->debug_reason
= DBG_REASON_UNDEFINED
;
4458 target
->reg_cache
= NULL
;
4459 target
->breakpoints
= NULL
;
4460 target
->watchpoints
= NULL
;
4461 target
->next
= NULL
;
4462 target
->arch_info
= NULL
;
4464 target
->display
= 1;
4466 target
->halt_issued
= false;
4468 /* initialize trace information */
4469 target
->trace_info
= malloc(sizeof(struct trace
));
4470 target
->trace_info
->num_trace_points
= 0;
4471 target
->trace_info
->trace_points_size
= 0;
4472 target
->trace_info
->trace_points
= NULL
;
4473 target
->trace_info
->trace_history_size
= 0;
4474 target
->trace_info
->trace_history
= NULL
;
4475 target
->trace_info
->trace_history_pos
= 0;
4476 target
->trace_info
->trace_history_overflowed
= 0;
4478 target
->dbgmsg
= NULL
;
4479 target
->dbg_msg_enabled
= 0;
4481 target
->endianness
= TARGET_ENDIAN_UNKNOWN
;
4483 /* Do the rest as "configure" options */
4484 goi
->isconfigure
= 1;
4485 e
= target_configure(goi
, target
);
4487 if (target
->tap
== NULL
)
4489 Jim_SetResultString(interp
, "-chain-position required when creating target", -1);
4499 if (target
->endianness
== TARGET_ENDIAN_UNKNOWN
) {
4500 /* default endian to little if not specified */
4501 target
->endianness
= TARGET_LITTLE_ENDIAN
;
4504 /* incase variant is not set */
4505 if (!target
->variant
)
4506 target
->variant
= strdup("");
4508 cp
= Jim_GetString(new_cmd
, NULL
);
4509 target
->cmd_name
= strdup(cp
);
4511 /* create the target specific commands */
4512 if (target
->type
->commands
) {
4513 e
= register_commands(cmd_ctx
, NULL
, target
->type
->commands
);
4515 LOG_ERROR("unable to register '%s' commands", cp
);
4517 if (target
->type
->target_create
) {
4518 (*(target
->type
->target_create
))(target
, goi
->interp
);
4521 /* append to end of list */
4523 struct target
**tpp
;
4524 tpp
= &(all_targets
);
4526 tpp
= &((*tpp
)->next
);
4531 /* now - create the new target name command */
4532 const const struct command_registration target_subcommands
[] = {
4534 .chain
= target_instance_command_handlers
,
4537 .chain
= target
->type
->commands
,
4539 COMMAND_REGISTRATION_DONE
4541 const const struct command_registration target_commands
[] = {
4544 .mode
= COMMAND_ANY
,
4545 .help
= "target command group",
4546 .chain
= target_subcommands
,
4548 COMMAND_REGISTRATION_DONE
4550 e
= register_commands(cmd_ctx
, NULL
, target_commands
);
4554 struct command
*c
= command_find_in_context(cmd_ctx
, cp
);
4556 command_set_handler_data(c
, target
);
4558 return (ERROR_OK
== e
) ? JIM_OK
: JIM_ERR
;
4561 static int jim_target_current(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4565 Jim_WrongNumArgs(interp
, 1, argv
, "Too many parameters");
4568 struct command_context
*cmd_ctx
= Jim_GetAssocData(interp
, "context");
4569 Jim_SetResultString(interp
, get_current_target(cmd_ctx
)->cmd_name
, -1);
4573 static int jim_target_types(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4577 Jim_WrongNumArgs(interp
, 1, argv
, "Too many parameters");
4580 Jim_SetResult(interp
, Jim_NewListObj(interp
, NULL
, 0));
4581 for (unsigned x
= 0; NULL
!= target_types
[x
]; x
++)
4583 Jim_ListAppendElement(interp
, Jim_GetResult(interp
),
4584 Jim_NewStringObj(interp
, target_types
[x
]->name
, -1));
4589 static int jim_target_names(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4593 Jim_WrongNumArgs(interp
, 1, argv
, "Too many parameters");
4596 Jim_SetResult(interp
, Jim_NewListObj(interp
, NULL
, 0));
4597 struct target
*target
= all_targets
;
4600 Jim_ListAppendElement(interp
, Jim_GetResult(interp
),
4601 Jim_NewStringObj(interp
, target_name(target
), -1));
4602 target
= target
->next
;
4607 static int jim_target_create(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4610 Jim_GetOpt_Setup(&goi
, interp
, argc
- 1, argv
+ 1);
4613 Jim_WrongNumArgs(goi
.interp
, goi
.argc
, goi
.argv
,
4614 "<name> <target_type> [<target_options> ...]");
4617 return target_create(&goi
);
4620 static int jim_target_number(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4623 Jim_GetOpt_Setup(&goi
, interp
, argc
- 1, argv
+ 1);
4625 /* It's OK to remove this mechanism sometime after August 2010 or so */
4626 LOG_WARNING("don't use numbers as target identifiers; use names");
4629 Jim_SetResult_sprintf(goi
.interp
, "usage: target number <number>");
4633 int e
= Jim_GetOpt_Wide(&goi
, &w
);
4637 struct target
*target
;
4638 for (target
= all_targets
; NULL
!= target
; target
= target
->next
)
4640 if (target
->target_number
!= w
)
4643 Jim_SetResultString(goi
.interp
, target_name(target
), -1);
4646 Jim_SetResult_sprintf(goi
.interp
,
4647 "Target: number %d does not exist", (int)(w
));
4651 static int jim_target_count(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4655 Jim_WrongNumArgs(interp
, 1, argv
, "<no parameters>");
4659 struct target
*target
= all_targets
;
4660 while (NULL
!= target
)
4662 target
= target
->next
;
4665 Jim_SetResult(interp
, Jim_NewIntObj(interp
, count
));
4669 static const struct command_registration target_subcommand_handlers
[] = {
4672 .mode
= COMMAND_ANY
,
4673 .jim_handler
= &jim_target_create
,
4674 .usage
= "<name> <type> ...",
4675 .help
= "Returns the currently selected target",
4679 .mode
= COMMAND_ANY
,
4680 .jim_handler
= &jim_target_current
,
4681 .help
= "Returns the currently selected target",
4685 .mode
= COMMAND_ANY
,
4686 .jim_handler
= &jim_target_types
,
4687 .help
= "Returns the available target types as a list of strings",
4691 .mode
= COMMAND_ANY
,
4692 .jim_handler
= &jim_target_names
,
4693 .help
= "Returns the names of all targets as a list of strings",
4697 .mode
= COMMAND_ANY
,
4698 .jim_handler
= &jim_target_number
,
4699 .usage
= "<number>",
4700 .help
= "Returns the name of target <n>",
4704 .mode
= COMMAND_ANY
,
4705 .jim_handler
= &jim_target_count
,
4706 .help
= "Returns the number of targets as an integer",
4708 COMMAND_REGISTRATION_DONE
4720 static int fastload_num
;
4721 static struct FastLoad
*fastload
;
4723 static void free_fastload(void)
4725 if (fastload
!= NULL
)
4728 for (i
= 0; i
< fastload_num
; i
++)
4730 if (fastload
[i
].data
)
4731 free(fastload
[i
].data
);
4741 COMMAND_HANDLER(handle_fast_load_image_command
)
4745 uint32_t image_size
;
4746 uint32_t min_address
= 0;
4747 uint32_t max_address
= 0xffffffff;
4752 int retval
= CALL_COMMAND_HANDLER(parse_load_image_command_CMD_ARGV
,
4753 &image
, &min_address
, &max_address
);
4754 if (ERROR_OK
!= retval
)
4757 struct duration bench
;
4758 duration_start(&bench
);
4760 if (image_open(&image
, CMD_ARGV
[0], (CMD_ARGC
>= 3) ? CMD_ARGV
[2] : NULL
) != ERROR_OK
)
4767 fastload_num
= image
.num_sections
;
4768 fastload
= (struct FastLoad
*)malloc(sizeof(struct FastLoad
)*image
.num_sections
);
4769 if (fastload
== NULL
)
4771 image_close(&image
);
4774 memset(fastload
, 0, sizeof(struct FastLoad
)*image
.num_sections
);
4775 for (i
= 0; i
< image
.num_sections
; i
++)
4777 buffer
= malloc(image
.sections
[i
].size
);
4780 command_print(CMD_CTX
, "error allocating buffer for section (%d bytes)",
4781 (int)(image
.sections
[i
].size
));
4785 if ((retval
= image_read_section(&image
, i
, 0x0, image
.sections
[i
].size
, buffer
, &buf_cnt
)) != ERROR_OK
)
4791 uint32_t offset
= 0;
4792 uint32_t length
= buf_cnt
;
4795 /* DANGER!!! beware of unsigned comparision here!!! */
4797 if ((image
.sections
[i
].base_address
+ buf_cnt
>= min_address
)&&
4798 (image
.sections
[i
].base_address
< max_address
))
4800 if (image
.sections
[i
].base_address
< min_address
)
4802 /* clip addresses below */
4803 offset
+= min_address
-image
.sections
[i
].base_address
;
4807 if (image
.sections
[i
].base_address
+ buf_cnt
> max_address
)
4809 length
-= (image
.sections
[i
].base_address
+ buf_cnt
)-max_address
;
4812 fastload
[i
].address
= image
.sections
[i
].base_address
+ offset
;
4813 fastload
[i
].data
= malloc(length
);
4814 if (fastload
[i
].data
== NULL
)
4819 memcpy(fastload
[i
].data
, buffer
+ offset
, length
);
4820 fastload
[i
].length
= length
;
4822 image_size
+= length
;
4823 command_print(CMD_CTX
, "%u bytes written at address 0x%8.8x",
4824 (unsigned int)length
,
4825 ((unsigned int)(image
.sections
[i
].base_address
+ offset
)));
4831 if ((ERROR_OK
== retval
) && (duration_measure(&bench
) == ERROR_OK
))
4833 command_print(CMD_CTX
, "Loaded %" PRIu32
" bytes "
4834 "in %fs (%0.3f kb/s)", image_size
,
4835 duration_elapsed(&bench
), duration_kbps(&bench
, image_size
));
4837 command_print(CMD_CTX
,
4838 "WARNING: image has not been loaded to target!"
4839 "You can issue a 'fast_load' to finish loading.");
4842 image_close(&image
);
4844 if (retval
!= ERROR_OK
)
4852 COMMAND_HANDLER(handle_fast_load_command
)
4855 return ERROR_COMMAND_SYNTAX_ERROR
;
4856 if (fastload
== NULL
)
4858 LOG_ERROR("No image in memory");
4862 int ms
= timeval_ms();
4864 int retval
= ERROR_OK
;
4865 for (i
= 0; i
< fastload_num
;i
++)
4867 struct target
*target
= get_current_target(CMD_CTX
);
4868 command_print(CMD_CTX
, "Write to 0x%08x, length 0x%08x",
4869 (unsigned int)(fastload
[i
].address
),
4870 (unsigned int)(fastload
[i
].length
));
4871 if (retval
== ERROR_OK
)
4873 retval
= target_write_buffer(target
, fastload
[i
].address
, fastload
[i
].length
, fastload
[i
].data
);
4875 size
+= fastload
[i
].length
;
4877 int after
= timeval_ms();
4878 command_print(CMD_CTX
, "Loaded image %f kBytes/s", (float)(size
/1024.0)/((float)(after
-ms
)/1000.0));
4882 static int jim_mcrmrc(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4884 struct command_context
*context
;
4885 struct target
*target
;
4888 context
= Jim_GetAssocData(interp
, "context");
4889 if (context
== NULL
) {
4890 LOG_ERROR("array2mem: no command context");
4893 target
= get_current_target(context
);
4894 if (target
== NULL
) {
4895 LOG_ERROR("array2mem: no current target");
4899 if ((argc
< 6) || (argc
> 7))
4913 e
= Jim_GetLong(interp
, argv
[1], &l
);
4919 e
= Jim_GetLong(interp
, argv
[2], &l
);
4925 e
= Jim_GetLong(interp
, argv
[3], &l
);
4931 e
= Jim_GetLong(interp
, argv
[4], &l
);
4937 e
= Jim_GetLong(interp
, argv
[5], &l
);
4947 e
= Jim_GetLong(interp
, argv
[6], &l
);
4953 retval
= target_mcr(target
, cpnum
, op1
, op2
, CRn
, CRm
, value
);
4954 if (retval
!= ERROR_OK
)
4958 retval
= target_mrc(target
, cpnum
, op1
, op2
, CRn
, CRm
, &value
);
4959 if (retval
!= ERROR_OK
)
4962 Jim_SetResult(interp
, Jim_NewIntObj(interp
, value
));
4968 static const struct command_registration target_command_handlers
[] = {
4971 .handler
= &handle_targets_command
,
4972 .mode
= COMMAND_ANY
,
4973 .help
= "change current command line target (one parameter) "
4974 "or list targets (no parameters)",
4975 .usage
= "[<new_current_target>]",
4979 .mode
= COMMAND_CONFIG
,
4980 .help
= "configure target",
4982 .chain
= target_subcommand_handlers
,
4984 COMMAND_REGISTRATION_DONE
4987 int target_register_commands(struct command_context
*cmd_ctx
)
4989 return register_commands(cmd_ctx
, NULL
, target_command_handlers
);
4992 static const struct command_registration target_exec_command_handlers
[] = {
4994 .name
= "fast_load_image",
4995 .handler
= &handle_fast_load_image_command
,
4996 .mode
= COMMAND_ANY
,
4997 .help
= "Load image into memory, mainly for profiling purposes",
4998 .usage
= "<file> <address> ['bin'|'ihex'|'elf'|'s19'] "
4999 "[min_address] [max_length]",
5002 .name
= "fast_load",
5003 .handler
= &handle_fast_load_command
,
5004 .mode
= COMMAND_ANY
,
5005 .help
= "loads active fast load image to current target "
5006 "- mainly for profiling purposes",
5010 .handler
= &handle_profile_command
,
5011 .mode
= COMMAND_EXEC
,
5012 .help
= "profiling samples the CPU PC",
5014 /** @todo don't register virt2phys() unless target supports it */
5016 .name
= "virt2phys",
5017 .handler
= &handle_virt2phys_command
,
5018 .mode
= COMMAND_ANY
,
5019 .help
= "translate a virtual address into a physical address",
5024 .handler
= &handle_reg_command
,
5025 .mode
= COMMAND_EXEC
,
5026 .help
= "display or set a register",
5031 .handler
= &handle_poll_command
,
5032 .mode
= COMMAND_EXEC
,
5033 .help
= "poll target state",
5036 .name
= "wait_halt",
5037 .handler
= &handle_wait_halt_command
,
5038 .mode
= COMMAND_EXEC
,
5039 .help
= "wait for target halt",
5040 .usage
= "[time (s)]",
5044 .handler
= &handle_halt_command
,
5045 .mode
= COMMAND_EXEC
,
5046 .help
= "halt target",
5050 .handler
= &handle_resume_command
,
5051 .mode
= COMMAND_EXEC
,
5052 .help
= "resume target",
5053 .usage
= "[<address>]",
5057 .handler
= &handle_reset_command
,
5058 .mode
= COMMAND_EXEC
,
5059 .usage
= "[run|halt|init]",
5060 .help
= "Reset all targets into the specified mode."
5061 "Default reset mode is run, if not given.",
5064 .name
= "soft_reset_halt",
5065 .handler
= &handle_soft_reset_halt_command
,
5066 .mode
= COMMAND_EXEC
,
5067 .help
= "halt the target and do a soft reset",
5072 .handler
= &handle_step_command
,
5073 .mode
= COMMAND_EXEC
,
5074 .help
= "step one instruction from current PC or [addr]",
5075 .usage
= "[<address>]",
5080 .handler
= &handle_md_command
,
5081 .mode
= COMMAND_EXEC
,
5082 .help
= "display memory words",
5083 .usage
= "[phys] <addr> [count]",
5087 .handler
= &handle_md_command
,
5088 .mode
= COMMAND_EXEC
,
5089 .help
= "display memory half-words",
5090 .usage
= "[phys] <addr> [count]",
5094 .handler
= &handle_md_command
,
5095 .mode
= COMMAND_EXEC
,
5096 .help
= "display memory bytes",
5097 .usage
= "[phys] <addr> [count]",
5102 .handler
= &handle_mw_command
,
5103 .mode
= COMMAND_EXEC
,
5104 .help
= "write memory word",
5105 .usage
= "[phys] <addr> <value> [count]",
5109 .handler
= &handle_mw_command
,
5110 .mode
= COMMAND_EXEC
,
5111 .help
= "write memory half-word",
5112 .usage
= "[phys] <addr> <value> [count]",
5116 .handler
= &handle_mw_command
,
5117 .mode
= COMMAND_EXEC
,
5118 .help
= "write memory byte",
5119 .usage
= "[phys] <addr> <value> [count]",
5124 .handler
= &handle_bp_command
,
5125 .mode
= COMMAND_EXEC
,
5126 .help
= "list or set breakpoint",
5127 .usage
= "[<address> <length> [hw]]",
5131 .handler
= &handle_rbp_command
,
5132 .mode
= COMMAND_EXEC
,
5133 .help
= "remove breakpoint",
5134 .usage
= "<address>",
5139 .handler
= &handle_wp_command
,
5140 .mode
= COMMAND_EXEC
,
5141 .help
= "list or set watchpoint",
5142 .usage
= "[<address> <length> <r/w/a> [value] [mask]]",
5146 .handler
= &handle_rwp_command
,
5147 .mode
= COMMAND_EXEC
,
5148 .help
= "remove watchpoint",
5149 .usage
= "<address>",
5153 .name
= "load_image",
5154 .handler
= &handle_load_image_command
,
5155 .mode
= COMMAND_EXEC
,
5156 .usage
= "<file> <address> ['bin'|'ihex'|'elf'|'s19'] "
5157 "[min_address] [max_length]",
5160 .name
= "dump_image",
5161 .handler
= &handle_dump_image_command
,
5162 .mode
= COMMAND_EXEC
,
5163 .usage
= "<file> <address> <size>",
5166 .name
= "verify_image",
5167 .handler
= &handle_verify_image_command
,
5168 .mode
= COMMAND_EXEC
,
5169 .usage
= "<file> [offset] [type]",
5172 .name
= "test_image",
5173 .handler
= &handle_test_image_command
,
5174 .mode
= COMMAND_EXEC
,
5175 .usage
= "<file> [offset] [type]",
5178 .name
= "ocd_mem2array",
5179 .mode
= COMMAND_EXEC
,
5180 .jim_handler
= &jim_mem2array
,
5181 .help
= "read memory and return as a TCL array "
5182 "for script processing",
5183 .usage
= "<arrayname> <width=32|16|8> <address> <count>",
5186 .name
= "ocd_array2mem",
5187 .mode
= COMMAND_EXEC
,
5188 .jim_handler
= &jim_array2mem
,
5189 .help
= "convert a TCL array to memory locations "
5190 "and write the values",
5191 .usage
= "<arrayname> <width=32|16|8> <address> <count>",
5193 COMMAND_REGISTRATION_DONE
5195 int target_register_user_commands(struct command_context
*cmd_ctx
)
5197 int retval
= ERROR_OK
;
5198 if ((retval
= target_request_register_commands(cmd_ctx
)) != ERROR_OK
)
5201 if ((retval
= trace_register_commands(cmd_ctx
)) != ERROR_OK
)
5205 return register_commands(cmd_ctx
, NULL
, target_exec_command_handlers
);