target: further shrink Jim-awareness
[openocd.git] / src / target / target.c
blob740db0f189200195a2e461fe3c016c6d64652dd7
1 /***************************************************************************
2 * Copyright (C) 2005 by Dominic Rath *
3 * Dominic.Rath@gmx.de *
4 * *
5 * Copyright (C) 2007-2009 Ø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 * This program is free software; you can redistribute it and/or modify *
18 * it under the terms of the GNU General Public License as published by *
19 * the Free Software Foundation; either version 2 of the License, or *
20 * (at your option) any later version. *
21 * *
22 * This program is distributed in the hope that it will be useful, *
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
25 * GNU General Public License for more details. *
26 * *
27 * You should have received a copy of the GNU General Public License *
28 * along with this program; if not, write to the *
29 * Free Software Foundation, Inc., *
30 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
31 ***************************************************************************/
32 #ifdef HAVE_CONFIG_H
33 #include "config.h"
34 #endif
36 #include <helper/time_support.h>
37 #include <jtag/jtag.h>
39 #include "target.h"
40 #include "target_type.h"
41 #include "target_request.h"
42 #include "breakpoints.h"
43 #include "register.h"
44 #include "trace.h"
45 #include "image.h"
48 static int target_array2mem(Jim_Interp *interp, struct target *target, int argc, Jim_Obj *const *argv);
49 static int target_mem2array(Jim_Interp *interp, struct target *target, int argc, Jim_Obj *const *argv);
51 /* targets */
52 extern struct target_type arm7tdmi_target;
53 extern struct target_type arm720t_target;
54 extern struct target_type arm9tdmi_target;
55 extern struct target_type arm920t_target;
56 extern struct target_type arm966e_target;
57 extern struct target_type arm926ejs_target;
58 extern struct target_type fa526_target;
59 extern struct target_type feroceon_target;
60 extern struct target_type dragonite_target;
61 extern struct target_type xscale_target;
62 extern struct target_type cortexm3_target;
63 extern struct target_type cortexa8_target;
64 extern struct target_type arm11_target;
65 extern struct target_type mips_m4k_target;
66 extern struct target_type avr_target;
67 extern struct target_type testee_target;
69 struct target_type *target_types[] =
71 &arm7tdmi_target,
72 &arm9tdmi_target,
73 &arm920t_target,
74 &arm720t_target,
75 &arm966e_target,
76 &arm926ejs_target,
77 &fa526_target,
78 &feroceon_target,
79 &dragonite_target,
80 &xscale_target,
81 &cortexm3_target,
82 &cortexa8_target,
83 &arm11_target,
84 &mips_m4k_target,
85 &avr_target,
86 &testee_target,
87 NULL,
90 struct target *all_targets = NULL;
91 struct target_event_callback *target_event_callbacks = NULL;
92 struct target_timer_callback *target_timer_callbacks = NULL;
94 static const Jim_Nvp nvp_assert[] = {
95 { .name = "assert", NVP_ASSERT },
96 { .name = "deassert", NVP_DEASSERT },
97 { .name = "T", NVP_ASSERT },
98 { .name = "F", NVP_DEASSERT },
99 { .name = "t", NVP_ASSERT },
100 { .name = "f", NVP_DEASSERT },
101 { .name = NULL, .value = -1 }
104 static const Jim_Nvp nvp_error_target[] = {
105 { .value = ERROR_TARGET_INVALID, .name = "err-invalid" },
106 { .value = ERROR_TARGET_INIT_FAILED, .name = "err-init-failed" },
107 { .value = ERROR_TARGET_TIMEOUT, .name = "err-timeout" },
108 { .value = ERROR_TARGET_NOT_HALTED, .name = "err-not-halted" },
109 { .value = ERROR_TARGET_FAILURE, .name = "err-failure" },
110 { .value = ERROR_TARGET_UNALIGNED_ACCESS , .name = "err-unaligned-access" },
111 { .value = ERROR_TARGET_DATA_ABORT , .name = "err-data-abort" },
112 { .value = ERROR_TARGET_RESOURCE_NOT_AVAILABLE , .name = "err-resource-not-available" },
113 { .value = ERROR_TARGET_TRANSLATION_FAULT , .name = "err-translation-fault" },
114 { .value = ERROR_TARGET_NOT_RUNNING, .name = "err-not-running" },
115 { .value = ERROR_TARGET_NOT_EXAMINED, .name = "err-not-examined" },
116 { .value = -1, .name = NULL }
119 const char *target_strerror_safe(int err)
121 const Jim_Nvp *n;
123 n = Jim_Nvp_value2name_simple(nvp_error_target, err);
124 if (n->name == NULL) {
125 return "unknown";
126 } else {
127 return n->name;
131 static const Jim_Nvp nvp_target_event[] = {
132 { .value = TARGET_EVENT_OLD_gdb_program_config , .name = "old-gdb_program_config" },
133 { .value = TARGET_EVENT_OLD_pre_resume , .name = "old-pre_resume" },
135 { .value = TARGET_EVENT_GDB_HALT, .name = "gdb-halt" },
136 { .value = TARGET_EVENT_HALTED, .name = "halted" },
137 { .value = TARGET_EVENT_RESUMED, .name = "resumed" },
138 { .value = TARGET_EVENT_RESUME_START, .name = "resume-start" },
139 { .value = TARGET_EVENT_RESUME_END, .name = "resume-end" },
141 { .name = "gdb-start", .value = TARGET_EVENT_GDB_START },
142 { .name = "gdb-end", .value = TARGET_EVENT_GDB_END },
144 /* historical name */
146 { .value = TARGET_EVENT_RESET_START, .name = "reset-start" },
148 { .value = TARGET_EVENT_RESET_ASSERT_PRE, .name = "reset-assert-pre" },
149 { .value = TARGET_EVENT_RESET_ASSERT, .name = "reset-assert" },
150 { .value = TARGET_EVENT_RESET_ASSERT_POST, .name = "reset-assert-post" },
151 { .value = TARGET_EVENT_RESET_DEASSERT_PRE, .name = "reset-deassert-pre" },
152 { .value = TARGET_EVENT_RESET_DEASSERT_POST, .name = "reset-deassert-post" },
153 { .value = TARGET_EVENT_RESET_HALT_PRE, .name = "reset-halt-pre" },
154 { .value = TARGET_EVENT_RESET_HALT_POST, .name = "reset-halt-post" },
155 { .value = TARGET_EVENT_RESET_WAIT_PRE, .name = "reset-wait-pre" },
156 { .value = TARGET_EVENT_RESET_WAIT_POST, .name = "reset-wait-post" },
157 { .value = TARGET_EVENT_RESET_INIT, .name = "reset-init" },
158 { .value = TARGET_EVENT_RESET_END, .name = "reset-end" },
160 { .value = TARGET_EVENT_EXAMINE_START, .name = "examine-start" },
161 { .value = TARGET_EVENT_EXAMINE_END, .name = "examine-end" },
163 { .value = TARGET_EVENT_DEBUG_HALTED, .name = "debug-halted" },
164 { .value = TARGET_EVENT_DEBUG_RESUMED, .name = "debug-resumed" },
166 { .value = TARGET_EVENT_GDB_ATTACH, .name = "gdb-attach" },
167 { .value = TARGET_EVENT_GDB_DETACH, .name = "gdb-detach" },
169 { .value = TARGET_EVENT_GDB_FLASH_WRITE_START, .name = "gdb-flash-write-start" },
170 { .value = TARGET_EVENT_GDB_FLASH_WRITE_END , .name = "gdb-flash-write-end" },
172 { .value = TARGET_EVENT_GDB_FLASH_ERASE_START, .name = "gdb-flash-erase-start" },
173 { .value = TARGET_EVENT_GDB_FLASH_ERASE_END , .name = "gdb-flash-erase-end" },
175 { .value = TARGET_EVENT_RESUME_START, .name = "resume-start" },
176 { .value = TARGET_EVENT_RESUMED , .name = "resume-ok" },
177 { .value = TARGET_EVENT_RESUME_END , .name = "resume-end" },
179 { .name = NULL, .value = -1 }
182 static const Jim_Nvp nvp_target_state[] = {
183 { .name = "unknown", .value = TARGET_UNKNOWN },
184 { .name = "running", .value = TARGET_RUNNING },
185 { .name = "halted", .value = TARGET_HALTED },
186 { .name = "reset", .value = TARGET_RESET },
187 { .name = "debug-running", .value = TARGET_DEBUG_RUNNING },
188 { .name = NULL, .value = -1 },
191 static const Jim_Nvp nvp_target_debug_reason [] = {
192 { .name = "debug-request" , .value = DBG_REASON_DBGRQ },
193 { .name = "breakpoint" , .value = DBG_REASON_BREAKPOINT },
194 { .name = "watchpoint" , .value = DBG_REASON_WATCHPOINT },
195 { .name = "watchpoint-and-breakpoint", .value = DBG_REASON_WPTANDBKPT },
196 { .name = "single-step" , .value = DBG_REASON_SINGLESTEP },
197 { .name = "target-not-halted" , .value = DBG_REASON_NOTHALTED },
198 { .name = "undefined" , .value = DBG_REASON_UNDEFINED },
199 { .name = NULL, .value = -1 },
202 static const Jim_Nvp nvp_target_endian[] = {
203 { .name = "big", .value = TARGET_BIG_ENDIAN },
204 { .name = "little", .value = TARGET_LITTLE_ENDIAN },
205 { .name = "be", .value = TARGET_BIG_ENDIAN },
206 { .name = "le", .value = TARGET_LITTLE_ENDIAN },
207 { .name = NULL, .value = -1 },
210 static const Jim_Nvp nvp_reset_modes[] = {
211 { .name = "unknown", .value = RESET_UNKNOWN },
212 { .name = "run" , .value = RESET_RUN },
213 { .name = "halt" , .value = RESET_HALT },
214 { .name = "init" , .value = RESET_INIT },
215 { .name = NULL , .value = -1 },
218 const char *debug_reason_name(struct target *t)
220 const char *cp;
222 cp = Jim_Nvp_value2name_simple(nvp_target_debug_reason,
223 t->debug_reason)->name;
224 if (!cp) {
225 LOG_ERROR("Invalid debug reason: %d", (int)(t->debug_reason));
226 cp = "(*BUG*unknown*BUG*)";
228 return cp;
231 const char *
232 target_state_name( struct target *t )
234 const char *cp;
235 cp = Jim_Nvp_value2name_simple(nvp_target_state, t->state)->name;
236 if( !cp ){
237 LOG_ERROR("Invalid target state: %d", (int)(t->state));
238 cp = "(*BUG*unknown*BUG*)";
240 return cp;
243 /* determine the number of the new target */
244 static int new_target_number(void)
246 struct target *t;
247 int x;
249 /* number is 0 based */
250 x = -1;
251 t = all_targets;
252 while (t) {
253 if (x < t->target_number) {
254 x = t->target_number;
256 t = t->next;
258 return x + 1;
261 /* read a uint32_t from a buffer in target memory endianness */
262 uint32_t target_buffer_get_u32(struct target *target, const uint8_t *buffer)
264 if (target->endianness == TARGET_LITTLE_ENDIAN)
265 return le_to_h_u32(buffer);
266 else
267 return be_to_h_u32(buffer);
270 /* read a uint16_t from a buffer in target memory endianness */
271 uint16_t target_buffer_get_u16(struct target *target, const uint8_t *buffer)
273 if (target->endianness == TARGET_LITTLE_ENDIAN)
274 return le_to_h_u16(buffer);
275 else
276 return be_to_h_u16(buffer);
279 /* read a uint8_t from a buffer in target memory endianness */
280 uint8_t target_buffer_get_u8(struct target *target, const uint8_t *buffer)
282 return *buffer & 0x0ff;
285 /* write a uint32_t to a buffer in target memory endianness */
286 void target_buffer_set_u32(struct target *target, uint8_t *buffer, uint32_t value)
288 if (target->endianness == TARGET_LITTLE_ENDIAN)
289 h_u32_to_le(buffer, value);
290 else
291 h_u32_to_be(buffer, value);
294 /* write a uint16_t to a buffer in target memory endianness */
295 void target_buffer_set_u16(struct target *target, uint8_t *buffer, uint16_t value)
297 if (target->endianness == TARGET_LITTLE_ENDIAN)
298 h_u16_to_le(buffer, value);
299 else
300 h_u16_to_be(buffer, value);
303 /* write a uint8_t to a buffer in target memory endianness */
304 void target_buffer_set_u8(struct target *target, uint8_t *buffer, uint8_t value)
306 *buffer = value;
309 /* return a pointer to a configured target; id is name or number */
310 struct target *get_target(const char *id)
312 struct target *target;
314 /* try as tcltarget name */
315 for (target = all_targets; target; target = target->next) {
316 if (target->cmd_name == NULL)
317 continue;
318 if (strcmp(id, target->cmd_name) == 0)
319 return target;
322 /* It's OK to remove this fallback sometime after August 2010 or so */
324 /* no match, try as number */
325 unsigned num;
326 if (parse_uint(id, &num) != ERROR_OK)
327 return NULL;
329 for (target = all_targets; target; target = target->next) {
330 if (target->target_number == (int)num) {
331 LOG_WARNING("use '%s' as target identifier, not '%u'",
332 target->cmd_name, num);
333 return target;
337 return NULL;
340 /* returns a pointer to the n-th configured target */
341 static struct target *get_target_by_num(int num)
343 struct target *target = all_targets;
345 while (target) {
346 if (target->target_number == num) {
347 return target;
349 target = target->next;
352 return NULL;
355 struct target* get_current_target(struct command_context *cmd_ctx)
357 struct target *target = get_target_by_num(cmd_ctx->current_target);
359 if (target == NULL)
361 LOG_ERROR("BUG: current_target out of bounds");
362 exit(-1);
365 return target;
368 int target_poll(struct target *target)
370 int retval;
372 /* We can't poll until after examine */
373 if (!target_was_examined(target))
375 /* Fail silently lest we pollute the log */
376 return ERROR_FAIL;
379 retval = target->type->poll(target);
380 if (retval != ERROR_OK)
381 return retval;
383 if (target->halt_issued)
385 if (target->state == TARGET_HALTED)
387 target->halt_issued = false;
388 } else
390 long long t = timeval_ms() - target->halt_issued_time;
391 if (t>1000)
393 target->halt_issued = false;
394 LOG_INFO("Halt timed out, wake up GDB.");
395 target_call_event_callbacks(target, TARGET_EVENT_GDB_HALT);
400 return ERROR_OK;
403 int target_halt(struct target *target)
405 int retval;
406 /* We can't poll until after examine */
407 if (!target_was_examined(target))
409 LOG_ERROR("Target not examined yet");
410 return ERROR_FAIL;
413 retval = target->type->halt(target);
414 if (retval != ERROR_OK)
415 return retval;
417 target->halt_issued = true;
418 target->halt_issued_time = timeval_ms();
420 return ERROR_OK;
423 int target_resume(struct target *target, int current, uint32_t address, int handle_breakpoints, int debug_execution)
425 int retval;
427 /* We can't poll until after examine */
428 if (!target_was_examined(target))
430 LOG_ERROR("Target not examined yet");
431 return ERROR_FAIL;
434 /* note that resume *must* be asynchronous. The CPU can halt before we poll. The CPU can
435 * even halt at the current PC as a result of a software breakpoint being inserted by (a bug?)
436 * the application.
438 if ((retval = target->type->resume(target, current, address, handle_breakpoints, debug_execution)) != ERROR_OK)
439 return retval;
441 return retval;
444 int target_process_reset(struct command_context *cmd_ctx, enum target_reset_mode reset_mode)
446 char buf[100];
447 int retval;
448 Jim_Nvp *n;
449 n = Jim_Nvp_value2name_simple(nvp_reset_modes, reset_mode);
450 if (n->name == NULL) {
451 LOG_ERROR("invalid reset mode");
452 return ERROR_FAIL;
455 /* disable polling during reset to make reset event scripts
456 * more predictable, i.e. dr/irscan & pathmove in events will
457 * not have JTAG operations injected into the middle of a sequence.
459 bool save_poll = jtag_poll_get_enabled();
461 jtag_poll_set_enabled(false);
463 sprintf(buf, "ocd_process_reset %s", n->name);
464 retval = Jim_Eval(cmd_ctx->interp, buf);
466 jtag_poll_set_enabled(save_poll);
468 if (retval != JIM_OK) {
469 Jim_PrintErrorMessage(cmd_ctx->interp);
470 return ERROR_FAIL;
473 /* We want any events to be processed before the prompt */
474 retval = target_call_timer_callbacks_now();
476 return retval;
479 static int identity_virt2phys(struct target *target,
480 uint32_t virtual, uint32_t *physical)
482 *physical = virtual;
483 return ERROR_OK;
486 static int no_mmu(struct target *target, int *enabled)
488 *enabled = 0;
489 return ERROR_OK;
492 static int default_examine(struct target *target)
494 target_set_examined(target);
495 return ERROR_OK;
498 int target_examine_one(struct target *target)
500 return target->type->examine(target);
503 static int jtag_enable_callback(enum jtag_event event, void *priv)
505 struct target *target = priv;
507 if (event != JTAG_TAP_EVENT_ENABLE || !target->tap->enabled)
508 return ERROR_OK;
510 jtag_unregister_event_callback(jtag_enable_callback, target);
511 return target_examine_one(target);
515 /* Targets that correctly implement init + examine, i.e.
516 * no communication with target during init:
518 * XScale
520 int target_examine(void)
522 int retval = ERROR_OK;
523 struct target *target;
525 for (target = all_targets; target; target = target->next)
527 /* defer examination, but don't skip it */
528 if (!target->tap->enabled) {
529 jtag_register_event_callback(jtag_enable_callback,
530 target);
531 continue;
533 if ((retval = target_examine_one(target)) != ERROR_OK)
534 return retval;
536 return retval;
538 const char *target_type_name(struct target *target)
540 return target->type->name;
543 static int target_write_memory_imp(struct target *target, uint32_t address, uint32_t size, uint32_t count, uint8_t *buffer)
545 if (!target_was_examined(target))
547 LOG_ERROR("Target not examined yet");
548 return ERROR_FAIL;
550 return target->type->write_memory_imp(target, address, size, count, buffer);
553 static int target_read_memory_imp(struct target *target, uint32_t address, uint32_t size, uint32_t count, uint8_t *buffer)
555 if (!target_was_examined(target))
557 LOG_ERROR("Target not examined yet");
558 return ERROR_FAIL;
560 return target->type->read_memory_imp(target, address, size, count, buffer);
563 static int target_soft_reset_halt_imp(struct target *target)
565 if (!target_was_examined(target))
567 LOG_ERROR("Target not examined yet");
568 return ERROR_FAIL;
570 if (!target->type->soft_reset_halt_imp) {
571 LOG_ERROR("Target %s does not support soft_reset_halt",
572 target_name(target));
573 return ERROR_FAIL;
575 return target->type->soft_reset_halt_imp(target);
578 static int target_run_algorithm_imp(struct target *target, int num_mem_params, struct mem_param *mem_params, int num_reg_params, struct reg_param *reg_param, uint32_t entry_point, uint32_t exit_point, int timeout_ms, void *arch_info)
580 if (!target_was_examined(target))
582 LOG_ERROR("Target not examined yet");
583 return ERROR_FAIL;
585 return target->type->run_algorithm_imp(target, num_mem_params, mem_params, num_reg_params, reg_param, entry_point, exit_point, timeout_ms, arch_info);
588 int target_read_memory(struct target *target,
589 uint32_t address, uint32_t size, uint32_t count, uint8_t *buffer)
591 return target->type->read_memory(target, address, size, count, buffer);
594 int target_read_phys_memory(struct target *target,
595 uint32_t address, uint32_t size, uint32_t count, uint8_t *buffer)
597 return target->type->read_phys_memory(target, address, size, count, buffer);
600 int target_write_memory(struct target *target,
601 uint32_t address, uint32_t size, uint32_t count, uint8_t *buffer)
603 return target->type->write_memory(target, address, size, count, buffer);
606 int target_write_phys_memory(struct target *target,
607 uint32_t address, uint32_t size, uint32_t count, uint8_t *buffer)
609 return target->type->write_phys_memory(target, address, size, count, buffer);
612 int target_bulk_write_memory(struct target *target,
613 uint32_t address, uint32_t count, uint8_t *buffer)
615 return target->type->bulk_write_memory(target, address, count, buffer);
618 int target_add_breakpoint(struct target *target,
619 struct breakpoint *breakpoint)
621 if (target->state != TARGET_HALTED) {
622 LOG_WARNING("target %s is not halted", target->cmd_name);
623 return ERROR_TARGET_NOT_HALTED;
625 return target->type->add_breakpoint(target, breakpoint);
627 int target_remove_breakpoint(struct target *target,
628 struct breakpoint *breakpoint)
630 return target->type->remove_breakpoint(target, breakpoint);
633 int target_add_watchpoint(struct target *target,
634 struct watchpoint *watchpoint)
636 if (target->state != TARGET_HALTED) {
637 LOG_WARNING("target %s is not halted", target->cmd_name);
638 return ERROR_TARGET_NOT_HALTED;
640 return target->type->add_watchpoint(target, watchpoint);
642 int target_remove_watchpoint(struct target *target,
643 struct watchpoint *watchpoint)
645 return target->type->remove_watchpoint(target, watchpoint);
648 int target_get_gdb_reg_list(struct target *target,
649 struct reg **reg_list[], int *reg_list_size)
651 return target->type->get_gdb_reg_list(target, reg_list, reg_list_size);
653 int target_step(struct target *target,
654 int current, uint32_t address, int handle_breakpoints)
656 return target->type->step(target, current, address, handle_breakpoints);
660 int target_run_algorithm(struct target *target,
661 int num_mem_params, struct mem_param *mem_params,
662 int num_reg_params, struct reg_param *reg_param,
663 uint32_t entry_point, uint32_t exit_point,
664 int timeout_ms, void *arch_info)
666 return target->type->run_algorithm(target,
667 num_mem_params, mem_params, num_reg_params, reg_param,
668 entry_point, exit_point, timeout_ms, arch_info);
672 * Reset the @c examined flag for the given target.
673 * Pure paranoia -- targets are zeroed on allocation.
675 static void target_reset_examined(struct target *target)
677 target->examined = false;
680 static int
681 err_read_phys_memory(struct target *target, uint32_t address,
682 uint32_t size, uint32_t count, uint8_t *buffer)
684 LOG_ERROR("Not implemented: %s", __func__);
685 return ERROR_FAIL;
688 static int
689 err_write_phys_memory(struct target *target, uint32_t address,
690 uint32_t size, uint32_t count, uint8_t *buffer)
692 LOG_ERROR("Not implemented: %s", __func__);
693 return ERROR_FAIL;
696 static int handle_target(void *priv);
698 static int target_init_one(struct command_context *cmd_ctx,
699 struct target *target)
701 target_reset_examined(target);
703 struct target_type *type = target->type;
704 if (type->examine == NULL)
705 type->examine = default_examine;
707 int retval = type->init_target(cmd_ctx, target);
708 if (ERROR_OK != retval)
710 LOG_ERROR("target '%s' init failed", target_name(target));
711 return retval;
715 * @todo get rid of those *memory_imp() methods, now that all
716 * callers are using target_*_memory() accessors ... and make
717 * sure the "physical" paths handle the same issues.
719 /* a non-invasive way(in terms of patches) to add some code that
720 * runs before the type->write/read_memory implementation
722 type->write_memory_imp = target->type->write_memory;
723 type->write_memory = target_write_memory_imp;
725 type->read_memory_imp = target->type->read_memory;
726 type->read_memory = target_read_memory_imp;
728 type->soft_reset_halt_imp = target->type->soft_reset_halt;
729 type->soft_reset_halt = target_soft_reset_halt_imp;
731 type->run_algorithm_imp = target->type->run_algorithm;
732 type->run_algorithm = target_run_algorithm_imp;
734 /* Sanity-check MMU support ... stub in what we must, to help
735 * implement it in stages, but warn if we need to do so.
737 if (type->mmu)
739 if (type->write_phys_memory == NULL)
741 LOG_ERROR("type '%s' is missing write_phys_memory",
742 type->name);
743 type->write_phys_memory = err_write_phys_memory;
745 if (type->read_phys_memory == NULL)
747 LOG_ERROR("type '%s' is missing read_phys_memory",
748 type->name);
749 type->read_phys_memory = err_read_phys_memory;
751 if (type->virt2phys == NULL)
753 LOG_ERROR("type '%s' is missing virt2phys", type->name);
754 type->virt2phys = identity_virt2phys;
757 else
759 /* Make sure no-MMU targets all behave the same: make no
760 * distinction between physical and virtual addresses, and
761 * ensure that virt2phys() is always an identity mapping.
763 if (type->write_phys_memory || type->read_phys_memory
764 || type->virt2phys)
766 LOG_WARNING("type '%s' has bad MMU hooks", type->name);
769 type->mmu = no_mmu;
770 type->write_phys_memory = type->write_memory;
771 type->read_phys_memory = type->read_memory;
772 type->virt2phys = identity_virt2phys;
774 return ERROR_OK;
777 int target_init(struct command_context *cmd_ctx)
779 struct target *target;
780 int retval;
782 for (target = all_targets; target; target = target->next)
784 retval = target_init_one(cmd_ctx, target);
785 if (ERROR_OK != retval)
786 return retval;
789 if (!all_targets)
790 return ERROR_OK;
792 retval = target_register_user_commands(cmd_ctx);
793 if (ERROR_OK != retval)
794 return retval;
796 retval = target_register_timer_callback(&handle_target,
797 100, 1, cmd_ctx->interp);
798 if (ERROR_OK != retval)
799 return retval;
801 return ERROR_OK;
804 COMMAND_HANDLER(handle_target_init_command)
806 if (CMD_ARGC != 0)
807 return ERROR_COMMAND_SYNTAX_ERROR;
809 static bool target_initialized = false;
810 if (target_initialized)
812 LOG_INFO("'target init' has already been called");
813 return ERROR_OK;
815 target_initialized = true;
817 LOG_DEBUG("Initializing targets...");
818 return target_init(CMD_CTX);
821 int target_register_event_callback(int (*callback)(struct target *target, enum target_event event, void *priv), void *priv)
823 struct target_event_callback **callbacks_p = &target_event_callbacks;
825 if (callback == NULL)
827 return ERROR_INVALID_ARGUMENTS;
830 if (*callbacks_p)
832 while ((*callbacks_p)->next)
833 callbacks_p = &((*callbacks_p)->next);
834 callbacks_p = &((*callbacks_p)->next);
837 (*callbacks_p) = malloc(sizeof(struct target_event_callback));
838 (*callbacks_p)->callback = callback;
839 (*callbacks_p)->priv = priv;
840 (*callbacks_p)->next = NULL;
842 return ERROR_OK;
845 int target_register_timer_callback(int (*callback)(void *priv), int time_ms, int periodic, void *priv)
847 struct target_timer_callback **callbacks_p = &target_timer_callbacks;
848 struct timeval now;
850 if (callback == NULL)
852 return ERROR_INVALID_ARGUMENTS;
855 if (*callbacks_p)
857 while ((*callbacks_p)->next)
858 callbacks_p = &((*callbacks_p)->next);
859 callbacks_p = &((*callbacks_p)->next);
862 (*callbacks_p) = malloc(sizeof(struct target_timer_callback));
863 (*callbacks_p)->callback = callback;
864 (*callbacks_p)->periodic = periodic;
865 (*callbacks_p)->time_ms = time_ms;
867 gettimeofday(&now, NULL);
868 (*callbacks_p)->when.tv_usec = now.tv_usec + (time_ms % 1000) * 1000;
869 time_ms -= (time_ms % 1000);
870 (*callbacks_p)->when.tv_sec = now.tv_sec + (time_ms / 1000);
871 if ((*callbacks_p)->when.tv_usec > 1000000)
873 (*callbacks_p)->when.tv_usec = (*callbacks_p)->when.tv_usec - 1000000;
874 (*callbacks_p)->when.tv_sec += 1;
877 (*callbacks_p)->priv = priv;
878 (*callbacks_p)->next = NULL;
880 return ERROR_OK;
883 int target_unregister_event_callback(int (*callback)(struct target *target, enum target_event event, void *priv), void *priv)
885 struct target_event_callback **p = &target_event_callbacks;
886 struct target_event_callback *c = target_event_callbacks;
888 if (callback == NULL)
890 return ERROR_INVALID_ARGUMENTS;
893 while (c)
895 struct target_event_callback *next = c->next;
896 if ((c->callback == callback) && (c->priv == priv))
898 *p = next;
899 free(c);
900 return ERROR_OK;
902 else
903 p = &(c->next);
904 c = next;
907 return ERROR_OK;
910 int target_unregister_timer_callback(int (*callback)(void *priv), void *priv)
912 struct target_timer_callback **p = &target_timer_callbacks;
913 struct target_timer_callback *c = target_timer_callbacks;
915 if (callback == NULL)
917 return ERROR_INVALID_ARGUMENTS;
920 while (c)
922 struct target_timer_callback *next = c->next;
923 if ((c->callback == callback) && (c->priv == priv))
925 *p = next;
926 free(c);
927 return ERROR_OK;
929 else
930 p = &(c->next);
931 c = next;
934 return ERROR_OK;
937 int target_call_event_callbacks(struct target *target, enum target_event event)
939 struct target_event_callback *callback = target_event_callbacks;
940 struct target_event_callback *next_callback;
942 if (event == TARGET_EVENT_HALTED)
944 /* execute early halted first */
945 target_call_event_callbacks(target, TARGET_EVENT_GDB_HALT);
948 LOG_DEBUG("target event %i (%s)",
949 event,
950 Jim_Nvp_value2name_simple(nvp_target_event, event)->name);
952 target_handle_event(target, event);
954 while (callback)
956 next_callback = callback->next;
957 callback->callback(target, event, callback->priv);
958 callback = next_callback;
961 return ERROR_OK;
964 static int target_timer_callback_periodic_restart(
965 struct target_timer_callback *cb, struct timeval *now)
967 int time_ms = cb->time_ms;
968 cb->when.tv_usec = now->tv_usec + (time_ms % 1000) * 1000;
969 time_ms -= (time_ms % 1000);
970 cb->when.tv_sec = now->tv_sec + time_ms / 1000;
971 if (cb->when.tv_usec > 1000000)
973 cb->when.tv_usec = cb->when.tv_usec - 1000000;
974 cb->when.tv_sec += 1;
976 return ERROR_OK;
979 static int target_call_timer_callback(struct target_timer_callback *cb,
980 struct timeval *now)
982 cb->callback(cb->priv);
984 if (cb->periodic)
985 return target_timer_callback_periodic_restart(cb, now);
987 return target_unregister_timer_callback(cb->callback, cb->priv);
990 static int target_call_timer_callbacks_check_time(int checktime)
992 keep_alive();
994 struct timeval now;
995 gettimeofday(&now, NULL);
997 struct target_timer_callback *callback = target_timer_callbacks;
998 while (callback)
1000 // cleaning up may unregister and free this callback
1001 struct target_timer_callback *next_callback = callback->next;
1003 bool call_it = callback->callback &&
1004 ((!checktime && callback->periodic) ||
1005 now.tv_sec > callback->when.tv_sec ||
1006 (now.tv_sec == callback->when.tv_sec &&
1007 now.tv_usec >= callback->when.tv_usec));
1009 if (call_it)
1011 int retval = target_call_timer_callback(callback, &now);
1012 if (retval != ERROR_OK)
1013 return retval;
1016 callback = next_callback;
1019 return ERROR_OK;
1022 int target_call_timer_callbacks(void)
1024 return target_call_timer_callbacks_check_time(1);
1027 /* invoke periodic callbacks immediately */
1028 int target_call_timer_callbacks_now(void)
1030 return target_call_timer_callbacks_check_time(0);
1033 int target_alloc_working_area(struct target *target, uint32_t size, struct working_area **area)
1035 struct working_area *c = target->working_areas;
1036 struct working_area *new_wa = NULL;
1038 /* Reevaluate working area address based on MMU state*/
1039 if (target->working_areas == NULL)
1041 int retval;
1042 int enabled;
1044 retval = target->type->mmu(target, &enabled);
1045 if (retval != ERROR_OK)
1047 return retval;
1050 if (!enabled) {
1051 if (target->working_area_phys_spec) {
1052 LOG_DEBUG("MMU disabled, using physical "
1053 "address for working memory 0x%08x",
1054 (unsigned)target->working_area_phys);
1055 target->working_area = target->working_area_phys;
1056 } else {
1057 LOG_ERROR("No working memory available. "
1058 "Specify -work-area-phys to target.");
1059 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
1061 } else {
1062 if (target->working_area_virt_spec) {
1063 LOG_DEBUG("MMU enabled, using virtual "
1064 "address for working memory 0x%08x",
1065 (unsigned)target->working_area_virt);
1066 target->working_area = target->working_area_virt;
1067 } else {
1068 LOG_ERROR("No working memory available. "
1069 "Specify -work-area-virt to target.");
1070 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
1075 /* only allocate multiples of 4 byte */
1076 if (size % 4)
1078 LOG_ERROR("BUG: code tried to allocate unaligned number of bytes (0x%08x), padding", ((unsigned)(size)));
1079 size = (size + 3) & (~3);
1082 /* see if there's already a matching working area */
1083 while (c)
1085 if ((c->free) && (c->size == size))
1087 new_wa = c;
1088 break;
1090 c = c->next;
1093 /* if not, allocate a new one */
1094 if (!new_wa)
1096 struct working_area **p = &target->working_areas;
1097 uint32_t first_free = target->working_area;
1098 uint32_t free_size = target->working_area_size;
1100 c = target->working_areas;
1101 while (c)
1103 first_free += c->size;
1104 free_size -= c->size;
1105 p = &c->next;
1106 c = c->next;
1109 if (free_size < size)
1111 LOG_WARNING("not enough working area available(requested %u, free %u)",
1112 (unsigned)(size), (unsigned)(free_size));
1113 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
1116 LOG_DEBUG("allocated new working area at address 0x%08x", (unsigned)first_free);
1118 new_wa = malloc(sizeof(struct working_area));
1119 new_wa->next = NULL;
1120 new_wa->size = size;
1121 new_wa->address = first_free;
1123 if (target->backup_working_area)
1125 int retval;
1126 new_wa->backup = malloc(new_wa->size);
1127 if ((retval = target_read_memory(target, new_wa->address, 4, new_wa->size / 4, new_wa->backup)) != ERROR_OK)
1129 free(new_wa->backup);
1130 free(new_wa);
1131 return retval;
1134 else
1136 new_wa->backup = NULL;
1139 /* put new entry in list */
1140 *p = new_wa;
1143 /* mark as used, and return the new (reused) area */
1144 new_wa->free = 0;
1145 *area = new_wa;
1147 /* user pointer */
1148 new_wa->user = area;
1150 return ERROR_OK;
1153 int target_free_working_area_restore(struct target *target, struct working_area *area, int restore)
1155 if (area->free)
1156 return ERROR_OK;
1158 if (restore && target->backup_working_area)
1160 int retval;
1161 if ((retval = target_write_memory(target, area->address, 4, area->size / 4, area->backup)) != ERROR_OK)
1162 return retval;
1165 area->free = 1;
1167 /* mark user pointer invalid */
1168 *area->user = NULL;
1169 area->user = NULL;
1171 return ERROR_OK;
1174 int target_free_working_area(struct target *target, struct working_area *area)
1176 return target_free_working_area_restore(target, area, 1);
1179 /* free resources and restore memory, if restoring memory fails,
1180 * free up resources anyway
1182 void target_free_all_working_areas_restore(struct target *target, int restore)
1184 struct working_area *c = target->working_areas;
1186 while (c)
1188 struct working_area *next = c->next;
1189 target_free_working_area_restore(target, c, restore);
1191 if (c->backup)
1192 free(c->backup);
1194 free(c);
1196 c = next;
1199 target->working_areas = NULL;
1202 void target_free_all_working_areas(struct target *target)
1204 target_free_all_working_areas_restore(target, 1);
1207 int target_arch_state(struct target *target)
1209 int retval;
1210 if (target == NULL)
1212 LOG_USER("No target has been configured");
1213 return ERROR_OK;
1216 LOG_USER("target state: %s", target_state_name( target ));
1218 if (target->state != TARGET_HALTED)
1219 return ERROR_OK;
1221 retval = target->type->arch_state(target);
1222 return retval;
1225 /* Single aligned words are guaranteed to use 16 or 32 bit access
1226 * mode respectively, otherwise data is handled as quickly as
1227 * possible
1229 int target_write_buffer(struct target *target, uint32_t address, uint32_t size, uint8_t *buffer)
1231 int retval;
1232 LOG_DEBUG("writing buffer of %i byte at 0x%8.8x",
1233 (int)size, (unsigned)address);
1235 if (!target_was_examined(target))
1237 LOG_ERROR("Target not examined yet");
1238 return ERROR_FAIL;
1241 if (size == 0) {
1242 return ERROR_OK;
1245 if ((address + size - 1) < address)
1247 /* GDB can request this when e.g. PC is 0xfffffffc*/
1248 LOG_ERROR("address + size wrapped(0x%08x, 0x%08x)",
1249 (unsigned)address,
1250 (unsigned)size);
1251 return ERROR_FAIL;
1254 if (((address % 2) == 0) && (size == 2))
1256 return target_write_memory(target, address, 2, 1, buffer);
1259 /* handle unaligned head bytes */
1260 if (address % 4)
1262 uint32_t unaligned = 4 - (address % 4);
1264 if (unaligned > size)
1265 unaligned = size;
1267 if ((retval = target_write_memory(target, address, 1, unaligned, buffer)) != ERROR_OK)
1268 return retval;
1270 buffer += unaligned;
1271 address += unaligned;
1272 size -= unaligned;
1275 /* handle aligned words */
1276 if (size >= 4)
1278 int aligned = size - (size % 4);
1280 /* use bulk writes above a certain limit. This may have to be changed */
1281 if (aligned > 128)
1283 if ((retval = target->type->bulk_write_memory(target, address, aligned / 4, buffer)) != ERROR_OK)
1284 return retval;
1286 else
1288 if ((retval = target_write_memory(target, address, 4, aligned / 4, buffer)) != ERROR_OK)
1289 return retval;
1292 buffer += aligned;
1293 address += aligned;
1294 size -= aligned;
1297 /* handle tail writes of less than 4 bytes */
1298 if (size > 0)
1300 if ((retval = target_write_memory(target, address, 1, size, buffer)) != ERROR_OK)
1301 return retval;
1304 return ERROR_OK;
1307 /* Single aligned words are guaranteed to use 16 or 32 bit access
1308 * mode respectively, otherwise data is handled as quickly as
1309 * possible
1311 int target_read_buffer(struct target *target, uint32_t address, uint32_t size, uint8_t *buffer)
1313 int retval;
1314 LOG_DEBUG("reading buffer of %i byte at 0x%8.8x",
1315 (int)size, (unsigned)address);
1317 if (!target_was_examined(target))
1319 LOG_ERROR("Target not examined yet");
1320 return ERROR_FAIL;
1323 if (size == 0) {
1324 return ERROR_OK;
1327 if ((address + size - 1) < address)
1329 /* GDB can request this when e.g. PC is 0xfffffffc*/
1330 LOG_ERROR("address + size wrapped(0x%08" PRIx32 ", 0x%08" PRIx32 ")",
1331 address,
1332 size);
1333 return ERROR_FAIL;
1336 if (((address % 2) == 0) && (size == 2))
1338 return target_read_memory(target, address, 2, 1, buffer);
1341 /* handle unaligned head bytes */
1342 if (address % 4)
1344 uint32_t unaligned = 4 - (address % 4);
1346 if (unaligned > size)
1347 unaligned = size;
1349 if ((retval = target_read_memory(target, address, 1, unaligned, buffer)) != ERROR_OK)
1350 return retval;
1352 buffer += unaligned;
1353 address += unaligned;
1354 size -= unaligned;
1357 /* handle aligned words */
1358 if (size >= 4)
1360 int aligned = size - (size % 4);
1362 if ((retval = target_read_memory(target, address, 4, aligned / 4, buffer)) != ERROR_OK)
1363 return retval;
1365 buffer += aligned;
1366 address += aligned;
1367 size -= aligned;
1370 /*prevent byte access when possible (avoid AHB access limitations in some cases)*/
1371 if(size >=2)
1373 int aligned = size - (size%2);
1374 retval = target_read_memory(target, address, 2, aligned / 2, buffer);
1375 if (retval != ERROR_OK)
1376 return retval;
1378 buffer += aligned;
1379 address += aligned;
1380 size -= aligned;
1382 /* handle tail writes of less than 4 bytes */
1383 if (size > 0)
1385 if ((retval = target_read_memory(target, address, 1, size, buffer)) != ERROR_OK)
1386 return retval;
1389 return ERROR_OK;
1392 int target_checksum_memory(struct target *target, uint32_t address, uint32_t size, uint32_t* crc)
1394 uint8_t *buffer;
1395 int retval;
1396 uint32_t i;
1397 uint32_t checksum = 0;
1398 if (!target_was_examined(target))
1400 LOG_ERROR("Target not examined yet");
1401 return ERROR_FAIL;
1404 if ((retval = target->type->checksum_memory(target, address,
1405 size, &checksum)) != ERROR_OK)
1407 buffer = malloc(size);
1408 if (buffer == NULL)
1410 LOG_ERROR("error allocating buffer for section (%d bytes)", (int)size);
1411 return ERROR_INVALID_ARGUMENTS;
1413 retval = target_read_buffer(target, address, size, buffer);
1414 if (retval != ERROR_OK)
1416 free(buffer);
1417 return retval;
1420 /* convert to target endianess */
1421 for (i = 0; i < (size/sizeof(uint32_t)); i++)
1423 uint32_t target_data;
1424 target_data = target_buffer_get_u32(target, &buffer[i*sizeof(uint32_t)]);
1425 target_buffer_set_u32(target, &buffer[i*sizeof(uint32_t)], target_data);
1428 retval = image_calculate_checksum(buffer, size, &checksum);
1429 free(buffer);
1432 *crc = checksum;
1434 return retval;
1437 int target_blank_check_memory(struct target *target, uint32_t address, uint32_t size, uint32_t* blank)
1439 int retval;
1440 if (!target_was_examined(target))
1442 LOG_ERROR("Target not examined yet");
1443 return ERROR_FAIL;
1446 if (target->type->blank_check_memory == 0)
1447 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
1449 retval = target->type->blank_check_memory(target, address, size, blank);
1451 return retval;
1454 int target_read_u32(struct target *target, uint32_t address, uint32_t *value)
1456 uint8_t value_buf[4];
1457 if (!target_was_examined(target))
1459 LOG_ERROR("Target not examined yet");
1460 return ERROR_FAIL;
1463 int retval = target_read_memory(target, address, 4, 1, value_buf);
1465 if (retval == ERROR_OK)
1467 *value = target_buffer_get_u32(target, value_buf);
1468 LOG_DEBUG("address: 0x%8.8" PRIx32 ", value: 0x%8.8" PRIx32 "",
1469 address,
1470 *value);
1472 else
1474 *value = 0x0;
1475 LOG_DEBUG("address: 0x%8.8" PRIx32 " failed",
1476 address);
1479 return retval;
1482 int target_read_u16(struct target *target, uint32_t address, uint16_t *value)
1484 uint8_t value_buf[2];
1485 if (!target_was_examined(target))
1487 LOG_ERROR("Target not examined yet");
1488 return ERROR_FAIL;
1491 int retval = target_read_memory(target, address, 2, 1, value_buf);
1493 if (retval == ERROR_OK)
1495 *value = target_buffer_get_u16(target, value_buf);
1496 LOG_DEBUG("address: 0x%8.8" PRIx32 ", value: 0x%4.4x",
1497 address,
1498 *value);
1500 else
1502 *value = 0x0;
1503 LOG_DEBUG("address: 0x%8.8" PRIx32 " failed",
1504 address);
1507 return retval;
1510 int target_read_u8(struct target *target, uint32_t address, uint8_t *value)
1512 int retval = target_read_memory(target, address, 1, 1, value);
1513 if (!target_was_examined(target))
1515 LOG_ERROR("Target not examined yet");
1516 return ERROR_FAIL;
1519 if (retval == ERROR_OK)
1521 LOG_DEBUG("address: 0x%8.8" PRIx32 ", value: 0x%2.2x",
1522 address,
1523 *value);
1525 else
1527 *value = 0x0;
1528 LOG_DEBUG("address: 0x%8.8" PRIx32 " failed",
1529 address);
1532 return retval;
1535 int target_write_u32(struct target *target, uint32_t address, uint32_t value)
1537 int retval;
1538 uint8_t value_buf[4];
1539 if (!target_was_examined(target))
1541 LOG_ERROR("Target not examined yet");
1542 return ERROR_FAIL;
1545 LOG_DEBUG("address: 0x%8.8" PRIx32 ", value: 0x%8.8" PRIx32 "",
1546 address,
1547 value);
1549 target_buffer_set_u32(target, value_buf, value);
1550 if ((retval = target_write_memory(target, address, 4, 1, value_buf)) != ERROR_OK)
1552 LOG_DEBUG("failed: %i", retval);
1555 return retval;
1558 int target_write_u16(struct target *target, uint32_t address, uint16_t value)
1560 int retval;
1561 uint8_t value_buf[2];
1562 if (!target_was_examined(target))
1564 LOG_ERROR("Target not examined yet");
1565 return ERROR_FAIL;
1568 LOG_DEBUG("address: 0x%8.8" PRIx32 ", value: 0x%8.8x",
1569 address,
1570 value);
1572 target_buffer_set_u16(target, value_buf, value);
1573 if ((retval = target_write_memory(target, address, 2, 1, value_buf)) != ERROR_OK)
1575 LOG_DEBUG("failed: %i", retval);
1578 return retval;
1581 int target_write_u8(struct target *target, uint32_t address, uint8_t value)
1583 int retval;
1584 if (!target_was_examined(target))
1586 LOG_ERROR("Target not examined yet");
1587 return ERROR_FAIL;
1590 LOG_DEBUG("address: 0x%8.8" PRIx32 ", value: 0x%2.2x",
1591 address, value);
1593 if ((retval = target_write_memory(target, address, 1, 1, &value)) != ERROR_OK)
1595 LOG_DEBUG("failed: %i", retval);
1598 return retval;
1601 COMMAND_HANDLER(handle_targets_command)
1603 struct target *target = all_targets;
1605 if (CMD_ARGC == 1)
1607 target = get_target(CMD_ARGV[0]);
1608 if (target == NULL) {
1609 command_print(CMD_CTX,"Target: %s is unknown, try one of:\n", CMD_ARGV[0]);
1610 goto DumpTargets;
1612 if (!target->tap->enabled) {
1613 command_print(CMD_CTX,"Target: TAP %s is disabled, "
1614 "can't be the current target\n",
1615 target->tap->dotted_name);
1616 return ERROR_FAIL;
1619 CMD_CTX->current_target = target->target_number;
1620 return ERROR_OK;
1622 DumpTargets:
1624 target = all_targets;
1625 command_print(CMD_CTX, " TargetName Type Endian TapName State ");
1626 command_print(CMD_CTX, "-- ------------------ ---------- ------ ------------------ ------------");
1627 while (target)
1629 const char *state;
1630 char marker = ' ';
1632 if (target->tap->enabled)
1633 state = target_state_name( target );
1634 else
1635 state = "tap-disabled";
1637 if (CMD_CTX->current_target == target->target_number)
1638 marker = '*';
1640 /* keep columns lined up to match the headers above */
1641 command_print(CMD_CTX, "%2d%c %-18s %-10s %-6s %-18s %s",
1642 target->target_number,
1643 marker,
1644 target_name(target),
1645 target_type_name(target),
1646 Jim_Nvp_value2name_simple(nvp_target_endian,
1647 target->endianness)->name,
1648 target->tap->dotted_name,
1649 state);
1650 target = target->next;
1653 return ERROR_OK;
1656 /* every 300ms we check for reset & powerdropout and issue a "reset halt" if so. */
1658 static int powerDropout;
1659 static int srstAsserted;
1661 static int runPowerRestore;
1662 static int runPowerDropout;
1663 static int runSrstAsserted;
1664 static int runSrstDeasserted;
1666 static int sense_handler(void)
1668 static int prevSrstAsserted = 0;
1669 static int prevPowerdropout = 0;
1671 int retval;
1672 if ((retval = jtag_power_dropout(&powerDropout)) != ERROR_OK)
1673 return retval;
1675 int powerRestored;
1676 powerRestored = prevPowerdropout && !powerDropout;
1677 if (powerRestored)
1679 runPowerRestore = 1;
1682 long long current = timeval_ms();
1683 static long long lastPower = 0;
1684 int waitMore = lastPower + 2000 > current;
1685 if (powerDropout && !waitMore)
1687 runPowerDropout = 1;
1688 lastPower = current;
1691 if ((retval = jtag_srst_asserted(&srstAsserted)) != ERROR_OK)
1692 return retval;
1694 int srstDeasserted;
1695 srstDeasserted = prevSrstAsserted && !srstAsserted;
1697 static long long lastSrst = 0;
1698 waitMore = lastSrst + 2000 > current;
1699 if (srstDeasserted && !waitMore)
1701 runSrstDeasserted = 1;
1702 lastSrst = current;
1705 if (!prevSrstAsserted && srstAsserted)
1707 runSrstAsserted = 1;
1710 prevSrstAsserted = srstAsserted;
1711 prevPowerdropout = powerDropout;
1713 if (srstDeasserted || powerRestored)
1715 /* Other than logging the event we can't do anything here.
1716 * Issuing a reset is a particularly bad idea as we might
1717 * be inside a reset already.
1721 return ERROR_OK;
1724 static void target_call_event_callbacks_all(enum target_event e) {
1725 struct target *target;
1726 target = all_targets;
1727 while (target) {
1728 target_call_event_callbacks(target, e);
1729 target = target->next;
1733 /* process target state changes */
1734 static int handle_target(void *priv)
1736 Jim_Interp *interp = (Jim_Interp *)priv;
1737 int retval = ERROR_OK;
1739 /* we do not want to recurse here... */
1740 static int recursive = 0;
1741 if (! recursive)
1743 recursive = 1;
1744 sense_handler();
1745 /* danger! running these procedures can trigger srst assertions and power dropouts.
1746 * We need to avoid an infinite loop/recursion here and we do that by
1747 * clearing the flags after running these events.
1749 int did_something = 0;
1750 if (runSrstAsserted)
1752 target_call_event_callbacks_all(TARGET_EVENT_GDB_HALT);
1753 Jim_Eval(interp, "srst_asserted");
1754 did_something = 1;
1756 if (runSrstDeasserted)
1758 Jim_Eval(interp, "srst_deasserted");
1759 did_something = 1;
1761 if (runPowerDropout)
1763 target_call_event_callbacks_all(TARGET_EVENT_GDB_HALT);
1764 Jim_Eval(interp, "power_dropout");
1765 did_something = 1;
1767 if (runPowerRestore)
1769 Jim_Eval(interp, "power_restore");
1770 did_something = 1;
1773 if (did_something)
1775 /* clear detect flags */
1776 sense_handler();
1779 /* clear action flags */
1781 runSrstAsserted = 0;
1782 runSrstDeasserted = 0;
1783 runPowerRestore = 0;
1784 runPowerDropout = 0;
1786 recursive = 0;
1789 /* Poll targets for state changes unless that's globally disabled.
1790 * Skip targets that are currently disabled.
1792 for (struct target *target = all_targets;
1793 is_jtag_poll_safe() && target;
1794 target = target->next)
1796 if (!target->tap->enabled)
1797 continue;
1799 /* only poll target if we've got power and srst isn't asserted */
1800 if (!powerDropout && !srstAsserted)
1802 /* polling may fail silently until the target has been examined */
1803 if ((retval = target_poll(target)) != ERROR_OK)
1805 target_call_event_callbacks(target, TARGET_EVENT_GDB_HALT);
1806 return retval;
1811 return retval;
1814 COMMAND_HANDLER(handle_reg_command)
1816 struct target *target;
1817 struct reg *reg = NULL;
1818 unsigned count = 0;
1819 char *value;
1821 LOG_DEBUG("-");
1823 target = get_current_target(CMD_CTX);
1825 /* list all available registers for the current target */
1826 if (CMD_ARGC == 0)
1828 struct reg_cache *cache = target->reg_cache;
1830 count = 0;
1831 while (cache)
1833 unsigned i;
1835 command_print(CMD_CTX, "===== %s", cache->name);
1837 for (i = 0, reg = cache->reg_list;
1838 i < cache->num_regs;
1839 i++, reg++, count++)
1841 /* only print cached values if they are valid */
1842 if (reg->valid) {
1843 value = buf_to_str(reg->value,
1844 reg->size, 16);
1845 command_print(CMD_CTX,
1846 "(%i) %s (/%" PRIu32 "): 0x%s%s",
1847 count, reg->name,
1848 reg->size, value,
1849 reg->dirty
1850 ? " (dirty)"
1851 : "");
1852 free(value);
1853 } else {
1854 command_print(CMD_CTX, "(%i) %s (/%" PRIu32 ")",
1855 count, reg->name,
1856 reg->size) ;
1859 cache = cache->next;
1862 return ERROR_OK;
1865 /* access a single register by its ordinal number */
1866 if ((CMD_ARGV[0][0] >= '0') && (CMD_ARGV[0][0] <= '9'))
1868 unsigned num;
1869 COMMAND_PARSE_NUMBER(uint, CMD_ARGV[0], num);
1871 struct reg_cache *cache = target->reg_cache;
1872 count = 0;
1873 while (cache)
1875 unsigned i;
1876 for (i = 0; i < cache->num_regs; i++)
1878 if (count++ == num)
1880 reg = &cache->reg_list[i];
1881 break;
1884 if (reg)
1885 break;
1886 cache = cache->next;
1889 if (!reg)
1891 command_print(CMD_CTX, "%i is out of bounds, the current target has only %i registers (0 - %i)", num, count, count - 1);
1892 return ERROR_OK;
1894 } else /* access a single register by its name */
1896 reg = register_get_by_name(target->reg_cache, CMD_ARGV[0], 1);
1898 if (!reg)
1900 command_print(CMD_CTX, "register %s not found in current target", CMD_ARGV[0]);
1901 return ERROR_OK;
1905 /* display a register */
1906 if ((CMD_ARGC == 1) || ((CMD_ARGC == 2) && !((CMD_ARGV[1][0] >= '0') && (CMD_ARGV[1][0] <= '9'))))
1908 if ((CMD_ARGC == 2) && (strcmp(CMD_ARGV[1], "force") == 0))
1909 reg->valid = 0;
1911 if (reg->valid == 0)
1913 reg->type->get(reg);
1915 value = buf_to_str(reg->value, reg->size, 16);
1916 command_print(CMD_CTX, "%s (/%i): 0x%s", reg->name, (int)(reg->size), value);
1917 free(value);
1918 return ERROR_OK;
1921 /* set register value */
1922 if (CMD_ARGC == 2)
1924 uint8_t *buf = malloc(DIV_ROUND_UP(reg->size, 8));
1925 str_to_buf(CMD_ARGV[1], strlen(CMD_ARGV[1]), buf, reg->size, 0);
1927 reg->type->set(reg, buf);
1929 value = buf_to_str(reg->value, reg->size, 16);
1930 command_print(CMD_CTX, "%s (/%i): 0x%s", reg->name, (int)(reg->size), value);
1931 free(value);
1933 free(buf);
1935 return ERROR_OK;
1938 command_print(CMD_CTX, "usage: reg <#|name> [value]");
1940 return ERROR_OK;
1943 COMMAND_HANDLER(handle_poll_command)
1945 int retval = ERROR_OK;
1946 struct target *target = get_current_target(CMD_CTX);
1948 if (CMD_ARGC == 0)
1950 command_print(CMD_CTX, "background polling: %s",
1951 jtag_poll_get_enabled() ? "on" : "off");
1952 command_print(CMD_CTX, "TAP: %s (%s)",
1953 target->tap->dotted_name,
1954 target->tap->enabled ? "enabled" : "disabled");
1955 if (!target->tap->enabled)
1956 return ERROR_OK;
1957 if ((retval = target_poll(target)) != ERROR_OK)
1958 return retval;
1959 if ((retval = target_arch_state(target)) != ERROR_OK)
1960 return retval;
1962 else if (CMD_ARGC == 1)
1964 bool enable;
1965 COMMAND_PARSE_ON_OFF(CMD_ARGV[0], enable);
1966 jtag_poll_set_enabled(enable);
1968 else
1970 return ERROR_COMMAND_SYNTAX_ERROR;
1973 return retval;
1976 COMMAND_HANDLER(handle_wait_halt_command)
1978 if (CMD_ARGC > 1)
1979 return ERROR_COMMAND_SYNTAX_ERROR;
1981 unsigned ms = 5000;
1982 if (1 == CMD_ARGC)
1984 int retval = parse_uint(CMD_ARGV[0], &ms);
1985 if (ERROR_OK != retval)
1987 command_print(CMD_CTX, "usage: %s [seconds]", CMD_NAME);
1988 return ERROR_COMMAND_SYNTAX_ERROR;
1990 // convert seconds (given) to milliseconds (needed)
1991 ms *= 1000;
1994 struct target *target = get_current_target(CMD_CTX);
1995 return target_wait_state(target, TARGET_HALTED, ms);
1998 /* wait for target state to change. The trick here is to have a low
1999 * latency for short waits and not to suck up all the CPU time
2000 * on longer waits.
2002 * After 500ms, keep_alive() is invoked
2004 int target_wait_state(struct target *target, enum target_state state, int ms)
2006 int retval;
2007 long long then = 0, cur;
2008 int once = 1;
2010 for (;;)
2012 if ((retval = target_poll(target)) != ERROR_OK)
2013 return retval;
2014 if (target->state == state)
2016 break;
2018 cur = timeval_ms();
2019 if (once)
2021 once = 0;
2022 then = timeval_ms();
2023 LOG_DEBUG("waiting for target %s...",
2024 Jim_Nvp_value2name_simple(nvp_target_state,state)->name);
2027 if (cur-then > 500)
2029 keep_alive();
2032 if ((cur-then) > ms)
2034 LOG_ERROR("timed out while waiting for target %s",
2035 Jim_Nvp_value2name_simple(nvp_target_state,state)->name);
2036 return ERROR_FAIL;
2040 return ERROR_OK;
2043 COMMAND_HANDLER(handle_halt_command)
2045 LOG_DEBUG("-");
2047 struct target *target = get_current_target(CMD_CTX);
2048 int retval = target_halt(target);
2049 if (ERROR_OK != retval)
2050 return retval;
2052 if (CMD_ARGC == 1)
2054 unsigned wait;
2055 retval = parse_uint(CMD_ARGV[0], &wait);
2056 if (ERROR_OK != retval)
2057 return ERROR_COMMAND_SYNTAX_ERROR;
2058 if (!wait)
2059 return ERROR_OK;
2062 return CALL_COMMAND_HANDLER(handle_wait_halt_command);
2065 COMMAND_HANDLER(handle_soft_reset_halt_command)
2067 struct target *target = get_current_target(CMD_CTX);
2069 LOG_USER("requesting target halt and executing a soft reset");
2071 target->type->soft_reset_halt(target);
2073 return ERROR_OK;
2076 COMMAND_HANDLER(handle_reset_command)
2078 if (CMD_ARGC > 1)
2079 return ERROR_COMMAND_SYNTAX_ERROR;
2081 enum target_reset_mode reset_mode = RESET_RUN;
2082 if (CMD_ARGC == 1)
2084 const Jim_Nvp *n;
2085 n = Jim_Nvp_name2value_simple(nvp_reset_modes, CMD_ARGV[0]);
2086 if ((n->name == NULL) || (n->value == RESET_UNKNOWN)) {
2087 return ERROR_COMMAND_SYNTAX_ERROR;
2089 reset_mode = n->value;
2092 /* reset *all* targets */
2093 return target_process_reset(CMD_CTX, reset_mode);
2097 COMMAND_HANDLER(handle_resume_command)
2099 int current = 1;
2100 if (CMD_ARGC > 1)
2101 return ERROR_COMMAND_SYNTAX_ERROR;
2103 struct target *target = get_current_target(CMD_CTX);
2104 target_handle_event(target, TARGET_EVENT_OLD_pre_resume);
2106 /* with no CMD_ARGV, resume from current pc, addr = 0,
2107 * with one arguments, addr = CMD_ARGV[0],
2108 * handle breakpoints, not debugging */
2109 uint32_t addr = 0;
2110 if (CMD_ARGC == 1)
2112 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], addr);
2113 current = 0;
2116 return target_resume(target, current, addr, 1, 0);
2119 COMMAND_HANDLER(handle_step_command)
2121 if (CMD_ARGC > 1)
2122 return ERROR_COMMAND_SYNTAX_ERROR;
2124 LOG_DEBUG("-");
2126 /* with no CMD_ARGV, step from current pc, addr = 0,
2127 * with one argument addr = CMD_ARGV[0],
2128 * handle breakpoints, debugging */
2129 uint32_t addr = 0;
2130 int current_pc = 1;
2131 if (CMD_ARGC == 1)
2133 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], addr);
2134 current_pc = 0;
2137 struct target *target = get_current_target(CMD_CTX);
2139 return target->type->step(target, current_pc, addr, 1);
2142 static void handle_md_output(struct command_context *cmd_ctx,
2143 struct target *target, uint32_t address, unsigned size,
2144 unsigned count, const uint8_t *buffer)
2146 const unsigned line_bytecnt = 32;
2147 unsigned line_modulo = line_bytecnt / size;
2149 char output[line_bytecnt * 4 + 1];
2150 unsigned output_len = 0;
2152 const char *value_fmt;
2153 switch (size) {
2154 case 4: value_fmt = "%8.8x "; break;
2155 case 2: value_fmt = "%4.2x "; break;
2156 case 1: value_fmt = "%2.2x "; break;
2157 default:
2158 /* "can't happen", caller checked */
2159 LOG_ERROR("invalid memory read size: %u", size);
2160 return;
2163 for (unsigned i = 0; i < count; i++)
2165 if (i % line_modulo == 0)
2167 output_len += snprintf(output + output_len,
2168 sizeof(output) - output_len,
2169 "0x%8.8x: ",
2170 (unsigned)(address + (i*size)));
2173 uint32_t value = 0;
2174 const uint8_t *value_ptr = buffer + i * size;
2175 switch (size) {
2176 case 4: value = target_buffer_get_u32(target, value_ptr); break;
2177 case 2: value = target_buffer_get_u16(target, value_ptr); break;
2178 case 1: value = *value_ptr;
2180 output_len += snprintf(output + output_len,
2181 sizeof(output) - output_len,
2182 value_fmt, value);
2184 if ((i % line_modulo == line_modulo - 1) || (i == count - 1))
2186 command_print(cmd_ctx, "%s", output);
2187 output_len = 0;
2192 COMMAND_HANDLER(handle_md_command)
2194 if (CMD_ARGC < 1)
2195 return ERROR_COMMAND_SYNTAX_ERROR;
2197 unsigned size = 0;
2198 switch (CMD_NAME[2]) {
2199 case 'w': size = 4; break;
2200 case 'h': size = 2; break;
2201 case 'b': size = 1; break;
2202 default: return ERROR_COMMAND_SYNTAX_ERROR;
2205 bool physical=strcmp(CMD_ARGV[0], "phys")==0;
2206 int (*fn)(struct target *target,
2207 uint32_t address, uint32_t size, uint32_t count, uint8_t *buffer);
2208 if (physical)
2210 CMD_ARGC--;
2211 CMD_ARGV++;
2212 fn=target_read_phys_memory;
2213 } else
2215 fn=target_read_memory;
2217 if ((CMD_ARGC < 1) || (CMD_ARGC > 2))
2219 return ERROR_COMMAND_SYNTAX_ERROR;
2222 uint32_t address;
2223 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], address);
2225 unsigned count = 1;
2226 if (CMD_ARGC == 2)
2227 COMMAND_PARSE_NUMBER(uint, CMD_ARGV[1], count);
2229 uint8_t *buffer = calloc(count, size);
2231 struct target *target = get_current_target(CMD_CTX);
2232 int retval = fn(target, address, size, count, buffer);
2233 if (ERROR_OK == retval)
2234 handle_md_output(CMD_CTX, target, address, size, count, buffer);
2236 free(buffer);
2238 return retval;
2241 COMMAND_HANDLER(handle_mw_command)
2243 if (CMD_ARGC < 2)
2245 return ERROR_COMMAND_SYNTAX_ERROR;
2247 bool physical=strcmp(CMD_ARGV[0], "phys")==0;
2248 int (*fn)(struct target *target,
2249 uint32_t address, uint32_t size, uint32_t count, uint8_t *buffer);
2250 if (physical)
2252 CMD_ARGC--;
2253 CMD_ARGV++;
2254 fn=target_write_phys_memory;
2255 } else
2257 fn=target_write_memory;
2259 if ((CMD_ARGC < 2) || (CMD_ARGC > 3))
2260 return ERROR_COMMAND_SYNTAX_ERROR;
2262 uint32_t address;
2263 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], address);
2265 uint32_t value;
2266 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], value);
2268 unsigned count = 1;
2269 if (CMD_ARGC == 3)
2270 COMMAND_PARSE_NUMBER(uint, CMD_ARGV[2], count);
2272 struct target *target = get_current_target(CMD_CTX);
2273 unsigned wordsize;
2274 uint8_t value_buf[4];
2275 switch (CMD_NAME[2])
2277 case 'w':
2278 wordsize = 4;
2279 target_buffer_set_u32(target, value_buf, value);
2280 break;
2281 case 'h':
2282 wordsize = 2;
2283 target_buffer_set_u16(target, value_buf, value);
2284 break;
2285 case 'b':
2286 wordsize = 1;
2287 value_buf[0] = value;
2288 break;
2289 default:
2290 return ERROR_COMMAND_SYNTAX_ERROR;
2292 for (unsigned i = 0; i < count; i++)
2294 int retval = fn(target,
2295 address + i * wordsize, wordsize, 1, value_buf);
2296 if (ERROR_OK != retval)
2297 return retval;
2298 keep_alive();
2301 return ERROR_OK;
2305 static COMMAND_HELPER(parse_load_image_command_CMD_ARGV, struct image *image,
2306 uint32_t *min_address, uint32_t *max_address)
2308 if (CMD_ARGC < 1 || CMD_ARGC > 5)
2309 return ERROR_COMMAND_SYNTAX_ERROR;
2311 /* a base address isn't always necessary,
2312 * default to 0x0 (i.e. don't relocate) */
2313 if (CMD_ARGC >= 2)
2315 uint32_t addr;
2316 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], addr);
2317 image->base_address = addr;
2318 image->base_address_set = 1;
2320 else
2321 image->base_address_set = 0;
2323 image->start_address_set = 0;
2325 if (CMD_ARGC >= 4)
2327 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[3], *min_address);
2329 if (CMD_ARGC == 5)
2331 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[4], *max_address);
2332 // use size (given) to find max (required)
2333 *max_address += *min_address;
2336 if (*min_address > *max_address)
2337 return ERROR_COMMAND_SYNTAX_ERROR;
2339 return ERROR_OK;
2342 COMMAND_HANDLER(handle_load_image_command)
2344 uint8_t *buffer;
2345 size_t buf_cnt;
2346 uint32_t image_size;
2347 uint32_t min_address = 0;
2348 uint32_t max_address = 0xffffffff;
2349 int i;
2350 struct image image;
2352 int retval = CALL_COMMAND_HANDLER(parse_load_image_command_CMD_ARGV,
2353 &image, &min_address, &max_address);
2354 if (ERROR_OK != retval)
2355 return retval;
2357 struct target *target = get_current_target(CMD_CTX);
2359 struct duration bench;
2360 duration_start(&bench);
2362 if (image_open(&image, CMD_ARGV[0], (CMD_ARGC >= 3) ? CMD_ARGV[2] : NULL) != ERROR_OK)
2364 return ERROR_OK;
2367 image_size = 0x0;
2368 retval = ERROR_OK;
2369 for (i = 0; i < image.num_sections; i++)
2371 buffer = malloc(image.sections[i].size);
2372 if (buffer == NULL)
2374 command_print(CMD_CTX,
2375 "error allocating buffer for section (%d bytes)",
2376 (int)(image.sections[i].size));
2377 break;
2380 if ((retval = image_read_section(&image, i, 0x0, image.sections[i].size, buffer, &buf_cnt)) != ERROR_OK)
2382 free(buffer);
2383 break;
2386 uint32_t offset = 0;
2387 uint32_t length = buf_cnt;
2389 /* DANGER!!! beware of unsigned comparision here!!! */
2391 if ((image.sections[i].base_address + buf_cnt >= min_address)&&
2392 (image.sections[i].base_address < max_address))
2394 if (image.sections[i].base_address < min_address)
2396 /* clip addresses below */
2397 offset += min_address-image.sections[i].base_address;
2398 length -= offset;
2401 if (image.sections[i].base_address + buf_cnt > max_address)
2403 length -= (image.sections[i].base_address + buf_cnt)-max_address;
2406 if ((retval = target_write_buffer(target, image.sections[i].base_address + offset, length, buffer + offset)) != ERROR_OK)
2408 free(buffer);
2409 break;
2411 image_size += length;
2412 command_print(CMD_CTX, "%u bytes written at address 0x%8.8" PRIx32 "",
2413 (unsigned int)length,
2414 image.sections[i].base_address + offset);
2417 free(buffer);
2420 if ((ERROR_OK == retval) && (duration_measure(&bench) == ERROR_OK))
2422 command_print(CMD_CTX, "downloaded %" PRIu32 " bytes "
2423 "in %fs (%0.3f kb/s)", image_size,
2424 duration_elapsed(&bench), duration_kbps(&bench, image_size));
2427 image_close(&image);
2429 return retval;
2433 COMMAND_HANDLER(handle_dump_image_command)
2435 struct fileio fileio;
2437 uint8_t buffer[560];
2438 int retvaltemp;
2441 struct target *target = get_current_target(CMD_CTX);
2443 if (CMD_ARGC != 3)
2445 command_print(CMD_CTX, "usage: dump_image <filename> <address> <size>");
2446 return ERROR_OK;
2449 uint32_t address;
2450 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], address);
2451 uint32_t size;
2452 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[2], size);
2454 if (fileio_open(&fileio, CMD_ARGV[0], FILEIO_WRITE, FILEIO_BINARY) != ERROR_OK)
2456 return ERROR_OK;
2459 struct duration bench;
2460 duration_start(&bench);
2462 int retval = ERROR_OK;
2463 while (size > 0)
2465 size_t size_written;
2466 uint32_t this_run_size = (size > 560) ? 560 : size;
2467 retval = target_read_buffer(target, address, this_run_size, buffer);
2468 if (retval != ERROR_OK)
2470 break;
2473 retval = fileio_write(&fileio, this_run_size, buffer, &size_written);
2474 if (retval != ERROR_OK)
2476 break;
2479 size -= this_run_size;
2480 address += this_run_size;
2483 if ((retvaltemp = fileio_close(&fileio)) != ERROR_OK)
2484 return retvaltemp;
2486 if ((ERROR_OK == retval) && (duration_measure(&bench) == ERROR_OK))
2488 command_print(CMD_CTX,
2489 "dumped %zu bytes in %fs (%0.3f kb/s)", fileio.size,
2490 duration_elapsed(&bench), duration_kbps(&bench, fileio.size));
2493 return retval;
2496 static COMMAND_HELPER(handle_verify_image_command_internal, int verify)
2498 uint8_t *buffer;
2499 size_t buf_cnt;
2500 uint32_t image_size;
2501 int i;
2502 int retval;
2503 uint32_t checksum = 0;
2504 uint32_t mem_checksum = 0;
2506 struct image image;
2508 struct target *target = get_current_target(CMD_CTX);
2510 if (CMD_ARGC < 1)
2512 return ERROR_COMMAND_SYNTAX_ERROR;
2515 if (!target)
2517 LOG_ERROR("no target selected");
2518 return ERROR_FAIL;
2521 struct duration bench;
2522 duration_start(&bench);
2524 if (CMD_ARGC >= 2)
2526 uint32_t addr;
2527 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], addr);
2528 image.base_address = addr;
2529 image.base_address_set = 1;
2531 else
2533 image.base_address_set = 0;
2534 image.base_address = 0x0;
2537 image.start_address_set = 0;
2539 if ((retval = image_open(&image, CMD_ARGV[0], (CMD_ARGC == 3) ? CMD_ARGV[2] : NULL)) != ERROR_OK)
2541 return retval;
2544 image_size = 0x0;
2545 retval = ERROR_OK;
2546 for (i = 0; i < image.num_sections; i++)
2548 buffer = malloc(image.sections[i].size);
2549 if (buffer == NULL)
2551 command_print(CMD_CTX,
2552 "error allocating buffer for section (%d bytes)",
2553 (int)(image.sections[i].size));
2554 break;
2556 if ((retval = image_read_section(&image, i, 0x0, image.sections[i].size, buffer, &buf_cnt)) != ERROR_OK)
2558 free(buffer);
2559 break;
2562 if (verify)
2564 /* calculate checksum of image */
2565 image_calculate_checksum(buffer, buf_cnt, &checksum);
2567 retval = target_checksum_memory(target, image.sections[i].base_address, buf_cnt, &mem_checksum);
2568 if (retval != ERROR_OK)
2570 free(buffer);
2571 break;
2574 if (checksum != mem_checksum)
2576 /* failed crc checksum, fall back to a binary compare */
2577 uint8_t *data;
2579 command_print(CMD_CTX, "checksum mismatch - attempting binary compare");
2581 data = (uint8_t*)malloc(buf_cnt);
2583 /* Can we use 32bit word accesses? */
2584 int size = 1;
2585 int count = buf_cnt;
2586 if ((count % 4) == 0)
2588 size *= 4;
2589 count /= 4;
2591 retval = target_read_memory(target, image.sections[i].base_address, size, count, data);
2592 if (retval == ERROR_OK)
2594 uint32_t t;
2595 for (t = 0; t < buf_cnt; t++)
2597 if (data[t] != buffer[t])
2599 command_print(CMD_CTX,
2600 "Verify operation failed address 0x%08x. Was 0x%02x instead of 0x%02x\n",
2601 (unsigned)(t + image.sections[i].base_address),
2602 data[t],
2603 buffer[t]);
2604 free(data);
2605 free(buffer);
2606 retval = ERROR_FAIL;
2607 goto done;
2609 if ((t%16384) == 0)
2611 keep_alive();
2616 free(data);
2618 } else
2620 command_print(CMD_CTX, "address 0x%08" PRIx32 " length 0x%08zx",
2621 image.sections[i].base_address,
2622 buf_cnt);
2625 free(buffer);
2626 image_size += buf_cnt;
2628 done:
2629 if ((ERROR_OK == retval) && (duration_measure(&bench) == ERROR_OK))
2631 command_print(CMD_CTX, "verified %" PRIu32 " bytes "
2632 "in %fs (%0.3f kb/s)", image_size,
2633 duration_elapsed(&bench), duration_kbps(&bench, image_size));
2636 image_close(&image);
2638 return retval;
2641 COMMAND_HANDLER(handle_verify_image_command)
2643 return CALL_COMMAND_HANDLER(handle_verify_image_command_internal, 1);
2646 COMMAND_HANDLER(handle_test_image_command)
2648 return CALL_COMMAND_HANDLER(handle_verify_image_command_internal, 0);
2651 static int handle_bp_command_list(struct command_context *cmd_ctx)
2653 struct target *target = get_current_target(cmd_ctx);
2654 struct breakpoint *breakpoint = target->breakpoints;
2655 while (breakpoint)
2657 if (breakpoint->type == BKPT_SOFT)
2659 char* buf = buf_to_str(breakpoint->orig_instr,
2660 breakpoint->length, 16);
2661 command_print(cmd_ctx, "0x%8.8" PRIx32 ", 0x%x, %i, 0x%s",
2662 breakpoint->address,
2663 breakpoint->length,
2664 breakpoint->set, buf);
2665 free(buf);
2667 else
2669 command_print(cmd_ctx, "0x%8.8" PRIx32 ", 0x%x, %i",
2670 breakpoint->address,
2671 breakpoint->length, breakpoint->set);
2674 breakpoint = breakpoint->next;
2676 return ERROR_OK;
2679 static int handle_bp_command_set(struct command_context *cmd_ctx,
2680 uint32_t addr, uint32_t length, int hw)
2682 struct target *target = get_current_target(cmd_ctx);
2683 int retval = breakpoint_add(target, addr, length, hw);
2684 if (ERROR_OK == retval)
2685 command_print(cmd_ctx, "breakpoint set at 0x%8.8" PRIx32 "", addr);
2686 else
2687 LOG_ERROR("Failure setting breakpoint");
2688 return retval;
2691 COMMAND_HANDLER(handle_bp_command)
2693 if (CMD_ARGC == 0)
2694 return handle_bp_command_list(CMD_CTX);
2696 if (CMD_ARGC < 2 || CMD_ARGC > 3)
2698 command_print(CMD_CTX, "usage: bp <address> <length> ['hw']");
2699 return ERROR_COMMAND_SYNTAX_ERROR;
2702 uint32_t addr;
2703 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], addr);
2704 uint32_t length;
2705 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], length);
2707 int hw = BKPT_SOFT;
2708 if (CMD_ARGC == 3)
2710 if (strcmp(CMD_ARGV[2], "hw") == 0)
2711 hw = BKPT_HARD;
2712 else
2713 return ERROR_COMMAND_SYNTAX_ERROR;
2716 return handle_bp_command_set(CMD_CTX, addr, length, hw);
2719 COMMAND_HANDLER(handle_rbp_command)
2721 if (CMD_ARGC != 1)
2722 return ERROR_COMMAND_SYNTAX_ERROR;
2724 uint32_t addr;
2725 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], addr);
2727 struct target *target = get_current_target(CMD_CTX);
2728 breakpoint_remove(target, addr);
2730 return ERROR_OK;
2733 COMMAND_HANDLER(handle_wp_command)
2735 struct target *target = get_current_target(CMD_CTX);
2737 if (CMD_ARGC == 0)
2739 struct watchpoint *watchpoint = target->watchpoints;
2741 while (watchpoint)
2743 command_print(CMD_CTX, "address: 0x%8.8" PRIx32
2744 ", len: 0x%8.8" PRIx32
2745 ", r/w/a: %i, value: 0x%8.8" PRIx32
2746 ", mask: 0x%8.8" PRIx32,
2747 watchpoint->address,
2748 watchpoint->length,
2749 (int)watchpoint->rw,
2750 watchpoint->value,
2751 watchpoint->mask);
2752 watchpoint = watchpoint->next;
2754 return ERROR_OK;
2757 enum watchpoint_rw type = WPT_ACCESS;
2758 uint32_t addr = 0;
2759 uint32_t length = 0;
2760 uint32_t data_value = 0x0;
2761 uint32_t data_mask = 0xffffffff;
2763 switch (CMD_ARGC)
2765 case 5:
2766 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[4], data_mask);
2767 // fall through
2768 case 4:
2769 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[3], data_value);
2770 // fall through
2771 case 3:
2772 switch (CMD_ARGV[2][0])
2774 case 'r':
2775 type = WPT_READ;
2776 break;
2777 case 'w':
2778 type = WPT_WRITE;
2779 break;
2780 case 'a':
2781 type = WPT_ACCESS;
2782 break;
2783 default:
2784 LOG_ERROR("invalid watchpoint mode ('%c')", CMD_ARGV[2][0]);
2785 return ERROR_COMMAND_SYNTAX_ERROR;
2787 // fall through
2788 case 2:
2789 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], length);
2790 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], addr);
2791 break;
2793 default:
2794 command_print(CMD_CTX, "usage: wp [address length "
2795 "[(r|w|a) [value [mask]]]]");
2796 return ERROR_COMMAND_SYNTAX_ERROR;
2799 int retval = watchpoint_add(target, addr, length, type,
2800 data_value, data_mask);
2801 if (ERROR_OK != retval)
2802 LOG_ERROR("Failure setting watchpoints");
2804 return retval;
2807 COMMAND_HANDLER(handle_rwp_command)
2809 if (CMD_ARGC != 1)
2810 return ERROR_COMMAND_SYNTAX_ERROR;
2812 uint32_t addr;
2813 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], addr);
2815 struct target *target = get_current_target(CMD_CTX);
2816 watchpoint_remove(target, addr);
2818 return ERROR_OK;
2823 * Translate a virtual address to a physical address.
2825 * The low-level target implementation must have logged a detailed error
2826 * which is forwarded to telnet/GDB session.
2828 COMMAND_HANDLER(handle_virt2phys_command)
2830 if (CMD_ARGC != 1)
2831 return ERROR_COMMAND_SYNTAX_ERROR;
2833 uint32_t va;
2834 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], va);
2835 uint32_t pa;
2837 struct target *target = get_current_target(CMD_CTX);
2838 int retval = target->type->virt2phys(target, va, &pa);
2839 if (retval == ERROR_OK)
2840 command_print(CMD_CTX, "Physical address 0x%08" PRIx32 "", pa);
2842 return retval;
2845 static void writeData(FILE *f, const void *data, size_t len)
2847 size_t written = fwrite(data, 1, len, f);
2848 if (written != len)
2849 LOG_ERROR("failed to write %zu bytes: %s", len, strerror(errno));
2852 static void writeLong(FILE *f, int l)
2854 int i;
2855 for (i = 0; i < 4; i++)
2857 char c = (l >> (i*8))&0xff;
2858 writeData(f, &c, 1);
2863 static void writeString(FILE *f, char *s)
2865 writeData(f, s, strlen(s));
2868 /* Dump a gmon.out histogram file. */
2869 static void writeGmon(uint32_t *samples, uint32_t sampleNum, const char *filename)
2871 uint32_t i;
2872 FILE *f = fopen(filename, "w");
2873 if (f == NULL)
2874 return;
2875 writeString(f, "gmon");
2876 writeLong(f, 0x00000001); /* Version */
2877 writeLong(f, 0); /* padding */
2878 writeLong(f, 0); /* padding */
2879 writeLong(f, 0); /* padding */
2881 uint8_t zero = 0; /* GMON_TAG_TIME_HIST */
2882 writeData(f, &zero, 1);
2884 /* figure out bucket size */
2885 uint32_t min = samples[0];
2886 uint32_t max = samples[0];
2887 for (i = 0; i < sampleNum; i++)
2889 if (min > samples[i])
2891 min = samples[i];
2893 if (max < samples[i])
2895 max = samples[i];
2899 int addressSpace = (max-min + 1);
2901 static const uint32_t maxBuckets = 256 * 1024; /* maximum buckets. */
2902 uint32_t length = addressSpace;
2903 if (length > maxBuckets)
2905 length = maxBuckets;
2907 int *buckets = malloc(sizeof(int)*length);
2908 if (buckets == NULL)
2910 fclose(f);
2911 return;
2913 memset(buckets, 0, sizeof(int)*length);
2914 for (i = 0; i < sampleNum;i++)
2916 uint32_t address = samples[i];
2917 long long a = address-min;
2918 long long b = length-1;
2919 long long c = addressSpace-1;
2920 int index = (a*b)/c; /* danger!!!! int32 overflows */
2921 buckets[index]++;
2924 /* append binary memory gmon.out &profile_hist_hdr ((char*)&profile_hist_hdr + sizeof(struct gmon_hist_hdr)) */
2925 writeLong(f, min); /* low_pc */
2926 writeLong(f, max); /* high_pc */
2927 writeLong(f, length); /* # of samples */
2928 writeLong(f, 64000000); /* 64MHz */
2929 writeString(f, "seconds");
2930 for (i = 0; i < (15-strlen("seconds")); i++)
2931 writeData(f, &zero, 1);
2932 writeString(f, "s");
2934 /*append binary memory gmon.out profile_hist_data (profile_hist_data + profile_hist_hdr.hist_size) */
2936 char *data = malloc(2*length);
2937 if (data != NULL)
2939 for (i = 0; i < length;i++)
2941 int val;
2942 val = buckets[i];
2943 if (val > 65535)
2945 val = 65535;
2947 data[i*2]=val&0xff;
2948 data[i*2 + 1]=(val >> 8)&0xff;
2950 free(buckets);
2951 writeData(f, data, length * 2);
2952 free(data);
2953 } else
2955 free(buckets);
2958 fclose(f);
2961 /* profiling samples the CPU PC as quickly as OpenOCD is able, which will be used as a random sampling of PC */
2962 COMMAND_HANDLER(handle_profile_command)
2964 struct target *target = get_current_target(CMD_CTX);
2965 struct timeval timeout, now;
2967 gettimeofday(&timeout, NULL);
2968 if (CMD_ARGC != 2)
2970 return ERROR_COMMAND_SYNTAX_ERROR;
2972 unsigned offset;
2973 COMMAND_PARSE_NUMBER(uint, CMD_ARGV[0], offset);
2975 timeval_add_time(&timeout, offset, 0);
2977 command_print(CMD_CTX, "Starting profiling. Halting and resuming the target as often as we can...");
2979 static const int maxSample = 10000;
2980 uint32_t *samples = malloc(sizeof(uint32_t)*maxSample);
2981 if (samples == NULL)
2982 return ERROR_OK;
2984 int numSamples = 0;
2985 /* hopefully it is safe to cache! We want to stop/restart as quickly as possible. */
2986 struct reg *reg = register_get_by_name(target->reg_cache, "pc", 1);
2988 for (;;)
2990 int retval;
2991 target_poll(target);
2992 if (target->state == TARGET_HALTED)
2994 uint32_t t=*((uint32_t *)reg->value);
2995 samples[numSamples++]=t;
2996 retval = target_resume(target, 1, 0, 0, 0); /* current pc, addr = 0, do not handle breakpoints, not debugging */
2997 target_poll(target);
2998 alive_sleep(10); /* sleep 10ms, i.e. <100 samples/second. */
2999 } else if (target->state == TARGET_RUNNING)
3001 /* We want to quickly sample the PC. */
3002 if ((retval = target_halt(target)) != ERROR_OK)
3004 free(samples);
3005 return retval;
3007 } else
3009 command_print(CMD_CTX, "Target not halted or running");
3010 retval = ERROR_OK;
3011 break;
3013 if (retval != ERROR_OK)
3015 break;
3018 gettimeofday(&now, NULL);
3019 if ((numSamples >= maxSample) || ((now.tv_sec >= timeout.tv_sec) && (now.tv_usec >= timeout.tv_usec)))
3021 command_print(CMD_CTX, "Profiling completed. %d samples.", numSamples);
3022 if ((retval = target_poll(target)) != ERROR_OK)
3024 free(samples);
3025 return retval;
3027 if (target->state == TARGET_HALTED)
3029 target_resume(target, 1, 0, 0, 0); /* current pc, addr = 0, do not handle breakpoints, not debugging */
3031 if ((retval = target_poll(target)) != ERROR_OK)
3033 free(samples);
3034 return retval;
3036 writeGmon(samples, numSamples, CMD_ARGV[1]);
3037 command_print(CMD_CTX, "Wrote %s", CMD_ARGV[1]);
3038 break;
3041 free(samples);
3043 return ERROR_OK;
3046 static int new_int_array_element(Jim_Interp * interp, const char *varname, int idx, uint32_t val)
3048 char *namebuf;
3049 Jim_Obj *nameObjPtr, *valObjPtr;
3050 int result;
3052 namebuf = alloc_printf("%s(%d)", varname, idx);
3053 if (!namebuf)
3054 return JIM_ERR;
3056 nameObjPtr = Jim_NewStringObj(interp, namebuf, -1);
3057 valObjPtr = Jim_NewIntObj(interp, val);
3058 if (!nameObjPtr || !valObjPtr)
3060 free(namebuf);
3061 return JIM_ERR;
3064 Jim_IncrRefCount(nameObjPtr);
3065 Jim_IncrRefCount(valObjPtr);
3066 result = Jim_SetVariable(interp, nameObjPtr, valObjPtr);
3067 Jim_DecrRefCount(interp, nameObjPtr);
3068 Jim_DecrRefCount(interp, valObjPtr);
3069 free(namebuf);
3070 /* printf("%s(%d) <= 0%08x\n", varname, idx, val); */
3071 return result;
3074 static int jim_mem2array(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
3076 struct command_context *context;
3077 struct target *target;
3079 context = Jim_GetAssocData(interp, "context");
3080 if (context == NULL)
3082 LOG_ERROR("mem2array: no command context");
3083 return JIM_ERR;
3085 target = get_current_target(context);
3086 if (target == NULL)
3088 LOG_ERROR("mem2array: no current target");
3089 return JIM_ERR;
3092 return target_mem2array(interp, target, argc-1, argv + 1);
3095 static int target_mem2array(Jim_Interp *interp, struct target *target, int argc, Jim_Obj *const *argv)
3097 long l;
3098 uint32_t width;
3099 int len;
3100 uint32_t addr;
3101 uint32_t count;
3102 uint32_t v;
3103 const char *varname;
3104 int n, e, retval;
3105 uint32_t i;
3107 /* argv[1] = name of array to receive the data
3108 * argv[2] = desired width
3109 * argv[3] = memory address
3110 * argv[4] = count of times to read
3112 if (argc != 4) {
3113 Jim_WrongNumArgs(interp, 1, argv, "varname width addr nelems");
3114 return JIM_ERR;
3116 varname = Jim_GetString(argv[0], &len);
3117 /* given "foo" get space for worse case "foo(%d)" .. add 20 */
3119 e = Jim_GetLong(interp, argv[1], &l);
3120 width = l;
3121 if (e != JIM_OK) {
3122 return e;
3125 e = Jim_GetLong(interp, argv[2], &l);
3126 addr = l;
3127 if (e != JIM_OK) {
3128 return e;
3130 e = Jim_GetLong(interp, argv[3], &l);
3131 len = l;
3132 if (e != JIM_OK) {
3133 return e;
3135 switch (width) {
3136 case 8:
3137 width = 1;
3138 break;
3139 case 16:
3140 width = 2;
3141 break;
3142 case 32:
3143 width = 4;
3144 break;
3145 default:
3146 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3147 Jim_AppendStrings(interp, Jim_GetResult(interp), "Invalid width param, must be 8/16/32", NULL);
3148 return JIM_ERR;
3150 if (len == 0) {
3151 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3152 Jim_AppendStrings(interp, Jim_GetResult(interp), "mem2array: zero width read?", NULL);
3153 return JIM_ERR;
3155 if ((addr + (len * width)) < addr) {
3156 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3157 Jim_AppendStrings(interp, Jim_GetResult(interp), "mem2array: addr + len - wraps to zero?", NULL);
3158 return JIM_ERR;
3160 /* absurd transfer size? */
3161 if (len > 65536) {
3162 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3163 Jim_AppendStrings(interp, Jim_GetResult(interp), "mem2array: absurd > 64K item request", NULL);
3164 return JIM_ERR;
3167 if ((width == 1) ||
3168 ((width == 2) && ((addr & 1) == 0)) ||
3169 ((width == 4) && ((addr & 3) == 0))) {
3170 /* all is well */
3171 } else {
3172 char buf[100];
3173 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3174 sprintf(buf, "mem2array address: 0x%08" PRIx32 " is not aligned for %" PRId32 " byte reads",
3175 addr,
3176 width);
3177 Jim_AppendStrings(interp, Jim_GetResult(interp), buf , NULL);
3178 return JIM_ERR;
3181 /* Transfer loop */
3183 /* index counter */
3184 n = 0;
3186 size_t buffersize = 4096;
3187 uint8_t *buffer = malloc(buffersize);
3188 if (buffer == NULL)
3189 return JIM_ERR;
3191 /* assume ok */
3192 e = JIM_OK;
3193 while (len) {
3194 /* Slurp... in buffer size chunks */
3196 count = len; /* in objects.. */
3197 if (count > (buffersize/width)) {
3198 count = (buffersize/width);
3201 retval = target_read_memory(target, addr, width, count, buffer);
3202 if (retval != ERROR_OK) {
3203 /* BOO !*/
3204 LOG_ERROR("mem2array: Read @ 0x%08x, w=%d, cnt=%d, failed",
3205 (unsigned int)addr,
3206 (int)width,
3207 (int)count);
3208 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3209 Jim_AppendStrings(interp, Jim_GetResult(interp), "mem2array: cannot read memory", NULL);
3210 e = JIM_ERR;
3211 len = 0;
3212 } else {
3213 v = 0; /* shut up gcc */
3214 for (i = 0 ;i < count ;i++, n++) {
3215 switch (width) {
3216 case 4:
3217 v = target_buffer_get_u32(target, &buffer[i*width]);
3218 break;
3219 case 2:
3220 v = target_buffer_get_u16(target, &buffer[i*width]);
3221 break;
3222 case 1:
3223 v = buffer[i] & 0x0ff;
3224 break;
3226 new_int_array_element(interp, varname, n, v);
3228 len -= count;
3232 free(buffer);
3234 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3236 return JIM_OK;
3239 static int get_int_array_element(Jim_Interp * interp, const char *varname, int idx, uint32_t *val)
3241 char *namebuf;
3242 Jim_Obj *nameObjPtr, *valObjPtr;
3243 int result;
3244 long l;
3246 namebuf = alloc_printf("%s(%d)", varname, idx);
3247 if (!namebuf)
3248 return JIM_ERR;
3250 nameObjPtr = Jim_NewStringObj(interp, namebuf, -1);
3251 if (!nameObjPtr)
3253 free(namebuf);
3254 return JIM_ERR;
3257 Jim_IncrRefCount(nameObjPtr);
3258 valObjPtr = Jim_GetVariable(interp, nameObjPtr, JIM_ERRMSG);
3259 Jim_DecrRefCount(interp, nameObjPtr);
3260 free(namebuf);
3261 if (valObjPtr == NULL)
3262 return JIM_ERR;
3264 result = Jim_GetLong(interp, valObjPtr, &l);
3265 /* printf("%s(%d) => 0%08x\n", varname, idx, val); */
3266 *val = l;
3267 return result;
3270 static int jim_array2mem(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
3272 struct command_context *context;
3273 struct target *target;
3275 context = Jim_GetAssocData(interp, "context");
3276 if (context == NULL) {
3277 LOG_ERROR("array2mem: no command context");
3278 return JIM_ERR;
3280 target = get_current_target(context);
3281 if (target == NULL) {
3282 LOG_ERROR("array2mem: no current target");
3283 return JIM_ERR;
3286 return target_array2mem(interp,target, argc-1, argv + 1);
3288 static int target_array2mem(Jim_Interp *interp, struct target *target, int argc, Jim_Obj *const *argv)
3290 long l;
3291 uint32_t width;
3292 int len;
3293 uint32_t addr;
3294 uint32_t count;
3295 uint32_t v;
3296 const char *varname;
3297 int n, e, retval;
3298 uint32_t i;
3300 /* argv[1] = name of array to get the data
3301 * argv[2] = desired width
3302 * argv[3] = memory address
3303 * argv[4] = count to write
3305 if (argc != 4) {
3306 Jim_WrongNumArgs(interp, 0, argv, "varname width addr nelems");
3307 return JIM_ERR;
3309 varname = Jim_GetString(argv[0], &len);
3310 /* given "foo" get space for worse case "foo(%d)" .. add 20 */
3312 e = Jim_GetLong(interp, argv[1], &l);
3313 width = l;
3314 if (e != JIM_OK) {
3315 return e;
3318 e = Jim_GetLong(interp, argv[2], &l);
3319 addr = l;
3320 if (e != JIM_OK) {
3321 return e;
3323 e = Jim_GetLong(interp, argv[3], &l);
3324 len = l;
3325 if (e != JIM_OK) {
3326 return e;
3328 switch (width) {
3329 case 8:
3330 width = 1;
3331 break;
3332 case 16:
3333 width = 2;
3334 break;
3335 case 32:
3336 width = 4;
3337 break;
3338 default:
3339 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3340 Jim_AppendStrings(interp, Jim_GetResult(interp), "Invalid width param, must be 8/16/32", NULL);
3341 return JIM_ERR;
3343 if (len == 0) {
3344 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3345 Jim_AppendStrings(interp, Jim_GetResult(interp), "array2mem: zero width read?", NULL);
3346 return JIM_ERR;
3348 if ((addr + (len * width)) < addr) {
3349 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3350 Jim_AppendStrings(interp, Jim_GetResult(interp), "array2mem: addr + len - wraps to zero?", NULL);
3351 return JIM_ERR;
3353 /* absurd transfer size? */
3354 if (len > 65536) {
3355 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3356 Jim_AppendStrings(interp, Jim_GetResult(interp), "array2mem: absurd > 64K item request", NULL);
3357 return JIM_ERR;
3360 if ((width == 1) ||
3361 ((width == 2) && ((addr & 1) == 0)) ||
3362 ((width == 4) && ((addr & 3) == 0))) {
3363 /* all is well */
3364 } else {
3365 char buf[100];
3366 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3367 sprintf(buf, "array2mem address: 0x%08x is not aligned for %d byte reads",
3368 (unsigned int)addr,
3369 (int)width);
3370 Jim_AppendStrings(interp, Jim_GetResult(interp), buf , NULL);
3371 return JIM_ERR;
3374 /* Transfer loop */
3376 /* index counter */
3377 n = 0;
3378 /* assume ok */
3379 e = JIM_OK;
3381 size_t buffersize = 4096;
3382 uint8_t *buffer = malloc(buffersize);
3383 if (buffer == NULL)
3384 return JIM_ERR;
3386 while (len) {
3387 /* Slurp... in buffer size chunks */
3389 count = len; /* in objects.. */
3390 if (count > (buffersize/width)) {
3391 count = (buffersize/width);
3394 v = 0; /* shut up gcc */
3395 for (i = 0 ;i < count ;i++, n++) {
3396 get_int_array_element(interp, varname, n, &v);
3397 switch (width) {
3398 case 4:
3399 target_buffer_set_u32(target, &buffer[i*width], v);
3400 break;
3401 case 2:
3402 target_buffer_set_u16(target, &buffer[i*width], v);
3403 break;
3404 case 1:
3405 buffer[i] = v & 0x0ff;
3406 break;
3409 len -= count;
3411 retval = target_write_memory(target, addr, width, count, buffer);
3412 if (retval != ERROR_OK) {
3413 /* BOO !*/
3414 LOG_ERROR("array2mem: Write @ 0x%08x, w=%d, cnt=%d, failed",
3415 (unsigned int)addr,
3416 (int)width,
3417 (int)count);
3418 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3419 Jim_AppendStrings(interp, Jim_GetResult(interp), "array2mem: cannot read memory", NULL);
3420 e = JIM_ERR;
3421 len = 0;
3425 free(buffer);
3427 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3429 return JIM_OK;
3432 void target_all_handle_event(enum target_event e)
3434 struct target *target;
3436 LOG_DEBUG("**all*targets: event: %d, %s",
3437 (int)e,
3438 Jim_Nvp_value2name_simple(nvp_target_event, e)->name);
3440 target = all_targets;
3441 while (target) {
3442 target_handle_event(target, e);
3443 target = target->next;
3448 /* FIX? should we propagate errors here rather than printing them
3449 * and continuing?
3451 void target_handle_event(struct target *target, enum target_event e)
3453 struct target_event_action *teap;
3455 for (teap = target->event_action; teap != NULL; teap = teap->next) {
3456 if (teap->event == e) {
3457 LOG_DEBUG("target: (%d) %s (%s) event: %d (%s) action: %s",
3458 target->target_number,
3459 target_name(target),
3460 target_type_name(target),
3462 Jim_Nvp_value2name_simple(nvp_target_event, e)->name,
3463 Jim_GetString(teap->body, NULL));
3464 if (Jim_EvalObj(teap->interp, teap->body) != JIM_OK)
3466 Jim_PrintErrorMessage(teap->interp);
3473 * Returns true only if the target has a handler for the specified event.
3475 bool target_has_event_action(struct target *target, enum target_event event)
3477 struct target_event_action *teap;
3479 for (teap = target->event_action; teap != NULL; teap = teap->next) {
3480 if (teap->event == event)
3481 return true;
3483 return false;
3486 enum target_cfg_param {
3487 TCFG_TYPE,
3488 TCFG_EVENT,
3489 TCFG_WORK_AREA_VIRT,
3490 TCFG_WORK_AREA_PHYS,
3491 TCFG_WORK_AREA_SIZE,
3492 TCFG_WORK_AREA_BACKUP,
3493 TCFG_ENDIAN,
3494 TCFG_VARIANT,
3495 TCFG_CHAIN_POSITION,
3498 static Jim_Nvp nvp_config_opts[] = {
3499 { .name = "-type", .value = TCFG_TYPE },
3500 { .name = "-event", .value = TCFG_EVENT },
3501 { .name = "-work-area-virt", .value = TCFG_WORK_AREA_VIRT },
3502 { .name = "-work-area-phys", .value = TCFG_WORK_AREA_PHYS },
3503 { .name = "-work-area-size", .value = TCFG_WORK_AREA_SIZE },
3504 { .name = "-work-area-backup", .value = TCFG_WORK_AREA_BACKUP },
3505 { .name = "-endian" , .value = TCFG_ENDIAN },
3506 { .name = "-variant", .value = TCFG_VARIANT },
3507 { .name = "-chain-position", .value = TCFG_CHAIN_POSITION },
3509 { .name = NULL, .value = -1 }
3512 static int target_configure(Jim_GetOptInfo *goi, struct target *target)
3514 Jim_Nvp *n;
3515 Jim_Obj *o;
3516 jim_wide w;
3517 char *cp;
3518 int e;
3520 /* parse config or cget options ... */
3521 while (goi->argc > 0) {
3522 Jim_SetEmptyResult(goi->interp);
3523 /* Jim_GetOpt_Debug(goi); */
3525 if (target->type->target_jim_configure) {
3526 /* target defines a configure function */
3527 /* target gets first dibs on parameters */
3528 e = (*(target->type->target_jim_configure))(target, goi);
3529 if (e == JIM_OK) {
3530 /* more? */
3531 continue;
3533 if (e == JIM_ERR) {
3534 /* An error */
3535 return e;
3537 /* otherwise we 'continue' below */
3539 e = Jim_GetOpt_Nvp(goi, nvp_config_opts, &n);
3540 if (e != JIM_OK) {
3541 Jim_GetOpt_NvpUnknown(goi, nvp_config_opts, 0);
3542 return e;
3544 switch (n->value) {
3545 case TCFG_TYPE:
3546 /* not setable */
3547 if (goi->isconfigure) {
3548 Jim_SetResult_sprintf(goi->interp,
3549 "not settable: %s", n->name);
3550 return JIM_ERR;
3551 } else {
3552 no_params:
3553 if (goi->argc != 0) {
3554 Jim_WrongNumArgs(goi->interp,
3555 goi->argc, goi->argv,
3556 "NO PARAMS");
3557 return JIM_ERR;
3560 Jim_SetResultString(goi->interp,
3561 target_type_name(target), -1);
3562 /* loop for more */
3563 break;
3564 case TCFG_EVENT:
3565 if (goi->argc == 0) {
3566 Jim_WrongNumArgs(goi->interp, goi->argc, goi->argv, "-event ?event-name? ...");
3567 return JIM_ERR;
3570 e = Jim_GetOpt_Nvp(goi, nvp_target_event, &n);
3571 if (e != JIM_OK) {
3572 Jim_GetOpt_NvpUnknown(goi, nvp_target_event, 1);
3573 return e;
3576 if (goi->isconfigure) {
3577 if (goi->argc != 1) {
3578 Jim_WrongNumArgs(goi->interp, goi->argc, goi->argv, "-event ?event-name? ?EVENT-BODY?");
3579 return JIM_ERR;
3581 } else {
3582 if (goi->argc != 0) {
3583 Jim_WrongNumArgs(goi->interp, goi->argc, goi->argv, "-event ?event-name?");
3584 return JIM_ERR;
3589 struct target_event_action *teap;
3591 teap = target->event_action;
3592 /* replace existing? */
3593 while (teap) {
3594 if (teap->event == (enum target_event)n->value) {
3595 break;
3597 teap = teap->next;
3600 if (goi->isconfigure) {
3601 bool replace = true;
3602 if (teap == NULL) {
3603 /* create new */
3604 teap = calloc(1, sizeof(*teap));
3605 replace = false;
3607 teap->event = n->value;
3608 teap->interp = goi->interp;
3609 Jim_GetOpt_Obj(goi, &o);
3610 if (teap->body) {
3611 Jim_DecrRefCount(teap->interp, teap->body);
3613 teap->body = Jim_DuplicateObj(goi->interp, o);
3615 * FIXME:
3616 * Tcl/TK - "tk events" have a nice feature.
3617 * See the "BIND" command.
3618 * We should support that here.
3619 * You can specify %X and %Y in the event code.
3620 * The idea is: %T - target name.
3621 * The idea is: %N - target number
3622 * The idea is: %E - event name.
3624 Jim_IncrRefCount(teap->body);
3626 if (!replace)
3628 /* add to head of event list */
3629 teap->next = target->event_action;
3630 target->event_action = teap;
3632 Jim_SetEmptyResult(goi->interp);
3633 } else {
3634 /* get */
3635 if (teap == NULL) {
3636 Jim_SetEmptyResult(goi->interp);
3637 } else {
3638 Jim_SetResult(goi->interp, Jim_DuplicateObj(goi->interp, teap->body));
3642 /* loop for more */
3643 break;
3645 case TCFG_WORK_AREA_VIRT:
3646 if (goi->isconfigure) {
3647 target_free_all_working_areas(target);
3648 e = Jim_GetOpt_Wide(goi, &w);
3649 if (e != JIM_OK) {
3650 return e;
3652 target->working_area_virt = w;
3653 target->working_area_virt_spec = true;
3654 } else {
3655 if (goi->argc != 0) {
3656 goto no_params;
3659 Jim_SetResult(goi->interp, Jim_NewIntObj(goi->interp, target->working_area_virt));
3660 /* loop for more */
3661 break;
3663 case TCFG_WORK_AREA_PHYS:
3664 if (goi->isconfigure) {
3665 target_free_all_working_areas(target);
3666 e = Jim_GetOpt_Wide(goi, &w);
3667 if (e != JIM_OK) {
3668 return e;
3670 target->working_area_phys = w;
3671 target->working_area_phys_spec = true;
3672 } else {
3673 if (goi->argc != 0) {
3674 goto no_params;
3677 Jim_SetResult(goi->interp, Jim_NewIntObj(goi->interp, target->working_area_phys));
3678 /* loop for more */
3679 break;
3681 case TCFG_WORK_AREA_SIZE:
3682 if (goi->isconfigure) {
3683 target_free_all_working_areas(target);
3684 e = Jim_GetOpt_Wide(goi, &w);
3685 if (e != JIM_OK) {
3686 return e;
3688 target->working_area_size = w;
3689 } else {
3690 if (goi->argc != 0) {
3691 goto no_params;
3694 Jim_SetResult(goi->interp, Jim_NewIntObj(goi->interp, target->working_area_size));
3695 /* loop for more */
3696 break;
3698 case TCFG_WORK_AREA_BACKUP:
3699 if (goi->isconfigure) {
3700 target_free_all_working_areas(target);
3701 e = Jim_GetOpt_Wide(goi, &w);
3702 if (e != JIM_OK) {
3703 return e;
3705 /* make this exactly 1 or 0 */
3706 target->backup_working_area = (!!w);
3707 } else {
3708 if (goi->argc != 0) {
3709 goto no_params;
3712 Jim_SetResult(goi->interp, Jim_NewIntObj(goi->interp, target->backup_working_area));
3713 /* loop for more e*/
3714 break;
3716 case TCFG_ENDIAN:
3717 if (goi->isconfigure) {
3718 e = Jim_GetOpt_Nvp(goi, nvp_target_endian, &n);
3719 if (e != JIM_OK) {
3720 Jim_GetOpt_NvpUnknown(goi, nvp_target_endian, 1);
3721 return e;
3723 target->endianness = n->value;
3724 } else {
3725 if (goi->argc != 0) {
3726 goto no_params;
3729 n = Jim_Nvp_value2name_simple(nvp_target_endian, target->endianness);
3730 if (n->name == NULL) {
3731 target->endianness = TARGET_LITTLE_ENDIAN;
3732 n = Jim_Nvp_value2name_simple(nvp_target_endian, target->endianness);
3734 Jim_SetResultString(goi->interp, n->name, -1);
3735 /* loop for more */
3736 break;
3738 case TCFG_VARIANT:
3739 if (goi->isconfigure) {
3740 if (goi->argc < 1) {
3741 Jim_SetResult_sprintf(goi->interp,
3742 "%s ?STRING?",
3743 n->name);
3744 return JIM_ERR;
3746 if (target->variant) {
3747 free((void *)(target->variant));
3749 e = Jim_GetOpt_String(goi, &cp, NULL);
3750 target->variant = strdup(cp);
3751 } else {
3752 if (goi->argc != 0) {
3753 goto no_params;
3756 Jim_SetResultString(goi->interp, target->variant,-1);
3757 /* loop for more */
3758 break;
3759 case TCFG_CHAIN_POSITION:
3760 if (goi->isconfigure) {
3761 Jim_Obj *o;
3762 struct jtag_tap *tap;
3763 target_free_all_working_areas(target);
3764 e = Jim_GetOpt_Obj(goi, &o);
3765 if (e != JIM_OK) {
3766 return e;
3768 tap = jtag_tap_by_jim_obj(goi->interp, o);
3769 if (tap == NULL) {
3770 return JIM_ERR;
3772 /* make this exactly 1 or 0 */
3773 target->tap = tap;
3774 } else {
3775 if (goi->argc != 0) {
3776 goto no_params;
3779 Jim_SetResultString(goi->interp, target->tap->dotted_name, -1);
3780 /* loop for more e*/
3781 break;
3783 } /* while (goi->argc) */
3786 /* done - we return */
3787 return JIM_OK;
3790 static int jim_target_configure(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
3792 Jim_GetOptInfo goi;
3793 Jim_GetOpt_Setup(&goi, interp, argc - 1, argv + 1);
3794 goi.isconfigure = strcmp(Jim_GetString(argv[0], NULL), "configure") == 0;
3795 int need_args = 1 + goi.isconfigure;
3796 if (goi.argc < need_args)
3798 Jim_WrongNumArgs(goi.interp, goi.argc, goi.argv,
3799 goi.isconfigure
3800 ? "missing: -option VALUE ..."
3801 : "missing: -option ...");
3802 return JIM_ERR;
3804 struct target *target = Jim_CmdPrivData(goi.interp);
3805 return target_configure(&goi, target);
3808 static int jim_target_mw(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
3810 const char *cmd_name = Jim_GetString(argv[0], NULL);
3812 Jim_GetOptInfo goi;
3813 Jim_GetOpt_Setup(&goi, interp, argc - 1, argv + 1);
3815 if (goi.argc != 2 && goi.argc != 3)
3817 Jim_SetResult_sprintf(goi.interp,
3818 "usage: %s <address> <data> [<count>]", cmd_name);
3819 return JIM_ERR;
3822 jim_wide a;
3823 int e = Jim_GetOpt_Wide(&goi, &a);
3824 if (e != JIM_OK)
3825 return e;
3827 jim_wide b;
3828 e = Jim_GetOpt_Wide(&goi, &b);
3829 if (e != JIM_OK)
3830 return e;
3832 jim_wide c = 1;
3833 if (goi.argc == 3)
3835 e = Jim_GetOpt_Wide(&goi, &c);
3836 if (e != JIM_OK)
3837 return e;
3840 struct target *target = Jim_CmdPrivData(goi.interp);
3841 uint8_t target_buf[32];
3842 if (strcasecmp(cmd_name, "mww") == 0) {
3843 target_buffer_set_u32(target, target_buf, b);
3844 b = 4;
3846 else if (strcasecmp(cmd_name, "mwh") == 0) {
3847 target_buffer_set_u16(target, target_buf, b);
3848 b = 2;
3850 else if (strcasecmp(cmd_name, "mwb") == 0) {
3851 target_buffer_set_u8(target, target_buf, b);
3852 b = 1;
3853 } else {
3854 LOG_ERROR("command '%s' unknown: ", cmd_name);
3855 return JIM_ERR;
3858 for (jim_wide x = 0; x < c; x++)
3860 e = target_write_memory(target, a, b, 1, target_buf);
3861 if (e != ERROR_OK)
3863 Jim_SetResult_sprintf(interp,
3864 "Error writing @ 0x%08x: %d\n", (int)(a), e);
3865 return JIM_ERR;
3867 /* b = width */
3868 a = a + b;
3870 return JIM_OK;
3873 static int jim_target_md(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
3875 const char *cmd_name = Jim_GetString(argv[0], NULL);
3877 Jim_GetOptInfo goi;
3878 Jim_GetOpt_Setup(&goi, interp, argc - 1, argv + 1);
3880 if ((goi.argc == 2) || (goi.argc == 3))
3882 Jim_SetResult_sprintf(goi.interp,
3883 "usage: %s <address> [<count>]", cmd_name);
3884 return JIM_ERR;
3887 jim_wide a;
3888 int e = Jim_GetOpt_Wide(&goi, &a);
3889 if (e != JIM_OK) {
3890 return JIM_ERR;
3892 jim_wide c;
3893 if (goi.argc) {
3894 e = Jim_GetOpt_Wide(&goi, &c);
3895 if (e != JIM_OK) {
3896 return JIM_ERR;
3898 } else {
3899 c = 1;
3901 jim_wide b = 1; /* shut up gcc */
3902 if (strcasecmp(cmd_name, "mdw") == 0)
3903 b = 4;
3904 else if (strcasecmp(cmd_name, "mdh") == 0)
3905 b = 2;
3906 else if (strcasecmp(cmd_name, "mdb") == 0)
3907 b = 1;
3908 else {
3909 LOG_ERROR("command '%s' unknown: ", cmd_name);
3910 return JIM_ERR;
3913 /* convert count to "bytes" */
3914 c = c * b;
3916 struct target *target = Jim_CmdPrivData(goi.interp);
3917 uint8_t target_buf[32];
3918 jim_wide x, y, z;
3919 while (c > 0) {
3920 y = c;
3921 if (y > 16) {
3922 y = 16;
3924 e = target_read_memory(target, a, b, y / b, target_buf);
3925 if (e != ERROR_OK) {
3926 Jim_SetResult_sprintf(interp, "error reading target @ 0x%08lx", (int)(a));
3927 return JIM_ERR;
3930 Jim_fprintf(interp, interp->cookie_stdout, "0x%08x ", (int)(a));
3931 switch (b) {
3932 case 4:
3933 for (x = 0; x < 16 && x < y; x += 4)
3935 z = target_buffer_get_u32(target, &(target_buf[ x * 4 ]));
3936 Jim_fprintf(interp, interp->cookie_stdout, "%08x ", (int)(z));
3938 for (; (x < 16) ; x += 4) {
3939 Jim_fprintf(interp, interp->cookie_stdout, " ");
3941 break;
3942 case 2:
3943 for (x = 0; x < 16 && x < y; x += 2)
3945 z = target_buffer_get_u16(target, &(target_buf[ x * 2 ]));
3946 Jim_fprintf(interp, interp->cookie_stdout, "%04x ", (int)(z));
3948 for (; (x < 16) ; x += 2) {
3949 Jim_fprintf(interp, interp->cookie_stdout, " ");
3951 break;
3952 case 1:
3953 default:
3954 for (x = 0 ; (x < 16) && (x < y) ; x += 1) {
3955 z = target_buffer_get_u8(target, &(target_buf[ x * 4 ]));
3956 Jim_fprintf(interp, interp->cookie_stdout, "%02x ", (int)(z));
3958 for (; (x < 16) ; x += 1) {
3959 Jim_fprintf(interp, interp->cookie_stdout, " ");
3961 break;
3963 /* ascii-ify the bytes */
3964 for (x = 0 ; x < y ; x++) {
3965 if ((target_buf[x] >= 0x20) &&
3966 (target_buf[x] <= 0x7e)) {
3967 /* good */
3968 } else {
3969 /* smack it */
3970 target_buf[x] = '.';
3973 /* space pad */
3974 while (x < 16) {
3975 target_buf[x] = ' ';
3976 x++;
3978 /* terminate */
3979 target_buf[16] = 0;
3980 /* print - with a newline */
3981 Jim_fprintf(interp, interp->cookie_stdout, "%s\n", target_buf);
3982 /* NEXT... */
3983 c -= 16;
3984 a += 16;
3986 return JIM_OK;
3989 static int jim_target_mem2array(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
3991 struct target *target = Jim_CmdPrivData(interp);
3992 return target_mem2array(interp, target, argc - 1, argv + 1);
3995 static int jim_target_array2mem(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
3997 struct target *target = Jim_CmdPrivData(interp);
3998 return target_array2mem(interp, target, argc - 1, argv + 1);
4001 static int jim_target_tap_disabled(Jim_Interp *interp)
4003 Jim_SetResult_sprintf(interp, "[TAP is disabled]");
4004 return JIM_ERR;
4007 static int jim_target_examine(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
4009 if (argc != 1)
4011 Jim_WrongNumArgs(interp, 1, argv, "[no parameters]");
4012 return JIM_ERR;
4014 struct target *target = Jim_CmdPrivData(interp);
4015 if (!target->tap->enabled)
4016 return jim_target_tap_disabled(interp);
4018 int e = target->type->examine(target);
4019 if (e != ERROR_OK)
4021 Jim_SetResult_sprintf(interp, "examine-fails: %d", e);
4022 return JIM_ERR;
4024 return JIM_OK;
4027 static int jim_target_poll(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
4029 if (argc != 1)
4031 Jim_WrongNumArgs(interp, 1, argv, "[no parameters]");
4032 return JIM_ERR;
4034 struct target *target = Jim_CmdPrivData(interp);
4035 if (!target->tap->enabled)
4036 return jim_target_tap_disabled(interp);
4038 int e;
4039 if (!(target_was_examined(target))) {
4040 e = ERROR_TARGET_NOT_EXAMINED;
4041 } else {
4042 e = target->type->poll(target);
4044 if (e != ERROR_OK)
4046 Jim_SetResult_sprintf(interp, "poll-fails: %d", e);
4047 return JIM_ERR;
4049 return JIM_OK;
4052 static int jim_target_reset(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
4054 Jim_GetOptInfo goi;
4055 Jim_GetOpt_Setup(&goi, interp, argc - 1, argv + 1);
4057 if (goi.argc != 2)
4059 Jim_WrongNumArgs(interp, 0, argv,
4060 "([tT]|[fF]|assert|deassert) BOOL");
4061 return JIM_ERR;
4064 Jim_Nvp *n;
4065 int e = Jim_GetOpt_Nvp(&goi, nvp_assert, &n);
4066 if (e != JIM_OK)
4068 Jim_GetOpt_NvpUnknown(&goi, nvp_assert, 1);
4069 return e;
4071 /* the halt or not param */
4072 jim_wide a;
4073 e = Jim_GetOpt_Wide(&goi, &a);
4074 if (e != JIM_OK)
4075 return e;
4077 struct target *target = Jim_CmdPrivData(goi.interp);
4078 if (!target->tap->enabled)
4079 return jim_target_tap_disabled(interp);
4080 if (!target->type->assert_reset || !target->type->deassert_reset)
4082 Jim_SetResult_sprintf(interp,
4083 "No target-specific reset for %s",
4084 target_name(target));
4085 return JIM_ERR;
4087 /* determine if we should halt or not. */
4088 target->reset_halt = !!a;
4089 /* When this happens - all workareas are invalid. */
4090 target_free_all_working_areas_restore(target, 0);
4092 /* do the assert */
4093 if (n->value == NVP_ASSERT) {
4094 e = target->type->assert_reset(target);
4095 } else {
4096 e = target->type->deassert_reset(target);
4098 return (e == ERROR_OK) ? JIM_OK : JIM_ERR;
4101 static int jim_target_halt(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
4103 if (argc != 1) {
4104 Jim_WrongNumArgs(interp, 1, argv, "[no parameters]");
4105 return JIM_ERR;
4107 struct target *target = Jim_CmdPrivData(interp);
4108 if (!target->tap->enabled)
4109 return jim_target_tap_disabled(interp);
4110 int e = target->type->halt(target);
4111 return (e == ERROR_OK) ? JIM_OK : JIM_ERR;
4114 static int jim_target_wait_state(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
4116 Jim_GetOptInfo goi;
4117 Jim_GetOpt_Setup(&goi, interp, argc - 1, argv + 1);
4119 /* params: <name> statename timeoutmsecs */
4120 if (goi.argc != 2)
4122 const char *cmd_name = Jim_GetString(argv[0], NULL);
4123 Jim_SetResult_sprintf(goi.interp,
4124 "%s <state_name> <timeout_in_msec>", cmd_name);
4125 return JIM_ERR;
4128 Jim_Nvp *n;
4129 int e = Jim_GetOpt_Nvp(&goi, nvp_target_state, &n);
4130 if (e != JIM_OK) {
4131 Jim_GetOpt_NvpUnknown(&goi, nvp_target_state,1);
4132 return e;
4134 jim_wide a;
4135 e = Jim_GetOpt_Wide(&goi, &a);
4136 if (e != JIM_OK) {
4137 return e;
4139 struct target *target = Jim_CmdPrivData(interp);
4140 if (!target->tap->enabled)
4141 return jim_target_tap_disabled(interp);
4143 e = target_wait_state(target, n->value, a);
4144 if (e != ERROR_OK)
4146 Jim_SetResult_sprintf(goi.interp,
4147 "target: %s wait %s fails (%d) %s",
4148 target_name(target), n->name,
4149 e, target_strerror_safe(e));
4150 return JIM_ERR;
4152 return JIM_OK;
4154 /* List for human, Events defined for this target.
4155 * scripts/programs should use 'name cget -event NAME'
4157 static int jim_target_event_list(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
4159 struct command_context *cmd_ctx = Jim_GetAssocData(interp, "context");
4160 struct target *target = Jim_CmdPrivData(interp);
4161 struct target_event_action *teap = target->event_action;
4162 command_print(cmd_ctx, "Event actions for target (%d) %s\n",
4163 target->target_number,
4164 target_name(target));
4165 command_print(cmd_ctx, "%-25s | Body", "Event");
4166 command_print(cmd_ctx, "------------------------- | "
4167 "----------------------------------------");
4168 while (teap)
4170 Jim_Nvp *opt = Jim_Nvp_value2name_simple(nvp_target_event, teap->event);
4171 command_print(cmd_ctx, "%-25s | %s",
4172 opt->name, Jim_GetString(teap->body, NULL));
4173 teap = teap->next;
4175 command_print(cmd_ctx, "***END***");
4176 return JIM_OK;
4178 static int jim_target_current_state(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
4180 if (argc != 1)
4182 Jim_WrongNumArgs(interp, 1, argv, "[no parameters]");
4183 return JIM_ERR;
4185 struct target *target = Jim_CmdPrivData(interp);
4186 Jim_SetResultString(interp, target_state_name(target), -1);
4187 return JIM_OK;
4189 static int jim_target_invoke_event(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
4191 Jim_GetOptInfo goi;
4192 Jim_GetOpt_Setup(&goi, interp, argc - 1, argv + 1);
4193 if (goi.argc != 1)
4195 const char *cmd_name = Jim_GetString(argv[0], NULL);
4196 Jim_SetResult_sprintf(goi.interp, "%s <eventname>", cmd_name);
4197 return JIM_ERR;
4199 Jim_Nvp *n;
4200 int e = Jim_GetOpt_Nvp(&goi, nvp_target_event, &n);
4201 if (e != JIM_OK)
4203 Jim_GetOpt_NvpUnknown(&goi, nvp_target_event, 1);
4204 return e;
4206 struct target *target = Jim_CmdPrivData(interp);
4207 target_handle_event(target, n->value);
4208 return JIM_OK;
4211 static const struct command_registration target_instance_command_handlers[] = {
4213 .name = "configure",
4214 .mode = COMMAND_CONFIG,
4215 .jim_handler = &jim_target_configure,
4216 .usage = "[<target_options> ...]",
4217 .help = "configure a new target for use",
4220 .name = "cget",
4221 .mode = COMMAND_ANY,
4222 .jim_handler = &jim_target_configure,
4223 .usage = "<target_type> [<target_options> ...]",
4224 .help = "configure a new target for use",
4227 .name = "mww",
4228 .mode = COMMAND_EXEC,
4229 .jim_handler = &jim_target_mw,
4230 .usage = "<address> <data> [<count>]",
4231 .help = "Write 32-bit word(s) to target memory",
4234 .name = "mwh",
4235 .mode = COMMAND_EXEC,
4236 .jim_handler = &jim_target_mw,
4237 .usage = "<address> <data> [<count>]",
4238 .help = "Write 16-bit half-word(s) to target memory",
4241 .name = "mwb",
4242 .mode = COMMAND_EXEC,
4243 .jim_handler = &jim_target_mw,
4244 .usage = "<address> <data> [<count>]",
4245 .help = "Write byte(s) to target memory",
4248 .name = "mdw",
4249 .mode = COMMAND_EXEC,
4250 .jim_handler = &jim_target_md,
4251 .usage = "<address> [<count>]",
4252 .help = "Display target memory as 32-bit words",
4255 .name = "mdh",
4256 .mode = COMMAND_EXEC,
4257 .jim_handler = &jim_target_md,
4258 .usage = "<address> [<count>]",
4259 .help = "Display target memory as 16-bit half-words",
4262 .name = "mdb",
4263 .mode = COMMAND_EXEC,
4264 .jim_handler = &jim_target_md,
4265 .usage = "<address> [<count>]",
4266 .help = "Display target memory as 8-bit bytes",
4269 .name = "array2mem",
4270 .mode = COMMAND_EXEC,
4271 .jim_handler = &jim_target_array2mem,
4274 .name = "mem2array",
4275 .mode = COMMAND_EXEC,
4276 .jim_handler = &jim_target_mem2array,
4279 .name = "eventlist",
4280 .mode = COMMAND_EXEC,
4281 .jim_handler = &jim_target_event_list,
4284 .name = "curstate",
4285 .mode = COMMAND_EXEC,
4286 .jim_handler = &jim_target_current_state,
4289 .name = "arp_examine",
4290 .mode = COMMAND_EXEC,
4291 .jim_handler = &jim_target_examine,
4294 .name = "arp_poll",
4295 .mode = COMMAND_EXEC,
4296 .jim_handler = &jim_target_poll,
4299 .name = "arp_reset",
4300 .mode = COMMAND_EXEC,
4301 .jim_handler = &jim_target_reset,
4304 .name = "arp_halt",
4305 .mode = COMMAND_EXEC,
4306 .jim_handler = &jim_target_halt,
4309 .name = "arp_waitstate",
4310 .mode = COMMAND_EXEC,
4311 .jim_handler = &jim_target_wait_state,
4314 .name = "invoke-event",
4315 .mode = COMMAND_EXEC,
4316 .jim_handler = &jim_target_invoke_event,
4318 COMMAND_REGISTRATION_DONE
4321 static int target_create(Jim_GetOptInfo *goi)
4323 Jim_Obj *new_cmd;
4324 Jim_Cmd *cmd;
4325 const char *cp;
4326 char *cp2;
4327 int e;
4328 int x;
4329 struct target *target;
4330 struct command_context *cmd_ctx;
4332 cmd_ctx = Jim_GetAssocData(goi->interp, "context");
4333 if (goi->argc < 3) {
4334 Jim_WrongNumArgs(goi->interp, 1, goi->argv, "?name? ?type? ..options...");
4335 return JIM_ERR;
4338 /* COMMAND */
4339 Jim_GetOpt_Obj(goi, &new_cmd);
4340 /* does this command exist? */
4341 cmd = Jim_GetCommand(goi->interp, new_cmd, JIM_ERRMSG);
4342 if (cmd) {
4343 cp = Jim_GetString(new_cmd, NULL);
4344 Jim_SetResult_sprintf(goi->interp, "Command/target: %s Exists", cp);
4345 return JIM_ERR;
4348 /* TYPE */
4349 e = Jim_GetOpt_String(goi, &cp2, NULL);
4350 cp = cp2;
4351 /* now does target type exist */
4352 for (x = 0 ; target_types[x] ; x++) {
4353 if (0 == strcmp(cp, target_types[x]->name)) {
4354 /* found */
4355 break;
4358 if (target_types[x] == NULL) {
4359 Jim_SetResult_sprintf(goi->interp, "Unknown target type %s, try one of ", cp);
4360 for (x = 0 ; target_types[x] ; x++) {
4361 if (target_types[x + 1]) {
4362 Jim_AppendStrings(goi->interp,
4363 Jim_GetResult(goi->interp),
4364 target_types[x]->name,
4365 ", ", NULL);
4366 } else {
4367 Jim_AppendStrings(goi->interp,
4368 Jim_GetResult(goi->interp),
4369 " or ",
4370 target_types[x]->name,NULL);
4373 return JIM_ERR;
4376 /* Create it */
4377 target = calloc(1,sizeof(struct target));
4378 /* set target number */
4379 target->target_number = new_target_number();
4381 /* allocate memory for each unique target type */
4382 target->type = (struct target_type*)calloc(1,sizeof(struct target_type));
4384 memcpy(target->type, target_types[x], sizeof(struct target_type));
4386 /* will be set by "-endian" */
4387 target->endianness = TARGET_ENDIAN_UNKNOWN;
4389 target->working_area = 0x0;
4390 target->working_area_size = 0x0;
4391 target->working_areas = NULL;
4392 target->backup_working_area = 0;
4394 target->state = TARGET_UNKNOWN;
4395 target->debug_reason = DBG_REASON_UNDEFINED;
4396 target->reg_cache = NULL;
4397 target->breakpoints = NULL;
4398 target->watchpoints = NULL;
4399 target->next = NULL;
4400 target->arch_info = NULL;
4402 target->display = 1;
4404 target->halt_issued = false;
4406 /* initialize trace information */
4407 target->trace_info = malloc(sizeof(struct trace));
4408 target->trace_info->num_trace_points = 0;
4409 target->trace_info->trace_points_size = 0;
4410 target->trace_info->trace_points = NULL;
4411 target->trace_info->trace_history_size = 0;
4412 target->trace_info->trace_history = NULL;
4413 target->trace_info->trace_history_pos = 0;
4414 target->trace_info->trace_history_overflowed = 0;
4416 target->dbgmsg = NULL;
4417 target->dbg_msg_enabled = 0;
4419 target->endianness = TARGET_ENDIAN_UNKNOWN;
4421 /* Do the rest as "configure" options */
4422 goi->isconfigure = 1;
4423 e = target_configure(goi, target);
4425 if (target->tap == NULL)
4427 Jim_SetResultString(goi->interp, "-chain-position required when creating target", -1);
4428 e = JIM_ERR;
4431 if (e != JIM_OK) {
4432 free(target->type);
4433 free(target);
4434 return e;
4437 if (target->endianness == TARGET_ENDIAN_UNKNOWN) {
4438 /* default endian to little if not specified */
4439 target->endianness = TARGET_LITTLE_ENDIAN;
4442 /* incase variant is not set */
4443 if (!target->variant)
4444 target->variant = strdup("");
4446 cp = Jim_GetString(new_cmd, NULL);
4447 target->cmd_name = strdup(cp);
4449 /* create the target specific commands */
4450 if (target->type->commands) {
4451 e = register_commands(cmd_ctx, NULL, target->type->commands);
4452 if (ERROR_OK != e)
4453 LOG_ERROR("unable to register '%s' commands", cp);
4455 if (target->type->target_create) {
4456 (*(target->type->target_create))(target, goi->interp);
4459 /* append to end of list */
4461 struct target **tpp;
4462 tpp = &(all_targets);
4463 while (*tpp) {
4464 tpp = &((*tpp)->next);
4466 *tpp = target;
4469 /* now - create the new target name command */
4470 const const struct command_registration target_subcommands[] = {
4472 .chain = target_instance_command_handlers,
4475 .chain = target->type->commands,
4477 COMMAND_REGISTRATION_DONE
4479 const const struct command_registration target_commands[] = {
4481 .name = cp,
4482 .mode = COMMAND_ANY,
4483 .help = "target command group",
4484 .chain = target_subcommands,
4486 COMMAND_REGISTRATION_DONE
4488 e = register_commands(cmd_ctx, NULL, target_commands);
4489 if (ERROR_OK != e)
4490 return JIM_ERR;
4492 struct command *c = command_find_in_context(cmd_ctx, cp);
4493 assert(c);
4494 command_set_handler_data(c, target);
4496 return (ERROR_OK == e) ? JIM_OK : JIM_ERR;
4499 static int jim_target_current(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
4501 if (argc != 1)
4503 Jim_WrongNumArgs(interp, 1, argv, "Too many parameters");
4504 return JIM_ERR;
4506 struct command_context *cmd_ctx = Jim_GetAssocData(interp, "context");
4507 Jim_SetResultString(interp, get_current_target(cmd_ctx)->cmd_name, -1);
4508 return JIM_OK;
4511 static int jim_target_types(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
4513 if (argc != 1)
4515 Jim_WrongNumArgs(interp, 1, argv, "Too many parameters");
4516 return JIM_ERR;
4518 Jim_SetResult(interp, Jim_NewListObj(interp, NULL, 0));
4519 for (unsigned x = 0; NULL != target_types[x]; x++)
4521 Jim_ListAppendElement(interp, Jim_GetResult(interp),
4522 Jim_NewStringObj(interp, target_types[x]->name, -1));
4524 return JIM_OK;
4527 static int jim_target_names(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
4529 if (argc != 1)
4531 Jim_WrongNumArgs(interp, 1, argv, "Too many parameters");
4532 return JIM_ERR;
4534 Jim_SetResult(interp, Jim_NewListObj(interp, NULL, 0));
4535 struct target *target = all_targets;
4536 while (target)
4538 Jim_ListAppendElement(interp, Jim_GetResult(interp),
4539 Jim_NewStringObj(interp, target_name(target), -1));
4540 target = target->next;
4542 return JIM_OK;
4545 static int jim_target_create(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
4547 Jim_GetOptInfo goi;
4548 Jim_GetOpt_Setup(&goi, interp, argc - 1, argv + 1);
4549 if (goi.argc < 3)
4551 Jim_WrongNumArgs(goi.interp, goi.argc, goi.argv,
4552 "<name> <target_type> [<target_options> ...]");
4553 return JIM_ERR;
4555 return target_create(&goi);
4558 static int jim_target_number(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
4560 Jim_GetOptInfo goi;
4561 Jim_GetOpt_Setup(&goi, interp, argc - 1, argv + 1);
4563 /* It's OK to remove this mechanism sometime after August 2010 or so */
4564 LOG_WARNING("don't use numbers as target identifiers; use names");
4565 if (goi.argc != 1)
4567 Jim_SetResult_sprintf(goi.interp, "usage: target number <number>");
4568 return JIM_ERR;
4570 jim_wide w;
4571 int e = Jim_GetOpt_Wide(&goi, &w);
4572 if (e != JIM_OK)
4573 return JIM_ERR;
4575 struct target *target;
4576 for (target = all_targets; NULL != target; target = target->next)
4578 if (target->target_number != w)
4579 continue;
4581 Jim_SetResultString(goi.interp, target_name(target), -1);
4582 return JIM_OK;
4584 Jim_SetResult_sprintf(goi.interp,
4585 "Target: number %d does not exist", (int)(w));
4586 return JIM_ERR;
4589 static int jim_target_count(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
4591 if (argc != 1)
4593 Jim_WrongNumArgs(interp, 1, argv, "<no parameters>");
4594 return JIM_ERR;
4596 unsigned count = 0;
4597 struct target *target = all_targets;
4598 while (NULL != target)
4600 target = target->next;
4601 count++;
4603 Jim_SetResult(interp, Jim_NewIntObj(interp, count));
4604 return JIM_OK;
4607 static const struct command_registration target_subcommand_handlers[] = {
4609 .name = "init",
4610 .mode = COMMAND_CONFIG,
4611 .handler = &handle_target_init_command,
4612 .help = "initialize targets",
4615 .name = "create",
4616 .mode = COMMAND_ANY,
4617 .jim_handler = &jim_target_create,
4618 .usage = "<name> <type> ...",
4619 .help = "Returns the currently selected target",
4622 .name = "current",
4623 .mode = COMMAND_ANY,
4624 .jim_handler = &jim_target_current,
4625 .help = "Returns the currently selected target",
4628 .name = "types",
4629 .mode = COMMAND_ANY,
4630 .jim_handler = &jim_target_types,
4631 .help = "Returns the available target types as a list of strings",
4634 .name = "names",
4635 .mode = COMMAND_ANY,
4636 .jim_handler = &jim_target_names,
4637 .help = "Returns the names of all targets as a list of strings",
4640 .name = "number",
4641 .mode = COMMAND_ANY,
4642 .jim_handler = &jim_target_number,
4643 .usage = "<number>",
4644 .help = "Returns the name of target <n>",
4647 .name = "count",
4648 .mode = COMMAND_ANY,
4649 .jim_handler = &jim_target_count,
4650 .help = "Returns the number of targets as an integer",
4652 COMMAND_REGISTRATION_DONE
4656 struct FastLoad
4658 uint32_t address;
4659 uint8_t *data;
4660 int length;
4664 static int fastload_num;
4665 static struct FastLoad *fastload;
4667 static void free_fastload(void)
4669 if (fastload != NULL)
4671 int i;
4672 for (i = 0; i < fastload_num; i++)
4674 if (fastload[i].data)
4675 free(fastload[i].data);
4677 free(fastload);
4678 fastload = NULL;
4685 COMMAND_HANDLER(handle_fast_load_image_command)
4687 uint8_t *buffer;
4688 size_t buf_cnt;
4689 uint32_t image_size;
4690 uint32_t min_address = 0;
4691 uint32_t max_address = 0xffffffff;
4692 int i;
4694 struct image image;
4696 int retval = CALL_COMMAND_HANDLER(parse_load_image_command_CMD_ARGV,
4697 &image, &min_address, &max_address);
4698 if (ERROR_OK != retval)
4699 return retval;
4701 struct duration bench;
4702 duration_start(&bench);
4704 if (image_open(&image, CMD_ARGV[0], (CMD_ARGC >= 3) ? CMD_ARGV[2] : NULL) != ERROR_OK)
4706 return ERROR_OK;
4709 image_size = 0x0;
4710 retval = ERROR_OK;
4711 fastload_num = image.num_sections;
4712 fastload = (struct FastLoad *)malloc(sizeof(struct FastLoad)*image.num_sections);
4713 if (fastload == NULL)
4715 image_close(&image);
4716 return ERROR_FAIL;
4718 memset(fastload, 0, sizeof(struct FastLoad)*image.num_sections);
4719 for (i = 0; i < image.num_sections; i++)
4721 buffer = malloc(image.sections[i].size);
4722 if (buffer == NULL)
4724 command_print(CMD_CTX, "error allocating buffer for section (%d bytes)",
4725 (int)(image.sections[i].size));
4726 break;
4729 if ((retval = image_read_section(&image, i, 0x0, image.sections[i].size, buffer, &buf_cnt)) != ERROR_OK)
4731 free(buffer);
4732 break;
4735 uint32_t offset = 0;
4736 uint32_t length = buf_cnt;
4739 /* DANGER!!! beware of unsigned comparision here!!! */
4741 if ((image.sections[i].base_address + buf_cnt >= min_address)&&
4742 (image.sections[i].base_address < max_address))
4744 if (image.sections[i].base_address < min_address)
4746 /* clip addresses below */
4747 offset += min_address-image.sections[i].base_address;
4748 length -= offset;
4751 if (image.sections[i].base_address + buf_cnt > max_address)
4753 length -= (image.sections[i].base_address + buf_cnt)-max_address;
4756 fastload[i].address = image.sections[i].base_address + offset;
4757 fastload[i].data = malloc(length);
4758 if (fastload[i].data == NULL)
4760 free(buffer);
4761 break;
4763 memcpy(fastload[i].data, buffer + offset, length);
4764 fastload[i].length = length;
4766 image_size += length;
4767 command_print(CMD_CTX, "%u bytes written at address 0x%8.8x",
4768 (unsigned int)length,
4769 ((unsigned int)(image.sections[i].base_address + offset)));
4772 free(buffer);
4775 if ((ERROR_OK == retval) && (duration_measure(&bench) == ERROR_OK))
4777 command_print(CMD_CTX, "Loaded %" PRIu32 " bytes "
4778 "in %fs (%0.3f kb/s)", image_size,
4779 duration_elapsed(&bench), duration_kbps(&bench, image_size));
4781 command_print(CMD_CTX,
4782 "WARNING: image has not been loaded to target!"
4783 "You can issue a 'fast_load' to finish loading.");
4786 image_close(&image);
4788 if (retval != ERROR_OK)
4790 free_fastload();
4793 return retval;
4796 COMMAND_HANDLER(handle_fast_load_command)
4798 if (CMD_ARGC > 0)
4799 return ERROR_COMMAND_SYNTAX_ERROR;
4800 if (fastload == NULL)
4802 LOG_ERROR("No image in memory");
4803 return ERROR_FAIL;
4805 int i;
4806 int ms = timeval_ms();
4807 int size = 0;
4808 int retval = ERROR_OK;
4809 for (i = 0; i < fastload_num;i++)
4811 struct target *target = get_current_target(CMD_CTX);
4812 command_print(CMD_CTX, "Write to 0x%08x, length 0x%08x",
4813 (unsigned int)(fastload[i].address),
4814 (unsigned int)(fastload[i].length));
4815 if (retval == ERROR_OK)
4817 retval = target_write_buffer(target, fastload[i].address, fastload[i].length, fastload[i].data);
4819 size += fastload[i].length;
4821 int after = timeval_ms();
4822 command_print(CMD_CTX, "Loaded image %f kBytes/s", (float)(size/1024.0)/((float)(after-ms)/1000.0));
4823 return retval;
4826 static const struct command_registration target_command_handlers[] = {
4828 .name = "targets",
4829 .handler = &handle_targets_command,
4830 .mode = COMMAND_ANY,
4831 .help = "change current command line target (one parameter) "
4832 "or list targets (no parameters)",
4833 .usage = "[<new_current_target>]",
4836 .name = "target",
4837 .mode = COMMAND_CONFIG,
4838 .help = "configure target",
4840 .chain = target_subcommand_handlers,
4842 COMMAND_REGISTRATION_DONE
4845 int target_register_commands(struct command_context *cmd_ctx)
4847 return register_commands(cmd_ctx, NULL, target_command_handlers);
4850 static const struct command_registration target_exec_command_handlers[] = {
4852 .name = "fast_load_image",
4853 .handler = &handle_fast_load_image_command,
4854 .mode = COMMAND_ANY,
4855 .help = "Load image into memory, mainly for profiling purposes",
4856 .usage = "<file> <address> ['bin'|'ihex'|'elf'|'s19'] "
4857 "[min_address] [max_length]",
4860 .name = "fast_load",
4861 .handler = &handle_fast_load_command,
4862 .mode = COMMAND_ANY,
4863 .help = "loads active fast load image to current target "
4864 "- mainly for profiling purposes",
4867 .name = "profile",
4868 .handler = &handle_profile_command,
4869 .mode = COMMAND_EXEC,
4870 .help = "profiling samples the CPU PC",
4872 /** @todo don't register virt2phys() unless target supports it */
4874 .name = "virt2phys",
4875 .handler = &handle_virt2phys_command,
4876 .mode = COMMAND_ANY,
4877 .help = "translate a virtual address into a physical address",
4881 .name = "reg",
4882 .handler = &handle_reg_command,
4883 .mode = COMMAND_EXEC,
4884 .help = "display or set a register",
4888 .name = "poll",
4889 .handler = &handle_poll_command,
4890 .mode = COMMAND_EXEC,
4891 .help = "poll target state",
4894 .name = "wait_halt",
4895 .handler = &handle_wait_halt_command,
4896 .mode = COMMAND_EXEC,
4897 .help = "wait for target halt",
4898 .usage = "[time (s)]",
4901 .name = "halt",
4902 .handler = &handle_halt_command,
4903 .mode = COMMAND_EXEC,
4904 .help = "halt target",
4907 .name = "resume",
4908 .handler = &handle_resume_command,
4909 .mode = COMMAND_EXEC,
4910 .help = "resume target",
4911 .usage = "[<address>]",
4914 .name = "reset",
4915 .handler = &handle_reset_command,
4916 .mode = COMMAND_EXEC,
4917 .usage = "[run|halt|init]",
4918 .help = "Reset all targets into the specified mode."
4919 "Default reset mode is run, if not given.",
4922 .name = "soft_reset_halt",
4923 .handler = &handle_soft_reset_halt_command,
4924 .mode = COMMAND_EXEC,
4925 .help = "halt the target and do a soft reset",
4929 .name = "step",
4930 .handler = &handle_step_command,
4931 .mode = COMMAND_EXEC,
4932 .help = "step one instruction from current PC or [addr]",
4933 .usage = "[<address>]",
4937 .name = "mdw",
4938 .handler = &handle_md_command,
4939 .mode = COMMAND_EXEC,
4940 .help = "display memory words",
4941 .usage = "[phys] <addr> [count]",
4944 .name = "mdh",
4945 .handler = &handle_md_command,
4946 .mode = COMMAND_EXEC,
4947 .help = "display memory half-words",
4948 .usage = "[phys] <addr> [count]",
4951 .name = "mdb",
4952 .handler = &handle_md_command,
4953 .mode = COMMAND_EXEC,
4954 .help = "display memory bytes",
4955 .usage = "[phys] <addr> [count]",
4959 .name = "mww",
4960 .handler = &handle_mw_command,
4961 .mode = COMMAND_EXEC,
4962 .help = "write memory word",
4963 .usage = "[phys] <addr> <value> [count]",
4966 .name = "mwh",
4967 .handler = &handle_mw_command,
4968 .mode = COMMAND_EXEC,
4969 .help = "write memory half-word",
4970 .usage = "[phys] <addr> <value> [count]",
4973 .name = "mwb",
4974 .handler = &handle_mw_command,
4975 .mode = COMMAND_EXEC,
4976 .help = "write memory byte",
4977 .usage = "[phys] <addr> <value> [count]",
4981 .name = "bp",
4982 .handler = &handle_bp_command,
4983 .mode = COMMAND_EXEC,
4984 .help = "list or set breakpoint",
4985 .usage = "[<address> <length> [hw]]",
4988 .name = "rbp",
4989 .handler = &handle_rbp_command,
4990 .mode = COMMAND_EXEC,
4991 .help = "remove breakpoint",
4992 .usage = "<address>",
4996 .name = "wp",
4997 .handler = &handle_wp_command,
4998 .mode = COMMAND_EXEC,
4999 .help = "list or set watchpoint",
5000 .usage = "[<address> <length> <r/w/a> [value] [mask]]",
5003 .name = "rwp",
5004 .handler = &handle_rwp_command,
5005 .mode = COMMAND_EXEC,
5006 .help = "remove watchpoint",
5007 .usage = "<address>",
5011 .name = "load_image",
5012 .handler = &handle_load_image_command,
5013 .mode = COMMAND_EXEC,
5014 .usage = "<file> <address> ['bin'|'ihex'|'elf'|'s19'] "
5015 "[min_address] [max_length]",
5018 .name = "dump_image",
5019 .handler = &handle_dump_image_command,
5020 .mode = COMMAND_EXEC,
5021 .usage = "<file> <address> <size>",
5024 .name = "verify_image",
5025 .handler = &handle_verify_image_command,
5026 .mode = COMMAND_EXEC,
5027 .usage = "<file> [offset] [type]",
5030 .name = "test_image",
5031 .handler = &handle_test_image_command,
5032 .mode = COMMAND_EXEC,
5033 .usage = "<file> [offset] [type]",
5036 .name = "ocd_mem2array",
5037 .mode = COMMAND_EXEC,
5038 .jim_handler = &jim_mem2array,
5039 .help = "read memory and return as a TCL array "
5040 "for script processing",
5041 .usage = "<arrayname> <width=32|16|8> <address> <count>",
5044 .name = "ocd_array2mem",
5045 .mode = COMMAND_EXEC,
5046 .jim_handler = &jim_array2mem,
5047 .help = "convert a TCL array to memory locations "
5048 "and write the values",
5049 .usage = "<arrayname> <width=32|16|8> <address> <count>",
5051 COMMAND_REGISTRATION_DONE
5053 int target_register_user_commands(struct command_context *cmd_ctx)
5055 int retval = ERROR_OK;
5056 if ((retval = target_request_register_commands(cmd_ctx)) != ERROR_OK)
5057 return retval;
5059 if ((retval = trace_register_commands(cmd_ctx)) != ERROR_OK)
5060 return retval;
5063 return register_commands(cmd_ctx, NULL, target_exec_command_handlers);