target: add check_reset hook
[openocd.git] / src / target / target.c
blobc56265c1d1e8c200adcc43366c46dd1e5b32b4b6
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,
49 int argc, Jim_Obj *const *argv);
50 static int target_mem2array(Jim_Interp *interp, struct target *target,
51 int argc, Jim_Obj *const *argv);
53 /* targets */
54 extern struct target_type arm7tdmi_target;
55 extern struct target_type arm720t_target;
56 extern struct target_type arm9tdmi_target;
57 extern struct target_type arm920t_target;
58 extern struct target_type arm966e_target;
59 extern struct target_type arm926ejs_target;
60 extern struct target_type fa526_target;
61 extern struct target_type feroceon_target;
62 extern struct target_type dragonite_target;
63 extern struct target_type xscale_target;
64 extern struct target_type cortexm3_target;
65 extern struct target_type cortexa8_target;
66 extern struct target_type arm11_target;
67 extern struct target_type mips_m4k_target;
68 extern struct target_type avr_target;
69 extern struct target_type dsp563xx_target;
70 extern struct target_type testee_target;
72 struct target_type *target_types[] =
74 &arm7tdmi_target,
75 &arm9tdmi_target,
76 &arm920t_target,
77 &arm720t_target,
78 &arm966e_target,
79 &arm926ejs_target,
80 &fa526_target,
81 &feroceon_target,
82 &dragonite_target,
83 &xscale_target,
84 &cortexm3_target,
85 &cortexa8_target,
86 &arm11_target,
87 &mips_m4k_target,
88 &avr_target,
89 &dsp563xx_target,
90 &testee_target,
91 NULL,
94 struct target *all_targets = NULL;
95 struct target_event_callback *target_event_callbacks = NULL;
96 struct target_timer_callback *target_timer_callbacks = NULL;
98 static const Jim_Nvp nvp_assert[] = {
99 { .name = "assert", NVP_ASSERT },
100 { .name = "deassert", NVP_DEASSERT },
101 { .name = "T", NVP_ASSERT },
102 { .name = "F", NVP_DEASSERT },
103 { .name = "t", NVP_ASSERT },
104 { .name = "f", NVP_DEASSERT },
105 { .name = NULL, .value = -1 }
108 static const Jim_Nvp nvp_error_target[] = {
109 { .value = ERROR_TARGET_INVALID, .name = "err-invalid" },
110 { .value = ERROR_TARGET_INIT_FAILED, .name = "err-init-failed" },
111 { .value = ERROR_TARGET_TIMEOUT, .name = "err-timeout" },
112 { .value = ERROR_TARGET_NOT_HALTED, .name = "err-not-halted" },
113 { .value = ERROR_TARGET_FAILURE, .name = "err-failure" },
114 { .value = ERROR_TARGET_UNALIGNED_ACCESS , .name = "err-unaligned-access" },
115 { .value = ERROR_TARGET_DATA_ABORT , .name = "err-data-abort" },
116 { .value = ERROR_TARGET_RESOURCE_NOT_AVAILABLE , .name = "err-resource-not-available" },
117 { .value = ERROR_TARGET_TRANSLATION_FAULT , .name = "err-translation-fault" },
118 { .value = ERROR_TARGET_NOT_RUNNING, .name = "err-not-running" },
119 { .value = ERROR_TARGET_NOT_EXAMINED, .name = "err-not-examined" },
120 { .value = -1, .name = NULL }
123 const char *target_strerror_safe(int err)
125 const Jim_Nvp *n;
127 n = Jim_Nvp_value2name_simple(nvp_error_target, err);
128 if (n->name == NULL) {
129 return "unknown";
130 } else {
131 return n->name;
135 static const Jim_Nvp nvp_target_event[] = {
136 { .value = TARGET_EVENT_OLD_gdb_program_config , .name = "old-gdb_program_config" },
137 { .value = TARGET_EVENT_OLD_pre_resume , .name = "old-pre_resume" },
139 { .value = TARGET_EVENT_GDB_HALT, .name = "gdb-halt" },
140 { .value = TARGET_EVENT_HALTED, .name = "halted" },
141 { .value = TARGET_EVENT_RESUMED, .name = "resumed" },
142 { .value = TARGET_EVENT_RESUME_START, .name = "resume-start" },
143 { .value = TARGET_EVENT_RESUME_END, .name = "resume-end" },
145 { .name = "gdb-start", .value = TARGET_EVENT_GDB_START },
146 { .name = "gdb-end", .value = TARGET_EVENT_GDB_END },
148 /* historical name */
150 { .value = TARGET_EVENT_RESET_START, .name = "reset-start" },
152 { .value = TARGET_EVENT_RESET_ASSERT_PRE, .name = "reset-assert-pre" },
153 { .value = TARGET_EVENT_RESET_ASSERT, .name = "reset-assert" },
154 { .value = TARGET_EVENT_RESET_ASSERT_POST, .name = "reset-assert-post" },
155 { .value = TARGET_EVENT_RESET_DEASSERT_PRE, .name = "reset-deassert-pre" },
156 { .value = TARGET_EVENT_RESET_DEASSERT_POST, .name = "reset-deassert-post" },
157 { .value = TARGET_EVENT_RESET_HALT_PRE, .name = "reset-halt-pre" },
158 { .value = TARGET_EVENT_RESET_HALT_POST, .name = "reset-halt-post" },
159 { .value = TARGET_EVENT_RESET_WAIT_PRE, .name = "reset-wait-pre" },
160 { .value = TARGET_EVENT_RESET_WAIT_POST, .name = "reset-wait-post" },
161 { .value = TARGET_EVENT_RESET_INIT, .name = "reset-init" },
162 { .value = TARGET_EVENT_RESET_END, .name = "reset-end" },
164 { .value = TARGET_EVENT_EXAMINE_START, .name = "examine-start" },
165 { .value = TARGET_EVENT_EXAMINE_END, .name = "examine-end" },
167 { .value = TARGET_EVENT_DEBUG_HALTED, .name = "debug-halted" },
168 { .value = TARGET_EVENT_DEBUG_RESUMED, .name = "debug-resumed" },
170 { .value = TARGET_EVENT_GDB_ATTACH, .name = "gdb-attach" },
171 { .value = TARGET_EVENT_GDB_DETACH, .name = "gdb-detach" },
173 { .value = TARGET_EVENT_GDB_FLASH_WRITE_START, .name = "gdb-flash-write-start" },
174 { .value = TARGET_EVENT_GDB_FLASH_WRITE_END , .name = "gdb-flash-write-end" },
176 { .value = TARGET_EVENT_GDB_FLASH_ERASE_START, .name = "gdb-flash-erase-start" },
177 { .value = TARGET_EVENT_GDB_FLASH_ERASE_END , .name = "gdb-flash-erase-end" },
179 { .value = TARGET_EVENT_RESUME_START, .name = "resume-start" },
180 { .value = TARGET_EVENT_RESUMED , .name = "resume-ok" },
181 { .value = TARGET_EVENT_RESUME_END , .name = "resume-end" },
183 { .name = NULL, .value = -1 }
186 static const Jim_Nvp nvp_target_state[] = {
187 { .name = "unknown", .value = TARGET_UNKNOWN },
188 { .name = "running", .value = TARGET_RUNNING },
189 { .name = "halted", .value = TARGET_HALTED },
190 { .name = "reset", .value = TARGET_RESET },
191 { .name = "debug-running", .value = TARGET_DEBUG_RUNNING },
192 { .name = NULL, .value = -1 },
195 static const Jim_Nvp nvp_target_debug_reason [] = {
196 { .name = "debug-request" , .value = DBG_REASON_DBGRQ },
197 { .name = "breakpoint" , .value = DBG_REASON_BREAKPOINT },
198 { .name = "watchpoint" , .value = DBG_REASON_WATCHPOINT },
199 { .name = "watchpoint-and-breakpoint", .value = DBG_REASON_WPTANDBKPT },
200 { .name = "single-step" , .value = DBG_REASON_SINGLESTEP },
201 { .name = "target-not-halted" , .value = DBG_REASON_NOTHALTED },
202 { .name = "undefined" , .value = DBG_REASON_UNDEFINED },
203 { .name = NULL, .value = -1 },
206 static const Jim_Nvp nvp_target_endian[] = {
207 { .name = "big", .value = TARGET_BIG_ENDIAN },
208 { .name = "little", .value = TARGET_LITTLE_ENDIAN },
209 { .name = "be", .value = TARGET_BIG_ENDIAN },
210 { .name = "le", .value = TARGET_LITTLE_ENDIAN },
211 { .name = NULL, .value = -1 },
214 static const Jim_Nvp nvp_reset_modes[] = {
215 { .name = "unknown", .value = RESET_UNKNOWN },
216 { .name = "run" , .value = RESET_RUN },
217 { .name = "halt" , .value = RESET_HALT },
218 { .name = "init" , .value = RESET_INIT },
219 { .name = NULL , .value = -1 },
222 const char *debug_reason_name(struct target *t)
224 const char *cp;
226 cp = Jim_Nvp_value2name_simple(nvp_target_debug_reason,
227 t->debug_reason)->name;
228 if (!cp) {
229 LOG_ERROR("Invalid debug reason: %d", (int)(t->debug_reason));
230 cp = "(*BUG*unknown*BUG*)";
232 return cp;
235 const char *
236 target_state_name( struct target *t )
238 const char *cp;
239 cp = Jim_Nvp_value2name_simple(nvp_target_state, t->state)->name;
240 if( !cp ){
241 LOG_ERROR("Invalid target state: %d", (int)(t->state));
242 cp = "(*BUG*unknown*BUG*)";
244 return cp;
247 /* determine the number of the new target */
248 static int new_target_number(void)
250 struct target *t;
251 int x;
253 /* number is 0 based */
254 x = -1;
255 t = all_targets;
256 while (t) {
257 if (x < t->target_number) {
258 x = t->target_number;
260 t = t->next;
262 return x + 1;
265 /* read a uint32_t from a buffer in target memory endianness */
266 uint32_t target_buffer_get_u32(struct target *target, const uint8_t *buffer)
268 if (target->endianness == TARGET_LITTLE_ENDIAN)
269 return le_to_h_u32(buffer);
270 else
271 return be_to_h_u32(buffer);
274 /* read a uint16_t from a buffer in target memory endianness */
275 uint16_t target_buffer_get_u16(struct target *target, const uint8_t *buffer)
277 if (target->endianness == TARGET_LITTLE_ENDIAN)
278 return le_to_h_u16(buffer);
279 else
280 return be_to_h_u16(buffer);
283 /* read a uint8_t from a buffer in target memory endianness */
284 uint8_t target_buffer_get_u8(struct target *target, const uint8_t *buffer)
286 return *buffer & 0x0ff;
289 /* write a uint32_t to a buffer in target memory endianness */
290 void target_buffer_set_u32(struct target *target, uint8_t *buffer, uint32_t value)
292 if (target->endianness == TARGET_LITTLE_ENDIAN)
293 h_u32_to_le(buffer, value);
294 else
295 h_u32_to_be(buffer, value);
298 /* write a uint16_t to a buffer in target memory endianness */
299 void target_buffer_set_u16(struct target *target, uint8_t *buffer, uint16_t value)
301 if (target->endianness == TARGET_LITTLE_ENDIAN)
302 h_u16_to_le(buffer, value);
303 else
304 h_u16_to_be(buffer, value);
307 /* write a uint8_t to a buffer in target memory endianness */
308 void target_buffer_set_u8(struct target *target, uint8_t *buffer, uint8_t value)
310 *buffer = value;
313 /* return a pointer to a configured target; id is name or number */
314 struct target *get_target(const char *id)
316 struct target *target;
318 /* try as tcltarget name */
319 for (target = all_targets; target; target = target->next) {
320 if (target->cmd_name == NULL)
321 continue;
322 if (strcmp(id, target->cmd_name) == 0)
323 return target;
326 /* It's OK to remove this fallback sometime after August 2010 or so */
328 /* no match, try as number */
329 unsigned num;
330 if (parse_uint(id, &num) != ERROR_OK)
331 return NULL;
333 for (target = all_targets; target; target = target->next) {
334 if (target->target_number == (int)num) {
335 LOG_WARNING("use '%s' as target identifier, not '%u'",
336 target->cmd_name, num);
337 return target;
341 return NULL;
344 /* returns a pointer to the n-th configured target */
345 static struct target *get_target_by_num(int num)
347 struct target *target = all_targets;
349 while (target) {
350 if (target->target_number == num) {
351 return target;
353 target = target->next;
356 return NULL;
359 struct target* get_current_target(struct command_context *cmd_ctx)
361 struct target *target = get_target_by_num(cmd_ctx->current_target);
363 if (target == NULL)
365 LOG_ERROR("BUG: current_target out of bounds");
366 exit(-1);
369 return target;
372 int target_poll(struct target *target)
374 int retval;
376 /* We can't poll until after examine */
377 if (!target_was_examined(target))
379 /* Fail silently lest we pollute the log */
380 return ERROR_FAIL;
383 retval = target->type->poll(target);
384 if (retval != ERROR_OK)
385 return retval;
387 if (target->halt_issued)
389 if (target->state == TARGET_HALTED)
391 target->halt_issued = false;
392 } else
394 long long t = timeval_ms() - target->halt_issued_time;
395 if (t>1000)
397 target->halt_issued = false;
398 LOG_INFO("Halt timed out, wake up GDB.");
399 target_call_event_callbacks(target, TARGET_EVENT_GDB_HALT);
404 return ERROR_OK;
407 int target_halt(struct target *target)
409 int retval;
410 /* We can't poll until after examine */
411 if (!target_was_examined(target))
413 LOG_ERROR("Target not examined yet");
414 return ERROR_FAIL;
417 retval = target->type->halt(target);
418 if (retval != ERROR_OK)
419 return retval;
421 target->halt_issued = true;
422 target->halt_issued_time = timeval_ms();
424 return ERROR_OK;
427 int target_resume(struct target *target, int current, uint32_t address, int handle_breakpoints, int debug_execution)
429 int retval;
431 /* We can't poll until after examine */
432 if (!target_was_examined(target))
434 LOG_ERROR("Target not examined yet");
435 return ERROR_FAIL;
438 /* note that resume *must* be asynchronous. The CPU can halt before we poll. The CPU can
439 * even halt at the current PC as a result of a software breakpoint being inserted by (a bug?)
440 * the application.
442 if ((retval = target->type->resume(target, current, address, handle_breakpoints, debug_execution)) != ERROR_OK)
443 return retval;
445 return retval;
448 int target_process_reset(struct command_context *cmd_ctx, enum target_reset_mode reset_mode)
450 char buf[100];
451 int retval;
452 Jim_Nvp *n;
453 n = Jim_Nvp_value2name_simple(nvp_reset_modes, reset_mode);
454 if (n->name == NULL) {
455 LOG_ERROR("invalid reset mode");
456 return ERROR_FAIL;
459 /* disable polling during reset to make reset event scripts
460 * more predictable, i.e. dr/irscan & pathmove in events will
461 * not have JTAG operations injected into the middle of a sequence.
463 bool save_poll = jtag_poll_get_enabled();
465 jtag_poll_set_enabled(false);
467 sprintf(buf, "ocd_process_reset %s", n->name);
468 retval = Jim_Eval(cmd_ctx->interp, buf);
470 jtag_poll_set_enabled(save_poll);
472 if (retval != JIM_OK) {
473 Jim_PrintErrorMessage(cmd_ctx->interp);
474 return ERROR_FAIL;
477 /* We want any events to be processed before the prompt */
478 retval = target_call_timer_callbacks_now();
480 struct target *target;
481 for (target = all_targets; target; target = target->next) {
482 target->type->check_reset(target);
485 return retval;
488 static int identity_virt2phys(struct target *target,
489 uint32_t virtual, uint32_t *physical)
491 *physical = virtual;
492 return ERROR_OK;
495 static int no_mmu(struct target *target, int *enabled)
497 *enabled = 0;
498 return ERROR_OK;
501 static int default_examine(struct target *target)
503 target_set_examined(target);
504 return ERROR_OK;
507 /* no check by default */
508 static int default_check_reset(struct target *target)
510 return ERROR_OK;
513 int target_examine_one(struct target *target)
515 return target->type->examine(target);
518 static int jtag_enable_callback(enum jtag_event event, void *priv)
520 struct target *target = priv;
522 if (event != JTAG_TAP_EVENT_ENABLE || !target->tap->enabled)
523 return ERROR_OK;
525 jtag_unregister_event_callback(jtag_enable_callback, target);
526 return target_examine_one(target);
530 /* Targets that correctly implement init + examine, i.e.
531 * no communication with target during init:
533 * XScale
535 int target_examine(void)
537 int retval = ERROR_OK;
538 struct target *target;
540 for (target = all_targets; target; target = target->next)
542 /* defer examination, but don't skip it */
543 if (!target->tap->enabled) {
544 jtag_register_event_callback(jtag_enable_callback,
545 target);
546 continue;
548 if ((retval = target_examine_one(target)) != ERROR_OK)
549 return retval;
551 return retval;
553 const char *target_type_name(struct target *target)
555 return target->type->name;
558 static int target_write_memory_imp(struct target *target, uint32_t address, uint32_t size, uint32_t count, uint8_t *buffer)
560 if (!target_was_examined(target))
562 LOG_ERROR("Target not examined yet");
563 return ERROR_FAIL;
565 return target->type->write_memory_imp(target, address, size, count, buffer);
568 static int target_read_memory_imp(struct target *target, uint32_t address, uint32_t size, uint32_t count, uint8_t *buffer)
570 if (!target_was_examined(target))
572 LOG_ERROR("Target not examined yet");
573 return ERROR_FAIL;
575 return target->type->read_memory_imp(target, address, size, count, buffer);
578 static int target_soft_reset_halt_imp(struct target *target)
580 if (!target_was_examined(target))
582 LOG_ERROR("Target not examined yet");
583 return ERROR_FAIL;
585 if (!target->type->soft_reset_halt_imp) {
586 LOG_ERROR("Target %s does not support soft_reset_halt",
587 target_name(target));
588 return ERROR_FAIL;
590 return target->type->soft_reset_halt_imp(target);
593 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)
595 if (!target_was_examined(target))
597 LOG_ERROR("Target not examined yet");
598 return ERROR_FAIL;
600 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);
603 int target_read_memory(struct target *target,
604 uint32_t address, uint32_t size, uint32_t count, uint8_t *buffer)
606 return target->type->read_memory(target, address, size, count, buffer);
609 int target_read_phys_memory(struct target *target,
610 uint32_t address, uint32_t size, uint32_t count, uint8_t *buffer)
612 return target->type->read_phys_memory(target, address, size, count, buffer);
615 int target_write_memory(struct target *target,
616 uint32_t address, uint32_t size, uint32_t count, uint8_t *buffer)
618 return target->type->write_memory(target, address, size, count, buffer);
621 int target_write_phys_memory(struct target *target,
622 uint32_t address, uint32_t size, uint32_t count, uint8_t *buffer)
624 return target->type->write_phys_memory(target, address, size, count, buffer);
627 int target_bulk_write_memory(struct target *target,
628 uint32_t address, uint32_t count, uint8_t *buffer)
630 return target->type->bulk_write_memory(target, address, count, buffer);
633 int target_add_breakpoint(struct target *target,
634 struct breakpoint *breakpoint)
636 if (target->state != TARGET_HALTED) {
637 LOG_WARNING("target %s is not halted", target->cmd_name);
638 return ERROR_TARGET_NOT_HALTED;
640 return target->type->add_breakpoint(target, breakpoint);
642 int target_remove_breakpoint(struct target *target,
643 struct breakpoint *breakpoint)
645 return target->type->remove_breakpoint(target, breakpoint);
648 int target_add_watchpoint(struct target *target,
649 struct watchpoint *watchpoint)
651 if (target->state != TARGET_HALTED) {
652 LOG_WARNING("target %s is not halted", target->cmd_name);
653 return ERROR_TARGET_NOT_HALTED;
655 return target->type->add_watchpoint(target, watchpoint);
657 int target_remove_watchpoint(struct target *target,
658 struct watchpoint *watchpoint)
660 return target->type->remove_watchpoint(target, watchpoint);
663 int target_get_gdb_reg_list(struct target *target,
664 struct reg **reg_list[], int *reg_list_size)
666 return target->type->get_gdb_reg_list(target, reg_list, reg_list_size);
668 int target_step(struct target *target,
669 int current, uint32_t address, int handle_breakpoints)
671 return target->type->step(target, current, address, handle_breakpoints);
675 int target_run_algorithm(struct target *target,
676 int num_mem_params, struct mem_param *mem_params,
677 int num_reg_params, struct reg_param *reg_param,
678 uint32_t entry_point, uint32_t exit_point,
679 int timeout_ms, void *arch_info)
681 return target->type->run_algorithm(target,
682 num_mem_params, mem_params, num_reg_params, reg_param,
683 entry_point, exit_point, timeout_ms, arch_info);
687 * Reset the @c examined flag for the given target.
688 * Pure paranoia -- targets are zeroed on allocation.
690 static void target_reset_examined(struct target *target)
692 target->examined = false;
695 static int
696 err_read_phys_memory(struct target *target, uint32_t address,
697 uint32_t size, uint32_t count, uint8_t *buffer)
699 LOG_ERROR("Not implemented: %s", __func__);
700 return ERROR_FAIL;
703 static int
704 err_write_phys_memory(struct target *target, uint32_t address,
705 uint32_t size, uint32_t count, uint8_t *buffer)
707 LOG_ERROR("Not implemented: %s", __func__);
708 return ERROR_FAIL;
711 static int handle_target(void *priv);
713 static int target_init_one(struct command_context *cmd_ctx,
714 struct target *target)
716 target_reset_examined(target);
718 struct target_type *type = target->type;
719 if (type->examine == NULL)
720 type->examine = default_examine;
722 if (type->check_reset== NULL)
723 type->check_reset = default_check_reset;
725 int retval = type->init_target(cmd_ctx, target);
726 if (ERROR_OK != retval)
728 LOG_ERROR("target '%s' init failed", target_name(target));
729 return retval;
733 * @todo get rid of those *memory_imp() methods, now that all
734 * callers are using target_*_memory() accessors ... and make
735 * sure the "physical" paths handle the same issues.
737 /* a non-invasive way(in terms of patches) to add some code that
738 * runs before the type->write/read_memory implementation
740 type->write_memory_imp = target->type->write_memory;
741 type->write_memory = target_write_memory_imp;
743 type->read_memory_imp = target->type->read_memory;
744 type->read_memory = target_read_memory_imp;
746 type->soft_reset_halt_imp = target->type->soft_reset_halt;
747 type->soft_reset_halt = target_soft_reset_halt_imp;
749 type->run_algorithm_imp = target->type->run_algorithm;
750 type->run_algorithm = target_run_algorithm_imp;
752 /* Sanity-check MMU support ... stub in what we must, to help
753 * implement it in stages, but warn if we need to do so.
755 if (type->mmu)
757 if (type->write_phys_memory == NULL)
759 LOG_ERROR("type '%s' is missing write_phys_memory",
760 type->name);
761 type->write_phys_memory = err_write_phys_memory;
763 if (type->read_phys_memory == NULL)
765 LOG_ERROR("type '%s' is missing read_phys_memory",
766 type->name);
767 type->read_phys_memory = err_read_phys_memory;
769 if (type->virt2phys == NULL)
771 LOG_ERROR("type '%s' is missing virt2phys", type->name);
772 type->virt2phys = identity_virt2phys;
775 else
777 /* Make sure no-MMU targets all behave the same: make no
778 * distinction between physical and virtual addresses, and
779 * ensure that virt2phys() is always an identity mapping.
781 if (type->write_phys_memory || type->read_phys_memory
782 || type->virt2phys)
784 LOG_WARNING("type '%s' has bad MMU hooks", type->name);
787 type->mmu = no_mmu;
788 type->write_phys_memory = type->write_memory;
789 type->read_phys_memory = type->read_memory;
790 type->virt2phys = identity_virt2phys;
792 return ERROR_OK;
795 int target_init(struct command_context *cmd_ctx)
797 struct target *target;
798 int retval;
800 for (target = all_targets; target; target = target->next)
802 retval = target_init_one(cmd_ctx, target);
803 if (ERROR_OK != retval)
804 return retval;
807 if (!all_targets)
808 return ERROR_OK;
810 retval = target_register_user_commands(cmd_ctx);
811 if (ERROR_OK != retval)
812 return retval;
814 retval = target_register_timer_callback(&handle_target,
815 100, 1, cmd_ctx->interp);
816 if (ERROR_OK != retval)
817 return retval;
819 return ERROR_OK;
822 COMMAND_HANDLER(handle_target_init_command)
824 if (CMD_ARGC != 0)
825 return ERROR_COMMAND_SYNTAX_ERROR;
827 static bool target_initialized = false;
828 if (target_initialized)
830 LOG_INFO("'target init' has already been called");
831 return ERROR_OK;
833 target_initialized = true;
835 LOG_DEBUG("Initializing targets...");
836 return target_init(CMD_CTX);
839 int target_register_event_callback(int (*callback)(struct target *target, enum target_event event, void *priv), void *priv)
841 struct target_event_callback **callbacks_p = &target_event_callbacks;
843 if (callback == NULL)
845 return ERROR_INVALID_ARGUMENTS;
848 if (*callbacks_p)
850 while ((*callbacks_p)->next)
851 callbacks_p = &((*callbacks_p)->next);
852 callbacks_p = &((*callbacks_p)->next);
855 (*callbacks_p) = malloc(sizeof(struct target_event_callback));
856 (*callbacks_p)->callback = callback;
857 (*callbacks_p)->priv = priv;
858 (*callbacks_p)->next = NULL;
860 return ERROR_OK;
863 int target_register_timer_callback(int (*callback)(void *priv), int time_ms, int periodic, void *priv)
865 struct target_timer_callback **callbacks_p = &target_timer_callbacks;
866 struct timeval now;
868 if (callback == NULL)
870 return ERROR_INVALID_ARGUMENTS;
873 if (*callbacks_p)
875 while ((*callbacks_p)->next)
876 callbacks_p = &((*callbacks_p)->next);
877 callbacks_p = &((*callbacks_p)->next);
880 (*callbacks_p) = malloc(sizeof(struct target_timer_callback));
881 (*callbacks_p)->callback = callback;
882 (*callbacks_p)->periodic = periodic;
883 (*callbacks_p)->time_ms = time_ms;
885 gettimeofday(&now, NULL);
886 (*callbacks_p)->when.tv_usec = now.tv_usec + (time_ms % 1000) * 1000;
887 time_ms -= (time_ms % 1000);
888 (*callbacks_p)->when.tv_sec = now.tv_sec + (time_ms / 1000);
889 if ((*callbacks_p)->when.tv_usec > 1000000)
891 (*callbacks_p)->when.tv_usec = (*callbacks_p)->when.tv_usec - 1000000;
892 (*callbacks_p)->when.tv_sec += 1;
895 (*callbacks_p)->priv = priv;
896 (*callbacks_p)->next = NULL;
898 return ERROR_OK;
901 int target_unregister_event_callback(int (*callback)(struct target *target, enum target_event event, void *priv), void *priv)
903 struct target_event_callback **p = &target_event_callbacks;
904 struct target_event_callback *c = target_event_callbacks;
906 if (callback == NULL)
908 return ERROR_INVALID_ARGUMENTS;
911 while (c)
913 struct target_event_callback *next = c->next;
914 if ((c->callback == callback) && (c->priv == priv))
916 *p = next;
917 free(c);
918 return ERROR_OK;
920 else
921 p = &(c->next);
922 c = next;
925 return ERROR_OK;
928 int target_unregister_timer_callback(int (*callback)(void *priv), void *priv)
930 struct target_timer_callback **p = &target_timer_callbacks;
931 struct target_timer_callback *c = target_timer_callbacks;
933 if (callback == NULL)
935 return ERROR_INVALID_ARGUMENTS;
938 while (c)
940 struct target_timer_callback *next = c->next;
941 if ((c->callback == callback) && (c->priv == priv))
943 *p = next;
944 free(c);
945 return ERROR_OK;
947 else
948 p = &(c->next);
949 c = next;
952 return ERROR_OK;
955 int target_call_event_callbacks(struct target *target, enum target_event event)
957 struct target_event_callback *callback = target_event_callbacks;
958 struct target_event_callback *next_callback;
960 if (event == TARGET_EVENT_HALTED)
962 /* execute early halted first */
963 target_call_event_callbacks(target, TARGET_EVENT_GDB_HALT);
966 LOG_DEBUG("target event %i (%s)",
967 event,
968 Jim_Nvp_value2name_simple(nvp_target_event, event)->name);
970 target_handle_event(target, event);
972 while (callback)
974 next_callback = callback->next;
975 callback->callback(target, event, callback->priv);
976 callback = next_callback;
979 return ERROR_OK;
982 static int target_timer_callback_periodic_restart(
983 struct target_timer_callback *cb, struct timeval *now)
985 int time_ms = cb->time_ms;
986 cb->when.tv_usec = now->tv_usec + (time_ms % 1000) * 1000;
987 time_ms -= (time_ms % 1000);
988 cb->when.tv_sec = now->tv_sec + time_ms / 1000;
989 if (cb->when.tv_usec > 1000000)
991 cb->when.tv_usec = cb->when.tv_usec - 1000000;
992 cb->when.tv_sec += 1;
994 return ERROR_OK;
997 static int target_call_timer_callback(struct target_timer_callback *cb,
998 struct timeval *now)
1000 cb->callback(cb->priv);
1002 if (cb->periodic)
1003 return target_timer_callback_periodic_restart(cb, now);
1005 return target_unregister_timer_callback(cb->callback, cb->priv);
1008 static int target_call_timer_callbacks_check_time(int checktime)
1010 keep_alive();
1012 struct timeval now;
1013 gettimeofday(&now, NULL);
1015 struct target_timer_callback *callback = target_timer_callbacks;
1016 while (callback)
1018 // cleaning up may unregister and free this callback
1019 struct target_timer_callback *next_callback = callback->next;
1021 bool call_it = callback->callback &&
1022 ((!checktime && callback->periodic) ||
1023 now.tv_sec > callback->when.tv_sec ||
1024 (now.tv_sec == callback->when.tv_sec &&
1025 now.tv_usec >= callback->when.tv_usec));
1027 if (call_it)
1029 int retval = target_call_timer_callback(callback, &now);
1030 if (retval != ERROR_OK)
1031 return retval;
1034 callback = next_callback;
1037 return ERROR_OK;
1040 int target_call_timer_callbacks(void)
1042 return target_call_timer_callbacks_check_time(1);
1045 /* invoke periodic callbacks immediately */
1046 int target_call_timer_callbacks_now(void)
1048 return target_call_timer_callbacks_check_time(0);
1051 int target_alloc_working_area(struct target *target, uint32_t size, struct working_area **area)
1053 struct working_area *c = target->working_areas;
1054 struct working_area *new_wa = NULL;
1056 /* Reevaluate working area address based on MMU state*/
1057 if (target->working_areas == NULL)
1059 int retval;
1060 int enabled;
1062 retval = target->type->mmu(target, &enabled);
1063 if (retval != ERROR_OK)
1065 return retval;
1068 if (!enabled) {
1069 if (target->working_area_phys_spec) {
1070 LOG_DEBUG("MMU disabled, using physical "
1071 "address for working memory 0x%08x",
1072 (unsigned)target->working_area_phys);
1073 target->working_area = target->working_area_phys;
1074 } else {
1075 LOG_ERROR("No working memory available. "
1076 "Specify -work-area-phys to target.");
1077 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
1079 } else {
1080 if (target->working_area_virt_spec) {
1081 LOG_DEBUG("MMU enabled, using virtual "
1082 "address for working memory 0x%08x",
1083 (unsigned)target->working_area_virt);
1084 target->working_area = target->working_area_virt;
1085 } else {
1086 LOG_ERROR("No working memory available. "
1087 "Specify -work-area-virt to target.");
1088 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
1093 /* only allocate multiples of 4 byte */
1094 if (size % 4)
1096 LOG_ERROR("BUG: code tried to allocate unaligned number of bytes (0x%08x), padding", ((unsigned)(size)));
1097 size = (size + 3) & (~3);
1100 /* see if there's already a matching working area */
1101 while (c)
1103 if ((c->free) && (c->size == size))
1105 new_wa = c;
1106 break;
1108 c = c->next;
1111 /* if not, allocate a new one */
1112 if (!new_wa)
1114 struct working_area **p = &target->working_areas;
1115 uint32_t first_free = target->working_area;
1116 uint32_t free_size = target->working_area_size;
1118 c = target->working_areas;
1119 while (c)
1121 first_free += c->size;
1122 free_size -= c->size;
1123 p = &c->next;
1124 c = c->next;
1127 if (free_size < size)
1129 LOG_WARNING("not enough working area available(requested %u, free %u)",
1130 (unsigned)(size), (unsigned)(free_size));
1131 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
1134 LOG_DEBUG("allocated new working area at address 0x%08x", (unsigned)first_free);
1136 new_wa = malloc(sizeof(struct working_area));
1137 new_wa->next = NULL;
1138 new_wa->size = size;
1139 new_wa->address = first_free;
1141 if (target->backup_working_area)
1143 int retval;
1144 new_wa->backup = malloc(new_wa->size);
1145 if ((retval = target_read_memory(target, new_wa->address, 4, new_wa->size / 4, new_wa->backup)) != ERROR_OK)
1147 free(new_wa->backup);
1148 free(new_wa);
1149 return retval;
1152 else
1154 new_wa->backup = NULL;
1157 /* put new entry in list */
1158 *p = new_wa;
1161 /* mark as used, and return the new (reused) area */
1162 new_wa->free = 0;
1163 *area = new_wa;
1165 /* user pointer */
1166 new_wa->user = area;
1168 return ERROR_OK;
1171 int target_free_working_area_restore(struct target *target, struct working_area *area, int restore)
1173 if (area->free)
1174 return ERROR_OK;
1176 if (restore && target->backup_working_area)
1178 int retval;
1179 if ((retval = target_write_memory(target, area->address, 4, area->size / 4, area->backup)) != ERROR_OK)
1180 return retval;
1183 area->free = 1;
1185 /* mark user pointer invalid */
1186 *area->user = NULL;
1187 area->user = NULL;
1189 return ERROR_OK;
1192 int target_free_working_area(struct target *target, struct working_area *area)
1194 return target_free_working_area_restore(target, area, 1);
1197 /* free resources and restore memory, if restoring memory fails,
1198 * free up resources anyway
1200 void target_free_all_working_areas_restore(struct target *target, int restore)
1202 struct working_area *c = target->working_areas;
1204 while (c)
1206 struct working_area *next = c->next;
1207 target_free_working_area_restore(target, c, restore);
1209 if (c->backup)
1210 free(c->backup);
1212 free(c);
1214 c = next;
1217 target->working_areas = NULL;
1220 void target_free_all_working_areas(struct target *target)
1222 target_free_all_working_areas_restore(target, 1);
1225 int target_arch_state(struct target *target)
1227 int retval;
1228 if (target == NULL)
1230 LOG_USER("No target has been configured");
1231 return ERROR_OK;
1234 LOG_USER("target state: %s", target_state_name( target ));
1236 if (target->state != TARGET_HALTED)
1237 return ERROR_OK;
1239 retval = target->type->arch_state(target);
1240 return retval;
1243 /* Single aligned words are guaranteed to use 16 or 32 bit access
1244 * mode respectively, otherwise data is handled as quickly as
1245 * possible
1247 int target_write_buffer(struct target *target, uint32_t address, uint32_t size, uint8_t *buffer)
1249 int retval;
1250 LOG_DEBUG("writing buffer of %i byte at 0x%8.8x",
1251 (int)size, (unsigned)address);
1253 if (!target_was_examined(target))
1255 LOG_ERROR("Target not examined yet");
1256 return ERROR_FAIL;
1259 if (size == 0) {
1260 return ERROR_OK;
1263 if ((address + size - 1) < address)
1265 /* GDB can request this when e.g. PC is 0xfffffffc*/
1266 LOG_ERROR("address + size wrapped(0x%08x, 0x%08x)",
1267 (unsigned)address,
1268 (unsigned)size);
1269 return ERROR_FAIL;
1272 if (((address % 2) == 0) && (size == 2))
1274 return target_write_memory(target, address, 2, 1, buffer);
1277 /* handle unaligned head bytes */
1278 if (address % 4)
1280 uint32_t unaligned = 4 - (address % 4);
1282 if (unaligned > size)
1283 unaligned = size;
1285 if ((retval = target_write_memory(target, address, 1, unaligned, buffer)) != ERROR_OK)
1286 return retval;
1288 buffer += unaligned;
1289 address += unaligned;
1290 size -= unaligned;
1293 /* handle aligned words */
1294 if (size >= 4)
1296 int aligned = size - (size % 4);
1298 /* use bulk writes above a certain limit. This may have to be changed */
1299 if (aligned > 128)
1301 if ((retval = target->type->bulk_write_memory(target, address, aligned / 4, buffer)) != ERROR_OK)
1302 return retval;
1304 else
1306 if ((retval = target_write_memory(target, address, 4, aligned / 4, buffer)) != ERROR_OK)
1307 return retval;
1310 buffer += aligned;
1311 address += aligned;
1312 size -= aligned;
1315 /* handle tail writes of less than 4 bytes */
1316 if (size > 0)
1318 if ((retval = target_write_memory(target, address, 1, size, buffer)) != ERROR_OK)
1319 return retval;
1322 return ERROR_OK;
1325 /* Single aligned words are guaranteed to use 16 or 32 bit access
1326 * mode respectively, otherwise data is handled as quickly as
1327 * possible
1329 int target_read_buffer(struct target *target, uint32_t address, uint32_t size, uint8_t *buffer)
1331 int retval;
1332 LOG_DEBUG("reading buffer of %i byte at 0x%8.8x",
1333 (int)size, (unsigned)address);
1335 if (!target_was_examined(target))
1337 LOG_ERROR("Target not examined yet");
1338 return ERROR_FAIL;
1341 if (size == 0) {
1342 return ERROR_OK;
1345 if ((address + size - 1) < address)
1347 /* GDB can request this when e.g. PC is 0xfffffffc*/
1348 LOG_ERROR("address + size wrapped(0x%08" PRIx32 ", 0x%08" PRIx32 ")",
1349 address,
1350 size);
1351 return ERROR_FAIL;
1354 if (((address % 2) == 0) && (size == 2))
1356 return target_read_memory(target, address, 2, 1, buffer);
1359 /* handle unaligned head bytes */
1360 if (address % 4)
1362 uint32_t unaligned = 4 - (address % 4);
1364 if (unaligned > size)
1365 unaligned = size;
1367 if ((retval = target_read_memory(target, address, 1, unaligned, buffer)) != ERROR_OK)
1368 return retval;
1370 buffer += unaligned;
1371 address += unaligned;
1372 size -= unaligned;
1375 /* handle aligned words */
1376 if (size >= 4)
1378 int aligned = size - (size % 4);
1380 if ((retval = target_read_memory(target, address, 4, aligned / 4, buffer)) != ERROR_OK)
1381 return retval;
1383 buffer += aligned;
1384 address += aligned;
1385 size -= aligned;
1388 /*prevent byte access when possible (avoid AHB access limitations in some cases)*/
1389 if(size >=2)
1391 int aligned = size - (size%2);
1392 retval = target_read_memory(target, address, 2, aligned / 2, buffer);
1393 if (retval != ERROR_OK)
1394 return retval;
1396 buffer += aligned;
1397 address += aligned;
1398 size -= aligned;
1400 /* handle tail writes of less than 4 bytes */
1401 if (size > 0)
1403 if ((retval = target_read_memory(target, address, 1, size, buffer)) != ERROR_OK)
1404 return retval;
1407 return ERROR_OK;
1410 int target_checksum_memory(struct target *target, uint32_t address, uint32_t size, uint32_t* crc)
1412 uint8_t *buffer;
1413 int retval;
1414 uint32_t i;
1415 uint32_t checksum = 0;
1416 if (!target_was_examined(target))
1418 LOG_ERROR("Target not examined yet");
1419 return ERROR_FAIL;
1422 if ((retval = target->type->checksum_memory(target, address,
1423 size, &checksum)) != ERROR_OK)
1425 buffer = malloc(size);
1426 if (buffer == NULL)
1428 LOG_ERROR("error allocating buffer for section (%d bytes)", (int)size);
1429 return ERROR_INVALID_ARGUMENTS;
1431 retval = target_read_buffer(target, address, size, buffer);
1432 if (retval != ERROR_OK)
1434 free(buffer);
1435 return retval;
1438 /* convert to target endianess */
1439 for (i = 0; i < (size/sizeof(uint32_t)); i++)
1441 uint32_t target_data;
1442 target_data = target_buffer_get_u32(target, &buffer[i*sizeof(uint32_t)]);
1443 target_buffer_set_u32(target, &buffer[i*sizeof(uint32_t)], target_data);
1446 retval = image_calculate_checksum(buffer, size, &checksum);
1447 free(buffer);
1450 *crc = checksum;
1452 return retval;
1455 int target_blank_check_memory(struct target *target, uint32_t address, uint32_t size, uint32_t* blank)
1457 int retval;
1458 if (!target_was_examined(target))
1460 LOG_ERROR("Target not examined yet");
1461 return ERROR_FAIL;
1464 if (target->type->blank_check_memory == 0)
1465 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
1467 retval = target->type->blank_check_memory(target, address, size, blank);
1469 return retval;
1472 int target_read_u32(struct target *target, uint32_t address, uint32_t *value)
1474 uint8_t value_buf[4];
1475 if (!target_was_examined(target))
1477 LOG_ERROR("Target not examined yet");
1478 return ERROR_FAIL;
1481 int retval = target_read_memory(target, address, 4, 1, value_buf);
1483 if (retval == ERROR_OK)
1485 *value = target_buffer_get_u32(target, value_buf);
1486 LOG_DEBUG("address: 0x%8.8" PRIx32 ", value: 0x%8.8" PRIx32 "",
1487 address,
1488 *value);
1490 else
1492 *value = 0x0;
1493 LOG_DEBUG("address: 0x%8.8" PRIx32 " failed",
1494 address);
1497 return retval;
1500 int target_read_u16(struct target *target, uint32_t address, uint16_t *value)
1502 uint8_t value_buf[2];
1503 if (!target_was_examined(target))
1505 LOG_ERROR("Target not examined yet");
1506 return ERROR_FAIL;
1509 int retval = target_read_memory(target, address, 2, 1, value_buf);
1511 if (retval == ERROR_OK)
1513 *value = target_buffer_get_u16(target, value_buf);
1514 LOG_DEBUG("address: 0x%8.8" PRIx32 ", value: 0x%4.4x",
1515 address,
1516 *value);
1518 else
1520 *value = 0x0;
1521 LOG_DEBUG("address: 0x%8.8" PRIx32 " failed",
1522 address);
1525 return retval;
1528 int target_read_u8(struct target *target, uint32_t address, uint8_t *value)
1530 int retval = target_read_memory(target, address, 1, 1, value);
1531 if (!target_was_examined(target))
1533 LOG_ERROR("Target not examined yet");
1534 return ERROR_FAIL;
1537 if (retval == ERROR_OK)
1539 LOG_DEBUG("address: 0x%8.8" PRIx32 ", value: 0x%2.2x",
1540 address,
1541 *value);
1543 else
1545 *value = 0x0;
1546 LOG_DEBUG("address: 0x%8.8" PRIx32 " failed",
1547 address);
1550 return retval;
1553 int target_write_u32(struct target *target, uint32_t address, uint32_t value)
1555 int retval;
1556 uint8_t value_buf[4];
1557 if (!target_was_examined(target))
1559 LOG_ERROR("Target not examined yet");
1560 return ERROR_FAIL;
1563 LOG_DEBUG("address: 0x%8.8" PRIx32 ", value: 0x%8.8" PRIx32 "",
1564 address,
1565 value);
1567 target_buffer_set_u32(target, value_buf, value);
1568 if ((retval = target_write_memory(target, address, 4, 1, value_buf)) != ERROR_OK)
1570 LOG_DEBUG("failed: %i", retval);
1573 return retval;
1576 int target_write_u16(struct target *target, uint32_t address, uint16_t value)
1578 int retval;
1579 uint8_t value_buf[2];
1580 if (!target_was_examined(target))
1582 LOG_ERROR("Target not examined yet");
1583 return ERROR_FAIL;
1586 LOG_DEBUG("address: 0x%8.8" PRIx32 ", value: 0x%8.8x",
1587 address,
1588 value);
1590 target_buffer_set_u16(target, value_buf, value);
1591 if ((retval = target_write_memory(target, address, 2, 1, value_buf)) != ERROR_OK)
1593 LOG_DEBUG("failed: %i", retval);
1596 return retval;
1599 int target_write_u8(struct target *target, uint32_t address, uint8_t value)
1601 int retval;
1602 if (!target_was_examined(target))
1604 LOG_ERROR("Target not examined yet");
1605 return ERROR_FAIL;
1608 LOG_DEBUG("address: 0x%8.8" PRIx32 ", value: 0x%2.2x",
1609 address, value);
1611 if ((retval = target_write_memory(target, address, 1, 1, &value)) != ERROR_OK)
1613 LOG_DEBUG("failed: %i", retval);
1616 return retval;
1619 COMMAND_HANDLER(handle_targets_command)
1621 struct target *target = all_targets;
1623 if (CMD_ARGC == 1)
1625 target = get_target(CMD_ARGV[0]);
1626 if (target == NULL) {
1627 command_print(CMD_CTX,"Target: %s is unknown, try one of:\n", CMD_ARGV[0]);
1628 goto DumpTargets;
1630 if (!target->tap->enabled) {
1631 command_print(CMD_CTX,"Target: TAP %s is disabled, "
1632 "can't be the current target\n",
1633 target->tap->dotted_name);
1634 return ERROR_FAIL;
1637 CMD_CTX->current_target = target->target_number;
1638 return ERROR_OK;
1640 DumpTargets:
1642 target = all_targets;
1643 command_print(CMD_CTX, " TargetName Type Endian TapName State ");
1644 command_print(CMD_CTX, "-- ------------------ ---------- ------ ------------------ ------------");
1645 while (target)
1647 const char *state;
1648 char marker = ' ';
1650 if (target->tap->enabled)
1651 state = target_state_name( target );
1652 else
1653 state = "tap-disabled";
1655 if (CMD_CTX->current_target == target->target_number)
1656 marker = '*';
1658 /* keep columns lined up to match the headers above */
1659 command_print(CMD_CTX, "%2d%c %-18s %-10s %-6s %-18s %s",
1660 target->target_number,
1661 marker,
1662 target_name(target),
1663 target_type_name(target),
1664 Jim_Nvp_value2name_simple(nvp_target_endian,
1665 target->endianness)->name,
1666 target->tap->dotted_name,
1667 state);
1668 target = target->next;
1671 return ERROR_OK;
1674 /* every 300ms we check for reset & powerdropout and issue a "reset halt" if so. */
1676 static int powerDropout;
1677 static int srstAsserted;
1679 static int runPowerRestore;
1680 static int runPowerDropout;
1681 static int runSrstAsserted;
1682 static int runSrstDeasserted;
1684 static int sense_handler(void)
1686 static int prevSrstAsserted = 0;
1687 static int prevPowerdropout = 0;
1689 int retval;
1690 if ((retval = jtag_power_dropout(&powerDropout)) != ERROR_OK)
1691 return retval;
1693 int powerRestored;
1694 powerRestored = prevPowerdropout && !powerDropout;
1695 if (powerRestored)
1697 runPowerRestore = 1;
1700 long long current = timeval_ms();
1701 static long long lastPower = 0;
1702 int waitMore = lastPower + 2000 > current;
1703 if (powerDropout && !waitMore)
1705 runPowerDropout = 1;
1706 lastPower = current;
1709 if ((retval = jtag_srst_asserted(&srstAsserted)) != ERROR_OK)
1710 return retval;
1712 int srstDeasserted;
1713 srstDeasserted = prevSrstAsserted && !srstAsserted;
1715 static long long lastSrst = 0;
1716 waitMore = lastSrst + 2000 > current;
1717 if (srstDeasserted && !waitMore)
1719 runSrstDeasserted = 1;
1720 lastSrst = current;
1723 if (!prevSrstAsserted && srstAsserted)
1725 runSrstAsserted = 1;
1728 prevSrstAsserted = srstAsserted;
1729 prevPowerdropout = powerDropout;
1731 if (srstDeasserted || powerRestored)
1733 /* Other than logging the event we can't do anything here.
1734 * Issuing a reset is a particularly bad idea as we might
1735 * be inside a reset already.
1739 return ERROR_OK;
1742 static void target_call_event_callbacks_all(enum target_event e) {
1743 struct target *target;
1744 target = all_targets;
1745 while (target) {
1746 target_call_event_callbacks(target, e);
1747 target = target->next;
1751 /* process target state changes */
1752 static int handle_target(void *priv)
1754 Jim_Interp *interp = (Jim_Interp *)priv;
1755 int retval = ERROR_OK;
1757 /* we do not want to recurse here... */
1758 static int recursive = 0;
1759 if (! recursive)
1761 recursive = 1;
1762 sense_handler();
1763 /* danger! running these procedures can trigger srst assertions and power dropouts.
1764 * We need to avoid an infinite loop/recursion here and we do that by
1765 * clearing the flags after running these events.
1767 int did_something = 0;
1768 if (runSrstAsserted)
1770 target_call_event_callbacks_all(TARGET_EVENT_GDB_HALT);
1771 Jim_Eval(interp, "srst_asserted");
1772 did_something = 1;
1774 if (runSrstDeasserted)
1776 Jim_Eval(interp, "srst_deasserted");
1777 did_something = 1;
1779 if (runPowerDropout)
1781 target_call_event_callbacks_all(TARGET_EVENT_GDB_HALT);
1782 Jim_Eval(interp, "power_dropout");
1783 did_something = 1;
1785 if (runPowerRestore)
1787 Jim_Eval(interp, "power_restore");
1788 did_something = 1;
1791 if (did_something)
1793 /* clear detect flags */
1794 sense_handler();
1797 /* clear action flags */
1799 runSrstAsserted = 0;
1800 runSrstDeasserted = 0;
1801 runPowerRestore = 0;
1802 runPowerDropout = 0;
1804 recursive = 0;
1807 /* Poll targets for state changes unless that's globally disabled.
1808 * Skip targets that are currently disabled.
1810 for (struct target *target = all_targets;
1811 is_jtag_poll_safe() && target;
1812 target = target->next)
1814 if (!target->tap->enabled)
1815 continue;
1817 /* only poll target if we've got power and srst isn't asserted */
1818 if (!powerDropout && !srstAsserted)
1820 /* polling may fail silently until the target has been examined */
1821 if ((retval = target_poll(target)) != ERROR_OK)
1823 target_call_event_callbacks(target, TARGET_EVENT_GDB_HALT);
1824 return retval;
1829 return retval;
1832 COMMAND_HANDLER(handle_reg_command)
1834 struct target *target;
1835 struct reg *reg = NULL;
1836 unsigned count = 0;
1837 char *value;
1839 LOG_DEBUG("-");
1841 target = get_current_target(CMD_CTX);
1843 /* list all available registers for the current target */
1844 if (CMD_ARGC == 0)
1846 struct reg_cache *cache = target->reg_cache;
1848 count = 0;
1849 while (cache)
1851 unsigned i;
1853 command_print(CMD_CTX, "===== %s", cache->name);
1855 for (i = 0, reg = cache->reg_list;
1856 i < cache->num_regs;
1857 i++, reg++, count++)
1859 /* only print cached values if they are valid */
1860 if (reg->valid) {
1861 value = buf_to_str(reg->value,
1862 reg->size, 16);
1863 command_print(CMD_CTX,
1864 "(%i) %s (/%" PRIu32 "): 0x%s%s",
1865 count, reg->name,
1866 reg->size, value,
1867 reg->dirty
1868 ? " (dirty)"
1869 : "");
1870 free(value);
1871 } else {
1872 command_print(CMD_CTX, "(%i) %s (/%" PRIu32 ")",
1873 count, reg->name,
1874 reg->size) ;
1877 cache = cache->next;
1880 return ERROR_OK;
1883 /* access a single register by its ordinal number */
1884 if ((CMD_ARGV[0][0] >= '0') && (CMD_ARGV[0][0] <= '9'))
1886 unsigned num;
1887 COMMAND_PARSE_NUMBER(uint, CMD_ARGV[0], num);
1889 struct reg_cache *cache = target->reg_cache;
1890 count = 0;
1891 while (cache)
1893 unsigned i;
1894 for (i = 0; i < cache->num_regs; i++)
1896 if (count++ == num)
1898 reg = &cache->reg_list[i];
1899 break;
1902 if (reg)
1903 break;
1904 cache = cache->next;
1907 if (!reg)
1909 command_print(CMD_CTX, "%i is out of bounds, the current target has only %i registers (0 - %i)", num, count, count - 1);
1910 return ERROR_OK;
1912 } else /* access a single register by its name */
1914 reg = register_get_by_name(target->reg_cache, CMD_ARGV[0], 1);
1916 if (!reg)
1918 command_print(CMD_CTX, "register %s not found in current target", CMD_ARGV[0]);
1919 return ERROR_OK;
1923 /* display a register */
1924 if ((CMD_ARGC == 1) || ((CMD_ARGC == 2) && !((CMD_ARGV[1][0] >= '0') && (CMD_ARGV[1][0] <= '9'))))
1926 if ((CMD_ARGC == 2) && (strcmp(CMD_ARGV[1], "force") == 0))
1927 reg->valid = 0;
1929 if (reg->valid == 0)
1931 reg->type->get(reg);
1933 value = buf_to_str(reg->value, reg->size, 16);
1934 command_print(CMD_CTX, "%s (/%i): 0x%s", reg->name, (int)(reg->size), value);
1935 free(value);
1936 return ERROR_OK;
1939 /* set register value */
1940 if (CMD_ARGC == 2)
1942 uint8_t *buf = malloc(DIV_ROUND_UP(reg->size, 8));
1943 str_to_buf(CMD_ARGV[1], strlen(CMD_ARGV[1]), buf, reg->size, 0);
1945 reg->type->set(reg, buf);
1947 value = buf_to_str(reg->value, reg->size, 16);
1948 command_print(CMD_CTX, "%s (/%i): 0x%s", reg->name, (int)(reg->size), value);
1949 free(value);
1951 free(buf);
1953 return ERROR_OK;
1956 command_print(CMD_CTX, "usage: reg <#|name> [value]");
1958 return ERROR_OK;
1961 COMMAND_HANDLER(handle_poll_command)
1963 int retval = ERROR_OK;
1964 struct target *target = get_current_target(CMD_CTX);
1966 if (CMD_ARGC == 0)
1968 command_print(CMD_CTX, "background polling: %s",
1969 jtag_poll_get_enabled() ? "on" : "off");
1970 command_print(CMD_CTX, "TAP: %s (%s)",
1971 target->tap->dotted_name,
1972 target->tap->enabled ? "enabled" : "disabled");
1973 if (!target->tap->enabled)
1974 return ERROR_OK;
1975 if ((retval = target_poll(target)) != ERROR_OK)
1976 return retval;
1977 if ((retval = target_arch_state(target)) != ERROR_OK)
1978 return retval;
1980 else if (CMD_ARGC == 1)
1982 bool enable;
1983 COMMAND_PARSE_ON_OFF(CMD_ARGV[0], enable);
1984 jtag_poll_set_enabled(enable);
1986 else
1988 return ERROR_COMMAND_SYNTAX_ERROR;
1991 return retval;
1994 COMMAND_HANDLER(handle_wait_halt_command)
1996 if (CMD_ARGC > 1)
1997 return ERROR_COMMAND_SYNTAX_ERROR;
1999 unsigned ms = 5000;
2000 if (1 == CMD_ARGC)
2002 int retval = parse_uint(CMD_ARGV[0], &ms);
2003 if (ERROR_OK != retval)
2005 command_print(CMD_CTX, "usage: %s [seconds]", CMD_NAME);
2006 return ERROR_COMMAND_SYNTAX_ERROR;
2008 // convert seconds (given) to milliseconds (needed)
2009 ms *= 1000;
2012 struct target *target = get_current_target(CMD_CTX);
2013 return target_wait_state(target, TARGET_HALTED, ms);
2016 /* wait for target state to change. The trick here is to have a low
2017 * latency for short waits and not to suck up all the CPU time
2018 * on longer waits.
2020 * After 500ms, keep_alive() is invoked
2022 int target_wait_state(struct target *target, enum target_state state, int ms)
2024 int retval;
2025 long long then = 0, cur;
2026 int once = 1;
2028 for (;;)
2030 if ((retval = target_poll(target)) != ERROR_OK)
2031 return retval;
2032 if (target->state == state)
2034 break;
2036 cur = timeval_ms();
2037 if (once)
2039 once = 0;
2040 then = timeval_ms();
2041 LOG_DEBUG("waiting for target %s...",
2042 Jim_Nvp_value2name_simple(nvp_target_state,state)->name);
2045 if (cur-then > 500)
2047 keep_alive();
2050 if ((cur-then) > ms)
2052 LOG_ERROR("timed out while waiting for target %s",
2053 Jim_Nvp_value2name_simple(nvp_target_state,state)->name);
2054 return ERROR_FAIL;
2058 return ERROR_OK;
2061 COMMAND_HANDLER(handle_halt_command)
2063 LOG_DEBUG("-");
2065 struct target *target = get_current_target(CMD_CTX);
2066 int retval = target_halt(target);
2067 if (ERROR_OK != retval)
2068 return retval;
2070 if (CMD_ARGC == 1)
2072 unsigned wait;
2073 retval = parse_uint(CMD_ARGV[0], &wait);
2074 if (ERROR_OK != retval)
2075 return ERROR_COMMAND_SYNTAX_ERROR;
2076 if (!wait)
2077 return ERROR_OK;
2080 return CALL_COMMAND_HANDLER(handle_wait_halt_command);
2083 COMMAND_HANDLER(handle_soft_reset_halt_command)
2085 struct target *target = get_current_target(CMD_CTX);
2087 LOG_USER("requesting target halt and executing a soft reset");
2089 target->type->soft_reset_halt(target);
2091 return ERROR_OK;
2094 COMMAND_HANDLER(handle_reset_command)
2096 if (CMD_ARGC > 1)
2097 return ERROR_COMMAND_SYNTAX_ERROR;
2099 enum target_reset_mode reset_mode = RESET_RUN;
2100 if (CMD_ARGC == 1)
2102 const Jim_Nvp *n;
2103 n = Jim_Nvp_name2value_simple(nvp_reset_modes, CMD_ARGV[0]);
2104 if ((n->name == NULL) || (n->value == RESET_UNKNOWN)) {
2105 return ERROR_COMMAND_SYNTAX_ERROR;
2107 reset_mode = n->value;
2110 /* reset *all* targets */
2111 return target_process_reset(CMD_CTX, reset_mode);
2115 COMMAND_HANDLER(handle_resume_command)
2117 int current = 1;
2118 if (CMD_ARGC > 1)
2119 return ERROR_COMMAND_SYNTAX_ERROR;
2121 struct target *target = get_current_target(CMD_CTX);
2122 target_handle_event(target, TARGET_EVENT_OLD_pre_resume);
2124 /* with no CMD_ARGV, resume from current pc, addr = 0,
2125 * with one arguments, addr = CMD_ARGV[0],
2126 * handle breakpoints, not debugging */
2127 uint32_t addr = 0;
2128 if (CMD_ARGC == 1)
2130 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], addr);
2131 current = 0;
2134 return target_resume(target, current, addr, 1, 0);
2137 COMMAND_HANDLER(handle_step_command)
2139 if (CMD_ARGC > 1)
2140 return ERROR_COMMAND_SYNTAX_ERROR;
2142 LOG_DEBUG("-");
2144 /* with no CMD_ARGV, step from current pc, addr = 0,
2145 * with one argument addr = CMD_ARGV[0],
2146 * handle breakpoints, debugging */
2147 uint32_t addr = 0;
2148 int current_pc = 1;
2149 if (CMD_ARGC == 1)
2151 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], addr);
2152 current_pc = 0;
2155 struct target *target = get_current_target(CMD_CTX);
2157 return target->type->step(target, current_pc, addr, 1);
2160 static void handle_md_output(struct command_context *cmd_ctx,
2161 struct target *target, uint32_t address, unsigned size,
2162 unsigned count, const uint8_t *buffer)
2164 const unsigned line_bytecnt = 32;
2165 unsigned line_modulo = line_bytecnt / size;
2167 char output[line_bytecnt * 4 + 1];
2168 unsigned output_len = 0;
2170 const char *value_fmt;
2171 switch (size) {
2172 case 4: value_fmt = "%8.8x "; break;
2173 case 2: value_fmt = "%4.2x "; break;
2174 case 1: value_fmt = "%2.2x "; break;
2175 default:
2176 /* "can't happen", caller checked */
2177 LOG_ERROR("invalid memory read size: %u", size);
2178 return;
2181 for (unsigned i = 0; i < count; i++)
2183 if (i % line_modulo == 0)
2185 output_len += snprintf(output + output_len,
2186 sizeof(output) - output_len,
2187 "0x%8.8x: ",
2188 (unsigned)(address + (i*size)));
2191 uint32_t value = 0;
2192 const uint8_t *value_ptr = buffer + i * size;
2193 switch (size) {
2194 case 4: value = target_buffer_get_u32(target, value_ptr); break;
2195 case 2: value = target_buffer_get_u16(target, value_ptr); break;
2196 case 1: value = *value_ptr;
2198 output_len += snprintf(output + output_len,
2199 sizeof(output) - output_len,
2200 value_fmt, value);
2202 if ((i % line_modulo == line_modulo - 1) || (i == count - 1))
2204 command_print(cmd_ctx, "%s", output);
2205 output_len = 0;
2210 COMMAND_HANDLER(handle_md_command)
2212 if (CMD_ARGC < 1)
2213 return ERROR_COMMAND_SYNTAX_ERROR;
2215 unsigned size = 0;
2216 switch (CMD_NAME[2]) {
2217 case 'w': size = 4; break;
2218 case 'h': size = 2; break;
2219 case 'b': size = 1; break;
2220 default: return ERROR_COMMAND_SYNTAX_ERROR;
2223 bool physical=strcmp(CMD_ARGV[0], "phys")==0;
2224 int (*fn)(struct target *target,
2225 uint32_t address, uint32_t size, uint32_t count, uint8_t *buffer);
2226 if (physical)
2228 CMD_ARGC--;
2229 CMD_ARGV++;
2230 fn=target_read_phys_memory;
2231 } else
2233 fn=target_read_memory;
2235 if ((CMD_ARGC < 1) || (CMD_ARGC > 2))
2237 return ERROR_COMMAND_SYNTAX_ERROR;
2240 uint32_t address;
2241 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], address);
2243 unsigned count = 1;
2244 if (CMD_ARGC == 2)
2245 COMMAND_PARSE_NUMBER(uint, CMD_ARGV[1], count);
2247 uint8_t *buffer = calloc(count, size);
2249 struct target *target = get_current_target(CMD_CTX);
2250 int retval = fn(target, address, size, count, buffer);
2251 if (ERROR_OK == retval)
2252 handle_md_output(CMD_CTX, target, address, size, count, buffer);
2254 free(buffer);
2256 return retval;
2259 COMMAND_HANDLER(handle_mw_command)
2261 if (CMD_ARGC < 2)
2263 return ERROR_COMMAND_SYNTAX_ERROR;
2265 bool physical=strcmp(CMD_ARGV[0], "phys")==0;
2266 int (*fn)(struct target *target,
2267 uint32_t address, uint32_t size, uint32_t count, uint8_t *buffer);
2268 if (physical)
2270 CMD_ARGC--;
2271 CMD_ARGV++;
2272 fn=target_write_phys_memory;
2273 } else
2275 fn=target_write_memory;
2277 if ((CMD_ARGC < 2) || (CMD_ARGC > 3))
2278 return ERROR_COMMAND_SYNTAX_ERROR;
2280 uint32_t address;
2281 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], address);
2283 uint32_t value;
2284 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], value);
2286 unsigned count = 1;
2287 if (CMD_ARGC == 3)
2288 COMMAND_PARSE_NUMBER(uint, CMD_ARGV[2], count);
2290 struct target *target = get_current_target(CMD_CTX);
2291 unsigned wordsize;
2292 uint8_t value_buf[4];
2293 switch (CMD_NAME[2])
2295 case 'w':
2296 wordsize = 4;
2297 target_buffer_set_u32(target, value_buf, value);
2298 break;
2299 case 'h':
2300 wordsize = 2;
2301 target_buffer_set_u16(target, value_buf, value);
2302 break;
2303 case 'b':
2304 wordsize = 1;
2305 value_buf[0] = value;
2306 break;
2307 default:
2308 return ERROR_COMMAND_SYNTAX_ERROR;
2310 for (unsigned i = 0; i < count; i++)
2312 int retval = fn(target,
2313 address + i * wordsize, wordsize, 1, value_buf);
2314 if (ERROR_OK != retval)
2315 return retval;
2316 keep_alive();
2319 return ERROR_OK;
2323 static COMMAND_HELPER(parse_load_image_command_CMD_ARGV, struct image *image,
2324 uint32_t *min_address, uint32_t *max_address)
2326 if (CMD_ARGC < 1 || CMD_ARGC > 5)
2327 return ERROR_COMMAND_SYNTAX_ERROR;
2329 /* a base address isn't always necessary,
2330 * default to 0x0 (i.e. don't relocate) */
2331 if (CMD_ARGC >= 2)
2333 uint32_t addr;
2334 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], addr);
2335 image->base_address = addr;
2336 image->base_address_set = 1;
2338 else
2339 image->base_address_set = 0;
2341 image->start_address_set = 0;
2343 if (CMD_ARGC >= 4)
2345 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[3], *min_address);
2347 if (CMD_ARGC == 5)
2349 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[4], *max_address);
2350 // use size (given) to find max (required)
2351 *max_address += *min_address;
2354 if (*min_address > *max_address)
2355 return ERROR_COMMAND_SYNTAX_ERROR;
2357 return ERROR_OK;
2360 COMMAND_HANDLER(handle_load_image_command)
2362 uint8_t *buffer;
2363 size_t buf_cnt;
2364 uint32_t image_size;
2365 uint32_t min_address = 0;
2366 uint32_t max_address = 0xffffffff;
2367 int i;
2368 struct image image;
2370 int retval = CALL_COMMAND_HANDLER(parse_load_image_command_CMD_ARGV,
2371 &image, &min_address, &max_address);
2372 if (ERROR_OK != retval)
2373 return retval;
2375 struct target *target = get_current_target(CMD_CTX);
2377 struct duration bench;
2378 duration_start(&bench);
2380 if (image_open(&image, CMD_ARGV[0], (CMD_ARGC >= 3) ? CMD_ARGV[2] : NULL) != ERROR_OK)
2382 return ERROR_OK;
2385 image_size = 0x0;
2386 retval = ERROR_OK;
2387 for (i = 0; i < image.num_sections; i++)
2389 buffer = malloc(image.sections[i].size);
2390 if (buffer == NULL)
2392 command_print(CMD_CTX,
2393 "error allocating buffer for section (%d bytes)",
2394 (int)(image.sections[i].size));
2395 break;
2398 if ((retval = image_read_section(&image, i, 0x0, image.sections[i].size, buffer, &buf_cnt)) != ERROR_OK)
2400 free(buffer);
2401 break;
2404 uint32_t offset = 0;
2405 uint32_t length = buf_cnt;
2407 /* DANGER!!! beware of unsigned comparision here!!! */
2409 if ((image.sections[i].base_address + buf_cnt >= min_address)&&
2410 (image.sections[i].base_address < max_address))
2412 if (image.sections[i].base_address < min_address)
2414 /* clip addresses below */
2415 offset += min_address-image.sections[i].base_address;
2416 length -= offset;
2419 if (image.sections[i].base_address + buf_cnt > max_address)
2421 length -= (image.sections[i].base_address + buf_cnt)-max_address;
2424 if ((retval = target_write_buffer(target, image.sections[i].base_address + offset, length, buffer + offset)) != ERROR_OK)
2426 free(buffer);
2427 break;
2429 image_size += length;
2430 command_print(CMD_CTX, "%u bytes written at address 0x%8.8" PRIx32 "",
2431 (unsigned int)length,
2432 image.sections[i].base_address + offset);
2435 free(buffer);
2438 if ((ERROR_OK == retval) && (duration_measure(&bench) == ERROR_OK))
2440 command_print(CMD_CTX, "downloaded %" PRIu32 " bytes "
2441 "in %fs (%0.3f kb/s)", image_size,
2442 duration_elapsed(&bench), duration_kbps(&bench, image_size));
2445 image_close(&image);
2447 return retval;
2451 COMMAND_HANDLER(handle_dump_image_command)
2453 struct fileio fileio;
2455 uint8_t buffer[560];
2456 int retvaltemp;
2459 struct target *target = get_current_target(CMD_CTX);
2461 if (CMD_ARGC != 3)
2463 command_print(CMD_CTX, "usage: dump_image <filename> <address> <size>");
2464 return ERROR_OK;
2467 uint32_t address;
2468 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], address);
2469 uint32_t size;
2470 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[2], size);
2472 if (fileio_open(&fileio, CMD_ARGV[0], FILEIO_WRITE, FILEIO_BINARY) != ERROR_OK)
2474 return ERROR_OK;
2477 struct duration bench;
2478 duration_start(&bench);
2480 int retval = ERROR_OK;
2481 while (size > 0)
2483 size_t size_written;
2484 uint32_t this_run_size = (size > 560) ? 560 : size;
2485 retval = target_read_buffer(target, address, this_run_size, buffer);
2486 if (retval != ERROR_OK)
2488 break;
2491 retval = fileio_write(&fileio, this_run_size, buffer, &size_written);
2492 if (retval != ERROR_OK)
2494 break;
2497 size -= this_run_size;
2498 address += this_run_size;
2501 if ((retvaltemp = fileio_close(&fileio)) != ERROR_OK)
2502 return retvaltemp;
2504 if ((ERROR_OK == retval) && (duration_measure(&bench) == ERROR_OK))
2506 command_print(CMD_CTX,
2507 "dumped %ld bytes in %fs (%0.3f kb/s)", (long)fileio.size,
2508 duration_elapsed(&bench), duration_kbps(&bench, fileio.size));
2511 return retval;
2514 static COMMAND_HELPER(handle_verify_image_command_internal, int verify)
2516 uint8_t *buffer;
2517 size_t buf_cnt;
2518 uint32_t image_size;
2519 int i;
2520 int retval;
2521 uint32_t checksum = 0;
2522 uint32_t mem_checksum = 0;
2524 struct image image;
2526 struct target *target = get_current_target(CMD_CTX);
2528 if (CMD_ARGC < 1)
2530 return ERROR_COMMAND_SYNTAX_ERROR;
2533 if (!target)
2535 LOG_ERROR("no target selected");
2536 return ERROR_FAIL;
2539 struct duration bench;
2540 duration_start(&bench);
2542 if (CMD_ARGC >= 2)
2544 uint32_t addr;
2545 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], addr);
2546 image.base_address = addr;
2547 image.base_address_set = 1;
2549 else
2551 image.base_address_set = 0;
2552 image.base_address = 0x0;
2555 image.start_address_set = 0;
2557 if ((retval = image_open(&image, CMD_ARGV[0], (CMD_ARGC == 3) ? CMD_ARGV[2] : NULL)) != ERROR_OK)
2559 return retval;
2562 image_size = 0x0;
2563 retval = ERROR_OK;
2564 for (i = 0; i < image.num_sections; i++)
2566 buffer = malloc(image.sections[i].size);
2567 if (buffer == NULL)
2569 command_print(CMD_CTX,
2570 "error allocating buffer for section (%d bytes)",
2571 (int)(image.sections[i].size));
2572 break;
2574 if ((retval = image_read_section(&image, i, 0x0, image.sections[i].size, buffer, &buf_cnt)) != ERROR_OK)
2576 free(buffer);
2577 break;
2580 if (verify)
2582 /* calculate checksum of image */
2583 image_calculate_checksum(buffer, buf_cnt, &checksum);
2585 retval = target_checksum_memory(target, image.sections[i].base_address, buf_cnt, &mem_checksum);
2586 if (retval != ERROR_OK)
2588 free(buffer);
2589 break;
2592 if (checksum != mem_checksum)
2594 /* failed crc checksum, fall back to a binary compare */
2595 uint8_t *data;
2597 command_print(CMD_CTX, "checksum mismatch - attempting binary compare");
2599 data = (uint8_t*)malloc(buf_cnt);
2601 /* Can we use 32bit word accesses? */
2602 int size = 1;
2603 int count = buf_cnt;
2604 if ((count % 4) == 0)
2606 size *= 4;
2607 count /= 4;
2609 retval = target_read_memory(target, image.sections[i].base_address, size, count, data);
2610 if (retval == ERROR_OK)
2612 uint32_t t;
2613 for (t = 0; t < buf_cnt; t++)
2615 if (data[t] != buffer[t])
2617 command_print(CMD_CTX,
2618 "Verify operation failed address 0x%08x. Was 0x%02x instead of 0x%02x\n",
2619 (unsigned)(t + image.sections[i].base_address),
2620 data[t],
2621 buffer[t]);
2622 free(data);
2623 free(buffer);
2624 retval = ERROR_FAIL;
2625 goto done;
2627 if ((t%16384) == 0)
2629 keep_alive();
2634 free(data);
2636 } else
2638 command_print(CMD_CTX, "address 0x%08" PRIx32 " length 0x%08zx",
2639 image.sections[i].base_address,
2640 buf_cnt);
2643 free(buffer);
2644 image_size += buf_cnt;
2646 done:
2647 if ((ERROR_OK == retval) && (duration_measure(&bench) == ERROR_OK))
2649 command_print(CMD_CTX, "verified %" PRIu32 " bytes "
2650 "in %fs (%0.3f kb/s)", image_size,
2651 duration_elapsed(&bench), duration_kbps(&bench, image_size));
2654 image_close(&image);
2656 return retval;
2659 COMMAND_HANDLER(handle_verify_image_command)
2661 return CALL_COMMAND_HANDLER(handle_verify_image_command_internal, 1);
2664 COMMAND_HANDLER(handle_test_image_command)
2666 return CALL_COMMAND_HANDLER(handle_verify_image_command_internal, 0);
2669 static int handle_bp_command_list(struct command_context *cmd_ctx)
2671 struct target *target = get_current_target(cmd_ctx);
2672 struct breakpoint *breakpoint = target->breakpoints;
2673 while (breakpoint)
2675 if (breakpoint->type == BKPT_SOFT)
2677 char* buf = buf_to_str(breakpoint->orig_instr,
2678 breakpoint->length, 16);
2679 command_print(cmd_ctx, "0x%8.8" PRIx32 ", 0x%x, %i, 0x%s",
2680 breakpoint->address,
2681 breakpoint->length,
2682 breakpoint->set, buf);
2683 free(buf);
2685 else
2687 command_print(cmd_ctx, "0x%8.8" PRIx32 ", 0x%x, %i",
2688 breakpoint->address,
2689 breakpoint->length, breakpoint->set);
2692 breakpoint = breakpoint->next;
2694 return ERROR_OK;
2697 static int handle_bp_command_set(struct command_context *cmd_ctx,
2698 uint32_t addr, uint32_t length, int hw)
2700 struct target *target = get_current_target(cmd_ctx);
2701 int retval = breakpoint_add(target, addr, length, hw);
2702 if (ERROR_OK == retval)
2703 command_print(cmd_ctx, "breakpoint set at 0x%8.8" PRIx32 "", addr);
2704 else
2705 LOG_ERROR("Failure setting breakpoint");
2706 return retval;
2709 COMMAND_HANDLER(handle_bp_command)
2711 if (CMD_ARGC == 0)
2712 return handle_bp_command_list(CMD_CTX);
2714 if (CMD_ARGC < 2 || CMD_ARGC > 3)
2716 command_print(CMD_CTX, "usage: bp <address> <length> ['hw']");
2717 return ERROR_COMMAND_SYNTAX_ERROR;
2720 uint32_t addr;
2721 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], addr);
2722 uint32_t length;
2723 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], length);
2725 int hw = BKPT_SOFT;
2726 if (CMD_ARGC == 3)
2728 if (strcmp(CMD_ARGV[2], "hw") == 0)
2729 hw = BKPT_HARD;
2730 else
2731 return ERROR_COMMAND_SYNTAX_ERROR;
2734 return handle_bp_command_set(CMD_CTX, addr, length, hw);
2737 COMMAND_HANDLER(handle_rbp_command)
2739 if (CMD_ARGC != 1)
2740 return ERROR_COMMAND_SYNTAX_ERROR;
2742 uint32_t addr;
2743 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], addr);
2745 struct target *target = get_current_target(CMD_CTX);
2746 breakpoint_remove(target, addr);
2748 return ERROR_OK;
2751 COMMAND_HANDLER(handle_wp_command)
2753 struct target *target = get_current_target(CMD_CTX);
2755 if (CMD_ARGC == 0)
2757 struct watchpoint *watchpoint = target->watchpoints;
2759 while (watchpoint)
2761 command_print(CMD_CTX, "address: 0x%8.8" PRIx32
2762 ", len: 0x%8.8" PRIx32
2763 ", r/w/a: %i, value: 0x%8.8" PRIx32
2764 ", mask: 0x%8.8" PRIx32,
2765 watchpoint->address,
2766 watchpoint->length,
2767 (int)watchpoint->rw,
2768 watchpoint->value,
2769 watchpoint->mask);
2770 watchpoint = watchpoint->next;
2772 return ERROR_OK;
2775 enum watchpoint_rw type = WPT_ACCESS;
2776 uint32_t addr = 0;
2777 uint32_t length = 0;
2778 uint32_t data_value = 0x0;
2779 uint32_t data_mask = 0xffffffff;
2781 switch (CMD_ARGC)
2783 case 5:
2784 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[4], data_mask);
2785 // fall through
2786 case 4:
2787 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[3], data_value);
2788 // fall through
2789 case 3:
2790 switch (CMD_ARGV[2][0])
2792 case 'r':
2793 type = WPT_READ;
2794 break;
2795 case 'w':
2796 type = WPT_WRITE;
2797 break;
2798 case 'a':
2799 type = WPT_ACCESS;
2800 break;
2801 default:
2802 LOG_ERROR("invalid watchpoint mode ('%c')", CMD_ARGV[2][0]);
2803 return ERROR_COMMAND_SYNTAX_ERROR;
2805 // fall through
2806 case 2:
2807 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], length);
2808 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], addr);
2809 break;
2811 default:
2812 command_print(CMD_CTX, "usage: wp [address length "
2813 "[(r|w|a) [value [mask]]]]");
2814 return ERROR_COMMAND_SYNTAX_ERROR;
2817 int retval = watchpoint_add(target, addr, length, type,
2818 data_value, data_mask);
2819 if (ERROR_OK != retval)
2820 LOG_ERROR("Failure setting watchpoints");
2822 return retval;
2825 COMMAND_HANDLER(handle_rwp_command)
2827 if (CMD_ARGC != 1)
2828 return ERROR_COMMAND_SYNTAX_ERROR;
2830 uint32_t addr;
2831 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], addr);
2833 struct target *target = get_current_target(CMD_CTX);
2834 watchpoint_remove(target, addr);
2836 return ERROR_OK;
2841 * Translate a virtual address to a physical address.
2843 * The low-level target implementation must have logged a detailed error
2844 * which is forwarded to telnet/GDB session.
2846 COMMAND_HANDLER(handle_virt2phys_command)
2848 if (CMD_ARGC != 1)
2849 return ERROR_COMMAND_SYNTAX_ERROR;
2851 uint32_t va;
2852 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], va);
2853 uint32_t pa;
2855 struct target *target = get_current_target(CMD_CTX);
2856 int retval = target->type->virt2phys(target, va, &pa);
2857 if (retval == ERROR_OK)
2858 command_print(CMD_CTX, "Physical address 0x%08" PRIx32 "", pa);
2860 return retval;
2863 static void writeData(FILE *f, const void *data, size_t len)
2865 size_t written = fwrite(data, 1, len, f);
2866 if (written != len)
2867 LOG_ERROR("failed to write %zu bytes: %s", len, strerror(errno));
2870 static void writeLong(FILE *f, int l)
2872 int i;
2873 for (i = 0; i < 4; i++)
2875 char c = (l >> (i*8))&0xff;
2876 writeData(f, &c, 1);
2881 static void writeString(FILE *f, char *s)
2883 writeData(f, s, strlen(s));
2886 /* Dump a gmon.out histogram file. */
2887 static void writeGmon(uint32_t *samples, uint32_t sampleNum, const char *filename)
2889 uint32_t i;
2890 FILE *f = fopen(filename, "w");
2891 if (f == NULL)
2892 return;
2893 writeString(f, "gmon");
2894 writeLong(f, 0x00000001); /* Version */
2895 writeLong(f, 0); /* padding */
2896 writeLong(f, 0); /* padding */
2897 writeLong(f, 0); /* padding */
2899 uint8_t zero = 0; /* GMON_TAG_TIME_HIST */
2900 writeData(f, &zero, 1);
2902 /* figure out bucket size */
2903 uint32_t min = samples[0];
2904 uint32_t max = samples[0];
2905 for (i = 0; i < sampleNum; i++)
2907 if (min > samples[i])
2909 min = samples[i];
2911 if (max < samples[i])
2913 max = samples[i];
2917 int addressSpace = (max-min + 1);
2919 static const uint32_t maxBuckets = 256 * 1024; /* maximum buckets. */
2920 uint32_t length = addressSpace;
2921 if (length > maxBuckets)
2923 length = maxBuckets;
2925 int *buckets = malloc(sizeof(int)*length);
2926 if (buckets == NULL)
2928 fclose(f);
2929 return;
2931 memset(buckets, 0, sizeof(int)*length);
2932 for (i = 0; i < sampleNum;i++)
2934 uint32_t address = samples[i];
2935 long long a = address-min;
2936 long long b = length-1;
2937 long long c = addressSpace-1;
2938 int index = (a*b)/c; /* danger!!!! int32 overflows */
2939 buckets[index]++;
2942 /* append binary memory gmon.out &profile_hist_hdr ((char*)&profile_hist_hdr + sizeof(struct gmon_hist_hdr)) */
2943 writeLong(f, min); /* low_pc */
2944 writeLong(f, max); /* high_pc */
2945 writeLong(f, length); /* # of samples */
2946 writeLong(f, 64000000); /* 64MHz */
2947 writeString(f, "seconds");
2948 for (i = 0; i < (15-strlen("seconds")); i++)
2949 writeData(f, &zero, 1);
2950 writeString(f, "s");
2952 /*append binary memory gmon.out profile_hist_data (profile_hist_data + profile_hist_hdr.hist_size) */
2954 char *data = malloc(2*length);
2955 if (data != NULL)
2957 for (i = 0; i < length;i++)
2959 int val;
2960 val = buckets[i];
2961 if (val > 65535)
2963 val = 65535;
2965 data[i*2]=val&0xff;
2966 data[i*2 + 1]=(val >> 8)&0xff;
2968 free(buckets);
2969 writeData(f, data, length * 2);
2970 free(data);
2971 } else
2973 free(buckets);
2976 fclose(f);
2979 /* profiling samples the CPU PC as quickly as OpenOCD is able,
2980 * which will be used as a random sampling of PC */
2981 COMMAND_HANDLER(handle_profile_command)
2983 struct target *target = get_current_target(CMD_CTX);
2984 struct timeval timeout, now;
2986 gettimeofday(&timeout, NULL);
2987 if (CMD_ARGC != 2)
2989 return ERROR_COMMAND_SYNTAX_ERROR;
2991 unsigned offset;
2992 COMMAND_PARSE_NUMBER(uint, CMD_ARGV[0], offset);
2994 timeval_add_time(&timeout, offset, 0);
2997 * @todo: Some cores let us sample the PC without the
2998 * annoying halt/resume step; for example, ARMv7 PCSR.
2999 * Provide a way to use that more efficient mechanism.
3002 command_print(CMD_CTX, "Starting profiling. Halting and resuming the target as often as we can...");
3004 static const int maxSample = 10000;
3005 uint32_t *samples = malloc(sizeof(uint32_t)*maxSample);
3006 if (samples == NULL)
3007 return ERROR_OK;
3009 int numSamples = 0;
3010 /* hopefully it is safe to cache! We want to stop/restart as quickly as possible. */
3011 struct reg *reg = register_get_by_name(target->reg_cache, "pc", 1);
3013 for (;;)
3015 int retval;
3016 target_poll(target);
3017 if (target->state == TARGET_HALTED)
3019 uint32_t t=*((uint32_t *)reg->value);
3020 samples[numSamples++]=t;
3021 retval = target_resume(target, 1, 0, 0, 0); /* current pc, addr = 0, do not handle breakpoints, not debugging */
3022 target_poll(target);
3023 alive_sleep(10); /* sleep 10ms, i.e. <100 samples/second. */
3024 } else if (target->state == TARGET_RUNNING)
3026 /* We want to quickly sample the PC. */
3027 if ((retval = target_halt(target)) != ERROR_OK)
3029 free(samples);
3030 return retval;
3032 } else
3034 command_print(CMD_CTX, "Target not halted or running");
3035 retval = ERROR_OK;
3036 break;
3038 if (retval != ERROR_OK)
3040 break;
3043 gettimeofday(&now, NULL);
3044 if ((numSamples >= maxSample) || ((now.tv_sec >= timeout.tv_sec) && (now.tv_usec >= timeout.tv_usec)))
3046 command_print(CMD_CTX, "Profiling completed. %d samples.", numSamples);
3047 if ((retval = target_poll(target)) != ERROR_OK)
3049 free(samples);
3050 return retval;
3052 if (target->state == TARGET_HALTED)
3054 target_resume(target, 1, 0, 0, 0); /* current pc, addr = 0, do not handle breakpoints, not debugging */
3056 if ((retval = target_poll(target)) != ERROR_OK)
3058 free(samples);
3059 return retval;
3061 writeGmon(samples, numSamples, CMD_ARGV[1]);
3062 command_print(CMD_CTX, "Wrote %s", CMD_ARGV[1]);
3063 break;
3066 free(samples);
3068 return ERROR_OK;
3071 static int new_int_array_element(Jim_Interp * interp, const char *varname, int idx, uint32_t val)
3073 char *namebuf;
3074 Jim_Obj *nameObjPtr, *valObjPtr;
3075 int result;
3077 namebuf = alloc_printf("%s(%d)", varname, idx);
3078 if (!namebuf)
3079 return JIM_ERR;
3081 nameObjPtr = Jim_NewStringObj(interp, namebuf, -1);
3082 valObjPtr = Jim_NewIntObj(interp, val);
3083 if (!nameObjPtr || !valObjPtr)
3085 free(namebuf);
3086 return JIM_ERR;
3089 Jim_IncrRefCount(nameObjPtr);
3090 Jim_IncrRefCount(valObjPtr);
3091 result = Jim_SetVariable(interp, nameObjPtr, valObjPtr);
3092 Jim_DecrRefCount(interp, nameObjPtr);
3093 Jim_DecrRefCount(interp, valObjPtr);
3094 free(namebuf);
3095 /* printf("%s(%d) <= 0%08x\n", varname, idx, val); */
3096 return result;
3099 static int jim_mem2array(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
3101 struct command_context *context;
3102 struct target *target;
3104 context = Jim_GetAssocData(interp, "context");
3105 if (context == NULL)
3107 LOG_ERROR("mem2array: no command context");
3108 return JIM_ERR;
3110 target = get_current_target(context);
3111 if (target == NULL)
3113 LOG_ERROR("mem2array: no current target");
3114 return JIM_ERR;
3117 return target_mem2array(interp, target, argc-1, argv + 1);
3120 static int target_mem2array(Jim_Interp *interp, struct target *target, int argc, Jim_Obj *const *argv)
3122 long l;
3123 uint32_t width;
3124 int len;
3125 uint32_t addr;
3126 uint32_t count;
3127 uint32_t v;
3128 const char *varname;
3129 int n, e, retval;
3130 uint32_t i;
3132 /* argv[1] = name of array to receive the data
3133 * argv[2] = desired width
3134 * argv[3] = memory address
3135 * argv[4] = count of times to read
3137 if (argc != 4) {
3138 Jim_WrongNumArgs(interp, 1, argv, "varname width addr nelems");
3139 return JIM_ERR;
3141 varname = Jim_GetString(argv[0], &len);
3142 /* given "foo" get space for worse case "foo(%d)" .. add 20 */
3144 e = Jim_GetLong(interp, argv[1], &l);
3145 width = l;
3146 if (e != JIM_OK) {
3147 return e;
3150 e = Jim_GetLong(interp, argv[2], &l);
3151 addr = l;
3152 if (e != JIM_OK) {
3153 return e;
3155 e = Jim_GetLong(interp, argv[3], &l);
3156 len = l;
3157 if (e != JIM_OK) {
3158 return e;
3160 switch (width) {
3161 case 8:
3162 width = 1;
3163 break;
3164 case 16:
3165 width = 2;
3166 break;
3167 case 32:
3168 width = 4;
3169 break;
3170 default:
3171 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3172 Jim_AppendStrings(interp, Jim_GetResult(interp), "Invalid width param, must be 8/16/32", NULL);
3173 return JIM_ERR;
3175 if (len == 0) {
3176 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3177 Jim_AppendStrings(interp, Jim_GetResult(interp), "mem2array: zero width read?", NULL);
3178 return JIM_ERR;
3180 if ((addr + (len * width)) < addr) {
3181 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3182 Jim_AppendStrings(interp, Jim_GetResult(interp), "mem2array: addr + len - wraps to zero?", NULL);
3183 return JIM_ERR;
3185 /* absurd transfer size? */
3186 if (len > 65536) {
3187 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3188 Jim_AppendStrings(interp, Jim_GetResult(interp), "mem2array: absurd > 64K item request", NULL);
3189 return JIM_ERR;
3192 if ((width == 1) ||
3193 ((width == 2) && ((addr & 1) == 0)) ||
3194 ((width == 4) && ((addr & 3) == 0))) {
3195 /* all is well */
3196 } else {
3197 char buf[100];
3198 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3199 sprintf(buf, "mem2array address: 0x%08" PRIx32 " is not aligned for %" PRId32 " byte reads",
3200 addr,
3201 width);
3202 Jim_AppendStrings(interp, Jim_GetResult(interp), buf , NULL);
3203 return JIM_ERR;
3206 /* Transfer loop */
3208 /* index counter */
3209 n = 0;
3211 size_t buffersize = 4096;
3212 uint8_t *buffer = malloc(buffersize);
3213 if (buffer == NULL)
3214 return JIM_ERR;
3216 /* assume ok */
3217 e = JIM_OK;
3218 while (len) {
3219 /* Slurp... in buffer size chunks */
3221 count = len; /* in objects.. */
3222 if (count > (buffersize/width)) {
3223 count = (buffersize/width);
3226 retval = target_read_memory(target, addr, width, count, buffer);
3227 if (retval != ERROR_OK) {
3228 /* BOO !*/
3229 LOG_ERROR("mem2array: Read @ 0x%08x, w=%d, cnt=%d, failed",
3230 (unsigned int)addr,
3231 (int)width,
3232 (int)count);
3233 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3234 Jim_AppendStrings(interp, Jim_GetResult(interp), "mem2array: cannot read memory", NULL);
3235 e = JIM_ERR;
3236 len = 0;
3237 } else {
3238 v = 0; /* shut up gcc */
3239 for (i = 0 ;i < count ;i++, n++) {
3240 switch (width) {
3241 case 4:
3242 v = target_buffer_get_u32(target, &buffer[i*width]);
3243 break;
3244 case 2:
3245 v = target_buffer_get_u16(target, &buffer[i*width]);
3246 break;
3247 case 1:
3248 v = buffer[i] & 0x0ff;
3249 break;
3251 new_int_array_element(interp, varname, n, v);
3253 len -= count;
3257 free(buffer);
3259 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3261 return JIM_OK;
3264 static int get_int_array_element(Jim_Interp * interp, const char *varname, int idx, uint32_t *val)
3266 char *namebuf;
3267 Jim_Obj *nameObjPtr, *valObjPtr;
3268 int result;
3269 long l;
3271 namebuf = alloc_printf("%s(%d)", varname, idx);
3272 if (!namebuf)
3273 return JIM_ERR;
3275 nameObjPtr = Jim_NewStringObj(interp, namebuf, -1);
3276 if (!nameObjPtr)
3278 free(namebuf);
3279 return JIM_ERR;
3282 Jim_IncrRefCount(nameObjPtr);
3283 valObjPtr = Jim_GetVariable(interp, nameObjPtr, JIM_ERRMSG);
3284 Jim_DecrRefCount(interp, nameObjPtr);
3285 free(namebuf);
3286 if (valObjPtr == NULL)
3287 return JIM_ERR;
3289 result = Jim_GetLong(interp, valObjPtr, &l);
3290 /* printf("%s(%d) => 0%08x\n", varname, idx, val); */
3291 *val = l;
3292 return result;
3295 static int jim_array2mem(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
3297 struct command_context *context;
3298 struct target *target;
3300 context = Jim_GetAssocData(interp, "context");
3301 if (context == NULL) {
3302 LOG_ERROR("array2mem: no command context");
3303 return JIM_ERR;
3305 target = get_current_target(context);
3306 if (target == NULL) {
3307 LOG_ERROR("array2mem: no current target");
3308 return JIM_ERR;
3311 return target_array2mem(interp,target, argc-1, argv + 1);
3314 static int target_array2mem(Jim_Interp *interp, struct target *target,
3315 int argc, Jim_Obj *const *argv)
3317 long l;
3318 uint32_t width;
3319 int len;
3320 uint32_t addr;
3321 uint32_t count;
3322 uint32_t v;
3323 const char *varname;
3324 int n, e, retval;
3325 uint32_t i;
3327 /* argv[1] = name of array to get the data
3328 * argv[2] = desired width
3329 * argv[3] = memory address
3330 * argv[4] = count to write
3332 if (argc != 4) {
3333 Jim_WrongNumArgs(interp, 0, argv, "varname width addr nelems");
3334 return JIM_ERR;
3336 varname = Jim_GetString(argv[0], &len);
3337 /* given "foo" get space for worse case "foo(%d)" .. add 20 */
3339 e = Jim_GetLong(interp, argv[1], &l);
3340 width = l;
3341 if (e != JIM_OK) {
3342 return e;
3345 e = Jim_GetLong(interp, argv[2], &l);
3346 addr = l;
3347 if (e != JIM_OK) {
3348 return e;
3350 e = Jim_GetLong(interp, argv[3], &l);
3351 len = l;
3352 if (e != JIM_OK) {
3353 return e;
3355 switch (width) {
3356 case 8:
3357 width = 1;
3358 break;
3359 case 16:
3360 width = 2;
3361 break;
3362 case 32:
3363 width = 4;
3364 break;
3365 default:
3366 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3367 Jim_AppendStrings(interp, Jim_GetResult(interp), "Invalid width param, must be 8/16/32", NULL);
3368 return JIM_ERR;
3370 if (len == 0) {
3371 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3372 Jim_AppendStrings(interp, Jim_GetResult(interp), "array2mem: zero width read?", NULL);
3373 return JIM_ERR;
3375 if ((addr + (len * width)) < addr) {
3376 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3377 Jim_AppendStrings(interp, Jim_GetResult(interp), "array2mem: addr + len - wraps to zero?", NULL);
3378 return JIM_ERR;
3380 /* absurd transfer size? */
3381 if (len > 65536) {
3382 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3383 Jim_AppendStrings(interp, Jim_GetResult(interp), "array2mem: absurd > 64K item request", NULL);
3384 return JIM_ERR;
3387 if ((width == 1) ||
3388 ((width == 2) && ((addr & 1) == 0)) ||
3389 ((width == 4) && ((addr & 3) == 0))) {
3390 /* all is well */
3391 } else {
3392 char buf[100];
3393 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3394 sprintf(buf, "array2mem address: 0x%08x is not aligned for %d byte reads",
3395 (unsigned int)addr,
3396 (int)width);
3397 Jim_AppendStrings(interp, Jim_GetResult(interp), buf , NULL);
3398 return JIM_ERR;
3401 /* Transfer loop */
3403 /* index counter */
3404 n = 0;
3405 /* assume ok */
3406 e = JIM_OK;
3408 size_t buffersize = 4096;
3409 uint8_t *buffer = malloc(buffersize);
3410 if (buffer == NULL)
3411 return JIM_ERR;
3413 while (len) {
3414 /* Slurp... in buffer size chunks */
3416 count = len; /* in objects.. */
3417 if (count > (buffersize/width)) {
3418 count = (buffersize/width);
3421 v = 0; /* shut up gcc */
3422 for (i = 0 ;i < count ;i++, n++) {
3423 get_int_array_element(interp, varname, n, &v);
3424 switch (width) {
3425 case 4:
3426 target_buffer_set_u32(target, &buffer[i*width], v);
3427 break;
3428 case 2:
3429 target_buffer_set_u16(target, &buffer[i*width], v);
3430 break;
3431 case 1:
3432 buffer[i] = v & 0x0ff;
3433 break;
3436 len -= count;
3438 retval = target_write_memory(target, addr, width, count, buffer);
3439 if (retval != ERROR_OK) {
3440 /* BOO !*/
3441 LOG_ERROR("array2mem: Write @ 0x%08x, w=%d, cnt=%d, failed",
3442 (unsigned int)addr,
3443 (int)width,
3444 (int)count);
3445 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3446 Jim_AppendStrings(interp, Jim_GetResult(interp), "array2mem: cannot read memory", NULL);
3447 e = JIM_ERR;
3448 len = 0;
3452 free(buffer);
3454 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3456 return JIM_OK;
3459 void target_all_handle_event(enum target_event e)
3461 struct target *target;
3463 LOG_DEBUG("**all*targets: event: %d, %s",
3464 (int)e,
3465 Jim_Nvp_value2name_simple(nvp_target_event, e)->name);
3467 target = all_targets;
3468 while (target) {
3469 target_handle_event(target, e);
3470 target = target->next;
3475 /* FIX? should we propagate errors here rather than printing them
3476 * and continuing?
3478 void target_handle_event(struct target *target, enum target_event e)
3480 struct target_event_action *teap;
3482 for (teap = target->event_action; teap != NULL; teap = teap->next) {
3483 if (teap->event == e) {
3484 LOG_DEBUG("target: (%d) %s (%s) event: %d (%s) action: %s",
3485 target->target_number,
3486 target_name(target),
3487 target_type_name(target),
3489 Jim_Nvp_value2name_simple(nvp_target_event, e)->name,
3490 Jim_GetString(teap->body, NULL));
3491 if (Jim_EvalObj(teap->interp, teap->body) != JIM_OK)
3493 Jim_PrintErrorMessage(teap->interp);
3500 * Returns true only if the target has a handler for the specified event.
3502 bool target_has_event_action(struct target *target, enum target_event event)
3504 struct target_event_action *teap;
3506 for (teap = target->event_action; teap != NULL; teap = teap->next) {
3507 if (teap->event == event)
3508 return true;
3510 return false;
3513 enum target_cfg_param {
3514 TCFG_TYPE,
3515 TCFG_EVENT,
3516 TCFG_WORK_AREA_VIRT,
3517 TCFG_WORK_AREA_PHYS,
3518 TCFG_WORK_AREA_SIZE,
3519 TCFG_WORK_AREA_BACKUP,
3520 TCFG_ENDIAN,
3521 TCFG_VARIANT,
3522 TCFG_CHAIN_POSITION,
3525 static Jim_Nvp nvp_config_opts[] = {
3526 { .name = "-type", .value = TCFG_TYPE },
3527 { .name = "-event", .value = TCFG_EVENT },
3528 { .name = "-work-area-virt", .value = TCFG_WORK_AREA_VIRT },
3529 { .name = "-work-area-phys", .value = TCFG_WORK_AREA_PHYS },
3530 { .name = "-work-area-size", .value = TCFG_WORK_AREA_SIZE },
3531 { .name = "-work-area-backup", .value = TCFG_WORK_AREA_BACKUP },
3532 { .name = "-endian" , .value = TCFG_ENDIAN },
3533 { .name = "-variant", .value = TCFG_VARIANT },
3534 { .name = "-chain-position", .value = TCFG_CHAIN_POSITION },
3536 { .name = NULL, .value = -1 }
3539 static int target_configure(Jim_GetOptInfo *goi, struct target *target)
3541 Jim_Nvp *n;
3542 Jim_Obj *o;
3543 jim_wide w;
3544 char *cp;
3545 int e;
3547 /* parse config or cget options ... */
3548 while (goi->argc > 0) {
3549 Jim_SetEmptyResult(goi->interp);
3550 /* Jim_GetOpt_Debug(goi); */
3552 if (target->type->target_jim_configure) {
3553 /* target defines a configure function */
3554 /* target gets first dibs on parameters */
3555 e = (*(target->type->target_jim_configure))(target, goi);
3556 if (e == JIM_OK) {
3557 /* more? */
3558 continue;
3560 if (e == JIM_ERR) {
3561 /* An error */
3562 return e;
3564 /* otherwise we 'continue' below */
3566 e = Jim_GetOpt_Nvp(goi, nvp_config_opts, &n);
3567 if (e != JIM_OK) {
3568 Jim_GetOpt_NvpUnknown(goi, nvp_config_opts, 0);
3569 return e;
3571 switch (n->value) {
3572 case TCFG_TYPE:
3573 /* not setable */
3574 if (goi->isconfigure) {
3575 Jim_SetResult_sprintf(goi->interp,
3576 "not settable: %s", n->name);
3577 return JIM_ERR;
3578 } else {
3579 no_params:
3580 if (goi->argc != 0) {
3581 Jim_WrongNumArgs(goi->interp,
3582 goi->argc, goi->argv,
3583 "NO PARAMS");
3584 return JIM_ERR;
3587 Jim_SetResultString(goi->interp,
3588 target_type_name(target), -1);
3589 /* loop for more */
3590 break;
3591 case TCFG_EVENT:
3592 if (goi->argc == 0) {
3593 Jim_WrongNumArgs(goi->interp, goi->argc, goi->argv, "-event ?event-name? ...");
3594 return JIM_ERR;
3597 e = Jim_GetOpt_Nvp(goi, nvp_target_event, &n);
3598 if (e != JIM_OK) {
3599 Jim_GetOpt_NvpUnknown(goi, nvp_target_event, 1);
3600 return e;
3603 if (goi->isconfigure) {
3604 if (goi->argc != 1) {
3605 Jim_WrongNumArgs(goi->interp, goi->argc, goi->argv, "-event ?event-name? ?EVENT-BODY?");
3606 return JIM_ERR;
3608 } else {
3609 if (goi->argc != 0) {
3610 Jim_WrongNumArgs(goi->interp, goi->argc, goi->argv, "-event ?event-name?");
3611 return JIM_ERR;
3616 struct target_event_action *teap;
3618 teap = target->event_action;
3619 /* replace existing? */
3620 while (teap) {
3621 if (teap->event == (enum target_event)n->value) {
3622 break;
3624 teap = teap->next;
3627 if (goi->isconfigure) {
3628 bool replace = true;
3629 if (teap == NULL) {
3630 /* create new */
3631 teap = calloc(1, sizeof(*teap));
3632 replace = false;
3634 teap->event = n->value;
3635 teap->interp = goi->interp;
3636 Jim_GetOpt_Obj(goi, &o);
3637 if (teap->body) {
3638 Jim_DecrRefCount(teap->interp, teap->body);
3640 teap->body = Jim_DuplicateObj(goi->interp, o);
3642 * FIXME:
3643 * Tcl/TK - "tk events" have a nice feature.
3644 * See the "BIND" command.
3645 * We should support that here.
3646 * You can specify %X and %Y in the event code.
3647 * The idea is: %T - target name.
3648 * The idea is: %N - target number
3649 * The idea is: %E - event name.
3651 Jim_IncrRefCount(teap->body);
3653 if (!replace)
3655 /* add to head of event list */
3656 teap->next = target->event_action;
3657 target->event_action = teap;
3659 Jim_SetEmptyResult(goi->interp);
3660 } else {
3661 /* get */
3662 if (teap == NULL) {
3663 Jim_SetEmptyResult(goi->interp);
3664 } else {
3665 Jim_SetResult(goi->interp, Jim_DuplicateObj(goi->interp, teap->body));
3669 /* loop for more */
3670 break;
3672 case TCFG_WORK_AREA_VIRT:
3673 if (goi->isconfigure) {
3674 target_free_all_working_areas(target);
3675 e = Jim_GetOpt_Wide(goi, &w);
3676 if (e != JIM_OK) {
3677 return e;
3679 target->working_area_virt = w;
3680 target->working_area_virt_spec = true;
3681 } else {
3682 if (goi->argc != 0) {
3683 goto no_params;
3686 Jim_SetResult(goi->interp, Jim_NewIntObj(goi->interp, target->working_area_virt));
3687 /* loop for more */
3688 break;
3690 case TCFG_WORK_AREA_PHYS:
3691 if (goi->isconfigure) {
3692 target_free_all_working_areas(target);
3693 e = Jim_GetOpt_Wide(goi, &w);
3694 if (e != JIM_OK) {
3695 return e;
3697 target->working_area_phys = w;
3698 target->working_area_phys_spec = true;
3699 } else {
3700 if (goi->argc != 0) {
3701 goto no_params;
3704 Jim_SetResult(goi->interp, Jim_NewIntObj(goi->interp, target->working_area_phys));
3705 /* loop for more */
3706 break;
3708 case TCFG_WORK_AREA_SIZE:
3709 if (goi->isconfigure) {
3710 target_free_all_working_areas(target);
3711 e = Jim_GetOpt_Wide(goi, &w);
3712 if (e != JIM_OK) {
3713 return e;
3715 target->working_area_size = w;
3716 } else {
3717 if (goi->argc != 0) {
3718 goto no_params;
3721 Jim_SetResult(goi->interp, Jim_NewIntObj(goi->interp, target->working_area_size));
3722 /* loop for more */
3723 break;
3725 case TCFG_WORK_AREA_BACKUP:
3726 if (goi->isconfigure) {
3727 target_free_all_working_areas(target);
3728 e = Jim_GetOpt_Wide(goi, &w);
3729 if (e != JIM_OK) {
3730 return e;
3732 /* make this exactly 1 or 0 */
3733 target->backup_working_area = (!!w);
3734 } else {
3735 if (goi->argc != 0) {
3736 goto no_params;
3739 Jim_SetResult(goi->interp, Jim_NewIntObj(goi->interp, target->backup_working_area));
3740 /* loop for more e*/
3741 break;
3743 case TCFG_ENDIAN:
3744 if (goi->isconfigure) {
3745 e = Jim_GetOpt_Nvp(goi, nvp_target_endian, &n);
3746 if (e != JIM_OK) {
3747 Jim_GetOpt_NvpUnknown(goi, nvp_target_endian, 1);
3748 return e;
3750 target->endianness = n->value;
3751 } else {
3752 if (goi->argc != 0) {
3753 goto no_params;
3756 n = Jim_Nvp_value2name_simple(nvp_target_endian, target->endianness);
3757 if (n->name == NULL) {
3758 target->endianness = TARGET_LITTLE_ENDIAN;
3759 n = Jim_Nvp_value2name_simple(nvp_target_endian, target->endianness);
3761 Jim_SetResultString(goi->interp, n->name, -1);
3762 /* loop for more */
3763 break;
3765 case TCFG_VARIANT:
3766 if (goi->isconfigure) {
3767 if (goi->argc < 1) {
3768 Jim_SetResult_sprintf(goi->interp,
3769 "%s ?STRING?",
3770 n->name);
3771 return JIM_ERR;
3773 if (target->variant) {
3774 free((void *)(target->variant));
3776 e = Jim_GetOpt_String(goi, &cp, NULL);
3777 target->variant = strdup(cp);
3778 } else {
3779 if (goi->argc != 0) {
3780 goto no_params;
3783 Jim_SetResultString(goi->interp, target->variant,-1);
3784 /* loop for more */
3785 break;
3786 case TCFG_CHAIN_POSITION:
3787 if (goi->isconfigure) {
3788 Jim_Obj *o;
3789 struct jtag_tap *tap;
3790 target_free_all_working_areas(target);
3791 e = Jim_GetOpt_Obj(goi, &o);
3792 if (e != JIM_OK) {
3793 return e;
3795 tap = jtag_tap_by_jim_obj(goi->interp, o);
3796 if (tap == NULL) {
3797 return JIM_ERR;
3799 /* make this exactly 1 or 0 */
3800 target->tap = tap;
3801 } else {
3802 if (goi->argc != 0) {
3803 goto no_params;
3806 Jim_SetResultString(goi->interp, target->tap->dotted_name, -1);
3807 /* loop for more e*/
3808 break;
3810 } /* while (goi->argc) */
3813 /* done - we return */
3814 return JIM_OK;
3817 static int
3818 jim_target_configure(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
3820 Jim_GetOptInfo goi;
3822 Jim_GetOpt_Setup(&goi, interp, argc - 1, argv + 1);
3823 goi.isconfigure = !strcmp(Jim_GetString(argv[0], NULL), "configure");
3824 int need_args = 1 + goi.isconfigure;
3825 if (goi.argc < need_args)
3827 Jim_WrongNumArgs(goi.interp, goi.argc, goi.argv,
3828 goi.isconfigure
3829 ? "missing: -option VALUE ..."
3830 : "missing: -option ...");
3831 return JIM_ERR;
3833 struct target *target = Jim_CmdPrivData(goi.interp);
3834 return target_configure(&goi, target);
3837 static int jim_target_mw(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
3839 const char *cmd_name = Jim_GetString(argv[0], NULL);
3841 Jim_GetOptInfo goi;
3842 Jim_GetOpt_Setup(&goi, interp, argc - 1, argv + 1);
3844 if (goi.argc != 2 && goi.argc != 3)
3846 Jim_SetResult_sprintf(goi.interp,
3847 "usage: %s <address> <data> [<count>]", cmd_name);
3848 return JIM_ERR;
3851 jim_wide a;
3852 int e = Jim_GetOpt_Wide(&goi, &a);
3853 if (e != JIM_OK)
3854 return e;
3856 jim_wide b;
3857 e = Jim_GetOpt_Wide(&goi, &b);
3858 if (e != JIM_OK)
3859 return e;
3861 jim_wide c = 1;
3862 if (goi.argc == 3)
3864 e = Jim_GetOpt_Wide(&goi, &c);
3865 if (e != JIM_OK)
3866 return e;
3869 struct target *target = Jim_CmdPrivData(goi.interp);
3870 uint8_t target_buf[32];
3871 if (strcasecmp(cmd_name, "mww") == 0) {
3872 target_buffer_set_u32(target, target_buf, b);
3873 b = 4;
3875 else if (strcasecmp(cmd_name, "mwh") == 0) {
3876 target_buffer_set_u16(target, target_buf, b);
3877 b = 2;
3879 else if (strcasecmp(cmd_name, "mwb") == 0) {
3880 target_buffer_set_u8(target, target_buf, b);
3881 b = 1;
3882 } else {
3883 LOG_ERROR("command '%s' unknown: ", cmd_name);
3884 return JIM_ERR;
3887 for (jim_wide x = 0; x < c; x++)
3889 e = target_write_memory(target, a, b, 1, target_buf);
3890 if (e != ERROR_OK)
3892 Jim_SetResult_sprintf(interp,
3893 "Error writing @ 0x%08x: %d\n", (int)(a), e);
3894 return JIM_ERR;
3896 /* b = width */
3897 a = a + b;
3899 return JIM_OK;
3902 static int jim_target_md(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
3904 const char *cmd_name = Jim_GetString(argv[0], NULL);
3906 Jim_GetOptInfo goi;
3907 Jim_GetOpt_Setup(&goi, interp, argc - 1, argv + 1);
3909 if ((goi.argc == 2) || (goi.argc == 3))
3911 Jim_SetResult_sprintf(goi.interp,
3912 "usage: %s <address> [<count>]", cmd_name);
3913 return JIM_ERR;
3916 jim_wide a;
3917 int e = Jim_GetOpt_Wide(&goi, &a);
3918 if (e != JIM_OK) {
3919 return JIM_ERR;
3921 jim_wide c;
3922 if (goi.argc) {
3923 e = Jim_GetOpt_Wide(&goi, &c);
3924 if (e != JIM_OK) {
3925 return JIM_ERR;
3927 } else {
3928 c = 1;
3930 jim_wide b = 1; /* shut up gcc */
3931 if (strcasecmp(cmd_name, "mdw") == 0)
3932 b = 4;
3933 else if (strcasecmp(cmd_name, "mdh") == 0)
3934 b = 2;
3935 else if (strcasecmp(cmd_name, "mdb") == 0)
3936 b = 1;
3937 else {
3938 LOG_ERROR("command '%s' unknown: ", cmd_name);
3939 return JIM_ERR;
3942 /* convert count to "bytes" */
3943 c = c * b;
3945 struct target *target = Jim_CmdPrivData(goi.interp);
3946 uint8_t target_buf[32];
3947 jim_wide x, y, z;
3948 while (c > 0) {
3949 y = c;
3950 if (y > 16) {
3951 y = 16;
3953 e = target_read_memory(target, a, b, y / b, target_buf);
3954 if (e != ERROR_OK) {
3955 Jim_SetResult_sprintf(interp, "error reading target @ 0x%08lx", (int)(a));
3956 return JIM_ERR;
3959 Jim_fprintf(interp, interp->cookie_stdout, "0x%08x ", (int)(a));
3960 switch (b) {
3961 case 4:
3962 for (x = 0; x < 16 && x < y; x += 4)
3964 z = target_buffer_get_u32(target, &(target_buf[ x * 4 ]));
3965 Jim_fprintf(interp, interp->cookie_stdout, "%08x ", (int)(z));
3967 for (; (x < 16) ; x += 4) {
3968 Jim_fprintf(interp, interp->cookie_stdout, " ");
3970 break;
3971 case 2:
3972 for (x = 0; x < 16 && x < y; x += 2)
3974 z = target_buffer_get_u16(target, &(target_buf[ x * 2 ]));
3975 Jim_fprintf(interp, interp->cookie_stdout, "%04x ", (int)(z));
3977 for (; (x < 16) ; x += 2) {
3978 Jim_fprintf(interp, interp->cookie_stdout, " ");
3980 break;
3981 case 1:
3982 default:
3983 for (x = 0 ; (x < 16) && (x < y) ; x += 1) {
3984 z = target_buffer_get_u8(target, &(target_buf[ x * 4 ]));
3985 Jim_fprintf(interp, interp->cookie_stdout, "%02x ", (int)(z));
3987 for (; (x < 16) ; x += 1) {
3988 Jim_fprintf(interp, interp->cookie_stdout, " ");
3990 break;
3992 /* ascii-ify the bytes */
3993 for (x = 0 ; x < y ; x++) {
3994 if ((target_buf[x] >= 0x20) &&
3995 (target_buf[x] <= 0x7e)) {
3996 /* good */
3997 } else {
3998 /* smack it */
3999 target_buf[x] = '.';
4002 /* space pad */
4003 while (x < 16) {
4004 target_buf[x] = ' ';
4005 x++;
4007 /* terminate */
4008 target_buf[16] = 0;
4009 /* print - with a newline */
4010 Jim_fprintf(interp, interp->cookie_stdout, "%s\n", target_buf);
4011 /* NEXT... */
4012 c -= 16;
4013 a += 16;
4015 return JIM_OK;
4018 static int jim_target_mem2array(Jim_Interp *interp,
4019 int argc, Jim_Obj *const *argv)
4021 struct target *target = Jim_CmdPrivData(interp);
4022 return target_mem2array(interp, target, argc - 1, argv + 1);
4025 static int jim_target_array2mem(Jim_Interp *interp,
4026 int argc, Jim_Obj *const *argv)
4028 struct target *target = Jim_CmdPrivData(interp);
4029 return target_array2mem(interp, target, argc - 1, argv + 1);
4032 static int jim_target_tap_disabled(Jim_Interp *interp)
4034 Jim_SetResult_sprintf(interp, "[TAP is disabled]");
4035 return JIM_ERR;
4038 static int jim_target_examine(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
4040 if (argc != 1)
4042 Jim_WrongNumArgs(interp, 1, argv, "[no parameters]");
4043 return JIM_ERR;
4045 struct target *target = Jim_CmdPrivData(interp);
4046 if (!target->tap->enabled)
4047 return jim_target_tap_disabled(interp);
4049 int e = target->type->examine(target);
4050 if (e != ERROR_OK)
4052 Jim_SetResult_sprintf(interp, "examine-fails: %d", e);
4053 return JIM_ERR;
4055 return JIM_OK;
4058 static int jim_target_poll(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
4060 if (argc != 1)
4062 Jim_WrongNumArgs(interp, 1, argv, "[no parameters]");
4063 return JIM_ERR;
4065 struct target *target = Jim_CmdPrivData(interp);
4066 if (!target->tap->enabled)
4067 return jim_target_tap_disabled(interp);
4069 int e;
4070 if (!(target_was_examined(target))) {
4071 e = ERROR_TARGET_NOT_EXAMINED;
4072 } else {
4073 e = target->type->poll(target);
4075 if (e != ERROR_OK)
4077 Jim_SetResult_sprintf(interp, "poll-fails: %d", e);
4078 return JIM_ERR;
4080 return JIM_OK;
4083 static int jim_target_reset(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
4085 Jim_GetOptInfo goi;
4086 Jim_GetOpt_Setup(&goi, interp, argc - 1, argv + 1);
4088 if (goi.argc != 2)
4090 Jim_WrongNumArgs(interp, 0, argv,
4091 "([tT]|[fF]|assert|deassert) BOOL");
4092 return JIM_ERR;
4095 Jim_Nvp *n;
4096 int e = Jim_GetOpt_Nvp(&goi, nvp_assert, &n);
4097 if (e != JIM_OK)
4099 Jim_GetOpt_NvpUnknown(&goi, nvp_assert, 1);
4100 return e;
4102 /* the halt or not param */
4103 jim_wide a;
4104 e = Jim_GetOpt_Wide(&goi, &a);
4105 if (e != JIM_OK)
4106 return e;
4108 struct target *target = Jim_CmdPrivData(goi.interp);
4109 if (!target->tap->enabled)
4110 return jim_target_tap_disabled(interp);
4111 if (!(target_was_examined(target)))
4113 LOG_ERROR("Target not examined yet");
4114 return ERROR_TARGET_NOT_EXAMINED;
4116 if (!target->type->assert_reset || !target->type->deassert_reset)
4118 Jim_SetResult_sprintf(interp,
4119 "No target-specific reset for %s",
4120 target_name(target));
4121 return JIM_ERR;
4123 /* determine if we should halt or not. */
4124 target->reset_halt = !!a;
4125 /* When this happens - all workareas are invalid. */
4126 target_free_all_working_areas_restore(target, 0);
4128 /* do the assert */
4129 if (n->value == NVP_ASSERT) {
4130 e = target->type->assert_reset(target);
4131 } else {
4132 e = target->type->deassert_reset(target);
4134 return (e == ERROR_OK) ? JIM_OK : JIM_ERR;
4137 static int jim_target_halt(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
4139 if (argc != 1) {
4140 Jim_WrongNumArgs(interp, 1, argv, "[no parameters]");
4141 return JIM_ERR;
4143 struct target *target = Jim_CmdPrivData(interp);
4144 if (!target->tap->enabled)
4145 return jim_target_tap_disabled(interp);
4146 int e = target->type->halt(target);
4147 return (e == ERROR_OK) ? JIM_OK : JIM_ERR;
4150 static int jim_target_wait_state(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
4152 Jim_GetOptInfo goi;
4153 Jim_GetOpt_Setup(&goi, interp, argc - 1, argv + 1);
4155 /* params: <name> statename timeoutmsecs */
4156 if (goi.argc != 2)
4158 const char *cmd_name = Jim_GetString(argv[0], NULL);
4159 Jim_SetResult_sprintf(goi.interp,
4160 "%s <state_name> <timeout_in_msec>", cmd_name);
4161 return JIM_ERR;
4164 Jim_Nvp *n;
4165 int e = Jim_GetOpt_Nvp(&goi, nvp_target_state, &n);
4166 if (e != JIM_OK) {
4167 Jim_GetOpt_NvpUnknown(&goi, nvp_target_state,1);
4168 return e;
4170 jim_wide a;
4171 e = Jim_GetOpt_Wide(&goi, &a);
4172 if (e != JIM_OK) {
4173 return e;
4175 struct target *target = Jim_CmdPrivData(interp);
4176 if (!target->tap->enabled)
4177 return jim_target_tap_disabled(interp);
4179 e = target_wait_state(target, n->value, a);
4180 if (e != ERROR_OK)
4182 Jim_SetResult_sprintf(goi.interp,
4183 "target: %s wait %s fails (%d) %s",
4184 target_name(target), n->name,
4185 e, target_strerror_safe(e));
4186 return JIM_ERR;
4188 return JIM_OK;
4190 /* List for human, Events defined for this target.
4191 * scripts/programs should use 'name cget -event NAME'
4193 static int jim_target_event_list(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
4195 struct command_context *cmd_ctx = Jim_GetAssocData(interp, "context");
4196 struct target *target = Jim_CmdPrivData(interp);
4197 struct target_event_action *teap = target->event_action;
4198 command_print(cmd_ctx, "Event actions for target (%d) %s\n",
4199 target->target_number,
4200 target_name(target));
4201 command_print(cmd_ctx, "%-25s | Body", "Event");
4202 command_print(cmd_ctx, "------------------------- | "
4203 "----------------------------------------");
4204 while (teap)
4206 Jim_Nvp *opt = Jim_Nvp_value2name_simple(nvp_target_event, teap->event);
4207 command_print(cmd_ctx, "%-25s | %s",
4208 opt->name, Jim_GetString(teap->body, NULL));
4209 teap = teap->next;
4211 command_print(cmd_ctx, "***END***");
4212 return JIM_OK;
4214 static int jim_target_current_state(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
4216 if (argc != 1)
4218 Jim_WrongNumArgs(interp, 1, argv, "[no parameters]");
4219 return JIM_ERR;
4221 struct target *target = Jim_CmdPrivData(interp);
4222 Jim_SetResultString(interp, target_state_name(target), -1);
4223 return JIM_OK;
4225 static int jim_target_invoke_event(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
4227 Jim_GetOptInfo goi;
4228 Jim_GetOpt_Setup(&goi, interp, argc - 1, argv + 1);
4229 if (goi.argc != 1)
4231 const char *cmd_name = Jim_GetString(argv[0], NULL);
4232 Jim_SetResult_sprintf(goi.interp, "%s <eventname>", cmd_name);
4233 return JIM_ERR;
4235 Jim_Nvp *n;
4236 int e = Jim_GetOpt_Nvp(&goi, nvp_target_event, &n);
4237 if (e != JIM_OK)
4239 Jim_GetOpt_NvpUnknown(&goi, nvp_target_event, 1);
4240 return e;
4242 struct target *target = Jim_CmdPrivData(interp);
4243 target_handle_event(target, n->value);
4244 return JIM_OK;
4247 static const struct command_registration target_instance_command_handlers[] = {
4249 .name = "configure",
4250 .mode = COMMAND_CONFIG,
4251 .jim_handler = jim_target_configure,
4252 .help = "configure a new target for use",
4253 .usage = "[target_attribute ...]",
4256 .name = "cget",
4257 .mode = COMMAND_ANY,
4258 .jim_handler = jim_target_configure,
4259 .help = "returns the specified target attribute",
4260 .usage = "target_attribute",
4263 .name = "mww",
4264 .mode = COMMAND_EXEC,
4265 .jim_handler = jim_target_mw,
4266 .help = "Write 32-bit word(s) to target memory",
4267 .usage = "address data [count]",
4270 .name = "mwh",
4271 .mode = COMMAND_EXEC,
4272 .jim_handler = jim_target_mw,
4273 .help = "Write 16-bit half-word(s) to target memory",
4274 .usage = "address data [count]",
4277 .name = "mwb",
4278 .mode = COMMAND_EXEC,
4279 .jim_handler = jim_target_mw,
4280 .help = "Write byte(s) to target memory",
4281 .usage = "address data [count]",
4284 .name = "mdw",
4285 .mode = COMMAND_EXEC,
4286 .jim_handler = jim_target_md,
4287 .help = "Display target memory as 32-bit words",
4288 .usage = "address [count]",
4291 .name = "mdh",
4292 .mode = COMMAND_EXEC,
4293 .jim_handler = jim_target_md,
4294 .help = "Display target memory as 16-bit half-words",
4295 .usage = "address [count]",
4298 .name = "mdb",
4299 .mode = COMMAND_EXEC,
4300 .jim_handler = jim_target_md,
4301 .help = "Display target memory as 8-bit bytes",
4302 .usage = "address [count]",
4305 .name = "array2mem",
4306 .mode = COMMAND_EXEC,
4307 .jim_handler = jim_target_array2mem,
4308 .help = "Writes Tcl array of 8/16/32 bit numbers "
4309 "to target memory",
4310 .usage = "arrayname bitwidth address count",
4313 .name = "mem2array",
4314 .mode = COMMAND_EXEC,
4315 .jim_handler = jim_target_mem2array,
4316 .help = "Loads Tcl array of 8/16/32 bit numbers "
4317 "from target memory",
4318 .usage = "arrayname bitwidth address count",
4321 .name = "eventlist",
4322 .mode = COMMAND_EXEC,
4323 .jim_handler = jim_target_event_list,
4324 .help = "displays a table of events defined for this target",
4327 .name = "curstate",
4328 .mode = COMMAND_EXEC,
4329 .jim_handler = jim_target_current_state,
4330 .help = "displays the current state of this target",
4333 .name = "arp_examine",
4334 .mode = COMMAND_EXEC,
4335 .jim_handler = jim_target_examine,
4336 .help = "used internally for reset processing",
4339 .name = "arp_poll",
4340 .mode = COMMAND_EXEC,
4341 .jim_handler = jim_target_poll,
4342 .help = "used internally for reset processing",
4345 .name = "arp_reset",
4346 .mode = COMMAND_EXEC,
4347 .jim_handler = jim_target_reset,
4348 .help = "used internally for reset processing",
4351 .name = "arp_halt",
4352 .mode = COMMAND_EXEC,
4353 .jim_handler = jim_target_halt,
4354 .help = "used internally for reset processing",
4357 .name = "arp_waitstate",
4358 .mode = COMMAND_EXEC,
4359 .jim_handler = jim_target_wait_state,
4360 .help = "used internally for reset processing",
4363 .name = "invoke-event",
4364 .mode = COMMAND_EXEC,
4365 .jim_handler = jim_target_invoke_event,
4366 .help = "invoke handler for specified event",
4367 .usage = "event_name",
4369 COMMAND_REGISTRATION_DONE
4372 static int target_create(Jim_GetOptInfo *goi)
4374 Jim_Obj *new_cmd;
4375 Jim_Cmd *cmd;
4376 const char *cp;
4377 char *cp2;
4378 int e;
4379 int x;
4380 struct target *target;
4381 struct command_context *cmd_ctx;
4383 cmd_ctx = Jim_GetAssocData(goi->interp, "context");
4384 if (goi->argc < 3) {
4385 Jim_WrongNumArgs(goi->interp, 1, goi->argv, "?name? ?type? ..options...");
4386 return JIM_ERR;
4389 /* COMMAND */
4390 Jim_GetOpt_Obj(goi, &new_cmd);
4391 /* does this command exist? */
4392 cmd = Jim_GetCommand(goi->interp, new_cmd, JIM_ERRMSG);
4393 if (cmd) {
4394 cp = Jim_GetString(new_cmd, NULL);
4395 Jim_SetResult_sprintf(goi->interp, "Command/target: %s Exists", cp);
4396 return JIM_ERR;
4399 /* TYPE */
4400 e = Jim_GetOpt_String(goi, &cp2, NULL);
4401 cp = cp2;
4402 /* now does target type exist */
4403 for (x = 0 ; target_types[x] ; x++) {
4404 if (0 == strcmp(cp, target_types[x]->name)) {
4405 /* found */
4406 break;
4409 if (target_types[x] == NULL) {
4410 Jim_SetResult_sprintf(goi->interp, "Unknown target type %s, try one of ", cp);
4411 for (x = 0 ; target_types[x] ; x++) {
4412 if (target_types[x + 1]) {
4413 Jim_AppendStrings(goi->interp,
4414 Jim_GetResult(goi->interp),
4415 target_types[x]->name,
4416 ", ", NULL);
4417 } else {
4418 Jim_AppendStrings(goi->interp,
4419 Jim_GetResult(goi->interp),
4420 " or ",
4421 target_types[x]->name,NULL);
4424 return JIM_ERR;
4427 /* Create it */
4428 target = calloc(1,sizeof(struct target));
4429 /* set target number */
4430 target->target_number = new_target_number();
4432 /* allocate memory for each unique target type */
4433 target->type = (struct target_type*)calloc(1,sizeof(struct target_type));
4435 memcpy(target->type, target_types[x], sizeof(struct target_type));
4437 /* will be set by "-endian" */
4438 target->endianness = TARGET_ENDIAN_UNKNOWN;
4440 target->working_area = 0x0;
4441 target->working_area_size = 0x0;
4442 target->working_areas = NULL;
4443 target->backup_working_area = 0;
4445 target->state = TARGET_UNKNOWN;
4446 target->debug_reason = DBG_REASON_UNDEFINED;
4447 target->reg_cache = NULL;
4448 target->breakpoints = NULL;
4449 target->watchpoints = NULL;
4450 target->next = NULL;
4451 target->arch_info = NULL;
4453 target->display = 1;
4455 target->halt_issued = false;
4457 /* initialize trace information */
4458 target->trace_info = malloc(sizeof(struct trace));
4459 target->trace_info->num_trace_points = 0;
4460 target->trace_info->trace_points_size = 0;
4461 target->trace_info->trace_points = NULL;
4462 target->trace_info->trace_history_size = 0;
4463 target->trace_info->trace_history = NULL;
4464 target->trace_info->trace_history_pos = 0;
4465 target->trace_info->trace_history_overflowed = 0;
4467 target->dbgmsg = NULL;
4468 target->dbg_msg_enabled = 0;
4470 target->endianness = TARGET_ENDIAN_UNKNOWN;
4472 /* Do the rest as "configure" options */
4473 goi->isconfigure = 1;
4474 e = target_configure(goi, target);
4476 if (target->tap == NULL)
4478 Jim_SetResultString(goi->interp, "-chain-position required when creating target", -1);
4479 e = JIM_ERR;
4482 if (e != JIM_OK) {
4483 free(target->type);
4484 free(target);
4485 return e;
4488 if (target->endianness == TARGET_ENDIAN_UNKNOWN) {
4489 /* default endian to little if not specified */
4490 target->endianness = TARGET_LITTLE_ENDIAN;
4493 /* incase variant is not set */
4494 if (!target->variant)
4495 target->variant = strdup("");
4497 cp = Jim_GetString(new_cmd, NULL);
4498 target->cmd_name = strdup(cp);
4500 /* create the target specific commands */
4501 if (target->type->commands) {
4502 e = register_commands(cmd_ctx, NULL, target->type->commands);
4503 if (ERROR_OK != e)
4504 LOG_ERROR("unable to register '%s' commands", cp);
4506 if (target->type->target_create) {
4507 (*(target->type->target_create))(target, goi->interp);
4510 /* append to end of list */
4512 struct target **tpp;
4513 tpp = &(all_targets);
4514 while (*tpp) {
4515 tpp = &((*tpp)->next);
4517 *tpp = target;
4520 /* now - create the new target name command */
4521 const const struct command_registration target_subcommands[] = {
4523 .chain = target_instance_command_handlers,
4526 .chain = target->type->commands,
4528 COMMAND_REGISTRATION_DONE
4530 const const struct command_registration target_commands[] = {
4532 .name = cp,
4533 .mode = COMMAND_ANY,
4534 .help = "target command group",
4535 .chain = target_subcommands,
4537 COMMAND_REGISTRATION_DONE
4539 e = register_commands(cmd_ctx, NULL, target_commands);
4540 if (ERROR_OK != e)
4541 return JIM_ERR;
4543 struct command *c = command_find_in_context(cmd_ctx, cp);
4544 assert(c);
4545 command_set_handler_data(c, target);
4547 return (ERROR_OK == e) ? JIM_OK : JIM_ERR;
4550 static int jim_target_current(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
4552 if (argc != 1)
4554 Jim_WrongNumArgs(interp, 1, argv, "Too many parameters");
4555 return JIM_ERR;
4557 struct command_context *cmd_ctx = Jim_GetAssocData(interp, "context");
4558 Jim_SetResultString(interp, get_current_target(cmd_ctx)->cmd_name, -1);
4559 return JIM_OK;
4562 static int jim_target_types(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
4564 if (argc != 1)
4566 Jim_WrongNumArgs(interp, 1, argv, "Too many parameters");
4567 return JIM_ERR;
4569 Jim_SetResult(interp, Jim_NewListObj(interp, NULL, 0));
4570 for (unsigned x = 0; NULL != target_types[x]; x++)
4572 Jim_ListAppendElement(interp, Jim_GetResult(interp),
4573 Jim_NewStringObj(interp, target_types[x]->name, -1));
4575 return JIM_OK;
4578 static int jim_target_names(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
4580 if (argc != 1)
4582 Jim_WrongNumArgs(interp, 1, argv, "Too many parameters");
4583 return JIM_ERR;
4585 Jim_SetResult(interp, Jim_NewListObj(interp, NULL, 0));
4586 struct target *target = all_targets;
4587 while (target)
4589 Jim_ListAppendElement(interp, Jim_GetResult(interp),
4590 Jim_NewStringObj(interp, target_name(target), -1));
4591 target = target->next;
4593 return JIM_OK;
4596 static int jim_target_create(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
4598 Jim_GetOptInfo goi;
4599 Jim_GetOpt_Setup(&goi, interp, argc - 1, argv + 1);
4600 if (goi.argc < 3)
4602 Jim_WrongNumArgs(goi.interp, goi.argc, goi.argv,
4603 "<name> <target_type> [<target_options> ...]");
4604 return JIM_ERR;
4606 return target_create(&goi);
4609 static int jim_target_number(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
4611 Jim_GetOptInfo goi;
4612 Jim_GetOpt_Setup(&goi, interp, argc - 1, argv + 1);
4614 /* It's OK to remove this mechanism sometime after August 2010 or so */
4615 LOG_WARNING("don't use numbers as target identifiers; use names");
4616 if (goi.argc != 1)
4618 Jim_SetResult_sprintf(goi.interp, "usage: target number <number>");
4619 return JIM_ERR;
4621 jim_wide w;
4622 int e = Jim_GetOpt_Wide(&goi, &w);
4623 if (e != JIM_OK)
4624 return JIM_ERR;
4626 struct target *target;
4627 for (target = all_targets; NULL != target; target = target->next)
4629 if (target->target_number != w)
4630 continue;
4632 Jim_SetResultString(goi.interp, target_name(target), -1);
4633 return JIM_OK;
4635 Jim_SetResult_sprintf(goi.interp,
4636 "Target: number %d does not exist", (int)(w));
4637 return JIM_ERR;
4640 static int jim_target_count(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
4642 if (argc != 1)
4644 Jim_WrongNumArgs(interp, 1, argv, "<no parameters>");
4645 return JIM_ERR;
4647 unsigned count = 0;
4648 struct target *target = all_targets;
4649 while (NULL != target)
4651 target = target->next;
4652 count++;
4654 Jim_SetResult(interp, Jim_NewIntObj(interp, count));
4655 return JIM_OK;
4658 static const struct command_registration target_subcommand_handlers[] = {
4660 .name = "init",
4661 .mode = COMMAND_CONFIG,
4662 .handler = handle_target_init_command,
4663 .help = "initialize targets",
4666 .name = "create",
4667 /* REVISIT this should be COMMAND_CONFIG ... */
4668 .mode = COMMAND_ANY,
4669 .jim_handler = jim_target_create,
4670 .usage = "name type '-chain-position' name [options ...]",
4671 .help = "Creates and selects a new target",
4674 .name = "current",
4675 .mode = COMMAND_ANY,
4676 .jim_handler = jim_target_current,
4677 .help = "Returns the currently selected target",
4680 .name = "types",
4681 .mode = COMMAND_ANY,
4682 .jim_handler = jim_target_types,
4683 .help = "Returns the available target types as "
4684 "a list of strings",
4687 .name = "names",
4688 .mode = COMMAND_ANY,
4689 .jim_handler = jim_target_names,
4690 .help = "Returns the names of all targets as a list of strings",
4693 .name = "number",
4694 .mode = COMMAND_ANY,
4695 .jim_handler = jim_target_number,
4696 .usage = "number",
4697 .help = "Returns the name of the numbered target "
4698 "(DEPRECATED)",
4701 .name = "count",
4702 .mode = COMMAND_ANY,
4703 .jim_handler = jim_target_count,
4704 .help = "Returns the number of targets as an integer "
4705 "(DEPRECATED)",
4707 COMMAND_REGISTRATION_DONE
4710 struct FastLoad
4712 uint32_t address;
4713 uint8_t *data;
4714 int length;
4718 static int fastload_num;
4719 static struct FastLoad *fastload;
4721 static void free_fastload(void)
4723 if (fastload != NULL)
4725 int i;
4726 for (i = 0; i < fastload_num; i++)
4728 if (fastload[i].data)
4729 free(fastload[i].data);
4731 free(fastload);
4732 fastload = NULL;
4739 COMMAND_HANDLER(handle_fast_load_image_command)
4741 uint8_t *buffer;
4742 size_t buf_cnt;
4743 uint32_t image_size;
4744 uint32_t min_address = 0;
4745 uint32_t max_address = 0xffffffff;
4746 int i;
4748 struct image image;
4750 int retval = CALL_COMMAND_HANDLER(parse_load_image_command_CMD_ARGV,
4751 &image, &min_address, &max_address);
4752 if (ERROR_OK != retval)
4753 return retval;
4755 struct duration bench;
4756 duration_start(&bench);
4758 if (image_open(&image, CMD_ARGV[0], (CMD_ARGC >= 3) ? CMD_ARGV[2] : NULL) != ERROR_OK)
4760 return ERROR_OK;
4763 image_size = 0x0;
4764 retval = ERROR_OK;
4765 fastload_num = image.num_sections;
4766 fastload = (struct FastLoad *)malloc(sizeof(struct FastLoad)*image.num_sections);
4767 if (fastload == NULL)
4769 image_close(&image);
4770 return ERROR_FAIL;
4772 memset(fastload, 0, sizeof(struct FastLoad)*image.num_sections);
4773 for (i = 0; i < image.num_sections; i++)
4775 buffer = malloc(image.sections[i].size);
4776 if (buffer == NULL)
4778 command_print(CMD_CTX, "error allocating buffer for section (%d bytes)",
4779 (int)(image.sections[i].size));
4780 break;
4783 if ((retval = image_read_section(&image, i, 0x0, image.sections[i].size, buffer, &buf_cnt)) != ERROR_OK)
4785 free(buffer);
4786 break;
4789 uint32_t offset = 0;
4790 uint32_t length = buf_cnt;
4793 /* DANGER!!! beware of unsigned comparision here!!! */
4795 if ((image.sections[i].base_address + buf_cnt >= min_address)&&
4796 (image.sections[i].base_address < max_address))
4798 if (image.sections[i].base_address < min_address)
4800 /* clip addresses below */
4801 offset += min_address-image.sections[i].base_address;
4802 length -= offset;
4805 if (image.sections[i].base_address + buf_cnt > max_address)
4807 length -= (image.sections[i].base_address + buf_cnt)-max_address;
4810 fastload[i].address = image.sections[i].base_address + offset;
4811 fastload[i].data = malloc(length);
4812 if (fastload[i].data == NULL)
4814 free(buffer);
4815 break;
4817 memcpy(fastload[i].data, buffer + offset, length);
4818 fastload[i].length = length;
4820 image_size += length;
4821 command_print(CMD_CTX, "%u bytes written at address 0x%8.8x",
4822 (unsigned int)length,
4823 ((unsigned int)(image.sections[i].base_address + offset)));
4826 free(buffer);
4829 if ((ERROR_OK == retval) && (duration_measure(&bench) == ERROR_OK))
4831 command_print(CMD_CTX, "Loaded %" PRIu32 " bytes "
4832 "in %fs (%0.3f kb/s)", image_size,
4833 duration_elapsed(&bench), duration_kbps(&bench, image_size));
4835 command_print(CMD_CTX,
4836 "WARNING: image has not been loaded to target!"
4837 "You can issue a 'fast_load' to finish loading.");
4840 image_close(&image);
4842 if (retval != ERROR_OK)
4844 free_fastload();
4847 return retval;
4850 COMMAND_HANDLER(handle_fast_load_command)
4852 if (CMD_ARGC > 0)
4853 return ERROR_COMMAND_SYNTAX_ERROR;
4854 if (fastload == NULL)
4856 LOG_ERROR("No image in memory");
4857 return ERROR_FAIL;
4859 int i;
4860 int ms = timeval_ms();
4861 int size = 0;
4862 int retval = ERROR_OK;
4863 for (i = 0; i < fastload_num;i++)
4865 struct target *target = get_current_target(CMD_CTX);
4866 command_print(CMD_CTX, "Write to 0x%08x, length 0x%08x",
4867 (unsigned int)(fastload[i].address),
4868 (unsigned int)(fastload[i].length));
4869 if (retval == ERROR_OK)
4871 retval = target_write_buffer(target, fastload[i].address, fastload[i].length, fastload[i].data);
4873 size += fastload[i].length;
4875 int after = timeval_ms();
4876 command_print(CMD_CTX, "Loaded image %f kBytes/s", (float)(size/1024.0)/((float)(after-ms)/1000.0));
4877 return retval;
4880 static const struct command_registration target_command_handlers[] = {
4882 .name = "targets",
4883 .handler = handle_targets_command,
4884 .mode = COMMAND_ANY,
4885 .help = "change current default target (one parameter) "
4886 "or prints table of all targets (no parameters)",
4887 .usage = "[target]",
4890 .name = "target",
4891 .mode = COMMAND_CONFIG,
4892 .help = "configure target",
4894 .chain = target_subcommand_handlers,
4896 COMMAND_REGISTRATION_DONE
4899 int target_register_commands(struct command_context *cmd_ctx)
4901 return register_commands(cmd_ctx, NULL, target_command_handlers);
4904 static bool target_reset_nag = true;
4906 bool get_target_reset_nag(void)
4908 return target_reset_nag;
4911 COMMAND_HANDLER(handle_target_reset_nag)
4913 return CALL_COMMAND_HANDLER(handle_command_parse_bool,
4914 &target_reset_nag, "Nag after each reset about options to improve "
4915 "performance");
4918 static const struct command_registration target_exec_command_handlers[] = {
4920 .name = "fast_load_image",
4921 .handler = handle_fast_load_image_command,
4922 .mode = COMMAND_ANY,
4923 .help = "Load image into server memory for later use by "
4924 "fast_load; primarily for profiling",
4925 .usage = "filename address ['bin'|'ihex'|'elf'|'s19'] "
4926 "[min_address [max_length]]",
4929 .name = "fast_load",
4930 .handler = handle_fast_load_command,
4931 .mode = COMMAND_EXEC,
4932 .help = "loads active fast load image to current target "
4933 "- mainly for profiling purposes",
4936 .name = "profile",
4937 .handler = handle_profile_command,
4938 .mode = COMMAND_EXEC,
4939 .help = "profiling samples the CPU PC",
4941 /** @todo don't register virt2phys() unless target supports it */
4943 .name = "virt2phys",
4944 .handler = handle_virt2phys_command,
4945 .mode = COMMAND_ANY,
4946 .help = "translate a virtual address into a physical address",
4947 .usage = "virtual_address",
4950 .name = "reg",
4951 .handler = handle_reg_command,
4952 .mode = COMMAND_EXEC,
4953 .help = "display or set a register; with no arguments, "
4954 "displays all registers and their values",
4955 .usage = "[(register_name|register_number) [value]]",
4958 .name = "poll",
4959 .handler = handle_poll_command,
4960 .mode = COMMAND_EXEC,
4961 .help = "poll target state; or reconfigure background polling",
4962 .usage = "['on'|'off']",
4965 .name = "wait_halt",
4966 .handler = handle_wait_halt_command,
4967 .mode = COMMAND_EXEC,
4968 .help = "wait up to the specified number of milliseconds "
4969 "(default 5) for a previously requested halt",
4970 .usage = "[milliseconds]",
4973 .name = "halt",
4974 .handler = handle_halt_command,
4975 .mode = COMMAND_EXEC,
4976 .help = "request target to halt, then wait up to the specified"
4977 "number of milliseconds (default 5) for it to complete",
4978 .usage = "[milliseconds]",
4981 .name = "resume",
4982 .handler = handle_resume_command,
4983 .mode = COMMAND_EXEC,
4984 .help = "resume target execution from current PC or address",
4985 .usage = "[address]",
4988 .name = "reset",
4989 .handler = handle_reset_command,
4990 .mode = COMMAND_EXEC,
4991 .usage = "[run|halt|init]",
4992 .help = "Reset all targets into the specified mode."
4993 "Default reset mode is run, if not given.",
4996 .name = "soft_reset_halt",
4997 .handler = handle_soft_reset_halt_command,
4998 .mode = COMMAND_EXEC,
4999 .help = "halt the target and do a soft reset",
5002 .name = "step",
5003 .handler = handle_step_command,
5004 .mode = COMMAND_EXEC,
5005 .help = "step one instruction from current PC or address",
5006 .usage = "[address]",
5009 .name = "mdw",
5010 .handler = handle_md_command,
5011 .mode = COMMAND_EXEC,
5012 .help = "display memory words",
5013 .usage = "['phys'] address [count]",
5016 .name = "mdh",
5017 .handler = handle_md_command,
5018 .mode = COMMAND_EXEC,
5019 .help = "display memory half-words",
5020 .usage = "['phys'] address [count]",
5023 .name = "mdb",
5024 .handler = handle_md_command,
5025 .mode = COMMAND_EXEC,
5026 .help = "display memory bytes",
5027 .usage = "['phys'] address [count]",
5030 .name = "mww",
5031 .handler = handle_mw_command,
5032 .mode = COMMAND_EXEC,
5033 .help = "write memory word",
5034 .usage = "['phys'] address value [count]",
5037 .name = "mwh",
5038 .handler = handle_mw_command,
5039 .mode = COMMAND_EXEC,
5040 .help = "write memory half-word",
5041 .usage = "['phys'] address value [count]",
5044 .name = "mwb",
5045 .handler = handle_mw_command,
5046 .mode = COMMAND_EXEC,
5047 .help = "write memory byte",
5048 .usage = "['phys'] address value [count]",
5051 .name = "bp",
5052 .handler = handle_bp_command,
5053 .mode = COMMAND_EXEC,
5054 .help = "list or set hardware or software breakpoint",
5055 .usage = "[address length ['hw']]",
5058 .name = "rbp",
5059 .handler = handle_rbp_command,
5060 .mode = COMMAND_EXEC,
5061 .help = "remove breakpoint",
5062 .usage = "address",
5065 .name = "wp",
5066 .handler = handle_wp_command,
5067 .mode = COMMAND_EXEC,
5068 .help = "list (no params) or create watchpoints",
5069 .usage = "[address length [('r'|'w'|'a') value [mask]]]",
5072 .name = "rwp",
5073 .handler = handle_rwp_command,
5074 .mode = COMMAND_EXEC,
5075 .help = "remove watchpoint",
5076 .usage = "address",
5079 .name = "load_image",
5080 .handler = handle_load_image_command,
5081 .mode = COMMAND_EXEC,
5082 .usage = "filename address ['bin'|'ihex'|'elf'|'s19'] "
5083 "[min_address] [max_length]",
5086 .name = "dump_image",
5087 .handler = handle_dump_image_command,
5088 .mode = COMMAND_EXEC,
5089 .usage = "filename address size",
5092 .name = "verify_image",
5093 .handler = handle_verify_image_command,
5094 .mode = COMMAND_EXEC,
5095 .usage = "filename [offset [type]]",
5098 .name = "test_image",
5099 .handler = handle_test_image_command,
5100 .mode = COMMAND_EXEC,
5101 .usage = "filename [offset [type]]",
5104 .name = "ocd_mem2array",
5105 .mode = COMMAND_EXEC,
5106 .jim_handler = jim_mem2array,
5107 .help = "read 8/16/32 bit memory and return as a TCL array "
5108 "for script processing",
5109 .usage = "arrayname bitwidth address count",
5112 .name = "ocd_array2mem",
5113 .mode = COMMAND_EXEC,
5114 .jim_handler = jim_array2mem,
5115 .help = "convert a TCL array to memory locations "
5116 "and write the 8/16/32 bit values",
5117 .usage = "arrayname bitwidth address count",
5120 .name = "reset_nag",
5121 .handler = handle_target_reset_nag,
5122 .mode = COMMAND_ANY,
5123 .help = "Nag after each reset about options that could have been "
5124 "enabled to improve performance. ",
5125 .usage = "['enable'|'disable']",
5127 COMMAND_REGISTRATION_DONE
5129 int target_register_user_commands(struct command_context *cmd_ctx)
5131 int retval = ERROR_OK;
5132 if ((retval = target_request_register_commands(cmd_ctx)) != ERROR_OK)
5133 return retval;
5135 if ((retval = trace_register_commands(cmd_ctx)) != ERROR_OK)
5136 return retval;
5139 return register_commands(cmd_ctx, NULL, target_exec_command_handlers);