target: remove needless "extern"s
[openocd/jflash.git] / src / target / target.c
bloba9f4dd87462356087bb64047f042af4bccc40111
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 "target.h"
37 #include "target_type.h"
38 #include "target_request.h"
39 #include "breakpoints.h"
40 #include <helper/time_support.h>
41 #include "register.h"
42 #include "trace.h"
43 #include "image.h"
44 #include <jtag/jtag.h>
47 static int target_array2mem(Jim_Interp *interp, struct target *target, int argc, Jim_Obj *const *argv);
48 static int target_mem2array(Jim_Interp *interp, struct target *target, int argc, Jim_Obj *const *argv);
50 /* targets */
51 extern struct target_type arm7tdmi_target;
52 extern struct target_type arm720t_target;
53 extern struct target_type arm9tdmi_target;
54 extern struct target_type arm920t_target;
55 extern struct target_type arm966e_target;
56 extern struct target_type arm926ejs_target;
57 extern struct target_type fa526_target;
58 extern struct target_type feroceon_target;
59 extern struct target_type dragonite_target;
60 extern struct target_type xscale_target;
61 extern struct target_type cortexm3_target;
62 extern struct target_type cortexa8_target;
63 extern struct target_type arm11_target;
64 extern struct target_type mips_m4k_target;
65 extern struct target_type avr_target;
66 extern struct target_type testee_target;
68 struct target_type *target_types[] =
70 &arm7tdmi_target,
71 &arm9tdmi_target,
72 &arm920t_target,
73 &arm720t_target,
74 &arm966e_target,
75 &arm926ejs_target,
76 &fa526_target,
77 &feroceon_target,
78 &dragonite_target,
79 &xscale_target,
80 &cortexm3_target,
81 &cortexa8_target,
82 &arm11_target,
83 &mips_m4k_target,
84 &avr_target,
85 &testee_target,
86 NULL,
89 struct target *all_targets = NULL;
90 struct target_event_callback *target_event_callbacks = NULL;
91 struct target_timer_callback *target_timer_callbacks = NULL;
93 static const Jim_Nvp nvp_assert[] = {
94 { .name = "assert", NVP_ASSERT },
95 { .name = "deassert", NVP_DEASSERT },
96 { .name = "T", NVP_ASSERT },
97 { .name = "F", NVP_DEASSERT },
98 { .name = "t", NVP_ASSERT },
99 { .name = "f", NVP_DEASSERT },
100 { .name = NULL, .value = -1 }
103 static const Jim_Nvp nvp_error_target[] = {
104 { .value = ERROR_TARGET_INVALID, .name = "err-invalid" },
105 { .value = ERROR_TARGET_INIT_FAILED, .name = "err-init-failed" },
106 { .value = ERROR_TARGET_TIMEOUT, .name = "err-timeout" },
107 { .value = ERROR_TARGET_NOT_HALTED, .name = "err-not-halted" },
108 { .value = ERROR_TARGET_FAILURE, .name = "err-failure" },
109 { .value = ERROR_TARGET_UNALIGNED_ACCESS , .name = "err-unaligned-access" },
110 { .value = ERROR_TARGET_DATA_ABORT , .name = "err-data-abort" },
111 { .value = ERROR_TARGET_RESOURCE_NOT_AVAILABLE , .name = "err-resource-not-available" },
112 { .value = ERROR_TARGET_TRANSLATION_FAULT , .name = "err-translation-fault" },
113 { .value = ERROR_TARGET_NOT_RUNNING, .name = "err-not-running" },
114 { .value = ERROR_TARGET_NOT_EXAMINED, .name = "err-not-examined" },
115 { .value = -1, .name = NULL }
118 const char *target_strerror_safe(int err)
120 const Jim_Nvp *n;
122 n = Jim_Nvp_value2name_simple(nvp_error_target, err);
123 if (n->name == NULL) {
124 return "unknown";
125 } else {
126 return n->name;
130 static const Jim_Nvp nvp_target_event[] = {
131 { .value = TARGET_EVENT_OLD_gdb_program_config , .name = "old-gdb_program_config" },
132 { .value = TARGET_EVENT_OLD_pre_resume , .name = "old-pre_resume" },
134 { .value = TARGET_EVENT_GDB_HALT, .name = "gdb-halt" },
135 { .value = TARGET_EVENT_HALTED, .name = "halted" },
136 { .value = TARGET_EVENT_RESUMED, .name = "resumed" },
137 { .value = TARGET_EVENT_RESUME_START, .name = "resume-start" },
138 { .value = TARGET_EVENT_RESUME_END, .name = "resume-end" },
140 { .name = "gdb-start", .value = TARGET_EVENT_GDB_START },
141 { .name = "gdb-end", .value = TARGET_EVENT_GDB_END },
143 /* historical name */
145 { .value = TARGET_EVENT_RESET_START, .name = "reset-start" },
147 { .value = TARGET_EVENT_RESET_ASSERT_PRE, .name = "reset-assert-pre" },
148 { .value = TARGET_EVENT_RESET_ASSERT, .name = "reset-assert" },
149 { .value = TARGET_EVENT_RESET_ASSERT_POST, .name = "reset-assert-post" },
150 { .value = TARGET_EVENT_RESET_DEASSERT_PRE, .name = "reset-deassert-pre" },
151 { .value = TARGET_EVENT_RESET_DEASSERT_POST, .name = "reset-deassert-post" },
152 { .value = TARGET_EVENT_RESET_HALT_PRE, .name = "reset-halt-pre" },
153 { .value = TARGET_EVENT_RESET_HALT_POST, .name = "reset-halt-post" },
154 { .value = TARGET_EVENT_RESET_WAIT_PRE, .name = "reset-wait-pre" },
155 { .value = TARGET_EVENT_RESET_WAIT_POST, .name = "reset-wait-post" },
156 { .value = TARGET_EVENT_RESET_INIT, .name = "reset-init" },
157 { .value = TARGET_EVENT_RESET_END, .name = "reset-end" },
159 { .value = TARGET_EVENT_EXAMINE_START, .name = "examine-start" },
160 { .value = TARGET_EVENT_EXAMINE_END, .name = "examine-end" },
162 { .value = TARGET_EVENT_DEBUG_HALTED, .name = "debug-halted" },
163 { .value = TARGET_EVENT_DEBUG_RESUMED, .name = "debug-resumed" },
165 { .value = TARGET_EVENT_GDB_ATTACH, .name = "gdb-attach" },
166 { .value = TARGET_EVENT_GDB_DETACH, .name = "gdb-detach" },
168 { .value = TARGET_EVENT_GDB_FLASH_WRITE_START, .name = "gdb-flash-write-start" },
169 { .value = TARGET_EVENT_GDB_FLASH_WRITE_END , .name = "gdb-flash-write-end" },
171 { .value = TARGET_EVENT_GDB_FLASH_ERASE_START, .name = "gdb-flash-erase-start" },
172 { .value = TARGET_EVENT_GDB_FLASH_ERASE_END , .name = "gdb-flash-erase-end" },
174 { .value = TARGET_EVENT_RESUME_START, .name = "resume-start" },
175 { .value = TARGET_EVENT_RESUMED , .name = "resume-ok" },
176 { .value = TARGET_EVENT_RESUME_END , .name = "resume-end" },
178 { .name = NULL, .value = -1 }
181 static const Jim_Nvp nvp_target_state[] = {
182 { .name = "unknown", .value = TARGET_UNKNOWN },
183 { .name = "running", .value = TARGET_RUNNING },
184 { .name = "halted", .value = TARGET_HALTED },
185 { .name = "reset", .value = TARGET_RESET },
186 { .name = "debug-running", .value = TARGET_DEBUG_RUNNING },
187 { .name = NULL, .value = -1 },
190 static const Jim_Nvp nvp_target_debug_reason [] = {
191 { .name = "debug-request" , .value = DBG_REASON_DBGRQ },
192 { .name = "breakpoint" , .value = DBG_REASON_BREAKPOINT },
193 { .name = "watchpoint" , .value = DBG_REASON_WATCHPOINT },
194 { .name = "watchpoint-and-breakpoint", .value = DBG_REASON_WPTANDBKPT },
195 { .name = "single-step" , .value = DBG_REASON_SINGLESTEP },
196 { .name = "target-not-halted" , .value = DBG_REASON_NOTHALTED },
197 { .name = "undefined" , .value = DBG_REASON_UNDEFINED },
198 { .name = NULL, .value = -1 },
201 static const Jim_Nvp nvp_target_endian[] = {
202 { .name = "big", .value = TARGET_BIG_ENDIAN },
203 { .name = "little", .value = TARGET_LITTLE_ENDIAN },
204 { .name = "be", .value = TARGET_BIG_ENDIAN },
205 { .name = "le", .value = TARGET_LITTLE_ENDIAN },
206 { .name = NULL, .value = -1 },
209 static const Jim_Nvp nvp_reset_modes[] = {
210 { .name = "unknown", .value = RESET_UNKNOWN },
211 { .name = "run" , .value = RESET_RUN },
212 { .name = "halt" , .value = RESET_HALT },
213 { .name = "init" , .value = RESET_INIT },
214 { .name = NULL , .value = -1 },
217 const char *debug_reason_name(struct target *t)
219 const char *cp;
221 cp = Jim_Nvp_value2name_simple(nvp_target_debug_reason,
222 t->debug_reason)->name;
223 if (!cp) {
224 LOG_ERROR("Invalid debug reason: %d", (int)(t->debug_reason));
225 cp = "(*BUG*unknown*BUG*)";
227 return cp;
230 const char *
231 target_state_name( struct target *t )
233 const char *cp;
234 cp = Jim_Nvp_value2name_simple(nvp_target_state, t->state)->name;
235 if( !cp ){
236 LOG_ERROR("Invalid target state: %d", (int)(t->state));
237 cp = "(*BUG*unknown*BUG*)";
239 return cp;
242 /* determine the number of the new target */
243 static int new_target_number(void)
245 struct target *t;
246 int x;
248 /* number is 0 based */
249 x = -1;
250 t = all_targets;
251 while (t) {
252 if (x < t->target_number) {
253 x = t->target_number;
255 t = t->next;
257 return x + 1;
260 /* read a uint32_t from a buffer in target memory endianness */
261 uint32_t target_buffer_get_u32(struct target *target, const uint8_t *buffer)
263 if (target->endianness == TARGET_LITTLE_ENDIAN)
264 return le_to_h_u32(buffer);
265 else
266 return be_to_h_u32(buffer);
269 /* read a uint16_t from a buffer in target memory endianness */
270 uint16_t target_buffer_get_u16(struct target *target, const uint8_t *buffer)
272 if (target->endianness == TARGET_LITTLE_ENDIAN)
273 return le_to_h_u16(buffer);
274 else
275 return be_to_h_u16(buffer);
278 /* read a uint8_t from a buffer in target memory endianness */
279 uint8_t target_buffer_get_u8(struct target *target, const uint8_t *buffer)
281 return *buffer & 0x0ff;
284 /* write a uint32_t to a buffer in target memory endianness */
285 void target_buffer_set_u32(struct target *target, uint8_t *buffer, uint32_t value)
287 if (target->endianness == TARGET_LITTLE_ENDIAN)
288 h_u32_to_le(buffer, value);
289 else
290 h_u32_to_be(buffer, value);
293 /* write a uint16_t to a buffer in target memory endianness */
294 void target_buffer_set_u16(struct target *target, uint8_t *buffer, uint16_t value)
296 if (target->endianness == TARGET_LITTLE_ENDIAN)
297 h_u16_to_le(buffer, value);
298 else
299 h_u16_to_be(buffer, value);
302 /* write a uint8_t to a buffer in target memory endianness */
303 void target_buffer_set_u8(struct target *target, uint8_t *buffer, uint8_t value)
305 *buffer = value;
308 /* return a pointer to a configured target; id is name or number */
309 struct target *get_target(const char *id)
311 struct target *target;
313 /* try as tcltarget name */
314 for (target = all_targets; target; target = target->next) {
315 if (target->cmd_name == NULL)
316 continue;
317 if (strcmp(id, target->cmd_name) == 0)
318 return target;
321 /* It's OK to remove this fallback sometime after August 2010 or so */
323 /* no match, try as number */
324 unsigned num;
325 if (parse_uint(id, &num) != ERROR_OK)
326 return NULL;
328 for (target = all_targets; target; target = target->next) {
329 if (target->target_number == (int)num) {
330 LOG_WARNING("use '%s' as target identifier, not '%u'",
331 target->cmd_name, num);
332 return target;
336 return NULL;
339 /* returns a pointer to the n-th configured target */
340 static struct target *get_target_by_num(int num)
342 struct target *target = all_targets;
344 while (target) {
345 if (target->target_number == num) {
346 return target;
348 target = target->next;
351 return NULL;
354 struct target* get_current_target(struct command_context *cmd_ctx)
356 struct target *target = get_target_by_num(cmd_ctx->current_target);
358 if (target == NULL)
360 LOG_ERROR("BUG: current_target out of bounds");
361 exit(-1);
364 return target;
367 int target_poll(struct target *target)
369 int retval;
371 /* We can't poll until after examine */
372 if (!target_was_examined(target))
374 /* Fail silently lest we pollute the log */
375 return ERROR_FAIL;
378 retval = target->type->poll(target);
379 if (retval != ERROR_OK)
380 return retval;
382 if (target->halt_issued)
384 if (target->state == TARGET_HALTED)
386 target->halt_issued = false;
387 } else
389 long long t = timeval_ms() - target->halt_issued_time;
390 if (t>1000)
392 target->halt_issued = false;
393 LOG_INFO("Halt timed out, wake up GDB.");
394 target_call_event_callbacks(target, TARGET_EVENT_GDB_HALT);
399 return ERROR_OK;
402 int target_halt(struct target *target)
404 int retval;
405 /* We can't poll until after examine */
406 if (!target_was_examined(target))
408 LOG_ERROR("Target not examined yet");
409 return ERROR_FAIL;
412 retval = target->type->halt(target);
413 if (retval != ERROR_OK)
414 return retval;
416 target->halt_issued = true;
417 target->halt_issued_time = timeval_ms();
419 return ERROR_OK;
422 int target_resume(struct target *target, int current, uint32_t address, int handle_breakpoints, int debug_execution)
424 int retval;
426 /* We can't poll until after examine */
427 if (!target_was_examined(target))
429 LOG_ERROR("Target not examined yet");
430 return ERROR_FAIL;
433 /* note that resume *must* be asynchronous. The CPU can halt before we poll. The CPU can
434 * even halt at the current PC as a result of a software breakpoint being inserted by (a bug?)
435 * the application.
437 if ((retval = target->type->resume(target, current, address, handle_breakpoints, debug_execution)) != ERROR_OK)
438 return retval;
440 return retval;
443 int target_process_reset(struct command_context *cmd_ctx, enum target_reset_mode reset_mode)
445 char buf[100];
446 int retval;
447 Jim_Nvp *n;
448 n = Jim_Nvp_value2name_simple(nvp_reset_modes, reset_mode);
449 if (n->name == NULL) {
450 LOG_ERROR("invalid reset mode");
451 return ERROR_FAIL;
454 /* disable polling during reset to make reset event scripts
455 * more predictable, i.e. dr/irscan & pathmove in events will
456 * not have JTAG operations injected into the middle of a sequence.
458 bool save_poll = jtag_poll_get_enabled();
460 jtag_poll_set_enabled(false);
462 sprintf(buf, "ocd_process_reset %s", n->name);
463 retval = Jim_Eval(cmd_ctx->interp, buf);
465 jtag_poll_set_enabled(save_poll);
467 if (retval != JIM_OK) {
468 Jim_PrintErrorMessage(cmd_ctx->interp);
469 return ERROR_FAIL;
472 /* We want any events to be processed before the prompt */
473 retval = target_call_timer_callbacks_now();
475 return retval;
478 static int identity_virt2phys(struct target *target,
479 uint32_t virtual, uint32_t *physical)
481 *physical = virtual;
482 return ERROR_OK;
485 static int no_mmu(struct target *target, int *enabled)
487 *enabled = 0;
488 return ERROR_OK;
491 static int default_examine(struct target *target)
493 target_set_examined(target);
494 return ERROR_OK;
497 int target_examine_one(struct target *target)
499 return target->type->examine(target);
502 static int jtag_enable_callback(enum jtag_event event, void *priv)
504 struct target *target = priv;
506 if (event != JTAG_TAP_EVENT_ENABLE || !target->tap->enabled)
507 return ERROR_OK;
509 jtag_unregister_event_callback(jtag_enable_callback, target);
510 return target_examine_one(target);
514 /* Targets that correctly implement init + examine, i.e.
515 * no communication with target during init:
517 * XScale
519 int target_examine(void)
521 int retval = ERROR_OK;
522 struct target *target;
524 for (target = all_targets; target; target = target->next)
526 /* defer examination, but don't skip it */
527 if (!target->tap->enabled) {
528 jtag_register_event_callback(jtag_enable_callback,
529 target);
530 continue;
532 if ((retval = target_examine_one(target)) != ERROR_OK)
533 return retval;
535 return retval;
537 const char *target_type_name(struct target *target)
539 return target->type->name;
542 static int target_write_memory_imp(struct target *target, uint32_t address, uint32_t size, uint32_t count, uint8_t *buffer)
544 if (!target_was_examined(target))
546 LOG_ERROR("Target not examined yet");
547 return ERROR_FAIL;
549 return target->type->write_memory_imp(target, address, size, count, buffer);
552 static int target_read_memory_imp(struct target *target, uint32_t address, uint32_t size, uint32_t count, uint8_t *buffer)
554 if (!target_was_examined(target))
556 LOG_ERROR("Target not examined yet");
557 return ERROR_FAIL;
559 return target->type->read_memory_imp(target, address, size, count, buffer);
562 static int target_soft_reset_halt_imp(struct target *target)
564 if (!target_was_examined(target))
566 LOG_ERROR("Target not examined yet");
567 return ERROR_FAIL;
569 if (!target->type->soft_reset_halt_imp) {
570 LOG_ERROR("Target %s does not support soft_reset_halt",
571 target_name(target));
572 return ERROR_FAIL;
574 return target->type->soft_reset_halt_imp(target);
577 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)
579 if (!target_was_examined(target))
581 LOG_ERROR("Target not examined yet");
582 return ERROR_FAIL;
584 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);
587 int target_read_memory(struct target *target,
588 uint32_t address, uint32_t size, uint32_t count, uint8_t *buffer)
590 return target->type->read_memory(target, address, size, count, buffer);
593 int target_read_phys_memory(struct target *target,
594 uint32_t address, uint32_t size, uint32_t count, uint8_t *buffer)
596 return target->type->read_phys_memory(target, address, size, count, buffer);
599 int target_write_memory(struct target *target,
600 uint32_t address, uint32_t size, uint32_t count, uint8_t *buffer)
602 return target->type->write_memory(target, address, size, count, buffer);
605 int target_write_phys_memory(struct target *target,
606 uint32_t address, uint32_t size, uint32_t count, uint8_t *buffer)
608 return target->type->write_phys_memory(target, address, size, count, buffer);
611 int target_bulk_write_memory(struct target *target,
612 uint32_t address, uint32_t count, uint8_t *buffer)
614 return target->type->bulk_write_memory(target, address, count, buffer);
617 int target_add_breakpoint(struct target *target,
618 struct breakpoint *breakpoint)
620 if (target->state != TARGET_HALTED) {
621 LOG_WARNING("target %s is not halted", target->cmd_name);
622 return ERROR_TARGET_NOT_HALTED;
624 return target->type->add_breakpoint(target, breakpoint);
626 int target_remove_breakpoint(struct target *target,
627 struct breakpoint *breakpoint)
629 return target->type->remove_breakpoint(target, breakpoint);
632 int target_add_watchpoint(struct target *target,
633 struct watchpoint *watchpoint)
635 if (target->state != TARGET_HALTED) {
636 LOG_WARNING("target %s is not halted", target->cmd_name);
637 return ERROR_TARGET_NOT_HALTED;
639 return target->type->add_watchpoint(target, watchpoint);
641 int target_remove_watchpoint(struct target *target,
642 struct watchpoint *watchpoint)
644 return target->type->remove_watchpoint(target, watchpoint);
647 int target_get_gdb_reg_list(struct target *target,
648 struct reg **reg_list[], int *reg_list_size)
650 return target->type->get_gdb_reg_list(target, reg_list, reg_list_size);
652 int target_step(struct target *target,
653 int current, uint32_t address, int handle_breakpoints)
655 return target->type->step(target, current, address, handle_breakpoints);
659 int target_run_algorithm(struct target *target,
660 int num_mem_params, struct mem_param *mem_params,
661 int num_reg_params, struct reg_param *reg_param,
662 uint32_t entry_point, uint32_t exit_point,
663 int timeout_ms, void *arch_info)
665 return target->type->run_algorithm(target,
666 num_mem_params, mem_params, num_reg_params, reg_param,
667 entry_point, exit_point, timeout_ms, arch_info);
671 * Reset the @c examined flag for the given target.
672 * Pure paranoia -- targets are zeroed on allocation.
674 static void target_reset_examined(struct target *target)
676 target->examined = false;
679 static int
680 err_read_phys_memory(struct target *target, uint32_t address,
681 uint32_t size, uint32_t count, uint8_t *buffer)
683 LOG_ERROR("Not implemented: %s", __func__);
684 return ERROR_FAIL;
687 static int
688 err_write_phys_memory(struct target *target, uint32_t address,
689 uint32_t size, uint32_t count, uint8_t *buffer)
691 LOG_ERROR("Not implemented: %s", __func__);
692 return ERROR_FAIL;
695 static int handle_target(void *priv);
697 static int target_init_one(struct command_context *cmd_ctx,
698 struct target *target)
700 target_reset_examined(target);
702 struct target_type *type = target->type;
703 if (type->examine == NULL)
704 type->examine = default_examine;
706 int retval = type->init_target(cmd_ctx, target);
707 if (ERROR_OK != retval)
709 LOG_ERROR("target '%s' init failed", target_name(target));
710 return retval;
714 * @todo get rid of those *memory_imp() methods, now that all
715 * callers are using target_*_memory() accessors ... and make
716 * sure the "physical" paths handle the same issues.
718 /* a non-invasive way(in terms of patches) to add some code that
719 * runs before the type->write/read_memory implementation
721 type->write_memory_imp = target->type->write_memory;
722 type->write_memory = target_write_memory_imp;
724 type->read_memory_imp = target->type->read_memory;
725 type->read_memory = target_read_memory_imp;
727 type->soft_reset_halt_imp = target->type->soft_reset_halt;
728 type->soft_reset_halt = target_soft_reset_halt_imp;
730 type->run_algorithm_imp = target->type->run_algorithm;
731 type->run_algorithm = target_run_algorithm_imp;
733 /* Sanity-check MMU support ... stub in what we must, to help
734 * implement it in stages, but warn if we need to do so.
736 if (type->mmu)
738 if (type->write_phys_memory == NULL)
740 LOG_ERROR("type '%s' is missing write_phys_memory",
741 type->name);
742 type->write_phys_memory = err_write_phys_memory;
744 if (type->read_phys_memory == NULL)
746 LOG_ERROR("type '%s' is missing read_phys_memory",
747 type->name);
748 type->read_phys_memory = err_read_phys_memory;
750 if (type->virt2phys == NULL)
752 LOG_ERROR("type '%s' is missing virt2phys", type->name);
753 type->virt2phys = identity_virt2phys;
756 else
758 /* Make sure no-MMU targets all behave the same: make no
759 * distinction between physical and virtual addresses, and
760 * ensure that virt2phys() is always an identity mapping.
762 if (type->write_phys_memory || type->read_phys_memory
763 || type->virt2phys)
765 LOG_WARNING("type '%s' has bad MMU hooks", type->name);
768 type->mmu = no_mmu;
769 type->write_phys_memory = type->write_memory;
770 type->read_phys_memory = type->read_memory;
771 type->virt2phys = identity_virt2phys;
773 return ERROR_OK;
776 int target_init(struct command_context *cmd_ctx)
778 struct target *target;
779 int retval;
781 for (target = all_targets; target; target = target->next)
783 retval = target_init_one(cmd_ctx, target);
784 if (ERROR_OK != retval)
785 return retval;
788 if (!all_targets)
789 return ERROR_OK;
791 retval = target_register_user_commands(cmd_ctx);
792 if (ERROR_OK != retval)
793 return retval;
795 retval = target_register_timer_callback(&handle_target,
796 100, 1, cmd_ctx->interp);
797 if (ERROR_OK != retval)
798 return retval;
800 return ERROR_OK;
803 COMMAND_HANDLER(handle_target_init_command)
805 if (CMD_ARGC != 0)
806 return ERROR_COMMAND_SYNTAX_ERROR;
808 static bool target_initialized = false;
809 if (target_initialized)
811 LOG_INFO("'target init' has already been called");
812 return ERROR_OK;
814 target_initialized = true;
816 LOG_DEBUG("Initializing targets...");
817 return target_init(CMD_CTX);
820 int target_register_event_callback(int (*callback)(struct target *target, enum target_event event, void *priv), void *priv)
822 struct target_event_callback **callbacks_p = &target_event_callbacks;
824 if (callback == NULL)
826 return ERROR_INVALID_ARGUMENTS;
829 if (*callbacks_p)
831 while ((*callbacks_p)->next)
832 callbacks_p = &((*callbacks_p)->next);
833 callbacks_p = &((*callbacks_p)->next);
836 (*callbacks_p) = malloc(sizeof(struct target_event_callback));
837 (*callbacks_p)->callback = callback;
838 (*callbacks_p)->priv = priv;
839 (*callbacks_p)->next = NULL;
841 return ERROR_OK;
844 int target_register_timer_callback(int (*callback)(void *priv), int time_ms, int periodic, void *priv)
846 struct target_timer_callback **callbacks_p = &target_timer_callbacks;
847 struct timeval now;
849 if (callback == NULL)
851 return ERROR_INVALID_ARGUMENTS;
854 if (*callbacks_p)
856 while ((*callbacks_p)->next)
857 callbacks_p = &((*callbacks_p)->next);
858 callbacks_p = &((*callbacks_p)->next);
861 (*callbacks_p) = malloc(sizeof(struct target_timer_callback));
862 (*callbacks_p)->callback = callback;
863 (*callbacks_p)->periodic = periodic;
864 (*callbacks_p)->time_ms = time_ms;
866 gettimeofday(&now, NULL);
867 (*callbacks_p)->when.tv_usec = now.tv_usec + (time_ms % 1000) * 1000;
868 time_ms -= (time_ms % 1000);
869 (*callbacks_p)->when.tv_sec = now.tv_sec + (time_ms / 1000);
870 if ((*callbacks_p)->when.tv_usec > 1000000)
872 (*callbacks_p)->when.tv_usec = (*callbacks_p)->when.tv_usec - 1000000;
873 (*callbacks_p)->when.tv_sec += 1;
876 (*callbacks_p)->priv = priv;
877 (*callbacks_p)->next = NULL;
879 return ERROR_OK;
882 int target_unregister_event_callback(int (*callback)(struct target *target, enum target_event event, void *priv), void *priv)
884 struct target_event_callback **p = &target_event_callbacks;
885 struct target_event_callback *c = target_event_callbacks;
887 if (callback == NULL)
889 return ERROR_INVALID_ARGUMENTS;
892 while (c)
894 struct target_event_callback *next = c->next;
895 if ((c->callback == callback) && (c->priv == priv))
897 *p = next;
898 free(c);
899 return ERROR_OK;
901 else
902 p = &(c->next);
903 c = next;
906 return ERROR_OK;
909 int target_unregister_timer_callback(int (*callback)(void *priv), void *priv)
911 struct target_timer_callback **p = &target_timer_callbacks;
912 struct target_timer_callback *c = target_timer_callbacks;
914 if (callback == NULL)
916 return ERROR_INVALID_ARGUMENTS;
919 while (c)
921 struct target_timer_callback *next = c->next;
922 if ((c->callback == callback) && (c->priv == priv))
924 *p = next;
925 free(c);
926 return ERROR_OK;
928 else
929 p = &(c->next);
930 c = next;
933 return ERROR_OK;
936 int target_call_event_callbacks(struct target *target, enum target_event event)
938 struct target_event_callback *callback = target_event_callbacks;
939 struct target_event_callback *next_callback;
941 if (event == TARGET_EVENT_HALTED)
943 /* execute early halted first */
944 target_call_event_callbacks(target, TARGET_EVENT_GDB_HALT);
947 LOG_DEBUG("target event %i (%s)",
948 event,
949 Jim_Nvp_value2name_simple(nvp_target_event, event)->name);
951 target_handle_event(target, event);
953 while (callback)
955 next_callback = callback->next;
956 callback->callback(target, event, callback->priv);
957 callback = next_callback;
960 return ERROR_OK;
963 static int target_timer_callback_periodic_restart(
964 struct target_timer_callback *cb, struct timeval *now)
966 int time_ms = cb->time_ms;
967 cb->when.tv_usec = now->tv_usec + (time_ms % 1000) * 1000;
968 time_ms -= (time_ms % 1000);
969 cb->when.tv_sec = now->tv_sec + time_ms / 1000;
970 if (cb->when.tv_usec > 1000000)
972 cb->when.tv_usec = cb->when.tv_usec - 1000000;
973 cb->when.tv_sec += 1;
975 return ERROR_OK;
978 static int target_call_timer_callback(struct target_timer_callback *cb,
979 struct timeval *now)
981 cb->callback(cb->priv);
983 if (cb->periodic)
984 return target_timer_callback_periodic_restart(cb, now);
986 return target_unregister_timer_callback(cb->callback, cb->priv);
989 static int target_call_timer_callbacks_check_time(int checktime)
991 keep_alive();
993 struct timeval now;
994 gettimeofday(&now, NULL);
996 struct target_timer_callback *callback = target_timer_callbacks;
997 while (callback)
999 // cleaning up may unregister and free this callback
1000 struct target_timer_callback *next_callback = callback->next;
1002 bool call_it = callback->callback &&
1003 ((!checktime && callback->periodic) ||
1004 now.tv_sec > callback->when.tv_sec ||
1005 (now.tv_sec == callback->when.tv_sec &&
1006 now.tv_usec >= callback->when.tv_usec));
1008 if (call_it)
1010 int retval = target_call_timer_callback(callback, &now);
1011 if (retval != ERROR_OK)
1012 return retval;
1015 callback = next_callback;
1018 return ERROR_OK;
1021 int target_call_timer_callbacks(void)
1023 return target_call_timer_callbacks_check_time(1);
1026 /* invoke periodic callbacks immediately */
1027 int target_call_timer_callbacks_now(void)
1029 return target_call_timer_callbacks_check_time(0);
1032 int target_alloc_working_area(struct target *target, uint32_t size, struct working_area **area)
1034 struct working_area *c = target->working_areas;
1035 struct working_area *new_wa = NULL;
1037 /* Reevaluate working area address based on MMU state*/
1038 if (target->working_areas == NULL)
1040 int retval;
1041 int enabled;
1043 retval = target->type->mmu(target, &enabled);
1044 if (retval != ERROR_OK)
1046 return retval;
1049 if (!enabled) {
1050 if (target->working_area_phys_spec) {
1051 LOG_DEBUG("MMU disabled, using physical "
1052 "address for working memory 0x%08x",
1053 (unsigned)target->working_area_phys);
1054 target->working_area = target->working_area_phys;
1055 } else {
1056 LOG_ERROR("No working memory available. "
1057 "Specify -work-area-phys to target.");
1058 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
1060 } else {
1061 if (target->working_area_virt_spec) {
1062 LOG_DEBUG("MMU enabled, using virtual "
1063 "address for working memory 0x%08x",
1064 (unsigned)target->working_area_virt);
1065 target->working_area = target->working_area_virt;
1066 } else {
1067 LOG_ERROR("No working memory available. "
1068 "Specify -work-area-virt to target.");
1069 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
1074 /* only allocate multiples of 4 byte */
1075 if (size % 4)
1077 LOG_ERROR("BUG: code tried to allocate unaligned number of bytes (0x%08x), padding", ((unsigned)(size)));
1078 size = (size + 3) & (~3);
1081 /* see if there's already a matching working area */
1082 while (c)
1084 if ((c->free) && (c->size == size))
1086 new_wa = c;
1087 break;
1089 c = c->next;
1092 /* if not, allocate a new one */
1093 if (!new_wa)
1095 struct working_area **p = &target->working_areas;
1096 uint32_t first_free = target->working_area;
1097 uint32_t free_size = target->working_area_size;
1099 c = target->working_areas;
1100 while (c)
1102 first_free += c->size;
1103 free_size -= c->size;
1104 p = &c->next;
1105 c = c->next;
1108 if (free_size < size)
1110 LOG_WARNING("not enough working area available(requested %u, free %u)",
1111 (unsigned)(size), (unsigned)(free_size));
1112 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
1115 LOG_DEBUG("allocated new working area at address 0x%08x", (unsigned)first_free);
1117 new_wa = malloc(sizeof(struct working_area));
1118 new_wa->next = NULL;
1119 new_wa->size = size;
1120 new_wa->address = first_free;
1122 if (target->backup_working_area)
1124 int retval;
1125 new_wa->backup = malloc(new_wa->size);
1126 if ((retval = target_read_memory(target, new_wa->address, 4, new_wa->size / 4, new_wa->backup)) != ERROR_OK)
1128 free(new_wa->backup);
1129 free(new_wa);
1130 return retval;
1133 else
1135 new_wa->backup = NULL;
1138 /* put new entry in list */
1139 *p = new_wa;
1142 /* mark as used, and return the new (reused) area */
1143 new_wa->free = 0;
1144 *area = new_wa;
1146 /* user pointer */
1147 new_wa->user = area;
1149 return ERROR_OK;
1152 int target_free_working_area_restore(struct target *target, struct working_area *area, int restore)
1154 if (area->free)
1155 return ERROR_OK;
1157 if (restore && target->backup_working_area)
1159 int retval;
1160 if ((retval = target_write_memory(target, area->address, 4, area->size / 4, area->backup)) != ERROR_OK)
1161 return retval;
1164 area->free = 1;
1166 /* mark user pointer invalid */
1167 *area->user = NULL;
1168 area->user = NULL;
1170 return ERROR_OK;
1173 int target_free_working_area(struct target *target, struct working_area *area)
1175 return target_free_working_area_restore(target, area, 1);
1178 /* free resources and restore memory, if restoring memory fails,
1179 * free up resources anyway
1181 void target_free_all_working_areas_restore(struct target *target, int restore)
1183 struct working_area *c = target->working_areas;
1185 while (c)
1187 struct working_area *next = c->next;
1188 target_free_working_area_restore(target, c, restore);
1190 if (c->backup)
1191 free(c->backup);
1193 free(c);
1195 c = next;
1198 target->working_areas = NULL;
1201 void target_free_all_working_areas(struct target *target)
1203 target_free_all_working_areas_restore(target, 1);
1206 int target_arch_state(struct target *target)
1208 int retval;
1209 if (target == NULL)
1211 LOG_USER("No target has been configured");
1212 return ERROR_OK;
1215 LOG_USER("target state: %s", target_state_name( target ));
1217 if (target->state != TARGET_HALTED)
1218 return ERROR_OK;
1220 retval = target->type->arch_state(target);
1221 return retval;
1224 /* Single aligned words are guaranteed to use 16 or 32 bit access
1225 * mode respectively, otherwise data is handled as quickly as
1226 * possible
1228 int target_write_buffer(struct target *target, uint32_t address, uint32_t size, uint8_t *buffer)
1230 int retval;
1231 LOG_DEBUG("writing buffer of %i byte at 0x%8.8x",
1232 (int)size, (unsigned)address);
1234 if (!target_was_examined(target))
1236 LOG_ERROR("Target not examined yet");
1237 return ERROR_FAIL;
1240 if (size == 0) {
1241 return ERROR_OK;
1244 if ((address + size - 1) < address)
1246 /* GDB can request this when e.g. PC is 0xfffffffc*/
1247 LOG_ERROR("address + size wrapped(0x%08x, 0x%08x)",
1248 (unsigned)address,
1249 (unsigned)size);
1250 return ERROR_FAIL;
1253 if (((address % 2) == 0) && (size == 2))
1255 return target_write_memory(target, address, 2, 1, buffer);
1258 /* handle unaligned head bytes */
1259 if (address % 4)
1261 uint32_t unaligned = 4 - (address % 4);
1263 if (unaligned > size)
1264 unaligned = size;
1266 if ((retval = target_write_memory(target, address, 1, unaligned, buffer)) != ERROR_OK)
1267 return retval;
1269 buffer += unaligned;
1270 address += unaligned;
1271 size -= unaligned;
1274 /* handle aligned words */
1275 if (size >= 4)
1277 int aligned = size - (size % 4);
1279 /* use bulk writes above a certain limit. This may have to be changed */
1280 if (aligned > 128)
1282 if ((retval = target->type->bulk_write_memory(target, address, aligned / 4, buffer)) != ERROR_OK)
1283 return retval;
1285 else
1287 if ((retval = target_write_memory(target, address, 4, aligned / 4, buffer)) != ERROR_OK)
1288 return retval;
1291 buffer += aligned;
1292 address += aligned;
1293 size -= aligned;
1296 /* handle tail writes of less than 4 bytes */
1297 if (size > 0)
1299 if ((retval = target_write_memory(target, address, 1, size, buffer)) != ERROR_OK)
1300 return retval;
1303 return ERROR_OK;
1306 /* Single aligned words are guaranteed to use 16 or 32 bit access
1307 * mode respectively, otherwise data is handled as quickly as
1308 * possible
1310 int target_read_buffer(struct target *target, uint32_t address, uint32_t size, uint8_t *buffer)
1312 int retval;
1313 LOG_DEBUG("reading buffer of %i byte at 0x%8.8x",
1314 (int)size, (unsigned)address);
1316 if (!target_was_examined(target))
1318 LOG_ERROR("Target not examined yet");
1319 return ERROR_FAIL;
1322 if (size == 0) {
1323 return ERROR_OK;
1326 if ((address + size - 1) < address)
1328 /* GDB can request this when e.g. PC is 0xfffffffc*/
1329 LOG_ERROR("address + size wrapped(0x%08" PRIx32 ", 0x%08" PRIx32 ")",
1330 address,
1331 size);
1332 return ERROR_FAIL;
1335 if (((address % 2) == 0) && (size == 2))
1337 return target_read_memory(target, address, 2, 1, buffer);
1340 /* handle unaligned head bytes */
1341 if (address % 4)
1343 uint32_t unaligned = 4 - (address % 4);
1345 if (unaligned > size)
1346 unaligned = size;
1348 if ((retval = target_read_memory(target, address, 1, unaligned, buffer)) != ERROR_OK)
1349 return retval;
1351 buffer += unaligned;
1352 address += unaligned;
1353 size -= unaligned;
1356 /* handle aligned words */
1357 if (size >= 4)
1359 int aligned = size - (size % 4);
1361 if ((retval = target_read_memory(target, address, 4, aligned / 4, buffer)) != ERROR_OK)
1362 return retval;
1364 buffer += aligned;
1365 address += aligned;
1366 size -= aligned;
1369 /*prevent byte access when possible (avoid AHB access limitations in some cases)*/
1370 if(size >=2)
1372 int aligned = size - (size%2);
1373 retval = target_read_memory(target, address, 2, aligned / 2, buffer);
1374 if (retval != ERROR_OK)
1375 return retval;
1377 buffer += aligned;
1378 address += aligned;
1379 size -= aligned;
1381 /* handle tail writes of less than 4 bytes */
1382 if (size > 0)
1384 if ((retval = target_read_memory(target, address, 1, size, buffer)) != ERROR_OK)
1385 return retval;
1388 return ERROR_OK;
1391 int target_checksum_memory(struct target *target, uint32_t address, uint32_t size, uint32_t* crc)
1393 uint8_t *buffer;
1394 int retval;
1395 uint32_t i;
1396 uint32_t checksum = 0;
1397 if (!target_was_examined(target))
1399 LOG_ERROR("Target not examined yet");
1400 return ERROR_FAIL;
1403 if ((retval = target->type->checksum_memory(target, address,
1404 size, &checksum)) != ERROR_OK)
1406 buffer = malloc(size);
1407 if (buffer == NULL)
1409 LOG_ERROR("error allocating buffer for section (%d bytes)", (int)size);
1410 return ERROR_INVALID_ARGUMENTS;
1412 retval = target_read_buffer(target, address, size, buffer);
1413 if (retval != ERROR_OK)
1415 free(buffer);
1416 return retval;
1419 /* convert to target endianess */
1420 for (i = 0; i < (size/sizeof(uint32_t)); i++)
1422 uint32_t target_data;
1423 target_data = target_buffer_get_u32(target, &buffer[i*sizeof(uint32_t)]);
1424 target_buffer_set_u32(target, &buffer[i*sizeof(uint32_t)], target_data);
1427 retval = image_calculate_checksum(buffer, size, &checksum);
1428 free(buffer);
1431 *crc = checksum;
1433 return retval;
1436 int target_blank_check_memory(struct target *target, uint32_t address, uint32_t size, uint32_t* blank)
1438 int retval;
1439 if (!target_was_examined(target))
1441 LOG_ERROR("Target not examined yet");
1442 return ERROR_FAIL;
1445 if (target->type->blank_check_memory == 0)
1446 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
1448 retval = target->type->blank_check_memory(target, address, size, blank);
1450 return retval;
1453 int target_read_u32(struct target *target, uint32_t address, uint32_t *value)
1455 uint8_t value_buf[4];
1456 if (!target_was_examined(target))
1458 LOG_ERROR("Target not examined yet");
1459 return ERROR_FAIL;
1462 int retval = target_read_memory(target, address, 4, 1, value_buf);
1464 if (retval == ERROR_OK)
1466 *value = target_buffer_get_u32(target, value_buf);
1467 LOG_DEBUG("address: 0x%8.8" PRIx32 ", value: 0x%8.8" PRIx32 "",
1468 address,
1469 *value);
1471 else
1473 *value = 0x0;
1474 LOG_DEBUG("address: 0x%8.8" PRIx32 " failed",
1475 address);
1478 return retval;
1481 int target_read_u16(struct target *target, uint32_t address, uint16_t *value)
1483 uint8_t value_buf[2];
1484 if (!target_was_examined(target))
1486 LOG_ERROR("Target not examined yet");
1487 return ERROR_FAIL;
1490 int retval = target_read_memory(target, address, 2, 1, value_buf);
1492 if (retval == ERROR_OK)
1494 *value = target_buffer_get_u16(target, value_buf);
1495 LOG_DEBUG("address: 0x%8.8" PRIx32 ", value: 0x%4.4x",
1496 address,
1497 *value);
1499 else
1501 *value = 0x0;
1502 LOG_DEBUG("address: 0x%8.8" PRIx32 " failed",
1503 address);
1506 return retval;
1509 int target_read_u8(struct target *target, uint32_t address, uint8_t *value)
1511 int retval = target_read_memory(target, address, 1, 1, value);
1512 if (!target_was_examined(target))
1514 LOG_ERROR("Target not examined yet");
1515 return ERROR_FAIL;
1518 if (retval == ERROR_OK)
1520 LOG_DEBUG("address: 0x%8.8" PRIx32 ", value: 0x%2.2x",
1521 address,
1522 *value);
1524 else
1526 *value = 0x0;
1527 LOG_DEBUG("address: 0x%8.8" PRIx32 " failed",
1528 address);
1531 return retval;
1534 int target_write_u32(struct target *target, uint32_t address, uint32_t value)
1536 int retval;
1537 uint8_t value_buf[4];
1538 if (!target_was_examined(target))
1540 LOG_ERROR("Target not examined yet");
1541 return ERROR_FAIL;
1544 LOG_DEBUG("address: 0x%8.8" PRIx32 ", value: 0x%8.8" PRIx32 "",
1545 address,
1546 value);
1548 target_buffer_set_u32(target, value_buf, value);
1549 if ((retval = target_write_memory(target, address, 4, 1, value_buf)) != ERROR_OK)
1551 LOG_DEBUG("failed: %i", retval);
1554 return retval;
1557 int target_write_u16(struct target *target, uint32_t address, uint16_t value)
1559 int retval;
1560 uint8_t value_buf[2];
1561 if (!target_was_examined(target))
1563 LOG_ERROR("Target not examined yet");
1564 return ERROR_FAIL;
1567 LOG_DEBUG("address: 0x%8.8" PRIx32 ", value: 0x%8.8x",
1568 address,
1569 value);
1571 target_buffer_set_u16(target, value_buf, value);
1572 if ((retval = target_write_memory(target, address, 2, 1, value_buf)) != ERROR_OK)
1574 LOG_DEBUG("failed: %i", retval);
1577 return retval;
1580 int target_write_u8(struct target *target, uint32_t address, uint8_t value)
1582 int retval;
1583 if (!target_was_examined(target))
1585 LOG_ERROR("Target not examined yet");
1586 return ERROR_FAIL;
1589 LOG_DEBUG("address: 0x%8.8" PRIx32 ", value: 0x%2.2x",
1590 address, value);
1592 if ((retval = target_write_memory(target, address, 1, 1, &value)) != ERROR_OK)
1594 LOG_DEBUG("failed: %i", retval);
1597 return retval;
1600 COMMAND_HANDLER(handle_targets_command)
1602 struct target *target = all_targets;
1604 if (CMD_ARGC == 1)
1606 target = get_target(CMD_ARGV[0]);
1607 if (target == NULL) {
1608 command_print(CMD_CTX,"Target: %s is unknown, try one of:\n", CMD_ARGV[0]);
1609 goto DumpTargets;
1611 if (!target->tap->enabled) {
1612 command_print(CMD_CTX,"Target: TAP %s is disabled, "
1613 "can't be the current target\n",
1614 target->tap->dotted_name);
1615 return ERROR_FAIL;
1618 CMD_CTX->current_target = target->target_number;
1619 return ERROR_OK;
1621 DumpTargets:
1623 target = all_targets;
1624 command_print(CMD_CTX, " TargetName Type Endian TapName State ");
1625 command_print(CMD_CTX, "-- ------------------ ---------- ------ ------------------ ------------");
1626 while (target)
1628 const char *state;
1629 char marker = ' ';
1631 if (target->tap->enabled)
1632 state = target_state_name( target );
1633 else
1634 state = "tap-disabled";
1636 if (CMD_CTX->current_target == target->target_number)
1637 marker = '*';
1639 /* keep columns lined up to match the headers above */
1640 command_print(CMD_CTX, "%2d%c %-18s %-10s %-6s %-18s %s",
1641 target->target_number,
1642 marker,
1643 target_name(target),
1644 target_type_name(target),
1645 Jim_Nvp_value2name_simple(nvp_target_endian,
1646 target->endianness)->name,
1647 target->tap->dotted_name,
1648 state);
1649 target = target->next;
1652 return ERROR_OK;
1655 /* every 300ms we check for reset & powerdropout and issue a "reset halt" if so. */
1657 static int powerDropout;
1658 static int srstAsserted;
1660 static int runPowerRestore;
1661 static int runPowerDropout;
1662 static int runSrstAsserted;
1663 static int runSrstDeasserted;
1665 static int sense_handler(void)
1667 static int prevSrstAsserted = 0;
1668 static int prevPowerdropout = 0;
1670 int retval;
1671 if ((retval = jtag_power_dropout(&powerDropout)) != ERROR_OK)
1672 return retval;
1674 int powerRestored;
1675 powerRestored = prevPowerdropout && !powerDropout;
1676 if (powerRestored)
1678 runPowerRestore = 1;
1681 long long current = timeval_ms();
1682 static long long lastPower = 0;
1683 int waitMore = lastPower + 2000 > current;
1684 if (powerDropout && !waitMore)
1686 runPowerDropout = 1;
1687 lastPower = current;
1690 if ((retval = jtag_srst_asserted(&srstAsserted)) != ERROR_OK)
1691 return retval;
1693 int srstDeasserted;
1694 srstDeasserted = prevSrstAsserted && !srstAsserted;
1696 static long long lastSrst = 0;
1697 waitMore = lastSrst + 2000 > current;
1698 if (srstDeasserted && !waitMore)
1700 runSrstDeasserted = 1;
1701 lastSrst = current;
1704 if (!prevSrstAsserted && srstAsserted)
1706 runSrstAsserted = 1;
1709 prevSrstAsserted = srstAsserted;
1710 prevPowerdropout = powerDropout;
1712 if (srstDeasserted || powerRestored)
1714 /* Other than logging the event we can't do anything here.
1715 * Issuing a reset is a particularly bad idea as we might
1716 * be inside a reset already.
1720 return ERROR_OK;
1723 static void target_call_event_callbacks_all(enum target_event e) {
1724 struct target *target;
1725 target = all_targets;
1726 while (target) {
1727 target_call_event_callbacks(target, e);
1728 target = target->next;
1732 /* process target state changes */
1733 static int handle_target(void *priv)
1735 Jim_Interp *interp = (Jim_Interp *)priv;
1736 int retval = ERROR_OK;
1738 /* we do not want to recurse here... */
1739 static int recursive = 0;
1740 if (! recursive)
1742 recursive = 1;
1743 sense_handler();
1744 /* danger! running these procedures can trigger srst assertions and power dropouts.
1745 * We need to avoid an infinite loop/recursion here and we do that by
1746 * clearing the flags after running these events.
1748 int did_something = 0;
1749 if (runSrstAsserted)
1751 target_call_event_callbacks_all(TARGET_EVENT_GDB_HALT);
1752 Jim_Eval(interp, "srst_asserted");
1753 did_something = 1;
1755 if (runSrstDeasserted)
1757 Jim_Eval(interp, "srst_deasserted");
1758 did_something = 1;
1760 if (runPowerDropout)
1762 target_call_event_callbacks_all(TARGET_EVENT_GDB_HALT);
1763 Jim_Eval(interp, "power_dropout");
1764 did_something = 1;
1766 if (runPowerRestore)
1768 Jim_Eval(interp, "power_restore");
1769 did_something = 1;
1772 if (did_something)
1774 /* clear detect flags */
1775 sense_handler();
1778 /* clear action flags */
1780 runSrstAsserted = 0;
1781 runSrstDeasserted = 0;
1782 runPowerRestore = 0;
1783 runPowerDropout = 0;
1785 recursive = 0;
1788 /* Poll targets for state changes unless that's globally disabled.
1789 * Skip targets that are currently disabled.
1791 for (struct target *target = all_targets;
1792 is_jtag_poll_safe() && target;
1793 target = target->next)
1795 if (!target->tap->enabled)
1796 continue;
1798 /* only poll target if we've got power and srst isn't asserted */
1799 if (!powerDropout && !srstAsserted)
1801 /* polling may fail silently until the target has been examined */
1802 if ((retval = target_poll(target)) != ERROR_OK)
1804 target_call_event_callbacks(target, TARGET_EVENT_GDB_HALT);
1805 return retval;
1810 return retval;
1813 COMMAND_HANDLER(handle_reg_command)
1815 struct target *target;
1816 struct reg *reg = NULL;
1817 unsigned count = 0;
1818 char *value;
1820 LOG_DEBUG("-");
1822 target = get_current_target(CMD_CTX);
1824 /* list all available registers for the current target */
1825 if (CMD_ARGC == 0)
1827 struct reg_cache *cache = target->reg_cache;
1829 count = 0;
1830 while (cache)
1832 unsigned i;
1834 command_print(CMD_CTX, "===== %s", cache->name);
1836 for (i = 0, reg = cache->reg_list;
1837 i < cache->num_regs;
1838 i++, reg++, count++)
1840 /* only print cached values if they are valid */
1841 if (reg->valid) {
1842 value = buf_to_str(reg->value,
1843 reg->size, 16);
1844 command_print(CMD_CTX,
1845 "(%i) %s (/%" PRIu32 "): 0x%s%s",
1846 count, reg->name,
1847 reg->size, value,
1848 reg->dirty
1849 ? " (dirty)"
1850 : "");
1851 free(value);
1852 } else {
1853 command_print(CMD_CTX, "(%i) %s (/%" PRIu32 ")",
1854 count, reg->name,
1855 reg->size) ;
1858 cache = cache->next;
1861 return ERROR_OK;
1864 /* access a single register by its ordinal number */
1865 if ((CMD_ARGV[0][0] >= '0') && (CMD_ARGV[0][0] <= '9'))
1867 unsigned num;
1868 COMMAND_PARSE_NUMBER(uint, CMD_ARGV[0], num);
1870 struct reg_cache *cache = target->reg_cache;
1871 count = 0;
1872 while (cache)
1874 unsigned i;
1875 for (i = 0; i < cache->num_regs; i++)
1877 if (count++ == num)
1879 reg = &cache->reg_list[i];
1880 break;
1883 if (reg)
1884 break;
1885 cache = cache->next;
1888 if (!reg)
1890 command_print(CMD_CTX, "%i is out of bounds, the current target has only %i registers (0 - %i)", num, count, count - 1);
1891 return ERROR_OK;
1893 } else /* access a single register by its name */
1895 reg = register_get_by_name(target->reg_cache, CMD_ARGV[0], 1);
1897 if (!reg)
1899 command_print(CMD_CTX, "register %s not found in current target", CMD_ARGV[0]);
1900 return ERROR_OK;
1904 /* display a register */
1905 if ((CMD_ARGC == 1) || ((CMD_ARGC == 2) && !((CMD_ARGV[1][0] >= '0') && (CMD_ARGV[1][0] <= '9'))))
1907 if ((CMD_ARGC == 2) && (strcmp(CMD_ARGV[1], "force") == 0))
1908 reg->valid = 0;
1910 if (reg->valid == 0)
1912 reg->type->get(reg);
1914 value = buf_to_str(reg->value, reg->size, 16);
1915 command_print(CMD_CTX, "%s (/%i): 0x%s", reg->name, (int)(reg->size), value);
1916 free(value);
1917 return ERROR_OK;
1920 /* set register value */
1921 if (CMD_ARGC == 2)
1923 uint8_t *buf = malloc(DIV_ROUND_UP(reg->size, 8));
1924 str_to_buf(CMD_ARGV[1], strlen(CMD_ARGV[1]), buf, reg->size, 0);
1926 reg->type->set(reg, buf);
1928 value = buf_to_str(reg->value, reg->size, 16);
1929 command_print(CMD_CTX, "%s (/%i): 0x%s", reg->name, (int)(reg->size), value);
1930 free(value);
1932 free(buf);
1934 return ERROR_OK;
1937 command_print(CMD_CTX, "usage: reg <#|name> [value]");
1939 return ERROR_OK;
1942 COMMAND_HANDLER(handle_poll_command)
1944 int retval = ERROR_OK;
1945 struct target *target = get_current_target(CMD_CTX);
1947 if (CMD_ARGC == 0)
1949 command_print(CMD_CTX, "background polling: %s",
1950 jtag_poll_get_enabled() ? "on" : "off");
1951 command_print(CMD_CTX, "TAP: %s (%s)",
1952 target->tap->dotted_name,
1953 target->tap->enabled ? "enabled" : "disabled");
1954 if (!target->tap->enabled)
1955 return ERROR_OK;
1956 if ((retval = target_poll(target)) != ERROR_OK)
1957 return retval;
1958 if ((retval = target_arch_state(target)) != ERROR_OK)
1959 return retval;
1961 else if (CMD_ARGC == 1)
1963 bool enable;
1964 COMMAND_PARSE_ON_OFF(CMD_ARGV[0], enable);
1965 jtag_poll_set_enabled(enable);
1967 else
1969 return ERROR_COMMAND_SYNTAX_ERROR;
1972 return retval;
1975 COMMAND_HANDLER(handle_wait_halt_command)
1977 if (CMD_ARGC > 1)
1978 return ERROR_COMMAND_SYNTAX_ERROR;
1980 unsigned ms = 5000;
1981 if (1 == CMD_ARGC)
1983 int retval = parse_uint(CMD_ARGV[0], &ms);
1984 if (ERROR_OK != retval)
1986 command_print(CMD_CTX, "usage: %s [seconds]", CMD_NAME);
1987 return ERROR_COMMAND_SYNTAX_ERROR;
1989 // convert seconds (given) to milliseconds (needed)
1990 ms *= 1000;
1993 struct target *target = get_current_target(CMD_CTX);
1994 return target_wait_state(target, TARGET_HALTED, ms);
1997 /* wait for target state to change. The trick here is to have a low
1998 * latency for short waits and not to suck up all the CPU time
1999 * on longer waits.
2001 * After 500ms, keep_alive() is invoked
2003 int target_wait_state(struct target *target, enum target_state state, int ms)
2005 int retval;
2006 long long then = 0, cur;
2007 int once = 1;
2009 for (;;)
2011 if ((retval = target_poll(target)) != ERROR_OK)
2012 return retval;
2013 if (target->state == state)
2015 break;
2017 cur = timeval_ms();
2018 if (once)
2020 once = 0;
2021 then = timeval_ms();
2022 LOG_DEBUG("waiting for target %s...",
2023 Jim_Nvp_value2name_simple(nvp_target_state,state)->name);
2026 if (cur-then > 500)
2028 keep_alive();
2031 if ((cur-then) > ms)
2033 LOG_ERROR("timed out while waiting for target %s",
2034 Jim_Nvp_value2name_simple(nvp_target_state,state)->name);
2035 return ERROR_FAIL;
2039 return ERROR_OK;
2042 COMMAND_HANDLER(handle_halt_command)
2044 LOG_DEBUG("-");
2046 struct target *target = get_current_target(CMD_CTX);
2047 int retval = target_halt(target);
2048 if (ERROR_OK != retval)
2049 return retval;
2051 if (CMD_ARGC == 1)
2053 unsigned wait;
2054 retval = parse_uint(CMD_ARGV[0], &wait);
2055 if (ERROR_OK != retval)
2056 return ERROR_COMMAND_SYNTAX_ERROR;
2057 if (!wait)
2058 return ERROR_OK;
2061 return CALL_COMMAND_HANDLER(handle_wait_halt_command);
2064 COMMAND_HANDLER(handle_soft_reset_halt_command)
2066 struct target *target = get_current_target(CMD_CTX);
2068 LOG_USER("requesting target halt and executing a soft reset");
2070 target->type->soft_reset_halt(target);
2072 return ERROR_OK;
2075 COMMAND_HANDLER(handle_reset_command)
2077 if (CMD_ARGC > 1)
2078 return ERROR_COMMAND_SYNTAX_ERROR;
2080 enum target_reset_mode reset_mode = RESET_RUN;
2081 if (CMD_ARGC == 1)
2083 const Jim_Nvp *n;
2084 n = Jim_Nvp_name2value_simple(nvp_reset_modes, CMD_ARGV[0]);
2085 if ((n->name == NULL) || (n->value == RESET_UNKNOWN)) {
2086 return ERROR_COMMAND_SYNTAX_ERROR;
2088 reset_mode = n->value;
2091 /* reset *all* targets */
2092 return target_process_reset(CMD_CTX, reset_mode);
2096 COMMAND_HANDLER(handle_resume_command)
2098 int current = 1;
2099 if (CMD_ARGC > 1)
2100 return ERROR_COMMAND_SYNTAX_ERROR;
2102 struct target *target = get_current_target(CMD_CTX);
2103 target_handle_event(target, TARGET_EVENT_OLD_pre_resume);
2105 /* with no CMD_ARGV, resume from current pc, addr = 0,
2106 * with one arguments, addr = CMD_ARGV[0],
2107 * handle breakpoints, not debugging */
2108 uint32_t addr = 0;
2109 if (CMD_ARGC == 1)
2111 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], addr);
2112 current = 0;
2115 return target_resume(target, current, addr, 1, 0);
2118 COMMAND_HANDLER(handle_step_command)
2120 if (CMD_ARGC > 1)
2121 return ERROR_COMMAND_SYNTAX_ERROR;
2123 LOG_DEBUG("-");
2125 /* with no CMD_ARGV, step from current pc, addr = 0,
2126 * with one argument addr = CMD_ARGV[0],
2127 * handle breakpoints, debugging */
2128 uint32_t addr = 0;
2129 int current_pc = 1;
2130 if (CMD_ARGC == 1)
2132 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], addr);
2133 current_pc = 0;
2136 struct target *target = get_current_target(CMD_CTX);
2138 return target->type->step(target, current_pc, addr, 1);
2141 static void handle_md_output(struct command_context *cmd_ctx,
2142 struct target *target, uint32_t address, unsigned size,
2143 unsigned count, const uint8_t *buffer)
2145 const unsigned line_bytecnt = 32;
2146 unsigned line_modulo = line_bytecnt / size;
2148 char output[line_bytecnt * 4 + 1];
2149 unsigned output_len = 0;
2151 const char *value_fmt;
2152 switch (size) {
2153 case 4: value_fmt = "%8.8x "; break;
2154 case 2: value_fmt = "%4.2x "; break;
2155 case 1: value_fmt = "%2.2x "; break;
2156 default:
2157 LOG_ERROR("invalid memory read size: %u", size);
2158 exit(-1);
2161 for (unsigned i = 0; i < count; i++)
2163 if (i % line_modulo == 0)
2165 output_len += snprintf(output + output_len,
2166 sizeof(output) - output_len,
2167 "0x%8.8x: ",
2168 (unsigned)(address + (i*size)));
2171 uint32_t value = 0;
2172 const uint8_t *value_ptr = buffer + i * size;
2173 switch (size) {
2174 case 4: value = target_buffer_get_u32(target, value_ptr); break;
2175 case 2: value = target_buffer_get_u16(target, value_ptr); break;
2176 case 1: value = *value_ptr;
2178 output_len += snprintf(output + output_len,
2179 sizeof(output) - output_len,
2180 value_fmt, value);
2182 if ((i % line_modulo == line_modulo - 1) || (i == count - 1))
2184 command_print(cmd_ctx, "%s", output);
2185 output_len = 0;
2190 COMMAND_HANDLER(handle_md_command)
2192 if (CMD_ARGC < 1)
2193 return ERROR_COMMAND_SYNTAX_ERROR;
2195 unsigned size = 0;
2196 switch (CMD_NAME[2]) {
2197 case 'w': size = 4; break;
2198 case 'h': size = 2; break;
2199 case 'b': size = 1; break;
2200 default: return ERROR_COMMAND_SYNTAX_ERROR;
2203 bool physical=strcmp(CMD_ARGV[0], "phys")==0;
2204 int (*fn)(struct target *target,
2205 uint32_t address, uint32_t size, uint32_t count, uint8_t *buffer);
2206 if (physical)
2208 CMD_ARGC--;
2209 CMD_ARGV++;
2210 fn=target_read_phys_memory;
2211 } else
2213 fn=target_read_memory;
2215 if ((CMD_ARGC < 1) || (CMD_ARGC > 2))
2217 return ERROR_COMMAND_SYNTAX_ERROR;
2220 uint32_t address;
2221 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], address);
2223 unsigned count = 1;
2224 if (CMD_ARGC == 2)
2225 COMMAND_PARSE_NUMBER(uint, CMD_ARGV[1], count);
2227 uint8_t *buffer = calloc(count, size);
2229 struct target *target = get_current_target(CMD_CTX);
2230 int retval = fn(target, address, size, count, buffer);
2231 if (ERROR_OK == retval)
2232 handle_md_output(CMD_CTX, target, address, size, count, buffer);
2234 free(buffer);
2236 return retval;
2239 COMMAND_HANDLER(handle_mw_command)
2241 if (CMD_ARGC < 2)
2243 return ERROR_COMMAND_SYNTAX_ERROR;
2245 bool physical=strcmp(CMD_ARGV[0], "phys")==0;
2246 int (*fn)(struct target *target,
2247 uint32_t address, uint32_t size, uint32_t count, uint8_t *buffer);
2248 if (physical)
2250 CMD_ARGC--;
2251 CMD_ARGV++;
2252 fn=target_write_phys_memory;
2253 } else
2255 fn=target_write_memory;
2257 if ((CMD_ARGC < 2) || (CMD_ARGC > 3))
2258 return ERROR_COMMAND_SYNTAX_ERROR;
2260 uint32_t address;
2261 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], address);
2263 uint32_t value;
2264 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], value);
2266 unsigned count = 1;
2267 if (CMD_ARGC == 3)
2268 COMMAND_PARSE_NUMBER(uint, CMD_ARGV[2], count);
2270 struct target *target = get_current_target(CMD_CTX);
2271 unsigned wordsize;
2272 uint8_t value_buf[4];
2273 switch (CMD_NAME[2])
2275 case 'w':
2276 wordsize = 4;
2277 target_buffer_set_u32(target, value_buf, value);
2278 break;
2279 case 'h':
2280 wordsize = 2;
2281 target_buffer_set_u16(target, value_buf, value);
2282 break;
2283 case 'b':
2284 wordsize = 1;
2285 value_buf[0] = value;
2286 break;
2287 default:
2288 return ERROR_COMMAND_SYNTAX_ERROR;
2290 for (unsigned i = 0; i < count; i++)
2292 int retval = fn(target,
2293 address + i * wordsize, wordsize, 1, value_buf);
2294 if (ERROR_OK != retval)
2295 return retval;
2296 keep_alive();
2299 return ERROR_OK;
2303 static COMMAND_HELPER(parse_load_image_command_CMD_ARGV, struct image *image,
2304 uint32_t *min_address, uint32_t *max_address)
2306 if (CMD_ARGC < 1 || CMD_ARGC > 5)
2307 return ERROR_COMMAND_SYNTAX_ERROR;
2309 /* a base address isn't always necessary,
2310 * default to 0x0 (i.e. don't relocate) */
2311 if (CMD_ARGC >= 2)
2313 uint32_t addr;
2314 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], addr);
2315 image->base_address = addr;
2316 image->base_address_set = 1;
2318 else
2319 image->base_address_set = 0;
2321 image->start_address_set = 0;
2323 if (CMD_ARGC >= 4)
2325 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[3], *min_address);
2327 if (CMD_ARGC == 5)
2329 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[4], *max_address);
2330 // use size (given) to find max (required)
2331 *max_address += *min_address;
2334 if (*min_address > *max_address)
2335 return ERROR_COMMAND_SYNTAX_ERROR;
2337 return ERROR_OK;
2340 COMMAND_HANDLER(handle_load_image_command)
2342 uint8_t *buffer;
2343 size_t buf_cnt;
2344 uint32_t image_size;
2345 uint32_t min_address = 0;
2346 uint32_t max_address = 0xffffffff;
2347 int i;
2348 struct image image;
2350 int retval = CALL_COMMAND_HANDLER(parse_load_image_command_CMD_ARGV,
2351 &image, &min_address, &max_address);
2352 if (ERROR_OK != retval)
2353 return retval;
2355 struct target *target = get_current_target(CMD_CTX);
2357 struct duration bench;
2358 duration_start(&bench);
2360 if (image_open(&image, CMD_ARGV[0], (CMD_ARGC >= 3) ? CMD_ARGV[2] : NULL) != ERROR_OK)
2362 return ERROR_OK;
2365 image_size = 0x0;
2366 retval = ERROR_OK;
2367 for (i = 0; i < image.num_sections; i++)
2369 buffer = malloc(image.sections[i].size);
2370 if (buffer == NULL)
2372 command_print(CMD_CTX,
2373 "error allocating buffer for section (%d bytes)",
2374 (int)(image.sections[i].size));
2375 break;
2378 if ((retval = image_read_section(&image, i, 0x0, image.sections[i].size, buffer, &buf_cnt)) != ERROR_OK)
2380 free(buffer);
2381 break;
2384 uint32_t offset = 0;
2385 uint32_t length = buf_cnt;
2387 /* DANGER!!! beware of unsigned comparision here!!! */
2389 if ((image.sections[i].base_address + buf_cnt >= min_address)&&
2390 (image.sections[i].base_address < max_address))
2392 if (image.sections[i].base_address < min_address)
2394 /* clip addresses below */
2395 offset += min_address-image.sections[i].base_address;
2396 length -= offset;
2399 if (image.sections[i].base_address + buf_cnt > max_address)
2401 length -= (image.sections[i].base_address + buf_cnt)-max_address;
2404 if ((retval = target_write_buffer(target, image.sections[i].base_address + offset, length, buffer + offset)) != ERROR_OK)
2406 free(buffer);
2407 break;
2409 image_size += length;
2410 command_print(CMD_CTX, "%u bytes written at address 0x%8.8" PRIx32 "",
2411 (unsigned int)length,
2412 image.sections[i].base_address + offset);
2415 free(buffer);
2418 if ((ERROR_OK == retval) && (duration_measure(&bench) == ERROR_OK))
2420 command_print(CMD_CTX, "downloaded %" PRIu32 " bytes "
2421 "in %fs (%0.3f kb/s)", image_size,
2422 duration_elapsed(&bench), duration_kbps(&bench, image_size));
2425 image_close(&image);
2427 return retval;
2431 COMMAND_HANDLER(handle_dump_image_command)
2433 struct fileio fileio;
2435 uint8_t buffer[560];
2436 int retvaltemp;
2439 struct target *target = get_current_target(CMD_CTX);
2441 if (CMD_ARGC != 3)
2443 command_print(CMD_CTX, "usage: dump_image <filename> <address> <size>");
2444 return ERROR_OK;
2447 uint32_t address;
2448 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], address);
2449 uint32_t size;
2450 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[2], size);
2452 if (fileio_open(&fileio, CMD_ARGV[0], FILEIO_WRITE, FILEIO_BINARY) != ERROR_OK)
2454 return ERROR_OK;
2457 struct duration bench;
2458 duration_start(&bench);
2460 int retval = ERROR_OK;
2461 while (size > 0)
2463 size_t size_written;
2464 uint32_t this_run_size = (size > 560) ? 560 : size;
2465 retval = target_read_buffer(target, address, this_run_size, buffer);
2466 if (retval != ERROR_OK)
2468 break;
2471 retval = fileio_write(&fileio, this_run_size, buffer, &size_written);
2472 if (retval != ERROR_OK)
2474 break;
2477 size -= this_run_size;
2478 address += this_run_size;
2481 if ((retvaltemp = fileio_close(&fileio)) != ERROR_OK)
2482 return retvaltemp;
2484 if ((ERROR_OK == retval) && (duration_measure(&bench) == ERROR_OK))
2486 command_print(CMD_CTX,
2487 "dumped %zu bytes in %fs (%0.3f kb/s)", fileio.size,
2488 duration_elapsed(&bench), duration_kbps(&bench, fileio.size));
2491 return retval;
2494 static COMMAND_HELPER(handle_verify_image_command_internal, int verify)
2496 uint8_t *buffer;
2497 size_t buf_cnt;
2498 uint32_t image_size;
2499 int i;
2500 int retval;
2501 uint32_t checksum = 0;
2502 uint32_t mem_checksum = 0;
2504 struct image image;
2506 struct target *target = get_current_target(CMD_CTX);
2508 if (CMD_ARGC < 1)
2510 return ERROR_COMMAND_SYNTAX_ERROR;
2513 if (!target)
2515 LOG_ERROR("no target selected");
2516 return ERROR_FAIL;
2519 struct duration bench;
2520 duration_start(&bench);
2522 if (CMD_ARGC >= 2)
2524 uint32_t addr;
2525 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], addr);
2526 image.base_address = addr;
2527 image.base_address_set = 1;
2529 else
2531 image.base_address_set = 0;
2532 image.base_address = 0x0;
2535 image.start_address_set = 0;
2537 if ((retval = image_open(&image, CMD_ARGV[0], (CMD_ARGC == 3) ? CMD_ARGV[2] : NULL)) != ERROR_OK)
2539 return retval;
2542 image_size = 0x0;
2543 retval = ERROR_OK;
2544 for (i = 0; i < image.num_sections; i++)
2546 buffer = malloc(image.sections[i].size);
2547 if (buffer == NULL)
2549 command_print(CMD_CTX,
2550 "error allocating buffer for section (%d bytes)",
2551 (int)(image.sections[i].size));
2552 break;
2554 if ((retval = image_read_section(&image, i, 0x0, image.sections[i].size, buffer, &buf_cnt)) != ERROR_OK)
2556 free(buffer);
2557 break;
2560 if (verify)
2562 /* calculate checksum of image */
2563 image_calculate_checksum(buffer, buf_cnt, &checksum);
2565 retval = target_checksum_memory(target, image.sections[i].base_address, buf_cnt, &mem_checksum);
2566 if (retval != ERROR_OK)
2568 free(buffer);
2569 break;
2572 if (checksum != mem_checksum)
2574 /* failed crc checksum, fall back to a binary compare */
2575 uint8_t *data;
2577 command_print(CMD_CTX, "checksum mismatch - attempting binary compare");
2579 data = (uint8_t*)malloc(buf_cnt);
2581 /* Can we use 32bit word accesses? */
2582 int size = 1;
2583 int count = buf_cnt;
2584 if ((count % 4) == 0)
2586 size *= 4;
2587 count /= 4;
2589 retval = target_read_memory(target, image.sections[i].base_address, size, count, data);
2590 if (retval == ERROR_OK)
2592 uint32_t t;
2593 for (t = 0; t < buf_cnt; t++)
2595 if (data[t] != buffer[t])
2597 command_print(CMD_CTX,
2598 "Verify operation failed address 0x%08x. Was 0x%02x instead of 0x%02x\n",
2599 (unsigned)(t + image.sections[i].base_address),
2600 data[t],
2601 buffer[t]);
2602 free(data);
2603 free(buffer);
2604 retval = ERROR_FAIL;
2605 goto done;
2607 if ((t%16384) == 0)
2609 keep_alive();
2614 free(data);
2616 } else
2618 command_print(CMD_CTX, "address 0x%08" PRIx32 " length 0x%08zx",
2619 image.sections[i].base_address,
2620 buf_cnt);
2623 free(buffer);
2624 image_size += buf_cnt;
2626 done:
2627 if ((ERROR_OK == retval) && (duration_measure(&bench) == ERROR_OK))
2629 command_print(CMD_CTX, "verified %" PRIu32 " bytes "
2630 "in %fs (%0.3f kb/s)", image_size,
2631 duration_elapsed(&bench), duration_kbps(&bench, image_size));
2634 image_close(&image);
2636 return retval;
2639 COMMAND_HANDLER(handle_verify_image_command)
2641 return CALL_COMMAND_HANDLER(handle_verify_image_command_internal, 1);
2644 COMMAND_HANDLER(handle_test_image_command)
2646 return CALL_COMMAND_HANDLER(handle_verify_image_command_internal, 0);
2649 static int handle_bp_command_list(struct command_context *cmd_ctx)
2651 struct target *target = get_current_target(cmd_ctx);
2652 struct breakpoint *breakpoint = target->breakpoints;
2653 while (breakpoint)
2655 if (breakpoint->type == BKPT_SOFT)
2657 char* buf = buf_to_str(breakpoint->orig_instr,
2658 breakpoint->length, 16);
2659 command_print(cmd_ctx, "0x%8.8" PRIx32 ", 0x%x, %i, 0x%s",
2660 breakpoint->address,
2661 breakpoint->length,
2662 breakpoint->set, buf);
2663 free(buf);
2665 else
2667 command_print(cmd_ctx, "0x%8.8" PRIx32 ", 0x%x, %i",
2668 breakpoint->address,
2669 breakpoint->length, breakpoint->set);
2672 breakpoint = breakpoint->next;
2674 return ERROR_OK;
2677 static int handle_bp_command_set(struct command_context *cmd_ctx,
2678 uint32_t addr, uint32_t length, int hw)
2680 struct target *target = get_current_target(cmd_ctx);
2681 int retval = breakpoint_add(target, addr, length, hw);
2682 if (ERROR_OK == retval)
2683 command_print(cmd_ctx, "breakpoint set at 0x%8.8" PRIx32 "", addr);
2684 else
2685 LOG_ERROR("Failure setting breakpoint");
2686 return retval;
2689 COMMAND_HANDLER(handle_bp_command)
2691 if (CMD_ARGC == 0)
2692 return handle_bp_command_list(CMD_CTX);
2694 if (CMD_ARGC < 2 || CMD_ARGC > 3)
2696 command_print(CMD_CTX, "usage: bp <address> <length> ['hw']");
2697 return ERROR_COMMAND_SYNTAX_ERROR;
2700 uint32_t addr;
2701 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], addr);
2702 uint32_t length;
2703 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], length);
2705 int hw = BKPT_SOFT;
2706 if (CMD_ARGC == 3)
2708 if (strcmp(CMD_ARGV[2], "hw") == 0)
2709 hw = BKPT_HARD;
2710 else
2711 return ERROR_COMMAND_SYNTAX_ERROR;
2714 return handle_bp_command_set(CMD_CTX, addr, length, hw);
2717 COMMAND_HANDLER(handle_rbp_command)
2719 if (CMD_ARGC != 1)
2720 return ERROR_COMMAND_SYNTAX_ERROR;
2722 uint32_t addr;
2723 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], addr);
2725 struct target *target = get_current_target(CMD_CTX);
2726 breakpoint_remove(target, addr);
2728 return ERROR_OK;
2731 COMMAND_HANDLER(handle_wp_command)
2733 struct target *target = get_current_target(CMD_CTX);
2735 if (CMD_ARGC == 0)
2737 struct watchpoint *watchpoint = target->watchpoints;
2739 while (watchpoint)
2741 command_print(CMD_CTX, "address: 0x%8.8" PRIx32
2742 ", len: 0x%8.8" PRIx32
2743 ", r/w/a: %i, value: 0x%8.8" PRIx32
2744 ", mask: 0x%8.8" PRIx32,
2745 watchpoint->address,
2746 watchpoint->length,
2747 (int)watchpoint->rw,
2748 watchpoint->value,
2749 watchpoint->mask);
2750 watchpoint = watchpoint->next;
2752 return ERROR_OK;
2755 enum watchpoint_rw type = WPT_ACCESS;
2756 uint32_t addr = 0;
2757 uint32_t length = 0;
2758 uint32_t data_value = 0x0;
2759 uint32_t data_mask = 0xffffffff;
2761 switch (CMD_ARGC)
2763 case 5:
2764 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[4], data_mask);
2765 // fall through
2766 case 4:
2767 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[3], data_value);
2768 // fall through
2769 case 3:
2770 switch (CMD_ARGV[2][0])
2772 case 'r':
2773 type = WPT_READ;
2774 break;
2775 case 'w':
2776 type = WPT_WRITE;
2777 break;
2778 case 'a':
2779 type = WPT_ACCESS;
2780 break;
2781 default:
2782 LOG_ERROR("invalid watchpoint mode ('%c')", CMD_ARGV[2][0]);
2783 return ERROR_COMMAND_SYNTAX_ERROR;
2785 // fall through
2786 case 2:
2787 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], length);
2788 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], addr);
2789 break;
2791 default:
2792 command_print(CMD_CTX, "usage: wp [address length "
2793 "[(r|w|a) [value [mask]]]]");
2794 return ERROR_COMMAND_SYNTAX_ERROR;
2797 int retval = watchpoint_add(target, addr, length, type,
2798 data_value, data_mask);
2799 if (ERROR_OK != retval)
2800 LOG_ERROR("Failure setting watchpoints");
2802 return retval;
2805 COMMAND_HANDLER(handle_rwp_command)
2807 if (CMD_ARGC != 1)
2808 return ERROR_COMMAND_SYNTAX_ERROR;
2810 uint32_t addr;
2811 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], addr);
2813 struct target *target = get_current_target(CMD_CTX);
2814 watchpoint_remove(target, addr);
2816 return ERROR_OK;
2821 * Translate a virtual address to a physical address.
2823 * The low-level target implementation must have logged a detailed error
2824 * which is forwarded to telnet/GDB session.
2826 COMMAND_HANDLER(handle_virt2phys_command)
2828 if (CMD_ARGC != 1)
2829 return ERROR_COMMAND_SYNTAX_ERROR;
2831 uint32_t va;
2832 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], va);
2833 uint32_t pa;
2835 struct target *target = get_current_target(CMD_CTX);
2836 int retval = target->type->virt2phys(target, va, &pa);
2837 if (retval == ERROR_OK)
2838 command_print(CMD_CTX, "Physical address 0x%08" PRIx32 "", pa);
2840 return retval;
2843 static void writeData(FILE *f, const void *data, size_t len)
2845 size_t written = fwrite(data, 1, len, f);
2846 if (written != len)
2847 LOG_ERROR("failed to write %zu bytes: %s", len, strerror(errno));
2850 static void writeLong(FILE *f, int l)
2852 int i;
2853 for (i = 0; i < 4; i++)
2855 char c = (l >> (i*8))&0xff;
2856 writeData(f, &c, 1);
2861 static void writeString(FILE *f, char *s)
2863 writeData(f, s, strlen(s));
2866 /* Dump a gmon.out histogram file. */
2867 static void writeGmon(uint32_t *samples, uint32_t sampleNum, const char *filename)
2869 uint32_t i;
2870 FILE *f = fopen(filename, "w");
2871 if (f == NULL)
2872 return;
2873 writeString(f, "gmon");
2874 writeLong(f, 0x00000001); /* Version */
2875 writeLong(f, 0); /* padding */
2876 writeLong(f, 0); /* padding */
2877 writeLong(f, 0); /* padding */
2879 uint8_t zero = 0; /* GMON_TAG_TIME_HIST */
2880 writeData(f, &zero, 1);
2882 /* figure out bucket size */
2883 uint32_t min = samples[0];
2884 uint32_t max = samples[0];
2885 for (i = 0; i < sampleNum; i++)
2887 if (min > samples[i])
2889 min = samples[i];
2891 if (max < samples[i])
2893 max = samples[i];
2897 int addressSpace = (max-min + 1);
2899 static const uint32_t maxBuckets = 256 * 1024; /* maximum buckets. */
2900 uint32_t length = addressSpace;
2901 if (length > maxBuckets)
2903 length = maxBuckets;
2905 int *buckets = malloc(sizeof(int)*length);
2906 if (buckets == NULL)
2908 fclose(f);
2909 return;
2911 memset(buckets, 0, sizeof(int)*length);
2912 for (i = 0; i < sampleNum;i++)
2914 uint32_t address = samples[i];
2915 long long a = address-min;
2916 long long b = length-1;
2917 long long c = addressSpace-1;
2918 int index = (a*b)/c; /* danger!!!! int32 overflows */
2919 buckets[index]++;
2922 /* append binary memory gmon.out &profile_hist_hdr ((char*)&profile_hist_hdr + sizeof(struct gmon_hist_hdr)) */
2923 writeLong(f, min); /* low_pc */
2924 writeLong(f, max); /* high_pc */
2925 writeLong(f, length); /* # of samples */
2926 writeLong(f, 64000000); /* 64MHz */
2927 writeString(f, "seconds");
2928 for (i = 0; i < (15-strlen("seconds")); i++)
2929 writeData(f, &zero, 1);
2930 writeString(f, "s");
2932 /*append binary memory gmon.out profile_hist_data (profile_hist_data + profile_hist_hdr.hist_size) */
2934 char *data = malloc(2*length);
2935 if (data != NULL)
2937 for (i = 0; i < length;i++)
2939 int val;
2940 val = buckets[i];
2941 if (val > 65535)
2943 val = 65535;
2945 data[i*2]=val&0xff;
2946 data[i*2 + 1]=(val >> 8)&0xff;
2948 free(buckets);
2949 writeData(f, data, length * 2);
2950 free(data);
2951 } else
2953 free(buckets);
2956 fclose(f);
2959 /* profiling samples the CPU PC as quickly as OpenOCD is able, which will be used as a random sampling of PC */
2960 COMMAND_HANDLER(handle_profile_command)
2962 struct target *target = get_current_target(CMD_CTX);
2963 struct timeval timeout, now;
2965 gettimeofday(&timeout, NULL);
2966 if (CMD_ARGC != 2)
2968 return ERROR_COMMAND_SYNTAX_ERROR;
2970 unsigned offset;
2971 COMMAND_PARSE_NUMBER(uint, CMD_ARGV[0], offset);
2973 timeval_add_time(&timeout, offset, 0);
2975 command_print(CMD_CTX, "Starting profiling. Halting and resuming the target as often as we can...");
2977 static const int maxSample = 10000;
2978 uint32_t *samples = malloc(sizeof(uint32_t)*maxSample);
2979 if (samples == NULL)
2980 return ERROR_OK;
2982 int numSamples = 0;
2983 /* hopefully it is safe to cache! We want to stop/restart as quickly as possible. */
2984 struct reg *reg = register_get_by_name(target->reg_cache, "pc", 1);
2986 for (;;)
2988 int retval;
2989 target_poll(target);
2990 if (target->state == TARGET_HALTED)
2992 uint32_t t=*((uint32_t *)reg->value);
2993 samples[numSamples++]=t;
2994 retval = target_resume(target, 1, 0, 0, 0); /* current pc, addr = 0, do not handle breakpoints, not debugging */
2995 target_poll(target);
2996 alive_sleep(10); /* sleep 10ms, i.e. <100 samples/second. */
2997 } else if (target->state == TARGET_RUNNING)
2999 /* We want to quickly sample the PC. */
3000 if ((retval = target_halt(target)) != ERROR_OK)
3002 free(samples);
3003 return retval;
3005 } else
3007 command_print(CMD_CTX, "Target not halted or running");
3008 retval = ERROR_OK;
3009 break;
3011 if (retval != ERROR_OK)
3013 break;
3016 gettimeofday(&now, NULL);
3017 if ((numSamples >= maxSample) || ((now.tv_sec >= timeout.tv_sec) && (now.tv_usec >= timeout.tv_usec)))
3019 command_print(CMD_CTX, "Profiling completed. %d samples.", numSamples);
3020 if ((retval = target_poll(target)) != ERROR_OK)
3022 free(samples);
3023 return retval;
3025 if (target->state == TARGET_HALTED)
3027 target_resume(target, 1, 0, 0, 0); /* current pc, addr = 0, do not handle breakpoints, not debugging */
3029 if ((retval = target_poll(target)) != ERROR_OK)
3031 free(samples);
3032 return retval;
3034 writeGmon(samples, numSamples, CMD_ARGV[1]);
3035 command_print(CMD_CTX, "Wrote %s", CMD_ARGV[1]);
3036 break;
3039 free(samples);
3041 return ERROR_OK;
3044 static int new_int_array_element(Jim_Interp * interp, const char *varname, int idx, uint32_t val)
3046 char *namebuf;
3047 Jim_Obj *nameObjPtr, *valObjPtr;
3048 int result;
3050 namebuf = alloc_printf("%s(%d)", varname, idx);
3051 if (!namebuf)
3052 return JIM_ERR;
3054 nameObjPtr = Jim_NewStringObj(interp, namebuf, -1);
3055 valObjPtr = Jim_NewIntObj(interp, val);
3056 if (!nameObjPtr || !valObjPtr)
3058 free(namebuf);
3059 return JIM_ERR;
3062 Jim_IncrRefCount(nameObjPtr);
3063 Jim_IncrRefCount(valObjPtr);
3064 result = Jim_SetVariable(interp, nameObjPtr, valObjPtr);
3065 Jim_DecrRefCount(interp, nameObjPtr);
3066 Jim_DecrRefCount(interp, valObjPtr);
3067 free(namebuf);
3068 /* printf("%s(%d) <= 0%08x\n", varname, idx, val); */
3069 return result;
3072 static int jim_mem2array(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
3074 struct command_context *context;
3075 struct target *target;
3077 context = Jim_GetAssocData(interp, "context");
3078 if (context == NULL)
3080 LOG_ERROR("mem2array: no command context");
3081 return JIM_ERR;
3083 target = get_current_target(context);
3084 if (target == NULL)
3086 LOG_ERROR("mem2array: no current target");
3087 return JIM_ERR;
3090 return target_mem2array(interp, target, argc-1, argv + 1);
3093 static int target_mem2array(Jim_Interp *interp, struct target *target, int argc, Jim_Obj *const *argv)
3095 long l;
3096 uint32_t width;
3097 int len;
3098 uint32_t addr;
3099 uint32_t count;
3100 uint32_t v;
3101 const char *varname;
3102 int n, e, retval;
3103 uint32_t i;
3105 /* argv[1] = name of array to receive the data
3106 * argv[2] = desired width
3107 * argv[3] = memory address
3108 * argv[4] = count of times to read
3110 if (argc != 4) {
3111 Jim_WrongNumArgs(interp, 1, argv, "varname width addr nelems");
3112 return JIM_ERR;
3114 varname = Jim_GetString(argv[0], &len);
3115 /* given "foo" get space for worse case "foo(%d)" .. add 20 */
3117 e = Jim_GetLong(interp, argv[1], &l);
3118 width = l;
3119 if (e != JIM_OK) {
3120 return e;
3123 e = Jim_GetLong(interp, argv[2], &l);
3124 addr = l;
3125 if (e != JIM_OK) {
3126 return e;
3128 e = Jim_GetLong(interp, argv[3], &l);
3129 len = l;
3130 if (e != JIM_OK) {
3131 return e;
3133 switch (width) {
3134 case 8:
3135 width = 1;
3136 break;
3137 case 16:
3138 width = 2;
3139 break;
3140 case 32:
3141 width = 4;
3142 break;
3143 default:
3144 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3145 Jim_AppendStrings(interp, Jim_GetResult(interp), "Invalid width param, must be 8/16/32", NULL);
3146 return JIM_ERR;
3148 if (len == 0) {
3149 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3150 Jim_AppendStrings(interp, Jim_GetResult(interp), "mem2array: zero width read?", NULL);
3151 return JIM_ERR;
3153 if ((addr + (len * width)) < addr) {
3154 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3155 Jim_AppendStrings(interp, Jim_GetResult(interp), "mem2array: addr + len - wraps to zero?", NULL);
3156 return JIM_ERR;
3158 /* absurd transfer size? */
3159 if (len > 65536) {
3160 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3161 Jim_AppendStrings(interp, Jim_GetResult(interp), "mem2array: absurd > 64K item request", NULL);
3162 return JIM_ERR;
3165 if ((width == 1) ||
3166 ((width == 2) && ((addr & 1) == 0)) ||
3167 ((width == 4) && ((addr & 3) == 0))) {
3168 /* all is well */
3169 } else {
3170 char buf[100];
3171 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3172 sprintf(buf, "mem2array address: 0x%08" PRIx32 " is not aligned for %" PRId32 " byte reads",
3173 addr,
3174 width);
3175 Jim_AppendStrings(interp, Jim_GetResult(interp), buf , NULL);
3176 return JIM_ERR;
3179 /* Transfer loop */
3181 /* index counter */
3182 n = 0;
3184 size_t buffersize = 4096;
3185 uint8_t *buffer = malloc(buffersize);
3186 if (buffer == NULL)
3187 return JIM_ERR;
3189 /* assume ok */
3190 e = JIM_OK;
3191 while (len) {
3192 /* Slurp... in buffer size chunks */
3194 count = len; /* in objects.. */
3195 if (count > (buffersize/width)) {
3196 count = (buffersize/width);
3199 retval = target_read_memory(target, addr, width, count, buffer);
3200 if (retval != ERROR_OK) {
3201 /* BOO !*/
3202 LOG_ERROR("mem2array: Read @ 0x%08x, w=%d, cnt=%d, failed",
3203 (unsigned int)addr,
3204 (int)width,
3205 (int)count);
3206 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3207 Jim_AppendStrings(interp, Jim_GetResult(interp), "mem2array: cannot read memory", NULL);
3208 e = JIM_ERR;
3209 len = 0;
3210 } else {
3211 v = 0; /* shut up gcc */
3212 for (i = 0 ;i < count ;i++, n++) {
3213 switch (width) {
3214 case 4:
3215 v = target_buffer_get_u32(target, &buffer[i*width]);
3216 break;
3217 case 2:
3218 v = target_buffer_get_u16(target, &buffer[i*width]);
3219 break;
3220 case 1:
3221 v = buffer[i] & 0x0ff;
3222 break;
3224 new_int_array_element(interp, varname, n, v);
3226 len -= count;
3230 free(buffer);
3232 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3234 return JIM_OK;
3237 static int get_int_array_element(Jim_Interp * interp, const char *varname, int idx, uint32_t *val)
3239 char *namebuf;
3240 Jim_Obj *nameObjPtr, *valObjPtr;
3241 int result;
3242 long l;
3244 namebuf = alloc_printf("%s(%d)", varname, idx);
3245 if (!namebuf)
3246 return JIM_ERR;
3248 nameObjPtr = Jim_NewStringObj(interp, namebuf, -1);
3249 if (!nameObjPtr)
3251 free(namebuf);
3252 return JIM_ERR;
3255 Jim_IncrRefCount(nameObjPtr);
3256 valObjPtr = Jim_GetVariable(interp, nameObjPtr, JIM_ERRMSG);
3257 Jim_DecrRefCount(interp, nameObjPtr);
3258 free(namebuf);
3259 if (valObjPtr == NULL)
3260 return JIM_ERR;
3262 result = Jim_GetLong(interp, valObjPtr, &l);
3263 /* printf("%s(%d) => 0%08x\n", varname, idx, val); */
3264 *val = l;
3265 return result;
3268 static int jim_array2mem(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
3270 struct command_context *context;
3271 struct target *target;
3273 context = Jim_GetAssocData(interp, "context");
3274 if (context == NULL) {
3275 LOG_ERROR("array2mem: no command context");
3276 return JIM_ERR;
3278 target = get_current_target(context);
3279 if (target == NULL) {
3280 LOG_ERROR("array2mem: no current target");
3281 return JIM_ERR;
3284 return target_array2mem(interp,target, argc-1, argv + 1);
3286 static int target_array2mem(Jim_Interp *interp, struct target *target, int argc, Jim_Obj *const *argv)
3288 long l;
3289 uint32_t width;
3290 int len;
3291 uint32_t addr;
3292 uint32_t count;
3293 uint32_t v;
3294 const char *varname;
3295 int n, e, retval;
3296 uint32_t i;
3298 /* argv[1] = name of array to get the data
3299 * argv[2] = desired width
3300 * argv[3] = memory address
3301 * argv[4] = count to write
3303 if (argc != 4) {
3304 Jim_WrongNumArgs(interp, 0, argv, "varname width addr nelems");
3305 return JIM_ERR;
3307 varname = Jim_GetString(argv[0], &len);
3308 /* given "foo" get space for worse case "foo(%d)" .. add 20 */
3310 e = Jim_GetLong(interp, argv[1], &l);
3311 width = l;
3312 if (e != JIM_OK) {
3313 return e;
3316 e = Jim_GetLong(interp, argv[2], &l);
3317 addr = l;
3318 if (e != JIM_OK) {
3319 return e;
3321 e = Jim_GetLong(interp, argv[3], &l);
3322 len = l;
3323 if (e != JIM_OK) {
3324 return e;
3326 switch (width) {
3327 case 8:
3328 width = 1;
3329 break;
3330 case 16:
3331 width = 2;
3332 break;
3333 case 32:
3334 width = 4;
3335 break;
3336 default:
3337 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3338 Jim_AppendStrings(interp, Jim_GetResult(interp), "Invalid width param, must be 8/16/32", NULL);
3339 return JIM_ERR;
3341 if (len == 0) {
3342 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3343 Jim_AppendStrings(interp, Jim_GetResult(interp), "array2mem: zero width read?", NULL);
3344 return JIM_ERR;
3346 if ((addr + (len * width)) < addr) {
3347 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3348 Jim_AppendStrings(interp, Jim_GetResult(interp), "array2mem: addr + len - wraps to zero?", NULL);
3349 return JIM_ERR;
3351 /* absurd transfer size? */
3352 if (len > 65536) {
3353 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3354 Jim_AppendStrings(interp, Jim_GetResult(interp), "array2mem: absurd > 64K item request", NULL);
3355 return JIM_ERR;
3358 if ((width == 1) ||
3359 ((width == 2) && ((addr & 1) == 0)) ||
3360 ((width == 4) && ((addr & 3) == 0))) {
3361 /* all is well */
3362 } else {
3363 char buf[100];
3364 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3365 sprintf(buf, "array2mem address: 0x%08x is not aligned for %d byte reads",
3366 (unsigned int)addr,
3367 (int)width);
3368 Jim_AppendStrings(interp, Jim_GetResult(interp), buf , NULL);
3369 return JIM_ERR;
3372 /* Transfer loop */
3374 /* index counter */
3375 n = 0;
3376 /* assume ok */
3377 e = JIM_OK;
3379 size_t buffersize = 4096;
3380 uint8_t *buffer = malloc(buffersize);
3381 if (buffer == NULL)
3382 return JIM_ERR;
3384 while (len) {
3385 /* Slurp... in buffer size chunks */
3387 count = len; /* in objects.. */
3388 if (count > (buffersize/width)) {
3389 count = (buffersize/width);
3392 v = 0; /* shut up gcc */
3393 for (i = 0 ;i < count ;i++, n++) {
3394 get_int_array_element(interp, varname, n, &v);
3395 switch (width) {
3396 case 4:
3397 target_buffer_set_u32(target, &buffer[i*width], v);
3398 break;
3399 case 2:
3400 target_buffer_set_u16(target, &buffer[i*width], v);
3401 break;
3402 case 1:
3403 buffer[i] = v & 0x0ff;
3404 break;
3407 len -= count;
3409 retval = target_write_memory(target, addr, width, count, buffer);
3410 if (retval != ERROR_OK) {
3411 /* BOO !*/
3412 LOG_ERROR("array2mem: Write @ 0x%08x, w=%d, cnt=%d, failed",
3413 (unsigned int)addr,
3414 (int)width,
3415 (int)count);
3416 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3417 Jim_AppendStrings(interp, Jim_GetResult(interp), "array2mem: cannot read memory", NULL);
3418 e = JIM_ERR;
3419 len = 0;
3423 free(buffer);
3425 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3427 return JIM_OK;
3430 void target_all_handle_event(enum target_event e)
3432 struct target *target;
3434 LOG_DEBUG("**all*targets: event: %d, %s",
3435 (int)e,
3436 Jim_Nvp_value2name_simple(nvp_target_event, e)->name);
3438 target = all_targets;
3439 while (target) {
3440 target_handle_event(target, e);
3441 target = target->next;
3446 /* FIX? should we propagate errors here rather than printing them
3447 * and continuing?
3449 void target_handle_event(struct target *target, enum target_event e)
3451 struct target_event_action *teap;
3453 for (teap = target->event_action; teap != NULL; teap = teap->next) {
3454 if (teap->event == e) {
3455 LOG_DEBUG("target: (%d) %s (%s) event: %d (%s) action: %s",
3456 target->target_number,
3457 target_name(target),
3458 target_type_name(target),
3460 Jim_Nvp_value2name_simple(nvp_target_event, e)->name,
3461 Jim_GetString(teap->body, NULL));
3462 if (Jim_EvalObj(teap->interp, teap->body) != JIM_OK)
3464 Jim_PrintErrorMessage(teap->interp);
3471 * Returns true only if the target has a handler for the specified event.
3473 bool target_has_event_action(struct target *target, enum target_event event)
3475 struct target_event_action *teap;
3477 for (teap = target->event_action; teap != NULL; teap = teap->next) {
3478 if (teap->event == event)
3479 return true;
3481 return false;
3484 enum target_cfg_param {
3485 TCFG_TYPE,
3486 TCFG_EVENT,
3487 TCFG_WORK_AREA_VIRT,
3488 TCFG_WORK_AREA_PHYS,
3489 TCFG_WORK_AREA_SIZE,
3490 TCFG_WORK_AREA_BACKUP,
3491 TCFG_ENDIAN,
3492 TCFG_VARIANT,
3493 TCFG_CHAIN_POSITION,
3496 static Jim_Nvp nvp_config_opts[] = {
3497 { .name = "-type", .value = TCFG_TYPE },
3498 { .name = "-event", .value = TCFG_EVENT },
3499 { .name = "-work-area-virt", .value = TCFG_WORK_AREA_VIRT },
3500 { .name = "-work-area-phys", .value = TCFG_WORK_AREA_PHYS },
3501 { .name = "-work-area-size", .value = TCFG_WORK_AREA_SIZE },
3502 { .name = "-work-area-backup", .value = TCFG_WORK_AREA_BACKUP },
3503 { .name = "-endian" , .value = TCFG_ENDIAN },
3504 { .name = "-variant", .value = TCFG_VARIANT },
3505 { .name = "-chain-position", .value = TCFG_CHAIN_POSITION },
3507 { .name = NULL, .value = -1 }
3510 static int target_configure(Jim_GetOptInfo *goi, struct target *target)
3512 Jim_Nvp *n;
3513 Jim_Obj *o;
3514 jim_wide w;
3515 char *cp;
3516 int e;
3518 /* parse config or cget options ... */
3519 while (goi->argc > 0) {
3520 Jim_SetEmptyResult(goi->interp);
3521 /* Jim_GetOpt_Debug(goi); */
3523 if (target->type->target_jim_configure) {
3524 /* target defines a configure function */
3525 /* target gets first dibs on parameters */
3526 e = (*(target->type->target_jim_configure))(target, goi);
3527 if (e == JIM_OK) {
3528 /* more? */
3529 continue;
3531 if (e == JIM_ERR) {
3532 /* An error */
3533 return e;
3535 /* otherwise we 'continue' below */
3537 e = Jim_GetOpt_Nvp(goi, nvp_config_opts, &n);
3538 if (e != JIM_OK) {
3539 Jim_GetOpt_NvpUnknown(goi, nvp_config_opts, 0);
3540 return e;
3542 switch (n->value) {
3543 case TCFG_TYPE:
3544 /* not setable */
3545 if (goi->isconfigure) {
3546 Jim_SetResult_sprintf(goi->interp,
3547 "not settable: %s", n->name);
3548 return JIM_ERR;
3549 } else {
3550 no_params:
3551 if (goi->argc != 0) {
3552 Jim_WrongNumArgs(goi->interp,
3553 goi->argc, goi->argv,
3554 "NO PARAMS");
3555 return JIM_ERR;
3558 Jim_SetResultString(goi->interp,
3559 target_type_name(target), -1);
3560 /* loop for more */
3561 break;
3562 case TCFG_EVENT:
3563 if (goi->argc == 0) {
3564 Jim_WrongNumArgs(goi->interp, goi->argc, goi->argv, "-event ?event-name? ...");
3565 return JIM_ERR;
3568 e = Jim_GetOpt_Nvp(goi, nvp_target_event, &n);
3569 if (e != JIM_OK) {
3570 Jim_GetOpt_NvpUnknown(goi, nvp_target_event, 1);
3571 return e;
3574 if (goi->isconfigure) {
3575 if (goi->argc != 1) {
3576 Jim_WrongNumArgs(goi->interp, goi->argc, goi->argv, "-event ?event-name? ?EVENT-BODY?");
3577 return JIM_ERR;
3579 } else {
3580 if (goi->argc != 0) {
3581 Jim_WrongNumArgs(goi->interp, goi->argc, goi->argv, "-event ?event-name?");
3582 return JIM_ERR;
3587 struct target_event_action *teap;
3589 teap = target->event_action;
3590 /* replace existing? */
3591 while (teap) {
3592 if (teap->event == (enum target_event)n->value) {
3593 break;
3595 teap = teap->next;
3598 if (goi->isconfigure) {
3599 bool replace = true;
3600 if (teap == NULL) {
3601 /* create new */
3602 teap = calloc(1, sizeof(*teap));
3603 replace = false;
3605 teap->event = n->value;
3606 teap->interp = goi->interp;
3607 Jim_GetOpt_Obj(goi, &o);
3608 if (teap->body) {
3609 Jim_DecrRefCount(teap->interp, teap->body);
3611 teap->body = Jim_DuplicateObj(goi->interp, o);
3613 * FIXME:
3614 * Tcl/TK - "tk events" have a nice feature.
3615 * See the "BIND" command.
3616 * We should support that here.
3617 * You can specify %X and %Y in the event code.
3618 * The idea is: %T - target name.
3619 * The idea is: %N - target number
3620 * The idea is: %E - event name.
3622 Jim_IncrRefCount(teap->body);
3624 if (!replace)
3626 /* add to head of event list */
3627 teap->next = target->event_action;
3628 target->event_action = teap;
3630 Jim_SetEmptyResult(goi->interp);
3631 } else {
3632 /* get */
3633 if (teap == NULL) {
3634 Jim_SetEmptyResult(goi->interp);
3635 } else {
3636 Jim_SetResult(goi->interp, Jim_DuplicateObj(goi->interp, teap->body));
3640 /* loop for more */
3641 break;
3643 case TCFG_WORK_AREA_VIRT:
3644 if (goi->isconfigure) {
3645 target_free_all_working_areas(target);
3646 e = Jim_GetOpt_Wide(goi, &w);
3647 if (e != JIM_OK) {
3648 return e;
3650 target->working_area_virt = w;
3651 target->working_area_virt_spec = true;
3652 } else {
3653 if (goi->argc != 0) {
3654 goto no_params;
3657 Jim_SetResult(goi->interp, Jim_NewIntObj(goi->interp, target->working_area_virt));
3658 /* loop for more */
3659 break;
3661 case TCFG_WORK_AREA_PHYS:
3662 if (goi->isconfigure) {
3663 target_free_all_working_areas(target);
3664 e = Jim_GetOpt_Wide(goi, &w);
3665 if (e != JIM_OK) {
3666 return e;
3668 target->working_area_phys = w;
3669 target->working_area_phys_spec = true;
3670 } else {
3671 if (goi->argc != 0) {
3672 goto no_params;
3675 Jim_SetResult(goi->interp, Jim_NewIntObj(goi->interp, target->working_area_phys));
3676 /* loop for more */
3677 break;
3679 case TCFG_WORK_AREA_SIZE:
3680 if (goi->isconfigure) {
3681 target_free_all_working_areas(target);
3682 e = Jim_GetOpt_Wide(goi, &w);
3683 if (e != JIM_OK) {
3684 return e;
3686 target->working_area_size = w;
3687 } else {
3688 if (goi->argc != 0) {
3689 goto no_params;
3692 Jim_SetResult(goi->interp, Jim_NewIntObj(goi->interp, target->working_area_size));
3693 /* loop for more */
3694 break;
3696 case TCFG_WORK_AREA_BACKUP:
3697 if (goi->isconfigure) {
3698 target_free_all_working_areas(target);
3699 e = Jim_GetOpt_Wide(goi, &w);
3700 if (e != JIM_OK) {
3701 return e;
3703 /* make this exactly 1 or 0 */
3704 target->backup_working_area = (!!w);
3705 } else {
3706 if (goi->argc != 0) {
3707 goto no_params;
3710 Jim_SetResult(goi->interp, Jim_NewIntObj(goi->interp, target->backup_working_area));
3711 /* loop for more e*/
3712 break;
3714 case TCFG_ENDIAN:
3715 if (goi->isconfigure) {
3716 e = Jim_GetOpt_Nvp(goi, nvp_target_endian, &n);
3717 if (e != JIM_OK) {
3718 Jim_GetOpt_NvpUnknown(goi, nvp_target_endian, 1);
3719 return e;
3721 target->endianness = n->value;
3722 } else {
3723 if (goi->argc != 0) {
3724 goto no_params;
3727 n = Jim_Nvp_value2name_simple(nvp_target_endian, target->endianness);
3728 if (n->name == NULL) {
3729 target->endianness = TARGET_LITTLE_ENDIAN;
3730 n = Jim_Nvp_value2name_simple(nvp_target_endian, target->endianness);
3732 Jim_SetResultString(goi->interp, n->name, -1);
3733 /* loop for more */
3734 break;
3736 case TCFG_VARIANT:
3737 if (goi->isconfigure) {
3738 if (goi->argc < 1) {
3739 Jim_SetResult_sprintf(goi->interp,
3740 "%s ?STRING?",
3741 n->name);
3742 return JIM_ERR;
3744 if (target->variant) {
3745 free((void *)(target->variant));
3747 e = Jim_GetOpt_String(goi, &cp, NULL);
3748 target->variant = strdup(cp);
3749 } else {
3750 if (goi->argc != 0) {
3751 goto no_params;
3754 Jim_SetResultString(goi->interp, target->variant,-1);
3755 /* loop for more */
3756 break;
3757 case TCFG_CHAIN_POSITION:
3758 if (goi->isconfigure) {
3759 Jim_Obj *o;
3760 struct jtag_tap *tap;
3761 target_free_all_working_areas(target);
3762 e = Jim_GetOpt_Obj(goi, &o);
3763 if (e != JIM_OK) {
3764 return e;
3766 tap = jtag_tap_by_jim_obj(goi->interp, o);
3767 if (tap == NULL) {
3768 return JIM_ERR;
3770 /* make this exactly 1 or 0 */
3771 target->tap = tap;
3772 } else {
3773 if (goi->argc != 0) {
3774 goto no_params;
3777 Jim_SetResultString(goi->interp, target->tap->dotted_name, -1);
3778 /* loop for more e*/
3779 break;
3781 } /* while (goi->argc) */
3784 /* done - we return */
3785 return JIM_OK;
3788 static int jim_target_configure(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
3790 Jim_GetOptInfo goi;
3791 Jim_GetOpt_Setup(&goi, interp, argc - 1, argv + 1);
3792 goi.isconfigure = strcmp(Jim_GetString(argv[0], NULL), "configure") == 0;
3793 int need_args = 1 + goi.isconfigure;
3794 if (goi.argc < need_args)
3796 Jim_WrongNumArgs(goi.interp, goi.argc, goi.argv,
3797 goi.isconfigure
3798 ? "missing: -option VALUE ..."
3799 : "missing: -option ...");
3800 return JIM_ERR;
3802 struct target *target = Jim_CmdPrivData(goi.interp);
3803 return target_configure(&goi, target);
3806 static int jim_target_mw(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
3808 const char *cmd_name = Jim_GetString(argv[0], NULL);
3810 Jim_GetOptInfo goi;
3811 Jim_GetOpt_Setup(&goi, interp, argc - 1, argv + 1);
3813 if (goi.argc != 2 && goi.argc != 3)
3815 Jim_SetResult_sprintf(goi.interp,
3816 "usage: %s <address> <data> [<count>]", cmd_name);
3817 return JIM_ERR;
3820 jim_wide a;
3821 int e = Jim_GetOpt_Wide(&goi, &a);
3822 if (e != JIM_OK)
3823 return e;
3825 jim_wide b;
3826 e = Jim_GetOpt_Wide(&goi, &b);
3827 if (e != JIM_OK)
3828 return e;
3830 jim_wide c = 1;
3831 if (goi.argc == 3)
3833 e = Jim_GetOpt_Wide(&goi, &c);
3834 if (e != JIM_OK)
3835 return e;
3838 struct target *target = Jim_CmdPrivData(goi.interp);
3839 uint8_t target_buf[32];
3840 if (strcasecmp(cmd_name, "mww") == 0) {
3841 target_buffer_set_u32(target, target_buf, b);
3842 b = 4;
3844 else if (strcasecmp(cmd_name, "mwh") == 0) {
3845 target_buffer_set_u16(target, target_buf, b);
3846 b = 2;
3848 else if (strcasecmp(cmd_name, "mwb") == 0) {
3849 target_buffer_set_u8(target, target_buf, b);
3850 b = 1;
3851 } else {
3852 LOG_ERROR("command '%s' unknown: ", cmd_name);
3853 return JIM_ERR;
3856 for (jim_wide x = 0; x < c; x++)
3858 e = target_write_memory(target, a, b, 1, target_buf);
3859 if (e != ERROR_OK)
3861 Jim_SetResult_sprintf(interp,
3862 "Error writing @ 0x%08x: %d\n", (int)(a), e);
3863 return JIM_ERR;
3865 /* b = width */
3866 a = a + b;
3868 return JIM_OK;
3871 static int jim_target_md(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
3873 const char *cmd_name = Jim_GetString(argv[0], NULL);
3875 Jim_GetOptInfo goi;
3876 Jim_GetOpt_Setup(&goi, interp, argc - 1, argv + 1);
3878 if ((goi.argc == 2) || (goi.argc == 3))
3880 Jim_SetResult_sprintf(goi.interp,
3881 "usage: %s <address> [<count>]", cmd_name);
3882 return JIM_ERR;
3885 jim_wide a;
3886 int e = Jim_GetOpt_Wide(&goi, &a);
3887 if (e != JIM_OK) {
3888 return JIM_ERR;
3890 jim_wide c;
3891 if (goi.argc) {
3892 e = Jim_GetOpt_Wide(&goi, &c);
3893 if (e != JIM_OK) {
3894 return JIM_ERR;
3896 } else {
3897 c = 1;
3899 jim_wide b = 1; /* shut up gcc */
3900 if (strcasecmp(cmd_name, "mdw") == 0)
3901 b = 4;
3902 else if (strcasecmp(cmd_name, "mdh") == 0)
3903 b = 2;
3904 else if (strcasecmp(cmd_name, "mdb") == 0)
3905 b = 1;
3906 else {
3907 LOG_ERROR("command '%s' unknown: ", cmd_name);
3908 return JIM_ERR;
3911 /* convert count to "bytes" */
3912 c = c * b;
3914 struct target *target = Jim_CmdPrivData(goi.interp);
3915 uint8_t target_buf[32];
3916 jim_wide x, y, z;
3917 while (c > 0) {
3918 y = c;
3919 if (y > 16) {
3920 y = 16;
3922 e = target_read_memory(target, a, b, y / b, target_buf);
3923 if (e != ERROR_OK) {
3924 Jim_SetResult_sprintf(interp, "error reading target @ 0x%08lx", (int)(a));
3925 return JIM_ERR;
3928 Jim_fprintf(interp, interp->cookie_stdout, "0x%08x ", (int)(a));
3929 switch (b) {
3930 case 4:
3931 for (x = 0; x < 16 && x < y; x += 4)
3933 z = target_buffer_get_u32(target, &(target_buf[ x * 4 ]));
3934 Jim_fprintf(interp, interp->cookie_stdout, "%08x ", (int)(z));
3936 for (; (x < 16) ; x += 4) {
3937 Jim_fprintf(interp, interp->cookie_stdout, " ");
3939 break;
3940 case 2:
3941 for (x = 0; x < 16 && x < y; x += 2)
3943 z = target_buffer_get_u16(target, &(target_buf[ x * 2 ]));
3944 Jim_fprintf(interp, interp->cookie_stdout, "%04x ", (int)(z));
3946 for (; (x < 16) ; x += 2) {
3947 Jim_fprintf(interp, interp->cookie_stdout, " ");
3949 break;
3950 case 1:
3951 default:
3952 for (x = 0 ; (x < 16) && (x < y) ; x += 1) {
3953 z = target_buffer_get_u8(target, &(target_buf[ x * 4 ]));
3954 Jim_fprintf(interp, interp->cookie_stdout, "%02x ", (int)(z));
3956 for (; (x < 16) ; x += 1) {
3957 Jim_fprintf(interp, interp->cookie_stdout, " ");
3959 break;
3961 /* ascii-ify the bytes */
3962 for (x = 0 ; x < y ; x++) {
3963 if ((target_buf[x] >= 0x20) &&
3964 (target_buf[x] <= 0x7e)) {
3965 /* good */
3966 } else {
3967 /* smack it */
3968 target_buf[x] = '.';
3971 /* space pad */
3972 while (x < 16) {
3973 target_buf[x] = ' ';
3974 x++;
3976 /* terminate */
3977 target_buf[16] = 0;
3978 /* print - with a newline */
3979 Jim_fprintf(interp, interp->cookie_stdout, "%s\n", target_buf);
3980 /* NEXT... */
3981 c -= 16;
3982 a += 16;
3984 return JIM_OK;
3987 static int jim_target_mem2array(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
3989 struct target *target = Jim_CmdPrivData(interp);
3990 return target_mem2array(interp, target, argc - 1, argv + 1);
3993 static int jim_target_array2mem(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
3995 struct target *target = Jim_CmdPrivData(interp);
3996 return target_array2mem(interp, target, argc - 1, argv + 1);
3999 static int jim_target_tap_disabled(Jim_Interp *interp)
4001 Jim_SetResult_sprintf(interp, "[TAP is disabled]");
4002 return JIM_ERR;
4005 static int jim_target_examine(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
4007 if (argc != 1)
4009 Jim_WrongNumArgs(interp, 1, argv, "[no parameters]");
4010 return JIM_ERR;
4012 struct target *target = Jim_CmdPrivData(interp);
4013 if (!target->tap->enabled)
4014 return jim_target_tap_disabled(interp);
4016 int e = target->type->examine(target);
4017 if (e != ERROR_OK)
4019 Jim_SetResult_sprintf(interp, "examine-fails: %d", e);
4020 return JIM_ERR;
4022 return JIM_OK;
4025 static int jim_target_poll(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
4027 if (argc != 1)
4029 Jim_WrongNumArgs(interp, 1, argv, "[no parameters]");
4030 return JIM_ERR;
4032 struct target *target = Jim_CmdPrivData(interp);
4033 if (!target->tap->enabled)
4034 return jim_target_tap_disabled(interp);
4036 int e;
4037 if (!(target_was_examined(target))) {
4038 e = ERROR_TARGET_NOT_EXAMINED;
4039 } else {
4040 e = target->type->poll(target);
4042 if (e != ERROR_OK)
4044 Jim_SetResult_sprintf(interp, "poll-fails: %d", e);
4045 return JIM_ERR;
4047 return JIM_OK;
4050 static int jim_target_reset(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
4052 Jim_GetOptInfo goi;
4053 Jim_GetOpt_Setup(&goi, interp, argc - 1, argv + 1);
4055 if (goi.argc != 2)
4057 Jim_WrongNumArgs(interp, 0, argv,
4058 "([tT]|[fF]|assert|deassert) BOOL");
4059 return JIM_ERR;
4062 Jim_Nvp *n;
4063 int e = Jim_GetOpt_Nvp(&goi, nvp_assert, &n);
4064 if (e != JIM_OK)
4066 Jim_GetOpt_NvpUnknown(&goi, nvp_assert, 1);
4067 return e;
4069 /* the halt or not param */
4070 jim_wide a;
4071 e = Jim_GetOpt_Wide(&goi, &a);
4072 if (e != JIM_OK)
4073 return e;
4075 struct target *target = Jim_CmdPrivData(goi.interp);
4076 if (!target->tap->enabled)
4077 return jim_target_tap_disabled(interp);
4078 if (!target->type->assert_reset || !target->type->deassert_reset)
4080 Jim_SetResult_sprintf(interp,
4081 "No target-specific reset for %s",
4082 target_name(target));
4083 return JIM_ERR;
4085 /* determine if we should halt or not. */
4086 target->reset_halt = !!a;
4087 /* When this happens - all workareas are invalid. */
4088 target_free_all_working_areas_restore(target, 0);
4090 /* do the assert */
4091 if (n->value == NVP_ASSERT) {
4092 e = target->type->assert_reset(target);
4093 } else {
4094 e = target->type->deassert_reset(target);
4096 return (e == ERROR_OK) ? JIM_OK : JIM_ERR;
4099 static int jim_target_halt(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
4101 if (argc != 1) {
4102 Jim_WrongNumArgs(interp, 1, argv, "[no parameters]");
4103 return JIM_ERR;
4105 struct target *target = Jim_CmdPrivData(interp);
4106 if (!target->tap->enabled)
4107 return jim_target_tap_disabled(interp);
4108 int e = target->type->halt(target);
4109 return (e == ERROR_OK) ? JIM_OK : JIM_ERR;
4112 static int jim_target_wait_state(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
4114 Jim_GetOptInfo goi;
4115 Jim_GetOpt_Setup(&goi, interp, argc - 1, argv + 1);
4117 /* params: <name> statename timeoutmsecs */
4118 if (goi.argc != 2)
4120 const char *cmd_name = Jim_GetString(argv[0], NULL);
4121 Jim_SetResult_sprintf(goi.interp,
4122 "%s <state_name> <timeout_in_msec>", cmd_name);
4123 return JIM_ERR;
4126 Jim_Nvp *n;
4127 int e = Jim_GetOpt_Nvp(&goi, nvp_target_state, &n);
4128 if (e != JIM_OK) {
4129 Jim_GetOpt_NvpUnknown(&goi, nvp_target_state,1);
4130 return e;
4132 jim_wide a;
4133 e = Jim_GetOpt_Wide(&goi, &a);
4134 if (e != JIM_OK) {
4135 return e;
4137 struct target *target = Jim_CmdPrivData(interp);
4138 if (!target->tap->enabled)
4139 return jim_target_tap_disabled(interp);
4141 e = target_wait_state(target, n->value, a);
4142 if (e != ERROR_OK)
4144 Jim_SetResult_sprintf(goi.interp,
4145 "target: %s wait %s fails (%d) %s",
4146 target_name(target), n->name,
4147 e, target_strerror_safe(e));
4148 return JIM_ERR;
4150 return JIM_OK;
4152 /* List for human, Events defined for this target.
4153 * scripts/programs should use 'name cget -event NAME'
4155 static int jim_target_event_list(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
4157 struct command_context *cmd_ctx = Jim_GetAssocData(interp, "context");
4158 struct target *target = Jim_CmdPrivData(interp);
4159 struct target_event_action *teap = target->event_action;
4160 command_print(cmd_ctx, "Event actions for target (%d) %s\n",
4161 target->target_number,
4162 target_name(target));
4163 command_print(cmd_ctx, "%-25s | Body", "Event");
4164 command_print(cmd_ctx, "------------------------- | "
4165 "----------------------------------------");
4166 while (teap)
4168 Jim_Nvp *opt = Jim_Nvp_value2name_simple(nvp_target_event, teap->event);
4169 command_print(cmd_ctx, "%-25s | %s",
4170 opt->name, Jim_GetString(teap->body, NULL));
4171 teap = teap->next;
4173 command_print(cmd_ctx, "***END***");
4174 return JIM_OK;
4176 static int jim_target_current_state(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
4178 if (argc != 1)
4180 Jim_WrongNumArgs(interp, 1, argv, "[no parameters]");
4181 return JIM_ERR;
4183 struct target *target = Jim_CmdPrivData(interp);
4184 Jim_SetResultString(interp, target_state_name(target), -1);
4185 return JIM_OK;
4187 static int jim_target_invoke_event(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
4189 Jim_GetOptInfo goi;
4190 Jim_GetOpt_Setup(&goi, interp, argc - 1, argv + 1);
4191 if (goi.argc != 1)
4193 const char *cmd_name = Jim_GetString(argv[0], NULL);
4194 Jim_SetResult_sprintf(goi.interp, "%s <eventname>", cmd_name);
4195 return JIM_ERR;
4197 Jim_Nvp *n;
4198 int e = Jim_GetOpt_Nvp(&goi, nvp_target_event, &n);
4199 if (e != JIM_OK)
4201 Jim_GetOpt_NvpUnknown(&goi, nvp_target_event, 1);
4202 return e;
4204 struct target *target = Jim_CmdPrivData(interp);
4205 target_handle_event(target, n->value);
4206 return JIM_OK;
4209 static const struct command_registration target_instance_command_handlers[] = {
4211 .name = "configure",
4212 .mode = COMMAND_CONFIG,
4213 .jim_handler = &jim_target_configure,
4214 .usage = "[<target_options> ...]",
4215 .help = "configure a new target for use",
4218 .name = "cget",
4219 .mode = COMMAND_ANY,
4220 .jim_handler = &jim_target_configure,
4221 .usage = "<target_type> [<target_options> ...]",
4222 .help = "configure a new target for use",
4225 .name = "mww",
4226 .mode = COMMAND_EXEC,
4227 .jim_handler = &jim_target_mw,
4228 .usage = "<address> <data> [<count>]",
4229 .help = "Write 32-bit word(s) to target memory",
4232 .name = "mwh",
4233 .mode = COMMAND_EXEC,
4234 .jim_handler = &jim_target_mw,
4235 .usage = "<address> <data> [<count>]",
4236 .help = "Write 16-bit half-word(s) to target memory",
4239 .name = "mwb",
4240 .mode = COMMAND_EXEC,
4241 .jim_handler = &jim_target_mw,
4242 .usage = "<address> <data> [<count>]",
4243 .help = "Write byte(s) to target memory",
4246 .name = "mdw",
4247 .mode = COMMAND_EXEC,
4248 .jim_handler = &jim_target_md,
4249 .usage = "<address> [<count>]",
4250 .help = "Display target memory as 32-bit words",
4253 .name = "mdh",
4254 .mode = COMMAND_EXEC,
4255 .jim_handler = &jim_target_md,
4256 .usage = "<address> [<count>]",
4257 .help = "Display target memory as 16-bit half-words",
4260 .name = "mdb",
4261 .mode = COMMAND_EXEC,
4262 .jim_handler = &jim_target_md,
4263 .usage = "<address> [<count>]",
4264 .help = "Display target memory as 8-bit bytes",
4267 .name = "array2mem",
4268 .mode = COMMAND_EXEC,
4269 .jim_handler = &jim_target_array2mem,
4272 .name = "mem2array",
4273 .mode = COMMAND_EXEC,
4274 .jim_handler = &jim_target_mem2array,
4277 .name = "eventlist",
4278 .mode = COMMAND_EXEC,
4279 .jim_handler = &jim_target_event_list,
4282 .name = "curstate",
4283 .mode = COMMAND_EXEC,
4284 .jim_handler = &jim_target_current_state,
4287 .name = "arp_examine",
4288 .mode = COMMAND_EXEC,
4289 .jim_handler = &jim_target_examine,
4292 .name = "arp_poll",
4293 .mode = COMMAND_EXEC,
4294 .jim_handler = &jim_target_poll,
4297 .name = "arp_reset",
4298 .mode = COMMAND_EXEC,
4299 .jim_handler = &jim_target_reset,
4302 .name = "arp_halt",
4303 .mode = COMMAND_EXEC,
4304 .jim_handler = &jim_target_halt,
4307 .name = "arp_waitstate",
4308 .mode = COMMAND_EXEC,
4309 .jim_handler = &jim_target_wait_state,
4312 .name = "invoke-event",
4313 .mode = COMMAND_EXEC,
4314 .jim_handler = &jim_target_invoke_event,
4316 COMMAND_REGISTRATION_DONE
4319 static int target_create(Jim_GetOptInfo *goi)
4321 Jim_Obj *new_cmd;
4322 Jim_Cmd *cmd;
4323 const char *cp;
4324 char *cp2;
4325 int e;
4326 int x;
4327 struct target *target;
4328 struct command_context *cmd_ctx;
4330 cmd_ctx = Jim_GetAssocData(goi->interp, "context");
4331 if (goi->argc < 3) {
4332 Jim_WrongNumArgs(goi->interp, 1, goi->argv, "?name? ?type? ..options...");
4333 return JIM_ERR;
4336 /* COMMAND */
4337 Jim_GetOpt_Obj(goi, &new_cmd);
4338 /* does this command exist? */
4339 cmd = Jim_GetCommand(goi->interp, new_cmd, JIM_ERRMSG);
4340 if (cmd) {
4341 cp = Jim_GetString(new_cmd, NULL);
4342 Jim_SetResult_sprintf(goi->interp, "Command/target: %s Exists", cp);
4343 return JIM_ERR;
4346 /* TYPE */
4347 e = Jim_GetOpt_String(goi, &cp2, NULL);
4348 cp = cp2;
4349 /* now does target type exist */
4350 for (x = 0 ; target_types[x] ; x++) {
4351 if (0 == strcmp(cp, target_types[x]->name)) {
4352 /* found */
4353 break;
4356 if (target_types[x] == NULL) {
4357 Jim_SetResult_sprintf(goi->interp, "Unknown target type %s, try one of ", cp);
4358 for (x = 0 ; target_types[x] ; x++) {
4359 if (target_types[x + 1]) {
4360 Jim_AppendStrings(goi->interp,
4361 Jim_GetResult(goi->interp),
4362 target_types[x]->name,
4363 ", ", NULL);
4364 } else {
4365 Jim_AppendStrings(goi->interp,
4366 Jim_GetResult(goi->interp),
4367 " or ",
4368 target_types[x]->name,NULL);
4371 return JIM_ERR;
4374 /* Create it */
4375 target = calloc(1,sizeof(struct target));
4376 /* set target number */
4377 target->target_number = new_target_number();
4379 /* allocate memory for each unique target type */
4380 target->type = (struct target_type*)calloc(1,sizeof(struct target_type));
4382 memcpy(target->type, target_types[x], sizeof(struct target_type));
4384 /* will be set by "-endian" */
4385 target->endianness = TARGET_ENDIAN_UNKNOWN;
4387 target->working_area = 0x0;
4388 target->working_area_size = 0x0;
4389 target->working_areas = NULL;
4390 target->backup_working_area = 0;
4392 target->state = TARGET_UNKNOWN;
4393 target->debug_reason = DBG_REASON_UNDEFINED;
4394 target->reg_cache = NULL;
4395 target->breakpoints = NULL;
4396 target->watchpoints = NULL;
4397 target->next = NULL;
4398 target->arch_info = NULL;
4400 target->display = 1;
4402 target->halt_issued = false;
4404 /* initialize trace information */
4405 target->trace_info = malloc(sizeof(struct trace));
4406 target->trace_info->num_trace_points = 0;
4407 target->trace_info->trace_points_size = 0;
4408 target->trace_info->trace_points = NULL;
4409 target->trace_info->trace_history_size = 0;
4410 target->trace_info->trace_history = NULL;
4411 target->trace_info->trace_history_pos = 0;
4412 target->trace_info->trace_history_overflowed = 0;
4414 target->dbgmsg = NULL;
4415 target->dbg_msg_enabled = 0;
4417 target->endianness = TARGET_ENDIAN_UNKNOWN;
4419 /* Do the rest as "configure" options */
4420 goi->isconfigure = 1;
4421 e = target_configure(goi, target);
4423 if (target->tap == NULL)
4425 Jim_SetResultString(goi->interp, "-chain-position required when creating target", -1);
4426 e = JIM_ERR;
4429 if (e != JIM_OK) {
4430 free(target->type);
4431 free(target);
4432 return e;
4435 if (target->endianness == TARGET_ENDIAN_UNKNOWN) {
4436 /* default endian to little if not specified */
4437 target->endianness = TARGET_LITTLE_ENDIAN;
4440 /* incase variant is not set */
4441 if (!target->variant)
4442 target->variant = strdup("");
4444 cp = Jim_GetString(new_cmd, NULL);
4445 target->cmd_name = strdup(cp);
4447 /* create the target specific commands */
4448 if (target->type->commands) {
4449 e = register_commands(cmd_ctx, NULL, target->type->commands);
4450 if (ERROR_OK != e)
4451 LOG_ERROR("unable to register '%s' commands", cp);
4453 if (target->type->target_create) {
4454 (*(target->type->target_create))(target, goi->interp);
4457 /* append to end of list */
4459 struct target **tpp;
4460 tpp = &(all_targets);
4461 while (*tpp) {
4462 tpp = &((*tpp)->next);
4464 *tpp = target;
4467 /* now - create the new target name command */
4468 const const struct command_registration target_subcommands[] = {
4470 .chain = target_instance_command_handlers,
4473 .chain = target->type->commands,
4475 COMMAND_REGISTRATION_DONE
4477 const const struct command_registration target_commands[] = {
4479 .name = cp,
4480 .mode = COMMAND_ANY,
4481 .help = "target command group",
4482 .chain = target_subcommands,
4484 COMMAND_REGISTRATION_DONE
4486 e = register_commands(cmd_ctx, NULL, target_commands);
4487 if (ERROR_OK != e)
4488 return JIM_ERR;
4490 struct command *c = command_find_in_context(cmd_ctx, cp);
4491 assert(c);
4492 command_set_handler_data(c, target);
4494 return (ERROR_OK == e) ? JIM_OK : JIM_ERR;
4497 static int jim_target_current(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
4499 if (argc != 1)
4501 Jim_WrongNumArgs(interp, 1, argv, "Too many parameters");
4502 return JIM_ERR;
4504 struct command_context *cmd_ctx = Jim_GetAssocData(interp, "context");
4505 Jim_SetResultString(interp, get_current_target(cmd_ctx)->cmd_name, -1);
4506 return JIM_OK;
4509 static int jim_target_types(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
4511 if (argc != 1)
4513 Jim_WrongNumArgs(interp, 1, argv, "Too many parameters");
4514 return JIM_ERR;
4516 Jim_SetResult(interp, Jim_NewListObj(interp, NULL, 0));
4517 for (unsigned x = 0; NULL != target_types[x]; x++)
4519 Jim_ListAppendElement(interp, Jim_GetResult(interp),
4520 Jim_NewStringObj(interp, target_types[x]->name, -1));
4522 return JIM_OK;
4525 static int jim_target_names(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
4527 if (argc != 1)
4529 Jim_WrongNumArgs(interp, 1, argv, "Too many parameters");
4530 return JIM_ERR;
4532 Jim_SetResult(interp, Jim_NewListObj(interp, NULL, 0));
4533 struct target *target = all_targets;
4534 while (target)
4536 Jim_ListAppendElement(interp, Jim_GetResult(interp),
4537 Jim_NewStringObj(interp, target_name(target), -1));
4538 target = target->next;
4540 return JIM_OK;
4543 static int jim_target_create(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
4545 Jim_GetOptInfo goi;
4546 Jim_GetOpt_Setup(&goi, interp, argc - 1, argv + 1);
4547 if (goi.argc < 3)
4549 Jim_WrongNumArgs(goi.interp, goi.argc, goi.argv,
4550 "<name> <target_type> [<target_options> ...]");
4551 return JIM_ERR;
4553 return target_create(&goi);
4556 static int jim_target_number(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
4558 Jim_GetOptInfo goi;
4559 Jim_GetOpt_Setup(&goi, interp, argc - 1, argv + 1);
4561 /* It's OK to remove this mechanism sometime after August 2010 or so */
4562 LOG_WARNING("don't use numbers as target identifiers; use names");
4563 if (goi.argc != 1)
4565 Jim_SetResult_sprintf(goi.interp, "usage: target number <number>");
4566 return JIM_ERR;
4568 jim_wide w;
4569 int e = Jim_GetOpt_Wide(&goi, &w);
4570 if (e != JIM_OK)
4571 return JIM_ERR;
4573 struct target *target;
4574 for (target = all_targets; NULL != target; target = target->next)
4576 if (target->target_number != w)
4577 continue;
4579 Jim_SetResultString(goi.interp, target_name(target), -1);
4580 return JIM_OK;
4582 Jim_SetResult_sprintf(goi.interp,
4583 "Target: number %d does not exist", (int)(w));
4584 return JIM_ERR;
4587 static int jim_target_count(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
4589 if (argc != 1)
4591 Jim_WrongNumArgs(interp, 1, argv, "<no parameters>");
4592 return JIM_ERR;
4594 unsigned count = 0;
4595 struct target *target = all_targets;
4596 while (NULL != target)
4598 target = target->next;
4599 count++;
4601 Jim_SetResult(interp, Jim_NewIntObj(interp, count));
4602 return JIM_OK;
4605 static const struct command_registration target_subcommand_handlers[] = {
4607 .name = "init",
4608 .mode = COMMAND_CONFIG,
4609 .handler = &handle_target_init_command,
4610 .help = "initialize targets",
4613 .name = "create",
4614 .mode = COMMAND_ANY,
4615 .jim_handler = &jim_target_create,
4616 .usage = "<name> <type> ...",
4617 .help = "Returns the currently selected target",
4620 .name = "current",
4621 .mode = COMMAND_ANY,
4622 .jim_handler = &jim_target_current,
4623 .help = "Returns the currently selected target",
4626 .name = "types",
4627 .mode = COMMAND_ANY,
4628 .jim_handler = &jim_target_types,
4629 .help = "Returns the available target types as a list of strings",
4632 .name = "names",
4633 .mode = COMMAND_ANY,
4634 .jim_handler = &jim_target_names,
4635 .help = "Returns the names of all targets as a list of strings",
4638 .name = "number",
4639 .mode = COMMAND_ANY,
4640 .jim_handler = &jim_target_number,
4641 .usage = "<number>",
4642 .help = "Returns the name of target <n>",
4645 .name = "count",
4646 .mode = COMMAND_ANY,
4647 .jim_handler = &jim_target_count,
4648 .help = "Returns the number of targets as an integer",
4650 COMMAND_REGISTRATION_DONE
4654 struct FastLoad
4656 uint32_t address;
4657 uint8_t *data;
4658 int length;
4662 static int fastload_num;
4663 static struct FastLoad *fastload;
4665 static void free_fastload(void)
4667 if (fastload != NULL)
4669 int i;
4670 for (i = 0; i < fastload_num; i++)
4672 if (fastload[i].data)
4673 free(fastload[i].data);
4675 free(fastload);
4676 fastload = NULL;
4683 COMMAND_HANDLER(handle_fast_load_image_command)
4685 uint8_t *buffer;
4686 size_t buf_cnt;
4687 uint32_t image_size;
4688 uint32_t min_address = 0;
4689 uint32_t max_address = 0xffffffff;
4690 int i;
4692 struct image image;
4694 int retval = CALL_COMMAND_HANDLER(parse_load_image_command_CMD_ARGV,
4695 &image, &min_address, &max_address);
4696 if (ERROR_OK != retval)
4697 return retval;
4699 struct duration bench;
4700 duration_start(&bench);
4702 if (image_open(&image, CMD_ARGV[0], (CMD_ARGC >= 3) ? CMD_ARGV[2] : NULL) != ERROR_OK)
4704 return ERROR_OK;
4707 image_size = 0x0;
4708 retval = ERROR_OK;
4709 fastload_num = image.num_sections;
4710 fastload = (struct FastLoad *)malloc(sizeof(struct FastLoad)*image.num_sections);
4711 if (fastload == NULL)
4713 image_close(&image);
4714 return ERROR_FAIL;
4716 memset(fastload, 0, sizeof(struct FastLoad)*image.num_sections);
4717 for (i = 0; i < image.num_sections; i++)
4719 buffer = malloc(image.sections[i].size);
4720 if (buffer == NULL)
4722 command_print(CMD_CTX, "error allocating buffer for section (%d bytes)",
4723 (int)(image.sections[i].size));
4724 break;
4727 if ((retval = image_read_section(&image, i, 0x0, image.sections[i].size, buffer, &buf_cnt)) != ERROR_OK)
4729 free(buffer);
4730 break;
4733 uint32_t offset = 0;
4734 uint32_t length = buf_cnt;
4737 /* DANGER!!! beware of unsigned comparision here!!! */
4739 if ((image.sections[i].base_address + buf_cnt >= min_address)&&
4740 (image.sections[i].base_address < max_address))
4742 if (image.sections[i].base_address < min_address)
4744 /* clip addresses below */
4745 offset += min_address-image.sections[i].base_address;
4746 length -= offset;
4749 if (image.sections[i].base_address + buf_cnt > max_address)
4751 length -= (image.sections[i].base_address + buf_cnt)-max_address;
4754 fastload[i].address = image.sections[i].base_address + offset;
4755 fastload[i].data = malloc(length);
4756 if (fastload[i].data == NULL)
4758 free(buffer);
4759 break;
4761 memcpy(fastload[i].data, buffer + offset, length);
4762 fastload[i].length = length;
4764 image_size += length;
4765 command_print(CMD_CTX, "%u bytes written at address 0x%8.8x",
4766 (unsigned int)length,
4767 ((unsigned int)(image.sections[i].base_address + offset)));
4770 free(buffer);
4773 if ((ERROR_OK == retval) && (duration_measure(&bench) == ERROR_OK))
4775 command_print(CMD_CTX, "Loaded %" PRIu32 " bytes "
4776 "in %fs (%0.3f kb/s)", image_size,
4777 duration_elapsed(&bench), duration_kbps(&bench, image_size));
4779 command_print(CMD_CTX,
4780 "WARNING: image has not been loaded to target!"
4781 "You can issue a 'fast_load' to finish loading.");
4784 image_close(&image);
4786 if (retval != ERROR_OK)
4788 free_fastload();
4791 return retval;
4794 COMMAND_HANDLER(handle_fast_load_command)
4796 if (CMD_ARGC > 0)
4797 return ERROR_COMMAND_SYNTAX_ERROR;
4798 if (fastload == NULL)
4800 LOG_ERROR("No image in memory");
4801 return ERROR_FAIL;
4803 int i;
4804 int ms = timeval_ms();
4805 int size = 0;
4806 int retval = ERROR_OK;
4807 for (i = 0; i < fastload_num;i++)
4809 struct target *target = get_current_target(CMD_CTX);
4810 command_print(CMD_CTX, "Write to 0x%08x, length 0x%08x",
4811 (unsigned int)(fastload[i].address),
4812 (unsigned int)(fastload[i].length));
4813 if (retval == ERROR_OK)
4815 retval = target_write_buffer(target, fastload[i].address, fastload[i].length, fastload[i].data);
4817 size += fastload[i].length;
4819 int after = timeval_ms();
4820 command_print(CMD_CTX, "Loaded image %f kBytes/s", (float)(size/1024.0)/((float)(after-ms)/1000.0));
4821 return retval;
4824 static const struct command_registration target_command_handlers[] = {
4826 .name = "targets",
4827 .handler = &handle_targets_command,
4828 .mode = COMMAND_ANY,
4829 .help = "change current command line target (one parameter) "
4830 "or list targets (no parameters)",
4831 .usage = "[<new_current_target>]",
4834 .name = "target",
4835 .mode = COMMAND_CONFIG,
4836 .help = "configure target",
4838 .chain = target_subcommand_handlers,
4840 COMMAND_REGISTRATION_DONE
4843 int target_register_commands(struct command_context *cmd_ctx)
4845 return register_commands(cmd_ctx, NULL, target_command_handlers);
4848 static const struct command_registration target_exec_command_handlers[] = {
4850 .name = "fast_load_image",
4851 .handler = &handle_fast_load_image_command,
4852 .mode = COMMAND_ANY,
4853 .help = "Load image into memory, mainly for profiling purposes",
4854 .usage = "<file> <address> ['bin'|'ihex'|'elf'|'s19'] "
4855 "[min_address] [max_length]",
4858 .name = "fast_load",
4859 .handler = &handle_fast_load_command,
4860 .mode = COMMAND_ANY,
4861 .help = "loads active fast load image to current target "
4862 "- mainly for profiling purposes",
4865 .name = "profile",
4866 .handler = &handle_profile_command,
4867 .mode = COMMAND_EXEC,
4868 .help = "profiling samples the CPU PC",
4870 /** @todo don't register virt2phys() unless target supports it */
4872 .name = "virt2phys",
4873 .handler = &handle_virt2phys_command,
4874 .mode = COMMAND_ANY,
4875 .help = "translate a virtual address into a physical address",
4879 .name = "reg",
4880 .handler = &handle_reg_command,
4881 .mode = COMMAND_EXEC,
4882 .help = "display or set a register",
4886 .name = "poll",
4887 .handler = &handle_poll_command,
4888 .mode = COMMAND_EXEC,
4889 .help = "poll target state",
4892 .name = "wait_halt",
4893 .handler = &handle_wait_halt_command,
4894 .mode = COMMAND_EXEC,
4895 .help = "wait for target halt",
4896 .usage = "[time (s)]",
4899 .name = "halt",
4900 .handler = &handle_halt_command,
4901 .mode = COMMAND_EXEC,
4902 .help = "halt target",
4905 .name = "resume",
4906 .handler = &handle_resume_command,
4907 .mode = COMMAND_EXEC,
4908 .help = "resume target",
4909 .usage = "[<address>]",
4912 .name = "reset",
4913 .handler = &handle_reset_command,
4914 .mode = COMMAND_EXEC,
4915 .usage = "[run|halt|init]",
4916 .help = "Reset all targets into the specified mode."
4917 "Default reset mode is run, if not given.",
4920 .name = "soft_reset_halt",
4921 .handler = &handle_soft_reset_halt_command,
4922 .mode = COMMAND_EXEC,
4923 .help = "halt the target and do a soft reset",
4927 .name = "step",
4928 .handler = &handle_step_command,
4929 .mode = COMMAND_EXEC,
4930 .help = "step one instruction from current PC or [addr]",
4931 .usage = "[<address>]",
4935 .name = "mdw",
4936 .handler = &handle_md_command,
4937 .mode = COMMAND_EXEC,
4938 .help = "display memory words",
4939 .usage = "[phys] <addr> [count]",
4942 .name = "mdh",
4943 .handler = &handle_md_command,
4944 .mode = COMMAND_EXEC,
4945 .help = "display memory half-words",
4946 .usage = "[phys] <addr> [count]",
4949 .name = "mdb",
4950 .handler = &handle_md_command,
4951 .mode = COMMAND_EXEC,
4952 .help = "display memory bytes",
4953 .usage = "[phys] <addr> [count]",
4957 .name = "mww",
4958 .handler = &handle_mw_command,
4959 .mode = COMMAND_EXEC,
4960 .help = "write memory word",
4961 .usage = "[phys] <addr> <value> [count]",
4964 .name = "mwh",
4965 .handler = &handle_mw_command,
4966 .mode = COMMAND_EXEC,
4967 .help = "write memory half-word",
4968 .usage = "[phys] <addr> <value> [count]",
4971 .name = "mwb",
4972 .handler = &handle_mw_command,
4973 .mode = COMMAND_EXEC,
4974 .help = "write memory byte",
4975 .usage = "[phys] <addr> <value> [count]",
4979 .name = "bp",
4980 .handler = &handle_bp_command,
4981 .mode = COMMAND_EXEC,
4982 .help = "list or set breakpoint",
4983 .usage = "[<address> <length> [hw]]",
4986 .name = "rbp",
4987 .handler = &handle_rbp_command,
4988 .mode = COMMAND_EXEC,
4989 .help = "remove breakpoint",
4990 .usage = "<address>",
4994 .name = "wp",
4995 .handler = &handle_wp_command,
4996 .mode = COMMAND_EXEC,
4997 .help = "list or set watchpoint",
4998 .usage = "[<address> <length> <r/w/a> [value] [mask]]",
5001 .name = "rwp",
5002 .handler = &handle_rwp_command,
5003 .mode = COMMAND_EXEC,
5004 .help = "remove watchpoint",
5005 .usage = "<address>",
5009 .name = "load_image",
5010 .handler = &handle_load_image_command,
5011 .mode = COMMAND_EXEC,
5012 .usage = "<file> <address> ['bin'|'ihex'|'elf'|'s19'] "
5013 "[min_address] [max_length]",
5016 .name = "dump_image",
5017 .handler = &handle_dump_image_command,
5018 .mode = COMMAND_EXEC,
5019 .usage = "<file> <address> <size>",
5022 .name = "verify_image",
5023 .handler = &handle_verify_image_command,
5024 .mode = COMMAND_EXEC,
5025 .usage = "<file> [offset] [type]",
5028 .name = "test_image",
5029 .handler = &handle_test_image_command,
5030 .mode = COMMAND_EXEC,
5031 .usage = "<file> [offset] [type]",
5034 .name = "ocd_mem2array",
5035 .mode = COMMAND_EXEC,
5036 .jim_handler = &jim_mem2array,
5037 .help = "read memory and return as a TCL array "
5038 "for script processing",
5039 .usage = "<arrayname> <width=32|16|8> <address> <count>",
5042 .name = "ocd_array2mem",
5043 .mode = COMMAND_EXEC,
5044 .jim_handler = &jim_array2mem,
5045 .help = "convert a TCL array to memory locations "
5046 "and write the values",
5047 .usage = "<arrayname> <width=32|16|8> <address> <count>",
5049 COMMAND_REGISTRATION_DONE
5051 int target_register_user_commands(struct command_context *cmd_ctx)
5053 int retval = ERROR_OK;
5054 if ((retval = target_request_register_commands(cmd_ctx)) != ERROR_OK)
5055 return retval;
5057 if ((retval = trace_register_commands(cmd_ctx)) != ERROR_OK)
5058 return retval;
5061 return register_commands(cmd_ctx, NULL, target_exec_command_handlers);