streamline and document helptext mode displays
[openocd.git] / src / target / target.c
blob73a762d4990a82df9a3cd619385ab48f461989da
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 dsp563xx_target;
68 extern struct target_type testee_target;
70 struct target_type *target_types[] =
72 &arm7tdmi_target,
73 &arm9tdmi_target,
74 &arm920t_target,
75 &arm720t_target,
76 &arm966e_target,
77 &arm926ejs_target,
78 &fa526_target,
79 &feroceon_target,
80 &dragonite_target,
81 &xscale_target,
82 &cortexm3_target,
83 &cortexa8_target,
84 &arm11_target,
85 &mips_m4k_target,
86 &avr_target,
87 &dsp563xx_target,
88 &testee_target,
89 NULL,
92 struct target *all_targets = NULL;
93 struct target_event_callback *target_event_callbacks = NULL;
94 struct target_timer_callback *target_timer_callbacks = NULL;
96 static const Jim_Nvp nvp_assert[] = {
97 { .name = "assert", NVP_ASSERT },
98 { .name = "deassert", NVP_DEASSERT },
99 { .name = "T", NVP_ASSERT },
100 { .name = "F", NVP_DEASSERT },
101 { .name = "t", NVP_ASSERT },
102 { .name = "f", NVP_DEASSERT },
103 { .name = NULL, .value = -1 }
106 static const Jim_Nvp nvp_error_target[] = {
107 { .value = ERROR_TARGET_INVALID, .name = "err-invalid" },
108 { .value = ERROR_TARGET_INIT_FAILED, .name = "err-init-failed" },
109 { .value = ERROR_TARGET_TIMEOUT, .name = "err-timeout" },
110 { .value = ERROR_TARGET_NOT_HALTED, .name = "err-not-halted" },
111 { .value = ERROR_TARGET_FAILURE, .name = "err-failure" },
112 { .value = ERROR_TARGET_UNALIGNED_ACCESS , .name = "err-unaligned-access" },
113 { .value = ERROR_TARGET_DATA_ABORT , .name = "err-data-abort" },
114 { .value = ERROR_TARGET_RESOURCE_NOT_AVAILABLE , .name = "err-resource-not-available" },
115 { .value = ERROR_TARGET_TRANSLATION_FAULT , .name = "err-translation-fault" },
116 { .value = ERROR_TARGET_NOT_RUNNING, .name = "err-not-running" },
117 { .value = ERROR_TARGET_NOT_EXAMINED, .name = "err-not-examined" },
118 { .value = -1, .name = NULL }
121 const char *target_strerror_safe(int err)
123 const Jim_Nvp *n;
125 n = Jim_Nvp_value2name_simple(nvp_error_target, err);
126 if (n->name == NULL) {
127 return "unknown";
128 } else {
129 return n->name;
133 static const Jim_Nvp nvp_target_event[] = {
134 { .value = TARGET_EVENT_OLD_gdb_program_config , .name = "old-gdb_program_config" },
135 { .value = TARGET_EVENT_OLD_pre_resume , .name = "old-pre_resume" },
137 { .value = TARGET_EVENT_GDB_HALT, .name = "gdb-halt" },
138 { .value = TARGET_EVENT_HALTED, .name = "halted" },
139 { .value = TARGET_EVENT_RESUMED, .name = "resumed" },
140 { .value = TARGET_EVENT_RESUME_START, .name = "resume-start" },
141 { .value = TARGET_EVENT_RESUME_END, .name = "resume-end" },
143 { .name = "gdb-start", .value = TARGET_EVENT_GDB_START },
144 { .name = "gdb-end", .value = TARGET_EVENT_GDB_END },
146 /* historical name */
148 { .value = TARGET_EVENT_RESET_START, .name = "reset-start" },
150 { .value = TARGET_EVENT_RESET_ASSERT_PRE, .name = "reset-assert-pre" },
151 { .value = TARGET_EVENT_RESET_ASSERT, .name = "reset-assert" },
152 { .value = TARGET_EVENT_RESET_ASSERT_POST, .name = "reset-assert-post" },
153 { .value = TARGET_EVENT_RESET_DEASSERT_PRE, .name = "reset-deassert-pre" },
154 { .value = TARGET_EVENT_RESET_DEASSERT_POST, .name = "reset-deassert-post" },
155 { .value = TARGET_EVENT_RESET_HALT_PRE, .name = "reset-halt-pre" },
156 { .value = TARGET_EVENT_RESET_HALT_POST, .name = "reset-halt-post" },
157 { .value = TARGET_EVENT_RESET_WAIT_PRE, .name = "reset-wait-pre" },
158 { .value = TARGET_EVENT_RESET_WAIT_POST, .name = "reset-wait-post" },
159 { .value = TARGET_EVENT_RESET_INIT, .name = "reset-init" },
160 { .value = TARGET_EVENT_RESET_END, .name = "reset-end" },
162 { .value = TARGET_EVENT_EXAMINE_START, .name = "examine-start" },
163 { .value = TARGET_EVENT_EXAMINE_END, .name = "examine-end" },
165 { .value = TARGET_EVENT_DEBUG_HALTED, .name = "debug-halted" },
166 { .value = TARGET_EVENT_DEBUG_RESUMED, .name = "debug-resumed" },
168 { .value = TARGET_EVENT_GDB_ATTACH, .name = "gdb-attach" },
169 { .value = TARGET_EVENT_GDB_DETACH, .name = "gdb-detach" },
171 { .value = TARGET_EVENT_GDB_FLASH_WRITE_START, .name = "gdb-flash-write-start" },
172 { .value = TARGET_EVENT_GDB_FLASH_WRITE_END , .name = "gdb-flash-write-end" },
174 { .value = TARGET_EVENT_GDB_FLASH_ERASE_START, .name = "gdb-flash-erase-start" },
175 { .value = TARGET_EVENT_GDB_FLASH_ERASE_END , .name = "gdb-flash-erase-end" },
177 { .value = TARGET_EVENT_RESUME_START, .name = "resume-start" },
178 { .value = TARGET_EVENT_RESUMED , .name = "resume-ok" },
179 { .value = TARGET_EVENT_RESUME_END , .name = "resume-end" },
181 { .name = NULL, .value = -1 }
184 static const Jim_Nvp nvp_target_state[] = {
185 { .name = "unknown", .value = TARGET_UNKNOWN },
186 { .name = "running", .value = TARGET_RUNNING },
187 { .name = "halted", .value = TARGET_HALTED },
188 { .name = "reset", .value = TARGET_RESET },
189 { .name = "debug-running", .value = TARGET_DEBUG_RUNNING },
190 { .name = NULL, .value = -1 },
193 static const Jim_Nvp nvp_target_debug_reason [] = {
194 { .name = "debug-request" , .value = DBG_REASON_DBGRQ },
195 { .name = "breakpoint" , .value = DBG_REASON_BREAKPOINT },
196 { .name = "watchpoint" , .value = DBG_REASON_WATCHPOINT },
197 { .name = "watchpoint-and-breakpoint", .value = DBG_REASON_WPTANDBKPT },
198 { .name = "single-step" , .value = DBG_REASON_SINGLESTEP },
199 { .name = "target-not-halted" , .value = DBG_REASON_NOTHALTED },
200 { .name = "undefined" , .value = DBG_REASON_UNDEFINED },
201 { .name = NULL, .value = -1 },
204 static const Jim_Nvp nvp_target_endian[] = {
205 { .name = "big", .value = TARGET_BIG_ENDIAN },
206 { .name = "little", .value = TARGET_LITTLE_ENDIAN },
207 { .name = "be", .value = TARGET_BIG_ENDIAN },
208 { .name = "le", .value = TARGET_LITTLE_ENDIAN },
209 { .name = NULL, .value = -1 },
212 static const Jim_Nvp nvp_reset_modes[] = {
213 { .name = "unknown", .value = RESET_UNKNOWN },
214 { .name = "run" , .value = RESET_RUN },
215 { .name = "halt" , .value = RESET_HALT },
216 { .name = "init" , .value = RESET_INIT },
217 { .name = NULL , .value = -1 },
220 const char *debug_reason_name(struct target *t)
222 const char *cp;
224 cp = Jim_Nvp_value2name_simple(nvp_target_debug_reason,
225 t->debug_reason)->name;
226 if (!cp) {
227 LOG_ERROR("Invalid debug reason: %d", (int)(t->debug_reason));
228 cp = "(*BUG*unknown*BUG*)";
230 return cp;
233 const char *
234 target_state_name( struct target *t )
236 const char *cp;
237 cp = Jim_Nvp_value2name_simple(nvp_target_state, t->state)->name;
238 if( !cp ){
239 LOG_ERROR("Invalid target state: %d", (int)(t->state));
240 cp = "(*BUG*unknown*BUG*)";
242 return cp;
245 /* determine the number of the new target */
246 static int new_target_number(void)
248 struct target *t;
249 int x;
251 /* number is 0 based */
252 x = -1;
253 t = all_targets;
254 while (t) {
255 if (x < t->target_number) {
256 x = t->target_number;
258 t = t->next;
260 return x + 1;
263 /* read a uint32_t from a buffer in target memory endianness */
264 uint32_t target_buffer_get_u32(struct target *target, const uint8_t *buffer)
266 if (target->endianness == TARGET_LITTLE_ENDIAN)
267 return le_to_h_u32(buffer);
268 else
269 return be_to_h_u32(buffer);
272 /* read a uint16_t from a buffer in target memory endianness */
273 uint16_t target_buffer_get_u16(struct target *target, const uint8_t *buffer)
275 if (target->endianness == TARGET_LITTLE_ENDIAN)
276 return le_to_h_u16(buffer);
277 else
278 return be_to_h_u16(buffer);
281 /* read a uint8_t from a buffer in target memory endianness */
282 uint8_t target_buffer_get_u8(struct target *target, const uint8_t *buffer)
284 return *buffer & 0x0ff;
287 /* write a uint32_t to a buffer in target memory endianness */
288 void target_buffer_set_u32(struct target *target, uint8_t *buffer, uint32_t value)
290 if (target->endianness == TARGET_LITTLE_ENDIAN)
291 h_u32_to_le(buffer, value);
292 else
293 h_u32_to_be(buffer, value);
296 /* write a uint16_t to a buffer in target memory endianness */
297 void target_buffer_set_u16(struct target *target, uint8_t *buffer, uint16_t value)
299 if (target->endianness == TARGET_LITTLE_ENDIAN)
300 h_u16_to_le(buffer, value);
301 else
302 h_u16_to_be(buffer, value);
305 /* write a uint8_t to a buffer in target memory endianness */
306 void target_buffer_set_u8(struct target *target, uint8_t *buffer, uint8_t value)
308 *buffer = value;
311 /* return a pointer to a configured target; id is name or number */
312 struct target *get_target(const char *id)
314 struct target *target;
316 /* try as tcltarget name */
317 for (target = all_targets; target; target = target->next) {
318 if (target->cmd_name == NULL)
319 continue;
320 if (strcmp(id, target->cmd_name) == 0)
321 return target;
324 /* It's OK to remove this fallback sometime after August 2010 or so */
326 /* no match, try as number */
327 unsigned num;
328 if (parse_uint(id, &num) != ERROR_OK)
329 return NULL;
331 for (target = all_targets; target; target = target->next) {
332 if (target->target_number == (int)num) {
333 LOG_WARNING("use '%s' as target identifier, not '%u'",
334 target->cmd_name, num);
335 return target;
339 return NULL;
342 /* returns a pointer to the n-th configured target */
343 static struct target *get_target_by_num(int num)
345 struct target *target = all_targets;
347 while (target) {
348 if (target->target_number == num) {
349 return target;
351 target = target->next;
354 return NULL;
357 struct target* get_current_target(struct command_context *cmd_ctx)
359 struct target *target = get_target_by_num(cmd_ctx->current_target);
361 if (target == NULL)
363 LOG_ERROR("BUG: current_target out of bounds");
364 exit(-1);
367 return target;
370 int target_poll(struct target *target)
372 int retval;
374 /* We can't poll until after examine */
375 if (!target_was_examined(target))
377 /* Fail silently lest we pollute the log */
378 return ERROR_FAIL;
381 retval = target->type->poll(target);
382 if (retval != ERROR_OK)
383 return retval;
385 if (target->halt_issued)
387 if (target->state == TARGET_HALTED)
389 target->halt_issued = false;
390 } else
392 long long t = timeval_ms() - target->halt_issued_time;
393 if (t>1000)
395 target->halt_issued = false;
396 LOG_INFO("Halt timed out, wake up GDB.");
397 target_call_event_callbacks(target, TARGET_EVENT_GDB_HALT);
402 return ERROR_OK;
405 int target_halt(struct target *target)
407 int retval;
408 /* We can't poll until after examine */
409 if (!target_was_examined(target))
411 LOG_ERROR("Target not examined yet");
412 return ERROR_FAIL;
415 retval = target->type->halt(target);
416 if (retval != ERROR_OK)
417 return retval;
419 target->halt_issued = true;
420 target->halt_issued_time = timeval_ms();
422 return ERROR_OK;
425 int target_resume(struct target *target, int current, uint32_t address, int handle_breakpoints, int debug_execution)
427 int retval;
429 /* We can't poll until after examine */
430 if (!target_was_examined(target))
432 LOG_ERROR("Target not examined yet");
433 return ERROR_FAIL;
436 /* note that resume *must* be asynchronous. The CPU can halt before we poll. The CPU can
437 * even halt at the current PC as a result of a software breakpoint being inserted by (a bug?)
438 * the application.
440 if ((retval = target->type->resume(target, current, address, handle_breakpoints, debug_execution)) != ERROR_OK)
441 return retval;
443 return retval;
446 int target_process_reset(struct command_context *cmd_ctx, enum target_reset_mode reset_mode)
448 char buf[100];
449 int retval;
450 Jim_Nvp *n;
451 n = Jim_Nvp_value2name_simple(nvp_reset_modes, reset_mode);
452 if (n->name == NULL) {
453 LOG_ERROR("invalid reset mode");
454 return ERROR_FAIL;
457 /* disable polling during reset to make reset event scripts
458 * more predictable, i.e. dr/irscan & pathmove in events will
459 * not have JTAG operations injected into the middle of a sequence.
461 bool save_poll = jtag_poll_get_enabled();
463 jtag_poll_set_enabled(false);
465 sprintf(buf, "ocd_process_reset %s", n->name);
466 retval = Jim_Eval(cmd_ctx->interp, buf);
468 jtag_poll_set_enabled(save_poll);
470 if (retval != JIM_OK) {
471 Jim_PrintErrorMessage(cmd_ctx->interp);
472 return ERROR_FAIL;
475 /* We want any events to be processed before the prompt */
476 retval = target_call_timer_callbacks_now();
478 return retval;
481 static int identity_virt2phys(struct target *target,
482 uint32_t virtual, uint32_t *physical)
484 *physical = virtual;
485 return ERROR_OK;
488 static int no_mmu(struct target *target, int *enabled)
490 *enabled = 0;
491 return ERROR_OK;
494 static int default_examine(struct target *target)
496 target_set_examined(target);
497 return ERROR_OK;
500 int target_examine_one(struct target *target)
502 return target->type->examine(target);
505 static int jtag_enable_callback(enum jtag_event event, void *priv)
507 struct target *target = priv;
509 if (event != JTAG_TAP_EVENT_ENABLE || !target->tap->enabled)
510 return ERROR_OK;
512 jtag_unregister_event_callback(jtag_enable_callback, target);
513 return target_examine_one(target);
517 /* Targets that correctly implement init + examine, i.e.
518 * no communication with target during init:
520 * XScale
522 int target_examine(void)
524 int retval = ERROR_OK;
525 struct target *target;
527 for (target = all_targets; target; target = target->next)
529 /* defer examination, but don't skip it */
530 if (!target->tap->enabled) {
531 jtag_register_event_callback(jtag_enable_callback,
532 target);
533 continue;
535 if ((retval = target_examine_one(target)) != ERROR_OK)
536 return retval;
538 return retval;
540 const char *target_type_name(struct target *target)
542 return target->type->name;
545 static int target_write_memory_imp(struct target *target, uint32_t address, uint32_t size, uint32_t count, uint8_t *buffer)
547 if (!target_was_examined(target))
549 LOG_ERROR("Target not examined yet");
550 return ERROR_FAIL;
552 return target->type->write_memory_imp(target, address, size, count, buffer);
555 static int target_read_memory_imp(struct target *target, uint32_t address, uint32_t size, uint32_t count, uint8_t *buffer)
557 if (!target_was_examined(target))
559 LOG_ERROR("Target not examined yet");
560 return ERROR_FAIL;
562 return target->type->read_memory_imp(target, address, size, count, buffer);
565 static int target_soft_reset_halt_imp(struct target *target)
567 if (!target_was_examined(target))
569 LOG_ERROR("Target not examined yet");
570 return ERROR_FAIL;
572 if (!target->type->soft_reset_halt_imp) {
573 LOG_ERROR("Target %s does not support soft_reset_halt",
574 target_name(target));
575 return ERROR_FAIL;
577 return target->type->soft_reset_halt_imp(target);
580 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)
582 if (!target_was_examined(target))
584 LOG_ERROR("Target not examined yet");
585 return ERROR_FAIL;
587 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);
590 int target_read_memory(struct target *target,
591 uint32_t address, uint32_t size, uint32_t count, uint8_t *buffer)
593 return target->type->read_memory(target, address, size, count, buffer);
596 int target_read_phys_memory(struct target *target,
597 uint32_t address, uint32_t size, uint32_t count, uint8_t *buffer)
599 return target->type->read_phys_memory(target, address, size, count, buffer);
602 int target_write_memory(struct target *target,
603 uint32_t address, uint32_t size, uint32_t count, uint8_t *buffer)
605 return target->type->write_memory(target, address, size, count, buffer);
608 int target_write_phys_memory(struct target *target,
609 uint32_t address, uint32_t size, uint32_t count, uint8_t *buffer)
611 return target->type->write_phys_memory(target, address, size, count, buffer);
614 int target_bulk_write_memory(struct target *target,
615 uint32_t address, uint32_t count, uint8_t *buffer)
617 return target->type->bulk_write_memory(target, address, count, buffer);
620 int target_add_breakpoint(struct target *target,
621 struct breakpoint *breakpoint)
623 if (target->state != TARGET_HALTED) {
624 LOG_WARNING("target %s is not halted", target->cmd_name);
625 return ERROR_TARGET_NOT_HALTED;
627 return target->type->add_breakpoint(target, breakpoint);
629 int target_remove_breakpoint(struct target *target,
630 struct breakpoint *breakpoint)
632 return target->type->remove_breakpoint(target, breakpoint);
635 int target_add_watchpoint(struct target *target,
636 struct watchpoint *watchpoint)
638 if (target->state != TARGET_HALTED) {
639 LOG_WARNING("target %s is not halted", target->cmd_name);
640 return ERROR_TARGET_NOT_HALTED;
642 return target->type->add_watchpoint(target, watchpoint);
644 int target_remove_watchpoint(struct target *target,
645 struct watchpoint *watchpoint)
647 return target->type->remove_watchpoint(target, watchpoint);
650 int target_get_gdb_reg_list(struct target *target,
651 struct reg **reg_list[], int *reg_list_size)
653 return target->type->get_gdb_reg_list(target, reg_list, reg_list_size);
655 int target_step(struct target *target,
656 int current, uint32_t address, int handle_breakpoints)
658 return target->type->step(target, current, address, handle_breakpoints);
662 int target_run_algorithm(struct target *target,
663 int num_mem_params, struct mem_param *mem_params,
664 int num_reg_params, struct reg_param *reg_param,
665 uint32_t entry_point, uint32_t exit_point,
666 int timeout_ms, void *arch_info)
668 return target->type->run_algorithm(target,
669 num_mem_params, mem_params, num_reg_params, reg_param,
670 entry_point, exit_point, timeout_ms, arch_info);
674 * Reset the @c examined flag for the given target.
675 * Pure paranoia -- targets are zeroed on allocation.
677 static void target_reset_examined(struct target *target)
679 target->examined = false;
682 static int
683 err_read_phys_memory(struct target *target, uint32_t address,
684 uint32_t size, uint32_t count, uint8_t *buffer)
686 LOG_ERROR("Not implemented: %s", __func__);
687 return ERROR_FAIL;
690 static int
691 err_write_phys_memory(struct target *target, uint32_t address,
692 uint32_t size, uint32_t count, uint8_t *buffer)
694 LOG_ERROR("Not implemented: %s", __func__);
695 return ERROR_FAIL;
698 static int handle_target(void *priv);
700 static int target_init_one(struct command_context *cmd_ctx,
701 struct target *target)
703 target_reset_examined(target);
705 struct target_type *type = target->type;
706 if (type->examine == NULL)
707 type->examine = default_examine;
709 int retval = type->init_target(cmd_ctx, target);
710 if (ERROR_OK != retval)
712 LOG_ERROR("target '%s' init failed", target_name(target));
713 return retval;
717 * @todo get rid of those *memory_imp() methods, now that all
718 * callers are using target_*_memory() accessors ... and make
719 * sure the "physical" paths handle the same issues.
721 /* a non-invasive way(in terms of patches) to add some code that
722 * runs before the type->write/read_memory implementation
724 type->write_memory_imp = target->type->write_memory;
725 type->write_memory = target_write_memory_imp;
727 type->read_memory_imp = target->type->read_memory;
728 type->read_memory = target_read_memory_imp;
730 type->soft_reset_halt_imp = target->type->soft_reset_halt;
731 type->soft_reset_halt = target_soft_reset_halt_imp;
733 type->run_algorithm_imp = target->type->run_algorithm;
734 type->run_algorithm = target_run_algorithm_imp;
736 /* Sanity-check MMU support ... stub in what we must, to help
737 * implement it in stages, but warn if we need to do so.
739 if (type->mmu)
741 if (type->write_phys_memory == NULL)
743 LOG_ERROR("type '%s' is missing write_phys_memory",
744 type->name);
745 type->write_phys_memory = err_write_phys_memory;
747 if (type->read_phys_memory == NULL)
749 LOG_ERROR("type '%s' is missing read_phys_memory",
750 type->name);
751 type->read_phys_memory = err_read_phys_memory;
753 if (type->virt2phys == NULL)
755 LOG_ERROR("type '%s' is missing virt2phys", type->name);
756 type->virt2phys = identity_virt2phys;
759 else
761 /* Make sure no-MMU targets all behave the same: make no
762 * distinction between physical and virtual addresses, and
763 * ensure that virt2phys() is always an identity mapping.
765 if (type->write_phys_memory || type->read_phys_memory
766 || type->virt2phys)
768 LOG_WARNING("type '%s' has bad MMU hooks", type->name);
771 type->mmu = no_mmu;
772 type->write_phys_memory = type->write_memory;
773 type->read_phys_memory = type->read_memory;
774 type->virt2phys = identity_virt2phys;
776 return ERROR_OK;
779 int target_init(struct command_context *cmd_ctx)
781 struct target *target;
782 int retval;
784 for (target = all_targets; target; target = target->next)
786 retval = target_init_one(cmd_ctx, target);
787 if (ERROR_OK != retval)
788 return retval;
791 if (!all_targets)
792 return ERROR_OK;
794 retval = target_register_user_commands(cmd_ctx);
795 if (ERROR_OK != retval)
796 return retval;
798 retval = target_register_timer_callback(&handle_target,
799 100, 1, cmd_ctx->interp);
800 if (ERROR_OK != retval)
801 return retval;
803 return ERROR_OK;
806 COMMAND_HANDLER(handle_target_init_command)
808 if (CMD_ARGC != 0)
809 return ERROR_COMMAND_SYNTAX_ERROR;
811 static bool target_initialized = false;
812 if (target_initialized)
814 LOG_INFO("'target init' has already been called");
815 return ERROR_OK;
817 target_initialized = true;
819 LOG_DEBUG("Initializing targets...");
820 return target_init(CMD_CTX);
823 int target_register_event_callback(int (*callback)(struct target *target, enum target_event event, void *priv), void *priv)
825 struct target_event_callback **callbacks_p = &target_event_callbacks;
827 if (callback == NULL)
829 return ERROR_INVALID_ARGUMENTS;
832 if (*callbacks_p)
834 while ((*callbacks_p)->next)
835 callbacks_p = &((*callbacks_p)->next);
836 callbacks_p = &((*callbacks_p)->next);
839 (*callbacks_p) = malloc(sizeof(struct target_event_callback));
840 (*callbacks_p)->callback = callback;
841 (*callbacks_p)->priv = priv;
842 (*callbacks_p)->next = NULL;
844 return ERROR_OK;
847 int target_register_timer_callback(int (*callback)(void *priv), int time_ms, int periodic, void *priv)
849 struct target_timer_callback **callbacks_p = &target_timer_callbacks;
850 struct timeval now;
852 if (callback == NULL)
854 return ERROR_INVALID_ARGUMENTS;
857 if (*callbacks_p)
859 while ((*callbacks_p)->next)
860 callbacks_p = &((*callbacks_p)->next);
861 callbacks_p = &((*callbacks_p)->next);
864 (*callbacks_p) = malloc(sizeof(struct target_timer_callback));
865 (*callbacks_p)->callback = callback;
866 (*callbacks_p)->periodic = periodic;
867 (*callbacks_p)->time_ms = time_ms;
869 gettimeofday(&now, NULL);
870 (*callbacks_p)->when.tv_usec = now.tv_usec + (time_ms % 1000) * 1000;
871 time_ms -= (time_ms % 1000);
872 (*callbacks_p)->when.tv_sec = now.tv_sec + (time_ms / 1000);
873 if ((*callbacks_p)->when.tv_usec > 1000000)
875 (*callbacks_p)->when.tv_usec = (*callbacks_p)->when.tv_usec - 1000000;
876 (*callbacks_p)->when.tv_sec += 1;
879 (*callbacks_p)->priv = priv;
880 (*callbacks_p)->next = NULL;
882 return ERROR_OK;
885 int target_unregister_event_callback(int (*callback)(struct target *target, enum target_event event, void *priv), void *priv)
887 struct target_event_callback **p = &target_event_callbacks;
888 struct target_event_callback *c = target_event_callbacks;
890 if (callback == NULL)
892 return ERROR_INVALID_ARGUMENTS;
895 while (c)
897 struct target_event_callback *next = c->next;
898 if ((c->callback == callback) && (c->priv == priv))
900 *p = next;
901 free(c);
902 return ERROR_OK;
904 else
905 p = &(c->next);
906 c = next;
909 return ERROR_OK;
912 int target_unregister_timer_callback(int (*callback)(void *priv), void *priv)
914 struct target_timer_callback **p = &target_timer_callbacks;
915 struct target_timer_callback *c = target_timer_callbacks;
917 if (callback == NULL)
919 return ERROR_INVALID_ARGUMENTS;
922 while (c)
924 struct target_timer_callback *next = c->next;
925 if ((c->callback == callback) && (c->priv == priv))
927 *p = next;
928 free(c);
929 return ERROR_OK;
931 else
932 p = &(c->next);
933 c = next;
936 return ERROR_OK;
939 int target_call_event_callbacks(struct target *target, enum target_event event)
941 struct target_event_callback *callback = target_event_callbacks;
942 struct target_event_callback *next_callback;
944 if (event == TARGET_EVENT_HALTED)
946 /* execute early halted first */
947 target_call_event_callbacks(target, TARGET_EVENT_GDB_HALT);
950 LOG_DEBUG("target event %i (%s)",
951 event,
952 Jim_Nvp_value2name_simple(nvp_target_event, event)->name);
954 target_handle_event(target, event);
956 while (callback)
958 next_callback = callback->next;
959 callback->callback(target, event, callback->priv);
960 callback = next_callback;
963 return ERROR_OK;
966 static int target_timer_callback_periodic_restart(
967 struct target_timer_callback *cb, struct timeval *now)
969 int time_ms = cb->time_ms;
970 cb->when.tv_usec = now->tv_usec + (time_ms % 1000) * 1000;
971 time_ms -= (time_ms % 1000);
972 cb->when.tv_sec = now->tv_sec + time_ms / 1000;
973 if (cb->when.tv_usec > 1000000)
975 cb->when.tv_usec = cb->when.tv_usec - 1000000;
976 cb->when.tv_sec += 1;
978 return ERROR_OK;
981 static int target_call_timer_callback(struct target_timer_callback *cb,
982 struct timeval *now)
984 cb->callback(cb->priv);
986 if (cb->periodic)
987 return target_timer_callback_periodic_restart(cb, now);
989 return target_unregister_timer_callback(cb->callback, cb->priv);
992 static int target_call_timer_callbacks_check_time(int checktime)
994 keep_alive();
996 struct timeval now;
997 gettimeofday(&now, NULL);
999 struct target_timer_callback *callback = target_timer_callbacks;
1000 while (callback)
1002 // cleaning up may unregister and free this callback
1003 struct target_timer_callback *next_callback = callback->next;
1005 bool call_it = callback->callback &&
1006 ((!checktime && callback->periodic) ||
1007 now.tv_sec > callback->when.tv_sec ||
1008 (now.tv_sec == callback->when.tv_sec &&
1009 now.tv_usec >= callback->when.tv_usec));
1011 if (call_it)
1013 int retval = target_call_timer_callback(callback, &now);
1014 if (retval != ERROR_OK)
1015 return retval;
1018 callback = next_callback;
1021 return ERROR_OK;
1024 int target_call_timer_callbacks(void)
1026 return target_call_timer_callbacks_check_time(1);
1029 /* invoke periodic callbacks immediately */
1030 int target_call_timer_callbacks_now(void)
1032 return target_call_timer_callbacks_check_time(0);
1035 int target_alloc_working_area(struct target *target, uint32_t size, struct working_area **area)
1037 struct working_area *c = target->working_areas;
1038 struct working_area *new_wa = NULL;
1040 /* Reevaluate working area address based on MMU state*/
1041 if (target->working_areas == NULL)
1043 int retval;
1044 int enabled;
1046 retval = target->type->mmu(target, &enabled);
1047 if (retval != ERROR_OK)
1049 return retval;
1052 if (!enabled) {
1053 if (target->working_area_phys_spec) {
1054 LOG_DEBUG("MMU disabled, using physical "
1055 "address for working memory 0x%08x",
1056 (unsigned)target->working_area_phys);
1057 target->working_area = target->working_area_phys;
1058 } else {
1059 LOG_ERROR("No working memory available. "
1060 "Specify -work-area-phys to target.");
1061 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
1063 } else {
1064 if (target->working_area_virt_spec) {
1065 LOG_DEBUG("MMU enabled, using virtual "
1066 "address for working memory 0x%08x",
1067 (unsigned)target->working_area_virt);
1068 target->working_area = target->working_area_virt;
1069 } else {
1070 LOG_ERROR("No working memory available. "
1071 "Specify -work-area-virt to target.");
1072 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
1077 /* only allocate multiples of 4 byte */
1078 if (size % 4)
1080 LOG_ERROR("BUG: code tried to allocate unaligned number of bytes (0x%08x), padding", ((unsigned)(size)));
1081 size = (size + 3) & (~3);
1084 /* see if there's already a matching working area */
1085 while (c)
1087 if ((c->free) && (c->size == size))
1089 new_wa = c;
1090 break;
1092 c = c->next;
1095 /* if not, allocate a new one */
1096 if (!new_wa)
1098 struct working_area **p = &target->working_areas;
1099 uint32_t first_free = target->working_area;
1100 uint32_t free_size = target->working_area_size;
1102 c = target->working_areas;
1103 while (c)
1105 first_free += c->size;
1106 free_size -= c->size;
1107 p = &c->next;
1108 c = c->next;
1111 if (free_size < size)
1113 LOG_WARNING("not enough working area available(requested %u, free %u)",
1114 (unsigned)(size), (unsigned)(free_size));
1115 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
1118 LOG_DEBUG("allocated new working area at address 0x%08x", (unsigned)first_free);
1120 new_wa = malloc(sizeof(struct working_area));
1121 new_wa->next = NULL;
1122 new_wa->size = size;
1123 new_wa->address = first_free;
1125 if (target->backup_working_area)
1127 int retval;
1128 new_wa->backup = malloc(new_wa->size);
1129 if ((retval = target_read_memory(target, new_wa->address, 4, new_wa->size / 4, new_wa->backup)) != ERROR_OK)
1131 free(new_wa->backup);
1132 free(new_wa);
1133 return retval;
1136 else
1138 new_wa->backup = NULL;
1141 /* put new entry in list */
1142 *p = new_wa;
1145 /* mark as used, and return the new (reused) area */
1146 new_wa->free = 0;
1147 *area = new_wa;
1149 /* user pointer */
1150 new_wa->user = area;
1152 return ERROR_OK;
1155 int target_free_working_area_restore(struct target *target, struct working_area *area, int restore)
1157 if (area->free)
1158 return ERROR_OK;
1160 if (restore && target->backup_working_area)
1162 int retval;
1163 if ((retval = target_write_memory(target, area->address, 4, area->size / 4, area->backup)) != ERROR_OK)
1164 return retval;
1167 area->free = 1;
1169 /* mark user pointer invalid */
1170 *area->user = NULL;
1171 area->user = NULL;
1173 return ERROR_OK;
1176 int target_free_working_area(struct target *target, struct working_area *area)
1178 return target_free_working_area_restore(target, area, 1);
1181 /* free resources and restore memory, if restoring memory fails,
1182 * free up resources anyway
1184 void target_free_all_working_areas_restore(struct target *target, int restore)
1186 struct working_area *c = target->working_areas;
1188 while (c)
1190 struct working_area *next = c->next;
1191 target_free_working_area_restore(target, c, restore);
1193 if (c->backup)
1194 free(c->backup);
1196 free(c);
1198 c = next;
1201 target->working_areas = NULL;
1204 void target_free_all_working_areas(struct target *target)
1206 target_free_all_working_areas_restore(target, 1);
1209 int target_arch_state(struct target *target)
1211 int retval;
1212 if (target == NULL)
1214 LOG_USER("No target has been configured");
1215 return ERROR_OK;
1218 LOG_USER("target state: %s", target_state_name( target ));
1220 if (target->state != TARGET_HALTED)
1221 return ERROR_OK;
1223 retval = target->type->arch_state(target);
1224 return retval;
1227 /* Single aligned words are guaranteed to use 16 or 32 bit access
1228 * mode respectively, otherwise data is handled as quickly as
1229 * possible
1231 int target_write_buffer(struct target *target, uint32_t address, uint32_t size, uint8_t *buffer)
1233 int retval;
1234 LOG_DEBUG("writing buffer of %i byte at 0x%8.8x",
1235 (int)size, (unsigned)address);
1237 if (!target_was_examined(target))
1239 LOG_ERROR("Target not examined yet");
1240 return ERROR_FAIL;
1243 if (size == 0) {
1244 return ERROR_OK;
1247 if ((address + size - 1) < address)
1249 /* GDB can request this when e.g. PC is 0xfffffffc*/
1250 LOG_ERROR("address + size wrapped(0x%08x, 0x%08x)",
1251 (unsigned)address,
1252 (unsigned)size);
1253 return ERROR_FAIL;
1256 if (((address % 2) == 0) && (size == 2))
1258 return target_write_memory(target, address, 2, 1, buffer);
1261 /* handle unaligned head bytes */
1262 if (address % 4)
1264 uint32_t unaligned = 4 - (address % 4);
1266 if (unaligned > size)
1267 unaligned = size;
1269 if ((retval = target_write_memory(target, address, 1, unaligned, buffer)) != ERROR_OK)
1270 return retval;
1272 buffer += unaligned;
1273 address += unaligned;
1274 size -= unaligned;
1277 /* handle aligned words */
1278 if (size >= 4)
1280 int aligned = size - (size % 4);
1282 /* use bulk writes above a certain limit. This may have to be changed */
1283 if (aligned > 128)
1285 if ((retval = target->type->bulk_write_memory(target, address, aligned / 4, buffer)) != ERROR_OK)
1286 return retval;
1288 else
1290 if ((retval = target_write_memory(target, address, 4, aligned / 4, buffer)) != ERROR_OK)
1291 return retval;
1294 buffer += aligned;
1295 address += aligned;
1296 size -= aligned;
1299 /* handle tail writes of less than 4 bytes */
1300 if (size > 0)
1302 if ((retval = target_write_memory(target, address, 1, size, buffer)) != ERROR_OK)
1303 return retval;
1306 return ERROR_OK;
1309 /* Single aligned words are guaranteed to use 16 or 32 bit access
1310 * mode respectively, otherwise data is handled as quickly as
1311 * possible
1313 int target_read_buffer(struct target *target, uint32_t address, uint32_t size, uint8_t *buffer)
1315 int retval;
1316 LOG_DEBUG("reading buffer of %i byte at 0x%8.8x",
1317 (int)size, (unsigned)address);
1319 if (!target_was_examined(target))
1321 LOG_ERROR("Target not examined yet");
1322 return ERROR_FAIL;
1325 if (size == 0) {
1326 return ERROR_OK;
1329 if ((address + size - 1) < address)
1331 /* GDB can request this when e.g. PC is 0xfffffffc*/
1332 LOG_ERROR("address + size wrapped(0x%08" PRIx32 ", 0x%08" PRIx32 ")",
1333 address,
1334 size);
1335 return ERROR_FAIL;
1338 if (((address % 2) == 0) && (size == 2))
1340 return target_read_memory(target, address, 2, 1, buffer);
1343 /* handle unaligned head bytes */
1344 if (address % 4)
1346 uint32_t unaligned = 4 - (address % 4);
1348 if (unaligned > size)
1349 unaligned = size;
1351 if ((retval = target_read_memory(target, address, 1, unaligned, buffer)) != ERROR_OK)
1352 return retval;
1354 buffer += unaligned;
1355 address += unaligned;
1356 size -= unaligned;
1359 /* handle aligned words */
1360 if (size >= 4)
1362 int aligned = size - (size % 4);
1364 if ((retval = target_read_memory(target, address, 4, aligned / 4, buffer)) != ERROR_OK)
1365 return retval;
1367 buffer += aligned;
1368 address += aligned;
1369 size -= aligned;
1372 /*prevent byte access when possible (avoid AHB access limitations in some cases)*/
1373 if(size >=2)
1375 int aligned = size - (size%2);
1376 retval = target_read_memory(target, address, 2, aligned / 2, buffer);
1377 if (retval != ERROR_OK)
1378 return retval;
1380 buffer += aligned;
1381 address += aligned;
1382 size -= aligned;
1384 /* handle tail writes of less than 4 bytes */
1385 if (size > 0)
1387 if ((retval = target_read_memory(target, address, 1, size, buffer)) != ERROR_OK)
1388 return retval;
1391 return ERROR_OK;
1394 int target_checksum_memory(struct target *target, uint32_t address, uint32_t size, uint32_t* crc)
1396 uint8_t *buffer;
1397 int retval;
1398 uint32_t i;
1399 uint32_t checksum = 0;
1400 if (!target_was_examined(target))
1402 LOG_ERROR("Target not examined yet");
1403 return ERROR_FAIL;
1406 if ((retval = target->type->checksum_memory(target, address,
1407 size, &checksum)) != ERROR_OK)
1409 buffer = malloc(size);
1410 if (buffer == NULL)
1412 LOG_ERROR("error allocating buffer for section (%d bytes)", (int)size);
1413 return ERROR_INVALID_ARGUMENTS;
1415 retval = target_read_buffer(target, address, size, buffer);
1416 if (retval != ERROR_OK)
1418 free(buffer);
1419 return retval;
1422 /* convert to target endianess */
1423 for (i = 0; i < (size/sizeof(uint32_t)); i++)
1425 uint32_t target_data;
1426 target_data = target_buffer_get_u32(target, &buffer[i*sizeof(uint32_t)]);
1427 target_buffer_set_u32(target, &buffer[i*sizeof(uint32_t)], target_data);
1430 retval = image_calculate_checksum(buffer, size, &checksum);
1431 free(buffer);
1434 *crc = checksum;
1436 return retval;
1439 int target_blank_check_memory(struct target *target, uint32_t address, uint32_t size, uint32_t* blank)
1441 int retval;
1442 if (!target_was_examined(target))
1444 LOG_ERROR("Target not examined yet");
1445 return ERROR_FAIL;
1448 if (target->type->blank_check_memory == 0)
1449 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
1451 retval = target->type->blank_check_memory(target, address, size, blank);
1453 return retval;
1456 int target_read_u32(struct target *target, uint32_t address, uint32_t *value)
1458 uint8_t value_buf[4];
1459 if (!target_was_examined(target))
1461 LOG_ERROR("Target not examined yet");
1462 return ERROR_FAIL;
1465 int retval = target_read_memory(target, address, 4, 1, value_buf);
1467 if (retval == ERROR_OK)
1469 *value = target_buffer_get_u32(target, value_buf);
1470 LOG_DEBUG("address: 0x%8.8" PRIx32 ", value: 0x%8.8" PRIx32 "",
1471 address,
1472 *value);
1474 else
1476 *value = 0x0;
1477 LOG_DEBUG("address: 0x%8.8" PRIx32 " failed",
1478 address);
1481 return retval;
1484 int target_read_u16(struct target *target, uint32_t address, uint16_t *value)
1486 uint8_t value_buf[2];
1487 if (!target_was_examined(target))
1489 LOG_ERROR("Target not examined yet");
1490 return ERROR_FAIL;
1493 int retval = target_read_memory(target, address, 2, 1, value_buf);
1495 if (retval == ERROR_OK)
1497 *value = target_buffer_get_u16(target, value_buf);
1498 LOG_DEBUG("address: 0x%8.8" PRIx32 ", value: 0x%4.4x",
1499 address,
1500 *value);
1502 else
1504 *value = 0x0;
1505 LOG_DEBUG("address: 0x%8.8" PRIx32 " failed",
1506 address);
1509 return retval;
1512 int target_read_u8(struct target *target, uint32_t address, uint8_t *value)
1514 int retval = target_read_memory(target, address, 1, 1, value);
1515 if (!target_was_examined(target))
1517 LOG_ERROR("Target not examined yet");
1518 return ERROR_FAIL;
1521 if (retval == ERROR_OK)
1523 LOG_DEBUG("address: 0x%8.8" PRIx32 ", value: 0x%2.2x",
1524 address,
1525 *value);
1527 else
1529 *value = 0x0;
1530 LOG_DEBUG("address: 0x%8.8" PRIx32 " failed",
1531 address);
1534 return retval;
1537 int target_write_u32(struct target *target, uint32_t address, uint32_t value)
1539 int retval;
1540 uint8_t value_buf[4];
1541 if (!target_was_examined(target))
1543 LOG_ERROR("Target not examined yet");
1544 return ERROR_FAIL;
1547 LOG_DEBUG("address: 0x%8.8" PRIx32 ", value: 0x%8.8" PRIx32 "",
1548 address,
1549 value);
1551 target_buffer_set_u32(target, value_buf, value);
1552 if ((retval = target_write_memory(target, address, 4, 1, value_buf)) != ERROR_OK)
1554 LOG_DEBUG("failed: %i", retval);
1557 return retval;
1560 int target_write_u16(struct target *target, uint32_t address, uint16_t value)
1562 int retval;
1563 uint8_t value_buf[2];
1564 if (!target_was_examined(target))
1566 LOG_ERROR("Target not examined yet");
1567 return ERROR_FAIL;
1570 LOG_DEBUG("address: 0x%8.8" PRIx32 ", value: 0x%8.8x",
1571 address,
1572 value);
1574 target_buffer_set_u16(target, value_buf, value);
1575 if ((retval = target_write_memory(target, address, 2, 1, value_buf)) != ERROR_OK)
1577 LOG_DEBUG("failed: %i", retval);
1580 return retval;
1583 int target_write_u8(struct target *target, uint32_t address, uint8_t value)
1585 int retval;
1586 if (!target_was_examined(target))
1588 LOG_ERROR("Target not examined yet");
1589 return ERROR_FAIL;
1592 LOG_DEBUG("address: 0x%8.8" PRIx32 ", value: 0x%2.2x",
1593 address, value);
1595 if ((retval = target_write_memory(target, address, 1, 1, &value)) != ERROR_OK)
1597 LOG_DEBUG("failed: %i", retval);
1600 return retval;
1603 COMMAND_HANDLER(handle_targets_command)
1605 struct target *target = all_targets;
1607 if (CMD_ARGC == 1)
1609 target = get_target(CMD_ARGV[0]);
1610 if (target == NULL) {
1611 command_print(CMD_CTX,"Target: %s is unknown, try one of:\n", CMD_ARGV[0]);
1612 goto DumpTargets;
1614 if (!target->tap->enabled) {
1615 command_print(CMD_CTX,"Target: TAP %s is disabled, "
1616 "can't be the current target\n",
1617 target->tap->dotted_name);
1618 return ERROR_FAIL;
1621 CMD_CTX->current_target = target->target_number;
1622 return ERROR_OK;
1624 DumpTargets:
1626 target = all_targets;
1627 command_print(CMD_CTX, " TargetName Type Endian TapName State ");
1628 command_print(CMD_CTX, "-- ------------------ ---------- ------ ------------------ ------------");
1629 while (target)
1631 const char *state;
1632 char marker = ' ';
1634 if (target->tap->enabled)
1635 state = target_state_name( target );
1636 else
1637 state = "tap-disabled";
1639 if (CMD_CTX->current_target == target->target_number)
1640 marker = '*';
1642 /* keep columns lined up to match the headers above */
1643 command_print(CMD_CTX, "%2d%c %-18s %-10s %-6s %-18s %s",
1644 target->target_number,
1645 marker,
1646 target_name(target),
1647 target_type_name(target),
1648 Jim_Nvp_value2name_simple(nvp_target_endian,
1649 target->endianness)->name,
1650 target->tap->dotted_name,
1651 state);
1652 target = target->next;
1655 return ERROR_OK;
1658 /* every 300ms we check for reset & powerdropout and issue a "reset halt" if so. */
1660 static int powerDropout;
1661 static int srstAsserted;
1663 static int runPowerRestore;
1664 static int runPowerDropout;
1665 static int runSrstAsserted;
1666 static int runSrstDeasserted;
1668 static int sense_handler(void)
1670 static int prevSrstAsserted = 0;
1671 static int prevPowerdropout = 0;
1673 int retval;
1674 if ((retval = jtag_power_dropout(&powerDropout)) != ERROR_OK)
1675 return retval;
1677 int powerRestored;
1678 powerRestored = prevPowerdropout && !powerDropout;
1679 if (powerRestored)
1681 runPowerRestore = 1;
1684 long long current = timeval_ms();
1685 static long long lastPower = 0;
1686 int waitMore = lastPower + 2000 > current;
1687 if (powerDropout && !waitMore)
1689 runPowerDropout = 1;
1690 lastPower = current;
1693 if ((retval = jtag_srst_asserted(&srstAsserted)) != ERROR_OK)
1694 return retval;
1696 int srstDeasserted;
1697 srstDeasserted = prevSrstAsserted && !srstAsserted;
1699 static long long lastSrst = 0;
1700 waitMore = lastSrst + 2000 > current;
1701 if (srstDeasserted && !waitMore)
1703 runSrstDeasserted = 1;
1704 lastSrst = current;
1707 if (!prevSrstAsserted && srstAsserted)
1709 runSrstAsserted = 1;
1712 prevSrstAsserted = srstAsserted;
1713 prevPowerdropout = powerDropout;
1715 if (srstDeasserted || powerRestored)
1717 /* Other than logging the event we can't do anything here.
1718 * Issuing a reset is a particularly bad idea as we might
1719 * be inside a reset already.
1723 return ERROR_OK;
1726 static void target_call_event_callbacks_all(enum target_event e) {
1727 struct target *target;
1728 target = all_targets;
1729 while (target) {
1730 target_call_event_callbacks(target, e);
1731 target = target->next;
1735 /* process target state changes */
1736 static int handle_target(void *priv)
1738 Jim_Interp *interp = (Jim_Interp *)priv;
1739 int retval = ERROR_OK;
1741 /* we do not want to recurse here... */
1742 static int recursive = 0;
1743 if (! recursive)
1745 recursive = 1;
1746 sense_handler();
1747 /* danger! running these procedures can trigger srst assertions and power dropouts.
1748 * We need to avoid an infinite loop/recursion here and we do that by
1749 * clearing the flags after running these events.
1751 int did_something = 0;
1752 if (runSrstAsserted)
1754 target_call_event_callbacks_all(TARGET_EVENT_GDB_HALT);
1755 Jim_Eval(interp, "srst_asserted");
1756 did_something = 1;
1758 if (runSrstDeasserted)
1760 Jim_Eval(interp, "srst_deasserted");
1761 did_something = 1;
1763 if (runPowerDropout)
1765 target_call_event_callbacks_all(TARGET_EVENT_GDB_HALT);
1766 Jim_Eval(interp, "power_dropout");
1767 did_something = 1;
1769 if (runPowerRestore)
1771 Jim_Eval(interp, "power_restore");
1772 did_something = 1;
1775 if (did_something)
1777 /* clear detect flags */
1778 sense_handler();
1781 /* clear action flags */
1783 runSrstAsserted = 0;
1784 runSrstDeasserted = 0;
1785 runPowerRestore = 0;
1786 runPowerDropout = 0;
1788 recursive = 0;
1791 /* Poll targets for state changes unless that's globally disabled.
1792 * Skip targets that are currently disabled.
1794 for (struct target *target = all_targets;
1795 is_jtag_poll_safe() && target;
1796 target = target->next)
1798 if (!target->tap->enabled)
1799 continue;
1801 /* only poll target if we've got power and srst isn't asserted */
1802 if (!powerDropout && !srstAsserted)
1804 /* polling may fail silently until the target has been examined */
1805 if ((retval = target_poll(target)) != ERROR_OK)
1807 target_call_event_callbacks(target, TARGET_EVENT_GDB_HALT);
1808 return retval;
1813 return retval;
1816 COMMAND_HANDLER(handle_reg_command)
1818 struct target *target;
1819 struct reg *reg = NULL;
1820 unsigned count = 0;
1821 char *value;
1823 LOG_DEBUG("-");
1825 target = get_current_target(CMD_CTX);
1827 /* list all available registers for the current target */
1828 if (CMD_ARGC == 0)
1830 struct reg_cache *cache = target->reg_cache;
1832 count = 0;
1833 while (cache)
1835 unsigned i;
1837 command_print(CMD_CTX, "===== %s", cache->name);
1839 for (i = 0, reg = cache->reg_list;
1840 i < cache->num_regs;
1841 i++, reg++, count++)
1843 /* only print cached values if they are valid */
1844 if (reg->valid) {
1845 value = buf_to_str(reg->value,
1846 reg->size, 16);
1847 command_print(CMD_CTX,
1848 "(%i) %s (/%" PRIu32 "): 0x%s%s",
1849 count, reg->name,
1850 reg->size, value,
1851 reg->dirty
1852 ? " (dirty)"
1853 : "");
1854 free(value);
1855 } else {
1856 command_print(CMD_CTX, "(%i) %s (/%" PRIu32 ")",
1857 count, reg->name,
1858 reg->size) ;
1861 cache = cache->next;
1864 return ERROR_OK;
1867 /* access a single register by its ordinal number */
1868 if ((CMD_ARGV[0][0] >= '0') && (CMD_ARGV[0][0] <= '9'))
1870 unsigned num;
1871 COMMAND_PARSE_NUMBER(uint, CMD_ARGV[0], num);
1873 struct reg_cache *cache = target->reg_cache;
1874 count = 0;
1875 while (cache)
1877 unsigned i;
1878 for (i = 0; i < cache->num_regs; i++)
1880 if (count++ == num)
1882 reg = &cache->reg_list[i];
1883 break;
1886 if (reg)
1887 break;
1888 cache = cache->next;
1891 if (!reg)
1893 command_print(CMD_CTX, "%i is out of bounds, the current target has only %i registers (0 - %i)", num, count, count - 1);
1894 return ERROR_OK;
1896 } else /* access a single register by its name */
1898 reg = register_get_by_name(target->reg_cache, CMD_ARGV[0], 1);
1900 if (!reg)
1902 command_print(CMD_CTX, "register %s not found in current target", CMD_ARGV[0]);
1903 return ERROR_OK;
1907 /* display a register */
1908 if ((CMD_ARGC == 1) || ((CMD_ARGC == 2) && !((CMD_ARGV[1][0] >= '0') && (CMD_ARGV[1][0] <= '9'))))
1910 if ((CMD_ARGC == 2) && (strcmp(CMD_ARGV[1], "force") == 0))
1911 reg->valid = 0;
1913 if (reg->valid == 0)
1915 reg->type->get(reg);
1917 value = buf_to_str(reg->value, reg->size, 16);
1918 command_print(CMD_CTX, "%s (/%i): 0x%s", reg->name, (int)(reg->size), value);
1919 free(value);
1920 return ERROR_OK;
1923 /* set register value */
1924 if (CMD_ARGC == 2)
1926 uint8_t *buf = malloc(DIV_ROUND_UP(reg->size, 8));
1927 str_to_buf(CMD_ARGV[1], strlen(CMD_ARGV[1]), buf, reg->size, 0);
1929 reg->type->set(reg, buf);
1931 value = buf_to_str(reg->value, reg->size, 16);
1932 command_print(CMD_CTX, "%s (/%i): 0x%s", reg->name, (int)(reg->size), value);
1933 free(value);
1935 free(buf);
1937 return ERROR_OK;
1940 command_print(CMD_CTX, "usage: reg <#|name> [value]");
1942 return ERROR_OK;
1945 COMMAND_HANDLER(handle_poll_command)
1947 int retval = ERROR_OK;
1948 struct target *target = get_current_target(CMD_CTX);
1950 if (CMD_ARGC == 0)
1952 command_print(CMD_CTX, "background polling: %s",
1953 jtag_poll_get_enabled() ? "on" : "off");
1954 command_print(CMD_CTX, "TAP: %s (%s)",
1955 target->tap->dotted_name,
1956 target->tap->enabled ? "enabled" : "disabled");
1957 if (!target->tap->enabled)
1958 return ERROR_OK;
1959 if ((retval = target_poll(target)) != ERROR_OK)
1960 return retval;
1961 if ((retval = target_arch_state(target)) != ERROR_OK)
1962 return retval;
1964 else if (CMD_ARGC == 1)
1966 bool enable;
1967 COMMAND_PARSE_ON_OFF(CMD_ARGV[0], enable);
1968 jtag_poll_set_enabled(enable);
1970 else
1972 return ERROR_COMMAND_SYNTAX_ERROR;
1975 return retval;
1978 COMMAND_HANDLER(handle_wait_halt_command)
1980 if (CMD_ARGC > 1)
1981 return ERROR_COMMAND_SYNTAX_ERROR;
1983 unsigned ms = 5000;
1984 if (1 == CMD_ARGC)
1986 int retval = parse_uint(CMD_ARGV[0], &ms);
1987 if (ERROR_OK != retval)
1989 command_print(CMD_CTX, "usage: %s [seconds]", CMD_NAME);
1990 return ERROR_COMMAND_SYNTAX_ERROR;
1992 // convert seconds (given) to milliseconds (needed)
1993 ms *= 1000;
1996 struct target *target = get_current_target(CMD_CTX);
1997 return target_wait_state(target, TARGET_HALTED, ms);
2000 /* wait for target state to change. The trick here is to have a low
2001 * latency for short waits and not to suck up all the CPU time
2002 * on longer waits.
2004 * After 500ms, keep_alive() is invoked
2006 int target_wait_state(struct target *target, enum target_state state, int ms)
2008 int retval;
2009 long long then = 0, cur;
2010 int once = 1;
2012 for (;;)
2014 if ((retval = target_poll(target)) != ERROR_OK)
2015 return retval;
2016 if (target->state == state)
2018 break;
2020 cur = timeval_ms();
2021 if (once)
2023 once = 0;
2024 then = timeval_ms();
2025 LOG_DEBUG("waiting for target %s...",
2026 Jim_Nvp_value2name_simple(nvp_target_state,state)->name);
2029 if (cur-then > 500)
2031 keep_alive();
2034 if ((cur-then) > ms)
2036 LOG_ERROR("timed out while waiting for target %s",
2037 Jim_Nvp_value2name_simple(nvp_target_state,state)->name);
2038 return ERROR_FAIL;
2042 return ERROR_OK;
2045 COMMAND_HANDLER(handle_halt_command)
2047 LOG_DEBUG("-");
2049 struct target *target = get_current_target(CMD_CTX);
2050 int retval = target_halt(target);
2051 if (ERROR_OK != retval)
2052 return retval;
2054 if (CMD_ARGC == 1)
2056 unsigned wait;
2057 retval = parse_uint(CMD_ARGV[0], &wait);
2058 if (ERROR_OK != retval)
2059 return ERROR_COMMAND_SYNTAX_ERROR;
2060 if (!wait)
2061 return ERROR_OK;
2064 return CALL_COMMAND_HANDLER(handle_wait_halt_command);
2067 COMMAND_HANDLER(handle_soft_reset_halt_command)
2069 struct target *target = get_current_target(CMD_CTX);
2071 LOG_USER("requesting target halt and executing a soft reset");
2073 target->type->soft_reset_halt(target);
2075 return ERROR_OK;
2078 COMMAND_HANDLER(handle_reset_command)
2080 if (CMD_ARGC > 1)
2081 return ERROR_COMMAND_SYNTAX_ERROR;
2083 enum target_reset_mode reset_mode = RESET_RUN;
2084 if (CMD_ARGC == 1)
2086 const Jim_Nvp *n;
2087 n = Jim_Nvp_name2value_simple(nvp_reset_modes, CMD_ARGV[0]);
2088 if ((n->name == NULL) || (n->value == RESET_UNKNOWN)) {
2089 return ERROR_COMMAND_SYNTAX_ERROR;
2091 reset_mode = n->value;
2094 /* reset *all* targets */
2095 return target_process_reset(CMD_CTX, reset_mode);
2099 COMMAND_HANDLER(handle_resume_command)
2101 int current = 1;
2102 if (CMD_ARGC > 1)
2103 return ERROR_COMMAND_SYNTAX_ERROR;
2105 struct target *target = get_current_target(CMD_CTX);
2106 target_handle_event(target, TARGET_EVENT_OLD_pre_resume);
2108 /* with no CMD_ARGV, resume from current pc, addr = 0,
2109 * with one arguments, addr = CMD_ARGV[0],
2110 * handle breakpoints, not debugging */
2111 uint32_t addr = 0;
2112 if (CMD_ARGC == 1)
2114 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], addr);
2115 current = 0;
2118 return target_resume(target, current, addr, 1, 0);
2121 COMMAND_HANDLER(handle_step_command)
2123 if (CMD_ARGC > 1)
2124 return ERROR_COMMAND_SYNTAX_ERROR;
2126 LOG_DEBUG("-");
2128 /* with no CMD_ARGV, step from current pc, addr = 0,
2129 * with one argument addr = CMD_ARGV[0],
2130 * handle breakpoints, debugging */
2131 uint32_t addr = 0;
2132 int current_pc = 1;
2133 if (CMD_ARGC == 1)
2135 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], addr);
2136 current_pc = 0;
2139 struct target *target = get_current_target(CMD_CTX);
2141 return target->type->step(target, current_pc, addr, 1);
2144 static void handle_md_output(struct command_context *cmd_ctx,
2145 struct target *target, uint32_t address, unsigned size,
2146 unsigned count, const uint8_t *buffer)
2148 const unsigned line_bytecnt = 32;
2149 unsigned line_modulo = line_bytecnt / size;
2151 char output[line_bytecnt * 4 + 1];
2152 unsigned output_len = 0;
2154 const char *value_fmt;
2155 switch (size) {
2156 case 4: value_fmt = "%8.8x "; break;
2157 case 2: value_fmt = "%4.2x "; break;
2158 case 1: value_fmt = "%2.2x "; break;
2159 default:
2160 /* "can't happen", caller checked */
2161 LOG_ERROR("invalid memory read size: %u", size);
2162 return;
2165 for (unsigned i = 0; i < count; i++)
2167 if (i % line_modulo == 0)
2169 output_len += snprintf(output + output_len,
2170 sizeof(output) - output_len,
2171 "0x%8.8x: ",
2172 (unsigned)(address + (i*size)));
2175 uint32_t value = 0;
2176 const uint8_t *value_ptr = buffer + i * size;
2177 switch (size) {
2178 case 4: value = target_buffer_get_u32(target, value_ptr); break;
2179 case 2: value = target_buffer_get_u16(target, value_ptr); break;
2180 case 1: value = *value_ptr;
2182 output_len += snprintf(output + output_len,
2183 sizeof(output) - output_len,
2184 value_fmt, value);
2186 if ((i % line_modulo == line_modulo - 1) || (i == count - 1))
2188 command_print(cmd_ctx, "%s", output);
2189 output_len = 0;
2194 COMMAND_HANDLER(handle_md_command)
2196 if (CMD_ARGC < 1)
2197 return ERROR_COMMAND_SYNTAX_ERROR;
2199 unsigned size = 0;
2200 switch (CMD_NAME[2]) {
2201 case 'w': size = 4; break;
2202 case 'h': size = 2; break;
2203 case 'b': size = 1; break;
2204 default: return ERROR_COMMAND_SYNTAX_ERROR;
2207 bool physical=strcmp(CMD_ARGV[0], "phys")==0;
2208 int (*fn)(struct target *target,
2209 uint32_t address, uint32_t size, uint32_t count, uint8_t *buffer);
2210 if (physical)
2212 CMD_ARGC--;
2213 CMD_ARGV++;
2214 fn=target_read_phys_memory;
2215 } else
2217 fn=target_read_memory;
2219 if ((CMD_ARGC < 1) || (CMD_ARGC > 2))
2221 return ERROR_COMMAND_SYNTAX_ERROR;
2224 uint32_t address;
2225 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], address);
2227 unsigned count = 1;
2228 if (CMD_ARGC == 2)
2229 COMMAND_PARSE_NUMBER(uint, CMD_ARGV[1], count);
2231 uint8_t *buffer = calloc(count, size);
2233 struct target *target = get_current_target(CMD_CTX);
2234 int retval = fn(target, address, size, count, buffer);
2235 if (ERROR_OK == retval)
2236 handle_md_output(CMD_CTX, target, address, size, count, buffer);
2238 free(buffer);
2240 return retval;
2243 COMMAND_HANDLER(handle_mw_command)
2245 if (CMD_ARGC < 2)
2247 return ERROR_COMMAND_SYNTAX_ERROR;
2249 bool physical=strcmp(CMD_ARGV[0], "phys")==0;
2250 int (*fn)(struct target *target,
2251 uint32_t address, uint32_t size, uint32_t count, uint8_t *buffer);
2252 if (physical)
2254 CMD_ARGC--;
2255 CMD_ARGV++;
2256 fn=target_write_phys_memory;
2257 } else
2259 fn=target_write_memory;
2261 if ((CMD_ARGC < 2) || (CMD_ARGC > 3))
2262 return ERROR_COMMAND_SYNTAX_ERROR;
2264 uint32_t address;
2265 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], address);
2267 uint32_t value;
2268 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], value);
2270 unsigned count = 1;
2271 if (CMD_ARGC == 3)
2272 COMMAND_PARSE_NUMBER(uint, CMD_ARGV[2], count);
2274 struct target *target = get_current_target(CMD_CTX);
2275 unsigned wordsize;
2276 uint8_t value_buf[4];
2277 switch (CMD_NAME[2])
2279 case 'w':
2280 wordsize = 4;
2281 target_buffer_set_u32(target, value_buf, value);
2282 break;
2283 case 'h':
2284 wordsize = 2;
2285 target_buffer_set_u16(target, value_buf, value);
2286 break;
2287 case 'b':
2288 wordsize = 1;
2289 value_buf[0] = value;
2290 break;
2291 default:
2292 return ERROR_COMMAND_SYNTAX_ERROR;
2294 for (unsigned i = 0; i < count; i++)
2296 int retval = fn(target,
2297 address + i * wordsize, wordsize, 1, value_buf);
2298 if (ERROR_OK != retval)
2299 return retval;
2300 keep_alive();
2303 return ERROR_OK;
2307 static COMMAND_HELPER(parse_load_image_command_CMD_ARGV, struct image *image,
2308 uint32_t *min_address, uint32_t *max_address)
2310 if (CMD_ARGC < 1 || CMD_ARGC > 5)
2311 return ERROR_COMMAND_SYNTAX_ERROR;
2313 /* a base address isn't always necessary,
2314 * default to 0x0 (i.e. don't relocate) */
2315 if (CMD_ARGC >= 2)
2317 uint32_t addr;
2318 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], addr);
2319 image->base_address = addr;
2320 image->base_address_set = 1;
2322 else
2323 image->base_address_set = 0;
2325 image->start_address_set = 0;
2327 if (CMD_ARGC >= 4)
2329 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[3], *min_address);
2331 if (CMD_ARGC == 5)
2333 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[4], *max_address);
2334 // use size (given) to find max (required)
2335 *max_address += *min_address;
2338 if (*min_address > *max_address)
2339 return ERROR_COMMAND_SYNTAX_ERROR;
2341 return ERROR_OK;
2344 COMMAND_HANDLER(handle_load_image_command)
2346 uint8_t *buffer;
2347 size_t buf_cnt;
2348 uint32_t image_size;
2349 uint32_t min_address = 0;
2350 uint32_t max_address = 0xffffffff;
2351 int i;
2352 struct image image;
2354 int retval = CALL_COMMAND_HANDLER(parse_load_image_command_CMD_ARGV,
2355 &image, &min_address, &max_address);
2356 if (ERROR_OK != retval)
2357 return retval;
2359 struct target *target = get_current_target(CMD_CTX);
2361 struct duration bench;
2362 duration_start(&bench);
2364 if (image_open(&image, CMD_ARGV[0], (CMD_ARGC >= 3) ? CMD_ARGV[2] : NULL) != ERROR_OK)
2366 return ERROR_OK;
2369 image_size = 0x0;
2370 retval = ERROR_OK;
2371 for (i = 0; i < image.num_sections; i++)
2373 buffer = malloc(image.sections[i].size);
2374 if (buffer == NULL)
2376 command_print(CMD_CTX,
2377 "error allocating buffer for section (%d bytes)",
2378 (int)(image.sections[i].size));
2379 break;
2382 if ((retval = image_read_section(&image, i, 0x0, image.sections[i].size, buffer, &buf_cnt)) != ERROR_OK)
2384 free(buffer);
2385 break;
2388 uint32_t offset = 0;
2389 uint32_t length = buf_cnt;
2391 /* DANGER!!! beware of unsigned comparision here!!! */
2393 if ((image.sections[i].base_address + buf_cnt >= min_address)&&
2394 (image.sections[i].base_address < max_address))
2396 if (image.sections[i].base_address < min_address)
2398 /* clip addresses below */
2399 offset += min_address-image.sections[i].base_address;
2400 length -= offset;
2403 if (image.sections[i].base_address + buf_cnt > max_address)
2405 length -= (image.sections[i].base_address + buf_cnt)-max_address;
2408 if ((retval = target_write_buffer(target, image.sections[i].base_address + offset, length, buffer + offset)) != ERROR_OK)
2410 free(buffer);
2411 break;
2413 image_size += length;
2414 command_print(CMD_CTX, "%u bytes written at address 0x%8.8" PRIx32 "",
2415 (unsigned int)length,
2416 image.sections[i].base_address + offset);
2419 free(buffer);
2422 if ((ERROR_OK == retval) && (duration_measure(&bench) == ERROR_OK))
2424 command_print(CMD_CTX, "downloaded %" PRIu32 " bytes "
2425 "in %fs (%0.3f kb/s)", image_size,
2426 duration_elapsed(&bench), duration_kbps(&bench, image_size));
2429 image_close(&image);
2431 return retval;
2435 COMMAND_HANDLER(handle_dump_image_command)
2437 struct fileio fileio;
2439 uint8_t buffer[560];
2440 int retvaltemp;
2443 struct target *target = get_current_target(CMD_CTX);
2445 if (CMD_ARGC != 3)
2447 command_print(CMD_CTX, "usage: dump_image <filename> <address> <size>");
2448 return ERROR_OK;
2451 uint32_t address;
2452 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], address);
2453 uint32_t size;
2454 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[2], size);
2456 if (fileio_open(&fileio, CMD_ARGV[0], FILEIO_WRITE, FILEIO_BINARY) != ERROR_OK)
2458 return ERROR_OK;
2461 struct duration bench;
2462 duration_start(&bench);
2464 int retval = ERROR_OK;
2465 while (size > 0)
2467 size_t size_written;
2468 uint32_t this_run_size = (size > 560) ? 560 : size;
2469 retval = target_read_buffer(target, address, this_run_size, buffer);
2470 if (retval != ERROR_OK)
2472 break;
2475 retval = fileio_write(&fileio, this_run_size, buffer, &size_written);
2476 if (retval != ERROR_OK)
2478 break;
2481 size -= this_run_size;
2482 address += this_run_size;
2485 if ((retvaltemp = fileio_close(&fileio)) != ERROR_OK)
2486 return retvaltemp;
2488 if ((ERROR_OK == retval) && (duration_measure(&bench) == ERROR_OK))
2490 command_print(CMD_CTX,
2491 "dumped %ld bytes in %fs (%0.3f kb/s)", (long)fileio.size,
2492 duration_elapsed(&bench), duration_kbps(&bench, fileio.size));
2495 return retval;
2498 static COMMAND_HELPER(handle_verify_image_command_internal, int verify)
2500 uint8_t *buffer;
2501 size_t buf_cnt;
2502 uint32_t image_size;
2503 int i;
2504 int retval;
2505 uint32_t checksum = 0;
2506 uint32_t mem_checksum = 0;
2508 struct image image;
2510 struct target *target = get_current_target(CMD_CTX);
2512 if (CMD_ARGC < 1)
2514 return ERROR_COMMAND_SYNTAX_ERROR;
2517 if (!target)
2519 LOG_ERROR("no target selected");
2520 return ERROR_FAIL;
2523 struct duration bench;
2524 duration_start(&bench);
2526 if (CMD_ARGC >= 2)
2528 uint32_t addr;
2529 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], addr);
2530 image.base_address = addr;
2531 image.base_address_set = 1;
2533 else
2535 image.base_address_set = 0;
2536 image.base_address = 0x0;
2539 image.start_address_set = 0;
2541 if ((retval = image_open(&image, CMD_ARGV[0], (CMD_ARGC == 3) ? CMD_ARGV[2] : NULL)) != ERROR_OK)
2543 return retval;
2546 image_size = 0x0;
2547 retval = ERROR_OK;
2548 for (i = 0; i < image.num_sections; i++)
2550 buffer = malloc(image.sections[i].size);
2551 if (buffer == NULL)
2553 command_print(CMD_CTX,
2554 "error allocating buffer for section (%d bytes)",
2555 (int)(image.sections[i].size));
2556 break;
2558 if ((retval = image_read_section(&image, i, 0x0, image.sections[i].size, buffer, &buf_cnt)) != ERROR_OK)
2560 free(buffer);
2561 break;
2564 if (verify)
2566 /* calculate checksum of image */
2567 image_calculate_checksum(buffer, buf_cnt, &checksum);
2569 retval = target_checksum_memory(target, image.sections[i].base_address, buf_cnt, &mem_checksum);
2570 if (retval != ERROR_OK)
2572 free(buffer);
2573 break;
2576 if (checksum != mem_checksum)
2578 /* failed crc checksum, fall back to a binary compare */
2579 uint8_t *data;
2581 command_print(CMD_CTX, "checksum mismatch - attempting binary compare");
2583 data = (uint8_t*)malloc(buf_cnt);
2585 /* Can we use 32bit word accesses? */
2586 int size = 1;
2587 int count = buf_cnt;
2588 if ((count % 4) == 0)
2590 size *= 4;
2591 count /= 4;
2593 retval = target_read_memory(target, image.sections[i].base_address, size, count, data);
2594 if (retval == ERROR_OK)
2596 uint32_t t;
2597 for (t = 0; t < buf_cnt; t++)
2599 if (data[t] != buffer[t])
2601 command_print(CMD_CTX,
2602 "Verify operation failed address 0x%08x. Was 0x%02x instead of 0x%02x\n",
2603 (unsigned)(t + image.sections[i].base_address),
2604 data[t],
2605 buffer[t]);
2606 free(data);
2607 free(buffer);
2608 retval = ERROR_FAIL;
2609 goto done;
2611 if ((t%16384) == 0)
2613 keep_alive();
2618 free(data);
2620 } else
2622 command_print(CMD_CTX, "address 0x%08" PRIx32 " length 0x%08zx",
2623 image.sections[i].base_address,
2624 buf_cnt);
2627 free(buffer);
2628 image_size += buf_cnt;
2630 done:
2631 if ((ERROR_OK == retval) && (duration_measure(&bench) == ERROR_OK))
2633 command_print(CMD_CTX, "verified %" PRIu32 " bytes "
2634 "in %fs (%0.3f kb/s)", image_size,
2635 duration_elapsed(&bench), duration_kbps(&bench, image_size));
2638 image_close(&image);
2640 return retval;
2643 COMMAND_HANDLER(handle_verify_image_command)
2645 return CALL_COMMAND_HANDLER(handle_verify_image_command_internal, 1);
2648 COMMAND_HANDLER(handle_test_image_command)
2650 return CALL_COMMAND_HANDLER(handle_verify_image_command_internal, 0);
2653 static int handle_bp_command_list(struct command_context *cmd_ctx)
2655 struct target *target = get_current_target(cmd_ctx);
2656 struct breakpoint *breakpoint = target->breakpoints;
2657 while (breakpoint)
2659 if (breakpoint->type == BKPT_SOFT)
2661 char* buf = buf_to_str(breakpoint->orig_instr,
2662 breakpoint->length, 16);
2663 command_print(cmd_ctx, "0x%8.8" PRIx32 ", 0x%x, %i, 0x%s",
2664 breakpoint->address,
2665 breakpoint->length,
2666 breakpoint->set, buf);
2667 free(buf);
2669 else
2671 command_print(cmd_ctx, "0x%8.8" PRIx32 ", 0x%x, %i",
2672 breakpoint->address,
2673 breakpoint->length, breakpoint->set);
2676 breakpoint = breakpoint->next;
2678 return ERROR_OK;
2681 static int handle_bp_command_set(struct command_context *cmd_ctx,
2682 uint32_t addr, uint32_t length, int hw)
2684 struct target *target = get_current_target(cmd_ctx);
2685 int retval = breakpoint_add(target, addr, length, hw);
2686 if (ERROR_OK == retval)
2687 command_print(cmd_ctx, "breakpoint set at 0x%8.8" PRIx32 "", addr);
2688 else
2689 LOG_ERROR("Failure setting breakpoint");
2690 return retval;
2693 COMMAND_HANDLER(handle_bp_command)
2695 if (CMD_ARGC == 0)
2696 return handle_bp_command_list(CMD_CTX);
2698 if (CMD_ARGC < 2 || CMD_ARGC > 3)
2700 command_print(CMD_CTX, "usage: bp <address> <length> ['hw']");
2701 return ERROR_COMMAND_SYNTAX_ERROR;
2704 uint32_t addr;
2705 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], addr);
2706 uint32_t length;
2707 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], length);
2709 int hw = BKPT_SOFT;
2710 if (CMD_ARGC == 3)
2712 if (strcmp(CMD_ARGV[2], "hw") == 0)
2713 hw = BKPT_HARD;
2714 else
2715 return ERROR_COMMAND_SYNTAX_ERROR;
2718 return handle_bp_command_set(CMD_CTX, addr, length, hw);
2721 COMMAND_HANDLER(handle_rbp_command)
2723 if (CMD_ARGC != 1)
2724 return ERROR_COMMAND_SYNTAX_ERROR;
2726 uint32_t addr;
2727 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], addr);
2729 struct target *target = get_current_target(CMD_CTX);
2730 breakpoint_remove(target, addr);
2732 return ERROR_OK;
2735 COMMAND_HANDLER(handle_wp_command)
2737 struct target *target = get_current_target(CMD_CTX);
2739 if (CMD_ARGC == 0)
2741 struct watchpoint *watchpoint = target->watchpoints;
2743 while (watchpoint)
2745 command_print(CMD_CTX, "address: 0x%8.8" PRIx32
2746 ", len: 0x%8.8" PRIx32
2747 ", r/w/a: %i, value: 0x%8.8" PRIx32
2748 ", mask: 0x%8.8" PRIx32,
2749 watchpoint->address,
2750 watchpoint->length,
2751 (int)watchpoint->rw,
2752 watchpoint->value,
2753 watchpoint->mask);
2754 watchpoint = watchpoint->next;
2756 return ERROR_OK;
2759 enum watchpoint_rw type = WPT_ACCESS;
2760 uint32_t addr = 0;
2761 uint32_t length = 0;
2762 uint32_t data_value = 0x0;
2763 uint32_t data_mask = 0xffffffff;
2765 switch (CMD_ARGC)
2767 case 5:
2768 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[4], data_mask);
2769 // fall through
2770 case 4:
2771 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[3], data_value);
2772 // fall through
2773 case 3:
2774 switch (CMD_ARGV[2][0])
2776 case 'r':
2777 type = WPT_READ;
2778 break;
2779 case 'w':
2780 type = WPT_WRITE;
2781 break;
2782 case 'a':
2783 type = WPT_ACCESS;
2784 break;
2785 default:
2786 LOG_ERROR("invalid watchpoint mode ('%c')", CMD_ARGV[2][0]);
2787 return ERROR_COMMAND_SYNTAX_ERROR;
2789 // fall through
2790 case 2:
2791 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], length);
2792 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], addr);
2793 break;
2795 default:
2796 command_print(CMD_CTX, "usage: wp [address length "
2797 "[(r|w|a) [value [mask]]]]");
2798 return ERROR_COMMAND_SYNTAX_ERROR;
2801 int retval = watchpoint_add(target, addr, length, type,
2802 data_value, data_mask);
2803 if (ERROR_OK != retval)
2804 LOG_ERROR("Failure setting watchpoints");
2806 return retval;
2809 COMMAND_HANDLER(handle_rwp_command)
2811 if (CMD_ARGC != 1)
2812 return ERROR_COMMAND_SYNTAX_ERROR;
2814 uint32_t addr;
2815 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], addr);
2817 struct target *target = get_current_target(CMD_CTX);
2818 watchpoint_remove(target, addr);
2820 return ERROR_OK;
2825 * Translate a virtual address to a physical address.
2827 * The low-level target implementation must have logged a detailed error
2828 * which is forwarded to telnet/GDB session.
2830 COMMAND_HANDLER(handle_virt2phys_command)
2832 if (CMD_ARGC != 1)
2833 return ERROR_COMMAND_SYNTAX_ERROR;
2835 uint32_t va;
2836 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], va);
2837 uint32_t pa;
2839 struct target *target = get_current_target(CMD_CTX);
2840 int retval = target->type->virt2phys(target, va, &pa);
2841 if (retval == ERROR_OK)
2842 command_print(CMD_CTX, "Physical address 0x%08" PRIx32 "", pa);
2844 return retval;
2847 static void writeData(FILE *f, const void *data, size_t len)
2849 size_t written = fwrite(data, 1, len, f);
2850 if (written != len)
2851 LOG_ERROR("failed to write %zu bytes: %s", len, strerror(errno));
2854 static void writeLong(FILE *f, int l)
2856 int i;
2857 for (i = 0; i < 4; i++)
2859 char c = (l >> (i*8))&0xff;
2860 writeData(f, &c, 1);
2865 static void writeString(FILE *f, char *s)
2867 writeData(f, s, strlen(s));
2870 /* Dump a gmon.out histogram file. */
2871 static void writeGmon(uint32_t *samples, uint32_t sampleNum, const char *filename)
2873 uint32_t i;
2874 FILE *f = fopen(filename, "w");
2875 if (f == NULL)
2876 return;
2877 writeString(f, "gmon");
2878 writeLong(f, 0x00000001); /* Version */
2879 writeLong(f, 0); /* padding */
2880 writeLong(f, 0); /* padding */
2881 writeLong(f, 0); /* padding */
2883 uint8_t zero = 0; /* GMON_TAG_TIME_HIST */
2884 writeData(f, &zero, 1);
2886 /* figure out bucket size */
2887 uint32_t min = samples[0];
2888 uint32_t max = samples[0];
2889 for (i = 0; i < sampleNum; i++)
2891 if (min > samples[i])
2893 min = samples[i];
2895 if (max < samples[i])
2897 max = samples[i];
2901 int addressSpace = (max-min + 1);
2903 static const uint32_t maxBuckets = 256 * 1024; /* maximum buckets. */
2904 uint32_t length = addressSpace;
2905 if (length > maxBuckets)
2907 length = maxBuckets;
2909 int *buckets = malloc(sizeof(int)*length);
2910 if (buckets == NULL)
2912 fclose(f);
2913 return;
2915 memset(buckets, 0, sizeof(int)*length);
2916 for (i = 0; i < sampleNum;i++)
2918 uint32_t address = samples[i];
2919 long long a = address-min;
2920 long long b = length-1;
2921 long long c = addressSpace-1;
2922 int index = (a*b)/c; /* danger!!!! int32 overflows */
2923 buckets[index]++;
2926 /* append binary memory gmon.out &profile_hist_hdr ((char*)&profile_hist_hdr + sizeof(struct gmon_hist_hdr)) */
2927 writeLong(f, min); /* low_pc */
2928 writeLong(f, max); /* high_pc */
2929 writeLong(f, length); /* # of samples */
2930 writeLong(f, 64000000); /* 64MHz */
2931 writeString(f, "seconds");
2932 for (i = 0; i < (15-strlen("seconds")); i++)
2933 writeData(f, &zero, 1);
2934 writeString(f, "s");
2936 /*append binary memory gmon.out profile_hist_data (profile_hist_data + profile_hist_hdr.hist_size) */
2938 char *data = malloc(2*length);
2939 if (data != NULL)
2941 for (i = 0; i < length;i++)
2943 int val;
2944 val = buckets[i];
2945 if (val > 65535)
2947 val = 65535;
2949 data[i*2]=val&0xff;
2950 data[i*2 + 1]=(val >> 8)&0xff;
2952 free(buckets);
2953 writeData(f, data, length * 2);
2954 free(data);
2955 } else
2957 free(buckets);
2960 fclose(f);
2963 /* profiling samples the CPU PC as quickly as OpenOCD is able, which will be used as a random sampling of PC */
2964 COMMAND_HANDLER(handle_profile_command)
2966 struct target *target = get_current_target(CMD_CTX);
2967 struct timeval timeout, now;
2969 gettimeofday(&timeout, NULL);
2970 if (CMD_ARGC != 2)
2972 return ERROR_COMMAND_SYNTAX_ERROR;
2974 unsigned offset;
2975 COMMAND_PARSE_NUMBER(uint, CMD_ARGV[0], offset);
2977 timeval_add_time(&timeout, offset, 0);
2979 command_print(CMD_CTX, "Starting profiling. Halting and resuming the target as often as we can...");
2981 static const int maxSample = 10000;
2982 uint32_t *samples = malloc(sizeof(uint32_t)*maxSample);
2983 if (samples == NULL)
2984 return ERROR_OK;
2986 int numSamples = 0;
2987 /* hopefully it is safe to cache! We want to stop/restart as quickly as possible. */
2988 struct reg *reg = register_get_by_name(target->reg_cache, "pc", 1);
2990 for (;;)
2992 int retval;
2993 target_poll(target);
2994 if (target->state == TARGET_HALTED)
2996 uint32_t t=*((uint32_t *)reg->value);
2997 samples[numSamples++]=t;
2998 retval = target_resume(target, 1, 0, 0, 0); /* current pc, addr = 0, do not handle breakpoints, not debugging */
2999 target_poll(target);
3000 alive_sleep(10); /* sleep 10ms, i.e. <100 samples/second. */
3001 } else if (target->state == TARGET_RUNNING)
3003 /* We want to quickly sample the PC. */
3004 if ((retval = target_halt(target)) != ERROR_OK)
3006 free(samples);
3007 return retval;
3009 } else
3011 command_print(CMD_CTX, "Target not halted or running");
3012 retval = ERROR_OK;
3013 break;
3015 if (retval != ERROR_OK)
3017 break;
3020 gettimeofday(&now, NULL);
3021 if ((numSamples >= maxSample) || ((now.tv_sec >= timeout.tv_sec) && (now.tv_usec >= timeout.tv_usec)))
3023 command_print(CMD_CTX, "Profiling completed. %d samples.", numSamples);
3024 if ((retval = target_poll(target)) != ERROR_OK)
3026 free(samples);
3027 return retval;
3029 if (target->state == TARGET_HALTED)
3031 target_resume(target, 1, 0, 0, 0); /* current pc, addr = 0, do not handle breakpoints, not debugging */
3033 if ((retval = target_poll(target)) != ERROR_OK)
3035 free(samples);
3036 return retval;
3038 writeGmon(samples, numSamples, CMD_ARGV[1]);
3039 command_print(CMD_CTX, "Wrote %s", CMD_ARGV[1]);
3040 break;
3043 free(samples);
3045 return ERROR_OK;
3048 static int new_int_array_element(Jim_Interp * interp, const char *varname, int idx, uint32_t val)
3050 char *namebuf;
3051 Jim_Obj *nameObjPtr, *valObjPtr;
3052 int result;
3054 namebuf = alloc_printf("%s(%d)", varname, idx);
3055 if (!namebuf)
3056 return JIM_ERR;
3058 nameObjPtr = Jim_NewStringObj(interp, namebuf, -1);
3059 valObjPtr = Jim_NewIntObj(interp, val);
3060 if (!nameObjPtr || !valObjPtr)
3062 free(namebuf);
3063 return JIM_ERR;
3066 Jim_IncrRefCount(nameObjPtr);
3067 Jim_IncrRefCount(valObjPtr);
3068 result = Jim_SetVariable(interp, nameObjPtr, valObjPtr);
3069 Jim_DecrRefCount(interp, nameObjPtr);
3070 Jim_DecrRefCount(interp, valObjPtr);
3071 free(namebuf);
3072 /* printf("%s(%d) <= 0%08x\n", varname, idx, val); */
3073 return result;
3076 static int jim_mem2array(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
3078 struct command_context *context;
3079 struct target *target;
3081 context = Jim_GetAssocData(interp, "context");
3082 if (context == NULL)
3084 LOG_ERROR("mem2array: no command context");
3085 return JIM_ERR;
3087 target = get_current_target(context);
3088 if (target == NULL)
3090 LOG_ERROR("mem2array: no current target");
3091 return JIM_ERR;
3094 return target_mem2array(interp, target, argc-1, argv + 1);
3097 static int target_mem2array(Jim_Interp *interp, struct target *target, int argc, Jim_Obj *const *argv)
3099 long l;
3100 uint32_t width;
3101 int len;
3102 uint32_t addr;
3103 uint32_t count;
3104 uint32_t v;
3105 const char *varname;
3106 int n, e, retval;
3107 uint32_t i;
3109 /* argv[1] = name of array to receive the data
3110 * argv[2] = desired width
3111 * argv[3] = memory address
3112 * argv[4] = count of times to read
3114 if (argc != 4) {
3115 Jim_WrongNumArgs(interp, 1, argv, "varname width addr nelems");
3116 return JIM_ERR;
3118 varname = Jim_GetString(argv[0], &len);
3119 /* given "foo" get space for worse case "foo(%d)" .. add 20 */
3121 e = Jim_GetLong(interp, argv[1], &l);
3122 width = l;
3123 if (e != JIM_OK) {
3124 return e;
3127 e = Jim_GetLong(interp, argv[2], &l);
3128 addr = l;
3129 if (e != JIM_OK) {
3130 return e;
3132 e = Jim_GetLong(interp, argv[3], &l);
3133 len = l;
3134 if (e != JIM_OK) {
3135 return e;
3137 switch (width) {
3138 case 8:
3139 width = 1;
3140 break;
3141 case 16:
3142 width = 2;
3143 break;
3144 case 32:
3145 width = 4;
3146 break;
3147 default:
3148 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3149 Jim_AppendStrings(interp, Jim_GetResult(interp), "Invalid width param, must be 8/16/32", NULL);
3150 return JIM_ERR;
3152 if (len == 0) {
3153 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3154 Jim_AppendStrings(interp, Jim_GetResult(interp), "mem2array: zero width read?", NULL);
3155 return JIM_ERR;
3157 if ((addr + (len * width)) < addr) {
3158 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3159 Jim_AppendStrings(interp, Jim_GetResult(interp), "mem2array: addr + len - wraps to zero?", NULL);
3160 return JIM_ERR;
3162 /* absurd transfer size? */
3163 if (len > 65536) {
3164 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3165 Jim_AppendStrings(interp, Jim_GetResult(interp), "mem2array: absurd > 64K item request", NULL);
3166 return JIM_ERR;
3169 if ((width == 1) ||
3170 ((width == 2) && ((addr & 1) == 0)) ||
3171 ((width == 4) && ((addr & 3) == 0))) {
3172 /* all is well */
3173 } else {
3174 char buf[100];
3175 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3176 sprintf(buf, "mem2array address: 0x%08" PRIx32 " is not aligned for %" PRId32 " byte reads",
3177 addr,
3178 width);
3179 Jim_AppendStrings(interp, Jim_GetResult(interp), buf , NULL);
3180 return JIM_ERR;
3183 /* Transfer loop */
3185 /* index counter */
3186 n = 0;
3188 size_t buffersize = 4096;
3189 uint8_t *buffer = malloc(buffersize);
3190 if (buffer == NULL)
3191 return JIM_ERR;
3193 /* assume ok */
3194 e = JIM_OK;
3195 while (len) {
3196 /* Slurp... in buffer size chunks */
3198 count = len; /* in objects.. */
3199 if (count > (buffersize/width)) {
3200 count = (buffersize/width);
3203 retval = target_read_memory(target, addr, width, count, buffer);
3204 if (retval != ERROR_OK) {
3205 /* BOO !*/
3206 LOG_ERROR("mem2array: Read @ 0x%08x, w=%d, cnt=%d, failed",
3207 (unsigned int)addr,
3208 (int)width,
3209 (int)count);
3210 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3211 Jim_AppendStrings(interp, Jim_GetResult(interp), "mem2array: cannot read memory", NULL);
3212 e = JIM_ERR;
3213 len = 0;
3214 } else {
3215 v = 0; /* shut up gcc */
3216 for (i = 0 ;i < count ;i++, n++) {
3217 switch (width) {
3218 case 4:
3219 v = target_buffer_get_u32(target, &buffer[i*width]);
3220 break;
3221 case 2:
3222 v = target_buffer_get_u16(target, &buffer[i*width]);
3223 break;
3224 case 1:
3225 v = buffer[i] & 0x0ff;
3226 break;
3228 new_int_array_element(interp, varname, n, v);
3230 len -= count;
3234 free(buffer);
3236 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3238 return JIM_OK;
3241 static int get_int_array_element(Jim_Interp * interp, const char *varname, int idx, uint32_t *val)
3243 char *namebuf;
3244 Jim_Obj *nameObjPtr, *valObjPtr;
3245 int result;
3246 long l;
3248 namebuf = alloc_printf("%s(%d)", varname, idx);
3249 if (!namebuf)
3250 return JIM_ERR;
3252 nameObjPtr = Jim_NewStringObj(interp, namebuf, -1);
3253 if (!nameObjPtr)
3255 free(namebuf);
3256 return JIM_ERR;
3259 Jim_IncrRefCount(nameObjPtr);
3260 valObjPtr = Jim_GetVariable(interp, nameObjPtr, JIM_ERRMSG);
3261 Jim_DecrRefCount(interp, nameObjPtr);
3262 free(namebuf);
3263 if (valObjPtr == NULL)
3264 return JIM_ERR;
3266 result = Jim_GetLong(interp, valObjPtr, &l);
3267 /* printf("%s(%d) => 0%08x\n", varname, idx, val); */
3268 *val = l;
3269 return result;
3272 static int jim_array2mem(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
3274 struct command_context *context;
3275 struct target *target;
3277 context = Jim_GetAssocData(interp, "context");
3278 if (context == NULL) {
3279 LOG_ERROR("array2mem: no command context");
3280 return JIM_ERR;
3282 target = get_current_target(context);
3283 if (target == NULL) {
3284 LOG_ERROR("array2mem: no current target");
3285 return JIM_ERR;
3288 return target_array2mem(interp,target, argc-1, argv + 1);
3290 static int target_array2mem(Jim_Interp *interp, struct target *target, int argc, Jim_Obj *const *argv)
3292 long l;
3293 uint32_t width;
3294 int len;
3295 uint32_t addr;
3296 uint32_t count;
3297 uint32_t v;
3298 const char *varname;
3299 int n, e, retval;
3300 uint32_t i;
3302 /* argv[1] = name of array to get the data
3303 * argv[2] = desired width
3304 * argv[3] = memory address
3305 * argv[4] = count to write
3307 if (argc != 4) {
3308 Jim_WrongNumArgs(interp, 0, argv, "varname width addr nelems");
3309 return JIM_ERR;
3311 varname = Jim_GetString(argv[0], &len);
3312 /* given "foo" get space for worse case "foo(%d)" .. add 20 */
3314 e = Jim_GetLong(interp, argv[1], &l);
3315 width = l;
3316 if (e != JIM_OK) {
3317 return e;
3320 e = Jim_GetLong(interp, argv[2], &l);
3321 addr = l;
3322 if (e != JIM_OK) {
3323 return e;
3325 e = Jim_GetLong(interp, argv[3], &l);
3326 len = l;
3327 if (e != JIM_OK) {
3328 return e;
3330 switch (width) {
3331 case 8:
3332 width = 1;
3333 break;
3334 case 16:
3335 width = 2;
3336 break;
3337 case 32:
3338 width = 4;
3339 break;
3340 default:
3341 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3342 Jim_AppendStrings(interp, Jim_GetResult(interp), "Invalid width param, must be 8/16/32", NULL);
3343 return JIM_ERR;
3345 if (len == 0) {
3346 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3347 Jim_AppendStrings(interp, Jim_GetResult(interp), "array2mem: zero width read?", NULL);
3348 return JIM_ERR;
3350 if ((addr + (len * width)) < addr) {
3351 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3352 Jim_AppendStrings(interp, Jim_GetResult(interp), "array2mem: addr + len - wraps to zero?", NULL);
3353 return JIM_ERR;
3355 /* absurd transfer size? */
3356 if (len > 65536) {
3357 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3358 Jim_AppendStrings(interp, Jim_GetResult(interp), "array2mem: absurd > 64K item request", NULL);
3359 return JIM_ERR;
3362 if ((width == 1) ||
3363 ((width == 2) && ((addr & 1) == 0)) ||
3364 ((width == 4) && ((addr & 3) == 0))) {
3365 /* all is well */
3366 } else {
3367 char buf[100];
3368 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3369 sprintf(buf, "array2mem address: 0x%08x is not aligned for %d byte reads",
3370 (unsigned int)addr,
3371 (int)width);
3372 Jim_AppendStrings(interp, Jim_GetResult(interp), buf , NULL);
3373 return JIM_ERR;
3376 /* Transfer loop */
3378 /* index counter */
3379 n = 0;
3380 /* assume ok */
3381 e = JIM_OK;
3383 size_t buffersize = 4096;
3384 uint8_t *buffer = malloc(buffersize);
3385 if (buffer == NULL)
3386 return JIM_ERR;
3388 while (len) {
3389 /* Slurp... in buffer size chunks */
3391 count = len; /* in objects.. */
3392 if (count > (buffersize/width)) {
3393 count = (buffersize/width);
3396 v = 0; /* shut up gcc */
3397 for (i = 0 ;i < count ;i++, n++) {
3398 get_int_array_element(interp, varname, n, &v);
3399 switch (width) {
3400 case 4:
3401 target_buffer_set_u32(target, &buffer[i*width], v);
3402 break;
3403 case 2:
3404 target_buffer_set_u16(target, &buffer[i*width], v);
3405 break;
3406 case 1:
3407 buffer[i] = v & 0x0ff;
3408 break;
3411 len -= count;
3413 retval = target_write_memory(target, addr, width, count, buffer);
3414 if (retval != ERROR_OK) {
3415 /* BOO !*/
3416 LOG_ERROR("array2mem: Write @ 0x%08x, w=%d, cnt=%d, failed",
3417 (unsigned int)addr,
3418 (int)width,
3419 (int)count);
3420 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3421 Jim_AppendStrings(interp, Jim_GetResult(interp), "array2mem: cannot read memory", NULL);
3422 e = JIM_ERR;
3423 len = 0;
3427 free(buffer);
3429 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3431 return JIM_OK;
3434 void target_all_handle_event(enum target_event e)
3436 struct target *target;
3438 LOG_DEBUG("**all*targets: event: %d, %s",
3439 (int)e,
3440 Jim_Nvp_value2name_simple(nvp_target_event, e)->name);
3442 target = all_targets;
3443 while (target) {
3444 target_handle_event(target, e);
3445 target = target->next;
3450 /* FIX? should we propagate errors here rather than printing them
3451 * and continuing?
3453 void target_handle_event(struct target *target, enum target_event e)
3455 struct target_event_action *teap;
3457 for (teap = target->event_action; teap != NULL; teap = teap->next) {
3458 if (teap->event == e) {
3459 LOG_DEBUG("target: (%d) %s (%s) event: %d (%s) action: %s",
3460 target->target_number,
3461 target_name(target),
3462 target_type_name(target),
3464 Jim_Nvp_value2name_simple(nvp_target_event, e)->name,
3465 Jim_GetString(teap->body, NULL));
3466 if (Jim_EvalObj(teap->interp, teap->body) != JIM_OK)
3468 Jim_PrintErrorMessage(teap->interp);
3475 * Returns true only if the target has a handler for the specified event.
3477 bool target_has_event_action(struct target *target, enum target_event event)
3479 struct target_event_action *teap;
3481 for (teap = target->event_action; teap != NULL; teap = teap->next) {
3482 if (teap->event == event)
3483 return true;
3485 return false;
3488 enum target_cfg_param {
3489 TCFG_TYPE,
3490 TCFG_EVENT,
3491 TCFG_WORK_AREA_VIRT,
3492 TCFG_WORK_AREA_PHYS,
3493 TCFG_WORK_AREA_SIZE,
3494 TCFG_WORK_AREA_BACKUP,
3495 TCFG_ENDIAN,
3496 TCFG_VARIANT,
3497 TCFG_CHAIN_POSITION,
3500 static Jim_Nvp nvp_config_opts[] = {
3501 { .name = "-type", .value = TCFG_TYPE },
3502 { .name = "-event", .value = TCFG_EVENT },
3503 { .name = "-work-area-virt", .value = TCFG_WORK_AREA_VIRT },
3504 { .name = "-work-area-phys", .value = TCFG_WORK_AREA_PHYS },
3505 { .name = "-work-area-size", .value = TCFG_WORK_AREA_SIZE },
3506 { .name = "-work-area-backup", .value = TCFG_WORK_AREA_BACKUP },
3507 { .name = "-endian" , .value = TCFG_ENDIAN },
3508 { .name = "-variant", .value = TCFG_VARIANT },
3509 { .name = "-chain-position", .value = TCFG_CHAIN_POSITION },
3511 { .name = NULL, .value = -1 }
3514 static int target_configure(Jim_GetOptInfo *goi, struct target *target)
3516 Jim_Nvp *n;
3517 Jim_Obj *o;
3518 jim_wide w;
3519 char *cp;
3520 int e;
3522 /* parse config or cget options ... */
3523 while (goi->argc > 0) {
3524 Jim_SetEmptyResult(goi->interp);
3525 /* Jim_GetOpt_Debug(goi); */
3527 if (target->type->target_jim_configure) {
3528 /* target defines a configure function */
3529 /* target gets first dibs on parameters */
3530 e = (*(target->type->target_jim_configure))(target, goi);
3531 if (e == JIM_OK) {
3532 /* more? */
3533 continue;
3535 if (e == JIM_ERR) {
3536 /* An error */
3537 return e;
3539 /* otherwise we 'continue' below */
3541 e = Jim_GetOpt_Nvp(goi, nvp_config_opts, &n);
3542 if (e != JIM_OK) {
3543 Jim_GetOpt_NvpUnknown(goi, nvp_config_opts, 0);
3544 return e;
3546 switch (n->value) {
3547 case TCFG_TYPE:
3548 /* not setable */
3549 if (goi->isconfigure) {
3550 Jim_SetResult_sprintf(goi->interp,
3551 "not settable: %s", n->name);
3552 return JIM_ERR;
3553 } else {
3554 no_params:
3555 if (goi->argc != 0) {
3556 Jim_WrongNumArgs(goi->interp,
3557 goi->argc, goi->argv,
3558 "NO PARAMS");
3559 return JIM_ERR;
3562 Jim_SetResultString(goi->interp,
3563 target_type_name(target), -1);
3564 /* loop for more */
3565 break;
3566 case TCFG_EVENT:
3567 if (goi->argc == 0) {
3568 Jim_WrongNumArgs(goi->interp, goi->argc, goi->argv, "-event ?event-name? ...");
3569 return JIM_ERR;
3572 e = Jim_GetOpt_Nvp(goi, nvp_target_event, &n);
3573 if (e != JIM_OK) {
3574 Jim_GetOpt_NvpUnknown(goi, nvp_target_event, 1);
3575 return e;
3578 if (goi->isconfigure) {
3579 if (goi->argc != 1) {
3580 Jim_WrongNumArgs(goi->interp, goi->argc, goi->argv, "-event ?event-name? ?EVENT-BODY?");
3581 return JIM_ERR;
3583 } else {
3584 if (goi->argc != 0) {
3585 Jim_WrongNumArgs(goi->interp, goi->argc, goi->argv, "-event ?event-name?");
3586 return JIM_ERR;
3591 struct target_event_action *teap;
3593 teap = target->event_action;
3594 /* replace existing? */
3595 while (teap) {
3596 if (teap->event == (enum target_event)n->value) {
3597 break;
3599 teap = teap->next;
3602 if (goi->isconfigure) {
3603 bool replace = true;
3604 if (teap == NULL) {
3605 /* create new */
3606 teap = calloc(1, sizeof(*teap));
3607 replace = false;
3609 teap->event = n->value;
3610 teap->interp = goi->interp;
3611 Jim_GetOpt_Obj(goi, &o);
3612 if (teap->body) {
3613 Jim_DecrRefCount(teap->interp, teap->body);
3615 teap->body = Jim_DuplicateObj(goi->interp, o);
3617 * FIXME:
3618 * Tcl/TK - "tk events" have a nice feature.
3619 * See the "BIND" command.
3620 * We should support that here.
3621 * You can specify %X and %Y in the event code.
3622 * The idea is: %T - target name.
3623 * The idea is: %N - target number
3624 * The idea is: %E - event name.
3626 Jim_IncrRefCount(teap->body);
3628 if (!replace)
3630 /* add to head of event list */
3631 teap->next = target->event_action;
3632 target->event_action = teap;
3634 Jim_SetEmptyResult(goi->interp);
3635 } else {
3636 /* get */
3637 if (teap == NULL) {
3638 Jim_SetEmptyResult(goi->interp);
3639 } else {
3640 Jim_SetResult(goi->interp, Jim_DuplicateObj(goi->interp, teap->body));
3644 /* loop for more */
3645 break;
3647 case TCFG_WORK_AREA_VIRT:
3648 if (goi->isconfigure) {
3649 target_free_all_working_areas(target);
3650 e = Jim_GetOpt_Wide(goi, &w);
3651 if (e != JIM_OK) {
3652 return e;
3654 target->working_area_virt = w;
3655 target->working_area_virt_spec = true;
3656 } else {
3657 if (goi->argc != 0) {
3658 goto no_params;
3661 Jim_SetResult(goi->interp, Jim_NewIntObj(goi->interp, target->working_area_virt));
3662 /* loop for more */
3663 break;
3665 case TCFG_WORK_AREA_PHYS:
3666 if (goi->isconfigure) {
3667 target_free_all_working_areas(target);
3668 e = Jim_GetOpt_Wide(goi, &w);
3669 if (e != JIM_OK) {
3670 return e;
3672 target->working_area_phys = w;
3673 target->working_area_phys_spec = true;
3674 } else {
3675 if (goi->argc != 0) {
3676 goto no_params;
3679 Jim_SetResult(goi->interp, Jim_NewIntObj(goi->interp, target->working_area_phys));
3680 /* loop for more */
3681 break;
3683 case TCFG_WORK_AREA_SIZE:
3684 if (goi->isconfigure) {
3685 target_free_all_working_areas(target);
3686 e = Jim_GetOpt_Wide(goi, &w);
3687 if (e != JIM_OK) {
3688 return e;
3690 target->working_area_size = w;
3691 } else {
3692 if (goi->argc != 0) {
3693 goto no_params;
3696 Jim_SetResult(goi->interp, Jim_NewIntObj(goi->interp, target->working_area_size));
3697 /* loop for more */
3698 break;
3700 case TCFG_WORK_AREA_BACKUP:
3701 if (goi->isconfigure) {
3702 target_free_all_working_areas(target);
3703 e = Jim_GetOpt_Wide(goi, &w);
3704 if (e != JIM_OK) {
3705 return e;
3707 /* make this exactly 1 or 0 */
3708 target->backup_working_area = (!!w);
3709 } else {
3710 if (goi->argc != 0) {
3711 goto no_params;
3714 Jim_SetResult(goi->interp, Jim_NewIntObj(goi->interp, target->backup_working_area));
3715 /* loop for more e*/
3716 break;
3718 case TCFG_ENDIAN:
3719 if (goi->isconfigure) {
3720 e = Jim_GetOpt_Nvp(goi, nvp_target_endian, &n);
3721 if (e != JIM_OK) {
3722 Jim_GetOpt_NvpUnknown(goi, nvp_target_endian, 1);
3723 return e;
3725 target->endianness = n->value;
3726 } else {
3727 if (goi->argc != 0) {
3728 goto no_params;
3731 n = Jim_Nvp_value2name_simple(nvp_target_endian, target->endianness);
3732 if (n->name == NULL) {
3733 target->endianness = TARGET_LITTLE_ENDIAN;
3734 n = Jim_Nvp_value2name_simple(nvp_target_endian, target->endianness);
3736 Jim_SetResultString(goi->interp, n->name, -1);
3737 /* loop for more */
3738 break;
3740 case TCFG_VARIANT:
3741 if (goi->isconfigure) {
3742 if (goi->argc < 1) {
3743 Jim_SetResult_sprintf(goi->interp,
3744 "%s ?STRING?",
3745 n->name);
3746 return JIM_ERR;
3748 if (target->variant) {
3749 free((void *)(target->variant));
3751 e = Jim_GetOpt_String(goi, &cp, NULL);
3752 target->variant = strdup(cp);
3753 } else {
3754 if (goi->argc != 0) {
3755 goto no_params;
3758 Jim_SetResultString(goi->interp, target->variant,-1);
3759 /* loop for more */
3760 break;
3761 case TCFG_CHAIN_POSITION:
3762 if (goi->isconfigure) {
3763 Jim_Obj *o;
3764 struct jtag_tap *tap;
3765 target_free_all_working_areas(target);
3766 e = Jim_GetOpt_Obj(goi, &o);
3767 if (e != JIM_OK) {
3768 return e;
3770 tap = jtag_tap_by_jim_obj(goi->interp, o);
3771 if (tap == NULL) {
3772 return JIM_ERR;
3774 /* make this exactly 1 or 0 */
3775 target->tap = tap;
3776 } else {
3777 if (goi->argc != 0) {
3778 goto no_params;
3781 Jim_SetResultString(goi->interp, target->tap->dotted_name, -1);
3782 /* loop for more e*/
3783 break;
3785 } /* while (goi->argc) */
3788 /* done - we return */
3789 return JIM_OK;
3792 static int jim_target_configure(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
3794 Jim_GetOptInfo goi;
3795 Jim_GetOpt_Setup(&goi, interp, argc - 1, argv + 1);
3796 goi.isconfigure = strcmp(Jim_GetString(argv[0], NULL), "configure") == 0;
3797 int need_args = 1 + goi.isconfigure;
3798 if (goi.argc < need_args)
3800 Jim_WrongNumArgs(goi.interp, goi.argc, goi.argv,
3801 goi.isconfigure
3802 ? "missing: -option VALUE ..."
3803 : "missing: -option ...");
3804 return JIM_ERR;
3806 struct target *target = Jim_CmdPrivData(goi.interp);
3807 return target_configure(&goi, target);
3810 static int jim_target_mw(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
3812 const char *cmd_name = Jim_GetString(argv[0], NULL);
3814 Jim_GetOptInfo goi;
3815 Jim_GetOpt_Setup(&goi, interp, argc - 1, argv + 1);
3817 if (goi.argc != 2 && goi.argc != 3)
3819 Jim_SetResult_sprintf(goi.interp,
3820 "usage: %s <address> <data> [<count>]", cmd_name);
3821 return JIM_ERR;
3824 jim_wide a;
3825 int e = Jim_GetOpt_Wide(&goi, &a);
3826 if (e != JIM_OK)
3827 return e;
3829 jim_wide b;
3830 e = Jim_GetOpt_Wide(&goi, &b);
3831 if (e != JIM_OK)
3832 return e;
3834 jim_wide c = 1;
3835 if (goi.argc == 3)
3837 e = Jim_GetOpt_Wide(&goi, &c);
3838 if (e != JIM_OK)
3839 return e;
3842 struct target *target = Jim_CmdPrivData(goi.interp);
3843 uint8_t target_buf[32];
3844 if (strcasecmp(cmd_name, "mww") == 0) {
3845 target_buffer_set_u32(target, target_buf, b);
3846 b = 4;
3848 else if (strcasecmp(cmd_name, "mwh") == 0) {
3849 target_buffer_set_u16(target, target_buf, b);
3850 b = 2;
3852 else if (strcasecmp(cmd_name, "mwb") == 0) {
3853 target_buffer_set_u8(target, target_buf, b);
3854 b = 1;
3855 } else {
3856 LOG_ERROR("command '%s' unknown: ", cmd_name);
3857 return JIM_ERR;
3860 for (jim_wide x = 0; x < c; x++)
3862 e = target_write_memory(target, a, b, 1, target_buf);
3863 if (e != ERROR_OK)
3865 Jim_SetResult_sprintf(interp,
3866 "Error writing @ 0x%08x: %d\n", (int)(a), e);
3867 return JIM_ERR;
3869 /* b = width */
3870 a = a + b;
3872 return JIM_OK;
3875 static int jim_target_md(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
3877 const char *cmd_name = Jim_GetString(argv[0], NULL);
3879 Jim_GetOptInfo goi;
3880 Jim_GetOpt_Setup(&goi, interp, argc - 1, argv + 1);
3882 if ((goi.argc == 2) || (goi.argc == 3))
3884 Jim_SetResult_sprintf(goi.interp,
3885 "usage: %s <address> [<count>]", cmd_name);
3886 return JIM_ERR;
3889 jim_wide a;
3890 int e = Jim_GetOpt_Wide(&goi, &a);
3891 if (e != JIM_OK) {
3892 return JIM_ERR;
3894 jim_wide c;
3895 if (goi.argc) {
3896 e = Jim_GetOpt_Wide(&goi, &c);
3897 if (e != JIM_OK) {
3898 return JIM_ERR;
3900 } else {
3901 c = 1;
3903 jim_wide b = 1; /* shut up gcc */
3904 if (strcasecmp(cmd_name, "mdw") == 0)
3905 b = 4;
3906 else if (strcasecmp(cmd_name, "mdh") == 0)
3907 b = 2;
3908 else if (strcasecmp(cmd_name, "mdb") == 0)
3909 b = 1;
3910 else {
3911 LOG_ERROR("command '%s' unknown: ", cmd_name);
3912 return JIM_ERR;
3915 /* convert count to "bytes" */
3916 c = c * b;
3918 struct target *target = Jim_CmdPrivData(goi.interp);
3919 uint8_t target_buf[32];
3920 jim_wide x, y, z;
3921 while (c > 0) {
3922 y = c;
3923 if (y > 16) {
3924 y = 16;
3926 e = target_read_memory(target, a, b, y / b, target_buf);
3927 if (e != ERROR_OK) {
3928 Jim_SetResult_sprintf(interp, "error reading target @ 0x%08lx", (int)(a));
3929 return JIM_ERR;
3932 Jim_fprintf(interp, interp->cookie_stdout, "0x%08x ", (int)(a));
3933 switch (b) {
3934 case 4:
3935 for (x = 0; x < 16 && x < y; x += 4)
3937 z = target_buffer_get_u32(target, &(target_buf[ x * 4 ]));
3938 Jim_fprintf(interp, interp->cookie_stdout, "%08x ", (int)(z));
3940 for (; (x < 16) ; x += 4) {
3941 Jim_fprintf(interp, interp->cookie_stdout, " ");
3943 break;
3944 case 2:
3945 for (x = 0; x < 16 && x < y; x += 2)
3947 z = target_buffer_get_u16(target, &(target_buf[ x * 2 ]));
3948 Jim_fprintf(interp, interp->cookie_stdout, "%04x ", (int)(z));
3950 for (; (x < 16) ; x += 2) {
3951 Jim_fprintf(interp, interp->cookie_stdout, " ");
3953 break;
3954 case 1:
3955 default:
3956 for (x = 0 ; (x < 16) && (x < y) ; x += 1) {
3957 z = target_buffer_get_u8(target, &(target_buf[ x * 4 ]));
3958 Jim_fprintf(interp, interp->cookie_stdout, "%02x ", (int)(z));
3960 for (; (x < 16) ; x += 1) {
3961 Jim_fprintf(interp, interp->cookie_stdout, " ");
3963 break;
3965 /* ascii-ify the bytes */
3966 for (x = 0 ; x < y ; x++) {
3967 if ((target_buf[x] >= 0x20) &&
3968 (target_buf[x] <= 0x7e)) {
3969 /* good */
3970 } else {
3971 /* smack it */
3972 target_buf[x] = '.';
3975 /* space pad */
3976 while (x < 16) {
3977 target_buf[x] = ' ';
3978 x++;
3980 /* terminate */
3981 target_buf[16] = 0;
3982 /* print - with a newline */
3983 Jim_fprintf(interp, interp->cookie_stdout, "%s\n", target_buf);
3984 /* NEXT... */
3985 c -= 16;
3986 a += 16;
3988 return JIM_OK;
3991 static int jim_target_mem2array(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
3993 struct target *target = Jim_CmdPrivData(interp);
3994 return target_mem2array(interp, target, argc - 1, argv + 1);
3997 static int jim_target_array2mem(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
3999 struct target *target = Jim_CmdPrivData(interp);
4000 return target_array2mem(interp, target, argc - 1, argv + 1);
4003 static int jim_target_tap_disabled(Jim_Interp *interp)
4005 Jim_SetResult_sprintf(interp, "[TAP is disabled]");
4006 return JIM_ERR;
4009 static int jim_target_examine(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
4011 if (argc != 1)
4013 Jim_WrongNumArgs(interp, 1, argv, "[no parameters]");
4014 return JIM_ERR;
4016 struct target *target = Jim_CmdPrivData(interp);
4017 if (!target->tap->enabled)
4018 return jim_target_tap_disabled(interp);
4020 int e = target->type->examine(target);
4021 if (e != ERROR_OK)
4023 Jim_SetResult_sprintf(interp, "examine-fails: %d", e);
4024 return JIM_ERR;
4026 return JIM_OK;
4029 static int jim_target_poll(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
4031 if (argc != 1)
4033 Jim_WrongNumArgs(interp, 1, argv, "[no parameters]");
4034 return JIM_ERR;
4036 struct target *target = Jim_CmdPrivData(interp);
4037 if (!target->tap->enabled)
4038 return jim_target_tap_disabled(interp);
4040 int e;
4041 if (!(target_was_examined(target))) {
4042 e = ERROR_TARGET_NOT_EXAMINED;
4043 } else {
4044 e = target->type->poll(target);
4046 if (e != ERROR_OK)
4048 Jim_SetResult_sprintf(interp, "poll-fails: %d", e);
4049 return JIM_ERR;
4051 return JIM_OK;
4054 static int jim_target_reset(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
4056 Jim_GetOptInfo goi;
4057 Jim_GetOpt_Setup(&goi, interp, argc - 1, argv + 1);
4059 if (goi.argc != 2)
4061 Jim_WrongNumArgs(interp, 0, argv,
4062 "([tT]|[fF]|assert|deassert) BOOL");
4063 return JIM_ERR;
4066 Jim_Nvp *n;
4067 int e = Jim_GetOpt_Nvp(&goi, nvp_assert, &n);
4068 if (e != JIM_OK)
4070 Jim_GetOpt_NvpUnknown(&goi, nvp_assert, 1);
4071 return e;
4073 /* the halt or not param */
4074 jim_wide a;
4075 e = Jim_GetOpt_Wide(&goi, &a);
4076 if (e != JIM_OK)
4077 return e;
4079 struct target *target = Jim_CmdPrivData(goi.interp);
4080 if (!target->tap->enabled)
4081 return jim_target_tap_disabled(interp);
4082 if (!(target_was_examined(target)))
4084 LOG_ERROR("Target not examined yet");
4085 return ERROR_TARGET_NOT_EXAMINED;
4087 if (!target->type->assert_reset || !target->type->deassert_reset)
4089 Jim_SetResult_sprintf(interp,
4090 "No target-specific reset for %s",
4091 target_name(target));
4092 return JIM_ERR;
4094 /* determine if we should halt or not. */
4095 target->reset_halt = !!a;
4096 /* When this happens - all workareas are invalid. */
4097 target_free_all_working_areas_restore(target, 0);
4099 /* do the assert */
4100 if (n->value == NVP_ASSERT) {
4101 e = target->type->assert_reset(target);
4102 } else {
4103 e = target->type->deassert_reset(target);
4105 return (e == ERROR_OK) ? JIM_OK : JIM_ERR;
4108 static int jim_target_halt(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
4110 if (argc != 1) {
4111 Jim_WrongNumArgs(interp, 1, argv, "[no parameters]");
4112 return JIM_ERR;
4114 struct target *target = Jim_CmdPrivData(interp);
4115 if (!target->tap->enabled)
4116 return jim_target_tap_disabled(interp);
4117 int e = target->type->halt(target);
4118 return (e == ERROR_OK) ? JIM_OK : JIM_ERR;
4121 static int jim_target_wait_state(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
4123 Jim_GetOptInfo goi;
4124 Jim_GetOpt_Setup(&goi, interp, argc - 1, argv + 1);
4126 /* params: <name> statename timeoutmsecs */
4127 if (goi.argc != 2)
4129 const char *cmd_name = Jim_GetString(argv[0], NULL);
4130 Jim_SetResult_sprintf(goi.interp,
4131 "%s <state_name> <timeout_in_msec>", cmd_name);
4132 return JIM_ERR;
4135 Jim_Nvp *n;
4136 int e = Jim_GetOpt_Nvp(&goi, nvp_target_state, &n);
4137 if (e != JIM_OK) {
4138 Jim_GetOpt_NvpUnknown(&goi, nvp_target_state,1);
4139 return e;
4141 jim_wide a;
4142 e = Jim_GetOpt_Wide(&goi, &a);
4143 if (e != JIM_OK) {
4144 return e;
4146 struct target *target = Jim_CmdPrivData(interp);
4147 if (!target->tap->enabled)
4148 return jim_target_tap_disabled(interp);
4150 e = target_wait_state(target, n->value, a);
4151 if (e != ERROR_OK)
4153 Jim_SetResult_sprintf(goi.interp,
4154 "target: %s wait %s fails (%d) %s",
4155 target_name(target), n->name,
4156 e, target_strerror_safe(e));
4157 return JIM_ERR;
4159 return JIM_OK;
4161 /* List for human, Events defined for this target.
4162 * scripts/programs should use 'name cget -event NAME'
4164 static int jim_target_event_list(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
4166 struct command_context *cmd_ctx = Jim_GetAssocData(interp, "context");
4167 struct target *target = Jim_CmdPrivData(interp);
4168 struct target_event_action *teap = target->event_action;
4169 command_print(cmd_ctx, "Event actions for target (%d) %s\n",
4170 target->target_number,
4171 target_name(target));
4172 command_print(cmd_ctx, "%-25s | Body", "Event");
4173 command_print(cmd_ctx, "------------------------- | "
4174 "----------------------------------------");
4175 while (teap)
4177 Jim_Nvp *opt = Jim_Nvp_value2name_simple(nvp_target_event, teap->event);
4178 command_print(cmd_ctx, "%-25s | %s",
4179 opt->name, Jim_GetString(teap->body, NULL));
4180 teap = teap->next;
4182 command_print(cmd_ctx, "***END***");
4183 return JIM_OK;
4185 static int jim_target_current_state(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
4187 if (argc != 1)
4189 Jim_WrongNumArgs(interp, 1, argv, "[no parameters]");
4190 return JIM_ERR;
4192 struct target *target = Jim_CmdPrivData(interp);
4193 Jim_SetResultString(interp, target_state_name(target), -1);
4194 return JIM_OK;
4196 static int jim_target_invoke_event(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
4198 Jim_GetOptInfo goi;
4199 Jim_GetOpt_Setup(&goi, interp, argc - 1, argv + 1);
4200 if (goi.argc != 1)
4202 const char *cmd_name = Jim_GetString(argv[0], NULL);
4203 Jim_SetResult_sprintf(goi.interp, "%s <eventname>", cmd_name);
4204 return JIM_ERR;
4206 Jim_Nvp *n;
4207 int e = Jim_GetOpt_Nvp(&goi, nvp_target_event, &n);
4208 if (e != JIM_OK)
4210 Jim_GetOpt_NvpUnknown(&goi, nvp_target_event, 1);
4211 return e;
4213 struct target *target = Jim_CmdPrivData(interp);
4214 target_handle_event(target, n->value);
4215 return JIM_OK;
4218 static const struct command_registration target_instance_command_handlers[] = {
4220 .name = "configure",
4221 .mode = COMMAND_CONFIG,
4222 .jim_handler = &jim_target_configure,
4223 .usage = "[<target_options> ...]",
4224 .help = "configure a new target for use",
4227 .name = "cget",
4228 .mode = COMMAND_ANY,
4229 .jim_handler = &jim_target_configure,
4230 .usage = "<target_type> [<target_options> ...]",
4231 .help = "configure a new target for use",
4234 .name = "mww",
4235 .mode = COMMAND_EXEC,
4236 .jim_handler = &jim_target_mw,
4237 .usage = "<address> <data> [<count>]",
4238 .help = "Write 32-bit word(s) to target memory",
4241 .name = "mwh",
4242 .mode = COMMAND_EXEC,
4243 .jim_handler = &jim_target_mw,
4244 .usage = "<address> <data> [<count>]",
4245 .help = "Write 16-bit half-word(s) to target memory",
4248 .name = "mwb",
4249 .mode = COMMAND_EXEC,
4250 .jim_handler = &jim_target_mw,
4251 .usage = "<address> <data> [<count>]",
4252 .help = "Write byte(s) to target memory",
4255 .name = "mdw",
4256 .mode = COMMAND_EXEC,
4257 .jim_handler = &jim_target_md,
4258 .usage = "<address> [<count>]",
4259 .help = "Display target memory as 32-bit words",
4262 .name = "mdh",
4263 .mode = COMMAND_EXEC,
4264 .jim_handler = &jim_target_md,
4265 .usage = "<address> [<count>]",
4266 .help = "Display target memory as 16-bit half-words",
4269 .name = "mdb",
4270 .mode = COMMAND_EXEC,
4271 .jim_handler = &jim_target_md,
4272 .usage = "<address> [<count>]",
4273 .help = "Display target memory as 8-bit bytes",
4276 .name = "array2mem",
4277 .mode = COMMAND_EXEC,
4278 .jim_handler = &jim_target_array2mem,
4281 .name = "mem2array",
4282 .mode = COMMAND_EXEC,
4283 .jim_handler = &jim_target_mem2array,
4286 .name = "eventlist",
4287 .mode = COMMAND_EXEC,
4288 .jim_handler = &jim_target_event_list,
4291 .name = "curstate",
4292 .mode = COMMAND_EXEC,
4293 .jim_handler = &jim_target_current_state,
4296 .name = "arp_examine",
4297 .mode = COMMAND_EXEC,
4298 .jim_handler = &jim_target_examine,
4301 .name = "arp_poll",
4302 .mode = COMMAND_EXEC,
4303 .jim_handler = &jim_target_poll,
4306 .name = "arp_reset",
4307 .mode = COMMAND_EXEC,
4308 .jim_handler = &jim_target_reset,
4311 .name = "arp_halt",
4312 .mode = COMMAND_EXEC,
4313 .jim_handler = &jim_target_halt,
4316 .name = "arp_waitstate",
4317 .mode = COMMAND_EXEC,
4318 .jim_handler = &jim_target_wait_state,
4321 .name = "invoke-event",
4322 .mode = COMMAND_EXEC,
4323 .jim_handler = &jim_target_invoke_event,
4325 COMMAND_REGISTRATION_DONE
4328 static int target_create(Jim_GetOptInfo *goi)
4330 Jim_Obj *new_cmd;
4331 Jim_Cmd *cmd;
4332 const char *cp;
4333 char *cp2;
4334 int e;
4335 int x;
4336 struct target *target;
4337 struct command_context *cmd_ctx;
4339 cmd_ctx = Jim_GetAssocData(goi->interp, "context");
4340 if (goi->argc < 3) {
4341 Jim_WrongNumArgs(goi->interp, 1, goi->argv, "?name? ?type? ..options...");
4342 return JIM_ERR;
4345 /* COMMAND */
4346 Jim_GetOpt_Obj(goi, &new_cmd);
4347 /* does this command exist? */
4348 cmd = Jim_GetCommand(goi->interp, new_cmd, JIM_ERRMSG);
4349 if (cmd) {
4350 cp = Jim_GetString(new_cmd, NULL);
4351 Jim_SetResult_sprintf(goi->interp, "Command/target: %s Exists", cp);
4352 return JIM_ERR;
4355 /* TYPE */
4356 e = Jim_GetOpt_String(goi, &cp2, NULL);
4357 cp = cp2;
4358 /* now does target type exist */
4359 for (x = 0 ; target_types[x] ; x++) {
4360 if (0 == strcmp(cp, target_types[x]->name)) {
4361 /* found */
4362 break;
4365 if (target_types[x] == NULL) {
4366 Jim_SetResult_sprintf(goi->interp, "Unknown target type %s, try one of ", cp);
4367 for (x = 0 ; target_types[x] ; x++) {
4368 if (target_types[x + 1]) {
4369 Jim_AppendStrings(goi->interp,
4370 Jim_GetResult(goi->interp),
4371 target_types[x]->name,
4372 ", ", NULL);
4373 } else {
4374 Jim_AppendStrings(goi->interp,
4375 Jim_GetResult(goi->interp),
4376 " or ",
4377 target_types[x]->name,NULL);
4380 return JIM_ERR;
4383 /* Create it */
4384 target = calloc(1,sizeof(struct target));
4385 /* set target number */
4386 target->target_number = new_target_number();
4388 /* allocate memory for each unique target type */
4389 target->type = (struct target_type*)calloc(1,sizeof(struct target_type));
4391 memcpy(target->type, target_types[x], sizeof(struct target_type));
4393 /* will be set by "-endian" */
4394 target->endianness = TARGET_ENDIAN_UNKNOWN;
4396 target->working_area = 0x0;
4397 target->working_area_size = 0x0;
4398 target->working_areas = NULL;
4399 target->backup_working_area = 0;
4401 target->state = TARGET_UNKNOWN;
4402 target->debug_reason = DBG_REASON_UNDEFINED;
4403 target->reg_cache = NULL;
4404 target->breakpoints = NULL;
4405 target->watchpoints = NULL;
4406 target->next = NULL;
4407 target->arch_info = NULL;
4409 target->display = 1;
4411 target->halt_issued = false;
4413 /* initialize trace information */
4414 target->trace_info = malloc(sizeof(struct trace));
4415 target->trace_info->num_trace_points = 0;
4416 target->trace_info->trace_points_size = 0;
4417 target->trace_info->trace_points = NULL;
4418 target->trace_info->trace_history_size = 0;
4419 target->trace_info->trace_history = NULL;
4420 target->trace_info->trace_history_pos = 0;
4421 target->trace_info->trace_history_overflowed = 0;
4423 target->dbgmsg = NULL;
4424 target->dbg_msg_enabled = 0;
4426 target->endianness = TARGET_ENDIAN_UNKNOWN;
4428 /* Do the rest as "configure" options */
4429 goi->isconfigure = 1;
4430 e = target_configure(goi, target);
4432 if (target->tap == NULL)
4434 Jim_SetResultString(goi->interp, "-chain-position required when creating target", -1);
4435 e = JIM_ERR;
4438 if (e != JIM_OK) {
4439 free(target->type);
4440 free(target);
4441 return e;
4444 if (target->endianness == TARGET_ENDIAN_UNKNOWN) {
4445 /* default endian to little if not specified */
4446 target->endianness = TARGET_LITTLE_ENDIAN;
4449 /* incase variant is not set */
4450 if (!target->variant)
4451 target->variant = strdup("");
4453 cp = Jim_GetString(new_cmd, NULL);
4454 target->cmd_name = strdup(cp);
4456 /* create the target specific commands */
4457 if (target->type->commands) {
4458 e = register_commands(cmd_ctx, NULL, target->type->commands);
4459 if (ERROR_OK != e)
4460 LOG_ERROR("unable to register '%s' commands", cp);
4462 if (target->type->target_create) {
4463 (*(target->type->target_create))(target, goi->interp);
4466 /* append to end of list */
4468 struct target **tpp;
4469 tpp = &(all_targets);
4470 while (*tpp) {
4471 tpp = &((*tpp)->next);
4473 *tpp = target;
4476 /* now - create the new target name command */
4477 const const struct command_registration target_subcommands[] = {
4479 .chain = target_instance_command_handlers,
4482 .chain = target->type->commands,
4484 COMMAND_REGISTRATION_DONE
4486 const const struct command_registration target_commands[] = {
4488 .name = cp,
4489 .mode = COMMAND_ANY,
4490 .help = "target command group",
4491 .chain = target_subcommands,
4493 COMMAND_REGISTRATION_DONE
4495 e = register_commands(cmd_ctx, NULL, target_commands);
4496 if (ERROR_OK != e)
4497 return JIM_ERR;
4499 struct command *c = command_find_in_context(cmd_ctx, cp);
4500 assert(c);
4501 command_set_handler_data(c, target);
4503 return (ERROR_OK == e) ? JIM_OK : JIM_ERR;
4506 static int jim_target_current(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
4508 if (argc != 1)
4510 Jim_WrongNumArgs(interp, 1, argv, "Too many parameters");
4511 return JIM_ERR;
4513 struct command_context *cmd_ctx = Jim_GetAssocData(interp, "context");
4514 Jim_SetResultString(interp, get_current_target(cmd_ctx)->cmd_name, -1);
4515 return JIM_OK;
4518 static int jim_target_types(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
4520 if (argc != 1)
4522 Jim_WrongNumArgs(interp, 1, argv, "Too many parameters");
4523 return JIM_ERR;
4525 Jim_SetResult(interp, Jim_NewListObj(interp, NULL, 0));
4526 for (unsigned x = 0; NULL != target_types[x]; x++)
4528 Jim_ListAppendElement(interp, Jim_GetResult(interp),
4529 Jim_NewStringObj(interp, target_types[x]->name, -1));
4531 return JIM_OK;
4534 static int jim_target_names(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
4536 if (argc != 1)
4538 Jim_WrongNumArgs(interp, 1, argv, "Too many parameters");
4539 return JIM_ERR;
4541 Jim_SetResult(interp, Jim_NewListObj(interp, NULL, 0));
4542 struct target *target = all_targets;
4543 while (target)
4545 Jim_ListAppendElement(interp, Jim_GetResult(interp),
4546 Jim_NewStringObj(interp, target_name(target), -1));
4547 target = target->next;
4549 return JIM_OK;
4552 static int jim_target_create(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
4554 Jim_GetOptInfo goi;
4555 Jim_GetOpt_Setup(&goi, interp, argc - 1, argv + 1);
4556 if (goi.argc < 3)
4558 Jim_WrongNumArgs(goi.interp, goi.argc, goi.argv,
4559 "<name> <target_type> [<target_options> ...]");
4560 return JIM_ERR;
4562 return target_create(&goi);
4565 static int jim_target_number(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
4567 Jim_GetOptInfo goi;
4568 Jim_GetOpt_Setup(&goi, interp, argc - 1, argv + 1);
4570 /* It's OK to remove this mechanism sometime after August 2010 or so */
4571 LOG_WARNING("don't use numbers as target identifiers; use names");
4572 if (goi.argc != 1)
4574 Jim_SetResult_sprintf(goi.interp, "usage: target number <number>");
4575 return JIM_ERR;
4577 jim_wide w;
4578 int e = Jim_GetOpt_Wide(&goi, &w);
4579 if (e != JIM_OK)
4580 return JIM_ERR;
4582 struct target *target;
4583 for (target = all_targets; NULL != target; target = target->next)
4585 if (target->target_number != w)
4586 continue;
4588 Jim_SetResultString(goi.interp, target_name(target), -1);
4589 return JIM_OK;
4591 Jim_SetResult_sprintf(goi.interp,
4592 "Target: number %d does not exist", (int)(w));
4593 return JIM_ERR;
4596 static int jim_target_count(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
4598 if (argc != 1)
4600 Jim_WrongNumArgs(interp, 1, argv, "<no parameters>");
4601 return JIM_ERR;
4603 unsigned count = 0;
4604 struct target *target = all_targets;
4605 while (NULL != target)
4607 target = target->next;
4608 count++;
4610 Jim_SetResult(interp, Jim_NewIntObj(interp, count));
4611 return JIM_OK;
4614 static const struct command_registration target_subcommand_handlers[] = {
4616 .name = "init",
4617 .mode = COMMAND_CONFIG,
4618 .handler = &handle_target_init_command,
4619 .help = "initialize targets",
4622 .name = "create",
4623 .mode = COMMAND_ANY,
4624 .jim_handler = &jim_target_create,
4625 .usage = "<name> <type> ...",
4626 .help = "Returns the currently selected target",
4629 .name = "current",
4630 .mode = COMMAND_ANY,
4631 .jim_handler = &jim_target_current,
4632 .help = "Returns the currently selected target",
4635 .name = "types",
4636 .mode = COMMAND_ANY,
4637 .jim_handler = &jim_target_types,
4638 .help = "Returns the available target types as a list of strings",
4641 .name = "names",
4642 .mode = COMMAND_ANY,
4643 .jim_handler = &jim_target_names,
4644 .help = "Returns the names of all targets as a list of strings",
4647 .name = "number",
4648 .mode = COMMAND_ANY,
4649 .jim_handler = &jim_target_number,
4650 .usage = "<number>",
4651 .help = "Returns the name of target <n>",
4654 .name = "count",
4655 .mode = COMMAND_ANY,
4656 .jim_handler = &jim_target_count,
4657 .help = "Returns the number of targets as an integer",
4659 COMMAND_REGISTRATION_DONE
4663 struct FastLoad
4665 uint32_t address;
4666 uint8_t *data;
4667 int length;
4671 static int fastload_num;
4672 static struct FastLoad *fastload;
4674 static void free_fastload(void)
4676 if (fastload != NULL)
4678 int i;
4679 for (i = 0; i < fastload_num; i++)
4681 if (fastload[i].data)
4682 free(fastload[i].data);
4684 free(fastload);
4685 fastload = NULL;
4692 COMMAND_HANDLER(handle_fast_load_image_command)
4694 uint8_t *buffer;
4695 size_t buf_cnt;
4696 uint32_t image_size;
4697 uint32_t min_address = 0;
4698 uint32_t max_address = 0xffffffff;
4699 int i;
4701 struct image image;
4703 int retval = CALL_COMMAND_HANDLER(parse_load_image_command_CMD_ARGV,
4704 &image, &min_address, &max_address);
4705 if (ERROR_OK != retval)
4706 return retval;
4708 struct duration bench;
4709 duration_start(&bench);
4711 if (image_open(&image, CMD_ARGV[0], (CMD_ARGC >= 3) ? CMD_ARGV[2] : NULL) != ERROR_OK)
4713 return ERROR_OK;
4716 image_size = 0x0;
4717 retval = ERROR_OK;
4718 fastload_num = image.num_sections;
4719 fastload = (struct FastLoad *)malloc(sizeof(struct FastLoad)*image.num_sections);
4720 if (fastload == NULL)
4722 image_close(&image);
4723 return ERROR_FAIL;
4725 memset(fastload, 0, sizeof(struct FastLoad)*image.num_sections);
4726 for (i = 0; i < image.num_sections; i++)
4728 buffer = malloc(image.sections[i].size);
4729 if (buffer == NULL)
4731 command_print(CMD_CTX, "error allocating buffer for section (%d bytes)",
4732 (int)(image.sections[i].size));
4733 break;
4736 if ((retval = image_read_section(&image, i, 0x0, image.sections[i].size, buffer, &buf_cnt)) != ERROR_OK)
4738 free(buffer);
4739 break;
4742 uint32_t offset = 0;
4743 uint32_t length = buf_cnt;
4746 /* DANGER!!! beware of unsigned comparision here!!! */
4748 if ((image.sections[i].base_address + buf_cnt >= min_address)&&
4749 (image.sections[i].base_address < max_address))
4751 if (image.sections[i].base_address < min_address)
4753 /* clip addresses below */
4754 offset += min_address-image.sections[i].base_address;
4755 length -= offset;
4758 if (image.sections[i].base_address + buf_cnt > max_address)
4760 length -= (image.sections[i].base_address + buf_cnt)-max_address;
4763 fastload[i].address = image.sections[i].base_address + offset;
4764 fastload[i].data = malloc(length);
4765 if (fastload[i].data == NULL)
4767 free(buffer);
4768 break;
4770 memcpy(fastload[i].data, buffer + offset, length);
4771 fastload[i].length = length;
4773 image_size += length;
4774 command_print(CMD_CTX, "%u bytes written at address 0x%8.8x",
4775 (unsigned int)length,
4776 ((unsigned int)(image.sections[i].base_address + offset)));
4779 free(buffer);
4782 if ((ERROR_OK == retval) && (duration_measure(&bench) == ERROR_OK))
4784 command_print(CMD_CTX, "Loaded %" PRIu32 " bytes "
4785 "in %fs (%0.3f kb/s)", image_size,
4786 duration_elapsed(&bench), duration_kbps(&bench, image_size));
4788 command_print(CMD_CTX,
4789 "WARNING: image has not been loaded to target!"
4790 "You can issue a 'fast_load' to finish loading.");
4793 image_close(&image);
4795 if (retval != ERROR_OK)
4797 free_fastload();
4800 return retval;
4803 COMMAND_HANDLER(handle_fast_load_command)
4805 if (CMD_ARGC > 0)
4806 return ERROR_COMMAND_SYNTAX_ERROR;
4807 if (fastload == NULL)
4809 LOG_ERROR("No image in memory");
4810 return ERROR_FAIL;
4812 int i;
4813 int ms = timeval_ms();
4814 int size = 0;
4815 int retval = ERROR_OK;
4816 for (i = 0; i < fastload_num;i++)
4818 struct target *target = get_current_target(CMD_CTX);
4819 command_print(CMD_CTX, "Write to 0x%08x, length 0x%08x",
4820 (unsigned int)(fastload[i].address),
4821 (unsigned int)(fastload[i].length));
4822 if (retval == ERROR_OK)
4824 retval = target_write_buffer(target, fastload[i].address, fastload[i].length, fastload[i].data);
4826 size += fastload[i].length;
4828 int after = timeval_ms();
4829 command_print(CMD_CTX, "Loaded image %f kBytes/s", (float)(size/1024.0)/((float)(after-ms)/1000.0));
4830 return retval;
4833 static const struct command_registration target_command_handlers[] = {
4835 .name = "targets",
4836 .handler = &handle_targets_command,
4837 .mode = COMMAND_ANY,
4838 .help = "change current command line target (one parameter) "
4839 "or list targets (no parameters)",
4840 .usage = "[<new_current_target>]",
4843 .name = "target",
4844 .mode = COMMAND_CONFIG,
4845 .help = "configure target",
4847 .chain = target_subcommand_handlers,
4849 COMMAND_REGISTRATION_DONE
4852 int target_register_commands(struct command_context *cmd_ctx)
4854 return register_commands(cmd_ctx, NULL, target_command_handlers);
4857 static const struct command_registration target_exec_command_handlers[] = {
4859 .name = "fast_load_image",
4860 .handler = &handle_fast_load_image_command,
4861 .mode = COMMAND_ANY,
4862 .help = "Load image into memory, mainly for profiling purposes",
4863 .usage = "<file> <address> ['bin'|'ihex'|'elf'|'s19'] "
4864 "[min_address] [max_length]",
4867 .name = "fast_load",
4868 .handler = &handle_fast_load_command,
4869 .mode = COMMAND_EXEC,
4870 .help = "loads active fast load image to current target "
4871 "- mainly for profiling purposes",
4874 .name = "profile",
4875 .handler = &handle_profile_command,
4876 .mode = COMMAND_EXEC,
4877 .help = "profiling samples the CPU PC",
4879 /** @todo don't register virt2phys() unless target supports it */
4881 .name = "virt2phys",
4882 .handler = &handle_virt2phys_command,
4883 .mode = COMMAND_ANY,
4884 .help = "translate a virtual address into a physical address",
4888 .name = "reg",
4889 .handler = &handle_reg_command,
4890 .mode = COMMAND_EXEC,
4891 .help = "display or set a register",
4895 .name = "poll",
4896 .handler = &handle_poll_command,
4897 .mode = COMMAND_EXEC,
4898 .help = "poll target state",
4901 .name = "wait_halt",
4902 .handler = &handle_wait_halt_command,
4903 .mode = COMMAND_EXEC,
4904 .help = "wait for target halt",
4905 .usage = "[time (s)]",
4908 .name = "halt",
4909 .handler = &handle_halt_command,
4910 .mode = COMMAND_EXEC,
4911 .help = "halt target",
4914 .name = "resume",
4915 .handler = &handle_resume_command,
4916 .mode = COMMAND_EXEC,
4917 .help = "resume target",
4918 .usage = "[<address>]",
4921 .name = "reset",
4922 .handler = &handle_reset_command,
4923 .mode = COMMAND_EXEC,
4924 .usage = "[run|halt|init]",
4925 .help = "Reset all targets into the specified mode."
4926 "Default reset mode is run, if not given.",
4929 .name = "soft_reset_halt",
4930 .handler = &handle_soft_reset_halt_command,
4931 .mode = COMMAND_EXEC,
4932 .help = "halt the target and do a soft reset",
4936 .name = "step",
4937 .handler = &handle_step_command,
4938 .mode = COMMAND_EXEC,
4939 .help = "step one instruction from current PC or [addr]",
4940 .usage = "[<address>]",
4944 .name = "mdw",
4945 .handler = &handle_md_command,
4946 .mode = COMMAND_EXEC,
4947 .help = "display memory words",
4948 .usage = "[phys] <addr> [count]",
4951 .name = "mdh",
4952 .handler = &handle_md_command,
4953 .mode = COMMAND_EXEC,
4954 .help = "display memory half-words",
4955 .usage = "[phys] <addr> [count]",
4958 .name = "mdb",
4959 .handler = &handle_md_command,
4960 .mode = COMMAND_EXEC,
4961 .help = "display memory bytes",
4962 .usage = "[phys] <addr> [count]",
4966 .name = "mww",
4967 .handler = &handle_mw_command,
4968 .mode = COMMAND_EXEC,
4969 .help = "write memory word",
4970 .usage = "[phys] <addr> <value> [count]",
4973 .name = "mwh",
4974 .handler = &handle_mw_command,
4975 .mode = COMMAND_EXEC,
4976 .help = "write memory half-word",
4977 .usage = "[phys] <addr> <value> [count]",
4980 .name = "mwb",
4981 .handler = &handle_mw_command,
4982 .mode = COMMAND_EXEC,
4983 .help = "write memory byte",
4984 .usage = "[phys] <addr> <value> [count]",
4988 .name = "bp",
4989 .handler = &handle_bp_command,
4990 .mode = COMMAND_EXEC,
4991 .help = "list or set breakpoint",
4992 .usage = "[<address> <length> [hw]]",
4995 .name = "rbp",
4996 .handler = &handle_rbp_command,
4997 .mode = COMMAND_EXEC,
4998 .help = "remove breakpoint",
4999 .usage = "<address>",
5003 .name = "wp",
5004 .handler = &handle_wp_command,
5005 .mode = COMMAND_EXEC,
5006 .help = "list or set watchpoint",
5007 .usage = "[<address> <length> <r/w/a> [value] [mask]]",
5010 .name = "rwp",
5011 .handler = &handle_rwp_command,
5012 .mode = COMMAND_EXEC,
5013 .help = "remove watchpoint",
5014 .usage = "<address>",
5018 .name = "load_image",
5019 .handler = &handle_load_image_command,
5020 .mode = COMMAND_EXEC,
5021 .usage = "<file> <address> ['bin'|'ihex'|'elf'|'s19'] "
5022 "[min_address] [max_length]",
5025 .name = "dump_image",
5026 .handler = &handle_dump_image_command,
5027 .mode = COMMAND_EXEC,
5028 .usage = "<file> <address> <size>",
5031 .name = "verify_image",
5032 .handler = &handle_verify_image_command,
5033 .mode = COMMAND_EXEC,
5034 .usage = "<file> [offset] [type]",
5037 .name = "test_image",
5038 .handler = &handle_test_image_command,
5039 .mode = COMMAND_EXEC,
5040 .usage = "<file> [offset] [type]",
5043 .name = "ocd_mem2array",
5044 .mode = COMMAND_EXEC,
5045 .jim_handler = &jim_mem2array,
5046 .help = "read memory and return as a TCL array "
5047 "for script processing",
5048 .usage = "<arrayname> <width=32|16|8> <address> <count>",
5051 .name = "ocd_array2mem",
5052 .mode = COMMAND_EXEC,
5053 .jim_handler = &jim_array2mem,
5054 .help = "convert a TCL array to memory locations "
5055 "and write the values",
5056 .usage = "<arrayname> <width=32|16|8> <address> <count>",
5058 COMMAND_REGISTRATION_DONE
5060 int target_register_user_commands(struct command_context *cmd_ctx)
5062 int retval = ERROR_OK;
5063 if ((retval = target_request_register_commands(cmd_ctx)) != ERROR_OK)
5064 return retval;
5066 if ((retval = trace_register_commands(cmd_ctx)) != ERROR_OK)
5067 return retval;
5070 return register_commands(cmd_ctx, NULL, target_exec_command_handlers);