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 ***************************************************************************/
36 #include "replacements.h"
38 #include "target_request.h"
41 #include "configuration.h"
42 #include "binarybuffer.h"
56 #include <time_support.h>
61 static int handle_targets_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
);
63 static int handle_reg_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
);
64 static int handle_poll_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
);
65 static int handle_halt_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
);
66 static int handle_wait_halt_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
);
67 static int handle_reset_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
);
68 static int handle_soft_reset_halt_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
);
69 static int handle_resume_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
);
70 static int handle_step_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
);
71 static int handle_md_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
);
72 static int handle_mw_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
);
73 static int handle_load_image_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
);
74 static int handle_dump_image_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
);
75 static int handle_verify_image_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
);
76 static int handle_test_image_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
);
77 static int handle_bp_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
);
78 static int handle_rbp_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
);
79 static int handle_wp_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
);
80 static int handle_rwp_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
);
81 static int handle_virt2phys_command(command_context_t
*cmd_ctx
, char *cmd
, char **args
, int argc
);
82 static int handle_profile_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
);
83 static int handle_fast_load_image_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
);
84 static int handle_fast_load_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
);
86 static int jim_array2mem(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
);
87 static int jim_mem2array(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
);
88 static int jim_target( Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
);
90 static int target_array2mem(Jim_Interp
*interp
, target_t
*target
, int argc
, Jim_Obj
*const *argv
);
91 static int target_mem2array(Jim_Interp
*interp
, target_t
*target
, int argc
, Jim_Obj
*const *argv
);
94 extern target_type_t arm7tdmi_target
;
95 extern target_type_t arm720t_target
;
96 extern target_type_t arm9tdmi_target
;
97 extern target_type_t arm920t_target
;
98 extern target_type_t arm966e_target
;
99 extern target_type_t arm926ejs_target
;
100 extern target_type_t feroceon_target
;
101 extern target_type_t xscale_target
;
102 extern target_type_t cortexm3_target
;
103 extern target_type_t cortexa8_target
;
104 extern target_type_t arm11_target
;
105 extern target_type_t mips_m4k_target
;
106 extern target_type_t avr_target
;
108 target_type_t
*target_types
[] =
126 target_t
*all_targets
= NULL
;
127 target_event_callback_t
*target_event_callbacks
= NULL
;
128 target_timer_callback_t
*target_timer_callbacks
= NULL
;
130 const Jim_Nvp nvp_assert
[] = {
131 { .name
= "assert", NVP_ASSERT
},
132 { .name
= "deassert", NVP_DEASSERT
},
133 { .name
= "T", NVP_ASSERT
},
134 { .name
= "F", NVP_DEASSERT
},
135 { .name
= "t", NVP_ASSERT
},
136 { .name
= "f", NVP_DEASSERT
},
137 { .name
= NULL
, .value
= -1 }
140 const Jim_Nvp nvp_error_target
[] = {
141 { .value
= ERROR_TARGET_INVALID
, .name
= "err-invalid" },
142 { .value
= ERROR_TARGET_INIT_FAILED
, .name
= "err-init-failed" },
143 { .value
= ERROR_TARGET_TIMEOUT
, .name
= "err-timeout" },
144 { .value
= ERROR_TARGET_NOT_HALTED
, .name
= "err-not-halted" },
145 { .value
= ERROR_TARGET_FAILURE
, .name
= "err-failure" },
146 { .value
= ERROR_TARGET_UNALIGNED_ACCESS
, .name
= "err-unaligned-access" },
147 { .value
= ERROR_TARGET_DATA_ABORT
, .name
= "err-data-abort" },
148 { .value
= ERROR_TARGET_RESOURCE_NOT_AVAILABLE
, .name
= "err-resource-not-available" },
149 { .value
= ERROR_TARGET_TRANSLATION_FAULT
, .name
= "err-translation-fault" },
150 { .value
= ERROR_TARGET_NOT_RUNNING
, .name
= "err-not-running" },
151 { .value
= ERROR_TARGET_NOT_EXAMINED
, .name
= "err-not-examined" },
152 { .value
= -1, .name
= NULL
}
155 const char *target_strerror_safe( int err
)
159 n
= Jim_Nvp_value2name_simple( nvp_error_target
, err
);
160 if( n
->name
== NULL
){
167 const Jim_Nvp nvp_target_event
[] = {
168 { .value
= TARGET_EVENT_OLD_gdb_program_config
, .name
= "old-gdb_program_config" },
169 { .value
= TARGET_EVENT_OLD_pre_resume
, .name
= "old-pre_resume" },
171 { .value
= TARGET_EVENT_EARLY_HALTED
, .name
= "early-halted" },
172 { .value
= TARGET_EVENT_HALTED
, .name
= "halted" },
173 { .value
= TARGET_EVENT_RESUMED
, .name
= "resumed" },
174 { .value
= TARGET_EVENT_RESUME_START
, .name
= "resume-start" },
175 { .value
= TARGET_EVENT_RESUME_END
, .name
= "resume-end" },
177 { .name
= "gdb-start", .value
= TARGET_EVENT_GDB_START
},
178 { .name
= "gdb-end", .value
= TARGET_EVENT_GDB_END
},
180 /* historical name */
182 { .value
= TARGET_EVENT_RESET_START
, .name
= "reset-start" },
184 { .value
= TARGET_EVENT_RESET_ASSERT_PRE
, .name
= "reset-assert-pre" },
185 { .value
= TARGET_EVENT_RESET_ASSERT_POST
, .name
= "reset-assert-post" },
186 { .value
= TARGET_EVENT_RESET_DEASSERT_PRE
, .name
= "reset-deassert-pre" },
187 { .value
= TARGET_EVENT_RESET_DEASSERT_POST
, .name
= "reset-deassert-post" },
188 { .value
= TARGET_EVENT_RESET_HALT_PRE
, .name
= "reset-halt-pre" },
189 { .value
= TARGET_EVENT_RESET_HALT_POST
, .name
= "reset-halt-post" },
190 { .value
= TARGET_EVENT_RESET_WAIT_PRE
, .name
= "reset-wait-pre" },
191 { .value
= TARGET_EVENT_RESET_WAIT_POST
, .name
= "reset-wait-post" },
192 { .value
= TARGET_EVENT_RESET_INIT
, .name
= "reset-init" },
193 { .value
= TARGET_EVENT_RESET_END
, .name
= "reset-end" },
195 { .value
= TARGET_EVENT_EXAMINE_START
, .name
= "examine-start" },
196 { .value
= TARGET_EVENT_EXAMINE_END
, .name
= "examine-end" },
198 { .value
= TARGET_EVENT_DEBUG_HALTED
, .name
= "debug-halted" },
199 { .value
= TARGET_EVENT_DEBUG_RESUMED
, .name
= "debug-resumed" },
201 { .value
= TARGET_EVENT_GDB_ATTACH
, .name
= "gdb-attach" },
202 { .value
= TARGET_EVENT_GDB_DETACH
, .name
= "gdb-detach" },
204 { .value
= TARGET_EVENT_GDB_FLASH_WRITE_START
, .name
= "gdb-flash-write-start" },
205 { .value
= TARGET_EVENT_GDB_FLASH_WRITE_END
, .name
= "gdb-flash-write-end" },
207 { .value
= TARGET_EVENT_GDB_FLASH_ERASE_START
, .name
= "gdb-flash-erase-start" },
208 { .value
= TARGET_EVENT_GDB_FLASH_ERASE_END
, .name
= "gdb-flash-erase-end" },
210 { .value
= TARGET_EVENT_RESUME_START
, .name
= "resume-start" },
211 { .value
= TARGET_EVENT_RESUMED
, .name
= "resume-ok" },
212 { .value
= TARGET_EVENT_RESUME_END
, .name
= "resume-end" },
214 { .name
= NULL
, .value
= -1 }
217 const Jim_Nvp nvp_target_state
[] = {
218 { .name
= "unknown", .value
= TARGET_UNKNOWN
},
219 { .name
= "running", .value
= TARGET_RUNNING
},
220 { .name
= "halted", .value
= TARGET_HALTED
},
221 { .name
= "reset", .value
= TARGET_RESET
},
222 { .name
= "debug-running", .value
= TARGET_DEBUG_RUNNING
},
223 { .name
= NULL
, .value
= -1 },
226 const Jim_Nvp nvp_target_debug_reason
[] = {
227 { .name
= "debug-request" , .value
= DBG_REASON_DBGRQ
},
228 { .name
= "breakpoint" , .value
= DBG_REASON_BREAKPOINT
},
229 { .name
= "watchpoint" , .value
= DBG_REASON_WATCHPOINT
},
230 { .name
= "watchpoint-and-breakpoint", .value
= DBG_REASON_WPTANDBKPT
},
231 { .name
= "single-step" , .value
= DBG_REASON_SINGLESTEP
},
232 { .name
= "target-not-halted" , .value
= DBG_REASON_NOTHALTED
},
233 { .name
= "undefined" , .value
= DBG_REASON_UNDEFINED
},
234 { .name
= NULL
, .value
= -1 },
237 const Jim_Nvp nvp_target_endian
[] = {
238 { .name
= "big", .value
= TARGET_BIG_ENDIAN
},
239 { .name
= "little", .value
= TARGET_LITTLE_ENDIAN
},
240 { .name
= "be", .value
= TARGET_BIG_ENDIAN
},
241 { .name
= "le", .value
= TARGET_LITTLE_ENDIAN
},
242 { .name
= NULL
, .value
= -1 },
245 const Jim_Nvp nvp_reset_modes
[] = {
246 { .name
= "unknown", .value
= RESET_UNKNOWN
},
247 { .name
= "run" , .value
= RESET_RUN
},
248 { .name
= "halt" , .value
= RESET_HALT
},
249 { .name
= "init" , .value
= RESET_INIT
},
250 { .name
= NULL
, .value
= -1 },
253 static int max_target_number(void)
261 if( x
< t
->target_number
){
262 x
= (t
->target_number
)+1;
269 /* determine the number of the new target */
270 static int new_target_number(void)
275 /* number is 0 based */
279 if( x
< t
->target_number
){
280 x
= t
->target_number
;
287 static int target_continous_poll
= 1;
289 /* read a u32 from a buffer in target memory endianness */
290 u32
target_buffer_get_u32(target_t
*target
, u8
*buffer
)
292 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
293 return le_to_h_u32(buffer
);
295 return be_to_h_u32(buffer
);
298 /* read a u16 from a buffer in target memory endianness */
299 u16
target_buffer_get_u16(target_t
*target
, u8
*buffer
)
301 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
302 return le_to_h_u16(buffer
);
304 return be_to_h_u16(buffer
);
307 /* read a u8 from a buffer in target memory endianness */
308 u8
target_buffer_get_u8(target_t
*target
, u8
*buffer
)
310 return *buffer
& 0x0ff;
313 /* write a u32 to a buffer in target memory endianness */
314 void target_buffer_set_u32(target_t
*target
, u8
*buffer
, u32 value
)
316 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
317 h_u32_to_le(buffer
, value
);
319 h_u32_to_be(buffer
, value
);
322 /* write a u16 to a buffer in target memory endianness */
323 void target_buffer_set_u16(target_t
*target
, u8
*buffer
, u16 value
)
325 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
326 h_u16_to_le(buffer
, value
);
328 h_u16_to_be(buffer
, value
);
331 /* write a u8 to a buffer in target memory endianness */
332 void target_buffer_set_u8(target_t
*target
, u8
*buffer
, u8 value
)
337 /* returns a pointer to the n-th configured target */
338 target_t
* get_target_by_num(int num
)
340 target_t
*target
= all_targets
;
343 if( target
->target_number
== num
){
346 target
= target
->next
;
352 int get_num_by_target(target_t
*query_target
)
354 return query_target
->target_number
;
357 target_t
* get_current_target(command_context_t
*cmd_ctx
)
359 target_t
*target
= get_target_by_num(cmd_ctx
->current_target
);
363 LOG_ERROR("BUG: current_target out of bounds");
370 int target_poll(struct target_s
*target
)
372 /* We can't poll until after examine */
373 if (!target
->type
->examined
)
375 /* Fail silently lest we pollute the log */
378 return target
->type
->poll(target
);
381 int target_halt(struct target_s
*target
)
383 /* We can't poll until after examine */
384 if (!target
->type
->examined
)
386 LOG_ERROR("Target not examined yet");
389 return target
->type
->halt(target
);
392 int target_resume(struct target_s
*target
, int current
, u32 address
, int handle_breakpoints
, int debug_execution
)
396 /* We can't poll until after examine */
397 if (!target
->type
->examined
)
399 LOG_ERROR("Target not examined yet");
403 /* note that resume *must* be asynchronous. The CPU can halt before we poll. The CPU can
404 * even halt at the current PC as a result of a software breakpoint being inserted by (a bug?)
407 if ((retval
= target
->type
->resume(target
, current
, address
, handle_breakpoints
, debug_execution
)) != ERROR_OK
)
413 int target_process_reset(struct command_context_s
*cmd_ctx
, enum target_reset_mode reset_mode
)
418 n
= Jim_Nvp_value2name_simple( nvp_reset_modes
, reset_mode
);
419 if( n
->name
== NULL
){
420 LOG_ERROR("invalid reset mode");
424 sprintf( buf
, "ocd_process_reset %s", n
->name
);
425 retval
= Jim_Eval( interp
, buf
);
427 if(retval
!= JIM_OK
) {
428 Jim_PrintErrorMessage(interp
);
432 /* We want any events to be processed before the prompt */
433 retval
= target_call_timer_callbacks_now();
438 static int default_virt2phys(struct target_s
*target
, u32
virtual, u32
*physical
)
444 static int default_mmu(struct target_s
*target
, int *enabled
)
450 static int default_examine(struct target_s
*target
)
452 target
->type
->examined
= 1;
456 /* Targets that correctly implement init+examine, i.e.
457 * no communication with target during init:
461 int target_examine(void)
463 int retval
= ERROR_OK
;
464 target_t
*target
= all_targets
;
467 if ((retval
= target
->type
->examine(target
))!=ERROR_OK
)
469 target
= target
->next
;
474 static int target_write_memory_imp(struct target_s
*target
, u32 address
, u32 size
, u32 count
, u8
*buffer
)
476 if (!target
->type
->examined
)
478 LOG_ERROR("Target not examined yet");
481 return target
->type
->write_memory_imp(target
, address
, size
, count
, buffer
);
484 static int target_read_memory_imp(struct target_s
*target
, u32 address
, u32 size
, u32 count
, u8
*buffer
)
486 if (!target
->type
->examined
)
488 LOG_ERROR("Target not examined yet");
491 return target
->type
->read_memory_imp(target
, address
, size
, count
, buffer
);
494 static int target_soft_reset_halt_imp(struct target_s
*target
)
496 if (!target
->type
->examined
)
498 LOG_ERROR("Target not examined yet");
501 return target
->type
->soft_reset_halt_imp(target
);
504 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
)
506 if (!target
->type
->examined
)
508 LOG_ERROR("Target not examined yet");
511 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
);
514 int target_init(struct command_context_s
*cmd_ctx
)
516 target_t
*target
= all_targets
;
521 target
->type
->examined
= 0;
522 if (target
->type
->examine
== NULL
)
524 target
->type
->examine
= default_examine
;
527 if ((retval
= target
->type
->init_target(cmd_ctx
, target
)) != ERROR_OK
)
529 LOG_ERROR("target '%s' init failed", target
->type
->name
);
533 /* Set up default functions if none are provided by target */
534 if (target
->type
->virt2phys
== NULL
)
536 target
->type
->virt2phys
= default_virt2phys
;
538 target
->type
->virt2phys
= default_virt2phys
;
539 /* a non-invasive way(in terms of patches) to add some code that
540 * runs before the type->write/read_memory implementation
542 target
->type
->write_memory_imp
= target
->type
->write_memory
;
543 target
->type
->write_memory
= target_write_memory_imp
;
544 target
->type
->read_memory_imp
= target
->type
->read_memory
;
545 target
->type
->read_memory
= target_read_memory_imp
;
546 target
->type
->soft_reset_halt_imp
= target
->type
->soft_reset_halt
;
547 target
->type
->soft_reset_halt
= target_soft_reset_halt_imp
;
548 target
->type
->run_algorithm_imp
= target
->type
->run_algorithm
;
549 target
->type
->run_algorithm
= target_run_algorithm_imp
;
551 if (target
->type
->mmu
== NULL
)
553 target
->type
->mmu
= default_mmu
;
555 target
= target
->next
;
560 if((retval
= target_register_user_commands(cmd_ctx
)) != ERROR_OK
)
562 if((retval
= target_register_timer_callback(handle_target
, 100, 1, NULL
)) != ERROR_OK
)
569 int target_register_event_callback(int (*callback
)(struct target_s
*target
, enum target_event event
, void *priv
), void *priv
)
571 target_event_callback_t
**callbacks_p
= &target_event_callbacks
;
573 if (callback
== NULL
)
575 return ERROR_INVALID_ARGUMENTS
;
580 while ((*callbacks_p
)->next
)
581 callbacks_p
= &((*callbacks_p
)->next
);
582 callbacks_p
= &((*callbacks_p
)->next
);
585 (*callbacks_p
) = malloc(sizeof(target_event_callback_t
));
586 (*callbacks_p
)->callback
= callback
;
587 (*callbacks_p
)->priv
= priv
;
588 (*callbacks_p
)->next
= NULL
;
593 int target_register_timer_callback(int (*callback
)(void *priv
), int time_ms
, int periodic
, void *priv
)
595 target_timer_callback_t
**callbacks_p
= &target_timer_callbacks
;
598 if (callback
== NULL
)
600 return ERROR_INVALID_ARGUMENTS
;
605 while ((*callbacks_p
)->next
)
606 callbacks_p
= &((*callbacks_p
)->next
);
607 callbacks_p
= &((*callbacks_p
)->next
);
610 (*callbacks_p
) = malloc(sizeof(target_timer_callback_t
));
611 (*callbacks_p
)->callback
= callback
;
612 (*callbacks_p
)->periodic
= periodic
;
613 (*callbacks_p
)->time_ms
= time_ms
;
615 gettimeofday(&now
, NULL
);
616 (*callbacks_p
)->when
.tv_usec
= now
.tv_usec
+ (time_ms
% 1000) * 1000;
617 time_ms
-= (time_ms
% 1000);
618 (*callbacks_p
)->when
.tv_sec
= now
.tv_sec
+ (time_ms
/ 1000);
619 if ((*callbacks_p
)->when
.tv_usec
> 1000000)
621 (*callbacks_p
)->when
.tv_usec
= (*callbacks_p
)->when
.tv_usec
- 1000000;
622 (*callbacks_p
)->when
.tv_sec
+= 1;
625 (*callbacks_p
)->priv
= priv
;
626 (*callbacks_p
)->next
= NULL
;
631 int target_unregister_event_callback(int (*callback
)(struct target_s
*target
, enum target_event event
, void *priv
), void *priv
)
633 target_event_callback_t
**p
= &target_event_callbacks
;
634 target_event_callback_t
*c
= target_event_callbacks
;
636 if (callback
== NULL
)
638 return ERROR_INVALID_ARGUMENTS
;
643 target_event_callback_t
*next
= c
->next
;
644 if ((c
->callback
== callback
) && (c
->priv
== priv
))
658 int target_unregister_timer_callback(int (*callback
)(void *priv
), void *priv
)
660 target_timer_callback_t
**p
= &target_timer_callbacks
;
661 target_timer_callback_t
*c
= target_timer_callbacks
;
663 if (callback
== NULL
)
665 return ERROR_INVALID_ARGUMENTS
;
670 target_timer_callback_t
*next
= c
->next
;
671 if ((c
->callback
== callback
) && (c
->priv
== priv
))
685 int target_call_event_callbacks(target_t
*target
, enum target_event event
)
687 target_event_callback_t
*callback
= target_event_callbacks
;
688 target_event_callback_t
*next_callback
;
690 if (event
== TARGET_EVENT_HALTED
)
692 /* execute early halted first */
693 target_call_event_callbacks(target
, TARGET_EVENT_EARLY_HALTED
);
696 LOG_DEBUG("target event %i (%s)",
698 Jim_Nvp_value2name_simple( nvp_target_event
, event
)->name
);
700 target_handle_event( target
, event
);
704 next_callback
= callback
->next
;
705 callback
->callback(target
, event
, callback
->priv
);
706 callback
= next_callback
;
712 static int target_call_timer_callbacks_check_time(int checktime
)
714 target_timer_callback_t
*callback
= target_timer_callbacks
;
715 target_timer_callback_t
*next_callback
;
720 gettimeofday(&now
, NULL
);
724 next_callback
= callback
->next
;
726 if ((!checktime
&&callback
->periodic
)||
727 (((now
.tv_sec
>= callback
->when
.tv_sec
) && (now
.tv_usec
>= callback
->when
.tv_usec
))
728 || (now
.tv_sec
> callback
->when
.tv_sec
)))
730 if(callback
->callback
!= NULL
)
732 callback
->callback(callback
->priv
);
733 if (callback
->periodic
)
735 int time_ms
= callback
->time_ms
;
736 callback
->when
.tv_usec
= now
.tv_usec
+ (time_ms
% 1000) * 1000;
737 time_ms
-= (time_ms
% 1000);
738 callback
->when
.tv_sec
= now
.tv_sec
+ time_ms
/ 1000;
739 if (callback
->when
.tv_usec
> 1000000)
741 callback
->when
.tv_usec
= callback
->when
.tv_usec
- 1000000;
742 callback
->when
.tv_sec
+= 1;
748 if((retval
= target_unregister_timer_callback(callback
->callback
, callback
->priv
)) != ERROR_OK
)
754 callback
= next_callback
;
760 int target_call_timer_callbacks(void)
762 return target_call_timer_callbacks_check_time(1);
765 /* invoke periodic callbacks immediately */
766 int target_call_timer_callbacks_now(void)
768 return target_call_timer_callbacks_check_time(0);
771 int target_alloc_working_area(struct target_s
*target
, u32 size
, working_area_t
**area
)
773 working_area_t
*c
= target
->working_areas
;
774 working_area_t
*new_wa
= NULL
;
776 /* Reevaluate working area address based on MMU state*/
777 if (target
->working_areas
== NULL
)
781 retval
= target
->type
->mmu(target
, &enabled
);
782 if (retval
!= ERROR_OK
)
788 target
->working_area
= target
->working_area_virt
;
792 target
->working_area
= target
->working_area_phys
;
796 /* only allocate multiples of 4 byte */
799 LOG_ERROR("BUG: code tried to allocate unaligned number of bytes, padding");
800 size
= CEIL(size
, 4);
803 /* see if there's already a matching working area */
806 if ((c
->free
) && (c
->size
== size
))
814 /* if not, allocate a new one */
817 working_area_t
**p
= &target
->working_areas
;
818 u32 first_free
= target
->working_area
;
819 u32 free_size
= target
->working_area_size
;
821 LOG_DEBUG("allocating new working area");
823 c
= target
->working_areas
;
826 first_free
+= c
->size
;
827 free_size
-= c
->size
;
832 if (free_size
< size
)
834 LOG_WARNING("not enough working area available(requested %d, free %d)", size
, free_size
);
835 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
838 new_wa
= malloc(sizeof(working_area_t
));
841 new_wa
->address
= first_free
;
843 if (target
->backup_working_area
)
846 new_wa
->backup
= malloc(new_wa
->size
);
847 if((retval
= target
->type
->read_memory(target
, new_wa
->address
, 4, new_wa
->size
/ 4, new_wa
->backup
)) != ERROR_OK
)
849 free(new_wa
->backup
);
856 new_wa
->backup
= NULL
;
859 /* put new entry in list */
863 /* mark as used, and return the new (reused) area */
873 int target_free_working_area_restore(struct target_s
*target
, working_area_t
*area
, int restore
)
878 if (restore
&&target
->backup_working_area
)
881 if((retval
= target
->type
->write_memory(target
, area
->address
, 4, area
->size
/ 4, area
->backup
)) != ERROR_OK
)
887 /* mark user pointer invalid */
894 int target_free_working_area(struct target_s
*target
, working_area_t
*area
)
896 return target_free_working_area_restore(target
, area
, 1);
899 /* free resources and restore memory, if restoring memory fails,
900 * free up resources anyway
902 void target_free_all_working_areas_restore(struct target_s
*target
, int restore
)
904 working_area_t
*c
= target
->working_areas
;
908 working_area_t
*next
= c
->next
;
909 target_free_working_area_restore(target
, c
, restore
);
919 target
->working_areas
= NULL
;
922 void target_free_all_working_areas(struct target_s
*target
)
924 target_free_all_working_areas_restore(target
, 1);
927 int target_register_commands(struct command_context_s
*cmd_ctx
)
930 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)");
935 register_jim(cmd_ctx
, "target", jim_target
, "configure target" );
940 int target_arch_state(struct target_s
*target
)
945 LOG_USER("No target has been configured");
949 LOG_USER("target state: %s",
950 Jim_Nvp_value2name_simple(nvp_target_state
,target
->state
)->name
);
952 if (target
->state
!=TARGET_HALTED
)
955 retval
=target
->type
->arch_state(target
);
959 /* Single aligned words are guaranteed to use 16 or 32 bit access
960 * mode respectively, otherwise data is handled as quickly as
963 int target_write_buffer(struct target_s
*target
, u32 address
, u32 size
, u8
*buffer
)
966 LOG_DEBUG("writing buffer of %i byte at 0x%8.8x", size
, address
);
968 if (!target
->type
->examined
)
970 LOG_ERROR("Target not examined yet");
974 if ((address
+ size
- 1) < address
)
976 /* GDB can request this when e.g. PC is 0xfffffffc*/
977 LOG_ERROR("address+size wrapped(0x%08x, 0x%08x)", address
, size
);
981 if (((address
% 2) == 0) && (size
== 2))
983 return target
->type
->write_memory(target
, address
, 2, 1, buffer
);
986 /* handle unaligned head bytes */
989 u32 unaligned
= 4 - (address
% 4);
991 if (unaligned
> size
)
994 if ((retval
= target
->type
->write_memory(target
, address
, 1, unaligned
, buffer
)) != ERROR_OK
)
998 address
+= unaligned
;
1002 /* handle aligned words */
1005 int aligned
= size
- (size
% 4);
1007 /* use bulk writes above a certain limit. This may have to be changed */
1010 if ((retval
= target
->type
->bulk_write_memory(target
, address
, aligned
/ 4, buffer
)) != ERROR_OK
)
1015 if ((retval
= target
->type
->write_memory(target
, address
, 4, aligned
/ 4, buffer
)) != ERROR_OK
)
1024 /* handle tail writes of less than 4 bytes */
1027 if ((retval
= target
->type
->write_memory(target
, address
, 1, size
, buffer
)) != ERROR_OK
)
1034 /* Single aligned words are guaranteed to use 16 or 32 bit access
1035 * mode respectively, otherwise data is handled as quickly as
1038 int target_read_buffer(struct target_s
*target
, u32 address
, u32 size
, u8
*buffer
)
1041 LOG_DEBUG("reading buffer of %i byte at 0x%8.8x", size
, address
);
1043 if (!target
->type
->examined
)
1045 LOG_ERROR("Target not examined yet");
1049 if ((address
+ size
- 1) < address
)
1051 /* GDB can request this when e.g. PC is 0xfffffffc*/
1052 LOG_ERROR("address+size wrapped(0x%08x, 0x%08x)", address
, size
);
1056 if (((address
% 2) == 0) && (size
== 2))
1058 return target
->type
->read_memory(target
, address
, 2, 1, buffer
);
1061 /* handle unaligned head bytes */
1064 u32 unaligned
= 4 - (address
% 4);
1066 if (unaligned
> size
)
1069 if ((retval
= target
->type
->read_memory(target
, address
, 1, unaligned
, buffer
)) != ERROR_OK
)
1072 buffer
+= unaligned
;
1073 address
+= unaligned
;
1077 /* handle aligned words */
1080 int aligned
= size
- (size
% 4);
1082 if ((retval
= target
->type
->read_memory(target
, address
, 4, aligned
/ 4, buffer
)) != ERROR_OK
)
1090 /* handle tail writes of less than 4 bytes */
1093 if ((retval
= target
->type
->read_memory(target
, address
, 1, size
, buffer
)) != ERROR_OK
)
1100 int target_checksum_memory(struct target_s
*target
, u32 address
, u32 size
, u32
* crc
)
1106 if (!target
->type
->examined
)
1108 LOG_ERROR("Target not examined yet");
1112 if ((retval
= target
->type
->checksum_memory(target
, address
,
1113 size
, &checksum
)) != ERROR_OK
)
1115 buffer
= malloc(size
);
1118 LOG_ERROR("error allocating buffer for section (%d bytes)", size
);
1119 return ERROR_INVALID_ARGUMENTS
;
1121 retval
= target_read_buffer(target
, address
, size
, buffer
);
1122 if (retval
!= ERROR_OK
)
1128 /* convert to target endianess */
1129 for (i
= 0; i
< (size
/sizeof(u32
)); i
++)
1132 target_data
= target_buffer_get_u32(target
, &buffer
[i
*sizeof(u32
)]);
1133 target_buffer_set_u32(target
, &buffer
[i
*sizeof(u32
)], target_data
);
1136 retval
= image_calculate_checksum( buffer
, size
, &checksum
);
1145 int target_blank_check_memory(struct target_s
*target
, u32 address
, u32 size
, u32
* blank
)
1148 if (!target
->type
->examined
)
1150 LOG_ERROR("Target not examined yet");
1154 if (target
->type
->blank_check_memory
== 0)
1155 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
1157 retval
= target
->type
->blank_check_memory(target
, address
, size
, blank
);
1162 int target_read_u32(struct target_s
*target
, u32 address
, u32
*value
)
1165 if (!target
->type
->examined
)
1167 LOG_ERROR("Target not examined yet");
1171 int retval
= target
->type
->read_memory(target
, address
, 4, 1, value_buf
);
1173 if (retval
== ERROR_OK
)
1175 *value
= target_buffer_get_u32(target
, value_buf
);
1176 LOG_DEBUG("address: 0x%8.8x, value: 0x%8.8x", address
, *value
);
1181 LOG_DEBUG("address: 0x%8.8x failed", address
);
1187 int target_read_u16(struct target_s
*target
, u32 address
, u16
*value
)
1190 if (!target
->type
->examined
)
1192 LOG_ERROR("Target not examined yet");
1196 int retval
= target
->type
->read_memory(target
, address
, 2, 1, value_buf
);
1198 if (retval
== ERROR_OK
)
1200 *value
= target_buffer_get_u16(target
, value_buf
);
1201 LOG_DEBUG("address: 0x%8.8x, value: 0x%4.4x", address
, *value
);
1206 LOG_DEBUG("address: 0x%8.8x failed", address
);
1212 int target_read_u8(struct target_s
*target
, u32 address
, u8
*value
)
1214 int retval
= target
->type
->read_memory(target
, address
, 1, 1, value
);
1215 if (!target
->type
->examined
)
1217 LOG_ERROR("Target not examined yet");
1221 if (retval
== ERROR_OK
)
1223 LOG_DEBUG("address: 0x%8.8x, value: 0x%2.2x", address
, *value
);
1228 LOG_DEBUG("address: 0x%8.8x failed", address
);
1234 int target_write_u32(struct target_s
*target
, u32 address
, u32 value
)
1238 if (!target
->type
->examined
)
1240 LOG_ERROR("Target not examined yet");
1244 LOG_DEBUG("address: 0x%8.8x, value: 0x%8.8x", address
, value
);
1246 target_buffer_set_u32(target
, value_buf
, value
);
1247 if ((retval
= target
->type
->write_memory(target
, address
, 4, 1, value_buf
)) != ERROR_OK
)
1249 LOG_DEBUG("failed: %i", retval
);
1255 int target_write_u16(struct target_s
*target
, u32 address
, u16 value
)
1259 if (!target
->type
->examined
)
1261 LOG_ERROR("Target not examined yet");
1265 LOG_DEBUG("address: 0x%8.8x, value: 0x%8.8x", address
, value
);
1267 target_buffer_set_u16(target
, value_buf
, value
);
1268 if ((retval
= target
->type
->write_memory(target
, address
, 2, 1, value_buf
)) != ERROR_OK
)
1270 LOG_DEBUG("failed: %i", retval
);
1276 int target_write_u8(struct target_s
*target
, u32 address
, u8 value
)
1279 if (!target
->type
->examined
)
1281 LOG_ERROR("Target not examined yet");
1285 LOG_DEBUG("address: 0x%8.8x, value: 0x%2.2x", address
, value
);
1287 if ((retval
= target
->type
->write_memory(target
, address
, 1, 1, &value
)) != ERROR_OK
)
1289 LOG_DEBUG("failed: %i", retval
);
1295 int target_register_user_commands(struct command_context_s
*cmd_ctx
)
1297 int retval
= ERROR_OK
;
1300 /* script procedures */
1301 register_command(cmd_ctx
, NULL
, "profile", handle_profile_command
, COMMAND_EXEC
, "profiling samples the CPU PC");
1302 register_jim(cmd_ctx
, "ocd_mem2array", jim_mem2array
, "read memory and return as a TCL array for script processing");
1303 register_jim(cmd_ctx
, "ocd_array2mem", jim_array2mem
, "convert a TCL array to memory locations and write the values");
1305 register_command(cmd_ctx
, NULL
, "fast_load_image", handle_fast_load_image_command
, COMMAND_ANY
,
1306 "same args as load_image, image stored in memory - mainly for profiling purposes");
1308 register_command(cmd_ctx
, NULL
, "fast_load", handle_fast_load_command
, COMMAND_ANY
,
1309 "loads active fast load image to current target - mainly for profiling purposes");
1312 register_command(cmd_ctx
, NULL
, "virt2phys", handle_virt2phys_command
, COMMAND_ANY
, "translate a virtual address into a physical address");
1313 register_command(cmd_ctx
, NULL
, "reg", handle_reg_command
, COMMAND_EXEC
, "display or set a register");
1314 register_command(cmd_ctx
, NULL
, "poll", handle_poll_command
, COMMAND_EXEC
, "poll target state");
1315 register_command(cmd_ctx
, NULL
, "wait_halt", handle_wait_halt_command
, COMMAND_EXEC
, "wait for target halt [time (s)]");
1316 register_command(cmd_ctx
, NULL
, "halt", handle_halt_command
, COMMAND_EXEC
, "halt target");
1317 register_command(cmd_ctx
, NULL
, "resume", handle_resume_command
, COMMAND_EXEC
, "resume target [addr]");
1318 register_command(cmd_ctx
, NULL
, "step", handle_step_command
, COMMAND_EXEC
, "step one instruction from current PC or [addr]");
1319 register_command(cmd_ctx
, NULL
, "reset", handle_reset_command
, COMMAND_EXEC
, "reset target [run|halt|init] - default is run");
1320 register_command(cmd_ctx
, NULL
, "soft_reset_halt", handle_soft_reset_halt_command
, COMMAND_EXEC
, "halt the target and do a soft reset");
1322 register_command(cmd_ctx
, NULL
, "mdw", handle_md_command
, COMMAND_EXEC
, "display memory words <addr> [count]");
1323 register_command(cmd_ctx
, NULL
, "mdh", handle_md_command
, COMMAND_EXEC
, "display memory half-words <addr> [count]");
1324 register_command(cmd_ctx
, NULL
, "mdb", handle_md_command
, COMMAND_EXEC
, "display memory bytes <addr> [count]");
1326 register_command(cmd_ctx
, NULL
, "mww", handle_mw_command
, COMMAND_EXEC
, "write memory word <addr> <value> [count]");
1327 register_command(cmd_ctx
, NULL
, "mwh", handle_mw_command
, COMMAND_EXEC
, "write memory half-word <addr> <value> [count]");
1328 register_command(cmd_ctx
, NULL
, "mwb", handle_mw_command
, COMMAND_EXEC
, "write memory byte <addr> <value> [count]");
1330 register_command(cmd_ctx
, NULL
, "bp", handle_bp_command
, COMMAND_EXEC
, "set breakpoint <address> <length> [hw]");
1331 register_command(cmd_ctx
, NULL
, "rbp", handle_rbp_command
, COMMAND_EXEC
, "remove breakpoint <adress>");
1332 register_command(cmd_ctx
, NULL
, "wp", handle_wp_command
, COMMAND_EXEC
, "set watchpoint <address> <length> <r/w/a> [value] [mask]");
1333 register_command(cmd_ctx
, NULL
, "rwp", handle_rwp_command
, COMMAND_EXEC
, "remove watchpoint <adress>");
1335 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]");
1336 register_command(cmd_ctx
, NULL
, "dump_image", handle_dump_image_command
, COMMAND_EXEC
, "dump_image <file> <address> <size>");
1337 register_command(cmd_ctx
, NULL
, "verify_image", handle_verify_image_command
, COMMAND_EXEC
, "verify_image <file> [offset] [type]");
1338 register_command(cmd_ctx
, NULL
, "test_image", handle_test_image_command
, COMMAND_EXEC
, "test_image <file> [offset] [type]");
1340 if((retval
= target_request_register_commands(cmd_ctx
)) != ERROR_OK
)
1342 if((retval
= trace_register_commands(cmd_ctx
)) != ERROR_OK
)
1348 static int handle_targets_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
1351 target_t
*target
= all_targets
;
1355 /* try as tcltarget name */
1356 for( target
= all_targets
; target
; target
= target
->next
){
1357 if( target
->cmd_name
){
1358 if( 0 == strcmp( args
[0], target
->cmd_name
) ){
1364 /* no match, try as number */
1366 int num
= strtoul(args
[0], &cp
, 0 );
1368 /* then it was not a number */
1369 command_print( cmd_ctx
, "Target: %s unknown, try one of:\n", args
[0] );
1373 target
= get_target_by_num( num
);
1374 if( target
== NULL
){
1375 command_print(cmd_ctx
,"Target: %s is unknown, try one of:\n", args
[0] );
1379 cmd_ctx
->current_target
= target
->target_number
;
1384 target
= all_targets
;
1385 command_print(cmd_ctx
, " CmdName Type Endian AbsChainPos Name State ");
1386 command_print(cmd_ctx
, "-- ---------- ---------- ---------- ----------- ------------- ----------");
1389 /* XX: abcdefghij abcdefghij abcdefghij abcdefghij */
1390 command_print(cmd_ctx
, "%2d: %-10s %-10s %-10s %10d %14s %s",
1391 target
->target_number
,
1394 Jim_Nvp_value2name_simple( nvp_target_endian
, target
->endianness
)->name
,
1395 target
->tap
->abs_chain_position
,
1396 target
->tap
->dotted_name
,
1397 Jim_Nvp_value2name_simple( nvp_target_state
, target
->state
)->name
);
1398 target
= target
->next
;
1404 /* every 300ms we check for reset & powerdropout and issue a "reset halt" if so. */
1406 static int powerDropout
;
1407 static int srstAsserted
;
1409 static int runPowerRestore
;
1410 static int runPowerDropout
;
1411 static int runSrstAsserted
;
1412 static int runSrstDeasserted
;
1414 static int sense_handler(void)
1416 static int prevSrstAsserted
= 0;
1417 static int prevPowerdropout
= 0;
1420 if ((retval
=jtag_power_dropout(&powerDropout
))!=ERROR_OK
)
1424 powerRestored
= prevPowerdropout
&& !powerDropout
;
1427 runPowerRestore
= 1;
1430 long long current
= timeval_ms();
1431 static long long lastPower
= 0;
1432 int waitMore
= lastPower
+ 2000 > current
;
1433 if (powerDropout
&& !waitMore
)
1435 runPowerDropout
= 1;
1436 lastPower
= current
;
1439 if ((retval
=jtag_srst_asserted(&srstAsserted
))!=ERROR_OK
)
1443 srstDeasserted
= prevSrstAsserted
&& !srstAsserted
;
1445 static long long lastSrst
= 0;
1446 waitMore
= lastSrst
+ 2000 > current
;
1447 if (srstDeasserted
&& !waitMore
)
1449 runSrstDeasserted
= 1;
1453 if (!prevSrstAsserted
&& srstAsserted
)
1455 runSrstAsserted
= 1;
1458 prevSrstAsserted
= srstAsserted
;
1459 prevPowerdropout
= powerDropout
;
1461 if (srstDeasserted
|| powerRestored
)
1463 /* Other than logging the event we can't do anything here.
1464 * Issuing a reset is a particularly bad idea as we might
1465 * be inside a reset already.
1472 /* process target state changes */
1473 int handle_target(void *priv
)
1475 int retval
= ERROR_OK
;
1477 /* we do not want to recurse here... */
1478 static int recursive
= 0;
1483 /* danger! running these procedures can trigger srst assertions and power dropouts.
1484 * We need to avoid an infinite loop/recursion here and we do that by
1485 * clearing the flags after running these events.
1487 int did_something
= 0;
1488 if (runSrstAsserted
)
1490 Jim_Eval( interp
, "srst_asserted");
1493 if (runSrstDeasserted
)
1495 Jim_Eval( interp
, "srst_deasserted");
1498 if (runPowerDropout
)
1500 Jim_Eval( interp
, "power_dropout");
1503 if (runPowerRestore
)
1505 Jim_Eval( interp
, "power_restore");
1511 /* clear detect flags */
1515 /* clear action flags */
1518 runSrstDeasserted
=0;
1525 target_t
*target
= all_targets
;
1530 /* only poll target if we've got power and srst isn't asserted */
1531 if (target_continous_poll
&&!powerDropout
&&!srstAsserted
)
1533 /* polling may fail silently until the target has been examined */
1534 if((retval
= target_poll(target
)) != ERROR_OK
)
1538 target
= target
->next
;
1544 static int handle_reg_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
1553 target
= get_current_target(cmd_ctx
);
1555 /* list all available registers for the current target */
1558 reg_cache_t
*cache
= target
->reg_cache
;
1564 for (i
= 0; i
< cache
->num_regs
; i
++)
1566 value
= buf_to_str(cache
->reg_list
[i
].value
, cache
->reg_list
[i
].size
, 16);
1567 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
);
1570 cache
= cache
->next
;
1576 /* access a single register by its ordinal number */
1577 if ((args
[0][0] >= '0') && (args
[0][0] <= '9'))
1579 int num
= strtoul(args
[0], NULL
, 0);
1580 reg_cache_t
*cache
= target
->reg_cache
;
1586 for (i
= 0; i
< cache
->num_regs
; i
++)
1590 reg
= &cache
->reg_list
[i
];
1596 cache
= cache
->next
;
1601 command_print(cmd_ctx
, "%i is out of bounds, the current target has only %i registers (0 - %i)", num
, count
, count
- 1);
1604 } else /* access a single register by its name */
1606 reg
= register_get_by_name(target
->reg_cache
, args
[0], 1);
1610 command_print(cmd_ctx
, "register %s not found in current target", args
[0]);
1615 /* display a register */
1616 if ((argc
== 1) || ((argc
== 2) && !((args
[1][0] >= '0') && (args
[1][0] <= '9'))))
1618 if ((argc
== 2) && (strcmp(args
[1], "force") == 0))
1621 if (reg
->valid
== 0)
1623 reg_arch_type_t
*arch_type
= register_get_arch_type(reg
->arch_type
);
1624 arch_type
->get(reg
);
1626 value
= buf_to_str(reg
->value
, reg
->size
, 16);
1627 command_print(cmd_ctx
, "%s (/%i): 0x%s", reg
->name
, reg
->size
, value
);
1632 /* set register value */
1635 u8
*buf
= malloc(CEIL(reg
->size
, 8));
1636 str_to_buf(args
[1], strlen(args
[1]), buf
, reg
->size
, 0);
1638 reg_arch_type_t
*arch_type
= register_get_arch_type(reg
->arch_type
);
1639 arch_type
->set(reg
, buf
);
1641 value
= buf_to_str(reg
->value
, reg
->size
, 16);
1642 command_print(cmd_ctx
, "%s (/%i): 0x%s", reg
->name
, reg
->size
, value
);
1650 command_print(cmd_ctx
, "usage: reg <#|name> [value]");
1655 static int handle_poll_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
1657 int retval
= ERROR_OK
;
1658 target_t
*target
= get_current_target(cmd_ctx
);
1662 if((retval
= target_poll(target
)) != ERROR_OK
)
1664 if((retval
= target_arch_state(target
)) != ERROR_OK
)
1670 if (strcmp(args
[0], "on") == 0)
1672 target_continous_poll
= 1;
1674 else if (strcmp(args
[0], "off") == 0)
1676 target_continous_poll
= 0;
1680 command_print(cmd_ctx
, "arg is \"on\" or \"off\"");
1684 return ERROR_COMMAND_SYNTAX_ERROR
;
1690 static int handle_wait_halt_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
1698 ms
= strtoul(args
[0], &end
, 0) * 1000;
1701 command_print(cmd_ctx
, "usage: %s [seconds]", cmd
);
1705 target_t
*target
= get_current_target(cmd_ctx
);
1707 return target_wait_state(target
, TARGET_HALTED
, ms
);
1710 /* wait for target state to change. The trick here is to have a low
1711 * latency for short waits and not to suck up all the CPU time
1714 * After 500ms, keep_alive() is invoked
1716 int target_wait_state(target_t
*target
, enum target_state state
, int ms
)
1719 long long then
=0, cur
;
1724 if ((retval
=target_poll(target
))!=ERROR_OK
)
1726 if (target
->state
== state
)
1734 then
= timeval_ms();
1735 LOG_DEBUG("waiting for target %s...",
1736 Jim_Nvp_value2name_simple(nvp_target_state
,state
)->name
);
1746 LOG_ERROR("timed out while waiting for target %s",
1747 Jim_Nvp_value2name_simple(nvp_target_state
,state
)->name
);
1755 static int handle_halt_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
1758 target_t
*target
= get_current_target(cmd_ctx
);
1762 if ((retval
= target_halt(target
)) != ERROR_OK
)
1772 wait
= strtoul(args
[0], &end
, 0);
1777 return handle_wait_halt_command(cmd_ctx
, cmd
, args
, argc
);
1780 static int handle_soft_reset_halt_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
1782 target_t
*target
= get_current_target(cmd_ctx
);
1784 LOG_USER("requesting target halt and executing a soft reset");
1786 target
->type
->soft_reset_halt(target
);
1791 static int handle_reset_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
1794 enum target_reset_mode reset_mode
= RESET_RUN
;
1798 n
= Jim_Nvp_name2value_simple( nvp_reset_modes
, args
[0] );
1799 if( (n
->name
== NULL
) || (n
->value
== RESET_UNKNOWN
) ){
1800 return ERROR_COMMAND_SYNTAX_ERROR
;
1802 reset_mode
= n
->value
;
1805 /* reset *all* targets */
1806 return target_process_reset(cmd_ctx
, reset_mode
);
1810 static int handle_resume_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
1813 target_t
*target
= get_current_target(cmd_ctx
);
1815 target_handle_event( target
, TARGET_EVENT_OLD_pre_resume
);
1818 retval
= target_resume(target
, 1, 0, 1, 0); /* current pc, addr = 0, handle breakpoints, not debugging */
1820 retval
= target_resume(target
, 0, strtoul(args
[0], NULL
, 0), 1, 0); /* addr = args[0], handle breakpoints, not debugging */
1823 retval
= ERROR_COMMAND_SYNTAX_ERROR
;
1829 static int handle_step_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
1831 target_t
*target
= get_current_target(cmd_ctx
);
1836 return target
->type
->step(target
, 1, 0, 1); /* current pc, addr = 0, handle breakpoints */
1839 return target
->type
->step(target
, 0, strtoul(args
[0], NULL
, 0), 1); /* addr = args[0], handle breakpoints */
1844 static int handle_md_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
1846 const int line_bytecnt
= 32;
1859 target_t
*target
= get_current_target(cmd_ctx
);
1865 count
= strtoul(args
[1], NULL
, 0);
1867 address
= strtoul(args
[0], NULL
, 0);
1872 size
= 4; line_modulo
= line_bytecnt
/ 4;
1875 size
= 2; line_modulo
= line_bytecnt
/ 2;
1878 size
= 1; line_modulo
= line_bytecnt
/ 1;
1884 buffer
= calloc(count
, size
);
1885 retval
= target
->type
->read_memory(target
, address
, size
, count
, buffer
);
1886 if (retval
== ERROR_OK
)
1890 for (i
= 0; i
< count
; i
++)
1892 if (i
%line_modulo
== 0)
1893 output_len
+= snprintf(output
+ output_len
, 128 - output_len
, "0x%8.8x: ", address
+ (i
*size
));
1898 output_len
+= snprintf(output
+ output_len
, 128 - output_len
, "%8.8x ", target_buffer_get_u32(target
, &buffer
[i
*4]));
1901 output_len
+= snprintf(output
+ output_len
, 128 - output_len
, "%4.4x ", target_buffer_get_u16(target
, &buffer
[i
*2]));
1904 output_len
+= snprintf(output
+ output_len
, 128 - output_len
, "%2.2x ", buffer
[i
*1]);
1908 if ((i
%line_modulo
== line_modulo
-1) || (i
== count
- 1))
1910 command_print(cmd_ctx
, output
);
1921 static int handle_mw_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
1928 target_t
*target
= get_current_target(cmd_ctx
);
1931 if ((argc
< 2) || (argc
> 3))
1932 return ERROR_COMMAND_SYNTAX_ERROR
;
1934 address
= strtoul(args
[0], NULL
, 0);
1935 value
= strtoul(args
[1], NULL
, 0);
1937 count
= strtoul(args
[2], NULL
, 0);
1943 target_buffer_set_u32(target
, value_buf
, value
);
1947 target_buffer_set_u16(target
, value_buf
, value
);
1951 value_buf
[0] = value
;
1954 return ERROR_COMMAND_SYNTAX_ERROR
;
1956 for (i
=0; i
<count
; i
++)
1962 retval
= target
->type
->write_memory(target
, address
+ i
*wordsize
, 4, 1, value_buf
);
1965 retval
= target
->type
->write_memory(target
, address
+ i
*wordsize
, 2, 1, value_buf
);
1968 retval
= target
->type
->write_memory(target
, address
+ i
*wordsize
, 1, 1, value_buf
);
1975 if (retval
!=ERROR_OK
)
1985 static int handle_load_image_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
1991 u32 max_address
=0xffffffff;
1993 int retval
, retvaltemp
;
1997 duration_t duration
;
1998 char *duration_text
;
2000 target_t
*target
= get_current_target(cmd_ctx
);
2002 if ((argc
< 1)||(argc
> 5))
2004 return ERROR_COMMAND_SYNTAX_ERROR
;
2007 /* a base address isn't always necessary, default to 0x0 (i.e. don't relocate) */
2010 image
.base_address_set
= 1;
2011 image
.base_address
= strtoul(args
[1], NULL
, 0);
2015 image
.base_address_set
= 0;
2019 image
.start_address_set
= 0;
2023 min_address
=strtoul(args
[3], NULL
, 0);
2027 max_address
=strtoul(args
[4], NULL
, 0)+min_address
;
2030 if (min_address
>max_address
)
2032 return ERROR_COMMAND_SYNTAX_ERROR
;
2035 duration_start_measure(&duration
);
2037 if (image_open(&image
, args
[0], (argc
>= 3) ? args
[2] : NULL
) != ERROR_OK
)
2044 for (i
= 0; i
< image
.num_sections
; i
++)
2046 buffer
= malloc(image
.sections
[i
].size
);
2049 command_print(cmd_ctx
, "error allocating buffer for section (%d bytes)", image
.sections
[i
].size
);
2053 if ((retval
= image_read_section(&image
, i
, 0x0, image
.sections
[i
].size
, buffer
, &buf_cnt
)) != ERROR_OK
)
2062 /* DANGER!!! beware of unsigned comparision here!!! */
2064 if ((image
.sections
[i
].base_address
+buf_cnt
>=min_address
)&&
2065 (image
.sections
[i
].base_address
<max_address
))
2067 if (image
.sections
[i
].base_address
<min_address
)
2069 /* clip addresses below */
2070 offset
+=min_address
-image
.sections
[i
].base_address
;
2074 if (image
.sections
[i
].base_address
+buf_cnt
>max_address
)
2076 length
-=(image
.sections
[i
].base_address
+buf_cnt
)-max_address
;
2079 if ((retval
= target_write_buffer(target
, image
.sections
[i
].base_address
+offset
, length
, buffer
+offset
)) != ERROR_OK
)
2084 image_size
+= length
;
2085 command_print(cmd_ctx
, "%u byte written at address 0x%8.8x", length
, image
.sections
[i
].base_address
+offset
);
2091 if((retvaltemp
= duration_stop_measure(&duration
, &duration_text
)) != ERROR_OK
)
2093 image_close(&image
);
2097 if (retval
==ERROR_OK
)
2099 command_print(cmd_ctx
, "downloaded %u byte in %s", image_size
, duration_text
);
2101 free(duration_text
);
2103 image_close(&image
);
2109 static int handle_dump_image_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
2116 int retval
=ERROR_OK
, retvaltemp
;
2118 duration_t duration
;
2119 char *duration_text
;
2121 target_t
*target
= get_current_target(cmd_ctx
);
2125 command_print(cmd_ctx
, "usage: dump_image <filename> <address> <size>");
2129 address
= strtoul(args
[1], NULL
, 0);
2130 size
= strtoul(args
[2], NULL
, 0);
2132 if ((address
& 3) || (size
& 3))
2134 command_print(cmd_ctx
, "only 32-bit aligned address and size are supported");
2138 if (fileio_open(&fileio
, args
[0], FILEIO_WRITE
, FILEIO_BINARY
) != ERROR_OK
)
2143 duration_start_measure(&duration
);
2148 u32 this_run_size
= (size
> 560) ? 560 : size
;
2150 retval
= target
->type
->read_memory(target
, address
, 4, this_run_size
/ 4, buffer
);
2151 if (retval
!= ERROR_OK
)
2156 retval
= fileio_write(&fileio
, this_run_size
, buffer
, &size_written
);
2157 if (retval
!= ERROR_OK
)
2162 size
-= this_run_size
;
2163 address
+= this_run_size
;
2166 if((retvaltemp
= fileio_close(&fileio
)) != ERROR_OK
)
2169 if((retvaltemp
= duration_stop_measure(&duration
, &duration_text
)) != ERROR_OK
)
2172 if (retval
==ERROR_OK
)
2174 command_print(cmd_ctx
, "dumped %"PRIi64
" byte in %s", fileio
.size
, duration_text
);
2176 free(duration_text
);
2181 static int handle_verify_image_command_internal(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
, int verify
)
2187 int retval
, retvaltemp
;
2189 u32 mem_checksum
= 0;
2193 duration_t duration
;
2194 char *duration_text
;
2196 target_t
*target
= get_current_target(cmd_ctx
);
2200 return ERROR_COMMAND_SYNTAX_ERROR
;
2205 LOG_ERROR("no target selected");
2209 duration_start_measure(&duration
);
2213 image
.base_address_set
= 1;
2214 image
.base_address
= strtoul(args
[1], NULL
, 0);
2218 image
.base_address_set
= 0;
2219 image
.base_address
= 0x0;
2222 image
.start_address_set
= 0;
2224 if ((retval
=image_open(&image
, args
[0], (argc
== 3) ? args
[2] : NULL
)) != ERROR_OK
)
2231 for (i
= 0; i
< image
.num_sections
; i
++)
2233 buffer
= malloc(image
.sections
[i
].size
);
2236 command_print(cmd_ctx
, "error allocating buffer for section (%d bytes)", image
.sections
[i
].size
);
2239 if ((retval
= image_read_section(&image
, i
, 0x0, image
.sections
[i
].size
, buffer
, &buf_cnt
)) != ERROR_OK
)
2247 /* calculate checksum of image */
2248 image_calculate_checksum( buffer
, buf_cnt
, &checksum
);
2250 retval
= target_checksum_memory(target
, image
.sections
[i
].base_address
, buf_cnt
, &mem_checksum
);
2251 if( retval
!= ERROR_OK
)
2257 if( checksum
!= mem_checksum
)
2259 /* failed crc checksum, fall back to a binary compare */
2262 command_print(cmd_ctx
, "checksum mismatch - attempting binary compare");
2264 data
= (u8
*)malloc(buf_cnt
);
2266 /* Can we use 32bit word accesses? */
2268 int count
= buf_cnt
;
2269 if ((count
% 4) == 0)
2274 retval
= target
->type
->read_memory(target
, image
.sections
[i
].base_address
, size
, count
, data
);
2275 if (retval
== ERROR_OK
)
2278 for (t
= 0; t
< buf_cnt
; t
++)
2280 if (data
[t
] != buffer
[t
])
2282 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
]);
2299 command_print(cmd_ctx
, "address 0x%08x length 0x%08x", image
.sections
[i
].base_address
, buf_cnt
);
2303 image_size
+= buf_cnt
;
2307 if((retvaltemp
= duration_stop_measure(&duration
, &duration_text
)) != ERROR_OK
)
2309 image_close(&image
);
2313 if (retval
==ERROR_OK
)
2315 command_print(cmd_ctx
, "verified %u bytes in %s", image_size
, duration_text
);
2317 free(duration_text
);
2319 image_close(&image
);
2324 static int handle_verify_image_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
2326 return handle_verify_image_command_internal(cmd_ctx
, cmd
, args
, argc
, 1);
2329 static int handle_test_image_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
2331 return handle_verify_image_command_internal(cmd_ctx
, cmd
, args
, argc
, 0);
2334 static int handle_bp_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
2337 target_t
*target
= get_current_target(cmd_ctx
);
2341 breakpoint_t
*breakpoint
= target
->breakpoints
;
2345 if (breakpoint
->type
== BKPT_SOFT
)
2347 char* buf
= buf_to_str(breakpoint
->orig_instr
, breakpoint
->length
, 16);
2348 command_print(cmd_ctx
, "0x%8.8x, 0x%x, %i, 0x%s", breakpoint
->address
, breakpoint
->length
, breakpoint
->set
, buf
);
2353 command_print(cmd_ctx
, "0x%8.8x, 0x%x, %i", breakpoint
->address
, breakpoint
->length
, breakpoint
->set
);
2355 breakpoint
= breakpoint
->next
;
2363 length
= strtoul(args
[1], NULL
, 0);
2366 if (strcmp(args
[2], "hw") == 0)
2369 if ((retval
= breakpoint_add(target
, strtoul(args
[0], NULL
, 0), length
, hw
)) != ERROR_OK
)
2371 LOG_ERROR("Failure setting breakpoints");
2375 command_print(cmd_ctx
, "breakpoint added at address 0x%8.8x", strtoul(args
[0], NULL
, 0));
2380 command_print(cmd_ctx
, "usage: bp <address> <length> ['hw']");
2386 static int handle_rbp_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
2388 target_t
*target
= get_current_target(cmd_ctx
);
2391 breakpoint_remove(target
, strtoul(args
[0], NULL
, 0));
2396 static int handle_wp_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
2398 target_t
*target
= get_current_target(cmd_ctx
);
2403 watchpoint_t
*watchpoint
= target
->watchpoints
;
2407 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
);
2408 watchpoint
= watchpoint
->next
;
2413 enum watchpoint_rw type
= WPT_ACCESS
;
2414 u32 data_value
= 0x0;
2415 u32 data_mask
= 0xffffffff;
2431 command_print(cmd_ctx
, "usage: wp <address> <length> [r/w/a] [value] [mask]");
2437 data_value
= strtoul(args
[3], NULL
, 0);
2441 data_mask
= strtoul(args
[4], NULL
, 0);
2444 if ((retval
= watchpoint_add(target
, strtoul(args
[0], NULL
, 0),
2445 strtoul(args
[1], NULL
, 0), type
, data_value
, data_mask
)) != ERROR_OK
)
2447 LOG_ERROR("Failure setting breakpoints");
2452 command_print(cmd_ctx
, "usage: wp <address> <length> [r/w/a] [value] [mask]");
2458 static int handle_rwp_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
2460 target_t
*target
= get_current_target(cmd_ctx
);
2463 watchpoint_remove(target
, strtoul(args
[0], NULL
, 0));
2468 static int handle_virt2phys_command(command_context_t
*cmd_ctx
, char *cmd
, char **args
, int argc
)
2471 target_t
*target
= get_current_target(cmd_ctx
);
2477 return ERROR_COMMAND_SYNTAX_ERROR
;
2479 va
= strtoul(args
[0], NULL
, 0);
2481 retval
= target
->type
->virt2phys(target
, va
, &pa
);
2482 if (retval
== ERROR_OK
)
2484 command_print(cmd_ctx
, "Physical address 0x%08x", pa
);
2488 /* lower levels will have logged a detailed error which is
2489 * forwarded to telnet/GDB session.
2495 static void writeData(FILE *f
, const void *data
, size_t len
)
2497 size_t written
= fwrite(data
, len
, 1, f
);
2499 LOG_ERROR("failed to write %zu bytes: %s", len
, strerror(errno
));
2502 static void writeLong(FILE *f
, int l
)
2507 char c
=(l
>>(i
*8))&0xff;
2508 writeData(f
, &c
, 1);
2513 static void writeString(FILE *f
, char *s
)
2515 writeData(f
, s
, strlen(s
));
2518 /* Dump a gmon.out histogram file. */
2519 static void writeGmon(u32
*samples
, u32 sampleNum
, char *filename
)
2522 FILE *f
=fopen(filename
, "w");
2525 writeString(f
, "gmon");
2526 writeLong(f
, 0x00000001); /* Version */
2527 writeLong(f
, 0); /* padding */
2528 writeLong(f
, 0); /* padding */
2529 writeLong(f
, 0); /* padding */
2531 u8 zero
= 0; /* GMON_TAG_TIME_HIST */
2532 writeData(f
, &zero
, 1);
2534 /* figure out bucket size */
2537 for (i
=0; i
<sampleNum
; i
++)
2549 int addressSpace
=(max
-min
+1);
2551 static const u32 maxBuckets
= 256 * 1024; /* maximum buckets. */
2552 u32 length
= addressSpace
;
2553 if (length
> maxBuckets
)
2557 int *buckets
=malloc(sizeof(int)*length
);
2563 memset(buckets
, 0, sizeof(int)*length
);
2564 for (i
=0; i
<sampleNum
;i
++)
2566 u32 address
=samples
[i
];
2567 long long a
=address
-min
;
2568 long long b
=length
-1;
2569 long long c
=addressSpace
-1;
2570 int index
=(a
*b
)/c
; /* danger!!!! int32 overflows */
2574 /* append binary memory gmon.out &profile_hist_hdr ((char*)&profile_hist_hdr + sizeof(struct gmon_hist_hdr)) */
2575 writeLong(f
, min
); /* low_pc */
2576 writeLong(f
, max
); /* high_pc */
2577 writeLong(f
, length
); /* # of samples */
2578 writeLong(f
, 64000000); /* 64MHz */
2579 writeString(f
, "seconds");
2580 for (i
=0; i
<(15-strlen("seconds")); i
++)
2581 writeData(f
, &zero
, 1);
2582 writeString(f
, "s");
2584 /*append binary memory gmon.out profile_hist_data (profile_hist_data + profile_hist_hdr.hist_size) */
2586 char *data
=malloc(2*length
);
2589 for (i
=0; i
<length
;i
++)
2598 data
[i
*2+1]=(val
>>8)&0xff;
2601 writeData(f
, data
, length
* 2);
2611 /* profiling samples the CPU PC as quickly as OpenOCD is able, which will be used as a random sampling of PC */
2612 static int handle_profile_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
2614 target_t
*target
= get_current_target(cmd_ctx
);
2615 struct timeval timeout
, now
;
2617 gettimeofday(&timeout
, NULL
);
2620 return ERROR_COMMAND_SYNTAX_ERROR
;
2623 timeval_add_time(&timeout
, strtoul(args
[0], &end
, 0), 0);
2629 command_print(cmd_ctx
, "Starting profiling. Halting and resuming the target as often as we can...");
2631 static const int maxSample
=10000;
2632 u32
*samples
=malloc(sizeof(u32
)*maxSample
);
2637 int retval
=ERROR_OK
;
2638 /* hopefully it is safe to cache! We want to stop/restart as quickly as possible. */
2639 reg_t
*reg
= register_get_by_name(target
->reg_cache
, "pc", 1);
2643 target_poll(target
);
2644 if (target
->state
== TARGET_HALTED
)
2646 u32 t
=*((u32
*)reg
->value
);
2647 samples
[numSamples
++]=t
;
2648 retval
= target_resume(target
, 1, 0, 0, 0); /* current pc, addr = 0, do not handle breakpoints, not debugging */
2649 target_poll(target
);
2650 alive_sleep(10); /* sleep 10ms, i.e. <100 samples/second. */
2651 } else if (target
->state
== TARGET_RUNNING
)
2653 /* We want to quickly sample the PC. */
2654 if((retval
= target_halt(target
)) != ERROR_OK
)
2661 command_print(cmd_ctx
, "Target not halted or running");
2665 if (retval
!=ERROR_OK
)
2670 gettimeofday(&now
, NULL
);
2671 if ((numSamples
>=maxSample
) || ((now
.tv_sec
>= timeout
.tv_sec
) && (now
.tv_usec
>= timeout
.tv_usec
)))
2673 command_print(cmd_ctx
, "Profiling completed. %d samples.", numSamples
);
2674 if((retval
= target_poll(target
)) != ERROR_OK
)
2679 if (target
->state
== TARGET_HALTED
)
2681 target_resume(target
, 1, 0, 0, 0); /* current pc, addr = 0, do not handle breakpoints, not debugging */
2683 if((retval
= target_poll(target
)) != ERROR_OK
)
2688 writeGmon(samples
, numSamples
, args
[1]);
2689 command_print(cmd_ctx
, "Wrote %s", args
[1]);
2698 static int new_int_array_element(Jim_Interp
* interp
, const char *varname
, int idx
, u32 val
)
2701 Jim_Obj
*nameObjPtr
, *valObjPtr
;
2704 namebuf
= alloc_printf("%s(%d)", varname
, idx
);
2708 nameObjPtr
= Jim_NewStringObj(interp
, namebuf
, -1);
2709 valObjPtr
= Jim_NewIntObj(interp
, val
);
2710 if (!nameObjPtr
|| !valObjPtr
)
2716 Jim_IncrRefCount(nameObjPtr
);
2717 Jim_IncrRefCount(valObjPtr
);
2718 result
= Jim_SetVariable(interp
, nameObjPtr
, valObjPtr
);
2719 Jim_DecrRefCount(interp
, nameObjPtr
);
2720 Jim_DecrRefCount(interp
, valObjPtr
);
2722 /* printf("%s(%d) <= 0%08x\n", varname, idx, val); */
2726 static int jim_mem2array(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
2728 command_context_t
*context
;
2731 context
= Jim_GetAssocData(interp
, "context");
2732 if (context
== NULL
)
2734 LOG_ERROR("mem2array: no command context");
2737 target
= get_current_target(context
);
2740 LOG_ERROR("mem2array: no current target");
2744 return target_mem2array(interp
, target
, argc
,argv
);
2747 static int target_mem2array(Jim_Interp
*interp
, target_t
*target
, int argc
, Jim_Obj
*const *argv
)
2755 const char *varname
;
2760 /* argv[1] = name of array to receive the data
2761 * argv[2] = desired width
2762 * argv[3] = memory address
2763 * argv[4] = count of times to read
2766 Jim_WrongNumArgs(interp
, 1, argv
, "varname width addr nelems");
2769 varname
= Jim_GetString(argv
[1], &len
);
2770 /* given "foo" get space for worse case "foo(%d)" .. add 20 */
2772 e
= Jim_GetLong(interp
, argv
[2], &l
);
2778 e
= Jim_GetLong(interp
, argv
[3], &l
);
2783 e
= Jim_GetLong(interp
, argv
[4], &l
);
2799 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
2800 Jim_AppendStrings( interp
, Jim_GetResult(interp
), "Invalid width param, must be 8/16/32", NULL
);
2804 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
2805 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "mem2array: zero width read?", NULL
);
2808 if ((addr
+ (len
* width
)) < addr
) {
2809 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
2810 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "mem2array: addr + len - wraps to zero?", NULL
);
2813 /* absurd transfer size? */
2815 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
2816 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "mem2array: absurd > 64K item request", NULL
);
2821 ((width
== 2) && ((addr
& 1) == 0)) ||
2822 ((width
== 4) && ((addr
& 3) == 0))) {
2826 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
2827 sprintf(buf
, "mem2array address: 0x%08x is not aligned for %d byte reads", addr
, width
);
2828 Jim_AppendStrings(interp
, Jim_GetResult(interp
), buf
, NULL
);
2839 /* Slurp... in buffer size chunks */
2841 count
= len
; /* in objects.. */
2842 if (count
> (sizeof(buffer
)/width
)) {
2843 count
= (sizeof(buffer
)/width
);
2846 retval
= target
->type
->read_memory( target
, addr
, width
, count
, buffer
);
2847 if (retval
!= ERROR_OK
) {
2849 LOG_ERROR("mem2array: Read @ 0x%08x, w=%d, cnt=%d, failed", addr
, width
, count
);
2850 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
2851 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "mem2array: cannot read memory", NULL
);
2855 v
= 0; /* shut up gcc */
2856 for (i
= 0 ;i
< count
;i
++, n
++) {
2859 v
= target_buffer_get_u32(target
, &buffer
[i
*width
]);
2862 v
= target_buffer_get_u16(target
, &buffer
[i
*width
]);
2865 v
= buffer
[i
] & 0x0ff;
2868 new_int_array_element(interp
, varname
, n
, v
);
2874 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
2879 static int get_int_array_element(Jim_Interp
* interp
, const char *varname
, int idx
, u32
*val
)
2882 Jim_Obj
*nameObjPtr
, *valObjPtr
;
2886 namebuf
= alloc_printf("%s(%d)", varname
, idx
);
2890 nameObjPtr
= Jim_NewStringObj(interp
, namebuf
, -1);
2897 Jim_IncrRefCount(nameObjPtr
);
2898 valObjPtr
= Jim_GetVariable(interp
, nameObjPtr
, JIM_ERRMSG
);
2899 Jim_DecrRefCount(interp
, nameObjPtr
);
2901 if (valObjPtr
== NULL
)
2904 result
= Jim_GetLong(interp
, valObjPtr
, &l
);
2905 /* printf("%s(%d) => 0%08x\n", varname, idx, val); */
2910 static int jim_array2mem(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
2912 command_context_t
*context
;
2915 context
= Jim_GetAssocData(interp
, "context");
2916 if (context
== NULL
){
2917 LOG_ERROR("array2mem: no command context");
2920 target
= get_current_target(context
);
2921 if (target
== NULL
){
2922 LOG_ERROR("array2mem: no current target");
2926 return target_array2mem( interp
,target
, argc
, argv
);
2929 static int target_array2mem(Jim_Interp
*interp
, target_t
*target
, int argc
, Jim_Obj
*const *argv
)
2937 const char *varname
;
2942 /* argv[1] = name of array to get the data
2943 * argv[2] = desired width
2944 * argv[3] = memory address
2945 * argv[4] = count to write
2948 Jim_WrongNumArgs(interp
, 1, argv
, "varname width addr nelems");
2951 varname
= Jim_GetString(argv
[1], &len
);
2952 /* given "foo" get space for worse case "foo(%d)" .. add 20 */
2954 e
= Jim_GetLong(interp
, argv
[2], &l
);
2960 e
= Jim_GetLong(interp
, argv
[3], &l
);
2965 e
= Jim_GetLong(interp
, argv
[4], &l
);
2981 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
2982 Jim_AppendStrings( interp
, Jim_GetResult(interp
), "Invalid width param, must be 8/16/32", NULL
);
2986 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
2987 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "array2mem: zero width read?", NULL
);
2990 if ((addr
+ (len
* width
)) < addr
) {
2991 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
2992 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "array2mem: addr + len - wraps to zero?", NULL
);
2995 /* absurd transfer size? */
2997 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
2998 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "array2mem: absurd > 64K item request", NULL
);
3003 ((width
== 2) && ((addr
& 1) == 0)) ||
3004 ((width
== 4) && ((addr
& 3) == 0))) {
3008 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3009 sprintf(buf
, "array2mem address: 0x%08x is not aligned for %d byte reads", addr
, width
);
3010 Jim_AppendStrings(interp
, Jim_GetResult(interp
), buf
, NULL
);
3021 /* Slurp... in buffer size chunks */
3023 count
= len
; /* in objects.. */
3024 if (count
> (sizeof(buffer
)/width
)) {
3025 count
= (sizeof(buffer
)/width
);
3028 v
= 0; /* shut up gcc */
3029 for (i
= 0 ;i
< count
;i
++, n
++) {
3030 get_int_array_element(interp
, varname
, n
, &v
);
3033 target_buffer_set_u32(target
, &buffer
[i
*width
], v
);
3036 target_buffer_set_u16(target
, &buffer
[i
*width
], v
);
3039 buffer
[i
] = v
& 0x0ff;
3045 retval
= target
->type
->write_memory(target
, addr
, width
, count
, buffer
);
3046 if (retval
!= ERROR_OK
) {
3048 LOG_ERROR("array2mem: Write @ 0x%08x, w=%d, cnt=%d, failed", addr
, width
, count
);
3049 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3050 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "array2mem: cannot read memory", NULL
);
3056 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3061 void target_all_handle_event( enum target_event e
)
3065 LOG_DEBUG( "**all*targets: event: %d, %s",
3067 Jim_Nvp_value2name_simple( nvp_target_event
, e
)->name
);
3069 target
= all_targets
;
3071 target_handle_event( target
, e
);
3072 target
= target
->next
;
3076 void target_handle_event( target_t
*target
, enum target_event e
)
3078 target_event_action_t
*teap
;
3081 teap
= target
->event_action
;
3085 if( teap
->event
== e
){
3087 LOG_DEBUG( "target: (%d) %s (%s) event: %d (%s) action: %s\n",
3088 target
->target_number
,
3092 Jim_Nvp_value2name_simple( nvp_target_event
, e
)->name
,
3093 Jim_GetString( teap
->body
, NULL
) );
3094 if (Jim_EvalObj( interp
, teap
->body
)!=JIM_OK
)
3096 Jim_PrintErrorMessage(interp
);
3102 LOG_DEBUG( "event: %d %s - no action",
3104 Jim_Nvp_value2name_simple( nvp_target_event
, e
)->name
);
3108 enum target_cfg_param
{
3111 TCFG_WORK_AREA_VIRT
,
3112 TCFG_WORK_AREA_PHYS
,
3113 TCFG_WORK_AREA_SIZE
,
3114 TCFG_WORK_AREA_BACKUP
,
3117 TCFG_CHAIN_POSITION
,
3120 static Jim_Nvp nvp_config_opts
[] = {
3121 { .name
= "-type", .value
= TCFG_TYPE
},
3122 { .name
= "-event", .value
= TCFG_EVENT
},
3123 { .name
= "-work-area-virt", .value
= TCFG_WORK_AREA_VIRT
},
3124 { .name
= "-work-area-phys", .value
= TCFG_WORK_AREA_PHYS
},
3125 { .name
= "-work-area-size", .value
= TCFG_WORK_AREA_SIZE
},
3126 { .name
= "-work-area-backup", .value
= TCFG_WORK_AREA_BACKUP
},
3127 { .name
= "-endian" , .value
= TCFG_ENDIAN
},
3128 { .name
= "-variant", .value
= TCFG_VARIANT
},
3129 { .name
= "-chain-position", .value
= TCFG_CHAIN_POSITION
},
3131 { .name
= NULL
, .value
= -1 }
3134 static int target_configure( Jim_GetOptInfo
*goi
, target_t
*target
)
3142 /* parse config or cget options ... */
3143 while( goi
->argc
> 0 ){
3144 Jim_SetEmptyResult( goi
->interp
);
3145 /* Jim_GetOpt_Debug( goi ); */
3147 if( target
->type
->target_jim_configure
){
3148 /* target defines a configure function */
3149 /* target gets first dibs on parameters */
3150 e
= (*(target
->type
->target_jim_configure
))( target
, goi
);
3159 /* otherwise we 'continue' below */
3161 e
= Jim_GetOpt_Nvp( goi
, nvp_config_opts
, &n
);
3163 Jim_GetOpt_NvpUnknown( goi
, nvp_config_opts
, 0 );
3169 if( goi
->isconfigure
){
3170 Jim_SetResult_sprintf( goi
->interp
, "not setable: %s", n
->name
);
3174 if( goi
->argc
!= 0 ){
3175 Jim_WrongNumArgs( goi
->interp
, goi
->argc
, goi
->argv
, "NO PARAMS");
3179 Jim_SetResultString( goi
->interp
, target
->type
->name
, -1 );
3183 if( goi
->argc
== 0 ){
3184 Jim_WrongNumArgs( goi
->interp
, goi
->argc
, goi
->argv
, "-event ?event-name? ...");
3188 e
= Jim_GetOpt_Nvp( goi
, nvp_target_event
, &n
);
3190 Jim_GetOpt_NvpUnknown( goi
, nvp_target_event
, 1 );
3194 if( goi
->isconfigure
){
3195 if( goi
->argc
!= 1 ){
3196 Jim_WrongNumArgs( goi
->interp
, goi
->argc
, goi
->argv
, "-event ?event-name? ?EVENT-BODY?");
3200 if( goi
->argc
!= 0 ){
3201 Jim_WrongNumArgs(goi
->interp
, goi
->argc
, goi
->argv
, "-event ?event-name?");
3207 target_event_action_t
*teap
;
3209 teap
= target
->event_action
;
3210 /* replace existing? */
3212 if( teap
->event
== (enum target_event
)n
->value
){
3218 if( goi
->isconfigure
){
3221 teap
= calloc( 1, sizeof(*teap
) );
3223 teap
->event
= n
->value
;
3224 Jim_GetOpt_Obj( goi
, &o
);
3226 Jim_DecrRefCount( interp
, teap
->body
);
3228 teap
->body
= Jim_DuplicateObj( goi
->interp
, o
);
3231 * Tcl/TK - "tk events" have a nice feature.
3232 * See the "BIND" command.
3233 * We should support that here.
3234 * You can specify %X and %Y in the event code.
3235 * The idea is: %T - target name.
3236 * The idea is: %N - target number
3237 * The idea is: %E - event name.
3239 Jim_IncrRefCount( teap
->body
);
3241 /* add to head of event list */
3242 teap
->next
= target
->event_action
;
3243 target
->event_action
= teap
;
3244 Jim_SetEmptyResult(goi
->interp
);
3248 Jim_SetEmptyResult( goi
->interp
);
3250 Jim_SetResult( goi
->interp
, Jim_DuplicateObj( goi
->interp
, teap
->body
) );
3257 case TCFG_WORK_AREA_VIRT
:
3258 if( goi
->isconfigure
){
3259 target_free_all_working_areas(target
);
3260 e
= Jim_GetOpt_Wide( goi
, &w
);
3264 target
->working_area_virt
= w
;
3266 if( goi
->argc
!= 0 ){
3270 Jim_SetResult( interp
, Jim_NewIntObj( goi
->interp
, target
->working_area_virt
) );
3274 case TCFG_WORK_AREA_PHYS
:
3275 if( goi
->isconfigure
){
3276 target_free_all_working_areas(target
);
3277 e
= Jim_GetOpt_Wide( goi
, &w
);
3281 target
->working_area_phys
= w
;
3283 if( goi
->argc
!= 0 ){
3287 Jim_SetResult( interp
, Jim_NewIntObj( goi
->interp
, target
->working_area_phys
) );
3291 case TCFG_WORK_AREA_SIZE
:
3292 if( goi
->isconfigure
){
3293 target_free_all_working_areas(target
);
3294 e
= Jim_GetOpt_Wide( goi
, &w
);
3298 target
->working_area_size
= w
;
3300 if( goi
->argc
!= 0 ){
3304 Jim_SetResult( interp
, Jim_NewIntObj( goi
->interp
, target
->working_area_size
) );
3308 case TCFG_WORK_AREA_BACKUP
:
3309 if( goi
->isconfigure
){
3310 target_free_all_working_areas(target
);
3311 e
= Jim_GetOpt_Wide( goi
, &w
);
3315 /* make this exactly 1 or 0 */
3316 target
->backup_working_area
= (!!w
);
3318 if( goi
->argc
!= 0 ){
3322 Jim_SetResult( interp
, Jim_NewIntObj( goi
->interp
, target
->working_area_size
) );
3323 /* loop for more e*/
3327 if( goi
->isconfigure
){
3328 e
= Jim_GetOpt_Nvp( goi
, nvp_target_endian
, &n
);
3330 Jim_GetOpt_NvpUnknown( goi
, nvp_target_endian
, 1 );
3333 target
->endianness
= n
->value
;
3335 if( goi
->argc
!= 0 ){
3339 n
= Jim_Nvp_value2name_simple( nvp_target_endian
, target
->endianness
);
3340 if( n
->name
== NULL
){
3341 target
->endianness
= TARGET_LITTLE_ENDIAN
;
3342 n
= Jim_Nvp_value2name_simple( nvp_target_endian
, target
->endianness
);
3344 Jim_SetResultString( goi
->interp
, n
->name
, -1 );
3349 if( goi
->isconfigure
){
3350 if( goi
->argc
< 1 ){
3351 Jim_SetResult_sprintf( goi
->interp
,
3356 if( target
->variant
){
3357 free((void *)(target
->variant
));
3359 e
= Jim_GetOpt_String( goi
, &cp
, NULL
);
3360 target
->variant
= strdup(cp
);
3362 if( goi
->argc
!= 0 ){
3366 Jim_SetResultString( goi
->interp
, target
->variant
,-1 );
3369 case TCFG_CHAIN_POSITION
:
3370 if( goi
->isconfigure
){
3373 target_free_all_working_areas(target
);
3374 e
= Jim_GetOpt_Obj( goi
, &o
);
3378 tap
= jtag_TapByJimObj( goi
->interp
, o
);
3382 /* make this exactly 1 or 0 */
3385 if( goi
->argc
!= 0 ){
3389 Jim_SetResultString( interp
, target
->tap
->dotted_name
, -1 );
3390 /* loop for more e*/
3393 } /* while( goi->argc ) */
3396 /* done - we return */
3400 /** this is the 'tcl' handler for the target specific command */
3401 static int tcl_target_func( Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
3409 struct command_context_s
*cmd_ctx
;
3416 TS_CMD_MWW
, TS_CMD_MWH
, TS_CMD_MWB
,
3417 TS_CMD_MDW
, TS_CMD_MDH
, TS_CMD_MDB
,
3418 TS_CMD_MRW
, TS_CMD_MRH
, TS_CMD_MRB
,
3419 TS_CMD_MEM2ARRAY
, TS_CMD_ARRAY2MEM
,
3427 TS_CMD_INVOKE_EVENT
,
3430 static const Jim_Nvp target_options
[] = {
3431 { .name
= "configure", .value
= TS_CMD_CONFIGURE
},
3432 { .name
= "cget", .value
= TS_CMD_CGET
},
3433 { .name
= "mww", .value
= TS_CMD_MWW
},
3434 { .name
= "mwh", .value
= TS_CMD_MWH
},
3435 { .name
= "mwb", .value
= TS_CMD_MWB
},
3436 { .name
= "mdw", .value
= TS_CMD_MDW
},
3437 { .name
= "mdh", .value
= TS_CMD_MDH
},
3438 { .name
= "mdb", .value
= TS_CMD_MDB
},
3439 { .name
= "mem2array", .value
= TS_CMD_MEM2ARRAY
},
3440 { .name
= "array2mem", .value
= TS_CMD_ARRAY2MEM
},
3441 { .name
= "eventlist", .value
= TS_CMD_EVENTLIST
},
3442 { .name
= "curstate", .value
= TS_CMD_CURSTATE
},
3444 { .name
= "arp_examine", .value
= TS_CMD_EXAMINE
},
3445 { .name
= "arp_poll", .value
= TS_CMD_POLL
},
3446 { .name
= "arp_reset", .value
= TS_CMD_RESET
},
3447 { .name
= "arp_halt", .value
= TS_CMD_HALT
},
3448 { .name
= "arp_waitstate", .value
= TS_CMD_WAITSTATE
},
3449 { .name
= "invoke-event", .value
= TS_CMD_INVOKE_EVENT
},
3451 { .name
= NULL
, .value
= -1 },
3454 /* go past the "command" */
3455 Jim_GetOpt_Setup( &goi
, interp
, argc
-1, argv
+1 );
3457 target
= Jim_CmdPrivData( goi
.interp
);
3458 cmd_ctx
= Jim_GetAssocData(goi
.interp
, "context");
3460 /* commands here are in an NVP table */
3461 e
= Jim_GetOpt_Nvp( &goi
, target_options
, &n
);
3463 Jim_GetOpt_NvpUnknown( &goi
, target_options
, 0 );
3466 /* Assume blank result */
3467 Jim_SetEmptyResult( goi
.interp
);
3470 case TS_CMD_CONFIGURE
:
3472 Jim_WrongNumArgs( goi
.interp
, goi
.argc
, goi
.argv
, "missing: -option VALUE ...");
3475 goi
.isconfigure
= 1;
3476 return target_configure( &goi
, target
);
3478 // some things take params
3480 Jim_WrongNumArgs( goi
.interp
, 0, goi
.argv
, "missing: ?-option?");
3483 goi
.isconfigure
= 0;
3484 return target_configure( &goi
, target
);
3492 * argv[3] = optional count.
3495 if( (goi
.argc
== 3) || (goi
.argc
== 4) ){
3499 Jim_SetResult_sprintf( goi
.interp
, "expected: %s ADDR DATA [COUNT]", n
->name
);
3503 e
= Jim_GetOpt_Wide( &goi
, &a
);
3508 e
= Jim_GetOpt_Wide( &goi
, &b
);
3513 e
= Jim_GetOpt_Wide( &goi
, &c
);
3523 target_buffer_set_u32( target
, target_buf
, b
);
3527 target_buffer_set_u16( target
, target_buf
, b
);
3531 target_buffer_set_u8( target
, target_buf
, b
);
3535 for( x
= 0 ; x
< c
; x
++ ){
3536 e
= target
->type
->write_memory( target
, a
, b
, 1, target_buf
);
3537 if( e
!= ERROR_OK
){
3538 Jim_SetResult_sprintf( interp
, "Error writing @ 0x%08x: %d\n", (int)(a
), e
);
3551 /* argv[0] = command
3553 * argv[2] = optional count
3555 if( (goi
.argc
== 2) || (goi
.argc
== 3) ){
3556 Jim_SetResult_sprintf( goi
.interp
, "expected: %s ADDR [COUNT]", n
->name
);
3559 e
= Jim_GetOpt_Wide( &goi
, &a
);
3564 e
= Jim_GetOpt_Wide( &goi
, &c
);
3571 b
= 1; /* shut up gcc */
3584 /* convert to "bytes" */
3586 /* count is now in 'BYTES' */
3592 e
= target
->type
->read_memory( target
, a
, b
, y
/ b
, target_buf
);
3593 if( e
!= ERROR_OK
){
3594 Jim_SetResult_sprintf( interp
, "error reading target @ 0x%08lx", (int)(a
) );
3598 Jim_fprintf( interp
, interp
->cookie_stdout
, "0x%08x ", (int)(a
) );
3601 for( x
= 0 ; (x
< 16) && (x
< y
) ; x
+= 4 ){
3602 z
= target_buffer_get_u32( target
, &(target_buf
[ x
* 4 ]) );
3603 Jim_fprintf( interp
, interp
->cookie_stdout
, "%08x ", (int)(z
) );
3605 for( ; (x
< 16) ; x
+= 4 ){
3606 Jim_fprintf( interp
, interp
->cookie_stdout
, " " );
3610 for( x
= 0 ; (x
< 16) && (x
< y
) ; x
+= 2 ){
3611 z
= target_buffer_get_u16( target
, &(target_buf
[ x
* 2 ]) );
3612 Jim_fprintf( interp
, interp
->cookie_stdout
, "%04x ", (int)(z
) );
3614 for( ; (x
< 16) ; x
+= 2 ){
3615 Jim_fprintf( interp
, interp
->cookie_stdout
, " " );
3620 for( x
= 0 ; (x
< 16) && (x
< y
) ; x
+= 1 ){
3621 z
= target_buffer_get_u8( target
, &(target_buf
[ x
* 4 ]) );
3622 Jim_fprintf( interp
, interp
->cookie_stdout
, "%02x ", (int)(z
) );
3624 for( ; (x
< 16) ; x
+= 1 ){
3625 Jim_fprintf( interp
, interp
->cookie_stdout
, " " );
3629 /* ascii-ify the bytes */
3630 for( x
= 0 ; x
< y
; x
++ ){
3631 if( (target_buf
[x
] >= 0x20) &&
3632 (target_buf
[x
] <= 0x7e) ){
3636 target_buf
[x
] = '.';
3641 target_buf
[x
] = ' ';
3646 /* print - with a newline */
3647 Jim_fprintf( interp
, interp
->cookie_stdout
, "%s\n", target_buf
);
3653 case TS_CMD_MEM2ARRAY
:
3654 return target_mem2array( goi
.interp
, target
, goi
.argc
, goi
.argv
);
3656 case TS_CMD_ARRAY2MEM
:
3657 return target_array2mem( goi
.interp
, target
, goi
.argc
, goi
.argv
);
3659 case TS_CMD_EXAMINE
:
3661 Jim_WrongNumArgs( goi
.interp
, 2, argv
, "[no parameters]");
3664 e
= target
->type
->examine( target
);
3665 if( e
!= ERROR_OK
){
3666 Jim_SetResult_sprintf( interp
, "examine-fails: %d", e
);
3672 Jim_WrongNumArgs( goi
.interp
, 2, argv
, "[no parameters]");
3675 if( !(target
->type
->examined
) ){
3676 e
= ERROR_TARGET_NOT_EXAMINED
;
3678 e
= target
->type
->poll( target
);
3680 if( e
!= ERROR_OK
){
3681 Jim_SetResult_sprintf( interp
, "poll-fails: %d", e
);
3688 if( goi
.argc
!= 2 ){
3689 Jim_WrongNumArgs( interp
, 2, argv
, "t|f|assert|deassert BOOL");
3692 e
= Jim_GetOpt_Nvp( &goi
, nvp_assert
, &n
);
3694 Jim_GetOpt_NvpUnknown( &goi
, nvp_assert
, 1 );
3697 /* the halt or not param */
3698 e
= Jim_GetOpt_Wide( &goi
, &a
);
3702 /* determine if we should halt or not. */
3703 target
->reset_halt
= !!a
;
3704 /* When this happens - all workareas are invalid. */
3705 target_free_all_working_areas_restore(target
, 0);
3708 if( n
->value
== NVP_ASSERT
){
3709 target
->type
->assert_reset( target
);
3711 target
->type
->deassert_reset( target
);
3716 Jim_WrongNumArgs( goi
.interp
, 0, argv
, "halt [no parameters]");
3719 target
->type
->halt( target
);
3721 case TS_CMD_WAITSTATE
:
3722 /* params: <name> statename timeoutmsecs */
3723 if( goi
.argc
!= 2 ){
3724 Jim_SetResult_sprintf( goi
.interp
, "%s STATENAME TIMEOUTMSECS", n
->name
);
3727 e
= Jim_GetOpt_Nvp( &goi
, nvp_target_state
, &n
);
3729 Jim_GetOpt_NvpUnknown( &goi
, nvp_target_state
,1 );
3732 e
= Jim_GetOpt_Wide( &goi
, &a
);
3736 e
= target_wait_state( target
, n
->value
, a
);
3737 if( e
!= ERROR_OK
){
3738 Jim_SetResult_sprintf( goi
.interp
,
3739 "target: %s wait %s fails (%d) %s",
3742 e
, target_strerror_safe(e
) );
3747 case TS_CMD_EVENTLIST
:
3748 /* List for human, Events defined for this target.
3749 * scripts/programs should use 'name cget -event NAME'
3752 target_event_action_t
*teap
;
3753 teap
= target
->event_action
;
3754 command_print( cmd_ctx
, "Event actions for target (%d) %s\n",
3755 target
->target_number
,
3757 command_print( cmd_ctx
, "%-25s | Body", "Event");
3758 command_print( cmd_ctx
, "------------------------- | ----------------------------------------");
3760 command_print( cmd_ctx
,
3762 Jim_Nvp_value2name_simple( nvp_target_event
, teap
->event
)->name
,
3763 Jim_GetString( teap
->body
, NULL
) );
3766 command_print( cmd_ctx
, "***END***");
3769 case TS_CMD_CURSTATE
:
3770 if( goi
.argc
!= 0 ){
3771 Jim_WrongNumArgs( goi
.interp
, 0, argv
, "[no parameters]");
3774 Jim_SetResultString( goi
.interp
,
3775 Jim_Nvp_value2name_simple(nvp_target_state
,target
->state
)->name
,-1);
3777 case TS_CMD_INVOKE_EVENT
:
3778 if( goi
.argc
!= 1 ){
3779 Jim_SetResult_sprintf( goi
.interp
, "%s ?EVENTNAME?",n
->name
);
3782 e
= Jim_GetOpt_Nvp( &goi
, nvp_target_event
, &n
);
3784 Jim_GetOpt_NvpUnknown( &goi
, nvp_target_event
, 1 );
3787 target_handle_event( target
, n
->value
);
3793 static int target_create( Jim_GetOptInfo
*goi
)
3802 struct command_context_s
*cmd_ctx
;
3804 cmd_ctx
= Jim_GetAssocData(goi
->interp
, "context");
3805 if( goi
->argc
< 3 ){
3806 Jim_WrongNumArgs( goi
->interp
, 1, goi
->argv
, "?name? ?type? ..options...");
3811 Jim_GetOpt_Obj( goi
, &new_cmd
);
3812 /* does this command exist? */
3813 cmd
= Jim_GetCommand( goi
->interp
, new_cmd
, JIM_ERRMSG
);
3815 cp
= Jim_GetString( new_cmd
, NULL
);
3816 Jim_SetResult_sprintf(goi
->interp
, "Command/target: %s Exists", cp
);
3821 e
= Jim_GetOpt_String( goi
, &cp2
, NULL
);
3823 /* now does target type exist */
3824 for( x
= 0 ; target_types
[x
] ; x
++ ){
3825 if( 0 == strcmp( cp
, target_types
[x
]->name
) ){
3830 if( target_types
[x
] == NULL
){
3831 Jim_SetResult_sprintf( goi
->interp
, "Unknown target type %s, try one of ", cp
);
3832 for( x
= 0 ; target_types
[x
] ; x
++ ){
3833 if( target_types
[x
+1] ){
3834 Jim_AppendStrings( goi
->interp
,
3835 Jim_GetResult(goi
->interp
),
3836 target_types
[x
]->name
,
3839 Jim_AppendStrings( goi
->interp
,
3840 Jim_GetResult(goi
->interp
),
3842 target_types
[x
]->name
,NULL
);
3849 target
= calloc(1,sizeof(target_t
));
3850 /* set target number */
3851 target
->target_number
= new_target_number();
3853 /* allocate memory for each unique target type */
3854 target
->type
= (target_type_t
*)calloc(1,sizeof(target_type_t
));
3856 memcpy( target
->type
, target_types
[x
], sizeof(target_type_t
));
3858 /* will be set by "-endian" */
3859 target
->endianness
= TARGET_ENDIAN_UNKNOWN
;
3861 target
->working_area
= 0x0;
3862 target
->working_area_size
= 0x0;
3863 target
->working_areas
= NULL
;
3864 target
->backup_working_area
= 0;
3866 target
->state
= TARGET_UNKNOWN
;
3867 target
->debug_reason
= DBG_REASON_UNDEFINED
;
3868 target
->reg_cache
= NULL
;
3869 target
->breakpoints
= NULL
;
3870 target
->watchpoints
= NULL
;
3871 target
->next
= NULL
;
3872 target
->arch_info
= NULL
;
3874 target
->display
= 1;
3876 /* initialize trace information */
3877 target
->trace_info
= malloc(sizeof(trace_t
));
3878 target
->trace_info
->num_trace_points
= 0;
3879 target
->trace_info
->trace_points_size
= 0;
3880 target
->trace_info
->trace_points
= NULL
;
3881 target
->trace_info
->trace_history_size
= 0;
3882 target
->trace_info
->trace_history
= NULL
;
3883 target
->trace_info
->trace_history_pos
= 0;
3884 target
->trace_info
->trace_history_overflowed
= 0;
3886 target
->dbgmsg
= NULL
;
3887 target
->dbg_msg_enabled
= 0;
3889 target
->endianness
= TARGET_ENDIAN_UNKNOWN
;
3891 /* Do the rest as "configure" options */
3892 goi
->isconfigure
= 1;
3893 e
= target_configure( goi
, target
);
3895 if (target
->tap
== NULL
)
3897 Jim_SetResultString( interp
, "-chain-position required when creating target", -1);
3902 free( target
->type
);
3907 if( target
->endianness
== TARGET_ENDIAN_UNKNOWN
){
3908 /* default endian to little if not specified */
3909 target
->endianness
= TARGET_LITTLE_ENDIAN
;
3912 /* incase variant is not set */
3913 if (!target
->variant
)
3914 target
->variant
= strdup("");
3916 /* create the target specific commands */
3917 if( target
->type
->register_commands
){
3918 (*(target
->type
->register_commands
))( cmd_ctx
);
3920 if( target
->type
->target_create
){
3921 (*(target
->type
->target_create
))( target
, goi
->interp
);
3924 /* append to end of list */
3927 tpp
= &(all_targets
);
3929 tpp
= &( (*tpp
)->next
);
3934 cp
= Jim_GetString( new_cmd
, NULL
);
3935 target
->cmd_name
= strdup(cp
);
3937 /* now - create the new target name command */
3938 e
= Jim_CreateCommand( goi
->interp
,
3941 tcl_target_func
, /* C function */
3942 target
, /* private data */
3943 NULL
); /* no del proc */
3948 static int jim_target( Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
3952 struct command_context_s
*cmd_ctx
;
3956 /* TG = target generic */
3964 const char *target_cmds
[] = {
3965 "create", "types", "names", "current", "number",
3967 NULL
/* terminate */
3970 LOG_DEBUG("Target command params:");
3971 LOG_DEBUG("%s", Jim_Debug_ArgvString(interp
, argc
, argv
));
3973 cmd_ctx
= Jim_GetAssocData( interp
, "context" );
3975 Jim_GetOpt_Setup( &goi
, interp
, argc
-1, argv
+1 );
3977 if( goi
.argc
== 0 ){
3978 Jim_WrongNumArgs(interp
, 1, argv
, "missing: command ...");
3982 /* Jim_GetOpt_Debug( &goi ); */
3983 r
= Jim_GetOpt_Enum( &goi
, target_cmds
, &x
);
3990 Jim_Panic(goi
.interp
,"Why am I here?");
3992 case TG_CMD_CURRENT
:
3993 if( goi
.argc
!= 0 ){
3994 Jim_WrongNumArgs( goi
.interp
, 1, goi
.argv
, "Too many parameters");
3997 Jim_SetResultString( goi
.interp
, get_current_target( cmd_ctx
)->cmd_name
, -1 );
4000 if( goi
.argc
!= 0 ){
4001 Jim_WrongNumArgs( goi
.interp
, 1, goi
.argv
, "Too many parameters" );
4004 Jim_SetResult( goi
.interp
, Jim_NewListObj( goi
.interp
, NULL
, 0 ) );
4005 for( x
= 0 ; target_types
[x
] ; x
++ ){
4006 Jim_ListAppendElement( goi
.interp
,
4007 Jim_GetResult(goi
.interp
),
4008 Jim_NewStringObj( goi
.interp
, target_types
[x
]->name
, -1 ) );
4012 if( goi
.argc
!= 0 ){
4013 Jim_WrongNumArgs( goi
.interp
, 1, goi
.argv
, "Too many parameters" );
4016 Jim_SetResult( goi
.interp
, Jim_NewListObj( goi
.interp
, NULL
, 0 ) );
4017 target
= all_targets
;
4019 Jim_ListAppendElement( goi
.interp
,
4020 Jim_GetResult(goi
.interp
),
4021 Jim_NewStringObj( goi
.interp
, target
->cmd_name
, -1 ) );
4022 target
= target
->next
;
4027 Jim_WrongNumArgs( goi
.interp
, goi
.argc
, goi
.argv
, "?name ... config options ...");
4030 return target_create( &goi
);
4033 if( goi
.argc
!= 1 ){
4034 Jim_SetResult_sprintf( goi
.interp
, "expected: target number ?NUMBER?");
4037 e
= Jim_GetOpt_Wide( &goi
, &w
);
4043 t
= get_target_by_num(w
);
4045 Jim_SetResult_sprintf( goi
.interp
,"Target: number %d does not exist", (int)(w
));
4048 Jim_SetResultString( goi
.interp
, t
->cmd_name
, -1 );
4052 if( goi
.argc
!= 0 ){
4053 Jim_WrongNumArgs( goi
.interp
, 0, goi
.argv
, "<no parameters>");
4056 Jim_SetResult( goi
.interp
,
4057 Jim_NewIntObj( goi
.interp
, max_target_number()));
4073 static int fastload_num
;
4074 static struct FastLoad
*fastload
;
4076 static void free_fastload(void)
4081 for (i
=0; i
<fastload_num
; i
++)
4083 if (fastload
[i
].data
)
4084 free(fastload
[i
].data
);
4094 static int handle_fast_load_image_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
4100 u32 max_address
=0xffffffff;
4106 duration_t duration
;
4107 char *duration_text
;
4109 if ((argc
< 1)||(argc
> 5))
4111 return ERROR_COMMAND_SYNTAX_ERROR
;
4114 /* a base address isn't always necessary, default to 0x0 (i.e. don't relocate) */
4117 image
.base_address_set
= 1;
4118 image
.base_address
= strtoul(args
[1], NULL
, 0);
4122 image
.base_address_set
= 0;
4126 image
.start_address_set
= 0;
4130 min_address
=strtoul(args
[3], NULL
, 0);
4134 max_address
=strtoul(args
[4], NULL
, 0)+min_address
;
4137 if (min_address
>max_address
)
4139 return ERROR_COMMAND_SYNTAX_ERROR
;
4142 duration_start_measure(&duration
);
4144 if (image_open(&image
, args
[0], (argc
>= 3) ? args
[2] : NULL
) != ERROR_OK
)
4151 fastload_num
=image
.num_sections
;
4152 fastload
=(struct FastLoad
*)malloc(sizeof(struct FastLoad
)*image
.num_sections
);
4155 image_close(&image
);
4158 memset(fastload
, 0, sizeof(struct FastLoad
)*image
.num_sections
);
4159 for (i
= 0; i
< image
.num_sections
; i
++)
4161 buffer
= malloc(image
.sections
[i
].size
);
4164 command_print(cmd_ctx
, "error allocating buffer for section (%d bytes)", image
.sections
[i
].size
);
4168 if ((retval
= image_read_section(&image
, i
, 0x0, image
.sections
[i
].size
, buffer
, &buf_cnt
)) != ERROR_OK
)
4178 /* DANGER!!! beware of unsigned comparision here!!! */
4180 if ((image
.sections
[i
].base_address
+buf_cnt
>=min_address
)&&
4181 (image
.sections
[i
].base_address
<max_address
))
4183 if (image
.sections
[i
].base_address
<min_address
)
4185 /* clip addresses below */
4186 offset
+=min_address
-image
.sections
[i
].base_address
;
4190 if (image
.sections
[i
].base_address
+buf_cnt
>max_address
)
4192 length
-=(image
.sections
[i
].base_address
+buf_cnt
)-max_address
;
4195 fastload
[i
].address
=image
.sections
[i
].base_address
+offset
;
4196 fastload
[i
].data
=malloc(length
);
4197 if (fastload
[i
].data
==NULL
)
4202 memcpy(fastload
[i
].data
, buffer
+offset
, length
);
4203 fastload
[i
].length
=length
;
4205 image_size
+= length
;
4206 command_print(cmd_ctx
, "%u byte written at address 0x%8.8x", length
, image
.sections
[i
].base_address
+offset
);
4212 duration_stop_measure(&duration
, &duration_text
);
4213 if (retval
==ERROR_OK
)
4215 command_print(cmd_ctx
, "Loaded %u bytes in %s", image_size
, duration_text
);
4216 command_print(cmd_ctx
, "NB!!! image has not been loaded to target, issue a subsequent 'fast_load' to do so.");
4218 free(duration_text
);
4220 image_close(&image
);
4222 if (retval
!=ERROR_OK
)
4230 static int handle_fast_load_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
4233 return ERROR_COMMAND_SYNTAX_ERROR
;
4236 LOG_ERROR("No image in memory");
4240 int ms
=timeval_ms();
4242 int retval
=ERROR_OK
;
4243 for (i
=0; i
<fastload_num
;i
++)
4245 target_t
*target
= get_current_target(cmd_ctx
);
4246 command_print(cmd_ctx
, "Write to 0x%08x, length 0x%08x", fastload
[i
].address
, fastload
[i
].length
);
4247 if (retval
==ERROR_OK
)
4249 retval
= target_write_buffer(target
, fastload
[i
].address
, fastload
[i
].length
, fastload
[i
].data
);
4251 size
+=fastload
[i
].length
;
4253 int after
=timeval_ms();
4254 command_print(cmd_ctx
, "Loaded image %f kBytes/s", (float)(size
/1024.0)/((float)(after
-ms
)/1000.0));
Linking to existing account procedure
If you already have an account and want to add another login method
you
MUST first sign in with your existing account and
then change URL to read
https://review.openocd.org/login/?link
to get to this page again but this time it'll work for linking. Thank you.
SSH host keys fingerprints
1024 SHA256:YKx8b7u5ZWdcbp7/4AeXNaqElP49m6QrwfXaqQGJAOk gerrit-code-review@openocd.zylin.com (DSA)
384 SHA256:jHIbSQa4REvwCFG4cq5LBlBLxmxSqelQPem/EXIrxjk gerrit-code-review@openocd.org (ECDSA)
521 SHA256:UAOPYkU9Fjtcao0Ul/Rrlnj/OsQvt+pgdYSZ4jOYdgs gerrit-code-review@openocd.org (ECDSA)
256 SHA256:A13M5QlnozFOvTllybRZH6vm7iSt0XLxbA48yfc2yfY gerrit-code-review@openocd.org (ECDSA)
256 SHA256:spYMBqEYoAOtK7yZBrcwE8ZpYt6b68Cfh9yEVetvbXg gerrit-code-review@openocd.org (ED25519)
+--[ED25519 256]--+
|=.. |
|+o.. . |
|*.o . . |
|+B . . . |
|Bo. = o S |
|Oo.+ + = |
|oB=.* = . o |
| =+=.+ + E |
|. .=o . o |
+----[SHA256]-----+
2048 SHA256:0Onrb7/PHjpo6iVZ7xQX2riKN83FJ3KGU0TvI0TaFG4 gerrit-code-review@openocd.zylin.com (RSA)