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 },
241 target_state_name( target_t *t )
244 cp = Jim_Nvp_value2name_simple(nvp_target_state, t->state)->name;
246 LOG_ERROR("Invalid target state: %d", (int)(t->state));
247 cp = "(*BUG*unknown*BUG*)";
252 static int max_target_number(void)
260 if (x < t->target_number) {
261 x = (t->target_number) + 1;
268 /* determine the number of the new target */
269 static int new_target_number(void)
274 /* number is 0 based */
278 if (x < t->target_number) {
279 x = t->target_number;
286 static int target_continuous_poll = 1;
288 /* read a uint32_t from a buffer in target memory endianness */
289 uint32_t target_buffer_get_u32(target_t *target, const uint8_t *buffer)
291 if (target->endianness == TARGET_LITTLE_ENDIAN)
292 return le_to_h_u32(buffer);
294 return be_to_h_u32(buffer);
297 /* read a uint16_t from a buffer in target memory endianness */
298 uint16_t target_buffer_get_u16(target_t *target, const uint8_t *buffer)
300 if (target->endianness == TARGET_LITTLE_ENDIAN)
301 return le_to_h_u16(buffer);
303 return be_to_h_u16(buffer);
306 /* read a uint8_t from a buffer in target memory endianness */
307 uint8_t target_buffer_get_u8(target_t *target, const uint8_t *buffer)
309 return *buffer & 0x0ff;
312 /* write a uint32_t to a buffer in target memory endianness */
313 void target_buffer_set_u32(target_t *target, uint8_t *buffer, uint32_t value)
315 if (target->endianness == TARGET_LITTLE_ENDIAN)
316 h_u32_to_le(buffer, value);
318 h_u32_to_be(buffer, value);
321 /* write a uint16_t to a buffer in target memory endianness */
322 void target_buffer_set_u16(target_t *target, uint8_t *buffer, uint16_t value)
324 if (target->endianness == TARGET_LITTLE_ENDIAN)
325 h_u16_to_le(buffer, value);
327 h_u16_to_be(buffer, value);
330 /* write a uint8_t to a buffer in target memory endianness */
331 void target_buffer_set_u8(target_t *target, uint8_t *buffer, uint8_t value)
336 /* return a pointer to a configured target; id is name or number */
337 target_t *get_target(const char *id)
341 /* try as tcltarget name */
342 for (target = all_targets; target; target = target->next) {
343 if (target->cmd_name == NULL)
345 if (strcmp(id, target->cmd_name) == 0)
349 /* no match, try as number */
351 if (parse_uint(id, &num) != ERROR_OK)
354 for (target = all_targets; target; target = target->next) {
355 if (target->target_number == (int)num)
362 /* returns a pointer to the n-th configured target */
363 static target_t *get_target_by_num(int num)
365 target_t *target = all_targets;
368 if (target->target_number == num) {
371 target = target->next;
377 int get_num_by_target(target_t *query_target)
379 return query_target->target_number;
382 target_t* get_current_target(command_context_t *cmd_ctx)
384 target_t *target = get_target_by_num(cmd_ctx->current_target);
388 LOG_ERROR("BUG: current_target out of bounds");
395 int target_poll(struct target_s *target)
397 /* We can't poll until after examine */
398 if (!target_was_examined(target))
400 /* Fail silently lest we pollute the log */
403 return target->type->poll(target);
406 int target_halt(struct target_s *target)
408 /* We can't poll until after examine */
409 if (!target_was_examined(target))
411 LOG_ERROR("Target not examined yet");
414 return target->type->halt(target);
417 int target_resume(struct target_s *target, int current, uint32_t address, int handle_breakpoints, int debug_execution)
421 /* We can't poll until after examine */
422 if (!target_was_examined(target))
424 LOG_ERROR("Target not examined yet");
428 /* note that resume *must* be asynchronous. The CPU can halt before we poll. The CPU can
429 * even halt at the current PC as a result of a software breakpoint being inserted by (a bug?)
432 if ((retval = target->type->resume(target, current, address, handle_breakpoints, debug_execution)) != ERROR_OK)
438 int target_process_reset(struct command_context_s *cmd_ctx, enum target_reset_mode reset_mode)
443 n = Jim_Nvp_value2name_simple(nvp_reset_modes, reset_mode);
444 if (n->name == NULL) {
445 LOG_ERROR("invalid reset mode");
449 /* disable polling during reset to make reset event scripts
450 * more predictable, i.e. dr/irscan & pathmove in events will
451 * not have JTAG operations injected into the middle of a sequence.
453 int save_poll = target_continuous_poll;
454 target_continuous_poll = 0;
456 sprintf(buf, "ocd_process_reset %s", n->name);
457 retval = Jim_Eval(interp, buf);
459 target_continuous_poll = save_poll;
461 if (retval != JIM_OK) {
462 Jim_PrintErrorMessage(interp);
466 /* We want any events to be processed before the prompt */
467 retval = target_call_timer_callbacks_now();
472 static int default_virt2phys(struct target_s *target, uint32_t virtual, uint32_t *physical)
478 static int default_mmu(struct target_s *target, int *enabled)
484 static int default_examine(struct target_s *target)
486 target_set_examined(target);
490 int target_examine_one(struct target_s *target)
492 return target->type->examine(target);
495 static int jtag_enable_callback(enum jtag_event event, void *priv)
497 target_t *target = priv;
499 if (event != JTAG_TAP_EVENT_ENABLE || !target->tap->enabled)
502 jtag_unregister_event_callback(jtag_enable_callback, target);
503 return target_examine_one(target);
507 /* Targets that correctly implement init + examine, i.e.
508 * no communication with target during init:
512 int target_examine(void)
514 int retval = ERROR_OK;
517 for (target = all_targets; target; target = target->next)
519 /* defer examination, but don't skip it */
520 if (!target->tap->enabled) {
521 jtag_register_event_callback(jtag_enable_callback,
525 if ((retval = target_examine_one(target)) != ERROR_OK)
530 const char *target_get_name(struct target_s *target)
532 return target->type->name;
535 static int target_write_memory_imp(struct target_s *target, uint32_t address, uint32_t size, uint32_t count, uint8_t *buffer)
537 if (!target_was_examined(target))
539 LOG_ERROR("Target not examined yet");
542 return target->type->write_memory_imp(target, address, size, count, buffer);
545 static int target_read_memory_imp(struct target_s *target, uint32_t address, uint32_t size, uint32_t count, uint8_t *buffer)
547 if (!target_was_examined(target))
549 LOG_ERROR("Target not examined yet");
552 return target->type->read_memory_imp(target, address, size, count, buffer);
555 static int target_soft_reset_halt_imp(struct target_s *target)
557 if (!target_was_examined(target))
559 LOG_ERROR("Target not examined yet");
562 return target->type->soft_reset_halt_imp(target);
565 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)
567 if (!target_was_examined(target))
569 LOG_ERROR("Target not examined yet");
572 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);
575 int target_read_memory(struct target_s *target,
576 uint32_t address, uint32_t size, uint32_t count, uint8_t *buffer)
578 return target->type->read_memory(target, address, size, count, buffer);
581 int target_write_memory(struct target_s *target,
582 uint32_t address, uint32_t size, uint32_t count, uint8_t *buffer)
584 return target->type->write_memory(target, address, size, count, buffer);
586 int target_bulk_write_memory(struct target_s *target,
587 uint32_t address, uint32_t count, uint8_t *buffer)
589 return target->type->bulk_write_memory(target, address, count, buffer);
592 int target_add_breakpoint(struct target_s *target,
593 struct breakpoint_s *breakpoint)
595 return target->type->add_breakpoint(target, breakpoint);
597 int target_remove_breakpoint(struct target_s *target,
598 struct breakpoint_s *breakpoint)
600 return target->type->remove_breakpoint(target, breakpoint);
603 int target_add_watchpoint(struct target_s *target,
604 struct watchpoint_s *watchpoint)
606 return target->type->add_watchpoint(target, watchpoint);
608 int target_remove_watchpoint(struct target_s *target,
609 struct watchpoint_s *watchpoint)
611 return target->type->remove_watchpoint(target, watchpoint);
614 int target_get_gdb_reg_list(struct target_s *target,
615 struct reg_s **reg_list[], int *reg_list_size)
617 return target->type->get_gdb_reg_list(target, reg_list, reg_list_size);
619 int target_step(struct target_s *target,
620 int current, uint32_t address, int handle_breakpoints)
622 return target->type->step(target, current, address, handle_breakpoints);
626 int target_run_algorithm(struct target_s *target,
627 int num_mem_params, mem_param_t *mem_params,
628 int num_reg_params, reg_param_t *reg_param,
629 uint32_t entry_point, uint32_t exit_point,
630 int timeout_ms, void *arch_info)
632 return target->type->run_algorithm(target,
633 num_mem_params, mem_params, num_reg_params, reg_param,
634 entry_point, exit_point, timeout_ms, arch_info);
637 /// @returns @c true if the target has been examined.
638 bool target_was_examined(struct target_s *target)
640 return target->type->examined;
642 /// Sets the @c examined flag for the given target.
643 void target_set_examined(struct target_s *target)
645 target->type->examined = true;
647 // Reset the @c examined flag for the given target.
648 void target_reset_examined(struct target_s *target)
650 target->type->examined = false;
654 int target_init(struct command_context_s *cmd_ctx)
656 target_t *target = all_targets;
661 target_reset_examined(target);
662 if (target->type->examine == NULL)
664 target->type->examine = default_examine;
667 if ((retval = target->type->init_target(cmd_ctx, target)) != ERROR_OK)
669 LOG_ERROR("target '%s' init failed", target_get_name(target));
673 /* Set up default functions if none are provided by target */
674 if (target->type->virt2phys == NULL)
676 target->type->virt2phys = default_virt2phys;
678 target->type->virt2phys = default_virt2phys;
679 /* a non-invasive way(in terms of patches) to add some code that
680 * runs before the type->write/read_memory implementation
682 target->type->write_memory_imp = target->type->write_memory;
683 target->type->write_memory = target_write_memory_imp;
684 target->type->read_memory_imp = target->type->read_memory;
685 target->type->read_memory = target_read_memory_imp;
686 target->type->soft_reset_halt_imp = target->type->soft_reset_halt;
687 target->type->soft_reset_halt = target_soft_reset_halt_imp;
688 target->type->run_algorithm_imp = target->type->run_algorithm;
689 target->type->run_algorithm = target_run_algorithm_imp;
691 if (target->type->mmu == NULL)
693 target->type->mmu = default_mmu;
695 target = target->next;
700 if ((retval = target_register_user_commands(cmd_ctx)) != ERROR_OK)
702 if ((retval = target_register_timer_callback(handle_target, 100, 1, NULL)) != ERROR_OK)
709 int target_register_event_callback(int (*callback)(struct target_s *target, enum target_event event, void *priv), void *priv)
711 target_event_callback_t **callbacks_p = &target_event_callbacks;
713 if (callback == NULL)
715 return ERROR_INVALID_ARGUMENTS;
720 while ((*callbacks_p)->next)
721 callbacks_p = &((*callbacks_p)->next);
722 callbacks_p = &((*callbacks_p)->next);
725 (*callbacks_p) = malloc(sizeof(target_event_callback_t));
726 (*callbacks_p)->callback = callback;
727 (*callbacks_p)->priv = priv;
728 (*callbacks_p)->next = NULL;
733 int target_register_timer_callback(int (*callback)(void *priv), int time_ms, int periodic, void *priv)
735 target_timer_callback_t **callbacks_p = &target_timer_callbacks;
738 if (callback == NULL)
740 return ERROR_INVALID_ARGUMENTS;
745 while ((*callbacks_p)->next)
746 callbacks_p = &((*callbacks_p)->next);
747 callbacks_p = &((*callbacks_p)->next);
750 (*callbacks_p) = malloc(sizeof(target_timer_callback_t));
751 (*callbacks_p)->callback = callback;
752 (*callbacks_p)->periodic = periodic;
753 (*callbacks_p)->time_ms = time_ms;
755 gettimeofday(&now, NULL);
756 (*callbacks_p)->when.tv_usec = now.tv_usec + (time_ms % 1000) * 1000;
757 time_ms -= (time_ms % 1000);
758 (*callbacks_p)->when.tv_sec = now.tv_sec + (time_ms / 1000);
759 if ((*callbacks_p)->when.tv_usec > 1000000)
761 (*callbacks_p)->when.tv_usec = (*callbacks_p)->when.tv_usec - 1000000;
762 (*callbacks_p)->when.tv_sec += 1;
765 (*callbacks_p)->priv = priv;
766 (*callbacks_p)->next = NULL;
771 int target_unregister_event_callback(int (*callback)(struct target_s *target, enum target_event event, void *priv), void *priv)
773 target_event_callback_t **p = &target_event_callbacks;
774 target_event_callback_t *c = target_event_callbacks;
776 if (callback == NULL)
778 return ERROR_INVALID_ARGUMENTS;
783 target_event_callback_t *next = c->next;
784 if ((c->callback == callback) && (c->priv == priv))
798 int target_unregister_timer_callback(int (*callback)(void *priv), void *priv)
800 target_timer_callback_t **p = &target_timer_callbacks;
801 target_timer_callback_t *c = target_timer_callbacks;
803 if (callback == NULL)
805 return ERROR_INVALID_ARGUMENTS;
810 target_timer_callback_t *next = c->next;
811 if ((c->callback == callback) && (c->priv == priv))
825 int target_call_event_callbacks(target_t *target, enum target_event event)
827 target_event_callback_t *callback = target_event_callbacks;
828 target_event_callback_t *next_callback;
830 if (event == TARGET_EVENT_HALTED)
832 /* execute early halted first */
833 target_call_event_callbacks(target, TARGET_EVENT_EARLY_HALTED);
836 LOG_DEBUG("target event %i (%s)",
838 Jim_Nvp_value2name_simple(nvp_target_event, event)->name);
840 target_handle_event(target, event);
844 next_callback = callback->next;
845 callback->callback(target, event, callback->priv);
846 callback = next_callback;
852 static int target_timer_callback_periodic_restart(
853 target_timer_callback_t *cb, struct timeval *now)
855 int time_ms = cb->time_ms;
856 cb->when.tv_usec = now->tv_usec + (time_ms % 1000) * 1000;
857 time_ms -= (time_ms % 1000);
858 cb->when.tv_sec = now->tv_sec + time_ms / 1000;
859 if (cb->when.tv_usec > 1000000)
861 cb->when.tv_usec = cb->when.tv_usec - 1000000;
862 cb->when.tv_sec += 1;
867 static int target_call_timer_callback(target_timer_callback_t *cb,
870 cb->callback(cb->priv);
873 return target_timer_callback_periodic_restart(cb, now);
875 return target_unregister_timer_callback(cb->callback, cb->priv);
878 static int target_call_timer_callbacks_check_time(int checktime)
883 gettimeofday(&now, NULL);
885 target_timer_callback_t *callback = target_timer_callbacks;
888 // cleaning up may unregister and free this callback
889 target_timer_callback_t *next_callback = callback->next;
891 bool call_it = callback->callback &&
892 ((!checktime && callback->periodic) ||
893 now.tv_sec > callback->when.tv_sec ||
894 (now.tv_sec == callback->when.tv_sec &&
895 now.tv_usec >= callback->when.tv_usec));
899 int retval = target_call_timer_callback(callback, &now);
900 if (retval != ERROR_OK)
904 callback = next_callback;
910 int target_call_timer_callbacks(void)
912 return target_call_timer_callbacks_check_time(1);
915 /* invoke periodic callbacks immediately */
916 int target_call_timer_callbacks_now(void)
918 return target_call_timer_callbacks_check_time(0);
921 int target_alloc_working_area(struct target_s *target, uint32_t size, working_area_t **area)
923 working_area_t *c = target->working_areas;
924 working_area_t *new_wa = NULL;
926 /* Reevaluate working area address based on MMU state*/
927 if (target->working_areas == NULL)
931 retval = target->type->mmu(target, &enabled);
932 if (retval != ERROR_OK)
938 target->working_area = target->working_area_virt;
942 target->working_area = target->working_area_phys;
946 /* only allocate multiples of 4 byte */
949 LOG_ERROR("BUG: code tried to allocate unaligned number of bytes (0x%08x), padding", ((unsigned)(size)));
950 size = (size + 3) & (~3);
953 /* see if there's already a matching working area */
956 if ((c->free) && (c->size == size))
964 /* if not, allocate a new one */
967 working_area_t **p = &target->working_areas;
968 uint32_t first_free = target->working_area;
969 uint32_t free_size = target->working_area_size;
971 LOG_DEBUG("allocating new working area");
973 c = target->working_areas;
976 first_free += c->size;
977 free_size -= c->size;
982 if (free_size < size)
984 LOG_WARNING("not enough working area available(requested %u, free %u)",
985 (unsigned)(size), (unsigned)(free_size));
986 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
989 new_wa = malloc(sizeof(working_area_t));
992 new_wa->address = first_free;
994 if (target->backup_working_area)
997 new_wa->backup = malloc(new_wa->size);
998 if ((retval = target_read_memory(target, new_wa->address, 4, new_wa->size / 4, new_wa->backup)) != ERROR_OK)
1000 free(new_wa->backup);
1007 new_wa->backup = NULL;
1010 /* put new entry in list */
1014 /* mark as used, and return the new (reused) area */
1019 new_wa->user = area;
1024 int target_free_working_area_restore(struct target_s *target, working_area_t *area, int restore)
1029 if (restore && target->backup_working_area)
1032 if ((retval = target_write_memory(target, area->address, 4, area->size / 4, area->backup)) != ERROR_OK)
1038 /* mark user pointer invalid */
1045 int target_free_working_area(struct target_s *target, working_area_t *area)
1047 return target_free_working_area_restore(target, area, 1);
1050 /* free resources and restore memory, if restoring memory fails,
1051 * free up resources anyway
1053 void target_free_all_working_areas_restore(struct target_s *target, int restore)
1055 working_area_t *c = target->working_areas;
1059 working_area_t *next = c->next;
1060 target_free_working_area_restore(target, c, restore);
1070 target->working_areas = NULL;
1073 void target_free_all_working_areas(struct target_s *target)
1075 target_free_all_working_areas_restore(target, 1);
1078 int target_register_commands(struct command_context_s *cmd_ctx)
1081 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)");
1086 register_jim(cmd_ctx, "target", jim_target, "configure target");
1091 int target_arch_state(struct target_s *target)
1096 LOG_USER("No target has been configured");
1100 LOG_USER("target state: %s", target_state_name( target ));
1102 if (target->state != TARGET_HALTED)
1105 retval = target->type->arch_state(target);
1109 /* Single aligned words are guaranteed to use 16 or 32 bit access
1110 * mode respectively, otherwise data is handled as quickly as
1113 int target_write_buffer(struct target_s *target, uint32_t address, uint32_t size, uint8_t *buffer)
1116 LOG_DEBUG("writing buffer of %i byte at 0x%8.8x",
1117 (int)size, (unsigned)address);
1119 if (!target_was_examined(target))
1121 LOG_ERROR("Target not examined yet");
1129 if ((address + size - 1) < address)
1131 /* GDB can request this when e.g. PC is 0xfffffffc*/
1132 LOG_ERROR("address + size wrapped(0x%08x, 0x%08x)",
1138 if (((address % 2) == 0) && (size == 2))
1140 return target_write_memory(target, address, 2, 1, buffer);
1143 /* handle unaligned head bytes */
1146 uint32_t unaligned = 4 - (address % 4);
1148 if (unaligned > size)
1151 if ((retval = target_write_memory(target, address, 1, unaligned, buffer)) != ERROR_OK)
1154 buffer += unaligned;
1155 address += unaligned;
1159 /* handle aligned words */
1162 int aligned = size - (size % 4);
1164 /* use bulk writes above a certain limit. This may have to be changed */
1167 if ((retval = target->type->bulk_write_memory(target, address, aligned / 4, buffer)) != ERROR_OK)
1172 if ((retval = target_write_memory(target, address, 4, aligned / 4, buffer)) != ERROR_OK)
1181 /* handle tail writes of less than 4 bytes */
1184 if ((retval = target_write_memory(target, address, 1, size, buffer)) != ERROR_OK)
1191 /* Single aligned words are guaranteed to use 16 or 32 bit access
1192 * mode respectively, otherwise data is handled as quickly as
1195 int target_read_buffer(struct target_s *target, uint32_t address, uint32_t size, uint8_t *buffer)
1198 LOG_DEBUG("reading buffer of %i byte at 0x%8.8x",
1199 (int)size, (unsigned)address);
1201 if (!target_was_examined(target))
1203 LOG_ERROR("Target not examined yet");
1211 if ((address + size - 1) < address)
1213 /* GDB can request this when e.g. PC is 0xfffffffc*/
1214 LOG_ERROR("address + size wrapped(0x%08" PRIx32 ", 0x%08" PRIx32 ")",
1220 if (((address % 2) == 0) && (size == 2))
1222 return target_read_memory(target, address, 2, 1, buffer);
1225 /* handle unaligned head bytes */
1228 uint32_t unaligned = 4 - (address % 4);
1230 if (unaligned > size)
1233 if ((retval = target_read_memory(target, address, 1, unaligned, buffer)) != ERROR_OK)
1236 buffer += unaligned;
1237 address += unaligned;
1241 /* handle aligned words */
1244 int aligned = size - (size % 4);
1246 if ((retval = target_read_memory(target, address, 4, aligned / 4, buffer)) != ERROR_OK)
1254 /* handle tail writes of less than 4 bytes */
1257 if ((retval = target_read_memory(target, address, 1, size, buffer)) != ERROR_OK)
1264 int target_checksum_memory(struct target_s *target, uint32_t address, uint32_t size, uint32_t* crc)
1269 uint32_t checksum = 0;
1270 if (!target_was_examined(target))
1272 LOG_ERROR("Target not examined yet");
1276 if ((retval = target->type->checksum_memory(target, address,
1277 size, &checksum)) != ERROR_OK)
1279 buffer = malloc(size);
1282 LOG_ERROR("error allocating buffer for section (%d bytes)", (int)size);
1283 return ERROR_INVALID_ARGUMENTS;
1285 retval = target_read_buffer(target, address, size, buffer);
1286 if (retval != ERROR_OK)
1292 /* convert to target endianess */
1293 for (i = 0; i < (size/sizeof(uint32_t)); i++)
1295 uint32_t target_data;
1296 target_data = target_buffer_get_u32(target, &buffer[i*sizeof(uint32_t)]);
1297 target_buffer_set_u32(target, &buffer[i*sizeof(uint32_t)], target_data);
1300 retval = image_calculate_checksum(buffer, size, &checksum);
1309 int target_blank_check_memory(struct target_s *target, uint32_t address, uint32_t size, uint32_t* blank)
1312 if (!target_was_examined(target))
1314 LOG_ERROR("Target not examined yet");
1318 if (target->type->blank_check_memory == 0)
1319 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
1321 retval = target->type->blank_check_memory(target, address, size, blank);
1326 int target_read_u32(struct target_s *target, uint32_t address, uint32_t *value)
1328 uint8_t value_buf[4];
1329 if (!target_was_examined(target))
1331 LOG_ERROR("Target not examined yet");
1335 int retval = target_read_memory(target, address, 4, 1, value_buf);
1337 if (retval == ERROR_OK)
1339 *value = target_buffer_get_u32(target, value_buf);
1340 LOG_DEBUG("address: 0x%8.8" PRIx32 ", value: 0x%8.8" PRIx32 "",
1347 LOG_DEBUG("address: 0x%8.8" PRIx32 " failed",
1354 int target_read_u16(struct target_s *target, uint32_t address, uint16_t *value)
1356 uint8_t value_buf[2];
1357 if (!target_was_examined(target))
1359 LOG_ERROR("Target not examined yet");
1363 int retval = target_read_memory(target, address, 2, 1, value_buf);
1365 if (retval == ERROR_OK)
1367 *value = target_buffer_get_u16(target, value_buf);
1368 LOG_DEBUG("address: 0x%8.8" PRIx32 ", value: 0x%4.4x",
1375 LOG_DEBUG("address: 0x%8.8" PRIx32 " failed",
1382 int target_read_u8(struct target_s *target, uint32_t address, uint8_t *value)
1384 int retval = target_read_memory(target, address, 1, 1, value);
1385 if (!target_was_examined(target))
1387 LOG_ERROR("Target not examined yet");
1391 if (retval == ERROR_OK)
1393 LOG_DEBUG("address: 0x%8.8" PRIx32 ", value: 0x%2.2x",
1400 LOG_DEBUG("address: 0x%8.8" PRIx32 " failed",
1407 int target_write_u32(struct target_s *target, uint32_t address, uint32_t value)
1410 uint8_t value_buf[4];
1411 if (!target_was_examined(target))
1413 LOG_ERROR("Target not examined yet");
1417 LOG_DEBUG("address: 0x%8.8" PRIx32 ", value: 0x%8.8" PRIx32 "",
1421 target_buffer_set_u32(target, value_buf, value);
1422 if ((retval = target_write_memory(target, address, 4, 1, value_buf)) != ERROR_OK)
1424 LOG_DEBUG("failed: %i", retval);
1430 int target_write_u16(struct target_s *target, uint32_t address, uint16_t value)
1433 uint8_t value_buf[2];
1434 if (!target_was_examined(target))
1436 LOG_ERROR("Target not examined yet");
1440 LOG_DEBUG("address: 0x%8.8" PRIx32 ", value: 0x%8.8x",
1444 target_buffer_set_u16(target, value_buf, value);
1445 if ((retval = target_write_memory(target, address, 2, 1, value_buf)) != ERROR_OK)
1447 LOG_DEBUG("failed: %i", retval);
1453 int target_write_u8(struct target_s *target, uint32_t address, uint8_t value)
1456 if (!target_was_examined(target))
1458 LOG_ERROR("Target not examined yet");
1462 LOG_DEBUG("address: 0x%8.8" PRIx32 ", value: 0x%2.2x",
1465 if ((retval = target_write_memory(target, address, 1, 1, &value)) != ERROR_OK)
1467 LOG_DEBUG("failed: %i", retval);
1473 int target_register_user_commands(struct command_context_s *cmd_ctx)
1475 int retval = ERROR_OK;
1478 /* script procedures */
1479 register_command(cmd_ctx, NULL, "profile", handle_profile_command, COMMAND_EXEC, "profiling samples the CPU PC");
1480 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>");
1481 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>");
1483 register_command(cmd_ctx, NULL, "fast_load_image", handle_fast_load_image_command, COMMAND_ANY,
1484 "same args as load_image, image stored in memory - mainly for profiling purposes");
1486 register_command(cmd_ctx, NULL, "fast_load", handle_fast_load_command, COMMAND_ANY,
1487 "loads active fast load image to current target - mainly for profiling purposes");
1490 register_command(cmd_ctx, NULL, "virt2phys", handle_virt2phys_command, COMMAND_ANY, "translate a virtual address into a physical address");
1491 register_command(cmd_ctx, NULL, "reg", handle_reg_command, COMMAND_EXEC, "display or set a register");
1492 register_command(cmd_ctx, NULL, "poll", handle_poll_command, COMMAND_EXEC, "poll target state");
1493 register_command(cmd_ctx, NULL, "wait_halt", handle_wait_halt_command, COMMAND_EXEC, "wait for target halt [time (s)]");
1494 register_command(cmd_ctx, NULL, "halt", handle_halt_command, COMMAND_EXEC, "halt target");
1495 register_command(cmd_ctx, NULL, "resume", handle_resume_command, COMMAND_EXEC, "resume target [addr]");
1496 register_command(cmd_ctx, NULL, "step", handle_step_command, COMMAND_EXEC, "step one instruction from current PC or [addr]");
1497 register_command(cmd_ctx, NULL, "reset", handle_reset_command, COMMAND_EXEC, "reset target [run | halt | init] - default is run");
1498 register_command(cmd_ctx, NULL, "soft_reset_halt", handle_soft_reset_halt_command, COMMAND_EXEC, "halt the target and do a soft reset");
1500 register_command(cmd_ctx, NULL, "mdw", handle_md_command, COMMAND_EXEC, "display memory words <addr> [count]");
1501 register_command(cmd_ctx, NULL, "mdh", handle_md_command, COMMAND_EXEC, "display memory half-words <addr> [count]");
1502 register_command(cmd_ctx, NULL, "mdb", handle_md_command, COMMAND_EXEC, "display memory bytes <addr> [count]");
1504 register_command(cmd_ctx, NULL, "mww", handle_mw_command, COMMAND_EXEC, "write memory word <addr> <value> [count]");
1505 register_command(cmd_ctx, NULL, "mwh", handle_mw_command, COMMAND_EXEC, "write memory half-word <addr> <value> [count]");
1506 register_command(cmd_ctx, NULL, "mwb", handle_mw_command, COMMAND_EXEC, "write memory byte <addr> <value> [count]");
1508 register_command(cmd_ctx, NULL, "bp",
1509 handle_bp_command, COMMAND_EXEC,
1510 "list or set breakpoint [<address> <length> [hw]]");
1511 register_command(cmd_ctx, NULL, "rbp",
1512 handle_rbp_command, COMMAND_EXEC,
1513 "remove breakpoint <address>");
1514 register_command(cmd_ctx, NULL, "wp",
1515 handle_wp_command, COMMAND_EXEC,
1516 "list or set watchpoint "
1517 "[<address> <length> <r/w/a> [value] [mask]]");
1518 register_command(cmd_ctx, NULL, "rwp",
1519 handle_rwp_command, COMMAND_EXEC,
1520 "remove watchpoint <address>");
1522 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]");
1523 register_command(cmd_ctx, NULL, "dump_image", handle_dump_image_command, COMMAND_EXEC, "dump_image <file> <address> <size>");
1524 register_command(cmd_ctx, NULL, "verify_image", handle_verify_image_command, COMMAND_EXEC, "verify_image <file> [offset] [type]");
1525 register_command(cmd_ctx, NULL, "test_image", handle_test_image_command, COMMAND_EXEC, "test_image <file> [offset] [type]");
1527 if ((retval = target_request_register_commands(cmd_ctx)) != ERROR_OK)
1529 if ((retval = trace_register_commands(cmd_ctx)) != ERROR_OK)
1535 static int handle_targets_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1537 target_t *target = all_targets;
1541 target = get_target(args[0]);
1542 if (target == NULL) {
1543 command_print(cmd_ctx,"Target: %s is unknown, try one of:\n", args[0]);
1546 if (!target->tap->enabled) {
1547 command_print(cmd_ctx,"Target: TAP %s is disabled, "
1548 "can't be the current target\n",
1549 target->tap->dotted_name);
1553 cmd_ctx->current_target = target->target_number;
1558 target = all_targets;
1559 command_print(cmd_ctx, " TargetName Type Endian TapName State ");
1560 command_print(cmd_ctx, "-- ------------------ ---------- ------ ------------------ ------------");
1566 if (target->tap->enabled)
1567 state = target_state_name( target );
1569 state = "tap-disabled";
1571 if (cmd_ctx->current_target == target->target_number)
1574 /* keep columns lined up to match the headers above */
1575 command_print(cmd_ctx, "%2d%c %-18s %-10s %-6s %-18s %s",
1576 target->target_number,
1579 target_get_name(target),
1580 Jim_Nvp_value2name_simple(nvp_target_endian,
1581 target->endianness)->name,
1582 target->tap->dotted_name,
1584 target = target->next;
1590 /* every 300ms we check for reset & powerdropout and issue a "reset halt" if so. */
1592 static int powerDropout;
1593 static int srstAsserted;
1595 static int runPowerRestore;
1596 static int runPowerDropout;
1597 static int runSrstAsserted;
1598 static int runSrstDeasserted;
1600 static int sense_handler(void)
1602 static int prevSrstAsserted = 0;
1603 static int prevPowerdropout = 0;
1606 if ((retval = jtag_power_dropout(&powerDropout)) != ERROR_OK)
1610 powerRestored = prevPowerdropout && !powerDropout;
1613 runPowerRestore = 1;
1616 long long current = timeval_ms();
1617 static long long lastPower = 0;
1618 int waitMore = lastPower + 2000 > current;
1619 if (powerDropout && !waitMore)
1621 runPowerDropout = 1;
1622 lastPower = current;
1625 if ((retval = jtag_srst_asserted(&srstAsserted)) != ERROR_OK)
1629 srstDeasserted = prevSrstAsserted && !srstAsserted;
1631 static long long lastSrst = 0;
1632 waitMore = lastSrst + 2000 > current;
1633 if (srstDeasserted && !waitMore)
1635 runSrstDeasserted = 1;
1639 if (!prevSrstAsserted && srstAsserted)
1641 runSrstAsserted = 1;
1644 prevSrstAsserted = srstAsserted;
1645 prevPowerdropout = powerDropout;
1647 if (srstDeasserted || powerRestored)
1649 /* Other than logging the event we can't do anything here.
1650 * Issuing a reset is a particularly bad idea as we might
1651 * be inside a reset already.
1658 /* process target state changes */
1659 int handle_target(void *priv)
1661 int retval = ERROR_OK;
1663 /* we do not want to recurse here... */
1664 static int recursive = 0;
1669 /* danger! running these procedures can trigger srst assertions and power dropouts.
1670 * We need to avoid an infinite loop/recursion here and we do that by
1671 * clearing the flags after running these events.
1673 int did_something = 0;
1674 if (runSrstAsserted)
1676 Jim_Eval(interp, "srst_asserted");
1679 if (runSrstDeasserted)
1681 Jim_Eval(interp, "srst_deasserted");
1684 if (runPowerDropout)
1686 Jim_Eval(interp, "power_dropout");
1689 if (runPowerRestore)
1691 Jim_Eval(interp, "power_restore");
1697 /* clear detect flags */
1701 /* clear action flags */
1703 runSrstAsserted = 0;
1704 runSrstDeasserted = 0;
1705 runPowerRestore = 0;
1706 runPowerDropout = 0;
1711 /* Poll targets for state changes unless that's globally disabled.
1712 * Skip targets that are currently disabled.
1714 for (target_t *target = all_targets;
1715 target_continuous_poll && target;
1716 target = target->next)
1718 if (!target->tap->enabled)
1721 /* only poll target if we've got power and srst isn't asserted */
1722 if (!powerDropout && !srstAsserted)
1724 /* polling may fail silently until the target has been examined */
1725 if ((retval = target_poll(target)) != ERROR_OK)
1733 static int handle_reg_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1742 target = get_current_target(cmd_ctx);
1744 /* list all available registers for the current target */
1747 reg_cache_t *cache = target->reg_cache;
1754 for (i = 0, reg = cache->reg_list;
1755 i < cache->num_regs;
1756 i++, reg++, count++)
1758 /* only print cached values if they are valid */
1760 value = buf_to_str(reg->value,
1762 command_print(cmd_ctx,
1763 "(%i) %s (/%" PRIu32 "): 0x%s%s",
1771 command_print(cmd_ctx, "(%i) %s (/%" PRIu32 ")",
1776 cache = cache->next;
1782 /* access a single register by its ordinal number */
1783 if ((args[0][0] >= '0') && (args[0][0] <= '9'))
1786 int retval = parse_uint(args[0], &num);
1787 if (ERROR_OK != retval)
1788 return ERROR_COMMAND_SYNTAX_ERROR;
1790 reg_cache_t *cache = target->reg_cache;
1795 for (i = 0; i < cache->num_regs; i++)
1797 if (count++ == (int)num)
1799 reg = &cache->reg_list[i];
1805 cache = cache->next;
1810 command_print(cmd_ctx, "%i is out of bounds, the current target has only %i registers (0 - %i)", num, count, count - 1);
1813 } else /* access a single register by its name */
1815 reg = register_get_by_name(target->reg_cache, args[0], 1);
1819 command_print(cmd_ctx, "register %s not found in current target", args[0]);
1824 /* display a register */
1825 if ((argc == 1) || ((argc == 2) && !((args[1][0] >= '0') && (args[1][0] <= '9'))))
1827 if ((argc == 2) && (strcmp(args[1], "force") == 0))
1830 if (reg->valid == 0)
1832 reg_arch_type_t *arch_type = register_get_arch_type(reg->arch_type);
1833 arch_type->get(reg);
1835 value = buf_to_str(reg->value, reg->size, 16);
1836 command_print(cmd_ctx, "%s (/%i): 0x%s", reg->name, (int)(reg->size), value);
1841 /* set register value */
1844 uint8_t *buf = malloc(CEIL(reg->size, 8));
1845 str_to_buf(args[1], strlen(args[1]), buf, reg->size, 0);
1847 reg_arch_type_t *arch_type = register_get_arch_type(reg->arch_type);
1848 arch_type->set(reg, buf);
1850 value = buf_to_str(reg->value, reg->size, 16);
1851 command_print(cmd_ctx, "%s (/%i): 0x%s", reg->name, (int)(reg->size), value);
1859 command_print(cmd_ctx, "usage: reg <#|name> [value]");
1864 static int handle_poll_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1866 int retval = ERROR_OK;
1867 target_t *target = get_current_target(cmd_ctx);
1871 command_print(cmd_ctx, "background polling: %s",
1872 target_continuous_poll ? "on" : "off");
1873 command_print(cmd_ctx, "TAP: %s (%s)",
1874 target->tap->dotted_name,
1875 target->tap->enabled ? "enabled" : "disabled");
1876 if (!target->tap->enabled)
1878 if ((retval = target_poll(target)) != ERROR_OK)
1880 if ((retval = target_arch_state(target)) != ERROR_OK)
1886 if (strcmp(args[0], "on") == 0)
1888 target_continuous_poll = 1;
1890 else if (strcmp(args[0], "off") == 0)
1892 target_continuous_poll = 0;
1896 command_print(cmd_ctx, "arg is \"on\" or \"off\"");
1900 return ERROR_COMMAND_SYNTAX_ERROR;
1906 static int handle_wait_halt_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1909 return ERROR_COMMAND_SYNTAX_ERROR;
1914 int retval = parse_uint(args[0], &ms);
1915 if (ERROR_OK != retval)
1917 command_print(cmd_ctx, "usage: %s [seconds]", cmd);
1918 return ERROR_COMMAND_SYNTAX_ERROR;
1920 // convert seconds (given) to milliseconds (needed)
1924 target_t *target = get_current_target(cmd_ctx);
1925 return target_wait_state(target, TARGET_HALTED, ms);
1928 /* wait for target state to change. The trick here is to have a low
1929 * latency for short waits and not to suck up all the CPU time
1932 * After 500ms, keep_alive() is invoked
1934 int target_wait_state(target_t *target, enum target_state state, int ms)
1937 long long then = 0, cur;
1942 if ((retval = target_poll(target)) != ERROR_OK)
1944 if (target->state == state)
1952 then = timeval_ms();
1953 LOG_DEBUG("waiting for target %s...",
1954 Jim_Nvp_value2name_simple(nvp_target_state,state)->name);
1962 if ((cur-then) > ms)
1964 LOG_ERROR("timed out while waiting for target %s",
1965 Jim_Nvp_value2name_simple(nvp_target_state,state)->name);
1973 static int handle_halt_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1977 target_t *target = get_current_target(cmd_ctx);
1978 int retval = target_halt(target);
1979 if (ERROR_OK != retval)
1985 retval = parse_uint(args[0], &wait);
1986 if (ERROR_OK != retval)
1987 return ERROR_COMMAND_SYNTAX_ERROR;
1992 return handle_wait_halt_command(cmd_ctx, cmd, args, argc);
1995 static int handle_soft_reset_halt_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1997 target_t *target = get_current_target(cmd_ctx);
1999 LOG_USER("requesting target halt and executing a soft reset");
2001 target->type->soft_reset_halt(target);
2006 static int handle_reset_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2009 return ERROR_COMMAND_SYNTAX_ERROR;
2011 enum target_reset_mode reset_mode = RESET_RUN;
2015 n = Jim_Nvp_name2value_simple(nvp_reset_modes, args[0]);
2016 if ((n->name == NULL) || (n->value == RESET_UNKNOWN)) {
2017 return ERROR_COMMAND_SYNTAX_ERROR;
2019 reset_mode = n->value;
2022 /* reset *all* targets */
2023 return target_process_reset(cmd_ctx, reset_mode);
2027 static int handle_resume_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2031 return ERROR_COMMAND_SYNTAX_ERROR;
2033 target_t *target = get_current_target(cmd_ctx);
2034 target_handle_event(target, TARGET_EVENT_OLD_pre_resume);
2036 /* with no args, resume from current pc, addr = 0,
2037 * with one arguments, addr = args[0],
2038 * handle breakpoints, not debugging */
2042 int retval = parse_u32(args[0], &addr);
2043 if (ERROR_OK != retval)
2048 return target_resume(target, current, addr, 1, 0);
2051 static int handle_step_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2054 return ERROR_COMMAND_SYNTAX_ERROR;
2058 /* with no args, step from current pc, addr = 0,
2059 * with one argument addr = args[0],
2060 * handle breakpoints, debugging */
2065 int retval = parse_u32(args[0], &addr);
2066 if (ERROR_OK != retval)
2071 target_t *target = get_current_target(cmd_ctx);
2073 return target->type->step(target, current_pc, addr, 1);
2076 static void handle_md_output(struct command_context_s *cmd_ctx,
2077 struct target_s *target, uint32_t address, unsigned size,
2078 unsigned count, const uint8_t *buffer)
2080 const unsigned line_bytecnt = 32;
2081 unsigned line_modulo = line_bytecnt / size;
2083 char output[line_bytecnt * 4 + 1];
2084 unsigned output_len = 0;
2086 const char *value_fmt;
2088 case 4: value_fmt = "%8.8x "; break;
2089 case 2: value_fmt = "%4.2x "; break;
2090 case 1: value_fmt = "%2.2x "; break;
2092 LOG_ERROR("invalid memory read size: %u", size);
2096 for (unsigned i = 0; i < count; i++)
2098 if (i % line_modulo == 0)
2100 output_len += snprintf(output + output_len,
2101 sizeof(output) - output_len,
2103 (unsigned)(address + (i*size)));
2107 const uint8_t *value_ptr = buffer + i * size;
2109 case 4: value = target_buffer_get_u32(target, value_ptr); break;
2110 case 2: value = target_buffer_get_u16(target, value_ptr); break;
2111 case 1: value = *value_ptr;
2113 output_len += snprintf(output + output_len,
2114 sizeof(output) - output_len,
2117 if ((i % line_modulo == line_modulo - 1) || (i == count - 1))
2119 command_print(cmd_ctx, "%s", output);
2125 static int handle_md_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2128 return ERROR_COMMAND_SYNTAX_ERROR;
2132 case 'w': size = 4; break;
2133 case 'h': size = 2; break;
2134 case 'b': size = 1; break;
2135 default: return ERROR_COMMAND_SYNTAX_ERROR;
2139 int retval = parse_u32(args[0], &address);
2140 if (ERROR_OK != retval)
2146 retval = parse_uint(args[1], &count);
2147 if (ERROR_OK != retval)
2151 uint8_t *buffer = calloc(count, size);
2153 target_t *target = get_current_target(cmd_ctx);
2154 retval = target_read_memory(target,
2155 address, size, count, buffer);
2156 if (ERROR_OK == retval)
2157 handle_md_output(cmd_ctx, target, address, size, count, buffer);
2164 static int handle_mw_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2166 if ((argc < 2) || (argc > 3))
2167 return ERROR_COMMAND_SYNTAX_ERROR;
2170 int retval = parse_u32(args[0], &address);
2171 if (ERROR_OK != retval)
2175 retval = parse_u32(args[1], &value);
2176 if (ERROR_OK != retval)
2182 retval = parse_uint(args[2], &count);
2183 if (ERROR_OK != retval)
2187 target_t *target = get_current_target(cmd_ctx);
2189 uint8_t value_buf[4];
2194 target_buffer_set_u32(target, value_buf, value);
2198 target_buffer_set_u16(target, value_buf, value);
2202 value_buf[0] = value;
2205 return ERROR_COMMAND_SYNTAX_ERROR;
2207 for (unsigned i = 0; i < count; i++)
2209 retval = target_write_memory(target,
2210 address + i * wordsize, wordsize, 1, value_buf);
2211 if (ERROR_OK != retval)
2220 static int parse_load_image_command_args(char **args, int argc,
2221 image_t *image, uint32_t *min_address, uint32_t *max_address)
2223 if (argc < 1 || argc > 5)
2224 return ERROR_COMMAND_SYNTAX_ERROR;
2226 /* a base address isn't always necessary,
2227 * default to 0x0 (i.e. don't relocate) */
2231 int retval = parse_u32(args[1], &addr);
2232 if (ERROR_OK != retval)
2233 return ERROR_COMMAND_SYNTAX_ERROR;
2234 image->base_address = addr;
2235 image->base_address_set = 1;
2238 image->base_address_set = 0;
2240 image->start_address_set = 0;
2244 int retval = parse_u32(args[3], min_address);
2245 if (ERROR_OK != retval)
2246 return ERROR_COMMAND_SYNTAX_ERROR;
2250 int retval = parse_u32(args[4], max_address);
2251 if (ERROR_OK != retval)
2252 return ERROR_COMMAND_SYNTAX_ERROR;
2253 // use size (given) to find max (required)
2254 *max_address += *min_address;
2257 if (*min_address > *max_address)
2258 return ERROR_COMMAND_SYNTAX_ERROR;
2263 static int handle_load_image_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2267 uint32_t image_size;
2268 uint32_t min_address = 0;
2269 uint32_t max_address = 0xffffffff;
2275 duration_t duration;
2276 char *duration_text;
2278 int retval = parse_load_image_command_args(args, argc,
2279 &image, &min_address, &max_address);
2280 if (ERROR_OK != retval)
2283 target_t *target = get_current_target(cmd_ctx);
2284 duration_start_measure(&duration);
2286 if (image_open(&image, args[0], (argc >= 3) ? args[2] : NULL) != ERROR_OK)
2293 for (i = 0; i < image.num_sections; i++)
2295 buffer = malloc(image.sections[i].size);
2298 command_print(cmd_ctx,
2299 "error allocating buffer for section (%d bytes)",
2300 (int)(image.sections[i].size));
2304 if ((retval = image_read_section(&image, i, 0x0, image.sections[i].size, buffer, &buf_cnt)) != ERROR_OK)
2310 uint32_t offset = 0;
2311 uint32_t length = buf_cnt;
2313 /* DANGER!!! beware of unsigned comparision here!!! */
2315 if ((image.sections[i].base_address + buf_cnt >= min_address)&&
2316 (image.sections[i].base_address < max_address))
2318 if (image.sections[i].base_address < min_address)
2320 /* clip addresses below */
2321 offset += min_address-image.sections[i].base_address;
2325 if (image.sections[i].base_address + buf_cnt > max_address)
2327 length -= (image.sections[i].base_address + buf_cnt)-max_address;
2330 if ((retval = target_write_buffer(target, image.sections[i].base_address + offset, length, buffer + offset)) != ERROR_OK)
2335 image_size += length;
2336 command_print(cmd_ctx, "%u byte written at address 0x%8.8" PRIx32 "",
2337 (unsigned int)length,
2338 image.sections[i].base_address + offset);
2344 if ((retvaltemp = duration_stop_measure(&duration, &duration_text)) != ERROR_OK)
2346 image_close(&image);
2350 if (retval == ERROR_OK)
2352 command_print(cmd_ctx, "downloaded %u byte in %s",
2353 (unsigned int)image_size,
2356 free(duration_text);
2358 image_close(&image);
2364 static int handle_dump_image_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2368 uint8_t buffer[560];
2371 duration_t duration;
2372 char *duration_text;
2374 target_t *target = get_current_target(cmd_ctx);
2378 command_print(cmd_ctx, "usage: dump_image <filename> <address> <size>");
2383 int retval = parse_u32(args[1], &address);
2384 if (ERROR_OK != retval)
2388 retval = parse_u32(args[2], &size);
2389 if (ERROR_OK != retval)
2392 if (fileio_open(&fileio, args[0], FILEIO_WRITE, FILEIO_BINARY) != ERROR_OK)
2397 duration_start_measure(&duration);
2401 uint32_t size_written;
2402 uint32_t this_run_size = (size > 560) ? 560 : size;
2404 retval = target_read_buffer(target, address, this_run_size, buffer);
2405 if (retval != ERROR_OK)
2410 retval = fileio_write(&fileio, this_run_size, buffer, &size_written);
2411 if (retval != ERROR_OK)
2416 size -= this_run_size;
2417 address += this_run_size;
2420 if ((retvaltemp = fileio_close(&fileio)) != ERROR_OK)
2423 if ((retvaltemp = duration_stop_measure(&duration, &duration_text)) != ERROR_OK)
2426 if (retval == ERROR_OK)
2428 command_print(cmd_ctx, "dumped %lld byte in %s",
2429 fileio.size, duration_text);
2430 free(duration_text);
2436 static int handle_verify_image_command_internal(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc, int verify)
2440 uint32_t image_size;
2442 int retval, retvaltemp;
2443 uint32_t checksum = 0;
2444 uint32_t mem_checksum = 0;
2448 duration_t duration;
2449 char *duration_text;
2451 target_t *target = get_current_target(cmd_ctx);
2455 return ERROR_COMMAND_SYNTAX_ERROR;
2460 LOG_ERROR("no target selected");
2464 duration_start_measure(&duration);
2469 retval = parse_u32(args[1], &addr);
2470 if (ERROR_OK != retval)
2471 return ERROR_COMMAND_SYNTAX_ERROR;
2472 image.base_address = addr;
2473 image.base_address_set = 1;
2477 image.base_address_set = 0;
2478 image.base_address = 0x0;
2481 image.start_address_set = 0;
2483 if ((retval = image_open(&image, args[0], (argc == 3) ? args[2] : NULL)) != ERROR_OK)
2490 for (i = 0; i < image.num_sections; i++)
2492 buffer = malloc(image.sections[i].size);
2495 command_print(cmd_ctx,
2496 "error allocating buffer for section (%d bytes)",
2497 (int)(image.sections[i].size));
2500 if ((retval = image_read_section(&image, i, 0x0, image.sections[i].size, buffer, &buf_cnt)) != ERROR_OK)
2508 /* calculate checksum of image */
2509 image_calculate_checksum(buffer, buf_cnt, &checksum);
2511 retval = target_checksum_memory(target, image.sections[i].base_address, buf_cnt, &mem_checksum);
2512 if (retval != ERROR_OK)
2518 if (checksum != mem_checksum)
2520 /* failed crc checksum, fall back to a binary compare */
2523 command_print(cmd_ctx, "checksum mismatch - attempting binary compare");
2525 data = (uint8_t*)malloc(buf_cnt);
2527 /* Can we use 32bit word accesses? */
2529 int count = buf_cnt;
2530 if ((count % 4) == 0)
2535 retval = target_read_memory(target, image.sections[i].base_address, size, count, data);
2536 if (retval == ERROR_OK)
2539 for (t = 0; t < buf_cnt; t++)
2541 if (data[t] != buffer[t])
2543 command_print(cmd_ctx,
2544 "Verify operation failed address 0x%08x. Was 0x%02x instead of 0x%02x\n",
2545 (unsigned)(t + image.sections[i].base_address),
2550 retval = ERROR_FAIL;
2564 command_print(cmd_ctx, "address 0x%08" PRIx32 " length 0x%08" PRIx32 "",
2565 image.sections[i].base_address,
2570 image_size += buf_cnt;
2574 if ((retvaltemp = duration_stop_measure(&duration, &duration_text)) != ERROR_OK)
2576 image_close(&image);
2580 if (retval == ERROR_OK)
2582 command_print(cmd_ctx, "verified %u bytes in %s",
2583 (unsigned int)image_size,
2586 free(duration_text);
2588 image_close(&image);
2593 static int handle_verify_image_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2595 return handle_verify_image_command_internal(cmd_ctx, cmd, args, argc, 1);
2598 static int handle_test_image_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2600 return handle_verify_image_command_internal(cmd_ctx, cmd, args, argc, 0);
2603 static int handle_bp_command_list(struct command_context_s *cmd_ctx)
2605 target_t *target = get_current_target(cmd_ctx);
2606 breakpoint_t *breakpoint = target->breakpoints;
2609 if (breakpoint->type == BKPT_SOFT)
2611 char* buf = buf_to_str(breakpoint->orig_instr,
2612 breakpoint->length, 16);
2613 command_print(cmd_ctx, "0x%8.8" PRIx32 ", 0x%x, %i, 0x%s",
2614 breakpoint->address,
2616 breakpoint->set, buf);
2621 command_print(cmd_ctx, "0x%8.8" PRIx32 ", 0x%x, %i",
2622 breakpoint->address,
2623 breakpoint->length, breakpoint->set);
2626 breakpoint = breakpoint->next;
2631 static int handle_bp_command_set(struct command_context_s *cmd_ctx,
2632 uint32_t addr, uint32_t length, int hw)
2634 target_t *target = get_current_target(cmd_ctx);
2635 int retval = breakpoint_add(target, addr, length, hw);
2636 if (ERROR_OK == retval)
2637 command_print(cmd_ctx, "breakpoint set at 0x%8.8" PRIx32 "", addr);
2639 LOG_ERROR("Failure setting breakpoint");
2643 static int handle_bp_command(struct command_context_s *cmd_ctx,
2644 char *cmd, char **args, int argc)
2647 return handle_bp_command_list(cmd_ctx);
2649 if (argc < 2 || argc > 3)
2651 command_print(cmd_ctx, "usage: bp <address> <length> ['hw']");
2652 return ERROR_COMMAND_SYNTAX_ERROR;
2656 int retval = parse_u32(args[0], &addr);
2657 if (ERROR_OK != retval)
2661 retval = parse_u32(args[1], &length);
2662 if (ERROR_OK != retval)
2668 if (strcmp(args[2], "hw") == 0)
2671 return ERROR_COMMAND_SYNTAX_ERROR;
2674 return handle_bp_command_set(cmd_ctx, addr, length, hw);
2677 static int handle_rbp_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2680 return ERROR_COMMAND_SYNTAX_ERROR;
2683 int retval = parse_u32(args[0], &addr);
2684 if (ERROR_OK != retval)
2687 target_t *target = get_current_target(cmd_ctx);
2688 breakpoint_remove(target, addr);
2693 static int handle_wp_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2695 target_t *target = get_current_target(cmd_ctx);
2699 watchpoint_t *watchpoint = target->watchpoints;
2703 command_print(cmd_ctx,
2704 "address: 0x%8.8" PRIx32 ", len: 0x%8.8x, r/w/a: %i, value: 0x%8.8" PRIx32 ", mask: 0x%8.8" PRIx32 "",
2705 watchpoint->address,
2707 (int)(watchpoint->rw),
2710 watchpoint = watchpoint->next;
2715 enum watchpoint_rw type = WPT_ACCESS;
2717 uint32_t length = 0;
2718 uint32_t data_value = 0x0;
2719 uint32_t data_mask = 0xffffffff;
2725 retval = parse_u32(args[4], &data_mask);
2726 if (ERROR_OK != retval)
2730 retval = parse_u32(args[3], &data_value);
2731 if (ERROR_OK != retval)
2747 LOG_ERROR("invalid watchpoint mode ('%c')", args[2][0]);
2748 return ERROR_COMMAND_SYNTAX_ERROR;
2752 retval = parse_u32(args[1], &length);
2753 if (ERROR_OK != retval)
2755 retval = parse_u32(args[0], &addr);
2756 if (ERROR_OK != retval)
2761 command_print(cmd_ctx, "usage: wp <address> <length> [r/w/a] [value] [mask]");
2762 return ERROR_COMMAND_SYNTAX_ERROR;
2765 retval = watchpoint_add(target, addr, length, type,
2766 data_value, data_mask);
2767 if (ERROR_OK != retval)
2768 LOG_ERROR("Failure setting watchpoints");
2773 static int handle_rwp_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2776 return ERROR_COMMAND_SYNTAX_ERROR;
2779 int retval = parse_u32(args[0], &addr);
2780 if (ERROR_OK != retval)
2783 target_t *target = get_current_target(cmd_ctx);
2784 watchpoint_remove(target, addr);
2791 * Translate a virtual address to a physical address.
2793 * The low-level target implementation must have logged a detailed error
2794 * which is forwarded to telnet/GDB session.
2796 static int handle_virt2phys_command(command_context_t *cmd_ctx,
2797 char *cmd, char **args, int argc)
2800 return ERROR_COMMAND_SYNTAX_ERROR;
2803 int retval = parse_u32(args[0], &va);
2804 if (ERROR_OK != retval)
2808 target_t *target = get_current_target(cmd_ctx);
2809 retval = target->type->virt2phys(target, va, &pa);
2810 if (retval == ERROR_OK)
2811 command_print(cmd_ctx, "Physical address 0x%08" PRIx32 "", pa);
2816 static void writeData(FILE *f, const void *data, size_t len)
2818 size_t written = fwrite(data, 1, len, f);
2820 LOG_ERROR("failed to write %zu bytes: %s", len, strerror(errno));
2823 static void writeLong(FILE *f, int l)
2826 for (i = 0; i < 4; i++)
2828 char c = (l >> (i*8))&0xff;
2829 writeData(f, &c, 1);
2834 static void writeString(FILE *f, char *s)
2836 writeData(f, s, strlen(s));
2839 /* Dump a gmon.out histogram file. */
2840 static void writeGmon(uint32_t *samples, uint32_t sampleNum, char *filename)
2843 FILE *f = fopen(filename, "w");
2846 writeString(f, "gmon");
2847 writeLong(f, 0x00000001); /* Version */
2848 writeLong(f, 0); /* padding */
2849 writeLong(f, 0); /* padding */
2850 writeLong(f, 0); /* padding */
2852 uint8_t zero = 0; /* GMON_TAG_TIME_HIST */
2853 writeData(f, &zero, 1);
2855 /* figure out bucket size */
2856 uint32_t min = samples[0];
2857 uint32_t max = samples[0];
2858 for (i = 0; i < sampleNum; i++)
2860 if (min > samples[i])
2864 if (max < samples[i])
2870 int addressSpace = (max-min + 1);
2872 static const uint32_t maxBuckets = 256 * 1024; /* maximum buckets. */
2873 uint32_t length = addressSpace;
2874 if (length > maxBuckets)
2876 length = maxBuckets;
2878 int *buckets = malloc(sizeof(int)*length);
2879 if (buckets == NULL)
2884 memset(buckets, 0, sizeof(int)*length);
2885 for (i = 0; i < sampleNum;i++)
2887 uint32_t address = samples[i];
2888 long long a = address-min;
2889 long long b = length-1;
2890 long long c = addressSpace-1;
2891 int index = (a*b)/c; /* danger!!!! int32 overflows */
2895 /* append binary memory gmon.out &profile_hist_hdr ((char*)&profile_hist_hdr + sizeof(struct gmon_hist_hdr)) */
2896 writeLong(f, min); /* low_pc */
2897 writeLong(f, max); /* high_pc */
2898 writeLong(f, length); /* # of samples */
2899 writeLong(f, 64000000); /* 64MHz */
2900 writeString(f, "seconds");
2901 for (i = 0; i < (15-strlen("seconds")); i++)
2902 writeData(f, &zero, 1);
2903 writeString(f, "s");
2905 /*append binary memory gmon.out profile_hist_data (profile_hist_data + profile_hist_hdr.hist_size) */
2907 char *data = malloc(2*length);
2910 for (i = 0; i < length;i++)
2919 data[i*2 + 1]=(val >> 8)&0xff;
2922 writeData(f, data, length * 2);
2932 /* profiling samples the CPU PC as quickly as OpenOCD is able, which will be used as a random sampling of PC */
2933 static int handle_profile_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2935 target_t *target = get_current_target(cmd_ctx);
2936 struct timeval timeout, now;
2938 gettimeofday(&timeout, NULL);
2941 return ERROR_COMMAND_SYNTAX_ERROR;
2944 int retval = parse_uint(args[0], &offset);
2945 if (ERROR_OK != retval)
2948 timeval_add_time(&timeout, offset, 0);
2950 command_print(cmd_ctx, "Starting profiling. Halting and resuming the target as often as we can...");
2952 static const int maxSample = 10000;
2953 uint32_t *samples = malloc(sizeof(uint32_t)*maxSample);
2954 if (samples == NULL)
2958 /* hopefully it is safe to cache! We want to stop/restart as quickly as possible. */
2959 reg_t *reg = register_get_by_name(target->reg_cache, "pc", 1);
2963 target_poll(target);
2964 if (target->state == TARGET_HALTED)
2966 uint32_t t=*((uint32_t *)reg->value);
2967 samples[numSamples++]=t;
2968 retval = target_resume(target, 1, 0, 0, 0); /* current pc, addr = 0, do not handle breakpoints, not debugging */
2969 target_poll(target);
2970 alive_sleep(10); /* sleep 10ms, i.e. <100 samples/second. */
2971 } else if (target->state == TARGET_RUNNING)
2973 /* We want to quickly sample the PC. */
2974 if ((retval = target_halt(target)) != ERROR_OK)
2981 command_print(cmd_ctx, "Target not halted or running");
2985 if (retval != ERROR_OK)
2990 gettimeofday(&now, NULL);
2991 if ((numSamples >= maxSample) || ((now.tv_sec >= timeout.tv_sec) && (now.tv_usec >= timeout.tv_usec)))
2993 command_print(cmd_ctx, "Profiling completed. %d samples.", numSamples);
2994 if ((retval = target_poll(target)) != ERROR_OK)
2999 if (target->state == TARGET_HALTED)
3001 target_resume(target, 1, 0, 0, 0); /* current pc, addr = 0, do not handle breakpoints, not debugging */
3003 if ((retval = target_poll(target)) != ERROR_OK)
3008 writeGmon(samples, numSamples, args[1]);
3009 command_print(cmd_ctx, "Wrote %s", args[1]);
3018 static int new_int_array_element(Jim_Interp * interp, const char *varname, int idx, uint32_t val)
3021 Jim_Obj *nameObjPtr, *valObjPtr;
3024 namebuf = alloc_printf("%s(%d)", varname, idx);
3028 nameObjPtr = Jim_NewStringObj(interp, namebuf, -1);
3029 valObjPtr = Jim_NewIntObj(interp, val);
3030 if (!nameObjPtr || !valObjPtr)
3036 Jim_IncrRefCount(nameObjPtr);
3037 Jim_IncrRefCount(valObjPtr);
3038 result = Jim_SetVariable(interp, nameObjPtr, valObjPtr);
3039 Jim_DecrRefCount(interp, nameObjPtr);
3040 Jim_DecrRefCount(interp, valObjPtr);
3042 /* printf("%s(%d) <= 0%08x\n", varname, idx, val); */
3046 static int jim_mem2array(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
3048 command_context_t *context;
3051 context = Jim_GetAssocData(interp, "context");
3052 if (context == NULL)
3054 LOG_ERROR("mem2array: no command context");
3057 target = get_current_target(context);
3060 LOG_ERROR("mem2array: no current target");
3064 return target_mem2array(interp, target, argc-1, argv + 1);
3067 static int target_mem2array(Jim_Interp *interp, target_t *target, int argc, Jim_Obj *const *argv)
3075 const char *varname;
3076 uint8_t buffer[4096];
3080 /* argv[1] = name of array to receive the data
3081 * argv[2] = desired width
3082 * argv[3] = memory address
3083 * argv[4] = count of times to read
3086 Jim_WrongNumArgs(interp, 1, argv, "varname width addr nelems");
3089 varname = Jim_GetString(argv[0], &len);
3090 /* given "foo" get space for worse case "foo(%d)" .. add 20 */
3092 e = Jim_GetLong(interp, argv[1], &l);
3098 e = Jim_GetLong(interp, argv[2], &l);
3103 e = Jim_GetLong(interp, argv[3], &l);
3119 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3120 Jim_AppendStrings(interp, Jim_GetResult(interp), "Invalid width param, must be 8/16/32", NULL);
3124 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3125 Jim_AppendStrings(interp, Jim_GetResult(interp), "mem2array: zero width read?", NULL);
3128 if ((addr + (len * width)) < addr) {
3129 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3130 Jim_AppendStrings(interp, Jim_GetResult(interp), "mem2array: addr + len - wraps to zero?", NULL);
3133 /* absurd transfer size? */
3135 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3136 Jim_AppendStrings(interp, Jim_GetResult(interp), "mem2array: absurd > 64K item request", NULL);
3141 ((width == 2) && ((addr & 1) == 0)) ||
3142 ((width == 4) && ((addr & 3) == 0))) {
3146 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3147 sprintf(buf, "mem2array address: 0x%08" PRIx32 " is not aligned for %" PRId32 " byte reads",
3150 Jim_AppendStrings(interp, Jim_GetResult(interp), buf , NULL);
3161 /* Slurp... in buffer size chunks */
3163 count = len; /* in objects.. */
3164 if (count > (sizeof(buffer)/width)) {
3165 count = (sizeof(buffer)/width);
3168 retval = target_read_memory(target, addr, width, count, buffer);
3169 if (retval != ERROR_OK) {
3171 LOG_ERROR("mem2array: Read @ 0x%08x, w=%d, cnt=%d, failed",
3175 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3176 Jim_AppendStrings(interp, Jim_GetResult(interp), "mem2array: cannot read memory", NULL);
3180 v = 0; /* shut up gcc */
3181 for (i = 0 ;i < count ;i++, n++) {
3184 v = target_buffer_get_u32(target, &buffer[i*width]);
3187 v = target_buffer_get_u16(target, &buffer[i*width]);
3190 v = buffer[i] & 0x0ff;
3193 new_int_array_element(interp, varname, n, v);
3199 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3204 static int get_int_array_element(Jim_Interp * interp, const char *varname, int idx, uint32_t *val)
3207 Jim_Obj *nameObjPtr, *valObjPtr;
3211 namebuf = alloc_printf("%s(%d)", varname, idx);
3215 nameObjPtr = Jim_NewStringObj(interp, namebuf, -1);
3222 Jim_IncrRefCount(nameObjPtr);
3223 valObjPtr = Jim_GetVariable(interp, nameObjPtr, JIM_ERRMSG);
3224 Jim_DecrRefCount(interp, nameObjPtr);
3226 if (valObjPtr == NULL)
3229 result = Jim_GetLong(interp, valObjPtr, &l);
3230 /* printf("%s(%d) => 0%08x\n", varname, idx, val); */
3235 static int jim_array2mem(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
3237 command_context_t *context;
3240 context = Jim_GetAssocData(interp, "context");
3241 if (context == NULL) {
3242 LOG_ERROR("array2mem: no command context");
3245 target = get_current_target(context);
3246 if (target == NULL) {
3247 LOG_ERROR("array2mem: no current target");
3251 return target_array2mem(interp,target, argc-1, argv + 1);
3254 static int target_array2mem(Jim_Interp *interp, target_t *target, int argc, Jim_Obj *const *argv)
3262 const char *varname;
3263 uint8_t buffer[4096];
3267 /* argv[1] = name of array to get the data
3268 * argv[2] = desired width
3269 * argv[3] = memory address
3270 * argv[4] = count to write
3273 Jim_WrongNumArgs(interp, 1, argv, "varname width addr nelems");
3276 varname = Jim_GetString(argv[0], &len);
3277 /* given "foo" get space for worse case "foo(%d)" .. add 20 */
3279 e = Jim_GetLong(interp, argv[1], &l);
3285 e = Jim_GetLong(interp, argv[2], &l);
3290 e = Jim_GetLong(interp, argv[3], &l);
3306 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3307 Jim_AppendStrings(interp, Jim_GetResult(interp), "Invalid width param, must be 8/16/32", NULL);
3311 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3312 Jim_AppendStrings(interp, Jim_GetResult(interp), "array2mem: zero width read?", NULL);
3315 if ((addr + (len * width)) < addr) {
3316 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3317 Jim_AppendStrings(interp, Jim_GetResult(interp), "array2mem: addr + len - wraps to zero?", NULL);
3320 /* absurd transfer size? */
3322 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3323 Jim_AppendStrings(interp, Jim_GetResult(interp), "array2mem: absurd > 64K item request", NULL);
3328 ((width == 2) && ((addr & 1) == 0)) ||
3329 ((width == 4) && ((addr & 3) == 0))) {
3333 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3334 sprintf(buf, "array2mem address: 0x%08x is not aligned for %d byte reads",
3337 Jim_AppendStrings(interp, Jim_GetResult(interp), buf , NULL);
3348 /* Slurp... in buffer size chunks */
3350 count = len; /* in objects.. */
3351 if (count > (sizeof(buffer)/width)) {
3352 count = (sizeof(buffer)/width);
3355 v = 0; /* shut up gcc */
3356 for (i = 0 ;i < count ;i++, n++) {
3357 get_int_array_element(interp, varname, n, &v);
3360 target_buffer_set_u32(target, &buffer[i*width], v);
3363 target_buffer_set_u16(target, &buffer[i*width], v);
3366 buffer[i] = v & 0x0ff;
3372 retval = target_write_memory(target, addr, width, count, buffer);
3373 if (retval != ERROR_OK) {
3375 LOG_ERROR("array2mem: Write @ 0x%08x, w=%d, cnt=%d, failed",
3379 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3380 Jim_AppendStrings(interp, Jim_GetResult(interp), "array2mem: cannot read memory", NULL);
3386 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3391 void target_all_handle_event(enum target_event e)
3395 LOG_DEBUG("**all*targets: event: %d, %s",
3397 Jim_Nvp_value2name_simple(nvp_target_event, e)->name);
3399 target = all_targets;
3401 target_handle_event(target, e);
3402 target = target->next;
3406 void target_handle_event(target_t *target, enum target_event e)
3408 target_event_action_t *teap;
3411 teap = target->event_action;
3415 if (teap->event == e) {
3417 LOG_DEBUG("target: (%d) %s (%s) event: %d (%s) action: %s",
3418 target->target_number,
3420 target_get_name(target),
3422 Jim_Nvp_value2name_simple(nvp_target_event, e)->name,
3423 Jim_GetString(teap->body, NULL));
3424 if (Jim_EvalObj(interp, teap->body) != JIM_OK)
3426 Jim_PrintErrorMessage(interp);
3432 LOG_DEBUG("event: %d %s - no action",
3434 Jim_Nvp_value2name_simple(nvp_target_event, e)->name);
3438 enum target_cfg_param {
3441 TCFG_WORK_AREA_VIRT,
3442 TCFG_WORK_AREA_PHYS,
3443 TCFG_WORK_AREA_SIZE,
3444 TCFG_WORK_AREA_BACKUP,
3447 TCFG_CHAIN_POSITION,
3450 static Jim_Nvp nvp_config_opts[] = {
3451 { .name = "-type", .value = TCFG_TYPE },
3452 { .name = "-event", .value = TCFG_EVENT },
3453 { .name = "-work-area-virt", .value = TCFG_WORK_AREA_VIRT },
3454 { .name = "-work-area-phys", .value = TCFG_WORK_AREA_PHYS },
3455 { .name = "-work-area-size", .value = TCFG_WORK_AREA_SIZE },
3456 { .name = "-work-area-backup", .value = TCFG_WORK_AREA_BACKUP },
3457 { .name = "-endian" , .value = TCFG_ENDIAN },
3458 { .name = "-variant", .value = TCFG_VARIANT },
3459 { .name = "-chain-position", .value = TCFG_CHAIN_POSITION },
3461 { .name = NULL, .value = -1 }
3464 static int target_configure(Jim_GetOptInfo *goi, target_t *target)
3472 /* parse config or cget options ... */
3473 while (goi->argc > 0) {
3474 Jim_SetEmptyResult(goi->interp);
3475 /* Jim_GetOpt_Debug(goi); */
3477 if (target->type->target_jim_configure) {
3478 /* target defines a configure function */
3479 /* target gets first dibs on parameters */
3480 e = (*(target->type->target_jim_configure))(target, goi);
3489 /* otherwise we 'continue' below */
3491 e = Jim_GetOpt_Nvp(goi, nvp_config_opts, &n);
3493 Jim_GetOpt_NvpUnknown(goi, nvp_config_opts, 0);
3499 if (goi->isconfigure) {
3500 Jim_SetResult_sprintf(goi->interp, "not setable: %s", n->name);
3504 if (goi->argc != 0) {
3505 Jim_WrongNumArgs(goi->interp, goi->argc, goi->argv, "NO PARAMS");
3509 Jim_SetResultString(goi->interp, target_get_name(target), -1);
3513 if (goi->argc == 0) {
3514 Jim_WrongNumArgs(goi->interp, goi->argc, goi->argv, "-event ?event-name? ...");
3518 e = Jim_GetOpt_Nvp(goi, nvp_target_event, &n);
3520 Jim_GetOpt_NvpUnknown(goi, nvp_target_event, 1);
3524 if (goi->isconfigure) {
3525 if (goi->argc != 1) {
3526 Jim_WrongNumArgs(goi->interp, goi->argc, goi->argv, "-event ?event-name? ?EVENT-BODY?");
3530 if (goi->argc != 0) {
3531 Jim_WrongNumArgs(goi->interp, goi->argc, goi->argv, "-event ?event-name?");
3537 target_event_action_t *teap;
3539 teap = target->event_action;
3540 /* replace existing? */
3542 if (teap->event == (enum target_event)n->value) {
3548 if (goi->isconfigure) {
3551 teap = calloc(1, sizeof(*teap));
3553 teap->event = n->value;
3554 Jim_GetOpt_Obj(goi, &o);
3556 Jim_DecrRefCount(interp, teap->body);
3558 teap->body = Jim_DuplicateObj(goi->interp, o);
3561 * Tcl/TK - "tk events" have a nice feature.
3562 * See the "BIND" command.
3563 * We should support that here.
3564 * You can specify %X and %Y in the event code.
3565 * The idea is: %T - target name.
3566 * The idea is: %N - target number
3567 * The idea is: %E - event name.
3569 Jim_IncrRefCount(teap->body);
3571 /* add to head of event list */
3572 teap->next = target->event_action;
3573 target->event_action = teap;
3574 Jim_SetEmptyResult(goi->interp);
3578 Jim_SetEmptyResult(goi->interp);
3580 Jim_SetResult(goi->interp, Jim_DuplicateObj(goi->interp, teap->body));
3587 case TCFG_WORK_AREA_VIRT:
3588 if (goi->isconfigure) {
3589 target_free_all_working_areas(target);
3590 e = Jim_GetOpt_Wide(goi, &w);
3594 target->working_area_virt = w;
3596 if (goi->argc != 0) {
3600 Jim_SetResult(interp, Jim_NewIntObj(goi->interp, target->working_area_virt));
3604 case TCFG_WORK_AREA_PHYS:
3605 if (goi->isconfigure) {
3606 target_free_all_working_areas(target);
3607 e = Jim_GetOpt_Wide(goi, &w);
3611 target->working_area_phys = w;
3613 if (goi->argc != 0) {
3617 Jim_SetResult(interp, Jim_NewIntObj(goi->interp, target->working_area_phys));
3621 case TCFG_WORK_AREA_SIZE:
3622 if (goi->isconfigure) {
3623 target_free_all_working_areas(target);
3624 e = Jim_GetOpt_Wide(goi, &w);
3628 target->working_area_size = w;
3630 if (goi->argc != 0) {
3634 Jim_SetResult(interp, Jim_NewIntObj(goi->interp, target->working_area_size));
3638 case TCFG_WORK_AREA_BACKUP:
3639 if (goi->isconfigure) {
3640 target_free_all_working_areas(target);
3641 e = Jim_GetOpt_Wide(goi, &w);
3645 /* make this exactly 1 or 0 */
3646 target->backup_working_area = (!!w);
3648 if (goi->argc != 0) {
3652 Jim_SetResult(interp, Jim_NewIntObj(goi->interp, target->backup_working_area));
3653 /* loop for more e*/
3657 if (goi->isconfigure) {
3658 e = Jim_GetOpt_Nvp(goi, nvp_target_endian, &n);
3660 Jim_GetOpt_NvpUnknown(goi, nvp_target_endian, 1);
3663 target->endianness = n->value;
3665 if (goi->argc != 0) {
3669 n = Jim_Nvp_value2name_simple(nvp_target_endian, target->endianness);
3670 if (n->name == NULL) {
3671 target->endianness = TARGET_LITTLE_ENDIAN;
3672 n = Jim_Nvp_value2name_simple(nvp_target_endian, target->endianness);
3674 Jim_SetResultString(goi->interp, n->name, -1);
3679 if (goi->isconfigure) {
3680 if (goi->argc < 1) {
3681 Jim_SetResult_sprintf(goi->interp,
3686 if (target->variant) {
3687 free((void *)(target->variant));
3689 e = Jim_GetOpt_String(goi, &cp, NULL);
3690 target->variant = strdup(cp);
3692 if (goi->argc != 0) {
3696 Jim_SetResultString(goi->interp, target->variant,-1);
3699 case TCFG_CHAIN_POSITION:
3700 if (goi->isconfigure) {
3703 target_free_all_working_areas(target);
3704 e = Jim_GetOpt_Obj(goi, &o);
3708 tap = jtag_tap_by_jim_obj(goi->interp, o);
3712 /* make this exactly 1 or 0 */
3715 if (goi->argc != 0) {
3719 Jim_SetResultString(interp, target->tap->dotted_name, -1);
3720 /* loop for more e*/
3723 } /* while (goi->argc) */
3726 /* done - we return */
3730 /** this is the 'tcl' handler for the target specific command */
3731 static int tcl_target_func(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
3736 uint8_t target_buf[32];
3739 struct command_context_s *cmd_ctx;
3746 TS_CMD_MWW, TS_CMD_MWH, TS_CMD_MWB,
3747 TS_CMD_MDW, TS_CMD_MDH, TS_CMD_MDB,
3748 TS_CMD_MRW, TS_CMD_MRH, TS_CMD_MRB,
3749 TS_CMD_MEM2ARRAY, TS_CMD_ARRAY2MEM,
3757 TS_CMD_INVOKE_EVENT,
3760 static const Jim_Nvp target_options[] = {
3761 { .name = "configure", .value = TS_CMD_CONFIGURE },
3762 { .name = "cget", .value = TS_CMD_CGET },
3763 { .name = "mww", .value = TS_CMD_MWW },
3764 { .name = "mwh", .value = TS_CMD_MWH },
3765 { .name = "mwb", .value = TS_CMD_MWB },
3766 { .name = "mdw", .value = TS_CMD_MDW },
3767 { .name = "mdh", .value = TS_CMD_MDH },
3768 { .name = "mdb", .value = TS_CMD_MDB },
3769 { .name = "mem2array", .value = TS_CMD_MEM2ARRAY },
3770 { .name = "array2mem", .value = TS_CMD_ARRAY2MEM },
3771 { .name = "eventlist", .value = TS_CMD_EVENTLIST },
3772 { .name = "curstate", .value = TS_CMD_CURSTATE },
3774 { .name = "arp_examine", .value = TS_CMD_EXAMINE },
3775 { .name = "arp_poll", .value = TS_CMD_POLL },
3776 { .name = "arp_reset", .value = TS_CMD_RESET },
3777 { .name = "arp_halt", .value = TS_CMD_HALT },
3778 { .name = "arp_waitstate", .value = TS_CMD_WAITSTATE },
3779 { .name = "invoke-event", .value = TS_CMD_INVOKE_EVENT },
3781 { .name = NULL, .value = -1 },
3784 /* go past the "command" */
3785 Jim_GetOpt_Setup(&goi, interp, argc-1, argv + 1);
3787 target = Jim_CmdPrivData(goi.interp);
3788 cmd_ctx = Jim_GetAssocData(goi.interp, "context");
3790 /* commands here are in an NVP table */
3791 e = Jim_GetOpt_Nvp(&goi, target_options, &n);
3793 Jim_GetOpt_NvpUnknown(&goi, target_options, 0);
3796 /* Assume blank result */
3797 Jim_SetEmptyResult(goi.interp);
3800 case TS_CMD_CONFIGURE:
3802 Jim_WrongNumArgs(goi.interp, goi.argc, goi.argv, "missing: -option VALUE ...");
3805 goi.isconfigure = 1;
3806 return target_configure(&goi, target);
3808 // some things take params
3810 Jim_WrongNumArgs(goi.interp, 0, goi.argv, "missing: ?-option?");
3813 goi.isconfigure = 0;
3814 return target_configure(&goi, target);
3822 * argv[3] = optional count.
3825 if ((goi.argc == 2) || (goi.argc == 3)) {
3829 Jim_SetResult_sprintf(goi.interp, "expected: %s ADDR DATA [COUNT]", n->name);
3833 e = Jim_GetOpt_Wide(&goi, &a);
3838 e = Jim_GetOpt_Wide(&goi, &b);
3842 if (goi.argc == 3) {
3843 e = Jim_GetOpt_Wide(&goi, &c);
3853 target_buffer_set_u32(target, target_buf, b);
3857 target_buffer_set_u16(target, target_buf, b);
3861 target_buffer_set_u8(target, target_buf, b);
3865 for (x = 0 ; x < c ; x++) {
3866 e = target_write_memory(target, a, b, 1, target_buf);
3867 if (e != ERROR_OK) {
3868 Jim_SetResult_sprintf(interp, "Error writing @ 0x%08x: %d\n", (int)(a), e);
3881 /* argv[0] = command
3883 * argv[2] = optional count
3885 if ((goi.argc == 2) || (goi.argc == 3)) {
3886 Jim_SetResult_sprintf(goi.interp, "expected: %s ADDR [COUNT]", n->name);
3889 e = Jim_GetOpt_Wide(&goi, &a);
3894 e = Jim_GetOpt_Wide(&goi, &c);
3901 b = 1; /* shut up gcc */
3914 /* convert to "bytes" */
3916 /* count is now in 'BYTES' */
3922 e = target_read_memory(target, a, b, y / b, target_buf);
3923 if (e != ERROR_OK) {
3924 Jim_SetResult_sprintf(interp, "error reading target @ 0x%08lx", (int)(a));
3928 Jim_fprintf(interp, interp->cookie_stdout, "0x%08x ", (int)(a));
3931 for (x = 0 ; (x < 16) && (x < y) ; x += 4) {
3932 z = target_buffer_get_u32(target, &(target_buf[ x * 4 ]));
3933 Jim_fprintf(interp, interp->cookie_stdout, "%08x ", (int)(z));
3935 for (; (x < 16) ; x += 4) {
3936 Jim_fprintf(interp, interp->cookie_stdout, " ");
3940 for (x = 0 ; (x < 16) && (x < y) ; x += 2) {
3941 z = target_buffer_get_u16(target, &(target_buf[ x * 2 ]));
3942 Jim_fprintf(interp, interp->cookie_stdout, "%04x ", (int)(z));
3944 for (; (x < 16) ; x += 2) {
3945 Jim_fprintf(interp, interp->cookie_stdout, " ");
3950 for (x = 0 ; (x < 16) && (x < y) ; x += 1) {
3951 z = target_buffer_get_u8(target, &(target_buf[ x * 4 ]));
3952 Jim_fprintf(interp, interp->cookie_stdout, "%02x ", (int)(z));
3954 for (; (x < 16) ; x += 1) {
3955 Jim_fprintf(interp, interp->cookie_stdout, " ");
3959 /* ascii-ify the bytes */
3960 for (x = 0 ; x < y ; x++) {
3961 if ((target_buf[x] >= 0x20) &&
3962 (target_buf[x] <= 0x7e)) {
3966 target_buf[x] = '.';
3971 target_buf[x] = ' ';
3976 /* print - with a newline */
3977 Jim_fprintf(interp, interp->cookie_stdout, "%s\n", target_buf);
3983 case TS_CMD_MEM2ARRAY:
3984 return target_mem2array(goi.interp, target, goi.argc, goi.argv);
3986 case TS_CMD_ARRAY2MEM:
3987 return target_array2mem(goi.interp, target, goi.argc, goi.argv);
3989 case TS_CMD_EXAMINE:
3991 Jim_WrongNumArgs(goi.interp, 2, argv, "[no parameters]");
3994 if (!target->tap->enabled)
3995 goto err_tap_disabled;
3996 e = target->type->examine(target);
3997 if (e != ERROR_OK) {
3998 Jim_SetResult_sprintf(interp, "examine-fails: %d", e);
4004 Jim_WrongNumArgs(goi.interp, 2, argv, "[no parameters]");
4007 if (!target->tap->enabled)
4008 goto err_tap_disabled;
4009 if (!(target_was_examined(target))) {
4010 e = ERROR_TARGET_NOT_EXAMINED;
4012 e = target->type->poll(target);
4014 if (e != ERROR_OK) {
4015 Jim_SetResult_sprintf(interp, "poll-fails: %d", e);
4022 if (goi.argc != 2) {
4023 Jim_WrongNumArgs(interp, 2, argv, "t | f|assert | deassert BOOL");
4026 e = Jim_GetOpt_Nvp(&goi, nvp_assert, &n);
4028 Jim_GetOpt_NvpUnknown(&goi, nvp_assert, 1);
4031 /* the halt or not param */
4032 e = Jim_GetOpt_Wide(&goi, &a);
4036 if (!target->tap->enabled)
4037 goto err_tap_disabled;
4038 /* determine if we should halt or not. */
4039 target->reset_halt = !!a;
4040 /* When this happens - all workareas are invalid. */
4041 target_free_all_working_areas_restore(target, 0);
4044 if (n->value == NVP_ASSERT) {
4045 target->type->assert_reset(target);
4047 target->type->deassert_reset(target);
4052 Jim_WrongNumArgs(goi.interp, 0, argv, "halt [no parameters]");
4055 if (!target->tap->enabled)
4056 goto err_tap_disabled;
4057 target->type->halt(target);
4059 case TS_CMD_WAITSTATE:
4060 /* params: <name> statename timeoutmsecs */
4061 if (goi.argc != 2) {
4062 Jim_SetResult_sprintf(goi.interp, "%s STATENAME TIMEOUTMSECS", n->name);
4065 e = Jim_GetOpt_Nvp(&goi, nvp_target_state, &n);
4067 Jim_GetOpt_NvpUnknown(&goi, nvp_target_state,1);
4070 e = Jim_GetOpt_Wide(&goi, &a);
4074 if (!target->tap->enabled)
4075 goto err_tap_disabled;
4076 e = target_wait_state(target, n->value, a);
4077 if (e != ERROR_OK) {
4078 Jim_SetResult_sprintf(goi.interp,
4079 "target: %s wait %s fails (%d) %s",
4082 e, target_strerror_safe(e));
4087 case TS_CMD_EVENTLIST:
4088 /* List for human, Events defined for this target.
4089 * scripts/programs should use 'name cget -event NAME'
4092 target_event_action_t *teap;
4093 teap = target->event_action;
4094 command_print(cmd_ctx, "Event actions for target (%d) %s\n",
4095 target->target_number,
4097 command_print(cmd_ctx, "%-25s | Body", "Event");
4098 command_print(cmd_ctx, "------------------------- | ----------------------------------------");
4100 command_print(cmd_ctx,
4102 Jim_Nvp_value2name_simple(nvp_target_event, teap->event)->name,
4103 Jim_GetString(teap->body, NULL));
4106 command_print(cmd_ctx, "***END***");
4109 case TS_CMD_CURSTATE:
4110 if (goi.argc != 0) {
4111 Jim_WrongNumArgs(goi.interp, 0, argv, "[no parameters]");
4114 Jim_SetResultString(goi.interp,
4115 target_state_name( target ),
4118 case TS_CMD_INVOKE_EVENT:
4119 if (goi.argc != 1) {
4120 Jim_SetResult_sprintf(goi.interp, "%s ?EVENTNAME?",n->name);
4123 e = Jim_GetOpt_Nvp(&goi, nvp_target_event, &n);
4125 Jim_GetOpt_NvpUnknown(&goi, nvp_target_event, 1);
4128 target_handle_event(target, n->value);
4134 Jim_SetResult_sprintf(interp, "[TAP is disabled]");
4138 static int target_create(Jim_GetOptInfo *goi)
4147 struct command_context_s *cmd_ctx;
4149 cmd_ctx = Jim_GetAssocData(goi->interp, "context");
4150 if (goi->argc < 3) {
4151 Jim_WrongNumArgs(goi->interp, 1, goi->argv, "?name? ?type? ..options...");
4156 Jim_GetOpt_Obj(goi, &new_cmd);
4157 /* does this command exist? */
4158 cmd = Jim_GetCommand(goi->interp, new_cmd, JIM_ERRMSG);
4160 cp = Jim_GetString(new_cmd, NULL);
4161 Jim_SetResult_sprintf(goi->interp, "Command/target: %s Exists", cp);
4166 e = Jim_GetOpt_String(goi, &cp2, NULL);
4168 /* now does target type exist */
4169 for (x = 0 ; target_types[x] ; x++) {
4170 if (0 == strcmp(cp, target_types[x]->name)) {
4175 if (target_types[x] == NULL) {
4176 Jim_SetResult_sprintf(goi->interp, "Unknown target type %s, try one of ", cp);
4177 for (x = 0 ; target_types[x] ; x++) {
4178 if (target_types[x + 1]) {
4179 Jim_AppendStrings(goi->interp,
4180 Jim_GetResult(goi->interp),
4181 target_types[x]->name,
4184 Jim_AppendStrings(goi->interp,
4185 Jim_GetResult(goi->interp),
4187 target_types[x]->name,NULL);
4194 target = calloc(1,sizeof(target_t));
4195 /* set target number */
4196 target->target_number = new_target_number();
4198 /* allocate memory for each unique target type */
4199 target->type = (target_type_t*)calloc(1,sizeof(target_type_t));
4201 memcpy(target->type, target_types[x], sizeof(target_type_t));
4203 /* will be set by "-endian" */
4204 target->endianness = TARGET_ENDIAN_UNKNOWN;
4206 target->working_area = 0x0;
4207 target->working_area_size = 0x0;
4208 target->working_areas = NULL;
4209 target->backup_working_area = 0;
4211 target->state = TARGET_UNKNOWN;
4212 target->debug_reason = DBG_REASON_UNDEFINED;
4213 target->reg_cache = NULL;
4214 target->breakpoints = NULL;
4215 target->watchpoints = NULL;
4216 target->next = NULL;
4217 target->arch_info = NULL;
4219 target->display = 1;
4221 /* initialize trace information */
4222 target->trace_info = malloc(sizeof(trace_t));
4223 target->trace_info->num_trace_points = 0;
4224 target->trace_info->trace_points_size = 0;
4225 target->trace_info->trace_points = NULL;
4226 target->trace_info->trace_history_size = 0;
4227 target->trace_info->trace_history = NULL;
4228 target->trace_info->trace_history_pos = 0;
4229 target->trace_info->trace_history_overflowed = 0;
4231 target->dbgmsg = NULL;
4232 target->dbg_msg_enabled = 0;
4234 target->endianness = TARGET_ENDIAN_UNKNOWN;
4236 /* Do the rest as "configure" options */
4237 goi->isconfigure = 1;
4238 e = target_configure(goi, target);
4240 if (target->tap == NULL)
4242 Jim_SetResultString(interp, "-chain-position required when creating target", -1);
4252 if (target->endianness == TARGET_ENDIAN_UNKNOWN) {
4253 /* default endian to little if not specified */
4254 target->endianness = TARGET_LITTLE_ENDIAN;
4257 /* incase variant is not set */
4258 if (!target->variant)
4259 target->variant = strdup("");
4261 /* create the target specific commands */
4262 if (target->type->register_commands) {
4263 (*(target->type->register_commands))(cmd_ctx);
4265 if (target->type->target_create) {
4266 (*(target->type->target_create))(target, goi->interp);
4269 /* append to end of list */
4272 tpp = &(all_targets);
4274 tpp = &((*tpp)->next);
4279 cp = Jim_GetString(new_cmd, NULL);
4280 target->cmd_name = strdup(cp);
4282 /* now - create the new target name command */
4283 e = Jim_CreateCommand(goi->interp,
4286 tcl_target_func, /* C function */
4287 target, /* private data */
4288 NULL); /* no del proc */
4293 static int jim_target(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
4297 struct command_context_s *cmd_ctx;
4301 /* TG = target generic */
4309 const char *target_cmds[] = {
4310 "create", "types", "names", "current", "number",
4312 NULL /* terminate */
4315 LOG_DEBUG("Target command params:");
4316 LOG_DEBUG("%s", Jim_Debug_ArgvString(interp, argc, argv));
4318 cmd_ctx = Jim_GetAssocData(interp, "context");
4320 Jim_GetOpt_Setup(&goi, interp, argc-1, argv + 1);
4322 if (goi.argc == 0) {
4323 Jim_WrongNumArgs(interp, 1, argv, "missing: command ...");
4327 /* Jim_GetOpt_Debug(&goi); */
4328 r = Jim_GetOpt_Enum(&goi, target_cmds, &x);
4335 Jim_Panic(goi.interp,"Why am I here?");
4337 case TG_CMD_CURRENT:
4338 if (goi.argc != 0) {
4339 Jim_WrongNumArgs(goi.interp, 1, goi.argv, "Too many parameters");
4342 Jim_SetResultString(goi.interp, get_current_target(cmd_ctx)->cmd_name, -1);
4345 if (goi.argc != 0) {
4346 Jim_WrongNumArgs(goi.interp, 1, goi.argv, "Too many parameters");
4349 Jim_SetResult(goi.interp, Jim_NewListObj(goi.interp, NULL, 0));
4350 for (x = 0 ; target_types[x] ; x++) {
4351 Jim_ListAppendElement(goi.interp,
4352 Jim_GetResult(goi.interp),
4353 Jim_NewStringObj(goi.interp, target_types[x]->name, -1));
4357 if (goi.argc != 0) {
4358 Jim_WrongNumArgs(goi.interp, 1, goi.argv, "Too many parameters");
4361 Jim_SetResult(goi.interp, Jim_NewListObj(goi.interp, NULL, 0));
4362 target = all_targets;
4364 Jim_ListAppendElement(goi.interp,
4365 Jim_GetResult(goi.interp),
4366 Jim_NewStringObj(goi.interp, target->cmd_name, -1));
4367 target = target->next;
4372 Jim_WrongNumArgs(goi.interp, goi.argc, goi.argv, "?name ... config options ...");
4375 return target_create(&goi);
4378 if (goi.argc != 1) {
4379 Jim_SetResult_sprintf(goi.interp, "expected: target number ?NUMBER?");
4382 e = Jim_GetOpt_Wide(&goi, &w);
4388 t = get_target_by_num(w);
4390 Jim_SetResult_sprintf(goi.interp,"Target: number %d does not exist", (int)(w));
4393 Jim_SetResultString(goi.interp, t->cmd_name, -1);
4397 if (goi.argc != 0) {
4398 Jim_WrongNumArgs(goi.interp, 0, goi.argv, "<no parameters>");
4401 Jim_SetResult(goi.interp,
4402 Jim_NewIntObj(goi.interp, max_target_number()));
4418 static int fastload_num;
4419 static struct FastLoad *fastload;
4421 static void free_fastload(void)
4423 if (fastload != NULL)
4426 for (i = 0; i < fastload_num; i++)
4428 if (fastload[i].data)
4429 free(fastload[i].data);
4439 static int handle_fast_load_image_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
4443 uint32_t image_size;
4444 uint32_t min_address = 0;
4445 uint32_t max_address = 0xffffffff;
4450 duration_t duration;
4451 char *duration_text;
4453 int retval = parse_load_image_command_args(args, argc,
4454 &image, &min_address, &max_address);
4455 if (ERROR_OK != retval)
4458 duration_start_measure(&duration);
4460 if (image_open(&image, args[0], (argc >= 3) ? args[2] : NULL) != ERROR_OK)
4467 fastload_num = image.num_sections;
4468 fastload = (struct FastLoad *)malloc(sizeof(struct FastLoad)*image.num_sections);
4469 if (fastload == NULL)
4471 image_close(&image);
4474 memset(fastload, 0, sizeof(struct FastLoad)*image.num_sections);
4475 for (i = 0; i < image.num_sections; i++)
4477 buffer = malloc(image.sections[i].size);
4480 command_print(cmd_ctx, "error allocating buffer for section (%d bytes)",
4481 (int)(image.sections[i].size));
4485 if ((retval = image_read_section(&image, i, 0x0, image.sections[i].size, buffer, &buf_cnt)) != ERROR_OK)
4491 uint32_t offset = 0;
4492 uint32_t length = buf_cnt;
4495 /* DANGER!!! beware of unsigned comparision here!!! */
4497 if ((image.sections[i].base_address + buf_cnt >= min_address)&&
4498 (image.sections[i].base_address < max_address))
4500 if (image.sections[i].base_address < min_address)
4502 /* clip addresses below */
4503 offset += min_address-image.sections[i].base_address;
4507 if (image.sections[i].base_address + buf_cnt > max_address)
4509 length -= (image.sections[i].base_address + buf_cnt)-max_address;
4512 fastload[i].address = image.sections[i].base_address + offset;
4513 fastload[i].data = malloc(length);
4514 if (fastload[i].data == NULL)
4519 memcpy(fastload[i].data, buffer + offset, length);
4520 fastload[i].length = length;
4522 image_size += length;
4523 command_print(cmd_ctx, "%u byte written at address 0x%8.8x",
4524 (unsigned int)length,
4525 ((unsigned int)(image.sections[i].base_address + offset)));
4531 duration_stop_measure(&duration, &duration_text);
4532 if (retval == ERROR_OK)
4534 command_print(cmd_ctx, "Loaded %u bytes in %s", (unsigned int)image_size, duration_text);
4535 command_print(cmd_ctx, "NB!!! image has not been loaded to target, issue a subsequent 'fast_load' to do so.");
4537 free(duration_text);
4539 image_close(&image);
4541 if (retval != ERROR_OK)
4549 static int handle_fast_load_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
4552 return ERROR_COMMAND_SYNTAX_ERROR;
4553 if (fastload == NULL)
4555 LOG_ERROR("No image in memory");
4559 int ms = timeval_ms();
4561 int retval = ERROR_OK;
4562 for (i = 0; i < fastload_num;i++)
4564 target_t *target = get_current_target(cmd_ctx);
4565 command_print(cmd_ctx, "Write to 0x%08x, length 0x%08x",
4566 (unsigned int)(fastload[i].address),
4567 (unsigned int)(fastload[i].length));
4568 if (retval == ERROR_OK)
4570 retval = target_write_buffer(target, fastload[i].address, fastload[i].length, fastload[i].data);
4572 size += fastload[i].length;
4574 int after = timeval_ms();
4575 command_print(cmd_ctx, "Loaded image %f kBytes/s", (float)(size/1024.0)/((float)(after-ms)/1000.0));