1 /***************************************************************************
2 * Copyright (C) 2005 by Dominic Rath *
3 * Dominic.Rath@gmx.de *
5 * Copyright (C) 2007,2008 Øyvind Harboe *
6 * oyvind.harboe@zylin.com *
8 * Copyright (C) 2008, Duane Ellis *
9 * openocd@duaneeellis.com *
11 * Copyright (C) 2008 by Spencer Oliver *
12 * spen@spen-soft.co.uk *
14 * Copyright (C) 2008 by Rick Altherr *
15 * kc8apf@kc8apf.net> *
17 * This program is free software; you can redistribute it and/or modify *
18 * it under the terms of the GNU General Public License as published by *
19 * the Free Software Foundation; either version 2 of the License, or *
20 * (at your option) any later version. *
22 * This program is distributed in the hope that it will be useful, *
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
25 * GNU General Public License for more details. *
27 * You should have received a copy of the GNU General Public License *
28 * along with this program; if not, write to the *
29 * Free Software Foundation, Inc., *
30 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
31 ***************************************************************************/
36 #include "replacements.h"
38 #include "target_request.h"
41 #include "configuration.h"
42 #include "binarybuffer.h"
49 #include <sys/types.h>
57 #include <time_support.h>
62 int cli_target_callback_event_handler(struct target_s *target, enum target_event event, void *priv);
65 int handle_targets_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
67 int handle_reg_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
68 int handle_poll_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
69 int handle_halt_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
70 int handle_wait_halt_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
71 int handle_reset_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
72 int handle_soft_reset_halt_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
73 int handle_resume_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
74 int handle_step_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
75 int handle_md_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
76 int handle_mw_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
77 int handle_load_image_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
78 int handle_dump_image_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
79 int handle_verify_image_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
80 int handle_bp_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
81 int handle_rbp_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
82 int handle_wp_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
83 int handle_rwp_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
84 int handle_virt2phys_command(command_context_t *cmd_ctx, char *cmd, char **args, int argc);
85 int handle_profile_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
86 static int jim_array2mem(Jim_Interp *interp, int argc, Jim_Obj *const *argv);
87 static int jim_mem2array(Jim_Interp *interp, int argc, Jim_Obj *const *argv);
88 static int jim_target( Jim_Interp *interp, int argc, Jim_Obj *const *argv);
90 static int target_array2mem(Jim_Interp *interp, target_t *target, int argc, Jim_Obj *const *argv);
91 static int target_mem2array(Jim_Interp *interp, target_t *target, int argc, Jim_Obj *const *argv);
96 extern target_type_t arm7tdmi_target;
97 extern target_type_t arm720t_target;
98 extern target_type_t arm9tdmi_target;
99 extern target_type_t arm920t_target;
100 extern target_type_t arm966e_target;
101 extern target_type_t arm926ejs_target;
102 extern target_type_t feroceon_target;
103 extern target_type_t xscale_target;
104 extern target_type_t cortexm3_target;
105 extern target_type_t arm11_target;
106 extern target_type_t mips_m4k_target;
108 target_type_t *target_types[] =
124 target_t *all_targets = NULL;
125 target_event_callback_t *target_event_callbacks = NULL;
126 target_timer_callback_t *target_timer_callbacks = NULL;
128 const Jim_Nvp nvp_assert[] = {
129 { .name = "assert", NVP_ASSERT },
130 { .name = "deassert", NVP_DEASSERT },
131 { .name = "T", NVP_ASSERT },
132 { .name = "F", NVP_DEASSERT },
133 { .name = "t", NVP_ASSERT },
134 { .name = "f", NVP_DEASSERT },
135 { .name = NULL, .value = -1 }
138 const Jim_Nvp nvp_error_target[] = {
139 { .value = ERROR_TARGET_INVALID, .name = "err-invalid" },
140 { .value = ERROR_TARGET_INIT_FAILED, .name = "err-init-failed" },
141 { .value = ERROR_TARGET_TIMEOUT, .name = "err-timeout" },
142 { .value = ERROR_TARGET_NOT_HALTED, .name = "err-not-halted" },
143 { .value = ERROR_TARGET_FAILURE, .name = "err-failure" },
144 { .value = ERROR_TARGET_UNALIGNED_ACCESS , .name = "err-unaligned-access" },
145 { .value = ERROR_TARGET_DATA_ABORT , .name = "err-data-abort" },
146 { .value = ERROR_TARGET_RESOURCE_NOT_AVAILABLE , .name = "err-resource-not-available" },
147 { .value = ERROR_TARGET_TRANSLATION_FAULT , .name = "err-translation-fault" },
148 { .value = ERROR_TARGET_NOT_RUNNING, .name = "err-not-running" },
149 { .value = ERROR_TARGET_NOT_EXAMINED, .name = "err-not-examined" },
150 { .value = -1, .name = NULL }
153 const char *target_strerror_safe( int err )
157 n = Jim_Nvp_value2name_simple( nvp_error_target, err );
158 if( n->name == NULL ){
165 const Jim_Nvp nvp_target_event[] = {
166 { .value = TARGET_EVENT_OLD_gdb_program_config , .name = "old-gdb_program_config" },
167 { .value = TARGET_EVENT_OLD_pre_resume , .name = "old-pre_resume" },
170 { .value = TARGET_EVENT_EARLY_HALTED, .name = "early-halted" },
171 { .value = TARGET_EVENT_HALTED, .name = "halted" },
172 { .value = TARGET_EVENT_RESUMED, .name = "resumed" },
173 { .value = TARGET_EVENT_RESUME_START, .name = "resume-start" },
174 { .value = TARGET_EVENT_RESUME_END, .name = "resume-end" },
177 { .name = "gdb-start", .value = TARGET_EVENT_GDB_START },
178 { .name = "gdb-end", .value = TARGET_EVENT_GDB_END },
181 /* historical name */
183 { .value = TARGET_EVENT_RESET_START, .name = "reset-start" },
185 { .value = TARGET_EVENT_RESET_ASSERT_PRE, .name = "reset-assert-pre" },
186 { .value = TARGET_EVENT_RESET_ASSERT_POST, .name = "reset-assert-post" },
187 { .value = TARGET_EVENT_RESET_DEASSERT_PRE, .name = "reset-deassert-pre" },
188 { .value = TARGET_EVENT_RESET_DEASSERT_POST, .name = "reset-deassert-post" },
189 { .value = TARGET_EVENT_RESET_HALT_PRE, .name = "reset-halt-pre" },
190 { .value = TARGET_EVENT_RESET_HALT_POST, .name = "reset-halt-post" },
191 { .value = TARGET_EVENT_RESET_WAIT_PRE, .name = "reset-wait-pre" },
192 { .value = TARGET_EVENT_RESET_WAIT_POST, .name = "reset-wait-post" },
193 { .value = TARGET_EVENT_RESET_INIT , .name = "reset-init" },
194 { .value = TARGET_EVENT_RESET_END, .name = "reset-end" },
200 { .value = TARGET_EVENT_EXAMINE_START, .name = "examine-start" },
201 { .value = TARGET_EVENT_EXAMINE_START, .name = "examine-end" },
204 { .value = TARGET_EVENT_DEBUG_HALTED, .name = "debug-halted" },
205 { .value = TARGET_EVENT_DEBUG_RESUMED, .name = "debug-resumed" },
207 { .value = TARGET_EVENT_GDB_ATTACH, .name = "gdb-attach" },
208 { .value = TARGET_EVENT_GDB_DETACH, .name = "gdb-detach" },
211 { .value = TARGET_EVENT_GDB_FLASH_WRITE_START, .name = "gdb-flash-write-start" },
212 { .value = TARGET_EVENT_GDB_FLASH_WRITE_END , .name = "gdb-flash-write-end" },
214 { .value = TARGET_EVENT_GDB_FLASH_ERASE_START, .name = "gdb-flash-erase-start" },
215 { .value = TARGET_EVENT_GDB_FLASH_ERASE_END , .name = "gdb-flash-erase-end" },
217 { .value = TARGET_EVENT_RESUME_START, .name = "resume-start" },
218 { .value = TARGET_EVENT_RESUMED , .name = "resume-ok" },
219 { .value = TARGET_EVENT_RESUME_END , .name = "resume-end" },
221 { .name = NULL, .value = -1 }
224 const Jim_Nvp nvp_target_state[] = {
225 { .name = "unknown", .value = TARGET_UNKNOWN },
226 { .name = "running", .value = TARGET_RUNNING },
227 { .name = "halted", .value = TARGET_HALTED },
228 { .name = "reset", .value = TARGET_RESET },
229 { .name = "debug-running", .value = TARGET_DEBUG_RUNNING },
230 { .name = NULL, .value = -1 },
234 const Jim_Nvp nvp_target_debug_reason [] = {
235 { .name = "debug-request" , .value = DBG_REASON_DBGRQ },
236 { .name = "breakpoint" , .value = DBG_REASON_BREAKPOINT },
237 { .name = "watchpoint" , .value = DBG_REASON_WATCHPOINT },
238 { .name = "watchpoint-and-breakpoint", .value = DBG_REASON_WPTANDBKPT },
239 { .name = "single-step" , .value = DBG_REASON_SINGLESTEP },
240 { .name = "target-not-halted" , .value = DBG_REASON_NOTHALTED },
241 { .name = "undefined" , .value = DBG_REASON_UNDEFINED },
242 { .name = NULL, .value = -1 },
246 const Jim_Nvp nvp_target_endian[] = {
247 { .name = "big", .value = TARGET_BIG_ENDIAN },
248 { .name = "little", .value = TARGET_LITTLE_ENDIAN },
249 { .name = "be", .value = TARGET_BIG_ENDIAN },
250 { .name = "le", .value = TARGET_LITTLE_ENDIAN },
251 { .name = NULL, .value = -1 },
254 const Jim_Nvp nvp_reset_modes[] = {
255 { .name = "unknown", .value = RESET_UNKNOWN },
256 { .name = "run" , .value = RESET_RUN },
257 { .name = "halt" , .value = RESET_HALT },
258 { .name = "init" , .value = RESET_INIT },
259 { .name = NULL , .value = -1 },
263 max_target_number( void )
271 if( x < t->target_number ){
272 x = (t->target_number)+1;
279 /* determine the number of the new target */
281 new_target_number( void )
286 /* number is 0 based */
290 if( x < t->target_number ){
291 x = t->target_number;
298 static int target_continous_poll = 1;
300 /* read a u32 from a buffer in target memory endianness */
301 u32 target_buffer_get_u32(target_t *target, u8 *buffer)
303 if (target->endianness == TARGET_LITTLE_ENDIAN)
304 return le_to_h_u32(buffer);
306 return be_to_h_u32(buffer);
309 /* read a u16 from a buffer in target memory endianness */
310 u16 target_buffer_get_u16(target_t *target, u8 *buffer)
312 if (target->endianness == TARGET_LITTLE_ENDIAN)
313 return le_to_h_u16(buffer);
315 return be_to_h_u16(buffer);
318 /* read a u8 from a buffer in target memory endianness */
319 u8 target_buffer_get_u8(target_t *target, u8 *buffer)
321 return *buffer & 0x0ff;
324 /* write a u32 to a buffer in target memory endianness */
325 void target_buffer_set_u32(target_t *target, u8 *buffer, u32 value)
327 if (target->endianness == TARGET_LITTLE_ENDIAN)
328 h_u32_to_le(buffer, value);
330 h_u32_to_be(buffer, value);
333 /* write a u16 to a buffer in target memory endianness */
334 void target_buffer_set_u16(target_t *target, u8 *buffer, u16 value)
336 if (target->endianness == TARGET_LITTLE_ENDIAN)
337 h_u16_to_le(buffer, value);
339 h_u16_to_be(buffer, value);
342 /* write a u8 to a buffer in target memory endianness */
343 void target_buffer_set_u8(target_t *target, u8 *buffer, u8 value)
348 /* returns a pointer to the n-th configured target */
349 target_t* get_target_by_num(int num)
351 target_t *target = all_targets;
354 if( target->target_number == num ){
357 target = target->next;
363 int get_num_by_target(target_t *query_target)
365 return query_target->target_number;
368 target_t* get_current_target(command_context_t *cmd_ctx)
370 target_t *target = get_target_by_num(cmd_ctx->current_target);
374 LOG_ERROR("BUG: current_target out of bounds");
382 int target_poll(struct target_s *target)
384 /* We can't poll until after examine */
385 if (!target->type->examined)
387 /* Fail silently lest we pollute the log */
390 return target->type->poll(target);
393 int target_halt(struct target_s *target)
395 /* We can't poll until after examine */
396 if (!target->type->examined)
398 LOG_ERROR("Target not examined yet");
401 return target->type->halt(target);
404 int target_resume(struct target_s *target, int current, u32 address, int handle_breakpoints, int debug_execution)
408 /* We can't poll until after examine */
409 if (!target->type->examined)
411 LOG_ERROR("Target not examined yet");
415 /* note that resume *must* be asynchronous. The CPU can halt before we poll. The CPU can
416 * even halt at the current PC as a result of a software breakpoint being inserted by (a bug?)
419 if ((retval = target->type->resume(target, current, address, handle_breakpoints, debug_execution)) != ERROR_OK)
426 int target_process_reset(struct command_context_s *cmd_ctx, enum target_reset_mode reset_mode)
431 n = Jim_Nvp_value2name_simple( nvp_reset_modes, reset_mode );
432 if( n->name == NULL ){
433 LOG_ERROR("invalid reset mode");
437 sprintf( buf, "ocd_process_reset %s", n->name );
438 retval = Jim_Eval( interp, buf );
440 if(retval != JIM_OK) {
441 Jim_PrintErrorMessage(interp);
445 /* We want any events to be processed before the prompt */
446 retval = target_call_timer_callbacks_now();
452 static int default_virt2phys(struct target_s *target, u32 virtual, u32 *physical)
458 static int default_mmu(struct target_s *target, int *enabled)
464 static int default_examine(struct target_s *target)
466 target->type->examined = 1;
471 /* Targets that correctly implement init+examine, i.e.
472 * no communication with target during init:
476 int target_examine(void)
478 int retval = ERROR_OK;
479 target_t *target = all_targets;
482 if ((retval = target->type->examine(target))!=ERROR_OK)
484 target = target->next;
489 static int target_write_memory_imp(struct target_s *target, u32 address, u32 size, u32 count, u8 *buffer)
491 if (!target->type->examined)
493 LOG_ERROR("Target not examined yet");
496 return target->type->write_memory_imp(target, address, size, count, buffer);
499 static int target_read_memory_imp(struct target_s *target, u32 address, u32 size, u32 count, u8 *buffer)
501 if (!target->type->examined)
503 LOG_ERROR("Target not examined yet");
506 return target->type->read_memory_imp(target, address, size, count, buffer);
509 static int target_soft_reset_halt_imp(struct target_s *target)
511 if (!target->type->examined)
513 LOG_ERROR("Target not examined yet");
516 return target->type->soft_reset_halt_imp(target);
519 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)
521 if (!target->type->examined)
523 LOG_ERROR("Target not examined yet");
526 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);
529 int target_init(struct command_context_s *cmd_ctx)
531 target_t *target = all_targets;
536 target->type->examined = 0;
537 if (target->type->examine == NULL)
539 target->type->examine = default_examine;
542 if ((retval = target->type->init_target(cmd_ctx, target)) != ERROR_OK)
544 LOG_ERROR("target '%s' init failed", target->type->name);
548 /* Set up default functions if none are provided by target */
549 if (target->type->virt2phys == NULL)
551 target->type->virt2phys = default_virt2phys;
553 target->type->virt2phys = default_virt2phys;
554 /* a non-invasive way(in terms of patches) to add some code that
555 * runs before the type->write/read_memory implementation
557 target->type->write_memory_imp = target->type->write_memory;
558 target->type->write_memory = target_write_memory_imp;
559 target->type->read_memory_imp = target->type->read_memory;
560 target->type->read_memory = target_read_memory_imp;
561 target->type->soft_reset_halt_imp = target->type->soft_reset_halt;
562 target->type->soft_reset_halt = target_soft_reset_halt_imp;
563 target->type->run_algorithm_imp = target->type->run_algorithm;
564 target->type->run_algorithm = target_run_algorithm_imp;
567 if (target->type->mmu == NULL)
569 target->type->mmu = default_mmu;
571 target = target->next;
576 if((retval = target_register_user_commands(cmd_ctx)) != ERROR_OK)
578 if((retval = target_register_timer_callback(handle_target, 100, 1, NULL)) != ERROR_OK)
585 int target_register_event_callback(int (*callback)(struct target_s *target, enum target_event event, void *priv), void *priv)
587 target_event_callback_t **callbacks_p = &target_event_callbacks;
589 if (callback == NULL)
591 return ERROR_INVALID_ARGUMENTS;
596 while ((*callbacks_p)->next)
597 callbacks_p = &((*callbacks_p)->next);
598 callbacks_p = &((*callbacks_p)->next);
601 (*callbacks_p) = malloc(sizeof(target_event_callback_t));
602 (*callbacks_p)->callback = callback;
603 (*callbacks_p)->priv = priv;
604 (*callbacks_p)->next = NULL;
609 int target_register_timer_callback(int (*callback)(void *priv), int time_ms, int periodic, void *priv)
611 target_timer_callback_t **callbacks_p = &target_timer_callbacks;
614 if (callback == NULL)
616 return ERROR_INVALID_ARGUMENTS;
621 while ((*callbacks_p)->next)
622 callbacks_p = &((*callbacks_p)->next);
623 callbacks_p = &((*callbacks_p)->next);
626 (*callbacks_p) = malloc(sizeof(target_timer_callback_t));
627 (*callbacks_p)->callback = callback;
628 (*callbacks_p)->periodic = periodic;
629 (*callbacks_p)->time_ms = time_ms;
631 gettimeofday(&now, NULL);
632 (*callbacks_p)->when.tv_usec = now.tv_usec + (time_ms % 1000) * 1000;
633 time_ms -= (time_ms % 1000);
634 (*callbacks_p)->when.tv_sec = now.tv_sec + (time_ms / 1000);
635 if ((*callbacks_p)->when.tv_usec > 1000000)
637 (*callbacks_p)->when.tv_usec = (*callbacks_p)->when.tv_usec - 1000000;
638 (*callbacks_p)->when.tv_sec += 1;
641 (*callbacks_p)->priv = priv;
642 (*callbacks_p)->next = NULL;
647 int target_unregister_event_callback(int (*callback)(struct target_s *target, enum target_event event, void *priv), void *priv)
649 target_event_callback_t **p = &target_event_callbacks;
650 target_event_callback_t *c = target_event_callbacks;
652 if (callback == NULL)
654 return ERROR_INVALID_ARGUMENTS;
659 target_event_callback_t *next = c->next;
660 if ((c->callback == callback) && (c->priv == priv))
674 int target_unregister_timer_callback(int (*callback)(void *priv), void *priv)
676 target_timer_callback_t **p = &target_timer_callbacks;
677 target_timer_callback_t *c = target_timer_callbacks;
679 if (callback == NULL)
681 return ERROR_INVALID_ARGUMENTS;
686 target_timer_callback_t *next = c->next;
687 if ((c->callback == callback) && (c->priv == priv))
701 int target_call_event_callbacks(target_t *target, enum target_event event)
703 target_event_callback_t *callback = target_event_callbacks;
704 target_event_callback_t *next_callback;
706 if (event == TARGET_EVENT_HALTED)
708 /* execute early halted first */
709 target_call_event_callbacks(target, TARGET_EVENT_EARLY_HALTED);
713 LOG_DEBUG("target event %i (%s)",
715 Jim_Nvp_value2name_simple( nvp_target_event, event )->name );
717 target_handle_event( target, event );
721 next_callback = callback->next;
722 callback->callback(target, event, callback->priv);
723 callback = next_callback;
729 static int target_call_timer_callbacks_check_time(int checktime)
731 target_timer_callback_t *callback = target_timer_callbacks;
732 target_timer_callback_t *next_callback;
737 gettimeofday(&now, NULL);
741 next_callback = callback->next;
743 if ((!checktime&&callback->periodic)||
744 (((now.tv_sec >= callback->when.tv_sec) && (now.tv_usec >= callback->when.tv_usec))
745 || (now.tv_sec > callback->when.tv_sec)))
747 if(callback->callback != NULL)
749 callback->callback(callback->priv);
750 if (callback->periodic)
752 int time_ms = callback->time_ms;
753 callback->when.tv_usec = now.tv_usec + (time_ms % 1000) * 1000;
754 time_ms -= (time_ms % 1000);
755 callback->when.tv_sec = now.tv_sec + time_ms / 1000;
756 if (callback->when.tv_usec > 1000000)
758 callback->when.tv_usec = callback->when.tv_usec - 1000000;
759 callback->when.tv_sec += 1;
765 if((retval = target_unregister_timer_callback(callback->callback, callback->priv)) != ERROR_OK)
771 callback = next_callback;
777 int target_call_timer_callbacks(void)
779 return target_call_timer_callbacks_check_time(1);
782 /* invoke periodic callbacks immediately */
783 int target_call_timer_callbacks_now(void)
785 return target_call_timer_callbacks_check_time(0);
788 int target_alloc_working_area(struct target_s *target, u32 size, working_area_t **area)
790 working_area_t *c = target->working_areas;
791 working_area_t *new_wa = NULL;
793 /* Reevaluate working area address based on MMU state*/
794 if (target->working_areas == NULL)
798 retval = target->type->mmu(target, &enabled);
799 if (retval != ERROR_OK)
805 target->working_area = target->working_area_virt;
809 target->working_area = target->working_area_phys;
813 /* only allocate multiples of 4 byte */
816 LOG_ERROR("BUG: code tried to allocate unaligned number of bytes, padding");
817 size = CEIL(size, 4);
820 /* see if there's already a matching working area */
823 if ((c->free) && (c->size == size))
831 /* if not, allocate a new one */
834 working_area_t **p = &target->working_areas;
835 u32 first_free = target->working_area;
836 u32 free_size = target->working_area_size;
838 LOG_DEBUG("allocating new working area");
840 c = target->working_areas;
843 first_free += c->size;
844 free_size -= c->size;
849 if (free_size < size)
851 LOG_WARNING("not enough working area available(requested %d, free %d)", size, free_size);
852 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
855 new_wa = malloc(sizeof(working_area_t));
858 new_wa->address = first_free;
860 if (target->backup_working_area)
863 new_wa->backup = malloc(new_wa->size);
864 if((retval = target->type->read_memory(target, new_wa->address, 4, new_wa->size / 4, new_wa->backup)) != ERROR_OK)
866 free(new_wa->backup);
873 new_wa->backup = NULL;
876 /* put new entry in list */
880 /* mark as used, and return the new (reused) area */
890 int target_free_working_area_restore(struct target_s *target, working_area_t *area, int restore)
895 if (restore&&target->backup_working_area)
898 if((retval = target->type->write_memory(target, area->address, 4, area->size / 4, area->backup)) != ERROR_OK)
904 /* mark user pointer invalid */
911 int target_free_working_area(struct target_s *target, working_area_t *area)
913 return target_free_working_area_restore(target, area, 1);
916 /* free resources and restore memory, if restoring memory fails,
917 * free up resources anyway
919 void target_free_all_working_areas_restore(struct target_s *target, int restore)
921 working_area_t *c = target->working_areas;
925 working_area_t *next = c->next;
926 target_free_working_area_restore(target, c, restore);
936 target->working_areas = NULL;
939 void target_free_all_working_areas(struct target_s *target)
941 target_free_all_working_areas_restore(target, 1);
944 int target_register_commands(struct command_context_s *cmd_ctx)
947 register_command(cmd_ctx, NULL, "targets", handle_targets_command, COMMAND_EXEC, "change the current command line target (one parameter) or lists targets (with no parameter)");
948 register_command(cmd_ctx, NULL, "virt2phys", handle_virt2phys_command, COMMAND_ANY, "translate a virtual address into a physical address");
949 register_command(cmd_ctx, NULL, "profile", handle_profile_command, COMMAND_EXEC, "profiling samples the CPU PC");
951 register_jim(cmd_ctx, "target", jim_target, "configure target" );
954 /* script procedures */
955 register_jim(cmd_ctx, "ocd_mem2array", jim_mem2array, "read memory and return as a TCL array for script processing");
956 register_jim(cmd_ctx, "ocd_array2mem", jim_array2mem, "convert a TCL array to memory locations and write the values");
960 int target_arch_state(struct target_s *target)
965 LOG_USER("No target has been configured");
969 LOG_USER("target state: %s",
970 Jim_Nvp_value2name_simple(nvp_target_state,target->state)->name);
972 if (target->state!=TARGET_HALTED)
975 retval=target->type->arch_state(target);
979 /* Single aligned words are guaranteed to use 16 or 32 bit access
980 * mode respectively, otherwise data is handled as quickly as
983 int target_write_buffer(struct target_s *target, u32 address, u32 size, u8 *buffer)
986 LOG_DEBUG("writing buffer of %i byte at 0x%8.8x", size, address);
988 if (!target->type->examined)
990 LOG_ERROR("Target not examined yet");
994 if ((address + size - 1) < address)
996 /* GDB can request this when e.g. PC is 0xfffffffc*/
997 LOG_ERROR("address+size wrapped(0x%08x, 0x%08x)", address, size);
1001 if (((address % 2) == 0) && (size == 2))
1003 return target->type->write_memory(target, address, 2, 1, buffer);
1006 /* handle unaligned head bytes */
1009 int unaligned = 4 - (address % 4);
1011 if (unaligned > size)
1014 if ((retval = target->type->write_memory(target, address, 1, unaligned, buffer)) != ERROR_OK)
1017 buffer += unaligned;
1018 address += unaligned;
1022 /* handle aligned words */
1025 int aligned = size - (size % 4);
1027 /* use bulk writes above a certain limit. This may have to be changed */
1030 if ((retval = target->type->bulk_write_memory(target, address, aligned / 4, buffer)) != ERROR_OK)
1035 if ((retval = target->type->write_memory(target, address, 4, aligned / 4, buffer)) != ERROR_OK)
1044 /* handle tail writes of less than 4 bytes */
1047 if ((retval = target->type->write_memory(target, address, 1, size, buffer)) != ERROR_OK)
1055 /* Single aligned words are guaranteed to use 16 or 32 bit access
1056 * mode respectively, otherwise data is handled as quickly as
1059 int target_read_buffer(struct target_s *target, u32 address, u32 size, u8 *buffer)
1062 LOG_DEBUG("reading buffer of %i byte at 0x%8.8x", size, address);
1064 if (!target->type->examined)
1066 LOG_ERROR("Target not examined yet");
1070 if ((address + size - 1) < address)
1072 /* GDB can request this when e.g. PC is 0xfffffffc*/
1073 LOG_ERROR("address+size wrapped(0x%08x, 0x%08x)", address, size);
1077 if (((address % 2) == 0) && (size == 2))
1079 return target->type->read_memory(target, address, 2, 1, buffer);
1082 /* handle unaligned head bytes */
1085 int unaligned = 4 - (address % 4);
1087 if (unaligned > size)
1090 if ((retval = target->type->read_memory(target, address, 1, unaligned, buffer)) != ERROR_OK)
1093 buffer += unaligned;
1094 address += unaligned;
1098 /* handle aligned words */
1101 int aligned = size - (size % 4);
1103 if ((retval = target->type->read_memory(target, address, 4, aligned / 4, buffer)) != ERROR_OK)
1111 /* handle tail writes of less than 4 bytes */
1114 if ((retval = target->type->read_memory(target, address, 1, size, buffer)) != ERROR_OK)
1121 int target_checksum_memory(struct target_s *target, u32 address, u32 size, u32* crc)
1127 if (!target->type->examined)
1129 LOG_ERROR("Target not examined yet");
1133 if ((retval = target->type->checksum_memory(target, address,
1134 size, &checksum)) != ERROR_OK)
1136 buffer = malloc(size);
1139 LOG_ERROR("error allocating buffer for section (%d bytes)", size);
1140 return ERROR_INVALID_ARGUMENTS;
1142 retval = target_read_buffer(target, address, size, buffer);
1143 if (retval != ERROR_OK)
1149 /* convert to target endianess */
1150 for (i = 0; i < (size/sizeof(u32)); i++)
1153 target_data = target_buffer_get_u32(target, &buffer[i*sizeof(u32)]);
1154 target_buffer_set_u32(target, &buffer[i*sizeof(u32)], target_data);
1157 retval = image_calculate_checksum( buffer, size, &checksum );
1166 int target_blank_check_memory(struct target_s *target, u32 address, u32 size, u32* blank)
1169 if (!target->type->examined)
1171 LOG_ERROR("Target not examined yet");
1175 if (target->type->blank_check_memory == 0)
1176 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
1178 retval = target->type->blank_check_memory(target, address, size, blank);
1183 int target_read_u32(struct target_s *target, u32 address, u32 *value)
1186 if (!target->type->examined)
1188 LOG_ERROR("Target not examined yet");
1192 int retval = target->type->read_memory(target, address, 4, 1, value_buf);
1194 if (retval == ERROR_OK)
1196 *value = target_buffer_get_u32(target, value_buf);
1197 LOG_DEBUG("address: 0x%8.8x, value: 0x%8.8x", address, *value);
1202 LOG_DEBUG("address: 0x%8.8x failed", address);
1208 int target_read_u16(struct target_s *target, u32 address, u16 *value)
1211 if (!target->type->examined)
1213 LOG_ERROR("Target not examined yet");
1217 int retval = target->type->read_memory(target, address, 2, 1, value_buf);
1219 if (retval == ERROR_OK)
1221 *value = target_buffer_get_u16(target, value_buf);
1222 LOG_DEBUG("address: 0x%8.8x, value: 0x%4.4x", address, *value);
1227 LOG_DEBUG("address: 0x%8.8x failed", address);
1233 int target_read_u8(struct target_s *target, u32 address, u8 *value)
1235 int retval = target->type->read_memory(target, address, 1, 1, value);
1236 if (!target->type->examined)
1238 LOG_ERROR("Target not examined yet");
1242 if (retval == ERROR_OK)
1244 LOG_DEBUG("address: 0x%8.8x, value: 0x%2.2x", address, *value);
1249 LOG_DEBUG("address: 0x%8.8x failed", address);
1255 int target_write_u32(struct target_s *target, u32 address, u32 value)
1259 if (!target->type->examined)
1261 LOG_ERROR("Target not examined yet");
1265 LOG_DEBUG("address: 0x%8.8x, value: 0x%8.8x", address, value);
1267 target_buffer_set_u32(target, value_buf, value);
1268 if ((retval = target->type->write_memory(target, address, 4, 1, value_buf)) != ERROR_OK)
1270 LOG_DEBUG("failed: %i", retval);
1276 int target_write_u16(struct target_s *target, u32 address, u16 value)
1280 if (!target->type->examined)
1282 LOG_ERROR("Target not examined yet");
1286 LOG_DEBUG("address: 0x%8.8x, value: 0x%8.8x", address, value);
1288 target_buffer_set_u16(target, value_buf, value);
1289 if ((retval = target->type->write_memory(target, address, 2, 1, value_buf)) != ERROR_OK)
1291 LOG_DEBUG("failed: %i", retval);
1297 int target_write_u8(struct target_s *target, u32 address, u8 value)
1300 if (!target->type->examined)
1302 LOG_ERROR("Target not examined yet");
1306 LOG_DEBUG("address: 0x%8.8x, value: 0x%2.2x", address, value);
1308 if ((retval = target->type->write_memory(target, address, 1, 1, &value)) != ERROR_OK)
1310 LOG_DEBUG("failed: %i", retval);
1316 int target_register_user_commands(struct command_context_s *cmd_ctx)
1318 int retval = ERROR_OK;
1319 register_command(cmd_ctx, NULL, "reg", handle_reg_command, COMMAND_EXEC, "display or set a register");
1320 register_command(cmd_ctx, NULL, "poll", handle_poll_command, COMMAND_EXEC, "poll target state");
1321 register_command(cmd_ctx, NULL, "wait_halt", handle_wait_halt_command, COMMAND_EXEC, "wait for target halt [time (s)]");
1322 register_command(cmd_ctx, NULL, "halt", handle_halt_command, COMMAND_EXEC, "halt target");
1323 register_command(cmd_ctx, NULL, "resume", handle_resume_command, COMMAND_EXEC, "resume target [addr]");
1324 register_command(cmd_ctx, NULL, "step", handle_step_command, COMMAND_EXEC, "step one instruction from current PC or [addr]");
1325 register_command(cmd_ctx, NULL, "reset", handle_reset_command, COMMAND_EXEC, "reset target [run|halt|init] - default is run");
1326 register_command(cmd_ctx, NULL, "soft_reset_halt", handle_soft_reset_halt_command, COMMAND_EXEC, "halt the target and do a soft reset");
1328 register_command(cmd_ctx, NULL, "mdw", handle_md_command, COMMAND_EXEC, "display memory words <addr> [count]");
1329 register_command(cmd_ctx, NULL, "mdh", handle_md_command, COMMAND_EXEC, "display memory half-words <addr> [count]");
1330 register_command(cmd_ctx, NULL, "mdb", handle_md_command, COMMAND_EXEC, "display memory bytes <addr> [count]");
1332 register_command(cmd_ctx, NULL, "mww", handle_mw_command, COMMAND_EXEC, "write memory word <addr> <value> [count]");
1333 register_command(cmd_ctx, NULL, "mwh", handle_mw_command, COMMAND_EXEC, "write memory half-word <addr> <value> [count]");
1334 register_command(cmd_ctx, NULL, "mwb", handle_mw_command, COMMAND_EXEC, "write memory byte <addr> <value> [count]");
1336 register_command(cmd_ctx, NULL, "bp", handle_bp_command, COMMAND_EXEC, "set breakpoint <address> <length> [hw]");
1337 register_command(cmd_ctx, NULL, "rbp", handle_rbp_command, COMMAND_EXEC, "remove breakpoint <adress>");
1338 register_command(cmd_ctx, NULL, "wp", handle_wp_command, COMMAND_EXEC, "set watchpoint <address> <length> <r/w/a> [value] [mask]");
1339 register_command(cmd_ctx, NULL, "rwp", handle_rwp_command, COMMAND_EXEC, "remove watchpoint <adress>");
1341 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]");
1342 register_command(cmd_ctx, NULL, "dump_image", handle_dump_image_command, COMMAND_EXEC, "dump_image <file> <address> <size>");
1343 register_command(cmd_ctx, NULL, "verify_image", handle_verify_image_command, COMMAND_EXEC, "verify_image <file> [offset] [type]");
1345 if((retval = target_request_register_commands(cmd_ctx)) != ERROR_OK)
1347 if((retval = trace_register_commands(cmd_ctx)) != ERROR_OK)
1354 int handle_targets_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1357 target_t *target = all_targets;
1361 /* try as tcltarget name */
1362 for( target = all_targets ; target ; target = target->next ){
1363 if( target->cmd_name ){
1364 if( 0 == strcmp( args[0], target->cmd_name ) ){
1370 /* no match, try as number */
1372 int num = strtoul(args[0], &cp, 0 );
1374 /* then it was not a number */
1375 command_print( cmd_ctx, "Target: %s unknown, try one of:\n", args[0] );
1379 target = get_target_by_num( num );
1380 if( target == NULL ){
1381 command_print(cmd_ctx,"Target: %s is unknown, try one of:\n", args[0] );
1385 cmd_ctx->current_target = target->target_number;
1390 target = all_targets;
1391 command_print(cmd_ctx, " CmdName Type Endian ChainPos State ");
1392 command_print(cmd_ctx, "-- ---------- ---------- ---------- -------- ----------");
1395 /* XX: abcdefghij abcdefghij abcdefghij abcdefghij */
1396 command_print(cmd_ctx, "%2d: %-10s %-10s %-10s %8d %s",
1397 target->target_number,
1400 Jim_Nvp_value2name_simple( nvp_target_endian, target->endianness )->name,
1401 target->chain_position,
1402 Jim_Nvp_value2name_simple( nvp_target_state, target->state )->name );
1403 target = target->next;
1409 // every 300ms we check for reset & powerdropout and issue a "reset halt" if
1412 static int powerDropout;
1413 static int srstAsserted;
1415 static int runPowerRestore;
1416 static int runPowerDropout;
1417 static int runSrstAsserted;
1418 static int runSrstDeasserted;
1420 static int sense_handler()
1422 static int prevSrstAsserted = 0;
1423 static int prevPowerdropout = 0;
1426 if ((retval=jtag_power_dropout(&powerDropout))!=ERROR_OK)
1430 powerRestored = prevPowerdropout && !powerDropout;
1433 runPowerRestore = 1;
1436 long long current = timeval_ms();
1437 static long long lastPower = 0;
1438 int waitMore = lastPower + 2000 > current;
1439 if (powerDropout && !waitMore)
1441 runPowerDropout = 1;
1442 lastPower = current;
1445 if ((retval=jtag_srst_asserted(&srstAsserted))!=ERROR_OK)
1449 srstDeasserted = prevSrstAsserted && !srstAsserted;
1451 static long long lastSrst = 0;
1452 waitMore = lastSrst + 2000 > current;
1453 if (srstDeasserted && !waitMore)
1455 runSrstDeasserted = 1;
1459 if (!prevSrstAsserted && srstAsserted)
1461 runSrstAsserted = 1;
1464 prevSrstAsserted = srstAsserted;
1465 prevPowerdropout = powerDropout;
1467 if (srstDeasserted || powerRestored)
1469 /* Other than logging the event we can't do anything here.
1470 * Issuing a reset is a particularly bad idea as we might
1471 * be inside a reset already.
1479 /* process target state changes */
1480 int handle_target(void *priv)
1482 int retval = ERROR_OK;
1484 /* we do not want to recurse here... */
1485 static int recursive = 0;
1490 /* danger! running these procedures can trigger srst assertions and power dropouts.
1491 * We need to avoid an infinite loop/recursion here and we do that by
1492 * clearing the flags after running these events.
1494 int did_something = 0;
1495 if (runSrstAsserted)
1497 Jim_Eval( interp, "srst_asserted");
1500 if (runSrstDeasserted)
1502 Jim_Eval( interp, "srst_deasserted");
1505 if (runPowerDropout)
1507 Jim_Eval( interp, "power_dropout");
1510 if (runPowerRestore)
1512 Jim_Eval( interp, "power_restore");
1518 /* clear detect flags */
1522 /* clear action flags */
1525 runSrstDeasserted=0;
1532 target_t *target = all_targets;
1537 /* only poll target if we've got power and srst isn't asserted */
1538 if (target_continous_poll&&!powerDropout&&!srstAsserted)
1540 /* polling may fail silently until the target has been examined */
1541 if((retval = target_poll(target)) != ERROR_OK)
1545 target = target->next;
1552 int handle_reg_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1561 target = get_current_target(cmd_ctx);
1563 /* list all available registers for the current target */
1566 reg_cache_t *cache = target->reg_cache;
1572 for (i = 0; i < cache->num_regs; i++)
1574 value = buf_to_str(cache->reg_list[i].value, cache->reg_list[i].size, 16);
1575 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);
1578 cache = cache->next;
1584 /* access a single register by its ordinal number */
1585 if ((args[0][0] >= '0') && (args[0][0] <= '9'))
1587 int num = strtoul(args[0], NULL, 0);
1588 reg_cache_t *cache = target->reg_cache;
1594 for (i = 0; i < cache->num_regs; i++)
1598 reg = &cache->reg_list[i];
1604 cache = cache->next;
1609 command_print(cmd_ctx, "%i is out of bounds, the current target has only %i registers (0 - %i)", num, count, count - 1);
1612 } else /* access a single register by its name */
1614 reg = register_get_by_name(target->reg_cache, args[0], 1);
1618 command_print(cmd_ctx, "register %s not found in current target", args[0]);
1623 /* display a register */
1624 if ((argc == 1) || ((argc == 2) && !((args[1][0] >= '0') && (args[1][0] <= '9'))))
1626 if ((argc == 2) && (strcmp(args[1], "force") == 0))
1629 if (reg->valid == 0)
1631 reg_arch_type_t *arch_type = register_get_arch_type(reg->arch_type);
1632 arch_type->get(reg);
1634 value = buf_to_str(reg->value, reg->size, 16);
1635 command_print(cmd_ctx, "%s (/%i): 0x%s", reg->name, reg->size, value);
1640 /* set register value */
1643 u8 *buf = malloc(CEIL(reg->size, 8));
1644 str_to_buf(args[1], strlen(args[1]), buf, reg->size, 0);
1646 reg_arch_type_t *arch_type = register_get_arch_type(reg->arch_type);
1647 arch_type->set(reg, buf);
1649 value = buf_to_str(reg->value, reg->size, 16);
1650 command_print(cmd_ctx, "%s (/%i): 0x%s", reg->name, reg->size, value);
1658 command_print(cmd_ctx, "usage: reg <#|name> [value]");
1664 int handle_poll_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1666 int retval = ERROR_OK;
1667 target_t *target = get_current_target(cmd_ctx);
1671 if((retval = target_poll(target)) != ERROR_OK)
1673 if((retval = target_arch_state(target)) != ERROR_OK)
1679 if (strcmp(args[0], "on") == 0)
1681 target_continous_poll = 1;
1683 else if (strcmp(args[0], "off") == 0)
1685 target_continous_poll = 0;
1689 command_print(cmd_ctx, "arg is \"on\" or \"off\"");
1693 return ERROR_COMMAND_SYNTAX_ERROR;
1700 int handle_wait_halt_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1708 ms = strtoul(args[0], &end, 0) * 1000;
1711 command_print(cmd_ctx, "usage: %s [seconds]", cmd);
1715 target_t *target = get_current_target(cmd_ctx);
1717 return target_wait_state(target, TARGET_HALTED, ms);
1720 int target_wait_state(target_t *target, enum target_state state, int ms)
1723 struct timeval timeout, now;
1725 gettimeofday(&timeout, NULL);
1726 timeval_add_time(&timeout, 0, ms * 1000);
1730 if ((retval=target_poll(target))!=ERROR_OK)
1733 if (target->state == state)
1740 LOG_DEBUG("waiting for target %s...",
1741 Jim_Nvp_value2name_simple(nvp_target_state,state)->name);
1744 gettimeofday(&now, NULL);
1745 if ((now.tv_sec > timeout.tv_sec) || ((now.tv_sec == timeout.tv_sec) && (now.tv_usec >= timeout.tv_usec)))
1747 LOG_ERROR("timed out while waiting for target %s",
1748 Jim_Nvp_value2name_simple(nvp_target_state,state)->name);
1756 int handle_halt_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1759 target_t *target = get_current_target(cmd_ctx);
1763 if ((retval = target_halt(target)) != ERROR_OK)
1768 return handle_wait_halt_command(cmd_ctx, cmd, args, argc);
1771 int handle_soft_reset_halt_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1773 target_t *target = get_current_target(cmd_ctx);
1775 LOG_USER("requesting target halt and executing a soft reset");
1777 target->type->soft_reset_halt(target);
1782 int handle_reset_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1785 enum target_reset_mode reset_mode = RESET_RUN;
1789 n = Jim_Nvp_name2value_simple( nvp_reset_modes, args[0] );
1790 if( (n->name == NULL) || (n->value == RESET_UNKNOWN) ){
1791 return ERROR_COMMAND_SYNTAX_ERROR;
1793 reset_mode = n->value;
1796 /* reset *all* targets */
1797 return target_process_reset(cmd_ctx, reset_mode);
1801 int handle_resume_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1804 target_t *target = get_current_target(cmd_ctx);
1806 target_handle_event( target, TARGET_EVENT_OLD_pre_resume );
1809 retval = target_resume(target, 1, 0, 1, 0); /* current pc, addr = 0, handle breakpoints, not debugging */
1811 retval = target_resume(target, 0, strtoul(args[0], NULL, 0), 1, 0); /* addr = args[0], handle breakpoints, not debugging */
1814 retval = ERROR_COMMAND_SYNTAX_ERROR;
1820 int handle_step_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1822 target_t *target = get_current_target(cmd_ctx);
1827 return target->type->step(target, 1, 0, 1); /* current pc, addr = 0, handle breakpoints */
1830 return target->type->step(target, 0, strtoul(args[0], NULL, 0), 1); /* addr = args[0], handle breakpoints */
1835 int handle_md_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1837 const int line_bytecnt = 32;
1850 target_t *target = get_current_target(cmd_ctx);
1856 count = strtoul(args[1], NULL, 0);
1858 address = strtoul(args[0], NULL, 0);
1864 size = 4; line_modulo = line_bytecnt / 4;
1867 size = 2; line_modulo = line_bytecnt / 2;
1870 size = 1; line_modulo = line_bytecnt / 1;
1876 buffer = calloc(count, size);
1877 retval = target->type->read_memory(target, address, size, count, buffer);
1878 if (retval == ERROR_OK)
1882 for (i = 0; i < count; i++)
1884 if (i%line_modulo == 0)
1885 output_len += snprintf(output + output_len, 128 - output_len, "0x%8.8x: ", address + (i*size));
1890 output_len += snprintf(output + output_len, 128 - output_len, "%8.8x ", target_buffer_get_u32(target, &buffer[i*4]));
1893 output_len += snprintf(output + output_len, 128 - output_len, "%4.4x ", target_buffer_get_u16(target, &buffer[i*2]));
1896 output_len += snprintf(output + output_len, 128 - output_len, "%2.2x ", buffer[i*1]);
1900 if ((i%line_modulo == line_modulo-1) || (i == count - 1))
1902 command_print(cmd_ctx, output);
1913 int handle_mw_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1920 target_t *target = get_current_target(cmd_ctx);
1923 if ((argc < 2) || (argc > 3))
1924 return ERROR_COMMAND_SYNTAX_ERROR;
1926 address = strtoul(args[0], NULL, 0);
1927 value = strtoul(args[1], NULL, 0);
1929 count = strtoul(args[2], NULL, 0);
1935 target_buffer_set_u32(target, value_buf, value);
1939 target_buffer_set_u16(target, value_buf, value);
1943 value_buf[0] = value;
1946 return ERROR_COMMAND_SYNTAX_ERROR;
1948 for (i=0; i<count; i++)
1954 retval = target->type->write_memory(target, address + i*wordsize, 4, 1, value_buf);
1957 retval = target->type->write_memory(target, address + i*wordsize, 2, 1, value_buf);
1960 retval = target->type->write_memory(target, address + i*wordsize, 1, 1, value_buf);
1967 if (retval!=ERROR_OK)
1977 int handle_load_image_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1983 u32 max_address=0xffffffff;
1985 int retval, retvaltemp;
1989 duration_t duration;
1990 char *duration_text;
1992 target_t *target = get_current_target(cmd_ctx);
1994 if ((argc < 1)||(argc > 5))
1996 return ERROR_COMMAND_SYNTAX_ERROR;
1999 /* a base address isn't always necessary, default to 0x0 (i.e. don't relocate) */
2002 image.base_address_set = 1;
2003 image.base_address = strtoul(args[1], NULL, 0);
2007 image.base_address_set = 0;
2011 image.start_address_set = 0;
2015 min_address=strtoul(args[3], NULL, 0);
2019 max_address=strtoul(args[4], NULL, 0)+min_address;
2022 if (min_address>max_address)
2024 return ERROR_COMMAND_SYNTAX_ERROR;
2028 duration_start_measure(&duration);
2030 if (image_open(&image, args[0], (argc >= 3) ? args[2] : NULL) != ERROR_OK)
2037 for (i = 0; i < image.num_sections; i++)
2039 buffer = malloc(image.sections[i].size);
2042 command_print(cmd_ctx, "error allocating buffer for section (%d bytes)", image.sections[i].size);
2046 if ((retval = image_read_section(&image, i, 0x0, image.sections[i].size, buffer, &buf_cnt)) != ERROR_OK)
2056 /* DANGER!!! beware of unsigned comparision here!!! */
2058 if ((image.sections[i].base_address+buf_cnt>=min_address)&&
2059 (image.sections[i].base_address<max_address))
2061 if (image.sections[i].base_address<min_address)
2063 /* clip addresses below */
2064 offset+=min_address-image.sections[i].base_address;
2068 if (image.sections[i].base_address+buf_cnt>max_address)
2070 length-=(image.sections[i].base_address+buf_cnt)-max_address;
2073 if ((retval = target_write_buffer(target, image.sections[i].base_address+offset, length, buffer+offset)) != ERROR_OK)
2078 image_size += length;
2079 command_print(cmd_ctx, "%u byte written at address 0x%8.8x", length, image.sections[i].base_address+offset);
2085 if((retvaltemp = duration_stop_measure(&duration, &duration_text)) != ERROR_OK)
2087 image_close(&image);
2091 if (retval==ERROR_OK)
2093 command_print(cmd_ctx, "downloaded %u byte in %s", image_size, duration_text);
2095 free(duration_text);
2097 image_close(&image);
2103 int handle_dump_image_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2110 int retval=ERROR_OK, retvaltemp;
2112 duration_t duration;
2113 char *duration_text;
2115 target_t *target = get_current_target(cmd_ctx);
2119 command_print(cmd_ctx, "usage: dump_image <filename> <address> <size>");
2123 address = strtoul(args[1], NULL, 0);
2124 size = strtoul(args[2], NULL, 0);
2126 if ((address & 3) || (size & 3))
2128 command_print(cmd_ctx, "only 32-bit aligned address and size are supported");
2132 if (fileio_open(&fileio, args[0], FILEIO_WRITE, FILEIO_BINARY) != ERROR_OK)
2137 duration_start_measure(&duration);
2142 u32 this_run_size = (size > 560) ? 560 : size;
2144 retval = target->type->read_memory(target, address, 4, this_run_size / 4, buffer);
2145 if (retval != ERROR_OK)
2150 retval = fileio_write(&fileio, this_run_size, buffer, &size_written);
2151 if (retval != ERROR_OK)
2156 size -= this_run_size;
2157 address += this_run_size;
2160 if((retvaltemp = fileio_close(&fileio)) != ERROR_OK)
2163 if((retvaltemp = duration_stop_measure(&duration, &duration_text)) != ERROR_OK)
2166 if (retval==ERROR_OK)
2168 command_print(cmd_ctx, "dumped %"PRIi64" byte in %s", fileio.size, duration_text);
2170 free(duration_text);
2175 int handle_verify_image_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2181 int retval, retvaltemp;
2183 u32 mem_checksum = 0;
2187 duration_t duration;
2188 char *duration_text;
2190 target_t *target = get_current_target(cmd_ctx);
2194 return ERROR_COMMAND_SYNTAX_ERROR;
2199 LOG_ERROR("no target selected");
2203 duration_start_measure(&duration);
2207 image.base_address_set = 1;
2208 image.base_address = strtoul(args[1], NULL, 0);
2212 image.base_address_set = 0;
2213 image.base_address = 0x0;
2216 image.start_address_set = 0;
2218 if ((retval=image_open(&image, args[0], (argc == 3) ? args[2] : NULL)) != ERROR_OK)
2225 for (i = 0; i < image.num_sections; i++)
2227 buffer = malloc(image.sections[i].size);
2230 command_print(cmd_ctx, "error allocating buffer for section (%d bytes)", image.sections[i].size);
2233 if ((retval = image_read_section(&image, i, 0x0, image.sections[i].size, buffer, &buf_cnt)) != ERROR_OK)
2239 /* calculate checksum of image */
2240 image_calculate_checksum( buffer, buf_cnt, &checksum );
2242 retval = target_checksum_memory(target, image.sections[i].base_address, buf_cnt, &mem_checksum);
2243 if( retval != ERROR_OK )
2249 if( checksum != mem_checksum )
2251 /* failed crc checksum, fall back to a binary compare */
2254 command_print(cmd_ctx, "checksum mismatch - attempting binary compare");
2256 data = (u8*)malloc(buf_cnt);
2258 /* Can we use 32bit word accesses? */
2260 int count = buf_cnt;
2261 if ((count % 4) == 0)
2266 retval = target->type->read_memory(target, image.sections[i].base_address, size, count, data);
2267 if (retval == ERROR_OK)
2270 for (t = 0; t < buf_cnt; t++)
2272 if (data[t] != buffer[t])
2274 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]);
2291 image_size += buf_cnt;
2295 if((retvaltemp = duration_stop_measure(&duration, &duration_text)) != ERROR_OK)
2297 image_close(&image);
2301 if (retval==ERROR_OK)
2303 command_print(cmd_ctx, "verified %u bytes in %s", image_size, duration_text);
2305 free(duration_text);
2307 image_close(&image);
2312 int handle_bp_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2315 target_t *target = get_current_target(cmd_ctx);
2319 breakpoint_t *breakpoint = target->breakpoints;
2323 if (breakpoint->type == BKPT_SOFT)
2325 char* buf = buf_to_str(breakpoint->orig_instr, breakpoint->length, 16);
2326 command_print(cmd_ctx, "0x%8.8x, 0x%x, %i, 0x%s", breakpoint->address, breakpoint->length, breakpoint->set, buf);
2331 command_print(cmd_ctx, "0x%8.8x, 0x%x, %i", breakpoint->address, breakpoint->length, breakpoint->set);
2333 breakpoint = breakpoint->next;
2341 length = strtoul(args[1], NULL, 0);
2344 if (strcmp(args[2], "hw") == 0)
2347 if ((retval = breakpoint_add(target, strtoul(args[0], NULL, 0), length, hw)) != ERROR_OK)
2349 LOG_ERROR("Failure setting breakpoints");
2353 command_print(cmd_ctx, "breakpoint added at address 0x%8.8x", strtoul(args[0], NULL, 0));
2358 command_print(cmd_ctx, "usage: bp <address> <length> ['hw']");
2364 int handle_rbp_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2366 target_t *target = get_current_target(cmd_ctx);
2369 breakpoint_remove(target, strtoul(args[0], NULL, 0));
2374 int handle_wp_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2376 target_t *target = get_current_target(cmd_ctx);
2381 watchpoint_t *watchpoint = target->watchpoints;
2385 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);
2386 watchpoint = watchpoint->next;
2391 enum watchpoint_rw type = WPT_ACCESS;
2392 u32 data_value = 0x0;
2393 u32 data_mask = 0xffffffff;
2409 command_print(cmd_ctx, "usage: wp <address> <length> [r/w/a] [value] [mask]");
2415 data_value = strtoul(args[3], NULL, 0);
2419 data_mask = strtoul(args[4], NULL, 0);
2422 if ((retval = watchpoint_add(target, strtoul(args[0], NULL, 0),
2423 strtoul(args[1], NULL, 0), type, data_value, data_mask)) != ERROR_OK)
2425 LOG_ERROR("Failure setting breakpoints");
2430 command_print(cmd_ctx, "usage: wp <address> <length> [r/w/a] [value] [mask]");
2436 int handle_rwp_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2438 target_t *target = get_current_target(cmd_ctx);
2441 watchpoint_remove(target, strtoul(args[0], NULL, 0));
2446 int handle_virt2phys_command(command_context_t *cmd_ctx, char *cmd, char **args, int argc)
2449 target_t *target = get_current_target(cmd_ctx);
2455 return ERROR_COMMAND_SYNTAX_ERROR;
2457 va = strtoul(args[0], NULL, 0);
2459 retval = target->type->virt2phys(target, va, &pa);
2460 if (retval == ERROR_OK)
2462 command_print(cmd_ctx, "Physical address 0x%08x", pa);
2466 /* lower levels will have logged a detailed error which is
2467 * forwarded to telnet/GDB session.
2472 static void writeLong(FILE *f, int l)
2477 char c=(l>>(i*8))&0xff;
2478 fwrite(&c, 1, 1, f);
2482 static void writeString(FILE *f, char *s)
2484 fwrite(s, 1, strlen(s), f);
2489 // Dump a gmon.out histogram file.
2490 static void writeGmon(u32 *samples, int sampleNum, char *filename)
2493 FILE *f=fopen(filename, "w");
2496 fwrite("gmon", 1, 4, f);
2497 writeLong(f, 0x00000001); // Version
2498 writeLong(f, 0); // padding
2499 writeLong(f, 0); // padding
2500 writeLong(f, 0); // padding
2502 fwrite("", 1, 1, f); // GMON_TAG_TIME_HIST
2504 // figure out bucket size
2507 for (i=0; i<sampleNum; i++)
2519 int addressSpace=(max-min+1);
2521 static int const maxBuckets=256*1024; // maximum buckets.
2522 int length=addressSpace;
2523 if (length > maxBuckets)
2527 int *buckets=malloc(sizeof(int)*length);
2533 memset(buckets, 0, sizeof(int)*length);
2534 for (i=0; i<sampleNum;i++)
2536 u32 address=samples[i];
2537 long long a=address-min;
2538 long long b=length-1;
2539 long long c=addressSpace-1;
2540 int index=(a*b)/c; // danger!!!! int32 overflows
2544 // append binary memory gmon.out &profile_hist_hdr ((char*)&profile_hist_hdr + sizeof(struct gmon_hist_hdr))
2545 writeLong(f, min); // low_pc
2546 writeLong(f, max); // high_pc
2547 writeLong(f, length); // # of samples
2548 writeLong(f, 64000000); // 64MHz
2549 writeString(f, "seconds");
2550 for (i=0; i<(15-strlen("seconds")); i++)
2552 fwrite("", 1, 1, f); // padding
2554 writeString(f, "s");
2556 // append binary memory gmon.out profile_hist_data (profile_hist_data + profile_hist_hdr.hist_size)
2558 char *data=malloc(2*length);
2561 for (i=0; i<length;i++)
2570 data[i*2+1]=(val>>8)&0xff;
2573 fwrite(data, 1, length*2, f);
2583 /* profiling samples the CPU PC as quickly as OpenOCD is able, which will be used as a random sampling of PC */
2584 int handle_profile_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2586 target_t *target = get_current_target(cmd_ctx);
2587 struct timeval timeout, now;
2589 gettimeofday(&timeout, NULL);
2592 return ERROR_COMMAND_SYNTAX_ERROR;
2595 timeval_add_time(&timeout, strtoul(args[0], &end, 0), 0);
2601 command_print(cmd_ctx, "Starting profiling. Halting and resuming the target as often as we can...");
2603 static const int maxSample=10000;
2604 u32 *samples=malloc(sizeof(u32)*maxSample);
2609 int retval=ERROR_OK;
2610 // hopefully it is safe to cache! We want to stop/restart as quickly as possible.
2611 reg_t *reg = register_get_by_name(target->reg_cache, "pc", 1);
2615 target_poll(target);
2616 if (target->state == TARGET_HALTED)
2618 u32 t=*((u32 *)reg->value);
2619 samples[numSamples++]=t;
2620 retval = target_resume(target, 1, 0, 0, 0); /* current pc, addr = 0, do not handle breakpoints, not debugging */
2621 target_poll(target);
2622 alive_sleep(10); // sleep 10ms, i.e. <100 samples/second.
2623 } else if (target->state == TARGET_RUNNING)
2625 // We want to quickly sample the PC.
2626 if((retval = target_halt(target)) != ERROR_OK)
2633 command_print(cmd_ctx, "Target not halted or running");
2637 if (retval!=ERROR_OK)
2642 gettimeofday(&now, NULL);
2643 if ((numSamples>=maxSample) || ((now.tv_sec >= timeout.tv_sec) && (now.tv_usec >= timeout.tv_usec)))
2645 command_print(cmd_ctx, "Profiling completed. %d samples.", numSamples);
2646 if((retval = target_poll(target)) != ERROR_OK)
2651 if (target->state == TARGET_HALTED)
2653 target_resume(target, 1, 0, 0, 0); /* current pc, addr = 0, do not handle breakpoints, not debugging */
2655 if((retval = target_poll(target)) != ERROR_OK)
2660 writeGmon(samples, numSamples, args[1]);
2661 command_print(cmd_ctx, "Wrote %s", args[1]);
2670 static int new_int_array_element(Jim_Interp * interp, const char *varname, int idx, u32 val)
2673 Jim_Obj *nameObjPtr, *valObjPtr;
2676 namebuf = alloc_printf("%s(%d)", varname, idx);
2680 nameObjPtr = Jim_NewStringObj(interp, namebuf, -1);
2681 valObjPtr = Jim_NewIntObj(interp, val);
2682 if (!nameObjPtr || !valObjPtr)
2688 Jim_IncrRefCount(nameObjPtr);
2689 Jim_IncrRefCount(valObjPtr);
2690 result = Jim_SetVariable(interp, nameObjPtr, valObjPtr);
2691 Jim_DecrRefCount(interp, nameObjPtr);
2692 Jim_DecrRefCount(interp, valObjPtr);
2694 /* printf("%s(%d) <= 0%08x\n", varname, idx, val); */
2698 static int jim_mem2array(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
2700 command_context_t *context;
2703 context = Jim_GetAssocData(interp, "context");
2704 if (context == NULL)
2706 LOG_ERROR("mem2array: no command context");
2709 target = get_current_target(context);
2712 LOG_ERROR("mem2array: no current target");
2716 return target_mem2array(interp, target, argc,argv);
2719 static int target_mem2array(Jim_Interp *interp, target_t *target, int argc, Jim_Obj *const *argv)
2727 const char *varname;
2729 int i, n, e, retval;
2731 /* argv[1] = name of array to receive the data
2732 * argv[2] = desired width
2733 * argv[3] = memory address
2734 * argv[4] = count of times to read
2737 Jim_WrongNumArgs(interp, 1, argv, "varname width addr nelems");
2740 varname = Jim_GetString(argv[1], &len);
2741 /* given "foo" get space for worse case "foo(%d)" .. add 20 */
2743 e = Jim_GetLong(interp, argv[2], &l);
2749 e = Jim_GetLong(interp, argv[3], &l);
2754 e = Jim_GetLong(interp, argv[4], &l);
2770 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
2771 Jim_AppendStrings( interp, Jim_GetResult(interp), "Invalid width param, must be 8/16/32", NULL );
2775 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
2776 Jim_AppendStrings(interp, Jim_GetResult(interp), "mem2array: zero width read?", NULL);
2779 if ((addr + (len * width)) < addr) {
2780 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
2781 Jim_AppendStrings(interp, Jim_GetResult(interp), "mem2array: addr + len - wraps to zero?", NULL);
2784 /* absurd transfer size? */
2786 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
2787 Jim_AppendStrings(interp, Jim_GetResult(interp), "mem2array: absurd > 64K item request", NULL);
2792 ((width == 2) && ((addr & 1) == 0)) ||
2793 ((width == 4) && ((addr & 3) == 0))) {
2797 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
2798 sprintf(buf, "mem2array address: 0x%08x is not aligned for %d byte reads", addr, width);
2799 Jim_AppendStrings(interp, Jim_GetResult(interp), buf , NULL);
2810 /* Slurp... in buffer size chunks */
2812 count = len; /* in objects.. */
2813 if (count > (sizeof(buffer)/width)) {
2814 count = (sizeof(buffer)/width);
2817 retval = target->type->read_memory( target, addr, width, count, buffer );
2818 if (retval != ERROR_OK) {
2820 LOG_ERROR("mem2array: Read @ 0x%08x, w=%d, cnt=%d, failed", addr, width, count);
2821 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
2822 Jim_AppendStrings(interp, Jim_GetResult(interp), "mem2array: cannot read memory", NULL);
2826 v = 0; /* shut up gcc */
2827 for (i = 0 ;i < count ;i++, n++) {
2830 v = target_buffer_get_u32(target, &buffer[i*width]);
2833 v = target_buffer_get_u16(target, &buffer[i*width]);
2836 v = buffer[i] & 0x0ff;
2839 new_int_array_element(interp, varname, n, v);
2845 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
2850 static int get_int_array_element(Jim_Interp * interp, const char *varname, int idx, u32 *val)
2853 Jim_Obj *nameObjPtr, *valObjPtr;
2857 namebuf = alloc_printf("%s(%d)", varname, idx);
2861 nameObjPtr = Jim_NewStringObj(interp, namebuf, -1);
2868 Jim_IncrRefCount(nameObjPtr);
2869 valObjPtr = Jim_GetVariable(interp, nameObjPtr, JIM_ERRMSG);
2870 Jim_DecrRefCount(interp, nameObjPtr);
2872 if (valObjPtr == NULL)
2875 result = Jim_GetLong(interp, valObjPtr, &l);
2876 /* printf("%s(%d) => 0%08x\n", varname, idx, val); */
2881 static int jim_array2mem(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
2883 command_context_t *context;
2886 context = Jim_GetAssocData(interp, "context");
2887 if (context == NULL){
2888 LOG_ERROR("array2mem: no command context");
2891 target = get_current_target(context);
2892 if (target == NULL){
2893 LOG_ERROR("array2mem: no current target");
2897 return target_array2mem( interp,target, argc, argv );
2901 static int target_array2mem(Jim_Interp *interp, target_t *target, int argc, Jim_Obj *const *argv)
2909 const char *varname;
2911 int i, n, e, retval;
2913 /* argv[1] = name of array to get the data
2914 * argv[2] = desired width
2915 * argv[3] = memory address
2916 * argv[4] = count to write
2919 Jim_WrongNumArgs(interp, 1, argv, "varname width addr nelems");
2922 varname = Jim_GetString(argv[1], &len);
2923 /* given "foo" get space for worse case "foo(%d)" .. add 20 */
2925 e = Jim_GetLong(interp, argv[2], &l);
2931 e = Jim_GetLong(interp, argv[3], &l);
2936 e = Jim_GetLong(interp, argv[4], &l);
2952 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
2953 Jim_AppendStrings( interp, Jim_GetResult(interp), "Invalid width param, must be 8/16/32", NULL );
2957 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
2958 Jim_AppendStrings(interp, Jim_GetResult(interp), "array2mem: zero width read?", NULL);
2961 if ((addr + (len * width)) < addr) {
2962 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
2963 Jim_AppendStrings(interp, Jim_GetResult(interp), "array2mem: addr + len - wraps to zero?", NULL);
2966 /* absurd transfer size? */
2968 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
2969 Jim_AppendStrings(interp, Jim_GetResult(interp), "array2mem: absurd > 64K item request", NULL);
2974 ((width == 2) && ((addr & 1) == 0)) ||
2975 ((width == 4) && ((addr & 3) == 0))) {
2979 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
2980 sprintf(buf, "array2mem address: 0x%08x is not aligned for %d byte reads", addr, width);
2981 Jim_AppendStrings(interp, Jim_GetResult(interp), buf , NULL);
2993 /* Slurp... in buffer size chunks */
2995 count = len; /* in objects.. */
2996 if (count > (sizeof(buffer)/width)) {
2997 count = (sizeof(buffer)/width);
3000 v = 0; /* shut up gcc */
3001 for (i = 0 ;i < count ;i++, n++) {
3002 get_int_array_element(interp, varname, n, &v);
3005 target_buffer_set_u32(target, &buffer[i*width], v);
3008 target_buffer_set_u16(target, &buffer[i*width], v);
3011 buffer[i] = v & 0x0ff;
3017 retval = target->type->write_memory(target, addr, width, count, buffer);
3018 if (retval != ERROR_OK) {
3020 LOG_ERROR("array2mem: Write @ 0x%08x, w=%d, cnt=%d, failed", addr, width, count);
3021 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3022 Jim_AppendStrings(interp, Jim_GetResult(interp), "array2mem: cannot read memory", NULL);
3028 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3034 target_all_handle_event( enum target_event e )
3039 LOG_DEBUG( "**all*targets: event: %d, %s",
3041 Jim_Nvp_value2name_simple( nvp_target_event, e )->name );
3043 target = all_targets;
3045 target_handle_event( target, e );
3046 target = target->next;
3051 target_handle_event( target_t *target, enum target_event e )
3053 target_event_action_t *teap;
3056 teap = target->event_action;
3060 if( teap->event == e ){
3062 LOG_DEBUG( "target: (%d) %s (%s) event: %d (%s) action: %s\n",
3063 target->target_number,
3067 Jim_Nvp_value2name_simple( nvp_target_event, e )->name,
3068 Jim_GetString( teap->body, NULL ) );
3069 if (Jim_EvalObj( interp, teap->body )!=JIM_OK)
3071 Jim_PrintErrorMessage(interp);
3077 LOG_DEBUG( "event: %d %s - no action",
3079 Jim_Nvp_value2name_simple( nvp_target_event, e )->name );
3083 enum target_cfg_param {
3086 TCFG_WORK_AREA_VIRT,
3087 TCFG_WORK_AREA_PHYS,
3088 TCFG_WORK_AREA_SIZE,
3089 TCFG_WORK_AREA_BACKUP,
3092 TCFG_CHAIN_POSITION,
3096 static Jim_Nvp nvp_config_opts[] = {
3097 { .name = "-type", .value = TCFG_TYPE },
3098 { .name = "-event", .value = TCFG_EVENT },
3099 { .name = "-work-area-virt", .value = TCFG_WORK_AREA_VIRT },
3100 { .name = "-work-area-phys", .value = TCFG_WORK_AREA_PHYS },
3101 { .name = "-work-area-size", .value = TCFG_WORK_AREA_SIZE },
3102 { .name = "-work-area-backup", .value = TCFG_WORK_AREA_BACKUP },
3103 { .name = "-endian" , .value = TCFG_ENDIAN },
3104 { .name = "-variant", .value = TCFG_VARIANT },
3105 { .name = "-chain-position", .value = TCFG_CHAIN_POSITION },
3107 { .name = NULL, .value = -1 }
3112 target_configure( Jim_GetOptInfo *goi,
3122 /* parse config or cget options ... */
3123 while( goi->argc > 0 ){
3124 Jim_SetEmptyResult( goi->interp );
3125 //Jim_GetOpt_Debug( goi );
3127 if( target->type->target_jim_configure ){
3128 /* target defines a configure function */
3129 /* target gets first dibs on parameters */
3130 e = (*(target->type->target_jim_configure))( target, goi );
3139 /* otherwise we 'continue' below */
3141 e = Jim_GetOpt_Nvp( goi, nvp_config_opts, &n );
3143 Jim_GetOpt_NvpUnknown( goi, nvp_config_opts, 0 );
3149 if( goi->isconfigure ){
3150 Jim_SetResult_sprintf( goi->interp, "not setable: %s", n->name );
3154 if( goi->argc != 0 ){
3155 Jim_WrongNumArgs( goi->interp, goi->argc, goi->argv, "NO PARAMS");
3159 Jim_SetResultString( goi->interp, target->type->name, -1 );
3163 if( goi->argc == 0 ){
3164 Jim_WrongNumArgs( goi->interp, goi->argc, goi->argv, "-event ?event-name? ...");
3168 e = Jim_GetOpt_Nvp( goi, nvp_target_event, &n );
3170 Jim_GetOpt_NvpUnknown( goi, nvp_target_event, 1 );
3174 if( goi->isconfigure ){
3175 if( goi->argc != 1 ){
3176 Jim_WrongNumArgs( goi->interp, goi->argc, goi->argv, "-event ?event-name? ?EVENT-BODY?");
3180 if( goi->argc != 0 ){
3181 Jim_WrongNumArgs(goi->interp, goi->argc, goi->argv, "-event ?event-name?");
3188 target_event_action_t *teap;
3190 teap = target->event_action;
3191 /* replace existing? */
3193 if( teap->event == n->value ){
3199 if( goi->isconfigure ){
3202 teap = calloc( 1, sizeof(*teap) );
3204 teap->event = n->value;
3205 Jim_GetOpt_Obj( goi, &o );
3207 Jim_DecrRefCount( interp, teap->body );
3209 teap->body = Jim_DuplicateObj( goi->interp, o );
3212 * Tcl/TK - "tk events" have a nice feature.
3213 * See the "BIND" command.
3214 * We should support that here.
3215 * You can specify %X and %Y in the event code.
3216 * The idea is: %T - target name.
3217 * The idea is: %N - target number
3218 * The idea is: %E - event name.
3220 Jim_IncrRefCount( teap->body );
3222 /* add to head of event list */
3223 teap->next = target->event_action;
3224 target->event_action = teap;
3225 Jim_SetEmptyResult(goi->interp);
3229 Jim_SetEmptyResult( goi->interp );
3231 Jim_SetResult( goi->interp, Jim_DuplicateObj( goi->interp, teap->body ) );
3238 case TCFG_WORK_AREA_VIRT:
3239 if( goi->isconfigure ){
3240 target_free_all_working_areas(target);
3241 e = Jim_GetOpt_Wide( goi, &w );
3245 target->working_area_virt = w;
3247 if( goi->argc != 0 ){
3251 Jim_SetResult( interp, Jim_NewIntObj( goi->interp, target->working_area_virt ) );
3255 case TCFG_WORK_AREA_PHYS:
3256 if( goi->isconfigure ){
3257 target_free_all_working_areas(target);
3258 e = Jim_GetOpt_Wide( goi, &w );
3262 target->working_area_phys = w;
3264 if( goi->argc != 0 ){
3268 Jim_SetResult( interp, Jim_NewIntObj( goi->interp, target->working_area_phys ) );
3272 case TCFG_WORK_AREA_SIZE:
3273 if( goi->isconfigure ){
3274 target_free_all_working_areas(target);
3275 e = Jim_GetOpt_Wide( goi, &w );
3279 target->working_area_size = w;
3281 if( goi->argc != 0 ){
3285 Jim_SetResult( interp, Jim_NewIntObj( goi->interp, target->working_area_size ) );
3289 case TCFG_WORK_AREA_BACKUP:
3290 if( goi->isconfigure ){
3291 target_free_all_working_areas(target);
3292 e = Jim_GetOpt_Wide( goi, &w );
3296 /* make this exactly 1 or 0 */
3297 target->backup_working_area = (!!w);
3299 if( goi->argc != 0 ){
3303 Jim_SetResult( interp, Jim_NewIntObj( goi->interp, target->working_area_size ) );
3304 /* loop for more e*/
3308 if( goi->isconfigure ){
3309 e = Jim_GetOpt_Nvp( goi, nvp_target_endian, &n );
3311 Jim_GetOpt_NvpUnknown( goi, nvp_target_endian, 1 );
3314 target->endianness = n->value;
3316 if( goi->argc != 0 ){
3320 n = Jim_Nvp_value2name_simple( nvp_target_endian, target->endianness );
3321 if( n->name == NULL ){
3322 target->endianness = TARGET_LITTLE_ENDIAN;
3323 n = Jim_Nvp_value2name_simple( nvp_target_endian, target->endianness );
3325 Jim_SetResultString( goi->interp, n->name, -1 );
3330 if( goi->isconfigure ){
3331 if( goi->argc < 1 ){
3332 Jim_SetResult_sprintf( goi->interp,
3337 if( target->variant ){
3338 free((void *)(target->variant));
3340 e = Jim_GetOpt_String( goi, &cp, NULL );
3341 target->variant = strdup(cp);
3343 if( goi->argc != 0 ){
3347 Jim_SetResultString( goi->interp, target->variant,-1 );
3350 case TCFG_CHAIN_POSITION:
3351 if( goi->isconfigure ){
3352 target_free_all_working_areas(target);
3353 e = Jim_GetOpt_Wide( goi, &w );
3357 if (jtag_get_device(w)==NULL)
3360 /* make this exactly 1 or 0 */
3361 target->chain_position = w;
3363 if( goi->argc != 0 ){
3367 Jim_SetResult( interp, Jim_NewIntObj( goi->interp, target->chain_position ) );
3368 /* loop for more e*/
3371 } /* while( goi->argc ) */
3372 /* done - we return */
3377 /** this is the 'tcl' handler for the target specific command */
3379 tcl_target_func( Jim_Interp *interp,
3381 Jim_Obj *const *argv )
3389 struct command_context_s *cmd_ctx;
3397 TS_CMD_MWW, TS_CMD_MWH, TS_CMD_MWB,
3398 TS_CMD_MDW, TS_CMD_MDH, TS_CMD_MDB,
3399 TS_CMD_MRW, TS_CMD_MRH, TS_CMD_MRB,
3400 TS_CMD_MEM2ARRAY, TS_CMD_ARRAY2MEM,
3408 TS_CMD_INVOKE_EVENT,
3411 static const Jim_Nvp target_options[] = {
3412 { .name = "configure", .value = TS_CMD_CONFIGURE },
3413 { .name = "cget", .value = TS_CMD_CGET },
3414 { .name = "mww", .value = TS_CMD_MWW },
3415 { .name = "mwh", .value = TS_CMD_MWH },
3416 { .name = "mwb", .value = TS_CMD_MWB },
3417 { .name = "mdw", .value = TS_CMD_MDW },
3418 { .name = "mdh", .value = TS_CMD_MDH },
3419 { .name = "mdb", .value = TS_CMD_MDB },
3420 { .name = "mem2array", .value = TS_CMD_MEM2ARRAY },
3421 { .name = "array2mem", .value = TS_CMD_ARRAY2MEM },
3422 { .name = "eventlist", .value = TS_CMD_EVENTLIST },
3423 { .name = "curstate", .value = TS_CMD_CURSTATE },
3425 { .name = "arp_examine", .value = TS_CMD_EXAMINE },
3426 { .name = "arp_poll", .value = TS_CMD_POLL },
3427 { .name = "arp_reset", .value = TS_CMD_RESET },
3428 { .name = "arp_halt", .value = TS_CMD_HALT },
3429 { .name = "arp_waitstate", .value = TS_CMD_WAITSTATE },
3430 { .name = "invoke-event", .value = TS_CMD_INVOKE_EVENT },
3432 { .name = NULL, .value = -1 },
3436 /* go past the "command" */
3437 Jim_GetOpt_Setup( &goi, interp, argc-1, argv+1 );
3439 target = Jim_CmdPrivData( goi.interp );
3440 cmd_ctx = Jim_GetAssocData(goi.interp, "context");
3442 /* commands here are in an NVP table */
3443 e = Jim_GetOpt_Nvp( &goi, target_options, &n );
3445 Jim_GetOpt_NvpUnknown( &goi, target_options, 0 );
3448 // Assume blank result
3449 Jim_SetEmptyResult( goi.interp );
3452 case TS_CMD_CONFIGURE:
3454 Jim_WrongNumArgs( goi.interp, goi.argc, goi.argv, "missing: -option VALUE ...");
3457 goi.isconfigure = 1;
3458 return target_configure( &goi, target );
3460 // some things take params
3462 Jim_WrongNumArgs( goi.interp, 0, goi.argv, "missing: ?-option?");
3465 goi.isconfigure = 0;
3466 return target_configure( &goi, target );
3474 * argv[3] = optional count.
3477 if( (goi.argc == 3) || (goi.argc == 4) ){
3481 Jim_SetResult_sprintf( goi.interp, "expected: %s ADDR DATA [COUNT]", n->name );
3485 e = Jim_GetOpt_Wide( &goi, &a );
3490 e = Jim_GetOpt_Wide( &goi, &b );
3495 e = Jim_GetOpt_Wide( &goi, &c );
3505 target_buffer_set_u32( target, target_buf, b );
3509 target_buffer_set_u16( target, target_buf, b );
3513 target_buffer_set_u8( target, target_buf, b );
3517 for( x = 0 ; x < c ; x++ ){
3518 e = target->type->write_memory( target, a, b, 1, target_buf );
3519 if( e != ERROR_OK ){
3520 Jim_SetResult_sprintf( interp, "Error writing @ 0x%08x: %d\n", (int)(a), e );
3533 /* argv[0] = command
3535 * argv[2] = optional count
3537 if( (goi.argc == 2) || (goi.argc == 3) ){
3538 Jim_SetResult_sprintf( goi.interp, "expected: %s ADDR [COUNT]", n->name );
3541 e = Jim_GetOpt_Wide( &goi, &a );
3546 e = Jim_GetOpt_Wide( &goi, &c );
3553 b = 1; /* shut up gcc */
3566 /* convert to "bytes" */
3568 /* count is now in 'BYTES' */
3574 e = target->type->read_memory( target, a, b, y / b, target_buf );
3575 if( e != ERROR_OK ){
3576 Jim_SetResult_sprintf( interp, "error reading target @ 0x%08lx", (int)(a) );
3580 Jim_fprintf( interp, interp->cookie_stdout, "0x%08x ", (int)(a) );
3583 for( x = 0 ; (x < 16) && (x < y) ; x += 4 ){
3584 z = target_buffer_get_u32( target, &(target_buf[ x * 4 ]) );
3585 Jim_fprintf( interp, interp->cookie_stdout, "%08x ", (int)(z) );
3587 for( ; (x < 16) ; x += 4 ){
3588 Jim_fprintf( interp, interp->cookie_stdout, " " );
3592 for( x = 0 ; (x < 16) && (x < y) ; x += 2 ){
3593 z = target_buffer_get_u16( target, &(target_buf[ x * 2 ]) );
3594 Jim_fprintf( interp, interp->cookie_stdout, "%04x ", (int)(z) );
3596 for( ; (x < 16) ; x += 2 ){
3597 Jim_fprintf( interp, interp->cookie_stdout, " " );
3602 for( x = 0 ; (x < 16) && (x < y) ; x += 1 ){
3603 z = target_buffer_get_u8( target, &(target_buf[ x * 4 ]) );
3604 Jim_fprintf( interp, interp->cookie_stdout, "%02x ", (int)(z) );
3606 for( ; (x < 16) ; x += 1 ){
3607 Jim_fprintf( interp, interp->cookie_stdout, " " );
3611 /* ascii-ify the bytes */
3612 for( x = 0 ; x < y ; x++ ){
3613 if( (target_buf[x] >= 0x20) &&
3614 (target_buf[x] <= 0x7e) ){
3618 target_buf[x] = '.';
3623 target_buf[x] = ' ';
3628 /* print - with a newline */
3629 Jim_fprintf( interp, interp->cookie_stdout, "%s\n", target_buf );
3635 case TS_CMD_MEM2ARRAY:
3636 return target_mem2array( goi.interp, target, goi.argc, goi.argv );
3638 case TS_CMD_ARRAY2MEM:
3639 return target_array2mem( goi.interp, target, goi.argc, goi.argv );
3641 case TS_CMD_EXAMINE:
3643 Jim_WrongNumArgs( goi.interp, 2, argv, "[no parameters]");
3646 e = target->type->examine( target );
3647 if( e != ERROR_OK ){
3648 Jim_SetResult_sprintf( interp, "examine-fails: %d", e );
3654 Jim_WrongNumArgs( goi.interp, 2, argv, "[no parameters]");
3657 if( !(target->type->examined) ){
3658 e = ERROR_TARGET_NOT_EXAMINED;
3660 e = target->type->poll( target );
3662 if( e != ERROR_OK ){
3663 Jim_SetResult_sprintf( interp, "poll-fails: %d", e );
3670 if( goi.argc != 2 ){
3671 Jim_WrongNumArgs( interp, 2, argv, "t|f|assert|deassert BOOL");
3674 e = Jim_GetOpt_Nvp( &goi, nvp_assert, &n );
3676 Jim_GetOpt_NvpUnknown( &goi, nvp_assert, 1 );
3679 // the halt or not param
3680 e = Jim_GetOpt_Wide( &goi, &a);
3684 // determine if we should halt or not.
3685 target->reset_halt = !!a;
3686 // When this happens - all workareas are invalid.
3687 target_free_all_working_areas_restore(target, 0);
3690 if( n->value == NVP_ASSERT ){
3691 target->type->assert_reset( target );
3693 target->type->deassert_reset( target );
3698 Jim_WrongNumArgs( goi.interp, 0, argv, "halt [no parameters]");
3701 target->type->halt( target );
3703 case TS_CMD_WAITSTATE:
3704 // params: <name> statename timeoutmsecs
3705 if( goi.argc != 2 ){
3706 Jim_SetResult_sprintf( goi.interp, "%s STATENAME TIMEOUTMSECS", n->name );
3709 e = Jim_GetOpt_Nvp( &goi, nvp_target_state, &n );
3711 Jim_GetOpt_NvpUnknown( &goi, nvp_target_state,1 );
3714 e = Jim_GetOpt_Wide( &goi, &a );
3718 e = target_wait_state( target, n->value, a );
3719 if( e != ERROR_OK ){
3720 Jim_SetResult_sprintf( goi.interp,
3721 "target: %s wait %s fails (%d) %s",
3724 e, target_strerror_safe(e) );
3729 case TS_CMD_EVENTLIST:
3730 /* List for human, Events defined for this target.
3731 * scripts/programs should use 'name cget -event NAME'
3734 target_event_action_t *teap;
3735 teap = target->event_action;
3736 command_print( cmd_ctx, "Event actions for target (%d) %s\n",
3737 target->target_number,
3739 command_print( cmd_ctx, "%-25s | Body", "Event");
3740 command_print( cmd_ctx, "------------------------- | ----------------------------------------");
3742 command_print( cmd_ctx,
3744 Jim_Nvp_value2name_simple( nvp_target_event, teap->event )->name,
3745 Jim_GetString( teap->body, NULL ) );
3748 command_print( cmd_ctx, "***END***");
3751 case TS_CMD_CURSTATE:
3752 if( goi.argc != 0 ){
3753 Jim_WrongNumArgs( goi.interp, 0, argv, "[no parameters]");
3756 Jim_SetResultString( goi.interp,
3757 Jim_Nvp_value2name_simple(nvp_target_state,target->state)->name,-1);
3759 case TS_CMD_INVOKE_EVENT:
3760 if( goi.argc != 1 ){
3761 Jim_SetResult_sprintf( goi.interp, "%s ?EVENTNAME?",n->name);
3764 e = Jim_GetOpt_Nvp( &goi, nvp_target_event, &n );
3766 Jim_GetOpt_NvpUnknown( &goi, nvp_target_event, 1 );
3769 target_handle_event( target, n->value );
3777 target_create( Jim_GetOptInfo *goi )
3787 struct command_context_s *cmd_ctx;
3789 cmd_ctx = Jim_GetAssocData(goi->interp, "context");
3790 if( goi->argc < 3 ){
3791 Jim_WrongNumArgs( goi->interp, 1, goi->argv, "?name? ?type? ..options...");
3796 Jim_GetOpt_Obj( goi, &new_cmd );
3797 /* does this command exist? */
3798 cmd = Jim_GetCommand( goi->interp, new_cmd, JIM_ERRMSG );
3800 cp = Jim_GetString( new_cmd, NULL );
3801 Jim_SetResult_sprintf(goi->interp, "Command/target: %s Exists", cp);
3806 e = Jim_GetOpt_String( goi, &cp2, NULL );
3808 /* now does target type exist */
3809 for( x = 0 ; target_types[x] ; x++ ){
3810 if( 0 == strcmp( cp, target_types[x]->name ) ){
3815 if( target_types[x] == NULL ){
3816 Jim_SetResult_sprintf( goi->interp, "Unknown target type %s, try one of ", cp );
3817 for( x = 0 ; target_types[x] ; x++ ){
3818 if( target_types[x+1] ){
3819 Jim_AppendStrings( goi->interp,
3820 Jim_GetResult(goi->interp),
3821 target_types[x]->name,
3824 Jim_AppendStrings( goi->interp,
3825 Jim_GetResult(goi->interp),
3827 target_types[x]->name,NULL );
3835 target = calloc(1,sizeof(target_t));
3836 /* set target number */
3837 target->target_number = new_target_number();
3839 /* allocate memory for each unique target type */
3840 target->type = (target_type_t*)calloc(1,sizeof(target_type_t));
3842 memcpy( target->type, target_types[x], sizeof(target_type_t));
3844 /* will be set by "-endian" */
3845 target->endianness = TARGET_ENDIAN_UNKNOWN;
3847 target->working_area = 0x0;
3848 target->working_area_size = 0x0;
3849 target->working_areas = NULL;
3850 target->backup_working_area = 0;
3852 target->state = TARGET_UNKNOWN;
3853 target->debug_reason = DBG_REASON_UNDEFINED;
3854 target->reg_cache = NULL;
3855 target->breakpoints = NULL;
3856 target->watchpoints = NULL;
3857 target->next = NULL;
3858 target->arch_info = NULL;
3860 target->display = 1;
3862 /* initialize trace information */
3863 target->trace_info = malloc(sizeof(trace_t));
3864 target->trace_info->num_trace_points = 0;
3865 target->trace_info->trace_points_size = 0;
3866 target->trace_info->trace_points = NULL;
3867 target->trace_info->trace_history_size = 0;
3868 target->trace_info->trace_history = NULL;
3869 target->trace_info->trace_history_pos = 0;
3870 target->trace_info->trace_history_overflowed = 0;
3872 target->dbgmsg = NULL;
3873 target->dbg_msg_enabled = 0;
3875 target->endianness = TARGET_ENDIAN_UNKNOWN;
3877 /* Do the rest as "configure" options */
3878 goi->isconfigure = 1;
3879 e = target_configure( goi, target);
3881 free( target->type );
3886 if( target->endianness == TARGET_ENDIAN_UNKNOWN ){
3887 /* default endian to little if not specified */
3888 target->endianness = TARGET_LITTLE_ENDIAN;
3891 /* create the target specific commands */
3892 if( target->type->register_commands ){
3893 (*(target->type->register_commands))( cmd_ctx );
3895 if( target->type->target_create ){
3896 (*(target->type->target_create))( target, goi->interp );
3899 /* append to end of list */
3902 tpp = &(all_targets);
3904 tpp = &( (*tpp)->next );
3909 cp = Jim_GetString( new_cmd, NULL );
3910 target->cmd_name = strdup(cp);
3912 /* now - create the new target name command */
3913 e = Jim_CreateCommand( goi->interp,
3916 tcl_target_func, /* C function */
3917 target, /* private data */
3918 NULL ); /* no del proc */
3924 jim_target( Jim_Interp *interp, int argc, Jim_Obj *const *argv )
3928 struct command_context_s *cmd_ctx;
3932 /* TG = target generic */
3940 const char *target_cmds[] = {
3941 "create", "types", "names", "current", "number",
3946 LOG_DEBUG("Target command params:");
3947 LOG_DEBUG(Jim_Debug_ArgvString( interp, argc, argv) );
3949 cmd_ctx = Jim_GetAssocData( interp, "context" );
3951 Jim_GetOpt_Setup( &goi, interp, argc-1, argv+1 );
3953 if( goi.argc == 0 ){
3954 Jim_WrongNumArgs(interp, 1, argv, "missing: command ...");
3958 //Jim_GetOpt_Debug( &goi );
3959 r = Jim_GetOpt_Enum( &goi, target_cmds, &x );
3966 Jim_Panic(goi.interp,"Why am I here?");
3968 case TG_CMD_CURRENT:
3969 if( goi.argc != 0 ){
3970 Jim_WrongNumArgs( goi.interp, 1, goi.argv, "Too many parameters");
3973 Jim_SetResultString( goi.interp, get_current_target( cmd_ctx )->cmd_name, -1 );
3976 if( goi.argc != 0 ){
3977 Jim_WrongNumArgs( goi.interp, 1, goi.argv, "Too many parameters" );
3980 Jim_SetResult( goi.interp, Jim_NewListObj( goi.interp, NULL, 0 ) );
3981 for( x = 0 ; target_types[x] ; x++ ){
3982 Jim_ListAppendElement( goi.interp,
3983 Jim_GetResult(goi.interp),
3984 Jim_NewStringObj( goi.interp, target_types[x]->name, -1 ) );
3988 if( goi.argc != 0 ){
3989 Jim_WrongNumArgs( goi.interp, 1, goi.argv, "Too many parameters" );
3992 Jim_SetResult( goi.interp, Jim_NewListObj( goi.interp, NULL, 0 ) );
3993 target = all_targets;
3995 Jim_ListAppendElement( goi.interp,
3996 Jim_GetResult(goi.interp),
3997 Jim_NewStringObj( goi.interp, target->cmd_name, -1 ) );
3998 target = target->next;
4003 Jim_WrongNumArgs( goi.interp, goi.argc, goi.argv, "?name ... config options ...");
4006 return target_create( &goi );
4009 if( goi.argc != 1 ){
4010 Jim_SetResult_sprintf( goi.interp, "expected: target number ?NUMBER?");
4013 e = Jim_GetOpt_Wide( &goi, &w );
4019 t = get_target_by_num(w);
4021 Jim_SetResult_sprintf( goi.interp,"Target: number %d does not exist", (int)(w));
4024 Jim_SetResultString( goi.interp, t->cmd_name, -1 );
4028 if( goi.argc != 0 ){
4029 Jim_WrongNumArgs( goi.interp, 0, goi.argv, "<no parameters>");
4032 Jim_SetResult( goi.interp,
4033 Jim_NewIntObj( goi.interp, max_target_number()));