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 (0x%08x), padding", ((unsigned)(size)));
938 size = (size + 3) & (~3);
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 %u, free %u)",
973 (unsigned)(size), (unsigned)(free_size));
974 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
977 new_wa = malloc(sizeof(working_area_t));
980 new_wa->address = first_free;
982 if (target->backup_working_area)
985 new_wa->backup = malloc(new_wa->size);
986 if ((retval = target_read_memory(target, new_wa->address, 4, new_wa->size / 4, new_wa->backup)) != ERROR_OK)
988 free(new_wa->backup);
995 new_wa->backup = NULL;
998 /* put new entry in list */
1002 /* mark as used, and return the new (reused) area */
1007 new_wa->user = area;
1012 int target_free_working_area_restore(struct target_s *target, working_area_t *area, int restore)
1017 if (restore && target->backup_working_area)
1020 if ((retval = target_write_memory(target, area->address, 4, area->size / 4, area->backup)) != ERROR_OK)
1026 /* mark user pointer invalid */
1033 int target_free_working_area(struct target_s *target, working_area_t *area)
1035 return target_free_working_area_restore(target, area, 1);
1038 /* free resources and restore memory, if restoring memory fails,
1039 * free up resources anyway
1041 void target_free_all_working_areas_restore(struct target_s *target, int restore)
1043 working_area_t *c = target->working_areas;
1047 working_area_t *next = c->next;
1048 target_free_working_area_restore(target, c, restore);
1058 target->working_areas = NULL;
1061 void target_free_all_working_areas(struct target_s *target)
1063 target_free_all_working_areas_restore(target, 1);
1066 int target_register_commands(struct command_context_s *cmd_ctx)
1069 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)");
1074 register_jim(cmd_ctx, "target", jim_target, "configure target");
1079 int target_arch_state(struct target_s *target)
1084 LOG_USER("No target has been configured");
1088 LOG_USER("target state: %s",
1089 Jim_Nvp_value2name_simple(nvp_target_state,target->state)->name);
1091 if (target->state != TARGET_HALTED)
1094 retval = target->type->arch_state(target);
1098 /* Single aligned words are guaranteed to use 16 or 32 bit access
1099 * mode respectively, otherwise data is handled as quickly as
1102 int target_write_buffer(struct target_s *target, uint32_t address, uint32_t size, uint8_t *buffer)
1105 LOG_DEBUG("writing buffer of %i byte at 0x%8.8x",
1106 (int)size, (unsigned)address);
1108 if (!target_was_examined(target))
1110 LOG_ERROR("Target not examined yet");
1118 if ((address + size - 1) < address)
1120 /* GDB can request this when e.g. PC is 0xfffffffc*/
1121 LOG_ERROR("address + size wrapped(0x%08x, 0x%08x)",
1127 if (((address % 2) == 0) && (size == 2))
1129 return target_write_memory(target, address, 2, 1, buffer);
1132 /* handle unaligned head bytes */
1135 uint32_t unaligned = 4 - (address % 4);
1137 if (unaligned > size)
1140 if ((retval = target_write_memory(target, address, 1, unaligned, buffer)) != ERROR_OK)
1143 buffer += unaligned;
1144 address += unaligned;
1148 /* handle aligned words */
1151 int aligned = size - (size % 4);
1153 /* use bulk writes above a certain limit. This may have to be changed */
1156 if ((retval = target->type->bulk_write_memory(target, address, aligned / 4, buffer)) != ERROR_OK)
1161 if ((retval = target_write_memory(target, address, 4, aligned / 4, buffer)) != ERROR_OK)
1170 /* handle tail writes of less than 4 bytes */
1173 if ((retval = target_write_memory(target, address, 1, size, buffer)) != ERROR_OK)
1180 /* Single aligned words are guaranteed to use 16 or 32 bit access
1181 * mode respectively, otherwise data is handled as quickly as
1184 int target_read_buffer(struct target_s *target, uint32_t address, uint32_t size, uint8_t *buffer)
1187 LOG_DEBUG("reading buffer of %i byte at 0x%8.8x",
1188 (int)size, (unsigned)address);
1190 if (!target_was_examined(target))
1192 LOG_ERROR("Target not examined yet");
1200 if ((address + size - 1) < address)
1202 /* GDB can request this when e.g. PC is 0xfffffffc*/
1203 LOG_ERROR("address + size wrapped(0x%08" PRIx32 ", 0x%08" PRIx32 ")",
1209 if (((address % 2) == 0) && (size == 2))
1211 return target_read_memory(target, address, 2, 1, buffer);
1214 /* handle unaligned head bytes */
1217 uint32_t unaligned = 4 - (address % 4);
1219 if (unaligned > size)
1222 if ((retval = target_read_memory(target, address, 1, unaligned, buffer)) != ERROR_OK)
1225 buffer += unaligned;
1226 address += unaligned;
1230 /* handle aligned words */
1233 int aligned = size - (size % 4);
1235 if ((retval = target_read_memory(target, address, 4, aligned / 4, buffer)) != ERROR_OK)
1243 /* handle tail writes of less than 4 bytes */
1246 if ((retval = target_read_memory(target, address, 1, size, buffer)) != ERROR_OK)
1253 int target_checksum_memory(struct target_s *target, uint32_t address, uint32_t size, uint32_t* crc)
1258 uint32_t checksum = 0;
1259 if (!target_was_examined(target))
1261 LOG_ERROR("Target not examined yet");
1265 if ((retval = target->type->checksum_memory(target, address,
1266 size, &checksum)) != ERROR_OK)
1268 buffer = malloc(size);
1271 LOG_ERROR("error allocating buffer for section (%d bytes)", (int)size);
1272 return ERROR_INVALID_ARGUMENTS;
1274 retval = target_read_buffer(target, address, size, buffer);
1275 if (retval != ERROR_OK)
1281 /* convert to target endianess */
1282 for (i = 0; i < (size/sizeof(uint32_t)); i++)
1284 uint32_t target_data;
1285 target_data = target_buffer_get_u32(target, &buffer[i*sizeof(uint32_t)]);
1286 target_buffer_set_u32(target, &buffer[i*sizeof(uint32_t)], target_data);
1289 retval = image_calculate_checksum(buffer, size, &checksum);
1298 int target_blank_check_memory(struct target_s *target, uint32_t address, uint32_t size, uint32_t* blank)
1301 if (!target_was_examined(target))
1303 LOG_ERROR("Target not examined yet");
1307 if (target->type->blank_check_memory == 0)
1308 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
1310 retval = target->type->blank_check_memory(target, address, size, blank);
1315 int target_read_u32(struct target_s *target, uint32_t address, uint32_t *value)
1317 uint8_t value_buf[4];
1318 if (!target_was_examined(target))
1320 LOG_ERROR("Target not examined yet");
1324 int retval = target_read_memory(target, address, 4, 1, value_buf);
1326 if (retval == ERROR_OK)
1328 *value = target_buffer_get_u32(target, value_buf);
1329 LOG_DEBUG("address: 0x%8.8" PRIx32 ", value: 0x%8.8" PRIx32 "",
1336 LOG_DEBUG("address: 0x%8.8" PRIx32 " failed",
1343 int target_read_u16(struct target_s *target, uint32_t address, uint16_t *value)
1345 uint8_t value_buf[2];
1346 if (!target_was_examined(target))
1348 LOG_ERROR("Target not examined yet");
1352 int retval = target_read_memory(target, address, 2, 1, value_buf);
1354 if (retval == ERROR_OK)
1356 *value = target_buffer_get_u16(target, value_buf);
1357 LOG_DEBUG("address: 0x%8.8" PRIx32 ", value: 0x%4.4x",
1364 LOG_DEBUG("address: 0x%8.8" PRIx32 " failed",
1371 int target_read_u8(struct target_s *target, uint32_t address, uint8_t *value)
1373 int retval = target_read_memory(target, address, 1, 1, value);
1374 if (!target_was_examined(target))
1376 LOG_ERROR("Target not examined yet");
1380 if (retval == ERROR_OK)
1382 LOG_DEBUG("address: 0x%8.8" PRIx32 ", value: 0x%2.2x",
1389 LOG_DEBUG("address: 0x%8.8" PRIx32 " failed",
1396 int target_write_u32(struct target_s *target, uint32_t address, uint32_t value)
1399 uint8_t value_buf[4];
1400 if (!target_was_examined(target))
1402 LOG_ERROR("Target not examined yet");
1406 LOG_DEBUG("address: 0x%8.8" PRIx32 ", value: 0x%8.8" PRIx32 "",
1410 target_buffer_set_u32(target, value_buf, value);
1411 if ((retval = target_write_memory(target, address, 4, 1, value_buf)) != ERROR_OK)
1413 LOG_DEBUG("failed: %i", retval);
1419 int target_write_u16(struct target_s *target, uint32_t address, uint16_t value)
1422 uint8_t value_buf[2];
1423 if (!target_was_examined(target))
1425 LOG_ERROR("Target not examined yet");
1429 LOG_DEBUG("address: 0x%8.8" PRIx32 ", value: 0x%8.8x",
1433 target_buffer_set_u16(target, value_buf, value);
1434 if ((retval = target_write_memory(target, address, 2, 1, value_buf)) != ERROR_OK)
1436 LOG_DEBUG("failed: %i", retval);
1442 int target_write_u8(struct target_s *target, uint32_t address, uint8_t value)
1445 if (!target_was_examined(target))
1447 LOG_ERROR("Target not examined yet");
1451 LOG_DEBUG("address: 0x%8.8" PRIx32 ", value: 0x%2.2x",
1454 if ((retval = target_write_memory(target, address, 1, 1, &value)) != ERROR_OK)
1456 LOG_DEBUG("failed: %i", retval);
1462 int target_register_user_commands(struct command_context_s *cmd_ctx)
1464 int retval = ERROR_OK;
1467 /* script procedures */
1468 register_command(cmd_ctx, NULL, "profile", handle_profile_command, COMMAND_EXEC, "profiling samples the CPU PC");
1469 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>");
1470 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>");
1472 register_command(cmd_ctx, NULL, "fast_load_image", handle_fast_load_image_command, COMMAND_ANY,
1473 "same args as load_image, image stored in memory - mainly for profiling purposes");
1475 register_command(cmd_ctx, NULL, "fast_load", handle_fast_load_command, COMMAND_ANY,
1476 "loads active fast load image to current target - mainly for profiling purposes");
1479 register_command(cmd_ctx, NULL, "virt2phys", handle_virt2phys_command, COMMAND_ANY, "translate a virtual address into a physical address");
1480 register_command(cmd_ctx, NULL, "reg", handle_reg_command, COMMAND_EXEC, "display or set a register");
1481 register_command(cmd_ctx, NULL, "poll", handle_poll_command, COMMAND_EXEC, "poll target state");
1482 register_command(cmd_ctx, NULL, "wait_halt", handle_wait_halt_command, COMMAND_EXEC, "wait for target halt [time (s)]");
1483 register_command(cmd_ctx, NULL, "halt", handle_halt_command, COMMAND_EXEC, "halt target");
1484 register_command(cmd_ctx, NULL, "resume", handle_resume_command, COMMAND_EXEC, "resume target [addr]");
1485 register_command(cmd_ctx, NULL, "step", handle_step_command, COMMAND_EXEC, "step one instruction from current PC or [addr]");
1486 register_command(cmd_ctx, NULL, "reset", handle_reset_command, COMMAND_EXEC, "reset target [run | halt | init] - default is run");
1487 register_command(cmd_ctx, NULL, "soft_reset_halt", handle_soft_reset_halt_command, COMMAND_EXEC, "halt the target and do a soft reset");
1489 register_command(cmd_ctx, NULL, "mdw", handle_md_command, COMMAND_EXEC, "display memory words <addr> [count]");
1490 register_command(cmd_ctx, NULL, "mdh", handle_md_command, COMMAND_EXEC, "display memory half-words <addr> [count]");
1491 register_command(cmd_ctx, NULL, "mdb", handle_md_command, COMMAND_EXEC, "display memory bytes <addr> [count]");
1493 register_command(cmd_ctx, NULL, "mww", handle_mw_command, COMMAND_EXEC, "write memory word <addr> <value> [count]");
1494 register_command(cmd_ctx, NULL, "mwh", handle_mw_command, COMMAND_EXEC, "write memory half-word <addr> <value> [count]");
1495 register_command(cmd_ctx, NULL, "mwb", handle_mw_command, COMMAND_EXEC, "write memory byte <addr> <value> [count]");
1497 register_command(cmd_ctx, NULL, "bp", handle_bp_command, COMMAND_EXEC, "set breakpoint <address> <length> [hw]");
1498 register_command(cmd_ctx, NULL, "rbp", handle_rbp_command, COMMAND_EXEC, "remove breakpoint <adress>");
1499 register_command(cmd_ctx, NULL, "wp", handle_wp_command, COMMAND_EXEC, "set watchpoint <address> <length> <r/w/a> [value] [mask]");
1500 register_command(cmd_ctx, NULL, "rwp", handle_rwp_command, COMMAND_EXEC, "remove watchpoint <adress>");
1502 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]");
1503 register_command(cmd_ctx, NULL, "dump_image", handle_dump_image_command, COMMAND_EXEC, "dump_image <file> <address> <size>");
1504 register_command(cmd_ctx, NULL, "verify_image", handle_verify_image_command, COMMAND_EXEC, "verify_image <file> [offset] [type]");
1505 register_command(cmd_ctx, NULL, "test_image", handle_test_image_command, COMMAND_EXEC, "test_image <file> [offset] [type]");
1507 if ((retval = target_request_register_commands(cmd_ctx)) != ERROR_OK)
1509 if ((retval = trace_register_commands(cmd_ctx)) != ERROR_OK)
1515 static int handle_targets_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1517 target_t *target = all_targets;
1521 target = get_target(args[0]);
1522 if (target == NULL) {
1523 command_print(cmd_ctx,"Target: %s is unknown, try one of:\n", args[0]);
1526 if (!target->tap->enabled) {
1527 command_print(cmd_ctx,"Target: TAP %s is disabled, "
1528 "can't be the current target\n",
1529 target->tap->dotted_name);
1533 cmd_ctx->current_target = target->target_number;
1538 target = all_targets;
1539 command_print(cmd_ctx, " TargetName Type Endian TapName State ");
1540 command_print(cmd_ctx, "-- ------------------ ---------- ------ ------------------ ------------");
1546 if (target->tap->enabled)
1547 state = Jim_Nvp_value2name_simple(nvp_target_state,
1548 target->state)->name;
1550 state = "tap-disabled";
1552 if (cmd_ctx->current_target == target->target_number)
1555 /* keep columns lined up to match the headers above */
1556 command_print(cmd_ctx, "%2d%c %-18s %-10s %-6s %-18s %s",
1557 target->target_number,
1560 target_get_name(target),
1561 Jim_Nvp_value2name_simple(nvp_target_endian,
1562 target->endianness)->name,
1563 target->tap->dotted_name,
1565 target = target->next;
1571 /* every 300ms we check for reset & powerdropout and issue a "reset halt" if so. */
1573 static int powerDropout;
1574 static int srstAsserted;
1576 static int runPowerRestore;
1577 static int runPowerDropout;
1578 static int runSrstAsserted;
1579 static int runSrstDeasserted;
1581 static int sense_handler(void)
1583 static int prevSrstAsserted = 0;
1584 static int prevPowerdropout = 0;
1587 if ((retval = jtag_power_dropout(&powerDropout)) != ERROR_OK)
1591 powerRestored = prevPowerdropout && !powerDropout;
1594 runPowerRestore = 1;
1597 long long current = timeval_ms();
1598 static long long lastPower = 0;
1599 int waitMore = lastPower + 2000 > current;
1600 if (powerDropout && !waitMore)
1602 runPowerDropout = 1;
1603 lastPower = current;
1606 if ((retval = jtag_srst_asserted(&srstAsserted)) != ERROR_OK)
1610 srstDeasserted = prevSrstAsserted && !srstAsserted;
1612 static long long lastSrst = 0;
1613 waitMore = lastSrst + 2000 > current;
1614 if (srstDeasserted && !waitMore)
1616 runSrstDeasserted = 1;
1620 if (!prevSrstAsserted && srstAsserted)
1622 runSrstAsserted = 1;
1625 prevSrstAsserted = srstAsserted;
1626 prevPowerdropout = powerDropout;
1628 if (srstDeasserted || powerRestored)
1630 /* Other than logging the event we can't do anything here.
1631 * Issuing a reset is a particularly bad idea as we might
1632 * be inside a reset already.
1639 /* process target state changes */
1640 int handle_target(void *priv)
1642 int retval = ERROR_OK;
1644 /* we do not want to recurse here... */
1645 static int recursive = 0;
1650 /* danger! running these procedures can trigger srst assertions and power dropouts.
1651 * We need to avoid an infinite loop/recursion here and we do that by
1652 * clearing the flags after running these events.
1654 int did_something = 0;
1655 if (runSrstAsserted)
1657 Jim_Eval(interp, "srst_asserted");
1660 if (runSrstDeasserted)
1662 Jim_Eval(interp, "srst_deasserted");
1665 if (runPowerDropout)
1667 Jim_Eval(interp, "power_dropout");
1670 if (runPowerRestore)
1672 Jim_Eval(interp, "power_restore");
1678 /* clear detect flags */
1682 /* clear action flags */
1684 runSrstAsserted = 0;
1685 runSrstDeasserted = 0;
1686 runPowerRestore = 0;
1687 runPowerDropout = 0;
1692 /* Poll targets for state changes unless that's globally disabled.
1693 * Skip targets that are currently disabled.
1695 for (target_t *target = all_targets;
1696 target_continuous_poll && target;
1697 target = target->next)
1699 if (!target->tap->enabled)
1702 /* only poll target if we've got power and srst isn't asserted */
1703 if (!powerDropout && !srstAsserted)
1705 /* polling may fail silently until the target has been examined */
1706 if ((retval = target_poll(target)) != ERROR_OK)
1714 static int handle_reg_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1723 target = get_current_target(cmd_ctx);
1725 /* list all available registers for the current target */
1728 reg_cache_t *cache = target->reg_cache;
1734 for (i = 0; i < cache->num_regs; i++)
1736 value = buf_to_str(cache->reg_list[i].value, cache->reg_list[i].size, 16);
1737 command_print(cmd_ctx, "(%i) %s (/%i): 0x%s (dirty: %i, valid: %i)",
1739 cache->reg_list[i].name,
1740 (int)(cache->reg_list[i].size),
1742 cache->reg_list[i].dirty,
1743 cache->reg_list[i].valid);
1746 cache = cache->next;
1752 /* access a single register by its ordinal number */
1753 if ((args[0][0] >= '0') && (args[0][0] <= '9'))
1756 int retval = parse_uint(args[0], &num);
1757 if (ERROR_OK != retval)
1758 return ERROR_COMMAND_SYNTAX_ERROR;
1760 reg_cache_t *cache = target->reg_cache;
1765 for (i = 0; i < cache->num_regs; i++)
1767 if (count++ == (int)num)
1769 reg = &cache->reg_list[i];
1775 cache = cache->next;
1780 command_print(cmd_ctx, "%i is out of bounds, the current target has only %i registers (0 - %i)", num, count, count - 1);
1783 } else /* access a single register by its name */
1785 reg = register_get_by_name(target->reg_cache, args[0], 1);
1789 command_print(cmd_ctx, "register %s not found in current target", args[0]);
1794 /* display a register */
1795 if ((argc == 1) || ((argc == 2) && !((args[1][0] >= '0') && (args[1][0] <= '9'))))
1797 if ((argc == 2) && (strcmp(args[1], "force") == 0))
1800 if (reg->valid == 0)
1802 reg_arch_type_t *arch_type = register_get_arch_type(reg->arch_type);
1803 arch_type->get(reg);
1805 value = buf_to_str(reg->value, reg->size, 16);
1806 command_print(cmd_ctx, "%s (/%i): 0x%s", reg->name, (int)(reg->size), value);
1811 /* set register value */
1814 uint8_t *buf = malloc(CEIL(reg->size, 8));
1815 str_to_buf(args[1], strlen(args[1]), buf, reg->size, 0);
1817 reg_arch_type_t *arch_type = register_get_arch_type(reg->arch_type);
1818 arch_type->set(reg, buf);
1820 value = buf_to_str(reg->value, reg->size, 16);
1821 command_print(cmd_ctx, "%s (/%i): 0x%s", reg->name, (int)(reg->size), value);
1829 command_print(cmd_ctx, "usage: reg <#|name> [value]");
1834 static int handle_poll_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1836 int retval = ERROR_OK;
1837 target_t *target = get_current_target(cmd_ctx);
1841 command_print(cmd_ctx, "background polling: %s",
1842 target_continuous_poll ? "on" : "off");
1843 command_print(cmd_ctx, "TAP: %s (%s)",
1844 target->tap->dotted_name,
1845 target->tap->enabled ? "enabled" : "disabled");
1846 if (!target->tap->enabled)
1848 if ((retval = target_poll(target)) != ERROR_OK)
1850 if ((retval = target_arch_state(target)) != ERROR_OK)
1856 if (strcmp(args[0], "on") == 0)
1858 target_continuous_poll = 1;
1860 else if (strcmp(args[0], "off") == 0)
1862 target_continuous_poll = 0;
1866 command_print(cmd_ctx, "arg is \"on\" or \"off\"");
1870 return ERROR_COMMAND_SYNTAX_ERROR;
1876 static int handle_wait_halt_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1879 return ERROR_COMMAND_SYNTAX_ERROR;
1884 int retval = parse_uint(args[0], &ms);
1885 if (ERROR_OK != retval)
1887 command_print(cmd_ctx, "usage: %s [seconds]", cmd);
1888 return ERROR_COMMAND_SYNTAX_ERROR;
1890 // convert seconds (given) to milliseconds (needed)
1894 target_t *target = get_current_target(cmd_ctx);
1895 return target_wait_state(target, TARGET_HALTED, ms);
1898 /* wait for target state to change. The trick here is to have a low
1899 * latency for short waits and not to suck up all the CPU time
1902 * After 500ms, keep_alive() is invoked
1904 int target_wait_state(target_t *target, enum target_state state, int ms)
1907 long long then = 0, cur;
1912 if ((retval = target_poll(target)) != ERROR_OK)
1914 if (target->state == state)
1922 then = timeval_ms();
1923 LOG_DEBUG("waiting for target %s...",
1924 Jim_Nvp_value2name_simple(nvp_target_state,state)->name);
1932 if ((cur-then) > ms)
1934 LOG_ERROR("timed out while waiting for target %s",
1935 Jim_Nvp_value2name_simple(nvp_target_state,state)->name);
1943 static int handle_halt_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1947 target_t *target = get_current_target(cmd_ctx);
1948 int retval = target_halt(target);
1949 if (ERROR_OK != retval)
1955 retval = parse_uint(args[0], &wait);
1956 if (ERROR_OK != retval)
1957 return ERROR_COMMAND_SYNTAX_ERROR;
1962 return handle_wait_halt_command(cmd_ctx, cmd, args, argc);
1965 static int handle_soft_reset_halt_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1967 target_t *target = get_current_target(cmd_ctx);
1969 LOG_USER("requesting target halt and executing a soft reset");
1971 target->type->soft_reset_halt(target);
1976 static int handle_reset_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1979 return ERROR_COMMAND_SYNTAX_ERROR;
1981 enum target_reset_mode reset_mode = RESET_RUN;
1985 n = Jim_Nvp_name2value_simple(nvp_reset_modes, args[0]);
1986 if ((n->name == NULL) || (n->value == RESET_UNKNOWN)) {
1987 return ERROR_COMMAND_SYNTAX_ERROR;
1989 reset_mode = n->value;
1992 /* reset *all* targets */
1993 return target_process_reset(cmd_ctx, reset_mode);
1997 static int handle_resume_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2001 return ERROR_COMMAND_SYNTAX_ERROR;
2003 target_t *target = get_current_target(cmd_ctx);
2004 target_handle_event(target, TARGET_EVENT_OLD_pre_resume);
2006 /* with no args, resume from current pc, addr = 0,
2007 * with one arguments, addr = args[0],
2008 * handle breakpoints, not debugging */
2012 int retval = parse_u32(args[0], &addr);
2013 if (ERROR_OK != retval)
2018 return target_resume(target, current, addr, 1, 0);
2021 static int handle_step_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2024 return ERROR_COMMAND_SYNTAX_ERROR;
2028 /* with no args, step from current pc, addr = 0,
2029 * with one argument addr = args[0],
2030 * handle breakpoints, debugging */
2034 int retval = parse_u32(args[0], &addr);
2035 if (ERROR_OK != retval)
2039 target_t *target = get_current_target(cmd_ctx);
2040 return target->type->step(target, 0, addr, 1);
2043 static void handle_md_output(struct command_context_s *cmd_ctx,
2044 struct target_s *target, uint32_t address, unsigned size,
2045 unsigned count, const uint8_t *buffer)
2047 const unsigned line_bytecnt = 32;
2048 unsigned line_modulo = line_bytecnt / size;
2050 char output[line_bytecnt * 4 + 1];
2051 unsigned output_len = 0;
2053 const char *value_fmt;
2055 case 4: value_fmt = "%8.8x "; break;
2056 case 2: value_fmt = "%4.2x "; break;
2057 case 1: value_fmt = "%2.2x "; break;
2059 LOG_ERROR("invalid memory read size: %u", size);
2063 for (unsigned i = 0; i < count; i++)
2065 if (i % line_modulo == 0)
2067 output_len += snprintf(output + output_len,
2068 sizeof(output) - output_len,
2070 (unsigned)(address + (i*size)));
2074 const uint8_t *value_ptr = buffer + i * size;
2076 case 4: value = target_buffer_get_u32(target, value_ptr); break;
2077 case 2: value = target_buffer_get_u16(target, value_ptr); break;
2078 case 1: value = *value_ptr;
2080 output_len += snprintf(output + output_len,
2081 sizeof(output) - output_len,
2084 if ((i % line_modulo == line_modulo - 1) || (i == count - 1))
2086 command_print(cmd_ctx, "%s", output);
2092 static int handle_md_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2095 return ERROR_COMMAND_SYNTAX_ERROR;
2099 case 'w': size = 4; break;
2100 case 'h': size = 2; break;
2101 case 'b': size = 1; break;
2102 default: return ERROR_COMMAND_SYNTAX_ERROR;
2106 int retval = parse_u32(args[0], &address);
2107 if (ERROR_OK != retval)
2113 retval = parse_uint(args[1], &count);
2114 if (ERROR_OK != retval)
2118 uint8_t *buffer = calloc(count, size);
2120 target_t *target = get_current_target(cmd_ctx);
2121 retval = target_read_memory(target,
2122 address, size, count, buffer);
2123 if (ERROR_OK == retval)
2124 handle_md_output(cmd_ctx, target, address, size, count, buffer);
2131 static int handle_mw_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2133 if ((argc < 2) || (argc > 3))
2134 return ERROR_COMMAND_SYNTAX_ERROR;
2137 int retval = parse_u32(args[0], &address);
2138 if (ERROR_OK != retval)
2142 retval = parse_u32(args[1], &value);
2143 if (ERROR_OK != retval)
2149 retval = parse_uint(args[2], &count);
2150 if (ERROR_OK != retval)
2154 target_t *target = get_current_target(cmd_ctx);
2156 uint8_t value_buf[4];
2161 target_buffer_set_u32(target, value_buf, value);
2165 target_buffer_set_u16(target, value_buf, value);
2169 value_buf[0] = value;
2172 return ERROR_COMMAND_SYNTAX_ERROR;
2174 for (unsigned i = 0; i < count; i++)
2176 retval = target_write_memory(target,
2177 address + i * wordsize, wordsize, 1, value_buf);
2178 if (ERROR_OK != retval)
2187 static int parse_load_image_command_args(char **args, int argc,
2188 image_t *image, uint32_t *min_address, uint32_t *max_address)
2190 if (argc < 1 || argc > 5)
2191 return ERROR_COMMAND_SYNTAX_ERROR;
2193 /* a base address isn't always necessary,
2194 * default to 0x0 (i.e. don't relocate) */
2198 int retval = parse_u32(args[1], &addr);
2199 if (ERROR_OK != retval)
2200 return ERROR_COMMAND_SYNTAX_ERROR;
2201 image->base_address = addr;
2202 image->base_address_set = 1;
2205 image->base_address_set = 0;
2207 image->start_address_set = 0;
2211 int retval = parse_u32(args[3], min_address);
2212 if (ERROR_OK != retval)
2213 return ERROR_COMMAND_SYNTAX_ERROR;
2217 int retval = parse_u32(args[4], max_address);
2218 if (ERROR_OK != retval)
2219 return ERROR_COMMAND_SYNTAX_ERROR;
2220 // use size (given) to find max (required)
2221 *max_address += *min_address;
2224 if (*min_address > *max_address)
2225 return ERROR_COMMAND_SYNTAX_ERROR;
2230 static int handle_load_image_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2234 uint32_t image_size;
2235 uint32_t min_address = 0;
2236 uint32_t max_address = 0xffffffff;
2242 duration_t duration;
2243 char *duration_text;
2245 int retval = parse_load_image_command_args(args, argc,
2246 &image, &min_address, &max_address);
2247 if (ERROR_OK != retval)
2250 target_t *target = get_current_target(cmd_ctx);
2251 duration_start_measure(&duration);
2253 if (image_open(&image, args[0], (argc >= 3) ? args[2] : NULL) != ERROR_OK)
2260 for (i = 0; i < image.num_sections; i++)
2262 buffer = malloc(image.sections[i].size);
2265 command_print(cmd_ctx,
2266 "error allocating buffer for section (%d bytes)",
2267 (int)(image.sections[i].size));
2271 if ((retval = image_read_section(&image, i, 0x0, image.sections[i].size, buffer, &buf_cnt)) != ERROR_OK)
2277 uint32_t offset = 0;
2278 uint32_t length = buf_cnt;
2280 /* DANGER!!! beware of unsigned comparision here!!! */
2282 if ((image.sections[i].base_address + buf_cnt >= min_address)&&
2283 (image.sections[i].base_address < max_address))
2285 if (image.sections[i].base_address < min_address)
2287 /* clip addresses below */
2288 offset += min_address-image.sections[i].base_address;
2292 if (image.sections[i].base_address + buf_cnt > max_address)
2294 length -= (image.sections[i].base_address + buf_cnt)-max_address;
2297 if ((retval = target_write_buffer(target, image.sections[i].base_address + offset, length, buffer + offset)) != ERROR_OK)
2302 image_size += length;
2303 command_print(cmd_ctx, "%u byte written at address 0x%8.8" PRIx32 "",
2304 (unsigned int)length,
2305 image.sections[i].base_address + offset);
2311 if ((retvaltemp = duration_stop_measure(&duration, &duration_text)) != ERROR_OK)
2313 image_close(&image);
2317 if (retval == ERROR_OK)
2319 command_print(cmd_ctx, "downloaded %u byte in %s",
2320 (unsigned int)image_size,
2323 free(duration_text);
2325 image_close(&image);
2331 static int handle_dump_image_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2335 uint8_t buffer[560];
2338 duration_t duration;
2339 char *duration_text;
2341 target_t *target = get_current_target(cmd_ctx);
2345 command_print(cmd_ctx, "usage: dump_image <filename> <address> <size>");
2350 int retval = parse_u32(args[1], &address);
2351 if (ERROR_OK != retval)
2355 retval = parse_u32(args[2], &size);
2356 if (ERROR_OK != retval)
2359 if (fileio_open(&fileio, args[0], FILEIO_WRITE, FILEIO_BINARY) != ERROR_OK)
2364 duration_start_measure(&duration);
2368 uint32_t size_written;
2369 uint32_t this_run_size = (size > 560) ? 560 : size;
2371 retval = target_read_buffer(target, address, this_run_size, buffer);
2372 if (retval != ERROR_OK)
2377 retval = fileio_write(&fileio, this_run_size, buffer, &size_written);
2378 if (retval != ERROR_OK)
2383 size -= this_run_size;
2384 address += this_run_size;
2387 if ((retvaltemp = fileio_close(&fileio)) != ERROR_OK)
2390 if ((retvaltemp = duration_stop_measure(&duration, &duration_text)) != ERROR_OK)
2393 if (retval == ERROR_OK)
2395 command_print(cmd_ctx, "dumped %lld byte in %s",
2396 fileio.size, duration_text);
2397 free(duration_text);
2403 static int handle_verify_image_command_internal(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc, int verify)
2407 uint32_t image_size;
2409 int retval, retvaltemp;
2410 uint32_t checksum = 0;
2411 uint32_t mem_checksum = 0;
2415 duration_t duration;
2416 char *duration_text;
2418 target_t *target = get_current_target(cmd_ctx);
2422 return ERROR_COMMAND_SYNTAX_ERROR;
2427 LOG_ERROR("no target selected");
2431 duration_start_measure(&duration);
2436 retval = parse_u32(args[1], &addr);
2437 if (ERROR_OK != retval)
2438 return ERROR_COMMAND_SYNTAX_ERROR;
2439 image.base_address = addr;
2440 image.base_address_set = 1;
2444 image.base_address_set = 0;
2445 image.base_address = 0x0;
2448 image.start_address_set = 0;
2450 if ((retval = image_open(&image, args[0], (argc == 3) ? args[2] : NULL)) != ERROR_OK)
2457 for (i = 0; i < image.num_sections; i++)
2459 buffer = malloc(image.sections[i].size);
2462 command_print(cmd_ctx,
2463 "error allocating buffer for section (%d bytes)",
2464 (int)(image.sections[i].size));
2467 if ((retval = image_read_section(&image, i, 0x0, image.sections[i].size, buffer, &buf_cnt)) != ERROR_OK)
2475 /* calculate checksum of image */
2476 image_calculate_checksum(buffer, buf_cnt, &checksum);
2478 retval = target_checksum_memory(target, image.sections[i].base_address, buf_cnt, &mem_checksum);
2479 if (retval != ERROR_OK)
2485 if (checksum != mem_checksum)
2487 /* failed crc checksum, fall back to a binary compare */
2490 command_print(cmd_ctx, "checksum mismatch - attempting binary compare");
2492 data = (uint8_t*)malloc(buf_cnt);
2494 /* Can we use 32bit word accesses? */
2496 int count = buf_cnt;
2497 if ((count % 4) == 0)
2502 retval = target_read_memory(target, image.sections[i].base_address, size, count, data);
2503 if (retval == ERROR_OK)
2506 for (t = 0; t < buf_cnt; t++)
2508 if (data[t] != buffer[t])
2510 command_print(cmd_ctx,
2511 "Verify operation failed address 0x%08x. Was 0x%02x instead of 0x%02x\n",
2512 (unsigned)(t + image.sections[i].base_address),
2517 retval = ERROR_FAIL;
2531 command_print(cmd_ctx, "address 0x%08" PRIx32 " length 0x%08" PRIx32 "",
2532 image.sections[i].base_address,
2537 image_size += buf_cnt;
2541 if ((retvaltemp = duration_stop_measure(&duration, &duration_text)) != ERROR_OK)
2543 image_close(&image);
2547 if (retval == ERROR_OK)
2549 command_print(cmd_ctx, "verified %u bytes in %s",
2550 (unsigned int)image_size,
2553 free(duration_text);
2555 image_close(&image);
2560 static int handle_verify_image_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2562 return handle_verify_image_command_internal(cmd_ctx, cmd, args, argc, 1);
2565 static int handle_test_image_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2567 return handle_verify_image_command_internal(cmd_ctx, cmd, args, argc, 0);
2570 static int handle_bp_command_list(struct command_context_s *cmd_ctx)
2572 target_t *target = get_current_target(cmd_ctx);
2573 breakpoint_t *breakpoint = target->breakpoints;
2576 if (breakpoint->type == BKPT_SOFT)
2578 char* buf = buf_to_str(breakpoint->orig_instr,
2579 breakpoint->length, 16);
2580 command_print(cmd_ctx, "0x%8.8" PRIx32 ", 0x%x, %i, 0x%s",
2581 breakpoint->address,
2583 breakpoint->set, buf);
2588 command_print(cmd_ctx, "0x%8.8" PRIx32 ", 0x%x, %i",
2589 breakpoint->address,
2590 breakpoint->length, breakpoint->set);
2593 breakpoint = breakpoint->next;
2598 static int handle_bp_command_set(struct command_context_s *cmd_ctx,
2599 uint32_t addr, uint32_t length, int hw)
2601 target_t *target = get_current_target(cmd_ctx);
2602 int retval = breakpoint_add(target, addr, length, hw);
2603 if (ERROR_OK == retval)
2604 command_print(cmd_ctx, "breakpoint set at 0x%8.8" PRIx32 "", addr);
2606 LOG_ERROR("Failure setting breakpoint");
2610 static int handle_bp_command(struct command_context_s *cmd_ctx,
2611 char *cmd, char **args, int argc)
2614 return handle_bp_command_list(cmd_ctx);
2616 if (argc < 2 || argc > 3)
2618 command_print(cmd_ctx, "usage: bp <address> <length> ['hw']");
2619 return ERROR_COMMAND_SYNTAX_ERROR;
2623 int retval = parse_u32(args[0], &addr);
2624 if (ERROR_OK != retval)
2628 retval = parse_u32(args[1], &length);
2629 if (ERROR_OK != retval)
2635 if (strcmp(args[2], "hw") == 0)
2638 return ERROR_COMMAND_SYNTAX_ERROR;
2641 return handle_bp_command_set(cmd_ctx, addr, length, hw);
2644 static int handle_rbp_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2647 return ERROR_COMMAND_SYNTAX_ERROR;
2650 int retval = parse_u32(args[0], &addr);
2651 if (ERROR_OK != retval)
2654 target_t *target = get_current_target(cmd_ctx);
2655 breakpoint_remove(target, addr);
2660 static int handle_wp_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2662 target_t *target = get_current_target(cmd_ctx);
2666 watchpoint_t *watchpoint = target->watchpoints;
2670 command_print(cmd_ctx,
2671 "address: 0x%8.8" PRIx32 ", len: 0x%8.8x, r/w/a: %i, value: 0x%8.8" PRIx32 ", mask: 0x%8.8" PRIx32 "",
2672 watchpoint->address,
2674 (int)(watchpoint->rw),
2677 watchpoint = watchpoint->next;
2682 enum watchpoint_rw type = WPT_ACCESS;
2684 uint32_t length = 0;
2685 uint32_t data_value = 0x0;
2686 uint32_t data_mask = 0xffffffff;
2692 retval = parse_u32(args[4], &data_mask);
2693 if (ERROR_OK != retval)
2697 retval = parse_u32(args[3], &data_value);
2698 if (ERROR_OK != retval)
2714 LOG_ERROR("invalid watchpoint mode ('%c')", args[2][0]);
2715 return ERROR_COMMAND_SYNTAX_ERROR;
2719 retval = parse_u32(args[1], &length);
2720 if (ERROR_OK != retval)
2722 retval = parse_u32(args[0], &addr);
2723 if (ERROR_OK != retval)
2728 command_print(cmd_ctx, "usage: wp <address> <length> [r/w/a] [value] [mask]");
2729 return ERROR_COMMAND_SYNTAX_ERROR;
2732 retval = watchpoint_add(target, addr, length, type,
2733 data_value, data_mask);
2734 if (ERROR_OK != retval)
2735 LOG_ERROR("Failure setting watchpoints");
2740 static int handle_rwp_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2743 return ERROR_COMMAND_SYNTAX_ERROR;
2746 int retval = parse_u32(args[0], &addr);
2747 if (ERROR_OK != retval)
2750 target_t *target = get_current_target(cmd_ctx);
2751 watchpoint_remove(target, addr);
2758 * Translate a virtual address to a physical address.
2760 * The low-level target implementation must have logged a detailed error
2761 * which is forwarded to telnet/GDB session.
2763 static int handle_virt2phys_command(command_context_t *cmd_ctx,
2764 char *cmd, char **args, int argc)
2767 return ERROR_COMMAND_SYNTAX_ERROR;
2770 int retval = parse_u32(args[0], &va);
2771 if (ERROR_OK != retval)
2775 target_t *target = get_current_target(cmd_ctx);
2776 retval = target->type->virt2phys(target, va, &pa);
2777 if (retval == ERROR_OK)
2778 command_print(cmd_ctx, "Physical address 0x%08" PRIx32 "", pa);
2783 static void writeData(FILE *f, const void *data, size_t len)
2785 size_t written = fwrite(data, 1, len, f);
2787 LOG_ERROR("failed to write %zu bytes: %s", len, strerror(errno));
2790 static void writeLong(FILE *f, int l)
2793 for (i = 0; i < 4; i++)
2795 char c = (l >> (i*8))&0xff;
2796 writeData(f, &c, 1);
2801 static void writeString(FILE *f, char *s)
2803 writeData(f, s, strlen(s));
2806 /* Dump a gmon.out histogram file. */
2807 static void writeGmon(uint32_t *samples, uint32_t sampleNum, char *filename)
2810 FILE *f = fopen(filename, "w");
2813 writeString(f, "gmon");
2814 writeLong(f, 0x00000001); /* Version */
2815 writeLong(f, 0); /* padding */
2816 writeLong(f, 0); /* padding */
2817 writeLong(f, 0); /* padding */
2819 uint8_t zero = 0; /* GMON_TAG_TIME_HIST */
2820 writeData(f, &zero, 1);
2822 /* figure out bucket size */
2823 uint32_t min = samples[0];
2824 uint32_t max = samples[0];
2825 for (i = 0; i < sampleNum; i++)
2827 if (min > samples[i])
2831 if (max < samples[i])
2837 int addressSpace = (max-min + 1);
2839 static const uint32_t maxBuckets = 256 * 1024; /* maximum buckets. */
2840 uint32_t length = addressSpace;
2841 if (length > maxBuckets)
2843 length = maxBuckets;
2845 int *buckets = malloc(sizeof(int)*length);
2846 if (buckets == NULL)
2851 memset(buckets, 0, sizeof(int)*length);
2852 for (i = 0; i < sampleNum;i++)
2854 uint32_t address = samples[i];
2855 long long a = address-min;
2856 long long b = length-1;
2857 long long c = addressSpace-1;
2858 int index = (a*b)/c; /* danger!!!! int32 overflows */
2862 /* append binary memory gmon.out &profile_hist_hdr ((char*)&profile_hist_hdr + sizeof(struct gmon_hist_hdr)) */
2863 writeLong(f, min); /* low_pc */
2864 writeLong(f, max); /* high_pc */
2865 writeLong(f, length); /* # of samples */
2866 writeLong(f, 64000000); /* 64MHz */
2867 writeString(f, "seconds");
2868 for (i = 0; i < (15-strlen("seconds")); i++)
2869 writeData(f, &zero, 1);
2870 writeString(f, "s");
2872 /*append binary memory gmon.out profile_hist_data (profile_hist_data + profile_hist_hdr.hist_size) */
2874 char *data = malloc(2*length);
2877 for (i = 0; i < length;i++)
2886 data[i*2 + 1]=(val >> 8)&0xff;
2889 writeData(f, data, length * 2);
2899 /* profiling samples the CPU PC as quickly as OpenOCD is able, which will be used as a random sampling of PC */
2900 static int handle_profile_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2902 target_t *target = get_current_target(cmd_ctx);
2903 struct timeval timeout, now;
2905 gettimeofday(&timeout, NULL);
2908 return ERROR_COMMAND_SYNTAX_ERROR;
2911 int retval = parse_uint(args[0], &offset);
2912 if (ERROR_OK != retval)
2915 timeval_add_time(&timeout, offset, 0);
2917 command_print(cmd_ctx, "Starting profiling. Halting and resuming the target as often as we can...");
2919 static const int maxSample = 10000;
2920 uint32_t *samples = malloc(sizeof(uint32_t)*maxSample);
2921 if (samples == NULL)
2925 /* hopefully it is safe to cache! We want to stop/restart as quickly as possible. */
2926 reg_t *reg = register_get_by_name(target->reg_cache, "pc", 1);
2930 target_poll(target);
2931 if (target->state == TARGET_HALTED)
2933 uint32_t t=*((uint32_t *)reg->value);
2934 samples[numSamples++]=t;
2935 retval = target_resume(target, 1, 0, 0, 0); /* current pc, addr = 0, do not handle breakpoints, not debugging */
2936 target_poll(target);
2937 alive_sleep(10); /* sleep 10ms, i.e. <100 samples/second. */
2938 } else if (target->state == TARGET_RUNNING)
2940 /* We want to quickly sample the PC. */
2941 if ((retval = target_halt(target)) != ERROR_OK)
2948 command_print(cmd_ctx, "Target not halted or running");
2952 if (retval != ERROR_OK)
2957 gettimeofday(&now, NULL);
2958 if ((numSamples >= maxSample) || ((now.tv_sec >= timeout.tv_sec) && (now.tv_usec >= timeout.tv_usec)))
2960 command_print(cmd_ctx, "Profiling completed. %d samples.", numSamples);
2961 if ((retval = target_poll(target)) != ERROR_OK)
2966 if (target->state == TARGET_HALTED)
2968 target_resume(target, 1, 0, 0, 0); /* current pc, addr = 0, do not handle breakpoints, not debugging */
2970 if ((retval = target_poll(target)) != ERROR_OK)
2975 writeGmon(samples, numSamples, args[1]);
2976 command_print(cmd_ctx, "Wrote %s", args[1]);
2985 static int new_int_array_element(Jim_Interp * interp, const char *varname, int idx, uint32_t val)
2988 Jim_Obj *nameObjPtr, *valObjPtr;
2991 namebuf = alloc_printf("%s(%d)", varname, idx);
2995 nameObjPtr = Jim_NewStringObj(interp, namebuf, -1);
2996 valObjPtr = Jim_NewIntObj(interp, val);
2997 if (!nameObjPtr || !valObjPtr)
3003 Jim_IncrRefCount(nameObjPtr);
3004 Jim_IncrRefCount(valObjPtr);
3005 result = Jim_SetVariable(interp, nameObjPtr, valObjPtr);
3006 Jim_DecrRefCount(interp, nameObjPtr);
3007 Jim_DecrRefCount(interp, valObjPtr);
3009 /* printf("%s(%d) <= 0%08x\n", varname, idx, val); */
3013 static int jim_mem2array(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
3015 command_context_t *context;
3018 context = Jim_GetAssocData(interp, "context");
3019 if (context == NULL)
3021 LOG_ERROR("mem2array: no command context");
3024 target = get_current_target(context);
3027 LOG_ERROR("mem2array: no current target");
3031 return target_mem2array(interp, target, argc-1, argv + 1);
3034 static int target_mem2array(Jim_Interp *interp, target_t *target, int argc, Jim_Obj *const *argv)
3042 const char *varname;
3043 uint8_t buffer[4096];
3047 /* argv[1] = name of array to receive the data
3048 * argv[2] = desired width
3049 * argv[3] = memory address
3050 * argv[4] = count of times to read
3053 Jim_WrongNumArgs(interp, 1, argv, "varname width addr nelems");
3056 varname = Jim_GetString(argv[0], &len);
3057 /* given "foo" get space for worse case "foo(%d)" .. add 20 */
3059 e = Jim_GetLong(interp, argv[1], &l);
3065 e = Jim_GetLong(interp, argv[2], &l);
3070 e = Jim_GetLong(interp, argv[3], &l);
3086 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3087 Jim_AppendStrings(interp, Jim_GetResult(interp), "Invalid width param, must be 8/16/32", NULL);
3091 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3092 Jim_AppendStrings(interp, Jim_GetResult(interp), "mem2array: zero width read?", NULL);
3095 if ((addr + (len * width)) < addr) {
3096 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3097 Jim_AppendStrings(interp, Jim_GetResult(interp), "mem2array: addr + len - wraps to zero?", NULL);
3100 /* absurd transfer size? */
3102 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3103 Jim_AppendStrings(interp, Jim_GetResult(interp), "mem2array: absurd > 64K item request", NULL);
3108 ((width == 2) && ((addr & 1) == 0)) ||
3109 ((width == 4) && ((addr & 3) == 0))) {
3113 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3114 sprintf(buf, "mem2array address: 0x%08" PRIx32 " is not aligned for %" PRId32 " byte reads",
3117 Jim_AppendStrings(interp, Jim_GetResult(interp), buf , NULL);
3128 /* Slurp... in buffer size chunks */
3130 count = len; /* in objects.. */
3131 if (count > (sizeof(buffer)/width)) {
3132 count = (sizeof(buffer)/width);
3135 retval = target_read_memory(target, addr, width, count, buffer);
3136 if (retval != ERROR_OK) {
3138 LOG_ERROR("mem2array: Read @ 0x%08x, w=%d, cnt=%d, failed",
3142 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3143 Jim_AppendStrings(interp, Jim_GetResult(interp), "mem2array: cannot read memory", NULL);
3147 v = 0; /* shut up gcc */
3148 for (i = 0 ;i < count ;i++, n++) {
3151 v = target_buffer_get_u32(target, &buffer[i*width]);
3154 v = target_buffer_get_u16(target, &buffer[i*width]);
3157 v = buffer[i] & 0x0ff;
3160 new_int_array_element(interp, varname, n, v);
3166 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3171 static int get_int_array_element(Jim_Interp * interp, const char *varname, int idx, uint32_t *val)
3174 Jim_Obj *nameObjPtr, *valObjPtr;
3178 namebuf = alloc_printf("%s(%d)", varname, idx);
3182 nameObjPtr = Jim_NewStringObj(interp, namebuf, -1);
3189 Jim_IncrRefCount(nameObjPtr);
3190 valObjPtr = Jim_GetVariable(interp, nameObjPtr, JIM_ERRMSG);
3191 Jim_DecrRefCount(interp, nameObjPtr);
3193 if (valObjPtr == NULL)
3196 result = Jim_GetLong(interp, valObjPtr, &l);
3197 /* printf("%s(%d) => 0%08x\n", varname, idx, val); */
3202 static int jim_array2mem(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
3204 command_context_t *context;
3207 context = Jim_GetAssocData(interp, "context");
3208 if (context == NULL) {
3209 LOG_ERROR("array2mem: no command context");
3212 target = get_current_target(context);
3213 if (target == NULL) {
3214 LOG_ERROR("array2mem: no current target");
3218 return target_array2mem(interp,target, argc-1, argv + 1);
3221 static int target_array2mem(Jim_Interp *interp, target_t *target, int argc, Jim_Obj *const *argv)
3229 const char *varname;
3230 uint8_t buffer[4096];
3234 /* argv[1] = name of array to get the data
3235 * argv[2] = desired width
3236 * argv[3] = memory address
3237 * argv[4] = count to write
3240 Jim_WrongNumArgs(interp, 1, argv, "varname width addr nelems");
3243 varname = Jim_GetString(argv[0], &len);
3244 /* given "foo" get space for worse case "foo(%d)" .. add 20 */
3246 e = Jim_GetLong(interp, argv[1], &l);
3252 e = Jim_GetLong(interp, argv[2], &l);
3257 e = Jim_GetLong(interp, argv[3], &l);
3273 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3274 Jim_AppendStrings(interp, Jim_GetResult(interp), "Invalid width param, must be 8/16/32", NULL);
3278 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3279 Jim_AppendStrings(interp, Jim_GetResult(interp), "array2mem: zero width read?", NULL);
3282 if ((addr + (len * width)) < addr) {
3283 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3284 Jim_AppendStrings(interp, Jim_GetResult(interp), "array2mem: addr + len - wraps to zero?", NULL);
3287 /* absurd transfer size? */
3289 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3290 Jim_AppendStrings(interp, Jim_GetResult(interp), "array2mem: absurd > 64K item request", NULL);
3295 ((width == 2) && ((addr & 1) == 0)) ||
3296 ((width == 4) && ((addr & 3) == 0))) {
3300 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3301 sprintf(buf, "array2mem address: 0x%08x is not aligned for %d byte reads",
3304 Jim_AppendStrings(interp, Jim_GetResult(interp), buf , NULL);
3315 /* Slurp... in buffer size chunks */
3317 count = len; /* in objects.. */
3318 if (count > (sizeof(buffer)/width)) {
3319 count = (sizeof(buffer)/width);
3322 v = 0; /* shut up gcc */
3323 for (i = 0 ;i < count ;i++, n++) {
3324 get_int_array_element(interp, varname, n, &v);
3327 target_buffer_set_u32(target, &buffer[i*width], v);
3330 target_buffer_set_u16(target, &buffer[i*width], v);
3333 buffer[i] = v & 0x0ff;
3339 retval = target_write_memory(target, addr, width, count, buffer);
3340 if (retval != ERROR_OK) {
3342 LOG_ERROR("array2mem: Write @ 0x%08x, w=%d, cnt=%d, failed",
3346 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3347 Jim_AppendStrings(interp, Jim_GetResult(interp), "array2mem: cannot read memory", NULL);
3353 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3358 void target_all_handle_event(enum target_event e)
3362 LOG_DEBUG("**all*targets: event: %d, %s",
3364 Jim_Nvp_value2name_simple(nvp_target_event, e)->name);
3366 target = all_targets;
3368 target_handle_event(target, e);
3369 target = target->next;
3373 void target_handle_event(target_t *target, enum target_event e)
3375 target_event_action_t *teap;
3378 teap = target->event_action;
3382 if (teap->event == e) {
3384 LOG_DEBUG("target: (%d) %s (%s) event: %d (%s) action: %s\n",
3385 target->target_number,
3387 target_get_name(target),
3389 Jim_Nvp_value2name_simple(nvp_target_event, e)->name,
3390 Jim_GetString(teap->body, NULL));
3391 if (Jim_EvalObj(interp, teap->body) != JIM_OK)
3393 Jim_PrintErrorMessage(interp);
3399 LOG_DEBUG("event: %d %s - no action",
3401 Jim_Nvp_value2name_simple(nvp_target_event, e)->name);
3405 enum target_cfg_param {
3408 TCFG_WORK_AREA_VIRT,
3409 TCFG_WORK_AREA_PHYS,
3410 TCFG_WORK_AREA_SIZE,
3411 TCFG_WORK_AREA_BACKUP,
3414 TCFG_CHAIN_POSITION,
3417 static Jim_Nvp nvp_config_opts[] = {
3418 { .name = "-type", .value = TCFG_TYPE },
3419 { .name = "-event", .value = TCFG_EVENT },
3420 { .name = "-work-area-virt", .value = TCFG_WORK_AREA_VIRT },
3421 { .name = "-work-area-phys", .value = TCFG_WORK_AREA_PHYS },
3422 { .name = "-work-area-size", .value = TCFG_WORK_AREA_SIZE },
3423 { .name = "-work-area-backup", .value = TCFG_WORK_AREA_BACKUP },
3424 { .name = "-endian" , .value = TCFG_ENDIAN },
3425 { .name = "-variant", .value = TCFG_VARIANT },
3426 { .name = "-chain-position", .value = TCFG_CHAIN_POSITION },
3428 { .name = NULL, .value = -1 }
3431 static int target_configure(Jim_GetOptInfo *goi, target_t *target)
3439 /* parse config or cget options ... */
3440 while (goi->argc > 0) {
3441 Jim_SetEmptyResult(goi->interp);
3442 /* Jim_GetOpt_Debug(goi); */
3444 if (target->type->target_jim_configure) {
3445 /* target defines a configure function */
3446 /* target gets first dibs on parameters */
3447 e = (*(target->type->target_jim_configure))(target, goi);
3456 /* otherwise we 'continue' below */
3458 e = Jim_GetOpt_Nvp(goi, nvp_config_opts, &n);
3460 Jim_GetOpt_NvpUnknown(goi, nvp_config_opts, 0);
3466 if (goi->isconfigure) {
3467 Jim_SetResult_sprintf(goi->interp, "not setable: %s", n->name);
3471 if (goi->argc != 0) {
3472 Jim_WrongNumArgs(goi->interp, goi->argc, goi->argv, "NO PARAMS");
3476 Jim_SetResultString(goi->interp, target_get_name(target), -1);
3480 if (goi->argc == 0) {
3481 Jim_WrongNumArgs(goi->interp, goi->argc, goi->argv, "-event ?event-name? ...");
3485 e = Jim_GetOpt_Nvp(goi, nvp_target_event, &n);
3487 Jim_GetOpt_NvpUnknown(goi, nvp_target_event, 1);
3491 if (goi->isconfigure) {
3492 if (goi->argc != 1) {
3493 Jim_WrongNumArgs(goi->interp, goi->argc, goi->argv, "-event ?event-name? ?EVENT-BODY?");
3497 if (goi->argc != 0) {
3498 Jim_WrongNumArgs(goi->interp, goi->argc, goi->argv, "-event ?event-name?");
3504 target_event_action_t *teap;
3506 teap = target->event_action;
3507 /* replace existing? */
3509 if (teap->event == (enum target_event)n->value) {
3515 if (goi->isconfigure) {
3518 teap = calloc(1, sizeof(*teap));
3520 teap->event = n->value;
3521 Jim_GetOpt_Obj(goi, &o);
3523 Jim_DecrRefCount(interp, teap->body);
3525 teap->body = Jim_DuplicateObj(goi->interp, o);
3528 * Tcl/TK - "tk events" have a nice feature.
3529 * See the "BIND" command.
3530 * We should support that here.
3531 * You can specify %X and %Y in the event code.
3532 * The idea is: %T - target name.
3533 * The idea is: %N - target number
3534 * The idea is: %E - event name.
3536 Jim_IncrRefCount(teap->body);
3538 /* add to head of event list */
3539 teap->next = target->event_action;
3540 target->event_action = teap;
3541 Jim_SetEmptyResult(goi->interp);
3545 Jim_SetEmptyResult(goi->interp);
3547 Jim_SetResult(goi->interp, Jim_DuplicateObj(goi->interp, teap->body));
3554 case TCFG_WORK_AREA_VIRT:
3555 if (goi->isconfigure) {
3556 target_free_all_working_areas(target);
3557 e = Jim_GetOpt_Wide(goi, &w);
3561 target->working_area_virt = w;
3563 if (goi->argc != 0) {
3567 Jim_SetResult(interp, Jim_NewIntObj(goi->interp, target->working_area_virt));
3571 case TCFG_WORK_AREA_PHYS:
3572 if (goi->isconfigure) {
3573 target_free_all_working_areas(target);
3574 e = Jim_GetOpt_Wide(goi, &w);
3578 target->working_area_phys = w;
3580 if (goi->argc != 0) {
3584 Jim_SetResult(interp, Jim_NewIntObj(goi->interp, target->working_area_phys));
3588 case TCFG_WORK_AREA_SIZE:
3589 if (goi->isconfigure) {
3590 target_free_all_working_areas(target);
3591 e = Jim_GetOpt_Wide(goi, &w);
3595 target->working_area_size = w;
3597 if (goi->argc != 0) {
3601 Jim_SetResult(interp, Jim_NewIntObj(goi->interp, target->working_area_size));
3605 case TCFG_WORK_AREA_BACKUP:
3606 if (goi->isconfigure) {
3607 target_free_all_working_areas(target);
3608 e = Jim_GetOpt_Wide(goi, &w);
3612 /* make this exactly 1 or 0 */
3613 target->backup_working_area = (!!w);
3615 if (goi->argc != 0) {
3619 Jim_SetResult(interp, Jim_NewIntObj(goi->interp, target->backup_working_area));
3620 /* loop for more e*/
3624 if (goi->isconfigure) {
3625 e = Jim_GetOpt_Nvp(goi, nvp_target_endian, &n);
3627 Jim_GetOpt_NvpUnknown(goi, nvp_target_endian, 1);
3630 target->endianness = n->value;
3632 if (goi->argc != 0) {
3636 n = Jim_Nvp_value2name_simple(nvp_target_endian, target->endianness);
3637 if (n->name == NULL) {
3638 target->endianness = TARGET_LITTLE_ENDIAN;
3639 n = Jim_Nvp_value2name_simple(nvp_target_endian, target->endianness);
3641 Jim_SetResultString(goi->interp, n->name, -1);
3646 if (goi->isconfigure) {
3647 if (goi->argc < 1) {
3648 Jim_SetResult_sprintf(goi->interp,
3653 if (target->variant) {
3654 free((void *)(target->variant));
3656 e = Jim_GetOpt_String(goi, &cp, NULL);
3657 target->variant = strdup(cp);
3659 if (goi->argc != 0) {
3663 Jim_SetResultString(goi->interp, target->variant,-1);
3666 case TCFG_CHAIN_POSITION:
3667 if (goi->isconfigure) {
3670 target_free_all_working_areas(target);
3671 e = Jim_GetOpt_Obj(goi, &o);
3675 tap = jtag_tap_by_jim_obj(goi->interp, o);
3679 /* make this exactly 1 or 0 */
3682 if (goi->argc != 0) {
3686 Jim_SetResultString(interp, target->tap->dotted_name, -1);
3687 /* loop for more e*/
3690 } /* while (goi->argc) */
3693 /* done - we return */
3697 /** this is the 'tcl' handler for the target specific command */
3698 static int tcl_target_func(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
3703 uint8_t target_buf[32];
3706 struct command_context_s *cmd_ctx;
3713 TS_CMD_MWW, TS_CMD_MWH, TS_CMD_MWB,
3714 TS_CMD_MDW, TS_CMD_MDH, TS_CMD_MDB,
3715 TS_CMD_MRW, TS_CMD_MRH, TS_CMD_MRB,
3716 TS_CMD_MEM2ARRAY, TS_CMD_ARRAY2MEM,
3724 TS_CMD_INVOKE_EVENT,
3727 static const Jim_Nvp target_options[] = {
3728 { .name = "configure", .value = TS_CMD_CONFIGURE },
3729 { .name = "cget", .value = TS_CMD_CGET },
3730 { .name = "mww", .value = TS_CMD_MWW },
3731 { .name = "mwh", .value = TS_CMD_MWH },
3732 { .name = "mwb", .value = TS_CMD_MWB },
3733 { .name = "mdw", .value = TS_CMD_MDW },
3734 { .name = "mdh", .value = TS_CMD_MDH },
3735 { .name = "mdb", .value = TS_CMD_MDB },
3736 { .name = "mem2array", .value = TS_CMD_MEM2ARRAY },
3737 { .name = "array2mem", .value = TS_CMD_ARRAY2MEM },
3738 { .name = "eventlist", .value = TS_CMD_EVENTLIST },
3739 { .name = "curstate", .value = TS_CMD_CURSTATE },
3741 { .name = "arp_examine", .value = TS_CMD_EXAMINE },
3742 { .name = "arp_poll", .value = TS_CMD_POLL },
3743 { .name = "arp_reset", .value = TS_CMD_RESET },
3744 { .name = "arp_halt", .value = TS_CMD_HALT },
3745 { .name = "arp_waitstate", .value = TS_CMD_WAITSTATE },
3746 { .name = "invoke-event", .value = TS_CMD_INVOKE_EVENT },
3748 { .name = NULL, .value = -1 },
3751 /* go past the "command" */
3752 Jim_GetOpt_Setup(&goi, interp, argc-1, argv + 1);
3754 target = Jim_CmdPrivData(goi.interp);
3755 cmd_ctx = Jim_GetAssocData(goi.interp, "context");
3757 /* commands here are in an NVP table */
3758 e = Jim_GetOpt_Nvp(&goi, target_options, &n);
3760 Jim_GetOpt_NvpUnknown(&goi, target_options, 0);
3763 /* Assume blank result */
3764 Jim_SetEmptyResult(goi.interp);
3767 case TS_CMD_CONFIGURE:
3769 Jim_WrongNumArgs(goi.interp, goi.argc, goi.argv, "missing: -option VALUE ...");
3772 goi.isconfigure = 1;
3773 return target_configure(&goi, target);
3775 // some things take params
3777 Jim_WrongNumArgs(goi.interp, 0, goi.argv, "missing: ?-option?");
3780 goi.isconfigure = 0;
3781 return target_configure(&goi, target);
3789 * argv[3] = optional count.
3792 if ((goi.argc == 2) || (goi.argc == 3)) {
3796 Jim_SetResult_sprintf(goi.interp, "expected: %s ADDR DATA [COUNT]", n->name);
3800 e = Jim_GetOpt_Wide(&goi, &a);
3805 e = Jim_GetOpt_Wide(&goi, &b);
3809 if (goi.argc == 3) {
3810 e = Jim_GetOpt_Wide(&goi, &c);
3820 target_buffer_set_u32(target, target_buf, b);
3824 target_buffer_set_u16(target, target_buf, b);
3828 target_buffer_set_u8(target, target_buf, b);
3832 for (x = 0 ; x < c ; x++) {
3833 e = target_write_memory(target, a, b, 1, target_buf);
3834 if (e != ERROR_OK) {
3835 Jim_SetResult_sprintf(interp, "Error writing @ 0x%08x: %d\n", (int)(a), e);
3848 /* argv[0] = command
3850 * argv[2] = optional count
3852 if ((goi.argc == 2) || (goi.argc == 3)) {
3853 Jim_SetResult_sprintf(goi.interp, "expected: %s ADDR [COUNT]", n->name);
3856 e = Jim_GetOpt_Wide(&goi, &a);
3861 e = Jim_GetOpt_Wide(&goi, &c);
3868 b = 1; /* shut up gcc */
3881 /* convert to "bytes" */
3883 /* count is now in 'BYTES' */
3889 e = target_read_memory(target, a, b, y / b, target_buf);
3890 if (e != ERROR_OK) {
3891 Jim_SetResult_sprintf(interp, "error reading target @ 0x%08lx", (int)(a));
3895 Jim_fprintf(interp, interp->cookie_stdout, "0x%08x ", (int)(a));
3898 for (x = 0 ; (x < 16) && (x < y) ; x += 4) {
3899 z = target_buffer_get_u32(target, &(target_buf[ x * 4 ]));
3900 Jim_fprintf(interp, interp->cookie_stdout, "%08x ", (int)(z));
3902 for (; (x < 16) ; x += 4) {
3903 Jim_fprintf(interp, interp->cookie_stdout, " ");
3907 for (x = 0 ; (x < 16) && (x < y) ; x += 2) {
3908 z = target_buffer_get_u16(target, &(target_buf[ x * 2 ]));
3909 Jim_fprintf(interp, interp->cookie_stdout, "%04x ", (int)(z));
3911 for (; (x < 16) ; x += 2) {
3912 Jim_fprintf(interp, interp->cookie_stdout, " ");
3917 for (x = 0 ; (x < 16) && (x < y) ; x += 1) {
3918 z = target_buffer_get_u8(target, &(target_buf[ x * 4 ]));
3919 Jim_fprintf(interp, interp->cookie_stdout, "%02x ", (int)(z));
3921 for (; (x < 16) ; x += 1) {
3922 Jim_fprintf(interp, interp->cookie_stdout, " ");
3926 /* ascii-ify the bytes */
3927 for (x = 0 ; x < y ; x++) {
3928 if ((target_buf[x] >= 0x20) &&
3929 (target_buf[x] <= 0x7e)) {
3933 target_buf[x] = '.';
3938 target_buf[x] = ' ';
3943 /* print - with a newline */
3944 Jim_fprintf(interp, interp->cookie_stdout, "%s\n", target_buf);
3950 case TS_CMD_MEM2ARRAY:
3951 return target_mem2array(goi.interp, target, goi.argc, goi.argv);
3953 case TS_CMD_ARRAY2MEM:
3954 return target_array2mem(goi.interp, target, goi.argc, goi.argv);
3956 case TS_CMD_EXAMINE:
3958 Jim_WrongNumArgs(goi.interp, 2, argv, "[no parameters]");
3961 if (!target->tap->enabled)
3962 goto err_tap_disabled;
3963 e = target->type->examine(target);
3964 if (e != ERROR_OK) {
3965 Jim_SetResult_sprintf(interp, "examine-fails: %d", e);
3971 Jim_WrongNumArgs(goi.interp, 2, argv, "[no parameters]");
3974 if (!target->tap->enabled)
3975 goto err_tap_disabled;
3976 if (!(target_was_examined(target))) {
3977 e = ERROR_TARGET_NOT_EXAMINED;
3979 e = target->type->poll(target);
3981 if (e != ERROR_OK) {
3982 Jim_SetResult_sprintf(interp, "poll-fails: %d", e);
3989 if (goi.argc != 2) {
3990 Jim_WrongNumArgs(interp, 2, argv, "t | f|assert | deassert BOOL");
3993 e = Jim_GetOpt_Nvp(&goi, nvp_assert, &n);
3995 Jim_GetOpt_NvpUnknown(&goi, nvp_assert, 1);
3998 /* the halt or not param */
3999 e = Jim_GetOpt_Wide(&goi, &a);
4003 if (!target->tap->enabled)
4004 goto err_tap_disabled;
4005 /* determine if we should halt or not. */
4006 target->reset_halt = !!a;
4007 /* When this happens - all workareas are invalid. */
4008 target_free_all_working_areas_restore(target, 0);
4011 if (n->value == NVP_ASSERT) {
4012 target->type->assert_reset(target);
4014 target->type->deassert_reset(target);
4019 Jim_WrongNumArgs(goi.interp, 0, argv, "halt [no parameters]");
4022 if (!target->tap->enabled)
4023 goto err_tap_disabled;
4024 target->type->halt(target);
4026 case TS_CMD_WAITSTATE:
4027 /* params: <name> statename timeoutmsecs */
4028 if (goi.argc != 2) {
4029 Jim_SetResult_sprintf(goi.interp, "%s STATENAME TIMEOUTMSECS", n->name);
4032 e = Jim_GetOpt_Nvp(&goi, nvp_target_state, &n);
4034 Jim_GetOpt_NvpUnknown(&goi, nvp_target_state,1);
4037 e = Jim_GetOpt_Wide(&goi, &a);
4041 if (!target->tap->enabled)
4042 goto err_tap_disabled;
4043 e = target_wait_state(target, n->value, a);
4044 if (e != ERROR_OK) {
4045 Jim_SetResult_sprintf(goi.interp,
4046 "target: %s wait %s fails (%d) %s",
4049 e, target_strerror_safe(e));
4054 case TS_CMD_EVENTLIST:
4055 /* List for human, Events defined for this target.
4056 * scripts/programs should use 'name cget -event NAME'
4059 target_event_action_t *teap;
4060 teap = target->event_action;
4061 command_print(cmd_ctx, "Event actions for target (%d) %s\n",
4062 target->target_number,
4064 command_print(cmd_ctx, "%-25s | Body", "Event");
4065 command_print(cmd_ctx, "------------------------- | ----------------------------------------");
4067 command_print(cmd_ctx,
4069 Jim_Nvp_value2name_simple(nvp_target_event, teap->event)->name,
4070 Jim_GetString(teap->body, NULL));
4073 command_print(cmd_ctx, "***END***");
4076 case TS_CMD_CURSTATE:
4077 if (goi.argc != 0) {
4078 Jim_WrongNumArgs(goi.interp, 0, argv, "[no parameters]");
4081 Jim_SetResultString(goi.interp,
4082 Jim_Nvp_value2name_simple(nvp_target_state,target->state)->name,-1);
4084 case TS_CMD_INVOKE_EVENT:
4085 if (goi.argc != 1) {
4086 Jim_SetResult_sprintf(goi.interp, "%s ?EVENTNAME?",n->name);
4089 e = Jim_GetOpt_Nvp(&goi, nvp_target_event, &n);
4091 Jim_GetOpt_NvpUnknown(&goi, nvp_target_event, 1);
4094 target_handle_event(target, n->value);
4100 Jim_SetResult_sprintf(interp, "[TAP is disabled]");
4104 static int target_create(Jim_GetOptInfo *goi)
4113 struct command_context_s *cmd_ctx;
4115 cmd_ctx = Jim_GetAssocData(goi->interp, "context");
4116 if (goi->argc < 3) {
4117 Jim_WrongNumArgs(goi->interp, 1, goi->argv, "?name? ?type? ..options...");
4122 Jim_GetOpt_Obj(goi, &new_cmd);
4123 /* does this command exist? */
4124 cmd = Jim_GetCommand(goi->interp, new_cmd, JIM_ERRMSG);
4126 cp = Jim_GetString(new_cmd, NULL);
4127 Jim_SetResult_sprintf(goi->interp, "Command/target: %s Exists", cp);
4132 e = Jim_GetOpt_String(goi, &cp2, NULL);
4134 /* now does target type exist */
4135 for (x = 0 ; target_types[x] ; x++) {
4136 if (0 == strcmp(cp, target_types[x]->name)) {
4141 if (target_types[x] == NULL) {
4142 Jim_SetResult_sprintf(goi->interp, "Unknown target type %s, try one of ", cp);
4143 for (x = 0 ; target_types[x] ; x++) {
4144 if (target_types[x + 1]) {
4145 Jim_AppendStrings(goi->interp,
4146 Jim_GetResult(goi->interp),
4147 target_types[x]->name,
4150 Jim_AppendStrings(goi->interp,
4151 Jim_GetResult(goi->interp),
4153 target_types[x]->name,NULL);
4160 target = calloc(1,sizeof(target_t));
4161 /* set target number */
4162 target->target_number = new_target_number();
4164 /* allocate memory for each unique target type */
4165 target->type = (target_type_t*)calloc(1,sizeof(target_type_t));
4167 memcpy(target->type, target_types[x], sizeof(target_type_t));
4169 /* will be set by "-endian" */
4170 target->endianness = TARGET_ENDIAN_UNKNOWN;
4172 target->working_area = 0x0;
4173 target->working_area_size = 0x0;
4174 target->working_areas = NULL;
4175 target->backup_working_area = 0;
4177 target->state = TARGET_UNKNOWN;
4178 target->debug_reason = DBG_REASON_UNDEFINED;
4179 target->reg_cache = NULL;
4180 target->breakpoints = NULL;
4181 target->watchpoints = NULL;
4182 target->next = NULL;
4183 target->arch_info = NULL;
4185 target->display = 1;
4187 /* initialize trace information */
4188 target->trace_info = malloc(sizeof(trace_t));
4189 target->trace_info->num_trace_points = 0;
4190 target->trace_info->trace_points_size = 0;
4191 target->trace_info->trace_points = NULL;
4192 target->trace_info->trace_history_size = 0;
4193 target->trace_info->trace_history = NULL;
4194 target->trace_info->trace_history_pos = 0;
4195 target->trace_info->trace_history_overflowed = 0;
4197 target->dbgmsg = NULL;
4198 target->dbg_msg_enabled = 0;
4200 target->endianness = TARGET_ENDIAN_UNKNOWN;
4202 /* Do the rest as "configure" options */
4203 goi->isconfigure = 1;
4204 e = target_configure(goi, target);
4206 if (target->tap == NULL)
4208 Jim_SetResultString(interp, "-chain-position required when creating target", -1);
4218 if (target->endianness == TARGET_ENDIAN_UNKNOWN) {
4219 /* default endian to little if not specified */
4220 target->endianness = TARGET_LITTLE_ENDIAN;
4223 /* incase variant is not set */
4224 if (!target->variant)
4225 target->variant = strdup("");
4227 /* create the target specific commands */
4228 if (target->type->register_commands) {
4229 (*(target->type->register_commands))(cmd_ctx);
4231 if (target->type->target_create) {
4232 (*(target->type->target_create))(target, goi->interp);
4235 /* append to end of list */
4238 tpp = &(all_targets);
4240 tpp = &((*tpp)->next);
4245 cp = Jim_GetString(new_cmd, NULL);
4246 target->cmd_name = strdup(cp);
4248 /* now - create the new target name command */
4249 e = Jim_CreateCommand(goi->interp,
4252 tcl_target_func, /* C function */
4253 target, /* private data */
4254 NULL); /* no del proc */
4259 static int jim_target(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
4263 struct command_context_s *cmd_ctx;
4267 /* TG = target generic */
4275 const char *target_cmds[] = {
4276 "create", "types", "names", "current", "number",
4278 NULL /* terminate */
4281 LOG_DEBUG("Target command params:");
4282 LOG_DEBUG("%s", Jim_Debug_ArgvString(interp, argc, argv));
4284 cmd_ctx = Jim_GetAssocData(interp, "context");
4286 Jim_GetOpt_Setup(&goi, interp, argc-1, argv + 1);
4288 if (goi.argc == 0) {
4289 Jim_WrongNumArgs(interp, 1, argv, "missing: command ...");
4293 /* Jim_GetOpt_Debug(&goi); */
4294 r = Jim_GetOpt_Enum(&goi, target_cmds, &x);
4301 Jim_Panic(goi.interp,"Why am I here?");
4303 case TG_CMD_CURRENT:
4304 if (goi.argc != 0) {
4305 Jim_WrongNumArgs(goi.interp, 1, goi.argv, "Too many parameters");
4308 Jim_SetResultString(goi.interp, get_current_target(cmd_ctx)->cmd_name, -1);
4311 if (goi.argc != 0) {
4312 Jim_WrongNumArgs(goi.interp, 1, goi.argv, "Too many parameters");
4315 Jim_SetResult(goi.interp, Jim_NewListObj(goi.interp, NULL, 0));
4316 for (x = 0 ; target_types[x] ; x++) {
4317 Jim_ListAppendElement(goi.interp,
4318 Jim_GetResult(goi.interp),
4319 Jim_NewStringObj(goi.interp, target_types[x]->name, -1));
4323 if (goi.argc != 0) {
4324 Jim_WrongNumArgs(goi.interp, 1, goi.argv, "Too many parameters");
4327 Jim_SetResult(goi.interp, Jim_NewListObj(goi.interp, NULL, 0));
4328 target = all_targets;
4330 Jim_ListAppendElement(goi.interp,
4331 Jim_GetResult(goi.interp),
4332 Jim_NewStringObj(goi.interp, target->cmd_name, -1));
4333 target = target->next;
4338 Jim_WrongNumArgs(goi.interp, goi.argc, goi.argv, "?name ... config options ...");
4341 return target_create(&goi);
4344 if (goi.argc != 1) {
4345 Jim_SetResult_sprintf(goi.interp, "expected: target number ?NUMBER?");
4348 e = Jim_GetOpt_Wide(&goi, &w);
4354 t = get_target_by_num(w);
4356 Jim_SetResult_sprintf(goi.interp,"Target: number %d does not exist", (int)(w));
4359 Jim_SetResultString(goi.interp, t->cmd_name, -1);
4363 if (goi.argc != 0) {
4364 Jim_WrongNumArgs(goi.interp, 0, goi.argv, "<no parameters>");
4367 Jim_SetResult(goi.interp,
4368 Jim_NewIntObj(goi.interp, max_target_number()));
4384 static int fastload_num;
4385 static struct FastLoad *fastload;
4387 static void free_fastload(void)
4389 if (fastload != NULL)
4392 for (i = 0; i < fastload_num; i++)
4394 if (fastload[i].data)
4395 free(fastload[i].data);
4405 static int handle_fast_load_image_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
4409 uint32_t image_size;
4410 uint32_t min_address = 0;
4411 uint32_t max_address = 0xffffffff;
4416 duration_t duration;
4417 char *duration_text;
4419 int retval = parse_load_image_command_args(args, argc,
4420 &image, &min_address, &max_address);
4421 if (ERROR_OK != retval)
4424 duration_start_measure(&duration);
4426 if (image_open(&image, args[0], (argc >= 3) ? args[2] : NULL) != ERROR_OK)
4433 fastload_num = image.num_sections;
4434 fastload = (struct FastLoad *)malloc(sizeof(struct FastLoad)*image.num_sections);
4435 if (fastload == NULL)
4437 image_close(&image);
4440 memset(fastload, 0, sizeof(struct FastLoad)*image.num_sections);
4441 for (i = 0; i < image.num_sections; i++)
4443 buffer = malloc(image.sections[i].size);
4446 command_print(cmd_ctx, "error allocating buffer for section (%d bytes)",
4447 (int)(image.sections[i].size));
4451 if ((retval = image_read_section(&image, i, 0x0, image.sections[i].size, buffer, &buf_cnt)) != ERROR_OK)
4457 uint32_t offset = 0;
4458 uint32_t length = buf_cnt;
4461 /* DANGER!!! beware of unsigned comparision here!!! */
4463 if ((image.sections[i].base_address + buf_cnt >= min_address)&&
4464 (image.sections[i].base_address < max_address))
4466 if (image.sections[i].base_address < min_address)
4468 /* clip addresses below */
4469 offset += min_address-image.sections[i].base_address;
4473 if (image.sections[i].base_address + buf_cnt > max_address)
4475 length -= (image.sections[i].base_address + buf_cnt)-max_address;
4478 fastload[i].address = image.sections[i].base_address + offset;
4479 fastload[i].data = malloc(length);
4480 if (fastload[i].data == NULL)
4485 memcpy(fastload[i].data, buffer + offset, length);
4486 fastload[i].length = length;
4488 image_size += length;
4489 command_print(cmd_ctx, "%u byte written at address 0x%8.8x",
4490 (unsigned int)length,
4491 ((unsigned int)(image.sections[i].base_address + offset)));
4497 duration_stop_measure(&duration, &duration_text);
4498 if (retval == ERROR_OK)
4500 command_print(cmd_ctx, "Loaded %u bytes in %s", (unsigned int)image_size, duration_text);
4501 command_print(cmd_ctx, "NB!!! image has not been loaded to target, issue a subsequent 'fast_load' to do so.");
4503 free(duration_text);
4505 image_close(&image);
4507 if (retval != ERROR_OK)
4515 static int handle_fast_load_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
4518 return ERROR_COMMAND_SYNTAX_ERROR;
4519 if (fastload == NULL)
4521 LOG_ERROR("No image in memory");
4525 int ms = timeval_ms();
4527 int retval = ERROR_OK;
4528 for (i = 0; i < fastload_num;i++)
4530 target_t *target = get_current_target(cmd_ctx);
4531 command_print(cmd_ctx, "Write to 0x%08x, length 0x%08x",
4532 (unsigned int)(fastload[i].address),
4533 (unsigned int)(fastload[i].length));
4534 if (retval == ERROR_OK)
4536 retval = target_write_buffer(target, fastload[i].address, fastload[i].length, fastload[i].data);
4538 size += fastload[i].length;
4540 int after = timeval_ms();
4541 command_print(cmd_ctx, "Loaded image %f kBytes/s", (float)(size/1024.0)/((float)(after-ms)/1000.0));