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 * Copyright (C) 2011 Andreas Fritiofson *
24 * andreas.fritiofson@gmail.com *
26 * This program is free software; you can redistribute it and/or modify *
27 * it under the terms of the GNU General Public License as published by *
28 * the Free Software Foundation; either version 2 of the License, or *
29 * (at your option) any later version. *
31 * This program is distributed in the hope that it will be useful, *
32 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
33 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
34 * GNU General Public License for more details. *
36 * You should have received a copy of the GNU General Public License *
37 * along with this program; if not, write to the *
38 * Free Software Foundation, Inc., *
39 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
40 ***************************************************************************/
46 #include <helper/time_support.h>
47 #include <jtag/jtag.h>
48 #include <flash/nor/core.h>
51 #include "target_type.h"
52 #include "target_request.h"
53 #include "breakpoints.h"
57 #include "rtos/rtos.h"
59 static int target_read_buffer_default(struct target *target, uint32_t address,
60 uint32_t size, uint8_t *buffer);
61 static int target_write_buffer_default(struct target *target, uint32_t address,
62 uint32_t size, const uint8_t *buffer);
63 static int target_array2mem(Jim_Interp *interp, struct target *target,
64 int argc, Jim_Obj * const *argv);
65 static int target_mem2array(Jim_Interp *interp, struct target *target,
66 int argc, Jim_Obj * const *argv);
67 static int target_register_user_commands(struct command_context *cmd_ctx);
70 extern struct target_type arm7tdmi_target;
71 extern struct target_type arm720t_target;
72 extern struct target_type arm9tdmi_target;
73 extern struct target_type arm920t_target;
74 extern struct target_type arm966e_target;
75 extern struct target_type arm946e_target;
76 extern struct target_type arm926ejs_target;
77 extern struct target_type fa526_target;
78 extern struct target_type feroceon_target;
79 extern struct target_type dragonite_target;
80 extern struct target_type xscale_target;
81 extern struct target_type cortexm3_target;
82 extern struct target_type cortexa8_target;
83 extern struct target_type arm11_target;
84 extern struct target_type mips_m4k_target;
85 extern struct target_type avr_target;
86 extern struct target_type dsp563xx_target;
87 extern struct target_type dsp5680xx_target;
88 extern struct target_type testee_target;
89 extern struct target_type avr32_ap7k_target;
90 extern struct target_type stm32_stlink_target;
92 static struct target_type *target_types[] = {
113 &stm32_stlink_target,
117 struct target *all_targets;
118 static struct target_event_callback *target_event_callbacks;
119 static struct target_timer_callback *target_timer_callbacks;
120 static const int polling_interval = 100;
122 static const Jim_Nvp nvp_assert[] = {
123 { .name = "assert", NVP_ASSERT },
124 { .name = "deassert", NVP_DEASSERT },
125 { .name = "T", NVP_ASSERT },
126 { .name = "F", NVP_DEASSERT },
127 { .name = "t", NVP_ASSERT },
128 { .name = "f", NVP_DEASSERT },
129 { .name = NULL, .value = -1 }
132 static const Jim_Nvp nvp_error_target[] = {
133 { .value = ERROR_TARGET_INVALID, .name = "err-invalid" },
134 { .value = ERROR_TARGET_INIT_FAILED, .name = "err-init-failed" },
135 { .value = ERROR_TARGET_TIMEOUT, .name = "err-timeout" },
136 { .value = ERROR_TARGET_NOT_HALTED, .name = "err-not-halted" },
137 { .value = ERROR_TARGET_FAILURE, .name = "err-failure" },
138 { .value = ERROR_TARGET_UNALIGNED_ACCESS , .name = "err-unaligned-access" },
139 { .value = ERROR_TARGET_DATA_ABORT , .name = "err-data-abort" },
140 { .value = ERROR_TARGET_RESOURCE_NOT_AVAILABLE , .name = "err-resource-not-available" },
141 { .value = ERROR_TARGET_TRANSLATION_FAULT , .name = "err-translation-fault" },
142 { .value = ERROR_TARGET_NOT_RUNNING, .name = "err-not-running" },
143 { .value = ERROR_TARGET_NOT_EXAMINED, .name = "err-not-examined" },
144 { .value = -1, .name = NULL }
147 static const char *target_strerror_safe(int err)
151 n = Jim_Nvp_value2name_simple(nvp_error_target, err);
158 static const Jim_Nvp nvp_target_event[] = {
159 { .value = TARGET_EVENT_OLD_gdb_program_config , .name = "old-gdb_program_config" },
160 { .value = TARGET_EVENT_OLD_pre_resume , .name = "old-pre_resume" },
162 { .value = TARGET_EVENT_GDB_HALT, .name = "gdb-halt" },
163 { .value = TARGET_EVENT_HALTED, .name = "halted" },
164 { .value = TARGET_EVENT_RESUMED, .name = "resumed" },
165 { .value = TARGET_EVENT_RESUME_START, .name = "resume-start" },
166 { .value = TARGET_EVENT_RESUME_END, .name = "resume-end" },
168 { .name = "gdb-start", .value = TARGET_EVENT_GDB_START },
169 { .name = "gdb-end", .value = TARGET_EVENT_GDB_END },
171 /* historical name */
173 { .value = TARGET_EVENT_RESET_START, .name = "reset-start" },
175 { .value = TARGET_EVENT_RESET_ASSERT_PRE, .name = "reset-assert-pre" },
176 { .value = TARGET_EVENT_RESET_ASSERT, .name = "reset-assert" },
177 { .value = TARGET_EVENT_RESET_ASSERT_POST, .name = "reset-assert-post" },
178 { .value = TARGET_EVENT_RESET_DEASSERT_PRE, .name = "reset-deassert-pre" },
179 { .value = TARGET_EVENT_RESET_DEASSERT_POST, .name = "reset-deassert-post" },
180 { .value = TARGET_EVENT_RESET_HALT_PRE, .name = "reset-halt-pre" },
181 { .value = TARGET_EVENT_RESET_HALT_POST, .name = "reset-halt-post" },
182 { .value = TARGET_EVENT_RESET_WAIT_PRE, .name = "reset-wait-pre" },
183 { .value = TARGET_EVENT_RESET_WAIT_POST, .name = "reset-wait-post" },
184 { .value = TARGET_EVENT_RESET_INIT, .name = "reset-init" },
185 { .value = TARGET_EVENT_RESET_END, .name = "reset-end" },
187 { .value = TARGET_EVENT_EXAMINE_START, .name = "examine-start" },
188 { .value = TARGET_EVENT_EXAMINE_END, .name = "examine-end" },
190 { .value = TARGET_EVENT_DEBUG_HALTED, .name = "debug-halted" },
191 { .value = TARGET_EVENT_DEBUG_RESUMED, .name = "debug-resumed" },
193 { .value = TARGET_EVENT_GDB_ATTACH, .name = "gdb-attach" },
194 { .value = TARGET_EVENT_GDB_DETACH, .name = "gdb-detach" },
196 { .value = TARGET_EVENT_GDB_FLASH_WRITE_START, .name = "gdb-flash-write-start" },
197 { .value = TARGET_EVENT_GDB_FLASH_WRITE_END , .name = "gdb-flash-write-end" },
199 { .value = TARGET_EVENT_GDB_FLASH_ERASE_START, .name = "gdb-flash-erase-start" },
200 { .value = TARGET_EVENT_GDB_FLASH_ERASE_END , .name = "gdb-flash-erase-end" },
202 { .value = TARGET_EVENT_RESUME_START, .name = "resume-start" },
203 { .value = TARGET_EVENT_RESUMED , .name = "resume-ok" },
204 { .value = TARGET_EVENT_RESUME_END , .name = "resume-end" },
206 { .name = NULL, .value = -1 }
209 static const Jim_Nvp nvp_target_state[] = {
210 { .name = "unknown", .value = TARGET_UNKNOWN },
211 { .name = "running", .value = TARGET_RUNNING },
212 { .name = "halted", .value = TARGET_HALTED },
213 { .name = "reset", .value = TARGET_RESET },
214 { .name = "debug-running", .value = TARGET_DEBUG_RUNNING },
215 { .name = NULL, .value = -1 },
218 static const Jim_Nvp nvp_target_debug_reason[] = {
219 { .name = "debug-request" , .value = DBG_REASON_DBGRQ },
220 { .name = "breakpoint" , .value = DBG_REASON_BREAKPOINT },
221 { .name = "watchpoint" , .value = DBG_REASON_WATCHPOINT },
222 { .name = "watchpoint-and-breakpoint", .value = DBG_REASON_WPTANDBKPT },
223 { .name = "single-step" , .value = DBG_REASON_SINGLESTEP },
224 { .name = "target-not-halted" , .value = DBG_REASON_NOTHALTED },
225 { .name = "undefined" , .value = DBG_REASON_UNDEFINED },
226 { .name = NULL, .value = -1 },
229 static const Jim_Nvp nvp_target_endian[] = {
230 { .name = "big", .value = TARGET_BIG_ENDIAN },
231 { .name = "little", .value = TARGET_LITTLE_ENDIAN },
232 { .name = "be", .value = TARGET_BIG_ENDIAN },
233 { .name = "le", .value = TARGET_LITTLE_ENDIAN },
234 { .name = NULL, .value = -1 },
237 static const Jim_Nvp nvp_reset_modes[] = {
238 { .name = "unknown", .value = RESET_UNKNOWN },
239 { .name = "run" , .value = RESET_RUN },
240 { .name = "halt" , .value = RESET_HALT },
241 { .name = "init" , .value = RESET_INIT },
242 { .name = NULL , .value = -1 },
245 const char *debug_reason_name(struct target *t)
249 cp = Jim_Nvp_value2name_simple(nvp_target_debug_reason,
250 t->debug_reason)->name;
252 LOG_ERROR("Invalid debug reason: %d", (int)(t->debug_reason));
253 cp = "(*BUG*unknown*BUG*)";
258 const char *target_state_name(struct target *t)
261 cp = Jim_Nvp_value2name_simple(nvp_target_state, t->state)->name;
263 LOG_ERROR("Invalid target state: %d", (int)(t->state));
264 cp = "(*BUG*unknown*BUG*)";
269 /* determine the number of the new target */
270 static int new_target_number(void)
275 /* number is 0 based */
279 if (x < t->target_number)
280 x = t->target_number;
286 /* read a uint32_t from a buffer in target memory endianness */
287 uint32_t target_buffer_get_u32(struct target *target, const uint8_t *buffer)
289 if (target->endianness == TARGET_LITTLE_ENDIAN)
290 return le_to_h_u32(buffer);
292 return be_to_h_u32(buffer);
295 /* read a uint24_t from a buffer in target memory endianness */
296 uint32_t target_buffer_get_u24(struct target *target, const uint8_t *buffer)
298 if (target->endianness == TARGET_LITTLE_ENDIAN)
299 return le_to_h_u24(buffer);
301 return be_to_h_u24(buffer);
304 /* read a uint16_t from a buffer in target memory endianness */
305 uint16_t target_buffer_get_u16(struct target *target, const uint8_t *buffer)
307 if (target->endianness == TARGET_LITTLE_ENDIAN)
308 return le_to_h_u16(buffer);
310 return be_to_h_u16(buffer);
313 /* read a uint8_t from a buffer in target memory endianness */
314 static uint8_t target_buffer_get_u8(struct target *target, const uint8_t *buffer)
316 return *buffer & 0x0ff;
319 /* write a uint32_t to a buffer in target memory endianness */
320 void target_buffer_set_u32(struct target *target, uint8_t *buffer, uint32_t value)
322 if (target->endianness == TARGET_LITTLE_ENDIAN)
323 h_u32_to_le(buffer, value);
325 h_u32_to_be(buffer, value);
328 /* write a uint24_t to a buffer in target memory endianness */
329 void target_buffer_set_u24(struct target *target, uint8_t *buffer, uint32_t value)
331 if (target->endianness == TARGET_LITTLE_ENDIAN)
332 h_u24_to_le(buffer, value);
334 h_u24_to_be(buffer, value);
337 /* write a uint16_t to a buffer in target memory endianness */
338 void target_buffer_set_u16(struct target *target, uint8_t *buffer, uint16_t value)
340 if (target->endianness == TARGET_LITTLE_ENDIAN)
341 h_u16_to_le(buffer, value);
343 h_u16_to_be(buffer, value);
346 /* write a uint8_t to a buffer in target memory endianness */
347 static void target_buffer_set_u8(struct target *target, uint8_t *buffer, uint8_t value)
352 /* write a uint32_t array to a buffer in target memory endianness */
353 void target_buffer_get_u32_array(struct target *target, const uint8_t *buffer, uint32_t count, uint32_t *dstbuf)
356 for (i = 0; i < count; i++)
357 dstbuf[i] = target_buffer_get_u32(target, &buffer[i * 4]);
360 /* write a uint16_t array to a buffer in target memory endianness */
361 void target_buffer_get_u16_array(struct target *target, const uint8_t *buffer, uint32_t count, uint16_t *dstbuf)
364 for (i = 0; i < count; i++)
365 dstbuf[i] = target_buffer_get_u16(target, &buffer[i * 2]);
368 /* write a uint32_t array to a buffer in target memory endianness */
369 void target_buffer_set_u32_array(struct target *target, uint8_t *buffer, uint32_t count, uint32_t *srcbuf)
372 for (i = 0; i < count; i++)
373 target_buffer_set_u32(target, &buffer[i * 4], srcbuf[i]);
376 /* write a uint16_t array to a buffer in target memory endianness */
377 void target_buffer_set_u16_array(struct target *target, uint8_t *buffer, uint32_t count, uint16_t *srcbuf)
380 for (i = 0; i < count; i++)
381 target_buffer_set_u16(target, &buffer[i * 2], srcbuf[i]);
384 /* return a pointer to a configured target; id is name or number */
385 struct target *get_target(const char *id)
387 struct target *target;
389 /* try as tcltarget name */
390 for (target = all_targets; target; target = target->next) {
391 if (target->cmd_name == NULL)
393 if (strcmp(id, target->cmd_name) == 0)
397 /* It's OK to remove this fallback sometime after August 2010 or so */
399 /* no match, try as number */
401 if (parse_uint(id, &num) != ERROR_OK)
404 for (target = all_targets; target; target = target->next) {
405 if (target->target_number == (int)num) {
406 LOG_WARNING("use '%s' as target identifier, not '%u'",
407 target->cmd_name, num);
415 /* returns a pointer to the n-th configured target */
416 static struct target *get_target_by_num(int num)
418 struct target *target = all_targets;
421 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);
433 if (target == NULL) {
434 LOG_ERROR("BUG: current_target out of bounds");
441 int target_poll(struct target *target)
445 /* We can't poll until after examine */
446 if (!target_was_examined(target)) {
447 /* Fail silently lest we pollute the log */
451 retval = target->type->poll(target);
452 if (retval != ERROR_OK)
455 if (target->halt_issued) {
456 if (target->state == TARGET_HALTED)
457 target->halt_issued = false;
459 long long t = timeval_ms() - target->halt_issued_time;
461 target->halt_issued = false;
462 LOG_INFO("Halt timed out, wake up GDB.");
463 target_call_event_callbacks(target, TARGET_EVENT_GDB_HALT);
471 int target_halt(struct target *target)
474 /* We can't poll until after examine */
475 if (!target_was_examined(target)) {
476 LOG_ERROR("Target not examined yet");
480 retval = target->type->halt(target);
481 if (retval != ERROR_OK)
484 target->halt_issued = true;
485 target->halt_issued_time = timeval_ms();
491 * Make the target (re)start executing using its saved execution
492 * context (possibly with some modifications).
494 * @param target Which target should start executing.
495 * @param current True to use the target's saved program counter instead
496 * of the address parameter
497 * @param address Optionally used as the program counter.
498 * @param handle_breakpoints True iff breakpoints at the resumption PC
499 * should be skipped. (For example, maybe execution was stopped by
500 * such a breakpoint, in which case it would be counterprodutive to
502 * @param debug_execution False if all working areas allocated by OpenOCD
503 * should be released and/or restored to their original contents.
504 * (This would for example be true to run some downloaded "helper"
505 * algorithm code, which resides in one such working buffer and uses
506 * another for data storage.)
508 * @todo Resolve the ambiguity about what the "debug_execution" flag
509 * signifies. For example, Target implementations don't agree on how
510 * it relates to invalidation of the register cache, or to whether
511 * breakpoints and watchpoints should be enabled. (It would seem wrong
512 * to enable breakpoints when running downloaded "helper" algorithms
513 * (debug_execution true), since the breakpoints would be set to match
514 * target firmware being debugged, not the helper algorithm.... and
515 * enabling them could cause such helpers to malfunction (for example,
516 * by overwriting data with a breakpoint instruction. On the other
517 * hand the infrastructure for running such helpers might use this
518 * procedure but rely on hardware breakpoint to detect termination.)
520 int target_resume(struct target *target, int current, uint32_t address, int handle_breakpoints, int debug_execution)
524 /* We can't poll until after examine */
525 if (!target_was_examined(target)) {
526 LOG_ERROR("Target not examined yet");
530 /* note that resume *must* be asynchronous. The CPU can halt before
531 * we poll. The CPU can even halt at the current PC as a result of
532 * a software breakpoint being inserted by (a bug?) the application.
534 retval = target->type->resume(target, current, address, handle_breakpoints, debug_execution);
535 if (retval != ERROR_OK)
541 static int target_process_reset(struct command_context *cmd_ctx, enum target_reset_mode reset_mode)
546 n = Jim_Nvp_value2name_simple(nvp_reset_modes, reset_mode);
547 if (n->name == NULL) {
548 LOG_ERROR("invalid reset mode");
552 /* disable polling during reset to make reset event scripts
553 * more predictable, i.e. dr/irscan & pathmove in events will
554 * not have JTAG operations injected into the middle of a sequence.
556 bool save_poll = jtag_poll_get_enabled();
558 jtag_poll_set_enabled(false);
560 sprintf(buf, "ocd_process_reset %s", n->name);
561 retval = Jim_Eval(cmd_ctx->interp, buf);
563 jtag_poll_set_enabled(save_poll);
565 if (retval != JIM_OK) {
566 Jim_MakeErrorMessage(cmd_ctx->interp);
567 command_print(NULL, "%s\n", Jim_GetString(Jim_GetResult(cmd_ctx->interp), NULL));
571 /* We want any events to be processed before the prompt */
572 retval = target_call_timer_callbacks_now();
574 struct target *target;
575 for (target = all_targets; target; target = target->next)
576 target->type->check_reset(target);
581 static int identity_virt2phys(struct target *target,
582 uint32_t virtual, uint32_t *physical)
588 static int no_mmu(struct target *target, int *enabled)
594 static int default_examine(struct target *target)
596 target_set_examined(target);
600 /* no check by default */
601 static int default_check_reset(struct target *target)
606 int target_examine_one(struct target *target)
608 return target->type->examine(target);
611 static int jtag_enable_callback(enum jtag_event event, void *priv)
613 struct target *target = priv;
615 if (event != JTAG_TAP_EVENT_ENABLE || !target->tap->enabled)
618 jtag_unregister_event_callback(jtag_enable_callback, target);
619 return target_examine_one(target);
623 /* Targets that correctly implement init + examine, i.e.
624 * no communication with target during init:
628 int target_examine(void)
630 int retval = ERROR_OK;
631 struct target *target;
633 for (target = all_targets; target; target = target->next) {
634 /* defer examination, but don't skip it */
635 if (!target->tap->enabled) {
636 jtag_register_event_callback(jtag_enable_callback,
640 retval = target_examine_one(target);
641 if (retval != ERROR_OK)
646 const char *target_type_name(struct target *target)
648 return target->type->name;
651 static int target_write_memory_imp(struct target *target, uint32_t address,
652 uint32_t size, uint32_t count, const uint8_t *buffer)
654 if (!target_was_examined(target)) {
655 LOG_ERROR("Target not examined yet");
658 return target->type->write_memory_imp(target, address, size, count, buffer);
661 static int target_read_memory_imp(struct target *target, uint32_t address,
662 uint32_t size, uint32_t count, uint8_t *buffer)
664 if (!target_was_examined(target)) {
665 LOG_ERROR("Target not examined yet");
668 return target->type->read_memory_imp(target, address, size, count, buffer);
671 static int target_soft_reset_halt_imp(struct target *target)
673 if (!target_was_examined(target)) {
674 LOG_ERROR("Target not examined yet");
677 if (!target->type->soft_reset_halt_imp) {
678 LOG_ERROR("Target %s does not support soft_reset_halt",
679 target_name(target));
682 return target->type->soft_reset_halt_imp(target);
686 * Downloads a target-specific native code algorithm to the target,
687 * and executes it. * Note that some targets may need to set up, enable,
688 * and tear down a breakpoint (hard or * soft) to detect algorithm
689 * termination, while others may support lower overhead schemes where
690 * soft breakpoints embedded in the algorithm automatically terminate the
693 * @param target used to run the algorithm
694 * @param arch_info target-specific description of the algorithm.
696 int target_run_algorithm(struct target *target,
697 int num_mem_params, struct mem_param *mem_params,
698 int num_reg_params, struct reg_param *reg_param,
699 uint32_t entry_point, uint32_t exit_point,
700 int timeout_ms, void *arch_info)
702 int retval = ERROR_FAIL;
704 if (!target_was_examined(target)) {
705 LOG_ERROR("Target not examined yet");
708 if (!target->type->run_algorithm) {
709 LOG_ERROR("Target type '%s' does not support %s",
710 target_type_name(target), __func__);
714 target->running_alg = true;
715 retval = target->type->run_algorithm(target,
716 num_mem_params, mem_params,
717 num_reg_params, reg_param,
718 entry_point, exit_point, timeout_ms, arch_info);
719 target->running_alg = false;
726 * Downloads a target-specific native code algorithm to the target,
727 * executes and leaves it running.
729 * @param target used to run the algorithm
730 * @param arch_info target-specific description of the algorithm.
732 int target_start_algorithm(struct target *target,
733 int num_mem_params, struct mem_param *mem_params,
734 int num_reg_params, struct reg_param *reg_params,
735 uint32_t entry_point, uint32_t exit_point,
738 int retval = ERROR_FAIL;
740 if (!target_was_examined(target)) {
741 LOG_ERROR("Target not examined yet");
744 if (!target->type->start_algorithm) {
745 LOG_ERROR("Target type '%s' does not support %s",
746 target_type_name(target), __func__);
749 if (target->running_alg) {
750 LOG_ERROR("Target is already running an algorithm");
754 target->running_alg = true;
755 retval = target->type->start_algorithm(target,
756 num_mem_params, mem_params,
757 num_reg_params, reg_params,
758 entry_point, exit_point, arch_info);
765 * Waits for an algorithm started with target_start_algorithm() to complete.
767 * @param target used to run the algorithm
768 * @param arch_info target-specific description of the algorithm.
770 int target_wait_algorithm(struct target *target,
771 int num_mem_params, struct mem_param *mem_params,
772 int num_reg_params, struct reg_param *reg_params,
773 uint32_t exit_point, int timeout_ms,
776 int retval = ERROR_FAIL;
778 if (!target->type->wait_algorithm) {
779 LOG_ERROR("Target type '%s' does not support %s",
780 target_type_name(target), __func__);
783 if (!target->running_alg) {
784 LOG_ERROR("Target is not running an algorithm");
788 retval = target->type->wait_algorithm(target,
789 num_mem_params, mem_params,
790 num_reg_params, reg_params,
791 exit_point, timeout_ms, arch_info);
792 if (retval != ERROR_TARGET_TIMEOUT)
793 target->running_alg = false;
800 * Executes a target-specific native code algorithm in the target.
801 * It differs from target_run_algorithm in that the algorithm is asynchronous.
802 * Because of this it requires an compliant algorithm:
803 * see contrib/loaders/flash/stm32f1x.S for example.
805 * @param target used to run the algorithm
808 int target_run_flash_async_algorithm(struct target *target,
809 uint8_t *buffer, uint32_t count, int block_size,
810 int num_mem_params, struct mem_param *mem_params,
811 int num_reg_params, struct reg_param *reg_params,
812 uint32_t buffer_start, uint32_t buffer_size,
813 uint32_t entry_point, uint32_t exit_point, void *arch_info)
817 /* Set up working area. First word is write pointer, second word is read pointer,
818 * rest is fifo data area. */
819 uint32_t wp_addr = buffer_start;
820 uint32_t rp_addr = buffer_start + 4;
821 uint32_t fifo_start_addr = buffer_start + 8;
822 uint32_t fifo_end_addr = buffer_start + buffer_size;
824 uint32_t wp = fifo_start_addr;
825 uint32_t rp = fifo_start_addr;
827 /* validate block_size is 2^n */
828 assert(!block_size || !(block_size & (block_size - 1)));
830 retval = target_write_u32(target, wp_addr, wp);
831 if (retval != ERROR_OK)
833 retval = target_write_u32(target, rp_addr, rp);
834 if (retval != ERROR_OK)
837 /* Start up algorithm on target and let it idle while writing the first chunk */
838 retval = target_start_algorithm(target, num_mem_params, mem_params,
839 num_reg_params, reg_params,
844 if (retval != ERROR_OK) {
845 LOG_ERROR("error starting target flash write algorithm");
851 retval = target_read_u32(target, rp_addr, &rp);
852 if (retval != ERROR_OK) {
853 LOG_ERROR("failed to get read pointer");
857 LOG_DEBUG("count 0x%" PRIx32 " wp 0x%" PRIx32 " rp 0x%" PRIx32, count, wp, rp);
860 LOG_ERROR("flash write algorithm aborted by target");
861 retval = ERROR_FLASH_OPERATION_FAILED;
865 if ((rp & (block_size - 1)) || rp < fifo_start_addr || rp >= fifo_end_addr) {
866 LOG_ERROR("corrupted fifo read pointer 0x%" PRIx32, rp);
870 /* Count the number of bytes available in the fifo without
871 * crossing the wrap around. Make sure to not fill it completely,
872 * because that would make wp == rp and that's the empty condition. */
873 uint32_t thisrun_bytes;
875 thisrun_bytes = rp - wp - block_size;
876 else if (rp > fifo_start_addr)
877 thisrun_bytes = fifo_end_addr - wp;
879 thisrun_bytes = fifo_end_addr - wp - block_size;
881 if (thisrun_bytes == 0) {
882 /* Throttle polling a bit if transfer is (much) faster than flash
883 * programming. The exact delay shouldn't matter as long as it's
884 * less than buffer size / flash speed. This is very unlikely to
885 * run when using high latency connections such as USB. */
890 /* Limit to the amount of data we actually want to write */
891 if (thisrun_bytes > count * block_size)
892 thisrun_bytes = count * block_size;
894 /* Write data to fifo */
895 retval = target_write_buffer(target, wp, thisrun_bytes, buffer);
896 if (retval != ERROR_OK)
899 /* Update counters and wrap write pointer */
900 buffer += thisrun_bytes;
901 count -= thisrun_bytes / block_size;
903 if (wp >= fifo_end_addr)
904 wp = fifo_start_addr;
906 /* Store updated write pointer to target */
907 retval = target_write_u32(target, wp_addr, wp);
908 if (retval != ERROR_OK)
912 if (retval != ERROR_OK) {
913 /* abort flash write algorithm on target */
914 target_write_u32(target, wp_addr, 0);
917 int retval2 = target_wait_algorithm(target, num_mem_params, mem_params,
918 num_reg_params, reg_params,
923 if (retval2 != ERROR_OK) {
924 LOG_ERROR("error waiting for target flash write algorithm");
931 int target_read_memory(struct target *target,
932 uint32_t address, uint32_t size, uint32_t count, uint8_t *buffer)
934 return target->type->read_memory(target, address, size, count, buffer);
937 static int target_read_phys_memory(struct target *target,
938 uint32_t address, uint32_t size, uint32_t count, uint8_t *buffer)
940 return target->type->read_phys_memory(target, address, size, count, buffer);
943 int target_write_memory(struct target *target,
944 uint32_t address, uint32_t size, uint32_t count, const uint8_t *buffer)
946 return target->type->write_memory(target, address, size, count, buffer);
949 static int target_write_phys_memory(struct target *target,
950 uint32_t address, uint32_t size, uint32_t count, const uint8_t *buffer)
952 return target->type->write_phys_memory(target, address, size, count, buffer);
955 int target_bulk_write_memory(struct target *target,
956 uint32_t address, uint32_t count, const uint8_t *buffer)
958 return target->type->bulk_write_memory(target, address, count, buffer);
961 int target_add_breakpoint(struct target *target,
962 struct breakpoint *breakpoint)
964 if ((target->state != TARGET_HALTED) && (breakpoint->type != BKPT_HARD)) {
965 LOG_WARNING("target %s is not halted", target->cmd_name);
966 return ERROR_TARGET_NOT_HALTED;
968 return target->type->add_breakpoint(target, breakpoint);
971 int target_add_context_breakpoint(struct target *target,
972 struct breakpoint *breakpoint)
974 if (target->state != TARGET_HALTED) {
975 LOG_WARNING("target %s is not halted", target->cmd_name);
976 return ERROR_TARGET_NOT_HALTED;
978 return target->type->add_context_breakpoint(target, breakpoint);
981 int target_add_hybrid_breakpoint(struct target *target,
982 struct breakpoint *breakpoint)
984 if (target->state != TARGET_HALTED) {
985 LOG_WARNING("target %s is not halted", target->cmd_name);
986 return ERROR_TARGET_NOT_HALTED;
988 return target->type->add_hybrid_breakpoint(target, breakpoint);
991 int target_remove_breakpoint(struct target *target,
992 struct breakpoint *breakpoint)
994 return target->type->remove_breakpoint(target, breakpoint);
997 int target_add_watchpoint(struct target *target,
998 struct watchpoint *watchpoint)
1000 if (target->state != TARGET_HALTED) {
1001 LOG_WARNING("target %s is not halted", target->cmd_name);
1002 return ERROR_TARGET_NOT_HALTED;
1004 return target->type->add_watchpoint(target, watchpoint);
1006 int target_remove_watchpoint(struct target *target,
1007 struct watchpoint *watchpoint)
1009 return target->type->remove_watchpoint(target, watchpoint);
1012 int target_get_gdb_reg_list(struct target *target,
1013 struct reg **reg_list[], int *reg_list_size)
1015 return target->type->get_gdb_reg_list(target, reg_list, reg_list_size);
1017 int target_step(struct target *target,
1018 int current, uint32_t address, int handle_breakpoints)
1020 return target->type->step(target, current, address, handle_breakpoints);
1024 * Reset the @c examined flag for the given target.
1025 * Pure paranoia -- targets are zeroed on allocation.
1027 static void target_reset_examined(struct target *target)
1029 target->examined = false;
1032 static int err_read_phys_memory(struct target *target, uint32_t address,
1033 uint32_t size, uint32_t count, uint8_t *buffer)
1035 LOG_ERROR("Not implemented: %s", __func__);
1039 static int err_write_phys_memory(struct target *target, uint32_t address,
1040 uint32_t size, uint32_t count, const uint8_t *buffer)
1042 LOG_ERROR("Not implemented: %s", __func__);
1046 static int handle_target(void *priv);
1048 static int target_init_one(struct command_context *cmd_ctx,
1049 struct target *target)
1051 target_reset_examined(target);
1053 struct target_type *type = target->type;
1054 if (type->examine == NULL)
1055 type->examine = default_examine;
1057 if (type->check_reset == NULL)
1058 type->check_reset = default_check_reset;
1060 assert(type->init_target != NULL);
1062 int retval = type->init_target(cmd_ctx, target);
1063 if (ERROR_OK != retval) {
1064 LOG_ERROR("target '%s' init failed", target_name(target));
1069 * @todo get rid of those *memory_imp() methods, now that all
1070 * callers are using target_*_memory() accessors ... and make
1071 * sure the "physical" paths handle the same issues.
1073 /* a non-invasive way(in terms of patches) to add some code that
1074 * runs before the type->write/read_memory implementation
1076 type->write_memory_imp = target->type->write_memory;
1077 type->write_memory = target_write_memory_imp;
1079 type->read_memory_imp = target->type->read_memory;
1080 type->read_memory = target_read_memory_imp;
1082 type->soft_reset_halt_imp = target->type->soft_reset_halt;
1083 type->soft_reset_halt = target_soft_reset_halt_imp;
1085 /* Sanity-check MMU support ... stub in what we must, to help
1086 * implement it in stages, but warn if we need to do so.
1089 if (type->write_phys_memory == NULL) {
1090 LOG_ERROR("type '%s' is missing write_phys_memory",
1092 type->write_phys_memory = err_write_phys_memory;
1094 if (type->read_phys_memory == NULL) {
1095 LOG_ERROR("type '%s' is missing read_phys_memory",
1097 type->read_phys_memory = err_read_phys_memory;
1099 if (type->virt2phys == NULL) {
1100 LOG_ERROR("type '%s' is missing virt2phys", type->name);
1101 type->virt2phys = identity_virt2phys;
1104 /* Make sure no-MMU targets all behave the same: make no
1105 * distinction between physical and virtual addresses, and
1106 * ensure that virt2phys() is always an identity mapping.
1108 if (type->write_phys_memory || type->read_phys_memory || type->virt2phys)
1109 LOG_WARNING("type '%s' has bad MMU hooks", type->name);
1112 type->write_phys_memory = type->write_memory;
1113 type->read_phys_memory = type->read_memory;
1114 type->virt2phys = identity_virt2phys;
1117 if (target->type->read_buffer == NULL)
1118 target->type->read_buffer = target_read_buffer_default;
1120 if (target->type->write_buffer == NULL)
1121 target->type->write_buffer = target_write_buffer_default;
1126 static int target_init(struct command_context *cmd_ctx)
1128 struct target *target;
1131 for (target = all_targets; target; target = target->next) {
1132 retval = target_init_one(cmd_ctx, target);
1133 if (ERROR_OK != retval)
1140 retval = target_register_user_commands(cmd_ctx);
1141 if (ERROR_OK != retval)
1144 retval = target_register_timer_callback(&handle_target,
1145 polling_interval, 1, cmd_ctx->interp);
1146 if (ERROR_OK != retval)
1152 COMMAND_HANDLER(handle_target_init_command)
1157 return ERROR_COMMAND_SYNTAX_ERROR;
1159 static bool target_initialized;
1160 if (target_initialized) {
1161 LOG_INFO("'target init' has already been called");
1164 target_initialized = true;
1166 retval = command_run_line(CMD_CTX, "init_targets");
1167 if (ERROR_OK != retval)
1170 retval = command_run_line(CMD_CTX, "init_board");
1171 if (ERROR_OK != retval)
1174 LOG_DEBUG("Initializing targets...");
1175 return target_init(CMD_CTX);
1178 int target_register_event_callback(int (*callback)(struct target *target,
1179 enum target_event event, void *priv), void *priv)
1181 struct target_event_callback **callbacks_p = &target_event_callbacks;
1183 if (callback == NULL)
1184 return ERROR_COMMAND_SYNTAX_ERROR;
1187 while ((*callbacks_p)->next)
1188 callbacks_p = &((*callbacks_p)->next);
1189 callbacks_p = &((*callbacks_p)->next);
1192 (*callbacks_p) = malloc(sizeof(struct target_event_callback));
1193 (*callbacks_p)->callback = callback;
1194 (*callbacks_p)->priv = priv;
1195 (*callbacks_p)->next = NULL;
1200 int target_register_timer_callback(int (*callback)(void *priv), int time_ms, int periodic, void *priv)
1202 struct target_timer_callback **callbacks_p = &target_timer_callbacks;
1205 if (callback == NULL)
1206 return ERROR_COMMAND_SYNTAX_ERROR;
1209 while ((*callbacks_p)->next)
1210 callbacks_p = &((*callbacks_p)->next);
1211 callbacks_p = &((*callbacks_p)->next);
1214 (*callbacks_p) = malloc(sizeof(struct target_timer_callback));
1215 (*callbacks_p)->callback = callback;
1216 (*callbacks_p)->periodic = periodic;
1217 (*callbacks_p)->time_ms = time_ms;
1219 gettimeofday(&now, NULL);
1220 (*callbacks_p)->when.tv_usec = now.tv_usec + (time_ms % 1000) * 1000;
1221 time_ms -= (time_ms % 1000);
1222 (*callbacks_p)->when.tv_sec = now.tv_sec + (time_ms / 1000);
1223 if ((*callbacks_p)->when.tv_usec > 1000000) {
1224 (*callbacks_p)->when.tv_usec = (*callbacks_p)->when.tv_usec - 1000000;
1225 (*callbacks_p)->when.tv_sec += 1;
1228 (*callbacks_p)->priv = priv;
1229 (*callbacks_p)->next = NULL;
1234 int target_unregister_event_callback(int (*callback)(struct target *target,
1235 enum target_event event, void *priv), void *priv)
1237 struct target_event_callback **p = &target_event_callbacks;
1238 struct target_event_callback *c = target_event_callbacks;
1240 if (callback == NULL)
1241 return ERROR_COMMAND_SYNTAX_ERROR;
1244 struct target_event_callback *next = c->next;
1245 if ((c->callback == callback) && (c->priv == priv)) {
1257 static int target_unregister_timer_callback(int (*callback)(void *priv), void *priv)
1259 struct target_timer_callback **p = &target_timer_callbacks;
1260 struct target_timer_callback *c = target_timer_callbacks;
1262 if (callback == NULL)
1263 return ERROR_COMMAND_SYNTAX_ERROR;
1266 struct target_timer_callback *next = c->next;
1267 if ((c->callback == callback) && (c->priv == priv)) {
1279 int target_call_event_callbacks(struct target *target, enum target_event event)
1281 struct target_event_callback *callback = target_event_callbacks;
1282 struct target_event_callback *next_callback;
1284 if (event == TARGET_EVENT_HALTED) {
1285 /* execute early halted first */
1286 target_call_event_callbacks(target, TARGET_EVENT_GDB_HALT);
1289 LOG_DEBUG("target event %i (%s)", event,
1290 Jim_Nvp_value2name_simple(nvp_target_event, event)->name);
1292 target_handle_event(target, event);
1295 next_callback = callback->next;
1296 callback->callback(target, event, callback->priv);
1297 callback = next_callback;
1303 static int target_timer_callback_periodic_restart(
1304 struct target_timer_callback *cb, struct timeval *now)
1306 int time_ms = cb->time_ms;
1307 cb->when.tv_usec = now->tv_usec + (time_ms % 1000) * 1000;
1308 time_ms -= (time_ms % 1000);
1309 cb->when.tv_sec = now->tv_sec + time_ms / 1000;
1310 if (cb->when.tv_usec > 1000000) {
1311 cb->when.tv_usec = cb->when.tv_usec - 1000000;
1312 cb->when.tv_sec += 1;
1317 static int target_call_timer_callback(struct target_timer_callback *cb,
1318 struct timeval *now)
1320 cb->callback(cb->priv);
1323 return target_timer_callback_periodic_restart(cb, now);
1325 return target_unregister_timer_callback(cb->callback, cb->priv);
1328 static int target_call_timer_callbacks_check_time(int checktime)
1333 gettimeofday(&now, NULL);
1335 struct target_timer_callback *callback = target_timer_callbacks;
1337 /* cleaning up may unregister and free this callback */
1338 struct target_timer_callback *next_callback = callback->next;
1340 bool call_it = callback->callback &&
1341 ((!checktime && callback->periodic) ||
1342 now.tv_sec > callback->when.tv_sec ||
1343 (now.tv_sec == callback->when.tv_sec &&
1344 now.tv_usec >= callback->when.tv_usec));
1347 int retval = target_call_timer_callback(callback, &now);
1348 if (retval != ERROR_OK)
1352 callback = next_callback;
1358 int target_call_timer_callbacks(void)
1360 return target_call_timer_callbacks_check_time(1);
1363 /* invoke periodic callbacks immediately */
1364 int target_call_timer_callbacks_now(void)
1366 return target_call_timer_callbacks_check_time(0);
1369 /* Prints the working area layout for debug purposes */
1370 static void print_wa_layout(struct target *target)
1372 struct working_area *c = target->working_areas;
1375 LOG_DEBUG("%c%c 0x%08"PRIx32"-0x%08"PRIx32" (%"PRIu32" bytes)",
1376 c->backup ? 'b' : ' ', c->free ? ' ' : '*',
1377 c->address, c->address + c->size - 1, c->size);
1382 /* Reduce area to size bytes, create a new free area from the remaining bytes, if any. */
1383 static void target_split_working_area(struct working_area *area, uint32_t size)
1385 assert(area->free); /* Shouldn't split an allocated area */
1386 assert(size <= area->size); /* Caller should guarantee this */
1388 /* Split only if not already the right size */
1389 if (size < area->size) {
1390 struct working_area *new_wa = malloc(sizeof(*new_wa));
1395 new_wa->next = area->next;
1396 new_wa->size = area->size - size;
1397 new_wa->address = area->address + size;
1398 new_wa->backup = NULL;
1399 new_wa->user = NULL;
1400 new_wa->free = true;
1402 area->next = new_wa;
1405 /* If backup memory was allocated to this area, it has the wrong size
1406 * now so free it and it will be reallocated if/when needed */
1409 area->backup = NULL;
1414 /* Merge all adjacent free areas into one */
1415 static void target_merge_working_areas(struct target *target)
1417 struct working_area *c = target->working_areas;
1419 while (c && c->next) {
1420 assert(c->next->address == c->address + c->size); /* This is an invariant */
1422 /* Find two adjacent free areas */
1423 if (c->free && c->next->free) {
1424 /* Merge the last into the first */
1425 c->size += c->next->size;
1427 /* Remove the last */
1428 struct working_area *to_be_freed = c->next;
1429 c->next = c->next->next;
1430 if (to_be_freed->backup)
1431 free(to_be_freed->backup);
1434 /* If backup memory was allocated to the remaining area, it's has
1435 * the wrong size now */
1446 int target_alloc_working_area_try(struct target *target, uint32_t size, struct working_area **area)
1448 /* Reevaluate working area address based on MMU state*/
1449 if (target->working_areas == NULL) {
1453 retval = target->type->mmu(target, &enabled);
1454 if (retval != ERROR_OK)
1458 if (target->working_area_phys_spec) {
1459 LOG_DEBUG("MMU disabled, using physical "
1460 "address for working memory 0x%08"PRIx32,
1461 target->working_area_phys);
1462 target->working_area = target->working_area_phys;
1464 LOG_ERROR("No working memory available. "
1465 "Specify -work-area-phys to target.");
1466 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
1469 if (target->working_area_virt_spec) {
1470 LOG_DEBUG("MMU enabled, using virtual "
1471 "address for working memory 0x%08"PRIx32,
1472 target->working_area_virt);
1473 target->working_area = target->working_area_virt;
1475 LOG_ERROR("No working memory available. "
1476 "Specify -work-area-virt to target.");
1477 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
1481 /* Set up initial working area on first call */
1482 struct working_area *new_wa = malloc(sizeof(*new_wa));
1484 new_wa->next = NULL;
1485 new_wa->size = target->working_area_size & ~3UL; /* 4-byte align */
1486 new_wa->address = target->working_area;
1487 new_wa->backup = NULL;
1488 new_wa->user = NULL;
1489 new_wa->free = true;
1492 target->working_areas = new_wa;
1495 /* only allocate multiples of 4 byte */
1497 size = (size + 3) & (~3UL);
1499 struct working_area *c = target->working_areas;
1501 /* Find the first large enough working area */
1503 if (c->free && c->size >= size)
1509 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
1511 /* Split the working area into the requested size */
1512 target_split_working_area(c, size);
1514 LOG_DEBUG("allocated new working area of %"PRIu32" bytes at address 0x%08"PRIx32, size, c->address);
1516 if (target->backup_working_area) {
1517 if (c->backup == NULL) {
1518 c->backup = malloc(c->size);
1519 if (c->backup == NULL)
1523 int retval = target_read_memory(target, c->address, 4, c->size / 4, c->backup);
1524 if (retval != ERROR_OK)
1528 /* mark as used, and return the new (reused) area */
1535 print_wa_layout(target);
1540 int target_alloc_working_area(struct target *target, uint32_t size, struct working_area **area)
1544 retval = target_alloc_working_area_try(target, size, area);
1545 if (retval == ERROR_TARGET_RESOURCE_NOT_AVAILABLE)
1546 LOG_WARNING("not enough working area available(requested %"PRIu32")", size);
1551 static int target_restore_working_area(struct target *target, struct working_area *area)
1553 int retval = ERROR_OK;
1555 if (target->backup_working_area && area->backup != NULL) {
1556 retval = target_write_memory(target, area->address, 4, area->size / 4, area->backup);
1557 if (retval != ERROR_OK)
1558 LOG_ERROR("failed to restore %"PRIu32" bytes of working area at address 0x%08"PRIx32,
1559 area->size, area->address);
1565 /* Restore the area's backup memory, if any, and return the area to the allocation pool */
1566 static int target_free_working_area_restore(struct target *target, struct working_area *area, int restore)
1568 int retval = ERROR_OK;
1574 retval = target_restore_working_area(target, area);
1575 /* REVISIT: Perhaps the area should be freed even if restoring fails. */
1576 if (retval != ERROR_OK)
1582 LOG_DEBUG("freed %"PRIu32" bytes of working area at address 0x%08"PRIx32,
1583 area->size, area->address);
1585 /* mark user pointer invalid */
1586 /* TODO: Is this really safe? It points to some previous caller's memory.
1587 * How could we know that the area pointer is still in that place and not
1588 * some other vital data? What's the purpose of this, anyway? */
1592 target_merge_working_areas(target);
1594 print_wa_layout(target);
1599 int target_free_working_area(struct target *target, struct working_area *area)
1601 return target_free_working_area_restore(target, area, 1);
1604 /* free resources and restore memory, if restoring memory fails,
1605 * free up resources anyway
1607 static void target_free_all_working_areas_restore(struct target *target, int restore)
1609 struct working_area *c = target->working_areas;
1611 LOG_DEBUG("freeing all working areas");
1613 /* Loop through all areas, restoring the allocated ones and marking them as free */
1617 target_restore_working_area(target, c);
1619 *c->user = NULL; /* Same as above */
1625 /* Run a merge pass to combine all areas into one */
1626 target_merge_working_areas(target);
1628 print_wa_layout(target);
1631 void target_free_all_working_areas(struct target *target)
1633 target_free_all_working_areas_restore(target, 1);
1636 /* Find the largest number of bytes that can be allocated */
1637 uint32_t target_get_working_area_avail(struct target *target)
1639 struct working_area *c = target->working_areas;
1640 uint32_t max_size = 0;
1643 return target->working_area_size;
1646 if (c->free && max_size < c->size)
1655 int target_arch_state(struct target *target)
1658 if (target == NULL) {
1659 LOG_USER("No target has been configured");
1663 LOG_USER("target state: %s", target_state_name(target));
1665 if (target->state != TARGET_HALTED)
1668 retval = target->type->arch_state(target);
1672 /* Single aligned words are guaranteed to use 16 or 32 bit access
1673 * mode respectively, otherwise data is handled as quickly as
1676 int target_write_buffer(struct target *target, uint32_t address, uint32_t size, const uint8_t *buffer)
1678 LOG_DEBUG("writing buffer of %i byte at 0x%8.8x",
1679 (int)size, (unsigned)address);
1681 if (!target_was_examined(target)) {
1682 LOG_ERROR("Target not examined yet");
1689 if ((address + size - 1) < address) {
1690 /* GDB can request this when e.g. PC is 0xfffffffc*/
1691 LOG_ERROR("address + size wrapped(0x%08x, 0x%08x)",
1697 return target->type->write_buffer(target, address, size, buffer);
1700 static int target_write_buffer_default(struct target *target, uint32_t address, uint32_t size, const uint8_t *buffer)
1702 int retval = ERROR_OK;
1704 if (((address % 2) == 0) && (size == 2))
1705 return target_write_memory(target, address, 2, 1, buffer);
1707 /* handle unaligned head bytes */
1709 uint32_t unaligned = 4 - (address % 4);
1711 if (unaligned > size)
1714 retval = target_write_memory(target, address, 1, unaligned, buffer);
1715 if (retval != ERROR_OK)
1718 buffer += unaligned;
1719 address += unaligned;
1723 /* handle aligned words */
1725 int aligned = size - (size % 4);
1727 /* use bulk writes above a certain limit. This may have to be changed */
1728 if (aligned > 128) {
1729 retval = target->type->bulk_write_memory(target, address, aligned / 4, buffer);
1730 if (retval != ERROR_OK)
1733 retval = target_write_memory(target, address, 4, aligned / 4, buffer);
1734 if (retval != ERROR_OK)
1743 /* handle tail writes of less than 4 bytes */
1745 retval = target_write_memory(target, address, 1, size, buffer);
1746 if (retval != ERROR_OK)
1753 /* Single aligned words are guaranteed to use 16 or 32 bit access
1754 * mode respectively, otherwise data is handled as quickly as
1757 int target_read_buffer(struct target *target, uint32_t address, uint32_t size, uint8_t *buffer)
1759 LOG_DEBUG("reading buffer of %i byte at 0x%8.8x",
1760 (int)size, (unsigned)address);
1762 if (!target_was_examined(target)) {
1763 LOG_ERROR("Target not examined yet");
1770 if ((address + size - 1) < address) {
1771 /* GDB can request this when e.g. PC is 0xfffffffc*/
1772 LOG_ERROR("address + size wrapped(0x%08" PRIx32 ", 0x%08" PRIx32 ")",
1778 return target->type->read_buffer(target, address, size, buffer);
1781 static int target_read_buffer_default(struct target *target, uint32_t address, uint32_t size, uint8_t *buffer)
1783 int retval = ERROR_OK;
1785 if (((address % 2) == 0) && (size == 2))
1786 return target_read_memory(target, address, 2, 1, buffer);
1788 /* handle unaligned head bytes */
1790 uint32_t unaligned = 4 - (address % 4);
1792 if (unaligned > size)
1795 retval = target_read_memory(target, address, 1, unaligned, buffer);
1796 if (retval != ERROR_OK)
1799 buffer += unaligned;
1800 address += unaligned;
1804 /* handle aligned words */
1806 int aligned = size - (size % 4);
1808 retval = target_read_memory(target, address, 4, aligned / 4, buffer);
1809 if (retval != ERROR_OK)
1817 /*prevent byte access when possible (avoid AHB access limitations in some cases)*/
1819 int aligned = size - (size % 2);
1820 retval = target_read_memory(target, address, 2, aligned / 2, buffer);
1821 if (retval != ERROR_OK)
1828 /* handle tail writes of less than 4 bytes */
1830 retval = target_read_memory(target, address, 1, size, buffer);
1831 if (retval != ERROR_OK)
1838 int target_checksum_memory(struct target *target, uint32_t address, uint32_t size, uint32_t* crc)
1843 uint32_t checksum = 0;
1844 if (!target_was_examined(target)) {
1845 LOG_ERROR("Target not examined yet");
1849 retval = target->type->checksum_memory(target, address, size, &checksum);
1850 if (retval != ERROR_OK) {
1851 buffer = malloc(size);
1852 if (buffer == NULL) {
1853 LOG_ERROR("error allocating buffer for section (%d bytes)", (int)size);
1854 return ERROR_COMMAND_SYNTAX_ERROR;
1856 retval = target_read_buffer(target, address, size, buffer);
1857 if (retval != ERROR_OK) {
1862 /* convert to target endianness */
1863 for (i = 0; i < (size/sizeof(uint32_t)); i++) {
1864 uint32_t target_data;
1865 target_data = target_buffer_get_u32(target, &buffer[i*sizeof(uint32_t)]);
1866 target_buffer_set_u32(target, &buffer[i*sizeof(uint32_t)], target_data);
1869 retval = image_calculate_checksum(buffer, size, &checksum);
1878 int target_blank_check_memory(struct target *target, uint32_t address, uint32_t size, uint32_t* blank)
1881 if (!target_was_examined(target)) {
1882 LOG_ERROR("Target not examined yet");
1886 if (target->type->blank_check_memory == 0)
1887 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
1889 retval = target->type->blank_check_memory(target, address, size, blank);
1894 int target_read_u32(struct target *target, uint32_t address, uint32_t *value)
1896 uint8_t value_buf[4];
1897 if (!target_was_examined(target)) {
1898 LOG_ERROR("Target not examined yet");
1902 int retval = target_read_memory(target, address, 4, 1, value_buf);
1904 if (retval == ERROR_OK) {
1905 *value = target_buffer_get_u32(target, value_buf);
1906 LOG_DEBUG("address: 0x%8.8" PRIx32 ", value: 0x%8.8" PRIx32 "",
1911 LOG_DEBUG("address: 0x%8.8" PRIx32 " failed",
1918 int target_read_u16(struct target *target, uint32_t address, uint16_t *value)
1920 uint8_t value_buf[2];
1921 if (!target_was_examined(target)) {
1922 LOG_ERROR("Target not examined yet");
1926 int retval = target_read_memory(target, address, 2, 1, value_buf);
1928 if (retval == ERROR_OK) {
1929 *value = target_buffer_get_u16(target, value_buf);
1930 LOG_DEBUG("address: 0x%8.8" PRIx32 ", value: 0x%4.4x",
1935 LOG_DEBUG("address: 0x%8.8" PRIx32 " failed",
1942 int target_read_u8(struct target *target, uint32_t address, uint8_t *value)
1944 int retval = target_read_memory(target, address, 1, 1, value);
1945 if (!target_was_examined(target)) {
1946 LOG_ERROR("Target not examined yet");
1950 if (retval == ERROR_OK) {
1951 LOG_DEBUG("address: 0x%8.8" PRIx32 ", value: 0x%2.2x",
1956 LOG_DEBUG("address: 0x%8.8" PRIx32 " failed",
1963 int target_write_u32(struct target *target, uint32_t address, uint32_t value)
1966 uint8_t value_buf[4];
1967 if (!target_was_examined(target)) {
1968 LOG_ERROR("Target not examined yet");
1972 LOG_DEBUG("address: 0x%8.8" PRIx32 ", value: 0x%8.8" PRIx32 "",
1976 target_buffer_set_u32(target, value_buf, value);
1977 retval = target_write_memory(target, address, 4, 1, value_buf);
1978 if (retval != ERROR_OK)
1979 LOG_DEBUG("failed: %i", retval);
1984 int target_write_u16(struct target *target, uint32_t address, uint16_t value)
1987 uint8_t value_buf[2];
1988 if (!target_was_examined(target)) {
1989 LOG_ERROR("Target not examined yet");
1993 LOG_DEBUG("address: 0x%8.8" PRIx32 ", value: 0x%8.8x",
1997 target_buffer_set_u16(target, value_buf, value);
1998 retval = target_write_memory(target, address, 2, 1, value_buf);
1999 if (retval != ERROR_OK)
2000 LOG_DEBUG("failed: %i", retval);
2005 int target_write_u8(struct target *target, uint32_t address, uint8_t value)
2008 if (!target_was_examined(target)) {
2009 LOG_ERROR("Target not examined yet");
2013 LOG_DEBUG("address: 0x%8.8" PRIx32 ", value: 0x%2.2x",
2016 retval = target_write_memory(target, address, 1, 1, &value);
2017 if (retval != ERROR_OK)
2018 LOG_DEBUG("failed: %i", retval);
2023 static int find_target(struct command_context *cmd_ctx, const char *name)
2025 struct target *target = get_target(name);
2026 if (target == NULL) {
2027 LOG_ERROR("Target: %s is unknown, try one of:\n", name);
2030 if (!target->tap->enabled) {
2031 LOG_USER("Target: TAP %s is disabled, "
2032 "can't be the current target\n",
2033 target->tap->dotted_name);
2037 cmd_ctx->current_target = target->target_number;
2042 COMMAND_HANDLER(handle_targets_command)
2044 int retval = ERROR_OK;
2045 if (CMD_ARGC == 1) {
2046 retval = find_target(CMD_CTX, CMD_ARGV[0]);
2047 if (retval == ERROR_OK) {
2053 struct target *target = all_targets;
2054 command_print(CMD_CTX, " TargetName Type Endian TapName State ");
2055 command_print(CMD_CTX, "-- ------------------ ---------- ------ ------------------ ------------");
2060 if (target->tap->enabled)
2061 state = target_state_name(target);
2063 state = "tap-disabled";
2065 if (CMD_CTX->current_target == target->target_number)
2068 /* keep columns lined up to match the headers above */
2069 command_print(CMD_CTX,
2070 "%2d%c %-18s %-10s %-6s %-18s %s",
2071 target->target_number,
2073 target_name(target),
2074 target_type_name(target),
2075 Jim_Nvp_value2name_simple(nvp_target_endian,
2076 target->endianness)->name,
2077 target->tap->dotted_name,
2079 target = target->next;
2085 /* every 300ms we check for reset & powerdropout and issue a "reset halt" if so. */
2087 static int powerDropout;
2088 static int srstAsserted;
2090 static int runPowerRestore;
2091 static int runPowerDropout;
2092 static int runSrstAsserted;
2093 static int runSrstDeasserted;
2095 static int sense_handler(void)
2097 static int prevSrstAsserted;
2098 static int prevPowerdropout;
2100 int retval = jtag_power_dropout(&powerDropout);
2101 if (retval != ERROR_OK)
2105 powerRestored = prevPowerdropout && !powerDropout;
2107 runPowerRestore = 1;
2109 long long current = timeval_ms();
2110 static long long lastPower;
2111 int waitMore = lastPower + 2000 > current;
2112 if (powerDropout && !waitMore) {
2113 runPowerDropout = 1;
2114 lastPower = current;
2117 retval = jtag_srst_asserted(&srstAsserted);
2118 if (retval != ERROR_OK)
2122 srstDeasserted = prevSrstAsserted && !srstAsserted;
2124 static long long lastSrst;
2125 waitMore = lastSrst + 2000 > current;
2126 if (srstDeasserted && !waitMore) {
2127 runSrstDeasserted = 1;
2131 if (!prevSrstAsserted && srstAsserted)
2132 runSrstAsserted = 1;
2134 prevSrstAsserted = srstAsserted;
2135 prevPowerdropout = powerDropout;
2137 if (srstDeasserted || powerRestored) {
2138 /* Other than logging the event we can't do anything here.
2139 * Issuing a reset is a particularly bad idea as we might
2140 * be inside a reset already.
2147 static int backoff_times;
2148 static int backoff_count;
2150 /* process target state changes */
2151 static int handle_target(void *priv)
2153 Jim_Interp *interp = (Jim_Interp *)priv;
2154 int retval = ERROR_OK;
2156 if (!is_jtag_poll_safe()) {
2157 /* polling is disabled currently */
2161 /* we do not want to recurse here... */
2162 static int recursive;
2166 /* danger! running these procedures can trigger srst assertions and power dropouts.
2167 * We need to avoid an infinite loop/recursion here and we do that by
2168 * clearing the flags after running these events.
2170 int did_something = 0;
2171 if (runSrstAsserted) {
2172 LOG_INFO("srst asserted detected, running srst_asserted proc.");
2173 Jim_Eval(interp, "srst_asserted");
2176 if (runSrstDeasserted) {
2177 Jim_Eval(interp, "srst_deasserted");
2180 if (runPowerDropout) {
2181 LOG_INFO("Power dropout detected, running power_dropout proc.");
2182 Jim_Eval(interp, "power_dropout");
2185 if (runPowerRestore) {
2186 Jim_Eval(interp, "power_restore");
2190 if (did_something) {
2191 /* clear detect flags */
2195 /* clear action flags */
2197 runSrstAsserted = 0;
2198 runSrstDeasserted = 0;
2199 runPowerRestore = 0;
2200 runPowerDropout = 0;
2205 if (backoff_times > backoff_count) {
2206 /* do not poll this time as we failed previously */
2212 /* Poll targets for state changes unless that's globally disabled.
2213 * Skip targets that are currently disabled.
2215 for (struct target *target = all_targets;
2216 is_jtag_poll_safe() && target;
2217 target = target->next) {
2218 if (!target->tap->enabled)
2221 /* only poll target if we've got power and srst isn't asserted */
2222 if (!powerDropout && !srstAsserted) {
2223 /* polling may fail silently until the target has been examined */
2224 retval = target_poll(target);
2225 if (retval != ERROR_OK) {
2226 /* 100ms polling interval. Increase interval between polling up to 5000ms */
2227 if (backoff_times * polling_interval < 5000) {
2231 LOG_USER("Polling target failed, GDB will be halted. Polling again in %dms",
2232 backoff_times * polling_interval);
2234 /* Tell GDB to halt the debugger. This allows the user to
2235 * run monitor commands to handle the situation.
2237 target_call_event_callbacks(target, TARGET_EVENT_GDB_HALT);
2240 /* Since we succeeded, we reset backoff count */
2241 if (backoff_times > 0)
2242 LOG_USER("Polling succeeded again");
2250 COMMAND_HANDLER(handle_reg_command)
2252 struct target *target;
2253 struct reg *reg = NULL;
2259 target = get_current_target(CMD_CTX);
2261 /* list all available registers for the current target */
2262 if (CMD_ARGC == 0) {
2263 struct reg_cache *cache = target->reg_cache;
2269 command_print(CMD_CTX, "===== %s", cache->name);
2271 for (i = 0, reg = cache->reg_list;
2272 i < cache->num_regs;
2273 i++, reg++, count++) {
2274 /* only print cached values if they are valid */
2276 value = buf_to_str(reg->value,
2278 command_print(CMD_CTX,
2279 "(%i) %s (/%" PRIu32 "): 0x%s%s",
2287 command_print(CMD_CTX, "(%i) %s (/%" PRIu32 ")",
2292 cache = cache->next;
2298 /* access a single register by its ordinal number */
2299 if ((CMD_ARGV[0][0] >= '0') && (CMD_ARGV[0][0] <= '9')) {
2301 COMMAND_PARSE_NUMBER(uint, CMD_ARGV[0], num);
2303 struct reg_cache *cache = target->reg_cache;
2307 for (i = 0; i < cache->num_regs; i++) {
2308 if (count++ == num) {
2309 reg = &cache->reg_list[i];
2315 cache = cache->next;
2319 command_print(CMD_CTX, "%i is out of bounds, the current target "
2320 "has only %i registers (0 - %i)", num, count, count - 1);
2324 /* access a single register by its name */
2325 reg = register_get_by_name(target->reg_cache, CMD_ARGV[0], 1);
2328 command_print(CMD_CTX, "register %s not found in current target", CMD_ARGV[0]);
2333 assert(reg != NULL); /* give clang a hint that we *know* reg is != NULL here */
2335 /* display a register */
2336 if ((CMD_ARGC == 1) || ((CMD_ARGC == 2) && !((CMD_ARGV[1][0] >= '0')
2337 && (CMD_ARGV[1][0] <= '9')))) {
2338 if ((CMD_ARGC == 2) && (strcmp(CMD_ARGV[1], "force") == 0))
2341 if (reg->valid == 0)
2342 reg->type->get(reg);
2343 value = buf_to_str(reg->value, reg->size, 16);
2344 command_print(CMD_CTX, "%s (/%i): 0x%s", reg->name, (int)(reg->size), value);
2349 /* set register value */
2350 if (CMD_ARGC == 2) {
2351 uint8_t *buf = malloc(DIV_ROUND_UP(reg->size, 8));
2354 str_to_buf(CMD_ARGV[1], strlen(CMD_ARGV[1]), buf, reg->size, 0);
2356 reg->type->set(reg, buf);
2358 value = buf_to_str(reg->value, reg->size, 16);
2359 command_print(CMD_CTX, "%s (/%i): 0x%s", reg->name, (int)(reg->size), value);
2367 return ERROR_COMMAND_SYNTAX_ERROR;
2370 COMMAND_HANDLER(handle_poll_command)
2372 int retval = ERROR_OK;
2373 struct target *target = get_current_target(CMD_CTX);
2375 if (CMD_ARGC == 0) {
2376 command_print(CMD_CTX, "background polling: %s",
2377 jtag_poll_get_enabled() ? "on" : "off");
2378 command_print(CMD_CTX, "TAP: %s (%s)",
2379 target->tap->dotted_name,
2380 target->tap->enabled ? "enabled" : "disabled");
2381 if (!target->tap->enabled)
2383 retval = target_poll(target);
2384 if (retval != ERROR_OK)
2386 retval = target_arch_state(target);
2387 if (retval != ERROR_OK)
2389 } else if (CMD_ARGC == 1) {
2391 COMMAND_PARSE_ON_OFF(CMD_ARGV[0], enable);
2392 jtag_poll_set_enabled(enable);
2394 return ERROR_COMMAND_SYNTAX_ERROR;
2399 COMMAND_HANDLER(handle_wait_halt_command)
2402 return ERROR_COMMAND_SYNTAX_ERROR;
2405 if (1 == CMD_ARGC) {
2406 int retval = parse_uint(CMD_ARGV[0], &ms);
2407 if (ERROR_OK != retval)
2408 return ERROR_COMMAND_SYNTAX_ERROR;
2409 /* convert seconds (given) to milliseconds (needed) */
2413 struct target *target = get_current_target(CMD_CTX);
2414 return target_wait_state(target, TARGET_HALTED, ms);
2417 /* wait for target state to change. The trick here is to have a low
2418 * latency for short waits and not to suck up all the CPU time
2421 * After 500ms, keep_alive() is invoked
2423 int target_wait_state(struct target *target, enum target_state state, int ms)
2426 long long then = 0, cur;
2430 retval = target_poll(target);
2431 if (retval != ERROR_OK)
2433 if (target->state == state)
2438 then = timeval_ms();
2439 LOG_DEBUG("waiting for target %s...",
2440 Jim_Nvp_value2name_simple(nvp_target_state, state)->name);
2446 if ((cur-then) > ms) {
2447 LOG_ERROR("timed out while waiting for target %s",
2448 Jim_Nvp_value2name_simple(nvp_target_state, state)->name);
2456 COMMAND_HANDLER(handle_halt_command)
2460 struct target *target = get_current_target(CMD_CTX);
2461 int retval = target_halt(target);
2462 if (ERROR_OK != retval)
2465 if (CMD_ARGC == 1) {
2466 unsigned wait_local;
2467 retval = parse_uint(CMD_ARGV[0], &wait_local);
2468 if (ERROR_OK != retval)
2469 return ERROR_COMMAND_SYNTAX_ERROR;
2474 return CALL_COMMAND_HANDLER(handle_wait_halt_command);
2477 COMMAND_HANDLER(handle_soft_reset_halt_command)
2479 struct target *target = get_current_target(CMD_CTX);
2481 LOG_USER("requesting target halt and executing a soft reset");
2483 target->type->soft_reset_halt(target);
2488 COMMAND_HANDLER(handle_reset_command)
2491 return ERROR_COMMAND_SYNTAX_ERROR;
2493 enum target_reset_mode reset_mode = RESET_RUN;
2494 if (CMD_ARGC == 1) {
2496 n = Jim_Nvp_name2value_simple(nvp_reset_modes, CMD_ARGV[0]);
2497 if ((n->name == NULL) || (n->value == RESET_UNKNOWN))
2498 return ERROR_COMMAND_SYNTAX_ERROR;
2499 reset_mode = n->value;
2502 /* reset *all* targets */
2503 return target_process_reset(CMD_CTX, reset_mode);
2507 COMMAND_HANDLER(handle_resume_command)
2511 return ERROR_COMMAND_SYNTAX_ERROR;
2513 struct target *target = get_current_target(CMD_CTX);
2514 target_handle_event(target, TARGET_EVENT_OLD_pre_resume);
2516 /* with no CMD_ARGV, resume from current pc, addr = 0,
2517 * with one arguments, addr = CMD_ARGV[0],
2518 * handle breakpoints, not debugging */
2520 if (CMD_ARGC == 1) {
2521 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], addr);
2525 return target_resume(target, current, addr, 1, 0);
2528 COMMAND_HANDLER(handle_step_command)
2531 return ERROR_COMMAND_SYNTAX_ERROR;
2535 /* with no CMD_ARGV, step from current pc, addr = 0,
2536 * with one argument addr = CMD_ARGV[0],
2537 * handle breakpoints, debugging */
2540 if (CMD_ARGC == 1) {
2541 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], addr);
2545 struct target *target = get_current_target(CMD_CTX);
2547 return target->type->step(target, current_pc, addr, 1);
2550 static void handle_md_output(struct command_context *cmd_ctx,
2551 struct target *target, uint32_t address, unsigned size,
2552 unsigned count, const uint8_t *buffer)
2554 const unsigned line_bytecnt = 32;
2555 unsigned line_modulo = line_bytecnt / size;
2557 char output[line_bytecnt * 4 + 1];
2558 unsigned output_len = 0;
2560 const char *value_fmt;
2563 value_fmt = "%8.8x ";
2566 value_fmt = "%4.4x ";
2569 value_fmt = "%2.2x ";
2572 /* "can't happen", caller checked */
2573 LOG_ERROR("invalid memory read size: %u", size);
2577 for (unsigned i = 0; i < count; i++) {
2578 if (i % line_modulo == 0) {
2579 output_len += snprintf(output + output_len,
2580 sizeof(output) - output_len,
2582 (unsigned)(address + (i*size)));
2586 const uint8_t *value_ptr = buffer + i * size;
2589 value = target_buffer_get_u32(target, value_ptr);
2592 value = target_buffer_get_u16(target, value_ptr);
2597 output_len += snprintf(output + output_len,
2598 sizeof(output) - output_len,
2601 if ((i % line_modulo == line_modulo - 1) || (i == count - 1)) {
2602 command_print(cmd_ctx, "%s", output);
2608 COMMAND_HANDLER(handle_md_command)
2611 return ERROR_COMMAND_SYNTAX_ERROR;
2614 switch (CMD_NAME[2]) {
2625 return ERROR_COMMAND_SYNTAX_ERROR;
2628 bool physical = strcmp(CMD_ARGV[0], "phys") == 0;
2629 int (*fn)(struct target *target,
2630 uint32_t address, uint32_t size_value, uint32_t count, uint8_t *buffer);
2634 fn = target_read_phys_memory;
2636 fn = target_read_memory;
2637 if ((CMD_ARGC < 1) || (CMD_ARGC > 2))
2638 return ERROR_COMMAND_SYNTAX_ERROR;
2641 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], address);
2645 COMMAND_PARSE_NUMBER(uint, CMD_ARGV[1], count);
2647 uint8_t *buffer = calloc(count, size);
2649 struct target *target = get_current_target(CMD_CTX);
2650 int retval = fn(target, address, size, count, buffer);
2651 if (ERROR_OK == retval)
2652 handle_md_output(CMD_CTX, target, address, size, count, buffer);
2659 typedef int (*target_write_fn)(struct target *target,
2660 uint32_t address, uint32_t size, uint32_t count, const uint8_t *buffer);
2662 static int target_write_memory_fast(struct target *target,
2663 uint32_t address, uint32_t size, uint32_t count, const uint8_t *buffer)
2665 return target_write_buffer(target, address, size * count, buffer);
2668 static int target_fill_mem(struct target *target,
2677 /* We have to write in reasonably large chunks to be able
2678 * to fill large memory areas with any sane speed */
2679 const unsigned chunk_size = 16384;
2680 uint8_t *target_buf = malloc(chunk_size * data_size);
2681 if (target_buf == NULL) {
2682 LOG_ERROR("Out of memory");
2686 for (unsigned i = 0; i < chunk_size; i++) {
2687 switch (data_size) {
2689 target_buffer_set_u32(target, target_buf + i * data_size, b);
2692 target_buffer_set_u16(target, target_buf + i * data_size, b);
2695 target_buffer_set_u8(target, target_buf + i * data_size, b);
2702 int retval = ERROR_OK;
2704 for (unsigned x = 0; x < c; x += chunk_size) {
2707 if (current > chunk_size)
2708 current = chunk_size;
2709 retval = fn(target, address + x * data_size, data_size, current, target_buf);
2710 if (retval != ERROR_OK)
2712 /* avoid GDB timeouts */
2721 COMMAND_HANDLER(handle_mw_command)
2724 return ERROR_COMMAND_SYNTAX_ERROR;
2725 bool physical = strcmp(CMD_ARGV[0], "phys") == 0;
2730 fn = target_write_phys_memory;
2732 fn = target_write_memory_fast;
2733 if ((CMD_ARGC < 2) || (CMD_ARGC > 3))
2734 return ERROR_COMMAND_SYNTAX_ERROR;
2737 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], address);
2740 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], value);
2744 COMMAND_PARSE_NUMBER(uint, CMD_ARGV[2], count);
2746 struct target *target = get_current_target(CMD_CTX);
2748 switch (CMD_NAME[2]) {
2759 return ERROR_COMMAND_SYNTAX_ERROR;
2762 return target_fill_mem(target, address, fn, wordsize, value, count);
2765 static COMMAND_HELPER(parse_load_image_command_CMD_ARGV, struct image *image,
2766 uint32_t *min_address, uint32_t *max_address)
2768 if (CMD_ARGC < 1 || CMD_ARGC > 5)
2769 return ERROR_COMMAND_SYNTAX_ERROR;
2771 /* a base address isn't always necessary,
2772 * default to 0x0 (i.e. don't relocate) */
2773 if (CMD_ARGC >= 2) {
2775 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], addr);
2776 image->base_address = addr;
2777 image->base_address_set = 1;
2779 image->base_address_set = 0;
2781 image->start_address_set = 0;
2784 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[3], *min_address);
2785 if (CMD_ARGC == 5) {
2786 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[4], *max_address);
2787 /* use size (given) to find max (required) */
2788 *max_address += *min_address;
2791 if (*min_address > *max_address)
2792 return ERROR_COMMAND_SYNTAX_ERROR;
2797 COMMAND_HANDLER(handle_load_image_command)
2801 uint32_t image_size;
2802 uint32_t min_address = 0;
2803 uint32_t max_address = 0xffffffff;
2807 int retval = CALL_COMMAND_HANDLER(parse_load_image_command_CMD_ARGV,
2808 &image, &min_address, &max_address);
2809 if (ERROR_OK != retval)
2812 struct target *target = get_current_target(CMD_CTX);
2814 struct duration bench;
2815 duration_start(&bench);
2817 if (image_open(&image, CMD_ARGV[0], (CMD_ARGC >= 3) ? CMD_ARGV[2] : NULL) != ERROR_OK)
2822 for (i = 0; i < image.num_sections; i++) {
2823 buffer = malloc(image.sections[i].size);
2824 if (buffer == NULL) {
2825 command_print(CMD_CTX,
2826 "error allocating buffer for section (%d bytes)",
2827 (int)(image.sections[i].size));
2831 retval = image_read_section(&image, i, 0x0, image.sections[i].size, buffer, &buf_cnt);
2832 if (retval != ERROR_OK) {
2837 uint32_t offset = 0;
2838 uint32_t length = buf_cnt;
2840 /* DANGER!!! beware of unsigned comparision here!!! */
2842 if ((image.sections[i].base_address + buf_cnt >= min_address) &&
2843 (image.sections[i].base_address < max_address)) {
2845 if (image.sections[i].base_address < min_address) {
2846 /* clip addresses below */
2847 offset += min_address-image.sections[i].base_address;
2851 if (image.sections[i].base_address + buf_cnt > max_address)
2852 length -= (image.sections[i].base_address + buf_cnt)-max_address;
2854 retval = target_write_buffer(target,
2855 image.sections[i].base_address + offset, length, buffer + offset);
2856 if (retval != ERROR_OK) {
2860 image_size += length;
2861 command_print(CMD_CTX, "%u bytes written at address 0x%8.8" PRIx32 "",
2862 (unsigned int)length,
2863 image.sections[i].base_address + offset);
2869 if ((ERROR_OK == retval) && (duration_measure(&bench) == ERROR_OK)) {
2870 command_print(CMD_CTX, "downloaded %" PRIu32 " bytes "
2871 "in %fs (%0.3f KiB/s)", image_size,
2872 duration_elapsed(&bench), duration_kbps(&bench, image_size));
2875 image_close(&image);
2881 COMMAND_HANDLER(handle_dump_image_command)
2883 struct fileio fileio;
2885 int retval, retvaltemp;
2886 uint32_t address, size;
2887 struct duration bench;
2888 struct target *target = get_current_target(CMD_CTX);
2891 return ERROR_COMMAND_SYNTAX_ERROR;
2893 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], address);
2894 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[2], size);
2896 uint32_t buf_size = (size > 4096) ? 4096 : size;
2897 buffer = malloc(buf_size);
2901 retval = fileio_open(&fileio, CMD_ARGV[0], FILEIO_WRITE, FILEIO_BINARY);
2902 if (retval != ERROR_OK) {
2907 duration_start(&bench);
2910 size_t size_written;
2911 uint32_t this_run_size = (size > buf_size) ? buf_size : size;
2912 retval = target_read_buffer(target, address, this_run_size, buffer);
2913 if (retval != ERROR_OK)
2916 retval = fileio_write(&fileio, this_run_size, buffer, &size_written);
2917 if (retval != ERROR_OK)
2920 size -= this_run_size;
2921 address += this_run_size;
2926 if ((ERROR_OK == retval) && (duration_measure(&bench) == ERROR_OK)) {
2928 retval = fileio_size(&fileio, &filesize);
2929 if (retval != ERROR_OK)
2931 command_print(CMD_CTX,
2932 "dumped %ld bytes in %fs (%0.3f KiB/s)", (long)filesize,
2933 duration_elapsed(&bench), duration_kbps(&bench, filesize));
2936 retvaltemp = fileio_close(&fileio);
2937 if (retvaltemp != ERROR_OK)
2943 static COMMAND_HELPER(handle_verify_image_command_internal, int verify)
2947 uint32_t image_size;
2950 uint32_t checksum = 0;
2951 uint32_t mem_checksum = 0;
2955 struct target *target = get_current_target(CMD_CTX);
2958 return ERROR_COMMAND_SYNTAX_ERROR;
2961 LOG_ERROR("no target selected");
2965 struct duration bench;
2966 duration_start(&bench);
2968 if (CMD_ARGC >= 2) {
2970 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], addr);
2971 image.base_address = addr;
2972 image.base_address_set = 1;
2974 image.base_address_set = 0;
2975 image.base_address = 0x0;
2978 image.start_address_set = 0;
2980 retval = image_open(&image, CMD_ARGV[0], (CMD_ARGC == 3) ? CMD_ARGV[2] : NULL);
2981 if (retval != ERROR_OK)
2987 for (i = 0; i < image.num_sections; i++) {
2988 buffer = malloc(image.sections[i].size);
2989 if (buffer == NULL) {
2990 command_print(CMD_CTX,
2991 "error allocating buffer for section (%d bytes)",
2992 (int)(image.sections[i].size));
2995 retval = image_read_section(&image, i, 0x0, image.sections[i].size, buffer, &buf_cnt);
2996 if (retval != ERROR_OK) {
3002 /* calculate checksum of image */
3003 retval = image_calculate_checksum(buffer, buf_cnt, &checksum);
3004 if (retval != ERROR_OK) {
3009 retval = target_checksum_memory(target, image.sections[i].base_address, buf_cnt, &mem_checksum);
3010 if (retval != ERROR_OK) {
3015 if (checksum != mem_checksum) {
3016 /* failed crc checksum, fall back to a binary compare */
3020 LOG_ERROR("checksum mismatch - attempting binary compare");
3022 data = (uint8_t *)malloc(buf_cnt);
3024 /* Can we use 32bit word accesses? */
3026 int count = buf_cnt;
3027 if ((count % 4) == 0) {
3031 retval = target_read_memory(target, image.sections[i].base_address, size, count, data);
3032 if (retval == ERROR_OK) {
3034 for (t = 0; t < buf_cnt; t++) {
3035 if (data[t] != buffer[t]) {
3036 command_print(CMD_CTX,
3037 "diff %d address 0x%08x. Was 0x%02x instead of 0x%02x",
3039 (unsigned)(t + image.sections[i].base_address),
3042 if (diffs++ >= 127) {
3043 command_print(CMD_CTX, "More than 128 errors, the rest are not printed.");
3055 command_print(CMD_CTX, "address 0x%08" PRIx32 " length 0x%08zx",
3056 image.sections[i].base_address,
3061 image_size += buf_cnt;
3064 command_print(CMD_CTX, "No more differences found.");
3067 retval = ERROR_FAIL;
3068 if ((ERROR_OK == retval) && (duration_measure(&bench) == ERROR_OK)) {
3069 command_print(CMD_CTX, "verified %" PRIu32 " bytes "
3070 "in %fs (%0.3f KiB/s)", image_size,
3071 duration_elapsed(&bench), duration_kbps(&bench, image_size));
3074 image_close(&image);
3079 COMMAND_HANDLER(handle_verify_image_command)
3081 return CALL_COMMAND_HANDLER(handle_verify_image_command_internal, 1);
3084 COMMAND_HANDLER(handle_test_image_command)
3086 return CALL_COMMAND_HANDLER(handle_verify_image_command_internal, 0);
3089 static int handle_bp_command_list(struct command_context *cmd_ctx)
3091 struct target *target = get_current_target(cmd_ctx);
3092 struct breakpoint *breakpoint = target->breakpoints;
3093 while (breakpoint) {
3094 if (breakpoint->type == BKPT_SOFT) {
3095 char *buf = buf_to_str(breakpoint->orig_instr,
3096 breakpoint->length, 16);
3097 command_print(cmd_ctx, "IVA breakpoint: 0x%8.8" PRIx32 ", 0x%x, %i, 0x%s",
3098 breakpoint->address,
3100 breakpoint->set, buf);
3103 if ((breakpoint->address == 0) && (breakpoint->asid != 0))
3104 command_print(cmd_ctx, "Context breakpoint: 0x%8.8" PRIx32 ", 0x%x, %i",
3106 breakpoint->length, breakpoint->set);
3107 else if ((breakpoint->address != 0) && (breakpoint->asid != 0)) {
3108 command_print(cmd_ctx, "Hybrid breakpoint(IVA): 0x%8.8" PRIx32 ", 0x%x, %i",
3109 breakpoint->address,
3110 breakpoint->length, breakpoint->set);
3111 command_print(cmd_ctx, "\t|--->linked with ContextID: 0x%8.8" PRIx32,
3114 command_print(cmd_ctx, "Breakpoint(IVA): 0x%8.8" PRIx32 ", 0x%x, %i",
3115 breakpoint->address,
3116 breakpoint->length, breakpoint->set);
3119 breakpoint = breakpoint->next;
3124 static int handle_bp_command_set(struct command_context *cmd_ctx,
3125 uint32_t addr, uint32_t asid, uint32_t length, int hw)
3127 struct target *target = get_current_target(cmd_ctx);
3130 int retval = breakpoint_add(target, addr, length, hw);
3131 if (ERROR_OK == retval)
3132 command_print(cmd_ctx, "breakpoint set at 0x%8.8" PRIx32 "", addr);
3134 LOG_ERROR("Failure setting breakpoint, the same address(IVA) is already used");
3137 } else if (addr == 0) {
3138 int retval = context_breakpoint_add(target, asid, length, hw);
3139 if (ERROR_OK == retval)
3140 command_print(cmd_ctx, "Context breakpoint set at 0x%8.8" PRIx32 "", asid);
3142 LOG_ERROR("Failure setting breakpoint, the same address(CONTEXTID) is already used");
3146 int retval = hybrid_breakpoint_add(target, addr, asid, length, hw);
3147 if (ERROR_OK == retval)
3148 command_print(cmd_ctx, "Hybrid breakpoint set at 0x%8.8" PRIx32 "", asid);
3150 LOG_ERROR("Failure setting breakpoint, the same address is already used");
3157 COMMAND_HANDLER(handle_bp_command)
3166 return handle_bp_command_list(CMD_CTX);
3170 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], addr);
3171 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], length);
3172 return handle_bp_command_set(CMD_CTX, addr, asid, length, hw);
3175 if (strcmp(CMD_ARGV[2], "hw") == 0) {
3177 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], addr);
3179 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], length);
3182 return handle_bp_command_set(CMD_CTX, addr, asid, length, hw);
3183 } else if (strcmp(CMD_ARGV[2], "hw_ctx") == 0) {
3185 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], asid);
3186 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], length);
3188 return handle_bp_command_set(CMD_CTX, addr, asid, length, hw);
3193 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], addr);
3194 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], asid);
3195 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[2], length);
3196 return handle_bp_command_set(CMD_CTX, addr, asid, length, hw);
3199 return ERROR_COMMAND_SYNTAX_ERROR;
3203 COMMAND_HANDLER(handle_rbp_command)
3206 return ERROR_COMMAND_SYNTAX_ERROR;
3209 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], addr);
3211 struct target *target = get_current_target(CMD_CTX);
3212 breakpoint_remove(target, addr);
3217 COMMAND_HANDLER(handle_wp_command)
3219 struct target *target = get_current_target(CMD_CTX);
3221 if (CMD_ARGC == 0) {
3222 struct watchpoint *watchpoint = target->watchpoints;
3224 while (watchpoint) {
3225 command_print(CMD_CTX, "address: 0x%8.8" PRIx32
3226 ", len: 0x%8.8" PRIx32
3227 ", r/w/a: %i, value: 0x%8.8" PRIx32
3228 ", mask: 0x%8.8" PRIx32,
3229 watchpoint->address,
3231 (int)watchpoint->rw,
3234 watchpoint = watchpoint->next;
3239 enum watchpoint_rw type = WPT_ACCESS;
3241 uint32_t length = 0;
3242 uint32_t data_value = 0x0;
3243 uint32_t data_mask = 0xffffffff;
3247 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[4], data_mask);
3250 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[3], data_value);
3253 switch (CMD_ARGV[2][0]) {
3264 LOG_ERROR("invalid watchpoint mode ('%c')", CMD_ARGV[2][0]);
3265 return ERROR_COMMAND_SYNTAX_ERROR;
3269 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], length);
3270 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], addr);
3274 return ERROR_COMMAND_SYNTAX_ERROR;
3277 int retval = watchpoint_add(target, addr, length, type,
3278 data_value, data_mask);
3279 if (ERROR_OK != retval)
3280 LOG_ERROR("Failure setting watchpoints");
3285 COMMAND_HANDLER(handle_rwp_command)
3288 return ERROR_COMMAND_SYNTAX_ERROR;
3291 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], addr);
3293 struct target *target = get_current_target(CMD_CTX);
3294 watchpoint_remove(target, addr);
3300 * Translate a virtual address to a physical address.
3302 * The low-level target implementation must have logged a detailed error
3303 * which is forwarded to telnet/GDB session.
3305 COMMAND_HANDLER(handle_virt2phys_command)
3308 return ERROR_COMMAND_SYNTAX_ERROR;
3311 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], va);
3314 struct target *target = get_current_target(CMD_CTX);
3315 int retval = target->type->virt2phys(target, va, &pa);
3316 if (retval == ERROR_OK)
3317 command_print(CMD_CTX, "Physical address 0x%08" PRIx32 "", pa);
3322 static void writeData(FILE *f, const void *data, size_t len)
3324 size_t written = fwrite(data, 1, len, f);
3326 LOG_ERROR("failed to write %zu bytes: %s", len, strerror(errno));
3329 static void writeLong(FILE *f, int l)
3332 for (i = 0; i < 4; i++) {
3333 char c = (l >> (i*8))&0xff;
3334 writeData(f, &c, 1);
3339 static void writeString(FILE *f, char *s)
3341 writeData(f, s, strlen(s));
3344 /* Dump a gmon.out histogram file. */
3345 static void writeGmon(uint32_t *samples, uint32_t sampleNum, const char *filename)
3348 FILE *f = fopen(filename, "w");
3351 writeString(f, "gmon");
3352 writeLong(f, 0x00000001); /* Version */
3353 writeLong(f, 0); /* padding */
3354 writeLong(f, 0); /* padding */
3355 writeLong(f, 0); /* padding */
3357 uint8_t zero = 0; /* GMON_TAG_TIME_HIST */
3358 writeData(f, &zero, 1);
3360 /* figure out bucket size */
3361 uint32_t min = samples[0];
3362 uint32_t max = samples[0];
3363 for (i = 0; i < sampleNum; i++) {
3364 if (min > samples[i])
3366 if (max < samples[i])
3370 int addressSpace = (max - min + 1);
3371 assert(addressSpace >= 2);
3373 static const uint32_t maxBuckets = 16 * 1024; /* maximum buckets. */
3374 uint32_t length = addressSpace;
3375 if (length > maxBuckets)
3376 length = maxBuckets;
3377 int *buckets = malloc(sizeof(int)*length);
3378 if (buckets == NULL) {
3382 memset(buckets, 0, sizeof(int) * length);
3383 for (i = 0; i < sampleNum; i++) {
3384 uint32_t address = samples[i];
3385 long long a = address - min;
3386 long long b = length - 1;
3387 long long c = addressSpace - 1;
3388 int index_t = (a * b) / c; /* danger!!!! int32 overflows */
3392 /* append binary memory gmon.out &profile_hist_hdr ((char*)&profile_hist_hdr + sizeof(struct gmon_hist_hdr)) */
3393 writeLong(f, min); /* low_pc */
3394 writeLong(f, max); /* high_pc */
3395 writeLong(f, length); /* # of samples */
3396 writeLong(f, 100); /* KLUDGE! We lie, ca. 100Hz best case. */
3397 writeString(f, "seconds");
3398 for (i = 0; i < (15-strlen("seconds")); i++)
3399 writeData(f, &zero, 1);
3400 writeString(f, "s");
3402 /*append binary memory gmon.out profile_hist_data (profile_hist_data + profile_hist_hdr.hist_size) */
3404 char *data = malloc(2 * length);
3406 for (i = 0; i < length; i++) {
3411 data[i * 2] = val&0xff;
3412 data[i * 2 + 1] = (val >> 8) & 0xff;
3415 writeData(f, data, length * 2);
3423 /* profiling samples the CPU PC as quickly as OpenOCD is able,
3424 * which will be used as a random sampling of PC */
3425 COMMAND_HANDLER(handle_profile_command)
3427 struct target *target = get_current_target(CMD_CTX);
3428 struct timeval timeout, now;
3430 gettimeofday(&timeout, NULL);
3432 return ERROR_COMMAND_SYNTAX_ERROR;
3434 COMMAND_PARSE_NUMBER(uint, CMD_ARGV[0], offset);
3436 timeval_add_time(&timeout, offset, 0);
3439 * @todo: Some cores let us sample the PC without the
3440 * annoying halt/resume step; for example, ARMv7 PCSR.
3441 * Provide a way to use that more efficient mechanism.
3444 command_print(CMD_CTX, "Starting profiling. Halting and resuming the target as often as we can...");
3446 static const int maxSample = 10000;
3447 uint32_t *samples = malloc(sizeof(uint32_t)*maxSample);
3448 if (samples == NULL)
3452 /* hopefully it is safe to cache! We want to stop/restart as quickly as possible. */
3453 struct reg *reg = register_get_by_name(target->reg_cache, "pc", 1);
3455 int retval = ERROR_OK;
3457 target_poll(target);
3458 if (target->state == TARGET_HALTED) {
3459 uint32_t t = *((uint32_t *)reg->value);
3460 samples[numSamples++] = t;
3461 /* current pc, addr = 0, do not handle breakpoints, not debugging */
3462 retval = target_resume(target, 1, 0, 0, 0);
3463 target_poll(target);
3464 alive_sleep(10); /* sleep 10ms, i.e. <100 samples/second. */
3465 } else if (target->state == TARGET_RUNNING) {
3466 /* We want to quickly sample the PC. */
3467 retval = target_halt(target);
3468 if (retval != ERROR_OK) {
3473 command_print(CMD_CTX, "Target not halted or running");
3477 if (retval != ERROR_OK)
3480 gettimeofday(&now, NULL);
3481 if ((numSamples >= maxSample) || ((now.tv_sec >= timeout.tv_sec)
3482 && (now.tv_usec >= timeout.tv_usec))) {
3483 command_print(CMD_CTX, "Profiling completed. %d samples.", numSamples);
3484 retval = target_poll(target);
3485 if (retval != ERROR_OK) {
3489 if (target->state == TARGET_HALTED) {
3490 /* current pc, addr = 0, do not handle
3491 * breakpoints, not debugging */
3492 target_resume(target, 1, 0, 0, 0);
3494 retval = target_poll(target);
3495 if (retval != ERROR_OK) {
3499 writeGmon(samples, numSamples, CMD_ARGV[1]);
3500 command_print(CMD_CTX, "Wrote %s", CMD_ARGV[1]);
3509 static int new_int_array_element(Jim_Interp *interp, const char *varname, int idx, uint32_t val)
3512 Jim_Obj *nameObjPtr, *valObjPtr;
3515 namebuf = alloc_printf("%s(%d)", varname, idx);
3519 nameObjPtr = Jim_NewStringObj(interp, namebuf, -1);
3520 valObjPtr = Jim_NewIntObj(interp, val);
3521 if (!nameObjPtr || !valObjPtr) {
3526 Jim_IncrRefCount(nameObjPtr);
3527 Jim_IncrRefCount(valObjPtr);
3528 result = Jim_SetVariable(interp, nameObjPtr, valObjPtr);
3529 Jim_DecrRefCount(interp, nameObjPtr);
3530 Jim_DecrRefCount(interp, valObjPtr);
3532 /* printf("%s(%d) <= 0%08x\n", varname, idx, val); */
3536 static int jim_mem2array(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
3538 struct command_context *context;
3539 struct target *target;
3541 context = current_command_context(interp);
3542 assert(context != NULL);
3544 target = get_current_target(context);
3545 if (target == NULL) {
3546 LOG_ERROR("mem2array: no current target");
3550 return target_mem2array(interp, target, argc - 1, argv + 1);
3553 static int target_mem2array(Jim_Interp *interp, struct target *target, int argc, Jim_Obj *const *argv)
3561 const char *varname;
3565 /* argv[1] = name of array to receive the data
3566 * argv[2] = desired width
3567 * argv[3] = memory address
3568 * argv[4] = count of times to read
3571 Jim_WrongNumArgs(interp, 1, argv, "varname width addr nelems");
3574 varname = Jim_GetString(argv[0], &len);
3575 /* given "foo" get space for worse case "foo(%d)" .. add 20 */
3577 e = Jim_GetLong(interp, argv[1], &l);
3582 e = Jim_GetLong(interp, argv[2], &l);
3586 e = Jim_GetLong(interp, argv[3], &l);
3601 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3602 Jim_AppendStrings(interp, Jim_GetResult(interp), "Invalid width param, must be 8/16/32", NULL);
3606 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3607 Jim_AppendStrings(interp, Jim_GetResult(interp), "mem2array: zero width read?", NULL);
3610 if ((addr + (len * width)) < addr) {
3611 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3612 Jim_AppendStrings(interp, Jim_GetResult(interp), "mem2array: addr + len - wraps to zero?", NULL);
3615 /* absurd transfer size? */
3617 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3618 Jim_AppendStrings(interp, Jim_GetResult(interp), "mem2array: absurd > 64K item request", NULL);
3623 ((width == 2) && ((addr & 1) == 0)) ||
3624 ((width == 4) && ((addr & 3) == 0))) {
3628 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3629 sprintf(buf, "mem2array address: 0x%08" PRIx32 " is not aligned for %" PRId32 " byte reads",
3632 Jim_AppendStrings(interp, Jim_GetResult(interp), buf , NULL);
3641 size_t buffersize = 4096;
3642 uint8_t *buffer = malloc(buffersize);
3649 /* Slurp... in buffer size chunks */
3651 count = len; /* in objects.. */
3652 if (count > (buffersize / width))
3653 count = (buffersize / width);
3655 retval = target_read_memory(target, addr, width, count, buffer);
3656 if (retval != ERROR_OK) {
3658 LOG_ERROR("mem2array: Read @ 0x%08x, w=%d, cnt=%d, failed",
3662 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3663 Jim_AppendStrings(interp, Jim_GetResult(interp), "mem2array: cannot read memory", NULL);
3667 v = 0; /* shut up gcc */
3668 for (i = 0; i < count ; i++, n++) {
3671 v = target_buffer_get_u32(target, &buffer[i*width]);
3674 v = target_buffer_get_u16(target, &buffer[i*width]);
3677 v = buffer[i] & 0x0ff;
3680 new_int_array_element(interp, varname, n, v);
3688 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3693 static int get_int_array_element(Jim_Interp *interp, const char *varname, int idx, uint32_t *val)
3696 Jim_Obj *nameObjPtr, *valObjPtr;
3700 namebuf = alloc_printf("%s(%d)", varname, idx);
3704 nameObjPtr = Jim_NewStringObj(interp, namebuf, -1);
3710 Jim_IncrRefCount(nameObjPtr);
3711 valObjPtr = Jim_GetVariable(interp, nameObjPtr, JIM_ERRMSG);
3712 Jim_DecrRefCount(interp, nameObjPtr);
3714 if (valObjPtr == NULL)
3717 result = Jim_GetLong(interp, valObjPtr, &l);
3718 /* printf("%s(%d) => 0%08x\n", varname, idx, val); */
3723 static int jim_array2mem(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
3725 struct command_context *context;
3726 struct target *target;
3728 context = current_command_context(interp);
3729 assert(context != NULL);
3731 target = get_current_target(context);
3732 if (target == NULL) {
3733 LOG_ERROR("array2mem: no current target");
3737 return target_array2mem(interp, target, argc-1, argv + 1);
3740 static int target_array2mem(Jim_Interp *interp, struct target *target,
3741 int argc, Jim_Obj *const *argv)
3749 const char *varname;
3753 /* argv[1] = name of array to get the data
3754 * argv[2] = desired width
3755 * argv[3] = memory address
3756 * argv[4] = count to write
3759 Jim_WrongNumArgs(interp, 0, argv, "varname width addr nelems");
3762 varname = Jim_GetString(argv[0], &len);
3763 /* given "foo" get space for worse case "foo(%d)" .. add 20 */
3765 e = Jim_GetLong(interp, argv[1], &l);
3770 e = Jim_GetLong(interp, argv[2], &l);
3774 e = Jim_GetLong(interp, argv[3], &l);
3789 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3790 Jim_AppendStrings(interp, Jim_GetResult(interp),
3791 "Invalid width param, must be 8/16/32", NULL);
3795 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3796 Jim_AppendStrings(interp, Jim_GetResult(interp),
3797 "array2mem: zero width read?", NULL);
3800 if ((addr + (len * width)) < addr) {
3801 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3802 Jim_AppendStrings(interp, Jim_GetResult(interp),
3803 "array2mem: addr + len - wraps to zero?", NULL);
3806 /* absurd transfer size? */
3808 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3809 Jim_AppendStrings(interp, Jim_GetResult(interp),
3810 "array2mem: absurd > 64K item request", NULL);
3815 ((width == 2) && ((addr & 1) == 0)) ||
3816 ((width == 4) && ((addr & 3) == 0))) {
3820 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3821 sprintf(buf, "array2mem address: 0x%08x is not aligned for %d byte reads",
3824 Jim_AppendStrings(interp, Jim_GetResult(interp), buf , NULL);
3835 size_t buffersize = 4096;
3836 uint8_t *buffer = malloc(buffersize);
3841 /* Slurp... in buffer size chunks */
3843 count = len; /* in objects.. */
3844 if (count > (buffersize / width))
3845 count = (buffersize / width);
3847 v = 0; /* shut up gcc */
3848 for (i = 0; i < count; i++, n++) {
3849 get_int_array_element(interp, varname, n, &v);
3852 target_buffer_set_u32(target, &buffer[i * width], v);
3855 target_buffer_set_u16(target, &buffer[i * width], v);
3858 buffer[i] = v & 0x0ff;
3864 retval = target_write_memory(target, addr, width, count, buffer);
3865 if (retval != ERROR_OK) {
3867 LOG_ERROR("array2mem: Write @ 0x%08x, w=%d, cnt=%d, failed",
3871 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3872 Jim_AppendStrings(interp, Jim_GetResult(interp), "array2mem: cannot read memory", NULL);
3880 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3885 /* FIX? should we propagate errors here rather than printing them
3888 void target_handle_event(struct target *target, enum target_event e)
3890 struct target_event_action *teap;
3892 for (teap = target->event_action; teap != NULL; teap = teap->next) {
3893 if (teap->event == e) {
3894 LOG_DEBUG("target: (%d) %s (%s) event: %d (%s) action: %s",
3895 target->target_number,
3896 target_name(target),
3897 target_type_name(target),
3899 Jim_Nvp_value2name_simple(nvp_target_event, e)->name,
3900 Jim_GetString(teap->body, NULL));
3901 if (Jim_EvalObj(teap->interp, teap->body) != JIM_OK) {
3902 Jim_MakeErrorMessage(teap->interp);
3903 command_print(NULL, "%s\n", Jim_GetString(Jim_GetResult(teap->interp), NULL));
3910 * Returns true only if the target has a handler for the specified event.
3912 bool target_has_event_action(struct target *target, enum target_event event)
3914 struct target_event_action *teap;
3916 for (teap = target->event_action; teap != NULL; teap = teap->next) {
3917 if (teap->event == event)
3923 enum target_cfg_param {
3926 TCFG_WORK_AREA_VIRT,
3927 TCFG_WORK_AREA_PHYS,
3928 TCFG_WORK_AREA_SIZE,
3929 TCFG_WORK_AREA_BACKUP,
3933 TCFG_CHAIN_POSITION,
3938 static Jim_Nvp nvp_config_opts[] = {
3939 { .name = "-type", .value = TCFG_TYPE },
3940 { .name = "-event", .value = TCFG_EVENT },
3941 { .name = "-work-area-virt", .value = TCFG_WORK_AREA_VIRT },
3942 { .name = "-work-area-phys", .value = TCFG_WORK_AREA_PHYS },
3943 { .name = "-work-area-size", .value = TCFG_WORK_AREA_SIZE },
3944 { .name = "-work-area-backup", .value = TCFG_WORK_AREA_BACKUP },
3945 { .name = "-endian" , .value = TCFG_ENDIAN },
3946 { .name = "-variant", .value = TCFG_VARIANT },
3947 { .name = "-coreid", .value = TCFG_COREID },
3948 { .name = "-chain-position", .value = TCFG_CHAIN_POSITION },
3949 { .name = "-dbgbase", .value = TCFG_DBGBASE },
3950 { .name = "-rtos", .value = TCFG_RTOS },
3951 { .name = NULL, .value = -1 }
3954 static int target_configure(Jim_GetOptInfo *goi, struct target *target)
3962 /* parse config or cget options ... */
3963 while (goi->argc > 0) {
3964 Jim_SetEmptyResult(goi->interp);
3965 /* Jim_GetOpt_Debug(goi); */
3967 if (target->type->target_jim_configure) {
3968 /* target defines a configure function */
3969 /* target gets first dibs on parameters */
3970 e = (*(target->type->target_jim_configure))(target, goi);
3979 /* otherwise we 'continue' below */
3981 e = Jim_GetOpt_Nvp(goi, nvp_config_opts, &n);
3983 Jim_GetOpt_NvpUnknown(goi, nvp_config_opts, 0);
3989 if (goi->isconfigure) {
3990 Jim_SetResultFormatted(goi->interp,
3991 "not settable: %s", n->name);
3995 if (goi->argc != 0) {
3996 Jim_WrongNumArgs(goi->interp,
3997 goi->argc, goi->argv,
4002 Jim_SetResultString(goi->interp,
4003 target_type_name(target), -1);
4007 if (goi->argc == 0) {
4008 Jim_WrongNumArgs(goi->interp, goi->argc, goi->argv, "-event ?event-name? ...");
4012 e = Jim_GetOpt_Nvp(goi, nvp_target_event, &n);
4014 Jim_GetOpt_NvpUnknown(goi, nvp_target_event, 1);
4018 if (goi->isconfigure) {
4019 if (goi->argc != 1) {
4020 Jim_WrongNumArgs(goi->interp, goi->argc, goi->argv, "-event ?event-name? ?EVENT-BODY?");
4024 if (goi->argc != 0) {
4025 Jim_WrongNumArgs(goi->interp, goi->argc, goi->argv, "-event ?event-name?");
4031 struct target_event_action *teap;
4033 teap = target->event_action;
4034 /* replace existing? */
4036 if (teap->event == (enum target_event)n->value)
4041 if (goi->isconfigure) {
4042 bool replace = true;
4045 teap = calloc(1, sizeof(*teap));
4048 teap->event = n->value;
4049 teap->interp = goi->interp;
4050 Jim_GetOpt_Obj(goi, &o);
4052 Jim_DecrRefCount(teap->interp, teap->body);
4053 teap->body = Jim_DuplicateObj(goi->interp, o);
4056 * Tcl/TK - "tk events" have a nice feature.
4057 * See the "BIND" command.
4058 * We should support that here.
4059 * You can specify %X and %Y in the event code.
4060 * The idea is: %T - target name.
4061 * The idea is: %N - target number
4062 * The idea is: %E - event name.
4064 Jim_IncrRefCount(teap->body);
4067 /* add to head of event list */
4068 teap->next = target->event_action;
4069 target->event_action = teap;
4071 Jim_SetEmptyResult(goi->interp);
4075 Jim_SetEmptyResult(goi->interp);
4077 Jim_SetResult(goi->interp, Jim_DuplicateObj(goi->interp, teap->body));
4083 case TCFG_WORK_AREA_VIRT:
4084 if (goi->isconfigure) {
4085 target_free_all_working_areas(target);
4086 e = Jim_GetOpt_Wide(goi, &w);
4089 target->working_area_virt = w;
4090 target->working_area_virt_spec = true;
4095 Jim_SetResult(goi->interp, Jim_NewIntObj(goi->interp, target->working_area_virt));
4099 case TCFG_WORK_AREA_PHYS:
4100 if (goi->isconfigure) {
4101 target_free_all_working_areas(target);
4102 e = Jim_GetOpt_Wide(goi, &w);
4105 target->working_area_phys = w;
4106 target->working_area_phys_spec = true;
4111 Jim_SetResult(goi->interp, Jim_NewIntObj(goi->interp, target->working_area_phys));
4115 case TCFG_WORK_AREA_SIZE:
4116 if (goi->isconfigure) {
4117 target_free_all_working_areas(target);
4118 e = Jim_GetOpt_Wide(goi, &w);
4121 target->working_area_size = w;
4126 Jim_SetResult(goi->interp, Jim_NewIntObj(goi->interp, target->working_area_size));
4130 case TCFG_WORK_AREA_BACKUP:
4131 if (goi->isconfigure) {
4132 target_free_all_working_areas(target);
4133 e = Jim_GetOpt_Wide(goi, &w);
4136 /* make this exactly 1 or 0 */
4137 target->backup_working_area = (!!w);
4142 Jim_SetResult(goi->interp, Jim_NewIntObj(goi->interp, target->backup_working_area));
4143 /* loop for more e*/
4148 if (goi->isconfigure) {
4149 e = Jim_GetOpt_Nvp(goi, nvp_target_endian, &n);
4151 Jim_GetOpt_NvpUnknown(goi, nvp_target_endian, 1);
4154 target->endianness = n->value;
4159 n = Jim_Nvp_value2name_simple(nvp_target_endian, target->endianness);
4160 if (n->name == NULL) {
4161 target->endianness = TARGET_LITTLE_ENDIAN;
4162 n = Jim_Nvp_value2name_simple(nvp_target_endian, target->endianness);
4164 Jim_SetResultString(goi->interp, n->name, -1);
4169 if (goi->isconfigure) {
4170 if (goi->argc < 1) {
4171 Jim_SetResultFormatted(goi->interp,
4176 if (target->variant)
4177 free((void *)(target->variant));
4178 e = Jim_GetOpt_String(goi, &cp, NULL);
4181 target->variant = strdup(cp);
4186 Jim_SetResultString(goi->interp, target->variant, -1);
4191 if (goi->isconfigure) {
4192 e = Jim_GetOpt_Wide(goi, &w);
4195 target->coreid = (int32_t)w;
4200 Jim_SetResult(goi->interp, Jim_NewIntObj(goi->interp, target->working_area_size));
4204 case TCFG_CHAIN_POSITION:
4205 if (goi->isconfigure) {
4207 struct jtag_tap *tap;
4208 target_free_all_working_areas(target);
4209 e = Jim_GetOpt_Obj(goi, &o_t);
4212 tap = jtag_tap_by_jim_obj(goi->interp, o_t);
4215 /* make this exactly 1 or 0 */
4221 Jim_SetResultString(goi->interp, target->tap->dotted_name, -1);
4222 /* loop for more e*/
4225 if (goi->isconfigure) {
4226 e = Jim_GetOpt_Wide(goi, &w);
4229 target->dbgbase = (uint32_t)w;
4230 target->dbgbase_set = true;
4235 Jim_SetResult(goi->interp, Jim_NewIntObj(goi->interp, target->dbgbase));
4242 int result = rtos_create(goi, target);
4243 if (result != JIM_OK)
4249 } /* while (goi->argc) */
4252 /* done - we return */
4256 static int jim_target_configure(Jim_Interp *interp, int argc, Jim_Obj * const *argv)
4260 Jim_GetOpt_Setup(&goi, interp, argc - 1, argv + 1);
4261 goi.isconfigure = !strcmp(Jim_GetString(argv[0], NULL), "configure");
4262 int need_args = 1 + goi.isconfigure;
4263 if (goi.argc < need_args) {
4264 Jim_WrongNumArgs(goi.interp, goi.argc, goi.argv,
4266 ? "missing: -option VALUE ..."
4267 : "missing: -option ...");
4270 struct target *target = Jim_CmdPrivData(goi.interp);
4271 return target_configure(&goi, target);
4274 static int jim_target_mw(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
4276 const char *cmd_name = Jim_GetString(argv[0], NULL);
4279 Jim_GetOpt_Setup(&goi, interp, argc - 1, argv + 1);
4281 if (goi.argc < 2 || goi.argc > 4) {
4282 Jim_SetResultFormatted(goi.interp,
4283 "usage: %s [phys] <address> <data> [<count>]", cmd_name);
4288 fn = target_write_memory_fast;
4291 if (strcmp(Jim_GetString(argv[1], NULL), "phys") == 0) {
4293 struct Jim_Obj *obj;
4294 e = Jim_GetOpt_Obj(&goi, &obj);
4298 fn = target_write_phys_memory;
4302 e = Jim_GetOpt_Wide(&goi, &a);
4307 e = Jim_GetOpt_Wide(&goi, &b);
4312 if (goi.argc == 1) {
4313 e = Jim_GetOpt_Wide(&goi, &c);
4318 /* all args must be consumed */
4322 struct target *target = Jim_CmdPrivData(goi.interp);
4324 if (strcasecmp(cmd_name, "mww") == 0)
4326 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)) {
4346 Jim_SetResultFormatted(goi.interp,
4347 "usage: %s [phys] <address> [<count>]", cmd_name);
4351 int (*fn)(struct target *target,
4352 uint32_t address, uint32_t size, uint32_t count, uint8_t *buffer);
4353 fn = target_read_memory;
4356 if (strcmp(Jim_GetString(argv[1], NULL), "phys") == 0) {
4358 struct Jim_Obj *obj;
4359 e = Jim_GetOpt_Obj(&goi, &obj);
4363 fn = target_read_phys_memory;
4367 e = Jim_GetOpt_Wide(&goi, &a);
4371 if (goi.argc == 1) {
4372 e = Jim_GetOpt_Wide(&goi, &c);
4378 /* all args must be consumed */
4382 jim_wide b = 1; /* shut up gcc */
4383 if (strcasecmp(cmd_name, "mdw") == 0)
4385 else if (strcasecmp(cmd_name, "mdh") == 0)
4387 else if (strcasecmp(cmd_name, "mdb") == 0)
4390 LOG_ERROR("command '%s' unknown: ", cmd_name);
4394 /* convert count to "bytes" */
4397 struct target *target = Jim_CmdPrivData(goi.interp);
4398 uint8_t target_buf[32];
4404 e = fn(target, a, b, y / b, target_buf);
4405 if (e != ERROR_OK) {
4407 snprintf(tmp, sizeof(tmp), "%08lx", (long)a);
4408 Jim_SetResultFormatted(interp, "error reading target @ 0x%s", tmp);
4412 command_print(NULL, "0x%08x ", (int)(a));
4415 for (x = 0; x < 16 && x < y; x += 4) {
4416 z = target_buffer_get_u32(target, &(target_buf[x]));
4417 command_print(NULL, "%08x ", (int)(z));
4419 for (; (x < 16) ; x += 4)
4420 command_print(NULL, " ");
4423 for (x = 0; x < 16 && x < y; x += 2) {
4424 z = target_buffer_get_u16(target, &(target_buf[x]));
4425 command_print(NULL, "%04x ", (int)(z));
4427 for (; (x < 16) ; x += 2)
4428 command_print(NULL, " ");
4432 for (x = 0 ; (x < 16) && (x < y) ; x += 1) {
4433 z = target_buffer_get_u8(target, &(target_buf[x]));
4434 command_print(NULL, "%02x ", (int)(z));
4436 for (; (x < 16) ; x += 1)
4437 command_print(NULL, " ");
4440 /* ascii-ify the bytes */
4441 for (x = 0 ; x < y ; x++) {
4442 if ((target_buf[x] >= 0x20) &&
4443 (target_buf[x] <= 0x7e)) {
4447 target_buf[x] = '.';
4452 target_buf[x] = ' ';
4457 /* print - with a newline */
4458 command_print(NULL, "%s\n", target_buf);
4466 static int jim_target_mem2array(Jim_Interp *interp,
4467 int argc, Jim_Obj *const *argv)
4469 struct target *target = Jim_CmdPrivData(interp);
4470 return target_mem2array(interp, target, argc - 1, argv + 1);
4473 static int jim_target_array2mem(Jim_Interp *interp,
4474 int argc, Jim_Obj *const *argv)
4476 struct target *target = Jim_CmdPrivData(interp);
4477 return target_array2mem(interp, target, argc - 1, argv + 1);
4480 static int jim_target_tap_disabled(Jim_Interp *interp)
4482 Jim_SetResultFormatted(interp, "[TAP is disabled]");
4486 static int jim_target_examine(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
4489 Jim_WrongNumArgs(interp, 1, argv, "[no parameters]");
4492 struct target *target = Jim_CmdPrivData(interp);
4493 if (!target->tap->enabled)
4494 return jim_target_tap_disabled(interp);
4496 int e = target->type->examine(target);
4502 static int jim_target_halt_gdb(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
4505 Jim_WrongNumArgs(interp, 1, argv, "[no parameters]");
4508 struct target *target = Jim_CmdPrivData(interp);
4510 if (target_call_event_callbacks(target, TARGET_EVENT_GDB_HALT) != ERROR_OK)
4516 static int jim_target_poll(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
4519 Jim_WrongNumArgs(interp, 1, argv, "[no parameters]");
4522 struct target *target = Jim_CmdPrivData(interp);
4523 if (!target->tap->enabled)
4524 return jim_target_tap_disabled(interp);
4527 if (!(target_was_examined(target)))
4528 e = ERROR_TARGET_NOT_EXAMINED;
4530 e = target->type->poll(target);
4536 static int jim_target_reset(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
4539 Jim_GetOpt_Setup(&goi, interp, argc - 1, argv + 1);
4541 if (goi.argc != 2) {
4542 Jim_WrongNumArgs(interp, 0, argv,
4543 "([tT]|[fF]|assert|deassert) BOOL");
4548 int e = Jim_GetOpt_Nvp(&goi, nvp_assert, &n);
4550 Jim_GetOpt_NvpUnknown(&goi, nvp_assert, 1);
4553 /* the halt or not param */
4555 e = Jim_GetOpt_Wide(&goi, &a);
4559 struct target *target = Jim_CmdPrivData(goi.interp);
4560 if (!target->tap->enabled)
4561 return jim_target_tap_disabled(interp);
4562 if (!(target_was_examined(target))) {
4563 LOG_ERROR("Target not examined yet");
4564 return ERROR_TARGET_NOT_EXAMINED;
4566 if (!target->type->assert_reset || !target->type->deassert_reset) {
4567 Jim_SetResultFormatted(interp,
4568 "No target-specific reset for %s",
4569 target_name(target));
4572 /* determine if we should halt or not. */
4573 target->reset_halt = !!a;
4574 /* When this happens - all workareas are invalid. */
4575 target_free_all_working_areas_restore(target, 0);
4578 if (n->value == NVP_ASSERT)
4579 e = target->type->assert_reset(target);
4581 e = target->type->deassert_reset(target);
4582 return (e == ERROR_OK) ? JIM_OK : JIM_ERR;
4585 static int jim_target_halt(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
4588 Jim_WrongNumArgs(interp, 1, argv, "[no parameters]");
4591 struct target *target = Jim_CmdPrivData(interp);
4592 if (!target->tap->enabled)
4593 return jim_target_tap_disabled(interp);
4594 int e = target->type->halt(target);
4595 return (e == ERROR_OK) ? JIM_OK : JIM_ERR;
4598 static int jim_target_wait_state(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
4601 Jim_GetOpt_Setup(&goi, interp, argc - 1, argv + 1);
4603 /* params: <name> statename timeoutmsecs */
4604 if (goi.argc != 2) {
4605 const char *cmd_name = Jim_GetString(argv[0], NULL);
4606 Jim_SetResultFormatted(goi.interp,
4607 "%s <state_name> <timeout_in_msec>", cmd_name);
4612 int e = Jim_GetOpt_Nvp(&goi, nvp_target_state, &n);
4614 Jim_GetOpt_NvpUnknown(&goi, nvp_target_state, 1);
4618 e = Jim_GetOpt_Wide(&goi, &a);
4621 struct target *target = Jim_CmdPrivData(interp);
4622 if (!target->tap->enabled)
4623 return jim_target_tap_disabled(interp);
4625 e = target_wait_state(target, n->value, a);
4626 if (e != ERROR_OK) {
4627 Jim_Obj *eObj = Jim_NewIntObj(interp, e);
4628 Jim_SetResultFormatted(goi.interp,
4629 "target: %s wait %s fails (%#s) %s",
4630 target_name(target), n->name,
4631 eObj, target_strerror_safe(e));
4632 Jim_FreeNewObj(interp, eObj);
4637 /* List for human, Events defined for this target.
4638 * scripts/programs should use 'name cget -event NAME'
4640 static int jim_target_event_list(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
4642 struct command_context *cmd_ctx = current_command_context(interp);
4643 assert(cmd_ctx != NULL);
4645 struct target *target = Jim_CmdPrivData(interp);
4646 struct target_event_action *teap = target->event_action;
4647 command_print(cmd_ctx, "Event actions for target (%d) %s\n",
4648 target->target_number,
4649 target_name(target));
4650 command_print(cmd_ctx, "%-25s | Body", "Event");
4651 command_print(cmd_ctx, "------------------------- | "
4652 "----------------------------------------");
4654 Jim_Nvp *opt = Jim_Nvp_value2name_simple(nvp_target_event, teap->event);
4655 command_print(cmd_ctx, "%-25s | %s",
4656 opt->name, Jim_GetString(teap->body, NULL));
4659 command_print(cmd_ctx, "***END***");
4662 static int jim_target_current_state(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
4665 Jim_WrongNumArgs(interp, 1, argv, "[no parameters]");
4668 struct target *target = Jim_CmdPrivData(interp);
4669 Jim_SetResultString(interp, target_state_name(target), -1);
4672 static int jim_target_invoke_event(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
4675 Jim_GetOpt_Setup(&goi, interp, argc - 1, argv + 1);
4676 if (goi.argc != 1) {
4677 const char *cmd_name = Jim_GetString(argv[0], NULL);
4678 Jim_SetResultFormatted(goi.interp, "%s <eventname>", cmd_name);
4682 int e = Jim_GetOpt_Nvp(&goi, nvp_target_event, &n);
4684 Jim_GetOpt_NvpUnknown(&goi, nvp_target_event, 1);
4687 struct target *target = Jim_CmdPrivData(interp);
4688 target_handle_event(target, n->value);
4692 static const struct command_registration target_instance_command_handlers[] = {
4694 .name = "configure",
4695 .mode = COMMAND_CONFIG,
4696 .jim_handler = jim_target_configure,
4697 .help = "configure a new target for use",
4698 .usage = "[target_attribute ...]",
4702 .mode = COMMAND_ANY,
4703 .jim_handler = jim_target_configure,
4704 .help = "returns the specified target attribute",
4705 .usage = "target_attribute",
4709 .mode = COMMAND_EXEC,
4710 .jim_handler = jim_target_mw,
4711 .help = "Write 32-bit word(s) to target memory",
4712 .usage = "address data [count]",
4716 .mode = COMMAND_EXEC,
4717 .jim_handler = jim_target_mw,
4718 .help = "Write 16-bit half-word(s) to target memory",
4719 .usage = "address data [count]",
4723 .mode = COMMAND_EXEC,
4724 .jim_handler = jim_target_mw,
4725 .help = "Write byte(s) to target memory",
4726 .usage = "address data [count]",
4730 .mode = COMMAND_EXEC,
4731 .jim_handler = jim_target_md,
4732 .help = "Display target memory as 32-bit words",
4733 .usage = "address [count]",
4737 .mode = COMMAND_EXEC,
4738 .jim_handler = jim_target_md,
4739 .help = "Display target memory as 16-bit half-words",
4740 .usage = "address [count]",
4744 .mode = COMMAND_EXEC,
4745 .jim_handler = jim_target_md,
4746 .help = "Display target memory as 8-bit bytes",
4747 .usage = "address [count]",
4750 .name = "array2mem",
4751 .mode = COMMAND_EXEC,
4752 .jim_handler = jim_target_array2mem,
4753 .help = "Writes Tcl array of 8/16/32 bit numbers "
4755 .usage = "arrayname bitwidth address count",
4758 .name = "mem2array",
4759 .mode = COMMAND_EXEC,
4760 .jim_handler = jim_target_mem2array,
4761 .help = "Loads Tcl array of 8/16/32 bit numbers "
4762 "from target memory",
4763 .usage = "arrayname bitwidth address count",
4766 .name = "eventlist",
4767 .mode = COMMAND_EXEC,
4768 .jim_handler = jim_target_event_list,
4769 .help = "displays a table of events defined for this target",
4773 .mode = COMMAND_EXEC,
4774 .jim_handler = jim_target_current_state,
4775 .help = "displays the current state of this target",
4778 .name = "arp_examine",
4779 .mode = COMMAND_EXEC,
4780 .jim_handler = jim_target_examine,
4781 .help = "used internally for reset processing",
4784 .name = "arp_halt_gdb",
4785 .mode = COMMAND_EXEC,
4786 .jim_handler = jim_target_halt_gdb,
4787 .help = "used internally for reset processing to halt GDB",
4791 .mode = COMMAND_EXEC,
4792 .jim_handler = jim_target_poll,
4793 .help = "used internally for reset processing",
4796 .name = "arp_reset",
4797 .mode = COMMAND_EXEC,
4798 .jim_handler = jim_target_reset,
4799 .help = "used internally for reset processing",
4803 .mode = COMMAND_EXEC,
4804 .jim_handler = jim_target_halt,
4805 .help = "used internally for reset processing",
4808 .name = "arp_waitstate",
4809 .mode = COMMAND_EXEC,
4810 .jim_handler = jim_target_wait_state,
4811 .help = "used internally for reset processing",
4814 .name = "invoke-event",
4815 .mode = COMMAND_EXEC,
4816 .jim_handler = jim_target_invoke_event,
4817 .help = "invoke handler for specified event",
4818 .usage = "event_name",
4820 COMMAND_REGISTRATION_DONE
4823 static int target_create(Jim_GetOptInfo *goi)
4831 struct target *target;
4832 struct command_context *cmd_ctx;
4834 cmd_ctx = current_command_context(goi->interp);
4835 assert(cmd_ctx != NULL);
4837 if (goi->argc < 3) {
4838 Jim_WrongNumArgs(goi->interp, 1, goi->argv, "?name? ?type? ..options...");
4843 Jim_GetOpt_Obj(goi, &new_cmd);
4844 /* does this command exist? */
4845 cmd = Jim_GetCommand(goi->interp, new_cmd, JIM_ERRMSG);
4847 cp = Jim_GetString(new_cmd, NULL);
4848 Jim_SetResultFormatted(goi->interp, "Command/target: %s Exists", cp);
4853 e = Jim_GetOpt_String(goi, &cp2, NULL);
4857 /* now does target type exist */
4858 for (x = 0 ; target_types[x] ; x++) {
4859 if (0 == strcmp(cp, target_types[x]->name)) {
4864 if (target_types[x] == NULL) {
4865 Jim_SetResultFormatted(goi->interp, "Unknown target type %s, try one of ", cp);
4866 for (x = 0 ; target_types[x] ; x++) {
4867 if (target_types[x + 1]) {
4868 Jim_AppendStrings(goi->interp,
4869 Jim_GetResult(goi->interp),
4870 target_types[x]->name,
4873 Jim_AppendStrings(goi->interp,
4874 Jim_GetResult(goi->interp),
4876 target_types[x]->name, NULL);
4883 target = calloc(1, sizeof(struct target));
4884 /* set target number */
4885 target->target_number = new_target_number();
4887 /* allocate memory for each unique target type */
4888 target->type = (struct target_type *)calloc(1, sizeof(struct target_type));
4890 memcpy(target->type, target_types[x], sizeof(struct target_type));
4892 /* will be set by "-endian" */
4893 target->endianness = TARGET_ENDIAN_UNKNOWN;
4895 /* default to first core, override with -coreid */
4898 target->working_area = 0x0;
4899 target->working_area_size = 0x0;
4900 target->working_areas = NULL;
4901 target->backup_working_area = 0;
4903 target->state = TARGET_UNKNOWN;
4904 target->debug_reason = DBG_REASON_UNDEFINED;
4905 target->reg_cache = NULL;
4906 target->breakpoints = NULL;
4907 target->watchpoints = NULL;
4908 target->next = NULL;
4909 target->arch_info = NULL;
4911 target->display = 1;
4913 target->halt_issued = false;
4915 /* initialize trace information */
4916 target->trace_info = malloc(sizeof(struct trace));
4917 target->trace_info->num_trace_points = 0;
4918 target->trace_info->trace_points_size = 0;
4919 target->trace_info->trace_points = NULL;
4920 target->trace_info->trace_history_size = 0;
4921 target->trace_info->trace_history = NULL;
4922 target->trace_info->trace_history_pos = 0;
4923 target->trace_info->trace_history_overflowed = 0;
4925 target->dbgmsg = NULL;
4926 target->dbg_msg_enabled = 0;
4928 target->endianness = TARGET_ENDIAN_UNKNOWN;
4930 target->rtos = NULL;
4931 target->rtos_auto_detect = false;
4933 /* Do the rest as "configure" options */
4934 goi->isconfigure = 1;
4935 e = target_configure(goi, target);
4937 if (target->tap == NULL) {
4938 Jim_SetResultString(goi->interp, "-chain-position required when creating target", -1);
4948 if (target->endianness == TARGET_ENDIAN_UNKNOWN) {
4949 /* default endian to little if not specified */
4950 target->endianness = TARGET_LITTLE_ENDIAN;
4953 /* incase variant is not set */
4954 if (!target->variant)
4955 target->variant = strdup("");
4957 cp = Jim_GetString(new_cmd, NULL);
4958 target->cmd_name = strdup(cp);
4960 /* create the target specific commands */
4961 if (target->type->commands) {
4962 e = register_commands(cmd_ctx, NULL, target->type->commands);
4964 LOG_ERROR("unable to register '%s' commands", cp);
4966 if (target->type->target_create)
4967 (*(target->type->target_create))(target, goi->interp);
4969 /* append to end of list */
4971 struct target **tpp;
4972 tpp = &(all_targets);
4974 tpp = &((*tpp)->next);
4978 /* now - create the new target name command */
4979 const const struct command_registration target_subcommands[] = {
4981 .chain = target_instance_command_handlers,
4984 .chain = target->type->commands,
4986 COMMAND_REGISTRATION_DONE
4988 const const struct command_registration target_commands[] = {
4991 .mode = COMMAND_ANY,
4992 .help = "target command group",
4994 .chain = target_subcommands,
4996 COMMAND_REGISTRATION_DONE
4998 e = register_commands(cmd_ctx, NULL, target_commands);
5002 struct command *c = command_find_in_context(cmd_ctx, cp);
5004 command_set_handler_data(c, target);
5006 return (ERROR_OK == e) ? JIM_OK : JIM_ERR;
5009 static int jim_target_current(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
5012 Jim_WrongNumArgs(interp, 1, argv, "Too many parameters");
5015 struct command_context *cmd_ctx = current_command_context(interp);
5016 assert(cmd_ctx != NULL);
5018 Jim_SetResultString(interp, get_current_target(cmd_ctx)->cmd_name, -1);
5022 static int jim_target_types(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
5025 Jim_WrongNumArgs(interp, 1, argv, "Too many parameters");
5028 Jim_SetResult(interp, Jim_NewListObj(interp, NULL, 0));
5029 for (unsigned x = 0; NULL != target_types[x]; x++) {
5030 Jim_ListAppendElement(interp, Jim_GetResult(interp),
5031 Jim_NewStringObj(interp, target_types[x]->name, -1));
5036 static int jim_target_names(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
5039 Jim_WrongNumArgs(interp, 1, argv, "Too many parameters");
5042 Jim_SetResult(interp, Jim_NewListObj(interp, NULL, 0));
5043 struct target *target = all_targets;
5045 Jim_ListAppendElement(interp, Jim_GetResult(interp),
5046 Jim_NewStringObj(interp, target_name(target), -1));
5047 target = target->next;
5052 static int jim_target_smp(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
5055 const char *targetname;
5057 struct target *target = (struct target *) NULL;
5058 struct target_list *head, *curr, *new;
5059 curr = (struct target_list *) NULL;
5060 head = (struct target_list *) NULL;
5063 LOG_DEBUG("%d", argc);
5064 /* argv[1] = target to associate in smp
5065 * argv[2] = target to assoicate in smp
5069 for (i = 1; i < argc; i++) {
5071 targetname = Jim_GetString(argv[i], &len);
5072 target = get_target(targetname);
5073 LOG_DEBUG("%s ", targetname);
5075 new = malloc(sizeof(struct target_list));
5076 new->target = target;
5077 new->next = (struct target_list *)NULL;
5078 if (head == (struct target_list *)NULL) {
5087 /* now parse the list of cpu and put the target in smp mode*/
5090 while (curr != (struct target_list *)NULL) {
5091 target = curr->target;
5093 target->head = head;
5097 retval = rtos_smp_init(head->target);
5102 static int jim_target_create(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
5105 Jim_GetOpt_Setup(&goi, interp, argc - 1, argv + 1);
5107 Jim_WrongNumArgs(goi.interp, goi.argc, goi.argv,
5108 "<name> <target_type> [<target_options> ...]");
5111 return target_create(&goi);
5114 static int jim_target_number(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
5117 Jim_GetOpt_Setup(&goi, interp, argc - 1, argv + 1);
5119 /* It's OK to remove this mechanism sometime after August 2010 or so */
5120 LOG_WARNING("don't use numbers as target identifiers; use names");
5121 if (goi.argc != 1) {
5122 Jim_SetResultFormatted(goi.interp, "usage: target number <number>");
5126 int e = Jim_GetOpt_Wide(&goi, &w);
5130 struct target *target;
5131 for (target = all_targets; NULL != target; target = target->next) {
5132 if (target->target_number != w)
5135 Jim_SetResultString(goi.interp, target_name(target), -1);
5139 Jim_Obj *wObj = Jim_NewIntObj(goi.interp, w);
5140 Jim_SetResultFormatted(goi.interp,
5141 "Target: number %#s does not exist", wObj);
5142 Jim_FreeNewObj(interp, wObj);
5147 static int jim_target_count(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
5150 Jim_WrongNumArgs(interp, 1, argv, "<no parameters>");
5154 struct target *target = all_targets;
5155 while (NULL != target) {
5156 target = target->next;
5159 Jim_SetResult(interp, Jim_NewIntObj(interp, count));
5163 static const struct command_registration target_subcommand_handlers[] = {
5166 .mode = COMMAND_CONFIG,
5167 .handler = handle_target_init_command,
5168 .help = "initialize targets",
5172 /* REVISIT this should be COMMAND_CONFIG ... */
5173 .mode = COMMAND_ANY,
5174 .jim_handler = jim_target_create,
5175 .usage = "name type '-chain-position' name [options ...]",
5176 .help = "Creates and selects a new target",
5180 .mode = COMMAND_ANY,
5181 .jim_handler = jim_target_current,
5182 .help = "Returns the currently selected target",
5186 .mode = COMMAND_ANY,
5187 .jim_handler = jim_target_types,
5188 .help = "Returns the available target types as "
5189 "a list of strings",
5193 .mode = COMMAND_ANY,
5194 .jim_handler = jim_target_names,
5195 .help = "Returns the names of all targets as a list of strings",
5199 .mode = COMMAND_ANY,
5200 .jim_handler = jim_target_number,
5202 .help = "Returns the name of the numbered target "
5207 .mode = COMMAND_ANY,
5208 .jim_handler = jim_target_count,
5209 .help = "Returns the number of targets as an integer "
5214 .mode = COMMAND_ANY,
5215 .jim_handler = jim_target_smp,
5216 .usage = "targetname1 targetname2 ...",
5217 .help = "gather several target in a smp list"
5220 COMMAND_REGISTRATION_DONE
5230 static int fastload_num;
5231 static struct FastLoad *fastload;
5233 static void free_fastload(void)
5235 if (fastload != NULL) {
5237 for (i = 0; i < fastload_num; i++) {
5238 if (fastload[i].data)
5239 free(fastload[i].data);
5246 COMMAND_HANDLER(handle_fast_load_image_command)
5250 uint32_t image_size;
5251 uint32_t min_address = 0;
5252 uint32_t max_address = 0xffffffff;
5257 int retval = CALL_COMMAND_HANDLER(parse_load_image_command_CMD_ARGV,
5258 &image, &min_address, &max_address);
5259 if (ERROR_OK != retval)
5262 struct duration bench;
5263 duration_start(&bench);
5265 retval = image_open(&image, CMD_ARGV[0], (CMD_ARGC >= 3) ? CMD_ARGV[2] : NULL);
5266 if (retval != ERROR_OK)
5271 fastload_num = image.num_sections;
5272 fastload = (struct FastLoad *)malloc(sizeof(struct FastLoad)*image.num_sections);
5273 if (fastload == NULL) {
5274 command_print(CMD_CTX, "out of memory");
5275 image_close(&image);
5278 memset(fastload, 0, sizeof(struct FastLoad)*image.num_sections);
5279 for (i = 0; i < image.num_sections; i++) {
5280 buffer = malloc(image.sections[i].size);
5281 if (buffer == NULL) {
5282 command_print(CMD_CTX, "error allocating buffer for section (%d bytes)",
5283 (int)(image.sections[i].size));
5284 retval = ERROR_FAIL;
5288 retval = image_read_section(&image, i, 0x0, image.sections[i].size, buffer, &buf_cnt);
5289 if (retval != ERROR_OK) {
5294 uint32_t offset = 0;
5295 uint32_t length = buf_cnt;
5297 /* DANGER!!! beware of unsigned comparision here!!! */
5299 if ((image.sections[i].base_address + buf_cnt >= min_address) &&
5300 (image.sections[i].base_address < max_address)) {
5301 if (image.sections[i].base_address < min_address) {
5302 /* clip addresses below */
5303 offset += min_address-image.sections[i].base_address;
5307 if (image.sections[i].base_address + buf_cnt > max_address)
5308 length -= (image.sections[i].base_address + buf_cnt)-max_address;
5310 fastload[i].address = image.sections[i].base_address + offset;
5311 fastload[i].data = malloc(length);
5312 if (fastload[i].data == NULL) {
5314 command_print(CMD_CTX, "error allocating buffer for section (%d bytes)",
5316 retval = ERROR_FAIL;
5319 memcpy(fastload[i].data, buffer + offset, length);
5320 fastload[i].length = length;
5322 image_size += length;
5323 command_print(CMD_CTX, "%u bytes written at address 0x%8.8x",
5324 (unsigned int)length,
5325 ((unsigned int)(image.sections[i].base_address + offset)));
5331 if ((ERROR_OK == retval) && (duration_measure(&bench) == ERROR_OK)) {
5332 command_print(CMD_CTX, "Loaded %" PRIu32 " bytes "
5333 "in %fs (%0.3f KiB/s)", image_size,
5334 duration_elapsed(&bench), duration_kbps(&bench, image_size));
5336 command_print(CMD_CTX,
5337 "WARNING: image has not been loaded to target!"
5338 "You can issue a 'fast_load' to finish loading.");
5341 image_close(&image);
5343 if (retval != ERROR_OK)
5349 COMMAND_HANDLER(handle_fast_load_command)
5352 return ERROR_COMMAND_SYNTAX_ERROR;
5353 if (fastload == NULL) {
5354 LOG_ERROR("No image in memory");
5358 int ms = timeval_ms();
5360 int retval = ERROR_OK;
5361 for (i = 0; i < fastload_num; i++) {
5362 struct target *target = get_current_target(CMD_CTX);
5363 command_print(CMD_CTX, "Write to 0x%08x, length 0x%08x",
5364 (unsigned int)(fastload[i].address),
5365 (unsigned int)(fastload[i].length));
5366 retval = target_write_buffer(target, fastload[i].address, fastload[i].length, fastload[i].data);
5367 if (retval != ERROR_OK)
5369 size += fastload[i].length;
5371 if (retval == ERROR_OK) {
5372 int after = timeval_ms();
5373 command_print(CMD_CTX, "Loaded image %f kBytes/s", (float)(size/1024.0)/((float)(after-ms)/1000.0));
5378 static const struct command_registration target_command_handlers[] = {
5381 .handler = handle_targets_command,
5382 .mode = COMMAND_ANY,
5383 .help = "change current default target (one parameter) "
5384 "or prints table of all targets (no parameters)",
5385 .usage = "[target]",
5389 .mode = COMMAND_CONFIG,
5390 .help = "configure target",
5392 .chain = target_subcommand_handlers,
5394 COMMAND_REGISTRATION_DONE
5397 int target_register_commands(struct command_context *cmd_ctx)
5399 return register_commands(cmd_ctx, NULL, target_command_handlers);
5402 static bool target_reset_nag = true;
5404 bool get_target_reset_nag(void)
5406 return target_reset_nag;
5409 COMMAND_HANDLER(handle_target_reset_nag)
5411 return CALL_COMMAND_HANDLER(handle_command_parse_bool,
5412 &target_reset_nag, "Nag after each reset about options to improve "
5416 COMMAND_HANDLER(handle_ps_command)
5418 struct target *target = get_current_target(CMD_CTX);
5420 if (target->state != TARGET_HALTED) {
5421 LOG_INFO("target not halted !!");
5425 if ((target->rtos) && (target->rtos->type)
5426 && (target->rtos->type->ps_command)) {
5427 display = target->rtos->type->ps_command(target);
5428 command_print(CMD_CTX, "%s", display);
5433 return ERROR_TARGET_FAILURE;
5437 static const struct command_registration target_exec_command_handlers[] = {
5439 .name = "fast_load_image",
5440 .handler = handle_fast_load_image_command,
5441 .mode = COMMAND_ANY,
5442 .help = "Load image into server memory for later use by "
5443 "fast_load; primarily for profiling",
5444 .usage = "filename address ['bin'|'ihex'|'elf'|'s19'] "
5445 "[min_address [max_length]]",
5448 .name = "fast_load",
5449 .handler = handle_fast_load_command,
5450 .mode = COMMAND_EXEC,
5451 .help = "loads active fast load image to current target "
5452 "- mainly for profiling purposes",
5457 .handler = handle_profile_command,
5458 .mode = COMMAND_EXEC,
5459 .usage = "seconds filename",
5460 .help = "profiling samples the CPU PC",
5462 /** @todo don't register virt2phys() unless target supports it */
5464 .name = "virt2phys",
5465 .handler = handle_virt2phys_command,
5466 .mode = COMMAND_ANY,
5467 .help = "translate a virtual address into a physical address",
5468 .usage = "virtual_address",
5472 .handler = handle_reg_command,
5473 .mode = COMMAND_EXEC,
5474 .help = "display or set a register; with no arguments, "
5475 "displays all registers and their values",
5476 .usage = "[(register_name|register_number) [value]]",
5480 .handler = handle_poll_command,
5481 .mode = COMMAND_EXEC,
5482 .help = "poll target state; or reconfigure background polling",
5483 .usage = "['on'|'off']",
5486 .name = "wait_halt",
5487 .handler = handle_wait_halt_command,
5488 .mode = COMMAND_EXEC,
5489 .help = "wait up to the specified number of milliseconds "
5490 "(default 5) for a previously requested halt",
5491 .usage = "[milliseconds]",
5495 .handler = handle_halt_command,
5496 .mode = COMMAND_EXEC,
5497 .help = "request target to halt, then wait up to the specified"
5498 "number of milliseconds (default 5) for it to complete",
5499 .usage = "[milliseconds]",
5503 .handler = handle_resume_command,
5504 .mode = COMMAND_EXEC,
5505 .help = "resume target execution from current PC or address",
5506 .usage = "[address]",
5510 .handler = handle_reset_command,
5511 .mode = COMMAND_EXEC,
5512 .usage = "[run|halt|init]",
5513 .help = "Reset all targets into the specified mode."
5514 "Default reset mode is run, if not given.",
5517 .name = "soft_reset_halt",
5518 .handler = handle_soft_reset_halt_command,
5519 .mode = COMMAND_EXEC,
5521 .help = "halt the target and do a soft reset",
5525 .handler = handle_step_command,
5526 .mode = COMMAND_EXEC,
5527 .help = "step one instruction from current PC or address",
5528 .usage = "[address]",
5532 .handler = handle_md_command,
5533 .mode = COMMAND_EXEC,
5534 .help = "display memory words",
5535 .usage = "['phys'] address [count]",
5539 .handler = handle_md_command,
5540 .mode = COMMAND_EXEC,
5541 .help = "display memory half-words",
5542 .usage = "['phys'] address [count]",
5546 .handler = handle_md_command,
5547 .mode = COMMAND_EXEC,
5548 .help = "display memory bytes",
5549 .usage = "['phys'] address [count]",
5553 .handler = handle_mw_command,
5554 .mode = COMMAND_EXEC,
5555 .help = "write memory word",
5556 .usage = "['phys'] address value [count]",
5560 .handler = handle_mw_command,
5561 .mode = COMMAND_EXEC,
5562 .help = "write memory half-word",
5563 .usage = "['phys'] address value [count]",
5567 .handler = handle_mw_command,
5568 .mode = COMMAND_EXEC,
5569 .help = "write memory byte",
5570 .usage = "['phys'] address value [count]",
5574 .handler = handle_bp_command,
5575 .mode = COMMAND_EXEC,
5576 .help = "list or set hardware or software breakpoint",
5577 .usage = "<address> [<asid>]<length> ['hw'|'hw_ctx']",
5581 .handler = handle_rbp_command,
5582 .mode = COMMAND_EXEC,
5583 .help = "remove breakpoint",
5588 .handler = handle_wp_command,
5589 .mode = COMMAND_EXEC,
5590 .help = "list (no params) or create watchpoints",
5591 .usage = "[address length [('r'|'w'|'a') value [mask]]]",
5595 .handler = handle_rwp_command,
5596 .mode = COMMAND_EXEC,
5597 .help = "remove watchpoint",
5601 .name = "load_image",
5602 .handler = handle_load_image_command,
5603 .mode = COMMAND_EXEC,
5604 .usage = "filename address ['bin'|'ihex'|'elf'|'s19'] "
5605 "[min_address] [max_length]",
5608 .name = "dump_image",
5609 .handler = handle_dump_image_command,
5610 .mode = COMMAND_EXEC,
5611 .usage = "filename address size",
5614 .name = "verify_image",
5615 .handler = handle_verify_image_command,
5616 .mode = COMMAND_EXEC,
5617 .usage = "filename [offset [type]]",
5620 .name = "test_image",
5621 .handler = handle_test_image_command,
5622 .mode = COMMAND_EXEC,
5623 .usage = "filename [offset [type]]",
5626 .name = "mem2array",
5627 .mode = COMMAND_EXEC,
5628 .jim_handler = jim_mem2array,
5629 .help = "read 8/16/32 bit memory and return as a TCL array "
5630 "for script processing",
5631 .usage = "arrayname bitwidth address count",
5634 .name = "array2mem",
5635 .mode = COMMAND_EXEC,
5636 .jim_handler = jim_array2mem,
5637 .help = "convert a TCL array to memory locations "
5638 "and write the 8/16/32 bit values",
5639 .usage = "arrayname bitwidth address count",
5642 .name = "reset_nag",
5643 .handler = handle_target_reset_nag,
5644 .mode = COMMAND_ANY,
5645 .help = "Nag after each reset about options that could have been "
5646 "enabled to improve performance. ",
5647 .usage = "['enable'|'disable']",
5651 .handler = handle_ps_command,
5652 .mode = COMMAND_EXEC,
5653 .help = "list all tasks ",
5657 COMMAND_REGISTRATION_DONE
5659 static int target_register_user_commands(struct command_context *cmd_ctx)
5661 int retval = ERROR_OK;
5662 retval = target_request_register_commands(cmd_ctx);
5663 if (retval != ERROR_OK)
5666 retval = trace_register_commands(cmd_ctx);
5667 if (retval != ERROR_OK)
5671 return register_commands(cmd_ctx, NULL, target_exec_command_handlers);