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 * This program is free software; you can redistribute it and/or modify *
9 * it under the terms of the GNU General Public License as published by *
10 * the Free Software Foundation; either version 2 of the License, or *
11 * (at your option) any later version. *
13 * This program is distributed in the hope that it will be useful, *
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
16 * GNU General Public License for more details. *
18 * You should have received a copy of the GNU General Public License *
19 * along with this program; if not, write to the *
20 * Free Software Foundation, Inc., *
21 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
22 ***************************************************************************/
27 #include "replacements.h"
29 #include "target_request.h"
32 #include "configuration.h"
33 #include "binarybuffer.h"
40 #include <sys/types.h>
48 #include <time_support.h>
53 int cli_target_callback_event_handler(struct target_s
*target
, enum target_event event
, void *priv
);
55 int handle_target_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
);
56 int handle_targets_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
);
58 int handle_working_area_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
);
60 int handle_reg_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
);
61 int handle_poll_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
);
62 int handle_halt_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
);
63 int handle_wait_halt_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
);
64 int handle_reset_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
);
65 int handle_soft_reset_halt_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
);
66 int handle_resume_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
);
67 int handle_step_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
);
68 int handle_md_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
);
69 int handle_mw_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
);
70 int handle_load_image_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
);
71 int handle_dump_image_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
);
72 int handle_verify_image_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
);
73 int handle_bp_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
);
74 int handle_rbp_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
);
75 int handle_wp_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
);
76 int handle_rwp_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
);
77 int handle_virt2phys_command(command_context_t
*cmd_ctx
, char *cmd
, char **args
, int argc
);
78 int handle_profile_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
);
79 static int jim_array2mem(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
);
80 static int jim_mem2array(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
);
83 static int target_array2mem(Jim_Interp
*interp
, target_t
*target
, int argc
, Jim_Obj
*const *argv
);
84 static int target_mem2array(Jim_Interp
*interp
, target_t
*target
, int argc
, Jim_Obj
*const *argv
);
89 extern target_type_t arm7tdmi_target
;
90 extern target_type_t arm720t_target
;
91 extern target_type_t arm9tdmi_target
;
92 extern target_type_t arm920t_target
;
93 extern target_type_t arm966e_target
;
94 extern target_type_t arm926ejs_target
;
95 extern target_type_t feroceon_target
;
96 extern target_type_t xscale_target
;
97 extern target_type_t cortexm3_target
;
98 extern target_type_t arm11_target
;
99 extern target_type_t mips_m4k_target
;
101 target_type_t
*target_types
[] =
117 target_t
*all_targets
= NULL
;
118 target_event_callback_t
*target_event_callbacks
= NULL
;
119 target_timer_callback_t
*target_timer_callbacks
= NULL
;
121 const Jim_Nvp nvp_assert
[] = {
122 { .name
= "assert", NVP_ASSERT
},
123 { .name
= "deassert", NVP_DEASSERT
},
124 { .name
= "T", NVP_ASSERT
},
125 { .name
= "F", NVP_DEASSERT
},
126 { .name
= "t", NVP_ASSERT
},
127 { .name
= "f", NVP_DEASSERT
},
128 { .name
= NULL
, .value
= -1 }
131 const Jim_Nvp nvp_target_event
[] = {
132 { .value
= TARGET_EVENT_HALTED
, .name
= "halted" },
133 { .value
= TARGET_EVENT_RESUMED
, .name
= "resumed" },
134 { .value
= TARGET_EVENT_RESUME_START
, .name
= "resume-start" },
135 { .value
= TARGET_EVENT_RESUME_END
, .name
= "resume-end" },
137 /* historical name */
138 { .value
= TARGET_EVENT_RESET_START
, .name
= "pre_reset" },
139 { .value
= TARGET_EVENT_RESET_START
, .name
= "reset-start" },
140 /* historical name */
141 { .value
= TARGET_EVENT_RESET
, .name
= "reset" },
142 { .value
= TARGET_EVENT_RESET_INIT
, .name
= "reset-init" },
143 { .value
= TARGET_EVENT_RESET_END
, .name
= "reset-end" },
145 { .value
= TARGET_EVENT_DEBUG_HALTED
, .name
= "debug-halted" },
146 { .value
= TARGET_EVENT_DEBUG_RESUMED
, .name
= "debug-resumed" },
148 { .value
= TARGET_EVENT_GDB_ATTACH
, .name
= "gdb-attach" },
149 { .value
= TARGET_EVENT_GDB_DETACH
, .name
= "gdb-detach" },
151 { .value
= TARGET_EVENT_GDB_FLASH_WRITE_START
, .name
= "gdb-flash-write-start" },
152 { .value
= TARGET_EVENT_GDB_FLASH_WRITE_END
, .name
= "gdb-flash-write-end" },
154 { .value
= TARGET_EVENT_GDB_FLASH_ERASE_START
, .name
= "gdb_program_config" },
156 { .value
= TARGET_EVENT_GDB_FLASH_ERASE_START
, .name
= "gdb-flash-erase-start" },
157 { .value
= TARGET_EVENT_GDB_FLASH_ERASE_END
, .name
= "gdb-flash-erase-end" },
159 { .value
= TARGET_EVENT_RESUME_START
, .name
= "resume-start" },
160 { .value
= TARGET_EVENT_RESUMED
, .name
= "resume-ok" },
161 { .value
= TARGET_EVENT_RESUME_END
, .name
= "resume-end" },
163 { .name
= NULL
, .value
= -1 }
166 const Jim_Nvp nvp_target_state
[] = {
167 { .name
= "unknown", .value
= TARGET_UNKNOWN
},
168 { .name
= "running", .value
= TARGET_RUNNING
},
169 { .name
= "halted", .value
= TARGET_HALTED
},
170 { .name
= "reset", .value
= TARGET_RESET
},
171 { .name
= "debug-running", .value
= TARGET_DEBUG_RUNNING
},
172 { .name
= NULL
, .value
= -1 },
176 const Jim_Nvp nvp_target_debug_reason
[] = {
177 { .name
= "debug-request" , .value
= DBG_REASON_DBGRQ
},
178 { .name
= "breakpoint" , .value
= DBG_REASON_BREAKPOINT
},
179 { .name
= "watchpoint" , .value
= DBG_REASON_WATCHPOINT
},
180 { .name
= "watchpoint-and-breakpoint", .value
= DBG_REASON_WPTANDBKPT
},
181 { .name
= "single-step" , .value
= DBG_REASON_SINGLESTEP
},
182 { .name
= "target-not-halted" , .value
= DBG_REASON_NOTHALTED
},
183 { .name
= "undefined" , .value
= DBG_REASON_UNDEFINED
},
184 { .name
= NULL
, .value
= -1 },
188 const Jim_Nvp nvp_target_endian
[] = {
189 { .name
= "big", .value
= TARGET_BIG_ENDIAN
},
190 { .name
= "little", .value
= TARGET_LITTLE_ENDIAN
},
191 { .name
= "be", .value
= TARGET_BIG_ENDIAN
},
192 { .name
= "le", .value
= TARGET_LITTLE_ENDIAN
},
193 { .name
= NULL
, .value
= -1 },
197 /* determine the number of the new target */
199 new_target_number( void )
204 /* number is 0 based */
208 if( x
< t
->target_number
){
209 x
= t
->target_number
;
216 static int target_continous_poll
= 1;
218 /* read a u32 from a buffer in target memory endianness */
219 u32
target_buffer_get_u32(target_t
*target
, u8
*buffer
)
221 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
222 return le_to_h_u32(buffer
);
224 return be_to_h_u32(buffer
);
227 /* read a u16 from a buffer in target memory endianness */
228 u16
target_buffer_get_u16(target_t
*target
, u8
*buffer
)
230 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
231 return le_to_h_u16(buffer
);
233 return be_to_h_u16(buffer
);
236 /* read a u8 from a buffer in target memory endianness */
237 u8
target_buffer_get_u8(target_t
*target
, u8
*buffer
)
239 return *buffer
& 0x0ff;
242 /* write a u32 to a buffer in target memory endianness */
243 void target_buffer_set_u32(target_t
*target
, u8
*buffer
, u32 value
)
245 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
246 h_u32_to_le(buffer
, value
);
248 h_u32_to_be(buffer
, value
);
251 /* write a u16 to a buffer in target memory endianness */
252 void target_buffer_set_u16(target_t
*target
, u8
*buffer
, u16 value
)
254 if (target
->endianness
== TARGET_LITTLE_ENDIAN
)
255 h_u16_to_le(buffer
, value
);
257 h_u16_to_be(buffer
, value
);
260 /* write a u8 to a buffer in target memory endianness */
261 void target_buffer_set_u8(target_t
*target
, u8
*buffer
, u8 value
)
266 /* returns a pointer to the n-th configured target */
267 target_t
* get_target_by_num(int num
)
269 target_t
*target
= all_targets
;
272 if( target
->target_number
== num
){
275 target
= target
->next
;
281 int get_num_by_target(target_t
*query_target
)
283 return query_target
->target_number
;
286 target_t
* get_current_target(command_context_t
*cmd_ctx
)
288 target_t
*target
= get_target_by_num(cmd_ctx
->current_target
);
292 LOG_ERROR("BUG: current_target out of bounds");
300 int target_poll(struct target_s
*target
)
302 /* We can't poll until after examine */
303 if (!target
->type
->examined
)
305 /* Fail silently lest we pollute the log */
308 return target
->type
->poll(target
);
311 int target_halt(struct target_s
*target
)
313 /* We can't poll until after examine */
314 if (!target
->type
->examined
)
316 LOG_ERROR("Target not examined yet");
319 return target
->type
->halt(target
);
322 int target_resume(struct target_s
*target
, int current
, u32 address
, int handle_breakpoints
, int debug_execution
)
326 /* We can't poll until after examine */
327 if (!target
->type
->examined
)
329 LOG_ERROR("Target not examined yet");
333 /* note that resume *must* be asynchronous. The CPU can halt before we poll. The CPU can
334 * even halt at the current PC as a result of a software breakpoint being inserted by (a bug?)
337 if ((retval
= target
->type
->resume(target
, current
, address
, handle_breakpoints
, debug_execution
)) != ERROR_OK
)
343 int target_process_reset(struct command_context_s
*cmd_ctx
, enum target_reset_mode reset_mode
)
345 int retval
= ERROR_OK
;
348 target
= all_targets
;
351 target_invoke_script(cmd_ctx
, target
, "pre_reset");
352 target
= target
->next
;
355 if ((retval
= jtag_init_reset(cmd_ctx
)) != ERROR_OK
)
358 keep_alive(); /* we might be running on a very slow JTAG clk */
360 /* First time this is executed after launching OpenOCD, it will read out
361 * the type of CPU, etc. and init Embedded ICE registers in host
364 * It will also set up ICE registers in the target.
366 * However, if we assert TRST later, we need to set up the registers again.
368 * For the "reset halt/init" case we must only set up the registers here.
370 if ((retval
= target_examine()) != ERROR_OK
)
373 keep_alive(); /* we might be running on a very slow JTAG clk */
375 target
= all_targets
;
378 /* we have no idea what state the target is in, so we
379 * have to drop working areas
381 target_free_all_working_areas_restore(target
, 0);
382 target
->reset_halt
=((reset_mode
==RESET_HALT
)||(reset_mode
==RESET_INIT
));
383 if ((retval
= target
->type
->assert_reset(target
))!=ERROR_OK
)
385 target
= target
->next
;
388 target
= all_targets
;
391 if ((retval
= target
->type
->deassert_reset(target
))!=ERROR_OK
)
393 target
= target
->next
;
396 target
= all_targets
;
399 /* We can fail to bring the target into the halted state, try after reset has been deasserted */
400 if (target
->reset_halt
)
402 /* wait up to 1 second for halt. */
403 target_wait_state(target
, TARGET_HALTED
, 1000);
404 if (target
->state
!= TARGET_HALTED
)
406 LOG_WARNING("Failed to reset target into halted mode - issuing halt");
407 if ((retval
= target
->type
->halt(target
))!=ERROR_OK
)
412 target
= target
->next
;
416 LOG_DEBUG("Waiting for halted stated as appropriate");
418 if ((reset_mode
== RESET_HALT
) || (reset_mode
== RESET_INIT
))
420 target
= all_targets
;
423 /* Wait for reset to complete, maximum 5 seconds. */
424 if (((retval
=target_wait_state(target
, TARGET_HALTED
, 5000)))==ERROR_OK
)
426 if (reset_mode
== RESET_INIT
)
427 target_invoke_script(cmd_ctx
, target
, "post_reset");
430 target
= target
->next
;
434 /* We want any events to be processed before the prompt */
435 target_call_timer_callbacks_now();
440 static int default_virt2phys(struct target_s
*target
, u32
virtual, u32
*physical
)
446 static int default_mmu(struct target_s
*target
, int *enabled
)
452 static int default_examine(struct target_s
*target
)
454 target
->type
->examined
= 1;
459 /* Targets that correctly implement init+examine, i.e.
460 * no communication with target during init:
464 int target_examine(void)
466 int retval
= ERROR_OK
;
467 target_t
*target
= all_targets
;
470 if ((retval
= target
->type
->examine(target
))!=ERROR_OK
)
472 target
= target
->next
;
477 static int target_write_memory_imp(struct target_s
*target
, u32 address
, u32 size
, u32 count
, u8
*buffer
)
479 if (!target
->type
->examined
)
481 LOG_ERROR("Target not examined yet");
484 return target
->type
->write_memory_imp(target
, address
, size
, count
, buffer
);
487 static int target_read_memory_imp(struct target_s
*target
, u32 address
, u32 size
, u32 count
, u8
*buffer
)
489 if (!target
->type
->examined
)
491 LOG_ERROR("Target not examined yet");
494 return target
->type
->read_memory_imp(target
, address
, size
, count
, buffer
);
497 static int target_soft_reset_halt_imp(struct target_s
*target
)
499 if (!target
->type
->examined
)
501 LOG_ERROR("Target not examined yet");
504 return target
->type
->soft_reset_halt_imp(target
);
507 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
)
509 if (!target
->type
->examined
)
511 LOG_ERROR("Target not examined yet");
514 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
);
517 int target_init(struct command_context_s
*cmd_ctx
)
519 target_t
*target
= all_targets
;
523 target
->type
->examined
= 0;
524 if (target
->type
->examine
== NULL
)
526 target
->type
->examine
= default_examine
;
529 if (target
->type
->init_target(cmd_ctx
, target
) != ERROR_OK
)
531 LOG_ERROR("target '%s' init failed", target
->type
->name
);
535 /* Set up default functions if none are provided by target */
536 if (target
->type
->virt2phys
== NULL
)
538 target
->type
->virt2phys
= default_virt2phys
;
540 target
->type
->virt2phys
= default_virt2phys
;
541 /* a non-invasive way(in terms of patches) to add some code that
542 * runs before the type->write/read_memory implementation
544 target
->type
->write_memory_imp
= target
->type
->write_memory
;
545 target
->type
->write_memory
= target_write_memory_imp
;
546 target
->type
->read_memory_imp
= target
->type
->read_memory
;
547 target
->type
->read_memory
= target_read_memory_imp
;
548 target
->type
->soft_reset_halt_imp
= target
->type
->soft_reset_halt
;
549 target
->type
->soft_reset_halt
= target_soft_reset_halt_imp
;
550 target
->type
->run_algorithm_imp
= target
->type
->run_algorithm
;
551 target
->type
->run_algorithm
= target_run_algorithm_imp
;
554 if (target
->type
->mmu
== NULL
)
556 target
->type
->mmu
= default_mmu
;
558 target
= target
->next
;
563 target_register_user_commands(cmd_ctx
);
564 target_register_timer_callback(handle_target
, 100, 1, NULL
);
570 int target_register_event_callback(int (*callback
)(struct target_s
*target
, enum target_event event
, void *priv
), void *priv
)
572 target_event_callback_t
**callbacks_p
= &target_event_callbacks
;
574 if (callback
== NULL
)
576 return ERROR_INVALID_ARGUMENTS
;
581 while ((*callbacks_p
)->next
)
582 callbacks_p
= &((*callbacks_p
)->next
);
583 callbacks_p
= &((*callbacks_p
)->next
);
586 (*callbacks_p
) = malloc(sizeof(target_event_callback_t
));
587 (*callbacks_p
)->callback
= callback
;
588 (*callbacks_p
)->priv
= priv
;
589 (*callbacks_p
)->next
= NULL
;
594 int target_register_timer_callback(int (*callback
)(void *priv
), int time_ms
, int periodic
, void *priv
)
596 target_timer_callback_t
**callbacks_p
= &target_timer_callbacks
;
599 if (callback
== NULL
)
601 return ERROR_INVALID_ARGUMENTS
;
606 while ((*callbacks_p
)->next
)
607 callbacks_p
= &((*callbacks_p
)->next
);
608 callbacks_p
= &((*callbacks_p
)->next
);
611 (*callbacks_p
) = malloc(sizeof(target_timer_callback_t
));
612 (*callbacks_p
)->callback
= callback
;
613 (*callbacks_p
)->periodic
= periodic
;
614 (*callbacks_p
)->time_ms
= time_ms
;
616 gettimeofday(&now
, NULL
);
617 (*callbacks_p
)->when
.tv_usec
= now
.tv_usec
+ (time_ms
% 1000) * 1000;
618 time_ms
-= (time_ms
% 1000);
619 (*callbacks_p
)->when
.tv_sec
= now
.tv_sec
+ (time_ms
/ 1000);
620 if ((*callbacks_p
)->when
.tv_usec
> 1000000)
622 (*callbacks_p
)->when
.tv_usec
= (*callbacks_p
)->when
.tv_usec
- 1000000;
623 (*callbacks_p
)->when
.tv_sec
+= 1;
626 (*callbacks_p
)->priv
= priv
;
627 (*callbacks_p
)->next
= NULL
;
632 int target_unregister_event_callback(int (*callback
)(struct target_s
*target
, enum target_event event
, void *priv
), void *priv
)
634 target_event_callback_t
**p
= &target_event_callbacks
;
635 target_event_callback_t
*c
= target_event_callbacks
;
637 if (callback
== NULL
)
639 return ERROR_INVALID_ARGUMENTS
;
644 target_event_callback_t
*next
= c
->next
;
645 if ((c
->callback
== callback
) && (c
->priv
== priv
))
659 int target_unregister_timer_callback(int (*callback
)(void *priv
), void *priv
)
661 target_timer_callback_t
**p
= &target_timer_callbacks
;
662 target_timer_callback_t
*c
= target_timer_callbacks
;
664 if (callback
== NULL
)
666 return ERROR_INVALID_ARGUMENTS
;
671 target_timer_callback_t
*next
= c
->next
;
672 if ((c
->callback
== callback
) && (c
->priv
== priv
))
686 int target_call_event_callbacks(target_t
*target
, enum target_event event
)
688 target_event_callback_t
*callback
= target_event_callbacks
;
689 target_event_callback_t
*next_callback
;
691 LOG_DEBUG("target event %i", event
);
696 next_callback
= callback
->next
;
697 callback
->callback(target
, event
, callback
->priv
);
698 callback
= next_callback
;
704 static int target_call_timer_callbacks_check_time(int checktime
)
706 target_timer_callback_t
*callback
= target_timer_callbacks
;
707 target_timer_callback_t
*next_callback
;
712 gettimeofday(&now
, NULL
);
716 next_callback
= callback
->next
;
718 if ((!checktime
&&callback
->periodic
)||
719 (((now
.tv_sec
>= callback
->when
.tv_sec
) && (now
.tv_usec
>= callback
->when
.tv_usec
))
720 || (now
.tv_sec
> callback
->when
.tv_sec
)))
722 if(callback
->callback
!= NULL
)
724 callback
->callback(callback
->priv
);
725 if (callback
->periodic
)
727 int time_ms
= callback
->time_ms
;
728 callback
->when
.tv_usec
= now
.tv_usec
+ (time_ms
% 1000) * 1000;
729 time_ms
-= (time_ms
% 1000);
730 callback
->when
.tv_sec
= now
.tv_sec
+ time_ms
/ 1000;
731 if (callback
->when
.tv_usec
> 1000000)
733 callback
->when
.tv_usec
= callback
->when
.tv_usec
- 1000000;
734 callback
->when
.tv_sec
+= 1;
738 target_unregister_timer_callback(callback
->callback
, callback
->priv
);
742 callback
= next_callback
;
748 int target_call_timer_callbacks(void)
750 return target_call_timer_callbacks_check_time(1);
753 /* invoke periodic callbacks immediately */
754 int target_call_timer_callbacks_now(void)
756 return target_call_timer_callbacks();
759 int target_alloc_working_area(struct target_s
*target
, u32 size
, working_area_t
**area
)
761 working_area_t
*c
= target
->working_areas
;
762 working_area_t
*new_wa
= NULL
;
764 /* Reevaluate working area address based on MMU state*/
765 if (target
->working_areas
== NULL
)
769 retval
= target
->type
->mmu(target
, &enabled
);
770 if (retval
!= ERROR_OK
)
776 target
->working_area
= target
->working_area_virt
;
780 target
->working_area
= target
->working_area_phys
;
784 /* only allocate multiples of 4 byte */
787 LOG_ERROR("BUG: code tried to allocate unaligned number of bytes, padding");
788 size
= CEIL(size
, 4);
791 /* see if there's already a matching working area */
794 if ((c
->free
) && (c
->size
== size
))
802 /* if not, allocate a new one */
805 working_area_t
**p
= &target
->working_areas
;
806 u32 first_free
= target
->working_area
;
807 u32 free_size
= target
->working_area_size
;
809 LOG_DEBUG("allocating new working area");
811 c
= target
->working_areas
;
814 first_free
+= c
->size
;
815 free_size
-= c
->size
;
820 if (free_size
< size
)
822 LOG_WARNING("not enough working area available(requested %d, free %d)", size
, free_size
);
823 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
826 new_wa
= malloc(sizeof(working_area_t
));
829 new_wa
->address
= first_free
;
831 if (target
->backup_working_area
)
833 new_wa
->backup
= malloc(new_wa
->size
);
834 target
->type
->read_memory(target
, new_wa
->address
, 4, new_wa
->size
/ 4, new_wa
->backup
);
838 new_wa
->backup
= NULL
;
841 /* put new entry in list */
845 /* mark as used, and return the new (reused) area */
855 int target_free_working_area_restore(struct target_s
*target
, working_area_t
*area
, int restore
)
860 if (restore
&&target
->backup_working_area
)
861 target
->type
->write_memory(target
, area
->address
, 4, area
->size
/ 4, area
->backup
);
865 /* mark user pointer invalid */
872 int target_free_working_area(struct target_s
*target
, working_area_t
*area
)
874 return target_free_working_area_restore(target
, area
, 1);
877 int target_free_all_working_areas_restore(struct target_s
*target
, int restore
)
879 working_area_t
*c
= target
->working_areas
;
883 working_area_t
*next
= c
->next
;
884 target_free_working_area_restore(target
, c
, restore
);
894 target
->working_areas
= NULL
;
899 int target_free_all_working_areas(struct target_s
*target
)
901 return target_free_all_working_areas_restore(target
, 1);
904 int target_register_commands(struct command_context_s
*cmd_ctx
)
906 register_command(cmd_ctx
, NULL
, "target", handle_target_command
, COMMAND_CONFIG
, "target <cpu> [reset_init default - DEPRECATED] <chainpos> <endianness> <variant> [cpu type specifc args]");
907 register_command(cmd_ctx
, NULL
, "targets", handle_targets_command
, COMMAND_EXEC
, NULL
);
908 register_command(cmd_ctx
, NULL
, "working_area", handle_working_area_command
, COMMAND_ANY
, "working_area <target#> <address> <size> <'backup'|'nobackup'> [virtual address]");
909 register_command(cmd_ctx
, NULL
, "virt2phys", handle_virt2phys_command
, COMMAND_ANY
, "virt2phys <virtual address>");
910 register_command(cmd_ctx
, NULL
, "profile", handle_profile_command
, COMMAND_EXEC
, "PRELIMINARY! - profile <seconds> <gmon.out>");
914 /* script procedures */
915 register_jim(cmd_ctx
, "ocd_mem2array", jim_mem2array
, "read memory and return as a TCL array for script processing");
916 register_jim(cmd_ctx
, "ocd_array2mem", jim_array2mem
, "convert a TCL array to memory locations and write the values");
920 int target_arch_state(struct target_s
*target
)
925 LOG_USER("No target has been configured");
929 LOG_USER("target state: %s",
930 Jim_Nvp_value2name_simple(nvp_target_state
,target
->state
)->name
);
932 if (target
->state
!=TARGET_HALTED
)
935 retval
=target
->type
->arch_state(target
);
939 /* Single aligned words are guaranteed to use 16 or 32 bit access
940 * mode respectively, otherwise data is handled as quickly as
943 int target_write_buffer(struct target_s
*target
, u32 address
, u32 size
, u8
*buffer
)
946 LOG_DEBUG("writing buffer of %i byte at 0x%8.8x", size
, address
);
948 if (!target
->type
->examined
)
950 LOG_ERROR("Target not examined yet");
954 if ((address
+ size
- 1) < address
)
956 /* GDB can request this when e.g. PC is 0xfffffffc*/
957 LOG_ERROR("address+size wrapped(0x%08x, 0x%08x)", address
, size
);
961 if (((address
% 2) == 0) && (size
== 2))
963 return target
->type
->write_memory(target
, address
, 2, 1, buffer
);
966 /* handle unaligned head bytes */
969 int unaligned
= 4 - (address
% 4);
971 if (unaligned
> size
)
974 if ((retval
= target
->type
->write_memory(target
, address
, 1, unaligned
, buffer
)) != ERROR_OK
)
978 address
+= unaligned
;
982 /* handle aligned words */
985 int aligned
= size
- (size
% 4);
987 /* use bulk writes above a certain limit. This may have to be changed */
990 if ((retval
= target
->type
->bulk_write_memory(target
, address
, aligned
/ 4, buffer
)) != ERROR_OK
)
995 if ((retval
= target
->type
->write_memory(target
, address
, 4, aligned
/ 4, buffer
)) != ERROR_OK
)
1004 /* handle tail writes of less than 4 bytes */
1007 if ((retval
= target
->type
->write_memory(target
, address
, 1, size
, buffer
)) != ERROR_OK
)
1015 /* Single aligned words are guaranteed to use 16 or 32 bit access
1016 * mode respectively, otherwise data is handled as quickly as
1019 int target_read_buffer(struct target_s
*target
, u32 address
, u32 size
, u8
*buffer
)
1022 LOG_DEBUG("reading buffer of %i byte at 0x%8.8x", size
, address
);
1024 if (!target
->type
->examined
)
1026 LOG_ERROR("Target not examined yet");
1030 if ((address
+ size
- 1) < address
)
1032 /* GDB can request this when e.g. PC is 0xfffffffc*/
1033 LOG_ERROR("address+size wrapped(0x%08x, 0x%08x)", address
, size
);
1037 if (((address
% 2) == 0) && (size
== 2))
1039 return target
->type
->read_memory(target
, address
, 2, 1, buffer
);
1042 /* handle unaligned head bytes */
1045 int unaligned
= 4 - (address
% 4);
1047 if (unaligned
> size
)
1050 if ((retval
= target
->type
->read_memory(target
, address
, 1, unaligned
, buffer
)) != ERROR_OK
)
1053 buffer
+= unaligned
;
1054 address
+= unaligned
;
1058 /* handle aligned words */
1061 int aligned
= size
- (size
% 4);
1063 if ((retval
= target
->type
->read_memory(target
, address
, 4, aligned
/ 4, buffer
)) != ERROR_OK
)
1071 /* handle tail writes of less than 4 bytes */
1074 if ((retval
= target
->type
->read_memory(target
, address
, 1, size
, buffer
)) != ERROR_OK
)
1081 int target_checksum_memory(struct target_s
*target
, u32 address
, u32 size
, u32
* crc
)
1087 if (!target
->type
->examined
)
1089 LOG_ERROR("Target not examined yet");
1093 if ((retval
= target
->type
->checksum_memory(target
, address
,
1094 size
, &checksum
)) == ERROR_TARGET_RESOURCE_NOT_AVAILABLE
)
1096 buffer
= malloc(size
);
1099 LOG_ERROR("error allocating buffer for section (%d bytes)", size
);
1100 return ERROR_INVALID_ARGUMENTS
;
1102 retval
= target_read_buffer(target
, address
, size
, buffer
);
1103 if (retval
!= ERROR_OK
)
1109 /* convert to target endianess */
1110 for (i
= 0; i
< (size
/sizeof(u32
)); i
++)
1113 target_data
= target_buffer_get_u32(target
, &buffer
[i
*sizeof(u32
)]);
1114 target_buffer_set_u32(target
, &buffer
[i
*sizeof(u32
)], target_data
);
1117 retval
= image_calculate_checksum( buffer
, size
, &checksum
);
1126 int target_blank_check_memory(struct target_s
*target
, u32 address
, u32 size
, u32
* blank
)
1129 if (!target
->type
->examined
)
1131 LOG_ERROR("Target not examined yet");
1135 if (target
->type
->blank_check_memory
== 0)
1136 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
1138 retval
= target
->type
->blank_check_memory(target
, address
, size
, blank
);
1143 int target_read_u32(struct target_s
*target
, u32 address
, u32
*value
)
1146 if (!target
->type
->examined
)
1148 LOG_ERROR("Target not examined yet");
1152 int retval
= target
->type
->read_memory(target
, address
, 4, 1, value_buf
);
1154 if (retval
== ERROR_OK
)
1156 *value
= target_buffer_get_u32(target
, value_buf
);
1157 LOG_DEBUG("address: 0x%8.8x, value: 0x%8.8x", address
, *value
);
1162 LOG_DEBUG("address: 0x%8.8x failed", address
);
1168 int target_read_u16(struct target_s
*target
, u32 address
, u16
*value
)
1171 if (!target
->type
->examined
)
1173 LOG_ERROR("Target not examined yet");
1177 int retval
= target
->type
->read_memory(target
, address
, 2, 1, value_buf
);
1179 if (retval
== ERROR_OK
)
1181 *value
= target_buffer_get_u16(target
, value_buf
);
1182 LOG_DEBUG("address: 0x%8.8x, value: 0x%4.4x", address
, *value
);
1187 LOG_DEBUG("address: 0x%8.8x failed", address
);
1193 int target_read_u8(struct target_s
*target
, u32 address
, u8
*value
)
1195 int retval
= target
->type
->read_memory(target
, address
, 1, 1, value
);
1196 if (!target
->type
->examined
)
1198 LOG_ERROR("Target not examined yet");
1202 if (retval
== ERROR_OK
)
1204 LOG_DEBUG("address: 0x%8.8x, value: 0x%2.2x", address
, *value
);
1209 LOG_DEBUG("address: 0x%8.8x failed", address
);
1215 int target_write_u32(struct target_s
*target
, u32 address
, u32 value
)
1219 if (!target
->type
->examined
)
1221 LOG_ERROR("Target not examined yet");
1225 LOG_DEBUG("address: 0x%8.8x, value: 0x%8.8x", address
, value
);
1227 target_buffer_set_u32(target
, value_buf
, value
);
1228 if ((retval
= target
->type
->write_memory(target
, address
, 4, 1, value_buf
)) != ERROR_OK
)
1230 LOG_DEBUG("failed: %i", retval
);
1236 int target_write_u16(struct target_s
*target
, u32 address
, u16 value
)
1240 if (!target
->type
->examined
)
1242 LOG_ERROR("Target not examined yet");
1246 LOG_DEBUG("address: 0x%8.8x, value: 0x%8.8x", address
, value
);
1248 target_buffer_set_u16(target
, value_buf
, value
);
1249 if ((retval
= target
->type
->write_memory(target
, address
, 2, 1, value_buf
)) != ERROR_OK
)
1251 LOG_DEBUG("failed: %i", retval
);
1257 int target_write_u8(struct target_s
*target
, u32 address
, u8 value
)
1260 if (!target
->type
->examined
)
1262 LOG_ERROR("Target not examined yet");
1266 LOG_DEBUG("address: 0x%8.8x, value: 0x%2.2x", address
, value
);
1268 if ((retval
= target
->type
->read_memory(target
, address
, 1, 1, &value
)) != ERROR_OK
)
1270 LOG_DEBUG("failed: %i", retval
);
1276 int target_register_user_commands(struct command_context_s
*cmd_ctx
)
1278 register_command(cmd_ctx
, NULL
, "reg", handle_reg_command
, COMMAND_EXEC
, NULL
);
1279 register_command(cmd_ctx
, NULL
, "poll", handle_poll_command
, COMMAND_EXEC
, "poll target state");
1280 register_command(cmd_ctx
, NULL
, "wait_halt", handle_wait_halt_command
, COMMAND_EXEC
, "wait for target halt [time (s)]");
1281 register_command(cmd_ctx
, NULL
, "halt", handle_halt_command
, COMMAND_EXEC
, "halt target");
1282 register_command(cmd_ctx
, NULL
, "resume", handle_resume_command
, COMMAND_EXEC
, "resume target [addr]");
1283 register_command(cmd_ctx
, NULL
, "step", handle_step_command
, COMMAND_EXEC
, "step one instruction from current PC or [addr]");
1284 register_command(cmd_ctx
, NULL
, "reset", handle_reset_command
, COMMAND_EXEC
, "reset target [run|halt|init] - default is run");
1285 register_command(cmd_ctx
, NULL
, "soft_reset_halt", handle_soft_reset_halt_command
, COMMAND_EXEC
, "halt the target and do a soft reset");
1287 register_command(cmd_ctx
, NULL
, "mdw", handle_md_command
, COMMAND_EXEC
, "display memory words <addr> [count]");
1288 register_command(cmd_ctx
, NULL
, "mdh", handle_md_command
, COMMAND_EXEC
, "display memory half-words <addr> [count]");
1289 register_command(cmd_ctx
, NULL
, "mdb", handle_md_command
, COMMAND_EXEC
, "display memory bytes <addr> [count]");
1291 register_command(cmd_ctx
, NULL
, "mww", handle_mw_command
, COMMAND_EXEC
, "write memory word <addr> <value> [count]");
1292 register_command(cmd_ctx
, NULL
, "mwh", handle_mw_command
, COMMAND_EXEC
, "write memory half-word <addr> <value> [count]");
1293 register_command(cmd_ctx
, NULL
, "mwb", handle_mw_command
, COMMAND_EXEC
, "write memory byte <addr> <value> [count]");
1295 register_command(cmd_ctx
, NULL
, "bp", handle_bp_command
, COMMAND_EXEC
, "set breakpoint <address> <length> [hw]");
1296 register_command(cmd_ctx
, NULL
, "rbp", handle_rbp_command
, COMMAND_EXEC
, "remove breakpoint <adress>");
1297 register_command(cmd_ctx
, NULL
, "wp", handle_wp_command
, COMMAND_EXEC
, "set watchpoint <address> <length> <r/w/a> [value] [mask]");
1298 register_command(cmd_ctx
, NULL
, "rwp", handle_rwp_command
, COMMAND_EXEC
, "remove watchpoint <adress>");
1300 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]");
1301 register_command(cmd_ctx
, NULL
, "dump_image", handle_dump_image_command
, COMMAND_EXEC
, "dump_image <file> <address> <size>");
1302 register_command(cmd_ctx
, NULL
, "verify_image", handle_verify_image_command
, COMMAND_EXEC
, "verify_image <file> [offset] [type]");
1304 target_request_register_commands(cmd_ctx
);
1305 trace_register_commands(cmd_ctx
);
1310 int handle_targets_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
1313 target_t
*target
= all_targets
;
1317 /* try as tcltarget name */
1318 for( target
= all_targets
; target
; target
++ ){
1319 if( target
->cmd_name
){
1320 if( 0 == strcmp( args
[0], target
->cmd_name
) ){
1326 /* no match, try as number */
1328 int num
= strtoul(args
[0], &cp
, 0 );
1330 /* then it was not a number */
1331 command_print( cmd_ctx
, "Target: %s unknown, try one of:\n", args
[0] );
1335 target
= get_target_by_num( num
);
1336 if( target
== NULL
){
1337 command_print(cmd_ctx
,"Target: %s is unknown, try one of:\n", args
[0] );
1341 cmd_ctx
->current_target
= target
->target_number
;
1346 command_print(cmd_ctx
, " CmdName Type Endian State ");
1347 command_print(cmd_ctx
, "-- ---------- ---------- ---------- ----------");
1350 /* XX: abcdefghij abcdefghij abcdefghij abcdefghij */
1351 command_print(cmd_ctx
, "%2d: %-10s %-10s %-10s %s",
1352 target
->target_number
,
1353 "", // future: target->cmd_name
1355 Jim_Nvp_value2name_simple( nvp_target_endian
, target
->endianness
)->name
,
1356 Jim_Nvp_value2name_simple( nvp_target_state
, target
->state
)->name
);
1357 target
= target
->next
;
1363 int handle_target_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
1370 return ERROR_COMMAND_SYNTAX_ERROR
;
1373 /* search for the specified target */
1374 if (args
[0] && (args
[0][0] != 0))
1376 for (i
= 0; target_types
[i
]; i
++)
1378 if (strcmp(args
[0], target_types
[i
]->name
) == 0)
1380 target_t
**last_target_p
= &all_targets
;
1382 /* register target specific commands */
1383 if (target_types
[i
]->register_commands(cmd_ctx
) != ERROR_OK
)
1385 LOG_ERROR("couldn't register '%s' commands", args
[0]);
1391 while ((*last_target_p
)->next
)
1392 last_target_p
= &((*last_target_p
)->next
);
1393 last_target_p
= &((*last_target_p
)->next
);
1396 // get target number *before* adding new target to the list */
1397 i
= new_target_number();
1398 // calloc will init the memory to zero for us
1399 *last_target_p
= calloc(1,sizeof(target_t
));
1400 // save target number.
1401 (*last_target_p
)->cmd_name
= NULL
;
1402 (*last_target_p
)->target_number
= i
;
1404 /* allocate memory for each unique target type */
1405 (*last_target_p
)->type
= (target_type_t
*)malloc(sizeof(target_type_t
));
1406 *((*last_target_p
)->type
) = *target_types
[i
];
1408 if (strcmp(args
[1], "big") == 0)
1409 (*last_target_p
)->endianness
= TARGET_BIG_ENDIAN
;
1410 else if (strcmp(args
[1], "little") == 0)
1411 (*last_target_p
)->endianness
= TARGET_LITTLE_ENDIAN
;
1414 LOG_ERROR("endianness must be either 'little' or 'big', not '%s'", args
[1]);
1415 return ERROR_COMMAND_SYNTAX_ERROR
;
1418 if (strcmp(args
[2], "reset_halt") == 0)
1420 LOG_WARNING("reset_mode argument is obsolete.");
1421 return ERROR_COMMAND_SYNTAX_ERROR
;
1423 else if (strcmp(args
[2], "reset_run") == 0)
1425 LOG_WARNING("reset_mode argument is obsolete.");
1426 return ERROR_COMMAND_SYNTAX_ERROR
;
1428 else if (strcmp(args
[2], "reset_init") == 0)
1430 LOG_WARNING("reset_mode argument is obsolete.");
1431 return ERROR_COMMAND_SYNTAX_ERROR
;
1433 else if (strcmp(args
[2], "run_and_halt") == 0)
1435 LOG_WARNING("reset_mode argument is obsolete.");
1436 return ERROR_COMMAND_SYNTAX_ERROR
;
1438 else if (strcmp(args
[2], "run_and_init") == 0)
1440 LOG_WARNING("reset_mode argument is obsolete.");
1441 return ERROR_COMMAND_SYNTAX_ERROR
;
1445 /* Kludge! we want to make this reset arg optional while remaining compatible! */
1450 (*last_target_p
)->working_area
= 0x0;
1451 (*last_target_p
)->working_area_size
= 0x0;
1452 (*last_target_p
)->working_areas
= NULL
;
1453 (*last_target_p
)->backup_working_area
= 0;
1455 (*last_target_p
)->state
= TARGET_UNKNOWN
;
1456 (*last_target_p
)->debug_reason
= DBG_REASON_UNDEFINED
;
1457 (*last_target_p
)->reg_cache
= NULL
;
1458 (*last_target_p
)->breakpoints
= NULL
;
1459 (*last_target_p
)->watchpoints
= NULL
;
1460 (*last_target_p
)->next
= NULL
;
1461 (*last_target_p
)->arch_info
= NULL
;
1463 /* initialize trace information */
1464 (*last_target_p
)->trace_info
= malloc(sizeof(trace_t
));
1465 (*last_target_p
)->trace_info
->num_trace_points
= 0;
1466 (*last_target_p
)->trace_info
->trace_points_size
= 0;
1467 (*last_target_p
)->trace_info
->trace_points
= NULL
;
1468 (*last_target_p
)->trace_info
->trace_history_size
= 0;
1469 (*last_target_p
)->trace_info
->trace_history
= NULL
;
1470 (*last_target_p
)->trace_info
->trace_history_pos
= 0;
1471 (*last_target_p
)->trace_info
->trace_history_overflowed
= 0;
1473 (*last_target_p
)->dbgmsg
= NULL
;
1474 (*last_target_p
)->dbg_msg_enabled
= 0;
1476 (*last_target_p
)->type
->target_command(cmd_ctx
, cmd
, args
, argc
, *last_target_p
);
1484 /* no matching target found */
1487 LOG_ERROR("target '%s' not found", args
[0]);
1488 return ERROR_COMMAND_SYNTAX_ERROR
;
1494 int target_invoke_script(struct command_context_s
*cmd_ctx
, target_t
*target
, char *name
)
1496 return command_run_linef(cmd_ctx
, " if {[catch {info body target_%d_%s} t]==0} {target_%d_%s}",
1497 get_num_by_target(target
), name
,
1498 get_num_by_target(target
), name
);
1501 int handle_working_area_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
1503 target_t
*target
= NULL
;
1505 if ((argc
< 4) || (argc
> 5))
1507 return ERROR_COMMAND_SYNTAX_ERROR
;
1510 target
= get_target_by_num(strtoul(args
[0], NULL
, 0));
1513 return ERROR_COMMAND_SYNTAX_ERROR
;
1515 target_free_all_working_areas(target
);
1517 target
->working_area_phys
= target
->working_area_virt
= strtoul(args
[1], NULL
, 0);
1520 target
->working_area_virt
= strtoul(args
[4], NULL
, 0);
1522 target
->working_area_size
= strtoul(args
[2], NULL
, 0);
1524 if (strcmp(args
[3], "backup") == 0)
1526 target
->backup_working_area
= 1;
1528 else if (strcmp(args
[3], "nobackup") == 0)
1530 target
->backup_working_area
= 0;
1534 LOG_ERROR("unrecognized <backup|nobackup> argument (%s)", args
[3]);
1535 return ERROR_COMMAND_SYNTAX_ERROR
;
1542 /* process target state changes */
1543 int handle_target(void *priv
)
1545 target_t
*target
= all_targets
;
1549 if (target_continous_poll
)
1551 /* polling may fail silently until the target has been examined */
1552 target_poll(target
);
1555 target
= target
->next
;
1561 int handle_reg_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
1570 target
= get_current_target(cmd_ctx
);
1572 /* list all available registers for the current target */
1575 reg_cache_t
*cache
= target
->reg_cache
;
1581 for (i
= 0; i
< cache
->num_regs
; i
++)
1583 value
= buf_to_str(cache
->reg_list
[i
].value
, cache
->reg_list
[i
].size
, 16);
1584 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
);
1587 cache
= cache
->next
;
1593 /* access a single register by its ordinal number */
1594 if ((args
[0][0] >= '0') && (args
[0][0] <= '9'))
1596 int num
= strtoul(args
[0], NULL
, 0);
1597 reg_cache_t
*cache
= target
->reg_cache
;
1603 for (i
= 0; i
< cache
->num_regs
; i
++)
1607 reg
= &cache
->reg_list
[i
];
1613 cache
= cache
->next
;
1618 command_print(cmd_ctx
, "%i is out of bounds, the current target has only %i registers (0 - %i)", num
, count
, count
- 1);
1621 } else /* access a single register by its name */
1623 reg
= register_get_by_name(target
->reg_cache
, args
[0], 1);
1627 command_print(cmd_ctx
, "register %s not found in current target", args
[0]);
1632 /* display a register */
1633 if ((argc
== 1) || ((argc
== 2) && !((args
[1][0] >= '0') && (args
[1][0] <= '9'))))
1635 if ((argc
== 2) && (strcmp(args
[1], "force") == 0))
1638 if (reg
->valid
== 0)
1640 reg_arch_type_t
*arch_type
= register_get_arch_type(reg
->arch_type
);
1641 if (arch_type
== NULL
)
1643 LOG_ERROR("BUG: encountered unregistered arch type");
1646 arch_type
->get(reg
);
1648 value
= buf_to_str(reg
->value
, reg
->size
, 16);
1649 command_print(cmd_ctx
, "%s (/%i): 0x%s", reg
->name
, reg
->size
, value
);
1654 /* set register value */
1657 u8
*buf
= malloc(CEIL(reg
->size
, 8));
1658 str_to_buf(args
[1], strlen(args
[1]), buf
, reg
->size
, 0);
1660 reg_arch_type_t
*arch_type
= register_get_arch_type(reg
->arch_type
);
1661 if (arch_type
== NULL
)
1663 LOG_ERROR("BUG: encountered unregistered arch type");
1667 arch_type
->set(reg
, buf
);
1669 value
= buf_to_str(reg
->value
, reg
->size
, 16);
1670 command_print(cmd_ctx
, "%s (/%i): 0x%s", reg
->name
, reg
->size
, value
);
1678 command_print(cmd_ctx
, "usage: reg <#|name> [value]");
1684 int handle_poll_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
1686 target_t
*target
= get_current_target(cmd_ctx
);
1690 target_poll(target
);
1691 target_arch_state(target
);
1695 if (strcmp(args
[0], "on") == 0)
1697 target_continous_poll
= 1;
1699 else if (strcmp(args
[0], "off") == 0)
1701 target_continous_poll
= 0;
1705 command_print(cmd_ctx
, "arg is \"on\" or \"off\"");
1713 int handle_wait_halt_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
1721 ms
= strtoul(args
[0], &end
, 0) * 1000;
1724 command_print(cmd_ctx
, "usage: %s [seconds]", cmd
);
1728 target_t
*target
= get_current_target(cmd_ctx
);
1730 return target_wait_state(target
, TARGET_HALTED
, ms
);
1733 int target_wait_state(target_t
*target
, enum target_state state
, int ms
)
1736 struct timeval timeout
, now
;
1738 gettimeofday(&timeout
, NULL
);
1739 timeval_add_time(&timeout
, 0, ms
* 1000);
1743 if ((retval
=target_poll(target
))!=ERROR_OK
)
1746 if (target
->state
== state
)
1753 LOG_DEBUG("waiting for target %s...",
1754 Jim_Nvp_value2name_simple(nvp_target_state
,state
)->name
);
1757 gettimeofday(&now
, NULL
);
1758 if ((now
.tv_sec
> timeout
.tv_sec
) || ((now
.tv_sec
== timeout
.tv_sec
) && (now
.tv_usec
>= timeout
.tv_usec
)))
1760 LOG_ERROR("timed out while waiting for target %s",
1761 Jim_Nvp_value2name_simple(nvp_target_state
,state
)->name
);
1769 int handle_halt_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
1772 target_t
*target
= get_current_target(cmd_ctx
);
1776 if ((retval
= target_halt(target
)) != ERROR_OK
)
1781 return handle_wait_halt_command(cmd_ctx
, cmd
, args
, argc
);
1784 int handle_soft_reset_halt_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
1786 target_t
*target
= get_current_target(cmd_ctx
);
1788 LOG_USER("requesting target halt and executing a soft reset");
1790 target
->type
->soft_reset_halt(target
);
1795 int handle_reset_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
1797 enum target_reset_mode reset_mode
= RESET_RUN
;
1801 if (strcmp("run", args
[0]) == 0)
1802 reset_mode
= RESET_RUN
;
1803 else if (strcmp("halt", args
[0]) == 0)
1804 reset_mode
= RESET_HALT
;
1805 else if (strcmp("init", args
[0]) == 0)
1806 reset_mode
= RESET_INIT
;
1809 return ERROR_COMMAND_SYNTAX_ERROR
;
1813 /* reset *all* targets */
1814 return target_process_reset(cmd_ctx
, reset_mode
);
1817 int handle_resume_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
1820 target_t
*target
= get_current_target(cmd_ctx
);
1822 target_invoke_script(cmd_ctx
, target
, "pre_resume");
1825 retval
= target_resume(target
, 1, 0, 1, 0); /* current pc, addr = 0, handle breakpoints, not debugging */
1827 retval
= target_resume(target
, 0, strtoul(args
[0], NULL
, 0), 1, 0); /* addr = args[0], handle breakpoints, not debugging */
1830 return ERROR_COMMAND_SYNTAX_ERROR
;
1836 int handle_step_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
1838 target_t
*target
= get_current_target(cmd_ctx
);
1843 target
->type
->step(target
, 1, 0, 1); /* current pc, addr = 0, handle breakpoints */
1846 target
->type
->step(target
, 0, strtoul(args
[0], NULL
, 0), 1); /* addr = args[0], handle breakpoints */
1851 int handle_md_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
1853 const int line_bytecnt
= 32;
1866 target_t
*target
= get_current_target(cmd_ctx
);
1872 count
= strtoul(args
[1], NULL
, 0);
1874 address
= strtoul(args
[0], NULL
, 0);
1880 size
= 4; line_modulo
= line_bytecnt
/ 4;
1883 size
= 2; line_modulo
= line_bytecnt
/ 2;
1886 size
= 1; line_modulo
= line_bytecnt
/ 1;
1892 buffer
= calloc(count
, size
);
1893 retval
= target
->type
->read_memory(target
, address
, size
, count
, buffer
);
1894 if (retval
== ERROR_OK
)
1898 for (i
= 0; i
< count
; i
++)
1900 if (i
%line_modulo
== 0)
1901 output_len
+= snprintf(output
+ output_len
, 128 - output_len
, "0x%8.8x: ", address
+ (i
*size
));
1906 output_len
+= snprintf(output
+ output_len
, 128 - output_len
, "%8.8x ", target_buffer_get_u32(target
, &buffer
[i
*4]));
1909 output_len
+= snprintf(output
+ output_len
, 128 - output_len
, "%4.4x ", target_buffer_get_u16(target
, &buffer
[i
*2]));
1912 output_len
+= snprintf(output
+ output_len
, 128 - output_len
, "%2.2x ", buffer
[i
*1]);
1916 if ((i
%line_modulo
== line_modulo
-1) || (i
== count
- 1))
1918 command_print(cmd_ctx
, output
);
1929 int handle_mw_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
1936 target_t
*target
= get_current_target(cmd_ctx
);
1939 if ((argc
< 2) || (argc
> 3))
1940 return ERROR_COMMAND_SYNTAX_ERROR
;
1942 address
= strtoul(args
[0], NULL
, 0);
1943 value
= strtoul(args
[1], NULL
, 0);
1945 count
= strtoul(args
[2], NULL
, 0);
1951 target_buffer_set_u32(target
, value_buf
, value
);
1955 target_buffer_set_u16(target
, value_buf
, value
);
1959 value_buf
[0] = value
;
1962 return ERROR_COMMAND_SYNTAX_ERROR
;
1964 for (i
=0; i
<count
; i
++)
1970 retval
= target
->type
->write_memory(target
, address
+ i
*wordsize
, 4, 1, value_buf
);
1973 retval
= target
->type
->write_memory(target
, address
+ i
*wordsize
, 2, 1, value_buf
);
1976 retval
= target
->type
->write_memory(target
, address
+ i
*wordsize
, 1, 1, value_buf
);
1981 if (retval
!=ERROR_OK
)
1991 int handle_load_image_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
1997 u32 max_address
=0xffffffff;
2003 duration_t duration
;
2004 char *duration_text
;
2006 target_t
*target
= get_current_target(cmd_ctx
);
2008 if ((argc
< 1)||(argc
> 5))
2010 return ERROR_COMMAND_SYNTAX_ERROR
;
2013 /* a base address isn't always necessary, default to 0x0 (i.e. don't relocate) */
2016 image
.base_address_set
= 1;
2017 image
.base_address
= strtoul(args
[1], NULL
, 0);
2021 image
.base_address_set
= 0;
2025 image
.start_address_set
= 0;
2029 min_address
=strtoul(args
[3], NULL
, 0);
2033 max_address
=strtoul(args
[4], NULL
, 0)+min_address
;
2036 if (min_address
>max_address
)
2038 return ERROR_COMMAND_SYNTAX_ERROR
;
2042 duration_start_measure(&duration
);
2044 if (image_open(&image
, args
[0], (argc
>= 3) ? args
[2] : NULL
) != ERROR_OK
)
2051 for (i
= 0; i
< image
.num_sections
; i
++)
2053 buffer
= malloc(image
.sections
[i
].size
);
2056 command_print(cmd_ctx
, "error allocating buffer for section (%d bytes)", image
.sections
[i
].size
);
2060 if ((retval
= image_read_section(&image
, i
, 0x0, image
.sections
[i
].size
, buffer
, &buf_cnt
)) != ERROR_OK
)
2070 /* DANGER!!! beware of unsigned comparision here!!! */
2072 if ((image
.sections
[i
].base_address
+buf_cnt
>=min_address
)&&
2073 (image
.sections
[i
].base_address
<max_address
))
2075 if (image
.sections
[i
].base_address
<min_address
)
2077 /* clip addresses below */
2078 offset
+=min_address
-image
.sections
[i
].base_address
;
2082 if (image
.sections
[i
].base_address
+buf_cnt
>max_address
)
2084 length
-=(image
.sections
[i
].base_address
+buf_cnt
)-max_address
;
2087 if ((retval
= target_write_buffer(target
, image
.sections
[i
].base_address
+offset
, length
, buffer
+offset
)) != ERROR_OK
)
2092 image_size
+= length
;
2093 command_print(cmd_ctx
, "%u byte written at address 0x%8.8x", length
, image
.sections
[i
].base_address
+offset
);
2099 duration_stop_measure(&duration
, &duration_text
);
2100 if (retval
==ERROR_OK
)
2102 command_print(cmd_ctx
, "downloaded %u byte in %s", image_size
, duration_text
);
2104 free(duration_text
);
2106 image_close(&image
);
2112 int handle_dump_image_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
2119 int retval
=ERROR_OK
;
2121 duration_t duration
;
2122 char *duration_text
;
2124 target_t
*target
= get_current_target(cmd_ctx
);
2128 command_print(cmd_ctx
, "usage: dump_image <filename> <address> <size>");
2132 address
= strtoul(args
[1], NULL
, 0);
2133 size
= strtoul(args
[2], NULL
, 0);
2135 if ((address
& 3) || (size
& 3))
2137 command_print(cmd_ctx
, "only 32-bit aligned address and size are supported");
2141 if (fileio_open(&fileio
, args
[0], FILEIO_WRITE
, FILEIO_BINARY
) != ERROR_OK
)
2146 duration_start_measure(&duration
);
2151 u32 this_run_size
= (size
> 560) ? 560 : size
;
2153 retval
= target
->type
->read_memory(target
, address
, 4, this_run_size
/ 4, buffer
);
2154 if (retval
!= ERROR_OK
)
2159 retval
= fileio_write(&fileio
, this_run_size
, buffer
, &size_written
);
2160 if (retval
!= ERROR_OK
)
2165 size
-= this_run_size
;
2166 address
+= this_run_size
;
2169 fileio_close(&fileio
);
2171 duration_stop_measure(&duration
, &duration_text
);
2172 if (retval
==ERROR_OK
)
2174 command_print(cmd_ctx
, "dumped %"PRIi64
" byte in %s", fileio
.size
, duration_text
);
2176 free(duration_text
);
2181 int handle_verify_image_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
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
)
2245 /* calculate checksum of image */
2246 image_calculate_checksum( buffer
, buf_cnt
, &checksum
);
2248 retval
= target_checksum_memory(target
, image
.sections
[i
].base_address
, buf_cnt
, &mem_checksum
);
2249 if( retval
!= ERROR_OK
)
2255 if( checksum
!= mem_checksum
)
2257 /* failed crc checksum, fall back to a binary compare */
2260 command_print(cmd_ctx
, "checksum mismatch - attempting binary compare");
2262 data
= (u8
*)malloc(buf_cnt
);
2264 /* Can we use 32bit word accesses? */
2266 int count
= buf_cnt
;
2267 if ((count
% 4) == 0)
2272 retval
= target
->type
->read_memory(target
, image
.sections
[i
].base_address
, size
, count
, data
);
2273 if (retval
== ERROR_OK
)
2276 for (t
= 0; t
< buf_cnt
; t
++)
2278 if (data
[t
] != buffer
[t
])
2280 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
]);
2293 image_size
+= buf_cnt
;
2296 duration_stop_measure(&duration
, &duration_text
);
2297 if (retval
==ERROR_OK
)
2299 command_print(cmd_ctx
, "verified %u bytes in %s", image_size
, duration_text
);
2301 free(duration_text
);
2303 image_close(&image
);
2308 int handle_bp_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
2311 target_t
*target
= get_current_target(cmd_ctx
);
2315 breakpoint_t
*breakpoint
= target
->breakpoints
;
2319 if (breakpoint
->type
== BKPT_SOFT
)
2321 char* buf
= buf_to_str(breakpoint
->orig_instr
, breakpoint
->length
, 16);
2322 command_print(cmd_ctx
, "0x%8.8x, 0x%x, %i, 0x%s", breakpoint
->address
, breakpoint
->length
, breakpoint
->set
, buf
);
2327 command_print(cmd_ctx
, "0x%8.8x, 0x%x, %i", breakpoint
->address
, breakpoint
->length
, breakpoint
->set
);
2329 breakpoint
= breakpoint
->next
;
2337 length
= strtoul(args
[1], NULL
, 0);
2340 if (strcmp(args
[2], "hw") == 0)
2343 if ((retval
= breakpoint_add(target
, strtoul(args
[0], NULL
, 0), length
, hw
)) != ERROR_OK
)
2345 LOG_ERROR("Failure setting breakpoints");
2349 command_print(cmd_ctx
, "breakpoint added at address 0x%8.8x", strtoul(args
[0], NULL
, 0));
2354 command_print(cmd_ctx
, "usage: bp <address> <length> ['hw']");
2360 int handle_rbp_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
2362 target_t
*target
= get_current_target(cmd_ctx
);
2365 breakpoint_remove(target
, strtoul(args
[0], NULL
, 0));
2370 int handle_wp_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
2372 target_t
*target
= get_current_target(cmd_ctx
);
2377 watchpoint_t
*watchpoint
= target
->watchpoints
;
2381 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
);
2382 watchpoint
= watchpoint
->next
;
2387 enum watchpoint_rw type
= WPT_ACCESS
;
2388 u32 data_value
= 0x0;
2389 u32 data_mask
= 0xffffffff;
2405 command_print(cmd_ctx
, "usage: wp <address> <length> [r/w/a] [value] [mask]");
2411 data_value
= strtoul(args
[3], NULL
, 0);
2415 data_mask
= strtoul(args
[4], NULL
, 0);
2418 if ((retval
= watchpoint_add(target
, strtoul(args
[0], NULL
, 0),
2419 strtoul(args
[1], NULL
, 0), type
, data_value
, data_mask
)) != ERROR_OK
)
2421 LOG_ERROR("Failure setting breakpoints");
2426 command_print(cmd_ctx
, "usage: wp <address> <length> [r/w/a] [value] [mask]");
2432 int handle_rwp_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
2434 target_t
*target
= get_current_target(cmd_ctx
);
2437 watchpoint_remove(target
, strtoul(args
[0], NULL
, 0));
2442 int handle_virt2phys_command(command_context_t
*cmd_ctx
, char *cmd
, char **args
, int argc
)
2445 target_t
*target
= get_current_target(cmd_ctx
);
2451 return ERROR_COMMAND_SYNTAX_ERROR
;
2453 va
= strtoul(args
[0], NULL
, 0);
2455 retval
= target
->type
->virt2phys(target
, va
, &pa
);
2456 if (retval
== ERROR_OK
)
2458 command_print(cmd_ctx
, "Physical address 0x%08x", pa
);
2462 /* lower levels will have logged a detailed error which is
2463 * forwarded to telnet/GDB session.
2468 static void writeLong(FILE *f
, int l
)
2473 char c
=(l
>>(i
*8))&0xff;
2474 fwrite(&c
, 1, 1, f
);
2478 static void writeString(FILE *f
, char *s
)
2480 fwrite(s
, 1, strlen(s
), f
);
2485 // Dump a gmon.out histogram file.
2486 static void writeGmon(u32
*samples
, int sampleNum
, char *filename
)
2489 FILE *f
=fopen(filename
, "w");
2492 fwrite("gmon", 1, 4, f
);
2493 writeLong(f
, 0x00000001); // Version
2494 writeLong(f
, 0); // padding
2495 writeLong(f
, 0); // padding
2496 writeLong(f
, 0); // padding
2498 fwrite("", 1, 1, f
); // GMON_TAG_TIME_HIST
2500 // figure out bucket size
2503 for (i
=0; i
<sampleNum
; i
++)
2515 int addressSpace
=(max
-min
+1);
2517 static int const maxBuckets
=256*1024; // maximum buckets.
2518 int length
=addressSpace
;
2519 if (length
> maxBuckets
)
2523 int *buckets
=malloc(sizeof(int)*length
);
2529 memset(buckets
, 0, sizeof(int)*length
);
2530 for (i
=0; i
<sampleNum
;i
++)
2532 u32 address
=samples
[i
];
2533 long long a
=address
-min
;
2534 long long b
=length
-1;
2535 long long c
=addressSpace
-1;
2536 int index
=(a
*b
)/c
; // danger!!!! int32 overflows
2540 // append binary memory gmon.out &profile_hist_hdr ((char*)&profile_hist_hdr + sizeof(struct gmon_hist_hdr))
2541 writeLong(f
, min
); // low_pc
2542 writeLong(f
, max
); // high_pc
2543 writeLong(f
, length
); // # of samples
2544 writeLong(f
, 64000000); // 64MHz
2545 writeString(f
, "seconds");
2546 for (i
=0; i
<(15-strlen("seconds")); i
++)
2548 fwrite("", 1, 1, f
); // padding
2550 writeString(f
, "s");
2552 // append binary memory gmon.out profile_hist_data (profile_hist_data + profile_hist_hdr.hist_size)
2554 char *data
=malloc(2*length
);
2557 for (i
=0; i
<length
;i
++)
2566 data
[i
*2+1]=(val
>>8)&0xff;
2569 fwrite(data
, 1, length
*2, f
);
2579 /* profiling samples the CPU PC as quickly as OpenOCD is able, which will be used as a random sampling of PC */
2580 int handle_profile_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
2582 target_t
*target
= get_current_target(cmd_ctx
);
2583 struct timeval timeout
, now
;
2585 gettimeofday(&timeout
, NULL
);
2588 return ERROR_COMMAND_SYNTAX_ERROR
;
2591 timeval_add_time(&timeout
, strtoul(args
[0], &end
, 0), 0);
2597 command_print(cmd_ctx
, "Starting profiling. Halting and resuming the target as often as we can...");
2599 static const int maxSample
=10000;
2600 u32
*samples
=malloc(sizeof(u32
)*maxSample
);
2605 int retval
=ERROR_OK
;
2606 // hopefully it is safe to cache! We want to stop/restart as quickly as possible.
2607 reg_t
*reg
= register_get_by_name(target
->reg_cache
, "pc", 1);
2611 target_poll(target
);
2612 if (target
->state
== TARGET_HALTED
)
2614 u32 t
=*((u32
*)reg
->value
);
2615 samples
[numSamples
++]=t
;
2616 retval
= target_resume(target
, 1, 0, 0, 0); /* current pc, addr = 0, do not handle breakpoints, not debugging */
2617 target_poll(target
);
2618 alive_sleep(10); // sleep 10ms, i.e. <100 samples/second.
2619 } else if (target
->state
== TARGET_RUNNING
)
2621 // We want to quickly sample the PC.
2622 target_halt(target
);
2625 command_print(cmd_ctx
, "Target not halted or running");
2629 if (retval
!=ERROR_OK
)
2634 gettimeofday(&now
, NULL
);
2635 if ((numSamples
>=maxSample
) || ((now
.tv_sec
>= timeout
.tv_sec
) && (now
.tv_usec
>= timeout
.tv_usec
)))
2637 command_print(cmd_ctx
, "Profiling completed. %d samples.", numSamples
);
2638 target_poll(target
);
2639 if (target
->state
== TARGET_HALTED
)
2641 target_resume(target
, 1, 0, 0, 0); /* current pc, addr = 0, do not handle breakpoints, not debugging */
2643 target_poll(target
);
2644 writeGmon(samples
, numSamples
, args
[1]);
2645 command_print(cmd_ctx
, "Wrote %s", args
[1]);
2654 static int new_int_array_element(Jim_Interp
* interp
, const char *varname
, int idx
, u32 val
)
2657 Jim_Obj
*nameObjPtr
, *valObjPtr
;
2660 namebuf
= alloc_printf("%s(%d)", varname
, idx
);
2664 nameObjPtr
= Jim_NewStringObj(interp
, namebuf
, -1);
2665 valObjPtr
= Jim_NewIntObj(interp
, val
);
2666 if (!nameObjPtr
|| !valObjPtr
)
2672 Jim_IncrRefCount(nameObjPtr
);
2673 Jim_IncrRefCount(valObjPtr
);
2674 result
= Jim_SetVariable(interp
, nameObjPtr
, valObjPtr
);
2675 Jim_DecrRefCount(interp
, nameObjPtr
);
2676 Jim_DecrRefCount(interp
, valObjPtr
);
2678 /* printf("%s(%d) <= 0%08x\n", varname, idx, val); */
2682 static int jim_mem2array(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
2684 command_context_t
*context
;
2687 context
= Jim_GetAssocData(interp
, "context");
2688 if (context
== NULL
)
2690 LOG_ERROR("mem2array: no command context");
2693 target
= get_current_target(context
);
2696 LOG_ERROR("mem2array: no current target");
2700 return target_mem2array(interp
, target
, argc
,argv
);
2703 static int target_mem2array(Jim_Interp
*interp
, target_t
*target
, int argc
, Jim_Obj
*const *argv
)
2711 const char *varname
;
2713 int i
, n
, e
, retval
;
2715 /* argv[1] = name of array to receive the data
2716 * argv[2] = desired width
2717 * argv[3] = memory address
2718 * argv[4] = count of times to read
2721 Jim_WrongNumArgs(interp
, 1, argv
, "varname width addr nelems");
2724 varname
= Jim_GetString(argv
[1], &len
);
2725 /* given "foo" get space for worse case "foo(%d)" .. add 20 */
2727 e
= Jim_GetLong(interp
, argv
[2], &l
);
2733 e
= Jim_GetLong(interp
, argv
[3], &l
);
2738 e
= Jim_GetLong(interp
, argv
[4], &l
);
2754 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
2755 Jim_AppendStrings( interp
, Jim_GetResult(interp
), "Invalid width param, must be 8/16/32", NULL
);
2759 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
2760 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "mem2array: zero width read?", NULL
);
2763 if ((addr
+ (len
* width
)) < addr
) {
2764 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
2765 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "mem2array: addr + len - wraps to zero?", NULL
);
2768 /* absurd transfer size? */
2770 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
2771 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "mem2array: absurd > 64K item request", NULL
);
2776 ((width
== 2) && ((addr
& 1) == 0)) ||
2777 ((width
== 4) && ((addr
& 3) == 0))) {
2781 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
2782 sprintf(buf
, "mem2array address: 0x%08x is not aligned for %d byte reads", addr
, width
);
2783 Jim_AppendStrings(interp
, Jim_GetResult(interp
), buf
, NULL
);
2794 /* Slurp... in buffer size chunks */
2796 count
= len
; /* in objects.. */
2797 if (count
> (sizeof(buffer
)/width
)) {
2798 count
= (sizeof(buffer
)/width
);
2801 retval
= target
->type
->read_memory( target
, addr
, width
, count
, buffer
);
2802 if (retval
!= ERROR_OK
) {
2804 LOG_ERROR("mem2array: Read @ 0x%08x, w=%d, cnt=%d, failed", addr
, width
, count
);
2805 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
2806 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "mem2array: cannot read memory", NULL
);
2810 v
= 0; /* shut up gcc */
2811 for (i
= 0 ;i
< count
;i
++, n
++) {
2814 v
= target_buffer_get_u32(target
, &buffer
[i
*width
]);
2817 v
= target_buffer_get_u16(target
, &buffer
[i
*width
]);
2820 v
= buffer
[i
] & 0x0ff;
2823 new_int_array_element(interp
, varname
, n
, v
);
2829 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
2834 static int get_int_array_element(Jim_Interp
* interp
, const char *varname
, int idx
, u32
*val
)
2837 Jim_Obj
*nameObjPtr
, *valObjPtr
;
2841 namebuf
= alloc_printf("%s(%d)", varname
, idx
);
2845 nameObjPtr
= Jim_NewStringObj(interp
, namebuf
, -1);
2852 Jim_IncrRefCount(nameObjPtr
);
2853 valObjPtr
= Jim_GetVariable(interp
, nameObjPtr
, JIM_ERRMSG
);
2854 Jim_DecrRefCount(interp
, nameObjPtr
);
2856 if (valObjPtr
== NULL
)
2859 result
= Jim_GetLong(interp
, valObjPtr
, &l
);
2860 /* printf("%s(%d) => 0%08x\n", varname, idx, val); */
2865 static int jim_array2mem(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
2867 command_context_t
*context
;
2870 context
= Jim_GetAssocData(interp
, "context");
2871 if (context
== NULL
){
2872 LOG_ERROR("array2mem: no command context");
2875 target
= get_current_target(context
);
2876 if (target
== NULL
){
2877 LOG_ERROR("array2mem: no current target");
2881 return target_array2mem( interp
,target
, argc
, argv
);
2885 static int target_array2mem(Jim_Interp
*interp
, target_t
*target
, int argc
, Jim_Obj
*const *argv
)
2893 const char *varname
;
2895 int i
, n
, e
, retval
;
2897 /* argv[1] = name of array to get the data
2898 * argv[2] = desired width
2899 * argv[3] = memory address
2900 * argv[4] = count to write
2903 Jim_WrongNumArgs(interp
, 1, argv
, "varname width addr nelems");
2906 varname
= Jim_GetString(argv
[1], &len
);
2907 /* given "foo" get space for worse case "foo(%d)" .. add 20 */
2909 e
= Jim_GetLong(interp
, argv
[2], &l
);
2915 e
= Jim_GetLong(interp
, argv
[3], &l
);
2920 e
= Jim_GetLong(interp
, argv
[4], &l
);
2936 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
2937 Jim_AppendStrings( interp
, Jim_GetResult(interp
), "Invalid width param, must be 8/16/32", NULL
);
2941 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
2942 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "array2mem: zero width read?", NULL
);
2945 if ((addr
+ (len
* width
)) < addr
) {
2946 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
2947 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "array2mem: addr + len - wraps to zero?", NULL
);
2950 /* absurd transfer size? */
2952 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
2953 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "array2mem: absurd > 64K item request", NULL
);
2958 ((width
== 2) && ((addr
& 1) == 0)) ||
2959 ((width
== 4) && ((addr
& 3) == 0))) {
2963 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
2964 sprintf(buf
, "array2mem address: 0x%08x is not aligned for %d byte reads", addr
, width
);
2965 Jim_AppendStrings(interp
, Jim_GetResult(interp
), buf
, NULL
);
2977 /* Slurp... in buffer size chunks */
2979 count
= len
; /* in objects.. */
2980 if (count
> (sizeof(buffer
)/width
)) {
2981 count
= (sizeof(buffer
)/width
);
2984 v
= 0; /* shut up gcc */
2985 for (i
= 0 ;i
< count
;i
++, n
++) {
2986 get_int_array_element(interp
, varname
, n
, &v
);
2989 target_buffer_set_u32(target
, &buffer
[i
*width
], v
);
2992 target_buffer_set_u16(target
, &buffer
[i
*width
], v
);
2995 buffer
[i
] = v
& 0x0ff;
3001 retval
= target
->type
->write_memory(target
, addr
, width
, count
, buffer
);
3002 if (retval
!= ERROR_OK
) {
3004 LOG_ERROR("array2mem: Write @ 0x%08x, w=%d, cnt=%d, failed", addr
, width
, count
);
3005 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3006 Jim_AppendStrings(interp
, Jim_GetResult(interp
), "array2mem: cannot read memory", NULL
);
3012 Jim_SetResult(interp
, Jim_NewEmptyStringObj(interp
));
3019 * Local Variables: ***
3020 * c-basic-offset: 4 ***
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)