1 /***************************************************************************
2 * Copyright (C) 2005 by Dominic Rath *
3 * Dominic.Rath@gmx.de *
5 * Copyright (C) 2007-2010 Ø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>
38 #include <flash/nor/core.h>
41 #include "target_type.h"
42 #include "target_request.h"
43 #include "breakpoints.h"
49 static int target_array2mem(Jim_Interp *interp, struct target *target,
50 int argc, Jim_Obj *const *argv);
51 static int target_mem2array(Jim_Interp *interp, struct target *target,
52 int argc, Jim_Obj *const *argv);
53 static int target_register_user_commands(struct command_context *cmd_ctx);
56 extern struct target_type arm7tdmi_target;
57 extern struct target_type arm720t_target;
58 extern struct target_type arm9tdmi_target;
59 extern struct target_type arm920t_target;
60 extern struct target_type arm966e_target;
61 extern struct target_type arm926ejs_target;
62 extern struct target_type fa526_target;
63 extern struct target_type feroceon_target;
64 extern struct target_type dragonite_target;
65 extern struct target_type xscale_target;
66 extern struct target_type cortexm3_target;
67 extern struct target_type cortexa8_target;
68 extern struct target_type arm11_target;
69 extern struct target_type mips_m4k_target;
70 extern struct target_type avr_target;
71 extern struct target_type dsp563xx_target;
72 extern struct target_type testee_target;
74 static struct target_type *target_types[] =
96 struct target *all_targets = NULL;
97 static struct target_event_callback *target_event_callbacks = NULL;
98 static struct target_timer_callback *target_timer_callbacks = NULL;
100 static const Jim_Nvp nvp_assert[] = {
101 { .name = "assert", NVP_ASSERT },
102 { .name = "deassert", NVP_DEASSERT },
103 { .name = "T", NVP_ASSERT },
104 { .name = "F", NVP_DEASSERT },
105 { .name = "t", NVP_ASSERT },
106 { .name = "f", NVP_DEASSERT },
107 { .name = NULL, .value = -1 }
110 static const Jim_Nvp nvp_error_target[] = {
111 { .value = ERROR_TARGET_INVALID, .name = "err-invalid" },
112 { .value = ERROR_TARGET_INIT_FAILED, .name = "err-init-failed" },
113 { .value = ERROR_TARGET_TIMEOUT, .name = "err-timeout" },
114 { .value = ERROR_TARGET_NOT_HALTED, .name = "err-not-halted" },
115 { .value = ERROR_TARGET_FAILURE, .name = "err-failure" },
116 { .value = ERROR_TARGET_UNALIGNED_ACCESS , .name = "err-unaligned-access" },
117 { .value = ERROR_TARGET_DATA_ABORT , .name = "err-data-abort" },
118 { .value = ERROR_TARGET_RESOURCE_NOT_AVAILABLE , .name = "err-resource-not-available" },
119 { .value = ERROR_TARGET_TRANSLATION_FAULT , .name = "err-translation-fault" },
120 { .value = ERROR_TARGET_NOT_RUNNING, .name = "err-not-running" },
121 { .value = ERROR_TARGET_NOT_EXAMINED, .name = "err-not-examined" },
122 { .value = -1, .name = NULL }
125 static const char *target_strerror_safe(int err)
129 n = Jim_Nvp_value2name_simple(nvp_error_target, err);
130 if (n->name == NULL) {
137 static const Jim_Nvp nvp_target_event[] = {
138 { .value = TARGET_EVENT_OLD_gdb_program_config , .name = "old-gdb_program_config" },
139 { .value = TARGET_EVENT_OLD_pre_resume , .name = "old-pre_resume" },
141 { .value = TARGET_EVENT_GDB_HALT, .name = "gdb-halt" },
142 { .value = TARGET_EVENT_HALTED, .name = "halted" },
143 { .value = TARGET_EVENT_RESUMED, .name = "resumed" },
144 { .value = TARGET_EVENT_RESUME_START, .name = "resume-start" },
145 { .value = TARGET_EVENT_RESUME_END, .name = "resume-end" },
147 { .name = "gdb-start", .value = TARGET_EVENT_GDB_START },
148 { .name = "gdb-end", .value = TARGET_EVENT_GDB_END },
150 /* historical name */
152 { .value = TARGET_EVENT_RESET_START, .name = "reset-start" },
154 { .value = TARGET_EVENT_RESET_ASSERT_PRE, .name = "reset-assert-pre" },
155 { .value = TARGET_EVENT_RESET_ASSERT, .name = "reset-assert" },
156 { .value = TARGET_EVENT_RESET_ASSERT_POST, .name = "reset-assert-post" },
157 { .value = TARGET_EVENT_RESET_DEASSERT_PRE, .name = "reset-deassert-pre" },
158 { .value = TARGET_EVENT_RESET_DEASSERT_POST, .name = "reset-deassert-post" },
159 { .value = TARGET_EVENT_RESET_HALT_PRE, .name = "reset-halt-pre" },
160 { .value = TARGET_EVENT_RESET_HALT_POST, .name = "reset-halt-post" },
161 { .value = TARGET_EVENT_RESET_WAIT_PRE, .name = "reset-wait-pre" },
162 { .value = TARGET_EVENT_RESET_WAIT_POST, .name = "reset-wait-post" },
163 { .value = TARGET_EVENT_RESET_INIT, .name = "reset-init" },
164 { .value = TARGET_EVENT_RESET_END, .name = "reset-end" },
166 { .value = TARGET_EVENT_EXAMINE_START, .name = "examine-start" },
167 { .value = TARGET_EVENT_EXAMINE_END, .name = "examine-end" },
169 { .value = TARGET_EVENT_DEBUG_HALTED, .name = "debug-halted" },
170 { .value = TARGET_EVENT_DEBUG_RESUMED, .name = "debug-resumed" },
172 { .value = TARGET_EVENT_GDB_ATTACH, .name = "gdb-attach" },
173 { .value = TARGET_EVENT_GDB_DETACH, .name = "gdb-detach" },
175 { .value = TARGET_EVENT_GDB_FLASH_WRITE_START, .name = "gdb-flash-write-start" },
176 { .value = TARGET_EVENT_GDB_FLASH_WRITE_END , .name = "gdb-flash-write-end" },
178 { .value = TARGET_EVENT_GDB_FLASH_ERASE_START, .name = "gdb-flash-erase-start" },
179 { .value = TARGET_EVENT_GDB_FLASH_ERASE_END , .name = "gdb-flash-erase-end" },
181 { .value = TARGET_EVENT_RESUME_START, .name = "resume-start" },
182 { .value = TARGET_EVENT_RESUMED , .name = "resume-ok" },
183 { .value = TARGET_EVENT_RESUME_END , .name = "resume-end" },
185 { .name = NULL, .value = -1 }
188 static const Jim_Nvp nvp_target_state[] = {
189 { .name = "unknown", .value = TARGET_UNKNOWN },
190 { .name = "running", .value = TARGET_RUNNING },
191 { .name = "halted", .value = TARGET_HALTED },
192 { .name = "reset", .value = TARGET_RESET },
193 { .name = "debug-running", .value = TARGET_DEBUG_RUNNING },
194 { .name = NULL, .value = -1 },
197 static const Jim_Nvp nvp_target_debug_reason [] = {
198 { .name = "debug-request" , .value = DBG_REASON_DBGRQ },
199 { .name = "breakpoint" , .value = DBG_REASON_BREAKPOINT },
200 { .name = "watchpoint" , .value = DBG_REASON_WATCHPOINT },
201 { .name = "watchpoint-and-breakpoint", .value = DBG_REASON_WPTANDBKPT },
202 { .name = "single-step" , .value = DBG_REASON_SINGLESTEP },
203 { .name = "target-not-halted" , .value = DBG_REASON_NOTHALTED },
204 { .name = "undefined" , .value = DBG_REASON_UNDEFINED },
205 { .name = NULL, .value = -1 },
208 static const Jim_Nvp nvp_target_endian[] = {
209 { .name = "big", .value = TARGET_BIG_ENDIAN },
210 { .name = "little", .value = TARGET_LITTLE_ENDIAN },
211 { .name = "be", .value = TARGET_BIG_ENDIAN },
212 { .name = "le", .value = TARGET_LITTLE_ENDIAN },
213 { .name = NULL, .value = -1 },
216 static const Jim_Nvp nvp_reset_modes[] = {
217 { .name = "unknown", .value = RESET_UNKNOWN },
218 { .name = "run" , .value = RESET_RUN },
219 { .name = "halt" , .value = RESET_HALT },
220 { .name = "init" , .value = RESET_INIT },
221 { .name = NULL , .value = -1 },
224 const char *debug_reason_name(struct target *t)
228 cp = Jim_Nvp_value2name_simple(nvp_target_debug_reason,
229 t->debug_reason)->name;
231 LOG_ERROR("Invalid debug reason: %d", (int)(t->debug_reason));
232 cp = "(*BUG*unknown*BUG*)";
238 target_state_name( struct target *t )
241 cp = Jim_Nvp_value2name_simple(nvp_target_state, t->state)->name;
243 LOG_ERROR("Invalid target state: %d", (int)(t->state));
244 cp = "(*BUG*unknown*BUG*)";
249 /* determine the number of the new target */
250 static int new_target_number(void)
255 /* number is 0 based */
259 if (x < t->target_number) {
260 x = t->target_number;
267 /* read a uint32_t from a buffer in target memory endianness */
268 uint32_t target_buffer_get_u32(struct target *target, const uint8_t *buffer)
270 if (target->endianness == TARGET_LITTLE_ENDIAN)
271 return le_to_h_u32(buffer);
273 return be_to_h_u32(buffer);
276 /* read a uint16_t from a buffer in target memory endianness */
277 uint16_t target_buffer_get_u16(struct target *target, const uint8_t *buffer)
279 if (target->endianness == TARGET_LITTLE_ENDIAN)
280 return le_to_h_u16(buffer);
282 return be_to_h_u16(buffer);
285 /* read a uint8_t from a buffer in target memory endianness */
286 static uint8_t target_buffer_get_u8(struct target *target, const uint8_t *buffer)
288 return *buffer & 0x0ff;
291 /* write a uint32_t to a buffer in target memory endianness */
292 void target_buffer_set_u32(struct target *target, uint8_t *buffer, uint32_t value)
294 if (target->endianness == TARGET_LITTLE_ENDIAN)
295 h_u32_to_le(buffer, value);
297 h_u32_to_be(buffer, value);
300 /* write a uint16_t to a buffer in target memory endianness */
301 void target_buffer_set_u16(struct target *target, uint8_t *buffer, uint16_t value)
303 if (target->endianness == TARGET_LITTLE_ENDIAN)
304 h_u16_to_le(buffer, value);
306 h_u16_to_be(buffer, value);
309 /* write a uint8_t to a buffer in target memory endianness */
310 static void target_buffer_set_u8(struct target *target, uint8_t *buffer, uint8_t value)
315 /* return a pointer to a configured target; id is name or number */
316 struct target *get_target(const char *id)
318 struct target *target;
320 /* try as tcltarget name */
321 for (target = all_targets; target; target = target->next) {
322 if (target->cmd_name == NULL)
324 if (strcmp(id, target->cmd_name) == 0)
328 /* It's OK to remove this fallback sometime after August 2010 or so */
330 /* no match, try as number */
332 if (parse_uint(id, &num) != ERROR_OK)
335 for (target = all_targets; target; target = target->next) {
336 if (target->target_number == (int)num) {
337 LOG_WARNING("use '%s' as target identifier, not '%u'",
338 target->cmd_name, num);
346 /* returns a pointer to the n-th configured target */
347 static struct target *get_target_by_num(int num)
349 struct target *target = all_targets;
352 if (target->target_number == num) {
355 target = target->next;
361 struct target* get_current_target(struct command_context *cmd_ctx)
363 struct target *target = get_target_by_num(cmd_ctx->current_target);
367 LOG_ERROR("BUG: current_target out of bounds");
374 int target_poll(struct target *target)
378 /* We can't poll until after examine */
379 if (!target_was_examined(target))
381 /* Fail silently lest we pollute the log */
385 retval = target->type->poll(target);
386 if (retval != ERROR_OK)
389 if (target->halt_issued)
391 if (target->state == TARGET_HALTED)
393 target->halt_issued = false;
396 long long t = timeval_ms() - target->halt_issued_time;
399 target->halt_issued = false;
400 LOG_INFO("Halt timed out, wake up GDB.");
401 target_call_event_callbacks(target, TARGET_EVENT_GDB_HALT);
409 int target_halt(struct target *target)
412 /* We can't poll until after examine */
413 if (!target_was_examined(target))
415 LOG_ERROR("Target not examined yet");
419 retval = target->type->halt(target);
420 if (retval != ERROR_OK)
423 target->halt_issued = true;
424 target->halt_issued_time = timeval_ms();
430 * Make the target (re)start executing using its saved execution
431 * context (possibly with some modifications).
433 * @param target Which target should start executing.
434 * @param current True to use the target's saved program counter instead
435 * of the address parameter
436 * @param address Optionally used as the program counter.
437 * @param handle_breakpoints True iff breakpoints at the resumption PC
438 * should be skipped. (For example, maybe execution was stopped by
439 * such a breakpoint, in which case it would be counterprodutive to
441 * @param debug_execution False if all working areas allocated by OpenOCD
442 * should be released and/or restored to their original contents.
443 * (This would for example be true to run some downloaded "helper"
444 * algorithm code, which resides in one such working buffer and uses
445 * another for data storage.)
447 * @todo Resolve the ambiguity about what the "debug_execution" flag
448 * signifies. For example, Target implementations don't agree on how
449 * it relates to invalidation of the register cache, or to whether
450 * breakpoints and watchpoints should be enabled. (It would seem wrong
451 * to enable breakpoints when running downloaded "helper" algorithms
452 * (debug_execution true), since the breakpoints would be set to match
453 * target firmware being debugged, not the helper algorithm.... and
454 * enabling them could cause such helpers to malfunction (for example,
455 * by overwriting data with a breakpoint instruction. On the other
456 * hand the infrastructure for running such helpers might use this
457 * procedure but rely on hardware breakpoint to detect termination.)
459 int target_resume(struct target *target, int current, uint32_t address, int handle_breakpoints, int debug_execution)
463 /* We can't poll until after examine */
464 if (!target_was_examined(target))
466 LOG_ERROR("Target not examined yet");
470 /* note that resume *must* be asynchronous. The CPU can halt before
471 * we poll. The CPU can even halt at the current PC as a result of
472 * a software breakpoint being inserted by (a bug?) the application.
474 if ((retval = target->type->resume(target, current, address, handle_breakpoints, debug_execution)) != ERROR_OK)
477 /* Invalidate any cached protect/erase/... flash status, since
478 * almost all targets will now be able modify the flash by
479 * themselves. We want flash drivers and infrastructure to
480 * be able to rely on (non-invalidated) cached state.
482 * For now we require that algorithms provided by OpenOCD are
483 * used only by code which properly maintains that cached state.
486 * REVISIT do the same for NAND ; maybe other flash flavors too...
488 if (!target->running_alg)
493 static int target_process_reset(struct command_context *cmd_ctx, enum target_reset_mode reset_mode)
498 n = Jim_Nvp_value2name_simple(nvp_reset_modes, reset_mode);
499 if (n->name == NULL) {
500 LOG_ERROR("invalid reset mode");
504 /* disable polling during reset to make reset event scripts
505 * more predictable, i.e. dr/irscan & pathmove in events will
506 * not have JTAG operations injected into the middle of a sequence.
508 bool save_poll = jtag_poll_get_enabled();
510 jtag_poll_set_enabled(false);
512 sprintf(buf, "ocd_process_reset %s", n->name);
513 retval = Jim_Eval(cmd_ctx->interp, buf);
515 jtag_poll_set_enabled(save_poll);
517 if (retval != JIM_OK) {
518 Jim_PrintErrorMessage(cmd_ctx->interp);
522 /* We want any events to be processed before the prompt */
523 retval = target_call_timer_callbacks_now();
525 struct target *target;
526 for (target = all_targets; target; target = target->next) {
527 target->type->check_reset(target);
533 static int identity_virt2phys(struct target *target,
534 uint32_t virtual, uint32_t *physical)
540 static int no_mmu(struct target *target, int *enabled)
546 static int default_examine(struct target *target)
548 target_set_examined(target);
552 /* no check by default */
553 static int default_check_reset(struct target *target)
558 int target_examine_one(struct target *target)
560 return target->type->examine(target);
563 static int jtag_enable_callback(enum jtag_event event, void *priv)
565 struct target *target = priv;
567 if (event != JTAG_TAP_EVENT_ENABLE || !target->tap->enabled)
570 jtag_unregister_event_callback(jtag_enable_callback, target);
571 return target_examine_one(target);
575 /* Targets that correctly implement init + examine, i.e.
576 * no communication with target during init:
580 int target_examine(void)
582 int retval = ERROR_OK;
583 struct target *target;
585 for (target = all_targets; target; target = target->next)
587 /* defer examination, but don't skip it */
588 if (!target->tap->enabled) {
589 jtag_register_event_callback(jtag_enable_callback,
593 if ((retval = target_examine_one(target)) != ERROR_OK)
598 const char *target_type_name(struct target *target)
600 return target->type->name;
603 static int target_write_memory_imp(struct target *target, uint32_t address, uint32_t size, uint32_t count, uint8_t *buffer)
605 if (!target_was_examined(target))
607 LOG_ERROR("Target not examined yet");
610 return target->type->write_memory_imp(target, address, size, count, buffer);
613 static int target_read_memory_imp(struct target *target, uint32_t address, uint32_t size, uint32_t count, uint8_t *buffer)
615 if (!target_was_examined(target))
617 LOG_ERROR("Target not examined yet");
620 return target->type->read_memory_imp(target, address, size, count, buffer);
623 static int target_soft_reset_halt_imp(struct target *target)
625 if (!target_was_examined(target))
627 LOG_ERROR("Target not examined yet");
630 if (!target->type->soft_reset_halt_imp) {
631 LOG_ERROR("Target %s does not support soft_reset_halt",
632 target_name(target));
635 return target->type->soft_reset_halt_imp(target);
639 * Downloads a target-specific native code algorithm to the target,
640 * and executes it. * Note that some targets may need to set up, enable,
641 * and tear down a breakpoint (hard or * soft) to detect algorithm
642 * termination, while others may support lower overhead schemes where
643 * soft breakpoints embedded in the algorithm automatically terminate the
646 * @param target used to run the algorithm
647 * @param arch_info target-specific description of the algorithm.
649 int target_run_algorithm(struct target *target,
650 int num_mem_params, struct mem_param *mem_params,
651 int num_reg_params, struct reg_param *reg_param,
652 uint32_t entry_point, uint32_t exit_point,
653 int timeout_ms, void *arch_info)
655 int retval = ERROR_FAIL;
657 if (!target_was_examined(target))
659 LOG_ERROR("Target not examined yet");
662 if (!target->type->run_algorithm) {
663 LOG_ERROR("Target type '%s' does not support %s",
664 target_type_name(target), __func__);
668 target->running_alg = true;
669 retval = target->type->run_algorithm(target,
670 num_mem_params, mem_params,
671 num_reg_params, reg_param,
672 entry_point, exit_point, timeout_ms, arch_info);
673 target->running_alg = false;
680 int target_read_memory(struct target *target,
681 uint32_t address, uint32_t size, uint32_t count, uint8_t *buffer)
683 return target->type->read_memory(target, address, size, count, buffer);
686 static int target_read_phys_memory(struct target *target,
687 uint32_t address, uint32_t size, uint32_t count, uint8_t *buffer)
689 return target->type->read_phys_memory(target, address, size, count, buffer);
692 int target_write_memory(struct target *target,
693 uint32_t address, uint32_t size, uint32_t count, uint8_t *buffer)
695 return target->type->write_memory(target, address, size, count, buffer);
698 static int target_write_phys_memory(struct target *target,
699 uint32_t address, uint32_t size, uint32_t count, uint8_t *buffer)
701 return target->type->write_phys_memory(target, address, size, count, buffer);
704 int target_bulk_write_memory(struct target *target,
705 uint32_t address, uint32_t count, uint8_t *buffer)
707 return target->type->bulk_write_memory(target, address, count, buffer);
710 int target_add_breakpoint(struct target *target,
711 struct breakpoint *breakpoint)
713 if (target->state != TARGET_HALTED) {
714 LOG_WARNING("target %s is not halted", target->cmd_name);
715 return ERROR_TARGET_NOT_HALTED;
717 return target->type->add_breakpoint(target, breakpoint);
719 int target_remove_breakpoint(struct target *target,
720 struct breakpoint *breakpoint)
722 return target->type->remove_breakpoint(target, breakpoint);
725 int target_add_watchpoint(struct target *target,
726 struct watchpoint *watchpoint)
728 if (target->state != TARGET_HALTED) {
729 LOG_WARNING("target %s is not halted", target->cmd_name);
730 return ERROR_TARGET_NOT_HALTED;
732 return target->type->add_watchpoint(target, watchpoint);
734 int target_remove_watchpoint(struct target *target,
735 struct watchpoint *watchpoint)
737 return target->type->remove_watchpoint(target, watchpoint);
740 int target_get_gdb_reg_list(struct target *target,
741 struct reg **reg_list[], int *reg_list_size)
743 return target->type->get_gdb_reg_list(target, reg_list, reg_list_size);
745 int target_step(struct target *target,
746 int current, uint32_t address, int handle_breakpoints)
748 return target->type->step(target, current, address, handle_breakpoints);
753 * Reset the @c examined flag for the given target.
754 * Pure paranoia -- targets are zeroed on allocation.
756 static void target_reset_examined(struct target *target)
758 target->examined = false;
762 err_read_phys_memory(struct target *target, uint32_t address,
763 uint32_t size, uint32_t count, uint8_t *buffer)
765 LOG_ERROR("Not implemented: %s", __func__);
770 err_write_phys_memory(struct target *target, uint32_t address,
771 uint32_t size, uint32_t count, uint8_t *buffer)
773 LOG_ERROR("Not implemented: %s", __func__);
777 static int handle_target(void *priv);
779 static int target_init_one(struct command_context *cmd_ctx,
780 struct target *target)
782 target_reset_examined(target);
784 struct target_type *type = target->type;
785 if (type->examine == NULL)
786 type->examine = default_examine;
788 if (type->check_reset== NULL)
789 type->check_reset = default_check_reset;
791 int retval = type->init_target(cmd_ctx, target);
792 if (ERROR_OK != retval)
794 LOG_ERROR("target '%s' init failed", target_name(target));
799 * @todo get rid of those *memory_imp() methods, now that all
800 * callers are using target_*_memory() accessors ... and make
801 * sure the "physical" paths handle the same issues.
803 /* a non-invasive way(in terms of patches) to add some code that
804 * runs before the type->write/read_memory implementation
806 type->write_memory_imp = target->type->write_memory;
807 type->write_memory = target_write_memory_imp;
809 type->read_memory_imp = target->type->read_memory;
810 type->read_memory = target_read_memory_imp;
812 type->soft_reset_halt_imp = target->type->soft_reset_halt;
813 type->soft_reset_halt = target_soft_reset_halt_imp;
815 /* Sanity-check MMU support ... stub in what we must, to help
816 * implement it in stages, but warn if we need to do so.
820 if (type->write_phys_memory == NULL)
822 LOG_ERROR("type '%s' is missing write_phys_memory",
824 type->write_phys_memory = err_write_phys_memory;
826 if (type->read_phys_memory == NULL)
828 LOG_ERROR("type '%s' is missing read_phys_memory",
830 type->read_phys_memory = err_read_phys_memory;
832 if (type->virt2phys == NULL)
834 LOG_ERROR("type '%s' is missing virt2phys", type->name);
835 type->virt2phys = identity_virt2phys;
840 /* Make sure no-MMU targets all behave the same: make no
841 * distinction between physical and virtual addresses, and
842 * ensure that virt2phys() is always an identity mapping.
844 if (type->write_phys_memory || type->read_phys_memory
847 LOG_WARNING("type '%s' has bad MMU hooks", type->name);
851 type->write_phys_memory = type->write_memory;
852 type->read_phys_memory = type->read_memory;
853 type->virt2phys = identity_virt2phys;
858 static int target_init(struct command_context *cmd_ctx)
860 struct target *target;
863 for (target = all_targets; target; target = target->next)
865 retval = target_init_one(cmd_ctx, target);
866 if (ERROR_OK != retval)
873 retval = target_register_user_commands(cmd_ctx);
874 if (ERROR_OK != retval)
877 retval = target_register_timer_callback(&handle_target,
878 100, 1, cmd_ctx->interp);
879 if (ERROR_OK != retval)
885 COMMAND_HANDLER(handle_target_init_command)
888 return ERROR_COMMAND_SYNTAX_ERROR;
890 static bool target_initialized = false;
891 if (target_initialized)
893 LOG_INFO("'target init' has already been called");
896 target_initialized = true;
898 LOG_DEBUG("Initializing targets...");
899 return target_init(CMD_CTX);
902 int target_register_event_callback(int (*callback)(struct target *target, enum target_event event, void *priv), void *priv)
904 struct target_event_callback **callbacks_p = &target_event_callbacks;
906 if (callback == NULL)
908 return ERROR_INVALID_ARGUMENTS;
913 while ((*callbacks_p)->next)
914 callbacks_p = &((*callbacks_p)->next);
915 callbacks_p = &((*callbacks_p)->next);
918 (*callbacks_p) = malloc(sizeof(struct target_event_callback));
919 (*callbacks_p)->callback = callback;
920 (*callbacks_p)->priv = priv;
921 (*callbacks_p)->next = NULL;
926 int target_register_timer_callback(int (*callback)(void *priv), int time_ms, int periodic, void *priv)
928 struct target_timer_callback **callbacks_p = &target_timer_callbacks;
931 if (callback == NULL)
933 return ERROR_INVALID_ARGUMENTS;
938 while ((*callbacks_p)->next)
939 callbacks_p = &((*callbacks_p)->next);
940 callbacks_p = &((*callbacks_p)->next);
943 (*callbacks_p) = malloc(sizeof(struct target_timer_callback));
944 (*callbacks_p)->callback = callback;
945 (*callbacks_p)->periodic = periodic;
946 (*callbacks_p)->time_ms = time_ms;
948 gettimeofday(&now, NULL);
949 (*callbacks_p)->when.tv_usec = now.tv_usec + (time_ms % 1000) * 1000;
950 time_ms -= (time_ms % 1000);
951 (*callbacks_p)->when.tv_sec = now.tv_sec + (time_ms / 1000);
952 if ((*callbacks_p)->when.tv_usec > 1000000)
954 (*callbacks_p)->when.tv_usec = (*callbacks_p)->when.tv_usec - 1000000;
955 (*callbacks_p)->when.tv_sec += 1;
958 (*callbacks_p)->priv = priv;
959 (*callbacks_p)->next = NULL;
964 int target_unregister_event_callback(int (*callback)(struct target *target, enum target_event event, void *priv), void *priv)
966 struct target_event_callback **p = &target_event_callbacks;
967 struct target_event_callback *c = target_event_callbacks;
969 if (callback == NULL)
971 return ERROR_INVALID_ARGUMENTS;
976 struct target_event_callback *next = c->next;
977 if ((c->callback == callback) && (c->priv == priv))
991 static int target_unregister_timer_callback(int (*callback)(void *priv), void *priv)
993 struct target_timer_callback **p = &target_timer_callbacks;
994 struct target_timer_callback *c = target_timer_callbacks;
996 if (callback == NULL)
998 return ERROR_INVALID_ARGUMENTS;
1003 struct target_timer_callback *next = c->next;
1004 if ((c->callback == callback) && (c->priv == priv))
1018 int target_call_event_callbacks(struct target *target, enum target_event event)
1020 struct target_event_callback *callback = target_event_callbacks;
1021 struct target_event_callback *next_callback;
1023 if (event == TARGET_EVENT_HALTED)
1025 /* execute early halted first */
1026 target_call_event_callbacks(target, TARGET_EVENT_GDB_HALT);
1029 LOG_DEBUG("target event %i (%s)",
1031 Jim_Nvp_value2name_simple(nvp_target_event, event)->name);
1033 target_handle_event(target, event);
1037 next_callback = callback->next;
1038 callback->callback(target, event, callback->priv);
1039 callback = next_callback;
1045 static int target_timer_callback_periodic_restart(
1046 struct target_timer_callback *cb, struct timeval *now)
1048 int time_ms = cb->time_ms;
1049 cb->when.tv_usec = now->tv_usec + (time_ms % 1000) * 1000;
1050 time_ms -= (time_ms % 1000);
1051 cb->when.tv_sec = now->tv_sec + time_ms / 1000;
1052 if (cb->when.tv_usec > 1000000)
1054 cb->when.tv_usec = cb->when.tv_usec - 1000000;
1055 cb->when.tv_sec += 1;
1060 static int target_call_timer_callback(struct target_timer_callback *cb,
1061 struct timeval *now)
1063 cb->callback(cb->priv);
1066 return target_timer_callback_periodic_restart(cb, now);
1068 return target_unregister_timer_callback(cb->callback, cb->priv);
1071 static int target_call_timer_callbacks_check_time(int checktime)
1076 gettimeofday(&now, NULL);
1078 struct target_timer_callback *callback = target_timer_callbacks;
1081 // cleaning up may unregister and free this callback
1082 struct target_timer_callback *next_callback = callback->next;
1084 bool call_it = callback->callback &&
1085 ((!checktime && callback->periodic) ||
1086 now.tv_sec > callback->when.tv_sec ||
1087 (now.tv_sec == callback->when.tv_sec &&
1088 now.tv_usec >= callback->when.tv_usec));
1092 int retval = target_call_timer_callback(callback, &now);
1093 if (retval != ERROR_OK)
1097 callback = next_callback;
1103 int target_call_timer_callbacks(void)
1105 return target_call_timer_callbacks_check_time(1);
1108 /* invoke periodic callbacks immediately */
1109 int target_call_timer_callbacks_now(void)
1111 return target_call_timer_callbacks_check_time(0);
1114 int target_alloc_working_area_try(struct target *target, uint32_t size, struct working_area **area)
1116 struct working_area *c = target->working_areas;
1117 struct working_area *new_wa = NULL;
1119 /* Reevaluate working area address based on MMU state*/
1120 if (target->working_areas == NULL)
1125 retval = target->type->mmu(target, &enabled);
1126 if (retval != ERROR_OK)
1132 if (target->working_area_phys_spec) {
1133 LOG_DEBUG("MMU disabled, using physical "
1134 "address for working memory 0x%08x",
1135 (unsigned)target->working_area_phys);
1136 target->working_area = target->working_area_phys;
1138 LOG_ERROR("No working memory available. "
1139 "Specify -work-area-phys to target.");
1140 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
1143 if (target->working_area_virt_spec) {
1144 LOG_DEBUG("MMU enabled, using virtual "
1145 "address for working memory 0x%08x",
1146 (unsigned)target->working_area_virt);
1147 target->working_area = target->working_area_virt;
1149 LOG_ERROR("No working memory available. "
1150 "Specify -work-area-virt to target.");
1151 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
1156 /* only allocate multiples of 4 byte */
1159 LOG_ERROR("BUG: code tried to allocate unaligned number of bytes (0x%08x), padding", ((unsigned)(size)));
1160 size = (size + 3) & (~3);
1163 /* see if there's already a matching working area */
1166 if ((c->free) && (c->size == size))
1174 /* if not, allocate a new one */
1177 struct working_area **p = &target->working_areas;
1178 uint32_t first_free = target->working_area;
1179 uint32_t free_size = target->working_area_size;
1181 c = target->working_areas;
1184 first_free += c->size;
1185 free_size -= c->size;
1190 if (free_size < size)
1192 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
1195 LOG_DEBUG("allocated new working area at address 0x%08x", (unsigned)first_free);
1197 new_wa = malloc(sizeof(struct working_area));
1198 new_wa->next = NULL;
1199 new_wa->size = size;
1200 new_wa->address = first_free;
1202 if (target->backup_working_area)
1205 new_wa->backup = malloc(new_wa->size);
1206 if ((retval = target_read_memory(target, new_wa->address, 4, new_wa->size / 4, new_wa->backup)) != ERROR_OK)
1208 free(new_wa->backup);
1215 new_wa->backup = NULL;
1218 /* put new entry in list */
1222 /* mark as used, and return the new (reused) area */
1227 new_wa->user = area;
1232 int target_alloc_working_area(struct target *target, uint32_t size, struct working_area **area)
1236 retval = target_alloc_working_area_try(target, size, area);
1237 if (retval == ERROR_TARGET_RESOURCE_NOT_AVAILABLE)
1239 LOG_WARNING("not enough working area available(requested %u)", (unsigned)(size));
1245 static int target_free_working_area_restore(struct target *target, struct working_area *area, int restore)
1250 if (restore && target->backup_working_area)
1253 if ((retval = target_write_memory(target, area->address, 4, area->size / 4, area->backup)) != ERROR_OK)
1259 /* mark user pointer invalid */
1266 int target_free_working_area(struct target *target, struct working_area *area)
1268 return target_free_working_area_restore(target, area, 1);
1271 /* free resources and restore memory, if restoring memory fails,
1272 * free up resources anyway
1274 static void target_free_all_working_areas_restore(struct target *target, int restore)
1276 struct working_area *c = target->working_areas;
1280 struct working_area *next = c->next;
1281 target_free_working_area_restore(target, c, restore);
1291 target->working_areas = NULL;
1294 void target_free_all_working_areas(struct target *target)
1296 target_free_all_working_areas_restore(target, 1);
1299 int target_arch_state(struct target *target)
1304 LOG_USER("No target has been configured");
1308 LOG_USER("target state: %s", target_state_name( target ));
1310 if (target->state != TARGET_HALTED)
1313 retval = target->type->arch_state(target);
1317 /* Single aligned words are guaranteed to use 16 or 32 bit access
1318 * mode respectively, otherwise data is handled as quickly as
1321 int target_write_buffer(struct target *target, uint32_t address, uint32_t size, uint8_t *buffer)
1324 LOG_DEBUG("writing buffer of %i byte at 0x%8.8x",
1325 (int)size, (unsigned)address);
1327 if (!target_was_examined(target))
1329 LOG_ERROR("Target not examined yet");
1337 if ((address + size - 1) < address)
1339 /* GDB can request this when e.g. PC is 0xfffffffc*/
1340 LOG_ERROR("address + size wrapped(0x%08x, 0x%08x)",
1346 if (((address % 2) == 0) && (size == 2))
1348 return target_write_memory(target, address, 2, 1, buffer);
1351 /* handle unaligned head bytes */
1354 uint32_t unaligned = 4 - (address % 4);
1356 if (unaligned > size)
1359 if ((retval = target_write_memory(target, address, 1, unaligned, buffer)) != ERROR_OK)
1362 buffer += unaligned;
1363 address += unaligned;
1367 /* handle aligned words */
1370 int aligned = size - (size % 4);
1372 /* use bulk writes above a certain limit. This may have to be changed */
1375 if ((retval = target->type->bulk_write_memory(target, address, aligned / 4, buffer)) != ERROR_OK)
1380 if ((retval = target_write_memory(target, address, 4, aligned / 4, buffer)) != ERROR_OK)
1389 /* handle tail writes of less than 4 bytes */
1392 if ((retval = target_write_memory(target, address, 1, size, buffer)) != ERROR_OK)
1399 /* Single aligned words are guaranteed to use 16 or 32 bit access
1400 * mode respectively, otherwise data is handled as quickly as
1403 int target_read_buffer(struct target *target, uint32_t address, uint32_t size, uint8_t *buffer)
1406 LOG_DEBUG("reading buffer of %i byte at 0x%8.8x",
1407 (int)size, (unsigned)address);
1409 if (!target_was_examined(target))
1411 LOG_ERROR("Target not examined yet");
1419 if ((address + size - 1) < address)
1421 /* GDB can request this when e.g. PC is 0xfffffffc*/
1422 LOG_ERROR("address + size wrapped(0x%08" PRIx32 ", 0x%08" PRIx32 ")",
1428 if (((address % 2) == 0) && (size == 2))
1430 return target_read_memory(target, address, 2, 1, buffer);
1433 /* handle unaligned head bytes */
1436 uint32_t unaligned = 4 - (address % 4);
1438 if (unaligned > size)
1441 if ((retval = target_read_memory(target, address, 1, unaligned, buffer)) != ERROR_OK)
1444 buffer += unaligned;
1445 address += unaligned;
1449 /* handle aligned words */
1452 int aligned = size - (size % 4);
1454 if ((retval = target_read_memory(target, address, 4, aligned / 4, buffer)) != ERROR_OK)
1462 /*prevent byte access when possible (avoid AHB access limitations in some cases)*/
1465 int aligned = size - (size%2);
1466 retval = target_read_memory(target, address, 2, aligned / 2, buffer);
1467 if (retval != ERROR_OK)
1474 /* handle tail writes of less than 4 bytes */
1477 if ((retval = target_read_memory(target, address, 1, size, buffer)) != ERROR_OK)
1484 int target_checksum_memory(struct target *target, uint32_t address, uint32_t size, uint32_t* crc)
1489 uint32_t checksum = 0;
1490 if (!target_was_examined(target))
1492 LOG_ERROR("Target not examined yet");
1496 if ((retval = target->type->checksum_memory(target, address,
1497 size, &checksum)) != ERROR_OK)
1499 buffer = malloc(size);
1502 LOG_ERROR("error allocating buffer for section (%d bytes)", (int)size);
1503 return ERROR_INVALID_ARGUMENTS;
1505 retval = target_read_buffer(target, address, size, buffer);
1506 if (retval != ERROR_OK)
1512 /* convert to target endianess */
1513 for (i = 0; i < (size/sizeof(uint32_t)); i++)
1515 uint32_t target_data;
1516 target_data = target_buffer_get_u32(target, &buffer[i*sizeof(uint32_t)]);
1517 target_buffer_set_u32(target, &buffer[i*sizeof(uint32_t)], target_data);
1520 retval = image_calculate_checksum(buffer, size, &checksum);
1529 int target_blank_check_memory(struct target *target, uint32_t address, uint32_t size, uint32_t* blank)
1532 if (!target_was_examined(target))
1534 LOG_ERROR("Target not examined yet");
1538 if (target->type->blank_check_memory == 0)
1539 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
1541 retval = target->type->blank_check_memory(target, address, size, blank);
1546 int target_read_u32(struct target *target, uint32_t address, uint32_t *value)
1548 uint8_t value_buf[4];
1549 if (!target_was_examined(target))
1551 LOG_ERROR("Target not examined yet");
1555 int retval = target_read_memory(target, address, 4, 1, value_buf);
1557 if (retval == ERROR_OK)
1559 *value = target_buffer_get_u32(target, value_buf);
1560 LOG_DEBUG("address: 0x%8.8" PRIx32 ", value: 0x%8.8" PRIx32 "",
1567 LOG_DEBUG("address: 0x%8.8" PRIx32 " failed",
1574 int target_read_u16(struct target *target, uint32_t address, uint16_t *value)
1576 uint8_t value_buf[2];
1577 if (!target_was_examined(target))
1579 LOG_ERROR("Target not examined yet");
1583 int retval = target_read_memory(target, address, 2, 1, value_buf);
1585 if (retval == ERROR_OK)
1587 *value = target_buffer_get_u16(target, value_buf);
1588 LOG_DEBUG("address: 0x%8.8" PRIx32 ", value: 0x%4.4x",
1595 LOG_DEBUG("address: 0x%8.8" PRIx32 " failed",
1602 int target_read_u8(struct target *target, uint32_t address, uint8_t *value)
1604 int retval = target_read_memory(target, address, 1, 1, value);
1605 if (!target_was_examined(target))
1607 LOG_ERROR("Target not examined yet");
1611 if (retval == ERROR_OK)
1613 LOG_DEBUG("address: 0x%8.8" PRIx32 ", value: 0x%2.2x",
1620 LOG_DEBUG("address: 0x%8.8" PRIx32 " failed",
1627 int target_write_u32(struct target *target, uint32_t address, uint32_t value)
1630 uint8_t value_buf[4];
1631 if (!target_was_examined(target))
1633 LOG_ERROR("Target not examined yet");
1637 LOG_DEBUG("address: 0x%8.8" PRIx32 ", value: 0x%8.8" PRIx32 "",
1641 target_buffer_set_u32(target, value_buf, value);
1642 if ((retval = target_write_memory(target, address, 4, 1, value_buf)) != ERROR_OK)
1644 LOG_DEBUG("failed: %i", retval);
1650 int target_write_u16(struct target *target, uint32_t address, uint16_t value)
1653 uint8_t value_buf[2];
1654 if (!target_was_examined(target))
1656 LOG_ERROR("Target not examined yet");
1660 LOG_DEBUG("address: 0x%8.8" PRIx32 ", value: 0x%8.8x",
1664 target_buffer_set_u16(target, value_buf, value);
1665 if ((retval = target_write_memory(target, address, 2, 1, value_buf)) != ERROR_OK)
1667 LOG_DEBUG("failed: %i", retval);
1673 int target_write_u8(struct target *target, uint32_t address, uint8_t value)
1676 if (!target_was_examined(target))
1678 LOG_ERROR("Target not examined yet");
1682 LOG_DEBUG("address: 0x%8.8" PRIx32 ", value: 0x%2.2x",
1685 if ((retval = target_write_memory(target, address, 1, 1, &value)) != ERROR_OK)
1687 LOG_DEBUG("failed: %i", retval);
1693 COMMAND_HANDLER(handle_targets_command)
1695 struct target *target = all_targets;
1699 target = get_target(CMD_ARGV[0]);
1700 if (target == NULL) {
1701 command_print(CMD_CTX,"Target: %s is unknown, try one of:\n", CMD_ARGV[0]);
1704 if (!target->tap->enabled) {
1705 command_print(CMD_CTX,"Target: TAP %s is disabled, "
1706 "can't be the current target\n",
1707 target->tap->dotted_name);
1711 CMD_CTX->current_target = target->target_number;
1716 target = all_targets;
1717 command_print(CMD_CTX, " TargetName Type Endian TapName State ");
1718 command_print(CMD_CTX, "-- ------------------ ---------- ------ ------------------ ------------");
1724 if (target->tap->enabled)
1725 state = target_state_name( target );
1727 state = "tap-disabled";
1729 if (CMD_CTX->current_target == target->target_number)
1732 /* keep columns lined up to match the headers above */
1733 command_print(CMD_CTX, "%2d%c %-18s %-10s %-6s %-18s %s",
1734 target->target_number,
1736 target_name(target),
1737 target_type_name(target),
1738 Jim_Nvp_value2name_simple(nvp_target_endian,
1739 target->endianness)->name,
1740 target->tap->dotted_name,
1742 target = target->next;
1748 /* every 300ms we check for reset & powerdropout and issue a "reset halt" if so. */
1750 static int powerDropout;
1751 static int srstAsserted;
1753 static int runPowerRestore;
1754 static int runPowerDropout;
1755 static int runSrstAsserted;
1756 static int runSrstDeasserted;
1758 static int sense_handler(void)
1760 static int prevSrstAsserted = 0;
1761 static int prevPowerdropout = 0;
1764 if ((retval = jtag_power_dropout(&powerDropout)) != ERROR_OK)
1768 powerRestored = prevPowerdropout && !powerDropout;
1771 runPowerRestore = 1;
1774 long long current = timeval_ms();
1775 static long long lastPower = 0;
1776 int waitMore = lastPower + 2000 > current;
1777 if (powerDropout && !waitMore)
1779 runPowerDropout = 1;
1780 lastPower = current;
1783 if ((retval = jtag_srst_asserted(&srstAsserted)) != ERROR_OK)
1787 srstDeasserted = prevSrstAsserted && !srstAsserted;
1789 static long long lastSrst = 0;
1790 waitMore = lastSrst + 2000 > current;
1791 if (srstDeasserted && !waitMore)
1793 runSrstDeasserted = 1;
1797 if (!prevSrstAsserted && srstAsserted)
1799 runSrstAsserted = 1;
1802 prevSrstAsserted = srstAsserted;
1803 prevPowerdropout = powerDropout;
1805 if (srstDeasserted || powerRestored)
1807 /* Other than logging the event we can't do anything here.
1808 * Issuing a reset is a particularly bad idea as we might
1809 * be inside a reset already.
1816 /* process target state changes */
1817 static int handle_target(void *priv)
1819 Jim_Interp *interp = (Jim_Interp *)priv;
1820 int retval = ERROR_OK;
1822 if (!is_jtag_poll_safe())
1824 /* polling is disabled currently */
1828 /* we do not want to recurse here... */
1829 static int recursive = 0;
1834 /* danger! running these procedures can trigger srst assertions and power dropouts.
1835 * We need to avoid an infinite loop/recursion here and we do that by
1836 * clearing the flags after running these events.
1838 int did_something = 0;
1839 if (runSrstAsserted)
1841 LOG_INFO("srst asserted detected, running srst_asserted proc.");
1842 Jim_Eval(interp, "srst_asserted");
1845 if (runSrstDeasserted)
1847 Jim_Eval(interp, "srst_deasserted");
1850 if (runPowerDropout)
1852 LOG_INFO("Power dropout detected, running power_dropout proc.");
1853 Jim_Eval(interp, "power_dropout");
1856 if (runPowerRestore)
1858 Jim_Eval(interp, "power_restore");
1864 /* clear detect flags */
1868 /* clear action flags */
1870 runSrstAsserted = 0;
1871 runSrstDeasserted = 0;
1872 runPowerRestore = 0;
1873 runPowerDropout = 0;
1878 /* Poll targets for state changes unless that's globally disabled.
1879 * Skip targets that are currently disabled.
1881 for (struct target *target = all_targets;
1882 is_jtag_poll_safe() && target;
1883 target = target->next)
1885 if (!target->tap->enabled)
1888 /* only poll target if we've got power and srst isn't asserted */
1889 if (!powerDropout && !srstAsserted)
1891 /* polling may fail silently until the target has been examined */
1892 if ((retval = target_poll(target)) != ERROR_OK)
1894 /* FIX!!!!! If we add a LOG_INFO() here to output a line in GDB
1895 * *why* we are aborting GDB, then we'll spam telnet when the
1896 * poll is failing persistently.
1898 * If we could implement an event that detected the
1899 * target going from non-pollable to pollable, we could issue
1900 * an error only upon the transition.
1902 target_call_event_callbacks(target, TARGET_EVENT_GDB_HALT);
1911 COMMAND_HANDLER(handle_reg_command)
1913 struct target *target;
1914 struct reg *reg = NULL;
1920 target = get_current_target(CMD_CTX);
1922 /* list all available registers for the current target */
1925 struct reg_cache *cache = target->reg_cache;
1932 command_print(CMD_CTX, "===== %s", cache->name);
1934 for (i = 0, reg = cache->reg_list;
1935 i < cache->num_regs;
1936 i++, reg++, count++)
1938 /* only print cached values if they are valid */
1940 value = buf_to_str(reg->value,
1942 command_print(CMD_CTX,
1943 "(%i) %s (/%" PRIu32 "): 0x%s%s",
1951 command_print(CMD_CTX, "(%i) %s (/%" PRIu32 ")",
1956 cache = cache->next;
1962 /* access a single register by its ordinal number */
1963 if ((CMD_ARGV[0][0] >= '0') && (CMD_ARGV[0][0] <= '9'))
1966 COMMAND_PARSE_NUMBER(uint, CMD_ARGV[0], num);
1968 struct reg_cache *cache = target->reg_cache;
1973 for (i = 0; i < cache->num_regs; i++)
1977 reg = &cache->reg_list[i];
1983 cache = cache->next;
1988 command_print(CMD_CTX, "%i is out of bounds, the current target has only %i registers (0 - %i)", num, count, count - 1);
1991 } else /* access a single register by its name */
1993 reg = register_get_by_name(target->reg_cache, CMD_ARGV[0], 1);
1997 command_print(CMD_CTX, "register %s not found in current target", CMD_ARGV[0]);
2002 /* display a register */
2003 if ((CMD_ARGC == 1) || ((CMD_ARGC == 2) && !((CMD_ARGV[1][0] >= '0') && (CMD_ARGV[1][0] <= '9'))))
2005 if ((CMD_ARGC == 2) && (strcmp(CMD_ARGV[1], "force") == 0))
2008 if (reg->valid == 0)
2010 reg->type->get(reg);
2012 value = buf_to_str(reg->value, reg->size, 16);
2013 command_print(CMD_CTX, "%s (/%i): 0x%s", reg->name, (int)(reg->size), value);
2018 /* set register value */
2021 uint8_t *buf = malloc(DIV_ROUND_UP(reg->size, 8));
2022 str_to_buf(CMD_ARGV[1], strlen(CMD_ARGV[1]), buf, reg->size, 0);
2024 reg->type->set(reg, buf);
2026 value = buf_to_str(reg->value, reg->size, 16);
2027 command_print(CMD_CTX, "%s (/%i): 0x%s", reg->name, (int)(reg->size), value);
2035 command_print(CMD_CTX, "usage: reg <#|name> [value]");
2040 COMMAND_HANDLER(handle_poll_command)
2042 int retval = ERROR_OK;
2043 struct target *target = get_current_target(CMD_CTX);
2047 command_print(CMD_CTX, "background polling: %s",
2048 jtag_poll_get_enabled() ? "on" : "off");
2049 command_print(CMD_CTX, "TAP: %s (%s)",
2050 target->tap->dotted_name,
2051 target->tap->enabled ? "enabled" : "disabled");
2052 if (!target->tap->enabled)
2054 if ((retval = target_poll(target)) != ERROR_OK)
2056 if ((retval = target_arch_state(target)) != ERROR_OK)
2059 else if (CMD_ARGC == 1)
2062 COMMAND_PARSE_ON_OFF(CMD_ARGV[0], enable);
2063 jtag_poll_set_enabled(enable);
2067 return ERROR_COMMAND_SYNTAX_ERROR;
2073 COMMAND_HANDLER(handle_wait_halt_command)
2076 return ERROR_COMMAND_SYNTAX_ERROR;
2081 int retval = parse_uint(CMD_ARGV[0], &ms);
2082 if (ERROR_OK != retval)
2084 command_print(CMD_CTX, "usage: %s [seconds]", CMD_NAME);
2085 return ERROR_COMMAND_SYNTAX_ERROR;
2087 // convert seconds (given) to milliseconds (needed)
2091 struct target *target = get_current_target(CMD_CTX);
2092 return target_wait_state(target, TARGET_HALTED, ms);
2095 /* wait for target state to change. The trick here is to have a low
2096 * latency for short waits and not to suck up all the CPU time
2099 * After 500ms, keep_alive() is invoked
2101 int target_wait_state(struct target *target, enum target_state state, int ms)
2104 long long then = 0, cur;
2109 if ((retval = target_poll(target)) != ERROR_OK)
2111 if (target->state == state)
2119 then = timeval_ms();
2120 LOG_DEBUG("waiting for target %s...",
2121 Jim_Nvp_value2name_simple(nvp_target_state,state)->name);
2129 if ((cur-then) > ms)
2131 LOG_ERROR("timed out while waiting for target %s",
2132 Jim_Nvp_value2name_simple(nvp_target_state,state)->name);
2140 COMMAND_HANDLER(handle_halt_command)
2144 struct target *target = get_current_target(CMD_CTX);
2145 int retval = target_halt(target);
2146 if (ERROR_OK != retval)
2152 retval = parse_uint(CMD_ARGV[0], &wait);
2153 if (ERROR_OK != retval)
2154 return ERROR_COMMAND_SYNTAX_ERROR;
2159 return CALL_COMMAND_HANDLER(handle_wait_halt_command);
2162 COMMAND_HANDLER(handle_soft_reset_halt_command)
2164 struct target *target = get_current_target(CMD_CTX);
2166 LOG_USER("requesting target halt and executing a soft reset");
2168 target->type->soft_reset_halt(target);
2173 COMMAND_HANDLER(handle_reset_command)
2176 return ERROR_COMMAND_SYNTAX_ERROR;
2178 enum target_reset_mode reset_mode = RESET_RUN;
2182 n = Jim_Nvp_name2value_simple(nvp_reset_modes, CMD_ARGV[0]);
2183 if ((n->name == NULL) || (n->value == RESET_UNKNOWN)) {
2184 return ERROR_COMMAND_SYNTAX_ERROR;
2186 reset_mode = n->value;
2189 /* reset *all* targets */
2190 return target_process_reset(CMD_CTX, reset_mode);
2194 COMMAND_HANDLER(handle_resume_command)
2198 return ERROR_COMMAND_SYNTAX_ERROR;
2200 struct target *target = get_current_target(CMD_CTX);
2201 target_handle_event(target, TARGET_EVENT_OLD_pre_resume);
2203 /* with no CMD_ARGV, resume from current pc, addr = 0,
2204 * with one arguments, addr = CMD_ARGV[0],
2205 * handle breakpoints, not debugging */
2209 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], addr);
2213 return target_resume(target, current, addr, 1, 0);
2216 COMMAND_HANDLER(handle_step_command)
2219 return ERROR_COMMAND_SYNTAX_ERROR;
2223 /* with no CMD_ARGV, step from current pc, addr = 0,
2224 * with one argument addr = CMD_ARGV[0],
2225 * handle breakpoints, debugging */
2230 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], addr);
2234 struct target *target = get_current_target(CMD_CTX);
2236 return target->type->step(target, current_pc, addr, 1);
2239 static void handle_md_output(struct command_context *cmd_ctx,
2240 struct target *target, uint32_t address, unsigned size,
2241 unsigned count, const uint8_t *buffer)
2243 const unsigned line_bytecnt = 32;
2244 unsigned line_modulo = line_bytecnt / size;
2246 char output[line_bytecnt * 4 + 1];
2247 unsigned output_len = 0;
2249 const char *value_fmt;
2251 case 4: value_fmt = "%8.8x "; break;
2252 case 2: value_fmt = "%4.4x "; break;
2253 case 1: value_fmt = "%2.2x "; break;
2255 /* "can't happen", caller checked */
2256 LOG_ERROR("invalid memory read size: %u", size);
2260 for (unsigned i = 0; i < count; i++)
2262 if (i % line_modulo == 0)
2264 output_len += snprintf(output + output_len,
2265 sizeof(output) - output_len,
2267 (unsigned)(address + (i*size)));
2271 const uint8_t *value_ptr = buffer + i * size;
2273 case 4: value = target_buffer_get_u32(target, value_ptr); break;
2274 case 2: value = target_buffer_get_u16(target, value_ptr); break;
2275 case 1: value = *value_ptr;
2277 output_len += snprintf(output + output_len,
2278 sizeof(output) - output_len,
2281 if ((i % line_modulo == line_modulo - 1) || (i == count - 1))
2283 command_print(cmd_ctx, "%s", output);
2289 COMMAND_HANDLER(handle_md_command)
2292 return ERROR_COMMAND_SYNTAX_ERROR;
2295 switch (CMD_NAME[2]) {
2296 case 'w': size = 4; break;
2297 case 'h': size = 2; break;
2298 case 'b': size = 1; break;
2299 default: return ERROR_COMMAND_SYNTAX_ERROR;
2302 bool physical=strcmp(CMD_ARGV[0], "phys")==0;
2303 int (*fn)(struct target *target,
2304 uint32_t address, uint32_t size, uint32_t count, uint8_t *buffer);
2309 fn=target_read_phys_memory;
2312 fn=target_read_memory;
2314 if ((CMD_ARGC < 1) || (CMD_ARGC > 2))
2316 return ERROR_COMMAND_SYNTAX_ERROR;
2320 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], address);
2324 COMMAND_PARSE_NUMBER(uint, CMD_ARGV[1], count);
2326 uint8_t *buffer = calloc(count, size);
2328 struct target *target = get_current_target(CMD_CTX);
2329 int retval = fn(target, address, size, count, buffer);
2330 if (ERROR_OK == retval)
2331 handle_md_output(CMD_CTX, target, address, size, count, buffer);
2338 typedef int (*target_write_fn)(struct target *target,
2339 uint32_t address, uint32_t size, uint32_t count, uint8_t *buffer);
2341 static int target_write_memory_fast(struct target *target,
2342 uint32_t address, uint32_t size, uint32_t count, uint8_t *buffer)
2344 return target_write_buffer(target, address, size * count, buffer);
2347 static int target_fill_mem(struct target *target,
2356 /* We have to write in reasonably large chunks to be able
2357 * to fill large memory areas with any sane speed */
2358 const unsigned chunk_size = 16384;
2359 uint8_t *target_buf = malloc(chunk_size * data_size);
2360 if (target_buf == NULL)
2362 LOG_ERROR("Out of memory");
2366 for (unsigned i = 0; i < chunk_size; i ++)
2371 target_buffer_set_u32(target, target_buf + i*data_size, b);
2374 target_buffer_set_u16(target, target_buf + i*data_size, b);
2377 target_buffer_set_u8(target, target_buf + i*data_size, b);
2384 int retval = ERROR_OK;
2386 for (unsigned x = 0; x < c; x += chunk_size)
2390 if (current > chunk_size)
2392 current = chunk_size;
2394 int retval = fn(target, address + x * data_size, data_size, current, target_buf);
2395 if (retval != ERROR_OK)
2399 /* avoid GDB timeouts */
2408 COMMAND_HANDLER(handle_mw_command)
2412 return ERROR_COMMAND_SYNTAX_ERROR;
2414 bool physical=strcmp(CMD_ARGV[0], "phys")==0;
2420 fn=target_write_phys_memory;
2423 fn = target_write_memory_fast;
2425 if ((CMD_ARGC < 2) || (CMD_ARGC > 3))
2426 return ERROR_COMMAND_SYNTAX_ERROR;
2429 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], address);
2432 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], value);
2436 COMMAND_PARSE_NUMBER(uint, CMD_ARGV[2], count);
2438 struct target *target = get_current_target(CMD_CTX);
2440 switch (CMD_NAME[2])
2452 return ERROR_COMMAND_SYNTAX_ERROR;
2455 return target_fill_mem(target, address, fn, wordsize, value, count);
2458 static COMMAND_HELPER(parse_load_image_command_CMD_ARGV, struct image *image,
2459 uint32_t *min_address, uint32_t *max_address)
2461 if (CMD_ARGC < 1 || CMD_ARGC > 5)
2462 return ERROR_COMMAND_SYNTAX_ERROR;
2464 /* a base address isn't always necessary,
2465 * default to 0x0 (i.e. don't relocate) */
2469 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], addr);
2470 image->base_address = addr;
2471 image->base_address_set = 1;
2474 image->base_address_set = 0;
2476 image->start_address_set = 0;
2480 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[3], *min_address);
2484 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[4], *max_address);
2485 // use size (given) to find max (required)
2486 *max_address += *min_address;
2489 if (*min_address > *max_address)
2490 return ERROR_COMMAND_SYNTAX_ERROR;
2495 COMMAND_HANDLER(handle_load_image_command)
2499 uint32_t image_size;
2500 uint32_t min_address = 0;
2501 uint32_t max_address = 0xffffffff;
2505 int retval = CALL_COMMAND_HANDLER(parse_load_image_command_CMD_ARGV,
2506 &image, &min_address, &max_address);
2507 if (ERROR_OK != retval)
2510 struct target *target = get_current_target(CMD_CTX);
2512 struct duration bench;
2513 duration_start(&bench);
2515 if (image_open(&image, CMD_ARGV[0], (CMD_ARGC >= 3) ? CMD_ARGV[2] : NULL) != ERROR_OK)
2522 for (i = 0; i < image.num_sections; i++)
2524 buffer = malloc(image.sections[i].size);
2527 command_print(CMD_CTX,
2528 "error allocating buffer for section (%d bytes)",
2529 (int)(image.sections[i].size));
2533 if ((retval = image_read_section(&image, i, 0x0, image.sections[i].size, buffer, &buf_cnt)) != ERROR_OK)
2539 uint32_t offset = 0;
2540 uint32_t length = buf_cnt;
2542 /* DANGER!!! beware of unsigned comparision here!!! */
2544 if ((image.sections[i].base_address + buf_cnt >= min_address)&&
2545 (image.sections[i].base_address < max_address))
2547 if (image.sections[i].base_address < min_address)
2549 /* clip addresses below */
2550 offset += min_address-image.sections[i].base_address;
2554 if (image.sections[i].base_address + buf_cnt > max_address)
2556 length -= (image.sections[i].base_address + buf_cnt)-max_address;
2559 if ((retval = target_write_buffer(target, image.sections[i].base_address + offset, length, buffer + offset)) != ERROR_OK)
2564 image_size += length;
2565 command_print(CMD_CTX, "%u bytes written at address 0x%8.8" PRIx32 "",
2566 (unsigned int)length,
2567 image.sections[i].base_address + offset);
2573 if ((ERROR_OK == retval) && (duration_measure(&bench) == ERROR_OK))
2575 command_print(CMD_CTX, "downloaded %" PRIu32 " bytes "
2576 "in %fs (%0.3f kb/s)", image_size,
2577 duration_elapsed(&bench), duration_kbps(&bench, image_size));
2580 image_close(&image);
2586 COMMAND_HANDLER(handle_dump_image_command)
2588 struct fileio fileio;
2590 uint8_t buffer[560];
2594 struct target *target = get_current_target(CMD_CTX);
2598 command_print(CMD_CTX, "usage: dump_image <filename> <address> <size>");
2603 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], address);
2605 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[2], size);
2607 if (fileio_open(&fileio, CMD_ARGV[0], FILEIO_WRITE, FILEIO_BINARY) != ERROR_OK)
2612 struct duration bench;
2613 duration_start(&bench);
2615 int retval = ERROR_OK;
2618 size_t size_written;
2619 uint32_t this_run_size = (size > 560) ? 560 : size;
2620 retval = target_read_buffer(target, address, this_run_size, buffer);
2621 if (retval != ERROR_OK)
2626 retval = fileio_write(&fileio, this_run_size, buffer, &size_written);
2627 if (retval != ERROR_OK)
2632 size -= this_run_size;
2633 address += this_run_size;
2636 if ((retvaltemp = fileio_close(&fileio)) != ERROR_OK)
2639 if ((ERROR_OK == retval) && (duration_measure(&bench) == ERROR_OK))
2641 command_print(CMD_CTX,
2642 "dumped %ld bytes in %fs (%0.3f kb/s)", (long)fileio.size,
2643 duration_elapsed(&bench), duration_kbps(&bench, fileio.size));
2649 static COMMAND_HELPER(handle_verify_image_command_internal, int verify)
2653 uint32_t image_size;
2656 uint32_t checksum = 0;
2657 uint32_t mem_checksum = 0;
2661 struct target *target = get_current_target(CMD_CTX);
2665 return ERROR_COMMAND_SYNTAX_ERROR;
2670 LOG_ERROR("no target selected");
2674 struct duration bench;
2675 duration_start(&bench);
2680 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], addr);
2681 image.base_address = addr;
2682 image.base_address_set = 1;
2686 image.base_address_set = 0;
2687 image.base_address = 0x0;
2690 image.start_address_set = 0;
2692 if ((retval = image_open(&image, CMD_ARGV[0], (CMD_ARGC == 3) ? CMD_ARGV[2] : NULL)) != ERROR_OK)
2699 for (i = 0; i < image.num_sections; i++)
2701 buffer = malloc(image.sections[i].size);
2704 command_print(CMD_CTX,
2705 "error allocating buffer for section (%d bytes)",
2706 (int)(image.sections[i].size));
2709 if ((retval = image_read_section(&image, i, 0x0, image.sections[i].size, buffer, &buf_cnt)) != ERROR_OK)
2717 /* calculate checksum of image */
2718 image_calculate_checksum(buffer, buf_cnt, &checksum);
2720 retval = target_checksum_memory(target, image.sections[i].base_address, buf_cnt, &mem_checksum);
2721 if (retval != ERROR_OK)
2727 if (checksum != mem_checksum)
2729 /* failed crc checksum, fall back to a binary compare */
2732 command_print(CMD_CTX, "checksum mismatch - attempting binary compare");
2734 data = (uint8_t*)malloc(buf_cnt);
2736 /* Can we use 32bit word accesses? */
2738 int count = buf_cnt;
2739 if ((count % 4) == 0)
2744 retval = target_read_memory(target, image.sections[i].base_address, size, count, data);
2745 if (retval == ERROR_OK)
2748 for (t = 0; t < buf_cnt; t++)
2750 if (data[t] != buffer[t])
2752 command_print(CMD_CTX,
2753 "Verify operation failed address 0x%08x. Was 0x%02x instead of 0x%02x\n",
2754 (unsigned)(t + image.sections[i].base_address),
2759 retval = ERROR_FAIL;
2773 command_print(CMD_CTX, "address 0x%08" PRIx32 " length 0x%08zx",
2774 image.sections[i].base_address,
2779 image_size += buf_cnt;
2782 if ((ERROR_OK == retval) && (duration_measure(&bench) == ERROR_OK))
2784 command_print(CMD_CTX, "verified %" PRIu32 " bytes "
2785 "in %fs (%0.3f kb/s)", image_size,
2786 duration_elapsed(&bench), duration_kbps(&bench, image_size));
2789 image_close(&image);
2794 COMMAND_HANDLER(handle_verify_image_command)
2796 return CALL_COMMAND_HANDLER(handle_verify_image_command_internal, 1);
2799 COMMAND_HANDLER(handle_test_image_command)
2801 return CALL_COMMAND_HANDLER(handle_verify_image_command_internal, 0);
2804 static int handle_bp_command_list(struct command_context *cmd_ctx)
2806 struct target *target = get_current_target(cmd_ctx);
2807 struct breakpoint *breakpoint = target->breakpoints;
2810 if (breakpoint->type == BKPT_SOFT)
2812 char* buf = buf_to_str(breakpoint->orig_instr,
2813 breakpoint->length, 16);
2814 command_print(cmd_ctx, "0x%8.8" PRIx32 ", 0x%x, %i, 0x%s",
2815 breakpoint->address,
2817 breakpoint->set, buf);
2822 command_print(cmd_ctx, "0x%8.8" PRIx32 ", 0x%x, %i",
2823 breakpoint->address,
2824 breakpoint->length, breakpoint->set);
2827 breakpoint = breakpoint->next;
2832 static int handle_bp_command_set(struct command_context *cmd_ctx,
2833 uint32_t addr, uint32_t length, int hw)
2835 struct target *target = get_current_target(cmd_ctx);
2836 int retval = breakpoint_add(target, addr, length, hw);
2837 if (ERROR_OK == retval)
2838 command_print(cmd_ctx, "breakpoint set at 0x%8.8" PRIx32 "", addr);
2840 LOG_ERROR("Failure setting breakpoint");
2844 COMMAND_HANDLER(handle_bp_command)
2847 return handle_bp_command_list(CMD_CTX);
2849 if (CMD_ARGC < 2 || CMD_ARGC > 3)
2851 command_print(CMD_CTX, "usage: bp <address> <length> ['hw']");
2852 return ERROR_COMMAND_SYNTAX_ERROR;
2856 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], addr);
2858 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], length);
2863 if (strcmp(CMD_ARGV[2], "hw") == 0)
2866 return ERROR_COMMAND_SYNTAX_ERROR;
2869 return handle_bp_command_set(CMD_CTX, addr, length, hw);
2872 COMMAND_HANDLER(handle_rbp_command)
2875 return ERROR_COMMAND_SYNTAX_ERROR;
2878 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], addr);
2880 struct target *target = get_current_target(CMD_CTX);
2881 breakpoint_remove(target, addr);
2886 COMMAND_HANDLER(handle_wp_command)
2888 struct target *target = get_current_target(CMD_CTX);
2892 struct watchpoint *watchpoint = target->watchpoints;
2896 command_print(CMD_CTX, "address: 0x%8.8" PRIx32
2897 ", len: 0x%8.8" PRIx32
2898 ", r/w/a: %i, value: 0x%8.8" PRIx32
2899 ", mask: 0x%8.8" PRIx32,
2900 watchpoint->address,
2902 (int)watchpoint->rw,
2905 watchpoint = watchpoint->next;
2910 enum watchpoint_rw type = WPT_ACCESS;
2912 uint32_t length = 0;
2913 uint32_t data_value = 0x0;
2914 uint32_t data_mask = 0xffffffff;
2919 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[4], data_mask);
2922 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[3], data_value);
2925 switch (CMD_ARGV[2][0])
2937 LOG_ERROR("invalid watchpoint mode ('%c')", CMD_ARGV[2][0]);
2938 return ERROR_COMMAND_SYNTAX_ERROR;
2942 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], length);
2943 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], addr);
2947 command_print(CMD_CTX, "usage: wp [address length "
2948 "[(r|w|a) [value [mask]]]]");
2949 return ERROR_COMMAND_SYNTAX_ERROR;
2952 int retval = watchpoint_add(target, addr, length, type,
2953 data_value, data_mask);
2954 if (ERROR_OK != retval)
2955 LOG_ERROR("Failure setting watchpoints");
2960 COMMAND_HANDLER(handle_rwp_command)
2963 return ERROR_COMMAND_SYNTAX_ERROR;
2966 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], addr);
2968 struct target *target = get_current_target(CMD_CTX);
2969 watchpoint_remove(target, addr);
2976 * Translate a virtual address to a physical address.
2978 * The low-level target implementation must have logged a detailed error
2979 * which is forwarded to telnet/GDB session.
2981 COMMAND_HANDLER(handle_virt2phys_command)
2984 return ERROR_COMMAND_SYNTAX_ERROR;
2987 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], va);
2990 struct target *target = get_current_target(CMD_CTX);
2991 int retval = target->type->virt2phys(target, va, &pa);
2992 if (retval == ERROR_OK)
2993 command_print(CMD_CTX, "Physical address 0x%08" PRIx32 "", pa);
2998 static void writeData(FILE *f, const void *data, size_t len)
3000 size_t written = fwrite(data, 1, len, f);
3002 LOG_ERROR("failed to write %zu bytes: %s", len, strerror(errno));
3005 static void writeLong(FILE *f, int l)
3008 for (i = 0; i < 4; i++)
3010 char c = (l >> (i*8))&0xff;
3011 writeData(f, &c, 1);
3016 static void writeString(FILE *f, char *s)
3018 writeData(f, s, strlen(s));
3021 /* Dump a gmon.out histogram file. */
3022 static void writeGmon(uint32_t *samples, uint32_t sampleNum, const char *filename)
3025 FILE *f = fopen(filename, "w");
3028 writeString(f, "gmon");
3029 writeLong(f, 0x00000001); /* Version */
3030 writeLong(f, 0); /* padding */
3031 writeLong(f, 0); /* padding */
3032 writeLong(f, 0); /* padding */
3034 uint8_t zero = 0; /* GMON_TAG_TIME_HIST */
3035 writeData(f, &zero, 1);
3037 /* figure out bucket size */
3038 uint32_t min = samples[0];
3039 uint32_t max = samples[0];
3040 for (i = 0; i < sampleNum; i++)
3042 if (min > samples[i])
3046 if (max < samples[i])
3052 int addressSpace = (max-min + 1);
3054 static const uint32_t maxBuckets = 256 * 1024; /* maximum buckets. */
3055 uint32_t length = addressSpace;
3056 if (length > maxBuckets)
3058 length = maxBuckets;
3060 int *buckets = malloc(sizeof(int)*length);
3061 if (buckets == NULL)
3066 memset(buckets, 0, sizeof(int)*length);
3067 for (i = 0; i < sampleNum;i++)
3069 uint32_t address = samples[i];
3070 long long a = address-min;
3071 long long b = length-1;
3072 long long c = addressSpace-1;
3073 int index = (a*b)/c; /* danger!!!! int32 overflows */
3077 /* append binary memory gmon.out &profile_hist_hdr ((char*)&profile_hist_hdr + sizeof(struct gmon_hist_hdr)) */
3078 writeLong(f, min); /* low_pc */
3079 writeLong(f, max); /* high_pc */
3080 writeLong(f, length); /* # of samples */
3081 writeLong(f, 64000000); /* 64MHz */
3082 writeString(f, "seconds");
3083 for (i = 0; i < (15-strlen("seconds")); i++)
3084 writeData(f, &zero, 1);
3085 writeString(f, "s");
3087 /*append binary memory gmon.out profile_hist_data (profile_hist_data + profile_hist_hdr.hist_size) */
3089 char *data = malloc(2*length);
3092 for (i = 0; i < length;i++)
3101 data[i*2 + 1]=(val >> 8)&0xff;
3104 writeData(f, data, length * 2);
3114 /* profiling samples the CPU PC as quickly as OpenOCD is able,
3115 * which will be used as a random sampling of PC */
3116 COMMAND_HANDLER(handle_profile_command)
3118 struct target *target = get_current_target(CMD_CTX);
3119 struct timeval timeout, now;
3121 gettimeofday(&timeout, NULL);
3124 return ERROR_COMMAND_SYNTAX_ERROR;
3127 COMMAND_PARSE_NUMBER(uint, CMD_ARGV[0], offset);
3129 timeval_add_time(&timeout, offset, 0);
3132 * @todo: Some cores let us sample the PC without the
3133 * annoying halt/resume step; for example, ARMv7 PCSR.
3134 * Provide a way to use that more efficient mechanism.
3137 command_print(CMD_CTX, "Starting profiling. Halting and resuming the target as often as we can...");
3139 static const int maxSample = 10000;
3140 uint32_t *samples = malloc(sizeof(uint32_t)*maxSample);
3141 if (samples == NULL)
3145 /* hopefully it is safe to cache! We want to stop/restart as quickly as possible. */
3146 struct reg *reg = register_get_by_name(target->reg_cache, "pc", 1);
3151 target_poll(target);
3152 if (target->state == TARGET_HALTED)
3154 uint32_t t=*((uint32_t *)reg->value);
3155 samples[numSamples++]=t;
3156 retval = target_resume(target, 1, 0, 0, 0); /* current pc, addr = 0, do not handle breakpoints, not debugging */
3157 target_poll(target);
3158 alive_sleep(10); /* sleep 10ms, i.e. <100 samples/second. */
3159 } else if (target->state == TARGET_RUNNING)
3161 /* We want to quickly sample the PC. */
3162 if ((retval = target_halt(target)) != ERROR_OK)
3169 command_print(CMD_CTX, "Target not halted or running");
3173 if (retval != ERROR_OK)
3178 gettimeofday(&now, NULL);
3179 if ((numSamples >= maxSample) || ((now.tv_sec >= timeout.tv_sec) && (now.tv_usec >= timeout.tv_usec)))
3181 command_print(CMD_CTX, "Profiling completed. %d samples.", numSamples);
3182 if ((retval = target_poll(target)) != ERROR_OK)
3187 if (target->state == TARGET_HALTED)
3189 target_resume(target, 1, 0, 0, 0); /* current pc, addr = 0, do not handle breakpoints, not debugging */
3191 if ((retval = target_poll(target)) != ERROR_OK)
3196 writeGmon(samples, numSamples, CMD_ARGV[1]);
3197 command_print(CMD_CTX, "Wrote %s", CMD_ARGV[1]);
3206 static int new_int_array_element(Jim_Interp * interp, const char *varname, int idx, uint32_t val)
3209 Jim_Obj *nameObjPtr, *valObjPtr;
3212 namebuf = alloc_printf("%s(%d)", varname, idx);
3216 nameObjPtr = Jim_NewStringObj(interp, namebuf, -1);
3217 valObjPtr = Jim_NewIntObj(interp, val);
3218 if (!nameObjPtr || !valObjPtr)
3224 Jim_IncrRefCount(nameObjPtr);
3225 Jim_IncrRefCount(valObjPtr);
3226 result = Jim_SetVariable(interp, nameObjPtr, valObjPtr);
3227 Jim_DecrRefCount(interp, nameObjPtr);
3228 Jim_DecrRefCount(interp, valObjPtr);
3230 /* printf("%s(%d) <= 0%08x\n", varname, idx, val); */
3234 static int jim_mem2array(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
3236 struct command_context *context;
3237 struct target *target;
3239 context = Jim_GetAssocData(interp, "context");
3240 if (context == NULL)
3242 LOG_ERROR("mem2array: no command context");
3245 target = get_current_target(context);
3248 LOG_ERROR("mem2array: no current target");
3252 return target_mem2array(interp, target, argc-1, argv + 1);
3255 static int target_mem2array(Jim_Interp *interp, struct target *target, int argc, Jim_Obj *const *argv)
3263 const char *varname;
3267 /* argv[1] = name of array to receive the data
3268 * argv[2] = desired width
3269 * argv[3] = memory address
3270 * argv[4] = count of times to read
3273 Jim_WrongNumArgs(interp, 1, argv, "varname width addr nelems");
3276 varname = Jim_GetString(argv[0], &len);
3277 /* given "foo" get space for worse case "foo(%d)" .. add 20 */
3279 e = Jim_GetLong(interp, argv[1], &l);
3285 e = Jim_GetLong(interp, argv[2], &l);
3290 e = Jim_GetLong(interp, argv[3], &l);
3306 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3307 Jim_AppendStrings(interp, Jim_GetResult(interp), "Invalid width param, must be 8/16/32", NULL);
3311 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3312 Jim_AppendStrings(interp, Jim_GetResult(interp), "mem2array: zero width read?", NULL);
3315 if ((addr + (len * width)) < addr) {
3316 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3317 Jim_AppendStrings(interp, Jim_GetResult(interp), "mem2array: addr + len - wraps to zero?", NULL);
3320 /* absurd transfer size? */
3322 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3323 Jim_AppendStrings(interp, Jim_GetResult(interp), "mem2array: absurd > 64K item request", NULL);
3328 ((width == 2) && ((addr & 1) == 0)) ||
3329 ((width == 4) && ((addr & 3) == 0))) {
3333 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3334 sprintf(buf, "mem2array address: 0x%08" PRIx32 " is not aligned for %" PRId32 " byte reads",
3337 Jim_AppendStrings(interp, Jim_GetResult(interp), buf , NULL);
3346 size_t buffersize = 4096;
3347 uint8_t *buffer = malloc(buffersize);
3354 /* Slurp... in buffer size chunks */
3356 count = len; /* in objects.. */
3357 if (count > (buffersize/width)) {
3358 count = (buffersize/width);
3361 retval = target_read_memory(target, addr, width, count, buffer);
3362 if (retval != ERROR_OK) {
3364 LOG_ERROR("mem2array: Read @ 0x%08x, w=%d, cnt=%d, failed",
3368 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3369 Jim_AppendStrings(interp, Jim_GetResult(interp), "mem2array: cannot read memory", NULL);
3373 v = 0; /* shut up gcc */
3374 for (i = 0 ;i < count ;i++, n++) {
3377 v = target_buffer_get_u32(target, &buffer[i*width]);
3380 v = target_buffer_get_u16(target, &buffer[i*width]);
3383 v = buffer[i] & 0x0ff;
3386 new_int_array_element(interp, varname, n, v);
3394 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3399 static int get_int_array_element(Jim_Interp * interp, const char *varname, int idx, uint32_t *val)
3402 Jim_Obj *nameObjPtr, *valObjPtr;
3406 namebuf = alloc_printf("%s(%d)", varname, idx);
3410 nameObjPtr = Jim_NewStringObj(interp, namebuf, -1);
3417 Jim_IncrRefCount(nameObjPtr);
3418 valObjPtr = Jim_GetVariable(interp, nameObjPtr, JIM_ERRMSG);
3419 Jim_DecrRefCount(interp, nameObjPtr);
3421 if (valObjPtr == NULL)
3424 result = Jim_GetLong(interp, valObjPtr, &l);
3425 /* printf("%s(%d) => 0%08x\n", varname, idx, val); */
3430 static int jim_array2mem(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
3432 struct command_context *context;
3433 struct target *target;
3435 context = Jim_GetAssocData(interp, "context");
3436 if (context == NULL) {
3437 LOG_ERROR("array2mem: no command context");
3440 target = get_current_target(context);
3441 if (target == NULL) {
3442 LOG_ERROR("array2mem: no current target");
3446 return target_array2mem(interp,target, argc-1, argv + 1);
3449 static int target_array2mem(Jim_Interp *interp, struct target *target,
3450 int argc, Jim_Obj *const *argv)
3458 const char *varname;
3462 /* argv[1] = name of array to get the data
3463 * argv[2] = desired width
3464 * argv[3] = memory address
3465 * argv[4] = count to write
3468 Jim_WrongNumArgs(interp, 0, argv, "varname width addr nelems");
3471 varname = Jim_GetString(argv[0], &len);
3472 /* given "foo" get space for worse case "foo(%d)" .. add 20 */
3474 e = Jim_GetLong(interp, argv[1], &l);
3480 e = Jim_GetLong(interp, argv[2], &l);
3485 e = Jim_GetLong(interp, argv[3], &l);
3501 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3502 Jim_AppendStrings(interp, Jim_GetResult(interp), "Invalid width param, must be 8/16/32", NULL);
3506 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3507 Jim_AppendStrings(interp, Jim_GetResult(interp), "array2mem: zero width read?", NULL);
3510 if ((addr + (len * width)) < addr) {
3511 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3512 Jim_AppendStrings(interp, Jim_GetResult(interp), "array2mem: addr + len - wraps to zero?", NULL);
3515 /* absurd transfer size? */
3517 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3518 Jim_AppendStrings(interp, Jim_GetResult(interp), "array2mem: absurd > 64K item request", NULL);
3523 ((width == 2) && ((addr & 1) == 0)) ||
3524 ((width == 4) && ((addr & 3) == 0))) {
3528 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3529 sprintf(buf, "array2mem address: 0x%08x is not aligned for %d byte reads",
3532 Jim_AppendStrings(interp, Jim_GetResult(interp), buf , NULL);
3543 size_t buffersize = 4096;
3544 uint8_t *buffer = malloc(buffersize);
3549 /* Slurp... in buffer size chunks */
3551 count = len; /* in objects.. */
3552 if (count > (buffersize/width)) {
3553 count = (buffersize/width);
3556 v = 0; /* shut up gcc */
3557 for (i = 0 ;i < count ;i++, n++) {
3558 get_int_array_element(interp, varname, n, &v);
3561 target_buffer_set_u32(target, &buffer[i*width], v);
3564 target_buffer_set_u16(target, &buffer[i*width], v);
3567 buffer[i] = v & 0x0ff;
3573 retval = target_write_memory(target, addr, width, count, buffer);
3574 if (retval != ERROR_OK) {
3576 LOG_ERROR("array2mem: Write @ 0x%08x, w=%d, cnt=%d, failed",
3580 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3581 Jim_AppendStrings(interp, Jim_GetResult(interp), "array2mem: cannot read memory", NULL);
3589 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3594 /* FIX? should we propagate errors here rather than printing them
3597 void target_handle_event(struct target *target, enum target_event e)
3599 struct target_event_action *teap;
3601 for (teap = target->event_action; teap != NULL; teap = teap->next) {
3602 if (teap->event == e) {
3603 LOG_DEBUG("target: (%d) %s (%s) event: %d (%s) action: %s",
3604 target->target_number,
3605 target_name(target),
3606 target_type_name(target),
3608 Jim_Nvp_value2name_simple(nvp_target_event, e)->name,
3609 Jim_GetString(teap->body, NULL));
3610 if (Jim_EvalObj(teap->interp, teap->body) != JIM_OK)
3612 Jim_PrintErrorMessage(teap->interp);
3619 * Returns true only if the target has a handler for the specified event.
3621 bool target_has_event_action(struct target *target, enum target_event event)
3623 struct target_event_action *teap;
3625 for (teap = target->event_action; teap != NULL; teap = teap->next) {
3626 if (teap->event == event)
3632 enum target_cfg_param {
3635 TCFG_WORK_AREA_VIRT,
3636 TCFG_WORK_AREA_PHYS,
3637 TCFG_WORK_AREA_SIZE,
3638 TCFG_WORK_AREA_BACKUP,
3641 TCFG_CHAIN_POSITION,
3644 static Jim_Nvp nvp_config_opts[] = {
3645 { .name = "-type", .value = TCFG_TYPE },
3646 { .name = "-event", .value = TCFG_EVENT },
3647 { .name = "-work-area-virt", .value = TCFG_WORK_AREA_VIRT },
3648 { .name = "-work-area-phys", .value = TCFG_WORK_AREA_PHYS },
3649 { .name = "-work-area-size", .value = TCFG_WORK_AREA_SIZE },
3650 { .name = "-work-area-backup", .value = TCFG_WORK_AREA_BACKUP },
3651 { .name = "-endian" , .value = TCFG_ENDIAN },
3652 { .name = "-variant", .value = TCFG_VARIANT },
3653 { .name = "-chain-position", .value = TCFG_CHAIN_POSITION },
3655 { .name = NULL, .value = -1 }
3658 static int target_configure(Jim_GetOptInfo *goi, struct target *target)
3666 /* parse config or cget options ... */
3667 while (goi->argc > 0) {
3668 Jim_SetEmptyResult(goi->interp);
3669 /* Jim_GetOpt_Debug(goi); */
3671 if (target->type->target_jim_configure) {
3672 /* target defines a configure function */
3673 /* target gets first dibs on parameters */
3674 e = (*(target->type->target_jim_configure))(target, goi);
3683 /* otherwise we 'continue' below */
3685 e = Jim_GetOpt_Nvp(goi, nvp_config_opts, &n);
3687 Jim_GetOpt_NvpUnknown(goi, nvp_config_opts, 0);
3693 if (goi->isconfigure) {
3694 Jim_SetResult_sprintf(goi->interp,
3695 "not settable: %s", n->name);
3699 if (goi->argc != 0) {
3700 Jim_WrongNumArgs(goi->interp,
3701 goi->argc, goi->argv,
3706 Jim_SetResultString(goi->interp,
3707 target_type_name(target), -1);
3711 if (goi->argc == 0) {
3712 Jim_WrongNumArgs(goi->interp, goi->argc, goi->argv, "-event ?event-name? ...");
3716 e = Jim_GetOpt_Nvp(goi, nvp_target_event, &n);
3718 Jim_GetOpt_NvpUnknown(goi, nvp_target_event, 1);
3722 if (goi->isconfigure) {
3723 if (goi->argc != 1) {
3724 Jim_WrongNumArgs(goi->interp, goi->argc, goi->argv, "-event ?event-name? ?EVENT-BODY?");
3728 if (goi->argc != 0) {
3729 Jim_WrongNumArgs(goi->interp, goi->argc, goi->argv, "-event ?event-name?");
3735 struct target_event_action *teap;
3737 teap = target->event_action;
3738 /* replace existing? */
3740 if (teap->event == (enum target_event)n->value) {
3746 if (goi->isconfigure) {
3747 bool replace = true;
3750 teap = calloc(1, sizeof(*teap));
3753 teap->event = n->value;
3754 teap->interp = goi->interp;
3755 Jim_GetOpt_Obj(goi, &o);
3757 Jim_DecrRefCount(teap->interp, teap->body);
3759 teap->body = Jim_DuplicateObj(goi->interp, o);
3762 * Tcl/TK - "tk events" have a nice feature.
3763 * See the "BIND" command.
3764 * We should support that here.
3765 * You can specify %X and %Y in the event code.
3766 * The idea is: %T - target name.
3767 * The idea is: %N - target number
3768 * The idea is: %E - event name.
3770 Jim_IncrRefCount(teap->body);
3774 /* add to head of event list */
3775 teap->next = target->event_action;
3776 target->event_action = teap;
3778 Jim_SetEmptyResult(goi->interp);
3782 Jim_SetEmptyResult(goi->interp);
3784 Jim_SetResult(goi->interp, Jim_DuplicateObj(goi->interp, teap->body));
3791 case TCFG_WORK_AREA_VIRT:
3792 if (goi->isconfigure) {
3793 target_free_all_working_areas(target);
3794 e = Jim_GetOpt_Wide(goi, &w);
3798 target->working_area_virt = w;
3799 target->working_area_virt_spec = true;
3801 if (goi->argc != 0) {
3805 Jim_SetResult(goi->interp, Jim_NewIntObj(goi->interp, target->working_area_virt));
3809 case TCFG_WORK_AREA_PHYS:
3810 if (goi->isconfigure) {
3811 target_free_all_working_areas(target);
3812 e = Jim_GetOpt_Wide(goi, &w);
3816 target->working_area_phys = w;
3817 target->working_area_phys_spec = true;
3819 if (goi->argc != 0) {
3823 Jim_SetResult(goi->interp, Jim_NewIntObj(goi->interp, target->working_area_phys));
3827 case TCFG_WORK_AREA_SIZE:
3828 if (goi->isconfigure) {
3829 target_free_all_working_areas(target);
3830 e = Jim_GetOpt_Wide(goi, &w);
3834 target->working_area_size = w;
3836 if (goi->argc != 0) {
3840 Jim_SetResult(goi->interp, Jim_NewIntObj(goi->interp, target->working_area_size));
3844 case TCFG_WORK_AREA_BACKUP:
3845 if (goi->isconfigure) {
3846 target_free_all_working_areas(target);
3847 e = Jim_GetOpt_Wide(goi, &w);
3851 /* make this exactly 1 or 0 */
3852 target->backup_working_area = (!!w);
3854 if (goi->argc != 0) {
3858 Jim_SetResult(goi->interp, Jim_NewIntObj(goi->interp, target->backup_working_area));
3859 /* loop for more e*/
3863 if (goi->isconfigure) {
3864 e = Jim_GetOpt_Nvp(goi, nvp_target_endian, &n);
3866 Jim_GetOpt_NvpUnknown(goi, nvp_target_endian, 1);
3869 target->endianness = n->value;
3871 if (goi->argc != 0) {
3875 n = Jim_Nvp_value2name_simple(nvp_target_endian, target->endianness);
3876 if (n->name == NULL) {
3877 target->endianness = TARGET_LITTLE_ENDIAN;
3878 n = Jim_Nvp_value2name_simple(nvp_target_endian, target->endianness);
3880 Jim_SetResultString(goi->interp, n->name, -1);
3885 if (goi->isconfigure) {
3886 if (goi->argc < 1) {
3887 Jim_SetResult_sprintf(goi->interp,
3892 if (target->variant) {
3893 free((void *)(target->variant));
3895 e = Jim_GetOpt_String(goi, &cp, NULL);
3896 target->variant = strdup(cp);
3898 if (goi->argc != 0) {
3902 Jim_SetResultString(goi->interp, target->variant,-1);
3905 case TCFG_CHAIN_POSITION:
3906 if (goi->isconfigure) {
3908 struct jtag_tap *tap;
3909 target_free_all_working_areas(target);
3910 e = Jim_GetOpt_Obj(goi, &o);
3914 tap = jtag_tap_by_jim_obj(goi->interp, o);
3918 /* make this exactly 1 or 0 */
3921 if (goi->argc != 0) {
3925 Jim_SetResultString(goi->interp, target->tap->dotted_name, -1);
3926 /* loop for more e*/
3929 } /* while (goi->argc) */
3932 /* done - we return */
3937 jim_target_configure(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
3941 Jim_GetOpt_Setup(&goi, interp, argc - 1, argv + 1);
3942 goi.isconfigure = !strcmp(Jim_GetString(argv[0], NULL), "configure");
3943 int need_args = 1 + goi.isconfigure;
3944 if (goi.argc < need_args)
3946 Jim_WrongNumArgs(goi.interp, goi.argc, goi.argv,
3948 ? "missing: -option VALUE ..."
3949 : "missing: -option ...");
3952 struct target *target = Jim_CmdPrivData(goi.interp);
3953 return target_configure(&goi, target);
3956 static int jim_target_mw(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
3958 const char *cmd_name = Jim_GetString(argv[0], NULL);
3961 Jim_GetOpt_Setup(&goi, interp, argc - 1, argv + 1);
3963 /* danger! goi.argc will be modified below! */
3966 if (argc != 2 && argc != 3)
3968 Jim_SetResult_sprintf(goi.interp,
3969 "usage: %s <address> <data> [<count>]", cmd_name);
3975 int e = Jim_GetOpt_Wide(&goi, &a);
3980 e = Jim_GetOpt_Wide(&goi, &b);
3987 e = Jim_GetOpt_Wide(&goi, &c);
3992 struct target *target = Jim_CmdPrivData(goi.interp);
3994 if (strcasecmp(cmd_name, "mww") == 0) {
3997 else if (strcasecmp(cmd_name, "mwh") == 0) {
4000 else if (strcasecmp(cmd_name, "mwb") == 0) {
4003 LOG_ERROR("command '%s' unknown: ", cmd_name);
4007 return (target_fill_mem(target, a, target_write_memory_fast, data_size, b, c) == ERROR_OK) ? JIM_OK : JIM_ERR;
4010 static int jim_target_md(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
4012 const char *cmd_name = Jim_GetString(argv[0], NULL);
4015 Jim_GetOpt_Setup(&goi, interp, argc - 1, argv + 1);
4017 /* danger! goi.argc will be modified below! */
4020 if ((argc != 1) && (argc != 2))
4022 Jim_SetResult_sprintf(goi.interp,
4023 "usage: %s <address> [<count>]", cmd_name);
4028 int e = Jim_GetOpt_Wide(&goi, &a);
4034 e = Jim_GetOpt_Wide(&goi, &c);
4041 jim_wide b = 1; /* shut up gcc */
4042 if (strcasecmp(cmd_name, "mdw") == 0)
4044 else if (strcasecmp(cmd_name, "mdh") == 0)
4046 else if (strcasecmp(cmd_name, "mdb") == 0)
4049 LOG_ERROR("command '%s' unknown: ", cmd_name);
4053 /* convert count to "bytes" */
4056 struct target *target = Jim_CmdPrivData(goi.interp);
4057 uint8_t target_buf[32];
4064 e = target_read_memory(target, a, b, y / b, target_buf);
4065 if (e != ERROR_OK) {
4066 Jim_SetResult_sprintf(interp, "error reading target @ 0x%08lx", (int)(a));
4070 Jim_fprintf(interp, interp->cookie_stdout, "0x%08x ", (int)(a));
4073 for (x = 0; x < 16 && x < y; x += 4)
4075 z = target_buffer_get_u32(target, &(target_buf[ x ]));
4076 Jim_fprintf(interp, interp->cookie_stdout, "%08x ", (int)(z));
4078 for (; (x < 16) ; x += 4) {
4079 Jim_fprintf(interp, interp->cookie_stdout, " ");
4083 for (x = 0; x < 16 && x < y; x += 2)
4085 z = target_buffer_get_u16(target, &(target_buf[ x ]));
4086 Jim_fprintf(interp, interp->cookie_stdout, "%04x ", (int)(z));
4088 for (; (x < 16) ; x += 2) {
4089 Jim_fprintf(interp, interp->cookie_stdout, " ");
4094 for (x = 0 ; (x < 16) && (x < y) ; x += 1) {
4095 z = target_buffer_get_u8(target, &(target_buf[ x ]));
4096 Jim_fprintf(interp, interp->cookie_stdout, "%02x ", (int)(z));
4098 for (; (x < 16) ; x += 1) {
4099 Jim_fprintf(interp, interp->cookie_stdout, " ");
4103 /* ascii-ify the bytes */
4104 for (x = 0 ; x < y ; x++) {
4105 if ((target_buf[x] >= 0x20) &&
4106 (target_buf[x] <= 0x7e)) {
4110 target_buf[x] = '.';
4115 target_buf[x] = ' ';
4120 /* print - with a newline */
4121 Jim_fprintf(interp, interp->cookie_stdout, "%s\n", target_buf);
4129 static int jim_target_mem2array(Jim_Interp *interp,
4130 int argc, Jim_Obj *const *argv)
4132 struct target *target = Jim_CmdPrivData(interp);
4133 return target_mem2array(interp, target, argc - 1, argv + 1);
4136 static int jim_target_array2mem(Jim_Interp *interp,
4137 int argc, Jim_Obj *const *argv)
4139 struct target *target = Jim_CmdPrivData(interp);
4140 return target_array2mem(interp, target, argc - 1, argv + 1);
4143 static int jim_target_tap_disabled(Jim_Interp *interp)
4145 Jim_SetResult_sprintf(interp, "[TAP is disabled]");
4149 static int jim_target_examine(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
4153 Jim_WrongNumArgs(interp, 1, argv, "[no parameters]");
4156 struct target *target = Jim_CmdPrivData(interp);
4157 if (!target->tap->enabled)
4158 return jim_target_tap_disabled(interp);
4160 int e = target->type->examine(target);
4163 Jim_SetResult_sprintf(interp, "examine-fails: %d", e);
4169 static int jim_target_halt_gdb(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
4173 Jim_WrongNumArgs(interp, 1, argv, "[no parameters]");
4176 struct target *target = Jim_CmdPrivData(interp);
4178 if (target_call_event_callbacks(target, TARGET_EVENT_GDB_HALT) != ERROR_OK)
4184 static int jim_target_poll(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
4188 Jim_WrongNumArgs(interp, 1, argv, "[no parameters]");
4191 struct target *target = Jim_CmdPrivData(interp);
4192 if (!target->tap->enabled)
4193 return jim_target_tap_disabled(interp);
4196 if (!(target_was_examined(target))) {
4197 e = ERROR_TARGET_NOT_EXAMINED;
4199 e = target->type->poll(target);
4203 Jim_SetResult_sprintf(interp, "poll-fails: %d", e);
4209 static int jim_target_reset(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
4212 Jim_GetOpt_Setup(&goi, interp, argc - 1, argv + 1);
4216 Jim_WrongNumArgs(interp, 0, argv,
4217 "([tT]|[fF]|assert|deassert) BOOL");
4222 int e = Jim_GetOpt_Nvp(&goi, nvp_assert, &n);
4225 Jim_GetOpt_NvpUnknown(&goi, nvp_assert, 1);
4228 /* the halt or not param */
4230 e = Jim_GetOpt_Wide(&goi, &a);
4234 struct target *target = Jim_CmdPrivData(goi.interp);
4235 if (!target->tap->enabled)
4236 return jim_target_tap_disabled(interp);
4237 if (!(target_was_examined(target)))
4239 LOG_ERROR("Target not examined yet");
4240 return ERROR_TARGET_NOT_EXAMINED;
4242 if (!target->type->assert_reset || !target->type->deassert_reset)
4244 Jim_SetResult_sprintf(interp,
4245 "No target-specific reset for %s",
4246 target_name(target));
4249 /* determine if we should halt or not. */
4250 target->reset_halt = !!a;
4251 /* When this happens - all workareas are invalid. */
4252 target_free_all_working_areas_restore(target, 0);
4255 if (n->value == NVP_ASSERT) {
4256 e = target->type->assert_reset(target);
4258 e = target->type->deassert_reset(target);
4260 return (e == ERROR_OK) ? JIM_OK : JIM_ERR;
4263 static int jim_target_halt(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
4266 Jim_WrongNumArgs(interp, 1, argv, "[no parameters]");
4269 struct target *target = Jim_CmdPrivData(interp);
4270 if (!target->tap->enabled)
4271 return jim_target_tap_disabled(interp);
4272 int e = target->type->halt(target);
4273 return (e == ERROR_OK) ? JIM_OK : JIM_ERR;
4276 static int jim_target_wait_state(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
4279 Jim_GetOpt_Setup(&goi, interp, argc - 1, argv + 1);
4281 /* params: <name> statename timeoutmsecs */
4284 const char *cmd_name = Jim_GetString(argv[0], NULL);
4285 Jim_SetResult_sprintf(goi.interp,
4286 "%s <state_name> <timeout_in_msec>", cmd_name);
4291 int e = Jim_GetOpt_Nvp(&goi, nvp_target_state, &n);
4293 Jim_GetOpt_NvpUnknown(&goi, nvp_target_state,1);
4297 e = Jim_GetOpt_Wide(&goi, &a);
4301 struct target *target = Jim_CmdPrivData(interp);
4302 if (!target->tap->enabled)
4303 return jim_target_tap_disabled(interp);
4305 e = target_wait_state(target, n->value, a);
4308 Jim_SetResult_sprintf(goi.interp,
4309 "target: %s wait %s fails (%d) %s",
4310 target_name(target), n->name,
4311 e, target_strerror_safe(e));
4316 /* List for human, Events defined for this target.
4317 * scripts/programs should use 'name cget -event NAME'
4319 static int jim_target_event_list(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
4321 struct command_context *cmd_ctx = Jim_GetAssocData(interp, "context");
4322 struct target *target = Jim_CmdPrivData(interp);
4323 struct target_event_action *teap = target->event_action;
4324 command_print(cmd_ctx, "Event actions for target (%d) %s\n",
4325 target->target_number,
4326 target_name(target));
4327 command_print(cmd_ctx, "%-25s | Body", "Event");
4328 command_print(cmd_ctx, "------------------------- | "
4329 "----------------------------------------");
4332 Jim_Nvp *opt = Jim_Nvp_value2name_simple(nvp_target_event, teap->event);
4333 command_print(cmd_ctx, "%-25s | %s",
4334 opt->name, Jim_GetString(teap->body, NULL));
4337 command_print(cmd_ctx, "***END***");
4340 static int jim_target_current_state(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
4344 Jim_WrongNumArgs(interp, 1, argv, "[no parameters]");
4347 struct target *target = Jim_CmdPrivData(interp);
4348 Jim_SetResultString(interp, target_state_name(target), -1);
4351 static int jim_target_invoke_event(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
4354 Jim_GetOpt_Setup(&goi, interp, argc - 1, argv + 1);
4357 const char *cmd_name = Jim_GetString(argv[0], NULL);
4358 Jim_SetResult_sprintf(goi.interp, "%s <eventname>", cmd_name);
4362 int e = Jim_GetOpt_Nvp(&goi, nvp_target_event, &n);
4365 Jim_GetOpt_NvpUnknown(&goi, nvp_target_event, 1);
4368 struct target *target = Jim_CmdPrivData(interp);
4369 target_handle_event(target, n->value);
4373 static const struct command_registration target_instance_command_handlers[] = {
4375 .name = "configure",
4376 .mode = COMMAND_CONFIG,
4377 .jim_handler = jim_target_configure,
4378 .help = "configure a new target for use",
4379 .usage = "[target_attribute ...]",
4383 .mode = COMMAND_ANY,
4384 .jim_handler = jim_target_configure,
4385 .help = "returns the specified target attribute",
4386 .usage = "target_attribute",
4390 .mode = COMMAND_EXEC,
4391 .jim_handler = jim_target_mw,
4392 .help = "Write 32-bit word(s) to target memory",
4393 .usage = "address data [count]",
4397 .mode = COMMAND_EXEC,
4398 .jim_handler = jim_target_mw,
4399 .help = "Write 16-bit half-word(s) to target memory",
4400 .usage = "address data [count]",
4404 .mode = COMMAND_EXEC,
4405 .jim_handler = jim_target_mw,
4406 .help = "Write byte(s) to target memory",
4407 .usage = "address data [count]",
4411 .mode = COMMAND_EXEC,
4412 .jim_handler = jim_target_md,
4413 .help = "Display target memory as 32-bit words",
4414 .usage = "address [count]",
4418 .mode = COMMAND_EXEC,
4419 .jim_handler = jim_target_md,
4420 .help = "Display target memory as 16-bit half-words",
4421 .usage = "address [count]",
4425 .mode = COMMAND_EXEC,
4426 .jim_handler = jim_target_md,
4427 .help = "Display target memory as 8-bit bytes",
4428 .usage = "address [count]",
4431 .name = "array2mem",
4432 .mode = COMMAND_EXEC,
4433 .jim_handler = jim_target_array2mem,
4434 .help = "Writes Tcl array of 8/16/32 bit numbers "
4436 .usage = "arrayname bitwidth address count",
4439 .name = "mem2array",
4440 .mode = COMMAND_EXEC,
4441 .jim_handler = jim_target_mem2array,
4442 .help = "Loads Tcl array of 8/16/32 bit numbers "
4443 "from target memory",
4444 .usage = "arrayname bitwidth address count",
4447 .name = "eventlist",
4448 .mode = COMMAND_EXEC,
4449 .jim_handler = jim_target_event_list,
4450 .help = "displays a table of events defined for this target",
4454 .mode = COMMAND_EXEC,
4455 .jim_handler = jim_target_current_state,
4456 .help = "displays the current state of this target",
4459 .name = "arp_examine",
4460 .mode = COMMAND_EXEC,
4461 .jim_handler = jim_target_examine,
4462 .help = "used internally for reset processing",
4465 .name = "arp_halt_gdb",
4466 .mode = COMMAND_EXEC,
4467 .jim_handler = jim_target_halt_gdb,
4468 .help = "used internally for reset processing to halt GDB",
4472 .mode = COMMAND_EXEC,
4473 .jim_handler = jim_target_poll,
4474 .help = "used internally for reset processing",
4477 .name = "arp_reset",
4478 .mode = COMMAND_EXEC,
4479 .jim_handler = jim_target_reset,
4480 .help = "used internally for reset processing",
4484 .mode = COMMAND_EXEC,
4485 .jim_handler = jim_target_halt,
4486 .help = "used internally for reset processing",
4489 .name = "arp_waitstate",
4490 .mode = COMMAND_EXEC,
4491 .jim_handler = jim_target_wait_state,
4492 .help = "used internally for reset processing",
4495 .name = "invoke-event",
4496 .mode = COMMAND_EXEC,
4497 .jim_handler = jim_target_invoke_event,
4498 .help = "invoke handler for specified event",
4499 .usage = "event_name",
4501 COMMAND_REGISTRATION_DONE
4504 static int target_create(Jim_GetOptInfo *goi)
4512 struct target *target;
4513 struct command_context *cmd_ctx;
4515 cmd_ctx = Jim_GetAssocData(goi->interp, "context");
4516 if (goi->argc < 3) {
4517 Jim_WrongNumArgs(goi->interp, 1, goi->argv, "?name? ?type? ..options...");
4522 Jim_GetOpt_Obj(goi, &new_cmd);
4523 /* does this command exist? */
4524 cmd = Jim_GetCommand(goi->interp, new_cmd, JIM_ERRMSG);
4526 cp = Jim_GetString(new_cmd, NULL);
4527 Jim_SetResult_sprintf(goi->interp, "Command/target: %s Exists", cp);
4532 e = Jim_GetOpt_String(goi, &cp2, NULL);
4534 /* now does target type exist */
4535 for (x = 0 ; target_types[x] ; x++) {
4536 if (0 == strcmp(cp, target_types[x]->name)) {
4541 if (target_types[x] == NULL) {
4542 Jim_SetResult_sprintf(goi->interp, "Unknown target type %s, try one of ", cp);
4543 for (x = 0 ; target_types[x] ; x++) {
4544 if (target_types[x + 1]) {
4545 Jim_AppendStrings(goi->interp,
4546 Jim_GetResult(goi->interp),
4547 target_types[x]->name,
4550 Jim_AppendStrings(goi->interp,
4551 Jim_GetResult(goi->interp),
4553 target_types[x]->name,NULL);
4560 target = calloc(1,sizeof(struct target));
4561 /* set target number */
4562 target->target_number = new_target_number();
4564 /* allocate memory for each unique target type */
4565 target->type = (struct target_type*)calloc(1,sizeof(struct target_type));
4567 memcpy(target->type, target_types[x], sizeof(struct target_type));
4569 /* will be set by "-endian" */
4570 target->endianness = TARGET_ENDIAN_UNKNOWN;
4572 target->working_area = 0x0;
4573 target->working_area_size = 0x0;
4574 target->working_areas = NULL;
4575 target->backup_working_area = 0;
4577 target->state = TARGET_UNKNOWN;
4578 target->debug_reason = DBG_REASON_UNDEFINED;
4579 target->reg_cache = NULL;
4580 target->breakpoints = NULL;
4581 target->watchpoints = NULL;
4582 target->next = NULL;
4583 target->arch_info = NULL;
4585 target->display = 1;
4587 target->halt_issued = false;
4589 /* initialize trace information */
4590 target->trace_info = malloc(sizeof(struct trace));
4591 target->trace_info->num_trace_points = 0;
4592 target->trace_info->trace_points_size = 0;
4593 target->trace_info->trace_points = NULL;
4594 target->trace_info->trace_history_size = 0;
4595 target->trace_info->trace_history = NULL;
4596 target->trace_info->trace_history_pos = 0;
4597 target->trace_info->trace_history_overflowed = 0;
4599 target->dbgmsg = NULL;
4600 target->dbg_msg_enabled = 0;
4602 target->endianness = TARGET_ENDIAN_UNKNOWN;
4604 /* Do the rest as "configure" options */
4605 goi->isconfigure = 1;
4606 e = target_configure(goi, target);
4608 if (target->tap == NULL)
4610 Jim_SetResultString(goi->interp, "-chain-position required when creating target", -1);
4620 if (target->endianness == TARGET_ENDIAN_UNKNOWN) {
4621 /* default endian to little if not specified */
4622 target->endianness = TARGET_LITTLE_ENDIAN;
4625 /* incase variant is not set */
4626 if (!target->variant)
4627 target->variant = strdup("");
4629 cp = Jim_GetString(new_cmd, NULL);
4630 target->cmd_name = strdup(cp);
4632 /* create the target specific commands */
4633 if (target->type->commands) {
4634 e = register_commands(cmd_ctx, NULL, target->type->commands);
4636 LOG_ERROR("unable to register '%s' commands", cp);
4638 if (target->type->target_create) {
4639 (*(target->type->target_create))(target, goi->interp);
4642 /* append to end of list */
4644 struct target **tpp;
4645 tpp = &(all_targets);
4647 tpp = &((*tpp)->next);
4652 /* now - create the new target name command */
4653 const const struct command_registration target_subcommands[] = {
4655 .chain = target_instance_command_handlers,
4658 .chain = target->type->commands,
4660 COMMAND_REGISTRATION_DONE
4662 const const struct command_registration target_commands[] = {
4665 .mode = COMMAND_ANY,
4666 .help = "target command group",
4667 .chain = target_subcommands,
4669 COMMAND_REGISTRATION_DONE
4671 e = register_commands(cmd_ctx, NULL, target_commands);
4675 struct command *c = command_find_in_context(cmd_ctx, cp);
4677 command_set_handler_data(c, target);
4679 return (ERROR_OK == e) ? JIM_OK : JIM_ERR;
4682 static int jim_target_current(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
4686 Jim_WrongNumArgs(interp, 1, argv, "Too many parameters");
4689 struct command_context *cmd_ctx = Jim_GetAssocData(interp, "context");
4690 Jim_SetResultString(interp, get_current_target(cmd_ctx)->cmd_name, -1);
4694 static int jim_target_types(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
4698 Jim_WrongNumArgs(interp, 1, argv, "Too many parameters");
4701 Jim_SetResult(interp, Jim_NewListObj(interp, NULL, 0));
4702 for (unsigned x = 0; NULL != target_types[x]; x++)
4704 Jim_ListAppendElement(interp, Jim_GetResult(interp),
4705 Jim_NewStringObj(interp, target_types[x]->name, -1));
4710 static int jim_target_names(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
4714 Jim_WrongNumArgs(interp, 1, argv, "Too many parameters");
4717 Jim_SetResult(interp, Jim_NewListObj(interp, NULL, 0));
4718 struct target *target = all_targets;
4721 Jim_ListAppendElement(interp, Jim_GetResult(interp),
4722 Jim_NewStringObj(interp, target_name(target), -1));
4723 target = target->next;
4728 static int jim_target_create(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
4731 Jim_GetOpt_Setup(&goi, interp, argc - 1, argv + 1);
4734 Jim_WrongNumArgs(goi.interp, goi.argc, goi.argv,
4735 "<name> <target_type> [<target_options> ...]");
4738 return target_create(&goi);
4741 static int jim_target_number(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
4744 Jim_GetOpt_Setup(&goi, interp, argc - 1, argv + 1);
4746 /* It's OK to remove this mechanism sometime after August 2010 or so */
4747 LOG_WARNING("don't use numbers as target identifiers; use names");
4750 Jim_SetResult_sprintf(goi.interp, "usage: target number <number>");
4754 int e = Jim_GetOpt_Wide(&goi, &w);
4758 struct target *target;
4759 for (target = all_targets; NULL != target; target = target->next)
4761 if (target->target_number != w)
4764 Jim_SetResultString(goi.interp, target_name(target), -1);
4767 Jim_SetResult_sprintf(goi.interp,
4768 "Target: number %d does not exist", (int)(w));
4772 static int jim_target_count(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
4776 Jim_WrongNumArgs(interp, 1, argv, "<no parameters>");
4780 struct target *target = all_targets;
4781 while (NULL != target)
4783 target = target->next;
4786 Jim_SetResult(interp, Jim_NewIntObj(interp, count));
4790 static const struct command_registration target_subcommand_handlers[] = {
4793 .mode = COMMAND_CONFIG,
4794 .handler = handle_target_init_command,
4795 .help = "initialize targets",
4799 /* REVISIT this should be COMMAND_CONFIG ... */
4800 .mode = COMMAND_ANY,
4801 .jim_handler = jim_target_create,
4802 .usage = "name type '-chain-position' name [options ...]",
4803 .help = "Creates and selects a new target",
4807 .mode = COMMAND_ANY,
4808 .jim_handler = jim_target_current,
4809 .help = "Returns the currently selected target",
4813 .mode = COMMAND_ANY,
4814 .jim_handler = jim_target_types,
4815 .help = "Returns the available target types as "
4816 "a list of strings",
4820 .mode = COMMAND_ANY,
4821 .jim_handler = jim_target_names,
4822 .help = "Returns the names of all targets as a list of strings",
4826 .mode = COMMAND_ANY,
4827 .jim_handler = jim_target_number,
4829 .help = "Returns the name of the numbered target "
4834 .mode = COMMAND_ANY,
4835 .jim_handler = jim_target_count,
4836 .help = "Returns the number of targets as an integer "
4839 COMMAND_REGISTRATION_DONE
4850 static int fastload_num;
4851 static struct FastLoad *fastload;
4853 static void free_fastload(void)
4855 if (fastload != NULL)
4858 for (i = 0; i < fastload_num; i++)
4860 if (fastload[i].data)
4861 free(fastload[i].data);
4871 COMMAND_HANDLER(handle_fast_load_image_command)
4875 uint32_t image_size;
4876 uint32_t min_address = 0;
4877 uint32_t max_address = 0xffffffff;
4882 int retval = CALL_COMMAND_HANDLER(parse_load_image_command_CMD_ARGV,
4883 &image, &min_address, &max_address);
4884 if (ERROR_OK != retval)
4887 struct duration bench;
4888 duration_start(&bench);
4890 if (image_open(&image, CMD_ARGV[0], (CMD_ARGC >= 3) ? CMD_ARGV[2] : NULL) != ERROR_OK)
4897 fastload_num = image.num_sections;
4898 fastload = (struct FastLoad *)malloc(sizeof(struct FastLoad)*image.num_sections);
4899 if (fastload == NULL)
4901 image_close(&image);
4904 memset(fastload, 0, sizeof(struct FastLoad)*image.num_sections);
4905 for (i = 0; i < image.num_sections; i++)
4907 buffer = malloc(image.sections[i].size);
4910 command_print(CMD_CTX, "error allocating buffer for section (%d bytes)",
4911 (int)(image.sections[i].size));
4915 if ((retval = image_read_section(&image, i, 0x0, image.sections[i].size, buffer, &buf_cnt)) != ERROR_OK)
4921 uint32_t offset = 0;
4922 uint32_t length = buf_cnt;
4925 /* DANGER!!! beware of unsigned comparision here!!! */
4927 if ((image.sections[i].base_address + buf_cnt >= min_address)&&
4928 (image.sections[i].base_address < max_address))
4930 if (image.sections[i].base_address < min_address)
4932 /* clip addresses below */
4933 offset += min_address-image.sections[i].base_address;
4937 if (image.sections[i].base_address + buf_cnt > max_address)
4939 length -= (image.sections[i].base_address + buf_cnt)-max_address;
4942 fastload[i].address = image.sections[i].base_address + offset;
4943 fastload[i].data = malloc(length);
4944 if (fastload[i].data == NULL)
4949 memcpy(fastload[i].data, buffer + offset, length);
4950 fastload[i].length = length;
4952 image_size += length;
4953 command_print(CMD_CTX, "%u bytes written at address 0x%8.8x",
4954 (unsigned int)length,
4955 ((unsigned int)(image.sections[i].base_address + offset)));
4961 if ((ERROR_OK == retval) && (duration_measure(&bench) == ERROR_OK))
4963 command_print(CMD_CTX, "Loaded %" PRIu32 " bytes "
4964 "in %fs (%0.3f kb/s)", image_size,
4965 duration_elapsed(&bench), duration_kbps(&bench, image_size));
4967 command_print(CMD_CTX,
4968 "WARNING: image has not been loaded to target!"
4969 "You can issue a 'fast_load' to finish loading.");
4972 image_close(&image);
4974 if (retval != ERROR_OK)
4982 COMMAND_HANDLER(handle_fast_load_command)
4985 return ERROR_COMMAND_SYNTAX_ERROR;
4986 if (fastload == NULL)
4988 LOG_ERROR("No image in memory");
4992 int ms = timeval_ms();
4994 int retval = ERROR_OK;
4995 for (i = 0; i < fastload_num;i++)
4997 struct target *target = get_current_target(CMD_CTX);
4998 command_print(CMD_CTX, "Write to 0x%08x, length 0x%08x",
4999 (unsigned int)(fastload[i].address),
5000 (unsigned int)(fastload[i].length));
5001 if (retval == ERROR_OK)
5003 retval = target_write_buffer(target, fastload[i].address, fastload[i].length, fastload[i].data);
5005 size += fastload[i].length;
5007 int after = timeval_ms();
5008 command_print(CMD_CTX, "Loaded image %f kBytes/s", (float)(size/1024.0)/((float)(after-ms)/1000.0));
5012 static const struct command_registration target_command_handlers[] = {
5015 .handler = handle_targets_command,
5016 .mode = COMMAND_ANY,
5017 .help = "change current default target (one parameter) "
5018 "or prints table of all targets (no parameters)",
5019 .usage = "[target]",
5023 .mode = COMMAND_CONFIG,
5024 .help = "configure target",
5026 .chain = target_subcommand_handlers,
5028 COMMAND_REGISTRATION_DONE
5031 int target_register_commands(struct command_context *cmd_ctx)
5033 return register_commands(cmd_ctx, NULL, target_command_handlers);
5036 static bool target_reset_nag = true;
5038 bool get_target_reset_nag(void)
5040 return target_reset_nag;
5043 COMMAND_HANDLER(handle_target_reset_nag)
5045 return CALL_COMMAND_HANDLER(handle_command_parse_bool,
5046 &target_reset_nag, "Nag after each reset about options to improve "
5050 static const struct command_registration target_exec_command_handlers[] = {
5052 .name = "fast_load_image",
5053 .handler = handle_fast_load_image_command,
5054 .mode = COMMAND_ANY,
5055 .help = "Load image into server memory for later use by "
5056 "fast_load; primarily for profiling",
5057 .usage = "filename address ['bin'|'ihex'|'elf'|'s19'] "
5058 "[min_address [max_length]]",
5061 .name = "fast_load",
5062 .handler = handle_fast_load_command,
5063 .mode = COMMAND_EXEC,
5064 .help = "loads active fast load image to current target "
5065 "- mainly for profiling purposes",
5069 .handler = handle_profile_command,
5070 .mode = COMMAND_EXEC,
5071 .help = "profiling samples the CPU PC",
5073 /** @todo don't register virt2phys() unless target supports it */
5075 .name = "virt2phys",
5076 .handler = handle_virt2phys_command,
5077 .mode = COMMAND_ANY,
5078 .help = "translate a virtual address into a physical address",
5079 .usage = "virtual_address",
5083 .handler = handle_reg_command,
5084 .mode = COMMAND_EXEC,
5085 .help = "display or set a register; with no arguments, "
5086 "displays all registers and their values",
5087 .usage = "[(register_name|register_number) [value]]",
5091 .handler = handle_poll_command,
5092 .mode = COMMAND_EXEC,
5093 .help = "poll target state; or reconfigure background polling",
5094 .usage = "['on'|'off']",
5097 .name = "wait_halt",
5098 .handler = handle_wait_halt_command,
5099 .mode = COMMAND_EXEC,
5100 .help = "wait up to the specified number of milliseconds "
5101 "(default 5) for a previously requested halt",
5102 .usage = "[milliseconds]",
5106 .handler = handle_halt_command,
5107 .mode = COMMAND_EXEC,
5108 .help = "request target to halt, then wait up to the specified"
5109 "number of milliseconds (default 5) for it to complete",
5110 .usage = "[milliseconds]",
5114 .handler = handle_resume_command,
5115 .mode = COMMAND_EXEC,
5116 .help = "resume target execution from current PC or address",
5117 .usage = "[address]",
5121 .handler = handle_reset_command,
5122 .mode = COMMAND_EXEC,
5123 .usage = "[run|halt|init]",
5124 .help = "Reset all targets into the specified mode."
5125 "Default reset mode is run, if not given.",
5128 .name = "soft_reset_halt",
5129 .handler = handle_soft_reset_halt_command,
5130 .mode = COMMAND_EXEC,
5131 .help = "halt the target and do a soft reset",
5135 .handler = handle_step_command,
5136 .mode = COMMAND_EXEC,
5137 .help = "step one instruction from current PC or address",
5138 .usage = "[address]",
5142 .handler = handle_md_command,
5143 .mode = COMMAND_EXEC,
5144 .help = "display memory words",
5145 .usage = "['phys'] address [count]",
5149 .handler = handle_md_command,
5150 .mode = COMMAND_EXEC,
5151 .help = "display memory half-words",
5152 .usage = "['phys'] address [count]",
5156 .handler = handle_md_command,
5157 .mode = COMMAND_EXEC,
5158 .help = "display memory bytes",
5159 .usage = "['phys'] address [count]",
5163 .handler = handle_mw_command,
5164 .mode = COMMAND_EXEC,
5165 .help = "write memory word",
5166 .usage = "['phys'] address value [count]",
5170 .handler = handle_mw_command,
5171 .mode = COMMAND_EXEC,
5172 .help = "write memory half-word",
5173 .usage = "['phys'] address value [count]",
5177 .handler = handle_mw_command,
5178 .mode = COMMAND_EXEC,
5179 .help = "write memory byte",
5180 .usage = "['phys'] address value [count]",
5184 .handler = handle_bp_command,
5185 .mode = COMMAND_EXEC,
5186 .help = "list or set hardware or software breakpoint",
5187 .usage = "[address length ['hw']]",
5191 .handler = handle_rbp_command,
5192 .mode = COMMAND_EXEC,
5193 .help = "remove breakpoint",
5198 .handler = handle_wp_command,
5199 .mode = COMMAND_EXEC,
5200 .help = "list (no params) or create watchpoints",
5201 .usage = "[address length [('r'|'w'|'a') value [mask]]]",
5205 .handler = handle_rwp_command,
5206 .mode = COMMAND_EXEC,
5207 .help = "remove watchpoint",
5211 .name = "load_image",
5212 .handler = handle_load_image_command,
5213 .mode = COMMAND_EXEC,
5214 .usage = "filename address ['bin'|'ihex'|'elf'|'s19'] "
5215 "[min_address] [max_length]",
5218 .name = "dump_image",
5219 .handler = handle_dump_image_command,
5220 .mode = COMMAND_EXEC,
5221 .usage = "filename address size",
5224 .name = "verify_image",
5225 .handler = handle_verify_image_command,
5226 .mode = COMMAND_EXEC,
5227 .usage = "filename [offset [type]]",
5230 .name = "test_image",
5231 .handler = handle_test_image_command,
5232 .mode = COMMAND_EXEC,
5233 .usage = "filename [offset [type]]",
5236 .name = "ocd_mem2array",
5237 .mode = COMMAND_EXEC,
5238 .jim_handler = jim_mem2array,
5239 .help = "read 8/16/32 bit memory and return as a TCL array "
5240 "for script processing",
5241 .usage = "arrayname bitwidth address count",
5244 .name = "ocd_array2mem",
5245 .mode = COMMAND_EXEC,
5246 .jim_handler = jim_array2mem,
5247 .help = "convert a TCL array to memory locations "
5248 "and write the 8/16/32 bit values",
5249 .usage = "arrayname bitwidth address count",
5252 .name = "reset_nag",
5253 .handler = handle_target_reset_nag,
5254 .mode = COMMAND_ANY,
5255 .help = "Nag after each reset about options that could have been "
5256 "enabled to improve performance. ",
5257 .usage = "['enable'|'disable']",
5259 COMMAND_REGISTRATION_DONE
5261 static int target_register_user_commands(struct command_context *cmd_ctx)
5263 int retval = ERROR_OK;
5264 if ((retval = target_request_register_commands(cmd_ctx)) != ERROR_OK)
5267 if ((retval = trace_register_commands(cmd_ctx)) != ERROR_OK)
5271 return register_commands(cmd_ctx, NULL, target_exec_command_handlers);