STM32 ST-LINK target initial release
[openocd/andreasf.git] / src / target / target.c
blob6b034665c757540c799706bcc6e1d53d4a441694
1 /***************************************************************************
2 * Copyright (C) 2005 by Dominic Rath *
3 * Dominic.Rath@gmx.de *
4 * *
5 * Copyright (C) 2007-2010 Øyvind Harboe *
6 * oyvind.harboe@zylin.com *
7 * *
8 * Copyright (C) 2008, Duane Ellis *
9 * openocd@duaneeellis.com *
10 * *
11 * Copyright (C) 2008 by Spencer Oliver *
12 * spen@spen-soft.co.uk *
13 * *
14 * Copyright (C) 2008 by Rick Altherr *
15 * kc8apf@kc8apf.net> *
16 * *
17 * Copyright (C) 2011 by Broadcom Corporation *
18 * Evan Hunter - ehunter@broadcom.com *
19 * *
20 * Copyright (C) ST-Ericsson SA 2011 *
21 * michel.jaouen@stericsson.com : smp minimum support *
22 * *
23 * This program is free software; you can redistribute it and/or modify *
24 * it under the terms of the GNU General Public License as published by *
25 * the Free Software Foundation; either version 2 of the License, or *
26 * (at your option) any later version. *
27 * *
28 * This program is distributed in the hope that it will be useful, *
29 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
30 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
31 * GNU General Public License for more details. *
32 * *
33 * You should have received a copy of the GNU General Public License *
34 * along with this program; if not, write to the *
35 * Free Software Foundation, Inc., *
36 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
37 ***************************************************************************/
38 #ifdef HAVE_CONFIG_H
39 #include "config.h"
40 #endif
42 #include <helper/time_support.h>
43 #include <jtag/jtag.h>
44 #include <flash/nor/core.h>
46 #include "target.h"
47 #include "target_type.h"
48 #include "target_request.h"
49 #include "breakpoints.h"
50 #include "register.h"
51 #include "trace.h"
52 #include "image.h"
53 #include "rtos/rtos.h"
56 static int target_read_buffer_default(struct target *target, uint32_t address,
57 uint32_t size, uint8_t *buffer);
58 static int target_write_buffer_default(struct target *target, uint32_t address,
59 uint32_t size, const uint8_t *buffer);
60 static int target_array2mem(Jim_Interp *interp, struct target *target,
61 int argc, Jim_Obj *const *argv);
62 static int target_mem2array(Jim_Interp *interp, struct target *target,
63 int argc, Jim_Obj *const *argv);
64 static int target_register_user_commands(struct command_context *cmd_ctx);
66 /* targets */
67 extern struct target_type arm7tdmi_target;
68 extern struct target_type arm720t_target;
69 extern struct target_type arm9tdmi_target;
70 extern struct target_type arm920t_target;
71 extern struct target_type arm966e_target;
72 extern struct target_type arm946e_target;
73 extern struct target_type arm926ejs_target;
74 extern struct target_type fa526_target;
75 extern struct target_type feroceon_target;
76 extern struct target_type dragonite_target;
77 extern struct target_type xscale_target;
78 extern struct target_type cortexm3_target;
79 extern struct target_type cortexa8_target;
80 extern struct target_type arm11_target;
81 extern struct target_type mips_m4k_target;
82 extern struct target_type avr_target;
83 extern struct target_type dsp563xx_target;
84 extern struct target_type dsp5680xx_target;
85 extern struct target_type testee_target;
86 extern struct target_type avr32_ap7k_target;
87 extern struct target_type stm32_stlink_target;
89 static struct target_type *target_types[] =
91 &arm7tdmi_target,
92 &arm9tdmi_target,
93 &arm920t_target,
94 &arm720t_target,
95 &arm966e_target,
96 &arm946e_target,
97 &arm926ejs_target,
98 &fa526_target,
99 &feroceon_target,
100 &dragonite_target,
101 &xscale_target,
102 &cortexm3_target,
103 &cortexa8_target,
104 &arm11_target,
105 &mips_m4k_target,
106 &avr_target,
107 &dsp563xx_target,
108 &dsp5680xx_target,
109 &testee_target,
110 &avr32_ap7k_target,
111 &stm32_stlink_target,
112 NULL,
115 struct target *all_targets = NULL;
116 static struct target_event_callback *target_event_callbacks = NULL;
117 static struct target_timer_callback *target_timer_callbacks = NULL;
118 static const int polling_interval = 100;
120 static const Jim_Nvp nvp_assert[] = {
121 { .name = "assert", NVP_ASSERT },
122 { .name = "deassert", NVP_DEASSERT },
123 { .name = "T", NVP_ASSERT },
124 { .name = "F", NVP_DEASSERT },
125 { .name = "t", NVP_ASSERT },
126 { .name = "f", NVP_DEASSERT },
127 { .name = NULL, .value = -1 }
130 static const Jim_Nvp nvp_error_target[] = {
131 { .value = ERROR_TARGET_INVALID, .name = "err-invalid" },
132 { .value = ERROR_TARGET_INIT_FAILED, .name = "err-init-failed" },
133 { .value = ERROR_TARGET_TIMEOUT, .name = "err-timeout" },
134 { .value = ERROR_TARGET_NOT_HALTED, .name = "err-not-halted" },
135 { .value = ERROR_TARGET_FAILURE, .name = "err-failure" },
136 { .value = ERROR_TARGET_UNALIGNED_ACCESS , .name = "err-unaligned-access" },
137 { .value = ERROR_TARGET_DATA_ABORT , .name = "err-data-abort" },
138 { .value = ERROR_TARGET_RESOURCE_NOT_AVAILABLE , .name = "err-resource-not-available" },
139 { .value = ERROR_TARGET_TRANSLATION_FAULT , .name = "err-translation-fault" },
140 { .value = ERROR_TARGET_NOT_RUNNING, .name = "err-not-running" },
141 { .value = ERROR_TARGET_NOT_EXAMINED, .name = "err-not-examined" },
142 { .value = -1, .name = NULL }
145 static const char *target_strerror_safe(int err)
147 const Jim_Nvp *n;
149 n = Jim_Nvp_value2name_simple(nvp_error_target, err);
150 if (n->name == NULL) {
151 return "unknown";
152 } else {
153 return n->name;
157 static const Jim_Nvp nvp_target_event[] = {
158 { .value = TARGET_EVENT_OLD_gdb_program_config , .name = "old-gdb_program_config" },
159 { .value = TARGET_EVENT_OLD_pre_resume , .name = "old-pre_resume" },
161 { .value = TARGET_EVENT_GDB_HALT, .name = "gdb-halt" },
162 { .value = TARGET_EVENT_HALTED, .name = "halted" },
163 { .value = TARGET_EVENT_RESUMED, .name = "resumed" },
164 { .value = TARGET_EVENT_RESUME_START, .name = "resume-start" },
165 { .value = TARGET_EVENT_RESUME_END, .name = "resume-end" },
167 { .name = "gdb-start", .value = TARGET_EVENT_GDB_START },
168 { .name = "gdb-end", .value = TARGET_EVENT_GDB_END },
170 /* historical name */
172 { .value = TARGET_EVENT_RESET_START, .name = "reset-start" },
174 { .value = TARGET_EVENT_RESET_ASSERT_PRE, .name = "reset-assert-pre" },
175 { .value = TARGET_EVENT_RESET_ASSERT, .name = "reset-assert" },
176 { .value = TARGET_EVENT_RESET_ASSERT_POST, .name = "reset-assert-post" },
177 { .value = TARGET_EVENT_RESET_DEASSERT_PRE, .name = "reset-deassert-pre" },
178 { .value = TARGET_EVENT_RESET_DEASSERT_POST, .name = "reset-deassert-post" },
179 { .value = TARGET_EVENT_RESET_HALT_PRE, .name = "reset-halt-pre" },
180 { .value = TARGET_EVENT_RESET_HALT_POST, .name = "reset-halt-post" },
181 { .value = TARGET_EVENT_RESET_WAIT_PRE, .name = "reset-wait-pre" },
182 { .value = TARGET_EVENT_RESET_WAIT_POST, .name = "reset-wait-post" },
183 { .value = TARGET_EVENT_RESET_INIT, .name = "reset-init" },
184 { .value = TARGET_EVENT_RESET_END, .name = "reset-end" },
186 { .value = TARGET_EVENT_EXAMINE_START, .name = "examine-start" },
187 { .value = TARGET_EVENT_EXAMINE_END, .name = "examine-end" },
189 { .value = TARGET_EVENT_DEBUG_HALTED, .name = "debug-halted" },
190 { .value = TARGET_EVENT_DEBUG_RESUMED, .name = "debug-resumed" },
192 { .value = TARGET_EVENT_GDB_ATTACH, .name = "gdb-attach" },
193 { .value = TARGET_EVENT_GDB_DETACH, .name = "gdb-detach" },
195 { .value = TARGET_EVENT_GDB_FLASH_WRITE_START, .name = "gdb-flash-write-start" },
196 { .value = TARGET_EVENT_GDB_FLASH_WRITE_END , .name = "gdb-flash-write-end" },
198 { .value = TARGET_EVENT_GDB_FLASH_ERASE_START, .name = "gdb-flash-erase-start" },
199 { .value = TARGET_EVENT_GDB_FLASH_ERASE_END , .name = "gdb-flash-erase-end" },
201 { .value = TARGET_EVENT_RESUME_START, .name = "resume-start" },
202 { .value = TARGET_EVENT_RESUMED , .name = "resume-ok" },
203 { .value = TARGET_EVENT_RESUME_END , .name = "resume-end" },
205 { .name = NULL, .value = -1 }
208 static const Jim_Nvp nvp_target_state[] = {
209 { .name = "unknown", .value = TARGET_UNKNOWN },
210 { .name = "running", .value = TARGET_RUNNING },
211 { .name = "halted", .value = TARGET_HALTED },
212 { .name = "reset", .value = TARGET_RESET },
213 { .name = "debug-running", .value = TARGET_DEBUG_RUNNING },
214 { .name = NULL, .value = -1 },
217 static const Jim_Nvp nvp_target_debug_reason [] = {
218 { .name = "debug-request" , .value = DBG_REASON_DBGRQ },
219 { .name = "breakpoint" , .value = DBG_REASON_BREAKPOINT },
220 { .name = "watchpoint" , .value = DBG_REASON_WATCHPOINT },
221 { .name = "watchpoint-and-breakpoint", .value = DBG_REASON_WPTANDBKPT },
222 { .name = "single-step" , .value = DBG_REASON_SINGLESTEP },
223 { .name = "target-not-halted" , .value = DBG_REASON_NOTHALTED },
224 { .name = "undefined" , .value = DBG_REASON_UNDEFINED },
225 { .name = NULL, .value = -1 },
228 static const Jim_Nvp nvp_target_endian[] = {
229 { .name = "big", .value = TARGET_BIG_ENDIAN },
230 { .name = "little", .value = TARGET_LITTLE_ENDIAN },
231 { .name = "be", .value = TARGET_BIG_ENDIAN },
232 { .name = "le", .value = TARGET_LITTLE_ENDIAN },
233 { .name = NULL, .value = -1 },
236 static const Jim_Nvp nvp_reset_modes[] = {
237 { .name = "unknown", .value = RESET_UNKNOWN },
238 { .name = "run" , .value = RESET_RUN },
239 { .name = "halt" , .value = RESET_HALT },
240 { .name = "init" , .value = RESET_INIT },
241 { .name = NULL , .value = -1 },
244 const char *debug_reason_name(struct target *t)
246 const char *cp;
248 cp = Jim_Nvp_value2name_simple(nvp_target_debug_reason,
249 t->debug_reason)->name;
250 if (!cp) {
251 LOG_ERROR("Invalid debug reason: %d", (int)(t->debug_reason));
252 cp = "(*BUG*unknown*BUG*)";
254 return cp;
257 const char *
258 target_state_name( struct target *t )
260 const char *cp;
261 cp = Jim_Nvp_value2name_simple(nvp_target_state, t->state)->name;
262 if( !cp ){
263 LOG_ERROR("Invalid target state: %d", (int)(t->state));
264 cp = "(*BUG*unknown*BUG*)";
266 return cp;
269 /* determine the number of the new target */
270 static int new_target_number(void)
272 struct target *t;
273 int x;
275 /* number is 0 based */
276 x = -1;
277 t = all_targets;
278 while (t) {
279 if (x < t->target_number) {
280 x = t->target_number;
282 t = t->next;
284 return x + 1;
287 /* read a uint32_t from a buffer in target memory endianness */
288 uint32_t target_buffer_get_u32(struct target *target, const uint8_t *buffer)
290 if (target->endianness == TARGET_LITTLE_ENDIAN)
291 return le_to_h_u32(buffer);
292 else
293 return be_to_h_u32(buffer);
296 /* read a uint24_t from a buffer in target memory endianness */
297 uint32_t target_buffer_get_u24(struct target *target, const uint8_t *buffer)
299 if (target->endianness == TARGET_LITTLE_ENDIAN)
300 return le_to_h_u24(buffer);
301 else
302 return be_to_h_u24(buffer);
305 /* read a uint16_t from a buffer in target memory endianness */
306 uint16_t target_buffer_get_u16(struct target *target, const uint8_t *buffer)
308 if (target->endianness == TARGET_LITTLE_ENDIAN)
309 return le_to_h_u16(buffer);
310 else
311 return be_to_h_u16(buffer);
314 /* read a uint8_t from a buffer in target memory endianness */
315 static uint8_t target_buffer_get_u8(struct target *target, const uint8_t *buffer)
317 return *buffer & 0x0ff;
320 /* write a uint32_t to a buffer in target memory endianness */
321 void target_buffer_set_u32(struct target *target, uint8_t *buffer, uint32_t value)
323 if (target->endianness == TARGET_LITTLE_ENDIAN)
324 h_u32_to_le(buffer, value);
325 else
326 h_u32_to_be(buffer, value);
329 /* write a uint24_t to a buffer in target memory endianness */
330 void target_buffer_set_u24(struct target *target, uint8_t *buffer, uint32_t value)
332 if (target->endianness == TARGET_LITTLE_ENDIAN)
333 h_u24_to_le(buffer, value);
334 else
335 h_u24_to_be(buffer, value);
338 /* write a uint16_t to a buffer in target memory endianness */
339 void target_buffer_set_u16(struct target *target, uint8_t *buffer, uint16_t value)
341 if (target->endianness == TARGET_LITTLE_ENDIAN)
342 h_u16_to_le(buffer, value);
343 else
344 h_u16_to_be(buffer, value);
347 /* write a uint8_t to a buffer in target memory endianness */
348 static void target_buffer_set_u8(struct target *target, uint8_t *buffer, uint8_t value)
350 *buffer = value;
353 /* write a uint32_t array to a buffer in target memory endianness */
354 void target_buffer_get_u32_array(struct target *target, const uint8_t *buffer, uint32_t count, uint32_t *dstbuf)
356 uint32_t i;
357 for(i = 0; i < count; i ++)
358 dstbuf[i] = target_buffer_get_u32(target,&buffer[i*4]);
361 /* write a uint16_t array to a buffer in target memory endianness */
362 void target_buffer_get_u16_array(struct target *target, const uint8_t *buffer, uint32_t count, uint16_t *dstbuf)
364 uint32_t i;
365 for(i = 0; i < count; i ++)
366 dstbuf[i] = target_buffer_get_u16(target,&buffer[i*2]);
369 /* write a uint32_t array to a buffer in target memory endianness */
370 void target_buffer_set_u32_array(struct target *target, uint8_t *buffer, uint32_t count, uint32_t *srcbuf)
372 uint32_t i;
373 for(i = 0; i < count; i ++)
374 target_buffer_set_u32(target,&buffer[i*4],srcbuf[i]);
377 /* write a uint16_t array to a buffer in target memory endianness */
378 void target_buffer_set_u16_array(struct target *target, uint8_t *buffer, uint32_t count, uint16_t *srcbuf)
380 uint32_t i;
381 for(i = 0; i < count; i ++)
382 target_buffer_set_u16(target,&buffer[i*2],srcbuf[i]);
385 /* return a pointer to a configured target; id is name or number */
386 struct target *get_target(const char *id)
388 struct target *target;
390 /* try as tcltarget name */
391 for (target = all_targets; target; target = target->next) {
392 if (target->cmd_name == NULL)
393 continue;
394 if (strcmp(id, target->cmd_name) == 0)
395 return target;
398 /* It's OK to remove this fallback sometime after August 2010 or so */
400 /* no match, try as number */
401 unsigned num;
402 if (parse_uint(id, &num) != ERROR_OK)
403 return NULL;
405 for (target = all_targets; target; target = target->next) {
406 if (target->target_number == (int)num) {
407 LOG_WARNING("use '%s' as target identifier, not '%u'",
408 target->cmd_name, num);
409 return target;
413 return NULL;
416 /* returns a pointer to the n-th configured target */
417 static struct target *get_target_by_num(int num)
419 struct target *target = all_targets;
421 while (target) {
422 if (target->target_number == num) {
423 return target;
425 target = target->next;
428 return NULL;
431 struct target* get_current_target(struct command_context *cmd_ctx)
433 struct target *target = get_target_by_num(cmd_ctx->current_target);
435 if (target == NULL)
437 LOG_ERROR("BUG: current_target out of bounds");
438 exit(-1);
441 return target;
444 int target_poll(struct target *target)
446 int retval;
448 /* We can't poll until after examine */
449 if (!target_was_examined(target))
451 /* Fail silently lest we pollute the log */
452 return ERROR_FAIL;
455 retval = target->type->poll(target);
456 if (retval != ERROR_OK)
457 return retval;
459 if (target->halt_issued)
461 if (target->state == TARGET_HALTED)
463 target->halt_issued = false;
464 } else
466 long long t = timeval_ms() - target->halt_issued_time;
467 if (t>1000)
469 target->halt_issued = false;
470 LOG_INFO("Halt timed out, wake up GDB.");
471 target_call_event_callbacks(target, TARGET_EVENT_GDB_HALT);
476 return ERROR_OK;
479 int target_halt(struct target *target)
481 int retval;
482 /* We can't poll until after examine */
483 if (!target_was_examined(target))
485 LOG_ERROR("Target not examined yet");
486 return ERROR_FAIL;
489 retval = target->type->halt(target);
490 if (retval != ERROR_OK)
491 return retval;
493 target->halt_issued = true;
494 target->halt_issued_time = timeval_ms();
496 return ERROR_OK;
500 * Make the target (re)start executing using its saved execution
501 * context (possibly with some modifications).
503 * @param target Which target should start executing.
504 * @param current True to use the target's saved program counter instead
505 * of the address parameter
506 * @param address Optionally used as the program counter.
507 * @param handle_breakpoints True iff breakpoints at the resumption PC
508 * should be skipped. (For example, maybe execution was stopped by
509 * such a breakpoint, in which case it would be counterprodutive to
510 * let it re-trigger.
511 * @param debug_execution False if all working areas allocated by OpenOCD
512 * should be released and/or restored to their original contents.
513 * (This would for example be true to run some downloaded "helper"
514 * algorithm code, which resides in one such working buffer and uses
515 * another for data storage.)
517 * @todo Resolve the ambiguity about what the "debug_execution" flag
518 * signifies. For example, Target implementations don't agree on how
519 * it relates to invalidation of the register cache, or to whether
520 * breakpoints and watchpoints should be enabled. (It would seem wrong
521 * to enable breakpoints when running downloaded "helper" algorithms
522 * (debug_execution true), since the breakpoints would be set to match
523 * target firmware being debugged, not the helper algorithm.... and
524 * enabling them could cause such helpers to malfunction (for example,
525 * by overwriting data with a breakpoint instruction. On the other
526 * hand the infrastructure for running such helpers might use this
527 * procedure but rely on hardware breakpoint to detect termination.)
529 int target_resume(struct target *target, int current, uint32_t address, int handle_breakpoints, int debug_execution)
531 int retval;
533 /* We can't poll until after examine */
534 if (!target_was_examined(target))
536 LOG_ERROR("Target not examined yet");
537 return ERROR_FAIL;
540 /* note that resume *must* be asynchronous. The CPU can halt before
541 * we poll. The CPU can even halt at the current PC as a result of
542 * a software breakpoint being inserted by (a bug?) the application.
544 if ((retval = target->type->resume(target, current, address, handle_breakpoints, debug_execution)) != ERROR_OK)
545 return retval;
547 return retval;
550 static int target_process_reset(struct command_context *cmd_ctx, enum target_reset_mode reset_mode)
552 char buf[100];
553 int retval;
554 Jim_Nvp *n;
555 n = Jim_Nvp_value2name_simple(nvp_reset_modes, reset_mode);
556 if (n->name == NULL) {
557 LOG_ERROR("invalid reset mode");
558 return ERROR_FAIL;
561 /* disable polling during reset to make reset event scripts
562 * more predictable, i.e. dr/irscan & pathmove in events will
563 * not have JTAG operations injected into the middle of a sequence.
565 bool save_poll = jtag_poll_get_enabled();
567 jtag_poll_set_enabled(false);
569 sprintf(buf, "ocd_process_reset %s", n->name);
570 retval = Jim_Eval(cmd_ctx->interp, buf);
572 jtag_poll_set_enabled(save_poll);
574 if (retval != JIM_OK) {
575 Jim_MakeErrorMessage(cmd_ctx->interp);
576 command_print(NULL,"%s\n", Jim_GetString(Jim_GetResult(cmd_ctx->interp), NULL));
577 return ERROR_FAIL;
580 /* We want any events to be processed before the prompt */
581 retval = target_call_timer_callbacks_now();
583 struct target *target;
584 for (target = all_targets; target; target = target->next) {
585 target->type->check_reset(target);
588 return retval;
591 static int identity_virt2phys(struct target *target,
592 uint32_t virtual, uint32_t *physical)
594 *physical = virtual;
595 return ERROR_OK;
598 static int no_mmu(struct target *target, int *enabled)
600 *enabled = 0;
601 return ERROR_OK;
604 static int default_examine(struct target *target)
606 target_set_examined(target);
607 return ERROR_OK;
610 /* no check by default */
611 static int default_check_reset(struct target *target)
613 return ERROR_OK;
616 int target_examine_one(struct target *target)
618 return target->type->examine(target);
621 static int jtag_enable_callback(enum jtag_event event, void *priv)
623 struct target *target = priv;
625 if (event != JTAG_TAP_EVENT_ENABLE || !target->tap->enabled)
626 return ERROR_OK;
628 jtag_unregister_event_callback(jtag_enable_callback, target);
629 return target_examine_one(target);
633 /* Targets that correctly implement init + examine, i.e.
634 * no communication with target during init:
636 * XScale
638 int target_examine(void)
640 int retval = ERROR_OK;
641 struct target *target;
643 for (target = all_targets; target; target = target->next)
645 /* defer examination, but don't skip it */
646 if (!target->tap->enabled) {
647 jtag_register_event_callback(jtag_enable_callback,
648 target);
649 continue;
651 if ((retval = target_examine_one(target)) != ERROR_OK)
652 return retval;
654 return retval;
656 const char *target_type_name(struct target *target)
658 return target->type->name;
661 static int target_write_memory_imp(struct target *target, uint32_t address, uint32_t size, uint32_t count, const uint8_t *buffer)
663 if (!target_was_examined(target))
665 LOG_ERROR("Target not examined yet");
666 return ERROR_FAIL;
668 return target->type->write_memory_imp(target, address, size, count, buffer);
671 static int target_read_memory_imp(struct target *target, uint32_t address, uint32_t size, uint32_t count, uint8_t *buffer)
673 if (!target_was_examined(target))
675 LOG_ERROR("Target not examined yet");
676 return ERROR_FAIL;
678 return target->type->read_memory_imp(target, address, size, count, buffer);
681 static int target_soft_reset_halt_imp(struct target *target)
683 if (!target_was_examined(target))
685 LOG_ERROR("Target not examined yet");
686 return ERROR_FAIL;
688 if (!target->type->soft_reset_halt_imp) {
689 LOG_ERROR("Target %s does not support soft_reset_halt",
690 target_name(target));
691 return ERROR_FAIL;
693 return target->type->soft_reset_halt_imp(target);
697 * Downloads a target-specific native code algorithm to the target,
698 * and executes it. * Note that some targets may need to set up, enable,
699 * and tear down a breakpoint (hard or * soft) to detect algorithm
700 * termination, while others may support lower overhead schemes where
701 * soft breakpoints embedded in the algorithm automatically terminate the
702 * algorithm.
704 * @param target used to run the algorithm
705 * @param arch_info target-specific description of the algorithm.
707 int target_run_algorithm(struct target *target,
708 int num_mem_params, struct mem_param *mem_params,
709 int num_reg_params, struct reg_param *reg_param,
710 uint32_t entry_point, uint32_t exit_point,
711 int timeout_ms, void *arch_info)
713 int retval = ERROR_FAIL;
715 if (!target_was_examined(target))
717 LOG_ERROR("Target not examined yet");
718 goto done;
720 if (!target->type->run_algorithm) {
721 LOG_ERROR("Target type '%s' does not support %s",
722 target_type_name(target), __func__);
723 goto done;
726 target->running_alg = true;
727 retval = target->type->run_algorithm(target,
728 num_mem_params, mem_params,
729 num_reg_params, reg_param,
730 entry_point, exit_point, timeout_ms, arch_info);
731 target->running_alg = false;
733 done:
734 return retval;
738 * Downloads a target-specific native code algorithm to the target,
739 * executes and leaves it running.
741 * @param target used to run the algorithm
742 * @param arch_info target-specific description of the algorithm.
744 int target_start_algorithm(struct target *target,
745 int num_mem_params, struct mem_param *mem_params,
746 int num_reg_params, struct reg_param *reg_params,
747 uint32_t entry_point, uint32_t exit_point,
748 void *arch_info)
750 int retval = ERROR_FAIL;
752 if (!target_was_examined(target))
754 LOG_ERROR("Target not examined yet");
755 goto done;
757 if (!target->type->start_algorithm) {
758 LOG_ERROR("Target type '%s' does not support %s",
759 target_type_name(target), __func__);
760 goto done;
762 if (target->running_alg) {
763 LOG_ERROR("Target is already running an algorithm");
764 goto done;
767 target->running_alg = true;
768 retval = target->type->start_algorithm(target,
769 num_mem_params, mem_params,
770 num_reg_params, reg_params,
771 entry_point, exit_point, arch_info);
773 done:
774 return retval;
778 * Waits for an algorithm started with target_start_algorithm() to complete.
780 * @param target used to run the algorithm
781 * @param arch_info target-specific description of the algorithm.
783 int target_wait_algorithm(struct target *target,
784 int num_mem_params, struct mem_param *mem_params,
785 int num_reg_params, struct reg_param *reg_params,
786 uint32_t exit_point, int timeout_ms,
787 void *arch_info)
789 int retval = ERROR_FAIL;
791 if (!target->type->wait_algorithm) {
792 LOG_ERROR("Target type '%s' does not support %s",
793 target_type_name(target), __func__);
794 goto done;
796 if (!target->running_alg) {
797 LOG_ERROR("Target is not running an algorithm");
798 goto done;
801 retval = target->type->wait_algorithm(target,
802 num_mem_params, mem_params,
803 num_reg_params, reg_params,
804 exit_point, timeout_ms, arch_info);
805 if (retval != ERROR_TARGET_TIMEOUT)
806 target->running_alg = false;
808 done:
809 return retval;
813 int target_read_memory(struct target *target,
814 uint32_t address, uint32_t size, uint32_t count, uint8_t *buffer)
816 return target->type->read_memory(target, address, size, count, buffer);
819 static int target_read_phys_memory(struct target *target,
820 uint32_t address, uint32_t size, uint32_t count, uint8_t *buffer)
822 return target->type->read_phys_memory(target, address, size, count, buffer);
825 int target_write_memory(struct target *target,
826 uint32_t address, uint32_t size, uint32_t count, const uint8_t *buffer)
828 return target->type->write_memory(target, address, size, count, buffer);
831 static int target_write_phys_memory(struct target *target,
832 uint32_t address, uint32_t size, uint32_t count, const uint8_t *buffer)
834 return target->type->write_phys_memory(target, address, size, count, buffer);
837 int target_bulk_write_memory(struct target *target,
838 uint32_t address, uint32_t count, const uint8_t *buffer)
840 return target->type->bulk_write_memory(target, address, count, buffer);
843 int target_add_breakpoint(struct target *target,
844 struct breakpoint *breakpoint)
846 if ((target->state != TARGET_HALTED)&&(breakpoint->type!=BKPT_HARD)) {
847 LOG_WARNING("target %s is not halted", target->cmd_name);
848 return ERROR_TARGET_NOT_HALTED;
850 return target->type->add_breakpoint(target, breakpoint);
853 int target_add_context_breakpoint(struct target *target,
854 struct breakpoint *breakpoint)
856 if (target->state != TARGET_HALTED) {
857 LOG_WARNING("target %s is not halted", target->cmd_name);
858 return ERROR_TARGET_NOT_HALTED;
860 return target->type->add_context_breakpoint(target, breakpoint);
863 int target_add_hybrid_breakpoint(struct target *target,
864 struct breakpoint *breakpoint)
866 if (target->state != TARGET_HALTED) {
867 LOG_WARNING("target %s is not halted", target->cmd_name);
868 return ERROR_TARGET_NOT_HALTED;
870 return target->type->add_hybrid_breakpoint(target, breakpoint);
873 int target_remove_breakpoint(struct target *target,
874 struct breakpoint *breakpoint)
876 return target->type->remove_breakpoint(target, breakpoint);
879 int target_add_watchpoint(struct target *target,
880 struct watchpoint *watchpoint)
882 if (target->state != TARGET_HALTED) {
883 LOG_WARNING("target %s is not halted", target->cmd_name);
884 return ERROR_TARGET_NOT_HALTED;
886 return target->type->add_watchpoint(target, watchpoint);
888 int target_remove_watchpoint(struct target *target,
889 struct watchpoint *watchpoint)
891 return target->type->remove_watchpoint(target, watchpoint);
894 int target_get_gdb_reg_list(struct target *target,
895 struct reg **reg_list[], int *reg_list_size)
897 return target->type->get_gdb_reg_list(target, reg_list, reg_list_size);
899 int target_step(struct target *target,
900 int current, uint32_t address, int handle_breakpoints)
902 return target->type->step(target, current, address, handle_breakpoints);
907 * Reset the @c examined flag for the given target.
908 * Pure paranoia -- targets are zeroed on allocation.
910 static void target_reset_examined(struct target *target)
912 target->examined = false;
915 static int
916 err_read_phys_memory(struct target *target, uint32_t address,
917 uint32_t size, uint32_t count, uint8_t *buffer)
919 LOG_ERROR("Not implemented: %s", __func__);
920 return ERROR_FAIL;
923 static int
924 err_write_phys_memory(struct target *target, uint32_t address,
925 uint32_t size, uint32_t count, const uint8_t *buffer)
927 LOG_ERROR("Not implemented: %s", __func__);
928 return ERROR_FAIL;
931 static int handle_target(void *priv);
933 static int target_init_one(struct command_context *cmd_ctx,
934 struct target *target)
936 target_reset_examined(target);
938 struct target_type *type = target->type;
939 if (type->examine == NULL)
940 type->examine = default_examine;
942 if (type->check_reset== NULL)
943 type->check_reset = default_check_reset;
945 assert(type->init_target != NULL);
947 int retval = type->init_target(cmd_ctx, target);
948 if (ERROR_OK != retval)
950 LOG_ERROR("target '%s' init failed", target_name(target));
951 return retval;
955 * @todo get rid of those *memory_imp() methods, now that all
956 * callers are using target_*_memory() accessors ... and make
957 * sure the "physical" paths handle the same issues.
959 /* a non-invasive way(in terms of patches) to add some code that
960 * runs before the type->write/read_memory implementation
962 type->write_memory_imp = target->type->write_memory;
963 type->write_memory = target_write_memory_imp;
965 type->read_memory_imp = target->type->read_memory;
966 type->read_memory = target_read_memory_imp;
968 type->soft_reset_halt_imp = target->type->soft_reset_halt;
969 type->soft_reset_halt = target_soft_reset_halt_imp;
971 /* Sanity-check MMU support ... stub in what we must, to help
972 * implement it in stages, but warn if we need to do so.
974 if (type->mmu)
976 if (type->write_phys_memory == NULL)
978 LOG_ERROR("type '%s' is missing write_phys_memory",
979 type->name);
980 type->write_phys_memory = err_write_phys_memory;
982 if (type->read_phys_memory == NULL)
984 LOG_ERROR("type '%s' is missing read_phys_memory",
985 type->name);
986 type->read_phys_memory = err_read_phys_memory;
988 if (type->virt2phys == NULL)
990 LOG_ERROR("type '%s' is missing virt2phys", type->name);
991 type->virt2phys = identity_virt2phys;
994 else
996 /* Make sure no-MMU targets all behave the same: make no
997 * distinction between physical and virtual addresses, and
998 * ensure that virt2phys() is always an identity mapping.
1000 if (type->write_phys_memory || type->read_phys_memory
1001 || type->virt2phys)
1003 LOG_WARNING("type '%s' has bad MMU hooks", type->name);
1006 type->mmu = no_mmu;
1007 type->write_phys_memory = type->write_memory;
1008 type->read_phys_memory = type->read_memory;
1009 type->virt2phys = identity_virt2phys;
1012 if (target->type->read_buffer == NULL)
1013 target->type->read_buffer = target_read_buffer_default;
1015 if (target->type->write_buffer == NULL)
1016 target->type->write_buffer = target_write_buffer_default;
1018 return ERROR_OK;
1021 static int target_init(struct command_context *cmd_ctx)
1023 struct target *target;
1024 int retval;
1026 for (target = all_targets; target; target = target->next)
1028 retval = target_init_one(cmd_ctx, target);
1029 if (ERROR_OK != retval)
1030 return retval;
1033 if (!all_targets)
1034 return ERROR_OK;
1036 retval = target_register_user_commands(cmd_ctx);
1037 if (ERROR_OK != retval)
1038 return retval;
1040 retval = target_register_timer_callback(&handle_target,
1041 polling_interval, 1, cmd_ctx->interp);
1042 if (ERROR_OK != retval)
1043 return retval;
1045 return ERROR_OK;
1048 COMMAND_HANDLER(handle_target_init_command)
1050 int retval;
1052 if (CMD_ARGC != 0)
1053 return ERROR_COMMAND_SYNTAX_ERROR;
1055 static bool target_initialized = false;
1056 if (target_initialized)
1058 LOG_INFO("'target init' has already been called");
1059 return ERROR_OK;
1061 target_initialized = true;
1063 retval = command_run_line(CMD_CTX, "init_targets");
1064 if (ERROR_OK != retval)
1065 return retval;
1067 LOG_DEBUG("Initializing targets...");
1068 return target_init(CMD_CTX);
1071 int target_register_event_callback(int (*callback)(struct target *target, enum target_event event, void *priv), void *priv)
1073 struct target_event_callback **callbacks_p = &target_event_callbacks;
1075 if (callback == NULL)
1077 return ERROR_INVALID_ARGUMENTS;
1080 if (*callbacks_p)
1082 while ((*callbacks_p)->next)
1083 callbacks_p = &((*callbacks_p)->next);
1084 callbacks_p = &((*callbacks_p)->next);
1087 (*callbacks_p) = malloc(sizeof(struct target_event_callback));
1088 (*callbacks_p)->callback = callback;
1089 (*callbacks_p)->priv = priv;
1090 (*callbacks_p)->next = NULL;
1092 return ERROR_OK;
1095 int target_register_timer_callback(int (*callback)(void *priv), int time_ms, int periodic, void *priv)
1097 struct target_timer_callback **callbacks_p = &target_timer_callbacks;
1098 struct timeval now;
1100 if (callback == NULL)
1102 return ERROR_INVALID_ARGUMENTS;
1105 if (*callbacks_p)
1107 while ((*callbacks_p)->next)
1108 callbacks_p = &((*callbacks_p)->next);
1109 callbacks_p = &((*callbacks_p)->next);
1112 (*callbacks_p) = malloc(sizeof(struct target_timer_callback));
1113 (*callbacks_p)->callback = callback;
1114 (*callbacks_p)->periodic = periodic;
1115 (*callbacks_p)->time_ms = time_ms;
1117 gettimeofday(&now, NULL);
1118 (*callbacks_p)->when.tv_usec = now.tv_usec + (time_ms % 1000) * 1000;
1119 time_ms -= (time_ms % 1000);
1120 (*callbacks_p)->when.tv_sec = now.tv_sec + (time_ms / 1000);
1121 if ((*callbacks_p)->when.tv_usec > 1000000)
1123 (*callbacks_p)->when.tv_usec = (*callbacks_p)->when.tv_usec - 1000000;
1124 (*callbacks_p)->when.tv_sec += 1;
1127 (*callbacks_p)->priv = priv;
1128 (*callbacks_p)->next = NULL;
1130 return ERROR_OK;
1133 int target_unregister_event_callback(int (*callback)(struct target *target, enum target_event event, void *priv), void *priv)
1135 struct target_event_callback **p = &target_event_callbacks;
1136 struct target_event_callback *c = target_event_callbacks;
1138 if (callback == NULL)
1140 return ERROR_INVALID_ARGUMENTS;
1143 while (c)
1145 struct target_event_callback *next = c->next;
1146 if ((c->callback == callback) && (c->priv == priv))
1148 *p = next;
1149 free(c);
1150 return ERROR_OK;
1152 else
1153 p = &(c->next);
1154 c = next;
1157 return ERROR_OK;
1160 static int target_unregister_timer_callback(int (*callback)(void *priv), void *priv)
1162 struct target_timer_callback **p = &target_timer_callbacks;
1163 struct target_timer_callback *c = target_timer_callbacks;
1165 if (callback == NULL)
1167 return ERROR_INVALID_ARGUMENTS;
1170 while (c)
1172 struct target_timer_callback *next = c->next;
1173 if ((c->callback == callback) && (c->priv == priv))
1175 *p = next;
1176 free(c);
1177 return ERROR_OK;
1179 else
1180 p = &(c->next);
1181 c = next;
1184 return ERROR_OK;
1187 int target_call_event_callbacks(struct target *target, enum target_event event)
1189 struct target_event_callback *callback = target_event_callbacks;
1190 struct target_event_callback *next_callback;
1192 if (event == TARGET_EVENT_HALTED)
1194 /* execute early halted first */
1195 target_call_event_callbacks(target, TARGET_EVENT_GDB_HALT);
1198 LOG_DEBUG("target event %i (%s)",
1199 event,
1200 Jim_Nvp_value2name_simple(nvp_target_event, event)->name);
1202 target_handle_event(target, event);
1204 while (callback)
1206 next_callback = callback->next;
1207 callback->callback(target, event, callback->priv);
1208 callback = next_callback;
1211 return ERROR_OK;
1214 static int target_timer_callback_periodic_restart(
1215 struct target_timer_callback *cb, struct timeval *now)
1217 int time_ms = cb->time_ms;
1218 cb->when.tv_usec = now->tv_usec + (time_ms % 1000) * 1000;
1219 time_ms -= (time_ms % 1000);
1220 cb->when.tv_sec = now->tv_sec + time_ms / 1000;
1221 if (cb->when.tv_usec > 1000000)
1223 cb->when.tv_usec = cb->when.tv_usec - 1000000;
1224 cb->when.tv_sec += 1;
1226 return ERROR_OK;
1229 static int target_call_timer_callback(struct target_timer_callback *cb,
1230 struct timeval *now)
1232 cb->callback(cb->priv);
1234 if (cb->periodic)
1235 return target_timer_callback_periodic_restart(cb, now);
1237 return target_unregister_timer_callback(cb->callback, cb->priv);
1240 static int target_call_timer_callbacks_check_time(int checktime)
1242 keep_alive();
1244 struct timeval now;
1245 gettimeofday(&now, NULL);
1247 struct target_timer_callback *callback = target_timer_callbacks;
1248 while (callback)
1250 // cleaning up may unregister and free this callback
1251 struct target_timer_callback *next_callback = callback->next;
1253 bool call_it = callback->callback &&
1254 ((!checktime && callback->periodic) ||
1255 now.tv_sec > callback->when.tv_sec ||
1256 (now.tv_sec == callback->when.tv_sec &&
1257 now.tv_usec >= callback->when.tv_usec));
1259 if (call_it)
1261 int retval = target_call_timer_callback(callback, &now);
1262 if (retval != ERROR_OK)
1263 return retval;
1266 callback = next_callback;
1269 return ERROR_OK;
1272 int target_call_timer_callbacks(void)
1274 return target_call_timer_callbacks_check_time(1);
1277 /* invoke periodic callbacks immediately */
1278 int target_call_timer_callbacks_now(void)
1280 return target_call_timer_callbacks_check_time(0);
1283 int target_alloc_working_area_try(struct target *target, uint32_t size, struct working_area **area)
1285 struct working_area *c = target->working_areas;
1286 struct working_area *new_wa = NULL;
1288 /* Reevaluate working area address based on MMU state*/
1289 if (target->working_areas == NULL)
1291 int retval;
1292 int enabled;
1294 retval = target->type->mmu(target, &enabled);
1295 if (retval != ERROR_OK)
1297 return retval;
1300 if (!enabled) {
1301 if (target->working_area_phys_spec) {
1302 LOG_DEBUG("MMU disabled, using physical "
1303 "address for working memory 0x%08x",
1304 (unsigned)target->working_area_phys);
1305 target->working_area = target->working_area_phys;
1306 } else {
1307 LOG_ERROR("No working memory available. "
1308 "Specify -work-area-phys to target.");
1309 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
1311 } else {
1312 if (target->working_area_virt_spec) {
1313 LOG_DEBUG("MMU enabled, using virtual "
1314 "address for working memory 0x%08x",
1315 (unsigned)target->working_area_virt);
1316 target->working_area = target->working_area_virt;
1317 } else {
1318 LOG_ERROR("No working memory available. "
1319 "Specify -work-area-virt to target.");
1320 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
1325 /* only allocate multiples of 4 byte */
1326 if (size % 4)
1328 LOG_ERROR("BUG: code tried to allocate unaligned number of bytes (0x%08x), padding", ((unsigned)(size)));
1329 size = (size + 3) & (~3);
1332 /* see if there's already a matching working area */
1333 while (c)
1335 if ((c->free) && (c->size == size))
1337 new_wa = c;
1338 break;
1340 c = c->next;
1343 /* if not, allocate a new one */
1344 if (!new_wa)
1346 struct working_area **p = &target->working_areas;
1347 uint32_t first_free = target->working_area;
1348 uint32_t free_size = target->working_area_size;
1350 c = target->working_areas;
1351 while (c)
1353 first_free += c->size;
1354 free_size -= c->size;
1355 p = &c->next;
1356 c = c->next;
1359 if (free_size < size)
1361 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
1364 LOG_DEBUG("allocated new working area at address 0x%08x", (unsigned)first_free);
1366 new_wa = malloc(sizeof(struct working_area));
1367 new_wa->next = NULL;
1368 new_wa->size = size;
1369 new_wa->address = first_free;
1371 if (target->backup_working_area)
1373 int retval;
1374 new_wa->backup = malloc(new_wa->size);
1375 if ((retval = target_read_memory(target, new_wa->address, 4, new_wa->size / 4, new_wa->backup)) != ERROR_OK)
1377 free(new_wa->backup);
1378 free(new_wa);
1379 return retval;
1382 else
1384 new_wa->backup = NULL;
1387 /* put new entry in list */
1388 *p = new_wa;
1391 /* mark as used, and return the new (reused) area */
1392 new_wa->free = false;
1393 *area = new_wa;
1395 /* user pointer */
1396 new_wa->user = area;
1398 return ERROR_OK;
1401 int target_alloc_working_area(struct target *target, uint32_t size, struct working_area **area)
1403 int retval;
1405 retval = target_alloc_working_area_try(target, size, area);
1406 if (retval == ERROR_TARGET_RESOURCE_NOT_AVAILABLE)
1408 LOG_WARNING("not enough working area available(requested %u)", (unsigned)(size));
1410 return retval;
1414 static int target_free_working_area_restore(struct target *target, struct working_area *area, int restore)
1416 if (area->free)
1417 return ERROR_OK;
1419 if (restore && target->backup_working_area)
1421 int retval;
1422 if ((retval = target_write_memory(target, area->address, 4, area->size / 4, area->backup)) != ERROR_OK)
1423 return retval;
1426 area->free = true;
1428 /* mark user pointer invalid */
1429 *area->user = NULL;
1430 area->user = NULL;
1432 return ERROR_OK;
1435 int target_free_working_area(struct target *target, struct working_area *area)
1437 return target_free_working_area_restore(target, area, 1);
1440 /* free resources and restore memory, if restoring memory fails,
1441 * free up resources anyway
1443 static void target_free_all_working_areas_restore(struct target *target, int restore)
1445 struct working_area *c = target->working_areas;
1447 while (c)
1449 struct working_area *next = c->next;
1450 target_free_working_area_restore(target, c, restore);
1452 if (c->backup)
1453 free(c->backup);
1455 free(c);
1457 c = next;
1460 target->working_areas = NULL;
1463 void target_free_all_working_areas(struct target *target)
1465 target_free_all_working_areas_restore(target, 1);
1468 int target_arch_state(struct target *target)
1470 int retval;
1471 if (target == NULL)
1473 LOG_USER("No target has been configured");
1474 return ERROR_OK;
1477 LOG_USER("target state: %s", target_state_name( target ));
1479 if (target->state != TARGET_HALTED)
1480 return ERROR_OK;
1482 retval = target->type->arch_state(target);
1483 return retval;
1486 /* Single aligned words are guaranteed to use 16 or 32 bit access
1487 * mode respectively, otherwise data is handled as quickly as
1488 * possible
1490 int target_write_buffer(struct target *target, uint32_t address, uint32_t size, const uint8_t *buffer)
1492 LOG_DEBUG("writing buffer of %i byte at 0x%8.8x",
1493 (int)size, (unsigned)address);
1495 if (!target_was_examined(target))
1497 LOG_ERROR("Target not examined yet");
1498 return ERROR_FAIL;
1501 if (size == 0) {
1502 return ERROR_OK;
1505 if ((address + size - 1) < address)
1507 /* GDB can request this when e.g. PC is 0xfffffffc*/
1508 LOG_ERROR("address + size wrapped(0x%08x, 0x%08x)",
1509 (unsigned)address,
1510 (unsigned)size);
1511 return ERROR_FAIL;
1514 return target->type->write_buffer(target, address, size, buffer);
1517 static int target_write_buffer_default(struct target *target, uint32_t address, uint32_t size, const uint8_t *buffer)
1519 int retval = ERROR_OK;
1521 if (((address % 2) == 0) && (size == 2))
1523 return target_write_memory(target, address, 2, 1, buffer);
1526 /* handle unaligned head bytes */
1527 if (address % 4)
1529 uint32_t unaligned = 4 - (address % 4);
1531 if (unaligned > size)
1532 unaligned = size;
1534 if ((retval = target_write_memory(target, address, 1, unaligned, buffer)) != ERROR_OK)
1535 return retval;
1537 buffer += unaligned;
1538 address += unaligned;
1539 size -= unaligned;
1542 /* handle aligned words */
1543 if (size >= 4)
1545 int aligned = size - (size % 4);
1547 /* use bulk writes above a certain limit. This may have to be changed */
1548 if (aligned > 128)
1550 if ((retval = target->type->bulk_write_memory(target, address, aligned / 4, buffer)) != ERROR_OK)
1551 return retval;
1553 else
1555 if ((retval = target_write_memory(target, address, 4, aligned / 4, buffer)) != ERROR_OK)
1556 return retval;
1559 buffer += aligned;
1560 address += aligned;
1561 size -= aligned;
1564 /* handle tail writes of less than 4 bytes */
1565 if (size > 0)
1567 if ((retval = target_write_memory(target, address, 1, size, buffer)) != ERROR_OK)
1568 return retval;
1571 return retval;
1574 /* Single aligned words are guaranteed to use 16 or 32 bit access
1575 * mode respectively, otherwise data is handled as quickly as
1576 * possible
1578 int target_read_buffer(struct target *target, uint32_t address, uint32_t size, uint8_t *buffer)
1580 LOG_DEBUG("reading buffer of %i byte at 0x%8.8x",
1581 (int)size, (unsigned)address);
1583 if (!target_was_examined(target))
1585 LOG_ERROR("Target not examined yet");
1586 return ERROR_FAIL;
1589 if (size == 0) {
1590 return ERROR_OK;
1593 if ((address + size - 1) < address)
1595 /* GDB can request this when e.g. PC is 0xfffffffc*/
1596 LOG_ERROR("address + size wrapped(0x%08" PRIx32 ", 0x%08" PRIx32 ")",
1597 address,
1598 size);
1599 return ERROR_FAIL;
1602 return target->type->read_buffer(target, address, size, buffer);
1605 static int target_read_buffer_default(struct target *target, uint32_t address, uint32_t size, uint8_t *buffer)
1607 int retval = ERROR_OK;
1609 if (((address % 2) == 0) && (size == 2))
1611 return target_read_memory(target, address, 2, 1, buffer);
1614 /* handle unaligned head bytes */
1615 if (address % 4)
1617 uint32_t unaligned = 4 - (address % 4);
1619 if (unaligned > size)
1620 unaligned = size;
1622 if ((retval = target_read_memory(target, address, 1, unaligned, buffer)) != ERROR_OK)
1623 return retval;
1625 buffer += unaligned;
1626 address += unaligned;
1627 size -= unaligned;
1630 /* handle aligned words */
1631 if (size >= 4)
1633 int aligned = size - (size % 4);
1635 if ((retval = target_read_memory(target, address, 4, aligned / 4, buffer)) != ERROR_OK)
1636 return retval;
1638 buffer += aligned;
1639 address += aligned;
1640 size -= aligned;
1643 /*prevent byte access when possible (avoid AHB access limitations in some cases)*/
1644 if(size >=2)
1646 int aligned = size - (size%2);
1647 retval = target_read_memory(target, address, 2, aligned / 2, buffer);
1648 if (retval != ERROR_OK)
1649 return retval;
1651 buffer += aligned;
1652 address += aligned;
1653 size -= aligned;
1655 /* handle tail writes of less than 4 bytes */
1656 if (size > 0)
1658 if ((retval = target_read_memory(target, address, 1, size, buffer)) != ERROR_OK)
1659 return retval;
1662 return ERROR_OK;
1665 int target_checksum_memory(struct target *target, uint32_t address, uint32_t size, uint32_t* crc)
1667 uint8_t *buffer;
1668 int retval;
1669 uint32_t i;
1670 uint32_t checksum = 0;
1671 if (!target_was_examined(target))
1673 LOG_ERROR("Target not examined yet");
1674 return ERROR_FAIL;
1677 if ((retval = target->type->checksum_memory(target, address,
1678 size, &checksum)) != ERROR_OK)
1680 buffer = malloc(size);
1681 if (buffer == NULL)
1683 LOG_ERROR("error allocating buffer for section (%d bytes)", (int)size);
1684 return ERROR_INVALID_ARGUMENTS;
1686 retval = target_read_buffer(target, address, size, buffer);
1687 if (retval != ERROR_OK)
1689 free(buffer);
1690 return retval;
1693 /* convert to target endianness */
1694 for (i = 0; i < (size/sizeof(uint32_t)); i++)
1696 uint32_t target_data;
1697 target_data = target_buffer_get_u32(target, &buffer[i*sizeof(uint32_t)]);
1698 target_buffer_set_u32(target, &buffer[i*sizeof(uint32_t)], target_data);
1701 retval = image_calculate_checksum(buffer, size, &checksum);
1702 free(buffer);
1705 *crc = checksum;
1707 return retval;
1710 int target_blank_check_memory(struct target *target, uint32_t address, uint32_t size, uint32_t* blank)
1712 int retval;
1713 if (!target_was_examined(target))
1715 LOG_ERROR("Target not examined yet");
1716 return ERROR_FAIL;
1719 if (target->type->blank_check_memory == 0)
1720 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
1722 retval = target->type->blank_check_memory(target, address, size, blank);
1724 return retval;
1727 int target_read_u32(struct target *target, uint32_t address, uint32_t *value)
1729 uint8_t value_buf[4];
1730 if (!target_was_examined(target))
1732 LOG_ERROR("Target not examined yet");
1733 return ERROR_FAIL;
1736 int retval = target_read_memory(target, address, 4, 1, value_buf);
1738 if (retval == ERROR_OK)
1740 *value = target_buffer_get_u32(target, value_buf);
1741 LOG_DEBUG("address: 0x%8.8" PRIx32 ", value: 0x%8.8" PRIx32 "",
1742 address,
1743 *value);
1745 else
1747 *value = 0x0;
1748 LOG_DEBUG("address: 0x%8.8" PRIx32 " failed",
1749 address);
1752 return retval;
1755 int target_read_u16(struct target *target, uint32_t address, uint16_t *value)
1757 uint8_t value_buf[2];
1758 if (!target_was_examined(target))
1760 LOG_ERROR("Target not examined yet");
1761 return ERROR_FAIL;
1764 int retval = target_read_memory(target, address, 2, 1, value_buf);
1766 if (retval == ERROR_OK)
1768 *value = target_buffer_get_u16(target, value_buf);
1769 LOG_DEBUG("address: 0x%8.8" PRIx32 ", value: 0x%4.4x",
1770 address,
1771 *value);
1773 else
1775 *value = 0x0;
1776 LOG_DEBUG("address: 0x%8.8" PRIx32 " failed",
1777 address);
1780 return retval;
1783 int target_read_u8(struct target *target, uint32_t address, uint8_t *value)
1785 int retval = target_read_memory(target, address, 1, 1, value);
1786 if (!target_was_examined(target))
1788 LOG_ERROR("Target not examined yet");
1789 return ERROR_FAIL;
1792 if (retval == ERROR_OK)
1794 LOG_DEBUG("address: 0x%8.8" PRIx32 ", value: 0x%2.2x",
1795 address,
1796 *value);
1798 else
1800 *value = 0x0;
1801 LOG_DEBUG("address: 0x%8.8" PRIx32 " failed",
1802 address);
1805 return retval;
1808 int target_write_u32(struct target *target, uint32_t address, uint32_t value)
1810 int retval;
1811 uint8_t value_buf[4];
1812 if (!target_was_examined(target))
1814 LOG_ERROR("Target not examined yet");
1815 return ERROR_FAIL;
1818 LOG_DEBUG("address: 0x%8.8" PRIx32 ", value: 0x%8.8" PRIx32 "",
1819 address,
1820 value);
1822 target_buffer_set_u32(target, value_buf, value);
1823 if ((retval = target_write_memory(target, address, 4, 1, value_buf)) != ERROR_OK)
1825 LOG_DEBUG("failed: %i", retval);
1828 return retval;
1831 int target_write_u16(struct target *target, uint32_t address, uint16_t value)
1833 int retval;
1834 uint8_t value_buf[2];
1835 if (!target_was_examined(target))
1837 LOG_ERROR("Target not examined yet");
1838 return ERROR_FAIL;
1841 LOG_DEBUG("address: 0x%8.8" PRIx32 ", value: 0x%8.8x",
1842 address,
1843 value);
1845 target_buffer_set_u16(target, value_buf, value);
1846 if ((retval = target_write_memory(target, address, 2, 1, value_buf)) != ERROR_OK)
1848 LOG_DEBUG("failed: %i", retval);
1851 return retval;
1854 int target_write_u8(struct target *target, uint32_t address, uint8_t value)
1856 int retval;
1857 if (!target_was_examined(target))
1859 LOG_ERROR("Target not examined yet");
1860 return ERROR_FAIL;
1863 LOG_DEBUG("address: 0x%8.8" PRIx32 ", value: 0x%2.2x",
1864 address, value);
1866 if ((retval = target_write_memory(target, address, 1, 1, &value)) != ERROR_OK)
1868 LOG_DEBUG("failed: %i", retval);
1871 return retval;
1874 static int find_target(struct command_context *cmd_ctx, const char *name)
1876 struct target *target = get_target(name);
1877 if (target == NULL) {
1878 LOG_ERROR("Target: %s is unknown, try one of:\n", name);
1879 return ERROR_FAIL;
1881 if (!target->tap->enabled) {
1882 LOG_USER("Target: TAP %s is disabled, "
1883 "can't be the current target\n",
1884 target->tap->dotted_name);
1885 return ERROR_FAIL;
1888 cmd_ctx->current_target = target->target_number;
1889 return ERROR_OK;
1893 COMMAND_HANDLER(handle_targets_command)
1895 int retval = ERROR_OK;
1896 if (CMD_ARGC == 1)
1898 retval = find_target(CMD_CTX, CMD_ARGV[0]);
1899 if (retval == ERROR_OK) {
1900 /* we're done! */
1901 return retval;
1905 struct target *target = all_targets;
1906 command_print(CMD_CTX, " TargetName Type Endian TapName State ");
1907 command_print(CMD_CTX, "-- ------------------ ---------- ------ ------------------ ------------");
1908 while (target)
1910 const char *state;
1911 char marker = ' ';
1913 if (target->tap->enabled)
1914 state = target_state_name( target );
1915 else
1916 state = "tap-disabled";
1918 if (CMD_CTX->current_target == target->target_number)
1919 marker = '*';
1921 /* keep columns lined up to match the headers above */
1922 command_print(CMD_CTX,
1923 "%2d%c %-18s %-10s %-6s %-18s %s",
1924 target->target_number,
1925 marker,
1926 target_name(target),
1927 target_type_name(target),
1928 Jim_Nvp_value2name_simple(nvp_target_endian,
1929 target->endianness)->name,
1930 target->tap->dotted_name,
1931 state);
1932 target = target->next;
1935 return retval;
1938 /* every 300ms we check for reset & powerdropout and issue a "reset halt" if so. */
1940 static int powerDropout;
1941 static int srstAsserted;
1943 static int runPowerRestore;
1944 static int runPowerDropout;
1945 static int runSrstAsserted;
1946 static int runSrstDeasserted;
1948 static int sense_handler(void)
1950 static int prevSrstAsserted = 0;
1951 static int prevPowerdropout = 0;
1953 int retval;
1954 if ((retval = jtag_power_dropout(&powerDropout)) != ERROR_OK)
1955 return retval;
1957 int powerRestored;
1958 powerRestored = prevPowerdropout && !powerDropout;
1959 if (powerRestored)
1961 runPowerRestore = 1;
1964 long long current = timeval_ms();
1965 static long long lastPower = 0;
1966 int waitMore = lastPower + 2000 > current;
1967 if (powerDropout && !waitMore)
1969 runPowerDropout = 1;
1970 lastPower = current;
1973 if ((retval = jtag_srst_asserted(&srstAsserted)) != ERROR_OK)
1974 return retval;
1976 int srstDeasserted;
1977 srstDeasserted = prevSrstAsserted && !srstAsserted;
1979 static long long lastSrst = 0;
1980 waitMore = lastSrst + 2000 > current;
1981 if (srstDeasserted && !waitMore)
1983 runSrstDeasserted = 1;
1984 lastSrst = current;
1987 if (!prevSrstAsserted && srstAsserted)
1989 runSrstAsserted = 1;
1992 prevSrstAsserted = srstAsserted;
1993 prevPowerdropout = powerDropout;
1995 if (srstDeasserted || powerRestored)
1997 /* Other than logging the event we can't do anything here.
1998 * Issuing a reset is a particularly bad idea as we might
1999 * be inside a reset already.
2003 return ERROR_OK;
2006 static int backoff_times = 0;
2007 static int backoff_count = 0;
2009 /* process target state changes */
2010 static int handle_target(void *priv)
2012 Jim_Interp *interp = (Jim_Interp *)priv;
2013 int retval = ERROR_OK;
2015 if (!is_jtag_poll_safe())
2017 /* polling is disabled currently */
2018 return ERROR_OK;
2021 /* we do not want to recurse here... */
2022 static int recursive = 0;
2023 if (! recursive)
2025 recursive = 1;
2026 sense_handler();
2027 /* danger! running these procedures can trigger srst assertions and power dropouts.
2028 * We need to avoid an infinite loop/recursion here and we do that by
2029 * clearing the flags after running these events.
2031 int did_something = 0;
2032 if (runSrstAsserted)
2034 LOG_INFO("srst asserted detected, running srst_asserted proc.");
2035 Jim_Eval(interp, "srst_asserted");
2036 did_something = 1;
2038 if (runSrstDeasserted)
2040 Jim_Eval(interp, "srst_deasserted");
2041 did_something = 1;
2043 if (runPowerDropout)
2045 LOG_INFO("Power dropout detected, running power_dropout proc.");
2046 Jim_Eval(interp, "power_dropout");
2047 did_something = 1;
2049 if (runPowerRestore)
2051 Jim_Eval(interp, "power_restore");
2052 did_something = 1;
2055 if (did_something)
2057 /* clear detect flags */
2058 sense_handler();
2061 /* clear action flags */
2063 runSrstAsserted = 0;
2064 runSrstDeasserted = 0;
2065 runPowerRestore = 0;
2066 runPowerDropout = 0;
2068 recursive = 0;
2071 if (backoff_times > backoff_count)
2073 /* do not poll this time as we failed previously */
2074 backoff_count++;
2075 return ERROR_OK;
2077 backoff_count = 0;
2079 /* Poll targets for state changes unless that's globally disabled.
2080 * Skip targets that are currently disabled.
2082 for (struct target *target = all_targets;
2083 is_jtag_poll_safe() && target;
2084 target = target->next)
2086 if (!target->tap->enabled)
2087 continue;
2089 /* only poll target if we've got power and srst isn't asserted */
2090 if (!powerDropout && !srstAsserted)
2092 /* polling may fail silently until the target has been examined */
2093 if ((retval = target_poll(target)) != ERROR_OK)
2095 /* 100ms polling interval. Increase interval between polling up to 5000ms */
2096 if (backoff_times * polling_interval < 5000)
2098 backoff_times *= 2;
2099 backoff_times++;
2101 LOG_USER("Polling target failed, GDB will be halted. Polling again in %dms", backoff_times * polling_interval);
2103 /* Tell GDB to halt the debugger. This allows the user to
2104 * run monitor commands to handle the situation.
2106 target_call_event_callbacks(target, TARGET_EVENT_GDB_HALT);
2107 return retval;
2109 /* Since we succeeded, we reset backoff count */
2110 if (backoff_times > 0)
2112 LOG_USER("Polling succeeded again");
2114 backoff_times = 0;
2118 return retval;
2121 COMMAND_HANDLER(handle_reg_command)
2123 struct target *target;
2124 struct reg *reg = NULL;
2125 unsigned count = 0;
2126 char *value;
2128 LOG_DEBUG("-");
2130 target = get_current_target(CMD_CTX);
2132 /* list all available registers for the current target */
2133 if (CMD_ARGC == 0)
2135 struct reg_cache *cache = target->reg_cache;
2137 count = 0;
2138 while (cache)
2140 unsigned i;
2142 command_print(CMD_CTX, "===== %s", cache->name);
2144 for (i = 0, reg = cache->reg_list;
2145 i < cache->num_regs;
2146 i++, reg++, count++)
2148 /* only print cached values if they are valid */
2149 if (reg->valid) {
2150 value = buf_to_str(reg->value,
2151 reg->size, 16);
2152 command_print(CMD_CTX,
2153 "(%i) %s (/%" PRIu32 "): 0x%s%s",
2154 count, reg->name,
2155 reg->size, value,
2156 reg->dirty
2157 ? " (dirty)"
2158 : "");
2159 free(value);
2160 } else {
2161 command_print(CMD_CTX, "(%i) %s (/%" PRIu32 ")",
2162 count, reg->name,
2163 reg->size) ;
2166 cache = cache->next;
2169 return ERROR_OK;
2172 /* access a single register by its ordinal number */
2173 if ((CMD_ARGV[0][0] >= '0') && (CMD_ARGV[0][0] <= '9'))
2175 unsigned num;
2176 COMMAND_PARSE_NUMBER(uint, CMD_ARGV[0], num);
2178 struct reg_cache *cache = target->reg_cache;
2179 count = 0;
2180 while (cache)
2182 unsigned i;
2183 for (i = 0; i < cache->num_regs; i++)
2185 if (count++ == num)
2187 reg = &cache->reg_list[i];
2188 break;
2191 if (reg)
2192 break;
2193 cache = cache->next;
2196 if (!reg)
2198 command_print(CMD_CTX, "%i is out of bounds, the current target has only %i registers (0 - %i)", num, count, count - 1);
2199 return ERROR_OK;
2201 } else /* access a single register by its name */
2203 reg = register_get_by_name(target->reg_cache, CMD_ARGV[0], 1);
2205 if (!reg)
2207 command_print(CMD_CTX, "register %s not found in current target", CMD_ARGV[0]);
2208 return ERROR_OK;
2212 assert(reg != NULL); /* give clang a hint that we *know* reg is != NULL here */
2214 /* display a register */
2215 if ((CMD_ARGC == 1) || ((CMD_ARGC == 2) && !((CMD_ARGV[1][0] >= '0') && (CMD_ARGV[1][0] <= '9'))))
2217 if ((CMD_ARGC == 2) && (strcmp(CMD_ARGV[1], "force") == 0))
2218 reg->valid = 0;
2220 if (reg->valid == 0)
2222 reg->type->get(reg);
2224 value = buf_to_str(reg->value, reg->size, 16);
2225 command_print(CMD_CTX, "%s (/%i): 0x%s", reg->name, (int)(reg->size), value);
2226 free(value);
2227 return ERROR_OK;
2230 /* set register value */
2231 if (CMD_ARGC == 2)
2233 uint8_t *buf = malloc(DIV_ROUND_UP(reg->size, 8));
2234 if (buf == NULL)
2235 return ERROR_FAIL;
2236 str_to_buf(CMD_ARGV[1], strlen(CMD_ARGV[1]), buf, reg->size, 0);
2238 reg->type->set(reg, buf);
2240 value = buf_to_str(reg->value, reg->size, 16);
2241 command_print(CMD_CTX, "%s (/%i): 0x%s", reg->name, (int)(reg->size), value);
2242 free(value);
2244 free(buf);
2246 return ERROR_OK;
2249 command_print(CMD_CTX, "usage: reg <#|name> [value]");
2251 return ERROR_OK;
2254 COMMAND_HANDLER(handle_poll_command)
2256 int retval = ERROR_OK;
2257 struct target *target = get_current_target(CMD_CTX);
2259 if (CMD_ARGC == 0)
2261 command_print(CMD_CTX, "background polling: %s",
2262 jtag_poll_get_enabled() ? "on" : "off");
2263 command_print(CMD_CTX, "TAP: %s (%s)",
2264 target->tap->dotted_name,
2265 target->tap->enabled ? "enabled" : "disabled");
2266 if (!target->tap->enabled)
2267 return ERROR_OK;
2268 if ((retval = target_poll(target)) != ERROR_OK)
2269 return retval;
2270 if ((retval = target_arch_state(target)) != ERROR_OK)
2271 return retval;
2273 else if (CMD_ARGC == 1)
2275 bool enable;
2276 COMMAND_PARSE_ON_OFF(CMD_ARGV[0], enable);
2277 jtag_poll_set_enabled(enable);
2279 else
2281 return ERROR_COMMAND_SYNTAX_ERROR;
2284 return retval;
2287 COMMAND_HANDLER(handle_wait_halt_command)
2289 if (CMD_ARGC > 1)
2290 return ERROR_COMMAND_SYNTAX_ERROR;
2292 unsigned ms = 5000;
2293 if (1 == CMD_ARGC)
2295 int retval = parse_uint(CMD_ARGV[0], &ms);
2296 if (ERROR_OK != retval)
2298 command_print(CMD_CTX, "usage: %s [seconds]", CMD_NAME);
2299 return ERROR_COMMAND_SYNTAX_ERROR;
2301 // convert seconds (given) to milliseconds (needed)
2302 ms *= 1000;
2305 struct target *target = get_current_target(CMD_CTX);
2306 return target_wait_state(target, TARGET_HALTED, ms);
2309 /* wait for target state to change. The trick here is to have a low
2310 * latency for short waits and not to suck up all the CPU time
2311 * on longer waits.
2313 * After 500ms, keep_alive() is invoked
2315 int target_wait_state(struct target *target, enum target_state state, int ms)
2317 int retval;
2318 long long then = 0, cur;
2319 int once = 1;
2321 for (;;)
2323 if ((retval = target_poll(target)) != ERROR_OK)
2324 return retval;
2325 if (target->state == state)
2327 break;
2329 cur = timeval_ms();
2330 if (once)
2332 once = 0;
2333 then = timeval_ms();
2334 LOG_DEBUG("waiting for target %s...",
2335 Jim_Nvp_value2name_simple(nvp_target_state,state)->name);
2338 if (cur-then > 500)
2340 keep_alive();
2343 if ((cur-then) > ms)
2345 LOG_ERROR("timed out while waiting for target %s",
2346 Jim_Nvp_value2name_simple(nvp_target_state,state)->name);
2347 return ERROR_FAIL;
2351 return ERROR_OK;
2354 COMMAND_HANDLER(handle_halt_command)
2356 LOG_DEBUG("-");
2358 struct target *target = get_current_target(CMD_CTX);
2359 int retval = target_halt(target);
2360 if (ERROR_OK != retval)
2361 return retval;
2363 if (CMD_ARGC == 1)
2365 unsigned wait_local;
2366 retval = parse_uint(CMD_ARGV[0], &wait_local);
2367 if (ERROR_OK != retval)
2368 return ERROR_COMMAND_SYNTAX_ERROR;
2369 if (!wait_local)
2370 return ERROR_OK;
2373 return CALL_COMMAND_HANDLER(handle_wait_halt_command);
2376 COMMAND_HANDLER(handle_soft_reset_halt_command)
2378 struct target *target = get_current_target(CMD_CTX);
2380 LOG_USER("requesting target halt and executing a soft reset");
2382 target->type->soft_reset_halt(target);
2384 return ERROR_OK;
2387 COMMAND_HANDLER(handle_reset_command)
2389 if (CMD_ARGC > 1)
2390 return ERROR_COMMAND_SYNTAX_ERROR;
2392 enum target_reset_mode reset_mode = RESET_RUN;
2393 if (CMD_ARGC == 1)
2395 const Jim_Nvp *n;
2396 n = Jim_Nvp_name2value_simple(nvp_reset_modes, CMD_ARGV[0]);
2397 if ((n->name == NULL) || (n->value == RESET_UNKNOWN)) {
2398 return ERROR_COMMAND_SYNTAX_ERROR;
2400 reset_mode = n->value;
2403 /* reset *all* targets */
2404 return target_process_reset(CMD_CTX, reset_mode);
2408 COMMAND_HANDLER(handle_resume_command)
2410 int current = 1;
2411 if (CMD_ARGC > 1)
2412 return ERROR_COMMAND_SYNTAX_ERROR;
2414 struct target *target = get_current_target(CMD_CTX);
2415 target_handle_event(target, TARGET_EVENT_OLD_pre_resume);
2417 /* with no CMD_ARGV, resume from current pc, addr = 0,
2418 * with one arguments, addr = CMD_ARGV[0],
2419 * handle breakpoints, not debugging */
2420 uint32_t addr = 0;
2421 if (CMD_ARGC == 1)
2423 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], addr);
2424 current = 0;
2427 return target_resume(target, current, addr, 1, 0);
2430 COMMAND_HANDLER(handle_step_command)
2432 if (CMD_ARGC > 1)
2433 return ERROR_COMMAND_SYNTAX_ERROR;
2435 LOG_DEBUG("-");
2437 /* with no CMD_ARGV, step from current pc, addr = 0,
2438 * with one argument addr = CMD_ARGV[0],
2439 * handle breakpoints, debugging */
2440 uint32_t addr = 0;
2441 int current_pc = 1;
2442 if (CMD_ARGC == 1)
2444 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], addr);
2445 current_pc = 0;
2448 struct target *target = get_current_target(CMD_CTX);
2450 return target->type->step(target, current_pc, addr, 1);
2453 static void handle_md_output(struct command_context *cmd_ctx,
2454 struct target *target, uint32_t address, unsigned size,
2455 unsigned count, const uint8_t *buffer)
2457 const unsigned line_bytecnt = 32;
2458 unsigned line_modulo = line_bytecnt / size;
2460 char output[line_bytecnt * 4 + 1];
2461 unsigned output_len = 0;
2463 const char *value_fmt;
2464 switch (size) {
2465 case 4: value_fmt = "%8.8x "; break;
2466 case 2: value_fmt = "%4.4x "; break;
2467 case 1: value_fmt = "%2.2x "; break;
2468 default:
2469 /* "can't happen", caller checked */
2470 LOG_ERROR("invalid memory read size: %u", size);
2471 return;
2474 for (unsigned i = 0; i < count; i++)
2476 if (i % line_modulo == 0)
2478 output_len += snprintf(output + output_len,
2479 sizeof(output) - output_len,
2480 "0x%8.8x: ",
2481 (unsigned)(address + (i*size)));
2484 uint32_t value = 0;
2485 const uint8_t *value_ptr = buffer + i * size;
2486 switch (size) {
2487 case 4: value = target_buffer_get_u32(target, value_ptr); break;
2488 case 2: value = target_buffer_get_u16(target, value_ptr); break;
2489 case 1: value = *value_ptr;
2491 output_len += snprintf(output + output_len,
2492 sizeof(output) - output_len,
2493 value_fmt, value);
2495 if ((i % line_modulo == line_modulo - 1) || (i == count - 1))
2497 command_print(cmd_ctx, "%s", output);
2498 output_len = 0;
2503 COMMAND_HANDLER(handle_md_command)
2505 if (CMD_ARGC < 1)
2506 return ERROR_COMMAND_SYNTAX_ERROR;
2508 unsigned size = 0;
2509 switch (CMD_NAME[2]) {
2510 case 'w': size = 4; break;
2511 case 'h': size = 2; break;
2512 case 'b': size = 1; break;
2513 default: return ERROR_COMMAND_SYNTAX_ERROR;
2516 bool physical=strcmp(CMD_ARGV[0], "phys")==0;
2517 int (*fn)(struct target *target,
2518 uint32_t address, uint32_t size_value, uint32_t count, uint8_t *buffer);
2519 if (physical)
2521 CMD_ARGC--;
2522 CMD_ARGV++;
2523 fn=target_read_phys_memory;
2524 } else
2526 fn=target_read_memory;
2528 if ((CMD_ARGC < 1) || (CMD_ARGC > 2))
2530 return ERROR_COMMAND_SYNTAX_ERROR;
2533 uint32_t address;
2534 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], address);
2536 unsigned count = 1;
2537 if (CMD_ARGC == 2)
2538 COMMAND_PARSE_NUMBER(uint, CMD_ARGV[1], count);
2540 uint8_t *buffer = calloc(count, size);
2542 struct target *target = get_current_target(CMD_CTX);
2543 int retval = fn(target, address, size, count, buffer);
2544 if (ERROR_OK == retval)
2545 handle_md_output(CMD_CTX, target, address, size, count, buffer);
2547 free(buffer);
2549 return retval;
2552 typedef int (*target_write_fn)(struct target *target,
2553 uint32_t address, uint32_t size, uint32_t count, const uint8_t *buffer);
2555 static int target_write_memory_fast(struct target *target,
2556 uint32_t address, uint32_t size, uint32_t count, const uint8_t *buffer)
2558 return target_write_buffer(target, address, size * count, buffer);
2561 static int target_fill_mem(struct target *target,
2562 uint32_t address,
2563 target_write_fn fn,
2564 unsigned data_size,
2565 /* value */
2566 uint32_t b,
2567 /* count */
2568 unsigned c)
2570 /* We have to write in reasonably large chunks to be able
2571 * to fill large memory areas with any sane speed */
2572 const unsigned chunk_size = 16384;
2573 uint8_t *target_buf = malloc(chunk_size * data_size);
2574 if (target_buf == NULL)
2576 LOG_ERROR("Out of memory");
2577 return ERROR_FAIL;
2580 for (unsigned i = 0; i < chunk_size; i ++)
2582 switch (data_size)
2584 case 4:
2585 target_buffer_set_u32(target, target_buf + i*data_size, b);
2586 break;
2587 case 2:
2588 target_buffer_set_u16(target, target_buf + i*data_size, b);
2589 break;
2590 case 1:
2591 target_buffer_set_u8(target, target_buf + i*data_size, b);
2592 break;
2593 default:
2594 exit(-1);
2598 int retval = ERROR_OK;
2600 for (unsigned x = 0; x < c; x += chunk_size)
2602 unsigned current;
2603 current = c - x;
2604 if (current > chunk_size)
2606 current = chunk_size;
2608 retval = fn(target, address + x * data_size, data_size, current, target_buf);
2609 if (retval != ERROR_OK)
2611 break;
2613 /* avoid GDB timeouts */
2614 keep_alive();
2616 free(target_buf);
2618 return retval;
2622 COMMAND_HANDLER(handle_mw_command)
2624 if (CMD_ARGC < 2)
2626 return ERROR_COMMAND_SYNTAX_ERROR;
2628 bool physical=strcmp(CMD_ARGV[0], "phys")==0;
2629 target_write_fn fn;
2630 if (physical)
2632 CMD_ARGC--;
2633 CMD_ARGV++;
2634 fn=target_write_phys_memory;
2635 } else
2637 fn = target_write_memory_fast;
2639 if ((CMD_ARGC < 2) || (CMD_ARGC > 3))
2640 return ERROR_COMMAND_SYNTAX_ERROR;
2642 uint32_t address;
2643 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], address);
2645 uint32_t value;
2646 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], value);
2648 unsigned count = 1;
2649 if (CMD_ARGC == 3)
2650 COMMAND_PARSE_NUMBER(uint, CMD_ARGV[2], count);
2652 struct target *target = get_current_target(CMD_CTX);
2653 unsigned wordsize;
2654 switch (CMD_NAME[2])
2656 case 'w':
2657 wordsize = 4;
2658 break;
2659 case 'h':
2660 wordsize = 2;
2661 break;
2662 case 'b':
2663 wordsize = 1;
2664 break;
2665 default:
2666 return ERROR_COMMAND_SYNTAX_ERROR;
2669 return target_fill_mem(target, address, fn, wordsize, value, count);
2672 static COMMAND_HELPER(parse_load_image_command_CMD_ARGV, struct image *image,
2673 uint32_t *min_address, uint32_t *max_address)
2675 if (CMD_ARGC < 1 || CMD_ARGC > 5)
2676 return ERROR_COMMAND_SYNTAX_ERROR;
2678 /* a base address isn't always necessary,
2679 * default to 0x0 (i.e. don't relocate) */
2680 if (CMD_ARGC >= 2)
2682 uint32_t addr;
2683 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], addr);
2684 image->base_address = addr;
2685 image->base_address_set = 1;
2687 else
2688 image->base_address_set = 0;
2690 image->start_address_set = 0;
2692 if (CMD_ARGC >= 4)
2694 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[3], *min_address);
2696 if (CMD_ARGC == 5)
2698 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[4], *max_address);
2699 // use size (given) to find max (required)
2700 *max_address += *min_address;
2703 if (*min_address > *max_address)
2704 return ERROR_COMMAND_SYNTAX_ERROR;
2706 return ERROR_OK;
2709 COMMAND_HANDLER(handle_load_image_command)
2711 uint8_t *buffer;
2712 size_t buf_cnt;
2713 uint32_t image_size;
2714 uint32_t min_address = 0;
2715 uint32_t max_address = 0xffffffff;
2716 int i;
2717 struct image image;
2719 int retval = CALL_COMMAND_HANDLER(parse_load_image_command_CMD_ARGV,
2720 &image, &min_address, &max_address);
2721 if (ERROR_OK != retval)
2722 return retval;
2724 struct target *target = get_current_target(CMD_CTX);
2726 struct duration bench;
2727 duration_start(&bench);
2729 if (image_open(&image, CMD_ARGV[0], (CMD_ARGC >= 3) ? CMD_ARGV[2] : NULL) != ERROR_OK)
2731 return ERROR_OK;
2734 image_size = 0x0;
2735 retval = ERROR_OK;
2736 for (i = 0; i < image.num_sections; i++)
2738 buffer = malloc(image.sections[i].size);
2739 if (buffer == NULL)
2741 command_print(CMD_CTX,
2742 "error allocating buffer for section (%d bytes)",
2743 (int)(image.sections[i].size));
2744 break;
2747 if ((retval = image_read_section(&image, i, 0x0, image.sections[i].size, buffer, &buf_cnt)) != ERROR_OK)
2749 free(buffer);
2750 break;
2753 uint32_t offset = 0;
2754 uint32_t length = buf_cnt;
2756 /* DANGER!!! beware of unsigned comparision here!!! */
2758 if ((image.sections[i].base_address + buf_cnt >= min_address)&&
2759 (image.sections[i].base_address < max_address))
2761 if (image.sections[i].base_address < min_address)
2763 /* clip addresses below */
2764 offset += min_address-image.sections[i].base_address;
2765 length -= offset;
2768 if (image.sections[i].base_address + buf_cnt > max_address)
2770 length -= (image.sections[i].base_address + buf_cnt)-max_address;
2773 if ((retval = target_write_buffer(target, image.sections[i].base_address + offset, length, buffer + offset)) != ERROR_OK)
2775 free(buffer);
2776 break;
2778 image_size += length;
2779 command_print(CMD_CTX, "%u bytes written at address 0x%8.8" PRIx32 "",
2780 (unsigned int)length,
2781 image.sections[i].base_address + offset);
2784 free(buffer);
2787 if ((ERROR_OK == retval) && (duration_measure(&bench) == ERROR_OK))
2789 command_print(CMD_CTX, "downloaded %" PRIu32 " bytes "
2790 "in %fs (%0.3f KiB/s)", image_size,
2791 duration_elapsed(&bench), duration_kbps(&bench, image_size));
2794 image_close(&image);
2796 return retval;
2800 COMMAND_HANDLER(handle_dump_image_command)
2802 struct fileio fileio;
2803 uint8_t buffer[560];
2804 int retval, retvaltemp;
2805 uint32_t address, size;
2806 struct duration bench;
2807 struct target *target = get_current_target(CMD_CTX);
2809 if (CMD_ARGC != 3)
2810 return ERROR_COMMAND_SYNTAX_ERROR;
2812 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], address);
2813 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[2], size);
2815 retval = fileio_open(&fileio, CMD_ARGV[0], FILEIO_WRITE, FILEIO_BINARY);
2816 if (retval != ERROR_OK)
2817 return retval;
2819 duration_start(&bench);
2821 retval = ERROR_OK;
2822 while (size > 0)
2824 size_t size_written;
2825 uint32_t this_run_size = (size > 560) ? 560 : size;
2826 retval = target_read_buffer(target, address, this_run_size, buffer);
2827 if (retval != ERROR_OK)
2829 break;
2832 retval = fileio_write(&fileio, this_run_size, buffer, &size_written);
2833 if (retval != ERROR_OK)
2835 break;
2838 size -= this_run_size;
2839 address += this_run_size;
2842 if ((ERROR_OK == retval) && (duration_measure(&bench) == ERROR_OK))
2844 int filesize;
2845 retval = fileio_size(&fileio, &filesize);
2846 if (retval != ERROR_OK)
2847 return retval;
2848 command_print(CMD_CTX,
2849 "dumped %ld bytes in %fs (%0.3f KiB/s)", (long)filesize,
2850 duration_elapsed(&bench), duration_kbps(&bench, filesize));
2853 if ((retvaltemp = fileio_close(&fileio)) != ERROR_OK)
2854 return retvaltemp;
2856 return retval;
2859 static COMMAND_HELPER(handle_verify_image_command_internal, int verify)
2861 uint8_t *buffer;
2862 size_t buf_cnt;
2863 uint32_t image_size;
2864 int i;
2865 int retval;
2866 uint32_t checksum = 0;
2867 uint32_t mem_checksum = 0;
2869 struct image image;
2871 struct target *target = get_current_target(CMD_CTX);
2873 if (CMD_ARGC < 1)
2875 return ERROR_COMMAND_SYNTAX_ERROR;
2878 if (!target)
2880 LOG_ERROR("no target selected");
2881 return ERROR_FAIL;
2884 struct duration bench;
2885 duration_start(&bench);
2887 if (CMD_ARGC >= 2)
2889 uint32_t addr;
2890 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], addr);
2891 image.base_address = addr;
2892 image.base_address_set = 1;
2894 else
2896 image.base_address_set = 0;
2897 image.base_address = 0x0;
2900 image.start_address_set = 0;
2902 if ((retval = image_open(&image, CMD_ARGV[0], (CMD_ARGC == 3) ? CMD_ARGV[2] : NULL)) != ERROR_OK)
2904 return retval;
2907 image_size = 0x0;
2908 int diffs = 0;
2909 retval = ERROR_OK;
2910 for (i = 0; i < image.num_sections; i++)
2912 buffer = malloc(image.sections[i].size);
2913 if (buffer == NULL)
2915 command_print(CMD_CTX,
2916 "error allocating buffer for section (%d bytes)",
2917 (int)(image.sections[i].size));
2918 break;
2920 if ((retval = image_read_section(&image, i, 0x0, image.sections[i].size, buffer, &buf_cnt)) != ERROR_OK)
2922 free(buffer);
2923 break;
2926 if (verify)
2928 /* calculate checksum of image */
2929 retval = image_calculate_checksum(buffer, buf_cnt, &checksum);
2930 if (retval != ERROR_OK)
2932 free(buffer);
2933 break;
2936 retval = target_checksum_memory(target, image.sections[i].base_address, buf_cnt, &mem_checksum);
2937 if (retval != ERROR_OK)
2939 free(buffer);
2940 break;
2943 if (checksum != mem_checksum)
2945 /* failed crc checksum, fall back to a binary compare */
2946 uint8_t *data;
2948 if (diffs == 0)
2950 LOG_ERROR("checksum mismatch - attempting binary compare");
2953 data = (uint8_t*)malloc(buf_cnt);
2955 /* Can we use 32bit word accesses? */
2956 int size = 1;
2957 int count = buf_cnt;
2958 if ((count % 4) == 0)
2960 size *= 4;
2961 count /= 4;
2963 retval = target_read_memory(target, image.sections[i].base_address, size, count, data);
2964 if (retval == ERROR_OK)
2966 uint32_t t;
2967 for (t = 0; t < buf_cnt; t++)
2969 if (data[t] != buffer[t])
2971 command_print(CMD_CTX,
2972 "diff %d address 0x%08x. Was 0x%02x instead of 0x%02x",
2973 diffs,
2974 (unsigned)(t + image.sections[i].base_address),
2975 data[t],
2976 buffer[t]);
2977 if (diffs++ >= 127)
2979 command_print(CMD_CTX, "More than 128 errors, the rest are not printed.");
2980 free(data);
2981 free(buffer);
2982 goto done;
2985 keep_alive();
2988 free(data);
2990 } else
2992 command_print(CMD_CTX, "address 0x%08" PRIx32 " length 0x%08zx",
2993 image.sections[i].base_address,
2994 buf_cnt);
2997 free(buffer);
2998 image_size += buf_cnt;
3000 if (diffs > 0)
3002 command_print(CMD_CTX, "No more differences found.");
3004 done:
3005 if (diffs > 0)
3007 retval = ERROR_FAIL;
3009 if ((ERROR_OK == retval) && (duration_measure(&bench) == ERROR_OK))
3011 command_print(CMD_CTX, "verified %" PRIu32 " bytes "
3012 "in %fs (%0.3f KiB/s)", image_size,
3013 duration_elapsed(&bench), duration_kbps(&bench, image_size));
3016 image_close(&image);
3018 return retval;
3021 COMMAND_HANDLER(handle_verify_image_command)
3023 return CALL_COMMAND_HANDLER(handle_verify_image_command_internal, 1);
3026 COMMAND_HANDLER(handle_test_image_command)
3028 return CALL_COMMAND_HANDLER(handle_verify_image_command_internal, 0);
3031 static int handle_bp_command_list(struct command_context *cmd_ctx)
3033 struct target *target = get_current_target(cmd_ctx);
3034 struct breakpoint *breakpoint = target->breakpoints;
3035 while (breakpoint)
3037 if (breakpoint->type == BKPT_SOFT)
3039 char* buf = buf_to_str(breakpoint->orig_instr,
3040 breakpoint->length, 16);
3041 command_print(cmd_ctx, "IVA breakpoint: 0x%8.8" PRIx32 ", 0x%x, %i, 0x%s",
3042 breakpoint->address,
3043 breakpoint->length,
3044 breakpoint->set, buf);
3045 free(buf);
3047 else
3049 if ((breakpoint->address == 0) && (breakpoint->asid != 0))
3050 command_print(cmd_ctx, "Context breakpoint: 0x%8.8" PRIx32 ", 0x%x, %i",
3051 breakpoint->asid,
3052 breakpoint->length, breakpoint->set);
3053 else if ((breakpoint->address != 0) && (breakpoint->asid != 0))
3055 command_print(cmd_ctx, "Hybrid breakpoint(IVA): 0x%8.8" PRIx32 ", 0x%x, %i",
3056 breakpoint->address,
3057 breakpoint->length, breakpoint->set);
3058 command_print(cmd_ctx, "\t|--->linked with ContextID: 0x%8.8" PRIx32,
3059 breakpoint->asid);
3061 else
3062 command_print(cmd_ctx, "Breakpoint(IVA): 0x%8.8" PRIx32 ", 0x%x, %i",
3063 breakpoint->address,
3064 breakpoint->length, breakpoint->set);
3067 breakpoint = breakpoint->next;
3069 return ERROR_OK;
3072 static int handle_bp_command_set(struct command_context *cmd_ctx,
3073 uint32_t addr, uint32_t asid, uint32_t length, int hw)
3075 struct target *target = get_current_target(cmd_ctx);
3077 if (asid == 0)
3079 int retval = breakpoint_add(target, addr, length, hw);
3080 if (ERROR_OK == retval)
3081 command_print(cmd_ctx, "breakpoint set at 0x%8.8" PRIx32 "", addr);
3082 else
3084 LOG_ERROR("Failure setting breakpoint, the same address(IVA) is already used");
3085 return retval;
3088 else if (addr == 0)
3090 int retval = context_breakpoint_add(target, asid, length, hw);
3091 if (ERROR_OK == retval)
3092 command_print(cmd_ctx, "Context breakpoint set at 0x%8.8" PRIx32 "", asid);
3093 else
3095 LOG_ERROR("Failure setting breakpoint, the same address(CONTEXTID) is already used");
3096 return retval;
3099 else
3101 int retval = hybrid_breakpoint_add(target, addr, asid, length, hw);
3102 if(ERROR_OK == retval)
3103 command_print(cmd_ctx, "Hybrid breakpoint set at 0x%8.8" PRIx32 "", asid);
3104 else
3106 LOG_ERROR("Failure setting breakpoint, the same address is already used");
3107 return retval;
3110 return ERROR_OK;
3113 COMMAND_HANDLER(handle_bp_command)
3115 uint32_t addr;
3116 uint32_t asid;
3117 uint32_t length;
3118 int hw = BKPT_SOFT;
3119 switch(CMD_ARGC)
3121 case 0:
3122 return handle_bp_command_list(CMD_CTX);
3124 case 2:
3125 asid = 0;
3126 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], addr);
3127 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], length);
3128 return handle_bp_command_set(CMD_CTX, addr, asid, length, hw);
3130 case 3:
3131 if(strcmp(CMD_ARGV[2], "hw") == 0)
3133 hw = BKPT_HARD;
3134 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], addr);
3136 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], length);
3138 asid = 0;
3139 return handle_bp_command_set(CMD_CTX, addr, asid, length, hw);
3141 else if(strcmp(CMD_ARGV[2], "hw_ctx") == 0)
3143 hw = BKPT_HARD;
3144 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], asid);
3145 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], length);
3146 addr = 0;
3147 return handle_bp_command_set(CMD_CTX, addr, asid, length, hw);
3150 case 4:
3151 hw = BKPT_HARD;
3152 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], addr);
3153 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], asid);
3154 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[2], length);
3155 return handle_bp_command_set(CMD_CTX, addr, asid, length, hw);
3157 default:
3158 command_print(CMD_CTX, "usage: bp <address> [<asid>]<length> ['hw'|'hw_ctx']");
3159 return ERROR_COMMAND_SYNTAX_ERROR;
3163 COMMAND_HANDLER(handle_rbp_command)
3165 if (CMD_ARGC != 1)
3166 return ERROR_COMMAND_SYNTAX_ERROR;
3168 uint32_t addr;
3169 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], addr);
3171 struct target *target = get_current_target(CMD_CTX);
3172 breakpoint_remove(target, addr);
3174 return ERROR_OK;
3177 COMMAND_HANDLER(handle_wp_command)
3179 struct target *target = get_current_target(CMD_CTX);
3181 if (CMD_ARGC == 0)
3183 struct watchpoint *watchpoint = target->watchpoints;
3185 while (watchpoint)
3187 command_print(CMD_CTX, "address: 0x%8.8" PRIx32
3188 ", len: 0x%8.8" PRIx32
3189 ", r/w/a: %i, value: 0x%8.8" PRIx32
3190 ", mask: 0x%8.8" PRIx32,
3191 watchpoint->address,
3192 watchpoint->length,
3193 (int)watchpoint->rw,
3194 watchpoint->value,
3195 watchpoint->mask);
3196 watchpoint = watchpoint->next;
3198 return ERROR_OK;
3201 enum watchpoint_rw type = WPT_ACCESS;
3202 uint32_t addr = 0;
3203 uint32_t length = 0;
3204 uint32_t data_value = 0x0;
3205 uint32_t data_mask = 0xffffffff;
3207 switch (CMD_ARGC)
3209 case 5:
3210 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[4], data_mask);
3211 // fall through
3212 case 4:
3213 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[3], data_value);
3214 // fall through
3215 case 3:
3216 switch (CMD_ARGV[2][0])
3218 case 'r':
3219 type = WPT_READ;
3220 break;
3221 case 'w':
3222 type = WPT_WRITE;
3223 break;
3224 case 'a':
3225 type = WPT_ACCESS;
3226 break;
3227 default:
3228 LOG_ERROR("invalid watchpoint mode ('%c')", CMD_ARGV[2][0]);
3229 return ERROR_COMMAND_SYNTAX_ERROR;
3231 // fall through
3232 case 2:
3233 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], length);
3234 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], addr);
3235 break;
3237 default:
3238 command_print(CMD_CTX, "usage: wp [address length "
3239 "[(r|w|a) [value [mask]]]]");
3240 return ERROR_COMMAND_SYNTAX_ERROR;
3243 int retval = watchpoint_add(target, addr, length, type,
3244 data_value, data_mask);
3245 if (ERROR_OK != retval)
3246 LOG_ERROR("Failure setting watchpoints");
3248 return retval;
3251 COMMAND_HANDLER(handle_rwp_command)
3253 if (CMD_ARGC != 1)
3254 return ERROR_COMMAND_SYNTAX_ERROR;
3256 uint32_t addr;
3257 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], addr);
3259 struct target *target = get_current_target(CMD_CTX);
3260 watchpoint_remove(target, addr);
3262 return ERROR_OK;
3267 * Translate a virtual address to a physical address.
3269 * The low-level target implementation must have logged a detailed error
3270 * which is forwarded to telnet/GDB session.
3272 COMMAND_HANDLER(handle_virt2phys_command)
3274 if (CMD_ARGC != 1)
3275 return ERROR_COMMAND_SYNTAX_ERROR;
3277 uint32_t va;
3278 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], va);
3279 uint32_t pa;
3281 struct target *target = get_current_target(CMD_CTX);
3282 int retval = target->type->virt2phys(target, va, &pa);
3283 if (retval == ERROR_OK)
3284 command_print(CMD_CTX, "Physical address 0x%08" PRIx32 "", pa);
3286 return retval;
3289 static void writeData(FILE *f, const void *data, size_t len)
3291 size_t written = fwrite(data, 1, len, f);
3292 if (written != len)
3293 LOG_ERROR("failed to write %zu bytes: %s", len, strerror(errno));
3296 static void writeLong(FILE *f, int l)
3298 int i;
3299 for (i = 0; i < 4; i++)
3301 char c = (l >> (i*8))&0xff;
3302 writeData(f, &c, 1);
3307 static void writeString(FILE *f, char *s)
3309 writeData(f, s, strlen(s));
3312 /* Dump a gmon.out histogram file. */
3313 static void writeGmon(uint32_t *samples, uint32_t sampleNum, const char *filename)
3315 uint32_t i;
3316 FILE *f = fopen(filename, "w");
3317 if (f == NULL)
3318 return;
3319 writeString(f, "gmon");
3320 writeLong(f, 0x00000001); /* Version */
3321 writeLong(f, 0); /* padding */
3322 writeLong(f, 0); /* padding */
3323 writeLong(f, 0); /* padding */
3325 uint8_t zero = 0; /* GMON_TAG_TIME_HIST */
3326 writeData(f, &zero, 1);
3328 /* figure out bucket size */
3329 uint32_t min = samples[0];
3330 uint32_t max = samples[0];
3331 for (i = 0; i < sampleNum; i++)
3333 if (min > samples[i])
3335 min = samples[i];
3337 if (max < samples[i])
3339 max = samples[i];
3343 int addressSpace = (max - min + 1);
3344 assert(addressSpace >= 2);
3346 static const uint32_t maxBuckets = 16 * 1024; /* maximum buckets. */
3347 uint32_t length = addressSpace;
3348 if (length > maxBuckets)
3350 length = maxBuckets;
3352 int *buckets = malloc(sizeof(int)*length);
3353 if (buckets == NULL)
3355 fclose(f);
3356 return;
3358 memset(buckets, 0, sizeof(int)*length);
3359 for (i = 0; i < sampleNum;i++)
3361 uint32_t address = samples[i];
3362 long long a = address-min;
3363 long long b = length-1;
3364 long long c = addressSpace-1;
3365 int index_t = (a*b)/c; /* danger!!!! int32 overflows */
3366 buckets[index_t]++;
3369 /* append binary memory gmon.out &profile_hist_hdr ((char*)&profile_hist_hdr + sizeof(struct gmon_hist_hdr)) */
3370 writeLong(f, min); /* low_pc */
3371 writeLong(f, max); /* high_pc */
3372 writeLong(f, length); /* # of samples */
3373 writeLong(f, 100); /* KLUDGE! We lie, ca. 100Hz best case. */
3374 writeString(f, "seconds");
3375 for (i = 0; i < (15-strlen("seconds")); i++)
3376 writeData(f, &zero, 1);
3377 writeString(f, "s");
3379 /*append binary memory gmon.out profile_hist_data (profile_hist_data + profile_hist_hdr.hist_size) */
3381 char *data = malloc(2*length);
3382 if (data != NULL)
3384 for (i = 0; i < length;i++)
3386 int val;
3387 val = buckets[i];
3388 if (val > 65535)
3390 val = 65535;
3392 data[i*2]=val&0xff;
3393 data[i*2 + 1]=(val >> 8)&0xff;
3395 free(buckets);
3396 writeData(f, data, length * 2);
3397 free(data);
3398 } else
3400 free(buckets);
3403 fclose(f);
3406 /* profiling samples the CPU PC as quickly as OpenOCD is able,
3407 * which will be used as a random sampling of PC */
3408 COMMAND_HANDLER(handle_profile_command)
3410 struct target *target = get_current_target(CMD_CTX);
3411 struct timeval timeout, now;
3413 gettimeofday(&timeout, NULL);
3414 if (CMD_ARGC != 2)
3416 return ERROR_COMMAND_SYNTAX_ERROR;
3418 unsigned offset;
3419 COMMAND_PARSE_NUMBER(uint, CMD_ARGV[0], offset);
3421 timeval_add_time(&timeout, offset, 0);
3424 * @todo: Some cores let us sample the PC without the
3425 * annoying halt/resume step; for example, ARMv7 PCSR.
3426 * Provide a way to use that more efficient mechanism.
3429 command_print(CMD_CTX, "Starting profiling. Halting and resuming the target as often as we can...");
3431 static const int maxSample = 10000;
3432 uint32_t *samples = malloc(sizeof(uint32_t)*maxSample);
3433 if (samples == NULL)
3434 return ERROR_OK;
3436 int numSamples = 0;
3437 /* hopefully it is safe to cache! We want to stop/restart as quickly as possible. */
3438 struct reg *reg = register_get_by_name(target->reg_cache, "pc", 1);
3440 int retval = ERROR_OK;
3441 for (;;)
3443 target_poll(target);
3444 if (target->state == TARGET_HALTED)
3446 uint32_t t=*((uint32_t *)reg->value);
3447 samples[numSamples++]=t;
3448 retval = target_resume(target, 1, 0, 0, 0); /* current pc, addr = 0, do not handle breakpoints, not debugging */
3449 target_poll(target);
3450 alive_sleep(10); /* sleep 10ms, i.e. <100 samples/second. */
3451 } else if (target->state == TARGET_RUNNING)
3453 /* We want to quickly sample the PC. */
3454 if ((retval = target_halt(target)) != ERROR_OK)
3456 free(samples);
3457 return retval;
3459 } else
3461 command_print(CMD_CTX, "Target not halted or running");
3462 retval = ERROR_OK;
3463 break;
3465 if (retval != ERROR_OK)
3467 break;
3470 gettimeofday(&now, NULL);
3471 if ((numSamples >= maxSample) || ((now.tv_sec >= timeout.tv_sec) && (now.tv_usec >= timeout.tv_usec)))
3473 command_print(CMD_CTX, "Profiling completed. %d samples.", numSamples);
3474 if ((retval = target_poll(target)) != ERROR_OK)
3476 free(samples);
3477 return retval;
3479 if (target->state == TARGET_HALTED)
3481 target_resume(target, 1, 0, 0, 0); /* current pc, addr = 0, do not handle breakpoints, not debugging */
3483 if ((retval = target_poll(target)) != ERROR_OK)
3485 free(samples);
3486 return retval;
3488 writeGmon(samples, numSamples, CMD_ARGV[1]);
3489 command_print(CMD_CTX, "Wrote %s", CMD_ARGV[1]);
3490 break;
3493 free(samples);
3495 return retval;
3498 static int new_int_array_element(Jim_Interp * interp, const char *varname, int idx, uint32_t val)
3500 char *namebuf;
3501 Jim_Obj *nameObjPtr, *valObjPtr;
3502 int result;
3504 namebuf = alloc_printf("%s(%d)", varname, idx);
3505 if (!namebuf)
3506 return JIM_ERR;
3508 nameObjPtr = Jim_NewStringObj(interp, namebuf, -1);
3509 valObjPtr = Jim_NewIntObj(interp, val);
3510 if (!nameObjPtr || !valObjPtr)
3512 free(namebuf);
3513 return JIM_ERR;
3516 Jim_IncrRefCount(nameObjPtr);
3517 Jim_IncrRefCount(valObjPtr);
3518 result = Jim_SetVariable(interp, nameObjPtr, valObjPtr);
3519 Jim_DecrRefCount(interp, nameObjPtr);
3520 Jim_DecrRefCount(interp, valObjPtr);
3521 free(namebuf);
3522 /* printf("%s(%d) <= 0%08x\n", varname, idx, val); */
3523 return result;
3526 static int jim_mem2array(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
3528 struct command_context *context;
3529 struct target *target;
3531 context = current_command_context(interp);
3532 assert (context != NULL);
3534 target = get_current_target(context);
3535 if (target == NULL)
3537 LOG_ERROR("mem2array: no current target");
3538 return JIM_ERR;
3541 return target_mem2array(interp, target, argc-1, argv + 1);
3544 static int target_mem2array(Jim_Interp *interp, struct target *target, int argc, Jim_Obj *const *argv)
3546 long l;
3547 uint32_t width;
3548 int len;
3549 uint32_t addr;
3550 uint32_t count;
3551 uint32_t v;
3552 const char *varname;
3553 int n, e, retval;
3554 uint32_t i;
3556 /* argv[1] = name of array to receive the data
3557 * argv[2] = desired width
3558 * argv[3] = memory address
3559 * argv[4] = count of times to read
3561 if (argc != 4) {
3562 Jim_WrongNumArgs(interp, 1, argv, "varname width addr nelems");
3563 return JIM_ERR;
3565 varname = Jim_GetString(argv[0], &len);
3566 /* given "foo" get space for worse case "foo(%d)" .. add 20 */
3568 e = Jim_GetLong(interp, argv[1], &l);
3569 width = l;
3570 if (e != JIM_OK) {
3571 return e;
3574 e = Jim_GetLong(interp, argv[2], &l);
3575 addr = l;
3576 if (e != JIM_OK) {
3577 return e;
3579 e = Jim_GetLong(interp, argv[3], &l);
3580 len = l;
3581 if (e != JIM_OK) {
3582 return e;
3584 switch (width) {
3585 case 8:
3586 width = 1;
3587 break;
3588 case 16:
3589 width = 2;
3590 break;
3591 case 32:
3592 width = 4;
3593 break;
3594 default:
3595 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3596 Jim_AppendStrings(interp, Jim_GetResult(interp), "Invalid width param, must be 8/16/32", NULL);
3597 return JIM_ERR;
3599 if (len == 0) {
3600 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3601 Jim_AppendStrings(interp, Jim_GetResult(interp), "mem2array: zero width read?", NULL);
3602 return JIM_ERR;
3604 if ((addr + (len * width)) < addr) {
3605 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3606 Jim_AppendStrings(interp, Jim_GetResult(interp), "mem2array: addr + len - wraps to zero?", NULL);
3607 return JIM_ERR;
3609 /* absurd transfer size? */
3610 if (len > 65536) {
3611 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3612 Jim_AppendStrings(interp, Jim_GetResult(interp), "mem2array: absurd > 64K item request", NULL);
3613 return JIM_ERR;
3616 if ((width == 1) ||
3617 ((width == 2) && ((addr & 1) == 0)) ||
3618 ((width == 4) && ((addr & 3) == 0))) {
3619 /* all is well */
3620 } else {
3621 char buf[100];
3622 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3623 sprintf(buf, "mem2array address: 0x%08" PRIx32 " is not aligned for %" PRId32 " byte reads",
3624 addr,
3625 width);
3626 Jim_AppendStrings(interp, Jim_GetResult(interp), buf , NULL);
3627 return JIM_ERR;
3630 /* Transfer loop */
3632 /* index counter */
3633 n = 0;
3635 size_t buffersize = 4096;
3636 uint8_t *buffer = malloc(buffersize);
3637 if (buffer == NULL)
3638 return JIM_ERR;
3640 /* assume ok */
3641 e = JIM_OK;
3642 while (len) {
3643 /* Slurp... in buffer size chunks */
3645 count = len; /* in objects.. */
3646 if (count > (buffersize/width)) {
3647 count = (buffersize/width);
3650 retval = target_read_memory(target, addr, width, count, buffer);
3651 if (retval != ERROR_OK) {
3652 /* BOO !*/
3653 LOG_ERROR("mem2array: Read @ 0x%08x, w=%d, cnt=%d, failed",
3654 (unsigned int)addr,
3655 (int)width,
3656 (int)count);
3657 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3658 Jim_AppendStrings(interp, Jim_GetResult(interp), "mem2array: cannot read memory", NULL);
3659 e = JIM_ERR;
3660 break;
3661 } else {
3662 v = 0; /* shut up gcc */
3663 for (i = 0 ;i < count ;i++, n++) {
3664 switch (width) {
3665 case 4:
3666 v = target_buffer_get_u32(target, &buffer[i*width]);
3667 break;
3668 case 2:
3669 v = target_buffer_get_u16(target, &buffer[i*width]);
3670 break;
3671 case 1:
3672 v = buffer[i] & 0x0ff;
3673 break;
3675 new_int_array_element(interp, varname, n, v);
3677 len -= count;
3681 free(buffer);
3683 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3685 return e;
3688 static int get_int_array_element(Jim_Interp * interp, const char *varname, int idx, uint32_t *val)
3690 char *namebuf;
3691 Jim_Obj *nameObjPtr, *valObjPtr;
3692 int result;
3693 long l;
3695 namebuf = alloc_printf("%s(%d)", varname, idx);
3696 if (!namebuf)
3697 return JIM_ERR;
3699 nameObjPtr = Jim_NewStringObj(interp, namebuf, -1);
3700 if (!nameObjPtr)
3702 free(namebuf);
3703 return JIM_ERR;
3706 Jim_IncrRefCount(nameObjPtr);
3707 valObjPtr = Jim_GetVariable(interp, nameObjPtr, JIM_ERRMSG);
3708 Jim_DecrRefCount(interp, nameObjPtr);
3709 free(namebuf);
3710 if (valObjPtr == NULL)
3711 return JIM_ERR;
3713 result = Jim_GetLong(interp, valObjPtr, &l);
3714 /* printf("%s(%d) => 0%08x\n", varname, idx, val); */
3715 *val = l;
3716 return result;
3719 static int jim_array2mem(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
3721 struct command_context *context;
3722 struct target *target;
3724 context = current_command_context(interp);
3725 assert (context != NULL);
3727 target = get_current_target(context);
3728 if (target == NULL) {
3729 LOG_ERROR("array2mem: no current target");
3730 return JIM_ERR;
3733 return target_array2mem(interp,target, argc-1, argv + 1);
3736 static int target_array2mem(Jim_Interp *interp, struct target *target,
3737 int argc, Jim_Obj *const *argv)
3739 long l;
3740 uint32_t width;
3741 int len;
3742 uint32_t addr;
3743 uint32_t count;
3744 uint32_t v;
3745 const char *varname;
3746 int n, e, retval;
3747 uint32_t i;
3749 /* argv[1] = name of array to get the data
3750 * argv[2] = desired width
3751 * argv[3] = memory address
3752 * argv[4] = count to write
3754 if (argc != 4) {
3755 Jim_WrongNumArgs(interp, 0, argv, "varname width addr nelems");
3756 return JIM_ERR;
3758 varname = Jim_GetString(argv[0], &len);
3759 /* given "foo" get space for worse case "foo(%d)" .. add 20 */
3761 e = Jim_GetLong(interp, argv[1], &l);
3762 width = l;
3763 if (e != JIM_OK) {
3764 return e;
3767 e = Jim_GetLong(interp, argv[2], &l);
3768 addr = l;
3769 if (e != JIM_OK) {
3770 return e;
3772 e = Jim_GetLong(interp, argv[3], &l);
3773 len = l;
3774 if (e != JIM_OK) {
3775 return e;
3777 switch (width) {
3778 case 8:
3779 width = 1;
3780 break;
3781 case 16:
3782 width = 2;
3783 break;
3784 case 32:
3785 width = 4;
3786 break;
3787 default:
3788 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3789 Jim_AppendStrings(interp, Jim_GetResult(interp), "Invalid width param, must be 8/16/32", NULL);
3790 return JIM_ERR;
3792 if (len == 0) {
3793 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3794 Jim_AppendStrings(interp, Jim_GetResult(interp), "array2mem: zero width read?", NULL);
3795 return JIM_ERR;
3797 if ((addr + (len * width)) < addr) {
3798 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3799 Jim_AppendStrings(interp, Jim_GetResult(interp), "array2mem: addr + len - wraps to zero?", NULL);
3800 return JIM_ERR;
3802 /* absurd transfer size? */
3803 if (len > 65536) {
3804 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3805 Jim_AppendStrings(interp, Jim_GetResult(interp), "array2mem: absurd > 64K item request", NULL);
3806 return JIM_ERR;
3809 if ((width == 1) ||
3810 ((width == 2) && ((addr & 1) == 0)) ||
3811 ((width == 4) && ((addr & 3) == 0))) {
3812 /* all is well */
3813 } else {
3814 char buf[100];
3815 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3816 sprintf(buf, "array2mem address: 0x%08x is not aligned for %d byte reads",
3817 (unsigned int)addr,
3818 (int)width);
3819 Jim_AppendStrings(interp, Jim_GetResult(interp), buf , NULL);
3820 return JIM_ERR;
3823 /* Transfer loop */
3825 /* index counter */
3826 n = 0;
3827 /* assume ok */
3828 e = JIM_OK;
3830 size_t buffersize = 4096;
3831 uint8_t *buffer = malloc(buffersize);
3832 if (buffer == NULL)
3833 return JIM_ERR;
3835 while (len) {
3836 /* Slurp... in buffer size chunks */
3838 count = len; /* in objects.. */
3839 if (count > (buffersize/width)) {
3840 count = (buffersize/width);
3843 v = 0; /* shut up gcc */
3844 for (i = 0 ;i < count ;i++, n++) {
3845 get_int_array_element(interp, varname, n, &v);
3846 switch (width) {
3847 case 4:
3848 target_buffer_set_u32(target, &buffer[i*width], v);
3849 break;
3850 case 2:
3851 target_buffer_set_u16(target, &buffer[i*width], v);
3852 break;
3853 case 1:
3854 buffer[i] = v & 0x0ff;
3855 break;
3858 len -= count;
3860 retval = target_write_memory(target, addr, width, count, buffer);
3861 if (retval != ERROR_OK) {
3862 /* BOO !*/
3863 LOG_ERROR("array2mem: Write @ 0x%08x, w=%d, cnt=%d, failed",
3864 (unsigned int)addr,
3865 (int)width,
3866 (int)count);
3867 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3868 Jim_AppendStrings(interp, Jim_GetResult(interp), "array2mem: cannot read memory", NULL);
3869 e = JIM_ERR;
3870 break;
3874 free(buffer);
3876 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3878 return e;
3881 /* FIX? should we propagate errors here rather than printing them
3882 * and continuing?
3884 void target_handle_event(struct target *target, enum target_event e)
3886 struct target_event_action *teap;
3888 for (teap = target->event_action; teap != NULL; teap = teap->next) {
3889 if (teap->event == e) {
3890 LOG_DEBUG("target: (%d) %s (%s) event: %d (%s) action: %s",
3891 target->target_number,
3892 target_name(target),
3893 target_type_name(target),
3895 Jim_Nvp_value2name_simple(nvp_target_event, e)->name,
3896 Jim_GetString(teap->body, NULL));
3897 if (Jim_EvalObj(teap->interp, teap->body) != JIM_OK)
3899 Jim_MakeErrorMessage(teap->interp);
3900 command_print(NULL,"%s\n", Jim_GetString(Jim_GetResult(teap->interp), NULL));
3907 * Returns true only if the target has a handler for the specified event.
3909 bool target_has_event_action(struct target *target, enum target_event event)
3911 struct target_event_action *teap;
3913 for (teap = target->event_action; teap != NULL; teap = teap->next) {
3914 if (teap->event == event)
3915 return true;
3917 return false;
3920 enum target_cfg_param {
3921 TCFG_TYPE,
3922 TCFG_EVENT,
3923 TCFG_WORK_AREA_VIRT,
3924 TCFG_WORK_AREA_PHYS,
3925 TCFG_WORK_AREA_SIZE,
3926 TCFG_WORK_AREA_BACKUP,
3927 TCFG_ENDIAN,
3928 TCFG_VARIANT,
3929 TCFG_COREID,
3930 TCFG_CHAIN_POSITION,
3931 TCFG_DBGBASE,
3932 TCFG_RTOS,
3935 static Jim_Nvp nvp_config_opts[] = {
3936 { .name = "-type", .value = TCFG_TYPE },
3937 { .name = "-event", .value = TCFG_EVENT },
3938 { .name = "-work-area-virt", .value = TCFG_WORK_AREA_VIRT },
3939 { .name = "-work-area-phys", .value = TCFG_WORK_AREA_PHYS },
3940 { .name = "-work-area-size", .value = TCFG_WORK_AREA_SIZE },
3941 { .name = "-work-area-backup", .value = TCFG_WORK_AREA_BACKUP },
3942 { .name = "-endian" , .value = TCFG_ENDIAN },
3943 { .name = "-variant", .value = TCFG_VARIANT },
3944 { .name = "-coreid", .value = TCFG_COREID },
3945 { .name = "-chain-position", .value = TCFG_CHAIN_POSITION },
3946 { .name = "-dbgbase", .value = TCFG_DBGBASE },
3947 { .name = "-rtos", .value = TCFG_RTOS },
3948 { .name = NULL, .value = -1 }
3951 static int target_configure(Jim_GetOptInfo *goi, struct target *target)
3953 Jim_Nvp *n;
3954 Jim_Obj *o;
3955 jim_wide w;
3956 char *cp;
3957 int e;
3959 /* parse config or cget options ... */
3960 while (goi->argc > 0) {
3961 Jim_SetEmptyResult(goi->interp);
3962 /* Jim_GetOpt_Debug(goi); */
3964 if (target->type->target_jim_configure) {
3965 /* target defines a configure function */
3966 /* target gets first dibs on parameters */
3967 e = (*(target->type->target_jim_configure))(target, goi);
3968 if (e == JIM_OK) {
3969 /* more? */
3970 continue;
3972 if (e == JIM_ERR) {
3973 /* An error */
3974 return e;
3976 /* otherwise we 'continue' below */
3978 e = Jim_GetOpt_Nvp(goi, nvp_config_opts, &n);
3979 if (e != JIM_OK) {
3980 Jim_GetOpt_NvpUnknown(goi, nvp_config_opts, 0);
3981 return e;
3983 switch (n->value) {
3984 case TCFG_TYPE:
3985 /* not setable */
3986 if (goi->isconfigure) {
3987 Jim_SetResultFormatted(goi->interp,
3988 "not settable: %s", n->name);
3989 return JIM_ERR;
3990 } else {
3991 no_params:
3992 if (goi->argc != 0) {
3993 Jim_WrongNumArgs(goi->interp,
3994 goi->argc, goi->argv,
3995 "NO PARAMS");
3996 return JIM_ERR;
3999 Jim_SetResultString(goi->interp,
4000 target_type_name(target), -1);
4001 /* loop for more */
4002 break;
4003 case TCFG_EVENT:
4004 if (goi->argc == 0) {
4005 Jim_WrongNumArgs(goi->interp, goi->argc, goi->argv, "-event ?event-name? ...");
4006 return JIM_ERR;
4009 e = Jim_GetOpt_Nvp(goi, nvp_target_event, &n);
4010 if (e != JIM_OK) {
4011 Jim_GetOpt_NvpUnknown(goi, nvp_target_event, 1);
4012 return e;
4015 if (goi->isconfigure) {
4016 if (goi->argc != 1) {
4017 Jim_WrongNumArgs(goi->interp, goi->argc, goi->argv, "-event ?event-name? ?EVENT-BODY?");
4018 return JIM_ERR;
4020 } else {
4021 if (goi->argc != 0) {
4022 Jim_WrongNumArgs(goi->interp, goi->argc, goi->argv, "-event ?event-name?");
4023 return JIM_ERR;
4028 struct target_event_action *teap;
4030 teap = target->event_action;
4031 /* replace existing? */
4032 while (teap) {
4033 if (teap->event == (enum target_event)n->value) {
4034 break;
4036 teap = teap->next;
4039 if (goi->isconfigure) {
4040 bool replace = true;
4041 if (teap == NULL) {
4042 /* create new */
4043 teap = calloc(1, sizeof(*teap));
4044 replace = false;
4046 teap->event = n->value;
4047 teap->interp = goi->interp;
4048 Jim_GetOpt_Obj(goi, &o);
4049 if (teap->body) {
4050 Jim_DecrRefCount(teap->interp, teap->body);
4052 teap->body = Jim_DuplicateObj(goi->interp, o);
4054 * FIXME:
4055 * Tcl/TK - "tk events" have a nice feature.
4056 * See the "BIND" command.
4057 * We should support that here.
4058 * You can specify %X and %Y in the event code.
4059 * The idea is: %T - target name.
4060 * The idea is: %N - target number
4061 * The idea is: %E - event name.
4063 Jim_IncrRefCount(teap->body);
4065 if (!replace)
4067 /* add to head of event list */
4068 teap->next = target->event_action;
4069 target->event_action = teap;
4071 Jim_SetEmptyResult(goi->interp);
4072 } else {
4073 /* get */
4074 if (teap == NULL) {
4075 Jim_SetEmptyResult(goi->interp);
4076 } else {
4077 Jim_SetResult(goi->interp, Jim_DuplicateObj(goi->interp, teap->body));
4081 /* loop for more */
4082 break;
4084 case TCFG_WORK_AREA_VIRT:
4085 if (goi->isconfigure) {
4086 target_free_all_working_areas(target);
4087 e = Jim_GetOpt_Wide(goi, &w);
4088 if (e != JIM_OK) {
4089 return e;
4091 target->working_area_virt = w;
4092 target->working_area_virt_spec = true;
4093 } else {
4094 if (goi->argc != 0) {
4095 goto no_params;
4098 Jim_SetResult(goi->interp, Jim_NewIntObj(goi->interp, target->working_area_virt));
4099 /* loop for more */
4100 break;
4102 case TCFG_WORK_AREA_PHYS:
4103 if (goi->isconfigure) {
4104 target_free_all_working_areas(target);
4105 e = Jim_GetOpt_Wide(goi, &w);
4106 if (e != JIM_OK) {
4107 return e;
4109 target->working_area_phys = w;
4110 target->working_area_phys_spec = true;
4111 } else {
4112 if (goi->argc != 0) {
4113 goto no_params;
4116 Jim_SetResult(goi->interp, Jim_NewIntObj(goi->interp, target->working_area_phys));
4117 /* loop for more */
4118 break;
4120 case TCFG_WORK_AREA_SIZE:
4121 if (goi->isconfigure) {
4122 target_free_all_working_areas(target);
4123 e = Jim_GetOpt_Wide(goi, &w);
4124 if (e != JIM_OK) {
4125 return e;
4127 target->working_area_size = w;
4128 } else {
4129 if (goi->argc != 0) {
4130 goto no_params;
4133 Jim_SetResult(goi->interp, Jim_NewIntObj(goi->interp, target->working_area_size));
4134 /* loop for more */
4135 break;
4137 case TCFG_WORK_AREA_BACKUP:
4138 if (goi->isconfigure) {
4139 target_free_all_working_areas(target);
4140 e = Jim_GetOpt_Wide(goi, &w);
4141 if (e != JIM_OK) {
4142 return e;
4144 /* make this exactly 1 or 0 */
4145 target->backup_working_area = (!!w);
4146 } else {
4147 if (goi->argc != 0) {
4148 goto no_params;
4151 Jim_SetResult(goi->interp, Jim_NewIntObj(goi->interp, target->backup_working_area));
4152 /* loop for more e*/
4153 break;
4156 case TCFG_ENDIAN:
4157 if (goi->isconfigure) {
4158 e = Jim_GetOpt_Nvp(goi, nvp_target_endian, &n);
4159 if (e != JIM_OK) {
4160 Jim_GetOpt_NvpUnknown(goi, nvp_target_endian, 1);
4161 return e;
4163 target->endianness = n->value;
4164 } else {
4165 if (goi->argc != 0) {
4166 goto no_params;
4169 n = Jim_Nvp_value2name_simple(nvp_target_endian, target->endianness);
4170 if (n->name == NULL) {
4171 target->endianness = TARGET_LITTLE_ENDIAN;
4172 n = Jim_Nvp_value2name_simple(nvp_target_endian, target->endianness);
4174 Jim_SetResultString(goi->interp, n->name, -1);
4175 /* loop for more */
4176 break;
4178 case TCFG_VARIANT:
4179 if (goi->isconfigure) {
4180 if (goi->argc < 1) {
4181 Jim_SetResultFormatted(goi->interp,
4182 "%s ?STRING?",
4183 n->name);
4184 return JIM_ERR;
4186 if (target->variant) {
4187 free((void *)(target->variant));
4189 e = Jim_GetOpt_String(goi, &cp, NULL);
4190 if (e != JIM_OK)
4191 return e;
4192 target->variant = strdup(cp);
4193 } else {
4194 if (goi->argc != 0) {
4195 goto no_params;
4198 Jim_SetResultString(goi->interp, target->variant,-1);
4199 /* loop for more */
4200 break;
4202 case TCFG_COREID:
4203 if (goi->isconfigure) {
4204 e = Jim_GetOpt_Wide(goi, &w);
4205 if (e != JIM_OK) {
4206 return e;
4208 target->coreid = (int32_t)w;
4209 } else {
4210 if (goi->argc != 0) {
4211 goto no_params;
4214 Jim_SetResult(goi->interp, Jim_NewIntObj(goi->interp, target->working_area_size));
4215 /* loop for more */
4216 break;
4218 case TCFG_CHAIN_POSITION:
4219 if (goi->isconfigure) {
4220 Jim_Obj *o_t;
4221 struct jtag_tap *tap;
4222 target_free_all_working_areas(target);
4223 e = Jim_GetOpt_Obj(goi, &o_t);
4224 if (e != JIM_OK) {
4225 return e;
4227 tap = jtag_tap_by_jim_obj(goi->interp, o_t);
4228 if (tap == NULL) {
4229 return JIM_ERR;
4231 /* make this exactly 1 or 0 */
4232 target->tap = tap;
4233 } else {
4234 if (goi->argc != 0) {
4235 goto no_params;
4238 Jim_SetResultString(goi->interp, target->tap->dotted_name, -1);
4239 /* loop for more e*/
4240 break;
4241 case TCFG_DBGBASE:
4242 if (goi->isconfigure) {
4243 e = Jim_GetOpt_Wide(goi, &w);
4244 if (e != JIM_OK) {
4245 return e;
4247 target->dbgbase = (uint32_t)w;
4248 target->dbgbase_set = true;
4249 } else {
4250 if (goi->argc != 0) {
4251 goto no_params;
4254 Jim_SetResult(goi->interp, Jim_NewIntObj(goi->interp, target->dbgbase));
4255 /* loop for more */
4256 break;
4258 case TCFG_RTOS:
4259 /* RTOS */
4261 int result = rtos_create( goi, target );
4262 if ( result != JIM_OK )
4264 return result;
4267 /* loop for more */
4268 break;
4270 } /* while (goi->argc) */
4273 /* done - we return */
4274 return JIM_OK;
4277 static int
4278 jim_target_configure(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
4280 Jim_GetOptInfo goi;
4282 Jim_GetOpt_Setup(&goi, interp, argc - 1, argv + 1);
4283 goi.isconfigure = !strcmp(Jim_GetString(argv[0], NULL), "configure");
4284 int need_args = 1 + goi.isconfigure;
4285 if (goi.argc < need_args)
4287 Jim_WrongNumArgs(goi.interp, goi.argc, goi.argv,
4288 goi.isconfigure
4289 ? "missing: -option VALUE ..."
4290 : "missing: -option ...");
4291 return JIM_ERR;
4293 struct target *target = Jim_CmdPrivData(goi.interp);
4294 return target_configure(&goi, target);
4297 static int jim_target_mw(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
4299 const char *cmd_name = Jim_GetString(argv[0], NULL);
4301 Jim_GetOptInfo goi;
4302 Jim_GetOpt_Setup(&goi, interp, argc - 1, argv + 1);
4304 if (goi.argc < 2 || goi.argc > 4)
4306 Jim_SetResultFormatted(goi.interp,
4307 "usage: %s [phys] <address> <data> [<count>]", cmd_name);
4308 return JIM_ERR;
4311 target_write_fn fn;
4312 fn = target_write_memory_fast;
4314 int e;
4315 if (strcmp(Jim_GetString(argv[1], NULL), "phys") == 0)
4317 /* consume it */
4318 struct Jim_Obj *obj;
4319 e = Jim_GetOpt_Obj(&goi, &obj);
4320 if (e != JIM_OK)
4321 return e;
4323 fn = target_write_phys_memory;
4326 jim_wide a;
4327 e = Jim_GetOpt_Wide(&goi, &a);
4328 if (e != JIM_OK)
4329 return e;
4331 jim_wide b;
4332 e = Jim_GetOpt_Wide(&goi, &b);
4333 if (e != JIM_OK)
4334 return e;
4336 jim_wide c = 1;
4337 if (goi.argc == 1)
4339 e = Jim_GetOpt_Wide(&goi, &c);
4340 if (e != JIM_OK)
4341 return e;
4344 /* all args must be consumed */
4345 if (goi.argc != 0)
4347 return JIM_ERR;
4350 struct target *target = Jim_CmdPrivData(goi.interp);
4351 unsigned data_size;
4352 if (strcasecmp(cmd_name, "mww") == 0) {
4353 data_size = 4;
4355 else if (strcasecmp(cmd_name, "mwh") == 0) {
4356 data_size = 2;
4358 else if (strcasecmp(cmd_name, "mwb") == 0) {
4359 data_size = 1;
4360 } else {
4361 LOG_ERROR("command '%s' unknown: ", cmd_name);
4362 return JIM_ERR;
4365 return (target_fill_mem(target, a, fn, data_size, b, c) == ERROR_OK) ? JIM_OK : JIM_ERR;
4368 static int jim_target_md(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
4370 const char *cmd_name = Jim_GetString(argv[0], NULL);
4372 Jim_GetOptInfo goi;
4373 Jim_GetOpt_Setup(&goi, interp, argc - 1, argv + 1);
4375 if ((goi.argc < 1) || (goi.argc > 3))
4377 Jim_SetResultFormatted(goi.interp,
4378 "usage: %s [phys] <address> [<count>]", cmd_name);
4379 return JIM_ERR;
4382 int (*fn)(struct target *target,
4383 uint32_t address, uint32_t size, uint32_t count, uint8_t *buffer);
4384 fn=target_read_memory;
4386 int e;
4387 if (strcmp(Jim_GetString(argv[1], NULL), "phys") == 0)
4389 /* consume it */
4390 struct Jim_Obj *obj;
4391 e = Jim_GetOpt_Obj(&goi, &obj);
4392 if (e != JIM_OK)
4393 return e;
4395 fn=target_read_phys_memory;
4398 jim_wide a;
4399 e = Jim_GetOpt_Wide(&goi, &a);
4400 if (e != JIM_OK) {
4401 return JIM_ERR;
4403 jim_wide c;
4404 if (goi.argc == 1) {
4405 e = Jim_GetOpt_Wide(&goi, &c);
4406 if (e != JIM_OK) {
4407 return JIM_ERR;
4409 } else {
4410 c = 1;
4413 /* all args must be consumed */
4414 if (goi.argc != 0)
4416 return JIM_ERR;
4419 jim_wide b = 1; /* shut up gcc */
4420 if (strcasecmp(cmd_name, "mdw") == 0)
4421 b = 4;
4422 else if (strcasecmp(cmd_name, "mdh") == 0)
4423 b = 2;
4424 else if (strcasecmp(cmd_name, "mdb") == 0)
4425 b = 1;
4426 else {
4427 LOG_ERROR("command '%s' unknown: ", cmd_name);
4428 return JIM_ERR;
4431 /* convert count to "bytes" */
4432 c = c * b;
4434 struct target *target = Jim_CmdPrivData(goi.interp);
4435 uint8_t target_buf[32];
4436 jim_wide x, y, z;
4437 while (c > 0) {
4438 y = c;
4439 if (y > 16) {
4440 y = 16;
4442 e = fn(target, a, b, y / b, target_buf);
4443 if (e != ERROR_OK) {
4444 char tmp[10];
4445 snprintf(tmp, sizeof(tmp), "%08lx", (long)a);
4446 Jim_SetResultFormatted(interp, "error reading target @ 0x%s", tmp);
4447 return JIM_ERR;
4450 command_print(NULL, "0x%08x ", (int)(a));
4451 switch (b) {
4452 case 4:
4453 for (x = 0; x < 16 && x < y; x += 4)
4455 z = target_buffer_get_u32(target, &(target_buf[ x ]));
4456 command_print(NULL, "%08x ", (int)(z));
4458 for (; (x < 16) ; x += 4) {
4459 command_print(NULL, " ");
4461 break;
4462 case 2:
4463 for (x = 0; x < 16 && x < y; x += 2)
4465 z = target_buffer_get_u16(target, &(target_buf[ x ]));
4466 command_print(NULL, "%04x ", (int)(z));
4468 for (; (x < 16) ; x += 2) {
4469 command_print(NULL, " ");
4471 break;
4472 case 1:
4473 default:
4474 for (x = 0 ; (x < 16) && (x < y) ; x += 1) {
4475 z = target_buffer_get_u8(target, &(target_buf[ x ]));
4476 command_print(NULL, "%02x ", (int)(z));
4478 for (; (x < 16) ; x += 1) {
4479 command_print(NULL, " ");
4481 break;
4483 /* ascii-ify the bytes */
4484 for (x = 0 ; x < y ; x++) {
4485 if ((target_buf[x] >= 0x20) &&
4486 (target_buf[x] <= 0x7e)) {
4487 /* good */
4488 } else {
4489 /* smack it */
4490 target_buf[x] = '.';
4493 /* space pad */
4494 while (x < 16) {
4495 target_buf[x] = ' ';
4496 x++;
4498 /* terminate */
4499 target_buf[16] = 0;
4500 /* print - with a newline */
4501 command_print(NULL, "%s\n", target_buf);
4502 /* NEXT... */
4503 c -= 16;
4504 a += 16;
4506 return JIM_OK;
4509 static int jim_target_mem2array(Jim_Interp *interp,
4510 int argc, Jim_Obj *const *argv)
4512 struct target *target = Jim_CmdPrivData(interp);
4513 return target_mem2array(interp, target, argc - 1, argv + 1);
4516 static int jim_target_array2mem(Jim_Interp *interp,
4517 int argc, Jim_Obj *const *argv)
4519 struct target *target = Jim_CmdPrivData(interp);
4520 return target_array2mem(interp, target, argc - 1, argv + 1);
4523 static int jim_target_tap_disabled(Jim_Interp *interp)
4525 Jim_SetResultFormatted(interp, "[TAP is disabled]");
4526 return JIM_ERR;
4529 static int jim_target_examine(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
4531 if (argc != 1)
4533 Jim_WrongNumArgs(interp, 1, argv, "[no parameters]");
4534 return JIM_ERR;
4536 struct target *target = Jim_CmdPrivData(interp);
4537 if (!target->tap->enabled)
4538 return jim_target_tap_disabled(interp);
4540 int e = target->type->examine(target);
4541 if (e != ERROR_OK)
4543 return JIM_ERR;
4545 return JIM_OK;
4548 static int jim_target_halt_gdb(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
4550 if (argc != 1)
4552 Jim_WrongNumArgs(interp, 1, argv, "[no parameters]");
4553 return JIM_ERR;
4555 struct target *target = Jim_CmdPrivData(interp);
4557 if (target_call_event_callbacks(target, TARGET_EVENT_GDB_HALT) != ERROR_OK)
4558 return JIM_ERR;
4560 return JIM_OK;
4563 static int jim_target_poll(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
4565 if (argc != 1)
4567 Jim_WrongNumArgs(interp, 1, argv, "[no parameters]");
4568 return JIM_ERR;
4570 struct target *target = Jim_CmdPrivData(interp);
4571 if (!target->tap->enabled)
4572 return jim_target_tap_disabled(interp);
4574 int e;
4575 if (!(target_was_examined(target))) {
4576 e = ERROR_TARGET_NOT_EXAMINED;
4577 } else {
4578 e = target->type->poll(target);
4580 if (e != ERROR_OK)
4582 return JIM_ERR;
4584 return JIM_OK;
4587 static int jim_target_reset(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
4589 Jim_GetOptInfo goi;
4590 Jim_GetOpt_Setup(&goi, interp, argc - 1, argv + 1);
4592 if (goi.argc != 2)
4594 Jim_WrongNumArgs(interp, 0, argv,
4595 "([tT]|[fF]|assert|deassert) BOOL");
4596 return JIM_ERR;
4599 Jim_Nvp *n;
4600 int e = Jim_GetOpt_Nvp(&goi, nvp_assert, &n);
4601 if (e != JIM_OK)
4603 Jim_GetOpt_NvpUnknown(&goi, nvp_assert, 1);
4604 return e;
4606 /* the halt or not param */
4607 jim_wide a;
4608 e = Jim_GetOpt_Wide(&goi, &a);
4609 if (e != JIM_OK)
4610 return e;
4612 struct target *target = Jim_CmdPrivData(goi.interp);
4613 if (!target->tap->enabled)
4614 return jim_target_tap_disabled(interp);
4615 if (!(target_was_examined(target)))
4617 LOG_ERROR("Target not examined yet");
4618 return ERROR_TARGET_NOT_EXAMINED;
4620 if (!target->type->assert_reset || !target->type->deassert_reset)
4622 Jim_SetResultFormatted(interp,
4623 "No target-specific reset for %s",
4624 target_name(target));
4625 return JIM_ERR;
4627 /* determine if we should halt or not. */
4628 target->reset_halt = !!a;
4629 /* When this happens - all workareas are invalid. */
4630 target_free_all_working_areas_restore(target, 0);
4632 /* do the assert */
4633 if (n->value == NVP_ASSERT) {
4634 e = target->type->assert_reset(target);
4635 } else {
4636 e = target->type->deassert_reset(target);
4638 return (e == ERROR_OK) ? JIM_OK : JIM_ERR;
4641 static int jim_target_halt(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
4643 if (argc != 1) {
4644 Jim_WrongNumArgs(interp, 1, argv, "[no parameters]");
4645 return JIM_ERR;
4647 struct target *target = Jim_CmdPrivData(interp);
4648 if (!target->tap->enabled)
4649 return jim_target_tap_disabled(interp);
4650 int e = target->type->halt(target);
4651 return (e == ERROR_OK) ? JIM_OK : JIM_ERR;
4654 static int jim_target_wait_state(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
4656 Jim_GetOptInfo goi;
4657 Jim_GetOpt_Setup(&goi, interp, argc - 1, argv + 1);
4659 /* params: <name> statename timeoutmsecs */
4660 if (goi.argc != 2)
4662 const char *cmd_name = Jim_GetString(argv[0], NULL);
4663 Jim_SetResultFormatted(goi.interp,
4664 "%s <state_name> <timeout_in_msec>", cmd_name);
4665 return JIM_ERR;
4668 Jim_Nvp *n;
4669 int e = Jim_GetOpt_Nvp(&goi, nvp_target_state, &n);
4670 if (e != JIM_OK) {
4671 Jim_GetOpt_NvpUnknown(&goi, nvp_target_state,1);
4672 return e;
4674 jim_wide a;
4675 e = Jim_GetOpt_Wide(&goi, &a);
4676 if (e != JIM_OK) {
4677 return e;
4679 struct target *target = Jim_CmdPrivData(interp);
4680 if (!target->tap->enabled)
4681 return jim_target_tap_disabled(interp);
4683 e = target_wait_state(target, n->value, a);
4684 if (e != ERROR_OK)
4686 Jim_Obj *eObj = Jim_NewIntObj(interp, e);
4687 Jim_SetResultFormatted(goi.interp,
4688 "target: %s wait %s fails (%#s) %s",
4689 target_name(target), n->name,
4690 eObj, target_strerror_safe(e));
4691 Jim_FreeNewObj(interp, eObj);
4692 return JIM_ERR;
4694 return JIM_OK;
4696 /* List for human, Events defined for this target.
4697 * scripts/programs should use 'name cget -event NAME'
4699 static int jim_target_event_list(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
4701 struct command_context *cmd_ctx = current_command_context(interp);
4702 assert (cmd_ctx != NULL);
4704 struct target *target = Jim_CmdPrivData(interp);
4705 struct target_event_action *teap = target->event_action;
4706 command_print(cmd_ctx, "Event actions for target (%d) %s\n",
4707 target->target_number,
4708 target_name(target));
4709 command_print(cmd_ctx, "%-25s | Body", "Event");
4710 command_print(cmd_ctx, "------------------------- | "
4711 "----------------------------------------");
4712 while (teap)
4714 Jim_Nvp *opt = Jim_Nvp_value2name_simple(nvp_target_event, teap->event);
4715 command_print(cmd_ctx, "%-25s | %s",
4716 opt->name, Jim_GetString(teap->body, NULL));
4717 teap = teap->next;
4719 command_print(cmd_ctx, "***END***");
4720 return JIM_OK;
4722 static int jim_target_current_state(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
4724 if (argc != 1)
4726 Jim_WrongNumArgs(interp, 1, argv, "[no parameters]");
4727 return JIM_ERR;
4729 struct target *target = Jim_CmdPrivData(interp);
4730 Jim_SetResultString(interp, target_state_name(target), -1);
4731 return JIM_OK;
4733 static int jim_target_invoke_event(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
4735 Jim_GetOptInfo goi;
4736 Jim_GetOpt_Setup(&goi, interp, argc - 1, argv + 1);
4737 if (goi.argc != 1)
4739 const char *cmd_name = Jim_GetString(argv[0], NULL);
4740 Jim_SetResultFormatted(goi.interp, "%s <eventname>", cmd_name);
4741 return JIM_ERR;
4743 Jim_Nvp *n;
4744 int e = Jim_GetOpt_Nvp(&goi, nvp_target_event, &n);
4745 if (e != JIM_OK)
4747 Jim_GetOpt_NvpUnknown(&goi, nvp_target_event, 1);
4748 return e;
4750 struct target *target = Jim_CmdPrivData(interp);
4751 target_handle_event(target, n->value);
4752 return JIM_OK;
4755 static const struct command_registration target_instance_command_handlers[] = {
4757 .name = "configure",
4758 .mode = COMMAND_CONFIG,
4759 .jim_handler = jim_target_configure,
4760 .help = "configure a new target for use",
4761 .usage = "[target_attribute ...]",
4764 .name = "cget",
4765 .mode = COMMAND_ANY,
4766 .jim_handler = jim_target_configure,
4767 .help = "returns the specified target attribute",
4768 .usage = "target_attribute",
4771 .name = "mww",
4772 .mode = COMMAND_EXEC,
4773 .jim_handler = jim_target_mw,
4774 .help = "Write 32-bit word(s) to target memory",
4775 .usage = "address data [count]",
4778 .name = "mwh",
4779 .mode = COMMAND_EXEC,
4780 .jim_handler = jim_target_mw,
4781 .help = "Write 16-bit half-word(s) to target memory",
4782 .usage = "address data [count]",
4785 .name = "mwb",
4786 .mode = COMMAND_EXEC,
4787 .jim_handler = jim_target_mw,
4788 .help = "Write byte(s) to target memory",
4789 .usage = "address data [count]",
4792 .name = "mdw",
4793 .mode = COMMAND_EXEC,
4794 .jim_handler = jim_target_md,
4795 .help = "Display target memory as 32-bit words",
4796 .usage = "address [count]",
4799 .name = "mdh",
4800 .mode = COMMAND_EXEC,
4801 .jim_handler = jim_target_md,
4802 .help = "Display target memory as 16-bit half-words",
4803 .usage = "address [count]",
4806 .name = "mdb",
4807 .mode = COMMAND_EXEC,
4808 .jim_handler = jim_target_md,
4809 .help = "Display target memory as 8-bit bytes",
4810 .usage = "address [count]",
4813 .name = "array2mem",
4814 .mode = COMMAND_EXEC,
4815 .jim_handler = jim_target_array2mem,
4816 .help = "Writes Tcl array of 8/16/32 bit numbers "
4817 "to target memory",
4818 .usage = "arrayname bitwidth address count",
4821 .name = "mem2array",
4822 .mode = COMMAND_EXEC,
4823 .jim_handler = jim_target_mem2array,
4824 .help = "Loads Tcl array of 8/16/32 bit numbers "
4825 "from target memory",
4826 .usage = "arrayname bitwidth address count",
4829 .name = "eventlist",
4830 .mode = COMMAND_EXEC,
4831 .jim_handler = jim_target_event_list,
4832 .help = "displays a table of events defined for this target",
4835 .name = "curstate",
4836 .mode = COMMAND_EXEC,
4837 .jim_handler = jim_target_current_state,
4838 .help = "displays the current state of this target",
4841 .name = "arp_examine",
4842 .mode = COMMAND_EXEC,
4843 .jim_handler = jim_target_examine,
4844 .help = "used internally for reset processing",
4847 .name = "arp_halt_gdb",
4848 .mode = COMMAND_EXEC,
4849 .jim_handler = jim_target_halt_gdb,
4850 .help = "used internally for reset processing to halt GDB",
4853 .name = "arp_poll",
4854 .mode = COMMAND_EXEC,
4855 .jim_handler = jim_target_poll,
4856 .help = "used internally for reset processing",
4859 .name = "arp_reset",
4860 .mode = COMMAND_EXEC,
4861 .jim_handler = jim_target_reset,
4862 .help = "used internally for reset processing",
4865 .name = "arp_halt",
4866 .mode = COMMAND_EXEC,
4867 .jim_handler = jim_target_halt,
4868 .help = "used internally for reset processing",
4871 .name = "arp_waitstate",
4872 .mode = COMMAND_EXEC,
4873 .jim_handler = jim_target_wait_state,
4874 .help = "used internally for reset processing",
4877 .name = "invoke-event",
4878 .mode = COMMAND_EXEC,
4879 .jim_handler = jim_target_invoke_event,
4880 .help = "invoke handler for specified event",
4881 .usage = "event_name",
4883 COMMAND_REGISTRATION_DONE
4886 static int target_create(Jim_GetOptInfo *goi)
4888 Jim_Obj *new_cmd;
4889 Jim_Cmd *cmd;
4890 const char *cp;
4891 char *cp2;
4892 int e;
4893 int x;
4894 struct target *target;
4895 struct command_context *cmd_ctx;
4897 cmd_ctx = current_command_context(goi->interp);
4898 assert (cmd_ctx != NULL);
4900 if (goi->argc < 3) {
4901 Jim_WrongNumArgs(goi->interp, 1, goi->argv, "?name? ?type? ..options...");
4902 return JIM_ERR;
4905 /* COMMAND */
4906 Jim_GetOpt_Obj(goi, &new_cmd);
4907 /* does this command exist? */
4908 cmd = Jim_GetCommand(goi->interp, new_cmd, JIM_ERRMSG);
4909 if (cmd) {
4910 cp = Jim_GetString(new_cmd, NULL);
4911 Jim_SetResultFormatted(goi->interp, "Command/target: %s Exists", cp);
4912 return JIM_ERR;
4915 /* TYPE */
4916 e = Jim_GetOpt_String(goi, &cp2, NULL);
4917 if (e != JIM_OK)
4918 return e;
4919 cp = cp2;
4920 /* now does target type exist */
4921 for (x = 0 ; target_types[x] ; x++) {
4922 if (0 == strcmp(cp, target_types[x]->name)) {
4923 /* found */
4924 break;
4927 if (target_types[x] == NULL) {
4928 Jim_SetResultFormatted(goi->interp, "Unknown target type %s, try one of ", cp);
4929 for (x = 0 ; target_types[x] ; x++) {
4930 if (target_types[x + 1]) {
4931 Jim_AppendStrings(goi->interp,
4932 Jim_GetResult(goi->interp),
4933 target_types[x]->name,
4934 ", ", NULL);
4935 } else {
4936 Jim_AppendStrings(goi->interp,
4937 Jim_GetResult(goi->interp),
4938 " or ",
4939 target_types[x]->name,NULL);
4942 return JIM_ERR;
4945 /* Create it */
4946 target = calloc(1,sizeof(struct target));
4947 /* set target number */
4948 target->target_number = new_target_number();
4950 /* allocate memory for each unique target type */
4951 target->type = (struct target_type*)calloc(1,sizeof(struct target_type));
4953 memcpy(target->type, target_types[x], sizeof(struct target_type));
4955 /* will be set by "-endian" */
4956 target->endianness = TARGET_ENDIAN_UNKNOWN;
4958 /* default to first core, override with -coreid */
4959 target->coreid = 0;
4961 target->working_area = 0x0;
4962 target->working_area_size = 0x0;
4963 target->working_areas = NULL;
4964 target->backup_working_area = 0;
4966 target->state = TARGET_UNKNOWN;
4967 target->debug_reason = DBG_REASON_UNDEFINED;
4968 target->reg_cache = NULL;
4969 target->breakpoints = NULL;
4970 target->watchpoints = NULL;
4971 target->next = NULL;
4972 target->arch_info = NULL;
4974 target->display = 1;
4976 target->halt_issued = false;
4978 /* initialize trace information */
4979 target->trace_info = malloc(sizeof(struct trace));
4980 target->trace_info->num_trace_points = 0;
4981 target->trace_info->trace_points_size = 0;
4982 target->trace_info->trace_points = NULL;
4983 target->trace_info->trace_history_size = 0;
4984 target->trace_info->trace_history = NULL;
4985 target->trace_info->trace_history_pos = 0;
4986 target->trace_info->trace_history_overflowed = 0;
4988 target->dbgmsg = NULL;
4989 target->dbg_msg_enabled = 0;
4991 target->endianness = TARGET_ENDIAN_UNKNOWN;
4993 target->rtos = NULL;
4994 target->rtos_auto_detect = false;
4996 /* Do the rest as "configure" options */
4997 goi->isconfigure = 1;
4998 e = target_configure(goi, target);
5000 if (target->tap == NULL)
5002 Jim_SetResultString(goi->interp, "-chain-position required when creating target", -1);
5003 e = JIM_ERR;
5006 if (e != JIM_OK) {
5007 free(target->type);
5008 free(target);
5009 return e;
5012 if (target->endianness == TARGET_ENDIAN_UNKNOWN) {
5013 /* default endian to little if not specified */
5014 target->endianness = TARGET_LITTLE_ENDIAN;
5017 /* incase variant is not set */
5018 if (!target->variant)
5019 target->variant = strdup("");
5021 cp = Jim_GetString(new_cmd, NULL);
5022 target->cmd_name = strdup(cp);
5024 /* create the target specific commands */
5025 if (target->type->commands) {
5026 e = register_commands(cmd_ctx, NULL, target->type->commands);
5027 if (ERROR_OK != e)
5028 LOG_ERROR("unable to register '%s' commands", cp);
5030 if (target->type->target_create) {
5031 (*(target->type->target_create))(target, goi->interp);
5034 /* append to end of list */
5036 struct target **tpp;
5037 tpp = &(all_targets);
5038 while (*tpp) {
5039 tpp = &((*tpp)->next);
5041 *tpp = target;
5044 /* now - create the new target name command */
5045 const const struct command_registration target_subcommands[] = {
5047 .chain = target_instance_command_handlers,
5050 .chain = target->type->commands,
5052 COMMAND_REGISTRATION_DONE
5054 const const struct command_registration target_commands[] = {
5056 .name = cp,
5057 .mode = COMMAND_ANY,
5058 .help = "target command group",
5059 .chain = target_subcommands,
5061 COMMAND_REGISTRATION_DONE
5063 e = register_commands(cmd_ctx, NULL, target_commands);
5064 if (ERROR_OK != e)
5065 return JIM_ERR;
5067 struct command *c = command_find_in_context(cmd_ctx, cp);
5068 assert(c);
5069 command_set_handler_data(c, target);
5071 return (ERROR_OK == e) ? JIM_OK : JIM_ERR;
5074 static int jim_target_current(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
5076 if (argc != 1)
5078 Jim_WrongNumArgs(interp, 1, argv, "Too many parameters");
5079 return JIM_ERR;
5081 struct command_context *cmd_ctx = current_command_context(interp);
5082 assert (cmd_ctx != NULL);
5084 Jim_SetResultString(interp, get_current_target(cmd_ctx)->cmd_name, -1);
5085 return JIM_OK;
5088 static int jim_target_types(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
5090 if (argc != 1)
5092 Jim_WrongNumArgs(interp, 1, argv, "Too many parameters");
5093 return JIM_ERR;
5095 Jim_SetResult(interp, Jim_NewListObj(interp, NULL, 0));
5096 for (unsigned x = 0; NULL != target_types[x]; x++)
5098 Jim_ListAppendElement(interp, Jim_GetResult(interp),
5099 Jim_NewStringObj(interp, target_types[x]->name, -1));
5101 return JIM_OK;
5104 static int jim_target_names(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
5106 if (argc != 1)
5108 Jim_WrongNumArgs(interp, 1, argv, "Too many parameters");
5109 return JIM_ERR;
5111 Jim_SetResult(interp, Jim_NewListObj(interp, NULL, 0));
5112 struct target *target = all_targets;
5113 while (target)
5115 Jim_ListAppendElement(interp, Jim_GetResult(interp),
5116 Jim_NewStringObj(interp, target_name(target), -1));
5117 target = target->next;
5119 return JIM_OK;
5122 static int jim_target_smp(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
5124 int i;
5125 const char *targetname;
5126 int retval,len;
5127 struct target *target;
5128 struct target_list *head, *curr;
5129 curr = (struct target_list*) NULL;
5130 head = (struct target_list*) NULL;
5132 retval = 0;
5133 LOG_DEBUG("%d",argc);
5134 /* argv[1] = target to associate in smp
5135 * argv[2] = target to assoicate in smp
5136 * argv[3] ...
5139 for(i=1;i<argc;i++)
5142 targetname = Jim_GetString(argv[i], &len);
5143 target = get_target(targetname);
5144 LOG_DEBUG("%s ",targetname);
5145 if (target)
5147 struct target_list *new;
5148 new=malloc(sizeof(struct target_list));
5149 new->target = target;
5150 new->next = (struct target_list*)NULL;
5151 if (head == (struct target_list*)NULL)
5153 head = new;
5154 curr = head;
5156 else
5158 curr->next = new;
5159 curr = new;
5163 /* now parse the list of cpu and put the target in smp mode*/
5164 curr=head;
5166 while(curr!=(struct target_list *)NULL)
5168 target=curr->target;
5169 target->smp = 1;
5170 target->head = head;
5171 curr=curr->next;
5173 return retval;
5177 static int jim_target_create(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
5179 Jim_GetOptInfo goi;
5180 Jim_GetOpt_Setup(&goi, interp, argc - 1, argv + 1);
5181 if (goi.argc < 3)
5183 Jim_WrongNumArgs(goi.interp, goi.argc, goi.argv,
5184 "<name> <target_type> [<target_options> ...]");
5185 return JIM_ERR;
5187 return target_create(&goi);
5190 static int jim_target_number(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
5192 Jim_GetOptInfo goi;
5193 Jim_GetOpt_Setup(&goi, interp, argc - 1, argv + 1);
5195 /* It's OK to remove this mechanism sometime after August 2010 or so */
5196 LOG_WARNING("don't use numbers as target identifiers; use names");
5197 if (goi.argc != 1)
5199 Jim_SetResultFormatted(goi.interp, "usage: target number <number>");
5200 return JIM_ERR;
5202 jim_wide w;
5203 int e = Jim_GetOpt_Wide(&goi, &w);
5204 if (e != JIM_OK)
5205 return JIM_ERR;
5207 struct target *target;
5208 for (target = all_targets; NULL != target; target = target->next)
5210 if (target->target_number != w)
5211 continue;
5213 Jim_SetResultString(goi.interp, target_name(target), -1);
5214 return JIM_OK;
5217 Jim_Obj *wObj = Jim_NewIntObj(goi.interp, w);
5218 Jim_SetResultFormatted(goi.interp,
5219 "Target: number %#s does not exist", wObj);
5220 Jim_FreeNewObj(interp, wObj);
5222 return JIM_ERR;
5225 static int jim_target_count(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
5227 if (argc != 1)
5229 Jim_WrongNumArgs(interp, 1, argv, "<no parameters>");
5230 return JIM_ERR;
5232 unsigned count = 0;
5233 struct target *target = all_targets;
5234 while (NULL != target)
5236 target = target->next;
5237 count++;
5239 Jim_SetResult(interp, Jim_NewIntObj(interp, count));
5240 return JIM_OK;
5243 static const struct command_registration target_subcommand_handlers[] = {
5245 .name = "init",
5246 .mode = COMMAND_CONFIG,
5247 .handler = handle_target_init_command,
5248 .help = "initialize targets",
5251 .name = "create",
5252 /* REVISIT this should be COMMAND_CONFIG ... */
5253 .mode = COMMAND_ANY,
5254 .jim_handler = jim_target_create,
5255 .usage = "name type '-chain-position' name [options ...]",
5256 .help = "Creates and selects a new target",
5259 .name = "current",
5260 .mode = COMMAND_ANY,
5261 .jim_handler = jim_target_current,
5262 .help = "Returns the currently selected target",
5265 .name = "types",
5266 .mode = COMMAND_ANY,
5267 .jim_handler = jim_target_types,
5268 .help = "Returns the available target types as "
5269 "a list of strings",
5272 .name = "names",
5273 .mode = COMMAND_ANY,
5274 .jim_handler = jim_target_names,
5275 .help = "Returns the names of all targets as a list of strings",
5278 .name = "number",
5279 .mode = COMMAND_ANY,
5280 .jim_handler = jim_target_number,
5281 .usage = "number",
5282 .help = "Returns the name of the numbered target "
5283 "(DEPRECATED)",
5286 .name = "count",
5287 .mode = COMMAND_ANY,
5288 .jim_handler = jim_target_count,
5289 .help = "Returns the number of targets as an integer "
5290 "(DEPRECATED)",
5293 .name = "smp",
5294 .mode = COMMAND_ANY,
5295 .jim_handler = jim_target_smp,
5296 .usage = "targetname1 targetname2 ...",
5297 .help = "gather several target in a smp list"
5300 COMMAND_REGISTRATION_DONE
5303 struct FastLoad
5305 uint32_t address;
5306 uint8_t *data;
5307 int length;
5311 static int fastload_num;
5312 static struct FastLoad *fastload;
5314 static void free_fastload(void)
5316 if (fastload != NULL)
5318 int i;
5319 for (i = 0; i < fastload_num; i++)
5321 if (fastload[i].data)
5322 free(fastload[i].data);
5324 free(fastload);
5325 fastload = NULL;
5332 COMMAND_HANDLER(handle_fast_load_image_command)
5334 uint8_t *buffer;
5335 size_t buf_cnt;
5336 uint32_t image_size;
5337 uint32_t min_address = 0;
5338 uint32_t max_address = 0xffffffff;
5339 int i;
5341 struct image image;
5343 int retval = CALL_COMMAND_HANDLER(parse_load_image_command_CMD_ARGV,
5344 &image, &min_address, &max_address);
5345 if (ERROR_OK != retval)
5346 return retval;
5348 struct duration bench;
5349 duration_start(&bench);
5351 retval = image_open(&image, CMD_ARGV[0], (CMD_ARGC >= 3) ? CMD_ARGV[2] : NULL);
5352 if (retval != ERROR_OK)
5354 return retval;
5357 image_size = 0x0;
5358 retval = ERROR_OK;
5359 fastload_num = image.num_sections;
5360 fastload = (struct FastLoad *)malloc(sizeof(struct FastLoad)*image.num_sections);
5361 if (fastload == NULL)
5363 command_print(CMD_CTX, "out of memory");
5364 image_close(&image);
5365 return ERROR_FAIL;
5367 memset(fastload, 0, sizeof(struct FastLoad)*image.num_sections);
5368 for (i = 0; i < image.num_sections; i++)
5370 buffer = malloc(image.sections[i].size);
5371 if (buffer == NULL)
5373 command_print(CMD_CTX, "error allocating buffer for section (%d bytes)",
5374 (int)(image.sections[i].size));
5375 retval = ERROR_FAIL;
5376 break;
5379 if ((retval = image_read_section(&image, i, 0x0, image.sections[i].size, buffer, &buf_cnt)) != ERROR_OK)
5381 free(buffer);
5382 break;
5385 uint32_t offset = 0;
5386 uint32_t length = buf_cnt;
5389 /* DANGER!!! beware of unsigned comparision here!!! */
5391 if ((image.sections[i].base_address + buf_cnt >= min_address)&&
5392 (image.sections[i].base_address < max_address))
5394 if (image.sections[i].base_address < min_address)
5396 /* clip addresses below */
5397 offset += min_address-image.sections[i].base_address;
5398 length -= offset;
5401 if (image.sections[i].base_address + buf_cnt > max_address)
5403 length -= (image.sections[i].base_address + buf_cnt)-max_address;
5406 fastload[i].address = image.sections[i].base_address + offset;
5407 fastload[i].data = malloc(length);
5408 if (fastload[i].data == NULL)
5410 free(buffer);
5411 command_print(CMD_CTX, "error allocating buffer for section (%d bytes)",
5412 length);
5413 retval = ERROR_FAIL;
5414 break;
5416 memcpy(fastload[i].data, buffer + offset, length);
5417 fastload[i].length = length;
5419 image_size += length;
5420 command_print(CMD_CTX, "%u bytes written at address 0x%8.8x",
5421 (unsigned int)length,
5422 ((unsigned int)(image.sections[i].base_address + offset)));
5425 free(buffer);
5428 if ((ERROR_OK == retval) && (duration_measure(&bench) == ERROR_OK))
5430 command_print(CMD_CTX, "Loaded %" PRIu32 " bytes "
5431 "in %fs (%0.3f KiB/s)", image_size,
5432 duration_elapsed(&bench), duration_kbps(&bench, image_size));
5434 command_print(CMD_CTX,
5435 "WARNING: image has not been loaded to target!"
5436 "You can issue a 'fast_load' to finish loading.");
5439 image_close(&image);
5441 if (retval != ERROR_OK)
5443 free_fastload();
5446 return retval;
5449 COMMAND_HANDLER(handle_fast_load_command)
5451 if (CMD_ARGC > 0)
5452 return ERROR_COMMAND_SYNTAX_ERROR;
5453 if (fastload == NULL)
5455 LOG_ERROR("No image in memory");
5456 return ERROR_FAIL;
5458 int i;
5459 int ms = timeval_ms();
5460 int size = 0;
5461 int retval = ERROR_OK;
5462 for (i = 0; i < fastload_num;i++)
5464 struct target *target = get_current_target(CMD_CTX);
5465 command_print(CMD_CTX, "Write to 0x%08x, length 0x%08x",
5466 (unsigned int)(fastload[i].address),
5467 (unsigned int)(fastload[i].length));
5468 retval = target_write_buffer(target, fastload[i].address, fastload[i].length, fastload[i].data);
5469 if (retval != ERROR_OK)
5471 break;
5473 size += fastload[i].length;
5475 if (retval == ERROR_OK)
5477 int after = timeval_ms();
5478 command_print(CMD_CTX, "Loaded image %f kBytes/s", (float)(size/1024.0)/((float)(after-ms)/1000.0));
5480 return retval;
5483 static const struct command_registration target_command_handlers[] = {
5485 .name = "targets",
5486 .handler = handle_targets_command,
5487 .mode = COMMAND_ANY,
5488 .help = "change current default target (one parameter) "
5489 "or prints table of all targets (no parameters)",
5490 .usage = "[target]",
5493 .name = "target",
5494 .mode = COMMAND_CONFIG,
5495 .help = "configure target",
5497 .chain = target_subcommand_handlers,
5499 COMMAND_REGISTRATION_DONE
5502 int target_register_commands(struct command_context *cmd_ctx)
5504 return register_commands(cmd_ctx, NULL, target_command_handlers);
5507 static bool target_reset_nag = true;
5509 bool get_target_reset_nag(void)
5511 return target_reset_nag;
5514 COMMAND_HANDLER(handle_target_reset_nag)
5516 return CALL_COMMAND_HANDLER(handle_command_parse_bool,
5517 &target_reset_nag, "Nag after each reset about options to improve "
5518 "performance");
5521 static const struct command_registration target_exec_command_handlers[] = {
5523 .name = "fast_load_image",
5524 .handler = handle_fast_load_image_command,
5525 .mode = COMMAND_ANY,
5526 .help = "Load image into server memory for later use by "
5527 "fast_load; primarily for profiling",
5528 .usage = "filename address ['bin'|'ihex'|'elf'|'s19'] "
5529 "[min_address [max_length]]",
5532 .name = "fast_load",
5533 .handler = handle_fast_load_command,
5534 .mode = COMMAND_EXEC,
5535 .help = "loads active fast load image to current target "
5536 "- mainly for profiling purposes",
5539 .name = "profile",
5540 .handler = handle_profile_command,
5541 .mode = COMMAND_EXEC,
5542 .help = "profiling samples the CPU PC",
5544 /** @todo don't register virt2phys() unless target supports it */
5546 .name = "virt2phys",
5547 .handler = handle_virt2phys_command,
5548 .mode = COMMAND_ANY,
5549 .help = "translate a virtual address into a physical address",
5550 .usage = "virtual_address",
5553 .name = "reg",
5554 .handler = handle_reg_command,
5555 .mode = COMMAND_EXEC,
5556 .help = "display or set a register; with no arguments, "
5557 "displays all registers and their values",
5558 .usage = "[(register_name|register_number) [value]]",
5561 .name = "poll",
5562 .handler = handle_poll_command,
5563 .mode = COMMAND_EXEC,
5564 .help = "poll target state; or reconfigure background polling",
5565 .usage = "['on'|'off']",
5568 .name = "wait_halt",
5569 .handler = handle_wait_halt_command,
5570 .mode = COMMAND_EXEC,
5571 .help = "wait up to the specified number of milliseconds "
5572 "(default 5) for a previously requested halt",
5573 .usage = "[milliseconds]",
5576 .name = "halt",
5577 .handler = handle_halt_command,
5578 .mode = COMMAND_EXEC,
5579 .help = "request target to halt, then wait up to the specified"
5580 "number of milliseconds (default 5) for it to complete",
5581 .usage = "[milliseconds]",
5584 .name = "resume",
5585 .handler = handle_resume_command,
5586 .mode = COMMAND_EXEC,
5587 .help = "resume target execution from current PC or address",
5588 .usage = "[address]",
5591 .name = "reset",
5592 .handler = handle_reset_command,
5593 .mode = COMMAND_EXEC,
5594 .usage = "[run|halt|init]",
5595 .help = "Reset all targets into the specified mode."
5596 "Default reset mode is run, if not given.",
5599 .name = "soft_reset_halt",
5600 .handler = handle_soft_reset_halt_command,
5601 .mode = COMMAND_EXEC,
5602 .help = "halt the target and do a soft reset",
5605 .name = "step",
5606 .handler = handle_step_command,
5607 .mode = COMMAND_EXEC,
5608 .help = "step one instruction from current PC or address",
5609 .usage = "[address]",
5612 .name = "mdw",
5613 .handler = handle_md_command,
5614 .mode = COMMAND_EXEC,
5615 .help = "display memory words",
5616 .usage = "['phys'] address [count]",
5619 .name = "mdh",
5620 .handler = handle_md_command,
5621 .mode = COMMAND_EXEC,
5622 .help = "display memory half-words",
5623 .usage = "['phys'] address [count]",
5626 .name = "mdb",
5627 .handler = handle_md_command,
5628 .mode = COMMAND_EXEC,
5629 .help = "display memory bytes",
5630 .usage = "['phys'] address [count]",
5633 .name = "mww",
5634 .handler = handle_mw_command,
5635 .mode = COMMAND_EXEC,
5636 .help = "write memory word",
5637 .usage = "['phys'] address value [count]",
5640 .name = "mwh",
5641 .handler = handle_mw_command,
5642 .mode = COMMAND_EXEC,
5643 .help = "write memory half-word",
5644 .usage = "['phys'] address value [count]",
5647 .name = "mwb",
5648 .handler = handle_mw_command,
5649 .mode = COMMAND_EXEC,
5650 .help = "write memory byte",
5651 .usage = "['phys'] address value [count]",
5654 .name = "bp",
5655 .handler = handle_bp_command,
5656 .mode = COMMAND_EXEC,
5657 .help = "list or set hardware or software breakpoint",
5658 .usage = "usage: bp <address> [<asid>]<length> ['hw'|'hw_ctx']",
5661 .name = "rbp",
5662 .handler = handle_rbp_command,
5663 .mode = COMMAND_EXEC,
5664 .help = "remove breakpoint",
5665 .usage = "address",
5668 .name = "wp",
5669 .handler = handle_wp_command,
5670 .mode = COMMAND_EXEC,
5671 .help = "list (no params) or create watchpoints",
5672 .usage = "[address length [('r'|'w'|'a') value [mask]]]",
5675 .name = "rwp",
5676 .handler = handle_rwp_command,
5677 .mode = COMMAND_EXEC,
5678 .help = "remove watchpoint",
5679 .usage = "address",
5682 .name = "load_image",
5683 .handler = handle_load_image_command,
5684 .mode = COMMAND_EXEC,
5685 .usage = "filename address ['bin'|'ihex'|'elf'|'s19'] "
5686 "[min_address] [max_length]",
5689 .name = "dump_image",
5690 .handler = handle_dump_image_command,
5691 .mode = COMMAND_EXEC,
5692 .usage = "filename address size",
5695 .name = "verify_image",
5696 .handler = handle_verify_image_command,
5697 .mode = COMMAND_EXEC,
5698 .usage = "filename [offset [type]]",
5701 .name = "test_image",
5702 .handler = handle_test_image_command,
5703 .mode = COMMAND_EXEC,
5704 .usage = "filename [offset [type]]",
5707 .name = "mem2array",
5708 .mode = COMMAND_EXEC,
5709 .jim_handler = jim_mem2array,
5710 .help = "read 8/16/32 bit memory and return as a TCL array "
5711 "for script processing",
5712 .usage = "arrayname bitwidth address count",
5715 .name = "array2mem",
5716 .mode = COMMAND_EXEC,
5717 .jim_handler = jim_array2mem,
5718 .help = "convert a TCL array to memory locations "
5719 "and write the 8/16/32 bit values",
5720 .usage = "arrayname bitwidth address count",
5723 .name = "reset_nag",
5724 .handler = handle_target_reset_nag,
5725 .mode = COMMAND_ANY,
5726 .help = "Nag after each reset about options that could have been "
5727 "enabled to improve performance. ",
5728 .usage = "['enable'|'disable']",
5730 COMMAND_REGISTRATION_DONE
5732 static int target_register_user_commands(struct command_context *cmd_ctx)
5734 int retval = ERROR_OK;
5735 if ((retval = target_request_register_commands(cmd_ctx)) != ERROR_OK)
5736 return retval;
5738 if ((retval = trace_register_commands(cmd_ctx)) != ERROR_OK)
5739 return retval;
5742 return register_commands(cmd_ctx, NULL, target_exec_command_handlers);