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 * This program is free software; you can redistribute it and/or modify *
15 * it under the terms of the GNU General Public License as published by *
16 * the Free Software Foundation; either version 2 of the License, or *
17 * (at your option) any later version. *
19 * This program is distributed in the hope that it will be useful, *
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
22 * GNU General Public License for more details. *
24 * You should have received a copy of the GNU General Public License *
25 * along with this program; if not, write to the *
26 * Free Software Foundation, Inc., *
27 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
28 ***************************************************************************/
33 #include "replacements.h"
35 #include "target_request.h"
38 #include "configuration.h"
39 #include "binarybuffer.h"
46 #include <sys/types.h>
54 #include <time_support.h>
59 int cli_target_callback_event_handler(struct target_s *target, enum target_event event, void *priv);
62 int handle_targets_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
64 int handle_working_area_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
66 int handle_reg_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
67 int handle_poll_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
68 int handle_halt_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
69 int handle_wait_halt_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
70 int handle_reset_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
71 int handle_soft_reset_halt_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
72 int handle_resume_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
73 int handle_step_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
74 int handle_md_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
75 int handle_mw_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
76 int handle_load_image_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
77 int handle_dump_image_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
78 int handle_verify_image_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
79 int handle_bp_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
80 int handle_rbp_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
81 int handle_wp_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
82 int handle_rwp_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
83 int handle_virt2phys_command(command_context_t *cmd_ctx, char *cmd, char **args, int argc);
84 int handle_profile_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
85 static int jim_array2mem(Jim_Interp *interp, int argc, Jim_Obj *const *argv);
86 static int jim_mem2array(Jim_Interp *interp, int argc, Jim_Obj *const *argv);
87 static int jim_target( Jim_Interp *interp, int argc, Jim_Obj *const *argv);
89 static int target_array2mem(Jim_Interp *interp, target_t *target, int argc, Jim_Obj *const *argv);
90 static int target_mem2array(Jim_Interp *interp, target_t *target, int argc, Jim_Obj *const *argv);
95 extern target_type_t arm7tdmi_target;
96 extern target_type_t arm720t_target;
97 extern target_type_t arm9tdmi_target;
98 extern target_type_t arm920t_target;
99 extern target_type_t arm966e_target;
100 extern target_type_t arm926ejs_target;
101 extern target_type_t feroceon_target;
102 extern target_type_t xscale_target;
103 extern target_type_t cortexm3_target;
104 extern target_type_t arm11_target;
105 extern target_type_t mips_m4k_target;
107 target_type_t *target_types[] =
123 target_t *all_targets = NULL;
124 target_event_callback_t *target_event_callbacks = NULL;
125 target_timer_callback_t *target_timer_callbacks = NULL;
127 const Jim_Nvp nvp_assert[] = {
128 { .name = "assert", NVP_ASSERT },
129 { .name = "deassert", NVP_DEASSERT },
130 { .name = "T", NVP_ASSERT },
131 { .name = "F", NVP_DEASSERT },
132 { .name = "t", NVP_ASSERT },
133 { .name = "f", NVP_DEASSERT },
134 { .name = NULL, .value = -1 }
137 const Jim_Nvp nvp_error_target[] = {
138 { .value = ERROR_TARGET_INVALID, .name = "err-invalid" },
139 { .value = ERROR_TARGET_INIT_FAILED, .name = "err-init-failed" },
140 { .value = ERROR_TARGET_TIMEOUT, .name = "err-timeout" },
141 { .value = ERROR_TARGET_NOT_HALTED, .name = "err-not-halted" },
142 { .value = ERROR_TARGET_FAILURE, .name = "err-failure" },
143 { .value = ERROR_TARGET_UNALIGNED_ACCESS , .name = "err-unaligned-access" },
144 { .value = ERROR_TARGET_DATA_ABORT , .name = "err-data-abort" },
145 { .value = ERROR_TARGET_RESOURCE_NOT_AVAILABLE , .name = "err-resource-not-available" },
146 { .value = ERROR_TARGET_TRANSLATION_FAULT , .name = "err-translation-fault" },
147 { .value = ERROR_TARGET_NOT_RUNNING, .name = "err-not-running" },
148 { .value = ERROR_TARGET_NOT_EXAMINED, .name = "err-not-examined" },
149 { .value = -1, .name = NULL }
152 const char *target_strerror_safe( int err )
156 n = Jim_Nvp_value2name_simple( nvp_error_target, err );
157 if( n->name == NULL ){
164 const Jim_Nvp nvp_target_event[] = {
165 { .value = TARGET_EVENT_OLD_pre_reset , .name = "old-pre_reset" },
166 { .value = TARGET_EVENT_OLD_gdb_program_config , .name = "old-gdb_program_config" },
167 { .value = TARGET_EVENT_OLD_post_reset , .name = "old-post_reset" },
168 { .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 /* historical name */
179 { .value = TARGET_EVENT_RESET_START, .name = "reset-start" },
181 { .value = TARGET_EVENT_RESET_ASSERT_PRE, .name = "reset-assert-pre" },
182 { .value = TARGET_EVENT_RESET_ASSERT_POST, .name = "reset-assert-post" },
183 { .value = TARGET_EVENT_RESET_DEASSERT_PRE, .name = "reset-deassert-pre" },
184 { .value = TARGET_EVENT_RESET_DEASSERT_POST, .name = "reset-deassert-post" },
185 { .value = TARGET_EVENT_RESET_HALT_PRE, .name = "reset-halt-pre" },
186 { .value = TARGET_EVENT_RESET_HALT_POST, .name = "reset-halt-post" },
187 { .value = TARGET_EVENT_RESET_WAIT_PRE, .name = "reset-wait-pre" },
188 { .value = TARGET_EVENT_RESET_WAIT_POST, .name = "reset-wait-post" },
189 { .value = TARGET_EVENT_RESET_INIT , .name = "reset-init" },
190 { .value = TARGET_EVENT_RESET_END, .name = "reset-end" },
196 { .value = TARGET_EVENT_EXAMINE_START, .name = "examine-start" },
197 { .value = TARGET_EVENT_EXAMINE_START, .name = "examine-end" },
200 { .value = TARGET_EVENT_DEBUG_HALTED, .name = "debug-halted" },
201 { .value = TARGET_EVENT_DEBUG_RESUMED, .name = "debug-resumed" },
203 { .value = TARGET_EVENT_GDB_ATTACH, .name = "gdb-attach" },
204 { .value = TARGET_EVENT_GDB_DETACH, .name = "gdb-detach" },
207 { .value = TARGET_EVENT_GDB_FLASH_WRITE_START, .name = "gdb-flash-write-start" },
208 { .value = TARGET_EVENT_GDB_FLASH_WRITE_END , .name = "gdb-flash-write-end" },
210 { .value = TARGET_EVENT_GDB_FLASH_ERASE_START, .name = "gdb-flash-erase-start" },
211 { .value = TARGET_EVENT_GDB_FLASH_ERASE_END , .name = "gdb-flash-erase-end" },
213 { .value = TARGET_EVENT_RESUME_START, .name = "resume-start" },
214 { .value = TARGET_EVENT_RESUMED , .name = "resume-ok" },
215 { .value = TARGET_EVENT_RESUME_END , .name = "resume-end" },
217 { .name = NULL, .value = -1 }
220 const Jim_Nvp nvp_target_state[] = {
221 { .name = "unknown", .value = TARGET_UNKNOWN },
222 { .name = "running", .value = TARGET_RUNNING },
223 { .name = "halted", .value = TARGET_HALTED },
224 { .name = "reset", .value = TARGET_RESET },
225 { .name = "debug-running", .value = TARGET_DEBUG_RUNNING },
226 { .name = NULL, .value = -1 },
230 const Jim_Nvp nvp_target_debug_reason [] = {
231 { .name = "debug-request" , .value = DBG_REASON_DBGRQ },
232 { .name = "breakpoint" , .value = DBG_REASON_BREAKPOINT },
233 { .name = "watchpoint" , .value = DBG_REASON_WATCHPOINT },
234 { .name = "watchpoint-and-breakpoint", .value = DBG_REASON_WPTANDBKPT },
235 { .name = "single-step" , .value = DBG_REASON_SINGLESTEP },
236 { .name = "target-not-halted" , .value = DBG_REASON_NOTHALTED },
237 { .name = "undefined" , .value = DBG_REASON_UNDEFINED },
238 { .name = NULL, .value = -1 },
242 const Jim_Nvp nvp_target_endian[] = {
243 { .name = "big", .value = TARGET_BIG_ENDIAN },
244 { .name = "little", .value = TARGET_LITTLE_ENDIAN },
245 { .name = "be", .value = TARGET_BIG_ENDIAN },
246 { .name = "le", .value = TARGET_LITTLE_ENDIAN },
247 { .name = NULL, .value = -1 },
250 const Jim_Nvp nvp_reset_modes[] = {
251 { .name = "unknown", .value = RESET_UNKNOWN },
252 { .name = "run" , .value = RESET_RUN },
253 { .name = "halt" , .value = RESET_HALT },
254 { .name = "init" , .value = RESET_INIT },
255 { .name = NULL , .value = -1 },
259 max_target_number( void )
267 if( x < t->target_number ){
268 x = (t->target_number)+1;
275 /* determine the number of the new target */
277 new_target_number( void )
282 /* number is 0 based */
286 if( x < t->target_number ){
287 x = t->target_number;
294 static int target_continous_poll = 1;
296 /* read a u32 from a buffer in target memory endianness */
297 u32 target_buffer_get_u32(target_t *target, u8 *buffer)
299 if (target->endianness == TARGET_LITTLE_ENDIAN)
300 return le_to_h_u32(buffer);
302 return be_to_h_u32(buffer);
305 /* read a u16 from a buffer in target memory endianness */
306 u16 target_buffer_get_u16(target_t *target, u8 *buffer)
308 if (target->endianness == TARGET_LITTLE_ENDIAN)
309 return le_to_h_u16(buffer);
311 return be_to_h_u16(buffer);
314 /* read a u8 from a buffer in target memory endianness */
315 u8 target_buffer_get_u8(target_t *target, u8 *buffer)
317 return *buffer & 0x0ff;
320 /* write a u32 to a buffer in target memory endianness */
321 void target_buffer_set_u32(target_t *target, u8 *buffer, u32 value)
323 if (target->endianness == TARGET_LITTLE_ENDIAN)
324 h_u32_to_le(buffer, value);
326 h_u32_to_be(buffer, value);
329 /* write a u16 to a buffer in target memory endianness */
330 void target_buffer_set_u16(target_t *target, u8 *buffer, u16 value)
332 if (target->endianness == TARGET_LITTLE_ENDIAN)
333 h_u16_to_le(buffer, value);
335 h_u16_to_be(buffer, value);
338 /* write a u8 to a buffer in target memory endianness */
339 void target_buffer_set_u8(target_t *target, u8 *buffer, u8 value)
344 /* returns a pointer to the n-th configured target */
345 target_t* get_target_by_num(int num)
347 target_t *target = all_targets;
350 if( target->target_number == num ){
353 target = target->next;
359 int get_num_by_target(target_t *query_target)
361 return query_target->target_number;
364 target_t* get_current_target(command_context_t *cmd_ctx)
366 target_t *target = get_target_by_num(cmd_ctx->current_target);
370 LOG_ERROR("BUG: current_target out of bounds");
378 int target_poll(struct target_s *target)
380 /* We can't poll until after examine */
381 if (!target->type->examined)
383 /* Fail silently lest we pollute the log */
386 return target->type->poll(target);
389 int target_halt(struct target_s *target)
391 /* We can't poll until after examine */
392 if (!target->type->examined)
394 LOG_ERROR("Target not examined yet");
397 return target->type->halt(target);
400 int target_resume(struct target_s *target, int current, u32 address, int handle_breakpoints, int debug_execution)
404 /* We can't poll until after examine */
405 if (!target->type->examined)
407 LOG_ERROR("Target not examined yet");
411 /* note that resume *must* be asynchronous. The CPU can halt before we poll. The CPU can
412 * even halt at the current PC as a result of a software breakpoint being inserted by (a bug?)
415 if ((retval = target->type->resume(target, current, address, handle_breakpoints, debug_execution)) != ERROR_OK)
422 int target_process_reset(struct command_context_s *cmd_ctx, enum target_reset_mode reset_mode)
427 n = Jim_Nvp_value2name_simple( nvp_reset_modes, reset_mode );
428 if( n->name == NULL ){
429 LOG_ERROR("invalid reset mode");
433 sprintf( buf, "ocd_process_reset %s", n->name );
434 retval = Jim_Eval( interp, buf );
436 if(retval != JIM_OK) {
437 Jim_PrintErrorMessage(interp);
441 /* We want any events to be processed before the prompt */
442 retval = target_call_timer_callbacks_now();
448 static int default_virt2phys(struct target_s *target, u32 virtual, u32 *physical)
454 static int default_mmu(struct target_s *target, int *enabled)
460 static int default_examine(struct target_s *target)
462 target->type->examined = 1;
467 /* Targets that correctly implement init+examine, i.e.
468 * no communication with target during init:
472 int target_examine(void)
474 int retval = ERROR_OK;
475 target_t *target = all_targets;
478 if ((retval = target->type->examine(target))!=ERROR_OK)
480 target = target->next;
485 static int target_write_memory_imp(struct target_s *target, u32 address, u32 size, u32 count, u8 *buffer)
487 if (!target->type->examined)
489 LOG_ERROR("Target not examined yet");
492 return target->type->write_memory_imp(target, address, size, count, buffer);
495 static int target_read_memory_imp(struct target_s *target, u32 address, u32 size, u32 count, u8 *buffer)
497 if (!target->type->examined)
499 LOG_ERROR("Target not examined yet");
502 return target->type->read_memory_imp(target, address, size, count, buffer);
505 static int target_soft_reset_halt_imp(struct target_s *target)
507 if (!target->type->examined)
509 LOG_ERROR("Target not examined yet");
512 return target->type->soft_reset_halt_imp(target);
515 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)
517 if (!target->type->examined)
519 LOG_ERROR("Target not examined yet");
522 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);
525 int target_init(struct command_context_s *cmd_ctx)
527 target_t *target = all_targets;
532 target->type->examined = 0;
533 if (target->type->examine == NULL)
535 target->type->examine = default_examine;
538 if (target->type->init_target(cmd_ctx, target) != ERROR_OK)
540 LOG_ERROR("target '%s' init failed", target->type->name);
544 /* Set up default functions if none are provided by target */
545 if (target->type->virt2phys == NULL)
547 target->type->virt2phys = default_virt2phys;
549 target->type->virt2phys = default_virt2phys;
550 /* a non-invasive way(in terms of patches) to add some code that
551 * runs before the type->write/read_memory implementation
553 target->type->write_memory_imp = target->type->write_memory;
554 target->type->write_memory = target_write_memory_imp;
555 target->type->read_memory_imp = target->type->read_memory;
556 target->type->read_memory = target_read_memory_imp;
557 target->type->soft_reset_halt_imp = target->type->soft_reset_halt;
558 target->type->soft_reset_halt = target_soft_reset_halt_imp;
559 target->type->run_algorithm_imp = target->type->run_algorithm;
560 target->type->run_algorithm = target_run_algorithm_imp;
563 if (target->type->mmu == NULL)
565 target->type->mmu = default_mmu;
567 target = target->next;
572 if((retval = target_register_user_commands(cmd_ctx)) != ERROR_OK)
574 if((retval = target_register_timer_callback(handle_target, 100, 1, NULL)) != ERROR_OK)
581 int target_register_event_callback(int (*callback)(struct target_s *target, enum target_event event, void *priv), void *priv)
583 target_event_callback_t **callbacks_p = &target_event_callbacks;
585 if (callback == NULL)
587 return ERROR_INVALID_ARGUMENTS;
592 while ((*callbacks_p)->next)
593 callbacks_p = &((*callbacks_p)->next);
594 callbacks_p = &((*callbacks_p)->next);
597 (*callbacks_p) = malloc(sizeof(target_event_callback_t));
598 (*callbacks_p)->callback = callback;
599 (*callbacks_p)->priv = priv;
600 (*callbacks_p)->next = NULL;
605 int target_register_timer_callback(int (*callback)(void *priv), int time_ms, int periodic, void *priv)
607 target_timer_callback_t **callbacks_p = &target_timer_callbacks;
610 if (callback == NULL)
612 return ERROR_INVALID_ARGUMENTS;
617 while ((*callbacks_p)->next)
618 callbacks_p = &((*callbacks_p)->next);
619 callbacks_p = &((*callbacks_p)->next);
622 (*callbacks_p) = malloc(sizeof(target_timer_callback_t));
623 (*callbacks_p)->callback = callback;
624 (*callbacks_p)->periodic = periodic;
625 (*callbacks_p)->time_ms = time_ms;
627 gettimeofday(&now, NULL);
628 (*callbacks_p)->when.tv_usec = now.tv_usec + (time_ms % 1000) * 1000;
629 time_ms -= (time_ms % 1000);
630 (*callbacks_p)->when.tv_sec = now.tv_sec + (time_ms / 1000);
631 if ((*callbacks_p)->when.tv_usec > 1000000)
633 (*callbacks_p)->when.tv_usec = (*callbacks_p)->when.tv_usec - 1000000;
634 (*callbacks_p)->when.tv_sec += 1;
637 (*callbacks_p)->priv = priv;
638 (*callbacks_p)->next = NULL;
643 int target_unregister_event_callback(int (*callback)(struct target_s *target, enum target_event event, void *priv), void *priv)
645 target_event_callback_t **p = &target_event_callbacks;
646 target_event_callback_t *c = target_event_callbacks;
648 if (callback == NULL)
650 return ERROR_INVALID_ARGUMENTS;
655 target_event_callback_t *next = c->next;
656 if ((c->callback == callback) && (c->priv == priv))
670 int target_unregister_timer_callback(int (*callback)(void *priv), void *priv)
672 target_timer_callback_t **p = &target_timer_callbacks;
673 target_timer_callback_t *c = target_timer_callbacks;
675 if (callback == NULL)
677 return ERROR_INVALID_ARGUMENTS;
682 target_timer_callback_t *next = c->next;
683 if ((c->callback == callback) && (c->priv == priv))
697 int target_call_event_callbacks(target_t *target, enum target_event event)
699 target_event_callback_t *callback = target_event_callbacks;
700 target_event_callback_t *next_callback;
702 if (event == TARGET_EVENT_HALTED)
704 /* execute early halted first */
705 target_call_event_callbacks(target, TARGET_EVENT_EARLY_HALTED);
709 LOG_DEBUG("target event %i (%s)",
711 Jim_Nvp_value2name_simple( nvp_target_event, event )->name );
713 target_handle_event( target, event );
717 next_callback = callback->next;
718 callback->callback(target, event, callback->priv);
719 callback = next_callback;
725 static int target_call_timer_callbacks_check_time(int checktime)
727 target_timer_callback_t *callback = target_timer_callbacks;
728 target_timer_callback_t *next_callback;
733 gettimeofday(&now, NULL);
737 next_callback = callback->next;
739 if ((!checktime&&callback->periodic)||
740 (((now.tv_sec >= callback->when.tv_sec) && (now.tv_usec >= callback->when.tv_usec))
741 || (now.tv_sec > callback->when.tv_sec)))
743 if(callback->callback != NULL)
745 callback->callback(callback->priv);
746 if (callback->periodic)
748 int time_ms = callback->time_ms;
749 callback->when.tv_usec = now.tv_usec + (time_ms % 1000) * 1000;
750 time_ms -= (time_ms % 1000);
751 callback->when.tv_sec = now.tv_sec + time_ms / 1000;
752 if (callback->when.tv_usec > 1000000)
754 callback->when.tv_usec = callback->when.tv_usec - 1000000;
755 callback->when.tv_sec += 1;
761 if((retval = target_unregister_timer_callback(callback->callback, callback->priv)) != ERROR_OK)
767 callback = next_callback;
773 int target_call_timer_callbacks(void)
775 return target_call_timer_callbacks_check_time(1);
778 /* invoke periodic callbacks immediately */
779 int target_call_timer_callbacks_now(void)
781 return target_call_timer_callbacks_check_time(0);
784 int target_alloc_working_area(struct target_s *target, u32 size, working_area_t **area)
786 working_area_t *c = target->working_areas;
787 working_area_t *new_wa = NULL;
789 /* Reevaluate working area address based on MMU state*/
790 if (target->working_areas == NULL)
794 retval = target->type->mmu(target, &enabled);
795 if (retval != ERROR_OK)
801 target->working_area = target->working_area_virt;
805 target->working_area = target->working_area_phys;
809 /* only allocate multiples of 4 byte */
812 LOG_ERROR("BUG: code tried to allocate unaligned number of bytes, padding");
813 size = CEIL(size, 4);
816 /* see if there's already a matching working area */
819 if ((c->free) && (c->size == size))
827 /* if not, allocate a new one */
830 working_area_t **p = &target->working_areas;
831 u32 first_free = target->working_area;
832 u32 free_size = target->working_area_size;
834 LOG_DEBUG("allocating new working area");
836 c = target->working_areas;
839 first_free += c->size;
840 free_size -= c->size;
845 if (free_size < size)
847 LOG_WARNING("not enough working area available(requested %d, free %d)", size, free_size);
848 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
851 new_wa = malloc(sizeof(working_area_t));
854 new_wa->address = first_free;
856 if (target->backup_working_area)
859 new_wa->backup = malloc(new_wa->size);
860 if((retval = target->type->read_memory(target, new_wa->address, 4, new_wa->size / 4, new_wa->backup)) != ERROR_OK)
862 free(new_wa->backup);
869 new_wa->backup = NULL;
872 /* put new entry in list */
876 /* mark as used, and return the new (reused) area */
886 int target_free_working_area_restore(struct target_s *target, working_area_t *area, int restore)
891 if (restore&&target->backup_working_area)
894 if((retval = target->type->write_memory(target, area->address, 4, area->size / 4, area->backup)) != ERROR_OK)
900 /* mark user pointer invalid */
907 int target_free_working_area(struct target_s *target, working_area_t *area)
909 return target_free_working_area_restore(target, area, 1);
912 /* free resources and restore memory, if restoring memory fails,
913 * free up resources anyway
915 void target_free_all_working_areas_restore(struct target_s *target, int restore)
917 working_area_t *c = target->working_areas;
921 working_area_t *next = c->next;
922 target_free_working_area_restore(target, c, restore);
932 target->working_areas = NULL;
935 void target_free_all_working_areas(struct target_s *target)
937 target_free_all_working_areas_restore(target, 1);
940 int target_register_commands(struct command_context_s *cmd_ctx)
943 register_command(cmd_ctx, NULL, "targets", handle_targets_command, COMMAND_EXEC, NULL);
944 register_command(cmd_ctx, NULL, "working_area", handle_working_area_command, COMMAND_ANY, "working_area <target#> <address> <size> <'backup'|'nobackup'> [virtual address]");
945 register_command(cmd_ctx, NULL, "virt2phys", handle_virt2phys_command, COMMAND_ANY, "virt2phys <virtual address>");
946 register_command(cmd_ctx, NULL, "profile", handle_profile_command, COMMAND_EXEC, "PRELIMINARY! - profile <seconds> <gmon.out>");
948 register_jim(cmd_ctx, "target", jim_target, "configure target" );
951 /* script procedures */
952 register_jim(cmd_ctx, "ocd_mem2array", jim_mem2array, "read memory and return as a TCL array for script processing");
953 register_jim(cmd_ctx, "ocd_array2mem", jim_array2mem, "convert a TCL array to memory locations and write the values");
957 int target_arch_state(struct target_s *target)
962 LOG_USER("No target has been configured");
966 LOG_USER("target state: %s",
967 Jim_Nvp_value2name_simple(nvp_target_state,target->state)->name);
969 if (target->state!=TARGET_HALTED)
972 retval=target->type->arch_state(target);
976 /* Single aligned words are guaranteed to use 16 or 32 bit access
977 * mode respectively, otherwise data is handled as quickly as
980 int target_write_buffer(struct target_s *target, u32 address, u32 size, u8 *buffer)
983 LOG_DEBUG("writing buffer of %i byte at 0x%8.8x", size, address);
985 if (!target->type->examined)
987 LOG_ERROR("Target not examined yet");
991 if ((address + size - 1) < address)
993 /* GDB can request this when e.g. PC is 0xfffffffc*/
994 LOG_ERROR("address+size wrapped(0x%08x, 0x%08x)", address, size);
998 if (((address % 2) == 0) && (size == 2))
1000 return target->type->write_memory(target, address, 2, 1, buffer);
1003 /* handle unaligned head bytes */
1006 int unaligned = 4 - (address % 4);
1008 if (unaligned > size)
1011 if ((retval = target->type->write_memory(target, address, 1, unaligned, buffer)) != ERROR_OK)
1014 buffer += unaligned;
1015 address += unaligned;
1019 /* handle aligned words */
1022 int aligned = size - (size % 4);
1024 /* use bulk writes above a certain limit. This may have to be changed */
1027 if ((retval = target->type->bulk_write_memory(target, address, aligned / 4, buffer)) != ERROR_OK)
1032 if ((retval = target->type->write_memory(target, address, 4, aligned / 4, buffer)) != ERROR_OK)
1041 /* handle tail writes of less than 4 bytes */
1044 if ((retval = target->type->write_memory(target, address, 1, size, buffer)) != ERROR_OK)
1052 /* Single aligned words are guaranteed to use 16 or 32 bit access
1053 * mode respectively, otherwise data is handled as quickly as
1056 int target_read_buffer(struct target_s *target, u32 address, u32 size, u8 *buffer)
1059 LOG_DEBUG("reading buffer of %i byte at 0x%8.8x", size, address);
1061 if (!target->type->examined)
1063 LOG_ERROR("Target not examined yet");
1067 if ((address + size - 1) < address)
1069 /* GDB can request this when e.g. PC is 0xfffffffc*/
1070 LOG_ERROR("address+size wrapped(0x%08x, 0x%08x)", address, size);
1074 if (((address % 2) == 0) && (size == 2))
1076 return target->type->read_memory(target, address, 2, 1, buffer);
1079 /* handle unaligned head bytes */
1082 int unaligned = 4 - (address % 4);
1084 if (unaligned > size)
1087 if ((retval = target->type->read_memory(target, address, 1, unaligned, buffer)) != ERROR_OK)
1090 buffer += unaligned;
1091 address += unaligned;
1095 /* handle aligned words */
1098 int aligned = size - (size % 4);
1100 if ((retval = target->type->read_memory(target, address, 4, aligned / 4, buffer)) != ERROR_OK)
1108 /* handle tail writes of less than 4 bytes */
1111 if ((retval = target->type->read_memory(target, address, 1, size, buffer)) != ERROR_OK)
1118 int target_checksum_memory(struct target_s *target, u32 address, u32 size, u32* crc)
1124 if (!target->type->examined)
1126 LOG_ERROR("Target not examined yet");
1130 if ((retval = target->type->checksum_memory(target, address,
1131 size, &checksum)) != ERROR_OK)
1133 buffer = malloc(size);
1136 LOG_ERROR("error allocating buffer for section (%d bytes)", size);
1137 return ERROR_INVALID_ARGUMENTS;
1139 retval = target_read_buffer(target, address, size, buffer);
1140 if (retval != ERROR_OK)
1146 /* convert to target endianess */
1147 for (i = 0; i < (size/sizeof(u32)); i++)
1150 target_data = target_buffer_get_u32(target, &buffer[i*sizeof(u32)]);
1151 target_buffer_set_u32(target, &buffer[i*sizeof(u32)], target_data);
1154 retval = image_calculate_checksum( buffer, size, &checksum );
1163 int target_blank_check_memory(struct target_s *target, u32 address, u32 size, u32* blank)
1166 if (!target->type->examined)
1168 LOG_ERROR("Target not examined yet");
1172 if (target->type->blank_check_memory == 0)
1173 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
1175 retval = target->type->blank_check_memory(target, address, size, blank);
1180 int target_read_u32(struct target_s *target, u32 address, u32 *value)
1183 if (!target->type->examined)
1185 LOG_ERROR("Target not examined yet");
1189 int retval = target->type->read_memory(target, address, 4, 1, value_buf);
1191 if (retval == ERROR_OK)
1193 *value = target_buffer_get_u32(target, value_buf);
1194 LOG_DEBUG("address: 0x%8.8x, value: 0x%8.8x", address, *value);
1199 LOG_DEBUG("address: 0x%8.8x failed", address);
1205 int target_read_u16(struct target_s *target, u32 address, u16 *value)
1208 if (!target->type->examined)
1210 LOG_ERROR("Target not examined yet");
1214 int retval = target->type->read_memory(target, address, 2, 1, value_buf);
1216 if (retval == ERROR_OK)
1218 *value = target_buffer_get_u16(target, value_buf);
1219 LOG_DEBUG("address: 0x%8.8x, value: 0x%4.4x", address, *value);
1224 LOG_DEBUG("address: 0x%8.8x failed", address);
1230 int target_read_u8(struct target_s *target, u32 address, u8 *value)
1232 int retval = target->type->read_memory(target, address, 1, 1, value);
1233 if (!target->type->examined)
1235 LOG_ERROR("Target not examined yet");
1239 if (retval == ERROR_OK)
1241 LOG_DEBUG("address: 0x%8.8x, value: 0x%2.2x", address, *value);
1246 LOG_DEBUG("address: 0x%8.8x failed", address);
1252 int target_write_u32(struct target_s *target, u32 address, u32 value)
1256 if (!target->type->examined)
1258 LOG_ERROR("Target not examined yet");
1262 LOG_DEBUG("address: 0x%8.8x, value: 0x%8.8x", address, value);
1264 target_buffer_set_u32(target, value_buf, value);
1265 if ((retval = target->type->write_memory(target, address, 4, 1, value_buf)) != ERROR_OK)
1267 LOG_DEBUG("failed: %i", retval);
1273 int target_write_u16(struct target_s *target, u32 address, u16 value)
1277 if (!target->type->examined)
1279 LOG_ERROR("Target not examined yet");
1283 LOG_DEBUG("address: 0x%8.8x, value: 0x%8.8x", address, value);
1285 target_buffer_set_u16(target, value_buf, value);
1286 if ((retval = target->type->write_memory(target, address, 2, 1, value_buf)) != ERROR_OK)
1288 LOG_DEBUG("failed: %i", retval);
1294 int target_write_u8(struct target_s *target, u32 address, u8 value)
1297 if (!target->type->examined)
1299 LOG_ERROR("Target not examined yet");
1303 LOG_DEBUG("address: 0x%8.8x, value: 0x%2.2x", address, value);
1305 if ((retval = target->type->read_memory(target, address, 1, 1, &value)) != ERROR_OK)
1307 LOG_DEBUG("failed: %i", retval);
1313 int target_register_user_commands(struct command_context_s *cmd_ctx)
1315 int retval = ERROR_OK;
1316 register_command(cmd_ctx, NULL, "reg", handle_reg_command, COMMAND_EXEC, NULL);
1317 register_command(cmd_ctx, NULL, "poll", handle_poll_command, COMMAND_EXEC, "poll target state");
1318 register_command(cmd_ctx, NULL, "wait_halt", handle_wait_halt_command, COMMAND_EXEC, "wait for target halt [time (s)]");
1319 register_command(cmd_ctx, NULL, "halt", handle_halt_command, COMMAND_EXEC, "halt target");
1320 register_command(cmd_ctx, NULL, "resume", handle_resume_command, COMMAND_EXEC, "resume target [addr]");
1321 register_command(cmd_ctx, NULL, "step", handle_step_command, COMMAND_EXEC, "step one instruction from current PC or [addr]");
1322 register_command(cmd_ctx, NULL, "reset", handle_reset_command, COMMAND_EXEC, "reset target [run|halt|init] - default is run");
1323 register_command(cmd_ctx, NULL, "soft_reset_halt", handle_soft_reset_halt_command, COMMAND_EXEC, "halt the target and do a soft reset");
1325 register_command(cmd_ctx, NULL, "mdw", handle_md_command, COMMAND_EXEC, "display memory words <addr> [count]");
1326 register_command(cmd_ctx, NULL, "mdh", handle_md_command, COMMAND_EXEC, "display memory half-words <addr> [count]");
1327 register_command(cmd_ctx, NULL, "mdb", handle_md_command, COMMAND_EXEC, "display memory bytes <addr> [count]");
1329 register_command(cmd_ctx, NULL, "mww", handle_mw_command, COMMAND_EXEC, "write memory word <addr> <value> [count]");
1330 register_command(cmd_ctx, NULL, "mwh", handle_mw_command, COMMAND_EXEC, "write memory half-word <addr> <value> [count]");
1331 register_command(cmd_ctx, NULL, "mwb", handle_mw_command, COMMAND_EXEC, "write memory byte <addr> <value> [count]");
1333 register_command(cmd_ctx, NULL, "bp", handle_bp_command, COMMAND_EXEC, "set breakpoint <address> <length> [hw]");
1334 register_command(cmd_ctx, NULL, "rbp", handle_rbp_command, COMMAND_EXEC, "remove breakpoint <adress>");
1335 register_command(cmd_ctx, NULL, "wp", handle_wp_command, COMMAND_EXEC, "set watchpoint <address> <length> <r/w/a> [value] [mask]");
1336 register_command(cmd_ctx, NULL, "rwp", handle_rwp_command, COMMAND_EXEC, "remove watchpoint <adress>");
1338 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]");
1339 register_command(cmd_ctx, NULL, "dump_image", handle_dump_image_command, COMMAND_EXEC, "dump_image <file> <address> <size>");
1340 register_command(cmd_ctx, NULL, "verify_image", handle_verify_image_command, COMMAND_EXEC, "verify_image <file> [offset] [type]");
1342 if((retval = target_request_register_commands(cmd_ctx)) != ERROR_OK)
1344 if((retval = trace_register_commands(cmd_ctx)) != ERROR_OK)
1351 int handle_targets_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1354 target_t *target = all_targets;
1358 /* try as tcltarget name */
1359 for( target = all_targets ; target ; target++ ){
1360 if( target->cmd_name ){
1361 if( 0 == strcmp( args[0], target->cmd_name ) ){
1367 /* no match, try as number */
1369 int num = strtoul(args[0], &cp, 0 );
1371 /* then it was not a number */
1372 command_print( cmd_ctx, "Target: %s unknown, try one of:\n", args[0] );
1376 target = get_target_by_num( num );
1377 if( target == NULL ){
1378 command_print(cmd_ctx,"Target: %s is unknown, try one of:\n", args[0] );
1382 cmd_ctx->current_target = target->target_number;
1387 command_print(cmd_ctx, " CmdName Type Endian ChainPos State ");
1388 command_print(cmd_ctx, "-- ---------- ---------- ---------- -------- ----------");
1391 /* XX: abcdefghij abcdefghij abcdefghij abcdefghij */
1392 command_print(cmd_ctx, "%2d: %-10s %-10s %-10s %8d %s",
1393 target->target_number,
1396 Jim_Nvp_value2name_simple( nvp_target_endian, target->endianness )->name,
1397 target->chain_position,
1398 Jim_Nvp_value2name_simple( nvp_target_state, target->state )->name );
1399 target = target->next;
1407 int handle_working_area_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1409 int retval = ERROR_OK;
1410 target_t *target = NULL;
1412 if ((argc < 4) || (argc > 5))
1414 return ERROR_COMMAND_SYNTAX_ERROR;
1417 target = get_target_by_num(strtoul(args[0], NULL, 0));
1420 return ERROR_COMMAND_SYNTAX_ERROR;
1422 target_free_all_working_areas(target);
1424 target->working_area_phys = target->working_area_virt = strtoul(args[1], NULL, 0);
1427 target->working_area_virt = strtoul(args[4], NULL, 0);
1429 target->working_area_size = strtoul(args[2], NULL, 0);
1431 if (strcmp(args[3], "backup") == 0)
1433 target->backup_working_area = 1;
1435 else if (strcmp(args[3], "nobackup") == 0)
1437 target->backup_working_area = 0;
1441 LOG_ERROR("unrecognized <backup|nobackup> argument (%s)", args[3]);
1442 return ERROR_COMMAND_SYNTAX_ERROR;
1449 /* process target state changes */
1450 int handle_target(void *priv)
1452 int retval = ERROR_OK;
1453 target_t *target = all_targets;
1457 if (target_continous_poll)
1459 /* polling may fail silently until the target has been examined */
1460 if((retval = target_poll(target)) != ERROR_OK)
1464 target = target->next;
1470 int handle_reg_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1479 target = get_current_target(cmd_ctx);
1481 /* list all available registers for the current target */
1484 reg_cache_t *cache = target->reg_cache;
1490 for (i = 0; i < cache->num_regs; i++)
1492 value = buf_to_str(cache->reg_list[i].value, cache->reg_list[i].size, 16);
1493 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);
1496 cache = cache->next;
1502 /* access a single register by its ordinal number */
1503 if ((args[0][0] >= '0') && (args[0][0] <= '9'))
1505 int num = strtoul(args[0], NULL, 0);
1506 reg_cache_t *cache = target->reg_cache;
1512 for (i = 0; i < cache->num_regs; i++)
1516 reg = &cache->reg_list[i];
1522 cache = cache->next;
1527 command_print(cmd_ctx, "%i is out of bounds, the current target has only %i registers (0 - %i)", num, count, count - 1);
1530 } else /* access a single register by its name */
1532 reg = register_get_by_name(target->reg_cache, args[0], 1);
1536 command_print(cmd_ctx, "register %s not found in current target", args[0]);
1541 /* display a register */
1542 if ((argc == 1) || ((argc == 2) && !((args[1][0] >= '0') && (args[1][0] <= '9'))))
1544 if ((argc == 2) && (strcmp(args[1], "force") == 0))
1547 if (reg->valid == 0)
1549 reg_arch_type_t *arch_type = register_get_arch_type(reg->arch_type);
1550 arch_type->get(reg);
1552 value = buf_to_str(reg->value, reg->size, 16);
1553 command_print(cmd_ctx, "%s (/%i): 0x%s", reg->name, reg->size, value);
1558 /* set register value */
1561 u8 *buf = malloc(CEIL(reg->size, 8));
1562 str_to_buf(args[1], strlen(args[1]), buf, reg->size, 0);
1564 reg_arch_type_t *arch_type = register_get_arch_type(reg->arch_type);
1565 arch_type->set(reg, buf);
1567 value = buf_to_str(reg->value, reg->size, 16);
1568 command_print(cmd_ctx, "%s (/%i): 0x%s", reg->name, reg->size, value);
1576 command_print(cmd_ctx, "usage: reg <#|name> [value]");
1582 int handle_poll_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1584 int retval = ERROR_OK;
1585 target_t *target = get_current_target(cmd_ctx);
1589 if((retval = target_poll(target)) != ERROR_OK)
1591 if((retval = target_arch_state(target)) != ERROR_OK)
1597 if (strcmp(args[0], "on") == 0)
1599 target_continous_poll = 1;
1601 else if (strcmp(args[0], "off") == 0)
1603 target_continous_poll = 0;
1607 command_print(cmd_ctx, "arg is \"on\" or \"off\"");
1611 return ERROR_COMMAND_SYNTAX_ERROR;
1618 int handle_wait_halt_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1626 ms = strtoul(args[0], &end, 0) * 1000;
1629 command_print(cmd_ctx, "usage: %s [seconds]", cmd);
1633 target_t *target = get_current_target(cmd_ctx);
1635 return target_wait_state(target, TARGET_HALTED, ms);
1638 int target_wait_state(target_t *target, enum target_state state, int ms)
1641 struct timeval timeout, now;
1643 gettimeofday(&timeout, NULL);
1644 timeval_add_time(&timeout, 0, ms * 1000);
1648 if ((retval=target_poll(target))!=ERROR_OK)
1651 if (target->state == state)
1658 LOG_DEBUG("waiting for target %s...",
1659 Jim_Nvp_value2name_simple(nvp_target_state,state)->name);
1662 gettimeofday(&now, NULL);
1663 if ((now.tv_sec > timeout.tv_sec) || ((now.tv_sec == timeout.tv_sec) && (now.tv_usec >= timeout.tv_usec)))
1665 LOG_ERROR("timed out while waiting for target %s",
1666 Jim_Nvp_value2name_simple(nvp_target_state,state)->name);
1674 int handle_halt_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1677 target_t *target = get_current_target(cmd_ctx);
1681 if ((retval = target_halt(target)) != ERROR_OK)
1686 return handle_wait_halt_command(cmd_ctx, cmd, args, argc);
1689 int handle_soft_reset_halt_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1691 target_t *target = get_current_target(cmd_ctx);
1693 LOG_USER("requesting target halt and executing a soft reset");
1695 target->type->soft_reset_halt(target);
1700 int handle_reset_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1703 enum target_reset_mode reset_mode = RESET_RUN;
1707 n = Jim_Nvp_name2value_simple( nvp_reset_modes, args[0] );
1708 if( (n->name == NULL) || (n->value == RESET_UNKNOWN) ){
1709 return ERROR_COMMAND_SYNTAX_ERROR;
1711 reset_mode = n->value;
1714 /* reset *all* targets */
1715 return target_process_reset(cmd_ctx, reset_mode);
1719 int handle_resume_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1722 target_t *target = get_current_target(cmd_ctx);
1724 target_handle_event( target, TARGET_EVENT_OLD_pre_resume );
1727 retval = target_resume(target, 1, 0, 1, 0); /* current pc, addr = 0, handle breakpoints, not debugging */
1729 retval = target_resume(target, 0, strtoul(args[0], NULL, 0), 1, 0); /* addr = args[0], handle breakpoints, not debugging */
1732 retval = ERROR_COMMAND_SYNTAX_ERROR;
1738 int handle_step_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1740 target_t *target = get_current_target(cmd_ctx);
1745 target->type->step(target, 1, 0, 1); /* current pc, addr = 0, handle breakpoints */
1748 target->type->step(target, 0, strtoul(args[0], NULL, 0), 1); /* addr = args[0], handle breakpoints */
1753 int handle_md_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1755 const int line_bytecnt = 32;
1768 target_t *target = get_current_target(cmd_ctx);
1774 count = strtoul(args[1], NULL, 0);
1776 address = strtoul(args[0], NULL, 0);
1782 size = 4; line_modulo = line_bytecnt / 4;
1785 size = 2; line_modulo = line_bytecnt / 2;
1788 size = 1; line_modulo = line_bytecnt / 1;
1794 buffer = calloc(count, size);
1795 retval = target->type->read_memory(target, address, size, count, buffer);
1796 if (retval == ERROR_OK)
1800 for (i = 0; i < count; i++)
1802 if (i%line_modulo == 0)
1803 output_len += snprintf(output + output_len, 128 - output_len, "0x%8.8x: ", address + (i*size));
1808 output_len += snprintf(output + output_len, 128 - output_len, "%8.8x ", target_buffer_get_u32(target, &buffer[i*4]));
1811 output_len += snprintf(output + output_len, 128 - output_len, "%4.4x ", target_buffer_get_u16(target, &buffer[i*2]));
1814 output_len += snprintf(output + output_len, 128 - output_len, "%2.2x ", buffer[i*1]);
1818 if ((i%line_modulo == line_modulo-1) || (i == count - 1))
1820 command_print(cmd_ctx, output);
1831 int handle_mw_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1838 target_t *target = get_current_target(cmd_ctx);
1841 if ((argc < 2) || (argc > 3))
1842 return ERROR_COMMAND_SYNTAX_ERROR;
1844 address = strtoul(args[0], NULL, 0);
1845 value = strtoul(args[1], NULL, 0);
1847 count = strtoul(args[2], NULL, 0);
1853 target_buffer_set_u32(target, value_buf, value);
1857 target_buffer_set_u16(target, value_buf, value);
1861 value_buf[0] = value;
1864 return ERROR_COMMAND_SYNTAX_ERROR;
1866 for (i=0; i<count; i++)
1872 retval = target->type->write_memory(target, address + i*wordsize, 4, 1, value_buf);
1875 retval = target->type->write_memory(target, address + i*wordsize, 2, 1, value_buf);
1878 retval = target->type->write_memory(target, address + i*wordsize, 1, 1, value_buf);
1885 if (retval!=ERROR_OK)
1895 int handle_load_image_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1901 u32 max_address=0xffffffff;
1903 int retval, retvaltemp;
1907 duration_t duration;
1908 char *duration_text;
1910 target_t *target = get_current_target(cmd_ctx);
1912 if ((argc < 1)||(argc > 5))
1914 return ERROR_COMMAND_SYNTAX_ERROR;
1917 /* a base address isn't always necessary, default to 0x0 (i.e. don't relocate) */
1920 image.base_address_set = 1;
1921 image.base_address = strtoul(args[1], NULL, 0);
1925 image.base_address_set = 0;
1929 image.start_address_set = 0;
1933 min_address=strtoul(args[3], NULL, 0);
1937 max_address=strtoul(args[4], NULL, 0)+min_address;
1940 if (min_address>max_address)
1942 return ERROR_COMMAND_SYNTAX_ERROR;
1946 duration_start_measure(&duration);
1948 if (image_open(&image, args[0], (argc >= 3) ? args[2] : NULL) != ERROR_OK)
1955 for (i = 0; i < image.num_sections; i++)
1957 buffer = malloc(image.sections[i].size);
1960 command_print(cmd_ctx, "error allocating buffer for section (%d bytes)", image.sections[i].size);
1964 if ((retval = image_read_section(&image, i, 0x0, image.sections[i].size, buffer, &buf_cnt)) != ERROR_OK)
1974 /* DANGER!!! beware of unsigned comparision here!!! */
1976 if ((image.sections[i].base_address+buf_cnt>=min_address)&&
1977 (image.sections[i].base_address<max_address))
1979 if (image.sections[i].base_address<min_address)
1981 /* clip addresses below */
1982 offset+=min_address-image.sections[i].base_address;
1986 if (image.sections[i].base_address+buf_cnt>max_address)
1988 length-=(image.sections[i].base_address+buf_cnt)-max_address;
1991 if ((retval = target_write_buffer(target, image.sections[i].base_address+offset, length, buffer+offset)) != ERROR_OK)
1996 image_size += length;
1997 command_print(cmd_ctx, "%u byte written at address 0x%8.8x", length, image.sections[i].base_address+offset);
2003 if((retvaltemp = duration_stop_measure(&duration, &duration_text)) != ERROR_OK)
2005 image_close(&image);
2009 if (retval==ERROR_OK)
2011 command_print(cmd_ctx, "downloaded %u byte in %s", image_size, duration_text);
2013 free(duration_text);
2015 image_close(&image);
2021 int handle_dump_image_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2028 int retval=ERROR_OK, retvaltemp;
2030 duration_t duration;
2031 char *duration_text;
2033 target_t *target = get_current_target(cmd_ctx);
2037 command_print(cmd_ctx, "usage: dump_image <filename> <address> <size>");
2041 address = strtoul(args[1], NULL, 0);
2042 size = strtoul(args[2], NULL, 0);
2044 if ((address & 3) || (size & 3))
2046 command_print(cmd_ctx, "only 32-bit aligned address and size are supported");
2050 if (fileio_open(&fileio, args[0], FILEIO_WRITE, FILEIO_BINARY) != ERROR_OK)
2055 duration_start_measure(&duration);
2060 u32 this_run_size = (size > 560) ? 560 : size;
2062 retval = target->type->read_memory(target, address, 4, this_run_size / 4, buffer);
2063 if (retval != ERROR_OK)
2068 retval = fileio_write(&fileio, this_run_size, buffer, &size_written);
2069 if (retval != ERROR_OK)
2074 size -= this_run_size;
2075 address += this_run_size;
2078 if((retvaltemp = fileio_close(&fileio)) != ERROR_OK)
2081 if((retvaltemp = duration_stop_measure(&duration, &duration_text)) != ERROR_OK)
2084 if (retval==ERROR_OK)
2086 command_print(cmd_ctx, "dumped %"PRIi64" byte in %s", fileio.size, duration_text);
2088 free(duration_text);
2093 int handle_verify_image_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2099 int retval, retvaltemp;
2101 u32 mem_checksum = 0;
2105 duration_t duration;
2106 char *duration_text;
2108 target_t *target = get_current_target(cmd_ctx);
2112 return ERROR_COMMAND_SYNTAX_ERROR;
2117 LOG_ERROR("no target selected");
2121 duration_start_measure(&duration);
2125 image.base_address_set = 1;
2126 image.base_address = strtoul(args[1], NULL, 0);
2130 image.base_address_set = 0;
2131 image.base_address = 0x0;
2134 image.start_address_set = 0;
2136 if ((retval=image_open(&image, args[0], (argc == 3) ? args[2] : NULL)) != ERROR_OK)
2143 for (i = 0; i < image.num_sections; i++)
2145 buffer = malloc(image.sections[i].size);
2148 command_print(cmd_ctx, "error allocating buffer for section (%d bytes)", image.sections[i].size);
2151 if ((retval = image_read_section(&image, i, 0x0, image.sections[i].size, buffer, &buf_cnt)) != ERROR_OK)
2157 /* calculate checksum of image */
2158 image_calculate_checksum( buffer, buf_cnt, &checksum );
2160 retval = target_checksum_memory(target, image.sections[i].base_address, buf_cnt, &mem_checksum);
2161 if( retval != ERROR_OK )
2167 if( checksum != mem_checksum )
2169 /* failed crc checksum, fall back to a binary compare */
2172 command_print(cmd_ctx, "checksum mismatch - attempting binary compare");
2174 data = (u8*)malloc(buf_cnt);
2176 /* Can we use 32bit word accesses? */
2178 int count = buf_cnt;
2179 if ((count % 4) == 0)
2184 retval = target->type->read_memory(target, image.sections[i].base_address, size, count, data);
2185 if (retval == ERROR_OK)
2188 for (t = 0; t < buf_cnt; t++)
2190 if (data[t] != buffer[t])
2192 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]);
2209 image_size += buf_cnt;
2213 if((retvaltemp = duration_stop_measure(&duration, &duration_text)) != ERROR_OK)
2215 image_close(&image);
2219 if (retval==ERROR_OK)
2221 command_print(cmd_ctx, "verified %u bytes in %s", image_size, duration_text);
2223 free(duration_text);
2225 image_close(&image);
2230 int handle_bp_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2233 target_t *target = get_current_target(cmd_ctx);
2237 breakpoint_t *breakpoint = target->breakpoints;
2241 if (breakpoint->type == BKPT_SOFT)
2243 char* buf = buf_to_str(breakpoint->orig_instr, breakpoint->length, 16);
2244 command_print(cmd_ctx, "0x%8.8x, 0x%x, %i, 0x%s", breakpoint->address, breakpoint->length, breakpoint->set, buf);
2249 command_print(cmd_ctx, "0x%8.8x, 0x%x, %i", breakpoint->address, breakpoint->length, breakpoint->set);
2251 breakpoint = breakpoint->next;
2259 length = strtoul(args[1], NULL, 0);
2262 if (strcmp(args[2], "hw") == 0)
2265 if ((retval = breakpoint_add(target, strtoul(args[0], NULL, 0), length, hw)) != ERROR_OK)
2267 LOG_ERROR("Failure setting breakpoints");
2271 command_print(cmd_ctx, "breakpoint added at address 0x%8.8x", strtoul(args[0], NULL, 0));
2276 command_print(cmd_ctx, "usage: bp <address> <length> ['hw']");
2282 int handle_rbp_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2284 target_t *target = get_current_target(cmd_ctx);
2287 breakpoint_remove(target, strtoul(args[0], NULL, 0));
2292 int handle_wp_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2294 target_t *target = get_current_target(cmd_ctx);
2299 watchpoint_t *watchpoint = target->watchpoints;
2303 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);
2304 watchpoint = watchpoint->next;
2309 enum watchpoint_rw type = WPT_ACCESS;
2310 u32 data_value = 0x0;
2311 u32 data_mask = 0xffffffff;
2327 command_print(cmd_ctx, "usage: wp <address> <length> [r/w/a] [value] [mask]");
2333 data_value = strtoul(args[3], NULL, 0);
2337 data_mask = strtoul(args[4], NULL, 0);
2340 if ((retval = watchpoint_add(target, strtoul(args[0], NULL, 0),
2341 strtoul(args[1], NULL, 0), type, data_value, data_mask)) != ERROR_OK)
2343 LOG_ERROR("Failure setting breakpoints");
2348 command_print(cmd_ctx, "usage: wp <address> <length> [r/w/a] [value] [mask]");
2354 int handle_rwp_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2356 target_t *target = get_current_target(cmd_ctx);
2359 watchpoint_remove(target, strtoul(args[0], NULL, 0));
2364 int handle_virt2phys_command(command_context_t *cmd_ctx, char *cmd, char **args, int argc)
2367 target_t *target = get_current_target(cmd_ctx);
2373 return ERROR_COMMAND_SYNTAX_ERROR;
2375 va = strtoul(args[0], NULL, 0);
2377 retval = target->type->virt2phys(target, va, &pa);
2378 if (retval == ERROR_OK)
2380 command_print(cmd_ctx, "Physical address 0x%08x", pa);
2384 /* lower levels will have logged a detailed error which is
2385 * forwarded to telnet/GDB session.
2390 static void writeLong(FILE *f, int l)
2395 char c=(l>>(i*8))&0xff;
2396 fwrite(&c, 1, 1, f);
2400 static void writeString(FILE *f, char *s)
2402 fwrite(s, 1, strlen(s), f);
2407 // Dump a gmon.out histogram file.
2408 static void writeGmon(u32 *samples, int sampleNum, char *filename)
2411 FILE *f=fopen(filename, "w");
2414 fwrite("gmon", 1, 4, f);
2415 writeLong(f, 0x00000001); // Version
2416 writeLong(f, 0); // padding
2417 writeLong(f, 0); // padding
2418 writeLong(f, 0); // padding
2420 fwrite("", 1, 1, f); // GMON_TAG_TIME_HIST
2422 // figure out bucket size
2425 for (i=0; i<sampleNum; i++)
2437 int addressSpace=(max-min+1);
2439 static int const maxBuckets=256*1024; // maximum buckets.
2440 int length=addressSpace;
2441 if (length > maxBuckets)
2445 int *buckets=malloc(sizeof(int)*length);
2451 memset(buckets, 0, sizeof(int)*length);
2452 for (i=0; i<sampleNum;i++)
2454 u32 address=samples[i];
2455 long long a=address-min;
2456 long long b=length-1;
2457 long long c=addressSpace-1;
2458 int index=(a*b)/c; // danger!!!! int32 overflows
2462 // append binary memory gmon.out &profile_hist_hdr ((char*)&profile_hist_hdr + sizeof(struct gmon_hist_hdr))
2463 writeLong(f, min); // low_pc
2464 writeLong(f, max); // high_pc
2465 writeLong(f, length); // # of samples
2466 writeLong(f, 64000000); // 64MHz
2467 writeString(f, "seconds");
2468 for (i=0; i<(15-strlen("seconds")); i++)
2470 fwrite("", 1, 1, f); // padding
2472 writeString(f, "s");
2474 // append binary memory gmon.out profile_hist_data (profile_hist_data + profile_hist_hdr.hist_size)
2476 char *data=malloc(2*length);
2479 for (i=0; i<length;i++)
2488 data[i*2+1]=(val>>8)&0xff;
2491 fwrite(data, 1, length*2, f);
2501 /* profiling samples the CPU PC as quickly as OpenOCD is able, which will be used as a random sampling of PC */
2502 int handle_profile_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2504 target_t *target = get_current_target(cmd_ctx);
2505 struct timeval timeout, now;
2507 gettimeofday(&timeout, NULL);
2510 return ERROR_COMMAND_SYNTAX_ERROR;
2513 timeval_add_time(&timeout, strtoul(args[0], &end, 0), 0);
2519 command_print(cmd_ctx, "Starting profiling. Halting and resuming the target as often as we can...");
2521 static const int maxSample=10000;
2522 u32 *samples=malloc(sizeof(u32)*maxSample);
2527 int retval=ERROR_OK;
2528 // hopefully it is safe to cache! We want to stop/restart as quickly as possible.
2529 reg_t *reg = register_get_by_name(target->reg_cache, "pc", 1);
2533 target_poll(target);
2534 if (target->state == TARGET_HALTED)
2536 u32 t=*((u32 *)reg->value);
2537 samples[numSamples++]=t;
2538 retval = target_resume(target, 1, 0, 0, 0); /* current pc, addr = 0, do not handle breakpoints, not debugging */
2539 target_poll(target);
2540 alive_sleep(10); // sleep 10ms, i.e. <100 samples/second.
2541 } else if (target->state == TARGET_RUNNING)
2543 // We want to quickly sample the PC.
2544 if((retval = target_halt(target)) != ERROR_OK)
2551 command_print(cmd_ctx, "Target not halted or running");
2555 if (retval!=ERROR_OK)
2560 gettimeofday(&now, NULL);
2561 if ((numSamples>=maxSample) || ((now.tv_sec >= timeout.tv_sec) && (now.tv_usec >= timeout.tv_usec)))
2563 command_print(cmd_ctx, "Profiling completed. %d samples.", numSamples);
2564 if((retval = target_poll(target)) != ERROR_OK)
2569 if (target->state == TARGET_HALTED)
2571 target_resume(target, 1, 0, 0, 0); /* current pc, addr = 0, do not handle breakpoints, not debugging */
2573 if((retval = target_poll(target)) != ERROR_OK)
2578 writeGmon(samples, numSamples, args[1]);
2579 command_print(cmd_ctx, "Wrote %s", args[1]);
2588 static int new_int_array_element(Jim_Interp * interp, const char *varname, int idx, u32 val)
2591 Jim_Obj *nameObjPtr, *valObjPtr;
2594 namebuf = alloc_printf("%s(%d)", varname, idx);
2598 nameObjPtr = Jim_NewStringObj(interp, namebuf, -1);
2599 valObjPtr = Jim_NewIntObj(interp, val);
2600 if (!nameObjPtr || !valObjPtr)
2606 Jim_IncrRefCount(nameObjPtr);
2607 Jim_IncrRefCount(valObjPtr);
2608 result = Jim_SetVariable(interp, nameObjPtr, valObjPtr);
2609 Jim_DecrRefCount(interp, nameObjPtr);
2610 Jim_DecrRefCount(interp, valObjPtr);
2612 /* printf("%s(%d) <= 0%08x\n", varname, idx, val); */
2616 static int jim_mem2array(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
2618 command_context_t *context;
2621 context = Jim_GetAssocData(interp, "context");
2622 if (context == NULL)
2624 LOG_ERROR("mem2array: no command context");
2627 target = get_current_target(context);
2630 LOG_ERROR("mem2array: no current target");
2634 return target_mem2array(interp, target, argc,argv);
2637 static int target_mem2array(Jim_Interp *interp, target_t *target, int argc, Jim_Obj *const *argv)
2645 const char *varname;
2647 int i, n, e, retval;
2649 /* argv[1] = name of array to receive the data
2650 * argv[2] = desired width
2651 * argv[3] = memory address
2652 * argv[4] = count of times to read
2655 Jim_WrongNumArgs(interp, 1, argv, "varname width addr nelems");
2658 varname = Jim_GetString(argv[1], &len);
2659 /* given "foo" get space for worse case "foo(%d)" .. add 20 */
2661 e = Jim_GetLong(interp, argv[2], &l);
2667 e = Jim_GetLong(interp, argv[3], &l);
2672 e = Jim_GetLong(interp, argv[4], &l);
2688 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
2689 Jim_AppendStrings( interp, Jim_GetResult(interp), "Invalid width param, must be 8/16/32", NULL );
2693 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
2694 Jim_AppendStrings(interp, Jim_GetResult(interp), "mem2array: zero width read?", NULL);
2697 if ((addr + (len * width)) < addr) {
2698 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
2699 Jim_AppendStrings(interp, Jim_GetResult(interp), "mem2array: addr + len - wraps to zero?", NULL);
2702 /* absurd transfer size? */
2704 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
2705 Jim_AppendStrings(interp, Jim_GetResult(interp), "mem2array: absurd > 64K item request", NULL);
2710 ((width == 2) && ((addr & 1) == 0)) ||
2711 ((width == 4) && ((addr & 3) == 0))) {
2715 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
2716 sprintf(buf, "mem2array address: 0x%08x is not aligned for %d byte reads", addr, width);
2717 Jim_AppendStrings(interp, Jim_GetResult(interp), buf , NULL);
2728 /* Slurp... in buffer size chunks */
2730 count = len; /* in objects.. */
2731 if (count > (sizeof(buffer)/width)) {
2732 count = (sizeof(buffer)/width);
2735 retval = target->type->read_memory( target, addr, width, count, buffer );
2736 if (retval != ERROR_OK) {
2738 LOG_ERROR("mem2array: Read @ 0x%08x, w=%d, cnt=%d, failed", addr, width, count);
2739 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
2740 Jim_AppendStrings(interp, Jim_GetResult(interp), "mem2array: cannot read memory", NULL);
2744 v = 0; /* shut up gcc */
2745 for (i = 0 ;i < count ;i++, n++) {
2748 v = target_buffer_get_u32(target, &buffer[i*width]);
2751 v = target_buffer_get_u16(target, &buffer[i*width]);
2754 v = buffer[i] & 0x0ff;
2757 new_int_array_element(interp, varname, n, v);
2763 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
2768 static int get_int_array_element(Jim_Interp * interp, const char *varname, int idx, u32 *val)
2771 Jim_Obj *nameObjPtr, *valObjPtr;
2775 namebuf = alloc_printf("%s(%d)", varname, idx);
2779 nameObjPtr = Jim_NewStringObj(interp, namebuf, -1);
2786 Jim_IncrRefCount(nameObjPtr);
2787 valObjPtr = Jim_GetVariable(interp, nameObjPtr, JIM_ERRMSG);
2788 Jim_DecrRefCount(interp, nameObjPtr);
2790 if (valObjPtr == NULL)
2793 result = Jim_GetLong(interp, valObjPtr, &l);
2794 /* printf("%s(%d) => 0%08x\n", varname, idx, val); */
2799 static int jim_array2mem(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
2801 command_context_t *context;
2804 context = Jim_GetAssocData(interp, "context");
2805 if (context == NULL){
2806 LOG_ERROR("array2mem: no command context");
2809 target = get_current_target(context);
2810 if (target == NULL){
2811 LOG_ERROR("array2mem: no current target");
2815 return target_array2mem( interp,target, argc, argv );
2819 static int target_array2mem(Jim_Interp *interp, target_t *target, int argc, Jim_Obj *const *argv)
2827 const char *varname;
2829 int i, n, e, retval;
2831 /* argv[1] = name of array to get the data
2832 * argv[2] = desired width
2833 * argv[3] = memory address
2834 * argv[4] = count to write
2837 Jim_WrongNumArgs(interp, 1, argv, "varname width addr nelems");
2840 varname = Jim_GetString(argv[1], &len);
2841 /* given "foo" get space for worse case "foo(%d)" .. add 20 */
2843 e = Jim_GetLong(interp, argv[2], &l);
2849 e = Jim_GetLong(interp, argv[3], &l);
2854 e = Jim_GetLong(interp, argv[4], &l);
2870 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
2871 Jim_AppendStrings( interp, Jim_GetResult(interp), "Invalid width param, must be 8/16/32", NULL );
2875 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
2876 Jim_AppendStrings(interp, Jim_GetResult(interp), "array2mem: zero width read?", NULL);
2879 if ((addr + (len * width)) < addr) {
2880 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
2881 Jim_AppendStrings(interp, Jim_GetResult(interp), "array2mem: addr + len - wraps to zero?", NULL);
2884 /* absurd transfer size? */
2886 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
2887 Jim_AppendStrings(interp, Jim_GetResult(interp), "array2mem: absurd > 64K item request", NULL);
2892 ((width == 2) && ((addr & 1) == 0)) ||
2893 ((width == 4) && ((addr & 3) == 0))) {
2897 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
2898 sprintf(buf, "array2mem address: 0x%08x is not aligned for %d byte reads", addr, width);
2899 Jim_AppendStrings(interp, Jim_GetResult(interp), buf , NULL);
2911 /* Slurp... in buffer size chunks */
2913 count = len; /* in objects.. */
2914 if (count > (sizeof(buffer)/width)) {
2915 count = (sizeof(buffer)/width);
2918 v = 0; /* shut up gcc */
2919 for (i = 0 ;i < count ;i++, n++) {
2920 get_int_array_element(interp, varname, n, &v);
2923 target_buffer_set_u32(target, &buffer[i*width], v);
2926 target_buffer_set_u16(target, &buffer[i*width], v);
2929 buffer[i] = v & 0x0ff;
2935 retval = target->type->write_memory(target, addr, width, count, buffer);
2936 if (retval != ERROR_OK) {
2938 LOG_ERROR("array2mem: Write @ 0x%08x, w=%d, cnt=%d, failed", addr, width, count);
2939 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
2940 Jim_AppendStrings(interp, Jim_GetResult(interp), "array2mem: cannot read memory", NULL);
2946 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
2952 target_all_handle_event( enum target_event e )
2957 LOG_DEBUG( "**all*targets: event: %d, %s",
2959 Jim_Nvp_value2name_simple( nvp_target_event, e )->name );
2961 target = all_targets;
2963 target_handle_event( target, e );
2964 target = target->next;
2969 target_handle_event( target_t *target, enum target_event e )
2971 target_event_action_t *teap;
2974 teap = target->event_action;
2978 if( teap->event == e ){
2980 LOG_DEBUG( "target: (%d) %s (%s) event: %d (%s) action: %s\n",
2981 target->target_number,
2985 Jim_Nvp_value2name_simple( nvp_target_event, e )->name,
2986 Jim_GetString( teap->body, NULL ) );
2987 if (Jim_EvalObj( interp, teap->body )!=JIM_OK)
2989 Jim_PrintErrorMessage(interp);
2995 LOG_DEBUG( "event: %d %s - no action",
2997 Jim_Nvp_value2name_simple( nvp_target_event, e )->name );
3001 enum target_cfg_param {
3004 TCFG_WORK_AREA_VIRT,
3005 TCFG_WORK_AREA_PHYS,
3006 TCFG_WORK_AREA_SIZE,
3007 TCFG_WORK_AREA_BACKUP,
3010 TCFG_CHAIN_POSITION,
3014 static Jim_Nvp nvp_config_opts[] = {
3015 { .name = "-type", .value = TCFG_TYPE },
3016 { .name = "-event", .value = TCFG_EVENT },
3017 { .name = "-work-area-virt", .value = TCFG_WORK_AREA_VIRT },
3018 { .name = "-work-area-phys", .value = TCFG_WORK_AREA_PHYS },
3019 { .name = "-work-area-size", .value = TCFG_WORK_AREA_SIZE },
3020 { .name = "-work-area-backup", .value = TCFG_WORK_AREA_BACKUP },
3021 { .name = "-endian" , .value = TCFG_ENDIAN },
3022 { .name = "-variant", .value = TCFG_VARIANT },
3023 { .name = "-chain-position", .value = TCFG_CHAIN_POSITION },
3025 { .name = NULL, .value = -1 }
3030 target_configure( Jim_GetOptInfo *goi,
3040 /* parse config or cget options ... */
3041 while( goi->argc > 0 ){
3042 Jim_SetEmptyResult( goi->interp );
3043 //Jim_GetOpt_Debug( goi );
3045 if( target->type->target_jim_configure ){
3046 /* target defines a configure function */
3047 /* target gets first dibs on parameters */
3048 e = (*(target->type->target_jim_configure))( target, goi );
3057 /* otherwise we 'continue' below */
3059 e = Jim_GetOpt_Nvp( goi, nvp_config_opts, &n );
3061 Jim_GetOpt_NvpUnknown( goi, nvp_config_opts, 0 );
3067 if( goi->isconfigure ){
3068 Jim_SetResult_sprintf( goi->interp, "not setable: %s", n->name );
3072 if( goi->argc != 0 ){
3073 Jim_WrongNumArgs( goi->interp, goi->argc, goi->argv, "NO PARAMS");
3077 Jim_SetResultString( goi->interp, target->type->name, -1 );
3081 if( goi->argc == 0 ){
3082 Jim_WrongNumArgs( goi->interp, goi->argc, goi->argv, "-event ?event-name? ...");
3086 e = Jim_GetOpt_Nvp( goi, nvp_target_event, &n );
3088 Jim_GetOpt_NvpUnknown( goi, nvp_target_event, 1 );
3092 if( goi->isconfigure ){
3093 if( goi->argc != 1 ){
3094 Jim_WrongNumArgs( goi->interp, goi->argc, goi->argv, "-event ?event-name? ?EVENT-BODY?");
3098 if( goi->argc != 0 ){
3099 Jim_WrongNumArgs(goi->interp, goi->argc, goi->argv, "-event ?event-name?");
3106 target_event_action_t *teap;
3108 teap = target->event_action;
3109 /* replace existing? */
3111 if( teap->event == n->value ){
3117 if( goi->isconfigure ){
3120 teap = calloc( 1, sizeof(*teap) );
3122 teap->event = n->value;
3123 Jim_GetOpt_Obj( goi, &o );
3125 Jim_DecrRefCount( interp, teap->body );
3127 teap->body = Jim_DuplicateObj( goi->interp, o );
3130 * Tcl/TK - "tk events" have a nice feature.
3131 * See the "BIND" command.
3132 * We should support that here.
3133 * You can specify %X and %Y in the event code.
3134 * The idea is: %T - target name.
3135 * The idea is: %N - target number
3136 * The idea is: %E - event name.
3138 Jim_IncrRefCount( teap->body );
3140 /* add to head of event list */
3141 teap->next = target->event_action;
3142 target->event_action = teap;
3143 Jim_SetEmptyResult(goi->interp);
3147 Jim_SetEmptyResult( goi->interp );
3149 Jim_SetResult( goi->interp, Jim_DuplicateObj( goi->interp, teap->body ) );
3156 case TCFG_WORK_AREA_VIRT:
3157 if( goi->isconfigure ){
3158 target_free_all_working_areas(target);
3159 e = Jim_GetOpt_Wide( goi, &w );
3163 target->working_area_virt = w;
3165 if( goi->argc != 0 ){
3169 Jim_SetResult( interp, Jim_NewIntObj( goi->interp, target->working_area_virt ) );
3173 case TCFG_WORK_AREA_PHYS:
3174 if( goi->isconfigure ){
3175 target_free_all_working_areas(target);
3176 e = Jim_GetOpt_Wide( goi, &w );
3180 target->working_area_phys = w;
3182 if( goi->argc != 0 ){
3186 Jim_SetResult( interp, Jim_NewIntObj( goi->interp, target->working_area_phys ) );
3190 case TCFG_WORK_AREA_SIZE:
3191 if( goi->isconfigure ){
3192 target_free_all_working_areas(target);
3193 e = Jim_GetOpt_Wide( goi, &w );
3197 target->working_area_size = w;
3199 if( goi->argc != 0 ){
3203 Jim_SetResult( interp, Jim_NewIntObj( goi->interp, target->working_area_size ) );
3207 case TCFG_WORK_AREA_BACKUP:
3208 if( goi->isconfigure ){
3209 target_free_all_working_areas(target);
3210 e = Jim_GetOpt_Wide( goi, &w );
3214 /* make this exactly 1 or 0 */
3215 target->backup_working_area = (!!w);
3217 if( goi->argc != 0 ){
3221 Jim_SetResult( interp, Jim_NewIntObj( goi->interp, target->working_area_size ) );
3222 /* loop for more e*/
3226 if( goi->isconfigure ){
3227 e = Jim_GetOpt_Nvp( goi, nvp_target_endian, &n );
3229 Jim_GetOpt_NvpUnknown( goi, nvp_target_endian, 1 );
3232 target->endianness = n->value;
3234 if( goi->argc != 0 ){
3238 n = Jim_Nvp_value2name_simple( nvp_target_endian, target->endianness );
3239 if( n->name == NULL ){
3240 target->endianness = TARGET_LITTLE_ENDIAN;
3241 n = Jim_Nvp_value2name_simple( nvp_target_endian, target->endianness );
3243 Jim_SetResultString( goi->interp, n->name, -1 );
3248 if( goi->isconfigure ){
3249 if( goi->argc < 1 ){
3250 Jim_SetResult_sprintf( goi->interp,
3255 if( target->variant ){
3256 free((void *)(target->variant));
3258 e = Jim_GetOpt_String( goi, &cp, NULL );
3259 target->variant = strdup(cp);
3261 if( goi->argc != 0 ){
3265 Jim_SetResultString( goi->interp, target->variant,-1 );
3268 case TCFG_CHAIN_POSITION:
3269 if( goi->isconfigure ){
3270 target_free_all_working_areas(target);
3271 e = Jim_GetOpt_Wide( goi, &w );
3275 /* make this exactly 1 or 0 */
3276 target->chain_position = w;
3278 if( goi->argc != 0 ){
3282 Jim_SetResult( interp, Jim_NewIntObj( goi->interp, target->chain_position ) );
3283 /* loop for more e*/
3286 } /* while( goi->argc ) */
3287 /* done - we return */
3292 /** this is the 'tcl' handler for the target specific command */
3294 tcl_target_func( Jim_Interp *interp,
3296 Jim_Obj *const *argv )
3304 struct command_context_s *cmd_ctx;
3312 TS_CMD_MWW, TS_CMD_MWH, TS_CMD_MWB,
3313 TS_CMD_MDW, TS_CMD_MDH, TS_CMD_MDB,
3314 TS_CMD_MRW, TS_CMD_MRH, TS_CMD_MRB,
3315 TS_CMD_MEM2ARRAY, TS_CMD_ARRAY2MEM,
3323 TS_CMD_INVOKE_EVENT,
3326 static const Jim_Nvp target_options[] = {
3327 { .name = "configure", .value = TS_CMD_CONFIGURE },
3328 { .name = "cget", .value = TS_CMD_CGET },
3329 { .name = "mww", .value = TS_CMD_MWW },
3330 { .name = "mwh", .value = TS_CMD_MWH },
3331 { .name = "mwb", .value = TS_CMD_MWB },
3332 { .name = "mdw", .value = TS_CMD_MDW },
3333 { .name = "mdh", .value = TS_CMD_MDH },
3334 { .name = "mdb", .value = TS_CMD_MDB },
3335 { .name = "mem2array", .value = TS_CMD_MEM2ARRAY },
3336 { .name = "array2mem", .value = TS_CMD_ARRAY2MEM },
3337 { .name = "eventlist", .value = TS_CMD_EVENTLIST },
3338 { .name = "curstate", .value = TS_CMD_CURSTATE },
3340 { .name = "arp_examine", .value = TS_CMD_EXAMINE },
3341 { .name = "arp_poll", .value = TS_CMD_POLL },
3342 { .name = "arp_reset", .value = TS_CMD_RESET },
3343 { .name = "arp_halt", .value = TS_CMD_HALT },
3344 { .name = "arp_waitstate", .value = TS_CMD_WAITSTATE },
3345 { .name = "invoke-event", .value = TS_CMD_INVOKE_EVENT },
3347 { .name = NULL, .value = -1 },
3351 /* go past the "command" */
3352 Jim_GetOpt_Setup( &goi, interp, argc-1, argv+1 );
3354 target = Jim_CmdPrivData( goi.interp );
3355 cmd_ctx = Jim_GetAssocData(goi.interp, "context");
3357 /* commands here are in an NVP table */
3358 e = Jim_GetOpt_Nvp( &goi, target_options, &n );
3360 Jim_GetOpt_NvpUnknown( &goi, target_options, 0 );
3363 // Assume blank result
3364 Jim_SetEmptyResult( goi.interp );
3367 case TS_CMD_CONFIGURE:
3369 Jim_WrongNumArgs( goi.interp, goi.argc, goi.argv, "missing: -option VALUE ...");
3372 goi.isconfigure = 1;
3373 return target_configure( &goi, target );
3375 // some things take params
3377 Jim_WrongNumArgs( goi.interp, 0, goi.argv, "missing: ?-option?");
3380 goi.isconfigure = 0;
3381 return target_configure( &goi, target );
3389 * argv[3] = optional count.
3392 if( (goi.argc == 3) || (goi.argc == 4) ){
3396 Jim_SetResult_sprintf( goi.interp, "expected: %s ADDR DATA [COUNT]", n->name );
3400 e = Jim_GetOpt_Wide( &goi, &a );
3405 e = Jim_GetOpt_Wide( &goi, &b );
3410 e = Jim_GetOpt_Wide( &goi, &c );
3420 target_buffer_set_u32( target, target_buf, b );
3424 target_buffer_set_u16( target, target_buf, b );
3428 target_buffer_set_u8( target, target_buf, b );
3432 for( x = 0 ; x < c ; x++ ){
3433 e = target->type->write_memory( target, a, b, 1, target_buf );
3434 if( e != ERROR_OK ){
3435 Jim_SetResult_sprintf( interp, "Error writing @ 0x%08x: %d\n", (int)(a), e );
3448 /* argv[0] = command
3450 * argv[2] = optional count
3452 if( (goi.argc == 2) || (goi.argc == 3) ){
3453 Jim_SetResult_sprintf( goi.interp, "expected: %s ADDR [COUNT]", n->name );
3456 e = Jim_GetOpt_Wide( &goi, &a );
3461 e = Jim_GetOpt_Wide( &goi, &c );
3468 b = 1; /* shut up gcc */
3481 /* convert to "bytes" */
3483 /* count is now in 'BYTES' */
3489 e = target->type->read_memory( target, a, b, y / b, target_buf );
3490 if( e != ERROR_OK ){
3491 Jim_SetResult_sprintf( interp, "error reading target @ 0x%08lx", (int)(a) );
3495 Jim_fprintf( interp, interp->cookie_stdout, "0x%08x ", (int)(a) );
3498 for( x = 0 ; (x < 16) && (x < y) ; x += 4 ){
3499 z = target_buffer_get_u32( target, &(target_buf[ x * 4 ]) );
3500 Jim_fprintf( interp, interp->cookie_stdout, "%08x ", (int)(z) );
3502 for( ; (x < 16) ; x += 4 ){
3503 Jim_fprintf( interp, interp->cookie_stdout, " " );
3507 for( x = 0 ; (x < 16) && (x < y) ; x += 2 ){
3508 z = target_buffer_get_u16( target, &(target_buf[ x * 2 ]) );
3509 Jim_fprintf( interp, interp->cookie_stdout, "%04x ", (int)(z) );
3511 for( ; (x < 16) ; x += 2 ){
3512 Jim_fprintf( interp, interp->cookie_stdout, " " );
3517 for( x = 0 ; (x < 16) && (x < y) ; x += 1 ){
3518 z = target_buffer_get_u8( target, &(target_buf[ x * 4 ]) );
3519 Jim_fprintf( interp, interp->cookie_stdout, "%02x ", (int)(z) );
3521 for( ; (x < 16) ; x += 1 ){
3522 Jim_fprintf( interp, interp->cookie_stdout, " " );
3526 /* ascii-ify the bytes */
3527 for( x = 0 ; x < y ; x++ ){
3528 if( (target_buf[x] >= 0x20) &&
3529 (target_buf[x] <= 0x7e) ){
3533 target_buf[x] = '.';
3538 target_buf[x] = ' ';
3543 /* print - with a newline */
3544 Jim_fprintf( interp, interp->cookie_stdout, "%s\n", target_buf );
3550 case TS_CMD_MEM2ARRAY:
3551 return target_mem2array( goi.interp, target, goi.argc, goi.argv );
3553 case TS_CMD_ARRAY2MEM:
3554 return target_array2mem( goi.interp, target, goi.argc, goi.argv );
3556 case TS_CMD_EXAMINE:
3558 Jim_WrongNumArgs( goi.interp, 2, argv, "[no parameters]");
3561 e = target->type->examine( target );
3562 if( e != ERROR_OK ){
3563 Jim_SetResult_sprintf( interp, "examine-fails: %d", e );
3569 Jim_WrongNumArgs( goi.interp, 2, argv, "[no parameters]");
3572 if( !(target->type->examined) ){
3573 e = ERROR_TARGET_NOT_EXAMINED;
3575 e = target->type->poll( target );
3577 if( e != ERROR_OK ){
3578 Jim_SetResult_sprintf( interp, "poll-fails: %d", e );
3585 if( goi.argc != 2 ){
3586 Jim_WrongNumArgs( interp, 2, argv, "t|f|assert|deassert BOOL");
3589 e = Jim_GetOpt_Nvp( &goi, nvp_assert, &n );
3591 Jim_GetOpt_NvpUnknown( &goi, nvp_assert, 1 );
3594 // the halt or not param
3595 e = Jim_GetOpt_Wide( &goi, &a);
3599 // determine if we should halt or not.
3600 target->reset_halt = !!a;
3601 // When this happens - all workareas are invalid.
3602 target_free_all_working_areas_restore(target, 0);
3605 if( n->value == NVP_ASSERT ){
3606 target->type->assert_reset( target );
3608 target->type->deassert_reset( target );
3613 Jim_WrongNumArgs( goi.interp, 0, argv, "halt [no parameters]");
3616 target->type->halt( target );
3618 case TS_CMD_WAITSTATE:
3619 // params: <name> statename timeoutmsecs
3620 if( goi.argc != 2 ){
3621 Jim_SetResult_sprintf( goi.interp, "%s STATENAME TIMEOUTMSECS", n->name );
3624 e = Jim_GetOpt_Nvp( &goi, nvp_target_state, &n );
3626 Jim_GetOpt_NvpUnknown( &goi, nvp_target_state,1 );
3629 e = Jim_GetOpt_Wide( &goi, &a );
3633 e = target_wait_state( target, n->value, a );
3634 if( e != ERROR_OK ){
3635 Jim_SetResult_sprintf( goi.interp,
3636 "target: %s wait %s fails (%d) %s",
3639 e, target_strerror_safe(e) );
3644 case TS_CMD_EVENTLIST:
3645 /* List for human, Events defined for this target.
3646 * scripts/programs should use 'name cget -event NAME'
3649 target_event_action_t *teap;
3650 teap = target->event_action;
3651 command_print( cmd_ctx, "Event actions for target (%d) %s\n",
3652 target->target_number,
3654 command_print( cmd_ctx, "%-25s | Body", "Event");
3655 command_print( cmd_ctx, "------------------------- | ----------------------------------------");
3657 command_print( cmd_ctx,
3659 Jim_Nvp_value2name_simple( nvp_target_event, teap->event )->name,
3660 Jim_GetString( teap->body, NULL ) );
3663 command_print( cmd_ctx, "***END***");
3666 case TS_CMD_CURSTATE:
3667 if( goi.argc != 0 ){
3668 Jim_WrongNumArgs( goi.interp, 0, argv, "[no parameters]");
3671 Jim_SetResultString( goi.interp,
3672 Jim_Nvp_value2name_simple(nvp_target_state,target->state)->name,-1);
3674 case TS_CMD_INVOKE_EVENT:
3675 if( goi.argc != 1 ){
3676 Jim_SetResult_sprintf( goi.interp, "%s ?EVENTNAME?",n->name);
3679 e = Jim_GetOpt_Nvp( &goi, nvp_target_event, &n );
3681 Jim_GetOpt_NvpUnknown( &goi, nvp_target_event, 1 );
3684 target_handle_event( target, n->value );
3692 target_create( Jim_GetOptInfo *goi )
3702 struct command_context_s *cmd_ctx;
3704 cmd_ctx = Jim_GetAssocData(goi->interp, "context");
3705 if( goi->argc < 3 ){
3706 Jim_WrongNumArgs( goi->interp, 1, goi->argv, "?name? ?type? ..options...");
3711 Jim_GetOpt_Obj( goi, &new_cmd );
3712 /* does this command exist? */
3713 cmd = Jim_GetCommand( goi->interp, new_cmd, JIM_ERRMSG );
3715 cp = Jim_GetString( new_cmd, NULL );
3716 Jim_SetResult_sprintf(goi->interp, "Command/target: %s Exists", cp);
3721 e = Jim_GetOpt_String( goi, &cp2, NULL );
3723 /* now does target type exist */
3724 for( x = 0 ; target_types[x] ; x++ ){
3725 if( 0 == strcmp( cp, target_types[x]->name ) ){
3730 if( target_types[x] == NULL ){
3731 Jim_SetResult_sprintf( goi->interp, "Unknown target type %s, try one of ", cp );
3732 for( x = 0 ; target_types[x] ; x++ ){
3733 if( target_types[x+1] ){
3734 Jim_AppendStrings( goi->interp,
3735 Jim_GetResult(goi->interp),
3736 target_types[x]->name,
3739 Jim_AppendStrings( goi->interp,
3740 Jim_GetResult(goi->interp),
3742 target_types[x]->name,NULL );
3750 target = calloc(1,sizeof(target_t));
3751 /* set target number */
3752 target->target_number = new_target_number();
3754 /* allocate memory for each unique target type */
3755 target->type = (target_type_t*)calloc(1,sizeof(target_type_t));
3757 memcpy( target->type, target_types[x], sizeof(target_type_t));
3759 /* will be set by "-endian" */
3760 target->endianness = TARGET_ENDIAN_UNKNOWN;
3762 target->working_area = 0x0;
3763 target->working_area_size = 0x0;
3764 target->working_areas = NULL;
3765 target->backup_working_area = 0;
3767 target->state = TARGET_UNKNOWN;
3768 target->debug_reason = DBG_REASON_UNDEFINED;
3769 target->reg_cache = NULL;
3770 target->breakpoints = NULL;
3771 target->watchpoints = NULL;
3772 target->next = NULL;
3773 target->arch_info = NULL;
3775 /* initialize trace information */
3776 target->trace_info = malloc(sizeof(trace_t));
3777 target->trace_info->num_trace_points = 0;
3778 target->trace_info->trace_points_size = 0;
3779 target->trace_info->trace_points = NULL;
3780 target->trace_info->trace_history_size = 0;
3781 target->trace_info->trace_history = NULL;
3782 target->trace_info->trace_history_pos = 0;
3783 target->trace_info->trace_history_overflowed = 0;
3785 target->dbgmsg = NULL;
3786 target->dbg_msg_enabled = 0;
3788 target->endianness = TARGET_ENDIAN_UNKNOWN;
3790 /* Do the rest as "configure" options */
3791 goi->isconfigure = 1;
3792 e = target_configure( goi, target);
3794 free( target->type );
3799 if( target->endianness == TARGET_ENDIAN_UNKNOWN ){
3800 /* default endian to little if not specified */
3801 target->endianness = TARGET_LITTLE_ENDIAN;
3804 /* create the target specific commands */
3805 if( target->type->register_commands ){
3806 (*(target->type->register_commands))( cmd_ctx );
3808 if( target->type->target_create ){
3809 (*(target->type->target_create))( target, goi->interp );
3812 /* append to end of list */
3815 tpp = &(all_targets);
3817 tpp = &( (*tpp)->next );
3822 cp = Jim_GetString( new_cmd, NULL );
3823 target->cmd_name = strdup(cp);
3825 /* now - create the new target name command */
3826 e = Jim_CreateCommand( goi->interp,
3829 tcl_target_func, /* C function */
3830 target, /* private data */
3831 NULL ); /* no del proc */
3833 (*(target->type->target_create))( target, goi->interp );
3838 jim_target( Jim_Interp *interp, int argc, Jim_Obj *const *argv )
3842 struct command_context_s *cmd_ctx;
3847 /* TG = target generic */
3855 const char *target_cmds[] = {
3856 "create", "types", "names", "current", "number",
3861 LOG_DEBUG("Target command params:");
3862 LOG_DEBUG(Jim_Debug_ArgvString( interp, argc, argv) );
3864 cmd_ctx = Jim_GetAssocData( interp, "context" );
3866 Jim_GetOpt_Setup( &goi, interp, argc-1, argv+1 );
3868 if( goi.argc == 0 ){
3869 Jim_WrongNumArgs(interp, 1, argv, "missing: command ...");
3873 /* is this old syntax? */
3874 /* To determine: We have to peek at argv[0]*/
3875 cp = Jim_GetString( goi.argv[0], NULL );
3876 for( x = 0 ; target_types[x] ; x++ ){
3877 if( 0 == strcmp(cp,target_types[x]->name) ){
3881 if( target_types[x] ){
3882 /* YES IT IS OLD SYNTAX */
3883 Jim_Obj *new_argv[10];
3886 /* target_old_syntax
3888 * argv[0] typename (above)
3890 * argv[2] reset method, deprecated/ignored
3891 * argv[3] = old param
3892 * argv[4] = old param
3894 * We will combine all "old params" into a single param.
3895 * Then later, split them again.
3898 Jim_WrongNumArgs( interp, 1, argv, "[OLDSYNTAX] ?TYPE? ?ENDIAN? ?RESET? ?old-params?");
3902 new_argv[0] = argv[0];
3903 new_argv[1] = Jim_NewStringObj( interp, "create", -1 );
3906 sprintf( buf, "target%d", new_target_number() );
3907 new_argv[2] = Jim_NewStringObj( interp, buf , -1 );
3909 new_argv[3] = goi.argv[0]; /* typename */
3910 new_argv[4] = Jim_NewStringObj( interp, "-endian", -1 );
3911 new_argv[5] = goi.argv[1];
3912 new_argv[6] = Jim_NewStringObj( interp, "-chain-position", -1 );
3913 new_argv[7] = goi.argv[2];
3914 new_argv[8] = Jim_NewStringObj( interp, "-variant", -1 );
3915 new_argv[9] = goi.argv[3];
3922 * argv[3] = typename
3923 * argv[4] = **FIRST** "configure" option.
3925 * Here, we make them:
3929 * argv[6] = -position
3931 * argv[8] = -variant
3932 * argv[9] = "somestring"
3935 /* don't let these be released */
3936 for( x = 0 ; x < new_argc ; x++ ){
3937 Jim_IncrRefCount( new_argv[x]);
3940 LOG_DEBUG("Target OLD SYNTAX - converted to new syntax");
3942 r = jim_target( goi.interp, new_argc, new_argv );
3944 /* release? these items */
3945 for( x = 0 ; x < new_argc ; x++ ){
3946 Jim_DecrRefCount( interp, new_argv[x] );
3951 //Jim_GetOpt_Debug( &goi );
3952 r = Jim_GetOpt_Enum( &goi, target_cmds, &x );
3959 Jim_Panic(goi.interp,"Why am I here?");
3961 case TG_CMD_CURRENT:
3962 if( goi.argc != 0 ){
3963 Jim_WrongNumArgs( goi.interp, 1, goi.argv, "Too many parameters");
3966 Jim_SetResultString( goi.interp, get_current_target( cmd_ctx )->cmd_name, -1 );
3969 if( goi.argc != 0 ){
3970 Jim_WrongNumArgs( goi.interp, 1, goi.argv, "Too many parameters" );
3973 Jim_SetResult( goi.interp, Jim_NewListObj( goi.interp, NULL, 0 ) );
3974 for( x = 0 ; target_types[x] ; x++ ){
3975 Jim_ListAppendElement( goi.interp,
3976 Jim_GetResult(goi.interp),
3977 Jim_NewStringObj( goi.interp, target_types[x]->name, -1 ) );
3981 if( goi.argc != 0 ){
3982 Jim_WrongNumArgs( goi.interp, 1, goi.argv, "Too many parameters" );
3985 Jim_SetResult( goi.interp, Jim_NewListObj( goi.interp, NULL, 0 ) );
3986 target = all_targets;
3988 Jim_ListAppendElement( goi.interp,
3989 Jim_GetResult(goi.interp),
3990 Jim_NewStringObj( goi.interp, target->cmd_name, -1 ) );
3991 target = target->next;
3996 Jim_WrongNumArgs( goi.interp, goi.argc, goi.argv, "?name ... config options ...");
3999 return target_create( &goi );
4002 if( goi.argc != 1 ){
4003 Jim_SetResult_sprintf( goi.interp, "expected: target number ?NUMBER?");
4006 e = Jim_GetOpt_Wide( &goi, &w );
4012 t = get_target_by_num(w);
4014 Jim_SetResult_sprintf( goi.interp,"Target: number %d does not exist", (int)(w));
4017 Jim_SetResultString( goi.interp, t->cmd_name, -1 );
4021 if( goi.argc != 0 ){
4022 Jim_WrongNumArgs( goi.interp, 0, goi.argv, "<no parameters>");
4025 Jim_SetResult( goi.interp,
4026 Jim_NewIntObj( goi.interp, max_target_number()));