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 "time_support.h"
46 static int handle_targets_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
);
48 static int handle_reg_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
);
49 static int handle_poll_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
);
50 static int handle_halt_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
);
51 static int handle_wait_halt_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
);
52 static int handle_reset_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
);
53 static int handle_soft_reset_halt_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
);
54 static int handle_resume_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
);
55 static int handle_step_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
);
56 static int handle_md_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
);
57 static int handle_mw_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
);
58 static int handle_load_image_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
);
59 static int handle_dump_image_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
);
60 static int handle_verify_image_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
);
61 static int handle_test_image_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
);
62 static int handle_bp_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
);
63 static int handle_rbp_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
);
64 static int handle_wp_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
);
65 static int handle_rwp_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
);
66 static int handle_virt2phys_command(command_context_t
*cmd_ctx
, char *cmd
, char **args
, int argc
);
67 static int handle_profile_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
);
68 static int handle_fast_load_image_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
);
69 static int handle_fast_load_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
);
71 static int jim_array2mem(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
);
72 static int jim_mcrmrc(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
);
73 static int jim_mem2array(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
);
74 static int jim_target(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
);
76 static int target_array2mem(Jim_Interp
*interp
, target_t
*target
, int argc
, Jim_Obj
*const *argv
);
77 static int target_mem2array(Jim_Interp
*interp
, target_t
*target
, int argc
, Jim_Obj
*const *argv
);
80 extern target_type_t arm7tdmi_target
;
81 extern target_type_t arm720t_target
;
82 extern target_type_t arm9tdmi_target
;
83 extern target_type_t arm920t_target
;
84 extern target_type_t arm966e_target
;
85 extern target_type_t arm926ejs_target
;
86 extern target_type_t fa526_target
;
87 extern target_type_t feroceon_target
;
88 extern target_type_t dragonite_target
;
89 extern target_type_t xscale_target
;
90 extern target_type_t cortexm3_target
;
91 extern target_type_t cortexa8_target
;
92 extern target_type_t arm11_target
;
93 extern target_type_t mips_m4k_target
;
94 extern target_type_t avr_target
;
96 target_type_t
*target_types
[] =
116 target_t
*all_targets
= NULL
;
117 target_event_callback_t
*target_event_callbacks
= NULL
;
118 target_timer_callback_t
*target_timer_callbacks
= NULL
;
120 const Jim_Nvp nvp_assert
[] = {
121 { .name
= "assert", NVP_ASSERT
},
122 { .name
= "deassert", NVP_DEASSERT
},
123 { .name
= "T", NVP_ASSERT
},
124 { .name
= "F", NVP_DEASSERT
},
125 { .name
= "t", NVP_ASSERT
},
126 { .name
= "f", NVP_DEASSERT
},
127 { .name
= NULL
, .value
= -1 }
130 const Jim_Nvp nvp_error_target
[] = {
131 { .value
= ERROR_TARGET_INVALID
, .name
= "err-invalid" },
132 { .value
= ERROR_TARGET_INIT_FAILED
, .name
= "err-init-failed" },
133 { .value
= ERROR_TARGET_TIMEOUT
, .name
= "err-timeout" },
134 { .value
= ERROR_TARGET_NOT_HALTED
, .name
= "err-not-halted" },
135 { .value
= ERROR_TARGET_FAILURE
, .name
= "err-failure" },
136 { .value
= ERROR_TARGET_UNALIGNED_ACCESS
, .name
= "err-unaligned-access" },
137 { .value
= ERROR_TARGET_DATA_ABORT
, .name
= "err-data-abort" },
138 { .value
= ERROR_TARGET_RESOURCE_NOT_AVAILABLE
, .name
= "err-resource-not-available" },
139 { .value
= ERROR_TARGET_TRANSLATION_FAULT
, .name
= "err-translation-fault" },
140 { .value
= ERROR_TARGET_NOT_RUNNING
, .name
= "err-not-running" },
141 { .value
= ERROR_TARGET_NOT_EXAMINED
, .name
= "err-not-examined" },
142 { .value
= -1, .name
= NULL
}
145 const char *target_strerror_safe(int err
)
149 n
= Jim_Nvp_value2name_simple(nvp_error_target
, err
);
150 if (n
->name
== NULL
) {
157 static const Jim_Nvp nvp_target_event
[] = {
158 { .value
= TARGET_EVENT_OLD_gdb_program_config
, .name
= "old-gdb_program_config" },
159 { .value
= TARGET_EVENT_OLD_pre_resume
, .name
= "old-pre_resume" },
161 { .value
= TARGET_EVENT_GDB_HALT
, .name
= "gdb-halt" },
162 { .value
= TARGET_EVENT_HALTED
, .name
= "halted" },
163 { .value
= TARGET_EVENT_RESUMED
, .name
= "resumed" },
164 { .value
= TARGET_EVENT_RESUME_START
, .name
= "resume-start" },
165 { .value
= TARGET_EVENT_RESUME_END
, .name
= "resume-end" },
167 { .name
= "gdb-start", .value
= TARGET_EVENT_GDB_START
},
168 { .name
= "gdb-end", .value
= TARGET_EVENT_GDB_END
},
170 /* historical name */
172 { .value
= TARGET_EVENT_RESET_START
, .name
= "reset-start" },
174 { .value
= TARGET_EVENT_RESET_ASSERT_PRE
, .name
= "reset-assert-pre" },
175 { .value
= TARGET_EVENT_RESET_ASSERT_POST
, .name
= "reset-assert-post" },
176 { .value
= TARGET_EVENT_RESET_DEASSERT_PRE
, .name
= "reset-deassert-pre" },
177 { .value
= TARGET_EVENT_RESET_DEASSERT_POST
, .name
= "reset-deassert-post" },
178 { .value
= TARGET_EVENT_RESET_HALT_PRE
, .name
= "reset-halt-pre" },
179 { .value
= TARGET_EVENT_RESET_HALT_POST
, .name
= "reset-halt-post" },
180 { .value
= TARGET_EVENT_RESET_WAIT_PRE
, .name
= "reset-wait-pre" },
181 { .value
= TARGET_EVENT_RESET_WAIT_POST
, .name
= "reset-wait-post" },
182 { .value
= TARGET_EVENT_RESET_INIT
, .name
= "reset-init" },
183 { .value
= TARGET_EVENT_RESET_END
, .name
= "reset-end" },
185 { .value
= TARGET_EVENT_EXAMINE_START
, .name
= "examine-start" },
186 { .value
= TARGET_EVENT_EXAMINE_END
, .name
= "examine-end" },
188 { .value
= TARGET_EVENT_DEBUG_HALTED
, .name
= "debug-halted" },
189 { .value
= TARGET_EVENT_DEBUG_RESUMED
, .name
= "debug-resumed" },
191 { .value
= TARGET_EVENT_GDB_ATTACH
, .name
= "gdb-attach" },
192 { .value
= TARGET_EVENT_GDB_DETACH
, .name
= "gdb-detach" },
194 { .value
= TARGET_EVENT_GDB_FLASH_WRITE_START
, .name
= "gdb-flash-write-start" },
195 { .value
= TARGET_EVENT_GDB_FLASH_WRITE_END
, .name
= "gdb-flash-write-end" },
197 { .value
= TARGET_EVENT_GDB_FLASH_ERASE_START
, .name
= "gdb-flash-erase-start" },
198 { .value
= TARGET_EVENT_GDB_FLASH_ERASE_END
, .name
= "gdb-flash-erase-end" },
200 { .value
= TARGET_EVENT_RESUME_START
, .name
= "resume-start" },
201 { .value
= TARGET_EVENT_RESUMED
, .name
= "resume-ok" },
202 { .value
= TARGET_EVENT_RESUME_END
, .name
= "resume-end" },
204 { .name
= NULL
, .value
= -1 }
207 const Jim_Nvp nvp_target_state
[] = {
208 { .name
= "unknown", .value
= TARGET_UNKNOWN
},
209 { .name
= "running", .value
= TARGET_RUNNING
},
210 { .name
= "halted", .value
= TARGET_HALTED
},
211 { .name
= "reset", .value
= TARGET_RESET
},
212 { .name
= "debug-running", .value
= TARGET_DEBUG_RUNNING
},
213 { .name
= NULL
, .value
= -1 },
216 const Jim_Nvp nvp_target_debug_reason
[] = {
217 { .name
= "debug-request" , .value
= DBG_REASON_DBGRQ
},
218 { .name
= "breakpoint" , .value
= DBG_REASON_BREAKPOINT
},
219 { .name
= "watchpoint" , .value
= DBG_REASON_WATCHPOINT
},
220 { .name
= "watchpoint-and-breakpoint", .value
= DBG_REASON_WPTANDBKPT
},
221 { .name
= "single-step" , .value
= DBG_REASON_SINGLESTEP
},
222 { .name
= "target-not-halted" , .value
= DBG_REASON_NOTHALTED
},
223 { .name
= "undefined" , .value
= DBG_REASON_UNDEFINED
},
224 { .name
= NULL
, .value
= -1 },
227 const Jim_Nvp nvp_target_endian
[] = {
228 { .name
= "big", .value
= TARGET_BIG_ENDIAN
},
229 { .name
= "little", .value
= TARGET_LITTLE_ENDIAN
},
230 { .name
= "be", .value
= TARGET_BIG_ENDIAN
},
231 { .name
= "le", .value
= TARGET_LITTLE_ENDIAN
},
232 { .name
= NULL
, .value
= -1 },
235 const Jim_Nvp nvp_reset_modes
[] = {
236 { .name
= "unknown", .value
= RESET_UNKNOWN
},
237 { .name
= "run" , .value
= RESET_RUN
},
238 { .name
= "halt" , .value
= RESET_HALT
},
239 { .name
= "init" , .value
= RESET_INIT
},
240 { .name
= NULL
, .value
= -1 },
244 target_state_name( target_t
*t
)
247 cp
= Jim_Nvp_value2name_simple(nvp_target_state
, t
->state
)->name
;
249 LOG_ERROR("Invalid target state: %d", (int)(t
->state
));
250 cp
= "(*BUG*unknown*BUG*)";
255 /* determine the number of the new target */
256 static int new_target_number(void)
261 /* number is 0 based */
265 if (x
< t
->target_number
) {
266 x
= t
->target_number
;
273 /* read a uint32_t from a buffer in target memory endianness */
274 uint32_t target_buffer_get_u32(target_t
*target
, const uint8_t *buffer
)
276 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
277 return le_to_h_u32(buffer
);
279 return be_to_h_u32(buffer
);
282 /* read a uint16_t from a buffer in target memory endianness */
283 uint16_t target_buffer_get_u16(target_t
*target
, const uint8_t *buffer
)
285 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
286 return le_to_h_u16(buffer
);
288 return be_to_h_u16(buffer
);
291 /* read a uint8_t from a buffer in target memory endianness */
292 uint8_t target_buffer_get_u8(target_t
*target
, const uint8_t *buffer
)
294 return *buffer
& 0x0ff;
297 /* write a uint32_t to a buffer in target memory endianness */
298 void target_buffer_set_u32(target_t
*target
, uint8_t *buffer
, uint32_t value
)
300 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
301 h_u32_to_le(buffer
, value
);
303 h_u32_to_be(buffer
, value
);
306 /* write a uint16_t to a buffer in target memory endianness */
307 void target_buffer_set_u16(target_t
*target
, uint8_t *buffer
, uint16_t value
)
309 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
310 h_u16_to_le(buffer
, value
);
312 h_u16_to_be(buffer
, value
);
315 /* write a uint8_t to a buffer in target memory endianness */
316 void target_buffer_set_u8(target_t
*target
, uint8_t *buffer
, uint8_t value
)
321 /* return a pointer to a configured target; id is name or number */
322 target_t
*get_target(const char *id
)
326 /* try as tcltarget name */
327 for (target
= all_targets
; target
; target
= target
->next
) {
328 if (target
->cmd_name
== NULL
)
330 if (strcmp(id
, target
->cmd_name
) == 0)
334 /* It's OK to remove this fallback sometime after August 2010 or so */
336 /* no match, try as number */
338 if (parse_uint(id
, &num
) != ERROR_OK
)
341 for (target
= all_targets
; target
; target
= target
->next
) {
342 if (target
->target_number
== (int)num
) {
343 LOG_WARNING("use '%s' as target identifier, not '%u'",
344 target
->cmd_name
, num
);
352 /* returns a pointer to the n-th configured target */
353 static target_t
*get_target_by_num(int num
)
355 target_t
*target
= all_targets
;
358 if (target
->target_number
== num
) {
361 target
= target
->next
;
367 target_t
* get_current_target(command_context_t
*cmd_ctx
)
369 target_t
*target
= get_target_by_num(cmd_ctx
->current_target
);
373 LOG_ERROR("BUG: current_target out of bounds");
380 int target_poll(struct target_s
*target
)
384 /* We can't poll until after examine */
385 if (!target_was_examined(target
))
387 /* Fail silently lest we pollute the log */
391 retval
= target
->type
->poll(target
);
392 if (retval
!= ERROR_OK
)
395 if (target
->halt_issued
)
397 if (target
->state
== TARGET_HALTED
)
399 target
->halt_issued
= false;
402 long long t
= timeval_ms() - target
->halt_issued_time
;
405 target
->halt_issued
= false;
406 LOG_INFO("Halt timed out, wake up GDB.");
407 target_call_event_callbacks(target
, TARGET_EVENT_GDB_HALT
);
415 int target_halt(struct target_s
*target
)
418 /* We can't poll until after examine */
419 if (!target_was_examined(target
))
421 LOG_ERROR("Target not examined yet");
425 retval
= target
->type
->halt(target
);
426 if (retval
!= ERROR_OK
)
429 target
->halt_issued
= true;
430 target
->halt_issued_time
= timeval_ms();
435 int target_resume(struct target_s
*target
, int current
, uint32_t address
, int handle_breakpoints
, int debug_execution
)
439 /* We can't poll until after examine */
440 if (!target_was_examined(target
))
442 LOG_ERROR("Target not examined yet");
446 /* note that resume *must* be asynchronous. The CPU can halt before we poll. The CPU can
447 * even halt at the current PC as a result of a software breakpoint being inserted by (a bug?)
450 if ((retval
= target
->type
->resume(target
, current
, address
, handle_breakpoints
, debug_execution
)) != ERROR_OK
)
456 int target_process_reset(struct command_context_s
*cmd_ctx
, enum target_reset_mode reset_mode
)
461 n
= Jim_Nvp_value2name_simple(nvp_reset_modes
, reset_mode
);
462 if (n
->name
== NULL
) {
463 LOG_ERROR("invalid reset mode");
467 /* disable polling during reset to make reset event scripts
468 * more predictable, i.e. dr/irscan & pathmove in events will
469 * not have JTAG operations injected into the middle of a sequence.
471 bool save_poll
= jtag_poll_get_enabled();
473 jtag_poll_set_enabled(false);
475 sprintf(buf
, "ocd_process_reset %s", n
->name
);
476 retval
= Jim_Eval(interp
, buf
);
478 jtag_poll_set_enabled(save_poll
);
480 if (retval
!= JIM_OK
) {
481 Jim_PrintErrorMessage(interp
);
485 /* We want any events to be processed before the prompt */
486 retval
= target_call_timer_callbacks_now();
491 static int default_virt2phys(struct target_s
*target
, uint32_t virtual, uint32_t *physical
)
497 static int default_mmu(struct target_s
*target
, int *enabled
)
503 static int default_examine(struct target_s
*target
)
505 target_set_examined(target
);
509 int target_examine_one(struct target_s
*target
)
511 return target
->type
->examine(target
);
514 static int jtag_enable_callback(enum jtag_event event
, void *priv
)
516 target_t
*target
= priv
;
518 if (event
!= JTAG_TAP_EVENT_ENABLE
|| !target
->tap
->enabled
)
521 jtag_unregister_event_callback(jtag_enable_callback
, target
);
522 return target_examine_one(target
);
526 /* Targets that correctly implement init + examine, i.e.
527 * no communication with target during init:
531 int target_examine(void)
533 int retval
= ERROR_OK
;
536 for (target
= all_targets
; target
; target
= target
->next
)
538 /* defer examination, but don't skip it */
539 if (!target
->tap
->enabled
) {
540 jtag_register_event_callback(jtag_enable_callback
,
544 if ((retval
= target_examine_one(target
)) != ERROR_OK
)
549 const char *target_get_name(struct target_s
*target
)
551 return target
->type
->name
;
554 static int target_write_memory_imp(struct target_s
*target
, uint32_t address
, uint32_t size
, uint32_t count
, uint8_t *buffer
)
556 if (!target_was_examined(target
))
558 LOG_ERROR("Target not examined yet");
561 return target
->type
->write_memory_imp(target
, address
, size
, count
, buffer
);
564 static int target_read_memory_imp(struct target_s
*target
, uint32_t address
, uint32_t size
, uint32_t count
, uint8_t *buffer
)
566 if (!target_was_examined(target
))
568 LOG_ERROR("Target not examined yet");
571 return target
->type
->read_memory_imp(target
, address
, size
, count
, buffer
);
574 static int target_soft_reset_halt_imp(struct target_s
*target
)
576 if (!target_was_examined(target
))
578 LOG_ERROR("Target not examined yet");
581 if (!target
->type
->soft_reset_halt_imp
) {
582 LOG_ERROR("Target %s does not support soft_reset_halt",
586 return target
->type
->soft_reset_halt_imp(target
);
589 static int target_run_algorithm_imp(struct target_s
*target
, int num_mem_params
, mem_param_t
*mem_params
, int num_reg_params
, reg_param_t
*reg_param
, uint32_t entry_point
, uint32_t exit_point
, int timeout_ms
, void *arch_info
)
591 if (!target_was_examined(target
))
593 LOG_ERROR("Target not examined yet");
596 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
);
599 int target_read_memory(struct target_s
*target
,
600 uint32_t address
, uint32_t size
, uint32_t count
, uint8_t *buffer
)
602 return target
->type
->read_memory(target
, address
, size
, count
, buffer
);
605 int target_read_phys_memory(struct target_s
*target
,
606 uint32_t address
, uint32_t size
, uint32_t count
, uint8_t *buffer
)
608 return target
->type
->read_phys_memory(target
, address
, size
, count
, buffer
);
611 int target_write_memory(struct target_s
*target
,
612 uint32_t address
, uint32_t size
, uint32_t count
, uint8_t *buffer
)
614 return target
->type
->write_memory(target
, address
, size
, count
, buffer
);
617 int target_write_phys_memory(struct target_s
*target
,
618 uint32_t address
, uint32_t size
, uint32_t count
, uint8_t *buffer
)
620 return target
->type
->write_phys_memory(target
, address
, size
, count
, buffer
);
623 int target_bulk_write_memory(struct target_s
*target
,
624 uint32_t address
, uint32_t count
, uint8_t *buffer
)
626 return target
->type
->bulk_write_memory(target
, address
, count
, buffer
);
629 int target_add_breakpoint(struct target_s
*target
,
630 struct breakpoint_s
*breakpoint
)
632 return target
->type
->add_breakpoint(target
, breakpoint
);
634 int target_remove_breakpoint(struct target_s
*target
,
635 struct breakpoint_s
*breakpoint
)
637 return target
->type
->remove_breakpoint(target
, breakpoint
);
640 int target_add_watchpoint(struct target_s
*target
,
641 struct watchpoint_s
*watchpoint
)
643 return target
->type
->add_watchpoint(target
, watchpoint
);
645 int target_remove_watchpoint(struct target_s
*target
,
646 struct watchpoint_s
*watchpoint
)
648 return target
->type
->remove_watchpoint(target
, watchpoint
);
651 int target_get_gdb_reg_list(struct target_s
*target
,
652 struct reg_s
**reg_list
[], int *reg_list_size
)
654 return target
->type
->get_gdb_reg_list(target
, reg_list
, reg_list_size
);
656 int target_step(struct target_s
*target
,
657 int current
, uint32_t address
, int handle_breakpoints
)
659 return target
->type
->step(target
, current
, address
, handle_breakpoints
);
663 int target_run_algorithm(struct target_s
*target
,
664 int num_mem_params
, mem_param_t
*mem_params
,
665 int num_reg_params
, reg_param_t
*reg_param
,
666 uint32_t entry_point
, uint32_t exit_point
,
667 int timeout_ms
, void *arch_info
)
669 return target
->type
->run_algorithm(target
,
670 num_mem_params
, mem_params
, num_reg_params
, reg_param
,
671 entry_point
, exit_point
, timeout_ms
, arch_info
);
674 /// @returns @c true if the target has been examined.
675 bool target_was_examined(struct target_s
*target
)
677 return target
->type
->examined
;
679 /// Sets the @c examined flag for the given target.
680 void target_set_examined(struct target_s
*target
)
682 target
->type
->examined
= true;
684 // Reset the @c examined flag for the given target.
685 void target_reset_examined(struct target_s
*target
)
687 target
->type
->examined
= false;
692 static int default_mrc(struct target_s
*target
, int cpnum
, uint32_t op1
, uint32_t op2
, uint32_t CRn
, uint32_t CRm
, uint32_t *value
)
694 LOG_ERROR("Not implemented");
698 static int default_mcr(struct target_s
*target
, int cpnum
, uint32_t op1
, uint32_t op2
, uint32_t CRn
, uint32_t CRm
, uint32_t value
)
700 LOG_ERROR("Not implemented");
704 static int arm_cp_check(struct target_s
*target
, int cpnum
, uint32_t op1
, uint32_t op2
, uint32_t CRn
, uint32_t CRm
)
707 if (!target_was_examined(target
))
709 LOG_ERROR("Target not examined yet");
713 if ((cpnum
<0) || (cpnum
> 15))
715 LOG_ERROR("Illegal co-processor %d", cpnum
);
721 LOG_ERROR("Illegal op1");
727 LOG_ERROR("Illegal op2");
733 LOG_ERROR("Illegal CRn");
739 LOG_ERROR("Illegal CRm");
746 int target_mrc(struct target_s
*target
, int cpnum
, uint32_t op1
, uint32_t op2
, uint32_t CRn
, uint32_t CRm
, uint32_t *value
)
750 retval
= arm_cp_check(target
, cpnum
, op1
, op2
, CRn
, CRm
);
751 if (retval
!= ERROR_OK
)
754 return target
->type
->mrc(target
, cpnum
, op1
, op2
, CRn
, CRm
, value
);
757 int target_mcr(struct target_s
*target
, int cpnum
, uint32_t op1
, uint32_t op2
, uint32_t CRn
, uint32_t CRm
, uint32_t value
)
761 retval
= arm_cp_check(target
, cpnum
, op1
, op2
, CRn
, CRm
);
762 if (retval
!= ERROR_OK
)
765 return target
->type
->mcr(target
, cpnum
, op1
, op2
, CRn
, CRm
, value
);
768 static int default_read_phys_memory(struct target_s
*target
, uint32_t address
, uint32_t size
, uint32_t count
, uint8_t *buffer
)
770 LOG_ERROR("Not implemented");
774 static int default_write_phys_memory(struct target_s
*target
, uint32_t address
, uint32_t size
, uint32_t count
, uint8_t *buffer
)
776 LOG_ERROR("Not implemented");
781 int target_init(struct command_context_s
*cmd_ctx
)
783 target_t
*target
= all_targets
;
788 target_reset_examined(target
);
789 if (target
->type
->examine
== NULL
)
791 target
->type
->examine
= default_examine
;
794 if ((retval
= target
->type
->init_target(cmd_ctx
, target
)) != ERROR_OK
)
796 LOG_ERROR("target '%s' init failed", target_get_name(target
));
800 /* Set up default functions if none are provided by target */
801 if (target
->type
->virt2phys
== NULL
)
803 target
->type
->virt2phys
= default_virt2phys
;
806 if (target
->type
->read_phys_memory
== NULL
)
808 target
->type
->read_phys_memory
= default_read_phys_memory
;
811 if (target
->type
->write_phys_memory
== NULL
)
813 target
->type
->write_phys_memory
= default_write_phys_memory
;
816 if (target
->type
->mcr
== NULL
)
818 target
->type
->mcr
= default_mcr
;
821 /* FIX! multiple targets will generally register global commands
822 * multiple times. Only register this one if *one* of the
823 * targets need the command. Hmm... make it a command on the
824 * Jim Tcl target object?
826 register_jim(cmd_ctx
, "mcr", jim_mcrmrc
, "write coprocessor <cpnum> <op1> <op2> <CRn> <CRm> <value>");
829 if (target
->type
->mrc
== NULL
)
831 target
->type
->mrc
= default_mrc
;
834 register_jim(cmd_ctx
, "mrc", jim_mcrmrc
, "read coprocessor <cpnum> <op1> <op2> <CRn> <CRm>");
838 /* a non-invasive way(in terms of patches) to add some code that
839 * runs before the type->write/read_memory implementation
841 target
->type
->write_memory_imp
= target
->type
->write_memory
;
842 target
->type
->write_memory
= target_write_memory_imp
;
843 target
->type
->read_memory_imp
= target
->type
->read_memory
;
844 target
->type
->read_memory
= target_read_memory_imp
;
845 target
->type
->soft_reset_halt_imp
= target
->type
->soft_reset_halt
;
846 target
->type
->soft_reset_halt
= target_soft_reset_halt_imp
;
847 target
->type
->run_algorithm_imp
= target
->type
->run_algorithm
;
848 target
->type
->run_algorithm
= target_run_algorithm_imp
;
850 if (target
->type
->mmu
== NULL
)
852 target
->type
->mmu
= default_mmu
;
854 target
= target
->next
;
859 if ((retval
= target_register_user_commands(cmd_ctx
)) != ERROR_OK
)
861 if ((retval
= target_register_timer_callback(handle_target
, 100, 1, NULL
)) != ERROR_OK
)
868 int target_register_event_callback(int (*callback
)(struct target_s
*target
, enum target_event event
, void *priv
), void *priv
)
870 target_event_callback_t
**callbacks_p
= &target_event_callbacks
;
872 if (callback
== NULL
)
874 return ERROR_INVALID_ARGUMENTS
;
879 while ((*callbacks_p
)->next
)
880 callbacks_p
= &((*callbacks_p
)->next
);
881 callbacks_p
= &((*callbacks_p
)->next
);
884 (*callbacks_p
) = malloc(sizeof(target_event_callback_t
));
885 (*callbacks_p
)->callback
= callback
;
886 (*callbacks_p
)->priv
= priv
;
887 (*callbacks_p
)->next
= NULL
;
892 int target_register_timer_callback(int (*callback
)(void *priv
), int time_ms
, int periodic
, void *priv
)
894 target_timer_callback_t
**callbacks_p
= &target_timer_callbacks
;
897 if (callback
== NULL
)
899 return ERROR_INVALID_ARGUMENTS
;
904 while ((*callbacks_p
)->next
)
905 callbacks_p
= &((*callbacks_p
)->next
);
906 callbacks_p
= &((*callbacks_p
)->next
);
909 (*callbacks_p
) = malloc(sizeof(target_timer_callback_t
));
910 (*callbacks_p
)->callback
= callback
;
911 (*callbacks_p
)->periodic
= periodic
;
912 (*callbacks_p
)->time_ms
= time_ms
;
914 gettimeofday(&now
, NULL
);
915 (*callbacks_p
)->when
.tv_usec
= now
.tv_usec
+ (time_ms
% 1000) * 1000;
916 time_ms
-= (time_ms
% 1000);
917 (*callbacks_p
)->when
.tv_sec
= now
.tv_sec
+ (time_ms
/ 1000);
918 if ((*callbacks_p
)->when
.tv_usec
> 1000000)
920 (*callbacks_p
)->when
.tv_usec
= (*callbacks_p
)->when
.tv_usec
- 1000000;
921 (*callbacks_p
)->when
.tv_sec
+= 1;
924 (*callbacks_p
)->priv
= priv
;
925 (*callbacks_p
)->next
= NULL
;
930 int target_unregister_event_callback(int (*callback
)(struct target_s
*target
, enum target_event event
, void *priv
), void *priv
)
932 target_event_callback_t
**p
= &target_event_callbacks
;
933 target_event_callback_t
*c
= target_event_callbacks
;
935 if (callback
== NULL
)
937 return ERROR_INVALID_ARGUMENTS
;
942 target_event_callback_t
*next
= c
->next
;
943 if ((c
->callback
== callback
) && (c
->priv
== priv
))
957 int target_unregister_timer_callback(int (*callback
)(void *priv
), void *priv
)
959 target_timer_callback_t
**p
= &target_timer_callbacks
;
960 target_timer_callback_t
*c
= target_timer_callbacks
;
962 if (callback
== NULL
)
964 return ERROR_INVALID_ARGUMENTS
;
969 target_timer_callback_t
*next
= c
->next
;
970 if ((c
->callback
== callback
) && (c
->priv
== priv
))
984 int target_call_event_callbacks(target_t
*target
, enum target_event event
)
986 target_event_callback_t
*callback
= target_event_callbacks
;
987 target_event_callback_t
*next_callback
;
989 if (event
== TARGET_EVENT_HALTED
)
991 /* execute early halted first */
992 target_call_event_callbacks(target
, TARGET_EVENT_GDB_HALT
);
995 LOG_DEBUG("target event %i (%s)",
997 Jim_Nvp_value2name_simple(nvp_target_event
, event
)->name
);
999 target_handle_event(target
, event
);
1003 next_callback
= callback
->next
;
1004 callback
->callback(target
, event
, callback
->priv
);
1005 callback
= next_callback
;
1011 static int target_timer_callback_periodic_restart(
1012 target_timer_callback_t
*cb
, struct timeval
*now
)
1014 int time_ms
= cb
->time_ms
;
1015 cb
->when
.tv_usec
= now
->tv_usec
+ (time_ms
% 1000) * 1000;
1016 time_ms
-= (time_ms
% 1000);
1017 cb
->when
.tv_sec
= now
->tv_sec
+ time_ms
/ 1000;
1018 if (cb
->when
.tv_usec
> 1000000)
1020 cb
->when
.tv_usec
= cb
->when
.tv_usec
- 1000000;
1021 cb
->when
.tv_sec
+= 1;
1026 static int target_call_timer_callback(target_timer_callback_t
*cb
,
1027 struct timeval
*now
)
1029 cb
->callback(cb
->priv
);
1032 return target_timer_callback_periodic_restart(cb
, now
);
1034 return target_unregister_timer_callback(cb
->callback
, cb
->priv
);
1037 static int target_call_timer_callbacks_check_time(int checktime
)
1042 gettimeofday(&now
, NULL
);
1044 target_timer_callback_t
*callback
= target_timer_callbacks
;
1047 // cleaning up may unregister and free this callback
1048 target_timer_callback_t
*next_callback
= callback
->next
;
1050 bool call_it
= callback
->callback
&&
1051 ((!checktime
&& callback
->periodic
) ||
1052 now
.tv_sec
> callback
->when
.tv_sec
||
1053 (now
.tv_sec
== callback
->when
.tv_sec
&&
1054 now
.tv_usec
>= callback
->when
.tv_usec
));
1058 int retval
= target_call_timer_callback(callback
, &now
);
1059 if (retval
!= ERROR_OK
)
1063 callback
= next_callback
;
1069 int target_call_timer_callbacks(void)
1071 return target_call_timer_callbacks_check_time(1);
1074 /* invoke periodic callbacks immediately */
1075 int target_call_timer_callbacks_now(void)
1077 return target_call_timer_callbacks_check_time(0);
1080 int target_alloc_working_area(struct target_s
*target
, uint32_t size
, working_area_t
**area
)
1082 working_area_t
*c
= target
->working_areas
;
1083 working_area_t
*new_wa
= NULL
;
1085 /* Reevaluate working area address based on MMU state*/
1086 if (target
->working_areas
== NULL
)
1091 retval
= target
->type
->mmu(target
, &enabled
);
1092 if (retval
!= ERROR_OK
)
1098 if (target
->working_area_phys_spec
) {
1099 LOG_DEBUG("MMU disabled, using physical "
1100 "address for working memory 0x%08x",
1101 (unsigned)target
->working_area_phys
);
1102 target
->working_area
= target
->working_area_phys
;
1104 LOG_ERROR("No working memory available. "
1105 "Specify -work-area-phys to target.");
1106 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
1109 if (target
->working_area_virt_spec
) {
1110 LOG_DEBUG("MMU enabled, using virtual "
1111 "address for working memory 0x%08x",
1112 (unsigned)target
->working_area_virt
);
1113 target
->working_area
= target
->working_area_virt
;
1115 LOG_ERROR("No working memory available. "
1116 "Specify -work-area-virt to target.");
1117 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
1122 /* only allocate multiples of 4 byte */
1125 LOG_ERROR("BUG: code tried to allocate unaligned number of bytes (0x%08x), padding", ((unsigned)(size
)));
1126 size
= (size
+ 3) & (~3);
1129 /* see if there's already a matching working area */
1132 if ((c
->free
) && (c
->size
== size
))
1140 /* if not, allocate a new one */
1143 working_area_t
**p
= &target
->working_areas
;
1144 uint32_t first_free
= target
->working_area
;
1145 uint32_t free_size
= target
->working_area_size
;
1147 c
= target
->working_areas
;
1150 first_free
+= c
->size
;
1151 free_size
-= c
->size
;
1156 if (free_size
< size
)
1158 LOG_WARNING("not enough working area available(requested %u, free %u)",
1159 (unsigned)(size
), (unsigned)(free_size
));
1160 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
1163 LOG_DEBUG("allocated new working area at address 0x%08x", (unsigned)first_free
);
1165 new_wa
= malloc(sizeof(working_area_t
));
1166 new_wa
->next
= NULL
;
1167 new_wa
->size
= size
;
1168 new_wa
->address
= first_free
;
1170 if (target
->backup_working_area
)
1173 new_wa
->backup
= malloc(new_wa
->size
);
1174 if ((retval
= target_read_memory(target
, new_wa
->address
, 4, new_wa
->size
/ 4, new_wa
->backup
)) != ERROR_OK
)
1176 free(new_wa
->backup
);
1183 new_wa
->backup
= NULL
;
1186 /* put new entry in list */
1190 /* mark as used, and return the new (reused) area */
1195 new_wa
->user
= area
;
1200 int target_free_working_area_restore(struct target_s
*target
, working_area_t
*area
, int restore
)
1205 if (restore
&& target
->backup_working_area
)
1208 if ((retval
= target_write_memory(target
, area
->address
, 4, area
->size
/ 4, area
->backup
)) != ERROR_OK
)
1214 /* mark user pointer invalid */
1221 int target_free_working_area(struct target_s
*target
, working_area_t
*area
)
1223 return target_free_working_area_restore(target
, area
, 1);
1226 /* free resources and restore memory, if restoring memory fails,
1227 * free up resources anyway
1229 void target_free_all_working_areas_restore(struct target_s
*target
, int restore
)
1231 working_area_t
*c
= target
->working_areas
;
1235 working_area_t
*next
= c
->next
;
1236 target_free_working_area_restore(target
, c
, restore
);
1246 target
->working_areas
= NULL
;
1249 void target_free_all_working_areas(struct target_s
*target
)
1251 target_free_all_working_areas_restore(target
, 1);
1254 int target_register_commands(struct command_context_s
*cmd_ctx
)
1257 register_command(cmd_ctx
, NULL
, "targets",
1258 handle_targets_command
, COMMAND_EXEC
,
1259 "change current command line target (one parameter) "
1260 "or list targets (no parameters)");
1262 register_jim(cmd_ctx
, "target", jim_target
, "configure target");
1267 int target_arch_state(struct target_s
*target
)
1272 LOG_USER("No target has been configured");
1276 LOG_USER("target state: %s", target_state_name( target
));
1278 if (target
->state
!= TARGET_HALTED
)
1281 retval
= target
->type
->arch_state(target
);
1285 /* Single aligned words are guaranteed to use 16 or 32 bit access
1286 * mode respectively, otherwise data is handled as quickly as
1289 int target_write_buffer(struct target_s
*target
, uint32_t address
, uint32_t size
, uint8_t *buffer
)
1292 LOG_DEBUG("writing buffer of %i byte at 0x%8.8x",
1293 (int)size
, (unsigned)address
);
1295 if (!target_was_examined(target
))
1297 LOG_ERROR("Target not examined yet");
1305 if ((address
+ size
- 1) < address
)
1307 /* GDB can request this when e.g. PC is 0xfffffffc*/
1308 LOG_ERROR("address + size wrapped(0x%08x, 0x%08x)",
1314 if (((address
% 2) == 0) && (size
== 2))
1316 return target_write_memory(target
, address
, 2, 1, buffer
);
1319 /* handle unaligned head bytes */
1322 uint32_t unaligned
= 4 - (address
% 4);
1324 if (unaligned
> size
)
1327 if ((retval
= target_write_memory(target
, address
, 1, unaligned
, buffer
)) != ERROR_OK
)
1330 buffer
+= unaligned
;
1331 address
+= unaligned
;
1335 /* handle aligned words */
1338 int aligned
= size
- (size
% 4);
1340 /* use bulk writes above a certain limit. This may have to be changed */
1343 if ((retval
= target
->type
->bulk_write_memory(target
, address
, aligned
/ 4, buffer
)) != ERROR_OK
)
1348 if ((retval
= target_write_memory(target
, address
, 4, aligned
/ 4, buffer
)) != ERROR_OK
)
1357 /* handle tail writes of less than 4 bytes */
1360 if ((retval
= target_write_memory(target
, address
, 1, size
, buffer
)) != ERROR_OK
)
1367 /* Single aligned words are guaranteed to use 16 or 32 bit access
1368 * mode respectively, otherwise data is handled as quickly as
1371 int target_read_buffer(struct target_s
*target
, uint32_t address
, uint32_t size
, uint8_t *buffer
)
1374 LOG_DEBUG("reading buffer of %i byte at 0x%8.8x",
1375 (int)size
, (unsigned)address
);
1377 if (!target_was_examined(target
))
1379 LOG_ERROR("Target not examined yet");
1387 if ((address
+ size
- 1) < address
)
1389 /* GDB can request this when e.g. PC is 0xfffffffc*/
1390 LOG_ERROR("address + size wrapped(0x%08" PRIx32
", 0x%08" PRIx32
")",
1396 if (((address
% 2) == 0) && (size
== 2))
1398 return target_read_memory(target
, address
, 2, 1, buffer
);
1401 /* handle unaligned head bytes */
1404 uint32_t unaligned
= 4 - (address
% 4);
1406 if (unaligned
> size
)
1409 if ((retval
= target_read_memory(target
, address
, 1, unaligned
, buffer
)) != ERROR_OK
)
1412 buffer
+= unaligned
;
1413 address
+= unaligned
;
1417 /* handle aligned words */
1420 int aligned
= size
- (size
% 4);
1422 if ((retval
= target_read_memory(target
, address
, 4, aligned
/ 4, buffer
)) != ERROR_OK
)
1430 /*prevent byte access when possible (avoid AHB access limitations in some cases)*/
1433 int aligned
= size
- (size
%2);
1434 retval
= target_read_memory(target
, address
, 2, aligned
/ 2, buffer
);
1435 if (retval
!= ERROR_OK
)
1442 /* handle tail writes of less than 4 bytes */
1445 if ((retval
= target_read_memory(target
, address
, 1, size
, buffer
)) != ERROR_OK
)
1452 int target_checksum_memory(struct target_s
*target
, uint32_t address
, uint32_t size
, uint32_t* crc
)
1457 uint32_t checksum
= 0;
1458 if (!target_was_examined(target
))
1460 LOG_ERROR("Target not examined yet");
1464 if ((retval
= target
->type
->checksum_memory(target
, address
,
1465 size
, &checksum
)) != ERROR_OK
)
1467 buffer
= malloc(size
);
1470 LOG_ERROR("error allocating buffer for section (%d bytes)", (int)size
);
1471 return ERROR_INVALID_ARGUMENTS
;
1473 retval
= target_read_buffer(target
, address
, size
, buffer
);
1474 if (retval
!= ERROR_OK
)
1480 /* convert to target endianess */
1481 for (i
= 0; i
< (size
/sizeof(uint32_t)); i
++)
1483 uint32_t target_data
;
1484 target_data
= target_buffer_get_u32(target
, &buffer
[i
*sizeof(uint32_t)]);
1485 target_buffer_set_u32(target
, &buffer
[i
*sizeof(uint32_t)], target_data
);
1488 retval
= image_calculate_checksum(buffer
, size
, &checksum
);
1497 int target_blank_check_memory(struct target_s
*target
, uint32_t address
, uint32_t size
, uint32_t* blank
)
1500 if (!target_was_examined(target
))
1502 LOG_ERROR("Target not examined yet");
1506 if (target
->type
->blank_check_memory
== 0)
1507 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
1509 retval
= target
->type
->blank_check_memory(target
, address
, size
, blank
);
1514 int target_read_u32(struct target_s
*target
, uint32_t address
, uint32_t *value
)
1516 uint8_t value_buf
[4];
1517 if (!target_was_examined(target
))
1519 LOG_ERROR("Target not examined yet");
1523 int retval
= target_read_memory(target
, address
, 4, 1, value_buf
);
1525 if (retval
== ERROR_OK
)
1527 *value
= target_buffer_get_u32(target
, value_buf
);
1528 LOG_DEBUG("address: 0x%8.8" PRIx32
", value: 0x%8.8" PRIx32
"",
1535 LOG_DEBUG("address: 0x%8.8" PRIx32
" failed",
1542 int target_read_u16(struct target_s
*target
, uint32_t address
, uint16_t *value
)
1544 uint8_t value_buf
[2];
1545 if (!target_was_examined(target
))
1547 LOG_ERROR("Target not examined yet");
1551 int retval
= target_read_memory(target
, address
, 2, 1, value_buf
);
1553 if (retval
== ERROR_OK
)
1555 *value
= target_buffer_get_u16(target
, value_buf
);
1556 LOG_DEBUG("address: 0x%8.8" PRIx32
", value: 0x%4.4x",
1563 LOG_DEBUG("address: 0x%8.8" PRIx32
" failed",
1570 int target_read_u8(struct target_s
*target
, uint32_t address
, uint8_t *value
)
1572 int retval
= target_read_memory(target
, address
, 1, 1, value
);
1573 if (!target_was_examined(target
))
1575 LOG_ERROR("Target not examined yet");
1579 if (retval
== ERROR_OK
)
1581 LOG_DEBUG("address: 0x%8.8" PRIx32
", value: 0x%2.2x",
1588 LOG_DEBUG("address: 0x%8.8" PRIx32
" failed",
1595 int target_write_u32(struct target_s
*target
, uint32_t address
, uint32_t value
)
1598 uint8_t value_buf
[4];
1599 if (!target_was_examined(target
))
1601 LOG_ERROR("Target not examined yet");
1605 LOG_DEBUG("address: 0x%8.8" PRIx32
", value: 0x%8.8" PRIx32
"",
1609 target_buffer_set_u32(target
, value_buf
, value
);
1610 if ((retval
= target_write_memory(target
, address
, 4, 1, value_buf
)) != ERROR_OK
)
1612 LOG_DEBUG("failed: %i", retval
);
1618 int target_write_u16(struct target_s
*target
, uint32_t address
, uint16_t value
)
1621 uint8_t value_buf
[2];
1622 if (!target_was_examined(target
))
1624 LOG_ERROR("Target not examined yet");
1628 LOG_DEBUG("address: 0x%8.8" PRIx32
", value: 0x%8.8x",
1632 target_buffer_set_u16(target
, value_buf
, value
);
1633 if ((retval
= target_write_memory(target
, address
, 2, 1, value_buf
)) != ERROR_OK
)
1635 LOG_DEBUG("failed: %i", retval
);
1641 int target_write_u8(struct target_s
*target
, uint32_t address
, uint8_t value
)
1644 if (!target_was_examined(target
))
1646 LOG_ERROR("Target not examined yet");
1650 LOG_DEBUG("address: 0x%8.8" PRIx32
", value: 0x%2.2x",
1653 if ((retval
= target_write_memory(target
, address
, 1, 1, &value
)) != ERROR_OK
)
1655 LOG_DEBUG("failed: %i", retval
);
1661 int target_register_user_commands(struct command_context_s
*cmd_ctx
)
1663 int retval
= ERROR_OK
;
1666 /* script procedures */
1667 register_command(cmd_ctx
, NULL
, "profile", handle_profile_command
, COMMAND_EXEC
, "profiling samples the CPU PC");
1668 register_jim(cmd_ctx
, "ocd_mem2array", jim_mem2array
, "read memory and return as a TCL array for script processing <ARRAYNAME> <WIDTH = 32/16/8> <ADDRESS> <COUNT>");
1669 register_jim(cmd_ctx
, "ocd_array2mem", jim_array2mem
, "convert a TCL array to memory locations and write the values <ARRAYNAME> <WIDTH = 32/16/8> <ADDRESS> <COUNT>");
1671 register_command(cmd_ctx
, NULL
, "fast_load_image", handle_fast_load_image_command
, COMMAND_ANY
,
1672 "same args as load_image, image stored in memory - mainly for profiling purposes");
1674 register_command(cmd_ctx
, NULL
, "fast_load", handle_fast_load_command
, COMMAND_ANY
,
1675 "loads active fast load image to current target - mainly for profiling purposes");
1678 register_command(cmd_ctx
, NULL
, "virt2phys", handle_virt2phys_command
, COMMAND_ANY
, "translate a virtual address into a physical address");
1679 register_command(cmd_ctx
, NULL
, "reg", handle_reg_command
, COMMAND_EXEC
, "display or set a register");
1680 register_command(cmd_ctx
, NULL
, "poll", handle_poll_command
, COMMAND_EXEC
, "poll target state");
1681 register_command(cmd_ctx
, NULL
, "wait_halt", handle_wait_halt_command
, COMMAND_EXEC
, "wait for target halt [time (s)]");
1682 register_command(cmd_ctx
, NULL
, "halt", handle_halt_command
, COMMAND_EXEC
, "halt target");
1683 register_command(cmd_ctx
, NULL
, "resume", handle_resume_command
, COMMAND_EXEC
, "resume target [addr]");
1684 register_command(cmd_ctx
, NULL
, "step", handle_step_command
, COMMAND_EXEC
, "step one instruction from current PC or [addr]");
1685 register_command(cmd_ctx
, NULL
, "reset", handle_reset_command
, COMMAND_EXEC
, "reset target [run | halt | init] - default is run");
1686 register_command(cmd_ctx
, NULL
, "soft_reset_halt", handle_soft_reset_halt_command
, COMMAND_EXEC
, "halt the target and do a soft reset");
1688 register_command(cmd_ctx
, NULL
, "mdw", handle_md_command
, COMMAND_EXEC
, "display memory words [phys] <addr> [count]");
1689 register_command(cmd_ctx
, NULL
, "mdh", handle_md_command
, COMMAND_EXEC
, "display memory half-words [phys] <addr> [count]");
1690 register_command(cmd_ctx
, NULL
, "mdb", handle_md_command
, COMMAND_EXEC
, "display memory bytes [phys] <addr> [count]");
1692 register_command(cmd_ctx
, NULL
, "mww", handle_mw_command
, COMMAND_EXEC
, "write memory word [phys] <addr> <value> [count]");
1693 register_command(cmd_ctx
, NULL
, "mwh", handle_mw_command
, COMMAND_EXEC
, "write memory half-word [phys] <addr> <value> [count]");
1694 register_command(cmd_ctx
, NULL
, "mwb", handle_mw_command
, COMMAND_EXEC
, "write memory byte [phys] <addr> <value> [count]");
1696 register_command(cmd_ctx
, NULL
, "bp",
1697 handle_bp_command
, COMMAND_EXEC
,
1698 "list or set breakpoint [<address> <length> [hw]]");
1699 register_command(cmd_ctx
, NULL
, "rbp",
1700 handle_rbp_command
, COMMAND_EXEC
,
1701 "remove breakpoint <address>");
1702 register_command(cmd_ctx
, NULL
, "wp",
1703 handle_wp_command
, COMMAND_EXEC
,
1704 "list or set watchpoint "
1705 "[<address> <length> <r/w/a> [value] [mask]]");
1706 register_command(cmd_ctx
, NULL
, "rwp",
1707 handle_rwp_command
, COMMAND_EXEC
,
1708 "remove watchpoint <address>");
1710 register_command(cmd_ctx
, NULL
, "load_image", handle_load_image_command
, COMMAND_EXEC
, "load_image <file> <address> ['bin'|'ihex'|'elf'|'s19'] [min_address] [max_length]");
1711 register_command(cmd_ctx
, NULL
, "dump_image", handle_dump_image_command
, COMMAND_EXEC
, "dump_image <file> <address> <size>");
1712 register_command(cmd_ctx
, NULL
, "verify_image", handle_verify_image_command
, COMMAND_EXEC
, "verify_image <file> [offset] [type]");
1713 register_command(cmd_ctx
, NULL
, "test_image", handle_test_image_command
, COMMAND_EXEC
, "test_image <file> [offset] [type]");
1715 if ((retval
= target_request_register_commands(cmd_ctx
)) != ERROR_OK
)
1717 if ((retval
= trace_register_commands(cmd_ctx
)) != ERROR_OK
)
1723 static int handle_targets_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
1725 target_t
*target
= all_targets
;
1729 target
= get_target(args
[0]);
1730 if (target
== NULL
) {
1731 command_print(cmd_ctx
,"Target: %s is unknown, try one of:\n", args
[0]);
1734 if (!target
->tap
->enabled
) {
1735 command_print(cmd_ctx
,"Target: TAP %s is disabled, "
1736 "can't be the current target\n",
1737 target
->tap
->dotted_name
);
1741 cmd_ctx
->current_target
= target
->target_number
;
1746 target
= all_targets
;
1747 command_print(cmd_ctx
, " TargetName Type Endian TapName State ");
1748 command_print(cmd_ctx
, "-- ------------------ ---------- ------ ------------------ ------------");
1754 if (target
->tap
->enabled
)
1755 state
= target_state_name( target
);
1757 state
= "tap-disabled";
1759 if (cmd_ctx
->current_target
== target
->target_number
)
1762 /* keep columns lined up to match the headers above */
1763 command_print(cmd_ctx
, "%2d%c %-18s %-10s %-6s %-18s %s",
1764 target
->target_number
,
1767 target_get_name(target
),
1768 Jim_Nvp_value2name_simple(nvp_target_endian
,
1769 target
->endianness
)->name
,
1770 target
->tap
->dotted_name
,
1772 target
= target
->next
;
1778 /* every 300ms we check for reset & powerdropout and issue a "reset halt" if so. */
1780 static int powerDropout
;
1781 static int srstAsserted
;
1783 static int runPowerRestore
;
1784 static int runPowerDropout
;
1785 static int runSrstAsserted
;
1786 static int runSrstDeasserted
;
1788 static int sense_handler(void)
1790 static int prevSrstAsserted
= 0;
1791 static int prevPowerdropout
= 0;
1794 if ((retval
= jtag_power_dropout(&powerDropout
)) != ERROR_OK
)
1798 powerRestored
= prevPowerdropout
&& !powerDropout
;
1801 runPowerRestore
= 1;
1804 long long current
= timeval_ms();
1805 static long long lastPower
= 0;
1806 int waitMore
= lastPower
+ 2000 > current
;
1807 if (powerDropout
&& !waitMore
)
1809 runPowerDropout
= 1;
1810 lastPower
= current
;
1813 if ((retval
= jtag_srst_asserted(&srstAsserted
)) != ERROR_OK
)
1817 srstDeasserted
= prevSrstAsserted
&& !srstAsserted
;
1819 static long long lastSrst
= 0;
1820 waitMore
= lastSrst
+ 2000 > current
;
1821 if (srstDeasserted
&& !waitMore
)
1823 runSrstDeasserted
= 1;
1827 if (!prevSrstAsserted
&& srstAsserted
)
1829 runSrstAsserted
= 1;
1832 prevSrstAsserted
= srstAsserted
;
1833 prevPowerdropout
= powerDropout
;
1835 if (srstDeasserted
|| powerRestored
)
1837 /* Other than logging the event we can't do anything here.
1838 * Issuing a reset is a particularly bad idea as we might
1839 * be inside a reset already.
1846 static void target_call_event_callbacks_all(enum target_event e
) {
1848 target
= all_targets
;
1850 target_call_event_callbacks(target
, e
);
1851 target
= target
->next
;
1855 /* process target state changes */
1856 int handle_target(void *priv
)
1858 int retval
= ERROR_OK
;
1860 /* we do not want to recurse here... */
1861 static int recursive
= 0;
1866 /* danger! running these procedures can trigger srst assertions and power dropouts.
1867 * We need to avoid an infinite loop/recursion here and we do that by
1868 * clearing the flags after running these events.
1870 int did_something
= 0;
1871 if (runSrstAsserted
)
1873 target_call_event_callbacks_all(TARGET_EVENT_GDB_HALT
);
1874 Jim_Eval(interp
, "srst_asserted");
1877 if (runSrstDeasserted
)
1879 Jim_Eval(interp
, "srst_deasserted");
1882 if (runPowerDropout
)
1884 target_call_event_callbacks_all(TARGET_EVENT_GDB_HALT
);
1885 Jim_Eval(interp
, "power_dropout");
1888 if (runPowerRestore
)
1890 Jim_Eval(interp
, "power_restore");
1896 /* clear detect flags */
1900 /* clear action flags */
1902 runSrstAsserted
= 0;
1903 runSrstDeasserted
= 0;
1904 runPowerRestore
= 0;
1905 runPowerDropout
= 0;
1910 /* Poll targets for state changes unless that's globally disabled.
1911 * Skip targets that are currently disabled.
1913 for (target_t
*target
= all_targets
;
1914 is_jtag_poll_safe() && target
;
1915 target
= target
->next
)
1917 if (!target
->tap
->enabled
)
1920 /* only poll target if we've got power and srst isn't asserted */
1921 if (!powerDropout
&& !srstAsserted
)
1923 /* polling may fail silently until the target has been examined */
1924 if ((retval
= target_poll(target
)) != ERROR_OK
)
1926 target_call_event_callbacks(target
, TARGET_EVENT_GDB_HALT
);
1935 static int handle_reg_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
1944 target
= get_current_target(cmd_ctx
);
1946 /* list all available registers for the current target */
1949 reg_cache_t
*cache
= target
->reg_cache
;
1956 command_print(cmd_ctx
, "===== %s", cache
->name
);
1958 for (i
= 0, reg
= cache
->reg_list
;
1959 i
< cache
->num_regs
;
1960 i
++, reg
++, count
++)
1962 /* only print cached values if they are valid */
1964 value
= buf_to_str(reg
->value
,
1966 command_print(cmd_ctx
,
1967 "(%i) %s (/%" PRIu32
"): 0x%s%s",
1975 command_print(cmd_ctx
, "(%i) %s (/%" PRIu32
")",
1980 cache
= cache
->next
;
1986 /* access a single register by its ordinal number */
1987 if ((args
[0][0] >= '0') && (args
[0][0] <= '9'))
1990 COMMAND_PARSE_NUMBER(uint
, args
[0], num
);
1992 reg_cache_t
*cache
= target
->reg_cache
;
1997 for (i
= 0; i
< cache
->num_regs
; i
++)
1999 if (count
++ == (int)num
)
2001 reg
= &cache
->reg_list
[i
];
2007 cache
= cache
->next
;
2012 command_print(cmd_ctx
, "%i is out of bounds, the current target has only %i registers (0 - %i)", num
, count
, count
- 1);
2015 } else /* access a single register by its name */
2017 reg
= register_get_by_name(target
->reg_cache
, args
[0], 1);
2021 command_print(cmd_ctx
, "register %s not found in current target", args
[0]);
2026 /* display a register */
2027 if ((argc
== 1) || ((argc
== 2) && !((args
[1][0] >= '0') && (args
[1][0] <= '9'))))
2029 if ((argc
== 2) && (strcmp(args
[1], "force") == 0))
2032 if (reg
->valid
== 0)
2034 reg_arch_type_t
*arch_type
= register_get_arch_type(reg
->arch_type
);
2035 arch_type
->get(reg
);
2037 value
= buf_to_str(reg
->value
, reg
->size
, 16);
2038 command_print(cmd_ctx
, "%s (/%i): 0x%s", reg
->name
, (int)(reg
->size
), value
);
2043 /* set register value */
2046 uint8_t *buf
= malloc(CEIL(reg
->size
, 8));
2047 str_to_buf(args
[1], strlen(args
[1]), buf
, reg
->size
, 0);
2049 reg_arch_type_t
*arch_type
= register_get_arch_type(reg
->arch_type
);
2050 arch_type
->set(reg
, buf
);
2052 value
= buf_to_str(reg
->value
, reg
->size
, 16);
2053 command_print(cmd_ctx
, "%s (/%i): 0x%s", reg
->name
, (int)(reg
->size
), value
);
2061 command_print(cmd_ctx
, "usage: reg <#|name> [value]");
2066 static int handle_poll_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
2068 int retval
= ERROR_OK
;
2069 target_t
*target
= get_current_target(cmd_ctx
);
2073 command_print(cmd_ctx
, "background polling: %s",
2074 jtag_poll_get_enabled() ? "on" : "off");
2075 command_print(cmd_ctx
, "TAP: %s (%s)",
2076 target
->tap
->dotted_name
,
2077 target
->tap
->enabled
? "enabled" : "disabled");
2078 if (!target
->tap
->enabled
)
2080 if ((retval
= target_poll(target
)) != ERROR_OK
)
2082 if ((retval
= target_arch_state(target
)) != ERROR_OK
)
2088 if (strcmp(args
[0], "on") == 0)
2090 jtag_poll_set_enabled(true);
2092 else if (strcmp(args
[0], "off") == 0)
2094 jtag_poll_set_enabled(false);
2098 command_print(cmd_ctx
, "arg is \"on\" or \"off\"");
2102 return ERROR_COMMAND_SYNTAX_ERROR
;
2108 static int handle_wait_halt_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
2111 return ERROR_COMMAND_SYNTAX_ERROR
;
2116 int retval
= parse_uint(args
[0], &ms
);
2117 if (ERROR_OK
!= retval
)
2119 command_print(cmd_ctx
, "usage: %s [seconds]", cmd
);
2120 return ERROR_COMMAND_SYNTAX_ERROR
;
2122 // convert seconds (given) to milliseconds (needed)
2126 target_t
*target
= get_current_target(cmd_ctx
);
2127 return target_wait_state(target
, TARGET_HALTED
, ms
);
2130 /* wait for target state to change. The trick here is to have a low
2131 * latency for short waits and not to suck up all the CPU time
2134 * After 500ms, keep_alive() is invoked
2136 int target_wait_state(target_t
*target
, enum target_state state
, int ms
)
2139 long long then
= 0, cur
;
2144 if ((retval
= target_poll(target
)) != ERROR_OK
)
2146 if (target
->state
== state
)
2154 then
= timeval_ms();
2155 LOG_DEBUG("waiting for target %s...",
2156 Jim_Nvp_value2name_simple(nvp_target_state
,state
)->name
);
2164 if ((cur
-then
) > ms
)
2166 LOG_ERROR("timed out while waiting for target %s",
2167 Jim_Nvp_value2name_simple(nvp_target_state
,state
)->name
);
2175 static int handle_halt_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
2179 target_t
*target
= get_current_target(cmd_ctx
);
2180 int retval
= target_halt(target
);
2181 if (ERROR_OK
!= retval
)
2187 retval
= parse_uint(args
[0], &wait
);
2188 if (ERROR_OK
!= retval
)
2189 return ERROR_COMMAND_SYNTAX_ERROR
;
2194 return handle_wait_halt_command(cmd_ctx
, cmd
, args
, argc
);
2197 static int handle_soft_reset_halt_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
2199 target_t
*target
= get_current_target(cmd_ctx
);
2201 LOG_USER("requesting target halt and executing a soft reset");
2203 target
->type
->soft_reset_halt(target
);
2208 static int handle_reset_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
2211 return ERROR_COMMAND_SYNTAX_ERROR
;
2213 enum target_reset_mode reset_mode
= RESET_RUN
;
2217 n
= Jim_Nvp_name2value_simple(nvp_reset_modes
, args
[0]);
2218 if ((n
->name
== NULL
) || (n
->value
== RESET_UNKNOWN
)) {
2219 return ERROR_COMMAND_SYNTAX_ERROR
;
2221 reset_mode
= n
->value
;
2224 /* reset *all* targets */
2225 return target_process_reset(cmd_ctx
, reset_mode
);
2229 static int handle_resume_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
2233 return ERROR_COMMAND_SYNTAX_ERROR
;
2235 target_t
*target
= get_current_target(cmd_ctx
);
2236 target_handle_event(target
, TARGET_EVENT_OLD_pre_resume
);
2238 /* with no args, resume from current pc, addr = 0,
2239 * with one arguments, addr = args[0],
2240 * handle breakpoints, not debugging */
2244 COMMAND_PARSE_NUMBER(u32
, args
[0], addr
);
2248 return target_resume(target
, current
, addr
, 1, 0);
2251 static int handle_step_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
2254 return ERROR_COMMAND_SYNTAX_ERROR
;
2258 /* with no args, step from current pc, addr = 0,
2259 * with one argument addr = args[0],
2260 * handle breakpoints, debugging */
2265 COMMAND_PARSE_NUMBER(u32
, args
[0], addr
);
2269 target_t
*target
= get_current_target(cmd_ctx
);
2271 return target
->type
->step(target
, current_pc
, addr
, 1);
2274 static void handle_md_output(struct command_context_s
*cmd_ctx
,
2275 struct target_s
*target
, uint32_t address
, unsigned size
,
2276 unsigned count
, const uint8_t *buffer
)
2278 const unsigned line_bytecnt
= 32;
2279 unsigned line_modulo
= line_bytecnt
/ size
;
2281 char output
[line_bytecnt
* 4 + 1];
2282 unsigned output_len
= 0;
2284 const char *value_fmt
;
2286 case 4: value_fmt
= "%8.8x "; break;
2287 case 2: value_fmt
= "%4.2x "; break;
2288 case 1: value_fmt
= "%2.2x "; break;
2290 LOG_ERROR("invalid memory read size: %u", size
);
2294 for (unsigned i
= 0; i
< count
; i
++)
2296 if (i
% line_modulo
== 0)
2298 output_len
+= snprintf(output
+ output_len
,
2299 sizeof(output
) - output_len
,
2301 (unsigned)(address
+ (i
*size
)));
2305 const uint8_t *value_ptr
= buffer
+ i
* size
;
2307 case 4: value
= target_buffer_get_u32(target
, value_ptr
); break;
2308 case 2: value
= target_buffer_get_u16(target
, value_ptr
); break;
2309 case 1: value
= *value_ptr
;
2311 output_len
+= snprintf(output
+ output_len
,
2312 sizeof(output
) - output_len
,
2315 if ((i
% line_modulo
== line_modulo
- 1) || (i
== count
- 1))
2317 command_print(cmd_ctx
, "%s", output
);
2323 static int handle_md_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
2326 return ERROR_COMMAND_SYNTAX_ERROR
;
2330 case 'w': size
= 4; break;
2331 case 'h': size
= 2; break;
2332 case 'b': size
= 1; break;
2333 default: return ERROR_COMMAND_SYNTAX_ERROR
;
2336 bool physical
=strcmp(args
[0], "phys")==0;
2337 int (*fn
)(struct target_s
*target
,
2338 uint32_t address
, uint32_t size
, uint32_t count
, uint8_t *buffer
);
2343 fn
=target_read_phys_memory
;
2346 fn
=target_read_memory
;
2348 if ((argc
< 1) || (argc
> 2))
2350 return ERROR_COMMAND_SYNTAX_ERROR
;
2354 COMMAND_PARSE_NUMBER(u32
, args
[0], address
);
2358 COMMAND_PARSE_NUMBER(uint
, args
[1], count
);
2360 uint8_t *buffer
= calloc(count
, size
);
2362 target_t
*target
= get_current_target(cmd_ctx
);
2363 int retval
= fn(target
, address
, size
, count
, buffer
);
2364 if (ERROR_OK
== retval
)
2365 handle_md_output(cmd_ctx
, target
, address
, size
, count
, buffer
);
2372 static int handle_mw_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
2376 return ERROR_COMMAND_SYNTAX_ERROR
;
2378 bool physical
=strcmp(args
[0], "phys")==0;
2379 int (*fn
)(struct target_s
*target
,
2380 uint32_t address
, uint32_t size
, uint32_t count
, uint8_t *buffer
);
2385 fn
=target_write_phys_memory
;
2388 fn
=target_write_memory
;
2390 if ((argc
< 2) || (argc
> 3))
2391 return ERROR_COMMAND_SYNTAX_ERROR
;
2394 COMMAND_PARSE_NUMBER(u32
, args
[0], address
);
2397 COMMAND_PARSE_NUMBER(u32
, args
[1], value
);
2401 COMMAND_PARSE_NUMBER(uint
, args
[2], count
);
2403 target_t
*target
= get_current_target(cmd_ctx
);
2405 uint8_t value_buf
[4];
2410 target_buffer_set_u32(target
, value_buf
, value
);
2414 target_buffer_set_u16(target
, value_buf
, value
);
2418 value_buf
[0] = value
;
2421 return ERROR_COMMAND_SYNTAX_ERROR
;
2423 for (unsigned i
= 0; i
< count
; i
++)
2425 int retval
= fn(target
,
2426 address
+ i
* wordsize
, wordsize
, 1, value_buf
);
2427 if (ERROR_OK
!= retval
)
2436 static int parse_load_image_command_args(struct command_context_s
*cmd_ctx
,
2437 char **args
, int argc
, image_t
*image
,
2438 uint32_t *min_address
, uint32_t *max_address
)
2440 if (argc
< 1 || argc
> 5)
2441 return ERROR_COMMAND_SYNTAX_ERROR
;
2443 /* a base address isn't always necessary,
2444 * default to 0x0 (i.e. don't relocate) */
2448 COMMAND_PARSE_NUMBER(u32
, args
[1], addr
);
2449 image
->base_address
= addr
;
2450 image
->base_address_set
= 1;
2453 image
->base_address_set
= 0;
2455 image
->start_address_set
= 0;
2459 COMMAND_PARSE_NUMBER(u32
, args
[3], *min_address
);
2463 COMMAND_PARSE_NUMBER(u32
, args
[4], *max_address
);
2464 // use size (given) to find max (required)
2465 *max_address
+= *min_address
;
2468 if (*min_address
> *max_address
)
2469 return ERROR_COMMAND_SYNTAX_ERROR
;
2474 static int handle_load_image_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
2478 uint32_t image_size
;
2479 uint32_t min_address
= 0;
2480 uint32_t max_address
= 0xffffffff;
2484 int retval
= parse_load_image_command_args(cmd_ctx
, args
, argc
,
2485 &image
, &min_address
, &max_address
);
2486 if (ERROR_OK
!= retval
)
2489 target_t
*target
= get_current_target(cmd_ctx
);
2491 struct duration bench
;
2492 duration_start(&bench
);
2494 if (image_open(&image
, args
[0], (argc
>= 3) ? args
[2] : NULL
) != ERROR_OK
)
2501 for (i
= 0; i
< image
.num_sections
; i
++)
2503 buffer
= malloc(image
.sections
[i
].size
);
2506 command_print(cmd_ctx
,
2507 "error allocating buffer for section (%d bytes)",
2508 (int)(image
.sections
[i
].size
));
2512 if ((retval
= image_read_section(&image
, i
, 0x0, image
.sections
[i
].size
, buffer
, &buf_cnt
)) != ERROR_OK
)
2518 uint32_t offset
= 0;
2519 uint32_t length
= buf_cnt
;
2521 /* DANGER!!! beware of unsigned comparision here!!! */
2523 if ((image
.sections
[i
].base_address
+ buf_cnt
>= min_address
)&&
2524 (image
.sections
[i
].base_address
< max_address
))
2526 if (image
.sections
[i
].base_address
< min_address
)
2528 /* clip addresses below */
2529 offset
+= min_address
-image
.sections
[i
].base_address
;
2533 if (image
.sections
[i
].base_address
+ buf_cnt
> max_address
)
2535 length
-= (image
.sections
[i
].base_address
+ buf_cnt
)-max_address
;
2538 if ((retval
= target_write_buffer(target
, image
.sections
[i
].base_address
+ offset
, length
, buffer
+ offset
)) != ERROR_OK
)
2543 image_size
+= length
;
2544 command_print(cmd_ctx
, "%u bytes written at address 0x%8.8" PRIx32
"",
2545 (unsigned int)length
,
2546 image
.sections
[i
].base_address
+ offset
);
2552 if ((ERROR_OK
== retval
) && (duration_measure(&bench
) == ERROR_OK
))
2554 command_print(cmd_ctx
, "downloaded %" PRIu32
" bytes "
2555 "in %fs (%0.3f kb/s)", image_size
,
2556 duration_elapsed(&bench
), duration_kbps(&bench
, image_size
));
2559 image_close(&image
);
2565 static int handle_dump_image_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
2569 uint8_t buffer
[560];
2573 target_t
*target
= get_current_target(cmd_ctx
);
2577 command_print(cmd_ctx
, "usage: dump_image <filename> <address> <size>");
2582 COMMAND_PARSE_NUMBER(u32
, args
[1], address
);
2584 COMMAND_PARSE_NUMBER(u32
, args
[2], size
);
2586 if (fileio_open(&fileio
, args
[0], FILEIO_WRITE
, FILEIO_BINARY
) != ERROR_OK
)
2591 struct duration bench
;
2592 duration_start(&bench
);
2594 int retval
= ERROR_OK
;
2597 uint32_t size_written
;
2598 uint32_t this_run_size
= (size
> 560) ? 560 : size
;
2599 retval
= target_read_buffer(target
, address
, this_run_size
, buffer
);
2600 if (retval
!= ERROR_OK
)
2605 retval
= fileio_write(&fileio
, this_run_size
, buffer
, &size_written
);
2606 if (retval
!= ERROR_OK
)
2611 size
-= this_run_size
;
2612 address
+= this_run_size
;
2615 if ((retvaltemp
= fileio_close(&fileio
)) != ERROR_OK
)
2618 if ((ERROR_OK
== retval
) && (duration_measure(&bench
) == ERROR_OK
))
2620 command_print(cmd_ctx
,
2621 "dumped %lld bytes in %fs (%0.3f kb/s)", fileio
.size
,
2622 duration_elapsed(&bench
), duration_kbps(&bench
, fileio
.size
));
2628 static int handle_verify_image_command_internal(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
, int verify
)
2632 uint32_t image_size
;
2635 uint32_t checksum
= 0;
2636 uint32_t mem_checksum
= 0;
2640 target_t
*target
= get_current_target(cmd_ctx
);
2644 return ERROR_COMMAND_SYNTAX_ERROR
;
2649 LOG_ERROR("no target selected");
2653 struct duration bench
;
2654 duration_start(&bench
);
2659 COMMAND_PARSE_NUMBER(u32
, args
[1], addr
);
2660 image
.base_address
= addr
;
2661 image
.base_address_set
= 1;
2665 image
.base_address_set
= 0;
2666 image
.base_address
= 0x0;
2669 image
.start_address_set
= 0;
2671 if ((retval
= image_open(&image
, args
[0], (argc
== 3) ? args
[2] : NULL
)) != ERROR_OK
)
2678 for (i
= 0; i
< image
.num_sections
; i
++)
2680 buffer
= malloc(image
.sections
[i
].size
);
2683 command_print(cmd_ctx
,
2684 "error allocating buffer for section (%d bytes)",
2685 (int)(image
.sections
[i
].size
));
2688 if ((retval
= image_read_section(&image
, i
, 0x0, image
.sections
[i
].size
, buffer
, &buf_cnt
)) != ERROR_OK
)
2696 /* calculate checksum of image */
2697 image_calculate_checksum(buffer
, buf_cnt
, &checksum
);
2699 retval
= target_checksum_memory(target
, image
.sections
[i
].base_address
, buf_cnt
, &mem_checksum
);
2700 if (retval
!= ERROR_OK
)
2706 if (checksum
!= mem_checksum
)
2708 /* failed crc checksum, fall back to a binary compare */
2711 command_print(cmd_ctx
, "checksum mismatch - attempting binary compare");
2713 data
= (uint8_t*)malloc(buf_cnt
);
2715 /* Can we use 32bit word accesses? */
2717 int count
= buf_cnt
;
2718 if ((count
% 4) == 0)
2723 retval
= target_read_memory(target
, image
.sections
[i
].base_address
, size
, count
, data
);
2724 if (retval
== ERROR_OK
)
2727 for (t
= 0; t
< buf_cnt
; t
++)
2729 if (data
[t
] != buffer
[t
])
2731 command_print(cmd_ctx
,
2732 "Verify operation failed address 0x%08x. Was 0x%02x instead of 0x%02x\n",
2733 (unsigned)(t
+ image
.sections
[i
].base_address
),
2738 retval
= ERROR_FAIL
;
2752 command_print(cmd_ctx
, "address 0x%08" PRIx32
" length 0x%08" PRIx32
"",
2753 image
.sections
[i
].base_address
,
2758 image_size
+= buf_cnt
;
2761 if ((ERROR_OK
== retval
) && (duration_measure(&bench
) == ERROR_OK
))
2763 command_print(cmd_ctx
, "verified %" PRIu32
" bytes "
2764 "in %fs (%0.3f kb/s)", image_size
,
2765 duration_elapsed(&bench
), duration_kbps(&bench
, image_size
));
2768 image_close(&image
);
2773 static int handle_verify_image_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
2775 return handle_verify_image_command_internal(cmd_ctx
, cmd
, args
, argc
, 1);
2778 static int handle_test_image_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
2780 return handle_verify_image_command_internal(cmd_ctx
, cmd
, args
, argc
, 0);
2783 static int handle_bp_command_list(struct command_context_s
*cmd_ctx
)
2785 target_t
*target
= get_current_target(cmd_ctx
);
2786 breakpoint_t
*breakpoint
= target
->breakpoints
;
2789 if (breakpoint
->type
== BKPT_SOFT
)
2791 char* buf
= buf_to_str(breakpoint
->orig_instr
,
2792 breakpoint
->length
, 16);
2793 command_print(cmd_ctx
, "0x%8.8" PRIx32
", 0x%x, %i, 0x%s",
2794 breakpoint
->address
,
2796 breakpoint
->set
, buf
);
2801 command_print(cmd_ctx
, "0x%8.8" PRIx32
", 0x%x, %i",
2802 breakpoint
->address
,
2803 breakpoint
->length
, breakpoint
->set
);
2806 breakpoint
= breakpoint
->next
;
2811 static int handle_bp_command_set(struct command_context_s
*cmd_ctx
,
2812 uint32_t addr
, uint32_t length
, int hw
)
2814 target_t
*target
= get_current_target(cmd_ctx
);
2815 int retval
= breakpoint_add(target
, addr
, length
, hw
);
2816 if (ERROR_OK
== retval
)
2817 command_print(cmd_ctx
, "breakpoint set at 0x%8.8" PRIx32
"", addr
);
2819 LOG_ERROR("Failure setting breakpoint");
2823 static int handle_bp_command(struct command_context_s
*cmd_ctx
,
2824 char *cmd
, char **args
, int argc
)
2827 return handle_bp_command_list(cmd_ctx
);
2829 if (argc
< 2 || argc
> 3)
2831 command_print(cmd_ctx
, "usage: bp <address> <length> ['hw']");
2832 return ERROR_COMMAND_SYNTAX_ERROR
;
2836 COMMAND_PARSE_NUMBER(u32
, args
[0], addr
);
2838 COMMAND_PARSE_NUMBER(u32
, args
[1], length
);
2843 if (strcmp(args
[2], "hw") == 0)
2846 return ERROR_COMMAND_SYNTAX_ERROR
;
2849 return handle_bp_command_set(cmd_ctx
, addr
, length
, hw
);
2852 static int handle_rbp_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
2855 return ERROR_COMMAND_SYNTAX_ERROR
;
2858 COMMAND_PARSE_NUMBER(u32
, args
[0], addr
);
2860 target_t
*target
= get_current_target(cmd_ctx
);
2861 breakpoint_remove(target
, addr
);
2866 static int handle_wp_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
2868 target_t
*target
= get_current_target(cmd_ctx
);
2872 watchpoint_t
*watchpoint
= target
->watchpoints
;
2876 command_print(cmd_ctx
,
2877 "address: 0x%8.8" PRIx32
", len: 0x%8.8x, r/w/a: %i, value: 0x%8.8" PRIx32
", mask: 0x%8.8" PRIx32
"",
2878 watchpoint
->address
,
2880 (int)(watchpoint
->rw
),
2883 watchpoint
= watchpoint
->next
;
2888 enum watchpoint_rw type
= WPT_ACCESS
;
2890 uint32_t length
= 0;
2891 uint32_t data_value
= 0x0;
2892 uint32_t data_mask
= 0xffffffff;
2897 COMMAND_PARSE_NUMBER(u32
, args
[4], data_mask
);
2900 COMMAND_PARSE_NUMBER(u32
, args
[3], data_value
);
2915 LOG_ERROR("invalid watchpoint mode ('%c')", args
[2][0]);
2916 return ERROR_COMMAND_SYNTAX_ERROR
;
2920 COMMAND_PARSE_NUMBER(u32
, args
[1], length
);
2921 COMMAND_PARSE_NUMBER(u32
, args
[0], addr
);
2925 command_print(cmd_ctx
, "usage: wp [address length "
2926 "[(r|w|a) [value [mask]]]]");
2927 return ERROR_COMMAND_SYNTAX_ERROR
;
2930 int retval
= watchpoint_add(target
, addr
, length
, type
,
2931 data_value
, data_mask
);
2932 if (ERROR_OK
!= retval
)
2933 LOG_ERROR("Failure setting watchpoints");
2938 static int handle_rwp_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
2941 return ERROR_COMMAND_SYNTAX_ERROR
;
2944 COMMAND_PARSE_NUMBER(u32
, args
[0], addr
);
2946 target_t
*target
= get_current_target(cmd_ctx
);
2947 watchpoint_remove(target
, addr
);
2954 * Translate a virtual address to a physical address.
2956 * The low-level target implementation must have logged a detailed error
2957 * which is forwarded to telnet/GDB session.
2959 static int handle_virt2phys_command(command_context_t
*cmd_ctx
,
2960 char *cmd
, char **args
, int argc
)
2963 return ERROR_COMMAND_SYNTAX_ERROR
;
2966 COMMAND_PARSE_NUMBER(u32
, args
[0], va
);
2969 target_t
*target
= get_current_target(cmd_ctx
);
2970 int retval
= target
->type
->virt2phys(target
, va
, &pa
);
2971 if (retval
== ERROR_OK
)
2972 command_print(cmd_ctx
, "Physical address 0x%08" PRIx32
"", pa
);
2977 static void writeData(FILE *f
, const void *data
, size_t len
)
2979 size_t written
= fwrite(data
, 1, len
, f
);
2981 LOG_ERROR("failed to write %zu bytes: %s", len
, strerror(errno
));
2984 static void writeLong(FILE *f
, int l
)
2987 for (i
= 0; i
< 4; i
++)
2989 char c
= (l
>> (i
*8))&0xff;
2990 writeData(f
, &c
, 1);
2995 static void writeString(FILE *f
, char *s
)
2997 writeData(f
, s
, strlen(s
));
3000 /* Dump a gmon.out histogram file. */
3001 static void writeGmon(uint32_t *samples
, uint32_t sampleNum
, char *filename
)
3004 FILE *f
= fopen(filename
, "w");
3007 writeString(f
, "gmon");
3008 writeLong(f
, 0x00000001); /* Version */
3009 writeLong(f
, 0); /* padding */
3010 writeLong(f
, 0); /* padding */
3011 writeLong(f
, 0); /* padding */
3013 uint8_t zero
= 0; /* GMON_TAG_TIME_HIST */
3014 writeData(f
, &zero
, 1);
3016 /* figure out bucket size */
3017 uint32_t min
= samples
[0];
3018 uint32_t max
= samples
[0];
3019 for (i
= 0; i
< sampleNum
; i
++)
3021 if (min
> samples
[i
])
3025 if (max
< samples
[i
])
3031 int addressSpace
= (max
-min
+ 1);
3033 static const uint32_t maxBuckets
= 256 * 1024; /* maximum buckets. */
3034 uint32_t length
= addressSpace
;
3035 if (length
> maxBuckets
)
3037 length
= maxBuckets
;
3039 int *buckets
= malloc(sizeof(int)*length
);
3040 if (buckets
== NULL
)
3045 memset(buckets
, 0, sizeof(int)*length
);
3046 for (i
= 0; i
< sampleNum
;i
++)
3048 uint32_t address
= samples
[i
];
3049 long long a
= address
-min
;
3050 long long b
= length
-1;
3051 long long c
= addressSpace
-1;
3052 int index
= (a
*b
)/c
; /* danger!!!! int32 overflows */
3056 /* append binary memory gmon.out &profile_hist_hdr ((char*)&profile_hist_hdr + sizeof(struct gmon_hist_hdr)) */
3057 writeLong(f
, min
); /* low_pc */
3058 writeLong(f
, max
); /* high_pc */
3059 writeLong(f
, length
); /* # of samples */
3060 writeLong(f
, 64000000); /* 64MHz */
3061 writeString(f
, "seconds");
3062 for (i
= 0; i
< (15-strlen("seconds")); i
++)
3063 writeData(f
, &zero
, 1);
3064 writeString(f
, "s");
3066 /*append binary memory gmon.out profile_hist_data (profile_hist_data + profile_hist_hdr.hist_size) */
3068 char *data
= malloc(2*length
);
3071 for (i
= 0; i
< length
;i
++)
3080 data
[i
*2 + 1]=(val
>> 8)&0xff;
3083 writeData(f
, data
, length
* 2);
3093 /* profiling samples the CPU PC as quickly as OpenOCD is able, which will be used as a random sampling of PC */
3094 static int handle_profile_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
3096 target_t
*target
= get_current_target(cmd_ctx
);
3097 struct timeval timeout
, now
;
3099 gettimeofday(&timeout
, NULL
);
3102 return ERROR_COMMAND_SYNTAX_ERROR
;
3105 COMMAND_PARSE_NUMBER(uint
, args
[0], offset
);
3107 timeval_add_time(&timeout
, offset
, 0);
3109 command_print(cmd_ctx
, "Starting profiling. Halting and resuming the target as often as we can...");
3111 static const int maxSample
= 10000;
3112 uint32_t *samples
= malloc(sizeof(uint32_t)*maxSample
);
3113 if (samples
== NULL
)
3117 /* hopefully it is safe to cache! We want to stop/restart as quickly as possible. */
3118 reg_t
*reg
= register_get_by_name(target
->reg_cache
, "pc", 1);
3123 target_poll(target
);
3124 if (target
->state
== TARGET_HALTED
)
3126 uint32_t t
=*((uint32_t *)reg
->value
);
3127 samples
[numSamples
++]=t
;
3128 retval
= target_resume(target
, 1, 0, 0, 0); /* current pc, addr = 0, do not handle breakpoints, not debugging */
3129 target_poll(target
);
3130 alive_sleep(10); /* sleep 10ms, i.e. <100 samples/second. */
3131 } else if (target
->state
== TARGET_RUNNING
)
3133 /* We want to quickly sample the PC. */
3134 if ((retval
= target_halt(target
)) != ERROR_OK
)
3141 command_print(cmd_ctx
, "Target not halted or running");
3145 if (retval
!= ERROR_OK
)
3150 gettimeofday(&now
, NULL
);
3151 if ((numSamples
>= maxSample
) || ((now
.tv_sec
>= timeout
.tv_sec
) && (now
.tv_usec
>= timeout
.tv_usec
)))
3153 command_print(cmd_ctx
, "Profiling completed. %d samples.", numSamples
);
3154 if ((retval
= target_poll(target
)) != ERROR_OK
)
3159 if (target
->state
== TARGET_HALTED
)
3161 target_resume(target
, 1, 0, 0, 0); /* current pc, addr = 0, do not handle breakpoints, not debugging */
3163 if ((retval
= target_poll(target
)) != ERROR_OK
)
3168 writeGmon(samples
, numSamples
, args
[1]);
3169 command_print(cmd_ctx
, "Wrote %s", args
[1]);
3178 static int new_int_array_element(Jim_Interp
* interp
, const char *varname
, int idx
, uint32_t val
)
3181 Jim_Obj
*nameObjPtr
, *valObjPtr
;
3184 namebuf
= alloc_printf("%s(%d)", varname
, idx
);
3188 nameObjPtr
= Jim_NewStringObj(interp
, namebuf
, -1);
3189 valObjPtr
= Jim_NewIntObj(interp
, val
);
3190 if (!nameObjPtr
|| !valObjPtr
)
3196 Jim_IncrRefCount(nameObjPtr
);
3197 Jim_IncrRefCount(valObjPtr
);
3198 result
= Jim_SetVariable(interp
, nameObjPtr
, valObjPtr
);
3199 Jim_DecrRefCount(interp
, nameObjPtr
);
3200 Jim_DecrRefCount(interp
, valObjPtr
);
3202 /* printf("%s(%d) <= 0%08x\n", varname, idx, val); */
3206 static int jim_mem2array(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
3208 command_context_t
*context
;
3211 context
= Jim_GetAssocData(interp
, "context");
3212 if (context
== NULL
)
3214 LOG_ERROR("mem2array: no command context");
3217 target
= get_current_target(context
);
3220 LOG_ERROR("mem2array: no current target");
3224 return target_mem2array(interp
, target
, argc
-1, argv
+ 1);
3227 static int target_mem2array(Jim_Interp
*interp
, target_t
*target
, int argc
, Jim_Obj
*const *argv
)
3235 const char *varname
;
3236 uint8_t buffer
[4096];
3240 /* argv[1] = name of array to receive the data
3241 * argv[2] = desired width
3242 * argv[3] = memory address
3243 * argv[4] = count of times to read
3246 Jim_WrongNumArgs(interp
, 1, argv
, "varname width addr nelems");
3249 varname
= Jim_GetString(argv
[0], &len
);
3250 /* given "foo" get space for worse case "foo(%d)" .. add 20 */
3252 e
= Jim_GetLong(interp
, argv
[1], &l
);
3258 e
= Jim_GetLong(interp
, argv
[2], &l
);
3263 e
= Jim_GetLong(interp
, argv
[3], &l
);
3279 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3280 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "Invalid width param, must be 8/16/32", NULL
);
3284 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3285 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "mem2array: zero width read?", NULL
);
3288 if ((addr
+ (len
* width
)) < addr
) {
3289 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3290 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "mem2array: addr + len - wraps to zero?", NULL
);
3293 /* absurd transfer size? */
3295 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3296 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "mem2array: absurd > 64K item request", NULL
);
3301 ((width
== 2) && ((addr
& 1) == 0)) ||
3302 ((width
== 4) && ((addr
& 3) == 0))) {
3306 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3307 sprintf(buf
, "mem2array address: 0x%08" PRIx32
" is not aligned for %" PRId32
" byte reads",
3310 Jim_AppendStrings(interp
, Jim_GetResult(interp
), buf
, NULL
);
3321 /* Slurp... in buffer size chunks */
3323 count
= len
; /* in objects.. */
3324 if (count
> (sizeof(buffer
)/width
)) {
3325 count
= (sizeof(buffer
)/width
);
3328 retval
= target_read_memory(target
, addr
, width
, count
, buffer
);
3329 if (retval
!= ERROR_OK
) {
3331 LOG_ERROR("mem2array: Read @ 0x%08x, w=%d, cnt=%d, failed",
3335 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3336 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "mem2array: cannot read memory", NULL
);
3340 v
= 0; /* shut up gcc */
3341 for (i
= 0 ;i
< count
;i
++, n
++) {
3344 v
= target_buffer_get_u32(target
, &buffer
[i
*width
]);
3347 v
= target_buffer_get_u16(target
, &buffer
[i
*width
]);
3350 v
= buffer
[i
] & 0x0ff;
3353 new_int_array_element(interp
, varname
, n
, v
);
3359 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3364 static int get_int_array_element(Jim_Interp
* interp
, const char *varname
, int idx
, uint32_t *val
)
3367 Jim_Obj
*nameObjPtr
, *valObjPtr
;
3371 namebuf
= alloc_printf("%s(%d)", varname
, idx
);
3375 nameObjPtr
= Jim_NewStringObj(interp
, namebuf
, -1);
3382 Jim_IncrRefCount(nameObjPtr
);
3383 valObjPtr
= Jim_GetVariable(interp
, nameObjPtr
, JIM_ERRMSG
);
3384 Jim_DecrRefCount(interp
, nameObjPtr
);
3386 if (valObjPtr
== NULL
)
3389 result
= Jim_GetLong(interp
, valObjPtr
, &l
);
3390 /* printf("%s(%d) => 0%08x\n", varname, idx, val); */
3395 static int jim_array2mem(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
3397 command_context_t
*context
;
3400 context
= Jim_GetAssocData(interp
, "context");
3401 if (context
== NULL
) {
3402 LOG_ERROR("array2mem: no command context");
3405 target
= get_current_target(context
);
3406 if (target
== NULL
) {
3407 LOG_ERROR("array2mem: no current target");
3411 return target_array2mem(interp
,target
, argc
-1, argv
+ 1);
3413 static int target_array2mem(Jim_Interp
*interp
, target_t
*target
, int argc
, Jim_Obj
*const *argv
)
3421 const char *varname
;
3422 uint8_t buffer
[4096];
3426 /* argv[1] = name of array to get the data
3427 * argv[2] = desired width
3428 * argv[3] = memory address
3429 * argv[4] = count to write
3432 Jim_WrongNumArgs(interp
, 1, argv
, "varname width addr nelems");
3435 varname
= Jim_GetString(argv
[0], &len
);
3436 /* given "foo" get space for worse case "foo(%d)" .. add 20 */
3438 e
= Jim_GetLong(interp
, argv
[1], &l
);
3444 e
= Jim_GetLong(interp
, argv
[2], &l
);
3449 e
= Jim_GetLong(interp
, argv
[3], &l
);
3465 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3466 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "Invalid width param, must be 8/16/32", NULL
);
3470 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3471 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "array2mem: zero width read?", NULL
);
3474 if ((addr
+ (len
* width
)) < addr
) {
3475 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3476 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "array2mem: addr + len - wraps to zero?", NULL
);
3479 /* absurd transfer size? */
3481 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3482 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "array2mem: absurd > 64K item request", NULL
);
3487 ((width
== 2) && ((addr
& 1) == 0)) ||
3488 ((width
== 4) && ((addr
& 3) == 0))) {
3492 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3493 sprintf(buf
, "array2mem address: 0x%08x is not aligned for %d byte reads",
3496 Jim_AppendStrings(interp
, Jim_GetResult(interp
), buf
, NULL
);
3507 /* Slurp... in buffer size chunks */
3509 count
= len
; /* in objects.. */
3510 if (count
> (sizeof(buffer
)/width
)) {
3511 count
= (sizeof(buffer
)/width
);
3514 v
= 0; /* shut up gcc */
3515 for (i
= 0 ;i
< count
;i
++, n
++) {
3516 get_int_array_element(interp
, varname
, n
, &v
);
3519 target_buffer_set_u32(target
, &buffer
[i
*width
], v
);
3522 target_buffer_set_u16(target
, &buffer
[i
*width
], v
);
3525 buffer
[i
] = v
& 0x0ff;
3531 retval
= target_write_memory(target
, addr
, width
, count
, buffer
);
3532 if (retval
!= ERROR_OK
) {
3534 LOG_ERROR("array2mem: Write @ 0x%08x, w=%d, cnt=%d, failed",
3538 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3539 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "array2mem: cannot read memory", NULL
);
3545 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3550 void target_all_handle_event(enum target_event e
)
3554 LOG_DEBUG("**all*targets: event: %d, %s",
3556 Jim_Nvp_value2name_simple(nvp_target_event
, e
)->name
);
3558 target
= all_targets
;
3560 target_handle_event(target
, e
);
3561 target
= target
->next
;
3566 /* FIX? should we propagate errors here rather than printing them
3569 void target_handle_event(target_t
*target
, enum target_event e
)
3571 target_event_action_t
*teap
;
3573 for (teap
= target
->event_action
; teap
!= NULL
; teap
= teap
->next
) {
3574 if (teap
->event
== e
) {
3575 LOG_DEBUG("target: (%d) %s (%s) event: %d (%s) action: %s",
3576 target
->target_number
,
3578 target_get_name(target
),
3580 Jim_Nvp_value2name_simple(nvp_target_event
, e
)->name
,
3581 Jim_GetString(teap
->body
, NULL
));
3582 if (Jim_EvalObj(interp
, teap
->body
) != JIM_OK
)
3584 Jim_PrintErrorMessage(interp
);
3590 enum target_cfg_param
{
3593 TCFG_WORK_AREA_VIRT
,
3594 TCFG_WORK_AREA_PHYS
,
3595 TCFG_WORK_AREA_SIZE
,
3596 TCFG_WORK_AREA_BACKUP
,
3599 TCFG_CHAIN_POSITION
,
3602 static Jim_Nvp nvp_config_opts
[] = {
3603 { .name
= "-type", .value
= TCFG_TYPE
},
3604 { .name
= "-event", .value
= TCFG_EVENT
},
3605 { .name
= "-work-area-virt", .value
= TCFG_WORK_AREA_VIRT
},
3606 { .name
= "-work-area-phys", .value
= TCFG_WORK_AREA_PHYS
},
3607 { .name
= "-work-area-size", .value
= TCFG_WORK_AREA_SIZE
},
3608 { .name
= "-work-area-backup", .value
= TCFG_WORK_AREA_BACKUP
},
3609 { .name
= "-endian" , .value
= TCFG_ENDIAN
},
3610 { .name
= "-variant", .value
= TCFG_VARIANT
},
3611 { .name
= "-chain-position", .value
= TCFG_CHAIN_POSITION
},
3613 { .name
= NULL
, .value
= -1 }
3616 static int target_configure(Jim_GetOptInfo
*goi
, target_t
*target
)
3624 /* parse config or cget options ... */
3625 while (goi
->argc
> 0) {
3626 Jim_SetEmptyResult(goi
->interp
);
3627 /* Jim_GetOpt_Debug(goi); */
3629 if (target
->type
->target_jim_configure
) {
3630 /* target defines a configure function */
3631 /* target gets first dibs on parameters */
3632 e
= (*(target
->type
->target_jim_configure
))(target
, goi
);
3641 /* otherwise we 'continue' below */
3643 e
= Jim_GetOpt_Nvp(goi
, nvp_config_opts
, &n
);
3645 Jim_GetOpt_NvpUnknown(goi
, nvp_config_opts
, 0);
3651 if (goi
->isconfigure
) {
3652 Jim_SetResult_sprintf(goi
->interp
, "not setable: %s", n
->name
);
3656 if (goi
->argc
!= 0) {
3657 Jim_WrongNumArgs(goi
->interp
, goi
->argc
, goi
->argv
, "NO PARAMS");
3661 Jim_SetResultString(goi
->interp
, target_get_name(target
), -1);
3665 if (goi
->argc
== 0) {
3666 Jim_WrongNumArgs(goi
->interp
, goi
->argc
, goi
->argv
, "-event ?event-name? ...");
3670 e
= Jim_GetOpt_Nvp(goi
, nvp_target_event
, &n
);
3672 Jim_GetOpt_NvpUnknown(goi
, nvp_target_event
, 1);
3676 if (goi
->isconfigure
) {
3677 if (goi
->argc
!= 1) {
3678 Jim_WrongNumArgs(goi
->interp
, goi
->argc
, goi
->argv
, "-event ?event-name? ?EVENT-BODY?");
3682 if (goi
->argc
!= 0) {
3683 Jim_WrongNumArgs(goi
->interp
, goi
->argc
, goi
->argv
, "-event ?event-name?");
3689 target_event_action_t
*teap
;
3691 teap
= target
->event_action
;
3692 /* replace existing? */
3694 if (teap
->event
== (enum target_event
)n
->value
) {
3700 if (goi
->isconfigure
) {
3701 bool replace
= true;
3704 teap
= calloc(1, sizeof(*teap
));
3707 teap
->event
= n
->value
;
3708 Jim_GetOpt_Obj(goi
, &o
);
3710 Jim_DecrRefCount(interp
, teap
->body
);
3712 teap
->body
= Jim_DuplicateObj(goi
->interp
, o
);
3715 * Tcl/TK - "tk events" have a nice feature.
3716 * See the "BIND" command.
3717 * We should support that here.
3718 * You can specify %X and %Y in the event code.
3719 * The idea is: %T - target name.
3720 * The idea is: %N - target number
3721 * The idea is: %E - event name.
3723 Jim_IncrRefCount(teap
->body
);
3727 /* add to head of event list */
3728 teap
->next
= target
->event_action
;
3729 target
->event_action
= teap
;
3731 Jim_SetEmptyResult(goi
->interp
);
3735 Jim_SetEmptyResult(goi
->interp
);
3737 Jim_SetResult(goi
->interp
, Jim_DuplicateObj(goi
->interp
, teap
->body
));
3744 case TCFG_WORK_AREA_VIRT
:
3745 if (goi
->isconfigure
) {
3746 target_free_all_working_areas(target
);
3747 e
= Jim_GetOpt_Wide(goi
, &w
);
3751 target
->working_area_virt
= w
;
3752 target
->working_area_virt_spec
= true;
3754 if (goi
->argc
!= 0) {
3758 Jim_SetResult(interp
, Jim_NewIntObj(goi
->interp
, target
->working_area_virt
));
3762 case TCFG_WORK_AREA_PHYS
:
3763 if (goi
->isconfigure
) {
3764 target_free_all_working_areas(target
);
3765 e
= Jim_GetOpt_Wide(goi
, &w
);
3769 target
->working_area_phys
= w
;
3770 target
->working_area_phys_spec
= true;
3772 if (goi
->argc
!= 0) {
3776 Jim_SetResult(interp
, Jim_NewIntObj(goi
->interp
, target
->working_area_phys
));
3780 case TCFG_WORK_AREA_SIZE
:
3781 if (goi
->isconfigure
) {
3782 target_free_all_working_areas(target
);
3783 e
= Jim_GetOpt_Wide(goi
, &w
);
3787 target
->working_area_size
= w
;
3789 if (goi
->argc
!= 0) {
3793 Jim_SetResult(interp
, Jim_NewIntObj(goi
->interp
, target
->working_area_size
));
3797 case TCFG_WORK_AREA_BACKUP
:
3798 if (goi
->isconfigure
) {
3799 target_free_all_working_areas(target
);
3800 e
= Jim_GetOpt_Wide(goi
, &w
);
3804 /* make this exactly 1 or 0 */
3805 target
->backup_working_area
= (!!w
);
3807 if (goi
->argc
!= 0) {
3811 Jim_SetResult(interp
, Jim_NewIntObj(goi
->interp
, target
->backup_working_area
));
3812 /* loop for more e*/
3816 if (goi
->isconfigure
) {
3817 e
= Jim_GetOpt_Nvp(goi
, nvp_target_endian
, &n
);
3819 Jim_GetOpt_NvpUnknown(goi
, nvp_target_endian
, 1);
3822 target
->endianness
= n
->value
;
3824 if (goi
->argc
!= 0) {
3828 n
= Jim_Nvp_value2name_simple(nvp_target_endian
, target
->endianness
);
3829 if (n
->name
== NULL
) {
3830 target
->endianness
= TARGET_LITTLE_ENDIAN
;
3831 n
= Jim_Nvp_value2name_simple(nvp_target_endian
, target
->endianness
);
3833 Jim_SetResultString(goi
->interp
, n
->name
, -1);
3838 if (goi
->isconfigure
) {
3839 if (goi
->argc
< 1) {
3840 Jim_SetResult_sprintf(goi
->interp
,
3845 if (target
->variant
) {
3846 free((void *)(target
->variant
));
3848 e
= Jim_GetOpt_String(goi
, &cp
, NULL
);
3849 target
->variant
= strdup(cp
);
3851 if (goi
->argc
!= 0) {
3855 Jim_SetResultString(goi
->interp
, target
->variant
,-1);
3858 case TCFG_CHAIN_POSITION
:
3859 if (goi
->isconfigure
) {
3862 target_free_all_working_areas(target
);
3863 e
= Jim_GetOpt_Obj(goi
, &o
);
3867 tap
= jtag_tap_by_jim_obj(goi
->interp
, o
);
3871 /* make this exactly 1 or 0 */
3874 if (goi
->argc
!= 0) {
3878 Jim_SetResultString(interp
, target
->tap
->dotted_name
, -1);
3879 /* loop for more e*/
3882 } /* while (goi->argc) */
3885 /* done - we return */
3889 /** this is the 'tcl' handler for the target specific command */
3890 static int tcl_target_func(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
3895 uint8_t target_buf
[32];
3898 struct command_context_s
*cmd_ctx
;
3905 TS_CMD_MWW
, TS_CMD_MWH
, TS_CMD_MWB
,
3906 TS_CMD_MDW
, TS_CMD_MDH
, TS_CMD_MDB
,
3907 TS_CMD_MRW
, TS_CMD_MRH
, TS_CMD_MRB
,
3908 TS_CMD_MEM2ARRAY
, TS_CMD_ARRAY2MEM
,
3916 TS_CMD_INVOKE_EVENT
,
3919 static const Jim_Nvp target_options
[] = {
3920 { .name
= "configure", .value
= TS_CMD_CONFIGURE
},
3921 { .name
= "cget", .value
= TS_CMD_CGET
},
3922 { .name
= "mww", .value
= TS_CMD_MWW
},
3923 { .name
= "mwh", .value
= TS_CMD_MWH
},
3924 { .name
= "mwb", .value
= TS_CMD_MWB
},
3925 { .name
= "mdw", .value
= TS_CMD_MDW
},
3926 { .name
= "mdh", .value
= TS_CMD_MDH
},
3927 { .name
= "mdb", .value
= TS_CMD_MDB
},
3928 { .name
= "mem2array", .value
= TS_CMD_MEM2ARRAY
},
3929 { .name
= "array2mem", .value
= TS_CMD_ARRAY2MEM
},
3930 { .name
= "eventlist", .value
= TS_CMD_EVENTLIST
},
3931 { .name
= "curstate", .value
= TS_CMD_CURSTATE
},
3933 { .name
= "arp_examine", .value
= TS_CMD_EXAMINE
},
3934 { .name
= "arp_poll", .value
= TS_CMD_POLL
},
3935 { .name
= "arp_reset", .value
= TS_CMD_RESET
},
3936 { .name
= "arp_halt", .value
= TS_CMD_HALT
},
3937 { .name
= "arp_waitstate", .value
= TS_CMD_WAITSTATE
},
3938 { .name
= "invoke-event", .value
= TS_CMD_INVOKE_EVENT
},
3940 { .name
= NULL
, .value
= -1 },
3943 /* go past the "command" */
3944 Jim_GetOpt_Setup(&goi
, interp
, argc
-1, argv
+ 1);
3946 target
= Jim_CmdPrivData(goi
.interp
);
3947 cmd_ctx
= Jim_GetAssocData(goi
.interp
, "context");
3949 /* commands here are in an NVP table */
3950 e
= Jim_GetOpt_Nvp(&goi
, target_options
, &n
);
3952 Jim_GetOpt_NvpUnknown(&goi
, target_options
, 0);
3955 /* Assume blank result */
3956 Jim_SetEmptyResult(goi
.interp
);
3959 case TS_CMD_CONFIGURE
:
3961 Jim_WrongNumArgs(goi
.interp
, goi
.argc
, goi
.argv
, "missing: -option VALUE ...");
3964 goi
.isconfigure
= 1;
3965 return target_configure(&goi
, target
);
3967 // some things take params
3969 Jim_WrongNumArgs(goi
.interp
, 0, goi
.argv
, "missing: ?-option?");
3972 goi
.isconfigure
= 0;
3973 return target_configure(&goi
, target
);
3981 * argv[3] = optional count.
3984 if ((goi
.argc
== 2) || (goi
.argc
== 3)) {
3988 Jim_SetResult_sprintf(goi
.interp
, "expected: %s ADDR DATA [COUNT]", n
->name
);
3992 e
= Jim_GetOpt_Wide(&goi
, &a
);
3997 e
= Jim_GetOpt_Wide(&goi
, &b
);
4001 if (goi
.argc
== 3) {
4002 e
= Jim_GetOpt_Wide(&goi
, &c
);
4012 target_buffer_set_u32(target
, target_buf
, b
);
4016 target_buffer_set_u16(target
, target_buf
, b
);
4020 target_buffer_set_u8(target
, target_buf
, b
);
4024 for (x
= 0 ; x
< c
; x
++) {
4025 e
= target_write_memory(target
, a
, b
, 1, target_buf
);
4026 if (e
!= ERROR_OK
) {
4027 Jim_SetResult_sprintf(interp
, "Error writing @ 0x%08x: %d\n", (int)(a
), e
);
4040 /* argv[0] = command
4042 * argv[2] = optional count
4044 if ((goi
.argc
== 2) || (goi
.argc
== 3)) {
4045 Jim_SetResult_sprintf(goi
.interp
, "expected: %s ADDR [COUNT]", n
->name
);
4048 e
= Jim_GetOpt_Wide(&goi
, &a
);
4053 e
= Jim_GetOpt_Wide(&goi
, &c
);
4060 b
= 1; /* shut up gcc */
4073 /* convert to "bytes" */
4075 /* count is now in 'BYTES' */
4081 e
= target_read_memory(target
, a
, b
, y
/ b
, target_buf
);
4082 if (e
!= ERROR_OK
) {
4083 Jim_SetResult_sprintf(interp
, "error reading target @ 0x%08lx", (int)(a
));
4087 Jim_fprintf(interp
, interp
->cookie_stdout
, "0x%08x ", (int)(a
));
4090 for (x
= 0 ; (x
< 16) && (x
< y
) ; x
+= 4) {
4091 z
= target_buffer_get_u32(target
, &(target_buf
[ x
* 4 ]));
4092 Jim_fprintf(interp
, interp
->cookie_stdout
, "%08x ", (int)(z
));
4094 for (; (x
< 16) ; x
+= 4) {
4095 Jim_fprintf(interp
, interp
->cookie_stdout
, " ");
4099 for (x
= 0 ; (x
< 16) && (x
< y
) ; x
+= 2) {
4100 z
= target_buffer_get_u16(target
, &(target_buf
[ x
* 2 ]));
4101 Jim_fprintf(interp
, interp
->cookie_stdout
, "%04x ", (int)(z
));
4103 for (; (x
< 16) ; x
+= 2) {
4104 Jim_fprintf(interp
, interp
->cookie_stdout
, " ");
4109 for (x
= 0 ; (x
< 16) && (x
< y
) ; x
+= 1) {
4110 z
= target_buffer_get_u8(target
, &(target_buf
[ x
* 4 ]));
4111 Jim_fprintf(interp
, interp
->cookie_stdout
, "%02x ", (int)(z
));
4113 for (; (x
< 16) ; x
+= 1) {
4114 Jim_fprintf(interp
, interp
->cookie_stdout
, " ");
4118 /* ascii-ify the bytes */
4119 for (x
= 0 ; x
< y
; x
++) {
4120 if ((target_buf
[x
] >= 0x20) &&
4121 (target_buf
[x
] <= 0x7e)) {
4125 target_buf
[x
] = '.';
4130 target_buf
[x
] = ' ';
4135 /* print - with a newline */
4136 Jim_fprintf(interp
, interp
->cookie_stdout
, "%s\n", target_buf
);
4142 case TS_CMD_MEM2ARRAY
:
4143 return target_mem2array(goi
.interp
, target
, goi
.argc
, goi
.argv
);
4145 case TS_CMD_ARRAY2MEM
:
4146 return target_array2mem(goi
.interp
, target
, goi
.argc
, goi
.argv
);
4148 case TS_CMD_EXAMINE
:
4150 Jim_WrongNumArgs(goi
.interp
, 2, argv
, "[no parameters]");
4153 if (!target
->tap
->enabled
)
4154 goto err_tap_disabled
;
4155 e
= target
->type
->examine(target
);
4156 if (e
!= ERROR_OK
) {
4157 Jim_SetResult_sprintf(interp
, "examine-fails: %d", e
);
4163 Jim_WrongNumArgs(goi
.interp
, 2, argv
, "[no parameters]");
4166 if (!target
->tap
->enabled
)
4167 goto err_tap_disabled
;
4168 if (!(target_was_examined(target
))) {
4169 e
= ERROR_TARGET_NOT_EXAMINED
;
4171 e
= target
->type
->poll(target
);
4173 if (e
!= ERROR_OK
) {
4174 Jim_SetResult_sprintf(interp
, "poll-fails: %d", e
);
4181 if (goi
.argc
!= 2) {
4182 Jim_WrongNumArgs(interp
, 2, argv
,
4183 "([tT]|[fF]|assert|deassert) BOOL");
4186 e
= Jim_GetOpt_Nvp(&goi
, nvp_assert
, &n
);
4188 Jim_GetOpt_NvpUnknown(&goi
, nvp_assert
, 1);
4191 /* the halt or not param */
4192 e
= Jim_GetOpt_Wide(&goi
, &a
);
4196 if (!target
->tap
->enabled
)
4197 goto err_tap_disabled
;
4198 if (!target
->type
->assert_reset
4199 || !target
->type
->deassert_reset
) {
4200 Jim_SetResult_sprintf(interp
,
4201 "No target-specific reset for %s",
4205 /* determine if we should halt or not. */
4206 target
->reset_halt
= !!a
;
4207 /* When this happens - all workareas are invalid. */
4208 target_free_all_working_areas_restore(target
, 0);
4211 if (n
->value
== NVP_ASSERT
) {
4212 e
= target
->type
->assert_reset(target
);
4214 e
= target
->type
->deassert_reset(target
);
4216 return (e
== ERROR_OK
) ? JIM_OK
: JIM_ERR
;
4219 Jim_WrongNumArgs(goi
.interp
, 0, argv
, "halt [no parameters]");
4222 if (!target
->tap
->enabled
)
4223 goto err_tap_disabled
;
4224 e
= target
->type
->halt(target
);
4225 return (e
== ERROR_OK
) ? JIM_OK
: JIM_ERR
;
4226 case TS_CMD_WAITSTATE
:
4227 /* params: <name> statename timeoutmsecs */
4228 if (goi
.argc
!= 2) {
4229 Jim_SetResult_sprintf(goi
.interp
, "%s STATENAME TIMEOUTMSECS", n
->name
);
4232 e
= Jim_GetOpt_Nvp(&goi
, nvp_target_state
, &n
);
4234 Jim_GetOpt_NvpUnknown(&goi
, nvp_target_state
,1);
4237 e
= Jim_GetOpt_Wide(&goi
, &a
);
4241 if (!target
->tap
->enabled
)
4242 goto err_tap_disabled
;
4243 e
= target_wait_state(target
, n
->value
, a
);
4244 if (e
!= ERROR_OK
) {
4245 Jim_SetResult_sprintf(goi
.interp
,
4246 "target: %s wait %s fails (%d) %s",
4249 e
, target_strerror_safe(e
));
4254 case TS_CMD_EVENTLIST
:
4255 /* List for human, Events defined for this target.
4256 * scripts/programs should use 'name cget -event NAME'
4259 target_event_action_t
*teap
;
4260 teap
= target
->event_action
;
4261 command_print(cmd_ctx
, "Event actions for target (%d) %s\n",
4262 target
->target_number
,
4264 command_print(cmd_ctx
, "%-25s | Body", "Event");
4265 command_print(cmd_ctx
, "------------------------- | ----------------------------------------");
4267 command_print(cmd_ctx
,
4269 Jim_Nvp_value2name_simple(nvp_target_event
, teap
->event
)->name
,
4270 Jim_GetString(teap
->body
, NULL
));
4273 command_print(cmd_ctx
, "***END***");
4276 case TS_CMD_CURSTATE
:
4277 if (goi
.argc
!= 0) {
4278 Jim_WrongNumArgs(goi
.interp
, 0, argv
, "[no parameters]");
4281 Jim_SetResultString(goi
.interp
,
4282 target_state_name( target
),
4285 case TS_CMD_INVOKE_EVENT
:
4286 if (goi
.argc
!= 1) {
4287 Jim_SetResult_sprintf(goi
.interp
, "%s ?EVENTNAME?",n
->name
);
4290 e
= Jim_GetOpt_Nvp(&goi
, nvp_target_event
, &n
);
4292 Jim_GetOpt_NvpUnknown(&goi
, nvp_target_event
, 1);
4295 target_handle_event(target
, n
->value
);
4301 Jim_SetResult_sprintf(interp
, "[TAP is disabled]");
4305 static int target_create(Jim_GetOptInfo
*goi
)
4314 struct command_context_s
*cmd_ctx
;
4316 cmd_ctx
= Jim_GetAssocData(goi
->interp
, "context");
4317 if (goi
->argc
< 3) {
4318 Jim_WrongNumArgs(goi
->interp
, 1, goi
->argv
, "?name? ?type? ..options...");
4323 Jim_GetOpt_Obj(goi
, &new_cmd
);
4324 /* does this command exist? */
4325 cmd
= Jim_GetCommand(goi
->interp
, new_cmd
, JIM_ERRMSG
);
4327 cp
= Jim_GetString(new_cmd
, NULL
);
4328 Jim_SetResult_sprintf(goi
->interp
, "Command/target: %s Exists", cp
);
4333 e
= Jim_GetOpt_String(goi
, &cp2
, NULL
);
4335 /* now does target type exist */
4336 for (x
= 0 ; target_types
[x
] ; x
++) {
4337 if (0 == strcmp(cp
, target_types
[x
]->name
)) {
4342 if (target_types
[x
] == NULL
) {
4343 Jim_SetResult_sprintf(goi
->interp
, "Unknown target type %s, try one of ", cp
);
4344 for (x
= 0 ; target_types
[x
] ; x
++) {
4345 if (target_types
[x
+ 1]) {
4346 Jim_AppendStrings(goi
->interp
,
4347 Jim_GetResult(goi
->interp
),
4348 target_types
[x
]->name
,
4351 Jim_AppendStrings(goi
->interp
,
4352 Jim_GetResult(goi
->interp
),
4354 target_types
[x
]->name
,NULL
);
4361 target
= calloc(1,sizeof(target_t
));
4362 /* set target number */
4363 target
->target_number
= new_target_number();
4365 /* allocate memory for each unique target type */
4366 target
->type
= (target_type_t
*)calloc(1,sizeof(target_type_t
));
4368 memcpy(target
->type
, target_types
[x
], sizeof(target_type_t
));
4370 /* will be set by "-endian" */
4371 target
->endianness
= TARGET_ENDIAN_UNKNOWN
;
4373 target
->working_area
= 0x0;
4374 target
->working_area_size
= 0x0;
4375 target
->working_areas
= NULL
;
4376 target
->backup_working_area
= 0;
4378 target
->state
= TARGET_UNKNOWN
;
4379 target
->debug_reason
= DBG_REASON_UNDEFINED
;
4380 target
->reg_cache
= NULL
;
4381 target
->breakpoints
= NULL
;
4382 target
->watchpoints
= NULL
;
4383 target
->next
= NULL
;
4384 target
->arch_info
= NULL
;
4386 target
->display
= 1;
4388 target
->halt_issued
= false;
4390 /* initialize trace information */
4391 target
->trace_info
= malloc(sizeof(trace_t
));
4392 target
->trace_info
->num_trace_points
= 0;
4393 target
->trace_info
->trace_points_size
= 0;
4394 target
->trace_info
->trace_points
= NULL
;
4395 target
->trace_info
->trace_history_size
= 0;
4396 target
->trace_info
->trace_history
= NULL
;
4397 target
->trace_info
->trace_history_pos
= 0;
4398 target
->trace_info
->trace_history_overflowed
= 0;
4400 target
->dbgmsg
= NULL
;
4401 target
->dbg_msg_enabled
= 0;
4403 target
->endianness
= TARGET_ENDIAN_UNKNOWN
;
4405 /* Do the rest as "configure" options */
4406 goi
->isconfigure
= 1;
4407 e
= target_configure(goi
, target
);
4409 if (target
->tap
== NULL
)
4411 Jim_SetResultString(interp
, "-chain-position required when creating target", -1);
4421 if (target
->endianness
== TARGET_ENDIAN_UNKNOWN
) {
4422 /* default endian to little if not specified */
4423 target
->endianness
= TARGET_LITTLE_ENDIAN
;
4426 /* incase variant is not set */
4427 if (!target
->variant
)
4428 target
->variant
= strdup("");
4430 /* create the target specific commands */
4431 if (target
->type
->register_commands
) {
4432 (*(target
->type
->register_commands
))(cmd_ctx
);
4434 if (target
->type
->target_create
) {
4435 (*(target
->type
->target_create
))(target
, goi
->interp
);
4438 /* append to end of list */
4441 tpp
= &(all_targets
);
4443 tpp
= &((*tpp
)->next
);
4448 cp
= Jim_GetString(new_cmd
, NULL
);
4449 target
->cmd_name
= strdup(cp
);
4451 /* now - create the new target name command */
4452 e
= Jim_CreateCommand(goi
->interp
,
4455 tcl_target_func
, /* C function */
4456 target
, /* private data */
4457 NULL
); /* no del proc */
4462 static int jim_target(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4466 struct command_context_s
*cmd_ctx
;
4470 /* TG = target generic */
4478 const char *target_cmds
[] = {
4479 "create", "types", "names", "current", "number",
4481 NULL
/* terminate */
4484 LOG_DEBUG("Target command params:");
4485 LOG_DEBUG("%s", Jim_Debug_ArgvString(interp
, argc
, argv
));
4487 cmd_ctx
= Jim_GetAssocData(interp
, "context");
4489 Jim_GetOpt_Setup(&goi
, interp
, argc
-1, argv
+ 1);
4491 if (goi
.argc
== 0) {
4492 Jim_WrongNumArgs(interp
, 1, argv
, "missing: command ...");
4496 /* Jim_GetOpt_Debug(&goi); */
4497 r
= Jim_GetOpt_Enum(&goi
, target_cmds
, &x
);
4504 Jim_Panic(goi
.interp
,"Why am I here?");
4506 case TG_CMD_CURRENT
:
4507 if (goi
.argc
!= 0) {
4508 Jim_WrongNumArgs(goi
.interp
, 1, goi
.argv
, "Too many parameters");
4511 Jim_SetResultString(goi
.interp
, get_current_target(cmd_ctx
)->cmd_name
, -1);
4514 if (goi
.argc
!= 0) {
4515 Jim_WrongNumArgs(goi
.interp
, 1, goi
.argv
, "Too many parameters");
4518 Jim_SetResult(goi
.interp
, Jim_NewListObj(goi
.interp
, NULL
, 0));
4519 for (x
= 0 ; target_types
[x
] ; x
++) {
4520 Jim_ListAppendElement(goi
.interp
,
4521 Jim_GetResult(goi
.interp
),
4522 Jim_NewStringObj(goi
.interp
, target_types
[x
]->name
, -1));
4526 if (goi
.argc
!= 0) {
4527 Jim_WrongNumArgs(goi
.interp
, 1, goi
.argv
, "Too many parameters");
4530 Jim_SetResult(goi
.interp
, Jim_NewListObj(goi
.interp
, NULL
, 0));
4531 target
= all_targets
;
4533 Jim_ListAppendElement(goi
.interp
,
4534 Jim_GetResult(goi
.interp
),
4535 Jim_NewStringObj(goi
.interp
, target
->cmd_name
, -1));
4536 target
= target
->next
;
4541 Jim_WrongNumArgs(goi
.interp
, goi
.argc
, goi
.argv
, "?name ... config options ...");
4544 return target_create(&goi
);
4547 /* It's OK to remove this mechanism sometime after August 2010 or so */
4548 LOG_WARNING("don't use numbers as target identifiers; use names");
4549 if (goi
.argc
!= 1) {
4550 Jim_SetResult_sprintf(goi
.interp
, "expected: target number ?NUMBER?");
4553 e
= Jim_GetOpt_Wide(&goi
, &w
);
4557 for (x
= 0, target
= all_targets
; target
; target
= target
->next
, x
++) {
4558 if (target
->target_number
== w
)
4561 if (target
== NULL
) {
4562 Jim_SetResult_sprintf(goi
.interp
,
4563 "Target: number %d does not exist", (int)(w
));
4566 Jim_SetResultString(goi
.interp
, target
->cmd_name
, -1);
4569 if (goi
.argc
!= 0) {
4570 Jim_WrongNumArgs(goi
.interp
, 0, goi
.argv
, "<no parameters>");
4573 for (x
= 0, target
= all_targets
; target
; target
= target
->next
, x
++)
4575 Jim_SetResult(goi
.interp
, Jim_NewIntObj(goi
.interp
, x
));
4591 static int fastload_num
;
4592 static struct FastLoad
*fastload
;
4594 static void free_fastload(void)
4596 if (fastload
!= NULL
)
4599 for (i
= 0; i
< fastload_num
; i
++)
4601 if (fastload
[i
].data
)
4602 free(fastload
[i
].data
);
4612 static int handle_fast_load_image_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
4616 uint32_t image_size
;
4617 uint32_t min_address
= 0;
4618 uint32_t max_address
= 0xffffffff;
4623 int retval
= parse_load_image_command_args(cmd_ctx
, args
, argc
,
4624 &image
, &min_address
, &max_address
);
4625 if (ERROR_OK
!= retval
)
4628 struct duration bench
;
4629 duration_start(&bench
);
4631 if (image_open(&image
, args
[0], (argc
>= 3) ? args
[2] : NULL
) != ERROR_OK
)
4638 fastload_num
= image
.num_sections
;
4639 fastload
= (struct FastLoad
*)malloc(sizeof(struct FastLoad
)*image
.num_sections
);
4640 if (fastload
== NULL
)
4642 image_close(&image
);
4645 memset(fastload
, 0, sizeof(struct FastLoad
)*image
.num_sections
);
4646 for (i
= 0; i
< image
.num_sections
; i
++)
4648 buffer
= malloc(image
.sections
[i
].size
);
4651 command_print(cmd_ctx
, "error allocating buffer for section (%d bytes)",
4652 (int)(image
.sections
[i
].size
));
4656 if ((retval
= image_read_section(&image
, i
, 0x0, image
.sections
[i
].size
, buffer
, &buf_cnt
)) != ERROR_OK
)
4662 uint32_t offset
= 0;
4663 uint32_t length
= buf_cnt
;
4666 /* DANGER!!! beware of unsigned comparision here!!! */
4668 if ((image
.sections
[i
].base_address
+ buf_cnt
>= min_address
)&&
4669 (image
.sections
[i
].base_address
< max_address
))
4671 if (image
.sections
[i
].base_address
< min_address
)
4673 /* clip addresses below */
4674 offset
+= min_address
-image
.sections
[i
].base_address
;
4678 if (image
.sections
[i
].base_address
+ buf_cnt
> max_address
)
4680 length
-= (image
.sections
[i
].base_address
+ buf_cnt
)-max_address
;
4683 fastload
[i
].address
= image
.sections
[i
].base_address
+ offset
;
4684 fastload
[i
].data
= malloc(length
);
4685 if (fastload
[i
].data
== NULL
)
4690 memcpy(fastload
[i
].data
, buffer
+ offset
, length
);
4691 fastload
[i
].length
= length
;
4693 image_size
+= length
;
4694 command_print(cmd_ctx
, "%u bytes written at address 0x%8.8x",
4695 (unsigned int)length
,
4696 ((unsigned int)(image
.sections
[i
].base_address
+ offset
)));
4702 if ((ERROR_OK
== retval
) && (duration_measure(&bench
) == ERROR_OK
))
4704 command_print(cmd_ctx
, "Loaded %" PRIu32
" bytes "
4705 "in %fs (%0.3f kb/s)", image_size
,
4706 duration_elapsed(&bench
), duration_kbps(&bench
, image_size
));
4708 command_print(cmd_ctx
,
4709 "WARNING: image has not been loaded to target!"
4710 "You can issue a 'fast_load' to finish loading.");
4713 image_close(&image
);
4715 if (retval
!= ERROR_OK
)
4723 static int handle_fast_load_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
4726 return ERROR_COMMAND_SYNTAX_ERROR
;
4727 if (fastload
== NULL
)
4729 LOG_ERROR("No image in memory");
4733 int ms
= timeval_ms();
4735 int retval
= ERROR_OK
;
4736 for (i
= 0; i
< fastload_num
;i
++)
4738 target_t
*target
= get_current_target(cmd_ctx
);
4739 command_print(cmd_ctx
, "Write to 0x%08x, length 0x%08x",
4740 (unsigned int)(fastload
[i
].address
),
4741 (unsigned int)(fastload
[i
].length
));
4742 if (retval
== ERROR_OK
)
4744 retval
= target_write_buffer(target
, fastload
[i
].address
, fastload
[i
].length
, fastload
[i
].data
);
4746 size
+= fastload
[i
].length
;
4748 int after
= timeval_ms();
4749 command_print(cmd_ctx
, "Loaded image %f kBytes/s", (float)(size
/1024.0)/((float)(after
-ms
)/1000.0));
4753 static int jim_mcrmrc(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4755 command_context_t
*context
;
4759 context
= Jim_GetAssocData(interp
, "context");
4760 if (context
== NULL
) {
4761 LOG_ERROR("array2mem: no command context");
4764 target
= get_current_target(context
);
4765 if (target
== NULL
) {
4766 LOG_ERROR("array2mem: no current target");
4770 if ((argc
< 6) || (argc
> 7))
4784 e
= Jim_GetLong(interp
, argv
[1], &l
);
4790 e
= Jim_GetLong(interp
, argv
[2], &l
);
4796 e
= Jim_GetLong(interp
, argv
[3], &l
);
4802 e
= Jim_GetLong(interp
, argv
[4], &l
);
4808 e
= Jim_GetLong(interp
, argv
[5], &l
);
4818 e
= Jim_GetLong(interp
, argv
[6], &l
);
4824 retval
= target_mcr(target
, cpnum
, op1
, op2
, CRn
, CRm
, value
);
4825 if (retval
!= ERROR_OK
)
4829 retval
= target_mrc(target
, cpnum
, op1
, op2
, CRn
, CRm
, &value
);
4830 if (retval
!= ERROR_OK
)
4833 Jim_SetResult(interp
, Jim_NewIntObj(interp
, value
));