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
)
499 LOG_ERROR("Not implemented.");
503 static int default_has_mmu(struct target_s
*target
, bool *has_mmu
)
509 static int default_examine(struct target_s
*target
)
511 target_set_examined(target
);
515 int target_examine_one(struct target_s
*target
)
517 return target
->type
->examine(target
);
520 static int jtag_enable_callback(enum jtag_event event
, void *priv
)
522 target_t
*target
= priv
;
524 if (event
!= JTAG_TAP_EVENT_ENABLE
|| !target
->tap
->enabled
)
527 jtag_unregister_event_callback(jtag_enable_callback
, target
);
528 return target_examine_one(target
);
532 /* Targets that correctly implement init + examine, i.e.
533 * no communication with target during init:
537 int target_examine(void)
539 int retval
= ERROR_OK
;
542 for (target
= all_targets
; target
; target
= target
->next
)
544 /* defer examination, but don't skip it */
545 if (!target
->tap
->enabled
) {
546 jtag_register_event_callback(jtag_enable_callback
,
550 if ((retval
= target_examine_one(target
)) != ERROR_OK
)
555 const char *target_get_name(struct target_s
*target
)
557 return target
->type
->name
;
560 static int target_write_memory_imp(struct target_s
*target
, uint32_t address
, uint32_t size
, uint32_t count
, uint8_t *buffer
)
562 if (!target_was_examined(target
))
564 LOG_ERROR("Target not examined yet");
567 return target
->type
->write_memory_imp(target
, address
, size
, count
, buffer
);
570 static int target_read_memory_imp(struct target_s
*target
, uint32_t address
, uint32_t size
, uint32_t count
, uint8_t *buffer
)
572 if (!target_was_examined(target
))
574 LOG_ERROR("Target not examined yet");
577 return target
->type
->read_memory_imp(target
, address
, size
, count
, buffer
);
580 static int target_soft_reset_halt_imp(struct target_s
*target
)
582 if (!target_was_examined(target
))
584 LOG_ERROR("Target not examined yet");
587 if (!target
->type
->soft_reset_halt_imp
) {
588 LOG_ERROR("Target %s does not support soft_reset_halt",
592 return target
->type
->soft_reset_halt_imp(target
);
595 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
)
597 if (!target_was_examined(target
))
599 LOG_ERROR("Target not examined yet");
602 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
);
605 int target_read_memory(struct target_s
*target
,
606 uint32_t address
, uint32_t size
, uint32_t count
, uint8_t *buffer
)
608 return target
->type
->read_memory(target
, address
, size
, count
, buffer
);
611 int target_read_phys_memory(struct target_s
*target
,
612 uint32_t address
, uint32_t size
, uint32_t count
, uint8_t *buffer
)
614 return target
->type
->read_phys_memory(target
, address
, size
, count
, buffer
);
617 int target_write_memory(struct target_s
*target
,
618 uint32_t address
, uint32_t size
, uint32_t count
, uint8_t *buffer
)
620 return target
->type
->write_memory(target
, address
, size
, count
, buffer
);
623 int target_write_phys_memory(struct target_s
*target
,
624 uint32_t address
, uint32_t size
, uint32_t count
, uint8_t *buffer
)
626 return target
->type
->write_phys_memory(target
, address
, size
, count
, buffer
);
629 int target_bulk_write_memory(struct target_s
*target
,
630 uint32_t address
, uint32_t count
, uint8_t *buffer
)
632 return target
->type
->bulk_write_memory(target
, address
, count
, buffer
);
635 int target_add_breakpoint(struct target_s
*target
,
636 struct breakpoint_s
*breakpoint
)
638 return target
->type
->add_breakpoint(target
, breakpoint
);
640 int target_remove_breakpoint(struct target_s
*target
,
641 struct breakpoint_s
*breakpoint
)
643 return target
->type
->remove_breakpoint(target
, breakpoint
);
646 int target_add_watchpoint(struct target_s
*target
,
647 struct watchpoint_s
*watchpoint
)
649 return target
->type
->add_watchpoint(target
, watchpoint
);
651 int target_remove_watchpoint(struct target_s
*target
,
652 struct watchpoint_s
*watchpoint
)
654 return target
->type
->remove_watchpoint(target
, watchpoint
);
657 int target_get_gdb_reg_list(struct target_s
*target
,
658 struct reg_s
**reg_list
[], int *reg_list_size
)
660 return target
->type
->get_gdb_reg_list(target
, reg_list
, reg_list_size
);
662 int target_step(struct target_s
*target
,
663 int current
, uint32_t address
, int handle_breakpoints
)
665 return target
->type
->step(target
, current
, address
, handle_breakpoints
);
669 int target_run_algorithm(struct target_s
*target
,
670 int num_mem_params
, mem_param_t
*mem_params
,
671 int num_reg_params
, reg_param_t
*reg_param
,
672 uint32_t entry_point
, uint32_t exit_point
,
673 int timeout_ms
, void *arch_info
)
675 return target
->type
->run_algorithm(target
,
676 num_mem_params
, mem_params
, num_reg_params
, reg_param
,
677 entry_point
, exit_point
, timeout_ms
, arch_info
);
680 /// @returns @c true if the target has been examined.
681 bool target_was_examined(struct target_s
*target
)
683 return target
->type
->examined
;
685 /// Sets the @c examined flag for the given target.
686 void target_set_examined(struct target_s
*target
)
688 target
->type
->examined
= true;
690 // Reset the @c examined flag for the given target.
691 void target_reset_examined(struct target_s
*target
)
693 target
->type
->examined
= false;
698 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
)
700 LOG_ERROR("Not implemented");
704 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
)
706 LOG_ERROR("Not implemented");
710 static int arm_cp_check(struct target_s
*target
, int cpnum
, uint32_t op1
, uint32_t op2
, uint32_t CRn
, uint32_t CRm
)
713 if (!target_was_examined(target
))
715 LOG_ERROR("Target not examined yet");
719 if ((cpnum
<0) || (cpnum
> 15))
721 LOG_ERROR("Illegal co-processor %d", cpnum
);
727 LOG_ERROR("Illegal op1");
733 LOG_ERROR("Illegal op2");
739 LOG_ERROR("Illegal CRn");
745 LOG_ERROR("Illegal CRm");
752 int target_mrc(struct target_s
*target
, int cpnum
, uint32_t op1
, uint32_t op2
, uint32_t CRn
, uint32_t CRm
, uint32_t *value
)
756 retval
= arm_cp_check(target
, cpnum
, op1
, op2
, CRn
, CRm
);
757 if (retval
!= ERROR_OK
)
760 return target
->type
->mrc(target
, cpnum
, op1
, op2
, CRn
, CRm
, value
);
763 int target_mcr(struct target_s
*target
, int cpnum
, uint32_t op1
, uint32_t op2
, uint32_t CRn
, uint32_t CRm
, uint32_t value
)
767 retval
= arm_cp_check(target
, cpnum
, op1
, op2
, CRn
, CRm
);
768 if (retval
!= ERROR_OK
)
771 return target
->type
->mcr(target
, cpnum
, op1
, op2
, CRn
, CRm
, value
);
774 static int default_read_phys_memory(struct target_s
*target
, uint32_t address
, uint32_t size
, uint32_t count
, uint8_t *buffer
)
778 retval
= target
->type
->has_mmu(target
, &mmu
);
779 if (retval
!= ERROR_OK
)
783 LOG_ERROR("Not implemented");
786 return target_read_memory(target
, address
, size
, count
, buffer
);
789 static int default_write_phys_memory(struct target_s
*target
, uint32_t address
, uint32_t size
, uint32_t count
, uint8_t *buffer
)
793 retval
= target
->type
->has_mmu(target
, &mmu
);
794 if (retval
!= ERROR_OK
)
798 LOG_ERROR("Not implemented");
801 return target_write_memory(target
, address
, size
, count
, buffer
);
805 int target_init(struct command_context_s
*cmd_ctx
)
807 target_t
*target
= all_targets
;
812 target_reset_examined(target
);
813 if (target
->type
->examine
== NULL
)
815 target
->type
->examine
= default_examine
;
818 if ((retval
= target
->type
->init_target(cmd_ctx
, target
)) != ERROR_OK
)
820 LOG_ERROR("target '%s' init failed", target_get_name(target
));
824 /* Set up default functions if none are provided by target */
825 if (target
->type
->virt2phys
== NULL
)
827 target
->type
->virt2phys
= default_virt2phys
;
830 if (target
->type
->read_phys_memory
== NULL
)
832 target
->type
->read_phys_memory
= default_read_phys_memory
;
835 if (target
->type
->write_phys_memory
== NULL
)
837 target
->type
->write_phys_memory
= default_write_phys_memory
;
840 if (target
->type
->mcr
== NULL
)
842 target
->type
->mcr
= default_mcr
;
845 /* FIX! multiple targets will generally register global commands
846 * multiple times. Only register this one if *one* of the
847 * targets need the command. Hmm... make it a command on the
848 * Jim Tcl target object?
850 register_jim(cmd_ctx
, "mcr", jim_mcrmrc
, "write coprocessor <cpnum> <op1> <op2> <CRn> <CRm> <value>");
853 if (target
->type
->mrc
== NULL
)
855 target
->type
->mrc
= default_mrc
;
858 register_jim(cmd_ctx
, "mrc", jim_mcrmrc
, "read coprocessor <cpnum> <op1> <op2> <CRn> <CRm>");
862 /* a non-invasive way(in terms of patches) to add some code that
863 * runs before the type->write/read_memory implementation
865 target
->type
->write_memory_imp
= target
->type
->write_memory
;
866 target
->type
->write_memory
= target_write_memory_imp
;
867 target
->type
->read_memory_imp
= target
->type
->read_memory
;
868 target
->type
->read_memory
= target_read_memory_imp
;
869 target
->type
->soft_reset_halt_imp
= target
->type
->soft_reset_halt
;
870 target
->type
->soft_reset_halt
= target_soft_reset_halt_imp
;
871 target
->type
->run_algorithm_imp
= target
->type
->run_algorithm
;
872 target
->type
->run_algorithm
= target_run_algorithm_imp
;
874 if (target
->type
->mmu
== NULL
)
876 target
->type
->mmu
= default_mmu
;
878 if (target
->type
->has_mmu
== NULL
)
880 target
->type
->has_mmu
= default_has_mmu
;
882 target
= target
->next
;
887 if ((retval
= target_register_user_commands(cmd_ctx
)) != ERROR_OK
)
889 if ((retval
= target_register_timer_callback(handle_target
, 100, 1, NULL
)) != ERROR_OK
)
896 int target_register_event_callback(int (*callback
)(struct target_s
*target
, enum target_event event
, void *priv
), void *priv
)
898 target_event_callback_t
**callbacks_p
= &target_event_callbacks
;
900 if (callback
== NULL
)
902 return ERROR_INVALID_ARGUMENTS
;
907 while ((*callbacks_p
)->next
)
908 callbacks_p
= &((*callbacks_p
)->next
);
909 callbacks_p
= &((*callbacks_p
)->next
);
912 (*callbacks_p
) = malloc(sizeof(target_event_callback_t
));
913 (*callbacks_p
)->callback
= callback
;
914 (*callbacks_p
)->priv
= priv
;
915 (*callbacks_p
)->next
= NULL
;
920 int target_register_timer_callback(int (*callback
)(void *priv
), int time_ms
, int periodic
, void *priv
)
922 target_timer_callback_t
**callbacks_p
= &target_timer_callbacks
;
925 if (callback
== NULL
)
927 return ERROR_INVALID_ARGUMENTS
;
932 while ((*callbacks_p
)->next
)
933 callbacks_p
= &((*callbacks_p
)->next
);
934 callbacks_p
= &((*callbacks_p
)->next
);
937 (*callbacks_p
) = malloc(sizeof(target_timer_callback_t
));
938 (*callbacks_p
)->callback
= callback
;
939 (*callbacks_p
)->periodic
= periodic
;
940 (*callbacks_p
)->time_ms
= time_ms
;
942 gettimeofday(&now
, NULL
);
943 (*callbacks_p
)->when
.tv_usec
= now
.tv_usec
+ (time_ms
% 1000) * 1000;
944 time_ms
-= (time_ms
% 1000);
945 (*callbacks_p
)->when
.tv_sec
= now
.tv_sec
+ (time_ms
/ 1000);
946 if ((*callbacks_p
)->when
.tv_usec
> 1000000)
948 (*callbacks_p
)->when
.tv_usec
= (*callbacks_p
)->when
.tv_usec
- 1000000;
949 (*callbacks_p
)->when
.tv_sec
+= 1;
952 (*callbacks_p
)->priv
= priv
;
953 (*callbacks_p
)->next
= NULL
;
958 int target_unregister_event_callback(int (*callback
)(struct target_s
*target
, enum target_event event
, void *priv
), void *priv
)
960 target_event_callback_t
**p
= &target_event_callbacks
;
961 target_event_callback_t
*c
= target_event_callbacks
;
963 if (callback
== NULL
)
965 return ERROR_INVALID_ARGUMENTS
;
970 target_event_callback_t
*next
= c
->next
;
971 if ((c
->callback
== callback
) && (c
->priv
== priv
))
985 int target_unregister_timer_callback(int (*callback
)(void *priv
), void *priv
)
987 target_timer_callback_t
**p
= &target_timer_callbacks
;
988 target_timer_callback_t
*c
= target_timer_callbacks
;
990 if (callback
== NULL
)
992 return ERROR_INVALID_ARGUMENTS
;
997 target_timer_callback_t
*next
= c
->next
;
998 if ((c
->callback
== callback
) && (c
->priv
== priv
))
1012 int target_call_event_callbacks(target_t
*target
, enum target_event event
)
1014 target_event_callback_t
*callback
= target_event_callbacks
;
1015 target_event_callback_t
*next_callback
;
1017 if (event
== TARGET_EVENT_HALTED
)
1019 /* execute early halted first */
1020 target_call_event_callbacks(target
, TARGET_EVENT_GDB_HALT
);
1023 LOG_DEBUG("target event %i (%s)",
1025 Jim_Nvp_value2name_simple(nvp_target_event
, event
)->name
);
1027 target_handle_event(target
, event
);
1031 next_callback
= callback
->next
;
1032 callback
->callback(target
, event
, callback
->priv
);
1033 callback
= next_callback
;
1039 static int target_timer_callback_periodic_restart(
1040 target_timer_callback_t
*cb
, struct timeval
*now
)
1042 int time_ms
= cb
->time_ms
;
1043 cb
->when
.tv_usec
= now
->tv_usec
+ (time_ms
% 1000) * 1000;
1044 time_ms
-= (time_ms
% 1000);
1045 cb
->when
.tv_sec
= now
->tv_sec
+ time_ms
/ 1000;
1046 if (cb
->when
.tv_usec
> 1000000)
1048 cb
->when
.tv_usec
= cb
->when
.tv_usec
- 1000000;
1049 cb
->when
.tv_sec
+= 1;
1054 static int target_call_timer_callback(target_timer_callback_t
*cb
,
1055 struct timeval
*now
)
1057 cb
->callback(cb
->priv
);
1060 return target_timer_callback_periodic_restart(cb
, now
);
1062 return target_unregister_timer_callback(cb
->callback
, cb
->priv
);
1065 static int target_call_timer_callbacks_check_time(int checktime
)
1070 gettimeofday(&now
, NULL
);
1072 target_timer_callback_t
*callback
= target_timer_callbacks
;
1075 // cleaning up may unregister and free this callback
1076 target_timer_callback_t
*next_callback
= callback
->next
;
1078 bool call_it
= callback
->callback
&&
1079 ((!checktime
&& callback
->periodic
) ||
1080 now
.tv_sec
> callback
->when
.tv_sec
||
1081 (now
.tv_sec
== callback
->when
.tv_sec
&&
1082 now
.tv_usec
>= callback
->when
.tv_usec
));
1086 int retval
= target_call_timer_callback(callback
, &now
);
1087 if (retval
!= ERROR_OK
)
1091 callback
= next_callback
;
1097 int target_call_timer_callbacks(void)
1099 return target_call_timer_callbacks_check_time(1);
1102 /* invoke periodic callbacks immediately */
1103 int target_call_timer_callbacks_now(void)
1105 return target_call_timer_callbacks_check_time(0);
1108 int target_alloc_working_area(struct target_s
*target
, uint32_t size
, working_area_t
**area
)
1110 working_area_t
*c
= target
->working_areas
;
1111 working_area_t
*new_wa
= NULL
;
1113 /* Reevaluate working area address based on MMU state*/
1114 if (target
->working_areas
== NULL
)
1119 retval
= target
->type
->mmu(target
, &enabled
);
1120 if (retval
!= ERROR_OK
)
1126 if (target
->working_area_phys_spec
) {
1127 LOG_DEBUG("MMU disabled, using physical "
1128 "address for working memory 0x%08x",
1129 (unsigned)target
->working_area_phys
);
1130 target
->working_area
= target
->working_area_phys
;
1132 LOG_ERROR("No working memory available. "
1133 "Specify -work-area-phys to target.");
1134 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
1137 if (target
->working_area_virt_spec
) {
1138 LOG_DEBUG("MMU enabled, using virtual "
1139 "address for working memory 0x%08x",
1140 (unsigned)target
->working_area_virt
);
1141 target
->working_area
= target
->working_area_virt
;
1143 LOG_ERROR("No working memory available. "
1144 "Specify -work-area-virt to target.");
1145 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
1150 /* only allocate multiples of 4 byte */
1153 LOG_ERROR("BUG: code tried to allocate unaligned number of bytes (0x%08x), padding", ((unsigned)(size
)));
1154 size
= (size
+ 3) & (~3);
1157 /* see if there's already a matching working area */
1160 if ((c
->free
) && (c
->size
== size
))
1168 /* if not, allocate a new one */
1171 working_area_t
**p
= &target
->working_areas
;
1172 uint32_t first_free
= target
->working_area
;
1173 uint32_t free_size
= target
->working_area_size
;
1175 c
= target
->working_areas
;
1178 first_free
+= c
->size
;
1179 free_size
-= c
->size
;
1184 if (free_size
< size
)
1186 LOG_WARNING("not enough working area available(requested %u, free %u)",
1187 (unsigned)(size
), (unsigned)(free_size
));
1188 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
1191 LOG_DEBUG("allocated new working area at address 0x%08x", (unsigned)first_free
);
1193 new_wa
= malloc(sizeof(working_area_t
));
1194 new_wa
->next
= NULL
;
1195 new_wa
->size
= size
;
1196 new_wa
->address
= first_free
;
1198 if (target
->backup_working_area
)
1201 new_wa
->backup
= malloc(new_wa
->size
);
1202 if ((retval
= target_read_memory(target
, new_wa
->address
, 4, new_wa
->size
/ 4, new_wa
->backup
)) != ERROR_OK
)
1204 free(new_wa
->backup
);
1211 new_wa
->backup
= NULL
;
1214 /* put new entry in list */
1218 /* mark as used, and return the new (reused) area */
1223 new_wa
->user
= area
;
1228 int target_free_working_area_restore(struct target_s
*target
, working_area_t
*area
, int restore
)
1233 if (restore
&& target
->backup_working_area
)
1236 if ((retval
= target_write_memory(target
, area
->address
, 4, area
->size
/ 4, area
->backup
)) != ERROR_OK
)
1242 /* mark user pointer invalid */
1249 int target_free_working_area(struct target_s
*target
, working_area_t
*area
)
1251 return target_free_working_area_restore(target
, area
, 1);
1254 /* free resources and restore memory, if restoring memory fails,
1255 * free up resources anyway
1257 void target_free_all_working_areas_restore(struct target_s
*target
, int restore
)
1259 working_area_t
*c
= target
->working_areas
;
1263 working_area_t
*next
= c
->next
;
1264 target_free_working_area_restore(target
, c
, restore
);
1274 target
->working_areas
= NULL
;
1277 void target_free_all_working_areas(struct target_s
*target
)
1279 target_free_all_working_areas_restore(target
, 1);
1282 int target_register_commands(struct command_context_s
*cmd_ctx
)
1285 register_command(cmd_ctx
, NULL
, "targets", handle_targets_command
, COMMAND_EXEC
, "change the current command line target (one parameter) or lists targets (with no parameter)");
1290 register_jim(cmd_ctx
, "target", jim_target
, "configure target");
1295 int target_arch_state(struct target_s
*target
)
1300 LOG_USER("No target has been configured");
1304 LOG_USER("target state: %s", target_state_name( target
));
1306 if (target
->state
!= TARGET_HALTED
)
1309 retval
= target
->type
->arch_state(target
);
1313 /* Single aligned words are guaranteed to use 16 or 32 bit access
1314 * mode respectively, otherwise data is handled as quickly as
1317 int target_write_buffer(struct target_s
*target
, uint32_t address
, uint32_t size
, uint8_t *buffer
)
1320 LOG_DEBUG("writing buffer of %i byte at 0x%8.8x",
1321 (int)size
, (unsigned)address
);
1323 if (!target_was_examined(target
))
1325 LOG_ERROR("Target not examined yet");
1333 if ((address
+ size
- 1) < address
)
1335 /* GDB can request this when e.g. PC is 0xfffffffc*/
1336 LOG_ERROR("address + size wrapped(0x%08x, 0x%08x)",
1342 if (((address
% 2) == 0) && (size
== 2))
1344 return target_write_memory(target
, address
, 2, 1, buffer
);
1347 /* handle unaligned head bytes */
1350 uint32_t unaligned
= 4 - (address
% 4);
1352 if (unaligned
> size
)
1355 if ((retval
= target_write_memory(target
, address
, 1, unaligned
, buffer
)) != ERROR_OK
)
1358 buffer
+= unaligned
;
1359 address
+= unaligned
;
1363 /* handle aligned words */
1366 int aligned
= size
- (size
% 4);
1368 /* use bulk writes above a certain limit. This may have to be changed */
1371 if ((retval
= target
->type
->bulk_write_memory(target
, address
, aligned
/ 4, buffer
)) != ERROR_OK
)
1376 if ((retval
= target_write_memory(target
, address
, 4, aligned
/ 4, buffer
)) != ERROR_OK
)
1385 /* handle tail writes of less than 4 bytes */
1388 if ((retval
= target_write_memory(target
, address
, 1, size
, buffer
)) != ERROR_OK
)
1395 /* Single aligned words are guaranteed to use 16 or 32 bit access
1396 * mode respectively, otherwise data is handled as quickly as
1399 int target_read_buffer(struct target_s
*target
, uint32_t address
, uint32_t size
, uint8_t *buffer
)
1402 LOG_DEBUG("reading buffer of %i byte at 0x%8.8x",
1403 (int)size
, (unsigned)address
);
1405 if (!target_was_examined(target
))
1407 LOG_ERROR("Target not examined yet");
1415 if ((address
+ size
- 1) < address
)
1417 /* GDB can request this when e.g. PC is 0xfffffffc*/
1418 LOG_ERROR("address + size wrapped(0x%08" PRIx32
", 0x%08" PRIx32
")",
1424 if (((address
% 2) == 0) && (size
== 2))
1426 return target_read_memory(target
, address
, 2, 1, buffer
);
1429 /* handle unaligned head bytes */
1432 uint32_t unaligned
= 4 - (address
% 4);
1434 if (unaligned
> size
)
1437 if ((retval
= target_read_memory(target
, address
, 1, unaligned
, buffer
)) != ERROR_OK
)
1440 buffer
+= unaligned
;
1441 address
+= unaligned
;
1445 /* handle aligned words */
1448 int aligned
= size
- (size
% 4);
1450 if ((retval
= target_read_memory(target
, address
, 4, aligned
/ 4, buffer
)) != ERROR_OK
)
1458 /*prevent byte access when possible (avoid AHB access limitations in some cases)*/
1461 int aligned
= size
- (size
%2);
1462 retval
= target_read_memory(target
, address
, 2, aligned
/ 2, buffer
);
1463 if (retval
!= ERROR_OK
)
1470 /* handle tail writes of less than 4 bytes */
1473 if ((retval
= target_read_memory(target
, address
, 1, size
, buffer
)) != ERROR_OK
)
1480 int target_checksum_memory(struct target_s
*target
, uint32_t address
, uint32_t size
, uint32_t* crc
)
1485 uint32_t checksum
= 0;
1486 if (!target_was_examined(target
))
1488 LOG_ERROR("Target not examined yet");
1492 if ((retval
= target
->type
->checksum_memory(target
, address
,
1493 size
, &checksum
)) != ERROR_OK
)
1495 buffer
= malloc(size
);
1498 LOG_ERROR("error allocating buffer for section (%d bytes)", (int)size
);
1499 return ERROR_INVALID_ARGUMENTS
;
1501 retval
= target_read_buffer(target
, address
, size
, buffer
);
1502 if (retval
!= ERROR_OK
)
1508 /* convert to target endianess */
1509 for (i
= 0; i
< (size
/sizeof(uint32_t)); i
++)
1511 uint32_t target_data
;
1512 target_data
= target_buffer_get_u32(target
, &buffer
[i
*sizeof(uint32_t)]);
1513 target_buffer_set_u32(target
, &buffer
[i
*sizeof(uint32_t)], target_data
);
1516 retval
= image_calculate_checksum(buffer
, size
, &checksum
);
1525 int target_blank_check_memory(struct target_s
*target
, uint32_t address
, uint32_t size
, uint32_t* blank
)
1528 if (!target_was_examined(target
))
1530 LOG_ERROR("Target not examined yet");
1534 if (target
->type
->blank_check_memory
== 0)
1535 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
1537 retval
= target
->type
->blank_check_memory(target
, address
, size
, blank
);
1542 int target_read_u32(struct target_s
*target
, uint32_t address
, uint32_t *value
)
1544 uint8_t value_buf
[4];
1545 if (!target_was_examined(target
))
1547 LOG_ERROR("Target not examined yet");
1551 int retval
= target_read_memory(target
, address
, 4, 1, value_buf
);
1553 if (retval
== ERROR_OK
)
1555 *value
= target_buffer_get_u32(target
, value_buf
);
1556 LOG_DEBUG("address: 0x%8.8" PRIx32
", value: 0x%8.8" PRIx32
"",
1563 LOG_DEBUG("address: 0x%8.8" PRIx32
" failed",
1570 int target_read_u16(struct target_s
*target
, uint32_t address
, uint16_t *value
)
1572 uint8_t value_buf
[2];
1573 if (!target_was_examined(target
))
1575 LOG_ERROR("Target not examined yet");
1579 int retval
= target_read_memory(target
, address
, 2, 1, value_buf
);
1581 if (retval
== ERROR_OK
)
1583 *value
= target_buffer_get_u16(target
, value_buf
);
1584 LOG_DEBUG("address: 0x%8.8" PRIx32
", value: 0x%4.4x",
1591 LOG_DEBUG("address: 0x%8.8" PRIx32
" failed",
1598 int target_read_u8(struct target_s
*target
, uint32_t address
, uint8_t *value
)
1600 int retval
= target_read_memory(target
, address
, 1, 1, value
);
1601 if (!target_was_examined(target
))
1603 LOG_ERROR("Target not examined yet");
1607 if (retval
== ERROR_OK
)
1609 LOG_DEBUG("address: 0x%8.8" PRIx32
", value: 0x%2.2x",
1616 LOG_DEBUG("address: 0x%8.8" PRIx32
" failed",
1623 int target_write_u32(struct target_s
*target
, uint32_t address
, uint32_t value
)
1626 uint8_t value_buf
[4];
1627 if (!target_was_examined(target
))
1629 LOG_ERROR("Target not examined yet");
1633 LOG_DEBUG("address: 0x%8.8" PRIx32
", value: 0x%8.8" PRIx32
"",
1637 target_buffer_set_u32(target
, value_buf
, value
);
1638 if ((retval
= target_write_memory(target
, address
, 4, 1, value_buf
)) != ERROR_OK
)
1640 LOG_DEBUG("failed: %i", retval
);
1646 int target_write_u16(struct target_s
*target
, uint32_t address
, uint16_t value
)
1649 uint8_t value_buf
[2];
1650 if (!target_was_examined(target
))
1652 LOG_ERROR("Target not examined yet");
1656 LOG_DEBUG("address: 0x%8.8" PRIx32
", value: 0x%8.8x",
1660 target_buffer_set_u16(target
, value_buf
, value
);
1661 if ((retval
= target_write_memory(target
, address
, 2, 1, value_buf
)) != ERROR_OK
)
1663 LOG_DEBUG("failed: %i", retval
);
1669 int target_write_u8(struct target_s
*target
, uint32_t address
, uint8_t value
)
1672 if (!target_was_examined(target
))
1674 LOG_ERROR("Target not examined yet");
1678 LOG_DEBUG("address: 0x%8.8" PRIx32
", value: 0x%2.2x",
1681 if ((retval
= target_write_memory(target
, address
, 1, 1, &value
)) != ERROR_OK
)
1683 LOG_DEBUG("failed: %i", retval
);
1689 int target_register_user_commands(struct command_context_s
*cmd_ctx
)
1691 int retval
= ERROR_OK
;
1694 /* script procedures */
1695 register_command(cmd_ctx
, NULL
, "profile", handle_profile_command
, COMMAND_EXEC
, "profiling samples the CPU PC");
1696 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>");
1697 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>");
1699 register_command(cmd_ctx
, NULL
, "fast_load_image", handle_fast_load_image_command
, COMMAND_ANY
,
1700 "same args as load_image, image stored in memory - mainly for profiling purposes");
1702 register_command(cmd_ctx
, NULL
, "fast_load", handle_fast_load_command
, COMMAND_ANY
,
1703 "loads active fast load image to current target - mainly for profiling purposes");
1706 register_command(cmd_ctx
, NULL
, "virt2phys", handle_virt2phys_command
, COMMAND_ANY
, "translate a virtual address into a physical address");
1707 register_command(cmd_ctx
, NULL
, "reg", handle_reg_command
, COMMAND_EXEC
, "display or set a register");
1708 register_command(cmd_ctx
, NULL
, "poll", handle_poll_command
, COMMAND_EXEC
, "poll target state");
1709 register_command(cmd_ctx
, NULL
, "wait_halt", handle_wait_halt_command
, COMMAND_EXEC
, "wait for target halt [time (s)]");
1710 register_command(cmd_ctx
, NULL
, "halt", handle_halt_command
, COMMAND_EXEC
, "halt target");
1711 register_command(cmd_ctx
, NULL
, "resume", handle_resume_command
, COMMAND_EXEC
, "resume target [addr]");
1712 register_command(cmd_ctx
, NULL
, "step", handle_step_command
, COMMAND_EXEC
, "step one instruction from current PC or [addr]");
1713 register_command(cmd_ctx
, NULL
, "reset", handle_reset_command
, COMMAND_EXEC
, "reset target [run | halt | init] - default is run");
1714 register_command(cmd_ctx
, NULL
, "soft_reset_halt", handle_soft_reset_halt_command
, COMMAND_EXEC
, "halt the target and do a soft reset");
1716 register_command(cmd_ctx
, NULL
, "mdw", handle_md_command
, COMMAND_EXEC
, "display memory words [phys] <addr> [count]");
1717 register_command(cmd_ctx
, NULL
, "mdh", handle_md_command
, COMMAND_EXEC
, "display memory half-words [phys] <addr> [count]");
1718 register_command(cmd_ctx
, NULL
, "mdb", handle_md_command
, COMMAND_EXEC
, "display memory bytes [phys] <addr> [count]");
1720 register_command(cmd_ctx
, NULL
, "mww", handle_mw_command
, COMMAND_EXEC
, "write memory word [phys] <addr> <value> [count]");
1721 register_command(cmd_ctx
, NULL
, "mwh", handle_mw_command
, COMMAND_EXEC
, "write memory half-word [phys] <addr> <value> [count]");
1722 register_command(cmd_ctx
, NULL
, "mwb", handle_mw_command
, COMMAND_EXEC
, "write memory byte [phys] <addr> <value> [count]");
1724 register_command(cmd_ctx
, NULL
, "bp",
1725 handle_bp_command
, COMMAND_EXEC
,
1726 "list or set breakpoint [<address> <length> [hw]]");
1727 register_command(cmd_ctx
, NULL
, "rbp",
1728 handle_rbp_command
, COMMAND_EXEC
,
1729 "remove breakpoint <address>");
1730 register_command(cmd_ctx
, NULL
, "wp",
1731 handle_wp_command
, COMMAND_EXEC
,
1732 "list or set watchpoint "
1733 "[<address> <length> <r/w/a> [value] [mask]]");
1734 register_command(cmd_ctx
, NULL
, "rwp",
1735 handle_rwp_command
, COMMAND_EXEC
,
1736 "remove watchpoint <address>");
1738 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]");
1739 register_command(cmd_ctx
, NULL
, "dump_image", handle_dump_image_command
, COMMAND_EXEC
, "dump_image <file> <address> <size>");
1740 register_command(cmd_ctx
, NULL
, "verify_image", handle_verify_image_command
, COMMAND_EXEC
, "verify_image <file> [offset] [type]");
1741 register_command(cmd_ctx
, NULL
, "test_image", handle_test_image_command
, COMMAND_EXEC
, "test_image <file> [offset] [type]");
1743 if ((retval
= target_request_register_commands(cmd_ctx
)) != ERROR_OK
)
1745 if ((retval
= trace_register_commands(cmd_ctx
)) != ERROR_OK
)
1751 static int handle_targets_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
1753 target_t
*target
= all_targets
;
1757 target
= get_target(args
[0]);
1758 if (target
== NULL
) {
1759 command_print(cmd_ctx
,"Target: %s is unknown, try one of:\n", args
[0]);
1762 if (!target
->tap
->enabled
) {
1763 command_print(cmd_ctx
,"Target: TAP %s is disabled, "
1764 "can't be the current target\n",
1765 target
->tap
->dotted_name
);
1769 cmd_ctx
->current_target
= target
->target_number
;
1774 target
= all_targets
;
1775 command_print(cmd_ctx
, " TargetName Type Endian TapName State ");
1776 command_print(cmd_ctx
, "-- ------------------ ---------- ------ ------------------ ------------");
1782 if (target
->tap
->enabled
)
1783 state
= target_state_name( target
);
1785 state
= "tap-disabled";
1787 if (cmd_ctx
->current_target
== target
->target_number
)
1790 /* keep columns lined up to match the headers above */
1791 command_print(cmd_ctx
, "%2d%c %-18s %-10s %-6s %-18s %s",
1792 target
->target_number
,
1795 target_get_name(target
),
1796 Jim_Nvp_value2name_simple(nvp_target_endian
,
1797 target
->endianness
)->name
,
1798 target
->tap
->dotted_name
,
1800 target
= target
->next
;
1806 /* every 300ms we check for reset & powerdropout and issue a "reset halt" if so. */
1808 static int powerDropout
;
1809 static int srstAsserted
;
1811 static int runPowerRestore
;
1812 static int runPowerDropout
;
1813 static int runSrstAsserted
;
1814 static int runSrstDeasserted
;
1816 static int sense_handler(void)
1818 static int prevSrstAsserted
= 0;
1819 static int prevPowerdropout
= 0;
1822 if ((retval
= jtag_power_dropout(&powerDropout
)) != ERROR_OK
)
1826 powerRestored
= prevPowerdropout
&& !powerDropout
;
1829 runPowerRestore
= 1;
1832 long long current
= timeval_ms();
1833 static long long lastPower
= 0;
1834 int waitMore
= lastPower
+ 2000 > current
;
1835 if (powerDropout
&& !waitMore
)
1837 runPowerDropout
= 1;
1838 lastPower
= current
;
1841 if ((retval
= jtag_srst_asserted(&srstAsserted
)) != ERROR_OK
)
1845 srstDeasserted
= prevSrstAsserted
&& !srstAsserted
;
1847 static long long lastSrst
= 0;
1848 waitMore
= lastSrst
+ 2000 > current
;
1849 if (srstDeasserted
&& !waitMore
)
1851 runSrstDeasserted
= 1;
1855 if (!prevSrstAsserted
&& srstAsserted
)
1857 runSrstAsserted
= 1;
1860 prevSrstAsserted
= srstAsserted
;
1861 prevPowerdropout
= powerDropout
;
1863 if (srstDeasserted
|| powerRestored
)
1865 /* Other than logging the event we can't do anything here.
1866 * Issuing a reset is a particularly bad idea as we might
1867 * be inside a reset already.
1874 static void target_call_event_callbacks_all(enum target_event e
) {
1876 target
= all_targets
;
1878 target_call_event_callbacks(target
, e
);
1879 target
= target
->next
;
1883 /* process target state changes */
1884 int handle_target(void *priv
)
1886 int retval
= ERROR_OK
;
1888 /* we do not want to recurse here... */
1889 static int recursive
= 0;
1894 /* danger! running these procedures can trigger srst assertions and power dropouts.
1895 * We need to avoid an infinite loop/recursion here and we do that by
1896 * clearing the flags after running these events.
1898 int did_something
= 0;
1899 if (runSrstAsserted
)
1901 target_call_event_callbacks_all(TARGET_EVENT_GDB_HALT
);
1902 Jim_Eval(interp
, "srst_asserted");
1905 if (runSrstDeasserted
)
1907 Jim_Eval(interp
, "srst_deasserted");
1910 if (runPowerDropout
)
1912 target_call_event_callbacks_all(TARGET_EVENT_GDB_HALT
);
1913 Jim_Eval(interp
, "power_dropout");
1916 if (runPowerRestore
)
1918 Jim_Eval(interp
, "power_restore");
1924 /* clear detect flags */
1928 /* clear action flags */
1930 runSrstAsserted
= 0;
1931 runSrstDeasserted
= 0;
1932 runPowerRestore
= 0;
1933 runPowerDropout
= 0;
1938 /* Poll targets for state changes unless that's globally disabled.
1939 * Skip targets that are currently disabled.
1941 for (target_t
*target
= all_targets
;
1942 is_jtag_poll_safe() && target
;
1943 target
= target
->next
)
1945 if (!target
->tap
->enabled
)
1948 /* only poll target if we've got power and srst isn't asserted */
1949 if (!powerDropout
&& !srstAsserted
)
1951 /* polling may fail silently until the target has been examined */
1952 if ((retval
= target_poll(target
)) != ERROR_OK
)
1954 target_call_event_callbacks(target
, TARGET_EVENT_GDB_HALT
);
1963 static int handle_reg_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
1972 target
= get_current_target(cmd_ctx
);
1974 /* list all available registers for the current target */
1977 reg_cache_t
*cache
= target
->reg_cache
;
1984 command_print(cmd_ctx
, "===== %s", cache
->name
);
1986 for (i
= 0, reg
= cache
->reg_list
;
1987 i
< cache
->num_regs
;
1988 i
++, reg
++, count
++)
1990 /* only print cached values if they are valid */
1992 value
= buf_to_str(reg
->value
,
1994 command_print(cmd_ctx
,
1995 "(%i) %s (/%" PRIu32
"): 0x%s%s",
2003 command_print(cmd_ctx
, "(%i) %s (/%" PRIu32
")",
2008 cache
= cache
->next
;
2014 /* access a single register by its ordinal number */
2015 if ((args
[0][0] >= '0') && (args
[0][0] <= '9'))
2018 COMMAND_PARSE_NUMBER(uint
, args
[0], num
);
2020 reg_cache_t
*cache
= target
->reg_cache
;
2025 for (i
= 0; i
< cache
->num_regs
; i
++)
2027 if (count
++ == (int)num
)
2029 reg
= &cache
->reg_list
[i
];
2035 cache
= cache
->next
;
2040 command_print(cmd_ctx
, "%i is out of bounds, the current target has only %i registers (0 - %i)", num
, count
, count
- 1);
2043 } else /* access a single register by its name */
2045 reg
= register_get_by_name(target
->reg_cache
, args
[0], 1);
2049 command_print(cmd_ctx
, "register %s not found in current target", args
[0]);
2054 /* display a register */
2055 if ((argc
== 1) || ((argc
== 2) && !((args
[1][0] >= '0') && (args
[1][0] <= '9'))))
2057 if ((argc
== 2) && (strcmp(args
[1], "force") == 0))
2060 if (reg
->valid
== 0)
2062 reg_arch_type_t
*arch_type
= register_get_arch_type(reg
->arch_type
);
2063 arch_type
->get(reg
);
2065 value
= buf_to_str(reg
->value
, reg
->size
, 16);
2066 command_print(cmd_ctx
, "%s (/%i): 0x%s", reg
->name
, (int)(reg
->size
), value
);
2071 /* set register value */
2074 uint8_t *buf
= malloc(CEIL(reg
->size
, 8));
2075 str_to_buf(args
[1], strlen(args
[1]), buf
, reg
->size
, 0);
2077 reg_arch_type_t
*arch_type
= register_get_arch_type(reg
->arch_type
);
2078 arch_type
->set(reg
, buf
);
2080 value
= buf_to_str(reg
->value
, reg
->size
, 16);
2081 command_print(cmd_ctx
, "%s (/%i): 0x%s", reg
->name
, (int)(reg
->size
), value
);
2089 command_print(cmd_ctx
, "usage: reg <#|name> [value]");
2094 static int handle_poll_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
2096 int retval
= ERROR_OK
;
2097 target_t
*target
= get_current_target(cmd_ctx
);
2101 command_print(cmd_ctx
, "background polling: %s",
2102 jtag_poll_get_enabled() ? "on" : "off");
2103 command_print(cmd_ctx
, "TAP: %s (%s)",
2104 target
->tap
->dotted_name
,
2105 target
->tap
->enabled
? "enabled" : "disabled");
2106 if (!target
->tap
->enabled
)
2108 if ((retval
= target_poll(target
)) != ERROR_OK
)
2110 if ((retval
= target_arch_state(target
)) != ERROR_OK
)
2116 if (strcmp(args
[0], "on") == 0)
2118 jtag_poll_set_enabled(true);
2120 else if (strcmp(args
[0], "off") == 0)
2122 jtag_poll_set_enabled(false);
2126 command_print(cmd_ctx
, "arg is \"on\" or \"off\"");
2130 return ERROR_COMMAND_SYNTAX_ERROR
;
2136 static int handle_wait_halt_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
2139 return ERROR_COMMAND_SYNTAX_ERROR
;
2144 int retval
= parse_uint(args
[0], &ms
);
2145 if (ERROR_OK
!= retval
)
2147 command_print(cmd_ctx
, "usage: %s [seconds]", cmd
);
2148 return ERROR_COMMAND_SYNTAX_ERROR
;
2150 // convert seconds (given) to milliseconds (needed)
2154 target_t
*target
= get_current_target(cmd_ctx
);
2155 return target_wait_state(target
, TARGET_HALTED
, ms
);
2158 /* wait for target state to change. The trick here is to have a low
2159 * latency for short waits and not to suck up all the CPU time
2162 * After 500ms, keep_alive() is invoked
2164 int target_wait_state(target_t
*target
, enum target_state state
, int ms
)
2167 long long then
= 0, cur
;
2172 if ((retval
= target_poll(target
)) != ERROR_OK
)
2174 if (target
->state
== state
)
2182 then
= timeval_ms();
2183 LOG_DEBUG("waiting for target %s...",
2184 Jim_Nvp_value2name_simple(nvp_target_state
,state
)->name
);
2192 if ((cur
-then
) > ms
)
2194 LOG_ERROR("timed out while waiting for target %s",
2195 Jim_Nvp_value2name_simple(nvp_target_state
,state
)->name
);
2203 static int handle_halt_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
2207 target_t
*target
= get_current_target(cmd_ctx
);
2208 int retval
= target_halt(target
);
2209 if (ERROR_OK
!= retval
)
2215 retval
= parse_uint(args
[0], &wait
);
2216 if (ERROR_OK
!= retval
)
2217 return ERROR_COMMAND_SYNTAX_ERROR
;
2222 return handle_wait_halt_command(cmd_ctx
, cmd
, args
, argc
);
2225 static int handle_soft_reset_halt_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
2227 target_t
*target
= get_current_target(cmd_ctx
);
2229 LOG_USER("requesting target halt and executing a soft reset");
2231 target
->type
->soft_reset_halt(target
);
2236 static int handle_reset_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
2239 return ERROR_COMMAND_SYNTAX_ERROR
;
2241 enum target_reset_mode reset_mode
= RESET_RUN
;
2245 n
= Jim_Nvp_name2value_simple(nvp_reset_modes
, args
[0]);
2246 if ((n
->name
== NULL
) || (n
->value
== RESET_UNKNOWN
)) {
2247 return ERROR_COMMAND_SYNTAX_ERROR
;
2249 reset_mode
= n
->value
;
2252 /* reset *all* targets */
2253 return target_process_reset(cmd_ctx
, reset_mode
);
2257 static int handle_resume_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
2261 return ERROR_COMMAND_SYNTAX_ERROR
;
2263 target_t
*target
= get_current_target(cmd_ctx
);
2264 target_handle_event(target
, TARGET_EVENT_OLD_pre_resume
);
2266 /* with no args, resume from current pc, addr = 0,
2267 * with one arguments, addr = args[0],
2268 * handle breakpoints, not debugging */
2272 COMMAND_PARSE_NUMBER(u32
, args
[0], addr
);
2276 return target_resume(target
, current
, addr
, 1, 0);
2279 static int handle_step_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
2282 return ERROR_COMMAND_SYNTAX_ERROR
;
2286 /* with no args, step from current pc, addr = 0,
2287 * with one argument addr = args[0],
2288 * handle breakpoints, debugging */
2293 COMMAND_PARSE_NUMBER(u32
, args
[0], addr
);
2297 target_t
*target
= get_current_target(cmd_ctx
);
2299 return target
->type
->step(target
, current_pc
, addr
, 1);
2302 static void handle_md_output(struct command_context_s
*cmd_ctx
,
2303 struct target_s
*target
, uint32_t address
, unsigned size
,
2304 unsigned count
, const uint8_t *buffer
)
2306 const unsigned line_bytecnt
= 32;
2307 unsigned line_modulo
= line_bytecnt
/ size
;
2309 char output
[line_bytecnt
* 4 + 1];
2310 unsigned output_len
= 0;
2312 const char *value_fmt
;
2314 case 4: value_fmt
= "%8.8x "; break;
2315 case 2: value_fmt
= "%4.2x "; break;
2316 case 1: value_fmt
= "%2.2x "; break;
2318 LOG_ERROR("invalid memory read size: %u", size
);
2322 for (unsigned i
= 0; i
< count
; i
++)
2324 if (i
% line_modulo
== 0)
2326 output_len
+= snprintf(output
+ output_len
,
2327 sizeof(output
) - output_len
,
2329 (unsigned)(address
+ (i
*size
)));
2333 const uint8_t *value_ptr
= buffer
+ i
* size
;
2335 case 4: value
= target_buffer_get_u32(target
, value_ptr
); break;
2336 case 2: value
= target_buffer_get_u16(target
, value_ptr
); break;
2337 case 1: value
= *value_ptr
;
2339 output_len
+= snprintf(output
+ output_len
,
2340 sizeof(output
) - output_len
,
2343 if ((i
% line_modulo
== line_modulo
- 1) || (i
== count
- 1))
2345 command_print(cmd_ctx
, "%s", output
);
2351 static int handle_md_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
2354 return ERROR_COMMAND_SYNTAX_ERROR
;
2358 case 'w': size
= 4; break;
2359 case 'h': size
= 2; break;
2360 case 'b': size
= 1; break;
2361 default: return ERROR_COMMAND_SYNTAX_ERROR
;
2364 bool physical
=strcmp(args
[0], "phys")==0;
2365 int (*fn
)(struct target_s
*target
,
2366 uint32_t address
, uint32_t size
, uint32_t count
, uint8_t *buffer
);
2371 fn
=target_read_phys_memory
;
2374 fn
=target_read_memory
;
2376 if ((argc
< 1) || (argc
> 2))
2378 return ERROR_COMMAND_SYNTAX_ERROR
;
2382 COMMAND_PARSE_NUMBER(u32
, args
[0], address
);
2386 COMMAND_PARSE_NUMBER(uint
, args
[1], count
);
2388 uint8_t *buffer
= calloc(count
, size
);
2390 target_t
*target
= get_current_target(cmd_ctx
);
2391 int retval
= fn(target
, address
, size
, count
, buffer
);
2392 if (ERROR_OK
== retval
)
2393 handle_md_output(cmd_ctx
, target
, address
, size
, count
, buffer
);
2400 static int handle_mw_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
2404 return ERROR_COMMAND_SYNTAX_ERROR
;
2406 bool physical
=strcmp(args
[0], "phys")==0;
2407 int (*fn
)(struct target_s
*target
,
2408 uint32_t address
, uint32_t size
, uint32_t count
, uint8_t *buffer
);
2413 fn
=target_write_phys_memory
;
2416 fn
=target_write_memory
;
2418 if ((argc
< 2) || (argc
> 3))
2419 return ERROR_COMMAND_SYNTAX_ERROR
;
2422 COMMAND_PARSE_NUMBER(u32
, args
[0], address
);
2425 COMMAND_PARSE_NUMBER(u32
, args
[1], value
);
2429 COMMAND_PARSE_NUMBER(uint
, args
[2], count
);
2431 target_t
*target
= get_current_target(cmd_ctx
);
2433 uint8_t value_buf
[4];
2438 target_buffer_set_u32(target
, value_buf
, value
);
2442 target_buffer_set_u16(target
, value_buf
, value
);
2446 value_buf
[0] = value
;
2449 return ERROR_COMMAND_SYNTAX_ERROR
;
2451 for (unsigned i
= 0; i
< count
; i
++)
2453 int retval
= fn(target
,
2454 address
+ i
* wordsize
, wordsize
, 1, value_buf
);
2455 if (ERROR_OK
!= retval
)
2464 static int parse_load_image_command_args(struct command_context_s
*cmd_ctx
,
2465 char **args
, int argc
, image_t
*image
,
2466 uint32_t *min_address
, uint32_t *max_address
)
2468 if (argc
< 1 || argc
> 5)
2469 return ERROR_COMMAND_SYNTAX_ERROR
;
2471 /* a base address isn't always necessary,
2472 * default to 0x0 (i.e. don't relocate) */
2476 COMMAND_PARSE_NUMBER(u32
, args
[1], addr
);
2477 image
->base_address
= addr
;
2478 image
->base_address_set
= 1;
2481 image
->base_address_set
= 0;
2483 image
->start_address_set
= 0;
2487 COMMAND_PARSE_NUMBER(u32
, args
[3], *min_address
);
2491 COMMAND_PARSE_NUMBER(u32
, args
[4], *max_address
);
2492 // use size (given) to find max (required)
2493 *max_address
+= *min_address
;
2496 if (*min_address
> *max_address
)
2497 return ERROR_COMMAND_SYNTAX_ERROR
;
2502 static int handle_load_image_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
2506 uint32_t image_size
;
2507 uint32_t min_address
= 0;
2508 uint32_t max_address
= 0xffffffff;
2514 duration_t duration
;
2515 char *duration_text
;
2517 int retval
= parse_load_image_command_args(cmd_ctx
, args
, argc
,
2518 &image
, &min_address
, &max_address
);
2519 if (ERROR_OK
!= retval
)
2522 target_t
*target
= get_current_target(cmd_ctx
);
2523 duration_start_measure(&duration
);
2525 if (image_open(&image
, args
[0], (argc
>= 3) ? args
[2] : NULL
) != ERROR_OK
)
2532 for (i
= 0; i
< image
.num_sections
; i
++)
2534 buffer
= malloc(image
.sections
[i
].size
);
2537 command_print(cmd_ctx
,
2538 "error allocating buffer for section (%d bytes)",
2539 (int)(image
.sections
[i
].size
));
2543 if ((retval
= image_read_section(&image
, i
, 0x0, image
.sections
[i
].size
, buffer
, &buf_cnt
)) != ERROR_OK
)
2549 uint32_t offset
= 0;
2550 uint32_t length
= buf_cnt
;
2552 /* DANGER!!! beware of unsigned comparision here!!! */
2554 if ((image
.sections
[i
].base_address
+ buf_cnt
>= min_address
)&&
2555 (image
.sections
[i
].base_address
< max_address
))
2557 if (image
.sections
[i
].base_address
< min_address
)
2559 /* clip addresses below */
2560 offset
+= min_address
-image
.sections
[i
].base_address
;
2564 if (image
.sections
[i
].base_address
+ buf_cnt
> max_address
)
2566 length
-= (image
.sections
[i
].base_address
+ buf_cnt
)-max_address
;
2569 if ((retval
= target_write_buffer(target
, image
.sections
[i
].base_address
+ offset
, length
, buffer
+ offset
)) != ERROR_OK
)
2574 image_size
+= length
;
2575 command_print(cmd_ctx
, "%u bytes written at address 0x%8.8" PRIx32
"",
2576 (unsigned int)length
,
2577 image
.sections
[i
].base_address
+ offset
);
2583 if ((retvaltemp
= duration_stop_measure(&duration
, &duration_text
)) != ERROR_OK
)
2585 image_close(&image
);
2589 if (retval
== ERROR_OK
)
2591 command_print(cmd_ctx
, "downloaded %u byte in %s",
2592 (unsigned int)image_size
,
2595 free(duration_text
);
2597 image_close(&image
);
2603 static int handle_dump_image_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
2607 uint8_t buffer
[560];
2610 duration_t duration
;
2611 char *duration_text
;
2613 target_t
*target
= get_current_target(cmd_ctx
);
2617 command_print(cmd_ctx
, "usage: dump_image <filename> <address> <size>");
2622 COMMAND_PARSE_NUMBER(u32
, args
[1], address
);
2624 COMMAND_PARSE_NUMBER(u32
, args
[2], size
);
2626 if (fileio_open(&fileio
, args
[0], FILEIO_WRITE
, FILEIO_BINARY
) != ERROR_OK
)
2631 duration_start_measure(&duration
);
2633 int retval
= ERROR_OK
;
2636 uint32_t size_written
;
2637 uint32_t this_run_size
= (size
> 560) ? 560 : size
;
2638 retval
= target_read_buffer(target
, address
, this_run_size
, buffer
);
2639 if (retval
!= ERROR_OK
)
2644 retval
= fileio_write(&fileio
, this_run_size
, buffer
, &size_written
);
2645 if (retval
!= ERROR_OK
)
2650 size
-= this_run_size
;
2651 address
+= this_run_size
;
2654 if ((retvaltemp
= fileio_close(&fileio
)) != ERROR_OK
)
2657 if ((retvaltemp
= duration_stop_measure(&duration
, &duration_text
)) != ERROR_OK
)
2660 if (retval
== ERROR_OK
)
2662 command_print(cmd_ctx
, "dumped %lld byte in %s",
2663 fileio
.size
, duration_text
);
2664 free(duration_text
);
2670 static int handle_verify_image_command_internal(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
, int verify
)
2674 uint32_t image_size
;
2676 int retval
, retvaltemp
;
2677 uint32_t checksum
= 0;
2678 uint32_t mem_checksum
= 0;
2682 duration_t duration
;
2683 char *duration_text
;
2685 target_t
*target
= get_current_target(cmd_ctx
);
2689 return ERROR_COMMAND_SYNTAX_ERROR
;
2694 LOG_ERROR("no target selected");
2698 duration_start_measure(&duration
);
2703 COMMAND_PARSE_NUMBER(u32
, args
[1], addr
);
2704 image
.base_address
= addr
;
2705 image
.base_address_set
= 1;
2709 image
.base_address_set
= 0;
2710 image
.base_address
= 0x0;
2713 image
.start_address_set
= 0;
2715 if ((retval
= image_open(&image
, args
[0], (argc
== 3) ? args
[2] : NULL
)) != ERROR_OK
)
2722 for (i
= 0; i
< image
.num_sections
; i
++)
2724 buffer
= malloc(image
.sections
[i
].size
);
2727 command_print(cmd_ctx
,
2728 "error allocating buffer for section (%d bytes)",
2729 (int)(image
.sections
[i
].size
));
2732 if ((retval
= image_read_section(&image
, i
, 0x0, image
.sections
[i
].size
, buffer
, &buf_cnt
)) != ERROR_OK
)
2740 /* calculate checksum of image */
2741 image_calculate_checksum(buffer
, buf_cnt
, &checksum
);
2743 retval
= target_checksum_memory(target
, image
.sections
[i
].base_address
, buf_cnt
, &mem_checksum
);
2744 if (retval
!= ERROR_OK
)
2750 if (checksum
!= mem_checksum
)
2752 /* failed crc checksum, fall back to a binary compare */
2755 command_print(cmd_ctx
, "checksum mismatch - attempting binary compare");
2757 data
= (uint8_t*)malloc(buf_cnt
);
2759 /* Can we use 32bit word accesses? */
2761 int count
= buf_cnt
;
2762 if ((count
% 4) == 0)
2767 retval
= target_read_memory(target
, image
.sections
[i
].base_address
, size
, count
, data
);
2768 if (retval
== ERROR_OK
)
2771 for (t
= 0; t
< buf_cnt
; t
++)
2773 if (data
[t
] != buffer
[t
])
2775 command_print(cmd_ctx
,
2776 "Verify operation failed address 0x%08x. Was 0x%02x instead of 0x%02x\n",
2777 (unsigned)(t
+ image
.sections
[i
].base_address
),
2782 retval
= ERROR_FAIL
;
2796 command_print(cmd_ctx
, "address 0x%08" PRIx32
" length 0x%08" PRIx32
"",
2797 image
.sections
[i
].base_address
,
2802 image_size
+= buf_cnt
;
2806 if ((retvaltemp
= duration_stop_measure(&duration
, &duration_text
)) != ERROR_OK
)
2808 image_close(&image
);
2812 if (retval
== ERROR_OK
)
2814 command_print(cmd_ctx
, "verified %u bytes in %s",
2815 (unsigned int)image_size
,
2818 free(duration_text
);
2820 image_close(&image
);
2825 static int handle_verify_image_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
2827 return handle_verify_image_command_internal(cmd_ctx
, cmd
, args
, argc
, 1);
2830 static int handle_test_image_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
2832 return handle_verify_image_command_internal(cmd_ctx
, cmd
, args
, argc
, 0);
2835 static int handle_bp_command_list(struct command_context_s
*cmd_ctx
)
2837 target_t
*target
= get_current_target(cmd_ctx
);
2838 breakpoint_t
*breakpoint
= target
->breakpoints
;
2841 if (breakpoint
->type
== BKPT_SOFT
)
2843 char* buf
= buf_to_str(breakpoint
->orig_instr
,
2844 breakpoint
->length
, 16);
2845 command_print(cmd_ctx
, "0x%8.8" PRIx32
", 0x%x, %i, 0x%s",
2846 breakpoint
->address
,
2848 breakpoint
->set
, buf
);
2853 command_print(cmd_ctx
, "0x%8.8" PRIx32
", 0x%x, %i",
2854 breakpoint
->address
,
2855 breakpoint
->length
, breakpoint
->set
);
2858 breakpoint
= breakpoint
->next
;
2863 static int handle_bp_command_set(struct command_context_s
*cmd_ctx
,
2864 uint32_t addr
, uint32_t length
, int hw
)
2866 target_t
*target
= get_current_target(cmd_ctx
);
2867 int retval
= breakpoint_add(target
, addr
, length
, hw
);
2868 if (ERROR_OK
== retval
)
2869 command_print(cmd_ctx
, "breakpoint set at 0x%8.8" PRIx32
"", addr
);
2871 LOG_ERROR("Failure setting breakpoint");
2875 static int handle_bp_command(struct command_context_s
*cmd_ctx
,
2876 char *cmd
, char **args
, int argc
)
2879 return handle_bp_command_list(cmd_ctx
);
2881 if (argc
< 2 || argc
> 3)
2883 command_print(cmd_ctx
, "usage: bp <address> <length> ['hw']");
2884 return ERROR_COMMAND_SYNTAX_ERROR
;
2888 COMMAND_PARSE_NUMBER(u32
, args
[0], addr
);
2890 COMMAND_PARSE_NUMBER(u32
, args
[1], length
);
2895 if (strcmp(args
[2], "hw") == 0)
2898 return ERROR_COMMAND_SYNTAX_ERROR
;
2901 return handle_bp_command_set(cmd_ctx
, addr
, length
, hw
);
2904 static int handle_rbp_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
2907 return ERROR_COMMAND_SYNTAX_ERROR
;
2910 COMMAND_PARSE_NUMBER(u32
, args
[0], addr
);
2912 target_t
*target
= get_current_target(cmd_ctx
);
2913 breakpoint_remove(target
, addr
);
2918 static int handle_wp_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
2920 target_t
*target
= get_current_target(cmd_ctx
);
2924 watchpoint_t
*watchpoint
= target
->watchpoints
;
2928 command_print(cmd_ctx
,
2929 "address: 0x%8.8" PRIx32
", len: 0x%8.8x, r/w/a: %i, value: 0x%8.8" PRIx32
", mask: 0x%8.8" PRIx32
"",
2930 watchpoint
->address
,
2932 (int)(watchpoint
->rw
),
2935 watchpoint
= watchpoint
->next
;
2940 enum watchpoint_rw type
= WPT_ACCESS
;
2942 uint32_t length
= 0;
2943 uint32_t data_value
= 0x0;
2944 uint32_t data_mask
= 0xffffffff;
2949 COMMAND_PARSE_NUMBER(u32
, args
[4], data_mask
);
2952 COMMAND_PARSE_NUMBER(u32
, args
[3], data_value
);
2967 LOG_ERROR("invalid watchpoint mode ('%c')", args
[2][0]);
2968 return ERROR_COMMAND_SYNTAX_ERROR
;
2972 COMMAND_PARSE_NUMBER(u32
, args
[1], length
);
2973 COMMAND_PARSE_NUMBER(u32
, args
[0], addr
);
2977 command_print(cmd_ctx
, "usage: wp [address length "
2978 "[(r|w|a) [value [mask]]]]");
2979 return ERROR_COMMAND_SYNTAX_ERROR
;
2982 int retval
= watchpoint_add(target
, addr
, length
, type
,
2983 data_value
, data_mask
);
2984 if (ERROR_OK
!= retval
)
2985 LOG_ERROR("Failure setting watchpoints");
2990 static int handle_rwp_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
2993 return ERROR_COMMAND_SYNTAX_ERROR
;
2996 COMMAND_PARSE_NUMBER(u32
, args
[0], addr
);
2998 target_t
*target
= get_current_target(cmd_ctx
);
2999 watchpoint_remove(target
, addr
);
3006 * Translate a virtual address to a physical address.
3008 * The low-level target implementation must have logged a detailed error
3009 * which is forwarded to telnet/GDB session.
3011 static int handle_virt2phys_command(command_context_t
*cmd_ctx
,
3012 char *cmd
, char **args
, int argc
)
3015 return ERROR_COMMAND_SYNTAX_ERROR
;
3018 COMMAND_PARSE_NUMBER(u32
, args
[0], va
);
3021 target_t
*target
= get_current_target(cmd_ctx
);
3022 int retval
= target
->type
->virt2phys(target
, va
, &pa
);
3023 if (retval
== ERROR_OK
)
3024 command_print(cmd_ctx
, "Physical address 0x%08" PRIx32
"", pa
);
3029 static void writeData(FILE *f
, const void *data
, size_t len
)
3031 size_t written
= fwrite(data
, 1, len
, f
);
3033 LOG_ERROR("failed to write %zu bytes: %s", len
, strerror(errno
));
3036 static void writeLong(FILE *f
, int l
)
3039 for (i
= 0; i
< 4; i
++)
3041 char c
= (l
>> (i
*8))&0xff;
3042 writeData(f
, &c
, 1);
3047 static void writeString(FILE *f
, char *s
)
3049 writeData(f
, s
, strlen(s
));
3052 /* Dump a gmon.out histogram file. */
3053 static void writeGmon(uint32_t *samples
, uint32_t sampleNum
, char *filename
)
3056 FILE *f
= fopen(filename
, "w");
3059 writeString(f
, "gmon");
3060 writeLong(f
, 0x00000001); /* Version */
3061 writeLong(f
, 0); /* padding */
3062 writeLong(f
, 0); /* padding */
3063 writeLong(f
, 0); /* padding */
3065 uint8_t zero
= 0; /* GMON_TAG_TIME_HIST */
3066 writeData(f
, &zero
, 1);
3068 /* figure out bucket size */
3069 uint32_t min
= samples
[0];
3070 uint32_t max
= samples
[0];
3071 for (i
= 0; i
< sampleNum
; i
++)
3073 if (min
> samples
[i
])
3077 if (max
< samples
[i
])
3083 int addressSpace
= (max
-min
+ 1);
3085 static const uint32_t maxBuckets
= 256 * 1024; /* maximum buckets. */
3086 uint32_t length
= addressSpace
;
3087 if (length
> maxBuckets
)
3089 length
= maxBuckets
;
3091 int *buckets
= malloc(sizeof(int)*length
);
3092 if (buckets
== NULL
)
3097 memset(buckets
, 0, sizeof(int)*length
);
3098 for (i
= 0; i
< sampleNum
;i
++)
3100 uint32_t address
= samples
[i
];
3101 long long a
= address
-min
;
3102 long long b
= length
-1;
3103 long long c
= addressSpace
-1;
3104 int index
= (a
*b
)/c
; /* danger!!!! int32 overflows */
3108 /* append binary memory gmon.out &profile_hist_hdr ((char*)&profile_hist_hdr + sizeof(struct gmon_hist_hdr)) */
3109 writeLong(f
, min
); /* low_pc */
3110 writeLong(f
, max
); /* high_pc */
3111 writeLong(f
, length
); /* # of samples */
3112 writeLong(f
, 64000000); /* 64MHz */
3113 writeString(f
, "seconds");
3114 for (i
= 0; i
< (15-strlen("seconds")); i
++)
3115 writeData(f
, &zero
, 1);
3116 writeString(f
, "s");
3118 /*append binary memory gmon.out profile_hist_data (profile_hist_data + profile_hist_hdr.hist_size) */
3120 char *data
= malloc(2*length
);
3123 for (i
= 0; i
< length
;i
++)
3132 data
[i
*2 + 1]=(val
>> 8)&0xff;
3135 writeData(f
, data
, length
* 2);
3145 /* profiling samples the CPU PC as quickly as OpenOCD is able, which will be used as a random sampling of PC */
3146 static int handle_profile_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
3148 target_t
*target
= get_current_target(cmd_ctx
);
3149 struct timeval timeout
, now
;
3151 gettimeofday(&timeout
, NULL
);
3154 return ERROR_COMMAND_SYNTAX_ERROR
;
3157 COMMAND_PARSE_NUMBER(uint
, args
[0], offset
);
3159 timeval_add_time(&timeout
, offset
, 0);
3161 command_print(cmd_ctx
, "Starting profiling. Halting and resuming the target as often as we can...");
3163 static const int maxSample
= 10000;
3164 uint32_t *samples
= malloc(sizeof(uint32_t)*maxSample
);
3165 if (samples
== NULL
)
3169 /* hopefully it is safe to cache! We want to stop/restart as quickly as possible. */
3170 reg_t
*reg
= register_get_by_name(target
->reg_cache
, "pc", 1);
3175 target_poll(target
);
3176 if (target
->state
== TARGET_HALTED
)
3178 uint32_t t
=*((uint32_t *)reg
->value
);
3179 samples
[numSamples
++]=t
;
3180 retval
= target_resume(target
, 1, 0, 0, 0); /* current pc, addr = 0, do not handle breakpoints, not debugging */
3181 target_poll(target
);
3182 alive_sleep(10); /* sleep 10ms, i.e. <100 samples/second. */
3183 } else if (target
->state
== TARGET_RUNNING
)
3185 /* We want to quickly sample the PC. */
3186 if ((retval
= target_halt(target
)) != ERROR_OK
)
3193 command_print(cmd_ctx
, "Target not halted or running");
3197 if (retval
!= ERROR_OK
)
3202 gettimeofday(&now
, NULL
);
3203 if ((numSamples
>= maxSample
) || ((now
.tv_sec
>= timeout
.tv_sec
) && (now
.tv_usec
>= timeout
.tv_usec
)))
3205 command_print(cmd_ctx
, "Profiling completed. %d samples.", numSamples
);
3206 if ((retval
= target_poll(target
)) != ERROR_OK
)
3211 if (target
->state
== TARGET_HALTED
)
3213 target_resume(target
, 1, 0, 0, 0); /* current pc, addr = 0, do not handle breakpoints, not debugging */
3215 if ((retval
= target_poll(target
)) != ERROR_OK
)
3220 writeGmon(samples
, numSamples
, args
[1]);
3221 command_print(cmd_ctx
, "Wrote %s", args
[1]);
3230 static int new_int_array_element(Jim_Interp
* interp
, const char *varname
, int idx
, uint32_t val
)
3233 Jim_Obj
*nameObjPtr
, *valObjPtr
;
3236 namebuf
= alloc_printf("%s(%d)", varname
, idx
);
3240 nameObjPtr
= Jim_NewStringObj(interp
, namebuf
, -1);
3241 valObjPtr
= Jim_NewIntObj(interp
, val
);
3242 if (!nameObjPtr
|| !valObjPtr
)
3248 Jim_IncrRefCount(nameObjPtr
);
3249 Jim_IncrRefCount(valObjPtr
);
3250 result
= Jim_SetVariable(interp
, nameObjPtr
, valObjPtr
);
3251 Jim_DecrRefCount(interp
, nameObjPtr
);
3252 Jim_DecrRefCount(interp
, valObjPtr
);
3254 /* printf("%s(%d) <= 0%08x\n", varname, idx, val); */
3258 static int jim_mem2array(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
3260 command_context_t
*context
;
3263 context
= Jim_GetAssocData(interp
, "context");
3264 if (context
== NULL
)
3266 LOG_ERROR("mem2array: no command context");
3269 target
= get_current_target(context
);
3272 LOG_ERROR("mem2array: no current target");
3276 return target_mem2array(interp
, target
, argc
-1, argv
+ 1);
3279 static int target_mem2array(Jim_Interp
*interp
, target_t
*target
, int argc
, Jim_Obj
*const *argv
)
3287 const char *varname
;
3288 uint8_t buffer
[4096];
3292 /* argv[1] = name of array to receive the data
3293 * argv[2] = desired width
3294 * argv[3] = memory address
3295 * argv[4] = count of times to read
3298 Jim_WrongNumArgs(interp
, 1, argv
, "varname width addr nelems");
3301 varname
= Jim_GetString(argv
[0], &len
);
3302 /* given "foo" get space for worse case "foo(%d)" .. add 20 */
3304 e
= Jim_GetLong(interp
, argv
[1], &l
);
3310 e
= Jim_GetLong(interp
, argv
[2], &l
);
3315 e
= Jim_GetLong(interp
, argv
[3], &l
);
3331 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3332 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "Invalid width param, must be 8/16/32", NULL
);
3336 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3337 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "mem2array: zero width read?", NULL
);
3340 if ((addr
+ (len
* width
)) < addr
) {
3341 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3342 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "mem2array: addr + len - wraps to zero?", NULL
);
3345 /* absurd transfer size? */
3347 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3348 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "mem2array: absurd > 64K item request", NULL
);
3353 ((width
== 2) && ((addr
& 1) == 0)) ||
3354 ((width
== 4) && ((addr
& 3) == 0))) {
3358 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3359 sprintf(buf
, "mem2array address: 0x%08" PRIx32
" is not aligned for %" PRId32
" byte reads",
3362 Jim_AppendStrings(interp
, Jim_GetResult(interp
), buf
, NULL
);
3373 /* Slurp... in buffer size chunks */
3375 count
= len
; /* in objects.. */
3376 if (count
> (sizeof(buffer
)/width
)) {
3377 count
= (sizeof(buffer
)/width
);
3380 retval
= target_read_memory(target
, addr
, width
, count
, buffer
);
3381 if (retval
!= ERROR_OK
) {
3383 LOG_ERROR("mem2array: Read @ 0x%08x, w=%d, cnt=%d, failed",
3387 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3388 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "mem2array: cannot read memory", NULL
);
3392 v
= 0; /* shut up gcc */
3393 for (i
= 0 ;i
< count
;i
++, n
++) {
3396 v
= target_buffer_get_u32(target
, &buffer
[i
*width
]);
3399 v
= target_buffer_get_u16(target
, &buffer
[i
*width
]);
3402 v
= buffer
[i
] & 0x0ff;
3405 new_int_array_element(interp
, varname
, n
, v
);
3411 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3416 static int get_int_array_element(Jim_Interp
* interp
, const char *varname
, int idx
, uint32_t *val
)
3419 Jim_Obj
*nameObjPtr
, *valObjPtr
;
3423 namebuf
= alloc_printf("%s(%d)", varname
, idx
);
3427 nameObjPtr
= Jim_NewStringObj(interp
, namebuf
, -1);
3434 Jim_IncrRefCount(nameObjPtr
);
3435 valObjPtr
= Jim_GetVariable(interp
, nameObjPtr
, JIM_ERRMSG
);
3436 Jim_DecrRefCount(interp
, nameObjPtr
);
3438 if (valObjPtr
== NULL
)
3441 result
= Jim_GetLong(interp
, valObjPtr
, &l
);
3442 /* printf("%s(%d) => 0%08x\n", varname, idx, val); */
3447 static int jim_array2mem(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
3449 command_context_t
*context
;
3452 context
= Jim_GetAssocData(interp
, "context");
3453 if (context
== NULL
) {
3454 LOG_ERROR("array2mem: no command context");
3457 target
= get_current_target(context
);
3458 if (target
== NULL
) {
3459 LOG_ERROR("array2mem: no current target");
3463 return target_array2mem(interp
,target
, argc
-1, argv
+ 1);
3465 static int target_array2mem(Jim_Interp
*interp
, target_t
*target
, int argc
, Jim_Obj
*const *argv
)
3473 const char *varname
;
3474 uint8_t buffer
[4096];
3478 /* argv[1] = name of array to get the data
3479 * argv[2] = desired width
3480 * argv[3] = memory address
3481 * argv[4] = count to write
3484 Jim_WrongNumArgs(interp
, 1, argv
, "varname width addr nelems");
3487 varname
= Jim_GetString(argv
[0], &len
);
3488 /* given "foo" get space for worse case "foo(%d)" .. add 20 */
3490 e
= Jim_GetLong(interp
, argv
[1], &l
);
3496 e
= Jim_GetLong(interp
, argv
[2], &l
);
3501 e
= Jim_GetLong(interp
, argv
[3], &l
);
3517 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3518 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "Invalid width param, must be 8/16/32", NULL
);
3522 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3523 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "array2mem: zero width read?", NULL
);
3526 if ((addr
+ (len
* width
)) < addr
) {
3527 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3528 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "array2mem: addr + len - wraps to zero?", NULL
);
3531 /* absurd transfer size? */
3533 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3534 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "array2mem: absurd > 64K item request", NULL
);
3539 ((width
== 2) && ((addr
& 1) == 0)) ||
3540 ((width
== 4) && ((addr
& 3) == 0))) {
3544 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3545 sprintf(buf
, "array2mem address: 0x%08x is not aligned for %d byte reads",
3548 Jim_AppendStrings(interp
, Jim_GetResult(interp
), buf
, NULL
);
3559 /* Slurp... in buffer size chunks */
3561 count
= len
; /* in objects.. */
3562 if (count
> (sizeof(buffer
)/width
)) {
3563 count
= (sizeof(buffer
)/width
);
3566 v
= 0; /* shut up gcc */
3567 for (i
= 0 ;i
< count
;i
++, n
++) {
3568 get_int_array_element(interp
, varname
, n
, &v
);
3571 target_buffer_set_u32(target
, &buffer
[i
*width
], v
);
3574 target_buffer_set_u16(target
, &buffer
[i
*width
], v
);
3577 buffer
[i
] = v
& 0x0ff;
3583 retval
= target_write_memory(target
, addr
, width
, count
, buffer
);
3584 if (retval
!= ERROR_OK
) {
3586 LOG_ERROR("array2mem: Write @ 0x%08x, w=%d, cnt=%d, failed",
3590 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3591 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "array2mem: cannot read memory", NULL
);
3597 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3602 void target_all_handle_event(enum target_event e
)
3606 LOG_DEBUG("**all*targets: event: %d, %s",
3608 Jim_Nvp_value2name_simple(nvp_target_event
, e
)->name
);
3610 target
= all_targets
;
3612 target_handle_event(target
, e
);
3613 target
= target
->next
;
3618 /* FIX? should we propagate errors here rather than printing them
3621 void target_handle_event(target_t
*target
, enum target_event e
)
3623 target_event_action_t
*teap
;
3625 for (teap
= target
->event_action
; teap
!= NULL
; teap
= teap
->next
) {
3626 if (teap
->event
== e
) {
3627 LOG_DEBUG("target: (%d) %s (%s) event: %d (%s) action: %s",
3628 target
->target_number
,
3630 target_get_name(target
),
3632 Jim_Nvp_value2name_simple(nvp_target_event
, e
)->name
,
3633 Jim_GetString(teap
->body
, NULL
));
3634 if (Jim_EvalObj(interp
, teap
->body
) != JIM_OK
)
3636 Jim_PrintErrorMessage(interp
);
3642 enum target_cfg_param
{
3645 TCFG_WORK_AREA_VIRT
,
3646 TCFG_WORK_AREA_PHYS
,
3647 TCFG_WORK_AREA_SIZE
,
3648 TCFG_WORK_AREA_BACKUP
,
3651 TCFG_CHAIN_POSITION
,
3654 static Jim_Nvp nvp_config_opts
[] = {
3655 { .name
= "-type", .value
= TCFG_TYPE
},
3656 { .name
= "-event", .value
= TCFG_EVENT
},
3657 { .name
= "-work-area-virt", .value
= TCFG_WORK_AREA_VIRT
},
3658 { .name
= "-work-area-phys", .value
= TCFG_WORK_AREA_PHYS
},
3659 { .name
= "-work-area-size", .value
= TCFG_WORK_AREA_SIZE
},
3660 { .name
= "-work-area-backup", .value
= TCFG_WORK_AREA_BACKUP
},
3661 { .name
= "-endian" , .value
= TCFG_ENDIAN
},
3662 { .name
= "-variant", .value
= TCFG_VARIANT
},
3663 { .name
= "-chain-position", .value
= TCFG_CHAIN_POSITION
},
3665 { .name
= NULL
, .value
= -1 }
3668 static int target_configure(Jim_GetOptInfo
*goi
, target_t
*target
)
3676 /* parse config or cget options ... */
3677 while (goi
->argc
> 0) {
3678 Jim_SetEmptyResult(goi
->interp
);
3679 /* Jim_GetOpt_Debug(goi); */
3681 if (target
->type
->target_jim_configure
) {
3682 /* target defines a configure function */
3683 /* target gets first dibs on parameters */
3684 e
= (*(target
->type
->target_jim_configure
))(target
, goi
);
3693 /* otherwise we 'continue' below */
3695 e
= Jim_GetOpt_Nvp(goi
, nvp_config_opts
, &n
);
3697 Jim_GetOpt_NvpUnknown(goi
, nvp_config_opts
, 0);
3703 if (goi
->isconfigure
) {
3704 Jim_SetResult_sprintf(goi
->interp
, "not setable: %s", n
->name
);
3708 if (goi
->argc
!= 0) {
3709 Jim_WrongNumArgs(goi
->interp
, goi
->argc
, goi
->argv
, "NO PARAMS");
3713 Jim_SetResultString(goi
->interp
, target_get_name(target
), -1);
3717 if (goi
->argc
== 0) {
3718 Jim_WrongNumArgs(goi
->interp
, goi
->argc
, goi
->argv
, "-event ?event-name? ...");
3722 e
= Jim_GetOpt_Nvp(goi
, nvp_target_event
, &n
);
3724 Jim_GetOpt_NvpUnknown(goi
, nvp_target_event
, 1);
3728 if (goi
->isconfigure
) {
3729 if (goi
->argc
!= 1) {
3730 Jim_WrongNumArgs(goi
->interp
, goi
->argc
, goi
->argv
, "-event ?event-name? ?EVENT-BODY?");
3734 if (goi
->argc
!= 0) {
3735 Jim_WrongNumArgs(goi
->interp
, goi
->argc
, goi
->argv
, "-event ?event-name?");
3741 target_event_action_t
*teap
;
3743 teap
= target
->event_action
;
3744 /* replace existing? */
3746 if (teap
->event
== (enum target_event
)n
->value
) {
3752 if (goi
->isconfigure
) {
3753 bool replace
= true;
3756 teap
= calloc(1, sizeof(*teap
));
3759 teap
->event
= n
->value
;
3760 Jim_GetOpt_Obj(goi
, &o
);
3762 Jim_DecrRefCount(interp
, teap
->body
);
3764 teap
->body
= Jim_DuplicateObj(goi
->interp
, o
);
3767 * Tcl/TK - "tk events" have a nice feature.
3768 * See the "BIND" command.
3769 * We should support that here.
3770 * You can specify %X and %Y in the event code.
3771 * The idea is: %T - target name.
3772 * The idea is: %N - target number
3773 * The idea is: %E - event name.
3775 Jim_IncrRefCount(teap
->body
);
3779 /* add to head of event list */
3780 teap
->next
= target
->event_action
;
3781 target
->event_action
= teap
;
3783 Jim_SetEmptyResult(goi
->interp
);
3787 Jim_SetEmptyResult(goi
->interp
);
3789 Jim_SetResult(goi
->interp
, Jim_DuplicateObj(goi
->interp
, teap
->body
));
3796 case TCFG_WORK_AREA_VIRT
:
3797 if (goi
->isconfigure
) {
3798 target_free_all_working_areas(target
);
3799 e
= Jim_GetOpt_Wide(goi
, &w
);
3803 target
->working_area_virt
= w
;
3804 target
->working_area_virt_spec
= true;
3806 if (goi
->argc
!= 0) {
3810 Jim_SetResult(interp
, Jim_NewIntObj(goi
->interp
, target
->working_area_virt
));
3814 case TCFG_WORK_AREA_PHYS
:
3815 if (goi
->isconfigure
) {
3816 target_free_all_working_areas(target
);
3817 e
= Jim_GetOpt_Wide(goi
, &w
);
3821 target
->working_area_phys
= w
;
3822 target
->working_area_phys_spec
= true;
3824 if (goi
->argc
!= 0) {
3828 Jim_SetResult(interp
, Jim_NewIntObj(goi
->interp
, target
->working_area_phys
));
3832 case TCFG_WORK_AREA_SIZE
:
3833 if (goi
->isconfigure
) {
3834 target_free_all_working_areas(target
);
3835 e
= Jim_GetOpt_Wide(goi
, &w
);
3839 target
->working_area_size
= w
;
3841 if (goi
->argc
!= 0) {
3845 Jim_SetResult(interp
, Jim_NewIntObj(goi
->interp
, target
->working_area_size
));
3849 case TCFG_WORK_AREA_BACKUP
:
3850 if (goi
->isconfigure
) {
3851 target_free_all_working_areas(target
);
3852 e
= Jim_GetOpt_Wide(goi
, &w
);
3856 /* make this exactly 1 or 0 */
3857 target
->backup_working_area
= (!!w
);
3859 if (goi
->argc
!= 0) {
3863 Jim_SetResult(interp
, Jim_NewIntObj(goi
->interp
, target
->backup_working_area
));
3864 /* loop for more e*/
3868 if (goi
->isconfigure
) {
3869 e
= Jim_GetOpt_Nvp(goi
, nvp_target_endian
, &n
);
3871 Jim_GetOpt_NvpUnknown(goi
, nvp_target_endian
, 1);
3874 target
->endianness
= n
->value
;
3876 if (goi
->argc
!= 0) {
3880 n
= Jim_Nvp_value2name_simple(nvp_target_endian
, target
->endianness
);
3881 if (n
->name
== NULL
) {
3882 target
->endianness
= TARGET_LITTLE_ENDIAN
;
3883 n
= Jim_Nvp_value2name_simple(nvp_target_endian
, target
->endianness
);
3885 Jim_SetResultString(goi
->interp
, n
->name
, -1);
3890 if (goi
->isconfigure
) {
3891 if (goi
->argc
< 1) {
3892 Jim_SetResult_sprintf(goi
->interp
,
3897 if (target
->variant
) {
3898 free((void *)(target
->variant
));
3900 e
= Jim_GetOpt_String(goi
, &cp
, NULL
);
3901 target
->variant
= strdup(cp
);
3903 if (goi
->argc
!= 0) {
3907 Jim_SetResultString(goi
->interp
, target
->variant
,-1);
3910 case TCFG_CHAIN_POSITION
:
3911 if (goi
->isconfigure
) {
3914 target_free_all_working_areas(target
);
3915 e
= Jim_GetOpt_Obj(goi
, &o
);
3919 tap
= jtag_tap_by_jim_obj(goi
->interp
, o
);
3923 /* make this exactly 1 or 0 */
3926 if (goi
->argc
!= 0) {
3930 Jim_SetResultString(interp
, target
->tap
->dotted_name
, -1);
3931 /* loop for more e*/
3934 } /* while (goi->argc) */
3937 /* done - we return */
3941 /** this is the 'tcl' handler for the target specific command */
3942 static int tcl_target_func(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
3947 uint8_t target_buf
[32];
3950 struct command_context_s
*cmd_ctx
;
3957 TS_CMD_MWW
, TS_CMD_MWH
, TS_CMD_MWB
,
3958 TS_CMD_MDW
, TS_CMD_MDH
, TS_CMD_MDB
,
3959 TS_CMD_MRW
, TS_CMD_MRH
, TS_CMD_MRB
,
3960 TS_CMD_MEM2ARRAY
, TS_CMD_ARRAY2MEM
,
3968 TS_CMD_INVOKE_EVENT
,
3971 static const Jim_Nvp target_options
[] = {
3972 { .name
= "configure", .value
= TS_CMD_CONFIGURE
},
3973 { .name
= "cget", .value
= TS_CMD_CGET
},
3974 { .name
= "mww", .value
= TS_CMD_MWW
},
3975 { .name
= "mwh", .value
= TS_CMD_MWH
},
3976 { .name
= "mwb", .value
= TS_CMD_MWB
},
3977 { .name
= "mdw", .value
= TS_CMD_MDW
},
3978 { .name
= "mdh", .value
= TS_CMD_MDH
},
3979 { .name
= "mdb", .value
= TS_CMD_MDB
},
3980 { .name
= "mem2array", .value
= TS_CMD_MEM2ARRAY
},
3981 { .name
= "array2mem", .value
= TS_CMD_ARRAY2MEM
},
3982 { .name
= "eventlist", .value
= TS_CMD_EVENTLIST
},
3983 { .name
= "curstate", .value
= TS_CMD_CURSTATE
},
3985 { .name
= "arp_examine", .value
= TS_CMD_EXAMINE
},
3986 { .name
= "arp_poll", .value
= TS_CMD_POLL
},
3987 { .name
= "arp_reset", .value
= TS_CMD_RESET
},
3988 { .name
= "arp_halt", .value
= TS_CMD_HALT
},
3989 { .name
= "arp_waitstate", .value
= TS_CMD_WAITSTATE
},
3990 { .name
= "invoke-event", .value
= TS_CMD_INVOKE_EVENT
},
3992 { .name
= NULL
, .value
= -1 },
3995 /* go past the "command" */
3996 Jim_GetOpt_Setup(&goi
, interp
, argc
-1, argv
+ 1);
3998 target
= Jim_CmdPrivData(goi
.interp
);
3999 cmd_ctx
= Jim_GetAssocData(goi
.interp
, "context");
4001 /* commands here are in an NVP table */
4002 e
= Jim_GetOpt_Nvp(&goi
, target_options
, &n
);
4004 Jim_GetOpt_NvpUnknown(&goi
, target_options
, 0);
4007 /* Assume blank result */
4008 Jim_SetEmptyResult(goi
.interp
);
4011 case TS_CMD_CONFIGURE
:
4013 Jim_WrongNumArgs(goi
.interp
, goi
.argc
, goi
.argv
, "missing: -option VALUE ...");
4016 goi
.isconfigure
= 1;
4017 return target_configure(&goi
, target
);
4019 // some things take params
4021 Jim_WrongNumArgs(goi
.interp
, 0, goi
.argv
, "missing: ?-option?");
4024 goi
.isconfigure
= 0;
4025 return target_configure(&goi
, target
);
4033 * argv[3] = optional count.
4036 if ((goi
.argc
== 2) || (goi
.argc
== 3)) {
4040 Jim_SetResult_sprintf(goi
.interp
, "expected: %s ADDR DATA [COUNT]", n
->name
);
4044 e
= Jim_GetOpt_Wide(&goi
, &a
);
4049 e
= Jim_GetOpt_Wide(&goi
, &b
);
4053 if (goi
.argc
== 3) {
4054 e
= Jim_GetOpt_Wide(&goi
, &c
);
4064 target_buffer_set_u32(target
, target_buf
, b
);
4068 target_buffer_set_u16(target
, target_buf
, b
);
4072 target_buffer_set_u8(target
, target_buf
, b
);
4076 for (x
= 0 ; x
< c
; x
++) {
4077 e
= target_write_memory(target
, a
, b
, 1, target_buf
);
4078 if (e
!= ERROR_OK
) {
4079 Jim_SetResult_sprintf(interp
, "Error writing @ 0x%08x: %d\n", (int)(a
), e
);
4092 /* argv[0] = command
4094 * argv[2] = optional count
4096 if ((goi
.argc
== 2) || (goi
.argc
== 3)) {
4097 Jim_SetResult_sprintf(goi
.interp
, "expected: %s ADDR [COUNT]", n
->name
);
4100 e
= Jim_GetOpt_Wide(&goi
, &a
);
4105 e
= Jim_GetOpt_Wide(&goi
, &c
);
4112 b
= 1; /* shut up gcc */
4125 /* convert to "bytes" */
4127 /* count is now in 'BYTES' */
4133 e
= target_read_memory(target
, a
, b
, y
/ b
, target_buf
);
4134 if (e
!= ERROR_OK
) {
4135 Jim_SetResult_sprintf(interp
, "error reading target @ 0x%08lx", (int)(a
));
4139 Jim_fprintf(interp
, interp
->cookie_stdout
, "0x%08x ", (int)(a
));
4142 for (x
= 0 ; (x
< 16) && (x
< y
) ; x
+= 4) {
4143 z
= target_buffer_get_u32(target
, &(target_buf
[ x
* 4 ]));
4144 Jim_fprintf(interp
, interp
->cookie_stdout
, "%08x ", (int)(z
));
4146 for (; (x
< 16) ; x
+= 4) {
4147 Jim_fprintf(interp
, interp
->cookie_stdout
, " ");
4151 for (x
= 0 ; (x
< 16) && (x
< y
) ; x
+= 2) {
4152 z
= target_buffer_get_u16(target
, &(target_buf
[ x
* 2 ]));
4153 Jim_fprintf(interp
, interp
->cookie_stdout
, "%04x ", (int)(z
));
4155 for (; (x
< 16) ; x
+= 2) {
4156 Jim_fprintf(interp
, interp
->cookie_stdout
, " ");
4161 for (x
= 0 ; (x
< 16) && (x
< y
) ; x
+= 1) {
4162 z
= target_buffer_get_u8(target
, &(target_buf
[ x
* 4 ]));
4163 Jim_fprintf(interp
, interp
->cookie_stdout
, "%02x ", (int)(z
));
4165 for (; (x
< 16) ; x
+= 1) {
4166 Jim_fprintf(interp
, interp
->cookie_stdout
, " ");
4170 /* ascii-ify the bytes */
4171 for (x
= 0 ; x
< y
; x
++) {
4172 if ((target_buf
[x
] >= 0x20) &&
4173 (target_buf
[x
] <= 0x7e)) {
4177 target_buf
[x
] = '.';
4182 target_buf
[x
] = ' ';
4187 /* print - with a newline */
4188 Jim_fprintf(interp
, interp
->cookie_stdout
, "%s\n", target_buf
);
4194 case TS_CMD_MEM2ARRAY
:
4195 return target_mem2array(goi
.interp
, target
, goi
.argc
, goi
.argv
);
4197 case TS_CMD_ARRAY2MEM
:
4198 return target_array2mem(goi
.interp
, target
, goi
.argc
, goi
.argv
);
4200 case TS_CMD_EXAMINE
:
4202 Jim_WrongNumArgs(goi
.interp
, 2, argv
, "[no parameters]");
4205 if (!target
->tap
->enabled
)
4206 goto err_tap_disabled
;
4207 e
= target
->type
->examine(target
);
4208 if (e
!= ERROR_OK
) {
4209 Jim_SetResult_sprintf(interp
, "examine-fails: %d", e
);
4215 Jim_WrongNumArgs(goi
.interp
, 2, argv
, "[no parameters]");
4218 if (!target
->tap
->enabled
)
4219 goto err_tap_disabled
;
4220 if (!(target_was_examined(target
))) {
4221 e
= ERROR_TARGET_NOT_EXAMINED
;
4223 e
= target
->type
->poll(target
);
4225 if (e
!= ERROR_OK
) {
4226 Jim_SetResult_sprintf(interp
, "poll-fails: %d", e
);
4233 if (goi
.argc
!= 2) {
4234 Jim_WrongNumArgs(interp
, 2, argv
,
4235 "([tT]|[fF]|assert|deassert) BOOL");
4238 e
= Jim_GetOpt_Nvp(&goi
, nvp_assert
, &n
);
4240 Jim_GetOpt_NvpUnknown(&goi
, nvp_assert
, 1);
4243 /* the halt or not param */
4244 e
= Jim_GetOpt_Wide(&goi
, &a
);
4248 if (!target
->tap
->enabled
)
4249 goto err_tap_disabled
;
4250 if (!target
->type
->assert_reset
4251 || !target
->type
->deassert_reset
) {
4252 Jim_SetResult_sprintf(interp
,
4253 "No target-specific reset for %s",
4257 /* determine if we should halt or not. */
4258 target
->reset_halt
= !!a
;
4259 /* When this happens - all workareas are invalid. */
4260 target_free_all_working_areas_restore(target
, 0);
4263 if (n
->value
== NVP_ASSERT
) {
4264 e
= target
->type
->assert_reset(target
);
4266 e
= target
->type
->deassert_reset(target
);
4268 return (e
== ERROR_OK
) ? JIM_OK
: JIM_ERR
;
4271 Jim_WrongNumArgs(goi
.interp
, 0, argv
, "halt [no parameters]");
4274 if (!target
->tap
->enabled
)
4275 goto err_tap_disabled
;
4276 e
= target
->type
->halt(target
);
4277 return (e
== ERROR_OK
) ? JIM_OK
: JIM_ERR
;
4278 case TS_CMD_WAITSTATE
:
4279 /* params: <name> statename timeoutmsecs */
4280 if (goi
.argc
!= 2) {
4281 Jim_SetResult_sprintf(goi
.interp
, "%s STATENAME TIMEOUTMSECS", n
->name
);
4284 e
= Jim_GetOpt_Nvp(&goi
, nvp_target_state
, &n
);
4286 Jim_GetOpt_NvpUnknown(&goi
, nvp_target_state
,1);
4289 e
= Jim_GetOpt_Wide(&goi
, &a
);
4293 if (!target
->tap
->enabled
)
4294 goto err_tap_disabled
;
4295 e
= target_wait_state(target
, n
->value
, a
);
4296 if (e
!= ERROR_OK
) {
4297 Jim_SetResult_sprintf(goi
.interp
,
4298 "target: %s wait %s fails (%d) %s",
4301 e
, target_strerror_safe(e
));
4306 case TS_CMD_EVENTLIST
:
4307 /* List for human, Events defined for this target.
4308 * scripts/programs should use 'name cget -event NAME'
4311 target_event_action_t
*teap
;
4312 teap
= target
->event_action
;
4313 command_print(cmd_ctx
, "Event actions for target (%d) %s\n",
4314 target
->target_number
,
4316 command_print(cmd_ctx
, "%-25s | Body", "Event");
4317 command_print(cmd_ctx
, "------------------------- | ----------------------------------------");
4319 command_print(cmd_ctx
,
4321 Jim_Nvp_value2name_simple(nvp_target_event
, teap
->event
)->name
,
4322 Jim_GetString(teap
->body
, NULL
));
4325 command_print(cmd_ctx
, "***END***");
4328 case TS_CMD_CURSTATE
:
4329 if (goi
.argc
!= 0) {
4330 Jim_WrongNumArgs(goi
.interp
, 0, argv
, "[no parameters]");
4333 Jim_SetResultString(goi
.interp
,
4334 target_state_name( target
),
4337 case TS_CMD_INVOKE_EVENT
:
4338 if (goi
.argc
!= 1) {
4339 Jim_SetResult_sprintf(goi
.interp
, "%s ?EVENTNAME?",n
->name
);
4342 e
= Jim_GetOpt_Nvp(&goi
, nvp_target_event
, &n
);
4344 Jim_GetOpt_NvpUnknown(&goi
, nvp_target_event
, 1);
4347 target_handle_event(target
, n
->value
);
4353 Jim_SetResult_sprintf(interp
, "[TAP is disabled]");
4357 static int target_create(Jim_GetOptInfo
*goi
)
4366 struct command_context_s
*cmd_ctx
;
4368 cmd_ctx
= Jim_GetAssocData(goi
->interp
, "context");
4369 if (goi
->argc
< 3) {
4370 Jim_WrongNumArgs(goi
->interp
, 1, goi
->argv
, "?name? ?type? ..options...");
4375 Jim_GetOpt_Obj(goi
, &new_cmd
);
4376 /* does this command exist? */
4377 cmd
= Jim_GetCommand(goi
->interp
, new_cmd
, JIM_ERRMSG
);
4379 cp
= Jim_GetString(new_cmd
, NULL
);
4380 Jim_SetResult_sprintf(goi
->interp
, "Command/target: %s Exists", cp
);
4385 e
= Jim_GetOpt_String(goi
, &cp2
, NULL
);
4387 /* now does target type exist */
4388 for (x
= 0 ; target_types
[x
] ; x
++) {
4389 if (0 == strcmp(cp
, target_types
[x
]->name
)) {
4394 if (target_types
[x
] == NULL
) {
4395 Jim_SetResult_sprintf(goi
->interp
, "Unknown target type %s, try one of ", cp
);
4396 for (x
= 0 ; target_types
[x
] ; x
++) {
4397 if (target_types
[x
+ 1]) {
4398 Jim_AppendStrings(goi
->interp
,
4399 Jim_GetResult(goi
->interp
),
4400 target_types
[x
]->name
,
4403 Jim_AppendStrings(goi
->interp
,
4404 Jim_GetResult(goi
->interp
),
4406 target_types
[x
]->name
,NULL
);
4413 target
= calloc(1,sizeof(target_t
));
4414 /* set target number */
4415 target
->target_number
= new_target_number();
4417 /* allocate memory for each unique target type */
4418 target
->type
= (target_type_t
*)calloc(1,sizeof(target_type_t
));
4420 memcpy(target
->type
, target_types
[x
], sizeof(target_type_t
));
4422 /* will be set by "-endian" */
4423 target
->endianness
= TARGET_ENDIAN_UNKNOWN
;
4425 target
->working_area
= 0x0;
4426 target
->working_area_size
= 0x0;
4427 target
->working_areas
= NULL
;
4428 target
->backup_working_area
= 0;
4430 target
->state
= TARGET_UNKNOWN
;
4431 target
->debug_reason
= DBG_REASON_UNDEFINED
;
4432 target
->reg_cache
= NULL
;
4433 target
->breakpoints
= NULL
;
4434 target
->watchpoints
= NULL
;
4435 target
->next
= NULL
;
4436 target
->arch_info
= NULL
;
4438 target
->display
= 1;
4440 target
->halt_issued
= false;
4442 /* initialize trace information */
4443 target
->trace_info
= malloc(sizeof(trace_t
));
4444 target
->trace_info
->num_trace_points
= 0;
4445 target
->trace_info
->trace_points_size
= 0;
4446 target
->trace_info
->trace_points
= NULL
;
4447 target
->trace_info
->trace_history_size
= 0;
4448 target
->trace_info
->trace_history
= NULL
;
4449 target
->trace_info
->trace_history_pos
= 0;
4450 target
->trace_info
->trace_history_overflowed
= 0;
4452 target
->dbgmsg
= NULL
;
4453 target
->dbg_msg_enabled
= 0;
4455 target
->endianness
= TARGET_ENDIAN_UNKNOWN
;
4457 /* Do the rest as "configure" options */
4458 goi
->isconfigure
= 1;
4459 e
= target_configure(goi
, target
);
4461 if (target
->tap
== NULL
)
4463 Jim_SetResultString(interp
, "-chain-position required when creating target", -1);
4473 if (target
->endianness
== TARGET_ENDIAN_UNKNOWN
) {
4474 /* default endian to little if not specified */
4475 target
->endianness
= TARGET_LITTLE_ENDIAN
;
4478 /* incase variant is not set */
4479 if (!target
->variant
)
4480 target
->variant
= strdup("");
4482 /* create the target specific commands */
4483 if (target
->type
->register_commands
) {
4484 (*(target
->type
->register_commands
))(cmd_ctx
);
4486 if (target
->type
->target_create
) {
4487 (*(target
->type
->target_create
))(target
, goi
->interp
);
4490 /* append to end of list */
4493 tpp
= &(all_targets
);
4495 tpp
= &((*tpp
)->next
);
4500 cp
= Jim_GetString(new_cmd
, NULL
);
4501 target
->cmd_name
= strdup(cp
);
4503 /* now - create the new target name command */
4504 e
= Jim_CreateCommand(goi
->interp
,
4507 tcl_target_func
, /* C function */
4508 target
, /* private data */
4509 NULL
); /* no del proc */
4514 static int jim_target(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4518 struct command_context_s
*cmd_ctx
;
4522 /* TG = target generic */
4530 const char *target_cmds
[] = {
4531 "create", "types", "names", "current", "number",
4533 NULL
/* terminate */
4536 LOG_DEBUG("Target command params:");
4537 LOG_DEBUG("%s", Jim_Debug_ArgvString(interp
, argc
, argv
));
4539 cmd_ctx
= Jim_GetAssocData(interp
, "context");
4541 Jim_GetOpt_Setup(&goi
, interp
, argc
-1, argv
+ 1);
4543 if (goi
.argc
== 0) {
4544 Jim_WrongNumArgs(interp
, 1, argv
, "missing: command ...");
4548 /* Jim_GetOpt_Debug(&goi); */
4549 r
= Jim_GetOpt_Enum(&goi
, target_cmds
, &x
);
4556 Jim_Panic(goi
.interp
,"Why am I here?");
4558 case TG_CMD_CURRENT
:
4559 if (goi
.argc
!= 0) {
4560 Jim_WrongNumArgs(goi
.interp
, 1, goi
.argv
, "Too many parameters");
4563 Jim_SetResultString(goi
.interp
, get_current_target(cmd_ctx
)->cmd_name
, -1);
4566 if (goi
.argc
!= 0) {
4567 Jim_WrongNumArgs(goi
.interp
, 1, goi
.argv
, "Too many parameters");
4570 Jim_SetResult(goi
.interp
, Jim_NewListObj(goi
.interp
, NULL
, 0));
4571 for (x
= 0 ; target_types
[x
] ; x
++) {
4572 Jim_ListAppendElement(goi
.interp
,
4573 Jim_GetResult(goi
.interp
),
4574 Jim_NewStringObj(goi
.interp
, target_types
[x
]->name
, -1));
4578 if (goi
.argc
!= 0) {
4579 Jim_WrongNumArgs(goi
.interp
, 1, goi
.argv
, "Too many parameters");
4582 Jim_SetResult(goi
.interp
, Jim_NewListObj(goi
.interp
, NULL
, 0));
4583 target
= all_targets
;
4585 Jim_ListAppendElement(goi
.interp
,
4586 Jim_GetResult(goi
.interp
),
4587 Jim_NewStringObj(goi
.interp
, target
->cmd_name
, -1));
4588 target
= target
->next
;
4593 Jim_WrongNumArgs(goi
.interp
, goi
.argc
, goi
.argv
, "?name ... config options ...");
4596 return target_create(&goi
);
4599 /* It's OK to remove this mechanism sometime after August 2010 or so */
4600 LOG_WARNING("don't use numbers as target identifiers; use names");
4601 if (goi
.argc
!= 1) {
4602 Jim_SetResult_sprintf(goi
.interp
, "expected: target number ?NUMBER?");
4605 e
= Jim_GetOpt_Wide(&goi
, &w
);
4609 for (x
= 0, target
= all_targets
; target
; target
= target
->next
, x
++) {
4610 if (target
->target_number
== w
)
4613 if (target
== NULL
) {
4614 Jim_SetResult_sprintf(goi
.interp
,
4615 "Target: number %d does not exist", (int)(w
));
4618 Jim_SetResultString(goi
.interp
, target
->cmd_name
, -1);
4621 if (goi
.argc
!= 0) {
4622 Jim_WrongNumArgs(goi
.interp
, 0, goi
.argv
, "<no parameters>");
4625 for (x
= 0, target
= all_targets
; target
; target
= target
->next
, x
++)
4627 Jim_SetResult(goi
.interp
, Jim_NewIntObj(goi
.interp
, x
));
4643 static int fastload_num
;
4644 static struct FastLoad
*fastload
;
4646 static void free_fastload(void)
4648 if (fastload
!= NULL
)
4651 for (i
= 0; i
< fastload_num
; i
++)
4653 if (fastload
[i
].data
)
4654 free(fastload
[i
].data
);
4664 static int handle_fast_load_image_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
4668 uint32_t image_size
;
4669 uint32_t min_address
= 0;
4670 uint32_t max_address
= 0xffffffff;
4675 duration_t duration
;
4676 char *duration_text
;
4678 int retval
= parse_load_image_command_args(cmd_ctx
, args
, argc
,
4679 &image
, &min_address
, &max_address
);
4680 if (ERROR_OK
!= retval
)
4683 duration_start_measure(&duration
);
4685 if (image_open(&image
, args
[0], (argc
>= 3) ? args
[2] : NULL
) != ERROR_OK
)
4692 fastload_num
= image
.num_sections
;
4693 fastload
= (struct FastLoad
*)malloc(sizeof(struct FastLoad
)*image
.num_sections
);
4694 if (fastload
== NULL
)
4696 image_close(&image
);
4699 memset(fastload
, 0, sizeof(struct FastLoad
)*image
.num_sections
);
4700 for (i
= 0; i
< image
.num_sections
; i
++)
4702 buffer
= malloc(image
.sections
[i
].size
);
4705 command_print(cmd_ctx
, "error allocating buffer for section (%d bytes)",
4706 (int)(image
.sections
[i
].size
));
4710 if ((retval
= image_read_section(&image
, i
, 0x0, image
.sections
[i
].size
, buffer
, &buf_cnt
)) != ERROR_OK
)
4716 uint32_t offset
= 0;
4717 uint32_t length
= buf_cnt
;
4720 /* DANGER!!! beware of unsigned comparision here!!! */
4722 if ((image
.sections
[i
].base_address
+ buf_cnt
>= min_address
)&&
4723 (image
.sections
[i
].base_address
< max_address
))
4725 if (image
.sections
[i
].base_address
< min_address
)
4727 /* clip addresses below */
4728 offset
+= min_address
-image
.sections
[i
].base_address
;
4732 if (image
.sections
[i
].base_address
+ buf_cnt
> max_address
)
4734 length
-= (image
.sections
[i
].base_address
+ buf_cnt
)-max_address
;
4737 fastload
[i
].address
= image
.sections
[i
].base_address
+ offset
;
4738 fastload
[i
].data
= malloc(length
);
4739 if (fastload
[i
].data
== NULL
)
4744 memcpy(fastload
[i
].data
, buffer
+ offset
, length
);
4745 fastload
[i
].length
= length
;
4747 image_size
+= length
;
4748 command_print(cmd_ctx
, "%u bytes written at address 0x%8.8x",
4749 (unsigned int)length
,
4750 ((unsigned int)(image
.sections
[i
].base_address
+ offset
)));
4756 duration_stop_measure(&duration
, &duration_text
);
4757 if (retval
== ERROR_OK
)
4759 command_print(cmd_ctx
, "Loaded %u bytes in %s", (unsigned int)image_size
, duration_text
);
4760 command_print(cmd_ctx
, "NB!!! image has not been loaded to target, issue a subsequent 'fast_load' to do so.");
4762 free(duration_text
);
4764 image_close(&image
);
4766 if (retval
!= ERROR_OK
)
4774 static int handle_fast_load_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
4777 return ERROR_COMMAND_SYNTAX_ERROR
;
4778 if (fastload
== NULL
)
4780 LOG_ERROR("No image in memory");
4784 int ms
= timeval_ms();
4786 int retval
= ERROR_OK
;
4787 for (i
= 0; i
< fastload_num
;i
++)
4789 target_t
*target
= get_current_target(cmd_ctx
);
4790 command_print(cmd_ctx
, "Write to 0x%08x, length 0x%08x",
4791 (unsigned int)(fastload
[i
].address
),
4792 (unsigned int)(fastload
[i
].length
));
4793 if (retval
== ERROR_OK
)
4795 retval
= target_write_buffer(target
, fastload
[i
].address
, fastload
[i
].length
, fastload
[i
].data
);
4797 size
+= fastload
[i
].length
;
4799 int after
= timeval_ms();
4800 command_print(cmd_ctx
, "Loaded image %f kBytes/s", (float)(size
/1024.0)/((float)(after
-ms
)/1000.0));
4804 static int jim_mcrmrc(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4806 command_context_t
*context
;
4810 context
= Jim_GetAssocData(interp
, "context");
4811 if (context
== NULL
) {
4812 LOG_ERROR("array2mem: no command context");
4815 target
= get_current_target(context
);
4816 if (target
== NULL
) {
4817 LOG_ERROR("array2mem: no current target");
4821 if ((argc
< 6) || (argc
> 7))
4835 e
= Jim_GetLong(interp
, argv
[1], &l
);
4841 e
= Jim_GetLong(interp
, argv
[2], &l
);
4847 e
= Jim_GetLong(interp
, argv
[3], &l
);
4853 e
= Jim_GetLong(interp
, argv
[4], &l
);
4859 e
= Jim_GetLong(interp
, argv
[5], &l
);
4869 e
= Jim_GetLong(interp
, argv
[6], &l
);
4875 retval
= target_mcr(target
, cpnum
, op1
, op2
, CRn
, CRm
, value
);
4876 if (retval
!= ERROR_OK
)
4880 retval
= target_mrc(target
, cpnum
, op1
, op2
, CRn
, CRm
, &value
);
4881 if (retval
!= ERROR_OK
)
4884 Jim_SetResult(interp
, Jim_NewIntObj(interp
, value
));