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);
64 int handle_targets_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 int handle_fast_load_image_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
86 int handle_fast_load_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
88 static int jim_array2mem(Jim_Interp *interp, int argc, Jim_Obj *const *argv);
89 static int jim_mem2array(Jim_Interp *interp, int argc, Jim_Obj *const *argv);
90 static int jim_target( Jim_Interp *interp, int argc, Jim_Obj *const *argv);
92 static int target_array2mem(Jim_Interp *interp, target_t *target, int argc, Jim_Obj *const *argv);
93 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" },
169 { .value = TARGET_EVENT_EARLY_HALTED, .name = "early-halted" },
170 { .value = TARGET_EVENT_HALTED, .name = "halted" },
171 { .value = TARGET_EVENT_RESUMED, .name = "resumed" },
172 { .value = TARGET_EVENT_RESUME_START, .name = "resume-start" },
173 { .value = TARGET_EVENT_RESUME_END, .name = "resume-end" },
175 { .name = "gdb-start", .value = TARGET_EVENT_GDB_START },
176 { .name = "gdb-end", .value = TARGET_EVENT_GDB_END },
178 /* historical name */
180 { .value = TARGET_EVENT_RESET_START, .name = "reset-start" },
182 { .value = TARGET_EVENT_RESET_ASSERT_PRE, .name = "reset-assert-pre" },
183 { .value = TARGET_EVENT_RESET_ASSERT_POST, .name = "reset-assert-post" },
184 { .value = TARGET_EVENT_RESET_DEASSERT_PRE, .name = "reset-deassert-pre" },
185 { .value = TARGET_EVENT_RESET_DEASSERT_POST, .name = "reset-deassert-post" },
186 { .value = TARGET_EVENT_RESET_HALT_PRE, .name = "reset-halt-pre" },
187 { .value = TARGET_EVENT_RESET_HALT_POST, .name = "reset-halt-post" },
188 { .value = TARGET_EVENT_RESET_WAIT_PRE, .name = "reset-wait-pre" },
189 { .value = TARGET_EVENT_RESET_WAIT_POST, .name = "reset-wait-post" },
190 { .value = TARGET_EVENT_RESET_INIT , .name = "reset-init" },
191 { .value = TARGET_EVENT_RESET_END, .name = "reset-end" },
193 { .value = TARGET_EVENT_EXAMINE_START, .name = "examine-start" },
194 { .value = TARGET_EVENT_EXAMINE_END, .name = "examine-end" },
196 { .value = TARGET_EVENT_DEBUG_HALTED, .name = "debug-halted" },
197 { .value = TARGET_EVENT_DEBUG_RESUMED, .name = "debug-resumed" },
199 { .value = TARGET_EVENT_GDB_ATTACH, .name = "gdb-attach" },
200 { .value = TARGET_EVENT_GDB_DETACH, .name = "gdb-detach" },
202 { .value = TARGET_EVENT_GDB_FLASH_WRITE_START, .name = "gdb-flash-write-start" },
203 { .value = TARGET_EVENT_GDB_FLASH_WRITE_END , .name = "gdb-flash-write-end" },
205 { .value = TARGET_EVENT_GDB_FLASH_ERASE_START, .name = "gdb-flash-erase-start" },
206 { .value = TARGET_EVENT_GDB_FLASH_ERASE_END , .name = "gdb-flash-erase-end" },
208 { .value = TARGET_EVENT_RESUME_START, .name = "resume-start" },
209 { .value = TARGET_EVENT_RESUMED , .name = "resume-ok" },
210 { .value = TARGET_EVENT_RESUME_END , .name = "resume-end" },
212 { .name = NULL, .value = -1 }
215 const Jim_Nvp nvp_target_state[] = {
216 { .name = "unknown", .value = TARGET_UNKNOWN },
217 { .name = "running", .value = TARGET_RUNNING },
218 { .name = "halted", .value = TARGET_HALTED },
219 { .name = "reset", .value = TARGET_RESET },
220 { .name = "debug-running", .value = TARGET_DEBUG_RUNNING },
221 { .name = NULL, .value = -1 },
224 const Jim_Nvp nvp_target_debug_reason [] = {
225 { .name = "debug-request" , .value = DBG_REASON_DBGRQ },
226 { .name = "breakpoint" , .value = DBG_REASON_BREAKPOINT },
227 { .name = "watchpoint" , .value = DBG_REASON_WATCHPOINT },
228 { .name = "watchpoint-and-breakpoint", .value = DBG_REASON_WPTANDBKPT },
229 { .name = "single-step" , .value = DBG_REASON_SINGLESTEP },
230 { .name = "target-not-halted" , .value = DBG_REASON_NOTHALTED },
231 { .name = "undefined" , .value = DBG_REASON_UNDEFINED },
232 { .name = NULL, .value = -1 },
236 const Jim_Nvp nvp_target_endian[] = {
237 { .name = "big", .value = TARGET_BIG_ENDIAN },
238 { .name = "little", .value = TARGET_LITTLE_ENDIAN },
239 { .name = "be", .value = TARGET_BIG_ENDIAN },
240 { .name = "le", .value = TARGET_LITTLE_ENDIAN },
241 { .name = NULL, .value = -1 },
244 const Jim_Nvp nvp_reset_modes[] = {
245 { .name = "unknown", .value = RESET_UNKNOWN },
246 { .name = "run" , .value = RESET_RUN },
247 { .name = "halt" , .value = RESET_HALT },
248 { .name = "init" , .value = RESET_INIT },
249 { .name = NULL , .value = -1 },
253 max_target_number( void )
261 if( x < t->target_number ){
262 x = (t->target_number)+1;
269 /* determine the number of the new target */
271 new_target_number( void )
276 /* number is 0 based */
280 if( x < t->target_number ){
281 x = t->target_number;
288 static int target_continous_poll = 1;
290 /* read a u32 from a buffer in target memory endianness */
291 u32 target_buffer_get_u32(target_t *target, u8 *buffer)
293 if (target->endianness == TARGET_LITTLE_ENDIAN)
294 return le_to_h_u32(buffer);
296 return be_to_h_u32(buffer);
299 /* read a u16 from a buffer in target memory endianness */
300 u16 target_buffer_get_u16(target_t *target, u8 *buffer)
302 if (target->endianness == TARGET_LITTLE_ENDIAN)
303 return le_to_h_u16(buffer);
305 return be_to_h_u16(buffer);
308 /* read a u8 from a buffer in target memory endianness */
309 u8 target_buffer_get_u8(target_t *target, u8 *buffer)
311 return *buffer & 0x0ff;
314 /* write a u32 to a buffer in target memory endianness */
315 void target_buffer_set_u32(target_t *target, u8 *buffer, u32 value)
317 if (target->endianness == TARGET_LITTLE_ENDIAN)
318 h_u32_to_le(buffer, value);
320 h_u32_to_be(buffer, value);
323 /* write a u16 to a buffer in target memory endianness */
324 void target_buffer_set_u16(target_t *target, u8 *buffer, u16 value)
326 if (target->endianness == TARGET_LITTLE_ENDIAN)
327 h_u16_to_le(buffer, value);
329 h_u16_to_be(buffer, value);
332 /* write a u8 to a buffer in target memory endianness */
333 void target_buffer_set_u8(target_t *target, u8 *buffer, u8 value)
338 /* returns a pointer to the n-th configured target */
339 target_t* get_target_by_num(int num)
341 target_t *target = all_targets;
344 if( target->target_number == num ){
347 target = target->next;
353 int get_num_by_target(target_t *query_target)
355 return query_target->target_number;
358 target_t* get_current_target(command_context_t *cmd_ctx)
360 target_t *target = get_target_by_num(cmd_ctx->current_target);
364 LOG_ERROR("BUG: current_target out of bounds");
371 int target_poll(struct target_s *target)
373 /* We can't poll until after examine */
374 if (!target->type->examined)
376 /* Fail silently lest we pollute the log */
379 return target->type->poll(target);
382 int target_halt(struct target_s *target)
384 /* We can't poll until after examine */
385 if (!target->type->examined)
387 LOG_ERROR("Target not examined yet");
390 return target->type->halt(target);
393 int target_resume(struct target_s *target, int current, u32 address, int handle_breakpoints, int debug_execution)
397 /* We can't poll until after examine */
398 if (!target->type->examined)
400 LOG_ERROR("Target not examined yet");
404 /* note that resume *must* be asynchronous. The CPU can halt before we poll. The CPU can
405 * even halt at the current PC as a result of a software breakpoint being inserted by (a bug?)
408 if ((retval = target->type->resume(target, current, address, handle_breakpoints, debug_execution)) != ERROR_OK)
414 int target_process_reset(struct command_context_s *cmd_ctx, enum target_reset_mode reset_mode)
419 n = Jim_Nvp_value2name_simple( nvp_reset_modes, reset_mode );
420 if( n->name == NULL ){
421 LOG_ERROR("invalid reset mode");
425 sprintf( buf, "ocd_process_reset %s", n->name );
426 retval = Jim_Eval( interp, buf );
428 if(retval != JIM_OK) {
429 Jim_PrintErrorMessage(interp);
433 /* We want any events to be processed before the prompt */
434 retval = target_call_timer_callbacks_now();
439 static int default_virt2phys(struct target_s *target, u32 virtual, u32 *physical)
445 static int default_mmu(struct target_s *target, int *enabled)
451 static int default_examine(struct target_s *target)
453 target->type->examined = 1;
457 /* Targets that correctly implement init+examine, i.e.
458 * no communication with target during init:
462 int target_examine(void)
464 int retval = ERROR_OK;
465 target_t *target = all_targets;
468 if ((retval = target->type->examine(target))!=ERROR_OK)
470 target = target->next;
475 static int target_write_memory_imp(struct target_s *target, u32 address, u32 size, u32 count, u8 *buffer)
477 if (!target->type->examined)
479 LOG_ERROR("Target not examined yet");
482 return target->type->write_memory_imp(target, address, size, count, buffer);
485 static int target_read_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->read_memory_imp(target, address, size, count, buffer);
495 static int target_soft_reset_halt_imp(struct target_s *target)
497 if (!target->type->examined)
499 LOG_ERROR("Target not examined yet");
502 return target->type->soft_reset_halt_imp(target);
505 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)
507 if (!target->type->examined)
509 LOG_ERROR("Target not examined yet");
512 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);
515 int target_init(struct command_context_s *cmd_ctx)
517 target_t *target = all_targets;
522 target->type->examined = 0;
523 if (target->type->examine == NULL)
525 target->type->examine = default_examine;
528 if ((retval = target->type->init_target(cmd_ctx, target)) != ERROR_OK)
530 LOG_ERROR("target '%s' init failed", target->type->name);
534 /* Set up default functions if none are provided by target */
535 if (target->type->virt2phys == NULL)
537 target->type->virt2phys = default_virt2phys;
539 target->type->virt2phys = default_virt2phys;
540 /* a non-invasive way(in terms of patches) to add some code that
541 * runs before the type->write/read_memory implementation
543 target->type->write_memory_imp = target->type->write_memory;
544 target->type->write_memory = target_write_memory_imp;
545 target->type->read_memory_imp = target->type->read_memory;
546 target->type->read_memory = target_read_memory_imp;
547 target->type->soft_reset_halt_imp = target->type->soft_reset_halt;
548 target->type->soft_reset_halt = target_soft_reset_halt_imp;
549 target->type->run_algorithm_imp = target->type->run_algorithm;
550 target->type->run_algorithm = target_run_algorithm_imp;
552 if (target->type->mmu == NULL)
554 target->type->mmu = default_mmu;
556 target = target->next;
561 if((retval = target_register_user_commands(cmd_ctx)) != ERROR_OK)
563 if((retval = target_register_timer_callback(handle_target, 100, 1, NULL)) != ERROR_OK)
570 int target_register_event_callback(int (*callback)(struct target_s *target, enum target_event event, void *priv), void *priv)
572 target_event_callback_t **callbacks_p = &target_event_callbacks;
574 if (callback == NULL)
576 return ERROR_INVALID_ARGUMENTS;
581 while ((*callbacks_p)->next)
582 callbacks_p = &((*callbacks_p)->next);
583 callbacks_p = &((*callbacks_p)->next);
586 (*callbacks_p) = malloc(sizeof(target_event_callback_t));
587 (*callbacks_p)->callback = callback;
588 (*callbacks_p)->priv = priv;
589 (*callbacks_p)->next = NULL;
594 int target_register_timer_callback(int (*callback)(void *priv), int time_ms, int periodic, void *priv)
596 target_timer_callback_t **callbacks_p = &target_timer_callbacks;
599 if (callback == NULL)
601 return ERROR_INVALID_ARGUMENTS;
606 while ((*callbacks_p)->next)
607 callbacks_p = &((*callbacks_p)->next);
608 callbacks_p = &((*callbacks_p)->next);
611 (*callbacks_p) = malloc(sizeof(target_timer_callback_t));
612 (*callbacks_p)->callback = callback;
613 (*callbacks_p)->periodic = periodic;
614 (*callbacks_p)->time_ms = time_ms;
616 gettimeofday(&now, NULL);
617 (*callbacks_p)->when.tv_usec = now.tv_usec + (time_ms % 1000) * 1000;
618 time_ms -= (time_ms % 1000);
619 (*callbacks_p)->when.tv_sec = now.tv_sec + (time_ms / 1000);
620 if ((*callbacks_p)->when.tv_usec > 1000000)
622 (*callbacks_p)->when.tv_usec = (*callbacks_p)->when.tv_usec - 1000000;
623 (*callbacks_p)->when.tv_sec += 1;
626 (*callbacks_p)->priv = priv;
627 (*callbacks_p)->next = NULL;
632 int target_unregister_event_callback(int (*callback)(struct target_s *target, enum target_event event, void *priv), void *priv)
634 target_event_callback_t **p = &target_event_callbacks;
635 target_event_callback_t *c = target_event_callbacks;
637 if (callback == NULL)
639 return ERROR_INVALID_ARGUMENTS;
644 target_event_callback_t *next = c->next;
645 if ((c->callback == callback) && (c->priv == priv))
659 int target_unregister_timer_callback(int (*callback)(void *priv), void *priv)
661 target_timer_callback_t **p = &target_timer_callbacks;
662 target_timer_callback_t *c = target_timer_callbacks;
664 if (callback == NULL)
666 return ERROR_INVALID_ARGUMENTS;
671 target_timer_callback_t *next = c->next;
672 if ((c->callback == callback) && (c->priv == priv))
686 int target_call_event_callbacks(target_t *target, enum target_event event)
688 target_event_callback_t *callback = target_event_callbacks;
689 target_event_callback_t *next_callback;
691 if (event == TARGET_EVENT_HALTED)
693 /* execute early halted first */
694 target_call_event_callbacks(target, TARGET_EVENT_EARLY_HALTED);
697 LOG_DEBUG("target event %i (%s)",
699 Jim_Nvp_value2name_simple( nvp_target_event, event )->name );
701 target_handle_event( target, event );
705 next_callback = callback->next;
706 callback->callback(target, event, callback->priv);
707 callback = next_callback;
713 static int target_call_timer_callbacks_check_time(int checktime)
715 target_timer_callback_t *callback = target_timer_callbacks;
716 target_timer_callback_t *next_callback;
721 gettimeofday(&now, NULL);
725 next_callback = callback->next;
727 if ((!checktime&&callback->periodic)||
728 (((now.tv_sec >= callback->when.tv_sec) && (now.tv_usec >= callback->when.tv_usec))
729 || (now.tv_sec > callback->when.tv_sec)))
731 if(callback->callback != NULL)
733 callback->callback(callback->priv);
734 if (callback->periodic)
736 int time_ms = callback->time_ms;
737 callback->when.tv_usec = now.tv_usec + (time_ms % 1000) * 1000;
738 time_ms -= (time_ms % 1000);
739 callback->when.tv_sec = now.tv_sec + time_ms / 1000;
740 if (callback->when.tv_usec > 1000000)
742 callback->when.tv_usec = callback->when.tv_usec - 1000000;
743 callback->when.tv_sec += 1;
749 if((retval = target_unregister_timer_callback(callback->callback, callback->priv)) != ERROR_OK)
755 callback = next_callback;
761 int target_call_timer_callbacks(void)
763 return target_call_timer_callbacks_check_time(1);
766 /* invoke periodic callbacks immediately */
767 int target_call_timer_callbacks_now(void)
769 return target_call_timer_callbacks_check_time(0);
772 int target_alloc_working_area(struct target_s *target, u32 size, working_area_t **area)
774 working_area_t *c = target->working_areas;
775 working_area_t *new_wa = NULL;
777 /* Reevaluate working area address based on MMU state*/
778 if (target->working_areas == NULL)
782 retval = target->type->mmu(target, &enabled);
783 if (retval != ERROR_OK)
789 target->working_area = target->working_area_virt;
793 target->working_area = target->working_area_phys;
797 /* only allocate multiples of 4 byte */
800 LOG_ERROR("BUG: code tried to allocate unaligned number of bytes, padding");
801 size = CEIL(size, 4);
804 /* see if there's already a matching working area */
807 if ((c->free) && (c->size == size))
815 /* if not, allocate a new one */
818 working_area_t **p = &target->working_areas;
819 u32 first_free = target->working_area;
820 u32 free_size = target->working_area_size;
822 LOG_DEBUG("allocating new working area");
824 c = target->working_areas;
827 first_free += c->size;
828 free_size -= c->size;
833 if (free_size < size)
835 LOG_WARNING("not enough working area available(requested %d, free %d)", size, free_size);
836 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
839 new_wa = malloc(sizeof(working_area_t));
842 new_wa->address = first_free;
844 if (target->backup_working_area)
847 new_wa->backup = malloc(new_wa->size);
848 if((retval = target->type->read_memory(target, new_wa->address, 4, new_wa->size / 4, new_wa->backup)) != ERROR_OK)
850 free(new_wa->backup);
857 new_wa->backup = NULL;
860 /* put new entry in list */
864 /* mark as used, and return the new (reused) area */
874 int target_free_working_area_restore(struct target_s *target, working_area_t *area, int restore)
879 if (restore&&target->backup_working_area)
882 if((retval = target->type->write_memory(target, area->address, 4, area->size / 4, area->backup)) != ERROR_OK)
888 /* mark user pointer invalid */
895 int target_free_working_area(struct target_s *target, working_area_t *area)
897 return target_free_working_area_restore(target, area, 1);
900 /* free resources and restore memory, if restoring memory fails,
901 * free up resources anyway
903 void target_free_all_working_areas_restore(struct target_s *target, int restore)
905 working_area_t *c = target->working_areas;
909 working_area_t *next = c->next;
910 target_free_working_area_restore(target, c, restore);
920 target->working_areas = NULL;
923 void target_free_all_working_areas(struct target_s *target)
925 target_free_all_working_areas_restore(target, 1);
928 int target_register_commands(struct command_context_s *cmd_ctx)
931 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)");
932 register_command(cmd_ctx, NULL, "virt2phys", handle_virt2phys_command, COMMAND_ANY, "translate a virtual address into a physical address");
933 register_command(cmd_ctx, NULL, "profile", handle_profile_command, COMMAND_EXEC, "profiling samples the CPU PC");
935 register_command(cmd_ctx, NULL, "fast_load_image", handle_fast_load_image_command, COMMAND_ANY,
936 "same args as load_image, image stored in memory - mainly for profiling purposes");
938 register_command(cmd_ctx, NULL, "fast_load", handle_fast_load_command, COMMAND_ANY,
939 "loads active fast load image to current target - mainly for profiling purposes");
943 register_jim(cmd_ctx, "target", jim_target, "configure target" );
946 /* script procedures */
947 register_jim(cmd_ctx, "ocd_mem2array", jim_mem2array, "read memory and return as a TCL array for script processing");
948 register_jim(cmd_ctx, "ocd_array2mem", jim_array2mem, "convert a TCL array to memory locations and write the values");
952 int target_arch_state(struct target_s *target)
957 LOG_USER("No target has been configured");
961 LOG_USER("target state: %s",
962 Jim_Nvp_value2name_simple(nvp_target_state,target->state)->name);
964 if (target->state!=TARGET_HALTED)
967 retval=target->type->arch_state(target);
971 /* Single aligned words are guaranteed to use 16 or 32 bit access
972 * mode respectively, otherwise data is handled as quickly as
975 int target_write_buffer(struct target_s *target, u32 address, u32 size, u8 *buffer)
978 LOG_DEBUG("writing buffer of %i byte at 0x%8.8x", size, address);
980 if (!target->type->examined)
982 LOG_ERROR("Target not examined yet");
986 if ((address + size - 1) < address)
988 /* GDB can request this when e.g. PC is 0xfffffffc*/
989 LOG_ERROR("address+size wrapped(0x%08x, 0x%08x)", address, size);
993 if (((address % 2) == 0) && (size == 2))
995 return target->type->write_memory(target, address, 2, 1, buffer);
998 /* handle unaligned head bytes */
1001 int unaligned = 4 - (address % 4);
1003 if (unaligned > size)
1006 if ((retval = target->type->write_memory(target, address, 1, unaligned, buffer)) != ERROR_OK)
1009 buffer += unaligned;
1010 address += unaligned;
1014 /* handle aligned words */
1017 int aligned = size - (size % 4);
1019 /* use bulk writes above a certain limit. This may have to be changed */
1022 if ((retval = target->type->bulk_write_memory(target, address, aligned / 4, buffer)) != ERROR_OK)
1027 if ((retval = target->type->write_memory(target, address, 4, aligned / 4, buffer)) != ERROR_OK)
1036 /* handle tail writes of less than 4 bytes */
1039 if ((retval = target->type->write_memory(target, address, 1, size, buffer)) != ERROR_OK)
1046 /* Single aligned words are guaranteed to use 16 or 32 bit access
1047 * mode respectively, otherwise data is handled as quickly as
1050 int target_read_buffer(struct target_s *target, u32 address, u32 size, u8 *buffer)
1053 LOG_DEBUG("reading buffer of %i byte at 0x%8.8x", size, address);
1055 if (!target->type->examined)
1057 LOG_ERROR("Target not examined yet");
1061 if ((address + size - 1) < address)
1063 /* GDB can request this when e.g. PC is 0xfffffffc*/
1064 LOG_ERROR("address+size wrapped(0x%08x, 0x%08x)", address, size);
1068 if (((address % 2) == 0) && (size == 2))
1070 return target->type->read_memory(target, address, 2, 1, buffer);
1073 /* handle unaligned head bytes */
1076 int unaligned = 4 - (address % 4);
1078 if (unaligned > size)
1081 if ((retval = target->type->read_memory(target, address, 1, unaligned, buffer)) != ERROR_OK)
1084 buffer += unaligned;
1085 address += unaligned;
1089 /* handle aligned words */
1092 int aligned = size - (size % 4);
1094 if ((retval = target->type->read_memory(target, address, 4, aligned / 4, buffer)) != ERROR_OK)
1102 /* handle tail writes of less than 4 bytes */
1105 if ((retval = target->type->read_memory(target, address, 1, size, buffer)) != ERROR_OK)
1112 int target_checksum_memory(struct target_s *target, u32 address, u32 size, u32* crc)
1118 if (!target->type->examined)
1120 LOG_ERROR("Target not examined yet");
1124 if ((retval = target->type->checksum_memory(target, address,
1125 size, &checksum)) != ERROR_OK)
1127 buffer = malloc(size);
1130 LOG_ERROR("error allocating buffer for section (%d bytes)", size);
1131 return ERROR_INVALID_ARGUMENTS;
1133 retval = target_read_buffer(target, address, size, buffer);
1134 if (retval != ERROR_OK)
1140 /* convert to target endianess */
1141 for (i = 0; i < (size/sizeof(u32)); i++)
1144 target_data = target_buffer_get_u32(target, &buffer[i*sizeof(u32)]);
1145 target_buffer_set_u32(target, &buffer[i*sizeof(u32)], target_data);
1148 retval = image_calculate_checksum( buffer, size, &checksum );
1157 int target_blank_check_memory(struct target_s *target, u32 address, u32 size, u32* blank)
1160 if (!target->type->examined)
1162 LOG_ERROR("Target not examined yet");
1166 if (target->type->blank_check_memory == 0)
1167 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
1169 retval = target->type->blank_check_memory(target, address, size, blank);
1174 int target_read_u32(struct target_s *target, u32 address, u32 *value)
1177 if (!target->type->examined)
1179 LOG_ERROR("Target not examined yet");
1183 int retval = target->type->read_memory(target, address, 4, 1, value_buf);
1185 if (retval == ERROR_OK)
1187 *value = target_buffer_get_u32(target, value_buf);
1188 LOG_DEBUG("address: 0x%8.8x, value: 0x%8.8x", address, *value);
1193 LOG_DEBUG("address: 0x%8.8x failed", address);
1199 int target_read_u16(struct target_s *target, u32 address, u16 *value)
1202 if (!target->type->examined)
1204 LOG_ERROR("Target not examined yet");
1208 int retval = target->type->read_memory(target, address, 2, 1, value_buf);
1210 if (retval == ERROR_OK)
1212 *value = target_buffer_get_u16(target, value_buf);
1213 LOG_DEBUG("address: 0x%8.8x, value: 0x%4.4x", address, *value);
1218 LOG_DEBUG("address: 0x%8.8x failed", address);
1224 int target_read_u8(struct target_s *target, u32 address, u8 *value)
1226 int retval = target->type->read_memory(target, address, 1, 1, value);
1227 if (!target->type->examined)
1229 LOG_ERROR("Target not examined yet");
1233 if (retval == ERROR_OK)
1235 LOG_DEBUG("address: 0x%8.8x, value: 0x%2.2x", address, *value);
1240 LOG_DEBUG("address: 0x%8.8x failed", address);
1246 int target_write_u32(struct target_s *target, u32 address, u32 value)
1250 if (!target->type->examined)
1252 LOG_ERROR("Target not examined yet");
1256 LOG_DEBUG("address: 0x%8.8x, value: 0x%8.8x", address, value);
1258 target_buffer_set_u32(target, value_buf, value);
1259 if ((retval = target->type->write_memory(target, address, 4, 1, value_buf)) != ERROR_OK)
1261 LOG_DEBUG("failed: %i", retval);
1267 int target_write_u16(struct target_s *target, u32 address, u16 value)
1271 if (!target->type->examined)
1273 LOG_ERROR("Target not examined yet");
1277 LOG_DEBUG("address: 0x%8.8x, value: 0x%8.8x", address, value);
1279 target_buffer_set_u16(target, value_buf, value);
1280 if ((retval = target->type->write_memory(target, address, 2, 1, value_buf)) != ERROR_OK)
1282 LOG_DEBUG("failed: %i", retval);
1288 int target_write_u8(struct target_s *target, u32 address, u8 value)
1291 if (!target->type->examined)
1293 LOG_ERROR("Target not examined yet");
1297 LOG_DEBUG("address: 0x%8.8x, value: 0x%2.2x", address, value);
1299 if ((retval = target->type->write_memory(target, address, 1, 1, &value)) != ERROR_OK)
1301 LOG_DEBUG("failed: %i", retval);
1307 int target_register_user_commands(struct command_context_s *cmd_ctx)
1309 int retval = ERROR_OK;
1310 register_command(cmd_ctx, NULL, "reg", handle_reg_command, COMMAND_EXEC, "display or set a register");
1311 register_command(cmd_ctx, NULL, "poll", handle_poll_command, COMMAND_EXEC, "poll target state");
1312 register_command(cmd_ctx, NULL, "wait_halt", handle_wait_halt_command, COMMAND_EXEC, "wait for target halt [time (s)]");
1313 register_command(cmd_ctx, NULL, "halt", handle_halt_command, COMMAND_EXEC, "halt target");
1314 register_command(cmd_ctx, NULL, "resume", handle_resume_command, COMMAND_EXEC, "resume target [addr]");
1315 register_command(cmd_ctx, NULL, "step", handle_step_command, COMMAND_EXEC, "step one instruction from current PC or [addr]");
1316 register_command(cmd_ctx, NULL, "reset", handle_reset_command, COMMAND_EXEC, "reset target [run|halt|init] - default is run");
1317 register_command(cmd_ctx, NULL, "soft_reset_halt", handle_soft_reset_halt_command, COMMAND_EXEC, "halt the target and do a soft reset");
1319 register_command(cmd_ctx, NULL, "mdw", handle_md_command, COMMAND_EXEC, "display memory words <addr> [count]");
1320 register_command(cmd_ctx, NULL, "mdh", handle_md_command, COMMAND_EXEC, "display memory half-words <addr> [count]");
1321 register_command(cmd_ctx, NULL, "mdb", handle_md_command, COMMAND_EXEC, "display memory bytes <addr> [count]");
1323 register_command(cmd_ctx, NULL, "mww", handle_mw_command, COMMAND_EXEC, "write memory word <addr> <value> [count]");
1324 register_command(cmd_ctx, NULL, "mwh", handle_mw_command, COMMAND_EXEC, "write memory half-word <addr> <value> [count]");
1325 register_command(cmd_ctx, NULL, "mwb", handle_mw_command, COMMAND_EXEC, "write memory byte <addr> <value> [count]");
1327 register_command(cmd_ctx, NULL, "bp", handle_bp_command, COMMAND_EXEC, "set breakpoint <address> <length> [hw]");
1328 register_command(cmd_ctx, NULL, "rbp", handle_rbp_command, COMMAND_EXEC, "remove breakpoint <adress>");
1329 register_command(cmd_ctx, NULL, "wp", handle_wp_command, COMMAND_EXEC, "set watchpoint <address> <length> <r/w/a> [value] [mask]");
1330 register_command(cmd_ctx, NULL, "rwp", handle_rwp_command, COMMAND_EXEC, "remove watchpoint <adress>");
1332 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]");
1333 register_command(cmd_ctx, NULL, "dump_image", handle_dump_image_command, COMMAND_EXEC, "dump_image <file> <address> <size>");
1334 register_command(cmd_ctx, NULL, "verify_image", handle_verify_image_command, COMMAND_EXEC, "verify_image <file> [offset] [type]");
1336 if((retval = target_request_register_commands(cmd_ctx)) != ERROR_OK)
1338 if((retval = trace_register_commands(cmd_ctx)) != ERROR_OK)
1344 int handle_targets_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1347 target_t *target = all_targets;
1351 /* try as tcltarget name */
1352 for( target = all_targets ; target ; target = target->next ){
1353 if( target->cmd_name ){
1354 if( 0 == strcmp( args[0], target->cmd_name ) ){
1360 /* no match, try as number */
1362 int num = strtoul(args[0], &cp, 0 );
1364 /* then it was not a number */
1365 command_print( cmd_ctx, "Target: %s unknown, try one of:\n", args[0] );
1369 target = get_target_by_num( num );
1370 if( target == NULL ){
1371 command_print(cmd_ctx,"Target: %s is unknown, try one of:\n", args[0] );
1375 cmd_ctx->current_target = target->target_number;
1380 target = all_targets;
1381 command_print(cmd_ctx, " CmdName Type Endian AbsChainPos Name State ");
1382 command_print(cmd_ctx, "-- ---------- ---------- ---------- ----------- ------------- ----------");
1385 /* XX: abcdefghij abcdefghij abcdefghij abcdefghij */
1386 command_print(cmd_ctx, "%2d: %-10s %-10s %-10s %10d %14s %s",
1387 target->target_number,
1390 Jim_Nvp_value2name_simple( nvp_target_endian, target->endianness )->name,
1391 target->tap->abs_chain_position,
1392 target->tap->dotted_name,
1393 Jim_Nvp_value2name_simple( nvp_target_state, target->state )->name );
1394 target = target->next;
1400 /* every 300ms we check for reset & powerdropout and issue a "reset halt" if so. */
1402 static int powerDropout;
1403 static int srstAsserted;
1405 static int runPowerRestore;
1406 static int runPowerDropout;
1407 static int runSrstAsserted;
1408 static int runSrstDeasserted;
1410 static int sense_handler(void)
1412 static int prevSrstAsserted = 0;
1413 static int prevPowerdropout = 0;
1416 if ((retval=jtag_power_dropout(&powerDropout))!=ERROR_OK)
1420 powerRestored = prevPowerdropout && !powerDropout;
1423 runPowerRestore = 1;
1426 long long current = timeval_ms();
1427 static long long lastPower = 0;
1428 int waitMore = lastPower + 2000 > current;
1429 if (powerDropout && !waitMore)
1431 runPowerDropout = 1;
1432 lastPower = current;
1435 if ((retval=jtag_srst_asserted(&srstAsserted))!=ERROR_OK)
1439 srstDeasserted = prevSrstAsserted && !srstAsserted;
1441 static long long lastSrst = 0;
1442 waitMore = lastSrst + 2000 > current;
1443 if (srstDeasserted && !waitMore)
1445 runSrstDeasserted = 1;
1449 if (!prevSrstAsserted && srstAsserted)
1451 runSrstAsserted = 1;
1454 prevSrstAsserted = srstAsserted;
1455 prevPowerdropout = powerDropout;
1457 if (srstDeasserted || powerRestored)
1459 /* Other than logging the event we can't do anything here.
1460 * Issuing a reset is a particularly bad idea as we might
1461 * be inside a reset already.
1468 /* process target state changes */
1469 int handle_target(void *priv)
1471 int retval = ERROR_OK;
1473 /* we do not want to recurse here... */
1474 static int recursive = 0;
1479 /* danger! running these procedures can trigger srst assertions and power dropouts.
1480 * We need to avoid an infinite loop/recursion here and we do that by
1481 * clearing the flags after running these events.
1483 int did_something = 0;
1484 if (runSrstAsserted)
1486 Jim_Eval( interp, "srst_asserted");
1489 if (runSrstDeasserted)
1491 Jim_Eval( interp, "srst_deasserted");
1494 if (runPowerDropout)
1496 Jim_Eval( interp, "power_dropout");
1499 if (runPowerRestore)
1501 Jim_Eval( interp, "power_restore");
1507 /* clear detect flags */
1511 /* clear action flags */
1514 runSrstDeasserted=0;
1521 target_t *target = all_targets;
1526 /* only poll target if we've got power and srst isn't asserted */
1527 if (target_continous_poll&&!powerDropout&&!srstAsserted)
1529 /* polling may fail silently until the target has been examined */
1530 if((retval = target_poll(target)) != ERROR_OK)
1534 target = target->next;
1540 int handle_reg_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1549 target = get_current_target(cmd_ctx);
1551 /* list all available registers for the current target */
1554 reg_cache_t *cache = target->reg_cache;
1560 for (i = 0; i < cache->num_regs; i++)
1562 value = buf_to_str(cache->reg_list[i].value, cache->reg_list[i].size, 16);
1563 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);
1566 cache = cache->next;
1572 /* access a single register by its ordinal number */
1573 if ((args[0][0] >= '0') && (args[0][0] <= '9'))
1575 int num = strtoul(args[0], NULL, 0);
1576 reg_cache_t *cache = target->reg_cache;
1582 for (i = 0; i < cache->num_regs; i++)
1586 reg = &cache->reg_list[i];
1592 cache = cache->next;
1597 command_print(cmd_ctx, "%i is out of bounds, the current target has only %i registers (0 - %i)", num, count, count - 1);
1600 } else /* access a single register by its name */
1602 reg = register_get_by_name(target->reg_cache, args[0], 1);
1606 command_print(cmd_ctx, "register %s not found in current target", args[0]);
1611 /* display a register */
1612 if ((argc == 1) || ((argc == 2) && !((args[1][0] >= '0') && (args[1][0] <= '9'))))
1614 if ((argc == 2) && (strcmp(args[1], "force") == 0))
1617 if (reg->valid == 0)
1619 reg_arch_type_t *arch_type = register_get_arch_type(reg->arch_type);
1620 arch_type->get(reg);
1622 value = buf_to_str(reg->value, reg->size, 16);
1623 command_print(cmd_ctx, "%s (/%i): 0x%s", reg->name, reg->size, value);
1628 /* set register value */
1631 u8 *buf = malloc(CEIL(reg->size, 8));
1632 str_to_buf(args[1], strlen(args[1]), buf, reg->size, 0);
1634 reg_arch_type_t *arch_type = register_get_arch_type(reg->arch_type);
1635 arch_type->set(reg, buf);
1637 value = buf_to_str(reg->value, reg->size, 16);
1638 command_print(cmd_ctx, "%s (/%i): 0x%s", reg->name, reg->size, value);
1646 command_print(cmd_ctx, "usage: reg <#|name> [value]");
1651 int handle_poll_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1653 int retval = ERROR_OK;
1654 target_t *target = get_current_target(cmd_ctx);
1658 if((retval = target_poll(target)) != ERROR_OK)
1660 if((retval = target_arch_state(target)) != ERROR_OK)
1666 if (strcmp(args[0], "on") == 0)
1668 target_continous_poll = 1;
1670 else if (strcmp(args[0], "off") == 0)
1672 target_continous_poll = 0;
1676 command_print(cmd_ctx, "arg is \"on\" or \"off\"");
1680 return ERROR_COMMAND_SYNTAX_ERROR;
1686 int handle_wait_halt_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1694 ms = strtoul(args[0], &end, 0) * 1000;
1697 command_print(cmd_ctx, "usage: %s [seconds]", cmd);
1701 target_t *target = get_current_target(cmd_ctx);
1703 return target_wait_state(target, TARGET_HALTED, ms);
1706 int target_wait_state(target_t *target, enum target_state state, int ms)
1709 struct timeval timeout, now;
1711 gettimeofday(&timeout, NULL);
1712 timeval_add_time(&timeout, 0, ms * 1000);
1716 if ((retval=target_poll(target))!=ERROR_OK)
1719 if (target->state == state)
1726 LOG_DEBUG("waiting for target %s...",
1727 Jim_Nvp_value2name_simple(nvp_target_state,state)->name);
1730 gettimeofday(&now, NULL);
1731 if ((now.tv_sec > timeout.tv_sec) || ((now.tv_sec == timeout.tv_sec) && (now.tv_usec >= timeout.tv_usec)))
1733 LOG_ERROR("timed out while waiting for target %s",
1734 Jim_Nvp_value2name_simple(nvp_target_state,state)->name);
1742 int handle_halt_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1745 target_t *target = get_current_target(cmd_ctx);
1749 if ((retval = target_halt(target)) != ERROR_OK)
1754 return handle_wait_halt_command(cmd_ctx, cmd, args, argc);
1757 int handle_soft_reset_halt_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1759 target_t *target = get_current_target(cmd_ctx);
1761 LOG_USER("requesting target halt and executing a soft reset");
1763 target->type->soft_reset_halt(target);
1768 int handle_reset_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1771 enum target_reset_mode reset_mode = RESET_RUN;
1775 n = Jim_Nvp_name2value_simple( nvp_reset_modes, args[0] );
1776 if( (n->name == NULL) || (n->value == RESET_UNKNOWN) ){
1777 return ERROR_COMMAND_SYNTAX_ERROR;
1779 reset_mode = n->value;
1782 /* reset *all* targets */
1783 return target_process_reset(cmd_ctx, reset_mode);
1787 int handle_resume_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1790 target_t *target = get_current_target(cmd_ctx);
1792 target_handle_event( target, TARGET_EVENT_OLD_pre_resume );
1795 retval = target_resume(target, 1, 0, 1, 0); /* current pc, addr = 0, handle breakpoints, not debugging */
1797 retval = target_resume(target, 0, strtoul(args[0], NULL, 0), 1, 0); /* addr = args[0], handle breakpoints, not debugging */
1800 retval = ERROR_COMMAND_SYNTAX_ERROR;
1806 int handle_step_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1808 target_t *target = get_current_target(cmd_ctx);
1813 return target->type->step(target, 1, 0, 1); /* current pc, addr = 0, handle breakpoints */
1816 return target->type->step(target, 0, strtoul(args[0], NULL, 0), 1); /* addr = args[0], handle breakpoints */
1821 int handle_md_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1823 const int line_bytecnt = 32;
1836 target_t *target = get_current_target(cmd_ctx);
1842 count = strtoul(args[1], NULL, 0);
1844 address = strtoul(args[0], NULL, 0);
1849 size = 4; line_modulo = line_bytecnt / 4;
1852 size = 2; line_modulo = line_bytecnt / 2;
1855 size = 1; line_modulo = line_bytecnt / 1;
1861 buffer = calloc(count, size);
1862 retval = target->type->read_memory(target, address, size, count, buffer);
1863 if (retval == ERROR_OK)
1867 for (i = 0; i < count; i++)
1869 if (i%line_modulo == 0)
1870 output_len += snprintf(output + output_len, 128 - output_len, "0x%8.8x: ", address + (i*size));
1875 output_len += snprintf(output + output_len, 128 - output_len, "%8.8x ", target_buffer_get_u32(target, &buffer[i*4]));
1878 output_len += snprintf(output + output_len, 128 - output_len, "%4.4x ", target_buffer_get_u16(target, &buffer[i*2]));
1881 output_len += snprintf(output + output_len, 128 - output_len, "%2.2x ", buffer[i*1]);
1885 if ((i%line_modulo == line_modulo-1) || (i == count - 1))
1887 command_print(cmd_ctx, output);
1898 int handle_mw_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1905 target_t *target = get_current_target(cmd_ctx);
1908 if ((argc < 2) || (argc > 3))
1909 return ERROR_COMMAND_SYNTAX_ERROR;
1911 address = strtoul(args[0], NULL, 0);
1912 value = strtoul(args[1], NULL, 0);
1914 count = strtoul(args[2], NULL, 0);
1920 target_buffer_set_u32(target, value_buf, value);
1924 target_buffer_set_u16(target, value_buf, value);
1928 value_buf[0] = value;
1931 return ERROR_COMMAND_SYNTAX_ERROR;
1933 for (i=0; i<count; i++)
1939 retval = target->type->write_memory(target, address + i*wordsize, 4, 1, value_buf);
1942 retval = target->type->write_memory(target, address + i*wordsize, 2, 1, value_buf);
1945 retval = target->type->write_memory(target, address + i*wordsize, 1, 1, value_buf);
1952 if (retval!=ERROR_OK)
1962 int handle_load_image_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1968 u32 max_address=0xffffffff;
1970 int retval, retvaltemp;
1974 duration_t duration;
1975 char *duration_text;
1977 target_t *target = get_current_target(cmd_ctx);
1979 if ((argc < 1)||(argc > 5))
1981 return ERROR_COMMAND_SYNTAX_ERROR;
1984 /* a base address isn't always necessary, default to 0x0 (i.e. don't relocate) */
1987 image.base_address_set = 1;
1988 image.base_address = strtoul(args[1], NULL, 0);
1992 image.base_address_set = 0;
1996 image.start_address_set = 0;
2000 min_address=strtoul(args[3], NULL, 0);
2004 max_address=strtoul(args[4], NULL, 0)+min_address;
2007 if (min_address>max_address)
2009 return ERROR_COMMAND_SYNTAX_ERROR;
2012 duration_start_measure(&duration);
2014 if (image_open(&image, args[0], (argc >= 3) ? args[2] : NULL) != ERROR_OK)
2021 for (i = 0; i < image.num_sections; i++)
2023 buffer = malloc(image.sections[i].size);
2026 command_print(cmd_ctx, "error allocating buffer for section (%d bytes)", image.sections[i].size);
2030 if ((retval = image_read_section(&image, i, 0x0, image.sections[i].size, buffer, &buf_cnt)) != ERROR_OK)
2039 /* DANGER!!! beware of unsigned comparision here!!! */
2041 if ((image.sections[i].base_address+buf_cnt>=min_address)&&
2042 (image.sections[i].base_address<max_address))
2044 if (image.sections[i].base_address<min_address)
2046 /* clip addresses below */
2047 offset+=min_address-image.sections[i].base_address;
2051 if (image.sections[i].base_address+buf_cnt>max_address)
2053 length-=(image.sections[i].base_address+buf_cnt)-max_address;
2056 if ((retval = target_write_buffer(target, image.sections[i].base_address+offset, length, buffer+offset)) != ERROR_OK)
2061 image_size += length;
2062 command_print(cmd_ctx, "%u byte written at address 0x%8.8x", length, image.sections[i].base_address+offset);
2068 if((retvaltemp = duration_stop_measure(&duration, &duration_text)) != ERROR_OK)
2070 image_close(&image);
2074 if (retval==ERROR_OK)
2076 command_print(cmd_ctx, "downloaded %u byte in %s", image_size, duration_text);
2078 free(duration_text);
2080 image_close(&image);
2086 int handle_dump_image_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2093 int retval=ERROR_OK, retvaltemp;
2095 duration_t duration;
2096 char *duration_text;
2098 target_t *target = get_current_target(cmd_ctx);
2102 command_print(cmd_ctx, "usage: dump_image <filename> <address> <size>");
2106 address = strtoul(args[1], NULL, 0);
2107 size = strtoul(args[2], NULL, 0);
2109 if ((address & 3) || (size & 3))
2111 command_print(cmd_ctx, "only 32-bit aligned address and size are supported");
2115 if (fileio_open(&fileio, args[0], FILEIO_WRITE, FILEIO_BINARY) != ERROR_OK)
2120 duration_start_measure(&duration);
2125 u32 this_run_size = (size > 560) ? 560 : size;
2127 retval = target->type->read_memory(target, address, 4, this_run_size / 4, buffer);
2128 if (retval != ERROR_OK)
2133 retval = fileio_write(&fileio, this_run_size, buffer, &size_written);
2134 if (retval != ERROR_OK)
2139 size -= this_run_size;
2140 address += this_run_size;
2143 if((retvaltemp = fileio_close(&fileio)) != ERROR_OK)
2146 if((retvaltemp = duration_stop_measure(&duration, &duration_text)) != ERROR_OK)
2149 if (retval==ERROR_OK)
2151 command_print(cmd_ctx, "dumped %"PRIi64" byte in %s", fileio.size, duration_text);
2153 free(duration_text);
2158 int handle_verify_image_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2164 int retval, retvaltemp;
2166 u32 mem_checksum = 0;
2170 duration_t duration;
2171 char *duration_text;
2173 target_t *target = get_current_target(cmd_ctx);
2177 return ERROR_COMMAND_SYNTAX_ERROR;
2182 LOG_ERROR("no target selected");
2186 duration_start_measure(&duration);
2190 image.base_address_set = 1;
2191 image.base_address = strtoul(args[1], NULL, 0);
2195 image.base_address_set = 0;
2196 image.base_address = 0x0;
2199 image.start_address_set = 0;
2201 if ((retval=image_open(&image, args[0], (argc == 3) ? args[2] : NULL)) != ERROR_OK)
2208 for (i = 0; i < image.num_sections; i++)
2210 buffer = malloc(image.sections[i].size);
2213 command_print(cmd_ctx, "error allocating buffer for section (%d bytes)", image.sections[i].size);
2216 if ((retval = image_read_section(&image, i, 0x0, image.sections[i].size, buffer, &buf_cnt)) != ERROR_OK)
2222 /* calculate checksum of image */
2223 image_calculate_checksum( buffer, buf_cnt, &checksum );
2225 retval = target_checksum_memory(target, image.sections[i].base_address, buf_cnt, &mem_checksum);
2226 if( retval != ERROR_OK )
2232 if( checksum != mem_checksum )
2234 /* failed crc checksum, fall back to a binary compare */
2237 command_print(cmd_ctx, "checksum mismatch - attempting binary compare");
2239 data = (u8*)malloc(buf_cnt);
2241 /* Can we use 32bit word accesses? */
2243 int count = buf_cnt;
2244 if ((count % 4) == 0)
2249 retval = target->type->read_memory(target, image.sections[i].base_address, size, count, data);
2250 if (retval == ERROR_OK)
2253 for (t = 0; t < buf_cnt; t++)
2255 if (data[t] != buffer[t])
2257 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]);
2274 image_size += buf_cnt;
2278 if((retvaltemp = duration_stop_measure(&duration, &duration_text)) != ERROR_OK)
2280 image_close(&image);
2284 if (retval==ERROR_OK)
2286 command_print(cmd_ctx, "verified %u bytes in %s", image_size, duration_text);
2288 free(duration_text);
2290 image_close(&image);
2295 int handle_bp_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2298 target_t *target = get_current_target(cmd_ctx);
2302 breakpoint_t *breakpoint = target->breakpoints;
2306 if (breakpoint->type == BKPT_SOFT)
2308 char* buf = buf_to_str(breakpoint->orig_instr, breakpoint->length, 16);
2309 command_print(cmd_ctx, "0x%8.8x, 0x%x, %i, 0x%s", breakpoint->address, breakpoint->length, breakpoint->set, buf);
2314 command_print(cmd_ctx, "0x%8.8x, 0x%x, %i", breakpoint->address, breakpoint->length, breakpoint->set);
2316 breakpoint = breakpoint->next;
2324 length = strtoul(args[1], NULL, 0);
2327 if (strcmp(args[2], "hw") == 0)
2330 if ((retval = breakpoint_add(target, strtoul(args[0], NULL, 0), length, hw)) != ERROR_OK)
2332 LOG_ERROR("Failure setting breakpoints");
2336 command_print(cmd_ctx, "breakpoint added at address 0x%8.8x", strtoul(args[0], NULL, 0));
2341 command_print(cmd_ctx, "usage: bp <address> <length> ['hw']");
2347 int handle_rbp_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2349 target_t *target = get_current_target(cmd_ctx);
2352 breakpoint_remove(target, strtoul(args[0], NULL, 0));
2357 int handle_wp_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2359 target_t *target = get_current_target(cmd_ctx);
2364 watchpoint_t *watchpoint = target->watchpoints;
2368 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);
2369 watchpoint = watchpoint->next;
2374 enum watchpoint_rw type = WPT_ACCESS;
2375 u32 data_value = 0x0;
2376 u32 data_mask = 0xffffffff;
2392 command_print(cmd_ctx, "usage: wp <address> <length> [r/w/a] [value] [mask]");
2398 data_value = strtoul(args[3], NULL, 0);
2402 data_mask = strtoul(args[4], NULL, 0);
2405 if ((retval = watchpoint_add(target, strtoul(args[0], NULL, 0),
2406 strtoul(args[1], NULL, 0), type, data_value, data_mask)) != ERROR_OK)
2408 LOG_ERROR("Failure setting breakpoints");
2413 command_print(cmd_ctx, "usage: wp <address> <length> [r/w/a] [value] [mask]");
2419 int handle_rwp_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2421 target_t *target = get_current_target(cmd_ctx);
2424 watchpoint_remove(target, strtoul(args[0], NULL, 0));
2429 int handle_virt2phys_command(command_context_t *cmd_ctx, char *cmd, char **args, int argc)
2432 target_t *target = get_current_target(cmd_ctx);
2438 return ERROR_COMMAND_SYNTAX_ERROR;
2440 va = strtoul(args[0], NULL, 0);
2442 retval = target->type->virt2phys(target, va, &pa);
2443 if (retval == ERROR_OK)
2445 command_print(cmd_ctx, "Physical address 0x%08x", pa);
2449 /* lower levels will have logged a detailed error which is
2450 * forwarded to telnet/GDB session.
2456 static void writeLong(FILE *f, int l)
2461 char c=(l>>(i*8))&0xff;
2462 fwrite(&c, 1, 1, f);
2467 static void writeString(FILE *f, char *s)
2469 fwrite(s, 1, strlen(s), f);
2472 /* Dump a gmon.out histogram file. */
2473 static void writeGmon(u32 *samples, int sampleNum, char *filename)
2476 FILE *f=fopen(filename, "w");
2479 fwrite("gmon", 1, 4, f);
2480 writeLong(f, 0x00000001); /* Version */
2481 writeLong(f, 0); /* padding */
2482 writeLong(f, 0); /* padding */
2483 writeLong(f, 0); /* padding */
2485 fwrite("", 1, 1, f); /* GMON_TAG_TIME_HIST */
2487 /* figure out bucket size */
2490 for (i=0; i<sampleNum; i++)
2502 int addressSpace=(max-min+1);
2504 static int const maxBuckets=256*1024; /* maximum buckets. */
2505 int length=addressSpace;
2506 if (length > maxBuckets)
2510 int *buckets=malloc(sizeof(int)*length);
2516 memset(buckets, 0, sizeof(int)*length);
2517 for (i=0; i<sampleNum;i++)
2519 u32 address=samples[i];
2520 long long a=address-min;
2521 long long b=length-1;
2522 long long c=addressSpace-1;
2523 int index=(a*b)/c; /* danger!!!! int32 overflows */
2527 /* append binary memory gmon.out &profile_hist_hdr ((char*)&profile_hist_hdr + sizeof(struct gmon_hist_hdr)) */
2528 writeLong(f, min); /* low_pc */
2529 writeLong(f, max); /* high_pc */
2530 writeLong(f, length); /* # of samples */
2531 writeLong(f, 64000000); /* 64MHz */
2532 writeString(f, "seconds");
2533 for (i=0; i<(15-strlen("seconds")); i++)
2535 fwrite("", 1, 1, f); /* padding */
2537 writeString(f, "s");
2539 /*append binary memory gmon.out profile_hist_data (profile_hist_data + profile_hist_hdr.hist_size) */
2541 char *data=malloc(2*length);
2544 for (i=0; i<length;i++)
2553 data[i*2+1]=(val>>8)&0xff;
2556 fwrite(data, 1, length*2, f);
2566 /* profiling samples the CPU PC as quickly as OpenOCD is able, which will be used as a random sampling of PC */
2567 int handle_profile_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2569 target_t *target = get_current_target(cmd_ctx);
2570 struct timeval timeout, now;
2572 gettimeofday(&timeout, NULL);
2575 return ERROR_COMMAND_SYNTAX_ERROR;
2578 timeval_add_time(&timeout, strtoul(args[0], &end, 0), 0);
2584 command_print(cmd_ctx, "Starting profiling. Halting and resuming the target as often as we can...");
2586 static const int maxSample=10000;
2587 u32 *samples=malloc(sizeof(u32)*maxSample);
2592 int retval=ERROR_OK;
2593 /* hopefully it is safe to cache! We want to stop/restart as quickly as possible. */
2594 reg_t *reg = register_get_by_name(target->reg_cache, "pc", 1);
2598 target_poll(target);
2599 if (target->state == TARGET_HALTED)
2601 u32 t=*((u32 *)reg->value);
2602 samples[numSamples++]=t;
2603 retval = target_resume(target, 1, 0, 0, 0); /* current pc, addr = 0, do not handle breakpoints, not debugging */
2604 target_poll(target);
2605 alive_sleep(10); /* sleep 10ms, i.e. <100 samples/second. */
2606 } else if (target->state == TARGET_RUNNING)
2608 /* We want to quickly sample the PC. */
2609 if((retval = target_halt(target)) != ERROR_OK)
2616 command_print(cmd_ctx, "Target not halted or running");
2620 if (retval!=ERROR_OK)
2625 gettimeofday(&now, NULL);
2626 if ((numSamples>=maxSample) || ((now.tv_sec >= timeout.tv_sec) && (now.tv_usec >= timeout.tv_usec)))
2628 command_print(cmd_ctx, "Profiling completed. %d samples.", numSamples);
2629 if((retval = target_poll(target)) != ERROR_OK)
2634 if (target->state == TARGET_HALTED)
2636 target_resume(target, 1, 0, 0, 0); /* current pc, addr = 0, do not handle breakpoints, not debugging */
2638 if((retval = target_poll(target)) != ERROR_OK)
2643 writeGmon(samples, numSamples, args[1]);
2644 command_print(cmd_ctx, "Wrote %s", args[1]);
2653 static int new_int_array_element(Jim_Interp * interp, const char *varname, int idx, u32 val)
2656 Jim_Obj *nameObjPtr, *valObjPtr;
2659 namebuf = alloc_printf("%s(%d)", varname, idx);
2663 nameObjPtr = Jim_NewStringObj(interp, namebuf, -1);
2664 valObjPtr = Jim_NewIntObj(interp, val);
2665 if (!nameObjPtr || !valObjPtr)
2671 Jim_IncrRefCount(nameObjPtr);
2672 Jim_IncrRefCount(valObjPtr);
2673 result = Jim_SetVariable(interp, nameObjPtr, valObjPtr);
2674 Jim_DecrRefCount(interp, nameObjPtr);
2675 Jim_DecrRefCount(interp, valObjPtr);
2677 /* printf("%s(%d) <= 0%08x\n", varname, idx, val); */
2681 static int jim_mem2array(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
2683 command_context_t *context;
2686 context = Jim_GetAssocData(interp, "context");
2687 if (context == NULL)
2689 LOG_ERROR("mem2array: no command context");
2692 target = get_current_target(context);
2695 LOG_ERROR("mem2array: no current target");
2699 return target_mem2array(interp, target, argc,argv);
2702 static int target_mem2array(Jim_Interp *interp, target_t *target, int argc, Jim_Obj *const *argv)
2710 const char *varname;
2712 int i, n, e, retval;
2714 /* argv[1] = name of array to receive the data
2715 * argv[2] = desired width
2716 * argv[3] = memory address
2717 * argv[4] = count of times to read
2720 Jim_WrongNumArgs(interp, 1, argv, "varname width addr nelems");
2723 varname = Jim_GetString(argv[1], &len);
2724 /* given "foo" get space for worse case "foo(%d)" .. add 20 */
2726 e = Jim_GetLong(interp, argv[2], &l);
2732 e = Jim_GetLong(interp, argv[3], &l);
2737 e = Jim_GetLong(interp, argv[4], &l);
2753 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
2754 Jim_AppendStrings( interp, Jim_GetResult(interp), "Invalid width param, must be 8/16/32", NULL );
2758 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
2759 Jim_AppendStrings(interp, Jim_GetResult(interp), "mem2array: zero width read?", NULL);
2762 if ((addr + (len * width)) < addr) {
2763 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
2764 Jim_AppendStrings(interp, Jim_GetResult(interp), "mem2array: addr + len - wraps to zero?", NULL);
2767 /* absurd transfer size? */
2769 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
2770 Jim_AppendStrings(interp, Jim_GetResult(interp), "mem2array: absurd > 64K item request", NULL);
2775 ((width == 2) && ((addr & 1) == 0)) ||
2776 ((width == 4) && ((addr & 3) == 0))) {
2780 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
2781 sprintf(buf, "mem2array address: 0x%08x is not aligned for %d byte reads", addr, width);
2782 Jim_AppendStrings(interp, Jim_GetResult(interp), buf , NULL);
2793 /* Slurp... in buffer size chunks */
2795 count = len; /* in objects.. */
2796 if (count > (sizeof(buffer)/width)) {
2797 count = (sizeof(buffer)/width);
2800 retval = target->type->read_memory( target, addr, width, count, buffer );
2801 if (retval != ERROR_OK) {
2803 LOG_ERROR("mem2array: Read @ 0x%08x, w=%d, cnt=%d, failed", addr, width, count);
2804 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
2805 Jim_AppendStrings(interp, Jim_GetResult(interp), "mem2array: cannot read memory", NULL);
2809 v = 0; /* shut up gcc */
2810 for (i = 0 ;i < count ;i++, n++) {
2813 v = target_buffer_get_u32(target, &buffer[i*width]);
2816 v = target_buffer_get_u16(target, &buffer[i*width]);
2819 v = buffer[i] & 0x0ff;
2822 new_int_array_element(interp, varname, n, v);
2828 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
2833 static int get_int_array_element(Jim_Interp * interp, const char *varname, int idx, u32 *val)
2836 Jim_Obj *nameObjPtr, *valObjPtr;
2840 namebuf = alloc_printf("%s(%d)", varname, idx);
2844 nameObjPtr = Jim_NewStringObj(interp, namebuf, -1);
2851 Jim_IncrRefCount(nameObjPtr);
2852 valObjPtr = Jim_GetVariable(interp, nameObjPtr, JIM_ERRMSG);
2853 Jim_DecrRefCount(interp, nameObjPtr);
2855 if (valObjPtr == NULL)
2858 result = Jim_GetLong(interp, valObjPtr, &l);
2859 /* printf("%s(%d) => 0%08x\n", varname, idx, val); */
2864 static int jim_array2mem(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
2866 command_context_t *context;
2869 context = Jim_GetAssocData(interp, "context");
2870 if (context == NULL){
2871 LOG_ERROR("array2mem: no command context");
2874 target = get_current_target(context);
2875 if (target == NULL){
2876 LOG_ERROR("array2mem: no current target");
2880 return target_array2mem( interp,target, argc, argv );
2883 static int target_array2mem(Jim_Interp *interp, target_t *target, int argc, Jim_Obj *const *argv)
2891 const char *varname;
2893 int i, n, e, retval;
2895 /* argv[1] = name of array to get the data
2896 * argv[2] = desired width
2897 * argv[3] = memory address
2898 * argv[4] = count to write
2901 Jim_WrongNumArgs(interp, 1, argv, "varname width addr nelems");
2904 varname = Jim_GetString(argv[1], &len);
2905 /* given "foo" get space for worse case "foo(%d)" .. add 20 */
2907 e = Jim_GetLong(interp, argv[2], &l);
2913 e = Jim_GetLong(interp, argv[3], &l);
2918 e = Jim_GetLong(interp, argv[4], &l);
2934 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
2935 Jim_AppendStrings( interp, Jim_GetResult(interp), "Invalid width param, must be 8/16/32", NULL );
2939 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
2940 Jim_AppendStrings(interp, Jim_GetResult(interp), "array2mem: zero width read?", NULL);
2943 if ((addr + (len * width)) < addr) {
2944 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
2945 Jim_AppendStrings(interp, Jim_GetResult(interp), "array2mem: addr + len - wraps to zero?", NULL);
2948 /* absurd transfer size? */
2950 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
2951 Jim_AppendStrings(interp, Jim_GetResult(interp), "array2mem: absurd > 64K item request", NULL);
2956 ((width == 2) && ((addr & 1) == 0)) ||
2957 ((width == 4) && ((addr & 3) == 0))) {
2961 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
2962 sprintf(buf, "array2mem address: 0x%08x is not aligned for %d byte reads", addr, width);
2963 Jim_AppendStrings(interp, Jim_GetResult(interp), buf , NULL);
2974 /* Slurp... in buffer size chunks */
2976 count = len; /* in objects.. */
2977 if (count > (sizeof(buffer)/width)) {
2978 count = (sizeof(buffer)/width);
2981 v = 0; /* shut up gcc */
2982 for (i = 0 ;i < count ;i++, n++) {
2983 get_int_array_element(interp, varname, n, &v);
2986 target_buffer_set_u32(target, &buffer[i*width], v);
2989 target_buffer_set_u16(target, &buffer[i*width], v);
2992 buffer[i] = v & 0x0ff;
2998 retval = target->type->write_memory(target, addr, width, count, buffer);
2999 if (retval != ERROR_OK) {
3001 LOG_ERROR("array2mem: Write @ 0x%08x, w=%d, cnt=%d, failed", addr, width, count);
3002 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3003 Jim_AppendStrings(interp, Jim_GetResult(interp), "array2mem: cannot read memory", NULL);
3009 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3014 void target_all_handle_event( enum target_event e )
3018 LOG_DEBUG( "**all*targets: event: %d, %s",
3020 Jim_Nvp_value2name_simple( nvp_target_event, e )->name );
3022 target = all_targets;
3024 target_handle_event( target, e );
3025 target = target->next;
3029 void target_handle_event( target_t *target, enum target_event e )
3031 target_event_action_t *teap;
3034 teap = target->event_action;
3038 if( teap->event == e ){
3040 LOG_DEBUG( "target: (%d) %s (%s) event: %d (%s) action: %s\n",
3041 target->target_number,
3045 Jim_Nvp_value2name_simple( nvp_target_event, e )->name,
3046 Jim_GetString( teap->body, NULL ) );
3047 if (Jim_EvalObj( interp, teap->body )!=JIM_OK)
3049 Jim_PrintErrorMessage(interp);
3055 LOG_DEBUG( "event: %d %s - no action",
3057 Jim_Nvp_value2name_simple( nvp_target_event, e )->name );
3061 enum target_cfg_param {
3064 TCFG_WORK_AREA_VIRT,
3065 TCFG_WORK_AREA_PHYS,
3066 TCFG_WORK_AREA_SIZE,
3067 TCFG_WORK_AREA_BACKUP,
3070 TCFG_CHAIN_POSITION,
3073 static Jim_Nvp nvp_config_opts[] = {
3074 { .name = "-type", .value = TCFG_TYPE },
3075 { .name = "-event", .value = TCFG_EVENT },
3076 { .name = "-work-area-virt", .value = TCFG_WORK_AREA_VIRT },
3077 { .name = "-work-area-phys", .value = TCFG_WORK_AREA_PHYS },
3078 { .name = "-work-area-size", .value = TCFG_WORK_AREA_SIZE },
3079 { .name = "-work-area-backup", .value = TCFG_WORK_AREA_BACKUP },
3080 { .name = "-endian" , .value = TCFG_ENDIAN },
3081 { .name = "-variant", .value = TCFG_VARIANT },
3082 { .name = "-chain-position", .value = TCFG_CHAIN_POSITION },
3084 { .name = NULL, .value = -1 }
3087 static int target_configure( Jim_GetOptInfo *goi, target_t *target )
3095 /* parse config or cget options ... */
3096 while( goi->argc > 0 ){
3097 Jim_SetEmptyResult( goi->interp );
3098 /* Jim_GetOpt_Debug( goi ); */
3100 if( target->type->target_jim_configure ){
3101 /* target defines a configure function */
3102 /* target gets first dibs on parameters */
3103 e = (*(target->type->target_jim_configure))( target, goi );
3112 /* otherwise we 'continue' below */
3114 e = Jim_GetOpt_Nvp( goi, nvp_config_opts, &n );
3116 Jim_GetOpt_NvpUnknown( goi, nvp_config_opts, 0 );
3122 if( goi->isconfigure ){
3123 Jim_SetResult_sprintf( goi->interp, "not setable: %s", n->name );
3127 if( goi->argc != 0 ){
3128 Jim_WrongNumArgs( goi->interp, goi->argc, goi->argv, "NO PARAMS");
3132 Jim_SetResultString( goi->interp, target->type->name, -1 );
3136 if( goi->argc == 0 ){
3137 Jim_WrongNumArgs( goi->interp, goi->argc, goi->argv, "-event ?event-name? ...");
3141 e = Jim_GetOpt_Nvp( goi, nvp_target_event, &n );
3143 Jim_GetOpt_NvpUnknown( goi, nvp_target_event, 1 );
3147 if( goi->isconfigure ){
3148 if( goi->argc != 1 ){
3149 Jim_WrongNumArgs( goi->interp, goi->argc, goi->argv, "-event ?event-name? ?EVENT-BODY?");
3153 if( goi->argc != 0 ){
3154 Jim_WrongNumArgs(goi->interp, goi->argc, goi->argv, "-event ?event-name?");
3160 target_event_action_t *teap;
3162 teap = target->event_action;
3163 /* replace existing? */
3165 if( teap->event == n->value ){
3171 if( goi->isconfigure ){
3174 teap = calloc( 1, sizeof(*teap) );
3176 teap->event = n->value;
3177 Jim_GetOpt_Obj( goi, &o );
3179 Jim_DecrRefCount( interp, teap->body );
3181 teap->body = Jim_DuplicateObj( goi->interp, o );
3184 * Tcl/TK - "tk events" have a nice feature.
3185 * See the "BIND" command.
3186 * We should support that here.
3187 * You can specify %X and %Y in the event code.
3188 * The idea is: %T - target name.
3189 * The idea is: %N - target number
3190 * The idea is: %E - event name.
3192 Jim_IncrRefCount( teap->body );
3194 /* add to head of event list */
3195 teap->next = target->event_action;
3196 target->event_action = teap;
3197 Jim_SetEmptyResult(goi->interp);
3201 Jim_SetEmptyResult( goi->interp );
3203 Jim_SetResult( goi->interp, Jim_DuplicateObj( goi->interp, teap->body ) );
3210 case TCFG_WORK_AREA_VIRT:
3211 if( goi->isconfigure ){
3212 target_free_all_working_areas(target);
3213 e = Jim_GetOpt_Wide( goi, &w );
3217 target->working_area_virt = w;
3219 if( goi->argc != 0 ){
3223 Jim_SetResult( interp, Jim_NewIntObj( goi->interp, target->working_area_virt ) );
3227 case TCFG_WORK_AREA_PHYS:
3228 if( goi->isconfigure ){
3229 target_free_all_working_areas(target);
3230 e = Jim_GetOpt_Wide( goi, &w );
3234 target->working_area_phys = w;
3236 if( goi->argc != 0 ){
3240 Jim_SetResult( interp, Jim_NewIntObj( goi->interp, target->working_area_phys ) );
3244 case TCFG_WORK_AREA_SIZE:
3245 if( goi->isconfigure ){
3246 target_free_all_working_areas(target);
3247 e = Jim_GetOpt_Wide( goi, &w );
3251 target->working_area_size = w;
3253 if( goi->argc != 0 ){
3257 Jim_SetResult( interp, Jim_NewIntObj( goi->interp, target->working_area_size ) );
3261 case TCFG_WORK_AREA_BACKUP:
3262 if( goi->isconfigure ){
3263 target_free_all_working_areas(target);
3264 e = Jim_GetOpt_Wide( goi, &w );
3268 /* make this exactly 1 or 0 */
3269 target->backup_working_area = (!!w);
3271 if( goi->argc != 0 ){
3275 Jim_SetResult( interp, Jim_NewIntObj( goi->interp, target->working_area_size ) );
3276 /* loop for more e*/
3280 if( goi->isconfigure ){
3281 e = Jim_GetOpt_Nvp( goi, nvp_target_endian, &n );
3283 Jim_GetOpt_NvpUnknown( goi, nvp_target_endian, 1 );
3286 target->endianness = n->value;
3288 if( goi->argc != 0 ){
3292 n = Jim_Nvp_value2name_simple( nvp_target_endian, target->endianness );
3293 if( n->name == NULL ){
3294 target->endianness = TARGET_LITTLE_ENDIAN;
3295 n = Jim_Nvp_value2name_simple( nvp_target_endian, target->endianness );
3297 Jim_SetResultString( goi->interp, n->name, -1 );
3302 if( goi->isconfigure ){
3303 if( goi->argc < 1 ){
3304 Jim_SetResult_sprintf( goi->interp,
3309 if( target->variant ){
3310 free((void *)(target->variant));
3312 e = Jim_GetOpt_String( goi, &cp, NULL );
3313 target->variant = strdup(cp);
3315 if( goi->argc != 0 ){
3319 Jim_SetResultString( goi->interp, target->variant,-1 );
3322 case TCFG_CHAIN_POSITION:
3323 if( goi->isconfigure ){
3326 target_free_all_working_areas(target);
3327 e = Jim_GetOpt_Obj( goi, &o );
3331 tap = jtag_TapByJimObj( goi->interp, o );
3335 /* make this exactly 1 or 0 */
3338 if( goi->argc != 0 ){
3342 Jim_SetResultString( interp, target->tap->dotted_name, -1 );
3343 /* loop for more e*/
3346 } /* while( goi->argc ) */
3347 /* done - we return */
3351 /** this is the 'tcl' handler for the target specific command */
3352 static int tcl_target_func( Jim_Interp *interp, int argc, Jim_Obj *const *argv )
3360 struct command_context_s *cmd_ctx;
3367 TS_CMD_MWW, TS_CMD_MWH, TS_CMD_MWB,
3368 TS_CMD_MDW, TS_CMD_MDH, TS_CMD_MDB,
3369 TS_CMD_MRW, TS_CMD_MRH, TS_CMD_MRB,
3370 TS_CMD_MEM2ARRAY, TS_CMD_ARRAY2MEM,
3378 TS_CMD_INVOKE_EVENT,
3381 static const Jim_Nvp target_options[] = {
3382 { .name = "configure", .value = TS_CMD_CONFIGURE },
3383 { .name = "cget", .value = TS_CMD_CGET },
3384 { .name = "mww", .value = TS_CMD_MWW },
3385 { .name = "mwh", .value = TS_CMD_MWH },
3386 { .name = "mwb", .value = TS_CMD_MWB },
3387 { .name = "mdw", .value = TS_CMD_MDW },
3388 { .name = "mdh", .value = TS_CMD_MDH },
3389 { .name = "mdb", .value = TS_CMD_MDB },
3390 { .name = "mem2array", .value = TS_CMD_MEM2ARRAY },
3391 { .name = "array2mem", .value = TS_CMD_ARRAY2MEM },
3392 { .name = "eventlist", .value = TS_CMD_EVENTLIST },
3393 { .name = "curstate", .value = TS_CMD_CURSTATE },
3395 { .name = "arp_examine", .value = TS_CMD_EXAMINE },
3396 { .name = "arp_poll", .value = TS_CMD_POLL },
3397 { .name = "arp_reset", .value = TS_CMD_RESET },
3398 { .name = "arp_halt", .value = TS_CMD_HALT },
3399 { .name = "arp_waitstate", .value = TS_CMD_WAITSTATE },
3400 { .name = "invoke-event", .value = TS_CMD_INVOKE_EVENT },
3402 { .name = NULL, .value = -1 },
3405 /* go past the "command" */
3406 Jim_GetOpt_Setup( &goi, interp, argc-1, argv+1 );
3408 target = Jim_CmdPrivData( goi.interp );
3409 cmd_ctx = Jim_GetAssocData(goi.interp, "context");
3411 /* commands here are in an NVP table */
3412 e = Jim_GetOpt_Nvp( &goi, target_options, &n );
3414 Jim_GetOpt_NvpUnknown( &goi, target_options, 0 );
3417 /* Assume blank result */
3418 Jim_SetEmptyResult( goi.interp );
3421 case TS_CMD_CONFIGURE:
3423 Jim_WrongNumArgs( goi.interp, goi.argc, goi.argv, "missing: -option VALUE ...");
3426 goi.isconfigure = 1;
3427 return target_configure( &goi, target );
3429 // some things take params
3431 Jim_WrongNumArgs( goi.interp, 0, goi.argv, "missing: ?-option?");
3434 goi.isconfigure = 0;
3435 return target_configure( &goi, target );
3443 * argv[3] = optional count.
3446 if( (goi.argc == 3) || (goi.argc == 4) ){
3450 Jim_SetResult_sprintf( goi.interp, "expected: %s ADDR DATA [COUNT]", n->name );
3454 e = Jim_GetOpt_Wide( &goi, &a );
3459 e = Jim_GetOpt_Wide( &goi, &b );
3464 e = Jim_GetOpt_Wide( &goi, &c );
3474 target_buffer_set_u32( target, target_buf, b );
3478 target_buffer_set_u16( target, target_buf, b );
3482 target_buffer_set_u8( target, target_buf, b );
3486 for( x = 0 ; x < c ; x++ ){
3487 e = target->type->write_memory( target, a, b, 1, target_buf );
3488 if( e != ERROR_OK ){
3489 Jim_SetResult_sprintf( interp, "Error writing @ 0x%08x: %d\n", (int)(a), e );
3502 /* argv[0] = command
3504 * argv[2] = optional count
3506 if( (goi.argc == 2) || (goi.argc == 3) ){
3507 Jim_SetResult_sprintf( goi.interp, "expected: %s ADDR [COUNT]", n->name );
3510 e = Jim_GetOpt_Wide( &goi, &a );
3515 e = Jim_GetOpt_Wide( &goi, &c );
3522 b = 1; /* shut up gcc */
3535 /* convert to "bytes" */
3537 /* count is now in 'BYTES' */
3543 e = target->type->read_memory( target, a, b, y / b, target_buf );
3544 if( e != ERROR_OK ){
3545 Jim_SetResult_sprintf( interp, "error reading target @ 0x%08lx", (int)(a) );
3549 Jim_fprintf( interp, interp->cookie_stdout, "0x%08x ", (int)(a) );
3552 for( x = 0 ; (x < 16) && (x < y) ; x += 4 ){
3553 z = target_buffer_get_u32( target, &(target_buf[ x * 4 ]) );
3554 Jim_fprintf( interp, interp->cookie_stdout, "%08x ", (int)(z) );
3556 for( ; (x < 16) ; x += 4 ){
3557 Jim_fprintf( interp, interp->cookie_stdout, " " );
3561 for( x = 0 ; (x < 16) && (x < y) ; x += 2 ){
3562 z = target_buffer_get_u16( target, &(target_buf[ x * 2 ]) );
3563 Jim_fprintf( interp, interp->cookie_stdout, "%04x ", (int)(z) );
3565 for( ; (x < 16) ; x += 2 ){
3566 Jim_fprintf( interp, interp->cookie_stdout, " " );
3571 for( x = 0 ; (x < 16) && (x < y) ; x += 1 ){
3572 z = target_buffer_get_u8( target, &(target_buf[ x * 4 ]) );
3573 Jim_fprintf( interp, interp->cookie_stdout, "%02x ", (int)(z) );
3575 for( ; (x < 16) ; x += 1 ){
3576 Jim_fprintf( interp, interp->cookie_stdout, " " );
3580 /* ascii-ify the bytes */
3581 for( x = 0 ; x < y ; x++ ){
3582 if( (target_buf[x] >= 0x20) &&
3583 (target_buf[x] <= 0x7e) ){
3587 target_buf[x] = '.';
3592 target_buf[x] = ' ';
3597 /* print - with a newline */
3598 Jim_fprintf( interp, interp->cookie_stdout, "%s\n", target_buf );
3604 case TS_CMD_MEM2ARRAY:
3605 return target_mem2array( goi.interp, target, goi.argc, goi.argv );
3607 case TS_CMD_ARRAY2MEM:
3608 return target_array2mem( goi.interp, target, goi.argc, goi.argv );
3610 case TS_CMD_EXAMINE:
3612 Jim_WrongNumArgs( goi.interp, 2, argv, "[no parameters]");
3615 e = target->type->examine( target );
3616 if( e != ERROR_OK ){
3617 Jim_SetResult_sprintf( interp, "examine-fails: %d", e );
3623 Jim_WrongNumArgs( goi.interp, 2, argv, "[no parameters]");
3626 if( !(target->type->examined) ){
3627 e = ERROR_TARGET_NOT_EXAMINED;
3629 e = target->type->poll( target );
3631 if( e != ERROR_OK ){
3632 Jim_SetResult_sprintf( interp, "poll-fails: %d", e );
3639 if( goi.argc != 2 ){
3640 Jim_WrongNumArgs( interp, 2, argv, "t|f|assert|deassert BOOL");
3643 e = Jim_GetOpt_Nvp( &goi, nvp_assert, &n );
3645 Jim_GetOpt_NvpUnknown( &goi, nvp_assert, 1 );
3648 /* the halt or not param */
3649 e = Jim_GetOpt_Wide( &goi, &a);
3653 /* determine if we should halt or not. */
3654 target->reset_halt = !!a;
3655 /* When this happens - all workareas are invalid. */
3656 target_free_all_working_areas_restore(target, 0);
3659 if( n->value == NVP_ASSERT ){
3660 target->type->assert_reset( target );
3662 target->type->deassert_reset( target );
3667 Jim_WrongNumArgs( goi.interp, 0, argv, "halt [no parameters]");
3670 target->type->halt( target );
3672 case TS_CMD_WAITSTATE:
3673 /* params: <name> statename timeoutmsecs */
3674 if( goi.argc != 2 ){
3675 Jim_SetResult_sprintf( goi.interp, "%s STATENAME TIMEOUTMSECS", n->name );
3678 e = Jim_GetOpt_Nvp( &goi, nvp_target_state, &n );
3680 Jim_GetOpt_NvpUnknown( &goi, nvp_target_state,1 );
3683 e = Jim_GetOpt_Wide( &goi, &a );
3687 e = target_wait_state( target, n->value, a );
3688 if( e != ERROR_OK ){
3689 Jim_SetResult_sprintf( goi.interp,
3690 "target: %s wait %s fails (%d) %s",
3693 e, target_strerror_safe(e) );
3698 case TS_CMD_EVENTLIST:
3699 /* List for human, Events defined for this target.
3700 * scripts/programs should use 'name cget -event NAME'
3703 target_event_action_t *teap;
3704 teap = target->event_action;
3705 command_print( cmd_ctx, "Event actions for target (%d) %s\n",
3706 target->target_number,
3708 command_print( cmd_ctx, "%-25s | Body", "Event");
3709 command_print( cmd_ctx, "------------------------- | ----------------------------------------");
3711 command_print( cmd_ctx,
3713 Jim_Nvp_value2name_simple( nvp_target_event, teap->event )->name,
3714 Jim_GetString( teap->body, NULL ) );
3717 command_print( cmd_ctx, "***END***");
3720 case TS_CMD_CURSTATE:
3721 if( goi.argc != 0 ){
3722 Jim_WrongNumArgs( goi.interp, 0, argv, "[no parameters]");
3725 Jim_SetResultString( goi.interp,
3726 Jim_Nvp_value2name_simple(nvp_target_state,target->state)->name,-1);
3728 case TS_CMD_INVOKE_EVENT:
3729 if( goi.argc != 1 ){
3730 Jim_SetResult_sprintf( goi.interp, "%s ?EVENTNAME?",n->name);
3733 e = Jim_GetOpt_Nvp( &goi, nvp_target_event, &n );
3735 Jim_GetOpt_NvpUnknown( &goi, nvp_target_event, 1 );
3738 target_handle_event( target, n->value );
3744 static int target_create( Jim_GetOptInfo *goi )
3753 struct command_context_s *cmd_ctx;
3755 cmd_ctx = Jim_GetAssocData(goi->interp, "context");
3756 if( goi->argc < 3 ){
3757 Jim_WrongNumArgs( goi->interp, 1, goi->argv, "?name? ?type? ..options...");
3762 Jim_GetOpt_Obj( goi, &new_cmd );
3763 /* does this command exist? */
3764 cmd = Jim_GetCommand( goi->interp, new_cmd, JIM_ERRMSG );
3766 cp = Jim_GetString( new_cmd, NULL );
3767 Jim_SetResult_sprintf(goi->interp, "Command/target: %s Exists", cp);
3772 e = Jim_GetOpt_String( goi, &cp2, NULL );
3774 /* now does target type exist */
3775 for( x = 0 ; target_types[x] ; x++ ){
3776 if( 0 == strcmp( cp, target_types[x]->name ) ){
3781 if( target_types[x] == NULL ){
3782 Jim_SetResult_sprintf( goi->interp, "Unknown target type %s, try one of ", cp );
3783 for( x = 0 ; target_types[x] ; x++ ){
3784 if( target_types[x+1] ){
3785 Jim_AppendStrings( goi->interp,
3786 Jim_GetResult(goi->interp),
3787 target_types[x]->name,
3790 Jim_AppendStrings( goi->interp,
3791 Jim_GetResult(goi->interp),
3793 target_types[x]->name,NULL );
3800 target = calloc(1,sizeof(target_t));
3801 /* set target number */
3802 target->target_number = new_target_number();
3804 /* allocate memory for each unique target type */
3805 target->type = (target_type_t*)calloc(1,sizeof(target_type_t));
3807 memcpy( target->type, target_types[x], sizeof(target_type_t));
3809 /* will be set by "-endian" */
3810 target->endianness = TARGET_ENDIAN_UNKNOWN;
3812 target->working_area = 0x0;
3813 target->working_area_size = 0x0;
3814 target->working_areas = NULL;
3815 target->backup_working_area = 0;
3817 target->state = TARGET_UNKNOWN;
3818 target->debug_reason = DBG_REASON_UNDEFINED;
3819 target->reg_cache = NULL;
3820 target->breakpoints = NULL;
3821 target->watchpoints = NULL;
3822 target->next = NULL;
3823 target->arch_info = NULL;
3825 target->display = 1;
3827 /* initialize trace information */
3828 target->trace_info = malloc(sizeof(trace_t));
3829 target->trace_info->num_trace_points = 0;
3830 target->trace_info->trace_points_size = 0;
3831 target->trace_info->trace_points = NULL;
3832 target->trace_info->trace_history_size = 0;
3833 target->trace_info->trace_history = NULL;
3834 target->trace_info->trace_history_pos = 0;
3835 target->trace_info->trace_history_overflowed = 0;
3837 target->dbgmsg = NULL;
3838 target->dbg_msg_enabled = 0;
3840 target->endianness = TARGET_ENDIAN_UNKNOWN;
3842 /* Do the rest as "configure" options */
3843 goi->isconfigure = 1;
3844 e = target_configure( goi, target);
3846 free( target->type );
3851 if( target->endianness == TARGET_ENDIAN_UNKNOWN ){
3852 /* default endian to little if not specified */
3853 target->endianness = TARGET_LITTLE_ENDIAN;
3856 /* incase variant is not set */
3857 if (!target->variant)
3858 target->variant = strdup("");
3860 /* create the target specific commands */
3861 if( target->type->register_commands ){
3862 (*(target->type->register_commands))( cmd_ctx );
3864 if( target->type->target_create ){
3865 (*(target->type->target_create))( target, goi->interp );
3868 /* append to end of list */
3871 tpp = &(all_targets);
3873 tpp = &( (*tpp)->next );
3878 cp = Jim_GetString( new_cmd, NULL );
3879 target->cmd_name = strdup(cp);
3881 /* now - create the new target name command */
3882 e = Jim_CreateCommand( goi->interp,
3885 tcl_target_func, /* C function */
3886 target, /* private data */
3887 NULL ); /* no del proc */
3892 static int jim_target( Jim_Interp *interp, int argc, Jim_Obj *const *argv )
3896 struct command_context_s *cmd_ctx;
3900 /* TG = target generic */
3908 const char *target_cmds[] = {
3909 "create", "types", "names", "current", "number",
3911 NULL /* terminate */
3914 LOG_DEBUG("Target command params:");
3915 LOG_DEBUG(Jim_Debug_ArgvString( interp, argc, argv) );
3917 cmd_ctx = Jim_GetAssocData( interp, "context" );
3919 Jim_GetOpt_Setup( &goi, interp, argc-1, argv+1 );
3921 if( goi.argc == 0 ){
3922 Jim_WrongNumArgs(interp, 1, argv, "missing: command ...");
3926 /* Jim_GetOpt_Debug( &goi ); */
3927 r = Jim_GetOpt_Enum( &goi, target_cmds, &x );
3934 Jim_Panic(goi.interp,"Why am I here?");
3936 case TG_CMD_CURRENT:
3937 if( goi.argc != 0 ){
3938 Jim_WrongNumArgs( goi.interp, 1, goi.argv, "Too many parameters");
3941 Jim_SetResultString( goi.interp, get_current_target( cmd_ctx )->cmd_name, -1 );
3944 if( goi.argc != 0 ){
3945 Jim_WrongNumArgs( goi.interp, 1, goi.argv, "Too many parameters" );
3948 Jim_SetResult( goi.interp, Jim_NewListObj( goi.interp, NULL, 0 ) );
3949 for( x = 0 ; target_types[x] ; x++ ){
3950 Jim_ListAppendElement( goi.interp,
3951 Jim_GetResult(goi.interp),
3952 Jim_NewStringObj( goi.interp, target_types[x]->name, -1 ) );
3956 if( goi.argc != 0 ){
3957 Jim_WrongNumArgs( goi.interp, 1, goi.argv, "Too many parameters" );
3960 Jim_SetResult( goi.interp, Jim_NewListObj( goi.interp, NULL, 0 ) );
3961 target = all_targets;
3963 Jim_ListAppendElement( goi.interp,
3964 Jim_GetResult(goi.interp),
3965 Jim_NewStringObj( goi.interp, target->cmd_name, -1 ) );
3966 target = target->next;
3971 Jim_WrongNumArgs( goi.interp, goi.argc, goi.argv, "?name ... config options ...");
3974 return target_create( &goi );
3977 if( goi.argc != 1 ){
3978 Jim_SetResult_sprintf( goi.interp, "expected: target number ?NUMBER?");
3981 e = Jim_GetOpt_Wide( &goi, &w );
3987 t = get_target_by_num(w);
3989 Jim_SetResult_sprintf( goi.interp,"Target: number %d does not exist", (int)(w));
3992 Jim_SetResultString( goi.interp, t->cmd_name, -1 );
3996 if( goi.argc != 0 ){
3997 Jim_WrongNumArgs( goi.interp, 0, goi.argv, "<no parameters>");
4000 Jim_SetResult( goi.interp,
4001 Jim_NewIntObj( goi.interp, max_target_number()));
4017 static int fastload_num;
4018 static struct FastLoad *fastload;
4020 static void free_fastload()
4025 for (i=0; i<fastload_num; i++)
4027 if (fastload[i].data)
4028 free(fastload[i].data);
4038 int handle_fast_load_image_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
4044 u32 max_address=0xffffffff;
4050 duration_t duration;
4051 char *duration_text;
4053 if ((argc < 1)||(argc > 5))
4055 return ERROR_COMMAND_SYNTAX_ERROR;
4058 /* a base address isn't always necessary, default to 0x0 (i.e. don't relocate) */
4061 image.base_address_set = 1;
4062 image.base_address = strtoul(args[1], NULL, 0);
4066 image.base_address_set = 0;
4070 image.start_address_set = 0;
4074 min_address=strtoul(args[3], NULL, 0);
4078 max_address=strtoul(args[4], NULL, 0)+min_address;
4081 if (min_address>max_address)
4083 return ERROR_COMMAND_SYNTAX_ERROR;
4086 duration_start_measure(&duration);
4088 if (image_open(&image, args[0], (argc >= 3) ? args[2] : NULL) != ERROR_OK)
4095 fastload_num=image.num_sections;
4096 fastload=(struct FastLoad *)malloc(sizeof(struct FastLoad)*image.num_sections);
4099 image_close(&image);
4102 memset(fastload, 0, sizeof(struct FastLoad)*image.num_sections);
4103 for (i = 0; i < image.num_sections; i++)
4105 buffer = malloc(image.sections[i].size);
4108 command_print(cmd_ctx, "error allocating buffer for section (%d bytes)", image.sections[i].size);
4112 if ((retval = image_read_section(&image, i, 0x0, image.sections[i].size, buffer, &buf_cnt)) != ERROR_OK)
4122 /* DANGER!!! beware of unsigned comparision here!!! */
4124 if ((image.sections[i].base_address+buf_cnt>=min_address)&&
4125 (image.sections[i].base_address<max_address))
4127 if (image.sections[i].base_address<min_address)
4129 /* clip addresses below */
4130 offset+=min_address-image.sections[i].base_address;
4134 if (image.sections[i].base_address+buf_cnt>max_address)
4136 length-=(image.sections[i].base_address+buf_cnt)-max_address;
4139 fastload[i].address=image.sections[i].base_address+offset;
4140 fastload[i].data=malloc(length);
4141 if (fastload[i].data==NULL)
4146 memcpy(fastload[i].data, buffer+offset, length);
4147 fastload[i].length=length;
4149 image_size += length;
4150 command_print(cmd_ctx, "%u byte written at address 0x%8.8x", length, image.sections[i].base_address+offset);
4156 duration_stop_measure(&duration, &duration_text);
4157 if (retval==ERROR_OK)
4159 command_print(cmd_ctx, "Loaded %u bytes in %s", image_size, duration_text);
4160 command_print(cmd_ctx, "NB!!! image has not been loaded to target, issue a subsequent 'fast_load' to do so.");
4162 free(duration_text);
4164 image_close(&image);
4166 if (retval!=ERROR_OK)
4174 int handle_fast_load_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
4177 return ERROR_COMMAND_SYNTAX_ERROR;
4180 LOG_ERROR("No image in memory");
4184 int ms=timeval_ms();
4186 for (i=0; i<fastload_num;i++)
4189 target_t *target = get_current_target(cmd_ctx);
4190 if ((retval = target_write_buffer(target, fastload[i].address, fastload[i].length, fastload[i].data)) != ERROR_OK)
4194 size+=fastload[i].length;
4196 int after=timeval_ms();
4197 command_print(cmd_ctx, "Loaded image %f kBytes/s", (float)(size/1024.0)/((float)(after-ms)/1000.0));