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 ***************************************************************************/
36 #include <helper/time_support.h>
37 #include <jtag/jtag.h>
40 #include "target_type.h"
41 #include "target_request.h"
42 #include "breakpoints.h"
48 static int target_array2mem(Jim_Interp *interp, struct target *target,
49 int argc, Jim_Obj *const *argv);
50 static int target_mem2array(Jim_Interp *interp, struct target *target,
51 int argc, Jim_Obj *const *argv);
54 extern struct target_type arm7tdmi_target;
55 extern struct target_type arm720t_target;
56 extern struct target_type arm9tdmi_target;
57 extern struct target_type arm920t_target;
58 extern struct target_type arm966e_target;
59 extern struct target_type arm926ejs_target;
60 extern struct target_type fa526_target;
61 extern struct target_type feroceon_target;
62 extern struct target_type dragonite_target;
63 extern struct target_type xscale_target;
64 extern struct target_type cortexm3_target;
65 extern struct target_type cortexa8_target;
66 extern struct target_type arm11_target;
67 extern struct target_type mips_m4k_target;
68 extern struct target_type avr_target;
69 extern struct target_type dsp563xx_target;
70 extern struct target_type testee_target;
72 struct target_type *target_types[] =
94 struct target *all_targets = NULL;
95 struct target_event_callback *target_event_callbacks = NULL;
96 struct target_timer_callback *target_timer_callbacks = NULL;
98 static const Jim_Nvp nvp_assert[] = {
99 { .name = "assert", NVP_ASSERT },
100 { .name = "deassert", NVP_DEASSERT },
101 { .name = "T", NVP_ASSERT },
102 { .name = "F", NVP_DEASSERT },
103 { .name = "t", NVP_ASSERT },
104 { .name = "f", NVP_DEASSERT },
105 { .name = NULL, .value = -1 }
108 static const Jim_Nvp nvp_error_target[] = {
109 { .value = ERROR_TARGET_INVALID, .name = "err-invalid" },
110 { .value = ERROR_TARGET_INIT_FAILED, .name = "err-init-failed" },
111 { .value = ERROR_TARGET_TIMEOUT, .name = "err-timeout" },
112 { .value = ERROR_TARGET_NOT_HALTED, .name = "err-not-halted" },
113 { .value = ERROR_TARGET_FAILURE, .name = "err-failure" },
114 { .value = ERROR_TARGET_UNALIGNED_ACCESS , .name = "err-unaligned-access" },
115 { .value = ERROR_TARGET_DATA_ABORT , .name = "err-data-abort" },
116 { .value = ERROR_TARGET_RESOURCE_NOT_AVAILABLE , .name = "err-resource-not-available" },
117 { .value = ERROR_TARGET_TRANSLATION_FAULT , .name = "err-translation-fault" },
118 { .value = ERROR_TARGET_NOT_RUNNING, .name = "err-not-running" },
119 { .value = ERROR_TARGET_NOT_EXAMINED, .name = "err-not-examined" },
120 { .value = -1, .name = NULL }
123 const char *target_strerror_safe(int err)
127 n = Jim_Nvp_value2name_simple(nvp_error_target, err);
128 if (n->name == NULL) {
135 static const Jim_Nvp nvp_target_event[] = {
136 { .value = TARGET_EVENT_OLD_gdb_program_config , .name = "old-gdb_program_config" },
137 { .value = TARGET_EVENT_OLD_pre_resume , .name = "old-pre_resume" },
139 { .value = TARGET_EVENT_GDB_HALT, .name = "gdb-halt" },
140 { .value = TARGET_EVENT_HALTED, .name = "halted" },
141 { .value = TARGET_EVENT_RESUMED, .name = "resumed" },
142 { .value = TARGET_EVENT_RESUME_START, .name = "resume-start" },
143 { .value = TARGET_EVENT_RESUME_END, .name = "resume-end" },
145 { .name = "gdb-start", .value = TARGET_EVENT_GDB_START },
146 { .name = "gdb-end", .value = TARGET_EVENT_GDB_END },
148 /* historical name */
150 { .value = TARGET_EVENT_RESET_START, .name = "reset-start" },
152 { .value = TARGET_EVENT_RESET_ASSERT_PRE, .name = "reset-assert-pre" },
153 { .value = TARGET_EVENT_RESET_ASSERT, .name = "reset-assert" },
154 { .value = TARGET_EVENT_RESET_ASSERT_POST, .name = "reset-assert-post" },
155 { .value = TARGET_EVENT_RESET_DEASSERT_PRE, .name = "reset-deassert-pre" },
156 { .value = TARGET_EVENT_RESET_DEASSERT_POST, .name = "reset-deassert-post" },
157 { .value = TARGET_EVENT_RESET_HALT_PRE, .name = "reset-halt-pre" },
158 { .value = TARGET_EVENT_RESET_HALT_POST, .name = "reset-halt-post" },
159 { .value = TARGET_EVENT_RESET_WAIT_PRE, .name = "reset-wait-pre" },
160 { .value = TARGET_EVENT_RESET_WAIT_POST, .name = "reset-wait-post" },
161 { .value = TARGET_EVENT_RESET_INIT, .name = "reset-init" },
162 { .value = TARGET_EVENT_RESET_END, .name = "reset-end" },
164 { .value = TARGET_EVENT_EXAMINE_START, .name = "examine-start" },
165 { .value = TARGET_EVENT_EXAMINE_END, .name = "examine-end" },
167 { .value = TARGET_EVENT_DEBUG_HALTED, .name = "debug-halted" },
168 { .value = TARGET_EVENT_DEBUG_RESUMED, .name = "debug-resumed" },
170 { .value = TARGET_EVENT_GDB_ATTACH, .name = "gdb-attach" },
171 { .value = TARGET_EVENT_GDB_DETACH, .name = "gdb-detach" },
173 { .value = TARGET_EVENT_GDB_FLASH_WRITE_START, .name = "gdb-flash-write-start" },
174 { .value = TARGET_EVENT_GDB_FLASH_WRITE_END , .name = "gdb-flash-write-end" },
176 { .value = TARGET_EVENT_GDB_FLASH_ERASE_START, .name = "gdb-flash-erase-start" },
177 { .value = TARGET_EVENT_GDB_FLASH_ERASE_END , .name = "gdb-flash-erase-end" },
179 { .value = TARGET_EVENT_RESUME_START, .name = "resume-start" },
180 { .value = TARGET_EVENT_RESUMED , .name = "resume-ok" },
181 { .value = TARGET_EVENT_RESUME_END , .name = "resume-end" },
183 { .name = NULL, .value = -1 }
186 static const Jim_Nvp nvp_target_state[] = {
187 { .name = "unknown", .value = TARGET_UNKNOWN },
188 { .name = "running", .value = TARGET_RUNNING },
189 { .name = "halted", .value = TARGET_HALTED },
190 { .name = "reset", .value = TARGET_RESET },
191 { .name = "debug-running", .value = TARGET_DEBUG_RUNNING },
192 { .name = NULL, .value = -1 },
195 static const Jim_Nvp nvp_target_debug_reason [] = {
196 { .name = "debug-request" , .value = DBG_REASON_DBGRQ },
197 { .name = "breakpoint" , .value = DBG_REASON_BREAKPOINT },
198 { .name = "watchpoint" , .value = DBG_REASON_WATCHPOINT },
199 { .name = "watchpoint-and-breakpoint", .value = DBG_REASON_WPTANDBKPT },
200 { .name = "single-step" , .value = DBG_REASON_SINGLESTEP },
201 { .name = "target-not-halted" , .value = DBG_REASON_NOTHALTED },
202 { .name = "undefined" , .value = DBG_REASON_UNDEFINED },
203 { .name = NULL, .value = -1 },
206 static const Jim_Nvp nvp_target_endian[] = {
207 { .name = "big", .value = TARGET_BIG_ENDIAN },
208 { .name = "little", .value = TARGET_LITTLE_ENDIAN },
209 { .name = "be", .value = TARGET_BIG_ENDIAN },
210 { .name = "le", .value = TARGET_LITTLE_ENDIAN },
211 { .name = NULL, .value = -1 },
214 static const Jim_Nvp nvp_reset_modes[] = {
215 { .name = "unknown", .value = RESET_UNKNOWN },
216 { .name = "run" , .value = RESET_RUN },
217 { .name = "halt" , .value = RESET_HALT },
218 { .name = "init" , .value = RESET_INIT },
219 { .name = NULL , .value = -1 },
222 const char *debug_reason_name(struct target *t)
226 cp = Jim_Nvp_value2name_simple(nvp_target_debug_reason,
227 t->debug_reason)->name;
229 LOG_ERROR("Invalid debug reason: %d", (int)(t->debug_reason));
230 cp = "(*BUG*unknown*BUG*)";
236 target_state_name( struct target *t )
239 cp = Jim_Nvp_value2name_simple(nvp_target_state, t->state)->name;
241 LOG_ERROR("Invalid target state: %d", (int)(t->state));
242 cp = "(*BUG*unknown*BUG*)";
247 /* determine the number of the new target */
248 static int new_target_number(void)
253 /* number is 0 based */
257 if (x < t->target_number) {
258 x = t->target_number;
265 /* read a uint32_t from a buffer in target memory endianness */
266 uint32_t target_buffer_get_u32(struct target *target, const uint8_t *buffer)
268 if (target->endianness == TARGET_LITTLE_ENDIAN)
269 return le_to_h_u32(buffer);
271 return be_to_h_u32(buffer);
274 /* read a uint16_t from a buffer in target memory endianness */
275 uint16_t target_buffer_get_u16(struct target *target, const uint8_t *buffer)
277 if (target->endianness == TARGET_LITTLE_ENDIAN)
278 return le_to_h_u16(buffer);
280 return be_to_h_u16(buffer);
283 /* read a uint8_t from a buffer in target memory endianness */
284 uint8_t target_buffer_get_u8(struct target *target, const uint8_t *buffer)
286 return *buffer & 0x0ff;
289 /* write a uint32_t to a buffer in target memory endianness */
290 void target_buffer_set_u32(struct target *target, uint8_t *buffer, uint32_t value)
292 if (target->endianness == TARGET_LITTLE_ENDIAN)
293 h_u32_to_le(buffer, value);
295 h_u32_to_be(buffer, value);
298 /* write a uint16_t to a buffer in target memory endianness */
299 void target_buffer_set_u16(struct target *target, uint8_t *buffer, uint16_t value)
301 if (target->endianness == TARGET_LITTLE_ENDIAN)
302 h_u16_to_le(buffer, value);
304 h_u16_to_be(buffer, value);
307 /* write a uint8_t to a buffer in target memory endianness */
308 void target_buffer_set_u8(struct target *target, uint8_t *buffer, uint8_t value)
313 /* return a pointer to a configured target; id is name or number */
314 struct target *get_target(const char *id)
316 struct target *target;
318 /* try as tcltarget name */
319 for (target = all_targets; target; target = target->next) {
320 if (target->cmd_name == NULL)
322 if (strcmp(id, target->cmd_name) == 0)
326 /* It's OK to remove this fallback sometime after August 2010 or so */
328 /* no match, try as number */
330 if (parse_uint(id, &num) != ERROR_OK)
333 for (target = all_targets; target; target = target->next) {
334 if (target->target_number == (int)num) {
335 LOG_WARNING("use '%s' as target identifier, not '%u'",
336 target->cmd_name, num);
344 /* returns a pointer to the n-th configured target */
345 static struct target *get_target_by_num(int num)
347 struct target *target = all_targets;
350 if (target->target_number == num) {
353 target = target->next;
359 struct target* get_current_target(struct command_context *cmd_ctx)
361 struct target *target = get_target_by_num(cmd_ctx->current_target);
365 LOG_ERROR("BUG: current_target out of bounds");
372 int target_poll(struct target *target)
376 /* We can't poll until after examine */
377 if (!target_was_examined(target))
379 /* Fail silently lest we pollute the log */
383 retval = target->type->poll(target);
384 if (retval != ERROR_OK)
387 if (target->halt_issued)
389 if (target->state == TARGET_HALTED)
391 target->halt_issued = false;
394 long long t = timeval_ms() - target->halt_issued_time;
397 target->halt_issued = false;
398 LOG_INFO("Halt timed out, wake up GDB.");
399 target_call_event_callbacks(target, TARGET_EVENT_GDB_HALT);
407 int target_halt(struct target *target)
410 /* We can't poll until after examine */
411 if (!target_was_examined(target))
413 LOG_ERROR("Target not examined yet");
417 retval = target->type->halt(target);
418 if (retval != ERROR_OK)
421 target->halt_issued = true;
422 target->halt_issued_time = timeval_ms();
428 * Make the target (re)start executing using its saved execution
429 * context (possibly with some modifications).
431 * @param target Which target should start executing.
432 * @param current True to use the target's saved program counter instead
433 * of the address parameter
434 * @param address Optionally used as the program counter.
435 * @param handle_breakpoints True iff breakpoints at the resumption PC
436 * should be skipped. (For example, maybe execution was stopped by
437 * such a breakpoint, in which case it would be counterprodutive to
439 * @param debug_execution False if all working areas allocated by OpenOCD
440 * should be released and/or restored to their original contents.
441 * (This would for example be true to run some downloaded "helper"
442 * algorithm code, which resides in one such working buffer and uses
443 * another for data storage.)
445 * @todo Resolve the ambiguity about what the "debug_execution" flag
446 * signifies. For example, Target implementations don't agree on how
447 * it relates to invalidation of the register cache, or to whether
448 * breakpoints and watchpoints should be enabled. (It would seem wrong
449 * to enable breakpoints when running downloaded "helper" algorithms
450 * (debug_execution true), since the breakpoints would be set to match
451 * target firmware being debugged, not the helper algorithm.... and
452 * enabling them could cause such helpers to malfunction (for example,
453 * by overwriting data with a breakpoint instruction. On the other
454 * hand the infrastructure for running such helpers might use this
455 * procedure but rely on hardware breakpoint to detect termination.)
457 int target_resume(struct target *target, int current, uint32_t address, int handle_breakpoints, int debug_execution)
461 /* We can't poll until after examine */
462 if (!target_was_examined(target))
464 LOG_ERROR("Target not examined yet");
468 /* note that resume *must* be asynchronous. The CPU can halt before
469 * we poll. The CPU can even halt at the current PC as a result of
470 * a software breakpoint being inserted by (a bug?) the application.
472 if ((retval = target->type->resume(target, current, address, handle_breakpoints, debug_execution)) != ERROR_OK)
478 int target_process_reset(struct command_context *cmd_ctx, enum target_reset_mode reset_mode)
483 n = Jim_Nvp_value2name_simple(nvp_reset_modes, reset_mode);
484 if (n->name == NULL) {
485 LOG_ERROR("invalid reset mode");
489 /* disable polling during reset to make reset event scripts
490 * more predictable, i.e. dr/irscan & pathmove in events will
491 * not have JTAG operations injected into the middle of a sequence.
493 bool save_poll = jtag_poll_get_enabled();
495 jtag_poll_set_enabled(false);
497 sprintf(buf, "ocd_process_reset %s", n->name);
498 retval = Jim_Eval(cmd_ctx->interp, buf);
500 jtag_poll_set_enabled(save_poll);
502 if (retval != JIM_OK) {
503 Jim_PrintErrorMessage(cmd_ctx->interp);
507 /* We want any events to be processed before the prompt */
508 retval = target_call_timer_callbacks_now();
510 struct target *target;
511 for (target = all_targets; target; target = target->next) {
512 target->type->check_reset(target);
518 static int identity_virt2phys(struct target *target,
519 uint32_t virtual, uint32_t *physical)
525 static int no_mmu(struct target *target, int *enabled)
531 static int default_examine(struct target *target)
533 target_set_examined(target);
537 /* no check by default */
538 static int default_check_reset(struct target *target)
543 int target_examine_one(struct target *target)
545 return target->type->examine(target);
548 static int jtag_enable_callback(enum jtag_event event, void *priv)
550 struct target *target = priv;
552 if (event != JTAG_TAP_EVENT_ENABLE || !target->tap->enabled)
555 jtag_unregister_event_callback(jtag_enable_callback, target);
556 return target_examine_one(target);
560 /* Targets that correctly implement init + examine, i.e.
561 * no communication with target during init:
565 int target_examine(void)
567 int retval = ERROR_OK;
568 struct target *target;
570 for (target = all_targets; target; target = target->next)
572 /* defer examination, but don't skip it */
573 if (!target->tap->enabled) {
574 jtag_register_event_callback(jtag_enable_callback,
578 if ((retval = target_examine_one(target)) != ERROR_OK)
583 const char *target_type_name(struct target *target)
585 return target->type->name;
588 static int target_write_memory_imp(struct target *target, uint32_t address, uint32_t size, uint32_t count, uint8_t *buffer)
590 if (!target_was_examined(target))
592 LOG_ERROR("Target not examined yet");
595 return target->type->write_memory_imp(target, address, size, count, buffer);
598 static int target_read_memory_imp(struct target *target, uint32_t address, uint32_t size, uint32_t count, uint8_t *buffer)
600 if (!target_was_examined(target))
602 LOG_ERROR("Target not examined yet");
605 return target->type->read_memory_imp(target, address, size, count, buffer);
608 static int target_soft_reset_halt_imp(struct target *target)
610 if (!target_was_examined(target))
612 LOG_ERROR("Target not examined yet");
615 if (!target->type->soft_reset_halt_imp) {
616 LOG_ERROR("Target %s does not support soft_reset_halt",
617 target_name(target));
620 return target->type->soft_reset_halt_imp(target);
623 static int target_run_algorithm_imp(struct target *target, int num_mem_params, struct mem_param *mem_params, int num_reg_params, struct reg_param *reg_param, uint32_t entry_point, uint32_t exit_point, int timeout_ms, void *arch_info)
625 if (!target_was_examined(target))
627 LOG_ERROR("Target not examined yet");
630 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);
633 int target_read_memory(struct target *target,
634 uint32_t address, uint32_t size, uint32_t count, uint8_t *buffer)
636 return target->type->read_memory(target, address, size, count, buffer);
639 int target_read_phys_memory(struct target *target,
640 uint32_t address, uint32_t size, uint32_t count, uint8_t *buffer)
642 return target->type->read_phys_memory(target, address, size, count, buffer);
645 int target_write_memory(struct target *target,
646 uint32_t address, uint32_t size, uint32_t count, uint8_t *buffer)
648 return target->type->write_memory(target, address, size, count, buffer);
651 int target_write_phys_memory(struct target *target,
652 uint32_t address, uint32_t size, uint32_t count, uint8_t *buffer)
654 return target->type->write_phys_memory(target, address, size, count, buffer);
657 int target_bulk_write_memory(struct target *target,
658 uint32_t address, uint32_t count, uint8_t *buffer)
660 return target->type->bulk_write_memory(target, address, count, buffer);
663 int target_add_breakpoint(struct target *target,
664 struct breakpoint *breakpoint)
666 if (target->state != TARGET_HALTED) {
667 LOG_WARNING("target %s is not halted", target->cmd_name);
668 return ERROR_TARGET_NOT_HALTED;
670 return target->type->add_breakpoint(target, breakpoint);
672 int target_remove_breakpoint(struct target *target,
673 struct breakpoint *breakpoint)
675 return target->type->remove_breakpoint(target, breakpoint);
678 int target_add_watchpoint(struct target *target,
679 struct watchpoint *watchpoint)
681 if (target->state != TARGET_HALTED) {
682 LOG_WARNING("target %s is not halted", target->cmd_name);
683 return ERROR_TARGET_NOT_HALTED;
685 return target->type->add_watchpoint(target, watchpoint);
687 int target_remove_watchpoint(struct target *target,
688 struct watchpoint *watchpoint)
690 return target->type->remove_watchpoint(target, watchpoint);
693 int target_get_gdb_reg_list(struct target *target,
694 struct reg **reg_list[], int *reg_list_size)
696 return target->type->get_gdb_reg_list(target, reg_list, reg_list_size);
698 int target_step(struct target *target,
699 int current, uint32_t address, int handle_breakpoints)
701 return target->type->step(target, current, address, handle_breakpoints);
705 int target_run_algorithm(struct target *target,
706 int num_mem_params, struct mem_param *mem_params,
707 int num_reg_params, struct reg_param *reg_param,
708 uint32_t entry_point, uint32_t exit_point,
709 int timeout_ms, void *arch_info)
711 return target->type->run_algorithm(target,
712 num_mem_params, mem_params, num_reg_params, reg_param,
713 entry_point, exit_point, timeout_ms, arch_info);
717 * Reset the @c examined flag for the given target.
718 * Pure paranoia -- targets are zeroed on allocation.
720 static void target_reset_examined(struct target *target)
722 target->examined = false;
726 err_read_phys_memory(struct target *target, uint32_t address,
727 uint32_t size, uint32_t count, uint8_t *buffer)
729 LOG_ERROR("Not implemented: %s", __func__);
734 err_write_phys_memory(struct target *target, uint32_t address,
735 uint32_t size, uint32_t count, uint8_t *buffer)
737 LOG_ERROR("Not implemented: %s", __func__);
741 static int handle_target(void *priv);
743 static int target_init_one(struct command_context *cmd_ctx,
744 struct target *target)
746 target_reset_examined(target);
748 struct target_type *type = target->type;
749 if (type->examine == NULL)
750 type->examine = default_examine;
752 if (type->check_reset== NULL)
753 type->check_reset = default_check_reset;
755 int retval = type->init_target(cmd_ctx, target);
756 if (ERROR_OK != retval)
758 LOG_ERROR("target '%s' init failed", target_name(target));
763 * @todo get rid of those *memory_imp() methods, now that all
764 * callers are using target_*_memory() accessors ... and make
765 * sure the "physical" paths handle the same issues.
767 /* a non-invasive way(in terms of patches) to add some code that
768 * runs before the type->write/read_memory implementation
770 type->write_memory_imp = target->type->write_memory;
771 type->write_memory = target_write_memory_imp;
773 type->read_memory_imp = target->type->read_memory;
774 type->read_memory = target_read_memory_imp;
776 type->soft_reset_halt_imp = target->type->soft_reset_halt;
777 type->soft_reset_halt = target_soft_reset_halt_imp;
779 type->run_algorithm_imp = target->type->run_algorithm;
780 type->run_algorithm = target_run_algorithm_imp;
782 /* Sanity-check MMU support ... stub in what we must, to help
783 * implement it in stages, but warn if we need to do so.
787 if (type->write_phys_memory == NULL)
789 LOG_ERROR("type '%s' is missing write_phys_memory",
791 type->write_phys_memory = err_write_phys_memory;
793 if (type->read_phys_memory == NULL)
795 LOG_ERROR("type '%s' is missing read_phys_memory",
797 type->read_phys_memory = err_read_phys_memory;
799 if (type->virt2phys == NULL)
801 LOG_ERROR("type '%s' is missing virt2phys", type->name);
802 type->virt2phys = identity_virt2phys;
807 /* Make sure no-MMU targets all behave the same: make no
808 * distinction between physical and virtual addresses, and
809 * ensure that virt2phys() is always an identity mapping.
811 if (type->write_phys_memory || type->read_phys_memory
814 LOG_WARNING("type '%s' has bad MMU hooks", type->name);
818 type->write_phys_memory = type->write_memory;
819 type->read_phys_memory = type->read_memory;
820 type->virt2phys = identity_virt2phys;
825 int target_init(struct command_context *cmd_ctx)
827 struct target *target;
830 for (target = all_targets; target; target = target->next)
832 retval = target_init_one(cmd_ctx, target);
833 if (ERROR_OK != retval)
840 retval = target_register_user_commands(cmd_ctx);
841 if (ERROR_OK != retval)
844 retval = target_register_timer_callback(&handle_target,
845 100, 1, cmd_ctx->interp);
846 if (ERROR_OK != retval)
852 COMMAND_HANDLER(handle_target_init_command)
855 return ERROR_COMMAND_SYNTAX_ERROR;
857 static bool target_initialized = false;
858 if (target_initialized)
860 LOG_INFO("'target init' has already been called");
863 target_initialized = true;
865 LOG_DEBUG("Initializing targets...");
866 return target_init(CMD_CTX);
869 int target_register_event_callback(int (*callback)(struct target *target, enum target_event event, void *priv), void *priv)
871 struct target_event_callback **callbacks_p = &target_event_callbacks;
873 if (callback == NULL)
875 return ERROR_INVALID_ARGUMENTS;
880 while ((*callbacks_p)->next)
881 callbacks_p = &((*callbacks_p)->next);
882 callbacks_p = &((*callbacks_p)->next);
885 (*callbacks_p) = malloc(sizeof(struct target_event_callback));
886 (*callbacks_p)->callback = callback;
887 (*callbacks_p)->priv = priv;
888 (*callbacks_p)->next = NULL;
893 int target_register_timer_callback(int (*callback)(void *priv), int time_ms, int periodic, void *priv)
895 struct target_timer_callback **callbacks_p = &target_timer_callbacks;
898 if (callback == NULL)
900 return ERROR_INVALID_ARGUMENTS;
905 while ((*callbacks_p)->next)
906 callbacks_p = &((*callbacks_p)->next);
907 callbacks_p = &((*callbacks_p)->next);
910 (*callbacks_p) = malloc(sizeof(struct target_timer_callback));
911 (*callbacks_p)->callback = callback;
912 (*callbacks_p)->periodic = periodic;
913 (*callbacks_p)->time_ms = time_ms;
915 gettimeofday(&now, NULL);
916 (*callbacks_p)->when.tv_usec = now.tv_usec + (time_ms % 1000) * 1000;
917 time_ms -= (time_ms % 1000);
918 (*callbacks_p)->when.tv_sec = now.tv_sec + (time_ms / 1000);
919 if ((*callbacks_p)->when.tv_usec > 1000000)
921 (*callbacks_p)->when.tv_usec = (*callbacks_p)->when.tv_usec - 1000000;
922 (*callbacks_p)->when.tv_sec += 1;
925 (*callbacks_p)->priv = priv;
926 (*callbacks_p)->next = NULL;
931 int target_unregister_event_callback(int (*callback)(struct target *target, enum target_event event, void *priv), void *priv)
933 struct target_event_callback **p = &target_event_callbacks;
934 struct target_event_callback *c = target_event_callbacks;
936 if (callback == NULL)
938 return ERROR_INVALID_ARGUMENTS;
943 struct target_event_callback *next = c->next;
944 if ((c->callback == callback) && (c->priv == priv))
958 int target_unregister_timer_callback(int (*callback)(void *priv), void *priv)
960 struct target_timer_callback **p = &target_timer_callbacks;
961 struct target_timer_callback *c = target_timer_callbacks;
963 if (callback == NULL)
965 return ERROR_INVALID_ARGUMENTS;
970 struct target_timer_callback *next = c->next;
971 if ((c->callback == callback) && (c->priv == priv))
985 int target_call_event_callbacks(struct target *target, enum target_event event)
987 struct target_event_callback *callback = target_event_callbacks;
988 struct target_event_callback *next_callback;
990 if (event == TARGET_EVENT_HALTED)
992 /* execute early halted first */
993 target_call_event_callbacks(target, TARGET_EVENT_GDB_HALT);
996 LOG_DEBUG("target event %i (%s)",
998 Jim_Nvp_value2name_simple(nvp_target_event, event)->name);
1000 target_handle_event(target, event);
1004 next_callback = callback->next;
1005 callback->callback(target, event, callback->priv);
1006 callback = next_callback;
1012 static int target_timer_callback_periodic_restart(
1013 struct target_timer_callback *cb, struct timeval *now)
1015 int time_ms = cb->time_ms;
1016 cb->when.tv_usec = now->tv_usec + (time_ms % 1000) * 1000;
1017 time_ms -= (time_ms % 1000);
1018 cb->when.tv_sec = now->tv_sec + time_ms / 1000;
1019 if (cb->when.tv_usec > 1000000)
1021 cb->when.tv_usec = cb->when.tv_usec - 1000000;
1022 cb->when.tv_sec += 1;
1027 static int target_call_timer_callback(struct target_timer_callback *cb,
1028 struct timeval *now)
1030 cb->callback(cb->priv);
1033 return target_timer_callback_periodic_restart(cb, now);
1035 return target_unregister_timer_callback(cb->callback, cb->priv);
1038 static int target_call_timer_callbacks_check_time(int checktime)
1043 gettimeofday(&now, NULL);
1045 struct target_timer_callback *callback = target_timer_callbacks;
1048 // cleaning up may unregister and free this callback
1049 struct target_timer_callback *next_callback = callback->next;
1051 bool call_it = callback->callback &&
1052 ((!checktime && callback->periodic) ||
1053 now.tv_sec > callback->when.tv_sec ||
1054 (now.tv_sec == callback->when.tv_sec &&
1055 now.tv_usec >= callback->when.tv_usec));
1059 int retval = target_call_timer_callback(callback, &now);
1060 if (retval != ERROR_OK)
1064 callback = next_callback;
1070 int target_call_timer_callbacks(void)
1072 return target_call_timer_callbacks_check_time(1);
1075 /* invoke periodic callbacks immediately */
1076 int target_call_timer_callbacks_now(void)
1078 return target_call_timer_callbacks_check_time(0);
1081 int target_alloc_working_area(struct target *target, uint32_t size, struct working_area **area)
1083 struct working_area *c = target->working_areas;
1084 struct working_area *new_wa = NULL;
1086 /* Reevaluate working area address based on MMU state*/
1087 if (target->working_areas == NULL)
1092 retval = target->type->mmu(target, &enabled);
1093 if (retval != ERROR_OK)
1099 if (target->working_area_phys_spec) {
1100 LOG_DEBUG("MMU disabled, using physical "
1101 "address for working memory 0x%08x",
1102 (unsigned)target->working_area_phys);
1103 target->working_area = target->working_area_phys;
1105 LOG_ERROR("No working memory available. "
1106 "Specify -work-area-phys to target.");
1107 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
1110 if (target->working_area_virt_spec) {
1111 LOG_DEBUG("MMU enabled, using virtual "
1112 "address for working memory 0x%08x",
1113 (unsigned)target->working_area_virt);
1114 target->working_area = target->working_area_virt;
1116 LOG_ERROR("No working memory available. "
1117 "Specify -work-area-virt to target.");
1118 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
1123 /* only allocate multiples of 4 byte */
1126 LOG_ERROR("BUG: code tried to allocate unaligned number of bytes (0x%08x), padding", ((unsigned)(size)));
1127 size = (size + 3) & (~3);
1130 /* see if there's already a matching working area */
1133 if ((c->free) && (c->size == size))
1141 /* if not, allocate a new one */
1144 struct working_area **p = &target->working_areas;
1145 uint32_t first_free = target->working_area;
1146 uint32_t free_size = target->working_area_size;
1148 c = target->working_areas;
1151 first_free += c->size;
1152 free_size -= c->size;
1157 if (free_size < size)
1159 LOG_WARNING("not enough working area available(requested %u, free %u)",
1160 (unsigned)(size), (unsigned)(free_size));
1161 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
1164 LOG_DEBUG("allocated new working area at address 0x%08x", (unsigned)first_free);
1166 new_wa = malloc(sizeof(struct working_area));
1167 new_wa->next = NULL;
1168 new_wa->size = size;
1169 new_wa->address = first_free;
1171 if (target->backup_working_area)
1174 new_wa->backup = malloc(new_wa->size);
1175 if ((retval = target_read_memory(target, new_wa->address, 4, new_wa->size / 4, new_wa->backup)) != ERROR_OK)
1177 free(new_wa->backup);
1184 new_wa->backup = NULL;
1187 /* put new entry in list */
1191 /* mark as used, and return the new (reused) area */
1196 new_wa->user = area;
1201 int target_free_working_area_restore(struct target *target, struct working_area *area, int restore)
1206 if (restore && target->backup_working_area)
1209 if ((retval = target_write_memory(target, area->address, 4, area->size / 4, area->backup)) != ERROR_OK)
1215 /* mark user pointer invalid */
1222 int target_free_working_area(struct target *target, struct working_area *area)
1224 return target_free_working_area_restore(target, area, 1);
1227 /* free resources and restore memory, if restoring memory fails,
1228 * free up resources anyway
1230 void target_free_all_working_areas_restore(struct target *target, int restore)
1232 struct working_area *c = target->working_areas;
1236 struct working_area *next = c->next;
1237 target_free_working_area_restore(target, c, restore);
1247 target->working_areas = NULL;
1250 void target_free_all_working_areas(struct target *target)
1252 target_free_all_working_areas_restore(target, 1);
1255 int target_arch_state(struct target *target)
1260 LOG_USER("No target has been configured");
1264 LOG_USER("target state: %s", target_state_name( target ));
1266 if (target->state != TARGET_HALTED)
1269 retval = target->type->arch_state(target);
1273 /* Single aligned words are guaranteed to use 16 or 32 bit access
1274 * mode respectively, otherwise data is handled as quickly as
1277 int target_write_buffer(struct target *target, uint32_t address, uint32_t size, uint8_t *buffer)
1280 LOG_DEBUG("writing buffer of %i byte at 0x%8.8x",
1281 (int)size, (unsigned)address);
1283 if (!target_was_examined(target))
1285 LOG_ERROR("Target not examined yet");
1293 if ((address + size - 1) < address)
1295 /* GDB can request this when e.g. PC is 0xfffffffc*/
1296 LOG_ERROR("address + size wrapped(0x%08x, 0x%08x)",
1302 if (((address % 2) == 0) && (size == 2))
1304 return target_write_memory(target, address, 2, 1, buffer);
1307 /* handle unaligned head bytes */
1310 uint32_t unaligned = 4 - (address % 4);
1312 if (unaligned > size)
1315 if ((retval = target_write_memory(target, address, 1, unaligned, buffer)) != ERROR_OK)
1318 buffer += unaligned;
1319 address += unaligned;
1323 /* handle aligned words */
1326 int aligned = size - (size % 4);
1328 /* use bulk writes above a certain limit. This may have to be changed */
1331 if ((retval = target->type->bulk_write_memory(target, address, aligned / 4, buffer)) != ERROR_OK)
1336 if ((retval = target_write_memory(target, address, 4, aligned / 4, buffer)) != ERROR_OK)
1345 /* handle tail writes of less than 4 bytes */
1348 if ((retval = target_write_memory(target, address, 1, size, buffer)) != ERROR_OK)
1355 /* Single aligned words are guaranteed to use 16 or 32 bit access
1356 * mode respectively, otherwise data is handled as quickly as
1359 int target_read_buffer(struct target *target, uint32_t address, uint32_t size, uint8_t *buffer)
1362 LOG_DEBUG("reading buffer of %i byte at 0x%8.8x",
1363 (int)size, (unsigned)address);
1365 if (!target_was_examined(target))
1367 LOG_ERROR("Target not examined yet");
1375 if ((address + size - 1) < address)
1377 /* GDB can request this when e.g. PC is 0xfffffffc*/
1378 LOG_ERROR("address + size wrapped(0x%08" PRIx32 ", 0x%08" PRIx32 ")",
1384 if (((address % 2) == 0) && (size == 2))
1386 return target_read_memory(target, address, 2, 1, buffer);
1389 /* handle unaligned head bytes */
1392 uint32_t unaligned = 4 - (address % 4);
1394 if (unaligned > size)
1397 if ((retval = target_read_memory(target, address, 1, unaligned, buffer)) != ERROR_OK)
1400 buffer += unaligned;
1401 address += unaligned;
1405 /* handle aligned words */
1408 int aligned = size - (size % 4);
1410 if ((retval = target_read_memory(target, address, 4, aligned / 4, buffer)) != ERROR_OK)
1418 /*prevent byte access when possible (avoid AHB access limitations in some cases)*/
1421 int aligned = size - (size%2);
1422 retval = target_read_memory(target, address, 2, aligned / 2, buffer);
1423 if (retval != ERROR_OK)
1430 /* handle tail writes of less than 4 bytes */
1433 if ((retval = target_read_memory(target, address, 1, size, buffer)) != ERROR_OK)
1440 int target_checksum_memory(struct target *target, uint32_t address, uint32_t size, uint32_t* crc)
1445 uint32_t checksum = 0;
1446 if (!target_was_examined(target))
1448 LOG_ERROR("Target not examined yet");
1452 if ((retval = target->type->checksum_memory(target, address,
1453 size, &checksum)) != ERROR_OK)
1455 buffer = malloc(size);
1458 LOG_ERROR("error allocating buffer for section (%d bytes)", (int)size);
1459 return ERROR_INVALID_ARGUMENTS;
1461 retval = target_read_buffer(target, address, size, buffer);
1462 if (retval != ERROR_OK)
1468 /* convert to target endianess */
1469 for (i = 0; i < (size/sizeof(uint32_t)); i++)
1471 uint32_t target_data;
1472 target_data = target_buffer_get_u32(target, &buffer[i*sizeof(uint32_t)]);
1473 target_buffer_set_u32(target, &buffer[i*sizeof(uint32_t)], target_data);
1476 retval = image_calculate_checksum(buffer, size, &checksum);
1485 int target_blank_check_memory(struct target *target, uint32_t address, uint32_t size, uint32_t* blank)
1488 if (!target_was_examined(target))
1490 LOG_ERROR("Target not examined yet");
1494 if (target->type->blank_check_memory == 0)
1495 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
1497 retval = target->type->blank_check_memory(target, address, size, blank);
1502 int target_read_u32(struct target *target, uint32_t address, uint32_t *value)
1504 uint8_t value_buf[4];
1505 if (!target_was_examined(target))
1507 LOG_ERROR("Target not examined yet");
1511 int retval = target_read_memory(target, address, 4, 1, value_buf);
1513 if (retval == ERROR_OK)
1515 *value = target_buffer_get_u32(target, value_buf);
1516 LOG_DEBUG("address: 0x%8.8" PRIx32 ", value: 0x%8.8" PRIx32 "",
1523 LOG_DEBUG("address: 0x%8.8" PRIx32 " failed",
1530 int target_read_u16(struct target *target, uint32_t address, uint16_t *value)
1532 uint8_t value_buf[2];
1533 if (!target_was_examined(target))
1535 LOG_ERROR("Target not examined yet");
1539 int retval = target_read_memory(target, address, 2, 1, value_buf);
1541 if (retval == ERROR_OK)
1543 *value = target_buffer_get_u16(target, value_buf);
1544 LOG_DEBUG("address: 0x%8.8" PRIx32 ", value: 0x%4.4x",
1551 LOG_DEBUG("address: 0x%8.8" PRIx32 " failed",
1558 int target_read_u8(struct target *target, uint32_t address, uint8_t *value)
1560 int retval = target_read_memory(target, address, 1, 1, value);
1561 if (!target_was_examined(target))
1563 LOG_ERROR("Target not examined yet");
1567 if (retval == ERROR_OK)
1569 LOG_DEBUG("address: 0x%8.8" PRIx32 ", value: 0x%2.2x",
1576 LOG_DEBUG("address: 0x%8.8" PRIx32 " failed",
1583 int target_write_u32(struct target *target, uint32_t address, uint32_t value)
1586 uint8_t value_buf[4];
1587 if (!target_was_examined(target))
1589 LOG_ERROR("Target not examined yet");
1593 LOG_DEBUG("address: 0x%8.8" PRIx32 ", value: 0x%8.8" PRIx32 "",
1597 target_buffer_set_u32(target, value_buf, value);
1598 if ((retval = target_write_memory(target, address, 4, 1, value_buf)) != ERROR_OK)
1600 LOG_DEBUG("failed: %i", retval);
1606 int target_write_u16(struct target *target, uint32_t address, uint16_t value)
1609 uint8_t value_buf[2];
1610 if (!target_was_examined(target))
1612 LOG_ERROR("Target not examined yet");
1616 LOG_DEBUG("address: 0x%8.8" PRIx32 ", value: 0x%8.8x",
1620 target_buffer_set_u16(target, value_buf, value);
1621 if ((retval = target_write_memory(target, address, 2, 1, value_buf)) != ERROR_OK)
1623 LOG_DEBUG("failed: %i", retval);
1629 int target_write_u8(struct target *target, uint32_t address, uint8_t value)
1632 if (!target_was_examined(target))
1634 LOG_ERROR("Target not examined yet");
1638 LOG_DEBUG("address: 0x%8.8" PRIx32 ", value: 0x%2.2x",
1641 if ((retval = target_write_memory(target, address, 1, 1, &value)) != ERROR_OK)
1643 LOG_DEBUG("failed: %i", retval);
1649 COMMAND_HANDLER(handle_targets_command)
1651 struct target *target = all_targets;
1655 target = get_target(CMD_ARGV[0]);
1656 if (target == NULL) {
1657 command_print(CMD_CTX,"Target: %s is unknown, try one of:\n", CMD_ARGV[0]);
1660 if (!target->tap->enabled) {
1661 command_print(CMD_CTX,"Target: TAP %s is disabled, "
1662 "can't be the current target\n",
1663 target->tap->dotted_name);
1667 CMD_CTX->current_target = target->target_number;
1672 target = all_targets;
1673 command_print(CMD_CTX, " TargetName Type Endian TapName State ");
1674 command_print(CMD_CTX, "-- ------------------ ---------- ------ ------------------ ------------");
1680 if (target->tap->enabled)
1681 state = target_state_name( target );
1683 state = "tap-disabled";
1685 if (CMD_CTX->current_target == target->target_number)
1688 /* keep columns lined up to match the headers above */
1689 command_print(CMD_CTX, "%2d%c %-18s %-10s %-6s %-18s %s",
1690 target->target_number,
1692 target_name(target),
1693 target_type_name(target),
1694 Jim_Nvp_value2name_simple(nvp_target_endian,
1695 target->endianness)->name,
1696 target->tap->dotted_name,
1698 target = target->next;
1704 /* every 300ms we check for reset & powerdropout and issue a "reset halt" if so. */
1706 static int powerDropout;
1707 static int srstAsserted;
1709 static int runPowerRestore;
1710 static int runPowerDropout;
1711 static int runSrstAsserted;
1712 static int runSrstDeasserted;
1714 static int sense_handler(void)
1716 static int prevSrstAsserted = 0;
1717 static int prevPowerdropout = 0;
1720 if ((retval = jtag_power_dropout(&powerDropout)) != ERROR_OK)
1724 powerRestored = prevPowerdropout && !powerDropout;
1727 runPowerRestore = 1;
1730 long long current = timeval_ms();
1731 static long long lastPower = 0;
1732 int waitMore = lastPower + 2000 > current;
1733 if (powerDropout && !waitMore)
1735 runPowerDropout = 1;
1736 lastPower = current;
1739 if ((retval = jtag_srst_asserted(&srstAsserted)) != ERROR_OK)
1743 srstDeasserted = prevSrstAsserted && !srstAsserted;
1745 static long long lastSrst = 0;
1746 waitMore = lastSrst + 2000 > current;
1747 if (srstDeasserted && !waitMore)
1749 runSrstDeasserted = 1;
1753 if (!prevSrstAsserted && srstAsserted)
1755 runSrstAsserted = 1;
1758 prevSrstAsserted = srstAsserted;
1759 prevPowerdropout = powerDropout;
1761 if (srstDeasserted || powerRestored)
1763 /* Other than logging the event we can't do anything here.
1764 * Issuing a reset is a particularly bad idea as we might
1765 * be inside a reset already.
1772 /* process target state changes */
1773 static int handle_target(void *priv)
1775 Jim_Interp *interp = (Jim_Interp *)priv;
1776 int retval = ERROR_OK;
1778 /* we do not want to recurse here... */
1779 static int recursive = 0;
1784 /* danger! running these procedures can trigger srst assertions and power dropouts.
1785 * We need to avoid an infinite loop/recursion here and we do that by
1786 * clearing the flags after running these events.
1788 int did_something = 0;
1789 if (runSrstAsserted)
1791 LOG_INFO("srst asserted detected, running srst_asserted proc.");
1792 Jim_Eval(interp, "srst_asserted");
1795 if (runSrstDeasserted)
1797 Jim_Eval(interp, "srst_deasserted");
1800 if (runPowerDropout)
1802 LOG_INFO("Power dropout detected, running power_dropout proc.");
1803 Jim_Eval(interp, "power_dropout");
1806 if (runPowerRestore)
1808 Jim_Eval(interp, "power_restore");
1814 /* clear detect flags */
1818 /* clear action flags */
1820 runSrstAsserted = 0;
1821 runSrstDeasserted = 0;
1822 runPowerRestore = 0;
1823 runPowerDropout = 0;
1828 /* Poll targets for state changes unless that's globally disabled.
1829 * Skip targets that are currently disabled.
1831 for (struct target *target = all_targets;
1832 is_jtag_poll_safe() && target;
1833 target = target->next)
1835 if (!target->tap->enabled)
1838 /* only poll target if we've got power and srst isn't asserted */
1839 if (!powerDropout && !srstAsserted)
1841 /* polling may fail silently until the target has been examined */
1842 if ((retval = target_poll(target)) != ERROR_OK)
1844 /* FIX!!!!! If we add a LOG_INFO() here to output a line in GDB
1845 * *why* we are aborting GDB, then we'll spam telnet when the
1846 * poll is failing persistently.
1848 * If we could implement an event that detected the
1849 * target going from non-pollable to pollable, we could issue
1850 * an error only upon the transition.
1852 target_call_event_callbacks(target, TARGET_EVENT_GDB_HALT);
1861 COMMAND_HANDLER(handle_reg_command)
1863 struct target *target;
1864 struct reg *reg = NULL;
1870 target = get_current_target(CMD_CTX);
1872 /* list all available registers for the current target */
1875 struct reg_cache *cache = target->reg_cache;
1882 command_print(CMD_CTX, "===== %s", cache->name);
1884 for (i = 0, reg = cache->reg_list;
1885 i < cache->num_regs;
1886 i++, reg++, count++)
1888 /* only print cached values if they are valid */
1890 value = buf_to_str(reg->value,
1892 command_print(CMD_CTX,
1893 "(%i) %s (/%" PRIu32 "): 0x%s%s",
1901 command_print(CMD_CTX, "(%i) %s (/%" PRIu32 ")",
1906 cache = cache->next;
1912 /* access a single register by its ordinal number */
1913 if ((CMD_ARGV[0][0] >= '0') && (CMD_ARGV[0][0] <= '9'))
1916 COMMAND_PARSE_NUMBER(uint, CMD_ARGV[0], num);
1918 struct reg_cache *cache = target->reg_cache;
1923 for (i = 0; i < cache->num_regs; i++)
1927 reg = &cache->reg_list[i];
1933 cache = cache->next;
1938 command_print(CMD_CTX, "%i is out of bounds, the current target has only %i registers (0 - %i)", num, count, count - 1);
1941 } else /* access a single register by its name */
1943 reg = register_get_by_name(target->reg_cache, CMD_ARGV[0], 1);
1947 command_print(CMD_CTX, "register %s not found in current target", CMD_ARGV[0]);
1952 /* display a register */
1953 if ((CMD_ARGC == 1) || ((CMD_ARGC == 2) && !((CMD_ARGV[1][0] >= '0') && (CMD_ARGV[1][0] <= '9'))))
1955 if ((CMD_ARGC == 2) && (strcmp(CMD_ARGV[1], "force") == 0))
1958 if (reg->valid == 0)
1960 reg->type->get(reg);
1962 value = buf_to_str(reg->value, reg->size, 16);
1963 command_print(CMD_CTX, "%s (/%i): 0x%s", reg->name, (int)(reg->size), value);
1968 /* set register value */
1971 uint8_t *buf = malloc(DIV_ROUND_UP(reg->size, 8));
1972 str_to_buf(CMD_ARGV[1], strlen(CMD_ARGV[1]), buf, reg->size, 0);
1974 reg->type->set(reg, buf);
1976 value = buf_to_str(reg->value, reg->size, 16);
1977 command_print(CMD_CTX, "%s (/%i): 0x%s", reg->name, (int)(reg->size), value);
1985 command_print(CMD_CTX, "usage: reg <#|name> [value]");
1990 COMMAND_HANDLER(handle_poll_command)
1992 int retval = ERROR_OK;
1993 struct target *target = get_current_target(CMD_CTX);
1997 command_print(CMD_CTX, "background polling: %s",
1998 jtag_poll_get_enabled() ? "on" : "off");
1999 command_print(CMD_CTX, "TAP: %s (%s)",
2000 target->tap->dotted_name,
2001 target->tap->enabled ? "enabled" : "disabled");
2002 if (!target->tap->enabled)
2004 if ((retval = target_poll(target)) != ERROR_OK)
2006 if ((retval = target_arch_state(target)) != ERROR_OK)
2009 else if (CMD_ARGC == 1)
2012 COMMAND_PARSE_ON_OFF(CMD_ARGV[0], enable);
2013 jtag_poll_set_enabled(enable);
2017 return ERROR_COMMAND_SYNTAX_ERROR;
2023 COMMAND_HANDLER(handle_wait_halt_command)
2026 return ERROR_COMMAND_SYNTAX_ERROR;
2031 int retval = parse_uint(CMD_ARGV[0], &ms);
2032 if (ERROR_OK != retval)
2034 command_print(CMD_CTX, "usage: %s [seconds]", CMD_NAME);
2035 return ERROR_COMMAND_SYNTAX_ERROR;
2037 // convert seconds (given) to milliseconds (needed)
2041 struct target *target = get_current_target(CMD_CTX);
2042 return target_wait_state(target, TARGET_HALTED, ms);
2045 /* wait for target state to change. The trick here is to have a low
2046 * latency for short waits and not to suck up all the CPU time
2049 * After 500ms, keep_alive() is invoked
2051 int target_wait_state(struct target *target, enum target_state state, int ms)
2054 long long then = 0, cur;
2059 if ((retval = target_poll(target)) != ERROR_OK)
2061 if (target->state == state)
2069 then = timeval_ms();
2070 LOG_DEBUG("waiting for target %s...",
2071 Jim_Nvp_value2name_simple(nvp_target_state,state)->name);
2079 if ((cur-then) > ms)
2081 LOG_ERROR("timed out while waiting for target %s",
2082 Jim_Nvp_value2name_simple(nvp_target_state,state)->name);
2090 COMMAND_HANDLER(handle_halt_command)
2094 struct target *target = get_current_target(CMD_CTX);
2095 int retval = target_halt(target);
2096 if (ERROR_OK != retval)
2102 retval = parse_uint(CMD_ARGV[0], &wait);
2103 if (ERROR_OK != retval)
2104 return ERROR_COMMAND_SYNTAX_ERROR;
2109 return CALL_COMMAND_HANDLER(handle_wait_halt_command);
2112 COMMAND_HANDLER(handle_soft_reset_halt_command)
2114 struct target *target = get_current_target(CMD_CTX);
2116 LOG_USER("requesting target halt and executing a soft reset");
2118 target->type->soft_reset_halt(target);
2123 COMMAND_HANDLER(handle_reset_command)
2126 return ERROR_COMMAND_SYNTAX_ERROR;
2128 enum target_reset_mode reset_mode = RESET_RUN;
2132 n = Jim_Nvp_name2value_simple(nvp_reset_modes, CMD_ARGV[0]);
2133 if ((n->name == NULL) || (n->value == RESET_UNKNOWN)) {
2134 return ERROR_COMMAND_SYNTAX_ERROR;
2136 reset_mode = n->value;
2139 /* reset *all* targets */
2140 return target_process_reset(CMD_CTX, reset_mode);
2144 COMMAND_HANDLER(handle_resume_command)
2148 return ERROR_COMMAND_SYNTAX_ERROR;
2150 struct target *target = get_current_target(CMD_CTX);
2151 target_handle_event(target, TARGET_EVENT_OLD_pre_resume);
2153 /* with no CMD_ARGV, resume from current pc, addr = 0,
2154 * with one arguments, addr = CMD_ARGV[0],
2155 * handle breakpoints, not debugging */
2159 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], addr);
2163 return target_resume(target, current, addr, 1, 0);
2166 COMMAND_HANDLER(handle_step_command)
2169 return ERROR_COMMAND_SYNTAX_ERROR;
2173 /* with no CMD_ARGV, step from current pc, addr = 0,
2174 * with one argument addr = CMD_ARGV[0],
2175 * handle breakpoints, debugging */
2180 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], addr);
2184 struct target *target = get_current_target(CMD_CTX);
2186 return target->type->step(target, current_pc, addr, 1);
2189 static void handle_md_output(struct command_context *cmd_ctx,
2190 struct target *target, uint32_t address, unsigned size,
2191 unsigned count, const uint8_t *buffer)
2193 const unsigned line_bytecnt = 32;
2194 unsigned line_modulo = line_bytecnt / size;
2196 char output[line_bytecnt * 4 + 1];
2197 unsigned output_len = 0;
2199 const char *value_fmt;
2201 case 4: value_fmt = "%8.8x "; break;
2202 case 2: value_fmt = "%4.4x "; break;
2203 case 1: value_fmt = "%2.2x "; break;
2205 /* "can't happen", caller checked */
2206 LOG_ERROR("invalid memory read size: %u", size);
2210 for (unsigned i = 0; i < count; i++)
2212 if (i % line_modulo == 0)
2214 output_len += snprintf(output + output_len,
2215 sizeof(output) - output_len,
2217 (unsigned)(address + (i*size)));
2221 const uint8_t *value_ptr = buffer + i * size;
2223 case 4: value = target_buffer_get_u32(target, value_ptr); break;
2224 case 2: value = target_buffer_get_u16(target, value_ptr); break;
2225 case 1: value = *value_ptr;
2227 output_len += snprintf(output + output_len,
2228 sizeof(output) - output_len,
2231 if ((i % line_modulo == line_modulo - 1) || (i == count - 1))
2233 command_print(cmd_ctx, "%s", output);
2239 COMMAND_HANDLER(handle_md_command)
2242 return ERROR_COMMAND_SYNTAX_ERROR;
2245 switch (CMD_NAME[2]) {
2246 case 'w': size = 4; break;
2247 case 'h': size = 2; break;
2248 case 'b': size = 1; break;
2249 default: return ERROR_COMMAND_SYNTAX_ERROR;
2252 bool physical=strcmp(CMD_ARGV[0], "phys")==0;
2253 int (*fn)(struct target *target,
2254 uint32_t address, uint32_t size, uint32_t count, uint8_t *buffer);
2259 fn=target_read_phys_memory;
2262 fn=target_read_memory;
2264 if ((CMD_ARGC < 1) || (CMD_ARGC > 2))
2266 return ERROR_COMMAND_SYNTAX_ERROR;
2270 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], address);
2274 COMMAND_PARSE_NUMBER(uint, CMD_ARGV[1], count);
2276 uint8_t *buffer = calloc(count, size);
2278 struct target *target = get_current_target(CMD_CTX);
2279 int retval = fn(target, address, size, count, buffer);
2280 if (ERROR_OK == retval)
2281 handle_md_output(CMD_CTX, target, address, size, count, buffer);
2288 COMMAND_HANDLER(handle_mw_command)
2292 return ERROR_COMMAND_SYNTAX_ERROR;
2294 bool physical=strcmp(CMD_ARGV[0], "phys")==0;
2295 int (*fn)(struct target *target,
2296 uint32_t address, uint32_t size, uint32_t count, uint8_t *buffer);
2301 fn=target_write_phys_memory;
2304 fn=target_write_memory;
2306 if ((CMD_ARGC < 2) || (CMD_ARGC > 3))
2307 return ERROR_COMMAND_SYNTAX_ERROR;
2310 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], address);
2313 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], value);
2317 COMMAND_PARSE_NUMBER(uint, CMD_ARGV[2], count);
2319 struct target *target = get_current_target(CMD_CTX);
2321 uint8_t value_buf[4];
2322 switch (CMD_NAME[2])
2326 target_buffer_set_u32(target, value_buf, value);
2330 target_buffer_set_u16(target, value_buf, value);
2334 value_buf[0] = value;
2337 return ERROR_COMMAND_SYNTAX_ERROR;
2339 for (unsigned i = 0; i < count; i++)
2341 int retval = fn(target,
2342 address + i * wordsize, wordsize, 1, value_buf);
2343 if (ERROR_OK != retval)
2352 static COMMAND_HELPER(parse_load_image_command_CMD_ARGV, struct image *image,
2353 uint32_t *min_address, uint32_t *max_address)
2355 if (CMD_ARGC < 1 || CMD_ARGC > 5)
2356 return ERROR_COMMAND_SYNTAX_ERROR;
2358 /* a base address isn't always necessary,
2359 * default to 0x0 (i.e. don't relocate) */
2363 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], addr);
2364 image->base_address = addr;
2365 image->base_address_set = 1;
2368 image->base_address_set = 0;
2370 image->start_address_set = 0;
2374 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[3], *min_address);
2378 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[4], *max_address);
2379 // use size (given) to find max (required)
2380 *max_address += *min_address;
2383 if (*min_address > *max_address)
2384 return ERROR_COMMAND_SYNTAX_ERROR;
2389 COMMAND_HANDLER(handle_load_image_command)
2393 uint32_t image_size;
2394 uint32_t min_address = 0;
2395 uint32_t max_address = 0xffffffff;
2399 int retval = CALL_COMMAND_HANDLER(parse_load_image_command_CMD_ARGV,
2400 &image, &min_address, &max_address);
2401 if (ERROR_OK != retval)
2404 struct target *target = get_current_target(CMD_CTX);
2406 struct duration bench;
2407 duration_start(&bench);
2409 if (image_open(&image, CMD_ARGV[0], (CMD_ARGC >= 3) ? CMD_ARGV[2] : NULL) != ERROR_OK)
2416 for (i = 0; i < image.num_sections; i++)
2418 buffer = malloc(image.sections[i].size);
2421 command_print(CMD_CTX,
2422 "error allocating buffer for section (%d bytes)",
2423 (int)(image.sections[i].size));
2427 if ((retval = image_read_section(&image, i, 0x0, image.sections[i].size, buffer, &buf_cnt)) != ERROR_OK)
2433 uint32_t offset = 0;
2434 uint32_t length = buf_cnt;
2436 /* DANGER!!! beware of unsigned comparision here!!! */
2438 if ((image.sections[i].base_address + buf_cnt >= min_address)&&
2439 (image.sections[i].base_address < max_address))
2441 if (image.sections[i].base_address < min_address)
2443 /* clip addresses below */
2444 offset += min_address-image.sections[i].base_address;
2448 if (image.sections[i].base_address + buf_cnt > max_address)
2450 length -= (image.sections[i].base_address + buf_cnt)-max_address;
2453 if ((retval = target_write_buffer(target, image.sections[i].base_address + offset, length, buffer + offset)) != ERROR_OK)
2458 image_size += length;
2459 command_print(CMD_CTX, "%u bytes written at address 0x%8.8" PRIx32 "",
2460 (unsigned int)length,
2461 image.sections[i].base_address + offset);
2467 if ((ERROR_OK == retval) && (duration_measure(&bench) == ERROR_OK))
2469 command_print(CMD_CTX, "downloaded %" PRIu32 " bytes "
2470 "in %fs (%0.3f kb/s)", image_size,
2471 duration_elapsed(&bench), duration_kbps(&bench, image_size));
2474 image_close(&image);
2480 COMMAND_HANDLER(handle_dump_image_command)
2482 struct fileio fileio;
2484 uint8_t buffer[560];
2488 struct target *target = get_current_target(CMD_CTX);
2492 command_print(CMD_CTX, "usage: dump_image <filename> <address> <size>");
2497 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], address);
2499 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[2], size);
2501 if (fileio_open(&fileio, CMD_ARGV[0], FILEIO_WRITE, FILEIO_BINARY) != ERROR_OK)
2506 struct duration bench;
2507 duration_start(&bench);
2509 int retval = ERROR_OK;
2512 size_t size_written;
2513 uint32_t this_run_size = (size > 560) ? 560 : size;
2514 retval = target_read_buffer(target, address, this_run_size, buffer);
2515 if (retval != ERROR_OK)
2520 retval = fileio_write(&fileio, this_run_size, buffer, &size_written);
2521 if (retval != ERROR_OK)
2526 size -= this_run_size;
2527 address += this_run_size;
2530 if ((retvaltemp = fileio_close(&fileio)) != ERROR_OK)
2533 if ((ERROR_OK == retval) && (duration_measure(&bench) == ERROR_OK))
2535 command_print(CMD_CTX,
2536 "dumped %ld bytes in %fs (%0.3f kb/s)", (long)fileio.size,
2537 duration_elapsed(&bench), duration_kbps(&bench, fileio.size));
2543 static COMMAND_HELPER(handle_verify_image_command_internal, int verify)
2547 uint32_t image_size;
2550 uint32_t checksum = 0;
2551 uint32_t mem_checksum = 0;
2555 struct target *target = get_current_target(CMD_CTX);
2559 return ERROR_COMMAND_SYNTAX_ERROR;
2564 LOG_ERROR("no target selected");
2568 struct duration bench;
2569 duration_start(&bench);
2574 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], addr);
2575 image.base_address = addr;
2576 image.base_address_set = 1;
2580 image.base_address_set = 0;
2581 image.base_address = 0x0;
2584 image.start_address_set = 0;
2586 if ((retval = image_open(&image, CMD_ARGV[0], (CMD_ARGC == 3) ? CMD_ARGV[2] : NULL)) != ERROR_OK)
2593 for (i = 0; i < image.num_sections; i++)
2595 buffer = malloc(image.sections[i].size);
2598 command_print(CMD_CTX,
2599 "error allocating buffer for section (%d bytes)",
2600 (int)(image.sections[i].size));
2603 if ((retval = image_read_section(&image, i, 0x0, image.sections[i].size, buffer, &buf_cnt)) != ERROR_OK)
2611 /* calculate checksum of image */
2612 image_calculate_checksum(buffer, buf_cnt, &checksum);
2614 retval = target_checksum_memory(target, image.sections[i].base_address, buf_cnt, &mem_checksum);
2615 if (retval != ERROR_OK)
2621 if (checksum != mem_checksum)
2623 /* failed crc checksum, fall back to a binary compare */
2626 command_print(CMD_CTX, "checksum mismatch - attempting binary compare");
2628 data = (uint8_t*)malloc(buf_cnt);
2630 /* Can we use 32bit word accesses? */
2632 int count = buf_cnt;
2633 if ((count % 4) == 0)
2638 retval = target_read_memory(target, image.sections[i].base_address, size, count, data);
2639 if (retval == ERROR_OK)
2642 for (t = 0; t < buf_cnt; t++)
2644 if (data[t] != buffer[t])
2646 command_print(CMD_CTX,
2647 "Verify operation failed address 0x%08x. Was 0x%02x instead of 0x%02x\n",
2648 (unsigned)(t + image.sections[i].base_address),
2653 retval = ERROR_FAIL;
2667 command_print(CMD_CTX, "address 0x%08" PRIx32 " length 0x%08zx",
2668 image.sections[i].base_address,
2673 image_size += buf_cnt;
2676 if ((ERROR_OK == retval) && (duration_measure(&bench) == ERROR_OK))
2678 command_print(CMD_CTX, "verified %" PRIu32 " bytes "
2679 "in %fs (%0.3f kb/s)", image_size,
2680 duration_elapsed(&bench), duration_kbps(&bench, image_size));
2683 image_close(&image);
2688 COMMAND_HANDLER(handle_verify_image_command)
2690 return CALL_COMMAND_HANDLER(handle_verify_image_command_internal, 1);
2693 COMMAND_HANDLER(handle_test_image_command)
2695 return CALL_COMMAND_HANDLER(handle_verify_image_command_internal, 0);
2698 static int handle_bp_command_list(struct command_context *cmd_ctx)
2700 struct target *target = get_current_target(cmd_ctx);
2701 struct breakpoint *breakpoint = target->breakpoints;
2704 if (breakpoint->type == BKPT_SOFT)
2706 char* buf = buf_to_str(breakpoint->orig_instr,
2707 breakpoint->length, 16);
2708 command_print(cmd_ctx, "0x%8.8" PRIx32 ", 0x%x, %i, 0x%s",
2709 breakpoint->address,
2711 breakpoint->set, buf);
2716 command_print(cmd_ctx, "0x%8.8" PRIx32 ", 0x%x, %i",
2717 breakpoint->address,
2718 breakpoint->length, breakpoint->set);
2721 breakpoint = breakpoint->next;
2726 static int handle_bp_command_set(struct command_context *cmd_ctx,
2727 uint32_t addr, uint32_t length, int hw)
2729 struct target *target = get_current_target(cmd_ctx);
2730 int retval = breakpoint_add(target, addr, length, hw);
2731 if (ERROR_OK == retval)
2732 command_print(cmd_ctx, "breakpoint set at 0x%8.8" PRIx32 "", addr);
2734 LOG_ERROR("Failure setting breakpoint");
2738 COMMAND_HANDLER(handle_bp_command)
2741 return handle_bp_command_list(CMD_CTX);
2743 if (CMD_ARGC < 2 || CMD_ARGC > 3)
2745 command_print(CMD_CTX, "usage: bp <address> <length> ['hw']");
2746 return ERROR_COMMAND_SYNTAX_ERROR;
2750 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], addr);
2752 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], length);
2757 if (strcmp(CMD_ARGV[2], "hw") == 0)
2760 return ERROR_COMMAND_SYNTAX_ERROR;
2763 return handle_bp_command_set(CMD_CTX, addr, length, hw);
2766 COMMAND_HANDLER(handle_rbp_command)
2769 return ERROR_COMMAND_SYNTAX_ERROR;
2772 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], addr);
2774 struct target *target = get_current_target(CMD_CTX);
2775 breakpoint_remove(target, addr);
2780 COMMAND_HANDLER(handle_wp_command)
2782 struct target *target = get_current_target(CMD_CTX);
2786 struct watchpoint *watchpoint = target->watchpoints;
2790 command_print(CMD_CTX, "address: 0x%8.8" PRIx32
2791 ", len: 0x%8.8" PRIx32
2792 ", r/w/a: %i, value: 0x%8.8" PRIx32
2793 ", mask: 0x%8.8" PRIx32,
2794 watchpoint->address,
2796 (int)watchpoint->rw,
2799 watchpoint = watchpoint->next;
2804 enum watchpoint_rw type = WPT_ACCESS;
2806 uint32_t length = 0;
2807 uint32_t data_value = 0x0;
2808 uint32_t data_mask = 0xffffffff;
2813 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[4], data_mask);
2816 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[3], data_value);
2819 switch (CMD_ARGV[2][0])
2831 LOG_ERROR("invalid watchpoint mode ('%c')", CMD_ARGV[2][0]);
2832 return ERROR_COMMAND_SYNTAX_ERROR;
2836 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], length);
2837 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], addr);
2841 command_print(CMD_CTX, "usage: wp [address length "
2842 "[(r|w|a) [value [mask]]]]");
2843 return ERROR_COMMAND_SYNTAX_ERROR;
2846 int retval = watchpoint_add(target, addr, length, type,
2847 data_value, data_mask);
2848 if (ERROR_OK != retval)
2849 LOG_ERROR("Failure setting watchpoints");
2854 COMMAND_HANDLER(handle_rwp_command)
2857 return ERROR_COMMAND_SYNTAX_ERROR;
2860 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], addr);
2862 struct target *target = get_current_target(CMD_CTX);
2863 watchpoint_remove(target, addr);
2870 * Translate a virtual address to a physical address.
2872 * The low-level target implementation must have logged a detailed error
2873 * which is forwarded to telnet/GDB session.
2875 COMMAND_HANDLER(handle_virt2phys_command)
2878 return ERROR_COMMAND_SYNTAX_ERROR;
2881 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], va);
2884 struct target *target = get_current_target(CMD_CTX);
2885 int retval = target->type->virt2phys(target, va, &pa);
2886 if (retval == ERROR_OK)
2887 command_print(CMD_CTX, "Physical address 0x%08" PRIx32 "", pa);
2892 static void writeData(FILE *f, const void *data, size_t len)
2894 size_t written = fwrite(data, 1, len, f);
2896 LOG_ERROR("failed to write %zu bytes: %s", len, strerror(errno));
2899 static void writeLong(FILE *f, int l)
2902 for (i = 0; i < 4; i++)
2904 char c = (l >> (i*8))&0xff;
2905 writeData(f, &c, 1);
2910 static void writeString(FILE *f, char *s)
2912 writeData(f, s, strlen(s));
2915 /* Dump a gmon.out histogram file. */
2916 static void writeGmon(uint32_t *samples, uint32_t sampleNum, const char *filename)
2919 FILE *f = fopen(filename, "w");
2922 writeString(f, "gmon");
2923 writeLong(f, 0x00000001); /* Version */
2924 writeLong(f, 0); /* padding */
2925 writeLong(f, 0); /* padding */
2926 writeLong(f, 0); /* padding */
2928 uint8_t zero = 0; /* GMON_TAG_TIME_HIST */
2929 writeData(f, &zero, 1);
2931 /* figure out bucket size */
2932 uint32_t min = samples[0];
2933 uint32_t max = samples[0];
2934 for (i = 0; i < sampleNum; i++)
2936 if (min > samples[i])
2940 if (max < samples[i])
2946 int addressSpace = (max-min + 1);
2948 static const uint32_t maxBuckets = 256 * 1024; /* maximum buckets. */
2949 uint32_t length = addressSpace;
2950 if (length > maxBuckets)
2952 length = maxBuckets;
2954 int *buckets = malloc(sizeof(int)*length);
2955 if (buckets == NULL)
2960 memset(buckets, 0, sizeof(int)*length);
2961 for (i = 0; i < sampleNum;i++)
2963 uint32_t address = samples[i];
2964 long long a = address-min;
2965 long long b = length-1;
2966 long long c = addressSpace-1;
2967 int index = (a*b)/c; /* danger!!!! int32 overflows */
2971 /* append binary memory gmon.out &profile_hist_hdr ((char*)&profile_hist_hdr + sizeof(struct gmon_hist_hdr)) */
2972 writeLong(f, min); /* low_pc */
2973 writeLong(f, max); /* high_pc */
2974 writeLong(f, length); /* # of samples */
2975 writeLong(f, 64000000); /* 64MHz */
2976 writeString(f, "seconds");
2977 for (i = 0; i < (15-strlen("seconds")); i++)
2978 writeData(f, &zero, 1);
2979 writeString(f, "s");
2981 /*append binary memory gmon.out profile_hist_data (profile_hist_data + profile_hist_hdr.hist_size) */
2983 char *data = malloc(2*length);
2986 for (i = 0; i < length;i++)
2995 data[i*2 + 1]=(val >> 8)&0xff;
2998 writeData(f, data, length * 2);
3008 /* profiling samples the CPU PC as quickly as OpenOCD is able,
3009 * which will be used as a random sampling of PC */
3010 COMMAND_HANDLER(handle_profile_command)
3012 struct target *target = get_current_target(CMD_CTX);
3013 struct timeval timeout, now;
3015 gettimeofday(&timeout, NULL);
3018 return ERROR_COMMAND_SYNTAX_ERROR;
3021 COMMAND_PARSE_NUMBER(uint, CMD_ARGV[0], offset);
3023 timeval_add_time(&timeout, offset, 0);
3026 * @todo: Some cores let us sample the PC without the
3027 * annoying halt/resume step; for example, ARMv7 PCSR.
3028 * Provide a way to use that more efficient mechanism.
3031 command_print(CMD_CTX, "Starting profiling. Halting and resuming the target as often as we can...");
3033 static const int maxSample = 10000;
3034 uint32_t *samples = malloc(sizeof(uint32_t)*maxSample);
3035 if (samples == NULL)
3039 /* hopefully it is safe to cache! We want to stop/restart as quickly as possible. */
3040 struct reg *reg = register_get_by_name(target->reg_cache, "pc", 1);
3045 target_poll(target);
3046 if (target->state == TARGET_HALTED)
3048 uint32_t t=*((uint32_t *)reg->value);
3049 samples[numSamples++]=t;
3050 retval = target_resume(target, 1, 0, 0, 0); /* current pc, addr = 0, do not handle breakpoints, not debugging */
3051 target_poll(target);
3052 alive_sleep(10); /* sleep 10ms, i.e. <100 samples/second. */
3053 } else if (target->state == TARGET_RUNNING)
3055 /* We want to quickly sample the PC. */
3056 if ((retval = target_halt(target)) != ERROR_OK)
3063 command_print(CMD_CTX, "Target not halted or running");
3067 if (retval != ERROR_OK)
3072 gettimeofday(&now, NULL);
3073 if ((numSamples >= maxSample) || ((now.tv_sec >= timeout.tv_sec) && (now.tv_usec >= timeout.tv_usec)))
3075 command_print(CMD_CTX, "Profiling completed. %d samples.", numSamples);
3076 if ((retval = target_poll(target)) != ERROR_OK)
3081 if (target->state == TARGET_HALTED)
3083 target_resume(target, 1, 0, 0, 0); /* current pc, addr = 0, do not handle breakpoints, not debugging */
3085 if ((retval = target_poll(target)) != ERROR_OK)
3090 writeGmon(samples, numSamples, CMD_ARGV[1]);
3091 command_print(CMD_CTX, "Wrote %s", CMD_ARGV[1]);
3100 static int new_int_array_element(Jim_Interp * interp, const char *varname, int idx, uint32_t val)
3103 Jim_Obj *nameObjPtr, *valObjPtr;
3106 namebuf = alloc_printf("%s(%d)", varname, idx);
3110 nameObjPtr = Jim_NewStringObj(interp, namebuf, -1);
3111 valObjPtr = Jim_NewIntObj(interp, val);
3112 if (!nameObjPtr || !valObjPtr)
3118 Jim_IncrRefCount(nameObjPtr);
3119 Jim_IncrRefCount(valObjPtr);
3120 result = Jim_SetVariable(interp, nameObjPtr, valObjPtr);
3121 Jim_DecrRefCount(interp, nameObjPtr);
3122 Jim_DecrRefCount(interp, valObjPtr);
3124 /* printf("%s(%d) <= 0%08x\n", varname, idx, val); */
3128 static int jim_mem2array(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
3130 struct command_context *context;
3131 struct target *target;
3133 context = Jim_GetAssocData(interp, "context");
3134 if (context == NULL)
3136 LOG_ERROR("mem2array: no command context");
3139 target = get_current_target(context);
3142 LOG_ERROR("mem2array: no current target");
3146 return target_mem2array(interp, target, argc-1, argv + 1);
3149 static int target_mem2array(Jim_Interp *interp, struct target *target, int argc, Jim_Obj *const *argv)
3157 const char *varname;
3161 /* argv[1] = name of array to receive the data
3162 * argv[2] = desired width
3163 * argv[3] = memory address
3164 * argv[4] = count of times to read
3167 Jim_WrongNumArgs(interp, 1, argv, "varname width addr nelems");
3170 varname = Jim_GetString(argv[0], &len);
3171 /* given "foo" get space for worse case "foo(%d)" .. add 20 */
3173 e = Jim_GetLong(interp, argv[1], &l);
3179 e = Jim_GetLong(interp, argv[2], &l);
3184 e = Jim_GetLong(interp, argv[3], &l);
3200 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3201 Jim_AppendStrings(interp, Jim_GetResult(interp), "Invalid width param, must be 8/16/32", NULL);
3205 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3206 Jim_AppendStrings(interp, Jim_GetResult(interp), "mem2array: zero width read?", NULL);
3209 if ((addr + (len * width)) < addr) {
3210 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3211 Jim_AppendStrings(interp, Jim_GetResult(interp), "mem2array: addr + len - wraps to zero?", NULL);
3214 /* absurd transfer size? */
3216 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3217 Jim_AppendStrings(interp, Jim_GetResult(interp), "mem2array: absurd > 64K item request", NULL);
3222 ((width == 2) && ((addr & 1) == 0)) ||
3223 ((width == 4) && ((addr & 3) == 0))) {
3227 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3228 sprintf(buf, "mem2array address: 0x%08" PRIx32 " is not aligned for %" PRId32 " byte reads",
3231 Jim_AppendStrings(interp, Jim_GetResult(interp), buf , NULL);
3240 size_t buffersize = 4096;
3241 uint8_t *buffer = malloc(buffersize);
3248 /* Slurp... in buffer size chunks */
3250 count = len; /* in objects.. */
3251 if (count > (buffersize/width)) {
3252 count = (buffersize/width);
3255 retval = target_read_memory(target, addr, width, count, buffer);
3256 if (retval != ERROR_OK) {
3258 LOG_ERROR("mem2array: Read @ 0x%08x, w=%d, cnt=%d, failed",
3262 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3263 Jim_AppendStrings(interp, Jim_GetResult(interp), "mem2array: cannot read memory", NULL);
3267 v = 0; /* shut up gcc */
3268 for (i = 0 ;i < count ;i++, n++) {
3271 v = target_buffer_get_u32(target, &buffer[i*width]);
3274 v = target_buffer_get_u16(target, &buffer[i*width]);
3277 v = buffer[i] & 0x0ff;
3280 new_int_array_element(interp, varname, n, v);
3288 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3293 static int get_int_array_element(Jim_Interp * interp, const char *varname, int idx, uint32_t *val)
3296 Jim_Obj *nameObjPtr, *valObjPtr;
3300 namebuf = alloc_printf("%s(%d)", varname, idx);
3304 nameObjPtr = Jim_NewStringObj(interp, namebuf, -1);
3311 Jim_IncrRefCount(nameObjPtr);
3312 valObjPtr = Jim_GetVariable(interp, nameObjPtr, JIM_ERRMSG);
3313 Jim_DecrRefCount(interp, nameObjPtr);
3315 if (valObjPtr == NULL)
3318 result = Jim_GetLong(interp, valObjPtr, &l);
3319 /* printf("%s(%d) => 0%08x\n", varname, idx, val); */
3324 static int jim_array2mem(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
3326 struct command_context *context;
3327 struct target *target;
3329 context = Jim_GetAssocData(interp, "context");
3330 if (context == NULL) {
3331 LOG_ERROR("array2mem: no command context");
3334 target = get_current_target(context);
3335 if (target == NULL) {
3336 LOG_ERROR("array2mem: no current target");
3340 return target_array2mem(interp,target, argc-1, argv + 1);
3343 static int target_array2mem(Jim_Interp *interp, struct target *target,
3344 int argc, Jim_Obj *const *argv)
3352 const char *varname;
3356 /* argv[1] = name of array to get the data
3357 * argv[2] = desired width
3358 * argv[3] = memory address
3359 * argv[4] = count to write
3362 Jim_WrongNumArgs(interp, 0, argv, "varname width addr nelems");
3365 varname = Jim_GetString(argv[0], &len);
3366 /* given "foo" get space for worse case "foo(%d)" .. add 20 */
3368 e = Jim_GetLong(interp, argv[1], &l);
3374 e = Jim_GetLong(interp, argv[2], &l);
3379 e = Jim_GetLong(interp, argv[3], &l);
3395 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3396 Jim_AppendStrings(interp, Jim_GetResult(interp), "Invalid width param, must be 8/16/32", NULL);
3400 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3401 Jim_AppendStrings(interp, Jim_GetResult(interp), "array2mem: zero width read?", NULL);
3404 if ((addr + (len * width)) < addr) {
3405 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3406 Jim_AppendStrings(interp, Jim_GetResult(interp), "array2mem: addr + len - wraps to zero?", NULL);
3409 /* absurd transfer size? */
3411 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3412 Jim_AppendStrings(interp, Jim_GetResult(interp), "array2mem: absurd > 64K item request", NULL);
3417 ((width == 2) && ((addr & 1) == 0)) ||
3418 ((width == 4) && ((addr & 3) == 0))) {
3422 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3423 sprintf(buf, "array2mem address: 0x%08x is not aligned for %d byte reads",
3426 Jim_AppendStrings(interp, Jim_GetResult(interp), buf , NULL);
3437 size_t buffersize = 4096;
3438 uint8_t *buffer = malloc(buffersize);
3443 /* Slurp... in buffer size chunks */
3445 count = len; /* in objects.. */
3446 if (count > (buffersize/width)) {
3447 count = (buffersize/width);
3450 v = 0; /* shut up gcc */
3451 for (i = 0 ;i < count ;i++, n++) {
3452 get_int_array_element(interp, varname, n, &v);
3455 target_buffer_set_u32(target, &buffer[i*width], v);
3458 target_buffer_set_u16(target, &buffer[i*width], v);
3461 buffer[i] = v & 0x0ff;
3467 retval = target_write_memory(target, addr, width, count, buffer);
3468 if (retval != ERROR_OK) {
3470 LOG_ERROR("array2mem: Write @ 0x%08x, w=%d, cnt=%d, failed",
3474 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3475 Jim_AppendStrings(interp, Jim_GetResult(interp), "array2mem: cannot read memory", NULL);
3483 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3488 void target_all_handle_event(enum target_event e)
3490 struct target *target;
3492 LOG_DEBUG("**all*targets: event: %d, %s",
3494 Jim_Nvp_value2name_simple(nvp_target_event, e)->name);
3496 target = all_targets;
3498 target_handle_event(target, e);
3499 target = target->next;
3504 /* FIX? should we propagate errors here rather than printing them
3507 void target_handle_event(struct target *target, enum target_event e)
3509 struct target_event_action *teap;
3511 for (teap = target->event_action; teap != NULL; teap = teap->next) {
3512 if (teap->event == e) {
3513 LOG_DEBUG("target: (%d) %s (%s) event: %d (%s) action: %s",
3514 target->target_number,
3515 target_name(target),
3516 target_type_name(target),
3518 Jim_Nvp_value2name_simple(nvp_target_event, e)->name,
3519 Jim_GetString(teap->body, NULL));
3520 if (Jim_EvalObj(teap->interp, teap->body) != JIM_OK)
3522 Jim_PrintErrorMessage(teap->interp);
3529 * Returns true only if the target has a handler for the specified event.
3531 bool target_has_event_action(struct target *target, enum target_event event)
3533 struct target_event_action *teap;
3535 for (teap = target->event_action; teap != NULL; teap = teap->next) {
3536 if (teap->event == event)
3542 enum target_cfg_param {
3545 TCFG_WORK_AREA_VIRT,
3546 TCFG_WORK_AREA_PHYS,
3547 TCFG_WORK_AREA_SIZE,
3548 TCFG_WORK_AREA_BACKUP,
3551 TCFG_CHAIN_POSITION,
3554 static Jim_Nvp nvp_config_opts[] = {
3555 { .name = "-type", .value = TCFG_TYPE },
3556 { .name = "-event", .value = TCFG_EVENT },
3557 { .name = "-work-area-virt", .value = TCFG_WORK_AREA_VIRT },
3558 { .name = "-work-area-phys", .value = TCFG_WORK_AREA_PHYS },
3559 { .name = "-work-area-size", .value = TCFG_WORK_AREA_SIZE },
3560 { .name = "-work-area-backup", .value = TCFG_WORK_AREA_BACKUP },
3561 { .name = "-endian" , .value = TCFG_ENDIAN },
3562 { .name = "-variant", .value = TCFG_VARIANT },
3563 { .name = "-chain-position", .value = TCFG_CHAIN_POSITION },
3565 { .name = NULL, .value = -1 }
3568 static int target_configure(Jim_GetOptInfo *goi, struct target *target)
3576 /* parse config or cget options ... */
3577 while (goi->argc > 0) {
3578 Jim_SetEmptyResult(goi->interp);
3579 /* Jim_GetOpt_Debug(goi); */
3581 if (target->type->target_jim_configure) {
3582 /* target defines a configure function */
3583 /* target gets first dibs on parameters */
3584 e = (*(target->type->target_jim_configure))(target, goi);
3593 /* otherwise we 'continue' below */
3595 e = Jim_GetOpt_Nvp(goi, nvp_config_opts, &n);
3597 Jim_GetOpt_NvpUnknown(goi, nvp_config_opts, 0);
3603 if (goi->isconfigure) {
3604 Jim_SetResult_sprintf(goi->interp,
3605 "not settable: %s", n->name);
3609 if (goi->argc != 0) {
3610 Jim_WrongNumArgs(goi->interp,
3611 goi->argc, goi->argv,
3616 Jim_SetResultString(goi->interp,
3617 target_type_name(target), -1);
3621 if (goi->argc == 0) {
3622 Jim_WrongNumArgs(goi->interp, goi->argc, goi->argv, "-event ?event-name? ...");
3626 e = Jim_GetOpt_Nvp(goi, nvp_target_event, &n);
3628 Jim_GetOpt_NvpUnknown(goi, nvp_target_event, 1);
3632 if (goi->isconfigure) {
3633 if (goi->argc != 1) {
3634 Jim_WrongNumArgs(goi->interp, goi->argc, goi->argv, "-event ?event-name? ?EVENT-BODY?");
3638 if (goi->argc != 0) {
3639 Jim_WrongNumArgs(goi->interp, goi->argc, goi->argv, "-event ?event-name?");
3645 struct target_event_action *teap;
3647 teap = target->event_action;
3648 /* replace existing? */
3650 if (teap->event == (enum target_event)n->value) {
3656 if (goi->isconfigure) {
3657 bool replace = true;
3660 teap = calloc(1, sizeof(*teap));
3663 teap->event = n->value;
3664 teap->interp = goi->interp;
3665 Jim_GetOpt_Obj(goi, &o);
3667 Jim_DecrRefCount(teap->interp, teap->body);
3669 teap->body = Jim_DuplicateObj(goi->interp, o);
3672 * Tcl/TK - "tk events" have a nice feature.
3673 * See the "BIND" command.
3674 * We should support that here.
3675 * You can specify %X and %Y in the event code.
3676 * The idea is: %T - target name.
3677 * The idea is: %N - target number
3678 * The idea is: %E - event name.
3680 Jim_IncrRefCount(teap->body);
3684 /* add to head of event list */
3685 teap->next = target->event_action;
3686 target->event_action = teap;
3688 Jim_SetEmptyResult(goi->interp);
3692 Jim_SetEmptyResult(goi->interp);
3694 Jim_SetResult(goi->interp, Jim_DuplicateObj(goi->interp, teap->body));
3701 case TCFG_WORK_AREA_VIRT:
3702 if (goi->isconfigure) {
3703 target_free_all_working_areas(target);
3704 e = Jim_GetOpt_Wide(goi, &w);
3708 target->working_area_virt = w;
3709 target->working_area_virt_spec = true;
3711 if (goi->argc != 0) {
3715 Jim_SetResult(goi->interp, Jim_NewIntObj(goi->interp, target->working_area_virt));
3719 case TCFG_WORK_AREA_PHYS:
3720 if (goi->isconfigure) {
3721 target_free_all_working_areas(target);
3722 e = Jim_GetOpt_Wide(goi, &w);
3726 target->working_area_phys = w;
3727 target->working_area_phys_spec = true;
3729 if (goi->argc != 0) {
3733 Jim_SetResult(goi->interp, Jim_NewIntObj(goi->interp, target->working_area_phys));
3737 case TCFG_WORK_AREA_SIZE:
3738 if (goi->isconfigure) {
3739 target_free_all_working_areas(target);
3740 e = Jim_GetOpt_Wide(goi, &w);
3744 target->working_area_size = w;
3746 if (goi->argc != 0) {
3750 Jim_SetResult(goi->interp, Jim_NewIntObj(goi->interp, target->working_area_size));
3754 case TCFG_WORK_AREA_BACKUP:
3755 if (goi->isconfigure) {
3756 target_free_all_working_areas(target);
3757 e = Jim_GetOpt_Wide(goi, &w);
3761 /* make this exactly 1 or 0 */
3762 target->backup_working_area = (!!w);
3764 if (goi->argc != 0) {
3768 Jim_SetResult(goi->interp, Jim_NewIntObj(goi->interp, target->backup_working_area));
3769 /* loop for more e*/
3773 if (goi->isconfigure) {
3774 e = Jim_GetOpt_Nvp(goi, nvp_target_endian, &n);
3776 Jim_GetOpt_NvpUnknown(goi, nvp_target_endian, 1);
3779 target->endianness = n->value;
3781 if (goi->argc != 0) {
3785 n = Jim_Nvp_value2name_simple(nvp_target_endian, target->endianness);
3786 if (n->name == NULL) {
3787 target->endianness = TARGET_LITTLE_ENDIAN;
3788 n = Jim_Nvp_value2name_simple(nvp_target_endian, target->endianness);
3790 Jim_SetResultString(goi->interp, n->name, -1);
3795 if (goi->isconfigure) {
3796 if (goi->argc < 1) {
3797 Jim_SetResult_sprintf(goi->interp,
3802 if (target->variant) {
3803 free((void *)(target->variant));
3805 e = Jim_GetOpt_String(goi, &cp, NULL);
3806 target->variant = strdup(cp);
3808 if (goi->argc != 0) {
3812 Jim_SetResultString(goi->interp, target->variant,-1);
3815 case TCFG_CHAIN_POSITION:
3816 if (goi->isconfigure) {
3818 struct jtag_tap *tap;
3819 target_free_all_working_areas(target);
3820 e = Jim_GetOpt_Obj(goi, &o);
3824 tap = jtag_tap_by_jim_obj(goi->interp, o);
3828 /* make this exactly 1 or 0 */
3831 if (goi->argc != 0) {
3835 Jim_SetResultString(goi->interp, target->tap->dotted_name, -1);
3836 /* loop for more e*/
3839 } /* while (goi->argc) */
3842 /* done - we return */
3847 jim_target_configure(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
3851 Jim_GetOpt_Setup(&goi, interp, argc - 1, argv + 1);
3852 goi.isconfigure = !strcmp(Jim_GetString(argv[0], NULL), "configure");
3853 int need_args = 1 + goi.isconfigure;
3854 if (goi.argc < need_args)
3856 Jim_WrongNumArgs(goi.interp, goi.argc, goi.argv,
3858 ? "missing: -option VALUE ..."
3859 : "missing: -option ...");
3862 struct target *target = Jim_CmdPrivData(goi.interp);
3863 return target_configure(&goi, target);
3866 static int jim_target_mw(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
3868 const char *cmd_name = Jim_GetString(argv[0], NULL);
3871 Jim_GetOpt_Setup(&goi, interp, argc - 1, argv + 1);
3873 if (goi.argc != 2 && goi.argc != 3)
3875 Jim_SetResult_sprintf(goi.interp,
3876 "usage: %s <address> <data> [<count>]", cmd_name);
3881 int e = Jim_GetOpt_Wide(&goi, &a);
3886 e = Jim_GetOpt_Wide(&goi, &b);
3893 e = Jim_GetOpt_Wide(&goi, &c);
3898 struct target *target = Jim_CmdPrivData(goi.interp);
3899 uint8_t target_buf[32];
3900 if (strcasecmp(cmd_name, "mww") == 0) {
3901 target_buffer_set_u32(target, target_buf, b);
3904 else if (strcasecmp(cmd_name, "mwh") == 0) {
3905 target_buffer_set_u16(target, target_buf, b);
3908 else if (strcasecmp(cmd_name, "mwb") == 0) {
3909 target_buffer_set_u8(target, target_buf, b);
3912 LOG_ERROR("command '%s' unknown: ", cmd_name);
3916 for (jim_wide x = 0; x < c; x++)
3918 e = target_write_memory(target, a, b, 1, target_buf);
3921 Jim_SetResult_sprintf(interp,
3922 "Error writing @ 0x%08x: %d\n", (int)(a), e);
3931 static int jim_target_md(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
3933 const char *cmd_name = Jim_GetString(argv[0], NULL);
3936 Jim_GetOpt_Setup(&goi, interp, argc - 1, argv + 1);
3938 if ((goi.argc == 2) || (goi.argc == 3))
3940 Jim_SetResult_sprintf(goi.interp,
3941 "usage: %s <address> [<count>]", cmd_name);
3946 int e = Jim_GetOpt_Wide(&goi, &a);
3952 e = Jim_GetOpt_Wide(&goi, &c);
3959 jim_wide b = 1; /* shut up gcc */
3960 if (strcasecmp(cmd_name, "mdw") == 0)
3962 else if (strcasecmp(cmd_name, "mdh") == 0)
3964 else if (strcasecmp(cmd_name, "mdb") == 0)
3967 LOG_ERROR("command '%s' unknown: ", cmd_name);
3971 /* convert count to "bytes" */
3974 struct target *target = Jim_CmdPrivData(goi.interp);
3975 uint8_t target_buf[32];
3982 e = target_read_memory(target, a, b, y / b, target_buf);
3983 if (e != ERROR_OK) {
3984 Jim_SetResult_sprintf(interp, "error reading target @ 0x%08lx", (int)(a));
3988 Jim_fprintf(interp, interp->cookie_stdout, "0x%08x ", (int)(a));
3991 for (x = 0; x < 16 && x < y; x += 4)
3993 z = target_buffer_get_u32(target, &(target_buf[ x * 4 ]));
3994 Jim_fprintf(interp, interp->cookie_stdout, "%08x ", (int)(z));
3996 for (; (x < 16) ; x += 4) {
3997 Jim_fprintf(interp, interp->cookie_stdout, " ");
4001 for (x = 0; x < 16 && x < y; x += 2)
4003 z = target_buffer_get_u16(target, &(target_buf[ x * 2 ]));
4004 Jim_fprintf(interp, interp->cookie_stdout, "%04x ", (int)(z));
4006 for (; (x < 16) ; x += 2) {
4007 Jim_fprintf(interp, interp->cookie_stdout, " ");
4012 for (x = 0 ; (x < 16) && (x < y) ; x += 1) {
4013 z = target_buffer_get_u8(target, &(target_buf[ x * 4 ]));
4014 Jim_fprintf(interp, interp->cookie_stdout, "%02x ", (int)(z));
4016 for (; (x < 16) ; x += 1) {
4017 Jim_fprintf(interp, interp->cookie_stdout, " ");
4021 /* ascii-ify the bytes */
4022 for (x = 0 ; x < y ; x++) {
4023 if ((target_buf[x] >= 0x20) &&
4024 (target_buf[x] <= 0x7e)) {
4028 target_buf[x] = '.';
4033 target_buf[x] = ' ';
4038 /* print - with a newline */
4039 Jim_fprintf(interp, interp->cookie_stdout, "%s\n", target_buf);
4047 static int jim_target_mem2array(Jim_Interp *interp,
4048 int argc, Jim_Obj *const *argv)
4050 struct target *target = Jim_CmdPrivData(interp);
4051 return target_mem2array(interp, target, argc - 1, argv + 1);
4054 static int jim_target_array2mem(Jim_Interp *interp,
4055 int argc, Jim_Obj *const *argv)
4057 struct target *target = Jim_CmdPrivData(interp);
4058 return target_array2mem(interp, target, argc - 1, argv + 1);
4061 static int jim_target_tap_disabled(Jim_Interp *interp)
4063 Jim_SetResult_sprintf(interp, "[TAP is disabled]");
4067 static int jim_target_examine(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
4071 Jim_WrongNumArgs(interp, 1, argv, "[no parameters]");
4074 struct target *target = Jim_CmdPrivData(interp);
4075 if (!target->tap->enabled)
4076 return jim_target_tap_disabled(interp);
4078 int e = target->type->examine(target);
4081 Jim_SetResult_sprintf(interp, "examine-fails: %d", e);
4087 static int jim_target_halt_gdb(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
4091 Jim_WrongNumArgs(interp, 1, argv, "[no parameters]");
4094 struct target *target = Jim_CmdPrivData(interp);
4096 if (target_call_event_callbacks(target, TARGET_EVENT_GDB_HALT) != ERROR_OK)
4102 static int jim_target_poll(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
4106 Jim_WrongNumArgs(interp, 1, argv, "[no parameters]");
4109 struct target *target = Jim_CmdPrivData(interp);
4110 if (!target->tap->enabled)
4111 return jim_target_tap_disabled(interp);
4114 if (!(target_was_examined(target))) {
4115 e = ERROR_TARGET_NOT_EXAMINED;
4117 e = target->type->poll(target);
4121 Jim_SetResult_sprintf(interp, "poll-fails: %d", e);
4127 static int jim_target_reset(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
4130 Jim_GetOpt_Setup(&goi, interp, argc - 1, argv + 1);
4134 Jim_WrongNumArgs(interp, 0, argv,
4135 "([tT]|[fF]|assert|deassert) BOOL");
4140 int e = Jim_GetOpt_Nvp(&goi, nvp_assert, &n);
4143 Jim_GetOpt_NvpUnknown(&goi, nvp_assert, 1);
4146 /* the halt or not param */
4148 e = Jim_GetOpt_Wide(&goi, &a);
4152 struct target *target = Jim_CmdPrivData(goi.interp);
4153 if (!target->tap->enabled)
4154 return jim_target_tap_disabled(interp);
4155 if (!(target_was_examined(target)))
4157 LOG_ERROR("Target not examined yet");
4158 return ERROR_TARGET_NOT_EXAMINED;
4160 if (!target->type->assert_reset || !target->type->deassert_reset)
4162 Jim_SetResult_sprintf(interp,
4163 "No target-specific reset for %s",
4164 target_name(target));
4167 /* determine if we should halt or not. */
4168 target->reset_halt = !!a;
4169 /* When this happens - all workareas are invalid. */
4170 target_free_all_working_areas_restore(target, 0);
4173 if (n->value == NVP_ASSERT) {
4174 e = target->type->assert_reset(target);
4176 e = target->type->deassert_reset(target);
4178 return (e == ERROR_OK) ? JIM_OK : JIM_ERR;
4181 static int jim_target_halt(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
4184 Jim_WrongNumArgs(interp, 1, argv, "[no parameters]");
4187 struct target *target = Jim_CmdPrivData(interp);
4188 if (!target->tap->enabled)
4189 return jim_target_tap_disabled(interp);
4190 int e = target->type->halt(target);
4191 return (e == ERROR_OK) ? JIM_OK : JIM_ERR;
4194 static int jim_target_wait_state(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
4197 Jim_GetOpt_Setup(&goi, interp, argc - 1, argv + 1);
4199 /* params: <name> statename timeoutmsecs */
4202 const char *cmd_name = Jim_GetString(argv[0], NULL);
4203 Jim_SetResult_sprintf(goi.interp,
4204 "%s <state_name> <timeout_in_msec>", cmd_name);
4209 int e = Jim_GetOpt_Nvp(&goi, nvp_target_state, &n);
4211 Jim_GetOpt_NvpUnknown(&goi, nvp_target_state,1);
4215 e = Jim_GetOpt_Wide(&goi, &a);
4219 struct target *target = Jim_CmdPrivData(interp);
4220 if (!target->tap->enabled)
4221 return jim_target_tap_disabled(interp);
4223 e = target_wait_state(target, n->value, a);
4226 Jim_SetResult_sprintf(goi.interp,
4227 "target: %s wait %s fails (%d) %s",
4228 target_name(target), n->name,
4229 e, target_strerror_safe(e));
4234 /* List for human, Events defined for this target.
4235 * scripts/programs should use 'name cget -event NAME'
4237 static int jim_target_event_list(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
4239 struct command_context *cmd_ctx = Jim_GetAssocData(interp, "context");
4240 struct target *target = Jim_CmdPrivData(interp);
4241 struct target_event_action *teap = target->event_action;
4242 command_print(cmd_ctx, "Event actions for target (%d) %s\n",
4243 target->target_number,
4244 target_name(target));
4245 command_print(cmd_ctx, "%-25s | Body", "Event");
4246 command_print(cmd_ctx, "------------------------- | "
4247 "----------------------------------------");
4250 Jim_Nvp *opt = Jim_Nvp_value2name_simple(nvp_target_event, teap->event);
4251 command_print(cmd_ctx, "%-25s | %s",
4252 opt->name, Jim_GetString(teap->body, NULL));
4255 command_print(cmd_ctx, "***END***");
4258 static int jim_target_current_state(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
4262 Jim_WrongNumArgs(interp, 1, argv, "[no parameters]");
4265 struct target *target = Jim_CmdPrivData(interp);
4266 Jim_SetResultString(interp, target_state_name(target), -1);
4269 static int jim_target_invoke_event(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
4272 Jim_GetOpt_Setup(&goi, interp, argc - 1, argv + 1);
4275 const char *cmd_name = Jim_GetString(argv[0], NULL);
4276 Jim_SetResult_sprintf(goi.interp, "%s <eventname>", cmd_name);
4280 int e = Jim_GetOpt_Nvp(&goi, nvp_target_event, &n);
4283 Jim_GetOpt_NvpUnknown(&goi, nvp_target_event, 1);
4286 struct target *target = Jim_CmdPrivData(interp);
4287 target_handle_event(target, n->value);
4291 static const struct command_registration target_instance_command_handlers[] = {
4293 .name = "configure",
4294 .mode = COMMAND_CONFIG,
4295 .jim_handler = jim_target_configure,
4296 .help = "configure a new target for use",
4297 .usage = "[target_attribute ...]",
4301 .mode = COMMAND_ANY,
4302 .jim_handler = jim_target_configure,
4303 .help = "returns the specified target attribute",
4304 .usage = "target_attribute",
4308 .mode = COMMAND_EXEC,
4309 .jim_handler = jim_target_mw,
4310 .help = "Write 32-bit word(s) to target memory",
4311 .usage = "address data [count]",
4315 .mode = COMMAND_EXEC,
4316 .jim_handler = jim_target_mw,
4317 .help = "Write 16-bit half-word(s) to target memory",
4318 .usage = "address data [count]",
4322 .mode = COMMAND_EXEC,
4323 .jim_handler = jim_target_mw,
4324 .help = "Write byte(s) to target memory",
4325 .usage = "address data [count]",
4329 .mode = COMMAND_EXEC,
4330 .jim_handler = jim_target_md,
4331 .help = "Display target memory as 32-bit words",
4332 .usage = "address [count]",
4336 .mode = COMMAND_EXEC,
4337 .jim_handler = jim_target_md,
4338 .help = "Display target memory as 16-bit half-words",
4339 .usage = "address [count]",
4343 .mode = COMMAND_EXEC,
4344 .jim_handler = jim_target_md,
4345 .help = "Display target memory as 8-bit bytes",
4346 .usage = "address [count]",
4349 .name = "array2mem",
4350 .mode = COMMAND_EXEC,
4351 .jim_handler = jim_target_array2mem,
4352 .help = "Writes Tcl array of 8/16/32 bit numbers "
4354 .usage = "arrayname bitwidth address count",
4357 .name = "mem2array",
4358 .mode = COMMAND_EXEC,
4359 .jim_handler = jim_target_mem2array,
4360 .help = "Loads Tcl array of 8/16/32 bit numbers "
4361 "from target memory",
4362 .usage = "arrayname bitwidth address count",
4365 .name = "eventlist",
4366 .mode = COMMAND_EXEC,
4367 .jim_handler = jim_target_event_list,
4368 .help = "displays a table of events defined for this target",
4372 .mode = COMMAND_EXEC,
4373 .jim_handler = jim_target_current_state,
4374 .help = "displays the current state of this target",
4377 .name = "arp_examine",
4378 .mode = COMMAND_EXEC,
4379 .jim_handler = jim_target_examine,
4380 .help = "used internally for reset processing",
4383 .name = "arp_halt_gdb",
4384 .mode = COMMAND_EXEC,
4385 .jim_handler = jim_target_halt_gdb,
4386 .help = "used internally for reset processing to halt GDB",
4390 .mode = COMMAND_EXEC,
4391 .jim_handler = jim_target_poll,
4392 .help = "used internally for reset processing",
4395 .name = "arp_reset",
4396 .mode = COMMAND_EXEC,
4397 .jim_handler = jim_target_reset,
4398 .help = "used internally for reset processing",
4402 .mode = COMMAND_EXEC,
4403 .jim_handler = jim_target_halt,
4404 .help = "used internally for reset processing",
4407 .name = "arp_waitstate",
4408 .mode = COMMAND_EXEC,
4409 .jim_handler = jim_target_wait_state,
4410 .help = "used internally for reset processing",
4413 .name = "invoke-event",
4414 .mode = COMMAND_EXEC,
4415 .jim_handler = jim_target_invoke_event,
4416 .help = "invoke handler for specified event",
4417 .usage = "event_name",
4419 COMMAND_REGISTRATION_DONE
4422 static int target_create(Jim_GetOptInfo *goi)
4430 struct target *target;
4431 struct command_context *cmd_ctx;
4433 cmd_ctx = Jim_GetAssocData(goi->interp, "context");
4434 if (goi->argc < 3) {
4435 Jim_WrongNumArgs(goi->interp, 1, goi->argv, "?name? ?type? ..options...");
4440 Jim_GetOpt_Obj(goi, &new_cmd);
4441 /* does this command exist? */
4442 cmd = Jim_GetCommand(goi->interp, new_cmd, JIM_ERRMSG);
4444 cp = Jim_GetString(new_cmd, NULL);
4445 Jim_SetResult_sprintf(goi->interp, "Command/target: %s Exists", cp);
4450 e = Jim_GetOpt_String(goi, &cp2, NULL);
4452 /* now does target type exist */
4453 for (x = 0 ; target_types[x] ; x++) {
4454 if (0 == strcmp(cp, target_types[x]->name)) {
4459 if (target_types[x] == NULL) {
4460 Jim_SetResult_sprintf(goi->interp, "Unknown target type %s, try one of ", cp);
4461 for (x = 0 ; target_types[x] ; x++) {
4462 if (target_types[x + 1]) {
4463 Jim_AppendStrings(goi->interp,
4464 Jim_GetResult(goi->interp),
4465 target_types[x]->name,
4468 Jim_AppendStrings(goi->interp,
4469 Jim_GetResult(goi->interp),
4471 target_types[x]->name,NULL);
4478 target = calloc(1,sizeof(struct target));
4479 /* set target number */
4480 target->target_number = new_target_number();
4482 /* allocate memory for each unique target type */
4483 target->type = (struct target_type*)calloc(1,sizeof(struct target_type));
4485 memcpy(target->type, target_types[x], sizeof(struct target_type));
4487 /* will be set by "-endian" */
4488 target->endianness = TARGET_ENDIAN_UNKNOWN;
4490 target->working_area = 0x0;
4491 target->working_area_size = 0x0;
4492 target->working_areas = NULL;
4493 target->backup_working_area = 0;
4495 target->state = TARGET_UNKNOWN;
4496 target->debug_reason = DBG_REASON_UNDEFINED;
4497 target->reg_cache = NULL;
4498 target->breakpoints = NULL;
4499 target->watchpoints = NULL;
4500 target->next = NULL;
4501 target->arch_info = NULL;
4503 target->display = 1;
4505 target->halt_issued = false;
4507 /* initialize trace information */
4508 target->trace_info = malloc(sizeof(struct trace));
4509 target->trace_info->num_trace_points = 0;
4510 target->trace_info->trace_points_size = 0;
4511 target->trace_info->trace_points = NULL;
4512 target->trace_info->trace_history_size = 0;
4513 target->trace_info->trace_history = NULL;
4514 target->trace_info->trace_history_pos = 0;
4515 target->trace_info->trace_history_overflowed = 0;
4517 target->dbgmsg = NULL;
4518 target->dbg_msg_enabled = 0;
4520 target->endianness = TARGET_ENDIAN_UNKNOWN;
4522 /* Do the rest as "configure" options */
4523 goi->isconfigure = 1;
4524 e = target_configure(goi, target);
4526 if (target->tap == NULL)
4528 Jim_SetResultString(goi->interp, "-chain-position required when creating target", -1);
4538 if (target->endianness == TARGET_ENDIAN_UNKNOWN) {
4539 /* default endian to little if not specified */
4540 target->endianness = TARGET_LITTLE_ENDIAN;
4543 /* incase variant is not set */
4544 if (!target->variant)
4545 target->variant = strdup("");
4547 cp = Jim_GetString(new_cmd, NULL);
4548 target->cmd_name = strdup(cp);
4550 /* create the target specific commands */
4551 if (target->type->commands) {
4552 e = register_commands(cmd_ctx, NULL, target->type->commands);
4554 LOG_ERROR("unable to register '%s' commands", cp);
4556 if (target->type->target_create) {
4557 (*(target->type->target_create))(target, goi->interp);
4560 /* append to end of list */
4562 struct target **tpp;
4563 tpp = &(all_targets);
4565 tpp = &((*tpp)->next);
4570 /* now - create the new target name command */
4571 const const struct command_registration target_subcommands[] = {
4573 .chain = target_instance_command_handlers,
4576 .chain = target->type->commands,
4578 COMMAND_REGISTRATION_DONE
4580 const const struct command_registration target_commands[] = {
4583 .mode = COMMAND_ANY,
4584 .help = "target command group",
4585 .chain = target_subcommands,
4587 COMMAND_REGISTRATION_DONE
4589 e = register_commands(cmd_ctx, NULL, target_commands);
4593 struct command *c = command_find_in_context(cmd_ctx, cp);
4595 command_set_handler_data(c, target);
4597 return (ERROR_OK == e) ? JIM_OK : JIM_ERR;
4600 static int jim_target_current(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
4604 Jim_WrongNumArgs(interp, 1, argv, "Too many parameters");
4607 struct command_context *cmd_ctx = Jim_GetAssocData(interp, "context");
4608 Jim_SetResultString(interp, get_current_target(cmd_ctx)->cmd_name, -1);
4612 static int jim_target_types(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
4616 Jim_WrongNumArgs(interp, 1, argv, "Too many parameters");
4619 Jim_SetResult(interp, Jim_NewListObj(interp, NULL, 0));
4620 for (unsigned x = 0; NULL != target_types[x]; x++)
4622 Jim_ListAppendElement(interp, Jim_GetResult(interp),
4623 Jim_NewStringObj(interp, target_types[x]->name, -1));
4628 static int jim_target_names(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
4632 Jim_WrongNumArgs(interp, 1, argv, "Too many parameters");
4635 Jim_SetResult(interp, Jim_NewListObj(interp, NULL, 0));
4636 struct target *target = all_targets;
4639 Jim_ListAppendElement(interp, Jim_GetResult(interp),
4640 Jim_NewStringObj(interp, target_name(target), -1));
4641 target = target->next;
4646 static int jim_target_create(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
4649 Jim_GetOpt_Setup(&goi, interp, argc - 1, argv + 1);
4652 Jim_WrongNumArgs(goi.interp, goi.argc, goi.argv,
4653 "<name> <target_type> [<target_options> ...]");
4656 return target_create(&goi);
4659 static int jim_target_number(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
4662 Jim_GetOpt_Setup(&goi, interp, argc - 1, argv + 1);
4664 /* It's OK to remove this mechanism sometime after August 2010 or so */
4665 LOG_WARNING("don't use numbers as target identifiers; use names");
4668 Jim_SetResult_sprintf(goi.interp, "usage: target number <number>");
4672 int e = Jim_GetOpt_Wide(&goi, &w);
4676 struct target *target;
4677 for (target = all_targets; NULL != target; target = target->next)
4679 if (target->target_number != w)
4682 Jim_SetResultString(goi.interp, target_name(target), -1);
4685 Jim_SetResult_sprintf(goi.interp,
4686 "Target: number %d does not exist", (int)(w));
4690 static int jim_target_count(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
4694 Jim_WrongNumArgs(interp, 1, argv, "<no parameters>");
4698 struct target *target = all_targets;
4699 while (NULL != target)
4701 target = target->next;
4704 Jim_SetResult(interp, Jim_NewIntObj(interp, count));
4708 static const struct command_registration target_subcommand_handlers[] = {
4711 .mode = COMMAND_CONFIG,
4712 .handler = handle_target_init_command,
4713 .help = "initialize targets",
4717 /* REVISIT this should be COMMAND_CONFIG ... */
4718 .mode = COMMAND_ANY,
4719 .jim_handler = jim_target_create,
4720 .usage = "name type '-chain-position' name [options ...]",
4721 .help = "Creates and selects a new target",
4725 .mode = COMMAND_ANY,
4726 .jim_handler = jim_target_current,
4727 .help = "Returns the currently selected target",
4731 .mode = COMMAND_ANY,
4732 .jim_handler = jim_target_types,
4733 .help = "Returns the available target types as "
4734 "a list of strings",
4738 .mode = COMMAND_ANY,
4739 .jim_handler = jim_target_names,
4740 .help = "Returns the names of all targets as a list of strings",
4744 .mode = COMMAND_ANY,
4745 .jim_handler = jim_target_number,
4747 .help = "Returns the name of the numbered target "
4752 .mode = COMMAND_ANY,
4753 .jim_handler = jim_target_count,
4754 .help = "Returns the number of targets as an integer "
4757 COMMAND_REGISTRATION_DONE
4768 static int fastload_num;
4769 static struct FastLoad *fastload;
4771 static void free_fastload(void)
4773 if (fastload != NULL)
4776 for (i = 0; i < fastload_num; i++)
4778 if (fastload[i].data)
4779 free(fastload[i].data);
4789 COMMAND_HANDLER(handle_fast_load_image_command)
4793 uint32_t image_size;
4794 uint32_t min_address = 0;
4795 uint32_t max_address = 0xffffffff;
4800 int retval = CALL_COMMAND_HANDLER(parse_load_image_command_CMD_ARGV,
4801 &image, &min_address, &max_address);
4802 if (ERROR_OK != retval)
4805 struct duration bench;
4806 duration_start(&bench);
4808 if (image_open(&image, CMD_ARGV[0], (CMD_ARGC >= 3) ? CMD_ARGV[2] : NULL) != ERROR_OK)
4815 fastload_num = image.num_sections;
4816 fastload = (struct FastLoad *)malloc(sizeof(struct FastLoad)*image.num_sections);
4817 if (fastload == NULL)
4819 image_close(&image);
4822 memset(fastload, 0, sizeof(struct FastLoad)*image.num_sections);
4823 for (i = 0; i < image.num_sections; i++)
4825 buffer = malloc(image.sections[i].size);
4828 command_print(CMD_CTX, "error allocating buffer for section (%d bytes)",
4829 (int)(image.sections[i].size));
4833 if ((retval = image_read_section(&image, i, 0x0, image.sections[i].size, buffer, &buf_cnt)) != ERROR_OK)
4839 uint32_t offset = 0;
4840 uint32_t length = buf_cnt;
4843 /* DANGER!!! beware of unsigned comparision here!!! */
4845 if ((image.sections[i].base_address + buf_cnt >= min_address)&&
4846 (image.sections[i].base_address < max_address))
4848 if (image.sections[i].base_address < min_address)
4850 /* clip addresses below */
4851 offset += min_address-image.sections[i].base_address;
4855 if (image.sections[i].base_address + buf_cnt > max_address)
4857 length -= (image.sections[i].base_address + buf_cnt)-max_address;
4860 fastload[i].address = image.sections[i].base_address + offset;
4861 fastload[i].data = malloc(length);
4862 if (fastload[i].data == NULL)
4867 memcpy(fastload[i].data, buffer + offset, length);
4868 fastload[i].length = length;
4870 image_size += length;
4871 command_print(CMD_CTX, "%u bytes written at address 0x%8.8x",
4872 (unsigned int)length,
4873 ((unsigned int)(image.sections[i].base_address + offset)));
4879 if ((ERROR_OK == retval) && (duration_measure(&bench) == ERROR_OK))
4881 command_print(CMD_CTX, "Loaded %" PRIu32 " bytes "
4882 "in %fs (%0.3f kb/s)", image_size,
4883 duration_elapsed(&bench), duration_kbps(&bench, image_size));
4885 command_print(CMD_CTX,
4886 "WARNING: image has not been loaded to target!"
4887 "You can issue a 'fast_load' to finish loading.");
4890 image_close(&image);
4892 if (retval != ERROR_OK)
4900 COMMAND_HANDLER(handle_fast_load_command)
4903 return ERROR_COMMAND_SYNTAX_ERROR;
4904 if (fastload == NULL)
4906 LOG_ERROR("No image in memory");
4910 int ms = timeval_ms();
4912 int retval = ERROR_OK;
4913 for (i = 0; i < fastload_num;i++)
4915 struct target *target = get_current_target(CMD_CTX);
4916 command_print(CMD_CTX, "Write to 0x%08x, length 0x%08x",
4917 (unsigned int)(fastload[i].address),
4918 (unsigned int)(fastload[i].length));
4919 if (retval == ERROR_OK)
4921 retval = target_write_buffer(target, fastload[i].address, fastload[i].length, fastload[i].data);
4923 size += fastload[i].length;
4925 int after = timeval_ms();
4926 command_print(CMD_CTX, "Loaded image %f kBytes/s", (float)(size/1024.0)/((float)(after-ms)/1000.0));
4930 static const struct command_registration target_command_handlers[] = {
4933 .handler = handle_targets_command,
4934 .mode = COMMAND_ANY,
4935 .help = "change current default target (one parameter) "
4936 "or prints table of all targets (no parameters)",
4937 .usage = "[target]",
4941 .mode = COMMAND_CONFIG,
4942 .help = "configure target",
4944 .chain = target_subcommand_handlers,
4946 COMMAND_REGISTRATION_DONE
4949 int target_register_commands(struct command_context *cmd_ctx)
4951 return register_commands(cmd_ctx, NULL, target_command_handlers);
4954 static bool target_reset_nag = true;
4956 bool get_target_reset_nag(void)
4958 return target_reset_nag;
4961 COMMAND_HANDLER(handle_target_reset_nag)
4963 return CALL_COMMAND_HANDLER(handle_command_parse_bool,
4964 &target_reset_nag, "Nag after each reset about options to improve "
4968 static const struct command_registration target_exec_command_handlers[] = {
4970 .name = "fast_load_image",
4971 .handler = handle_fast_load_image_command,
4972 .mode = COMMAND_ANY,
4973 .help = "Load image into server memory for later use by "
4974 "fast_load; primarily for profiling",
4975 .usage = "filename address ['bin'|'ihex'|'elf'|'s19'] "
4976 "[min_address [max_length]]",
4979 .name = "fast_load",
4980 .handler = handle_fast_load_command,
4981 .mode = COMMAND_EXEC,
4982 .help = "loads active fast load image to current target "
4983 "- mainly for profiling purposes",
4987 .handler = handle_profile_command,
4988 .mode = COMMAND_EXEC,
4989 .help = "profiling samples the CPU PC",
4991 /** @todo don't register virt2phys() unless target supports it */
4993 .name = "virt2phys",
4994 .handler = handle_virt2phys_command,
4995 .mode = COMMAND_ANY,
4996 .help = "translate a virtual address into a physical address",
4997 .usage = "virtual_address",
5001 .handler = handle_reg_command,
5002 .mode = COMMAND_EXEC,
5003 .help = "display or set a register; with no arguments, "
5004 "displays all registers and their values",
5005 .usage = "[(register_name|register_number) [value]]",
5009 .handler = handle_poll_command,
5010 .mode = COMMAND_EXEC,
5011 .help = "poll target state; or reconfigure background polling",
5012 .usage = "['on'|'off']",
5015 .name = "wait_halt",
5016 .handler = handle_wait_halt_command,
5017 .mode = COMMAND_EXEC,
5018 .help = "wait up to the specified number of milliseconds "
5019 "(default 5) for a previously requested halt",
5020 .usage = "[milliseconds]",
5024 .handler = handle_halt_command,
5025 .mode = COMMAND_EXEC,
5026 .help = "request target to halt, then wait up to the specified"
5027 "number of milliseconds (default 5) for it to complete",
5028 .usage = "[milliseconds]",
5032 .handler = handle_resume_command,
5033 .mode = COMMAND_EXEC,
5034 .help = "resume target execution from current PC or address",
5035 .usage = "[address]",
5039 .handler = handle_reset_command,
5040 .mode = COMMAND_EXEC,
5041 .usage = "[run|halt|init]",
5042 .help = "Reset all targets into the specified mode."
5043 "Default reset mode is run, if not given.",
5046 .name = "soft_reset_halt",
5047 .handler = handle_soft_reset_halt_command,
5048 .mode = COMMAND_EXEC,
5049 .help = "halt the target and do a soft reset",
5053 .handler = handle_step_command,
5054 .mode = COMMAND_EXEC,
5055 .help = "step one instruction from current PC or address",
5056 .usage = "[address]",
5060 .handler = handle_md_command,
5061 .mode = COMMAND_EXEC,
5062 .help = "display memory words",
5063 .usage = "['phys'] address [count]",
5067 .handler = handle_md_command,
5068 .mode = COMMAND_EXEC,
5069 .help = "display memory half-words",
5070 .usage = "['phys'] address [count]",
5074 .handler = handle_md_command,
5075 .mode = COMMAND_EXEC,
5076 .help = "display memory bytes",
5077 .usage = "['phys'] address [count]",
5081 .handler = handle_mw_command,
5082 .mode = COMMAND_EXEC,
5083 .help = "write memory word",
5084 .usage = "['phys'] address value [count]",
5088 .handler = handle_mw_command,
5089 .mode = COMMAND_EXEC,
5090 .help = "write memory half-word",
5091 .usage = "['phys'] address value [count]",
5095 .handler = handle_mw_command,
5096 .mode = COMMAND_EXEC,
5097 .help = "write memory byte",
5098 .usage = "['phys'] address value [count]",
5102 .handler = handle_bp_command,
5103 .mode = COMMAND_EXEC,
5104 .help = "list or set hardware or software breakpoint",
5105 .usage = "[address length ['hw']]",
5109 .handler = handle_rbp_command,
5110 .mode = COMMAND_EXEC,
5111 .help = "remove breakpoint",
5116 .handler = handle_wp_command,
5117 .mode = COMMAND_EXEC,
5118 .help = "list (no params) or create watchpoints",
5119 .usage = "[address length [('r'|'w'|'a') value [mask]]]",
5123 .handler = handle_rwp_command,
5124 .mode = COMMAND_EXEC,
5125 .help = "remove watchpoint",
5129 .name = "load_image",
5130 .handler = handle_load_image_command,
5131 .mode = COMMAND_EXEC,
5132 .usage = "filename address ['bin'|'ihex'|'elf'|'s19'] "
5133 "[min_address] [max_length]",
5136 .name = "dump_image",
5137 .handler = handle_dump_image_command,
5138 .mode = COMMAND_EXEC,
5139 .usage = "filename address size",
5142 .name = "verify_image",
5143 .handler = handle_verify_image_command,
5144 .mode = COMMAND_EXEC,
5145 .usage = "filename [offset [type]]",
5148 .name = "test_image",
5149 .handler = handle_test_image_command,
5150 .mode = COMMAND_EXEC,
5151 .usage = "filename [offset [type]]",
5154 .name = "ocd_mem2array",
5155 .mode = COMMAND_EXEC,
5156 .jim_handler = jim_mem2array,
5157 .help = "read 8/16/32 bit memory and return as a TCL array "
5158 "for script processing",
5159 .usage = "arrayname bitwidth address count",
5162 .name = "ocd_array2mem",
5163 .mode = COMMAND_EXEC,
5164 .jim_handler = jim_array2mem,
5165 .help = "convert a TCL array to memory locations "
5166 "and write the 8/16/32 bit values",
5167 .usage = "arrayname bitwidth address count",
5170 .name = "reset_nag",
5171 .handler = handle_target_reset_nag,
5172 .mode = COMMAND_ANY,
5173 .help = "Nag after each reset about options that could have been "
5174 "enabled to improve performance. ",
5175 .usage = "['enable'|'disable']",
5177 COMMAND_REGISTRATION_DONE
5179 int target_register_user_commands(struct command_context *cmd_ctx)
5181 int retval = ERROR_OK;
5182 if ((retval = target_request_register_commands(cmd_ctx)) != ERROR_OK)
5185 if ((retval = trace_register_commands(cmd_ctx)) != ERROR_OK)
5189 return register_commands(cmd_ctx, NULL, target_exec_command_handlers);