cfg: Beaglebone/AM335x refactor
[openocd/andreasf.git] / src / target / target.c
blob51518e683390d0bcaf86bd0846a9c349c2331748
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 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, 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 static int target_read_buffer_default(struct target *target, uint32_t address,
60 uint32_t size, uint8_t *buffer);
61 static int target_write_buffer_default(struct target *target, uint32_t address,
62 uint32_t size, const uint8_t *buffer);
63 static int target_array2mem(Jim_Interp *interp, struct target *target,
64 int argc, Jim_Obj * const *argv);
65 static int target_mem2array(Jim_Interp *interp, struct target *target,
66 int argc, Jim_Obj * const *argv);
67 static int target_register_user_commands(struct command_context *cmd_ctx);
69 /* targets */
70 extern struct target_type arm7tdmi_target;
71 extern struct target_type arm720t_target;
72 extern struct target_type arm9tdmi_target;
73 extern struct target_type arm920t_target;
74 extern struct target_type arm966e_target;
75 extern struct target_type arm946e_target;
76 extern struct target_type arm926ejs_target;
77 extern struct target_type fa526_target;
78 extern struct target_type feroceon_target;
79 extern struct target_type dragonite_target;
80 extern struct target_type xscale_target;
81 extern struct target_type cortexm3_target;
82 extern struct target_type cortexa8_target;
83 extern struct target_type arm11_target;
84 extern struct target_type mips_m4k_target;
85 extern struct target_type avr_target;
86 extern struct target_type dsp563xx_target;
87 extern struct target_type dsp5680xx_target;
88 extern struct target_type testee_target;
89 extern struct target_type avr32_ap7k_target;
90 extern struct target_type stm32_stlink_target;
92 static struct target_type *target_types[] = {
93 &arm7tdmi_target,
94 &arm9tdmi_target,
95 &arm920t_target,
96 &arm720t_target,
97 &arm966e_target,
98 &arm946e_target,
99 &arm926ejs_target,
100 &fa526_target,
101 &feroceon_target,
102 &dragonite_target,
103 &xscale_target,
104 &cortexm3_target,
105 &cortexa8_target,
106 &arm11_target,
107 &mips_m4k_target,
108 &avr_target,
109 &dsp563xx_target,
110 &dsp5680xx_target,
111 &testee_target,
112 &avr32_ap7k_target,
113 &stm32_stlink_target,
114 NULL,
117 struct target *all_targets;
118 static struct target_event_callback *target_event_callbacks;
119 static struct target_timer_callback *target_timer_callbacks;
120 static const int polling_interval = 100;
122 static const Jim_Nvp nvp_assert[] = {
123 { .name = "assert", NVP_ASSERT },
124 { .name = "deassert", NVP_DEASSERT },
125 { .name = "T", NVP_ASSERT },
126 { .name = "F", NVP_DEASSERT },
127 { .name = "t", NVP_ASSERT },
128 { .name = "f", NVP_DEASSERT },
129 { .name = NULL, .value = -1 }
132 static const Jim_Nvp nvp_error_target[] = {
133 { .value = ERROR_TARGET_INVALID, .name = "err-invalid" },
134 { .value = ERROR_TARGET_INIT_FAILED, .name = "err-init-failed" },
135 { .value = ERROR_TARGET_TIMEOUT, .name = "err-timeout" },
136 { .value = ERROR_TARGET_NOT_HALTED, .name = "err-not-halted" },
137 { .value = ERROR_TARGET_FAILURE, .name = "err-failure" },
138 { .value = ERROR_TARGET_UNALIGNED_ACCESS , .name = "err-unaligned-access" },
139 { .value = ERROR_TARGET_DATA_ABORT , .name = "err-data-abort" },
140 { .value = ERROR_TARGET_RESOURCE_NOT_AVAILABLE , .name = "err-resource-not-available" },
141 { .value = ERROR_TARGET_TRANSLATION_FAULT , .name = "err-translation-fault" },
142 { .value = ERROR_TARGET_NOT_RUNNING, .name = "err-not-running" },
143 { .value = ERROR_TARGET_NOT_EXAMINED, .name = "err-not-examined" },
144 { .value = -1, .name = NULL }
147 static const char *target_strerror_safe(int err)
149 const Jim_Nvp *n;
151 n = Jim_Nvp_value2name_simple(nvp_error_target, err);
152 if (n->name == NULL)
153 return "unknown";
154 else
155 return n->name;
158 static const Jim_Nvp nvp_target_event[] = {
159 { .value = TARGET_EVENT_OLD_gdb_program_config , .name = "old-gdb_program_config" },
160 { .value = TARGET_EVENT_OLD_pre_resume , .name = "old-pre_resume" },
162 { .value = TARGET_EVENT_GDB_HALT, .name = "gdb-halt" },
163 { .value = TARGET_EVENT_HALTED, .name = "halted" },
164 { .value = TARGET_EVENT_RESUMED, .name = "resumed" },
165 { .value = TARGET_EVENT_RESUME_START, .name = "resume-start" },
166 { .value = TARGET_EVENT_RESUME_END, .name = "resume-end" },
168 { .name = "gdb-start", .value = TARGET_EVENT_GDB_START },
169 { .name = "gdb-end", .value = TARGET_EVENT_GDB_END },
171 /* historical name */
173 { .value = TARGET_EVENT_RESET_START, .name = "reset-start" },
175 { .value = TARGET_EVENT_RESET_ASSERT_PRE, .name = "reset-assert-pre" },
176 { .value = TARGET_EVENT_RESET_ASSERT, .name = "reset-assert" },
177 { .value = TARGET_EVENT_RESET_ASSERT_POST, .name = "reset-assert-post" },
178 { .value = TARGET_EVENT_RESET_DEASSERT_PRE, .name = "reset-deassert-pre" },
179 { .value = TARGET_EVENT_RESET_DEASSERT_POST, .name = "reset-deassert-post" },
180 { .value = TARGET_EVENT_RESET_HALT_PRE, .name = "reset-halt-pre" },
181 { .value = TARGET_EVENT_RESET_HALT_POST, .name = "reset-halt-post" },
182 { .value = TARGET_EVENT_RESET_WAIT_PRE, .name = "reset-wait-pre" },
183 { .value = TARGET_EVENT_RESET_WAIT_POST, .name = "reset-wait-post" },
184 { .value = TARGET_EVENT_RESET_INIT, .name = "reset-init" },
185 { .value = TARGET_EVENT_RESET_END, .name = "reset-end" },
187 { .value = TARGET_EVENT_EXAMINE_START, .name = "examine-start" },
188 { .value = TARGET_EVENT_EXAMINE_END, .name = "examine-end" },
190 { .value = TARGET_EVENT_DEBUG_HALTED, .name = "debug-halted" },
191 { .value = TARGET_EVENT_DEBUG_RESUMED, .name = "debug-resumed" },
193 { .value = TARGET_EVENT_GDB_ATTACH, .name = "gdb-attach" },
194 { .value = TARGET_EVENT_GDB_DETACH, .name = "gdb-detach" },
196 { .value = TARGET_EVENT_GDB_FLASH_WRITE_START, .name = "gdb-flash-write-start" },
197 { .value = TARGET_EVENT_GDB_FLASH_WRITE_END , .name = "gdb-flash-write-end" },
199 { .value = TARGET_EVENT_GDB_FLASH_ERASE_START, .name = "gdb-flash-erase-start" },
200 { .value = TARGET_EVENT_GDB_FLASH_ERASE_END , .name = "gdb-flash-erase-end" },
202 { .value = TARGET_EVENT_RESUME_START, .name = "resume-start" },
203 { .value = TARGET_EVENT_RESUMED , .name = "resume-ok" },
204 { .value = TARGET_EVENT_RESUME_END , .name = "resume-end" },
206 { .name = NULL, .value = -1 }
209 static const Jim_Nvp nvp_target_state[] = {
210 { .name = "unknown", .value = TARGET_UNKNOWN },
211 { .name = "running", .value = TARGET_RUNNING },
212 { .name = "halted", .value = TARGET_HALTED },
213 { .name = "reset", .value = TARGET_RESET },
214 { .name = "debug-running", .value = TARGET_DEBUG_RUNNING },
215 { .name = NULL, .value = -1 },
218 static const Jim_Nvp nvp_target_debug_reason[] = {
219 { .name = "debug-request" , .value = DBG_REASON_DBGRQ },
220 { .name = "breakpoint" , .value = DBG_REASON_BREAKPOINT },
221 { .name = "watchpoint" , .value = DBG_REASON_WATCHPOINT },
222 { .name = "watchpoint-and-breakpoint", .value = DBG_REASON_WPTANDBKPT },
223 { .name = "single-step" , .value = DBG_REASON_SINGLESTEP },
224 { .name = "target-not-halted" , .value = DBG_REASON_NOTHALTED },
225 { .name = "undefined" , .value = DBG_REASON_UNDEFINED },
226 { .name = NULL, .value = -1 },
229 static const Jim_Nvp nvp_target_endian[] = {
230 { .name = "big", .value = TARGET_BIG_ENDIAN },
231 { .name = "little", .value = TARGET_LITTLE_ENDIAN },
232 { .name = "be", .value = TARGET_BIG_ENDIAN },
233 { .name = "le", .value = TARGET_LITTLE_ENDIAN },
234 { .name = NULL, .value = -1 },
237 static const Jim_Nvp nvp_reset_modes[] = {
238 { .name = "unknown", .value = RESET_UNKNOWN },
239 { .name = "run" , .value = RESET_RUN },
240 { .name = "halt" , .value = RESET_HALT },
241 { .name = "init" , .value = RESET_INIT },
242 { .name = NULL , .value = -1 },
245 const char *debug_reason_name(struct target *t)
247 const char *cp;
249 cp = Jim_Nvp_value2name_simple(nvp_target_debug_reason,
250 t->debug_reason)->name;
251 if (!cp) {
252 LOG_ERROR("Invalid debug reason: %d", (int)(t->debug_reason));
253 cp = "(*BUG*unknown*BUG*)";
255 return cp;
258 const char *target_state_name(struct target *t)
260 const char *cp;
261 cp = Jim_Nvp_value2name_simple(nvp_target_state, t->state)->name;
262 if (!cp) {
263 LOG_ERROR("Invalid target state: %d", (int)(t->state));
264 cp = "(*BUG*unknown*BUG*)";
266 return cp;
269 /* determine the number of the new target */
270 static int new_target_number(void)
272 struct target *t;
273 int x;
275 /* number is 0 based */
276 x = -1;
277 t = all_targets;
278 while (t) {
279 if (x < t->target_number)
280 x = t->target_number;
281 t = t->next;
283 return x + 1;
286 /* read a uint32_t from a buffer in target memory endianness */
287 uint32_t target_buffer_get_u32(struct target *target, const uint8_t *buffer)
289 if (target->endianness == TARGET_LITTLE_ENDIAN)
290 return le_to_h_u32(buffer);
291 else
292 return be_to_h_u32(buffer);
295 /* read a uint24_t from a buffer in target memory endianness */
296 uint32_t target_buffer_get_u24(struct target *target, const uint8_t *buffer)
298 if (target->endianness == TARGET_LITTLE_ENDIAN)
299 return le_to_h_u24(buffer);
300 else
301 return be_to_h_u24(buffer);
304 /* read a uint16_t from a buffer in target memory endianness */
305 uint16_t target_buffer_get_u16(struct target *target, const uint8_t *buffer)
307 if (target->endianness == TARGET_LITTLE_ENDIAN)
308 return le_to_h_u16(buffer);
309 else
310 return be_to_h_u16(buffer);
313 /* read a uint8_t from a buffer in target memory endianness */
314 static uint8_t target_buffer_get_u8(struct target *target, const uint8_t *buffer)
316 return *buffer & 0x0ff;
319 /* write a uint32_t to a buffer in target memory endianness */
320 void target_buffer_set_u32(struct target *target, uint8_t *buffer, uint32_t value)
322 if (target->endianness == TARGET_LITTLE_ENDIAN)
323 h_u32_to_le(buffer, value);
324 else
325 h_u32_to_be(buffer, value);
328 /* write a uint24_t to a buffer in target memory endianness */
329 void target_buffer_set_u24(struct target *target, uint8_t *buffer, uint32_t value)
331 if (target->endianness == TARGET_LITTLE_ENDIAN)
332 h_u24_to_le(buffer, value);
333 else
334 h_u24_to_be(buffer, value);
337 /* write a uint16_t to a buffer in target memory endianness */
338 void target_buffer_set_u16(struct target *target, uint8_t *buffer, uint16_t value)
340 if (target->endianness == TARGET_LITTLE_ENDIAN)
341 h_u16_to_le(buffer, value);
342 else
343 h_u16_to_be(buffer, value);
346 /* write a uint8_t to a buffer in target memory endianness */
347 static void target_buffer_set_u8(struct target *target, uint8_t *buffer, uint8_t value)
349 *buffer = value;
352 /* write a uint32_t array to a buffer in target memory endianness */
353 void target_buffer_get_u32_array(struct target *target, const uint8_t *buffer, uint32_t count, uint32_t *dstbuf)
355 uint32_t i;
356 for (i = 0; i < count; i++)
357 dstbuf[i] = target_buffer_get_u32(target, &buffer[i * 4]);
360 /* write a uint16_t array to a buffer in target memory endianness */
361 void target_buffer_get_u16_array(struct target *target, const uint8_t *buffer, uint32_t count, uint16_t *dstbuf)
363 uint32_t i;
364 for (i = 0; i < count; i++)
365 dstbuf[i] = target_buffer_get_u16(target, &buffer[i * 2]);
368 /* write a uint32_t array to a buffer in target memory endianness */
369 void target_buffer_set_u32_array(struct target *target, uint8_t *buffer, uint32_t count, uint32_t *srcbuf)
371 uint32_t i;
372 for (i = 0; i < count; i++)
373 target_buffer_set_u32(target, &buffer[i * 4], srcbuf[i]);
376 /* write a uint16_t array to a buffer in target memory endianness */
377 void target_buffer_set_u16_array(struct target *target, uint8_t *buffer, uint32_t count, uint16_t *srcbuf)
379 uint32_t i;
380 for (i = 0; i < count; i++)
381 target_buffer_set_u16(target, &buffer[i * 2], srcbuf[i]);
384 /* return a pointer to a configured target; id is name or number */
385 struct target *get_target(const char *id)
387 struct target *target;
389 /* try as tcltarget name */
390 for (target = all_targets; target; target = target->next) {
391 if (target->cmd_name == NULL)
392 continue;
393 if (strcmp(id, target->cmd_name) == 0)
394 return target;
397 /* It's OK to remove this fallback sometime after August 2010 or so */
399 /* no match, try as number */
400 unsigned num;
401 if (parse_uint(id, &num) != ERROR_OK)
402 return NULL;
404 for (target = all_targets; target; target = target->next) {
405 if (target->target_number == (int)num) {
406 LOG_WARNING("use '%s' as target identifier, not '%u'",
407 target->cmd_name, num);
408 return target;
412 return NULL;
415 /* returns a pointer to the n-th configured target */
416 static struct target *get_target_by_num(int num)
418 struct target *target = all_targets;
420 while (target) {
421 if (target->target_number == num)
422 return target;
423 target = target->next;
426 return NULL;
429 struct target *get_current_target(struct command_context *cmd_ctx)
431 struct target *target = get_target_by_num(cmd_ctx->current_target);
433 if (target == NULL) {
434 LOG_ERROR("BUG: current_target out of bounds");
435 exit(-1);
438 return target;
441 int target_poll(struct target *target)
443 int retval;
445 /* We can't poll until after examine */
446 if (!target_was_examined(target)) {
447 /* Fail silently lest we pollute the log */
448 return ERROR_FAIL;
451 retval = target->type->poll(target);
452 if (retval != ERROR_OK)
453 return retval;
455 if (target->halt_issued) {
456 if (target->state == TARGET_HALTED)
457 target->halt_issued = false;
458 else {
459 long long t = timeval_ms() - target->halt_issued_time;
460 if (t > 1000) {
461 target->halt_issued = false;
462 LOG_INFO("Halt timed out, wake up GDB.");
463 target_call_event_callbacks(target, TARGET_EVENT_GDB_HALT);
468 return ERROR_OK;
471 int target_halt(struct target *target)
473 int retval;
474 /* We can't poll until after examine */
475 if (!target_was_examined(target)) {
476 LOG_ERROR("Target not examined yet");
477 return ERROR_FAIL;
480 retval = target->type->halt(target);
481 if (retval != ERROR_OK)
482 return retval;
484 target->halt_issued = true;
485 target->halt_issued_time = timeval_ms();
487 return ERROR_OK;
491 * Make the target (re)start executing using its saved execution
492 * context (possibly with some modifications).
494 * @param target Which target should start executing.
495 * @param current True to use the target's saved program counter instead
496 * of the address parameter
497 * @param address Optionally used as the program counter.
498 * @param handle_breakpoints True iff breakpoints at the resumption PC
499 * should be skipped. (For example, maybe execution was stopped by
500 * such a breakpoint, in which case it would be counterprodutive to
501 * let it re-trigger.
502 * @param debug_execution False if all working areas allocated by OpenOCD
503 * should be released and/or restored to their original contents.
504 * (This would for example be true to run some downloaded "helper"
505 * algorithm code, which resides in one such working buffer and uses
506 * another for data storage.)
508 * @todo Resolve the ambiguity about what the "debug_execution" flag
509 * signifies. For example, Target implementations don't agree on how
510 * it relates to invalidation of the register cache, or to whether
511 * breakpoints and watchpoints should be enabled. (It would seem wrong
512 * to enable breakpoints when running downloaded "helper" algorithms
513 * (debug_execution true), since the breakpoints would be set to match
514 * target firmware being debugged, not the helper algorithm.... and
515 * enabling them could cause such helpers to malfunction (for example,
516 * by overwriting data with a breakpoint instruction. On the other
517 * hand the infrastructure for running such helpers might use this
518 * procedure but rely on hardware breakpoint to detect termination.)
520 int target_resume(struct target *target, int current, uint32_t address, int handle_breakpoints, int debug_execution)
522 int retval;
524 /* We can't poll until after examine */
525 if (!target_was_examined(target)) {
526 LOG_ERROR("Target not examined yet");
527 return ERROR_FAIL;
530 /* note that resume *must* be asynchronous. The CPU can halt before
531 * we poll. The CPU can even halt at the current PC as a result of
532 * a software breakpoint being inserted by (a bug?) the application.
534 retval = target->type->resume(target, current, address, handle_breakpoints, debug_execution);
535 if (retval != ERROR_OK)
536 return retval;
538 return retval;
541 static int target_process_reset(struct command_context *cmd_ctx, enum target_reset_mode reset_mode)
543 char buf[100];
544 int retval;
545 Jim_Nvp *n;
546 n = Jim_Nvp_value2name_simple(nvp_reset_modes, reset_mode);
547 if (n->name == NULL) {
548 LOG_ERROR("invalid reset mode");
549 return ERROR_FAIL;
552 /* disable polling during reset to make reset event scripts
553 * more predictable, i.e. dr/irscan & pathmove in events will
554 * not have JTAG operations injected into the middle of a sequence.
556 bool save_poll = jtag_poll_get_enabled();
558 jtag_poll_set_enabled(false);
560 sprintf(buf, "ocd_process_reset %s", n->name);
561 retval = Jim_Eval(cmd_ctx->interp, buf);
563 jtag_poll_set_enabled(save_poll);
565 if (retval != JIM_OK) {
566 Jim_MakeErrorMessage(cmd_ctx->interp);
567 command_print(NULL, "%s\n", Jim_GetString(Jim_GetResult(cmd_ctx->interp), NULL));
568 return ERROR_FAIL;
571 /* We want any events to be processed before the prompt */
572 retval = target_call_timer_callbacks_now();
574 struct target *target;
575 for (target = all_targets; target; target = target->next)
576 target->type->check_reset(target);
578 return retval;
581 static int identity_virt2phys(struct target *target,
582 uint32_t virtual, uint32_t *physical)
584 *physical = virtual;
585 return ERROR_OK;
588 static int no_mmu(struct target *target, int *enabled)
590 *enabled = 0;
591 return ERROR_OK;
594 static int default_examine(struct target *target)
596 target_set_examined(target);
597 return ERROR_OK;
600 /* no check by default */
601 static int default_check_reset(struct target *target)
603 return ERROR_OK;
606 int target_examine_one(struct target *target)
608 return target->type->examine(target);
611 static int jtag_enable_callback(enum jtag_event event, void *priv)
613 struct target *target = priv;
615 if (event != JTAG_TAP_EVENT_ENABLE || !target->tap->enabled)
616 return ERROR_OK;
618 jtag_unregister_event_callback(jtag_enable_callback, target);
619 return target_examine_one(target);
623 /* Targets that correctly implement init + examine, i.e.
624 * no communication with target during init:
626 * XScale
628 int target_examine(void)
630 int retval = ERROR_OK;
631 struct target *target;
633 for (target = all_targets; target; target = target->next) {
634 /* defer examination, but don't skip it */
635 if (!target->tap->enabled) {
636 jtag_register_event_callback(jtag_enable_callback,
637 target);
638 continue;
640 retval = target_examine_one(target);
641 if (retval != ERROR_OK)
642 return retval;
644 return retval;
646 const char *target_type_name(struct target *target)
648 return target->type->name;
651 static int target_write_memory_imp(struct target *target, uint32_t address,
652 uint32_t size, uint32_t count, const uint8_t *buffer)
654 if (!target_was_examined(target)) {
655 LOG_ERROR("Target not examined yet");
656 return ERROR_FAIL;
658 return target->type->write_memory_imp(target, address, size, count, buffer);
661 static int target_read_memory_imp(struct target *target, uint32_t address,
662 uint32_t size, uint32_t count, uint8_t *buffer)
664 if (!target_was_examined(target)) {
665 LOG_ERROR("Target not examined yet");
666 return ERROR_FAIL;
668 return target->type->read_memory_imp(target, address, size, count, buffer);
671 static int target_soft_reset_halt_imp(struct target *target)
673 if (!target_was_examined(target)) {
674 LOG_ERROR("Target not examined yet");
675 return ERROR_FAIL;
677 if (!target->type->soft_reset_halt_imp) {
678 LOG_ERROR("Target %s does not support soft_reset_halt",
679 target_name(target));
680 return ERROR_FAIL;
682 return target->type->soft_reset_halt_imp(target);
686 * Downloads a target-specific native code algorithm to the target,
687 * and executes it. * Note that some targets may need to set up, enable,
688 * and tear down a breakpoint (hard or * soft) to detect algorithm
689 * termination, while others may support lower overhead schemes where
690 * soft breakpoints embedded in the algorithm automatically terminate the
691 * algorithm.
693 * @param target used to run the algorithm
694 * @param arch_info target-specific description of the algorithm.
696 int target_run_algorithm(struct target *target,
697 int num_mem_params, struct mem_param *mem_params,
698 int num_reg_params, struct reg_param *reg_param,
699 uint32_t entry_point, uint32_t exit_point,
700 int timeout_ms, void *arch_info)
702 int retval = ERROR_FAIL;
704 if (!target_was_examined(target)) {
705 LOG_ERROR("Target not examined yet");
706 goto done;
708 if (!target->type->run_algorithm) {
709 LOG_ERROR("Target type '%s' does not support %s",
710 target_type_name(target), __func__);
711 goto done;
714 target->running_alg = true;
715 retval = target->type->run_algorithm(target,
716 num_mem_params, mem_params,
717 num_reg_params, reg_param,
718 entry_point, exit_point, timeout_ms, arch_info);
719 target->running_alg = false;
721 done:
722 return retval;
726 * Downloads a target-specific native code algorithm to the target,
727 * executes and leaves it running.
729 * @param target used to run the algorithm
730 * @param arch_info target-specific description of the algorithm.
732 int target_start_algorithm(struct target *target,
733 int num_mem_params, struct mem_param *mem_params,
734 int num_reg_params, struct reg_param *reg_params,
735 uint32_t entry_point, uint32_t exit_point,
736 void *arch_info)
738 int retval = ERROR_FAIL;
740 if (!target_was_examined(target)) {
741 LOG_ERROR("Target not examined yet");
742 goto done;
744 if (!target->type->start_algorithm) {
745 LOG_ERROR("Target type '%s' does not support %s",
746 target_type_name(target), __func__);
747 goto done;
749 if (target->running_alg) {
750 LOG_ERROR("Target is already running an algorithm");
751 goto done;
754 target->running_alg = true;
755 retval = target->type->start_algorithm(target,
756 num_mem_params, mem_params,
757 num_reg_params, reg_params,
758 entry_point, exit_point, arch_info);
760 done:
761 return retval;
765 * Waits for an algorithm started with target_start_algorithm() to complete.
767 * @param target used to run the algorithm
768 * @param arch_info target-specific description of the algorithm.
770 int target_wait_algorithm(struct target *target,
771 int num_mem_params, struct mem_param *mem_params,
772 int num_reg_params, struct reg_param *reg_params,
773 uint32_t exit_point, int timeout_ms,
774 void *arch_info)
776 int retval = ERROR_FAIL;
778 if (!target->type->wait_algorithm) {
779 LOG_ERROR("Target type '%s' does not support %s",
780 target_type_name(target), __func__);
781 goto done;
783 if (!target->running_alg) {
784 LOG_ERROR("Target is not running an algorithm");
785 goto done;
788 retval = target->type->wait_algorithm(target,
789 num_mem_params, mem_params,
790 num_reg_params, reg_params,
791 exit_point, timeout_ms, arch_info);
792 if (retval != ERROR_TARGET_TIMEOUT)
793 target->running_alg = false;
795 done:
796 return retval;
800 * Executes a target-specific native code algorithm in the target.
801 * It differs from target_run_algorithm in that the algorithm is asynchronous.
802 * Because of this it requires an compliant algorithm:
803 * see contrib/loaders/flash/stm32f1x.S for example.
805 * @param target used to run the algorithm
808 int target_run_flash_async_algorithm(struct target *target,
809 uint8_t *buffer, uint32_t count, int block_size,
810 int num_mem_params, struct mem_param *mem_params,
811 int num_reg_params, struct reg_param *reg_params,
812 uint32_t buffer_start, uint32_t buffer_size,
813 uint32_t entry_point, uint32_t exit_point, void *arch_info)
815 int retval;
817 /* Set up working area. First word is write pointer, second word is read pointer,
818 * rest is fifo data area. */
819 uint32_t wp_addr = buffer_start;
820 uint32_t rp_addr = buffer_start + 4;
821 uint32_t fifo_start_addr = buffer_start + 8;
822 uint32_t fifo_end_addr = buffer_start + buffer_size;
824 uint32_t wp = fifo_start_addr;
825 uint32_t rp = fifo_start_addr;
827 /* validate block_size is 2^n */
828 assert(!block_size || !(block_size & (block_size - 1)));
830 retval = target_write_u32(target, wp_addr, wp);
831 if (retval != ERROR_OK)
832 return retval;
833 retval = target_write_u32(target, rp_addr, rp);
834 if (retval != ERROR_OK)
835 return retval;
837 /* Start up algorithm on target and let it idle while writing the first chunk */
838 retval = target_start_algorithm(target, num_mem_params, mem_params,
839 num_reg_params, reg_params,
840 entry_point,
841 exit_point,
842 arch_info);
844 if (retval != ERROR_OK) {
845 LOG_ERROR("error starting target flash write algorithm");
846 return retval;
849 while (count > 0) {
851 retval = target_read_u32(target, rp_addr, &rp);
852 if (retval != ERROR_OK) {
853 LOG_ERROR("failed to get read pointer");
854 break;
857 LOG_DEBUG("count 0x%" PRIx32 " wp 0x%" PRIx32 " rp 0x%" PRIx32, count, wp, rp);
859 if (rp == 0) {
860 LOG_ERROR("flash write algorithm aborted by target");
861 retval = ERROR_FLASH_OPERATION_FAILED;
862 break;
865 if ((rp & (block_size - 1)) || rp < fifo_start_addr || rp >= fifo_end_addr) {
866 LOG_ERROR("corrupted fifo read pointer 0x%" PRIx32, rp);
867 break;
870 /* Count the number of bytes available in the fifo without
871 * crossing the wrap around. Make sure to not fill it completely,
872 * because that would make wp == rp and that's the empty condition. */
873 uint32_t thisrun_bytes;
874 if (rp > wp)
875 thisrun_bytes = rp - wp - block_size;
876 else if (rp > fifo_start_addr)
877 thisrun_bytes = fifo_end_addr - wp;
878 else
879 thisrun_bytes = fifo_end_addr - wp - block_size;
881 if (thisrun_bytes == 0) {
882 /* Throttle polling a bit if transfer is (much) faster than flash
883 * programming. The exact delay shouldn't matter as long as it's
884 * less than buffer size / flash speed. This is very unlikely to
885 * run when using high latency connections such as USB. */
886 alive_sleep(10);
887 continue;
890 /* Limit to the amount of data we actually want to write */
891 if (thisrun_bytes > count * block_size)
892 thisrun_bytes = count * block_size;
894 /* Write data to fifo */
895 retval = target_write_buffer(target, wp, thisrun_bytes, buffer);
896 if (retval != ERROR_OK)
897 break;
899 /* Update counters and wrap write pointer */
900 buffer += thisrun_bytes;
901 count -= thisrun_bytes / block_size;
902 wp += thisrun_bytes;
903 if (wp >= fifo_end_addr)
904 wp = fifo_start_addr;
906 /* Store updated write pointer to target */
907 retval = target_write_u32(target, wp_addr, wp);
908 if (retval != ERROR_OK)
909 break;
912 if (retval != ERROR_OK) {
913 /* abort flash write algorithm on target */
914 target_write_u32(target, wp_addr, 0);
917 int retval2 = target_wait_algorithm(target, num_mem_params, mem_params,
918 num_reg_params, reg_params,
919 exit_point,
920 10000,
921 arch_info);
923 if (retval2 != ERROR_OK) {
924 LOG_ERROR("error waiting for target flash write algorithm");
925 retval = retval2;
928 return retval;
931 int target_read_memory(struct target *target,
932 uint32_t address, uint32_t size, uint32_t count, uint8_t *buffer)
934 return target->type->read_memory(target, address, size, count, buffer);
937 static int target_read_phys_memory(struct target *target,
938 uint32_t address, uint32_t size, uint32_t count, uint8_t *buffer)
940 return target->type->read_phys_memory(target, address, size, count, buffer);
943 int target_write_memory(struct target *target,
944 uint32_t address, uint32_t size, uint32_t count, const uint8_t *buffer)
946 return target->type->write_memory(target, address, size, count, buffer);
949 static int target_write_phys_memory(struct target *target,
950 uint32_t address, uint32_t size, uint32_t count, const uint8_t *buffer)
952 return target->type->write_phys_memory(target, address, size, count, buffer);
955 int target_bulk_write_memory(struct target *target,
956 uint32_t address, uint32_t count, const uint8_t *buffer)
958 return target->type->bulk_write_memory(target, address, count, buffer);
961 int target_add_breakpoint(struct target *target,
962 struct breakpoint *breakpoint)
964 if ((target->state != TARGET_HALTED) && (breakpoint->type != BKPT_HARD)) {
965 LOG_WARNING("target %s is not halted", target->cmd_name);
966 return ERROR_TARGET_NOT_HALTED;
968 return target->type->add_breakpoint(target, breakpoint);
971 int target_add_context_breakpoint(struct target *target,
972 struct breakpoint *breakpoint)
974 if (target->state != TARGET_HALTED) {
975 LOG_WARNING("target %s is not halted", target->cmd_name);
976 return ERROR_TARGET_NOT_HALTED;
978 return target->type->add_context_breakpoint(target, breakpoint);
981 int target_add_hybrid_breakpoint(struct target *target,
982 struct breakpoint *breakpoint)
984 if (target->state != TARGET_HALTED) {
985 LOG_WARNING("target %s is not halted", target->cmd_name);
986 return ERROR_TARGET_NOT_HALTED;
988 return target->type->add_hybrid_breakpoint(target, breakpoint);
991 int target_remove_breakpoint(struct target *target,
992 struct breakpoint *breakpoint)
994 return target->type->remove_breakpoint(target, breakpoint);
997 int target_add_watchpoint(struct target *target,
998 struct watchpoint *watchpoint)
1000 if (target->state != TARGET_HALTED) {
1001 LOG_WARNING("target %s is not halted", target->cmd_name);
1002 return ERROR_TARGET_NOT_HALTED;
1004 return target->type->add_watchpoint(target, watchpoint);
1006 int target_remove_watchpoint(struct target *target,
1007 struct watchpoint *watchpoint)
1009 return target->type->remove_watchpoint(target, watchpoint);
1012 int target_get_gdb_reg_list(struct target *target,
1013 struct reg **reg_list[], int *reg_list_size)
1015 return target->type->get_gdb_reg_list(target, reg_list, reg_list_size);
1017 int target_step(struct target *target,
1018 int current, uint32_t address, int handle_breakpoints)
1020 return target->type->step(target, current, address, handle_breakpoints);
1024 * Reset the @c examined flag for the given target.
1025 * Pure paranoia -- targets are zeroed on allocation.
1027 static void target_reset_examined(struct target *target)
1029 target->examined = false;
1032 static int err_read_phys_memory(struct target *target, uint32_t address,
1033 uint32_t size, uint32_t count, uint8_t *buffer)
1035 LOG_ERROR("Not implemented: %s", __func__);
1036 return ERROR_FAIL;
1039 static int err_write_phys_memory(struct target *target, uint32_t address,
1040 uint32_t size, uint32_t count, const uint8_t *buffer)
1042 LOG_ERROR("Not implemented: %s", __func__);
1043 return ERROR_FAIL;
1046 static int handle_target(void *priv);
1048 static int target_init_one(struct command_context *cmd_ctx,
1049 struct target *target)
1051 target_reset_examined(target);
1053 struct target_type *type = target->type;
1054 if (type->examine == NULL)
1055 type->examine = default_examine;
1057 if (type->check_reset == NULL)
1058 type->check_reset = default_check_reset;
1060 assert(type->init_target != NULL);
1062 int retval = type->init_target(cmd_ctx, target);
1063 if (ERROR_OK != retval) {
1064 LOG_ERROR("target '%s' init failed", target_name(target));
1065 return retval;
1069 * @todo get rid of those *memory_imp() methods, now that all
1070 * callers are using target_*_memory() accessors ... and make
1071 * sure the "physical" paths handle the same issues.
1073 /* a non-invasive way(in terms of patches) to add some code that
1074 * runs before the type->write/read_memory implementation
1076 type->write_memory_imp = target->type->write_memory;
1077 type->write_memory = target_write_memory_imp;
1079 type->read_memory_imp = target->type->read_memory;
1080 type->read_memory = target_read_memory_imp;
1082 type->soft_reset_halt_imp = target->type->soft_reset_halt;
1083 type->soft_reset_halt = target_soft_reset_halt_imp;
1085 /* Sanity-check MMU support ... stub in what we must, to help
1086 * implement it in stages, but warn if we need to do so.
1088 if (type->mmu) {
1089 if (type->write_phys_memory == NULL) {
1090 LOG_ERROR("type '%s' is missing write_phys_memory",
1091 type->name);
1092 type->write_phys_memory = err_write_phys_memory;
1094 if (type->read_phys_memory == NULL) {
1095 LOG_ERROR("type '%s' is missing read_phys_memory",
1096 type->name);
1097 type->read_phys_memory = err_read_phys_memory;
1099 if (type->virt2phys == NULL) {
1100 LOG_ERROR("type '%s' is missing virt2phys", type->name);
1101 type->virt2phys = identity_virt2phys;
1103 } else {
1104 /* Make sure no-MMU targets all behave the same: make no
1105 * distinction between physical and virtual addresses, and
1106 * ensure that virt2phys() is always an identity mapping.
1108 if (type->write_phys_memory || type->read_phys_memory || type->virt2phys)
1109 LOG_WARNING("type '%s' has bad MMU hooks", type->name);
1111 type->mmu = no_mmu;
1112 type->write_phys_memory = type->write_memory;
1113 type->read_phys_memory = type->read_memory;
1114 type->virt2phys = identity_virt2phys;
1117 if (target->type->read_buffer == NULL)
1118 target->type->read_buffer = target_read_buffer_default;
1120 if (target->type->write_buffer == NULL)
1121 target->type->write_buffer = target_write_buffer_default;
1123 return ERROR_OK;
1126 static int target_init(struct command_context *cmd_ctx)
1128 struct target *target;
1129 int retval;
1131 for (target = all_targets; target; target = target->next) {
1132 retval = target_init_one(cmd_ctx, target);
1133 if (ERROR_OK != retval)
1134 return retval;
1137 if (!all_targets)
1138 return ERROR_OK;
1140 retval = target_register_user_commands(cmd_ctx);
1141 if (ERROR_OK != retval)
1142 return retval;
1144 retval = target_register_timer_callback(&handle_target,
1145 polling_interval, 1, cmd_ctx->interp);
1146 if (ERROR_OK != retval)
1147 return retval;
1149 return ERROR_OK;
1152 COMMAND_HANDLER(handle_target_init_command)
1154 int retval;
1156 if (CMD_ARGC != 0)
1157 return ERROR_COMMAND_SYNTAX_ERROR;
1159 static bool target_initialized;
1160 if (target_initialized) {
1161 LOG_INFO("'target init' has already been called");
1162 return ERROR_OK;
1164 target_initialized = true;
1166 retval = command_run_line(CMD_CTX, "init_targets");
1167 if (ERROR_OK != retval)
1168 return retval;
1170 retval = command_run_line(CMD_CTX, "init_board");
1171 if (ERROR_OK != retval)
1172 return retval;
1174 LOG_DEBUG("Initializing targets...");
1175 return target_init(CMD_CTX);
1178 int target_register_event_callback(int (*callback)(struct target *target,
1179 enum target_event event, void *priv), void *priv)
1181 struct target_event_callback **callbacks_p = &target_event_callbacks;
1183 if (callback == NULL)
1184 return ERROR_COMMAND_SYNTAX_ERROR;
1186 if (*callbacks_p) {
1187 while ((*callbacks_p)->next)
1188 callbacks_p = &((*callbacks_p)->next);
1189 callbacks_p = &((*callbacks_p)->next);
1192 (*callbacks_p) = malloc(sizeof(struct target_event_callback));
1193 (*callbacks_p)->callback = callback;
1194 (*callbacks_p)->priv = priv;
1195 (*callbacks_p)->next = NULL;
1197 return ERROR_OK;
1200 int target_register_timer_callback(int (*callback)(void *priv), int time_ms, int periodic, void *priv)
1202 struct target_timer_callback **callbacks_p = &target_timer_callbacks;
1203 struct timeval now;
1205 if (callback == NULL)
1206 return ERROR_COMMAND_SYNTAX_ERROR;
1208 if (*callbacks_p) {
1209 while ((*callbacks_p)->next)
1210 callbacks_p = &((*callbacks_p)->next);
1211 callbacks_p = &((*callbacks_p)->next);
1214 (*callbacks_p) = malloc(sizeof(struct target_timer_callback));
1215 (*callbacks_p)->callback = callback;
1216 (*callbacks_p)->periodic = periodic;
1217 (*callbacks_p)->time_ms = time_ms;
1219 gettimeofday(&now, NULL);
1220 (*callbacks_p)->when.tv_usec = now.tv_usec + (time_ms % 1000) * 1000;
1221 time_ms -= (time_ms % 1000);
1222 (*callbacks_p)->when.tv_sec = now.tv_sec + (time_ms / 1000);
1223 if ((*callbacks_p)->when.tv_usec > 1000000) {
1224 (*callbacks_p)->when.tv_usec = (*callbacks_p)->when.tv_usec - 1000000;
1225 (*callbacks_p)->when.tv_sec += 1;
1228 (*callbacks_p)->priv = priv;
1229 (*callbacks_p)->next = NULL;
1231 return ERROR_OK;
1234 int target_unregister_event_callback(int (*callback)(struct target *target,
1235 enum target_event event, void *priv), void *priv)
1237 struct target_event_callback **p = &target_event_callbacks;
1238 struct target_event_callback *c = target_event_callbacks;
1240 if (callback == NULL)
1241 return ERROR_COMMAND_SYNTAX_ERROR;
1243 while (c) {
1244 struct target_event_callback *next = c->next;
1245 if ((c->callback == callback) && (c->priv == priv)) {
1246 *p = next;
1247 free(c);
1248 return ERROR_OK;
1249 } else
1250 p = &(c->next);
1251 c = next;
1254 return ERROR_OK;
1257 static int target_unregister_timer_callback(int (*callback)(void *priv), void *priv)
1259 struct target_timer_callback **p = &target_timer_callbacks;
1260 struct target_timer_callback *c = target_timer_callbacks;
1262 if (callback == NULL)
1263 return ERROR_COMMAND_SYNTAX_ERROR;
1265 while (c) {
1266 struct target_timer_callback *next = c->next;
1267 if ((c->callback == callback) && (c->priv == priv)) {
1268 *p = next;
1269 free(c);
1270 return ERROR_OK;
1271 } else
1272 p = &(c->next);
1273 c = next;
1276 return ERROR_OK;
1279 int target_call_event_callbacks(struct target *target, enum target_event event)
1281 struct target_event_callback *callback = target_event_callbacks;
1282 struct target_event_callback *next_callback;
1284 if (event == TARGET_EVENT_HALTED) {
1285 /* execute early halted first */
1286 target_call_event_callbacks(target, TARGET_EVENT_GDB_HALT);
1289 LOG_DEBUG("target event %i (%s)", event,
1290 Jim_Nvp_value2name_simple(nvp_target_event, event)->name);
1292 target_handle_event(target, event);
1294 while (callback) {
1295 next_callback = callback->next;
1296 callback->callback(target, event, callback->priv);
1297 callback = next_callback;
1300 return ERROR_OK;
1303 static int target_timer_callback_periodic_restart(
1304 struct target_timer_callback *cb, struct timeval *now)
1306 int time_ms = cb->time_ms;
1307 cb->when.tv_usec = now->tv_usec + (time_ms % 1000) * 1000;
1308 time_ms -= (time_ms % 1000);
1309 cb->when.tv_sec = now->tv_sec + time_ms / 1000;
1310 if (cb->when.tv_usec > 1000000) {
1311 cb->when.tv_usec = cb->when.tv_usec - 1000000;
1312 cb->when.tv_sec += 1;
1314 return ERROR_OK;
1317 static int target_call_timer_callback(struct target_timer_callback *cb,
1318 struct timeval *now)
1320 cb->callback(cb->priv);
1322 if (cb->periodic)
1323 return target_timer_callback_periodic_restart(cb, now);
1325 return target_unregister_timer_callback(cb->callback, cb->priv);
1328 static int target_call_timer_callbacks_check_time(int checktime)
1330 keep_alive();
1332 struct timeval now;
1333 gettimeofday(&now, NULL);
1335 struct target_timer_callback *callback = target_timer_callbacks;
1336 while (callback) {
1337 /* cleaning up may unregister and free this callback */
1338 struct target_timer_callback *next_callback = callback->next;
1340 bool call_it = callback->callback &&
1341 ((!checktime && callback->periodic) ||
1342 now.tv_sec > callback->when.tv_sec ||
1343 (now.tv_sec == callback->when.tv_sec &&
1344 now.tv_usec >= callback->when.tv_usec));
1346 if (call_it) {
1347 int retval = target_call_timer_callback(callback, &now);
1348 if (retval != ERROR_OK)
1349 return retval;
1352 callback = next_callback;
1355 return ERROR_OK;
1358 int target_call_timer_callbacks(void)
1360 return target_call_timer_callbacks_check_time(1);
1363 /* invoke periodic callbacks immediately */
1364 int target_call_timer_callbacks_now(void)
1366 return target_call_timer_callbacks_check_time(0);
1369 /* Prints the working area layout for debug purposes */
1370 static void print_wa_layout(struct target *target)
1372 struct working_area *c = target->working_areas;
1374 while (c) {
1375 LOG_DEBUG("%c%c 0x%08"PRIx32"-0x%08"PRIx32" (%"PRIu32" bytes)",
1376 c->backup ? 'b' : ' ', c->free ? ' ' : '*',
1377 c->address, c->address + c->size - 1, c->size);
1378 c = c->next;
1382 /* Reduce area to size bytes, create a new free area from the remaining bytes, if any. */
1383 static void target_split_working_area(struct working_area *area, uint32_t size)
1385 assert(area->free); /* Shouldn't split an allocated area */
1386 assert(size <= area->size); /* Caller should guarantee this */
1388 /* Split only if not already the right size */
1389 if (size < area->size) {
1390 struct working_area *new_wa = malloc(sizeof(*new_wa));
1392 if (new_wa == NULL)
1393 return;
1395 new_wa->next = area->next;
1396 new_wa->size = area->size - size;
1397 new_wa->address = area->address + size;
1398 new_wa->backup = NULL;
1399 new_wa->user = NULL;
1400 new_wa->free = true;
1402 area->next = new_wa;
1403 area->size = size;
1405 /* If backup memory was allocated to this area, it has the wrong size
1406 * now so free it and it will be reallocated if/when needed */
1407 if (area->backup) {
1408 free(area->backup);
1409 area->backup = NULL;
1414 /* Merge all adjacent free areas into one */
1415 static void target_merge_working_areas(struct target *target)
1417 struct working_area *c = target->working_areas;
1419 while (c && c->next) {
1420 assert(c->next->address == c->address + c->size); /* This is an invariant */
1422 /* Find two adjacent free areas */
1423 if (c->free && c->next->free) {
1424 /* Merge the last into the first */
1425 c->size += c->next->size;
1427 /* Remove the last */
1428 struct working_area *to_be_freed = c->next;
1429 c->next = c->next->next;
1430 if (to_be_freed->backup)
1431 free(to_be_freed->backup);
1432 free(to_be_freed);
1434 /* If backup memory was allocated to the remaining area, it's has
1435 * the wrong size now */
1436 if (c->backup) {
1437 free(c->backup);
1438 c->backup = NULL;
1440 } else {
1441 c = c->next;
1446 int target_alloc_working_area_try(struct target *target, uint32_t size, struct working_area **area)
1448 /* Reevaluate working area address based on MMU state*/
1449 if (target->working_areas == NULL) {
1450 int retval;
1451 int enabled;
1453 retval = target->type->mmu(target, &enabled);
1454 if (retval != ERROR_OK)
1455 return retval;
1457 if (!enabled) {
1458 if (target->working_area_phys_spec) {
1459 LOG_DEBUG("MMU disabled, using physical "
1460 "address for working memory 0x%08"PRIx32,
1461 target->working_area_phys);
1462 target->working_area = target->working_area_phys;
1463 } else {
1464 LOG_ERROR("No working memory available. "
1465 "Specify -work-area-phys to target.");
1466 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
1468 } else {
1469 if (target->working_area_virt_spec) {
1470 LOG_DEBUG("MMU enabled, using virtual "
1471 "address for working memory 0x%08"PRIx32,
1472 target->working_area_virt);
1473 target->working_area = target->working_area_virt;
1474 } else {
1475 LOG_ERROR("No working memory available. "
1476 "Specify -work-area-virt to target.");
1477 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
1481 /* Set up initial working area on first call */
1482 struct working_area *new_wa = malloc(sizeof(*new_wa));
1483 if (new_wa) {
1484 new_wa->next = NULL;
1485 new_wa->size = target->working_area_size & ~3UL; /* 4-byte align */
1486 new_wa->address = target->working_area;
1487 new_wa->backup = NULL;
1488 new_wa->user = NULL;
1489 new_wa->free = true;
1492 target->working_areas = new_wa;
1495 /* only allocate multiples of 4 byte */
1496 if (size % 4)
1497 size = (size + 3) & (~3UL);
1499 struct working_area *c = target->working_areas;
1501 /* Find the first large enough working area */
1502 while (c) {
1503 if (c->free && c->size >= size)
1504 break;
1505 c = c->next;
1508 if (c == NULL)
1509 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
1511 /* Split the working area into the requested size */
1512 target_split_working_area(c, size);
1514 LOG_DEBUG("allocated new working area of %"PRIu32" bytes at address 0x%08"PRIx32, size, c->address);
1516 if (target->backup_working_area) {
1517 if (c->backup == NULL) {
1518 c->backup = malloc(c->size);
1519 if (c->backup == NULL)
1520 return ERROR_FAIL;
1523 int retval = target_read_memory(target, c->address, 4, c->size / 4, c->backup);
1524 if (retval != ERROR_OK)
1525 return retval;
1528 /* mark as used, and return the new (reused) area */
1529 c->free = false;
1530 *area = c;
1532 /* user pointer */
1533 c->user = area;
1535 print_wa_layout(target);
1537 return ERROR_OK;
1540 int target_alloc_working_area(struct target *target, uint32_t size, struct working_area **area)
1542 int retval;
1544 retval = target_alloc_working_area_try(target, size, area);
1545 if (retval == ERROR_TARGET_RESOURCE_NOT_AVAILABLE)
1546 LOG_WARNING("not enough working area available(requested %"PRIu32")", size);
1547 return retval;
1551 static int target_restore_working_area(struct target *target, struct working_area *area)
1553 int retval = ERROR_OK;
1555 if (target->backup_working_area && area->backup != NULL) {
1556 retval = target_write_memory(target, area->address, 4, area->size / 4, area->backup);
1557 if (retval != ERROR_OK)
1558 LOG_ERROR("failed to restore %"PRIu32" bytes of working area at address 0x%08"PRIx32,
1559 area->size, area->address);
1562 return retval;
1565 /* Restore the area's backup memory, if any, and return the area to the allocation pool */
1566 static int target_free_working_area_restore(struct target *target, struct working_area *area, int restore)
1568 int retval = ERROR_OK;
1570 if (area->free)
1571 return retval;
1573 if (restore) {
1574 retval = target_restore_working_area(target, area);
1575 /* REVISIT: Perhaps the area should be freed even if restoring fails. */
1576 if (retval != ERROR_OK)
1577 return retval;
1580 area->free = true;
1582 LOG_DEBUG("freed %"PRIu32" bytes of working area at address 0x%08"PRIx32,
1583 area->size, area->address);
1585 /* mark user pointer invalid */
1586 /* TODO: Is this really safe? It points to some previous caller's memory.
1587 * How could we know that the area pointer is still in that place and not
1588 * some other vital data? What's the purpose of this, anyway? */
1589 *area->user = NULL;
1590 area->user = NULL;
1592 target_merge_working_areas(target);
1594 print_wa_layout(target);
1596 return retval;
1599 int target_free_working_area(struct target *target, struct working_area *area)
1601 return target_free_working_area_restore(target, area, 1);
1604 /* free resources and restore memory, if restoring memory fails,
1605 * free up resources anyway
1607 static void target_free_all_working_areas_restore(struct target *target, int restore)
1609 struct working_area *c = target->working_areas;
1611 LOG_DEBUG("freeing all working areas");
1613 /* Loop through all areas, restoring the allocated ones and marking them as free */
1614 while (c) {
1615 if (!c->free) {
1616 if (restore)
1617 target_restore_working_area(target, c);
1618 c->free = true;
1619 *c->user = NULL; /* Same as above */
1620 c->user = NULL;
1622 c = c->next;
1625 /* Run a merge pass to combine all areas into one */
1626 target_merge_working_areas(target);
1628 print_wa_layout(target);
1631 void target_free_all_working_areas(struct target *target)
1633 target_free_all_working_areas_restore(target, 1);
1636 /* Find the largest number of bytes that can be allocated */
1637 uint32_t target_get_working_area_avail(struct target *target)
1639 struct working_area *c = target->working_areas;
1640 uint32_t max_size = 0;
1642 if (c == NULL)
1643 return target->working_area_size;
1645 while (c) {
1646 if (c->free && max_size < c->size)
1647 max_size = c->size;
1649 c = c->next;
1652 return max_size;
1655 int target_arch_state(struct target *target)
1657 int retval;
1658 if (target == NULL) {
1659 LOG_USER("No target has been configured");
1660 return ERROR_OK;
1663 LOG_USER("target state: %s", target_state_name(target));
1665 if (target->state != TARGET_HALTED)
1666 return ERROR_OK;
1668 retval = target->type->arch_state(target);
1669 return retval;
1672 /* Single aligned words are guaranteed to use 16 or 32 bit access
1673 * mode respectively, otherwise data is handled as quickly as
1674 * possible
1676 int target_write_buffer(struct target *target, uint32_t address, uint32_t size, const uint8_t *buffer)
1678 LOG_DEBUG("writing buffer of %i byte at 0x%8.8x",
1679 (int)size, (unsigned)address);
1681 if (!target_was_examined(target)) {
1682 LOG_ERROR("Target not examined yet");
1683 return ERROR_FAIL;
1686 if (size == 0)
1687 return ERROR_OK;
1689 if ((address + size - 1) < address) {
1690 /* GDB can request this when e.g. PC is 0xfffffffc*/
1691 LOG_ERROR("address + size wrapped(0x%08x, 0x%08x)",
1692 (unsigned)address,
1693 (unsigned)size);
1694 return ERROR_FAIL;
1697 return target->type->write_buffer(target, address, size, buffer);
1700 static int target_write_buffer_default(struct target *target, uint32_t address, uint32_t size, const uint8_t *buffer)
1702 int retval = ERROR_OK;
1704 if (((address % 2) == 0) && (size == 2))
1705 return target_write_memory(target, address, 2, 1, buffer);
1707 /* handle unaligned head bytes */
1708 if (address % 4) {
1709 uint32_t unaligned = 4 - (address % 4);
1711 if (unaligned > size)
1712 unaligned = size;
1714 retval = target_write_memory(target, address, 1, unaligned, buffer);
1715 if (retval != ERROR_OK)
1716 return retval;
1718 buffer += unaligned;
1719 address += unaligned;
1720 size -= unaligned;
1723 /* handle aligned words */
1724 if (size >= 4) {
1725 int aligned = size - (size % 4);
1727 /* use bulk writes above a certain limit. This may have to be changed */
1728 if (aligned > 128) {
1729 retval = target->type->bulk_write_memory(target, address, aligned / 4, buffer);
1730 if (retval != ERROR_OK)
1731 return retval;
1732 } else {
1733 retval = target_write_memory(target, address, 4, aligned / 4, buffer);
1734 if (retval != ERROR_OK)
1735 return retval;
1738 buffer += aligned;
1739 address += aligned;
1740 size -= aligned;
1743 /* handle tail writes of less than 4 bytes */
1744 if (size > 0) {
1745 retval = target_write_memory(target, address, 1, size, buffer);
1746 if (retval != ERROR_OK)
1747 return retval;
1750 return retval;
1753 /* Single aligned words are guaranteed to use 16 or 32 bit access
1754 * mode respectively, otherwise data is handled as quickly as
1755 * possible
1757 int target_read_buffer(struct target *target, uint32_t address, uint32_t size, uint8_t *buffer)
1759 LOG_DEBUG("reading buffer of %i byte at 0x%8.8x",
1760 (int)size, (unsigned)address);
1762 if (!target_was_examined(target)) {
1763 LOG_ERROR("Target not examined yet");
1764 return ERROR_FAIL;
1767 if (size == 0)
1768 return ERROR_OK;
1770 if ((address + size - 1) < address) {
1771 /* GDB can request this when e.g. PC is 0xfffffffc*/
1772 LOG_ERROR("address + size wrapped(0x%08" PRIx32 ", 0x%08" PRIx32 ")",
1773 address,
1774 size);
1775 return ERROR_FAIL;
1778 return target->type->read_buffer(target, address, size, buffer);
1781 static int target_read_buffer_default(struct target *target, uint32_t address, uint32_t size, uint8_t *buffer)
1783 int retval = ERROR_OK;
1785 if (((address % 2) == 0) && (size == 2))
1786 return target_read_memory(target, address, 2, 1, buffer);
1788 /* handle unaligned head bytes */
1789 if (address % 4) {
1790 uint32_t unaligned = 4 - (address % 4);
1792 if (unaligned > size)
1793 unaligned = size;
1795 retval = target_read_memory(target, address, 1, unaligned, buffer);
1796 if (retval != ERROR_OK)
1797 return retval;
1799 buffer += unaligned;
1800 address += unaligned;
1801 size -= unaligned;
1804 /* handle aligned words */
1805 if (size >= 4) {
1806 int aligned = size - (size % 4);
1808 retval = target_read_memory(target, address, 4, aligned / 4, buffer);
1809 if (retval != ERROR_OK)
1810 return retval;
1812 buffer += aligned;
1813 address += aligned;
1814 size -= aligned;
1817 /*prevent byte access when possible (avoid AHB access limitations in some cases)*/
1818 if (size >= 2) {
1819 int aligned = size - (size % 2);
1820 retval = target_read_memory(target, address, 2, aligned / 2, buffer);
1821 if (retval != ERROR_OK)
1822 return retval;
1824 buffer += aligned;
1825 address += aligned;
1826 size -= aligned;
1828 /* handle tail writes of less than 4 bytes */
1829 if (size > 0) {
1830 retval = target_read_memory(target, address, 1, size, buffer);
1831 if (retval != ERROR_OK)
1832 return retval;
1835 return ERROR_OK;
1838 int target_checksum_memory(struct target *target, uint32_t address, uint32_t size, uint32_t* crc)
1840 uint8_t *buffer;
1841 int retval;
1842 uint32_t i;
1843 uint32_t checksum = 0;
1844 if (!target_was_examined(target)) {
1845 LOG_ERROR("Target not examined yet");
1846 return ERROR_FAIL;
1849 retval = target->type->checksum_memory(target, address, size, &checksum);
1850 if (retval != ERROR_OK) {
1851 buffer = malloc(size);
1852 if (buffer == NULL) {
1853 LOG_ERROR("error allocating buffer for section (%d bytes)", (int)size);
1854 return ERROR_COMMAND_SYNTAX_ERROR;
1856 retval = target_read_buffer(target, address, size, buffer);
1857 if (retval != ERROR_OK) {
1858 free(buffer);
1859 return retval;
1862 /* convert to target endianness */
1863 for (i = 0; i < (size/sizeof(uint32_t)); i++) {
1864 uint32_t target_data;
1865 target_data = target_buffer_get_u32(target, &buffer[i*sizeof(uint32_t)]);
1866 target_buffer_set_u32(target, &buffer[i*sizeof(uint32_t)], target_data);
1869 retval = image_calculate_checksum(buffer, size, &checksum);
1870 free(buffer);
1873 *crc = checksum;
1875 return retval;
1878 int target_blank_check_memory(struct target *target, uint32_t address, uint32_t size, uint32_t* blank)
1880 int retval;
1881 if (!target_was_examined(target)) {
1882 LOG_ERROR("Target not examined yet");
1883 return ERROR_FAIL;
1886 if (target->type->blank_check_memory == 0)
1887 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
1889 retval = target->type->blank_check_memory(target, address, size, blank);
1891 return retval;
1894 int target_read_u32(struct target *target, uint32_t address, uint32_t *value)
1896 uint8_t value_buf[4];
1897 if (!target_was_examined(target)) {
1898 LOG_ERROR("Target not examined yet");
1899 return ERROR_FAIL;
1902 int retval = target_read_memory(target, address, 4, 1, value_buf);
1904 if (retval == ERROR_OK) {
1905 *value = target_buffer_get_u32(target, value_buf);
1906 LOG_DEBUG("address: 0x%8.8" PRIx32 ", value: 0x%8.8" PRIx32 "",
1907 address,
1908 *value);
1909 } else {
1910 *value = 0x0;
1911 LOG_DEBUG("address: 0x%8.8" PRIx32 " failed",
1912 address);
1915 return retval;
1918 int target_read_u16(struct target *target, uint32_t address, uint16_t *value)
1920 uint8_t value_buf[2];
1921 if (!target_was_examined(target)) {
1922 LOG_ERROR("Target not examined yet");
1923 return ERROR_FAIL;
1926 int retval = target_read_memory(target, address, 2, 1, value_buf);
1928 if (retval == ERROR_OK) {
1929 *value = target_buffer_get_u16(target, value_buf);
1930 LOG_DEBUG("address: 0x%8.8" PRIx32 ", value: 0x%4.4x",
1931 address,
1932 *value);
1933 } else {
1934 *value = 0x0;
1935 LOG_DEBUG("address: 0x%8.8" PRIx32 " failed",
1936 address);
1939 return retval;
1942 int target_read_u8(struct target *target, uint32_t address, uint8_t *value)
1944 int retval = target_read_memory(target, address, 1, 1, value);
1945 if (!target_was_examined(target)) {
1946 LOG_ERROR("Target not examined yet");
1947 return ERROR_FAIL;
1950 if (retval == ERROR_OK) {
1951 LOG_DEBUG("address: 0x%8.8" PRIx32 ", value: 0x%2.2x",
1952 address,
1953 *value);
1954 } else {
1955 *value = 0x0;
1956 LOG_DEBUG("address: 0x%8.8" PRIx32 " failed",
1957 address);
1960 return retval;
1963 int target_write_u32(struct target *target, uint32_t address, uint32_t value)
1965 int retval;
1966 uint8_t value_buf[4];
1967 if (!target_was_examined(target)) {
1968 LOG_ERROR("Target not examined yet");
1969 return ERROR_FAIL;
1972 LOG_DEBUG("address: 0x%8.8" PRIx32 ", value: 0x%8.8" PRIx32 "",
1973 address,
1974 value);
1976 target_buffer_set_u32(target, value_buf, value);
1977 retval = target_write_memory(target, address, 4, 1, value_buf);
1978 if (retval != ERROR_OK)
1979 LOG_DEBUG("failed: %i", retval);
1981 return retval;
1984 int target_write_u16(struct target *target, uint32_t address, uint16_t value)
1986 int retval;
1987 uint8_t value_buf[2];
1988 if (!target_was_examined(target)) {
1989 LOG_ERROR("Target not examined yet");
1990 return ERROR_FAIL;
1993 LOG_DEBUG("address: 0x%8.8" PRIx32 ", value: 0x%8.8x",
1994 address,
1995 value);
1997 target_buffer_set_u16(target, value_buf, value);
1998 retval = target_write_memory(target, address, 2, 1, value_buf);
1999 if (retval != ERROR_OK)
2000 LOG_DEBUG("failed: %i", retval);
2002 return retval;
2005 int target_write_u8(struct target *target, uint32_t address, uint8_t value)
2007 int retval;
2008 if (!target_was_examined(target)) {
2009 LOG_ERROR("Target not examined yet");
2010 return ERROR_FAIL;
2013 LOG_DEBUG("address: 0x%8.8" PRIx32 ", value: 0x%2.2x",
2014 address, value);
2016 retval = target_write_memory(target, address, 1, 1, &value);
2017 if (retval != ERROR_OK)
2018 LOG_DEBUG("failed: %i", retval);
2020 return retval;
2023 static int find_target(struct command_context *cmd_ctx, const char *name)
2025 struct target *target = get_target(name);
2026 if (target == NULL) {
2027 LOG_ERROR("Target: %s is unknown, try one of:\n", name);
2028 return ERROR_FAIL;
2030 if (!target->tap->enabled) {
2031 LOG_USER("Target: TAP %s is disabled, "
2032 "can't be the current target\n",
2033 target->tap->dotted_name);
2034 return ERROR_FAIL;
2037 cmd_ctx->current_target = target->target_number;
2038 return ERROR_OK;
2042 COMMAND_HANDLER(handle_targets_command)
2044 int retval = ERROR_OK;
2045 if (CMD_ARGC == 1) {
2046 retval = find_target(CMD_CTX, CMD_ARGV[0]);
2047 if (retval == ERROR_OK) {
2048 /* we're done! */
2049 return retval;
2053 struct target *target = all_targets;
2054 command_print(CMD_CTX, " TargetName Type Endian TapName State ");
2055 command_print(CMD_CTX, "-- ------------------ ---------- ------ ------------------ ------------");
2056 while (target) {
2057 const char *state;
2058 char marker = ' ';
2060 if (target->tap->enabled)
2061 state = target_state_name(target);
2062 else
2063 state = "tap-disabled";
2065 if (CMD_CTX->current_target == target->target_number)
2066 marker = '*';
2068 /* keep columns lined up to match the headers above */
2069 command_print(CMD_CTX,
2070 "%2d%c %-18s %-10s %-6s %-18s %s",
2071 target->target_number,
2072 marker,
2073 target_name(target),
2074 target_type_name(target),
2075 Jim_Nvp_value2name_simple(nvp_target_endian,
2076 target->endianness)->name,
2077 target->tap->dotted_name,
2078 state);
2079 target = target->next;
2082 return retval;
2085 /* every 300ms we check for reset & powerdropout and issue a "reset halt" if so. */
2087 static int powerDropout;
2088 static int srstAsserted;
2090 static int runPowerRestore;
2091 static int runPowerDropout;
2092 static int runSrstAsserted;
2093 static int runSrstDeasserted;
2095 static int sense_handler(void)
2097 static int prevSrstAsserted;
2098 static int prevPowerdropout;
2100 int retval = jtag_power_dropout(&powerDropout);
2101 if (retval != ERROR_OK)
2102 return retval;
2104 int powerRestored;
2105 powerRestored = prevPowerdropout && !powerDropout;
2106 if (powerRestored)
2107 runPowerRestore = 1;
2109 long long current = timeval_ms();
2110 static long long lastPower;
2111 int waitMore = lastPower + 2000 > current;
2112 if (powerDropout && !waitMore) {
2113 runPowerDropout = 1;
2114 lastPower = current;
2117 retval = jtag_srst_asserted(&srstAsserted);
2118 if (retval != ERROR_OK)
2119 return retval;
2121 int srstDeasserted;
2122 srstDeasserted = prevSrstAsserted && !srstAsserted;
2124 static long long lastSrst;
2125 waitMore = lastSrst + 2000 > current;
2126 if (srstDeasserted && !waitMore) {
2127 runSrstDeasserted = 1;
2128 lastSrst = current;
2131 if (!prevSrstAsserted && srstAsserted)
2132 runSrstAsserted = 1;
2134 prevSrstAsserted = srstAsserted;
2135 prevPowerdropout = powerDropout;
2137 if (srstDeasserted || powerRestored) {
2138 /* Other than logging the event we can't do anything here.
2139 * Issuing a reset is a particularly bad idea as we might
2140 * be inside a reset already.
2144 return ERROR_OK;
2147 static int backoff_times;
2148 static int backoff_count;
2150 /* process target state changes */
2151 static int handle_target(void *priv)
2153 Jim_Interp *interp = (Jim_Interp *)priv;
2154 int retval = ERROR_OK;
2156 if (!is_jtag_poll_safe()) {
2157 /* polling is disabled currently */
2158 return ERROR_OK;
2161 /* we do not want to recurse here... */
2162 static int recursive;
2163 if (!recursive) {
2164 recursive = 1;
2165 sense_handler();
2166 /* danger! running these procedures can trigger srst assertions and power dropouts.
2167 * We need to avoid an infinite loop/recursion here and we do that by
2168 * clearing the flags after running these events.
2170 int did_something = 0;
2171 if (runSrstAsserted) {
2172 LOG_INFO("srst asserted detected, running srst_asserted proc.");
2173 Jim_Eval(interp, "srst_asserted");
2174 did_something = 1;
2176 if (runSrstDeasserted) {
2177 Jim_Eval(interp, "srst_deasserted");
2178 did_something = 1;
2180 if (runPowerDropout) {
2181 LOG_INFO("Power dropout detected, running power_dropout proc.");
2182 Jim_Eval(interp, "power_dropout");
2183 did_something = 1;
2185 if (runPowerRestore) {
2186 Jim_Eval(interp, "power_restore");
2187 did_something = 1;
2190 if (did_something) {
2191 /* clear detect flags */
2192 sense_handler();
2195 /* clear action flags */
2197 runSrstAsserted = 0;
2198 runSrstDeasserted = 0;
2199 runPowerRestore = 0;
2200 runPowerDropout = 0;
2202 recursive = 0;
2205 if (backoff_times > backoff_count) {
2206 /* do not poll this time as we failed previously */
2207 backoff_count++;
2208 return ERROR_OK;
2210 backoff_count = 0;
2212 /* Poll targets for state changes unless that's globally disabled.
2213 * Skip targets that are currently disabled.
2215 for (struct target *target = all_targets;
2216 is_jtag_poll_safe() && target;
2217 target = target->next) {
2218 if (!target->tap->enabled)
2219 continue;
2221 /* only poll target if we've got power and srst isn't asserted */
2222 if (!powerDropout && !srstAsserted) {
2223 /* polling may fail silently until the target has been examined */
2224 retval = target_poll(target);
2225 if (retval != ERROR_OK) {
2226 /* 100ms polling interval. Increase interval between polling up to 5000ms */
2227 if (backoff_times * polling_interval < 5000) {
2228 backoff_times *= 2;
2229 backoff_times++;
2231 LOG_USER("Polling target failed, GDB will be halted. Polling again in %dms",
2232 backoff_times * polling_interval);
2234 /* Tell GDB to halt the debugger. This allows the user to
2235 * run monitor commands to handle the situation.
2237 target_call_event_callbacks(target, TARGET_EVENT_GDB_HALT);
2238 return retval;
2240 /* Since we succeeded, we reset backoff count */
2241 if (backoff_times > 0)
2242 LOG_USER("Polling succeeded again");
2243 backoff_times = 0;
2247 return retval;
2250 COMMAND_HANDLER(handle_reg_command)
2252 struct target *target;
2253 struct reg *reg = NULL;
2254 unsigned count = 0;
2255 char *value;
2257 LOG_DEBUG("-");
2259 target = get_current_target(CMD_CTX);
2261 /* list all available registers for the current target */
2262 if (CMD_ARGC == 0) {
2263 struct reg_cache *cache = target->reg_cache;
2265 count = 0;
2266 while (cache) {
2267 unsigned i;
2269 command_print(CMD_CTX, "===== %s", cache->name);
2271 for (i = 0, reg = cache->reg_list;
2272 i < cache->num_regs;
2273 i++, reg++, count++) {
2274 /* only print cached values if they are valid */
2275 if (reg->valid) {
2276 value = buf_to_str(reg->value,
2277 reg->size, 16);
2278 command_print(CMD_CTX,
2279 "(%i) %s (/%" PRIu32 "): 0x%s%s",
2280 count, reg->name,
2281 reg->size, value,
2282 reg->dirty
2283 ? " (dirty)"
2284 : "");
2285 free(value);
2286 } else {
2287 command_print(CMD_CTX, "(%i) %s (/%" PRIu32 ")",
2288 count, reg->name,
2289 reg->size) ;
2292 cache = cache->next;
2295 return ERROR_OK;
2298 /* access a single register by its ordinal number */
2299 if ((CMD_ARGV[0][0] >= '0') && (CMD_ARGV[0][0] <= '9')) {
2300 unsigned num;
2301 COMMAND_PARSE_NUMBER(uint, CMD_ARGV[0], num);
2303 struct reg_cache *cache = target->reg_cache;
2304 count = 0;
2305 while (cache) {
2306 unsigned i;
2307 for (i = 0; i < cache->num_regs; i++) {
2308 if (count++ == num) {
2309 reg = &cache->reg_list[i];
2310 break;
2313 if (reg)
2314 break;
2315 cache = cache->next;
2318 if (!reg) {
2319 command_print(CMD_CTX, "%i is out of bounds, the current target "
2320 "has only %i registers (0 - %i)", num, count, count - 1);
2321 return ERROR_OK;
2323 } else {
2324 /* access a single register by its name */
2325 reg = register_get_by_name(target->reg_cache, CMD_ARGV[0], 1);
2327 if (!reg) {
2328 command_print(CMD_CTX, "register %s not found in current target", CMD_ARGV[0]);
2329 return ERROR_OK;
2333 assert(reg != NULL); /* give clang a hint that we *know* reg is != NULL here */
2335 /* display a register */
2336 if ((CMD_ARGC == 1) || ((CMD_ARGC == 2) && !((CMD_ARGV[1][0] >= '0')
2337 && (CMD_ARGV[1][0] <= '9')))) {
2338 if ((CMD_ARGC == 2) && (strcmp(CMD_ARGV[1], "force") == 0))
2339 reg->valid = 0;
2341 if (reg->valid == 0)
2342 reg->type->get(reg);
2343 value = buf_to_str(reg->value, reg->size, 16);
2344 command_print(CMD_CTX, "%s (/%i): 0x%s", reg->name, (int)(reg->size), value);
2345 free(value);
2346 return ERROR_OK;
2349 /* set register value */
2350 if (CMD_ARGC == 2) {
2351 uint8_t *buf = malloc(DIV_ROUND_UP(reg->size, 8));
2352 if (buf == NULL)
2353 return ERROR_FAIL;
2354 str_to_buf(CMD_ARGV[1], strlen(CMD_ARGV[1]), buf, reg->size, 0);
2356 reg->type->set(reg, buf);
2358 value = buf_to_str(reg->value, reg->size, 16);
2359 command_print(CMD_CTX, "%s (/%i): 0x%s", reg->name, (int)(reg->size), value);
2360 free(value);
2362 free(buf);
2364 return ERROR_OK;
2367 return ERROR_COMMAND_SYNTAX_ERROR;
2370 COMMAND_HANDLER(handle_poll_command)
2372 int retval = ERROR_OK;
2373 struct target *target = get_current_target(CMD_CTX);
2375 if (CMD_ARGC == 0) {
2376 command_print(CMD_CTX, "background polling: %s",
2377 jtag_poll_get_enabled() ? "on" : "off");
2378 command_print(CMD_CTX, "TAP: %s (%s)",
2379 target->tap->dotted_name,
2380 target->tap->enabled ? "enabled" : "disabled");
2381 if (!target->tap->enabled)
2382 return ERROR_OK;
2383 retval = target_poll(target);
2384 if (retval != ERROR_OK)
2385 return retval;
2386 retval = target_arch_state(target);
2387 if (retval != ERROR_OK)
2388 return retval;
2389 } else if (CMD_ARGC == 1) {
2390 bool enable;
2391 COMMAND_PARSE_ON_OFF(CMD_ARGV[0], enable);
2392 jtag_poll_set_enabled(enable);
2393 } else
2394 return ERROR_COMMAND_SYNTAX_ERROR;
2396 return retval;
2399 COMMAND_HANDLER(handle_wait_halt_command)
2401 if (CMD_ARGC > 1)
2402 return ERROR_COMMAND_SYNTAX_ERROR;
2404 unsigned ms = 5000;
2405 if (1 == CMD_ARGC) {
2406 int retval = parse_uint(CMD_ARGV[0], &ms);
2407 if (ERROR_OK != retval)
2408 return ERROR_COMMAND_SYNTAX_ERROR;
2409 /* convert seconds (given) to milliseconds (needed) */
2410 ms *= 1000;
2413 struct target *target = get_current_target(CMD_CTX);
2414 return target_wait_state(target, TARGET_HALTED, ms);
2417 /* wait for target state to change. The trick here is to have a low
2418 * latency for short waits and not to suck up all the CPU time
2419 * on longer waits.
2421 * After 500ms, keep_alive() is invoked
2423 int target_wait_state(struct target *target, enum target_state state, int ms)
2425 int retval;
2426 long long then = 0, cur;
2427 int once = 1;
2429 for (;;) {
2430 retval = target_poll(target);
2431 if (retval != ERROR_OK)
2432 return retval;
2433 if (target->state == state)
2434 break;
2435 cur = timeval_ms();
2436 if (once) {
2437 once = 0;
2438 then = timeval_ms();
2439 LOG_DEBUG("waiting for target %s...",
2440 Jim_Nvp_value2name_simple(nvp_target_state, state)->name);
2443 if (cur-then > 500)
2444 keep_alive();
2446 if ((cur-then) > ms) {
2447 LOG_ERROR("timed out while waiting for target %s",
2448 Jim_Nvp_value2name_simple(nvp_target_state, state)->name);
2449 return ERROR_FAIL;
2453 return ERROR_OK;
2456 COMMAND_HANDLER(handle_halt_command)
2458 LOG_DEBUG("-");
2460 struct target *target = get_current_target(CMD_CTX);
2461 int retval = target_halt(target);
2462 if (ERROR_OK != retval)
2463 return retval;
2465 if (CMD_ARGC == 1) {
2466 unsigned wait_local;
2467 retval = parse_uint(CMD_ARGV[0], &wait_local);
2468 if (ERROR_OK != retval)
2469 return ERROR_COMMAND_SYNTAX_ERROR;
2470 if (!wait_local)
2471 return ERROR_OK;
2474 return CALL_COMMAND_HANDLER(handle_wait_halt_command);
2477 COMMAND_HANDLER(handle_soft_reset_halt_command)
2479 struct target *target = get_current_target(CMD_CTX);
2481 LOG_USER("requesting target halt and executing a soft reset");
2483 target->type->soft_reset_halt(target);
2485 return ERROR_OK;
2488 COMMAND_HANDLER(handle_reset_command)
2490 if (CMD_ARGC > 1)
2491 return ERROR_COMMAND_SYNTAX_ERROR;
2493 enum target_reset_mode reset_mode = RESET_RUN;
2494 if (CMD_ARGC == 1) {
2495 const Jim_Nvp *n;
2496 n = Jim_Nvp_name2value_simple(nvp_reset_modes, CMD_ARGV[0]);
2497 if ((n->name == NULL) || (n->value == RESET_UNKNOWN))
2498 return ERROR_COMMAND_SYNTAX_ERROR;
2499 reset_mode = n->value;
2502 /* reset *all* targets */
2503 return target_process_reset(CMD_CTX, reset_mode);
2507 COMMAND_HANDLER(handle_resume_command)
2509 int current = 1;
2510 if (CMD_ARGC > 1)
2511 return ERROR_COMMAND_SYNTAX_ERROR;
2513 struct target *target = get_current_target(CMD_CTX);
2514 target_handle_event(target, TARGET_EVENT_OLD_pre_resume);
2516 /* with no CMD_ARGV, resume from current pc, addr = 0,
2517 * with one arguments, addr = CMD_ARGV[0],
2518 * handle breakpoints, not debugging */
2519 uint32_t addr = 0;
2520 if (CMD_ARGC == 1) {
2521 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], addr);
2522 current = 0;
2525 return target_resume(target, current, addr, 1, 0);
2528 COMMAND_HANDLER(handle_step_command)
2530 if (CMD_ARGC > 1)
2531 return ERROR_COMMAND_SYNTAX_ERROR;
2533 LOG_DEBUG("-");
2535 /* with no CMD_ARGV, step from current pc, addr = 0,
2536 * with one argument addr = CMD_ARGV[0],
2537 * handle breakpoints, debugging */
2538 uint32_t addr = 0;
2539 int current_pc = 1;
2540 if (CMD_ARGC == 1) {
2541 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], addr);
2542 current_pc = 0;
2545 struct target *target = get_current_target(CMD_CTX);
2547 return target->type->step(target, current_pc, addr, 1);
2550 static void handle_md_output(struct command_context *cmd_ctx,
2551 struct target *target, uint32_t address, unsigned size,
2552 unsigned count, const uint8_t *buffer)
2554 const unsigned line_bytecnt = 32;
2555 unsigned line_modulo = line_bytecnt / size;
2557 char output[line_bytecnt * 4 + 1];
2558 unsigned output_len = 0;
2560 const char *value_fmt;
2561 switch (size) {
2562 case 4:
2563 value_fmt = "%8.8x ";
2564 break;
2565 case 2:
2566 value_fmt = "%4.4x ";
2567 break;
2568 case 1:
2569 value_fmt = "%2.2x ";
2570 break;
2571 default:
2572 /* "can't happen", caller checked */
2573 LOG_ERROR("invalid memory read size: %u", size);
2574 return;
2577 for (unsigned i = 0; i < count; i++) {
2578 if (i % line_modulo == 0) {
2579 output_len += snprintf(output + output_len,
2580 sizeof(output) - output_len,
2581 "0x%8.8x: ",
2582 (unsigned)(address + (i*size)));
2585 uint32_t value = 0;
2586 const uint8_t *value_ptr = buffer + i * size;
2587 switch (size) {
2588 case 4:
2589 value = target_buffer_get_u32(target, value_ptr);
2590 break;
2591 case 2:
2592 value = target_buffer_get_u16(target, value_ptr);
2593 break;
2594 case 1:
2595 value = *value_ptr;
2597 output_len += snprintf(output + output_len,
2598 sizeof(output) - output_len,
2599 value_fmt, value);
2601 if ((i % line_modulo == line_modulo - 1) || (i == count - 1)) {
2602 command_print(cmd_ctx, "%s", output);
2603 output_len = 0;
2608 COMMAND_HANDLER(handle_md_command)
2610 if (CMD_ARGC < 1)
2611 return ERROR_COMMAND_SYNTAX_ERROR;
2613 unsigned size = 0;
2614 switch (CMD_NAME[2]) {
2615 case 'w':
2616 size = 4;
2617 break;
2618 case 'h':
2619 size = 2;
2620 break;
2621 case 'b':
2622 size = 1;
2623 break;
2624 default:
2625 return ERROR_COMMAND_SYNTAX_ERROR;
2628 bool physical = strcmp(CMD_ARGV[0], "phys") == 0;
2629 int (*fn)(struct target *target,
2630 uint32_t address, uint32_t size_value, uint32_t count, uint8_t *buffer);
2631 if (physical) {
2632 CMD_ARGC--;
2633 CMD_ARGV++;
2634 fn = target_read_phys_memory;
2635 } else
2636 fn = target_read_memory;
2637 if ((CMD_ARGC < 1) || (CMD_ARGC > 2))
2638 return ERROR_COMMAND_SYNTAX_ERROR;
2640 uint32_t address;
2641 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], address);
2643 unsigned count = 1;
2644 if (CMD_ARGC == 2)
2645 COMMAND_PARSE_NUMBER(uint, CMD_ARGV[1], count);
2647 uint8_t *buffer = calloc(count, size);
2649 struct target *target = get_current_target(CMD_CTX);
2650 int retval = fn(target, address, size, count, buffer);
2651 if (ERROR_OK == retval)
2652 handle_md_output(CMD_CTX, target, address, size, count, buffer);
2654 free(buffer);
2656 return retval;
2659 typedef int (*target_write_fn)(struct target *target,
2660 uint32_t address, uint32_t size, uint32_t count, const uint8_t *buffer);
2662 static int target_write_memory_fast(struct target *target,
2663 uint32_t address, uint32_t size, uint32_t count, const uint8_t *buffer)
2665 return target_write_buffer(target, address, size * count, buffer);
2668 static int target_fill_mem(struct target *target,
2669 uint32_t address,
2670 target_write_fn fn,
2671 unsigned data_size,
2672 /* value */
2673 uint32_t b,
2674 /* count */
2675 unsigned c)
2677 /* We have to write in reasonably large chunks to be able
2678 * to fill large memory areas with any sane speed */
2679 const unsigned chunk_size = 16384;
2680 uint8_t *target_buf = malloc(chunk_size * data_size);
2681 if (target_buf == NULL) {
2682 LOG_ERROR("Out of memory");
2683 return ERROR_FAIL;
2686 for (unsigned i = 0; i < chunk_size; i++) {
2687 switch (data_size) {
2688 case 4:
2689 target_buffer_set_u32(target, target_buf + i * data_size, b);
2690 break;
2691 case 2:
2692 target_buffer_set_u16(target, target_buf + i * data_size, b);
2693 break;
2694 case 1:
2695 target_buffer_set_u8(target, target_buf + i * data_size, b);
2696 break;
2697 default:
2698 exit(-1);
2702 int retval = ERROR_OK;
2704 for (unsigned x = 0; x < c; x += chunk_size) {
2705 unsigned current;
2706 current = c - x;
2707 if (current > chunk_size)
2708 current = chunk_size;
2709 retval = fn(target, address + x * data_size, data_size, current, target_buf);
2710 if (retval != ERROR_OK)
2711 break;
2712 /* avoid GDB timeouts */
2713 keep_alive();
2715 free(target_buf);
2717 return retval;
2721 COMMAND_HANDLER(handle_mw_command)
2723 if (CMD_ARGC < 2)
2724 return ERROR_COMMAND_SYNTAX_ERROR;
2725 bool physical = strcmp(CMD_ARGV[0], "phys") == 0;
2726 target_write_fn fn;
2727 if (physical) {
2728 CMD_ARGC--;
2729 CMD_ARGV++;
2730 fn = target_write_phys_memory;
2731 } else
2732 fn = target_write_memory_fast;
2733 if ((CMD_ARGC < 2) || (CMD_ARGC > 3))
2734 return ERROR_COMMAND_SYNTAX_ERROR;
2736 uint32_t address;
2737 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], address);
2739 uint32_t value;
2740 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], value);
2742 unsigned count = 1;
2743 if (CMD_ARGC == 3)
2744 COMMAND_PARSE_NUMBER(uint, CMD_ARGV[2], count);
2746 struct target *target = get_current_target(CMD_CTX);
2747 unsigned wordsize;
2748 switch (CMD_NAME[2]) {
2749 case 'w':
2750 wordsize = 4;
2751 break;
2752 case 'h':
2753 wordsize = 2;
2754 break;
2755 case 'b':
2756 wordsize = 1;
2757 break;
2758 default:
2759 return ERROR_COMMAND_SYNTAX_ERROR;
2762 return target_fill_mem(target, address, fn, wordsize, value, count);
2765 static COMMAND_HELPER(parse_load_image_command_CMD_ARGV, struct image *image,
2766 uint32_t *min_address, uint32_t *max_address)
2768 if (CMD_ARGC < 1 || CMD_ARGC > 5)
2769 return ERROR_COMMAND_SYNTAX_ERROR;
2771 /* a base address isn't always necessary,
2772 * default to 0x0 (i.e. don't relocate) */
2773 if (CMD_ARGC >= 2) {
2774 uint32_t addr;
2775 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], addr);
2776 image->base_address = addr;
2777 image->base_address_set = 1;
2778 } else
2779 image->base_address_set = 0;
2781 image->start_address_set = 0;
2783 if (CMD_ARGC >= 4)
2784 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[3], *min_address);
2785 if (CMD_ARGC == 5) {
2786 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[4], *max_address);
2787 /* use size (given) to find max (required) */
2788 *max_address += *min_address;
2791 if (*min_address > *max_address)
2792 return ERROR_COMMAND_SYNTAX_ERROR;
2794 return ERROR_OK;
2797 COMMAND_HANDLER(handle_load_image_command)
2799 uint8_t *buffer;
2800 size_t buf_cnt;
2801 uint32_t image_size;
2802 uint32_t min_address = 0;
2803 uint32_t max_address = 0xffffffff;
2804 int i;
2805 struct image image;
2807 int retval = CALL_COMMAND_HANDLER(parse_load_image_command_CMD_ARGV,
2808 &image, &min_address, &max_address);
2809 if (ERROR_OK != retval)
2810 return retval;
2812 struct target *target = get_current_target(CMD_CTX);
2814 struct duration bench;
2815 duration_start(&bench);
2817 if (image_open(&image, CMD_ARGV[0], (CMD_ARGC >= 3) ? CMD_ARGV[2] : NULL) != ERROR_OK)
2818 return ERROR_OK;
2820 image_size = 0x0;
2821 retval = ERROR_OK;
2822 for (i = 0; i < image.num_sections; i++) {
2823 buffer = malloc(image.sections[i].size);
2824 if (buffer == NULL) {
2825 command_print(CMD_CTX,
2826 "error allocating buffer for section (%d bytes)",
2827 (int)(image.sections[i].size));
2828 break;
2831 retval = image_read_section(&image, i, 0x0, image.sections[i].size, buffer, &buf_cnt);
2832 if (retval != ERROR_OK) {
2833 free(buffer);
2834 break;
2837 uint32_t offset = 0;
2838 uint32_t length = buf_cnt;
2840 /* DANGER!!! beware of unsigned comparision here!!! */
2842 if ((image.sections[i].base_address + buf_cnt >= min_address) &&
2843 (image.sections[i].base_address < max_address)) {
2845 if (image.sections[i].base_address < min_address) {
2846 /* clip addresses below */
2847 offset += min_address-image.sections[i].base_address;
2848 length -= offset;
2851 if (image.sections[i].base_address + buf_cnt > max_address)
2852 length -= (image.sections[i].base_address + buf_cnt)-max_address;
2854 retval = target_write_buffer(target,
2855 image.sections[i].base_address + offset, length, buffer + offset);
2856 if (retval != ERROR_OK) {
2857 free(buffer);
2858 break;
2860 image_size += length;
2861 command_print(CMD_CTX, "%u bytes written at address 0x%8.8" PRIx32 "",
2862 (unsigned int)length,
2863 image.sections[i].base_address + offset);
2866 free(buffer);
2869 if ((ERROR_OK == retval) && (duration_measure(&bench) == ERROR_OK)) {
2870 command_print(CMD_CTX, "downloaded %" PRIu32 " bytes "
2871 "in %fs (%0.3f KiB/s)", image_size,
2872 duration_elapsed(&bench), duration_kbps(&bench, image_size));
2875 image_close(&image);
2877 return retval;
2881 COMMAND_HANDLER(handle_dump_image_command)
2883 struct fileio fileio;
2884 uint8_t *buffer;
2885 int retval, retvaltemp;
2886 uint32_t address, size;
2887 struct duration bench;
2888 struct target *target = get_current_target(CMD_CTX);
2890 if (CMD_ARGC != 3)
2891 return ERROR_COMMAND_SYNTAX_ERROR;
2893 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], address);
2894 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[2], size);
2896 uint32_t buf_size = (size > 4096) ? 4096 : size;
2897 buffer = malloc(buf_size);
2898 if (!buffer)
2899 return ERROR_FAIL;
2901 retval = fileio_open(&fileio, CMD_ARGV[0], FILEIO_WRITE, FILEIO_BINARY);
2902 if (retval != ERROR_OK) {
2903 free(buffer);
2904 return retval;
2907 duration_start(&bench);
2909 while (size > 0) {
2910 size_t size_written;
2911 uint32_t this_run_size = (size > buf_size) ? buf_size : size;
2912 retval = target_read_buffer(target, address, this_run_size, buffer);
2913 if (retval != ERROR_OK)
2914 break;
2916 retval = fileio_write(&fileio, this_run_size, buffer, &size_written);
2917 if (retval != ERROR_OK)
2918 break;
2920 size -= this_run_size;
2921 address += this_run_size;
2924 free(buffer);
2926 if ((ERROR_OK == retval) && (duration_measure(&bench) == ERROR_OK)) {
2927 int filesize;
2928 retval = fileio_size(&fileio, &filesize);
2929 if (retval != ERROR_OK)
2930 return retval;
2931 command_print(CMD_CTX,
2932 "dumped %ld bytes in %fs (%0.3f KiB/s)", (long)filesize,
2933 duration_elapsed(&bench), duration_kbps(&bench, filesize));
2936 retvaltemp = fileio_close(&fileio);
2937 if (retvaltemp != ERROR_OK)
2938 return retvaltemp;
2940 return retval;
2943 static COMMAND_HELPER(handle_verify_image_command_internal, int verify)
2945 uint8_t *buffer;
2946 size_t buf_cnt;
2947 uint32_t image_size;
2948 int i;
2949 int retval;
2950 uint32_t checksum = 0;
2951 uint32_t mem_checksum = 0;
2953 struct image image;
2955 struct target *target = get_current_target(CMD_CTX);
2957 if (CMD_ARGC < 1)
2958 return ERROR_COMMAND_SYNTAX_ERROR;
2960 if (!target) {
2961 LOG_ERROR("no target selected");
2962 return ERROR_FAIL;
2965 struct duration bench;
2966 duration_start(&bench);
2968 if (CMD_ARGC >= 2) {
2969 uint32_t addr;
2970 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], addr);
2971 image.base_address = addr;
2972 image.base_address_set = 1;
2973 } else {
2974 image.base_address_set = 0;
2975 image.base_address = 0x0;
2978 image.start_address_set = 0;
2980 retval = image_open(&image, CMD_ARGV[0], (CMD_ARGC == 3) ? CMD_ARGV[2] : NULL);
2981 if (retval != ERROR_OK)
2982 return retval;
2984 image_size = 0x0;
2985 int diffs = 0;
2986 retval = ERROR_OK;
2987 for (i = 0; i < image.num_sections; i++) {
2988 buffer = malloc(image.sections[i].size);
2989 if (buffer == NULL) {
2990 command_print(CMD_CTX,
2991 "error allocating buffer for section (%d bytes)",
2992 (int)(image.sections[i].size));
2993 break;
2995 retval = image_read_section(&image, i, 0x0, image.sections[i].size, buffer, &buf_cnt);
2996 if (retval != ERROR_OK) {
2997 free(buffer);
2998 break;
3001 if (verify) {
3002 /* calculate checksum of image */
3003 retval = image_calculate_checksum(buffer, buf_cnt, &checksum);
3004 if (retval != ERROR_OK) {
3005 free(buffer);
3006 break;
3009 retval = target_checksum_memory(target, image.sections[i].base_address, buf_cnt, &mem_checksum);
3010 if (retval != ERROR_OK) {
3011 free(buffer);
3012 break;
3015 if (checksum != mem_checksum) {
3016 /* failed crc checksum, fall back to a binary compare */
3017 uint8_t *data;
3019 if (diffs == 0)
3020 LOG_ERROR("checksum mismatch - attempting binary compare");
3022 data = (uint8_t *)malloc(buf_cnt);
3024 /* Can we use 32bit word accesses? */
3025 int size = 1;
3026 int count = buf_cnt;
3027 if ((count % 4) == 0) {
3028 size *= 4;
3029 count /= 4;
3031 retval = target_read_memory(target, image.sections[i].base_address, size, count, data);
3032 if (retval == ERROR_OK) {
3033 uint32_t t;
3034 for (t = 0; t < buf_cnt; t++) {
3035 if (data[t] != buffer[t]) {
3036 command_print(CMD_CTX,
3037 "diff %d address 0x%08x. Was 0x%02x instead of 0x%02x",
3038 diffs,
3039 (unsigned)(t + image.sections[i].base_address),
3040 data[t],
3041 buffer[t]);
3042 if (diffs++ >= 127) {
3043 command_print(CMD_CTX, "More than 128 errors, the rest are not printed.");
3044 free(data);
3045 free(buffer);
3046 goto done;
3049 keep_alive();
3052 free(data);
3054 } else {
3055 command_print(CMD_CTX, "address 0x%08" PRIx32 " length 0x%08zx",
3056 image.sections[i].base_address,
3057 buf_cnt);
3060 free(buffer);
3061 image_size += buf_cnt;
3063 if (diffs > 0)
3064 command_print(CMD_CTX, "No more differences found.");
3065 done:
3066 if (diffs > 0)
3067 retval = ERROR_FAIL;
3068 if ((ERROR_OK == retval) && (duration_measure(&bench) == ERROR_OK)) {
3069 command_print(CMD_CTX, "verified %" PRIu32 " bytes "
3070 "in %fs (%0.3f KiB/s)", image_size,
3071 duration_elapsed(&bench), duration_kbps(&bench, image_size));
3074 image_close(&image);
3076 return retval;
3079 COMMAND_HANDLER(handle_verify_image_command)
3081 return CALL_COMMAND_HANDLER(handle_verify_image_command_internal, 1);
3084 COMMAND_HANDLER(handle_test_image_command)
3086 return CALL_COMMAND_HANDLER(handle_verify_image_command_internal, 0);
3089 static int handle_bp_command_list(struct command_context *cmd_ctx)
3091 struct target *target = get_current_target(cmd_ctx);
3092 struct breakpoint *breakpoint = target->breakpoints;
3093 while (breakpoint) {
3094 if (breakpoint->type == BKPT_SOFT) {
3095 char *buf = buf_to_str(breakpoint->orig_instr,
3096 breakpoint->length, 16);
3097 command_print(cmd_ctx, "IVA breakpoint: 0x%8.8" PRIx32 ", 0x%x, %i, 0x%s",
3098 breakpoint->address,
3099 breakpoint->length,
3100 breakpoint->set, buf);
3101 free(buf);
3102 } else {
3103 if ((breakpoint->address == 0) && (breakpoint->asid != 0))
3104 command_print(cmd_ctx, "Context breakpoint: 0x%8.8" PRIx32 ", 0x%x, %i",
3105 breakpoint->asid,
3106 breakpoint->length, breakpoint->set);
3107 else if ((breakpoint->address != 0) && (breakpoint->asid != 0)) {
3108 command_print(cmd_ctx, "Hybrid breakpoint(IVA): 0x%8.8" PRIx32 ", 0x%x, %i",
3109 breakpoint->address,
3110 breakpoint->length, breakpoint->set);
3111 command_print(cmd_ctx, "\t|--->linked with ContextID: 0x%8.8" PRIx32,
3112 breakpoint->asid);
3113 } else
3114 command_print(cmd_ctx, "Breakpoint(IVA): 0x%8.8" PRIx32 ", 0x%x, %i",
3115 breakpoint->address,
3116 breakpoint->length, breakpoint->set);
3119 breakpoint = breakpoint->next;
3121 return ERROR_OK;
3124 static int handle_bp_command_set(struct command_context *cmd_ctx,
3125 uint32_t addr, uint32_t asid, uint32_t length, int hw)
3127 struct target *target = get_current_target(cmd_ctx);
3129 if (asid == 0) {
3130 int retval = breakpoint_add(target, addr, length, hw);
3131 if (ERROR_OK == retval)
3132 command_print(cmd_ctx, "breakpoint set at 0x%8.8" PRIx32 "", addr);
3133 else {
3134 LOG_ERROR("Failure setting breakpoint, the same address(IVA) is already used");
3135 return retval;
3137 } else if (addr == 0) {
3138 int retval = context_breakpoint_add(target, asid, length, hw);
3139 if (ERROR_OK == retval)
3140 command_print(cmd_ctx, "Context breakpoint set at 0x%8.8" PRIx32 "", asid);
3141 else {
3142 LOG_ERROR("Failure setting breakpoint, the same address(CONTEXTID) is already used");
3143 return retval;
3145 } else {
3146 int retval = hybrid_breakpoint_add(target, addr, asid, length, hw);
3147 if (ERROR_OK == retval)
3148 command_print(cmd_ctx, "Hybrid breakpoint set at 0x%8.8" PRIx32 "", asid);
3149 else {
3150 LOG_ERROR("Failure setting breakpoint, the same address is already used");
3151 return retval;
3154 return ERROR_OK;
3157 COMMAND_HANDLER(handle_bp_command)
3159 uint32_t addr;
3160 uint32_t asid;
3161 uint32_t length;
3162 int hw = BKPT_SOFT;
3164 switch (CMD_ARGC) {
3165 case 0:
3166 return handle_bp_command_list(CMD_CTX);
3168 case 2:
3169 asid = 0;
3170 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], addr);
3171 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], length);
3172 return handle_bp_command_set(CMD_CTX, addr, asid, length, hw);
3174 case 3:
3175 if (strcmp(CMD_ARGV[2], "hw") == 0) {
3176 hw = BKPT_HARD;
3177 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], addr);
3179 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], length);
3181 asid = 0;
3182 return handle_bp_command_set(CMD_CTX, addr, asid, length, hw);
3183 } else if (strcmp(CMD_ARGV[2], "hw_ctx") == 0) {
3184 hw = BKPT_HARD;
3185 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], asid);
3186 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], length);
3187 addr = 0;
3188 return handle_bp_command_set(CMD_CTX, addr, asid, length, hw);
3191 case 4:
3192 hw = BKPT_HARD;
3193 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], addr);
3194 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], asid);
3195 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[2], length);
3196 return handle_bp_command_set(CMD_CTX, addr, asid, length, hw);
3198 default:
3199 return ERROR_COMMAND_SYNTAX_ERROR;
3203 COMMAND_HANDLER(handle_rbp_command)
3205 if (CMD_ARGC != 1)
3206 return ERROR_COMMAND_SYNTAX_ERROR;
3208 uint32_t addr;
3209 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], addr);
3211 struct target *target = get_current_target(CMD_CTX);
3212 breakpoint_remove(target, addr);
3214 return ERROR_OK;
3217 COMMAND_HANDLER(handle_wp_command)
3219 struct target *target = get_current_target(CMD_CTX);
3221 if (CMD_ARGC == 0) {
3222 struct watchpoint *watchpoint = target->watchpoints;
3224 while (watchpoint) {
3225 command_print(CMD_CTX, "address: 0x%8.8" PRIx32
3226 ", len: 0x%8.8" PRIx32
3227 ", r/w/a: %i, value: 0x%8.8" PRIx32
3228 ", mask: 0x%8.8" PRIx32,
3229 watchpoint->address,
3230 watchpoint->length,
3231 (int)watchpoint->rw,
3232 watchpoint->value,
3233 watchpoint->mask);
3234 watchpoint = watchpoint->next;
3236 return ERROR_OK;
3239 enum watchpoint_rw type = WPT_ACCESS;
3240 uint32_t addr = 0;
3241 uint32_t length = 0;
3242 uint32_t data_value = 0x0;
3243 uint32_t data_mask = 0xffffffff;
3245 switch (CMD_ARGC) {
3246 case 5:
3247 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[4], data_mask);
3248 /* fall through */
3249 case 4:
3250 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[3], data_value);
3251 /* fall through */
3252 case 3:
3253 switch (CMD_ARGV[2][0]) {
3254 case 'r':
3255 type = WPT_READ;
3256 break;
3257 case 'w':
3258 type = WPT_WRITE;
3259 break;
3260 case 'a':
3261 type = WPT_ACCESS;
3262 break;
3263 default:
3264 LOG_ERROR("invalid watchpoint mode ('%c')", CMD_ARGV[2][0]);
3265 return ERROR_COMMAND_SYNTAX_ERROR;
3267 /* fall through */
3268 case 2:
3269 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], length);
3270 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], addr);
3271 break;
3273 default:
3274 return ERROR_COMMAND_SYNTAX_ERROR;
3277 int retval = watchpoint_add(target, addr, length, type,
3278 data_value, data_mask);
3279 if (ERROR_OK != retval)
3280 LOG_ERROR("Failure setting watchpoints");
3282 return retval;
3285 COMMAND_HANDLER(handle_rwp_command)
3287 if (CMD_ARGC != 1)
3288 return ERROR_COMMAND_SYNTAX_ERROR;
3290 uint32_t addr;
3291 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], addr);
3293 struct target *target = get_current_target(CMD_CTX);
3294 watchpoint_remove(target, addr);
3296 return ERROR_OK;
3300 * Translate a virtual address to a physical address.
3302 * The low-level target implementation must have logged a detailed error
3303 * which is forwarded to telnet/GDB session.
3305 COMMAND_HANDLER(handle_virt2phys_command)
3307 if (CMD_ARGC != 1)
3308 return ERROR_COMMAND_SYNTAX_ERROR;
3310 uint32_t va;
3311 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], va);
3312 uint32_t pa;
3314 struct target *target = get_current_target(CMD_CTX);
3315 int retval = target->type->virt2phys(target, va, &pa);
3316 if (retval == ERROR_OK)
3317 command_print(CMD_CTX, "Physical address 0x%08" PRIx32 "", pa);
3319 return retval;
3322 static void writeData(FILE *f, const void *data, size_t len)
3324 size_t written = fwrite(data, 1, len, f);
3325 if (written != len)
3326 LOG_ERROR("failed to write %zu bytes: %s", len, strerror(errno));
3329 static void writeLong(FILE *f, int l)
3331 int i;
3332 for (i = 0; i < 4; i++) {
3333 char c = (l >> (i*8))&0xff;
3334 writeData(f, &c, 1);
3339 static void writeString(FILE *f, char *s)
3341 writeData(f, s, strlen(s));
3344 /* Dump a gmon.out histogram file. */
3345 static void writeGmon(uint32_t *samples, uint32_t sampleNum, const char *filename)
3347 uint32_t i;
3348 FILE *f = fopen(filename, "w");
3349 if (f == NULL)
3350 return;
3351 writeString(f, "gmon");
3352 writeLong(f, 0x00000001); /* Version */
3353 writeLong(f, 0); /* padding */
3354 writeLong(f, 0); /* padding */
3355 writeLong(f, 0); /* padding */
3357 uint8_t zero = 0; /* GMON_TAG_TIME_HIST */
3358 writeData(f, &zero, 1);
3360 /* figure out bucket size */
3361 uint32_t min = samples[0];
3362 uint32_t max = samples[0];
3363 for (i = 0; i < sampleNum; i++) {
3364 if (min > samples[i])
3365 min = samples[i];
3366 if (max < samples[i])
3367 max = samples[i];
3370 int addressSpace = (max - min + 1);
3371 assert(addressSpace >= 2);
3373 static const uint32_t maxBuckets = 16 * 1024; /* maximum buckets. */
3374 uint32_t length = addressSpace;
3375 if (length > maxBuckets)
3376 length = maxBuckets;
3377 int *buckets = malloc(sizeof(int)*length);
3378 if (buckets == NULL) {
3379 fclose(f);
3380 return;
3382 memset(buckets, 0, sizeof(int) * length);
3383 for (i = 0; i < sampleNum; i++) {
3384 uint32_t address = samples[i];
3385 long long a = address - min;
3386 long long b = length - 1;
3387 long long c = addressSpace - 1;
3388 int index_t = (a * b) / c; /* danger!!!! int32 overflows */
3389 buckets[index_t]++;
3392 /* append binary memory gmon.out &profile_hist_hdr ((char*)&profile_hist_hdr + sizeof(struct gmon_hist_hdr)) */
3393 writeLong(f, min); /* low_pc */
3394 writeLong(f, max); /* high_pc */
3395 writeLong(f, length); /* # of samples */
3396 writeLong(f, 100); /* KLUDGE! We lie, ca. 100Hz best case. */
3397 writeString(f, "seconds");
3398 for (i = 0; i < (15-strlen("seconds")); i++)
3399 writeData(f, &zero, 1);
3400 writeString(f, "s");
3402 /*append binary memory gmon.out profile_hist_data (profile_hist_data + profile_hist_hdr.hist_size) */
3404 char *data = malloc(2 * length);
3405 if (data != NULL) {
3406 for (i = 0; i < length; i++) {
3407 int val;
3408 val = buckets[i];
3409 if (val > 65535)
3410 val = 65535;
3411 data[i * 2] = val&0xff;
3412 data[i * 2 + 1] = (val >> 8) & 0xff;
3414 free(buckets);
3415 writeData(f, data, length * 2);
3416 free(data);
3417 } else
3418 free(buckets);
3420 fclose(f);
3423 /* profiling samples the CPU PC as quickly as OpenOCD is able,
3424 * which will be used as a random sampling of PC */
3425 COMMAND_HANDLER(handle_profile_command)
3427 struct target *target = get_current_target(CMD_CTX);
3428 struct timeval timeout, now;
3430 gettimeofday(&timeout, NULL);
3431 if (CMD_ARGC != 2)
3432 return ERROR_COMMAND_SYNTAX_ERROR;
3433 unsigned offset;
3434 COMMAND_PARSE_NUMBER(uint, CMD_ARGV[0], offset);
3436 timeval_add_time(&timeout, offset, 0);
3439 * @todo: Some cores let us sample the PC without the
3440 * annoying halt/resume step; for example, ARMv7 PCSR.
3441 * Provide a way to use that more efficient mechanism.
3444 command_print(CMD_CTX, "Starting profiling. Halting and resuming the target as often as we can...");
3446 static const int maxSample = 10000;
3447 uint32_t *samples = malloc(sizeof(uint32_t)*maxSample);
3448 if (samples == NULL)
3449 return ERROR_OK;
3451 int numSamples = 0;
3452 /* hopefully it is safe to cache! We want to stop/restart as quickly as possible. */
3453 struct reg *reg = register_get_by_name(target->reg_cache, "pc", 1);
3455 int retval = ERROR_OK;
3456 for (;;) {
3457 target_poll(target);
3458 if (target->state == TARGET_HALTED) {
3459 uint32_t t = *((uint32_t *)reg->value);
3460 samples[numSamples++] = t;
3461 /* current pc, addr = 0, do not handle breakpoints, not debugging */
3462 retval = target_resume(target, 1, 0, 0, 0);
3463 target_poll(target);
3464 alive_sleep(10); /* sleep 10ms, i.e. <100 samples/second. */
3465 } else if (target->state == TARGET_RUNNING) {
3466 /* We want to quickly sample the PC. */
3467 retval = target_halt(target);
3468 if (retval != ERROR_OK) {
3469 free(samples);
3470 return retval;
3472 } else {
3473 command_print(CMD_CTX, "Target not halted or running");
3474 retval = ERROR_OK;
3475 break;
3477 if (retval != ERROR_OK)
3478 break;
3480 gettimeofday(&now, NULL);
3481 if ((numSamples >= maxSample) || ((now.tv_sec >= timeout.tv_sec)
3482 && (now.tv_usec >= timeout.tv_usec))) {
3483 command_print(CMD_CTX, "Profiling completed. %d samples.", numSamples);
3484 retval = target_poll(target);
3485 if (retval != ERROR_OK) {
3486 free(samples);
3487 return retval;
3489 if (target->state == TARGET_HALTED) {
3490 /* current pc, addr = 0, do not handle
3491 * breakpoints, not debugging */
3492 target_resume(target, 1, 0, 0, 0);
3494 retval = target_poll(target);
3495 if (retval != ERROR_OK) {
3496 free(samples);
3497 return retval;
3499 writeGmon(samples, numSamples, CMD_ARGV[1]);
3500 command_print(CMD_CTX, "Wrote %s", CMD_ARGV[1]);
3501 break;
3504 free(samples);
3506 return retval;
3509 static int new_int_array_element(Jim_Interp *interp, const char *varname, int idx, uint32_t val)
3511 char *namebuf;
3512 Jim_Obj *nameObjPtr, *valObjPtr;
3513 int result;
3515 namebuf = alloc_printf("%s(%d)", varname, idx);
3516 if (!namebuf)
3517 return JIM_ERR;
3519 nameObjPtr = Jim_NewStringObj(interp, namebuf, -1);
3520 valObjPtr = Jim_NewIntObj(interp, val);
3521 if (!nameObjPtr || !valObjPtr) {
3522 free(namebuf);
3523 return JIM_ERR;
3526 Jim_IncrRefCount(nameObjPtr);
3527 Jim_IncrRefCount(valObjPtr);
3528 result = Jim_SetVariable(interp, nameObjPtr, valObjPtr);
3529 Jim_DecrRefCount(interp, nameObjPtr);
3530 Jim_DecrRefCount(interp, valObjPtr);
3531 free(namebuf);
3532 /* printf("%s(%d) <= 0%08x\n", varname, idx, val); */
3533 return result;
3536 static int jim_mem2array(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
3538 struct command_context *context;
3539 struct target *target;
3541 context = current_command_context(interp);
3542 assert(context != NULL);
3544 target = get_current_target(context);
3545 if (target == NULL) {
3546 LOG_ERROR("mem2array: no current target");
3547 return JIM_ERR;
3550 return target_mem2array(interp, target, argc - 1, argv + 1);
3553 static int target_mem2array(Jim_Interp *interp, struct target *target, int argc, Jim_Obj *const *argv)
3555 long l;
3556 uint32_t width;
3557 int len;
3558 uint32_t addr;
3559 uint32_t count;
3560 uint32_t v;
3561 const char *varname;
3562 int n, e, retval;
3563 uint32_t i;
3565 /* argv[1] = name of array to receive the data
3566 * argv[2] = desired width
3567 * argv[3] = memory address
3568 * argv[4] = count of times to read
3570 if (argc != 4) {
3571 Jim_WrongNumArgs(interp, 1, argv, "varname width addr nelems");
3572 return JIM_ERR;
3574 varname = Jim_GetString(argv[0], &len);
3575 /* given "foo" get space for worse case "foo(%d)" .. add 20 */
3577 e = Jim_GetLong(interp, argv[1], &l);
3578 width = l;
3579 if (e != JIM_OK)
3580 return e;
3582 e = Jim_GetLong(interp, argv[2], &l);
3583 addr = l;
3584 if (e != JIM_OK)
3585 return e;
3586 e = Jim_GetLong(interp, argv[3], &l);
3587 len = l;
3588 if (e != JIM_OK)
3589 return e;
3590 switch (width) {
3591 case 8:
3592 width = 1;
3593 break;
3594 case 16:
3595 width = 2;
3596 break;
3597 case 32:
3598 width = 4;
3599 break;
3600 default:
3601 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3602 Jim_AppendStrings(interp, Jim_GetResult(interp), "Invalid width param, must be 8/16/32", NULL);
3603 return JIM_ERR;
3605 if (len == 0) {
3606 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3607 Jim_AppendStrings(interp, Jim_GetResult(interp), "mem2array: zero width read?", NULL);
3608 return JIM_ERR;
3610 if ((addr + (len * width)) < addr) {
3611 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3612 Jim_AppendStrings(interp, Jim_GetResult(interp), "mem2array: addr + len - wraps to zero?", NULL);
3613 return JIM_ERR;
3615 /* absurd transfer size? */
3616 if (len > 65536) {
3617 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3618 Jim_AppendStrings(interp, Jim_GetResult(interp), "mem2array: absurd > 64K item request", NULL);
3619 return JIM_ERR;
3622 if ((width == 1) ||
3623 ((width == 2) && ((addr & 1) == 0)) ||
3624 ((width == 4) && ((addr & 3) == 0))) {
3625 /* all is well */
3626 } else {
3627 char buf[100];
3628 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3629 sprintf(buf, "mem2array address: 0x%08" PRIx32 " is not aligned for %" PRId32 " byte reads",
3630 addr,
3631 width);
3632 Jim_AppendStrings(interp, Jim_GetResult(interp), buf , NULL);
3633 return JIM_ERR;
3636 /* Transfer loop */
3638 /* index counter */
3639 n = 0;
3641 size_t buffersize = 4096;
3642 uint8_t *buffer = malloc(buffersize);
3643 if (buffer == NULL)
3644 return JIM_ERR;
3646 /* assume ok */
3647 e = JIM_OK;
3648 while (len) {
3649 /* Slurp... in buffer size chunks */
3651 count = len; /* in objects.. */
3652 if (count > (buffersize / width))
3653 count = (buffersize / width);
3655 retval = target_read_memory(target, addr, width, count, buffer);
3656 if (retval != ERROR_OK) {
3657 /* BOO !*/
3658 LOG_ERROR("mem2array: Read @ 0x%08x, w=%d, cnt=%d, failed",
3659 (unsigned int)addr,
3660 (int)width,
3661 (int)count);
3662 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3663 Jim_AppendStrings(interp, Jim_GetResult(interp), "mem2array: cannot read memory", NULL);
3664 e = JIM_ERR;
3665 break;
3666 } else {
3667 v = 0; /* shut up gcc */
3668 for (i = 0; i < count ; i++, n++) {
3669 switch (width) {
3670 case 4:
3671 v = target_buffer_get_u32(target, &buffer[i*width]);
3672 break;
3673 case 2:
3674 v = target_buffer_get_u16(target, &buffer[i*width]);
3675 break;
3676 case 1:
3677 v = buffer[i] & 0x0ff;
3678 break;
3680 new_int_array_element(interp, varname, n, v);
3682 len -= count;
3686 free(buffer);
3688 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3690 return e;
3693 static int get_int_array_element(Jim_Interp *interp, const char *varname, int idx, uint32_t *val)
3695 char *namebuf;
3696 Jim_Obj *nameObjPtr, *valObjPtr;
3697 int result;
3698 long l;
3700 namebuf = alloc_printf("%s(%d)", varname, idx);
3701 if (!namebuf)
3702 return JIM_ERR;
3704 nameObjPtr = Jim_NewStringObj(interp, namebuf, -1);
3705 if (!nameObjPtr) {
3706 free(namebuf);
3707 return JIM_ERR;
3710 Jim_IncrRefCount(nameObjPtr);
3711 valObjPtr = Jim_GetVariable(interp, nameObjPtr, JIM_ERRMSG);
3712 Jim_DecrRefCount(interp, nameObjPtr);
3713 free(namebuf);
3714 if (valObjPtr == NULL)
3715 return JIM_ERR;
3717 result = Jim_GetLong(interp, valObjPtr, &l);
3718 /* printf("%s(%d) => 0%08x\n", varname, idx, val); */
3719 *val = l;
3720 return result;
3723 static int jim_array2mem(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
3725 struct command_context *context;
3726 struct target *target;
3728 context = current_command_context(interp);
3729 assert(context != NULL);
3731 target = get_current_target(context);
3732 if (target == NULL) {
3733 LOG_ERROR("array2mem: no current target");
3734 return JIM_ERR;
3737 return target_array2mem(interp, target, argc-1, argv + 1);
3740 static int target_array2mem(Jim_Interp *interp, struct target *target,
3741 int argc, Jim_Obj *const *argv)
3743 long l;
3744 uint32_t width;
3745 int len;
3746 uint32_t addr;
3747 uint32_t count;
3748 uint32_t v;
3749 const char *varname;
3750 int n, e, retval;
3751 uint32_t i;
3753 /* argv[1] = name of array to get the data
3754 * argv[2] = desired width
3755 * argv[3] = memory address
3756 * argv[4] = count to write
3758 if (argc != 4) {
3759 Jim_WrongNumArgs(interp, 0, argv, "varname width addr nelems");
3760 return JIM_ERR;
3762 varname = Jim_GetString(argv[0], &len);
3763 /* given "foo" get space for worse case "foo(%d)" .. add 20 */
3765 e = Jim_GetLong(interp, argv[1], &l);
3766 width = l;
3767 if (e != JIM_OK)
3768 return e;
3770 e = Jim_GetLong(interp, argv[2], &l);
3771 addr = l;
3772 if (e != JIM_OK)
3773 return e;
3774 e = Jim_GetLong(interp, argv[3], &l);
3775 len = l;
3776 if (e != JIM_OK)
3777 return e;
3778 switch (width) {
3779 case 8:
3780 width = 1;
3781 break;
3782 case 16:
3783 width = 2;
3784 break;
3785 case 32:
3786 width = 4;
3787 break;
3788 default:
3789 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3790 Jim_AppendStrings(interp, Jim_GetResult(interp),
3791 "Invalid width param, must be 8/16/32", NULL);
3792 return JIM_ERR;
3794 if (len == 0) {
3795 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3796 Jim_AppendStrings(interp, Jim_GetResult(interp),
3797 "array2mem: zero width read?", NULL);
3798 return JIM_ERR;
3800 if ((addr + (len * width)) < addr) {
3801 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3802 Jim_AppendStrings(interp, Jim_GetResult(interp),
3803 "array2mem: addr + len - wraps to zero?", NULL);
3804 return JIM_ERR;
3806 /* absurd transfer size? */
3807 if (len > 65536) {
3808 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3809 Jim_AppendStrings(interp, Jim_GetResult(interp),
3810 "array2mem: absurd > 64K item request", NULL);
3811 return JIM_ERR;
3814 if ((width == 1) ||
3815 ((width == 2) && ((addr & 1) == 0)) ||
3816 ((width == 4) && ((addr & 3) == 0))) {
3817 /* all is well */
3818 } else {
3819 char buf[100];
3820 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3821 sprintf(buf, "array2mem address: 0x%08x is not aligned for %d byte reads",
3822 (unsigned int)addr,
3823 (int)width);
3824 Jim_AppendStrings(interp, Jim_GetResult(interp), buf , NULL);
3825 return JIM_ERR;
3828 /* Transfer loop */
3830 /* index counter */
3831 n = 0;
3832 /* assume ok */
3833 e = JIM_OK;
3835 size_t buffersize = 4096;
3836 uint8_t *buffer = malloc(buffersize);
3837 if (buffer == NULL)
3838 return JIM_ERR;
3840 while (len) {
3841 /* Slurp... in buffer size chunks */
3843 count = len; /* in objects.. */
3844 if (count > (buffersize / width))
3845 count = (buffersize / width);
3847 v = 0; /* shut up gcc */
3848 for (i = 0; i < count; i++, n++) {
3849 get_int_array_element(interp, varname, n, &v);
3850 switch (width) {
3851 case 4:
3852 target_buffer_set_u32(target, &buffer[i * width], v);
3853 break;
3854 case 2:
3855 target_buffer_set_u16(target, &buffer[i * width], v);
3856 break;
3857 case 1:
3858 buffer[i] = v & 0x0ff;
3859 break;
3862 len -= count;
3864 retval = target_write_memory(target, addr, width, count, buffer);
3865 if (retval != ERROR_OK) {
3866 /* BOO !*/
3867 LOG_ERROR("array2mem: Write @ 0x%08x, w=%d, cnt=%d, failed",
3868 (unsigned int)addr,
3869 (int)width,
3870 (int)count);
3871 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3872 Jim_AppendStrings(interp, Jim_GetResult(interp), "array2mem: cannot read memory", NULL);
3873 e = JIM_ERR;
3874 break;
3878 free(buffer);
3880 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3882 return e;
3885 /* FIX? should we propagate errors here rather than printing them
3886 * and continuing?
3888 void target_handle_event(struct target *target, enum target_event e)
3890 struct target_event_action *teap;
3892 for (teap = target->event_action; teap != NULL; teap = teap->next) {
3893 if (teap->event == e) {
3894 LOG_DEBUG("target: (%d) %s (%s) event: %d (%s) action: %s",
3895 target->target_number,
3896 target_name(target),
3897 target_type_name(target),
3899 Jim_Nvp_value2name_simple(nvp_target_event, e)->name,
3900 Jim_GetString(teap->body, NULL));
3901 if (Jim_EvalObj(teap->interp, teap->body) != JIM_OK) {
3902 Jim_MakeErrorMessage(teap->interp);
3903 command_print(NULL, "%s\n", Jim_GetString(Jim_GetResult(teap->interp), NULL));
3910 * Returns true only if the target has a handler for the specified event.
3912 bool target_has_event_action(struct target *target, enum target_event event)
3914 struct target_event_action *teap;
3916 for (teap = target->event_action; teap != NULL; teap = teap->next) {
3917 if (teap->event == event)
3918 return true;
3920 return false;
3923 enum target_cfg_param {
3924 TCFG_TYPE,
3925 TCFG_EVENT,
3926 TCFG_WORK_AREA_VIRT,
3927 TCFG_WORK_AREA_PHYS,
3928 TCFG_WORK_AREA_SIZE,
3929 TCFG_WORK_AREA_BACKUP,
3930 TCFG_ENDIAN,
3931 TCFG_VARIANT,
3932 TCFG_COREID,
3933 TCFG_CHAIN_POSITION,
3934 TCFG_DBGBASE,
3935 TCFG_RTOS,
3938 static Jim_Nvp nvp_config_opts[] = {
3939 { .name = "-type", .value = TCFG_TYPE },
3940 { .name = "-event", .value = TCFG_EVENT },
3941 { .name = "-work-area-virt", .value = TCFG_WORK_AREA_VIRT },
3942 { .name = "-work-area-phys", .value = TCFG_WORK_AREA_PHYS },
3943 { .name = "-work-area-size", .value = TCFG_WORK_AREA_SIZE },
3944 { .name = "-work-area-backup", .value = TCFG_WORK_AREA_BACKUP },
3945 { .name = "-endian" , .value = TCFG_ENDIAN },
3946 { .name = "-variant", .value = TCFG_VARIANT },
3947 { .name = "-coreid", .value = TCFG_COREID },
3948 { .name = "-chain-position", .value = TCFG_CHAIN_POSITION },
3949 { .name = "-dbgbase", .value = TCFG_DBGBASE },
3950 { .name = "-rtos", .value = TCFG_RTOS },
3951 { .name = NULL, .value = -1 }
3954 static int target_configure(Jim_GetOptInfo *goi, struct target *target)
3956 Jim_Nvp *n;
3957 Jim_Obj *o;
3958 jim_wide w;
3959 char *cp;
3960 int e;
3962 /* parse config or cget options ... */
3963 while (goi->argc > 0) {
3964 Jim_SetEmptyResult(goi->interp);
3965 /* Jim_GetOpt_Debug(goi); */
3967 if (target->type->target_jim_configure) {
3968 /* target defines a configure function */
3969 /* target gets first dibs on parameters */
3970 e = (*(target->type->target_jim_configure))(target, goi);
3971 if (e == JIM_OK) {
3972 /* more? */
3973 continue;
3975 if (e == JIM_ERR) {
3976 /* An error */
3977 return e;
3979 /* otherwise we 'continue' below */
3981 e = Jim_GetOpt_Nvp(goi, nvp_config_opts, &n);
3982 if (e != JIM_OK) {
3983 Jim_GetOpt_NvpUnknown(goi, nvp_config_opts, 0);
3984 return e;
3986 switch (n->value) {
3987 case TCFG_TYPE:
3988 /* not setable */
3989 if (goi->isconfigure) {
3990 Jim_SetResultFormatted(goi->interp,
3991 "not settable: %s", n->name);
3992 return JIM_ERR;
3993 } else {
3994 no_params:
3995 if (goi->argc != 0) {
3996 Jim_WrongNumArgs(goi->interp,
3997 goi->argc, goi->argv,
3998 "NO PARAMS");
3999 return JIM_ERR;
4002 Jim_SetResultString(goi->interp,
4003 target_type_name(target), -1);
4004 /* loop for more */
4005 break;
4006 case TCFG_EVENT:
4007 if (goi->argc == 0) {
4008 Jim_WrongNumArgs(goi->interp, goi->argc, goi->argv, "-event ?event-name? ...");
4009 return JIM_ERR;
4012 e = Jim_GetOpt_Nvp(goi, nvp_target_event, &n);
4013 if (e != JIM_OK) {
4014 Jim_GetOpt_NvpUnknown(goi, nvp_target_event, 1);
4015 return e;
4018 if (goi->isconfigure) {
4019 if (goi->argc != 1) {
4020 Jim_WrongNumArgs(goi->interp, goi->argc, goi->argv, "-event ?event-name? ?EVENT-BODY?");
4021 return JIM_ERR;
4023 } else {
4024 if (goi->argc != 0) {
4025 Jim_WrongNumArgs(goi->interp, goi->argc, goi->argv, "-event ?event-name?");
4026 return JIM_ERR;
4031 struct target_event_action *teap;
4033 teap = target->event_action;
4034 /* replace existing? */
4035 while (teap) {
4036 if (teap->event == (enum target_event)n->value)
4037 break;
4038 teap = teap->next;
4041 if (goi->isconfigure) {
4042 bool replace = true;
4043 if (teap == NULL) {
4044 /* create new */
4045 teap = calloc(1, sizeof(*teap));
4046 replace = false;
4048 teap->event = n->value;
4049 teap->interp = goi->interp;
4050 Jim_GetOpt_Obj(goi, &o);
4051 if (teap->body)
4052 Jim_DecrRefCount(teap->interp, teap->body);
4053 teap->body = Jim_DuplicateObj(goi->interp, o);
4055 * FIXME:
4056 * Tcl/TK - "tk events" have a nice feature.
4057 * See the "BIND" command.
4058 * We should support that here.
4059 * You can specify %X and %Y in the event code.
4060 * The idea is: %T - target name.
4061 * The idea is: %N - target number
4062 * The idea is: %E - event name.
4064 Jim_IncrRefCount(teap->body);
4066 if (!replace) {
4067 /* add to head of event list */
4068 teap->next = target->event_action;
4069 target->event_action = teap;
4071 Jim_SetEmptyResult(goi->interp);
4072 } else {
4073 /* get */
4074 if (teap == NULL)
4075 Jim_SetEmptyResult(goi->interp);
4076 else
4077 Jim_SetResult(goi->interp, Jim_DuplicateObj(goi->interp, teap->body));
4080 /* loop for more */
4081 break;
4083 case TCFG_WORK_AREA_VIRT:
4084 if (goi->isconfigure) {
4085 target_free_all_working_areas(target);
4086 e = Jim_GetOpt_Wide(goi, &w);
4087 if (e != JIM_OK)
4088 return e;
4089 target->working_area_virt = w;
4090 target->working_area_virt_spec = true;
4091 } else {
4092 if (goi->argc != 0)
4093 goto no_params;
4095 Jim_SetResult(goi->interp, Jim_NewIntObj(goi->interp, target->working_area_virt));
4096 /* loop for more */
4097 break;
4099 case TCFG_WORK_AREA_PHYS:
4100 if (goi->isconfigure) {
4101 target_free_all_working_areas(target);
4102 e = Jim_GetOpt_Wide(goi, &w);
4103 if (e != JIM_OK)
4104 return e;
4105 target->working_area_phys = w;
4106 target->working_area_phys_spec = true;
4107 } else {
4108 if (goi->argc != 0)
4109 goto no_params;
4111 Jim_SetResult(goi->interp, Jim_NewIntObj(goi->interp, target->working_area_phys));
4112 /* loop for more */
4113 break;
4115 case TCFG_WORK_AREA_SIZE:
4116 if (goi->isconfigure) {
4117 target_free_all_working_areas(target);
4118 e = Jim_GetOpt_Wide(goi, &w);
4119 if (e != JIM_OK)
4120 return e;
4121 target->working_area_size = w;
4122 } else {
4123 if (goi->argc != 0)
4124 goto no_params;
4126 Jim_SetResult(goi->interp, Jim_NewIntObj(goi->interp, target->working_area_size));
4127 /* loop for more */
4128 break;
4130 case TCFG_WORK_AREA_BACKUP:
4131 if (goi->isconfigure) {
4132 target_free_all_working_areas(target);
4133 e = Jim_GetOpt_Wide(goi, &w);
4134 if (e != JIM_OK)
4135 return e;
4136 /* make this exactly 1 or 0 */
4137 target->backup_working_area = (!!w);
4138 } else {
4139 if (goi->argc != 0)
4140 goto no_params;
4142 Jim_SetResult(goi->interp, Jim_NewIntObj(goi->interp, target->backup_working_area));
4143 /* loop for more e*/
4144 break;
4147 case TCFG_ENDIAN:
4148 if (goi->isconfigure) {
4149 e = Jim_GetOpt_Nvp(goi, nvp_target_endian, &n);
4150 if (e != JIM_OK) {
4151 Jim_GetOpt_NvpUnknown(goi, nvp_target_endian, 1);
4152 return e;
4154 target->endianness = n->value;
4155 } else {
4156 if (goi->argc != 0)
4157 goto no_params;
4159 n = Jim_Nvp_value2name_simple(nvp_target_endian, target->endianness);
4160 if (n->name == NULL) {
4161 target->endianness = TARGET_LITTLE_ENDIAN;
4162 n = Jim_Nvp_value2name_simple(nvp_target_endian, target->endianness);
4164 Jim_SetResultString(goi->interp, n->name, -1);
4165 /* loop for more */
4166 break;
4168 case TCFG_VARIANT:
4169 if (goi->isconfigure) {
4170 if (goi->argc < 1) {
4171 Jim_SetResultFormatted(goi->interp,
4172 "%s ?STRING?",
4173 n->name);
4174 return JIM_ERR;
4176 if (target->variant)
4177 free((void *)(target->variant));
4178 e = Jim_GetOpt_String(goi, &cp, NULL);
4179 if (e != JIM_OK)
4180 return e;
4181 target->variant = strdup(cp);
4182 } else {
4183 if (goi->argc != 0)
4184 goto no_params;
4186 Jim_SetResultString(goi->interp, target->variant, -1);
4187 /* loop for more */
4188 break;
4190 case TCFG_COREID:
4191 if (goi->isconfigure) {
4192 e = Jim_GetOpt_Wide(goi, &w);
4193 if (e != JIM_OK)
4194 return e;
4195 target->coreid = (int32_t)w;
4196 } else {
4197 if (goi->argc != 0)
4198 goto no_params;
4200 Jim_SetResult(goi->interp, Jim_NewIntObj(goi->interp, target->working_area_size));
4201 /* loop for more */
4202 break;
4204 case TCFG_CHAIN_POSITION:
4205 if (goi->isconfigure) {
4206 Jim_Obj *o_t;
4207 struct jtag_tap *tap;
4208 target_free_all_working_areas(target);
4209 e = Jim_GetOpt_Obj(goi, &o_t);
4210 if (e != JIM_OK)
4211 return e;
4212 tap = jtag_tap_by_jim_obj(goi->interp, o_t);
4213 if (tap == NULL)
4214 return JIM_ERR;
4215 /* make this exactly 1 or 0 */
4216 target->tap = tap;
4217 } else {
4218 if (goi->argc != 0)
4219 goto no_params;
4221 Jim_SetResultString(goi->interp, target->tap->dotted_name, -1);
4222 /* loop for more e*/
4223 break;
4224 case TCFG_DBGBASE:
4225 if (goi->isconfigure) {
4226 e = Jim_GetOpt_Wide(goi, &w);
4227 if (e != JIM_OK)
4228 return e;
4229 target->dbgbase = (uint32_t)w;
4230 target->dbgbase_set = true;
4231 } else {
4232 if (goi->argc != 0)
4233 goto no_params;
4235 Jim_SetResult(goi->interp, Jim_NewIntObj(goi->interp, target->dbgbase));
4236 /* loop for more */
4237 break;
4239 case TCFG_RTOS:
4240 /* RTOS */
4242 int result = rtos_create(goi, target);
4243 if (result != JIM_OK)
4244 return result;
4246 /* loop for more */
4247 break;
4249 } /* while (goi->argc) */
4252 /* done - we return */
4253 return JIM_OK;
4256 static int jim_target_configure(Jim_Interp *interp, int argc, Jim_Obj * const *argv)
4258 Jim_GetOptInfo goi;
4260 Jim_GetOpt_Setup(&goi, interp, argc - 1, argv + 1);
4261 goi.isconfigure = !strcmp(Jim_GetString(argv[0], NULL), "configure");
4262 int need_args = 1 + goi.isconfigure;
4263 if (goi.argc < need_args) {
4264 Jim_WrongNumArgs(goi.interp, goi.argc, goi.argv,
4265 goi.isconfigure
4266 ? "missing: -option VALUE ..."
4267 : "missing: -option ...");
4268 return JIM_ERR;
4270 struct target *target = Jim_CmdPrivData(goi.interp);
4271 return target_configure(&goi, target);
4274 static int jim_target_mw(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
4276 const char *cmd_name = Jim_GetString(argv[0], NULL);
4278 Jim_GetOptInfo goi;
4279 Jim_GetOpt_Setup(&goi, interp, argc - 1, argv + 1);
4281 if (goi.argc < 2 || goi.argc > 4) {
4282 Jim_SetResultFormatted(goi.interp,
4283 "usage: %s [phys] <address> <data> [<count>]", cmd_name);
4284 return JIM_ERR;
4287 target_write_fn fn;
4288 fn = target_write_memory_fast;
4290 int e;
4291 if (strcmp(Jim_GetString(argv[1], NULL), "phys") == 0) {
4292 /* consume it */
4293 struct Jim_Obj *obj;
4294 e = Jim_GetOpt_Obj(&goi, &obj);
4295 if (e != JIM_OK)
4296 return e;
4298 fn = target_write_phys_memory;
4301 jim_wide a;
4302 e = Jim_GetOpt_Wide(&goi, &a);
4303 if (e != JIM_OK)
4304 return e;
4306 jim_wide b;
4307 e = Jim_GetOpt_Wide(&goi, &b);
4308 if (e != JIM_OK)
4309 return e;
4311 jim_wide c = 1;
4312 if (goi.argc == 1) {
4313 e = Jim_GetOpt_Wide(&goi, &c);
4314 if (e != JIM_OK)
4315 return e;
4318 /* all args must be consumed */
4319 if (goi.argc != 0)
4320 return JIM_ERR;
4322 struct target *target = Jim_CmdPrivData(goi.interp);
4323 unsigned data_size;
4324 if (strcasecmp(cmd_name, "mww") == 0)
4325 data_size = 4;
4326 else if (strcasecmp(cmd_name, "mwh") == 0)
4327 data_size = 2;
4328 else if (strcasecmp(cmd_name, "mwb") == 0)
4329 data_size = 1;
4330 else {
4331 LOG_ERROR("command '%s' unknown: ", cmd_name);
4332 return JIM_ERR;
4335 return (target_fill_mem(target, a, fn, data_size, b, c) == ERROR_OK) ? JIM_OK : JIM_ERR;
4338 static int jim_target_md(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
4340 const char *cmd_name = Jim_GetString(argv[0], NULL);
4342 Jim_GetOptInfo goi;
4343 Jim_GetOpt_Setup(&goi, interp, argc - 1, argv + 1);
4345 if ((goi.argc < 1) || (goi.argc > 3)) {
4346 Jim_SetResultFormatted(goi.interp,
4347 "usage: %s [phys] <address> [<count>]", cmd_name);
4348 return JIM_ERR;
4351 int (*fn)(struct target *target,
4352 uint32_t address, uint32_t size, uint32_t count, uint8_t *buffer);
4353 fn = target_read_memory;
4355 int e;
4356 if (strcmp(Jim_GetString(argv[1], NULL), "phys") == 0) {
4357 /* consume it */
4358 struct Jim_Obj *obj;
4359 e = Jim_GetOpt_Obj(&goi, &obj);
4360 if (e != JIM_OK)
4361 return e;
4363 fn = target_read_phys_memory;
4366 jim_wide a;
4367 e = Jim_GetOpt_Wide(&goi, &a);
4368 if (e != JIM_OK)
4369 return JIM_ERR;
4370 jim_wide c;
4371 if (goi.argc == 1) {
4372 e = Jim_GetOpt_Wide(&goi, &c);
4373 if (e != JIM_OK)
4374 return JIM_ERR;
4375 } else
4376 c = 1;
4378 /* all args must be consumed */
4379 if (goi.argc != 0)
4380 return JIM_ERR;
4382 jim_wide b = 1; /* shut up gcc */
4383 if (strcasecmp(cmd_name, "mdw") == 0)
4384 b = 4;
4385 else if (strcasecmp(cmd_name, "mdh") == 0)
4386 b = 2;
4387 else if (strcasecmp(cmd_name, "mdb") == 0)
4388 b = 1;
4389 else {
4390 LOG_ERROR("command '%s' unknown: ", cmd_name);
4391 return JIM_ERR;
4394 /* convert count to "bytes" */
4395 c = c * b;
4397 struct target *target = Jim_CmdPrivData(goi.interp);
4398 uint8_t target_buf[32];
4399 jim_wide x, y, z;
4400 while (c > 0) {
4401 y = c;
4402 if (y > 16)
4403 y = 16;
4404 e = fn(target, a, b, y / b, target_buf);
4405 if (e != ERROR_OK) {
4406 char tmp[10];
4407 snprintf(tmp, sizeof(tmp), "%08lx", (long)a);
4408 Jim_SetResultFormatted(interp, "error reading target @ 0x%s", tmp);
4409 return JIM_ERR;
4412 command_print(NULL, "0x%08x ", (int)(a));
4413 switch (b) {
4414 case 4:
4415 for (x = 0; x < 16 && x < y; x += 4) {
4416 z = target_buffer_get_u32(target, &(target_buf[x]));
4417 command_print(NULL, "%08x ", (int)(z));
4419 for (; (x < 16) ; x += 4)
4420 command_print(NULL, " ");
4421 break;
4422 case 2:
4423 for (x = 0; x < 16 && x < y; x += 2) {
4424 z = target_buffer_get_u16(target, &(target_buf[x]));
4425 command_print(NULL, "%04x ", (int)(z));
4427 for (; (x < 16) ; x += 2)
4428 command_print(NULL, " ");
4429 break;
4430 case 1:
4431 default:
4432 for (x = 0 ; (x < 16) && (x < y) ; x += 1) {
4433 z = target_buffer_get_u8(target, &(target_buf[x]));
4434 command_print(NULL, "%02x ", (int)(z));
4436 for (; (x < 16) ; x += 1)
4437 command_print(NULL, " ");
4438 break;
4440 /* ascii-ify the bytes */
4441 for (x = 0 ; x < y ; x++) {
4442 if ((target_buf[x] >= 0x20) &&
4443 (target_buf[x] <= 0x7e)) {
4444 /* good */
4445 } else {
4446 /* smack it */
4447 target_buf[x] = '.';
4450 /* space pad */
4451 while (x < 16) {
4452 target_buf[x] = ' ';
4453 x++;
4455 /* terminate */
4456 target_buf[16] = 0;
4457 /* print - with a newline */
4458 command_print(NULL, "%s\n", target_buf);
4459 /* NEXT... */
4460 c -= 16;
4461 a += 16;
4463 return JIM_OK;
4466 static int jim_target_mem2array(Jim_Interp *interp,
4467 int argc, Jim_Obj *const *argv)
4469 struct target *target = Jim_CmdPrivData(interp);
4470 return target_mem2array(interp, target, argc - 1, argv + 1);
4473 static int jim_target_array2mem(Jim_Interp *interp,
4474 int argc, Jim_Obj *const *argv)
4476 struct target *target = Jim_CmdPrivData(interp);
4477 return target_array2mem(interp, target, argc - 1, argv + 1);
4480 static int jim_target_tap_disabled(Jim_Interp *interp)
4482 Jim_SetResultFormatted(interp, "[TAP is disabled]");
4483 return JIM_ERR;
4486 static int jim_target_examine(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
4488 if (argc != 1) {
4489 Jim_WrongNumArgs(interp, 1, argv, "[no parameters]");
4490 return JIM_ERR;
4492 struct target *target = Jim_CmdPrivData(interp);
4493 if (!target->tap->enabled)
4494 return jim_target_tap_disabled(interp);
4496 int e = target->type->examine(target);
4497 if (e != ERROR_OK)
4498 return JIM_ERR;
4499 return JIM_OK;
4502 static int jim_target_halt_gdb(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
4504 if (argc != 1) {
4505 Jim_WrongNumArgs(interp, 1, argv, "[no parameters]");
4506 return JIM_ERR;
4508 struct target *target = Jim_CmdPrivData(interp);
4510 if (target_call_event_callbacks(target, TARGET_EVENT_GDB_HALT) != ERROR_OK)
4511 return JIM_ERR;
4513 return JIM_OK;
4516 static int jim_target_poll(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
4518 if (argc != 1) {
4519 Jim_WrongNumArgs(interp, 1, argv, "[no parameters]");
4520 return JIM_ERR;
4522 struct target *target = Jim_CmdPrivData(interp);
4523 if (!target->tap->enabled)
4524 return jim_target_tap_disabled(interp);
4526 int e;
4527 if (!(target_was_examined(target)))
4528 e = ERROR_TARGET_NOT_EXAMINED;
4529 else
4530 e = target->type->poll(target);
4531 if (e != ERROR_OK)
4532 return JIM_ERR;
4533 return JIM_OK;
4536 static int jim_target_reset(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
4538 Jim_GetOptInfo goi;
4539 Jim_GetOpt_Setup(&goi, interp, argc - 1, argv + 1);
4541 if (goi.argc != 2) {
4542 Jim_WrongNumArgs(interp, 0, argv,
4543 "([tT]|[fF]|assert|deassert) BOOL");
4544 return JIM_ERR;
4547 Jim_Nvp *n;
4548 int e = Jim_GetOpt_Nvp(&goi, nvp_assert, &n);
4549 if (e != JIM_OK) {
4550 Jim_GetOpt_NvpUnknown(&goi, nvp_assert, 1);
4551 return e;
4553 /* the halt or not param */
4554 jim_wide a;
4555 e = Jim_GetOpt_Wide(&goi, &a);
4556 if (e != JIM_OK)
4557 return e;
4559 struct target *target = Jim_CmdPrivData(goi.interp);
4560 if (!target->tap->enabled)
4561 return jim_target_tap_disabled(interp);
4562 if (!(target_was_examined(target))) {
4563 LOG_ERROR("Target not examined yet");
4564 return ERROR_TARGET_NOT_EXAMINED;
4566 if (!target->type->assert_reset || !target->type->deassert_reset) {
4567 Jim_SetResultFormatted(interp,
4568 "No target-specific reset for %s",
4569 target_name(target));
4570 return JIM_ERR;
4572 /* determine if we should halt or not. */
4573 target->reset_halt = !!a;
4574 /* When this happens - all workareas are invalid. */
4575 target_free_all_working_areas_restore(target, 0);
4577 /* do the assert */
4578 if (n->value == NVP_ASSERT)
4579 e = target->type->assert_reset(target);
4580 else
4581 e = target->type->deassert_reset(target);
4582 return (e == ERROR_OK) ? JIM_OK : JIM_ERR;
4585 static int jim_target_halt(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
4587 if (argc != 1) {
4588 Jim_WrongNumArgs(interp, 1, argv, "[no parameters]");
4589 return JIM_ERR;
4591 struct target *target = Jim_CmdPrivData(interp);
4592 if (!target->tap->enabled)
4593 return jim_target_tap_disabled(interp);
4594 int e = target->type->halt(target);
4595 return (e == ERROR_OK) ? JIM_OK : JIM_ERR;
4598 static int jim_target_wait_state(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
4600 Jim_GetOptInfo goi;
4601 Jim_GetOpt_Setup(&goi, interp, argc - 1, argv + 1);
4603 /* params: <name> statename timeoutmsecs */
4604 if (goi.argc != 2) {
4605 const char *cmd_name = Jim_GetString(argv[0], NULL);
4606 Jim_SetResultFormatted(goi.interp,
4607 "%s <state_name> <timeout_in_msec>", cmd_name);
4608 return JIM_ERR;
4611 Jim_Nvp *n;
4612 int e = Jim_GetOpt_Nvp(&goi, nvp_target_state, &n);
4613 if (e != JIM_OK) {
4614 Jim_GetOpt_NvpUnknown(&goi, nvp_target_state, 1);
4615 return e;
4617 jim_wide a;
4618 e = Jim_GetOpt_Wide(&goi, &a);
4619 if (e != JIM_OK)
4620 return e;
4621 struct target *target = Jim_CmdPrivData(interp);
4622 if (!target->tap->enabled)
4623 return jim_target_tap_disabled(interp);
4625 e = target_wait_state(target, n->value, a);
4626 if (e != ERROR_OK) {
4627 Jim_Obj *eObj = Jim_NewIntObj(interp, e);
4628 Jim_SetResultFormatted(goi.interp,
4629 "target: %s wait %s fails (%#s) %s",
4630 target_name(target), n->name,
4631 eObj, target_strerror_safe(e));
4632 Jim_FreeNewObj(interp, eObj);
4633 return JIM_ERR;
4635 return JIM_OK;
4637 /* List for human, Events defined for this target.
4638 * scripts/programs should use 'name cget -event NAME'
4640 static int jim_target_event_list(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
4642 struct command_context *cmd_ctx = current_command_context(interp);
4643 assert(cmd_ctx != NULL);
4645 struct target *target = Jim_CmdPrivData(interp);
4646 struct target_event_action *teap = target->event_action;
4647 command_print(cmd_ctx, "Event actions for target (%d) %s\n",
4648 target->target_number,
4649 target_name(target));
4650 command_print(cmd_ctx, "%-25s | Body", "Event");
4651 command_print(cmd_ctx, "------------------------- | "
4652 "----------------------------------------");
4653 while (teap) {
4654 Jim_Nvp *opt = Jim_Nvp_value2name_simple(nvp_target_event, teap->event);
4655 command_print(cmd_ctx, "%-25s | %s",
4656 opt->name, Jim_GetString(teap->body, NULL));
4657 teap = teap->next;
4659 command_print(cmd_ctx, "***END***");
4660 return JIM_OK;
4662 static int jim_target_current_state(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
4664 if (argc != 1) {
4665 Jim_WrongNumArgs(interp, 1, argv, "[no parameters]");
4666 return JIM_ERR;
4668 struct target *target = Jim_CmdPrivData(interp);
4669 Jim_SetResultString(interp, target_state_name(target), -1);
4670 return JIM_OK;
4672 static int jim_target_invoke_event(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
4674 Jim_GetOptInfo goi;
4675 Jim_GetOpt_Setup(&goi, interp, argc - 1, argv + 1);
4676 if (goi.argc != 1) {
4677 const char *cmd_name = Jim_GetString(argv[0], NULL);
4678 Jim_SetResultFormatted(goi.interp, "%s <eventname>", cmd_name);
4679 return JIM_ERR;
4681 Jim_Nvp *n;
4682 int e = Jim_GetOpt_Nvp(&goi, nvp_target_event, &n);
4683 if (e != JIM_OK) {
4684 Jim_GetOpt_NvpUnknown(&goi, nvp_target_event, 1);
4685 return e;
4687 struct target *target = Jim_CmdPrivData(interp);
4688 target_handle_event(target, n->value);
4689 return JIM_OK;
4692 static const struct command_registration target_instance_command_handlers[] = {
4694 .name = "configure",
4695 .mode = COMMAND_CONFIG,
4696 .jim_handler = jim_target_configure,
4697 .help = "configure a new target for use",
4698 .usage = "[target_attribute ...]",
4701 .name = "cget",
4702 .mode = COMMAND_ANY,
4703 .jim_handler = jim_target_configure,
4704 .help = "returns the specified target attribute",
4705 .usage = "target_attribute",
4708 .name = "mww",
4709 .mode = COMMAND_EXEC,
4710 .jim_handler = jim_target_mw,
4711 .help = "Write 32-bit word(s) to target memory",
4712 .usage = "address data [count]",
4715 .name = "mwh",
4716 .mode = COMMAND_EXEC,
4717 .jim_handler = jim_target_mw,
4718 .help = "Write 16-bit half-word(s) to target memory",
4719 .usage = "address data [count]",
4722 .name = "mwb",
4723 .mode = COMMAND_EXEC,
4724 .jim_handler = jim_target_mw,
4725 .help = "Write byte(s) to target memory",
4726 .usage = "address data [count]",
4729 .name = "mdw",
4730 .mode = COMMAND_EXEC,
4731 .jim_handler = jim_target_md,
4732 .help = "Display target memory as 32-bit words",
4733 .usage = "address [count]",
4736 .name = "mdh",
4737 .mode = COMMAND_EXEC,
4738 .jim_handler = jim_target_md,
4739 .help = "Display target memory as 16-bit half-words",
4740 .usage = "address [count]",
4743 .name = "mdb",
4744 .mode = COMMAND_EXEC,
4745 .jim_handler = jim_target_md,
4746 .help = "Display target memory as 8-bit bytes",
4747 .usage = "address [count]",
4750 .name = "array2mem",
4751 .mode = COMMAND_EXEC,
4752 .jim_handler = jim_target_array2mem,
4753 .help = "Writes Tcl array of 8/16/32 bit numbers "
4754 "to target memory",
4755 .usage = "arrayname bitwidth address count",
4758 .name = "mem2array",
4759 .mode = COMMAND_EXEC,
4760 .jim_handler = jim_target_mem2array,
4761 .help = "Loads Tcl array of 8/16/32 bit numbers "
4762 "from target memory",
4763 .usage = "arrayname bitwidth address count",
4766 .name = "eventlist",
4767 .mode = COMMAND_EXEC,
4768 .jim_handler = jim_target_event_list,
4769 .help = "displays a table of events defined for this target",
4772 .name = "curstate",
4773 .mode = COMMAND_EXEC,
4774 .jim_handler = jim_target_current_state,
4775 .help = "displays the current state of this target",
4778 .name = "arp_examine",
4779 .mode = COMMAND_EXEC,
4780 .jim_handler = jim_target_examine,
4781 .help = "used internally for reset processing",
4784 .name = "arp_halt_gdb",
4785 .mode = COMMAND_EXEC,
4786 .jim_handler = jim_target_halt_gdb,
4787 .help = "used internally for reset processing to halt GDB",
4790 .name = "arp_poll",
4791 .mode = COMMAND_EXEC,
4792 .jim_handler = jim_target_poll,
4793 .help = "used internally for reset processing",
4796 .name = "arp_reset",
4797 .mode = COMMAND_EXEC,
4798 .jim_handler = jim_target_reset,
4799 .help = "used internally for reset processing",
4802 .name = "arp_halt",
4803 .mode = COMMAND_EXEC,
4804 .jim_handler = jim_target_halt,
4805 .help = "used internally for reset processing",
4808 .name = "arp_waitstate",
4809 .mode = COMMAND_EXEC,
4810 .jim_handler = jim_target_wait_state,
4811 .help = "used internally for reset processing",
4814 .name = "invoke-event",
4815 .mode = COMMAND_EXEC,
4816 .jim_handler = jim_target_invoke_event,
4817 .help = "invoke handler for specified event",
4818 .usage = "event_name",
4820 COMMAND_REGISTRATION_DONE
4823 static int target_create(Jim_GetOptInfo *goi)
4825 Jim_Obj *new_cmd;
4826 Jim_Cmd *cmd;
4827 const char *cp;
4828 char *cp2;
4829 int e;
4830 int x;
4831 struct target *target;
4832 struct command_context *cmd_ctx;
4834 cmd_ctx = current_command_context(goi->interp);
4835 assert(cmd_ctx != NULL);
4837 if (goi->argc < 3) {
4838 Jim_WrongNumArgs(goi->interp, 1, goi->argv, "?name? ?type? ..options...");
4839 return JIM_ERR;
4842 /* COMMAND */
4843 Jim_GetOpt_Obj(goi, &new_cmd);
4844 /* does this command exist? */
4845 cmd = Jim_GetCommand(goi->interp, new_cmd, JIM_ERRMSG);
4846 if (cmd) {
4847 cp = Jim_GetString(new_cmd, NULL);
4848 Jim_SetResultFormatted(goi->interp, "Command/target: %s Exists", cp);
4849 return JIM_ERR;
4852 /* TYPE */
4853 e = Jim_GetOpt_String(goi, &cp2, NULL);
4854 if (e != JIM_OK)
4855 return e;
4856 cp = cp2;
4857 /* now does target type exist */
4858 for (x = 0 ; target_types[x] ; x++) {
4859 if (0 == strcmp(cp, target_types[x]->name)) {
4860 /* found */
4861 break;
4864 if (target_types[x] == NULL) {
4865 Jim_SetResultFormatted(goi->interp, "Unknown target type %s, try one of ", cp);
4866 for (x = 0 ; target_types[x] ; x++) {
4867 if (target_types[x + 1]) {
4868 Jim_AppendStrings(goi->interp,
4869 Jim_GetResult(goi->interp),
4870 target_types[x]->name,
4871 ", ", NULL);
4872 } else {
4873 Jim_AppendStrings(goi->interp,
4874 Jim_GetResult(goi->interp),
4875 " or ",
4876 target_types[x]->name, NULL);
4879 return JIM_ERR;
4882 /* Create it */
4883 target = calloc(1, sizeof(struct target));
4884 /* set target number */
4885 target->target_number = new_target_number();
4887 /* allocate memory for each unique target type */
4888 target->type = (struct target_type *)calloc(1, sizeof(struct target_type));
4890 memcpy(target->type, target_types[x], sizeof(struct target_type));
4892 /* will be set by "-endian" */
4893 target->endianness = TARGET_ENDIAN_UNKNOWN;
4895 /* default to first core, override with -coreid */
4896 target->coreid = 0;
4898 target->working_area = 0x0;
4899 target->working_area_size = 0x0;
4900 target->working_areas = NULL;
4901 target->backup_working_area = 0;
4903 target->state = TARGET_UNKNOWN;
4904 target->debug_reason = DBG_REASON_UNDEFINED;
4905 target->reg_cache = NULL;
4906 target->breakpoints = NULL;
4907 target->watchpoints = NULL;
4908 target->next = NULL;
4909 target->arch_info = NULL;
4911 target->display = 1;
4913 target->halt_issued = false;
4915 /* initialize trace information */
4916 target->trace_info = malloc(sizeof(struct trace));
4917 target->trace_info->num_trace_points = 0;
4918 target->trace_info->trace_points_size = 0;
4919 target->trace_info->trace_points = NULL;
4920 target->trace_info->trace_history_size = 0;
4921 target->trace_info->trace_history = NULL;
4922 target->trace_info->trace_history_pos = 0;
4923 target->trace_info->trace_history_overflowed = 0;
4925 target->dbgmsg = NULL;
4926 target->dbg_msg_enabled = 0;
4928 target->endianness = TARGET_ENDIAN_UNKNOWN;
4930 target->rtos = NULL;
4931 target->rtos_auto_detect = false;
4933 /* Do the rest as "configure" options */
4934 goi->isconfigure = 1;
4935 e = target_configure(goi, target);
4937 if (target->tap == NULL) {
4938 Jim_SetResultString(goi->interp, "-chain-position required when creating target", -1);
4939 e = JIM_ERR;
4942 if (e != JIM_OK) {
4943 free(target->type);
4944 free(target);
4945 return e;
4948 if (target->endianness == TARGET_ENDIAN_UNKNOWN) {
4949 /* default endian to little if not specified */
4950 target->endianness = TARGET_LITTLE_ENDIAN;
4953 /* incase variant is not set */
4954 if (!target->variant)
4955 target->variant = strdup("");
4957 cp = Jim_GetString(new_cmd, NULL);
4958 target->cmd_name = strdup(cp);
4960 /* create the target specific commands */
4961 if (target->type->commands) {
4962 e = register_commands(cmd_ctx, NULL, target->type->commands);
4963 if (ERROR_OK != e)
4964 LOG_ERROR("unable to register '%s' commands", cp);
4966 if (target->type->target_create)
4967 (*(target->type->target_create))(target, goi->interp);
4969 /* append to end of list */
4971 struct target **tpp;
4972 tpp = &(all_targets);
4973 while (*tpp)
4974 tpp = &((*tpp)->next);
4975 *tpp = target;
4978 /* now - create the new target name command */
4979 const const struct command_registration target_subcommands[] = {
4981 .chain = target_instance_command_handlers,
4984 .chain = target->type->commands,
4986 COMMAND_REGISTRATION_DONE
4988 const const struct command_registration target_commands[] = {
4990 .name = cp,
4991 .mode = COMMAND_ANY,
4992 .help = "target command group",
4993 .usage = "",
4994 .chain = target_subcommands,
4996 COMMAND_REGISTRATION_DONE
4998 e = register_commands(cmd_ctx, NULL, target_commands);
4999 if (ERROR_OK != e)
5000 return JIM_ERR;
5002 struct command *c = command_find_in_context(cmd_ctx, cp);
5003 assert(c);
5004 command_set_handler_data(c, target);
5006 return (ERROR_OK == e) ? JIM_OK : JIM_ERR;
5009 static int jim_target_current(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
5011 if (argc != 1) {
5012 Jim_WrongNumArgs(interp, 1, argv, "Too many parameters");
5013 return JIM_ERR;
5015 struct command_context *cmd_ctx = current_command_context(interp);
5016 assert(cmd_ctx != NULL);
5018 Jim_SetResultString(interp, get_current_target(cmd_ctx)->cmd_name, -1);
5019 return JIM_OK;
5022 static int jim_target_types(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
5024 if (argc != 1) {
5025 Jim_WrongNumArgs(interp, 1, argv, "Too many parameters");
5026 return JIM_ERR;
5028 Jim_SetResult(interp, Jim_NewListObj(interp, NULL, 0));
5029 for (unsigned x = 0; NULL != target_types[x]; x++) {
5030 Jim_ListAppendElement(interp, Jim_GetResult(interp),
5031 Jim_NewStringObj(interp, target_types[x]->name, -1));
5033 return JIM_OK;
5036 static int jim_target_names(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
5038 if (argc != 1) {
5039 Jim_WrongNumArgs(interp, 1, argv, "Too many parameters");
5040 return JIM_ERR;
5042 Jim_SetResult(interp, Jim_NewListObj(interp, NULL, 0));
5043 struct target *target = all_targets;
5044 while (target) {
5045 Jim_ListAppendElement(interp, Jim_GetResult(interp),
5046 Jim_NewStringObj(interp, target_name(target), -1));
5047 target = target->next;
5049 return JIM_OK;
5052 static int jim_target_smp(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
5054 int i;
5055 const char *targetname;
5056 int retval, len;
5057 struct target *target = (struct target *) NULL;
5058 struct target_list *head, *curr, *new;
5059 curr = (struct target_list *) NULL;
5060 head = (struct target_list *) NULL;
5061 new = (struct target_list *) NULL;
5063 retval = 0;
5064 LOG_DEBUG("%d", argc);
5065 /* argv[1] = target to associate in smp
5066 * argv[2] = target to assoicate in smp
5067 * argv[3] ...
5070 for (i = 1; i < argc; i++) {
5072 targetname = Jim_GetString(argv[i], &len);
5073 target = get_target(targetname);
5074 LOG_DEBUG("%s ", targetname);
5075 if (target) {
5076 new = malloc(sizeof(struct target_list));
5077 new->target = target;
5078 new->next = (struct target_list *)NULL;
5079 if (head == (struct target_list *)NULL) {
5080 head = new;
5081 curr = head;
5082 } else {
5083 curr->next = new;
5084 curr = new;
5088 /* now parse the list of cpu and put the target in smp mode*/
5089 curr = head;
5091 while (curr != (struct target_list *)NULL) {
5092 target = curr->target;
5093 target->smp = 1;
5094 target->head = head;
5095 curr = curr->next;
5097 if (target->rtos)
5098 retval = rtos_smp_init(head->target);
5099 return retval;
5103 static int jim_target_create(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
5105 Jim_GetOptInfo goi;
5106 Jim_GetOpt_Setup(&goi, interp, argc - 1, argv + 1);
5107 if (goi.argc < 3) {
5108 Jim_WrongNumArgs(goi.interp, goi.argc, goi.argv,
5109 "<name> <target_type> [<target_options> ...]");
5110 return JIM_ERR;
5112 return target_create(&goi);
5115 static int jim_target_number(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
5117 Jim_GetOptInfo goi;
5118 Jim_GetOpt_Setup(&goi, interp, argc - 1, argv + 1);
5120 /* It's OK to remove this mechanism sometime after August 2010 or so */
5121 LOG_WARNING("don't use numbers as target identifiers; use names");
5122 if (goi.argc != 1) {
5123 Jim_SetResultFormatted(goi.interp, "usage: target number <number>");
5124 return JIM_ERR;
5126 jim_wide w;
5127 int e = Jim_GetOpt_Wide(&goi, &w);
5128 if (e != JIM_OK)
5129 return JIM_ERR;
5131 struct target *target;
5132 for (target = all_targets; NULL != target; target = target->next) {
5133 if (target->target_number != w)
5134 continue;
5136 Jim_SetResultString(goi.interp, target_name(target), -1);
5137 return JIM_OK;
5140 Jim_Obj *wObj = Jim_NewIntObj(goi.interp, w);
5141 Jim_SetResultFormatted(goi.interp,
5142 "Target: number %#s does not exist", wObj);
5143 Jim_FreeNewObj(interp, wObj);
5145 return JIM_ERR;
5148 static int jim_target_count(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
5150 if (argc != 1) {
5151 Jim_WrongNumArgs(interp, 1, argv, "<no parameters>");
5152 return JIM_ERR;
5154 unsigned count = 0;
5155 struct target *target = all_targets;
5156 while (NULL != target) {
5157 target = target->next;
5158 count++;
5160 Jim_SetResult(interp, Jim_NewIntObj(interp, count));
5161 return JIM_OK;
5164 static const struct command_registration target_subcommand_handlers[] = {
5166 .name = "init",
5167 .mode = COMMAND_CONFIG,
5168 .handler = handle_target_init_command,
5169 .help = "initialize targets",
5172 .name = "create",
5173 /* REVISIT this should be COMMAND_CONFIG ... */
5174 .mode = COMMAND_ANY,
5175 .jim_handler = jim_target_create,
5176 .usage = "name type '-chain-position' name [options ...]",
5177 .help = "Creates and selects a new target",
5180 .name = "current",
5181 .mode = COMMAND_ANY,
5182 .jim_handler = jim_target_current,
5183 .help = "Returns the currently selected target",
5186 .name = "types",
5187 .mode = COMMAND_ANY,
5188 .jim_handler = jim_target_types,
5189 .help = "Returns the available target types as "
5190 "a list of strings",
5193 .name = "names",
5194 .mode = COMMAND_ANY,
5195 .jim_handler = jim_target_names,
5196 .help = "Returns the names of all targets as a list of strings",
5199 .name = "number",
5200 .mode = COMMAND_ANY,
5201 .jim_handler = jim_target_number,
5202 .usage = "number",
5203 .help = "Returns the name of the numbered target "
5204 "(DEPRECATED)",
5207 .name = "count",
5208 .mode = COMMAND_ANY,
5209 .jim_handler = jim_target_count,
5210 .help = "Returns the number of targets as an integer "
5211 "(DEPRECATED)",
5214 .name = "smp",
5215 .mode = COMMAND_ANY,
5216 .jim_handler = jim_target_smp,
5217 .usage = "targetname1 targetname2 ...",
5218 .help = "gather several target in a smp list"
5221 COMMAND_REGISTRATION_DONE
5224 struct FastLoad {
5225 uint32_t address;
5226 uint8_t *data;
5227 int length;
5231 static int fastload_num;
5232 static struct FastLoad *fastload;
5234 static void free_fastload(void)
5236 if (fastload != NULL) {
5237 int i;
5238 for (i = 0; i < fastload_num; i++) {
5239 if (fastload[i].data)
5240 free(fastload[i].data);
5242 free(fastload);
5243 fastload = NULL;
5247 COMMAND_HANDLER(handle_fast_load_image_command)
5249 uint8_t *buffer;
5250 size_t buf_cnt;
5251 uint32_t image_size;
5252 uint32_t min_address = 0;
5253 uint32_t max_address = 0xffffffff;
5254 int i;
5256 struct image image;
5258 int retval = CALL_COMMAND_HANDLER(parse_load_image_command_CMD_ARGV,
5259 &image, &min_address, &max_address);
5260 if (ERROR_OK != retval)
5261 return retval;
5263 struct duration bench;
5264 duration_start(&bench);
5266 retval = image_open(&image, CMD_ARGV[0], (CMD_ARGC >= 3) ? CMD_ARGV[2] : NULL);
5267 if (retval != ERROR_OK)
5268 return retval;
5270 image_size = 0x0;
5271 retval = ERROR_OK;
5272 fastload_num = image.num_sections;
5273 fastload = (struct FastLoad *)malloc(sizeof(struct FastLoad)*image.num_sections);
5274 if (fastload == NULL) {
5275 command_print(CMD_CTX, "out of memory");
5276 image_close(&image);
5277 return ERROR_FAIL;
5279 memset(fastload, 0, sizeof(struct FastLoad)*image.num_sections);
5280 for (i = 0; i < image.num_sections; i++) {
5281 buffer = malloc(image.sections[i].size);
5282 if (buffer == NULL) {
5283 command_print(CMD_CTX, "error allocating buffer for section (%d bytes)",
5284 (int)(image.sections[i].size));
5285 retval = ERROR_FAIL;
5286 break;
5289 retval = image_read_section(&image, i, 0x0, image.sections[i].size, buffer, &buf_cnt);
5290 if (retval != ERROR_OK) {
5291 free(buffer);
5292 break;
5295 uint32_t offset = 0;
5296 uint32_t length = buf_cnt;
5298 /* DANGER!!! beware of unsigned comparision here!!! */
5300 if ((image.sections[i].base_address + buf_cnt >= min_address) &&
5301 (image.sections[i].base_address < max_address)) {
5302 if (image.sections[i].base_address < min_address) {
5303 /* clip addresses below */
5304 offset += min_address-image.sections[i].base_address;
5305 length -= offset;
5308 if (image.sections[i].base_address + buf_cnt > max_address)
5309 length -= (image.sections[i].base_address + buf_cnt)-max_address;
5311 fastload[i].address = image.sections[i].base_address + offset;
5312 fastload[i].data = malloc(length);
5313 if (fastload[i].data == NULL) {
5314 free(buffer);
5315 command_print(CMD_CTX, "error allocating buffer for section (%d bytes)",
5316 length);
5317 retval = ERROR_FAIL;
5318 break;
5320 memcpy(fastload[i].data, buffer + offset, length);
5321 fastload[i].length = length;
5323 image_size += length;
5324 command_print(CMD_CTX, "%u bytes written at address 0x%8.8x",
5325 (unsigned int)length,
5326 ((unsigned int)(image.sections[i].base_address + offset)));
5329 free(buffer);
5332 if ((ERROR_OK == retval) && (duration_measure(&bench) == ERROR_OK)) {
5333 command_print(CMD_CTX, "Loaded %" PRIu32 " bytes "
5334 "in %fs (%0.3f KiB/s)", image_size,
5335 duration_elapsed(&bench), duration_kbps(&bench, image_size));
5337 command_print(CMD_CTX,
5338 "WARNING: image has not been loaded to target!"
5339 "You can issue a 'fast_load' to finish loading.");
5342 image_close(&image);
5344 if (retval != ERROR_OK)
5345 free_fastload();
5347 return retval;
5350 COMMAND_HANDLER(handle_fast_load_command)
5352 if (CMD_ARGC > 0)
5353 return ERROR_COMMAND_SYNTAX_ERROR;
5354 if (fastload == NULL) {
5355 LOG_ERROR("No image in memory");
5356 return ERROR_FAIL;
5358 int i;
5359 int ms = timeval_ms();
5360 int size = 0;
5361 int retval = ERROR_OK;
5362 for (i = 0; i < fastload_num; i++) {
5363 struct target *target = get_current_target(CMD_CTX);
5364 command_print(CMD_CTX, "Write to 0x%08x, length 0x%08x",
5365 (unsigned int)(fastload[i].address),
5366 (unsigned int)(fastload[i].length));
5367 retval = target_write_buffer(target, fastload[i].address, fastload[i].length, fastload[i].data);
5368 if (retval != ERROR_OK)
5369 break;
5370 size += fastload[i].length;
5372 if (retval == ERROR_OK) {
5373 int after = timeval_ms();
5374 command_print(CMD_CTX, "Loaded image %f kBytes/s", (float)(size/1024.0)/((float)(after-ms)/1000.0));
5376 return retval;
5379 static const struct command_registration target_command_handlers[] = {
5381 .name = "targets",
5382 .handler = handle_targets_command,
5383 .mode = COMMAND_ANY,
5384 .help = "change current default target (one parameter) "
5385 "or prints table of all targets (no parameters)",
5386 .usage = "[target]",
5389 .name = "target",
5390 .mode = COMMAND_CONFIG,
5391 .help = "configure target",
5393 .chain = target_subcommand_handlers,
5395 COMMAND_REGISTRATION_DONE
5398 int target_register_commands(struct command_context *cmd_ctx)
5400 return register_commands(cmd_ctx, NULL, target_command_handlers);
5403 static bool target_reset_nag = true;
5405 bool get_target_reset_nag(void)
5407 return target_reset_nag;
5410 COMMAND_HANDLER(handle_target_reset_nag)
5412 return CALL_COMMAND_HANDLER(handle_command_parse_bool,
5413 &target_reset_nag, "Nag after each reset about options to improve "
5414 "performance");
5417 COMMAND_HANDLER(handle_ps_command)
5419 struct target *target = get_current_target(CMD_CTX);
5420 char *display;
5421 if (target->state != TARGET_HALTED) {
5422 LOG_INFO("target not halted !!");
5423 return ERROR_OK;
5426 if ((target->rtos) && (target->rtos->type)
5427 && (target->rtos->type->ps_command)) {
5428 display = target->rtos->type->ps_command(target);
5429 command_print(CMD_CTX, "%s", display);
5430 free(display);
5431 return ERROR_OK;
5432 } else {
5433 LOG_INFO("failed");
5434 return ERROR_TARGET_FAILURE;
5438 static const struct command_registration target_exec_command_handlers[] = {
5440 .name = "fast_load_image",
5441 .handler = handle_fast_load_image_command,
5442 .mode = COMMAND_ANY,
5443 .help = "Load image into server memory for later use by "
5444 "fast_load; primarily for profiling",
5445 .usage = "filename address ['bin'|'ihex'|'elf'|'s19'] "
5446 "[min_address [max_length]]",
5449 .name = "fast_load",
5450 .handler = handle_fast_load_command,
5451 .mode = COMMAND_EXEC,
5452 .help = "loads active fast load image to current target "
5453 "- mainly for profiling purposes",
5454 .usage = "",
5457 .name = "profile",
5458 .handler = handle_profile_command,
5459 .mode = COMMAND_EXEC,
5460 .usage = "seconds filename",
5461 .help = "profiling samples the CPU PC",
5463 /** @todo don't register virt2phys() unless target supports it */
5465 .name = "virt2phys",
5466 .handler = handle_virt2phys_command,
5467 .mode = COMMAND_ANY,
5468 .help = "translate a virtual address into a physical address",
5469 .usage = "virtual_address",
5472 .name = "reg",
5473 .handler = handle_reg_command,
5474 .mode = COMMAND_EXEC,
5475 .help = "display or set a register; with no arguments, "
5476 "displays all registers and their values",
5477 .usage = "[(register_name|register_number) [value]]",
5480 .name = "poll",
5481 .handler = handle_poll_command,
5482 .mode = COMMAND_EXEC,
5483 .help = "poll target state; or reconfigure background polling",
5484 .usage = "['on'|'off']",
5487 .name = "wait_halt",
5488 .handler = handle_wait_halt_command,
5489 .mode = COMMAND_EXEC,
5490 .help = "wait up to the specified number of milliseconds "
5491 "(default 5) for a previously requested halt",
5492 .usage = "[milliseconds]",
5495 .name = "halt",
5496 .handler = handle_halt_command,
5497 .mode = COMMAND_EXEC,
5498 .help = "request target to halt, then wait up to the specified"
5499 "number of milliseconds (default 5) for it to complete",
5500 .usage = "[milliseconds]",
5503 .name = "resume",
5504 .handler = handle_resume_command,
5505 .mode = COMMAND_EXEC,
5506 .help = "resume target execution from current PC or address",
5507 .usage = "[address]",
5510 .name = "reset",
5511 .handler = handle_reset_command,
5512 .mode = COMMAND_EXEC,
5513 .usage = "[run|halt|init]",
5514 .help = "Reset all targets into the specified mode."
5515 "Default reset mode is run, if not given.",
5518 .name = "soft_reset_halt",
5519 .handler = handle_soft_reset_halt_command,
5520 .mode = COMMAND_EXEC,
5521 .usage = "",
5522 .help = "halt the target and do a soft reset",
5525 .name = "step",
5526 .handler = handle_step_command,
5527 .mode = COMMAND_EXEC,
5528 .help = "step one instruction from current PC or address",
5529 .usage = "[address]",
5532 .name = "mdw",
5533 .handler = handle_md_command,
5534 .mode = COMMAND_EXEC,
5535 .help = "display memory words",
5536 .usage = "['phys'] address [count]",
5539 .name = "mdh",
5540 .handler = handle_md_command,
5541 .mode = COMMAND_EXEC,
5542 .help = "display memory half-words",
5543 .usage = "['phys'] address [count]",
5546 .name = "mdb",
5547 .handler = handle_md_command,
5548 .mode = COMMAND_EXEC,
5549 .help = "display memory bytes",
5550 .usage = "['phys'] address [count]",
5553 .name = "mww",
5554 .handler = handle_mw_command,
5555 .mode = COMMAND_EXEC,
5556 .help = "write memory word",
5557 .usage = "['phys'] address value [count]",
5560 .name = "mwh",
5561 .handler = handle_mw_command,
5562 .mode = COMMAND_EXEC,
5563 .help = "write memory half-word",
5564 .usage = "['phys'] address value [count]",
5567 .name = "mwb",
5568 .handler = handle_mw_command,
5569 .mode = COMMAND_EXEC,
5570 .help = "write memory byte",
5571 .usage = "['phys'] address value [count]",
5574 .name = "bp",
5575 .handler = handle_bp_command,
5576 .mode = COMMAND_EXEC,
5577 .help = "list or set hardware or software breakpoint",
5578 .usage = "<address> [<asid>]<length> ['hw'|'hw_ctx']",
5581 .name = "rbp",
5582 .handler = handle_rbp_command,
5583 .mode = COMMAND_EXEC,
5584 .help = "remove breakpoint",
5585 .usage = "address",
5588 .name = "wp",
5589 .handler = handle_wp_command,
5590 .mode = COMMAND_EXEC,
5591 .help = "list (no params) or create watchpoints",
5592 .usage = "[address length [('r'|'w'|'a') value [mask]]]",
5595 .name = "rwp",
5596 .handler = handle_rwp_command,
5597 .mode = COMMAND_EXEC,
5598 .help = "remove watchpoint",
5599 .usage = "address",
5602 .name = "load_image",
5603 .handler = handle_load_image_command,
5604 .mode = COMMAND_EXEC,
5605 .usage = "filename address ['bin'|'ihex'|'elf'|'s19'] "
5606 "[min_address] [max_length]",
5609 .name = "dump_image",
5610 .handler = handle_dump_image_command,
5611 .mode = COMMAND_EXEC,
5612 .usage = "filename address size",
5615 .name = "verify_image",
5616 .handler = handle_verify_image_command,
5617 .mode = COMMAND_EXEC,
5618 .usage = "filename [offset [type]]",
5621 .name = "test_image",
5622 .handler = handle_test_image_command,
5623 .mode = COMMAND_EXEC,
5624 .usage = "filename [offset [type]]",
5627 .name = "mem2array",
5628 .mode = COMMAND_EXEC,
5629 .jim_handler = jim_mem2array,
5630 .help = "read 8/16/32 bit memory and return as a TCL array "
5631 "for script processing",
5632 .usage = "arrayname bitwidth address count",
5635 .name = "array2mem",
5636 .mode = COMMAND_EXEC,
5637 .jim_handler = jim_array2mem,
5638 .help = "convert a TCL array to memory locations "
5639 "and write the 8/16/32 bit values",
5640 .usage = "arrayname bitwidth address count",
5643 .name = "reset_nag",
5644 .handler = handle_target_reset_nag,
5645 .mode = COMMAND_ANY,
5646 .help = "Nag after each reset about options that could have been "
5647 "enabled to improve performance. ",
5648 .usage = "['enable'|'disable']",
5651 .name = "ps",
5652 .handler = handle_ps_command,
5653 .mode = COMMAND_EXEC,
5654 .help = "list all tasks ",
5655 .usage = " ",
5658 COMMAND_REGISTRATION_DONE
5660 static int target_register_user_commands(struct command_context *cmd_ctx)
5662 int retval = ERROR_OK;
5663 retval = target_request_register_commands(cmd_ctx);
5664 if (retval != ERROR_OK)
5665 return retval;
5667 retval = trace_register_commands(cmd_ctx);
5668 if (retval != ERROR_OK)
5669 return retval;
5672 return register_commands(cmd_ctx, NULL, target_exec_command_handlers);