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", handle_bp_command, COMMAND_EXEC, "set breakpoint <address> <length> [hw]");
1509 register_command(cmd_ctx, NULL, "rbp", handle_rbp_command, COMMAND_EXEC, "remove breakpoint <adress>");
1510 register_command(cmd_ctx, NULL, "wp", handle_wp_command, COMMAND_EXEC, "set watchpoint <address> <length> <r/w/a> [value] [mask]");
1511 register_command(cmd_ctx, NULL, "rwp", handle_rwp_command, COMMAND_EXEC, "remove watchpoint <adress>");
1513 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]");
1514 register_command(cmd_ctx, NULL, "dump_image", handle_dump_image_command, COMMAND_EXEC, "dump_image <file> <address> <size>");
1515 register_command(cmd_ctx, NULL, "verify_image", handle_verify_image_command, COMMAND_EXEC, "verify_image <file> [offset] [type]");
1516 register_command(cmd_ctx, NULL, "test_image", handle_test_image_command, COMMAND_EXEC, "test_image <file> [offset] [type]");
1518 if ((retval = target_request_register_commands(cmd_ctx)) != ERROR_OK)
1520 if ((retval = trace_register_commands(cmd_ctx)) != ERROR_OK)
1526 static int handle_targets_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1528 target_t *target = all_targets;
1532 target = get_target(args[0]);
1533 if (target == NULL) {
1534 command_print(cmd_ctx,"Target: %s is unknown, try one of:\n", args[0]);
1537 if (!target->tap->enabled) {
1538 command_print(cmd_ctx,"Target: TAP %s is disabled, "
1539 "can't be the current target\n",
1540 target->tap->dotted_name);
1544 cmd_ctx->current_target = target->target_number;
1549 target = all_targets;
1550 command_print(cmd_ctx, " TargetName Type Endian TapName State ");
1551 command_print(cmd_ctx, "-- ------------------ ---------- ------ ------------------ ------------");
1557 if (target->tap->enabled)
1558 state = target_state_name( target );
1560 state = "tap-disabled";
1562 if (cmd_ctx->current_target == target->target_number)
1565 /* keep columns lined up to match the headers above */
1566 command_print(cmd_ctx, "%2d%c %-18s %-10s %-6s %-18s %s",
1567 target->target_number,
1570 target_get_name(target),
1571 Jim_Nvp_value2name_simple(nvp_target_endian,
1572 target->endianness)->name,
1573 target->tap->dotted_name,
1575 target = target->next;
1581 /* every 300ms we check for reset & powerdropout and issue a "reset halt" if so. */
1583 static int powerDropout;
1584 static int srstAsserted;
1586 static int runPowerRestore;
1587 static int runPowerDropout;
1588 static int runSrstAsserted;
1589 static int runSrstDeasserted;
1591 static int sense_handler(void)
1593 static int prevSrstAsserted = 0;
1594 static int prevPowerdropout = 0;
1597 if ((retval = jtag_power_dropout(&powerDropout)) != ERROR_OK)
1601 powerRestored = prevPowerdropout && !powerDropout;
1604 runPowerRestore = 1;
1607 long long current = timeval_ms();
1608 static long long lastPower = 0;
1609 int waitMore = lastPower + 2000 > current;
1610 if (powerDropout && !waitMore)
1612 runPowerDropout = 1;
1613 lastPower = current;
1616 if ((retval = jtag_srst_asserted(&srstAsserted)) != ERROR_OK)
1620 srstDeasserted = prevSrstAsserted && !srstAsserted;
1622 static long long lastSrst = 0;
1623 waitMore = lastSrst + 2000 > current;
1624 if (srstDeasserted && !waitMore)
1626 runSrstDeasserted = 1;
1630 if (!prevSrstAsserted && srstAsserted)
1632 runSrstAsserted = 1;
1635 prevSrstAsserted = srstAsserted;
1636 prevPowerdropout = powerDropout;
1638 if (srstDeasserted || powerRestored)
1640 /* Other than logging the event we can't do anything here.
1641 * Issuing a reset is a particularly bad idea as we might
1642 * be inside a reset already.
1649 /* process target state changes */
1650 int handle_target(void *priv)
1652 int retval = ERROR_OK;
1654 /* we do not want to recurse here... */
1655 static int recursive = 0;
1660 /* danger! running these procedures can trigger srst assertions and power dropouts.
1661 * We need to avoid an infinite loop/recursion here and we do that by
1662 * clearing the flags after running these events.
1664 int did_something = 0;
1665 if (runSrstAsserted)
1667 Jim_Eval(interp, "srst_asserted");
1670 if (runSrstDeasserted)
1672 Jim_Eval(interp, "srst_deasserted");
1675 if (runPowerDropout)
1677 Jim_Eval(interp, "power_dropout");
1680 if (runPowerRestore)
1682 Jim_Eval(interp, "power_restore");
1688 /* clear detect flags */
1692 /* clear action flags */
1694 runSrstAsserted = 0;
1695 runSrstDeasserted = 0;
1696 runPowerRestore = 0;
1697 runPowerDropout = 0;
1702 /* Poll targets for state changes unless that's globally disabled.
1703 * Skip targets that are currently disabled.
1705 for (target_t *target = all_targets;
1706 target_continuous_poll && target;
1707 target = target->next)
1709 if (!target->tap->enabled)
1712 /* only poll target if we've got power and srst isn't asserted */
1713 if (!powerDropout && !srstAsserted)
1715 /* polling may fail silently until the target has been examined */
1716 if ((retval = target_poll(target)) != ERROR_OK)
1724 static int handle_reg_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1733 target = get_current_target(cmd_ctx);
1735 /* list all available registers for the current target */
1738 reg_cache_t *cache = target->reg_cache;
1744 for (i = 0; i < cache->num_regs; i++)
1746 value = buf_to_str(cache->reg_list[i].value, cache->reg_list[i].size, 16);
1747 command_print(cmd_ctx, "(%i) %s (/%i): 0x%s (dirty: %i, valid: %i)",
1749 cache->reg_list[i].name,
1750 (int)(cache->reg_list[i].size),
1752 cache->reg_list[i].dirty,
1753 cache->reg_list[i].valid);
1756 cache = cache->next;
1762 /* access a single register by its ordinal number */
1763 if ((args[0][0] >= '0') && (args[0][0] <= '9'))
1766 int retval = parse_uint(args[0], &num);
1767 if (ERROR_OK != retval)
1768 return ERROR_COMMAND_SYNTAX_ERROR;
1770 reg_cache_t *cache = target->reg_cache;
1775 for (i = 0; i < cache->num_regs; i++)
1777 if (count++ == (int)num)
1779 reg = &cache->reg_list[i];
1785 cache = cache->next;
1790 command_print(cmd_ctx, "%i is out of bounds, the current target has only %i registers (0 - %i)", num, count, count - 1);
1793 } else /* access a single register by its name */
1795 reg = register_get_by_name(target->reg_cache, args[0], 1);
1799 command_print(cmd_ctx, "register %s not found in current target", args[0]);
1804 /* display a register */
1805 if ((argc == 1) || ((argc == 2) && !((args[1][0] >= '0') && (args[1][0] <= '9'))))
1807 if ((argc == 2) && (strcmp(args[1], "force") == 0))
1810 if (reg->valid == 0)
1812 reg_arch_type_t *arch_type = register_get_arch_type(reg->arch_type);
1813 arch_type->get(reg);
1815 value = buf_to_str(reg->value, reg->size, 16);
1816 command_print(cmd_ctx, "%s (/%i): 0x%s", reg->name, (int)(reg->size), value);
1821 /* set register value */
1824 uint8_t *buf = malloc(CEIL(reg->size, 8));
1825 str_to_buf(args[1], strlen(args[1]), buf, reg->size, 0);
1827 reg_arch_type_t *arch_type = register_get_arch_type(reg->arch_type);
1828 arch_type->set(reg, buf);
1830 value = buf_to_str(reg->value, reg->size, 16);
1831 command_print(cmd_ctx, "%s (/%i): 0x%s", reg->name, (int)(reg->size), value);
1839 command_print(cmd_ctx, "usage: reg <#|name> [value]");
1844 static int handle_poll_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1846 int retval = ERROR_OK;
1847 target_t *target = get_current_target(cmd_ctx);
1851 command_print(cmd_ctx, "background polling: %s",
1852 target_continuous_poll ? "on" : "off");
1853 command_print(cmd_ctx, "TAP: %s (%s)",
1854 target->tap->dotted_name,
1855 target->tap->enabled ? "enabled" : "disabled");
1856 if (!target->tap->enabled)
1858 if ((retval = target_poll(target)) != ERROR_OK)
1860 if ((retval = target_arch_state(target)) != ERROR_OK)
1866 if (strcmp(args[0], "on") == 0)
1868 target_continuous_poll = 1;
1870 else if (strcmp(args[0], "off") == 0)
1872 target_continuous_poll = 0;
1876 command_print(cmd_ctx, "arg is \"on\" or \"off\"");
1880 return ERROR_COMMAND_SYNTAX_ERROR;
1886 static int handle_wait_halt_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1889 return ERROR_COMMAND_SYNTAX_ERROR;
1894 int retval = parse_uint(args[0], &ms);
1895 if (ERROR_OK != retval)
1897 command_print(cmd_ctx, "usage: %s [seconds]", cmd);
1898 return ERROR_COMMAND_SYNTAX_ERROR;
1900 // convert seconds (given) to milliseconds (needed)
1904 target_t *target = get_current_target(cmd_ctx);
1905 return target_wait_state(target, TARGET_HALTED, ms);
1908 /* wait for target state to change. The trick here is to have a low
1909 * latency for short waits and not to suck up all the CPU time
1912 * After 500ms, keep_alive() is invoked
1914 int target_wait_state(target_t *target, enum target_state state, int ms)
1917 long long then = 0, cur;
1922 if ((retval = target_poll(target)) != ERROR_OK)
1924 if (target->state == state)
1932 then = timeval_ms();
1933 LOG_DEBUG("waiting for target %s...",
1934 Jim_Nvp_value2name_simple(nvp_target_state,state)->name);
1942 if ((cur-then) > ms)
1944 LOG_ERROR("timed out while waiting for target %s",
1945 Jim_Nvp_value2name_simple(nvp_target_state,state)->name);
1953 static int handle_halt_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1957 target_t *target = get_current_target(cmd_ctx);
1958 int retval = target_halt(target);
1959 if (ERROR_OK != retval)
1965 retval = parse_uint(args[0], &wait);
1966 if (ERROR_OK != retval)
1967 return ERROR_COMMAND_SYNTAX_ERROR;
1972 return handle_wait_halt_command(cmd_ctx, cmd, args, argc);
1975 static int handle_soft_reset_halt_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1977 target_t *target = get_current_target(cmd_ctx);
1979 LOG_USER("requesting target halt and executing a soft reset");
1981 target->type->soft_reset_halt(target);
1986 static int handle_reset_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1989 return ERROR_COMMAND_SYNTAX_ERROR;
1991 enum target_reset_mode reset_mode = RESET_RUN;
1995 n = Jim_Nvp_name2value_simple(nvp_reset_modes, args[0]);
1996 if ((n->name == NULL) || (n->value == RESET_UNKNOWN)) {
1997 return ERROR_COMMAND_SYNTAX_ERROR;
1999 reset_mode = n->value;
2002 /* reset *all* targets */
2003 return target_process_reset(cmd_ctx, reset_mode);
2007 static int handle_resume_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2011 return ERROR_COMMAND_SYNTAX_ERROR;
2013 target_t *target = get_current_target(cmd_ctx);
2014 target_handle_event(target, TARGET_EVENT_OLD_pre_resume);
2016 /* with no args, resume from current pc, addr = 0,
2017 * with one arguments, addr = args[0],
2018 * handle breakpoints, not debugging */
2022 int retval = parse_u32(args[0], &addr);
2023 if (ERROR_OK != retval)
2028 return target_resume(target, current, addr, 1, 0);
2031 static int handle_step_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2034 return ERROR_COMMAND_SYNTAX_ERROR;
2038 /* with no args, step from current pc, addr = 0,
2039 * with one argument addr = args[0],
2040 * handle breakpoints, debugging */
2045 int retval = parse_u32(args[0], &addr);
2046 if (ERROR_OK != retval)
2051 target_t *target = get_current_target(cmd_ctx);
2053 return target->type->step(target, current_pc, addr, 1);
2056 static void handle_md_output(struct command_context_s *cmd_ctx,
2057 struct target_s *target, uint32_t address, unsigned size,
2058 unsigned count, const uint8_t *buffer)
2060 const unsigned line_bytecnt = 32;
2061 unsigned line_modulo = line_bytecnt / size;
2063 char output[line_bytecnt * 4 + 1];
2064 unsigned output_len = 0;
2066 const char *value_fmt;
2068 case 4: value_fmt = "%8.8x "; break;
2069 case 2: value_fmt = "%4.2x "; break;
2070 case 1: value_fmt = "%2.2x "; break;
2072 LOG_ERROR("invalid memory read size: %u", size);
2076 for (unsigned i = 0; i < count; i++)
2078 if (i % line_modulo == 0)
2080 output_len += snprintf(output + output_len,
2081 sizeof(output) - output_len,
2083 (unsigned)(address + (i*size)));
2087 const uint8_t *value_ptr = buffer + i * size;
2089 case 4: value = target_buffer_get_u32(target, value_ptr); break;
2090 case 2: value = target_buffer_get_u16(target, value_ptr); break;
2091 case 1: value = *value_ptr;
2093 output_len += snprintf(output + output_len,
2094 sizeof(output) - output_len,
2097 if ((i % line_modulo == line_modulo - 1) || (i == count - 1))
2099 command_print(cmd_ctx, "%s", output);
2105 static int handle_md_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2108 return ERROR_COMMAND_SYNTAX_ERROR;
2112 case 'w': size = 4; break;
2113 case 'h': size = 2; break;
2114 case 'b': size = 1; break;
2115 default: return ERROR_COMMAND_SYNTAX_ERROR;
2119 int retval = parse_u32(args[0], &address);
2120 if (ERROR_OK != retval)
2126 retval = parse_uint(args[1], &count);
2127 if (ERROR_OK != retval)
2131 uint8_t *buffer = calloc(count, size);
2133 target_t *target = get_current_target(cmd_ctx);
2134 retval = target_read_memory(target,
2135 address, size, count, buffer);
2136 if (ERROR_OK == retval)
2137 handle_md_output(cmd_ctx, target, address, size, count, buffer);
2144 static int handle_mw_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2146 if ((argc < 2) || (argc > 3))
2147 return ERROR_COMMAND_SYNTAX_ERROR;
2150 int retval = parse_u32(args[0], &address);
2151 if (ERROR_OK != retval)
2155 retval = parse_u32(args[1], &value);
2156 if (ERROR_OK != retval)
2162 retval = parse_uint(args[2], &count);
2163 if (ERROR_OK != retval)
2167 target_t *target = get_current_target(cmd_ctx);
2169 uint8_t value_buf[4];
2174 target_buffer_set_u32(target, value_buf, value);
2178 target_buffer_set_u16(target, value_buf, value);
2182 value_buf[0] = value;
2185 return ERROR_COMMAND_SYNTAX_ERROR;
2187 for (unsigned i = 0; i < count; i++)
2189 retval = target_write_memory(target,
2190 address + i * wordsize, wordsize, 1, value_buf);
2191 if (ERROR_OK != retval)
2200 static int parse_load_image_command_args(char **args, int argc,
2201 image_t *image, uint32_t *min_address, uint32_t *max_address)
2203 if (argc < 1 || argc > 5)
2204 return ERROR_COMMAND_SYNTAX_ERROR;
2206 /* a base address isn't always necessary,
2207 * default to 0x0 (i.e. don't relocate) */
2211 int retval = parse_u32(args[1], &addr);
2212 if (ERROR_OK != retval)
2213 return ERROR_COMMAND_SYNTAX_ERROR;
2214 image->base_address = addr;
2215 image->base_address_set = 1;
2218 image->base_address_set = 0;
2220 image->start_address_set = 0;
2224 int retval = parse_u32(args[3], min_address);
2225 if (ERROR_OK != retval)
2226 return ERROR_COMMAND_SYNTAX_ERROR;
2230 int retval = parse_u32(args[4], max_address);
2231 if (ERROR_OK != retval)
2232 return ERROR_COMMAND_SYNTAX_ERROR;
2233 // use size (given) to find max (required)
2234 *max_address += *min_address;
2237 if (*min_address > *max_address)
2238 return ERROR_COMMAND_SYNTAX_ERROR;
2243 static int handle_load_image_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2247 uint32_t image_size;
2248 uint32_t min_address = 0;
2249 uint32_t max_address = 0xffffffff;
2255 duration_t duration;
2256 char *duration_text;
2258 int retval = parse_load_image_command_args(args, argc,
2259 &image, &min_address, &max_address);
2260 if (ERROR_OK != retval)
2263 target_t *target = get_current_target(cmd_ctx);
2264 duration_start_measure(&duration);
2266 if (image_open(&image, args[0], (argc >= 3) ? args[2] : NULL) != ERROR_OK)
2273 for (i = 0; i < image.num_sections; i++)
2275 buffer = malloc(image.sections[i].size);
2278 command_print(cmd_ctx,
2279 "error allocating buffer for section (%d bytes)",
2280 (int)(image.sections[i].size));
2284 if ((retval = image_read_section(&image, i, 0x0, image.sections[i].size, buffer, &buf_cnt)) != ERROR_OK)
2290 uint32_t offset = 0;
2291 uint32_t length = buf_cnt;
2293 /* DANGER!!! beware of unsigned comparision here!!! */
2295 if ((image.sections[i].base_address + buf_cnt >= min_address)&&
2296 (image.sections[i].base_address < max_address))
2298 if (image.sections[i].base_address < min_address)
2300 /* clip addresses below */
2301 offset += min_address-image.sections[i].base_address;
2305 if (image.sections[i].base_address + buf_cnt > max_address)
2307 length -= (image.sections[i].base_address + buf_cnt)-max_address;
2310 if ((retval = target_write_buffer(target, image.sections[i].base_address + offset, length, buffer + offset)) != ERROR_OK)
2315 image_size += length;
2316 command_print(cmd_ctx, "%u byte written at address 0x%8.8" PRIx32 "",
2317 (unsigned int)length,
2318 image.sections[i].base_address + offset);
2324 if ((retvaltemp = duration_stop_measure(&duration, &duration_text)) != ERROR_OK)
2326 image_close(&image);
2330 if (retval == ERROR_OK)
2332 command_print(cmd_ctx, "downloaded %u byte in %s",
2333 (unsigned int)image_size,
2336 free(duration_text);
2338 image_close(&image);
2344 static int handle_dump_image_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2348 uint8_t buffer[560];
2351 duration_t duration;
2352 char *duration_text;
2354 target_t *target = get_current_target(cmd_ctx);
2358 command_print(cmd_ctx, "usage: dump_image <filename> <address> <size>");
2363 int retval = parse_u32(args[1], &address);
2364 if (ERROR_OK != retval)
2368 retval = parse_u32(args[2], &size);
2369 if (ERROR_OK != retval)
2372 if (fileio_open(&fileio, args[0], FILEIO_WRITE, FILEIO_BINARY) != ERROR_OK)
2377 duration_start_measure(&duration);
2381 uint32_t size_written;
2382 uint32_t this_run_size = (size > 560) ? 560 : size;
2384 retval = target_read_buffer(target, address, this_run_size, buffer);
2385 if (retval != ERROR_OK)
2390 retval = fileio_write(&fileio, this_run_size, buffer, &size_written);
2391 if (retval != ERROR_OK)
2396 size -= this_run_size;
2397 address += this_run_size;
2400 if ((retvaltemp = fileio_close(&fileio)) != ERROR_OK)
2403 if ((retvaltemp = duration_stop_measure(&duration, &duration_text)) != ERROR_OK)
2406 if (retval == ERROR_OK)
2408 command_print(cmd_ctx, "dumped %lld byte in %s",
2409 fileio.size, duration_text);
2410 free(duration_text);
2416 static int handle_verify_image_command_internal(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc, int verify)
2420 uint32_t image_size;
2422 int retval, retvaltemp;
2423 uint32_t checksum = 0;
2424 uint32_t mem_checksum = 0;
2428 duration_t duration;
2429 char *duration_text;
2431 target_t *target = get_current_target(cmd_ctx);
2435 return ERROR_COMMAND_SYNTAX_ERROR;
2440 LOG_ERROR("no target selected");
2444 duration_start_measure(&duration);
2449 retval = parse_u32(args[1], &addr);
2450 if (ERROR_OK != retval)
2451 return ERROR_COMMAND_SYNTAX_ERROR;
2452 image.base_address = addr;
2453 image.base_address_set = 1;
2457 image.base_address_set = 0;
2458 image.base_address = 0x0;
2461 image.start_address_set = 0;
2463 if ((retval = image_open(&image, args[0], (argc == 3) ? args[2] : NULL)) != ERROR_OK)
2470 for (i = 0; i < image.num_sections; i++)
2472 buffer = malloc(image.sections[i].size);
2475 command_print(cmd_ctx,
2476 "error allocating buffer for section (%d bytes)",
2477 (int)(image.sections[i].size));
2480 if ((retval = image_read_section(&image, i, 0x0, image.sections[i].size, buffer, &buf_cnt)) != ERROR_OK)
2488 /* calculate checksum of image */
2489 image_calculate_checksum(buffer, buf_cnt, &checksum);
2491 retval = target_checksum_memory(target, image.sections[i].base_address, buf_cnt, &mem_checksum);
2492 if (retval != ERROR_OK)
2498 if (checksum != mem_checksum)
2500 /* failed crc checksum, fall back to a binary compare */
2503 command_print(cmd_ctx, "checksum mismatch - attempting binary compare");
2505 data = (uint8_t*)malloc(buf_cnt);
2507 /* Can we use 32bit word accesses? */
2509 int count = buf_cnt;
2510 if ((count % 4) == 0)
2515 retval = target_read_memory(target, image.sections[i].base_address, size, count, data);
2516 if (retval == ERROR_OK)
2519 for (t = 0; t < buf_cnt; t++)
2521 if (data[t] != buffer[t])
2523 command_print(cmd_ctx,
2524 "Verify operation failed address 0x%08x. Was 0x%02x instead of 0x%02x\n",
2525 (unsigned)(t + image.sections[i].base_address),
2530 retval = ERROR_FAIL;
2544 command_print(cmd_ctx, "address 0x%08" PRIx32 " length 0x%08" PRIx32 "",
2545 image.sections[i].base_address,
2550 image_size += buf_cnt;
2554 if ((retvaltemp = duration_stop_measure(&duration, &duration_text)) != ERROR_OK)
2556 image_close(&image);
2560 if (retval == ERROR_OK)
2562 command_print(cmd_ctx, "verified %u bytes in %s",
2563 (unsigned int)image_size,
2566 free(duration_text);
2568 image_close(&image);
2573 static int handle_verify_image_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2575 return handle_verify_image_command_internal(cmd_ctx, cmd, args, argc, 1);
2578 static int handle_test_image_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2580 return handle_verify_image_command_internal(cmd_ctx, cmd, args, argc, 0);
2583 static int handle_bp_command_list(struct command_context_s *cmd_ctx)
2585 target_t *target = get_current_target(cmd_ctx);
2586 breakpoint_t *breakpoint = target->breakpoints;
2589 if (breakpoint->type == BKPT_SOFT)
2591 char* buf = buf_to_str(breakpoint->orig_instr,
2592 breakpoint->length, 16);
2593 command_print(cmd_ctx, "0x%8.8" PRIx32 ", 0x%x, %i, 0x%s",
2594 breakpoint->address,
2596 breakpoint->set, buf);
2601 command_print(cmd_ctx, "0x%8.8" PRIx32 ", 0x%x, %i",
2602 breakpoint->address,
2603 breakpoint->length, breakpoint->set);
2606 breakpoint = breakpoint->next;
2611 static int handle_bp_command_set(struct command_context_s *cmd_ctx,
2612 uint32_t addr, uint32_t length, int hw)
2614 target_t *target = get_current_target(cmd_ctx);
2615 int retval = breakpoint_add(target, addr, length, hw);
2616 if (ERROR_OK == retval)
2617 command_print(cmd_ctx, "breakpoint set at 0x%8.8" PRIx32 "", addr);
2619 LOG_ERROR("Failure setting breakpoint");
2623 static int handle_bp_command(struct command_context_s *cmd_ctx,
2624 char *cmd, char **args, int argc)
2627 return handle_bp_command_list(cmd_ctx);
2629 if (argc < 2 || argc > 3)
2631 command_print(cmd_ctx, "usage: bp <address> <length> ['hw']");
2632 return ERROR_COMMAND_SYNTAX_ERROR;
2636 int retval = parse_u32(args[0], &addr);
2637 if (ERROR_OK != retval)
2641 retval = parse_u32(args[1], &length);
2642 if (ERROR_OK != retval)
2648 if (strcmp(args[2], "hw") == 0)
2651 return ERROR_COMMAND_SYNTAX_ERROR;
2654 return handle_bp_command_set(cmd_ctx, addr, length, hw);
2657 static int handle_rbp_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2660 return ERROR_COMMAND_SYNTAX_ERROR;
2663 int retval = parse_u32(args[0], &addr);
2664 if (ERROR_OK != retval)
2667 target_t *target = get_current_target(cmd_ctx);
2668 breakpoint_remove(target, addr);
2673 static int handle_wp_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2675 target_t *target = get_current_target(cmd_ctx);
2679 watchpoint_t *watchpoint = target->watchpoints;
2683 command_print(cmd_ctx,
2684 "address: 0x%8.8" PRIx32 ", len: 0x%8.8x, r/w/a: %i, value: 0x%8.8" PRIx32 ", mask: 0x%8.8" PRIx32 "",
2685 watchpoint->address,
2687 (int)(watchpoint->rw),
2690 watchpoint = watchpoint->next;
2695 enum watchpoint_rw type = WPT_ACCESS;
2697 uint32_t length = 0;
2698 uint32_t data_value = 0x0;
2699 uint32_t data_mask = 0xffffffff;
2705 retval = parse_u32(args[4], &data_mask);
2706 if (ERROR_OK != retval)
2710 retval = parse_u32(args[3], &data_value);
2711 if (ERROR_OK != retval)
2727 LOG_ERROR("invalid watchpoint mode ('%c')", args[2][0]);
2728 return ERROR_COMMAND_SYNTAX_ERROR;
2732 retval = parse_u32(args[1], &length);
2733 if (ERROR_OK != retval)
2735 retval = parse_u32(args[0], &addr);
2736 if (ERROR_OK != retval)
2741 command_print(cmd_ctx, "usage: wp <address> <length> [r/w/a] [value] [mask]");
2742 return ERROR_COMMAND_SYNTAX_ERROR;
2745 retval = watchpoint_add(target, addr, length, type,
2746 data_value, data_mask);
2747 if (ERROR_OK != retval)
2748 LOG_ERROR("Failure setting watchpoints");
2753 static int handle_rwp_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2756 return ERROR_COMMAND_SYNTAX_ERROR;
2759 int retval = parse_u32(args[0], &addr);
2760 if (ERROR_OK != retval)
2763 target_t *target = get_current_target(cmd_ctx);
2764 watchpoint_remove(target, addr);
2771 * Translate a virtual address to a physical address.
2773 * The low-level target implementation must have logged a detailed error
2774 * which is forwarded to telnet/GDB session.
2776 static int handle_virt2phys_command(command_context_t *cmd_ctx,
2777 char *cmd, char **args, int argc)
2780 return ERROR_COMMAND_SYNTAX_ERROR;
2783 int retval = parse_u32(args[0], &va);
2784 if (ERROR_OK != retval)
2788 target_t *target = get_current_target(cmd_ctx);
2789 retval = target->type->virt2phys(target, va, &pa);
2790 if (retval == ERROR_OK)
2791 command_print(cmd_ctx, "Physical address 0x%08" PRIx32 "", pa);
2796 static void writeData(FILE *f, const void *data, size_t len)
2798 size_t written = fwrite(data, 1, len, f);
2800 LOG_ERROR("failed to write %zu bytes: %s", len, strerror(errno));
2803 static void writeLong(FILE *f, int l)
2806 for (i = 0; i < 4; i++)
2808 char c = (l >> (i*8))&0xff;
2809 writeData(f, &c, 1);
2814 static void writeString(FILE *f, char *s)
2816 writeData(f, s, strlen(s));
2819 /* Dump a gmon.out histogram file. */
2820 static void writeGmon(uint32_t *samples, uint32_t sampleNum, char *filename)
2823 FILE *f = fopen(filename, "w");
2826 writeString(f, "gmon");
2827 writeLong(f, 0x00000001); /* Version */
2828 writeLong(f, 0); /* padding */
2829 writeLong(f, 0); /* padding */
2830 writeLong(f, 0); /* padding */
2832 uint8_t zero = 0; /* GMON_TAG_TIME_HIST */
2833 writeData(f, &zero, 1);
2835 /* figure out bucket size */
2836 uint32_t min = samples[0];
2837 uint32_t max = samples[0];
2838 for (i = 0; i < sampleNum; i++)
2840 if (min > samples[i])
2844 if (max < samples[i])
2850 int addressSpace = (max-min + 1);
2852 static const uint32_t maxBuckets = 256 * 1024; /* maximum buckets. */
2853 uint32_t length = addressSpace;
2854 if (length > maxBuckets)
2856 length = maxBuckets;
2858 int *buckets = malloc(sizeof(int)*length);
2859 if (buckets == NULL)
2864 memset(buckets, 0, sizeof(int)*length);
2865 for (i = 0; i < sampleNum;i++)
2867 uint32_t address = samples[i];
2868 long long a = address-min;
2869 long long b = length-1;
2870 long long c = addressSpace-1;
2871 int index = (a*b)/c; /* danger!!!! int32 overflows */
2875 /* append binary memory gmon.out &profile_hist_hdr ((char*)&profile_hist_hdr + sizeof(struct gmon_hist_hdr)) */
2876 writeLong(f, min); /* low_pc */
2877 writeLong(f, max); /* high_pc */
2878 writeLong(f, length); /* # of samples */
2879 writeLong(f, 64000000); /* 64MHz */
2880 writeString(f, "seconds");
2881 for (i = 0; i < (15-strlen("seconds")); i++)
2882 writeData(f, &zero, 1);
2883 writeString(f, "s");
2885 /*append binary memory gmon.out profile_hist_data (profile_hist_data + profile_hist_hdr.hist_size) */
2887 char *data = malloc(2*length);
2890 for (i = 0; i < length;i++)
2899 data[i*2 + 1]=(val >> 8)&0xff;
2902 writeData(f, data, length * 2);
2912 /* profiling samples the CPU PC as quickly as OpenOCD is able, which will be used as a random sampling of PC */
2913 static int handle_profile_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2915 target_t *target = get_current_target(cmd_ctx);
2916 struct timeval timeout, now;
2918 gettimeofday(&timeout, NULL);
2921 return ERROR_COMMAND_SYNTAX_ERROR;
2924 int retval = parse_uint(args[0], &offset);
2925 if (ERROR_OK != retval)
2928 timeval_add_time(&timeout, offset, 0);
2930 command_print(cmd_ctx, "Starting profiling. Halting and resuming the target as often as we can...");
2932 static const int maxSample = 10000;
2933 uint32_t *samples = malloc(sizeof(uint32_t)*maxSample);
2934 if (samples == NULL)
2938 /* hopefully it is safe to cache! We want to stop/restart as quickly as possible. */
2939 reg_t *reg = register_get_by_name(target->reg_cache, "pc", 1);
2943 target_poll(target);
2944 if (target->state == TARGET_HALTED)
2946 uint32_t t=*((uint32_t *)reg->value);
2947 samples[numSamples++]=t;
2948 retval = target_resume(target, 1, 0, 0, 0); /* current pc, addr = 0, do not handle breakpoints, not debugging */
2949 target_poll(target);
2950 alive_sleep(10); /* sleep 10ms, i.e. <100 samples/second. */
2951 } else if (target->state == TARGET_RUNNING)
2953 /* We want to quickly sample the PC. */
2954 if ((retval = target_halt(target)) != ERROR_OK)
2961 command_print(cmd_ctx, "Target not halted or running");
2965 if (retval != ERROR_OK)
2970 gettimeofday(&now, NULL);
2971 if ((numSamples >= maxSample) || ((now.tv_sec >= timeout.tv_sec) && (now.tv_usec >= timeout.tv_usec)))
2973 command_print(cmd_ctx, "Profiling completed. %d samples.", numSamples);
2974 if ((retval = target_poll(target)) != ERROR_OK)
2979 if (target->state == TARGET_HALTED)
2981 target_resume(target, 1, 0, 0, 0); /* current pc, addr = 0, do not handle breakpoints, not debugging */
2983 if ((retval = target_poll(target)) != ERROR_OK)
2988 writeGmon(samples, numSamples, args[1]);
2989 command_print(cmd_ctx, "Wrote %s", args[1]);
2998 static int new_int_array_element(Jim_Interp * interp, const char *varname, int idx, uint32_t val)
3001 Jim_Obj *nameObjPtr, *valObjPtr;
3004 namebuf = alloc_printf("%s(%d)", varname, idx);
3008 nameObjPtr = Jim_NewStringObj(interp, namebuf, -1);
3009 valObjPtr = Jim_NewIntObj(interp, val);
3010 if (!nameObjPtr || !valObjPtr)
3016 Jim_IncrRefCount(nameObjPtr);
3017 Jim_IncrRefCount(valObjPtr);
3018 result = Jim_SetVariable(interp, nameObjPtr, valObjPtr);
3019 Jim_DecrRefCount(interp, nameObjPtr);
3020 Jim_DecrRefCount(interp, valObjPtr);
3022 /* printf("%s(%d) <= 0%08x\n", varname, idx, val); */
3026 static int jim_mem2array(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
3028 command_context_t *context;
3031 context = Jim_GetAssocData(interp, "context");
3032 if (context == NULL)
3034 LOG_ERROR("mem2array: no command context");
3037 target = get_current_target(context);
3040 LOG_ERROR("mem2array: no current target");
3044 return target_mem2array(interp, target, argc-1, argv + 1);
3047 static int target_mem2array(Jim_Interp *interp, target_t *target, int argc, Jim_Obj *const *argv)
3055 const char *varname;
3056 uint8_t buffer[4096];
3060 /* argv[1] = name of array to receive the data
3061 * argv[2] = desired width
3062 * argv[3] = memory address
3063 * argv[4] = count of times to read
3066 Jim_WrongNumArgs(interp, 1, argv, "varname width addr nelems");
3069 varname = Jim_GetString(argv[0], &len);
3070 /* given "foo" get space for worse case "foo(%d)" .. add 20 */
3072 e = Jim_GetLong(interp, argv[1], &l);
3078 e = Jim_GetLong(interp, argv[2], &l);
3083 e = Jim_GetLong(interp, argv[3], &l);
3099 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3100 Jim_AppendStrings(interp, Jim_GetResult(interp), "Invalid width param, must be 8/16/32", NULL);
3104 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3105 Jim_AppendStrings(interp, Jim_GetResult(interp), "mem2array: zero width read?", NULL);
3108 if ((addr + (len * width)) < addr) {
3109 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3110 Jim_AppendStrings(interp, Jim_GetResult(interp), "mem2array: addr + len - wraps to zero?", NULL);
3113 /* absurd transfer size? */
3115 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3116 Jim_AppendStrings(interp, Jim_GetResult(interp), "mem2array: absurd > 64K item request", NULL);
3121 ((width == 2) && ((addr & 1) == 0)) ||
3122 ((width == 4) && ((addr & 3) == 0))) {
3126 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3127 sprintf(buf, "mem2array address: 0x%08" PRIx32 " is not aligned for %" PRId32 " byte reads",
3130 Jim_AppendStrings(interp, Jim_GetResult(interp), buf , NULL);
3141 /* Slurp... in buffer size chunks */
3143 count = len; /* in objects.. */
3144 if (count > (sizeof(buffer)/width)) {
3145 count = (sizeof(buffer)/width);
3148 retval = target_read_memory(target, addr, width, count, buffer);
3149 if (retval != ERROR_OK) {
3151 LOG_ERROR("mem2array: Read @ 0x%08x, w=%d, cnt=%d, failed",
3155 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3156 Jim_AppendStrings(interp, Jim_GetResult(interp), "mem2array: cannot read memory", NULL);
3160 v = 0; /* shut up gcc */
3161 for (i = 0 ;i < count ;i++, n++) {
3164 v = target_buffer_get_u32(target, &buffer[i*width]);
3167 v = target_buffer_get_u16(target, &buffer[i*width]);
3170 v = buffer[i] & 0x0ff;
3173 new_int_array_element(interp, varname, n, v);
3179 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3184 static int get_int_array_element(Jim_Interp * interp, const char *varname, int idx, uint32_t *val)
3187 Jim_Obj *nameObjPtr, *valObjPtr;
3191 namebuf = alloc_printf("%s(%d)", varname, idx);
3195 nameObjPtr = Jim_NewStringObj(interp, namebuf, -1);
3202 Jim_IncrRefCount(nameObjPtr);
3203 valObjPtr = Jim_GetVariable(interp, nameObjPtr, JIM_ERRMSG);
3204 Jim_DecrRefCount(interp, nameObjPtr);
3206 if (valObjPtr == NULL)
3209 result = Jim_GetLong(interp, valObjPtr, &l);
3210 /* printf("%s(%d) => 0%08x\n", varname, idx, val); */
3215 static int jim_array2mem(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
3217 command_context_t *context;
3220 context = Jim_GetAssocData(interp, "context");
3221 if (context == NULL) {
3222 LOG_ERROR("array2mem: no command context");
3225 target = get_current_target(context);
3226 if (target == NULL) {
3227 LOG_ERROR("array2mem: no current target");
3231 return target_array2mem(interp,target, argc-1, argv + 1);
3234 static int target_array2mem(Jim_Interp *interp, target_t *target, int argc, Jim_Obj *const *argv)
3242 const char *varname;
3243 uint8_t buffer[4096];
3247 /* argv[1] = name of array to get the data
3248 * argv[2] = desired width
3249 * argv[3] = memory address
3250 * argv[4] = count to write
3253 Jim_WrongNumArgs(interp, 1, argv, "varname width addr nelems");
3256 varname = Jim_GetString(argv[0], &len);
3257 /* given "foo" get space for worse case "foo(%d)" .. add 20 */
3259 e = Jim_GetLong(interp, argv[1], &l);
3265 e = Jim_GetLong(interp, argv[2], &l);
3270 e = Jim_GetLong(interp, argv[3], &l);
3286 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3287 Jim_AppendStrings(interp, Jim_GetResult(interp), "Invalid width param, must be 8/16/32", NULL);
3291 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3292 Jim_AppendStrings(interp, Jim_GetResult(interp), "array2mem: zero width read?", NULL);
3295 if ((addr + (len * width)) < addr) {
3296 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3297 Jim_AppendStrings(interp, Jim_GetResult(interp), "array2mem: addr + len - wraps to zero?", NULL);
3300 /* absurd transfer size? */
3302 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3303 Jim_AppendStrings(interp, Jim_GetResult(interp), "array2mem: absurd > 64K item request", NULL);
3308 ((width == 2) && ((addr & 1) == 0)) ||
3309 ((width == 4) && ((addr & 3) == 0))) {
3313 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3314 sprintf(buf, "array2mem address: 0x%08x is not aligned for %d byte reads",
3317 Jim_AppendStrings(interp, Jim_GetResult(interp), buf , NULL);
3328 /* Slurp... in buffer size chunks */
3330 count = len; /* in objects.. */
3331 if (count > (sizeof(buffer)/width)) {
3332 count = (sizeof(buffer)/width);
3335 v = 0; /* shut up gcc */
3336 for (i = 0 ;i < count ;i++, n++) {
3337 get_int_array_element(interp, varname, n, &v);
3340 target_buffer_set_u32(target, &buffer[i*width], v);
3343 target_buffer_set_u16(target, &buffer[i*width], v);
3346 buffer[i] = v & 0x0ff;
3352 retval = target_write_memory(target, addr, width, count, buffer);
3353 if (retval != ERROR_OK) {
3355 LOG_ERROR("array2mem: Write @ 0x%08x, w=%d, cnt=%d, failed",
3359 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3360 Jim_AppendStrings(interp, Jim_GetResult(interp), "array2mem: cannot read memory", NULL);
3366 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3371 void target_all_handle_event(enum target_event e)
3375 LOG_DEBUG("**all*targets: event: %d, %s",
3377 Jim_Nvp_value2name_simple(nvp_target_event, e)->name);
3379 target = all_targets;
3381 target_handle_event(target, e);
3382 target = target->next;
3386 void target_handle_event(target_t *target, enum target_event e)
3388 target_event_action_t *teap;
3391 teap = target->event_action;
3395 if (teap->event == e) {
3397 LOG_DEBUG("target: (%d) %s (%s) event: %d (%s) action: %s",
3398 target->target_number,
3400 target_get_name(target),
3402 Jim_Nvp_value2name_simple(nvp_target_event, e)->name,
3403 Jim_GetString(teap->body, NULL));
3404 if (Jim_EvalObj(interp, teap->body) != JIM_OK)
3406 Jim_PrintErrorMessage(interp);
3412 LOG_DEBUG("event: %d %s - no action",
3414 Jim_Nvp_value2name_simple(nvp_target_event, e)->name);
3418 enum target_cfg_param {
3421 TCFG_WORK_AREA_VIRT,
3422 TCFG_WORK_AREA_PHYS,
3423 TCFG_WORK_AREA_SIZE,
3424 TCFG_WORK_AREA_BACKUP,
3427 TCFG_CHAIN_POSITION,
3430 static Jim_Nvp nvp_config_opts[] = {
3431 { .name = "-type", .value = TCFG_TYPE },
3432 { .name = "-event", .value = TCFG_EVENT },
3433 { .name = "-work-area-virt", .value = TCFG_WORK_AREA_VIRT },
3434 { .name = "-work-area-phys", .value = TCFG_WORK_AREA_PHYS },
3435 { .name = "-work-area-size", .value = TCFG_WORK_AREA_SIZE },
3436 { .name = "-work-area-backup", .value = TCFG_WORK_AREA_BACKUP },
3437 { .name = "-endian" , .value = TCFG_ENDIAN },
3438 { .name = "-variant", .value = TCFG_VARIANT },
3439 { .name = "-chain-position", .value = TCFG_CHAIN_POSITION },
3441 { .name = NULL, .value = -1 }
3444 static int target_configure(Jim_GetOptInfo *goi, target_t *target)
3452 /* parse config or cget options ... */
3453 while (goi->argc > 0) {
3454 Jim_SetEmptyResult(goi->interp);
3455 /* Jim_GetOpt_Debug(goi); */
3457 if (target->type->target_jim_configure) {
3458 /* target defines a configure function */
3459 /* target gets first dibs on parameters */
3460 e = (*(target->type->target_jim_configure))(target, goi);
3469 /* otherwise we 'continue' below */
3471 e = Jim_GetOpt_Nvp(goi, nvp_config_opts, &n);
3473 Jim_GetOpt_NvpUnknown(goi, nvp_config_opts, 0);
3479 if (goi->isconfigure) {
3480 Jim_SetResult_sprintf(goi->interp, "not setable: %s", n->name);
3484 if (goi->argc != 0) {
3485 Jim_WrongNumArgs(goi->interp, goi->argc, goi->argv, "NO PARAMS");
3489 Jim_SetResultString(goi->interp, target_get_name(target), -1);
3493 if (goi->argc == 0) {
3494 Jim_WrongNumArgs(goi->interp, goi->argc, goi->argv, "-event ?event-name? ...");
3498 e = Jim_GetOpt_Nvp(goi, nvp_target_event, &n);
3500 Jim_GetOpt_NvpUnknown(goi, nvp_target_event, 1);
3504 if (goi->isconfigure) {
3505 if (goi->argc != 1) {
3506 Jim_WrongNumArgs(goi->interp, goi->argc, goi->argv, "-event ?event-name? ?EVENT-BODY?");
3510 if (goi->argc != 0) {
3511 Jim_WrongNumArgs(goi->interp, goi->argc, goi->argv, "-event ?event-name?");
3517 target_event_action_t *teap;
3519 teap = target->event_action;
3520 /* replace existing? */
3522 if (teap->event == (enum target_event)n->value) {
3528 if (goi->isconfigure) {
3531 teap = calloc(1, sizeof(*teap));
3533 teap->event = n->value;
3534 Jim_GetOpt_Obj(goi, &o);
3536 Jim_DecrRefCount(interp, teap->body);
3538 teap->body = Jim_DuplicateObj(goi->interp, o);
3541 * Tcl/TK - "tk events" have a nice feature.
3542 * See the "BIND" command.
3543 * We should support that here.
3544 * You can specify %X and %Y in the event code.
3545 * The idea is: %T - target name.
3546 * The idea is: %N - target number
3547 * The idea is: %E - event name.
3549 Jim_IncrRefCount(teap->body);
3551 /* add to head of event list */
3552 teap->next = target->event_action;
3553 target->event_action = teap;
3554 Jim_SetEmptyResult(goi->interp);
3558 Jim_SetEmptyResult(goi->interp);
3560 Jim_SetResult(goi->interp, Jim_DuplicateObj(goi->interp, teap->body));
3567 case TCFG_WORK_AREA_VIRT:
3568 if (goi->isconfigure) {
3569 target_free_all_working_areas(target);
3570 e = Jim_GetOpt_Wide(goi, &w);
3574 target->working_area_virt = w;
3576 if (goi->argc != 0) {
3580 Jim_SetResult(interp, Jim_NewIntObj(goi->interp, target->working_area_virt));
3584 case TCFG_WORK_AREA_PHYS:
3585 if (goi->isconfigure) {
3586 target_free_all_working_areas(target);
3587 e = Jim_GetOpt_Wide(goi, &w);
3591 target->working_area_phys = w;
3593 if (goi->argc != 0) {
3597 Jim_SetResult(interp, Jim_NewIntObj(goi->interp, target->working_area_phys));
3601 case TCFG_WORK_AREA_SIZE:
3602 if (goi->isconfigure) {
3603 target_free_all_working_areas(target);
3604 e = Jim_GetOpt_Wide(goi, &w);
3608 target->working_area_size = w;
3610 if (goi->argc != 0) {
3614 Jim_SetResult(interp, Jim_NewIntObj(goi->interp, target->working_area_size));
3618 case TCFG_WORK_AREA_BACKUP:
3619 if (goi->isconfigure) {
3620 target_free_all_working_areas(target);
3621 e = Jim_GetOpt_Wide(goi, &w);
3625 /* make this exactly 1 or 0 */
3626 target->backup_working_area = (!!w);
3628 if (goi->argc != 0) {
3632 Jim_SetResult(interp, Jim_NewIntObj(goi->interp, target->backup_working_area));
3633 /* loop for more e*/
3637 if (goi->isconfigure) {
3638 e = Jim_GetOpt_Nvp(goi, nvp_target_endian, &n);
3640 Jim_GetOpt_NvpUnknown(goi, nvp_target_endian, 1);
3643 target->endianness = n->value;
3645 if (goi->argc != 0) {
3649 n = Jim_Nvp_value2name_simple(nvp_target_endian, target->endianness);
3650 if (n->name == NULL) {
3651 target->endianness = TARGET_LITTLE_ENDIAN;
3652 n = Jim_Nvp_value2name_simple(nvp_target_endian, target->endianness);
3654 Jim_SetResultString(goi->interp, n->name, -1);
3659 if (goi->isconfigure) {
3660 if (goi->argc < 1) {
3661 Jim_SetResult_sprintf(goi->interp,
3666 if (target->variant) {
3667 free((void *)(target->variant));
3669 e = Jim_GetOpt_String(goi, &cp, NULL);
3670 target->variant = strdup(cp);
3672 if (goi->argc != 0) {
3676 Jim_SetResultString(goi->interp, target->variant,-1);
3679 case TCFG_CHAIN_POSITION:
3680 if (goi->isconfigure) {
3683 target_free_all_working_areas(target);
3684 e = Jim_GetOpt_Obj(goi, &o);
3688 tap = jtag_tap_by_jim_obj(goi->interp, o);
3692 /* make this exactly 1 or 0 */
3695 if (goi->argc != 0) {
3699 Jim_SetResultString(interp, target->tap->dotted_name, -1);
3700 /* loop for more e*/
3703 } /* while (goi->argc) */
3706 /* done - we return */
3710 /** this is the 'tcl' handler for the target specific command */
3711 static int tcl_target_func(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
3716 uint8_t target_buf[32];
3719 struct command_context_s *cmd_ctx;
3726 TS_CMD_MWW, TS_CMD_MWH, TS_CMD_MWB,
3727 TS_CMD_MDW, TS_CMD_MDH, TS_CMD_MDB,
3728 TS_CMD_MRW, TS_CMD_MRH, TS_CMD_MRB,
3729 TS_CMD_MEM2ARRAY, TS_CMD_ARRAY2MEM,
3737 TS_CMD_INVOKE_EVENT,
3740 static const Jim_Nvp target_options[] = {
3741 { .name = "configure", .value = TS_CMD_CONFIGURE },
3742 { .name = "cget", .value = TS_CMD_CGET },
3743 { .name = "mww", .value = TS_CMD_MWW },
3744 { .name = "mwh", .value = TS_CMD_MWH },
3745 { .name = "mwb", .value = TS_CMD_MWB },
3746 { .name = "mdw", .value = TS_CMD_MDW },
3747 { .name = "mdh", .value = TS_CMD_MDH },
3748 { .name = "mdb", .value = TS_CMD_MDB },
3749 { .name = "mem2array", .value = TS_CMD_MEM2ARRAY },
3750 { .name = "array2mem", .value = TS_CMD_ARRAY2MEM },
3751 { .name = "eventlist", .value = TS_CMD_EVENTLIST },
3752 { .name = "curstate", .value = TS_CMD_CURSTATE },
3754 { .name = "arp_examine", .value = TS_CMD_EXAMINE },
3755 { .name = "arp_poll", .value = TS_CMD_POLL },
3756 { .name = "arp_reset", .value = TS_CMD_RESET },
3757 { .name = "arp_halt", .value = TS_CMD_HALT },
3758 { .name = "arp_waitstate", .value = TS_CMD_WAITSTATE },
3759 { .name = "invoke-event", .value = TS_CMD_INVOKE_EVENT },
3761 { .name = NULL, .value = -1 },
3764 /* go past the "command" */
3765 Jim_GetOpt_Setup(&goi, interp, argc-1, argv + 1);
3767 target = Jim_CmdPrivData(goi.interp);
3768 cmd_ctx = Jim_GetAssocData(goi.interp, "context");
3770 /* commands here are in an NVP table */
3771 e = Jim_GetOpt_Nvp(&goi, target_options, &n);
3773 Jim_GetOpt_NvpUnknown(&goi, target_options, 0);
3776 /* Assume blank result */
3777 Jim_SetEmptyResult(goi.interp);
3780 case TS_CMD_CONFIGURE:
3782 Jim_WrongNumArgs(goi.interp, goi.argc, goi.argv, "missing: -option VALUE ...");
3785 goi.isconfigure = 1;
3786 return target_configure(&goi, target);
3788 // some things take params
3790 Jim_WrongNumArgs(goi.interp, 0, goi.argv, "missing: ?-option?");
3793 goi.isconfigure = 0;
3794 return target_configure(&goi, target);
3802 * argv[3] = optional count.
3805 if ((goi.argc == 2) || (goi.argc == 3)) {
3809 Jim_SetResult_sprintf(goi.interp, "expected: %s ADDR DATA [COUNT]", n->name);
3813 e = Jim_GetOpt_Wide(&goi, &a);
3818 e = Jim_GetOpt_Wide(&goi, &b);
3822 if (goi.argc == 3) {
3823 e = Jim_GetOpt_Wide(&goi, &c);
3833 target_buffer_set_u32(target, target_buf, b);
3837 target_buffer_set_u16(target, target_buf, b);
3841 target_buffer_set_u8(target, target_buf, b);
3845 for (x = 0 ; x < c ; x++) {
3846 e = target_write_memory(target, a, b, 1, target_buf);
3847 if (e != ERROR_OK) {
3848 Jim_SetResult_sprintf(interp, "Error writing @ 0x%08x: %d\n", (int)(a), e);
3861 /* argv[0] = command
3863 * argv[2] = optional count
3865 if ((goi.argc == 2) || (goi.argc == 3)) {
3866 Jim_SetResult_sprintf(goi.interp, "expected: %s ADDR [COUNT]", n->name);
3869 e = Jim_GetOpt_Wide(&goi, &a);
3874 e = Jim_GetOpt_Wide(&goi, &c);
3881 b = 1; /* shut up gcc */
3894 /* convert to "bytes" */
3896 /* count is now in 'BYTES' */
3902 e = target_read_memory(target, a, b, y / b, target_buf);
3903 if (e != ERROR_OK) {
3904 Jim_SetResult_sprintf(interp, "error reading target @ 0x%08lx", (int)(a));
3908 Jim_fprintf(interp, interp->cookie_stdout, "0x%08x ", (int)(a));
3911 for (x = 0 ; (x < 16) && (x < y) ; x += 4) {
3912 z = target_buffer_get_u32(target, &(target_buf[ x * 4 ]));
3913 Jim_fprintf(interp, interp->cookie_stdout, "%08x ", (int)(z));
3915 for (; (x < 16) ; x += 4) {
3916 Jim_fprintf(interp, interp->cookie_stdout, " ");
3920 for (x = 0 ; (x < 16) && (x < y) ; x += 2) {
3921 z = target_buffer_get_u16(target, &(target_buf[ x * 2 ]));
3922 Jim_fprintf(interp, interp->cookie_stdout, "%04x ", (int)(z));
3924 for (; (x < 16) ; x += 2) {
3925 Jim_fprintf(interp, interp->cookie_stdout, " ");
3930 for (x = 0 ; (x < 16) && (x < y) ; x += 1) {
3931 z = target_buffer_get_u8(target, &(target_buf[ x * 4 ]));
3932 Jim_fprintf(interp, interp->cookie_stdout, "%02x ", (int)(z));
3934 for (; (x < 16) ; x += 1) {
3935 Jim_fprintf(interp, interp->cookie_stdout, " ");
3939 /* ascii-ify the bytes */
3940 for (x = 0 ; x < y ; x++) {
3941 if ((target_buf[x] >= 0x20) &&
3942 (target_buf[x] <= 0x7e)) {
3946 target_buf[x] = '.';
3951 target_buf[x] = ' ';
3956 /* print - with a newline */
3957 Jim_fprintf(interp, interp->cookie_stdout, "%s\n", target_buf);
3963 case TS_CMD_MEM2ARRAY:
3964 return target_mem2array(goi.interp, target, goi.argc, goi.argv);
3966 case TS_CMD_ARRAY2MEM:
3967 return target_array2mem(goi.interp, target, goi.argc, goi.argv);
3969 case TS_CMD_EXAMINE:
3971 Jim_WrongNumArgs(goi.interp, 2, argv, "[no parameters]");
3974 if (!target->tap->enabled)
3975 goto err_tap_disabled;
3976 e = target->type->examine(target);
3977 if (e != ERROR_OK) {
3978 Jim_SetResult_sprintf(interp, "examine-fails: %d", e);
3984 Jim_WrongNumArgs(goi.interp, 2, argv, "[no parameters]");
3987 if (!target->tap->enabled)
3988 goto err_tap_disabled;
3989 if (!(target_was_examined(target))) {
3990 e = ERROR_TARGET_NOT_EXAMINED;
3992 e = target->type->poll(target);
3994 if (e != ERROR_OK) {
3995 Jim_SetResult_sprintf(interp, "poll-fails: %d", e);
4002 if (goi.argc != 2) {
4003 Jim_WrongNumArgs(interp, 2, argv, "t | f|assert | deassert BOOL");
4006 e = Jim_GetOpt_Nvp(&goi, nvp_assert, &n);
4008 Jim_GetOpt_NvpUnknown(&goi, nvp_assert, 1);
4011 /* the halt or not param */
4012 e = Jim_GetOpt_Wide(&goi, &a);
4016 if (!target->tap->enabled)
4017 goto err_tap_disabled;
4018 /* determine if we should halt or not. */
4019 target->reset_halt = !!a;
4020 /* When this happens - all workareas are invalid. */
4021 target_free_all_working_areas_restore(target, 0);
4024 if (n->value == NVP_ASSERT) {
4025 target->type->assert_reset(target);
4027 target->type->deassert_reset(target);
4032 Jim_WrongNumArgs(goi.interp, 0, argv, "halt [no parameters]");
4035 if (!target->tap->enabled)
4036 goto err_tap_disabled;
4037 target->type->halt(target);
4039 case TS_CMD_WAITSTATE:
4040 /* params: <name> statename timeoutmsecs */
4041 if (goi.argc != 2) {
4042 Jim_SetResult_sprintf(goi.interp, "%s STATENAME TIMEOUTMSECS", n->name);
4045 e = Jim_GetOpt_Nvp(&goi, nvp_target_state, &n);
4047 Jim_GetOpt_NvpUnknown(&goi, nvp_target_state,1);
4050 e = Jim_GetOpt_Wide(&goi, &a);
4054 if (!target->tap->enabled)
4055 goto err_tap_disabled;
4056 e = target_wait_state(target, n->value, a);
4057 if (e != ERROR_OK) {
4058 Jim_SetResult_sprintf(goi.interp,
4059 "target: %s wait %s fails (%d) %s",
4062 e, target_strerror_safe(e));
4067 case TS_CMD_EVENTLIST:
4068 /* List for human, Events defined for this target.
4069 * scripts/programs should use 'name cget -event NAME'
4072 target_event_action_t *teap;
4073 teap = target->event_action;
4074 command_print(cmd_ctx, "Event actions for target (%d) %s\n",
4075 target->target_number,
4077 command_print(cmd_ctx, "%-25s | Body", "Event");
4078 command_print(cmd_ctx, "------------------------- | ----------------------------------------");
4080 command_print(cmd_ctx,
4082 Jim_Nvp_value2name_simple(nvp_target_event, teap->event)->name,
4083 Jim_GetString(teap->body, NULL));
4086 command_print(cmd_ctx, "***END***");
4089 case TS_CMD_CURSTATE:
4090 if (goi.argc != 0) {
4091 Jim_WrongNumArgs(goi.interp, 0, argv, "[no parameters]");
4094 Jim_SetResultString(goi.interp,
4095 target_state_name( target ),
4098 case TS_CMD_INVOKE_EVENT:
4099 if (goi.argc != 1) {
4100 Jim_SetResult_sprintf(goi.interp, "%s ?EVENTNAME?",n->name);
4103 e = Jim_GetOpt_Nvp(&goi, nvp_target_event, &n);
4105 Jim_GetOpt_NvpUnknown(&goi, nvp_target_event, 1);
4108 target_handle_event(target, n->value);
4114 Jim_SetResult_sprintf(interp, "[TAP is disabled]");
4118 static int target_create(Jim_GetOptInfo *goi)
4127 struct command_context_s *cmd_ctx;
4129 cmd_ctx = Jim_GetAssocData(goi->interp, "context");
4130 if (goi->argc < 3) {
4131 Jim_WrongNumArgs(goi->interp, 1, goi->argv, "?name? ?type? ..options...");
4136 Jim_GetOpt_Obj(goi, &new_cmd);
4137 /* does this command exist? */
4138 cmd = Jim_GetCommand(goi->interp, new_cmd, JIM_ERRMSG);
4140 cp = Jim_GetString(new_cmd, NULL);
4141 Jim_SetResult_sprintf(goi->interp, "Command/target: %s Exists", cp);
4146 e = Jim_GetOpt_String(goi, &cp2, NULL);
4148 /* now does target type exist */
4149 for (x = 0 ; target_types[x] ; x++) {
4150 if (0 == strcmp(cp, target_types[x]->name)) {
4155 if (target_types[x] == NULL) {
4156 Jim_SetResult_sprintf(goi->interp, "Unknown target type %s, try one of ", cp);
4157 for (x = 0 ; target_types[x] ; x++) {
4158 if (target_types[x + 1]) {
4159 Jim_AppendStrings(goi->interp,
4160 Jim_GetResult(goi->interp),
4161 target_types[x]->name,
4164 Jim_AppendStrings(goi->interp,
4165 Jim_GetResult(goi->interp),
4167 target_types[x]->name,NULL);
4174 target = calloc(1,sizeof(target_t));
4175 /* set target number */
4176 target->target_number = new_target_number();
4178 /* allocate memory for each unique target type */
4179 target->type = (target_type_t*)calloc(1,sizeof(target_type_t));
4181 memcpy(target->type, target_types[x], sizeof(target_type_t));
4183 /* will be set by "-endian" */
4184 target->endianness = TARGET_ENDIAN_UNKNOWN;
4186 target->working_area = 0x0;
4187 target->working_area_size = 0x0;
4188 target->working_areas = NULL;
4189 target->backup_working_area = 0;
4191 target->state = TARGET_UNKNOWN;
4192 target->debug_reason = DBG_REASON_UNDEFINED;
4193 target->reg_cache = NULL;
4194 target->breakpoints = NULL;
4195 target->watchpoints = NULL;
4196 target->next = NULL;
4197 target->arch_info = NULL;
4199 target->display = 1;
4201 /* initialize trace information */
4202 target->trace_info = malloc(sizeof(trace_t));
4203 target->trace_info->num_trace_points = 0;
4204 target->trace_info->trace_points_size = 0;
4205 target->trace_info->trace_points = NULL;
4206 target->trace_info->trace_history_size = 0;
4207 target->trace_info->trace_history = NULL;
4208 target->trace_info->trace_history_pos = 0;
4209 target->trace_info->trace_history_overflowed = 0;
4211 target->dbgmsg = NULL;
4212 target->dbg_msg_enabled = 0;
4214 target->endianness = TARGET_ENDIAN_UNKNOWN;
4216 /* Do the rest as "configure" options */
4217 goi->isconfigure = 1;
4218 e = target_configure(goi, target);
4220 if (target->tap == NULL)
4222 Jim_SetResultString(interp, "-chain-position required when creating target", -1);
4232 if (target->endianness == TARGET_ENDIAN_UNKNOWN) {
4233 /* default endian to little if not specified */
4234 target->endianness = TARGET_LITTLE_ENDIAN;
4237 /* incase variant is not set */
4238 if (!target->variant)
4239 target->variant = strdup("");
4241 /* create the target specific commands */
4242 if (target->type->register_commands) {
4243 (*(target->type->register_commands))(cmd_ctx);
4245 if (target->type->target_create) {
4246 (*(target->type->target_create))(target, goi->interp);
4249 /* append to end of list */
4252 tpp = &(all_targets);
4254 tpp = &((*tpp)->next);
4259 cp = Jim_GetString(new_cmd, NULL);
4260 target->cmd_name = strdup(cp);
4262 /* now - create the new target name command */
4263 e = Jim_CreateCommand(goi->interp,
4266 tcl_target_func, /* C function */
4267 target, /* private data */
4268 NULL); /* no del proc */
4273 static int jim_target(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
4277 struct command_context_s *cmd_ctx;
4281 /* TG = target generic */
4289 const char *target_cmds[] = {
4290 "create", "types", "names", "current", "number",
4292 NULL /* terminate */
4295 LOG_DEBUG("Target command params:");
4296 LOG_DEBUG("%s", Jim_Debug_ArgvString(interp, argc, argv));
4298 cmd_ctx = Jim_GetAssocData(interp, "context");
4300 Jim_GetOpt_Setup(&goi, interp, argc-1, argv + 1);
4302 if (goi.argc == 0) {
4303 Jim_WrongNumArgs(interp, 1, argv, "missing: command ...");
4307 /* Jim_GetOpt_Debug(&goi); */
4308 r = Jim_GetOpt_Enum(&goi, target_cmds, &x);
4315 Jim_Panic(goi.interp,"Why am I here?");
4317 case TG_CMD_CURRENT:
4318 if (goi.argc != 0) {
4319 Jim_WrongNumArgs(goi.interp, 1, goi.argv, "Too many parameters");
4322 Jim_SetResultString(goi.interp, get_current_target(cmd_ctx)->cmd_name, -1);
4325 if (goi.argc != 0) {
4326 Jim_WrongNumArgs(goi.interp, 1, goi.argv, "Too many parameters");
4329 Jim_SetResult(goi.interp, Jim_NewListObj(goi.interp, NULL, 0));
4330 for (x = 0 ; target_types[x] ; x++) {
4331 Jim_ListAppendElement(goi.interp,
4332 Jim_GetResult(goi.interp),
4333 Jim_NewStringObj(goi.interp, target_types[x]->name, -1));
4337 if (goi.argc != 0) {
4338 Jim_WrongNumArgs(goi.interp, 1, goi.argv, "Too many parameters");
4341 Jim_SetResult(goi.interp, Jim_NewListObj(goi.interp, NULL, 0));
4342 target = all_targets;
4344 Jim_ListAppendElement(goi.interp,
4345 Jim_GetResult(goi.interp),
4346 Jim_NewStringObj(goi.interp, target->cmd_name, -1));
4347 target = target->next;
4352 Jim_WrongNumArgs(goi.interp, goi.argc, goi.argv, "?name ... config options ...");
4355 return target_create(&goi);
4358 if (goi.argc != 1) {
4359 Jim_SetResult_sprintf(goi.interp, "expected: target number ?NUMBER?");
4362 e = Jim_GetOpt_Wide(&goi, &w);
4368 t = get_target_by_num(w);
4370 Jim_SetResult_sprintf(goi.interp,"Target: number %d does not exist", (int)(w));
4373 Jim_SetResultString(goi.interp, t->cmd_name, -1);
4377 if (goi.argc != 0) {
4378 Jim_WrongNumArgs(goi.interp, 0, goi.argv, "<no parameters>");
4381 Jim_SetResult(goi.interp,
4382 Jim_NewIntObj(goi.interp, max_target_number()));
4398 static int fastload_num;
4399 static struct FastLoad *fastload;
4401 static void free_fastload(void)
4403 if (fastload != NULL)
4406 for (i = 0; i < fastload_num; i++)
4408 if (fastload[i].data)
4409 free(fastload[i].data);
4419 static int handle_fast_load_image_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
4423 uint32_t image_size;
4424 uint32_t min_address = 0;
4425 uint32_t max_address = 0xffffffff;
4430 duration_t duration;
4431 char *duration_text;
4433 int retval = parse_load_image_command_args(args, argc,
4434 &image, &min_address, &max_address);
4435 if (ERROR_OK != retval)
4438 duration_start_measure(&duration);
4440 if (image_open(&image, args[0], (argc >= 3) ? args[2] : NULL) != ERROR_OK)
4447 fastload_num = image.num_sections;
4448 fastload = (struct FastLoad *)malloc(sizeof(struct FastLoad)*image.num_sections);
4449 if (fastload == NULL)
4451 image_close(&image);
4454 memset(fastload, 0, sizeof(struct FastLoad)*image.num_sections);
4455 for (i = 0; i < image.num_sections; i++)
4457 buffer = malloc(image.sections[i].size);
4460 command_print(cmd_ctx, "error allocating buffer for section (%d bytes)",
4461 (int)(image.sections[i].size));
4465 if ((retval = image_read_section(&image, i, 0x0, image.sections[i].size, buffer, &buf_cnt)) != ERROR_OK)
4471 uint32_t offset = 0;
4472 uint32_t length = buf_cnt;
4475 /* DANGER!!! beware of unsigned comparision here!!! */
4477 if ((image.sections[i].base_address + buf_cnt >= min_address)&&
4478 (image.sections[i].base_address < max_address))
4480 if (image.sections[i].base_address < min_address)
4482 /* clip addresses below */
4483 offset += min_address-image.sections[i].base_address;
4487 if (image.sections[i].base_address + buf_cnt > max_address)
4489 length -= (image.sections[i].base_address + buf_cnt)-max_address;
4492 fastload[i].address = image.sections[i].base_address + offset;
4493 fastload[i].data = malloc(length);
4494 if (fastload[i].data == NULL)
4499 memcpy(fastload[i].data, buffer + offset, length);
4500 fastload[i].length = length;
4502 image_size += length;
4503 command_print(cmd_ctx, "%u byte written at address 0x%8.8x",
4504 (unsigned int)length,
4505 ((unsigned int)(image.sections[i].base_address + offset)));
4511 duration_stop_measure(&duration, &duration_text);
4512 if (retval == ERROR_OK)
4514 command_print(cmd_ctx, "Loaded %u bytes in %s", (unsigned int)image_size, duration_text);
4515 command_print(cmd_ctx, "NB!!! image has not been loaded to target, issue a subsequent 'fast_load' to do so.");
4517 free(duration_text);
4519 image_close(&image);
4521 if (retval != ERROR_OK)
4529 static int handle_fast_load_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
4532 return ERROR_COMMAND_SYNTAX_ERROR;
4533 if (fastload == NULL)
4535 LOG_ERROR("No image in memory");
4539 int ms = timeval_ms();
4541 int retval = ERROR_OK;
4542 for (i = 0; i < fastload_num;i++)
4544 target_t *target = get_current_target(cmd_ctx);
4545 command_print(cmd_ctx, "Write to 0x%08x, length 0x%08x",
4546 (unsigned int)(fastload[i].address),
4547 (unsigned int)(fastload[i].length));
4548 if (retval == ERROR_OK)
4550 retval = target_write_buffer(target, fastload[i].address, fastload[i].length, fastload[i].data);
4552 size += fastload[i].length;
4554 int after = timeval_ms();
4555 command_print(cmd_ctx, "Loaded image %f kBytes/s", (float)(size/1024.0)/((float)(after-ms)/1000.0));