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_mem2array(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
);
73 static int jim_target(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
);
75 static int target_array2mem(Jim_Interp
*interp
, target_t
*target
, int argc
, Jim_Obj
*const *argv
);
76 static int target_mem2array(Jim_Interp
*interp
, target_t
*target
, int argc
, Jim_Obj
*const *argv
);
79 extern target_type_t arm7tdmi_target
;
80 extern target_type_t arm720t_target
;
81 extern target_type_t arm9tdmi_target
;
82 extern target_type_t arm920t_target
;
83 extern target_type_t arm966e_target
;
84 extern target_type_t arm926ejs_target
;
85 extern target_type_t fa526_target
;
86 extern target_type_t feroceon_target
;
87 extern target_type_t dragonite_target
;
88 extern target_type_t xscale_target
;
89 extern target_type_t cortexm3_target
;
90 extern target_type_t cortexa8_target
;
91 extern target_type_t arm11_target
;
92 extern target_type_t mips_m4k_target
;
93 extern target_type_t avr_target
;
95 target_type_t
*target_types
[] =
115 target_t
*all_targets
= NULL
;
116 target_event_callback_t
*target_event_callbacks
= NULL
;
117 target_timer_callback_t
*target_timer_callbacks
= NULL
;
119 const Jim_Nvp nvp_assert
[] = {
120 { .name
= "assert", NVP_ASSERT
},
121 { .name
= "deassert", NVP_DEASSERT
},
122 { .name
= "T", NVP_ASSERT
},
123 { .name
= "F", NVP_DEASSERT
},
124 { .name
= "t", NVP_ASSERT
},
125 { .name
= "f", NVP_DEASSERT
},
126 { .name
= NULL
, .value
= -1 }
129 const Jim_Nvp nvp_error_target
[] = {
130 { .value
= ERROR_TARGET_INVALID
, .name
= "err-invalid" },
131 { .value
= ERROR_TARGET_INIT_FAILED
, .name
= "err-init-failed" },
132 { .value
= ERROR_TARGET_TIMEOUT
, .name
= "err-timeout" },
133 { .value
= ERROR_TARGET_NOT_HALTED
, .name
= "err-not-halted" },
134 { .value
= ERROR_TARGET_FAILURE
, .name
= "err-failure" },
135 { .value
= ERROR_TARGET_UNALIGNED_ACCESS
, .name
= "err-unaligned-access" },
136 { .value
= ERROR_TARGET_DATA_ABORT
, .name
= "err-data-abort" },
137 { .value
= ERROR_TARGET_RESOURCE_NOT_AVAILABLE
, .name
= "err-resource-not-available" },
138 { .value
= ERROR_TARGET_TRANSLATION_FAULT
, .name
= "err-translation-fault" },
139 { .value
= ERROR_TARGET_NOT_RUNNING
, .name
= "err-not-running" },
140 { .value
= ERROR_TARGET_NOT_EXAMINED
, .name
= "err-not-examined" },
141 { .value
= -1, .name
= NULL
}
144 const char *target_strerror_safe(int err
)
148 n
= Jim_Nvp_value2name_simple(nvp_error_target
, err
);
149 if (n
->name
== NULL
) {
156 static const Jim_Nvp nvp_target_event
[] = {
157 { .value
= TARGET_EVENT_OLD_gdb_program_config
, .name
= "old-gdb_program_config" },
158 { .value
= TARGET_EVENT_OLD_pre_resume
, .name
= "old-pre_resume" },
160 { .value
= TARGET_EVENT_GDB_HALT
, .name
= "gdb-halt" },
161 { .value
= TARGET_EVENT_HALTED
, .name
= "halted" },
162 { .value
= TARGET_EVENT_RESUMED
, .name
= "resumed" },
163 { .value
= TARGET_EVENT_RESUME_START
, .name
= "resume-start" },
164 { .value
= TARGET_EVENT_RESUME_END
, .name
= "resume-end" },
166 { .name
= "gdb-start", .value
= TARGET_EVENT_GDB_START
},
167 { .name
= "gdb-end", .value
= TARGET_EVENT_GDB_END
},
169 /* historical name */
171 { .value
= TARGET_EVENT_RESET_START
, .name
= "reset-start" },
173 { .value
= TARGET_EVENT_RESET_ASSERT_PRE
, .name
= "reset-assert-pre" },
174 { .value
= TARGET_EVENT_RESET_ASSERT_POST
, .name
= "reset-assert-post" },
175 { .value
= TARGET_EVENT_RESET_DEASSERT_PRE
, .name
= "reset-deassert-pre" },
176 { .value
= TARGET_EVENT_RESET_DEASSERT_POST
, .name
= "reset-deassert-post" },
177 { .value
= TARGET_EVENT_RESET_HALT_PRE
, .name
= "reset-halt-pre" },
178 { .value
= TARGET_EVENT_RESET_HALT_POST
, .name
= "reset-halt-post" },
179 { .value
= TARGET_EVENT_RESET_WAIT_PRE
, .name
= "reset-wait-pre" },
180 { .value
= TARGET_EVENT_RESET_WAIT_POST
, .name
= "reset-wait-post" },
181 { .value
= TARGET_EVENT_RESET_INIT
, .name
= "reset-init" },
182 { .value
= TARGET_EVENT_RESET_END
, .name
= "reset-end" },
184 { .value
= TARGET_EVENT_EXAMINE_START
, .name
= "examine-start" },
185 { .value
= TARGET_EVENT_EXAMINE_END
, .name
= "examine-end" },
187 { .value
= TARGET_EVENT_DEBUG_HALTED
, .name
= "debug-halted" },
188 { .value
= TARGET_EVENT_DEBUG_RESUMED
, .name
= "debug-resumed" },
190 { .value
= TARGET_EVENT_GDB_ATTACH
, .name
= "gdb-attach" },
191 { .value
= TARGET_EVENT_GDB_DETACH
, .name
= "gdb-detach" },
193 { .value
= TARGET_EVENT_GDB_FLASH_WRITE_START
, .name
= "gdb-flash-write-start" },
194 { .value
= TARGET_EVENT_GDB_FLASH_WRITE_END
, .name
= "gdb-flash-write-end" },
196 { .value
= TARGET_EVENT_GDB_FLASH_ERASE_START
, .name
= "gdb-flash-erase-start" },
197 { .value
= TARGET_EVENT_GDB_FLASH_ERASE_END
, .name
= "gdb-flash-erase-end" },
199 { .value
= TARGET_EVENT_RESUME_START
, .name
= "resume-start" },
200 { .value
= TARGET_EVENT_RESUMED
, .name
= "resume-ok" },
201 { .value
= TARGET_EVENT_RESUME_END
, .name
= "resume-end" },
203 { .name
= NULL
, .value
= -1 }
206 const Jim_Nvp nvp_target_state
[] = {
207 { .name
= "unknown", .value
= TARGET_UNKNOWN
},
208 { .name
= "running", .value
= TARGET_RUNNING
},
209 { .name
= "halted", .value
= TARGET_HALTED
},
210 { .name
= "reset", .value
= TARGET_RESET
},
211 { .name
= "debug-running", .value
= TARGET_DEBUG_RUNNING
},
212 { .name
= NULL
, .value
= -1 },
215 const Jim_Nvp nvp_target_debug_reason
[] = {
216 { .name
= "debug-request" , .value
= DBG_REASON_DBGRQ
},
217 { .name
= "breakpoint" , .value
= DBG_REASON_BREAKPOINT
},
218 { .name
= "watchpoint" , .value
= DBG_REASON_WATCHPOINT
},
219 { .name
= "watchpoint-and-breakpoint", .value
= DBG_REASON_WPTANDBKPT
},
220 { .name
= "single-step" , .value
= DBG_REASON_SINGLESTEP
},
221 { .name
= "target-not-halted" , .value
= DBG_REASON_NOTHALTED
},
222 { .name
= "undefined" , .value
= DBG_REASON_UNDEFINED
},
223 { .name
= NULL
, .value
= -1 },
226 const Jim_Nvp nvp_target_endian
[] = {
227 { .name
= "big", .value
= TARGET_BIG_ENDIAN
},
228 { .name
= "little", .value
= TARGET_LITTLE_ENDIAN
},
229 { .name
= "be", .value
= TARGET_BIG_ENDIAN
},
230 { .name
= "le", .value
= TARGET_LITTLE_ENDIAN
},
231 { .name
= NULL
, .value
= -1 },
234 const Jim_Nvp nvp_reset_modes
[] = {
235 { .name
= "unknown", .value
= RESET_UNKNOWN
},
236 { .name
= "run" , .value
= RESET_RUN
},
237 { .name
= "halt" , .value
= RESET_HALT
},
238 { .name
= "init" , .value
= RESET_INIT
},
239 { .name
= NULL
, .value
= -1 },
243 target_state_name( target_t
*t
)
246 cp
= Jim_Nvp_value2name_simple(nvp_target_state
, t
->state
)->name
;
248 LOG_ERROR("Invalid target state: %d", (int)(t
->state
));
249 cp
= "(*BUG*unknown*BUG*)";
254 /* determine the number of the new target */
255 static int new_target_number(void)
260 /* number is 0 based */
264 if (x
< t
->target_number
) {
265 x
= t
->target_number
;
272 /* read a uint32_t from a buffer in target memory endianness */
273 uint32_t target_buffer_get_u32(target_t
*target
, const uint8_t *buffer
)
275 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
276 return le_to_h_u32(buffer
);
278 return be_to_h_u32(buffer
);
281 /* read a uint16_t from a buffer in target memory endianness */
282 uint16_t target_buffer_get_u16(target_t
*target
, const uint8_t *buffer
)
284 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
285 return le_to_h_u16(buffer
);
287 return be_to_h_u16(buffer
);
290 /* read a uint8_t from a buffer in target memory endianness */
291 uint8_t target_buffer_get_u8(target_t
*target
, const uint8_t *buffer
)
293 return *buffer
& 0x0ff;
296 /* write a uint32_t to a buffer in target memory endianness */
297 void target_buffer_set_u32(target_t
*target
, uint8_t *buffer
, uint32_t value
)
299 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
300 h_u32_to_le(buffer
, value
);
302 h_u32_to_be(buffer
, value
);
305 /* write a uint16_t to a buffer in target memory endianness */
306 void target_buffer_set_u16(target_t
*target
, uint8_t *buffer
, uint16_t value
)
308 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
309 h_u16_to_le(buffer
, value
);
311 h_u16_to_be(buffer
, value
);
314 /* write a uint8_t to a buffer in target memory endianness */
315 void target_buffer_set_u8(target_t
*target
, uint8_t *buffer
, uint8_t value
)
320 /* return a pointer to a configured target; id is name or number */
321 target_t
*get_target(const char *id
)
325 /* try as tcltarget name */
326 for (target
= all_targets
; target
; target
= target
->next
) {
327 if (target
->cmd_name
== NULL
)
329 if (strcmp(id
, target
->cmd_name
) == 0)
333 /* It's OK to remove this fallback sometime after August 2010 or so */
335 /* no match, try as number */
337 if (parse_uint(id
, &num
) != ERROR_OK
)
340 for (target
= all_targets
; target
; target
= target
->next
) {
341 if (target
->target_number
== (int)num
) {
342 LOG_WARNING("use '%s' as target identifier, not '%u'",
343 target
->cmd_name
, num
);
351 /* returns a pointer to the n-th configured target */
352 static target_t
*get_target_by_num(int num
)
354 target_t
*target
= all_targets
;
357 if (target
->target_number
== num
) {
360 target
= target
->next
;
366 target_t
* get_current_target(command_context_t
*cmd_ctx
)
368 target_t
*target
= get_target_by_num(cmd_ctx
->current_target
);
372 LOG_ERROR("BUG: current_target out of bounds");
379 int target_poll(struct target_s
*target
)
381 /* We can't poll until after examine */
382 if (!target_was_examined(target
))
384 /* Fail silently lest we pollute the log */
387 return target
->type
->poll(target
);
390 int target_halt(struct target_s
*target
)
392 /* We can't poll until after examine */
393 if (!target_was_examined(target
))
395 LOG_ERROR("Target not examined yet");
398 return target
->type
->halt(target
);
401 int target_resume(struct target_s
*target
, int current
, uint32_t address
, int handle_breakpoints
, int debug_execution
)
405 /* We can't poll until after examine */
406 if (!target_was_examined(target
))
408 LOG_ERROR("Target not examined yet");
412 /* note that resume *must* be asynchronous. The CPU can halt before we poll. The CPU can
413 * even halt at the current PC as a result of a software breakpoint being inserted by (a bug?)
416 if ((retval
= target
->type
->resume(target
, current
, address
, handle_breakpoints
, debug_execution
)) != ERROR_OK
)
422 int target_process_reset(struct command_context_s
*cmd_ctx
, enum target_reset_mode reset_mode
)
427 n
= Jim_Nvp_value2name_simple(nvp_reset_modes
, reset_mode
);
428 if (n
->name
== NULL
) {
429 LOG_ERROR("invalid reset mode");
433 /* disable polling during reset to make reset event scripts
434 * more predictable, i.e. dr/irscan & pathmove in events will
435 * not have JTAG operations injected into the middle of a sequence.
437 bool save_poll
= jtag_poll_get_enabled();
439 jtag_poll_set_enabled(false);
441 sprintf(buf
, "ocd_process_reset %s", n
->name
);
442 retval
= Jim_Eval(interp
, buf
);
444 jtag_poll_set_enabled(save_poll
);
446 if (retval
!= JIM_OK
) {
447 Jim_PrintErrorMessage(interp
);
451 /* We want any events to be processed before the prompt */
452 retval
= target_call_timer_callbacks_now();
457 static int default_virt2phys(struct target_s
*target
, uint32_t virtual, uint32_t *physical
)
463 static int default_mmu(struct target_s
*target
, int *enabled
)
469 static int default_examine(struct target_s
*target
)
471 target_set_examined(target
);
475 int target_examine_one(struct target_s
*target
)
477 return target
->type
->examine(target
);
480 static int jtag_enable_callback(enum jtag_event event
, void *priv
)
482 target_t
*target
= priv
;
484 if (event
!= JTAG_TAP_EVENT_ENABLE
|| !target
->tap
->enabled
)
487 jtag_unregister_event_callback(jtag_enable_callback
, target
);
488 return target_examine_one(target
);
492 /* Targets that correctly implement init + examine, i.e.
493 * no communication with target during init:
497 int target_examine(void)
499 int retval
= ERROR_OK
;
502 for (target
= all_targets
; target
; target
= target
->next
)
504 /* defer examination, but don't skip it */
505 if (!target
->tap
->enabled
) {
506 jtag_register_event_callback(jtag_enable_callback
,
510 if ((retval
= target_examine_one(target
)) != ERROR_OK
)
515 const char *target_get_name(struct target_s
*target
)
517 return target
->type
->name
;
520 static int target_write_memory_imp(struct target_s
*target
, uint32_t address
, uint32_t size
, uint32_t count
, uint8_t *buffer
)
522 if (!target_was_examined(target
))
524 LOG_ERROR("Target not examined yet");
527 return target
->type
->write_memory_imp(target
, address
, size
, count
, buffer
);
530 static int target_read_memory_imp(struct target_s
*target
, uint32_t address
, uint32_t size
, uint32_t count
, uint8_t *buffer
)
532 if (!target_was_examined(target
))
534 LOG_ERROR("Target not examined yet");
537 return target
->type
->read_memory_imp(target
, address
, size
, count
, buffer
);
540 static int target_soft_reset_halt_imp(struct target_s
*target
)
542 if (!target_was_examined(target
))
544 LOG_ERROR("Target not examined yet");
547 if (!target
->type
->soft_reset_halt_imp
) {
548 LOG_ERROR("Target %s does not support soft_reset_halt",
552 return target
->type
->soft_reset_halt_imp(target
);
555 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
)
557 if (!target_was_examined(target
))
559 LOG_ERROR("Target not examined yet");
562 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
);
565 int target_read_memory(struct target_s
*target
,
566 uint32_t address
, uint32_t size
, uint32_t count
, uint8_t *buffer
)
568 return target
->type
->read_memory(target
, address
, size
, count
, buffer
);
571 int target_write_memory(struct target_s
*target
,
572 uint32_t address
, uint32_t size
, uint32_t count
, uint8_t *buffer
)
574 return target
->type
->write_memory(target
, address
, size
, count
, buffer
);
576 int target_bulk_write_memory(struct target_s
*target
,
577 uint32_t address
, uint32_t count
, uint8_t *buffer
)
579 return target
->type
->bulk_write_memory(target
, address
, count
, buffer
);
582 int target_add_breakpoint(struct target_s
*target
,
583 struct breakpoint_s
*breakpoint
)
585 return target
->type
->add_breakpoint(target
, breakpoint
);
587 int target_remove_breakpoint(struct target_s
*target
,
588 struct breakpoint_s
*breakpoint
)
590 return target
->type
->remove_breakpoint(target
, breakpoint
);
593 int target_add_watchpoint(struct target_s
*target
,
594 struct watchpoint_s
*watchpoint
)
596 return target
->type
->add_watchpoint(target
, watchpoint
);
598 int target_remove_watchpoint(struct target_s
*target
,
599 struct watchpoint_s
*watchpoint
)
601 return target
->type
->remove_watchpoint(target
, watchpoint
);
604 int target_get_gdb_reg_list(struct target_s
*target
,
605 struct reg_s
**reg_list
[], int *reg_list_size
)
607 return target
->type
->get_gdb_reg_list(target
, reg_list
, reg_list_size
);
609 int target_step(struct target_s
*target
,
610 int current
, uint32_t address
, int handle_breakpoints
)
612 return target
->type
->step(target
, current
, address
, handle_breakpoints
);
616 int target_run_algorithm(struct target_s
*target
,
617 int num_mem_params
, mem_param_t
*mem_params
,
618 int num_reg_params
, reg_param_t
*reg_param
,
619 uint32_t entry_point
, uint32_t exit_point
,
620 int timeout_ms
, void *arch_info
)
622 return target
->type
->run_algorithm(target
,
623 num_mem_params
, mem_params
, num_reg_params
, reg_param
,
624 entry_point
, exit_point
, timeout_ms
, arch_info
);
627 /// @returns @c true if the target has been examined.
628 bool target_was_examined(struct target_s
*target
)
630 return target
->type
->examined
;
632 /// Sets the @c examined flag for the given target.
633 void target_set_examined(struct target_s
*target
)
635 target
->type
->examined
= true;
637 // Reset the @c examined flag for the given target.
638 void target_reset_examined(struct target_s
*target
)
640 target
->type
->examined
= false;
644 int target_init(struct command_context_s
*cmd_ctx
)
646 target_t
*target
= all_targets
;
651 target_reset_examined(target
);
652 if (target
->type
->examine
== NULL
)
654 target
->type
->examine
= default_examine
;
657 if ((retval
= target
->type
->init_target(cmd_ctx
, target
)) != ERROR_OK
)
659 LOG_ERROR("target '%s' init failed", target_get_name(target
));
663 /* Set up default functions if none are provided by target */
664 if (target
->type
->virt2phys
== NULL
)
666 target
->type
->virt2phys
= default_virt2phys
;
668 target
->type
->virt2phys
= default_virt2phys
;
669 /* a non-invasive way(in terms of patches) to add some code that
670 * runs before the type->write/read_memory implementation
672 target
->type
->write_memory_imp
= target
->type
->write_memory
;
673 target
->type
->write_memory
= target_write_memory_imp
;
674 target
->type
->read_memory_imp
= target
->type
->read_memory
;
675 target
->type
->read_memory
= target_read_memory_imp
;
676 target
->type
->soft_reset_halt_imp
= target
->type
->soft_reset_halt
;
677 target
->type
->soft_reset_halt
= target_soft_reset_halt_imp
;
678 target
->type
->run_algorithm_imp
= target
->type
->run_algorithm
;
679 target
->type
->run_algorithm
= target_run_algorithm_imp
;
681 if (target
->type
->mmu
== NULL
)
683 target
->type
->mmu
= default_mmu
;
685 target
= target
->next
;
690 if ((retval
= target_register_user_commands(cmd_ctx
)) != ERROR_OK
)
692 if ((retval
= target_register_timer_callback(handle_target
, 100, 1, NULL
)) != ERROR_OK
)
699 int target_register_event_callback(int (*callback
)(struct target_s
*target
, enum target_event event
, void *priv
), void *priv
)
701 target_event_callback_t
**callbacks_p
= &target_event_callbacks
;
703 if (callback
== NULL
)
705 return ERROR_INVALID_ARGUMENTS
;
710 while ((*callbacks_p
)->next
)
711 callbacks_p
= &((*callbacks_p
)->next
);
712 callbacks_p
= &((*callbacks_p
)->next
);
715 (*callbacks_p
) = malloc(sizeof(target_event_callback_t
));
716 (*callbacks_p
)->callback
= callback
;
717 (*callbacks_p
)->priv
= priv
;
718 (*callbacks_p
)->next
= NULL
;
723 int target_register_timer_callback(int (*callback
)(void *priv
), int time_ms
, int periodic
, void *priv
)
725 target_timer_callback_t
**callbacks_p
= &target_timer_callbacks
;
728 if (callback
== NULL
)
730 return ERROR_INVALID_ARGUMENTS
;
735 while ((*callbacks_p
)->next
)
736 callbacks_p
= &((*callbacks_p
)->next
);
737 callbacks_p
= &((*callbacks_p
)->next
);
740 (*callbacks_p
) = malloc(sizeof(target_timer_callback_t
));
741 (*callbacks_p
)->callback
= callback
;
742 (*callbacks_p
)->periodic
= periodic
;
743 (*callbacks_p
)->time_ms
= time_ms
;
745 gettimeofday(&now
, NULL
);
746 (*callbacks_p
)->when
.tv_usec
= now
.tv_usec
+ (time_ms
% 1000) * 1000;
747 time_ms
-= (time_ms
% 1000);
748 (*callbacks_p
)->when
.tv_sec
= now
.tv_sec
+ (time_ms
/ 1000);
749 if ((*callbacks_p
)->when
.tv_usec
> 1000000)
751 (*callbacks_p
)->when
.tv_usec
= (*callbacks_p
)->when
.tv_usec
- 1000000;
752 (*callbacks_p
)->when
.tv_sec
+= 1;
755 (*callbacks_p
)->priv
= priv
;
756 (*callbacks_p
)->next
= NULL
;
761 int target_unregister_event_callback(int (*callback
)(struct target_s
*target
, enum target_event event
, void *priv
), void *priv
)
763 target_event_callback_t
**p
= &target_event_callbacks
;
764 target_event_callback_t
*c
= target_event_callbacks
;
766 if (callback
== NULL
)
768 return ERROR_INVALID_ARGUMENTS
;
773 target_event_callback_t
*next
= c
->next
;
774 if ((c
->callback
== callback
) && (c
->priv
== priv
))
788 int target_unregister_timer_callback(int (*callback
)(void *priv
), void *priv
)
790 target_timer_callback_t
**p
= &target_timer_callbacks
;
791 target_timer_callback_t
*c
= target_timer_callbacks
;
793 if (callback
== NULL
)
795 return ERROR_INVALID_ARGUMENTS
;
800 target_timer_callback_t
*next
= c
->next
;
801 if ((c
->callback
== callback
) && (c
->priv
== priv
))
815 int target_call_event_callbacks(target_t
*target
, enum target_event event
)
817 target_event_callback_t
*callback
= target_event_callbacks
;
818 target_event_callback_t
*next_callback
;
820 if (event
== TARGET_EVENT_HALTED
)
822 /* execute early halted first */
823 target_call_event_callbacks(target
, TARGET_EVENT_GDB_HALT
);
826 LOG_DEBUG("target event %i (%s)",
828 Jim_Nvp_value2name_simple(nvp_target_event
, event
)->name
);
830 target_handle_event(target
, event
);
834 next_callback
= callback
->next
;
835 callback
->callback(target
, event
, callback
->priv
);
836 callback
= next_callback
;
842 static int target_timer_callback_periodic_restart(
843 target_timer_callback_t
*cb
, struct timeval
*now
)
845 int time_ms
= cb
->time_ms
;
846 cb
->when
.tv_usec
= now
->tv_usec
+ (time_ms
% 1000) * 1000;
847 time_ms
-= (time_ms
% 1000);
848 cb
->when
.tv_sec
= now
->tv_sec
+ time_ms
/ 1000;
849 if (cb
->when
.tv_usec
> 1000000)
851 cb
->when
.tv_usec
= cb
->when
.tv_usec
- 1000000;
852 cb
->when
.tv_sec
+= 1;
857 static int target_call_timer_callback(target_timer_callback_t
*cb
,
860 cb
->callback(cb
->priv
);
863 return target_timer_callback_periodic_restart(cb
, now
);
865 return target_unregister_timer_callback(cb
->callback
, cb
->priv
);
868 static int target_call_timer_callbacks_check_time(int checktime
)
873 gettimeofday(&now
, NULL
);
875 target_timer_callback_t
*callback
= target_timer_callbacks
;
878 // cleaning up may unregister and free this callback
879 target_timer_callback_t
*next_callback
= callback
->next
;
881 bool call_it
= callback
->callback
&&
882 ((!checktime
&& callback
->periodic
) ||
883 now
.tv_sec
> callback
->when
.tv_sec
||
884 (now
.tv_sec
== callback
->when
.tv_sec
&&
885 now
.tv_usec
>= callback
->when
.tv_usec
));
889 int retval
= target_call_timer_callback(callback
, &now
);
890 if (retval
!= ERROR_OK
)
894 callback
= next_callback
;
900 int target_call_timer_callbacks(void)
902 return target_call_timer_callbacks_check_time(1);
905 /* invoke periodic callbacks immediately */
906 int target_call_timer_callbacks_now(void)
908 return target_call_timer_callbacks_check_time(0);
911 int target_alloc_working_area(struct target_s
*target
, uint32_t size
, working_area_t
**area
)
913 working_area_t
*c
= target
->working_areas
;
914 working_area_t
*new_wa
= NULL
;
916 /* Reevaluate working area address based on MMU state*/
917 if (target
->working_areas
== NULL
)
921 retval
= target
->type
->mmu(target
, &enabled
);
922 if (retval
!= ERROR_OK
)
928 target
->working_area
= target
->working_area_virt
;
932 target
->working_area
= target
->working_area_phys
;
936 /* only allocate multiples of 4 byte */
939 LOG_ERROR("BUG: code tried to allocate unaligned number of bytes (0x%08x), padding", ((unsigned)(size
)));
940 size
= (size
+ 3) & (~3);
943 /* see if there's already a matching working area */
946 if ((c
->free
) && (c
->size
== size
))
954 /* if not, allocate a new one */
957 working_area_t
**p
= &target
->working_areas
;
958 uint32_t first_free
= target
->working_area
;
959 uint32_t free_size
= target
->working_area_size
;
961 LOG_DEBUG("allocating new working area");
963 c
= target
->working_areas
;
966 first_free
+= c
->size
;
967 free_size
-= c
->size
;
972 if (free_size
< size
)
974 LOG_WARNING("not enough working area available(requested %u, free %u)",
975 (unsigned)(size
), (unsigned)(free_size
));
976 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
979 new_wa
= malloc(sizeof(working_area_t
));
982 new_wa
->address
= first_free
;
984 if (target
->backup_working_area
)
987 new_wa
->backup
= malloc(new_wa
->size
);
988 if ((retval
= target_read_memory(target
, new_wa
->address
, 4, new_wa
->size
/ 4, new_wa
->backup
)) != ERROR_OK
)
990 free(new_wa
->backup
);
997 new_wa
->backup
= NULL
;
1000 /* put new entry in list */
1004 /* mark as used, and return the new (reused) area */
1009 new_wa
->user
= area
;
1014 int target_free_working_area_restore(struct target_s
*target
, working_area_t
*area
, int restore
)
1019 if (restore
&& target
->backup_working_area
)
1022 if ((retval
= target_write_memory(target
, area
->address
, 4, area
->size
/ 4, area
->backup
)) != ERROR_OK
)
1028 /* mark user pointer invalid */
1035 int target_free_working_area(struct target_s
*target
, working_area_t
*area
)
1037 return target_free_working_area_restore(target
, area
, 1);
1040 /* free resources and restore memory, if restoring memory fails,
1041 * free up resources anyway
1043 void target_free_all_working_areas_restore(struct target_s
*target
, int restore
)
1045 working_area_t
*c
= target
->working_areas
;
1049 working_area_t
*next
= c
->next
;
1050 target_free_working_area_restore(target
, c
, restore
);
1060 target
->working_areas
= NULL
;
1063 void target_free_all_working_areas(struct target_s
*target
)
1065 target_free_all_working_areas_restore(target
, 1);
1068 int target_register_commands(struct command_context_s
*cmd_ctx
)
1071 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)");
1076 register_jim(cmd_ctx
, "target", jim_target
, "configure target");
1081 int target_arch_state(struct target_s
*target
)
1086 LOG_USER("No target has been configured");
1090 LOG_USER("target state: %s", target_state_name( target
));
1092 if (target
->state
!= TARGET_HALTED
)
1095 retval
= target
->type
->arch_state(target
);
1099 /* Single aligned words are guaranteed to use 16 or 32 bit access
1100 * mode respectively, otherwise data is handled as quickly as
1103 int target_write_buffer(struct target_s
*target
, uint32_t address
, uint32_t size
, uint8_t *buffer
)
1106 LOG_DEBUG("writing buffer of %i byte at 0x%8.8x",
1107 (int)size
, (unsigned)address
);
1109 if (!target_was_examined(target
))
1111 LOG_ERROR("Target not examined yet");
1119 if ((address
+ size
- 1) < address
)
1121 /* GDB can request this when e.g. PC is 0xfffffffc*/
1122 LOG_ERROR("address + size wrapped(0x%08x, 0x%08x)",
1128 if (((address
% 2) == 0) && (size
== 2))
1130 return target_write_memory(target
, address
, 2, 1, buffer
);
1133 /* handle unaligned head bytes */
1136 uint32_t unaligned
= 4 - (address
% 4);
1138 if (unaligned
> size
)
1141 if ((retval
= target_write_memory(target
, address
, 1, unaligned
, buffer
)) != ERROR_OK
)
1144 buffer
+= unaligned
;
1145 address
+= unaligned
;
1149 /* handle aligned words */
1152 int aligned
= size
- (size
% 4);
1154 /* use bulk writes above a certain limit. This may have to be changed */
1157 if ((retval
= target
->type
->bulk_write_memory(target
, address
, aligned
/ 4, buffer
)) != ERROR_OK
)
1162 if ((retval
= target_write_memory(target
, address
, 4, aligned
/ 4, buffer
)) != ERROR_OK
)
1171 /* handle tail writes of less than 4 bytes */
1174 if ((retval
= target_write_memory(target
, address
, 1, size
, buffer
)) != ERROR_OK
)
1181 /* Single aligned words are guaranteed to use 16 or 32 bit access
1182 * mode respectively, otherwise data is handled as quickly as
1185 int target_read_buffer(struct target_s
*target
, uint32_t address
, uint32_t size
, uint8_t *buffer
)
1188 LOG_DEBUG("reading buffer of %i byte at 0x%8.8x",
1189 (int)size
, (unsigned)address
);
1191 if (!target_was_examined(target
))
1193 LOG_ERROR("Target not examined yet");
1201 if ((address
+ size
- 1) < address
)
1203 /* GDB can request this when e.g. PC is 0xfffffffc*/
1204 LOG_ERROR("address + size wrapped(0x%08" PRIx32
", 0x%08" PRIx32
")",
1210 if (((address
% 2) == 0) && (size
== 2))
1212 return target_read_memory(target
, address
, 2, 1, buffer
);
1215 /* handle unaligned head bytes */
1218 uint32_t unaligned
= 4 - (address
% 4);
1220 if (unaligned
> size
)
1223 if ((retval
= target_read_memory(target
, address
, 1, unaligned
, buffer
)) != ERROR_OK
)
1226 buffer
+= unaligned
;
1227 address
+= unaligned
;
1231 /* handle aligned words */
1234 int aligned
= size
- (size
% 4);
1236 if ((retval
= target_read_memory(target
, address
, 4, aligned
/ 4, buffer
)) != ERROR_OK
)
1244 /*prevent byte access when possible (avoid AHB access limitations in some cases)*/
1247 int aligned
= size
- (size
%2);
1248 retval
= target_read_memory(target
, address
, 2, aligned
/ 2, buffer
);
1249 if (retval
!= ERROR_OK
)
1256 /* handle tail writes of less than 4 bytes */
1259 if ((retval
= target_read_memory(target
, address
, 1, size
, buffer
)) != ERROR_OK
)
1266 int target_checksum_memory(struct target_s
*target
, uint32_t address
, uint32_t size
, uint32_t* crc
)
1271 uint32_t checksum
= 0;
1272 if (!target_was_examined(target
))
1274 LOG_ERROR("Target not examined yet");
1278 if ((retval
= target
->type
->checksum_memory(target
, address
,
1279 size
, &checksum
)) != ERROR_OK
)
1281 buffer
= malloc(size
);
1284 LOG_ERROR("error allocating buffer for section (%d bytes)", (int)size
);
1285 return ERROR_INVALID_ARGUMENTS
;
1287 retval
= target_read_buffer(target
, address
, size
, buffer
);
1288 if (retval
!= ERROR_OK
)
1294 /* convert to target endianess */
1295 for (i
= 0; i
< (size
/sizeof(uint32_t)); i
++)
1297 uint32_t target_data
;
1298 target_data
= target_buffer_get_u32(target
, &buffer
[i
*sizeof(uint32_t)]);
1299 target_buffer_set_u32(target
, &buffer
[i
*sizeof(uint32_t)], target_data
);
1302 retval
= image_calculate_checksum(buffer
, size
, &checksum
);
1311 int target_blank_check_memory(struct target_s
*target
, uint32_t address
, uint32_t size
, uint32_t* blank
)
1314 if (!target_was_examined(target
))
1316 LOG_ERROR("Target not examined yet");
1320 if (target
->type
->blank_check_memory
== 0)
1321 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
1323 retval
= target
->type
->blank_check_memory(target
, address
, size
, blank
);
1328 int target_read_u32(struct target_s
*target
, uint32_t address
, uint32_t *value
)
1330 uint8_t value_buf
[4];
1331 if (!target_was_examined(target
))
1333 LOG_ERROR("Target not examined yet");
1337 int retval
= target_read_memory(target
, address
, 4, 1, value_buf
);
1339 if (retval
== ERROR_OK
)
1341 *value
= target_buffer_get_u32(target
, value_buf
);
1342 LOG_DEBUG("address: 0x%8.8" PRIx32
", value: 0x%8.8" PRIx32
"",
1349 LOG_DEBUG("address: 0x%8.8" PRIx32
" failed",
1356 int target_read_u16(struct target_s
*target
, uint32_t address
, uint16_t *value
)
1358 uint8_t value_buf
[2];
1359 if (!target_was_examined(target
))
1361 LOG_ERROR("Target not examined yet");
1365 int retval
= target_read_memory(target
, address
, 2, 1, value_buf
);
1367 if (retval
== ERROR_OK
)
1369 *value
= target_buffer_get_u16(target
, value_buf
);
1370 LOG_DEBUG("address: 0x%8.8" PRIx32
", value: 0x%4.4x",
1377 LOG_DEBUG("address: 0x%8.8" PRIx32
" failed",
1384 int target_read_u8(struct target_s
*target
, uint32_t address
, uint8_t *value
)
1386 int retval
= target_read_memory(target
, address
, 1, 1, value
);
1387 if (!target_was_examined(target
))
1389 LOG_ERROR("Target not examined yet");
1393 if (retval
== ERROR_OK
)
1395 LOG_DEBUG("address: 0x%8.8" PRIx32
", value: 0x%2.2x",
1402 LOG_DEBUG("address: 0x%8.8" PRIx32
" failed",
1409 int target_write_u32(struct target_s
*target
, uint32_t address
, uint32_t value
)
1412 uint8_t value_buf
[4];
1413 if (!target_was_examined(target
))
1415 LOG_ERROR("Target not examined yet");
1419 LOG_DEBUG("address: 0x%8.8" PRIx32
", value: 0x%8.8" PRIx32
"",
1423 target_buffer_set_u32(target
, value_buf
, value
);
1424 if ((retval
= target_write_memory(target
, address
, 4, 1, value_buf
)) != ERROR_OK
)
1426 LOG_DEBUG("failed: %i", retval
);
1432 int target_write_u16(struct target_s
*target
, uint32_t address
, uint16_t value
)
1435 uint8_t value_buf
[2];
1436 if (!target_was_examined(target
))
1438 LOG_ERROR("Target not examined yet");
1442 LOG_DEBUG("address: 0x%8.8" PRIx32
", value: 0x%8.8x",
1446 target_buffer_set_u16(target
, value_buf
, value
);
1447 if ((retval
= target_write_memory(target
, address
, 2, 1, value_buf
)) != ERROR_OK
)
1449 LOG_DEBUG("failed: %i", retval
);
1455 int target_write_u8(struct target_s
*target
, uint32_t address
, uint8_t value
)
1458 if (!target_was_examined(target
))
1460 LOG_ERROR("Target not examined yet");
1464 LOG_DEBUG("address: 0x%8.8" PRIx32
", value: 0x%2.2x",
1467 if ((retval
= target_write_memory(target
, address
, 1, 1, &value
)) != ERROR_OK
)
1469 LOG_DEBUG("failed: %i", retval
);
1475 int target_register_user_commands(struct command_context_s
*cmd_ctx
)
1477 int retval
= ERROR_OK
;
1480 /* script procedures */
1481 register_command(cmd_ctx
, NULL
, "profile", handle_profile_command
, COMMAND_EXEC
, "profiling samples the CPU PC");
1482 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>");
1483 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>");
1485 register_command(cmd_ctx
, NULL
, "fast_load_image", handle_fast_load_image_command
, COMMAND_ANY
,
1486 "same args as load_image, image stored in memory - mainly for profiling purposes");
1488 register_command(cmd_ctx
, NULL
, "fast_load", handle_fast_load_command
, COMMAND_ANY
,
1489 "loads active fast load image to current target - mainly for profiling purposes");
1492 register_command(cmd_ctx
, NULL
, "virt2phys", handle_virt2phys_command
, COMMAND_ANY
, "translate a virtual address into a physical address");
1493 register_command(cmd_ctx
, NULL
, "reg", handle_reg_command
, COMMAND_EXEC
, "display or set a register");
1494 register_command(cmd_ctx
, NULL
, "poll", handle_poll_command
, COMMAND_EXEC
, "poll target state");
1495 register_command(cmd_ctx
, NULL
, "wait_halt", handle_wait_halt_command
, COMMAND_EXEC
, "wait for target halt [time (s)]");
1496 register_command(cmd_ctx
, NULL
, "halt", handle_halt_command
, COMMAND_EXEC
, "halt target");
1497 register_command(cmd_ctx
, NULL
, "resume", handle_resume_command
, COMMAND_EXEC
, "resume target [addr]");
1498 register_command(cmd_ctx
, NULL
, "step", handle_step_command
, COMMAND_EXEC
, "step one instruction from current PC or [addr]");
1499 register_command(cmd_ctx
, NULL
, "reset", handle_reset_command
, COMMAND_EXEC
, "reset target [run | halt | init] - default is run");
1500 register_command(cmd_ctx
, NULL
, "soft_reset_halt", handle_soft_reset_halt_command
, COMMAND_EXEC
, "halt the target and do a soft reset");
1502 register_command(cmd_ctx
, NULL
, "mdw", handle_md_command
, COMMAND_EXEC
, "display memory words <addr> [count]");
1503 register_command(cmd_ctx
, NULL
, "mdh", handle_md_command
, COMMAND_EXEC
, "display memory half-words <addr> [count]");
1504 register_command(cmd_ctx
, NULL
, "mdb", handle_md_command
, COMMAND_EXEC
, "display memory bytes <addr> [count]");
1506 register_command(cmd_ctx
, NULL
, "mww", handle_mw_command
, COMMAND_EXEC
, "write memory word <addr> <value> [count]");
1507 register_command(cmd_ctx
, NULL
, "mwh", handle_mw_command
, COMMAND_EXEC
, "write memory half-word <addr> <value> [count]");
1508 register_command(cmd_ctx
, NULL
, "mwb", handle_mw_command
, COMMAND_EXEC
, "write memory byte <addr> <value> [count]");
1510 register_command(cmd_ctx
, NULL
, "bp",
1511 handle_bp_command
, COMMAND_EXEC
,
1512 "list or set breakpoint [<address> <length> [hw]]");
1513 register_command(cmd_ctx
, NULL
, "rbp",
1514 handle_rbp_command
, COMMAND_EXEC
,
1515 "remove breakpoint <address>");
1516 register_command(cmd_ctx
, NULL
, "wp",
1517 handle_wp_command
, COMMAND_EXEC
,
1518 "list or set watchpoint "
1519 "[<address> <length> <r/w/a> [value] [mask]]");
1520 register_command(cmd_ctx
, NULL
, "rwp",
1521 handle_rwp_command
, COMMAND_EXEC
,
1522 "remove watchpoint <address>");
1524 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]");
1525 register_command(cmd_ctx
, NULL
, "dump_image", handle_dump_image_command
, COMMAND_EXEC
, "dump_image <file> <address> <size>");
1526 register_command(cmd_ctx
, NULL
, "verify_image", handle_verify_image_command
, COMMAND_EXEC
, "verify_image <file> [offset] [type]");
1527 register_command(cmd_ctx
, NULL
, "test_image", handle_test_image_command
, COMMAND_EXEC
, "test_image <file> [offset] [type]");
1529 if ((retval
= target_request_register_commands(cmd_ctx
)) != ERROR_OK
)
1531 if ((retval
= trace_register_commands(cmd_ctx
)) != ERROR_OK
)
1537 static int handle_targets_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
1539 target_t
*target
= all_targets
;
1543 target
= get_target(args
[0]);
1544 if (target
== NULL
) {
1545 command_print(cmd_ctx
,"Target: %s is unknown, try one of:\n", args
[0]);
1548 if (!target
->tap
->enabled
) {
1549 command_print(cmd_ctx
,"Target: TAP %s is disabled, "
1550 "can't be the current target\n",
1551 target
->tap
->dotted_name
);
1555 cmd_ctx
->current_target
= target
->target_number
;
1560 target
= all_targets
;
1561 command_print(cmd_ctx
, " TargetName Type Endian TapName State ");
1562 command_print(cmd_ctx
, "-- ------------------ ---------- ------ ------------------ ------------");
1568 if (target
->tap
->enabled
)
1569 state
= target_state_name( target
);
1571 state
= "tap-disabled";
1573 if (cmd_ctx
->current_target
== target
->target_number
)
1576 /* keep columns lined up to match the headers above */
1577 command_print(cmd_ctx
, "%2d%c %-18s %-10s %-6s %-18s %s",
1578 target
->target_number
,
1581 target_get_name(target
),
1582 Jim_Nvp_value2name_simple(nvp_target_endian
,
1583 target
->endianness
)->name
,
1584 target
->tap
->dotted_name
,
1586 target
= target
->next
;
1592 /* every 300ms we check for reset & powerdropout and issue a "reset halt" if so. */
1594 static int powerDropout
;
1595 static int srstAsserted
;
1597 static int runPowerRestore
;
1598 static int runPowerDropout
;
1599 static int runSrstAsserted
;
1600 static int runSrstDeasserted
;
1602 static int sense_handler(void)
1604 static int prevSrstAsserted
= 0;
1605 static int prevPowerdropout
= 0;
1608 if ((retval
= jtag_power_dropout(&powerDropout
)) != ERROR_OK
)
1612 powerRestored
= prevPowerdropout
&& !powerDropout
;
1615 runPowerRestore
= 1;
1618 long long current
= timeval_ms();
1619 static long long lastPower
= 0;
1620 int waitMore
= lastPower
+ 2000 > current
;
1621 if (powerDropout
&& !waitMore
)
1623 runPowerDropout
= 1;
1624 lastPower
= current
;
1627 if ((retval
= jtag_srst_asserted(&srstAsserted
)) != ERROR_OK
)
1631 srstDeasserted
= prevSrstAsserted
&& !srstAsserted
;
1633 static long long lastSrst
= 0;
1634 waitMore
= lastSrst
+ 2000 > current
;
1635 if (srstDeasserted
&& !waitMore
)
1637 runSrstDeasserted
= 1;
1641 if (!prevSrstAsserted
&& srstAsserted
)
1643 runSrstAsserted
= 1;
1646 prevSrstAsserted
= srstAsserted
;
1647 prevPowerdropout
= powerDropout
;
1649 if (srstDeasserted
|| powerRestored
)
1651 /* Other than logging the event we can't do anything here.
1652 * Issuing a reset is a particularly bad idea as we might
1653 * be inside a reset already.
1660 static void target_call_event_callbacks_all(enum target_event e
) {
1662 target
= all_targets
;
1664 target_call_event_callbacks(target
, e
);
1665 target
= target
->next
;
1669 /* process target state changes */
1670 int handle_target(void *priv
)
1672 int retval
= ERROR_OK
;
1674 /* we do not want to recurse here... */
1675 static int recursive
= 0;
1680 /* danger! running these procedures can trigger srst assertions and power dropouts.
1681 * We need to avoid an infinite loop/recursion here and we do that by
1682 * clearing the flags after running these events.
1684 int did_something
= 0;
1685 if (runSrstAsserted
)
1687 target_call_event_callbacks_all(TARGET_EVENT_GDB_HALT
);
1688 Jim_Eval(interp
, "srst_asserted");
1691 if (runSrstDeasserted
)
1693 Jim_Eval(interp
, "srst_deasserted");
1696 if (runPowerDropout
)
1698 target_call_event_callbacks_all(TARGET_EVENT_GDB_HALT
);
1699 Jim_Eval(interp
, "power_dropout");
1702 if (runPowerRestore
)
1704 Jim_Eval(interp
, "power_restore");
1710 /* clear detect flags */
1714 /* clear action flags */
1716 runSrstAsserted
= 0;
1717 runSrstDeasserted
= 0;
1718 runPowerRestore
= 0;
1719 runPowerDropout
= 0;
1724 /* Poll targets for state changes unless that's globally disabled.
1725 * Skip targets that are currently disabled.
1727 for (target_t
*target
= all_targets
;
1728 is_jtag_poll_safe() && target
;
1729 target
= target
->next
)
1731 if (!target
->tap
->enabled
)
1734 /* only poll target if we've got power and srst isn't asserted */
1735 if (!powerDropout
&& !srstAsserted
)
1737 /* polling may fail silently until the target has been examined */
1738 if ((retval
= target_poll(target
)) != ERROR_OK
)
1740 target_call_event_callbacks(target
, TARGET_EVENT_GDB_HALT
);
1749 static int handle_reg_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
1758 target
= get_current_target(cmd_ctx
);
1760 /* list all available registers for the current target */
1763 reg_cache_t
*cache
= target
->reg_cache
;
1770 for (i
= 0, reg
= cache
->reg_list
;
1771 i
< cache
->num_regs
;
1772 i
++, reg
++, count
++)
1774 /* only print cached values if they are valid */
1776 value
= buf_to_str(reg
->value
,
1778 command_print(cmd_ctx
,
1779 "(%i) %s (/%" PRIu32
"): 0x%s%s",
1787 command_print(cmd_ctx
, "(%i) %s (/%" PRIu32
")",
1792 cache
= cache
->next
;
1798 /* access a single register by its ordinal number */
1799 if ((args
[0][0] >= '0') && (args
[0][0] <= '9'))
1802 int retval
= parse_uint(args
[0], &num
);
1803 if (ERROR_OK
!= retval
)
1804 return ERROR_COMMAND_SYNTAX_ERROR
;
1806 reg_cache_t
*cache
= target
->reg_cache
;
1811 for (i
= 0; i
< cache
->num_regs
; i
++)
1813 if (count
++ == (int)num
)
1815 reg
= &cache
->reg_list
[i
];
1821 cache
= cache
->next
;
1826 command_print(cmd_ctx
, "%i is out of bounds, the current target has only %i registers (0 - %i)", num
, count
, count
- 1);
1829 } else /* access a single register by its name */
1831 reg
= register_get_by_name(target
->reg_cache
, args
[0], 1);
1835 command_print(cmd_ctx
, "register %s not found in current target", args
[0]);
1840 /* display a register */
1841 if ((argc
== 1) || ((argc
== 2) && !((args
[1][0] >= '0') && (args
[1][0] <= '9'))))
1843 if ((argc
== 2) && (strcmp(args
[1], "force") == 0))
1846 if (reg
->valid
== 0)
1848 reg_arch_type_t
*arch_type
= register_get_arch_type(reg
->arch_type
);
1849 arch_type
->get(reg
);
1851 value
= buf_to_str(reg
->value
, reg
->size
, 16);
1852 command_print(cmd_ctx
, "%s (/%i): 0x%s", reg
->name
, (int)(reg
->size
), value
);
1857 /* set register value */
1860 uint8_t *buf
= malloc(CEIL(reg
->size
, 8));
1861 str_to_buf(args
[1], strlen(args
[1]), buf
, reg
->size
, 0);
1863 reg_arch_type_t
*arch_type
= register_get_arch_type(reg
->arch_type
);
1864 arch_type
->set(reg
, buf
);
1866 value
= buf_to_str(reg
->value
, reg
->size
, 16);
1867 command_print(cmd_ctx
, "%s (/%i): 0x%s", reg
->name
, (int)(reg
->size
), value
);
1875 command_print(cmd_ctx
, "usage: reg <#|name> [value]");
1880 static int handle_poll_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
1882 int retval
= ERROR_OK
;
1883 target_t
*target
= get_current_target(cmd_ctx
);
1887 command_print(cmd_ctx
, "background polling: %s",
1888 jtag_poll_get_enabled() ? "on" : "off");
1889 command_print(cmd_ctx
, "TAP: %s (%s)",
1890 target
->tap
->dotted_name
,
1891 target
->tap
->enabled
? "enabled" : "disabled");
1892 if (!target
->tap
->enabled
)
1894 if ((retval
= target_poll(target
)) != ERROR_OK
)
1896 if ((retval
= target_arch_state(target
)) != ERROR_OK
)
1902 if (strcmp(args
[0], "on") == 0)
1904 jtag_poll_set_enabled(true);
1906 else if (strcmp(args
[0], "off") == 0)
1908 jtag_poll_set_enabled(false);
1912 command_print(cmd_ctx
, "arg is \"on\" or \"off\"");
1916 return ERROR_COMMAND_SYNTAX_ERROR
;
1922 static int handle_wait_halt_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
1925 return ERROR_COMMAND_SYNTAX_ERROR
;
1930 int retval
= parse_uint(args
[0], &ms
);
1931 if (ERROR_OK
!= retval
)
1933 command_print(cmd_ctx
, "usage: %s [seconds]", cmd
);
1934 return ERROR_COMMAND_SYNTAX_ERROR
;
1936 // convert seconds (given) to milliseconds (needed)
1940 target_t
*target
= get_current_target(cmd_ctx
);
1941 return target_wait_state(target
, TARGET_HALTED
, ms
);
1944 /* wait for target state to change. The trick here is to have a low
1945 * latency for short waits and not to suck up all the CPU time
1948 * After 500ms, keep_alive() is invoked
1950 int target_wait_state(target_t
*target
, enum target_state state
, int ms
)
1953 long long then
= 0, cur
;
1958 if ((retval
= target_poll(target
)) != ERROR_OK
)
1960 if (target
->state
== state
)
1968 then
= timeval_ms();
1969 LOG_DEBUG("waiting for target %s...",
1970 Jim_Nvp_value2name_simple(nvp_target_state
,state
)->name
);
1978 if ((cur
-then
) > ms
)
1980 LOG_ERROR("timed out while waiting for target %s",
1981 Jim_Nvp_value2name_simple(nvp_target_state
,state
)->name
);
1989 static int handle_halt_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
1993 target_t
*target
= get_current_target(cmd_ctx
);
1994 int retval
= target_halt(target
);
1995 if (ERROR_OK
!= retval
)
2001 retval
= parse_uint(args
[0], &wait
);
2002 if (ERROR_OK
!= retval
)
2003 return ERROR_COMMAND_SYNTAX_ERROR
;
2008 return handle_wait_halt_command(cmd_ctx
, cmd
, args
, argc
);
2011 static int handle_soft_reset_halt_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
2013 target_t
*target
= get_current_target(cmd_ctx
);
2015 LOG_USER("requesting target halt and executing a soft reset");
2017 target
->type
->soft_reset_halt(target
);
2022 static int handle_reset_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
2025 return ERROR_COMMAND_SYNTAX_ERROR
;
2027 enum target_reset_mode reset_mode
= RESET_RUN
;
2031 n
= Jim_Nvp_name2value_simple(nvp_reset_modes
, args
[0]);
2032 if ((n
->name
== NULL
) || (n
->value
== RESET_UNKNOWN
)) {
2033 return ERROR_COMMAND_SYNTAX_ERROR
;
2035 reset_mode
= n
->value
;
2038 /* reset *all* targets */
2039 return target_process_reset(cmd_ctx
, reset_mode
);
2043 static int handle_resume_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
2047 return ERROR_COMMAND_SYNTAX_ERROR
;
2049 target_t
*target
= get_current_target(cmd_ctx
);
2050 target_handle_event(target
, TARGET_EVENT_OLD_pre_resume
);
2052 /* with no args, resume from current pc, addr = 0,
2053 * with one arguments, addr = args[0],
2054 * handle breakpoints, not debugging */
2058 int retval
= parse_u32(args
[0], &addr
);
2059 if (ERROR_OK
!= retval
)
2064 return target_resume(target
, current
, addr
, 1, 0);
2067 static int handle_step_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
2070 return ERROR_COMMAND_SYNTAX_ERROR
;
2074 /* with no args, step from current pc, addr = 0,
2075 * with one argument addr = args[0],
2076 * handle breakpoints, debugging */
2081 int retval
= parse_u32(args
[0], &addr
);
2082 if (ERROR_OK
!= retval
)
2087 target_t
*target
= get_current_target(cmd_ctx
);
2089 return target
->type
->step(target
, current_pc
, addr
, 1);
2092 static void handle_md_output(struct command_context_s
*cmd_ctx
,
2093 struct target_s
*target
, uint32_t address
, unsigned size
,
2094 unsigned count
, const uint8_t *buffer
)
2096 const unsigned line_bytecnt
= 32;
2097 unsigned line_modulo
= line_bytecnt
/ size
;
2099 char output
[line_bytecnt
* 4 + 1];
2100 unsigned output_len
= 0;
2102 const char *value_fmt
;
2104 case 4: value_fmt
= "%8.8x "; break;
2105 case 2: value_fmt
= "%4.2x "; break;
2106 case 1: value_fmt
= "%2.2x "; break;
2108 LOG_ERROR("invalid memory read size: %u", size
);
2112 for (unsigned i
= 0; i
< count
; i
++)
2114 if (i
% line_modulo
== 0)
2116 output_len
+= snprintf(output
+ output_len
,
2117 sizeof(output
) - output_len
,
2119 (unsigned)(address
+ (i
*size
)));
2123 const uint8_t *value_ptr
= buffer
+ i
* size
;
2125 case 4: value
= target_buffer_get_u32(target
, value_ptr
); break;
2126 case 2: value
= target_buffer_get_u16(target
, value_ptr
); break;
2127 case 1: value
= *value_ptr
;
2129 output_len
+= snprintf(output
+ output_len
,
2130 sizeof(output
) - output_len
,
2133 if ((i
% line_modulo
== line_modulo
- 1) || (i
== count
- 1))
2135 command_print(cmd_ctx
, "%s", output
);
2141 static int handle_md_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
2144 return ERROR_COMMAND_SYNTAX_ERROR
;
2148 case 'w': size
= 4; break;
2149 case 'h': size
= 2; break;
2150 case 'b': size
= 1; break;
2151 default: return ERROR_COMMAND_SYNTAX_ERROR
;
2155 int retval
= parse_u32(args
[0], &address
);
2156 if (ERROR_OK
!= retval
)
2162 retval
= parse_uint(args
[1], &count
);
2163 if (ERROR_OK
!= retval
)
2167 uint8_t *buffer
= calloc(count
, size
);
2169 target_t
*target
= get_current_target(cmd_ctx
);
2170 retval
= target_read_memory(target
,
2171 address
, size
, count
, buffer
);
2172 if (ERROR_OK
== retval
)
2173 handle_md_output(cmd_ctx
, target
, address
, size
, count
, buffer
);
2180 static int handle_mw_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
2182 if ((argc
< 2) || (argc
> 3))
2183 return ERROR_COMMAND_SYNTAX_ERROR
;
2186 int retval
= parse_u32(args
[0], &address
);
2187 if (ERROR_OK
!= retval
)
2191 retval
= parse_u32(args
[1], &value
);
2192 if (ERROR_OK
!= retval
)
2198 retval
= parse_uint(args
[2], &count
);
2199 if (ERROR_OK
!= retval
)
2203 target_t
*target
= get_current_target(cmd_ctx
);
2205 uint8_t value_buf
[4];
2210 target_buffer_set_u32(target
, value_buf
, value
);
2214 target_buffer_set_u16(target
, value_buf
, value
);
2218 value_buf
[0] = value
;
2221 return ERROR_COMMAND_SYNTAX_ERROR
;
2223 for (unsigned i
= 0; i
< count
; i
++)
2225 retval
= target_write_memory(target
,
2226 address
+ i
* wordsize
, wordsize
, 1, value_buf
);
2227 if (ERROR_OK
!= retval
)
2236 static int parse_load_image_command_args(char **args
, int argc
,
2237 image_t
*image
, uint32_t *min_address
, uint32_t *max_address
)
2239 if (argc
< 1 || argc
> 5)
2240 return ERROR_COMMAND_SYNTAX_ERROR
;
2242 /* a base address isn't always necessary,
2243 * default to 0x0 (i.e. don't relocate) */
2247 int retval
= parse_u32(args
[1], &addr
);
2248 if (ERROR_OK
!= retval
)
2249 return ERROR_COMMAND_SYNTAX_ERROR
;
2250 image
->base_address
= addr
;
2251 image
->base_address_set
= 1;
2254 image
->base_address_set
= 0;
2256 image
->start_address_set
= 0;
2260 int retval
= parse_u32(args
[3], min_address
);
2261 if (ERROR_OK
!= retval
)
2262 return ERROR_COMMAND_SYNTAX_ERROR
;
2266 int retval
= parse_u32(args
[4], max_address
);
2267 if (ERROR_OK
!= retval
)
2268 return ERROR_COMMAND_SYNTAX_ERROR
;
2269 // use size (given) to find max (required)
2270 *max_address
+= *min_address
;
2273 if (*min_address
> *max_address
)
2274 return ERROR_COMMAND_SYNTAX_ERROR
;
2279 static int handle_load_image_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
2283 uint32_t image_size
;
2284 uint32_t min_address
= 0;
2285 uint32_t max_address
= 0xffffffff;
2291 duration_t duration
;
2292 char *duration_text
;
2294 int retval
= parse_load_image_command_args(args
, argc
,
2295 &image
, &min_address
, &max_address
);
2296 if (ERROR_OK
!= retval
)
2299 target_t
*target
= get_current_target(cmd_ctx
);
2300 duration_start_measure(&duration
);
2302 if (image_open(&image
, args
[0], (argc
>= 3) ? args
[2] : NULL
) != ERROR_OK
)
2309 for (i
= 0; i
< image
.num_sections
; i
++)
2311 buffer
= malloc(image
.sections
[i
].size
);
2314 command_print(cmd_ctx
,
2315 "error allocating buffer for section (%d bytes)",
2316 (int)(image
.sections
[i
].size
));
2320 if ((retval
= image_read_section(&image
, i
, 0x0, image
.sections
[i
].size
, buffer
, &buf_cnt
)) != ERROR_OK
)
2326 uint32_t offset
= 0;
2327 uint32_t length
= buf_cnt
;
2329 /* DANGER!!! beware of unsigned comparision here!!! */
2331 if ((image
.sections
[i
].base_address
+ buf_cnt
>= min_address
)&&
2332 (image
.sections
[i
].base_address
< max_address
))
2334 if (image
.sections
[i
].base_address
< min_address
)
2336 /* clip addresses below */
2337 offset
+= min_address
-image
.sections
[i
].base_address
;
2341 if (image
.sections
[i
].base_address
+ buf_cnt
> max_address
)
2343 length
-= (image
.sections
[i
].base_address
+ buf_cnt
)-max_address
;
2346 if ((retval
= target_write_buffer(target
, image
.sections
[i
].base_address
+ offset
, length
, buffer
+ offset
)) != ERROR_OK
)
2351 image_size
+= length
;
2352 command_print(cmd_ctx
, "%u bytes written at address 0x%8.8" PRIx32
"",
2353 (unsigned int)length
,
2354 image
.sections
[i
].base_address
+ offset
);
2360 if ((retvaltemp
= duration_stop_measure(&duration
, &duration_text
)) != ERROR_OK
)
2362 image_close(&image
);
2366 if (retval
== ERROR_OK
)
2368 command_print(cmd_ctx
, "downloaded %u byte in %s",
2369 (unsigned int)image_size
,
2372 free(duration_text
);
2374 image_close(&image
);
2380 static int handle_dump_image_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
2384 uint8_t buffer
[560];
2387 duration_t duration
;
2388 char *duration_text
;
2390 target_t
*target
= get_current_target(cmd_ctx
);
2394 command_print(cmd_ctx
, "usage: dump_image <filename> <address> <size>");
2399 int retval
= parse_u32(args
[1], &address
);
2400 if (ERROR_OK
!= retval
)
2404 retval
= parse_u32(args
[2], &size
);
2405 if (ERROR_OK
!= retval
)
2408 if (fileio_open(&fileio
, args
[0], FILEIO_WRITE
, FILEIO_BINARY
) != ERROR_OK
)
2413 duration_start_measure(&duration
);
2417 uint32_t size_written
;
2418 uint32_t this_run_size
= (size
> 560) ? 560 : size
;
2420 retval
= target_read_buffer(target
, address
, this_run_size
, buffer
);
2421 if (retval
!= ERROR_OK
)
2426 retval
= fileio_write(&fileio
, this_run_size
, buffer
, &size_written
);
2427 if (retval
!= ERROR_OK
)
2432 size
-= this_run_size
;
2433 address
+= this_run_size
;
2436 if ((retvaltemp
= fileio_close(&fileio
)) != ERROR_OK
)
2439 if ((retvaltemp
= duration_stop_measure(&duration
, &duration_text
)) != ERROR_OK
)
2442 if (retval
== ERROR_OK
)
2444 command_print(cmd_ctx
, "dumped %lld byte in %s",
2445 fileio
.size
, duration_text
);
2446 free(duration_text
);
2452 static int handle_verify_image_command_internal(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
, int verify
)
2456 uint32_t image_size
;
2458 int retval
, retvaltemp
;
2459 uint32_t checksum
= 0;
2460 uint32_t mem_checksum
= 0;
2464 duration_t duration
;
2465 char *duration_text
;
2467 target_t
*target
= get_current_target(cmd_ctx
);
2471 return ERROR_COMMAND_SYNTAX_ERROR
;
2476 LOG_ERROR("no target selected");
2480 duration_start_measure(&duration
);
2485 retval
= parse_u32(args
[1], &addr
);
2486 if (ERROR_OK
!= retval
)
2487 return ERROR_COMMAND_SYNTAX_ERROR
;
2488 image
.base_address
= addr
;
2489 image
.base_address_set
= 1;
2493 image
.base_address_set
= 0;
2494 image
.base_address
= 0x0;
2497 image
.start_address_set
= 0;
2499 if ((retval
= image_open(&image
, args
[0], (argc
== 3) ? args
[2] : NULL
)) != ERROR_OK
)
2506 for (i
= 0; i
< image
.num_sections
; i
++)
2508 buffer
= malloc(image
.sections
[i
].size
);
2511 command_print(cmd_ctx
,
2512 "error allocating buffer for section (%d bytes)",
2513 (int)(image
.sections
[i
].size
));
2516 if ((retval
= image_read_section(&image
, i
, 0x0, image
.sections
[i
].size
, buffer
, &buf_cnt
)) != ERROR_OK
)
2524 /* calculate checksum of image */
2525 image_calculate_checksum(buffer
, buf_cnt
, &checksum
);
2527 retval
= target_checksum_memory(target
, image
.sections
[i
].base_address
, buf_cnt
, &mem_checksum
);
2528 if (retval
!= ERROR_OK
)
2534 if (checksum
!= mem_checksum
)
2536 /* failed crc checksum, fall back to a binary compare */
2539 command_print(cmd_ctx
, "checksum mismatch - attempting binary compare");
2541 data
= (uint8_t*)malloc(buf_cnt
);
2543 /* Can we use 32bit word accesses? */
2545 int count
= buf_cnt
;
2546 if ((count
% 4) == 0)
2551 retval
= target_read_memory(target
, image
.sections
[i
].base_address
, size
, count
, data
);
2552 if (retval
== ERROR_OK
)
2555 for (t
= 0; t
< buf_cnt
; t
++)
2557 if (data
[t
] != buffer
[t
])
2559 command_print(cmd_ctx
,
2560 "Verify operation failed address 0x%08x. Was 0x%02x instead of 0x%02x\n",
2561 (unsigned)(t
+ image
.sections
[i
].base_address
),
2566 retval
= ERROR_FAIL
;
2580 command_print(cmd_ctx
, "address 0x%08" PRIx32
" length 0x%08" PRIx32
"",
2581 image
.sections
[i
].base_address
,
2586 image_size
+= buf_cnt
;
2590 if ((retvaltemp
= duration_stop_measure(&duration
, &duration_text
)) != ERROR_OK
)
2592 image_close(&image
);
2596 if (retval
== ERROR_OK
)
2598 command_print(cmd_ctx
, "verified %u bytes in %s",
2599 (unsigned int)image_size
,
2602 free(duration_text
);
2604 image_close(&image
);
2609 static int handle_verify_image_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
2611 return handle_verify_image_command_internal(cmd_ctx
, cmd
, args
, argc
, 1);
2614 static int handle_test_image_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
2616 return handle_verify_image_command_internal(cmd_ctx
, cmd
, args
, argc
, 0);
2619 static int handle_bp_command_list(struct command_context_s
*cmd_ctx
)
2621 target_t
*target
= get_current_target(cmd_ctx
);
2622 breakpoint_t
*breakpoint
= target
->breakpoints
;
2625 if (breakpoint
->type
== BKPT_SOFT
)
2627 char* buf
= buf_to_str(breakpoint
->orig_instr
,
2628 breakpoint
->length
, 16);
2629 command_print(cmd_ctx
, "0x%8.8" PRIx32
", 0x%x, %i, 0x%s",
2630 breakpoint
->address
,
2632 breakpoint
->set
, buf
);
2637 command_print(cmd_ctx
, "0x%8.8" PRIx32
", 0x%x, %i",
2638 breakpoint
->address
,
2639 breakpoint
->length
, breakpoint
->set
);
2642 breakpoint
= breakpoint
->next
;
2647 static int handle_bp_command_set(struct command_context_s
*cmd_ctx
,
2648 uint32_t addr
, uint32_t length
, int hw
)
2650 target_t
*target
= get_current_target(cmd_ctx
);
2651 int retval
= breakpoint_add(target
, addr
, length
, hw
);
2652 if (ERROR_OK
== retval
)
2653 command_print(cmd_ctx
, "breakpoint set at 0x%8.8" PRIx32
"", addr
);
2655 LOG_ERROR("Failure setting breakpoint");
2659 static int handle_bp_command(struct command_context_s
*cmd_ctx
,
2660 char *cmd
, char **args
, int argc
)
2663 return handle_bp_command_list(cmd_ctx
);
2665 if (argc
< 2 || argc
> 3)
2667 command_print(cmd_ctx
, "usage: bp <address> <length> ['hw']");
2668 return ERROR_COMMAND_SYNTAX_ERROR
;
2672 int retval
= parse_u32(args
[0], &addr
);
2673 if (ERROR_OK
!= retval
)
2677 retval
= parse_u32(args
[1], &length
);
2678 if (ERROR_OK
!= retval
)
2684 if (strcmp(args
[2], "hw") == 0)
2687 return ERROR_COMMAND_SYNTAX_ERROR
;
2690 return handle_bp_command_set(cmd_ctx
, addr
, length
, hw
);
2693 static int handle_rbp_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
2696 return ERROR_COMMAND_SYNTAX_ERROR
;
2699 int retval
= parse_u32(args
[0], &addr
);
2700 if (ERROR_OK
!= retval
)
2703 target_t
*target
= get_current_target(cmd_ctx
);
2704 breakpoint_remove(target
, addr
);
2709 static int handle_wp_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
2711 target_t
*target
= get_current_target(cmd_ctx
);
2715 watchpoint_t
*watchpoint
= target
->watchpoints
;
2719 command_print(cmd_ctx
,
2720 "address: 0x%8.8" PRIx32
", len: 0x%8.8x, r/w/a: %i, value: 0x%8.8" PRIx32
", mask: 0x%8.8" PRIx32
"",
2721 watchpoint
->address
,
2723 (int)(watchpoint
->rw
),
2726 watchpoint
= watchpoint
->next
;
2731 enum watchpoint_rw type
= WPT_ACCESS
;
2733 uint32_t length
= 0;
2734 uint32_t data_value
= 0x0;
2735 uint32_t data_mask
= 0xffffffff;
2741 retval
= parse_u32(args
[4], &data_mask
);
2742 if (ERROR_OK
!= retval
)
2746 retval
= parse_u32(args
[3], &data_value
);
2747 if (ERROR_OK
!= retval
)
2763 LOG_ERROR("invalid watchpoint mode ('%c')", args
[2][0]);
2764 return ERROR_COMMAND_SYNTAX_ERROR
;
2768 retval
= parse_u32(args
[1], &length
);
2769 if (ERROR_OK
!= retval
)
2771 retval
= parse_u32(args
[0], &addr
);
2772 if (ERROR_OK
!= retval
)
2777 command_print(cmd_ctx
, "usage: wp <address> <length> [r/w/a] [value] [mask]");
2778 return ERROR_COMMAND_SYNTAX_ERROR
;
2781 retval
= watchpoint_add(target
, addr
, length
, type
,
2782 data_value
, data_mask
);
2783 if (ERROR_OK
!= retval
)
2784 LOG_ERROR("Failure setting watchpoints");
2789 static int handle_rwp_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
2792 return ERROR_COMMAND_SYNTAX_ERROR
;
2795 int retval
= parse_u32(args
[0], &addr
);
2796 if (ERROR_OK
!= retval
)
2799 target_t
*target
= get_current_target(cmd_ctx
);
2800 watchpoint_remove(target
, addr
);
2807 * Translate a virtual address to a physical address.
2809 * The low-level target implementation must have logged a detailed error
2810 * which is forwarded to telnet/GDB session.
2812 static int handle_virt2phys_command(command_context_t
*cmd_ctx
,
2813 char *cmd
, char **args
, int argc
)
2816 return ERROR_COMMAND_SYNTAX_ERROR
;
2819 int retval
= parse_u32(args
[0], &va
);
2820 if (ERROR_OK
!= retval
)
2824 target_t
*target
= get_current_target(cmd_ctx
);
2825 retval
= target
->type
->virt2phys(target
, va
, &pa
);
2826 if (retval
== ERROR_OK
)
2827 command_print(cmd_ctx
, "Physical address 0x%08" PRIx32
"", pa
);
2832 static void writeData(FILE *f
, const void *data
, size_t len
)
2834 size_t written
= fwrite(data
, 1, len
, f
);
2836 LOG_ERROR("failed to write %zu bytes: %s", len
, strerror(errno
));
2839 static void writeLong(FILE *f
, int l
)
2842 for (i
= 0; i
< 4; i
++)
2844 char c
= (l
>> (i
*8))&0xff;
2845 writeData(f
, &c
, 1);
2850 static void writeString(FILE *f
, char *s
)
2852 writeData(f
, s
, strlen(s
));
2855 /* Dump a gmon.out histogram file. */
2856 static void writeGmon(uint32_t *samples
, uint32_t sampleNum
, char *filename
)
2859 FILE *f
= fopen(filename
, "w");
2862 writeString(f
, "gmon");
2863 writeLong(f
, 0x00000001); /* Version */
2864 writeLong(f
, 0); /* padding */
2865 writeLong(f
, 0); /* padding */
2866 writeLong(f
, 0); /* padding */
2868 uint8_t zero
= 0; /* GMON_TAG_TIME_HIST */
2869 writeData(f
, &zero
, 1);
2871 /* figure out bucket size */
2872 uint32_t min
= samples
[0];
2873 uint32_t max
= samples
[0];
2874 for (i
= 0; i
< sampleNum
; i
++)
2876 if (min
> samples
[i
])
2880 if (max
< samples
[i
])
2886 int addressSpace
= (max
-min
+ 1);
2888 static const uint32_t maxBuckets
= 256 * 1024; /* maximum buckets. */
2889 uint32_t length
= addressSpace
;
2890 if (length
> maxBuckets
)
2892 length
= maxBuckets
;
2894 int *buckets
= malloc(sizeof(int)*length
);
2895 if (buckets
== NULL
)
2900 memset(buckets
, 0, sizeof(int)*length
);
2901 for (i
= 0; i
< sampleNum
;i
++)
2903 uint32_t address
= samples
[i
];
2904 long long a
= address
-min
;
2905 long long b
= length
-1;
2906 long long c
= addressSpace
-1;
2907 int index
= (a
*b
)/c
; /* danger!!!! int32 overflows */
2911 /* append binary memory gmon.out &profile_hist_hdr ((char*)&profile_hist_hdr + sizeof(struct gmon_hist_hdr)) */
2912 writeLong(f
, min
); /* low_pc */
2913 writeLong(f
, max
); /* high_pc */
2914 writeLong(f
, length
); /* # of samples */
2915 writeLong(f
, 64000000); /* 64MHz */
2916 writeString(f
, "seconds");
2917 for (i
= 0; i
< (15-strlen("seconds")); i
++)
2918 writeData(f
, &zero
, 1);
2919 writeString(f
, "s");
2921 /*append binary memory gmon.out profile_hist_data (profile_hist_data + profile_hist_hdr.hist_size) */
2923 char *data
= malloc(2*length
);
2926 for (i
= 0; i
< length
;i
++)
2935 data
[i
*2 + 1]=(val
>> 8)&0xff;
2938 writeData(f
, data
, length
* 2);
2948 /* profiling samples the CPU PC as quickly as OpenOCD is able, which will be used as a random sampling of PC */
2949 static int handle_profile_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
2951 target_t
*target
= get_current_target(cmd_ctx
);
2952 struct timeval timeout
, now
;
2954 gettimeofday(&timeout
, NULL
);
2957 return ERROR_COMMAND_SYNTAX_ERROR
;
2960 int retval
= parse_uint(args
[0], &offset
);
2961 if (ERROR_OK
!= retval
)
2964 timeval_add_time(&timeout
, offset
, 0);
2966 command_print(cmd_ctx
, "Starting profiling. Halting and resuming the target as often as we can...");
2968 static const int maxSample
= 10000;
2969 uint32_t *samples
= malloc(sizeof(uint32_t)*maxSample
);
2970 if (samples
== NULL
)
2974 /* hopefully it is safe to cache! We want to stop/restart as quickly as possible. */
2975 reg_t
*reg
= register_get_by_name(target
->reg_cache
, "pc", 1);
2979 target_poll(target
);
2980 if (target
->state
== TARGET_HALTED
)
2982 uint32_t t
=*((uint32_t *)reg
->value
);
2983 samples
[numSamples
++]=t
;
2984 retval
= target_resume(target
, 1, 0, 0, 0); /* current pc, addr = 0, do not handle breakpoints, not debugging */
2985 target_poll(target
);
2986 alive_sleep(10); /* sleep 10ms, i.e. <100 samples/second. */
2987 } else if (target
->state
== TARGET_RUNNING
)
2989 /* We want to quickly sample the PC. */
2990 if ((retval
= target_halt(target
)) != ERROR_OK
)
2997 command_print(cmd_ctx
, "Target not halted or running");
3001 if (retval
!= ERROR_OK
)
3006 gettimeofday(&now
, NULL
);
3007 if ((numSamples
>= maxSample
) || ((now
.tv_sec
>= timeout
.tv_sec
) && (now
.tv_usec
>= timeout
.tv_usec
)))
3009 command_print(cmd_ctx
, "Profiling completed. %d samples.", numSamples
);
3010 if ((retval
= target_poll(target
)) != ERROR_OK
)
3015 if (target
->state
== TARGET_HALTED
)
3017 target_resume(target
, 1, 0, 0, 0); /* current pc, addr = 0, do not handle breakpoints, not debugging */
3019 if ((retval
= target_poll(target
)) != ERROR_OK
)
3024 writeGmon(samples
, numSamples
, args
[1]);
3025 command_print(cmd_ctx
, "Wrote %s", args
[1]);
3034 static int new_int_array_element(Jim_Interp
* interp
, const char *varname
, int idx
, uint32_t val
)
3037 Jim_Obj
*nameObjPtr
, *valObjPtr
;
3040 namebuf
= alloc_printf("%s(%d)", varname
, idx
);
3044 nameObjPtr
= Jim_NewStringObj(interp
, namebuf
, -1);
3045 valObjPtr
= Jim_NewIntObj(interp
, val
);
3046 if (!nameObjPtr
|| !valObjPtr
)
3052 Jim_IncrRefCount(nameObjPtr
);
3053 Jim_IncrRefCount(valObjPtr
);
3054 result
= Jim_SetVariable(interp
, nameObjPtr
, valObjPtr
);
3055 Jim_DecrRefCount(interp
, nameObjPtr
);
3056 Jim_DecrRefCount(interp
, valObjPtr
);
3058 /* printf("%s(%d) <= 0%08x\n", varname, idx, val); */
3062 static int jim_mem2array(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
3064 command_context_t
*context
;
3067 context
= Jim_GetAssocData(interp
, "context");
3068 if (context
== NULL
)
3070 LOG_ERROR("mem2array: no command context");
3073 target
= get_current_target(context
);
3076 LOG_ERROR("mem2array: no current target");
3080 return target_mem2array(interp
, target
, argc
-1, argv
+ 1);
3083 static int target_mem2array(Jim_Interp
*interp
, target_t
*target
, int argc
, Jim_Obj
*const *argv
)
3091 const char *varname
;
3092 uint8_t buffer
[4096];
3096 /* argv[1] = name of array to receive the data
3097 * argv[2] = desired width
3098 * argv[3] = memory address
3099 * argv[4] = count of times to read
3102 Jim_WrongNumArgs(interp
, 1, argv
, "varname width addr nelems");
3105 varname
= Jim_GetString(argv
[0], &len
);
3106 /* given "foo" get space for worse case "foo(%d)" .. add 20 */
3108 e
= Jim_GetLong(interp
, argv
[1], &l
);
3114 e
= Jim_GetLong(interp
, argv
[2], &l
);
3119 e
= Jim_GetLong(interp
, argv
[3], &l
);
3135 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3136 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "Invalid width param, must be 8/16/32", NULL
);
3140 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3141 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "mem2array: zero width read?", NULL
);
3144 if ((addr
+ (len
* width
)) < addr
) {
3145 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3146 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "mem2array: addr + len - wraps to zero?", NULL
);
3149 /* absurd transfer size? */
3151 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3152 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "mem2array: absurd > 64K item request", NULL
);
3157 ((width
== 2) && ((addr
& 1) == 0)) ||
3158 ((width
== 4) && ((addr
& 3) == 0))) {
3162 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3163 sprintf(buf
, "mem2array address: 0x%08" PRIx32
" is not aligned for %" PRId32
" byte reads",
3166 Jim_AppendStrings(interp
, Jim_GetResult(interp
), buf
, NULL
);
3177 /* Slurp... in buffer size chunks */
3179 count
= len
; /* in objects.. */
3180 if (count
> (sizeof(buffer
)/width
)) {
3181 count
= (sizeof(buffer
)/width
);
3184 retval
= target_read_memory(target
, addr
, width
, count
, buffer
);
3185 if (retval
!= ERROR_OK
) {
3187 LOG_ERROR("mem2array: Read @ 0x%08x, w=%d, cnt=%d, failed",
3191 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3192 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "mem2array: cannot read memory", NULL
);
3196 v
= 0; /* shut up gcc */
3197 for (i
= 0 ;i
< count
;i
++, n
++) {
3200 v
= target_buffer_get_u32(target
, &buffer
[i
*width
]);
3203 v
= target_buffer_get_u16(target
, &buffer
[i
*width
]);
3206 v
= buffer
[i
] & 0x0ff;
3209 new_int_array_element(interp
, varname
, n
, v
);
3215 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3220 static int get_int_array_element(Jim_Interp
* interp
, const char *varname
, int idx
, uint32_t *val
)
3223 Jim_Obj
*nameObjPtr
, *valObjPtr
;
3227 namebuf
= alloc_printf("%s(%d)", varname
, idx
);
3231 nameObjPtr
= Jim_NewStringObj(interp
, namebuf
, -1);
3238 Jim_IncrRefCount(nameObjPtr
);
3239 valObjPtr
= Jim_GetVariable(interp
, nameObjPtr
, JIM_ERRMSG
);
3240 Jim_DecrRefCount(interp
, nameObjPtr
);
3242 if (valObjPtr
== NULL
)
3245 result
= Jim_GetLong(interp
, valObjPtr
, &l
);
3246 /* printf("%s(%d) => 0%08x\n", varname, idx, val); */
3251 static int jim_array2mem(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
3253 command_context_t
*context
;
3256 context
= Jim_GetAssocData(interp
, "context");
3257 if (context
== NULL
) {
3258 LOG_ERROR("array2mem: no command context");
3261 target
= get_current_target(context
);
3262 if (target
== NULL
) {
3263 LOG_ERROR("array2mem: no current target");
3267 return target_array2mem(interp
,target
, argc
-1, argv
+ 1);
3270 static int target_array2mem(Jim_Interp
*interp
, target_t
*target
, int argc
, Jim_Obj
*const *argv
)
3278 const char *varname
;
3279 uint8_t buffer
[4096];
3283 /* argv[1] = name of array to get the data
3284 * argv[2] = desired width
3285 * argv[3] = memory address
3286 * argv[4] = count to write
3289 Jim_WrongNumArgs(interp
, 1, argv
, "varname width addr nelems");
3292 varname
= Jim_GetString(argv
[0], &len
);
3293 /* given "foo" get space for worse case "foo(%d)" .. add 20 */
3295 e
= Jim_GetLong(interp
, argv
[1], &l
);
3301 e
= Jim_GetLong(interp
, argv
[2], &l
);
3306 e
= Jim_GetLong(interp
, argv
[3], &l
);
3322 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3323 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "Invalid width param, must be 8/16/32", NULL
);
3327 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3328 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "array2mem: zero width read?", NULL
);
3331 if ((addr
+ (len
* width
)) < addr
) {
3332 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3333 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "array2mem: addr + len - wraps to zero?", NULL
);
3336 /* absurd transfer size? */
3338 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3339 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "array2mem: absurd > 64K item request", NULL
);
3344 ((width
== 2) && ((addr
& 1) == 0)) ||
3345 ((width
== 4) && ((addr
& 3) == 0))) {
3349 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3350 sprintf(buf
, "array2mem address: 0x%08x is not aligned for %d byte reads",
3353 Jim_AppendStrings(interp
, Jim_GetResult(interp
), buf
, NULL
);
3364 /* Slurp... in buffer size chunks */
3366 count
= len
; /* in objects.. */
3367 if (count
> (sizeof(buffer
)/width
)) {
3368 count
= (sizeof(buffer
)/width
);
3371 v
= 0; /* shut up gcc */
3372 for (i
= 0 ;i
< count
;i
++, n
++) {
3373 get_int_array_element(interp
, varname
, n
, &v
);
3376 target_buffer_set_u32(target
, &buffer
[i
*width
], v
);
3379 target_buffer_set_u16(target
, &buffer
[i
*width
], v
);
3382 buffer
[i
] = v
& 0x0ff;
3388 retval
= target_write_memory(target
, addr
, width
, count
, buffer
);
3389 if (retval
!= ERROR_OK
) {
3391 LOG_ERROR("array2mem: Write @ 0x%08x, w=%d, cnt=%d, failed",
3395 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3396 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "array2mem: cannot read memory", NULL
);
3402 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3407 void target_all_handle_event(enum target_event e
)
3411 LOG_DEBUG("**all*targets: event: %d, %s",
3413 Jim_Nvp_value2name_simple(nvp_target_event
, e
)->name
);
3415 target
= all_targets
;
3417 target_handle_event(target
, e
);
3418 target
= target
->next
;
3422 void target_handle_event(target_t
*target
, enum target_event e
)
3424 target_event_action_t
*teap
;
3426 for (teap
= target
->event_action
; teap
!= NULL
; teap
= teap
->next
) {
3427 if (teap
->event
== e
) {
3428 LOG_DEBUG("target: (%d) %s (%s) event: %d (%s) action: %s",
3429 target
->target_number
,
3431 target_get_name(target
),
3433 Jim_Nvp_value2name_simple(nvp_target_event
, e
)->name
,
3434 Jim_GetString(teap
->body
, NULL
));
3435 if (Jim_EvalObj(interp
, teap
->body
) != JIM_OK
)
3437 Jim_PrintErrorMessage(interp
);
3443 enum target_cfg_param
{
3446 TCFG_WORK_AREA_VIRT
,
3447 TCFG_WORK_AREA_PHYS
,
3448 TCFG_WORK_AREA_SIZE
,
3449 TCFG_WORK_AREA_BACKUP
,
3452 TCFG_CHAIN_POSITION
,
3455 static Jim_Nvp nvp_config_opts
[] = {
3456 { .name
= "-type", .value
= TCFG_TYPE
},
3457 { .name
= "-event", .value
= TCFG_EVENT
},
3458 { .name
= "-work-area-virt", .value
= TCFG_WORK_AREA_VIRT
},
3459 { .name
= "-work-area-phys", .value
= TCFG_WORK_AREA_PHYS
},
3460 { .name
= "-work-area-size", .value
= TCFG_WORK_AREA_SIZE
},
3461 { .name
= "-work-area-backup", .value
= TCFG_WORK_AREA_BACKUP
},
3462 { .name
= "-endian" , .value
= TCFG_ENDIAN
},
3463 { .name
= "-variant", .value
= TCFG_VARIANT
},
3464 { .name
= "-chain-position", .value
= TCFG_CHAIN_POSITION
},
3466 { .name
= NULL
, .value
= -1 }
3469 static int target_configure(Jim_GetOptInfo
*goi
, target_t
*target
)
3477 /* parse config or cget options ... */
3478 while (goi
->argc
> 0) {
3479 Jim_SetEmptyResult(goi
->interp
);
3480 /* Jim_GetOpt_Debug(goi); */
3482 if (target
->type
->target_jim_configure
) {
3483 /* target defines a configure function */
3484 /* target gets first dibs on parameters */
3485 e
= (*(target
->type
->target_jim_configure
))(target
, goi
);
3494 /* otherwise we 'continue' below */
3496 e
= Jim_GetOpt_Nvp(goi
, nvp_config_opts
, &n
);
3498 Jim_GetOpt_NvpUnknown(goi
, nvp_config_opts
, 0);
3504 if (goi
->isconfigure
) {
3505 Jim_SetResult_sprintf(goi
->interp
, "not setable: %s", n
->name
);
3509 if (goi
->argc
!= 0) {
3510 Jim_WrongNumArgs(goi
->interp
, goi
->argc
, goi
->argv
, "NO PARAMS");
3514 Jim_SetResultString(goi
->interp
, target_get_name(target
), -1);
3518 if (goi
->argc
== 0) {
3519 Jim_WrongNumArgs(goi
->interp
, goi
->argc
, goi
->argv
, "-event ?event-name? ...");
3523 e
= Jim_GetOpt_Nvp(goi
, nvp_target_event
, &n
);
3525 Jim_GetOpt_NvpUnknown(goi
, nvp_target_event
, 1);
3529 if (goi
->isconfigure
) {
3530 if (goi
->argc
!= 1) {
3531 Jim_WrongNumArgs(goi
->interp
, goi
->argc
, goi
->argv
, "-event ?event-name? ?EVENT-BODY?");
3535 if (goi
->argc
!= 0) {
3536 Jim_WrongNumArgs(goi
->interp
, goi
->argc
, goi
->argv
, "-event ?event-name?");
3542 target_event_action_t
*teap
;
3544 teap
= target
->event_action
;
3545 /* replace existing? */
3547 if (teap
->event
== (enum target_event
)n
->value
) {
3553 if (goi
->isconfigure
) {
3554 bool replace
= true;
3557 teap
= calloc(1, sizeof(*teap
));
3560 teap
->event
= n
->value
;
3561 Jim_GetOpt_Obj(goi
, &o
);
3563 Jim_DecrRefCount(interp
, teap
->body
);
3565 teap
->body
= Jim_DuplicateObj(goi
->interp
, o
);
3568 * Tcl/TK - "tk events" have a nice feature.
3569 * See the "BIND" command.
3570 * We should support that here.
3571 * You can specify %X and %Y in the event code.
3572 * The idea is: %T - target name.
3573 * The idea is: %N - target number
3574 * The idea is: %E - event name.
3576 Jim_IncrRefCount(teap
->body
);
3580 /* add to head of event list */
3581 teap
->next
= target
->event_action
;
3582 target
->event_action
= teap
;
3584 Jim_SetEmptyResult(goi
->interp
);
3588 Jim_SetEmptyResult(goi
->interp
);
3590 Jim_SetResult(goi
->interp
, Jim_DuplicateObj(goi
->interp
, teap
->body
));
3597 case TCFG_WORK_AREA_VIRT
:
3598 if (goi
->isconfigure
) {
3599 target_free_all_working_areas(target
);
3600 e
= Jim_GetOpt_Wide(goi
, &w
);
3604 target
->working_area_virt
= w
;
3606 if (goi
->argc
!= 0) {
3610 Jim_SetResult(interp
, Jim_NewIntObj(goi
->interp
, target
->working_area_virt
));
3614 case TCFG_WORK_AREA_PHYS
:
3615 if (goi
->isconfigure
) {
3616 target_free_all_working_areas(target
);
3617 e
= Jim_GetOpt_Wide(goi
, &w
);
3621 target
->working_area_phys
= w
;
3623 if (goi
->argc
!= 0) {
3627 Jim_SetResult(interp
, Jim_NewIntObj(goi
->interp
, target
->working_area_phys
));
3631 case TCFG_WORK_AREA_SIZE
:
3632 if (goi
->isconfigure
) {
3633 target_free_all_working_areas(target
);
3634 e
= Jim_GetOpt_Wide(goi
, &w
);
3638 target
->working_area_size
= w
;
3640 if (goi
->argc
!= 0) {
3644 Jim_SetResult(interp
, Jim_NewIntObj(goi
->interp
, target
->working_area_size
));
3648 case TCFG_WORK_AREA_BACKUP
:
3649 if (goi
->isconfigure
) {
3650 target_free_all_working_areas(target
);
3651 e
= Jim_GetOpt_Wide(goi
, &w
);
3655 /* make this exactly 1 or 0 */
3656 target
->backup_working_area
= (!!w
);
3658 if (goi
->argc
!= 0) {
3662 Jim_SetResult(interp
, Jim_NewIntObj(goi
->interp
, target
->backup_working_area
));
3663 /* loop for more e*/
3667 if (goi
->isconfigure
) {
3668 e
= Jim_GetOpt_Nvp(goi
, nvp_target_endian
, &n
);
3670 Jim_GetOpt_NvpUnknown(goi
, nvp_target_endian
, 1);
3673 target
->endianness
= n
->value
;
3675 if (goi
->argc
!= 0) {
3679 n
= Jim_Nvp_value2name_simple(nvp_target_endian
, target
->endianness
);
3680 if (n
->name
== NULL
) {
3681 target
->endianness
= TARGET_LITTLE_ENDIAN
;
3682 n
= Jim_Nvp_value2name_simple(nvp_target_endian
, target
->endianness
);
3684 Jim_SetResultString(goi
->interp
, n
->name
, -1);
3689 if (goi
->isconfigure
) {
3690 if (goi
->argc
< 1) {
3691 Jim_SetResult_sprintf(goi
->interp
,
3696 if (target
->variant
) {
3697 free((void *)(target
->variant
));
3699 e
= Jim_GetOpt_String(goi
, &cp
, NULL
);
3700 target
->variant
= strdup(cp
);
3702 if (goi
->argc
!= 0) {
3706 Jim_SetResultString(goi
->interp
, target
->variant
,-1);
3709 case TCFG_CHAIN_POSITION
:
3710 if (goi
->isconfigure
) {
3713 target_free_all_working_areas(target
);
3714 e
= Jim_GetOpt_Obj(goi
, &o
);
3718 tap
= jtag_tap_by_jim_obj(goi
->interp
, o
);
3722 /* make this exactly 1 or 0 */
3725 if (goi
->argc
!= 0) {
3729 Jim_SetResultString(interp
, target
->tap
->dotted_name
, -1);
3730 /* loop for more e*/
3733 } /* while (goi->argc) */
3736 /* done - we return */
3740 /** this is the 'tcl' handler for the target specific command */
3741 static int tcl_target_func(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
3746 uint8_t target_buf
[32];
3749 struct command_context_s
*cmd_ctx
;
3756 TS_CMD_MWW
, TS_CMD_MWH
, TS_CMD_MWB
,
3757 TS_CMD_MDW
, TS_CMD_MDH
, TS_CMD_MDB
,
3758 TS_CMD_MRW
, TS_CMD_MRH
, TS_CMD_MRB
,
3759 TS_CMD_MEM2ARRAY
, TS_CMD_ARRAY2MEM
,
3767 TS_CMD_INVOKE_EVENT
,
3770 static const Jim_Nvp target_options
[] = {
3771 { .name
= "configure", .value
= TS_CMD_CONFIGURE
},
3772 { .name
= "cget", .value
= TS_CMD_CGET
},
3773 { .name
= "mww", .value
= TS_CMD_MWW
},
3774 { .name
= "mwh", .value
= TS_CMD_MWH
},
3775 { .name
= "mwb", .value
= TS_CMD_MWB
},
3776 { .name
= "mdw", .value
= TS_CMD_MDW
},
3777 { .name
= "mdh", .value
= TS_CMD_MDH
},
3778 { .name
= "mdb", .value
= TS_CMD_MDB
},
3779 { .name
= "mem2array", .value
= TS_CMD_MEM2ARRAY
},
3780 { .name
= "array2mem", .value
= TS_CMD_ARRAY2MEM
},
3781 { .name
= "eventlist", .value
= TS_CMD_EVENTLIST
},
3782 { .name
= "curstate", .value
= TS_CMD_CURSTATE
},
3784 { .name
= "arp_examine", .value
= TS_CMD_EXAMINE
},
3785 { .name
= "arp_poll", .value
= TS_CMD_POLL
},
3786 { .name
= "arp_reset", .value
= TS_CMD_RESET
},
3787 { .name
= "arp_halt", .value
= TS_CMD_HALT
},
3788 { .name
= "arp_waitstate", .value
= TS_CMD_WAITSTATE
},
3789 { .name
= "invoke-event", .value
= TS_CMD_INVOKE_EVENT
},
3791 { .name
= NULL
, .value
= -1 },
3794 /* go past the "command" */
3795 Jim_GetOpt_Setup(&goi
, interp
, argc
-1, argv
+ 1);
3797 target
= Jim_CmdPrivData(goi
.interp
);
3798 cmd_ctx
= Jim_GetAssocData(goi
.interp
, "context");
3800 /* commands here are in an NVP table */
3801 e
= Jim_GetOpt_Nvp(&goi
, target_options
, &n
);
3803 Jim_GetOpt_NvpUnknown(&goi
, target_options
, 0);
3806 /* Assume blank result */
3807 Jim_SetEmptyResult(goi
.interp
);
3810 case TS_CMD_CONFIGURE
:
3812 Jim_WrongNumArgs(goi
.interp
, goi
.argc
, goi
.argv
, "missing: -option VALUE ...");
3815 goi
.isconfigure
= 1;
3816 return target_configure(&goi
, target
);
3818 // some things take params
3820 Jim_WrongNumArgs(goi
.interp
, 0, goi
.argv
, "missing: ?-option?");
3823 goi
.isconfigure
= 0;
3824 return target_configure(&goi
, target
);
3832 * argv[3] = optional count.
3835 if ((goi
.argc
== 2) || (goi
.argc
== 3)) {
3839 Jim_SetResult_sprintf(goi
.interp
, "expected: %s ADDR DATA [COUNT]", n
->name
);
3843 e
= Jim_GetOpt_Wide(&goi
, &a
);
3848 e
= Jim_GetOpt_Wide(&goi
, &b
);
3852 if (goi
.argc
== 3) {
3853 e
= Jim_GetOpt_Wide(&goi
, &c
);
3863 target_buffer_set_u32(target
, target_buf
, b
);
3867 target_buffer_set_u16(target
, target_buf
, b
);
3871 target_buffer_set_u8(target
, target_buf
, b
);
3875 for (x
= 0 ; x
< c
; x
++) {
3876 e
= target_write_memory(target
, a
, b
, 1, target_buf
);
3877 if (e
!= ERROR_OK
) {
3878 Jim_SetResult_sprintf(interp
, "Error writing @ 0x%08x: %d\n", (int)(a
), e
);
3891 /* argv[0] = command
3893 * argv[2] = optional count
3895 if ((goi
.argc
== 2) || (goi
.argc
== 3)) {
3896 Jim_SetResult_sprintf(goi
.interp
, "expected: %s ADDR [COUNT]", n
->name
);
3899 e
= Jim_GetOpt_Wide(&goi
, &a
);
3904 e
= Jim_GetOpt_Wide(&goi
, &c
);
3911 b
= 1; /* shut up gcc */
3924 /* convert to "bytes" */
3926 /* count is now in 'BYTES' */
3932 e
= target_read_memory(target
, a
, b
, y
/ b
, target_buf
);
3933 if (e
!= ERROR_OK
) {
3934 Jim_SetResult_sprintf(interp
, "error reading target @ 0x%08lx", (int)(a
));
3938 Jim_fprintf(interp
, interp
->cookie_stdout
, "0x%08x ", (int)(a
));
3941 for (x
= 0 ; (x
< 16) && (x
< y
) ; x
+= 4) {
3942 z
= target_buffer_get_u32(target
, &(target_buf
[ x
* 4 ]));
3943 Jim_fprintf(interp
, interp
->cookie_stdout
, "%08x ", (int)(z
));
3945 for (; (x
< 16) ; x
+= 4) {
3946 Jim_fprintf(interp
, interp
->cookie_stdout
, " ");
3950 for (x
= 0 ; (x
< 16) && (x
< y
) ; x
+= 2) {
3951 z
= target_buffer_get_u16(target
, &(target_buf
[ x
* 2 ]));
3952 Jim_fprintf(interp
, interp
->cookie_stdout
, "%04x ", (int)(z
));
3954 for (; (x
< 16) ; x
+= 2) {
3955 Jim_fprintf(interp
, interp
->cookie_stdout
, " ");
3960 for (x
= 0 ; (x
< 16) && (x
< y
) ; x
+= 1) {
3961 z
= target_buffer_get_u8(target
, &(target_buf
[ x
* 4 ]));
3962 Jim_fprintf(interp
, interp
->cookie_stdout
, "%02x ", (int)(z
));
3964 for (; (x
< 16) ; x
+= 1) {
3965 Jim_fprintf(interp
, interp
->cookie_stdout
, " ");
3969 /* ascii-ify the bytes */
3970 for (x
= 0 ; x
< y
; x
++) {
3971 if ((target_buf
[x
] >= 0x20) &&
3972 (target_buf
[x
] <= 0x7e)) {
3976 target_buf
[x
] = '.';
3981 target_buf
[x
] = ' ';
3986 /* print - with a newline */
3987 Jim_fprintf(interp
, interp
->cookie_stdout
, "%s\n", target_buf
);
3993 case TS_CMD_MEM2ARRAY
:
3994 return target_mem2array(goi
.interp
, target
, goi
.argc
, goi
.argv
);
3996 case TS_CMD_ARRAY2MEM
:
3997 return target_array2mem(goi
.interp
, target
, goi
.argc
, goi
.argv
);
3999 case TS_CMD_EXAMINE
:
4001 Jim_WrongNumArgs(goi
.interp
, 2, argv
, "[no parameters]");
4004 if (!target
->tap
->enabled
)
4005 goto err_tap_disabled
;
4006 e
= target
->type
->examine(target
);
4007 if (e
!= ERROR_OK
) {
4008 Jim_SetResult_sprintf(interp
, "examine-fails: %d", e
);
4014 Jim_WrongNumArgs(goi
.interp
, 2, argv
, "[no parameters]");
4017 if (!target
->tap
->enabled
)
4018 goto err_tap_disabled
;
4019 if (!(target_was_examined(target
))) {
4020 e
= ERROR_TARGET_NOT_EXAMINED
;
4022 e
= target
->type
->poll(target
);
4024 if (e
!= ERROR_OK
) {
4025 Jim_SetResult_sprintf(interp
, "poll-fails: %d", e
);
4032 if (goi
.argc
!= 2) {
4033 Jim_WrongNumArgs(interp
, 2, argv
,
4034 "([tT]|[fF]|assert|deassert) BOOL");
4037 e
= Jim_GetOpt_Nvp(&goi
, nvp_assert
, &n
);
4039 Jim_GetOpt_NvpUnknown(&goi
, nvp_assert
, 1);
4042 /* the halt or not param */
4043 e
= Jim_GetOpt_Wide(&goi
, &a
);
4047 if (!target
->tap
->enabled
)
4048 goto err_tap_disabled
;
4049 if (!target
->type
->assert_reset
4050 || !target
->type
->deassert_reset
) {
4051 Jim_SetResult_sprintf(interp
,
4052 "No target-specific reset for %s",
4056 /* determine if we should halt or not. */
4057 target
->reset_halt
= !!a
;
4058 /* When this happens - all workareas are invalid. */
4059 target_free_all_working_areas_restore(target
, 0);
4062 if (n
->value
== NVP_ASSERT
) {
4063 target
->type
->assert_reset(target
);
4065 target
->type
->deassert_reset(target
);
4070 Jim_WrongNumArgs(goi
.interp
, 0, argv
, "halt [no parameters]");
4073 if (!target
->tap
->enabled
)
4074 goto err_tap_disabled
;
4075 target
->type
->halt(target
);
4077 case TS_CMD_WAITSTATE
:
4078 /* params: <name> statename timeoutmsecs */
4079 if (goi
.argc
!= 2) {
4080 Jim_SetResult_sprintf(goi
.interp
, "%s STATENAME TIMEOUTMSECS", n
->name
);
4083 e
= Jim_GetOpt_Nvp(&goi
, nvp_target_state
, &n
);
4085 Jim_GetOpt_NvpUnknown(&goi
, nvp_target_state
,1);
4088 e
= Jim_GetOpt_Wide(&goi
, &a
);
4092 if (!target
->tap
->enabled
)
4093 goto err_tap_disabled
;
4094 e
= target_wait_state(target
, n
->value
, a
);
4095 if (e
!= ERROR_OK
) {
4096 Jim_SetResult_sprintf(goi
.interp
,
4097 "target: %s wait %s fails (%d) %s",
4100 e
, target_strerror_safe(e
));
4105 case TS_CMD_EVENTLIST
:
4106 /* List for human, Events defined for this target.
4107 * scripts/programs should use 'name cget -event NAME'
4110 target_event_action_t
*teap
;
4111 teap
= target
->event_action
;
4112 command_print(cmd_ctx
, "Event actions for target (%d) %s\n",
4113 target
->target_number
,
4115 command_print(cmd_ctx
, "%-25s | Body", "Event");
4116 command_print(cmd_ctx
, "------------------------- | ----------------------------------------");
4118 command_print(cmd_ctx
,
4120 Jim_Nvp_value2name_simple(nvp_target_event
, teap
->event
)->name
,
4121 Jim_GetString(teap
->body
, NULL
));
4124 command_print(cmd_ctx
, "***END***");
4127 case TS_CMD_CURSTATE
:
4128 if (goi
.argc
!= 0) {
4129 Jim_WrongNumArgs(goi
.interp
, 0, argv
, "[no parameters]");
4132 Jim_SetResultString(goi
.interp
,
4133 target_state_name( target
),
4136 case TS_CMD_INVOKE_EVENT
:
4137 if (goi
.argc
!= 1) {
4138 Jim_SetResult_sprintf(goi
.interp
, "%s ?EVENTNAME?",n
->name
);
4141 e
= Jim_GetOpt_Nvp(&goi
, nvp_target_event
, &n
);
4143 Jim_GetOpt_NvpUnknown(&goi
, nvp_target_event
, 1);
4146 target_handle_event(target
, n
->value
);
4152 Jim_SetResult_sprintf(interp
, "[TAP is disabled]");
4156 static int target_create(Jim_GetOptInfo
*goi
)
4165 struct command_context_s
*cmd_ctx
;
4167 cmd_ctx
= Jim_GetAssocData(goi
->interp
, "context");
4168 if (goi
->argc
< 3) {
4169 Jim_WrongNumArgs(goi
->interp
, 1, goi
->argv
, "?name? ?type? ..options...");
4174 Jim_GetOpt_Obj(goi
, &new_cmd
);
4175 /* does this command exist? */
4176 cmd
= Jim_GetCommand(goi
->interp
, new_cmd
, JIM_ERRMSG
);
4178 cp
= Jim_GetString(new_cmd
, NULL
);
4179 Jim_SetResult_sprintf(goi
->interp
, "Command/target: %s Exists", cp
);
4184 e
= Jim_GetOpt_String(goi
, &cp2
, NULL
);
4186 /* now does target type exist */
4187 for (x
= 0 ; target_types
[x
] ; x
++) {
4188 if (0 == strcmp(cp
, target_types
[x
]->name
)) {
4193 if (target_types
[x
] == NULL
) {
4194 Jim_SetResult_sprintf(goi
->interp
, "Unknown target type %s, try one of ", cp
);
4195 for (x
= 0 ; target_types
[x
] ; x
++) {
4196 if (target_types
[x
+ 1]) {
4197 Jim_AppendStrings(goi
->interp
,
4198 Jim_GetResult(goi
->interp
),
4199 target_types
[x
]->name
,
4202 Jim_AppendStrings(goi
->interp
,
4203 Jim_GetResult(goi
->interp
),
4205 target_types
[x
]->name
,NULL
);
4212 target
= calloc(1,sizeof(target_t
));
4213 /* set target number */
4214 target
->target_number
= new_target_number();
4216 /* allocate memory for each unique target type */
4217 target
->type
= (target_type_t
*)calloc(1,sizeof(target_type_t
));
4219 memcpy(target
->type
, target_types
[x
], sizeof(target_type_t
));
4221 /* will be set by "-endian" */
4222 target
->endianness
= TARGET_ENDIAN_UNKNOWN
;
4224 target
->working_area
= 0x0;
4225 target
->working_area_size
= 0x0;
4226 target
->working_areas
= NULL
;
4227 target
->backup_working_area
= 0;
4229 target
->state
= TARGET_UNKNOWN
;
4230 target
->debug_reason
= DBG_REASON_UNDEFINED
;
4231 target
->reg_cache
= NULL
;
4232 target
->breakpoints
= NULL
;
4233 target
->watchpoints
= NULL
;
4234 target
->next
= NULL
;
4235 target
->arch_info
= NULL
;
4237 target
->display
= 1;
4239 /* initialize trace information */
4240 target
->trace_info
= malloc(sizeof(trace_t
));
4241 target
->trace_info
->num_trace_points
= 0;
4242 target
->trace_info
->trace_points_size
= 0;
4243 target
->trace_info
->trace_points
= NULL
;
4244 target
->trace_info
->trace_history_size
= 0;
4245 target
->trace_info
->trace_history
= NULL
;
4246 target
->trace_info
->trace_history_pos
= 0;
4247 target
->trace_info
->trace_history_overflowed
= 0;
4249 target
->dbgmsg
= NULL
;
4250 target
->dbg_msg_enabled
= 0;
4252 target
->endianness
= TARGET_ENDIAN_UNKNOWN
;
4254 /* Do the rest as "configure" options */
4255 goi
->isconfigure
= 1;
4256 e
= target_configure(goi
, target
);
4258 if (target
->tap
== NULL
)
4260 Jim_SetResultString(interp
, "-chain-position required when creating target", -1);
4270 if (target
->endianness
== TARGET_ENDIAN_UNKNOWN
) {
4271 /* default endian to little if not specified */
4272 target
->endianness
= TARGET_LITTLE_ENDIAN
;
4275 /* incase variant is not set */
4276 if (!target
->variant
)
4277 target
->variant
= strdup("");
4279 /* create the target specific commands */
4280 if (target
->type
->register_commands
) {
4281 (*(target
->type
->register_commands
))(cmd_ctx
);
4283 if (target
->type
->target_create
) {
4284 (*(target
->type
->target_create
))(target
, goi
->interp
);
4287 /* append to end of list */
4290 tpp
= &(all_targets
);
4292 tpp
= &((*tpp
)->next
);
4297 cp
= Jim_GetString(new_cmd
, NULL
);
4298 target
->cmd_name
= strdup(cp
);
4300 /* now - create the new target name command */
4301 e
= Jim_CreateCommand(goi
->interp
,
4304 tcl_target_func
, /* C function */
4305 target
, /* private data */
4306 NULL
); /* no del proc */
4311 static int jim_target(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4315 struct command_context_s
*cmd_ctx
;
4319 /* TG = target generic */
4327 const char *target_cmds
[] = {
4328 "create", "types", "names", "current", "number",
4330 NULL
/* terminate */
4333 LOG_DEBUG("Target command params:");
4334 LOG_DEBUG("%s", Jim_Debug_ArgvString(interp
, argc
, argv
));
4336 cmd_ctx
= Jim_GetAssocData(interp
, "context");
4338 Jim_GetOpt_Setup(&goi
, interp
, argc
-1, argv
+ 1);
4340 if (goi
.argc
== 0) {
4341 Jim_WrongNumArgs(interp
, 1, argv
, "missing: command ...");
4345 /* Jim_GetOpt_Debug(&goi); */
4346 r
= Jim_GetOpt_Enum(&goi
, target_cmds
, &x
);
4353 Jim_Panic(goi
.interp
,"Why am I here?");
4355 case TG_CMD_CURRENT
:
4356 if (goi
.argc
!= 0) {
4357 Jim_WrongNumArgs(goi
.interp
, 1, goi
.argv
, "Too many parameters");
4360 Jim_SetResultString(goi
.interp
, get_current_target(cmd_ctx
)->cmd_name
, -1);
4363 if (goi
.argc
!= 0) {
4364 Jim_WrongNumArgs(goi
.interp
, 1, goi
.argv
, "Too many parameters");
4367 Jim_SetResult(goi
.interp
, Jim_NewListObj(goi
.interp
, NULL
, 0));
4368 for (x
= 0 ; target_types
[x
] ; x
++) {
4369 Jim_ListAppendElement(goi
.interp
,
4370 Jim_GetResult(goi
.interp
),
4371 Jim_NewStringObj(goi
.interp
, target_types
[x
]->name
, -1));
4375 if (goi
.argc
!= 0) {
4376 Jim_WrongNumArgs(goi
.interp
, 1, goi
.argv
, "Too many parameters");
4379 Jim_SetResult(goi
.interp
, Jim_NewListObj(goi
.interp
, NULL
, 0));
4380 target
= all_targets
;
4382 Jim_ListAppendElement(goi
.interp
,
4383 Jim_GetResult(goi
.interp
),
4384 Jim_NewStringObj(goi
.interp
, target
->cmd_name
, -1));
4385 target
= target
->next
;
4390 Jim_WrongNumArgs(goi
.interp
, goi
.argc
, goi
.argv
, "?name ... config options ...");
4393 return target_create(&goi
);
4396 /* It's OK to remove this mechanism sometime after August 2010 or so */
4397 LOG_WARNING("don't use numbers as target identifiers; use names");
4398 if (goi
.argc
!= 1) {
4399 Jim_SetResult_sprintf(goi
.interp
, "expected: target number ?NUMBER?");
4402 e
= Jim_GetOpt_Wide(&goi
, &w
);
4406 for (x
= 0, target
= all_targets
; target
; target
= target
->next
, x
++) {
4407 if (target
->target_number
== w
)
4410 if (target
== NULL
) {
4411 Jim_SetResult_sprintf(goi
.interp
,
4412 "Target: number %d does not exist", (int)(w
));
4415 Jim_SetResultString(goi
.interp
, target
->cmd_name
, -1);
4418 if (goi
.argc
!= 0) {
4419 Jim_WrongNumArgs(goi
.interp
, 0, goi
.argv
, "<no parameters>");
4422 for (x
= 0, target
= all_targets
; target
; target
= target
->next
, x
++)
4424 Jim_SetResult(goi
.interp
, Jim_NewIntObj(goi
.interp
, x
));
4440 static int fastload_num
;
4441 static struct FastLoad
*fastload
;
4443 static void free_fastload(void)
4445 if (fastload
!= NULL
)
4448 for (i
= 0; i
< fastload_num
; i
++)
4450 if (fastload
[i
].data
)
4451 free(fastload
[i
].data
);
4461 static int handle_fast_load_image_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
4465 uint32_t image_size
;
4466 uint32_t min_address
= 0;
4467 uint32_t max_address
= 0xffffffff;
4472 duration_t duration
;
4473 char *duration_text
;
4475 int retval
= parse_load_image_command_args(args
, argc
,
4476 &image
, &min_address
, &max_address
);
4477 if (ERROR_OK
!= retval
)
4480 duration_start_measure(&duration
);
4482 if (image_open(&image
, args
[0], (argc
>= 3) ? args
[2] : NULL
) != ERROR_OK
)
4489 fastload_num
= image
.num_sections
;
4490 fastload
= (struct FastLoad
*)malloc(sizeof(struct FastLoad
)*image
.num_sections
);
4491 if (fastload
== NULL
)
4493 image_close(&image
);
4496 memset(fastload
, 0, sizeof(struct FastLoad
)*image
.num_sections
);
4497 for (i
= 0; i
< image
.num_sections
; i
++)
4499 buffer
= malloc(image
.sections
[i
].size
);
4502 command_print(cmd_ctx
, "error allocating buffer for section (%d bytes)",
4503 (int)(image
.sections
[i
].size
));
4507 if ((retval
= image_read_section(&image
, i
, 0x0, image
.sections
[i
].size
, buffer
, &buf_cnt
)) != ERROR_OK
)
4513 uint32_t offset
= 0;
4514 uint32_t length
= buf_cnt
;
4517 /* DANGER!!! beware of unsigned comparision here!!! */
4519 if ((image
.sections
[i
].base_address
+ buf_cnt
>= min_address
)&&
4520 (image
.sections
[i
].base_address
< max_address
))
4522 if (image
.sections
[i
].base_address
< min_address
)
4524 /* clip addresses below */
4525 offset
+= min_address
-image
.sections
[i
].base_address
;
4529 if (image
.sections
[i
].base_address
+ buf_cnt
> max_address
)
4531 length
-= (image
.sections
[i
].base_address
+ buf_cnt
)-max_address
;
4534 fastload
[i
].address
= image
.sections
[i
].base_address
+ offset
;
4535 fastload
[i
].data
= malloc(length
);
4536 if (fastload
[i
].data
== NULL
)
4541 memcpy(fastload
[i
].data
, buffer
+ offset
, length
);
4542 fastload
[i
].length
= length
;
4544 image_size
+= length
;
4545 command_print(cmd_ctx
, "%u bytes written at address 0x%8.8x",
4546 (unsigned int)length
,
4547 ((unsigned int)(image
.sections
[i
].base_address
+ offset
)));
4553 duration_stop_measure(&duration
, &duration_text
);
4554 if (retval
== ERROR_OK
)
4556 command_print(cmd_ctx
, "Loaded %u bytes in %s", (unsigned int)image_size
, duration_text
);
4557 command_print(cmd_ctx
, "NB!!! image has not been loaded to target, issue a subsequent 'fast_load' to do so.");
4559 free(duration_text
);
4561 image_close(&image
);
4563 if (retval
!= ERROR_OK
)
4571 static int handle_fast_load_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
4574 return ERROR_COMMAND_SYNTAX_ERROR
;
4575 if (fastload
== NULL
)
4577 LOG_ERROR("No image in memory");
4581 int ms
= timeval_ms();
4583 int retval
= ERROR_OK
;
4584 for (i
= 0; i
< fastload_num
;i
++)
4586 target_t
*target
= get_current_target(cmd_ctx
);
4587 command_print(cmd_ctx
, "Write to 0x%08x, length 0x%08x",
4588 (unsigned int)(fastload
[i
].address
),
4589 (unsigned int)(fastload
[i
].length
));
4590 if (retval
== ERROR_OK
)
4592 retval
= target_write_buffer(target
, fastload
[i
].address
, fastload
[i
].length
, fastload
[i
].data
);
4594 size
+= fastload
[i
].length
;
4596 int after
= timeval_ms();
4597 command_print(cmd_ctx
, "Loaded image %f kBytes/s", (float)(size
/1024.0)/((float)(after
-ms
)/1000.0));