arm_adi_v5: Add convenience "atomic"" function for DP reads
[openocd.git] / src / target / target.c
blob9416b25ba6bcd9c8a05bc910b28cd2fd4b27231d
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 * Copyright (C) 2011 Andreas Fritiofson *
24 * andreas.fritiofson@gmail.com *
25 * *
26 * This program is free software; you can redistribute it and/or modify *
27 * it under the terms of the GNU General Public License as published by *
28 * the Free Software Foundation; either version 2 of the License, or *
29 * (at your option) any later version. *
30 * *
31 * This program is distributed in the hope that it will be useful, *
32 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
33 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
34 * GNU General Public License for more details. *
35 * *
36 * You should have received a copy of the GNU General Public License *
37 * along with this program; if not, write to the *
38 * Free Software Foundation, Inc., *
39 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. *
40 ***************************************************************************/
42 #ifdef HAVE_CONFIG_H
43 #include "config.h"
44 #endif
46 #include <helper/time_support.h>
47 #include <jtag/jtag.h>
48 #include <flash/nor/core.h>
50 #include "target.h"
51 #include "target_type.h"
52 #include "target_request.h"
53 #include "breakpoints.h"
54 #include "register.h"
55 #include "trace.h"
56 #include "image.h"
57 #include "rtos/rtos.h"
59 /* default halt wait timeout (ms) */
60 #define DEFAULT_HALT_TIMEOUT 5000
62 static int target_read_buffer_default(struct target *target, uint32_t address,
63 uint32_t count, uint8_t *buffer);
64 static int target_write_buffer_default(struct target *target, uint32_t address,
65 uint32_t count, const uint8_t *buffer);
66 static int target_array2mem(Jim_Interp *interp, struct target *target,
67 int argc, Jim_Obj * const *argv);
68 static int target_mem2array(Jim_Interp *interp, struct target *target,
69 int argc, Jim_Obj * const *argv);
70 static int target_register_user_commands(struct command_context *cmd_ctx);
71 static int target_get_gdb_fileio_info_default(struct target *target,
72 struct gdb_fileio_info *fileio_info);
73 static int target_gdb_fileio_end_default(struct target *target, int retcode,
74 int fileio_errno, bool ctrl_c);
75 static int target_profiling_default(struct target *target, uint32_t *samples,
76 uint32_t max_num_samples, uint32_t *num_samples, uint32_t seconds);
78 /* targets */
79 extern struct target_type arm7tdmi_target;
80 extern struct target_type arm720t_target;
81 extern struct target_type arm9tdmi_target;
82 extern struct target_type arm920t_target;
83 extern struct target_type arm966e_target;
84 extern struct target_type arm946e_target;
85 extern struct target_type arm926ejs_target;
86 extern struct target_type fa526_target;
87 extern struct target_type feroceon_target;
88 extern struct target_type dragonite_target;
89 extern struct target_type xscale_target;
90 extern struct target_type cortexm_target;
91 extern struct target_type cortexa8_target;
92 extern struct target_type cortexr4_target;
93 extern struct target_type arm11_target;
94 extern struct target_type mips_m4k_target;
95 extern struct target_type avr_target;
96 extern struct target_type dsp563xx_target;
97 extern struct target_type dsp5680xx_target;
98 extern struct target_type testee_target;
99 extern struct target_type avr32_ap7k_target;
100 extern struct target_type hla_target;
101 extern struct target_type nds32_v2_target;
102 extern struct target_type nds32_v3_target;
103 extern struct target_type nds32_v3m_target;
104 extern struct target_type or1k_target;
105 extern struct target_type quark_x10xx_target;
107 static struct target_type *target_types[] = {
108 &arm7tdmi_target,
109 &arm9tdmi_target,
110 &arm920t_target,
111 &arm720t_target,
112 &arm966e_target,
113 &arm946e_target,
114 &arm926ejs_target,
115 &fa526_target,
116 &feroceon_target,
117 &dragonite_target,
118 &xscale_target,
119 &cortexm_target,
120 &cortexa8_target,
121 &cortexr4_target,
122 &arm11_target,
123 &mips_m4k_target,
124 &avr_target,
125 &dsp563xx_target,
126 &dsp5680xx_target,
127 &testee_target,
128 &avr32_ap7k_target,
129 &hla_target,
130 &nds32_v2_target,
131 &nds32_v3_target,
132 &nds32_v3m_target,
133 &or1k_target,
134 &quark_x10xx_target,
135 NULL,
138 struct target *all_targets;
139 static struct target_event_callback *target_event_callbacks;
140 static struct target_timer_callback *target_timer_callbacks;
141 static const int polling_interval = 100;
143 static const Jim_Nvp nvp_assert[] = {
144 { .name = "assert", NVP_ASSERT },
145 { .name = "deassert", NVP_DEASSERT },
146 { .name = "T", NVP_ASSERT },
147 { .name = "F", NVP_DEASSERT },
148 { .name = "t", NVP_ASSERT },
149 { .name = "f", NVP_DEASSERT },
150 { .name = NULL, .value = -1 }
153 static const Jim_Nvp nvp_error_target[] = {
154 { .value = ERROR_TARGET_INVALID, .name = "err-invalid" },
155 { .value = ERROR_TARGET_INIT_FAILED, .name = "err-init-failed" },
156 { .value = ERROR_TARGET_TIMEOUT, .name = "err-timeout" },
157 { .value = ERROR_TARGET_NOT_HALTED, .name = "err-not-halted" },
158 { .value = ERROR_TARGET_FAILURE, .name = "err-failure" },
159 { .value = ERROR_TARGET_UNALIGNED_ACCESS , .name = "err-unaligned-access" },
160 { .value = ERROR_TARGET_DATA_ABORT , .name = "err-data-abort" },
161 { .value = ERROR_TARGET_RESOURCE_NOT_AVAILABLE , .name = "err-resource-not-available" },
162 { .value = ERROR_TARGET_TRANSLATION_FAULT , .name = "err-translation-fault" },
163 { .value = ERROR_TARGET_NOT_RUNNING, .name = "err-not-running" },
164 { .value = ERROR_TARGET_NOT_EXAMINED, .name = "err-not-examined" },
165 { .value = -1, .name = NULL }
168 static const char *target_strerror_safe(int err)
170 const Jim_Nvp *n;
172 n = Jim_Nvp_value2name_simple(nvp_error_target, err);
173 if (n->name == NULL)
174 return "unknown";
175 else
176 return n->name;
179 static const Jim_Nvp nvp_target_event[] = {
181 { .value = TARGET_EVENT_GDB_HALT, .name = "gdb-halt" },
182 { .value = TARGET_EVENT_HALTED, .name = "halted" },
183 { .value = TARGET_EVENT_RESUMED, .name = "resumed" },
184 { .value = TARGET_EVENT_RESUME_START, .name = "resume-start" },
185 { .value = TARGET_EVENT_RESUME_END, .name = "resume-end" },
187 { .name = "gdb-start", .value = TARGET_EVENT_GDB_START },
188 { .name = "gdb-end", .value = TARGET_EVENT_GDB_END },
190 { .value = TARGET_EVENT_RESET_START, .name = "reset-start" },
191 { .value = TARGET_EVENT_RESET_ASSERT_PRE, .name = "reset-assert-pre" },
192 { .value = TARGET_EVENT_RESET_ASSERT, .name = "reset-assert" },
193 { .value = TARGET_EVENT_RESET_ASSERT_POST, .name = "reset-assert-post" },
194 { .value = TARGET_EVENT_RESET_DEASSERT_PRE, .name = "reset-deassert-pre" },
195 { .value = TARGET_EVENT_RESET_DEASSERT_POST, .name = "reset-deassert-post" },
196 { .value = TARGET_EVENT_RESET_HALT_PRE, .name = "reset-halt-pre" },
197 { .value = TARGET_EVENT_RESET_HALT_POST, .name = "reset-halt-post" },
198 { .value = TARGET_EVENT_RESET_WAIT_PRE, .name = "reset-wait-pre" },
199 { .value = TARGET_EVENT_RESET_WAIT_POST, .name = "reset-wait-post" },
200 { .value = TARGET_EVENT_RESET_INIT, .name = "reset-init" },
201 { .value = TARGET_EVENT_RESET_END, .name = "reset-end" },
203 { .value = TARGET_EVENT_EXAMINE_START, .name = "examine-start" },
204 { .value = TARGET_EVENT_EXAMINE_END, .name = "examine-end" },
206 { .value = TARGET_EVENT_DEBUG_HALTED, .name = "debug-halted" },
207 { .value = TARGET_EVENT_DEBUG_RESUMED, .name = "debug-resumed" },
209 { .value = TARGET_EVENT_GDB_ATTACH, .name = "gdb-attach" },
210 { .value = TARGET_EVENT_GDB_DETACH, .name = "gdb-detach" },
212 { .value = TARGET_EVENT_GDB_FLASH_WRITE_START, .name = "gdb-flash-write-start" },
213 { .value = TARGET_EVENT_GDB_FLASH_WRITE_END , .name = "gdb-flash-write-end" },
215 { .value = TARGET_EVENT_GDB_FLASH_ERASE_START, .name = "gdb-flash-erase-start" },
216 { .value = TARGET_EVENT_GDB_FLASH_ERASE_END , .name = "gdb-flash-erase-end" },
218 { .name = NULL, .value = -1 }
221 static const Jim_Nvp nvp_target_state[] = {
222 { .name = "unknown", .value = TARGET_UNKNOWN },
223 { .name = "running", .value = TARGET_RUNNING },
224 { .name = "halted", .value = TARGET_HALTED },
225 { .name = "reset", .value = TARGET_RESET },
226 { .name = "debug-running", .value = TARGET_DEBUG_RUNNING },
227 { .name = NULL, .value = -1 },
230 static const Jim_Nvp nvp_target_debug_reason[] = {
231 { .name = "debug-request" , .value = DBG_REASON_DBGRQ },
232 { .name = "breakpoint" , .value = DBG_REASON_BREAKPOINT },
233 { .name = "watchpoint" , .value = DBG_REASON_WATCHPOINT },
234 { .name = "watchpoint-and-breakpoint", .value = DBG_REASON_WPTANDBKPT },
235 { .name = "single-step" , .value = DBG_REASON_SINGLESTEP },
236 { .name = "target-not-halted" , .value = DBG_REASON_NOTHALTED },
237 { .name = "program-exit" , .value = DBG_REASON_EXIT },
238 { .name = "undefined" , .value = DBG_REASON_UNDEFINED },
239 { .name = NULL, .value = -1 },
242 static const Jim_Nvp nvp_target_endian[] = {
243 { .name = "big", .value = TARGET_BIG_ENDIAN },
244 { .name = "little", .value = TARGET_LITTLE_ENDIAN },
245 { .name = "be", .value = TARGET_BIG_ENDIAN },
246 { .name = "le", .value = TARGET_LITTLE_ENDIAN },
247 { .name = NULL, .value = -1 },
250 static const Jim_Nvp nvp_reset_modes[] = {
251 { .name = "unknown", .value = RESET_UNKNOWN },
252 { .name = "run" , .value = RESET_RUN },
253 { .name = "halt" , .value = RESET_HALT },
254 { .name = "init" , .value = RESET_INIT },
255 { .name = NULL , .value = -1 },
258 const char *debug_reason_name(struct target *t)
260 const char *cp;
262 cp = Jim_Nvp_value2name_simple(nvp_target_debug_reason,
263 t->debug_reason)->name;
264 if (!cp) {
265 LOG_ERROR("Invalid debug reason: %d", (int)(t->debug_reason));
266 cp = "(*BUG*unknown*BUG*)";
268 return cp;
271 const char *target_state_name(struct target *t)
273 const char *cp;
274 cp = Jim_Nvp_value2name_simple(nvp_target_state, t->state)->name;
275 if (!cp) {
276 LOG_ERROR("Invalid target state: %d", (int)(t->state));
277 cp = "(*BUG*unknown*BUG*)";
279 return cp;
282 /* determine the number of the new target */
283 static int new_target_number(void)
285 struct target *t;
286 int x;
288 /* number is 0 based */
289 x = -1;
290 t = all_targets;
291 while (t) {
292 if (x < t->target_number)
293 x = t->target_number;
294 t = t->next;
296 return x + 1;
299 /* read a uint64_t from a buffer in target memory endianness */
300 uint64_t target_buffer_get_u64(struct target *target, const uint8_t *buffer)
302 if (target->endianness == TARGET_LITTLE_ENDIAN)
303 return le_to_h_u64(buffer);
304 else
305 return be_to_h_u64(buffer);
308 /* read a uint32_t from a buffer in target memory endianness */
309 uint32_t target_buffer_get_u32(struct target *target, const uint8_t *buffer)
311 if (target->endianness == TARGET_LITTLE_ENDIAN)
312 return le_to_h_u32(buffer);
313 else
314 return be_to_h_u32(buffer);
317 /* read a uint24_t from a buffer in target memory endianness */
318 uint32_t target_buffer_get_u24(struct target *target, const uint8_t *buffer)
320 if (target->endianness == TARGET_LITTLE_ENDIAN)
321 return le_to_h_u24(buffer);
322 else
323 return be_to_h_u24(buffer);
326 /* read a uint16_t from a buffer in target memory endianness */
327 uint16_t target_buffer_get_u16(struct target *target, const uint8_t *buffer)
329 if (target->endianness == TARGET_LITTLE_ENDIAN)
330 return le_to_h_u16(buffer);
331 else
332 return be_to_h_u16(buffer);
335 /* read a uint8_t from a buffer in target memory endianness */
336 static uint8_t target_buffer_get_u8(struct target *target, const uint8_t *buffer)
338 return *buffer & 0x0ff;
341 /* write a uint64_t to a buffer in target memory endianness */
342 void target_buffer_set_u64(struct target *target, uint8_t *buffer, uint64_t value)
344 if (target->endianness == TARGET_LITTLE_ENDIAN)
345 h_u64_to_le(buffer, value);
346 else
347 h_u64_to_be(buffer, value);
350 /* write a uint32_t to a buffer in target memory endianness */
351 void target_buffer_set_u32(struct target *target, uint8_t *buffer, uint32_t value)
353 if (target->endianness == TARGET_LITTLE_ENDIAN)
354 h_u32_to_le(buffer, value);
355 else
356 h_u32_to_be(buffer, value);
359 /* write a uint24_t to a buffer in target memory endianness */
360 void target_buffer_set_u24(struct target *target, uint8_t *buffer, uint32_t value)
362 if (target->endianness == TARGET_LITTLE_ENDIAN)
363 h_u24_to_le(buffer, value);
364 else
365 h_u24_to_be(buffer, value);
368 /* write a uint16_t to a buffer in target memory endianness */
369 void target_buffer_set_u16(struct target *target, uint8_t *buffer, uint16_t value)
371 if (target->endianness == TARGET_LITTLE_ENDIAN)
372 h_u16_to_le(buffer, value);
373 else
374 h_u16_to_be(buffer, value);
377 /* write a uint8_t to a buffer in target memory endianness */
378 static void target_buffer_set_u8(struct target *target, uint8_t *buffer, uint8_t value)
380 *buffer = value;
383 /* write a uint64_t array to a buffer in target memory endianness */
384 void target_buffer_get_u64_array(struct target *target, const uint8_t *buffer, uint32_t count, uint64_t *dstbuf)
386 uint32_t i;
387 for (i = 0; i < count; i++)
388 dstbuf[i] = target_buffer_get_u64(target, &buffer[i * 8]);
391 /* write a uint32_t array to a buffer in target memory endianness */
392 void target_buffer_get_u32_array(struct target *target, const uint8_t *buffer, uint32_t count, uint32_t *dstbuf)
394 uint32_t i;
395 for (i = 0; i < count; i++)
396 dstbuf[i] = target_buffer_get_u32(target, &buffer[i * 4]);
399 /* write a uint16_t array to a buffer in target memory endianness */
400 void target_buffer_get_u16_array(struct target *target, const uint8_t *buffer, uint32_t count, uint16_t *dstbuf)
402 uint32_t i;
403 for (i = 0; i < count; i++)
404 dstbuf[i] = target_buffer_get_u16(target, &buffer[i * 2]);
407 /* write a uint64_t array to a buffer in target memory endianness */
408 void target_buffer_set_u64_array(struct target *target, uint8_t *buffer, uint32_t count, const uint64_t *srcbuf)
410 uint32_t i;
411 for (i = 0; i < count; i++)
412 target_buffer_set_u64(target, &buffer[i * 8], srcbuf[i]);
415 /* write a uint32_t array to a buffer in target memory endianness */
416 void target_buffer_set_u32_array(struct target *target, uint8_t *buffer, uint32_t count, const uint32_t *srcbuf)
418 uint32_t i;
419 for (i = 0; i < count; i++)
420 target_buffer_set_u32(target, &buffer[i * 4], srcbuf[i]);
423 /* write a uint16_t array to a buffer in target memory endianness */
424 void target_buffer_set_u16_array(struct target *target, uint8_t *buffer, uint32_t count, const uint16_t *srcbuf)
426 uint32_t i;
427 for (i = 0; i < count; i++)
428 target_buffer_set_u16(target, &buffer[i * 2], srcbuf[i]);
431 /* return a pointer to a configured target; id is name or number */
432 struct target *get_target(const char *id)
434 struct target *target;
436 /* try as tcltarget name */
437 for (target = all_targets; target; target = target->next) {
438 if (target_name(target) == NULL)
439 continue;
440 if (strcmp(id, target_name(target)) == 0)
441 return target;
444 /* It's OK to remove this fallback sometime after August 2010 or so */
446 /* no match, try as number */
447 unsigned num;
448 if (parse_uint(id, &num) != ERROR_OK)
449 return NULL;
451 for (target = all_targets; target; target = target->next) {
452 if (target->target_number == (int)num) {
453 LOG_WARNING("use '%s' as target identifier, not '%u'",
454 target_name(target), num);
455 return target;
459 return NULL;
462 /* returns a pointer to the n-th configured target */
463 static struct target *get_target_by_num(int num)
465 struct target *target = all_targets;
467 while (target) {
468 if (target->target_number == num)
469 return target;
470 target = target->next;
473 return NULL;
476 struct target *get_current_target(struct command_context *cmd_ctx)
478 struct target *target = get_target_by_num(cmd_ctx->current_target);
480 if (target == NULL) {
481 LOG_ERROR("BUG: current_target out of bounds");
482 exit(-1);
485 return target;
488 int target_poll(struct target *target)
490 int retval;
492 /* We can't poll until after examine */
493 if (!target_was_examined(target)) {
494 /* Fail silently lest we pollute the log */
495 return ERROR_FAIL;
498 retval = target->type->poll(target);
499 if (retval != ERROR_OK)
500 return retval;
502 if (target->halt_issued) {
503 if (target->state == TARGET_HALTED)
504 target->halt_issued = false;
505 else {
506 long long t = timeval_ms() - target->halt_issued_time;
507 if (t > DEFAULT_HALT_TIMEOUT) {
508 target->halt_issued = false;
509 LOG_INFO("Halt timed out, wake up GDB.");
510 target_call_event_callbacks(target, TARGET_EVENT_GDB_HALT);
515 return ERROR_OK;
518 int target_halt(struct target *target)
520 int retval;
521 /* We can't poll until after examine */
522 if (!target_was_examined(target)) {
523 LOG_ERROR("Target not examined yet");
524 return ERROR_FAIL;
527 retval = target->type->halt(target);
528 if (retval != ERROR_OK)
529 return retval;
531 target->halt_issued = true;
532 target->halt_issued_time = timeval_ms();
534 return ERROR_OK;
538 * Make the target (re)start executing using its saved execution
539 * context (possibly with some modifications).
541 * @param target Which target should start executing.
542 * @param current True to use the target's saved program counter instead
543 * of the address parameter
544 * @param address Optionally used as the program counter.
545 * @param handle_breakpoints True iff breakpoints at the resumption PC
546 * should be skipped. (For example, maybe execution was stopped by
547 * such a breakpoint, in which case it would be counterprodutive to
548 * let it re-trigger.
549 * @param debug_execution False if all working areas allocated by OpenOCD
550 * should be released and/or restored to their original contents.
551 * (This would for example be true to run some downloaded "helper"
552 * algorithm code, which resides in one such working buffer and uses
553 * another for data storage.)
555 * @todo Resolve the ambiguity about what the "debug_execution" flag
556 * signifies. For example, Target implementations don't agree on how
557 * it relates to invalidation of the register cache, or to whether
558 * breakpoints and watchpoints should be enabled. (It would seem wrong
559 * to enable breakpoints when running downloaded "helper" algorithms
560 * (debug_execution true), since the breakpoints would be set to match
561 * target firmware being debugged, not the helper algorithm.... and
562 * enabling them could cause such helpers to malfunction (for example,
563 * by overwriting data with a breakpoint instruction. On the other
564 * hand the infrastructure for running such helpers might use this
565 * procedure but rely on hardware breakpoint to detect termination.)
567 int target_resume(struct target *target, int current, uint32_t address, int handle_breakpoints, int debug_execution)
569 int retval;
571 /* We can't poll until after examine */
572 if (!target_was_examined(target)) {
573 LOG_ERROR("Target not examined yet");
574 return ERROR_FAIL;
577 target_call_event_callbacks(target, TARGET_EVENT_RESUME_START);
579 /* note that resume *must* be asynchronous. The CPU can halt before
580 * we poll. The CPU can even halt at the current PC as a result of
581 * a software breakpoint being inserted by (a bug?) the application.
583 retval = target->type->resume(target, current, address, handle_breakpoints, debug_execution);
584 if (retval != ERROR_OK)
585 return retval;
587 target_call_event_callbacks(target, TARGET_EVENT_RESUME_END);
589 return retval;
592 static int target_process_reset(struct command_context *cmd_ctx, enum target_reset_mode reset_mode)
594 char buf[100];
595 int retval;
596 Jim_Nvp *n;
597 n = Jim_Nvp_value2name_simple(nvp_reset_modes, reset_mode);
598 if (n->name == NULL) {
599 LOG_ERROR("invalid reset mode");
600 return ERROR_FAIL;
603 /* disable polling during reset to make reset event scripts
604 * more predictable, i.e. dr/irscan & pathmove in events will
605 * not have JTAG operations injected into the middle of a sequence.
607 bool save_poll = jtag_poll_get_enabled();
609 jtag_poll_set_enabled(false);
611 sprintf(buf, "ocd_process_reset %s", n->name);
612 retval = Jim_Eval(cmd_ctx->interp, buf);
614 jtag_poll_set_enabled(save_poll);
616 if (retval != JIM_OK) {
617 Jim_MakeErrorMessage(cmd_ctx->interp);
618 command_print(NULL, "%s\n", Jim_GetString(Jim_GetResult(cmd_ctx->interp), NULL));
619 return ERROR_FAIL;
622 /* We want any events to be processed before the prompt */
623 retval = target_call_timer_callbacks_now();
625 struct target *target;
626 for (target = all_targets; target; target = target->next) {
627 target->type->check_reset(target);
628 target->running_alg = false;
631 return retval;
634 static int identity_virt2phys(struct target *target,
635 uint32_t virtual, uint32_t *physical)
637 *physical = virtual;
638 return ERROR_OK;
641 static int no_mmu(struct target *target, int *enabled)
643 *enabled = 0;
644 return ERROR_OK;
647 static int default_examine(struct target *target)
649 target_set_examined(target);
650 return ERROR_OK;
653 /* no check by default */
654 static int default_check_reset(struct target *target)
656 return ERROR_OK;
659 int target_examine_one(struct target *target)
661 return target->type->examine(target);
664 static int jtag_enable_callback(enum jtag_event event, void *priv)
666 struct target *target = priv;
668 if (event != JTAG_TAP_EVENT_ENABLE || !target->tap->enabled)
669 return ERROR_OK;
671 jtag_unregister_event_callback(jtag_enable_callback, target);
673 target_call_event_callbacks(target, TARGET_EVENT_EXAMINE_START);
675 int retval = target_examine_one(target);
676 if (retval != ERROR_OK)
677 return retval;
679 target_call_event_callbacks(target, TARGET_EVENT_EXAMINE_END);
681 return retval;
684 /* Targets that correctly implement init + examine, i.e.
685 * no communication with target during init:
687 * XScale
689 int target_examine(void)
691 int retval = ERROR_OK;
692 struct target *target;
694 for (target = all_targets; target; target = target->next) {
695 /* defer examination, but don't skip it */
696 if (!target->tap->enabled) {
697 jtag_register_event_callback(jtag_enable_callback,
698 target);
699 continue;
702 target_call_event_callbacks(target, TARGET_EVENT_EXAMINE_START);
704 retval = target_examine_one(target);
705 if (retval != ERROR_OK)
706 return retval;
708 target_call_event_callbacks(target, TARGET_EVENT_EXAMINE_END);
710 return retval;
713 const char *target_type_name(struct target *target)
715 return target->type->name;
718 static int target_soft_reset_halt(struct target *target)
720 if (!target_was_examined(target)) {
721 LOG_ERROR("Target not examined yet");
722 return ERROR_FAIL;
724 if (!target->type->soft_reset_halt) {
725 LOG_ERROR("Target %s does not support soft_reset_halt",
726 target_name(target));
727 return ERROR_FAIL;
729 return target->type->soft_reset_halt(target);
733 * Downloads a target-specific native code algorithm to the target,
734 * and executes it. * Note that some targets may need to set up, enable,
735 * and tear down a breakpoint (hard or * soft) to detect algorithm
736 * termination, while others may support lower overhead schemes where
737 * soft breakpoints embedded in the algorithm automatically terminate the
738 * algorithm.
740 * @param target used to run the algorithm
741 * @param arch_info target-specific description of the algorithm.
743 int target_run_algorithm(struct target *target,
744 int num_mem_params, struct mem_param *mem_params,
745 int num_reg_params, struct reg_param *reg_param,
746 uint32_t entry_point, uint32_t exit_point,
747 int timeout_ms, void *arch_info)
749 int retval = ERROR_FAIL;
751 if (!target_was_examined(target)) {
752 LOG_ERROR("Target not examined yet");
753 goto done;
755 if (!target->type->run_algorithm) {
756 LOG_ERROR("Target type '%s' does not support %s",
757 target_type_name(target), __func__);
758 goto done;
761 target->running_alg = true;
762 retval = target->type->run_algorithm(target,
763 num_mem_params, mem_params,
764 num_reg_params, reg_param,
765 entry_point, exit_point, timeout_ms, arch_info);
766 target->running_alg = false;
768 done:
769 return retval;
773 * Downloads a target-specific native code algorithm to the target,
774 * executes and leaves it running.
776 * @param target used to run the algorithm
777 * @param arch_info target-specific description of the algorithm.
779 int target_start_algorithm(struct target *target,
780 int num_mem_params, struct mem_param *mem_params,
781 int num_reg_params, struct reg_param *reg_params,
782 uint32_t entry_point, uint32_t exit_point,
783 void *arch_info)
785 int retval = ERROR_FAIL;
787 if (!target_was_examined(target)) {
788 LOG_ERROR("Target not examined yet");
789 goto done;
791 if (!target->type->start_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 already running an algorithm");
798 goto done;
801 target->running_alg = true;
802 retval = target->type->start_algorithm(target,
803 num_mem_params, mem_params,
804 num_reg_params, reg_params,
805 entry_point, exit_point, arch_info);
807 done:
808 return retval;
812 * Waits for an algorithm started with target_start_algorithm() to complete.
814 * @param target used to run the algorithm
815 * @param arch_info target-specific description of the algorithm.
817 int target_wait_algorithm(struct target *target,
818 int num_mem_params, struct mem_param *mem_params,
819 int num_reg_params, struct reg_param *reg_params,
820 uint32_t exit_point, int timeout_ms,
821 void *arch_info)
823 int retval = ERROR_FAIL;
825 if (!target->type->wait_algorithm) {
826 LOG_ERROR("Target type '%s' does not support %s",
827 target_type_name(target), __func__);
828 goto done;
830 if (!target->running_alg) {
831 LOG_ERROR("Target is not running an algorithm");
832 goto done;
835 retval = target->type->wait_algorithm(target,
836 num_mem_params, mem_params,
837 num_reg_params, reg_params,
838 exit_point, timeout_ms, arch_info);
839 if (retval != ERROR_TARGET_TIMEOUT)
840 target->running_alg = false;
842 done:
843 return retval;
847 * Executes a target-specific native code algorithm in the target.
848 * It differs from target_run_algorithm in that the algorithm is asynchronous.
849 * Because of this it requires an compliant algorithm:
850 * see contrib/loaders/flash/stm32f1x.S for example.
852 * @param target used to run the algorithm
855 int target_run_flash_async_algorithm(struct target *target,
856 const uint8_t *buffer, uint32_t count, int block_size,
857 int num_mem_params, struct mem_param *mem_params,
858 int num_reg_params, struct reg_param *reg_params,
859 uint32_t buffer_start, uint32_t buffer_size,
860 uint32_t entry_point, uint32_t exit_point, void *arch_info)
862 int retval;
863 int timeout = 0;
865 /* Set up working area. First word is write pointer, second word is read pointer,
866 * rest is fifo data area. */
867 uint32_t wp_addr = buffer_start;
868 uint32_t rp_addr = buffer_start + 4;
869 uint32_t fifo_start_addr = buffer_start + 8;
870 uint32_t fifo_end_addr = buffer_start + buffer_size;
872 uint32_t wp = fifo_start_addr;
873 uint32_t rp = fifo_start_addr;
875 /* validate block_size is 2^n */
876 assert(!block_size || !(block_size & (block_size - 1)));
878 retval = target_write_u32(target, wp_addr, wp);
879 if (retval != ERROR_OK)
880 return retval;
881 retval = target_write_u32(target, rp_addr, rp);
882 if (retval != ERROR_OK)
883 return retval;
885 /* Start up algorithm on target and let it idle while writing the first chunk */
886 retval = target_start_algorithm(target, num_mem_params, mem_params,
887 num_reg_params, reg_params,
888 entry_point,
889 exit_point,
890 arch_info);
892 if (retval != ERROR_OK) {
893 LOG_ERROR("error starting target flash write algorithm");
894 return retval;
897 while (count > 0) {
899 retval = target_read_u32(target, rp_addr, &rp);
900 if (retval != ERROR_OK) {
901 LOG_ERROR("failed to get read pointer");
902 break;
905 LOG_DEBUG("count 0x%" PRIx32 " wp 0x%" PRIx32 " rp 0x%" PRIx32, count, wp, rp);
907 if (rp == 0) {
908 LOG_ERROR("flash write algorithm aborted by target");
909 retval = ERROR_FLASH_OPERATION_FAILED;
910 break;
913 if ((rp & (block_size - 1)) || rp < fifo_start_addr || rp >= fifo_end_addr) {
914 LOG_ERROR("corrupted fifo read pointer 0x%" PRIx32, rp);
915 break;
918 /* Count the number of bytes available in the fifo without
919 * crossing the wrap around. Make sure to not fill it completely,
920 * because that would make wp == rp and that's the empty condition. */
921 uint32_t thisrun_bytes;
922 if (rp > wp)
923 thisrun_bytes = rp - wp - block_size;
924 else if (rp > fifo_start_addr)
925 thisrun_bytes = fifo_end_addr - wp;
926 else
927 thisrun_bytes = fifo_end_addr - wp - block_size;
929 if (thisrun_bytes == 0) {
930 /* Throttle polling a bit if transfer is (much) faster than flash
931 * programming. The exact delay shouldn't matter as long as it's
932 * less than buffer size / flash speed. This is very unlikely to
933 * run when using high latency connections such as USB. */
934 alive_sleep(10);
936 /* to stop an infinite loop on some targets check and increment a timeout
937 * this issue was observed on a stellaris using the new ICDI interface */
938 if (timeout++ >= 500) {
939 LOG_ERROR("timeout waiting for algorithm, a target reset is recommended");
940 return ERROR_FLASH_OPERATION_FAILED;
942 continue;
945 /* reset our timeout */
946 timeout = 0;
948 /* Limit to the amount of data we actually want to write */
949 if (thisrun_bytes > count * block_size)
950 thisrun_bytes = count * block_size;
952 /* Write data to fifo */
953 retval = target_write_buffer(target, wp, thisrun_bytes, buffer);
954 if (retval != ERROR_OK)
955 break;
957 /* Update counters and wrap write pointer */
958 buffer += thisrun_bytes;
959 count -= thisrun_bytes / block_size;
960 wp += thisrun_bytes;
961 if (wp >= fifo_end_addr)
962 wp = fifo_start_addr;
964 /* Store updated write pointer to target */
965 retval = target_write_u32(target, wp_addr, wp);
966 if (retval != ERROR_OK)
967 break;
970 if (retval != ERROR_OK) {
971 /* abort flash write algorithm on target */
972 target_write_u32(target, wp_addr, 0);
975 int retval2 = target_wait_algorithm(target, num_mem_params, mem_params,
976 num_reg_params, reg_params,
977 exit_point,
978 10000,
979 arch_info);
981 if (retval2 != ERROR_OK) {
982 LOG_ERROR("error waiting for target flash write algorithm");
983 retval = retval2;
986 return retval;
989 int target_read_memory(struct target *target,
990 uint32_t address, uint32_t size, uint32_t count, uint8_t *buffer)
992 if (!target_was_examined(target)) {
993 LOG_ERROR("Target not examined yet");
994 return ERROR_FAIL;
996 return target->type->read_memory(target, address, size, count, buffer);
999 int target_read_phys_memory(struct target *target,
1000 uint32_t address, uint32_t size, uint32_t count, uint8_t *buffer)
1002 if (!target_was_examined(target)) {
1003 LOG_ERROR("Target not examined yet");
1004 return ERROR_FAIL;
1006 return target->type->read_phys_memory(target, address, size, count, buffer);
1009 int target_write_memory(struct target *target,
1010 uint32_t address, uint32_t size, uint32_t count, const uint8_t *buffer)
1012 if (!target_was_examined(target)) {
1013 LOG_ERROR("Target not examined yet");
1014 return ERROR_FAIL;
1016 return target->type->write_memory(target, address, size, count, buffer);
1019 int target_write_phys_memory(struct target *target,
1020 uint32_t address, uint32_t size, uint32_t count, const uint8_t *buffer)
1022 if (!target_was_examined(target)) {
1023 LOG_ERROR("Target not examined yet");
1024 return ERROR_FAIL;
1026 return target->type->write_phys_memory(target, address, size, count, buffer);
1029 int target_add_breakpoint(struct target *target,
1030 struct breakpoint *breakpoint)
1032 if ((target->state != TARGET_HALTED) && (breakpoint->type != BKPT_HARD)) {
1033 LOG_WARNING("target %s is not halted", target_name(target));
1034 return ERROR_TARGET_NOT_HALTED;
1036 return target->type->add_breakpoint(target, breakpoint);
1039 int target_add_context_breakpoint(struct target *target,
1040 struct breakpoint *breakpoint)
1042 if (target->state != TARGET_HALTED) {
1043 LOG_WARNING("target %s is not halted", target_name(target));
1044 return ERROR_TARGET_NOT_HALTED;
1046 return target->type->add_context_breakpoint(target, breakpoint);
1049 int target_add_hybrid_breakpoint(struct target *target,
1050 struct breakpoint *breakpoint)
1052 if (target->state != TARGET_HALTED) {
1053 LOG_WARNING("target %s is not halted", target_name(target));
1054 return ERROR_TARGET_NOT_HALTED;
1056 return target->type->add_hybrid_breakpoint(target, breakpoint);
1059 int target_remove_breakpoint(struct target *target,
1060 struct breakpoint *breakpoint)
1062 return target->type->remove_breakpoint(target, breakpoint);
1065 int target_add_watchpoint(struct target *target,
1066 struct watchpoint *watchpoint)
1068 if (target->state != TARGET_HALTED) {
1069 LOG_WARNING("target %s is not halted", target_name(target));
1070 return ERROR_TARGET_NOT_HALTED;
1072 return target->type->add_watchpoint(target, watchpoint);
1074 int target_remove_watchpoint(struct target *target,
1075 struct watchpoint *watchpoint)
1077 return target->type->remove_watchpoint(target, watchpoint);
1079 int target_hit_watchpoint(struct target *target,
1080 struct watchpoint **hit_watchpoint)
1082 if (target->state != TARGET_HALTED) {
1083 LOG_WARNING("target %s is not halted", target->cmd_name);
1084 return ERROR_TARGET_NOT_HALTED;
1087 if (target->type->hit_watchpoint == NULL) {
1088 /* For backward compatible, if hit_watchpoint is not implemented,
1089 * return ERROR_FAIL such that gdb_server will not take the nonsense
1090 * information. */
1091 return ERROR_FAIL;
1094 return target->type->hit_watchpoint(target, hit_watchpoint);
1097 int target_get_gdb_reg_list(struct target *target,
1098 struct reg **reg_list[], int *reg_list_size,
1099 enum target_register_class reg_class)
1101 return target->type->get_gdb_reg_list(target, reg_list, reg_list_size, reg_class);
1103 int target_step(struct target *target,
1104 int current, uint32_t address, int handle_breakpoints)
1106 return target->type->step(target, current, address, handle_breakpoints);
1109 int target_get_gdb_fileio_info(struct target *target, struct gdb_fileio_info *fileio_info)
1111 if (target->state != TARGET_HALTED) {
1112 LOG_WARNING("target %s is not halted", target->cmd_name);
1113 return ERROR_TARGET_NOT_HALTED;
1115 return target->type->get_gdb_fileio_info(target, fileio_info);
1118 int target_gdb_fileio_end(struct target *target, int retcode, int fileio_errno, bool ctrl_c)
1120 if (target->state != TARGET_HALTED) {
1121 LOG_WARNING("target %s is not halted", target->cmd_name);
1122 return ERROR_TARGET_NOT_HALTED;
1124 return target->type->gdb_fileio_end(target, retcode, fileio_errno, ctrl_c);
1127 int target_profiling(struct target *target, uint32_t *samples,
1128 uint32_t max_num_samples, uint32_t *num_samples, uint32_t seconds)
1130 if (target->state != TARGET_HALTED) {
1131 LOG_WARNING("target %s is not halted", target->cmd_name);
1132 return ERROR_TARGET_NOT_HALTED;
1134 return target->type->profiling(target, samples, max_num_samples,
1135 num_samples, seconds);
1139 * Reset the @c examined flag for the given target.
1140 * Pure paranoia -- targets are zeroed on allocation.
1142 static void target_reset_examined(struct target *target)
1144 target->examined = false;
1147 static int err_read_phys_memory(struct target *target, uint32_t address,
1148 uint32_t size, uint32_t count, uint8_t *buffer)
1150 LOG_ERROR("Not implemented: %s", __func__);
1151 return ERROR_FAIL;
1154 static int err_write_phys_memory(struct target *target, uint32_t address,
1155 uint32_t size, uint32_t count, const uint8_t *buffer)
1157 LOG_ERROR("Not implemented: %s", __func__);
1158 return ERROR_FAIL;
1161 static int handle_target(void *priv);
1163 static int target_init_one(struct command_context *cmd_ctx,
1164 struct target *target)
1166 target_reset_examined(target);
1168 struct target_type *type = target->type;
1169 if (type->examine == NULL)
1170 type->examine = default_examine;
1172 if (type->check_reset == NULL)
1173 type->check_reset = default_check_reset;
1175 assert(type->init_target != NULL);
1177 int retval = type->init_target(cmd_ctx, target);
1178 if (ERROR_OK != retval) {
1179 LOG_ERROR("target '%s' init failed", target_name(target));
1180 return retval;
1183 /* Sanity-check MMU support ... stub in what we must, to help
1184 * implement it in stages, but warn if we need to do so.
1186 if (type->mmu) {
1187 if (type->write_phys_memory == NULL) {
1188 LOG_ERROR("type '%s' is missing write_phys_memory",
1189 type->name);
1190 type->write_phys_memory = err_write_phys_memory;
1192 if (type->read_phys_memory == NULL) {
1193 LOG_ERROR("type '%s' is missing read_phys_memory",
1194 type->name);
1195 type->read_phys_memory = err_read_phys_memory;
1197 if (type->virt2phys == NULL) {
1198 LOG_ERROR("type '%s' is missing virt2phys", type->name);
1199 type->virt2phys = identity_virt2phys;
1201 } else {
1202 /* Make sure no-MMU targets all behave the same: make no
1203 * distinction between physical and virtual addresses, and
1204 * ensure that virt2phys() is always an identity mapping.
1206 if (type->write_phys_memory || type->read_phys_memory || type->virt2phys)
1207 LOG_WARNING("type '%s' has bad MMU hooks", type->name);
1209 type->mmu = no_mmu;
1210 type->write_phys_memory = type->write_memory;
1211 type->read_phys_memory = type->read_memory;
1212 type->virt2phys = identity_virt2phys;
1215 if (target->type->read_buffer == NULL)
1216 target->type->read_buffer = target_read_buffer_default;
1218 if (target->type->write_buffer == NULL)
1219 target->type->write_buffer = target_write_buffer_default;
1221 if (target->type->get_gdb_fileio_info == NULL)
1222 target->type->get_gdb_fileio_info = target_get_gdb_fileio_info_default;
1224 if (target->type->gdb_fileio_end == NULL)
1225 target->type->gdb_fileio_end = target_gdb_fileio_end_default;
1227 if (target->type->profiling == NULL)
1228 target->type->profiling = target_profiling_default;
1230 return ERROR_OK;
1233 static int target_init(struct command_context *cmd_ctx)
1235 struct target *target;
1236 int retval;
1238 for (target = all_targets; target; target = target->next) {
1239 retval = target_init_one(cmd_ctx, target);
1240 if (ERROR_OK != retval)
1241 return retval;
1244 if (!all_targets)
1245 return ERROR_OK;
1247 retval = target_register_user_commands(cmd_ctx);
1248 if (ERROR_OK != retval)
1249 return retval;
1251 retval = target_register_timer_callback(&handle_target,
1252 polling_interval, 1, cmd_ctx->interp);
1253 if (ERROR_OK != retval)
1254 return retval;
1256 return ERROR_OK;
1259 COMMAND_HANDLER(handle_target_init_command)
1261 int retval;
1263 if (CMD_ARGC != 0)
1264 return ERROR_COMMAND_SYNTAX_ERROR;
1266 static bool target_initialized;
1267 if (target_initialized) {
1268 LOG_INFO("'target init' has already been called");
1269 return ERROR_OK;
1271 target_initialized = true;
1273 retval = command_run_line(CMD_CTX, "init_targets");
1274 if (ERROR_OK != retval)
1275 return retval;
1277 retval = command_run_line(CMD_CTX, "init_target_events");
1278 if (ERROR_OK != retval)
1279 return retval;
1281 retval = command_run_line(CMD_CTX, "init_board");
1282 if (ERROR_OK != retval)
1283 return retval;
1285 LOG_DEBUG("Initializing targets...");
1286 return target_init(CMD_CTX);
1289 int target_register_event_callback(int (*callback)(struct target *target,
1290 enum target_event event, void *priv), void *priv)
1292 struct target_event_callback **callbacks_p = &target_event_callbacks;
1294 if (callback == NULL)
1295 return ERROR_COMMAND_SYNTAX_ERROR;
1297 if (*callbacks_p) {
1298 while ((*callbacks_p)->next)
1299 callbacks_p = &((*callbacks_p)->next);
1300 callbacks_p = &((*callbacks_p)->next);
1303 (*callbacks_p) = malloc(sizeof(struct target_event_callback));
1304 (*callbacks_p)->callback = callback;
1305 (*callbacks_p)->priv = priv;
1306 (*callbacks_p)->next = NULL;
1308 return ERROR_OK;
1311 int target_register_timer_callback(int (*callback)(void *priv), int time_ms, int periodic, void *priv)
1313 struct target_timer_callback **callbacks_p = &target_timer_callbacks;
1314 struct timeval now;
1316 if (callback == NULL)
1317 return ERROR_COMMAND_SYNTAX_ERROR;
1319 if (*callbacks_p) {
1320 while ((*callbacks_p)->next)
1321 callbacks_p = &((*callbacks_p)->next);
1322 callbacks_p = &((*callbacks_p)->next);
1325 (*callbacks_p) = malloc(sizeof(struct target_timer_callback));
1326 (*callbacks_p)->callback = callback;
1327 (*callbacks_p)->periodic = periodic;
1328 (*callbacks_p)->time_ms = time_ms;
1330 gettimeofday(&now, NULL);
1331 (*callbacks_p)->when.tv_usec = now.tv_usec + (time_ms % 1000) * 1000;
1332 time_ms -= (time_ms % 1000);
1333 (*callbacks_p)->when.tv_sec = now.tv_sec + (time_ms / 1000);
1334 if ((*callbacks_p)->when.tv_usec > 1000000) {
1335 (*callbacks_p)->when.tv_usec = (*callbacks_p)->when.tv_usec - 1000000;
1336 (*callbacks_p)->when.tv_sec += 1;
1339 (*callbacks_p)->priv = priv;
1340 (*callbacks_p)->next = NULL;
1342 return ERROR_OK;
1345 int target_unregister_event_callback(int (*callback)(struct target *target,
1346 enum target_event event, void *priv), void *priv)
1348 struct target_event_callback **p = &target_event_callbacks;
1349 struct target_event_callback *c = target_event_callbacks;
1351 if (callback == NULL)
1352 return ERROR_COMMAND_SYNTAX_ERROR;
1354 while (c) {
1355 struct target_event_callback *next = c->next;
1356 if ((c->callback == callback) && (c->priv == priv)) {
1357 *p = next;
1358 free(c);
1359 return ERROR_OK;
1360 } else
1361 p = &(c->next);
1362 c = next;
1365 return ERROR_OK;
1368 int target_unregister_timer_callback(int (*callback)(void *priv), void *priv)
1370 struct target_timer_callback **p = &target_timer_callbacks;
1371 struct target_timer_callback *c = target_timer_callbacks;
1373 if (callback == NULL)
1374 return ERROR_COMMAND_SYNTAX_ERROR;
1376 while (c) {
1377 struct target_timer_callback *next = c->next;
1378 if ((c->callback == callback) && (c->priv == priv)) {
1379 *p = next;
1380 free(c);
1381 return ERROR_OK;
1382 } else
1383 p = &(c->next);
1384 c = next;
1387 return ERROR_OK;
1390 int target_call_event_callbacks(struct target *target, enum target_event event)
1392 struct target_event_callback *callback = target_event_callbacks;
1393 struct target_event_callback *next_callback;
1395 if (event == TARGET_EVENT_HALTED) {
1396 /* execute early halted first */
1397 target_call_event_callbacks(target, TARGET_EVENT_GDB_HALT);
1400 LOG_DEBUG("target event %i (%s)", event,
1401 Jim_Nvp_value2name_simple(nvp_target_event, event)->name);
1403 target_handle_event(target, event);
1405 while (callback) {
1406 next_callback = callback->next;
1407 callback->callback(target, event, callback->priv);
1408 callback = next_callback;
1411 return ERROR_OK;
1414 static int target_timer_callback_periodic_restart(
1415 struct target_timer_callback *cb, struct timeval *now)
1417 int time_ms = cb->time_ms;
1418 cb->when.tv_usec = now->tv_usec + (time_ms % 1000) * 1000;
1419 time_ms -= (time_ms % 1000);
1420 cb->when.tv_sec = now->tv_sec + time_ms / 1000;
1421 if (cb->when.tv_usec > 1000000) {
1422 cb->when.tv_usec = cb->when.tv_usec - 1000000;
1423 cb->when.tv_sec += 1;
1425 return ERROR_OK;
1428 static int target_call_timer_callback(struct target_timer_callback *cb,
1429 struct timeval *now)
1431 cb->callback(cb->priv);
1433 if (cb->periodic)
1434 return target_timer_callback_periodic_restart(cb, now);
1436 return target_unregister_timer_callback(cb->callback, cb->priv);
1439 static int target_call_timer_callbacks_check_time(int checktime)
1441 keep_alive();
1443 struct timeval now;
1444 gettimeofday(&now, NULL);
1446 struct target_timer_callback *callback = target_timer_callbacks;
1447 while (callback) {
1448 /* cleaning up may unregister and free this callback */
1449 struct target_timer_callback *next_callback = callback->next;
1451 bool call_it = callback->callback &&
1452 ((!checktime && callback->periodic) ||
1453 now.tv_sec > callback->when.tv_sec ||
1454 (now.tv_sec == callback->when.tv_sec &&
1455 now.tv_usec >= callback->when.tv_usec));
1457 if (call_it) {
1458 int retval = target_call_timer_callback(callback, &now);
1459 if (retval != ERROR_OK)
1460 return retval;
1463 callback = next_callback;
1466 return ERROR_OK;
1469 int target_call_timer_callbacks(void)
1471 return target_call_timer_callbacks_check_time(1);
1474 /* invoke periodic callbacks immediately */
1475 int target_call_timer_callbacks_now(void)
1477 return target_call_timer_callbacks_check_time(0);
1480 /* Prints the working area layout for debug purposes */
1481 static void print_wa_layout(struct target *target)
1483 struct working_area *c = target->working_areas;
1485 while (c) {
1486 LOG_DEBUG("%c%c 0x%08"PRIx32"-0x%08"PRIx32" (%"PRIu32" bytes)",
1487 c->backup ? 'b' : ' ', c->free ? ' ' : '*',
1488 c->address, c->address + c->size - 1, c->size);
1489 c = c->next;
1493 /* Reduce area to size bytes, create a new free area from the remaining bytes, if any. */
1494 static void target_split_working_area(struct working_area *area, uint32_t size)
1496 assert(area->free); /* Shouldn't split an allocated area */
1497 assert(size <= area->size); /* Caller should guarantee this */
1499 /* Split only if not already the right size */
1500 if (size < area->size) {
1501 struct working_area *new_wa = malloc(sizeof(*new_wa));
1503 if (new_wa == NULL)
1504 return;
1506 new_wa->next = area->next;
1507 new_wa->size = area->size - size;
1508 new_wa->address = area->address + size;
1509 new_wa->backup = NULL;
1510 new_wa->user = NULL;
1511 new_wa->free = true;
1513 area->next = new_wa;
1514 area->size = size;
1516 /* If backup memory was allocated to this area, it has the wrong size
1517 * now so free it and it will be reallocated if/when needed */
1518 if (area->backup) {
1519 free(area->backup);
1520 area->backup = NULL;
1525 /* Merge all adjacent free areas into one */
1526 static void target_merge_working_areas(struct target *target)
1528 struct working_area *c = target->working_areas;
1530 while (c && c->next) {
1531 assert(c->next->address == c->address + c->size); /* This is an invariant */
1533 /* Find two adjacent free areas */
1534 if (c->free && c->next->free) {
1535 /* Merge the last into the first */
1536 c->size += c->next->size;
1538 /* Remove the last */
1539 struct working_area *to_be_freed = c->next;
1540 c->next = c->next->next;
1541 if (to_be_freed->backup)
1542 free(to_be_freed->backup);
1543 free(to_be_freed);
1545 /* If backup memory was allocated to the remaining area, it's has
1546 * the wrong size now */
1547 if (c->backup) {
1548 free(c->backup);
1549 c->backup = NULL;
1551 } else {
1552 c = c->next;
1557 int target_alloc_working_area_try(struct target *target, uint32_t size, struct working_area **area)
1559 /* Reevaluate working area address based on MMU state*/
1560 if (target->working_areas == NULL) {
1561 int retval;
1562 int enabled;
1564 retval = target->type->mmu(target, &enabled);
1565 if (retval != ERROR_OK)
1566 return retval;
1568 if (!enabled) {
1569 if (target->working_area_phys_spec) {
1570 LOG_DEBUG("MMU disabled, using physical "
1571 "address for working memory 0x%08"PRIx32,
1572 target->working_area_phys);
1573 target->working_area = target->working_area_phys;
1574 } else {
1575 LOG_ERROR("No working memory available. "
1576 "Specify -work-area-phys to target.");
1577 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
1579 } else {
1580 if (target->working_area_virt_spec) {
1581 LOG_DEBUG("MMU enabled, using virtual "
1582 "address for working memory 0x%08"PRIx32,
1583 target->working_area_virt);
1584 target->working_area = target->working_area_virt;
1585 } else {
1586 LOG_ERROR("No working memory available. "
1587 "Specify -work-area-virt to target.");
1588 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
1592 /* Set up initial working area on first call */
1593 struct working_area *new_wa = malloc(sizeof(*new_wa));
1594 if (new_wa) {
1595 new_wa->next = NULL;
1596 new_wa->size = target->working_area_size & ~3UL; /* 4-byte align */
1597 new_wa->address = target->working_area;
1598 new_wa->backup = NULL;
1599 new_wa->user = NULL;
1600 new_wa->free = true;
1603 target->working_areas = new_wa;
1606 /* only allocate multiples of 4 byte */
1607 if (size % 4)
1608 size = (size + 3) & (~3UL);
1610 struct working_area *c = target->working_areas;
1612 /* Find the first large enough working area */
1613 while (c) {
1614 if (c->free && c->size >= size)
1615 break;
1616 c = c->next;
1619 if (c == NULL)
1620 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
1622 /* Split the working area into the requested size */
1623 target_split_working_area(c, size);
1625 LOG_DEBUG("allocated new working area of %"PRIu32" bytes at address 0x%08"PRIx32, size, c->address);
1627 if (target->backup_working_area) {
1628 if (c->backup == NULL) {
1629 c->backup = malloc(c->size);
1630 if (c->backup == NULL)
1631 return ERROR_FAIL;
1634 int retval = target_read_memory(target, c->address, 4, c->size / 4, c->backup);
1635 if (retval != ERROR_OK)
1636 return retval;
1639 /* mark as used, and return the new (reused) area */
1640 c->free = false;
1641 *area = c;
1643 /* user pointer */
1644 c->user = area;
1646 print_wa_layout(target);
1648 return ERROR_OK;
1651 int target_alloc_working_area(struct target *target, uint32_t size, struct working_area **area)
1653 int retval;
1655 retval = target_alloc_working_area_try(target, size, area);
1656 if (retval == ERROR_TARGET_RESOURCE_NOT_AVAILABLE)
1657 LOG_WARNING("not enough working area available(requested %"PRIu32")", size);
1658 return retval;
1662 static int target_restore_working_area(struct target *target, struct working_area *area)
1664 int retval = ERROR_OK;
1666 if (target->backup_working_area && area->backup != NULL) {
1667 retval = target_write_memory(target, area->address, 4, area->size / 4, area->backup);
1668 if (retval != ERROR_OK)
1669 LOG_ERROR("failed to restore %"PRIu32" bytes of working area at address 0x%08"PRIx32,
1670 area->size, area->address);
1673 return retval;
1676 /* Restore the area's backup memory, if any, and return the area to the allocation pool */
1677 static int target_free_working_area_restore(struct target *target, struct working_area *area, int restore)
1679 int retval = ERROR_OK;
1681 if (area->free)
1682 return retval;
1684 if (restore) {
1685 retval = target_restore_working_area(target, area);
1686 /* REVISIT: Perhaps the area should be freed even if restoring fails. */
1687 if (retval != ERROR_OK)
1688 return retval;
1691 area->free = true;
1693 LOG_DEBUG("freed %"PRIu32" bytes of working area at address 0x%08"PRIx32,
1694 area->size, area->address);
1696 /* mark user pointer invalid */
1697 /* TODO: Is this really safe? It points to some previous caller's memory.
1698 * How could we know that the area pointer is still in that place and not
1699 * some other vital data? What's the purpose of this, anyway? */
1700 *area->user = NULL;
1701 area->user = NULL;
1703 target_merge_working_areas(target);
1705 print_wa_layout(target);
1707 return retval;
1710 int target_free_working_area(struct target *target, struct working_area *area)
1712 return target_free_working_area_restore(target, area, 1);
1715 /* free resources and restore memory, if restoring memory fails,
1716 * free up resources anyway
1718 static void target_free_all_working_areas_restore(struct target *target, int restore)
1720 struct working_area *c = target->working_areas;
1722 LOG_DEBUG("freeing all working areas");
1724 /* Loop through all areas, restoring the allocated ones and marking them as free */
1725 while (c) {
1726 if (!c->free) {
1727 if (restore)
1728 target_restore_working_area(target, c);
1729 c->free = true;
1730 *c->user = NULL; /* Same as above */
1731 c->user = NULL;
1733 c = c->next;
1736 /* Run a merge pass to combine all areas into one */
1737 target_merge_working_areas(target);
1739 print_wa_layout(target);
1742 void target_free_all_working_areas(struct target *target)
1744 target_free_all_working_areas_restore(target, 1);
1747 /* Find the largest number of bytes that can be allocated */
1748 uint32_t target_get_working_area_avail(struct target *target)
1750 struct working_area *c = target->working_areas;
1751 uint32_t max_size = 0;
1753 if (c == NULL)
1754 return target->working_area_size;
1756 while (c) {
1757 if (c->free && max_size < c->size)
1758 max_size = c->size;
1760 c = c->next;
1763 return max_size;
1766 int target_arch_state(struct target *target)
1768 int retval;
1769 if (target == NULL) {
1770 LOG_USER("No target has been configured");
1771 return ERROR_OK;
1774 LOG_USER("target state: %s", target_state_name(target));
1776 if (target->state != TARGET_HALTED)
1777 return ERROR_OK;
1779 retval = target->type->arch_state(target);
1780 return retval;
1783 static int target_get_gdb_fileio_info_default(struct target *target,
1784 struct gdb_fileio_info *fileio_info)
1786 /* If target does not support semi-hosting function, target
1787 has no need to provide .get_gdb_fileio_info callback.
1788 It just return ERROR_FAIL and gdb_server will return "Txx"
1789 as target halted every time. */
1790 return ERROR_FAIL;
1793 static int target_gdb_fileio_end_default(struct target *target,
1794 int retcode, int fileio_errno, bool ctrl_c)
1796 return ERROR_OK;
1799 static int target_profiling_default(struct target *target, uint32_t *samples,
1800 uint32_t max_num_samples, uint32_t *num_samples, uint32_t seconds)
1802 struct timeval timeout, now;
1804 gettimeofday(&timeout, NULL);
1805 timeval_add_time(&timeout, seconds, 0);
1807 LOG_INFO("Starting profiling. Halting and resuming the"
1808 " target as often as we can...");
1810 uint32_t sample_count = 0;
1811 /* hopefully it is safe to cache! We want to stop/restart as quickly as possible. */
1812 struct reg *reg = register_get_by_name(target->reg_cache, "pc", 1);
1814 int retval = ERROR_OK;
1815 for (;;) {
1816 target_poll(target);
1817 if (target->state == TARGET_HALTED) {
1818 uint32_t t = *((uint32_t *)reg->value);
1819 samples[sample_count++] = t;
1820 /* current pc, addr = 0, do not handle breakpoints, not debugging */
1821 retval = target_resume(target, 1, 0, 0, 0);
1822 target_poll(target);
1823 alive_sleep(10); /* sleep 10ms, i.e. <100 samples/second. */
1824 } else if (target->state == TARGET_RUNNING) {
1825 /* We want to quickly sample the PC. */
1826 retval = target_halt(target);
1827 } else {
1828 LOG_INFO("Target not halted or running");
1829 retval = ERROR_OK;
1830 break;
1833 if (retval != ERROR_OK)
1834 break;
1836 gettimeofday(&now, NULL);
1837 if ((sample_count >= max_num_samples) ||
1838 ((now.tv_sec >= timeout.tv_sec) && (now.tv_usec >= timeout.tv_usec))) {
1839 LOG_INFO("Profiling completed. %" PRIu32 " samples.", sample_count);
1840 break;
1844 *num_samples = sample_count;
1845 return retval;
1848 /* Single aligned words are guaranteed to use 16 or 32 bit access
1849 * mode respectively, otherwise data is handled as quickly as
1850 * possible
1852 int target_write_buffer(struct target *target, uint32_t address, uint32_t size, const uint8_t *buffer)
1854 LOG_DEBUG("writing buffer of %i byte at 0x%8.8x",
1855 (int)size, (unsigned)address);
1857 if (!target_was_examined(target)) {
1858 LOG_ERROR("Target not examined yet");
1859 return ERROR_FAIL;
1862 if (size == 0)
1863 return ERROR_OK;
1865 if ((address + size - 1) < address) {
1866 /* GDB can request this when e.g. PC is 0xfffffffc*/
1867 LOG_ERROR("address + size wrapped(0x%08x, 0x%08x)",
1868 (unsigned)address,
1869 (unsigned)size);
1870 return ERROR_FAIL;
1873 return target->type->write_buffer(target, address, size, buffer);
1876 static int target_write_buffer_default(struct target *target, uint32_t address, uint32_t count, const uint8_t *buffer)
1878 uint32_t size;
1880 /* Align up to maximum 4 bytes. The loop condition makes sure the next pass
1881 * will have something to do with the size we leave to it. */
1882 for (size = 1; size < 4 && count >= size * 2 + (address & size); size *= 2) {
1883 if (address & size) {
1884 int retval = target_write_memory(target, address, size, 1, buffer);
1885 if (retval != ERROR_OK)
1886 return retval;
1887 address += size;
1888 count -= size;
1889 buffer += size;
1893 /* Write the data with as large access size as possible. */
1894 for (; size > 0; size /= 2) {
1895 uint32_t aligned = count - count % size;
1896 if (aligned > 0) {
1897 int retval = target_write_memory(target, address, size, aligned / size, buffer);
1898 if (retval != ERROR_OK)
1899 return retval;
1900 address += aligned;
1901 count -= aligned;
1902 buffer += aligned;
1906 return ERROR_OK;
1909 /* Single aligned words are guaranteed to use 16 or 32 bit access
1910 * mode respectively, otherwise data is handled as quickly as
1911 * possible
1913 int target_read_buffer(struct target *target, uint32_t address, uint32_t size, uint8_t *buffer)
1915 LOG_DEBUG("reading buffer of %i byte at 0x%8.8x",
1916 (int)size, (unsigned)address);
1918 if (!target_was_examined(target)) {
1919 LOG_ERROR("Target not examined yet");
1920 return ERROR_FAIL;
1923 if (size == 0)
1924 return ERROR_OK;
1926 if ((address + size - 1) < address) {
1927 /* GDB can request this when e.g. PC is 0xfffffffc*/
1928 LOG_ERROR("address + size wrapped(0x%08" PRIx32 ", 0x%08" PRIx32 ")",
1929 address,
1930 size);
1931 return ERROR_FAIL;
1934 return target->type->read_buffer(target, address, size, buffer);
1937 static int target_read_buffer_default(struct target *target, uint32_t address, uint32_t count, uint8_t *buffer)
1939 uint32_t size;
1941 /* Align up to maximum 4 bytes. The loop condition makes sure the next pass
1942 * will have something to do with the size we leave to it. */
1943 for (size = 1; size < 4 && count >= size * 2 + (address & size); size *= 2) {
1944 if (address & size) {
1945 int retval = target_read_memory(target, address, size, 1, buffer);
1946 if (retval != ERROR_OK)
1947 return retval;
1948 address += size;
1949 count -= size;
1950 buffer += size;
1954 /* Read the data with as large access size as possible. */
1955 for (; size > 0; size /= 2) {
1956 uint32_t aligned = count - count % size;
1957 if (aligned > 0) {
1958 int retval = target_read_memory(target, address, size, aligned / size, buffer);
1959 if (retval != ERROR_OK)
1960 return retval;
1961 address += aligned;
1962 count -= aligned;
1963 buffer += aligned;
1967 return ERROR_OK;
1970 int target_checksum_memory(struct target *target, uint32_t address, uint32_t size, uint32_t* crc)
1972 uint8_t *buffer;
1973 int retval;
1974 uint32_t i;
1975 uint32_t checksum = 0;
1976 if (!target_was_examined(target)) {
1977 LOG_ERROR("Target not examined yet");
1978 return ERROR_FAIL;
1981 retval = target->type->checksum_memory(target, address, size, &checksum);
1982 if (retval != ERROR_OK) {
1983 buffer = malloc(size);
1984 if (buffer == NULL) {
1985 LOG_ERROR("error allocating buffer for section (%d bytes)", (int)size);
1986 return ERROR_COMMAND_SYNTAX_ERROR;
1988 retval = target_read_buffer(target, address, size, buffer);
1989 if (retval != ERROR_OK) {
1990 free(buffer);
1991 return retval;
1994 /* convert to target endianness */
1995 for (i = 0; i < (size/sizeof(uint32_t)); i++) {
1996 uint32_t target_data;
1997 target_data = target_buffer_get_u32(target, &buffer[i*sizeof(uint32_t)]);
1998 target_buffer_set_u32(target, &buffer[i*sizeof(uint32_t)], target_data);
2001 retval = image_calculate_checksum(buffer, size, &checksum);
2002 free(buffer);
2005 *crc = checksum;
2007 return retval;
2010 int target_blank_check_memory(struct target *target, uint32_t address, uint32_t size, uint32_t* blank)
2012 int retval;
2013 if (!target_was_examined(target)) {
2014 LOG_ERROR("Target not examined yet");
2015 return ERROR_FAIL;
2018 if (target->type->blank_check_memory == 0)
2019 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
2021 retval = target->type->blank_check_memory(target, address, size, blank);
2023 return retval;
2026 int target_read_u64(struct target *target, uint64_t address, uint64_t *value)
2028 uint8_t value_buf[8];
2029 if (!target_was_examined(target)) {
2030 LOG_ERROR("Target not examined yet");
2031 return ERROR_FAIL;
2034 int retval = target_read_memory(target, address, 8, 1, value_buf);
2036 if (retval == ERROR_OK) {
2037 *value = target_buffer_get_u64(target, value_buf);
2038 LOG_DEBUG("address: 0x%" PRIx64 ", value: 0x%16.16" PRIx64 "",
2039 address,
2040 *value);
2041 } else {
2042 *value = 0x0;
2043 LOG_DEBUG("address: 0x%" PRIx64 " failed",
2044 address);
2047 return retval;
2050 int target_read_u32(struct target *target, uint32_t address, uint32_t *value)
2052 uint8_t value_buf[4];
2053 if (!target_was_examined(target)) {
2054 LOG_ERROR("Target not examined yet");
2055 return ERROR_FAIL;
2058 int retval = target_read_memory(target, address, 4, 1, value_buf);
2060 if (retval == ERROR_OK) {
2061 *value = target_buffer_get_u32(target, value_buf);
2062 LOG_DEBUG("address: 0x%8.8" PRIx32 ", value: 0x%8.8" PRIx32 "",
2063 address,
2064 *value);
2065 } else {
2066 *value = 0x0;
2067 LOG_DEBUG("address: 0x%8.8" PRIx32 " failed",
2068 address);
2071 return retval;
2074 int target_read_u16(struct target *target, uint32_t address, uint16_t *value)
2076 uint8_t value_buf[2];
2077 if (!target_was_examined(target)) {
2078 LOG_ERROR("Target not examined yet");
2079 return ERROR_FAIL;
2082 int retval = target_read_memory(target, address, 2, 1, value_buf);
2084 if (retval == ERROR_OK) {
2085 *value = target_buffer_get_u16(target, value_buf);
2086 LOG_DEBUG("address: 0x%8.8" PRIx32 ", value: 0x%4.4x",
2087 address,
2088 *value);
2089 } else {
2090 *value = 0x0;
2091 LOG_DEBUG("address: 0x%8.8" PRIx32 " failed",
2092 address);
2095 return retval;
2098 int target_read_u8(struct target *target, uint32_t address, uint8_t *value)
2100 if (!target_was_examined(target)) {
2101 LOG_ERROR("Target not examined yet");
2102 return ERROR_FAIL;
2105 int retval = target_read_memory(target, address, 1, 1, value);
2107 if (retval == ERROR_OK) {
2108 LOG_DEBUG("address: 0x%8.8" PRIx32 ", value: 0x%2.2x",
2109 address,
2110 *value);
2111 } else {
2112 *value = 0x0;
2113 LOG_DEBUG("address: 0x%8.8" PRIx32 " failed",
2114 address);
2117 return retval;
2120 int target_write_u64(struct target *target, uint64_t address, uint64_t value)
2122 int retval;
2123 uint8_t value_buf[8];
2124 if (!target_was_examined(target)) {
2125 LOG_ERROR("Target not examined yet");
2126 return ERROR_FAIL;
2129 LOG_DEBUG("address: 0x%" PRIx64 ", value: 0x%16.16" PRIx64 "",
2130 address,
2131 value);
2133 target_buffer_set_u64(target, value_buf, value);
2134 retval = target_write_memory(target, address, 8, 1, value_buf);
2135 if (retval != ERROR_OK)
2136 LOG_DEBUG("failed: %i", retval);
2138 return retval;
2141 int target_write_u32(struct target *target, uint32_t address, uint32_t value)
2143 int retval;
2144 uint8_t value_buf[4];
2145 if (!target_was_examined(target)) {
2146 LOG_ERROR("Target not examined yet");
2147 return ERROR_FAIL;
2150 LOG_DEBUG("address: 0x%8.8" PRIx32 ", value: 0x%8.8" PRIx32 "",
2151 address,
2152 value);
2154 target_buffer_set_u32(target, value_buf, value);
2155 retval = target_write_memory(target, address, 4, 1, value_buf);
2156 if (retval != ERROR_OK)
2157 LOG_DEBUG("failed: %i", retval);
2159 return retval;
2162 int target_write_u16(struct target *target, uint32_t address, uint16_t value)
2164 int retval;
2165 uint8_t value_buf[2];
2166 if (!target_was_examined(target)) {
2167 LOG_ERROR("Target not examined yet");
2168 return ERROR_FAIL;
2171 LOG_DEBUG("address: 0x%8.8" PRIx32 ", value: 0x%8.8x",
2172 address,
2173 value);
2175 target_buffer_set_u16(target, value_buf, value);
2176 retval = target_write_memory(target, address, 2, 1, value_buf);
2177 if (retval != ERROR_OK)
2178 LOG_DEBUG("failed: %i", retval);
2180 return retval;
2183 int target_write_u8(struct target *target, uint32_t address, uint8_t value)
2185 int retval;
2186 if (!target_was_examined(target)) {
2187 LOG_ERROR("Target not examined yet");
2188 return ERROR_FAIL;
2191 LOG_DEBUG("address: 0x%8.8" PRIx32 ", value: 0x%2.2x",
2192 address, value);
2194 retval = target_write_memory(target, address, 1, 1, &value);
2195 if (retval != ERROR_OK)
2196 LOG_DEBUG("failed: %i", retval);
2198 return retval;
2201 static int find_target(struct command_context *cmd_ctx, const char *name)
2203 struct target *target = get_target(name);
2204 if (target == NULL) {
2205 LOG_ERROR("Target: %s is unknown, try one of:\n", name);
2206 return ERROR_FAIL;
2208 if (!target->tap->enabled) {
2209 LOG_USER("Target: TAP %s is disabled, "
2210 "can't be the current target\n",
2211 target->tap->dotted_name);
2212 return ERROR_FAIL;
2215 cmd_ctx->current_target = target->target_number;
2216 return ERROR_OK;
2220 COMMAND_HANDLER(handle_targets_command)
2222 int retval = ERROR_OK;
2223 if (CMD_ARGC == 1) {
2224 retval = find_target(CMD_CTX, CMD_ARGV[0]);
2225 if (retval == ERROR_OK) {
2226 /* we're done! */
2227 return retval;
2231 struct target *target = all_targets;
2232 command_print(CMD_CTX, " TargetName Type Endian TapName State ");
2233 command_print(CMD_CTX, "-- ------------------ ---------- ------ ------------------ ------------");
2234 while (target) {
2235 const char *state;
2236 char marker = ' ';
2238 if (target->tap->enabled)
2239 state = target_state_name(target);
2240 else
2241 state = "tap-disabled";
2243 if (CMD_CTX->current_target == target->target_number)
2244 marker = '*';
2246 /* keep columns lined up to match the headers above */
2247 command_print(CMD_CTX,
2248 "%2d%c %-18s %-10s %-6s %-18s %s",
2249 target->target_number,
2250 marker,
2251 target_name(target),
2252 target_type_name(target),
2253 Jim_Nvp_value2name_simple(nvp_target_endian,
2254 target->endianness)->name,
2255 target->tap->dotted_name,
2256 state);
2257 target = target->next;
2260 return retval;
2263 /* every 300ms we check for reset & powerdropout and issue a "reset halt" if so. */
2265 static int powerDropout;
2266 static int srstAsserted;
2268 static int runPowerRestore;
2269 static int runPowerDropout;
2270 static int runSrstAsserted;
2271 static int runSrstDeasserted;
2273 static int sense_handler(void)
2275 static int prevSrstAsserted;
2276 static int prevPowerdropout;
2278 int retval = jtag_power_dropout(&powerDropout);
2279 if (retval != ERROR_OK)
2280 return retval;
2282 int powerRestored;
2283 powerRestored = prevPowerdropout && !powerDropout;
2284 if (powerRestored)
2285 runPowerRestore = 1;
2287 long long current = timeval_ms();
2288 static long long lastPower;
2289 int waitMore = lastPower + 2000 > current;
2290 if (powerDropout && !waitMore) {
2291 runPowerDropout = 1;
2292 lastPower = current;
2295 retval = jtag_srst_asserted(&srstAsserted);
2296 if (retval != ERROR_OK)
2297 return retval;
2299 int srstDeasserted;
2300 srstDeasserted = prevSrstAsserted && !srstAsserted;
2302 static long long lastSrst;
2303 waitMore = lastSrst + 2000 > current;
2304 if (srstDeasserted && !waitMore) {
2305 runSrstDeasserted = 1;
2306 lastSrst = current;
2309 if (!prevSrstAsserted && srstAsserted)
2310 runSrstAsserted = 1;
2312 prevSrstAsserted = srstAsserted;
2313 prevPowerdropout = powerDropout;
2315 if (srstDeasserted || powerRestored) {
2316 /* Other than logging the event we can't do anything here.
2317 * Issuing a reset is a particularly bad idea as we might
2318 * be inside a reset already.
2322 return ERROR_OK;
2325 /* process target state changes */
2326 static int handle_target(void *priv)
2328 Jim_Interp *interp = (Jim_Interp *)priv;
2329 int retval = ERROR_OK;
2331 if (!is_jtag_poll_safe()) {
2332 /* polling is disabled currently */
2333 return ERROR_OK;
2336 /* we do not want to recurse here... */
2337 static int recursive;
2338 if (!recursive) {
2339 recursive = 1;
2340 sense_handler();
2341 /* danger! running these procedures can trigger srst assertions and power dropouts.
2342 * We need to avoid an infinite loop/recursion here and we do that by
2343 * clearing the flags after running these events.
2345 int did_something = 0;
2346 if (runSrstAsserted) {
2347 LOG_INFO("srst asserted detected, running srst_asserted proc.");
2348 Jim_Eval(interp, "srst_asserted");
2349 did_something = 1;
2351 if (runSrstDeasserted) {
2352 Jim_Eval(interp, "srst_deasserted");
2353 did_something = 1;
2355 if (runPowerDropout) {
2356 LOG_INFO("Power dropout detected, running power_dropout proc.");
2357 Jim_Eval(interp, "power_dropout");
2358 did_something = 1;
2360 if (runPowerRestore) {
2361 Jim_Eval(interp, "power_restore");
2362 did_something = 1;
2365 if (did_something) {
2366 /* clear detect flags */
2367 sense_handler();
2370 /* clear action flags */
2372 runSrstAsserted = 0;
2373 runSrstDeasserted = 0;
2374 runPowerRestore = 0;
2375 runPowerDropout = 0;
2377 recursive = 0;
2380 /* Poll targets for state changes unless that's globally disabled.
2381 * Skip targets that are currently disabled.
2383 for (struct target *target = all_targets;
2384 is_jtag_poll_safe() && target;
2385 target = target->next) {
2387 if (!target_was_examined(target))
2388 continue;
2390 if (!target->tap->enabled)
2391 continue;
2393 if (target->backoff.times > target->backoff.count) {
2394 /* do not poll this time as we failed previously */
2395 target->backoff.count++;
2396 continue;
2398 target->backoff.count = 0;
2400 /* only poll target if we've got power and srst isn't asserted */
2401 if (!powerDropout && !srstAsserted) {
2402 /* polling may fail silently until the target has been examined */
2403 retval = target_poll(target);
2404 if (retval != ERROR_OK) {
2405 /* 100ms polling interval. Increase interval between polling up to 5000ms */
2406 if (target->backoff.times * polling_interval < 5000) {
2407 target->backoff.times *= 2;
2408 target->backoff.times++;
2410 LOG_USER("Polling target %s failed, GDB will be halted. Polling again in %dms",
2411 target_name(target),
2412 target->backoff.times * polling_interval);
2414 /* Tell GDB to halt the debugger. This allows the user to
2415 * run monitor commands to handle the situation.
2417 target_call_event_callbacks(target, TARGET_EVENT_GDB_HALT);
2418 return retval;
2420 /* Since we succeeded, we reset backoff count */
2421 if (target->backoff.times > 0)
2422 LOG_USER("Polling target %s succeeded again", target_name(target));
2423 target->backoff.times = 0;
2427 return retval;
2430 COMMAND_HANDLER(handle_reg_command)
2432 struct target *target;
2433 struct reg *reg = NULL;
2434 unsigned count = 0;
2435 char *value;
2437 LOG_DEBUG("-");
2439 target = get_current_target(CMD_CTX);
2441 /* list all available registers for the current target */
2442 if (CMD_ARGC == 0) {
2443 struct reg_cache *cache = target->reg_cache;
2445 count = 0;
2446 while (cache) {
2447 unsigned i;
2449 command_print(CMD_CTX, "===== %s", cache->name);
2451 for (i = 0, reg = cache->reg_list;
2452 i < cache->num_regs;
2453 i++, reg++, count++) {
2454 /* only print cached values if they are valid */
2455 if (reg->valid) {
2456 value = buf_to_str(reg->value,
2457 reg->size, 16);
2458 command_print(CMD_CTX,
2459 "(%i) %s (/%" PRIu32 "): 0x%s%s",
2460 count, reg->name,
2461 reg->size, value,
2462 reg->dirty
2463 ? " (dirty)"
2464 : "");
2465 free(value);
2466 } else {
2467 command_print(CMD_CTX, "(%i) %s (/%" PRIu32 ")",
2468 count, reg->name,
2469 reg->size) ;
2472 cache = cache->next;
2475 return ERROR_OK;
2478 /* access a single register by its ordinal number */
2479 if ((CMD_ARGV[0][0] >= '0') && (CMD_ARGV[0][0] <= '9')) {
2480 unsigned num;
2481 COMMAND_PARSE_NUMBER(uint, CMD_ARGV[0], num);
2483 struct reg_cache *cache = target->reg_cache;
2484 count = 0;
2485 while (cache) {
2486 unsigned i;
2487 for (i = 0; i < cache->num_regs; i++) {
2488 if (count++ == num) {
2489 reg = &cache->reg_list[i];
2490 break;
2493 if (reg)
2494 break;
2495 cache = cache->next;
2498 if (!reg) {
2499 command_print(CMD_CTX, "%i is out of bounds, the current target "
2500 "has only %i registers (0 - %i)", num, count, count - 1);
2501 return ERROR_OK;
2503 } else {
2504 /* access a single register by its name */
2505 reg = register_get_by_name(target->reg_cache, CMD_ARGV[0], 1);
2507 if (!reg) {
2508 command_print(CMD_CTX, "register %s not found in current target", CMD_ARGV[0]);
2509 return ERROR_OK;
2513 assert(reg != NULL); /* give clang a hint that we *know* reg is != NULL here */
2515 /* display a register */
2516 if ((CMD_ARGC == 1) || ((CMD_ARGC == 2) && !((CMD_ARGV[1][0] >= '0')
2517 && (CMD_ARGV[1][0] <= '9')))) {
2518 if ((CMD_ARGC == 2) && (strcmp(CMD_ARGV[1], "force") == 0))
2519 reg->valid = 0;
2521 if (reg->valid == 0)
2522 reg->type->get(reg);
2523 value = buf_to_str(reg->value, reg->size, 16);
2524 command_print(CMD_CTX, "%s (/%i): 0x%s", reg->name, (int)(reg->size), value);
2525 free(value);
2526 return ERROR_OK;
2529 /* set register value */
2530 if (CMD_ARGC == 2) {
2531 uint8_t *buf = malloc(DIV_ROUND_UP(reg->size, 8));
2532 if (buf == NULL)
2533 return ERROR_FAIL;
2534 str_to_buf(CMD_ARGV[1], strlen(CMD_ARGV[1]), buf, reg->size, 0);
2536 reg->type->set(reg, buf);
2538 value = buf_to_str(reg->value, reg->size, 16);
2539 command_print(CMD_CTX, "%s (/%i): 0x%s", reg->name, (int)(reg->size), value);
2540 free(value);
2542 free(buf);
2544 return ERROR_OK;
2547 return ERROR_COMMAND_SYNTAX_ERROR;
2550 COMMAND_HANDLER(handle_poll_command)
2552 int retval = ERROR_OK;
2553 struct target *target = get_current_target(CMD_CTX);
2555 if (CMD_ARGC == 0) {
2556 command_print(CMD_CTX, "background polling: %s",
2557 jtag_poll_get_enabled() ? "on" : "off");
2558 command_print(CMD_CTX, "TAP: %s (%s)",
2559 target->tap->dotted_name,
2560 target->tap->enabled ? "enabled" : "disabled");
2561 if (!target->tap->enabled)
2562 return ERROR_OK;
2563 retval = target_poll(target);
2564 if (retval != ERROR_OK)
2565 return retval;
2566 retval = target_arch_state(target);
2567 if (retval != ERROR_OK)
2568 return retval;
2569 } else if (CMD_ARGC == 1) {
2570 bool enable;
2571 COMMAND_PARSE_ON_OFF(CMD_ARGV[0], enable);
2572 jtag_poll_set_enabled(enable);
2573 } else
2574 return ERROR_COMMAND_SYNTAX_ERROR;
2576 return retval;
2579 COMMAND_HANDLER(handle_wait_halt_command)
2581 if (CMD_ARGC > 1)
2582 return ERROR_COMMAND_SYNTAX_ERROR;
2584 unsigned ms = DEFAULT_HALT_TIMEOUT;
2585 if (1 == CMD_ARGC) {
2586 int retval = parse_uint(CMD_ARGV[0], &ms);
2587 if (ERROR_OK != retval)
2588 return ERROR_COMMAND_SYNTAX_ERROR;
2591 struct target *target = get_current_target(CMD_CTX);
2592 return target_wait_state(target, TARGET_HALTED, ms);
2595 /* wait for target state to change. The trick here is to have a low
2596 * latency for short waits and not to suck up all the CPU time
2597 * on longer waits.
2599 * After 500ms, keep_alive() is invoked
2601 int target_wait_state(struct target *target, enum target_state state, int ms)
2603 int retval;
2604 long long then = 0, cur;
2605 int once = 1;
2607 for (;;) {
2608 retval = target_poll(target);
2609 if (retval != ERROR_OK)
2610 return retval;
2611 if (target->state == state)
2612 break;
2613 cur = timeval_ms();
2614 if (once) {
2615 once = 0;
2616 then = timeval_ms();
2617 LOG_DEBUG("waiting for target %s...",
2618 Jim_Nvp_value2name_simple(nvp_target_state, state)->name);
2621 if (cur-then > 500)
2622 keep_alive();
2624 if ((cur-then) > ms) {
2625 LOG_ERROR("timed out while waiting for target %s",
2626 Jim_Nvp_value2name_simple(nvp_target_state, state)->name);
2627 return ERROR_FAIL;
2631 return ERROR_OK;
2634 COMMAND_HANDLER(handle_halt_command)
2636 LOG_DEBUG("-");
2638 struct target *target = get_current_target(CMD_CTX);
2639 int retval = target_halt(target);
2640 if (ERROR_OK != retval)
2641 return retval;
2643 if (CMD_ARGC == 1) {
2644 unsigned wait_local;
2645 retval = parse_uint(CMD_ARGV[0], &wait_local);
2646 if (ERROR_OK != retval)
2647 return ERROR_COMMAND_SYNTAX_ERROR;
2648 if (!wait_local)
2649 return ERROR_OK;
2652 return CALL_COMMAND_HANDLER(handle_wait_halt_command);
2655 COMMAND_HANDLER(handle_soft_reset_halt_command)
2657 struct target *target = get_current_target(CMD_CTX);
2659 LOG_USER("requesting target halt and executing a soft reset");
2661 target_soft_reset_halt(target);
2663 return ERROR_OK;
2666 COMMAND_HANDLER(handle_reset_command)
2668 if (CMD_ARGC > 1)
2669 return ERROR_COMMAND_SYNTAX_ERROR;
2671 enum target_reset_mode reset_mode = RESET_RUN;
2672 if (CMD_ARGC == 1) {
2673 const Jim_Nvp *n;
2674 n = Jim_Nvp_name2value_simple(nvp_reset_modes, CMD_ARGV[0]);
2675 if ((n->name == NULL) || (n->value == RESET_UNKNOWN))
2676 return ERROR_COMMAND_SYNTAX_ERROR;
2677 reset_mode = n->value;
2680 /* reset *all* targets */
2681 return target_process_reset(CMD_CTX, reset_mode);
2685 COMMAND_HANDLER(handle_resume_command)
2687 int current = 1;
2688 if (CMD_ARGC > 1)
2689 return ERROR_COMMAND_SYNTAX_ERROR;
2691 struct target *target = get_current_target(CMD_CTX);
2693 /* with no CMD_ARGV, resume from current pc, addr = 0,
2694 * with one arguments, addr = CMD_ARGV[0],
2695 * handle breakpoints, not debugging */
2696 uint32_t addr = 0;
2697 if (CMD_ARGC == 1) {
2698 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], addr);
2699 current = 0;
2702 return target_resume(target, current, addr, 1, 0);
2705 COMMAND_HANDLER(handle_step_command)
2707 if (CMD_ARGC > 1)
2708 return ERROR_COMMAND_SYNTAX_ERROR;
2710 LOG_DEBUG("-");
2712 /* with no CMD_ARGV, step from current pc, addr = 0,
2713 * with one argument addr = CMD_ARGV[0],
2714 * handle breakpoints, debugging */
2715 uint32_t addr = 0;
2716 int current_pc = 1;
2717 if (CMD_ARGC == 1) {
2718 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], addr);
2719 current_pc = 0;
2722 struct target *target = get_current_target(CMD_CTX);
2724 return target->type->step(target, current_pc, addr, 1);
2727 static void handle_md_output(struct command_context *cmd_ctx,
2728 struct target *target, uint32_t address, unsigned size,
2729 unsigned count, const uint8_t *buffer)
2731 const unsigned line_bytecnt = 32;
2732 unsigned line_modulo = line_bytecnt / size;
2734 char output[line_bytecnt * 4 + 1];
2735 unsigned output_len = 0;
2737 const char *value_fmt;
2738 switch (size) {
2739 case 4:
2740 value_fmt = "%8.8x ";
2741 break;
2742 case 2:
2743 value_fmt = "%4.4x ";
2744 break;
2745 case 1:
2746 value_fmt = "%2.2x ";
2747 break;
2748 default:
2749 /* "can't happen", caller checked */
2750 LOG_ERROR("invalid memory read size: %u", size);
2751 return;
2754 for (unsigned i = 0; i < count; i++) {
2755 if (i % line_modulo == 0) {
2756 output_len += snprintf(output + output_len,
2757 sizeof(output) - output_len,
2758 "0x%8.8x: ",
2759 (unsigned)(address + (i*size)));
2762 uint32_t value = 0;
2763 const uint8_t *value_ptr = buffer + i * size;
2764 switch (size) {
2765 case 4:
2766 value = target_buffer_get_u32(target, value_ptr);
2767 break;
2768 case 2:
2769 value = target_buffer_get_u16(target, value_ptr);
2770 break;
2771 case 1:
2772 value = *value_ptr;
2774 output_len += snprintf(output + output_len,
2775 sizeof(output) - output_len,
2776 value_fmt, value);
2778 if ((i % line_modulo == line_modulo - 1) || (i == count - 1)) {
2779 command_print(cmd_ctx, "%s", output);
2780 output_len = 0;
2785 COMMAND_HANDLER(handle_md_command)
2787 if (CMD_ARGC < 1)
2788 return ERROR_COMMAND_SYNTAX_ERROR;
2790 unsigned size = 0;
2791 switch (CMD_NAME[2]) {
2792 case 'w':
2793 size = 4;
2794 break;
2795 case 'h':
2796 size = 2;
2797 break;
2798 case 'b':
2799 size = 1;
2800 break;
2801 default:
2802 return ERROR_COMMAND_SYNTAX_ERROR;
2805 bool physical = strcmp(CMD_ARGV[0], "phys") == 0;
2806 int (*fn)(struct target *target,
2807 uint32_t address, uint32_t size_value, uint32_t count, uint8_t *buffer);
2808 if (physical) {
2809 CMD_ARGC--;
2810 CMD_ARGV++;
2811 fn = target_read_phys_memory;
2812 } else
2813 fn = target_read_memory;
2814 if ((CMD_ARGC < 1) || (CMD_ARGC > 2))
2815 return ERROR_COMMAND_SYNTAX_ERROR;
2817 uint32_t address;
2818 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], address);
2820 unsigned count = 1;
2821 if (CMD_ARGC == 2)
2822 COMMAND_PARSE_NUMBER(uint, CMD_ARGV[1], count);
2824 uint8_t *buffer = calloc(count, size);
2826 struct target *target = get_current_target(CMD_CTX);
2827 int retval = fn(target, address, size, count, buffer);
2828 if (ERROR_OK == retval)
2829 handle_md_output(CMD_CTX, target, address, size, count, buffer);
2831 free(buffer);
2833 return retval;
2836 typedef int (*target_write_fn)(struct target *target,
2837 uint32_t address, uint32_t size, uint32_t count, const uint8_t *buffer);
2839 static int target_fill_mem(struct target *target,
2840 uint32_t address,
2841 target_write_fn fn,
2842 unsigned data_size,
2843 /* value */
2844 uint32_t b,
2845 /* count */
2846 unsigned c)
2848 /* We have to write in reasonably large chunks to be able
2849 * to fill large memory areas with any sane speed */
2850 const unsigned chunk_size = 16384;
2851 uint8_t *target_buf = malloc(chunk_size * data_size);
2852 if (target_buf == NULL) {
2853 LOG_ERROR("Out of memory");
2854 return ERROR_FAIL;
2857 for (unsigned i = 0; i < chunk_size; i++) {
2858 switch (data_size) {
2859 case 4:
2860 target_buffer_set_u32(target, target_buf + i * data_size, b);
2861 break;
2862 case 2:
2863 target_buffer_set_u16(target, target_buf + i * data_size, b);
2864 break;
2865 case 1:
2866 target_buffer_set_u8(target, target_buf + i * data_size, b);
2867 break;
2868 default:
2869 exit(-1);
2873 int retval = ERROR_OK;
2875 for (unsigned x = 0; x < c; x += chunk_size) {
2876 unsigned current;
2877 current = c - x;
2878 if (current > chunk_size)
2879 current = chunk_size;
2880 retval = fn(target, address + x * data_size, data_size, current, target_buf);
2881 if (retval != ERROR_OK)
2882 break;
2883 /* avoid GDB timeouts */
2884 keep_alive();
2886 free(target_buf);
2888 return retval;
2892 COMMAND_HANDLER(handle_mw_command)
2894 if (CMD_ARGC < 2)
2895 return ERROR_COMMAND_SYNTAX_ERROR;
2896 bool physical = strcmp(CMD_ARGV[0], "phys") == 0;
2897 target_write_fn fn;
2898 if (physical) {
2899 CMD_ARGC--;
2900 CMD_ARGV++;
2901 fn = target_write_phys_memory;
2902 } else
2903 fn = target_write_memory;
2904 if ((CMD_ARGC < 2) || (CMD_ARGC > 3))
2905 return ERROR_COMMAND_SYNTAX_ERROR;
2907 uint32_t address;
2908 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], address);
2910 uint32_t value;
2911 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], value);
2913 unsigned count = 1;
2914 if (CMD_ARGC == 3)
2915 COMMAND_PARSE_NUMBER(uint, CMD_ARGV[2], count);
2917 struct target *target = get_current_target(CMD_CTX);
2918 unsigned wordsize;
2919 switch (CMD_NAME[2]) {
2920 case 'w':
2921 wordsize = 4;
2922 break;
2923 case 'h':
2924 wordsize = 2;
2925 break;
2926 case 'b':
2927 wordsize = 1;
2928 break;
2929 default:
2930 return ERROR_COMMAND_SYNTAX_ERROR;
2933 return target_fill_mem(target, address, fn, wordsize, value, count);
2936 static COMMAND_HELPER(parse_load_image_command_CMD_ARGV, struct image *image,
2937 uint32_t *min_address, uint32_t *max_address)
2939 if (CMD_ARGC < 1 || CMD_ARGC > 5)
2940 return ERROR_COMMAND_SYNTAX_ERROR;
2942 /* a base address isn't always necessary,
2943 * default to 0x0 (i.e. don't relocate) */
2944 if (CMD_ARGC >= 2) {
2945 uint32_t addr;
2946 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], addr);
2947 image->base_address = addr;
2948 image->base_address_set = 1;
2949 } else
2950 image->base_address_set = 0;
2952 image->start_address_set = 0;
2954 if (CMD_ARGC >= 4)
2955 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[3], *min_address);
2956 if (CMD_ARGC == 5) {
2957 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[4], *max_address);
2958 /* use size (given) to find max (required) */
2959 *max_address += *min_address;
2962 if (*min_address > *max_address)
2963 return ERROR_COMMAND_SYNTAX_ERROR;
2965 return ERROR_OK;
2968 COMMAND_HANDLER(handle_load_image_command)
2970 uint8_t *buffer;
2971 size_t buf_cnt;
2972 uint32_t image_size;
2973 uint32_t min_address = 0;
2974 uint32_t max_address = 0xffffffff;
2975 int i;
2976 struct image image;
2978 int retval = CALL_COMMAND_HANDLER(parse_load_image_command_CMD_ARGV,
2979 &image, &min_address, &max_address);
2980 if (ERROR_OK != retval)
2981 return retval;
2983 struct target *target = get_current_target(CMD_CTX);
2985 struct duration bench;
2986 duration_start(&bench);
2988 if (image_open(&image, CMD_ARGV[0], (CMD_ARGC >= 3) ? CMD_ARGV[2] : NULL) != ERROR_OK)
2989 return ERROR_OK;
2991 image_size = 0x0;
2992 retval = ERROR_OK;
2993 for (i = 0; i < image.num_sections; i++) {
2994 buffer = malloc(image.sections[i].size);
2995 if (buffer == NULL) {
2996 command_print(CMD_CTX,
2997 "error allocating buffer for section (%d bytes)",
2998 (int)(image.sections[i].size));
2999 break;
3002 retval = image_read_section(&image, i, 0x0, image.sections[i].size, buffer, &buf_cnt);
3003 if (retval != ERROR_OK) {
3004 free(buffer);
3005 break;
3008 uint32_t offset = 0;
3009 uint32_t length = buf_cnt;
3011 /* DANGER!!! beware of unsigned comparision here!!! */
3013 if ((image.sections[i].base_address + buf_cnt >= min_address) &&
3014 (image.sections[i].base_address < max_address)) {
3016 if (image.sections[i].base_address < min_address) {
3017 /* clip addresses below */
3018 offset += min_address-image.sections[i].base_address;
3019 length -= offset;
3022 if (image.sections[i].base_address + buf_cnt > max_address)
3023 length -= (image.sections[i].base_address + buf_cnt)-max_address;
3025 retval = target_write_buffer(target,
3026 image.sections[i].base_address + offset, length, buffer + offset);
3027 if (retval != ERROR_OK) {
3028 free(buffer);
3029 break;
3031 image_size += length;
3032 command_print(CMD_CTX, "%u bytes written at address 0x%8.8" PRIx32 "",
3033 (unsigned int)length,
3034 image.sections[i].base_address + offset);
3037 free(buffer);
3040 if ((ERROR_OK == retval) && (duration_measure(&bench) == ERROR_OK)) {
3041 command_print(CMD_CTX, "downloaded %" PRIu32 " bytes "
3042 "in %fs (%0.3f KiB/s)", image_size,
3043 duration_elapsed(&bench), duration_kbps(&bench, image_size));
3046 image_close(&image);
3048 return retval;
3052 COMMAND_HANDLER(handle_dump_image_command)
3054 struct fileio fileio;
3055 uint8_t *buffer;
3056 int retval, retvaltemp;
3057 uint32_t address, size;
3058 struct duration bench;
3059 struct target *target = get_current_target(CMD_CTX);
3061 if (CMD_ARGC != 3)
3062 return ERROR_COMMAND_SYNTAX_ERROR;
3064 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], address);
3065 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[2], size);
3067 uint32_t buf_size = (size > 4096) ? 4096 : size;
3068 buffer = malloc(buf_size);
3069 if (!buffer)
3070 return ERROR_FAIL;
3072 retval = fileio_open(&fileio, CMD_ARGV[0], FILEIO_WRITE, FILEIO_BINARY);
3073 if (retval != ERROR_OK) {
3074 free(buffer);
3075 return retval;
3078 duration_start(&bench);
3080 while (size > 0) {
3081 size_t size_written;
3082 uint32_t this_run_size = (size > buf_size) ? buf_size : size;
3083 retval = target_read_buffer(target, address, this_run_size, buffer);
3084 if (retval != ERROR_OK)
3085 break;
3087 retval = fileio_write(&fileio, this_run_size, buffer, &size_written);
3088 if (retval != ERROR_OK)
3089 break;
3091 size -= this_run_size;
3092 address += this_run_size;
3095 free(buffer);
3097 if ((ERROR_OK == retval) && (duration_measure(&bench) == ERROR_OK)) {
3098 int filesize;
3099 retval = fileio_size(&fileio, &filesize);
3100 if (retval != ERROR_OK)
3101 return retval;
3102 command_print(CMD_CTX,
3103 "dumped %ld bytes in %fs (%0.3f KiB/s)", (long)filesize,
3104 duration_elapsed(&bench), duration_kbps(&bench, filesize));
3107 retvaltemp = fileio_close(&fileio);
3108 if (retvaltemp != ERROR_OK)
3109 return retvaltemp;
3111 return retval;
3114 static COMMAND_HELPER(handle_verify_image_command_internal, int verify)
3116 uint8_t *buffer;
3117 size_t buf_cnt;
3118 uint32_t image_size;
3119 int i;
3120 int retval;
3121 uint32_t checksum = 0;
3122 uint32_t mem_checksum = 0;
3124 struct image image;
3126 struct target *target = get_current_target(CMD_CTX);
3128 if (CMD_ARGC < 1)
3129 return ERROR_COMMAND_SYNTAX_ERROR;
3131 if (!target) {
3132 LOG_ERROR("no target selected");
3133 return ERROR_FAIL;
3136 struct duration bench;
3137 duration_start(&bench);
3139 if (CMD_ARGC >= 2) {
3140 uint32_t addr;
3141 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], addr);
3142 image.base_address = addr;
3143 image.base_address_set = 1;
3144 } else {
3145 image.base_address_set = 0;
3146 image.base_address = 0x0;
3149 image.start_address_set = 0;
3151 retval = image_open(&image, CMD_ARGV[0], (CMD_ARGC == 3) ? CMD_ARGV[2] : NULL);
3152 if (retval != ERROR_OK)
3153 return retval;
3155 image_size = 0x0;
3156 int diffs = 0;
3157 retval = ERROR_OK;
3158 for (i = 0; i < image.num_sections; i++) {
3159 buffer = malloc(image.sections[i].size);
3160 if (buffer == NULL) {
3161 command_print(CMD_CTX,
3162 "error allocating buffer for section (%d bytes)",
3163 (int)(image.sections[i].size));
3164 break;
3166 retval = image_read_section(&image, i, 0x0, image.sections[i].size, buffer, &buf_cnt);
3167 if (retval != ERROR_OK) {
3168 free(buffer);
3169 break;
3172 if (verify) {
3173 /* calculate checksum of image */
3174 retval = image_calculate_checksum(buffer, buf_cnt, &checksum);
3175 if (retval != ERROR_OK) {
3176 free(buffer);
3177 break;
3180 retval = target_checksum_memory(target, image.sections[i].base_address, buf_cnt, &mem_checksum);
3181 if (retval != ERROR_OK) {
3182 free(buffer);
3183 break;
3186 if (checksum != mem_checksum) {
3187 /* failed crc checksum, fall back to a binary compare */
3188 uint8_t *data;
3190 if (diffs == 0)
3191 LOG_ERROR("checksum mismatch - attempting binary compare");
3193 data = malloc(buf_cnt);
3195 /* Can we use 32bit word accesses? */
3196 int size = 1;
3197 int count = buf_cnt;
3198 if ((count % 4) == 0) {
3199 size *= 4;
3200 count /= 4;
3202 retval = target_read_memory(target, image.sections[i].base_address, size, count, data);
3203 if (retval == ERROR_OK) {
3204 uint32_t t;
3205 for (t = 0; t < buf_cnt; t++) {
3206 if (data[t] != buffer[t]) {
3207 command_print(CMD_CTX,
3208 "diff %d address 0x%08x. Was 0x%02x instead of 0x%02x",
3209 diffs,
3210 (unsigned)(t + image.sections[i].base_address),
3211 data[t],
3212 buffer[t]);
3213 if (diffs++ >= 127) {
3214 command_print(CMD_CTX, "More than 128 errors, the rest are not printed.");
3215 free(data);
3216 free(buffer);
3217 goto done;
3220 keep_alive();
3223 free(data);
3225 } else {
3226 command_print(CMD_CTX, "address 0x%08" PRIx32 " length 0x%08zx",
3227 image.sections[i].base_address,
3228 buf_cnt);
3231 free(buffer);
3232 image_size += buf_cnt;
3234 if (diffs > 0)
3235 command_print(CMD_CTX, "No more differences found.");
3236 done:
3237 if (diffs > 0)
3238 retval = ERROR_FAIL;
3239 if ((ERROR_OK == retval) && (duration_measure(&bench) == ERROR_OK)) {
3240 command_print(CMD_CTX, "verified %" PRIu32 " bytes "
3241 "in %fs (%0.3f KiB/s)", image_size,
3242 duration_elapsed(&bench), duration_kbps(&bench, image_size));
3245 image_close(&image);
3247 return retval;
3250 COMMAND_HANDLER(handle_verify_image_command)
3252 return CALL_COMMAND_HANDLER(handle_verify_image_command_internal, 1);
3255 COMMAND_HANDLER(handle_test_image_command)
3257 return CALL_COMMAND_HANDLER(handle_verify_image_command_internal, 0);
3260 static int handle_bp_command_list(struct command_context *cmd_ctx)
3262 struct target *target = get_current_target(cmd_ctx);
3263 struct breakpoint *breakpoint = target->breakpoints;
3264 while (breakpoint) {
3265 if (breakpoint->type == BKPT_SOFT) {
3266 char *buf = buf_to_str(breakpoint->orig_instr,
3267 breakpoint->length, 16);
3268 command_print(cmd_ctx, "IVA breakpoint: 0x%8.8" PRIx32 ", 0x%x, %i, 0x%s",
3269 breakpoint->address,
3270 breakpoint->length,
3271 breakpoint->set, buf);
3272 free(buf);
3273 } else {
3274 if ((breakpoint->address == 0) && (breakpoint->asid != 0))
3275 command_print(cmd_ctx, "Context breakpoint: 0x%8.8" PRIx32 ", 0x%x, %i",
3276 breakpoint->asid,
3277 breakpoint->length, breakpoint->set);
3278 else if ((breakpoint->address != 0) && (breakpoint->asid != 0)) {
3279 command_print(cmd_ctx, "Hybrid breakpoint(IVA): 0x%8.8" PRIx32 ", 0x%x, %i",
3280 breakpoint->address,
3281 breakpoint->length, breakpoint->set);
3282 command_print(cmd_ctx, "\t|--->linked with ContextID: 0x%8.8" PRIx32,
3283 breakpoint->asid);
3284 } else
3285 command_print(cmd_ctx, "Breakpoint(IVA): 0x%8.8" PRIx32 ", 0x%x, %i",
3286 breakpoint->address,
3287 breakpoint->length, breakpoint->set);
3290 breakpoint = breakpoint->next;
3292 return ERROR_OK;
3295 static int handle_bp_command_set(struct command_context *cmd_ctx,
3296 uint32_t addr, uint32_t asid, uint32_t length, int hw)
3298 struct target *target = get_current_target(cmd_ctx);
3300 if (asid == 0) {
3301 int retval = breakpoint_add(target, addr, length, hw);
3302 if (ERROR_OK == retval)
3303 command_print(cmd_ctx, "breakpoint set at 0x%8.8" PRIx32 "", addr);
3304 else {
3305 LOG_ERROR("Failure setting breakpoint, the same address(IVA) is already used");
3306 return retval;
3308 } else if (addr == 0) {
3309 int retval = context_breakpoint_add(target, asid, length, hw);
3310 if (ERROR_OK == retval)
3311 command_print(cmd_ctx, "Context breakpoint set at 0x%8.8" PRIx32 "", asid);
3312 else {
3313 LOG_ERROR("Failure setting breakpoint, the same address(CONTEXTID) is already used");
3314 return retval;
3316 } else {
3317 int retval = hybrid_breakpoint_add(target, addr, asid, length, hw);
3318 if (ERROR_OK == retval)
3319 command_print(cmd_ctx, "Hybrid breakpoint set at 0x%8.8" PRIx32 "", asid);
3320 else {
3321 LOG_ERROR("Failure setting breakpoint, the same address is already used");
3322 return retval;
3325 return ERROR_OK;
3328 COMMAND_HANDLER(handle_bp_command)
3330 uint32_t addr;
3331 uint32_t asid;
3332 uint32_t length;
3333 int hw = BKPT_SOFT;
3335 switch (CMD_ARGC) {
3336 case 0:
3337 return handle_bp_command_list(CMD_CTX);
3339 case 2:
3340 asid = 0;
3341 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], addr);
3342 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], length);
3343 return handle_bp_command_set(CMD_CTX, addr, asid, length, hw);
3345 case 3:
3346 if (strcmp(CMD_ARGV[2], "hw") == 0) {
3347 hw = BKPT_HARD;
3348 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], addr);
3350 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], length);
3352 asid = 0;
3353 return handle_bp_command_set(CMD_CTX, addr, asid, length, hw);
3354 } else if (strcmp(CMD_ARGV[2], "hw_ctx") == 0) {
3355 hw = BKPT_HARD;
3356 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], asid);
3357 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], length);
3358 addr = 0;
3359 return handle_bp_command_set(CMD_CTX, addr, asid, length, hw);
3362 case 4:
3363 hw = BKPT_HARD;
3364 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], addr);
3365 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], asid);
3366 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[2], length);
3367 return handle_bp_command_set(CMD_CTX, addr, asid, length, hw);
3369 default:
3370 return ERROR_COMMAND_SYNTAX_ERROR;
3374 COMMAND_HANDLER(handle_rbp_command)
3376 if (CMD_ARGC != 1)
3377 return ERROR_COMMAND_SYNTAX_ERROR;
3379 uint32_t addr;
3380 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], addr);
3382 struct target *target = get_current_target(CMD_CTX);
3383 breakpoint_remove(target, addr);
3385 return ERROR_OK;
3388 COMMAND_HANDLER(handle_wp_command)
3390 struct target *target = get_current_target(CMD_CTX);
3392 if (CMD_ARGC == 0) {
3393 struct watchpoint *watchpoint = target->watchpoints;
3395 while (watchpoint) {
3396 command_print(CMD_CTX, "address: 0x%8.8" PRIx32
3397 ", len: 0x%8.8" PRIx32
3398 ", r/w/a: %i, value: 0x%8.8" PRIx32
3399 ", mask: 0x%8.8" PRIx32,
3400 watchpoint->address,
3401 watchpoint->length,
3402 (int)watchpoint->rw,
3403 watchpoint->value,
3404 watchpoint->mask);
3405 watchpoint = watchpoint->next;
3407 return ERROR_OK;
3410 enum watchpoint_rw type = WPT_ACCESS;
3411 uint32_t addr = 0;
3412 uint32_t length = 0;
3413 uint32_t data_value = 0x0;
3414 uint32_t data_mask = 0xffffffff;
3416 switch (CMD_ARGC) {
3417 case 5:
3418 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[4], data_mask);
3419 /* fall through */
3420 case 4:
3421 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[3], data_value);
3422 /* fall through */
3423 case 3:
3424 switch (CMD_ARGV[2][0]) {
3425 case 'r':
3426 type = WPT_READ;
3427 break;
3428 case 'w':
3429 type = WPT_WRITE;
3430 break;
3431 case 'a':
3432 type = WPT_ACCESS;
3433 break;
3434 default:
3435 LOG_ERROR("invalid watchpoint mode ('%c')", CMD_ARGV[2][0]);
3436 return ERROR_COMMAND_SYNTAX_ERROR;
3438 /* fall through */
3439 case 2:
3440 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], length);
3441 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], addr);
3442 break;
3444 default:
3445 return ERROR_COMMAND_SYNTAX_ERROR;
3448 int retval = watchpoint_add(target, addr, length, type,
3449 data_value, data_mask);
3450 if (ERROR_OK != retval)
3451 LOG_ERROR("Failure setting watchpoints");
3453 return retval;
3456 COMMAND_HANDLER(handle_rwp_command)
3458 if (CMD_ARGC != 1)
3459 return ERROR_COMMAND_SYNTAX_ERROR;
3461 uint32_t addr;
3462 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], addr);
3464 struct target *target = get_current_target(CMD_CTX);
3465 watchpoint_remove(target, addr);
3467 return ERROR_OK;
3471 * Translate a virtual address to a physical address.
3473 * The low-level target implementation must have logged a detailed error
3474 * which is forwarded to telnet/GDB session.
3476 COMMAND_HANDLER(handle_virt2phys_command)
3478 if (CMD_ARGC != 1)
3479 return ERROR_COMMAND_SYNTAX_ERROR;
3481 uint32_t va;
3482 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], va);
3483 uint32_t pa;
3485 struct target *target = get_current_target(CMD_CTX);
3486 int retval = target->type->virt2phys(target, va, &pa);
3487 if (retval == ERROR_OK)
3488 command_print(CMD_CTX, "Physical address 0x%08" PRIx32 "", pa);
3490 return retval;
3493 static void writeData(FILE *f, const void *data, size_t len)
3495 size_t written = fwrite(data, 1, len, f);
3496 if (written != len)
3497 LOG_ERROR("failed to write %zu bytes: %s", len, strerror(errno));
3500 static void writeLong(FILE *f, int l)
3502 int i;
3503 for (i = 0; i < 4; i++) {
3504 char c = (l >> (i*8))&0xff;
3505 writeData(f, &c, 1);
3510 static void writeString(FILE *f, char *s)
3512 writeData(f, s, strlen(s));
3515 typedef unsigned char UNIT[2]; /* unit of profiling */
3517 /* Dump a gmon.out histogram file. */
3518 static void write_gmon(uint32_t *samples, uint32_t sampleNum, const char *filename,
3519 bool with_range, uint32_t start_address, uint32_t end_address)
3521 uint32_t i;
3522 FILE *f = fopen(filename, "w");
3523 if (f == NULL)
3524 return;
3525 writeString(f, "gmon");
3526 writeLong(f, 0x00000001); /* Version */
3527 writeLong(f, 0); /* padding */
3528 writeLong(f, 0); /* padding */
3529 writeLong(f, 0); /* padding */
3531 uint8_t zero = 0; /* GMON_TAG_TIME_HIST */
3532 writeData(f, &zero, 1);
3534 /* figure out bucket size */
3535 uint32_t min;
3536 uint32_t max;
3537 if (with_range) {
3538 min = start_address;
3539 max = end_address;
3540 } else {
3541 min = samples[0];
3542 max = samples[0];
3543 for (i = 0; i < sampleNum; i++) {
3544 if (min > samples[i])
3545 min = samples[i];
3546 if (max < samples[i])
3547 max = samples[i];
3550 /* max should be (largest sample + 1)
3551 * Refer to binutils/gprof/hist.c (find_histogram_for_pc) */
3552 max++;
3555 int addressSpace = max - min;
3556 assert(addressSpace >= 2);
3558 /* FIXME: What is the reasonable number of buckets?
3559 * The profiling result will be more accurate if there are enough buckets. */
3560 static const uint32_t maxBuckets = 128 * 1024; /* maximum buckets. */
3561 uint32_t numBuckets = addressSpace / sizeof(UNIT);
3562 if (numBuckets > maxBuckets)
3563 numBuckets = maxBuckets;
3564 int *buckets = malloc(sizeof(int) * numBuckets);
3565 if (buckets == NULL) {
3566 fclose(f);
3567 return;
3569 memset(buckets, 0, sizeof(int) * numBuckets);
3570 for (i = 0; i < sampleNum; i++) {
3571 uint32_t address = samples[i];
3573 if ((address < min) || (max <= address))
3574 continue;
3576 long long a = address - min;
3577 long long b = numBuckets;
3578 long long c = addressSpace;
3579 int index_t = (a * b) / c; /* danger!!!! int32 overflows */
3580 buckets[index_t]++;
3583 /* append binary memory gmon.out &profile_hist_hdr ((char*)&profile_hist_hdr + sizeof(struct gmon_hist_hdr)) */
3584 writeLong(f, min); /* low_pc */
3585 writeLong(f, max); /* high_pc */
3586 writeLong(f, numBuckets); /* # of buckets */
3587 writeLong(f, 100); /* KLUDGE! We lie, ca. 100Hz best case. */
3588 writeString(f, "seconds");
3589 for (i = 0; i < (15-strlen("seconds")); i++)
3590 writeData(f, &zero, 1);
3591 writeString(f, "s");
3593 /*append binary memory gmon.out profile_hist_data (profile_hist_data + profile_hist_hdr.hist_size) */
3595 char *data = malloc(2 * numBuckets);
3596 if (data != NULL) {
3597 for (i = 0; i < numBuckets; i++) {
3598 int val;
3599 val = buckets[i];
3600 if (val > 65535)
3601 val = 65535;
3602 data[i * 2] = val&0xff;
3603 data[i * 2 + 1] = (val >> 8) & 0xff;
3605 free(buckets);
3606 writeData(f, data, numBuckets * 2);
3607 free(data);
3608 } else
3609 free(buckets);
3611 fclose(f);
3614 /* profiling samples the CPU PC as quickly as OpenOCD is able,
3615 * which will be used as a random sampling of PC */
3616 COMMAND_HANDLER(handle_profile_command)
3618 struct target *target = get_current_target(CMD_CTX);
3620 if ((CMD_ARGC != 2) && (CMD_ARGC != 4))
3621 return ERROR_COMMAND_SYNTAX_ERROR;
3623 const uint32_t MAX_PROFILE_SAMPLE_NUM = 10000;
3624 uint32_t offset;
3625 uint32_t num_of_samples;
3626 int retval = ERROR_OK;
3628 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], offset);
3630 uint32_t *samples = malloc(sizeof(uint32_t) * MAX_PROFILE_SAMPLE_NUM);
3631 if (samples == NULL) {
3632 LOG_ERROR("No memory to store samples.");
3633 return ERROR_FAIL;
3637 * Some cores let us sample the PC without the
3638 * annoying halt/resume step; for example, ARMv7 PCSR.
3639 * Provide a way to use that more efficient mechanism.
3641 retval = target_profiling(target, samples, MAX_PROFILE_SAMPLE_NUM,
3642 &num_of_samples, offset);
3643 if (retval != ERROR_OK) {
3644 free(samples);
3645 return retval;
3648 assert(num_of_samples <= MAX_PROFILE_SAMPLE_NUM);
3650 retval = target_poll(target);
3651 if (retval != ERROR_OK) {
3652 free(samples);
3653 return retval;
3655 if (target->state == TARGET_RUNNING) {
3656 retval = target_halt(target);
3657 if (retval != ERROR_OK) {
3658 free(samples);
3659 return retval;
3663 retval = target_poll(target);
3664 if (retval != ERROR_OK) {
3665 free(samples);
3666 return retval;
3669 uint32_t start_address = 0;
3670 uint32_t end_address = 0;
3671 bool with_range = false;
3672 if (CMD_ARGC == 4) {
3673 with_range = true;
3674 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[2], start_address);
3675 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[3], end_address);
3678 write_gmon(samples, num_of_samples, CMD_ARGV[1],
3679 with_range, start_address, end_address);
3680 command_print(CMD_CTX, "Wrote %s", CMD_ARGV[1]);
3682 free(samples);
3683 return retval;
3686 static int new_int_array_element(Jim_Interp *interp, const char *varname, int idx, uint32_t val)
3688 char *namebuf;
3689 Jim_Obj *nameObjPtr, *valObjPtr;
3690 int result;
3692 namebuf = alloc_printf("%s(%d)", varname, idx);
3693 if (!namebuf)
3694 return JIM_ERR;
3696 nameObjPtr = Jim_NewStringObj(interp, namebuf, -1);
3697 valObjPtr = Jim_NewIntObj(interp, val);
3698 if (!nameObjPtr || !valObjPtr) {
3699 free(namebuf);
3700 return JIM_ERR;
3703 Jim_IncrRefCount(nameObjPtr);
3704 Jim_IncrRefCount(valObjPtr);
3705 result = Jim_SetVariable(interp, nameObjPtr, valObjPtr);
3706 Jim_DecrRefCount(interp, nameObjPtr);
3707 Jim_DecrRefCount(interp, valObjPtr);
3708 free(namebuf);
3709 /* printf("%s(%d) <= 0%08x\n", varname, idx, val); */
3710 return result;
3713 static int jim_mem2array(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
3715 struct command_context *context;
3716 struct target *target;
3718 context = current_command_context(interp);
3719 assert(context != NULL);
3721 target = get_current_target(context);
3722 if (target == NULL) {
3723 LOG_ERROR("mem2array: no current target");
3724 return JIM_ERR;
3727 return target_mem2array(interp, target, argc - 1, argv + 1);
3730 static int target_mem2array(Jim_Interp *interp, struct target *target, int argc, Jim_Obj *const *argv)
3732 long l;
3733 uint32_t width;
3734 int len;
3735 uint32_t addr;
3736 uint32_t count;
3737 uint32_t v;
3738 const char *varname;
3739 int n, e, retval;
3740 uint32_t i;
3742 /* argv[1] = name of array to receive the data
3743 * argv[2] = desired width
3744 * argv[3] = memory address
3745 * argv[4] = count of times to read
3747 if (argc != 4) {
3748 Jim_WrongNumArgs(interp, 1, argv, "varname width addr nelems");
3749 return JIM_ERR;
3751 varname = Jim_GetString(argv[0], &len);
3752 /* given "foo" get space for worse case "foo(%d)" .. add 20 */
3754 e = Jim_GetLong(interp, argv[1], &l);
3755 width = l;
3756 if (e != JIM_OK)
3757 return e;
3759 e = Jim_GetLong(interp, argv[2], &l);
3760 addr = l;
3761 if (e != JIM_OK)
3762 return e;
3763 e = Jim_GetLong(interp, argv[3], &l);
3764 len = l;
3765 if (e != JIM_OK)
3766 return e;
3767 switch (width) {
3768 case 8:
3769 width = 1;
3770 break;
3771 case 16:
3772 width = 2;
3773 break;
3774 case 32:
3775 width = 4;
3776 break;
3777 default:
3778 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3779 Jim_AppendStrings(interp, Jim_GetResult(interp), "Invalid width param, must be 8/16/32", NULL);
3780 return JIM_ERR;
3782 if (len == 0) {
3783 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3784 Jim_AppendStrings(interp, Jim_GetResult(interp), "mem2array: zero width read?", NULL);
3785 return JIM_ERR;
3787 if ((addr + (len * width)) < addr) {
3788 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3789 Jim_AppendStrings(interp, Jim_GetResult(interp), "mem2array: addr + len - wraps to zero?", NULL);
3790 return JIM_ERR;
3792 /* absurd transfer size? */
3793 if (len > 65536) {
3794 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3795 Jim_AppendStrings(interp, Jim_GetResult(interp), "mem2array: absurd > 64K item request", NULL);
3796 return JIM_ERR;
3799 if ((width == 1) ||
3800 ((width == 2) && ((addr & 1) == 0)) ||
3801 ((width == 4) && ((addr & 3) == 0))) {
3802 /* all is well */
3803 } else {
3804 char buf[100];
3805 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3806 sprintf(buf, "mem2array address: 0x%08" PRIx32 " is not aligned for %" PRId32 " byte reads",
3807 addr,
3808 width);
3809 Jim_AppendStrings(interp, Jim_GetResult(interp), buf , NULL);
3810 return JIM_ERR;
3813 /* Transfer loop */
3815 /* index counter */
3816 n = 0;
3818 size_t buffersize = 4096;
3819 uint8_t *buffer = malloc(buffersize);
3820 if (buffer == NULL)
3821 return JIM_ERR;
3823 /* assume ok */
3824 e = JIM_OK;
3825 while (len) {
3826 /* Slurp... in buffer size chunks */
3828 count = len; /* in objects.. */
3829 if (count > (buffersize / width))
3830 count = (buffersize / width);
3832 retval = target_read_memory(target, addr, width, count, buffer);
3833 if (retval != ERROR_OK) {
3834 /* BOO !*/
3835 LOG_ERROR("mem2array: Read @ 0x%08x, w=%d, cnt=%d, failed",
3836 (unsigned int)addr,
3837 (int)width,
3838 (int)count);
3839 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3840 Jim_AppendStrings(interp, Jim_GetResult(interp), "mem2array: cannot read memory", NULL);
3841 e = JIM_ERR;
3842 break;
3843 } else {
3844 v = 0; /* shut up gcc */
3845 for (i = 0; i < count ; i++, n++) {
3846 switch (width) {
3847 case 4:
3848 v = target_buffer_get_u32(target, &buffer[i*width]);
3849 break;
3850 case 2:
3851 v = target_buffer_get_u16(target, &buffer[i*width]);
3852 break;
3853 case 1:
3854 v = buffer[i] & 0x0ff;
3855 break;
3857 new_int_array_element(interp, varname, n, v);
3859 len -= count;
3860 addr += count * width;
3864 free(buffer);
3866 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3868 return e;
3871 static int get_int_array_element(Jim_Interp *interp, const char *varname, int idx, uint32_t *val)
3873 char *namebuf;
3874 Jim_Obj *nameObjPtr, *valObjPtr;
3875 int result;
3876 long l;
3878 namebuf = alloc_printf("%s(%d)", varname, idx);
3879 if (!namebuf)
3880 return JIM_ERR;
3882 nameObjPtr = Jim_NewStringObj(interp, namebuf, -1);
3883 if (!nameObjPtr) {
3884 free(namebuf);
3885 return JIM_ERR;
3888 Jim_IncrRefCount(nameObjPtr);
3889 valObjPtr = Jim_GetVariable(interp, nameObjPtr, JIM_ERRMSG);
3890 Jim_DecrRefCount(interp, nameObjPtr);
3891 free(namebuf);
3892 if (valObjPtr == NULL)
3893 return JIM_ERR;
3895 result = Jim_GetLong(interp, valObjPtr, &l);
3896 /* printf("%s(%d) => 0%08x\n", varname, idx, val); */
3897 *val = l;
3898 return result;
3901 static int jim_array2mem(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
3903 struct command_context *context;
3904 struct target *target;
3906 context = current_command_context(interp);
3907 assert(context != NULL);
3909 target = get_current_target(context);
3910 if (target == NULL) {
3911 LOG_ERROR("array2mem: no current target");
3912 return JIM_ERR;
3915 return target_array2mem(interp, target, argc-1, argv + 1);
3918 static int target_array2mem(Jim_Interp *interp, struct target *target,
3919 int argc, Jim_Obj *const *argv)
3921 long l;
3922 uint32_t width;
3923 int len;
3924 uint32_t addr;
3925 uint32_t count;
3926 uint32_t v;
3927 const char *varname;
3928 int n, e, retval;
3929 uint32_t i;
3931 /* argv[1] = name of array to get the data
3932 * argv[2] = desired width
3933 * argv[3] = memory address
3934 * argv[4] = count to write
3936 if (argc != 4) {
3937 Jim_WrongNumArgs(interp, 0, argv, "varname width addr nelems");
3938 return JIM_ERR;
3940 varname = Jim_GetString(argv[0], &len);
3941 /* given "foo" get space for worse case "foo(%d)" .. add 20 */
3943 e = Jim_GetLong(interp, argv[1], &l);
3944 width = l;
3945 if (e != JIM_OK)
3946 return e;
3948 e = Jim_GetLong(interp, argv[2], &l);
3949 addr = l;
3950 if (e != JIM_OK)
3951 return e;
3952 e = Jim_GetLong(interp, argv[3], &l);
3953 len = l;
3954 if (e != JIM_OK)
3955 return e;
3956 switch (width) {
3957 case 8:
3958 width = 1;
3959 break;
3960 case 16:
3961 width = 2;
3962 break;
3963 case 32:
3964 width = 4;
3965 break;
3966 default:
3967 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3968 Jim_AppendStrings(interp, Jim_GetResult(interp),
3969 "Invalid width param, must be 8/16/32", NULL);
3970 return JIM_ERR;
3972 if (len == 0) {
3973 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3974 Jim_AppendStrings(interp, Jim_GetResult(interp),
3975 "array2mem: zero width read?", NULL);
3976 return JIM_ERR;
3978 if ((addr + (len * width)) < addr) {
3979 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3980 Jim_AppendStrings(interp, Jim_GetResult(interp),
3981 "array2mem: addr + len - wraps to zero?", NULL);
3982 return JIM_ERR;
3984 /* absurd transfer size? */
3985 if (len > 65536) {
3986 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3987 Jim_AppendStrings(interp, Jim_GetResult(interp),
3988 "array2mem: absurd > 64K item request", NULL);
3989 return JIM_ERR;
3992 if ((width == 1) ||
3993 ((width == 2) && ((addr & 1) == 0)) ||
3994 ((width == 4) && ((addr & 3) == 0))) {
3995 /* all is well */
3996 } else {
3997 char buf[100];
3998 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3999 sprintf(buf, "array2mem address: 0x%08x is not aligned for %d byte reads",
4000 (unsigned int)addr,
4001 (int)width);
4002 Jim_AppendStrings(interp, Jim_GetResult(interp), buf , NULL);
4003 return JIM_ERR;
4006 /* Transfer loop */
4008 /* index counter */
4009 n = 0;
4010 /* assume ok */
4011 e = JIM_OK;
4013 size_t buffersize = 4096;
4014 uint8_t *buffer = malloc(buffersize);
4015 if (buffer == NULL)
4016 return JIM_ERR;
4018 while (len) {
4019 /* Slurp... in buffer size chunks */
4021 count = len; /* in objects.. */
4022 if (count > (buffersize / width))
4023 count = (buffersize / width);
4025 v = 0; /* shut up gcc */
4026 for (i = 0; i < count; i++, n++) {
4027 get_int_array_element(interp, varname, n, &v);
4028 switch (width) {
4029 case 4:
4030 target_buffer_set_u32(target, &buffer[i * width], v);
4031 break;
4032 case 2:
4033 target_buffer_set_u16(target, &buffer[i * width], v);
4034 break;
4035 case 1:
4036 buffer[i] = v & 0x0ff;
4037 break;
4040 len -= count;
4042 retval = target_write_memory(target, addr, width, count, buffer);
4043 if (retval != ERROR_OK) {
4044 /* BOO !*/
4045 LOG_ERROR("array2mem: Write @ 0x%08x, w=%d, cnt=%d, failed",
4046 (unsigned int)addr,
4047 (int)width,
4048 (int)count);
4049 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
4050 Jim_AppendStrings(interp, Jim_GetResult(interp), "array2mem: cannot read memory", NULL);
4051 e = JIM_ERR;
4052 break;
4054 addr += count * width;
4057 free(buffer);
4059 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
4061 return e;
4064 /* FIX? should we propagate errors here rather than printing them
4065 * and continuing?
4067 void target_handle_event(struct target *target, enum target_event e)
4069 struct target_event_action *teap;
4071 for (teap = target->event_action; teap != NULL; teap = teap->next) {
4072 if (teap->event == e) {
4073 LOG_DEBUG("target: (%d) %s (%s) event: %d (%s) action: %s",
4074 target->target_number,
4075 target_name(target),
4076 target_type_name(target),
4078 Jim_Nvp_value2name_simple(nvp_target_event, e)->name,
4079 Jim_GetString(teap->body, NULL));
4080 if (Jim_EvalObj(teap->interp, teap->body) != JIM_OK) {
4081 Jim_MakeErrorMessage(teap->interp);
4082 command_print(NULL, "%s\n", Jim_GetString(Jim_GetResult(teap->interp), NULL));
4089 * Returns true only if the target has a handler for the specified event.
4091 bool target_has_event_action(struct target *target, enum target_event event)
4093 struct target_event_action *teap;
4095 for (teap = target->event_action; teap != NULL; teap = teap->next) {
4096 if (teap->event == event)
4097 return true;
4099 return false;
4102 enum target_cfg_param {
4103 TCFG_TYPE,
4104 TCFG_EVENT,
4105 TCFG_WORK_AREA_VIRT,
4106 TCFG_WORK_AREA_PHYS,
4107 TCFG_WORK_AREA_SIZE,
4108 TCFG_WORK_AREA_BACKUP,
4109 TCFG_ENDIAN,
4110 TCFG_VARIANT,
4111 TCFG_COREID,
4112 TCFG_CHAIN_POSITION,
4113 TCFG_DBGBASE,
4114 TCFG_RTOS,
4117 static Jim_Nvp nvp_config_opts[] = {
4118 { .name = "-type", .value = TCFG_TYPE },
4119 { .name = "-event", .value = TCFG_EVENT },
4120 { .name = "-work-area-virt", .value = TCFG_WORK_AREA_VIRT },
4121 { .name = "-work-area-phys", .value = TCFG_WORK_AREA_PHYS },
4122 { .name = "-work-area-size", .value = TCFG_WORK_AREA_SIZE },
4123 { .name = "-work-area-backup", .value = TCFG_WORK_AREA_BACKUP },
4124 { .name = "-endian" , .value = TCFG_ENDIAN },
4125 { .name = "-variant", .value = TCFG_VARIANT },
4126 { .name = "-coreid", .value = TCFG_COREID },
4127 { .name = "-chain-position", .value = TCFG_CHAIN_POSITION },
4128 { .name = "-dbgbase", .value = TCFG_DBGBASE },
4129 { .name = "-rtos", .value = TCFG_RTOS },
4130 { .name = NULL, .value = -1 }
4133 static int target_configure(Jim_GetOptInfo *goi, struct target *target)
4135 Jim_Nvp *n;
4136 Jim_Obj *o;
4137 jim_wide w;
4138 char *cp;
4139 int e;
4141 /* parse config or cget options ... */
4142 while (goi->argc > 0) {
4143 Jim_SetEmptyResult(goi->interp);
4144 /* Jim_GetOpt_Debug(goi); */
4146 if (target->type->target_jim_configure) {
4147 /* target defines a configure function */
4148 /* target gets first dibs on parameters */
4149 e = (*(target->type->target_jim_configure))(target, goi);
4150 if (e == JIM_OK) {
4151 /* more? */
4152 continue;
4154 if (e == JIM_ERR) {
4155 /* An error */
4156 return e;
4158 /* otherwise we 'continue' below */
4160 e = Jim_GetOpt_Nvp(goi, nvp_config_opts, &n);
4161 if (e != JIM_OK) {
4162 Jim_GetOpt_NvpUnknown(goi, nvp_config_opts, 0);
4163 return e;
4165 switch (n->value) {
4166 case TCFG_TYPE:
4167 /* not setable */
4168 if (goi->isconfigure) {
4169 Jim_SetResultFormatted(goi->interp,
4170 "not settable: %s", n->name);
4171 return JIM_ERR;
4172 } else {
4173 no_params:
4174 if (goi->argc != 0) {
4175 Jim_WrongNumArgs(goi->interp,
4176 goi->argc, goi->argv,
4177 "NO PARAMS");
4178 return JIM_ERR;
4181 Jim_SetResultString(goi->interp,
4182 target_type_name(target), -1);
4183 /* loop for more */
4184 break;
4185 case TCFG_EVENT:
4186 if (goi->argc == 0) {
4187 Jim_WrongNumArgs(goi->interp, goi->argc, goi->argv, "-event ?event-name? ...");
4188 return JIM_ERR;
4191 e = Jim_GetOpt_Nvp(goi, nvp_target_event, &n);
4192 if (e != JIM_OK) {
4193 Jim_GetOpt_NvpUnknown(goi, nvp_target_event, 1);
4194 return e;
4197 if (goi->isconfigure) {
4198 if (goi->argc != 1) {
4199 Jim_WrongNumArgs(goi->interp, goi->argc, goi->argv, "-event ?event-name? ?EVENT-BODY?");
4200 return JIM_ERR;
4202 } else {
4203 if (goi->argc != 0) {
4204 Jim_WrongNumArgs(goi->interp, goi->argc, goi->argv, "-event ?event-name?");
4205 return JIM_ERR;
4210 struct target_event_action *teap;
4212 teap = target->event_action;
4213 /* replace existing? */
4214 while (teap) {
4215 if (teap->event == (enum target_event)n->value)
4216 break;
4217 teap = teap->next;
4220 if (goi->isconfigure) {
4221 bool replace = true;
4222 if (teap == NULL) {
4223 /* create new */
4224 teap = calloc(1, sizeof(*teap));
4225 replace = false;
4227 teap->event = n->value;
4228 teap->interp = goi->interp;
4229 Jim_GetOpt_Obj(goi, &o);
4230 if (teap->body)
4231 Jim_DecrRefCount(teap->interp, teap->body);
4232 teap->body = Jim_DuplicateObj(goi->interp, o);
4234 * FIXME:
4235 * Tcl/TK - "tk events" have a nice feature.
4236 * See the "BIND" command.
4237 * We should support that here.
4238 * You can specify %X and %Y in the event code.
4239 * The idea is: %T - target name.
4240 * The idea is: %N - target number
4241 * The idea is: %E - event name.
4243 Jim_IncrRefCount(teap->body);
4245 if (!replace) {
4246 /* add to head of event list */
4247 teap->next = target->event_action;
4248 target->event_action = teap;
4250 Jim_SetEmptyResult(goi->interp);
4251 } else {
4252 /* get */
4253 if (teap == NULL)
4254 Jim_SetEmptyResult(goi->interp);
4255 else
4256 Jim_SetResult(goi->interp, Jim_DuplicateObj(goi->interp, teap->body));
4259 /* loop for more */
4260 break;
4262 case TCFG_WORK_AREA_VIRT:
4263 if (goi->isconfigure) {
4264 target_free_all_working_areas(target);
4265 e = Jim_GetOpt_Wide(goi, &w);
4266 if (e != JIM_OK)
4267 return e;
4268 target->working_area_virt = w;
4269 target->working_area_virt_spec = true;
4270 } else {
4271 if (goi->argc != 0)
4272 goto no_params;
4274 Jim_SetResult(goi->interp, Jim_NewIntObj(goi->interp, target->working_area_virt));
4275 /* loop for more */
4276 break;
4278 case TCFG_WORK_AREA_PHYS:
4279 if (goi->isconfigure) {
4280 target_free_all_working_areas(target);
4281 e = Jim_GetOpt_Wide(goi, &w);
4282 if (e != JIM_OK)
4283 return e;
4284 target->working_area_phys = w;
4285 target->working_area_phys_spec = true;
4286 } else {
4287 if (goi->argc != 0)
4288 goto no_params;
4290 Jim_SetResult(goi->interp, Jim_NewIntObj(goi->interp, target->working_area_phys));
4291 /* loop for more */
4292 break;
4294 case TCFG_WORK_AREA_SIZE:
4295 if (goi->isconfigure) {
4296 target_free_all_working_areas(target);
4297 e = Jim_GetOpt_Wide(goi, &w);
4298 if (e != JIM_OK)
4299 return e;
4300 target->working_area_size = w;
4301 } else {
4302 if (goi->argc != 0)
4303 goto no_params;
4305 Jim_SetResult(goi->interp, Jim_NewIntObj(goi->interp, target->working_area_size));
4306 /* loop for more */
4307 break;
4309 case TCFG_WORK_AREA_BACKUP:
4310 if (goi->isconfigure) {
4311 target_free_all_working_areas(target);
4312 e = Jim_GetOpt_Wide(goi, &w);
4313 if (e != JIM_OK)
4314 return e;
4315 /* make this exactly 1 or 0 */
4316 target->backup_working_area = (!!w);
4317 } else {
4318 if (goi->argc != 0)
4319 goto no_params;
4321 Jim_SetResult(goi->interp, Jim_NewIntObj(goi->interp, target->backup_working_area));
4322 /* loop for more e*/
4323 break;
4326 case TCFG_ENDIAN:
4327 if (goi->isconfigure) {
4328 e = Jim_GetOpt_Nvp(goi, nvp_target_endian, &n);
4329 if (e != JIM_OK) {
4330 Jim_GetOpt_NvpUnknown(goi, nvp_target_endian, 1);
4331 return e;
4333 target->endianness = n->value;
4334 } else {
4335 if (goi->argc != 0)
4336 goto no_params;
4338 n = Jim_Nvp_value2name_simple(nvp_target_endian, target->endianness);
4339 if (n->name == NULL) {
4340 target->endianness = TARGET_LITTLE_ENDIAN;
4341 n = Jim_Nvp_value2name_simple(nvp_target_endian, target->endianness);
4343 Jim_SetResultString(goi->interp, n->name, -1);
4344 /* loop for more */
4345 break;
4347 case TCFG_VARIANT:
4348 if (goi->isconfigure) {
4349 if (goi->argc < 1) {
4350 Jim_SetResultFormatted(goi->interp,
4351 "%s ?STRING?",
4352 n->name);
4353 return JIM_ERR;
4355 e = Jim_GetOpt_String(goi, &cp, NULL);
4356 if (e != JIM_OK)
4357 return e;
4358 free(target->variant);
4359 target->variant = strdup(cp);
4360 } else {
4361 if (goi->argc != 0)
4362 goto no_params;
4364 Jim_SetResultString(goi->interp, target->variant, -1);
4365 /* loop for more */
4366 break;
4368 case TCFG_COREID:
4369 if (goi->isconfigure) {
4370 e = Jim_GetOpt_Wide(goi, &w);
4371 if (e != JIM_OK)
4372 return e;
4373 target->coreid = (int32_t)w;
4374 } else {
4375 if (goi->argc != 0)
4376 goto no_params;
4378 Jim_SetResult(goi->interp, Jim_NewIntObj(goi->interp, target->working_area_size));
4379 /* loop for more */
4380 break;
4382 case TCFG_CHAIN_POSITION:
4383 if (goi->isconfigure) {
4384 Jim_Obj *o_t;
4385 struct jtag_tap *tap;
4386 target_free_all_working_areas(target);
4387 e = Jim_GetOpt_Obj(goi, &o_t);
4388 if (e != JIM_OK)
4389 return e;
4390 tap = jtag_tap_by_jim_obj(goi->interp, o_t);
4391 if (tap == NULL)
4392 return JIM_ERR;
4393 /* make this exactly 1 or 0 */
4394 target->tap = tap;
4395 } else {
4396 if (goi->argc != 0)
4397 goto no_params;
4399 Jim_SetResultString(goi->interp, target->tap->dotted_name, -1);
4400 /* loop for more e*/
4401 break;
4402 case TCFG_DBGBASE:
4403 if (goi->isconfigure) {
4404 e = Jim_GetOpt_Wide(goi, &w);
4405 if (e != JIM_OK)
4406 return e;
4407 target->dbgbase = (uint32_t)w;
4408 target->dbgbase_set = true;
4409 } else {
4410 if (goi->argc != 0)
4411 goto no_params;
4413 Jim_SetResult(goi->interp, Jim_NewIntObj(goi->interp, target->dbgbase));
4414 /* loop for more */
4415 break;
4417 case TCFG_RTOS:
4418 /* RTOS */
4420 int result = rtos_create(goi, target);
4421 if (result != JIM_OK)
4422 return result;
4424 /* loop for more */
4425 break;
4427 } /* while (goi->argc) */
4430 /* done - we return */
4431 return JIM_OK;
4434 static int jim_target_configure(Jim_Interp *interp, int argc, Jim_Obj * const *argv)
4436 Jim_GetOptInfo goi;
4438 Jim_GetOpt_Setup(&goi, interp, argc - 1, argv + 1);
4439 goi.isconfigure = !strcmp(Jim_GetString(argv[0], NULL), "configure");
4440 int need_args = 1 + goi.isconfigure;
4441 if (goi.argc < need_args) {
4442 Jim_WrongNumArgs(goi.interp, goi.argc, goi.argv,
4443 goi.isconfigure
4444 ? "missing: -option VALUE ..."
4445 : "missing: -option ...");
4446 return JIM_ERR;
4448 struct target *target = Jim_CmdPrivData(goi.interp);
4449 return target_configure(&goi, target);
4452 static int jim_target_mw(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
4454 const char *cmd_name = Jim_GetString(argv[0], NULL);
4456 Jim_GetOptInfo goi;
4457 Jim_GetOpt_Setup(&goi, interp, argc - 1, argv + 1);
4459 if (goi.argc < 2 || goi.argc > 4) {
4460 Jim_SetResultFormatted(goi.interp,
4461 "usage: %s [phys] <address> <data> [<count>]", cmd_name);
4462 return JIM_ERR;
4465 target_write_fn fn;
4466 fn = target_write_memory;
4468 int e;
4469 if (strcmp(Jim_GetString(argv[1], NULL), "phys") == 0) {
4470 /* consume it */
4471 struct Jim_Obj *obj;
4472 e = Jim_GetOpt_Obj(&goi, &obj);
4473 if (e != JIM_OK)
4474 return e;
4476 fn = target_write_phys_memory;
4479 jim_wide a;
4480 e = Jim_GetOpt_Wide(&goi, &a);
4481 if (e != JIM_OK)
4482 return e;
4484 jim_wide b;
4485 e = Jim_GetOpt_Wide(&goi, &b);
4486 if (e != JIM_OK)
4487 return e;
4489 jim_wide c = 1;
4490 if (goi.argc == 1) {
4491 e = Jim_GetOpt_Wide(&goi, &c);
4492 if (e != JIM_OK)
4493 return e;
4496 /* all args must be consumed */
4497 if (goi.argc != 0)
4498 return JIM_ERR;
4500 struct target *target = Jim_CmdPrivData(goi.interp);
4501 unsigned data_size;
4502 if (strcasecmp(cmd_name, "mww") == 0)
4503 data_size = 4;
4504 else if (strcasecmp(cmd_name, "mwh") == 0)
4505 data_size = 2;
4506 else if (strcasecmp(cmd_name, "mwb") == 0)
4507 data_size = 1;
4508 else {
4509 LOG_ERROR("command '%s' unknown: ", cmd_name);
4510 return JIM_ERR;
4513 return (target_fill_mem(target, a, fn, data_size, b, c) == ERROR_OK) ? JIM_OK : JIM_ERR;
4517 * @brief Reads an array of words/halfwords/bytes from target memory starting at specified address.
4519 * Usage: mdw [phys] <address> [<count>] - for 32 bit reads
4520 * mdh [phys] <address> [<count>] - for 16 bit reads
4521 * mdb [phys] <address> [<count>] - for 8 bit reads
4523 * Count defaults to 1.
4525 * Calls target_read_memory or target_read_phys_memory depending on
4526 * the presence of the "phys" argument
4527 * Reads the target memory in blocks of max. 32 bytes, and returns an array of ints formatted
4528 * to int representation in base16.
4529 * Also outputs read data in a human readable form using command_print
4531 * @param phys if present target_read_phys_memory will be used instead of target_read_memory
4532 * @param address address where to start the read. May be specified in decimal or hex using the standard "0x" prefix
4533 * @param count optional count parameter to read an array of values. If not specified, defaults to 1.
4534 * @returns: JIM_ERR on error or JIM_OK on success and sets the result string to an array of ascii formatted numbers
4535 * on success, with [<count>] number of elements.
4537 * In case of little endian target:
4538 * Example1: "mdw 0x00000000" returns "10123456"
4539 * Exmaple2: "mdh 0x00000000 1" returns "3456"
4540 * Example3: "mdb 0x00000000" returns "56"
4541 * Example4: "mdh 0x00000000 2" returns "3456 1012"
4542 * Example5: "mdb 0x00000000 3" returns "56 34 12"
4544 static int jim_target_md(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
4546 const char *cmd_name = Jim_GetString(argv[0], NULL);
4548 Jim_GetOptInfo goi;
4549 Jim_GetOpt_Setup(&goi, interp, argc - 1, argv + 1);
4551 if ((goi.argc < 1) || (goi.argc > 3)) {
4552 Jim_SetResultFormatted(goi.interp,
4553 "usage: %s [phys] <address> [<count>]", cmd_name);
4554 return JIM_ERR;
4557 int (*fn)(struct target *target,
4558 uint32_t address, uint32_t size, uint32_t count, uint8_t *buffer);
4559 fn = target_read_memory;
4561 int e;
4562 if (strcmp(Jim_GetString(argv[1], NULL), "phys") == 0) {
4563 /* consume it */
4564 struct Jim_Obj *obj;
4565 e = Jim_GetOpt_Obj(&goi, &obj);
4566 if (e != JIM_OK)
4567 return e;
4569 fn = target_read_phys_memory;
4572 /* Read address parameter */
4573 jim_wide addr;
4574 e = Jim_GetOpt_Wide(&goi, &addr);
4575 if (e != JIM_OK)
4576 return JIM_ERR;
4578 /* If next parameter exists, read it out as the count parameter, if not, set it to 1 (default) */
4579 jim_wide count;
4580 if (goi.argc == 1) {
4581 e = Jim_GetOpt_Wide(&goi, &count);
4582 if (e != JIM_OK)
4583 return JIM_ERR;
4584 } else
4585 count = 1;
4587 /* all args must be consumed */
4588 if (goi.argc != 0)
4589 return JIM_ERR;
4591 jim_wide dwidth = 1; /* shut up gcc */
4592 if (strcasecmp(cmd_name, "mdw") == 0)
4593 dwidth = 4;
4594 else if (strcasecmp(cmd_name, "mdh") == 0)
4595 dwidth = 2;
4596 else if (strcasecmp(cmd_name, "mdb") == 0)
4597 dwidth = 1;
4598 else {
4599 LOG_ERROR("command '%s' unknown: ", cmd_name);
4600 return JIM_ERR;
4603 /* convert count to "bytes" */
4604 int bytes = count * dwidth;
4606 struct target *target = Jim_CmdPrivData(goi.interp);
4607 uint8_t target_buf[32];
4608 jim_wide x, y, z;
4609 while (bytes > 0) {
4610 y = (bytes < 16) ? bytes : 16; /* y = min(bytes, 16); */
4612 /* Try to read out next block */
4613 e = fn(target, addr, dwidth, y / dwidth, target_buf);
4615 if (e != ERROR_OK) {
4616 Jim_SetResultFormatted(interp, "error reading target @ 0x%08lx", (long)addr);
4617 return JIM_ERR;
4620 command_print_sameline(NULL, "0x%08x ", (int)(addr));
4621 switch (dwidth) {
4622 case 4:
4623 for (x = 0; x < 16 && x < y; x += 4) {
4624 z = target_buffer_get_u32(target, &(target_buf[x]));
4625 command_print_sameline(NULL, "%08x ", (int)(z));
4627 for (; (x < 16) ; x += 4)
4628 command_print_sameline(NULL, " ");
4629 break;
4630 case 2:
4631 for (x = 0; x < 16 && x < y; x += 2) {
4632 z = target_buffer_get_u16(target, &(target_buf[x]));
4633 command_print_sameline(NULL, "%04x ", (int)(z));
4635 for (; (x < 16) ; x += 2)
4636 command_print_sameline(NULL, " ");
4637 break;
4638 case 1:
4639 default:
4640 for (x = 0 ; (x < 16) && (x < y) ; x += 1) {
4641 z = target_buffer_get_u8(target, &(target_buf[x]));
4642 command_print_sameline(NULL, "%02x ", (int)(z));
4644 for (; (x < 16) ; x += 1)
4645 command_print_sameline(NULL, " ");
4646 break;
4648 /* ascii-ify the bytes */
4649 for (x = 0 ; x < y ; x++) {
4650 if ((target_buf[x] >= 0x20) &&
4651 (target_buf[x] <= 0x7e)) {
4652 /* good */
4653 } else {
4654 /* smack it */
4655 target_buf[x] = '.';
4658 /* space pad */
4659 while (x < 16) {
4660 target_buf[x] = ' ';
4661 x++;
4663 /* terminate */
4664 target_buf[16] = 0;
4665 /* print - with a newline */
4666 command_print_sameline(NULL, "%s\n", target_buf);
4667 /* NEXT... */
4668 bytes -= 16;
4669 addr += 16;
4671 return JIM_OK;
4674 static int jim_target_mem2array(Jim_Interp *interp,
4675 int argc, Jim_Obj *const *argv)
4677 struct target *target = Jim_CmdPrivData(interp);
4678 return target_mem2array(interp, target, argc - 1, argv + 1);
4681 static int jim_target_array2mem(Jim_Interp *interp,
4682 int argc, Jim_Obj *const *argv)
4684 struct target *target = Jim_CmdPrivData(interp);
4685 return target_array2mem(interp, target, argc - 1, argv + 1);
4688 static int jim_target_tap_disabled(Jim_Interp *interp)
4690 Jim_SetResultFormatted(interp, "[TAP is disabled]");
4691 return JIM_ERR;
4694 static int jim_target_examine(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
4696 if (argc != 1) {
4697 Jim_WrongNumArgs(interp, 1, argv, "[no parameters]");
4698 return JIM_ERR;
4700 struct target *target = Jim_CmdPrivData(interp);
4701 if (!target->tap->enabled)
4702 return jim_target_tap_disabled(interp);
4704 int e = target->type->examine(target);
4705 if (e != ERROR_OK)
4706 return JIM_ERR;
4707 return JIM_OK;
4710 static int jim_target_halt_gdb(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
4712 if (argc != 1) {
4713 Jim_WrongNumArgs(interp, 1, argv, "[no parameters]");
4714 return JIM_ERR;
4716 struct target *target = Jim_CmdPrivData(interp);
4718 if (target_call_event_callbacks(target, TARGET_EVENT_GDB_HALT) != ERROR_OK)
4719 return JIM_ERR;
4721 return JIM_OK;
4724 static int jim_target_poll(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
4726 if (argc != 1) {
4727 Jim_WrongNumArgs(interp, 1, argv, "[no parameters]");
4728 return JIM_ERR;
4730 struct target *target = Jim_CmdPrivData(interp);
4731 if (!target->tap->enabled)
4732 return jim_target_tap_disabled(interp);
4734 int e;
4735 if (!(target_was_examined(target)))
4736 e = ERROR_TARGET_NOT_EXAMINED;
4737 else
4738 e = target->type->poll(target);
4739 if (e != ERROR_OK)
4740 return JIM_ERR;
4741 return JIM_OK;
4744 static int jim_target_reset(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
4746 Jim_GetOptInfo goi;
4747 Jim_GetOpt_Setup(&goi, interp, argc - 1, argv + 1);
4749 if (goi.argc != 2) {
4750 Jim_WrongNumArgs(interp, 0, argv,
4751 "([tT]|[fF]|assert|deassert) BOOL");
4752 return JIM_ERR;
4755 Jim_Nvp *n;
4756 int e = Jim_GetOpt_Nvp(&goi, nvp_assert, &n);
4757 if (e != JIM_OK) {
4758 Jim_GetOpt_NvpUnknown(&goi, nvp_assert, 1);
4759 return e;
4761 /* the halt or not param */
4762 jim_wide a;
4763 e = Jim_GetOpt_Wide(&goi, &a);
4764 if (e != JIM_OK)
4765 return e;
4767 struct target *target = Jim_CmdPrivData(goi.interp);
4768 if (!target->tap->enabled)
4769 return jim_target_tap_disabled(interp);
4770 if (!(target_was_examined(target))) {
4771 LOG_ERROR("Target not examined yet");
4772 return ERROR_TARGET_NOT_EXAMINED;
4774 if (!target->type->assert_reset || !target->type->deassert_reset) {
4775 Jim_SetResultFormatted(interp,
4776 "No target-specific reset for %s",
4777 target_name(target));
4778 return JIM_ERR;
4780 /* determine if we should halt or not. */
4781 target->reset_halt = !!a;
4782 /* When this happens - all workareas are invalid. */
4783 target_free_all_working_areas_restore(target, 0);
4785 /* do the assert */
4786 if (n->value == NVP_ASSERT)
4787 e = target->type->assert_reset(target);
4788 else
4789 e = target->type->deassert_reset(target);
4790 return (e == ERROR_OK) ? JIM_OK : JIM_ERR;
4793 static int jim_target_halt(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
4795 if (argc != 1) {
4796 Jim_WrongNumArgs(interp, 1, argv, "[no parameters]");
4797 return JIM_ERR;
4799 struct target *target = Jim_CmdPrivData(interp);
4800 if (!target->tap->enabled)
4801 return jim_target_tap_disabled(interp);
4802 int e = target->type->halt(target);
4803 return (e == ERROR_OK) ? JIM_OK : JIM_ERR;
4806 static int jim_target_wait_state(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
4808 Jim_GetOptInfo goi;
4809 Jim_GetOpt_Setup(&goi, interp, argc - 1, argv + 1);
4811 /* params: <name> statename timeoutmsecs */
4812 if (goi.argc != 2) {
4813 const char *cmd_name = Jim_GetString(argv[0], NULL);
4814 Jim_SetResultFormatted(goi.interp,
4815 "%s <state_name> <timeout_in_msec>", cmd_name);
4816 return JIM_ERR;
4819 Jim_Nvp *n;
4820 int e = Jim_GetOpt_Nvp(&goi, nvp_target_state, &n);
4821 if (e != JIM_OK) {
4822 Jim_GetOpt_NvpUnknown(&goi, nvp_target_state, 1);
4823 return e;
4825 jim_wide a;
4826 e = Jim_GetOpt_Wide(&goi, &a);
4827 if (e != JIM_OK)
4828 return e;
4829 struct target *target = Jim_CmdPrivData(interp);
4830 if (!target->tap->enabled)
4831 return jim_target_tap_disabled(interp);
4833 e = target_wait_state(target, n->value, a);
4834 if (e != ERROR_OK) {
4835 Jim_Obj *eObj = Jim_NewIntObj(interp, e);
4836 Jim_SetResultFormatted(goi.interp,
4837 "target: %s wait %s fails (%#s) %s",
4838 target_name(target), n->name,
4839 eObj, target_strerror_safe(e));
4840 Jim_FreeNewObj(interp, eObj);
4841 return JIM_ERR;
4843 return JIM_OK;
4845 /* List for human, Events defined for this target.
4846 * scripts/programs should use 'name cget -event NAME'
4848 static int jim_target_event_list(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
4850 struct command_context *cmd_ctx = current_command_context(interp);
4851 assert(cmd_ctx != NULL);
4853 struct target *target = Jim_CmdPrivData(interp);
4854 struct target_event_action *teap = target->event_action;
4855 command_print(cmd_ctx, "Event actions for target (%d) %s\n",
4856 target->target_number,
4857 target_name(target));
4858 command_print(cmd_ctx, "%-25s | Body", "Event");
4859 command_print(cmd_ctx, "------------------------- | "
4860 "----------------------------------------");
4861 while (teap) {
4862 Jim_Nvp *opt = Jim_Nvp_value2name_simple(nvp_target_event, teap->event);
4863 command_print(cmd_ctx, "%-25s | %s",
4864 opt->name, Jim_GetString(teap->body, NULL));
4865 teap = teap->next;
4867 command_print(cmd_ctx, "***END***");
4868 return JIM_OK;
4870 static int jim_target_current_state(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
4872 if (argc != 1) {
4873 Jim_WrongNumArgs(interp, 1, argv, "[no parameters]");
4874 return JIM_ERR;
4876 struct target *target = Jim_CmdPrivData(interp);
4877 Jim_SetResultString(interp, target_state_name(target), -1);
4878 return JIM_OK;
4880 static int jim_target_invoke_event(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
4882 Jim_GetOptInfo goi;
4883 Jim_GetOpt_Setup(&goi, interp, argc - 1, argv + 1);
4884 if (goi.argc != 1) {
4885 const char *cmd_name = Jim_GetString(argv[0], NULL);
4886 Jim_SetResultFormatted(goi.interp, "%s <eventname>", cmd_name);
4887 return JIM_ERR;
4889 Jim_Nvp *n;
4890 int e = Jim_GetOpt_Nvp(&goi, nvp_target_event, &n);
4891 if (e != JIM_OK) {
4892 Jim_GetOpt_NvpUnknown(&goi, nvp_target_event, 1);
4893 return e;
4895 struct target *target = Jim_CmdPrivData(interp);
4896 target_handle_event(target, n->value);
4897 return JIM_OK;
4900 static const struct command_registration target_instance_command_handlers[] = {
4902 .name = "configure",
4903 .mode = COMMAND_CONFIG,
4904 .jim_handler = jim_target_configure,
4905 .help = "configure a new target for use",
4906 .usage = "[target_attribute ...]",
4909 .name = "cget",
4910 .mode = COMMAND_ANY,
4911 .jim_handler = jim_target_configure,
4912 .help = "returns the specified target attribute",
4913 .usage = "target_attribute",
4916 .name = "mww",
4917 .mode = COMMAND_EXEC,
4918 .jim_handler = jim_target_mw,
4919 .help = "Write 32-bit word(s) to target memory",
4920 .usage = "address data [count]",
4923 .name = "mwh",
4924 .mode = COMMAND_EXEC,
4925 .jim_handler = jim_target_mw,
4926 .help = "Write 16-bit half-word(s) to target memory",
4927 .usage = "address data [count]",
4930 .name = "mwb",
4931 .mode = COMMAND_EXEC,
4932 .jim_handler = jim_target_mw,
4933 .help = "Write byte(s) to target memory",
4934 .usage = "address data [count]",
4937 .name = "mdw",
4938 .mode = COMMAND_EXEC,
4939 .jim_handler = jim_target_md,
4940 .help = "Display target memory as 32-bit words",
4941 .usage = "address [count]",
4944 .name = "mdh",
4945 .mode = COMMAND_EXEC,
4946 .jim_handler = jim_target_md,
4947 .help = "Display target memory as 16-bit half-words",
4948 .usage = "address [count]",
4951 .name = "mdb",
4952 .mode = COMMAND_EXEC,
4953 .jim_handler = jim_target_md,
4954 .help = "Display target memory as 8-bit bytes",
4955 .usage = "address [count]",
4958 .name = "array2mem",
4959 .mode = COMMAND_EXEC,
4960 .jim_handler = jim_target_array2mem,
4961 .help = "Writes Tcl array of 8/16/32 bit numbers "
4962 "to target memory",
4963 .usage = "arrayname bitwidth address count",
4966 .name = "mem2array",
4967 .mode = COMMAND_EXEC,
4968 .jim_handler = jim_target_mem2array,
4969 .help = "Loads Tcl array of 8/16/32 bit numbers "
4970 "from target memory",
4971 .usage = "arrayname bitwidth address count",
4974 .name = "eventlist",
4975 .mode = COMMAND_EXEC,
4976 .jim_handler = jim_target_event_list,
4977 .help = "displays a table of events defined for this target",
4980 .name = "curstate",
4981 .mode = COMMAND_EXEC,
4982 .jim_handler = jim_target_current_state,
4983 .help = "displays the current state of this target",
4986 .name = "arp_examine",
4987 .mode = COMMAND_EXEC,
4988 .jim_handler = jim_target_examine,
4989 .help = "used internally for reset processing",
4992 .name = "arp_halt_gdb",
4993 .mode = COMMAND_EXEC,
4994 .jim_handler = jim_target_halt_gdb,
4995 .help = "used internally for reset processing to halt GDB",
4998 .name = "arp_poll",
4999 .mode = COMMAND_EXEC,
5000 .jim_handler = jim_target_poll,
5001 .help = "used internally for reset processing",
5004 .name = "arp_reset",
5005 .mode = COMMAND_EXEC,
5006 .jim_handler = jim_target_reset,
5007 .help = "used internally for reset processing",
5010 .name = "arp_halt",
5011 .mode = COMMAND_EXEC,
5012 .jim_handler = jim_target_halt,
5013 .help = "used internally for reset processing",
5016 .name = "arp_waitstate",
5017 .mode = COMMAND_EXEC,
5018 .jim_handler = jim_target_wait_state,
5019 .help = "used internally for reset processing",
5022 .name = "invoke-event",
5023 .mode = COMMAND_EXEC,
5024 .jim_handler = jim_target_invoke_event,
5025 .help = "invoke handler for specified event",
5026 .usage = "event_name",
5028 COMMAND_REGISTRATION_DONE
5031 static int target_create(Jim_GetOptInfo *goi)
5033 Jim_Obj *new_cmd;
5034 Jim_Cmd *cmd;
5035 const char *cp;
5036 char *cp2;
5037 int e;
5038 int x;
5039 struct target *target;
5040 struct command_context *cmd_ctx;
5042 cmd_ctx = current_command_context(goi->interp);
5043 assert(cmd_ctx != NULL);
5045 if (goi->argc < 3) {
5046 Jim_WrongNumArgs(goi->interp, 1, goi->argv, "?name? ?type? ..options...");
5047 return JIM_ERR;
5050 /* COMMAND */
5051 Jim_GetOpt_Obj(goi, &new_cmd);
5052 /* does this command exist? */
5053 cmd = Jim_GetCommand(goi->interp, new_cmd, JIM_ERRMSG);
5054 if (cmd) {
5055 cp = Jim_GetString(new_cmd, NULL);
5056 Jim_SetResultFormatted(goi->interp, "Command/target: %s Exists", cp);
5057 return JIM_ERR;
5060 /* TYPE */
5061 e = Jim_GetOpt_String(goi, &cp2, NULL);
5062 if (e != JIM_OK)
5063 return e;
5064 cp = cp2;
5065 /* now does target type exist */
5066 for (x = 0 ; target_types[x] ; x++) {
5067 if (0 == strcmp(cp, target_types[x]->name)) {
5068 /* found */
5069 break;
5072 /* check for deprecated name */
5073 if (target_types[x]->deprecated_name) {
5074 if (0 == strcmp(cp, target_types[x]->deprecated_name)) {
5075 /* found */
5076 LOG_WARNING("target name is deprecated use: \'%s\'", target_types[x]->name);
5077 break;
5081 if (target_types[x] == NULL) {
5082 Jim_SetResultFormatted(goi->interp, "Unknown target type %s, try one of ", cp);
5083 for (x = 0 ; target_types[x] ; x++) {
5084 if (target_types[x + 1]) {
5085 Jim_AppendStrings(goi->interp,
5086 Jim_GetResult(goi->interp),
5087 target_types[x]->name,
5088 ", ", NULL);
5089 } else {
5090 Jim_AppendStrings(goi->interp,
5091 Jim_GetResult(goi->interp),
5092 " or ",
5093 target_types[x]->name, NULL);
5096 return JIM_ERR;
5099 /* Create it */
5100 target = calloc(1, sizeof(struct target));
5101 /* set target number */
5102 target->target_number = new_target_number();
5104 /* allocate memory for each unique target type */
5105 target->type = calloc(1, sizeof(struct target_type));
5107 memcpy(target->type, target_types[x], sizeof(struct target_type));
5109 /* will be set by "-endian" */
5110 target->endianness = TARGET_ENDIAN_UNKNOWN;
5112 /* default to first core, override with -coreid */
5113 target->coreid = 0;
5115 target->working_area = 0x0;
5116 target->working_area_size = 0x0;
5117 target->working_areas = NULL;
5118 target->backup_working_area = 0;
5120 target->state = TARGET_UNKNOWN;
5121 target->debug_reason = DBG_REASON_UNDEFINED;
5122 target->reg_cache = NULL;
5123 target->breakpoints = NULL;
5124 target->watchpoints = NULL;
5125 target->next = NULL;
5126 target->arch_info = NULL;
5128 target->display = 1;
5130 target->halt_issued = false;
5132 /* initialize trace information */
5133 target->trace_info = malloc(sizeof(struct trace));
5134 target->trace_info->num_trace_points = 0;
5135 target->trace_info->trace_points_size = 0;
5136 target->trace_info->trace_points = NULL;
5137 target->trace_info->trace_history_size = 0;
5138 target->trace_info->trace_history = NULL;
5139 target->trace_info->trace_history_pos = 0;
5140 target->trace_info->trace_history_overflowed = 0;
5142 target->dbgmsg = NULL;
5143 target->dbg_msg_enabled = 0;
5145 target->endianness = TARGET_ENDIAN_UNKNOWN;
5147 target->rtos = NULL;
5148 target->rtos_auto_detect = false;
5150 /* Do the rest as "configure" options */
5151 goi->isconfigure = 1;
5152 e = target_configure(goi, target);
5154 if (target->tap == NULL) {
5155 Jim_SetResultString(goi->interp, "-chain-position required when creating target", -1);
5156 e = JIM_ERR;
5159 if (e != JIM_OK) {
5160 free(target->type);
5161 free(target);
5162 return e;
5165 if (target->endianness == TARGET_ENDIAN_UNKNOWN) {
5166 /* default endian to little if not specified */
5167 target->endianness = TARGET_LITTLE_ENDIAN;
5170 /* incase variant is not set */
5171 if (!target->variant)
5172 target->variant = strdup("");
5174 cp = Jim_GetString(new_cmd, NULL);
5175 target->cmd_name = strdup(cp);
5177 /* create the target specific commands */
5178 if (target->type->commands) {
5179 e = register_commands(cmd_ctx, NULL, target->type->commands);
5180 if (ERROR_OK != e)
5181 LOG_ERROR("unable to register '%s' commands", cp);
5183 if (target->type->target_create)
5184 (*(target->type->target_create))(target, goi->interp);
5186 /* append to end of list */
5188 struct target **tpp;
5189 tpp = &(all_targets);
5190 while (*tpp)
5191 tpp = &((*tpp)->next);
5192 *tpp = target;
5195 /* now - create the new target name command */
5196 const struct command_registration target_subcommands[] = {
5198 .chain = target_instance_command_handlers,
5201 .chain = target->type->commands,
5203 COMMAND_REGISTRATION_DONE
5205 const struct command_registration target_commands[] = {
5207 .name = cp,
5208 .mode = COMMAND_ANY,
5209 .help = "target command group",
5210 .usage = "",
5211 .chain = target_subcommands,
5213 COMMAND_REGISTRATION_DONE
5215 e = register_commands(cmd_ctx, NULL, target_commands);
5216 if (ERROR_OK != e)
5217 return JIM_ERR;
5219 struct command *c = command_find_in_context(cmd_ctx, cp);
5220 assert(c);
5221 command_set_handler_data(c, target);
5223 return (ERROR_OK == e) ? JIM_OK : JIM_ERR;
5226 static int jim_target_current(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
5228 if (argc != 1) {
5229 Jim_WrongNumArgs(interp, 1, argv, "Too many parameters");
5230 return JIM_ERR;
5232 struct command_context *cmd_ctx = current_command_context(interp);
5233 assert(cmd_ctx != NULL);
5235 Jim_SetResultString(interp, target_name(get_current_target(cmd_ctx)), -1);
5236 return JIM_OK;
5239 static int jim_target_types(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
5241 if (argc != 1) {
5242 Jim_WrongNumArgs(interp, 1, argv, "Too many parameters");
5243 return JIM_ERR;
5245 Jim_SetResult(interp, Jim_NewListObj(interp, NULL, 0));
5246 for (unsigned x = 0; NULL != target_types[x]; x++) {
5247 Jim_ListAppendElement(interp, Jim_GetResult(interp),
5248 Jim_NewStringObj(interp, target_types[x]->name, -1));
5250 return JIM_OK;
5253 static int jim_target_names(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
5255 if (argc != 1) {
5256 Jim_WrongNumArgs(interp, 1, argv, "Too many parameters");
5257 return JIM_ERR;
5259 Jim_SetResult(interp, Jim_NewListObj(interp, NULL, 0));
5260 struct target *target = all_targets;
5261 while (target) {
5262 Jim_ListAppendElement(interp, Jim_GetResult(interp),
5263 Jim_NewStringObj(interp, target_name(target), -1));
5264 target = target->next;
5266 return JIM_OK;
5269 static int jim_target_smp(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
5271 int i;
5272 const char *targetname;
5273 int retval, len;
5274 struct target *target = (struct target *) NULL;
5275 struct target_list *head, *curr, *new;
5276 curr = (struct target_list *) NULL;
5277 head = (struct target_list *) NULL;
5279 retval = 0;
5280 LOG_DEBUG("%d", argc);
5281 /* argv[1] = target to associate in smp
5282 * argv[2] = target to assoicate in smp
5283 * argv[3] ...
5286 for (i = 1; i < argc; i++) {
5288 targetname = Jim_GetString(argv[i], &len);
5289 target = get_target(targetname);
5290 LOG_DEBUG("%s ", targetname);
5291 if (target) {
5292 new = malloc(sizeof(struct target_list));
5293 new->target = target;
5294 new->next = (struct target_list *)NULL;
5295 if (head == (struct target_list *)NULL) {
5296 head = new;
5297 curr = head;
5298 } else {
5299 curr->next = new;
5300 curr = new;
5304 /* now parse the list of cpu and put the target in smp mode*/
5305 curr = head;
5307 while (curr != (struct target_list *)NULL) {
5308 target = curr->target;
5309 target->smp = 1;
5310 target->head = head;
5311 curr = curr->next;
5314 if (target && target->rtos)
5315 retval = rtos_smp_init(head->target);
5317 return retval;
5321 static int jim_target_create(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
5323 Jim_GetOptInfo goi;
5324 Jim_GetOpt_Setup(&goi, interp, argc - 1, argv + 1);
5325 if (goi.argc < 3) {
5326 Jim_WrongNumArgs(goi.interp, goi.argc, goi.argv,
5327 "<name> <target_type> [<target_options> ...]");
5328 return JIM_ERR;
5330 return target_create(&goi);
5333 static int jim_target_number(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
5335 Jim_GetOptInfo goi;
5336 Jim_GetOpt_Setup(&goi, interp, argc - 1, argv + 1);
5338 /* It's OK to remove this mechanism sometime after August 2010 or so */
5339 LOG_WARNING("don't use numbers as target identifiers; use names");
5340 if (goi.argc != 1) {
5341 Jim_SetResultFormatted(goi.interp, "usage: target number <number>");
5342 return JIM_ERR;
5344 jim_wide w;
5345 int e = Jim_GetOpt_Wide(&goi, &w);
5346 if (e != JIM_OK)
5347 return JIM_ERR;
5349 struct target *target;
5350 for (target = all_targets; NULL != target; target = target->next) {
5351 if (target->target_number != w)
5352 continue;
5354 Jim_SetResultString(goi.interp, target_name(target), -1);
5355 return JIM_OK;
5358 Jim_Obj *wObj = Jim_NewIntObj(goi.interp, w);
5359 Jim_SetResultFormatted(goi.interp,
5360 "Target: number %#s does not exist", wObj);
5361 Jim_FreeNewObj(interp, wObj);
5363 return JIM_ERR;
5366 static int jim_target_count(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
5368 if (argc != 1) {
5369 Jim_WrongNumArgs(interp, 1, argv, "<no parameters>");
5370 return JIM_ERR;
5372 unsigned count = 0;
5373 struct target *target = all_targets;
5374 while (NULL != target) {
5375 target = target->next;
5376 count++;
5378 Jim_SetResult(interp, Jim_NewIntObj(interp, count));
5379 return JIM_OK;
5382 static const struct command_registration target_subcommand_handlers[] = {
5384 .name = "init",
5385 .mode = COMMAND_CONFIG,
5386 .handler = handle_target_init_command,
5387 .help = "initialize targets",
5390 .name = "create",
5391 /* REVISIT this should be COMMAND_CONFIG ... */
5392 .mode = COMMAND_ANY,
5393 .jim_handler = jim_target_create,
5394 .usage = "name type '-chain-position' name [options ...]",
5395 .help = "Creates and selects a new target",
5398 .name = "current",
5399 .mode = COMMAND_ANY,
5400 .jim_handler = jim_target_current,
5401 .help = "Returns the currently selected target",
5404 .name = "types",
5405 .mode = COMMAND_ANY,
5406 .jim_handler = jim_target_types,
5407 .help = "Returns the available target types as "
5408 "a list of strings",
5411 .name = "names",
5412 .mode = COMMAND_ANY,
5413 .jim_handler = jim_target_names,
5414 .help = "Returns the names of all targets as a list of strings",
5417 .name = "number",
5418 .mode = COMMAND_ANY,
5419 .jim_handler = jim_target_number,
5420 .usage = "number",
5421 .help = "Returns the name of the numbered target "
5422 "(DEPRECATED)",
5425 .name = "count",
5426 .mode = COMMAND_ANY,
5427 .jim_handler = jim_target_count,
5428 .help = "Returns the number of targets as an integer "
5429 "(DEPRECATED)",
5432 .name = "smp",
5433 .mode = COMMAND_ANY,
5434 .jim_handler = jim_target_smp,
5435 .usage = "targetname1 targetname2 ...",
5436 .help = "gather several target in a smp list"
5439 COMMAND_REGISTRATION_DONE
5442 struct FastLoad {
5443 uint32_t address;
5444 uint8_t *data;
5445 int length;
5449 static int fastload_num;
5450 static struct FastLoad *fastload;
5452 static void free_fastload(void)
5454 if (fastload != NULL) {
5455 int i;
5456 for (i = 0; i < fastload_num; i++) {
5457 if (fastload[i].data)
5458 free(fastload[i].data);
5460 free(fastload);
5461 fastload = NULL;
5465 COMMAND_HANDLER(handle_fast_load_image_command)
5467 uint8_t *buffer;
5468 size_t buf_cnt;
5469 uint32_t image_size;
5470 uint32_t min_address = 0;
5471 uint32_t max_address = 0xffffffff;
5472 int i;
5474 struct image image;
5476 int retval = CALL_COMMAND_HANDLER(parse_load_image_command_CMD_ARGV,
5477 &image, &min_address, &max_address);
5478 if (ERROR_OK != retval)
5479 return retval;
5481 struct duration bench;
5482 duration_start(&bench);
5484 retval = image_open(&image, CMD_ARGV[0], (CMD_ARGC >= 3) ? CMD_ARGV[2] : NULL);
5485 if (retval != ERROR_OK)
5486 return retval;
5488 image_size = 0x0;
5489 retval = ERROR_OK;
5490 fastload_num = image.num_sections;
5491 fastload = malloc(sizeof(struct FastLoad)*image.num_sections);
5492 if (fastload == NULL) {
5493 command_print(CMD_CTX, "out of memory");
5494 image_close(&image);
5495 return ERROR_FAIL;
5497 memset(fastload, 0, sizeof(struct FastLoad)*image.num_sections);
5498 for (i = 0; i < image.num_sections; i++) {
5499 buffer = malloc(image.sections[i].size);
5500 if (buffer == NULL) {
5501 command_print(CMD_CTX, "error allocating buffer for section (%d bytes)",
5502 (int)(image.sections[i].size));
5503 retval = ERROR_FAIL;
5504 break;
5507 retval = image_read_section(&image, i, 0x0, image.sections[i].size, buffer, &buf_cnt);
5508 if (retval != ERROR_OK) {
5509 free(buffer);
5510 break;
5513 uint32_t offset = 0;
5514 uint32_t length = buf_cnt;
5516 /* DANGER!!! beware of unsigned comparision here!!! */
5518 if ((image.sections[i].base_address + buf_cnt >= min_address) &&
5519 (image.sections[i].base_address < max_address)) {
5520 if (image.sections[i].base_address < min_address) {
5521 /* clip addresses below */
5522 offset += min_address-image.sections[i].base_address;
5523 length -= offset;
5526 if (image.sections[i].base_address + buf_cnt > max_address)
5527 length -= (image.sections[i].base_address + buf_cnt)-max_address;
5529 fastload[i].address = image.sections[i].base_address + offset;
5530 fastload[i].data = malloc(length);
5531 if (fastload[i].data == NULL) {
5532 free(buffer);
5533 command_print(CMD_CTX, "error allocating buffer for section (%" PRIu32 " bytes)",
5534 length);
5535 retval = ERROR_FAIL;
5536 break;
5538 memcpy(fastload[i].data, buffer + offset, length);
5539 fastload[i].length = length;
5541 image_size += length;
5542 command_print(CMD_CTX, "%u bytes written at address 0x%8.8x",
5543 (unsigned int)length,
5544 ((unsigned int)(image.sections[i].base_address + offset)));
5547 free(buffer);
5550 if ((ERROR_OK == retval) && (duration_measure(&bench) == ERROR_OK)) {
5551 command_print(CMD_CTX, "Loaded %" PRIu32 " bytes "
5552 "in %fs (%0.3f KiB/s)", image_size,
5553 duration_elapsed(&bench), duration_kbps(&bench, image_size));
5555 command_print(CMD_CTX,
5556 "WARNING: image has not been loaded to target!"
5557 "You can issue a 'fast_load' to finish loading.");
5560 image_close(&image);
5562 if (retval != ERROR_OK)
5563 free_fastload();
5565 return retval;
5568 COMMAND_HANDLER(handle_fast_load_command)
5570 if (CMD_ARGC > 0)
5571 return ERROR_COMMAND_SYNTAX_ERROR;
5572 if (fastload == NULL) {
5573 LOG_ERROR("No image in memory");
5574 return ERROR_FAIL;
5576 int i;
5577 int ms = timeval_ms();
5578 int size = 0;
5579 int retval = ERROR_OK;
5580 for (i = 0; i < fastload_num; i++) {
5581 struct target *target = get_current_target(CMD_CTX);
5582 command_print(CMD_CTX, "Write to 0x%08x, length 0x%08x",
5583 (unsigned int)(fastload[i].address),
5584 (unsigned int)(fastload[i].length));
5585 retval = target_write_buffer(target, fastload[i].address, fastload[i].length, fastload[i].data);
5586 if (retval != ERROR_OK)
5587 break;
5588 size += fastload[i].length;
5590 if (retval == ERROR_OK) {
5591 int after = timeval_ms();
5592 command_print(CMD_CTX, "Loaded image %f kBytes/s", (float)(size/1024.0)/((float)(after-ms)/1000.0));
5594 return retval;
5597 static const struct command_registration target_command_handlers[] = {
5599 .name = "targets",
5600 .handler = handle_targets_command,
5601 .mode = COMMAND_ANY,
5602 .help = "change current default target (one parameter) "
5603 "or prints table of all targets (no parameters)",
5604 .usage = "[target]",
5607 .name = "target",
5608 .mode = COMMAND_CONFIG,
5609 .help = "configure target",
5611 .chain = target_subcommand_handlers,
5613 COMMAND_REGISTRATION_DONE
5616 int target_register_commands(struct command_context *cmd_ctx)
5618 return register_commands(cmd_ctx, NULL, target_command_handlers);
5621 static bool target_reset_nag = true;
5623 bool get_target_reset_nag(void)
5625 return target_reset_nag;
5628 COMMAND_HANDLER(handle_target_reset_nag)
5630 return CALL_COMMAND_HANDLER(handle_command_parse_bool,
5631 &target_reset_nag, "Nag after each reset about options to improve "
5632 "performance");
5635 COMMAND_HANDLER(handle_ps_command)
5637 struct target *target = get_current_target(CMD_CTX);
5638 char *display;
5639 if (target->state != TARGET_HALTED) {
5640 LOG_INFO("target not halted !!");
5641 return ERROR_OK;
5644 if ((target->rtos) && (target->rtos->type)
5645 && (target->rtos->type->ps_command)) {
5646 display = target->rtos->type->ps_command(target);
5647 command_print(CMD_CTX, "%s", display);
5648 free(display);
5649 return ERROR_OK;
5650 } else {
5651 LOG_INFO("failed");
5652 return ERROR_TARGET_FAILURE;
5656 static void binprint(struct command_context *cmd_ctx, const char *text, const uint8_t *buf, int size)
5658 if (text != NULL)
5659 command_print_sameline(cmd_ctx, "%s", text);
5660 for (int i = 0; i < size; i++)
5661 command_print_sameline(cmd_ctx, " %02x", buf[i]);
5662 command_print(cmd_ctx, " ");
5665 COMMAND_HANDLER(handle_test_mem_access_command)
5667 struct target *target = get_current_target(CMD_CTX);
5668 uint32_t test_size;
5669 int retval = ERROR_OK;
5671 if (target->state != TARGET_HALTED) {
5672 LOG_INFO("target not halted !!");
5673 return ERROR_FAIL;
5676 if (CMD_ARGC != 1)
5677 return ERROR_COMMAND_SYNTAX_ERROR;
5679 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], test_size);
5681 /* Test reads */
5682 size_t num_bytes = test_size + 4;
5684 struct working_area *wa = NULL;
5685 retval = target_alloc_working_area(target, num_bytes, &wa);
5686 if (retval != ERROR_OK) {
5687 LOG_ERROR("Not enough working area");
5688 return ERROR_FAIL;
5691 uint8_t *test_pattern = malloc(num_bytes);
5693 for (size_t i = 0; i < num_bytes; i++)
5694 test_pattern[i] = rand();
5696 retval = target_write_memory(target, wa->address, 1, num_bytes, test_pattern);
5697 if (retval != ERROR_OK) {
5698 LOG_ERROR("Test pattern write failed");
5699 goto out;
5702 for (int host_offset = 0; host_offset <= 1; host_offset++) {
5703 for (int size = 1; size <= 4; size *= 2) {
5704 for (int offset = 0; offset < 4; offset++) {
5705 uint32_t count = test_size / size;
5706 size_t host_bufsiz = (count + 2) * size + host_offset;
5707 uint8_t *read_ref = malloc(host_bufsiz);
5708 uint8_t *read_buf = malloc(host_bufsiz);
5710 for (size_t i = 0; i < host_bufsiz; i++) {
5711 read_ref[i] = rand();
5712 read_buf[i] = read_ref[i];
5714 command_print_sameline(CMD_CTX,
5715 "Test read %" PRIu32 " x %d @ %d to %saligned buffer: ", count,
5716 size, offset, host_offset ? "un" : "");
5718 struct duration bench;
5719 duration_start(&bench);
5721 retval = target_read_memory(target, wa->address + offset, size, count,
5722 read_buf + size + host_offset);
5724 duration_measure(&bench);
5726 if (retval == ERROR_TARGET_UNALIGNED_ACCESS) {
5727 command_print(CMD_CTX, "Unsupported alignment");
5728 goto next;
5729 } else if (retval != ERROR_OK) {
5730 command_print(CMD_CTX, "Memory read failed");
5731 goto next;
5734 /* replay on host */
5735 memcpy(read_ref + size + host_offset, test_pattern + offset, count * size);
5737 /* check result */
5738 int result = memcmp(read_ref, read_buf, host_bufsiz);
5739 if (result == 0) {
5740 command_print(CMD_CTX, "Pass in %fs (%0.3f KiB/s)",
5741 duration_elapsed(&bench),
5742 duration_kbps(&bench, count * size));
5743 } else {
5744 command_print(CMD_CTX, "Compare failed");
5745 binprint(CMD_CTX, "ref:", read_ref, host_bufsiz);
5746 binprint(CMD_CTX, "buf:", read_buf, host_bufsiz);
5748 next:
5749 free(read_ref);
5750 free(read_buf);
5755 out:
5756 free(test_pattern);
5758 if (wa != NULL)
5759 target_free_working_area(target, wa);
5761 /* Test writes */
5762 num_bytes = test_size + 4 + 4 + 4;
5764 retval = target_alloc_working_area(target, num_bytes, &wa);
5765 if (retval != ERROR_OK) {
5766 LOG_ERROR("Not enough working area");
5767 return ERROR_FAIL;
5770 test_pattern = malloc(num_bytes);
5772 for (size_t i = 0; i < num_bytes; i++)
5773 test_pattern[i] = rand();
5775 for (int host_offset = 0; host_offset <= 1; host_offset++) {
5776 for (int size = 1; size <= 4; size *= 2) {
5777 for (int offset = 0; offset < 4; offset++) {
5778 uint32_t count = test_size / size;
5779 size_t host_bufsiz = count * size + host_offset;
5780 uint8_t *read_ref = malloc(num_bytes);
5781 uint8_t *read_buf = malloc(num_bytes);
5782 uint8_t *write_buf = malloc(host_bufsiz);
5784 for (size_t i = 0; i < host_bufsiz; i++)
5785 write_buf[i] = rand();
5786 command_print_sameline(CMD_CTX,
5787 "Test write %" PRIu32 " x %d @ %d from %saligned buffer: ", count,
5788 size, offset, host_offset ? "un" : "");
5790 retval = target_write_memory(target, wa->address, 1, num_bytes, test_pattern);
5791 if (retval != ERROR_OK) {
5792 command_print(CMD_CTX, "Test pattern write failed");
5793 goto nextw;
5796 /* replay on host */
5797 memcpy(read_ref, test_pattern, num_bytes);
5798 memcpy(read_ref + size + offset, write_buf + host_offset, count * size);
5800 struct duration bench;
5801 duration_start(&bench);
5803 retval = target_write_memory(target, wa->address + size + offset, size, count,
5804 write_buf + host_offset);
5806 duration_measure(&bench);
5808 if (retval == ERROR_TARGET_UNALIGNED_ACCESS) {
5809 command_print(CMD_CTX, "Unsupported alignment");
5810 goto nextw;
5811 } else if (retval != ERROR_OK) {
5812 command_print(CMD_CTX, "Memory write failed");
5813 goto nextw;
5816 /* read back */
5817 retval = target_read_memory(target, wa->address, 1, num_bytes, read_buf);
5818 if (retval != ERROR_OK) {
5819 command_print(CMD_CTX, "Test pattern write failed");
5820 goto nextw;
5823 /* check result */
5824 int result = memcmp(read_ref, read_buf, num_bytes);
5825 if (result == 0) {
5826 command_print(CMD_CTX, "Pass in %fs (%0.3f KiB/s)",
5827 duration_elapsed(&bench),
5828 duration_kbps(&bench, count * size));
5829 } else {
5830 command_print(CMD_CTX, "Compare failed");
5831 binprint(CMD_CTX, "ref:", read_ref, num_bytes);
5832 binprint(CMD_CTX, "buf:", read_buf, num_bytes);
5834 nextw:
5835 free(read_ref);
5836 free(read_buf);
5841 free(test_pattern);
5843 if (wa != NULL)
5844 target_free_working_area(target, wa);
5845 return retval;
5848 static const struct command_registration target_exec_command_handlers[] = {
5850 .name = "fast_load_image",
5851 .handler = handle_fast_load_image_command,
5852 .mode = COMMAND_ANY,
5853 .help = "Load image into server memory for later use by "
5854 "fast_load; primarily for profiling",
5855 .usage = "filename address ['bin'|'ihex'|'elf'|'s19'] "
5856 "[min_address [max_length]]",
5859 .name = "fast_load",
5860 .handler = handle_fast_load_command,
5861 .mode = COMMAND_EXEC,
5862 .help = "loads active fast load image to current target "
5863 "- mainly for profiling purposes",
5864 .usage = "",
5867 .name = "profile",
5868 .handler = handle_profile_command,
5869 .mode = COMMAND_EXEC,
5870 .usage = "seconds filename [start end]",
5871 .help = "profiling samples the CPU PC",
5873 /** @todo don't register virt2phys() unless target supports it */
5875 .name = "virt2phys",
5876 .handler = handle_virt2phys_command,
5877 .mode = COMMAND_ANY,
5878 .help = "translate a virtual address into a physical address",
5879 .usage = "virtual_address",
5882 .name = "reg",
5883 .handler = handle_reg_command,
5884 .mode = COMMAND_EXEC,
5885 .help = "display (reread from target with \"force\") or set a register; "
5886 "with no arguments, displays all registers and their values",
5887 .usage = "[(register_number|register_name) [(value|'force')]]",
5890 .name = "poll",
5891 .handler = handle_poll_command,
5892 .mode = COMMAND_EXEC,
5893 .help = "poll target state; or reconfigure background polling",
5894 .usage = "['on'|'off']",
5897 .name = "wait_halt",
5898 .handler = handle_wait_halt_command,
5899 .mode = COMMAND_EXEC,
5900 .help = "wait up to the specified number of milliseconds "
5901 "(default 5000) for a previously requested halt",
5902 .usage = "[milliseconds]",
5905 .name = "halt",
5906 .handler = handle_halt_command,
5907 .mode = COMMAND_EXEC,
5908 .help = "request target to halt, then wait up to the specified"
5909 "number of milliseconds (default 5000) for it to complete",
5910 .usage = "[milliseconds]",
5913 .name = "resume",
5914 .handler = handle_resume_command,
5915 .mode = COMMAND_EXEC,
5916 .help = "resume target execution from current PC or address",
5917 .usage = "[address]",
5920 .name = "reset",
5921 .handler = handle_reset_command,
5922 .mode = COMMAND_EXEC,
5923 .usage = "[run|halt|init]",
5924 .help = "Reset all targets into the specified mode."
5925 "Default reset mode is run, if not given.",
5928 .name = "soft_reset_halt",
5929 .handler = handle_soft_reset_halt_command,
5930 .mode = COMMAND_EXEC,
5931 .usage = "",
5932 .help = "halt the target and do a soft reset",
5935 .name = "step",
5936 .handler = handle_step_command,
5937 .mode = COMMAND_EXEC,
5938 .help = "step one instruction from current PC or address",
5939 .usage = "[address]",
5942 .name = "mdw",
5943 .handler = handle_md_command,
5944 .mode = COMMAND_EXEC,
5945 .help = "display memory words",
5946 .usage = "['phys'] address [count]",
5949 .name = "mdh",
5950 .handler = handle_md_command,
5951 .mode = COMMAND_EXEC,
5952 .help = "display memory half-words",
5953 .usage = "['phys'] address [count]",
5956 .name = "mdb",
5957 .handler = handle_md_command,
5958 .mode = COMMAND_EXEC,
5959 .help = "display memory bytes",
5960 .usage = "['phys'] address [count]",
5963 .name = "mww",
5964 .handler = handle_mw_command,
5965 .mode = COMMAND_EXEC,
5966 .help = "write memory word",
5967 .usage = "['phys'] address value [count]",
5970 .name = "mwh",
5971 .handler = handle_mw_command,
5972 .mode = COMMAND_EXEC,
5973 .help = "write memory half-word",
5974 .usage = "['phys'] address value [count]",
5977 .name = "mwb",
5978 .handler = handle_mw_command,
5979 .mode = COMMAND_EXEC,
5980 .help = "write memory byte",
5981 .usage = "['phys'] address value [count]",
5984 .name = "bp",
5985 .handler = handle_bp_command,
5986 .mode = COMMAND_EXEC,
5987 .help = "list or set hardware or software breakpoint",
5988 .usage = "<address> [<asid>]<length> ['hw'|'hw_ctx']",
5991 .name = "rbp",
5992 .handler = handle_rbp_command,
5993 .mode = COMMAND_EXEC,
5994 .help = "remove breakpoint",
5995 .usage = "address",
5998 .name = "wp",
5999 .handler = handle_wp_command,
6000 .mode = COMMAND_EXEC,
6001 .help = "list (no params) or create watchpoints",
6002 .usage = "[address length [('r'|'w'|'a') value [mask]]]",
6005 .name = "rwp",
6006 .handler = handle_rwp_command,
6007 .mode = COMMAND_EXEC,
6008 .help = "remove watchpoint",
6009 .usage = "address",
6012 .name = "load_image",
6013 .handler = handle_load_image_command,
6014 .mode = COMMAND_EXEC,
6015 .usage = "filename address ['bin'|'ihex'|'elf'|'s19'] "
6016 "[min_address] [max_length]",
6019 .name = "dump_image",
6020 .handler = handle_dump_image_command,
6021 .mode = COMMAND_EXEC,
6022 .usage = "filename address size",
6025 .name = "verify_image",
6026 .handler = handle_verify_image_command,
6027 .mode = COMMAND_EXEC,
6028 .usage = "filename [offset [type]]",
6031 .name = "test_image",
6032 .handler = handle_test_image_command,
6033 .mode = COMMAND_EXEC,
6034 .usage = "filename [offset [type]]",
6037 .name = "mem2array",
6038 .mode = COMMAND_EXEC,
6039 .jim_handler = jim_mem2array,
6040 .help = "read 8/16/32 bit memory and return as a TCL array "
6041 "for script processing",
6042 .usage = "arrayname bitwidth address count",
6045 .name = "array2mem",
6046 .mode = COMMAND_EXEC,
6047 .jim_handler = jim_array2mem,
6048 .help = "convert a TCL array to memory locations "
6049 "and write the 8/16/32 bit values",
6050 .usage = "arrayname bitwidth address count",
6053 .name = "reset_nag",
6054 .handler = handle_target_reset_nag,
6055 .mode = COMMAND_ANY,
6056 .help = "Nag after each reset about options that could have been "
6057 "enabled to improve performance. ",
6058 .usage = "['enable'|'disable']",
6061 .name = "ps",
6062 .handler = handle_ps_command,
6063 .mode = COMMAND_EXEC,
6064 .help = "list all tasks ",
6065 .usage = " ",
6068 .name = "test_mem_access",
6069 .handler = handle_test_mem_access_command,
6070 .mode = COMMAND_EXEC,
6071 .help = "Test the target's memory access functions",
6072 .usage = "size",
6075 COMMAND_REGISTRATION_DONE
6077 static int target_register_user_commands(struct command_context *cmd_ctx)
6079 int retval = ERROR_OK;
6080 retval = target_request_register_commands(cmd_ctx);
6081 if (retval != ERROR_OK)
6082 return retval;
6084 retval = trace_register_commands(cmd_ctx);
6085 if (retval != ERROR_OK)
6086 return retval;
6089 return register_commands(cmd_ctx, NULL, target_exec_command_handlers);