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 * Copyright (C) 2011 by Broadcom Corporation *
18 * Evan Hunter - ehunter@broadcom.com *
20 * Copyright (C) ST-Ericsson SA 2011 *
21 * michel.jaouen@stericsson.com : smp minimum support *
23 * This program is free software; you can redistribute it and/or modify *
24 * it under the terms of the GNU General Public License as published by *
25 * the Free Software Foundation; either version 2 of the License, or *
26 * (at your option) any later version. *
28 * This program is distributed in the hope that it will be useful, *
29 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
30 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
31 * GNU General Public License for more details. *
33 * You should have received a copy of the GNU General Public License *
34 * along with this program; if not, write to the *
35 * Free Software Foundation, Inc., *
36 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
37 ***************************************************************************/
42 #include <helper/time_support.h>
43 #include <jtag/jtag.h>
44 #include <flash/nor/core.h>
47 #include "target_type.h"
48 #include "target_request.h"
49 #include "breakpoints.h"
53 #include "rtos/rtos.h"
56 static int target_read_buffer_default(struct target *target, uint32_t address,
57 uint32_t size, uint8_t *buffer);
58 static int target_write_buffer_default(struct target *target, uint32_t address,
59 uint32_t size, const uint8_t *buffer);
60 static int target_array2mem(Jim_Interp *interp, struct target *target,
61 int argc, Jim_Obj *const *argv);
62 static int target_mem2array(Jim_Interp *interp, struct target *target,
63 int argc, Jim_Obj *const *argv);
64 static int target_register_user_commands(struct command_context *cmd_ctx);
67 extern struct target_type arm7tdmi_target;
68 extern struct target_type arm720t_target;
69 extern struct target_type arm9tdmi_target;
70 extern struct target_type arm920t_target;
71 extern struct target_type arm966e_target;
72 extern struct target_type arm946e_target;
73 extern struct target_type arm926ejs_target;
74 extern struct target_type fa526_target;
75 extern struct target_type feroceon_target;
76 extern struct target_type dragonite_target;
77 extern struct target_type xscale_target;
78 extern struct target_type cortexm3_target;
79 extern struct target_type cortexa8_target;
80 extern struct target_type arm11_target;
81 extern struct target_type mips_m4k_target;
82 extern struct target_type avr_target;
83 extern struct target_type dsp563xx_target;
84 extern struct target_type dsp5680xx_target;
85 extern struct target_type testee_target;
86 extern struct target_type avr32_ap7k_target;
88 static struct target_type *target_types[] =
113 struct target *all_targets = NULL;
114 static struct target_event_callback *target_event_callbacks = NULL;
115 static struct target_timer_callback *target_timer_callbacks = NULL;
116 static const int polling_interval = 100;
118 static const Jim_Nvp nvp_assert[] = {
119 { .name = "assert", NVP_ASSERT },
120 { .name = "deassert", NVP_DEASSERT },
121 { .name = "T", NVP_ASSERT },
122 { .name = "F", NVP_DEASSERT },
123 { .name = "t", NVP_ASSERT },
124 { .name = "f", NVP_DEASSERT },
125 { .name = NULL, .value = -1 }
128 static const Jim_Nvp nvp_error_target[] = {
129 { .value = ERROR_TARGET_INVALID, .name = "err-invalid" },
130 { .value = ERROR_TARGET_INIT_FAILED, .name = "err-init-failed" },
131 { .value = ERROR_TARGET_TIMEOUT, .name = "err-timeout" },
132 { .value = ERROR_TARGET_NOT_HALTED, .name = "err-not-halted" },
133 { .value = ERROR_TARGET_FAILURE, .name = "err-failure" },
134 { .value = ERROR_TARGET_UNALIGNED_ACCESS , .name = "err-unaligned-access" },
135 { .value = ERROR_TARGET_DATA_ABORT , .name = "err-data-abort" },
136 { .value = ERROR_TARGET_RESOURCE_NOT_AVAILABLE , .name = "err-resource-not-available" },
137 { .value = ERROR_TARGET_TRANSLATION_FAULT , .name = "err-translation-fault" },
138 { .value = ERROR_TARGET_NOT_RUNNING, .name = "err-not-running" },
139 { .value = ERROR_TARGET_NOT_EXAMINED, .name = "err-not-examined" },
140 { .value = -1, .name = NULL }
143 static const char *target_strerror_safe(int err)
147 n = Jim_Nvp_value2name_simple(nvp_error_target, err);
148 if (n->name == NULL) {
155 static const Jim_Nvp nvp_target_event[] = {
156 { .value = TARGET_EVENT_OLD_gdb_program_config , .name = "old-gdb_program_config" },
157 { .value = TARGET_EVENT_OLD_pre_resume , .name = "old-pre_resume" },
159 { .value = TARGET_EVENT_GDB_HALT, .name = "gdb-halt" },
160 { .value = TARGET_EVENT_HALTED, .name = "halted" },
161 { .value = TARGET_EVENT_RESUMED, .name = "resumed" },
162 { .value = TARGET_EVENT_RESUME_START, .name = "resume-start" },
163 { .value = TARGET_EVENT_RESUME_END, .name = "resume-end" },
165 { .name = "gdb-start", .value = TARGET_EVENT_GDB_START },
166 { .name = "gdb-end", .value = TARGET_EVENT_GDB_END },
168 /* historical name */
170 { .value = TARGET_EVENT_RESET_START, .name = "reset-start" },
172 { .value = TARGET_EVENT_RESET_ASSERT_PRE, .name = "reset-assert-pre" },
173 { .value = TARGET_EVENT_RESET_ASSERT, .name = "reset-assert" },
174 { .value = TARGET_EVENT_RESET_ASSERT_POST, .name = "reset-assert-post" },
175 { .value = TARGET_EVENT_RESET_DEASSERT_PRE, .name = "reset-deassert-pre" },
176 { .value = TARGET_EVENT_RESET_DEASSERT_POST, .name = "reset-deassert-post" },
177 { .value = TARGET_EVENT_RESET_HALT_PRE, .name = "reset-halt-pre" },
178 { .value = TARGET_EVENT_RESET_HALT_POST, .name = "reset-halt-post" },
179 { .value = TARGET_EVENT_RESET_WAIT_PRE, .name = "reset-wait-pre" },
180 { .value = TARGET_EVENT_RESET_WAIT_POST, .name = "reset-wait-post" },
181 { .value = TARGET_EVENT_RESET_INIT, .name = "reset-init" },
182 { .value = TARGET_EVENT_RESET_END, .name = "reset-end" },
184 { .value = TARGET_EVENT_EXAMINE_START, .name = "examine-start" },
185 { .value = TARGET_EVENT_EXAMINE_END, .name = "examine-end" },
187 { .value = TARGET_EVENT_DEBUG_HALTED, .name = "debug-halted" },
188 { .value = TARGET_EVENT_DEBUG_RESUMED, .name = "debug-resumed" },
190 { .value = TARGET_EVENT_GDB_ATTACH, .name = "gdb-attach" },
191 { .value = TARGET_EVENT_GDB_DETACH, .name = "gdb-detach" },
193 { .value = TARGET_EVENT_GDB_FLASH_WRITE_START, .name = "gdb-flash-write-start" },
194 { .value = TARGET_EVENT_GDB_FLASH_WRITE_END , .name = "gdb-flash-write-end" },
196 { .value = TARGET_EVENT_GDB_FLASH_ERASE_START, .name = "gdb-flash-erase-start" },
197 { .value = TARGET_EVENT_GDB_FLASH_ERASE_END , .name = "gdb-flash-erase-end" },
199 { .value = TARGET_EVENT_RESUME_START, .name = "resume-start" },
200 { .value = TARGET_EVENT_RESUMED , .name = "resume-ok" },
201 { .value = TARGET_EVENT_RESUME_END , .name = "resume-end" },
203 { .name = NULL, .value = -1 }
206 static const Jim_Nvp nvp_target_state[] = {
207 { .name = "unknown", .value = TARGET_UNKNOWN },
208 { .name = "running", .value = TARGET_RUNNING },
209 { .name = "halted", .value = TARGET_HALTED },
210 { .name = "reset", .value = TARGET_RESET },
211 { .name = "debug-running", .value = TARGET_DEBUG_RUNNING },
212 { .name = NULL, .value = -1 },
215 static const Jim_Nvp nvp_target_debug_reason [] = {
216 { .name = "debug-request" , .value = DBG_REASON_DBGRQ },
217 { .name = "breakpoint" , .value = DBG_REASON_BREAKPOINT },
218 { .name = "watchpoint" , .value = DBG_REASON_WATCHPOINT },
219 { .name = "watchpoint-and-breakpoint", .value = DBG_REASON_WPTANDBKPT },
220 { .name = "single-step" , .value = DBG_REASON_SINGLESTEP },
221 { .name = "target-not-halted" , .value = DBG_REASON_NOTHALTED },
222 { .name = "undefined" , .value = DBG_REASON_UNDEFINED },
223 { .name = NULL, .value = -1 },
226 static const Jim_Nvp nvp_target_endian[] = {
227 { .name = "big", .value = TARGET_BIG_ENDIAN },
228 { .name = "little", .value = TARGET_LITTLE_ENDIAN },
229 { .name = "be", .value = TARGET_BIG_ENDIAN },
230 { .name = "le", .value = TARGET_LITTLE_ENDIAN },
231 { .name = NULL, .value = -1 },
234 static const Jim_Nvp nvp_reset_modes[] = {
235 { .name = "unknown", .value = RESET_UNKNOWN },
236 { .name = "run" , .value = RESET_RUN },
237 { .name = "halt" , .value = RESET_HALT },
238 { .name = "init" , .value = RESET_INIT },
239 { .name = NULL , .value = -1 },
242 const char *debug_reason_name(struct target *t)
246 cp = Jim_Nvp_value2name_simple(nvp_target_debug_reason,
247 t->debug_reason)->name;
249 LOG_ERROR("Invalid debug reason: %d", (int)(t->debug_reason));
250 cp = "(*BUG*unknown*BUG*)";
256 target_state_name( struct target *t )
259 cp = Jim_Nvp_value2name_simple(nvp_target_state, t->state)->name;
261 LOG_ERROR("Invalid target state: %d", (int)(t->state));
262 cp = "(*BUG*unknown*BUG*)";
267 /* determine the number of the new target */
268 static int new_target_number(void)
273 /* number is 0 based */
277 if (x < t->target_number) {
278 x = t->target_number;
285 /* read a uint32_t from a buffer in target memory endianness */
286 uint32_t target_buffer_get_u32(struct target *target, const uint8_t *buffer)
288 if (target->endianness == TARGET_LITTLE_ENDIAN)
289 return le_to_h_u32(buffer);
291 return be_to_h_u32(buffer);
294 /* read a uint24_t from a buffer in target memory endianness */
295 uint32_t target_buffer_get_u24(struct target *target, const uint8_t *buffer)
297 if (target->endianness == TARGET_LITTLE_ENDIAN)
298 return le_to_h_u24(buffer);
300 return be_to_h_u24(buffer);
303 /* read a uint16_t from a buffer in target memory endianness */
304 uint16_t target_buffer_get_u16(struct target *target, const uint8_t *buffer)
306 if (target->endianness == TARGET_LITTLE_ENDIAN)
307 return le_to_h_u16(buffer);
309 return be_to_h_u16(buffer);
312 /* read a uint8_t from a buffer in target memory endianness */
313 static uint8_t target_buffer_get_u8(struct target *target, const uint8_t *buffer)
315 return *buffer & 0x0ff;
318 /* write a uint32_t to a buffer in target memory endianness */
319 void target_buffer_set_u32(struct target *target, uint8_t *buffer, uint32_t value)
321 if (target->endianness == TARGET_LITTLE_ENDIAN)
322 h_u32_to_le(buffer, value);
324 h_u32_to_be(buffer, value);
327 /* write a uint24_t to a buffer in target memory endianness */
328 void target_buffer_set_u24(struct target *target, uint8_t *buffer, uint32_t value)
330 if (target->endianness == TARGET_LITTLE_ENDIAN)
331 h_u24_to_le(buffer, value);
333 h_u24_to_be(buffer, value);
336 /* write a uint16_t to a buffer in target memory endianness */
337 void target_buffer_set_u16(struct target *target, uint8_t *buffer, uint16_t value)
339 if (target->endianness == TARGET_LITTLE_ENDIAN)
340 h_u16_to_le(buffer, value);
342 h_u16_to_be(buffer, value);
345 /* write a uint8_t to a buffer in target memory endianness */
346 static void target_buffer_set_u8(struct target *target, uint8_t *buffer, uint8_t value)
351 /* write a uint32_t array to a buffer in target memory endianness */
352 void target_buffer_get_u32_array(struct target *target, const uint8_t *buffer, uint32_t count, uint32_t *dstbuf)
355 for(i = 0; i < count; i ++)
356 dstbuf[i] = target_buffer_get_u32(target,&buffer[i*4]);
359 /* write a uint16_t array to a buffer in target memory endianness */
360 void target_buffer_get_u16_array(struct target *target, const uint8_t *buffer, uint32_t count, uint16_t *dstbuf)
363 for(i = 0; i < count; i ++)
364 dstbuf[i] = target_buffer_get_u16(target,&buffer[i*2]);
367 /* write a uint32_t array to a buffer in target memory endianness */
368 void target_buffer_set_u32_array(struct target *target, uint8_t *buffer, uint32_t count, uint32_t *srcbuf)
371 for(i = 0; i < count; i ++)
372 target_buffer_set_u32(target,&buffer[i*4],srcbuf[i]);
375 /* write a uint16_t array to a buffer in target memory endianness */
376 void target_buffer_set_u16_array(struct target *target, uint8_t *buffer, uint32_t count, uint16_t *srcbuf)
379 for(i = 0; i < count; i ++)
380 target_buffer_set_u16(target,&buffer[i*2],srcbuf[i]);
383 /* return a pointer to a configured target; id is name or number */
384 struct target *get_target(const char *id)
386 struct target *target;
388 /* try as tcltarget name */
389 for (target = all_targets; target; target = target->next) {
390 if (target->cmd_name == NULL)
392 if (strcmp(id, target->cmd_name) == 0)
396 /* It's OK to remove this fallback sometime after August 2010 or so */
398 /* no match, try as number */
400 if (parse_uint(id, &num) != ERROR_OK)
403 for (target = all_targets; target; target = target->next) {
404 if (target->target_number == (int)num) {
405 LOG_WARNING("use '%s' as target identifier, not '%u'",
406 target->cmd_name, num);
414 /* returns a pointer to the n-th configured target */
415 static struct target *get_target_by_num(int num)
417 struct target *target = all_targets;
420 if (target->target_number == num) {
423 target = target->next;
429 struct target* get_current_target(struct command_context *cmd_ctx)
431 struct target *target = get_target_by_num(cmd_ctx->current_target);
435 LOG_ERROR("BUG: current_target out of bounds");
442 int target_poll(struct target *target)
446 /* We can't poll until after examine */
447 if (!target_was_examined(target))
449 /* Fail silently lest we pollute the log */
453 retval = target->type->poll(target);
454 if (retval != ERROR_OK)
457 if (target->halt_issued)
459 if (target->state == TARGET_HALTED)
461 target->halt_issued = false;
464 long long t = timeval_ms() - target->halt_issued_time;
467 target->halt_issued = false;
468 LOG_INFO("Halt timed out, wake up GDB.");
469 target_call_event_callbacks(target, TARGET_EVENT_GDB_HALT);
477 int target_halt(struct target *target)
480 /* We can't poll until after examine */
481 if (!target_was_examined(target))
483 LOG_ERROR("Target not examined yet");
487 retval = target->type->halt(target);
488 if (retval != ERROR_OK)
491 target->halt_issued = true;
492 target->halt_issued_time = timeval_ms();
498 * Make the target (re)start executing using its saved execution
499 * context (possibly with some modifications).
501 * @param target Which target should start executing.
502 * @param current True to use the target's saved program counter instead
503 * of the address parameter
504 * @param address Optionally used as the program counter.
505 * @param handle_breakpoints True iff breakpoints at the resumption PC
506 * should be skipped. (For example, maybe execution was stopped by
507 * such a breakpoint, in which case it would be counterprodutive to
509 * @param debug_execution False if all working areas allocated by OpenOCD
510 * should be released and/or restored to their original contents.
511 * (This would for example be true to run some downloaded "helper"
512 * algorithm code, which resides in one such working buffer and uses
513 * another for data storage.)
515 * @todo Resolve the ambiguity about what the "debug_execution" flag
516 * signifies. For example, Target implementations don't agree on how
517 * it relates to invalidation of the register cache, or to whether
518 * breakpoints and watchpoints should be enabled. (It would seem wrong
519 * to enable breakpoints when running downloaded "helper" algorithms
520 * (debug_execution true), since the breakpoints would be set to match
521 * target firmware being debugged, not the helper algorithm.... and
522 * enabling them could cause such helpers to malfunction (for example,
523 * by overwriting data with a breakpoint instruction. On the other
524 * hand the infrastructure for running such helpers might use this
525 * procedure but rely on hardware breakpoint to detect termination.)
527 int target_resume(struct target *target, int current, uint32_t address, int handle_breakpoints, int debug_execution)
531 /* We can't poll until after examine */
532 if (!target_was_examined(target))
534 LOG_ERROR("Target not examined yet");
538 /* note that resume *must* be asynchronous. The CPU can halt before
539 * we poll. The CPU can even halt at the current PC as a result of
540 * a software breakpoint being inserted by (a bug?) the application.
542 if ((retval = target->type->resume(target, current, address, handle_breakpoints, debug_execution)) != ERROR_OK)
548 static int target_process_reset(struct command_context *cmd_ctx, enum target_reset_mode reset_mode)
553 n = Jim_Nvp_value2name_simple(nvp_reset_modes, reset_mode);
554 if (n->name == NULL) {
555 LOG_ERROR("invalid reset mode");
559 /* disable polling during reset to make reset event scripts
560 * more predictable, i.e. dr/irscan & pathmove in events will
561 * not have JTAG operations injected into the middle of a sequence.
563 bool save_poll = jtag_poll_get_enabled();
565 jtag_poll_set_enabled(false);
567 sprintf(buf, "ocd_process_reset %s", n->name);
568 retval = Jim_Eval(cmd_ctx->interp, buf);
570 jtag_poll_set_enabled(save_poll);
572 if (retval != JIM_OK) {
573 Jim_MakeErrorMessage(cmd_ctx->interp);
574 command_print(NULL,"%s\n", Jim_GetString(Jim_GetResult(cmd_ctx->interp), NULL));
578 /* We want any events to be processed before the prompt */
579 retval = target_call_timer_callbacks_now();
581 struct target *target;
582 for (target = all_targets; target; target = target->next) {
583 target->type->check_reset(target);
589 static int identity_virt2phys(struct target *target,
590 uint32_t virtual, uint32_t *physical)
596 static int no_mmu(struct target *target, int *enabled)
602 static int default_examine(struct target *target)
604 target_set_examined(target);
608 /* no check by default */
609 static int default_check_reset(struct target *target)
614 int target_examine_one(struct target *target)
616 return target->type->examine(target);
619 static int jtag_enable_callback(enum jtag_event event, void *priv)
621 struct target *target = priv;
623 if (event != JTAG_TAP_EVENT_ENABLE || !target->tap->enabled)
626 jtag_unregister_event_callback(jtag_enable_callback, target);
627 return target_examine_one(target);
631 /* Targets that correctly implement init + examine, i.e.
632 * no communication with target during init:
636 int target_examine(void)
638 int retval = ERROR_OK;
639 struct target *target;
641 for (target = all_targets; target; target = target->next)
643 /* defer examination, but don't skip it */
644 if (!target->tap->enabled) {
645 jtag_register_event_callback(jtag_enable_callback,
649 if ((retval = target_examine_one(target)) != ERROR_OK)
654 const char *target_type_name(struct target *target)
656 return target->type->name;
659 static int target_write_memory_imp(struct target *target, uint32_t address, uint32_t size, uint32_t count, const uint8_t *buffer)
661 if (!target_was_examined(target))
663 LOG_ERROR("Target not examined yet");
666 return target->type->write_memory_imp(target, address, size, count, buffer);
669 static int target_read_memory_imp(struct target *target, uint32_t address, uint32_t size, uint32_t count, uint8_t *buffer)
671 if (!target_was_examined(target))
673 LOG_ERROR("Target not examined yet");
676 return target->type->read_memory_imp(target, address, size, count, buffer);
679 static int target_soft_reset_halt_imp(struct target *target)
681 if (!target_was_examined(target))
683 LOG_ERROR("Target not examined yet");
686 if (!target->type->soft_reset_halt_imp) {
687 LOG_ERROR("Target %s does not support soft_reset_halt",
688 target_name(target));
691 return target->type->soft_reset_halt_imp(target);
695 * Downloads a target-specific native code algorithm to the target,
696 * and executes it. * Note that some targets may need to set up, enable,
697 * and tear down a breakpoint (hard or * soft) to detect algorithm
698 * termination, while others may support lower overhead schemes where
699 * soft breakpoints embedded in the algorithm automatically terminate the
702 * @param target used to run the algorithm
703 * @param arch_info target-specific description of the algorithm.
705 int target_run_algorithm(struct target *target,
706 int num_mem_params, struct mem_param *mem_params,
707 int num_reg_params, struct reg_param *reg_param,
708 uint32_t entry_point, uint32_t exit_point,
709 int timeout_ms, void *arch_info)
711 int retval = ERROR_FAIL;
713 if (!target_was_examined(target))
715 LOG_ERROR("Target not examined yet");
718 if (!target->type->run_algorithm) {
719 LOG_ERROR("Target type '%s' does not support %s",
720 target_type_name(target), __func__);
724 target->running_alg = true;
725 retval = target->type->run_algorithm(target,
726 num_mem_params, mem_params,
727 num_reg_params, reg_param,
728 entry_point, exit_point, timeout_ms, arch_info);
729 target->running_alg = false;
736 int target_read_memory(struct target *target,
737 uint32_t address, uint32_t size, uint32_t count, uint8_t *buffer)
739 return target->type->read_memory(target, address, size, count, buffer);
742 static int target_read_phys_memory(struct target *target,
743 uint32_t address, uint32_t size, uint32_t count, uint8_t *buffer)
745 return target->type->read_phys_memory(target, address, size, count, buffer);
748 int target_write_memory(struct target *target,
749 uint32_t address, uint32_t size, uint32_t count, const uint8_t *buffer)
751 return target->type->write_memory(target, address, size, count, buffer);
754 static int target_write_phys_memory(struct target *target,
755 uint32_t address, uint32_t size, uint32_t count, const uint8_t *buffer)
757 return target->type->write_phys_memory(target, address, size, count, buffer);
760 int target_bulk_write_memory(struct target *target,
761 uint32_t address, uint32_t count, const uint8_t *buffer)
763 return target->type->bulk_write_memory(target, address, count, buffer);
766 int target_add_breakpoint(struct target *target,
767 struct breakpoint *breakpoint)
769 if ((target->state != TARGET_HALTED)&&(breakpoint->type!=BKPT_HARD)) {
770 LOG_WARNING("target %s is not halted", target->cmd_name);
771 return ERROR_TARGET_NOT_HALTED;
773 return target->type->add_breakpoint(target, breakpoint);
775 int target_remove_breakpoint(struct target *target,
776 struct breakpoint *breakpoint)
778 return target->type->remove_breakpoint(target, breakpoint);
781 int target_add_watchpoint(struct target *target,
782 struct watchpoint *watchpoint)
784 if (target->state != TARGET_HALTED) {
785 LOG_WARNING("target %s is not halted", target->cmd_name);
786 return ERROR_TARGET_NOT_HALTED;
788 return target->type->add_watchpoint(target, watchpoint);
790 int target_remove_watchpoint(struct target *target,
791 struct watchpoint *watchpoint)
793 return target->type->remove_watchpoint(target, watchpoint);
796 int target_get_gdb_reg_list(struct target *target,
797 struct reg **reg_list[], int *reg_list_size)
799 return target->type->get_gdb_reg_list(target, reg_list, reg_list_size);
801 int target_step(struct target *target,
802 int current, uint32_t address, int handle_breakpoints)
804 return target->type->step(target, current, address, handle_breakpoints);
809 * Reset the @c examined flag for the given target.
810 * Pure paranoia -- targets are zeroed on allocation.
812 static void target_reset_examined(struct target *target)
814 target->examined = false;
818 err_read_phys_memory(struct target *target, uint32_t address,
819 uint32_t size, uint32_t count, uint8_t *buffer)
821 LOG_ERROR("Not implemented: %s", __func__);
826 err_write_phys_memory(struct target *target, uint32_t address,
827 uint32_t size, uint32_t count, const uint8_t *buffer)
829 LOG_ERROR("Not implemented: %s", __func__);
833 static int handle_target(void *priv);
835 static int target_init_one(struct command_context *cmd_ctx,
836 struct target *target)
838 target_reset_examined(target);
840 struct target_type *type = target->type;
841 if (type->examine == NULL)
842 type->examine = default_examine;
844 if (type->check_reset== NULL)
845 type->check_reset = default_check_reset;
847 int retval = type->init_target(cmd_ctx, target);
848 if (ERROR_OK != retval)
850 LOG_ERROR("target '%s' init failed", target_name(target));
855 * @todo get rid of those *memory_imp() methods, now that all
856 * callers are using target_*_memory() accessors ... and make
857 * sure the "physical" paths handle the same issues.
859 /* a non-invasive way(in terms of patches) to add some code that
860 * runs before the type->write/read_memory implementation
862 type->write_memory_imp = target->type->write_memory;
863 type->write_memory = target_write_memory_imp;
865 type->read_memory_imp = target->type->read_memory;
866 type->read_memory = target_read_memory_imp;
868 type->soft_reset_halt_imp = target->type->soft_reset_halt;
869 type->soft_reset_halt = target_soft_reset_halt_imp;
871 /* Sanity-check MMU support ... stub in what we must, to help
872 * implement it in stages, but warn if we need to do so.
876 if (type->write_phys_memory == NULL)
878 LOG_ERROR("type '%s' is missing write_phys_memory",
880 type->write_phys_memory = err_write_phys_memory;
882 if (type->read_phys_memory == NULL)
884 LOG_ERROR("type '%s' is missing read_phys_memory",
886 type->read_phys_memory = err_read_phys_memory;
888 if (type->virt2phys == NULL)
890 LOG_ERROR("type '%s' is missing virt2phys", type->name);
891 type->virt2phys = identity_virt2phys;
896 /* Make sure no-MMU targets all behave the same: make no
897 * distinction between physical and virtual addresses, and
898 * ensure that virt2phys() is always an identity mapping.
900 if (type->write_phys_memory || type->read_phys_memory
903 LOG_WARNING("type '%s' has bad MMU hooks", type->name);
907 type->write_phys_memory = type->write_memory;
908 type->read_phys_memory = type->read_memory;
909 type->virt2phys = identity_virt2phys;
912 if (target->type->read_buffer == NULL)
913 target->type->read_buffer = target_read_buffer_default;
915 if (target->type->write_buffer == NULL)
916 target->type->write_buffer = target_write_buffer_default;
921 static int target_init(struct command_context *cmd_ctx)
923 struct target *target;
926 for (target = all_targets; target; target = target->next)
928 retval = target_init_one(cmd_ctx, target);
929 if (ERROR_OK != retval)
936 retval = target_register_user_commands(cmd_ctx);
937 if (ERROR_OK != retval)
940 retval = target_register_timer_callback(&handle_target,
941 polling_interval, 1, cmd_ctx->interp);
942 if (ERROR_OK != retval)
948 COMMAND_HANDLER(handle_target_init_command)
951 return ERROR_COMMAND_SYNTAX_ERROR;
953 static bool target_initialized = false;
954 if (target_initialized)
956 LOG_INFO("'target init' has already been called");
959 target_initialized = true;
961 LOG_DEBUG("Initializing targets...");
962 return target_init(CMD_CTX);
965 int target_register_event_callback(int (*callback)(struct target *target, enum target_event event, void *priv), void *priv)
967 struct target_event_callback **callbacks_p = &target_event_callbacks;
969 if (callback == NULL)
971 return ERROR_INVALID_ARGUMENTS;
976 while ((*callbacks_p)->next)
977 callbacks_p = &((*callbacks_p)->next);
978 callbacks_p = &((*callbacks_p)->next);
981 (*callbacks_p) = malloc(sizeof(struct target_event_callback));
982 (*callbacks_p)->callback = callback;
983 (*callbacks_p)->priv = priv;
984 (*callbacks_p)->next = NULL;
989 int target_register_timer_callback(int (*callback)(void *priv), int time_ms, int periodic, void *priv)
991 struct target_timer_callback **callbacks_p = &target_timer_callbacks;
994 if (callback == NULL)
996 return ERROR_INVALID_ARGUMENTS;
1001 while ((*callbacks_p)->next)
1002 callbacks_p = &((*callbacks_p)->next);
1003 callbacks_p = &((*callbacks_p)->next);
1006 (*callbacks_p) = malloc(sizeof(struct target_timer_callback));
1007 (*callbacks_p)->callback = callback;
1008 (*callbacks_p)->periodic = periodic;
1009 (*callbacks_p)->time_ms = time_ms;
1011 gettimeofday(&now, NULL);
1012 (*callbacks_p)->when.tv_usec = now.tv_usec + (time_ms % 1000) * 1000;
1013 time_ms -= (time_ms % 1000);
1014 (*callbacks_p)->when.tv_sec = now.tv_sec + (time_ms / 1000);
1015 if ((*callbacks_p)->when.tv_usec > 1000000)
1017 (*callbacks_p)->when.tv_usec = (*callbacks_p)->when.tv_usec - 1000000;
1018 (*callbacks_p)->when.tv_sec += 1;
1021 (*callbacks_p)->priv = priv;
1022 (*callbacks_p)->next = NULL;
1027 int target_unregister_event_callback(int (*callback)(struct target *target, enum target_event event, void *priv), void *priv)
1029 struct target_event_callback **p = &target_event_callbacks;
1030 struct target_event_callback *c = target_event_callbacks;
1032 if (callback == NULL)
1034 return ERROR_INVALID_ARGUMENTS;
1039 struct target_event_callback *next = c->next;
1040 if ((c->callback == callback) && (c->priv == priv))
1054 static int target_unregister_timer_callback(int (*callback)(void *priv), void *priv)
1056 struct target_timer_callback **p = &target_timer_callbacks;
1057 struct target_timer_callback *c = target_timer_callbacks;
1059 if (callback == NULL)
1061 return ERROR_INVALID_ARGUMENTS;
1066 struct target_timer_callback *next = c->next;
1067 if ((c->callback == callback) && (c->priv == priv))
1081 int target_call_event_callbacks(struct target *target, enum target_event event)
1083 struct target_event_callback *callback = target_event_callbacks;
1084 struct target_event_callback *next_callback;
1086 if (event == TARGET_EVENT_HALTED)
1088 /* execute early halted first */
1089 target_call_event_callbacks(target, TARGET_EVENT_GDB_HALT);
1092 LOG_DEBUG("target event %i (%s)",
1094 Jim_Nvp_value2name_simple(nvp_target_event, event)->name);
1096 target_handle_event(target, event);
1100 next_callback = callback->next;
1101 callback->callback(target, event, callback->priv);
1102 callback = next_callback;
1108 static int target_timer_callback_periodic_restart(
1109 struct target_timer_callback *cb, struct timeval *now)
1111 int time_ms = cb->time_ms;
1112 cb->when.tv_usec = now->tv_usec + (time_ms % 1000) * 1000;
1113 time_ms -= (time_ms % 1000);
1114 cb->when.tv_sec = now->tv_sec + time_ms / 1000;
1115 if (cb->when.tv_usec > 1000000)
1117 cb->when.tv_usec = cb->when.tv_usec - 1000000;
1118 cb->when.tv_sec += 1;
1123 static int target_call_timer_callback(struct target_timer_callback *cb,
1124 struct timeval *now)
1126 cb->callback(cb->priv);
1129 return target_timer_callback_periodic_restart(cb, now);
1131 return target_unregister_timer_callback(cb->callback, cb->priv);
1134 static int target_call_timer_callbacks_check_time(int checktime)
1139 gettimeofday(&now, NULL);
1141 struct target_timer_callback *callback = target_timer_callbacks;
1144 // cleaning up may unregister and free this callback
1145 struct target_timer_callback *next_callback = callback->next;
1147 bool call_it = callback->callback &&
1148 ((!checktime && callback->periodic) ||
1149 now.tv_sec > callback->when.tv_sec ||
1150 (now.tv_sec == callback->when.tv_sec &&
1151 now.tv_usec >= callback->when.tv_usec));
1155 int retval = target_call_timer_callback(callback, &now);
1156 if (retval != ERROR_OK)
1160 callback = next_callback;
1166 int target_call_timer_callbacks(void)
1168 return target_call_timer_callbacks_check_time(1);
1171 /* invoke periodic callbacks immediately */
1172 int target_call_timer_callbacks_now(void)
1174 return target_call_timer_callbacks_check_time(0);
1177 int target_alloc_working_area_try(struct target *target, uint32_t size, struct working_area **area)
1179 struct working_area *c = target->working_areas;
1180 struct working_area *new_wa = NULL;
1182 /* Reevaluate working area address based on MMU state*/
1183 if (target->working_areas == NULL)
1188 retval = target->type->mmu(target, &enabled);
1189 if (retval != ERROR_OK)
1195 if (target->working_area_phys_spec) {
1196 LOG_DEBUG("MMU disabled, using physical "
1197 "address for working memory 0x%08x",
1198 (unsigned)target->working_area_phys);
1199 target->working_area = target->working_area_phys;
1201 LOG_ERROR("No working memory available. "
1202 "Specify -work-area-phys to target.");
1203 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
1206 if (target->working_area_virt_spec) {
1207 LOG_DEBUG("MMU enabled, using virtual "
1208 "address for working memory 0x%08x",
1209 (unsigned)target->working_area_virt);
1210 target->working_area = target->working_area_virt;
1212 LOG_ERROR("No working memory available. "
1213 "Specify -work-area-virt to target.");
1214 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
1219 /* only allocate multiples of 4 byte */
1222 LOG_ERROR("BUG: code tried to allocate unaligned number of bytes (0x%08x), padding", ((unsigned)(size)));
1223 size = (size + 3) & (~3);
1226 /* see if there's already a matching working area */
1229 if ((c->free) && (c->size == size))
1237 /* if not, allocate a new one */
1240 struct working_area **p = &target->working_areas;
1241 uint32_t first_free = target->working_area;
1242 uint32_t free_size = target->working_area_size;
1244 c = target->working_areas;
1247 first_free += c->size;
1248 free_size -= c->size;
1253 if (free_size < size)
1255 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
1258 LOG_DEBUG("allocated new working area at address 0x%08x", (unsigned)first_free);
1260 new_wa = malloc(sizeof(struct working_area));
1261 new_wa->next = NULL;
1262 new_wa->size = size;
1263 new_wa->address = first_free;
1265 if (target->backup_working_area)
1268 new_wa->backup = malloc(new_wa->size);
1269 if ((retval = target_read_memory(target, new_wa->address, 4, new_wa->size / 4, new_wa->backup)) != ERROR_OK)
1271 free(new_wa->backup);
1278 new_wa->backup = NULL;
1281 /* put new entry in list */
1285 /* mark as used, and return the new (reused) area */
1286 new_wa->free = false;
1290 new_wa->user = area;
1295 int target_alloc_working_area(struct target *target, uint32_t size, struct working_area **area)
1299 retval = target_alloc_working_area_try(target, size, area);
1300 if (retval == ERROR_TARGET_RESOURCE_NOT_AVAILABLE)
1302 LOG_WARNING("not enough working area available(requested %u)", (unsigned)(size));
1308 static int target_free_working_area_restore(struct target *target, struct working_area *area, int restore)
1313 if (restore && target->backup_working_area)
1316 if ((retval = target_write_memory(target, area->address, 4, area->size / 4, area->backup)) != ERROR_OK)
1322 /* mark user pointer invalid */
1329 int target_free_working_area(struct target *target, struct working_area *area)
1331 return target_free_working_area_restore(target, area, 1);
1334 /* free resources and restore memory, if restoring memory fails,
1335 * free up resources anyway
1337 static void target_free_all_working_areas_restore(struct target *target, int restore)
1339 struct working_area *c = target->working_areas;
1343 struct working_area *next = c->next;
1344 target_free_working_area_restore(target, c, restore);
1354 target->working_areas = NULL;
1357 void target_free_all_working_areas(struct target *target)
1359 target_free_all_working_areas_restore(target, 1);
1362 int target_arch_state(struct target *target)
1367 LOG_USER("No target has been configured");
1371 LOG_USER("target state: %s", target_state_name( target ));
1373 if (target->state != TARGET_HALTED)
1376 retval = target->type->arch_state(target);
1380 /* Single aligned words are guaranteed to use 16 or 32 bit access
1381 * mode respectively, otherwise data is handled as quickly as
1384 int target_write_buffer(struct target *target, uint32_t address, uint32_t size, const uint8_t *buffer)
1386 LOG_DEBUG("writing buffer of %i byte at 0x%8.8x",
1387 (int)size, (unsigned)address);
1389 if (!target_was_examined(target))
1391 LOG_ERROR("Target not examined yet");
1399 if ((address + size - 1) < address)
1401 /* GDB can request this when e.g. PC is 0xfffffffc*/
1402 LOG_ERROR("address + size wrapped(0x%08x, 0x%08x)",
1408 return target->type->write_buffer(target, address, size, buffer);
1411 static int target_write_buffer_default(struct target *target, uint32_t address, uint32_t size, const uint8_t *buffer)
1413 int retval = ERROR_OK;
1415 if (((address % 2) == 0) && (size == 2))
1417 return target_write_memory(target, address, 2, 1, buffer);
1420 /* handle unaligned head bytes */
1423 uint32_t unaligned = 4 - (address % 4);
1425 if (unaligned > size)
1428 if ((retval = target_write_memory(target, address, 1, unaligned, buffer)) != ERROR_OK)
1431 buffer += unaligned;
1432 address += unaligned;
1436 /* handle aligned words */
1439 int aligned = size - (size % 4);
1441 /* use bulk writes above a certain limit. This may have to be changed */
1444 if ((retval = target->type->bulk_write_memory(target, address, aligned / 4, buffer)) != ERROR_OK)
1449 if ((retval = target_write_memory(target, address, 4, aligned / 4, buffer)) != ERROR_OK)
1458 /* handle tail writes of less than 4 bytes */
1461 if ((retval = target_write_memory(target, address, 1, size, buffer)) != ERROR_OK)
1468 /* Single aligned words are guaranteed to use 16 or 32 bit access
1469 * mode respectively, otherwise data is handled as quickly as
1472 int target_read_buffer(struct target *target, uint32_t address, uint32_t size, uint8_t *buffer)
1474 LOG_DEBUG("reading buffer of %i byte at 0x%8.8x",
1475 (int)size, (unsigned)address);
1477 if (!target_was_examined(target))
1479 LOG_ERROR("Target not examined yet");
1487 if ((address + size - 1) < address)
1489 /* GDB can request this when e.g. PC is 0xfffffffc*/
1490 LOG_ERROR("address + size wrapped(0x%08" PRIx32 ", 0x%08" PRIx32 ")",
1496 return target->type->read_buffer(target, address, size, buffer);
1499 static int target_read_buffer_default(struct target *target, uint32_t address, uint32_t size, uint8_t *buffer)
1501 int retval = ERROR_OK;
1503 if (((address % 2) == 0) && (size == 2))
1505 return target_read_memory(target, address, 2, 1, buffer);
1508 /* handle unaligned head bytes */
1511 uint32_t unaligned = 4 - (address % 4);
1513 if (unaligned > size)
1516 if ((retval = target_read_memory(target, address, 1, unaligned, buffer)) != ERROR_OK)
1519 buffer += unaligned;
1520 address += unaligned;
1524 /* handle aligned words */
1527 int aligned = size - (size % 4);
1529 if ((retval = target_read_memory(target, address, 4, aligned / 4, buffer)) != ERROR_OK)
1537 /*prevent byte access when possible (avoid AHB access limitations in some cases)*/
1540 int aligned = size - (size%2);
1541 retval = target_read_memory(target, address, 2, aligned / 2, buffer);
1542 if (retval != ERROR_OK)
1549 /* handle tail writes of less than 4 bytes */
1552 if ((retval = target_read_memory(target, address, 1, size, buffer)) != ERROR_OK)
1559 int target_checksum_memory(struct target *target, uint32_t address, uint32_t size, uint32_t* crc)
1564 uint32_t checksum = 0;
1565 if (!target_was_examined(target))
1567 LOG_ERROR("Target not examined yet");
1571 if ((retval = target->type->checksum_memory(target, address,
1572 size, &checksum)) != ERROR_OK)
1574 buffer = malloc(size);
1577 LOG_ERROR("error allocating buffer for section (%d bytes)", (int)size);
1578 return ERROR_INVALID_ARGUMENTS;
1580 retval = target_read_buffer(target, address, size, buffer);
1581 if (retval != ERROR_OK)
1587 /* convert to target endianness */
1588 for (i = 0; i < (size/sizeof(uint32_t)); i++)
1590 uint32_t target_data;
1591 target_data = target_buffer_get_u32(target, &buffer[i*sizeof(uint32_t)]);
1592 target_buffer_set_u32(target, &buffer[i*sizeof(uint32_t)], target_data);
1595 retval = image_calculate_checksum(buffer, size, &checksum);
1604 int target_blank_check_memory(struct target *target, uint32_t address, uint32_t size, uint32_t* blank)
1607 if (!target_was_examined(target))
1609 LOG_ERROR("Target not examined yet");
1613 if (target->type->blank_check_memory == 0)
1614 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
1616 retval = target->type->blank_check_memory(target, address, size, blank);
1621 int target_read_u32(struct target *target, uint32_t address, uint32_t *value)
1623 uint8_t value_buf[4];
1624 if (!target_was_examined(target))
1626 LOG_ERROR("Target not examined yet");
1630 int retval = target_read_memory(target, address, 4, 1, value_buf);
1632 if (retval == ERROR_OK)
1634 *value = target_buffer_get_u32(target, value_buf);
1635 LOG_DEBUG("address: 0x%8.8" PRIx32 ", value: 0x%8.8" PRIx32 "",
1642 LOG_DEBUG("address: 0x%8.8" PRIx32 " failed",
1649 int target_read_u16(struct target *target, uint32_t address, uint16_t *value)
1651 uint8_t value_buf[2];
1652 if (!target_was_examined(target))
1654 LOG_ERROR("Target not examined yet");
1658 int retval = target_read_memory(target, address, 2, 1, value_buf);
1660 if (retval == ERROR_OK)
1662 *value = target_buffer_get_u16(target, value_buf);
1663 LOG_DEBUG("address: 0x%8.8" PRIx32 ", value: 0x%4.4x",
1670 LOG_DEBUG("address: 0x%8.8" PRIx32 " failed",
1677 int target_read_u8(struct target *target, uint32_t address, uint8_t *value)
1679 int retval = target_read_memory(target, address, 1, 1, value);
1680 if (!target_was_examined(target))
1682 LOG_ERROR("Target not examined yet");
1686 if (retval == ERROR_OK)
1688 LOG_DEBUG("address: 0x%8.8" PRIx32 ", value: 0x%2.2x",
1695 LOG_DEBUG("address: 0x%8.8" PRIx32 " failed",
1702 int target_write_u32(struct target *target, uint32_t address, uint32_t value)
1705 uint8_t value_buf[4];
1706 if (!target_was_examined(target))
1708 LOG_ERROR("Target not examined yet");
1712 LOG_DEBUG("address: 0x%8.8" PRIx32 ", value: 0x%8.8" PRIx32 "",
1716 target_buffer_set_u32(target, value_buf, value);
1717 if ((retval = target_write_memory(target, address, 4, 1, value_buf)) != ERROR_OK)
1719 LOG_DEBUG("failed: %i", retval);
1725 int target_write_u16(struct target *target, uint32_t address, uint16_t value)
1728 uint8_t value_buf[2];
1729 if (!target_was_examined(target))
1731 LOG_ERROR("Target not examined yet");
1735 LOG_DEBUG("address: 0x%8.8" PRIx32 ", value: 0x%8.8x",
1739 target_buffer_set_u16(target, value_buf, value);
1740 if ((retval = target_write_memory(target, address, 2, 1, value_buf)) != ERROR_OK)
1742 LOG_DEBUG("failed: %i", retval);
1748 int target_write_u8(struct target *target, uint32_t address, uint8_t value)
1751 if (!target_was_examined(target))
1753 LOG_ERROR("Target not examined yet");
1757 LOG_DEBUG("address: 0x%8.8" PRIx32 ", value: 0x%2.2x",
1760 if ((retval = target_write_memory(target, address, 1, 1, &value)) != ERROR_OK)
1762 LOG_DEBUG("failed: %i", retval);
1768 COMMAND_HANDLER(handle_targets_command)
1770 struct target *target = all_targets;
1774 target = get_target(CMD_ARGV[0]);
1775 if (target == NULL) {
1776 command_print(CMD_CTX,"Target: %s is unknown, try one of:\n", CMD_ARGV[0]);
1779 if (!target->tap->enabled) {
1780 command_print(CMD_CTX,"Target: TAP %s is disabled, "
1781 "can't be the current target\n",
1782 target->tap->dotted_name);
1786 CMD_CTX->current_target = target->target_number;
1791 target = all_targets;
1792 command_print(CMD_CTX, " TargetName Type Endian TapName State ");
1793 command_print(CMD_CTX, "-- ------------------ ---------- ------ ------------------ ------------");
1799 if (target->tap->enabled)
1800 state = target_state_name( target );
1802 state = "tap-disabled";
1804 if (CMD_CTX->current_target == target->target_number)
1807 /* keep columns lined up to match the headers above */
1808 command_print(CMD_CTX, "%2d%c %-18s %-10s %-6s %-18s %s",
1809 target->target_number,
1811 target_name(target),
1812 target_type_name(target),
1813 Jim_Nvp_value2name_simple(nvp_target_endian,
1814 target->endianness)->name,
1815 target->tap->dotted_name,
1817 target = target->next;
1823 /* every 300ms we check for reset & powerdropout and issue a "reset halt" if so. */
1825 static int powerDropout;
1826 static int srstAsserted;
1828 static int runPowerRestore;
1829 static int runPowerDropout;
1830 static int runSrstAsserted;
1831 static int runSrstDeasserted;
1833 static int sense_handler(void)
1835 static int prevSrstAsserted = 0;
1836 static int prevPowerdropout = 0;
1839 if ((retval = jtag_power_dropout(&powerDropout)) != ERROR_OK)
1843 powerRestored = prevPowerdropout && !powerDropout;
1846 runPowerRestore = 1;
1849 long long current = timeval_ms();
1850 static long long lastPower = 0;
1851 int waitMore = lastPower + 2000 > current;
1852 if (powerDropout && !waitMore)
1854 runPowerDropout = 1;
1855 lastPower = current;
1858 if ((retval = jtag_srst_asserted(&srstAsserted)) != ERROR_OK)
1862 srstDeasserted = prevSrstAsserted && !srstAsserted;
1864 static long long lastSrst = 0;
1865 waitMore = lastSrst + 2000 > current;
1866 if (srstDeasserted && !waitMore)
1868 runSrstDeasserted = 1;
1872 if (!prevSrstAsserted && srstAsserted)
1874 runSrstAsserted = 1;
1877 prevSrstAsserted = srstAsserted;
1878 prevPowerdropout = powerDropout;
1880 if (srstDeasserted || powerRestored)
1882 /* Other than logging the event we can't do anything here.
1883 * Issuing a reset is a particularly bad idea as we might
1884 * be inside a reset already.
1891 static int backoff_times = 0;
1892 static int backoff_count = 0;
1894 /* process target state changes */
1895 static int handle_target(void *priv)
1897 Jim_Interp *interp = (Jim_Interp *)priv;
1898 int retval = ERROR_OK;
1900 if (!is_jtag_poll_safe())
1902 /* polling is disabled currently */
1906 /* we do not want to recurse here... */
1907 static int recursive = 0;
1912 /* danger! running these procedures can trigger srst assertions and power dropouts.
1913 * We need to avoid an infinite loop/recursion here and we do that by
1914 * clearing the flags after running these events.
1916 int did_something = 0;
1917 if (runSrstAsserted)
1919 LOG_INFO("srst asserted detected, running srst_asserted proc.");
1920 Jim_Eval(interp, "srst_asserted");
1923 if (runSrstDeasserted)
1925 Jim_Eval(interp, "srst_deasserted");
1928 if (runPowerDropout)
1930 LOG_INFO("Power dropout detected, running power_dropout proc.");
1931 Jim_Eval(interp, "power_dropout");
1934 if (runPowerRestore)
1936 Jim_Eval(interp, "power_restore");
1942 /* clear detect flags */
1946 /* clear action flags */
1948 runSrstAsserted = 0;
1949 runSrstDeasserted = 0;
1950 runPowerRestore = 0;
1951 runPowerDropout = 0;
1956 if (backoff_times > backoff_count)
1958 /* do not poll this time as we failed previously */
1964 /* Poll targets for state changes unless that's globally disabled.
1965 * Skip targets that are currently disabled.
1967 for (struct target *target = all_targets;
1968 is_jtag_poll_safe() && target;
1969 target = target->next)
1971 if (!target->tap->enabled)
1974 /* only poll target if we've got power and srst isn't asserted */
1975 if (!powerDropout && !srstAsserted)
1977 /* polling may fail silently until the target has been examined */
1978 if ((retval = target_poll(target)) != ERROR_OK)
1980 /* 100ms polling interval. Increase interval between polling up to 5000ms */
1981 if (backoff_times * polling_interval < 5000)
1986 LOG_USER("Polling target failed, GDB will be halted. Polling again in %dms", backoff_times * polling_interval);
1988 /* Tell GDB to halt the debugger. This allows the user to
1989 * run monitor commands to handle the situation.
1991 target_call_event_callbacks(target, TARGET_EVENT_GDB_HALT);
1994 /* Since we succeeded, we reset backoff count */
1995 if (backoff_times > 0)
1997 LOG_USER("Polling succeeded again");
2006 COMMAND_HANDLER(handle_reg_command)
2008 struct target *target;
2009 struct reg *reg = NULL;
2015 target = get_current_target(CMD_CTX);
2017 /* list all available registers for the current target */
2020 struct reg_cache *cache = target->reg_cache;
2027 command_print(CMD_CTX, "===== %s", cache->name);
2029 for (i = 0, reg = cache->reg_list;
2030 i < cache->num_regs;
2031 i++, reg++, count++)
2033 /* only print cached values if they are valid */
2035 value = buf_to_str(reg->value,
2037 command_print(CMD_CTX,
2038 "(%i) %s (/%" PRIu32 "): 0x%s%s",
2046 command_print(CMD_CTX, "(%i) %s (/%" PRIu32 ")",
2051 cache = cache->next;
2057 /* access a single register by its ordinal number */
2058 if ((CMD_ARGV[0][0] >= '0') && (CMD_ARGV[0][0] <= '9'))
2061 COMMAND_PARSE_NUMBER(uint, CMD_ARGV[0], num);
2063 struct reg_cache *cache = target->reg_cache;
2068 for (i = 0; i < cache->num_regs; i++)
2072 reg = &cache->reg_list[i];
2078 cache = cache->next;
2083 command_print(CMD_CTX, "%i is out of bounds, the current target has only %i registers (0 - %i)", num, count, count - 1);
2086 } else /* access a single register by its name */
2088 reg = register_get_by_name(target->reg_cache, CMD_ARGV[0], 1);
2092 command_print(CMD_CTX, "register %s not found in current target", CMD_ARGV[0]);
2097 /* display a register */
2098 if ((CMD_ARGC == 1) || ((CMD_ARGC == 2) && !((CMD_ARGV[1][0] >= '0') && (CMD_ARGV[1][0] <= '9'))))
2100 if ((CMD_ARGC == 2) && (strcmp(CMD_ARGV[1], "force") == 0))
2103 if (reg->valid == 0)
2105 reg->type->get(reg);
2107 value = buf_to_str(reg->value, reg->size, 16);
2108 command_print(CMD_CTX, "%s (/%i): 0x%s", reg->name, (int)(reg->size), value);
2113 /* set register value */
2116 uint8_t *buf = malloc(DIV_ROUND_UP(reg->size, 8));
2117 str_to_buf(CMD_ARGV[1], strlen(CMD_ARGV[1]), buf, reg->size, 0);
2119 reg->type->set(reg, buf);
2121 value = buf_to_str(reg->value, reg->size, 16);
2122 command_print(CMD_CTX, "%s (/%i): 0x%s", reg->name, (int)(reg->size), value);
2130 command_print(CMD_CTX, "usage: reg <#|name> [value]");
2135 COMMAND_HANDLER(handle_poll_command)
2137 int retval = ERROR_OK;
2138 struct target *target = get_current_target(CMD_CTX);
2142 command_print(CMD_CTX, "background polling: %s",
2143 jtag_poll_get_enabled() ? "on" : "off");
2144 command_print(CMD_CTX, "TAP: %s (%s)",
2145 target->tap->dotted_name,
2146 target->tap->enabled ? "enabled" : "disabled");
2147 if (!target->tap->enabled)
2149 if ((retval = target_poll(target)) != ERROR_OK)
2151 if ((retval = target_arch_state(target)) != ERROR_OK)
2154 else if (CMD_ARGC == 1)
2157 COMMAND_PARSE_ON_OFF(CMD_ARGV[0], enable);
2158 jtag_poll_set_enabled(enable);
2162 return ERROR_COMMAND_SYNTAX_ERROR;
2168 COMMAND_HANDLER(handle_wait_halt_command)
2171 return ERROR_COMMAND_SYNTAX_ERROR;
2176 int retval = parse_uint(CMD_ARGV[0], &ms);
2177 if (ERROR_OK != retval)
2179 command_print(CMD_CTX, "usage: %s [seconds]", CMD_NAME);
2180 return ERROR_COMMAND_SYNTAX_ERROR;
2182 // convert seconds (given) to milliseconds (needed)
2186 struct target *target = get_current_target(CMD_CTX);
2187 return target_wait_state(target, TARGET_HALTED, ms);
2190 /* wait for target state to change. The trick here is to have a low
2191 * latency for short waits and not to suck up all the CPU time
2194 * After 500ms, keep_alive() is invoked
2196 int target_wait_state(struct target *target, enum target_state state, int ms)
2199 long long then = 0, cur;
2204 if ((retval = target_poll(target)) != ERROR_OK)
2206 if (target->state == state)
2214 then = timeval_ms();
2215 LOG_DEBUG("waiting for target %s...",
2216 Jim_Nvp_value2name_simple(nvp_target_state,state)->name);
2224 if ((cur-then) > ms)
2226 LOG_ERROR("timed out while waiting for target %s",
2227 Jim_Nvp_value2name_simple(nvp_target_state,state)->name);
2235 COMMAND_HANDLER(handle_halt_command)
2239 struct target *target = get_current_target(CMD_CTX);
2240 int retval = target_halt(target);
2241 if (ERROR_OK != retval)
2246 unsigned wait_local;
2247 retval = parse_uint(CMD_ARGV[0], &wait_local);
2248 if (ERROR_OK != retval)
2249 return ERROR_COMMAND_SYNTAX_ERROR;
2254 return CALL_COMMAND_HANDLER(handle_wait_halt_command);
2257 COMMAND_HANDLER(handle_soft_reset_halt_command)
2259 struct target *target = get_current_target(CMD_CTX);
2261 LOG_USER("requesting target halt and executing a soft reset");
2263 target->type->soft_reset_halt(target);
2268 COMMAND_HANDLER(handle_reset_command)
2271 return ERROR_COMMAND_SYNTAX_ERROR;
2273 enum target_reset_mode reset_mode = RESET_RUN;
2277 n = Jim_Nvp_name2value_simple(nvp_reset_modes, CMD_ARGV[0]);
2278 if ((n->name == NULL) || (n->value == RESET_UNKNOWN)) {
2279 return ERROR_COMMAND_SYNTAX_ERROR;
2281 reset_mode = n->value;
2284 /* reset *all* targets */
2285 return target_process_reset(CMD_CTX, reset_mode);
2289 COMMAND_HANDLER(handle_resume_command)
2293 return ERROR_COMMAND_SYNTAX_ERROR;
2295 struct target *target = get_current_target(CMD_CTX);
2296 target_handle_event(target, TARGET_EVENT_OLD_pre_resume);
2298 /* with no CMD_ARGV, resume from current pc, addr = 0,
2299 * with one arguments, addr = CMD_ARGV[0],
2300 * handle breakpoints, not debugging */
2304 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], addr);
2308 return target_resume(target, current, addr, 1, 0);
2311 COMMAND_HANDLER(handle_step_command)
2314 return ERROR_COMMAND_SYNTAX_ERROR;
2318 /* with no CMD_ARGV, step from current pc, addr = 0,
2319 * with one argument addr = CMD_ARGV[0],
2320 * handle breakpoints, debugging */
2325 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], addr);
2329 struct target *target = get_current_target(CMD_CTX);
2331 return target->type->step(target, current_pc, addr, 1);
2334 static void handle_md_output(struct command_context *cmd_ctx,
2335 struct target *target, uint32_t address, unsigned size,
2336 unsigned count, const uint8_t *buffer)
2338 const unsigned line_bytecnt = 32;
2339 unsigned line_modulo = line_bytecnt / size;
2341 char output[line_bytecnt * 4 + 1];
2342 unsigned output_len = 0;
2344 const char *value_fmt;
2346 case 4: value_fmt = "%8.8x "; break;
2347 case 2: value_fmt = "%4.4x "; break;
2348 case 1: value_fmt = "%2.2x "; break;
2350 /* "can't happen", caller checked */
2351 LOG_ERROR("invalid memory read size: %u", size);
2355 for (unsigned i = 0; i < count; i++)
2357 if (i % line_modulo == 0)
2359 output_len += snprintf(output + output_len,
2360 sizeof(output) - output_len,
2362 (unsigned)(address + (i*size)));
2366 const uint8_t *value_ptr = buffer + i * size;
2368 case 4: value = target_buffer_get_u32(target, value_ptr); break;
2369 case 2: value = target_buffer_get_u16(target, value_ptr); break;
2370 case 1: value = *value_ptr;
2372 output_len += snprintf(output + output_len,
2373 sizeof(output) - output_len,
2376 if ((i % line_modulo == line_modulo - 1) || (i == count - 1))
2378 command_print(cmd_ctx, "%s", output);
2384 COMMAND_HANDLER(handle_md_command)
2387 return ERROR_COMMAND_SYNTAX_ERROR;
2390 switch (CMD_NAME[2]) {
2391 case 'w': size = 4; break;
2392 case 'h': size = 2; break;
2393 case 'b': size = 1; break;
2394 default: return ERROR_COMMAND_SYNTAX_ERROR;
2397 bool physical=strcmp(CMD_ARGV[0], "phys")==0;
2398 int (*fn)(struct target *target,
2399 uint32_t address, uint32_t size_value, uint32_t count, uint8_t *buffer);
2404 fn=target_read_phys_memory;
2407 fn=target_read_memory;
2409 if ((CMD_ARGC < 1) || (CMD_ARGC > 2))
2411 return ERROR_COMMAND_SYNTAX_ERROR;
2415 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], address);
2419 COMMAND_PARSE_NUMBER(uint, CMD_ARGV[1], count);
2421 uint8_t *buffer = calloc(count, size);
2423 struct target *target = get_current_target(CMD_CTX);
2424 int retval = fn(target, address, size, count, buffer);
2425 if (ERROR_OK == retval)
2426 handle_md_output(CMD_CTX, target, address, size, count, buffer);
2433 typedef int (*target_write_fn)(struct target *target,
2434 uint32_t address, uint32_t size, uint32_t count, const uint8_t *buffer);
2436 static int target_write_memory_fast(struct target *target,
2437 uint32_t address, uint32_t size, uint32_t count, const uint8_t *buffer)
2439 return target_write_buffer(target, address, size * count, buffer);
2442 static int target_fill_mem(struct target *target,
2451 /* We have to write in reasonably large chunks to be able
2452 * to fill large memory areas with any sane speed */
2453 const unsigned chunk_size = 16384;
2454 uint8_t *target_buf = malloc(chunk_size * data_size);
2455 if (target_buf == NULL)
2457 LOG_ERROR("Out of memory");
2461 for (unsigned i = 0; i < chunk_size; i ++)
2466 target_buffer_set_u32(target, target_buf + i*data_size, b);
2469 target_buffer_set_u16(target, target_buf + i*data_size, b);
2472 target_buffer_set_u8(target, target_buf + i*data_size, b);
2479 int retval = ERROR_OK;
2481 for (unsigned x = 0; x < c; x += chunk_size)
2485 if (current > chunk_size)
2487 current = chunk_size;
2489 retval = fn(target, address + x * data_size, data_size, current, target_buf);
2490 if (retval != ERROR_OK)
2494 /* avoid GDB timeouts */
2503 COMMAND_HANDLER(handle_mw_command)
2507 return ERROR_COMMAND_SYNTAX_ERROR;
2509 bool physical=strcmp(CMD_ARGV[0], "phys")==0;
2515 fn=target_write_phys_memory;
2518 fn = target_write_memory_fast;
2520 if ((CMD_ARGC < 2) || (CMD_ARGC > 3))
2521 return ERROR_COMMAND_SYNTAX_ERROR;
2524 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], address);
2527 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], value);
2531 COMMAND_PARSE_NUMBER(uint, CMD_ARGV[2], count);
2533 struct target *target = get_current_target(CMD_CTX);
2535 switch (CMD_NAME[2])
2547 return ERROR_COMMAND_SYNTAX_ERROR;
2550 return target_fill_mem(target, address, fn, wordsize, value, count);
2553 static COMMAND_HELPER(parse_load_image_command_CMD_ARGV, struct image *image,
2554 uint32_t *min_address, uint32_t *max_address)
2556 if (CMD_ARGC < 1 || CMD_ARGC > 5)
2557 return ERROR_COMMAND_SYNTAX_ERROR;
2559 /* a base address isn't always necessary,
2560 * default to 0x0 (i.e. don't relocate) */
2564 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], addr);
2565 image->base_address = addr;
2566 image->base_address_set = 1;
2569 image->base_address_set = 0;
2571 image->start_address_set = 0;
2575 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[3], *min_address);
2579 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[4], *max_address);
2580 // use size (given) to find max (required)
2581 *max_address += *min_address;
2584 if (*min_address > *max_address)
2585 return ERROR_COMMAND_SYNTAX_ERROR;
2590 COMMAND_HANDLER(handle_load_image_command)
2594 uint32_t image_size;
2595 uint32_t min_address = 0;
2596 uint32_t max_address = 0xffffffff;
2600 int retval = CALL_COMMAND_HANDLER(parse_load_image_command_CMD_ARGV,
2601 &image, &min_address, &max_address);
2602 if (ERROR_OK != retval)
2605 struct target *target = get_current_target(CMD_CTX);
2607 struct duration bench;
2608 duration_start(&bench);
2610 if (image_open(&image, CMD_ARGV[0], (CMD_ARGC >= 3) ? CMD_ARGV[2] : NULL) != ERROR_OK)
2617 for (i = 0; i < image.num_sections; i++)
2619 buffer = malloc(image.sections[i].size);
2622 command_print(CMD_CTX,
2623 "error allocating buffer for section (%d bytes)",
2624 (int)(image.sections[i].size));
2628 if ((retval = image_read_section(&image, i, 0x0, image.sections[i].size, buffer, &buf_cnt)) != ERROR_OK)
2634 uint32_t offset = 0;
2635 uint32_t length = buf_cnt;
2637 /* DANGER!!! beware of unsigned comparision here!!! */
2639 if ((image.sections[i].base_address + buf_cnt >= min_address)&&
2640 (image.sections[i].base_address < max_address))
2642 if (image.sections[i].base_address < min_address)
2644 /* clip addresses below */
2645 offset += min_address-image.sections[i].base_address;
2649 if (image.sections[i].base_address + buf_cnt > max_address)
2651 length -= (image.sections[i].base_address + buf_cnt)-max_address;
2654 if ((retval = target_write_buffer(target, image.sections[i].base_address + offset, length, buffer + offset)) != ERROR_OK)
2659 image_size += length;
2660 command_print(CMD_CTX, "%u bytes written at address 0x%8.8" PRIx32 "",
2661 (unsigned int)length,
2662 image.sections[i].base_address + offset);
2668 if ((ERROR_OK == retval) && (duration_measure(&bench) == ERROR_OK))
2670 command_print(CMD_CTX, "downloaded %" PRIu32 " bytes "
2671 "in %fs (%0.3f KiB/s)", image_size,
2672 duration_elapsed(&bench), duration_kbps(&bench, image_size));
2675 image_close(&image);
2681 COMMAND_HANDLER(handle_dump_image_command)
2683 struct fileio fileio;
2684 uint8_t buffer[560];
2685 int retval, retvaltemp;
2686 uint32_t address, size;
2687 struct duration bench;
2688 struct target *target = get_current_target(CMD_CTX);
2691 return ERROR_COMMAND_SYNTAX_ERROR;
2693 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], address);
2694 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[2], size);
2696 retval = fileio_open(&fileio, CMD_ARGV[0], FILEIO_WRITE, FILEIO_BINARY);
2697 if (retval != ERROR_OK)
2700 duration_start(&bench);
2705 size_t size_written;
2706 uint32_t this_run_size = (size > 560) ? 560 : size;
2707 retval = target_read_buffer(target, address, this_run_size, buffer);
2708 if (retval != ERROR_OK)
2713 retval = fileio_write(&fileio, this_run_size, buffer, &size_written);
2714 if (retval != ERROR_OK)
2719 size -= this_run_size;
2720 address += this_run_size;
2723 if ((ERROR_OK == retval) && (duration_measure(&bench) == ERROR_OK))
2726 retval = fileio_size(&fileio, &filesize);
2727 if (retval != ERROR_OK)
2729 command_print(CMD_CTX,
2730 "dumped %ld bytes in %fs (%0.3f KiB/s)", (long)filesize,
2731 duration_elapsed(&bench), duration_kbps(&bench, filesize));
2734 if ((retvaltemp = fileio_close(&fileio)) != ERROR_OK)
2740 static COMMAND_HELPER(handle_verify_image_command_internal, int verify)
2744 uint32_t image_size;
2747 uint32_t checksum = 0;
2748 uint32_t mem_checksum = 0;
2752 struct target *target = get_current_target(CMD_CTX);
2756 return ERROR_COMMAND_SYNTAX_ERROR;
2761 LOG_ERROR("no target selected");
2765 struct duration bench;
2766 duration_start(&bench);
2771 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], addr);
2772 image.base_address = addr;
2773 image.base_address_set = 1;
2777 image.base_address_set = 0;
2778 image.base_address = 0x0;
2781 image.start_address_set = 0;
2783 if ((retval = image_open(&image, CMD_ARGV[0], (CMD_ARGC == 3) ? CMD_ARGV[2] : NULL)) != ERROR_OK)
2791 for (i = 0; i < image.num_sections; i++)
2793 buffer = malloc(image.sections[i].size);
2796 command_print(CMD_CTX,
2797 "error allocating buffer for section (%d bytes)",
2798 (int)(image.sections[i].size));
2801 if ((retval = image_read_section(&image, i, 0x0, image.sections[i].size, buffer, &buf_cnt)) != ERROR_OK)
2809 /* calculate checksum of image */
2810 retval = image_calculate_checksum(buffer, buf_cnt, &checksum);
2811 if (retval != ERROR_OK)
2817 retval = target_checksum_memory(target, image.sections[i].base_address, buf_cnt, &mem_checksum);
2818 if (retval != ERROR_OK)
2824 if (checksum != mem_checksum)
2826 /* failed crc checksum, fall back to a binary compare */
2831 LOG_ERROR("checksum mismatch - attempting binary compare");
2834 data = (uint8_t*)malloc(buf_cnt);
2836 /* Can we use 32bit word accesses? */
2838 int count = buf_cnt;
2839 if ((count % 4) == 0)
2844 retval = target_read_memory(target, image.sections[i].base_address, size, count, data);
2845 if (retval == ERROR_OK)
2848 for (t = 0; t < buf_cnt; t++)
2850 if (data[t] != buffer[t])
2852 command_print(CMD_CTX,
2853 "diff %d address 0x%08x. Was 0x%02x instead of 0x%02x",
2855 (unsigned)(t + image.sections[i].base_address),
2860 command_print(CMD_CTX, "More than 128 errors, the rest are not printed.");
2873 command_print(CMD_CTX, "address 0x%08" PRIx32 " length 0x%08zx",
2874 image.sections[i].base_address,
2879 image_size += buf_cnt;
2883 command_print(CMD_CTX, "No more differences found.");
2888 retval = ERROR_FAIL;
2890 if ((ERROR_OK == retval) && (duration_measure(&bench) == ERROR_OK))
2892 command_print(CMD_CTX, "verified %" PRIu32 " bytes "
2893 "in %fs (%0.3f KiB/s)", image_size,
2894 duration_elapsed(&bench), duration_kbps(&bench, image_size));
2897 image_close(&image);
2902 COMMAND_HANDLER(handle_verify_image_command)
2904 return CALL_COMMAND_HANDLER(handle_verify_image_command_internal, 1);
2907 COMMAND_HANDLER(handle_test_image_command)
2909 return CALL_COMMAND_HANDLER(handle_verify_image_command_internal, 0);
2912 static int handle_bp_command_list(struct command_context *cmd_ctx)
2914 struct target *target = get_current_target(cmd_ctx);
2915 struct breakpoint *breakpoint = target->breakpoints;
2918 if (breakpoint->type == BKPT_SOFT)
2920 char* buf = buf_to_str(breakpoint->orig_instr,
2921 breakpoint->length, 16);
2922 command_print(cmd_ctx, "0x%8.8" PRIx32 ", 0x%x, %i, 0x%s",
2923 breakpoint->address,
2925 breakpoint->set, buf);
2930 command_print(cmd_ctx, "0x%8.8" PRIx32 ", 0x%x, %i",
2931 breakpoint->address,
2932 breakpoint->length, breakpoint->set);
2935 breakpoint = breakpoint->next;
2940 static int handle_bp_command_set(struct command_context *cmd_ctx,
2941 uint32_t addr, uint32_t length, int hw)
2943 struct target *target = get_current_target(cmd_ctx);
2944 int retval = breakpoint_add(target, addr, length, hw);
2945 if (ERROR_OK == retval)
2946 command_print(cmd_ctx, "breakpoint set at 0x%8.8" PRIx32 "", addr);
2948 LOG_ERROR("Failure setting breakpoint");
2952 COMMAND_HANDLER(handle_bp_command)
2955 return handle_bp_command_list(CMD_CTX);
2957 if (CMD_ARGC < 2 || CMD_ARGC > 3)
2959 command_print(CMD_CTX, "usage: bp <address> <length> ['hw']");
2960 return ERROR_COMMAND_SYNTAX_ERROR;
2964 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], addr);
2966 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], length);
2971 if (strcmp(CMD_ARGV[2], "hw") == 0)
2974 return ERROR_COMMAND_SYNTAX_ERROR;
2977 return handle_bp_command_set(CMD_CTX, addr, length, hw);
2980 COMMAND_HANDLER(handle_rbp_command)
2983 return ERROR_COMMAND_SYNTAX_ERROR;
2986 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], addr);
2988 struct target *target = get_current_target(CMD_CTX);
2989 breakpoint_remove(target, addr);
2994 COMMAND_HANDLER(handle_wp_command)
2996 struct target *target = get_current_target(CMD_CTX);
3000 struct watchpoint *watchpoint = target->watchpoints;
3004 command_print(CMD_CTX, "address: 0x%8.8" PRIx32
3005 ", len: 0x%8.8" PRIx32
3006 ", r/w/a: %i, value: 0x%8.8" PRIx32
3007 ", mask: 0x%8.8" PRIx32,
3008 watchpoint->address,
3010 (int)watchpoint->rw,
3013 watchpoint = watchpoint->next;
3018 enum watchpoint_rw type = WPT_ACCESS;
3020 uint32_t length = 0;
3021 uint32_t data_value = 0x0;
3022 uint32_t data_mask = 0xffffffff;
3027 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[4], data_mask);
3030 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[3], data_value);
3033 switch (CMD_ARGV[2][0])
3045 LOG_ERROR("invalid watchpoint mode ('%c')", CMD_ARGV[2][0]);
3046 return ERROR_COMMAND_SYNTAX_ERROR;
3050 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], length);
3051 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], addr);
3055 command_print(CMD_CTX, "usage: wp [address length "
3056 "[(r|w|a) [value [mask]]]]");
3057 return ERROR_COMMAND_SYNTAX_ERROR;
3060 int retval = watchpoint_add(target, addr, length, type,
3061 data_value, data_mask);
3062 if (ERROR_OK != retval)
3063 LOG_ERROR("Failure setting watchpoints");
3068 COMMAND_HANDLER(handle_rwp_command)
3071 return ERROR_COMMAND_SYNTAX_ERROR;
3074 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], addr);
3076 struct target *target = get_current_target(CMD_CTX);
3077 watchpoint_remove(target, addr);
3084 * Translate a virtual address to a physical address.
3086 * The low-level target implementation must have logged a detailed error
3087 * which is forwarded to telnet/GDB session.
3089 COMMAND_HANDLER(handle_virt2phys_command)
3092 return ERROR_COMMAND_SYNTAX_ERROR;
3095 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], va);
3098 struct target *target = get_current_target(CMD_CTX);
3099 int retval = target->type->virt2phys(target, va, &pa);
3100 if (retval == ERROR_OK)
3101 command_print(CMD_CTX, "Physical address 0x%08" PRIx32 "", pa);
3106 static void writeData(FILE *f, const void *data, size_t len)
3108 size_t written = fwrite(data, 1, len, f);
3110 LOG_ERROR("failed to write %zu bytes: %s", len, strerror(errno));
3113 static void writeLong(FILE *f, int l)
3116 for (i = 0; i < 4; i++)
3118 char c = (l >> (i*8))&0xff;
3119 writeData(f, &c, 1);
3124 static void writeString(FILE *f, char *s)
3126 writeData(f, s, strlen(s));
3129 /* Dump a gmon.out histogram file. */
3130 static void writeGmon(uint32_t *samples, uint32_t sampleNum, const char *filename)
3133 FILE *f = fopen(filename, "w");
3136 writeString(f, "gmon");
3137 writeLong(f, 0x00000001); /* Version */
3138 writeLong(f, 0); /* padding */
3139 writeLong(f, 0); /* padding */
3140 writeLong(f, 0); /* padding */
3142 uint8_t zero = 0; /* GMON_TAG_TIME_HIST */
3143 writeData(f, &zero, 1);
3145 /* figure out bucket size */
3146 uint32_t min = samples[0];
3147 uint32_t max = samples[0];
3148 for (i = 0; i < sampleNum; i++)
3150 if (min > samples[i])
3154 if (max < samples[i])
3160 int addressSpace = (max-min + 1);
3162 static const uint32_t maxBuckets = 16 * 1024; /* maximum buckets. */
3163 uint32_t length = addressSpace;
3164 if (length > maxBuckets)
3166 length = maxBuckets;
3168 int *buckets = malloc(sizeof(int)*length);
3169 if (buckets == NULL)
3174 memset(buckets, 0, sizeof(int)*length);
3175 for (i = 0; i < sampleNum;i++)
3177 uint32_t address = samples[i];
3178 long long a = address-min;
3179 long long b = length-1;
3180 long long c = addressSpace-1;
3181 int index_t = (a*b)/c; /* danger!!!! int32 overflows */
3185 /* append binary memory gmon.out &profile_hist_hdr ((char*)&profile_hist_hdr + sizeof(struct gmon_hist_hdr)) */
3186 writeLong(f, min); /* low_pc */
3187 writeLong(f, max); /* high_pc */
3188 writeLong(f, length); /* # of samples */
3189 writeLong(f, 100); /* KLUDGE! We lie, ca. 100Hz best case. */
3190 writeString(f, "seconds");
3191 for (i = 0; i < (15-strlen("seconds")); i++)
3192 writeData(f, &zero, 1);
3193 writeString(f, "s");
3195 /*append binary memory gmon.out profile_hist_data (profile_hist_data + profile_hist_hdr.hist_size) */
3197 char *data = malloc(2*length);
3200 for (i = 0; i < length;i++)
3209 data[i*2 + 1]=(val >> 8)&0xff;
3212 writeData(f, data, length * 2);
3222 /* profiling samples the CPU PC as quickly as OpenOCD is able,
3223 * which will be used as a random sampling of PC */
3224 COMMAND_HANDLER(handle_profile_command)
3226 struct target *target = get_current_target(CMD_CTX);
3227 struct timeval timeout, now;
3229 gettimeofday(&timeout, NULL);
3232 return ERROR_COMMAND_SYNTAX_ERROR;
3235 COMMAND_PARSE_NUMBER(uint, CMD_ARGV[0], offset);
3237 timeval_add_time(&timeout, offset, 0);
3240 * @todo: Some cores let us sample the PC without the
3241 * annoying halt/resume step; for example, ARMv7 PCSR.
3242 * Provide a way to use that more efficient mechanism.
3245 command_print(CMD_CTX, "Starting profiling. Halting and resuming the target as often as we can...");
3247 static const int maxSample = 10000;
3248 uint32_t *samples = malloc(sizeof(uint32_t)*maxSample);
3249 if (samples == NULL)
3253 /* hopefully it is safe to cache! We want to stop/restart as quickly as possible. */
3254 struct reg *reg = register_get_by_name(target->reg_cache, "pc", 1);
3259 target_poll(target);
3260 if (target->state == TARGET_HALTED)
3262 uint32_t t=*((uint32_t *)reg->value);
3263 samples[numSamples++]=t;
3264 retval = target_resume(target, 1, 0, 0, 0); /* current pc, addr = 0, do not handle breakpoints, not debugging */
3265 target_poll(target);
3266 alive_sleep(10); /* sleep 10ms, i.e. <100 samples/second. */
3267 } else if (target->state == TARGET_RUNNING)
3269 /* We want to quickly sample the PC. */
3270 if ((retval = target_halt(target)) != ERROR_OK)
3277 command_print(CMD_CTX, "Target not halted or running");
3281 if (retval != ERROR_OK)
3286 gettimeofday(&now, NULL);
3287 if ((numSamples >= maxSample) || ((now.tv_sec >= timeout.tv_sec) && (now.tv_usec >= timeout.tv_usec)))
3289 command_print(CMD_CTX, "Profiling completed. %d samples.", numSamples);
3290 if ((retval = target_poll(target)) != ERROR_OK)
3295 if (target->state == TARGET_HALTED)
3297 target_resume(target, 1, 0, 0, 0); /* current pc, addr = 0, do not handle breakpoints, not debugging */
3299 if ((retval = target_poll(target)) != ERROR_OK)
3304 writeGmon(samples, numSamples, CMD_ARGV[1]);
3305 command_print(CMD_CTX, "Wrote %s", CMD_ARGV[1]);
3314 static int new_int_array_element(Jim_Interp * interp, const char *varname, int idx, uint32_t val)
3317 Jim_Obj *nameObjPtr, *valObjPtr;
3320 namebuf = alloc_printf("%s(%d)", varname, idx);
3324 nameObjPtr = Jim_NewStringObj(interp, namebuf, -1);
3325 valObjPtr = Jim_NewIntObj(interp, val);
3326 if (!nameObjPtr || !valObjPtr)
3332 Jim_IncrRefCount(nameObjPtr);
3333 Jim_IncrRefCount(valObjPtr);
3334 result = Jim_SetVariable(interp, nameObjPtr, valObjPtr);
3335 Jim_DecrRefCount(interp, nameObjPtr);
3336 Jim_DecrRefCount(interp, valObjPtr);
3338 /* printf("%s(%d) <= 0%08x\n", varname, idx, val); */
3342 static int jim_mem2array(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
3344 struct command_context *context;
3345 struct target *target;
3347 context = current_command_context(interp);
3348 assert (context != NULL);
3350 target = get_current_target(context);
3353 LOG_ERROR("mem2array: no current target");
3357 return target_mem2array(interp, target, argc-1, argv + 1);
3360 static int target_mem2array(Jim_Interp *interp, struct target *target, int argc, Jim_Obj *const *argv)
3368 const char *varname;
3372 /* argv[1] = name of array to receive the data
3373 * argv[2] = desired width
3374 * argv[3] = memory address
3375 * argv[4] = count of times to read
3378 Jim_WrongNumArgs(interp, 1, argv, "varname width addr nelems");
3381 varname = Jim_GetString(argv[0], &len);
3382 /* given "foo" get space for worse case "foo(%d)" .. add 20 */
3384 e = Jim_GetLong(interp, argv[1], &l);
3390 e = Jim_GetLong(interp, argv[2], &l);
3395 e = Jim_GetLong(interp, argv[3], &l);
3411 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3412 Jim_AppendStrings(interp, Jim_GetResult(interp), "Invalid width param, must be 8/16/32", NULL);
3416 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3417 Jim_AppendStrings(interp, Jim_GetResult(interp), "mem2array: zero width read?", NULL);
3420 if ((addr + (len * width)) < addr) {
3421 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3422 Jim_AppendStrings(interp, Jim_GetResult(interp), "mem2array: addr + len - wraps to zero?", NULL);
3425 /* absurd transfer size? */
3427 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3428 Jim_AppendStrings(interp, Jim_GetResult(interp), "mem2array: absurd > 64K item request", NULL);
3433 ((width == 2) && ((addr & 1) == 0)) ||
3434 ((width == 4) && ((addr & 3) == 0))) {
3438 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3439 sprintf(buf, "mem2array address: 0x%08" PRIx32 " is not aligned for %" PRId32 " byte reads",
3442 Jim_AppendStrings(interp, Jim_GetResult(interp), buf , NULL);
3451 size_t buffersize = 4096;
3452 uint8_t *buffer = malloc(buffersize);
3459 /* Slurp... in buffer size chunks */
3461 count = len; /* in objects.. */
3462 if (count > (buffersize/width)) {
3463 count = (buffersize/width);
3466 retval = target_read_memory(target, addr, width, count, buffer);
3467 if (retval != ERROR_OK) {
3469 LOG_ERROR("mem2array: Read @ 0x%08x, w=%d, cnt=%d, failed",
3473 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3474 Jim_AppendStrings(interp, Jim_GetResult(interp), "mem2array: cannot read memory", NULL);
3478 v = 0; /* shut up gcc */
3479 for (i = 0 ;i < count ;i++, n++) {
3482 v = target_buffer_get_u32(target, &buffer[i*width]);
3485 v = target_buffer_get_u16(target, &buffer[i*width]);
3488 v = buffer[i] & 0x0ff;
3491 new_int_array_element(interp, varname, n, v);
3499 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3504 static int get_int_array_element(Jim_Interp * interp, const char *varname, int idx, uint32_t *val)
3507 Jim_Obj *nameObjPtr, *valObjPtr;
3511 namebuf = alloc_printf("%s(%d)", varname, idx);
3515 nameObjPtr = Jim_NewStringObj(interp, namebuf, -1);
3522 Jim_IncrRefCount(nameObjPtr);
3523 valObjPtr = Jim_GetVariable(interp, nameObjPtr, JIM_ERRMSG);
3524 Jim_DecrRefCount(interp, nameObjPtr);
3526 if (valObjPtr == NULL)
3529 result = Jim_GetLong(interp, valObjPtr, &l);
3530 /* printf("%s(%d) => 0%08x\n", varname, idx, val); */
3535 static int jim_array2mem(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
3537 struct command_context *context;
3538 struct target *target;
3540 context = current_command_context(interp);
3541 assert (context != NULL);
3543 target = get_current_target(context);
3544 if (target == NULL) {
3545 LOG_ERROR("array2mem: no current target");
3549 return target_array2mem(interp,target, argc-1, argv + 1);
3552 static int target_array2mem(Jim_Interp *interp, struct target *target,
3553 int argc, Jim_Obj *const *argv)
3561 const char *varname;
3565 /* argv[1] = name of array to get the data
3566 * argv[2] = desired width
3567 * argv[3] = memory address
3568 * argv[4] = count to write
3571 Jim_WrongNumArgs(interp, 0, argv, "varname width addr nelems");
3574 varname = Jim_GetString(argv[0], &len);
3575 /* given "foo" get space for worse case "foo(%d)" .. add 20 */
3577 e = Jim_GetLong(interp, argv[1], &l);
3583 e = Jim_GetLong(interp, argv[2], &l);
3588 e = Jim_GetLong(interp, argv[3], &l);
3604 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3605 Jim_AppendStrings(interp, Jim_GetResult(interp), "Invalid width param, must be 8/16/32", NULL);
3609 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3610 Jim_AppendStrings(interp, Jim_GetResult(interp), "array2mem: zero width read?", NULL);
3613 if ((addr + (len * width)) < addr) {
3614 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3615 Jim_AppendStrings(interp, Jim_GetResult(interp), "array2mem: addr + len - wraps to zero?", NULL);
3618 /* absurd transfer size? */
3620 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3621 Jim_AppendStrings(interp, Jim_GetResult(interp), "array2mem: absurd > 64K item request", NULL);
3626 ((width == 2) && ((addr & 1) == 0)) ||
3627 ((width == 4) && ((addr & 3) == 0))) {
3631 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3632 sprintf(buf, "array2mem address: 0x%08x is not aligned for %d byte reads",
3635 Jim_AppendStrings(interp, Jim_GetResult(interp), buf , NULL);
3646 size_t buffersize = 4096;
3647 uint8_t *buffer = malloc(buffersize);
3652 /* Slurp... in buffer size chunks */
3654 count = len; /* in objects.. */
3655 if (count > (buffersize/width)) {
3656 count = (buffersize/width);
3659 v = 0; /* shut up gcc */
3660 for (i = 0 ;i < count ;i++, n++) {
3661 get_int_array_element(interp, varname, n, &v);
3664 target_buffer_set_u32(target, &buffer[i*width], v);
3667 target_buffer_set_u16(target, &buffer[i*width], v);
3670 buffer[i] = v & 0x0ff;
3676 retval = target_write_memory(target, addr, width, count, buffer);
3677 if (retval != ERROR_OK) {
3679 LOG_ERROR("array2mem: Write @ 0x%08x, w=%d, cnt=%d, failed",
3683 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3684 Jim_AppendStrings(interp, Jim_GetResult(interp), "array2mem: cannot read memory", NULL);
3692 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3697 /* FIX? should we propagate errors here rather than printing them
3700 void target_handle_event(struct target *target, enum target_event e)
3702 struct target_event_action *teap;
3704 for (teap = target->event_action; teap != NULL; teap = teap->next) {
3705 if (teap->event == e) {
3706 LOG_DEBUG("target: (%d) %s (%s) event: %d (%s) action: %s",
3707 target->target_number,
3708 target_name(target),
3709 target_type_name(target),
3711 Jim_Nvp_value2name_simple(nvp_target_event, e)->name,
3712 Jim_GetString(teap->body, NULL));
3713 if (Jim_EvalObj(teap->interp, teap->body) != JIM_OK)
3715 Jim_MakeErrorMessage(teap->interp);
3716 command_print(NULL,"%s\n", Jim_GetString(Jim_GetResult(teap->interp), NULL));
3723 * Returns true only if the target has a handler for the specified event.
3725 bool target_has_event_action(struct target *target, enum target_event event)
3727 struct target_event_action *teap;
3729 for (teap = target->event_action; teap != NULL; teap = teap->next) {
3730 if (teap->event == event)
3736 enum target_cfg_param {
3739 TCFG_WORK_AREA_VIRT,
3740 TCFG_WORK_AREA_PHYS,
3741 TCFG_WORK_AREA_SIZE,
3742 TCFG_WORK_AREA_BACKUP,
3746 TCFG_CHAIN_POSITION,
3751 static Jim_Nvp nvp_config_opts[] = {
3752 { .name = "-type", .value = TCFG_TYPE },
3753 { .name = "-event", .value = TCFG_EVENT },
3754 { .name = "-work-area-virt", .value = TCFG_WORK_AREA_VIRT },
3755 { .name = "-work-area-phys", .value = TCFG_WORK_AREA_PHYS },
3756 { .name = "-work-area-size", .value = TCFG_WORK_AREA_SIZE },
3757 { .name = "-work-area-backup", .value = TCFG_WORK_AREA_BACKUP },
3758 { .name = "-endian" , .value = TCFG_ENDIAN },
3759 { .name = "-variant", .value = TCFG_VARIANT },
3760 { .name = "-coreid", .value = TCFG_COREID },
3761 { .name = "-chain-position", .value = TCFG_CHAIN_POSITION },
3762 { .name = "-dbgbase", .value = TCFG_DBGBASE },
3763 { .name = "-rtos", .value = TCFG_RTOS },
3764 { .name = NULL, .value = -1 }
3767 static int target_configure(Jim_GetOptInfo *goi, struct target *target)
3775 /* parse config or cget options ... */
3776 while (goi->argc > 0) {
3777 Jim_SetEmptyResult(goi->interp);
3778 /* Jim_GetOpt_Debug(goi); */
3780 if (target->type->target_jim_configure) {
3781 /* target defines a configure function */
3782 /* target gets first dibs on parameters */
3783 e = (*(target->type->target_jim_configure))(target, goi);
3792 /* otherwise we 'continue' below */
3794 e = Jim_GetOpt_Nvp(goi, nvp_config_opts, &n);
3796 Jim_GetOpt_NvpUnknown(goi, nvp_config_opts, 0);
3802 if (goi->isconfigure) {
3803 Jim_SetResultFormatted(goi->interp,
3804 "not settable: %s", n->name);
3808 if (goi->argc != 0) {
3809 Jim_WrongNumArgs(goi->interp,
3810 goi->argc, goi->argv,
3815 Jim_SetResultString(goi->interp,
3816 target_type_name(target), -1);
3820 if (goi->argc == 0) {
3821 Jim_WrongNumArgs(goi->interp, goi->argc, goi->argv, "-event ?event-name? ...");
3825 e = Jim_GetOpt_Nvp(goi, nvp_target_event, &n);
3827 Jim_GetOpt_NvpUnknown(goi, nvp_target_event, 1);
3831 if (goi->isconfigure) {
3832 if (goi->argc != 1) {
3833 Jim_WrongNumArgs(goi->interp, goi->argc, goi->argv, "-event ?event-name? ?EVENT-BODY?");
3837 if (goi->argc != 0) {
3838 Jim_WrongNumArgs(goi->interp, goi->argc, goi->argv, "-event ?event-name?");
3844 struct target_event_action *teap;
3846 teap = target->event_action;
3847 /* replace existing? */
3849 if (teap->event == (enum target_event)n->value) {
3855 if (goi->isconfigure) {
3856 bool replace = true;
3859 teap = calloc(1, sizeof(*teap));
3862 teap->event = n->value;
3863 teap->interp = goi->interp;
3864 Jim_GetOpt_Obj(goi, &o);
3866 Jim_DecrRefCount(teap->interp, teap->body);
3868 teap->body = Jim_DuplicateObj(goi->interp, o);
3871 * Tcl/TK - "tk events" have a nice feature.
3872 * See the "BIND" command.
3873 * We should support that here.
3874 * You can specify %X and %Y in the event code.
3875 * The idea is: %T - target name.
3876 * The idea is: %N - target number
3877 * The idea is: %E - event name.
3879 Jim_IncrRefCount(teap->body);
3883 /* add to head of event list */
3884 teap->next = target->event_action;
3885 target->event_action = teap;
3887 Jim_SetEmptyResult(goi->interp);
3891 Jim_SetEmptyResult(goi->interp);
3893 Jim_SetResult(goi->interp, Jim_DuplicateObj(goi->interp, teap->body));
3900 case TCFG_WORK_AREA_VIRT:
3901 if (goi->isconfigure) {
3902 target_free_all_working_areas(target);
3903 e = Jim_GetOpt_Wide(goi, &w);
3907 target->working_area_virt = w;
3908 target->working_area_virt_spec = true;
3910 if (goi->argc != 0) {
3914 Jim_SetResult(goi->interp, Jim_NewIntObj(goi->interp, target->working_area_virt));
3918 case TCFG_WORK_AREA_PHYS:
3919 if (goi->isconfigure) {
3920 target_free_all_working_areas(target);
3921 e = Jim_GetOpt_Wide(goi, &w);
3925 target->working_area_phys = w;
3926 target->working_area_phys_spec = true;
3928 if (goi->argc != 0) {
3932 Jim_SetResult(goi->interp, Jim_NewIntObj(goi->interp, target->working_area_phys));
3936 case TCFG_WORK_AREA_SIZE:
3937 if (goi->isconfigure) {
3938 target_free_all_working_areas(target);
3939 e = Jim_GetOpt_Wide(goi, &w);
3943 target->working_area_size = w;
3945 if (goi->argc != 0) {
3949 Jim_SetResult(goi->interp, Jim_NewIntObj(goi->interp, target->working_area_size));
3953 case TCFG_WORK_AREA_BACKUP:
3954 if (goi->isconfigure) {
3955 target_free_all_working_areas(target);
3956 e = Jim_GetOpt_Wide(goi, &w);
3960 /* make this exactly 1 or 0 */
3961 target->backup_working_area = (!!w);
3963 if (goi->argc != 0) {
3967 Jim_SetResult(goi->interp, Jim_NewIntObj(goi->interp, target->backup_working_area));
3968 /* loop for more e*/
3973 if (goi->isconfigure) {
3974 e = Jim_GetOpt_Nvp(goi, nvp_target_endian, &n);
3976 Jim_GetOpt_NvpUnknown(goi, nvp_target_endian, 1);
3979 target->endianness = n->value;
3981 if (goi->argc != 0) {
3985 n = Jim_Nvp_value2name_simple(nvp_target_endian, target->endianness);
3986 if (n->name == NULL) {
3987 target->endianness = TARGET_LITTLE_ENDIAN;
3988 n = Jim_Nvp_value2name_simple(nvp_target_endian, target->endianness);
3990 Jim_SetResultString(goi->interp, n->name, -1);
3995 if (goi->isconfigure) {
3996 if (goi->argc < 1) {
3997 Jim_SetResultFormatted(goi->interp,
4002 if (target->variant) {
4003 free((void *)(target->variant));
4005 e = Jim_GetOpt_String(goi, &cp, NULL);
4006 target->variant = strdup(cp);
4008 if (goi->argc != 0) {
4012 Jim_SetResultString(goi->interp, target->variant,-1);
4017 if (goi->isconfigure) {
4018 e = Jim_GetOpt_Wide(goi, &w);
4022 target->coreid = (int32_t)w;
4024 if (goi->argc != 0) {
4028 Jim_SetResult(goi->interp, Jim_NewIntObj(goi->interp, target->working_area_size));
4032 case TCFG_CHAIN_POSITION:
4033 if (goi->isconfigure) {
4035 struct jtag_tap *tap;
4036 target_free_all_working_areas(target);
4037 e = Jim_GetOpt_Obj(goi, &o_t);
4041 tap = jtag_tap_by_jim_obj(goi->interp, o_t);
4045 /* make this exactly 1 or 0 */
4048 if (goi->argc != 0) {
4052 Jim_SetResultString(goi->interp, target->tap->dotted_name, -1);
4053 /* loop for more e*/
4056 if (goi->isconfigure) {
4057 e = Jim_GetOpt_Wide(goi, &w);
4061 target->dbgbase = (uint32_t)w;
4062 target->dbgbase_set = true;
4064 if (goi->argc != 0) {
4068 Jim_SetResult(goi->interp, Jim_NewIntObj(goi->interp, target->dbgbase));
4075 int result = rtos_create( goi, target );
4076 if ( result != JIM_OK )
4084 } /* while (goi->argc) */
4087 /* done - we return */
4092 jim_target_configure(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
4096 Jim_GetOpt_Setup(&goi, interp, argc - 1, argv + 1);
4097 goi.isconfigure = !strcmp(Jim_GetString(argv[0], NULL), "configure");
4098 int need_args = 1 + goi.isconfigure;
4099 if (goi.argc < need_args)
4101 Jim_WrongNumArgs(goi.interp, goi.argc, goi.argv,
4103 ? "missing: -option VALUE ..."
4104 : "missing: -option ...");
4107 struct target *target = Jim_CmdPrivData(goi.interp);
4108 return target_configure(&goi, target);
4111 static int jim_target_mw(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
4113 const char *cmd_name = Jim_GetString(argv[0], NULL);
4116 Jim_GetOpt_Setup(&goi, interp, argc - 1, argv + 1);
4118 if (goi.argc < 2 || goi.argc > 4)
4120 Jim_SetResultFormatted(goi.interp,
4121 "usage: %s [phys] <address> <data> [<count>]", cmd_name);
4126 fn = target_write_memory_fast;
4129 if (strcmp(Jim_GetString(argv[1], NULL), "phys") == 0)
4132 struct Jim_Obj *obj;
4133 e = Jim_GetOpt_Obj(&goi, &obj);
4137 fn = target_write_phys_memory;
4141 e = Jim_GetOpt_Wide(&goi, &a);
4146 e = Jim_GetOpt_Wide(&goi, &b);
4153 e = Jim_GetOpt_Wide(&goi, &c);
4158 /* all args must be consumed */
4164 struct target *target = Jim_CmdPrivData(goi.interp);
4166 if (strcasecmp(cmd_name, "mww") == 0) {
4169 else if (strcasecmp(cmd_name, "mwh") == 0) {
4172 else if (strcasecmp(cmd_name, "mwb") == 0) {
4175 LOG_ERROR("command '%s' unknown: ", cmd_name);
4179 return (target_fill_mem(target, a, fn, data_size, b, c) == ERROR_OK) ? JIM_OK : JIM_ERR;
4182 static int jim_target_md(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
4184 const char *cmd_name = Jim_GetString(argv[0], NULL);
4187 Jim_GetOpt_Setup(&goi, interp, argc - 1, argv + 1);
4189 if ((goi.argc < 1) || (goi.argc > 3))
4191 Jim_SetResultFormatted(goi.interp,
4192 "usage: %s [phys] <address> [<count>]", cmd_name);
4196 int (*fn)(struct target *target,
4197 uint32_t address, uint32_t size, uint32_t count, uint8_t *buffer);
4198 fn=target_read_memory;
4201 if (strcmp(Jim_GetString(argv[1], NULL), "phys") == 0)
4204 struct Jim_Obj *obj;
4205 e = Jim_GetOpt_Obj(&goi, &obj);
4209 fn=target_read_phys_memory;
4213 e = Jim_GetOpt_Wide(&goi, &a);
4218 if (goi.argc == 1) {
4219 e = Jim_GetOpt_Wide(&goi, &c);
4227 /* all args must be consumed */
4233 jim_wide b = 1; /* shut up gcc */
4234 if (strcasecmp(cmd_name, "mdw") == 0)
4236 else if (strcasecmp(cmd_name, "mdh") == 0)
4238 else if (strcasecmp(cmd_name, "mdb") == 0)
4241 LOG_ERROR("command '%s' unknown: ", cmd_name);
4245 /* convert count to "bytes" */
4248 struct target *target = Jim_CmdPrivData(goi.interp);
4249 uint8_t target_buf[32];
4256 e = fn(target, a, b, y / b, target_buf);
4257 if (e != ERROR_OK) {
4259 snprintf(tmp, sizeof(tmp), "%08lx", (long)a);
4260 Jim_SetResultFormatted(interp, "error reading target @ 0x%s", tmp);
4264 command_print(NULL, "0x%08x ", (int)(a));
4267 for (x = 0; x < 16 && x < y; x += 4)
4269 z = target_buffer_get_u32(target, &(target_buf[ x ]));
4270 command_print(NULL, "%08x ", (int)(z));
4272 for (; (x < 16) ; x += 4) {
4273 command_print(NULL, " ");
4277 for (x = 0; x < 16 && x < y; x += 2)
4279 z = target_buffer_get_u16(target, &(target_buf[ x ]));
4280 command_print(NULL, "%04x ", (int)(z));
4282 for (; (x < 16) ; x += 2) {
4283 command_print(NULL, " ");
4288 for (x = 0 ; (x < 16) && (x < y) ; x += 1) {
4289 z = target_buffer_get_u8(target, &(target_buf[ x ]));
4290 command_print(NULL, "%02x ", (int)(z));
4292 for (; (x < 16) ; x += 1) {
4293 command_print(NULL, " ");
4297 /* ascii-ify the bytes */
4298 for (x = 0 ; x < y ; x++) {
4299 if ((target_buf[x] >= 0x20) &&
4300 (target_buf[x] <= 0x7e)) {
4304 target_buf[x] = '.';
4309 target_buf[x] = ' ';
4314 /* print - with a newline */
4315 command_print(NULL, "%s\n", target_buf);
4323 static int jim_target_mem2array(Jim_Interp *interp,
4324 int argc, Jim_Obj *const *argv)
4326 struct target *target = Jim_CmdPrivData(interp);
4327 return target_mem2array(interp, target, argc - 1, argv + 1);
4330 static int jim_target_array2mem(Jim_Interp *interp,
4331 int argc, Jim_Obj *const *argv)
4333 struct target *target = Jim_CmdPrivData(interp);
4334 return target_array2mem(interp, target, argc - 1, argv + 1);
4337 static int jim_target_tap_disabled(Jim_Interp *interp)
4339 Jim_SetResultFormatted(interp, "[TAP is disabled]");
4343 static int jim_target_examine(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
4347 Jim_WrongNumArgs(interp, 1, argv, "[no parameters]");
4350 struct target *target = Jim_CmdPrivData(interp);
4351 if (!target->tap->enabled)
4352 return jim_target_tap_disabled(interp);
4354 int e = target->type->examine(target);
4362 static int jim_target_halt_gdb(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
4366 Jim_WrongNumArgs(interp, 1, argv, "[no parameters]");
4369 struct target *target = Jim_CmdPrivData(interp);
4371 if (target_call_event_callbacks(target, TARGET_EVENT_GDB_HALT) != ERROR_OK)
4377 static int jim_target_poll(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
4381 Jim_WrongNumArgs(interp, 1, argv, "[no parameters]");
4384 struct target *target = Jim_CmdPrivData(interp);
4385 if (!target->tap->enabled)
4386 return jim_target_tap_disabled(interp);
4389 if (!(target_was_examined(target))) {
4390 e = ERROR_TARGET_NOT_EXAMINED;
4392 e = target->type->poll(target);
4401 static int jim_target_reset(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
4404 Jim_GetOpt_Setup(&goi, interp, argc - 1, argv + 1);
4408 Jim_WrongNumArgs(interp, 0, argv,
4409 "([tT]|[fF]|assert|deassert) BOOL");
4414 int e = Jim_GetOpt_Nvp(&goi, nvp_assert, &n);
4417 Jim_GetOpt_NvpUnknown(&goi, nvp_assert, 1);
4420 /* the halt or not param */
4422 e = Jim_GetOpt_Wide(&goi, &a);
4426 struct target *target = Jim_CmdPrivData(goi.interp);
4427 if (!target->tap->enabled)
4428 return jim_target_tap_disabled(interp);
4429 if (!(target_was_examined(target)))
4431 LOG_ERROR("Target not examined yet");
4432 return ERROR_TARGET_NOT_EXAMINED;
4434 if (!target->type->assert_reset || !target->type->deassert_reset)
4436 Jim_SetResultFormatted(interp,
4437 "No target-specific reset for %s",
4438 target_name(target));
4441 /* determine if we should halt or not. */
4442 target->reset_halt = !!a;
4443 /* When this happens - all workareas are invalid. */
4444 target_free_all_working_areas_restore(target, 0);
4447 if (n->value == NVP_ASSERT) {
4448 e = target->type->assert_reset(target);
4450 e = target->type->deassert_reset(target);
4452 return (e == ERROR_OK) ? JIM_OK : JIM_ERR;
4455 static int jim_target_halt(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
4458 Jim_WrongNumArgs(interp, 1, argv, "[no parameters]");
4461 struct target *target = Jim_CmdPrivData(interp);
4462 if (!target->tap->enabled)
4463 return jim_target_tap_disabled(interp);
4464 int e = target->type->halt(target);
4465 return (e == ERROR_OK) ? JIM_OK : JIM_ERR;
4468 static int jim_target_wait_state(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
4471 Jim_GetOpt_Setup(&goi, interp, argc - 1, argv + 1);
4473 /* params: <name> statename timeoutmsecs */
4476 const char *cmd_name = Jim_GetString(argv[0], NULL);
4477 Jim_SetResultFormatted(goi.interp,
4478 "%s <state_name> <timeout_in_msec>", cmd_name);
4483 int e = Jim_GetOpt_Nvp(&goi, nvp_target_state, &n);
4485 Jim_GetOpt_NvpUnknown(&goi, nvp_target_state,1);
4489 e = Jim_GetOpt_Wide(&goi, &a);
4493 struct target *target = Jim_CmdPrivData(interp);
4494 if (!target->tap->enabled)
4495 return jim_target_tap_disabled(interp);
4497 e = target_wait_state(target, n->value, a);
4500 Jim_Obj *eObj = Jim_NewIntObj(interp, e);
4501 Jim_SetResultFormatted(goi.interp,
4502 "target: %s wait %s fails (%#s) %s",
4503 target_name(target), n->name,
4504 eObj, target_strerror_safe(e));
4505 Jim_FreeNewObj(interp, eObj);
4510 /* List for human, Events defined for this target.
4511 * scripts/programs should use 'name cget -event NAME'
4513 static int jim_target_event_list(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
4515 struct command_context *cmd_ctx = current_command_context(interp);
4516 assert (cmd_ctx != NULL);
4518 struct target *target = Jim_CmdPrivData(interp);
4519 struct target_event_action *teap = target->event_action;
4520 command_print(cmd_ctx, "Event actions for target (%d) %s\n",
4521 target->target_number,
4522 target_name(target));
4523 command_print(cmd_ctx, "%-25s | Body", "Event");
4524 command_print(cmd_ctx, "------------------------- | "
4525 "----------------------------------------");
4528 Jim_Nvp *opt = Jim_Nvp_value2name_simple(nvp_target_event, teap->event);
4529 command_print(cmd_ctx, "%-25s | %s",
4530 opt->name, Jim_GetString(teap->body, NULL));
4533 command_print(cmd_ctx, "***END***");
4536 static int jim_target_current_state(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
4540 Jim_WrongNumArgs(interp, 1, argv, "[no parameters]");
4543 struct target *target = Jim_CmdPrivData(interp);
4544 Jim_SetResultString(interp, target_state_name(target), -1);
4547 static int jim_target_invoke_event(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
4550 Jim_GetOpt_Setup(&goi, interp, argc - 1, argv + 1);
4553 const char *cmd_name = Jim_GetString(argv[0], NULL);
4554 Jim_SetResultFormatted(goi.interp, "%s <eventname>", cmd_name);
4558 int e = Jim_GetOpt_Nvp(&goi, nvp_target_event, &n);
4561 Jim_GetOpt_NvpUnknown(&goi, nvp_target_event, 1);
4564 struct target *target = Jim_CmdPrivData(interp);
4565 target_handle_event(target, n->value);
4569 static const struct command_registration target_instance_command_handlers[] = {
4571 .name = "configure",
4572 .mode = COMMAND_CONFIG,
4573 .jim_handler = jim_target_configure,
4574 .help = "configure a new target for use",
4575 .usage = "[target_attribute ...]",
4579 .mode = COMMAND_ANY,
4580 .jim_handler = jim_target_configure,
4581 .help = "returns the specified target attribute",
4582 .usage = "target_attribute",
4586 .mode = COMMAND_EXEC,
4587 .jim_handler = jim_target_mw,
4588 .help = "Write 32-bit word(s) to target memory",
4589 .usage = "address data [count]",
4593 .mode = COMMAND_EXEC,
4594 .jim_handler = jim_target_mw,
4595 .help = "Write 16-bit half-word(s) to target memory",
4596 .usage = "address data [count]",
4600 .mode = COMMAND_EXEC,
4601 .jim_handler = jim_target_mw,
4602 .help = "Write byte(s) to target memory",
4603 .usage = "address data [count]",
4607 .mode = COMMAND_EXEC,
4608 .jim_handler = jim_target_md,
4609 .help = "Display target memory as 32-bit words",
4610 .usage = "address [count]",
4614 .mode = COMMAND_EXEC,
4615 .jim_handler = jim_target_md,
4616 .help = "Display target memory as 16-bit half-words",
4617 .usage = "address [count]",
4621 .mode = COMMAND_EXEC,
4622 .jim_handler = jim_target_md,
4623 .help = "Display target memory as 8-bit bytes",
4624 .usage = "address [count]",
4627 .name = "array2mem",
4628 .mode = COMMAND_EXEC,
4629 .jim_handler = jim_target_array2mem,
4630 .help = "Writes Tcl array of 8/16/32 bit numbers "
4632 .usage = "arrayname bitwidth address count",
4635 .name = "mem2array",
4636 .mode = COMMAND_EXEC,
4637 .jim_handler = jim_target_mem2array,
4638 .help = "Loads Tcl array of 8/16/32 bit numbers "
4639 "from target memory",
4640 .usage = "arrayname bitwidth address count",
4643 .name = "eventlist",
4644 .mode = COMMAND_EXEC,
4645 .jim_handler = jim_target_event_list,
4646 .help = "displays a table of events defined for this target",
4650 .mode = COMMAND_EXEC,
4651 .jim_handler = jim_target_current_state,
4652 .help = "displays the current state of this target",
4655 .name = "arp_examine",
4656 .mode = COMMAND_EXEC,
4657 .jim_handler = jim_target_examine,
4658 .help = "used internally for reset processing",
4661 .name = "arp_halt_gdb",
4662 .mode = COMMAND_EXEC,
4663 .jim_handler = jim_target_halt_gdb,
4664 .help = "used internally for reset processing to halt GDB",
4668 .mode = COMMAND_EXEC,
4669 .jim_handler = jim_target_poll,
4670 .help = "used internally for reset processing",
4673 .name = "arp_reset",
4674 .mode = COMMAND_EXEC,
4675 .jim_handler = jim_target_reset,
4676 .help = "used internally for reset processing",
4680 .mode = COMMAND_EXEC,
4681 .jim_handler = jim_target_halt,
4682 .help = "used internally for reset processing",
4685 .name = "arp_waitstate",
4686 .mode = COMMAND_EXEC,
4687 .jim_handler = jim_target_wait_state,
4688 .help = "used internally for reset processing",
4691 .name = "invoke-event",
4692 .mode = COMMAND_EXEC,
4693 .jim_handler = jim_target_invoke_event,
4694 .help = "invoke handler for specified event",
4695 .usage = "event_name",
4697 COMMAND_REGISTRATION_DONE
4700 static int target_create(Jim_GetOptInfo *goi)
4708 struct target *target;
4709 struct command_context *cmd_ctx;
4711 cmd_ctx = current_command_context(goi->interp);
4712 assert (cmd_ctx != NULL);
4714 if (goi->argc < 3) {
4715 Jim_WrongNumArgs(goi->interp, 1, goi->argv, "?name? ?type? ..options...");
4720 Jim_GetOpt_Obj(goi, &new_cmd);
4721 /* does this command exist? */
4722 cmd = Jim_GetCommand(goi->interp, new_cmd, JIM_ERRMSG);
4724 cp = Jim_GetString(new_cmd, NULL);
4725 Jim_SetResultFormatted(goi->interp, "Command/target: %s Exists", cp);
4730 e = Jim_GetOpt_String(goi, &cp2, NULL);
4732 /* now does target type exist */
4733 for (x = 0 ; target_types[x] ; x++) {
4734 if (0 == strcmp(cp, target_types[x]->name)) {
4739 if (target_types[x] == NULL) {
4740 Jim_SetResultFormatted(goi->interp, "Unknown target type %s, try one of ", cp);
4741 for (x = 0 ; target_types[x] ; x++) {
4742 if (target_types[x + 1]) {
4743 Jim_AppendStrings(goi->interp,
4744 Jim_GetResult(goi->interp),
4745 target_types[x]->name,
4748 Jim_AppendStrings(goi->interp,
4749 Jim_GetResult(goi->interp),
4751 target_types[x]->name,NULL);
4758 target = calloc(1,sizeof(struct target));
4759 /* set target number */
4760 target->target_number = new_target_number();
4762 /* allocate memory for each unique target type */
4763 target->type = (struct target_type*)calloc(1,sizeof(struct target_type));
4765 memcpy(target->type, target_types[x], sizeof(struct target_type));
4767 /* will be set by "-endian" */
4768 target->endianness = TARGET_ENDIAN_UNKNOWN;
4770 /* default to first core, override with -coreid */
4773 target->working_area = 0x0;
4774 target->working_area_size = 0x0;
4775 target->working_areas = NULL;
4776 target->backup_working_area = 0;
4778 target->state = TARGET_UNKNOWN;
4779 target->debug_reason = DBG_REASON_UNDEFINED;
4780 target->reg_cache = NULL;
4781 target->breakpoints = NULL;
4782 target->watchpoints = NULL;
4783 target->next = NULL;
4784 target->arch_info = NULL;
4786 target->display = 1;
4788 target->halt_issued = false;
4790 /* initialize trace information */
4791 target->trace_info = malloc(sizeof(struct trace));
4792 target->trace_info->num_trace_points = 0;
4793 target->trace_info->trace_points_size = 0;
4794 target->trace_info->trace_points = NULL;
4795 target->trace_info->trace_history_size = 0;
4796 target->trace_info->trace_history = NULL;
4797 target->trace_info->trace_history_pos = 0;
4798 target->trace_info->trace_history_overflowed = 0;
4800 target->dbgmsg = NULL;
4801 target->dbg_msg_enabled = 0;
4803 target->endianness = TARGET_ENDIAN_UNKNOWN;
4805 target->rtos = NULL;
4806 target->rtos_auto_detect = false;
4808 /* Do the rest as "configure" options */
4809 goi->isconfigure = 1;
4810 e = target_configure(goi, target);
4812 if (target->tap == NULL)
4814 Jim_SetResultString(goi->interp, "-chain-position required when creating target", -1);
4824 if (target->endianness == TARGET_ENDIAN_UNKNOWN) {
4825 /* default endian to little if not specified */
4826 target->endianness = TARGET_LITTLE_ENDIAN;
4829 /* incase variant is not set */
4830 if (!target->variant)
4831 target->variant = strdup("");
4833 cp = Jim_GetString(new_cmd, NULL);
4834 target->cmd_name = strdup(cp);
4836 /* create the target specific commands */
4837 if (target->type->commands) {
4838 e = register_commands(cmd_ctx, NULL, target->type->commands);
4840 LOG_ERROR("unable to register '%s' commands", cp);
4842 if (target->type->target_create) {
4843 (*(target->type->target_create))(target, goi->interp);
4846 /* append to end of list */
4848 struct target **tpp;
4849 tpp = &(all_targets);
4851 tpp = &((*tpp)->next);
4856 /* now - create the new target name command */
4857 const const struct command_registration target_subcommands[] = {
4859 .chain = target_instance_command_handlers,
4862 .chain = target->type->commands,
4864 COMMAND_REGISTRATION_DONE
4866 const const struct command_registration target_commands[] = {
4869 .mode = COMMAND_ANY,
4870 .help = "target command group",
4871 .chain = target_subcommands,
4873 COMMAND_REGISTRATION_DONE
4875 e = register_commands(cmd_ctx, NULL, target_commands);
4879 struct command *c = command_find_in_context(cmd_ctx, cp);
4881 command_set_handler_data(c, target);
4883 return (ERROR_OK == e) ? JIM_OK : JIM_ERR;
4886 static int jim_target_current(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
4890 Jim_WrongNumArgs(interp, 1, argv, "Too many parameters");
4893 struct command_context *cmd_ctx = current_command_context(interp);
4894 assert (cmd_ctx != NULL);
4896 Jim_SetResultString(interp, get_current_target(cmd_ctx)->cmd_name, -1);
4900 static int jim_target_types(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
4904 Jim_WrongNumArgs(interp, 1, argv, "Too many parameters");
4907 Jim_SetResult(interp, Jim_NewListObj(interp, NULL, 0));
4908 for (unsigned x = 0; NULL != target_types[x]; x++)
4910 Jim_ListAppendElement(interp, Jim_GetResult(interp),
4911 Jim_NewStringObj(interp, target_types[x]->name, -1));
4916 static int jim_target_names(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
4920 Jim_WrongNumArgs(interp, 1, argv, "Too many parameters");
4923 Jim_SetResult(interp, Jim_NewListObj(interp, NULL, 0));
4924 struct target *target = all_targets;
4927 Jim_ListAppendElement(interp, Jim_GetResult(interp),
4928 Jim_NewStringObj(interp, target_name(target), -1));
4929 target = target->next;
4934 static int jim_target_smp(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
4937 const char *targetname;
4939 struct target *target;
4940 struct target_list *head, *curr, *new;
4941 curr = (struct target_list*) NULL;
4942 head = (struct target_list*) NULL;
4943 new = (struct target_list*) NULL;
4946 LOG_DEBUG("%d",argc);
4947 /* argv[1] = target to associate in smp
4948 * argv[2] = target to assoicate in smp
4955 targetname = Jim_GetString(argv[i], &len);
4956 target = get_target(targetname);
4957 LOG_DEBUG("%s ",targetname);
4960 new=malloc(sizeof(struct target_list));
4961 new->target = target;
4962 new->next = (struct target_list*)NULL;
4963 if (head == (struct target_list*)NULL)
4975 /* now parse the list of cpu and put the target in smp mode*/
4978 while(curr!=(struct target_list *)NULL)
4980 target=curr->target;
4982 target->head = head;
4989 static int jim_target_create(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
4992 Jim_GetOpt_Setup(&goi, interp, argc - 1, argv + 1);
4995 Jim_WrongNumArgs(goi.interp, goi.argc, goi.argv,
4996 "<name> <target_type> [<target_options> ...]");
4999 return target_create(&goi);
5002 static int jim_target_number(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
5005 Jim_GetOpt_Setup(&goi, interp, argc - 1, argv + 1);
5007 /* It's OK to remove this mechanism sometime after August 2010 or so */
5008 LOG_WARNING("don't use numbers as target identifiers; use names");
5011 Jim_SetResultFormatted(goi.interp, "usage: target number <number>");
5015 int e = Jim_GetOpt_Wide(&goi, &w);
5019 struct target *target;
5020 for (target = all_targets; NULL != target; target = target->next)
5022 if (target->target_number != w)
5025 Jim_SetResultString(goi.interp, target_name(target), -1);
5029 Jim_Obj *wObj = Jim_NewIntObj(goi.interp, w);
5030 Jim_SetResultFormatted(goi.interp,
5031 "Target: number %#s does not exist", wObj);
5032 Jim_FreeNewObj(interp, wObj);
5037 static int jim_target_count(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
5041 Jim_WrongNumArgs(interp, 1, argv, "<no parameters>");
5045 struct target *target = all_targets;
5046 while (NULL != target)
5048 target = target->next;
5051 Jim_SetResult(interp, Jim_NewIntObj(interp, count));
5055 static const struct command_registration target_subcommand_handlers[] = {
5058 .mode = COMMAND_CONFIG,
5059 .handler = handle_target_init_command,
5060 .help = "initialize targets",
5064 /* REVISIT this should be COMMAND_CONFIG ... */
5065 .mode = COMMAND_ANY,
5066 .jim_handler = jim_target_create,
5067 .usage = "name type '-chain-position' name [options ...]",
5068 .help = "Creates and selects a new target",
5072 .mode = COMMAND_ANY,
5073 .jim_handler = jim_target_current,
5074 .help = "Returns the currently selected target",
5078 .mode = COMMAND_ANY,
5079 .jim_handler = jim_target_types,
5080 .help = "Returns the available target types as "
5081 "a list of strings",
5085 .mode = COMMAND_ANY,
5086 .jim_handler = jim_target_names,
5087 .help = "Returns the names of all targets as a list of strings",
5091 .mode = COMMAND_ANY,
5092 .jim_handler = jim_target_number,
5094 .help = "Returns the name of the numbered target "
5099 .mode = COMMAND_ANY,
5100 .jim_handler = jim_target_count,
5101 .help = "Returns the number of targets as an integer "
5106 .mode = COMMAND_ANY,
5107 .jim_handler = jim_target_smp,
5108 .usage = "targetname1 targetname2 ...",
5109 .help = "gather several target in a smp list"
5112 COMMAND_REGISTRATION_DONE
5123 static int fastload_num;
5124 static struct FastLoad *fastload;
5126 static void free_fastload(void)
5128 if (fastload != NULL)
5131 for (i = 0; i < fastload_num; i++)
5133 if (fastload[i].data)
5134 free(fastload[i].data);
5144 COMMAND_HANDLER(handle_fast_load_image_command)
5148 uint32_t image_size;
5149 uint32_t min_address = 0;
5150 uint32_t max_address = 0xffffffff;
5155 int retval = CALL_COMMAND_HANDLER(parse_load_image_command_CMD_ARGV,
5156 &image, &min_address, &max_address);
5157 if (ERROR_OK != retval)
5160 struct duration bench;
5161 duration_start(&bench);
5163 retval = image_open(&image, CMD_ARGV[0], (CMD_ARGC >= 3) ? CMD_ARGV[2] : NULL);
5164 if (retval != ERROR_OK)
5171 fastload_num = image.num_sections;
5172 fastload = (struct FastLoad *)malloc(sizeof(struct FastLoad)*image.num_sections);
5173 if (fastload == NULL)
5175 command_print(CMD_CTX, "out of memory");
5176 image_close(&image);
5179 memset(fastload, 0, sizeof(struct FastLoad)*image.num_sections);
5180 for (i = 0; i < image.num_sections; i++)
5182 buffer = malloc(image.sections[i].size);
5185 command_print(CMD_CTX, "error allocating buffer for section (%d bytes)",
5186 (int)(image.sections[i].size));
5187 retval = ERROR_FAIL;
5191 if ((retval = image_read_section(&image, i, 0x0, image.sections[i].size, buffer, &buf_cnt)) != ERROR_OK)
5197 uint32_t offset = 0;
5198 uint32_t length = buf_cnt;
5201 /* DANGER!!! beware of unsigned comparision here!!! */
5203 if ((image.sections[i].base_address + buf_cnt >= min_address)&&
5204 (image.sections[i].base_address < max_address))
5206 if (image.sections[i].base_address < min_address)
5208 /* clip addresses below */
5209 offset += min_address-image.sections[i].base_address;
5213 if (image.sections[i].base_address + buf_cnt > max_address)
5215 length -= (image.sections[i].base_address + buf_cnt)-max_address;
5218 fastload[i].address = image.sections[i].base_address + offset;
5219 fastload[i].data = malloc(length);
5220 if (fastload[i].data == NULL)
5223 command_print(CMD_CTX, "error allocating buffer for section (%d bytes)",
5225 retval = ERROR_FAIL;
5228 memcpy(fastload[i].data, buffer + offset, length);
5229 fastload[i].length = length;
5231 image_size += length;
5232 command_print(CMD_CTX, "%u bytes written at address 0x%8.8x",
5233 (unsigned int)length,
5234 ((unsigned int)(image.sections[i].base_address + offset)));
5240 if ((ERROR_OK == retval) && (duration_measure(&bench) == ERROR_OK))
5242 command_print(CMD_CTX, "Loaded %" PRIu32 " bytes "
5243 "in %fs (%0.3f KiB/s)", image_size,
5244 duration_elapsed(&bench), duration_kbps(&bench, image_size));
5246 command_print(CMD_CTX,
5247 "WARNING: image has not been loaded to target!"
5248 "You can issue a 'fast_load' to finish loading.");
5251 image_close(&image);
5253 if (retval != ERROR_OK)
5261 COMMAND_HANDLER(handle_fast_load_command)
5264 return ERROR_COMMAND_SYNTAX_ERROR;
5265 if (fastload == NULL)
5267 LOG_ERROR("No image in memory");
5271 int ms = timeval_ms();
5273 int retval = ERROR_OK;
5274 for (i = 0; i < fastload_num;i++)
5276 struct target *target = get_current_target(CMD_CTX);
5277 command_print(CMD_CTX, "Write to 0x%08x, length 0x%08x",
5278 (unsigned int)(fastload[i].address),
5279 (unsigned int)(fastload[i].length));
5280 retval = target_write_buffer(target, fastload[i].address, fastload[i].length, fastload[i].data);
5281 if (retval != ERROR_OK)
5285 size += fastload[i].length;
5287 if (retval == ERROR_OK)
5289 int after = timeval_ms();
5290 command_print(CMD_CTX, "Loaded image %f kBytes/s", (float)(size/1024.0)/((float)(after-ms)/1000.0));
5295 static const struct command_registration target_command_handlers[] = {
5298 .handler = handle_targets_command,
5299 .mode = COMMAND_ANY,
5300 .help = "change current default target (one parameter) "
5301 "or prints table of all targets (no parameters)",
5302 .usage = "[target]",
5306 .mode = COMMAND_CONFIG,
5307 .help = "configure target",
5309 .chain = target_subcommand_handlers,
5311 COMMAND_REGISTRATION_DONE
5314 int target_register_commands(struct command_context *cmd_ctx)
5316 return register_commands(cmd_ctx, NULL, target_command_handlers);
5319 static bool target_reset_nag = true;
5321 bool get_target_reset_nag(void)
5323 return target_reset_nag;
5326 COMMAND_HANDLER(handle_target_reset_nag)
5328 return CALL_COMMAND_HANDLER(handle_command_parse_bool,
5329 &target_reset_nag, "Nag after each reset about options to improve "
5333 static const struct command_registration target_exec_command_handlers[] = {
5335 .name = "fast_load_image",
5336 .handler = handle_fast_load_image_command,
5337 .mode = COMMAND_ANY,
5338 .help = "Load image into server memory for later use by "
5339 "fast_load; primarily for profiling",
5340 .usage = "filename address ['bin'|'ihex'|'elf'|'s19'] "
5341 "[min_address [max_length]]",
5344 .name = "fast_load",
5345 .handler = handle_fast_load_command,
5346 .mode = COMMAND_EXEC,
5347 .help = "loads active fast load image to current target "
5348 "- mainly for profiling purposes",
5352 .handler = handle_profile_command,
5353 .mode = COMMAND_EXEC,
5354 .help = "profiling samples the CPU PC",
5356 /** @todo don't register virt2phys() unless target supports it */
5358 .name = "virt2phys",
5359 .handler = handle_virt2phys_command,
5360 .mode = COMMAND_ANY,
5361 .help = "translate a virtual address into a physical address",
5362 .usage = "virtual_address",
5366 .handler = handle_reg_command,
5367 .mode = COMMAND_EXEC,
5368 .help = "display or set a register; with no arguments, "
5369 "displays all registers and their values",
5370 .usage = "[(register_name|register_number) [value]]",
5374 .handler = handle_poll_command,
5375 .mode = COMMAND_EXEC,
5376 .help = "poll target state; or reconfigure background polling",
5377 .usage = "['on'|'off']",
5380 .name = "wait_halt",
5381 .handler = handle_wait_halt_command,
5382 .mode = COMMAND_EXEC,
5383 .help = "wait up to the specified number of milliseconds "
5384 "(default 5) for a previously requested halt",
5385 .usage = "[milliseconds]",
5389 .handler = handle_halt_command,
5390 .mode = COMMAND_EXEC,
5391 .help = "request target to halt, then wait up to the specified"
5392 "number of milliseconds (default 5) for it to complete",
5393 .usage = "[milliseconds]",
5397 .handler = handle_resume_command,
5398 .mode = COMMAND_EXEC,
5399 .help = "resume target execution from current PC or address",
5400 .usage = "[address]",
5404 .handler = handle_reset_command,
5405 .mode = COMMAND_EXEC,
5406 .usage = "[run|halt|init]",
5407 .help = "Reset all targets into the specified mode."
5408 "Default reset mode is run, if not given.",
5411 .name = "soft_reset_halt",
5412 .handler = handle_soft_reset_halt_command,
5413 .mode = COMMAND_EXEC,
5414 .help = "halt the target and do a soft reset",
5418 .handler = handle_step_command,
5419 .mode = COMMAND_EXEC,
5420 .help = "step one instruction from current PC or address",
5421 .usage = "[address]",
5425 .handler = handle_md_command,
5426 .mode = COMMAND_EXEC,
5427 .help = "display memory words",
5428 .usage = "['phys'] address [count]",
5432 .handler = handle_md_command,
5433 .mode = COMMAND_EXEC,
5434 .help = "display memory half-words",
5435 .usage = "['phys'] address [count]",
5439 .handler = handle_md_command,
5440 .mode = COMMAND_EXEC,
5441 .help = "display memory bytes",
5442 .usage = "['phys'] address [count]",
5446 .handler = handle_mw_command,
5447 .mode = COMMAND_EXEC,
5448 .help = "write memory word",
5449 .usage = "['phys'] address value [count]",
5453 .handler = handle_mw_command,
5454 .mode = COMMAND_EXEC,
5455 .help = "write memory half-word",
5456 .usage = "['phys'] address value [count]",
5460 .handler = handle_mw_command,
5461 .mode = COMMAND_EXEC,
5462 .help = "write memory byte",
5463 .usage = "['phys'] address value [count]",
5467 .handler = handle_bp_command,
5468 .mode = COMMAND_EXEC,
5469 .help = "list or set hardware or software breakpoint",
5470 .usage = "[address length ['hw']]",
5474 .handler = handle_rbp_command,
5475 .mode = COMMAND_EXEC,
5476 .help = "remove breakpoint",
5481 .handler = handle_wp_command,
5482 .mode = COMMAND_EXEC,
5483 .help = "list (no params) or create watchpoints",
5484 .usage = "[address length [('r'|'w'|'a') value [mask]]]",
5488 .handler = handle_rwp_command,
5489 .mode = COMMAND_EXEC,
5490 .help = "remove watchpoint",
5494 .name = "load_image",
5495 .handler = handle_load_image_command,
5496 .mode = COMMAND_EXEC,
5497 .usage = "filename address ['bin'|'ihex'|'elf'|'s19'] "
5498 "[min_address] [max_length]",
5501 .name = "dump_image",
5502 .handler = handle_dump_image_command,
5503 .mode = COMMAND_EXEC,
5504 .usage = "filename address size",
5507 .name = "verify_image",
5508 .handler = handle_verify_image_command,
5509 .mode = COMMAND_EXEC,
5510 .usage = "filename [offset [type]]",
5513 .name = "test_image",
5514 .handler = handle_test_image_command,
5515 .mode = COMMAND_EXEC,
5516 .usage = "filename [offset [type]]",
5519 .name = "mem2array",
5520 .mode = COMMAND_EXEC,
5521 .jim_handler = jim_mem2array,
5522 .help = "read 8/16/32 bit memory and return as a TCL array "
5523 "for script processing",
5524 .usage = "arrayname bitwidth address count",
5527 .name = "array2mem",
5528 .mode = COMMAND_EXEC,
5529 .jim_handler = jim_array2mem,
5530 .help = "convert a TCL array to memory locations "
5531 "and write the 8/16/32 bit values",
5532 .usage = "arrayname bitwidth address count",
5535 .name = "reset_nag",
5536 .handler = handle_target_reset_nag,
5537 .mode = COMMAND_ANY,
5538 .help = "Nag after each reset about options that could have been "
5539 "enabled to improve performance. ",
5540 .usage = "['enable'|'disable']",
5542 COMMAND_REGISTRATION_DONE
5544 static int target_register_user_commands(struct command_context *cmd_ctx)
5546 int retval = ERROR_OK;
5547 if ((retval = target_request_register_commands(cmd_ctx)) != ERROR_OK)
5550 if ((retval = trace_register_commands(cmd_ctx)) != ERROR_OK)
5554 return register_commands(cmd_ctx, NULL, target_exec_command_handlers);