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 * Downloads a target-specific native code algorithm to the target,
737 * executes and leaves it running.
739 * @param target used to run the algorithm
740 * @param arch_info target-specific description of the algorithm.
742 int target_start_algorithm(struct target *target,
743 int num_mem_params, struct mem_param *mem_params,
744 int num_reg_params, struct reg_param *reg_params,
745 uint32_t entry_point, uint32_t exit_point,
748 int retval = ERROR_FAIL;
750 if (!target_was_examined(target))
752 LOG_ERROR("Target not examined yet");
755 if (!target->type->start_algorithm) {
756 LOG_ERROR("Target type '%s' does not support %s",
757 target_type_name(target), __func__);
760 if (target->running_alg) {
761 LOG_ERROR("Target is already running an algorithm");
765 target->running_alg = true;
766 retval = target->type->start_algorithm(target,
767 num_mem_params, mem_params,
768 num_reg_params, reg_params,
769 entry_point, exit_point, arch_info);
776 * Waits for an algorithm started with target_start_algorithm() to complete.
778 * @param target used to run the algorithm
779 * @param arch_info target-specific description of the algorithm.
781 int target_wait_algorithm(struct target *target,
782 int num_mem_params, struct mem_param *mem_params,
783 int num_reg_params, struct reg_param *reg_params,
784 uint32_t exit_point, int timeout_ms,
787 int retval = ERROR_FAIL;
789 if (!target->type->wait_algorithm) {
790 LOG_ERROR("Target type '%s' does not support %s",
791 target_type_name(target), __func__);
794 if (!target->running_alg) {
795 LOG_ERROR("Target is not running an algorithm");
799 retval = target->type->wait_algorithm(target,
800 num_mem_params, mem_params,
801 num_reg_params, reg_params,
802 exit_point, timeout_ms, arch_info);
803 if (retval != ERROR_TARGET_TIMEOUT)
804 target->running_alg = false;
811 int target_read_memory(struct target *target,
812 uint32_t address, uint32_t size, uint32_t count, uint8_t *buffer)
814 return target->type->read_memory(target, address, size, count, buffer);
817 static int target_read_phys_memory(struct target *target,
818 uint32_t address, uint32_t size, uint32_t count, uint8_t *buffer)
820 return target->type->read_phys_memory(target, address, size, count, buffer);
823 int target_write_memory(struct target *target,
824 uint32_t address, uint32_t size, uint32_t count, const uint8_t *buffer)
826 return target->type->write_memory(target, address, size, count, buffer);
829 static int target_write_phys_memory(struct target *target,
830 uint32_t address, uint32_t size, uint32_t count, const uint8_t *buffer)
832 return target->type->write_phys_memory(target, address, size, count, buffer);
835 int target_bulk_write_memory(struct target *target,
836 uint32_t address, uint32_t count, const uint8_t *buffer)
838 return target->type->bulk_write_memory(target, address, count, buffer);
841 int target_add_breakpoint(struct target *target,
842 struct breakpoint *breakpoint)
844 if ((target->state != TARGET_HALTED)&&(breakpoint->type!=BKPT_HARD)) {
845 LOG_WARNING("target %s is not halted", target->cmd_name);
846 return ERROR_TARGET_NOT_HALTED;
848 return target->type->add_breakpoint(target, breakpoint);
851 int target_add_context_breakpoint(struct target *target,
852 struct breakpoint *breakpoint)
854 if (target->state != TARGET_HALTED) {
855 LOG_WARNING("target %s is not halted", target->cmd_name);
856 return ERROR_TARGET_NOT_HALTED;
858 return target->type->add_context_breakpoint(target, breakpoint);
861 int target_add_hybrid_breakpoint(struct target *target,
862 struct breakpoint *breakpoint)
864 if (target->state != TARGET_HALTED) {
865 LOG_WARNING("target %s is not halted", target->cmd_name);
866 return ERROR_TARGET_NOT_HALTED;
868 return target->type->add_hybrid_breakpoint(target, breakpoint);
871 int target_remove_breakpoint(struct target *target,
872 struct breakpoint *breakpoint)
874 return target->type->remove_breakpoint(target, breakpoint);
877 int target_add_watchpoint(struct target *target,
878 struct watchpoint *watchpoint)
880 if (target->state != TARGET_HALTED) {
881 LOG_WARNING("target %s is not halted", target->cmd_name);
882 return ERROR_TARGET_NOT_HALTED;
884 return target->type->add_watchpoint(target, watchpoint);
886 int target_remove_watchpoint(struct target *target,
887 struct watchpoint *watchpoint)
889 return target->type->remove_watchpoint(target, watchpoint);
892 int target_get_gdb_reg_list(struct target *target,
893 struct reg **reg_list[], int *reg_list_size)
895 return target->type->get_gdb_reg_list(target, reg_list, reg_list_size);
897 int target_step(struct target *target,
898 int current, uint32_t address, int handle_breakpoints)
900 return target->type->step(target, current, address, handle_breakpoints);
905 * Reset the @c examined flag for the given target.
906 * Pure paranoia -- targets are zeroed on allocation.
908 static void target_reset_examined(struct target *target)
910 target->examined = false;
914 err_read_phys_memory(struct target *target, uint32_t address,
915 uint32_t size, uint32_t count, uint8_t *buffer)
917 LOG_ERROR("Not implemented: %s", __func__);
922 err_write_phys_memory(struct target *target, uint32_t address,
923 uint32_t size, uint32_t count, const uint8_t *buffer)
925 LOG_ERROR("Not implemented: %s", __func__);
929 static int handle_target(void *priv);
931 static int target_init_one(struct command_context *cmd_ctx,
932 struct target *target)
934 target_reset_examined(target);
936 struct target_type *type = target->type;
937 if (type->examine == NULL)
938 type->examine = default_examine;
940 if (type->check_reset== NULL)
941 type->check_reset = default_check_reset;
943 int retval = type->init_target(cmd_ctx, target);
944 if (ERROR_OK != retval)
946 LOG_ERROR("target '%s' init failed", target_name(target));
951 * @todo get rid of those *memory_imp() methods, now that all
952 * callers are using target_*_memory() accessors ... and make
953 * sure the "physical" paths handle the same issues.
955 /* a non-invasive way(in terms of patches) to add some code that
956 * runs before the type->write/read_memory implementation
958 type->write_memory_imp = target->type->write_memory;
959 type->write_memory = target_write_memory_imp;
961 type->read_memory_imp = target->type->read_memory;
962 type->read_memory = target_read_memory_imp;
964 type->soft_reset_halt_imp = target->type->soft_reset_halt;
965 type->soft_reset_halt = target_soft_reset_halt_imp;
967 /* Sanity-check MMU support ... stub in what we must, to help
968 * implement it in stages, but warn if we need to do so.
972 if (type->write_phys_memory == NULL)
974 LOG_ERROR("type '%s' is missing write_phys_memory",
976 type->write_phys_memory = err_write_phys_memory;
978 if (type->read_phys_memory == NULL)
980 LOG_ERROR("type '%s' is missing read_phys_memory",
982 type->read_phys_memory = err_read_phys_memory;
984 if (type->virt2phys == NULL)
986 LOG_ERROR("type '%s' is missing virt2phys", type->name);
987 type->virt2phys = identity_virt2phys;
992 /* Make sure no-MMU targets all behave the same: make no
993 * distinction between physical and virtual addresses, and
994 * ensure that virt2phys() is always an identity mapping.
996 if (type->write_phys_memory || type->read_phys_memory
999 LOG_WARNING("type '%s' has bad MMU hooks", type->name);
1003 type->write_phys_memory = type->write_memory;
1004 type->read_phys_memory = type->read_memory;
1005 type->virt2phys = identity_virt2phys;
1008 if (target->type->read_buffer == NULL)
1009 target->type->read_buffer = target_read_buffer_default;
1011 if (target->type->write_buffer == NULL)
1012 target->type->write_buffer = target_write_buffer_default;
1017 static int target_init(struct command_context *cmd_ctx)
1019 struct target *target;
1022 for (target = all_targets; target; target = target->next)
1024 retval = target_init_one(cmd_ctx, target);
1025 if (ERROR_OK != retval)
1032 retval = target_register_user_commands(cmd_ctx);
1033 if (ERROR_OK != retval)
1036 retval = target_register_timer_callback(&handle_target,
1037 polling_interval, 1, cmd_ctx->interp);
1038 if (ERROR_OK != retval)
1044 COMMAND_HANDLER(handle_target_init_command)
1047 return ERROR_COMMAND_SYNTAX_ERROR;
1049 static bool target_initialized = false;
1050 if (target_initialized)
1052 LOG_INFO("'target init' has already been called");
1055 target_initialized = true;
1057 LOG_DEBUG("Initializing targets...");
1058 return target_init(CMD_CTX);
1061 int target_register_event_callback(int (*callback)(struct target *target, enum target_event event, void *priv), void *priv)
1063 struct target_event_callback **callbacks_p = &target_event_callbacks;
1065 if (callback == NULL)
1067 return ERROR_INVALID_ARGUMENTS;
1072 while ((*callbacks_p)->next)
1073 callbacks_p = &((*callbacks_p)->next);
1074 callbacks_p = &((*callbacks_p)->next);
1077 (*callbacks_p) = malloc(sizeof(struct target_event_callback));
1078 (*callbacks_p)->callback = callback;
1079 (*callbacks_p)->priv = priv;
1080 (*callbacks_p)->next = NULL;
1085 int target_register_timer_callback(int (*callback)(void *priv), int time_ms, int periodic, void *priv)
1087 struct target_timer_callback **callbacks_p = &target_timer_callbacks;
1090 if (callback == NULL)
1092 return ERROR_INVALID_ARGUMENTS;
1097 while ((*callbacks_p)->next)
1098 callbacks_p = &((*callbacks_p)->next);
1099 callbacks_p = &((*callbacks_p)->next);
1102 (*callbacks_p) = malloc(sizeof(struct target_timer_callback));
1103 (*callbacks_p)->callback = callback;
1104 (*callbacks_p)->periodic = periodic;
1105 (*callbacks_p)->time_ms = time_ms;
1107 gettimeofday(&now, NULL);
1108 (*callbacks_p)->when.tv_usec = now.tv_usec + (time_ms % 1000) * 1000;
1109 time_ms -= (time_ms % 1000);
1110 (*callbacks_p)->when.tv_sec = now.tv_sec + (time_ms / 1000);
1111 if ((*callbacks_p)->when.tv_usec > 1000000)
1113 (*callbacks_p)->when.tv_usec = (*callbacks_p)->when.tv_usec - 1000000;
1114 (*callbacks_p)->when.tv_sec += 1;
1117 (*callbacks_p)->priv = priv;
1118 (*callbacks_p)->next = NULL;
1123 int target_unregister_event_callback(int (*callback)(struct target *target, enum target_event event, void *priv), void *priv)
1125 struct target_event_callback **p = &target_event_callbacks;
1126 struct target_event_callback *c = target_event_callbacks;
1128 if (callback == NULL)
1130 return ERROR_INVALID_ARGUMENTS;
1135 struct target_event_callback *next = c->next;
1136 if ((c->callback == callback) && (c->priv == priv))
1150 static int target_unregister_timer_callback(int (*callback)(void *priv), void *priv)
1152 struct target_timer_callback **p = &target_timer_callbacks;
1153 struct target_timer_callback *c = target_timer_callbacks;
1155 if (callback == NULL)
1157 return ERROR_INVALID_ARGUMENTS;
1162 struct target_timer_callback *next = c->next;
1163 if ((c->callback == callback) && (c->priv == priv))
1177 int target_call_event_callbacks(struct target *target, enum target_event event)
1179 struct target_event_callback *callback = target_event_callbacks;
1180 struct target_event_callback *next_callback;
1182 if (event == TARGET_EVENT_HALTED)
1184 /* execute early halted first */
1185 target_call_event_callbacks(target, TARGET_EVENT_GDB_HALT);
1188 LOG_DEBUG("target event %i (%s)",
1190 Jim_Nvp_value2name_simple(nvp_target_event, event)->name);
1192 target_handle_event(target, event);
1196 next_callback = callback->next;
1197 callback->callback(target, event, callback->priv);
1198 callback = next_callback;
1204 static int target_timer_callback_periodic_restart(
1205 struct target_timer_callback *cb, struct timeval *now)
1207 int time_ms = cb->time_ms;
1208 cb->when.tv_usec = now->tv_usec + (time_ms % 1000) * 1000;
1209 time_ms -= (time_ms % 1000);
1210 cb->when.tv_sec = now->tv_sec + time_ms / 1000;
1211 if (cb->when.tv_usec > 1000000)
1213 cb->when.tv_usec = cb->when.tv_usec - 1000000;
1214 cb->when.tv_sec += 1;
1219 static int target_call_timer_callback(struct target_timer_callback *cb,
1220 struct timeval *now)
1222 cb->callback(cb->priv);
1225 return target_timer_callback_periodic_restart(cb, now);
1227 return target_unregister_timer_callback(cb->callback, cb->priv);
1230 static int target_call_timer_callbacks_check_time(int checktime)
1235 gettimeofday(&now, NULL);
1237 struct target_timer_callback *callback = target_timer_callbacks;
1240 // cleaning up may unregister and free this callback
1241 struct target_timer_callback *next_callback = callback->next;
1243 bool call_it = callback->callback &&
1244 ((!checktime && callback->periodic) ||
1245 now.tv_sec > callback->when.tv_sec ||
1246 (now.tv_sec == callback->when.tv_sec &&
1247 now.tv_usec >= callback->when.tv_usec));
1251 int retval = target_call_timer_callback(callback, &now);
1252 if (retval != ERROR_OK)
1256 callback = next_callback;
1262 int target_call_timer_callbacks(void)
1264 return target_call_timer_callbacks_check_time(1);
1267 /* invoke periodic callbacks immediately */
1268 int target_call_timer_callbacks_now(void)
1270 return target_call_timer_callbacks_check_time(0);
1273 int target_alloc_working_area_try(struct target *target, uint32_t size, struct working_area **area)
1275 struct working_area *c = target->working_areas;
1276 struct working_area *new_wa = NULL;
1278 /* Reevaluate working area address based on MMU state*/
1279 if (target->working_areas == NULL)
1284 retval = target->type->mmu(target, &enabled);
1285 if (retval != ERROR_OK)
1291 if (target->working_area_phys_spec) {
1292 LOG_DEBUG("MMU disabled, using physical "
1293 "address for working memory 0x%08x",
1294 (unsigned)target->working_area_phys);
1295 target->working_area = target->working_area_phys;
1297 LOG_ERROR("No working memory available. "
1298 "Specify -work-area-phys to target.");
1299 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
1302 if (target->working_area_virt_spec) {
1303 LOG_DEBUG("MMU enabled, using virtual "
1304 "address for working memory 0x%08x",
1305 (unsigned)target->working_area_virt);
1306 target->working_area = target->working_area_virt;
1308 LOG_ERROR("No working memory available. "
1309 "Specify -work-area-virt to target.");
1310 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
1315 /* only allocate multiples of 4 byte */
1318 LOG_ERROR("BUG: code tried to allocate unaligned number of bytes (0x%08x), padding", ((unsigned)(size)));
1319 size = (size + 3) & (~3);
1322 /* see if there's already a matching working area */
1325 if ((c->free) && (c->size == size))
1333 /* if not, allocate a new one */
1336 struct working_area **p = &target->working_areas;
1337 uint32_t first_free = target->working_area;
1338 uint32_t free_size = target->working_area_size;
1340 c = target->working_areas;
1343 first_free += c->size;
1344 free_size -= c->size;
1349 if (free_size < size)
1351 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
1354 LOG_DEBUG("allocated new working area at address 0x%08x", (unsigned)first_free);
1356 new_wa = malloc(sizeof(struct working_area));
1357 new_wa->next = NULL;
1358 new_wa->size = size;
1359 new_wa->address = first_free;
1361 if (target->backup_working_area)
1364 new_wa->backup = malloc(new_wa->size);
1365 if ((retval = target_read_memory(target, new_wa->address, 4, new_wa->size / 4, new_wa->backup)) != ERROR_OK)
1367 free(new_wa->backup);
1374 new_wa->backup = NULL;
1377 /* put new entry in list */
1381 /* mark as used, and return the new (reused) area */
1382 new_wa->free = false;
1386 new_wa->user = area;
1391 int target_alloc_working_area(struct target *target, uint32_t size, struct working_area **area)
1395 retval = target_alloc_working_area_try(target, size, area);
1396 if (retval == ERROR_TARGET_RESOURCE_NOT_AVAILABLE)
1398 LOG_WARNING("not enough working area available(requested %u)", (unsigned)(size));
1404 static int target_free_working_area_restore(struct target *target, struct working_area *area, int restore)
1409 if (restore && target->backup_working_area)
1412 if ((retval = target_write_memory(target, area->address, 4, area->size / 4, area->backup)) != ERROR_OK)
1418 /* mark user pointer invalid */
1425 int target_free_working_area(struct target *target, struct working_area *area)
1427 return target_free_working_area_restore(target, area, 1);
1430 /* free resources and restore memory, if restoring memory fails,
1431 * free up resources anyway
1433 static void target_free_all_working_areas_restore(struct target *target, int restore)
1435 struct working_area *c = target->working_areas;
1439 struct working_area *next = c->next;
1440 target_free_working_area_restore(target, c, restore);
1450 target->working_areas = NULL;
1453 void target_free_all_working_areas(struct target *target)
1455 target_free_all_working_areas_restore(target, 1);
1458 int target_arch_state(struct target *target)
1463 LOG_USER("No target has been configured");
1467 LOG_USER("target state: %s", target_state_name( target ));
1469 if (target->state != TARGET_HALTED)
1472 retval = target->type->arch_state(target);
1476 /* Single aligned words are guaranteed to use 16 or 32 bit access
1477 * mode respectively, otherwise data is handled as quickly as
1480 int target_write_buffer(struct target *target, uint32_t address, uint32_t size, const uint8_t *buffer)
1482 LOG_DEBUG("writing buffer of %i byte at 0x%8.8x",
1483 (int)size, (unsigned)address);
1485 if (!target_was_examined(target))
1487 LOG_ERROR("Target not examined yet");
1495 if ((address + size - 1) < address)
1497 /* GDB can request this when e.g. PC is 0xfffffffc*/
1498 LOG_ERROR("address + size wrapped(0x%08x, 0x%08x)",
1504 return target->type->write_buffer(target, address, size, buffer);
1507 static int target_write_buffer_default(struct target *target, uint32_t address, uint32_t size, const uint8_t *buffer)
1509 int retval = ERROR_OK;
1511 if (((address % 2) == 0) && (size == 2))
1513 return target_write_memory(target, address, 2, 1, buffer);
1516 /* handle unaligned head bytes */
1519 uint32_t unaligned = 4 - (address % 4);
1521 if (unaligned > size)
1524 if ((retval = target_write_memory(target, address, 1, unaligned, buffer)) != ERROR_OK)
1527 buffer += unaligned;
1528 address += unaligned;
1532 /* handle aligned words */
1535 int aligned = size - (size % 4);
1537 /* use bulk writes above a certain limit. This may have to be changed */
1540 if ((retval = target->type->bulk_write_memory(target, address, aligned / 4, buffer)) != ERROR_OK)
1545 if ((retval = target_write_memory(target, address, 4, aligned / 4, buffer)) != ERROR_OK)
1554 /* handle tail writes of less than 4 bytes */
1557 if ((retval = target_write_memory(target, address, 1, size, buffer)) != ERROR_OK)
1564 /* Single aligned words are guaranteed to use 16 or 32 bit access
1565 * mode respectively, otherwise data is handled as quickly as
1568 int target_read_buffer(struct target *target, uint32_t address, uint32_t size, uint8_t *buffer)
1570 LOG_DEBUG("reading buffer of %i byte at 0x%8.8x",
1571 (int)size, (unsigned)address);
1573 if (!target_was_examined(target))
1575 LOG_ERROR("Target not examined yet");
1583 if ((address + size - 1) < address)
1585 /* GDB can request this when e.g. PC is 0xfffffffc*/
1586 LOG_ERROR("address + size wrapped(0x%08" PRIx32 ", 0x%08" PRIx32 ")",
1592 return target->type->read_buffer(target, address, size, buffer);
1595 static int target_read_buffer_default(struct target *target, uint32_t address, uint32_t size, uint8_t *buffer)
1597 int retval = ERROR_OK;
1599 if (((address % 2) == 0) && (size == 2))
1601 return target_read_memory(target, address, 2, 1, buffer);
1604 /* handle unaligned head bytes */
1607 uint32_t unaligned = 4 - (address % 4);
1609 if (unaligned > size)
1612 if ((retval = target_read_memory(target, address, 1, unaligned, buffer)) != ERROR_OK)
1615 buffer += unaligned;
1616 address += unaligned;
1620 /* handle aligned words */
1623 int aligned = size - (size % 4);
1625 if ((retval = target_read_memory(target, address, 4, aligned / 4, buffer)) != ERROR_OK)
1633 /*prevent byte access when possible (avoid AHB access limitations in some cases)*/
1636 int aligned = size - (size%2);
1637 retval = target_read_memory(target, address, 2, aligned / 2, buffer);
1638 if (retval != ERROR_OK)
1645 /* handle tail writes of less than 4 bytes */
1648 if ((retval = target_read_memory(target, address, 1, size, buffer)) != ERROR_OK)
1655 int target_checksum_memory(struct target *target, uint32_t address, uint32_t size, uint32_t* crc)
1660 uint32_t checksum = 0;
1661 if (!target_was_examined(target))
1663 LOG_ERROR("Target not examined yet");
1667 if ((retval = target->type->checksum_memory(target, address,
1668 size, &checksum)) != ERROR_OK)
1670 buffer = malloc(size);
1673 LOG_ERROR("error allocating buffer for section (%d bytes)", (int)size);
1674 return ERROR_INVALID_ARGUMENTS;
1676 retval = target_read_buffer(target, address, size, buffer);
1677 if (retval != ERROR_OK)
1683 /* convert to target endianness */
1684 for (i = 0; i < (size/sizeof(uint32_t)); i++)
1686 uint32_t target_data;
1687 target_data = target_buffer_get_u32(target, &buffer[i*sizeof(uint32_t)]);
1688 target_buffer_set_u32(target, &buffer[i*sizeof(uint32_t)], target_data);
1691 retval = image_calculate_checksum(buffer, size, &checksum);
1700 int target_blank_check_memory(struct target *target, uint32_t address, uint32_t size, uint32_t* blank)
1703 if (!target_was_examined(target))
1705 LOG_ERROR("Target not examined yet");
1709 if (target->type->blank_check_memory == 0)
1710 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
1712 retval = target->type->blank_check_memory(target, address, size, blank);
1717 int target_read_u32(struct target *target, uint32_t address, uint32_t *value)
1719 uint8_t value_buf[4];
1720 if (!target_was_examined(target))
1722 LOG_ERROR("Target not examined yet");
1726 int retval = target_read_memory(target, address, 4, 1, value_buf);
1728 if (retval == ERROR_OK)
1730 *value = target_buffer_get_u32(target, value_buf);
1731 LOG_DEBUG("address: 0x%8.8" PRIx32 ", value: 0x%8.8" PRIx32 "",
1738 LOG_DEBUG("address: 0x%8.8" PRIx32 " failed",
1745 int target_read_u16(struct target *target, uint32_t address, uint16_t *value)
1747 uint8_t value_buf[2];
1748 if (!target_was_examined(target))
1750 LOG_ERROR("Target not examined yet");
1754 int retval = target_read_memory(target, address, 2, 1, value_buf);
1756 if (retval == ERROR_OK)
1758 *value = target_buffer_get_u16(target, value_buf);
1759 LOG_DEBUG("address: 0x%8.8" PRIx32 ", value: 0x%4.4x",
1766 LOG_DEBUG("address: 0x%8.8" PRIx32 " failed",
1773 int target_read_u8(struct target *target, uint32_t address, uint8_t *value)
1775 int retval = target_read_memory(target, address, 1, 1, value);
1776 if (!target_was_examined(target))
1778 LOG_ERROR("Target not examined yet");
1782 if (retval == ERROR_OK)
1784 LOG_DEBUG("address: 0x%8.8" PRIx32 ", value: 0x%2.2x",
1791 LOG_DEBUG("address: 0x%8.8" PRIx32 " failed",
1798 int target_write_u32(struct target *target, uint32_t address, uint32_t value)
1801 uint8_t value_buf[4];
1802 if (!target_was_examined(target))
1804 LOG_ERROR("Target not examined yet");
1808 LOG_DEBUG("address: 0x%8.8" PRIx32 ", value: 0x%8.8" PRIx32 "",
1812 target_buffer_set_u32(target, value_buf, value);
1813 if ((retval = target_write_memory(target, address, 4, 1, value_buf)) != ERROR_OK)
1815 LOG_DEBUG("failed: %i", retval);
1821 int target_write_u16(struct target *target, uint32_t address, uint16_t value)
1824 uint8_t value_buf[2];
1825 if (!target_was_examined(target))
1827 LOG_ERROR("Target not examined yet");
1831 LOG_DEBUG("address: 0x%8.8" PRIx32 ", value: 0x%8.8x",
1835 target_buffer_set_u16(target, value_buf, value);
1836 if ((retval = target_write_memory(target, address, 2, 1, value_buf)) != ERROR_OK)
1838 LOG_DEBUG("failed: %i", retval);
1844 int target_write_u8(struct target *target, uint32_t address, uint8_t value)
1847 if (!target_was_examined(target))
1849 LOG_ERROR("Target not examined yet");
1853 LOG_DEBUG("address: 0x%8.8" PRIx32 ", value: 0x%2.2x",
1856 if ((retval = target_write_memory(target, address, 1, 1, &value)) != ERROR_OK)
1858 LOG_DEBUG("failed: %i", retval);
1864 COMMAND_HANDLER(handle_targets_command)
1866 struct target *target = all_targets;
1870 target = get_target(CMD_ARGV[0]);
1871 if (target == NULL) {
1872 command_print(CMD_CTX,"Target: %s is unknown, try one of:\n", CMD_ARGV[0]);
1875 if (!target->tap->enabled) {
1876 command_print(CMD_CTX,"Target: TAP %s is disabled, "
1877 "can't be the current target\n",
1878 target->tap->dotted_name);
1882 CMD_CTX->current_target = target->target_number;
1887 target = all_targets;
1888 command_print(CMD_CTX, " TargetName Type Endian TapName State ");
1889 command_print(CMD_CTX, "-- ------------------ ---------- ------ ------------------ ------------");
1895 if (target->tap->enabled)
1896 state = target_state_name( target );
1898 state = "tap-disabled";
1900 if (CMD_CTX->current_target == target->target_number)
1903 /* keep columns lined up to match the headers above */
1904 command_print(CMD_CTX, "%2d%c %-18s %-10s %-6s %-18s %s",
1905 target->target_number,
1907 target_name(target),
1908 target_type_name(target),
1909 Jim_Nvp_value2name_simple(nvp_target_endian,
1910 target->endianness)->name,
1911 target->tap->dotted_name,
1913 target = target->next;
1919 /* every 300ms we check for reset & powerdropout and issue a "reset halt" if so. */
1921 static int powerDropout;
1922 static int srstAsserted;
1924 static int runPowerRestore;
1925 static int runPowerDropout;
1926 static int runSrstAsserted;
1927 static int runSrstDeasserted;
1929 static int sense_handler(void)
1931 static int prevSrstAsserted = 0;
1932 static int prevPowerdropout = 0;
1935 if ((retval = jtag_power_dropout(&powerDropout)) != ERROR_OK)
1939 powerRestored = prevPowerdropout && !powerDropout;
1942 runPowerRestore = 1;
1945 long long current = timeval_ms();
1946 static long long lastPower = 0;
1947 int waitMore = lastPower + 2000 > current;
1948 if (powerDropout && !waitMore)
1950 runPowerDropout = 1;
1951 lastPower = current;
1954 if ((retval = jtag_srst_asserted(&srstAsserted)) != ERROR_OK)
1958 srstDeasserted = prevSrstAsserted && !srstAsserted;
1960 static long long lastSrst = 0;
1961 waitMore = lastSrst + 2000 > current;
1962 if (srstDeasserted && !waitMore)
1964 runSrstDeasserted = 1;
1968 if (!prevSrstAsserted && srstAsserted)
1970 runSrstAsserted = 1;
1973 prevSrstAsserted = srstAsserted;
1974 prevPowerdropout = powerDropout;
1976 if (srstDeasserted || powerRestored)
1978 /* Other than logging the event we can't do anything here.
1979 * Issuing a reset is a particularly bad idea as we might
1980 * be inside a reset already.
1987 static int backoff_times = 0;
1988 static int backoff_count = 0;
1990 /* process target state changes */
1991 static int handle_target(void *priv)
1993 Jim_Interp *interp = (Jim_Interp *)priv;
1994 int retval = ERROR_OK;
1996 if (!is_jtag_poll_safe())
1998 /* polling is disabled currently */
2002 /* we do not want to recurse here... */
2003 static int recursive = 0;
2008 /* danger! running these procedures can trigger srst assertions and power dropouts.
2009 * We need to avoid an infinite loop/recursion here and we do that by
2010 * clearing the flags after running these events.
2012 int did_something = 0;
2013 if (runSrstAsserted)
2015 LOG_INFO("srst asserted detected, running srst_asserted proc.");
2016 Jim_Eval(interp, "srst_asserted");
2019 if (runSrstDeasserted)
2021 Jim_Eval(interp, "srst_deasserted");
2024 if (runPowerDropout)
2026 LOG_INFO("Power dropout detected, running power_dropout proc.");
2027 Jim_Eval(interp, "power_dropout");
2030 if (runPowerRestore)
2032 Jim_Eval(interp, "power_restore");
2038 /* clear detect flags */
2042 /* clear action flags */
2044 runSrstAsserted = 0;
2045 runSrstDeasserted = 0;
2046 runPowerRestore = 0;
2047 runPowerDropout = 0;
2052 if (backoff_times > backoff_count)
2054 /* do not poll this time as we failed previously */
2060 /* Poll targets for state changes unless that's globally disabled.
2061 * Skip targets that are currently disabled.
2063 for (struct target *target = all_targets;
2064 is_jtag_poll_safe() && target;
2065 target = target->next)
2067 if (!target->tap->enabled)
2070 /* only poll target if we've got power and srst isn't asserted */
2071 if (!powerDropout && !srstAsserted)
2073 /* polling may fail silently until the target has been examined */
2074 if ((retval = target_poll(target)) != ERROR_OK)
2076 /* 100ms polling interval. Increase interval between polling up to 5000ms */
2077 if (backoff_times * polling_interval < 5000)
2082 LOG_USER("Polling target failed, GDB will be halted. Polling again in %dms", backoff_times * polling_interval);
2084 /* Tell GDB to halt the debugger. This allows the user to
2085 * run monitor commands to handle the situation.
2087 target_call_event_callbacks(target, TARGET_EVENT_GDB_HALT);
2090 /* Since we succeeded, we reset backoff count */
2091 if (backoff_times > 0)
2093 LOG_USER("Polling succeeded again");
2102 COMMAND_HANDLER(handle_reg_command)
2104 struct target *target;
2105 struct reg *reg = NULL;
2111 target = get_current_target(CMD_CTX);
2113 /* list all available registers for the current target */
2116 struct reg_cache *cache = target->reg_cache;
2123 command_print(CMD_CTX, "===== %s", cache->name);
2125 for (i = 0, reg = cache->reg_list;
2126 i < cache->num_regs;
2127 i++, reg++, count++)
2129 /* only print cached values if they are valid */
2131 value = buf_to_str(reg->value,
2133 command_print(CMD_CTX,
2134 "(%i) %s (/%" PRIu32 "): 0x%s%s",
2142 command_print(CMD_CTX, "(%i) %s (/%" PRIu32 ")",
2147 cache = cache->next;
2153 /* access a single register by its ordinal number */
2154 if ((CMD_ARGV[0][0] >= '0') && (CMD_ARGV[0][0] <= '9'))
2157 COMMAND_PARSE_NUMBER(uint, CMD_ARGV[0], num);
2159 struct reg_cache *cache = target->reg_cache;
2164 for (i = 0; i < cache->num_regs; i++)
2168 reg = &cache->reg_list[i];
2174 cache = cache->next;
2179 command_print(CMD_CTX, "%i is out of bounds, the current target has only %i registers (0 - %i)", num, count, count - 1);
2182 } else /* access a single register by its name */
2184 reg = register_get_by_name(target->reg_cache, CMD_ARGV[0], 1);
2188 command_print(CMD_CTX, "register %s not found in current target", CMD_ARGV[0]);
2193 /* display a register */
2194 if ((CMD_ARGC == 1) || ((CMD_ARGC == 2) && !((CMD_ARGV[1][0] >= '0') && (CMD_ARGV[1][0] <= '9'))))
2196 if ((CMD_ARGC == 2) && (strcmp(CMD_ARGV[1], "force") == 0))
2199 if (reg->valid == 0)
2201 reg->type->get(reg);
2203 value = buf_to_str(reg->value, reg->size, 16);
2204 command_print(CMD_CTX, "%s (/%i): 0x%s", reg->name, (int)(reg->size), value);
2209 /* set register value */
2212 uint8_t *buf = malloc(DIV_ROUND_UP(reg->size, 8));
2213 str_to_buf(CMD_ARGV[1], strlen(CMD_ARGV[1]), buf, reg->size, 0);
2215 reg->type->set(reg, buf);
2217 value = buf_to_str(reg->value, reg->size, 16);
2218 command_print(CMD_CTX, "%s (/%i): 0x%s", reg->name, (int)(reg->size), value);
2226 command_print(CMD_CTX, "usage: reg <#|name> [value]");
2231 COMMAND_HANDLER(handle_poll_command)
2233 int retval = ERROR_OK;
2234 struct target *target = get_current_target(CMD_CTX);
2238 command_print(CMD_CTX, "background polling: %s",
2239 jtag_poll_get_enabled() ? "on" : "off");
2240 command_print(CMD_CTX, "TAP: %s (%s)",
2241 target->tap->dotted_name,
2242 target->tap->enabled ? "enabled" : "disabled");
2243 if (!target->tap->enabled)
2245 if ((retval = target_poll(target)) != ERROR_OK)
2247 if ((retval = target_arch_state(target)) != ERROR_OK)
2250 else if (CMD_ARGC == 1)
2253 COMMAND_PARSE_ON_OFF(CMD_ARGV[0], enable);
2254 jtag_poll_set_enabled(enable);
2258 return ERROR_COMMAND_SYNTAX_ERROR;
2264 COMMAND_HANDLER(handle_wait_halt_command)
2267 return ERROR_COMMAND_SYNTAX_ERROR;
2272 int retval = parse_uint(CMD_ARGV[0], &ms);
2273 if (ERROR_OK != retval)
2275 command_print(CMD_CTX, "usage: %s [seconds]", CMD_NAME);
2276 return ERROR_COMMAND_SYNTAX_ERROR;
2278 // convert seconds (given) to milliseconds (needed)
2282 struct target *target = get_current_target(CMD_CTX);
2283 return target_wait_state(target, TARGET_HALTED, ms);
2286 /* wait for target state to change. The trick here is to have a low
2287 * latency for short waits and not to suck up all the CPU time
2290 * After 500ms, keep_alive() is invoked
2292 int target_wait_state(struct target *target, enum target_state state, int ms)
2295 long long then = 0, cur;
2300 if ((retval = target_poll(target)) != ERROR_OK)
2302 if (target->state == state)
2310 then = timeval_ms();
2311 LOG_DEBUG("waiting for target %s...",
2312 Jim_Nvp_value2name_simple(nvp_target_state,state)->name);
2320 if ((cur-then) > ms)
2322 LOG_ERROR("timed out while waiting for target %s",
2323 Jim_Nvp_value2name_simple(nvp_target_state,state)->name);
2331 COMMAND_HANDLER(handle_halt_command)
2335 struct target *target = get_current_target(CMD_CTX);
2336 int retval = target_halt(target);
2337 if (ERROR_OK != retval)
2342 unsigned wait_local;
2343 retval = parse_uint(CMD_ARGV[0], &wait_local);
2344 if (ERROR_OK != retval)
2345 return ERROR_COMMAND_SYNTAX_ERROR;
2350 return CALL_COMMAND_HANDLER(handle_wait_halt_command);
2353 COMMAND_HANDLER(handle_soft_reset_halt_command)
2355 struct target *target = get_current_target(CMD_CTX);
2357 LOG_USER("requesting target halt and executing a soft reset");
2359 target->type->soft_reset_halt(target);
2364 COMMAND_HANDLER(handle_reset_command)
2367 return ERROR_COMMAND_SYNTAX_ERROR;
2369 enum target_reset_mode reset_mode = RESET_RUN;
2373 n = Jim_Nvp_name2value_simple(nvp_reset_modes, CMD_ARGV[0]);
2374 if ((n->name == NULL) || (n->value == RESET_UNKNOWN)) {
2375 return ERROR_COMMAND_SYNTAX_ERROR;
2377 reset_mode = n->value;
2380 /* reset *all* targets */
2381 return target_process_reset(CMD_CTX, reset_mode);
2385 COMMAND_HANDLER(handle_resume_command)
2389 return ERROR_COMMAND_SYNTAX_ERROR;
2391 struct target *target = get_current_target(CMD_CTX);
2392 target_handle_event(target, TARGET_EVENT_OLD_pre_resume);
2394 /* with no CMD_ARGV, resume from current pc, addr = 0,
2395 * with one arguments, addr = CMD_ARGV[0],
2396 * handle breakpoints, not debugging */
2400 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], addr);
2404 return target_resume(target, current, addr, 1, 0);
2407 COMMAND_HANDLER(handle_step_command)
2410 return ERROR_COMMAND_SYNTAX_ERROR;
2414 /* with no CMD_ARGV, step from current pc, addr = 0,
2415 * with one argument addr = CMD_ARGV[0],
2416 * handle breakpoints, debugging */
2421 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], addr);
2425 struct target *target = get_current_target(CMD_CTX);
2427 return target->type->step(target, current_pc, addr, 1);
2430 static void handle_md_output(struct command_context *cmd_ctx,
2431 struct target *target, uint32_t address, unsigned size,
2432 unsigned count, const uint8_t *buffer)
2434 const unsigned line_bytecnt = 32;
2435 unsigned line_modulo = line_bytecnt / size;
2437 char output[line_bytecnt * 4 + 1];
2438 unsigned output_len = 0;
2440 const char *value_fmt;
2442 case 4: value_fmt = "%8.8x "; break;
2443 case 2: value_fmt = "%4.4x "; break;
2444 case 1: value_fmt = "%2.2x "; break;
2446 /* "can't happen", caller checked */
2447 LOG_ERROR("invalid memory read size: %u", size);
2451 for (unsigned i = 0; i < count; i++)
2453 if (i % line_modulo == 0)
2455 output_len += snprintf(output + output_len,
2456 sizeof(output) - output_len,
2458 (unsigned)(address + (i*size)));
2462 const uint8_t *value_ptr = buffer + i * size;
2464 case 4: value = target_buffer_get_u32(target, value_ptr); break;
2465 case 2: value = target_buffer_get_u16(target, value_ptr); break;
2466 case 1: value = *value_ptr;
2468 output_len += snprintf(output + output_len,
2469 sizeof(output) - output_len,
2472 if ((i % line_modulo == line_modulo - 1) || (i == count - 1))
2474 command_print(cmd_ctx, "%s", output);
2480 COMMAND_HANDLER(handle_md_command)
2483 return ERROR_COMMAND_SYNTAX_ERROR;
2486 switch (CMD_NAME[2]) {
2487 case 'w': size = 4; break;
2488 case 'h': size = 2; break;
2489 case 'b': size = 1; break;
2490 default: return ERROR_COMMAND_SYNTAX_ERROR;
2493 bool physical=strcmp(CMD_ARGV[0], "phys")==0;
2494 int (*fn)(struct target *target,
2495 uint32_t address, uint32_t size_value, uint32_t count, uint8_t *buffer);
2500 fn=target_read_phys_memory;
2503 fn=target_read_memory;
2505 if ((CMD_ARGC < 1) || (CMD_ARGC > 2))
2507 return ERROR_COMMAND_SYNTAX_ERROR;
2511 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], address);
2515 COMMAND_PARSE_NUMBER(uint, CMD_ARGV[1], count);
2517 uint8_t *buffer = calloc(count, size);
2519 struct target *target = get_current_target(CMD_CTX);
2520 int retval = fn(target, address, size, count, buffer);
2521 if (ERROR_OK == retval)
2522 handle_md_output(CMD_CTX, target, address, size, count, buffer);
2529 typedef int (*target_write_fn)(struct target *target,
2530 uint32_t address, uint32_t size, uint32_t count, const uint8_t *buffer);
2532 static int target_write_memory_fast(struct target *target,
2533 uint32_t address, uint32_t size, uint32_t count, const uint8_t *buffer)
2535 return target_write_buffer(target, address, size * count, buffer);
2538 static int target_fill_mem(struct target *target,
2547 /* We have to write in reasonably large chunks to be able
2548 * to fill large memory areas with any sane speed */
2549 const unsigned chunk_size = 16384;
2550 uint8_t *target_buf = malloc(chunk_size * data_size);
2551 if (target_buf == NULL)
2553 LOG_ERROR("Out of memory");
2557 for (unsigned i = 0; i < chunk_size; i ++)
2562 target_buffer_set_u32(target, target_buf + i*data_size, b);
2565 target_buffer_set_u16(target, target_buf + i*data_size, b);
2568 target_buffer_set_u8(target, target_buf + i*data_size, b);
2575 int retval = ERROR_OK;
2577 for (unsigned x = 0; x < c; x += chunk_size)
2581 if (current > chunk_size)
2583 current = chunk_size;
2585 retval = fn(target, address + x * data_size, data_size, current, target_buf);
2586 if (retval != ERROR_OK)
2590 /* avoid GDB timeouts */
2599 COMMAND_HANDLER(handle_mw_command)
2603 return ERROR_COMMAND_SYNTAX_ERROR;
2605 bool physical=strcmp(CMD_ARGV[0], "phys")==0;
2611 fn=target_write_phys_memory;
2614 fn = target_write_memory_fast;
2616 if ((CMD_ARGC < 2) || (CMD_ARGC > 3))
2617 return ERROR_COMMAND_SYNTAX_ERROR;
2620 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], address);
2623 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], value);
2627 COMMAND_PARSE_NUMBER(uint, CMD_ARGV[2], count);
2629 struct target *target = get_current_target(CMD_CTX);
2631 switch (CMD_NAME[2])
2643 return ERROR_COMMAND_SYNTAX_ERROR;
2646 return target_fill_mem(target, address, fn, wordsize, value, count);
2649 static COMMAND_HELPER(parse_load_image_command_CMD_ARGV, struct image *image,
2650 uint32_t *min_address, uint32_t *max_address)
2652 if (CMD_ARGC < 1 || CMD_ARGC > 5)
2653 return ERROR_COMMAND_SYNTAX_ERROR;
2655 /* a base address isn't always necessary,
2656 * default to 0x0 (i.e. don't relocate) */
2660 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], addr);
2661 image->base_address = addr;
2662 image->base_address_set = 1;
2665 image->base_address_set = 0;
2667 image->start_address_set = 0;
2671 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[3], *min_address);
2675 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[4], *max_address);
2676 // use size (given) to find max (required)
2677 *max_address += *min_address;
2680 if (*min_address > *max_address)
2681 return ERROR_COMMAND_SYNTAX_ERROR;
2686 COMMAND_HANDLER(handle_load_image_command)
2690 uint32_t image_size;
2691 uint32_t min_address = 0;
2692 uint32_t max_address = 0xffffffff;
2696 int retval = CALL_COMMAND_HANDLER(parse_load_image_command_CMD_ARGV,
2697 &image, &min_address, &max_address);
2698 if (ERROR_OK != retval)
2701 struct target *target = get_current_target(CMD_CTX);
2703 struct duration bench;
2704 duration_start(&bench);
2706 if (image_open(&image, CMD_ARGV[0], (CMD_ARGC >= 3) ? CMD_ARGV[2] : NULL) != ERROR_OK)
2713 for (i = 0; i < image.num_sections; i++)
2715 buffer = malloc(image.sections[i].size);
2718 command_print(CMD_CTX,
2719 "error allocating buffer for section (%d bytes)",
2720 (int)(image.sections[i].size));
2724 if ((retval = image_read_section(&image, i, 0x0, image.sections[i].size, buffer, &buf_cnt)) != ERROR_OK)
2730 uint32_t offset = 0;
2731 uint32_t length = buf_cnt;
2733 /* DANGER!!! beware of unsigned comparision here!!! */
2735 if ((image.sections[i].base_address + buf_cnt >= min_address)&&
2736 (image.sections[i].base_address < max_address))
2738 if (image.sections[i].base_address < min_address)
2740 /* clip addresses below */
2741 offset += min_address-image.sections[i].base_address;
2745 if (image.sections[i].base_address + buf_cnt > max_address)
2747 length -= (image.sections[i].base_address + buf_cnt)-max_address;
2750 if ((retval = target_write_buffer(target, image.sections[i].base_address + offset, length, buffer + offset)) != ERROR_OK)
2755 image_size += length;
2756 command_print(CMD_CTX, "%u bytes written at address 0x%8.8" PRIx32 "",
2757 (unsigned int)length,
2758 image.sections[i].base_address + offset);
2764 if ((ERROR_OK == retval) && (duration_measure(&bench) == ERROR_OK))
2766 command_print(CMD_CTX, "downloaded %" PRIu32 " bytes "
2767 "in %fs (%0.3f KiB/s)", image_size,
2768 duration_elapsed(&bench), duration_kbps(&bench, image_size));
2771 image_close(&image);
2777 COMMAND_HANDLER(handle_dump_image_command)
2779 struct fileio fileio;
2780 uint8_t buffer[560];
2781 int retval, retvaltemp;
2782 uint32_t address, size;
2783 struct duration bench;
2784 struct target *target = get_current_target(CMD_CTX);
2787 return ERROR_COMMAND_SYNTAX_ERROR;
2789 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], address);
2790 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[2], size);
2792 retval = fileio_open(&fileio, CMD_ARGV[0], FILEIO_WRITE, FILEIO_BINARY);
2793 if (retval != ERROR_OK)
2796 duration_start(&bench);
2801 size_t size_written;
2802 uint32_t this_run_size = (size > 560) ? 560 : size;
2803 retval = target_read_buffer(target, address, this_run_size, buffer);
2804 if (retval != ERROR_OK)
2809 retval = fileio_write(&fileio, this_run_size, buffer, &size_written);
2810 if (retval != ERROR_OK)
2815 size -= this_run_size;
2816 address += this_run_size;
2819 if ((ERROR_OK == retval) && (duration_measure(&bench) == ERROR_OK))
2822 retval = fileio_size(&fileio, &filesize);
2823 if (retval != ERROR_OK)
2825 command_print(CMD_CTX,
2826 "dumped %ld bytes in %fs (%0.3f KiB/s)", (long)filesize,
2827 duration_elapsed(&bench), duration_kbps(&bench, filesize));
2830 if ((retvaltemp = fileio_close(&fileio)) != ERROR_OK)
2836 static COMMAND_HELPER(handle_verify_image_command_internal, int verify)
2840 uint32_t image_size;
2843 uint32_t checksum = 0;
2844 uint32_t mem_checksum = 0;
2848 struct target *target = get_current_target(CMD_CTX);
2852 return ERROR_COMMAND_SYNTAX_ERROR;
2857 LOG_ERROR("no target selected");
2861 struct duration bench;
2862 duration_start(&bench);
2867 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], addr);
2868 image.base_address = addr;
2869 image.base_address_set = 1;
2873 image.base_address_set = 0;
2874 image.base_address = 0x0;
2877 image.start_address_set = 0;
2879 if ((retval = image_open(&image, CMD_ARGV[0], (CMD_ARGC == 3) ? CMD_ARGV[2] : NULL)) != ERROR_OK)
2887 for (i = 0; i < image.num_sections; i++)
2889 buffer = malloc(image.sections[i].size);
2892 command_print(CMD_CTX,
2893 "error allocating buffer for section (%d bytes)",
2894 (int)(image.sections[i].size));
2897 if ((retval = image_read_section(&image, i, 0x0, image.sections[i].size, buffer, &buf_cnt)) != ERROR_OK)
2905 /* calculate checksum of image */
2906 retval = image_calculate_checksum(buffer, buf_cnt, &checksum);
2907 if (retval != ERROR_OK)
2913 retval = target_checksum_memory(target, image.sections[i].base_address, buf_cnt, &mem_checksum);
2914 if (retval != ERROR_OK)
2920 if (checksum != mem_checksum)
2922 /* failed crc checksum, fall back to a binary compare */
2927 LOG_ERROR("checksum mismatch - attempting binary compare");
2930 data = (uint8_t*)malloc(buf_cnt);
2932 /* Can we use 32bit word accesses? */
2934 int count = buf_cnt;
2935 if ((count % 4) == 0)
2940 retval = target_read_memory(target, image.sections[i].base_address, size, count, data);
2941 if (retval == ERROR_OK)
2944 for (t = 0; t < buf_cnt; t++)
2946 if (data[t] != buffer[t])
2948 command_print(CMD_CTX,
2949 "diff %d address 0x%08x. Was 0x%02x instead of 0x%02x",
2951 (unsigned)(t + image.sections[i].base_address),
2956 command_print(CMD_CTX, "More than 128 errors, the rest are not printed.");
2969 command_print(CMD_CTX, "address 0x%08" PRIx32 " length 0x%08zx",
2970 image.sections[i].base_address,
2975 image_size += buf_cnt;
2979 command_print(CMD_CTX, "No more differences found.");
2984 retval = ERROR_FAIL;
2986 if ((ERROR_OK == retval) && (duration_measure(&bench) == ERROR_OK))
2988 command_print(CMD_CTX, "verified %" PRIu32 " bytes "
2989 "in %fs (%0.3f KiB/s)", image_size,
2990 duration_elapsed(&bench), duration_kbps(&bench, image_size));
2993 image_close(&image);
2998 COMMAND_HANDLER(handle_verify_image_command)
3000 return CALL_COMMAND_HANDLER(handle_verify_image_command_internal, 1);
3003 COMMAND_HANDLER(handle_test_image_command)
3005 return CALL_COMMAND_HANDLER(handle_verify_image_command_internal, 0);
3008 static int handle_bp_command_list(struct command_context *cmd_ctx)
3010 struct target *target = get_current_target(cmd_ctx);
3011 struct breakpoint *breakpoint = target->breakpoints;
3014 if (breakpoint->type == BKPT_SOFT)
3016 char* buf = buf_to_str(breakpoint->orig_instr,
3017 breakpoint->length, 16);
3018 command_print(cmd_ctx, "IVA breakpoint: 0x%8.8" PRIx32 ", 0x%x, %i, 0x%s",
3019 breakpoint->address,
3021 breakpoint->set, buf);
3026 if ((breakpoint->address == 0) && (breakpoint->asid != 0))
3027 command_print(cmd_ctx, "Context breakpoint: 0x%8.8" PRIx32 ", 0x%x, %i",
3029 breakpoint->length, breakpoint->set);
3030 else if ((breakpoint->address != 0) && (breakpoint->asid != 0))
3032 command_print(cmd_ctx, "Hybrid breakpoint(IVA): 0x%8.8" PRIx32 ", 0x%x, %i",
3033 breakpoint->address,
3034 breakpoint->length, breakpoint->set);
3035 command_print(cmd_ctx, "\t|--->linked with ContextID: 0x%8.8" PRIx32,
3039 command_print(cmd_ctx, "Breakpoint(IVA): 0x%8.8" PRIx32 ", 0x%x, %i",
3040 breakpoint->address,
3041 breakpoint->length, breakpoint->set);
3044 breakpoint = breakpoint->next;
3049 static int handle_bp_command_set(struct command_context *cmd_ctx,
3050 uint32_t addr, uint32_t asid, uint32_t length, int hw)
3052 struct target *target = get_current_target(cmd_ctx);
3055 { int retval = breakpoint_add(target, addr, length, hw);
3056 if (ERROR_OK == retval)
3057 command_print(cmd_ctx, "breakpoint set at 0x%8.8" PRIx32 "", addr);
3060 LOG_ERROR("Failure setting breakpoint, the same address(IVA) is already used");
3066 int retval = context_breakpoint_add(target, asid, length, hw);
3067 if (ERROR_OK == retval)
3068 command_print(cmd_ctx, "Context breakpoint set at 0x%8.8" PRIx32 "", asid);
3071 LOG_ERROR("Failure setting breakpoint, the same address(CONTEXTID) is already used");
3077 int retval = hybrid_breakpoint_add(target, addr, asid, length, hw);
3078 if(ERROR_OK == retval)
3079 command_print(cmd_ctx, "Hybrid breakpoint set at 0x%8.8" PRIx32 "", asid);
3082 LOG_ERROR("Failure setting breakpoint, the same address is already used");
3091 COMMAND_HANDLER(handle_bp_command)
3100 return handle_bp_command_list(CMD_CTX);
3103 if(strcmp(CMD_ARGV[2], "hw") == 0)
3106 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], addr);
3108 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], length);
3111 return handle_bp_command_set(CMD_CTX, addr, asid, length, hw);
3113 else if(strcmp(CMD_ARGV[2], "hw_ctx") == 0)
3116 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], asid);
3117 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], length);
3119 return handle_bp_command_set(CMD_CTX, addr, asid, length, hw);
3124 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], addr);
3125 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], asid);
3126 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[2], length);
3127 return handle_bp_command_set(CMD_CTX, addr, asid, length, hw);
3129 command_print(CMD_CTX, "usage: bp <address> [<asid>]<length> ['hw'|'hw_ctx']");
3130 return ERROR_COMMAND_SYNTAX_ERROR;
3136 COMMAND_HANDLER(handle_rbp_command)
3139 return ERROR_COMMAND_SYNTAX_ERROR;
3142 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], addr);
3144 struct target *target = get_current_target(CMD_CTX);
3145 breakpoint_remove(target, addr);
3150 COMMAND_HANDLER(handle_wp_command)
3152 struct target *target = get_current_target(CMD_CTX);
3156 struct watchpoint *watchpoint = target->watchpoints;
3160 command_print(CMD_CTX, "address: 0x%8.8" PRIx32
3161 ", len: 0x%8.8" PRIx32
3162 ", r/w/a: %i, value: 0x%8.8" PRIx32
3163 ", mask: 0x%8.8" PRIx32,
3164 watchpoint->address,
3166 (int)watchpoint->rw,
3169 watchpoint = watchpoint->next;
3174 enum watchpoint_rw type = WPT_ACCESS;
3176 uint32_t length = 0;
3177 uint32_t data_value = 0x0;
3178 uint32_t data_mask = 0xffffffff;
3183 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[4], data_mask);
3186 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[3], data_value);
3189 switch (CMD_ARGV[2][0])
3201 LOG_ERROR("invalid watchpoint mode ('%c')", CMD_ARGV[2][0]);
3202 return ERROR_COMMAND_SYNTAX_ERROR;
3206 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], length);
3207 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], addr);
3211 command_print(CMD_CTX, "usage: wp [address length "
3212 "[(r|w|a) [value [mask]]]]");
3213 return ERROR_COMMAND_SYNTAX_ERROR;
3216 int retval = watchpoint_add(target, addr, length, type,
3217 data_value, data_mask);
3218 if (ERROR_OK != retval)
3219 LOG_ERROR("Failure setting watchpoints");
3224 COMMAND_HANDLER(handle_rwp_command)
3227 return ERROR_COMMAND_SYNTAX_ERROR;
3230 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], addr);
3232 struct target *target = get_current_target(CMD_CTX);
3233 watchpoint_remove(target, addr);
3240 * Translate a virtual address to a physical address.
3242 * The low-level target implementation must have logged a detailed error
3243 * which is forwarded to telnet/GDB session.
3245 COMMAND_HANDLER(handle_virt2phys_command)
3248 return ERROR_COMMAND_SYNTAX_ERROR;
3251 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], va);
3254 struct target *target = get_current_target(CMD_CTX);
3255 int retval = target->type->virt2phys(target, va, &pa);
3256 if (retval == ERROR_OK)
3257 command_print(CMD_CTX, "Physical address 0x%08" PRIx32 "", pa);
3262 static void writeData(FILE *f, const void *data, size_t len)
3264 size_t written = fwrite(data, 1, len, f);
3266 LOG_ERROR("failed to write %zu bytes: %s", len, strerror(errno));
3269 static void writeLong(FILE *f, int l)
3272 for (i = 0; i < 4; i++)
3274 char c = (l >> (i*8))&0xff;
3275 writeData(f, &c, 1);
3280 static void writeString(FILE *f, char *s)
3282 writeData(f, s, strlen(s));
3285 /* Dump a gmon.out histogram file. */
3286 static void writeGmon(uint32_t *samples, uint32_t sampleNum, const char *filename)
3289 FILE *f = fopen(filename, "w");
3292 writeString(f, "gmon");
3293 writeLong(f, 0x00000001); /* Version */
3294 writeLong(f, 0); /* padding */
3295 writeLong(f, 0); /* padding */
3296 writeLong(f, 0); /* padding */
3298 uint8_t zero = 0; /* GMON_TAG_TIME_HIST */
3299 writeData(f, &zero, 1);
3301 /* figure out bucket size */
3302 uint32_t min = samples[0];
3303 uint32_t max = samples[0];
3304 for (i = 0; i < sampleNum; i++)
3306 if (min > samples[i])
3310 if (max < samples[i])
3316 int addressSpace = (max-min + 1);
3318 static const uint32_t maxBuckets = 16 * 1024; /* maximum buckets. */
3319 uint32_t length = addressSpace;
3320 if (length > maxBuckets)
3322 length = maxBuckets;
3324 int *buckets = malloc(sizeof(int)*length);
3325 if (buckets == NULL)
3330 memset(buckets, 0, sizeof(int)*length);
3331 for (i = 0; i < sampleNum;i++)
3333 uint32_t address = samples[i];
3334 long long a = address-min;
3335 long long b = length-1;
3336 long long c = addressSpace-1;
3337 int index_t = (a*b)/c; /* danger!!!! int32 overflows */
3341 /* append binary memory gmon.out &profile_hist_hdr ((char*)&profile_hist_hdr + sizeof(struct gmon_hist_hdr)) */
3342 writeLong(f, min); /* low_pc */
3343 writeLong(f, max); /* high_pc */
3344 writeLong(f, length); /* # of samples */
3345 writeLong(f, 100); /* KLUDGE! We lie, ca. 100Hz best case. */
3346 writeString(f, "seconds");
3347 for (i = 0; i < (15-strlen("seconds")); i++)
3348 writeData(f, &zero, 1);
3349 writeString(f, "s");
3351 /*append binary memory gmon.out profile_hist_data (profile_hist_data + profile_hist_hdr.hist_size) */
3353 char *data = malloc(2*length);
3356 for (i = 0; i < length;i++)
3365 data[i*2 + 1]=(val >> 8)&0xff;
3368 writeData(f, data, length * 2);
3378 /* profiling samples the CPU PC as quickly as OpenOCD is able,
3379 * which will be used as a random sampling of PC */
3380 COMMAND_HANDLER(handle_profile_command)
3382 struct target *target = get_current_target(CMD_CTX);
3383 struct timeval timeout, now;
3385 gettimeofday(&timeout, NULL);
3388 return ERROR_COMMAND_SYNTAX_ERROR;
3391 COMMAND_PARSE_NUMBER(uint, CMD_ARGV[0], offset);
3393 timeval_add_time(&timeout, offset, 0);
3396 * @todo: Some cores let us sample the PC without the
3397 * annoying halt/resume step; for example, ARMv7 PCSR.
3398 * Provide a way to use that more efficient mechanism.
3401 command_print(CMD_CTX, "Starting profiling. Halting and resuming the target as often as we can...");
3403 static const int maxSample = 10000;
3404 uint32_t *samples = malloc(sizeof(uint32_t)*maxSample);
3405 if (samples == NULL)
3409 /* hopefully it is safe to cache! We want to stop/restart as quickly as possible. */
3410 struct reg *reg = register_get_by_name(target->reg_cache, "pc", 1);
3415 target_poll(target);
3416 if (target->state == TARGET_HALTED)
3418 uint32_t t=*((uint32_t *)reg->value);
3419 samples[numSamples++]=t;
3420 retval = target_resume(target, 1, 0, 0, 0); /* current pc, addr = 0, do not handle breakpoints, not debugging */
3421 target_poll(target);
3422 alive_sleep(10); /* sleep 10ms, i.e. <100 samples/second. */
3423 } else if (target->state == TARGET_RUNNING)
3425 /* We want to quickly sample the PC. */
3426 if ((retval = target_halt(target)) != ERROR_OK)
3433 command_print(CMD_CTX, "Target not halted or running");
3437 if (retval != ERROR_OK)
3442 gettimeofday(&now, NULL);
3443 if ((numSamples >= maxSample) || ((now.tv_sec >= timeout.tv_sec) && (now.tv_usec >= timeout.tv_usec)))
3445 command_print(CMD_CTX, "Profiling completed. %d samples.", numSamples);
3446 if ((retval = target_poll(target)) != ERROR_OK)
3451 if (target->state == TARGET_HALTED)
3453 target_resume(target, 1, 0, 0, 0); /* current pc, addr = 0, do not handle breakpoints, not debugging */
3455 if ((retval = target_poll(target)) != ERROR_OK)
3460 writeGmon(samples, numSamples, CMD_ARGV[1]);
3461 command_print(CMD_CTX, "Wrote %s", CMD_ARGV[1]);
3470 static int new_int_array_element(Jim_Interp * interp, const char *varname, int idx, uint32_t val)
3473 Jim_Obj *nameObjPtr, *valObjPtr;
3476 namebuf = alloc_printf("%s(%d)", varname, idx);
3480 nameObjPtr = Jim_NewStringObj(interp, namebuf, -1);
3481 valObjPtr = Jim_NewIntObj(interp, val);
3482 if (!nameObjPtr || !valObjPtr)
3488 Jim_IncrRefCount(nameObjPtr);
3489 Jim_IncrRefCount(valObjPtr);
3490 result = Jim_SetVariable(interp, nameObjPtr, valObjPtr);
3491 Jim_DecrRefCount(interp, nameObjPtr);
3492 Jim_DecrRefCount(interp, valObjPtr);
3494 /* printf("%s(%d) <= 0%08x\n", varname, idx, val); */
3498 static int jim_mem2array(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
3500 struct command_context *context;
3501 struct target *target;
3503 context = current_command_context(interp);
3504 assert (context != NULL);
3506 target = get_current_target(context);
3509 LOG_ERROR("mem2array: no current target");
3513 return target_mem2array(interp, target, argc-1, argv + 1);
3516 static int target_mem2array(Jim_Interp *interp, struct target *target, int argc, Jim_Obj *const *argv)
3524 const char *varname;
3528 /* argv[1] = name of array to receive the data
3529 * argv[2] = desired width
3530 * argv[3] = memory address
3531 * argv[4] = count of times to read
3534 Jim_WrongNumArgs(interp, 1, argv, "varname width addr nelems");
3537 varname = Jim_GetString(argv[0], &len);
3538 /* given "foo" get space for worse case "foo(%d)" .. add 20 */
3540 e = Jim_GetLong(interp, argv[1], &l);
3546 e = Jim_GetLong(interp, argv[2], &l);
3551 e = Jim_GetLong(interp, argv[3], &l);
3567 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3568 Jim_AppendStrings(interp, Jim_GetResult(interp), "Invalid width param, must be 8/16/32", NULL);
3572 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3573 Jim_AppendStrings(interp, Jim_GetResult(interp), "mem2array: zero width read?", NULL);
3576 if ((addr + (len * width)) < addr) {
3577 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3578 Jim_AppendStrings(interp, Jim_GetResult(interp), "mem2array: addr + len - wraps to zero?", NULL);
3581 /* absurd transfer size? */
3583 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3584 Jim_AppendStrings(interp, Jim_GetResult(interp), "mem2array: absurd > 64K item request", NULL);
3589 ((width == 2) && ((addr & 1) == 0)) ||
3590 ((width == 4) && ((addr & 3) == 0))) {
3594 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3595 sprintf(buf, "mem2array address: 0x%08" PRIx32 " is not aligned for %" PRId32 " byte reads",
3598 Jim_AppendStrings(interp, Jim_GetResult(interp), buf , NULL);
3607 size_t buffersize = 4096;
3608 uint8_t *buffer = malloc(buffersize);
3615 /* Slurp... in buffer size chunks */
3617 count = len; /* in objects.. */
3618 if (count > (buffersize/width)) {
3619 count = (buffersize/width);
3622 retval = target_read_memory(target, addr, width, count, buffer);
3623 if (retval != ERROR_OK) {
3625 LOG_ERROR("mem2array: Read @ 0x%08x, w=%d, cnt=%d, failed",
3629 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3630 Jim_AppendStrings(interp, Jim_GetResult(interp), "mem2array: cannot read memory", NULL);
3634 v = 0; /* shut up gcc */
3635 for (i = 0 ;i < count ;i++, n++) {
3638 v = target_buffer_get_u32(target, &buffer[i*width]);
3641 v = target_buffer_get_u16(target, &buffer[i*width]);
3644 v = buffer[i] & 0x0ff;
3647 new_int_array_element(interp, varname, n, v);
3655 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3660 static int get_int_array_element(Jim_Interp * interp, const char *varname, int idx, uint32_t *val)
3663 Jim_Obj *nameObjPtr, *valObjPtr;
3667 namebuf = alloc_printf("%s(%d)", varname, idx);
3671 nameObjPtr = Jim_NewStringObj(interp, namebuf, -1);
3678 Jim_IncrRefCount(nameObjPtr);
3679 valObjPtr = Jim_GetVariable(interp, nameObjPtr, JIM_ERRMSG);
3680 Jim_DecrRefCount(interp, nameObjPtr);
3682 if (valObjPtr == NULL)
3685 result = Jim_GetLong(interp, valObjPtr, &l);
3686 /* printf("%s(%d) => 0%08x\n", varname, idx, val); */
3691 static int jim_array2mem(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
3693 struct command_context *context;
3694 struct target *target;
3696 context = current_command_context(interp);
3697 assert (context != NULL);
3699 target = get_current_target(context);
3700 if (target == NULL) {
3701 LOG_ERROR("array2mem: no current target");
3705 return target_array2mem(interp,target, argc-1, argv + 1);
3708 static int target_array2mem(Jim_Interp *interp, struct target *target,
3709 int argc, Jim_Obj *const *argv)
3717 const char *varname;
3721 /* argv[1] = name of array to get the data
3722 * argv[2] = desired width
3723 * argv[3] = memory address
3724 * argv[4] = count to write
3727 Jim_WrongNumArgs(interp, 0, argv, "varname width addr nelems");
3730 varname = Jim_GetString(argv[0], &len);
3731 /* given "foo" get space for worse case "foo(%d)" .. add 20 */
3733 e = Jim_GetLong(interp, argv[1], &l);
3739 e = Jim_GetLong(interp, argv[2], &l);
3744 e = Jim_GetLong(interp, argv[3], &l);
3760 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3761 Jim_AppendStrings(interp, Jim_GetResult(interp), "Invalid width param, must be 8/16/32", NULL);
3765 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3766 Jim_AppendStrings(interp, Jim_GetResult(interp), "array2mem: zero width read?", NULL);
3769 if ((addr + (len * width)) < addr) {
3770 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3771 Jim_AppendStrings(interp, Jim_GetResult(interp), "array2mem: addr + len - wraps to zero?", NULL);
3774 /* absurd transfer size? */
3776 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3777 Jim_AppendStrings(interp, Jim_GetResult(interp), "array2mem: absurd > 64K item request", NULL);
3782 ((width == 2) && ((addr & 1) == 0)) ||
3783 ((width == 4) && ((addr & 3) == 0))) {
3787 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3788 sprintf(buf, "array2mem address: 0x%08x is not aligned for %d byte reads",
3791 Jim_AppendStrings(interp, Jim_GetResult(interp), buf , NULL);
3802 size_t buffersize = 4096;
3803 uint8_t *buffer = malloc(buffersize);
3808 /* Slurp... in buffer size chunks */
3810 count = len; /* in objects.. */
3811 if (count > (buffersize/width)) {
3812 count = (buffersize/width);
3815 v = 0; /* shut up gcc */
3816 for (i = 0 ;i < count ;i++, n++) {
3817 get_int_array_element(interp, varname, n, &v);
3820 target_buffer_set_u32(target, &buffer[i*width], v);
3823 target_buffer_set_u16(target, &buffer[i*width], v);
3826 buffer[i] = v & 0x0ff;
3832 retval = target_write_memory(target, addr, width, count, buffer);
3833 if (retval != ERROR_OK) {
3835 LOG_ERROR("array2mem: Write @ 0x%08x, w=%d, cnt=%d, failed",
3839 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3840 Jim_AppendStrings(interp, Jim_GetResult(interp), "array2mem: cannot read memory", NULL);
3848 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3853 /* FIX? should we propagate errors here rather than printing them
3856 void target_handle_event(struct target *target, enum target_event e)
3858 struct target_event_action *teap;
3860 for (teap = target->event_action; teap != NULL; teap = teap->next) {
3861 if (teap->event == e) {
3862 LOG_DEBUG("target: (%d) %s (%s) event: %d (%s) action: %s",
3863 target->target_number,
3864 target_name(target),
3865 target_type_name(target),
3867 Jim_Nvp_value2name_simple(nvp_target_event, e)->name,
3868 Jim_GetString(teap->body, NULL));
3869 if (Jim_EvalObj(teap->interp, teap->body) != JIM_OK)
3871 Jim_MakeErrorMessage(teap->interp);
3872 command_print(NULL,"%s\n", Jim_GetString(Jim_GetResult(teap->interp), NULL));
3879 * Returns true only if the target has a handler for the specified event.
3881 bool target_has_event_action(struct target *target, enum target_event event)
3883 struct target_event_action *teap;
3885 for (teap = target->event_action; teap != NULL; teap = teap->next) {
3886 if (teap->event == event)
3892 enum target_cfg_param {
3895 TCFG_WORK_AREA_VIRT,
3896 TCFG_WORK_AREA_PHYS,
3897 TCFG_WORK_AREA_SIZE,
3898 TCFG_WORK_AREA_BACKUP,
3902 TCFG_CHAIN_POSITION,
3907 static Jim_Nvp nvp_config_opts[] = {
3908 { .name = "-type", .value = TCFG_TYPE },
3909 { .name = "-event", .value = TCFG_EVENT },
3910 { .name = "-work-area-virt", .value = TCFG_WORK_AREA_VIRT },
3911 { .name = "-work-area-phys", .value = TCFG_WORK_AREA_PHYS },
3912 { .name = "-work-area-size", .value = TCFG_WORK_AREA_SIZE },
3913 { .name = "-work-area-backup", .value = TCFG_WORK_AREA_BACKUP },
3914 { .name = "-endian" , .value = TCFG_ENDIAN },
3915 { .name = "-variant", .value = TCFG_VARIANT },
3916 { .name = "-coreid", .value = TCFG_COREID },
3917 { .name = "-chain-position", .value = TCFG_CHAIN_POSITION },
3918 { .name = "-dbgbase", .value = TCFG_DBGBASE },
3919 { .name = "-rtos", .value = TCFG_RTOS },
3920 { .name = NULL, .value = -1 }
3923 static int target_configure(Jim_GetOptInfo *goi, struct target *target)
3931 /* parse config or cget options ... */
3932 while (goi->argc > 0) {
3933 Jim_SetEmptyResult(goi->interp);
3934 /* Jim_GetOpt_Debug(goi); */
3936 if (target->type->target_jim_configure) {
3937 /* target defines a configure function */
3938 /* target gets first dibs on parameters */
3939 e = (*(target->type->target_jim_configure))(target, goi);
3948 /* otherwise we 'continue' below */
3950 e = Jim_GetOpt_Nvp(goi, nvp_config_opts, &n);
3952 Jim_GetOpt_NvpUnknown(goi, nvp_config_opts, 0);
3958 if (goi->isconfigure) {
3959 Jim_SetResultFormatted(goi->interp,
3960 "not settable: %s", n->name);
3964 if (goi->argc != 0) {
3965 Jim_WrongNumArgs(goi->interp,
3966 goi->argc, goi->argv,
3971 Jim_SetResultString(goi->interp,
3972 target_type_name(target), -1);
3976 if (goi->argc == 0) {
3977 Jim_WrongNumArgs(goi->interp, goi->argc, goi->argv, "-event ?event-name? ...");
3981 e = Jim_GetOpt_Nvp(goi, nvp_target_event, &n);
3983 Jim_GetOpt_NvpUnknown(goi, nvp_target_event, 1);
3987 if (goi->isconfigure) {
3988 if (goi->argc != 1) {
3989 Jim_WrongNumArgs(goi->interp, goi->argc, goi->argv, "-event ?event-name? ?EVENT-BODY?");
3993 if (goi->argc != 0) {
3994 Jim_WrongNumArgs(goi->interp, goi->argc, goi->argv, "-event ?event-name?");
4000 struct target_event_action *teap;
4002 teap = target->event_action;
4003 /* replace existing? */
4005 if (teap->event == (enum target_event)n->value) {
4011 if (goi->isconfigure) {
4012 bool replace = true;
4015 teap = calloc(1, sizeof(*teap));
4018 teap->event = n->value;
4019 teap->interp = goi->interp;
4020 Jim_GetOpt_Obj(goi, &o);
4022 Jim_DecrRefCount(teap->interp, teap->body);
4024 teap->body = Jim_DuplicateObj(goi->interp, o);
4027 * Tcl/TK - "tk events" have a nice feature.
4028 * See the "BIND" command.
4029 * We should support that here.
4030 * You can specify %X and %Y in the event code.
4031 * The idea is: %T - target name.
4032 * The idea is: %N - target number
4033 * The idea is: %E - event name.
4035 Jim_IncrRefCount(teap->body);
4039 /* add to head of event list */
4040 teap->next = target->event_action;
4041 target->event_action = teap;
4043 Jim_SetEmptyResult(goi->interp);
4047 Jim_SetEmptyResult(goi->interp);
4049 Jim_SetResult(goi->interp, Jim_DuplicateObj(goi->interp, teap->body));
4056 case TCFG_WORK_AREA_VIRT:
4057 if (goi->isconfigure) {
4058 target_free_all_working_areas(target);
4059 e = Jim_GetOpt_Wide(goi, &w);
4063 target->working_area_virt = w;
4064 target->working_area_virt_spec = true;
4066 if (goi->argc != 0) {
4070 Jim_SetResult(goi->interp, Jim_NewIntObj(goi->interp, target->working_area_virt));
4074 case TCFG_WORK_AREA_PHYS:
4075 if (goi->isconfigure) {
4076 target_free_all_working_areas(target);
4077 e = Jim_GetOpt_Wide(goi, &w);
4081 target->working_area_phys = w;
4082 target->working_area_phys_spec = true;
4084 if (goi->argc != 0) {
4088 Jim_SetResult(goi->interp, Jim_NewIntObj(goi->interp, target->working_area_phys));
4092 case TCFG_WORK_AREA_SIZE:
4093 if (goi->isconfigure) {
4094 target_free_all_working_areas(target);
4095 e = Jim_GetOpt_Wide(goi, &w);
4099 target->working_area_size = w;
4101 if (goi->argc != 0) {
4105 Jim_SetResult(goi->interp, Jim_NewIntObj(goi->interp, target->working_area_size));
4109 case TCFG_WORK_AREA_BACKUP:
4110 if (goi->isconfigure) {
4111 target_free_all_working_areas(target);
4112 e = Jim_GetOpt_Wide(goi, &w);
4116 /* make this exactly 1 or 0 */
4117 target->backup_working_area = (!!w);
4119 if (goi->argc != 0) {
4123 Jim_SetResult(goi->interp, Jim_NewIntObj(goi->interp, target->backup_working_area));
4124 /* loop for more e*/
4129 if (goi->isconfigure) {
4130 e = Jim_GetOpt_Nvp(goi, nvp_target_endian, &n);
4132 Jim_GetOpt_NvpUnknown(goi, nvp_target_endian, 1);
4135 target->endianness = n->value;
4137 if (goi->argc != 0) {
4141 n = Jim_Nvp_value2name_simple(nvp_target_endian, target->endianness);
4142 if (n->name == NULL) {
4143 target->endianness = TARGET_LITTLE_ENDIAN;
4144 n = Jim_Nvp_value2name_simple(nvp_target_endian, target->endianness);
4146 Jim_SetResultString(goi->interp, n->name, -1);
4151 if (goi->isconfigure) {
4152 if (goi->argc < 1) {
4153 Jim_SetResultFormatted(goi->interp,
4158 if (target->variant) {
4159 free((void *)(target->variant));
4161 e = Jim_GetOpt_String(goi, &cp, NULL);
4162 target->variant = strdup(cp);
4164 if (goi->argc != 0) {
4168 Jim_SetResultString(goi->interp, target->variant,-1);
4173 if (goi->isconfigure) {
4174 e = Jim_GetOpt_Wide(goi, &w);
4178 target->coreid = (int32_t)w;
4180 if (goi->argc != 0) {
4184 Jim_SetResult(goi->interp, Jim_NewIntObj(goi->interp, target->working_area_size));
4188 case TCFG_CHAIN_POSITION:
4189 if (goi->isconfigure) {
4191 struct jtag_tap *tap;
4192 target_free_all_working_areas(target);
4193 e = Jim_GetOpt_Obj(goi, &o_t);
4197 tap = jtag_tap_by_jim_obj(goi->interp, o_t);
4201 /* make this exactly 1 or 0 */
4204 if (goi->argc != 0) {
4208 Jim_SetResultString(goi->interp, target->tap->dotted_name, -1);
4209 /* loop for more e*/
4212 if (goi->isconfigure) {
4213 e = Jim_GetOpt_Wide(goi, &w);
4217 target->dbgbase = (uint32_t)w;
4218 target->dbgbase_set = true;
4220 if (goi->argc != 0) {
4224 Jim_SetResult(goi->interp, Jim_NewIntObj(goi->interp, target->dbgbase));
4231 int result = rtos_create( goi, target );
4232 if ( result != JIM_OK )
4240 } /* while (goi->argc) */
4243 /* done - we return */
4248 jim_target_configure(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
4252 Jim_GetOpt_Setup(&goi, interp, argc - 1, argv + 1);
4253 goi.isconfigure = !strcmp(Jim_GetString(argv[0], NULL), "configure");
4254 int need_args = 1 + goi.isconfigure;
4255 if (goi.argc < need_args)
4257 Jim_WrongNumArgs(goi.interp, goi.argc, goi.argv,
4259 ? "missing: -option VALUE ..."
4260 : "missing: -option ...");
4263 struct target *target = Jim_CmdPrivData(goi.interp);
4264 return target_configure(&goi, target);
4267 static int jim_target_mw(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
4269 const char *cmd_name = Jim_GetString(argv[0], NULL);
4272 Jim_GetOpt_Setup(&goi, interp, argc - 1, argv + 1);
4274 if (goi.argc < 2 || goi.argc > 4)
4276 Jim_SetResultFormatted(goi.interp,
4277 "usage: %s [phys] <address> <data> [<count>]", cmd_name);
4282 fn = target_write_memory_fast;
4285 if (strcmp(Jim_GetString(argv[1], NULL), "phys") == 0)
4288 struct Jim_Obj *obj;
4289 e = Jim_GetOpt_Obj(&goi, &obj);
4293 fn = target_write_phys_memory;
4297 e = Jim_GetOpt_Wide(&goi, &a);
4302 e = Jim_GetOpt_Wide(&goi, &b);
4309 e = Jim_GetOpt_Wide(&goi, &c);
4314 /* all args must be consumed */
4320 struct target *target = Jim_CmdPrivData(goi.interp);
4322 if (strcasecmp(cmd_name, "mww") == 0) {
4325 else if (strcasecmp(cmd_name, "mwh") == 0) {
4328 else if (strcasecmp(cmd_name, "mwb") == 0) {
4331 LOG_ERROR("command '%s' unknown: ", cmd_name);
4335 return (target_fill_mem(target, a, fn, data_size, b, c) == ERROR_OK) ? JIM_OK : JIM_ERR;
4338 static int jim_target_md(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
4340 const char *cmd_name = Jim_GetString(argv[0], NULL);
4343 Jim_GetOpt_Setup(&goi, interp, argc - 1, argv + 1);
4345 if ((goi.argc < 1) || (goi.argc > 3))
4347 Jim_SetResultFormatted(goi.interp,
4348 "usage: %s [phys] <address> [<count>]", cmd_name);
4352 int (*fn)(struct target *target,
4353 uint32_t address, uint32_t size, uint32_t count, uint8_t *buffer);
4354 fn=target_read_memory;
4357 if (strcmp(Jim_GetString(argv[1], NULL), "phys") == 0)
4360 struct Jim_Obj *obj;
4361 e = Jim_GetOpt_Obj(&goi, &obj);
4365 fn=target_read_phys_memory;
4369 e = Jim_GetOpt_Wide(&goi, &a);
4374 if (goi.argc == 1) {
4375 e = Jim_GetOpt_Wide(&goi, &c);
4383 /* all args must be consumed */
4389 jim_wide b = 1; /* shut up gcc */
4390 if (strcasecmp(cmd_name, "mdw") == 0)
4392 else if (strcasecmp(cmd_name, "mdh") == 0)
4394 else if (strcasecmp(cmd_name, "mdb") == 0)
4397 LOG_ERROR("command '%s' unknown: ", cmd_name);
4401 /* convert count to "bytes" */
4404 struct target *target = Jim_CmdPrivData(goi.interp);
4405 uint8_t target_buf[32];
4412 e = fn(target, a, b, y / b, target_buf);
4413 if (e != ERROR_OK) {
4415 snprintf(tmp, sizeof(tmp), "%08lx", (long)a);
4416 Jim_SetResultFormatted(interp, "error reading target @ 0x%s", tmp);
4420 command_print(NULL, "0x%08x ", (int)(a));
4423 for (x = 0; x < 16 && x < y; x += 4)
4425 z = target_buffer_get_u32(target, &(target_buf[ x ]));
4426 command_print(NULL, "%08x ", (int)(z));
4428 for (; (x < 16) ; x += 4) {
4429 command_print(NULL, " ");
4433 for (x = 0; x < 16 && x < y; x += 2)
4435 z = target_buffer_get_u16(target, &(target_buf[ x ]));
4436 command_print(NULL, "%04x ", (int)(z));
4438 for (; (x < 16) ; x += 2) {
4439 command_print(NULL, " ");
4444 for (x = 0 ; (x < 16) && (x < y) ; x += 1) {
4445 z = target_buffer_get_u8(target, &(target_buf[ x ]));
4446 command_print(NULL, "%02x ", (int)(z));
4448 for (; (x < 16) ; x += 1) {
4449 command_print(NULL, " ");
4453 /* ascii-ify the bytes */
4454 for (x = 0 ; x < y ; x++) {
4455 if ((target_buf[x] >= 0x20) &&
4456 (target_buf[x] <= 0x7e)) {
4460 target_buf[x] = '.';
4465 target_buf[x] = ' ';
4470 /* print - with a newline */
4471 command_print(NULL, "%s\n", target_buf);
4479 static int jim_target_mem2array(Jim_Interp *interp,
4480 int argc, Jim_Obj *const *argv)
4482 struct target *target = Jim_CmdPrivData(interp);
4483 return target_mem2array(interp, target, argc - 1, argv + 1);
4486 static int jim_target_array2mem(Jim_Interp *interp,
4487 int argc, Jim_Obj *const *argv)
4489 struct target *target = Jim_CmdPrivData(interp);
4490 return target_array2mem(interp, target, argc - 1, argv + 1);
4493 static int jim_target_tap_disabled(Jim_Interp *interp)
4495 Jim_SetResultFormatted(interp, "[TAP is disabled]");
4499 static int jim_target_examine(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
4503 Jim_WrongNumArgs(interp, 1, argv, "[no parameters]");
4506 struct target *target = Jim_CmdPrivData(interp);
4507 if (!target->tap->enabled)
4508 return jim_target_tap_disabled(interp);
4510 int e = target->type->examine(target);
4518 static int jim_target_halt_gdb(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
4522 Jim_WrongNumArgs(interp, 1, argv, "[no parameters]");
4525 struct target *target = Jim_CmdPrivData(interp);
4527 if (target_call_event_callbacks(target, TARGET_EVENT_GDB_HALT) != ERROR_OK)
4533 static int jim_target_poll(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
4537 Jim_WrongNumArgs(interp, 1, argv, "[no parameters]");
4540 struct target *target = Jim_CmdPrivData(interp);
4541 if (!target->tap->enabled)
4542 return jim_target_tap_disabled(interp);
4545 if (!(target_was_examined(target))) {
4546 e = ERROR_TARGET_NOT_EXAMINED;
4548 e = target->type->poll(target);
4557 static int jim_target_reset(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
4560 Jim_GetOpt_Setup(&goi, interp, argc - 1, argv + 1);
4564 Jim_WrongNumArgs(interp, 0, argv,
4565 "([tT]|[fF]|assert|deassert) BOOL");
4570 int e = Jim_GetOpt_Nvp(&goi, nvp_assert, &n);
4573 Jim_GetOpt_NvpUnknown(&goi, nvp_assert, 1);
4576 /* the halt or not param */
4578 e = Jim_GetOpt_Wide(&goi, &a);
4582 struct target *target = Jim_CmdPrivData(goi.interp);
4583 if (!target->tap->enabled)
4584 return jim_target_tap_disabled(interp);
4585 if (!(target_was_examined(target)))
4587 LOG_ERROR("Target not examined yet");
4588 return ERROR_TARGET_NOT_EXAMINED;
4590 if (!target->type->assert_reset || !target->type->deassert_reset)
4592 Jim_SetResultFormatted(interp,
4593 "No target-specific reset for %s",
4594 target_name(target));
4597 /* determine if we should halt or not. */
4598 target->reset_halt = !!a;
4599 /* When this happens - all workareas are invalid. */
4600 target_free_all_working_areas_restore(target, 0);
4603 if (n->value == NVP_ASSERT) {
4604 e = target->type->assert_reset(target);
4606 e = target->type->deassert_reset(target);
4608 return (e == ERROR_OK) ? JIM_OK : JIM_ERR;
4611 static int jim_target_halt(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
4614 Jim_WrongNumArgs(interp, 1, argv, "[no parameters]");
4617 struct target *target = Jim_CmdPrivData(interp);
4618 if (!target->tap->enabled)
4619 return jim_target_tap_disabled(interp);
4620 int e = target->type->halt(target);
4621 return (e == ERROR_OK) ? JIM_OK : JIM_ERR;
4624 static int jim_target_wait_state(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
4627 Jim_GetOpt_Setup(&goi, interp, argc - 1, argv + 1);
4629 /* params: <name> statename timeoutmsecs */
4632 const char *cmd_name = Jim_GetString(argv[0], NULL);
4633 Jim_SetResultFormatted(goi.interp,
4634 "%s <state_name> <timeout_in_msec>", cmd_name);
4639 int e = Jim_GetOpt_Nvp(&goi, nvp_target_state, &n);
4641 Jim_GetOpt_NvpUnknown(&goi, nvp_target_state,1);
4645 e = Jim_GetOpt_Wide(&goi, &a);
4649 struct target *target = Jim_CmdPrivData(interp);
4650 if (!target->tap->enabled)
4651 return jim_target_tap_disabled(interp);
4653 e = target_wait_state(target, n->value, a);
4656 Jim_Obj *eObj = Jim_NewIntObj(interp, e);
4657 Jim_SetResultFormatted(goi.interp,
4658 "target: %s wait %s fails (%#s) %s",
4659 target_name(target), n->name,
4660 eObj, target_strerror_safe(e));
4661 Jim_FreeNewObj(interp, eObj);
4666 /* List for human, Events defined for this target.
4667 * scripts/programs should use 'name cget -event NAME'
4669 static int jim_target_event_list(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
4671 struct command_context *cmd_ctx = current_command_context(interp);
4672 assert (cmd_ctx != NULL);
4674 struct target *target = Jim_CmdPrivData(interp);
4675 struct target_event_action *teap = target->event_action;
4676 command_print(cmd_ctx, "Event actions for target (%d) %s\n",
4677 target->target_number,
4678 target_name(target));
4679 command_print(cmd_ctx, "%-25s | Body", "Event");
4680 command_print(cmd_ctx, "------------------------- | "
4681 "----------------------------------------");
4684 Jim_Nvp *opt = Jim_Nvp_value2name_simple(nvp_target_event, teap->event);
4685 command_print(cmd_ctx, "%-25s | %s",
4686 opt->name, Jim_GetString(teap->body, NULL));
4689 command_print(cmd_ctx, "***END***");
4692 static int jim_target_current_state(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
4696 Jim_WrongNumArgs(interp, 1, argv, "[no parameters]");
4699 struct target *target = Jim_CmdPrivData(interp);
4700 Jim_SetResultString(interp, target_state_name(target), -1);
4703 static int jim_target_invoke_event(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
4706 Jim_GetOpt_Setup(&goi, interp, argc - 1, argv + 1);
4709 const char *cmd_name = Jim_GetString(argv[0], NULL);
4710 Jim_SetResultFormatted(goi.interp, "%s <eventname>", cmd_name);
4714 int e = Jim_GetOpt_Nvp(&goi, nvp_target_event, &n);
4717 Jim_GetOpt_NvpUnknown(&goi, nvp_target_event, 1);
4720 struct target *target = Jim_CmdPrivData(interp);
4721 target_handle_event(target, n->value);
4725 static const struct command_registration target_instance_command_handlers[] = {
4727 .name = "configure",
4728 .mode = COMMAND_CONFIG,
4729 .jim_handler = jim_target_configure,
4730 .help = "configure a new target for use",
4731 .usage = "[target_attribute ...]",
4735 .mode = COMMAND_ANY,
4736 .jim_handler = jim_target_configure,
4737 .help = "returns the specified target attribute",
4738 .usage = "target_attribute",
4742 .mode = COMMAND_EXEC,
4743 .jim_handler = jim_target_mw,
4744 .help = "Write 32-bit word(s) to target memory",
4745 .usage = "address data [count]",
4749 .mode = COMMAND_EXEC,
4750 .jim_handler = jim_target_mw,
4751 .help = "Write 16-bit half-word(s) to target memory",
4752 .usage = "address data [count]",
4756 .mode = COMMAND_EXEC,
4757 .jim_handler = jim_target_mw,
4758 .help = "Write byte(s) to target memory",
4759 .usage = "address data [count]",
4763 .mode = COMMAND_EXEC,
4764 .jim_handler = jim_target_md,
4765 .help = "Display target memory as 32-bit words",
4766 .usage = "address [count]",
4770 .mode = COMMAND_EXEC,
4771 .jim_handler = jim_target_md,
4772 .help = "Display target memory as 16-bit half-words",
4773 .usage = "address [count]",
4777 .mode = COMMAND_EXEC,
4778 .jim_handler = jim_target_md,
4779 .help = "Display target memory as 8-bit bytes",
4780 .usage = "address [count]",
4783 .name = "array2mem",
4784 .mode = COMMAND_EXEC,
4785 .jim_handler = jim_target_array2mem,
4786 .help = "Writes Tcl array of 8/16/32 bit numbers "
4788 .usage = "arrayname bitwidth address count",
4791 .name = "mem2array",
4792 .mode = COMMAND_EXEC,
4793 .jim_handler = jim_target_mem2array,
4794 .help = "Loads Tcl array of 8/16/32 bit numbers "
4795 "from target memory",
4796 .usage = "arrayname bitwidth address count",
4799 .name = "eventlist",
4800 .mode = COMMAND_EXEC,
4801 .jim_handler = jim_target_event_list,
4802 .help = "displays a table of events defined for this target",
4806 .mode = COMMAND_EXEC,
4807 .jim_handler = jim_target_current_state,
4808 .help = "displays the current state of this target",
4811 .name = "arp_examine",
4812 .mode = COMMAND_EXEC,
4813 .jim_handler = jim_target_examine,
4814 .help = "used internally for reset processing",
4817 .name = "arp_halt_gdb",
4818 .mode = COMMAND_EXEC,
4819 .jim_handler = jim_target_halt_gdb,
4820 .help = "used internally for reset processing to halt GDB",
4824 .mode = COMMAND_EXEC,
4825 .jim_handler = jim_target_poll,
4826 .help = "used internally for reset processing",
4829 .name = "arp_reset",
4830 .mode = COMMAND_EXEC,
4831 .jim_handler = jim_target_reset,
4832 .help = "used internally for reset processing",
4836 .mode = COMMAND_EXEC,
4837 .jim_handler = jim_target_halt,
4838 .help = "used internally for reset processing",
4841 .name = "arp_waitstate",
4842 .mode = COMMAND_EXEC,
4843 .jim_handler = jim_target_wait_state,
4844 .help = "used internally for reset processing",
4847 .name = "invoke-event",
4848 .mode = COMMAND_EXEC,
4849 .jim_handler = jim_target_invoke_event,
4850 .help = "invoke handler for specified event",
4851 .usage = "event_name",
4853 COMMAND_REGISTRATION_DONE
4856 static int target_create(Jim_GetOptInfo *goi)
4864 struct target *target;
4865 struct command_context *cmd_ctx;
4867 cmd_ctx = current_command_context(goi->interp);
4868 assert (cmd_ctx != NULL);
4870 if (goi->argc < 3) {
4871 Jim_WrongNumArgs(goi->interp, 1, goi->argv, "?name? ?type? ..options...");
4876 Jim_GetOpt_Obj(goi, &new_cmd);
4877 /* does this command exist? */
4878 cmd = Jim_GetCommand(goi->interp, new_cmd, JIM_ERRMSG);
4880 cp = Jim_GetString(new_cmd, NULL);
4881 Jim_SetResultFormatted(goi->interp, "Command/target: %s Exists", cp);
4886 e = Jim_GetOpt_String(goi, &cp2, NULL);
4888 /* now does target type exist */
4889 for (x = 0 ; target_types[x] ; x++) {
4890 if (0 == strcmp(cp, target_types[x]->name)) {
4895 if (target_types[x] == NULL) {
4896 Jim_SetResultFormatted(goi->interp, "Unknown target type %s, try one of ", cp);
4897 for (x = 0 ; target_types[x] ; x++) {
4898 if (target_types[x + 1]) {
4899 Jim_AppendStrings(goi->interp,
4900 Jim_GetResult(goi->interp),
4901 target_types[x]->name,
4904 Jim_AppendStrings(goi->interp,
4905 Jim_GetResult(goi->interp),
4907 target_types[x]->name,NULL);
4914 target = calloc(1,sizeof(struct target));
4915 /* set target number */
4916 target->target_number = new_target_number();
4918 /* allocate memory for each unique target type */
4919 target->type = (struct target_type*)calloc(1,sizeof(struct target_type));
4921 memcpy(target->type, target_types[x], sizeof(struct target_type));
4923 /* will be set by "-endian" */
4924 target->endianness = TARGET_ENDIAN_UNKNOWN;
4926 /* default to first core, override with -coreid */
4929 target->working_area = 0x0;
4930 target->working_area_size = 0x0;
4931 target->working_areas = NULL;
4932 target->backup_working_area = 0;
4934 target->state = TARGET_UNKNOWN;
4935 target->debug_reason = DBG_REASON_UNDEFINED;
4936 target->reg_cache = NULL;
4937 target->breakpoints = NULL;
4938 target->watchpoints = NULL;
4939 target->next = NULL;
4940 target->arch_info = NULL;
4942 target->display = 1;
4944 target->halt_issued = false;
4946 /* initialize trace information */
4947 target->trace_info = malloc(sizeof(struct trace));
4948 target->trace_info->num_trace_points = 0;
4949 target->trace_info->trace_points_size = 0;
4950 target->trace_info->trace_points = NULL;
4951 target->trace_info->trace_history_size = 0;
4952 target->trace_info->trace_history = NULL;
4953 target->trace_info->trace_history_pos = 0;
4954 target->trace_info->trace_history_overflowed = 0;
4956 target->dbgmsg = NULL;
4957 target->dbg_msg_enabled = 0;
4959 target->endianness = TARGET_ENDIAN_UNKNOWN;
4961 target->rtos = NULL;
4962 target->rtos_auto_detect = false;
4964 /* Do the rest as "configure" options */
4965 goi->isconfigure = 1;
4966 e = target_configure(goi, target);
4968 if (target->tap == NULL)
4970 Jim_SetResultString(goi->interp, "-chain-position required when creating target", -1);
4980 if (target->endianness == TARGET_ENDIAN_UNKNOWN) {
4981 /* default endian to little if not specified */
4982 target->endianness = TARGET_LITTLE_ENDIAN;
4985 /* incase variant is not set */
4986 if (!target->variant)
4987 target->variant = strdup("");
4989 cp = Jim_GetString(new_cmd, NULL);
4990 target->cmd_name = strdup(cp);
4992 /* create the target specific commands */
4993 if (target->type->commands) {
4994 e = register_commands(cmd_ctx, NULL, target->type->commands);
4996 LOG_ERROR("unable to register '%s' commands", cp);
4998 if (target->type->target_create) {
4999 (*(target->type->target_create))(target, goi->interp);
5002 /* append to end of list */
5004 struct target **tpp;
5005 tpp = &(all_targets);
5007 tpp = &((*tpp)->next);
5012 /* now - create the new target name command */
5013 const const struct command_registration target_subcommands[] = {
5015 .chain = target_instance_command_handlers,
5018 .chain = target->type->commands,
5020 COMMAND_REGISTRATION_DONE
5022 const const struct command_registration target_commands[] = {
5025 .mode = COMMAND_ANY,
5026 .help = "target command group",
5027 .chain = target_subcommands,
5029 COMMAND_REGISTRATION_DONE
5031 e = register_commands(cmd_ctx, NULL, target_commands);
5035 struct command *c = command_find_in_context(cmd_ctx, cp);
5037 command_set_handler_data(c, target);
5039 return (ERROR_OK == e) ? JIM_OK : JIM_ERR;
5042 static int jim_target_current(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
5046 Jim_WrongNumArgs(interp, 1, argv, "Too many parameters");
5049 struct command_context *cmd_ctx = current_command_context(interp);
5050 assert (cmd_ctx != NULL);
5052 Jim_SetResultString(interp, get_current_target(cmd_ctx)->cmd_name, -1);
5056 static int jim_target_types(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
5060 Jim_WrongNumArgs(interp, 1, argv, "Too many parameters");
5063 Jim_SetResult(interp, Jim_NewListObj(interp, NULL, 0));
5064 for (unsigned x = 0; NULL != target_types[x]; x++)
5066 Jim_ListAppendElement(interp, Jim_GetResult(interp),
5067 Jim_NewStringObj(interp, target_types[x]->name, -1));
5072 static int jim_target_names(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
5076 Jim_WrongNumArgs(interp, 1, argv, "Too many parameters");
5079 Jim_SetResult(interp, Jim_NewListObj(interp, NULL, 0));
5080 struct target *target = all_targets;
5083 Jim_ListAppendElement(interp, Jim_GetResult(interp),
5084 Jim_NewStringObj(interp, target_name(target), -1));
5085 target = target->next;
5090 static int jim_target_smp(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
5093 const char *targetname;
5095 struct target *target;
5096 struct target_list *head, *curr, *new;
5097 curr = (struct target_list*) NULL;
5098 head = (struct target_list*) NULL;
5099 new = (struct target_list*) NULL;
5102 LOG_DEBUG("%d",argc);
5103 /* argv[1] = target to associate in smp
5104 * argv[2] = target to assoicate in smp
5111 targetname = Jim_GetString(argv[i], &len);
5112 target = get_target(targetname);
5113 LOG_DEBUG("%s ",targetname);
5116 new=malloc(sizeof(struct target_list));
5117 new->target = target;
5118 new->next = (struct target_list*)NULL;
5119 if (head == (struct target_list*)NULL)
5131 /* now parse the list of cpu and put the target in smp mode*/
5134 while(curr!=(struct target_list *)NULL)
5136 target=curr->target;
5138 target->head = head;
5145 static int jim_target_create(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
5148 Jim_GetOpt_Setup(&goi, interp, argc - 1, argv + 1);
5151 Jim_WrongNumArgs(goi.interp, goi.argc, goi.argv,
5152 "<name> <target_type> [<target_options> ...]");
5155 return target_create(&goi);
5158 static int jim_target_number(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
5161 Jim_GetOpt_Setup(&goi, interp, argc - 1, argv + 1);
5163 /* It's OK to remove this mechanism sometime after August 2010 or so */
5164 LOG_WARNING("don't use numbers as target identifiers; use names");
5167 Jim_SetResultFormatted(goi.interp, "usage: target number <number>");
5171 int e = Jim_GetOpt_Wide(&goi, &w);
5175 struct target *target;
5176 for (target = all_targets; NULL != target; target = target->next)
5178 if (target->target_number != w)
5181 Jim_SetResultString(goi.interp, target_name(target), -1);
5185 Jim_Obj *wObj = Jim_NewIntObj(goi.interp, w);
5186 Jim_SetResultFormatted(goi.interp,
5187 "Target: number %#s does not exist", wObj);
5188 Jim_FreeNewObj(interp, wObj);
5193 static int jim_target_count(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
5197 Jim_WrongNumArgs(interp, 1, argv, "<no parameters>");
5201 struct target *target = all_targets;
5202 while (NULL != target)
5204 target = target->next;
5207 Jim_SetResult(interp, Jim_NewIntObj(interp, count));
5211 static const struct command_registration target_subcommand_handlers[] = {
5214 .mode = COMMAND_CONFIG,
5215 .handler = handle_target_init_command,
5216 .help = "initialize targets",
5220 /* REVISIT this should be COMMAND_CONFIG ... */
5221 .mode = COMMAND_ANY,
5222 .jim_handler = jim_target_create,
5223 .usage = "name type '-chain-position' name [options ...]",
5224 .help = "Creates and selects a new target",
5228 .mode = COMMAND_ANY,
5229 .jim_handler = jim_target_current,
5230 .help = "Returns the currently selected target",
5234 .mode = COMMAND_ANY,
5235 .jim_handler = jim_target_types,
5236 .help = "Returns the available target types as "
5237 "a list of strings",
5241 .mode = COMMAND_ANY,
5242 .jim_handler = jim_target_names,
5243 .help = "Returns the names of all targets as a list of strings",
5247 .mode = COMMAND_ANY,
5248 .jim_handler = jim_target_number,
5250 .help = "Returns the name of the numbered target "
5255 .mode = COMMAND_ANY,
5256 .jim_handler = jim_target_count,
5257 .help = "Returns the number of targets as an integer "
5262 .mode = COMMAND_ANY,
5263 .jim_handler = jim_target_smp,
5264 .usage = "targetname1 targetname2 ...",
5265 .help = "gather several target in a smp list"
5268 COMMAND_REGISTRATION_DONE
5279 static int fastload_num;
5280 static struct FastLoad *fastload;
5282 static void free_fastload(void)
5284 if (fastload != NULL)
5287 for (i = 0; i < fastload_num; i++)
5289 if (fastload[i].data)
5290 free(fastload[i].data);
5300 COMMAND_HANDLER(handle_fast_load_image_command)
5304 uint32_t image_size;
5305 uint32_t min_address = 0;
5306 uint32_t max_address = 0xffffffff;
5311 int retval = CALL_COMMAND_HANDLER(parse_load_image_command_CMD_ARGV,
5312 &image, &min_address, &max_address);
5313 if (ERROR_OK != retval)
5316 struct duration bench;
5317 duration_start(&bench);
5319 retval = image_open(&image, CMD_ARGV[0], (CMD_ARGC >= 3) ? CMD_ARGV[2] : NULL);
5320 if (retval != ERROR_OK)
5327 fastload_num = image.num_sections;
5328 fastload = (struct FastLoad *)malloc(sizeof(struct FastLoad)*image.num_sections);
5329 if (fastload == NULL)
5331 command_print(CMD_CTX, "out of memory");
5332 image_close(&image);
5335 memset(fastload, 0, sizeof(struct FastLoad)*image.num_sections);
5336 for (i = 0; i < image.num_sections; i++)
5338 buffer = malloc(image.sections[i].size);
5341 command_print(CMD_CTX, "error allocating buffer for section (%d bytes)",
5342 (int)(image.sections[i].size));
5343 retval = ERROR_FAIL;
5347 if ((retval = image_read_section(&image, i, 0x0, image.sections[i].size, buffer, &buf_cnt)) != ERROR_OK)
5353 uint32_t offset = 0;
5354 uint32_t length = buf_cnt;
5357 /* DANGER!!! beware of unsigned comparision here!!! */
5359 if ((image.sections[i].base_address + buf_cnt >= min_address)&&
5360 (image.sections[i].base_address < max_address))
5362 if (image.sections[i].base_address < min_address)
5364 /* clip addresses below */
5365 offset += min_address-image.sections[i].base_address;
5369 if (image.sections[i].base_address + buf_cnt > max_address)
5371 length -= (image.sections[i].base_address + buf_cnt)-max_address;
5374 fastload[i].address = image.sections[i].base_address + offset;
5375 fastload[i].data = malloc(length);
5376 if (fastload[i].data == NULL)
5379 command_print(CMD_CTX, "error allocating buffer for section (%d bytes)",
5381 retval = ERROR_FAIL;
5384 memcpy(fastload[i].data, buffer + offset, length);
5385 fastload[i].length = length;
5387 image_size += length;
5388 command_print(CMD_CTX, "%u bytes written at address 0x%8.8x",
5389 (unsigned int)length,
5390 ((unsigned int)(image.sections[i].base_address + offset)));
5396 if ((ERROR_OK == retval) && (duration_measure(&bench) == ERROR_OK))
5398 command_print(CMD_CTX, "Loaded %" PRIu32 " bytes "
5399 "in %fs (%0.3f KiB/s)", image_size,
5400 duration_elapsed(&bench), duration_kbps(&bench, image_size));
5402 command_print(CMD_CTX,
5403 "WARNING: image has not been loaded to target!"
5404 "You can issue a 'fast_load' to finish loading.");
5407 image_close(&image);
5409 if (retval != ERROR_OK)
5417 COMMAND_HANDLER(handle_fast_load_command)
5420 return ERROR_COMMAND_SYNTAX_ERROR;
5421 if (fastload == NULL)
5423 LOG_ERROR("No image in memory");
5427 int ms = timeval_ms();
5429 int retval = ERROR_OK;
5430 for (i = 0; i < fastload_num;i++)
5432 struct target *target = get_current_target(CMD_CTX);
5433 command_print(CMD_CTX, "Write to 0x%08x, length 0x%08x",
5434 (unsigned int)(fastload[i].address),
5435 (unsigned int)(fastload[i].length));
5436 retval = target_write_buffer(target, fastload[i].address, fastload[i].length, fastload[i].data);
5437 if (retval != ERROR_OK)
5441 size += fastload[i].length;
5443 if (retval == ERROR_OK)
5445 int after = timeval_ms();
5446 command_print(CMD_CTX, "Loaded image %f kBytes/s", (float)(size/1024.0)/((float)(after-ms)/1000.0));
5451 static const struct command_registration target_command_handlers[] = {
5454 .handler = handle_targets_command,
5455 .mode = COMMAND_ANY,
5456 .help = "change current default target (one parameter) "
5457 "or prints table of all targets (no parameters)",
5458 .usage = "[target]",
5462 .mode = COMMAND_CONFIG,
5463 .help = "configure target",
5465 .chain = target_subcommand_handlers,
5467 COMMAND_REGISTRATION_DONE
5470 int target_register_commands(struct command_context *cmd_ctx)
5472 return register_commands(cmd_ctx, NULL, target_command_handlers);
5475 static bool target_reset_nag = true;
5477 bool get_target_reset_nag(void)
5479 return target_reset_nag;
5482 COMMAND_HANDLER(handle_target_reset_nag)
5484 return CALL_COMMAND_HANDLER(handle_command_parse_bool,
5485 &target_reset_nag, "Nag after each reset about options to improve "
5489 static const struct command_registration target_exec_command_handlers[] = {
5491 .name = "fast_load_image",
5492 .handler = handle_fast_load_image_command,
5493 .mode = COMMAND_ANY,
5494 .help = "Load image into server memory for later use by "
5495 "fast_load; primarily for profiling",
5496 .usage = "filename address ['bin'|'ihex'|'elf'|'s19'] "
5497 "[min_address [max_length]]",
5500 .name = "fast_load",
5501 .handler = handle_fast_load_command,
5502 .mode = COMMAND_EXEC,
5503 .help = "loads active fast load image to current target "
5504 "- mainly for profiling purposes",
5508 .handler = handle_profile_command,
5509 .mode = COMMAND_EXEC,
5510 .help = "profiling samples the CPU PC",
5512 /** @todo don't register virt2phys() unless target supports it */
5514 .name = "virt2phys",
5515 .handler = handle_virt2phys_command,
5516 .mode = COMMAND_ANY,
5517 .help = "translate a virtual address into a physical address",
5518 .usage = "virtual_address",
5522 .handler = handle_reg_command,
5523 .mode = COMMAND_EXEC,
5524 .help = "display or set a register; with no arguments, "
5525 "displays all registers and their values",
5526 .usage = "[(register_name|register_number) [value]]",
5530 .handler = handle_poll_command,
5531 .mode = COMMAND_EXEC,
5532 .help = "poll target state; or reconfigure background polling",
5533 .usage = "['on'|'off']",
5536 .name = "wait_halt",
5537 .handler = handle_wait_halt_command,
5538 .mode = COMMAND_EXEC,
5539 .help = "wait up to the specified number of milliseconds "
5540 "(default 5) for a previously requested halt",
5541 .usage = "[milliseconds]",
5545 .handler = handle_halt_command,
5546 .mode = COMMAND_EXEC,
5547 .help = "request target to halt, then wait up to the specified"
5548 "number of milliseconds (default 5) for it to complete",
5549 .usage = "[milliseconds]",
5553 .handler = handle_resume_command,
5554 .mode = COMMAND_EXEC,
5555 .help = "resume target execution from current PC or address",
5556 .usage = "[address]",
5560 .handler = handle_reset_command,
5561 .mode = COMMAND_EXEC,
5562 .usage = "[run|halt|init]",
5563 .help = "Reset all targets into the specified mode."
5564 "Default reset mode is run, if not given.",
5567 .name = "soft_reset_halt",
5568 .handler = handle_soft_reset_halt_command,
5569 .mode = COMMAND_EXEC,
5570 .help = "halt the target and do a soft reset",
5574 .handler = handle_step_command,
5575 .mode = COMMAND_EXEC,
5576 .help = "step one instruction from current PC or address",
5577 .usage = "[address]",
5581 .handler = handle_md_command,
5582 .mode = COMMAND_EXEC,
5583 .help = "display memory words",
5584 .usage = "['phys'] address [count]",
5588 .handler = handle_md_command,
5589 .mode = COMMAND_EXEC,
5590 .help = "display memory half-words",
5591 .usage = "['phys'] address [count]",
5595 .handler = handle_md_command,
5596 .mode = COMMAND_EXEC,
5597 .help = "display memory bytes",
5598 .usage = "['phys'] address [count]",
5602 .handler = handle_mw_command,
5603 .mode = COMMAND_EXEC,
5604 .help = "write memory word",
5605 .usage = "['phys'] address value [count]",
5609 .handler = handle_mw_command,
5610 .mode = COMMAND_EXEC,
5611 .help = "write memory half-word",
5612 .usage = "['phys'] address value [count]",
5616 .handler = handle_mw_command,
5617 .mode = COMMAND_EXEC,
5618 .help = "write memory byte",
5619 .usage = "['phys'] address value [count]",
5623 .handler = handle_bp_command,
5624 .mode = COMMAND_EXEC,
5625 .help = "list or set hardware or software breakpoint",
5626 .usage = "usage: bp <address> [<asid>]<length> ['hw'|'hw_ctx']",
5630 .handler = handle_rbp_command,
5631 .mode = COMMAND_EXEC,
5632 .help = "remove breakpoint",
5637 .handler = handle_wp_command,
5638 .mode = COMMAND_EXEC,
5639 .help = "list (no params) or create watchpoints",
5640 .usage = "[address length [('r'|'w'|'a') value [mask]]]",
5644 .handler = handle_rwp_command,
5645 .mode = COMMAND_EXEC,
5646 .help = "remove watchpoint",
5650 .name = "load_image",
5651 .handler = handle_load_image_command,
5652 .mode = COMMAND_EXEC,
5653 .usage = "filename address ['bin'|'ihex'|'elf'|'s19'] "
5654 "[min_address] [max_length]",
5657 .name = "dump_image",
5658 .handler = handle_dump_image_command,
5659 .mode = COMMAND_EXEC,
5660 .usage = "filename address size",
5663 .name = "verify_image",
5664 .handler = handle_verify_image_command,
5665 .mode = COMMAND_EXEC,
5666 .usage = "filename [offset [type]]",
5669 .name = "test_image",
5670 .handler = handle_test_image_command,
5671 .mode = COMMAND_EXEC,
5672 .usage = "filename [offset [type]]",
5675 .name = "mem2array",
5676 .mode = COMMAND_EXEC,
5677 .jim_handler = jim_mem2array,
5678 .help = "read 8/16/32 bit memory and return as a TCL array "
5679 "for script processing",
5680 .usage = "arrayname bitwidth address count",
5683 .name = "array2mem",
5684 .mode = COMMAND_EXEC,
5685 .jim_handler = jim_array2mem,
5686 .help = "convert a TCL array to memory locations "
5687 "and write the 8/16/32 bit values",
5688 .usage = "arrayname bitwidth address count",
5691 .name = "reset_nag",
5692 .handler = handle_target_reset_nag,
5693 .mode = COMMAND_ANY,
5694 .help = "Nag after each reset about options that could have been "
5695 "enabled to improve performance. ",
5696 .usage = "['enable'|'disable']",
5698 COMMAND_REGISTRATION_DONE
5700 static int target_register_user_commands(struct command_context *cmd_ctx)
5702 int retval = ERROR_OK;
5703 if ((retval = target_request_register_commands(cmd_ctx)) != ERROR_OK)
5706 if ((retval = trace_register_commands(cmd_ctx)) != ERROR_OK)
5710 return register_commands(cmd_ctx, NULL, target_exec_command_handlers);