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);
55 extern struct target_type arm7tdmi_target;
56 extern struct target_type arm720t_target;
57 extern struct target_type arm9tdmi_target;
58 extern struct target_type arm920t_target;
59 extern struct target_type arm966e_target;
60 extern struct target_type arm926ejs_target;
61 extern struct target_type fa526_target;
62 extern struct target_type feroceon_target;
63 extern struct target_type dragonite_target;
64 extern struct target_type xscale_target;
65 extern struct target_type cortexm3_target;
66 extern struct target_type cortexa8_target;
67 extern struct target_type arm11_target;
68 extern struct target_type mips_m4k_target;
69 extern struct target_type avr_target;
70 extern struct target_type dsp563xx_target;
71 extern struct target_type testee_target;
73 struct target_type *target_types[] =
95 struct target *all_targets = NULL;
96 struct target_event_callback *target_event_callbacks = NULL;
97 struct target_timer_callback *target_timer_callbacks = NULL;
99 static const Jim_Nvp nvp_assert[] = {
100 { .name = "assert", NVP_ASSERT },
101 { .name = "deassert", NVP_DEASSERT },
102 { .name = "T", NVP_ASSERT },
103 { .name = "F", NVP_DEASSERT },
104 { .name = "t", NVP_ASSERT },
105 { .name = "f", NVP_DEASSERT },
106 { .name = NULL, .value = -1 }
109 static const Jim_Nvp nvp_error_target[] = {
110 { .value = ERROR_TARGET_INVALID, .name = "err-invalid" },
111 { .value = ERROR_TARGET_INIT_FAILED, .name = "err-init-failed" },
112 { .value = ERROR_TARGET_TIMEOUT, .name = "err-timeout" },
113 { .value = ERROR_TARGET_NOT_HALTED, .name = "err-not-halted" },
114 { .value = ERROR_TARGET_FAILURE, .name = "err-failure" },
115 { .value = ERROR_TARGET_UNALIGNED_ACCESS , .name = "err-unaligned-access" },
116 { .value = ERROR_TARGET_DATA_ABORT , .name = "err-data-abort" },
117 { .value = ERROR_TARGET_RESOURCE_NOT_AVAILABLE , .name = "err-resource-not-available" },
118 { .value = ERROR_TARGET_TRANSLATION_FAULT , .name = "err-translation-fault" },
119 { .value = ERROR_TARGET_NOT_RUNNING, .name = "err-not-running" },
120 { .value = ERROR_TARGET_NOT_EXAMINED, .name = "err-not-examined" },
121 { .value = -1, .name = NULL }
124 const char *target_strerror_safe(int err)
128 n = Jim_Nvp_value2name_simple(nvp_error_target, err);
129 if (n->name == NULL) {
136 static const Jim_Nvp nvp_target_event[] = {
137 { .value = TARGET_EVENT_OLD_gdb_program_config , .name = "old-gdb_program_config" },
138 { .value = TARGET_EVENT_OLD_pre_resume , .name = "old-pre_resume" },
140 { .value = TARGET_EVENT_GDB_HALT, .name = "gdb-halt" },
141 { .value = TARGET_EVENT_HALTED, .name = "halted" },
142 { .value = TARGET_EVENT_RESUMED, .name = "resumed" },
143 { .value = TARGET_EVENT_RESUME_START, .name = "resume-start" },
144 { .value = TARGET_EVENT_RESUME_END, .name = "resume-end" },
146 { .name = "gdb-start", .value = TARGET_EVENT_GDB_START },
147 { .name = "gdb-end", .value = TARGET_EVENT_GDB_END },
149 /* historical name */
151 { .value = TARGET_EVENT_RESET_START, .name = "reset-start" },
153 { .value = TARGET_EVENT_RESET_ASSERT_PRE, .name = "reset-assert-pre" },
154 { .value = TARGET_EVENT_RESET_ASSERT, .name = "reset-assert" },
155 { .value = TARGET_EVENT_RESET_ASSERT_POST, .name = "reset-assert-post" },
156 { .value = TARGET_EVENT_RESET_DEASSERT_PRE, .name = "reset-deassert-pre" },
157 { .value = TARGET_EVENT_RESET_DEASSERT_POST, .name = "reset-deassert-post" },
158 { .value = TARGET_EVENT_RESET_HALT_PRE, .name = "reset-halt-pre" },
159 { .value = TARGET_EVENT_RESET_HALT_POST, .name = "reset-halt-post" },
160 { .value = TARGET_EVENT_RESET_WAIT_PRE, .name = "reset-wait-pre" },
161 { .value = TARGET_EVENT_RESET_WAIT_POST, .name = "reset-wait-post" },
162 { .value = TARGET_EVENT_RESET_INIT, .name = "reset-init" },
163 { .value = TARGET_EVENT_RESET_END, .name = "reset-end" },
165 { .value = TARGET_EVENT_EXAMINE_START, .name = "examine-start" },
166 { .value = TARGET_EVENT_EXAMINE_END, .name = "examine-end" },
168 { .value = TARGET_EVENT_DEBUG_HALTED, .name = "debug-halted" },
169 { .value = TARGET_EVENT_DEBUG_RESUMED, .name = "debug-resumed" },
171 { .value = TARGET_EVENT_GDB_ATTACH, .name = "gdb-attach" },
172 { .value = TARGET_EVENT_GDB_DETACH, .name = "gdb-detach" },
174 { .value = TARGET_EVENT_GDB_FLASH_WRITE_START, .name = "gdb-flash-write-start" },
175 { .value = TARGET_EVENT_GDB_FLASH_WRITE_END , .name = "gdb-flash-write-end" },
177 { .value = TARGET_EVENT_GDB_FLASH_ERASE_START, .name = "gdb-flash-erase-start" },
178 { .value = TARGET_EVENT_GDB_FLASH_ERASE_END , .name = "gdb-flash-erase-end" },
180 { .value = TARGET_EVENT_RESUME_START, .name = "resume-start" },
181 { .value = TARGET_EVENT_RESUMED , .name = "resume-ok" },
182 { .value = TARGET_EVENT_RESUME_END , .name = "resume-end" },
184 { .name = NULL, .value = -1 }
187 static const Jim_Nvp nvp_target_state[] = {
188 { .name = "unknown", .value = TARGET_UNKNOWN },
189 { .name = "running", .value = TARGET_RUNNING },
190 { .name = "halted", .value = TARGET_HALTED },
191 { .name = "reset", .value = TARGET_RESET },
192 { .name = "debug-running", .value = TARGET_DEBUG_RUNNING },
193 { .name = NULL, .value = -1 },
196 static const Jim_Nvp nvp_target_debug_reason [] = {
197 { .name = "debug-request" , .value = DBG_REASON_DBGRQ },
198 { .name = "breakpoint" , .value = DBG_REASON_BREAKPOINT },
199 { .name = "watchpoint" , .value = DBG_REASON_WATCHPOINT },
200 { .name = "watchpoint-and-breakpoint", .value = DBG_REASON_WPTANDBKPT },
201 { .name = "single-step" , .value = DBG_REASON_SINGLESTEP },
202 { .name = "target-not-halted" , .value = DBG_REASON_NOTHALTED },
203 { .name = "undefined" , .value = DBG_REASON_UNDEFINED },
204 { .name = NULL, .value = -1 },
207 static const Jim_Nvp nvp_target_endian[] = {
208 { .name = "big", .value = TARGET_BIG_ENDIAN },
209 { .name = "little", .value = TARGET_LITTLE_ENDIAN },
210 { .name = "be", .value = TARGET_BIG_ENDIAN },
211 { .name = "le", .value = TARGET_LITTLE_ENDIAN },
212 { .name = NULL, .value = -1 },
215 static const Jim_Nvp nvp_reset_modes[] = {
216 { .name = "unknown", .value = RESET_UNKNOWN },
217 { .name = "run" , .value = RESET_RUN },
218 { .name = "halt" , .value = RESET_HALT },
219 { .name = "init" , .value = RESET_INIT },
220 { .name = NULL , .value = -1 },
223 const char *debug_reason_name(struct target *t)
227 cp = Jim_Nvp_value2name_simple(nvp_target_debug_reason,
228 t->debug_reason)->name;
230 LOG_ERROR("Invalid debug reason: %d", (int)(t->debug_reason));
231 cp = "(*BUG*unknown*BUG*)";
237 target_state_name( struct target *t )
240 cp = Jim_Nvp_value2name_simple(nvp_target_state, t->state)->name;
242 LOG_ERROR("Invalid target state: %d", (int)(t->state));
243 cp = "(*BUG*unknown*BUG*)";
248 /* determine the number of the new target */
249 static int new_target_number(void)
254 /* number is 0 based */
258 if (x < t->target_number) {
259 x = t->target_number;
266 /* read a uint32_t from a buffer in target memory endianness */
267 uint32_t target_buffer_get_u32(struct target *target, const uint8_t *buffer)
269 if (target->endianness == TARGET_LITTLE_ENDIAN)
270 return le_to_h_u32(buffer);
272 return be_to_h_u32(buffer);
275 /* read a uint16_t from a buffer in target memory endianness */
276 uint16_t target_buffer_get_u16(struct target *target, const uint8_t *buffer)
278 if (target->endianness == TARGET_LITTLE_ENDIAN)
279 return le_to_h_u16(buffer);
281 return be_to_h_u16(buffer);
284 /* read a uint8_t from a buffer in target memory endianness */
285 uint8_t target_buffer_get_u8(struct target *target, const uint8_t *buffer)
287 return *buffer & 0x0ff;
290 /* write a uint32_t to a buffer in target memory endianness */
291 void target_buffer_set_u32(struct target *target, uint8_t *buffer, uint32_t value)
293 if (target->endianness == TARGET_LITTLE_ENDIAN)
294 h_u32_to_le(buffer, value);
296 h_u32_to_be(buffer, value);
299 /* write a uint16_t to a buffer in target memory endianness */
300 void target_buffer_set_u16(struct target *target, uint8_t *buffer, uint16_t value)
302 if (target->endianness == TARGET_LITTLE_ENDIAN)
303 h_u16_to_le(buffer, value);
305 h_u16_to_be(buffer, value);
308 /* write a uint8_t to a buffer in target memory endianness */
309 void target_buffer_set_u8(struct target *target, uint8_t *buffer, uint8_t value)
314 /* return a pointer to a configured target; id is name or number */
315 struct target *get_target(const char *id)
317 struct target *target;
319 /* try as tcltarget name */
320 for (target = all_targets; target; target = target->next) {
321 if (target->cmd_name == NULL)
323 if (strcmp(id, target->cmd_name) == 0)
327 /* It's OK to remove this fallback sometime after August 2010 or so */
329 /* no match, try as number */
331 if (parse_uint(id, &num) != ERROR_OK)
334 for (target = all_targets; target; target = target->next) {
335 if (target->target_number == (int)num) {
336 LOG_WARNING("use '%s' as target identifier, not '%u'",
337 target->cmd_name, num);
345 /* returns a pointer to the n-th configured target */
346 static struct target *get_target_by_num(int num)
348 struct target *target = all_targets;
351 if (target->target_number == num) {
354 target = target->next;
360 struct target* get_current_target(struct command_context *cmd_ctx)
362 struct target *target = get_target_by_num(cmd_ctx->current_target);
366 LOG_ERROR("BUG: current_target out of bounds");
373 int target_poll(struct target *target)
377 /* We can't poll until after examine */
378 if (!target_was_examined(target))
380 /* Fail silently lest we pollute the log */
384 retval = target->type->poll(target);
385 if (retval != ERROR_OK)
388 if (target->halt_issued)
390 if (target->state == TARGET_HALTED)
392 target->halt_issued = false;
395 long long t = timeval_ms() - target->halt_issued_time;
398 target->halt_issued = false;
399 LOG_INFO("Halt timed out, wake up GDB.");
400 target_call_event_callbacks(target, TARGET_EVENT_GDB_HALT);
408 int target_halt(struct target *target)
411 /* We can't poll until after examine */
412 if (!target_was_examined(target))
414 LOG_ERROR("Target not examined yet");
418 retval = target->type->halt(target);
419 if (retval != ERROR_OK)
422 target->halt_issued = true;
423 target->halt_issued_time = timeval_ms();
429 * Make the target (re)start executing using its saved execution
430 * context (possibly with some modifications).
432 * @param target Which target should start executing.
433 * @param current True to use the target's saved program counter instead
434 * of the address parameter
435 * @param address Optionally used as the program counter.
436 * @param handle_breakpoints True iff breakpoints at the resumption PC
437 * should be skipped. (For example, maybe execution was stopped by
438 * such a breakpoint, in which case it would be counterprodutive to
440 * @param debug_execution False if all working areas allocated by OpenOCD
441 * should be released and/or restored to their original contents.
442 * (This would for example be true to run some downloaded "helper"
443 * algorithm code, which resides in one such working buffer and uses
444 * another for data storage.)
446 * @todo Resolve the ambiguity about what the "debug_execution" flag
447 * signifies. For example, Target implementations don't agree on how
448 * it relates to invalidation of the register cache, or to whether
449 * breakpoints and watchpoints should be enabled. (It would seem wrong
450 * to enable breakpoints when running downloaded "helper" algorithms
451 * (debug_execution true), since the breakpoints would be set to match
452 * target firmware being debugged, not the helper algorithm.... and
453 * enabling them could cause such helpers to malfunction (for example,
454 * by overwriting data with a breakpoint instruction. On the other
455 * hand the infrastructure for running such helpers might use this
456 * procedure but rely on hardware breakpoint to detect termination.)
458 int target_resume(struct target *target, int current, uint32_t address, int handle_breakpoints, int debug_execution)
462 /* We can't poll until after examine */
463 if (!target_was_examined(target))
465 LOG_ERROR("Target not examined yet");
469 /* note that resume *must* be asynchronous. The CPU can halt before
470 * we poll. The CPU can even halt at the current PC as a result of
471 * a software breakpoint being inserted by (a bug?) the application.
473 if ((retval = target->type->resume(target, current, address, handle_breakpoints, debug_execution)) != ERROR_OK)
476 /* Invalidate any cached protect/erase/... flash status, since
477 * almost all targets will now be able modify the flash by
478 * themselves. We want flash drivers and infrastructure to
479 * be able to rely on (non-invalidated) cached state.
481 * REVISIT do the same for NAND ; maybe other flash flavors too...
487 int target_process_reset(struct command_context *cmd_ctx, enum target_reset_mode reset_mode)
492 n = Jim_Nvp_value2name_simple(nvp_reset_modes, reset_mode);
493 if (n->name == NULL) {
494 LOG_ERROR("invalid reset mode");
498 /* disable polling during reset to make reset event scripts
499 * more predictable, i.e. dr/irscan & pathmove in events will
500 * not have JTAG operations injected into the middle of a sequence.
502 bool save_poll = jtag_poll_get_enabled();
504 jtag_poll_set_enabled(false);
506 sprintf(buf, "ocd_process_reset %s", n->name);
507 retval = Jim_Eval(cmd_ctx->interp, buf);
509 jtag_poll_set_enabled(save_poll);
511 if (retval != JIM_OK) {
512 Jim_PrintErrorMessage(cmd_ctx->interp);
516 /* We want any events to be processed before the prompt */
517 retval = target_call_timer_callbacks_now();
519 struct target *target;
520 for (target = all_targets; target; target = target->next) {
521 target->type->check_reset(target);
527 static int identity_virt2phys(struct target *target,
528 uint32_t virtual, uint32_t *physical)
534 static int no_mmu(struct target *target, int *enabled)
540 static int default_examine(struct target *target)
542 target_set_examined(target);
546 /* no check by default */
547 static int default_check_reset(struct target *target)
552 int target_examine_one(struct target *target)
554 return target->type->examine(target);
557 static int jtag_enable_callback(enum jtag_event event, void *priv)
559 struct target *target = priv;
561 if (event != JTAG_TAP_EVENT_ENABLE || !target->tap->enabled)
564 jtag_unregister_event_callback(jtag_enable_callback, target);
565 return target_examine_one(target);
569 /* Targets that correctly implement init + examine, i.e.
570 * no communication with target during init:
574 int target_examine(void)
576 int retval = ERROR_OK;
577 struct target *target;
579 for (target = all_targets; target; target = target->next)
581 /* defer examination, but don't skip it */
582 if (!target->tap->enabled) {
583 jtag_register_event_callback(jtag_enable_callback,
587 if ((retval = target_examine_one(target)) != ERROR_OK)
592 const char *target_type_name(struct target *target)
594 return target->type->name;
597 static int target_write_memory_imp(struct target *target, uint32_t address, uint32_t size, uint32_t count, uint8_t *buffer)
599 if (!target_was_examined(target))
601 LOG_ERROR("Target not examined yet");
604 return target->type->write_memory_imp(target, address, size, count, buffer);
607 static int target_read_memory_imp(struct target *target, uint32_t address, uint32_t size, uint32_t count, uint8_t *buffer)
609 if (!target_was_examined(target))
611 LOG_ERROR("Target not examined yet");
614 return target->type->read_memory_imp(target, address, size, count, buffer);
617 static int target_soft_reset_halt_imp(struct target *target)
619 if (!target_was_examined(target))
621 LOG_ERROR("Target not examined yet");
624 if (!target->type->soft_reset_halt_imp) {
625 LOG_ERROR("Target %s does not support soft_reset_halt",
626 target_name(target));
629 return target->type->soft_reset_halt_imp(target);
632 static int target_run_algorithm_imp(struct target *target, int num_mem_params, struct mem_param *mem_params, int num_reg_params, struct reg_param *reg_param, uint32_t entry_point, uint32_t exit_point, int timeout_ms, void *arch_info)
634 if (!target_was_examined(target))
636 LOG_ERROR("Target not examined yet");
639 return target->type->run_algorithm_imp(target, num_mem_params, mem_params, num_reg_params, reg_param, entry_point, exit_point, timeout_ms, arch_info);
642 int target_read_memory(struct target *target,
643 uint32_t address, uint32_t size, uint32_t count, uint8_t *buffer)
645 return target->type->read_memory(target, address, size, count, buffer);
648 int target_read_phys_memory(struct target *target,
649 uint32_t address, uint32_t size, uint32_t count, uint8_t *buffer)
651 return target->type->read_phys_memory(target, address, size, count, buffer);
654 int target_write_memory(struct target *target,
655 uint32_t address, uint32_t size, uint32_t count, uint8_t *buffer)
657 return target->type->write_memory(target, address, size, count, buffer);
660 int target_write_phys_memory(struct target *target,
661 uint32_t address, uint32_t size, uint32_t count, uint8_t *buffer)
663 return target->type->write_phys_memory(target, address, size, count, buffer);
666 int target_bulk_write_memory(struct target *target,
667 uint32_t address, uint32_t count, uint8_t *buffer)
669 return target->type->bulk_write_memory(target, address, count, buffer);
672 int target_add_breakpoint(struct target *target,
673 struct breakpoint *breakpoint)
675 if (target->state != TARGET_HALTED) {
676 LOG_WARNING("target %s is not halted", target->cmd_name);
677 return ERROR_TARGET_NOT_HALTED;
679 return target->type->add_breakpoint(target, breakpoint);
681 int target_remove_breakpoint(struct target *target,
682 struct breakpoint *breakpoint)
684 return target->type->remove_breakpoint(target, breakpoint);
687 int target_add_watchpoint(struct target *target,
688 struct watchpoint *watchpoint)
690 if (target->state != TARGET_HALTED) {
691 LOG_WARNING("target %s is not halted", target->cmd_name);
692 return ERROR_TARGET_NOT_HALTED;
694 return target->type->add_watchpoint(target, watchpoint);
696 int target_remove_watchpoint(struct target *target,
697 struct watchpoint *watchpoint)
699 return target->type->remove_watchpoint(target, watchpoint);
702 int target_get_gdb_reg_list(struct target *target,
703 struct reg **reg_list[], int *reg_list_size)
705 return target->type->get_gdb_reg_list(target, reg_list, reg_list_size);
707 int target_step(struct target *target,
708 int current, uint32_t address, int handle_breakpoints)
710 return target->type->step(target, current, address, handle_breakpoints);
714 int target_run_algorithm(struct target *target,
715 int num_mem_params, struct mem_param *mem_params,
716 int num_reg_params, struct reg_param *reg_param,
717 uint32_t entry_point, uint32_t exit_point,
718 int timeout_ms, void *arch_info)
720 return target->type->run_algorithm(target,
721 num_mem_params, mem_params, num_reg_params, reg_param,
722 entry_point, exit_point, timeout_ms, arch_info);
726 * Reset the @c examined flag for the given target.
727 * Pure paranoia -- targets are zeroed on allocation.
729 static void target_reset_examined(struct target *target)
731 target->examined = false;
735 err_read_phys_memory(struct target *target, uint32_t address,
736 uint32_t size, uint32_t count, uint8_t *buffer)
738 LOG_ERROR("Not implemented: %s", __func__);
743 err_write_phys_memory(struct target *target, uint32_t address,
744 uint32_t size, uint32_t count, uint8_t *buffer)
746 LOG_ERROR("Not implemented: %s", __func__);
750 static int handle_target(void *priv);
752 static int target_init_one(struct command_context *cmd_ctx,
753 struct target *target)
755 target_reset_examined(target);
757 struct target_type *type = target->type;
758 if (type->examine == NULL)
759 type->examine = default_examine;
761 if (type->check_reset== NULL)
762 type->check_reset = default_check_reset;
764 int retval = type->init_target(cmd_ctx, target);
765 if (ERROR_OK != retval)
767 LOG_ERROR("target '%s' init failed", target_name(target));
772 * @todo get rid of those *memory_imp() methods, now that all
773 * callers are using target_*_memory() accessors ... and make
774 * sure the "physical" paths handle the same issues.
776 /* a non-invasive way(in terms of patches) to add some code that
777 * runs before the type->write/read_memory implementation
779 type->write_memory_imp = target->type->write_memory;
780 type->write_memory = target_write_memory_imp;
782 type->read_memory_imp = target->type->read_memory;
783 type->read_memory = target_read_memory_imp;
785 type->soft_reset_halt_imp = target->type->soft_reset_halt;
786 type->soft_reset_halt = target_soft_reset_halt_imp;
788 type->run_algorithm_imp = target->type->run_algorithm;
789 type->run_algorithm = target_run_algorithm_imp;
791 /* Sanity-check MMU support ... stub in what we must, to help
792 * implement it in stages, but warn if we need to do so.
796 if (type->write_phys_memory == NULL)
798 LOG_ERROR("type '%s' is missing write_phys_memory",
800 type->write_phys_memory = err_write_phys_memory;
802 if (type->read_phys_memory == NULL)
804 LOG_ERROR("type '%s' is missing read_phys_memory",
806 type->read_phys_memory = err_read_phys_memory;
808 if (type->virt2phys == NULL)
810 LOG_ERROR("type '%s' is missing virt2phys", type->name);
811 type->virt2phys = identity_virt2phys;
816 /* Make sure no-MMU targets all behave the same: make no
817 * distinction between physical and virtual addresses, and
818 * ensure that virt2phys() is always an identity mapping.
820 if (type->write_phys_memory || type->read_phys_memory
823 LOG_WARNING("type '%s' has bad MMU hooks", type->name);
827 type->write_phys_memory = type->write_memory;
828 type->read_phys_memory = type->read_memory;
829 type->virt2phys = identity_virt2phys;
834 int target_init(struct command_context *cmd_ctx)
836 struct target *target;
839 for (target = all_targets; target; target = target->next)
841 retval = target_init_one(cmd_ctx, target);
842 if (ERROR_OK != retval)
849 retval = target_register_user_commands(cmd_ctx);
850 if (ERROR_OK != retval)
853 retval = target_register_timer_callback(&handle_target,
854 100, 1, cmd_ctx->interp);
855 if (ERROR_OK != retval)
861 COMMAND_HANDLER(handle_target_init_command)
864 return ERROR_COMMAND_SYNTAX_ERROR;
866 static bool target_initialized = false;
867 if (target_initialized)
869 LOG_INFO("'target init' has already been called");
872 target_initialized = true;
874 LOG_DEBUG("Initializing targets...");
875 return target_init(CMD_CTX);
878 int target_register_event_callback(int (*callback)(struct target *target, enum target_event event, void *priv), void *priv)
880 struct target_event_callback **callbacks_p = &target_event_callbacks;
882 if (callback == NULL)
884 return ERROR_INVALID_ARGUMENTS;
889 while ((*callbacks_p)->next)
890 callbacks_p = &((*callbacks_p)->next);
891 callbacks_p = &((*callbacks_p)->next);
894 (*callbacks_p) = malloc(sizeof(struct target_event_callback));
895 (*callbacks_p)->callback = callback;
896 (*callbacks_p)->priv = priv;
897 (*callbacks_p)->next = NULL;
902 int target_register_timer_callback(int (*callback)(void *priv), int time_ms, int periodic, void *priv)
904 struct target_timer_callback **callbacks_p = &target_timer_callbacks;
907 if (callback == NULL)
909 return ERROR_INVALID_ARGUMENTS;
914 while ((*callbacks_p)->next)
915 callbacks_p = &((*callbacks_p)->next);
916 callbacks_p = &((*callbacks_p)->next);
919 (*callbacks_p) = malloc(sizeof(struct target_timer_callback));
920 (*callbacks_p)->callback = callback;
921 (*callbacks_p)->periodic = periodic;
922 (*callbacks_p)->time_ms = time_ms;
924 gettimeofday(&now, NULL);
925 (*callbacks_p)->when.tv_usec = now.tv_usec + (time_ms % 1000) * 1000;
926 time_ms -= (time_ms % 1000);
927 (*callbacks_p)->when.tv_sec = now.tv_sec + (time_ms / 1000);
928 if ((*callbacks_p)->when.tv_usec > 1000000)
930 (*callbacks_p)->when.tv_usec = (*callbacks_p)->when.tv_usec - 1000000;
931 (*callbacks_p)->when.tv_sec += 1;
934 (*callbacks_p)->priv = priv;
935 (*callbacks_p)->next = NULL;
940 int target_unregister_event_callback(int (*callback)(struct target *target, enum target_event event, void *priv), void *priv)
942 struct target_event_callback **p = &target_event_callbacks;
943 struct target_event_callback *c = target_event_callbacks;
945 if (callback == NULL)
947 return ERROR_INVALID_ARGUMENTS;
952 struct target_event_callback *next = c->next;
953 if ((c->callback == callback) && (c->priv == priv))
967 int target_unregister_timer_callback(int (*callback)(void *priv), void *priv)
969 struct target_timer_callback **p = &target_timer_callbacks;
970 struct target_timer_callback *c = target_timer_callbacks;
972 if (callback == NULL)
974 return ERROR_INVALID_ARGUMENTS;
979 struct target_timer_callback *next = c->next;
980 if ((c->callback == callback) && (c->priv == priv))
994 int target_call_event_callbacks(struct target *target, enum target_event event)
996 struct target_event_callback *callback = target_event_callbacks;
997 struct target_event_callback *next_callback;
999 if (event == TARGET_EVENT_HALTED)
1001 /* execute early halted first */
1002 target_call_event_callbacks(target, TARGET_EVENT_GDB_HALT);
1005 LOG_DEBUG("target event %i (%s)",
1007 Jim_Nvp_value2name_simple(nvp_target_event, event)->name);
1009 target_handle_event(target, event);
1013 next_callback = callback->next;
1014 callback->callback(target, event, callback->priv);
1015 callback = next_callback;
1021 static int target_timer_callback_periodic_restart(
1022 struct target_timer_callback *cb, struct timeval *now)
1024 int time_ms = cb->time_ms;
1025 cb->when.tv_usec = now->tv_usec + (time_ms % 1000) * 1000;
1026 time_ms -= (time_ms % 1000);
1027 cb->when.tv_sec = now->tv_sec + time_ms / 1000;
1028 if (cb->when.tv_usec > 1000000)
1030 cb->when.tv_usec = cb->when.tv_usec - 1000000;
1031 cb->when.tv_sec += 1;
1036 static int target_call_timer_callback(struct target_timer_callback *cb,
1037 struct timeval *now)
1039 cb->callback(cb->priv);
1042 return target_timer_callback_periodic_restart(cb, now);
1044 return target_unregister_timer_callback(cb->callback, cb->priv);
1047 static int target_call_timer_callbacks_check_time(int checktime)
1052 gettimeofday(&now, NULL);
1054 struct target_timer_callback *callback = target_timer_callbacks;
1057 // cleaning up may unregister and free this callback
1058 struct target_timer_callback *next_callback = callback->next;
1060 bool call_it = callback->callback &&
1061 ((!checktime && callback->periodic) ||
1062 now.tv_sec > callback->when.tv_sec ||
1063 (now.tv_sec == callback->when.tv_sec &&
1064 now.tv_usec >= callback->when.tv_usec));
1068 int retval = target_call_timer_callback(callback, &now);
1069 if (retval != ERROR_OK)
1073 callback = next_callback;
1079 int target_call_timer_callbacks(void)
1081 return target_call_timer_callbacks_check_time(1);
1084 /* invoke periodic callbacks immediately */
1085 int target_call_timer_callbacks_now(void)
1087 return target_call_timer_callbacks_check_time(0);
1090 int target_alloc_working_area(struct target *target, uint32_t size, struct working_area **area)
1092 struct working_area *c = target->working_areas;
1093 struct working_area *new_wa = NULL;
1095 /* Reevaluate working area address based on MMU state*/
1096 if (target->working_areas == NULL)
1101 retval = target->type->mmu(target, &enabled);
1102 if (retval != ERROR_OK)
1108 if (target->working_area_phys_spec) {
1109 LOG_DEBUG("MMU disabled, using physical "
1110 "address for working memory 0x%08x",
1111 (unsigned)target->working_area_phys);
1112 target->working_area = target->working_area_phys;
1114 LOG_ERROR("No working memory available. "
1115 "Specify -work-area-phys to target.");
1116 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
1119 if (target->working_area_virt_spec) {
1120 LOG_DEBUG("MMU enabled, using virtual "
1121 "address for working memory 0x%08x",
1122 (unsigned)target->working_area_virt);
1123 target->working_area = target->working_area_virt;
1125 LOG_ERROR("No working memory available. "
1126 "Specify -work-area-virt to target.");
1127 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
1132 /* only allocate multiples of 4 byte */
1135 LOG_ERROR("BUG: code tried to allocate unaligned number of bytes (0x%08x), padding", ((unsigned)(size)));
1136 size = (size + 3) & (~3);
1139 /* see if there's already a matching working area */
1142 if ((c->free) && (c->size == size))
1150 /* if not, allocate a new one */
1153 struct working_area **p = &target->working_areas;
1154 uint32_t first_free = target->working_area;
1155 uint32_t free_size = target->working_area_size;
1157 c = target->working_areas;
1160 first_free += c->size;
1161 free_size -= c->size;
1166 if (free_size < size)
1168 LOG_WARNING("not enough working area available(requested %u, free %u)",
1169 (unsigned)(size), (unsigned)(free_size));
1170 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
1173 LOG_DEBUG("allocated new working area at address 0x%08x", (unsigned)first_free);
1175 new_wa = malloc(sizeof(struct working_area));
1176 new_wa->next = NULL;
1177 new_wa->size = size;
1178 new_wa->address = first_free;
1180 if (target->backup_working_area)
1183 new_wa->backup = malloc(new_wa->size);
1184 if ((retval = target_read_memory(target, new_wa->address, 4, new_wa->size / 4, new_wa->backup)) != ERROR_OK)
1186 free(new_wa->backup);
1193 new_wa->backup = NULL;
1196 /* put new entry in list */
1200 /* mark as used, and return the new (reused) area */
1205 new_wa->user = area;
1210 int target_free_working_area_restore(struct target *target, struct working_area *area, int restore)
1215 if (restore && target->backup_working_area)
1218 if ((retval = target_write_memory(target, area->address, 4, area->size / 4, area->backup)) != ERROR_OK)
1224 /* mark user pointer invalid */
1231 int target_free_working_area(struct target *target, struct working_area *area)
1233 return target_free_working_area_restore(target, area, 1);
1236 /* free resources and restore memory, if restoring memory fails,
1237 * free up resources anyway
1239 void target_free_all_working_areas_restore(struct target *target, int restore)
1241 struct working_area *c = target->working_areas;
1245 struct working_area *next = c->next;
1246 target_free_working_area_restore(target, c, restore);
1256 target->working_areas = NULL;
1259 void target_free_all_working_areas(struct target *target)
1261 target_free_all_working_areas_restore(target, 1);
1264 int target_arch_state(struct target *target)
1269 LOG_USER("No target has been configured");
1273 LOG_USER("target state: %s", target_state_name( target ));
1275 if (target->state != TARGET_HALTED)
1278 retval = target->type->arch_state(target);
1282 /* Single aligned words are guaranteed to use 16 or 32 bit access
1283 * mode respectively, otherwise data is handled as quickly as
1286 int target_write_buffer(struct target *target, uint32_t address, uint32_t size, uint8_t *buffer)
1289 LOG_DEBUG("writing buffer of %i byte at 0x%8.8x",
1290 (int)size, (unsigned)address);
1292 if (!target_was_examined(target))
1294 LOG_ERROR("Target not examined yet");
1302 if ((address + size - 1) < address)
1304 /* GDB can request this when e.g. PC is 0xfffffffc*/
1305 LOG_ERROR("address + size wrapped(0x%08x, 0x%08x)",
1311 if (((address % 2) == 0) && (size == 2))
1313 return target_write_memory(target, address, 2, 1, buffer);
1316 /* handle unaligned head bytes */
1319 uint32_t unaligned = 4 - (address % 4);
1321 if (unaligned > size)
1324 if ((retval = target_write_memory(target, address, 1, unaligned, buffer)) != ERROR_OK)
1327 buffer += unaligned;
1328 address += unaligned;
1332 /* handle aligned words */
1335 int aligned = size - (size % 4);
1337 /* use bulk writes above a certain limit. This may have to be changed */
1340 if ((retval = target->type->bulk_write_memory(target, address, aligned / 4, buffer)) != ERROR_OK)
1345 if ((retval = target_write_memory(target, address, 4, aligned / 4, buffer)) != ERROR_OK)
1354 /* handle tail writes of less than 4 bytes */
1357 if ((retval = target_write_memory(target, address, 1, size, buffer)) != ERROR_OK)
1364 /* Single aligned words are guaranteed to use 16 or 32 bit access
1365 * mode respectively, otherwise data is handled as quickly as
1368 int target_read_buffer(struct target *target, uint32_t address, uint32_t size, uint8_t *buffer)
1371 LOG_DEBUG("reading buffer of %i byte at 0x%8.8x",
1372 (int)size, (unsigned)address);
1374 if (!target_was_examined(target))
1376 LOG_ERROR("Target not examined yet");
1384 if ((address + size - 1) < address)
1386 /* GDB can request this when e.g. PC is 0xfffffffc*/
1387 LOG_ERROR("address + size wrapped(0x%08" PRIx32 ", 0x%08" PRIx32 ")",
1393 if (((address % 2) == 0) && (size == 2))
1395 return target_read_memory(target, address, 2, 1, buffer);
1398 /* handle unaligned head bytes */
1401 uint32_t unaligned = 4 - (address % 4);
1403 if (unaligned > size)
1406 if ((retval = target_read_memory(target, address, 1, unaligned, buffer)) != ERROR_OK)
1409 buffer += unaligned;
1410 address += unaligned;
1414 /* handle aligned words */
1417 int aligned = size - (size % 4);
1419 if ((retval = target_read_memory(target, address, 4, aligned / 4, buffer)) != ERROR_OK)
1427 /*prevent byte access when possible (avoid AHB access limitations in some cases)*/
1430 int aligned = size - (size%2);
1431 retval = target_read_memory(target, address, 2, aligned / 2, buffer);
1432 if (retval != ERROR_OK)
1439 /* handle tail writes of less than 4 bytes */
1442 if ((retval = target_read_memory(target, address, 1, size, buffer)) != ERROR_OK)
1449 int target_checksum_memory(struct target *target, uint32_t address, uint32_t size, uint32_t* crc)
1454 uint32_t checksum = 0;
1455 if (!target_was_examined(target))
1457 LOG_ERROR("Target not examined yet");
1461 if ((retval = target->type->checksum_memory(target, address,
1462 size, &checksum)) != ERROR_OK)
1464 buffer = malloc(size);
1467 LOG_ERROR("error allocating buffer for section (%d bytes)", (int)size);
1468 return ERROR_INVALID_ARGUMENTS;
1470 retval = target_read_buffer(target, address, size, buffer);
1471 if (retval != ERROR_OK)
1477 /* convert to target endianess */
1478 for (i = 0; i < (size/sizeof(uint32_t)); i++)
1480 uint32_t target_data;
1481 target_data = target_buffer_get_u32(target, &buffer[i*sizeof(uint32_t)]);
1482 target_buffer_set_u32(target, &buffer[i*sizeof(uint32_t)], target_data);
1485 retval = image_calculate_checksum(buffer, size, &checksum);
1494 int target_blank_check_memory(struct target *target, uint32_t address, uint32_t size, uint32_t* blank)
1497 if (!target_was_examined(target))
1499 LOG_ERROR("Target not examined yet");
1503 if (target->type->blank_check_memory == 0)
1504 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
1506 retval = target->type->blank_check_memory(target, address, size, blank);
1511 int target_read_u32(struct target *target, uint32_t address, uint32_t *value)
1513 uint8_t value_buf[4];
1514 if (!target_was_examined(target))
1516 LOG_ERROR("Target not examined yet");
1520 int retval = target_read_memory(target, address, 4, 1, value_buf);
1522 if (retval == ERROR_OK)
1524 *value = target_buffer_get_u32(target, value_buf);
1525 LOG_DEBUG("address: 0x%8.8" PRIx32 ", value: 0x%8.8" PRIx32 "",
1532 LOG_DEBUG("address: 0x%8.8" PRIx32 " failed",
1539 int target_read_u16(struct target *target, uint32_t address, uint16_t *value)
1541 uint8_t value_buf[2];
1542 if (!target_was_examined(target))
1544 LOG_ERROR("Target not examined yet");
1548 int retval = target_read_memory(target, address, 2, 1, value_buf);
1550 if (retval == ERROR_OK)
1552 *value = target_buffer_get_u16(target, value_buf);
1553 LOG_DEBUG("address: 0x%8.8" PRIx32 ", value: 0x%4.4x",
1560 LOG_DEBUG("address: 0x%8.8" PRIx32 " failed",
1567 int target_read_u8(struct target *target, uint32_t address, uint8_t *value)
1569 int retval = target_read_memory(target, address, 1, 1, value);
1570 if (!target_was_examined(target))
1572 LOG_ERROR("Target not examined yet");
1576 if (retval == ERROR_OK)
1578 LOG_DEBUG("address: 0x%8.8" PRIx32 ", value: 0x%2.2x",
1585 LOG_DEBUG("address: 0x%8.8" PRIx32 " failed",
1592 int target_write_u32(struct target *target, uint32_t address, uint32_t value)
1595 uint8_t value_buf[4];
1596 if (!target_was_examined(target))
1598 LOG_ERROR("Target not examined yet");
1602 LOG_DEBUG("address: 0x%8.8" PRIx32 ", value: 0x%8.8" PRIx32 "",
1606 target_buffer_set_u32(target, value_buf, value);
1607 if ((retval = target_write_memory(target, address, 4, 1, value_buf)) != ERROR_OK)
1609 LOG_DEBUG("failed: %i", retval);
1615 int target_write_u16(struct target *target, uint32_t address, uint16_t value)
1618 uint8_t value_buf[2];
1619 if (!target_was_examined(target))
1621 LOG_ERROR("Target not examined yet");
1625 LOG_DEBUG("address: 0x%8.8" PRIx32 ", value: 0x%8.8x",
1629 target_buffer_set_u16(target, value_buf, value);
1630 if ((retval = target_write_memory(target, address, 2, 1, value_buf)) != ERROR_OK)
1632 LOG_DEBUG("failed: %i", retval);
1638 int target_write_u8(struct target *target, uint32_t address, uint8_t value)
1641 if (!target_was_examined(target))
1643 LOG_ERROR("Target not examined yet");
1647 LOG_DEBUG("address: 0x%8.8" PRIx32 ", value: 0x%2.2x",
1650 if ((retval = target_write_memory(target, address, 1, 1, &value)) != ERROR_OK)
1652 LOG_DEBUG("failed: %i", retval);
1658 COMMAND_HANDLER(handle_targets_command)
1660 struct target *target = all_targets;
1664 target = get_target(CMD_ARGV[0]);
1665 if (target == NULL) {
1666 command_print(CMD_CTX,"Target: %s is unknown, try one of:\n", CMD_ARGV[0]);
1669 if (!target->tap->enabled) {
1670 command_print(CMD_CTX,"Target: TAP %s is disabled, "
1671 "can't be the current target\n",
1672 target->tap->dotted_name);
1676 CMD_CTX->current_target = target->target_number;
1681 target = all_targets;
1682 command_print(CMD_CTX, " TargetName Type Endian TapName State ");
1683 command_print(CMD_CTX, "-- ------------------ ---------- ------ ------------------ ------------");
1689 if (target->tap->enabled)
1690 state = target_state_name( target );
1692 state = "tap-disabled";
1694 if (CMD_CTX->current_target == target->target_number)
1697 /* keep columns lined up to match the headers above */
1698 command_print(CMD_CTX, "%2d%c %-18s %-10s %-6s %-18s %s",
1699 target->target_number,
1701 target_name(target),
1702 target_type_name(target),
1703 Jim_Nvp_value2name_simple(nvp_target_endian,
1704 target->endianness)->name,
1705 target->tap->dotted_name,
1707 target = target->next;
1713 /* every 300ms we check for reset & powerdropout and issue a "reset halt" if so. */
1715 static int powerDropout;
1716 static int srstAsserted;
1718 static int runPowerRestore;
1719 static int runPowerDropout;
1720 static int runSrstAsserted;
1721 static int runSrstDeasserted;
1723 static int sense_handler(void)
1725 static int prevSrstAsserted = 0;
1726 static int prevPowerdropout = 0;
1729 if ((retval = jtag_power_dropout(&powerDropout)) != ERROR_OK)
1733 powerRestored = prevPowerdropout && !powerDropout;
1736 runPowerRestore = 1;
1739 long long current = timeval_ms();
1740 static long long lastPower = 0;
1741 int waitMore = lastPower + 2000 > current;
1742 if (powerDropout && !waitMore)
1744 runPowerDropout = 1;
1745 lastPower = current;
1748 if ((retval = jtag_srst_asserted(&srstAsserted)) != ERROR_OK)
1752 srstDeasserted = prevSrstAsserted && !srstAsserted;
1754 static long long lastSrst = 0;
1755 waitMore = lastSrst + 2000 > current;
1756 if (srstDeasserted && !waitMore)
1758 runSrstDeasserted = 1;
1762 if (!prevSrstAsserted && srstAsserted)
1764 runSrstAsserted = 1;
1767 prevSrstAsserted = srstAsserted;
1768 prevPowerdropout = powerDropout;
1770 if (srstDeasserted || powerRestored)
1772 /* Other than logging the event we can't do anything here.
1773 * Issuing a reset is a particularly bad idea as we might
1774 * be inside a reset already.
1781 /* process target state changes */
1782 static int handle_target(void *priv)
1784 Jim_Interp *interp = (Jim_Interp *)priv;
1785 int retval = ERROR_OK;
1787 /* we do not want to recurse here... */
1788 static int recursive = 0;
1793 /* danger! running these procedures can trigger srst assertions and power dropouts.
1794 * We need to avoid an infinite loop/recursion here and we do that by
1795 * clearing the flags after running these events.
1797 int did_something = 0;
1798 if (runSrstAsserted)
1800 LOG_INFO("srst asserted detected, running srst_asserted proc.");
1801 Jim_Eval(interp, "srst_asserted");
1804 if (runSrstDeasserted)
1806 Jim_Eval(interp, "srst_deasserted");
1809 if (runPowerDropout)
1811 LOG_INFO("Power dropout detected, running power_dropout proc.");
1812 Jim_Eval(interp, "power_dropout");
1815 if (runPowerRestore)
1817 Jim_Eval(interp, "power_restore");
1823 /* clear detect flags */
1827 /* clear action flags */
1829 runSrstAsserted = 0;
1830 runSrstDeasserted = 0;
1831 runPowerRestore = 0;
1832 runPowerDropout = 0;
1837 /* Poll targets for state changes unless that's globally disabled.
1838 * Skip targets that are currently disabled.
1840 for (struct target *target = all_targets;
1841 is_jtag_poll_safe() && target;
1842 target = target->next)
1844 if (!target->tap->enabled)
1847 /* only poll target if we've got power and srst isn't asserted */
1848 if (!powerDropout && !srstAsserted)
1850 /* polling may fail silently until the target has been examined */
1851 if ((retval = target_poll(target)) != ERROR_OK)
1853 /* FIX!!!!! If we add a LOG_INFO() here to output a line in GDB
1854 * *why* we are aborting GDB, then we'll spam telnet when the
1855 * poll is failing persistently.
1857 * If we could implement an event that detected the
1858 * target going from non-pollable to pollable, we could issue
1859 * an error only upon the transition.
1861 target_call_event_callbacks(target, TARGET_EVENT_GDB_HALT);
1870 COMMAND_HANDLER(handle_reg_command)
1872 struct target *target;
1873 struct reg *reg = NULL;
1879 target = get_current_target(CMD_CTX);
1881 /* list all available registers for the current target */
1884 struct reg_cache *cache = target->reg_cache;
1891 command_print(CMD_CTX, "===== %s", cache->name);
1893 for (i = 0, reg = cache->reg_list;
1894 i < cache->num_regs;
1895 i++, reg++, count++)
1897 /* only print cached values if they are valid */
1899 value = buf_to_str(reg->value,
1901 command_print(CMD_CTX,
1902 "(%i) %s (/%" PRIu32 "): 0x%s%s",
1910 command_print(CMD_CTX, "(%i) %s (/%" PRIu32 ")",
1915 cache = cache->next;
1921 /* access a single register by its ordinal number */
1922 if ((CMD_ARGV[0][0] >= '0') && (CMD_ARGV[0][0] <= '9'))
1925 COMMAND_PARSE_NUMBER(uint, CMD_ARGV[0], num);
1927 struct reg_cache *cache = target->reg_cache;
1932 for (i = 0; i < cache->num_regs; i++)
1936 reg = &cache->reg_list[i];
1942 cache = cache->next;
1947 command_print(CMD_CTX, "%i is out of bounds, the current target has only %i registers (0 - %i)", num, count, count - 1);
1950 } else /* access a single register by its name */
1952 reg = register_get_by_name(target->reg_cache, CMD_ARGV[0], 1);
1956 command_print(CMD_CTX, "register %s not found in current target", CMD_ARGV[0]);
1961 /* display a register */
1962 if ((CMD_ARGC == 1) || ((CMD_ARGC == 2) && !((CMD_ARGV[1][0] >= '0') && (CMD_ARGV[1][0] <= '9'))))
1964 if ((CMD_ARGC == 2) && (strcmp(CMD_ARGV[1], "force") == 0))
1967 if (reg->valid == 0)
1969 reg->type->get(reg);
1971 value = buf_to_str(reg->value, reg->size, 16);
1972 command_print(CMD_CTX, "%s (/%i): 0x%s", reg->name, (int)(reg->size), value);
1977 /* set register value */
1980 uint8_t *buf = malloc(DIV_ROUND_UP(reg->size, 8));
1981 str_to_buf(CMD_ARGV[1], strlen(CMD_ARGV[1]), buf, reg->size, 0);
1983 reg->type->set(reg, buf);
1985 value = buf_to_str(reg->value, reg->size, 16);
1986 command_print(CMD_CTX, "%s (/%i): 0x%s", reg->name, (int)(reg->size), value);
1994 command_print(CMD_CTX, "usage: reg <#|name> [value]");
1999 COMMAND_HANDLER(handle_poll_command)
2001 int retval = ERROR_OK;
2002 struct target *target = get_current_target(CMD_CTX);
2006 command_print(CMD_CTX, "background polling: %s",
2007 jtag_poll_get_enabled() ? "on" : "off");
2008 command_print(CMD_CTX, "TAP: %s (%s)",
2009 target->tap->dotted_name,
2010 target->tap->enabled ? "enabled" : "disabled");
2011 if (!target->tap->enabled)
2013 if ((retval = target_poll(target)) != ERROR_OK)
2015 if ((retval = target_arch_state(target)) != ERROR_OK)
2018 else if (CMD_ARGC == 1)
2021 COMMAND_PARSE_ON_OFF(CMD_ARGV[0], enable);
2022 jtag_poll_set_enabled(enable);
2026 return ERROR_COMMAND_SYNTAX_ERROR;
2032 COMMAND_HANDLER(handle_wait_halt_command)
2035 return ERROR_COMMAND_SYNTAX_ERROR;
2040 int retval = parse_uint(CMD_ARGV[0], &ms);
2041 if (ERROR_OK != retval)
2043 command_print(CMD_CTX, "usage: %s [seconds]", CMD_NAME);
2044 return ERROR_COMMAND_SYNTAX_ERROR;
2046 // convert seconds (given) to milliseconds (needed)
2050 struct target *target = get_current_target(CMD_CTX);
2051 return target_wait_state(target, TARGET_HALTED, ms);
2054 /* wait for target state to change. The trick here is to have a low
2055 * latency for short waits and not to suck up all the CPU time
2058 * After 500ms, keep_alive() is invoked
2060 int target_wait_state(struct target *target, enum target_state state, int ms)
2063 long long then = 0, cur;
2068 if ((retval = target_poll(target)) != ERROR_OK)
2070 if (target->state == state)
2078 then = timeval_ms();
2079 LOG_DEBUG("waiting for target %s...",
2080 Jim_Nvp_value2name_simple(nvp_target_state,state)->name);
2088 if ((cur-then) > ms)
2090 LOG_ERROR("timed out while waiting for target %s",
2091 Jim_Nvp_value2name_simple(nvp_target_state,state)->name);
2099 COMMAND_HANDLER(handle_halt_command)
2103 struct target *target = get_current_target(CMD_CTX);
2104 int retval = target_halt(target);
2105 if (ERROR_OK != retval)
2111 retval = parse_uint(CMD_ARGV[0], &wait);
2112 if (ERROR_OK != retval)
2113 return ERROR_COMMAND_SYNTAX_ERROR;
2118 return CALL_COMMAND_HANDLER(handle_wait_halt_command);
2121 COMMAND_HANDLER(handle_soft_reset_halt_command)
2123 struct target *target = get_current_target(CMD_CTX);
2125 LOG_USER("requesting target halt and executing a soft reset");
2127 target->type->soft_reset_halt(target);
2132 COMMAND_HANDLER(handle_reset_command)
2135 return ERROR_COMMAND_SYNTAX_ERROR;
2137 enum target_reset_mode reset_mode = RESET_RUN;
2141 n = Jim_Nvp_name2value_simple(nvp_reset_modes, CMD_ARGV[0]);
2142 if ((n->name == NULL) || (n->value == RESET_UNKNOWN)) {
2143 return ERROR_COMMAND_SYNTAX_ERROR;
2145 reset_mode = n->value;
2148 /* reset *all* targets */
2149 return target_process_reset(CMD_CTX, reset_mode);
2153 COMMAND_HANDLER(handle_resume_command)
2157 return ERROR_COMMAND_SYNTAX_ERROR;
2159 struct target *target = get_current_target(CMD_CTX);
2160 target_handle_event(target, TARGET_EVENT_OLD_pre_resume);
2162 /* with no CMD_ARGV, resume from current pc, addr = 0,
2163 * with one arguments, addr = CMD_ARGV[0],
2164 * handle breakpoints, not debugging */
2168 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], addr);
2172 return target_resume(target, current, addr, 1, 0);
2175 COMMAND_HANDLER(handle_step_command)
2178 return ERROR_COMMAND_SYNTAX_ERROR;
2182 /* with no CMD_ARGV, step from current pc, addr = 0,
2183 * with one argument addr = CMD_ARGV[0],
2184 * handle breakpoints, debugging */
2189 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], addr);
2193 struct target *target = get_current_target(CMD_CTX);
2195 return target->type->step(target, current_pc, addr, 1);
2198 static void handle_md_output(struct command_context *cmd_ctx,
2199 struct target *target, uint32_t address, unsigned size,
2200 unsigned count, const uint8_t *buffer)
2202 const unsigned line_bytecnt = 32;
2203 unsigned line_modulo = line_bytecnt / size;
2205 char output[line_bytecnt * 4 + 1];
2206 unsigned output_len = 0;
2208 const char *value_fmt;
2210 case 4: value_fmt = "%8.8x "; break;
2211 case 2: value_fmt = "%4.4x "; break;
2212 case 1: value_fmt = "%2.2x "; break;
2214 /* "can't happen", caller checked */
2215 LOG_ERROR("invalid memory read size: %u", size);
2219 for (unsigned i = 0; i < count; i++)
2221 if (i % line_modulo == 0)
2223 output_len += snprintf(output + output_len,
2224 sizeof(output) - output_len,
2226 (unsigned)(address + (i*size)));
2230 const uint8_t *value_ptr = buffer + i * size;
2232 case 4: value = target_buffer_get_u32(target, value_ptr); break;
2233 case 2: value = target_buffer_get_u16(target, value_ptr); break;
2234 case 1: value = *value_ptr;
2236 output_len += snprintf(output + output_len,
2237 sizeof(output) - output_len,
2240 if ((i % line_modulo == line_modulo - 1) || (i == count - 1))
2242 command_print(cmd_ctx, "%s", output);
2248 COMMAND_HANDLER(handle_md_command)
2251 return ERROR_COMMAND_SYNTAX_ERROR;
2254 switch (CMD_NAME[2]) {
2255 case 'w': size = 4; break;
2256 case 'h': size = 2; break;
2257 case 'b': size = 1; break;
2258 default: return ERROR_COMMAND_SYNTAX_ERROR;
2261 bool physical=strcmp(CMD_ARGV[0], "phys")==0;
2262 int (*fn)(struct target *target,
2263 uint32_t address, uint32_t size, uint32_t count, uint8_t *buffer);
2268 fn=target_read_phys_memory;
2271 fn=target_read_memory;
2273 if ((CMD_ARGC < 1) || (CMD_ARGC > 2))
2275 return ERROR_COMMAND_SYNTAX_ERROR;
2279 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], address);
2283 COMMAND_PARSE_NUMBER(uint, CMD_ARGV[1], count);
2285 uint8_t *buffer = calloc(count, size);
2287 struct target *target = get_current_target(CMD_CTX);
2288 int retval = fn(target, address, size, count, buffer);
2289 if (ERROR_OK == retval)
2290 handle_md_output(CMD_CTX, target, address, size, count, buffer);
2297 typedef int (*target_write_fn)(struct target *target,
2298 uint32_t address, uint32_t size, uint32_t count, uint8_t *buffer);
2300 static int target_write_memory_fast(struct target *target,
2301 uint32_t address, uint32_t size, uint32_t count, uint8_t *buffer)
2303 return target_write_buffer(target, address, size * count, buffer);
2306 static int target_fill_mem(struct target *target,
2315 /* We have to write in reasonably large chunks to be able
2316 * to fill large memory areas with any sane speed */
2317 const unsigned chunk_size = 16384;
2318 uint8_t *target_buf = malloc(chunk_size * data_size);
2319 if (target_buf == NULL)
2321 LOG_ERROR("Out of memory");
2325 for (unsigned i = 0; i < chunk_size; i ++)
2330 target_buffer_set_u32(target, target_buf + i*data_size, b);
2333 target_buffer_set_u16(target, target_buf + i*data_size, b);
2336 target_buffer_set_u8(target, target_buf + i*data_size, b);
2343 int retval = ERROR_OK;
2345 for (unsigned x = 0; x < c; x += chunk_size)
2349 if (current > chunk_size)
2351 current = chunk_size;
2353 int retval = fn(target, address + x * data_size, data_size, current, target_buf);
2354 if (retval != ERROR_OK)
2358 /* avoid GDB timeouts */
2367 COMMAND_HANDLER(handle_mw_command)
2371 return ERROR_COMMAND_SYNTAX_ERROR;
2373 bool physical=strcmp(CMD_ARGV[0], "phys")==0;
2379 fn=target_write_phys_memory;
2382 fn = target_write_memory_fast;
2384 if ((CMD_ARGC < 2) || (CMD_ARGC > 3))
2385 return ERROR_COMMAND_SYNTAX_ERROR;
2388 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], address);
2391 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], value);
2395 COMMAND_PARSE_NUMBER(uint, CMD_ARGV[2], count);
2397 struct target *target = get_current_target(CMD_CTX);
2399 switch (CMD_NAME[2])
2411 return ERROR_COMMAND_SYNTAX_ERROR;
2414 return target_fill_mem(target, address, fn, wordsize, value, count);
2417 static COMMAND_HELPER(parse_load_image_command_CMD_ARGV, struct image *image,
2418 uint32_t *min_address, uint32_t *max_address)
2420 if (CMD_ARGC < 1 || CMD_ARGC > 5)
2421 return ERROR_COMMAND_SYNTAX_ERROR;
2423 /* a base address isn't always necessary,
2424 * default to 0x0 (i.e. don't relocate) */
2428 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], addr);
2429 image->base_address = addr;
2430 image->base_address_set = 1;
2433 image->base_address_set = 0;
2435 image->start_address_set = 0;
2439 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[3], *min_address);
2443 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[4], *max_address);
2444 // use size (given) to find max (required)
2445 *max_address += *min_address;
2448 if (*min_address > *max_address)
2449 return ERROR_COMMAND_SYNTAX_ERROR;
2454 COMMAND_HANDLER(handle_load_image_command)
2458 uint32_t image_size;
2459 uint32_t min_address = 0;
2460 uint32_t max_address = 0xffffffff;
2464 int retval = CALL_COMMAND_HANDLER(parse_load_image_command_CMD_ARGV,
2465 &image, &min_address, &max_address);
2466 if (ERROR_OK != retval)
2469 struct target *target = get_current_target(CMD_CTX);
2471 struct duration bench;
2472 duration_start(&bench);
2474 if (image_open(&image, CMD_ARGV[0], (CMD_ARGC >= 3) ? CMD_ARGV[2] : NULL) != ERROR_OK)
2481 for (i = 0; i < image.num_sections; i++)
2483 buffer = malloc(image.sections[i].size);
2486 command_print(CMD_CTX,
2487 "error allocating buffer for section (%d bytes)",
2488 (int)(image.sections[i].size));
2492 if ((retval = image_read_section(&image, i, 0x0, image.sections[i].size, buffer, &buf_cnt)) != ERROR_OK)
2498 uint32_t offset = 0;
2499 uint32_t length = buf_cnt;
2501 /* DANGER!!! beware of unsigned comparision here!!! */
2503 if ((image.sections[i].base_address + buf_cnt >= min_address)&&
2504 (image.sections[i].base_address < max_address))
2506 if (image.sections[i].base_address < min_address)
2508 /* clip addresses below */
2509 offset += min_address-image.sections[i].base_address;
2513 if (image.sections[i].base_address + buf_cnt > max_address)
2515 length -= (image.sections[i].base_address + buf_cnt)-max_address;
2518 if ((retval = target_write_buffer(target, image.sections[i].base_address + offset, length, buffer + offset)) != ERROR_OK)
2523 image_size += length;
2524 command_print(CMD_CTX, "%u bytes written at address 0x%8.8" PRIx32 "",
2525 (unsigned int)length,
2526 image.sections[i].base_address + offset);
2532 if ((ERROR_OK == retval) && (duration_measure(&bench) == ERROR_OK))
2534 command_print(CMD_CTX, "downloaded %" PRIu32 " bytes "
2535 "in %fs (%0.3f kb/s)", image_size,
2536 duration_elapsed(&bench), duration_kbps(&bench, image_size));
2539 image_close(&image);
2545 COMMAND_HANDLER(handle_dump_image_command)
2547 struct fileio fileio;
2549 uint8_t buffer[560];
2553 struct target *target = get_current_target(CMD_CTX);
2557 command_print(CMD_CTX, "usage: dump_image <filename> <address> <size>");
2562 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], address);
2564 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[2], size);
2566 if (fileio_open(&fileio, CMD_ARGV[0], FILEIO_WRITE, FILEIO_BINARY) != ERROR_OK)
2571 struct duration bench;
2572 duration_start(&bench);
2574 int retval = ERROR_OK;
2577 size_t size_written;
2578 uint32_t this_run_size = (size > 560) ? 560 : size;
2579 retval = target_read_buffer(target, address, this_run_size, buffer);
2580 if (retval != ERROR_OK)
2585 retval = fileio_write(&fileio, this_run_size, buffer, &size_written);
2586 if (retval != ERROR_OK)
2591 size -= this_run_size;
2592 address += this_run_size;
2595 if ((retvaltemp = fileio_close(&fileio)) != ERROR_OK)
2598 if ((ERROR_OK == retval) && (duration_measure(&bench) == ERROR_OK))
2600 command_print(CMD_CTX,
2601 "dumped %ld bytes in %fs (%0.3f kb/s)", (long)fileio.size,
2602 duration_elapsed(&bench), duration_kbps(&bench, fileio.size));
2608 static COMMAND_HELPER(handle_verify_image_command_internal, int verify)
2612 uint32_t image_size;
2615 uint32_t checksum = 0;
2616 uint32_t mem_checksum = 0;
2620 struct target *target = get_current_target(CMD_CTX);
2624 return ERROR_COMMAND_SYNTAX_ERROR;
2629 LOG_ERROR("no target selected");
2633 struct duration bench;
2634 duration_start(&bench);
2639 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], addr);
2640 image.base_address = addr;
2641 image.base_address_set = 1;
2645 image.base_address_set = 0;
2646 image.base_address = 0x0;
2649 image.start_address_set = 0;
2651 if ((retval = image_open(&image, CMD_ARGV[0], (CMD_ARGC == 3) ? CMD_ARGV[2] : NULL)) != ERROR_OK)
2658 for (i = 0; i < image.num_sections; i++)
2660 buffer = malloc(image.sections[i].size);
2663 command_print(CMD_CTX,
2664 "error allocating buffer for section (%d bytes)",
2665 (int)(image.sections[i].size));
2668 if ((retval = image_read_section(&image, i, 0x0, image.sections[i].size, buffer, &buf_cnt)) != ERROR_OK)
2676 /* calculate checksum of image */
2677 image_calculate_checksum(buffer, buf_cnt, &checksum);
2679 retval = target_checksum_memory(target, image.sections[i].base_address, buf_cnt, &mem_checksum);
2680 if (retval != ERROR_OK)
2686 if (checksum != mem_checksum)
2688 /* failed crc checksum, fall back to a binary compare */
2691 command_print(CMD_CTX, "checksum mismatch - attempting binary compare");
2693 data = (uint8_t*)malloc(buf_cnt);
2695 /* Can we use 32bit word accesses? */
2697 int count = buf_cnt;
2698 if ((count % 4) == 0)
2703 retval = target_read_memory(target, image.sections[i].base_address, size, count, data);
2704 if (retval == ERROR_OK)
2707 for (t = 0; t < buf_cnt; t++)
2709 if (data[t] != buffer[t])
2711 command_print(CMD_CTX,
2712 "Verify operation failed address 0x%08x. Was 0x%02x instead of 0x%02x\n",
2713 (unsigned)(t + image.sections[i].base_address),
2718 retval = ERROR_FAIL;
2732 command_print(CMD_CTX, "address 0x%08" PRIx32 " length 0x%08zx",
2733 image.sections[i].base_address,
2738 image_size += buf_cnt;
2741 if ((ERROR_OK == retval) && (duration_measure(&bench) == ERROR_OK))
2743 command_print(CMD_CTX, "verified %" PRIu32 " bytes "
2744 "in %fs (%0.3f kb/s)", image_size,
2745 duration_elapsed(&bench), duration_kbps(&bench, image_size));
2748 image_close(&image);
2753 COMMAND_HANDLER(handle_verify_image_command)
2755 return CALL_COMMAND_HANDLER(handle_verify_image_command_internal, 1);
2758 COMMAND_HANDLER(handle_test_image_command)
2760 return CALL_COMMAND_HANDLER(handle_verify_image_command_internal, 0);
2763 static int handle_bp_command_list(struct command_context *cmd_ctx)
2765 struct target *target = get_current_target(cmd_ctx);
2766 struct breakpoint *breakpoint = target->breakpoints;
2769 if (breakpoint->type == BKPT_SOFT)
2771 char* buf = buf_to_str(breakpoint->orig_instr,
2772 breakpoint->length, 16);
2773 command_print(cmd_ctx, "0x%8.8" PRIx32 ", 0x%x, %i, 0x%s",
2774 breakpoint->address,
2776 breakpoint->set, buf);
2781 command_print(cmd_ctx, "0x%8.8" PRIx32 ", 0x%x, %i",
2782 breakpoint->address,
2783 breakpoint->length, breakpoint->set);
2786 breakpoint = breakpoint->next;
2791 static int handle_bp_command_set(struct command_context *cmd_ctx,
2792 uint32_t addr, uint32_t length, int hw)
2794 struct target *target = get_current_target(cmd_ctx);
2795 int retval = breakpoint_add(target, addr, length, hw);
2796 if (ERROR_OK == retval)
2797 command_print(cmd_ctx, "breakpoint set at 0x%8.8" PRIx32 "", addr);
2799 LOG_ERROR("Failure setting breakpoint");
2803 COMMAND_HANDLER(handle_bp_command)
2806 return handle_bp_command_list(CMD_CTX);
2808 if (CMD_ARGC < 2 || CMD_ARGC > 3)
2810 command_print(CMD_CTX, "usage: bp <address> <length> ['hw']");
2811 return ERROR_COMMAND_SYNTAX_ERROR;
2815 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], addr);
2817 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], length);
2822 if (strcmp(CMD_ARGV[2], "hw") == 0)
2825 return ERROR_COMMAND_SYNTAX_ERROR;
2828 return handle_bp_command_set(CMD_CTX, addr, length, hw);
2831 COMMAND_HANDLER(handle_rbp_command)
2834 return ERROR_COMMAND_SYNTAX_ERROR;
2837 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], addr);
2839 struct target *target = get_current_target(CMD_CTX);
2840 breakpoint_remove(target, addr);
2845 COMMAND_HANDLER(handle_wp_command)
2847 struct target *target = get_current_target(CMD_CTX);
2851 struct watchpoint *watchpoint = target->watchpoints;
2855 command_print(CMD_CTX, "address: 0x%8.8" PRIx32
2856 ", len: 0x%8.8" PRIx32
2857 ", r/w/a: %i, value: 0x%8.8" PRIx32
2858 ", mask: 0x%8.8" PRIx32,
2859 watchpoint->address,
2861 (int)watchpoint->rw,
2864 watchpoint = watchpoint->next;
2869 enum watchpoint_rw type = WPT_ACCESS;
2871 uint32_t length = 0;
2872 uint32_t data_value = 0x0;
2873 uint32_t data_mask = 0xffffffff;
2878 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[4], data_mask);
2881 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[3], data_value);
2884 switch (CMD_ARGV[2][0])
2896 LOG_ERROR("invalid watchpoint mode ('%c')", CMD_ARGV[2][0]);
2897 return ERROR_COMMAND_SYNTAX_ERROR;
2901 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], length);
2902 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], addr);
2906 command_print(CMD_CTX, "usage: wp [address length "
2907 "[(r|w|a) [value [mask]]]]");
2908 return ERROR_COMMAND_SYNTAX_ERROR;
2911 int retval = watchpoint_add(target, addr, length, type,
2912 data_value, data_mask);
2913 if (ERROR_OK != retval)
2914 LOG_ERROR("Failure setting watchpoints");
2919 COMMAND_HANDLER(handle_rwp_command)
2922 return ERROR_COMMAND_SYNTAX_ERROR;
2925 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], addr);
2927 struct target *target = get_current_target(CMD_CTX);
2928 watchpoint_remove(target, addr);
2935 * Translate a virtual address to a physical address.
2937 * The low-level target implementation must have logged a detailed error
2938 * which is forwarded to telnet/GDB session.
2940 COMMAND_HANDLER(handle_virt2phys_command)
2943 return ERROR_COMMAND_SYNTAX_ERROR;
2946 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], va);
2949 struct target *target = get_current_target(CMD_CTX);
2950 int retval = target->type->virt2phys(target, va, &pa);
2951 if (retval == ERROR_OK)
2952 command_print(CMD_CTX, "Physical address 0x%08" PRIx32 "", pa);
2957 static void writeData(FILE *f, const void *data, size_t len)
2959 size_t written = fwrite(data, 1, len, f);
2961 LOG_ERROR("failed to write %zu bytes: %s", len, strerror(errno));
2964 static void writeLong(FILE *f, int l)
2967 for (i = 0; i < 4; i++)
2969 char c = (l >> (i*8))&0xff;
2970 writeData(f, &c, 1);
2975 static void writeString(FILE *f, char *s)
2977 writeData(f, s, strlen(s));
2980 /* Dump a gmon.out histogram file. */
2981 static void writeGmon(uint32_t *samples, uint32_t sampleNum, const char *filename)
2984 FILE *f = fopen(filename, "w");
2987 writeString(f, "gmon");
2988 writeLong(f, 0x00000001); /* Version */
2989 writeLong(f, 0); /* padding */
2990 writeLong(f, 0); /* padding */
2991 writeLong(f, 0); /* padding */
2993 uint8_t zero = 0; /* GMON_TAG_TIME_HIST */
2994 writeData(f, &zero, 1);
2996 /* figure out bucket size */
2997 uint32_t min = samples[0];
2998 uint32_t max = samples[0];
2999 for (i = 0; i < sampleNum; i++)
3001 if (min > samples[i])
3005 if (max < samples[i])
3011 int addressSpace = (max-min + 1);
3013 static const uint32_t maxBuckets = 256 * 1024; /* maximum buckets. */
3014 uint32_t length = addressSpace;
3015 if (length > maxBuckets)
3017 length = maxBuckets;
3019 int *buckets = malloc(sizeof(int)*length);
3020 if (buckets == NULL)
3025 memset(buckets, 0, sizeof(int)*length);
3026 for (i = 0; i < sampleNum;i++)
3028 uint32_t address = samples[i];
3029 long long a = address-min;
3030 long long b = length-1;
3031 long long c = addressSpace-1;
3032 int index = (a*b)/c; /* danger!!!! int32 overflows */
3036 /* append binary memory gmon.out &profile_hist_hdr ((char*)&profile_hist_hdr + sizeof(struct gmon_hist_hdr)) */
3037 writeLong(f, min); /* low_pc */
3038 writeLong(f, max); /* high_pc */
3039 writeLong(f, length); /* # of samples */
3040 writeLong(f, 64000000); /* 64MHz */
3041 writeString(f, "seconds");
3042 for (i = 0; i < (15-strlen("seconds")); i++)
3043 writeData(f, &zero, 1);
3044 writeString(f, "s");
3046 /*append binary memory gmon.out profile_hist_data (profile_hist_data + profile_hist_hdr.hist_size) */
3048 char *data = malloc(2*length);
3051 for (i = 0; i < length;i++)
3060 data[i*2 + 1]=(val >> 8)&0xff;
3063 writeData(f, data, length * 2);
3073 /* profiling samples the CPU PC as quickly as OpenOCD is able,
3074 * which will be used as a random sampling of PC */
3075 COMMAND_HANDLER(handle_profile_command)
3077 struct target *target = get_current_target(CMD_CTX);
3078 struct timeval timeout, now;
3080 gettimeofday(&timeout, NULL);
3083 return ERROR_COMMAND_SYNTAX_ERROR;
3086 COMMAND_PARSE_NUMBER(uint, CMD_ARGV[0], offset);
3088 timeval_add_time(&timeout, offset, 0);
3091 * @todo: Some cores let us sample the PC without the
3092 * annoying halt/resume step; for example, ARMv7 PCSR.
3093 * Provide a way to use that more efficient mechanism.
3096 command_print(CMD_CTX, "Starting profiling. Halting and resuming the target as often as we can...");
3098 static const int maxSample = 10000;
3099 uint32_t *samples = malloc(sizeof(uint32_t)*maxSample);
3100 if (samples == NULL)
3104 /* hopefully it is safe to cache! We want to stop/restart as quickly as possible. */
3105 struct reg *reg = register_get_by_name(target->reg_cache, "pc", 1);
3110 target_poll(target);
3111 if (target->state == TARGET_HALTED)
3113 uint32_t t=*((uint32_t *)reg->value);
3114 samples[numSamples++]=t;
3115 retval = target_resume(target, 1, 0, 0, 0); /* current pc, addr = 0, do not handle breakpoints, not debugging */
3116 target_poll(target);
3117 alive_sleep(10); /* sleep 10ms, i.e. <100 samples/second. */
3118 } else if (target->state == TARGET_RUNNING)
3120 /* We want to quickly sample the PC. */
3121 if ((retval = target_halt(target)) != ERROR_OK)
3128 command_print(CMD_CTX, "Target not halted or running");
3132 if (retval != ERROR_OK)
3137 gettimeofday(&now, NULL);
3138 if ((numSamples >= maxSample) || ((now.tv_sec >= timeout.tv_sec) && (now.tv_usec >= timeout.tv_usec)))
3140 command_print(CMD_CTX, "Profiling completed. %d samples.", numSamples);
3141 if ((retval = target_poll(target)) != ERROR_OK)
3146 if (target->state == TARGET_HALTED)
3148 target_resume(target, 1, 0, 0, 0); /* current pc, addr = 0, do not handle breakpoints, not debugging */
3150 if ((retval = target_poll(target)) != ERROR_OK)
3155 writeGmon(samples, numSamples, CMD_ARGV[1]);
3156 command_print(CMD_CTX, "Wrote %s", CMD_ARGV[1]);
3165 static int new_int_array_element(Jim_Interp * interp, const char *varname, int idx, uint32_t val)
3168 Jim_Obj *nameObjPtr, *valObjPtr;
3171 namebuf = alloc_printf("%s(%d)", varname, idx);
3175 nameObjPtr = Jim_NewStringObj(interp, namebuf, -1);
3176 valObjPtr = Jim_NewIntObj(interp, val);
3177 if (!nameObjPtr || !valObjPtr)
3183 Jim_IncrRefCount(nameObjPtr);
3184 Jim_IncrRefCount(valObjPtr);
3185 result = Jim_SetVariable(interp, nameObjPtr, valObjPtr);
3186 Jim_DecrRefCount(interp, nameObjPtr);
3187 Jim_DecrRefCount(interp, valObjPtr);
3189 /* printf("%s(%d) <= 0%08x\n", varname, idx, val); */
3193 static int jim_mem2array(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
3195 struct command_context *context;
3196 struct target *target;
3198 context = Jim_GetAssocData(interp, "context");
3199 if (context == NULL)
3201 LOG_ERROR("mem2array: no command context");
3204 target = get_current_target(context);
3207 LOG_ERROR("mem2array: no current target");
3211 return target_mem2array(interp, target, argc-1, argv + 1);
3214 static int target_mem2array(Jim_Interp *interp, struct target *target, int argc, Jim_Obj *const *argv)
3222 const char *varname;
3226 /* argv[1] = name of array to receive the data
3227 * argv[2] = desired width
3228 * argv[3] = memory address
3229 * argv[4] = count of times to read
3232 Jim_WrongNumArgs(interp, 1, argv, "varname width addr nelems");
3235 varname = Jim_GetString(argv[0], &len);
3236 /* given "foo" get space for worse case "foo(%d)" .. add 20 */
3238 e = Jim_GetLong(interp, argv[1], &l);
3244 e = Jim_GetLong(interp, argv[2], &l);
3249 e = Jim_GetLong(interp, argv[3], &l);
3265 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3266 Jim_AppendStrings(interp, Jim_GetResult(interp), "Invalid width param, must be 8/16/32", NULL);
3270 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3271 Jim_AppendStrings(interp, Jim_GetResult(interp), "mem2array: zero width read?", NULL);
3274 if ((addr + (len * width)) < addr) {
3275 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3276 Jim_AppendStrings(interp, Jim_GetResult(interp), "mem2array: addr + len - wraps to zero?", NULL);
3279 /* absurd transfer size? */
3281 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3282 Jim_AppendStrings(interp, Jim_GetResult(interp), "mem2array: absurd > 64K item request", NULL);
3287 ((width == 2) && ((addr & 1) == 0)) ||
3288 ((width == 4) && ((addr & 3) == 0))) {
3292 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3293 sprintf(buf, "mem2array address: 0x%08" PRIx32 " is not aligned for %" PRId32 " byte reads",
3296 Jim_AppendStrings(interp, Jim_GetResult(interp), buf , NULL);
3305 size_t buffersize = 4096;
3306 uint8_t *buffer = malloc(buffersize);
3313 /* Slurp... in buffer size chunks */
3315 count = len; /* in objects.. */
3316 if (count > (buffersize/width)) {
3317 count = (buffersize/width);
3320 retval = target_read_memory(target, addr, width, count, buffer);
3321 if (retval != ERROR_OK) {
3323 LOG_ERROR("mem2array: Read @ 0x%08x, w=%d, cnt=%d, failed",
3327 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3328 Jim_AppendStrings(interp, Jim_GetResult(interp), "mem2array: cannot read memory", NULL);
3332 v = 0; /* shut up gcc */
3333 for (i = 0 ;i < count ;i++, n++) {
3336 v = target_buffer_get_u32(target, &buffer[i*width]);
3339 v = target_buffer_get_u16(target, &buffer[i*width]);
3342 v = buffer[i] & 0x0ff;
3345 new_int_array_element(interp, varname, n, v);
3353 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3358 static int get_int_array_element(Jim_Interp * interp, const char *varname, int idx, uint32_t *val)
3361 Jim_Obj *nameObjPtr, *valObjPtr;
3365 namebuf = alloc_printf("%s(%d)", varname, idx);
3369 nameObjPtr = Jim_NewStringObj(interp, namebuf, -1);
3376 Jim_IncrRefCount(nameObjPtr);
3377 valObjPtr = Jim_GetVariable(interp, nameObjPtr, JIM_ERRMSG);
3378 Jim_DecrRefCount(interp, nameObjPtr);
3380 if (valObjPtr == NULL)
3383 result = Jim_GetLong(interp, valObjPtr, &l);
3384 /* printf("%s(%d) => 0%08x\n", varname, idx, val); */
3389 static int jim_array2mem(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
3391 struct command_context *context;
3392 struct target *target;
3394 context = Jim_GetAssocData(interp, "context");
3395 if (context == NULL) {
3396 LOG_ERROR("array2mem: no command context");
3399 target = get_current_target(context);
3400 if (target == NULL) {
3401 LOG_ERROR("array2mem: no current target");
3405 return target_array2mem(interp,target, argc-1, argv + 1);
3408 static int target_array2mem(Jim_Interp *interp, struct target *target,
3409 int argc, Jim_Obj *const *argv)
3417 const char *varname;
3421 /* argv[1] = name of array to get the data
3422 * argv[2] = desired width
3423 * argv[3] = memory address
3424 * argv[4] = count to write
3427 Jim_WrongNumArgs(interp, 0, argv, "varname width addr nelems");
3430 varname = Jim_GetString(argv[0], &len);
3431 /* given "foo" get space for worse case "foo(%d)" .. add 20 */
3433 e = Jim_GetLong(interp, argv[1], &l);
3439 e = Jim_GetLong(interp, argv[2], &l);
3444 e = Jim_GetLong(interp, argv[3], &l);
3460 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3461 Jim_AppendStrings(interp, Jim_GetResult(interp), "Invalid width param, must be 8/16/32", NULL);
3465 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3466 Jim_AppendStrings(interp, Jim_GetResult(interp), "array2mem: zero width read?", NULL);
3469 if ((addr + (len * width)) < addr) {
3470 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3471 Jim_AppendStrings(interp, Jim_GetResult(interp), "array2mem: addr + len - wraps to zero?", NULL);
3474 /* absurd transfer size? */
3476 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3477 Jim_AppendStrings(interp, Jim_GetResult(interp), "array2mem: absurd > 64K item request", NULL);
3482 ((width == 2) && ((addr & 1) == 0)) ||
3483 ((width == 4) && ((addr & 3) == 0))) {
3487 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3488 sprintf(buf, "array2mem address: 0x%08x is not aligned for %d byte reads",
3491 Jim_AppendStrings(interp, Jim_GetResult(interp), buf , NULL);
3502 size_t buffersize = 4096;
3503 uint8_t *buffer = malloc(buffersize);
3508 /* Slurp... in buffer size chunks */
3510 count = len; /* in objects.. */
3511 if (count > (buffersize/width)) {
3512 count = (buffersize/width);
3515 v = 0; /* shut up gcc */
3516 for (i = 0 ;i < count ;i++, n++) {
3517 get_int_array_element(interp, varname, n, &v);
3520 target_buffer_set_u32(target, &buffer[i*width], v);
3523 target_buffer_set_u16(target, &buffer[i*width], v);
3526 buffer[i] = v & 0x0ff;
3532 retval = target_write_memory(target, addr, width, count, buffer);
3533 if (retval != ERROR_OK) {
3535 LOG_ERROR("array2mem: Write @ 0x%08x, w=%d, cnt=%d, failed",
3539 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3540 Jim_AppendStrings(interp, Jim_GetResult(interp), "array2mem: cannot read memory", NULL);
3548 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3553 void target_all_handle_event(enum target_event e)
3555 struct target *target;
3557 LOG_DEBUG("**all*targets: event: %d, %s",
3559 Jim_Nvp_value2name_simple(nvp_target_event, e)->name);
3561 target = all_targets;
3563 target_handle_event(target, e);
3564 target = target->next;
3569 /* FIX? should we propagate errors here rather than printing them
3572 void target_handle_event(struct target *target, enum target_event e)
3574 struct target_event_action *teap;
3576 for (teap = target->event_action; teap != NULL; teap = teap->next) {
3577 if (teap->event == e) {
3578 LOG_DEBUG("target: (%d) %s (%s) event: %d (%s) action: %s",
3579 target->target_number,
3580 target_name(target),
3581 target_type_name(target),
3583 Jim_Nvp_value2name_simple(nvp_target_event, e)->name,
3584 Jim_GetString(teap->body, NULL));
3585 if (Jim_EvalObj(teap->interp, teap->body) != JIM_OK)
3587 Jim_PrintErrorMessage(teap->interp);
3594 * Returns true only if the target has a handler for the specified event.
3596 bool target_has_event_action(struct target *target, enum target_event event)
3598 struct target_event_action *teap;
3600 for (teap = target->event_action; teap != NULL; teap = teap->next) {
3601 if (teap->event == event)
3607 enum target_cfg_param {
3610 TCFG_WORK_AREA_VIRT,
3611 TCFG_WORK_AREA_PHYS,
3612 TCFG_WORK_AREA_SIZE,
3613 TCFG_WORK_AREA_BACKUP,
3616 TCFG_CHAIN_POSITION,
3619 static Jim_Nvp nvp_config_opts[] = {
3620 { .name = "-type", .value = TCFG_TYPE },
3621 { .name = "-event", .value = TCFG_EVENT },
3622 { .name = "-work-area-virt", .value = TCFG_WORK_AREA_VIRT },
3623 { .name = "-work-area-phys", .value = TCFG_WORK_AREA_PHYS },
3624 { .name = "-work-area-size", .value = TCFG_WORK_AREA_SIZE },
3625 { .name = "-work-area-backup", .value = TCFG_WORK_AREA_BACKUP },
3626 { .name = "-endian" , .value = TCFG_ENDIAN },
3627 { .name = "-variant", .value = TCFG_VARIANT },
3628 { .name = "-chain-position", .value = TCFG_CHAIN_POSITION },
3630 { .name = NULL, .value = -1 }
3633 static int target_configure(Jim_GetOptInfo *goi, struct target *target)
3641 /* parse config or cget options ... */
3642 while (goi->argc > 0) {
3643 Jim_SetEmptyResult(goi->interp);
3644 /* Jim_GetOpt_Debug(goi); */
3646 if (target->type->target_jim_configure) {
3647 /* target defines a configure function */
3648 /* target gets first dibs on parameters */
3649 e = (*(target->type->target_jim_configure))(target, goi);
3658 /* otherwise we 'continue' below */
3660 e = Jim_GetOpt_Nvp(goi, nvp_config_opts, &n);
3662 Jim_GetOpt_NvpUnknown(goi, nvp_config_opts, 0);
3668 if (goi->isconfigure) {
3669 Jim_SetResult_sprintf(goi->interp,
3670 "not settable: %s", n->name);
3674 if (goi->argc != 0) {
3675 Jim_WrongNumArgs(goi->interp,
3676 goi->argc, goi->argv,
3681 Jim_SetResultString(goi->interp,
3682 target_type_name(target), -1);
3686 if (goi->argc == 0) {
3687 Jim_WrongNumArgs(goi->interp, goi->argc, goi->argv, "-event ?event-name? ...");
3691 e = Jim_GetOpt_Nvp(goi, nvp_target_event, &n);
3693 Jim_GetOpt_NvpUnknown(goi, nvp_target_event, 1);
3697 if (goi->isconfigure) {
3698 if (goi->argc != 1) {
3699 Jim_WrongNumArgs(goi->interp, goi->argc, goi->argv, "-event ?event-name? ?EVENT-BODY?");
3703 if (goi->argc != 0) {
3704 Jim_WrongNumArgs(goi->interp, goi->argc, goi->argv, "-event ?event-name?");
3710 struct target_event_action *teap;
3712 teap = target->event_action;
3713 /* replace existing? */
3715 if (teap->event == (enum target_event)n->value) {
3721 if (goi->isconfigure) {
3722 bool replace = true;
3725 teap = calloc(1, sizeof(*teap));
3728 teap->event = n->value;
3729 teap->interp = goi->interp;
3730 Jim_GetOpt_Obj(goi, &o);
3732 Jim_DecrRefCount(teap->interp, teap->body);
3734 teap->body = Jim_DuplicateObj(goi->interp, o);
3737 * Tcl/TK - "tk events" have a nice feature.
3738 * See the "BIND" command.
3739 * We should support that here.
3740 * You can specify %X and %Y in the event code.
3741 * The idea is: %T - target name.
3742 * The idea is: %N - target number
3743 * The idea is: %E - event name.
3745 Jim_IncrRefCount(teap->body);
3749 /* add to head of event list */
3750 teap->next = target->event_action;
3751 target->event_action = teap;
3753 Jim_SetEmptyResult(goi->interp);
3757 Jim_SetEmptyResult(goi->interp);
3759 Jim_SetResult(goi->interp, Jim_DuplicateObj(goi->interp, teap->body));
3766 case TCFG_WORK_AREA_VIRT:
3767 if (goi->isconfigure) {
3768 target_free_all_working_areas(target);
3769 e = Jim_GetOpt_Wide(goi, &w);
3773 target->working_area_virt = w;
3774 target->working_area_virt_spec = true;
3776 if (goi->argc != 0) {
3780 Jim_SetResult(goi->interp, Jim_NewIntObj(goi->interp, target->working_area_virt));
3784 case TCFG_WORK_AREA_PHYS:
3785 if (goi->isconfigure) {
3786 target_free_all_working_areas(target);
3787 e = Jim_GetOpt_Wide(goi, &w);
3791 target->working_area_phys = w;
3792 target->working_area_phys_spec = true;
3794 if (goi->argc != 0) {
3798 Jim_SetResult(goi->interp, Jim_NewIntObj(goi->interp, target->working_area_phys));
3802 case TCFG_WORK_AREA_SIZE:
3803 if (goi->isconfigure) {
3804 target_free_all_working_areas(target);
3805 e = Jim_GetOpt_Wide(goi, &w);
3809 target->working_area_size = w;
3811 if (goi->argc != 0) {
3815 Jim_SetResult(goi->interp, Jim_NewIntObj(goi->interp, target->working_area_size));
3819 case TCFG_WORK_AREA_BACKUP:
3820 if (goi->isconfigure) {
3821 target_free_all_working_areas(target);
3822 e = Jim_GetOpt_Wide(goi, &w);
3826 /* make this exactly 1 or 0 */
3827 target->backup_working_area = (!!w);
3829 if (goi->argc != 0) {
3833 Jim_SetResult(goi->interp, Jim_NewIntObj(goi->interp, target->backup_working_area));
3834 /* loop for more e*/
3838 if (goi->isconfigure) {
3839 e = Jim_GetOpt_Nvp(goi, nvp_target_endian, &n);
3841 Jim_GetOpt_NvpUnknown(goi, nvp_target_endian, 1);
3844 target->endianness = n->value;
3846 if (goi->argc != 0) {
3850 n = Jim_Nvp_value2name_simple(nvp_target_endian, target->endianness);
3851 if (n->name == NULL) {
3852 target->endianness = TARGET_LITTLE_ENDIAN;
3853 n = Jim_Nvp_value2name_simple(nvp_target_endian, target->endianness);
3855 Jim_SetResultString(goi->interp, n->name, -1);
3860 if (goi->isconfigure) {
3861 if (goi->argc < 1) {
3862 Jim_SetResult_sprintf(goi->interp,
3867 if (target->variant) {
3868 free((void *)(target->variant));
3870 e = Jim_GetOpt_String(goi, &cp, NULL);
3871 target->variant = strdup(cp);
3873 if (goi->argc != 0) {
3877 Jim_SetResultString(goi->interp, target->variant,-1);
3880 case TCFG_CHAIN_POSITION:
3881 if (goi->isconfigure) {
3883 struct jtag_tap *tap;
3884 target_free_all_working_areas(target);
3885 e = Jim_GetOpt_Obj(goi, &o);
3889 tap = jtag_tap_by_jim_obj(goi->interp, o);
3893 /* make this exactly 1 or 0 */
3896 if (goi->argc != 0) {
3900 Jim_SetResultString(goi->interp, target->tap->dotted_name, -1);
3901 /* loop for more e*/
3904 } /* while (goi->argc) */
3907 /* done - we return */
3912 jim_target_configure(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
3916 Jim_GetOpt_Setup(&goi, interp, argc - 1, argv + 1);
3917 goi.isconfigure = !strcmp(Jim_GetString(argv[0], NULL), "configure");
3918 int need_args = 1 + goi.isconfigure;
3919 if (goi.argc < need_args)
3921 Jim_WrongNumArgs(goi.interp, goi.argc, goi.argv,
3923 ? "missing: -option VALUE ..."
3924 : "missing: -option ...");
3927 struct target *target = Jim_CmdPrivData(goi.interp);
3928 return target_configure(&goi, target);
3931 static int jim_target_mw(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
3933 const char *cmd_name = Jim_GetString(argv[0], NULL);
3936 Jim_GetOpt_Setup(&goi, interp, argc - 1, argv + 1);
3938 /* danger! goi.argc will be modified below! */
3941 if (argc != 2 && argc != 3)
3943 Jim_SetResult_sprintf(goi.interp,
3944 "usage: %s <address> <data> [<count>]", cmd_name);
3950 int e = Jim_GetOpt_Wide(&goi, &a);
3955 e = Jim_GetOpt_Wide(&goi, &b);
3962 e = Jim_GetOpt_Wide(&goi, &c);
3967 struct target *target = Jim_CmdPrivData(goi.interp);
3969 if (strcasecmp(cmd_name, "mww") == 0) {
3972 else if (strcasecmp(cmd_name, "mwh") == 0) {
3975 else if (strcasecmp(cmd_name, "mwb") == 0) {
3978 LOG_ERROR("command '%s' unknown: ", cmd_name);
3982 return (target_fill_mem(target, a, target_write_memory_fast, data_size, b, c) == ERROR_OK) ? JIM_OK : JIM_ERR;
3985 static int jim_target_md(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
3987 const char *cmd_name = Jim_GetString(argv[0], NULL);
3990 Jim_GetOpt_Setup(&goi, interp, argc - 1, argv + 1);
3992 /* danger! goi.argc will be modified below! */
3995 if ((argc != 1) && (argc != 2))
3997 Jim_SetResult_sprintf(goi.interp,
3998 "usage: %s <address> [<count>]", cmd_name);
4003 int e = Jim_GetOpt_Wide(&goi, &a);
4009 e = Jim_GetOpt_Wide(&goi, &c);
4016 jim_wide b = 1; /* shut up gcc */
4017 if (strcasecmp(cmd_name, "mdw") == 0)
4019 else if (strcasecmp(cmd_name, "mdh") == 0)
4021 else if (strcasecmp(cmd_name, "mdb") == 0)
4024 LOG_ERROR("command '%s' unknown: ", cmd_name);
4028 /* convert count to "bytes" */
4031 struct target *target = Jim_CmdPrivData(goi.interp);
4032 uint8_t target_buf[32];
4039 e = target_read_memory(target, a, b, y / b, target_buf);
4040 if (e != ERROR_OK) {
4041 Jim_SetResult_sprintf(interp, "error reading target @ 0x%08lx", (int)(a));
4045 Jim_fprintf(interp, interp->cookie_stdout, "0x%08x ", (int)(a));
4048 for (x = 0; x < 16 && x < y; x += 4)
4050 z = target_buffer_get_u32(target, &(target_buf[ x ]));
4051 Jim_fprintf(interp, interp->cookie_stdout, "%08x ", (int)(z));
4053 for (; (x < 16) ; x += 4) {
4054 Jim_fprintf(interp, interp->cookie_stdout, " ");
4058 for (x = 0; x < 16 && x < y; x += 2)
4060 z = target_buffer_get_u16(target, &(target_buf[ x ]));
4061 Jim_fprintf(interp, interp->cookie_stdout, "%04x ", (int)(z));
4063 for (; (x < 16) ; x += 2) {
4064 Jim_fprintf(interp, interp->cookie_stdout, " ");
4069 for (x = 0 ; (x < 16) && (x < y) ; x += 1) {
4070 z = target_buffer_get_u8(target, &(target_buf[ x ]));
4071 Jim_fprintf(interp, interp->cookie_stdout, "%02x ", (int)(z));
4073 for (; (x < 16) ; x += 1) {
4074 Jim_fprintf(interp, interp->cookie_stdout, " ");
4078 /* ascii-ify the bytes */
4079 for (x = 0 ; x < y ; x++) {
4080 if ((target_buf[x] >= 0x20) &&
4081 (target_buf[x] <= 0x7e)) {
4085 target_buf[x] = '.';
4090 target_buf[x] = ' ';
4095 /* print - with a newline */
4096 Jim_fprintf(interp, interp->cookie_stdout, "%s\n", target_buf);
4104 static int jim_target_mem2array(Jim_Interp *interp,
4105 int argc, Jim_Obj *const *argv)
4107 struct target *target = Jim_CmdPrivData(interp);
4108 return target_mem2array(interp, target, argc - 1, argv + 1);
4111 static int jim_target_array2mem(Jim_Interp *interp,
4112 int argc, Jim_Obj *const *argv)
4114 struct target *target = Jim_CmdPrivData(interp);
4115 return target_array2mem(interp, target, argc - 1, argv + 1);
4118 static int jim_target_tap_disabled(Jim_Interp *interp)
4120 Jim_SetResult_sprintf(interp, "[TAP is disabled]");
4124 static int jim_target_examine(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
4128 Jim_WrongNumArgs(interp, 1, argv, "[no parameters]");
4131 struct target *target = Jim_CmdPrivData(interp);
4132 if (!target->tap->enabled)
4133 return jim_target_tap_disabled(interp);
4135 int e = target->type->examine(target);
4138 Jim_SetResult_sprintf(interp, "examine-fails: %d", e);
4144 static int jim_target_halt_gdb(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
4148 Jim_WrongNumArgs(interp, 1, argv, "[no parameters]");
4151 struct target *target = Jim_CmdPrivData(interp);
4153 if (target_call_event_callbacks(target, TARGET_EVENT_GDB_HALT) != ERROR_OK)
4159 static int jim_target_poll(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
4163 Jim_WrongNumArgs(interp, 1, argv, "[no parameters]");
4166 struct target *target = Jim_CmdPrivData(interp);
4167 if (!target->tap->enabled)
4168 return jim_target_tap_disabled(interp);
4171 if (!(target_was_examined(target))) {
4172 e = ERROR_TARGET_NOT_EXAMINED;
4174 e = target->type->poll(target);
4178 Jim_SetResult_sprintf(interp, "poll-fails: %d", e);
4184 static int jim_target_reset(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
4187 Jim_GetOpt_Setup(&goi, interp, argc - 1, argv + 1);
4191 Jim_WrongNumArgs(interp, 0, argv,
4192 "([tT]|[fF]|assert|deassert) BOOL");
4197 int e = Jim_GetOpt_Nvp(&goi, nvp_assert, &n);
4200 Jim_GetOpt_NvpUnknown(&goi, nvp_assert, 1);
4203 /* the halt or not param */
4205 e = Jim_GetOpt_Wide(&goi, &a);
4209 struct target *target = Jim_CmdPrivData(goi.interp);
4210 if (!target->tap->enabled)
4211 return jim_target_tap_disabled(interp);
4212 if (!(target_was_examined(target)))
4214 LOG_ERROR("Target not examined yet");
4215 return ERROR_TARGET_NOT_EXAMINED;
4217 if (!target->type->assert_reset || !target->type->deassert_reset)
4219 Jim_SetResult_sprintf(interp,
4220 "No target-specific reset for %s",
4221 target_name(target));
4224 /* determine if we should halt or not. */
4225 target->reset_halt = !!a;
4226 /* When this happens - all workareas are invalid. */
4227 target_free_all_working_areas_restore(target, 0);
4230 if (n->value == NVP_ASSERT) {
4231 e = target->type->assert_reset(target);
4233 e = target->type->deassert_reset(target);
4235 return (e == ERROR_OK) ? JIM_OK : JIM_ERR;
4238 static int jim_target_halt(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
4241 Jim_WrongNumArgs(interp, 1, argv, "[no parameters]");
4244 struct target *target = Jim_CmdPrivData(interp);
4245 if (!target->tap->enabled)
4246 return jim_target_tap_disabled(interp);
4247 int e = target->type->halt(target);
4248 return (e == ERROR_OK) ? JIM_OK : JIM_ERR;
4251 static int jim_target_wait_state(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
4254 Jim_GetOpt_Setup(&goi, interp, argc - 1, argv + 1);
4256 /* params: <name> statename timeoutmsecs */
4259 const char *cmd_name = Jim_GetString(argv[0], NULL);
4260 Jim_SetResult_sprintf(goi.interp,
4261 "%s <state_name> <timeout_in_msec>", cmd_name);
4266 int e = Jim_GetOpt_Nvp(&goi, nvp_target_state, &n);
4268 Jim_GetOpt_NvpUnknown(&goi, nvp_target_state,1);
4272 e = Jim_GetOpt_Wide(&goi, &a);
4276 struct target *target = Jim_CmdPrivData(interp);
4277 if (!target->tap->enabled)
4278 return jim_target_tap_disabled(interp);
4280 e = target_wait_state(target, n->value, a);
4283 Jim_SetResult_sprintf(goi.interp,
4284 "target: %s wait %s fails (%d) %s",
4285 target_name(target), n->name,
4286 e, target_strerror_safe(e));
4291 /* List for human, Events defined for this target.
4292 * scripts/programs should use 'name cget -event NAME'
4294 static int jim_target_event_list(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
4296 struct command_context *cmd_ctx = Jim_GetAssocData(interp, "context");
4297 struct target *target = Jim_CmdPrivData(interp);
4298 struct target_event_action *teap = target->event_action;
4299 command_print(cmd_ctx, "Event actions for target (%d) %s\n",
4300 target->target_number,
4301 target_name(target));
4302 command_print(cmd_ctx, "%-25s | Body", "Event");
4303 command_print(cmd_ctx, "------------------------- | "
4304 "----------------------------------------");
4307 Jim_Nvp *opt = Jim_Nvp_value2name_simple(nvp_target_event, teap->event);
4308 command_print(cmd_ctx, "%-25s | %s",
4309 opt->name, Jim_GetString(teap->body, NULL));
4312 command_print(cmd_ctx, "***END***");
4315 static int jim_target_current_state(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
4319 Jim_WrongNumArgs(interp, 1, argv, "[no parameters]");
4322 struct target *target = Jim_CmdPrivData(interp);
4323 Jim_SetResultString(interp, target_state_name(target), -1);
4326 static int jim_target_invoke_event(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
4329 Jim_GetOpt_Setup(&goi, interp, argc - 1, argv + 1);
4332 const char *cmd_name = Jim_GetString(argv[0], NULL);
4333 Jim_SetResult_sprintf(goi.interp, "%s <eventname>", cmd_name);
4337 int e = Jim_GetOpt_Nvp(&goi, nvp_target_event, &n);
4340 Jim_GetOpt_NvpUnknown(&goi, nvp_target_event, 1);
4343 struct target *target = Jim_CmdPrivData(interp);
4344 target_handle_event(target, n->value);
4348 static const struct command_registration target_instance_command_handlers[] = {
4350 .name = "configure",
4351 .mode = COMMAND_CONFIG,
4352 .jim_handler = jim_target_configure,
4353 .help = "configure a new target for use",
4354 .usage = "[target_attribute ...]",
4358 .mode = COMMAND_ANY,
4359 .jim_handler = jim_target_configure,
4360 .help = "returns the specified target attribute",
4361 .usage = "target_attribute",
4365 .mode = COMMAND_EXEC,
4366 .jim_handler = jim_target_mw,
4367 .help = "Write 32-bit word(s) to target memory",
4368 .usage = "address data [count]",
4372 .mode = COMMAND_EXEC,
4373 .jim_handler = jim_target_mw,
4374 .help = "Write 16-bit half-word(s) to target memory",
4375 .usage = "address data [count]",
4379 .mode = COMMAND_EXEC,
4380 .jim_handler = jim_target_mw,
4381 .help = "Write byte(s) to target memory",
4382 .usage = "address data [count]",
4386 .mode = COMMAND_EXEC,
4387 .jim_handler = jim_target_md,
4388 .help = "Display target memory as 32-bit words",
4389 .usage = "address [count]",
4393 .mode = COMMAND_EXEC,
4394 .jim_handler = jim_target_md,
4395 .help = "Display target memory as 16-bit half-words",
4396 .usage = "address [count]",
4400 .mode = COMMAND_EXEC,
4401 .jim_handler = jim_target_md,
4402 .help = "Display target memory as 8-bit bytes",
4403 .usage = "address [count]",
4406 .name = "array2mem",
4407 .mode = COMMAND_EXEC,
4408 .jim_handler = jim_target_array2mem,
4409 .help = "Writes Tcl array of 8/16/32 bit numbers "
4411 .usage = "arrayname bitwidth address count",
4414 .name = "mem2array",
4415 .mode = COMMAND_EXEC,
4416 .jim_handler = jim_target_mem2array,
4417 .help = "Loads Tcl array of 8/16/32 bit numbers "
4418 "from target memory",
4419 .usage = "arrayname bitwidth address count",
4422 .name = "eventlist",
4423 .mode = COMMAND_EXEC,
4424 .jim_handler = jim_target_event_list,
4425 .help = "displays a table of events defined for this target",
4429 .mode = COMMAND_EXEC,
4430 .jim_handler = jim_target_current_state,
4431 .help = "displays the current state of this target",
4434 .name = "arp_examine",
4435 .mode = COMMAND_EXEC,
4436 .jim_handler = jim_target_examine,
4437 .help = "used internally for reset processing",
4440 .name = "arp_halt_gdb",
4441 .mode = COMMAND_EXEC,
4442 .jim_handler = jim_target_halt_gdb,
4443 .help = "used internally for reset processing to halt GDB",
4447 .mode = COMMAND_EXEC,
4448 .jim_handler = jim_target_poll,
4449 .help = "used internally for reset processing",
4452 .name = "arp_reset",
4453 .mode = COMMAND_EXEC,
4454 .jim_handler = jim_target_reset,
4455 .help = "used internally for reset processing",
4459 .mode = COMMAND_EXEC,
4460 .jim_handler = jim_target_halt,
4461 .help = "used internally for reset processing",
4464 .name = "arp_waitstate",
4465 .mode = COMMAND_EXEC,
4466 .jim_handler = jim_target_wait_state,
4467 .help = "used internally for reset processing",
4470 .name = "invoke-event",
4471 .mode = COMMAND_EXEC,
4472 .jim_handler = jim_target_invoke_event,
4473 .help = "invoke handler for specified event",
4474 .usage = "event_name",
4476 COMMAND_REGISTRATION_DONE
4479 static int target_create(Jim_GetOptInfo *goi)
4487 struct target *target;
4488 struct command_context *cmd_ctx;
4490 cmd_ctx = Jim_GetAssocData(goi->interp, "context");
4491 if (goi->argc < 3) {
4492 Jim_WrongNumArgs(goi->interp, 1, goi->argv, "?name? ?type? ..options...");
4497 Jim_GetOpt_Obj(goi, &new_cmd);
4498 /* does this command exist? */
4499 cmd = Jim_GetCommand(goi->interp, new_cmd, JIM_ERRMSG);
4501 cp = Jim_GetString(new_cmd, NULL);
4502 Jim_SetResult_sprintf(goi->interp, "Command/target: %s Exists", cp);
4507 e = Jim_GetOpt_String(goi, &cp2, NULL);
4509 /* now does target type exist */
4510 for (x = 0 ; target_types[x] ; x++) {
4511 if (0 == strcmp(cp, target_types[x]->name)) {
4516 if (target_types[x] == NULL) {
4517 Jim_SetResult_sprintf(goi->interp, "Unknown target type %s, try one of ", cp);
4518 for (x = 0 ; target_types[x] ; x++) {
4519 if (target_types[x + 1]) {
4520 Jim_AppendStrings(goi->interp,
4521 Jim_GetResult(goi->interp),
4522 target_types[x]->name,
4525 Jim_AppendStrings(goi->interp,
4526 Jim_GetResult(goi->interp),
4528 target_types[x]->name,NULL);
4535 target = calloc(1,sizeof(struct target));
4536 /* set target number */
4537 target->target_number = new_target_number();
4539 /* allocate memory for each unique target type */
4540 target->type = (struct target_type*)calloc(1,sizeof(struct target_type));
4542 memcpy(target->type, target_types[x], sizeof(struct target_type));
4544 /* will be set by "-endian" */
4545 target->endianness = TARGET_ENDIAN_UNKNOWN;
4547 target->working_area = 0x0;
4548 target->working_area_size = 0x0;
4549 target->working_areas = NULL;
4550 target->backup_working_area = 0;
4552 target->state = TARGET_UNKNOWN;
4553 target->debug_reason = DBG_REASON_UNDEFINED;
4554 target->reg_cache = NULL;
4555 target->breakpoints = NULL;
4556 target->watchpoints = NULL;
4557 target->next = NULL;
4558 target->arch_info = NULL;
4560 target->display = 1;
4562 target->halt_issued = false;
4564 /* initialize trace information */
4565 target->trace_info = malloc(sizeof(struct trace));
4566 target->trace_info->num_trace_points = 0;
4567 target->trace_info->trace_points_size = 0;
4568 target->trace_info->trace_points = NULL;
4569 target->trace_info->trace_history_size = 0;
4570 target->trace_info->trace_history = NULL;
4571 target->trace_info->trace_history_pos = 0;
4572 target->trace_info->trace_history_overflowed = 0;
4574 target->dbgmsg = NULL;
4575 target->dbg_msg_enabled = 0;
4577 target->endianness = TARGET_ENDIAN_UNKNOWN;
4579 /* Do the rest as "configure" options */
4580 goi->isconfigure = 1;
4581 e = target_configure(goi, target);
4583 if (target->tap == NULL)
4585 Jim_SetResultString(goi->interp, "-chain-position required when creating target", -1);
4595 if (target->endianness == TARGET_ENDIAN_UNKNOWN) {
4596 /* default endian to little if not specified */
4597 target->endianness = TARGET_LITTLE_ENDIAN;
4600 /* incase variant is not set */
4601 if (!target->variant)
4602 target->variant = strdup("");
4604 cp = Jim_GetString(new_cmd, NULL);
4605 target->cmd_name = strdup(cp);
4607 /* create the target specific commands */
4608 if (target->type->commands) {
4609 e = register_commands(cmd_ctx, NULL, target->type->commands);
4611 LOG_ERROR("unable to register '%s' commands", cp);
4613 if (target->type->target_create) {
4614 (*(target->type->target_create))(target, goi->interp);
4617 /* append to end of list */
4619 struct target **tpp;
4620 tpp = &(all_targets);
4622 tpp = &((*tpp)->next);
4627 /* now - create the new target name command */
4628 const const struct command_registration target_subcommands[] = {
4630 .chain = target_instance_command_handlers,
4633 .chain = target->type->commands,
4635 COMMAND_REGISTRATION_DONE
4637 const const struct command_registration target_commands[] = {
4640 .mode = COMMAND_ANY,
4641 .help = "target command group",
4642 .chain = target_subcommands,
4644 COMMAND_REGISTRATION_DONE
4646 e = register_commands(cmd_ctx, NULL, target_commands);
4650 struct command *c = command_find_in_context(cmd_ctx, cp);
4652 command_set_handler_data(c, target);
4654 return (ERROR_OK == e) ? JIM_OK : JIM_ERR;
4657 static int jim_target_current(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
4661 Jim_WrongNumArgs(interp, 1, argv, "Too many parameters");
4664 struct command_context *cmd_ctx = Jim_GetAssocData(interp, "context");
4665 Jim_SetResultString(interp, get_current_target(cmd_ctx)->cmd_name, -1);
4669 static int jim_target_types(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
4673 Jim_WrongNumArgs(interp, 1, argv, "Too many parameters");
4676 Jim_SetResult(interp, Jim_NewListObj(interp, NULL, 0));
4677 for (unsigned x = 0; NULL != target_types[x]; x++)
4679 Jim_ListAppendElement(interp, Jim_GetResult(interp),
4680 Jim_NewStringObj(interp, target_types[x]->name, -1));
4685 static int jim_target_names(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
4689 Jim_WrongNumArgs(interp, 1, argv, "Too many parameters");
4692 Jim_SetResult(interp, Jim_NewListObj(interp, NULL, 0));
4693 struct target *target = all_targets;
4696 Jim_ListAppendElement(interp, Jim_GetResult(interp),
4697 Jim_NewStringObj(interp, target_name(target), -1));
4698 target = target->next;
4703 static int jim_target_create(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
4706 Jim_GetOpt_Setup(&goi, interp, argc - 1, argv + 1);
4709 Jim_WrongNumArgs(goi.interp, goi.argc, goi.argv,
4710 "<name> <target_type> [<target_options> ...]");
4713 return target_create(&goi);
4716 static int jim_target_number(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
4719 Jim_GetOpt_Setup(&goi, interp, argc - 1, argv + 1);
4721 /* It's OK to remove this mechanism sometime after August 2010 or so */
4722 LOG_WARNING("don't use numbers as target identifiers; use names");
4725 Jim_SetResult_sprintf(goi.interp, "usage: target number <number>");
4729 int e = Jim_GetOpt_Wide(&goi, &w);
4733 struct target *target;
4734 for (target = all_targets; NULL != target; target = target->next)
4736 if (target->target_number != w)
4739 Jim_SetResultString(goi.interp, target_name(target), -1);
4742 Jim_SetResult_sprintf(goi.interp,
4743 "Target: number %d does not exist", (int)(w));
4747 static int jim_target_count(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
4751 Jim_WrongNumArgs(interp, 1, argv, "<no parameters>");
4755 struct target *target = all_targets;
4756 while (NULL != target)
4758 target = target->next;
4761 Jim_SetResult(interp, Jim_NewIntObj(interp, count));
4765 static const struct command_registration target_subcommand_handlers[] = {
4768 .mode = COMMAND_CONFIG,
4769 .handler = handle_target_init_command,
4770 .help = "initialize targets",
4774 /* REVISIT this should be COMMAND_CONFIG ... */
4775 .mode = COMMAND_ANY,
4776 .jim_handler = jim_target_create,
4777 .usage = "name type '-chain-position' name [options ...]",
4778 .help = "Creates and selects a new target",
4782 .mode = COMMAND_ANY,
4783 .jim_handler = jim_target_current,
4784 .help = "Returns the currently selected target",
4788 .mode = COMMAND_ANY,
4789 .jim_handler = jim_target_types,
4790 .help = "Returns the available target types as "
4791 "a list of strings",
4795 .mode = COMMAND_ANY,
4796 .jim_handler = jim_target_names,
4797 .help = "Returns the names of all targets as a list of strings",
4801 .mode = COMMAND_ANY,
4802 .jim_handler = jim_target_number,
4804 .help = "Returns the name of the numbered target "
4809 .mode = COMMAND_ANY,
4810 .jim_handler = jim_target_count,
4811 .help = "Returns the number of targets as an integer "
4814 COMMAND_REGISTRATION_DONE
4825 static int fastload_num;
4826 static struct FastLoad *fastload;
4828 static void free_fastload(void)
4830 if (fastload != NULL)
4833 for (i = 0; i < fastload_num; i++)
4835 if (fastload[i].data)
4836 free(fastload[i].data);
4846 COMMAND_HANDLER(handle_fast_load_image_command)
4850 uint32_t image_size;
4851 uint32_t min_address = 0;
4852 uint32_t max_address = 0xffffffff;
4857 int retval = CALL_COMMAND_HANDLER(parse_load_image_command_CMD_ARGV,
4858 &image, &min_address, &max_address);
4859 if (ERROR_OK != retval)
4862 struct duration bench;
4863 duration_start(&bench);
4865 if (image_open(&image, CMD_ARGV[0], (CMD_ARGC >= 3) ? CMD_ARGV[2] : NULL) != ERROR_OK)
4872 fastload_num = image.num_sections;
4873 fastload = (struct FastLoad *)malloc(sizeof(struct FastLoad)*image.num_sections);
4874 if (fastload == NULL)
4876 image_close(&image);
4879 memset(fastload, 0, sizeof(struct FastLoad)*image.num_sections);
4880 for (i = 0; i < image.num_sections; i++)
4882 buffer = malloc(image.sections[i].size);
4885 command_print(CMD_CTX, "error allocating buffer for section (%d bytes)",
4886 (int)(image.sections[i].size));
4890 if ((retval = image_read_section(&image, i, 0x0, image.sections[i].size, buffer, &buf_cnt)) != ERROR_OK)
4896 uint32_t offset = 0;
4897 uint32_t length = buf_cnt;
4900 /* DANGER!!! beware of unsigned comparision here!!! */
4902 if ((image.sections[i].base_address + buf_cnt >= min_address)&&
4903 (image.sections[i].base_address < max_address))
4905 if (image.sections[i].base_address < min_address)
4907 /* clip addresses below */
4908 offset += min_address-image.sections[i].base_address;
4912 if (image.sections[i].base_address + buf_cnt > max_address)
4914 length -= (image.sections[i].base_address + buf_cnt)-max_address;
4917 fastload[i].address = image.sections[i].base_address + offset;
4918 fastload[i].data = malloc(length);
4919 if (fastload[i].data == NULL)
4924 memcpy(fastload[i].data, buffer + offset, length);
4925 fastload[i].length = length;
4927 image_size += length;
4928 command_print(CMD_CTX, "%u bytes written at address 0x%8.8x",
4929 (unsigned int)length,
4930 ((unsigned int)(image.sections[i].base_address + offset)));
4936 if ((ERROR_OK == retval) && (duration_measure(&bench) == ERROR_OK))
4938 command_print(CMD_CTX, "Loaded %" PRIu32 " bytes "
4939 "in %fs (%0.3f kb/s)", image_size,
4940 duration_elapsed(&bench), duration_kbps(&bench, image_size));
4942 command_print(CMD_CTX,
4943 "WARNING: image has not been loaded to target!"
4944 "You can issue a 'fast_load' to finish loading.");
4947 image_close(&image);
4949 if (retval != ERROR_OK)
4957 COMMAND_HANDLER(handle_fast_load_command)
4960 return ERROR_COMMAND_SYNTAX_ERROR;
4961 if (fastload == NULL)
4963 LOG_ERROR("No image in memory");
4967 int ms = timeval_ms();
4969 int retval = ERROR_OK;
4970 for (i = 0; i < fastload_num;i++)
4972 struct target *target = get_current_target(CMD_CTX);
4973 command_print(CMD_CTX, "Write to 0x%08x, length 0x%08x",
4974 (unsigned int)(fastload[i].address),
4975 (unsigned int)(fastload[i].length));
4976 if (retval == ERROR_OK)
4978 retval = target_write_buffer(target, fastload[i].address, fastload[i].length, fastload[i].data);
4980 size += fastload[i].length;
4982 int after = timeval_ms();
4983 command_print(CMD_CTX, "Loaded image %f kBytes/s", (float)(size/1024.0)/((float)(after-ms)/1000.0));
4987 static const struct command_registration target_command_handlers[] = {
4990 .handler = handle_targets_command,
4991 .mode = COMMAND_ANY,
4992 .help = "change current default target (one parameter) "
4993 "or prints table of all targets (no parameters)",
4994 .usage = "[target]",
4998 .mode = COMMAND_CONFIG,
4999 .help = "configure target",
5001 .chain = target_subcommand_handlers,
5003 COMMAND_REGISTRATION_DONE
5006 int target_register_commands(struct command_context *cmd_ctx)
5008 return register_commands(cmd_ctx, NULL, target_command_handlers);
5011 static bool target_reset_nag = true;
5013 bool get_target_reset_nag(void)
5015 return target_reset_nag;
5018 COMMAND_HANDLER(handle_target_reset_nag)
5020 return CALL_COMMAND_HANDLER(handle_command_parse_bool,
5021 &target_reset_nag, "Nag after each reset about options to improve "
5025 static const struct command_registration target_exec_command_handlers[] = {
5027 .name = "fast_load_image",
5028 .handler = handle_fast_load_image_command,
5029 .mode = COMMAND_ANY,
5030 .help = "Load image into server memory for later use by "
5031 "fast_load; primarily for profiling",
5032 .usage = "filename address ['bin'|'ihex'|'elf'|'s19'] "
5033 "[min_address [max_length]]",
5036 .name = "fast_load",
5037 .handler = handle_fast_load_command,
5038 .mode = COMMAND_EXEC,
5039 .help = "loads active fast load image to current target "
5040 "- mainly for profiling purposes",
5044 .handler = handle_profile_command,
5045 .mode = COMMAND_EXEC,
5046 .help = "profiling samples the CPU PC",
5048 /** @todo don't register virt2phys() unless target supports it */
5050 .name = "virt2phys",
5051 .handler = handle_virt2phys_command,
5052 .mode = COMMAND_ANY,
5053 .help = "translate a virtual address into a physical address",
5054 .usage = "virtual_address",
5058 .handler = handle_reg_command,
5059 .mode = COMMAND_EXEC,
5060 .help = "display or set a register; with no arguments, "
5061 "displays all registers and their values",
5062 .usage = "[(register_name|register_number) [value]]",
5066 .handler = handle_poll_command,
5067 .mode = COMMAND_EXEC,
5068 .help = "poll target state; or reconfigure background polling",
5069 .usage = "['on'|'off']",
5072 .name = "wait_halt",
5073 .handler = handle_wait_halt_command,
5074 .mode = COMMAND_EXEC,
5075 .help = "wait up to the specified number of milliseconds "
5076 "(default 5) for a previously requested halt",
5077 .usage = "[milliseconds]",
5081 .handler = handle_halt_command,
5082 .mode = COMMAND_EXEC,
5083 .help = "request target to halt, then wait up to the specified"
5084 "number of milliseconds (default 5) for it to complete",
5085 .usage = "[milliseconds]",
5089 .handler = handle_resume_command,
5090 .mode = COMMAND_EXEC,
5091 .help = "resume target execution from current PC or address",
5092 .usage = "[address]",
5096 .handler = handle_reset_command,
5097 .mode = COMMAND_EXEC,
5098 .usage = "[run|halt|init]",
5099 .help = "Reset all targets into the specified mode."
5100 "Default reset mode is run, if not given.",
5103 .name = "soft_reset_halt",
5104 .handler = handle_soft_reset_halt_command,
5105 .mode = COMMAND_EXEC,
5106 .help = "halt the target and do a soft reset",
5110 .handler = handle_step_command,
5111 .mode = COMMAND_EXEC,
5112 .help = "step one instruction from current PC or address",
5113 .usage = "[address]",
5117 .handler = handle_md_command,
5118 .mode = COMMAND_EXEC,
5119 .help = "display memory words",
5120 .usage = "['phys'] address [count]",
5124 .handler = handle_md_command,
5125 .mode = COMMAND_EXEC,
5126 .help = "display memory half-words",
5127 .usage = "['phys'] address [count]",
5131 .handler = handle_md_command,
5132 .mode = COMMAND_EXEC,
5133 .help = "display memory bytes",
5134 .usage = "['phys'] address [count]",
5138 .handler = handle_mw_command,
5139 .mode = COMMAND_EXEC,
5140 .help = "write memory word",
5141 .usage = "['phys'] address value [count]",
5145 .handler = handle_mw_command,
5146 .mode = COMMAND_EXEC,
5147 .help = "write memory half-word",
5148 .usage = "['phys'] address value [count]",
5152 .handler = handle_mw_command,
5153 .mode = COMMAND_EXEC,
5154 .help = "write memory byte",
5155 .usage = "['phys'] address value [count]",
5159 .handler = handle_bp_command,
5160 .mode = COMMAND_EXEC,
5161 .help = "list or set hardware or software breakpoint",
5162 .usage = "[address length ['hw']]",
5166 .handler = handle_rbp_command,
5167 .mode = COMMAND_EXEC,
5168 .help = "remove breakpoint",
5173 .handler = handle_wp_command,
5174 .mode = COMMAND_EXEC,
5175 .help = "list (no params) or create watchpoints",
5176 .usage = "[address length [('r'|'w'|'a') value [mask]]]",
5180 .handler = handle_rwp_command,
5181 .mode = COMMAND_EXEC,
5182 .help = "remove watchpoint",
5186 .name = "load_image",
5187 .handler = handle_load_image_command,
5188 .mode = COMMAND_EXEC,
5189 .usage = "filename address ['bin'|'ihex'|'elf'|'s19'] "
5190 "[min_address] [max_length]",
5193 .name = "dump_image",
5194 .handler = handle_dump_image_command,
5195 .mode = COMMAND_EXEC,
5196 .usage = "filename address size",
5199 .name = "verify_image",
5200 .handler = handle_verify_image_command,
5201 .mode = COMMAND_EXEC,
5202 .usage = "filename [offset [type]]",
5205 .name = "test_image",
5206 .handler = handle_test_image_command,
5207 .mode = COMMAND_EXEC,
5208 .usage = "filename [offset [type]]",
5211 .name = "ocd_mem2array",
5212 .mode = COMMAND_EXEC,
5213 .jim_handler = jim_mem2array,
5214 .help = "read 8/16/32 bit memory and return as a TCL array "
5215 "for script processing",
5216 .usage = "arrayname bitwidth address count",
5219 .name = "ocd_array2mem",
5220 .mode = COMMAND_EXEC,
5221 .jim_handler = jim_array2mem,
5222 .help = "convert a TCL array to memory locations "
5223 "and write the 8/16/32 bit values",
5224 .usage = "arrayname bitwidth address count",
5227 .name = "reset_nag",
5228 .handler = handle_target_reset_nag,
5229 .mode = COMMAND_ANY,
5230 .help = "Nag after each reset about options that could have been "
5231 "enabled to improve performance. ",
5232 .usage = "['enable'|'disable']",
5234 COMMAND_REGISTRATION_DONE
5236 int target_register_user_commands(struct command_context *cmd_ctx)
5238 int retval = ERROR_OK;
5239 if ((retval = target_request_register_commands(cmd_ctx)) != ERROR_OK)
5242 if ((retval = trace_register_commands(cmd_ctx)) != ERROR_OK)
5246 return register_commands(cmd_ctx, NULL, target_exec_command_handlers);