1 /***************************************************************************
2 * Copyright (C) 2005 by Dominic Rath *
3 * Dominic.Rath@gmx.de *
5 * Copyright (C) 2007-2010 Øyvind Harboe *
6 * oyvind.harboe@zylin.com *
8 * Copyright (C) 2008, Duane Ellis *
9 * openocd@duaneeellis.com *
11 * Copyright (C) 2008 by Spencer Oliver *
12 * spen@spen-soft.co.uk *
14 * Copyright (C) 2008 by Rick Altherr *
15 * kc8apf@kc8apf.net> *
17 * This program is free software; you can redistribute it and/or modify *
18 * it under the terms of the GNU General Public License as published by *
19 * the Free Software Foundation; either version 2 of the License, or *
20 * (at your option) any later version. *
22 * This program is distributed in the hope that it will be useful, *
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
25 * GNU General Public License for more details. *
27 * You should have received a copy of the GNU General Public License *
28 * along with this program; if not, write to the *
29 * Free Software Foundation, Inc., *
30 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
31 ***************************************************************************/
36 #include <helper/time_support.h>
37 #include <jtag/jtag.h>
38 #include <flash/nor/core.h>
41 #include "target_type.h"
42 #include "target_request.h"
43 #include "breakpoints.h"
49 static int target_array2mem(Jim_Interp *interp, struct target *target,
50 int argc, Jim_Obj *const *argv);
51 static int target_mem2array(Jim_Interp *interp, struct target *target,
52 int argc, Jim_Obj *const *argv);
53 static int target_register_user_commands(struct command_context *cmd_ctx);
56 extern struct target_type arm7tdmi_target;
57 extern struct target_type arm720t_target;
58 extern struct target_type arm9tdmi_target;
59 extern struct target_type arm920t_target;
60 extern struct target_type arm966e_target;
61 extern struct target_type arm926ejs_target;
62 extern struct target_type fa526_target;
63 extern struct target_type feroceon_target;
64 extern struct target_type dragonite_target;
65 extern struct target_type xscale_target;
66 extern struct target_type cortexm3_target;
67 extern struct target_type cortexa8_target;
68 extern struct target_type arm11_target;
69 extern struct target_type mips_m4k_target;
70 extern struct target_type avr_target;
71 extern struct target_type dsp563xx_target;
72 extern struct target_type testee_target;
73 extern struct target_type avr32_ap7k_target;
75 static struct target_type *target_types[] =
98 struct target *all_targets = NULL;
99 static struct target_event_callback *target_event_callbacks = NULL;
100 static struct target_timer_callback *target_timer_callbacks = NULL;
101 static const int polling_interval = 100;
103 static const Jim_Nvp nvp_assert[] = {
104 { .name = "assert", NVP_ASSERT },
105 { .name = "deassert", NVP_DEASSERT },
106 { .name = "T", NVP_ASSERT },
107 { .name = "F", NVP_DEASSERT },
108 { .name = "t", NVP_ASSERT },
109 { .name = "f", NVP_DEASSERT },
110 { .name = NULL, .value = -1 }
113 static const Jim_Nvp nvp_error_target[] = {
114 { .value = ERROR_TARGET_INVALID, .name = "err-invalid" },
115 { .value = ERROR_TARGET_INIT_FAILED, .name = "err-init-failed" },
116 { .value = ERROR_TARGET_TIMEOUT, .name = "err-timeout" },
117 { .value = ERROR_TARGET_NOT_HALTED, .name = "err-not-halted" },
118 { .value = ERROR_TARGET_FAILURE, .name = "err-failure" },
119 { .value = ERROR_TARGET_UNALIGNED_ACCESS , .name = "err-unaligned-access" },
120 { .value = ERROR_TARGET_DATA_ABORT , .name = "err-data-abort" },
121 { .value = ERROR_TARGET_RESOURCE_NOT_AVAILABLE , .name = "err-resource-not-available" },
122 { .value = ERROR_TARGET_TRANSLATION_FAULT , .name = "err-translation-fault" },
123 { .value = ERROR_TARGET_NOT_RUNNING, .name = "err-not-running" },
124 { .value = ERROR_TARGET_NOT_EXAMINED, .name = "err-not-examined" },
125 { .value = -1, .name = NULL }
128 static const char *target_strerror_safe(int err)
132 n = Jim_Nvp_value2name_simple(nvp_error_target, err);
133 if (n->name == NULL) {
140 static const Jim_Nvp nvp_target_event[] = {
141 { .value = TARGET_EVENT_OLD_gdb_program_config , .name = "old-gdb_program_config" },
142 { .value = TARGET_EVENT_OLD_pre_resume , .name = "old-pre_resume" },
144 { .value = TARGET_EVENT_GDB_HALT, .name = "gdb-halt" },
145 { .value = TARGET_EVENT_HALTED, .name = "halted" },
146 { .value = TARGET_EVENT_RESUMED, .name = "resumed" },
147 { .value = TARGET_EVENT_RESUME_START, .name = "resume-start" },
148 { .value = TARGET_EVENT_RESUME_END, .name = "resume-end" },
150 { .name = "gdb-start", .value = TARGET_EVENT_GDB_START },
151 { .name = "gdb-end", .value = TARGET_EVENT_GDB_END },
153 /* historical name */
155 { .value = TARGET_EVENT_RESET_START, .name = "reset-start" },
157 { .value = TARGET_EVENT_RESET_ASSERT_PRE, .name = "reset-assert-pre" },
158 { .value = TARGET_EVENT_RESET_ASSERT, .name = "reset-assert" },
159 { .value = TARGET_EVENT_RESET_ASSERT_POST, .name = "reset-assert-post" },
160 { .value = TARGET_EVENT_RESET_DEASSERT_PRE, .name = "reset-deassert-pre" },
161 { .value = TARGET_EVENT_RESET_DEASSERT_POST, .name = "reset-deassert-post" },
162 { .value = TARGET_EVENT_RESET_HALT_PRE, .name = "reset-halt-pre" },
163 { .value = TARGET_EVENT_RESET_HALT_POST, .name = "reset-halt-post" },
164 { .value = TARGET_EVENT_RESET_WAIT_PRE, .name = "reset-wait-pre" },
165 { .value = TARGET_EVENT_RESET_WAIT_POST, .name = "reset-wait-post" },
166 { .value = TARGET_EVENT_RESET_INIT, .name = "reset-init" },
167 { .value = TARGET_EVENT_RESET_END, .name = "reset-end" },
169 { .value = TARGET_EVENT_EXAMINE_START, .name = "examine-start" },
170 { .value = TARGET_EVENT_EXAMINE_END, .name = "examine-end" },
172 { .value = TARGET_EVENT_DEBUG_HALTED, .name = "debug-halted" },
173 { .value = TARGET_EVENT_DEBUG_RESUMED, .name = "debug-resumed" },
175 { .value = TARGET_EVENT_GDB_ATTACH, .name = "gdb-attach" },
176 { .value = TARGET_EVENT_GDB_DETACH, .name = "gdb-detach" },
178 { .value = TARGET_EVENT_GDB_FLASH_WRITE_START, .name = "gdb-flash-write-start" },
179 { .value = TARGET_EVENT_GDB_FLASH_WRITE_END , .name = "gdb-flash-write-end" },
181 { .value = TARGET_EVENT_GDB_FLASH_ERASE_START, .name = "gdb-flash-erase-start" },
182 { .value = TARGET_EVENT_GDB_FLASH_ERASE_END , .name = "gdb-flash-erase-end" },
184 { .value = TARGET_EVENT_RESUME_START, .name = "resume-start" },
185 { .value = TARGET_EVENT_RESUMED , .name = "resume-ok" },
186 { .value = TARGET_EVENT_RESUME_END , .name = "resume-end" },
188 { .name = NULL, .value = -1 }
191 static const Jim_Nvp nvp_target_state[] = {
192 { .name = "unknown", .value = TARGET_UNKNOWN },
193 { .name = "running", .value = TARGET_RUNNING },
194 { .name = "halted", .value = TARGET_HALTED },
195 { .name = "reset", .value = TARGET_RESET },
196 { .name = "debug-running", .value = TARGET_DEBUG_RUNNING },
197 { .name = NULL, .value = -1 },
200 static const Jim_Nvp nvp_target_debug_reason [] = {
201 { .name = "debug-request" , .value = DBG_REASON_DBGRQ },
202 { .name = "breakpoint" , .value = DBG_REASON_BREAKPOINT },
203 { .name = "watchpoint" , .value = DBG_REASON_WATCHPOINT },
204 { .name = "watchpoint-and-breakpoint", .value = DBG_REASON_WPTANDBKPT },
205 { .name = "single-step" , .value = DBG_REASON_SINGLESTEP },
206 { .name = "target-not-halted" , .value = DBG_REASON_NOTHALTED },
207 { .name = "undefined" , .value = DBG_REASON_UNDEFINED },
208 { .name = NULL, .value = -1 },
211 static const Jim_Nvp nvp_target_endian[] = {
212 { .name = "big", .value = TARGET_BIG_ENDIAN },
213 { .name = "little", .value = TARGET_LITTLE_ENDIAN },
214 { .name = "be", .value = TARGET_BIG_ENDIAN },
215 { .name = "le", .value = TARGET_LITTLE_ENDIAN },
216 { .name = NULL, .value = -1 },
219 static const Jim_Nvp nvp_reset_modes[] = {
220 { .name = "unknown", .value = RESET_UNKNOWN },
221 { .name = "run" , .value = RESET_RUN },
222 { .name = "halt" , .value = RESET_HALT },
223 { .name = "init" , .value = RESET_INIT },
224 { .name = NULL , .value = -1 },
227 const char *debug_reason_name(struct target *t)
231 cp = Jim_Nvp_value2name_simple(nvp_target_debug_reason,
232 t->debug_reason)->name;
234 LOG_ERROR("Invalid debug reason: %d", (int)(t->debug_reason));
235 cp = "(*BUG*unknown*BUG*)";
241 target_state_name( struct target *t )
244 cp = Jim_Nvp_value2name_simple(nvp_target_state, t->state)->name;
246 LOG_ERROR("Invalid target state: %d", (int)(t->state));
247 cp = "(*BUG*unknown*BUG*)";
252 /* determine the number of the new target */
253 static int new_target_number(void)
258 /* number is 0 based */
262 if (x < t->target_number) {
263 x = t->target_number;
270 /* read a uint32_t from a buffer in target memory endianness */
271 uint32_t target_buffer_get_u32(struct target *target, const uint8_t *buffer)
273 if (target->endianness == TARGET_LITTLE_ENDIAN)
274 return le_to_h_u32(buffer);
276 return be_to_h_u32(buffer);
279 /* read a uint16_t from a buffer in target memory endianness */
280 uint16_t target_buffer_get_u16(struct target *target, const uint8_t *buffer)
282 if (target->endianness == TARGET_LITTLE_ENDIAN)
283 return le_to_h_u16(buffer);
285 return be_to_h_u16(buffer);
288 /* read a uint8_t from a buffer in target memory endianness */
289 static uint8_t target_buffer_get_u8(struct target *target, const uint8_t *buffer)
291 return *buffer & 0x0ff;
294 /* write a uint32_t to a buffer in target memory endianness */
295 void target_buffer_set_u32(struct target *target, uint8_t *buffer, uint32_t value)
297 if (target->endianness == TARGET_LITTLE_ENDIAN)
298 h_u32_to_le(buffer, value);
300 h_u32_to_be(buffer, value);
303 /* write a uint16_t to a buffer in target memory endianness */
304 void target_buffer_set_u16(struct target *target, uint8_t *buffer, uint16_t value)
306 if (target->endianness == TARGET_LITTLE_ENDIAN)
307 h_u16_to_le(buffer, value);
309 h_u16_to_be(buffer, value);
312 /* write a uint8_t to a buffer in target memory endianness */
313 static void target_buffer_set_u8(struct target *target, uint8_t *buffer, uint8_t value)
318 /* return a pointer to a configured target; id is name or number */
319 struct target *get_target(const char *id)
321 struct target *target;
323 /* try as tcltarget name */
324 for (target = all_targets; target; target = target->next) {
325 if (target->cmd_name == NULL)
327 if (strcmp(id, target->cmd_name) == 0)
331 /* It's OK to remove this fallback sometime after August 2010 or so */
333 /* no match, try as number */
335 if (parse_uint(id, &num) != ERROR_OK)
338 for (target = all_targets; target; target = target->next) {
339 if (target->target_number == (int)num) {
340 LOG_WARNING("use '%s' as target identifier, not '%u'",
341 target->cmd_name, num);
349 /* returns a pointer to the n-th configured target */
350 static struct target *get_target_by_num(int num)
352 struct target *target = all_targets;
355 if (target->target_number == num) {
358 target = target->next;
364 struct target* get_current_target(struct command_context *cmd_ctx)
366 struct target *target = get_target_by_num(cmd_ctx->current_target);
370 LOG_ERROR("BUG: current_target out of bounds");
377 int target_poll(struct target *target)
381 /* We can't poll until after examine */
382 if (!target_was_examined(target))
384 /* Fail silently lest we pollute the log */
388 retval = target->type->poll(target);
389 if (retval != ERROR_OK)
392 if (target->halt_issued)
394 if (target->state == TARGET_HALTED)
396 target->halt_issued = false;
399 long long t = timeval_ms() - target->halt_issued_time;
402 target->halt_issued = false;
403 LOG_INFO("Halt timed out, wake up GDB.");
404 target_call_event_callbacks(target, TARGET_EVENT_GDB_HALT);
412 int target_halt(struct target *target)
415 /* We can't poll until after examine */
416 if (!target_was_examined(target))
418 LOG_ERROR("Target not examined yet");
422 retval = target->type->halt(target);
423 if (retval != ERROR_OK)
426 target->halt_issued = true;
427 target->halt_issued_time = timeval_ms();
433 * Make the target (re)start executing using its saved execution
434 * context (possibly with some modifications).
436 * @param target Which target should start executing.
437 * @param current True to use the target's saved program counter instead
438 * of the address parameter
439 * @param address Optionally used as the program counter.
440 * @param handle_breakpoints True iff breakpoints at the resumption PC
441 * should be skipped. (For example, maybe execution was stopped by
442 * such a breakpoint, in which case it would be counterprodutive to
444 * @param debug_execution False if all working areas allocated by OpenOCD
445 * should be released and/or restored to their original contents.
446 * (This would for example be true to run some downloaded "helper"
447 * algorithm code, which resides in one such working buffer and uses
448 * another for data storage.)
450 * @todo Resolve the ambiguity about what the "debug_execution" flag
451 * signifies. For example, Target implementations don't agree on how
452 * it relates to invalidation of the register cache, or to whether
453 * breakpoints and watchpoints should be enabled. (It would seem wrong
454 * to enable breakpoints when running downloaded "helper" algorithms
455 * (debug_execution true), since the breakpoints would be set to match
456 * target firmware being debugged, not the helper algorithm.... and
457 * enabling them could cause such helpers to malfunction (for example,
458 * by overwriting data with a breakpoint instruction. On the other
459 * hand the infrastructure for running such helpers might use this
460 * procedure but rely on hardware breakpoint to detect termination.)
462 int target_resume(struct target *target, int current, uint32_t address, int handle_breakpoints, int debug_execution)
466 /* We can't poll until after examine */
467 if (!target_was_examined(target))
469 LOG_ERROR("Target not examined yet");
473 /* note that resume *must* be asynchronous. The CPU can halt before
474 * we poll. The CPU can even halt at the current PC as a result of
475 * a software breakpoint being inserted by (a bug?) the application.
477 if ((retval = target->type->resume(target, current, address, handle_breakpoints, debug_execution)) != ERROR_OK)
483 static int target_process_reset(struct command_context *cmd_ctx, enum target_reset_mode reset_mode)
488 n = Jim_Nvp_value2name_simple(nvp_reset_modes, reset_mode);
489 if (n->name == NULL) {
490 LOG_ERROR("invalid reset mode");
494 /* disable polling during reset to make reset event scripts
495 * more predictable, i.e. dr/irscan & pathmove in events will
496 * not have JTAG operations injected into the middle of a sequence.
498 bool save_poll = jtag_poll_get_enabled();
500 jtag_poll_set_enabled(false);
502 sprintf(buf, "ocd_process_reset %s", n->name);
503 retval = Jim_Eval(cmd_ctx->interp, buf);
505 jtag_poll_set_enabled(save_poll);
507 if (retval != JIM_OK) {
508 Jim_MakeErrorMessage(cmd_ctx->interp);
509 command_print(NULL,"%s\n", Jim_GetString(Jim_GetResult(cmd_ctx->interp), NULL));
513 /* We want any events to be processed before the prompt */
514 retval = target_call_timer_callbacks_now();
516 struct target *target;
517 for (target = all_targets; target; target = target->next) {
518 target->type->check_reset(target);
524 static int identity_virt2phys(struct target *target,
525 uint32_t virtual, uint32_t *physical)
531 static int no_mmu(struct target *target, int *enabled)
537 static int default_examine(struct target *target)
539 target_set_examined(target);
543 /* no check by default */
544 static int default_check_reset(struct target *target)
549 int target_examine_one(struct target *target)
551 return target->type->examine(target);
554 static int jtag_enable_callback(enum jtag_event event, void *priv)
556 struct target *target = priv;
558 if (event != JTAG_TAP_EVENT_ENABLE || !target->tap->enabled)
561 jtag_unregister_event_callback(jtag_enable_callback, target);
562 return target_examine_one(target);
566 /* Targets that correctly implement init + examine, i.e.
567 * no communication with target during init:
571 int target_examine(void)
573 int retval = ERROR_OK;
574 struct target *target;
576 for (target = all_targets; target; target = target->next)
578 /* defer examination, but don't skip it */
579 if (!target->tap->enabled) {
580 jtag_register_event_callback(jtag_enable_callback,
584 if ((retval = target_examine_one(target)) != ERROR_OK)
589 const char *target_type_name(struct target *target)
591 return target->type->name;
594 static int target_write_memory_imp(struct target *target, uint32_t address, uint32_t size, uint32_t count, uint8_t *buffer)
596 if (!target_was_examined(target))
598 LOG_ERROR("Target not examined yet");
601 return target->type->write_memory_imp(target, address, size, count, buffer);
604 static int target_read_memory_imp(struct target *target, uint32_t address, uint32_t size, uint32_t count, uint8_t *buffer)
606 if (!target_was_examined(target))
608 LOG_ERROR("Target not examined yet");
611 return target->type->read_memory_imp(target, address, size, count, buffer);
614 static int target_soft_reset_halt_imp(struct target *target)
616 if (!target_was_examined(target))
618 LOG_ERROR("Target not examined yet");
621 if (!target->type->soft_reset_halt_imp) {
622 LOG_ERROR("Target %s does not support soft_reset_halt",
623 target_name(target));
626 return target->type->soft_reset_halt_imp(target);
630 * Downloads a target-specific native code algorithm to the target,
631 * and executes it. * Note that some targets may need to set up, enable,
632 * and tear down a breakpoint (hard or * soft) to detect algorithm
633 * termination, while others may support lower overhead schemes where
634 * soft breakpoints embedded in the algorithm automatically terminate the
637 * @param target used to run the algorithm
638 * @param arch_info target-specific description of the algorithm.
640 int target_run_algorithm(struct target *target,
641 int num_mem_params, struct mem_param *mem_params,
642 int num_reg_params, struct reg_param *reg_param,
643 uint32_t entry_point, uint32_t exit_point,
644 int timeout_ms, void *arch_info)
646 int retval = ERROR_FAIL;
648 if (!target_was_examined(target))
650 LOG_ERROR("Target not examined yet");
653 if (!target->type->run_algorithm) {
654 LOG_ERROR("Target type '%s' does not support %s",
655 target_type_name(target), __func__);
659 target->running_alg = true;
660 retval = target->type->run_algorithm(target,
661 num_mem_params, mem_params,
662 num_reg_params, reg_param,
663 entry_point, exit_point, timeout_ms, arch_info);
664 target->running_alg = false;
671 int target_read_memory(struct target *target,
672 uint32_t address, uint32_t size, uint32_t count, uint8_t *buffer)
674 return target->type->read_memory(target, address, size, count, buffer);
677 static int target_read_phys_memory(struct target *target,
678 uint32_t address, uint32_t size, uint32_t count, uint8_t *buffer)
680 return target->type->read_phys_memory(target, address, size, count, buffer);
683 int target_write_memory(struct target *target,
684 uint32_t address, uint32_t size, uint32_t count, uint8_t *buffer)
686 return target->type->write_memory(target, address, size, count, buffer);
689 static int target_write_phys_memory(struct target *target,
690 uint32_t address, uint32_t size, uint32_t count, uint8_t *buffer)
692 return target->type->write_phys_memory(target, address, size, count, buffer);
695 int target_bulk_write_memory(struct target *target,
696 uint32_t address, uint32_t count, uint8_t *buffer)
698 return target->type->bulk_write_memory(target, address, count, buffer);
701 int target_add_breakpoint(struct target *target,
702 struct breakpoint *breakpoint)
704 if (target->state != TARGET_HALTED) {
705 LOG_WARNING("target %s is not halted", target->cmd_name);
706 return ERROR_TARGET_NOT_HALTED;
708 return target->type->add_breakpoint(target, breakpoint);
710 int target_remove_breakpoint(struct target *target,
711 struct breakpoint *breakpoint)
713 return target->type->remove_breakpoint(target, breakpoint);
716 int target_add_watchpoint(struct target *target,
717 struct watchpoint *watchpoint)
719 if (target->state != TARGET_HALTED) {
720 LOG_WARNING("target %s is not halted", target->cmd_name);
721 return ERROR_TARGET_NOT_HALTED;
723 return target->type->add_watchpoint(target, watchpoint);
725 int target_remove_watchpoint(struct target *target,
726 struct watchpoint *watchpoint)
728 return target->type->remove_watchpoint(target, watchpoint);
731 int target_get_gdb_reg_list(struct target *target,
732 struct reg **reg_list[], int *reg_list_size)
734 return target->type->get_gdb_reg_list(target, reg_list, reg_list_size);
736 int target_step(struct target *target,
737 int current, uint32_t address, int handle_breakpoints)
739 return target->type->step(target, current, address, handle_breakpoints);
744 * Reset the @c examined flag for the given target.
745 * Pure paranoia -- targets are zeroed on allocation.
747 static void target_reset_examined(struct target *target)
749 target->examined = false;
753 err_read_phys_memory(struct target *target, uint32_t address,
754 uint32_t size, uint32_t count, uint8_t *buffer)
756 LOG_ERROR("Not implemented: %s", __func__);
761 err_write_phys_memory(struct target *target, uint32_t address,
762 uint32_t size, uint32_t count, uint8_t *buffer)
764 LOG_ERROR("Not implemented: %s", __func__);
768 static int handle_target(void *priv);
770 static int target_init_one(struct command_context *cmd_ctx,
771 struct target *target)
773 target_reset_examined(target);
775 struct target_type *type = target->type;
776 if (type->examine == NULL)
777 type->examine = default_examine;
779 if (type->check_reset== NULL)
780 type->check_reset = default_check_reset;
782 int retval = type->init_target(cmd_ctx, target);
783 if (ERROR_OK != retval)
785 LOG_ERROR("target '%s' init failed", target_name(target));
790 * @todo get rid of those *memory_imp() methods, now that all
791 * callers are using target_*_memory() accessors ... and make
792 * sure the "physical" paths handle the same issues.
794 /* a non-invasive way(in terms of patches) to add some code that
795 * runs before the type->write/read_memory implementation
797 type->write_memory_imp = target->type->write_memory;
798 type->write_memory = target_write_memory_imp;
800 type->read_memory_imp = target->type->read_memory;
801 type->read_memory = target_read_memory_imp;
803 type->soft_reset_halt_imp = target->type->soft_reset_halt;
804 type->soft_reset_halt = target_soft_reset_halt_imp;
806 /* Sanity-check MMU support ... stub in what we must, to help
807 * implement it in stages, but warn if we need to do so.
811 if (type->write_phys_memory == NULL)
813 LOG_ERROR("type '%s' is missing write_phys_memory",
815 type->write_phys_memory = err_write_phys_memory;
817 if (type->read_phys_memory == NULL)
819 LOG_ERROR("type '%s' is missing read_phys_memory",
821 type->read_phys_memory = err_read_phys_memory;
823 if (type->virt2phys == NULL)
825 LOG_ERROR("type '%s' is missing virt2phys", type->name);
826 type->virt2phys = identity_virt2phys;
831 /* Make sure no-MMU targets all behave the same: make no
832 * distinction between physical and virtual addresses, and
833 * ensure that virt2phys() is always an identity mapping.
835 if (type->write_phys_memory || type->read_phys_memory
838 LOG_WARNING("type '%s' has bad MMU hooks", type->name);
842 type->write_phys_memory = type->write_memory;
843 type->read_phys_memory = type->read_memory;
844 type->virt2phys = identity_virt2phys;
849 static int target_init(struct command_context *cmd_ctx)
851 struct target *target;
854 for (target = all_targets; target; target = target->next)
856 retval = target_init_one(cmd_ctx, target);
857 if (ERROR_OK != retval)
864 retval = target_register_user_commands(cmd_ctx);
865 if (ERROR_OK != retval)
868 retval = target_register_timer_callback(&handle_target,
869 polling_interval, 1, cmd_ctx->interp);
870 if (ERROR_OK != retval)
876 COMMAND_HANDLER(handle_target_init_command)
879 return ERROR_COMMAND_SYNTAX_ERROR;
881 static bool target_initialized = false;
882 if (target_initialized)
884 LOG_INFO("'target init' has already been called");
887 target_initialized = true;
889 LOG_DEBUG("Initializing targets...");
890 return target_init(CMD_CTX);
893 int target_register_event_callback(int (*callback)(struct target *target, enum target_event event, void *priv), void *priv)
895 struct target_event_callback **callbacks_p = &target_event_callbacks;
897 if (callback == NULL)
899 return ERROR_INVALID_ARGUMENTS;
904 while ((*callbacks_p)->next)
905 callbacks_p = &((*callbacks_p)->next);
906 callbacks_p = &((*callbacks_p)->next);
909 (*callbacks_p) = malloc(sizeof(struct target_event_callback));
910 (*callbacks_p)->callback = callback;
911 (*callbacks_p)->priv = priv;
912 (*callbacks_p)->next = NULL;
917 int target_register_timer_callback(int (*callback)(void *priv), int time_ms, int periodic, void *priv)
919 struct target_timer_callback **callbacks_p = &target_timer_callbacks;
922 if (callback == NULL)
924 return ERROR_INVALID_ARGUMENTS;
929 while ((*callbacks_p)->next)
930 callbacks_p = &((*callbacks_p)->next);
931 callbacks_p = &((*callbacks_p)->next);
934 (*callbacks_p) = malloc(sizeof(struct target_timer_callback));
935 (*callbacks_p)->callback = callback;
936 (*callbacks_p)->periodic = periodic;
937 (*callbacks_p)->time_ms = time_ms;
939 gettimeofday(&now, NULL);
940 (*callbacks_p)->when.tv_usec = now.tv_usec + (time_ms % 1000) * 1000;
941 time_ms -= (time_ms % 1000);
942 (*callbacks_p)->when.tv_sec = now.tv_sec + (time_ms / 1000);
943 if ((*callbacks_p)->when.tv_usec > 1000000)
945 (*callbacks_p)->when.tv_usec = (*callbacks_p)->when.tv_usec - 1000000;
946 (*callbacks_p)->when.tv_sec += 1;
949 (*callbacks_p)->priv = priv;
950 (*callbacks_p)->next = NULL;
955 int target_unregister_event_callback(int (*callback)(struct target *target, enum target_event event, void *priv), void *priv)
957 struct target_event_callback **p = &target_event_callbacks;
958 struct target_event_callback *c = target_event_callbacks;
960 if (callback == NULL)
962 return ERROR_INVALID_ARGUMENTS;
967 struct target_event_callback *next = c->next;
968 if ((c->callback == callback) && (c->priv == priv))
982 static int target_unregister_timer_callback(int (*callback)(void *priv), void *priv)
984 struct target_timer_callback **p = &target_timer_callbacks;
985 struct target_timer_callback *c = target_timer_callbacks;
987 if (callback == NULL)
989 return ERROR_INVALID_ARGUMENTS;
994 struct target_timer_callback *next = c->next;
995 if ((c->callback == callback) && (c->priv == priv))
1009 int target_call_event_callbacks(struct target *target, enum target_event event)
1011 struct target_event_callback *callback = target_event_callbacks;
1012 struct target_event_callback *next_callback;
1014 if (event == TARGET_EVENT_HALTED)
1016 /* execute early halted first */
1017 target_call_event_callbacks(target, TARGET_EVENT_GDB_HALT);
1020 LOG_DEBUG("target event %i (%s)",
1022 Jim_Nvp_value2name_simple(nvp_target_event, event)->name);
1024 target_handle_event(target, event);
1028 next_callback = callback->next;
1029 callback->callback(target, event, callback->priv);
1030 callback = next_callback;
1036 static int target_timer_callback_periodic_restart(
1037 struct target_timer_callback *cb, struct timeval *now)
1039 int time_ms = cb->time_ms;
1040 cb->when.tv_usec = now->tv_usec + (time_ms % 1000) * 1000;
1041 time_ms -= (time_ms % 1000);
1042 cb->when.tv_sec = now->tv_sec + time_ms / 1000;
1043 if (cb->when.tv_usec > 1000000)
1045 cb->when.tv_usec = cb->when.tv_usec - 1000000;
1046 cb->when.tv_sec += 1;
1051 static int target_call_timer_callback(struct target_timer_callback *cb,
1052 struct timeval *now)
1054 cb->callback(cb->priv);
1057 return target_timer_callback_periodic_restart(cb, now);
1059 return target_unregister_timer_callback(cb->callback, cb->priv);
1062 static int target_call_timer_callbacks_check_time(int checktime)
1067 gettimeofday(&now, NULL);
1069 struct target_timer_callback *callback = target_timer_callbacks;
1072 // cleaning up may unregister and free this callback
1073 struct target_timer_callback *next_callback = callback->next;
1075 bool call_it = callback->callback &&
1076 ((!checktime && callback->periodic) ||
1077 now.tv_sec > callback->when.tv_sec ||
1078 (now.tv_sec == callback->when.tv_sec &&
1079 now.tv_usec >= callback->when.tv_usec));
1083 int retval = target_call_timer_callback(callback, &now);
1084 if (retval != ERROR_OK)
1088 callback = next_callback;
1094 int target_call_timer_callbacks(void)
1096 return target_call_timer_callbacks_check_time(1);
1099 /* invoke periodic callbacks immediately */
1100 int target_call_timer_callbacks_now(void)
1102 return target_call_timer_callbacks_check_time(0);
1105 int target_alloc_working_area_try(struct target *target, uint32_t size, struct working_area **area)
1107 struct working_area *c = target->working_areas;
1108 struct working_area *new_wa = NULL;
1110 /* Reevaluate working area address based on MMU state*/
1111 if (target->working_areas == NULL)
1116 retval = target->type->mmu(target, &enabled);
1117 if (retval != ERROR_OK)
1123 if (target->working_area_phys_spec) {
1124 LOG_DEBUG("MMU disabled, using physical "
1125 "address for working memory 0x%08x",
1126 (unsigned)target->working_area_phys);
1127 target->working_area = target->working_area_phys;
1129 LOG_ERROR("No working memory available. "
1130 "Specify -work-area-phys to target.");
1131 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
1134 if (target->working_area_virt_spec) {
1135 LOG_DEBUG("MMU enabled, using virtual "
1136 "address for working memory 0x%08x",
1137 (unsigned)target->working_area_virt);
1138 target->working_area = target->working_area_virt;
1140 LOG_ERROR("No working memory available. "
1141 "Specify -work-area-virt to target.");
1142 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
1147 /* only allocate multiples of 4 byte */
1150 LOG_ERROR("BUG: code tried to allocate unaligned number of bytes (0x%08x), padding", ((unsigned)(size)));
1151 size = (size + 3) & (~3);
1154 /* see if there's already a matching working area */
1157 if ((c->free) && (c->size == size))
1165 /* if not, allocate a new one */
1168 struct working_area **p = &target->working_areas;
1169 uint32_t first_free = target->working_area;
1170 uint32_t free_size = target->working_area_size;
1172 c = target->working_areas;
1175 first_free += c->size;
1176 free_size -= c->size;
1181 if (free_size < size)
1183 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
1186 LOG_DEBUG("allocated new working area at address 0x%08x", (unsigned)first_free);
1188 new_wa = malloc(sizeof(struct working_area));
1189 new_wa->next = NULL;
1190 new_wa->size = size;
1191 new_wa->address = first_free;
1193 if (target->backup_working_area)
1196 new_wa->backup = malloc(new_wa->size);
1197 if ((retval = target_read_memory(target, new_wa->address, 4, new_wa->size / 4, new_wa->backup)) != ERROR_OK)
1199 free(new_wa->backup);
1206 new_wa->backup = NULL;
1209 /* put new entry in list */
1213 /* mark as used, and return the new (reused) area */
1218 new_wa->user = area;
1223 int target_alloc_working_area(struct target *target, uint32_t size, struct working_area **area)
1227 retval = target_alloc_working_area_try(target, size, area);
1228 if (retval == ERROR_TARGET_RESOURCE_NOT_AVAILABLE)
1230 LOG_WARNING("not enough working area available(requested %u)", (unsigned)(size));
1236 static int target_free_working_area_restore(struct target *target, struct working_area *area, int restore)
1241 if (restore && target->backup_working_area)
1244 if ((retval = target_write_memory(target, area->address, 4, area->size / 4, area->backup)) != ERROR_OK)
1250 /* mark user pointer invalid */
1257 int target_free_working_area(struct target *target, struct working_area *area)
1259 return target_free_working_area_restore(target, area, 1);
1262 /* free resources and restore memory, if restoring memory fails,
1263 * free up resources anyway
1265 static void target_free_all_working_areas_restore(struct target *target, int restore)
1267 struct working_area *c = target->working_areas;
1271 struct working_area *next = c->next;
1272 target_free_working_area_restore(target, c, restore);
1282 target->working_areas = NULL;
1285 void target_free_all_working_areas(struct target *target)
1287 target_free_all_working_areas_restore(target, 1);
1290 int target_arch_state(struct target *target)
1295 LOG_USER("No target has been configured");
1299 LOG_USER("target state: %s", target_state_name( target ));
1301 if (target->state != TARGET_HALTED)
1304 retval = target->type->arch_state(target);
1308 /* Single aligned words are guaranteed to use 16 or 32 bit access
1309 * mode respectively, otherwise data is handled as quickly as
1312 int target_write_buffer(struct target *target, uint32_t address, uint32_t size, uint8_t *buffer)
1315 LOG_DEBUG("writing buffer of %i byte at 0x%8.8x",
1316 (int)size, (unsigned)address);
1318 if (!target_was_examined(target))
1320 LOG_ERROR("Target not examined yet");
1328 if ((address + size - 1) < address)
1330 /* GDB can request this when e.g. PC is 0xfffffffc*/
1331 LOG_ERROR("address + size wrapped(0x%08x, 0x%08x)",
1337 if (((address % 2) == 0) && (size == 2))
1339 return target_write_memory(target, address, 2, 1, buffer);
1342 /* handle unaligned head bytes */
1345 uint32_t unaligned = 4 - (address % 4);
1347 if (unaligned > size)
1350 if ((retval = target_write_memory(target, address, 1, unaligned, buffer)) != ERROR_OK)
1353 buffer += unaligned;
1354 address += unaligned;
1358 /* handle aligned words */
1361 int aligned = size - (size % 4);
1363 /* use bulk writes above a certain limit. This may have to be changed */
1366 if ((retval = target->type->bulk_write_memory(target, address, aligned / 4, buffer)) != ERROR_OK)
1371 if ((retval = target_write_memory(target, address, 4, aligned / 4, buffer)) != ERROR_OK)
1380 /* handle tail writes of less than 4 bytes */
1383 if ((retval = target_write_memory(target, address, 1, size, buffer)) != ERROR_OK)
1390 /* Single aligned words are guaranteed to use 16 or 32 bit access
1391 * mode respectively, otherwise data is handled as quickly as
1394 int target_read_buffer(struct target *target, uint32_t address, uint32_t size, uint8_t *buffer)
1397 LOG_DEBUG("reading buffer of %i byte at 0x%8.8x",
1398 (int)size, (unsigned)address);
1400 if (!target_was_examined(target))
1402 LOG_ERROR("Target not examined yet");
1410 if ((address + size - 1) < address)
1412 /* GDB can request this when e.g. PC is 0xfffffffc*/
1413 LOG_ERROR("address + size wrapped(0x%08" PRIx32 ", 0x%08" PRIx32 ")",
1419 if (((address % 2) == 0) && (size == 2))
1421 return target_read_memory(target, address, 2, 1, buffer);
1424 /* handle unaligned head bytes */
1427 uint32_t unaligned = 4 - (address % 4);
1429 if (unaligned > size)
1432 if ((retval = target_read_memory(target, address, 1, unaligned, buffer)) != ERROR_OK)
1435 buffer += unaligned;
1436 address += unaligned;
1440 /* handle aligned words */
1443 int aligned = size - (size % 4);
1445 if ((retval = target_read_memory(target, address, 4, aligned / 4, buffer)) != ERROR_OK)
1453 /*prevent byte access when possible (avoid AHB access limitations in some cases)*/
1456 int aligned = size - (size%2);
1457 retval = target_read_memory(target, address, 2, aligned / 2, buffer);
1458 if (retval != ERROR_OK)
1465 /* handle tail writes of less than 4 bytes */
1468 if ((retval = target_read_memory(target, address, 1, size, buffer)) != ERROR_OK)
1475 int target_checksum_memory(struct target *target, uint32_t address, uint32_t size, uint32_t* crc)
1480 uint32_t checksum = 0;
1481 if (!target_was_examined(target))
1483 LOG_ERROR("Target not examined yet");
1487 if ((retval = target->type->checksum_memory(target, address,
1488 size, &checksum)) != ERROR_OK)
1490 buffer = malloc(size);
1493 LOG_ERROR("error allocating buffer for section (%d bytes)", (int)size);
1494 return ERROR_INVALID_ARGUMENTS;
1496 retval = target_read_buffer(target, address, size, buffer);
1497 if (retval != ERROR_OK)
1503 /* convert to target endianess */
1504 for (i = 0; i < (size/sizeof(uint32_t)); i++)
1506 uint32_t target_data;
1507 target_data = target_buffer_get_u32(target, &buffer[i*sizeof(uint32_t)]);
1508 target_buffer_set_u32(target, &buffer[i*sizeof(uint32_t)], target_data);
1511 retval = image_calculate_checksum(buffer, size, &checksum);
1520 int target_blank_check_memory(struct target *target, uint32_t address, uint32_t size, uint32_t* blank)
1523 if (!target_was_examined(target))
1525 LOG_ERROR("Target not examined yet");
1529 if (target->type->blank_check_memory == 0)
1530 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
1532 retval = target->type->blank_check_memory(target, address, size, blank);
1537 int target_read_u32(struct target *target, uint32_t address, uint32_t *value)
1539 uint8_t value_buf[4];
1540 if (!target_was_examined(target))
1542 LOG_ERROR("Target not examined yet");
1546 int retval = target_read_memory(target, address, 4, 1, value_buf);
1548 if (retval == ERROR_OK)
1550 *value = target_buffer_get_u32(target, value_buf);
1551 LOG_DEBUG("address: 0x%8.8" PRIx32 ", value: 0x%8.8" PRIx32 "",
1558 LOG_DEBUG("address: 0x%8.8" PRIx32 " failed",
1565 int target_read_u16(struct target *target, uint32_t address, uint16_t *value)
1567 uint8_t value_buf[2];
1568 if (!target_was_examined(target))
1570 LOG_ERROR("Target not examined yet");
1574 int retval = target_read_memory(target, address, 2, 1, value_buf);
1576 if (retval == ERROR_OK)
1578 *value = target_buffer_get_u16(target, value_buf);
1579 LOG_DEBUG("address: 0x%8.8" PRIx32 ", value: 0x%4.4x",
1586 LOG_DEBUG("address: 0x%8.8" PRIx32 " failed",
1593 int target_read_u8(struct target *target, uint32_t address, uint8_t *value)
1595 int retval = target_read_memory(target, address, 1, 1, value);
1596 if (!target_was_examined(target))
1598 LOG_ERROR("Target not examined yet");
1602 if (retval == ERROR_OK)
1604 LOG_DEBUG("address: 0x%8.8" PRIx32 ", value: 0x%2.2x",
1611 LOG_DEBUG("address: 0x%8.8" PRIx32 " failed",
1618 int target_write_u32(struct target *target, uint32_t address, uint32_t value)
1621 uint8_t value_buf[4];
1622 if (!target_was_examined(target))
1624 LOG_ERROR("Target not examined yet");
1628 LOG_DEBUG("address: 0x%8.8" PRIx32 ", value: 0x%8.8" PRIx32 "",
1632 target_buffer_set_u32(target, value_buf, value);
1633 if ((retval = target_write_memory(target, address, 4, 1, value_buf)) != ERROR_OK)
1635 LOG_DEBUG("failed: %i", retval);
1641 int target_write_u16(struct target *target, uint32_t address, uint16_t value)
1644 uint8_t value_buf[2];
1645 if (!target_was_examined(target))
1647 LOG_ERROR("Target not examined yet");
1651 LOG_DEBUG("address: 0x%8.8" PRIx32 ", value: 0x%8.8x",
1655 target_buffer_set_u16(target, value_buf, value);
1656 if ((retval = target_write_memory(target, address, 2, 1, value_buf)) != ERROR_OK)
1658 LOG_DEBUG("failed: %i", retval);
1664 int target_write_u8(struct target *target, uint32_t address, uint8_t value)
1667 if (!target_was_examined(target))
1669 LOG_ERROR("Target not examined yet");
1673 LOG_DEBUG("address: 0x%8.8" PRIx32 ", value: 0x%2.2x",
1676 if ((retval = target_write_memory(target, address, 1, 1, &value)) != ERROR_OK)
1678 LOG_DEBUG("failed: %i", retval);
1684 COMMAND_HANDLER(handle_targets_command)
1686 struct target *target = all_targets;
1690 target = get_target(CMD_ARGV[0]);
1691 if (target == NULL) {
1692 command_print(CMD_CTX,"Target: %s is unknown, try one of:\n", CMD_ARGV[0]);
1695 if (!target->tap->enabled) {
1696 command_print(CMD_CTX,"Target: TAP %s is disabled, "
1697 "can't be the current target\n",
1698 target->tap->dotted_name);
1702 CMD_CTX->current_target = target->target_number;
1707 target = all_targets;
1708 command_print(CMD_CTX, " TargetName Type Endian TapName State ");
1709 command_print(CMD_CTX, "-- ------------------ ---------- ------ ------------------ ------------");
1715 if (target->tap->enabled)
1716 state = target_state_name( target );
1718 state = "tap-disabled";
1720 if (CMD_CTX->current_target == target->target_number)
1723 /* keep columns lined up to match the headers above */
1724 command_print(CMD_CTX, "%2d%c %-18s %-10s %-6s %-18s %s",
1725 target->target_number,
1727 target_name(target),
1728 target_type_name(target),
1729 Jim_Nvp_value2name_simple(nvp_target_endian,
1730 target->endianness)->name,
1731 target->tap->dotted_name,
1733 target = target->next;
1739 /* every 300ms we check for reset & powerdropout and issue a "reset halt" if so. */
1741 static int powerDropout;
1742 static int srstAsserted;
1744 static int runPowerRestore;
1745 static int runPowerDropout;
1746 static int runSrstAsserted;
1747 static int runSrstDeasserted;
1749 static int sense_handler(void)
1751 static int prevSrstAsserted = 0;
1752 static int prevPowerdropout = 0;
1755 if ((retval = jtag_power_dropout(&powerDropout)) != ERROR_OK)
1759 powerRestored = prevPowerdropout && !powerDropout;
1762 runPowerRestore = 1;
1765 long long current = timeval_ms();
1766 static long long lastPower = 0;
1767 int waitMore = lastPower + 2000 > current;
1768 if (powerDropout && !waitMore)
1770 runPowerDropout = 1;
1771 lastPower = current;
1774 if ((retval = jtag_srst_asserted(&srstAsserted)) != ERROR_OK)
1778 srstDeasserted = prevSrstAsserted && !srstAsserted;
1780 static long long lastSrst = 0;
1781 waitMore = lastSrst + 2000 > current;
1782 if (srstDeasserted && !waitMore)
1784 runSrstDeasserted = 1;
1788 if (!prevSrstAsserted && srstAsserted)
1790 runSrstAsserted = 1;
1793 prevSrstAsserted = srstAsserted;
1794 prevPowerdropout = powerDropout;
1796 if (srstDeasserted || powerRestored)
1798 /* Other than logging the event we can't do anything here.
1799 * Issuing a reset is a particularly bad idea as we might
1800 * be inside a reset already.
1807 static int backoff_times = 0;
1808 static int backoff_count = 0;
1810 /* process target state changes */
1811 static int handle_target(void *priv)
1813 Jim_Interp *interp = (Jim_Interp *)priv;
1814 int retval = ERROR_OK;
1816 if (!is_jtag_poll_safe())
1818 /* polling is disabled currently */
1822 /* we do not want to recurse here... */
1823 static int recursive = 0;
1828 /* danger! running these procedures can trigger srst assertions and power dropouts.
1829 * We need to avoid an infinite loop/recursion here and we do that by
1830 * clearing the flags after running these events.
1832 int did_something = 0;
1833 if (runSrstAsserted)
1835 LOG_INFO("srst asserted detected, running srst_asserted proc.");
1836 Jim_Eval(interp, "srst_asserted");
1839 if (runSrstDeasserted)
1841 Jim_Eval(interp, "srst_deasserted");
1844 if (runPowerDropout)
1846 LOG_INFO("Power dropout detected, running power_dropout proc.");
1847 Jim_Eval(interp, "power_dropout");
1850 if (runPowerRestore)
1852 Jim_Eval(interp, "power_restore");
1858 /* clear detect flags */
1862 /* clear action flags */
1864 runSrstAsserted = 0;
1865 runSrstDeasserted = 0;
1866 runPowerRestore = 0;
1867 runPowerDropout = 0;
1872 if (backoff_times > backoff_count)
1874 /* do not poll this time as we failed previously */
1880 /* Poll targets for state changes unless that's globally disabled.
1881 * Skip targets that are currently disabled.
1883 for (struct target *target = all_targets;
1884 is_jtag_poll_safe() && target;
1885 target = target->next)
1887 if (!target->tap->enabled)
1890 /* only poll target if we've got power and srst isn't asserted */
1891 if (!powerDropout && !srstAsserted)
1893 /* polling may fail silently until the target has been examined */
1894 if ((retval = target_poll(target)) != ERROR_OK)
1896 /* 100ms polling interval. Increase interval between polling up to 5000ms */
1897 if (backoff_times * polling_interval < 5000)
1902 LOG_USER("Polling target failed, GDB will be halted. Polling again in %dms", backoff_times * polling_interval);
1904 /* Tell GDB to halt the debugger. This allows the user to
1905 * run monitor commands to handle the situation.
1907 target_call_event_callbacks(target, TARGET_EVENT_GDB_HALT);
1910 /* Since we succeeded, we reset backoff count */
1911 if (backoff_times > 0)
1913 LOG_USER("Polling succeeded again");
1922 COMMAND_HANDLER(handle_reg_command)
1924 struct target *target;
1925 struct reg *reg = NULL;
1931 target = get_current_target(CMD_CTX);
1933 /* list all available registers for the current target */
1936 struct reg_cache *cache = target->reg_cache;
1943 command_print(CMD_CTX, "===== %s", cache->name);
1945 for (i = 0, reg = cache->reg_list;
1946 i < cache->num_regs;
1947 i++, reg++, count++)
1949 /* only print cached values if they are valid */
1951 value = buf_to_str(reg->value,
1953 command_print(CMD_CTX,
1954 "(%i) %s (/%" PRIu32 "): 0x%s%s",
1962 command_print(CMD_CTX, "(%i) %s (/%" PRIu32 ")",
1967 cache = cache->next;
1973 /* access a single register by its ordinal number */
1974 if ((CMD_ARGV[0][0] >= '0') && (CMD_ARGV[0][0] <= '9'))
1977 COMMAND_PARSE_NUMBER(uint, CMD_ARGV[0], num);
1979 struct reg_cache *cache = target->reg_cache;
1984 for (i = 0; i < cache->num_regs; i++)
1988 reg = &cache->reg_list[i];
1994 cache = cache->next;
1999 command_print(CMD_CTX, "%i is out of bounds, the current target has only %i registers (0 - %i)", num, count, count - 1);
2002 } else /* access a single register by its name */
2004 reg = register_get_by_name(target->reg_cache, CMD_ARGV[0], 1);
2008 command_print(CMD_CTX, "register %s not found in current target", CMD_ARGV[0]);
2013 /* display a register */
2014 if ((CMD_ARGC == 1) || ((CMD_ARGC == 2) && !((CMD_ARGV[1][0] >= '0') && (CMD_ARGV[1][0] <= '9'))))
2016 if ((CMD_ARGC == 2) && (strcmp(CMD_ARGV[1], "force") == 0))
2019 if (reg->valid == 0)
2021 reg->type->get(reg);
2023 value = buf_to_str(reg->value, reg->size, 16);
2024 command_print(CMD_CTX, "%s (/%i): 0x%s", reg->name, (int)(reg->size), value);
2029 /* set register value */
2032 uint8_t *buf = malloc(DIV_ROUND_UP(reg->size, 8));
2033 str_to_buf(CMD_ARGV[1], strlen(CMD_ARGV[1]), buf, reg->size, 0);
2035 reg->type->set(reg, buf);
2037 value = buf_to_str(reg->value, reg->size, 16);
2038 command_print(CMD_CTX, "%s (/%i): 0x%s", reg->name, (int)(reg->size), value);
2046 command_print(CMD_CTX, "usage: reg <#|name> [value]");
2051 COMMAND_HANDLER(handle_poll_command)
2053 int retval = ERROR_OK;
2054 struct target *target = get_current_target(CMD_CTX);
2058 command_print(CMD_CTX, "background polling: %s",
2059 jtag_poll_get_enabled() ? "on" : "off");
2060 command_print(CMD_CTX, "TAP: %s (%s)",
2061 target->tap->dotted_name,
2062 target->tap->enabled ? "enabled" : "disabled");
2063 if (!target->tap->enabled)
2065 if ((retval = target_poll(target)) != ERROR_OK)
2067 if ((retval = target_arch_state(target)) != ERROR_OK)
2070 else if (CMD_ARGC == 1)
2073 COMMAND_PARSE_ON_OFF(CMD_ARGV[0], enable);
2074 jtag_poll_set_enabled(enable);
2078 return ERROR_COMMAND_SYNTAX_ERROR;
2084 COMMAND_HANDLER(handle_wait_halt_command)
2087 return ERROR_COMMAND_SYNTAX_ERROR;
2092 int retval = parse_uint(CMD_ARGV[0], &ms);
2093 if (ERROR_OK != retval)
2095 command_print(CMD_CTX, "usage: %s [seconds]", CMD_NAME);
2096 return ERROR_COMMAND_SYNTAX_ERROR;
2098 // convert seconds (given) to milliseconds (needed)
2102 struct target *target = get_current_target(CMD_CTX);
2103 return target_wait_state(target, TARGET_HALTED, ms);
2106 /* wait for target state to change. The trick here is to have a low
2107 * latency for short waits and not to suck up all the CPU time
2110 * After 500ms, keep_alive() is invoked
2112 int target_wait_state(struct target *target, enum target_state state, int ms)
2115 long long then = 0, cur;
2120 if ((retval = target_poll(target)) != ERROR_OK)
2122 if (target->state == state)
2130 then = timeval_ms();
2131 LOG_DEBUG("waiting for target %s...",
2132 Jim_Nvp_value2name_simple(nvp_target_state,state)->name);
2140 if ((cur-then) > ms)
2142 LOG_ERROR("timed out while waiting for target %s",
2143 Jim_Nvp_value2name_simple(nvp_target_state,state)->name);
2151 COMMAND_HANDLER(handle_halt_command)
2155 struct target *target = get_current_target(CMD_CTX);
2156 int retval = target_halt(target);
2157 if (ERROR_OK != retval)
2162 unsigned wait_local;
2163 retval = parse_uint(CMD_ARGV[0], &wait_local);
2164 if (ERROR_OK != retval)
2165 return ERROR_COMMAND_SYNTAX_ERROR;
2170 return CALL_COMMAND_HANDLER(handle_wait_halt_command);
2173 COMMAND_HANDLER(handle_soft_reset_halt_command)
2175 struct target *target = get_current_target(CMD_CTX);
2177 LOG_USER("requesting target halt and executing a soft reset");
2179 target->type->soft_reset_halt(target);
2184 COMMAND_HANDLER(handle_reset_command)
2187 return ERROR_COMMAND_SYNTAX_ERROR;
2189 enum target_reset_mode reset_mode = RESET_RUN;
2193 n = Jim_Nvp_name2value_simple(nvp_reset_modes, CMD_ARGV[0]);
2194 if ((n->name == NULL) || (n->value == RESET_UNKNOWN)) {
2195 return ERROR_COMMAND_SYNTAX_ERROR;
2197 reset_mode = n->value;
2200 /* reset *all* targets */
2201 return target_process_reset(CMD_CTX, reset_mode);
2205 COMMAND_HANDLER(handle_resume_command)
2209 return ERROR_COMMAND_SYNTAX_ERROR;
2211 struct target *target = get_current_target(CMD_CTX);
2212 target_handle_event(target, TARGET_EVENT_OLD_pre_resume);
2214 /* with no CMD_ARGV, resume from current pc, addr = 0,
2215 * with one arguments, addr = CMD_ARGV[0],
2216 * handle breakpoints, not debugging */
2220 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], addr);
2224 return target_resume(target, current, addr, 1, 0);
2227 COMMAND_HANDLER(handle_step_command)
2230 return ERROR_COMMAND_SYNTAX_ERROR;
2234 /* with no CMD_ARGV, step from current pc, addr = 0,
2235 * with one argument addr = CMD_ARGV[0],
2236 * handle breakpoints, debugging */
2241 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], addr);
2245 struct target *target = get_current_target(CMD_CTX);
2247 return target->type->step(target, current_pc, addr, 1);
2250 static void handle_md_output(struct command_context *cmd_ctx,
2251 struct target *target, uint32_t address, unsigned size,
2252 unsigned count, const uint8_t *buffer)
2254 const unsigned line_bytecnt = 32;
2255 unsigned line_modulo = line_bytecnt / size;
2257 char output[line_bytecnt * 4 + 1];
2258 unsigned output_len = 0;
2260 const char *value_fmt;
2262 case 4: value_fmt = "%8.8x "; break;
2263 case 2: value_fmt = "%4.4x "; break;
2264 case 1: value_fmt = "%2.2x "; break;
2266 /* "can't happen", caller checked */
2267 LOG_ERROR("invalid memory read size: %u", size);
2271 for (unsigned i = 0; i < count; i++)
2273 if (i % line_modulo == 0)
2275 output_len += snprintf(output + output_len,
2276 sizeof(output) - output_len,
2278 (unsigned)(address + (i*size)));
2282 const uint8_t *value_ptr = buffer + i * size;
2284 case 4: value = target_buffer_get_u32(target, value_ptr); break;
2285 case 2: value = target_buffer_get_u16(target, value_ptr); break;
2286 case 1: value = *value_ptr;
2288 output_len += snprintf(output + output_len,
2289 sizeof(output) - output_len,
2292 if ((i % line_modulo == line_modulo - 1) || (i == count - 1))
2294 command_print(cmd_ctx, "%s", output);
2300 COMMAND_HANDLER(handle_md_command)
2303 return ERROR_COMMAND_SYNTAX_ERROR;
2306 switch (CMD_NAME[2]) {
2307 case 'w': size = 4; break;
2308 case 'h': size = 2; break;
2309 case 'b': size = 1; break;
2310 default: return ERROR_COMMAND_SYNTAX_ERROR;
2313 bool physical=strcmp(CMD_ARGV[0], "phys")==0;
2314 int (*fn)(struct target *target,
2315 uint32_t address, uint32_t size, uint32_t count, uint8_t *buffer);
2320 fn=target_read_phys_memory;
2323 fn=target_read_memory;
2325 if ((CMD_ARGC < 1) || (CMD_ARGC > 2))
2327 return ERROR_COMMAND_SYNTAX_ERROR;
2331 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], address);
2335 COMMAND_PARSE_NUMBER(uint, CMD_ARGV[1], count);
2337 uint8_t *buffer = calloc(count, size);
2339 struct target *target = get_current_target(CMD_CTX);
2340 int retval = fn(target, address, size, count, buffer);
2341 if (ERROR_OK == retval)
2342 handle_md_output(CMD_CTX, target, address, size, count, buffer);
2349 typedef int (*target_write_fn)(struct target *target,
2350 uint32_t address, uint32_t size, uint32_t count, uint8_t *buffer);
2352 static int target_write_memory_fast(struct target *target,
2353 uint32_t address, uint32_t size, uint32_t count, uint8_t *buffer)
2355 return target_write_buffer(target, address, size * count, buffer);
2358 static int target_fill_mem(struct target *target,
2367 /* We have to write in reasonably large chunks to be able
2368 * to fill large memory areas with any sane speed */
2369 const unsigned chunk_size = 16384;
2370 uint8_t *target_buf = malloc(chunk_size * data_size);
2371 if (target_buf == NULL)
2373 LOG_ERROR("Out of memory");
2377 for (unsigned i = 0; i < chunk_size; i ++)
2382 target_buffer_set_u32(target, target_buf + i*data_size, b);
2385 target_buffer_set_u16(target, target_buf + i*data_size, b);
2388 target_buffer_set_u8(target, target_buf + i*data_size, b);
2395 int retval = ERROR_OK;
2397 for (unsigned x = 0; x < c; x += chunk_size)
2401 if (current > chunk_size)
2403 current = chunk_size;
2405 retval = fn(target, address + x * data_size, data_size, current, target_buf);
2406 if (retval != ERROR_OK)
2410 /* avoid GDB timeouts */
2419 COMMAND_HANDLER(handle_mw_command)
2423 return ERROR_COMMAND_SYNTAX_ERROR;
2425 bool physical=strcmp(CMD_ARGV[0], "phys")==0;
2431 fn=target_write_phys_memory;
2434 fn = target_write_memory_fast;
2436 if ((CMD_ARGC < 2) || (CMD_ARGC > 3))
2437 return ERROR_COMMAND_SYNTAX_ERROR;
2440 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], address);
2443 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], value);
2447 COMMAND_PARSE_NUMBER(uint, CMD_ARGV[2], count);
2449 struct target *target = get_current_target(CMD_CTX);
2451 switch (CMD_NAME[2])
2463 return ERROR_COMMAND_SYNTAX_ERROR;
2466 return target_fill_mem(target, address, fn, wordsize, value, count);
2469 static COMMAND_HELPER(parse_load_image_command_CMD_ARGV, struct image *image,
2470 uint32_t *min_address, uint32_t *max_address)
2472 if (CMD_ARGC < 1 || CMD_ARGC > 5)
2473 return ERROR_COMMAND_SYNTAX_ERROR;
2475 /* a base address isn't always necessary,
2476 * default to 0x0 (i.e. don't relocate) */
2480 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], addr);
2481 image->base_address = addr;
2482 image->base_address_set = 1;
2485 image->base_address_set = 0;
2487 image->start_address_set = 0;
2491 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[3], *min_address);
2495 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[4], *max_address);
2496 // use size (given) to find max (required)
2497 *max_address += *min_address;
2500 if (*min_address > *max_address)
2501 return ERROR_COMMAND_SYNTAX_ERROR;
2506 COMMAND_HANDLER(handle_load_image_command)
2510 uint32_t image_size;
2511 uint32_t min_address = 0;
2512 uint32_t max_address = 0xffffffff;
2516 int retval = CALL_COMMAND_HANDLER(parse_load_image_command_CMD_ARGV,
2517 &image, &min_address, &max_address);
2518 if (ERROR_OK != retval)
2521 struct target *target = get_current_target(CMD_CTX);
2523 struct duration bench;
2524 duration_start(&bench);
2526 if (image_open(&image, CMD_ARGV[0], (CMD_ARGC >= 3) ? CMD_ARGV[2] : NULL) != ERROR_OK)
2533 for (i = 0; i < image.num_sections; i++)
2535 buffer = malloc(image.sections[i].size);
2538 command_print(CMD_CTX,
2539 "error allocating buffer for section (%d bytes)",
2540 (int)(image.sections[i].size));
2544 if ((retval = image_read_section(&image, i, 0x0, image.sections[i].size, buffer, &buf_cnt)) != ERROR_OK)
2550 uint32_t offset = 0;
2551 uint32_t length = buf_cnt;
2553 /* DANGER!!! beware of unsigned comparision here!!! */
2555 if ((image.sections[i].base_address + buf_cnt >= min_address)&&
2556 (image.sections[i].base_address < max_address))
2558 if (image.sections[i].base_address < min_address)
2560 /* clip addresses below */
2561 offset += min_address-image.sections[i].base_address;
2565 if (image.sections[i].base_address + buf_cnt > max_address)
2567 length -= (image.sections[i].base_address + buf_cnt)-max_address;
2570 if ((retval = target_write_buffer(target, image.sections[i].base_address + offset, length, buffer + offset)) != ERROR_OK)
2575 image_size += length;
2576 command_print(CMD_CTX, "%u bytes written at address 0x%8.8" PRIx32 "",
2577 (unsigned int)length,
2578 image.sections[i].base_address + offset);
2584 if ((ERROR_OK == retval) && (duration_measure(&bench) == ERROR_OK))
2586 command_print(CMD_CTX, "downloaded %" PRIu32 " bytes "
2587 "in %fs (%0.3f KiB/s)", image_size,
2588 duration_elapsed(&bench), duration_kbps(&bench, image_size));
2591 image_close(&image);
2597 COMMAND_HANDLER(handle_dump_image_command)
2599 struct fileio fileio;
2600 uint8_t buffer[560];
2601 int retval, retvaltemp;
2602 uint32_t address, size;
2603 struct duration bench;
2604 struct target *target = get_current_target(CMD_CTX);
2607 return ERROR_COMMAND_SYNTAX_ERROR;
2609 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], address);
2610 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[2], size);
2612 retval = fileio_open(&fileio, CMD_ARGV[0], FILEIO_WRITE, FILEIO_BINARY);
2613 if (retval != ERROR_OK)
2616 duration_start(&bench);
2621 size_t size_written;
2622 uint32_t this_run_size = (size > 560) ? 560 : size;
2623 retval = target_read_buffer(target, address, this_run_size, buffer);
2624 if (retval != ERROR_OK)
2629 retval = fileio_write(&fileio, this_run_size, buffer, &size_written);
2630 if (retval != ERROR_OK)
2635 size -= this_run_size;
2636 address += this_run_size;
2639 if ((ERROR_OK == retval) && (duration_measure(&bench) == ERROR_OK))
2642 retval = fileio_size(&fileio, &filesize);
2643 if (retval != ERROR_OK)
2645 command_print(CMD_CTX,
2646 "dumped %ld bytes in %fs (%0.3f KiB/s)", (long)filesize,
2647 duration_elapsed(&bench), duration_kbps(&bench, filesize));
2650 if ((retvaltemp = fileio_close(&fileio)) != ERROR_OK)
2656 static COMMAND_HELPER(handle_verify_image_command_internal, int verify)
2660 uint32_t image_size;
2663 uint32_t checksum = 0;
2664 uint32_t mem_checksum = 0;
2668 struct target *target = get_current_target(CMD_CTX);
2672 return ERROR_COMMAND_SYNTAX_ERROR;
2677 LOG_ERROR("no target selected");
2681 struct duration bench;
2682 duration_start(&bench);
2687 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], addr);
2688 image.base_address = addr;
2689 image.base_address_set = 1;
2693 image.base_address_set = 0;
2694 image.base_address = 0x0;
2697 image.start_address_set = 0;
2699 if ((retval = image_open(&image, CMD_ARGV[0], (CMD_ARGC == 3) ? CMD_ARGV[2] : NULL)) != ERROR_OK)
2707 for (i = 0; i < image.num_sections; i++)
2709 buffer = malloc(image.sections[i].size);
2712 command_print(CMD_CTX,
2713 "error allocating buffer for section (%d bytes)",
2714 (int)(image.sections[i].size));
2717 if ((retval = image_read_section(&image, i, 0x0, image.sections[i].size, buffer, &buf_cnt)) != ERROR_OK)
2725 /* calculate checksum of image */
2726 retval = image_calculate_checksum(buffer, buf_cnt, &checksum);
2727 if (retval != ERROR_OK)
2733 retval = target_checksum_memory(target, image.sections[i].base_address, buf_cnt, &mem_checksum);
2734 if (retval != ERROR_OK)
2740 if (checksum != mem_checksum)
2742 /* failed crc checksum, fall back to a binary compare */
2747 LOG_ERROR("checksum mismatch - attempting binary compare");
2750 data = (uint8_t*)malloc(buf_cnt);
2752 /* Can we use 32bit word accesses? */
2754 int count = buf_cnt;
2755 if ((count % 4) == 0)
2760 retval = target_read_memory(target, image.sections[i].base_address, size, count, data);
2761 if (retval == ERROR_OK)
2764 for (t = 0; t < buf_cnt; t++)
2766 if (data[t] != buffer[t])
2768 command_print(CMD_CTX,
2769 "diff %d address 0x%08x. Was 0x%02x instead of 0x%02x",
2771 (unsigned)(t + image.sections[i].base_address),
2776 command_print(CMD_CTX, "More than 128 errors, the rest are not printed.");
2789 command_print(CMD_CTX, "address 0x%08" PRIx32 " length 0x%08zx",
2790 image.sections[i].base_address,
2795 image_size += buf_cnt;
2799 command_print(CMD_CTX, "No more differences found.");
2804 retval = ERROR_FAIL;
2806 if ((ERROR_OK == retval) && (duration_measure(&bench) == ERROR_OK))
2808 command_print(CMD_CTX, "verified %" PRIu32 " bytes "
2809 "in %fs (%0.3f KiB/s)", image_size,
2810 duration_elapsed(&bench), duration_kbps(&bench, image_size));
2813 image_close(&image);
2818 COMMAND_HANDLER(handle_verify_image_command)
2820 return CALL_COMMAND_HANDLER(handle_verify_image_command_internal, 1);
2823 COMMAND_HANDLER(handle_test_image_command)
2825 return CALL_COMMAND_HANDLER(handle_verify_image_command_internal, 0);
2828 static int handle_bp_command_list(struct command_context *cmd_ctx)
2830 struct target *target = get_current_target(cmd_ctx);
2831 struct breakpoint *breakpoint = target->breakpoints;
2834 if (breakpoint->type == BKPT_SOFT)
2836 char* buf = buf_to_str(breakpoint->orig_instr,
2837 breakpoint->length, 16);
2838 command_print(cmd_ctx, "0x%8.8" PRIx32 ", 0x%x, %i, 0x%s",
2839 breakpoint->address,
2841 breakpoint->set, buf);
2846 command_print(cmd_ctx, "0x%8.8" PRIx32 ", 0x%x, %i",
2847 breakpoint->address,
2848 breakpoint->length, breakpoint->set);
2851 breakpoint = breakpoint->next;
2856 static int handle_bp_command_set(struct command_context *cmd_ctx,
2857 uint32_t addr, uint32_t length, int hw)
2859 struct target *target = get_current_target(cmd_ctx);
2860 int retval = breakpoint_add(target, addr, length, hw);
2861 if (ERROR_OK == retval)
2862 command_print(cmd_ctx, "breakpoint set at 0x%8.8" PRIx32 "", addr);
2864 LOG_ERROR("Failure setting breakpoint");
2868 COMMAND_HANDLER(handle_bp_command)
2871 return handle_bp_command_list(CMD_CTX);
2873 if (CMD_ARGC < 2 || CMD_ARGC > 3)
2875 command_print(CMD_CTX, "usage: bp <address> <length> ['hw']");
2876 return ERROR_COMMAND_SYNTAX_ERROR;
2880 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], addr);
2882 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], length);
2887 if (strcmp(CMD_ARGV[2], "hw") == 0)
2890 return ERROR_COMMAND_SYNTAX_ERROR;
2893 return handle_bp_command_set(CMD_CTX, addr, length, hw);
2896 COMMAND_HANDLER(handle_rbp_command)
2899 return ERROR_COMMAND_SYNTAX_ERROR;
2902 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], addr);
2904 struct target *target = get_current_target(CMD_CTX);
2905 breakpoint_remove(target, addr);
2910 COMMAND_HANDLER(handle_wp_command)
2912 struct target *target = get_current_target(CMD_CTX);
2916 struct watchpoint *watchpoint = target->watchpoints;
2920 command_print(CMD_CTX, "address: 0x%8.8" PRIx32
2921 ", len: 0x%8.8" PRIx32
2922 ", r/w/a: %i, value: 0x%8.8" PRIx32
2923 ", mask: 0x%8.8" PRIx32,
2924 watchpoint->address,
2926 (int)watchpoint->rw,
2929 watchpoint = watchpoint->next;
2934 enum watchpoint_rw type = WPT_ACCESS;
2936 uint32_t length = 0;
2937 uint32_t data_value = 0x0;
2938 uint32_t data_mask = 0xffffffff;
2943 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[4], data_mask);
2946 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[3], data_value);
2949 switch (CMD_ARGV[2][0])
2961 LOG_ERROR("invalid watchpoint mode ('%c')", CMD_ARGV[2][0]);
2962 return ERROR_COMMAND_SYNTAX_ERROR;
2966 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], length);
2967 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], addr);
2971 command_print(CMD_CTX, "usage: wp [address length "
2972 "[(r|w|a) [value [mask]]]]");
2973 return ERROR_COMMAND_SYNTAX_ERROR;
2976 int retval = watchpoint_add(target, addr, length, type,
2977 data_value, data_mask);
2978 if (ERROR_OK != retval)
2979 LOG_ERROR("Failure setting watchpoints");
2984 COMMAND_HANDLER(handle_rwp_command)
2987 return ERROR_COMMAND_SYNTAX_ERROR;
2990 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], addr);
2992 struct target *target = get_current_target(CMD_CTX);
2993 watchpoint_remove(target, addr);
3000 * Translate a virtual address to a physical address.
3002 * The low-level target implementation must have logged a detailed error
3003 * which is forwarded to telnet/GDB session.
3005 COMMAND_HANDLER(handle_virt2phys_command)
3008 return ERROR_COMMAND_SYNTAX_ERROR;
3011 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], va);
3014 struct target *target = get_current_target(CMD_CTX);
3015 int retval = target->type->virt2phys(target, va, &pa);
3016 if (retval == ERROR_OK)
3017 command_print(CMD_CTX, "Physical address 0x%08" PRIx32 "", pa);
3022 static void writeData(FILE *f, const void *data, size_t len)
3024 size_t written = fwrite(data, 1, len, f);
3026 LOG_ERROR("failed to write %zu bytes: %s", len, strerror(errno));
3029 static void writeLong(FILE *f, int l)
3032 for (i = 0; i < 4; i++)
3034 char c = (l >> (i*8))&0xff;
3035 writeData(f, &c, 1);
3040 static void writeString(FILE *f, char *s)
3042 writeData(f, s, strlen(s));
3045 /* Dump a gmon.out histogram file. */
3046 static void writeGmon(uint32_t *samples, uint32_t sampleNum, const char *filename)
3049 FILE *f = fopen(filename, "w");
3052 writeString(f, "gmon");
3053 writeLong(f, 0x00000001); /* Version */
3054 writeLong(f, 0); /* padding */
3055 writeLong(f, 0); /* padding */
3056 writeLong(f, 0); /* padding */
3058 uint8_t zero = 0; /* GMON_TAG_TIME_HIST */
3059 writeData(f, &zero, 1);
3061 /* figure out bucket size */
3062 uint32_t min = samples[0];
3063 uint32_t max = samples[0];
3064 for (i = 0; i < sampleNum; i++)
3066 if (min > samples[i])
3070 if (max < samples[i])
3076 int addressSpace = (max-min + 1);
3078 static const uint32_t maxBuckets = 256 * 1024; /* maximum buckets. */
3079 uint32_t length = addressSpace;
3080 if (length > maxBuckets)
3082 length = maxBuckets;
3084 int *buckets = malloc(sizeof(int)*length);
3085 if (buckets == NULL)
3090 memset(buckets, 0, sizeof(int)*length);
3091 for (i = 0; i < sampleNum;i++)
3093 uint32_t address = samples[i];
3094 long long a = address-min;
3095 long long b = length-1;
3096 long long c = addressSpace-1;
3097 int index_t = (a*b)/c; /* danger!!!! int32 overflows */
3101 /* append binary memory gmon.out &profile_hist_hdr ((char*)&profile_hist_hdr + sizeof(struct gmon_hist_hdr)) */
3102 writeLong(f, min); /* low_pc */
3103 writeLong(f, max); /* high_pc */
3104 writeLong(f, length); /* # of samples */
3105 writeLong(f, 64000000); /* 64MHz */
3106 writeString(f, "seconds");
3107 for (i = 0; i < (15-strlen("seconds")); i++)
3108 writeData(f, &zero, 1);
3109 writeString(f, "s");
3111 /*append binary memory gmon.out profile_hist_data (profile_hist_data + profile_hist_hdr.hist_size) */
3113 char *data = malloc(2*length);
3116 for (i = 0; i < length;i++)
3125 data[i*2 + 1]=(val >> 8)&0xff;
3128 writeData(f, data, length * 2);
3138 /* profiling samples the CPU PC as quickly as OpenOCD is able,
3139 * which will be used as a random sampling of PC */
3140 COMMAND_HANDLER(handle_profile_command)
3142 struct target *target = get_current_target(CMD_CTX);
3143 struct timeval timeout, now;
3145 gettimeofday(&timeout, NULL);
3148 return ERROR_COMMAND_SYNTAX_ERROR;
3151 COMMAND_PARSE_NUMBER(uint, CMD_ARGV[0], offset);
3153 timeval_add_time(&timeout, offset, 0);
3156 * @todo: Some cores let us sample the PC without the
3157 * annoying halt/resume step; for example, ARMv7 PCSR.
3158 * Provide a way to use that more efficient mechanism.
3161 command_print(CMD_CTX, "Starting profiling. Halting and resuming the target as often as we can...");
3163 static const int maxSample = 10000;
3164 uint32_t *samples = malloc(sizeof(uint32_t)*maxSample);
3165 if (samples == NULL)
3169 /* hopefully it is safe to cache! We want to stop/restart as quickly as possible. */
3170 struct reg *reg = register_get_by_name(target->reg_cache, "pc", 1);
3175 target_poll(target);
3176 if (target->state == TARGET_HALTED)
3178 uint32_t t=*((uint32_t *)reg->value);
3179 samples[numSamples++]=t;
3180 retval = target_resume(target, 1, 0, 0, 0); /* current pc, addr = 0, do not handle breakpoints, not debugging */
3181 target_poll(target);
3182 alive_sleep(10); /* sleep 10ms, i.e. <100 samples/second. */
3183 } else if (target->state == TARGET_RUNNING)
3185 /* We want to quickly sample the PC. */
3186 if ((retval = target_halt(target)) != ERROR_OK)
3193 command_print(CMD_CTX, "Target not halted or running");
3197 if (retval != ERROR_OK)
3202 gettimeofday(&now, NULL);
3203 if ((numSamples >= maxSample) || ((now.tv_sec >= timeout.tv_sec) && (now.tv_usec >= timeout.tv_usec)))
3205 command_print(CMD_CTX, "Profiling completed. %d samples.", numSamples);
3206 if ((retval = target_poll(target)) != ERROR_OK)
3211 if (target->state == TARGET_HALTED)
3213 target_resume(target, 1, 0, 0, 0); /* current pc, addr = 0, do not handle breakpoints, not debugging */
3215 if ((retval = target_poll(target)) != ERROR_OK)
3220 writeGmon(samples, numSamples, CMD_ARGV[1]);
3221 command_print(CMD_CTX, "Wrote %s", CMD_ARGV[1]);
3230 static int new_int_array_element(Jim_Interp * interp, const char *varname, int idx, uint32_t val)
3233 Jim_Obj *nameObjPtr, *valObjPtr;
3236 namebuf = alloc_printf("%s(%d)", varname, idx);
3240 nameObjPtr = Jim_NewStringObj(interp, namebuf, -1);
3241 valObjPtr = Jim_NewIntObj(interp, val);
3242 if (!nameObjPtr || !valObjPtr)
3248 Jim_IncrRefCount(nameObjPtr);
3249 Jim_IncrRefCount(valObjPtr);
3250 result = Jim_SetVariable(interp, nameObjPtr, valObjPtr);
3251 Jim_DecrRefCount(interp, nameObjPtr);
3252 Jim_DecrRefCount(interp, valObjPtr);
3254 /* printf("%s(%d) <= 0%08x\n", varname, idx, val); */
3258 static int jim_mem2array(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
3260 struct command_context *context;
3261 struct target *target;
3263 context = current_command_context(interp);
3264 assert (context != NULL);
3266 target = get_current_target(context);
3269 LOG_ERROR("mem2array: no current target");
3273 return target_mem2array(interp, target, argc-1, argv + 1);
3276 static int target_mem2array(Jim_Interp *interp, struct target *target, int argc, Jim_Obj *const *argv)
3284 const char *varname;
3288 /* argv[1] = name of array to receive the data
3289 * argv[2] = desired width
3290 * argv[3] = memory address
3291 * argv[4] = count of times to read
3294 Jim_WrongNumArgs(interp, 1, argv, "varname width addr nelems");
3297 varname = Jim_GetString(argv[0], &len);
3298 /* given "foo" get space for worse case "foo(%d)" .. add 20 */
3300 e = Jim_GetLong(interp, argv[1], &l);
3306 e = Jim_GetLong(interp, argv[2], &l);
3311 e = Jim_GetLong(interp, argv[3], &l);
3327 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3328 Jim_AppendStrings(interp, Jim_GetResult(interp), "Invalid width param, must be 8/16/32", NULL);
3332 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3333 Jim_AppendStrings(interp, Jim_GetResult(interp), "mem2array: zero width read?", NULL);
3336 if ((addr + (len * width)) < addr) {
3337 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3338 Jim_AppendStrings(interp, Jim_GetResult(interp), "mem2array: addr + len - wraps to zero?", NULL);
3341 /* absurd transfer size? */
3343 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3344 Jim_AppendStrings(interp, Jim_GetResult(interp), "mem2array: absurd > 64K item request", NULL);
3349 ((width == 2) && ((addr & 1) == 0)) ||
3350 ((width == 4) && ((addr & 3) == 0))) {
3354 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3355 sprintf(buf, "mem2array address: 0x%08" PRIx32 " is not aligned for %" PRId32 " byte reads",
3358 Jim_AppendStrings(interp, Jim_GetResult(interp), buf , NULL);
3367 size_t buffersize = 4096;
3368 uint8_t *buffer = malloc(buffersize);
3375 /* Slurp... in buffer size chunks */
3377 count = len; /* in objects.. */
3378 if (count > (buffersize/width)) {
3379 count = (buffersize/width);
3382 retval = target_read_memory(target, addr, width, count, buffer);
3383 if (retval != ERROR_OK) {
3385 LOG_ERROR("mem2array: Read @ 0x%08x, w=%d, cnt=%d, failed",
3389 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3390 Jim_AppendStrings(interp, Jim_GetResult(interp), "mem2array: cannot read memory", NULL);
3394 v = 0; /* shut up gcc */
3395 for (i = 0 ;i < count ;i++, n++) {
3398 v = target_buffer_get_u32(target, &buffer[i*width]);
3401 v = target_buffer_get_u16(target, &buffer[i*width]);
3404 v = buffer[i] & 0x0ff;
3407 new_int_array_element(interp, varname, n, v);
3415 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3420 static int get_int_array_element(Jim_Interp * interp, const char *varname, int idx, uint32_t *val)
3423 Jim_Obj *nameObjPtr, *valObjPtr;
3427 namebuf = alloc_printf("%s(%d)", varname, idx);
3431 nameObjPtr = Jim_NewStringObj(interp, namebuf, -1);
3438 Jim_IncrRefCount(nameObjPtr);
3439 valObjPtr = Jim_GetVariable(interp, nameObjPtr, JIM_ERRMSG);
3440 Jim_DecrRefCount(interp, nameObjPtr);
3442 if (valObjPtr == NULL)
3445 result = Jim_GetLong(interp, valObjPtr, &l);
3446 /* printf("%s(%d) => 0%08x\n", varname, idx, val); */
3451 static int jim_array2mem(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
3453 struct command_context *context;
3454 struct target *target;
3456 context = current_command_context(interp);
3457 assert (context != NULL);
3459 target = get_current_target(context);
3460 if (target == NULL) {
3461 LOG_ERROR("array2mem: no current target");
3465 return target_array2mem(interp,target, argc-1, argv + 1);
3468 static int target_array2mem(Jim_Interp *interp, struct target *target,
3469 int argc, Jim_Obj *const *argv)
3477 const char *varname;
3481 /* argv[1] = name of array to get the data
3482 * argv[2] = desired width
3483 * argv[3] = memory address
3484 * argv[4] = count to write
3487 Jim_WrongNumArgs(interp, 0, argv, "varname width addr nelems");
3490 varname = Jim_GetString(argv[0], &len);
3491 /* given "foo" get space for worse case "foo(%d)" .. add 20 */
3493 e = Jim_GetLong(interp, argv[1], &l);
3499 e = Jim_GetLong(interp, argv[2], &l);
3504 e = Jim_GetLong(interp, argv[3], &l);
3520 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3521 Jim_AppendStrings(interp, Jim_GetResult(interp), "Invalid width param, must be 8/16/32", NULL);
3525 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3526 Jim_AppendStrings(interp, Jim_GetResult(interp), "array2mem: zero width read?", NULL);
3529 if ((addr + (len * width)) < addr) {
3530 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3531 Jim_AppendStrings(interp, Jim_GetResult(interp), "array2mem: addr + len - wraps to zero?", NULL);
3534 /* absurd transfer size? */
3536 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3537 Jim_AppendStrings(interp, Jim_GetResult(interp), "array2mem: absurd > 64K item request", NULL);
3542 ((width == 2) && ((addr & 1) == 0)) ||
3543 ((width == 4) && ((addr & 3) == 0))) {
3547 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3548 sprintf(buf, "array2mem address: 0x%08x is not aligned for %d byte reads",
3551 Jim_AppendStrings(interp, Jim_GetResult(interp), buf , NULL);
3562 size_t buffersize = 4096;
3563 uint8_t *buffer = malloc(buffersize);
3568 /* Slurp... in buffer size chunks */
3570 count = len; /* in objects.. */
3571 if (count > (buffersize/width)) {
3572 count = (buffersize/width);
3575 v = 0; /* shut up gcc */
3576 for (i = 0 ;i < count ;i++, n++) {
3577 get_int_array_element(interp, varname, n, &v);
3580 target_buffer_set_u32(target, &buffer[i*width], v);
3583 target_buffer_set_u16(target, &buffer[i*width], v);
3586 buffer[i] = v & 0x0ff;
3592 retval = target_write_memory(target, addr, width, count, buffer);
3593 if (retval != ERROR_OK) {
3595 LOG_ERROR("array2mem: Write @ 0x%08x, w=%d, cnt=%d, failed",
3599 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3600 Jim_AppendStrings(interp, Jim_GetResult(interp), "array2mem: cannot read memory", NULL);
3608 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3613 /* FIX? should we propagate errors here rather than printing them
3616 void target_handle_event(struct target *target, enum target_event e)
3618 struct target_event_action *teap;
3620 for (teap = target->event_action; teap != NULL; teap = teap->next) {
3621 if (teap->event == e) {
3622 LOG_DEBUG("target: (%d) %s (%s) event: %d (%s) action: %s",
3623 target->target_number,
3624 target_name(target),
3625 target_type_name(target),
3627 Jim_Nvp_value2name_simple(nvp_target_event, e)->name,
3628 Jim_GetString(teap->body, NULL));
3629 if (Jim_EvalObj(teap->interp, teap->body) != JIM_OK)
3631 Jim_MakeErrorMessage(teap->interp);
3632 command_print(NULL,"%s\n", Jim_GetString(Jim_GetResult(teap->interp), NULL));
3639 * Returns true only if the target has a handler for the specified event.
3641 bool target_has_event_action(struct target *target, enum target_event event)
3643 struct target_event_action *teap;
3645 for (teap = target->event_action; teap != NULL; teap = teap->next) {
3646 if (teap->event == event)
3652 enum target_cfg_param {
3655 TCFG_WORK_AREA_VIRT,
3656 TCFG_WORK_AREA_PHYS,
3657 TCFG_WORK_AREA_SIZE,
3658 TCFG_WORK_AREA_BACKUP,
3661 TCFG_CHAIN_POSITION,
3664 static Jim_Nvp nvp_config_opts[] = {
3665 { .name = "-type", .value = TCFG_TYPE },
3666 { .name = "-event", .value = TCFG_EVENT },
3667 { .name = "-work-area-virt", .value = TCFG_WORK_AREA_VIRT },
3668 { .name = "-work-area-phys", .value = TCFG_WORK_AREA_PHYS },
3669 { .name = "-work-area-size", .value = TCFG_WORK_AREA_SIZE },
3670 { .name = "-work-area-backup", .value = TCFG_WORK_AREA_BACKUP },
3671 { .name = "-endian" , .value = TCFG_ENDIAN },
3672 { .name = "-variant", .value = TCFG_VARIANT },
3673 { .name = "-chain-position", .value = TCFG_CHAIN_POSITION },
3675 { .name = NULL, .value = -1 }
3678 static int target_configure(Jim_GetOptInfo *goi, struct target *target)
3686 /* parse config or cget options ... */
3687 while (goi->argc > 0) {
3688 Jim_SetEmptyResult(goi->interp);
3689 /* Jim_GetOpt_Debug(goi); */
3691 if (target->type->target_jim_configure) {
3692 /* target defines a configure function */
3693 /* target gets first dibs on parameters */
3694 e = (*(target->type->target_jim_configure))(target, goi);
3703 /* otherwise we 'continue' below */
3705 e = Jim_GetOpt_Nvp(goi, nvp_config_opts, &n);
3707 Jim_GetOpt_NvpUnknown(goi, nvp_config_opts, 0);
3713 if (goi->isconfigure) {
3714 Jim_SetResultFormatted(goi->interp,
3715 "not settable: %s", n->name);
3719 if (goi->argc != 0) {
3720 Jim_WrongNumArgs(goi->interp,
3721 goi->argc, goi->argv,
3726 Jim_SetResultString(goi->interp,
3727 target_type_name(target), -1);
3731 if (goi->argc == 0) {
3732 Jim_WrongNumArgs(goi->interp, goi->argc, goi->argv, "-event ?event-name? ...");
3736 e = Jim_GetOpt_Nvp(goi, nvp_target_event, &n);
3738 Jim_GetOpt_NvpUnknown(goi, nvp_target_event, 1);
3742 if (goi->isconfigure) {
3743 if (goi->argc != 1) {
3744 Jim_WrongNumArgs(goi->interp, goi->argc, goi->argv, "-event ?event-name? ?EVENT-BODY?");
3748 if (goi->argc != 0) {
3749 Jim_WrongNumArgs(goi->interp, goi->argc, goi->argv, "-event ?event-name?");
3755 struct target_event_action *teap;
3757 teap = target->event_action;
3758 /* replace existing? */
3760 if (teap->event == (enum target_event)n->value) {
3766 if (goi->isconfigure) {
3767 bool replace = true;
3770 teap = calloc(1, sizeof(*teap));
3773 teap->event = n->value;
3774 teap->interp = goi->interp;
3775 Jim_GetOpt_Obj(goi, &o);
3777 Jim_DecrRefCount(teap->interp, teap->body);
3779 teap->body = Jim_DuplicateObj(goi->interp, o);
3782 * Tcl/TK - "tk events" have a nice feature.
3783 * See the "BIND" command.
3784 * We should support that here.
3785 * You can specify %X and %Y in the event code.
3786 * The idea is: %T - target name.
3787 * The idea is: %N - target number
3788 * The idea is: %E - event name.
3790 Jim_IncrRefCount(teap->body);
3794 /* add to head of event list */
3795 teap->next = target->event_action;
3796 target->event_action = teap;
3798 Jim_SetEmptyResult(goi->interp);
3802 Jim_SetEmptyResult(goi->interp);
3804 Jim_SetResult(goi->interp, Jim_DuplicateObj(goi->interp, teap->body));
3811 case TCFG_WORK_AREA_VIRT:
3812 if (goi->isconfigure) {
3813 target_free_all_working_areas(target);
3814 e = Jim_GetOpt_Wide(goi, &w);
3818 target->working_area_virt = w;
3819 target->working_area_virt_spec = true;
3821 if (goi->argc != 0) {
3825 Jim_SetResult(goi->interp, Jim_NewIntObj(goi->interp, target->working_area_virt));
3829 case TCFG_WORK_AREA_PHYS:
3830 if (goi->isconfigure) {
3831 target_free_all_working_areas(target);
3832 e = Jim_GetOpt_Wide(goi, &w);
3836 target->working_area_phys = w;
3837 target->working_area_phys_spec = true;
3839 if (goi->argc != 0) {
3843 Jim_SetResult(goi->interp, Jim_NewIntObj(goi->interp, target->working_area_phys));
3847 case TCFG_WORK_AREA_SIZE:
3848 if (goi->isconfigure) {
3849 target_free_all_working_areas(target);
3850 e = Jim_GetOpt_Wide(goi, &w);
3854 target->working_area_size = w;
3856 if (goi->argc != 0) {
3860 Jim_SetResult(goi->interp, Jim_NewIntObj(goi->interp, target->working_area_size));
3864 case TCFG_WORK_AREA_BACKUP:
3865 if (goi->isconfigure) {
3866 target_free_all_working_areas(target);
3867 e = Jim_GetOpt_Wide(goi, &w);
3871 /* make this exactly 1 or 0 */
3872 target->backup_working_area = (!!w);
3874 if (goi->argc != 0) {
3878 Jim_SetResult(goi->interp, Jim_NewIntObj(goi->interp, target->backup_working_area));
3879 /* loop for more e*/
3883 if (goi->isconfigure) {
3884 e = Jim_GetOpt_Nvp(goi, nvp_target_endian, &n);
3886 Jim_GetOpt_NvpUnknown(goi, nvp_target_endian, 1);
3889 target->endianness = n->value;
3891 if (goi->argc != 0) {
3895 n = Jim_Nvp_value2name_simple(nvp_target_endian, target->endianness);
3896 if (n->name == NULL) {
3897 target->endianness = TARGET_LITTLE_ENDIAN;
3898 n = Jim_Nvp_value2name_simple(nvp_target_endian, target->endianness);
3900 Jim_SetResultString(goi->interp, n->name, -1);
3905 if (goi->isconfigure) {
3906 if (goi->argc < 1) {
3907 Jim_SetResultFormatted(goi->interp,
3912 if (target->variant) {
3913 free((void *)(target->variant));
3915 e = Jim_GetOpt_String(goi, &cp, NULL);
3916 target->variant = strdup(cp);
3918 if (goi->argc != 0) {
3922 Jim_SetResultString(goi->interp, target->variant,-1);
3925 case TCFG_CHAIN_POSITION:
3926 if (goi->isconfigure) {
3928 struct jtag_tap *tap;
3929 target_free_all_working_areas(target);
3930 e = Jim_GetOpt_Obj(goi, &o_t);
3934 tap = jtag_tap_by_jim_obj(goi->interp, o_t);
3938 /* make this exactly 1 or 0 */
3941 if (goi->argc != 0) {
3945 Jim_SetResultString(goi->interp, target->tap->dotted_name, -1);
3946 /* loop for more e*/
3949 } /* while (goi->argc) */
3952 /* done - we return */
3957 jim_target_configure(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
3961 Jim_GetOpt_Setup(&goi, interp, argc - 1, argv + 1);
3962 goi.isconfigure = !strcmp(Jim_GetString(argv[0], NULL), "configure");
3963 int need_args = 1 + goi.isconfigure;
3964 if (goi.argc < need_args)
3966 Jim_WrongNumArgs(goi.interp, goi.argc, goi.argv,
3968 ? "missing: -option VALUE ..."
3969 : "missing: -option ...");
3972 struct target *target = Jim_CmdPrivData(goi.interp);
3973 return target_configure(&goi, target);
3976 static int jim_target_mw(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
3978 const char *cmd_name = Jim_GetString(argv[0], NULL);
3981 Jim_GetOpt_Setup(&goi, interp, argc - 1, argv + 1);
3983 if (goi.argc < 2 || goi.argc > 4)
3985 Jim_SetResultFormatted(goi.interp,
3986 "usage: %s [phys] <address> <data> [<count>]", cmd_name);
3991 fn = target_write_memory_fast;
3994 if (strcmp(Jim_GetString(argv[1], NULL), "phys") == 0)
3997 struct Jim_Obj *obj;
3998 e = Jim_GetOpt_Obj(&goi, &obj);
4002 fn = target_write_phys_memory;
4006 e = Jim_GetOpt_Wide(&goi, &a);
4011 e = Jim_GetOpt_Wide(&goi, &b);
4018 e = Jim_GetOpt_Wide(&goi, &c);
4023 /* all args must be consumed */
4029 struct target *target = Jim_CmdPrivData(goi.interp);
4031 if (strcasecmp(cmd_name, "mww") == 0) {
4034 else if (strcasecmp(cmd_name, "mwh") == 0) {
4037 else if (strcasecmp(cmd_name, "mwb") == 0) {
4040 LOG_ERROR("command '%s' unknown: ", cmd_name);
4044 return (target_fill_mem(target, a, fn, data_size, b, c) == ERROR_OK) ? JIM_OK : JIM_ERR;
4047 static int jim_target_md(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
4049 const char *cmd_name = Jim_GetString(argv[0], NULL);
4052 Jim_GetOpt_Setup(&goi, interp, argc - 1, argv + 1);
4054 if ((goi.argc < 1) || (goi.argc > 3))
4056 Jim_SetResultFormatted(goi.interp,
4057 "usage: %s [phys] <address> [<count>]", cmd_name);
4061 int (*fn)(struct target *target,
4062 uint32_t address, uint32_t size, uint32_t count, uint8_t *buffer);
4063 fn=target_read_memory;
4066 if (strcmp(Jim_GetString(argv[1], NULL), "phys") == 0)
4069 struct Jim_Obj *obj;
4070 e = Jim_GetOpt_Obj(&goi, &obj);
4074 fn=target_read_phys_memory;
4078 e = Jim_GetOpt_Wide(&goi, &a);
4083 if (goi.argc == 1) {
4084 e = Jim_GetOpt_Wide(&goi, &c);
4092 /* all args must be consumed */
4098 jim_wide b = 1; /* shut up gcc */
4099 if (strcasecmp(cmd_name, "mdw") == 0)
4101 else if (strcasecmp(cmd_name, "mdh") == 0)
4103 else if (strcasecmp(cmd_name, "mdb") == 0)
4106 LOG_ERROR("command '%s' unknown: ", cmd_name);
4110 /* convert count to "bytes" */
4113 struct target *target = Jim_CmdPrivData(goi.interp);
4114 uint8_t target_buf[32];
4121 e = fn(target, a, b, y / b, target_buf);
4122 if (e != ERROR_OK) {
4124 snprintf(tmp, sizeof(tmp), "%08lx", (long)a);
4125 Jim_SetResultFormatted(interp, "error reading target @ 0x%s", tmp);
4129 command_print(NULL, "0x%08x ", (int)(a));
4132 for (x = 0; x < 16 && x < y; x += 4)
4134 z = target_buffer_get_u32(target, &(target_buf[ x ]));
4135 command_print(NULL, "%08x ", (int)(z));
4137 for (; (x < 16) ; x += 4) {
4138 command_print(NULL, " ");
4142 for (x = 0; x < 16 && x < y; x += 2)
4144 z = target_buffer_get_u16(target, &(target_buf[ x ]));
4145 command_print(NULL, "%04x ", (int)(z));
4147 for (; (x < 16) ; x += 2) {
4148 command_print(NULL, " ");
4153 for (x = 0 ; (x < 16) && (x < y) ; x += 1) {
4154 z = target_buffer_get_u8(target, &(target_buf[ x ]));
4155 command_print(NULL, "%02x ", (int)(z));
4157 for (; (x < 16) ; x += 1) {
4158 command_print(NULL, " ");
4162 /* ascii-ify the bytes */
4163 for (x = 0 ; x < y ; x++) {
4164 if ((target_buf[x] >= 0x20) &&
4165 (target_buf[x] <= 0x7e)) {
4169 target_buf[x] = '.';
4174 target_buf[x] = ' ';
4179 /* print - with a newline */
4180 command_print(NULL, "%s\n", target_buf);
4188 static int jim_target_mem2array(Jim_Interp *interp,
4189 int argc, Jim_Obj *const *argv)
4191 struct target *target = Jim_CmdPrivData(interp);
4192 return target_mem2array(interp, target, argc - 1, argv + 1);
4195 static int jim_target_array2mem(Jim_Interp *interp,
4196 int argc, Jim_Obj *const *argv)
4198 struct target *target = Jim_CmdPrivData(interp);
4199 return target_array2mem(interp, target, argc - 1, argv + 1);
4202 static int jim_target_tap_disabled(Jim_Interp *interp)
4204 Jim_SetResultFormatted(interp, "[TAP is disabled]");
4208 static int jim_target_examine(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
4212 Jim_WrongNumArgs(interp, 1, argv, "[no parameters]");
4215 struct target *target = Jim_CmdPrivData(interp);
4216 if (!target->tap->enabled)
4217 return jim_target_tap_disabled(interp);
4219 int e = target->type->examine(target);
4222 Jim_Obj *eObj = Jim_NewIntObj(interp, e);
4223 Jim_SetResultFormatted(interp, "examine-fails: %#s", eObj);
4224 Jim_FreeNewObj(interp, eObj);
4230 static int jim_target_halt_gdb(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
4234 Jim_WrongNumArgs(interp, 1, argv, "[no parameters]");
4237 struct target *target = Jim_CmdPrivData(interp);
4239 if (target_call_event_callbacks(target, TARGET_EVENT_GDB_HALT) != ERROR_OK)
4245 static int jim_target_poll(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
4249 Jim_WrongNumArgs(interp, 1, argv, "[no parameters]");
4252 struct target *target = Jim_CmdPrivData(interp);
4253 if (!target->tap->enabled)
4254 return jim_target_tap_disabled(interp);
4257 if (!(target_was_examined(target))) {
4258 e = ERROR_TARGET_NOT_EXAMINED;
4260 e = target->type->poll(target);
4264 Jim_Obj *eObj = Jim_NewIntObj(interp, e);
4265 Jim_SetResultFormatted(interp, "poll-fails: %#s", eObj);
4266 Jim_FreeNewObj(interp, eObj);
4272 static int jim_target_reset(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
4275 Jim_GetOpt_Setup(&goi, interp, argc - 1, argv + 1);
4279 Jim_WrongNumArgs(interp, 0, argv,
4280 "([tT]|[fF]|assert|deassert) BOOL");
4285 int e = Jim_GetOpt_Nvp(&goi, nvp_assert, &n);
4288 Jim_GetOpt_NvpUnknown(&goi, nvp_assert, 1);
4291 /* the halt or not param */
4293 e = Jim_GetOpt_Wide(&goi, &a);
4297 struct target *target = Jim_CmdPrivData(goi.interp);
4298 if (!target->tap->enabled)
4299 return jim_target_tap_disabled(interp);
4300 if (!(target_was_examined(target)))
4302 LOG_ERROR("Target not examined yet");
4303 return ERROR_TARGET_NOT_EXAMINED;
4305 if (!target->type->assert_reset || !target->type->deassert_reset)
4307 Jim_SetResultFormatted(interp,
4308 "No target-specific reset for %s",
4309 target_name(target));
4312 /* determine if we should halt or not. */
4313 target->reset_halt = !!a;
4314 /* When this happens - all workareas are invalid. */
4315 target_free_all_working_areas_restore(target, 0);
4318 if (n->value == NVP_ASSERT) {
4319 e = target->type->assert_reset(target);
4321 e = target->type->deassert_reset(target);
4323 return (e == ERROR_OK) ? JIM_OK : JIM_ERR;
4326 static int jim_target_halt(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
4329 Jim_WrongNumArgs(interp, 1, argv, "[no parameters]");
4332 struct target *target = Jim_CmdPrivData(interp);
4333 if (!target->tap->enabled)
4334 return jim_target_tap_disabled(interp);
4335 int e = target->type->halt(target);
4336 return (e == ERROR_OK) ? JIM_OK : JIM_ERR;
4339 static int jim_target_wait_state(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
4342 Jim_GetOpt_Setup(&goi, interp, argc - 1, argv + 1);
4344 /* params: <name> statename timeoutmsecs */
4347 const char *cmd_name = Jim_GetString(argv[0], NULL);
4348 Jim_SetResultFormatted(goi.interp,
4349 "%s <state_name> <timeout_in_msec>", cmd_name);
4354 int e = Jim_GetOpt_Nvp(&goi, nvp_target_state, &n);
4356 Jim_GetOpt_NvpUnknown(&goi, nvp_target_state,1);
4360 e = Jim_GetOpt_Wide(&goi, &a);
4364 struct target *target = Jim_CmdPrivData(interp);
4365 if (!target->tap->enabled)
4366 return jim_target_tap_disabled(interp);
4368 e = target_wait_state(target, n->value, a);
4371 Jim_Obj *eObj = Jim_NewIntObj(interp, e);
4372 Jim_SetResultFormatted(goi.interp,
4373 "target: %s wait %s fails (%#s) %s",
4374 target_name(target), n->name,
4375 eObj, target_strerror_safe(e));
4376 Jim_FreeNewObj(interp, eObj);
4381 /* List for human, Events defined for this target.
4382 * scripts/programs should use 'name cget -event NAME'
4384 static int jim_target_event_list(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
4386 struct command_context *cmd_ctx = current_command_context(interp);
4387 assert (cmd_ctx != NULL);
4389 struct target *target = Jim_CmdPrivData(interp);
4390 struct target_event_action *teap = target->event_action;
4391 command_print(cmd_ctx, "Event actions for target (%d) %s\n",
4392 target->target_number,
4393 target_name(target));
4394 command_print(cmd_ctx, "%-25s | Body", "Event");
4395 command_print(cmd_ctx, "------------------------- | "
4396 "----------------------------------------");
4399 Jim_Nvp *opt = Jim_Nvp_value2name_simple(nvp_target_event, teap->event);
4400 command_print(cmd_ctx, "%-25s | %s",
4401 opt->name, Jim_GetString(teap->body, NULL));
4404 command_print(cmd_ctx, "***END***");
4407 static int jim_target_current_state(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
4411 Jim_WrongNumArgs(interp, 1, argv, "[no parameters]");
4414 struct target *target = Jim_CmdPrivData(interp);
4415 Jim_SetResultString(interp, target_state_name(target), -1);
4418 static int jim_target_invoke_event(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
4421 Jim_GetOpt_Setup(&goi, interp, argc - 1, argv + 1);
4424 const char *cmd_name = Jim_GetString(argv[0], NULL);
4425 Jim_SetResultFormatted(goi.interp, "%s <eventname>", cmd_name);
4429 int e = Jim_GetOpt_Nvp(&goi, nvp_target_event, &n);
4432 Jim_GetOpt_NvpUnknown(&goi, nvp_target_event, 1);
4435 struct target *target = Jim_CmdPrivData(interp);
4436 target_handle_event(target, n->value);
4440 static const struct command_registration target_instance_command_handlers[] = {
4442 .name = "configure",
4443 .mode = COMMAND_CONFIG,
4444 .jim_handler = jim_target_configure,
4445 .help = "configure a new target for use",
4446 .usage = "[target_attribute ...]",
4450 .mode = COMMAND_ANY,
4451 .jim_handler = jim_target_configure,
4452 .help = "returns the specified target attribute",
4453 .usage = "target_attribute",
4457 .mode = COMMAND_EXEC,
4458 .jim_handler = jim_target_mw,
4459 .help = "Write 32-bit word(s) to target memory",
4460 .usage = "address data [count]",
4464 .mode = COMMAND_EXEC,
4465 .jim_handler = jim_target_mw,
4466 .help = "Write 16-bit half-word(s) to target memory",
4467 .usage = "address data [count]",
4471 .mode = COMMAND_EXEC,
4472 .jim_handler = jim_target_mw,
4473 .help = "Write byte(s) to target memory",
4474 .usage = "address data [count]",
4478 .mode = COMMAND_EXEC,
4479 .jim_handler = jim_target_md,
4480 .help = "Display target memory as 32-bit words",
4481 .usage = "address [count]",
4485 .mode = COMMAND_EXEC,
4486 .jim_handler = jim_target_md,
4487 .help = "Display target memory as 16-bit half-words",
4488 .usage = "address [count]",
4492 .mode = COMMAND_EXEC,
4493 .jim_handler = jim_target_md,
4494 .help = "Display target memory as 8-bit bytes",
4495 .usage = "address [count]",
4498 .name = "array2mem",
4499 .mode = COMMAND_EXEC,
4500 .jim_handler = jim_target_array2mem,
4501 .help = "Writes Tcl array of 8/16/32 bit numbers "
4503 .usage = "arrayname bitwidth address count",
4506 .name = "mem2array",
4507 .mode = COMMAND_EXEC,
4508 .jim_handler = jim_target_mem2array,
4509 .help = "Loads Tcl array of 8/16/32 bit numbers "
4510 "from target memory",
4511 .usage = "arrayname bitwidth address count",
4514 .name = "eventlist",
4515 .mode = COMMAND_EXEC,
4516 .jim_handler = jim_target_event_list,
4517 .help = "displays a table of events defined for this target",
4521 .mode = COMMAND_EXEC,
4522 .jim_handler = jim_target_current_state,
4523 .help = "displays the current state of this target",
4526 .name = "arp_examine",
4527 .mode = COMMAND_EXEC,
4528 .jim_handler = jim_target_examine,
4529 .help = "used internally for reset processing",
4532 .name = "arp_halt_gdb",
4533 .mode = COMMAND_EXEC,
4534 .jim_handler = jim_target_halt_gdb,
4535 .help = "used internally for reset processing to halt GDB",
4539 .mode = COMMAND_EXEC,
4540 .jim_handler = jim_target_poll,
4541 .help = "used internally for reset processing",
4544 .name = "arp_reset",
4545 .mode = COMMAND_EXEC,
4546 .jim_handler = jim_target_reset,
4547 .help = "used internally for reset processing",
4551 .mode = COMMAND_EXEC,
4552 .jim_handler = jim_target_halt,
4553 .help = "used internally for reset processing",
4556 .name = "arp_waitstate",
4557 .mode = COMMAND_EXEC,
4558 .jim_handler = jim_target_wait_state,
4559 .help = "used internally for reset processing",
4562 .name = "invoke-event",
4563 .mode = COMMAND_EXEC,
4564 .jim_handler = jim_target_invoke_event,
4565 .help = "invoke handler for specified event",
4566 .usage = "event_name",
4568 COMMAND_REGISTRATION_DONE
4571 static int target_create(Jim_GetOptInfo *goi)
4579 struct target *target;
4580 struct command_context *cmd_ctx;
4582 cmd_ctx = current_command_context(goi->interp);
4583 assert (cmd_ctx != NULL);
4585 if (goi->argc < 3) {
4586 Jim_WrongNumArgs(goi->interp, 1, goi->argv, "?name? ?type? ..options...");
4591 Jim_GetOpt_Obj(goi, &new_cmd);
4592 /* does this command exist? */
4593 cmd = Jim_GetCommand(goi->interp, new_cmd, JIM_ERRMSG);
4595 cp = Jim_GetString(new_cmd, NULL);
4596 Jim_SetResultFormatted(goi->interp, "Command/target: %s Exists", cp);
4601 e = Jim_GetOpt_String(goi, &cp2, NULL);
4603 /* now does target type exist */
4604 for (x = 0 ; target_types[x] ; x++) {
4605 if (0 == strcmp(cp, target_types[x]->name)) {
4610 if (target_types[x] == NULL) {
4611 Jim_SetResultFormatted(goi->interp, "Unknown target type %s, try one of ", cp);
4612 for (x = 0 ; target_types[x] ; x++) {
4613 if (target_types[x + 1]) {
4614 Jim_AppendStrings(goi->interp,
4615 Jim_GetResult(goi->interp),
4616 target_types[x]->name,
4619 Jim_AppendStrings(goi->interp,
4620 Jim_GetResult(goi->interp),
4622 target_types[x]->name,NULL);
4629 target = calloc(1,sizeof(struct target));
4630 /* set target number */
4631 target->target_number = new_target_number();
4633 /* allocate memory for each unique target type */
4634 target->type = (struct target_type*)calloc(1,sizeof(struct target_type));
4636 memcpy(target->type, target_types[x], sizeof(struct target_type));
4638 /* will be set by "-endian" */
4639 target->endianness = TARGET_ENDIAN_UNKNOWN;
4641 target->working_area = 0x0;
4642 target->working_area_size = 0x0;
4643 target->working_areas = NULL;
4644 target->backup_working_area = 0;
4646 target->state = TARGET_UNKNOWN;
4647 target->debug_reason = DBG_REASON_UNDEFINED;
4648 target->reg_cache = NULL;
4649 target->breakpoints = NULL;
4650 target->watchpoints = NULL;
4651 target->next = NULL;
4652 target->arch_info = NULL;
4654 target->display = 1;
4656 target->halt_issued = false;
4658 /* initialize trace information */
4659 target->trace_info = malloc(sizeof(struct trace));
4660 target->trace_info->num_trace_points = 0;
4661 target->trace_info->trace_points_size = 0;
4662 target->trace_info->trace_points = NULL;
4663 target->trace_info->trace_history_size = 0;
4664 target->trace_info->trace_history = NULL;
4665 target->trace_info->trace_history_pos = 0;
4666 target->trace_info->trace_history_overflowed = 0;
4668 target->dbgmsg = NULL;
4669 target->dbg_msg_enabled = 0;
4671 target->endianness = TARGET_ENDIAN_UNKNOWN;
4673 /* Do the rest as "configure" options */
4674 goi->isconfigure = 1;
4675 e = target_configure(goi, target);
4677 if (target->tap == NULL)
4679 Jim_SetResultString(goi->interp, "-chain-position required when creating target", -1);
4689 if (target->endianness == TARGET_ENDIAN_UNKNOWN) {
4690 /* default endian to little if not specified */
4691 target->endianness = TARGET_LITTLE_ENDIAN;
4694 /* incase variant is not set */
4695 if (!target->variant)
4696 target->variant = strdup("");
4698 cp = Jim_GetString(new_cmd, NULL);
4699 target->cmd_name = strdup(cp);
4701 /* create the target specific commands */
4702 if (target->type->commands) {
4703 e = register_commands(cmd_ctx, NULL, target->type->commands);
4705 LOG_ERROR("unable to register '%s' commands", cp);
4707 if (target->type->target_create) {
4708 (*(target->type->target_create))(target, goi->interp);
4711 /* append to end of list */
4713 struct target **tpp;
4714 tpp = &(all_targets);
4716 tpp = &((*tpp)->next);
4721 /* now - create the new target name command */
4722 const const struct command_registration target_subcommands[] = {
4724 .chain = target_instance_command_handlers,
4727 .chain = target->type->commands,
4729 COMMAND_REGISTRATION_DONE
4731 const const struct command_registration target_commands[] = {
4734 .mode = COMMAND_ANY,
4735 .help = "target command group",
4736 .chain = target_subcommands,
4738 COMMAND_REGISTRATION_DONE
4740 e = register_commands(cmd_ctx, NULL, target_commands);
4744 struct command *c = command_find_in_context(cmd_ctx, cp);
4746 command_set_handler_data(c, target);
4748 return (ERROR_OK == e) ? JIM_OK : JIM_ERR;
4751 static int jim_target_current(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
4755 Jim_WrongNumArgs(interp, 1, argv, "Too many parameters");
4758 struct command_context *cmd_ctx = current_command_context(interp);
4759 assert (cmd_ctx != NULL);
4761 Jim_SetResultString(interp, get_current_target(cmd_ctx)->cmd_name, -1);
4765 static int jim_target_types(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
4769 Jim_WrongNumArgs(interp, 1, argv, "Too many parameters");
4772 Jim_SetResult(interp, Jim_NewListObj(interp, NULL, 0));
4773 for (unsigned x = 0; NULL != target_types[x]; x++)
4775 Jim_ListAppendElement(interp, Jim_GetResult(interp),
4776 Jim_NewStringObj(interp, target_types[x]->name, -1));
4781 static int jim_target_names(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
4785 Jim_WrongNumArgs(interp, 1, argv, "Too many parameters");
4788 Jim_SetResult(interp, Jim_NewListObj(interp, NULL, 0));
4789 struct target *target = all_targets;
4792 Jim_ListAppendElement(interp, Jim_GetResult(interp),
4793 Jim_NewStringObj(interp, target_name(target), -1));
4794 target = target->next;
4799 static int jim_target_create(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
4802 Jim_GetOpt_Setup(&goi, interp, argc - 1, argv + 1);
4805 Jim_WrongNumArgs(goi.interp, goi.argc, goi.argv,
4806 "<name> <target_type> [<target_options> ...]");
4809 return target_create(&goi);
4812 static int jim_target_number(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
4815 Jim_GetOpt_Setup(&goi, interp, argc - 1, argv + 1);
4817 /* It's OK to remove this mechanism sometime after August 2010 or so */
4818 LOG_WARNING("don't use numbers as target identifiers; use names");
4821 Jim_SetResultFormatted(goi.interp, "usage: target number <number>");
4825 int e = Jim_GetOpt_Wide(&goi, &w);
4829 struct target *target;
4830 for (target = all_targets; NULL != target; target = target->next)
4832 if (target->target_number != w)
4835 Jim_SetResultString(goi.interp, target_name(target), -1);
4839 Jim_Obj *wObj = Jim_NewIntObj(goi.interp, w);
4840 Jim_SetResultFormatted(goi.interp,
4841 "Target: number %#s does not exist", wObj);
4842 Jim_FreeNewObj(interp, wObj);
4847 static int jim_target_count(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
4851 Jim_WrongNumArgs(interp, 1, argv, "<no parameters>");
4855 struct target *target = all_targets;
4856 while (NULL != target)
4858 target = target->next;
4861 Jim_SetResult(interp, Jim_NewIntObj(interp, count));
4865 static const struct command_registration target_subcommand_handlers[] = {
4868 .mode = COMMAND_CONFIG,
4869 .handler = handle_target_init_command,
4870 .help = "initialize targets",
4874 /* REVISIT this should be COMMAND_CONFIG ... */
4875 .mode = COMMAND_ANY,
4876 .jim_handler = jim_target_create,
4877 .usage = "name type '-chain-position' name [options ...]",
4878 .help = "Creates and selects a new target",
4882 .mode = COMMAND_ANY,
4883 .jim_handler = jim_target_current,
4884 .help = "Returns the currently selected target",
4888 .mode = COMMAND_ANY,
4889 .jim_handler = jim_target_types,
4890 .help = "Returns the available target types as "
4891 "a list of strings",
4895 .mode = COMMAND_ANY,
4896 .jim_handler = jim_target_names,
4897 .help = "Returns the names of all targets as a list of strings",
4901 .mode = COMMAND_ANY,
4902 .jim_handler = jim_target_number,
4904 .help = "Returns the name of the numbered target "
4909 .mode = COMMAND_ANY,
4910 .jim_handler = jim_target_count,
4911 .help = "Returns the number of targets as an integer "
4914 COMMAND_REGISTRATION_DONE
4925 static int fastload_num;
4926 static struct FastLoad *fastload;
4928 static void free_fastload(void)
4930 if (fastload != NULL)
4933 for (i = 0; i < fastload_num; i++)
4935 if (fastload[i].data)
4936 free(fastload[i].data);
4946 COMMAND_HANDLER(handle_fast_load_image_command)
4950 uint32_t image_size;
4951 uint32_t min_address = 0;
4952 uint32_t max_address = 0xffffffff;
4957 int retval = CALL_COMMAND_HANDLER(parse_load_image_command_CMD_ARGV,
4958 &image, &min_address, &max_address);
4959 if (ERROR_OK != retval)
4962 struct duration bench;
4963 duration_start(&bench);
4965 if (image_open(&image, CMD_ARGV[0], (CMD_ARGC >= 3) ? CMD_ARGV[2] : NULL) != ERROR_OK)
4972 fastload_num = image.num_sections;
4973 fastload = (struct FastLoad *)malloc(sizeof(struct FastLoad)*image.num_sections);
4974 if (fastload == NULL)
4976 image_close(&image);
4979 memset(fastload, 0, sizeof(struct FastLoad)*image.num_sections);
4980 for (i = 0; i < image.num_sections; i++)
4982 buffer = malloc(image.sections[i].size);
4985 command_print(CMD_CTX, "error allocating buffer for section (%d bytes)",
4986 (int)(image.sections[i].size));
4990 if ((retval = image_read_section(&image, i, 0x0, image.sections[i].size, buffer, &buf_cnt)) != ERROR_OK)
4996 uint32_t offset = 0;
4997 uint32_t length = buf_cnt;
5000 /* DANGER!!! beware of unsigned comparision here!!! */
5002 if ((image.sections[i].base_address + buf_cnt >= min_address)&&
5003 (image.sections[i].base_address < max_address))
5005 if (image.sections[i].base_address < min_address)
5007 /* clip addresses below */
5008 offset += min_address-image.sections[i].base_address;
5012 if (image.sections[i].base_address + buf_cnt > max_address)
5014 length -= (image.sections[i].base_address + buf_cnt)-max_address;
5017 fastload[i].address = image.sections[i].base_address + offset;
5018 fastload[i].data = malloc(length);
5019 if (fastload[i].data == NULL)
5024 memcpy(fastload[i].data, buffer + offset, length);
5025 fastload[i].length = length;
5027 image_size += length;
5028 command_print(CMD_CTX, "%u bytes written at address 0x%8.8x",
5029 (unsigned int)length,
5030 ((unsigned int)(image.sections[i].base_address + offset)));
5036 if ((ERROR_OK == retval) && (duration_measure(&bench) == ERROR_OK))
5038 command_print(CMD_CTX, "Loaded %" PRIu32 " bytes "
5039 "in %fs (%0.3f KiB/s)", image_size,
5040 duration_elapsed(&bench), duration_kbps(&bench, image_size));
5042 command_print(CMD_CTX,
5043 "WARNING: image has not been loaded to target!"
5044 "You can issue a 'fast_load' to finish loading.");
5047 image_close(&image);
5049 if (retval != ERROR_OK)
5057 COMMAND_HANDLER(handle_fast_load_command)
5060 return ERROR_COMMAND_SYNTAX_ERROR;
5061 if (fastload == NULL)
5063 LOG_ERROR("No image in memory");
5067 int ms = timeval_ms();
5069 int retval = ERROR_OK;
5070 for (i = 0; i < fastload_num;i++)
5072 struct target *target = get_current_target(CMD_CTX);
5073 command_print(CMD_CTX, "Write to 0x%08x, length 0x%08x",
5074 (unsigned int)(fastload[i].address),
5075 (unsigned int)(fastload[i].length));
5076 if (retval == ERROR_OK)
5078 retval = target_write_buffer(target, fastload[i].address, fastload[i].length, fastload[i].data);
5080 size += fastload[i].length;
5082 int after = timeval_ms();
5083 command_print(CMD_CTX, "Loaded image %f kBytes/s", (float)(size/1024.0)/((float)(after-ms)/1000.0));
5087 static const struct command_registration target_command_handlers[] = {
5090 .handler = handle_targets_command,
5091 .mode = COMMAND_ANY,
5092 .help = "change current default target (one parameter) "
5093 "or prints table of all targets (no parameters)",
5094 .usage = "[target]",
5098 .mode = COMMAND_CONFIG,
5099 .help = "configure target",
5101 .chain = target_subcommand_handlers,
5103 COMMAND_REGISTRATION_DONE
5106 int target_register_commands(struct command_context *cmd_ctx)
5108 return register_commands(cmd_ctx, NULL, target_command_handlers);
5111 static bool target_reset_nag = true;
5113 bool get_target_reset_nag(void)
5115 return target_reset_nag;
5118 COMMAND_HANDLER(handle_target_reset_nag)
5120 return CALL_COMMAND_HANDLER(handle_command_parse_bool,
5121 &target_reset_nag, "Nag after each reset about options to improve "
5125 static const struct command_registration target_exec_command_handlers[] = {
5127 .name = "fast_load_image",
5128 .handler = handle_fast_load_image_command,
5129 .mode = COMMAND_ANY,
5130 .help = "Load image into server memory for later use by "
5131 "fast_load; primarily for profiling",
5132 .usage = "filename address ['bin'|'ihex'|'elf'|'s19'] "
5133 "[min_address [max_length]]",
5136 .name = "fast_load",
5137 .handler = handle_fast_load_command,
5138 .mode = COMMAND_EXEC,
5139 .help = "loads active fast load image to current target "
5140 "- mainly for profiling purposes",
5144 .handler = handle_profile_command,
5145 .mode = COMMAND_EXEC,
5146 .help = "profiling samples the CPU PC",
5148 /** @todo don't register virt2phys() unless target supports it */
5150 .name = "virt2phys",
5151 .handler = handle_virt2phys_command,
5152 .mode = COMMAND_ANY,
5153 .help = "translate a virtual address into a physical address",
5154 .usage = "virtual_address",
5158 .handler = handle_reg_command,
5159 .mode = COMMAND_EXEC,
5160 .help = "display or set a register; with no arguments, "
5161 "displays all registers and their values",
5162 .usage = "[(register_name|register_number) [value]]",
5166 .handler = handle_poll_command,
5167 .mode = COMMAND_EXEC,
5168 .help = "poll target state; or reconfigure background polling",
5169 .usage = "['on'|'off']",
5172 .name = "wait_halt",
5173 .handler = handle_wait_halt_command,
5174 .mode = COMMAND_EXEC,
5175 .help = "wait up to the specified number of milliseconds "
5176 "(default 5) for a previously requested halt",
5177 .usage = "[milliseconds]",
5181 .handler = handle_halt_command,
5182 .mode = COMMAND_EXEC,
5183 .help = "request target to halt, then wait up to the specified"
5184 "number of milliseconds (default 5) for it to complete",
5185 .usage = "[milliseconds]",
5189 .handler = handle_resume_command,
5190 .mode = COMMAND_EXEC,
5191 .help = "resume target execution from current PC or address",
5192 .usage = "[address]",
5196 .handler = handle_reset_command,
5197 .mode = COMMAND_EXEC,
5198 .usage = "[run|halt|init]",
5199 .help = "Reset all targets into the specified mode."
5200 "Default reset mode is run, if not given.",
5203 .name = "soft_reset_halt",
5204 .handler = handle_soft_reset_halt_command,
5205 .mode = COMMAND_EXEC,
5206 .help = "halt the target and do a soft reset",
5210 .handler = handle_step_command,
5211 .mode = COMMAND_EXEC,
5212 .help = "step one instruction from current PC or address",
5213 .usage = "[address]",
5217 .handler = handle_md_command,
5218 .mode = COMMAND_EXEC,
5219 .help = "display memory words",
5220 .usage = "['phys'] address [count]",
5224 .handler = handle_md_command,
5225 .mode = COMMAND_EXEC,
5226 .help = "display memory half-words",
5227 .usage = "['phys'] address [count]",
5231 .handler = handle_md_command,
5232 .mode = COMMAND_EXEC,
5233 .help = "display memory bytes",
5234 .usage = "['phys'] address [count]",
5238 .handler = handle_mw_command,
5239 .mode = COMMAND_EXEC,
5240 .help = "write memory word",
5241 .usage = "['phys'] address value [count]",
5245 .handler = handle_mw_command,
5246 .mode = COMMAND_EXEC,
5247 .help = "write memory half-word",
5248 .usage = "['phys'] address value [count]",
5252 .handler = handle_mw_command,
5253 .mode = COMMAND_EXEC,
5254 .help = "write memory byte",
5255 .usage = "['phys'] address value [count]",
5259 .handler = handle_bp_command,
5260 .mode = COMMAND_EXEC,
5261 .help = "list or set hardware or software breakpoint",
5262 .usage = "[address length ['hw']]",
5266 .handler = handle_rbp_command,
5267 .mode = COMMAND_EXEC,
5268 .help = "remove breakpoint",
5273 .handler = handle_wp_command,
5274 .mode = COMMAND_EXEC,
5275 .help = "list (no params) or create watchpoints",
5276 .usage = "[address length [('r'|'w'|'a') value [mask]]]",
5280 .handler = handle_rwp_command,
5281 .mode = COMMAND_EXEC,
5282 .help = "remove watchpoint",
5286 .name = "load_image",
5287 .handler = handle_load_image_command,
5288 .mode = COMMAND_EXEC,
5289 .usage = "filename address ['bin'|'ihex'|'elf'|'s19'] "
5290 "[min_address] [max_length]",
5293 .name = "dump_image",
5294 .handler = handle_dump_image_command,
5295 .mode = COMMAND_EXEC,
5296 .usage = "filename address size",
5299 .name = "verify_image",
5300 .handler = handle_verify_image_command,
5301 .mode = COMMAND_EXEC,
5302 .usage = "filename [offset [type]]",
5305 .name = "test_image",
5306 .handler = handle_test_image_command,
5307 .mode = COMMAND_EXEC,
5308 .usage = "filename [offset [type]]",
5311 .name = "mem2array",
5312 .mode = COMMAND_EXEC,
5313 .jim_handler = jim_mem2array,
5314 .help = "read 8/16/32 bit memory and return as a TCL array "
5315 "for script processing",
5316 .usage = "arrayname bitwidth address count",
5319 .name = "array2mem",
5320 .mode = COMMAND_EXEC,
5321 .jim_handler = jim_array2mem,
5322 .help = "convert a TCL array to memory locations "
5323 "and write the 8/16/32 bit values",
5324 .usage = "arrayname bitwidth address count",
5327 .name = "reset_nag",
5328 .handler = handle_target_reset_nag,
5329 .mode = COMMAND_ANY,
5330 .help = "Nag after each reset about options that could have been "
5331 "enabled to improve performance. ",
5332 .usage = "['enable'|'disable']",
5334 COMMAND_REGISTRATION_DONE
5336 static int target_register_user_commands(struct command_context *cmd_ctx)
5338 int retval = ERROR_OK;
5339 if ((retval = target_request_register_commands(cmd_ctx)) != ERROR_OK)
5342 if ((retval = trace_register_commands(cmd_ctx)) != ERROR_OK)
5346 return register_commands(cmd_ctx, NULL, target_exec_command_handlers);