1 /***************************************************************************
2 * Copyright (C) 2005 by Dominic Rath *
3 * Dominic.Rath@gmx.de *
5 * Copyright (C) 2007-2009 Ø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 dragonite_target;
88 extern target_type_t xscale_target;
89 extern target_type_t cortexm3_target;
90 extern target_type_t cortexa8_target;
91 extern target_type_t arm11_target;
92 extern target_type_t mips_m4k_target;
93 extern target_type_t avr_target;
95 target_type_t *target_types[] =
115 target_t *all_targets = NULL;
116 target_event_callback_t *target_event_callbacks = NULL;
117 target_timer_callback_t *target_timer_callbacks = NULL;
119 const Jim_Nvp nvp_assert[] = {
120 { .name = "assert", NVP_ASSERT },
121 { .name = "deassert", NVP_DEASSERT },
122 { .name = "T", NVP_ASSERT },
123 { .name = "F", NVP_DEASSERT },
124 { .name = "t", NVP_ASSERT },
125 { .name = "f", NVP_DEASSERT },
126 { .name = NULL, .value = -1 }
129 const Jim_Nvp nvp_error_target[] = {
130 { .value = ERROR_TARGET_INVALID, .name = "err-invalid" },
131 { .value = ERROR_TARGET_INIT_FAILED, .name = "err-init-failed" },
132 { .value = ERROR_TARGET_TIMEOUT, .name = "err-timeout" },
133 { .value = ERROR_TARGET_NOT_HALTED, .name = "err-not-halted" },
134 { .value = ERROR_TARGET_FAILURE, .name = "err-failure" },
135 { .value = ERROR_TARGET_UNALIGNED_ACCESS , .name = "err-unaligned-access" },
136 { .value = ERROR_TARGET_DATA_ABORT , .name = "err-data-abort" },
137 { .value = ERROR_TARGET_RESOURCE_NOT_AVAILABLE , .name = "err-resource-not-available" },
138 { .value = ERROR_TARGET_TRANSLATION_FAULT , .name = "err-translation-fault" },
139 { .value = ERROR_TARGET_NOT_RUNNING, .name = "err-not-running" },
140 { .value = ERROR_TARGET_NOT_EXAMINED, .name = "err-not-examined" },
141 { .value = -1, .name = NULL }
144 const char *target_strerror_safe(int err)
148 n = Jim_Nvp_value2name_simple(nvp_error_target, err);
149 if (n->name == NULL) {
156 static const Jim_Nvp nvp_target_event[] = {
157 { .value = TARGET_EVENT_OLD_gdb_program_config , .name = "old-gdb_program_config" },
158 { .value = TARGET_EVENT_OLD_pre_resume , .name = "old-pre_resume" },
160 { .value = TARGET_EVENT_GDB_HALT, .name = "gdb-halt" },
161 { .value = TARGET_EVENT_HALTED, .name = "halted" },
162 { .value = TARGET_EVENT_RESUMED, .name = "resumed" },
163 { .value = TARGET_EVENT_RESUME_START, .name = "resume-start" },
164 { .value = TARGET_EVENT_RESUME_END, .name = "resume-end" },
166 { .name = "gdb-start", .value = TARGET_EVENT_GDB_START },
167 { .name = "gdb-end", .value = TARGET_EVENT_GDB_END },
169 /* historical name */
171 { .value = TARGET_EVENT_RESET_START, .name = "reset-start" },
173 { .value = TARGET_EVENT_RESET_ASSERT_PRE, .name = "reset-assert-pre" },
174 { .value = TARGET_EVENT_RESET_ASSERT_POST, .name = "reset-assert-post" },
175 { .value = TARGET_EVENT_RESET_DEASSERT_PRE, .name = "reset-deassert-pre" },
176 { .value = TARGET_EVENT_RESET_DEASSERT_POST, .name = "reset-deassert-post" },
177 { .value = TARGET_EVENT_RESET_HALT_PRE, .name = "reset-halt-pre" },
178 { .value = TARGET_EVENT_RESET_HALT_POST, .name = "reset-halt-post" },
179 { .value = TARGET_EVENT_RESET_WAIT_PRE, .name = "reset-wait-pre" },
180 { .value = TARGET_EVENT_RESET_WAIT_POST, .name = "reset-wait-post" },
181 { .value = TARGET_EVENT_RESET_INIT , .name = "reset-init" },
182 { .value = TARGET_EVENT_RESET_END, .name = "reset-end" },
184 { .value = TARGET_EVENT_EXAMINE_START, .name = "examine-start" },
185 { .value = TARGET_EVENT_EXAMINE_END, .name = "examine-end" },
187 { .value = TARGET_EVENT_DEBUG_HALTED, .name = "debug-halted" },
188 { .value = TARGET_EVENT_DEBUG_RESUMED, .name = "debug-resumed" },
190 { .value = TARGET_EVENT_GDB_ATTACH, .name = "gdb-attach" },
191 { .value = TARGET_EVENT_GDB_DETACH, .name = "gdb-detach" },
193 { .value = TARGET_EVENT_GDB_FLASH_WRITE_START, .name = "gdb-flash-write-start" },
194 { .value = TARGET_EVENT_GDB_FLASH_WRITE_END , .name = "gdb-flash-write-end" },
196 { .value = TARGET_EVENT_GDB_FLASH_ERASE_START, .name = "gdb-flash-erase-start" },
197 { .value = TARGET_EVENT_GDB_FLASH_ERASE_END , .name = "gdb-flash-erase-end" },
199 { .value = TARGET_EVENT_RESUME_START, .name = "resume-start" },
200 { .value = TARGET_EVENT_RESUMED , .name = "resume-ok" },
201 { .value = TARGET_EVENT_RESUME_END , .name = "resume-end" },
203 { .name = NULL, .value = -1 }
206 const Jim_Nvp nvp_target_state[] = {
207 { .name = "unknown", .value = TARGET_UNKNOWN },
208 { .name = "running", .value = TARGET_RUNNING },
209 { .name = "halted", .value = TARGET_HALTED },
210 { .name = "reset", .value = TARGET_RESET },
211 { .name = "debug-running", .value = TARGET_DEBUG_RUNNING },
212 { .name = NULL, .value = -1 },
215 const Jim_Nvp nvp_target_debug_reason [] = {
216 { .name = "debug-request" , .value = DBG_REASON_DBGRQ },
217 { .name = "breakpoint" , .value = DBG_REASON_BREAKPOINT },
218 { .name = "watchpoint" , .value = DBG_REASON_WATCHPOINT },
219 { .name = "watchpoint-and-breakpoint", .value = DBG_REASON_WPTANDBKPT },
220 { .name = "single-step" , .value = DBG_REASON_SINGLESTEP },
221 { .name = "target-not-halted" , .value = DBG_REASON_NOTHALTED },
222 { .name = "undefined" , .value = DBG_REASON_UNDEFINED },
223 { .name = NULL, .value = -1 },
226 const Jim_Nvp nvp_target_endian[] = {
227 { .name = "big", .value = TARGET_BIG_ENDIAN },
228 { .name = "little", .value = TARGET_LITTLE_ENDIAN },
229 { .name = "be", .value = TARGET_BIG_ENDIAN },
230 { .name = "le", .value = TARGET_LITTLE_ENDIAN },
231 { .name = NULL, .value = -1 },
234 const Jim_Nvp nvp_reset_modes[] = {
235 { .name = "unknown", .value = RESET_UNKNOWN },
236 { .name = "run" , .value = RESET_RUN },
237 { .name = "halt" , .value = RESET_HALT },
238 { .name = "init" , .value = RESET_INIT },
239 { .name = NULL , .value = -1 },
243 target_state_name( target_t *t )
246 cp = Jim_Nvp_value2name_simple(nvp_target_state, t->state)->name;
248 LOG_ERROR("Invalid target state: %d", (int)(t->state));
249 cp = "(*BUG*unknown*BUG*)";
254 /* determine the number of the new target */
255 static int new_target_number(void)
260 /* number is 0 based */
264 if (x < t->target_number) {
265 x = t->target_number;
272 /* read a uint32_t from a buffer in target memory endianness */
273 uint32_t target_buffer_get_u32(target_t *target, const uint8_t *buffer)
275 if (target->endianness == TARGET_LITTLE_ENDIAN)
276 return le_to_h_u32(buffer);
278 return be_to_h_u32(buffer);
281 /* read a uint16_t from a buffer in target memory endianness */
282 uint16_t target_buffer_get_u16(target_t *target, const uint8_t *buffer)
284 if (target->endianness == TARGET_LITTLE_ENDIAN)
285 return le_to_h_u16(buffer);
287 return be_to_h_u16(buffer);
290 /* read a uint8_t from a buffer in target memory endianness */
291 uint8_t target_buffer_get_u8(target_t *target, const uint8_t *buffer)
293 return *buffer & 0x0ff;
296 /* write a uint32_t to a buffer in target memory endianness */
297 void target_buffer_set_u32(target_t *target, uint8_t *buffer, uint32_t value)
299 if (target->endianness == TARGET_LITTLE_ENDIAN)
300 h_u32_to_le(buffer, value);
302 h_u32_to_be(buffer, value);
305 /* write a uint16_t to a buffer in target memory endianness */
306 void target_buffer_set_u16(target_t *target, uint8_t *buffer, uint16_t value)
308 if (target->endianness == TARGET_LITTLE_ENDIAN)
309 h_u16_to_le(buffer, value);
311 h_u16_to_be(buffer, value);
314 /* write a uint8_t to a buffer in target memory endianness */
315 void target_buffer_set_u8(target_t *target, uint8_t *buffer, uint8_t value)
320 /* return a pointer to a configured target; id is name or number */
321 target_t *get_target(const char *id)
325 /* try as tcltarget name */
326 for (target = all_targets; target; target = target->next) {
327 if (target->cmd_name == NULL)
329 if (strcmp(id, target->cmd_name) == 0)
333 /* It's OK to remove this fallback sometime after August 2010 or so */
335 /* no match, try as number */
337 if (parse_uint(id, &num) != ERROR_OK)
340 for (target = all_targets; target; target = target->next) {
341 if (target->target_number == (int)num) {
342 LOG_WARNING("use '%s' as target identifier, not '%u'",
343 target->cmd_name, num);
351 /* returns a pointer to the n-th configured target */
352 static target_t *get_target_by_num(int num)
354 target_t *target = all_targets;
357 if (target->target_number == num) {
360 target = target->next;
366 target_t* get_current_target(command_context_t *cmd_ctx)
368 target_t *target = get_target_by_num(cmd_ctx->current_target);
372 LOG_ERROR("BUG: current_target out of bounds");
379 int target_poll(struct target_s *target)
383 /* We can't poll until after examine */
384 if (!target_was_examined(target))
386 /* Fail silently lest we pollute the log */
390 retval = target->type->poll(target);
391 if (retval != ERROR_OK)
394 if (target->halt_issued)
396 if (target->state == TARGET_HALTED)
398 target->halt_issued = false;
401 long long t = timeval_ms() - target->halt_issued_time;
404 target->halt_issued = false;
405 LOG_INFO("Halt timed out, wake up GDB.");
406 target_call_event_callbacks(target, TARGET_EVENT_GDB_HALT);
414 int target_halt(struct target_s *target)
417 /* We can't poll until after examine */
418 if (!target_was_examined(target))
420 LOG_ERROR("Target not examined yet");
424 retval = target->type->halt(target);
425 if (retval != ERROR_OK)
428 target->halt_issued = true;
429 target->halt_issued_time = timeval_ms();
434 int target_resume(struct target_s *target, int current, uint32_t address, int handle_breakpoints, int debug_execution)
438 /* We can't poll until after examine */
439 if (!target_was_examined(target))
441 LOG_ERROR("Target not examined yet");
445 /* note that resume *must* be asynchronous. The CPU can halt before we poll. The CPU can
446 * even halt at the current PC as a result of a software breakpoint being inserted by (a bug?)
449 if ((retval = target->type->resume(target, current, address, handle_breakpoints, debug_execution)) != ERROR_OK)
455 int target_process_reset(struct command_context_s *cmd_ctx, enum target_reset_mode reset_mode)
460 n = Jim_Nvp_value2name_simple(nvp_reset_modes, reset_mode);
461 if (n->name == NULL) {
462 LOG_ERROR("invalid reset mode");
466 /* disable polling during reset to make reset event scripts
467 * more predictable, i.e. dr/irscan & pathmove in events will
468 * not have JTAG operations injected into the middle of a sequence.
470 bool save_poll = jtag_poll_get_enabled();
472 jtag_poll_set_enabled(false);
474 sprintf(buf, "ocd_process_reset %s", n->name);
475 retval = Jim_Eval(interp, buf);
477 jtag_poll_set_enabled(save_poll);
479 if (retval != JIM_OK) {
480 Jim_PrintErrorMessage(interp);
484 /* We want any events to be processed before the prompt */
485 retval = target_call_timer_callbacks_now();
490 static int default_virt2phys(struct target_s *target, uint32_t virtual, uint32_t *physical)
496 static int default_mmu(struct target_s *target, int *enabled)
502 static int default_examine(struct target_s *target)
504 target_set_examined(target);
508 int target_examine_one(struct target_s *target)
510 return target->type->examine(target);
513 static int jtag_enable_callback(enum jtag_event event, void *priv)
515 target_t *target = priv;
517 if (event != JTAG_TAP_EVENT_ENABLE || !target->tap->enabled)
520 jtag_unregister_event_callback(jtag_enable_callback, target);
521 return target_examine_one(target);
525 /* Targets that correctly implement init + examine, i.e.
526 * no communication with target during init:
530 int target_examine(void)
532 int retval = ERROR_OK;
535 for (target = all_targets; target; target = target->next)
537 /* defer examination, but don't skip it */
538 if (!target->tap->enabled) {
539 jtag_register_event_callback(jtag_enable_callback,
543 if ((retval = target_examine_one(target)) != ERROR_OK)
548 const char *target_get_name(struct target_s *target)
550 return target->type->name;
553 static int target_write_memory_imp(struct target_s *target, uint32_t address, uint32_t size, uint32_t count, uint8_t *buffer)
555 if (!target_was_examined(target))
557 LOG_ERROR("Target not examined yet");
560 return target->type->write_memory_imp(target, address, size, count, buffer);
563 static int target_read_memory_imp(struct target_s *target, uint32_t address, uint32_t size, uint32_t count, uint8_t *buffer)
565 if (!target_was_examined(target))
567 LOG_ERROR("Target not examined yet");
570 return target->type->read_memory_imp(target, address, size, count, buffer);
573 static int target_soft_reset_halt_imp(struct target_s *target)
575 if (!target_was_examined(target))
577 LOG_ERROR("Target not examined yet");
580 if (!target->type->soft_reset_halt_imp) {
581 LOG_ERROR("Target %s does not support soft_reset_halt",
585 return target->type->soft_reset_halt_imp(target);
588 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)
590 if (!target_was_examined(target))
592 LOG_ERROR("Target not examined yet");
595 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);
598 int target_read_memory(struct target_s *target,
599 uint32_t address, uint32_t size, uint32_t count, uint8_t *buffer)
601 return target->type->read_memory(target, address, size, count, buffer);
604 int target_write_memory(struct target_s *target,
605 uint32_t address, uint32_t size, uint32_t count, uint8_t *buffer)
607 return target->type->write_memory(target, address, size, count, buffer);
609 int target_bulk_write_memory(struct target_s *target,
610 uint32_t address, uint32_t count, uint8_t *buffer)
612 return target->type->bulk_write_memory(target, address, count, buffer);
615 int target_add_breakpoint(struct target_s *target,
616 struct breakpoint_s *breakpoint)
618 return target->type->add_breakpoint(target, breakpoint);
620 int target_remove_breakpoint(struct target_s *target,
621 struct breakpoint_s *breakpoint)
623 return target->type->remove_breakpoint(target, breakpoint);
626 int target_add_watchpoint(struct target_s *target,
627 struct watchpoint_s *watchpoint)
629 return target->type->add_watchpoint(target, watchpoint);
631 int target_remove_watchpoint(struct target_s *target,
632 struct watchpoint_s *watchpoint)
634 return target->type->remove_watchpoint(target, watchpoint);
637 int target_get_gdb_reg_list(struct target_s *target,
638 struct reg_s **reg_list[], int *reg_list_size)
640 return target->type->get_gdb_reg_list(target, reg_list, reg_list_size);
642 int target_step(struct target_s *target,
643 int current, uint32_t address, int handle_breakpoints)
645 return target->type->step(target, current, address, handle_breakpoints);
649 int target_run_algorithm(struct target_s *target,
650 int num_mem_params, mem_param_t *mem_params,
651 int num_reg_params, reg_param_t *reg_param,
652 uint32_t entry_point, uint32_t exit_point,
653 int timeout_ms, void *arch_info)
655 return target->type->run_algorithm(target,
656 num_mem_params, mem_params, num_reg_params, reg_param,
657 entry_point, exit_point, timeout_ms, arch_info);
660 /// @returns @c true if the target has been examined.
661 bool target_was_examined(struct target_s *target)
663 return target->type->examined;
665 /// Sets the @c examined flag for the given target.
666 void target_set_examined(struct target_s *target)
668 target->type->examined = true;
670 // Reset the @c examined flag for the given target.
671 void target_reset_examined(struct target_s *target)
673 target->type->examined = false;
677 int target_init(struct command_context_s *cmd_ctx)
679 target_t *target = all_targets;
684 target_reset_examined(target);
685 if (target->type->examine == NULL)
687 target->type->examine = default_examine;
690 if ((retval = target->type->init_target(cmd_ctx, target)) != ERROR_OK)
692 LOG_ERROR("target '%s' init failed", target_get_name(target));
696 /* Set up default functions if none are provided by target */
697 if (target->type->virt2phys == NULL)
699 target->type->virt2phys = default_virt2phys;
701 target->type->virt2phys = default_virt2phys;
702 /* a non-invasive way(in terms of patches) to add some code that
703 * runs before the type->write/read_memory implementation
705 target->type->write_memory_imp = target->type->write_memory;
706 target->type->write_memory = target_write_memory_imp;
707 target->type->read_memory_imp = target->type->read_memory;
708 target->type->read_memory = target_read_memory_imp;
709 target->type->soft_reset_halt_imp = target->type->soft_reset_halt;
710 target->type->soft_reset_halt = target_soft_reset_halt_imp;
711 target->type->run_algorithm_imp = target->type->run_algorithm;
712 target->type->run_algorithm = target_run_algorithm_imp;
714 if (target->type->mmu == NULL)
716 target->type->mmu = default_mmu;
718 target = target->next;
723 if ((retval = target_register_user_commands(cmd_ctx)) != ERROR_OK)
725 if ((retval = target_register_timer_callback(handle_target, 100, 1, NULL)) != ERROR_OK)
732 int target_register_event_callback(int (*callback)(struct target_s *target, enum target_event event, void *priv), void *priv)
734 target_event_callback_t **callbacks_p = &target_event_callbacks;
736 if (callback == NULL)
738 return ERROR_INVALID_ARGUMENTS;
743 while ((*callbacks_p)->next)
744 callbacks_p = &((*callbacks_p)->next);
745 callbacks_p = &((*callbacks_p)->next);
748 (*callbacks_p) = malloc(sizeof(target_event_callback_t));
749 (*callbacks_p)->callback = callback;
750 (*callbacks_p)->priv = priv;
751 (*callbacks_p)->next = NULL;
756 int target_register_timer_callback(int (*callback)(void *priv), int time_ms, int periodic, void *priv)
758 target_timer_callback_t **callbacks_p = &target_timer_callbacks;
761 if (callback == NULL)
763 return ERROR_INVALID_ARGUMENTS;
768 while ((*callbacks_p)->next)
769 callbacks_p = &((*callbacks_p)->next);
770 callbacks_p = &((*callbacks_p)->next);
773 (*callbacks_p) = malloc(sizeof(target_timer_callback_t));
774 (*callbacks_p)->callback = callback;
775 (*callbacks_p)->periodic = periodic;
776 (*callbacks_p)->time_ms = time_ms;
778 gettimeofday(&now, NULL);
779 (*callbacks_p)->when.tv_usec = now.tv_usec + (time_ms % 1000) * 1000;
780 time_ms -= (time_ms % 1000);
781 (*callbacks_p)->when.tv_sec = now.tv_sec + (time_ms / 1000);
782 if ((*callbacks_p)->when.tv_usec > 1000000)
784 (*callbacks_p)->when.tv_usec = (*callbacks_p)->when.tv_usec - 1000000;
785 (*callbacks_p)->when.tv_sec += 1;
788 (*callbacks_p)->priv = priv;
789 (*callbacks_p)->next = NULL;
794 int target_unregister_event_callback(int (*callback)(struct target_s *target, enum target_event event, void *priv), void *priv)
796 target_event_callback_t **p = &target_event_callbacks;
797 target_event_callback_t *c = target_event_callbacks;
799 if (callback == NULL)
801 return ERROR_INVALID_ARGUMENTS;
806 target_event_callback_t *next = c->next;
807 if ((c->callback == callback) && (c->priv == priv))
821 int target_unregister_timer_callback(int (*callback)(void *priv), void *priv)
823 target_timer_callback_t **p = &target_timer_callbacks;
824 target_timer_callback_t *c = target_timer_callbacks;
826 if (callback == NULL)
828 return ERROR_INVALID_ARGUMENTS;
833 target_timer_callback_t *next = c->next;
834 if ((c->callback == callback) && (c->priv == priv))
848 int target_call_event_callbacks(target_t *target, enum target_event event)
850 target_event_callback_t *callback = target_event_callbacks;
851 target_event_callback_t *next_callback;
853 if (event == TARGET_EVENT_HALTED)
855 /* execute early halted first */
856 target_call_event_callbacks(target, TARGET_EVENT_GDB_HALT);
859 LOG_DEBUG("target event %i (%s)",
861 Jim_Nvp_value2name_simple(nvp_target_event, event)->name);
863 target_handle_event(target, event);
867 next_callback = callback->next;
868 callback->callback(target, event, callback->priv);
869 callback = next_callback;
875 static int target_timer_callback_periodic_restart(
876 target_timer_callback_t *cb, struct timeval *now)
878 int time_ms = cb->time_ms;
879 cb->when.tv_usec = now->tv_usec + (time_ms % 1000) * 1000;
880 time_ms -= (time_ms % 1000);
881 cb->when.tv_sec = now->tv_sec + time_ms / 1000;
882 if (cb->when.tv_usec > 1000000)
884 cb->when.tv_usec = cb->when.tv_usec - 1000000;
885 cb->when.tv_sec += 1;
890 static int target_call_timer_callback(target_timer_callback_t *cb,
893 cb->callback(cb->priv);
896 return target_timer_callback_periodic_restart(cb, now);
898 return target_unregister_timer_callback(cb->callback, cb->priv);
901 static int target_call_timer_callbacks_check_time(int checktime)
906 gettimeofday(&now, NULL);
908 target_timer_callback_t *callback = target_timer_callbacks;
911 // cleaning up may unregister and free this callback
912 target_timer_callback_t *next_callback = callback->next;
914 bool call_it = callback->callback &&
915 ((!checktime && callback->periodic) ||
916 now.tv_sec > callback->when.tv_sec ||
917 (now.tv_sec == callback->when.tv_sec &&
918 now.tv_usec >= callback->when.tv_usec));
922 int retval = target_call_timer_callback(callback, &now);
923 if (retval != ERROR_OK)
927 callback = next_callback;
933 int target_call_timer_callbacks(void)
935 return target_call_timer_callbacks_check_time(1);
938 /* invoke periodic callbacks immediately */
939 int target_call_timer_callbacks_now(void)
941 return target_call_timer_callbacks_check_time(0);
944 int target_alloc_working_area(struct target_s *target, uint32_t size, working_area_t **area)
946 working_area_t *c = target->working_areas;
947 working_area_t *new_wa = NULL;
949 /* Reevaluate working area address based on MMU state*/
950 if (target->working_areas == NULL)
954 retval = target->type->mmu(target, &enabled);
955 if (retval != ERROR_OK)
961 target->working_area = target->working_area_virt;
965 target->working_area = target->working_area_phys;
969 /* only allocate multiples of 4 byte */
972 LOG_ERROR("BUG: code tried to allocate unaligned number of bytes (0x%08x), padding", ((unsigned)(size)));
973 size = (size + 3) & (~3);
976 /* see if there's already a matching working area */
979 if ((c->free) && (c->size == size))
987 /* if not, allocate a new one */
990 working_area_t **p = &target->working_areas;
991 uint32_t first_free = target->working_area;
992 uint32_t free_size = target->working_area_size;
994 LOG_DEBUG("allocating new working area");
996 c = target->working_areas;
999 first_free += c->size;
1000 free_size -= c->size;
1005 if (free_size < size)
1007 LOG_WARNING("not enough working area available(requested %u, free %u)",
1008 (unsigned)(size), (unsigned)(free_size));
1009 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
1012 new_wa = malloc(sizeof(working_area_t));
1013 new_wa->next = NULL;
1014 new_wa->size = size;
1015 new_wa->address = first_free;
1017 if (target->backup_working_area)
1020 new_wa->backup = malloc(new_wa->size);
1021 if ((retval = target_read_memory(target, new_wa->address, 4, new_wa->size / 4, new_wa->backup)) != ERROR_OK)
1023 free(new_wa->backup);
1030 new_wa->backup = NULL;
1033 /* put new entry in list */
1037 /* mark as used, and return the new (reused) area */
1042 new_wa->user = area;
1047 int target_free_working_area_restore(struct target_s *target, working_area_t *area, int restore)
1052 if (restore && target->backup_working_area)
1055 if ((retval = target_write_memory(target, area->address, 4, area->size / 4, area->backup)) != ERROR_OK)
1061 /* mark user pointer invalid */
1068 int target_free_working_area(struct target_s *target, working_area_t *area)
1070 return target_free_working_area_restore(target, area, 1);
1073 /* free resources and restore memory, if restoring memory fails,
1074 * free up resources anyway
1076 void target_free_all_working_areas_restore(struct target_s *target, int restore)
1078 working_area_t *c = target->working_areas;
1082 working_area_t *next = c->next;
1083 target_free_working_area_restore(target, c, restore);
1093 target->working_areas = NULL;
1096 void target_free_all_working_areas(struct target_s *target)
1098 target_free_all_working_areas_restore(target, 1);
1101 int target_register_commands(struct command_context_s *cmd_ctx)
1104 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)");
1109 register_jim(cmd_ctx, "target", jim_target, "configure target");
1114 int target_arch_state(struct target_s *target)
1119 LOG_USER("No target has been configured");
1123 LOG_USER("target state: %s", target_state_name( target ));
1125 if (target->state != TARGET_HALTED)
1128 retval = target->type->arch_state(target);
1132 /* Single aligned words are guaranteed to use 16 or 32 bit access
1133 * mode respectively, otherwise data is handled as quickly as
1136 int target_write_buffer(struct target_s *target, uint32_t address, uint32_t size, uint8_t *buffer)
1139 LOG_DEBUG("writing buffer of %i byte at 0x%8.8x",
1140 (int)size, (unsigned)address);
1142 if (!target_was_examined(target))
1144 LOG_ERROR("Target not examined yet");
1152 if ((address + size - 1) < address)
1154 /* GDB can request this when e.g. PC is 0xfffffffc*/
1155 LOG_ERROR("address + size wrapped(0x%08x, 0x%08x)",
1161 if (((address % 2) == 0) && (size == 2))
1163 return target_write_memory(target, address, 2, 1, buffer);
1166 /* handle unaligned head bytes */
1169 uint32_t unaligned = 4 - (address % 4);
1171 if (unaligned > size)
1174 if ((retval = target_write_memory(target, address, 1, unaligned, buffer)) != ERROR_OK)
1177 buffer += unaligned;
1178 address += unaligned;
1182 /* handle aligned words */
1185 int aligned = size - (size % 4);
1187 /* use bulk writes above a certain limit. This may have to be changed */
1190 if ((retval = target->type->bulk_write_memory(target, address, aligned / 4, buffer)) != ERROR_OK)
1195 if ((retval = target_write_memory(target, address, 4, aligned / 4, buffer)) != ERROR_OK)
1204 /* handle tail writes of less than 4 bytes */
1207 if ((retval = target_write_memory(target, address, 1, size, buffer)) != ERROR_OK)
1214 /* Single aligned words are guaranteed to use 16 or 32 bit access
1215 * mode respectively, otherwise data is handled as quickly as
1218 int target_read_buffer(struct target_s *target, uint32_t address, uint32_t size, uint8_t *buffer)
1221 LOG_DEBUG("reading buffer of %i byte at 0x%8.8x",
1222 (int)size, (unsigned)address);
1224 if (!target_was_examined(target))
1226 LOG_ERROR("Target not examined yet");
1234 if ((address + size - 1) < address)
1236 /* GDB can request this when e.g. PC is 0xfffffffc*/
1237 LOG_ERROR("address + size wrapped(0x%08" PRIx32 ", 0x%08" PRIx32 ")",
1243 if (((address % 2) == 0) && (size == 2))
1245 return target_read_memory(target, address, 2, 1, buffer);
1248 /* handle unaligned head bytes */
1251 uint32_t unaligned = 4 - (address % 4);
1253 if (unaligned > size)
1256 if ((retval = target_read_memory(target, address, 1, unaligned, buffer)) != ERROR_OK)
1259 buffer += unaligned;
1260 address += unaligned;
1264 /* handle aligned words */
1267 int aligned = size - (size % 4);
1269 if ((retval = target_read_memory(target, address, 4, aligned / 4, buffer)) != ERROR_OK)
1277 /*prevent byte access when possible (avoid AHB access limitations in some cases)*/
1280 int aligned = size - (size%2);
1281 retval = target_read_memory(target, address, 2, aligned / 2, buffer);
1282 if (retval != ERROR_OK)
1289 /* handle tail writes of less than 4 bytes */
1292 if ((retval = target_read_memory(target, address, 1, size, buffer)) != ERROR_OK)
1299 int target_checksum_memory(struct target_s *target, uint32_t address, uint32_t size, uint32_t* crc)
1304 uint32_t checksum = 0;
1305 if (!target_was_examined(target))
1307 LOG_ERROR("Target not examined yet");
1311 if ((retval = target->type->checksum_memory(target, address,
1312 size, &checksum)) != ERROR_OK)
1314 buffer = malloc(size);
1317 LOG_ERROR("error allocating buffer for section (%d bytes)", (int)size);
1318 return ERROR_INVALID_ARGUMENTS;
1320 retval = target_read_buffer(target, address, size, buffer);
1321 if (retval != ERROR_OK)
1327 /* convert to target endianess */
1328 for (i = 0; i < (size/sizeof(uint32_t)); i++)
1330 uint32_t target_data;
1331 target_data = target_buffer_get_u32(target, &buffer[i*sizeof(uint32_t)]);
1332 target_buffer_set_u32(target, &buffer[i*sizeof(uint32_t)], target_data);
1335 retval = image_calculate_checksum(buffer, size, &checksum);
1344 int target_blank_check_memory(struct target_s *target, uint32_t address, uint32_t size, uint32_t* blank)
1347 if (!target_was_examined(target))
1349 LOG_ERROR("Target not examined yet");
1353 if (target->type->blank_check_memory == 0)
1354 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
1356 retval = target->type->blank_check_memory(target, address, size, blank);
1361 int target_read_u32(struct target_s *target, uint32_t address, uint32_t *value)
1363 uint8_t value_buf[4];
1364 if (!target_was_examined(target))
1366 LOG_ERROR("Target not examined yet");
1370 int retval = target_read_memory(target, address, 4, 1, value_buf);
1372 if (retval == ERROR_OK)
1374 *value = target_buffer_get_u32(target, value_buf);
1375 LOG_DEBUG("address: 0x%8.8" PRIx32 ", value: 0x%8.8" PRIx32 "",
1382 LOG_DEBUG("address: 0x%8.8" PRIx32 " failed",
1389 int target_read_u16(struct target_s *target, uint32_t address, uint16_t *value)
1391 uint8_t value_buf[2];
1392 if (!target_was_examined(target))
1394 LOG_ERROR("Target not examined yet");
1398 int retval = target_read_memory(target, address, 2, 1, value_buf);
1400 if (retval == ERROR_OK)
1402 *value = target_buffer_get_u16(target, value_buf);
1403 LOG_DEBUG("address: 0x%8.8" PRIx32 ", value: 0x%4.4x",
1410 LOG_DEBUG("address: 0x%8.8" PRIx32 " failed",
1417 int target_read_u8(struct target_s *target, uint32_t address, uint8_t *value)
1419 int retval = target_read_memory(target, address, 1, 1, value);
1420 if (!target_was_examined(target))
1422 LOG_ERROR("Target not examined yet");
1426 if (retval == ERROR_OK)
1428 LOG_DEBUG("address: 0x%8.8" PRIx32 ", value: 0x%2.2x",
1435 LOG_DEBUG("address: 0x%8.8" PRIx32 " failed",
1442 int target_write_u32(struct target_s *target, uint32_t address, uint32_t value)
1445 uint8_t value_buf[4];
1446 if (!target_was_examined(target))
1448 LOG_ERROR("Target not examined yet");
1452 LOG_DEBUG("address: 0x%8.8" PRIx32 ", value: 0x%8.8" PRIx32 "",
1456 target_buffer_set_u32(target, value_buf, value);
1457 if ((retval = target_write_memory(target, address, 4, 1, value_buf)) != ERROR_OK)
1459 LOG_DEBUG("failed: %i", retval);
1465 int target_write_u16(struct target_s *target, uint32_t address, uint16_t value)
1468 uint8_t value_buf[2];
1469 if (!target_was_examined(target))
1471 LOG_ERROR("Target not examined yet");
1475 LOG_DEBUG("address: 0x%8.8" PRIx32 ", value: 0x%8.8x",
1479 target_buffer_set_u16(target, value_buf, value);
1480 if ((retval = target_write_memory(target, address, 2, 1, value_buf)) != ERROR_OK)
1482 LOG_DEBUG("failed: %i", retval);
1488 int target_write_u8(struct target_s *target, uint32_t address, uint8_t value)
1491 if (!target_was_examined(target))
1493 LOG_ERROR("Target not examined yet");
1497 LOG_DEBUG("address: 0x%8.8" PRIx32 ", value: 0x%2.2x",
1500 if ((retval = target_write_memory(target, address, 1, 1, &value)) != ERROR_OK)
1502 LOG_DEBUG("failed: %i", retval);
1508 int target_register_user_commands(struct command_context_s *cmd_ctx)
1510 int retval = ERROR_OK;
1513 /* script procedures */
1514 register_command(cmd_ctx, NULL, "profile", handle_profile_command, COMMAND_EXEC, "profiling samples the CPU PC");
1515 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>");
1516 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>");
1518 register_command(cmd_ctx, NULL, "fast_load_image", handle_fast_load_image_command, COMMAND_ANY,
1519 "same args as load_image, image stored in memory - mainly for profiling purposes");
1521 register_command(cmd_ctx, NULL, "fast_load", handle_fast_load_command, COMMAND_ANY,
1522 "loads active fast load image to current target - mainly for profiling purposes");
1525 register_command(cmd_ctx, NULL, "virt2phys", handle_virt2phys_command, COMMAND_ANY, "translate a virtual address into a physical address");
1526 register_command(cmd_ctx, NULL, "reg", handle_reg_command, COMMAND_EXEC, "display or set a register");
1527 register_command(cmd_ctx, NULL, "poll", handle_poll_command, COMMAND_EXEC, "poll target state");
1528 register_command(cmd_ctx, NULL, "wait_halt", handle_wait_halt_command, COMMAND_EXEC, "wait for target halt [time (s)]");
1529 register_command(cmd_ctx, NULL, "halt", handle_halt_command, COMMAND_EXEC, "halt target");
1530 register_command(cmd_ctx, NULL, "resume", handle_resume_command, COMMAND_EXEC, "resume target [addr]");
1531 register_command(cmd_ctx, NULL, "step", handle_step_command, COMMAND_EXEC, "step one instruction from current PC or [addr]");
1532 register_command(cmd_ctx, NULL, "reset", handle_reset_command, COMMAND_EXEC, "reset target [run | halt | init] - default is run");
1533 register_command(cmd_ctx, NULL, "soft_reset_halt", handle_soft_reset_halt_command, COMMAND_EXEC, "halt the target and do a soft reset");
1535 register_command(cmd_ctx, NULL, "mdw", handle_md_command, COMMAND_EXEC, "display memory words <addr> [count]");
1536 register_command(cmd_ctx, NULL, "mdh", handle_md_command, COMMAND_EXEC, "display memory half-words <addr> [count]");
1537 register_command(cmd_ctx, NULL, "mdb", handle_md_command, COMMAND_EXEC, "display memory bytes <addr> [count]");
1539 register_command(cmd_ctx, NULL, "mww", handle_mw_command, COMMAND_EXEC, "write memory word <addr> <value> [count]");
1540 register_command(cmd_ctx, NULL, "mwh", handle_mw_command, COMMAND_EXEC, "write memory half-word <addr> <value> [count]");
1541 register_command(cmd_ctx, NULL, "mwb", handle_mw_command, COMMAND_EXEC, "write memory byte <addr> <value> [count]");
1543 register_command(cmd_ctx, NULL, "bp",
1544 handle_bp_command, COMMAND_EXEC,
1545 "list or set breakpoint [<address> <length> [hw]]");
1546 register_command(cmd_ctx, NULL, "rbp",
1547 handle_rbp_command, COMMAND_EXEC,
1548 "remove breakpoint <address>");
1549 register_command(cmd_ctx, NULL, "wp",
1550 handle_wp_command, COMMAND_EXEC,
1551 "list or set watchpoint "
1552 "[<address> <length> <r/w/a> [value] [mask]]");
1553 register_command(cmd_ctx, NULL, "rwp",
1554 handle_rwp_command, COMMAND_EXEC,
1555 "remove watchpoint <address>");
1557 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]");
1558 register_command(cmd_ctx, NULL, "dump_image", handle_dump_image_command, COMMAND_EXEC, "dump_image <file> <address> <size>");
1559 register_command(cmd_ctx, NULL, "verify_image", handle_verify_image_command, COMMAND_EXEC, "verify_image <file> [offset] [type]");
1560 register_command(cmd_ctx, NULL, "test_image", handle_test_image_command, COMMAND_EXEC, "test_image <file> [offset] [type]");
1562 if ((retval = target_request_register_commands(cmd_ctx)) != ERROR_OK)
1564 if ((retval = trace_register_commands(cmd_ctx)) != ERROR_OK)
1570 static int handle_targets_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1572 target_t *target = all_targets;
1576 target = get_target(args[0]);
1577 if (target == NULL) {
1578 command_print(cmd_ctx,"Target: %s is unknown, try one of:\n", args[0]);
1581 if (!target->tap->enabled) {
1582 command_print(cmd_ctx,"Target: TAP %s is disabled, "
1583 "can't be the current target\n",
1584 target->tap->dotted_name);
1588 cmd_ctx->current_target = target->target_number;
1593 target = all_targets;
1594 command_print(cmd_ctx, " TargetName Type Endian TapName State ");
1595 command_print(cmd_ctx, "-- ------------------ ---------- ------ ------------------ ------------");
1601 if (target->tap->enabled)
1602 state = target_state_name( target );
1604 state = "tap-disabled";
1606 if (cmd_ctx->current_target == target->target_number)
1609 /* keep columns lined up to match the headers above */
1610 command_print(cmd_ctx, "%2d%c %-18s %-10s %-6s %-18s %s",
1611 target->target_number,
1614 target_get_name(target),
1615 Jim_Nvp_value2name_simple(nvp_target_endian,
1616 target->endianness)->name,
1617 target->tap->dotted_name,
1619 target = target->next;
1625 /* every 300ms we check for reset & powerdropout and issue a "reset halt" if so. */
1627 static int powerDropout;
1628 static int srstAsserted;
1630 static int runPowerRestore;
1631 static int runPowerDropout;
1632 static int runSrstAsserted;
1633 static int runSrstDeasserted;
1635 static int sense_handler(void)
1637 static int prevSrstAsserted = 0;
1638 static int prevPowerdropout = 0;
1641 if ((retval = jtag_power_dropout(&powerDropout)) != ERROR_OK)
1645 powerRestored = prevPowerdropout && !powerDropout;
1648 runPowerRestore = 1;
1651 long long current = timeval_ms();
1652 static long long lastPower = 0;
1653 int waitMore = lastPower + 2000 > current;
1654 if (powerDropout && !waitMore)
1656 runPowerDropout = 1;
1657 lastPower = current;
1660 if ((retval = jtag_srst_asserted(&srstAsserted)) != ERROR_OK)
1664 srstDeasserted = prevSrstAsserted && !srstAsserted;
1666 static long long lastSrst = 0;
1667 waitMore = lastSrst + 2000 > current;
1668 if (srstDeasserted && !waitMore)
1670 runSrstDeasserted = 1;
1674 if (!prevSrstAsserted && srstAsserted)
1676 runSrstAsserted = 1;
1679 prevSrstAsserted = srstAsserted;
1680 prevPowerdropout = powerDropout;
1682 if (srstDeasserted || powerRestored)
1684 /* Other than logging the event we can't do anything here.
1685 * Issuing a reset is a particularly bad idea as we might
1686 * be inside a reset already.
1693 static void target_call_event_callbacks_all(enum target_event e) {
1695 target = all_targets;
1697 target_call_event_callbacks(target, e);
1698 target = target->next;
1702 /* process target state changes */
1703 int handle_target(void *priv)
1705 int retval = ERROR_OK;
1707 /* we do not want to recurse here... */
1708 static int recursive = 0;
1713 /* danger! running these procedures can trigger srst assertions and power dropouts.
1714 * We need to avoid an infinite loop/recursion here and we do that by
1715 * clearing the flags after running these events.
1717 int did_something = 0;
1718 if (runSrstAsserted)
1720 target_call_event_callbacks_all(TARGET_EVENT_GDB_HALT);
1721 Jim_Eval(interp, "srst_asserted");
1724 if (runSrstDeasserted)
1726 Jim_Eval(interp, "srst_deasserted");
1729 if (runPowerDropout)
1731 target_call_event_callbacks_all(TARGET_EVENT_GDB_HALT);
1732 Jim_Eval(interp, "power_dropout");
1735 if (runPowerRestore)
1737 Jim_Eval(interp, "power_restore");
1743 /* clear detect flags */
1747 /* clear action flags */
1749 runSrstAsserted = 0;
1750 runSrstDeasserted = 0;
1751 runPowerRestore = 0;
1752 runPowerDropout = 0;
1757 /* Poll targets for state changes unless that's globally disabled.
1758 * Skip targets that are currently disabled.
1760 for (target_t *target = all_targets;
1761 is_jtag_poll_safe() && target;
1762 target = target->next)
1764 if (!target->tap->enabled)
1767 /* only poll target if we've got power and srst isn't asserted */
1768 if (!powerDropout && !srstAsserted)
1770 /* polling may fail silently until the target has been examined */
1771 if ((retval = target_poll(target)) != ERROR_OK)
1773 target_call_event_callbacks(target, TARGET_EVENT_GDB_HALT);
1782 static int handle_reg_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1791 target = get_current_target(cmd_ctx);
1793 /* list all available registers for the current target */
1796 reg_cache_t *cache = target->reg_cache;
1803 for (i = 0, reg = cache->reg_list;
1804 i < cache->num_regs;
1805 i++, reg++, count++)
1807 /* only print cached values if they are valid */
1809 value = buf_to_str(reg->value,
1811 command_print(cmd_ctx,
1812 "(%i) %s (/%" PRIu32 "): 0x%s%s",
1820 command_print(cmd_ctx, "(%i) %s (/%" PRIu32 ")",
1825 cache = cache->next;
1831 /* access a single register by its ordinal number */
1832 if ((args[0][0] >= '0') && (args[0][0] <= '9'))
1835 int retval = parse_uint(args[0], &num);
1836 if (ERROR_OK != retval)
1837 return ERROR_COMMAND_SYNTAX_ERROR;
1839 reg_cache_t *cache = target->reg_cache;
1844 for (i = 0; i < cache->num_regs; i++)
1846 if (count++ == (int)num)
1848 reg = &cache->reg_list[i];
1854 cache = cache->next;
1859 command_print(cmd_ctx, "%i is out of bounds, the current target has only %i registers (0 - %i)", num, count, count - 1);
1862 } else /* access a single register by its name */
1864 reg = register_get_by_name(target->reg_cache, args[0], 1);
1868 command_print(cmd_ctx, "register %s not found in current target", args[0]);
1873 /* display a register */
1874 if ((argc == 1) || ((argc == 2) && !((args[1][0] >= '0') && (args[1][0] <= '9'))))
1876 if ((argc == 2) && (strcmp(args[1], "force") == 0))
1879 if (reg->valid == 0)
1881 reg_arch_type_t *arch_type = register_get_arch_type(reg->arch_type);
1882 arch_type->get(reg);
1884 value = buf_to_str(reg->value, reg->size, 16);
1885 command_print(cmd_ctx, "%s (/%i): 0x%s", reg->name, (int)(reg->size), value);
1890 /* set register value */
1893 uint8_t *buf = malloc(CEIL(reg->size, 8));
1894 str_to_buf(args[1], strlen(args[1]), buf, reg->size, 0);
1896 reg_arch_type_t *arch_type = register_get_arch_type(reg->arch_type);
1897 arch_type->set(reg, buf);
1899 value = buf_to_str(reg->value, reg->size, 16);
1900 command_print(cmd_ctx, "%s (/%i): 0x%s", reg->name, (int)(reg->size), value);
1908 command_print(cmd_ctx, "usage: reg <#|name> [value]");
1913 static int handle_poll_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1915 int retval = ERROR_OK;
1916 target_t *target = get_current_target(cmd_ctx);
1920 command_print(cmd_ctx, "background polling: %s",
1921 jtag_poll_get_enabled() ? "on" : "off");
1922 command_print(cmd_ctx, "TAP: %s (%s)",
1923 target->tap->dotted_name,
1924 target->tap->enabled ? "enabled" : "disabled");
1925 if (!target->tap->enabled)
1927 if ((retval = target_poll(target)) != ERROR_OK)
1929 if ((retval = target_arch_state(target)) != ERROR_OK)
1935 if (strcmp(args[0], "on") == 0)
1937 jtag_poll_set_enabled(true);
1939 else if (strcmp(args[0], "off") == 0)
1941 jtag_poll_set_enabled(false);
1945 command_print(cmd_ctx, "arg is \"on\" or \"off\"");
1949 return ERROR_COMMAND_SYNTAX_ERROR;
1955 static int handle_wait_halt_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1958 return ERROR_COMMAND_SYNTAX_ERROR;
1963 int retval = parse_uint(args[0], &ms);
1964 if (ERROR_OK != retval)
1966 command_print(cmd_ctx, "usage: %s [seconds]", cmd);
1967 return ERROR_COMMAND_SYNTAX_ERROR;
1969 // convert seconds (given) to milliseconds (needed)
1973 target_t *target = get_current_target(cmd_ctx);
1974 return target_wait_state(target, TARGET_HALTED, ms);
1977 /* wait for target state to change. The trick here is to have a low
1978 * latency for short waits and not to suck up all the CPU time
1981 * After 500ms, keep_alive() is invoked
1983 int target_wait_state(target_t *target, enum target_state state, int ms)
1986 long long then = 0, cur;
1991 if ((retval = target_poll(target)) != ERROR_OK)
1993 if (target->state == state)
2001 then = timeval_ms();
2002 LOG_DEBUG("waiting for target %s...",
2003 Jim_Nvp_value2name_simple(nvp_target_state,state)->name);
2011 if ((cur-then) > ms)
2013 LOG_ERROR("timed out while waiting for target %s",
2014 Jim_Nvp_value2name_simple(nvp_target_state,state)->name);
2022 static int handle_halt_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2026 target_t *target = get_current_target(cmd_ctx);
2027 int retval = target_halt(target);
2028 if (ERROR_OK != retval)
2034 retval = parse_uint(args[0], &wait);
2035 if (ERROR_OK != retval)
2036 return ERROR_COMMAND_SYNTAX_ERROR;
2041 return handle_wait_halt_command(cmd_ctx, cmd, args, argc);
2044 static int handle_soft_reset_halt_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2046 target_t *target = get_current_target(cmd_ctx);
2048 LOG_USER("requesting target halt and executing a soft reset");
2050 target->type->soft_reset_halt(target);
2055 static int handle_reset_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2058 return ERROR_COMMAND_SYNTAX_ERROR;
2060 enum target_reset_mode reset_mode = RESET_RUN;
2064 n = Jim_Nvp_name2value_simple(nvp_reset_modes, args[0]);
2065 if ((n->name == NULL) || (n->value == RESET_UNKNOWN)) {
2066 return ERROR_COMMAND_SYNTAX_ERROR;
2068 reset_mode = n->value;
2071 /* reset *all* targets */
2072 return target_process_reset(cmd_ctx, reset_mode);
2076 static int handle_resume_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2080 return ERROR_COMMAND_SYNTAX_ERROR;
2082 target_t *target = get_current_target(cmd_ctx);
2083 target_handle_event(target, TARGET_EVENT_OLD_pre_resume);
2085 /* with no args, resume from current pc, addr = 0,
2086 * with one arguments, addr = args[0],
2087 * handle breakpoints, not debugging */
2091 int retval = parse_u32(args[0], &addr);
2092 if (ERROR_OK != retval)
2097 return target_resume(target, current, addr, 1, 0);
2100 static int handle_step_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2103 return ERROR_COMMAND_SYNTAX_ERROR;
2107 /* with no args, step from current pc, addr = 0,
2108 * with one argument addr = args[0],
2109 * handle breakpoints, debugging */
2114 int retval = parse_u32(args[0], &addr);
2115 if (ERROR_OK != retval)
2120 target_t *target = get_current_target(cmd_ctx);
2122 return target->type->step(target, current_pc, addr, 1);
2125 static void handle_md_output(struct command_context_s *cmd_ctx,
2126 struct target_s *target, uint32_t address, unsigned size,
2127 unsigned count, const uint8_t *buffer)
2129 const unsigned line_bytecnt = 32;
2130 unsigned line_modulo = line_bytecnt / size;
2132 char output[line_bytecnt * 4 + 1];
2133 unsigned output_len = 0;
2135 const char *value_fmt;
2137 case 4: value_fmt = "%8.8x "; break;
2138 case 2: value_fmt = "%4.2x "; break;
2139 case 1: value_fmt = "%2.2x "; break;
2141 LOG_ERROR("invalid memory read size: %u", size);
2145 for (unsigned i = 0; i < count; i++)
2147 if (i % line_modulo == 0)
2149 output_len += snprintf(output + output_len,
2150 sizeof(output) - output_len,
2152 (unsigned)(address + (i*size)));
2156 const uint8_t *value_ptr = buffer + i * size;
2158 case 4: value = target_buffer_get_u32(target, value_ptr); break;
2159 case 2: value = target_buffer_get_u16(target, value_ptr); break;
2160 case 1: value = *value_ptr;
2162 output_len += snprintf(output + output_len,
2163 sizeof(output) - output_len,
2166 if ((i % line_modulo == line_modulo - 1) || (i == count - 1))
2168 command_print(cmd_ctx, "%s", output);
2174 static int handle_md_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2177 return ERROR_COMMAND_SYNTAX_ERROR;
2181 case 'w': size = 4; break;
2182 case 'h': size = 2; break;
2183 case 'b': size = 1; break;
2184 default: return ERROR_COMMAND_SYNTAX_ERROR;
2188 int retval = parse_u32(args[0], &address);
2189 if (ERROR_OK != retval)
2195 retval = parse_uint(args[1], &count);
2196 if (ERROR_OK != retval)
2200 uint8_t *buffer = calloc(count, size);
2202 target_t *target = get_current_target(cmd_ctx);
2203 retval = target_read_memory(target,
2204 address, size, count, buffer);
2205 if (ERROR_OK == retval)
2206 handle_md_output(cmd_ctx, target, address, size, count, buffer);
2213 static int handle_mw_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2215 if ((argc < 2) || (argc > 3))
2216 return ERROR_COMMAND_SYNTAX_ERROR;
2219 int retval = parse_u32(args[0], &address);
2220 if (ERROR_OK != retval)
2224 retval = parse_u32(args[1], &value);
2225 if (ERROR_OK != retval)
2231 retval = parse_uint(args[2], &count);
2232 if (ERROR_OK != retval)
2236 target_t *target = get_current_target(cmd_ctx);
2238 uint8_t value_buf[4];
2243 target_buffer_set_u32(target, value_buf, value);
2247 target_buffer_set_u16(target, value_buf, value);
2251 value_buf[0] = value;
2254 return ERROR_COMMAND_SYNTAX_ERROR;
2256 for (unsigned i = 0; i < count; i++)
2258 retval = target_write_memory(target,
2259 address + i * wordsize, wordsize, 1, value_buf);
2260 if (ERROR_OK != retval)
2269 static int parse_load_image_command_args(char **args, int argc,
2270 image_t *image, uint32_t *min_address, uint32_t *max_address)
2272 if (argc < 1 || argc > 5)
2273 return ERROR_COMMAND_SYNTAX_ERROR;
2275 /* a base address isn't always necessary,
2276 * default to 0x0 (i.e. don't relocate) */
2280 int retval = parse_u32(args[1], &addr);
2281 if (ERROR_OK != retval)
2282 return ERROR_COMMAND_SYNTAX_ERROR;
2283 image->base_address = addr;
2284 image->base_address_set = 1;
2287 image->base_address_set = 0;
2289 image->start_address_set = 0;
2293 int retval = parse_u32(args[3], min_address);
2294 if (ERROR_OK != retval)
2295 return ERROR_COMMAND_SYNTAX_ERROR;
2299 int retval = parse_u32(args[4], max_address);
2300 if (ERROR_OK != retval)
2301 return ERROR_COMMAND_SYNTAX_ERROR;
2302 // use size (given) to find max (required)
2303 *max_address += *min_address;
2306 if (*min_address > *max_address)
2307 return ERROR_COMMAND_SYNTAX_ERROR;
2312 static int handle_load_image_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2316 uint32_t image_size;
2317 uint32_t min_address = 0;
2318 uint32_t max_address = 0xffffffff;
2324 duration_t duration;
2325 char *duration_text;
2327 int retval = parse_load_image_command_args(args, argc,
2328 &image, &min_address, &max_address);
2329 if (ERROR_OK != retval)
2332 target_t *target = get_current_target(cmd_ctx);
2333 duration_start_measure(&duration);
2335 if (image_open(&image, args[0], (argc >= 3) ? args[2] : NULL) != ERROR_OK)
2342 for (i = 0; i < image.num_sections; i++)
2344 buffer = malloc(image.sections[i].size);
2347 command_print(cmd_ctx,
2348 "error allocating buffer for section (%d bytes)",
2349 (int)(image.sections[i].size));
2353 if ((retval = image_read_section(&image, i, 0x0, image.sections[i].size, buffer, &buf_cnt)) != ERROR_OK)
2359 uint32_t offset = 0;
2360 uint32_t length = buf_cnt;
2362 /* DANGER!!! beware of unsigned comparision here!!! */
2364 if ((image.sections[i].base_address + buf_cnt >= min_address)&&
2365 (image.sections[i].base_address < max_address))
2367 if (image.sections[i].base_address < min_address)
2369 /* clip addresses below */
2370 offset += min_address-image.sections[i].base_address;
2374 if (image.sections[i].base_address + buf_cnt > max_address)
2376 length -= (image.sections[i].base_address + buf_cnt)-max_address;
2379 if ((retval = target_write_buffer(target, image.sections[i].base_address + offset, length, buffer + offset)) != ERROR_OK)
2384 image_size += length;
2385 command_print(cmd_ctx, "%u bytes written at address 0x%8.8" PRIx32 "",
2386 (unsigned int)length,
2387 image.sections[i].base_address + offset);
2393 if ((retvaltemp = duration_stop_measure(&duration, &duration_text)) != ERROR_OK)
2395 image_close(&image);
2399 if (retval == ERROR_OK)
2401 command_print(cmd_ctx, "downloaded %u byte in %s",
2402 (unsigned int)image_size,
2405 free(duration_text);
2407 image_close(&image);
2413 static int handle_dump_image_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2417 uint8_t buffer[560];
2420 duration_t duration;
2421 char *duration_text;
2423 target_t *target = get_current_target(cmd_ctx);
2427 command_print(cmd_ctx, "usage: dump_image <filename> <address> <size>");
2432 int retval = parse_u32(args[1], &address);
2433 if (ERROR_OK != retval)
2437 retval = parse_u32(args[2], &size);
2438 if (ERROR_OK != retval)
2441 if (fileio_open(&fileio, args[0], FILEIO_WRITE, FILEIO_BINARY) != ERROR_OK)
2446 duration_start_measure(&duration);
2450 uint32_t size_written;
2451 uint32_t this_run_size = (size > 560) ? 560 : size;
2453 retval = target_read_buffer(target, address, this_run_size, buffer);
2454 if (retval != ERROR_OK)
2459 retval = fileio_write(&fileio, this_run_size, buffer, &size_written);
2460 if (retval != ERROR_OK)
2465 size -= this_run_size;
2466 address += this_run_size;
2469 if ((retvaltemp = fileio_close(&fileio)) != ERROR_OK)
2472 if ((retvaltemp = duration_stop_measure(&duration, &duration_text)) != ERROR_OK)
2475 if (retval == ERROR_OK)
2477 command_print(cmd_ctx, "dumped %lld byte in %s",
2478 fileio.size, duration_text);
2479 free(duration_text);
2485 static int handle_verify_image_command_internal(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc, int verify)
2489 uint32_t image_size;
2491 int retval, retvaltemp;
2492 uint32_t checksum = 0;
2493 uint32_t mem_checksum = 0;
2497 duration_t duration;
2498 char *duration_text;
2500 target_t *target = get_current_target(cmd_ctx);
2504 return ERROR_COMMAND_SYNTAX_ERROR;
2509 LOG_ERROR("no target selected");
2513 duration_start_measure(&duration);
2518 retval = parse_u32(args[1], &addr);
2519 if (ERROR_OK != retval)
2520 return ERROR_COMMAND_SYNTAX_ERROR;
2521 image.base_address = addr;
2522 image.base_address_set = 1;
2526 image.base_address_set = 0;
2527 image.base_address = 0x0;
2530 image.start_address_set = 0;
2532 if ((retval = image_open(&image, args[0], (argc == 3) ? args[2] : NULL)) != ERROR_OK)
2539 for (i = 0; i < image.num_sections; i++)
2541 buffer = malloc(image.sections[i].size);
2544 command_print(cmd_ctx,
2545 "error allocating buffer for section (%d bytes)",
2546 (int)(image.sections[i].size));
2549 if ((retval = image_read_section(&image, i, 0x0, image.sections[i].size, buffer, &buf_cnt)) != ERROR_OK)
2557 /* calculate checksum of image */
2558 image_calculate_checksum(buffer, buf_cnt, &checksum);
2560 retval = target_checksum_memory(target, image.sections[i].base_address, buf_cnt, &mem_checksum);
2561 if (retval != ERROR_OK)
2567 if (checksum != mem_checksum)
2569 /* failed crc checksum, fall back to a binary compare */
2572 command_print(cmd_ctx, "checksum mismatch - attempting binary compare");
2574 data = (uint8_t*)malloc(buf_cnt);
2576 /* Can we use 32bit word accesses? */
2578 int count = buf_cnt;
2579 if ((count % 4) == 0)
2584 retval = target_read_memory(target, image.sections[i].base_address, size, count, data);
2585 if (retval == ERROR_OK)
2588 for (t = 0; t < buf_cnt; t++)
2590 if (data[t] != buffer[t])
2592 command_print(cmd_ctx,
2593 "Verify operation failed address 0x%08x. Was 0x%02x instead of 0x%02x\n",
2594 (unsigned)(t + image.sections[i].base_address),
2599 retval = ERROR_FAIL;
2613 command_print(cmd_ctx, "address 0x%08" PRIx32 " length 0x%08" PRIx32 "",
2614 image.sections[i].base_address,
2619 image_size += buf_cnt;
2623 if ((retvaltemp = duration_stop_measure(&duration, &duration_text)) != ERROR_OK)
2625 image_close(&image);
2629 if (retval == ERROR_OK)
2631 command_print(cmd_ctx, "verified %u bytes in %s",
2632 (unsigned int)image_size,
2635 free(duration_text);
2637 image_close(&image);
2642 static int handle_verify_image_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2644 return handle_verify_image_command_internal(cmd_ctx, cmd, args, argc, 1);
2647 static int handle_test_image_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2649 return handle_verify_image_command_internal(cmd_ctx, cmd, args, argc, 0);
2652 static int handle_bp_command_list(struct command_context_s *cmd_ctx)
2654 target_t *target = get_current_target(cmd_ctx);
2655 breakpoint_t *breakpoint = target->breakpoints;
2658 if (breakpoint->type == BKPT_SOFT)
2660 char* buf = buf_to_str(breakpoint->orig_instr,
2661 breakpoint->length, 16);
2662 command_print(cmd_ctx, "0x%8.8" PRIx32 ", 0x%x, %i, 0x%s",
2663 breakpoint->address,
2665 breakpoint->set, buf);
2670 command_print(cmd_ctx, "0x%8.8" PRIx32 ", 0x%x, %i",
2671 breakpoint->address,
2672 breakpoint->length, breakpoint->set);
2675 breakpoint = breakpoint->next;
2680 static int handle_bp_command_set(struct command_context_s *cmd_ctx,
2681 uint32_t addr, uint32_t length, int hw)
2683 target_t *target = get_current_target(cmd_ctx);
2684 int retval = breakpoint_add(target, addr, length, hw);
2685 if (ERROR_OK == retval)
2686 command_print(cmd_ctx, "breakpoint set at 0x%8.8" PRIx32 "", addr);
2688 LOG_ERROR("Failure setting breakpoint");
2692 static int handle_bp_command(struct command_context_s *cmd_ctx,
2693 char *cmd, char **args, int argc)
2696 return handle_bp_command_list(cmd_ctx);
2698 if (argc < 2 || argc > 3)
2700 command_print(cmd_ctx, "usage: bp <address> <length> ['hw']");
2701 return ERROR_COMMAND_SYNTAX_ERROR;
2705 int retval = parse_u32(args[0], &addr);
2706 if (ERROR_OK != retval)
2710 retval = parse_u32(args[1], &length);
2711 if (ERROR_OK != retval)
2717 if (strcmp(args[2], "hw") == 0)
2720 return ERROR_COMMAND_SYNTAX_ERROR;
2723 return handle_bp_command_set(cmd_ctx, addr, length, hw);
2726 static int handle_rbp_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2729 return ERROR_COMMAND_SYNTAX_ERROR;
2732 int retval = parse_u32(args[0], &addr);
2733 if (ERROR_OK != retval)
2736 target_t *target = get_current_target(cmd_ctx);
2737 breakpoint_remove(target, addr);
2742 static int handle_wp_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2744 target_t *target = get_current_target(cmd_ctx);
2748 watchpoint_t *watchpoint = target->watchpoints;
2752 command_print(cmd_ctx,
2753 "address: 0x%8.8" PRIx32 ", len: 0x%8.8x, r/w/a: %i, value: 0x%8.8" PRIx32 ", mask: 0x%8.8" PRIx32 "",
2754 watchpoint->address,
2756 (int)(watchpoint->rw),
2759 watchpoint = watchpoint->next;
2764 enum watchpoint_rw type = WPT_ACCESS;
2766 uint32_t length = 0;
2767 uint32_t data_value = 0x0;
2768 uint32_t data_mask = 0xffffffff;
2774 retval = parse_u32(args[4], &data_mask);
2775 if (ERROR_OK != retval)
2779 retval = parse_u32(args[3], &data_value);
2780 if (ERROR_OK != retval)
2796 LOG_ERROR("invalid watchpoint mode ('%c')", args[2][0]);
2797 return ERROR_COMMAND_SYNTAX_ERROR;
2801 retval = parse_u32(args[1], &length);
2802 if (ERROR_OK != retval)
2804 retval = parse_u32(args[0], &addr);
2805 if (ERROR_OK != retval)
2810 command_print(cmd_ctx, "usage: wp <address> <length> [r/w/a] [value] [mask]");
2811 return ERROR_COMMAND_SYNTAX_ERROR;
2814 retval = watchpoint_add(target, addr, length, type,
2815 data_value, data_mask);
2816 if (ERROR_OK != retval)
2817 LOG_ERROR("Failure setting watchpoints");
2822 static int handle_rwp_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2825 return ERROR_COMMAND_SYNTAX_ERROR;
2828 int retval = parse_u32(args[0], &addr);
2829 if (ERROR_OK != retval)
2832 target_t *target = get_current_target(cmd_ctx);
2833 watchpoint_remove(target, addr);
2840 * Translate a virtual address to a physical address.
2842 * The low-level target implementation must have logged a detailed error
2843 * which is forwarded to telnet/GDB session.
2845 static int handle_virt2phys_command(command_context_t *cmd_ctx,
2846 char *cmd, char **args, int argc)
2849 return ERROR_COMMAND_SYNTAX_ERROR;
2852 int retval = parse_u32(args[0], &va);
2853 if (ERROR_OK != retval)
2857 target_t *target = get_current_target(cmd_ctx);
2858 retval = target->type->virt2phys(target, va, &pa);
2859 if (retval == ERROR_OK)
2860 command_print(cmd_ctx, "Physical address 0x%08" PRIx32 "", pa);
2865 static void writeData(FILE *f, const void *data, size_t len)
2867 size_t written = fwrite(data, 1, len, f);
2869 LOG_ERROR("failed to write %zu bytes: %s", len, strerror(errno));
2872 static void writeLong(FILE *f, int l)
2875 for (i = 0; i < 4; i++)
2877 char c = (l >> (i*8))&0xff;
2878 writeData(f, &c, 1);
2883 static void writeString(FILE *f, char *s)
2885 writeData(f, s, strlen(s));
2888 /* Dump a gmon.out histogram file. */
2889 static void writeGmon(uint32_t *samples, uint32_t sampleNum, char *filename)
2892 FILE *f = fopen(filename, "w");
2895 writeString(f, "gmon");
2896 writeLong(f, 0x00000001); /* Version */
2897 writeLong(f, 0); /* padding */
2898 writeLong(f, 0); /* padding */
2899 writeLong(f, 0); /* padding */
2901 uint8_t zero = 0; /* GMON_TAG_TIME_HIST */
2902 writeData(f, &zero, 1);
2904 /* figure out bucket size */
2905 uint32_t min = samples[0];
2906 uint32_t max = samples[0];
2907 for (i = 0; i < sampleNum; i++)
2909 if (min > samples[i])
2913 if (max < samples[i])
2919 int addressSpace = (max-min + 1);
2921 static const uint32_t maxBuckets = 256 * 1024; /* maximum buckets. */
2922 uint32_t length = addressSpace;
2923 if (length > maxBuckets)
2925 length = maxBuckets;
2927 int *buckets = malloc(sizeof(int)*length);
2928 if (buckets == NULL)
2933 memset(buckets, 0, sizeof(int)*length);
2934 for (i = 0; i < sampleNum;i++)
2936 uint32_t address = samples[i];
2937 long long a = address-min;
2938 long long b = length-1;
2939 long long c = addressSpace-1;
2940 int index = (a*b)/c; /* danger!!!! int32 overflows */
2944 /* append binary memory gmon.out &profile_hist_hdr ((char*)&profile_hist_hdr + sizeof(struct gmon_hist_hdr)) */
2945 writeLong(f, min); /* low_pc */
2946 writeLong(f, max); /* high_pc */
2947 writeLong(f, length); /* # of samples */
2948 writeLong(f, 64000000); /* 64MHz */
2949 writeString(f, "seconds");
2950 for (i = 0; i < (15-strlen("seconds")); i++)
2951 writeData(f, &zero, 1);
2952 writeString(f, "s");
2954 /*append binary memory gmon.out profile_hist_data (profile_hist_data + profile_hist_hdr.hist_size) */
2956 char *data = malloc(2*length);
2959 for (i = 0; i < length;i++)
2968 data[i*2 + 1]=(val >> 8)&0xff;
2971 writeData(f, data, length * 2);
2981 /* profiling samples the CPU PC as quickly as OpenOCD is able, which will be used as a random sampling of PC */
2982 static int handle_profile_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2984 target_t *target = get_current_target(cmd_ctx);
2985 struct timeval timeout, now;
2987 gettimeofday(&timeout, NULL);
2990 return ERROR_COMMAND_SYNTAX_ERROR;
2993 int retval = parse_uint(args[0], &offset);
2994 if (ERROR_OK != retval)
2997 timeval_add_time(&timeout, offset, 0);
2999 command_print(cmd_ctx, "Starting profiling. Halting and resuming the target as often as we can...");
3001 static const int maxSample = 10000;
3002 uint32_t *samples = malloc(sizeof(uint32_t)*maxSample);
3003 if (samples == NULL)
3007 /* hopefully it is safe to cache! We want to stop/restart as quickly as possible. */
3008 reg_t *reg = register_get_by_name(target->reg_cache, "pc", 1);
3012 target_poll(target);
3013 if (target->state == TARGET_HALTED)
3015 uint32_t t=*((uint32_t *)reg->value);
3016 samples[numSamples++]=t;
3017 retval = target_resume(target, 1, 0, 0, 0); /* current pc, addr = 0, do not handle breakpoints, not debugging */
3018 target_poll(target);
3019 alive_sleep(10); /* sleep 10ms, i.e. <100 samples/second. */
3020 } else if (target->state == TARGET_RUNNING)
3022 /* We want to quickly sample the PC. */
3023 if ((retval = target_halt(target)) != ERROR_OK)
3030 command_print(cmd_ctx, "Target not halted or running");
3034 if (retval != ERROR_OK)
3039 gettimeofday(&now, NULL);
3040 if ((numSamples >= maxSample) || ((now.tv_sec >= timeout.tv_sec) && (now.tv_usec >= timeout.tv_usec)))
3042 command_print(cmd_ctx, "Profiling completed. %d samples.", numSamples);
3043 if ((retval = target_poll(target)) != ERROR_OK)
3048 if (target->state == TARGET_HALTED)
3050 target_resume(target, 1, 0, 0, 0); /* current pc, addr = 0, do not handle breakpoints, not debugging */
3052 if ((retval = target_poll(target)) != ERROR_OK)
3057 writeGmon(samples, numSamples, args[1]);
3058 command_print(cmd_ctx, "Wrote %s", args[1]);
3067 static int new_int_array_element(Jim_Interp * interp, const char *varname, int idx, uint32_t val)
3070 Jim_Obj *nameObjPtr, *valObjPtr;
3073 namebuf = alloc_printf("%s(%d)", varname, idx);
3077 nameObjPtr = Jim_NewStringObj(interp, namebuf, -1);
3078 valObjPtr = Jim_NewIntObj(interp, val);
3079 if (!nameObjPtr || !valObjPtr)
3085 Jim_IncrRefCount(nameObjPtr);
3086 Jim_IncrRefCount(valObjPtr);
3087 result = Jim_SetVariable(interp, nameObjPtr, valObjPtr);
3088 Jim_DecrRefCount(interp, nameObjPtr);
3089 Jim_DecrRefCount(interp, valObjPtr);
3091 /* printf("%s(%d) <= 0%08x\n", varname, idx, val); */
3095 static int jim_mem2array(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
3097 command_context_t *context;
3100 context = Jim_GetAssocData(interp, "context");
3101 if (context == NULL)
3103 LOG_ERROR("mem2array: no command context");
3106 target = get_current_target(context);
3109 LOG_ERROR("mem2array: no current target");
3113 return target_mem2array(interp, target, argc-1, argv + 1);
3116 static int target_mem2array(Jim_Interp *interp, target_t *target, int argc, Jim_Obj *const *argv)
3124 const char *varname;
3125 uint8_t buffer[4096];
3129 /* argv[1] = name of array to receive the data
3130 * argv[2] = desired width
3131 * argv[3] = memory address
3132 * argv[4] = count of times to read
3135 Jim_WrongNumArgs(interp, 1, argv, "varname width addr nelems");
3138 varname = Jim_GetString(argv[0], &len);
3139 /* given "foo" get space for worse case "foo(%d)" .. add 20 */
3141 e = Jim_GetLong(interp, argv[1], &l);
3147 e = Jim_GetLong(interp, argv[2], &l);
3152 e = Jim_GetLong(interp, argv[3], &l);
3168 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3169 Jim_AppendStrings(interp, Jim_GetResult(interp), "Invalid width param, must be 8/16/32", NULL);
3173 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3174 Jim_AppendStrings(interp, Jim_GetResult(interp), "mem2array: zero width read?", NULL);
3177 if ((addr + (len * width)) < addr) {
3178 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3179 Jim_AppendStrings(interp, Jim_GetResult(interp), "mem2array: addr + len - wraps to zero?", NULL);
3182 /* absurd transfer size? */
3184 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3185 Jim_AppendStrings(interp, Jim_GetResult(interp), "mem2array: absurd > 64K item request", NULL);
3190 ((width == 2) && ((addr & 1) == 0)) ||
3191 ((width == 4) && ((addr & 3) == 0))) {
3195 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3196 sprintf(buf, "mem2array address: 0x%08" PRIx32 " is not aligned for %" PRId32 " byte reads",
3199 Jim_AppendStrings(interp, Jim_GetResult(interp), buf , NULL);
3210 /* Slurp... in buffer size chunks */
3212 count = len; /* in objects.. */
3213 if (count > (sizeof(buffer)/width)) {
3214 count = (sizeof(buffer)/width);
3217 retval = target_read_memory(target, addr, width, count, buffer);
3218 if (retval != ERROR_OK) {
3220 LOG_ERROR("mem2array: Read @ 0x%08x, w=%d, cnt=%d, failed",
3224 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3225 Jim_AppendStrings(interp, Jim_GetResult(interp), "mem2array: cannot read memory", NULL);
3229 v = 0; /* shut up gcc */
3230 for (i = 0 ;i < count ;i++, n++) {
3233 v = target_buffer_get_u32(target, &buffer[i*width]);
3236 v = target_buffer_get_u16(target, &buffer[i*width]);
3239 v = buffer[i] & 0x0ff;
3242 new_int_array_element(interp, varname, n, v);
3248 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3253 static int get_int_array_element(Jim_Interp * interp, const char *varname, int idx, uint32_t *val)
3256 Jim_Obj *nameObjPtr, *valObjPtr;
3260 namebuf = alloc_printf("%s(%d)", varname, idx);
3264 nameObjPtr = Jim_NewStringObj(interp, namebuf, -1);
3271 Jim_IncrRefCount(nameObjPtr);
3272 valObjPtr = Jim_GetVariable(interp, nameObjPtr, JIM_ERRMSG);
3273 Jim_DecrRefCount(interp, nameObjPtr);
3275 if (valObjPtr == NULL)
3278 result = Jim_GetLong(interp, valObjPtr, &l);
3279 /* printf("%s(%d) => 0%08x\n", varname, idx, val); */
3284 static int jim_array2mem(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
3286 command_context_t *context;
3289 context = Jim_GetAssocData(interp, "context");
3290 if (context == NULL) {
3291 LOG_ERROR("array2mem: no command context");
3294 target = get_current_target(context);
3295 if (target == NULL) {
3296 LOG_ERROR("array2mem: no current target");
3300 return target_array2mem(interp,target, argc-1, argv + 1);
3303 static int target_array2mem(Jim_Interp *interp, target_t *target, int argc, Jim_Obj *const *argv)
3311 const char *varname;
3312 uint8_t buffer[4096];
3316 /* argv[1] = name of array to get the data
3317 * argv[2] = desired width
3318 * argv[3] = memory address
3319 * argv[4] = count to write
3322 Jim_WrongNumArgs(interp, 1, argv, "varname width addr nelems");
3325 varname = Jim_GetString(argv[0], &len);
3326 /* given "foo" get space for worse case "foo(%d)" .. add 20 */
3328 e = Jim_GetLong(interp, argv[1], &l);
3334 e = Jim_GetLong(interp, argv[2], &l);
3339 e = Jim_GetLong(interp, argv[3], &l);
3355 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3356 Jim_AppendStrings(interp, Jim_GetResult(interp), "Invalid width param, must be 8/16/32", NULL);
3360 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3361 Jim_AppendStrings(interp, Jim_GetResult(interp), "array2mem: zero width read?", NULL);
3364 if ((addr + (len * width)) < addr) {
3365 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3366 Jim_AppendStrings(interp, Jim_GetResult(interp), "array2mem: addr + len - wraps to zero?", NULL);
3369 /* absurd transfer size? */
3371 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3372 Jim_AppendStrings(interp, Jim_GetResult(interp), "array2mem: absurd > 64K item request", NULL);
3377 ((width == 2) && ((addr & 1) == 0)) ||
3378 ((width == 4) && ((addr & 3) == 0))) {
3382 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3383 sprintf(buf, "array2mem address: 0x%08x is not aligned for %d byte reads",
3386 Jim_AppendStrings(interp, Jim_GetResult(interp), buf , NULL);
3397 /* Slurp... in buffer size chunks */
3399 count = len; /* in objects.. */
3400 if (count > (sizeof(buffer)/width)) {
3401 count = (sizeof(buffer)/width);
3404 v = 0; /* shut up gcc */
3405 for (i = 0 ;i < count ;i++, n++) {
3406 get_int_array_element(interp, varname, n, &v);
3409 target_buffer_set_u32(target, &buffer[i*width], v);
3412 target_buffer_set_u16(target, &buffer[i*width], v);
3415 buffer[i] = v & 0x0ff;
3421 retval = target_write_memory(target, addr, width, count, buffer);
3422 if (retval != ERROR_OK) {
3424 LOG_ERROR("array2mem: Write @ 0x%08x, w=%d, cnt=%d, failed",
3428 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3429 Jim_AppendStrings(interp, Jim_GetResult(interp), "array2mem: cannot read memory", NULL);
3435 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3440 void target_all_handle_event(enum target_event e)
3444 LOG_DEBUG("**all*targets: event: %d, %s",
3446 Jim_Nvp_value2name_simple(nvp_target_event, e)->name);
3448 target = all_targets;
3450 target_handle_event(target, e);
3451 target = target->next;
3455 void target_handle_event(target_t *target, enum target_event e)
3457 target_event_action_t *teap;
3459 for (teap = target->event_action; teap != NULL; teap = teap->next) {
3460 if (teap->event == e) {
3461 LOG_DEBUG("target: (%d) %s (%s) event: %d (%s) action: %s",
3462 target->target_number,
3464 target_get_name(target),
3466 Jim_Nvp_value2name_simple(nvp_target_event, e)->name,
3467 Jim_GetString(teap->body, NULL));
3468 if (Jim_EvalObj(interp, teap->body) != JIM_OK)
3470 Jim_PrintErrorMessage(interp);
3476 enum target_cfg_param {
3479 TCFG_WORK_AREA_VIRT,
3480 TCFG_WORK_AREA_PHYS,
3481 TCFG_WORK_AREA_SIZE,
3482 TCFG_WORK_AREA_BACKUP,
3485 TCFG_CHAIN_POSITION,
3488 static Jim_Nvp nvp_config_opts[] = {
3489 { .name = "-type", .value = TCFG_TYPE },
3490 { .name = "-event", .value = TCFG_EVENT },
3491 { .name = "-work-area-virt", .value = TCFG_WORK_AREA_VIRT },
3492 { .name = "-work-area-phys", .value = TCFG_WORK_AREA_PHYS },
3493 { .name = "-work-area-size", .value = TCFG_WORK_AREA_SIZE },
3494 { .name = "-work-area-backup", .value = TCFG_WORK_AREA_BACKUP },
3495 { .name = "-endian" , .value = TCFG_ENDIAN },
3496 { .name = "-variant", .value = TCFG_VARIANT },
3497 { .name = "-chain-position", .value = TCFG_CHAIN_POSITION },
3499 { .name = NULL, .value = -1 }
3502 static int target_configure(Jim_GetOptInfo *goi, target_t *target)
3510 /* parse config or cget options ... */
3511 while (goi->argc > 0) {
3512 Jim_SetEmptyResult(goi->interp);
3513 /* Jim_GetOpt_Debug(goi); */
3515 if (target->type->target_jim_configure) {
3516 /* target defines a configure function */
3517 /* target gets first dibs on parameters */
3518 e = (*(target->type->target_jim_configure))(target, goi);
3527 /* otherwise we 'continue' below */
3529 e = Jim_GetOpt_Nvp(goi, nvp_config_opts, &n);
3531 Jim_GetOpt_NvpUnknown(goi, nvp_config_opts, 0);
3537 if (goi->isconfigure) {
3538 Jim_SetResult_sprintf(goi->interp, "not setable: %s", n->name);
3542 if (goi->argc != 0) {
3543 Jim_WrongNumArgs(goi->interp, goi->argc, goi->argv, "NO PARAMS");
3547 Jim_SetResultString(goi->interp, target_get_name(target), -1);
3551 if (goi->argc == 0) {
3552 Jim_WrongNumArgs(goi->interp, goi->argc, goi->argv, "-event ?event-name? ...");
3556 e = Jim_GetOpt_Nvp(goi, nvp_target_event, &n);
3558 Jim_GetOpt_NvpUnknown(goi, nvp_target_event, 1);
3562 if (goi->isconfigure) {
3563 if (goi->argc != 1) {
3564 Jim_WrongNumArgs(goi->interp, goi->argc, goi->argv, "-event ?event-name? ?EVENT-BODY?");
3568 if (goi->argc != 0) {
3569 Jim_WrongNumArgs(goi->interp, goi->argc, goi->argv, "-event ?event-name?");
3575 target_event_action_t *teap;
3577 teap = target->event_action;
3578 /* replace existing? */
3580 if (teap->event == (enum target_event)n->value) {
3586 if (goi->isconfigure) {
3587 bool replace = true;
3590 teap = calloc(1, sizeof(*teap));
3593 teap->event = n->value;
3594 Jim_GetOpt_Obj(goi, &o);
3596 Jim_DecrRefCount(interp, teap->body);
3598 teap->body = Jim_DuplicateObj(goi->interp, o);
3601 * Tcl/TK - "tk events" have a nice feature.
3602 * See the "BIND" command.
3603 * We should support that here.
3604 * You can specify %X and %Y in the event code.
3605 * The idea is: %T - target name.
3606 * The idea is: %N - target number
3607 * The idea is: %E - event name.
3609 Jim_IncrRefCount(teap->body);
3613 /* add to head of event list */
3614 teap->next = target->event_action;
3615 target->event_action = teap;
3617 Jim_SetEmptyResult(goi->interp);
3621 Jim_SetEmptyResult(goi->interp);
3623 Jim_SetResult(goi->interp, Jim_DuplicateObj(goi->interp, teap->body));
3630 case TCFG_WORK_AREA_VIRT:
3631 if (goi->isconfigure) {
3632 target_free_all_working_areas(target);
3633 e = Jim_GetOpt_Wide(goi, &w);
3637 target->working_area_virt = w;
3639 if (goi->argc != 0) {
3643 Jim_SetResult(interp, Jim_NewIntObj(goi->interp, target->working_area_virt));
3647 case TCFG_WORK_AREA_PHYS:
3648 if (goi->isconfigure) {
3649 target_free_all_working_areas(target);
3650 e = Jim_GetOpt_Wide(goi, &w);
3654 target->working_area_phys = w;
3656 if (goi->argc != 0) {
3660 Jim_SetResult(interp, Jim_NewIntObj(goi->interp, target->working_area_phys));
3664 case TCFG_WORK_AREA_SIZE:
3665 if (goi->isconfigure) {
3666 target_free_all_working_areas(target);
3667 e = Jim_GetOpt_Wide(goi, &w);
3671 target->working_area_size = w;
3673 if (goi->argc != 0) {
3677 Jim_SetResult(interp, Jim_NewIntObj(goi->interp, target->working_area_size));
3681 case TCFG_WORK_AREA_BACKUP:
3682 if (goi->isconfigure) {
3683 target_free_all_working_areas(target);
3684 e = Jim_GetOpt_Wide(goi, &w);
3688 /* make this exactly 1 or 0 */
3689 target->backup_working_area = (!!w);
3691 if (goi->argc != 0) {
3695 Jim_SetResult(interp, Jim_NewIntObj(goi->interp, target->backup_working_area));
3696 /* loop for more e*/
3700 if (goi->isconfigure) {
3701 e = Jim_GetOpt_Nvp(goi, nvp_target_endian, &n);
3703 Jim_GetOpt_NvpUnknown(goi, nvp_target_endian, 1);
3706 target->endianness = n->value;
3708 if (goi->argc != 0) {
3712 n = Jim_Nvp_value2name_simple(nvp_target_endian, target->endianness);
3713 if (n->name == NULL) {
3714 target->endianness = TARGET_LITTLE_ENDIAN;
3715 n = Jim_Nvp_value2name_simple(nvp_target_endian, target->endianness);
3717 Jim_SetResultString(goi->interp, n->name, -1);
3722 if (goi->isconfigure) {
3723 if (goi->argc < 1) {
3724 Jim_SetResult_sprintf(goi->interp,
3729 if (target->variant) {
3730 free((void *)(target->variant));
3732 e = Jim_GetOpt_String(goi, &cp, NULL);
3733 target->variant = strdup(cp);
3735 if (goi->argc != 0) {
3739 Jim_SetResultString(goi->interp, target->variant,-1);
3742 case TCFG_CHAIN_POSITION:
3743 if (goi->isconfigure) {
3746 target_free_all_working_areas(target);
3747 e = Jim_GetOpt_Obj(goi, &o);
3751 tap = jtag_tap_by_jim_obj(goi->interp, o);
3755 /* make this exactly 1 or 0 */
3758 if (goi->argc != 0) {
3762 Jim_SetResultString(interp, target->tap->dotted_name, -1);
3763 /* loop for more e*/
3766 } /* while (goi->argc) */
3769 /* done - we return */
3773 /** this is the 'tcl' handler for the target specific command */
3774 static int tcl_target_func(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
3779 uint8_t target_buf[32];
3782 struct command_context_s *cmd_ctx;
3789 TS_CMD_MWW, TS_CMD_MWH, TS_CMD_MWB,
3790 TS_CMD_MDW, TS_CMD_MDH, TS_CMD_MDB,
3791 TS_CMD_MRW, TS_CMD_MRH, TS_CMD_MRB,
3792 TS_CMD_MEM2ARRAY, TS_CMD_ARRAY2MEM,
3800 TS_CMD_INVOKE_EVENT,
3803 static const Jim_Nvp target_options[] = {
3804 { .name = "configure", .value = TS_CMD_CONFIGURE },
3805 { .name = "cget", .value = TS_CMD_CGET },
3806 { .name = "mww", .value = TS_CMD_MWW },
3807 { .name = "mwh", .value = TS_CMD_MWH },
3808 { .name = "mwb", .value = TS_CMD_MWB },
3809 { .name = "mdw", .value = TS_CMD_MDW },
3810 { .name = "mdh", .value = TS_CMD_MDH },
3811 { .name = "mdb", .value = TS_CMD_MDB },
3812 { .name = "mem2array", .value = TS_CMD_MEM2ARRAY },
3813 { .name = "array2mem", .value = TS_CMD_ARRAY2MEM },
3814 { .name = "eventlist", .value = TS_CMD_EVENTLIST },
3815 { .name = "curstate", .value = TS_CMD_CURSTATE },
3817 { .name = "arp_examine", .value = TS_CMD_EXAMINE },
3818 { .name = "arp_poll", .value = TS_CMD_POLL },
3819 { .name = "arp_reset", .value = TS_CMD_RESET },
3820 { .name = "arp_halt", .value = TS_CMD_HALT },
3821 { .name = "arp_waitstate", .value = TS_CMD_WAITSTATE },
3822 { .name = "invoke-event", .value = TS_CMD_INVOKE_EVENT },
3824 { .name = NULL, .value = -1 },
3827 /* go past the "command" */
3828 Jim_GetOpt_Setup(&goi, interp, argc-1, argv + 1);
3830 target = Jim_CmdPrivData(goi.interp);
3831 cmd_ctx = Jim_GetAssocData(goi.interp, "context");
3833 /* commands here are in an NVP table */
3834 e = Jim_GetOpt_Nvp(&goi, target_options, &n);
3836 Jim_GetOpt_NvpUnknown(&goi, target_options, 0);
3839 /* Assume blank result */
3840 Jim_SetEmptyResult(goi.interp);
3843 case TS_CMD_CONFIGURE:
3845 Jim_WrongNumArgs(goi.interp, goi.argc, goi.argv, "missing: -option VALUE ...");
3848 goi.isconfigure = 1;
3849 return target_configure(&goi, target);
3851 // some things take params
3853 Jim_WrongNumArgs(goi.interp, 0, goi.argv, "missing: ?-option?");
3856 goi.isconfigure = 0;
3857 return target_configure(&goi, target);
3865 * argv[3] = optional count.
3868 if ((goi.argc == 2) || (goi.argc == 3)) {
3872 Jim_SetResult_sprintf(goi.interp, "expected: %s ADDR DATA [COUNT]", n->name);
3876 e = Jim_GetOpt_Wide(&goi, &a);
3881 e = Jim_GetOpt_Wide(&goi, &b);
3885 if (goi.argc == 3) {
3886 e = Jim_GetOpt_Wide(&goi, &c);
3896 target_buffer_set_u32(target, target_buf, b);
3900 target_buffer_set_u16(target, target_buf, b);
3904 target_buffer_set_u8(target, target_buf, b);
3908 for (x = 0 ; x < c ; x++) {
3909 e = target_write_memory(target, a, b, 1, target_buf);
3910 if (e != ERROR_OK) {
3911 Jim_SetResult_sprintf(interp, "Error writing @ 0x%08x: %d\n", (int)(a), e);
3924 /* argv[0] = command
3926 * argv[2] = optional count
3928 if ((goi.argc == 2) || (goi.argc == 3)) {
3929 Jim_SetResult_sprintf(goi.interp, "expected: %s ADDR [COUNT]", n->name);
3932 e = Jim_GetOpt_Wide(&goi, &a);
3937 e = Jim_GetOpt_Wide(&goi, &c);
3944 b = 1; /* shut up gcc */
3957 /* convert to "bytes" */
3959 /* count is now in 'BYTES' */
3965 e = target_read_memory(target, a, b, y / b, target_buf);
3966 if (e != ERROR_OK) {
3967 Jim_SetResult_sprintf(interp, "error reading target @ 0x%08lx", (int)(a));
3971 Jim_fprintf(interp, interp->cookie_stdout, "0x%08x ", (int)(a));
3974 for (x = 0 ; (x < 16) && (x < y) ; x += 4) {
3975 z = target_buffer_get_u32(target, &(target_buf[ x * 4 ]));
3976 Jim_fprintf(interp, interp->cookie_stdout, "%08x ", (int)(z));
3978 for (; (x < 16) ; x += 4) {
3979 Jim_fprintf(interp, interp->cookie_stdout, " ");
3983 for (x = 0 ; (x < 16) && (x < y) ; x += 2) {
3984 z = target_buffer_get_u16(target, &(target_buf[ x * 2 ]));
3985 Jim_fprintf(interp, interp->cookie_stdout, "%04x ", (int)(z));
3987 for (; (x < 16) ; x += 2) {
3988 Jim_fprintf(interp, interp->cookie_stdout, " ");
3993 for (x = 0 ; (x < 16) && (x < y) ; x += 1) {
3994 z = target_buffer_get_u8(target, &(target_buf[ x * 4 ]));
3995 Jim_fprintf(interp, interp->cookie_stdout, "%02x ", (int)(z));
3997 for (; (x < 16) ; x += 1) {
3998 Jim_fprintf(interp, interp->cookie_stdout, " ");
4002 /* ascii-ify the bytes */
4003 for (x = 0 ; x < y ; x++) {
4004 if ((target_buf[x] >= 0x20) &&
4005 (target_buf[x] <= 0x7e)) {
4009 target_buf[x] = '.';
4014 target_buf[x] = ' ';
4019 /* print - with a newline */
4020 Jim_fprintf(interp, interp->cookie_stdout, "%s\n", target_buf);
4026 case TS_CMD_MEM2ARRAY:
4027 return target_mem2array(goi.interp, target, goi.argc, goi.argv);
4029 case TS_CMD_ARRAY2MEM:
4030 return target_array2mem(goi.interp, target, goi.argc, goi.argv);
4032 case TS_CMD_EXAMINE:
4034 Jim_WrongNumArgs(goi.interp, 2, argv, "[no parameters]");
4037 if (!target->tap->enabled)
4038 goto err_tap_disabled;
4039 e = target->type->examine(target);
4040 if (e != ERROR_OK) {
4041 Jim_SetResult_sprintf(interp, "examine-fails: %d", e);
4047 Jim_WrongNumArgs(goi.interp, 2, argv, "[no parameters]");
4050 if (!target->tap->enabled)
4051 goto err_tap_disabled;
4052 if (!(target_was_examined(target))) {
4053 e = ERROR_TARGET_NOT_EXAMINED;
4055 e = target->type->poll(target);
4057 if (e != ERROR_OK) {
4058 Jim_SetResult_sprintf(interp, "poll-fails: %d", e);
4065 if (goi.argc != 2) {
4066 Jim_WrongNumArgs(interp, 2, argv,
4067 "([tT]|[fF]|assert|deassert) BOOL");
4070 e = Jim_GetOpt_Nvp(&goi, nvp_assert, &n);
4072 Jim_GetOpt_NvpUnknown(&goi, nvp_assert, 1);
4075 /* the halt or not param */
4076 e = Jim_GetOpt_Wide(&goi, &a);
4080 if (!target->tap->enabled)
4081 goto err_tap_disabled;
4082 if (!target->type->assert_reset
4083 || !target->type->deassert_reset) {
4084 Jim_SetResult_sprintf(interp,
4085 "No target-specific reset for %s",
4089 /* determine if we should halt or not. */
4090 target->reset_halt = !!a;
4091 /* When this happens - all workareas are invalid. */
4092 target_free_all_working_areas_restore(target, 0);
4095 if (n->value == NVP_ASSERT) {
4096 target->type->assert_reset(target);
4098 target->type->deassert_reset(target);
4103 Jim_WrongNumArgs(goi.interp, 0, argv, "halt [no parameters]");
4106 if (!target->tap->enabled)
4107 goto err_tap_disabled;
4108 target->type->halt(target);
4110 case TS_CMD_WAITSTATE:
4111 /* params: <name> statename timeoutmsecs */
4112 if (goi.argc != 2) {
4113 Jim_SetResult_sprintf(goi.interp, "%s STATENAME TIMEOUTMSECS", n->name);
4116 e = Jim_GetOpt_Nvp(&goi, nvp_target_state, &n);
4118 Jim_GetOpt_NvpUnknown(&goi, nvp_target_state,1);
4121 e = Jim_GetOpt_Wide(&goi, &a);
4125 if (!target->tap->enabled)
4126 goto err_tap_disabled;
4127 e = target_wait_state(target, n->value, a);
4128 if (e != ERROR_OK) {
4129 Jim_SetResult_sprintf(goi.interp,
4130 "target: %s wait %s fails (%d) %s",
4133 e, target_strerror_safe(e));
4138 case TS_CMD_EVENTLIST:
4139 /* List for human, Events defined for this target.
4140 * scripts/programs should use 'name cget -event NAME'
4143 target_event_action_t *teap;
4144 teap = target->event_action;
4145 command_print(cmd_ctx, "Event actions for target (%d) %s\n",
4146 target->target_number,
4148 command_print(cmd_ctx, "%-25s | Body", "Event");
4149 command_print(cmd_ctx, "------------------------- | ----------------------------------------");
4151 command_print(cmd_ctx,
4153 Jim_Nvp_value2name_simple(nvp_target_event, teap->event)->name,
4154 Jim_GetString(teap->body, NULL));
4157 command_print(cmd_ctx, "***END***");
4160 case TS_CMD_CURSTATE:
4161 if (goi.argc != 0) {
4162 Jim_WrongNumArgs(goi.interp, 0, argv, "[no parameters]");
4165 Jim_SetResultString(goi.interp,
4166 target_state_name( target ),
4169 case TS_CMD_INVOKE_EVENT:
4170 if (goi.argc != 1) {
4171 Jim_SetResult_sprintf(goi.interp, "%s ?EVENTNAME?",n->name);
4174 e = Jim_GetOpt_Nvp(&goi, nvp_target_event, &n);
4176 Jim_GetOpt_NvpUnknown(&goi, nvp_target_event, 1);
4179 target_handle_event(target, n->value);
4185 Jim_SetResult_sprintf(interp, "[TAP is disabled]");
4189 static int target_create(Jim_GetOptInfo *goi)
4198 struct command_context_s *cmd_ctx;
4200 cmd_ctx = Jim_GetAssocData(goi->interp, "context");
4201 if (goi->argc < 3) {
4202 Jim_WrongNumArgs(goi->interp, 1, goi->argv, "?name? ?type? ..options...");
4207 Jim_GetOpt_Obj(goi, &new_cmd);
4208 /* does this command exist? */
4209 cmd = Jim_GetCommand(goi->interp, new_cmd, JIM_ERRMSG);
4211 cp = Jim_GetString(new_cmd, NULL);
4212 Jim_SetResult_sprintf(goi->interp, "Command/target: %s Exists", cp);
4217 e = Jim_GetOpt_String(goi, &cp2, NULL);
4219 /* now does target type exist */
4220 for (x = 0 ; target_types[x] ; x++) {
4221 if (0 == strcmp(cp, target_types[x]->name)) {
4226 if (target_types[x] == NULL) {
4227 Jim_SetResult_sprintf(goi->interp, "Unknown target type %s, try one of ", cp);
4228 for (x = 0 ; target_types[x] ; x++) {
4229 if (target_types[x + 1]) {
4230 Jim_AppendStrings(goi->interp,
4231 Jim_GetResult(goi->interp),
4232 target_types[x]->name,
4235 Jim_AppendStrings(goi->interp,
4236 Jim_GetResult(goi->interp),
4238 target_types[x]->name,NULL);
4245 target = calloc(1,sizeof(target_t));
4246 /* set target number */
4247 target->target_number = new_target_number();
4249 /* allocate memory for each unique target type */
4250 target->type = (target_type_t*)calloc(1,sizeof(target_type_t));
4252 memcpy(target->type, target_types[x], sizeof(target_type_t));
4254 /* will be set by "-endian" */
4255 target->endianness = TARGET_ENDIAN_UNKNOWN;
4257 target->working_area = 0x0;
4258 target->working_area_size = 0x0;
4259 target->working_areas = NULL;
4260 target->backup_working_area = 0;
4262 target->state = TARGET_UNKNOWN;
4263 target->debug_reason = DBG_REASON_UNDEFINED;
4264 target->reg_cache = NULL;
4265 target->breakpoints = NULL;
4266 target->watchpoints = NULL;
4267 target->next = NULL;
4268 target->arch_info = NULL;
4270 target->display = 1;
4272 target->halt_issued = false;
4274 /* initialize trace information */
4275 target->trace_info = malloc(sizeof(trace_t));
4276 target->trace_info->num_trace_points = 0;
4277 target->trace_info->trace_points_size = 0;
4278 target->trace_info->trace_points = NULL;
4279 target->trace_info->trace_history_size = 0;
4280 target->trace_info->trace_history = NULL;
4281 target->trace_info->trace_history_pos = 0;
4282 target->trace_info->trace_history_overflowed = 0;
4284 target->dbgmsg = NULL;
4285 target->dbg_msg_enabled = 0;
4287 target->endianness = TARGET_ENDIAN_UNKNOWN;
4289 /* Do the rest as "configure" options */
4290 goi->isconfigure = 1;
4291 e = target_configure(goi, target);
4293 if (target->tap == NULL)
4295 Jim_SetResultString(interp, "-chain-position required when creating target", -1);
4305 if (target->endianness == TARGET_ENDIAN_UNKNOWN) {
4306 /* default endian to little if not specified */
4307 target->endianness = TARGET_LITTLE_ENDIAN;
4310 /* incase variant is not set */
4311 if (!target->variant)
4312 target->variant = strdup("");
4314 /* create the target specific commands */
4315 if (target->type->register_commands) {
4316 (*(target->type->register_commands))(cmd_ctx);
4318 if (target->type->target_create) {
4319 (*(target->type->target_create))(target, goi->interp);
4322 /* append to end of list */
4325 tpp = &(all_targets);
4327 tpp = &((*tpp)->next);
4332 cp = Jim_GetString(new_cmd, NULL);
4333 target->cmd_name = strdup(cp);
4335 /* now - create the new target name command */
4336 e = Jim_CreateCommand(goi->interp,
4339 tcl_target_func, /* C function */
4340 target, /* private data */
4341 NULL); /* no del proc */
4346 static int jim_target(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
4350 struct command_context_s *cmd_ctx;
4354 /* TG = target generic */
4362 const char *target_cmds[] = {
4363 "create", "types", "names", "current", "number",
4365 NULL /* terminate */
4368 LOG_DEBUG("Target command params:");
4369 LOG_DEBUG("%s", Jim_Debug_ArgvString(interp, argc, argv));
4371 cmd_ctx = Jim_GetAssocData(interp, "context");
4373 Jim_GetOpt_Setup(&goi, interp, argc-1, argv + 1);
4375 if (goi.argc == 0) {
4376 Jim_WrongNumArgs(interp, 1, argv, "missing: command ...");
4380 /* Jim_GetOpt_Debug(&goi); */
4381 r = Jim_GetOpt_Enum(&goi, target_cmds, &x);
4388 Jim_Panic(goi.interp,"Why am I here?");
4390 case TG_CMD_CURRENT:
4391 if (goi.argc != 0) {
4392 Jim_WrongNumArgs(goi.interp, 1, goi.argv, "Too many parameters");
4395 Jim_SetResultString(goi.interp, get_current_target(cmd_ctx)->cmd_name, -1);
4398 if (goi.argc != 0) {
4399 Jim_WrongNumArgs(goi.interp, 1, goi.argv, "Too many parameters");
4402 Jim_SetResult(goi.interp, Jim_NewListObj(goi.interp, NULL, 0));
4403 for (x = 0 ; target_types[x] ; x++) {
4404 Jim_ListAppendElement(goi.interp,
4405 Jim_GetResult(goi.interp),
4406 Jim_NewStringObj(goi.interp, target_types[x]->name, -1));
4410 if (goi.argc != 0) {
4411 Jim_WrongNumArgs(goi.interp, 1, goi.argv, "Too many parameters");
4414 Jim_SetResult(goi.interp, Jim_NewListObj(goi.interp, NULL, 0));
4415 target = all_targets;
4417 Jim_ListAppendElement(goi.interp,
4418 Jim_GetResult(goi.interp),
4419 Jim_NewStringObj(goi.interp, target->cmd_name, -1));
4420 target = target->next;
4425 Jim_WrongNumArgs(goi.interp, goi.argc, goi.argv, "?name ... config options ...");
4428 return target_create(&goi);
4431 /* It's OK to remove this mechanism sometime after August 2010 or so */
4432 LOG_WARNING("don't use numbers as target identifiers; use names");
4433 if (goi.argc != 1) {
4434 Jim_SetResult_sprintf(goi.interp, "expected: target number ?NUMBER?");
4437 e = Jim_GetOpt_Wide(&goi, &w);
4441 for (x = 0, target = all_targets; target; target = target->next, x++) {
4442 if (target->target_number == w)
4445 if (target == NULL) {
4446 Jim_SetResult_sprintf(goi.interp,
4447 "Target: number %d does not exist", (int)(w));
4450 Jim_SetResultString(goi.interp, target->cmd_name, -1);
4453 if (goi.argc != 0) {
4454 Jim_WrongNumArgs(goi.interp, 0, goi.argv, "<no parameters>");
4457 for (x = 0, target = all_targets; target; target = target->next, x++)
4459 Jim_SetResult(goi.interp, Jim_NewIntObj(goi.interp, x));
4475 static int fastload_num;
4476 static struct FastLoad *fastload;
4478 static void free_fastload(void)
4480 if (fastload != NULL)
4483 for (i = 0; i < fastload_num; i++)
4485 if (fastload[i].data)
4486 free(fastload[i].data);
4496 static int handle_fast_load_image_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
4500 uint32_t image_size;
4501 uint32_t min_address = 0;
4502 uint32_t max_address = 0xffffffff;
4507 duration_t duration;
4508 char *duration_text;
4510 int retval = parse_load_image_command_args(args, argc,
4511 &image, &min_address, &max_address);
4512 if (ERROR_OK != retval)
4515 duration_start_measure(&duration);
4517 if (image_open(&image, args[0], (argc >= 3) ? args[2] : NULL) != ERROR_OK)
4524 fastload_num = image.num_sections;
4525 fastload = (struct FastLoad *)malloc(sizeof(struct FastLoad)*image.num_sections);
4526 if (fastload == NULL)
4528 image_close(&image);
4531 memset(fastload, 0, sizeof(struct FastLoad)*image.num_sections);
4532 for (i = 0; i < image.num_sections; i++)
4534 buffer = malloc(image.sections[i].size);
4537 command_print(cmd_ctx, "error allocating buffer for section (%d bytes)",
4538 (int)(image.sections[i].size));
4542 if ((retval = image_read_section(&image, i, 0x0, image.sections[i].size, buffer, &buf_cnt)) != ERROR_OK)
4548 uint32_t offset = 0;
4549 uint32_t length = buf_cnt;
4552 /* DANGER!!! beware of unsigned comparision here!!! */
4554 if ((image.sections[i].base_address + buf_cnt >= min_address)&&
4555 (image.sections[i].base_address < max_address))
4557 if (image.sections[i].base_address < min_address)
4559 /* clip addresses below */
4560 offset += min_address-image.sections[i].base_address;
4564 if (image.sections[i].base_address + buf_cnt > max_address)
4566 length -= (image.sections[i].base_address + buf_cnt)-max_address;
4569 fastload[i].address = image.sections[i].base_address + offset;
4570 fastload[i].data = malloc(length);
4571 if (fastload[i].data == NULL)
4576 memcpy(fastload[i].data, buffer + offset, length);
4577 fastload[i].length = length;
4579 image_size += length;
4580 command_print(cmd_ctx, "%u bytes written at address 0x%8.8x",
4581 (unsigned int)length,
4582 ((unsigned int)(image.sections[i].base_address + offset)));
4588 duration_stop_measure(&duration, &duration_text);
4589 if (retval == ERROR_OK)
4591 command_print(cmd_ctx, "Loaded %u bytes in %s", (unsigned int)image_size, duration_text);
4592 command_print(cmd_ctx, "NB!!! image has not been loaded to target, issue a subsequent 'fast_load' to do so.");
4594 free(duration_text);
4596 image_close(&image);
4598 if (retval != ERROR_OK)
4606 static int handle_fast_load_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
4609 return ERROR_COMMAND_SYNTAX_ERROR;
4610 if (fastload == NULL)
4612 LOG_ERROR("No image in memory");
4616 int ms = timeval_ms();
4618 int retval = ERROR_OK;
4619 for (i = 0; i < fastload_num;i++)
4621 target_t *target = get_current_target(cmd_ctx);
4622 command_print(cmd_ctx, "Write to 0x%08x, length 0x%08x",
4623 (unsigned int)(fastload[i].address),
4624 (unsigned int)(fastload[i].length));
4625 if (retval == ERROR_OK)
4627 retval = target_write_buffer(target, fastload[i].address, fastload[i].length, fastload[i].data);
4629 size += fastload[i].length;
4631 int after = timeval_ms();
4632 command_print(cmd_ctx, "Loaded image %f kBytes/s", (float)(size/1024.0)/((float)(after-ms)/1000.0));