1 /***************************************************************************
2 * Copyright (C) 2005 by Dominic Rath *
3 * Dominic.Rath@gmx.de *
5 * Copyright (C) 2007,2008 Ø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 feroceon_target
;
86 extern target_type_t xscale_target
;
87 extern target_type_t cortexm3_target
;
88 extern target_type_t cortexa8_target
;
89 extern target_type_t arm11_target
;
90 extern target_type_t mips_m4k_target
;
91 extern target_type_t avr_target
;
93 target_type_t
*target_types
[] =
111 target_t
*all_targets
= NULL
;
112 target_event_callback_t
*target_event_callbacks
= NULL
;
113 target_timer_callback_t
*target_timer_callbacks
= NULL
;
115 const Jim_Nvp nvp_assert
[] = {
116 { .name
= "assert", NVP_ASSERT
},
117 { .name
= "deassert", NVP_DEASSERT
},
118 { .name
= "T", NVP_ASSERT
},
119 { .name
= "F", NVP_DEASSERT
},
120 { .name
= "t", NVP_ASSERT
},
121 { .name
= "f", NVP_DEASSERT
},
122 { .name
= NULL
, .value
= -1 }
125 const Jim_Nvp nvp_error_target
[] = {
126 { .value
= ERROR_TARGET_INVALID
, .name
= "err-invalid" },
127 { .value
= ERROR_TARGET_INIT_FAILED
, .name
= "err-init-failed" },
128 { .value
= ERROR_TARGET_TIMEOUT
, .name
= "err-timeout" },
129 { .value
= ERROR_TARGET_NOT_HALTED
, .name
= "err-not-halted" },
130 { .value
= ERROR_TARGET_FAILURE
, .name
= "err-failure" },
131 { .value
= ERROR_TARGET_UNALIGNED_ACCESS
, .name
= "err-unaligned-access" },
132 { .value
= ERROR_TARGET_DATA_ABORT
, .name
= "err-data-abort" },
133 { .value
= ERROR_TARGET_RESOURCE_NOT_AVAILABLE
, .name
= "err-resource-not-available" },
134 { .value
= ERROR_TARGET_TRANSLATION_FAULT
, .name
= "err-translation-fault" },
135 { .value
= ERROR_TARGET_NOT_RUNNING
, .name
= "err-not-running" },
136 { .value
= ERROR_TARGET_NOT_EXAMINED
, .name
= "err-not-examined" },
137 { .value
= -1, .name
= NULL
}
140 const char *target_strerror_safe( int err
)
144 n
= Jim_Nvp_value2name_simple( nvp_error_target
, err
);
145 if( n
->name
== NULL
){
152 static const Jim_Nvp nvp_target_event
[] = {
153 { .value
= TARGET_EVENT_OLD_gdb_program_config
, .name
= "old-gdb_program_config" },
154 { .value
= TARGET_EVENT_OLD_pre_resume
, .name
= "old-pre_resume" },
156 { .value
= TARGET_EVENT_EARLY_HALTED
, .name
= "early-halted" },
157 { .value
= TARGET_EVENT_HALTED
, .name
= "halted" },
158 { .value
= TARGET_EVENT_RESUMED
, .name
= "resumed" },
159 { .value
= TARGET_EVENT_RESUME_START
, .name
= "resume-start" },
160 { .value
= TARGET_EVENT_RESUME_END
, .name
= "resume-end" },
162 { .name
= "gdb-start", .value
= TARGET_EVENT_GDB_START
},
163 { .name
= "gdb-end", .value
= TARGET_EVENT_GDB_END
},
165 /* historical name */
167 { .value
= TARGET_EVENT_RESET_START
, .name
= "reset-start" },
169 { .value
= TARGET_EVENT_RESET_ASSERT_PRE
, .name
= "reset-assert-pre" },
170 { .value
= TARGET_EVENT_RESET_ASSERT_POST
, .name
= "reset-assert-post" },
171 { .value
= TARGET_EVENT_RESET_DEASSERT_PRE
, .name
= "reset-deassert-pre" },
172 { .value
= TARGET_EVENT_RESET_DEASSERT_POST
, .name
= "reset-deassert-post" },
173 { .value
= TARGET_EVENT_RESET_HALT_PRE
, .name
= "reset-halt-pre" },
174 { .value
= TARGET_EVENT_RESET_HALT_POST
, .name
= "reset-halt-post" },
175 { .value
= TARGET_EVENT_RESET_WAIT_PRE
, .name
= "reset-wait-pre" },
176 { .value
= TARGET_EVENT_RESET_WAIT_POST
, .name
= "reset-wait-post" },
177 { .value
= TARGET_EVENT_RESET_INIT
, .name
= "reset-init" },
178 { .value
= TARGET_EVENT_RESET_END
, .name
= "reset-end" },
180 { .value
= TARGET_EVENT_EXAMINE_START
, .name
= "examine-start" },
181 { .value
= TARGET_EVENT_EXAMINE_END
, .name
= "examine-end" },
183 { .value
= TARGET_EVENT_DEBUG_HALTED
, .name
= "debug-halted" },
184 { .value
= TARGET_EVENT_DEBUG_RESUMED
, .name
= "debug-resumed" },
186 { .value
= TARGET_EVENT_GDB_ATTACH
, .name
= "gdb-attach" },
187 { .value
= TARGET_EVENT_GDB_DETACH
, .name
= "gdb-detach" },
189 { .value
= TARGET_EVENT_GDB_FLASH_WRITE_START
, .name
= "gdb-flash-write-start" },
190 { .value
= TARGET_EVENT_GDB_FLASH_WRITE_END
, .name
= "gdb-flash-write-end" },
192 { .value
= TARGET_EVENT_GDB_FLASH_ERASE_START
, .name
= "gdb-flash-erase-start" },
193 { .value
= TARGET_EVENT_GDB_FLASH_ERASE_END
, .name
= "gdb-flash-erase-end" },
195 { .value
= TARGET_EVENT_RESUME_START
, .name
= "resume-start" },
196 { .value
= TARGET_EVENT_RESUMED
, .name
= "resume-ok" },
197 { .value
= TARGET_EVENT_RESUME_END
, .name
= "resume-end" },
199 { .name
= NULL
, .value
= -1 }
202 const Jim_Nvp nvp_target_state
[] = {
203 { .name
= "unknown", .value
= TARGET_UNKNOWN
},
204 { .name
= "running", .value
= TARGET_RUNNING
},
205 { .name
= "halted", .value
= TARGET_HALTED
},
206 { .name
= "reset", .value
= TARGET_RESET
},
207 { .name
= "debug-running", .value
= TARGET_DEBUG_RUNNING
},
208 { .name
= NULL
, .value
= -1 },
211 const Jim_Nvp nvp_target_debug_reason
[] = {
212 { .name
= "debug-request" , .value
= DBG_REASON_DBGRQ
},
213 { .name
= "breakpoint" , .value
= DBG_REASON_BREAKPOINT
},
214 { .name
= "watchpoint" , .value
= DBG_REASON_WATCHPOINT
},
215 { .name
= "watchpoint-and-breakpoint", .value
= DBG_REASON_WPTANDBKPT
},
216 { .name
= "single-step" , .value
= DBG_REASON_SINGLESTEP
},
217 { .name
= "target-not-halted" , .value
= DBG_REASON_NOTHALTED
},
218 { .name
= "undefined" , .value
= DBG_REASON_UNDEFINED
},
219 { .name
= NULL
, .value
= -1 },
222 const Jim_Nvp nvp_target_endian
[] = {
223 { .name
= "big", .value
= TARGET_BIG_ENDIAN
},
224 { .name
= "little", .value
= TARGET_LITTLE_ENDIAN
},
225 { .name
= "be", .value
= TARGET_BIG_ENDIAN
},
226 { .name
= "le", .value
= TARGET_LITTLE_ENDIAN
},
227 { .name
= NULL
, .value
= -1 },
230 const Jim_Nvp nvp_reset_modes
[] = {
231 { .name
= "unknown", .value
= RESET_UNKNOWN
},
232 { .name
= "run" , .value
= RESET_RUN
},
233 { .name
= "halt" , .value
= RESET_HALT
},
234 { .name
= "init" , .value
= RESET_INIT
},
235 { .name
= NULL
, .value
= -1 },
238 static int max_target_number(void)
246 if( x
< t
->target_number
){
247 x
= (t
->target_number
)+1;
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 static int target_continous_poll
= 1;
274 /* read a u32 from a buffer in target memory endianness */
275 u32
target_buffer_get_u32(target_t
*target
, const u8
*buffer
)
277 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
278 return le_to_h_u32(buffer
);
280 return be_to_h_u32(buffer
);
283 /* read a u16 from a buffer in target memory endianness */
284 u16
target_buffer_get_u16(target_t
*target
, const u8
*buffer
)
286 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
287 return le_to_h_u16(buffer
);
289 return be_to_h_u16(buffer
);
292 /* read a u8 from a buffer in target memory endianness */
293 u8
target_buffer_get_u8(target_t
*target
, const u8
*buffer
)
295 return *buffer
& 0x0ff;
298 /* write a u32 to a buffer in target memory endianness */
299 void target_buffer_set_u32(target_t
*target
, u8
*buffer
, u32 value
)
301 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
302 h_u32_to_le(buffer
, value
);
304 h_u32_to_be(buffer
, value
);
307 /* write a u16 to a buffer in target memory endianness */
308 void target_buffer_set_u16(target_t
*target
, u8
*buffer
, u16 value
)
310 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
311 h_u16_to_le(buffer
, value
);
313 h_u16_to_be(buffer
, value
);
316 /* write a u8 to a buffer in target memory endianness */
317 void target_buffer_set_u8(target_t
*target
, u8
*buffer
, u8 value
)
322 /* return a pointer to a configured target; id is name or number */
323 target_t
*get_target(const char *id
)
329 /* try as tcltarget name */
330 for (target
= all_targets
; target
; target
= target
->next
) {
331 if (target
->cmd_name
== NULL
)
333 if (strcmp(id
, target
->cmd_name
) == 0)
337 /* no match, try as number */
338 num
= strtoul(id
, &endptr
, 0);
342 for (target
= all_targets
; target
; target
= target
->next
) {
343 if (target
->target_number
== num
)
350 /* returns a pointer to the n-th configured target */
351 static target_t
*get_target_by_num(int num
)
353 target_t
*target
= all_targets
;
356 if( target
->target_number
== num
){
359 target
= target
->next
;
365 int get_num_by_target(target_t
*query_target
)
367 return query_target
->target_number
;
370 target_t
* get_current_target(command_context_t
*cmd_ctx
)
372 target_t
*target
= get_target_by_num(cmd_ctx
->current_target
);
376 LOG_ERROR("BUG: current_target out of bounds");
383 int target_poll(struct target_s
*target
)
385 /* We can't poll until after examine */
386 if (!target_was_examined(target
))
388 /* Fail silently lest we pollute the log */
391 return target
->type
->poll(target
);
394 int target_halt(struct target_s
*target
)
396 /* We can't poll until after examine */
397 if (!target_was_examined(target
))
399 LOG_ERROR("Target not examined yet");
402 return target
->type
->halt(target
);
405 int target_resume(struct target_s
*target
, int current
, u32 address
, int handle_breakpoints
, int debug_execution
)
409 /* We can't poll until after examine */
410 if (!target_was_examined(target
))
412 LOG_ERROR("Target not examined yet");
416 /* note that resume *must* be asynchronous. The CPU can halt before we poll. The CPU can
417 * even halt at the current PC as a result of a software breakpoint being inserted by (a bug?)
420 if ((retval
= target
->type
->resume(target
, current
, address
, handle_breakpoints
, debug_execution
)) != ERROR_OK
)
426 int target_process_reset(struct command_context_s
*cmd_ctx
, enum target_reset_mode reset_mode
)
431 n
= Jim_Nvp_value2name_simple( nvp_reset_modes
, reset_mode
);
432 if( n
->name
== NULL
){
433 LOG_ERROR("invalid reset mode");
437 sprintf( buf
, "ocd_process_reset %s", n
->name
);
438 retval
= Jim_Eval( interp
, buf
);
440 if(retval
!= JIM_OK
) {
441 Jim_PrintErrorMessage(interp
);
445 /* We want any events to be processed before the prompt */
446 retval
= target_call_timer_callbacks_now();
451 static int default_virt2phys(struct target_s
*target
, u32
virtual, u32
*physical
)
457 static int default_mmu(struct target_s
*target
, int *enabled
)
463 static int default_examine(struct target_s
*target
)
465 target_set_examined(target
);
469 int target_examine_one(struct target_s
*target
)
471 return target
->type
->examine(target
);
474 /* Targets that correctly implement init+examine, i.e.
475 * no communication with target during init:
479 int target_examine(void)
481 int retval
= ERROR_OK
;
482 target_t
*target
= all_targets
;
485 if ((retval
= target_examine_one(target
)) != ERROR_OK
)
487 target
= target
->next
;
491 const char *target_get_name(struct target_s
*target
)
493 return target
->type
->name
;
496 static int target_write_memory_imp(struct target_s
*target
, u32 address
, u32 size
, u32 count
, u8
*buffer
)
498 if (!target_was_examined(target
))
500 LOG_ERROR("Target not examined yet");
503 return target
->type
->write_memory_imp(target
, address
, size
, count
, buffer
);
506 static int target_read_memory_imp(struct target_s
*target
, u32 address
, u32 size
, u32 count
, u8
*buffer
)
508 if (!target_was_examined(target
))
510 LOG_ERROR("Target not examined yet");
513 return target
->type
->read_memory_imp(target
, address
, size
, count
, buffer
);
516 static int target_soft_reset_halt_imp(struct target_s
*target
)
518 if (!target_was_examined(target
))
520 LOG_ERROR("Target not examined yet");
523 return target
->type
->soft_reset_halt_imp(target
);
526 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
, u32 entry_point
, u32 exit_point
, int timeout_ms
, void *arch_info
)
528 if (!target_was_examined(target
))
530 LOG_ERROR("Target not examined yet");
533 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
);
536 int target_read_memory(struct target_s
*target
,
537 u32 address
, u32 size
, u32 count
, u8
*buffer
)
539 return target
->type
->read_memory(target
, address
, size
, count
, buffer
);
542 int target_write_memory(struct target_s
*target
,
543 u32 address
, u32 size
, u32 count
, u8
*buffer
)
545 return target
->type
->write_memory(target
, address
, size
, count
, buffer
);
547 int target_bulk_write_memory(struct target_s
*target
,
548 u32 address
, u32 count
, u8
*buffer
)
550 return target
->type
->bulk_write_memory(target
, address
, count
, buffer
);
553 int target_add_breakpoint(struct target_s
*target
,
554 struct breakpoint_s
*breakpoint
)
556 return target
->type
->add_breakpoint(target
, breakpoint
);
558 int target_remove_breakpoint(struct target_s
*target
,
559 struct breakpoint_s
*breakpoint
)
561 return target
->type
->remove_breakpoint(target
, breakpoint
);
564 int target_add_watchpoint(struct target_s
*target
,
565 struct watchpoint_s
*watchpoint
)
567 return target
->type
->add_watchpoint(target
, watchpoint
);
569 int target_remove_watchpoint(struct target_s
*target
,
570 struct watchpoint_s
*watchpoint
)
572 return target
->type
->remove_watchpoint(target
, watchpoint
);
575 int target_get_gdb_reg_list(struct target_s
*target
,
576 struct reg_s
**reg_list
[], int *reg_list_size
)
578 return target
->type
->get_gdb_reg_list(target
, reg_list
, reg_list_size
);
580 int target_step(struct target_s
*target
,
581 int current
, u32 address
, int handle_breakpoints
)
583 return target
->type
->step(target
, current
, address
, handle_breakpoints
);
587 int target_run_algorithm(struct target_s
*target
,
588 int num_mem_params
, mem_param_t
*mem_params
,
589 int num_reg_params
, reg_param_t
*reg_param
,
590 u32 entry_point
, u32 exit_point
,
591 int timeout_ms
, void *arch_info
)
593 return target
->type
->run_algorithm(target
,
594 num_mem_params
, mem_params
, num_reg_params
, reg_param
,
595 entry_point
, exit_point
, timeout_ms
, arch_info
);
598 /// @returns @c true if the target has been examined.
599 bool target_was_examined(struct target_s
*target
)
601 return target
->type
->examined
;
603 /// Sets the @c examined flag for the given target.
604 void target_set_examined(struct target_s
*target
)
606 target
->type
->examined
= true;
608 // Reset the @c examined flag for the given target.
609 void target_reset_examined(struct target_s
*target
)
611 target
->type
->examined
= false;
615 int target_init(struct command_context_s
*cmd_ctx
)
617 target_t
*target
= all_targets
;
622 target_reset_examined(target
);
623 if (target
->type
->examine
== NULL
)
625 target
->type
->examine
= default_examine
;
628 if ((retval
= target
->type
->init_target(cmd_ctx
, target
)) != ERROR_OK
)
630 LOG_ERROR("target '%s' init failed", target_get_name(target
));
634 /* Set up default functions if none are provided by target */
635 if (target
->type
->virt2phys
== NULL
)
637 target
->type
->virt2phys
= default_virt2phys
;
639 target
->type
->virt2phys
= default_virt2phys
;
640 /* a non-invasive way(in terms of patches) to add some code that
641 * runs before the type->write/read_memory implementation
643 target
->type
->write_memory_imp
= target
->type
->write_memory
;
644 target
->type
->write_memory
= target_write_memory_imp
;
645 target
->type
->read_memory_imp
= target
->type
->read_memory
;
646 target
->type
->read_memory
= target_read_memory_imp
;
647 target
->type
->soft_reset_halt_imp
= target
->type
->soft_reset_halt
;
648 target
->type
->soft_reset_halt
= target_soft_reset_halt_imp
;
649 target
->type
->run_algorithm_imp
= target
->type
->run_algorithm
;
650 target
->type
->run_algorithm
= target_run_algorithm_imp
;
652 if (target
->type
->mmu
== NULL
)
654 target
->type
->mmu
= default_mmu
;
656 target
= target
->next
;
661 if((retval
= target_register_user_commands(cmd_ctx
)) != ERROR_OK
)
663 if((retval
= target_register_timer_callback(handle_target
, 100, 1, NULL
)) != ERROR_OK
)
670 int target_register_event_callback(int (*callback
)(struct target_s
*target
, enum target_event event
, void *priv
), void *priv
)
672 target_event_callback_t
**callbacks_p
= &target_event_callbacks
;
674 if (callback
== NULL
)
676 return ERROR_INVALID_ARGUMENTS
;
681 while ((*callbacks_p
)->next
)
682 callbacks_p
= &((*callbacks_p
)->next
);
683 callbacks_p
= &((*callbacks_p
)->next
);
686 (*callbacks_p
) = malloc(sizeof(target_event_callback_t
));
687 (*callbacks_p
)->callback
= callback
;
688 (*callbacks_p
)->priv
= priv
;
689 (*callbacks_p
)->next
= NULL
;
694 int target_register_timer_callback(int (*callback
)(void *priv
), int time_ms
, int periodic
, void *priv
)
696 target_timer_callback_t
**callbacks_p
= &target_timer_callbacks
;
699 if (callback
== NULL
)
701 return ERROR_INVALID_ARGUMENTS
;
706 while ((*callbacks_p
)->next
)
707 callbacks_p
= &((*callbacks_p
)->next
);
708 callbacks_p
= &((*callbacks_p
)->next
);
711 (*callbacks_p
) = malloc(sizeof(target_timer_callback_t
));
712 (*callbacks_p
)->callback
= callback
;
713 (*callbacks_p
)->periodic
= periodic
;
714 (*callbacks_p
)->time_ms
= time_ms
;
716 gettimeofday(&now
, NULL
);
717 (*callbacks_p
)->when
.tv_usec
= now
.tv_usec
+ (time_ms
% 1000) * 1000;
718 time_ms
-= (time_ms
% 1000);
719 (*callbacks_p
)->when
.tv_sec
= now
.tv_sec
+ (time_ms
/ 1000);
720 if ((*callbacks_p
)->when
.tv_usec
> 1000000)
722 (*callbacks_p
)->when
.tv_usec
= (*callbacks_p
)->when
.tv_usec
- 1000000;
723 (*callbacks_p
)->when
.tv_sec
+= 1;
726 (*callbacks_p
)->priv
= priv
;
727 (*callbacks_p
)->next
= NULL
;
732 int target_unregister_event_callback(int (*callback
)(struct target_s
*target
, enum target_event event
, void *priv
), void *priv
)
734 target_event_callback_t
**p
= &target_event_callbacks
;
735 target_event_callback_t
*c
= target_event_callbacks
;
737 if (callback
== NULL
)
739 return ERROR_INVALID_ARGUMENTS
;
744 target_event_callback_t
*next
= c
->next
;
745 if ((c
->callback
== callback
) && (c
->priv
== priv
))
759 int target_unregister_timer_callback(int (*callback
)(void *priv
), void *priv
)
761 target_timer_callback_t
**p
= &target_timer_callbacks
;
762 target_timer_callback_t
*c
= target_timer_callbacks
;
764 if (callback
== NULL
)
766 return ERROR_INVALID_ARGUMENTS
;
771 target_timer_callback_t
*next
= c
->next
;
772 if ((c
->callback
== callback
) && (c
->priv
== priv
))
786 int target_call_event_callbacks(target_t
*target
, enum target_event event
)
788 target_event_callback_t
*callback
= target_event_callbacks
;
789 target_event_callback_t
*next_callback
;
791 if (event
== TARGET_EVENT_HALTED
)
793 /* execute early halted first */
794 target_call_event_callbacks(target
, TARGET_EVENT_EARLY_HALTED
);
797 LOG_DEBUG("target event %i (%s)",
799 Jim_Nvp_value2name_simple( nvp_target_event
, event
)->name
);
801 target_handle_event( target
, event
);
805 next_callback
= callback
->next
;
806 callback
->callback(target
, event
, callback
->priv
);
807 callback
= next_callback
;
813 static int target_call_timer_callbacks_check_time(int checktime
)
815 target_timer_callback_t
*callback
= target_timer_callbacks
;
816 target_timer_callback_t
*next_callback
;
821 gettimeofday(&now
, NULL
);
825 next_callback
= callback
->next
;
827 if ((!checktime
&&callback
->periodic
)||
828 (((now
.tv_sec
>= callback
->when
.tv_sec
) && (now
.tv_usec
>= callback
->when
.tv_usec
))
829 || (now
.tv_sec
> callback
->when
.tv_sec
)))
831 if(callback
->callback
!= NULL
)
833 callback
->callback(callback
->priv
);
834 if (callback
->periodic
)
836 int time_ms
= callback
->time_ms
;
837 callback
->when
.tv_usec
= now
.tv_usec
+ (time_ms
% 1000) * 1000;
838 time_ms
-= (time_ms
% 1000);
839 callback
->when
.tv_sec
= now
.tv_sec
+ time_ms
/ 1000;
840 if (callback
->when
.tv_usec
> 1000000)
842 callback
->when
.tv_usec
= callback
->when
.tv_usec
- 1000000;
843 callback
->when
.tv_sec
+= 1;
849 if((retval
= target_unregister_timer_callback(callback
->callback
, callback
->priv
)) != ERROR_OK
)
855 callback
= next_callback
;
861 int target_call_timer_callbacks(void)
863 return target_call_timer_callbacks_check_time(1);
866 /* invoke periodic callbacks immediately */
867 int target_call_timer_callbacks_now(void)
869 return target_call_timer_callbacks_check_time(0);
872 int target_alloc_working_area(struct target_s
*target
, u32 size
, working_area_t
**area
)
874 working_area_t
*c
= target
->working_areas
;
875 working_area_t
*new_wa
= NULL
;
877 /* Reevaluate working area address based on MMU state*/
878 if (target
->working_areas
== NULL
)
882 retval
= target
->type
->mmu(target
, &enabled
);
883 if (retval
!= ERROR_OK
)
889 target
->working_area
= target
->working_area_virt
;
893 target
->working_area
= target
->working_area_phys
;
897 /* only allocate multiples of 4 byte */
900 LOG_ERROR("BUG: code tried to allocate unaligned number of bytes, padding");
901 size
= CEIL(size
, 4);
904 /* see if there's already a matching working area */
907 if ((c
->free
) && (c
->size
== size
))
915 /* if not, allocate a new one */
918 working_area_t
**p
= &target
->working_areas
;
919 u32 first_free
= target
->working_area
;
920 u32 free_size
= target
->working_area_size
;
922 LOG_DEBUG("allocating new working area");
924 c
= target
->working_areas
;
927 first_free
+= c
->size
;
928 free_size
-= c
->size
;
933 if (free_size
< size
)
935 LOG_WARNING("not enough working area available(requested %d, free %d)", size
, free_size
);
936 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
939 new_wa
= malloc(sizeof(working_area_t
));
942 new_wa
->address
= first_free
;
944 if (target
->backup_working_area
)
947 new_wa
->backup
= malloc(new_wa
->size
);
948 if((retval
= target_read_memory(target
, new_wa
->address
, 4, new_wa
->size
/ 4, new_wa
->backup
)) != ERROR_OK
)
950 free(new_wa
->backup
);
957 new_wa
->backup
= NULL
;
960 /* put new entry in list */
964 /* mark as used, and return the new (reused) area */
974 int target_free_working_area_restore(struct target_s
*target
, working_area_t
*area
, int restore
)
979 if (restore
&&target
->backup_working_area
)
982 if((retval
= target_write_memory(target
, area
->address
, 4, area
->size
/ 4, area
->backup
)) != ERROR_OK
)
988 /* mark user pointer invalid */
995 int target_free_working_area(struct target_s
*target
, working_area_t
*area
)
997 return target_free_working_area_restore(target
, area
, 1);
1000 /* free resources and restore memory, if restoring memory fails,
1001 * free up resources anyway
1003 void target_free_all_working_areas_restore(struct target_s
*target
, int restore
)
1005 working_area_t
*c
= target
->working_areas
;
1009 working_area_t
*next
= c
->next
;
1010 target_free_working_area_restore(target
, c
, restore
);
1020 target
->working_areas
= NULL
;
1023 void target_free_all_working_areas(struct target_s
*target
)
1025 target_free_all_working_areas_restore(target
, 1);
1028 int target_register_commands(struct command_context_s
*cmd_ctx
)
1031 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)");
1036 register_jim(cmd_ctx
, "target", jim_target
, "configure target" );
1041 int target_arch_state(struct target_s
*target
)
1046 LOG_USER("No target has been configured");
1050 LOG_USER("target state: %s",
1051 Jim_Nvp_value2name_simple(nvp_target_state
,target
->state
)->name
);
1053 if (target
->state
!=TARGET_HALTED
)
1056 retval
=target
->type
->arch_state(target
);
1060 /* Single aligned words are guaranteed to use 16 or 32 bit access
1061 * mode respectively, otherwise data is handled as quickly as
1064 int target_write_buffer(struct target_s
*target
, u32 address
, u32 size
, u8
*buffer
)
1067 LOG_DEBUG("writing buffer of %i byte at 0x%8.8x", size
, address
);
1069 if (!target_was_examined(target
))
1071 LOG_ERROR("Target not examined yet");
1079 if ((address
+ size
- 1) < address
)
1081 /* GDB can request this when e.g. PC is 0xfffffffc*/
1082 LOG_ERROR("address+size wrapped(0x%08x, 0x%08x)", address
, size
);
1086 if (((address
% 2) == 0) && (size
== 2))
1088 return target_write_memory(target
, address
, 2, 1, buffer
);
1091 /* handle unaligned head bytes */
1094 u32 unaligned
= 4 - (address
% 4);
1096 if (unaligned
> size
)
1099 if ((retval
= target_write_memory(target
, address
, 1, unaligned
, buffer
)) != ERROR_OK
)
1102 buffer
+= unaligned
;
1103 address
+= unaligned
;
1107 /* handle aligned words */
1110 int aligned
= size
- (size
% 4);
1112 /* use bulk writes above a certain limit. This may have to be changed */
1115 if ((retval
= target
->type
->bulk_write_memory(target
, address
, aligned
/ 4, buffer
)) != ERROR_OK
)
1120 if ((retval
= target_write_memory(target
, address
, 4, aligned
/ 4, buffer
)) != ERROR_OK
)
1129 /* handle tail writes of less than 4 bytes */
1132 if ((retval
= target_write_memory(target
, address
, 1, size
, buffer
)) != ERROR_OK
)
1139 /* Single aligned words are guaranteed to use 16 or 32 bit access
1140 * mode respectively, otherwise data is handled as quickly as
1143 int target_read_buffer(struct target_s
*target
, u32 address
, u32 size
, u8
*buffer
)
1146 LOG_DEBUG("reading buffer of %i byte at 0x%8.8x", size
, address
);
1148 if (!target_was_examined(target
))
1150 LOG_ERROR("Target not examined yet");
1158 if ((address
+ size
- 1) < address
)
1160 /* GDB can request this when e.g. PC is 0xfffffffc*/
1161 LOG_ERROR("address+size wrapped(0x%08x, 0x%08x)", address
, size
);
1165 if (((address
% 2) == 0) && (size
== 2))
1167 return target_read_memory(target
, address
, 2, 1, buffer
);
1170 /* handle unaligned head bytes */
1173 u32 unaligned
= 4 - (address
% 4);
1175 if (unaligned
> size
)
1178 if ((retval
= target_read_memory(target
, address
, 1, unaligned
, buffer
)) != ERROR_OK
)
1181 buffer
+= unaligned
;
1182 address
+= unaligned
;
1186 /* handle aligned words */
1189 int aligned
= size
- (size
% 4);
1191 if ((retval
= target_read_memory(target
, address
, 4, aligned
/ 4, buffer
)) != ERROR_OK
)
1199 /* handle tail writes of less than 4 bytes */
1202 if ((retval
= target_read_memory(target
, address
, 1, size
, buffer
)) != ERROR_OK
)
1209 int target_checksum_memory(struct target_s
*target
, u32 address
, u32 size
, u32
* crc
)
1215 if (!target_was_examined(target
))
1217 LOG_ERROR("Target not examined yet");
1221 if ((retval
= target
->type
->checksum_memory(target
, address
,
1222 size
, &checksum
)) != ERROR_OK
)
1224 buffer
= malloc(size
);
1227 LOG_ERROR("error allocating buffer for section (%d bytes)", size
);
1228 return ERROR_INVALID_ARGUMENTS
;
1230 retval
= target_read_buffer(target
, address
, size
, buffer
);
1231 if (retval
!= ERROR_OK
)
1237 /* convert to target endianess */
1238 for (i
= 0; i
< (size
/sizeof(u32
)); i
++)
1241 target_data
= target_buffer_get_u32(target
, &buffer
[i
*sizeof(u32
)]);
1242 target_buffer_set_u32(target
, &buffer
[i
*sizeof(u32
)], target_data
);
1245 retval
= image_calculate_checksum( buffer
, size
, &checksum
);
1254 int target_blank_check_memory(struct target_s
*target
, u32 address
, u32 size
, u32
* blank
)
1257 if (!target_was_examined(target
))
1259 LOG_ERROR("Target not examined yet");
1263 if (target
->type
->blank_check_memory
== 0)
1264 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
1266 retval
= target
->type
->blank_check_memory(target
, address
, size
, blank
);
1271 int target_read_u32(struct target_s
*target
, u32 address
, u32
*value
)
1274 if (!target_was_examined(target
))
1276 LOG_ERROR("Target not examined yet");
1280 int retval
= target_read_memory(target
, address
, 4, 1, value_buf
);
1282 if (retval
== ERROR_OK
)
1284 *value
= target_buffer_get_u32(target
, value_buf
);
1285 LOG_DEBUG("address: 0x%8.8x, value: 0x%8.8x", address
, *value
);
1290 LOG_DEBUG("address: 0x%8.8x failed", address
);
1296 int target_read_u16(struct target_s
*target
, u32 address
, u16
*value
)
1299 if (!target_was_examined(target
))
1301 LOG_ERROR("Target not examined yet");
1305 int retval
= target_read_memory(target
, address
, 2, 1, value_buf
);
1307 if (retval
== ERROR_OK
)
1309 *value
= target_buffer_get_u16(target
, value_buf
);
1310 LOG_DEBUG("address: 0x%8.8x, value: 0x%4.4x", address
, *value
);
1315 LOG_DEBUG("address: 0x%8.8x failed", address
);
1321 int target_read_u8(struct target_s
*target
, u32 address
, u8
*value
)
1323 int retval
= target_read_memory(target
, address
, 1, 1, value
);
1324 if (!target_was_examined(target
))
1326 LOG_ERROR("Target not examined yet");
1330 if (retval
== ERROR_OK
)
1332 LOG_DEBUG("address: 0x%8.8x, value: 0x%2.2x", address
, *value
);
1337 LOG_DEBUG("address: 0x%8.8x failed", address
);
1343 int target_write_u32(struct target_s
*target
, u32 address
, u32 value
)
1347 if (!target_was_examined(target
))
1349 LOG_ERROR("Target not examined yet");
1353 LOG_DEBUG("address: 0x%8.8x, value: 0x%8.8x", address
, value
);
1355 target_buffer_set_u32(target
, value_buf
, value
);
1356 if ((retval
= target_write_memory(target
, address
, 4, 1, value_buf
)) != ERROR_OK
)
1358 LOG_DEBUG("failed: %i", retval
);
1364 int target_write_u16(struct target_s
*target
, u32 address
, u16 value
)
1368 if (!target_was_examined(target
))
1370 LOG_ERROR("Target not examined yet");
1374 LOG_DEBUG("address: 0x%8.8x, value: 0x%8.8x", address
, value
);
1376 target_buffer_set_u16(target
, value_buf
, value
);
1377 if ((retval
= target_write_memory(target
, address
, 2, 1, value_buf
)) != ERROR_OK
)
1379 LOG_DEBUG("failed: %i", retval
);
1385 int target_write_u8(struct target_s
*target
, u32 address
, u8 value
)
1388 if (!target_was_examined(target
))
1390 LOG_ERROR("Target not examined yet");
1394 LOG_DEBUG("address: 0x%8.8x, value: 0x%2.2x", address
, value
);
1396 if ((retval
= target_write_memory(target
, address
, 1, 1, &value
)) != ERROR_OK
)
1398 LOG_DEBUG("failed: %i", retval
);
1404 int target_register_user_commands(struct command_context_s
*cmd_ctx
)
1406 int retval
= ERROR_OK
;
1409 /* script procedures */
1410 register_command(cmd_ctx
, NULL
, "profile", handle_profile_command
, COMMAND_EXEC
, "profiling samples the CPU PC");
1411 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>");
1412 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>");
1414 register_command(cmd_ctx
, NULL
, "fast_load_image", handle_fast_load_image_command
, COMMAND_ANY
,
1415 "same args as load_image, image stored in memory - mainly for profiling purposes");
1417 register_command(cmd_ctx
, NULL
, "fast_load", handle_fast_load_command
, COMMAND_ANY
,
1418 "loads active fast load image to current target - mainly for profiling purposes");
1421 register_command(cmd_ctx
, NULL
, "virt2phys", handle_virt2phys_command
, COMMAND_ANY
, "translate a virtual address into a physical address");
1422 register_command(cmd_ctx
, NULL
, "reg", handle_reg_command
, COMMAND_EXEC
, "display or set a register");
1423 register_command(cmd_ctx
, NULL
, "poll", handle_poll_command
, COMMAND_EXEC
, "poll target state");
1424 register_command(cmd_ctx
, NULL
, "wait_halt", handle_wait_halt_command
, COMMAND_EXEC
, "wait for target halt [time (s)]");
1425 register_command(cmd_ctx
, NULL
, "halt", handle_halt_command
, COMMAND_EXEC
, "halt target");
1426 register_command(cmd_ctx
, NULL
, "resume", handle_resume_command
, COMMAND_EXEC
, "resume target [addr]");
1427 register_command(cmd_ctx
, NULL
, "step", handle_step_command
, COMMAND_EXEC
, "step one instruction from current PC or [addr]");
1428 register_command(cmd_ctx
, NULL
, "reset", handle_reset_command
, COMMAND_EXEC
, "reset target [run|halt|init] - default is run");
1429 register_command(cmd_ctx
, NULL
, "soft_reset_halt", handle_soft_reset_halt_command
, COMMAND_EXEC
, "halt the target and do a soft reset");
1431 register_command(cmd_ctx
, NULL
, "mdw", handle_md_command
, COMMAND_EXEC
, "display memory words <addr> [count]");
1432 register_command(cmd_ctx
, NULL
, "mdh", handle_md_command
, COMMAND_EXEC
, "display memory half-words <addr> [count]");
1433 register_command(cmd_ctx
, NULL
, "mdb", handle_md_command
, COMMAND_EXEC
, "display memory bytes <addr> [count]");
1435 register_command(cmd_ctx
, NULL
, "mww", handle_mw_command
, COMMAND_EXEC
, "write memory word <addr> <value> [count]");
1436 register_command(cmd_ctx
, NULL
, "mwh", handle_mw_command
, COMMAND_EXEC
, "write memory half-word <addr> <value> [count]");
1437 register_command(cmd_ctx
, NULL
, "mwb", handle_mw_command
, COMMAND_EXEC
, "write memory byte <addr> <value> [count]");
1439 register_command(cmd_ctx
, NULL
, "bp", handle_bp_command
, COMMAND_EXEC
, "set breakpoint <address> <length> [hw]");
1440 register_command(cmd_ctx
, NULL
, "rbp", handle_rbp_command
, COMMAND_EXEC
, "remove breakpoint <adress>");
1441 register_command(cmd_ctx
, NULL
, "wp", handle_wp_command
, COMMAND_EXEC
, "set watchpoint <address> <length> <r/w/a> [value] [mask]");
1442 register_command(cmd_ctx
, NULL
, "rwp", handle_rwp_command
, COMMAND_EXEC
, "remove watchpoint <adress>");
1444 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]");
1445 register_command(cmd_ctx
, NULL
, "dump_image", handle_dump_image_command
, COMMAND_EXEC
, "dump_image <file> <address> <size>");
1446 register_command(cmd_ctx
, NULL
, "verify_image", handle_verify_image_command
, COMMAND_EXEC
, "verify_image <file> [offset] [type]");
1447 register_command(cmd_ctx
, NULL
, "test_image", handle_test_image_command
, COMMAND_EXEC
, "test_image <file> [offset] [type]");
1449 if((retval
= target_request_register_commands(cmd_ctx
)) != ERROR_OK
)
1451 if((retval
= trace_register_commands(cmd_ctx
)) != ERROR_OK
)
1457 static int handle_targets_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
1459 target_t
*target
= all_targets
;
1463 target
= get_target(args
[0]);
1464 if (target
== NULL
) {
1465 command_print(cmd_ctx
,"Target: %s is unknown, try one of:\n", args
[0] );
1469 cmd_ctx
->current_target
= target
->target_number
;
1474 target
= all_targets
;
1475 command_print(cmd_ctx
, " CmdName Type Endian AbsChainPos Name State ");
1476 command_print(cmd_ctx
, "-- ---------- ---------- ---------- ----------- ------------- ----------");
1479 /* XX: abcdefghij abcdefghij abcdefghij abcdefghij */
1480 command_print(cmd_ctx
, "%2d: %-10s %-10s %-10s %10d %14s %s",
1481 target
->target_number
,
1483 target_get_name(target
),
1484 Jim_Nvp_value2name_simple( nvp_target_endian
, target
->endianness
)->name
,
1485 target
->tap
->abs_chain_position
,
1486 target
->tap
->dotted_name
,
1487 Jim_Nvp_value2name_simple( nvp_target_state
, target
->state
)->name
);
1488 target
= target
->next
;
1494 /* every 300ms we check for reset & powerdropout and issue a "reset halt" if so. */
1496 static int powerDropout
;
1497 static int srstAsserted
;
1499 static int runPowerRestore
;
1500 static int runPowerDropout
;
1501 static int runSrstAsserted
;
1502 static int runSrstDeasserted
;
1504 static int sense_handler(void)
1506 static int prevSrstAsserted
= 0;
1507 static int prevPowerdropout
= 0;
1510 if ((retval
=jtag_power_dropout(&powerDropout
))!=ERROR_OK
)
1514 powerRestored
= prevPowerdropout
&& !powerDropout
;
1517 runPowerRestore
= 1;
1520 long long current
= timeval_ms();
1521 static long long lastPower
= 0;
1522 int waitMore
= lastPower
+ 2000 > current
;
1523 if (powerDropout
&& !waitMore
)
1525 runPowerDropout
= 1;
1526 lastPower
= current
;
1529 if ((retval
=jtag_srst_asserted(&srstAsserted
))!=ERROR_OK
)
1533 srstDeasserted
= prevSrstAsserted
&& !srstAsserted
;
1535 static long long lastSrst
= 0;
1536 waitMore
= lastSrst
+ 2000 > current
;
1537 if (srstDeasserted
&& !waitMore
)
1539 runSrstDeasserted
= 1;
1543 if (!prevSrstAsserted
&& srstAsserted
)
1545 runSrstAsserted
= 1;
1548 prevSrstAsserted
= srstAsserted
;
1549 prevPowerdropout
= powerDropout
;
1551 if (srstDeasserted
|| powerRestored
)
1553 /* Other than logging the event we can't do anything here.
1554 * Issuing a reset is a particularly bad idea as we might
1555 * be inside a reset already.
1562 /* process target state changes */
1563 int handle_target(void *priv
)
1565 int retval
= ERROR_OK
;
1567 /* we do not want to recurse here... */
1568 static int recursive
= 0;
1573 /* danger! running these procedures can trigger srst assertions and power dropouts.
1574 * We need to avoid an infinite loop/recursion here and we do that by
1575 * clearing the flags after running these events.
1577 int did_something
= 0;
1578 if (runSrstAsserted
)
1580 Jim_Eval( interp
, "srst_asserted");
1583 if (runSrstDeasserted
)
1585 Jim_Eval( interp
, "srst_deasserted");
1588 if (runPowerDropout
)
1590 Jim_Eval( interp
, "power_dropout");
1593 if (runPowerRestore
)
1595 Jim_Eval( interp
, "power_restore");
1601 /* clear detect flags */
1605 /* clear action flags */
1608 runSrstDeasserted
=0;
1615 target_t
*target
= all_targets
;
1620 /* only poll target if we've got power and srst isn't asserted */
1621 if (target_continous_poll
&&!powerDropout
&&!srstAsserted
)
1623 /* polling may fail silently until the target has been examined */
1624 if((retval
= target_poll(target
)) != ERROR_OK
)
1628 target
= target
->next
;
1634 static int handle_reg_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
1643 target
= get_current_target(cmd_ctx
);
1645 /* list all available registers for the current target */
1648 reg_cache_t
*cache
= target
->reg_cache
;
1654 for (i
= 0; i
< cache
->num_regs
; i
++)
1656 value
= buf_to_str(cache
->reg_list
[i
].value
, cache
->reg_list
[i
].size
, 16);
1657 command_print(cmd_ctx
, "(%i) %s (/%i): 0x%s (dirty: %i, valid: %i)", count
++, cache
->reg_list
[i
].name
, cache
->reg_list
[i
].size
, value
, cache
->reg_list
[i
].dirty
, cache
->reg_list
[i
].valid
);
1660 cache
= cache
->next
;
1666 /* access a single register by its ordinal number */
1667 if ((args
[0][0] >= '0') && (args
[0][0] <= '9'))
1669 int num
= strtoul(args
[0], NULL
, 0);
1670 reg_cache_t
*cache
= target
->reg_cache
;
1676 for (i
= 0; i
< cache
->num_regs
; i
++)
1680 reg
= &cache
->reg_list
[i
];
1686 cache
= cache
->next
;
1691 command_print(cmd_ctx
, "%i is out of bounds, the current target has only %i registers (0 - %i)", num
, count
, count
- 1);
1694 } else /* access a single register by its name */
1696 reg
= register_get_by_name(target
->reg_cache
, args
[0], 1);
1700 command_print(cmd_ctx
, "register %s not found in current target", args
[0]);
1705 /* display a register */
1706 if ((argc
== 1) || ((argc
== 2) && !((args
[1][0] >= '0') && (args
[1][0] <= '9'))))
1708 if ((argc
== 2) && (strcmp(args
[1], "force") == 0))
1711 if (reg
->valid
== 0)
1713 reg_arch_type_t
*arch_type
= register_get_arch_type(reg
->arch_type
);
1714 arch_type
->get(reg
);
1716 value
= buf_to_str(reg
->value
, reg
->size
, 16);
1717 command_print(cmd_ctx
, "%s (/%i): 0x%s", reg
->name
, reg
->size
, value
);
1722 /* set register value */
1725 u8
*buf
= malloc(CEIL(reg
->size
, 8));
1726 str_to_buf(args
[1], strlen(args
[1]), buf
, reg
->size
, 0);
1728 reg_arch_type_t
*arch_type
= register_get_arch_type(reg
->arch_type
);
1729 arch_type
->set(reg
, buf
);
1731 value
= buf_to_str(reg
->value
, reg
->size
, 16);
1732 command_print(cmd_ctx
, "%s (/%i): 0x%s", reg
->name
, reg
->size
, value
);
1740 command_print(cmd_ctx
, "usage: reg <#|name> [value]");
1745 static int handle_poll_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
1747 int retval
= ERROR_OK
;
1748 target_t
*target
= get_current_target(cmd_ctx
);
1752 if((retval
= target_poll(target
)) != ERROR_OK
)
1754 if((retval
= target_arch_state(target
)) != ERROR_OK
)
1760 if (strcmp(args
[0], "on") == 0)
1762 target_continous_poll
= 1;
1764 else if (strcmp(args
[0], "off") == 0)
1766 target_continous_poll
= 0;
1770 command_print(cmd_ctx
, "arg is \"on\" or \"off\"");
1774 return ERROR_COMMAND_SYNTAX_ERROR
;
1780 static int handle_wait_halt_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
1788 ms
= strtoul(args
[0], &end
, 0) * 1000;
1791 command_print(cmd_ctx
, "usage: %s [seconds]", cmd
);
1795 target_t
*target
= get_current_target(cmd_ctx
);
1797 return target_wait_state(target
, TARGET_HALTED
, ms
);
1800 /* wait for target state to change. The trick here is to have a low
1801 * latency for short waits and not to suck up all the CPU time
1804 * After 500ms, keep_alive() is invoked
1806 int target_wait_state(target_t
*target
, enum target_state state
, int ms
)
1809 long long then
=0, cur
;
1814 if ((retval
=target_poll(target
))!=ERROR_OK
)
1816 if (target
->state
== state
)
1824 then
= timeval_ms();
1825 LOG_DEBUG("waiting for target %s...",
1826 Jim_Nvp_value2name_simple(nvp_target_state
,state
)->name
);
1836 LOG_ERROR("timed out while waiting for target %s",
1837 Jim_Nvp_value2name_simple(nvp_target_state
,state
)->name
);
1845 static int handle_halt_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
1848 target_t
*target
= get_current_target(cmd_ctx
);
1852 if ((retval
= target_halt(target
)) != ERROR_OK
)
1862 wait
= strtoul(args
[0], &end
, 0);
1867 return handle_wait_halt_command(cmd_ctx
, cmd
, args
, argc
);
1870 static int handle_soft_reset_halt_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
1872 target_t
*target
= get_current_target(cmd_ctx
);
1874 LOG_USER("requesting target halt and executing a soft reset");
1876 target
->type
->soft_reset_halt(target
);
1881 static int handle_reset_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
1884 enum target_reset_mode reset_mode
= RESET_RUN
;
1888 n
= Jim_Nvp_name2value_simple( nvp_reset_modes
, args
[0] );
1889 if( (n
->name
== NULL
) || (n
->value
== RESET_UNKNOWN
) ){
1890 return ERROR_COMMAND_SYNTAX_ERROR
;
1892 reset_mode
= n
->value
;
1895 /* reset *all* targets */
1896 return target_process_reset(cmd_ctx
, reset_mode
);
1900 static int handle_resume_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
1903 target_t
*target
= get_current_target(cmd_ctx
);
1905 target_handle_event( target
, TARGET_EVENT_OLD_pre_resume
);
1908 retval
= target_resume(target
, 1, 0, 1, 0); /* current pc, addr = 0, handle breakpoints, not debugging */
1910 retval
= target_resume(target
, 0, strtoul(args
[0], NULL
, 0), 1, 0); /* addr = args[0], handle breakpoints, not debugging */
1913 retval
= ERROR_COMMAND_SYNTAX_ERROR
;
1919 static int handle_step_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
1921 target_t
*target
= get_current_target(cmd_ctx
);
1926 return target
->type
->step(target
, 1, 0, 1); /* current pc, addr = 0, handle breakpoints */
1929 return target
->type
->step(target
, 0, strtoul(args
[0], NULL
, 0), 1); /* addr = args[0], handle breakpoints */
1934 static void handle_md_output(struct command_context_s
*cmd_ctx
,
1935 struct target_s
*target
, u32 address
, unsigned size
,
1936 unsigned count
, const u8
*buffer
)
1938 const unsigned line_bytecnt
= 32;
1939 unsigned line_modulo
= line_bytecnt
/ size
;
1941 char output
[line_bytecnt
* 4 + 1];
1942 unsigned output_len
= 0;
1944 const char *value_fmt
;
1946 case 4: value_fmt
= "%8.8x"; break;
1947 case 2: value_fmt
= "%4.2x"; break;
1948 case 1: value_fmt
= "%2.2x"; break;
1950 LOG_ERROR("invalid memory read size: %u", size
);
1954 for (unsigned i
= 0; i
< count
; i
++)
1956 if (i
% line_modulo
== 0)
1958 output_len
+= snprintf(output
+ output_len
,
1959 sizeof(output
) - output_len
,
1960 "0x%8.8x: ", address
+ (i
*size
));
1964 const u8
*value_ptr
= buffer
+ i
* size
;
1966 case 4: value
= target_buffer_get_u32(target
, value_ptr
); break;
1967 case 2: value
= target_buffer_get_u16(target
, value_ptr
); break;
1968 case 1: value
= *value_ptr
;
1970 output_len
+= snprintf(output
+ output_len
,
1971 sizeof(output
) - output_len
,
1974 if ((i
% line_modulo
== line_modulo
- 1) || (i
== count
- 1))
1976 command_print(cmd_ctx
, "%s", output
);
1982 static int handle_md_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
1985 return ERROR_COMMAND_SYNTAX_ERROR
;
1989 case 'w': size
= 4; break;
1990 case 'h': size
= 2; break;
1991 case 'b': size
= 1; break;
1992 default: return ERROR_COMMAND_SYNTAX_ERROR
;
1995 u32 address
= strtoul(args
[0], NULL
, 0);
1999 count
= strtoul(args
[1], NULL
, 0);
2001 u8
*buffer
= calloc(count
, size
);
2003 target_t
*target
= get_current_target(cmd_ctx
);
2004 int retval
= target_read_memory(target
,
2005 address
, size
, count
, buffer
);
2006 if (ERROR_OK
== retval
)
2007 handle_md_output(cmd_ctx
, target
, address
, size
, count
, buffer
);
2014 static int handle_mw_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
2021 target_t
*target
= get_current_target(cmd_ctx
);
2024 if ((argc
< 2) || (argc
> 3))
2025 return ERROR_COMMAND_SYNTAX_ERROR
;
2027 address
= strtoul(args
[0], NULL
, 0);
2028 value
= strtoul(args
[1], NULL
, 0);
2030 count
= strtoul(args
[2], NULL
, 0);
2036 target_buffer_set_u32(target
, value_buf
, value
);
2040 target_buffer_set_u16(target
, value_buf
, value
);
2044 value_buf
[0] = value
;
2047 return ERROR_COMMAND_SYNTAX_ERROR
;
2049 for (i
=0; i
<count
; i
++)
2051 int retval
= target_write_memory(target
,
2052 address
+ i
* wordsize
, wordsize
, 1, value_buf
);
2053 if (ERROR_OK
!= retval
)
2062 static int handle_load_image_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
2068 u32 max_address
=0xffffffff;
2070 int retval
, retvaltemp
;
2074 duration_t duration
;
2075 char *duration_text
;
2077 target_t
*target
= get_current_target(cmd_ctx
);
2079 if ((argc
< 1)||(argc
> 5))
2081 return ERROR_COMMAND_SYNTAX_ERROR
;
2084 /* a base address isn't always necessary, default to 0x0 (i.e. don't relocate) */
2087 image
.base_address_set
= 1;
2088 image
.base_address
= strtoul(args
[1], NULL
, 0);
2092 image
.base_address_set
= 0;
2096 image
.start_address_set
= 0;
2100 min_address
=strtoul(args
[3], NULL
, 0);
2104 max_address
=strtoul(args
[4], NULL
, 0)+min_address
;
2107 if (min_address
>max_address
)
2109 return ERROR_COMMAND_SYNTAX_ERROR
;
2112 duration_start_measure(&duration
);
2114 if (image_open(&image
, args
[0], (argc
>= 3) ? args
[2] : NULL
) != ERROR_OK
)
2121 for (i
= 0; i
< image
.num_sections
; i
++)
2123 buffer
= malloc(image
.sections
[i
].size
);
2126 command_print(cmd_ctx
, "error allocating buffer for section (%d bytes)", image
.sections
[i
].size
);
2130 if ((retval
= image_read_section(&image
, i
, 0x0, image
.sections
[i
].size
, buffer
, &buf_cnt
)) != ERROR_OK
)
2139 /* DANGER!!! beware of unsigned comparision here!!! */
2141 if ((image
.sections
[i
].base_address
+buf_cnt
>=min_address
)&&
2142 (image
.sections
[i
].base_address
<max_address
))
2144 if (image
.sections
[i
].base_address
<min_address
)
2146 /* clip addresses below */
2147 offset
+=min_address
-image
.sections
[i
].base_address
;
2151 if (image
.sections
[i
].base_address
+buf_cnt
>max_address
)
2153 length
-=(image
.sections
[i
].base_address
+buf_cnt
)-max_address
;
2156 if ((retval
= target_write_buffer(target
, image
.sections
[i
].base_address
+offset
, length
, buffer
+offset
)) != ERROR_OK
)
2161 image_size
+= length
;
2162 command_print(cmd_ctx
, "%u byte written at address 0x%8.8x", length
, image
.sections
[i
].base_address
+offset
);
2168 if((retvaltemp
= duration_stop_measure(&duration
, &duration_text
)) != ERROR_OK
)
2170 image_close(&image
);
2174 if (retval
==ERROR_OK
)
2176 command_print(cmd_ctx
, "downloaded %u byte in %s", image_size
, duration_text
);
2178 free(duration_text
);
2180 image_close(&image
);
2186 static int handle_dump_image_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
2193 int retval
=ERROR_OK
, retvaltemp
;
2195 duration_t duration
;
2196 char *duration_text
;
2198 target_t
*target
= get_current_target(cmd_ctx
);
2202 command_print(cmd_ctx
, "usage: dump_image <filename> <address> <size>");
2206 address
= strtoul(args
[1], NULL
, 0);
2207 size
= strtoul(args
[2], NULL
, 0);
2209 if (fileio_open(&fileio
, args
[0], FILEIO_WRITE
, FILEIO_BINARY
) != ERROR_OK
)
2214 duration_start_measure(&duration
);
2219 u32 this_run_size
= (size
> 560) ? 560 : size
;
2221 retval
= target_read_buffer(target
, address
, this_run_size
, buffer
);
2222 if (retval
!= ERROR_OK
)
2227 retval
= fileio_write(&fileio
, this_run_size
, buffer
, &size_written
);
2228 if (retval
!= ERROR_OK
)
2233 size
-= this_run_size
;
2234 address
+= this_run_size
;
2237 if((retvaltemp
= fileio_close(&fileio
)) != ERROR_OK
)
2240 if((retvaltemp
= duration_stop_measure(&duration
, &duration_text
)) != ERROR_OK
)
2243 if (retval
==ERROR_OK
)
2245 command_print(cmd_ctx
, "dumped %lld byte in %s",
2246 fileio
.size
, duration_text
);
2247 free(duration_text
);
2253 static int handle_verify_image_command_internal(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
, int verify
)
2259 int retval
, retvaltemp
;
2261 u32 mem_checksum
= 0;
2265 duration_t duration
;
2266 char *duration_text
;
2268 target_t
*target
= get_current_target(cmd_ctx
);
2272 return ERROR_COMMAND_SYNTAX_ERROR
;
2277 LOG_ERROR("no target selected");
2281 duration_start_measure(&duration
);
2285 image
.base_address_set
= 1;
2286 image
.base_address
= strtoul(args
[1], NULL
, 0);
2290 image
.base_address_set
= 0;
2291 image
.base_address
= 0x0;
2294 image
.start_address_set
= 0;
2296 if ((retval
=image_open(&image
, args
[0], (argc
== 3) ? args
[2] : NULL
)) != ERROR_OK
)
2303 for (i
= 0; i
< image
.num_sections
; i
++)
2305 buffer
= malloc(image
.sections
[i
].size
);
2308 command_print(cmd_ctx
, "error allocating buffer for section (%d bytes)", image
.sections
[i
].size
);
2311 if ((retval
= image_read_section(&image
, i
, 0x0, image
.sections
[i
].size
, buffer
, &buf_cnt
)) != ERROR_OK
)
2319 /* calculate checksum of image */
2320 image_calculate_checksum( buffer
, buf_cnt
, &checksum
);
2322 retval
= target_checksum_memory(target
, image
.sections
[i
].base_address
, buf_cnt
, &mem_checksum
);
2323 if( retval
!= ERROR_OK
)
2329 if( checksum
!= mem_checksum
)
2331 /* failed crc checksum, fall back to a binary compare */
2334 command_print(cmd_ctx
, "checksum mismatch - attempting binary compare");
2336 data
= (u8
*)malloc(buf_cnt
);
2338 /* Can we use 32bit word accesses? */
2340 int count
= buf_cnt
;
2341 if ((count
% 4) == 0)
2346 retval
= target_read_memory(target
, image
.sections
[i
].base_address
, size
, count
, data
);
2347 if (retval
== ERROR_OK
)
2350 for (t
= 0; t
< buf_cnt
; t
++)
2352 if (data
[t
] != buffer
[t
])
2354 command_print(cmd_ctx
, "Verify operation failed address 0x%08x. Was 0x%02x instead of 0x%02x\n", t
+ image
.sections
[i
].base_address
, data
[t
], buffer
[t
]);
2371 command_print(cmd_ctx
, "address 0x%08x length 0x%08x", image
.sections
[i
].base_address
, buf_cnt
);
2375 image_size
+= buf_cnt
;
2379 if((retvaltemp
= duration_stop_measure(&duration
, &duration_text
)) != ERROR_OK
)
2381 image_close(&image
);
2385 if (retval
==ERROR_OK
)
2387 command_print(cmd_ctx
, "verified %u bytes in %s", image_size
, duration_text
);
2389 free(duration_text
);
2391 image_close(&image
);
2396 static int handle_verify_image_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
2398 return handle_verify_image_command_internal(cmd_ctx
, cmd
, args
, argc
, 1);
2401 static int handle_test_image_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
2403 return handle_verify_image_command_internal(cmd_ctx
, cmd
, args
, argc
, 0);
2406 static int handle_bp_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
2409 target_t
*target
= get_current_target(cmd_ctx
);
2413 breakpoint_t
*breakpoint
= target
->breakpoints
;
2417 if (breakpoint
->type
== BKPT_SOFT
)
2419 char* buf
= buf_to_str(breakpoint
->orig_instr
, breakpoint
->length
, 16);
2420 command_print(cmd_ctx
, "0x%8.8x, 0x%x, %i, 0x%s", breakpoint
->address
, breakpoint
->length
, breakpoint
->set
, buf
);
2425 command_print(cmd_ctx
, "0x%8.8x, 0x%x, %i", breakpoint
->address
, breakpoint
->length
, breakpoint
->set
);
2427 breakpoint
= breakpoint
->next
;
2435 length
= strtoul(args
[1], NULL
, 0);
2438 if (strcmp(args
[2], "hw") == 0)
2441 if ((retval
= breakpoint_add(target
, strtoul(args
[0], NULL
, 0), length
, hw
)) != ERROR_OK
)
2443 LOG_ERROR("Failure setting breakpoints");
2447 command_print(cmd_ctx
, "breakpoint added at address 0x%8.8lx",
2448 strtoul(args
[0], NULL
, 0));
2453 command_print(cmd_ctx
, "usage: bp <address> <length> ['hw']");
2459 static int handle_rbp_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
2461 target_t
*target
= get_current_target(cmd_ctx
);
2464 breakpoint_remove(target
, strtoul(args
[0], NULL
, 0));
2469 static int handle_wp_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
2471 target_t
*target
= get_current_target(cmd_ctx
);
2476 watchpoint_t
*watchpoint
= target
->watchpoints
;
2480 command_print(cmd_ctx
, "address: 0x%8.8x, len: 0x%8.8x, r/w/a: %i, value: 0x%8.8x, mask: 0x%8.8x", watchpoint
->address
, watchpoint
->length
, watchpoint
->rw
, watchpoint
->value
, watchpoint
->mask
);
2481 watchpoint
= watchpoint
->next
;
2486 enum watchpoint_rw type
= WPT_ACCESS
;
2487 u32 data_value
= 0x0;
2488 u32 data_mask
= 0xffffffff;
2504 command_print(cmd_ctx
, "usage: wp <address> <length> [r/w/a] [value] [mask]");
2510 data_value
= strtoul(args
[3], NULL
, 0);
2514 data_mask
= strtoul(args
[4], NULL
, 0);
2517 if ((retval
= watchpoint_add(target
, strtoul(args
[0], NULL
, 0),
2518 strtoul(args
[1], NULL
, 0), type
, data_value
, data_mask
)) != ERROR_OK
)
2520 LOG_ERROR("Failure setting breakpoints");
2525 command_print(cmd_ctx
, "usage: wp <address> <length> [r/w/a] [value] [mask]");
2531 static int handle_rwp_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
2533 target_t
*target
= get_current_target(cmd_ctx
);
2536 watchpoint_remove(target
, strtoul(args
[0], NULL
, 0));
2541 static int handle_virt2phys_command(command_context_t
*cmd_ctx
, char *cmd
, char **args
, int argc
)
2544 target_t
*target
= get_current_target(cmd_ctx
);
2550 return ERROR_COMMAND_SYNTAX_ERROR
;
2552 va
= strtoul(args
[0], NULL
, 0);
2554 retval
= target
->type
->virt2phys(target
, va
, &pa
);
2555 if (retval
== ERROR_OK
)
2557 command_print(cmd_ctx
, "Physical address 0x%08x", pa
);
2561 /* lower levels will have logged a detailed error which is
2562 * forwarded to telnet/GDB session.
2568 static void writeData(FILE *f
, const void *data
, size_t len
)
2570 size_t written
= fwrite(data
, len
, 1, f
);
2572 LOG_ERROR("failed to write %zu bytes: %s", len
, strerror(errno
));
2575 static void writeLong(FILE *f
, int l
)
2580 char c
=(l
>>(i
*8))&0xff;
2581 writeData(f
, &c
, 1);
2586 static void writeString(FILE *f
, char *s
)
2588 writeData(f
, s
, strlen(s
));
2591 /* Dump a gmon.out histogram file. */
2592 static void writeGmon(u32
*samples
, u32 sampleNum
, char *filename
)
2595 FILE *f
=fopen(filename
, "w");
2598 writeString(f
, "gmon");
2599 writeLong(f
, 0x00000001); /* Version */
2600 writeLong(f
, 0); /* padding */
2601 writeLong(f
, 0); /* padding */
2602 writeLong(f
, 0); /* padding */
2604 u8 zero
= 0; /* GMON_TAG_TIME_HIST */
2605 writeData(f
, &zero
, 1);
2607 /* figure out bucket size */
2610 for (i
=0; i
<sampleNum
; i
++)
2622 int addressSpace
=(max
-min
+1);
2624 static const u32 maxBuckets
= 256 * 1024; /* maximum buckets. */
2625 u32 length
= addressSpace
;
2626 if (length
> maxBuckets
)
2630 int *buckets
=malloc(sizeof(int)*length
);
2636 memset(buckets
, 0, sizeof(int)*length
);
2637 for (i
=0; i
<sampleNum
;i
++)
2639 u32 address
=samples
[i
];
2640 long long a
=address
-min
;
2641 long long b
=length
-1;
2642 long long c
=addressSpace
-1;
2643 int index
=(a
*b
)/c
; /* danger!!!! int32 overflows */
2647 /* append binary memory gmon.out &profile_hist_hdr ((char*)&profile_hist_hdr + sizeof(struct gmon_hist_hdr)) */
2648 writeLong(f
, min
); /* low_pc */
2649 writeLong(f
, max
); /* high_pc */
2650 writeLong(f
, length
); /* # of samples */
2651 writeLong(f
, 64000000); /* 64MHz */
2652 writeString(f
, "seconds");
2653 for (i
=0; i
<(15-strlen("seconds")); i
++)
2654 writeData(f
, &zero
, 1);
2655 writeString(f
, "s");
2657 /*append binary memory gmon.out profile_hist_data (profile_hist_data + profile_hist_hdr.hist_size) */
2659 char *data
=malloc(2*length
);
2662 for (i
=0; i
<length
;i
++)
2671 data
[i
*2+1]=(val
>>8)&0xff;
2674 writeData(f
, data
, length
* 2);
2684 /* profiling samples the CPU PC as quickly as OpenOCD is able, which will be used as a random sampling of PC */
2685 static int handle_profile_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
2687 target_t
*target
= get_current_target(cmd_ctx
);
2688 struct timeval timeout
, now
;
2690 gettimeofday(&timeout
, NULL
);
2693 return ERROR_COMMAND_SYNTAX_ERROR
;
2696 timeval_add_time(&timeout
, strtoul(args
[0], &end
, 0), 0);
2702 command_print(cmd_ctx
, "Starting profiling. Halting and resuming the target as often as we can...");
2704 static const int maxSample
=10000;
2705 u32
*samples
=malloc(sizeof(u32
)*maxSample
);
2710 int retval
=ERROR_OK
;
2711 /* hopefully it is safe to cache! We want to stop/restart as quickly as possible. */
2712 reg_t
*reg
= register_get_by_name(target
->reg_cache
, "pc", 1);
2716 target_poll(target
);
2717 if (target
->state
== TARGET_HALTED
)
2719 u32 t
=*((u32
*)reg
->value
);
2720 samples
[numSamples
++]=t
;
2721 retval
= target_resume(target
, 1, 0, 0, 0); /* current pc, addr = 0, do not handle breakpoints, not debugging */
2722 target_poll(target
);
2723 alive_sleep(10); /* sleep 10ms, i.e. <100 samples/second. */
2724 } else if (target
->state
== TARGET_RUNNING
)
2726 /* We want to quickly sample the PC. */
2727 if((retval
= target_halt(target
)) != ERROR_OK
)
2734 command_print(cmd_ctx
, "Target not halted or running");
2738 if (retval
!=ERROR_OK
)
2743 gettimeofday(&now
, NULL
);
2744 if ((numSamples
>=maxSample
) || ((now
.tv_sec
>= timeout
.tv_sec
) && (now
.tv_usec
>= timeout
.tv_usec
)))
2746 command_print(cmd_ctx
, "Profiling completed. %d samples.", numSamples
);
2747 if((retval
= target_poll(target
)) != ERROR_OK
)
2752 if (target
->state
== TARGET_HALTED
)
2754 target_resume(target
, 1, 0, 0, 0); /* current pc, addr = 0, do not handle breakpoints, not debugging */
2756 if((retval
= target_poll(target
)) != ERROR_OK
)
2761 writeGmon(samples
, numSamples
, args
[1]);
2762 command_print(cmd_ctx
, "Wrote %s", args
[1]);
2771 static int new_int_array_element(Jim_Interp
* interp
, const char *varname
, int idx
, u32 val
)
2774 Jim_Obj
*nameObjPtr
, *valObjPtr
;
2777 namebuf
= alloc_printf("%s(%d)", varname
, idx
);
2781 nameObjPtr
= Jim_NewStringObj(interp
, namebuf
, -1);
2782 valObjPtr
= Jim_NewIntObj(interp
, val
);
2783 if (!nameObjPtr
|| !valObjPtr
)
2789 Jim_IncrRefCount(nameObjPtr
);
2790 Jim_IncrRefCount(valObjPtr
);
2791 result
= Jim_SetVariable(interp
, nameObjPtr
, valObjPtr
);
2792 Jim_DecrRefCount(interp
, nameObjPtr
);
2793 Jim_DecrRefCount(interp
, valObjPtr
);
2795 /* printf("%s(%d) <= 0%08x\n", varname, idx, val); */
2799 static int jim_mem2array(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
2801 command_context_t
*context
;
2804 context
= Jim_GetAssocData(interp
, "context");
2805 if (context
== NULL
)
2807 LOG_ERROR("mem2array: no command context");
2810 target
= get_current_target(context
);
2813 LOG_ERROR("mem2array: no current target");
2817 return target_mem2array(interp
, target
, argc
-1, argv
+1);
2820 static int target_mem2array(Jim_Interp
*interp
, target_t
*target
, int argc
, Jim_Obj
*const *argv
)
2828 const char *varname
;
2833 /* argv[1] = name of array to receive the data
2834 * argv[2] = desired width
2835 * argv[3] = memory address
2836 * argv[4] = count of times to read
2839 Jim_WrongNumArgs(interp
, 1, argv
, "varname width addr nelems");
2842 varname
= Jim_GetString(argv
[0], &len
);
2843 /* given "foo" get space for worse case "foo(%d)" .. add 20 */
2845 e
= Jim_GetLong(interp
, argv
[1], &l
);
2851 e
= Jim_GetLong(interp
, argv
[2], &l
);
2856 e
= Jim_GetLong(interp
, argv
[3], &l
);
2872 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
2873 Jim_AppendStrings( interp
, Jim_GetResult(interp
), "Invalid width param, must be 8/16/32", NULL
);
2877 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
2878 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "mem2array: zero width read?", NULL
);
2881 if ((addr
+ (len
* width
)) < addr
) {
2882 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
2883 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "mem2array: addr + len - wraps to zero?", NULL
);
2886 /* absurd transfer size? */
2888 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
2889 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "mem2array: absurd > 64K item request", NULL
);
2894 ((width
== 2) && ((addr
& 1) == 0)) ||
2895 ((width
== 4) && ((addr
& 3) == 0))) {
2899 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
2900 sprintf(buf
, "mem2array address: 0x%08x is not aligned for %d byte reads", addr
, width
);
2901 Jim_AppendStrings(interp
, Jim_GetResult(interp
), buf
, NULL
);
2912 /* Slurp... in buffer size chunks */
2914 count
= len
; /* in objects.. */
2915 if (count
> (sizeof(buffer
)/width
)) {
2916 count
= (sizeof(buffer
)/width
);
2919 retval
= target_read_memory( target
, addr
, width
, count
, buffer
);
2920 if (retval
!= ERROR_OK
) {
2922 LOG_ERROR("mem2array: Read @ 0x%08x, w=%d, cnt=%d, failed", addr
, width
, count
);
2923 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
2924 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "mem2array: cannot read memory", NULL
);
2928 v
= 0; /* shut up gcc */
2929 for (i
= 0 ;i
< count
;i
++, n
++) {
2932 v
= target_buffer_get_u32(target
, &buffer
[i
*width
]);
2935 v
= target_buffer_get_u16(target
, &buffer
[i
*width
]);
2938 v
= buffer
[i
] & 0x0ff;
2941 new_int_array_element(interp
, varname
, n
, v
);
2947 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
2952 static int get_int_array_element(Jim_Interp
* interp
, const char *varname
, int idx
, u32
*val
)
2955 Jim_Obj
*nameObjPtr
, *valObjPtr
;
2959 namebuf
= alloc_printf("%s(%d)", varname
, idx
);
2963 nameObjPtr
= Jim_NewStringObj(interp
, namebuf
, -1);
2970 Jim_IncrRefCount(nameObjPtr
);
2971 valObjPtr
= Jim_GetVariable(interp
, nameObjPtr
, JIM_ERRMSG
);
2972 Jim_DecrRefCount(interp
, nameObjPtr
);
2974 if (valObjPtr
== NULL
)
2977 result
= Jim_GetLong(interp
, valObjPtr
, &l
);
2978 /* printf("%s(%d) => 0%08x\n", varname, idx, val); */
2983 static int jim_array2mem(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
2985 command_context_t
*context
;
2988 context
= Jim_GetAssocData(interp
, "context");
2989 if (context
== NULL
){
2990 LOG_ERROR("array2mem: no command context");
2993 target
= get_current_target(context
);
2994 if (target
== NULL
){
2995 LOG_ERROR("array2mem: no current target");
2999 return target_array2mem( interp
,target
, argc
-1, argv
+1 );
3002 static int target_array2mem(Jim_Interp
*interp
, target_t
*target
, int argc
, Jim_Obj
*const *argv
)
3010 const char *varname
;
3015 /* argv[1] = name of array to get the data
3016 * argv[2] = desired width
3017 * argv[3] = memory address
3018 * argv[4] = count to write
3021 Jim_WrongNumArgs(interp
, 1, argv
, "varname width addr nelems");
3024 varname
= Jim_GetString(argv
[0], &len
);
3025 /* given "foo" get space for worse case "foo(%d)" .. add 20 */
3027 e
= Jim_GetLong(interp
, argv
[1], &l
);
3033 e
= Jim_GetLong(interp
, argv
[2], &l
);
3038 e
= Jim_GetLong(interp
, argv
[3], &l
);
3054 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3055 Jim_AppendStrings( interp
, Jim_GetResult(interp
), "Invalid width param, must be 8/16/32", NULL
);
3059 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3060 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "array2mem: zero width read?", NULL
);
3063 if ((addr
+ (len
* width
)) < addr
) {
3064 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3065 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "array2mem: addr + len - wraps to zero?", NULL
);
3068 /* absurd transfer size? */
3070 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3071 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "array2mem: absurd > 64K item request", NULL
);
3076 ((width
== 2) && ((addr
& 1) == 0)) ||
3077 ((width
== 4) && ((addr
& 3) == 0))) {
3081 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3082 sprintf(buf
, "array2mem address: 0x%08x is not aligned for %d byte reads", addr
, width
);
3083 Jim_AppendStrings(interp
, Jim_GetResult(interp
), buf
, NULL
);
3094 /* Slurp... in buffer size chunks */
3096 count
= len
; /* in objects.. */
3097 if (count
> (sizeof(buffer
)/width
)) {
3098 count
= (sizeof(buffer
)/width
);
3101 v
= 0; /* shut up gcc */
3102 for (i
= 0 ;i
< count
;i
++, n
++) {
3103 get_int_array_element(interp
, varname
, n
, &v
);
3106 target_buffer_set_u32(target
, &buffer
[i
*width
], v
);
3109 target_buffer_set_u16(target
, &buffer
[i
*width
], v
);
3112 buffer
[i
] = v
& 0x0ff;
3118 retval
= target_write_memory(target
, addr
, width
, count
, buffer
);
3119 if (retval
!= ERROR_OK
) {
3121 LOG_ERROR("array2mem: Write @ 0x%08x, w=%d, cnt=%d, failed", addr
, width
, count
);
3122 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3123 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "array2mem: cannot read memory", NULL
);
3129 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3134 void target_all_handle_event( enum target_event e
)
3138 LOG_DEBUG( "**all*targets: event: %d, %s",
3140 Jim_Nvp_value2name_simple( nvp_target_event
, e
)->name
);
3142 target
= all_targets
;
3144 target_handle_event( target
, e
);
3145 target
= target
->next
;
3149 void target_handle_event( target_t
*target
, enum target_event e
)
3151 target_event_action_t
*teap
;
3154 teap
= target
->event_action
;
3158 if( teap
->event
== e
){
3160 LOG_DEBUG( "target: (%d) %s (%s) event: %d (%s) action: %s\n",
3161 target
->target_number
,
3163 target_get_name(target
),
3165 Jim_Nvp_value2name_simple( nvp_target_event
, e
)->name
,
3166 Jim_GetString( teap
->body
, NULL
) );
3167 if (Jim_EvalObj( interp
, teap
->body
)!=JIM_OK
)
3169 Jim_PrintErrorMessage(interp
);
3175 LOG_DEBUG( "event: %d %s - no action",
3177 Jim_Nvp_value2name_simple( nvp_target_event
, e
)->name
);
3181 enum target_cfg_param
{
3184 TCFG_WORK_AREA_VIRT
,
3185 TCFG_WORK_AREA_PHYS
,
3186 TCFG_WORK_AREA_SIZE
,
3187 TCFG_WORK_AREA_BACKUP
,
3190 TCFG_CHAIN_POSITION
,
3193 static Jim_Nvp nvp_config_opts
[] = {
3194 { .name
= "-type", .value
= TCFG_TYPE
},
3195 { .name
= "-event", .value
= TCFG_EVENT
},
3196 { .name
= "-work-area-virt", .value
= TCFG_WORK_AREA_VIRT
},
3197 { .name
= "-work-area-phys", .value
= TCFG_WORK_AREA_PHYS
},
3198 { .name
= "-work-area-size", .value
= TCFG_WORK_AREA_SIZE
},
3199 { .name
= "-work-area-backup", .value
= TCFG_WORK_AREA_BACKUP
},
3200 { .name
= "-endian" , .value
= TCFG_ENDIAN
},
3201 { .name
= "-variant", .value
= TCFG_VARIANT
},
3202 { .name
= "-chain-position", .value
= TCFG_CHAIN_POSITION
},
3204 { .name
= NULL
, .value
= -1 }
3207 static int target_configure( Jim_GetOptInfo
*goi
, target_t
*target
)
3215 /* parse config or cget options ... */
3216 while( goi
->argc
> 0 ){
3217 Jim_SetEmptyResult( goi
->interp
);
3218 /* Jim_GetOpt_Debug( goi ); */
3220 if( target
->type
->target_jim_configure
){
3221 /* target defines a configure function */
3222 /* target gets first dibs on parameters */
3223 e
= (*(target
->type
->target_jim_configure
))( target
, goi
);
3232 /* otherwise we 'continue' below */
3234 e
= Jim_GetOpt_Nvp( goi
, nvp_config_opts
, &n
);
3236 Jim_GetOpt_NvpUnknown( goi
, nvp_config_opts
, 0 );
3242 if( goi
->isconfigure
){
3243 Jim_SetResult_sprintf( goi
->interp
, "not setable: %s", n
->name
);
3247 if( goi
->argc
!= 0 ){
3248 Jim_WrongNumArgs( goi
->interp
, goi
->argc
, goi
->argv
, "NO PARAMS");
3252 Jim_SetResultString( goi
->interp
, target_get_name(target
), -1 );
3256 if( goi
->argc
== 0 ){
3257 Jim_WrongNumArgs( goi
->interp
, goi
->argc
, goi
->argv
, "-event ?event-name? ...");
3261 e
= Jim_GetOpt_Nvp( goi
, nvp_target_event
, &n
);
3263 Jim_GetOpt_NvpUnknown( goi
, nvp_target_event
, 1 );
3267 if( goi
->isconfigure
){
3268 if( goi
->argc
!= 1 ){
3269 Jim_WrongNumArgs( goi
->interp
, goi
->argc
, goi
->argv
, "-event ?event-name? ?EVENT-BODY?");
3273 if( goi
->argc
!= 0 ){
3274 Jim_WrongNumArgs(goi
->interp
, goi
->argc
, goi
->argv
, "-event ?event-name?");
3280 target_event_action_t
*teap
;
3282 teap
= target
->event_action
;
3283 /* replace existing? */
3285 if( teap
->event
== (enum target_event
)n
->value
){
3291 if( goi
->isconfigure
){
3294 teap
= calloc( 1, sizeof(*teap
) );
3296 teap
->event
= n
->value
;
3297 Jim_GetOpt_Obj( goi
, &o
);
3299 Jim_DecrRefCount( interp
, teap
->body
);
3301 teap
->body
= Jim_DuplicateObj( goi
->interp
, o
);
3304 * Tcl/TK - "tk events" have a nice feature.
3305 * See the "BIND" command.
3306 * We should support that here.
3307 * You can specify %X and %Y in the event code.
3308 * The idea is: %T - target name.
3309 * The idea is: %N - target number
3310 * The idea is: %E - event name.
3312 Jim_IncrRefCount( teap
->body
);
3314 /* add to head of event list */
3315 teap
->next
= target
->event_action
;
3316 target
->event_action
= teap
;
3317 Jim_SetEmptyResult(goi
->interp
);
3321 Jim_SetEmptyResult( goi
->interp
);
3323 Jim_SetResult( goi
->interp
, Jim_DuplicateObj( goi
->interp
, teap
->body
) );
3330 case TCFG_WORK_AREA_VIRT
:
3331 if( goi
->isconfigure
){
3332 target_free_all_working_areas(target
);
3333 e
= Jim_GetOpt_Wide( goi
, &w
);
3337 target
->working_area_virt
= w
;
3339 if( goi
->argc
!= 0 ){
3343 Jim_SetResult( interp
, Jim_NewIntObj( goi
->interp
, target
->working_area_virt
) );
3347 case TCFG_WORK_AREA_PHYS
:
3348 if( goi
->isconfigure
){
3349 target_free_all_working_areas(target
);
3350 e
= Jim_GetOpt_Wide( goi
, &w
);
3354 target
->working_area_phys
= w
;
3356 if( goi
->argc
!= 0 ){
3360 Jim_SetResult( interp
, Jim_NewIntObj( goi
->interp
, target
->working_area_phys
) );
3364 case TCFG_WORK_AREA_SIZE
:
3365 if( goi
->isconfigure
){
3366 target_free_all_working_areas(target
);
3367 e
= Jim_GetOpt_Wide( goi
, &w
);
3371 target
->working_area_size
= w
;
3373 if( goi
->argc
!= 0 ){
3377 Jim_SetResult( interp
, Jim_NewIntObj( goi
->interp
, target
->working_area_size
) );
3381 case TCFG_WORK_AREA_BACKUP
:
3382 if( goi
->isconfigure
){
3383 target_free_all_working_areas(target
);
3384 e
= Jim_GetOpt_Wide( goi
, &w
);
3388 /* make this exactly 1 or 0 */
3389 target
->backup_working_area
= (!!w
);
3391 if( goi
->argc
!= 0 ){
3395 Jim_SetResult( interp
, Jim_NewIntObj( goi
->interp
, target
->working_area_size
) );
3396 /* loop for more e*/
3400 if( goi
->isconfigure
){
3401 e
= Jim_GetOpt_Nvp( goi
, nvp_target_endian
, &n
);
3403 Jim_GetOpt_NvpUnknown( goi
, nvp_target_endian
, 1 );
3406 target
->endianness
= n
->value
;
3408 if( goi
->argc
!= 0 ){
3412 n
= Jim_Nvp_value2name_simple( nvp_target_endian
, target
->endianness
);
3413 if( n
->name
== NULL
){
3414 target
->endianness
= TARGET_LITTLE_ENDIAN
;
3415 n
= Jim_Nvp_value2name_simple( nvp_target_endian
, target
->endianness
);
3417 Jim_SetResultString( goi
->interp
, n
->name
, -1 );
3422 if( goi
->isconfigure
){
3423 if( goi
->argc
< 1 ){
3424 Jim_SetResult_sprintf( goi
->interp
,
3429 if( target
->variant
){
3430 free((void *)(target
->variant
));
3432 e
= Jim_GetOpt_String( goi
, &cp
, NULL
);
3433 target
->variant
= strdup(cp
);
3435 if( goi
->argc
!= 0 ){
3439 Jim_SetResultString( goi
->interp
, target
->variant
,-1 );
3442 case TCFG_CHAIN_POSITION
:
3443 if( goi
->isconfigure
){
3446 target_free_all_working_areas(target
);
3447 e
= Jim_GetOpt_Obj( goi
, &o
);
3451 tap
= jtag_tap_by_jim_obj( goi
->interp
, o
);
3455 /* make this exactly 1 or 0 */
3458 if( goi
->argc
!= 0 ){
3462 Jim_SetResultString( interp
, target
->tap
->dotted_name
, -1 );
3463 /* loop for more e*/
3466 } /* while( goi->argc ) */
3469 /* done - we return */
3473 /** this is the 'tcl' handler for the target specific command */
3474 static int tcl_target_func( Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
3482 struct command_context_s
*cmd_ctx
;
3489 TS_CMD_MWW
, TS_CMD_MWH
, TS_CMD_MWB
,
3490 TS_CMD_MDW
, TS_CMD_MDH
, TS_CMD_MDB
,
3491 TS_CMD_MRW
, TS_CMD_MRH
, TS_CMD_MRB
,
3492 TS_CMD_MEM2ARRAY
, TS_CMD_ARRAY2MEM
,
3500 TS_CMD_INVOKE_EVENT
,
3503 static const Jim_Nvp target_options
[] = {
3504 { .name
= "configure", .value
= TS_CMD_CONFIGURE
},
3505 { .name
= "cget", .value
= TS_CMD_CGET
},
3506 { .name
= "mww", .value
= TS_CMD_MWW
},
3507 { .name
= "mwh", .value
= TS_CMD_MWH
},
3508 { .name
= "mwb", .value
= TS_CMD_MWB
},
3509 { .name
= "mdw", .value
= TS_CMD_MDW
},
3510 { .name
= "mdh", .value
= TS_CMD_MDH
},
3511 { .name
= "mdb", .value
= TS_CMD_MDB
},
3512 { .name
= "mem2array", .value
= TS_CMD_MEM2ARRAY
},
3513 { .name
= "array2mem", .value
= TS_CMD_ARRAY2MEM
},
3514 { .name
= "eventlist", .value
= TS_CMD_EVENTLIST
},
3515 { .name
= "curstate", .value
= TS_CMD_CURSTATE
},
3517 { .name
= "arp_examine", .value
= TS_CMD_EXAMINE
},
3518 { .name
= "arp_poll", .value
= TS_CMD_POLL
},
3519 { .name
= "arp_reset", .value
= TS_CMD_RESET
},
3520 { .name
= "arp_halt", .value
= TS_CMD_HALT
},
3521 { .name
= "arp_waitstate", .value
= TS_CMD_WAITSTATE
},
3522 { .name
= "invoke-event", .value
= TS_CMD_INVOKE_EVENT
},
3524 { .name
= NULL
, .value
= -1 },
3527 /* go past the "command" */
3528 Jim_GetOpt_Setup( &goi
, interp
, argc
-1, argv
+1 );
3530 target
= Jim_CmdPrivData( goi
.interp
);
3531 cmd_ctx
= Jim_GetAssocData(goi
.interp
, "context");
3533 /* commands here are in an NVP table */
3534 e
= Jim_GetOpt_Nvp( &goi
, target_options
, &n
);
3536 Jim_GetOpt_NvpUnknown( &goi
, target_options
, 0 );
3539 /* Assume blank result */
3540 Jim_SetEmptyResult( goi
.interp
);
3543 case TS_CMD_CONFIGURE
:
3545 Jim_WrongNumArgs( goi
.interp
, goi
.argc
, goi
.argv
, "missing: -option VALUE ...");
3548 goi
.isconfigure
= 1;
3549 return target_configure( &goi
, target
);
3551 // some things take params
3553 Jim_WrongNumArgs( goi
.interp
, 0, goi
.argv
, "missing: ?-option?");
3556 goi
.isconfigure
= 0;
3557 return target_configure( &goi
, target
);
3565 * argv[3] = optional count.
3568 if( (goi
.argc
== 3) || (goi
.argc
== 4) ){
3572 Jim_SetResult_sprintf( goi
.interp
, "expected: %s ADDR DATA [COUNT]", n
->name
);
3576 e
= Jim_GetOpt_Wide( &goi
, &a
);
3581 e
= Jim_GetOpt_Wide( &goi
, &b
);
3586 e
= Jim_GetOpt_Wide( &goi
, &c
);
3596 target_buffer_set_u32( target
, target_buf
, b
);
3600 target_buffer_set_u16( target
, target_buf
, b
);
3604 target_buffer_set_u8( target
, target_buf
, b
);
3608 for( x
= 0 ; x
< c
; x
++ ){
3609 e
= target_write_memory( target
, a
, b
, 1, target_buf
);
3610 if( e
!= ERROR_OK
){
3611 Jim_SetResult_sprintf( interp
, "Error writing @ 0x%08x: %d\n", (int)(a
), e
);
3624 /* argv[0] = command
3626 * argv[2] = optional count
3628 if( (goi
.argc
== 2) || (goi
.argc
== 3) ){
3629 Jim_SetResult_sprintf( goi
.interp
, "expected: %s ADDR [COUNT]", n
->name
);
3632 e
= Jim_GetOpt_Wide( &goi
, &a
);
3637 e
= Jim_GetOpt_Wide( &goi
, &c
);
3644 b
= 1; /* shut up gcc */
3657 /* convert to "bytes" */
3659 /* count is now in 'BYTES' */
3665 e
= target_read_memory( target
, a
, b
, y
/ b
, target_buf
);
3666 if( e
!= ERROR_OK
){
3667 Jim_SetResult_sprintf( interp
, "error reading target @ 0x%08lx", (int)(a
) );
3671 Jim_fprintf( interp
, interp
->cookie_stdout
, "0x%08x ", (int)(a
) );
3674 for( x
= 0 ; (x
< 16) && (x
< y
) ; x
+= 4 ){
3675 z
= target_buffer_get_u32( target
, &(target_buf
[ x
* 4 ]) );
3676 Jim_fprintf( interp
, interp
->cookie_stdout
, "%08x ", (int)(z
) );
3678 for( ; (x
< 16) ; x
+= 4 ){
3679 Jim_fprintf( interp
, interp
->cookie_stdout
, " " );
3683 for( x
= 0 ; (x
< 16) && (x
< y
) ; x
+= 2 ){
3684 z
= target_buffer_get_u16( target
, &(target_buf
[ x
* 2 ]) );
3685 Jim_fprintf( interp
, interp
->cookie_stdout
, "%04x ", (int)(z
) );
3687 for( ; (x
< 16) ; x
+= 2 ){
3688 Jim_fprintf( interp
, interp
->cookie_stdout
, " " );
3693 for( x
= 0 ; (x
< 16) && (x
< y
) ; x
+= 1 ){
3694 z
= target_buffer_get_u8( target
, &(target_buf
[ x
* 4 ]) );
3695 Jim_fprintf( interp
, interp
->cookie_stdout
, "%02x ", (int)(z
) );
3697 for( ; (x
< 16) ; x
+= 1 ){
3698 Jim_fprintf( interp
, interp
->cookie_stdout
, " " );
3702 /* ascii-ify the bytes */
3703 for( x
= 0 ; x
< y
; x
++ ){
3704 if( (target_buf
[x
] >= 0x20) &&
3705 (target_buf
[x
] <= 0x7e) ){
3709 target_buf
[x
] = '.';
3714 target_buf
[x
] = ' ';
3719 /* print - with a newline */
3720 Jim_fprintf( interp
, interp
->cookie_stdout
, "%s\n", target_buf
);
3726 case TS_CMD_MEM2ARRAY
:
3727 return target_mem2array( goi
.interp
, target
, goi
.argc
, goi
.argv
);
3729 case TS_CMD_ARRAY2MEM
:
3730 return target_array2mem( goi
.interp
, target
, goi
.argc
, goi
.argv
);
3732 case TS_CMD_EXAMINE
:
3734 Jim_WrongNumArgs( goi
.interp
, 2, argv
, "[no parameters]");
3737 e
= target
->type
->examine( target
);
3738 if( e
!= ERROR_OK
){
3739 Jim_SetResult_sprintf( interp
, "examine-fails: %d", e
);
3745 Jim_WrongNumArgs( goi
.interp
, 2, argv
, "[no parameters]");
3748 if( !(target_was_examined(target
)) ){
3749 e
= ERROR_TARGET_NOT_EXAMINED
;
3751 e
= target
->type
->poll( target
);
3753 if( e
!= ERROR_OK
){
3754 Jim_SetResult_sprintf( interp
, "poll-fails: %d", e
);
3761 if( goi
.argc
!= 2 ){
3762 Jim_WrongNumArgs( interp
, 2, argv
, "t|f|assert|deassert BOOL");
3765 e
= Jim_GetOpt_Nvp( &goi
, nvp_assert
, &n
);
3767 Jim_GetOpt_NvpUnknown( &goi
, nvp_assert
, 1 );
3770 /* the halt or not param */
3771 e
= Jim_GetOpt_Wide( &goi
, &a
);
3775 /* determine if we should halt or not. */
3776 target
->reset_halt
= !!a
;
3777 /* When this happens - all workareas are invalid. */
3778 target_free_all_working_areas_restore(target
, 0);
3781 if( n
->value
== NVP_ASSERT
){
3782 target
->type
->assert_reset( target
);
3784 target
->type
->deassert_reset( target
);
3789 Jim_WrongNumArgs( goi
.interp
, 0, argv
, "halt [no parameters]");
3792 target
->type
->halt( target
);
3794 case TS_CMD_WAITSTATE
:
3795 /* params: <name> statename timeoutmsecs */
3796 if( goi
.argc
!= 2 ){
3797 Jim_SetResult_sprintf( goi
.interp
, "%s STATENAME TIMEOUTMSECS", n
->name
);
3800 e
= Jim_GetOpt_Nvp( &goi
, nvp_target_state
, &n
);
3802 Jim_GetOpt_NvpUnknown( &goi
, nvp_target_state
,1 );
3805 e
= Jim_GetOpt_Wide( &goi
, &a
);
3809 e
= target_wait_state( target
, n
->value
, a
);
3810 if( e
!= ERROR_OK
){
3811 Jim_SetResult_sprintf( goi
.interp
,
3812 "target: %s wait %s fails (%d) %s",
3815 e
, target_strerror_safe(e
) );
3820 case TS_CMD_EVENTLIST
:
3821 /* List for human, Events defined for this target.
3822 * scripts/programs should use 'name cget -event NAME'
3825 target_event_action_t
*teap
;
3826 teap
= target
->event_action
;
3827 command_print( cmd_ctx
, "Event actions for target (%d) %s\n",
3828 target
->target_number
,
3830 command_print( cmd_ctx
, "%-25s | Body", "Event");
3831 command_print( cmd_ctx
, "------------------------- | ----------------------------------------");
3833 command_print( cmd_ctx
,
3835 Jim_Nvp_value2name_simple( nvp_target_event
, teap
->event
)->name
,
3836 Jim_GetString( teap
->body
, NULL
) );
3839 command_print( cmd_ctx
, "***END***");
3842 case TS_CMD_CURSTATE
:
3843 if( goi
.argc
!= 0 ){
3844 Jim_WrongNumArgs( goi
.interp
, 0, argv
, "[no parameters]");
3847 Jim_SetResultString( goi
.interp
,
3848 Jim_Nvp_value2name_simple(nvp_target_state
,target
->state
)->name
,-1);
3850 case TS_CMD_INVOKE_EVENT
:
3851 if( goi
.argc
!= 1 ){
3852 Jim_SetResult_sprintf( goi
.interp
, "%s ?EVENTNAME?",n
->name
);
3855 e
= Jim_GetOpt_Nvp( &goi
, nvp_target_event
, &n
);
3857 Jim_GetOpt_NvpUnknown( &goi
, nvp_target_event
, 1 );
3860 target_handle_event( target
, n
->value
);
3866 static int target_create( Jim_GetOptInfo
*goi
)
3875 struct command_context_s
*cmd_ctx
;
3877 cmd_ctx
= Jim_GetAssocData(goi
->interp
, "context");
3878 if( goi
->argc
< 3 ){
3879 Jim_WrongNumArgs( goi
->interp
, 1, goi
->argv
, "?name? ?type? ..options...");
3884 Jim_GetOpt_Obj( goi
, &new_cmd
);
3885 /* does this command exist? */
3886 cmd
= Jim_GetCommand( goi
->interp
, new_cmd
, JIM_ERRMSG
);
3888 cp
= Jim_GetString( new_cmd
, NULL
);
3889 Jim_SetResult_sprintf(goi
->interp
, "Command/target: %s Exists", cp
);
3894 e
= Jim_GetOpt_String( goi
, &cp2
, NULL
);
3896 /* now does target type exist */
3897 for( x
= 0 ; target_types
[x
] ; x
++ ){
3898 if( 0 == strcmp( cp
, target_types
[x
]->name
) ){
3903 if( target_types
[x
] == NULL
){
3904 Jim_SetResult_sprintf( goi
->interp
, "Unknown target type %s, try one of ", cp
);
3905 for( x
= 0 ; target_types
[x
] ; x
++ ){
3906 if( target_types
[x
+1] ){
3907 Jim_AppendStrings( goi
->interp
,
3908 Jim_GetResult(goi
->interp
),
3909 target_types
[x
]->name
,
3912 Jim_AppendStrings( goi
->interp
,
3913 Jim_GetResult(goi
->interp
),
3915 target_types
[x
]->name
,NULL
);
3922 target
= calloc(1,sizeof(target_t
));
3923 /* set target number */
3924 target
->target_number
= new_target_number();
3926 /* allocate memory for each unique target type */
3927 target
->type
= (target_type_t
*)calloc(1,sizeof(target_type_t
));
3929 memcpy( target
->type
, target_types
[x
], sizeof(target_type_t
));
3931 /* will be set by "-endian" */
3932 target
->endianness
= TARGET_ENDIAN_UNKNOWN
;
3934 target
->working_area
= 0x0;
3935 target
->working_area_size
= 0x0;
3936 target
->working_areas
= NULL
;
3937 target
->backup_working_area
= 0;
3939 target
->state
= TARGET_UNKNOWN
;
3940 target
->debug_reason
= DBG_REASON_UNDEFINED
;
3941 target
->reg_cache
= NULL
;
3942 target
->breakpoints
= NULL
;
3943 target
->watchpoints
= NULL
;
3944 target
->next
= NULL
;
3945 target
->arch_info
= NULL
;
3947 target
->display
= 1;
3949 /* initialize trace information */
3950 target
->trace_info
= malloc(sizeof(trace_t
));
3951 target
->trace_info
->num_trace_points
= 0;
3952 target
->trace_info
->trace_points_size
= 0;
3953 target
->trace_info
->trace_points
= NULL
;
3954 target
->trace_info
->trace_history_size
= 0;
3955 target
->trace_info
->trace_history
= NULL
;
3956 target
->trace_info
->trace_history_pos
= 0;
3957 target
->trace_info
->trace_history_overflowed
= 0;
3959 target
->dbgmsg
= NULL
;
3960 target
->dbg_msg_enabled
= 0;
3962 target
->endianness
= TARGET_ENDIAN_UNKNOWN
;
3964 /* Do the rest as "configure" options */
3965 goi
->isconfigure
= 1;
3966 e
= target_configure( goi
, target
);
3968 if (target
->tap
== NULL
)
3970 Jim_SetResultString( interp
, "-chain-position required when creating target", -1);
3975 free( target
->type
);
3980 if( target
->endianness
== TARGET_ENDIAN_UNKNOWN
){
3981 /* default endian to little if not specified */
3982 target
->endianness
= TARGET_LITTLE_ENDIAN
;
3985 /* incase variant is not set */
3986 if (!target
->variant
)
3987 target
->variant
= strdup("");
3989 /* create the target specific commands */
3990 if( target
->type
->register_commands
){
3991 (*(target
->type
->register_commands
))( cmd_ctx
);
3993 if( target
->type
->target_create
){
3994 (*(target
->type
->target_create
))( target
, goi
->interp
);
3997 /* append to end of list */
4000 tpp
= &(all_targets
);
4002 tpp
= &( (*tpp
)->next
);
4007 cp
= Jim_GetString( new_cmd
, NULL
);
4008 target
->cmd_name
= strdup(cp
);
4010 /* now - create the new target name command */
4011 e
= Jim_CreateCommand( goi
->interp
,
4014 tcl_target_func
, /* C function */
4015 target
, /* private data */
4016 NULL
); /* no del proc */
4021 static int jim_target( Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
4025 struct command_context_s
*cmd_ctx
;
4029 /* TG = target generic */
4037 const char *target_cmds
[] = {
4038 "create", "types", "names", "current", "number",
4040 NULL
/* terminate */
4043 LOG_DEBUG("Target command params:");
4044 LOG_DEBUG("%s", Jim_Debug_ArgvString(interp
, argc
, argv
));
4046 cmd_ctx
= Jim_GetAssocData( interp
, "context" );
4048 Jim_GetOpt_Setup( &goi
, interp
, argc
-1, argv
+1 );
4050 if( goi
.argc
== 0 ){
4051 Jim_WrongNumArgs(interp
, 1, argv
, "missing: command ...");
4055 /* Jim_GetOpt_Debug( &goi ); */
4056 r
= Jim_GetOpt_Enum( &goi
, target_cmds
, &x
);
4063 Jim_Panic(goi
.interp
,"Why am I here?");
4065 case TG_CMD_CURRENT
:
4066 if( goi
.argc
!= 0 ){
4067 Jim_WrongNumArgs( goi
.interp
, 1, goi
.argv
, "Too many parameters");
4070 Jim_SetResultString( goi
.interp
, get_current_target( cmd_ctx
)->cmd_name
, -1 );
4073 if( goi
.argc
!= 0 ){
4074 Jim_WrongNumArgs( goi
.interp
, 1, goi
.argv
, "Too many parameters" );
4077 Jim_SetResult( goi
.interp
, Jim_NewListObj( goi
.interp
, NULL
, 0 ) );
4078 for( x
= 0 ; target_types
[x
] ; x
++ ){
4079 Jim_ListAppendElement( goi
.interp
,
4080 Jim_GetResult(goi
.interp
),
4081 Jim_NewStringObj( goi
.interp
, target_types
[x
]->name
, -1 ) );
4085 if( goi
.argc
!= 0 ){
4086 Jim_WrongNumArgs( goi
.interp
, 1, goi
.argv
, "Too many parameters" );
4089 Jim_SetResult( goi
.interp
, Jim_NewListObj( goi
.interp
, NULL
, 0 ) );
4090 target
= all_targets
;
4092 Jim_ListAppendElement( goi
.interp
,
4093 Jim_GetResult(goi
.interp
),
4094 Jim_NewStringObj( goi
.interp
, target
->cmd_name
, -1 ) );
4095 target
= target
->next
;
4100 Jim_WrongNumArgs( goi
.interp
, goi
.argc
, goi
.argv
, "?name ... config options ...");
4103 return target_create( &goi
);
4106 if( goi
.argc
!= 1 ){
4107 Jim_SetResult_sprintf( goi
.interp
, "expected: target number ?NUMBER?");
4110 e
= Jim_GetOpt_Wide( &goi
, &w
);
4116 t
= get_target_by_num(w
);
4118 Jim_SetResult_sprintf( goi
.interp
,"Target: number %d does not exist", (int)(w
));
4121 Jim_SetResultString( goi
.interp
, t
->cmd_name
, -1 );
4125 if( goi
.argc
!= 0 ){
4126 Jim_WrongNumArgs( goi
.interp
, 0, goi
.argv
, "<no parameters>");
4129 Jim_SetResult( goi
.interp
,
4130 Jim_NewIntObj( goi
.interp
, max_target_number()));
4146 static int fastload_num
;
4147 static struct FastLoad
*fastload
;
4149 static void free_fastload(void)
4154 for (i
=0; i
<fastload_num
; i
++)
4156 if (fastload
[i
].data
)
4157 free(fastload
[i
].data
);
4167 static int handle_fast_load_image_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
4173 u32 max_address
=0xffffffff;
4179 duration_t duration
;
4180 char *duration_text
;
4182 if ((argc
< 1)||(argc
> 5))
4184 return ERROR_COMMAND_SYNTAX_ERROR
;
4187 /* a base address isn't always necessary, default to 0x0 (i.e. don't relocate) */
4190 image
.base_address_set
= 1;
4191 image
.base_address
= strtoul(args
[1], NULL
, 0);
4195 image
.base_address_set
= 0;
4199 image
.start_address_set
= 0;
4203 min_address
=strtoul(args
[3], NULL
, 0);
4207 max_address
=strtoul(args
[4], NULL
, 0)+min_address
;
4210 if (min_address
>max_address
)
4212 return ERROR_COMMAND_SYNTAX_ERROR
;
4215 duration_start_measure(&duration
);
4217 if (image_open(&image
, args
[0], (argc
>= 3) ? args
[2] : NULL
) != ERROR_OK
)
4224 fastload_num
=image
.num_sections
;
4225 fastload
=(struct FastLoad
*)malloc(sizeof(struct FastLoad
)*image
.num_sections
);
4228 image_close(&image
);
4231 memset(fastload
, 0, sizeof(struct FastLoad
)*image
.num_sections
);
4232 for (i
= 0; i
< image
.num_sections
; i
++)
4234 buffer
= malloc(image
.sections
[i
].size
);
4237 command_print(cmd_ctx
, "error allocating buffer for section (%d bytes)", image
.sections
[i
].size
);
4241 if ((retval
= image_read_section(&image
, i
, 0x0, image
.sections
[i
].size
, buffer
, &buf_cnt
)) != ERROR_OK
)
4251 /* DANGER!!! beware of unsigned comparision here!!! */
4253 if ((image
.sections
[i
].base_address
+buf_cnt
>=min_address
)&&
4254 (image
.sections
[i
].base_address
<max_address
))
4256 if (image
.sections
[i
].base_address
<min_address
)
4258 /* clip addresses below */
4259 offset
+=min_address
-image
.sections
[i
].base_address
;
4263 if (image
.sections
[i
].base_address
+buf_cnt
>max_address
)
4265 length
-=(image
.sections
[i
].base_address
+buf_cnt
)-max_address
;
4268 fastload
[i
].address
=image
.sections
[i
].base_address
+offset
;
4269 fastload
[i
].data
=malloc(length
);
4270 if (fastload
[i
].data
==NULL
)
4275 memcpy(fastload
[i
].data
, buffer
+offset
, length
);
4276 fastload
[i
].length
=length
;
4278 image_size
+= length
;
4279 command_print(cmd_ctx
, "%u byte written at address 0x%8.8x", length
, image
.sections
[i
].base_address
+offset
);
4285 duration_stop_measure(&duration
, &duration_text
);
4286 if (retval
==ERROR_OK
)
4288 command_print(cmd_ctx
, "Loaded %u bytes in %s", image_size
, duration_text
);
4289 command_print(cmd_ctx
, "NB!!! image has not been loaded to target, issue a subsequent 'fast_load' to do so.");
4291 free(duration_text
);
4293 image_close(&image
);
4295 if (retval
!=ERROR_OK
)
4303 static int handle_fast_load_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
4306 return ERROR_COMMAND_SYNTAX_ERROR
;
4309 LOG_ERROR("No image in memory");
4313 int ms
=timeval_ms();
4315 int retval
=ERROR_OK
;
4316 for (i
=0; i
<fastload_num
;i
++)
4318 target_t
*target
= get_current_target(cmd_ctx
);
4319 command_print(cmd_ctx
, "Write to 0x%08x, length 0x%08x", fastload
[i
].address
, fastload
[i
].length
);
4320 if (retval
==ERROR_OK
)
4322 retval
= target_write_buffer(target
, fastload
[i
].address
, fastload
[i
].length
, fastload
[i
].data
);
4324 size
+=fastload
[i
].length
;
4326 int after
=timeval_ms();
4327 command_print(cmd_ctx
, "Loaded image %f kBytes/s", (float)(size
/1024.0)/((float)(after
-ms
)/1000.0));