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 ***************************************************************************/
37 #include "target_type.h"
38 #include "target_request.h"
39 #include "time_support.h"
46 static int handle_targets_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
48 static int handle_reg_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
49 static int handle_poll_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
50 static int handle_halt_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
51 static int handle_wait_halt_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
52 static int handle_reset_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
53 static int handle_soft_reset_halt_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
54 static int handle_resume_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
55 static int handle_step_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
56 static int handle_md_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
57 static int handle_mw_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
58 static int handle_load_image_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
59 static int handle_dump_image_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
60 static int handle_verify_image_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
61 static int handle_test_image_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
62 static int handle_bp_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
63 static int handle_rbp_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
64 static int handle_wp_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
65 static int handle_rwp_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
66 static int handle_virt2phys_command(command_context_t *cmd_ctx, char *cmd, char **args, int argc);
67 static int handle_profile_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
68 static int handle_fast_load_image_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
69 static int handle_fast_load_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
71 static int jim_array2mem(Jim_Interp *interp, int argc, Jim_Obj *const *argv);
72 static int jim_mem2array(Jim_Interp *interp, int argc, Jim_Obj *const *argv);
73 static int jim_target( Jim_Interp *interp, int argc, Jim_Obj *const *argv);
75 static int target_array2mem(Jim_Interp *interp, target_t *target, int argc, Jim_Obj *const *argv);
76 static int target_mem2array(Jim_Interp *interp, target_t *target, int argc, Jim_Obj *const *argv);
79 extern target_type_t arm7tdmi_target;
80 extern target_type_t arm720t_target;
81 extern target_type_t arm9tdmi_target;
82 extern target_type_t arm920t_target;
83 extern target_type_t arm966e_target;
84 extern target_type_t arm926ejs_target;
85 extern target_type_t fa526_target;
86 extern target_type_t feroceon_target;
87 extern target_type_t xscale_target;
88 extern target_type_t cortexm3_target;
89 extern target_type_t cortexa8_target;
90 extern target_type_t arm11_target;
91 extern target_type_t mips_m4k_target;
92 extern target_type_t avr_target;
94 target_type_t *target_types[] =
113 target_t *all_targets = NULL;
114 target_event_callback_t *target_event_callbacks = NULL;
115 target_timer_callback_t *target_timer_callbacks = NULL;
117 const Jim_Nvp nvp_assert[] = {
118 { .name = "assert", NVP_ASSERT },
119 { .name = "deassert", NVP_DEASSERT },
120 { .name = "T", NVP_ASSERT },
121 { .name = "F", NVP_DEASSERT },
122 { .name = "t", NVP_ASSERT },
123 { .name = "f", NVP_DEASSERT },
124 { .name = NULL, .value = -1 }
127 const Jim_Nvp nvp_error_target[] = {
128 { .value = ERROR_TARGET_INVALID, .name = "err-invalid" },
129 { .value = ERROR_TARGET_INIT_FAILED, .name = "err-init-failed" },
130 { .value = ERROR_TARGET_TIMEOUT, .name = "err-timeout" },
131 { .value = ERROR_TARGET_NOT_HALTED, .name = "err-not-halted" },
132 { .value = ERROR_TARGET_FAILURE, .name = "err-failure" },
133 { .value = ERROR_TARGET_UNALIGNED_ACCESS , .name = "err-unaligned-access" },
134 { .value = ERROR_TARGET_DATA_ABORT , .name = "err-data-abort" },
135 { .value = ERROR_TARGET_RESOURCE_NOT_AVAILABLE , .name = "err-resource-not-available" },
136 { .value = ERROR_TARGET_TRANSLATION_FAULT , .name = "err-translation-fault" },
137 { .value = ERROR_TARGET_NOT_RUNNING, .name = "err-not-running" },
138 { .value = ERROR_TARGET_NOT_EXAMINED, .name = "err-not-examined" },
139 { .value = -1, .name = NULL }
142 const char *target_strerror_safe( int err )
146 n = Jim_Nvp_value2name_simple( nvp_error_target, err );
147 if( n->name == NULL ){
154 static const Jim_Nvp nvp_target_event[] = {
155 { .value = TARGET_EVENT_OLD_gdb_program_config , .name = "old-gdb_program_config" },
156 { .value = TARGET_EVENT_OLD_pre_resume , .name = "old-pre_resume" },
158 { .value = TARGET_EVENT_EARLY_HALTED, .name = "early-halted" },
159 { .value = TARGET_EVENT_HALTED, .name = "halted" },
160 { .value = TARGET_EVENT_RESUMED, .name = "resumed" },
161 { .value = TARGET_EVENT_RESUME_START, .name = "resume-start" },
162 { .value = TARGET_EVENT_RESUME_END, .name = "resume-end" },
164 { .name = "gdb-start", .value = TARGET_EVENT_GDB_START },
165 { .name = "gdb-end", .value = TARGET_EVENT_GDB_END },
167 /* historical name */
169 { .value = TARGET_EVENT_RESET_START, .name = "reset-start" },
171 { .value = TARGET_EVENT_RESET_ASSERT_PRE, .name = "reset-assert-pre" },
172 { .value = TARGET_EVENT_RESET_ASSERT_POST, .name = "reset-assert-post" },
173 { .value = TARGET_EVENT_RESET_DEASSERT_PRE, .name = "reset-deassert-pre" },
174 { .value = TARGET_EVENT_RESET_DEASSERT_POST, .name = "reset-deassert-post" },
175 { .value = TARGET_EVENT_RESET_HALT_PRE, .name = "reset-halt-pre" },
176 { .value = TARGET_EVENT_RESET_HALT_POST, .name = "reset-halt-post" },
177 { .value = TARGET_EVENT_RESET_WAIT_PRE, .name = "reset-wait-pre" },
178 { .value = TARGET_EVENT_RESET_WAIT_POST, .name = "reset-wait-post" },
179 { .value = TARGET_EVENT_RESET_INIT , .name = "reset-init" },
180 { .value = TARGET_EVENT_RESET_END, .name = "reset-end" },
182 { .value = TARGET_EVENT_EXAMINE_START, .name = "examine-start" },
183 { .value = TARGET_EVENT_EXAMINE_END, .name = "examine-end" },
185 { .value = TARGET_EVENT_DEBUG_HALTED, .name = "debug-halted" },
186 { .value = TARGET_EVENT_DEBUG_RESUMED, .name = "debug-resumed" },
188 { .value = TARGET_EVENT_GDB_ATTACH, .name = "gdb-attach" },
189 { .value = TARGET_EVENT_GDB_DETACH, .name = "gdb-detach" },
191 { .value = TARGET_EVENT_GDB_FLASH_WRITE_START, .name = "gdb-flash-write-start" },
192 { .value = TARGET_EVENT_GDB_FLASH_WRITE_END , .name = "gdb-flash-write-end" },
194 { .value = TARGET_EVENT_GDB_FLASH_ERASE_START, .name = "gdb-flash-erase-start" },
195 { .value = TARGET_EVENT_GDB_FLASH_ERASE_END , .name = "gdb-flash-erase-end" },
197 { .value = TARGET_EVENT_RESUME_START, .name = "resume-start" },
198 { .value = TARGET_EVENT_RESUMED , .name = "resume-ok" },
199 { .value = TARGET_EVENT_RESUME_END , .name = "resume-end" },
201 { .name = NULL, .value = -1 }
204 const Jim_Nvp nvp_target_state[] = {
205 { .name = "unknown", .value = TARGET_UNKNOWN },
206 { .name = "running", .value = TARGET_RUNNING },
207 { .name = "halted", .value = TARGET_HALTED },
208 { .name = "reset", .value = TARGET_RESET },
209 { .name = "debug-running", .value = TARGET_DEBUG_RUNNING },
210 { .name = NULL, .value = -1 },
213 const Jim_Nvp nvp_target_debug_reason [] = {
214 { .name = "debug-request" , .value = DBG_REASON_DBGRQ },
215 { .name = "breakpoint" , .value = DBG_REASON_BREAKPOINT },
216 { .name = "watchpoint" , .value = DBG_REASON_WATCHPOINT },
217 { .name = "watchpoint-and-breakpoint", .value = DBG_REASON_WPTANDBKPT },
218 { .name = "single-step" , .value = DBG_REASON_SINGLESTEP },
219 { .name = "target-not-halted" , .value = DBG_REASON_NOTHALTED },
220 { .name = "undefined" , .value = DBG_REASON_UNDEFINED },
221 { .name = NULL, .value = -1 },
224 const Jim_Nvp nvp_target_endian[] = {
225 { .name = "big", .value = TARGET_BIG_ENDIAN },
226 { .name = "little", .value = TARGET_LITTLE_ENDIAN },
227 { .name = "be", .value = TARGET_BIG_ENDIAN },
228 { .name = "le", .value = TARGET_LITTLE_ENDIAN },
229 { .name = NULL, .value = -1 },
232 const Jim_Nvp nvp_reset_modes[] = {
233 { .name = "unknown", .value = RESET_UNKNOWN },
234 { .name = "run" , .value = RESET_RUN },
235 { .name = "halt" , .value = RESET_HALT },
236 { .name = "init" , .value = RESET_INIT },
237 { .name = NULL , .value = -1 },
240 static int max_target_number(void)
248 if( x < t->target_number ){
249 x = (t->target_number)+1;
256 /* determine the number of the new target */
257 static int new_target_number(void)
262 /* number is 0 based */
266 if( x < t->target_number ){
267 x = t->target_number;
274 static int target_continuous_poll = 1;
276 /* read a uint32_t from a buffer in target memory endianness */
277 uint32_t target_buffer_get_u32(target_t *target, const uint8_t *buffer)
279 if (target->endianness == TARGET_LITTLE_ENDIAN)
280 return le_to_h_u32(buffer);
282 return be_to_h_u32(buffer);
285 /* read a uint16_t from a buffer in target memory endianness */
286 uint16_t target_buffer_get_u16(target_t *target, const uint8_t *buffer)
288 if (target->endianness == TARGET_LITTLE_ENDIAN)
289 return le_to_h_u16(buffer);
291 return be_to_h_u16(buffer);
294 /* read a uint8_t from a buffer in target memory endianness */
295 uint8_t target_buffer_get_u8(target_t *target, const uint8_t *buffer)
297 return *buffer & 0x0ff;
300 /* write a uint32_t to a buffer in target memory endianness */
301 void target_buffer_set_u32(target_t *target, uint8_t *buffer, uint32_t value)
303 if (target->endianness == TARGET_LITTLE_ENDIAN)
304 h_u32_to_le(buffer, value);
306 h_u32_to_be(buffer, value);
309 /* write a uint16_t to a buffer in target memory endianness */
310 void target_buffer_set_u16(target_t *target, uint8_t *buffer, uint16_t value)
312 if (target->endianness == TARGET_LITTLE_ENDIAN)
313 h_u16_to_le(buffer, value);
315 h_u16_to_be(buffer, value);
318 /* write a uint8_t to a buffer in target memory endianness */
319 void target_buffer_set_u8(target_t *target, uint8_t *buffer, uint8_t value)
324 /* return a pointer to a configured target; id is name or number */
325 target_t *get_target(const char *id)
329 /* try as tcltarget name */
330 for (target = all_targets; target; target = target->next) {
331 if (target->cmd_name == NULL)
333 if (strcmp(id, target->cmd_name) == 0)
337 /* no match, try as number */
339 if (parse_uint(id, &num) != ERROR_OK)
342 for (target = all_targets; target; target = target->next) {
343 if (target->target_number == (int)num)
350 /* returns a pointer to the n-th configured target */
351 static target_t *get_target_by_num(int num)
353 target_t *target = all_targets;
356 if( target->target_number == num ){
359 target = target->next;
365 int get_num_by_target(target_t *query_target)
367 return query_target->target_number;
370 target_t* get_current_target(command_context_t *cmd_ctx)
372 target_t *target = get_target_by_num(cmd_ctx->current_target);
376 LOG_ERROR("BUG: current_target out of bounds");
383 int target_poll(struct target_s *target)
385 /* We can't poll until after examine */
386 if (!target_was_examined(target))
388 /* Fail silently lest we pollute the log */
391 return target->type->poll(target);
394 int target_halt(struct target_s *target)
396 /* We can't poll until after examine */
397 if (!target_was_examined(target))
399 LOG_ERROR("Target not examined yet");
402 return target->type->halt(target);
405 int target_resume(struct target_s *target, int current, uint32_t address, int handle_breakpoints, int debug_execution)
409 /* We can't poll until after examine */
410 if (!target_was_examined(target))
412 LOG_ERROR("Target not examined yet");
416 /* note that resume *must* be asynchronous. The CPU can halt before we poll. The CPU can
417 * even halt at the current PC as a result of a software breakpoint being inserted by (a bug?)
420 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 /* disable polling during reset to make reset event scripts
438 * more predictable, i.e. dr/irscan & pathmove in events will
439 * not have JTAG operations injected into the middle of a sequence.
441 int save_poll = target_continuous_poll;
442 target_continuous_poll = 0;
444 sprintf( buf, "ocd_process_reset %s", n->name );
445 retval = Jim_Eval( interp, buf );
447 target_continuous_poll = save_poll;
449 if(retval != JIM_OK) {
450 Jim_PrintErrorMessage(interp);
454 /* We want any events to be processed before the prompt */
455 retval = target_call_timer_callbacks_now();
460 static int default_virt2phys(struct target_s *target, uint32_t virtual, uint32_t *physical)
466 static int default_mmu(struct target_s *target, int *enabled)
472 static int default_examine(struct target_s *target)
474 target_set_examined(target);
478 int target_examine_one(struct target_s *target)
480 return target->type->examine(target);
483 static int jtag_enable_callback(enum jtag_event event, void *priv)
485 target_t *target = priv;
487 if (event != JTAG_TAP_EVENT_ENABLE || !target->tap->enabled)
490 jtag_unregister_event_callback(jtag_enable_callback, target);
491 return target_examine_one(target);
495 /* Targets that correctly implement init+examine, i.e.
496 * no communication with target during init:
500 int target_examine(void)
502 int retval = ERROR_OK;
505 for (target = all_targets; target; target = target->next)
507 /* defer examination, but don't skip it */
508 if (!target->tap->enabled) {
509 jtag_register_event_callback(jtag_enable_callback,
513 if ((retval = target_examine_one(target)) != ERROR_OK)
518 const char *target_get_name(struct target_s *target)
520 return target->type->name;
523 static int target_write_memory_imp(struct target_s *target, uint32_t address, uint32_t size, uint32_t count, uint8_t *buffer)
525 if (!target_was_examined(target))
527 LOG_ERROR("Target not examined yet");
530 return target->type->write_memory_imp(target, address, size, count, buffer);
533 static int target_read_memory_imp(struct target_s *target, uint32_t address, uint32_t size, uint32_t count, uint8_t *buffer)
535 if (!target_was_examined(target))
537 LOG_ERROR("Target not examined yet");
540 return target->type->read_memory_imp(target, address, size, count, buffer);
543 static int target_soft_reset_halt_imp(struct target_s *target)
545 if (!target_was_examined(target))
547 LOG_ERROR("Target not examined yet");
550 return target->type->soft_reset_halt_imp(target);
553 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, uint32_t entry_point, uint32_t exit_point, int timeout_ms, void *arch_info)
555 if (!target_was_examined(target))
557 LOG_ERROR("Target not examined yet");
560 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);
563 int target_read_memory(struct target_s *target,
564 uint32_t address, uint32_t size, uint32_t count, uint8_t *buffer)
566 return target->type->read_memory(target, address, size, count, buffer);
569 int target_write_memory(struct target_s *target,
570 uint32_t address, uint32_t size, uint32_t count, uint8_t *buffer)
572 return target->type->write_memory(target, address, size, count, buffer);
574 int target_bulk_write_memory(struct target_s *target,
575 uint32_t address, uint32_t count, uint8_t *buffer)
577 return target->type->bulk_write_memory(target, address, count, buffer);
580 int target_add_breakpoint(struct target_s *target,
581 struct breakpoint_s *breakpoint)
583 return target->type->add_breakpoint(target, breakpoint);
585 int target_remove_breakpoint(struct target_s *target,
586 struct breakpoint_s *breakpoint)
588 return target->type->remove_breakpoint(target, breakpoint);
591 int target_add_watchpoint(struct target_s *target,
592 struct watchpoint_s *watchpoint)
594 return target->type->add_watchpoint(target, watchpoint);
596 int target_remove_watchpoint(struct target_s *target,
597 struct watchpoint_s *watchpoint)
599 return target->type->remove_watchpoint(target, watchpoint);
602 int target_get_gdb_reg_list(struct target_s *target,
603 struct reg_s **reg_list[], int *reg_list_size)
605 return target->type->get_gdb_reg_list(target, reg_list, reg_list_size);
607 int target_step(struct target_s *target,
608 int current, uint32_t address, int handle_breakpoints)
610 return target->type->step(target, current, address, handle_breakpoints);
614 int target_run_algorithm(struct target_s *target,
615 int num_mem_params, mem_param_t *mem_params,
616 int num_reg_params, reg_param_t *reg_param,
617 uint32_t entry_point, uint32_t exit_point,
618 int timeout_ms, void *arch_info)
620 return target->type->run_algorithm(target,
621 num_mem_params, mem_params, num_reg_params, reg_param,
622 entry_point, exit_point, timeout_ms, arch_info);
625 /// @returns @c true if the target has been examined.
626 bool target_was_examined(struct target_s *target)
628 return target->type->examined;
630 /// Sets the @c examined flag for the given target.
631 void target_set_examined(struct target_s *target)
633 target->type->examined = true;
635 // Reset the @c examined flag for the given target.
636 void target_reset_examined(struct target_s *target)
638 target->type->examined = false;
642 int target_init(struct command_context_s *cmd_ctx)
644 target_t *target = all_targets;
649 target_reset_examined(target);
650 if (target->type->examine == NULL)
652 target->type->examine = default_examine;
655 if ((retval = target->type->init_target(cmd_ctx, target)) != ERROR_OK)
657 LOG_ERROR("target '%s' init failed", target_get_name(target));
661 /* Set up default functions if none are provided by target */
662 if (target->type->virt2phys == NULL)
664 target->type->virt2phys = default_virt2phys;
666 target->type->virt2phys = default_virt2phys;
667 /* a non-invasive way(in terms of patches) to add some code that
668 * runs before the type->write/read_memory implementation
670 target->type->write_memory_imp = target->type->write_memory;
671 target->type->write_memory = target_write_memory_imp;
672 target->type->read_memory_imp = target->type->read_memory;
673 target->type->read_memory = target_read_memory_imp;
674 target->type->soft_reset_halt_imp = target->type->soft_reset_halt;
675 target->type->soft_reset_halt = target_soft_reset_halt_imp;
676 target->type->run_algorithm_imp = target->type->run_algorithm;
677 target->type->run_algorithm = target_run_algorithm_imp;
679 if (target->type->mmu == NULL)
681 target->type->mmu = default_mmu;
683 target = target->next;
688 if((retval = target_register_user_commands(cmd_ctx)) != ERROR_OK)
690 if((retval = target_register_timer_callback(handle_target, 100, 1, NULL)) != ERROR_OK)
697 int target_register_event_callback(int (*callback)(struct target_s *target, enum target_event event, void *priv), void *priv)
699 target_event_callback_t **callbacks_p = &target_event_callbacks;
701 if (callback == NULL)
703 return ERROR_INVALID_ARGUMENTS;
708 while ((*callbacks_p)->next)
709 callbacks_p = &((*callbacks_p)->next);
710 callbacks_p = &((*callbacks_p)->next);
713 (*callbacks_p) = malloc(sizeof(target_event_callback_t));
714 (*callbacks_p)->callback = callback;
715 (*callbacks_p)->priv = priv;
716 (*callbacks_p)->next = NULL;
721 int target_register_timer_callback(int (*callback)(void *priv), int time_ms, int periodic, void *priv)
723 target_timer_callback_t **callbacks_p = &target_timer_callbacks;
726 if (callback == NULL)
728 return ERROR_INVALID_ARGUMENTS;
733 while ((*callbacks_p)->next)
734 callbacks_p = &((*callbacks_p)->next);
735 callbacks_p = &((*callbacks_p)->next);
738 (*callbacks_p) = malloc(sizeof(target_timer_callback_t));
739 (*callbacks_p)->callback = callback;
740 (*callbacks_p)->periodic = periodic;
741 (*callbacks_p)->time_ms = time_ms;
743 gettimeofday(&now, NULL);
744 (*callbacks_p)->when.tv_usec = now.tv_usec + (time_ms % 1000) * 1000;
745 time_ms -= (time_ms % 1000);
746 (*callbacks_p)->when.tv_sec = now.tv_sec + (time_ms / 1000);
747 if ((*callbacks_p)->when.tv_usec > 1000000)
749 (*callbacks_p)->when.tv_usec = (*callbacks_p)->when.tv_usec - 1000000;
750 (*callbacks_p)->when.tv_sec += 1;
753 (*callbacks_p)->priv = priv;
754 (*callbacks_p)->next = NULL;
759 int target_unregister_event_callback(int (*callback)(struct target_s *target, enum target_event event, void *priv), void *priv)
761 target_event_callback_t **p = &target_event_callbacks;
762 target_event_callback_t *c = target_event_callbacks;
764 if (callback == NULL)
766 return ERROR_INVALID_ARGUMENTS;
771 target_event_callback_t *next = c->next;
772 if ((c->callback == callback) && (c->priv == priv))
786 int target_unregister_timer_callback(int (*callback)(void *priv), void *priv)
788 target_timer_callback_t **p = &target_timer_callbacks;
789 target_timer_callback_t *c = target_timer_callbacks;
791 if (callback == NULL)
793 return ERROR_INVALID_ARGUMENTS;
798 target_timer_callback_t *next = c->next;
799 if ((c->callback == callback) && (c->priv == priv))
813 int target_call_event_callbacks(target_t *target, enum target_event event)
815 target_event_callback_t *callback = target_event_callbacks;
816 target_event_callback_t *next_callback;
818 if (event == TARGET_EVENT_HALTED)
820 /* execute early halted first */
821 target_call_event_callbacks(target, TARGET_EVENT_EARLY_HALTED);
824 LOG_DEBUG("target event %i (%s)",
826 Jim_Nvp_value2name_simple( nvp_target_event, event )->name );
828 target_handle_event( target, event );
832 next_callback = callback->next;
833 callback->callback(target, event, callback->priv);
834 callback = next_callback;
840 static int target_timer_callback_periodic_restart(
841 target_timer_callback_t *cb, struct timeval *now)
843 int time_ms = cb->time_ms;
844 cb->when.tv_usec = now->tv_usec + (time_ms % 1000) * 1000;
845 time_ms -= (time_ms % 1000);
846 cb->when.tv_sec = now->tv_sec + time_ms / 1000;
847 if (cb->when.tv_usec > 1000000)
849 cb->when.tv_usec = cb->when.tv_usec - 1000000;
850 cb->when.tv_sec += 1;
855 static int target_call_timer_callback(target_timer_callback_t *cb,
858 cb->callback(cb->priv);
861 return target_timer_callback_periodic_restart(cb, now);
863 return target_unregister_timer_callback(cb->callback, cb->priv);
866 static int target_call_timer_callbacks_check_time(int checktime)
871 gettimeofday(&now, NULL);
873 target_timer_callback_t *callback = target_timer_callbacks;
876 // cleaning up may unregister and free this callback
877 target_timer_callback_t *next_callback = callback->next;
879 bool call_it = callback->callback &&
880 ((!checktime && callback->periodic) ||
881 now.tv_sec > callback->when.tv_sec ||
882 (now.tv_sec == callback->when.tv_sec &&
883 now.tv_usec >= callback->when.tv_usec));
887 int retval = target_call_timer_callback(callback, &now);
888 if (retval != ERROR_OK)
892 callback = next_callback;
898 int target_call_timer_callbacks(void)
900 return target_call_timer_callbacks_check_time(1);
903 /* invoke periodic callbacks immediately */
904 int target_call_timer_callbacks_now(void)
906 return target_call_timer_callbacks_check_time(0);
909 int target_alloc_working_area(struct target_s *target, uint32_t size, working_area_t **area)
911 working_area_t *c = target->working_areas;
912 working_area_t *new_wa = NULL;
914 /* Reevaluate working area address based on MMU state*/
915 if (target->working_areas == NULL)
919 retval = target->type->mmu(target, &enabled);
920 if (retval != ERROR_OK)
926 target->working_area = target->working_area_virt;
930 target->working_area = target->working_area_phys;
934 /* only allocate multiples of 4 byte */
937 LOG_ERROR("BUG: code tried to allocate unaligned number of bytes, padding");
938 size = CEIL(size, 4);
941 /* see if there's already a matching working area */
944 if ((c->free) && (c->size == size))
952 /* if not, allocate a new one */
955 working_area_t **p = &target->working_areas;
956 uint32_t first_free = target->working_area;
957 uint32_t free_size = target->working_area_size;
959 LOG_DEBUG("allocating new working area");
961 c = target->working_areas;
964 first_free += c->size;
965 free_size -= c->size;
970 if (free_size < size)
972 LOG_WARNING("not enough working area available(requested %d, free %d)", size, free_size);
973 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
976 new_wa = malloc(sizeof(working_area_t));
979 new_wa->address = first_free;
981 if (target->backup_working_area)
984 new_wa->backup = malloc(new_wa->size);
985 if((retval = target_read_memory(target, new_wa->address, 4, new_wa->size / 4, new_wa->backup)) != ERROR_OK)
987 free(new_wa->backup);
994 new_wa->backup = NULL;
997 /* put new entry in list */
1001 /* mark as used, and return the new (reused) area */
1006 new_wa->user = area;
1011 int target_free_working_area_restore(struct target_s *target, working_area_t *area, int restore)
1016 if (restore&&target->backup_working_area)
1019 if((retval = target_write_memory(target, area->address, 4, area->size / 4, area->backup)) != ERROR_OK)
1025 /* mark user pointer invalid */
1032 int target_free_working_area(struct target_s *target, working_area_t *area)
1034 return target_free_working_area_restore(target, area, 1);
1037 /* free resources and restore memory, if restoring memory fails,
1038 * free up resources anyway
1040 void target_free_all_working_areas_restore(struct target_s *target, int restore)
1042 working_area_t *c = target->working_areas;
1046 working_area_t *next = c->next;
1047 target_free_working_area_restore(target, c, restore);
1057 target->working_areas = NULL;
1060 void target_free_all_working_areas(struct target_s *target)
1062 target_free_all_working_areas_restore(target, 1);
1065 int target_register_commands(struct command_context_s *cmd_ctx)
1068 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)");
1073 register_jim(cmd_ctx, "target", jim_target, "configure target" );
1078 int target_arch_state(struct target_s *target)
1083 LOG_USER("No target has been configured");
1087 LOG_USER("target state: %s",
1088 Jim_Nvp_value2name_simple(nvp_target_state,target->state)->name);
1090 if (target->state!=TARGET_HALTED)
1093 retval=target->type->arch_state(target);
1097 /* Single aligned words are guaranteed to use 16 or 32 bit access
1098 * mode respectively, otherwise data is handled as quickly as
1101 int target_write_buffer(struct target_s *target, uint32_t address, uint32_t size, uint8_t *buffer)
1104 LOG_DEBUG("writing buffer of %i byte at 0x%8.8x", size, address);
1106 if (!target_was_examined(target))
1108 LOG_ERROR("Target not examined yet");
1116 if ((address + size - 1) < address)
1118 /* GDB can request this when e.g. PC is 0xfffffffc*/
1119 LOG_ERROR("address+size wrapped(0x%08x, 0x%08x)", address, size);
1123 if (((address % 2) == 0) && (size == 2))
1125 return target_write_memory(target, address, 2, 1, buffer);
1128 /* handle unaligned head bytes */
1131 uint32_t unaligned = 4 - (address % 4);
1133 if (unaligned > size)
1136 if ((retval = target_write_memory(target, address, 1, unaligned, buffer)) != ERROR_OK)
1139 buffer += unaligned;
1140 address += unaligned;
1144 /* handle aligned words */
1147 int aligned = size - (size % 4);
1149 /* use bulk writes above a certain limit. This may have to be changed */
1152 if ((retval = target->type->bulk_write_memory(target, address, aligned / 4, buffer)) != ERROR_OK)
1157 if ((retval = target_write_memory(target, address, 4, aligned / 4, buffer)) != ERROR_OK)
1166 /* handle tail writes of less than 4 bytes */
1169 if ((retval = target_write_memory(target, address, 1, size, buffer)) != ERROR_OK)
1176 /* Single aligned words are guaranteed to use 16 or 32 bit access
1177 * mode respectively, otherwise data is handled as quickly as
1180 int target_read_buffer(struct target_s *target, uint32_t address, uint32_t size, uint8_t *buffer)
1183 LOG_DEBUG("reading buffer of %i byte at 0x%8.8x", size, address);
1185 if (!target_was_examined(target))
1187 LOG_ERROR("Target not examined yet");
1195 if ((address + size - 1) < address)
1197 /* GDB can request this when e.g. PC is 0xfffffffc*/
1198 LOG_ERROR("address+size wrapped(0x%08x, 0x%08x)", address, size);
1202 if (((address % 2) == 0) && (size == 2))
1204 return target_read_memory(target, address, 2, 1, buffer);
1207 /* handle unaligned head bytes */
1210 uint32_t unaligned = 4 - (address % 4);
1212 if (unaligned > size)
1215 if ((retval = target_read_memory(target, address, 1, unaligned, buffer)) != ERROR_OK)
1218 buffer += unaligned;
1219 address += unaligned;
1223 /* handle aligned words */
1226 int aligned = size - (size % 4);
1228 if ((retval = target_read_memory(target, address, 4, aligned / 4, buffer)) != ERROR_OK)
1236 /* handle tail writes of less than 4 bytes */
1239 if ((retval = target_read_memory(target, address, 1, size, buffer)) != ERROR_OK)
1246 int target_checksum_memory(struct target_s *target, uint32_t address, uint32_t size, uint32_t* crc)
1251 uint32_t checksum = 0;
1252 if (!target_was_examined(target))
1254 LOG_ERROR("Target not examined yet");
1258 if ((retval = target->type->checksum_memory(target, address,
1259 size, &checksum)) != ERROR_OK)
1261 buffer = malloc(size);
1264 LOG_ERROR("error allocating buffer for section (%d bytes)", size);
1265 return ERROR_INVALID_ARGUMENTS;
1267 retval = target_read_buffer(target, address, size, buffer);
1268 if (retval != ERROR_OK)
1274 /* convert to target endianess */
1275 for (i = 0; i < (size/sizeof(uint32_t)); i++)
1277 uint32_t target_data;
1278 target_data = target_buffer_get_u32(target, &buffer[i*sizeof(uint32_t)]);
1279 target_buffer_set_u32(target, &buffer[i*sizeof(uint32_t)], target_data);
1282 retval = image_calculate_checksum( buffer, size, &checksum );
1291 int target_blank_check_memory(struct target_s *target, uint32_t address, uint32_t size, uint32_t* blank)
1294 if (!target_was_examined(target))
1296 LOG_ERROR("Target not examined yet");
1300 if (target->type->blank_check_memory == 0)
1301 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
1303 retval = target->type->blank_check_memory(target, address, size, blank);
1308 int target_read_u32(struct target_s *target, uint32_t address, uint32_t *value)
1310 uint8_t value_buf[4];
1311 if (!target_was_examined(target))
1313 LOG_ERROR("Target not examined yet");
1317 int retval = target_read_memory(target, address, 4, 1, value_buf);
1319 if (retval == ERROR_OK)
1321 *value = target_buffer_get_u32(target, value_buf);
1322 LOG_DEBUG("address: 0x%8.8x, value: 0x%8.8x", address, *value);
1327 LOG_DEBUG("address: 0x%8.8x failed", address);
1333 int target_read_u16(struct target_s *target, uint32_t address, uint16_t *value)
1335 uint8_t value_buf[2];
1336 if (!target_was_examined(target))
1338 LOG_ERROR("Target not examined yet");
1342 int retval = target_read_memory(target, address, 2, 1, value_buf);
1344 if (retval == ERROR_OK)
1346 *value = target_buffer_get_u16(target, value_buf);
1347 LOG_DEBUG("address: 0x%8.8x, value: 0x%4.4x", address, *value);
1352 LOG_DEBUG("address: 0x%8.8x failed", address);
1358 int target_read_u8(struct target_s *target, uint32_t address, uint8_t *value)
1360 int retval = target_read_memory(target, address, 1, 1, value);
1361 if (!target_was_examined(target))
1363 LOG_ERROR("Target not examined yet");
1367 if (retval == ERROR_OK)
1369 LOG_DEBUG("address: 0x%8.8x, value: 0x%2.2x", address, *value);
1374 LOG_DEBUG("address: 0x%8.8x failed", address);
1380 int target_write_u32(struct target_s *target, uint32_t address, uint32_t value)
1383 uint8_t value_buf[4];
1384 if (!target_was_examined(target))
1386 LOG_ERROR("Target not examined yet");
1390 LOG_DEBUG("address: 0x%8.8x, value: 0x%8.8x", address, value);
1392 target_buffer_set_u32(target, value_buf, value);
1393 if ((retval = target_write_memory(target, address, 4, 1, value_buf)) != ERROR_OK)
1395 LOG_DEBUG("failed: %i", retval);
1401 int target_write_u16(struct target_s *target, uint32_t address, uint16_t value)
1404 uint8_t value_buf[2];
1405 if (!target_was_examined(target))
1407 LOG_ERROR("Target not examined yet");
1411 LOG_DEBUG("address: 0x%8.8x, value: 0x%8.8x", address, value);
1413 target_buffer_set_u16(target, value_buf, value);
1414 if ((retval = target_write_memory(target, address, 2, 1, value_buf)) != ERROR_OK)
1416 LOG_DEBUG("failed: %i", retval);
1422 int target_write_u8(struct target_s *target, uint32_t address, uint8_t value)
1425 if (!target_was_examined(target))
1427 LOG_ERROR("Target not examined yet");
1431 LOG_DEBUG("address: 0x%8.8x, value: 0x%2.2x", address, value);
1433 if ((retval = target_write_memory(target, address, 1, 1, &value)) != ERROR_OK)
1435 LOG_DEBUG("failed: %i", retval);
1441 int target_register_user_commands(struct command_context_s *cmd_ctx)
1443 int retval = ERROR_OK;
1446 /* script procedures */
1447 register_command(cmd_ctx, NULL, "profile", handle_profile_command, COMMAND_EXEC, "profiling samples the CPU PC");
1448 register_jim(cmd_ctx, "ocd_mem2array", jim_mem2array, "read memory and return as a TCL array for script processing <ARRAYNAME> <WIDTH=32/16/8> <ADDRESS> <COUNT>");
1449 register_jim(cmd_ctx, "ocd_array2mem", jim_array2mem, "convert a TCL array to memory locations and write the values <ARRAYNAME> <WIDTH=32/16/8> <ADDRESS> <COUNT>");
1451 register_command(cmd_ctx, NULL, "fast_load_image", handle_fast_load_image_command, COMMAND_ANY,
1452 "same args as load_image, image stored in memory - mainly for profiling purposes");
1454 register_command(cmd_ctx, NULL, "fast_load", handle_fast_load_command, COMMAND_ANY,
1455 "loads active fast load image to current target - mainly for profiling purposes");
1458 register_command(cmd_ctx, NULL, "virt2phys", handle_virt2phys_command, COMMAND_ANY, "translate a virtual address into a physical address");
1459 register_command(cmd_ctx, NULL, "reg", handle_reg_command, COMMAND_EXEC, "display or set a register");
1460 register_command(cmd_ctx, NULL, "poll", handle_poll_command, COMMAND_EXEC, "poll target state");
1461 register_command(cmd_ctx, NULL, "wait_halt", handle_wait_halt_command, COMMAND_EXEC, "wait for target halt [time (s)]");
1462 register_command(cmd_ctx, NULL, "halt", handle_halt_command, COMMAND_EXEC, "halt target");
1463 register_command(cmd_ctx, NULL, "resume", handle_resume_command, COMMAND_EXEC, "resume target [addr]");
1464 register_command(cmd_ctx, NULL, "step", handle_step_command, COMMAND_EXEC, "step one instruction from current PC or [addr]");
1465 register_command(cmd_ctx, NULL, "reset", handle_reset_command, COMMAND_EXEC, "reset target [run|halt|init] - default is run");
1466 register_command(cmd_ctx, NULL, "soft_reset_halt", handle_soft_reset_halt_command, COMMAND_EXEC, "halt the target and do a soft reset");
1468 register_command(cmd_ctx, NULL, "mdw", handle_md_command, COMMAND_EXEC, "display memory words <addr> [count]");
1469 register_command(cmd_ctx, NULL, "mdh", handle_md_command, COMMAND_EXEC, "display memory half-words <addr> [count]");
1470 register_command(cmd_ctx, NULL, "mdb", handle_md_command, COMMAND_EXEC, "display memory bytes <addr> [count]");
1472 register_command(cmd_ctx, NULL, "mww", handle_mw_command, COMMAND_EXEC, "write memory word <addr> <value> [count]");
1473 register_command(cmd_ctx, NULL, "mwh", handle_mw_command, COMMAND_EXEC, "write memory half-word <addr> <value> [count]");
1474 register_command(cmd_ctx, NULL, "mwb", handle_mw_command, COMMAND_EXEC, "write memory byte <addr> <value> [count]");
1476 register_command(cmd_ctx, NULL, "bp", handle_bp_command, COMMAND_EXEC, "set breakpoint <address> <length> [hw]");
1477 register_command(cmd_ctx, NULL, "rbp", handle_rbp_command, COMMAND_EXEC, "remove breakpoint <adress>");
1478 register_command(cmd_ctx, NULL, "wp", handle_wp_command, COMMAND_EXEC, "set watchpoint <address> <length> <r/w/a> [value] [mask]");
1479 register_command(cmd_ctx, NULL, "rwp", handle_rwp_command, COMMAND_EXEC, "remove watchpoint <adress>");
1481 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]");
1482 register_command(cmd_ctx, NULL, "dump_image", handle_dump_image_command, COMMAND_EXEC, "dump_image <file> <address> <size>");
1483 register_command(cmd_ctx, NULL, "verify_image", handle_verify_image_command, COMMAND_EXEC, "verify_image <file> [offset] [type]");
1484 register_command(cmd_ctx, NULL, "test_image", handle_test_image_command, COMMAND_EXEC, "test_image <file> [offset] [type]");
1486 if((retval = target_request_register_commands(cmd_ctx)) != ERROR_OK)
1488 if((retval = trace_register_commands(cmd_ctx)) != ERROR_OK)
1494 static int handle_targets_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1496 target_t *target = all_targets;
1500 target = get_target(args[0]);
1501 if (target == NULL) {
1502 command_print(cmd_ctx,"Target: %s is unknown, try one of:\n", args[0] );
1505 if (!target->tap->enabled) {
1506 command_print(cmd_ctx,"Target: TAP %s is disabled, "
1507 "can't be the current target\n",
1508 target->tap->dotted_name);
1512 cmd_ctx->current_target = target->target_number;
1517 target = all_targets;
1518 command_print(cmd_ctx, " TargetName Type Endian TapName State ");
1519 command_print(cmd_ctx, "-- ------------------ ---------- ------ ------------------ ------------");
1525 if (target->tap->enabled)
1526 state = Jim_Nvp_value2name_simple(nvp_target_state,
1527 target->state)->name;
1529 state = "tap-disabled";
1531 if (cmd_ctx->current_target == target->target_number)
1534 /* keep columns lined up to match the headers above */
1535 command_print(cmd_ctx, "%2d%c %-18s %-10s %-6s %-18s %s",
1536 target->target_number,
1539 target_get_name(target),
1540 Jim_Nvp_value2name_simple(nvp_target_endian,
1541 target->endianness)->name,
1542 target->tap->dotted_name,
1544 target = target->next;
1550 /* every 300ms we check for reset & powerdropout and issue a "reset halt" if so. */
1552 static int powerDropout;
1553 static int srstAsserted;
1555 static int runPowerRestore;
1556 static int runPowerDropout;
1557 static int runSrstAsserted;
1558 static int runSrstDeasserted;
1560 static int sense_handler(void)
1562 static int prevSrstAsserted = 0;
1563 static int prevPowerdropout = 0;
1566 if ((retval=jtag_power_dropout(&powerDropout))!=ERROR_OK)
1570 powerRestored = prevPowerdropout && !powerDropout;
1573 runPowerRestore = 1;
1576 long long current = timeval_ms();
1577 static long long lastPower = 0;
1578 int waitMore = lastPower + 2000 > current;
1579 if (powerDropout && !waitMore)
1581 runPowerDropout = 1;
1582 lastPower = current;
1585 if ((retval=jtag_srst_asserted(&srstAsserted))!=ERROR_OK)
1589 srstDeasserted = prevSrstAsserted && !srstAsserted;
1591 static long long lastSrst = 0;
1592 waitMore = lastSrst + 2000 > current;
1593 if (srstDeasserted && !waitMore)
1595 runSrstDeasserted = 1;
1599 if (!prevSrstAsserted && srstAsserted)
1601 runSrstAsserted = 1;
1604 prevSrstAsserted = srstAsserted;
1605 prevPowerdropout = powerDropout;
1607 if (srstDeasserted || powerRestored)
1609 /* Other than logging the event we can't do anything here.
1610 * Issuing a reset is a particularly bad idea as we might
1611 * be inside a reset already.
1618 /* process target state changes */
1619 int handle_target(void *priv)
1621 int retval = ERROR_OK;
1623 /* we do not want to recurse here... */
1624 static int recursive = 0;
1629 /* danger! running these procedures can trigger srst assertions and power dropouts.
1630 * We need to avoid an infinite loop/recursion here and we do that by
1631 * clearing the flags after running these events.
1633 int did_something = 0;
1634 if (runSrstAsserted)
1636 Jim_Eval( interp, "srst_asserted");
1639 if (runSrstDeasserted)
1641 Jim_Eval( interp, "srst_deasserted");
1644 if (runPowerDropout)
1646 Jim_Eval( interp, "power_dropout");
1649 if (runPowerRestore)
1651 Jim_Eval( interp, "power_restore");
1657 /* clear detect flags */
1661 /* clear action flags */
1664 runSrstDeasserted=0;
1671 /* Poll targets for state changes unless that's globally disabled.
1672 * Skip targets that are currently disabled.
1674 for (target_t *target = all_targets;
1675 target_continuous_poll && target;
1676 target = target->next)
1678 if (!target->tap->enabled)
1681 /* only poll target if we've got power and srst isn't asserted */
1682 if (!powerDropout && !srstAsserted)
1684 /* polling may fail silently until the target has been examined */
1685 if((retval = target_poll(target)) != ERROR_OK)
1693 static int handle_reg_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1702 target = get_current_target(cmd_ctx);
1704 /* list all available registers for the current target */
1707 reg_cache_t *cache = target->reg_cache;
1713 for (i = 0; i < cache->num_regs; i++)
1715 value = buf_to_str(cache->reg_list[i].value, cache->reg_list[i].size, 16);
1716 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);
1719 cache = cache->next;
1725 /* access a single register by its ordinal number */
1726 if ((args[0][0] >= '0') && (args[0][0] <= '9'))
1729 int retval = parse_uint(args[0], &num);
1730 if (ERROR_OK != retval)
1731 return ERROR_COMMAND_SYNTAX_ERROR;
1733 reg_cache_t *cache = target->reg_cache;
1738 for (i = 0; i < cache->num_regs; i++)
1740 if (count++ == (int)num)
1742 reg = &cache->reg_list[i];
1748 cache = cache->next;
1753 command_print(cmd_ctx, "%i is out of bounds, the current target has only %i registers (0 - %i)", num, count, count - 1);
1756 } else /* access a single register by its name */
1758 reg = register_get_by_name(target->reg_cache, args[0], 1);
1762 command_print(cmd_ctx, "register %s not found in current target", args[0]);
1767 /* display a register */
1768 if ((argc == 1) || ((argc == 2) && !((args[1][0] >= '0') && (args[1][0] <= '9'))))
1770 if ((argc == 2) && (strcmp(args[1], "force") == 0))
1773 if (reg->valid == 0)
1775 reg_arch_type_t *arch_type = register_get_arch_type(reg->arch_type);
1776 arch_type->get(reg);
1778 value = buf_to_str(reg->value, reg->size, 16);
1779 command_print(cmd_ctx, "%s (/%i): 0x%s", reg->name, reg->size, value);
1784 /* set register value */
1787 uint8_t *buf = malloc(CEIL(reg->size, 8));
1788 str_to_buf(args[1], strlen(args[1]), buf, reg->size, 0);
1790 reg_arch_type_t *arch_type = register_get_arch_type(reg->arch_type);
1791 arch_type->set(reg, buf);
1793 value = buf_to_str(reg->value, reg->size, 16);
1794 command_print(cmd_ctx, "%s (/%i): 0x%s", reg->name, reg->size, value);
1802 command_print(cmd_ctx, "usage: reg <#|name> [value]");
1807 static int handle_poll_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1809 int retval = ERROR_OK;
1810 target_t *target = get_current_target(cmd_ctx);
1814 command_print(cmd_ctx, "background polling: %s",
1815 target_continuous_poll ? "on" : "off");
1816 command_print(cmd_ctx, "TAP: %s (%s)",
1817 target->tap->dotted_name,
1818 target->tap->enabled ? "enabled" : "disabled");
1819 if (!target->tap->enabled)
1821 if ((retval = target_poll(target)) != ERROR_OK)
1823 if ((retval = target_arch_state(target)) != ERROR_OK)
1829 if (strcmp(args[0], "on") == 0)
1831 target_continuous_poll = 1;
1833 else if (strcmp(args[0], "off") == 0)
1835 target_continuous_poll = 0;
1839 command_print(cmd_ctx, "arg is \"on\" or \"off\"");
1843 return ERROR_COMMAND_SYNTAX_ERROR;
1849 static int handle_wait_halt_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1852 return ERROR_COMMAND_SYNTAX_ERROR;
1857 int retval = parse_uint(args[0], &ms);
1858 if (ERROR_OK != retval)
1860 command_print(cmd_ctx, "usage: %s [seconds]", cmd);
1861 return ERROR_COMMAND_SYNTAX_ERROR;
1863 // convert seconds (given) to milliseconds (needed)
1867 target_t *target = get_current_target(cmd_ctx);
1868 return target_wait_state(target, TARGET_HALTED, ms);
1871 /* wait for target state to change. The trick here is to have a low
1872 * latency for short waits and not to suck up all the CPU time
1875 * After 500ms, keep_alive() is invoked
1877 int target_wait_state(target_t *target, enum target_state state, int ms)
1880 long long then=0, cur;
1885 if ((retval=target_poll(target))!=ERROR_OK)
1887 if (target->state == state)
1895 then = timeval_ms();
1896 LOG_DEBUG("waiting for target %s...",
1897 Jim_Nvp_value2name_simple(nvp_target_state,state)->name);
1907 LOG_ERROR("timed out while waiting for target %s",
1908 Jim_Nvp_value2name_simple(nvp_target_state,state)->name);
1916 static int handle_halt_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1920 target_t *target = get_current_target(cmd_ctx);
1921 int retval = target_halt(target);
1922 if (ERROR_OK != retval)
1928 retval = parse_uint(args[0], &wait);
1929 if (ERROR_OK != retval)
1930 return ERROR_COMMAND_SYNTAX_ERROR;
1935 return handle_wait_halt_command(cmd_ctx, cmd, args, argc);
1938 static int handle_soft_reset_halt_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1940 target_t *target = get_current_target(cmd_ctx);
1942 LOG_USER("requesting target halt and executing a soft reset");
1944 target->type->soft_reset_halt(target);
1949 static int handle_reset_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1952 return ERROR_COMMAND_SYNTAX_ERROR;
1954 enum target_reset_mode reset_mode = RESET_RUN;
1958 n = Jim_Nvp_name2value_simple( nvp_reset_modes, args[0] );
1959 if( (n->name == NULL) || (n->value == RESET_UNKNOWN) ){
1960 return ERROR_COMMAND_SYNTAX_ERROR;
1962 reset_mode = n->value;
1965 /* reset *all* targets */
1966 return target_process_reset(cmd_ctx, reset_mode);
1970 static int handle_resume_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1973 return ERROR_COMMAND_SYNTAX_ERROR;
1975 target_t *target = get_current_target(cmd_ctx);
1976 target_handle_event(target, TARGET_EVENT_OLD_pre_resume);
1978 /* with no args, resume from current pc, addr = 0,
1979 * with one arguments, addr = args[0],
1980 * handle breakpoints, not debugging */
1984 int retval = parse_u32(args[0], &addr);
1985 if (ERROR_OK != retval)
1989 return target_resume(target, 0, addr, 1, 0);
1992 static int handle_step_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1995 return ERROR_COMMAND_SYNTAX_ERROR;
1999 /* with no args, step from current pc, addr = 0,
2000 * with one argument addr = args[0],
2001 * handle breakpoints, debugging */
2005 int retval = parse_u32(args[0], &addr);
2006 if (ERROR_OK != retval)
2010 target_t *target = get_current_target(cmd_ctx);
2011 return target->type->step(target, 0, addr, 1);
2014 static void handle_md_output(struct command_context_s *cmd_ctx,
2015 struct target_s *target, uint32_t address, unsigned size,
2016 unsigned count, const uint8_t *buffer)
2018 const unsigned line_bytecnt = 32;
2019 unsigned line_modulo = line_bytecnt / size;
2021 char output[line_bytecnt * 4 + 1];
2022 unsigned output_len = 0;
2024 const char *value_fmt;
2026 case 4: value_fmt = "%8.8x "; break;
2027 case 2: value_fmt = "%4.2x "; break;
2028 case 1: value_fmt = "%2.2x "; break;
2030 LOG_ERROR("invalid memory read size: %u", size);
2034 for (unsigned i = 0; i < count; i++)
2036 if (i % line_modulo == 0)
2038 output_len += snprintf(output + output_len,
2039 sizeof(output) - output_len,
2040 "0x%8.8x: ", address + (i*size));
2044 const uint8_t *value_ptr = buffer + i * size;
2046 case 4: value = target_buffer_get_u32(target, value_ptr); break;
2047 case 2: value = target_buffer_get_u16(target, value_ptr); break;
2048 case 1: value = *value_ptr;
2050 output_len += snprintf(output + output_len,
2051 sizeof(output) - output_len,
2054 if ((i % line_modulo == line_modulo - 1) || (i == count - 1))
2056 command_print(cmd_ctx, "%s", output);
2062 static int handle_md_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2065 return ERROR_COMMAND_SYNTAX_ERROR;
2069 case 'w': size = 4; break;
2070 case 'h': size = 2; break;
2071 case 'b': size = 1; break;
2072 default: return ERROR_COMMAND_SYNTAX_ERROR;
2076 int retval = parse_u32(args[0], &address);
2077 if (ERROR_OK != retval)
2083 retval = parse_uint(args[1], &count);
2084 if (ERROR_OK != retval)
2088 uint8_t *buffer = calloc(count, size);
2090 target_t *target = get_current_target(cmd_ctx);
2091 retval = target_read_memory(target,
2092 address, size, count, buffer);
2093 if (ERROR_OK == retval)
2094 handle_md_output(cmd_ctx, target, address, size, count, buffer);
2101 static int handle_mw_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2103 if ((argc < 2) || (argc > 3))
2104 return ERROR_COMMAND_SYNTAX_ERROR;
2107 int retval = parse_u32(args[0], &address);
2108 if (ERROR_OK != retval)
2112 retval = parse_u32(args[1], &value);
2113 if (ERROR_OK != retval)
2119 retval = parse_uint(args[2], &count);
2120 if (ERROR_OK != retval)
2124 target_t *target = get_current_target(cmd_ctx);
2126 uint8_t value_buf[4];
2131 target_buffer_set_u32(target, value_buf, value);
2135 target_buffer_set_u16(target, value_buf, value);
2139 value_buf[0] = value;
2142 return ERROR_COMMAND_SYNTAX_ERROR;
2144 for (unsigned i = 0; i < count; i++)
2146 retval = target_write_memory(target,
2147 address + i * wordsize, wordsize, 1, value_buf);
2148 if (ERROR_OK != retval)
2157 static int parse_load_image_command_args(char **args, int argc,
2158 image_t *image, uint32_t *min_address, uint32_t *max_address)
2160 if (argc < 1 || argc > 5)
2161 return ERROR_COMMAND_SYNTAX_ERROR;
2163 /* a base address isn't always necessary,
2164 * default to 0x0 (i.e. don't relocate) */
2168 int retval = parse_u32(args[1], &addr);
2169 if (ERROR_OK != retval)
2170 return ERROR_COMMAND_SYNTAX_ERROR;
2171 image->base_address = addr;
2172 image->base_address_set = 1;
2175 image->base_address_set = 0;
2177 image->start_address_set = 0;
2181 int retval = parse_u32(args[3], min_address);
2182 if (ERROR_OK != retval)
2183 return ERROR_COMMAND_SYNTAX_ERROR;
2187 int retval = parse_u32(args[4], max_address);
2188 if (ERROR_OK != retval)
2189 return ERROR_COMMAND_SYNTAX_ERROR;
2190 // use size (given) to find max (required)
2191 *max_address += *min_address;
2194 if (*min_address > *max_address)
2195 return ERROR_COMMAND_SYNTAX_ERROR;
2200 static int handle_load_image_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2204 uint32_t image_size;
2205 uint32_t min_address = 0;
2206 uint32_t max_address = 0xffffffff;
2212 duration_t duration;
2213 char *duration_text;
2215 int retval = parse_load_image_command_args(args, argc,
2216 &image, &min_address, &max_address);
2217 if (ERROR_OK != retval)
2220 target_t *target = get_current_target(cmd_ctx);
2221 duration_start_measure(&duration);
2223 if (image_open(&image, args[0], (argc >= 3) ? args[2] : NULL) != ERROR_OK)
2230 for (i = 0; i < image.num_sections; i++)
2232 buffer = malloc(image.sections[i].size);
2235 command_print(cmd_ctx, "error allocating buffer for section (%d bytes)", image.sections[i].size);
2239 if ((retval = image_read_section(&image, i, 0x0, image.sections[i].size, buffer, &buf_cnt)) != ERROR_OK)
2246 uint32_t length=buf_cnt;
2248 /* DANGER!!! beware of unsigned comparision here!!! */
2250 if ((image.sections[i].base_address+buf_cnt>=min_address)&&
2251 (image.sections[i].base_address<max_address))
2253 if (image.sections[i].base_address<min_address)
2255 /* clip addresses below */
2256 offset+=min_address-image.sections[i].base_address;
2260 if (image.sections[i].base_address+buf_cnt>max_address)
2262 length-=(image.sections[i].base_address+buf_cnt)-max_address;
2265 if ((retval = target_write_buffer(target, image.sections[i].base_address+offset, length, buffer+offset)) != ERROR_OK)
2270 image_size += length;
2271 command_print(cmd_ctx, "%u byte written at address 0x%8.8x", length, image.sections[i].base_address+offset);
2277 if((retvaltemp = duration_stop_measure(&duration, &duration_text)) != ERROR_OK)
2279 image_close(&image);
2283 if (retval==ERROR_OK)
2285 command_print(cmd_ctx, "downloaded %u byte in %s", image_size, duration_text);
2287 free(duration_text);
2289 image_close(&image);
2295 static int handle_dump_image_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2299 uint8_t buffer[560];
2302 duration_t duration;
2303 char *duration_text;
2305 target_t *target = get_current_target(cmd_ctx);
2309 command_print(cmd_ctx, "usage: dump_image <filename> <address> <size>");
2314 int retval = parse_u32(args[1], &address);
2315 if (ERROR_OK != retval)
2319 retval = parse_u32(args[2], &size);
2320 if (ERROR_OK != retval)
2323 if (fileio_open(&fileio, args[0], FILEIO_WRITE, FILEIO_BINARY) != ERROR_OK)
2328 duration_start_measure(&duration);
2332 uint32_t size_written;
2333 uint32_t this_run_size = (size > 560) ? 560 : size;
2335 retval = target_read_buffer(target, address, this_run_size, buffer);
2336 if (retval != ERROR_OK)
2341 retval = fileio_write(&fileio, this_run_size, buffer, &size_written);
2342 if (retval != ERROR_OK)
2347 size -= this_run_size;
2348 address += this_run_size;
2351 if((retvaltemp = fileio_close(&fileio)) != ERROR_OK)
2354 if((retvaltemp = duration_stop_measure(&duration, &duration_text)) != ERROR_OK)
2357 if (retval==ERROR_OK)
2359 command_print(cmd_ctx, "dumped %lld byte in %s",
2360 fileio.size, duration_text);
2361 free(duration_text);
2367 static int handle_verify_image_command_internal(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc, int verify)
2371 uint32_t image_size;
2373 int retval, retvaltemp;
2374 uint32_t checksum = 0;
2375 uint32_t mem_checksum = 0;
2379 duration_t duration;
2380 char *duration_text;
2382 target_t *target = get_current_target(cmd_ctx);
2386 return ERROR_COMMAND_SYNTAX_ERROR;
2391 LOG_ERROR("no target selected");
2395 duration_start_measure(&duration);
2400 retval = parse_u32(args[1], &addr);
2401 if (ERROR_OK != retval)
2402 return ERROR_COMMAND_SYNTAX_ERROR;
2403 image.base_address = addr;
2404 image.base_address_set = 1;
2408 image.base_address_set = 0;
2409 image.base_address = 0x0;
2412 image.start_address_set = 0;
2414 if ((retval=image_open(&image, args[0], (argc == 3) ? args[2] : NULL)) != ERROR_OK)
2421 for (i = 0; i < image.num_sections; i++)
2423 buffer = malloc(image.sections[i].size);
2426 command_print(cmd_ctx, "error allocating buffer for section (%d bytes)", image.sections[i].size);
2429 if ((retval = image_read_section(&image, i, 0x0, image.sections[i].size, buffer, &buf_cnt)) != ERROR_OK)
2437 /* calculate checksum of image */
2438 image_calculate_checksum( buffer, buf_cnt, &checksum );
2440 retval = target_checksum_memory(target, image.sections[i].base_address, buf_cnt, &mem_checksum);
2441 if( retval != ERROR_OK )
2447 if( checksum != mem_checksum )
2449 /* failed crc checksum, fall back to a binary compare */
2452 command_print(cmd_ctx, "checksum mismatch - attempting binary compare");
2454 data = (uint8_t*)malloc(buf_cnt);
2456 /* Can we use 32bit word accesses? */
2458 int count = buf_cnt;
2459 if ((count % 4) == 0)
2464 retval = target_read_memory(target, image.sections[i].base_address, size, count, data);
2465 if (retval == ERROR_OK)
2468 for (t = 0; t < buf_cnt; t++)
2470 if (data[t] != buffer[t])
2472 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]);
2489 command_print(cmd_ctx, "address 0x%08x length 0x%08x", image.sections[i].base_address, buf_cnt);
2493 image_size += buf_cnt;
2497 if((retvaltemp = duration_stop_measure(&duration, &duration_text)) != ERROR_OK)
2499 image_close(&image);
2503 if (retval==ERROR_OK)
2505 command_print(cmd_ctx, "verified %u bytes in %s", image_size, duration_text);
2507 free(duration_text);
2509 image_close(&image);
2514 static int handle_verify_image_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2516 return handle_verify_image_command_internal(cmd_ctx, cmd, args, argc, 1);
2519 static int handle_test_image_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2521 return handle_verify_image_command_internal(cmd_ctx, cmd, args, argc, 0);
2524 static int handle_bp_command_list(struct command_context_s *cmd_ctx)
2526 target_t *target = get_current_target(cmd_ctx);
2527 breakpoint_t *breakpoint = target->breakpoints;
2530 if (breakpoint->type == BKPT_SOFT)
2532 char* buf = buf_to_str(breakpoint->orig_instr,
2533 breakpoint->length, 16);
2534 command_print(cmd_ctx, "0x%8.8x, 0x%x, %i, 0x%s",
2535 breakpoint->address, breakpoint->length,
2536 breakpoint->set, buf);
2541 command_print(cmd_ctx, "0x%8.8x, 0x%x, %i",
2542 breakpoint->address, breakpoint->length, breakpoint->set);
2545 breakpoint = breakpoint->next;
2550 static int handle_bp_command_set(struct command_context_s *cmd_ctx,
2551 uint32_t addr, uint32_t length, int hw)
2553 target_t *target = get_current_target(cmd_ctx);
2554 int retval = breakpoint_add(target, addr, length, hw);
2555 if (ERROR_OK == retval)
2556 command_print(cmd_ctx, "breakpoint set at 0x%8.8x", addr);
2558 LOG_ERROR("Failure setting breakpoint");
2562 static int handle_bp_command(struct command_context_s *cmd_ctx,
2563 char *cmd, char **args, int argc)
2566 return handle_bp_command_list(cmd_ctx);
2568 if (argc < 2 || argc > 3)
2570 command_print(cmd_ctx, "usage: bp <address> <length> ['hw']");
2571 return ERROR_COMMAND_SYNTAX_ERROR;
2575 int retval = parse_u32(args[0], &addr);
2576 if (ERROR_OK != retval)
2580 retval = parse_u32(args[1], &length);
2581 if (ERROR_OK != retval)
2587 if (strcmp(args[2], "hw") == 0)
2590 return ERROR_COMMAND_SYNTAX_ERROR;
2593 return handle_bp_command_set(cmd_ctx, addr, length, hw);
2596 static int handle_rbp_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2599 return ERROR_COMMAND_SYNTAX_ERROR;
2602 int retval = parse_u32(args[0], &addr);
2603 if (ERROR_OK != retval)
2606 target_t *target = get_current_target(cmd_ctx);
2607 breakpoint_remove(target, addr);
2612 static int handle_wp_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2614 target_t *target = get_current_target(cmd_ctx);
2618 watchpoint_t *watchpoint = target->watchpoints;
2622 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);
2623 watchpoint = watchpoint->next;
2628 enum watchpoint_rw type = WPT_ACCESS;
2630 uint32_t length = 0;
2631 uint32_t data_value = 0x0;
2632 uint32_t data_mask = 0xffffffff;
2638 retval = parse_u32(args[4], &data_mask);
2639 if (ERROR_OK != retval)
2643 retval = parse_u32(args[3], &data_value);
2644 if (ERROR_OK != retval)
2660 LOG_ERROR("invalid watchpoint mode ('%c')", args[2][0]);
2661 return ERROR_COMMAND_SYNTAX_ERROR;
2665 retval = parse_u32(args[1], &length);
2666 if (ERROR_OK != retval)
2668 retval = parse_u32(args[0], &addr);
2669 if (ERROR_OK != retval)
2674 command_print(cmd_ctx, "usage: wp <address> <length> [r/w/a] [value] [mask]");
2675 return ERROR_COMMAND_SYNTAX_ERROR;
2678 retval = watchpoint_add(target, addr, length, type,
2679 data_value, data_mask);
2680 if (ERROR_OK != retval)
2681 LOG_ERROR("Failure setting watchpoints");
2686 static int handle_rwp_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2689 return ERROR_COMMAND_SYNTAX_ERROR;
2692 int retval = parse_u32(args[0], &addr);
2693 if (ERROR_OK != retval)
2696 target_t *target = get_current_target(cmd_ctx);
2697 watchpoint_remove(target, addr);
2704 * Translate a virtual address to a physical address.
2706 * The low-level target implementation must have logged a detailed error
2707 * which is forwarded to telnet/GDB session.
2709 static int handle_virt2phys_command(command_context_t *cmd_ctx,
2710 char *cmd, char **args, int argc)
2713 return ERROR_COMMAND_SYNTAX_ERROR;
2716 int retval = parse_u32(args[0], &va);
2717 if (ERROR_OK != retval)
2721 target_t *target = get_current_target(cmd_ctx);
2722 retval = target->type->virt2phys(target, va, &pa);
2723 if (retval == ERROR_OK)
2724 command_print(cmd_ctx, "Physical address 0x%08x", pa);
2729 static void writeData(FILE *f, const void *data, size_t len)
2731 size_t written = fwrite(data, 1, len, f);
2733 LOG_ERROR("failed to write %zu bytes: %s", len, strerror(errno));
2736 static void writeLong(FILE *f, int l)
2741 char c=(l>>(i*8))&0xff;
2742 writeData(f, &c, 1);
2747 static void writeString(FILE *f, char *s)
2749 writeData(f, s, strlen(s));
2752 /* Dump a gmon.out histogram file. */
2753 static void writeGmon(uint32_t *samples, uint32_t sampleNum, char *filename)
2756 FILE *f=fopen(filename, "w");
2759 writeString(f, "gmon");
2760 writeLong(f, 0x00000001); /* Version */
2761 writeLong(f, 0); /* padding */
2762 writeLong(f, 0); /* padding */
2763 writeLong(f, 0); /* padding */
2765 uint8_t zero = 0; /* GMON_TAG_TIME_HIST */
2766 writeData(f, &zero, 1);
2768 /* figure out bucket size */
2769 uint32_t min=samples[0];
2770 uint32_t max=samples[0];
2771 for (i=0; i<sampleNum; i++)
2783 int addressSpace=(max-min+1);
2785 static const uint32_t maxBuckets = 256 * 1024; /* maximum buckets. */
2786 uint32_t length = addressSpace;
2787 if (length > maxBuckets)
2791 int *buckets=malloc(sizeof(int)*length);
2797 memset(buckets, 0, sizeof(int)*length);
2798 for (i=0; i<sampleNum;i++)
2800 uint32_t address=samples[i];
2801 long long a=address-min;
2802 long long b=length-1;
2803 long long c=addressSpace-1;
2804 int index=(a*b)/c; /* danger!!!! int32 overflows */
2808 /* append binary memory gmon.out &profile_hist_hdr ((char*)&profile_hist_hdr + sizeof(struct gmon_hist_hdr)) */
2809 writeLong(f, min); /* low_pc */
2810 writeLong(f, max); /* high_pc */
2811 writeLong(f, length); /* # of samples */
2812 writeLong(f, 64000000); /* 64MHz */
2813 writeString(f, "seconds");
2814 for (i=0; i<(15-strlen("seconds")); i++)
2815 writeData(f, &zero, 1);
2816 writeString(f, "s");
2818 /*append binary memory gmon.out profile_hist_data (profile_hist_data + profile_hist_hdr.hist_size) */
2820 char *data=malloc(2*length);
2823 for (i=0; i<length;i++)
2832 data[i*2+1]=(val>>8)&0xff;
2835 writeData(f, data, length * 2);
2845 /* profiling samples the CPU PC as quickly as OpenOCD is able, which will be used as a random sampling of PC */
2846 static int handle_profile_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2848 target_t *target = get_current_target(cmd_ctx);
2849 struct timeval timeout, now;
2851 gettimeofday(&timeout, NULL);
2854 return ERROR_COMMAND_SYNTAX_ERROR;
2857 int retval = parse_uint(args[0], &offset);
2858 if (ERROR_OK != retval)
2861 timeval_add_time(&timeout, offset, 0);
2863 command_print(cmd_ctx, "Starting profiling. Halting and resuming the target as often as we can...");
2865 static const int maxSample=10000;
2866 uint32_t *samples=malloc(sizeof(uint32_t)*maxSample);
2871 /* hopefully it is safe to cache! We want to stop/restart as quickly as possible. */
2872 reg_t *reg = register_get_by_name(target->reg_cache, "pc", 1);
2876 target_poll(target);
2877 if (target->state == TARGET_HALTED)
2879 uint32_t t=*((uint32_t *)reg->value);
2880 samples[numSamples++]=t;
2881 retval = target_resume(target, 1, 0, 0, 0); /* current pc, addr = 0, do not handle breakpoints, not debugging */
2882 target_poll(target);
2883 alive_sleep(10); /* sleep 10ms, i.e. <100 samples/second. */
2884 } else if (target->state == TARGET_RUNNING)
2886 /* We want to quickly sample the PC. */
2887 if((retval = target_halt(target)) != ERROR_OK)
2894 command_print(cmd_ctx, "Target not halted or running");
2898 if (retval!=ERROR_OK)
2903 gettimeofday(&now, NULL);
2904 if ((numSamples>=maxSample) || ((now.tv_sec >= timeout.tv_sec) && (now.tv_usec >= timeout.tv_usec)))
2906 command_print(cmd_ctx, "Profiling completed. %d samples.", numSamples);
2907 if((retval = target_poll(target)) != ERROR_OK)
2912 if (target->state == TARGET_HALTED)
2914 target_resume(target, 1, 0, 0, 0); /* current pc, addr = 0, do not handle breakpoints, not debugging */
2916 if((retval = target_poll(target)) != ERROR_OK)
2921 writeGmon(samples, numSamples, args[1]);
2922 command_print(cmd_ctx, "Wrote %s", args[1]);
2931 static int new_int_array_element(Jim_Interp * interp, const char *varname, int idx, uint32_t val)
2934 Jim_Obj *nameObjPtr, *valObjPtr;
2937 namebuf = alloc_printf("%s(%d)", varname, idx);
2941 nameObjPtr = Jim_NewStringObj(interp, namebuf, -1);
2942 valObjPtr = Jim_NewIntObj(interp, val);
2943 if (!nameObjPtr || !valObjPtr)
2949 Jim_IncrRefCount(nameObjPtr);
2950 Jim_IncrRefCount(valObjPtr);
2951 result = Jim_SetVariable(interp, nameObjPtr, valObjPtr);
2952 Jim_DecrRefCount(interp, nameObjPtr);
2953 Jim_DecrRefCount(interp, valObjPtr);
2955 /* printf("%s(%d) <= 0%08x\n", varname, idx, val); */
2959 static int jim_mem2array(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
2961 command_context_t *context;
2964 context = Jim_GetAssocData(interp, "context");
2965 if (context == NULL)
2967 LOG_ERROR("mem2array: no command context");
2970 target = get_current_target(context);
2973 LOG_ERROR("mem2array: no current target");
2977 return target_mem2array(interp, target, argc-1, argv+1);
2980 static int target_mem2array(Jim_Interp *interp, target_t *target, int argc, Jim_Obj *const *argv)
2988 const char *varname;
2989 uint8_t buffer[4096];
2993 /* argv[1] = name of array to receive the data
2994 * argv[2] = desired width
2995 * argv[3] = memory address
2996 * argv[4] = count of times to read
2999 Jim_WrongNumArgs(interp, 1, argv, "varname width addr nelems");
3002 varname = Jim_GetString(argv[0], &len);
3003 /* given "foo" get space for worse case "foo(%d)" .. add 20 */
3005 e = Jim_GetLong(interp, argv[1], &l);
3011 e = Jim_GetLong(interp, argv[2], &l);
3016 e = Jim_GetLong(interp, argv[3], &l);
3032 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3033 Jim_AppendStrings( interp, Jim_GetResult(interp), "Invalid width param, must be 8/16/32", NULL );
3037 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3038 Jim_AppendStrings(interp, Jim_GetResult(interp), "mem2array: zero width read?", NULL);
3041 if ((addr + (len * width)) < addr) {
3042 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3043 Jim_AppendStrings(interp, Jim_GetResult(interp), "mem2array: addr + len - wraps to zero?", NULL);
3046 /* absurd transfer size? */
3048 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3049 Jim_AppendStrings(interp, Jim_GetResult(interp), "mem2array: absurd > 64K item request", NULL);
3054 ((width == 2) && ((addr & 1) == 0)) ||
3055 ((width == 4) && ((addr & 3) == 0))) {
3059 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3060 sprintf(buf, "mem2array address: 0x%08x is not aligned for %d byte reads", addr, width);
3061 Jim_AppendStrings(interp, Jim_GetResult(interp), buf , NULL);
3072 /* Slurp... in buffer size chunks */
3074 count = len; /* in objects.. */
3075 if (count > (sizeof(buffer)/width)) {
3076 count = (sizeof(buffer)/width);
3079 retval = target_read_memory( target, addr, width, count, buffer );
3080 if (retval != ERROR_OK) {
3082 LOG_ERROR("mem2array: Read @ 0x%08x, w=%d, cnt=%d, failed", addr, width, count);
3083 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3084 Jim_AppendStrings(interp, Jim_GetResult(interp), "mem2array: cannot read memory", NULL);
3088 v = 0; /* shut up gcc */
3089 for (i = 0 ;i < count ;i++, n++) {
3092 v = target_buffer_get_u32(target, &buffer[i*width]);
3095 v = target_buffer_get_u16(target, &buffer[i*width]);
3098 v = buffer[i] & 0x0ff;
3101 new_int_array_element(interp, varname, n, v);
3107 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3112 static int get_int_array_element(Jim_Interp * interp, const char *varname, int idx, uint32_t *val)
3115 Jim_Obj *nameObjPtr, *valObjPtr;
3119 namebuf = alloc_printf("%s(%d)", varname, idx);
3123 nameObjPtr = Jim_NewStringObj(interp, namebuf, -1);
3130 Jim_IncrRefCount(nameObjPtr);
3131 valObjPtr = Jim_GetVariable(interp, nameObjPtr, JIM_ERRMSG);
3132 Jim_DecrRefCount(interp, nameObjPtr);
3134 if (valObjPtr == NULL)
3137 result = Jim_GetLong(interp, valObjPtr, &l);
3138 /* printf("%s(%d) => 0%08x\n", varname, idx, val); */
3143 static int jim_array2mem(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
3145 command_context_t *context;
3148 context = Jim_GetAssocData(interp, "context");
3149 if (context == NULL){
3150 LOG_ERROR("array2mem: no command context");
3153 target = get_current_target(context);
3154 if (target == NULL){
3155 LOG_ERROR("array2mem: no current target");
3159 return target_array2mem( interp,target, argc-1, argv+1 );
3162 static int target_array2mem(Jim_Interp *interp, target_t *target, int argc, Jim_Obj *const *argv)
3170 const char *varname;
3171 uint8_t buffer[4096];
3175 /* argv[1] = name of array to get the data
3176 * argv[2] = desired width
3177 * argv[3] = memory address
3178 * argv[4] = count to write
3181 Jim_WrongNumArgs(interp, 1, argv, "varname width addr nelems");
3184 varname = Jim_GetString(argv[0], &len);
3185 /* given "foo" get space for worse case "foo(%d)" .. add 20 */
3187 e = Jim_GetLong(interp, argv[1], &l);
3193 e = Jim_GetLong(interp, argv[2], &l);
3198 e = Jim_GetLong(interp, argv[3], &l);
3214 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3215 Jim_AppendStrings( interp, Jim_GetResult(interp), "Invalid width param, must be 8/16/32", NULL );
3219 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3220 Jim_AppendStrings(interp, Jim_GetResult(interp), "array2mem: zero width read?", NULL);
3223 if ((addr + (len * width)) < addr) {
3224 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3225 Jim_AppendStrings(interp, Jim_GetResult(interp), "array2mem: addr + len - wraps to zero?", NULL);
3228 /* absurd transfer size? */
3230 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3231 Jim_AppendStrings(interp, Jim_GetResult(interp), "array2mem: absurd > 64K item request", NULL);
3236 ((width == 2) && ((addr & 1) == 0)) ||
3237 ((width == 4) && ((addr & 3) == 0))) {
3241 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3242 sprintf(buf, "array2mem address: 0x%08x is not aligned for %d byte reads", addr, width);
3243 Jim_AppendStrings(interp, Jim_GetResult(interp), buf , NULL);
3254 /* Slurp... in buffer size chunks */
3256 count = len; /* in objects.. */
3257 if (count > (sizeof(buffer)/width)) {
3258 count = (sizeof(buffer)/width);
3261 v = 0; /* shut up gcc */
3262 for (i = 0 ;i < count ;i++, n++) {
3263 get_int_array_element(interp, varname, n, &v);
3266 target_buffer_set_u32(target, &buffer[i*width], v);
3269 target_buffer_set_u16(target, &buffer[i*width], v);
3272 buffer[i] = v & 0x0ff;
3278 retval = target_write_memory(target, addr, width, count, buffer);
3279 if (retval != ERROR_OK) {
3281 LOG_ERROR("array2mem: Write @ 0x%08x, w=%d, cnt=%d, failed", addr, width, count);
3282 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3283 Jim_AppendStrings(interp, Jim_GetResult(interp), "array2mem: cannot read memory", NULL);
3289 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3294 void target_all_handle_event( enum target_event e )
3298 LOG_DEBUG( "**all*targets: event: %d, %s",
3300 Jim_Nvp_value2name_simple( nvp_target_event, e )->name );
3302 target = all_targets;
3304 target_handle_event( target, e );
3305 target = target->next;
3309 void target_handle_event( target_t *target, enum target_event e )
3311 target_event_action_t *teap;
3314 teap = target->event_action;
3318 if( teap->event == e ){
3320 LOG_DEBUG( "target: (%d) %s (%s) event: %d (%s) action: %s\n",
3321 target->target_number,
3323 target_get_name(target),
3325 Jim_Nvp_value2name_simple( nvp_target_event, e )->name,
3326 Jim_GetString( teap->body, NULL ) );
3327 if (Jim_EvalObj( interp, teap->body )!=JIM_OK)
3329 Jim_PrintErrorMessage(interp);
3335 LOG_DEBUG( "event: %d %s - no action",
3337 Jim_Nvp_value2name_simple( nvp_target_event, e )->name );
3341 enum target_cfg_param {
3344 TCFG_WORK_AREA_VIRT,
3345 TCFG_WORK_AREA_PHYS,
3346 TCFG_WORK_AREA_SIZE,
3347 TCFG_WORK_AREA_BACKUP,
3350 TCFG_CHAIN_POSITION,
3353 static Jim_Nvp nvp_config_opts[] = {
3354 { .name = "-type", .value = TCFG_TYPE },
3355 { .name = "-event", .value = TCFG_EVENT },
3356 { .name = "-work-area-virt", .value = TCFG_WORK_AREA_VIRT },
3357 { .name = "-work-area-phys", .value = TCFG_WORK_AREA_PHYS },
3358 { .name = "-work-area-size", .value = TCFG_WORK_AREA_SIZE },
3359 { .name = "-work-area-backup", .value = TCFG_WORK_AREA_BACKUP },
3360 { .name = "-endian" , .value = TCFG_ENDIAN },
3361 { .name = "-variant", .value = TCFG_VARIANT },
3362 { .name = "-chain-position", .value = TCFG_CHAIN_POSITION },
3364 { .name = NULL, .value = -1 }
3367 static int target_configure( Jim_GetOptInfo *goi, target_t *target )
3375 /* parse config or cget options ... */
3376 while( goi->argc > 0 ){
3377 Jim_SetEmptyResult( goi->interp );
3378 /* Jim_GetOpt_Debug( goi ); */
3380 if( target->type->target_jim_configure ){
3381 /* target defines a configure function */
3382 /* target gets first dibs on parameters */
3383 e = (*(target->type->target_jim_configure))( target, goi );
3392 /* otherwise we 'continue' below */
3394 e = Jim_GetOpt_Nvp( goi, nvp_config_opts, &n );
3396 Jim_GetOpt_NvpUnknown( goi, nvp_config_opts, 0 );
3402 if( goi->isconfigure ){
3403 Jim_SetResult_sprintf( goi->interp, "not setable: %s", n->name );
3407 if( goi->argc != 0 ){
3408 Jim_WrongNumArgs( goi->interp, goi->argc, goi->argv, "NO PARAMS");
3412 Jim_SetResultString( goi->interp, target_get_name(target), -1 );
3416 if( goi->argc == 0 ){
3417 Jim_WrongNumArgs( goi->interp, goi->argc, goi->argv, "-event ?event-name? ...");
3421 e = Jim_GetOpt_Nvp( goi, nvp_target_event, &n );
3423 Jim_GetOpt_NvpUnknown( goi, nvp_target_event, 1 );
3427 if( goi->isconfigure ){
3428 if( goi->argc != 1 ){
3429 Jim_WrongNumArgs( goi->interp, goi->argc, goi->argv, "-event ?event-name? ?EVENT-BODY?");
3433 if( goi->argc != 0 ){
3434 Jim_WrongNumArgs(goi->interp, goi->argc, goi->argv, "-event ?event-name?");
3440 target_event_action_t *teap;
3442 teap = target->event_action;
3443 /* replace existing? */
3445 if( teap->event == (enum target_event)n->value ){
3451 if( goi->isconfigure ){
3454 teap = calloc( 1, sizeof(*teap) );
3456 teap->event = n->value;
3457 Jim_GetOpt_Obj( goi, &o );
3459 Jim_DecrRefCount( interp, teap->body );
3461 teap->body = Jim_DuplicateObj( goi->interp, o );
3464 * Tcl/TK - "tk events" have a nice feature.
3465 * See the "BIND" command.
3466 * We should support that here.
3467 * You can specify %X and %Y in the event code.
3468 * The idea is: %T - target name.
3469 * The idea is: %N - target number
3470 * The idea is: %E - event name.
3472 Jim_IncrRefCount( teap->body );
3474 /* add to head of event list */
3475 teap->next = target->event_action;
3476 target->event_action = teap;
3477 Jim_SetEmptyResult(goi->interp);
3481 Jim_SetEmptyResult( goi->interp );
3483 Jim_SetResult( goi->interp, Jim_DuplicateObj( goi->interp, teap->body ) );
3490 case TCFG_WORK_AREA_VIRT:
3491 if( goi->isconfigure ){
3492 target_free_all_working_areas(target);
3493 e = Jim_GetOpt_Wide( goi, &w );
3497 target->working_area_virt = w;
3499 if( goi->argc != 0 ){
3503 Jim_SetResult( interp, Jim_NewIntObj( goi->interp, target->working_area_virt ) );
3507 case TCFG_WORK_AREA_PHYS:
3508 if( goi->isconfigure ){
3509 target_free_all_working_areas(target);
3510 e = Jim_GetOpt_Wide( goi, &w );
3514 target->working_area_phys = w;
3516 if( goi->argc != 0 ){
3520 Jim_SetResult( interp, Jim_NewIntObj( goi->interp, target->working_area_phys ) );
3524 case TCFG_WORK_AREA_SIZE:
3525 if( goi->isconfigure ){
3526 target_free_all_working_areas(target);
3527 e = Jim_GetOpt_Wide( goi, &w );
3531 target->working_area_size = w;
3533 if( goi->argc != 0 ){
3537 Jim_SetResult( interp, Jim_NewIntObj( goi->interp, target->working_area_size ) );
3541 case TCFG_WORK_AREA_BACKUP:
3542 if( goi->isconfigure ){
3543 target_free_all_working_areas(target);
3544 e = Jim_GetOpt_Wide( goi, &w );
3548 /* make this exactly 1 or 0 */
3549 target->backup_working_area = (!!w);
3551 if( goi->argc != 0 ){
3555 Jim_SetResult(interp, Jim_NewIntObj(goi->interp, target->backup_working_area));
3556 /* loop for more e*/
3560 if( goi->isconfigure ){
3561 e = Jim_GetOpt_Nvp( goi, nvp_target_endian, &n );
3563 Jim_GetOpt_NvpUnknown( goi, nvp_target_endian, 1 );
3566 target->endianness = n->value;
3568 if( goi->argc != 0 ){
3572 n = Jim_Nvp_value2name_simple( nvp_target_endian, target->endianness );
3573 if( n->name == NULL ){
3574 target->endianness = TARGET_LITTLE_ENDIAN;
3575 n = Jim_Nvp_value2name_simple( nvp_target_endian, target->endianness );
3577 Jim_SetResultString( goi->interp, n->name, -1 );
3582 if( goi->isconfigure ){
3583 if( goi->argc < 1 ){
3584 Jim_SetResult_sprintf( goi->interp,
3589 if( target->variant ){
3590 free((void *)(target->variant));
3592 e = Jim_GetOpt_String( goi, &cp, NULL );
3593 target->variant = strdup(cp);
3595 if( goi->argc != 0 ){
3599 Jim_SetResultString( goi->interp, target->variant,-1 );
3602 case TCFG_CHAIN_POSITION:
3603 if( goi->isconfigure ){
3606 target_free_all_working_areas(target);
3607 e = Jim_GetOpt_Obj( goi, &o );
3611 tap = jtag_tap_by_jim_obj( goi->interp, o );
3615 /* make this exactly 1 or 0 */
3618 if( goi->argc != 0 ){
3622 Jim_SetResultString( interp, target->tap->dotted_name, -1 );
3623 /* loop for more e*/
3626 } /* while( goi->argc ) */
3629 /* done - we return */
3633 /** this is the 'tcl' handler for the target specific command */
3634 static int tcl_target_func( Jim_Interp *interp, int argc, Jim_Obj *const *argv )
3639 uint8_t target_buf[32];
3642 struct command_context_s *cmd_ctx;
3649 TS_CMD_MWW, TS_CMD_MWH, TS_CMD_MWB,
3650 TS_CMD_MDW, TS_CMD_MDH, TS_CMD_MDB,
3651 TS_CMD_MRW, TS_CMD_MRH, TS_CMD_MRB,
3652 TS_CMD_MEM2ARRAY, TS_CMD_ARRAY2MEM,
3660 TS_CMD_INVOKE_EVENT,
3663 static const Jim_Nvp target_options[] = {
3664 { .name = "configure", .value = TS_CMD_CONFIGURE },
3665 { .name = "cget", .value = TS_CMD_CGET },
3666 { .name = "mww", .value = TS_CMD_MWW },
3667 { .name = "mwh", .value = TS_CMD_MWH },
3668 { .name = "mwb", .value = TS_CMD_MWB },
3669 { .name = "mdw", .value = TS_CMD_MDW },
3670 { .name = "mdh", .value = TS_CMD_MDH },
3671 { .name = "mdb", .value = TS_CMD_MDB },
3672 { .name = "mem2array", .value = TS_CMD_MEM2ARRAY },
3673 { .name = "array2mem", .value = TS_CMD_ARRAY2MEM },
3674 { .name = "eventlist", .value = TS_CMD_EVENTLIST },
3675 { .name = "curstate", .value = TS_CMD_CURSTATE },
3677 { .name = "arp_examine", .value = TS_CMD_EXAMINE },
3678 { .name = "arp_poll", .value = TS_CMD_POLL },
3679 { .name = "arp_reset", .value = TS_CMD_RESET },
3680 { .name = "arp_halt", .value = TS_CMD_HALT },
3681 { .name = "arp_waitstate", .value = TS_CMD_WAITSTATE },
3682 { .name = "invoke-event", .value = TS_CMD_INVOKE_EVENT },
3684 { .name = NULL, .value = -1 },
3687 /* go past the "command" */
3688 Jim_GetOpt_Setup( &goi, interp, argc-1, argv+1 );
3690 target = Jim_CmdPrivData( goi.interp );
3691 cmd_ctx = Jim_GetAssocData(goi.interp, "context");
3693 /* commands here are in an NVP table */
3694 e = Jim_GetOpt_Nvp( &goi, target_options, &n );
3696 Jim_GetOpt_NvpUnknown( &goi, target_options, 0 );
3699 /* Assume blank result */
3700 Jim_SetEmptyResult( goi.interp );
3703 case TS_CMD_CONFIGURE:
3705 Jim_WrongNumArgs( goi.interp, goi.argc, goi.argv, "missing: -option VALUE ...");
3708 goi.isconfigure = 1;
3709 return target_configure( &goi, target );
3711 // some things take params
3713 Jim_WrongNumArgs( goi.interp, 0, goi.argv, "missing: ?-option?");
3716 goi.isconfigure = 0;
3717 return target_configure( &goi, target );
3725 * argv[3] = optional count.
3728 if( (goi.argc == 2) || (goi.argc == 3) ){
3732 Jim_SetResult_sprintf( goi.interp, "expected: %s ADDR DATA [COUNT]", n->name );
3736 e = Jim_GetOpt_Wide( &goi, &a );
3741 e = Jim_GetOpt_Wide( &goi, &b );
3745 if (goi.argc == 3) {
3746 e = Jim_GetOpt_Wide( &goi, &c );
3756 target_buffer_set_u32( target, target_buf, b );
3760 target_buffer_set_u16( target, target_buf, b );
3764 target_buffer_set_u8( target, target_buf, b );
3768 for( x = 0 ; x < c ; x++ ){
3769 e = target_write_memory( target, a, b, 1, target_buf );
3770 if( e != ERROR_OK ){
3771 Jim_SetResult_sprintf( interp, "Error writing @ 0x%08x: %d\n", (int)(a), e );
3784 /* argv[0] = command
3786 * argv[2] = optional count
3788 if( (goi.argc == 2) || (goi.argc == 3) ){
3789 Jim_SetResult_sprintf( goi.interp, "expected: %s ADDR [COUNT]", n->name );
3792 e = Jim_GetOpt_Wide( &goi, &a );
3797 e = Jim_GetOpt_Wide( &goi, &c );
3804 b = 1; /* shut up gcc */
3817 /* convert to "bytes" */
3819 /* count is now in 'BYTES' */
3825 e = target_read_memory( target, a, b, y / b, target_buf );
3826 if( e != ERROR_OK ){
3827 Jim_SetResult_sprintf( interp, "error reading target @ 0x%08lx", (int)(a) );
3831 Jim_fprintf( interp, interp->cookie_stdout, "0x%08x ", (int)(a) );
3834 for( x = 0 ; (x < 16) && (x < y) ; x += 4 ){
3835 z = target_buffer_get_u32( target, &(target_buf[ x * 4 ]) );
3836 Jim_fprintf( interp, interp->cookie_stdout, "%08x ", (int)(z) );
3838 for( ; (x < 16) ; x += 4 ){
3839 Jim_fprintf( interp, interp->cookie_stdout, " " );
3843 for( x = 0 ; (x < 16) && (x < y) ; x += 2 ){
3844 z = target_buffer_get_u16( target, &(target_buf[ x * 2 ]) );
3845 Jim_fprintf( interp, interp->cookie_stdout, "%04x ", (int)(z) );
3847 for( ; (x < 16) ; x += 2 ){
3848 Jim_fprintf( interp, interp->cookie_stdout, " " );
3853 for( x = 0 ; (x < 16) && (x < y) ; x += 1 ){
3854 z = target_buffer_get_u8( target, &(target_buf[ x * 4 ]) );
3855 Jim_fprintf( interp, interp->cookie_stdout, "%02x ", (int)(z) );
3857 for( ; (x < 16) ; x += 1 ){
3858 Jim_fprintf( interp, interp->cookie_stdout, " " );
3862 /* ascii-ify the bytes */
3863 for( x = 0 ; x < y ; x++ ){
3864 if( (target_buf[x] >= 0x20) &&
3865 (target_buf[x] <= 0x7e) ){
3869 target_buf[x] = '.';
3874 target_buf[x] = ' ';
3879 /* print - with a newline */
3880 Jim_fprintf( interp, interp->cookie_stdout, "%s\n", target_buf );
3886 case TS_CMD_MEM2ARRAY:
3887 return target_mem2array( goi.interp, target, goi.argc, goi.argv );
3889 case TS_CMD_ARRAY2MEM:
3890 return target_array2mem( goi.interp, target, goi.argc, goi.argv );
3892 case TS_CMD_EXAMINE:
3894 Jim_WrongNumArgs( goi.interp, 2, argv, "[no parameters]");
3897 if (!target->tap->enabled)
3898 goto err_tap_disabled;
3899 e = target->type->examine( target );
3900 if( e != ERROR_OK ){
3901 Jim_SetResult_sprintf( interp, "examine-fails: %d", e );
3907 Jim_WrongNumArgs( goi.interp, 2, argv, "[no parameters]");
3910 if (!target->tap->enabled)
3911 goto err_tap_disabled;
3912 if( !(target_was_examined(target)) ){
3913 e = ERROR_TARGET_NOT_EXAMINED;
3915 e = target->type->poll( target );
3917 if( e != ERROR_OK ){
3918 Jim_SetResult_sprintf( interp, "poll-fails: %d", e );
3925 if( goi.argc != 2 ){
3926 Jim_WrongNumArgs( interp, 2, argv, "t|f|assert|deassert BOOL");
3929 e = Jim_GetOpt_Nvp( &goi, nvp_assert, &n );
3931 Jim_GetOpt_NvpUnknown( &goi, nvp_assert, 1 );
3934 /* the halt or not param */
3935 e = Jim_GetOpt_Wide( &goi, &a);
3939 if (!target->tap->enabled)
3940 goto err_tap_disabled;
3941 /* determine if we should halt or not. */
3942 target->reset_halt = !!a;
3943 /* When this happens - all workareas are invalid. */
3944 target_free_all_working_areas_restore(target, 0);
3947 if( n->value == NVP_ASSERT ){
3948 target->type->assert_reset( target );
3950 target->type->deassert_reset( target );
3955 Jim_WrongNumArgs( goi.interp, 0, argv, "halt [no parameters]");
3958 if (!target->tap->enabled)
3959 goto err_tap_disabled;
3960 target->type->halt( target );
3962 case TS_CMD_WAITSTATE:
3963 /* params: <name> statename timeoutmsecs */
3964 if( goi.argc != 2 ){
3965 Jim_SetResult_sprintf( goi.interp, "%s STATENAME TIMEOUTMSECS", n->name );
3968 e = Jim_GetOpt_Nvp( &goi, nvp_target_state, &n );
3970 Jim_GetOpt_NvpUnknown( &goi, nvp_target_state,1 );
3973 e = Jim_GetOpt_Wide( &goi, &a );
3977 if (!target->tap->enabled)
3978 goto err_tap_disabled;
3979 e = target_wait_state( target, n->value, a );
3980 if( e != ERROR_OK ){
3981 Jim_SetResult_sprintf( goi.interp,
3982 "target: %s wait %s fails (%d) %s",
3985 e, target_strerror_safe(e) );
3990 case TS_CMD_EVENTLIST:
3991 /* List for human, Events defined for this target.
3992 * scripts/programs should use 'name cget -event NAME'
3995 target_event_action_t *teap;
3996 teap = target->event_action;
3997 command_print( cmd_ctx, "Event actions for target (%d) %s\n",
3998 target->target_number,
4000 command_print( cmd_ctx, "%-25s | Body", "Event");
4001 command_print( cmd_ctx, "------------------------- | ----------------------------------------");
4003 command_print( cmd_ctx,
4005 Jim_Nvp_value2name_simple( nvp_target_event, teap->event )->name,
4006 Jim_GetString( teap->body, NULL ) );
4009 command_print( cmd_ctx, "***END***");
4012 case TS_CMD_CURSTATE:
4013 if( goi.argc != 0 ){
4014 Jim_WrongNumArgs( goi.interp, 0, argv, "[no parameters]");
4017 Jim_SetResultString( goi.interp,
4018 Jim_Nvp_value2name_simple(nvp_target_state,target->state)->name,-1);
4020 case TS_CMD_INVOKE_EVENT:
4021 if( goi.argc != 1 ){
4022 Jim_SetResult_sprintf( goi.interp, "%s ?EVENTNAME?",n->name);
4025 e = Jim_GetOpt_Nvp( &goi, nvp_target_event, &n );
4027 Jim_GetOpt_NvpUnknown( &goi, nvp_target_event, 1 );
4030 target_handle_event( target, n->value );
4036 Jim_SetResult_sprintf(interp, "[TAP is disabled]");
4040 static int target_create( Jim_GetOptInfo *goi )
4049 struct command_context_s *cmd_ctx;
4051 cmd_ctx = Jim_GetAssocData(goi->interp, "context");
4052 if( goi->argc < 3 ){
4053 Jim_WrongNumArgs( goi->interp, 1, goi->argv, "?name? ?type? ..options...");
4058 Jim_GetOpt_Obj( goi, &new_cmd );
4059 /* does this command exist? */
4060 cmd = Jim_GetCommand( goi->interp, new_cmd, JIM_ERRMSG );
4062 cp = Jim_GetString( new_cmd, NULL );
4063 Jim_SetResult_sprintf(goi->interp, "Command/target: %s Exists", cp);
4068 e = Jim_GetOpt_String( goi, &cp2, NULL );
4070 /* now does target type exist */
4071 for( x = 0 ; target_types[x] ; x++ ){
4072 if( 0 == strcmp( cp, target_types[x]->name ) ){
4077 if( target_types[x] == NULL ){
4078 Jim_SetResult_sprintf( goi->interp, "Unknown target type %s, try one of ", cp );
4079 for( x = 0 ; target_types[x] ; x++ ){
4080 if( target_types[x+1] ){
4081 Jim_AppendStrings( goi->interp,
4082 Jim_GetResult(goi->interp),
4083 target_types[x]->name,
4086 Jim_AppendStrings( goi->interp,
4087 Jim_GetResult(goi->interp),
4089 target_types[x]->name,NULL );
4096 target = calloc(1,sizeof(target_t));
4097 /* set target number */
4098 target->target_number = new_target_number();
4100 /* allocate memory for each unique target type */
4101 target->type = (target_type_t*)calloc(1,sizeof(target_type_t));
4103 memcpy( target->type, target_types[x], sizeof(target_type_t));
4105 /* will be set by "-endian" */
4106 target->endianness = TARGET_ENDIAN_UNKNOWN;
4108 target->working_area = 0x0;
4109 target->working_area_size = 0x0;
4110 target->working_areas = NULL;
4111 target->backup_working_area = 0;
4113 target->state = TARGET_UNKNOWN;
4114 target->debug_reason = DBG_REASON_UNDEFINED;
4115 target->reg_cache = NULL;
4116 target->breakpoints = NULL;
4117 target->watchpoints = NULL;
4118 target->next = NULL;
4119 target->arch_info = NULL;
4121 target->display = 1;
4123 /* initialize trace information */
4124 target->trace_info = malloc(sizeof(trace_t));
4125 target->trace_info->num_trace_points = 0;
4126 target->trace_info->trace_points_size = 0;
4127 target->trace_info->trace_points = NULL;
4128 target->trace_info->trace_history_size = 0;
4129 target->trace_info->trace_history = NULL;
4130 target->trace_info->trace_history_pos = 0;
4131 target->trace_info->trace_history_overflowed = 0;
4133 target->dbgmsg = NULL;
4134 target->dbg_msg_enabled = 0;
4136 target->endianness = TARGET_ENDIAN_UNKNOWN;
4138 /* Do the rest as "configure" options */
4139 goi->isconfigure = 1;
4140 e = target_configure( goi, target);
4142 if (target->tap == NULL)
4144 Jim_SetResultString( interp, "-chain-position required when creating target", -1);
4149 free( target->type );
4154 if( target->endianness == TARGET_ENDIAN_UNKNOWN ){
4155 /* default endian to little if not specified */
4156 target->endianness = TARGET_LITTLE_ENDIAN;
4159 /* incase variant is not set */
4160 if (!target->variant)
4161 target->variant = strdup("");
4163 /* create the target specific commands */
4164 if( target->type->register_commands ){
4165 (*(target->type->register_commands))( cmd_ctx );
4167 if( target->type->target_create ){
4168 (*(target->type->target_create))( target, goi->interp );
4171 /* append to end of list */
4174 tpp = &(all_targets);
4176 tpp = &( (*tpp)->next );
4181 cp = Jim_GetString( new_cmd, NULL );
4182 target->cmd_name = strdup(cp);
4184 /* now - create the new target name command */
4185 e = Jim_CreateCommand( goi->interp,
4188 tcl_target_func, /* C function */
4189 target, /* private data */
4190 NULL ); /* no del proc */
4195 static int jim_target( Jim_Interp *interp, int argc, Jim_Obj *const *argv )
4199 struct command_context_s *cmd_ctx;
4203 /* TG = target generic */
4211 const char *target_cmds[] = {
4212 "create", "types", "names", "current", "number",
4214 NULL /* terminate */
4217 LOG_DEBUG("Target command params:");
4218 LOG_DEBUG("%s", Jim_Debug_ArgvString(interp, argc, argv));
4220 cmd_ctx = Jim_GetAssocData( interp, "context" );
4222 Jim_GetOpt_Setup( &goi, interp, argc-1, argv+1 );
4224 if( goi.argc == 0 ){
4225 Jim_WrongNumArgs(interp, 1, argv, "missing: command ...");
4229 /* Jim_GetOpt_Debug( &goi ); */
4230 r = Jim_GetOpt_Enum( &goi, target_cmds, &x );
4237 Jim_Panic(goi.interp,"Why am I here?");
4239 case TG_CMD_CURRENT:
4240 if( goi.argc != 0 ){
4241 Jim_WrongNumArgs( goi.interp, 1, goi.argv, "Too many parameters");
4244 Jim_SetResultString( goi.interp, get_current_target( cmd_ctx )->cmd_name, -1 );
4247 if( goi.argc != 0 ){
4248 Jim_WrongNumArgs( goi.interp, 1, goi.argv, "Too many parameters" );
4251 Jim_SetResult( goi.interp, Jim_NewListObj( goi.interp, NULL, 0 ) );
4252 for( x = 0 ; target_types[x] ; x++ ){
4253 Jim_ListAppendElement( goi.interp,
4254 Jim_GetResult(goi.interp),
4255 Jim_NewStringObj( goi.interp, target_types[x]->name, -1 ) );
4259 if( goi.argc != 0 ){
4260 Jim_WrongNumArgs( goi.interp, 1, goi.argv, "Too many parameters" );
4263 Jim_SetResult( goi.interp, Jim_NewListObj( goi.interp, NULL, 0 ) );
4264 target = all_targets;
4266 Jim_ListAppendElement( goi.interp,
4267 Jim_GetResult(goi.interp),
4268 Jim_NewStringObj( goi.interp, target->cmd_name, -1 ) );
4269 target = target->next;
4274 Jim_WrongNumArgs( goi.interp, goi.argc, goi.argv, "?name ... config options ...");
4277 return target_create( &goi );
4280 if( goi.argc != 1 ){
4281 Jim_SetResult_sprintf( goi.interp, "expected: target number ?NUMBER?");
4284 e = Jim_GetOpt_Wide( &goi, &w );
4290 t = get_target_by_num(w);
4292 Jim_SetResult_sprintf( goi.interp,"Target: number %d does not exist", (int)(w));
4295 Jim_SetResultString( goi.interp, t->cmd_name, -1 );
4299 if( goi.argc != 0 ){
4300 Jim_WrongNumArgs( goi.interp, 0, goi.argv, "<no parameters>");
4303 Jim_SetResult( goi.interp,
4304 Jim_NewIntObj( goi.interp, max_target_number()));
4320 static int fastload_num;
4321 static struct FastLoad *fastload;
4323 static void free_fastload(void)
4328 for (i=0; i<fastload_num; i++)
4330 if (fastload[i].data)
4331 free(fastload[i].data);
4341 static int handle_fast_load_image_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
4345 uint32_t image_size;
4346 uint32_t min_address=0;
4347 uint32_t max_address=0xffffffff;
4352 duration_t duration;
4353 char *duration_text;
4355 int retval = parse_load_image_command_args(args, argc,
4356 &image, &min_address, &max_address);
4357 if (ERROR_OK != retval)
4360 duration_start_measure(&duration);
4362 if (image_open(&image, args[0], (argc >= 3) ? args[2] : NULL) != ERROR_OK)
4369 fastload_num=image.num_sections;
4370 fastload=(struct FastLoad *)malloc(sizeof(struct FastLoad)*image.num_sections);
4373 image_close(&image);
4376 memset(fastload, 0, sizeof(struct FastLoad)*image.num_sections);
4377 for (i = 0; i < image.num_sections; i++)
4379 buffer = malloc(image.sections[i].size);
4382 command_print(cmd_ctx, "error allocating buffer for section (%d bytes)", image.sections[i].size);
4386 if ((retval = image_read_section(&image, i, 0x0, image.sections[i].size, buffer, &buf_cnt)) != ERROR_OK)
4393 uint32_t length=buf_cnt;
4396 /* DANGER!!! beware of unsigned comparision here!!! */
4398 if ((image.sections[i].base_address+buf_cnt>=min_address)&&
4399 (image.sections[i].base_address<max_address))
4401 if (image.sections[i].base_address<min_address)
4403 /* clip addresses below */
4404 offset+=min_address-image.sections[i].base_address;
4408 if (image.sections[i].base_address+buf_cnt>max_address)
4410 length-=(image.sections[i].base_address+buf_cnt)-max_address;
4413 fastload[i].address=image.sections[i].base_address+offset;
4414 fastload[i].data=malloc(length);
4415 if (fastload[i].data==NULL)
4420 memcpy(fastload[i].data, buffer+offset, length);
4421 fastload[i].length=length;
4423 image_size += length;
4424 command_print(cmd_ctx, "%u byte written at address 0x%8.8x", length, image.sections[i].base_address+offset);
4430 duration_stop_measure(&duration, &duration_text);
4431 if (retval==ERROR_OK)
4433 command_print(cmd_ctx, "Loaded %u bytes in %s", image_size, duration_text);
4434 command_print(cmd_ctx, "NB!!! image has not been loaded to target, issue a subsequent 'fast_load' to do so.");
4436 free(duration_text);
4438 image_close(&image);
4440 if (retval!=ERROR_OK)
4448 static int handle_fast_load_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
4451 return ERROR_COMMAND_SYNTAX_ERROR;
4454 LOG_ERROR("No image in memory");
4458 int ms=timeval_ms();
4460 int retval=ERROR_OK;
4461 for (i=0; i<fastload_num;i++)
4463 target_t *target = get_current_target(cmd_ctx);
4464 command_print(cmd_ctx, "Write to 0x%08x, length 0x%08x", fastload[i].address, fastload[i].length);
4465 if (retval==ERROR_OK)
4467 retval = target_write_buffer(target, fastload[i].address, fastload[i].length, fastload[i].data);
4469 size+=fastload[i].length;
4471 int after=timeval_ms();
4472 command_print(cmd_ctx, "Loaded image %f kBytes/s", (float)(size/1024.0)/((float)(after-ms)/1000.0));