ARM11: remove needless string format #ifdeffery
[dnglaze.git] / src / target / target.c
blob55adcce6a24d5571099adf62e77f13490c6b0bff
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 "time_support.h"
41 #include "register.h"
42 #include "trace.h"
43 #include "image.h"
44 #include "jtag.h"
47 static int jim_mcrmrc(Jim_Interp *interp, int argc, Jim_Obj *const *argv);
49 static int target_array2mem(Jim_Interp *interp, struct target *target, int argc, Jim_Obj *const *argv);
50 static int target_mem2array(Jim_Interp *interp, struct target *target, int argc, Jim_Obj *const *argv);
52 /* targets */
53 extern struct target_type arm7tdmi_target;
54 extern struct target_type arm720t_target;
55 extern struct target_type arm9tdmi_target;
56 extern struct target_type arm920t_target;
57 extern struct target_type arm966e_target;
58 extern struct target_type arm926ejs_target;
59 extern struct target_type fa526_target;
60 extern struct target_type feroceon_target;
61 extern struct target_type dragonite_target;
62 extern struct target_type xscale_target;
63 extern struct target_type cortexm3_target;
64 extern struct target_type cortexa8_target;
65 extern struct target_type arm11_target;
66 extern struct target_type mips_m4k_target;
67 extern struct target_type avr_target;
69 struct target_type *target_types[] =
71 &arm7tdmi_target,
72 &arm9tdmi_target,
73 &arm920t_target,
74 &arm720t_target,
75 &arm966e_target,
76 &arm926ejs_target,
77 &fa526_target,
78 &feroceon_target,
79 &dragonite_target,
80 &xscale_target,
81 &cortexm3_target,
82 &cortexa8_target,
83 &arm11_target,
84 &mips_m4k_target,
85 &avr_target,
86 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 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 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_POST, .name = "reset-assert-post" },
149 { .value = TARGET_EVENT_RESET_DEASSERT_PRE, .name = "reset-deassert-pre" },
150 { .value = TARGET_EVENT_RESET_DEASSERT_POST, .name = "reset-deassert-post" },
151 { .value = TARGET_EVENT_RESET_HALT_PRE, .name = "reset-halt-pre" },
152 { .value = TARGET_EVENT_RESET_HALT_POST, .name = "reset-halt-post" },
153 { .value = TARGET_EVENT_RESET_WAIT_PRE, .name = "reset-wait-pre" },
154 { .value = TARGET_EVENT_RESET_WAIT_POST, .name = "reset-wait-post" },
155 { .value = TARGET_EVENT_RESET_INIT , .name = "reset-init" },
156 { .value = TARGET_EVENT_RESET_END, .name = "reset-end" },
158 { .value = TARGET_EVENT_EXAMINE_START, .name = "examine-start" },
159 { .value = TARGET_EVENT_EXAMINE_END, .name = "examine-end" },
161 { .value = TARGET_EVENT_DEBUG_HALTED, .name = "debug-halted" },
162 { .value = TARGET_EVENT_DEBUG_RESUMED, .name = "debug-resumed" },
164 { .value = TARGET_EVENT_GDB_ATTACH, .name = "gdb-attach" },
165 { .value = TARGET_EVENT_GDB_DETACH, .name = "gdb-detach" },
167 { .value = TARGET_EVENT_GDB_FLASH_WRITE_START, .name = "gdb-flash-write-start" },
168 { .value = TARGET_EVENT_GDB_FLASH_WRITE_END , .name = "gdb-flash-write-end" },
170 { .value = TARGET_EVENT_GDB_FLASH_ERASE_START, .name = "gdb-flash-erase-start" },
171 { .value = TARGET_EVENT_GDB_FLASH_ERASE_END , .name = "gdb-flash-erase-end" },
173 { .value = TARGET_EVENT_RESUME_START, .name = "resume-start" },
174 { .value = TARGET_EVENT_RESUMED , .name = "resume-ok" },
175 { .value = TARGET_EVENT_RESUME_END , .name = "resume-end" },
177 { .name = NULL, .value = -1 }
180 const Jim_Nvp nvp_target_state[] = {
181 { .name = "unknown", .value = TARGET_UNKNOWN },
182 { .name = "running", .value = TARGET_RUNNING },
183 { .name = "halted", .value = TARGET_HALTED },
184 { .name = "reset", .value = TARGET_RESET },
185 { .name = "debug-running", .value = TARGET_DEBUG_RUNNING },
186 { .name = NULL, .value = -1 },
189 const Jim_Nvp nvp_target_debug_reason [] = {
190 { .name = "debug-request" , .value = DBG_REASON_DBGRQ },
191 { .name = "breakpoint" , .value = DBG_REASON_BREAKPOINT },
192 { .name = "watchpoint" , .value = DBG_REASON_WATCHPOINT },
193 { .name = "watchpoint-and-breakpoint", .value = DBG_REASON_WPTANDBKPT },
194 { .name = "single-step" , .value = DBG_REASON_SINGLESTEP },
195 { .name = "target-not-halted" , .value = DBG_REASON_NOTHALTED },
196 { .name = "undefined" , .value = DBG_REASON_UNDEFINED },
197 { .name = NULL, .value = -1 },
200 const Jim_Nvp nvp_target_endian[] = {
201 { .name = "big", .value = TARGET_BIG_ENDIAN },
202 { .name = "little", .value = TARGET_LITTLE_ENDIAN },
203 { .name = "be", .value = TARGET_BIG_ENDIAN },
204 { .name = "le", .value = TARGET_LITTLE_ENDIAN },
205 { .name = NULL, .value = -1 },
208 const Jim_Nvp nvp_reset_modes[] = {
209 { .name = "unknown", .value = RESET_UNKNOWN },
210 { .name = "run" , .value = RESET_RUN },
211 { .name = "halt" , .value = RESET_HALT },
212 { .name = "init" , .value = RESET_INIT },
213 { .name = NULL , .value = -1 },
216 const char *
217 target_state_name( struct target *t )
219 const char *cp;
220 cp = Jim_Nvp_value2name_simple(nvp_target_state, t->state)->name;
221 if( !cp ){
222 LOG_ERROR("Invalid target state: %d", (int)(t->state));
223 cp = "(*BUG*unknown*BUG*)";
225 return cp;
228 /* determine the number of the new target */
229 static int new_target_number(void)
231 struct target *t;
232 int x;
234 /* number is 0 based */
235 x = -1;
236 t = all_targets;
237 while (t) {
238 if (x < t->target_number) {
239 x = t->target_number;
241 t = t->next;
243 return x + 1;
246 /* read a uint32_t from a buffer in target memory endianness */
247 uint32_t target_buffer_get_u32(struct target *target, const uint8_t *buffer)
249 if (target->endianness == TARGET_LITTLE_ENDIAN)
250 return le_to_h_u32(buffer);
251 else
252 return be_to_h_u32(buffer);
255 /* read a uint16_t from a buffer in target memory endianness */
256 uint16_t target_buffer_get_u16(struct target *target, const uint8_t *buffer)
258 if (target->endianness == TARGET_LITTLE_ENDIAN)
259 return le_to_h_u16(buffer);
260 else
261 return be_to_h_u16(buffer);
264 /* read a uint8_t from a buffer in target memory endianness */
265 uint8_t target_buffer_get_u8(struct target *target, const uint8_t *buffer)
267 return *buffer & 0x0ff;
270 /* write a uint32_t to a buffer in target memory endianness */
271 void target_buffer_set_u32(struct target *target, uint8_t *buffer, uint32_t value)
273 if (target->endianness == TARGET_LITTLE_ENDIAN)
274 h_u32_to_le(buffer, value);
275 else
276 h_u32_to_be(buffer, value);
279 /* write a uint16_t to a buffer in target memory endianness */
280 void target_buffer_set_u16(struct target *target, uint8_t *buffer, uint16_t value)
282 if (target->endianness == TARGET_LITTLE_ENDIAN)
283 h_u16_to_le(buffer, value);
284 else
285 h_u16_to_be(buffer, value);
288 /* write a uint8_t to a buffer in target memory endianness */
289 void target_buffer_set_u8(struct target *target, uint8_t *buffer, uint8_t value)
291 *buffer = value;
294 /* return a pointer to a configured target; id is name or number */
295 struct target *get_target(const char *id)
297 struct target *target;
299 /* try as tcltarget name */
300 for (target = all_targets; target; target = target->next) {
301 if (target->cmd_name == NULL)
302 continue;
303 if (strcmp(id, target->cmd_name) == 0)
304 return target;
307 /* It's OK to remove this fallback sometime after August 2010 or so */
309 /* no match, try as number */
310 unsigned num;
311 if (parse_uint(id, &num) != ERROR_OK)
312 return NULL;
314 for (target = all_targets; target; target = target->next) {
315 if (target->target_number == (int)num) {
316 LOG_WARNING("use '%s' as target identifier, not '%u'",
317 target->cmd_name, num);
318 return target;
322 return NULL;
325 /* returns a pointer to the n-th configured target */
326 static struct target *get_target_by_num(int num)
328 struct target *target = all_targets;
330 while (target) {
331 if (target->target_number == num) {
332 return target;
334 target = target->next;
337 return NULL;
340 struct target* get_current_target(struct command_context *cmd_ctx)
342 struct target *target = get_target_by_num(cmd_ctx->current_target);
344 if (target == NULL)
346 LOG_ERROR("BUG: current_target out of bounds");
347 exit(-1);
350 return target;
353 int target_poll(struct target *target)
355 int retval;
357 /* We can't poll until after examine */
358 if (!target_was_examined(target))
360 /* Fail silently lest we pollute the log */
361 return ERROR_FAIL;
364 retval = target->type->poll(target);
365 if (retval != ERROR_OK)
366 return retval;
368 if (target->halt_issued)
370 if (target->state == TARGET_HALTED)
372 target->halt_issued = false;
373 } else
375 long long t = timeval_ms() - target->halt_issued_time;
376 if (t>1000)
378 target->halt_issued = false;
379 LOG_INFO("Halt timed out, wake up GDB.");
380 target_call_event_callbacks(target, TARGET_EVENT_GDB_HALT);
385 return ERROR_OK;
388 int target_halt(struct target *target)
390 int retval;
391 /* We can't poll until after examine */
392 if (!target_was_examined(target))
394 LOG_ERROR("Target not examined yet");
395 return ERROR_FAIL;
398 retval = target->type->halt(target);
399 if (retval != ERROR_OK)
400 return retval;
402 target->halt_issued = true;
403 target->halt_issued_time = timeval_ms();
405 return ERROR_OK;
408 int target_resume(struct target *target, int current, uint32_t address, int handle_breakpoints, int debug_execution)
410 int retval;
412 /* We can't poll until after examine */
413 if (!target_was_examined(target))
415 LOG_ERROR("Target not examined yet");
416 return ERROR_FAIL;
419 /* note that resume *must* be asynchronous. The CPU can halt before we poll. The CPU can
420 * even halt at the current PC as a result of a software breakpoint being inserted by (a bug?)
421 * the application.
423 if ((retval = target->type->resume(target, current, address, handle_breakpoints, debug_execution)) != ERROR_OK)
424 return retval;
426 return retval;
429 int target_process_reset(struct command_context *cmd_ctx, enum target_reset_mode reset_mode)
431 char buf[100];
432 int retval;
433 Jim_Nvp *n;
434 n = Jim_Nvp_value2name_simple(nvp_reset_modes, reset_mode);
435 if (n->name == NULL) {
436 LOG_ERROR("invalid reset mode");
437 return ERROR_FAIL;
440 /* disable polling during reset to make reset event scripts
441 * more predictable, i.e. dr/irscan & pathmove in events will
442 * not have JTAG operations injected into the middle of a sequence.
444 bool save_poll = jtag_poll_get_enabled();
446 jtag_poll_set_enabled(false);
448 sprintf(buf, "ocd_process_reset %s", n->name);
449 retval = Jim_Eval(interp, buf);
451 jtag_poll_set_enabled(save_poll);
453 if (retval != JIM_OK) {
454 Jim_PrintErrorMessage(interp);
455 return ERROR_FAIL;
458 /* We want any events to be processed before the prompt */
459 retval = target_call_timer_callbacks_now();
461 return retval;
464 static int identity_virt2phys(struct target *target,
465 uint32_t virtual, uint32_t *physical)
467 *physical = virtual;
468 return ERROR_OK;
471 static int no_mmu(struct target *target, int *enabled)
473 *enabled = 0;
474 return ERROR_OK;
477 static int default_examine(struct target *target)
479 target_set_examined(target);
480 return ERROR_OK;
483 int target_examine_one(struct target *target)
485 return target->type->examine(target);
488 static int jtag_enable_callback(enum jtag_event event, void *priv)
490 struct target *target = priv;
492 if (event != JTAG_TAP_EVENT_ENABLE || !target->tap->enabled)
493 return ERROR_OK;
495 jtag_unregister_event_callback(jtag_enable_callback, target);
496 return target_examine_one(target);
500 /* Targets that correctly implement init + examine, i.e.
501 * no communication with target during init:
503 * XScale
505 int target_examine(void)
507 int retval = ERROR_OK;
508 struct target *target;
510 for (target = all_targets; target; target = target->next)
512 /* defer examination, but don't skip it */
513 if (!target->tap->enabled) {
514 jtag_register_event_callback(jtag_enable_callback,
515 target);
516 continue;
518 if ((retval = target_examine_one(target)) != ERROR_OK)
519 return retval;
521 return retval;
523 const char *target_get_name(struct target *target)
525 return target->type->name;
528 static int target_write_memory_imp(struct target *target, uint32_t address, uint32_t size, uint32_t count, uint8_t *buffer)
530 if (!target_was_examined(target))
532 LOG_ERROR("Target not examined yet");
533 return ERROR_FAIL;
535 return target->type->write_memory_imp(target, address, size, count, buffer);
538 static int target_read_memory_imp(struct target *target, uint32_t address, uint32_t size, uint32_t count, uint8_t *buffer)
540 if (!target_was_examined(target))
542 LOG_ERROR("Target not examined yet");
543 return ERROR_FAIL;
545 return target->type->read_memory_imp(target, address, size, count, buffer);
548 static int target_soft_reset_halt_imp(struct target *target)
550 if (!target_was_examined(target))
552 LOG_ERROR("Target not examined yet");
553 return ERROR_FAIL;
555 if (!target->type->soft_reset_halt_imp) {
556 LOG_ERROR("Target %s does not support soft_reset_halt",
557 target->cmd_name);
558 return ERROR_FAIL;
560 return target->type->soft_reset_halt_imp(target);
563 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)
565 if (!target_was_examined(target))
567 LOG_ERROR("Target not examined yet");
568 return ERROR_FAIL;
570 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);
573 int target_read_memory(struct target *target,
574 uint32_t address, uint32_t size, uint32_t count, uint8_t *buffer)
576 return target->type->read_memory(target, address, size, count, buffer);
579 int target_read_phys_memory(struct target *target,
580 uint32_t address, uint32_t size, uint32_t count, uint8_t *buffer)
582 return target->type->read_phys_memory(target, address, size, count, buffer);
585 int target_write_memory(struct target *target,
586 uint32_t address, uint32_t size, uint32_t count, uint8_t *buffer)
588 return target->type->write_memory(target, address, size, count, buffer);
591 int target_write_phys_memory(struct target *target,
592 uint32_t address, uint32_t size, uint32_t count, uint8_t *buffer)
594 return target->type->write_phys_memory(target, address, size, count, buffer);
597 int target_bulk_write_memory(struct target *target,
598 uint32_t address, uint32_t count, uint8_t *buffer)
600 return target->type->bulk_write_memory(target, address, count, buffer);
603 int target_add_breakpoint(struct target *target,
604 struct breakpoint *breakpoint)
606 return target->type->add_breakpoint(target, breakpoint);
608 int target_remove_breakpoint(struct target *target,
609 struct breakpoint *breakpoint)
611 return target->type->remove_breakpoint(target, breakpoint);
614 int target_add_watchpoint(struct target *target,
615 struct watchpoint *watchpoint)
617 return target->type->add_watchpoint(target, watchpoint);
619 int target_remove_watchpoint(struct target *target,
620 struct watchpoint *watchpoint)
622 return target->type->remove_watchpoint(target, watchpoint);
625 int target_get_gdb_reg_list(struct target *target,
626 struct reg **reg_list[], int *reg_list_size)
628 return target->type->get_gdb_reg_list(target, reg_list, reg_list_size);
630 int target_step(struct target *target,
631 int current, uint32_t address, int handle_breakpoints)
633 return target->type->step(target, current, address, handle_breakpoints);
637 int target_run_algorithm(struct target *target,
638 int num_mem_params, struct mem_param *mem_params,
639 int num_reg_params, struct reg_param *reg_param,
640 uint32_t entry_point, uint32_t exit_point,
641 int timeout_ms, void *arch_info)
643 return target->type->run_algorithm(target,
644 num_mem_params, mem_params, num_reg_params, reg_param,
645 entry_point, exit_point, timeout_ms, arch_info);
649 * Reset the @c examined flag for the given target.
650 * Pure paranoia -- targets are zeroed on allocation.
652 static void target_reset_examined(struct target *target)
654 target->examined = false;
659 static int default_mrc(struct target *target, int cpnum, uint32_t op1, uint32_t op2, uint32_t CRn, uint32_t CRm, uint32_t *value)
661 LOG_ERROR("Not implemented: %s", __func__);
662 return ERROR_FAIL;
665 static int default_mcr(struct target *target, int cpnum, uint32_t op1, uint32_t op2, uint32_t CRn, uint32_t CRm, uint32_t value)
667 LOG_ERROR("Not implemented: %s", __func__);
668 return ERROR_FAIL;
671 static int arm_cp_check(struct target *target, int cpnum, uint32_t op1, uint32_t op2, uint32_t CRn, uint32_t CRm)
673 /* basic check */
674 if (!target_was_examined(target))
676 LOG_ERROR("Target not examined yet");
677 return ERROR_FAIL;
680 if ((cpnum <0) || (cpnum > 15))
682 LOG_ERROR("Illegal co-processor %d", cpnum);
683 return ERROR_FAIL;
686 if (op1 > 7)
688 LOG_ERROR("Illegal op1");
689 return ERROR_FAIL;
692 if (op2 > 7)
694 LOG_ERROR("Illegal op2");
695 return ERROR_FAIL;
698 if (CRn > 15)
700 LOG_ERROR("Illegal CRn");
701 return ERROR_FAIL;
704 if (CRm > 15)
706 LOG_ERROR("Illegal CRm");
707 return ERROR_FAIL;
710 return ERROR_OK;
713 int target_mrc(struct target *target, int cpnum, uint32_t op1, uint32_t op2, uint32_t CRn, uint32_t CRm, uint32_t *value)
715 int retval;
717 retval = arm_cp_check(target, cpnum, op1, op2, CRn, CRm);
718 if (retval != ERROR_OK)
719 return retval;
721 return target->type->mrc(target, cpnum, op1, op2, CRn, CRm, value);
724 int target_mcr(struct target *target, int cpnum, uint32_t op1, uint32_t op2, uint32_t CRn, uint32_t CRm, uint32_t value)
726 int retval;
728 retval = arm_cp_check(target, cpnum, op1, op2, CRn, CRm);
729 if (retval != ERROR_OK)
730 return retval;
732 return target->type->mcr(target, cpnum, op1, op2, CRn, CRm, value);
735 static int
736 err_read_phys_memory(struct target *target, uint32_t address,
737 uint32_t size, uint32_t count, uint8_t *buffer)
739 LOG_ERROR("Not implemented: %s", __func__);
740 return ERROR_FAIL;
743 static int
744 err_write_phys_memory(struct target *target, uint32_t address,
745 uint32_t size, uint32_t count, uint8_t *buffer)
747 LOG_ERROR("Not implemented: %s", __func__);
748 return ERROR_FAIL;
751 int target_init(struct command_context *cmd_ctx)
753 struct target *target;
754 int retval;
756 for (target = all_targets; target; target = target->next) {
757 struct target_type *type = target->type;
759 target_reset_examined(target);
760 if (target->type->examine == NULL)
762 target->type->examine = default_examine;
765 if ((retval = target->type->init_target(cmd_ctx, target)) != ERROR_OK)
767 LOG_ERROR("target '%s' init failed", target_get_name(target));
768 return retval;
772 * @todo MCR/MRC are ARM-specific; don't require them in
773 * all targets, or for ARMs without coprocessors.
775 if (target->type->mcr == NULL)
777 target->type->mcr = default_mcr;
778 } else
780 /* FIX! multiple targets will generally register global commands
781 * multiple times. Only register this one if *one* of the
782 * targets need the command. Hmm... make it a command on the
783 * Jim Tcl target object?
785 register_jim(cmd_ctx, "mcr", jim_mcrmrc, "write coprocessor <cpnum> <op1> <op2> <CRn> <CRm> <value>");
788 if (target->type->mrc == NULL)
790 target->type->mrc = default_mrc;
791 } else
793 register_jim(cmd_ctx, "mrc", jim_mcrmrc, "read coprocessor <cpnum> <op1> <op2> <CRn> <CRm>");
798 * @todo get rid of those *memory_imp() methods, now that all
799 * callers are using target_*_memory() accessors ... and make
800 * sure the "physical" paths handle the same issues.
803 /* a non-invasive way(in terms of patches) to add some code that
804 * runs before the type->write/read_memory implementation
806 target->type->write_memory_imp = target->type->write_memory;
807 target->type->write_memory = target_write_memory_imp;
808 target->type->read_memory_imp = target->type->read_memory;
809 target->type->read_memory = target_read_memory_imp;
810 target->type->soft_reset_halt_imp = target->type->soft_reset_halt;
811 target->type->soft_reset_halt = target_soft_reset_halt_imp;
812 target->type->run_algorithm_imp = target->type->run_algorithm;
813 target->type->run_algorithm = target_run_algorithm_imp;
815 /* Sanity-check MMU support ... stub in what we must, to help
816 * implement it in stages, but warn if we need to do so.
818 if (type->mmu) {
819 if (type->write_phys_memory == NULL) {
820 LOG_ERROR("type '%s' is missing %s",
821 type->name,
822 "write_phys_memory");
823 type->write_phys_memory = err_write_phys_memory;
825 if (type->read_phys_memory == NULL) {
826 LOG_ERROR("type '%s' is missing %s",
827 type->name,
828 "read_phys_memory");
829 type->read_phys_memory = err_read_phys_memory;
831 if (type->virt2phys == NULL) {
832 LOG_ERROR("type '%s' is missing %s",
833 type->name,
834 "virt2phys");
835 type->virt2phys = identity_virt2phys;
838 /* Make sure no-MMU targets all behave the same: make no
839 * distinction between physical and virtual addresses, and
840 * ensure that virt2phys() is always an identity mapping.
842 } else {
843 if (type->write_phys_memory
844 || type->read_phys_memory
845 || type->virt2phys)
846 LOG_WARNING("type '%s' has broken MMU hooks",
847 type->name);
849 type->mmu = no_mmu;
850 type->write_phys_memory = type->write_memory;
851 type->read_phys_memory = type->read_memory;
852 type->virt2phys = identity_virt2phys;
856 if (all_targets)
858 if ((retval = target_register_user_commands(cmd_ctx)) != ERROR_OK)
859 return retval;
860 if ((retval = target_register_timer_callback(handle_target, 100, 1, NULL)) != ERROR_OK)
861 return retval;
864 return ERROR_OK;
867 int target_register_event_callback(int (*callback)(struct target *target, enum target_event event, void *priv), void *priv)
869 struct target_event_callback **callbacks_p = &target_event_callbacks;
871 if (callback == NULL)
873 return ERROR_INVALID_ARGUMENTS;
876 if (*callbacks_p)
878 while ((*callbacks_p)->next)
879 callbacks_p = &((*callbacks_p)->next);
880 callbacks_p = &((*callbacks_p)->next);
883 (*callbacks_p) = malloc(sizeof(struct target_event_callback));
884 (*callbacks_p)->callback = callback;
885 (*callbacks_p)->priv = priv;
886 (*callbacks_p)->next = NULL;
888 return ERROR_OK;
891 int target_register_timer_callback(int (*callback)(void *priv), int time_ms, int periodic, void *priv)
893 struct target_timer_callback **callbacks_p = &target_timer_callbacks;
894 struct timeval now;
896 if (callback == NULL)
898 return ERROR_INVALID_ARGUMENTS;
901 if (*callbacks_p)
903 while ((*callbacks_p)->next)
904 callbacks_p = &((*callbacks_p)->next);
905 callbacks_p = &((*callbacks_p)->next);
908 (*callbacks_p) = malloc(sizeof(struct target_timer_callback));
909 (*callbacks_p)->callback = callback;
910 (*callbacks_p)->periodic = periodic;
911 (*callbacks_p)->time_ms = time_ms;
913 gettimeofday(&now, NULL);
914 (*callbacks_p)->when.tv_usec = now.tv_usec + (time_ms % 1000) * 1000;
915 time_ms -= (time_ms % 1000);
916 (*callbacks_p)->when.tv_sec = now.tv_sec + (time_ms / 1000);
917 if ((*callbacks_p)->when.tv_usec > 1000000)
919 (*callbacks_p)->when.tv_usec = (*callbacks_p)->when.tv_usec - 1000000;
920 (*callbacks_p)->when.tv_sec += 1;
923 (*callbacks_p)->priv = priv;
924 (*callbacks_p)->next = NULL;
926 return ERROR_OK;
929 int target_unregister_event_callback(int (*callback)(struct target *target, enum target_event event, void *priv), void *priv)
931 struct target_event_callback **p = &target_event_callbacks;
932 struct target_event_callback *c = target_event_callbacks;
934 if (callback == NULL)
936 return ERROR_INVALID_ARGUMENTS;
939 while (c)
941 struct target_event_callback *next = c->next;
942 if ((c->callback == callback) && (c->priv == priv))
944 *p = next;
945 free(c);
946 return ERROR_OK;
948 else
949 p = &(c->next);
950 c = next;
953 return ERROR_OK;
956 int target_unregister_timer_callback(int (*callback)(void *priv), void *priv)
958 struct target_timer_callback **p = &target_timer_callbacks;
959 struct target_timer_callback *c = target_timer_callbacks;
961 if (callback == NULL)
963 return ERROR_INVALID_ARGUMENTS;
966 while (c)
968 struct target_timer_callback *next = c->next;
969 if ((c->callback == callback) && (c->priv == priv))
971 *p = next;
972 free(c);
973 return ERROR_OK;
975 else
976 p = &(c->next);
977 c = next;
980 return ERROR_OK;
983 int target_call_event_callbacks(struct target *target, enum target_event event)
985 struct target_event_callback *callback = target_event_callbacks;
986 struct target_event_callback *next_callback;
988 if (event == TARGET_EVENT_HALTED)
990 /* execute early halted first */
991 target_call_event_callbacks(target, TARGET_EVENT_GDB_HALT);
994 LOG_DEBUG("target event %i (%s)",
995 event,
996 Jim_Nvp_value2name_simple(nvp_target_event, event)->name);
998 target_handle_event(target, event);
1000 while (callback)
1002 next_callback = callback->next;
1003 callback->callback(target, event, callback->priv);
1004 callback = next_callback;
1007 return ERROR_OK;
1010 static int target_timer_callback_periodic_restart(
1011 struct target_timer_callback *cb, struct timeval *now)
1013 int time_ms = cb->time_ms;
1014 cb->when.tv_usec = now->tv_usec + (time_ms % 1000) * 1000;
1015 time_ms -= (time_ms % 1000);
1016 cb->when.tv_sec = now->tv_sec + time_ms / 1000;
1017 if (cb->when.tv_usec > 1000000)
1019 cb->when.tv_usec = cb->when.tv_usec - 1000000;
1020 cb->when.tv_sec += 1;
1022 return ERROR_OK;
1025 static int target_call_timer_callback(struct target_timer_callback *cb,
1026 struct timeval *now)
1028 cb->callback(cb->priv);
1030 if (cb->periodic)
1031 return target_timer_callback_periodic_restart(cb, now);
1033 return target_unregister_timer_callback(cb->callback, cb->priv);
1036 static int target_call_timer_callbacks_check_time(int checktime)
1038 keep_alive();
1040 struct timeval now;
1041 gettimeofday(&now, NULL);
1043 struct target_timer_callback *callback = target_timer_callbacks;
1044 while (callback)
1046 // cleaning up may unregister and free this callback
1047 struct target_timer_callback *next_callback = callback->next;
1049 bool call_it = callback->callback &&
1050 ((!checktime && callback->periodic) ||
1051 now.tv_sec > callback->when.tv_sec ||
1052 (now.tv_sec == callback->when.tv_sec &&
1053 now.tv_usec >= callback->when.tv_usec));
1055 if (call_it)
1057 int retval = target_call_timer_callback(callback, &now);
1058 if (retval != ERROR_OK)
1059 return retval;
1062 callback = next_callback;
1065 return ERROR_OK;
1068 int target_call_timer_callbacks(void)
1070 return target_call_timer_callbacks_check_time(1);
1073 /* invoke periodic callbacks immediately */
1074 int target_call_timer_callbacks_now(void)
1076 return target_call_timer_callbacks_check_time(0);
1079 int target_alloc_working_area(struct target *target, uint32_t size, struct working_area **area)
1081 struct working_area *c = target->working_areas;
1082 struct working_area *new_wa = NULL;
1084 /* Reevaluate working area address based on MMU state*/
1085 if (target->working_areas == NULL)
1087 int retval;
1088 int enabled;
1090 retval = target->type->mmu(target, &enabled);
1091 if (retval != ERROR_OK)
1093 return retval;
1096 if (!enabled) {
1097 if (target->working_area_phys_spec) {
1098 LOG_DEBUG("MMU disabled, using physical "
1099 "address for working memory 0x%08x",
1100 (unsigned)target->working_area_phys);
1101 target->working_area = target->working_area_phys;
1102 } else {
1103 LOG_ERROR("No working memory available. "
1104 "Specify -work-area-phys to target.");
1105 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
1107 } else {
1108 if (target->working_area_virt_spec) {
1109 LOG_DEBUG("MMU enabled, using virtual "
1110 "address for working memory 0x%08x",
1111 (unsigned)target->working_area_virt);
1112 target->working_area = target->working_area_virt;
1113 } else {
1114 LOG_ERROR("No working memory available. "
1115 "Specify -work-area-virt to target.");
1116 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
1121 /* only allocate multiples of 4 byte */
1122 if (size % 4)
1124 LOG_ERROR("BUG: code tried to allocate unaligned number of bytes (0x%08x), padding", ((unsigned)(size)));
1125 size = (size + 3) & (~3);
1128 /* see if there's already a matching working area */
1129 while (c)
1131 if ((c->free) && (c->size == size))
1133 new_wa = c;
1134 break;
1136 c = c->next;
1139 /* if not, allocate a new one */
1140 if (!new_wa)
1142 struct working_area **p = &target->working_areas;
1143 uint32_t first_free = target->working_area;
1144 uint32_t free_size = target->working_area_size;
1146 c = target->working_areas;
1147 while (c)
1149 first_free += c->size;
1150 free_size -= c->size;
1151 p = &c->next;
1152 c = c->next;
1155 if (free_size < size)
1157 LOG_WARNING("not enough working area available(requested %u, free %u)",
1158 (unsigned)(size), (unsigned)(free_size));
1159 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
1162 LOG_DEBUG("allocated new working area at address 0x%08x", (unsigned)first_free);
1164 new_wa = malloc(sizeof(struct working_area));
1165 new_wa->next = NULL;
1166 new_wa->size = size;
1167 new_wa->address = first_free;
1169 if (target->backup_working_area)
1171 int retval;
1172 new_wa->backup = malloc(new_wa->size);
1173 if ((retval = target_read_memory(target, new_wa->address, 4, new_wa->size / 4, new_wa->backup)) != ERROR_OK)
1175 free(new_wa->backup);
1176 free(new_wa);
1177 return retval;
1180 else
1182 new_wa->backup = NULL;
1185 /* put new entry in list */
1186 *p = new_wa;
1189 /* mark as used, and return the new (reused) area */
1190 new_wa->free = 0;
1191 *area = new_wa;
1193 /* user pointer */
1194 new_wa->user = area;
1196 return ERROR_OK;
1199 int target_free_working_area_restore(struct target *target, struct working_area *area, int restore)
1201 if (area->free)
1202 return ERROR_OK;
1204 if (restore && target->backup_working_area)
1206 int retval;
1207 if ((retval = target_write_memory(target, area->address, 4, area->size / 4, area->backup)) != ERROR_OK)
1208 return retval;
1211 area->free = 1;
1213 /* mark user pointer invalid */
1214 *area->user = NULL;
1215 area->user = NULL;
1217 return ERROR_OK;
1220 int target_free_working_area(struct target *target, struct working_area *area)
1222 return target_free_working_area_restore(target, area, 1);
1225 /* free resources and restore memory, if restoring memory fails,
1226 * free up resources anyway
1228 void target_free_all_working_areas_restore(struct target *target, int restore)
1230 struct working_area *c = target->working_areas;
1232 while (c)
1234 struct working_area *next = c->next;
1235 target_free_working_area_restore(target, c, restore);
1237 if (c->backup)
1238 free(c->backup);
1240 free(c);
1242 c = next;
1245 target->working_areas = NULL;
1248 void target_free_all_working_areas(struct target *target)
1250 target_free_all_working_areas_restore(target, 1);
1253 int target_arch_state(struct target *target)
1255 int retval;
1256 if (target == NULL)
1258 LOG_USER("No target has been configured");
1259 return ERROR_OK;
1262 LOG_USER("target state: %s", target_state_name( target ));
1264 if (target->state != TARGET_HALTED)
1265 return ERROR_OK;
1267 retval = target->type->arch_state(target);
1268 return retval;
1271 /* Single aligned words are guaranteed to use 16 or 32 bit access
1272 * mode respectively, otherwise data is handled as quickly as
1273 * possible
1275 int target_write_buffer(struct target *target, uint32_t address, uint32_t size, uint8_t *buffer)
1277 int retval;
1278 LOG_DEBUG("writing buffer of %i byte at 0x%8.8x",
1279 (int)size, (unsigned)address);
1281 if (!target_was_examined(target))
1283 LOG_ERROR("Target not examined yet");
1284 return ERROR_FAIL;
1287 if (size == 0) {
1288 return ERROR_OK;
1291 if ((address + size - 1) < address)
1293 /* GDB can request this when e.g. PC is 0xfffffffc*/
1294 LOG_ERROR("address + size wrapped(0x%08x, 0x%08x)",
1295 (unsigned)address,
1296 (unsigned)size);
1297 return ERROR_FAIL;
1300 if (((address % 2) == 0) && (size == 2))
1302 return target_write_memory(target, address, 2, 1, buffer);
1305 /* handle unaligned head bytes */
1306 if (address % 4)
1308 uint32_t unaligned = 4 - (address % 4);
1310 if (unaligned > size)
1311 unaligned = size;
1313 if ((retval = target_write_memory(target, address, 1, unaligned, buffer)) != ERROR_OK)
1314 return retval;
1316 buffer += unaligned;
1317 address += unaligned;
1318 size -= unaligned;
1321 /* handle aligned words */
1322 if (size >= 4)
1324 int aligned = size - (size % 4);
1326 /* use bulk writes above a certain limit. This may have to be changed */
1327 if (aligned > 128)
1329 if ((retval = target->type->bulk_write_memory(target, address, aligned / 4, buffer)) != ERROR_OK)
1330 return retval;
1332 else
1334 if ((retval = target_write_memory(target, address, 4, aligned / 4, buffer)) != ERROR_OK)
1335 return retval;
1338 buffer += aligned;
1339 address += aligned;
1340 size -= aligned;
1343 /* handle tail writes of less than 4 bytes */
1344 if (size > 0)
1346 if ((retval = target_write_memory(target, address, 1, size, buffer)) != ERROR_OK)
1347 return retval;
1350 return ERROR_OK;
1353 /* Single aligned words are guaranteed to use 16 or 32 bit access
1354 * mode respectively, otherwise data is handled as quickly as
1355 * possible
1357 int target_read_buffer(struct target *target, uint32_t address, uint32_t size, uint8_t *buffer)
1359 int retval;
1360 LOG_DEBUG("reading buffer of %i byte at 0x%8.8x",
1361 (int)size, (unsigned)address);
1363 if (!target_was_examined(target))
1365 LOG_ERROR("Target not examined yet");
1366 return ERROR_FAIL;
1369 if (size == 0) {
1370 return ERROR_OK;
1373 if ((address + size - 1) < address)
1375 /* GDB can request this when e.g. PC is 0xfffffffc*/
1376 LOG_ERROR("address + size wrapped(0x%08" PRIx32 ", 0x%08" PRIx32 ")",
1377 address,
1378 size);
1379 return ERROR_FAIL;
1382 if (((address % 2) == 0) && (size == 2))
1384 return target_read_memory(target, address, 2, 1, buffer);
1387 /* handle unaligned head bytes */
1388 if (address % 4)
1390 uint32_t unaligned = 4 - (address % 4);
1392 if (unaligned > size)
1393 unaligned = size;
1395 if ((retval = target_read_memory(target, address, 1, unaligned, buffer)) != ERROR_OK)
1396 return retval;
1398 buffer += unaligned;
1399 address += unaligned;
1400 size -= unaligned;
1403 /* handle aligned words */
1404 if (size >= 4)
1406 int aligned = size - (size % 4);
1408 if ((retval = target_read_memory(target, address, 4, aligned / 4, buffer)) != ERROR_OK)
1409 return retval;
1411 buffer += aligned;
1412 address += aligned;
1413 size -= aligned;
1416 /*prevent byte access when possible (avoid AHB access limitations in some cases)*/
1417 if(size >=2)
1419 int aligned = size - (size%2);
1420 retval = target_read_memory(target, address, 2, aligned / 2, buffer);
1421 if (retval != ERROR_OK)
1422 return retval;
1424 buffer += aligned;
1425 address += aligned;
1426 size -= aligned;
1428 /* handle tail writes of less than 4 bytes */
1429 if (size > 0)
1431 if ((retval = target_read_memory(target, address, 1, size, buffer)) != ERROR_OK)
1432 return retval;
1435 return ERROR_OK;
1438 int target_checksum_memory(struct target *target, uint32_t address, uint32_t size, uint32_t* crc)
1440 uint8_t *buffer;
1441 int retval;
1442 uint32_t i;
1443 uint32_t checksum = 0;
1444 if (!target_was_examined(target))
1446 LOG_ERROR("Target not examined yet");
1447 return ERROR_FAIL;
1450 if ((retval = target->type->checksum_memory(target, address,
1451 size, &checksum)) != ERROR_OK)
1453 buffer = malloc(size);
1454 if (buffer == NULL)
1456 LOG_ERROR("error allocating buffer for section (%d bytes)", (int)size);
1457 return ERROR_INVALID_ARGUMENTS;
1459 retval = target_read_buffer(target, address, size, buffer);
1460 if (retval != ERROR_OK)
1462 free(buffer);
1463 return retval;
1466 /* convert to target endianess */
1467 for (i = 0; i < (size/sizeof(uint32_t)); i++)
1469 uint32_t target_data;
1470 target_data = target_buffer_get_u32(target, &buffer[i*sizeof(uint32_t)]);
1471 target_buffer_set_u32(target, &buffer[i*sizeof(uint32_t)], target_data);
1474 retval = image_calculate_checksum(buffer, size, &checksum);
1475 free(buffer);
1478 *crc = checksum;
1480 return retval;
1483 int target_blank_check_memory(struct target *target, uint32_t address, uint32_t size, uint32_t* blank)
1485 int retval;
1486 if (!target_was_examined(target))
1488 LOG_ERROR("Target not examined yet");
1489 return ERROR_FAIL;
1492 if (target->type->blank_check_memory == 0)
1493 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
1495 retval = target->type->blank_check_memory(target, address, size, blank);
1497 return retval;
1500 int target_read_u32(struct target *target, uint32_t address, uint32_t *value)
1502 uint8_t value_buf[4];
1503 if (!target_was_examined(target))
1505 LOG_ERROR("Target not examined yet");
1506 return ERROR_FAIL;
1509 int retval = target_read_memory(target, address, 4, 1, value_buf);
1511 if (retval == ERROR_OK)
1513 *value = target_buffer_get_u32(target, value_buf);
1514 LOG_DEBUG("address: 0x%8.8" PRIx32 ", value: 0x%8.8" PRIx32 "",
1515 address,
1516 *value);
1518 else
1520 *value = 0x0;
1521 LOG_DEBUG("address: 0x%8.8" PRIx32 " failed",
1522 address);
1525 return retval;
1528 int target_read_u16(struct target *target, uint32_t address, uint16_t *value)
1530 uint8_t value_buf[2];
1531 if (!target_was_examined(target))
1533 LOG_ERROR("Target not examined yet");
1534 return ERROR_FAIL;
1537 int retval = target_read_memory(target, address, 2, 1, value_buf);
1539 if (retval == ERROR_OK)
1541 *value = target_buffer_get_u16(target, value_buf);
1542 LOG_DEBUG("address: 0x%8.8" PRIx32 ", value: 0x%4.4x",
1543 address,
1544 *value);
1546 else
1548 *value = 0x0;
1549 LOG_DEBUG("address: 0x%8.8" PRIx32 " failed",
1550 address);
1553 return retval;
1556 int target_read_u8(struct target *target, uint32_t address, uint8_t *value)
1558 int retval = target_read_memory(target, address, 1, 1, value);
1559 if (!target_was_examined(target))
1561 LOG_ERROR("Target not examined yet");
1562 return ERROR_FAIL;
1565 if (retval == ERROR_OK)
1567 LOG_DEBUG("address: 0x%8.8" PRIx32 ", value: 0x%2.2x",
1568 address,
1569 *value);
1571 else
1573 *value = 0x0;
1574 LOG_DEBUG("address: 0x%8.8" PRIx32 " failed",
1575 address);
1578 return retval;
1581 int target_write_u32(struct target *target, uint32_t address, uint32_t value)
1583 int retval;
1584 uint8_t value_buf[4];
1585 if (!target_was_examined(target))
1587 LOG_ERROR("Target not examined yet");
1588 return ERROR_FAIL;
1591 LOG_DEBUG("address: 0x%8.8" PRIx32 ", value: 0x%8.8" PRIx32 "",
1592 address,
1593 value);
1595 target_buffer_set_u32(target, value_buf, value);
1596 if ((retval = target_write_memory(target, address, 4, 1, value_buf)) != ERROR_OK)
1598 LOG_DEBUG("failed: %i", retval);
1601 return retval;
1604 int target_write_u16(struct target *target, uint32_t address, uint16_t value)
1606 int retval;
1607 uint8_t value_buf[2];
1608 if (!target_was_examined(target))
1610 LOG_ERROR("Target not examined yet");
1611 return ERROR_FAIL;
1614 LOG_DEBUG("address: 0x%8.8" PRIx32 ", value: 0x%8.8x",
1615 address,
1616 value);
1618 target_buffer_set_u16(target, value_buf, value);
1619 if ((retval = target_write_memory(target, address, 2, 1, value_buf)) != ERROR_OK)
1621 LOG_DEBUG("failed: %i", retval);
1624 return retval;
1627 int target_write_u8(struct target *target, uint32_t address, uint8_t value)
1629 int retval;
1630 if (!target_was_examined(target))
1632 LOG_ERROR("Target not examined yet");
1633 return ERROR_FAIL;
1636 LOG_DEBUG("address: 0x%8.8" PRIx32 ", value: 0x%2.2x",
1637 address, value);
1639 if ((retval = target_write_memory(target, address, 1, 1, &value)) != ERROR_OK)
1641 LOG_DEBUG("failed: %i", retval);
1644 return retval;
1647 COMMAND_HANDLER(handle_targets_command)
1649 struct target *target = all_targets;
1651 if (CMD_ARGC == 1)
1653 target = get_target(CMD_ARGV[0]);
1654 if (target == NULL) {
1655 command_print(CMD_CTX,"Target: %s is unknown, try one of:\n", CMD_ARGV[0]);
1656 goto DumpTargets;
1658 if (!target->tap->enabled) {
1659 command_print(CMD_CTX,"Target: TAP %s is disabled, "
1660 "can't be the current target\n",
1661 target->tap->dotted_name);
1662 return ERROR_FAIL;
1665 CMD_CTX->current_target = target->target_number;
1666 return ERROR_OK;
1668 DumpTargets:
1670 target = all_targets;
1671 command_print(CMD_CTX, " TargetName Type Endian TapName State ");
1672 command_print(CMD_CTX, "-- ------------------ ---------- ------ ------------------ ------------");
1673 while (target)
1675 const char *state;
1676 char marker = ' ';
1678 if (target->tap->enabled)
1679 state = target_state_name( target );
1680 else
1681 state = "tap-disabled";
1683 if (CMD_CTX->current_target == target->target_number)
1684 marker = '*';
1686 /* keep columns lined up to match the headers above */
1687 command_print(CMD_CTX, "%2d%c %-18s %-10s %-6s %-18s %s",
1688 target->target_number,
1689 marker,
1690 target->cmd_name,
1691 target_get_name(target),
1692 Jim_Nvp_value2name_simple(nvp_target_endian,
1693 target->endianness)->name,
1694 target->tap->dotted_name,
1695 state);
1696 target = target->next;
1699 return ERROR_OK;
1702 /* every 300ms we check for reset & powerdropout and issue a "reset halt" if so. */
1704 static int powerDropout;
1705 static int srstAsserted;
1707 static int runPowerRestore;
1708 static int runPowerDropout;
1709 static int runSrstAsserted;
1710 static int runSrstDeasserted;
1712 static int sense_handler(void)
1714 static int prevSrstAsserted = 0;
1715 static int prevPowerdropout = 0;
1717 int retval;
1718 if ((retval = jtag_power_dropout(&powerDropout)) != ERROR_OK)
1719 return retval;
1721 int powerRestored;
1722 powerRestored = prevPowerdropout && !powerDropout;
1723 if (powerRestored)
1725 runPowerRestore = 1;
1728 long long current = timeval_ms();
1729 static long long lastPower = 0;
1730 int waitMore = lastPower + 2000 > current;
1731 if (powerDropout && !waitMore)
1733 runPowerDropout = 1;
1734 lastPower = current;
1737 if ((retval = jtag_srst_asserted(&srstAsserted)) != ERROR_OK)
1738 return retval;
1740 int srstDeasserted;
1741 srstDeasserted = prevSrstAsserted && !srstAsserted;
1743 static long long lastSrst = 0;
1744 waitMore = lastSrst + 2000 > current;
1745 if (srstDeasserted && !waitMore)
1747 runSrstDeasserted = 1;
1748 lastSrst = current;
1751 if (!prevSrstAsserted && srstAsserted)
1753 runSrstAsserted = 1;
1756 prevSrstAsserted = srstAsserted;
1757 prevPowerdropout = powerDropout;
1759 if (srstDeasserted || powerRestored)
1761 /* Other than logging the event we can't do anything here.
1762 * Issuing a reset is a particularly bad idea as we might
1763 * be inside a reset already.
1767 return ERROR_OK;
1770 static void target_call_event_callbacks_all(enum target_event e) {
1771 struct target *target;
1772 target = all_targets;
1773 while (target) {
1774 target_call_event_callbacks(target, e);
1775 target = target->next;
1779 /* process target state changes */
1780 int handle_target(void *priv)
1782 int retval = ERROR_OK;
1784 /* we do not want to recurse here... */
1785 static int recursive = 0;
1786 if (! recursive)
1788 recursive = 1;
1789 sense_handler();
1790 /* danger! running these procedures can trigger srst assertions and power dropouts.
1791 * We need to avoid an infinite loop/recursion here and we do that by
1792 * clearing the flags after running these events.
1794 int did_something = 0;
1795 if (runSrstAsserted)
1797 target_call_event_callbacks_all(TARGET_EVENT_GDB_HALT);
1798 Jim_Eval(interp, "srst_asserted");
1799 did_something = 1;
1801 if (runSrstDeasserted)
1803 Jim_Eval(interp, "srst_deasserted");
1804 did_something = 1;
1806 if (runPowerDropout)
1808 target_call_event_callbacks_all(TARGET_EVENT_GDB_HALT);
1809 Jim_Eval(interp, "power_dropout");
1810 did_something = 1;
1812 if (runPowerRestore)
1814 Jim_Eval(interp, "power_restore");
1815 did_something = 1;
1818 if (did_something)
1820 /* clear detect flags */
1821 sense_handler();
1824 /* clear action flags */
1826 runSrstAsserted = 0;
1827 runSrstDeasserted = 0;
1828 runPowerRestore = 0;
1829 runPowerDropout = 0;
1831 recursive = 0;
1834 /* Poll targets for state changes unless that's globally disabled.
1835 * Skip targets that are currently disabled.
1837 for (struct target *target = all_targets;
1838 is_jtag_poll_safe() && target;
1839 target = target->next)
1841 if (!target->tap->enabled)
1842 continue;
1844 /* only poll target if we've got power and srst isn't asserted */
1845 if (!powerDropout && !srstAsserted)
1847 /* polling may fail silently until the target has been examined */
1848 if ((retval = target_poll(target)) != ERROR_OK)
1850 target_call_event_callbacks(target, TARGET_EVENT_GDB_HALT);
1851 return retval;
1856 return retval;
1859 COMMAND_HANDLER(handle_reg_command)
1861 struct target *target;
1862 struct reg *reg = NULL;
1863 unsigned count = 0;
1864 char *value;
1866 LOG_DEBUG("-");
1868 target = get_current_target(CMD_CTX);
1870 /* list all available registers for the current target */
1871 if (CMD_ARGC == 0)
1873 struct reg_cache *cache = target->reg_cache;
1875 count = 0;
1876 while (cache)
1878 unsigned i;
1880 command_print(CMD_CTX, "===== %s", cache->name);
1882 for (i = 0, reg = cache->reg_list;
1883 i < cache->num_regs;
1884 i++, reg++, count++)
1886 /* only print cached values if they are valid */
1887 if (reg->valid) {
1888 value = buf_to_str(reg->value,
1889 reg->size, 16);
1890 command_print(CMD_CTX,
1891 "(%i) %s (/%" PRIu32 "): 0x%s%s",
1892 count, reg->name,
1893 reg->size, value,
1894 reg->dirty
1895 ? " (dirty)"
1896 : "");
1897 free(value);
1898 } else {
1899 command_print(CMD_CTX, "(%i) %s (/%" PRIu32 ")",
1900 count, reg->name,
1901 reg->size) ;
1904 cache = cache->next;
1907 return ERROR_OK;
1910 /* access a single register by its ordinal number */
1911 if ((CMD_ARGV[0][0] >= '0') && (CMD_ARGV[0][0] <= '9'))
1913 unsigned num;
1914 COMMAND_PARSE_NUMBER(uint, CMD_ARGV[0], num);
1916 struct reg_cache *cache = target->reg_cache;
1917 count = 0;
1918 while (cache)
1920 unsigned i;
1921 for (i = 0; i < cache->num_regs; i++)
1923 if (count++ == num)
1925 reg = &cache->reg_list[i];
1926 break;
1929 if (reg)
1930 break;
1931 cache = cache->next;
1934 if (!reg)
1936 command_print(CMD_CTX, "%i is out of bounds, the current target has only %i registers (0 - %i)", num, count, count - 1);
1937 return ERROR_OK;
1939 } else /* access a single register by its name */
1941 reg = register_get_by_name(target->reg_cache, CMD_ARGV[0], 1);
1943 if (!reg)
1945 command_print(CMD_CTX, "register %s not found in current target", CMD_ARGV[0]);
1946 return ERROR_OK;
1950 /* display a register */
1951 if ((CMD_ARGC == 1) || ((CMD_ARGC == 2) && !((CMD_ARGV[1][0] >= '0') && (CMD_ARGV[1][0] <= '9'))))
1953 if ((CMD_ARGC == 2) && (strcmp(CMD_ARGV[1], "force") == 0))
1954 reg->valid = 0;
1956 if (reg->valid == 0)
1958 reg->type->get(reg);
1960 value = buf_to_str(reg->value, reg->size, 16);
1961 command_print(CMD_CTX, "%s (/%i): 0x%s", reg->name, (int)(reg->size), value);
1962 free(value);
1963 return ERROR_OK;
1966 /* set register value */
1967 if (CMD_ARGC == 2)
1969 uint8_t *buf = malloc(DIV_ROUND_UP(reg->size, 8));
1970 str_to_buf(CMD_ARGV[1], strlen(CMD_ARGV[1]), buf, reg->size, 0);
1972 reg->type->set(reg, buf);
1974 value = buf_to_str(reg->value, reg->size, 16);
1975 command_print(CMD_CTX, "%s (/%i): 0x%s", reg->name, (int)(reg->size), value);
1976 free(value);
1978 free(buf);
1980 return ERROR_OK;
1983 command_print(CMD_CTX, "usage: reg <#|name> [value]");
1985 return ERROR_OK;
1988 COMMAND_HANDLER(handle_poll_command)
1990 int retval = ERROR_OK;
1991 struct target *target = get_current_target(CMD_CTX);
1993 if (CMD_ARGC == 0)
1995 command_print(CMD_CTX, "background polling: %s",
1996 jtag_poll_get_enabled() ? "on" : "off");
1997 command_print(CMD_CTX, "TAP: %s (%s)",
1998 target->tap->dotted_name,
1999 target->tap->enabled ? "enabled" : "disabled");
2000 if (!target->tap->enabled)
2001 return ERROR_OK;
2002 if ((retval = target_poll(target)) != ERROR_OK)
2003 return retval;
2004 if ((retval = target_arch_state(target)) != ERROR_OK)
2005 return retval;
2007 else if (CMD_ARGC == 1)
2009 bool enable;
2010 COMMAND_PARSE_ON_OFF(CMD_ARGV[0], enable);
2011 jtag_poll_set_enabled(enable);
2013 else
2015 return ERROR_COMMAND_SYNTAX_ERROR;
2018 return retval;
2021 COMMAND_HANDLER(handle_wait_halt_command)
2023 if (CMD_ARGC > 1)
2024 return ERROR_COMMAND_SYNTAX_ERROR;
2026 unsigned ms = 5000;
2027 if (1 == CMD_ARGC)
2029 int retval = parse_uint(CMD_ARGV[0], &ms);
2030 if (ERROR_OK != retval)
2032 command_print(CMD_CTX, "usage: %s [seconds]", CMD_NAME);
2033 return ERROR_COMMAND_SYNTAX_ERROR;
2035 // convert seconds (given) to milliseconds (needed)
2036 ms *= 1000;
2039 struct target *target = get_current_target(CMD_CTX);
2040 return target_wait_state(target, TARGET_HALTED, ms);
2043 /* wait for target state to change. The trick here is to have a low
2044 * latency for short waits and not to suck up all the CPU time
2045 * on longer waits.
2047 * After 500ms, keep_alive() is invoked
2049 int target_wait_state(struct target *target, enum target_state state, int ms)
2051 int retval;
2052 long long then = 0, cur;
2053 int once = 1;
2055 for (;;)
2057 if ((retval = target_poll(target)) != ERROR_OK)
2058 return retval;
2059 if (target->state == state)
2061 break;
2063 cur = timeval_ms();
2064 if (once)
2066 once = 0;
2067 then = timeval_ms();
2068 LOG_DEBUG("waiting for target %s...",
2069 Jim_Nvp_value2name_simple(nvp_target_state,state)->name);
2072 if (cur-then > 500)
2074 keep_alive();
2077 if ((cur-then) > ms)
2079 LOG_ERROR("timed out while waiting for target %s",
2080 Jim_Nvp_value2name_simple(nvp_target_state,state)->name);
2081 return ERROR_FAIL;
2085 return ERROR_OK;
2088 COMMAND_HANDLER(handle_halt_command)
2090 LOG_DEBUG("-");
2092 struct target *target = get_current_target(CMD_CTX);
2093 int retval = target_halt(target);
2094 if (ERROR_OK != retval)
2095 return retval;
2097 if (CMD_ARGC == 1)
2099 unsigned wait;
2100 retval = parse_uint(CMD_ARGV[0], &wait);
2101 if (ERROR_OK != retval)
2102 return ERROR_COMMAND_SYNTAX_ERROR;
2103 if (!wait)
2104 return ERROR_OK;
2107 return CALL_COMMAND_HANDLER(handle_wait_halt_command);
2110 COMMAND_HANDLER(handle_soft_reset_halt_command)
2112 struct target *target = get_current_target(CMD_CTX);
2114 LOG_USER("requesting target halt and executing a soft reset");
2116 target->type->soft_reset_halt(target);
2118 return ERROR_OK;
2121 COMMAND_HANDLER(handle_reset_command)
2123 if (CMD_ARGC > 1)
2124 return ERROR_COMMAND_SYNTAX_ERROR;
2126 enum target_reset_mode reset_mode = RESET_RUN;
2127 if (CMD_ARGC == 1)
2129 const Jim_Nvp *n;
2130 n = Jim_Nvp_name2value_simple(nvp_reset_modes, CMD_ARGV[0]);
2131 if ((n->name == NULL) || (n->value == RESET_UNKNOWN)) {
2132 return ERROR_COMMAND_SYNTAX_ERROR;
2134 reset_mode = n->value;
2137 /* reset *all* targets */
2138 return target_process_reset(CMD_CTX, reset_mode);
2142 COMMAND_HANDLER(handle_resume_command)
2144 int current = 1;
2145 if (CMD_ARGC > 1)
2146 return ERROR_COMMAND_SYNTAX_ERROR;
2148 struct target *target = get_current_target(CMD_CTX);
2149 target_handle_event(target, TARGET_EVENT_OLD_pre_resume);
2151 /* with no CMD_ARGV, resume from current pc, addr = 0,
2152 * with one arguments, addr = CMD_ARGV[0],
2153 * handle breakpoints, not debugging */
2154 uint32_t addr = 0;
2155 if (CMD_ARGC == 1)
2157 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], addr);
2158 current = 0;
2161 return target_resume(target, current, addr, 1, 0);
2164 COMMAND_HANDLER(handle_step_command)
2166 if (CMD_ARGC > 1)
2167 return ERROR_COMMAND_SYNTAX_ERROR;
2169 LOG_DEBUG("-");
2171 /* with no CMD_ARGV, step from current pc, addr = 0,
2172 * with one argument addr = CMD_ARGV[0],
2173 * handle breakpoints, debugging */
2174 uint32_t addr = 0;
2175 int current_pc = 1;
2176 if (CMD_ARGC == 1)
2178 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], addr);
2179 current_pc = 0;
2182 struct target *target = get_current_target(CMD_CTX);
2184 return target->type->step(target, current_pc, addr, 1);
2187 static void handle_md_output(struct command_context *cmd_ctx,
2188 struct target *target, uint32_t address, unsigned size,
2189 unsigned count, const uint8_t *buffer)
2191 const unsigned line_bytecnt = 32;
2192 unsigned line_modulo = line_bytecnt / size;
2194 char output[line_bytecnt * 4 + 1];
2195 unsigned output_len = 0;
2197 const char *value_fmt;
2198 switch (size) {
2199 case 4: value_fmt = "%8.8x "; break;
2200 case 2: value_fmt = "%4.2x "; break;
2201 case 1: value_fmt = "%2.2x "; break;
2202 default:
2203 LOG_ERROR("invalid memory read size: %u", size);
2204 exit(-1);
2207 for (unsigned i = 0; i < count; i++)
2209 if (i % line_modulo == 0)
2211 output_len += snprintf(output + output_len,
2212 sizeof(output) - output_len,
2213 "0x%8.8x: ",
2214 (unsigned)(address + (i*size)));
2217 uint32_t value = 0;
2218 const uint8_t *value_ptr = buffer + i * size;
2219 switch (size) {
2220 case 4: value = target_buffer_get_u32(target, value_ptr); break;
2221 case 2: value = target_buffer_get_u16(target, value_ptr); break;
2222 case 1: value = *value_ptr;
2224 output_len += snprintf(output + output_len,
2225 sizeof(output) - output_len,
2226 value_fmt, value);
2228 if ((i % line_modulo == line_modulo - 1) || (i == count - 1))
2230 command_print(cmd_ctx, "%s", output);
2231 output_len = 0;
2236 COMMAND_HANDLER(handle_md_command)
2238 if (CMD_ARGC < 1)
2239 return ERROR_COMMAND_SYNTAX_ERROR;
2241 unsigned size = 0;
2242 switch (CMD_NAME[2]) {
2243 case 'w': size = 4; break;
2244 case 'h': size = 2; break;
2245 case 'b': size = 1; break;
2246 default: return ERROR_COMMAND_SYNTAX_ERROR;
2249 bool physical=strcmp(CMD_ARGV[0], "phys")==0;
2250 int (*fn)(struct target *target,
2251 uint32_t address, uint32_t size, uint32_t count, uint8_t *buffer);
2252 if (physical)
2254 CMD_ARGC--;
2255 CMD_ARGV++;
2256 fn=target_read_phys_memory;
2257 } else
2259 fn=target_read_memory;
2261 if ((CMD_ARGC < 1) || (CMD_ARGC > 2))
2263 return ERROR_COMMAND_SYNTAX_ERROR;
2266 uint32_t address;
2267 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], address);
2269 unsigned count = 1;
2270 if (CMD_ARGC == 2)
2271 COMMAND_PARSE_NUMBER(uint, CMD_ARGV[1], count);
2273 uint8_t *buffer = calloc(count, size);
2275 struct target *target = get_current_target(CMD_CTX);
2276 int retval = fn(target, address, size, count, buffer);
2277 if (ERROR_OK == retval)
2278 handle_md_output(CMD_CTX, target, address, size, count, buffer);
2280 free(buffer);
2282 return retval;
2285 COMMAND_HANDLER(handle_mw_command)
2287 if (CMD_ARGC < 2)
2289 return ERROR_COMMAND_SYNTAX_ERROR;
2291 bool physical=strcmp(CMD_ARGV[0], "phys")==0;
2292 int (*fn)(struct target *target,
2293 uint32_t address, uint32_t size, uint32_t count, uint8_t *buffer);
2294 if (physical)
2296 CMD_ARGC--;
2297 CMD_ARGV++;
2298 fn=target_write_phys_memory;
2299 } else
2301 fn=target_write_memory;
2303 if ((CMD_ARGC < 2) || (CMD_ARGC > 3))
2304 return ERROR_COMMAND_SYNTAX_ERROR;
2306 uint32_t address;
2307 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], address);
2309 uint32_t value;
2310 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], value);
2312 unsigned count = 1;
2313 if (CMD_ARGC == 3)
2314 COMMAND_PARSE_NUMBER(uint, CMD_ARGV[2], count);
2316 struct target *target = get_current_target(CMD_CTX);
2317 unsigned wordsize;
2318 uint8_t value_buf[4];
2319 switch (CMD_NAME[2])
2321 case 'w':
2322 wordsize = 4;
2323 target_buffer_set_u32(target, value_buf, value);
2324 break;
2325 case 'h':
2326 wordsize = 2;
2327 target_buffer_set_u16(target, value_buf, value);
2328 break;
2329 case 'b':
2330 wordsize = 1;
2331 value_buf[0] = value;
2332 break;
2333 default:
2334 return ERROR_COMMAND_SYNTAX_ERROR;
2336 for (unsigned i = 0; i < count; i++)
2338 int retval = fn(target,
2339 address + i * wordsize, wordsize, 1, value_buf);
2340 if (ERROR_OK != retval)
2341 return retval;
2342 keep_alive();
2345 return ERROR_OK;
2349 static COMMAND_HELPER(parse_load_image_command_CMD_ARGV, struct image *image,
2350 uint32_t *min_address, uint32_t *max_address)
2352 if (CMD_ARGC < 1 || CMD_ARGC > 5)
2353 return ERROR_COMMAND_SYNTAX_ERROR;
2355 /* a base address isn't always necessary,
2356 * default to 0x0 (i.e. don't relocate) */
2357 if (CMD_ARGC >= 2)
2359 uint32_t addr;
2360 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], addr);
2361 image->base_address = addr;
2362 image->base_address_set = 1;
2364 else
2365 image->base_address_set = 0;
2367 image->start_address_set = 0;
2369 if (CMD_ARGC >= 4)
2371 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[3], *min_address);
2373 if (CMD_ARGC == 5)
2375 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[4], *max_address);
2376 // use size (given) to find max (required)
2377 *max_address += *min_address;
2380 if (*min_address > *max_address)
2381 return ERROR_COMMAND_SYNTAX_ERROR;
2383 return ERROR_OK;
2386 COMMAND_HANDLER(handle_load_image_command)
2388 uint8_t *buffer;
2389 size_t buf_cnt;
2390 uint32_t image_size;
2391 uint32_t min_address = 0;
2392 uint32_t max_address = 0xffffffff;
2393 int i;
2394 struct image image;
2396 int retval = CALL_COMMAND_HANDLER(parse_load_image_command_CMD_ARGV,
2397 &image, &min_address, &max_address);
2398 if (ERROR_OK != retval)
2399 return retval;
2401 struct target *target = get_current_target(CMD_CTX);
2403 struct duration bench;
2404 duration_start(&bench);
2406 if (image_open(&image, CMD_ARGV[0], (CMD_ARGC >= 3) ? CMD_ARGV[2] : NULL) != ERROR_OK)
2408 return ERROR_OK;
2411 image_size = 0x0;
2412 retval = ERROR_OK;
2413 for (i = 0; i < image.num_sections; i++)
2415 buffer = malloc(image.sections[i].size);
2416 if (buffer == NULL)
2418 command_print(CMD_CTX,
2419 "error allocating buffer for section (%d bytes)",
2420 (int)(image.sections[i].size));
2421 break;
2424 if ((retval = image_read_section(&image, i, 0x0, image.sections[i].size, buffer, &buf_cnt)) != ERROR_OK)
2426 free(buffer);
2427 break;
2430 uint32_t offset = 0;
2431 uint32_t length = buf_cnt;
2433 /* DANGER!!! beware of unsigned comparision here!!! */
2435 if ((image.sections[i].base_address + buf_cnt >= min_address)&&
2436 (image.sections[i].base_address < max_address))
2438 if (image.sections[i].base_address < min_address)
2440 /* clip addresses below */
2441 offset += min_address-image.sections[i].base_address;
2442 length -= offset;
2445 if (image.sections[i].base_address + buf_cnt > max_address)
2447 length -= (image.sections[i].base_address + buf_cnt)-max_address;
2450 if ((retval = target_write_buffer(target, image.sections[i].base_address + offset, length, buffer + offset)) != ERROR_OK)
2452 free(buffer);
2453 break;
2455 image_size += length;
2456 command_print(CMD_CTX, "%u bytes written at address 0x%8.8" PRIx32 "",
2457 (unsigned int)length,
2458 image.sections[i].base_address + offset);
2461 free(buffer);
2464 if ((ERROR_OK == retval) && (duration_measure(&bench) == ERROR_OK))
2466 command_print(CMD_CTX, "downloaded %" PRIu32 " bytes "
2467 "in %fs (%0.3f kb/s)", image_size,
2468 duration_elapsed(&bench), duration_kbps(&bench, image_size));
2471 image_close(&image);
2473 return retval;
2477 COMMAND_HANDLER(handle_dump_image_command)
2479 struct fileio fileio;
2481 uint8_t buffer[560];
2482 int retvaltemp;
2485 struct target *target = get_current_target(CMD_CTX);
2487 if (CMD_ARGC != 3)
2489 command_print(CMD_CTX, "usage: dump_image <filename> <address> <size>");
2490 return ERROR_OK;
2493 uint32_t address;
2494 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], address);
2495 uint32_t size;
2496 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[2], size);
2498 if (fileio_open(&fileio, CMD_ARGV[0], FILEIO_WRITE, FILEIO_BINARY) != ERROR_OK)
2500 return ERROR_OK;
2503 struct duration bench;
2504 duration_start(&bench);
2506 int retval = ERROR_OK;
2507 while (size > 0)
2509 size_t size_written;
2510 uint32_t this_run_size = (size > 560) ? 560 : size;
2511 retval = target_read_buffer(target, address, this_run_size, buffer);
2512 if (retval != ERROR_OK)
2514 break;
2517 retval = fileio_write(&fileio, this_run_size, buffer, &size_written);
2518 if (retval != ERROR_OK)
2520 break;
2523 size -= this_run_size;
2524 address += this_run_size;
2527 if ((retvaltemp = fileio_close(&fileio)) != ERROR_OK)
2528 return retvaltemp;
2530 if ((ERROR_OK == retval) && (duration_measure(&bench) == ERROR_OK))
2532 command_print(CMD_CTX,
2533 "dumped %zu bytes in %fs (%0.3f kb/s)", fileio.size,
2534 duration_elapsed(&bench), duration_kbps(&bench, fileio.size));
2537 return retval;
2540 static COMMAND_HELPER(handle_verify_image_command_internal, int verify)
2542 uint8_t *buffer;
2543 size_t buf_cnt;
2544 uint32_t image_size;
2545 int i;
2546 int retval;
2547 uint32_t checksum = 0;
2548 uint32_t mem_checksum = 0;
2550 struct image image;
2552 struct target *target = get_current_target(CMD_CTX);
2554 if (CMD_ARGC < 1)
2556 return ERROR_COMMAND_SYNTAX_ERROR;
2559 if (!target)
2561 LOG_ERROR("no target selected");
2562 return ERROR_FAIL;
2565 struct duration bench;
2566 duration_start(&bench);
2568 if (CMD_ARGC >= 2)
2570 uint32_t addr;
2571 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], addr);
2572 image.base_address = addr;
2573 image.base_address_set = 1;
2575 else
2577 image.base_address_set = 0;
2578 image.base_address = 0x0;
2581 image.start_address_set = 0;
2583 if ((retval = image_open(&image, CMD_ARGV[0], (CMD_ARGC == 3) ? CMD_ARGV[2] : NULL)) != ERROR_OK)
2585 return retval;
2588 image_size = 0x0;
2589 retval = ERROR_OK;
2590 for (i = 0; i < image.num_sections; i++)
2592 buffer = malloc(image.sections[i].size);
2593 if (buffer == NULL)
2595 command_print(CMD_CTX,
2596 "error allocating buffer for section (%d bytes)",
2597 (int)(image.sections[i].size));
2598 break;
2600 if ((retval = image_read_section(&image, i, 0x0, image.sections[i].size, buffer, &buf_cnt)) != ERROR_OK)
2602 free(buffer);
2603 break;
2606 if (verify)
2608 /* calculate checksum of image */
2609 image_calculate_checksum(buffer, buf_cnt, &checksum);
2611 retval = target_checksum_memory(target, image.sections[i].base_address, buf_cnt, &mem_checksum);
2612 if (retval != ERROR_OK)
2614 free(buffer);
2615 break;
2618 if (checksum != mem_checksum)
2620 /* failed crc checksum, fall back to a binary compare */
2621 uint8_t *data;
2623 command_print(CMD_CTX, "checksum mismatch - attempting binary compare");
2625 data = (uint8_t*)malloc(buf_cnt);
2627 /* Can we use 32bit word accesses? */
2628 int size = 1;
2629 int count = buf_cnt;
2630 if ((count % 4) == 0)
2632 size *= 4;
2633 count /= 4;
2635 retval = target_read_memory(target, image.sections[i].base_address, size, count, data);
2636 if (retval == ERROR_OK)
2638 uint32_t t;
2639 for (t = 0; t < buf_cnt; t++)
2641 if (data[t] != buffer[t])
2643 command_print(CMD_CTX,
2644 "Verify operation failed address 0x%08x. Was 0x%02x instead of 0x%02x\n",
2645 (unsigned)(t + image.sections[i].base_address),
2646 data[t],
2647 buffer[t]);
2648 free(data);
2649 free(buffer);
2650 retval = ERROR_FAIL;
2651 goto done;
2653 if ((t%16384) == 0)
2655 keep_alive();
2660 free(data);
2662 } else
2664 command_print(CMD_CTX, "address 0x%08" PRIx32 " length 0x%08zx",
2665 image.sections[i].base_address,
2666 buf_cnt);
2669 free(buffer);
2670 image_size += buf_cnt;
2672 done:
2673 if ((ERROR_OK == retval) && (duration_measure(&bench) == ERROR_OK))
2675 command_print(CMD_CTX, "verified %" PRIu32 " bytes "
2676 "in %fs (%0.3f kb/s)", image_size,
2677 duration_elapsed(&bench), duration_kbps(&bench, image_size));
2680 image_close(&image);
2682 return retval;
2685 COMMAND_HANDLER(handle_verify_image_command)
2687 return CALL_COMMAND_HANDLER(handle_verify_image_command_internal, 1);
2690 COMMAND_HANDLER(handle_test_image_command)
2692 return CALL_COMMAND_HANDLER(handle_verify_image_command_internal, 0);
2695 static int handle_bp_command_list(struct command_context *cmd_ctx)
2697 struct target *target = get_current_target(cmd_ctx);
2698 struct breakpoint *breakpoint = target->breakpoints;
2699 while (breakpoint)
2701 if (breakpoint->type == BKPT_SOFT)
2703 char* buf = buf_to_str(breakpoint->orig_instr,
2704 breakpoint->length, 16);
2705 command_print(cmd_ctx, "0x%8.8" PRIx32 ", 0x%x, %i, 0x%s",
2706 breakpoint->address,
2707 breakpoint->length,
2708 breakpoint->set, buf);
2709 free(buf);
2711 else
2713 command_print(cmd_ctx, "0x%8.8" PRIx32 ", 0x%x, %i",
2714 breakpoint->address,
2715 breakpoint->length, breakpoint->set);
2718 breakpoint = breakpoint->next;
2720 return ERROR_OK;
2723 static int handle_bp_command_set(struct command_context *cmd_ctx,
2724 uint32_t addr, uint32_t length, int hw)
2726 struct target *target = get_current_target(cmd_ctx);
2727 int retval = breakpoint_add(target, addr, length, hw);
2728 if (ERROR_OK == retval)
2729 command_print(cmd_ctx, "breakpoint set at 0x%8.8" PRIx32 "", addr);
2730 else
2731 LOG_ERROR("Failure setting breakpoint");
2732 return retval;
2735 COMMAND_HANDLER(handle_bp_command)
2737 if (CMD_ARGC == 0)
2738 return handle_bp_command_list(CMD_CTX);
2740 if (CMD_ARGC < 2 || CMD_ARGC > 3)
2742 command_print(CMD_CTX, "usage: bp <address> <length> ['hw']");
2743 return ERROR_COMMAND_SYNTAX_ERROR;
2746 uint32_t addr;
2747 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], addr);
2748 uint32_t length;
2749 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], length);
2751 int hw = BKPT_SOFT;
2752 if (CMD_ARGC == 3)
2754 if (strcmp(CMD_ARGV[2], "hw") == 0)
2755 hw = BKPT_HARD;
2756 else
2757 return ERROR_COMMAND_SYNTAX_ERROR;
2760 return handle_bp_command_set(CMD_CTX, addr, length, hw);
2763 COMMAND_HANDLER(handle_rbp_command)
2765 if (CMD_ARGC != 1)
2766 return ERROR_COMMAND_SYNTAX_ERROR;
2768 uint32_t addr;
2769 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], addr);
2771 struct target *target = get_current_target(CMD_CTX);
2772 breakpoint_remove(target, addr);
2774 return ERROR_OK;
2777 COMMAND_HANDLER(handle_wp_command)
2779 struct target *target = get_current_target(CMD_CTX);
2781 if (CMD_ARGC == 0)
2783 struct watchpoint *watchpoint = target->watchpoints;
2785 while (watchpoint)
2787 command_print(CMD_CTX, "address: 0x%8.8" PRIx32
2788 ", len: 0x%8.8" PRIx32
2789 ", r/w/a: %i, value: 0x%8.8" PRIx32
2790 ", mask: 0x%8.8" PRIx32,
2791 watchpoint->address,
2792 watchpoint->length,
2793 (int)watchpoint->rw,
2794 watchpoint->value,
2795 watchpoint->mask);
2796 watchpoint = watchpoint->next;
2798 return ERROR_OK;
2801 enum watchpoint_rw type = WPT_ACCESS;
2802 uint32_t addr = 0;
2803 uint32_t length = 0;
2804 uint32_t data_value = 0x0;
2805 uint32_t data_mask = 0xffffffff;
2807 switch (CMD_ARGC)
2809 case 5:
2810 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[4], data_mask);
2811 // fall through
2812 case 4:
2813 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[3], data_value);
2814 // fall through
2815 case 3:
2816 switch (CMD_ARGV[2][0])
2818 case 'r':
2819 type = WPT_READ;
2820 break;
2821 case 'w':
2822 type = WPT_WRITE;
2823 break;
2824 case 'a':
2825 type = WPT_ACCESS;
2826 break;
2827 default:
2828 LOG_ERROR("invalid watchpoint mode ('%c')", CMD_ARGV[2][0]);
2829 return ERROR_COMMAND_SYNTAX_ERROR;
2831 // fall through
2832 case 2:
2833 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], length);
2834 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], addr);
2835 break;
2837 default:
2838 command_print(CMD_CTX, "usage: wp [address length "
2839 "[(r|w|a) [value [mask]]]]");
2840 return ERROR_COMMAND_SYNTAX_ERROR;
2843 int retval = watchpoint_add(target, addr, length, type,
2844 data_value, data_mask);
2845 if (ERROR_OK != retval)
2846 LOG_ERROR("Failure setting watchpoints");
2848 return retval;
2851 COMMAND_HANDLER(handle_rwp_command)
2853 if (CMD_ARGC != 1)
2854 return ERROR_COMMAND_SYNTAX_ERROR;
2856 uint32_t addr;
2857 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], addr);
2859 struct target *target = get_current_target(CMD_CTX);
2860 watchpoint_remove(target, addr);
2862 return ERROR_OK;
2867 * Translate a virtual address to a physical address.
2869 * The low-level target implementation must have logged a detailed error
2870 * which is forwarded to telnet/GDB session.
2872 COMMAND_HANDLER(handle_virt2phys_command)
2874 if (CMD_ARGC != 1)
2875 return ERROR_COMMAND_SYNTAX_ERROR;
2877 uint32_t va;
2878 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], va);
2879 uint32_t pa;
2881 struct target *target = get_current_target(CMD_CTX);
2882 int retval = target->type->virt2phys(target, va, &pa);
2883 if (retval == ERROR_OK)
2884 command_print(CMD_CTX, "Physical address 0x%08" PRIx32 "", pa);
2886 return retval;
2889 static void writeData(FILE *f, const void *data, size_t len)
2891 size_t written = fwrite(data, 1, len, f);
2892 if (written != len)
2893 LOG_ERROR("failed to write %zu bytes: %s", len, strerror(errno));
2896 static void writeLong(FILE *f, int l)
2898 int i;
2899 for (i = 0; i < 4; i++)
2901 char c = (l >> (i*8))&0xff;
2902 writeData(f, &c, 1);
2907 static void writeString(FILE *f, char *s)
2909 writeData(f, s, strlen(s));
2912 /* Dump a gmon.out histogram file. */
2913 static void writeGmon(uint32_t *samples, uint32_t sampleNum, const char *filename)
2915 uint32_t i;
2916 FILE *f = fopen(filename, "w");
2917 if (f == NULL)
2918 return;
2919 writeString(f, "gmon");
2920 writeLong(f, 0x00000001); /* Version */
2921 writeLong(f, 0); /* padding */
2922 writeLong(f, 0); /* padding */
2923 writeLong(f, 0); /* padding */
2925 uint8_t zero = 0; /* GMON_TAG_TIME_HIST */
2926 writeData(f, &zero, 1);
2928 /* figure out bucket size */
2929 uint32_t min = samples[0];
2930 uint32_t max = samples[0];
2931 for (i = 0; i < sampleNum; i++)
2933 if (min > samples[i])
2935 min = samples[i];
2937 if (max < samples[i])
2939 max = samples[i];
2943 int addressSpace = (max-min + 1);
2945 static const uint32_t maxBuckets = 256 * 1024; /* maximum buckets. */
2946 uint32_t length = addressSpace;
2947 if (length > maxBuckets)
2949 length = maxBuckets;
2951 int *buckets = malloc(sizeof(int)*length);
2952 if (buckets == NULL)
2954 fclose(f);
2955 return;
2957 memset(buckets, 0, sizeof(int)*length);
2958 for (i = 0; i < sampleNum;i++)
2960 uint32_t address = samples[i];
2961 long long a = address-min;
2962 long long b = length-1;
2963 long long c = addressSpace-1;
2964 int index = (a*b)/c; /* danger!!!! int32 overflows */
2965 buckets[index]++;
2968 /* append binary memory gmon.out &profile_hist_hdr ((char*)&profile_hist_hdr + sizeof(struct gmon_hist_hdr)) */
2969 writeLong(f, min); /* low_pc */
2970 writeLong(f, max); /* high_pc */
2971 writeLong(f, length); /* # of samples */
2972 writeLong(f, 64000000); /* 64MHz */
2973 writeString(f, "seconds");
2974 for (i = 0; i < (15-strlen("seconds")); i++)
2975 writeData(f, &zero, 1);
2976 writeString(f, "s");
2978 /*append binary memory gmon.out profile_hist_data (profile_hist_data + profile_hist_hdr.hist_size) */
2980 char *data = malloc(2*length);
2981 if (data != NULL)
2983 for (i = 0; i < length;i++)
2985 int val;
2986 val = buckets[i];
2987 if (val > 65535)
2989 val = 65535;
2991 data[i*2]=val&0xff;
2992 data[i*2 + 1]=(val >> 8)&0xff;
2994 free(buckets);
2995 writeData(f, data, length * 2);
2996 free(data);
2997 } else
2999 free(buckets);
3002 fclose(f);
3005 /* profiling samples the CPU PC as quickly as OpenOCD is able, which will be used as a random sampling of PC */
3006 COMMAND_HANDLER(handle_profile_command)
3008 struct target *target = get_current_target(CMD_CTX);
3009 struct timeval timeout, now;
3011 gettimeofday(&timeout, NULL);
3012 if (CMD_ARGC != 2)
3014 return ERROR_COMMAND_SYNTAX_ERROR;
3016 unsigned offset;
3017 COMMAND_PARSE_NUMBER(uint, CMD_ARGV[0], offset);
3019 timeval_add_time(&timeout, offset, 0);
3021 command_print(CMD_CTX, "Starting profiling. Halting and resuming the target as often as we can...");
3023 static const int maxSample = 10000;
3024 uint32_t *samples = malloc(sizeof(uint32_t)*maxSample);
3025 if (samples == NULL)
3026 return ERROR_OK;
3028 int numSamples = 0;
3029 /* hopefully it is safe to cache! We want to stop/restart as quickly as possible. */
3030 struct reg *reg = register_get_by_name(target->reg_cache, "pc", 1);
3032 for (;;)
3034 int retval;
3035 target_poll(target);
3036 if (target->state == TARGET_HALTED)
3038 uint32_t t=*((uint32_t *)reg->value);
3039 samples[numSamples++]=t;
3040 retval = target_resume(target, 1, 0, 0, 0); /* current pc, addr = 0, do not handle breakpoints, not debugging */
3041 target_poll(target);
3042 alive_sleep(10); /* sleep 10ms, i.e. <100 samples/second. */
3043 } else if (target->state == TARGET_RUNNING)
3045 /* We want to quickly sample the PC. */
3046 if ((retval = target_halt(target)) != ERROR_OK)
3048 free(samples);
3049 return retval;
3051 } else
3053 command_print(CMD_CTX, "Target not halted or running");
3054 retval = ERROR_OK;
3055 break;
3057 if (retval != ERROR_OK)
3059 break;
3062 gettimeofday(&now, NULL);
3063 if ((numSamples >= maxSample) || ((now.tv_sec >= timeout.tv_sec) && (now.tv_usec >= timeout.tv_usec)))
3065 command_print(CMD_CTX, "Profiling completed. %d samples.", numSamples);
3066 if ((retval = target_poll(target)) != ERROR_OK)
3068 free(samples);
3069 return retval;
3071 if (target->state == TARGET_HALTED)
3073 target_resume(target, 1, 0, 0, 0); /* current pc, addr = 0, do not handle breakpoints, not debugging */
3075 if ((retval = target_poll(target)) != ERROR_OK)
3077 free(samples);
3078 return retval;
3080 writeGmon(samples, numSamples, CMD_ARGV[1]);
3081 command_print(CMD_CTX, "Wrote %s", CMD_ARGV[1]);
3082 break;
3085 free(samples);
3087 return ERROR_OK;
3090 static int new_int_array_element(Jim_Interp * interp, const char *varname, int idx, uint32_t val)
3092 char *namebuf;
3093 Jim_Obj *nameObjPtr, *valObjPtr;
3094 int result;
3096 namebuf = alloc_printf("%s(%d)", varname, idx);
3097 if (!namebuf)
3098 return JIM_ERR;
3100 nameObjPtr = Jim_NewStringObj(interp, namebuf, -1);
3101 valObjPtr = Jim_NewIntObj(interp, val);
3102 if (!nameObjPtr || !valObjPtr)
3104 free(namebuf);
3105 return JIM_ERR;
3108 Jim_IncrRefCount(nameObjPtr);
3109 Jim_IncrRefCount(valObjPtr);
3110 result = Jim_SetVariable(interp, nameObjPtr, valObjPtr);
3111 Jim_DecrRefCount(interp, nameObjPtr);
3112 Jim_DecrRefCount(interp, valObjPtr);
3113 free(namebuf);
3114 /* printf("%s(%d) <= 0%08x\n", varname, idx, val); */
3115 return result;
3118 static int jim_mem2array(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
3120 struct command_context *context;
3121 struct target *target;
3123 context = Jim_GetAssocData(interp, "context");
3124 if (context == NULL)
3126 LOG_ERROR("mem2array: no command context");
3127 return JIM_ERR;
3129 target = get_current_target(context);
3130 if (target == NULL)
3132 LOG_ERROR("mem2array: no current target");
3133 return JIM_ERR;
3136 return target_mem2array(interp, target, argc-1, argv + 1);
3139 static int target_mem2array(Jim_Interp *interp, struct target *target, int argc, Jim_Obj *const *argv)
3141 long l;
3142 uint32_t width;
3143 int len;
3144 uint32_t addr;
3145 uint32_t count;
3146 uint32_t v;
3147 const char *varname;
3148 int n, e, retval;
3149 uint32_t i;
3151 /* argv[1] = name of array to receive the data
3152 * argv[2] = desired width
3153 * argv[3] = memory address
3154 * argv[4] = count of times to read
3156 if (argc != 4) {
3157 Jim_WrongNumArgs(interp, 1, argv, "varname width addr nelems");
3158 return JIM_ERR;
3160 varname = Jim_GetString(argv[0], &len);
3161 /* given "foo" get space for worse case "foo(%d)" .. add 20 */
3163 e = Jim_GetLong(interp, argv[1], &l);
3164 width = l;
3165 if (e != JIM_OK) {
3166 return e;
3169 e = Jim_GetLong(interp, argv[2], &l);
3170 addr = l;
3171 if (e != JIM_OK) {
3172 return e;
3174 e = Jim_GetLong(interp, argv[3], &l);
3175 len = l;
3176 if (e != JIM_OK) {
3177 return e;
3179 switch (width) {
3180 case 8:
3181 width = 1;
3182 break;
3183 case 16:
3184 width = 2;
3185 break;
3186 case 32:
3187 width = 4;
3188 break;
3189 default:
3190 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3191 Jim_AppendStrings(interp, Jim_GetResult(interp), "Invalid width param, must be 8/16/32", NULL);
3192 return JIM_ERR;
3194 if (len == 0) {
3195 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3196 Jim_AppendStrings(interp, Jim_GetResult(interp), "mem2array: zero width read?", NULL);
3197 return JIM_ERR;
3199 if ((addr + (len * width)) < addr) {
3200 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3201 Jim_AppendStrings(interp, Jim_GetResult(interp), "mem2array: addr + len - wraps to zero?", NULL);
3202 return JIM_ERR;
3204 /* absurd transfer size? */
3205 if (len > 65536) {
3206 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3207 Jim_AppendStrings(interp, Jim_GetResult(interp), "mem2array: absurd > 64K item request", NULL);
3208 return JIM_ERR;
3211 if ((width == 1) ||
3212 ((width == 2) && ((addr & 1) == 0)) ||
3213 ((width == 4) && ((addr & 3) == 0))) {
3214 /* all is well */
3215 } else {
3216 char buf[100];
3217 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3218 sprintf(buf, "mem2array address: 0x%08" PRIx32 " is not aligned for %" PRId32 " byte reads",
3219 addr,
3220 width);
3221 Jim_AppendStrings(interp, Jim_GetResult(interp), buf , NULL);
3222 return JIM_ERR;
3225 /* Transfer loop */
3227 /* index counter */
3228 n = 0;
3230 size_t buffersize = 4096;
3231 uint8_t *buffer = malloc(buffersize);
3232 if (buffer == NULL)
3233 return JIM_ERR;
3235 /* assume ok */
3236 e = JIM_OK;
3237 while (len) {
3238 /* Slurp... in buffer size chunks */
3240 count = len; /* in objects.. */
3241 if (count > (buffersize/width)) {
3242 count = (buffersize/width);
3245 retval = target_read_memory(target, addr, width, count, buffer);
3246 if (retval != ERROR_OK) {
3247 /* BOO !*/
3248 LOG_ERROR("mem2array: Read @ 0x%08x, w=%d, cnt=%d, failed",
3249 (unsigned int)addr,
3250 (int)width,
3251 (int)count);
3252 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3253 Jim_AppendStrings(interp, Jim_GetResult(interp), "mem2array: cannot read memory", NULL);
3254 e = JIM_ERR;
3255 len = 0;
3256 } else {
3257 v = 0; /* shut up gcc */
3258 for (i = 0 ;i < count ;i++, n++) {
3259 switch (width) {
3260 case 4:
3261 v = target_buffer_get_u32(target, &buffer[i*width]);
3262 break;
3263 case 2:
3264 v = target_buffer_get_u16(target, &buffer[i*width]);
3265 break;
3266 case 1:
3267 v = buffer[i] & 0x0ff;
3268 break;
3270 new_int_array_element(interp, varname, n, v);
3272 len -= count;
3276 free(buffer);
3278 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3280 return JIM_OK;
3283 static int get_int_array_element(Jim_Interp * interp, const char *varname, int idx, uint32_t *val)
3285 char *namebuf;
3286 Jim_Obj *nameObjPtr, *valObjPtr;
3287 int result;
3288 long l;
3290 namebuf = alloc_printf("%s(%d)", varname, idx);
3291 if (!namebuf)
3292 return JIM_ERR;
3294 nameObjPtr = Jim_NewStringObj(interp, namebuf, -1);
3295 if (!nameObjPtr)
3297 free(namebuf);
3298 return JIM_ERR;
3301 Jim_IncrRefCount(nameObjPtr);
3302 valObjPtr = Jim_GetVariable(interp, nameObjPtr, JIM_ERRMSG);
3303 Jim_DecrRefCount(interp, nameObjPtr);
3304 free(namebuf);
3305 if (valObjPtr == NULL)
3306 return JIM_ERR;
3308 result = Jim_GetLong(interp, valObjPtr, &l);
3309 /* printf("%s(%d) => 0%08x\n", varname, idx, val); */
3310 *val = l;
3311 return result;
3314 static int jim_array2mem(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
3316 struct command_context *context;
3317 struct target *target;
3319 context = Jim_GetAssocData(interp, "context");
3320 if (context == NULL) {
3321 LOG_ERROR("array2mem: no command context");
3322 return JIM_ERR;
3324 target = get_current_target(context);
3325 if (target == NULL) {
3326 LOG_ERROR("array2mem: no current target");
3327 return JIM_ERR;
3330 return target_array2mem(interp,target, argc-1, argv + 1);
3332 static int target_array2mem(Jim_Interp *interp, struct target *target, int argc, Jim_Obj *const *argv)
3334 long l;
3335 uint32_t width;
3336 int len;
3337 uint32_t addr;
3338 uint32_t count;
3339 uint32_t v;
3340 const char *varname;
3341 int n, e, retval;
3342 uint32_t i;
3344 /* argv[1] = name of array to get the data
3345 * argv[2] = desired width
3346 * argv[3] = memory address
3347 * argv[4] = count to write
3349 if (argc != 4) {
3350 Jim_WrongNumArgs(interp, 1, argv, "varname width addr nelems");
3351 return JIM_ERR;
3353 varname = Jim_GetString(argv[0], &len);
3354 /* given "foo" get space for worse case "foo(%d)" .. add 20 */
3356 e = Jim_GetLong(interp, argv[1], &l);
3357 width = l;
3358 if (e != JIM_OK) {
3359 return e;
3362 e = Jim_GetLong(interp, argv[2], &l);
3363 addr = l;
3364 if (e != JIM_OK) {
3365 return e;
3367 e = Jim_GetLong(interp, argv[3], &l);
3368 len = l;
3369 if (e != JIM_OK) {
3370 return e;
3372 switch (width) {
3373 case 8:
3374 width = 1;
3375 break;
3376 case 16:
3377 width = 2;
3378 break;
3379 case 32:
3380 width = 4;
3381 break;
3382 default:
3383 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3384 Jim_AppendStrings(interp, Jim_GetResult(interp), "Invalid width param, must be 8/16/32", NULL);
3385 return JIM_ERR;
3387 if (len == 0) {
3388 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3389 Jim_AppendStrings(interp, Jim_GetResult(interp), "array2mem: zero width read?", NULL);
3390 return JIM_ERR;
3392 if ((addr + (len * width)) < addr) {
3393 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3394 Jim_AppendStrings(interp, Jim_GetResult(interp), "array2mem: addr + len - wraps to zero?", NULL);
3395 return JIM_ERR;
3397 /* absurd transfer size? */
3398 if (len > 65536) {
3399 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3400 Jim_AppendStrings(interp, Jim_GetResult(interp), "array2mem: absurd > 64K item request", NULL);
3401 return JIM_ERR;
3404 if ((width == 1) ||
3405 ((width == 2) && ((addr & 1) == 0)) ||
3406 ((width == 4) && ((addr & 3) == 0))) {
3407 /* all is well */
3408 } else {
3409 char buf[100];
3410 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3411 sprintf(buf, "array2mem address: 0x%08x is not aligned for %d byte reads",
3412 (unsigned int)addr,
3413 (int)width);
3414 Jim_AppendStrings(interp, Jim_GetResult(interp), buf , NULL);
3415 return JIM_ERR;
3418 /* Transfer loop */
3420 /* index counter */
3421 n = 0;
3422 /* assume ok */
3423 e = JIM_OK;
3425 size_t buffersize = 4096;
3426 uint8_t *buffer = malloc(buffersize);
3427 if (buffer == NULL)
3428 return JIM_ERR;
3430 while (len) {
3431 /* Slurp... in buffer size chunks */
3433 count = len; /* in objects.. */
3434 if (count > (buffersize/width)) {
3435 count = (buffersize/width);
3438 v = 0; /* shut up gcc */
3439 for (i = 0 ;i < count ;i++, n++) {
3440 get_int_array_element(interp, varname, n, &v);
3441 switch (width) {
3442 case 4:
3443 target_buffer_set_u32(target, &buffer[i*width], v);
3444 break;
3445 case 2:
3446 target_buffer_set_u16(target, &buffer[i*width], v);
3447 break;
3448 case 1:
3449 buffer[i] = v & 0x0ff;
3450 break;
3453 len -= count;
3455 retval = target_write_memory(target, addr, width, count, buffer);
3456 if (retval != ERROR_OK) {
3457 /* BOO !*/
3458 LOG_ERROR("array2mem: Write @ 0x%08x, w=%d, cnt=%d, failed",
3459 (unsigned int)addr,
3460 (int)width,
3461 (int)count);
3462 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3463 Jim_AppendStrings(interp, Jim_GetResult(interp), "array2mem: cannot read memory", NULL);
3464 e = JIM_ERR;
3465 len = 0;
3469 free(buffer);
3471 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3473 return JIM_OK;
3476 void target_all_handle_event(enum target_event e)
3478 struct target *target;
3480 LOG_DEBUG("**all*targets: event: %d, %s",
3481 (int)e,
3482 Jim_Nvp_value2name_simple(nvp_target_event, e)->name);
3484 target = all_targets;
3485 while (target) {
3486 target_handle_event(target, e);
3487 target = target->next;
3492 /* FIX? should we propagate errors here rather than printing them
3493 * and continuing?
3495 void target_handle_event(struct target *target, enum target_event e)
3497 struct target_event_action *teap;
3499 for (teap = target->event_action; teap != NULL; teap = teap->next) {
3500 if (teap->event == e) {
3501 LOG_DEBUG("target: (%d) %s (%s) event: %d (%s) action: %s",
3502 target->target_number,
3503 target->cmd_name,
3504 target_get_name(target),
3506 Jim_Nvp_value2name_simple(nvp_target_event, e)->name,
3507 Jim_GetString(teap->body, NULL));
3508 if (Jim_EvalObj(interp, teap->body) != JIM_OK)
3510 Jim_PrintErrorMessage(interp);
3516 enum target_cfg_param {
3517 TCFG_TYPE,
3518 TCFG_EVENT,
3519 TCFG_WORK_AREA_VIRT,
3520 TCFG_WORK_AREA_PHYS,
3521 TCFG_WORK_AREA_SIZE,
3522 TCFG_WORK_AREA_BACKUP,
3523 TCFG_ENDIAN,
3524 TCFG_VARIANT,
3525 TCFG_CHAIN_POSITION,
3528 static Jim_Nvp nvp_config_opts[] = {
3529 { .name = "-type", .value = TCFG_TYPE },
3530 { .name = "-event", .value = TCFG_EVENT },
3531 { .name = "-work-area-virt", .value = TCFG_WORK_AREA_VIRT },
3532 { .name = "-work-area-phys", .value = TCFG_WORK_AREA_PHYS },
3533 { .name = "-work-area-size", .value = TCFG_WORK_AREA_SIZE },
3534 { .name = "-work-area-backup", .value = TCFG_WORK_AREA_BACKUP },
3535 { .name = "-endian" , .value = TCFG_ENDIAN },
3536 { .name = "-variant", .value = TCFG_VARIANT },
3537 { .name = "-chain-position", .value = TCFG_CHAIN_POSITION },
3539 { .name = NULL, .value = -1 }
3542 static int target_configure(Jim_GetOptInfo *goi, struct target *target)
3544 Jim_Nvp *n;
3545 Jim_Obj *o;
3546 jim_wide w;
3547 char *cp;
3548 int e;
3550 /* parse config or cget options ... */
3551 while (goi->argc > 0) {
3552 Jim_SetEmptyResult(goi->interp);
3553 /* Jim_GetOpt_Debug(goi); */
3555 if (target->type->target_jim_configure) {
3556 /* target defines a configure function */
3557 /* target gets first dibs on parameters */
3558 e = (*(target->type->target_jim_configure))(target, goi);
3559 if (e == JIM_OK) {
3560 /* more? */
3561 continue;
3563 if (e == JIM_ERR) {
3564 /* An error */
3565 return e;
3567 /* otherwise we 'continue' below */
3569 e = Jim_GetOpt_Nvp(goi, nvp_config_opts, &n);
3570 if (e != JIM_OK) {
3571 Jim_GetOpt_NvpUnknown(goi, nvp_config_opts, 0);
3572 return e;
3574 switch (n->value) {
3575 case TCFG_TYPE:
3576 /* not setable */
3577 if (goi->isconfigure) {
3578 Jim_SetResult_sprintf(goi->interp, "not setable: %s", n->name);
3579 return JIM_ERR;
3580 } else {
3581 no_params:
3582 if (goi->argc != 0) {
3583 Jim_WrongNumArgs(goi->interp, goi->argc, goi->argv, "NO PARAMS");
3584 return JIM_ERR;
3587 Jim_SetResultString(goi->interp, target_get_name(target), -1);
3588 /* loop for more */
3589 break;
3590 case TCFG_EVENT:
3591 if (goi->argc == 0) {
3592 Jim_WrongNumArgs(goi->interp, goi->argc, goi->argv, "-event ?event-name? ...");
3593 return JIM_ERR;
3596 e = Jim_GetOpt_Nvp(goi, nvp_target_event, &n);
3597 if (e != JIM_OK) {
3598 Jim_GetOpt_NvpUnknown(goi, nvp_target_event, 1);
3599 return e;
3602 if (goi->isconfigure) {
3603 if (goi->argc != 1) {
3604 Jim_WrongNumArgs(goi->interp, goi->argc, goi->argv, "-event ?event-name? ?EVENT-BODY?");
3605 return JIM_ERR;
3607 } else {
3608 if (goi->argc != 0) {
3609 Jim_WrongNumArgs(goi->interp, goi->argc, goi->argv, "-event ?event-name?");
3610 return JIM_ERR;
3615 struct target_event_action *teap;
3617 teap = target->event_action;
3618 /* replace existing? */
3619 while (teap) {
3620 if (teap->event == (enum target_event)n->value) {
3621 break;
3623 teap = teap->next;
3626 if (goi->isconfigure) {
3627 bool replace = true;
3628 if (teap == NULL) {
3629 /* create new */
3630 teap = calloc(1, sizeof(*teap));
3631 replace = false;
3633 teap->event = n->value;
3634 Jim_GetOpt_Obj(goi, &o);
3635 if (teap->body) {
3636 Jim_DecrRefCount(interp, teap->body);
3638 teap->body = Jim_DuplicateObj(goi->interp, o);
3640 * FIXME:
3641 * Tcl/TK - "tk events" have a nice feature.
3642 * See the "BIND" command.
3643 * We should support that here.
3644 * You can specify %X and %Y in the event code.
3645 * The idea is: %T - target name.
3646 * The idea is: %N - target number
3647 * The idea is: %E - event name.
3649 Jim_IncrRefCount(teap->body);
3651 if (!replace)
3653 /* add to head of event list */
3654 teap->next = target->event_action;
3655 target->event_action = teap;
3657 Jim_SetEmptyResult(goi->interp);
3658 } else {
3659 /* get */
3660 if (teap == NULL) {
3661 Jim_SetEmptyResult(goi->interp);
3662 } else {
3663 Jim_SetResult(goi->interp, Jim_DuplicateObj(goi->interp, teap->body));
3667 /* loop for more */
3668 break;
3670 case TCFG_WORK_AREA_VIRT:
3671 if (goi->isconfigure) {
3672 target_free_all_working_areas(target);
3673 e = Jim_GetOpt_Wide(goi, &w);
3674 if (e != JIM_OK) {
3675 return e;
3677 target->working_area_virt = w;
3678 target->working_area_virt_spec = true;
3679 } else {
3680 if (goi->argc != 0) {
3681 goto no_params;
3684 Jim_SetResult(interp, Jim_NewIntObj(goi->interp, target->working_area_virt));
3685 /* loop for more */
3686 break;
3688 case TCFG_WORK_AREA_PHYS:
3689 if (goi->isconfigure) {
3690 target_free_all_working_areas(target);
3691 e = Jim_GetOpt_Wide(goi, &w);
3692 if (e != JIM_OK) {
3693 return e;
3695 target->working_area_phys = w;
3696 target->working_area_phys_spec = true;
3697 } else {
3698 if (goi->argc != 0) {
3699 goto no_params;
3702 Jim_SetResult(interp, Jim_NewIntObj(goi->interp, target->working_area_phys));
3703 /* loop for more */
3704 break;
3706 case TCFG_WORK_AREA_SIZE:
3707 if (goi->isconfigure) {
3708 target_free_all_working_areas(target);
3709 e = Jim_GetOpt_Wide(goi, &w);
3710 if (e != JIM_OK) {
3711 return e;
3713 target->working_area_size = w;
3714 } else {
3715 if (goi->argc != 0) {
3716 goto no_params;
3719 Jim_SetResult(interp, Jim_NewIntObj(goi->interp, target->working_area_size));
3720 /* loop for more */
3721 break;
3723 case TCFG_WORK_AREA_BACKUP:
3724 if (goi->isconfigure) {
3725 target_free_all_working_areas(target);
3726 e = Jim_GetOpt_Wide(goi, &w);
3727 if (e != JIM_OK) {
3728 return e;
3730 /* make this exactly 1 or 0 */
3731 target->backup_working_area = (!!w);
3732 } else {
3733 if (goi->argc != 0) {
3734 goto no_params;
3737 Jim_SetResult(interp, Jim_NewIntObj(goi->interp, target->backup_working_area));
3738 /* loop for more e*/
3739 break;
3741 case TCFG_ENDIAN:
3742 if (goi->isconfigure) {
3743 e = Jim_GetOpt_Nvp(goi, nvp_target_endian, &n);
3744 if (e != JIM_OK) {
3745 Jim_GetOpt_NvpUnknown(goi, nvp_target_endian, 1);
3746 return e;
3748 target->endianness = n->value;
3749 } else {
3750 if (goi->argc != 0) {
3751 goto no_params;
3754 n = Jim_Nvp_value2name_simple(nvp_target_endian, target->endianness);
3755 if (n->name == NULL) {
3756 target->endianness = TARGET_LITTLE_ENDIAN;
3757 n = Jim_Nvp_value2name_simple(nvp_target_endian, target->endianness);
3759 Jim_SetResultString(goi->interp, n->name, -1);
3760 /* loop for more */
3761 break;
3763 case TCFG_VARIANT:
3764 if (goi->isconfigure) {
3765 if (goi->argc < 1) {
3766 Jim_SetResult_sprintf(goi->interp,
3767 "%s ?STRING?",
3768 n->name);
3769 return JIM_ERR;
3771 if (target->variant) {
3772 free((void *)(target->variant));
3774 e = Jim_GetOpt_String(goi, &cp, NULL);
3775 target->variant = strdup(cp);
3776 } else {
3777 if (goi->argc != 0) {
3778 goto no_params;
3781 Jim_SetResultString(goi->interp, target->variant,-1);
3782 /* loop for more */
3783 break;
3784 case TCFG_CHAIN_POSITION:
3785 if (goi->isconfigure) {
3786 Jim_Obj *o;
3787 struct jtag_tap *tap;
3788 target_free_all_working_areas(target);
3789 e = Jim_GetOpt_Obj(goi, &o);
3790 if (e != JIM_OK) {
3791 return e;
3793 tap = jtag_tap_by_jim_obj(goi->interp, o);
3794 if (tap == NULL) {
3795 return JIM_ERR;
3797 /* make this exactly 1 or 0 */
3798 target->tap = tap;
3799 } else {
3800 if (goi->argc != 0) {
3801 goto no_params;
3804 Jim_SetResultString(interp, target->tap->dotted_name, -1);
3805 /* loop for more e*/
3806 break;
3808 } /* while (goi->argc) */
3811 /* done - we return */
3812 return JIM_OK;
3815 /** this is the 'tcl' handler for the target specific command */
3816 static int tcl_target_func(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
3818 Jim_GetOptInfo goi;
3819 jim_wide a,b,c;
3820 int x,y,z;
3821 uint8_t target_buf[32];
3822 Jim_Nvp *n;
3823 struct target *target;
3824 struct command_context *cmd_ctx;
3825 int e;
3827 enum {
3828 TS_CMD_CONFIGURE,
3829 TS_CMD_CGET,
3831 TS_CMD_MWW, TS_CMD_MWH, TS_CMD_MWB,
3832 TS_CMD_MDW, TS_CMD_MDH, TS_CMD_MDB,
3833 TS_CMD_MRW, TS_CMD_MRH, TS_CMD_MRB,
3834 TS_CMD_MEM2ARRAY, TS_CMD_ARRAY2MEM,
3835 TS_CMD_EXAMINE,
3836 TS_CMD_POLL,
3837 TS_CMD_RESET,
3838 TS_CMD_HALT,
3839 TS_CMD_WAITSTATE,
3840 TS_CMD_EVENTLIST,
3841 TS_CMD_CURSTATE,
3842 TS_CMD_INVOKE_EVENT,
3845 static const Jim_Nvp target_options[] = {
3846 { .name = "configure", .value = TS_CMD_CONFIGURE },
3847 { .name = "cget", .value = TS_CMD_CGET },
3848 { .name = "mww", .value = TS_CMD_MWW },
3849 { .name = "mwh", .value = TS_CMD_MWH },
3850 { .name = "mwb", .value = TS_CMD_MWB },
3851 { .name = "mdw", .value = TS_CMD_MDW },
3852 { .name = "mdh", .value = TS_CMD_MDH },
3853 { .name = "mdb", .value = TS_CMD_MDB },
3854 { .name = "mem2array", .value = TS_CMD_MEM2ARRAY },
3855 { .name = "array2mem", .value = TS_CMD_ARRAY2MEM },
3856 { .name = "eventlist", .value = TS_CMD_EVENTLIST },
3857 { .name = "curstate", .value = TS_CMD_CURSTATE },
3859 { .name = "arp_examine", .value = TS_CMD_EXAMINE },
3860 { .name = "arp_poll", .value = TS_CMD_POLL },
3861 { .name = "arp_reset", .value = TS_CMD_RESET },
3862 { .name = "arp_halt", .value = TS_CMD_HALT },
3863 { .name = "arp_waitstate", .value = TS_CMD_WAITSTATE },
3864 { .name = "invoke-event", .value = TS_CMD_INVOKE_EVENT },
3866 { .name = NULL, .value = -1 },
3869 /* go past the "command" */
3870 Jim_GetOpt_Setup(&goi, interp, argc-1, argv + 1);
3872 target = Jim_CmdPrivData(goi.interp);
3873 cmd_ctx = Jim_GetAssocData(goi.interp, "context");
3875 /* commands here are in an NVP table */
3876 e = Jim_GetOpt_Nvp(&goi, target_options, &n);
3877 if (e != JIM_OK) {
3878 Jim_GetOpt_NvpUnknown(&goi, target_options, 0);
3879 return e;
3881 /* Assume blank result */
3882 Jim_SetEmptyResult(goi.interp);
3884 switch (n->value) {
3885 case TS_CMD_CONFIGURE:
3886 if (goi.argc < 2) {
3887 Jim_WrongNumArgs(goi.interp, goi.argc, goi.argv, "missing: -option VALUE ...");
3888 return JIM_ERR;
3890 goi.isconfigure = 1;
3891 return target_configure(&goi, target);
3892 case TS_CMD_CGET:
3893 // some things take params
3894 if (goi.argc < 1) {
3895 Jim_WrongNumArgs(goi.interp, 0, goi.argv, "missing: ?-option?");
3896 return JIM_ERR;
3898 goi.isconfigure = 0;
3899 return target_configure(&goi, target);
3900 break;
3901 case TS_CMD_MWW:
3902 case TS_CMD_MWH:
3903 case TS_CMD_MWB:
3904 /* argv[0] = cmd
3905 * argv[1] = address
3906 * argv[2] = data
3907 * argv[3] = optional count.
3910 if ((goi.argc == 2) || (goi.argc == 3)) {
3911 /* all is well */
3912 } else {
3913 mwx_error:
3914 Jim_SetResult_sprintf(goi.interp, "expected: %s ADDR DATA [COUNT]", n->name);
3915 return JIM_ERR;
3918 e = Jim_GetOpt_Wide(&goi, &a);
3919 if (e != JIM_OK) {
3920 goto mwx_error;
3923 e = Jim_GetOpt_Wide(&goi, &b);
3924 if (e != JIM_OK) {
3925 goto mwx_error;
3927 if (goi.argc == 3) {
3928 e = Jim_GetOpt_Wide(&goi, &c);
3929 if (e != JIM_OK) {
3930 goto mwx_error;
3932 } else {
3933 c = 1;
3936 switch (n->value) {
3937 case TS_CMD_MWW:
3938 target_buffer_set_u32(target, target_buf, b);
3939 b = 4;
3940 break;
3941 case TS_CMD_MWH:
3942 target_buffer_set_u16(target, target_buf, b);
3943 b = 2;
3944 break;
3945 case TS_CMD_MWB:
3946 target_buffer_set_u8(target, target_buf, b);
3947 b = 1;
3948 break;
3950 for (x = 0 ; x < c ; x++) {
3951 e = target_write_memory(target, a, b, 1, target_buf);
3952 if (e != ERROR_OK) {
3953 Jim_SetResult_sprintf(interp, "Error writing @ 0x%08x: %d\n", (int)(a), e);
3954 return JIM_ERR;
3956 /* b = width */
3957 a = a + b;
3959 return JIM_OK;
3960 break;
3962 /* display */
3963 case TS_CMD_MDW:
3964 case TS_CMD_MDH:
3965 case TS_CMD_MDB:
3966 /* argv[0] = command
3967 * argv[1] = address
3968 * argv[2] = optional count
3970 if ((goi.argc == 2) || (goi.argc == 3)) {
3971 Jim_SetResult_sprintf(goi.interp, "expected: %s ADDR [COUNT]", n->name);
3972 return JIM_ERR;
3974 e = Jim_GetOpt_Wide(&goi, &a);
3975 if (e != JIM_OK) {
3976 return JIM_ERR;
3978 if (goi.argc) {
3979 e = Jim_GetOpt_Wide(&goi, &c);
3980 if (e != JIM_OK) {
3981 return JIM_ERR;
3983 } else {
3984 c = 1;
3986 b = 1; /* shut up gcc */
3987 switch (n->value) {
3988 case TS_CMD_MDW:
3989 b = 4;
3990 break;
3991 case TS_CMD_MDH:
3992 b = 2;
3993 break;
3994 case TS_CMD_MDB:
3995 b = 1;
3996 break;
3999 /* convert to "bytes" */
4000 c = c * b;
4001 /* count is now in 'BYTES' */
4002 while (c > 0) {
4003 y = c;
4004 if (y > 16) {
4005 y = 16;
4007 e = target_read_memory(target, a, b, y / b, target_buf);
4008 if (e != ERROR_OK) {
4009 Jim_SetResult_sprintf(interp, "error reading target @ 0x%08lx", (int)(a));
4010 return JIM_ERR;
4013 Jim_fprintf(interp, interp->cookie_stdout, "0x%08x ", (int)(a));
4014 switch (b) {
4015 case 4:
4016 for (x = 0 ; (x < 16) && (x < y) ; x += 4) {
4017 z = target_buffer_get_u32(target, &(target_buf[ x * 4 ]));
4018 Jim_fprintf(interp, interp->cookie_stdout, "%08x ", (int)(z));
4020 for (; (x < 16) ; x += 4) {
4021 Jim_fprintf(interp, interp->cookie_stdout, " ");
4023 break;
4024 case 2:
4025 for (x = 0 ; (x < 16) && (x < y) ; x += 2) {
4026 z = target_buffer_get_u16(target, &(target_buf[ x * 2 ]));
4027 Jim_fprintf(interp, interp->cookie_stdout, "%04x ", (int)(z));
4029 for (; (x < 16) ; x += 2) {
4030 Jim_fprintf(interp, interp->cookie_stdout, " ");
4032 break;
4033 case 1:
4034 default:
4035 for (x = 0 ; (x < 16) && (x < y) ; x += 1) {
4036 z = target_buffer_get_u8(target, &(target_buf[ x * 4 ]));
4037 Jim_fprintf(interp, interp->cookie_stdout, "%02x ", (int)(z));
4039 for (; (x < 16) ; x += 1) {
4040 Jim_fprintf(interp, interp->cookie_stdout, " ");
4042 break;
4044 /* ascii-ify the bytes */
4045 for (x = 0 ; x < y ; x++) {
4046 if ((target_buf[x] >= 0x20) &&
4047 (target_buf[x] <= 0x7e)) {
4048 /* good */
4049 } else {
4050 /* smack it */
4051 target_buf[x] = '.';
4054 /* space pad */
4055 while (x < 16) {
4056 target_buf[x] = ' ';
4057 x++;
4059 /* terminate */
4060 target_buf[16] = 0;
4061 /* print - with a newline */
4062 Jim_fprintf(interp, interp->cookie_stdout, "%s\n", target_buf);
4063 /* NEXT... */
4064 c -= 16;
4065 a += 16;
4067 return JIM_OK;
4068 case TS_CMD_MEM2ARRAY:
4069 return target_mem2array(goi.interp, target, goi.argc, goi.argv);
4070 break;
4071 case TS_CMD_ARRAY2MEM:
4072 return target_array2mem(goi.interp, target, goi.argc, goi.argv);
4073 break;
4074 case TS_CMD_EXAMINE:
4075 if (goi.argc) {
4076 Jim_WrongNumArgs(goi.interp, 2, argv, "[no parameters]");
4077 return JIM_ERR;
4079 if (!target->tap->enabled)
4080 goto err_tap_disabled;
4081 e = target->type->examine(target);
4082 if (e != ERROR_OK) {
4083 Jim_SetResult_sprintf(interp, "examine-fails: %d", e);
4084 return JIM_ERR;
4086 return JIM_OK;
4087 case TS_CMD_POLL:
4088 if (goi.argc) {
4089 Jim_WrongNumArgs(goi.interp, 2, argv, "[no parameters]");
4090 return JIM_ERR;
4092 if (!target->tap->enabled)
4093 goto err_tap_disabled;
4094 if (!(target_was_examined(target))) {
4095 e = ERROR_TARGET_NOT_EXAMINED;
4096 } else {
4097 e = target->type->poll(target);
4099 if (e != ERROR_OK) {
4100 Jim_SetResult_sprintf(interp, "poll-fails: %d", e);
4101 return JIM_ERR;
4102 } else {
4103 return JIM_OK;
4105 break;
4106 case TS_CMD_RESET:
4107 if (goi.argc != 2) {
4108 Jim_WrongNumArgs(interp, 2, argv,
4109 "([tT]|[fF]|assert|deassert) BOOL");
4110 return JIM_ERR;
4112 e = Jim_GetOpt_Nvp(&goi, nvp_assert, &n);
4113 if (e != JIM_OK) {
4114 Jim_GetOpt_NvpUnknown(&goi, nvp_assert, 1);
4115 return e;
4117 /* the halt or not param */
4118 e = Jim_GetOpt_Wide(&goi, &a);
4119 if (e != JIM_OK) {
4120 return e;
4122 if (!target->tap->enabled)
4123 goto err_tap_disabled;
4124 if (!target->type->assert_reset
4125 || !target->type->deassert_reset) {
4126 Jim_SetResult_sprintf(interp,
4127 "No target-specific reset for %s",
4128 target->cmd_name);
4129 return JIM_ERR;
4131 /* determine if we should halt or not. */
4132 target->reset_halt = !!a;
4133 /* When this happens - all workareas are invalid. */
4134 target_free_all_working_areas_restore(target, 0);
4136 /* do the assert */
4137 if (n->value == NVP_ASSERT) {
4138 e = target->type->assert_reset(target);
4139 } else {
4140 e = target->type->deassert_reset(target);
4142 return (e == ERROR_OK) ? JIM_OK : JIM_ERR;
4143 case TS_CMD_HALT:
4144 if (goi.argc) {
4145 Jim_WrongNumArgs(goi.interp, 0, argv, "halt [no parameters]");
4146 return JIM_ERR;
4148 if (!target->tap->enabled)
4149 goto err_tap_disabled;
4150 e = target->type->halt(target);
4151 return (e == ERROR_OK) ? JIM_OK : JIM_ERR;
4152 case TS_CMD_WAITSTATE:
4153 /* params: <name> statename timeoutmsecs */
4154 if (goi.argc != 2) {
4155 Jim_SetResult_sprintf(goi.interp, "%s STATENAME TIMEOUTMSECS", n->name);
4156 return JIM_ERR;
4158 e = Jim_GetOpt_Nvp(&goi, nvp_target_state, &n);
4159 if (e != JIM_OK) {
4160 Jim_GetOpt_NvpUnknown(&goi, nvp_target_state,1);
4161 return e;
4163 e = Jim_GetOpt_Wide(&goi, &a);
4164 if (e != JIM_OK) {
4165 return e;
4167 if (!target->tap->enabled)
4168 goto err_tap_disabled;
4169 e = target_wait_state(target, n->value, a);
4170 if (e != ERROR_OK) {
4171 Jim_SetResult_sprintf(goi.interp,
4172 "target: %s wait %s fails (%d) %s",
4173 target->cmd_name,
4174 n->name,
4175 e, target_strerror_safe(e));
4176 return JIM_ERR;
4177 } else {
4178 return JIM_OK;
4180 case TS_CMD_EVENTLIST:
4181 /* List for human, Events defined for this target.
4182 * scripts/programs should use 'name cget -event NAME'
4185 struct target_event_action *teap;
4186 teap = target->event_action;
4187 command_print(cmd_ctx, "Event actions for target (%d) %s\n",
4188 target->target_number,
4189 target->cmd_name);
4190 command_print(cmd_ctx, "%-25s | Body", "Event");
4191 command_print(cmd_ctx, "------------------------- | ----------------------------------------");
4192 while (teap) {
4193 command_print(cmd_ctx,
4194 "%-25s | %s",
4195 Jim_Nvp_value2name_simple(nvp_target_event, teap->event)->name,
4196 Jim_GetString(teap->body, NULL));
4197 teap = teap->next;
4199 command_print(cmd_ctx, "***END***");
4200 return JIM_OK;
4202 case TS_CMD_CURSTATE:
4203 if (goi.argc != 0) {
4204 Jim_WrongNumArgs(goi.interp, 0, argv, "[no parameters]");
4205 return JIM_ERR;
4207 Jim_SetResultString(goi.interp,
4208 target_state_name( target ),
4209 -1);
4210 return JIM_OK;
4211 case TS_CMD_INVOKE_EVENT:
4212 if (goi.argc != 1) {
4213 Jim_SetResult_sprintf(goi.interp, "%s ?EVENTNAME?",n->name);
4214 return JIM_ERR;
4216 e = Jim_GetOpt_Nvp(&goi, nvp_target_event, &n);
4217 if (e != JIM_OK) {
4218 Jim_GetOpt_NvpUnknown(&goi, nvp_target_event, 1);
4219 return e;
4221 target_handle_event(target, n->value);
4222 return JIM_OK;
4224 return JIM_ERR;
4226 err_tap_disabled:
4227 Jim_SetResult_sprintf(interp, "[TAP is disabled]");
4228 return JIM_ERR;
4231 static int target_create(Jim_GetOptInfo *goi)
4233 Jim_Obj *new_cmd;
4234 Jim_Cmd *cmd;
4235 const char *cp;
4236 char *cp2;
4237 int e;
4238 int x;
4239 struct target *target;
4240 struct command_context *cmd_ctx;
4242 cmd_ctx = Jim_GetAssocData(goi->interp, "context");
4243 if (goi->argc < 3) {
4244 Jim_WrongNumArgs(goi->interp, 1, goi->argv, "?name? ?type? ..options...");
4245 return JIM_ERR;
4248 /* COMMAND */
4249 Jim_GetOpt_Obj(goi, &new_cmd);
4250 /* does this command exist? */
4251 cmd = Jim_GetCommand(goi->interp, new_cmd, JIM_ERRMSG);
4252 if (cmd) {
4253 cp = Jim_GetString(new_cmd, NULL);
4254 Jim_SetResult_sprintf(goi->interp, "Command/target: %s Exists", cp);
4255 return JIM_ERR;
4258 /* TYPE */
4259 e = Jim_GetOpt_String(goi, &cp2, NULL);
4260 cp = cp2;
4261 /* now does target type exist */
4262 for (x = 0 ; target_types[x] ; x++) {
4263 if (0 == strcmp(cp, target_types[x]->name)) {
4264 /* found */
4265 break;
4268 if (target_types[x] == NULL) {
4269 Jim_SetResult_sprintf(goi->interp, "Unknown target type %s, try one of ", cp);
4270 for (x = 0 ; target_types[x] ; x++) {
4271 if (target_types[x + 1]) {
4272 Jim_AppendStrings(goi->interp,
4273 Jim_GetResult(goi->interp),
4274 target_types[x]->name,
4275 ", ", NULL);
4276 } else {
4277 Jim_AppendStrings(goi->interp,
4278 Jim_GetResult(goi->interp),
4279 " or ",
4280 target_types[x]->name,NULL);
4283 return JIM_ERR;
4286 /* Create it */
4287 target = calloc(1,sizeof(struct target));
4288 /* set target number */
4289 target->target_number = new_target_number();
4291 /* allocate memory for each unique target type */
4292 target->type = (struct target_type*)calloc(1,sizeof(struct target_type));
4294 memcpy(target->type, target_types[x], sizeof(struct target_type));
4296 /* will be set by "-endian" */
4297 target->endianness = TARGET_ENDIAN_UNKNOWN;
4299 target->working_area = 0x0;
4300 target->working_area_size = 0x0;
4301 target->working_areas = NULL;
4302 target->backup_working_area = 0;
4304 target->state = TARGET_UNKNOWN;
4305 target->debug_reason = DBG_REASON_UNDEFINED;
4306 target->reg_cache = NULL;
4307 target->breakpoints = NULL;
4308 target->watchpoints = NULL;
4309 target->next = NULL;
4310 target->arch_info = NULL;
4312 target->display = 1;
4314 target->halt_issued = false;
4316 /* initialize trace information */
4317 target->trace_info = malloc(sizeof(struct trace));
4318 target->trace_info->num_trace_points = 0;
4319 target->trace_info->trace_points_size = 0;
4320 target->trace_info->trace_points = NULL;
4321 target->trace_info->trace_history_size = 0;
4322 target->trace_info->trace_history = NULL;
4323 target->trace_info->trace_history_pos = 0;
4324 target->trace_info->trace_history_overflowed = 0;
4326 target->dbgmsg = NULL;
4327 target->dbg_msg_enabled = 0;
4329 target->endianness = TARGET_ENDIAN_UNKNOWN;
4331 /* Do the rest as "configure" options */
4332 goi->isconfigure = 1;
4333 e = target_configure(goi, target);
4335 if (target->tap == NULL)
4337 Jim_SetResultString(interp, "-chain-position required when creating target", -1);
4338 e = JIM_ERR;
4341 if (e != JIM_OK) {
4342 free(target->type);
4343 free(target);
4344 return e;
4347 if (target->endianness == TARGET_ENDIAN_UNKNOWN) {
4348 /* default endian to little if not specified */
4349 target->endianness = TARGET_LITTLE_ENDIAN;
4352 /* incase variant is not set */
4353 if (!target->variant)
4354 target->variant = strdup("");
4356 /* create the target specific commands */
4357 if (target->type->register_commands) {
4358 (*(target->type->register_commands))(cmd_ctx);
4360 if (target->type->target_create) {
4361 (*(target->type->target_create))(target, goi->interp);
4364 /* append to end of list */
4366 struct target **tpp;
4367 tpp = &(all_targets);
4368 while (*tpp) {
4369 tpp = &((*tpp)->next);
4371 *tpp = target;
4374 cp = Jim_GetString(new_cmd, NULL);
4375 target->cmd_name = strdup(cp);
4377 /* now - create the new target name command */
4378 e = Jim_CreateCommand(goi->interp,
4379 /* name */
4381 tcl_target_func, /* C function */
4382 target, /* private data */
4383 NULL); /* no del proc */
4385 return e;
4388 static int jim_target(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
4390 int x,r,e;
4391 jim_wide w;
4392 struct command_context *cmd_ctx;
4393 struct target *target;
4394 Jim_GetOptInfo goi;
4395 enum tcmd {
4396 /* TG = target generic */
4397 TG_CMD_CREATE,
4398 TG_CMD_TYPES,
4399 TG_CMD_NAMES,
4400 TG_CMD_CURRENT,
4401 TG_CMD_NUMBER,
4402 TG_CMD_COUNT,
4404 const char *target_cmds[] = {
4405 "create", "types", "names", "current", "number",
4406 "count",
4407 NULL /* terminate */
4410 LOG_DEBUG("Target command params:");
4411 LOG_DEBUG("%s", Jim_Debug_ArgvString(interp, argc, argv));
4413 cmd_ctx = Jim_GetAssocData(interp, "context");
4415 Jim_GetOpt_Setup(&goi, interp, argc-1, argv + 1);
4417 if (goi.argc == 0) {
4418 Jim_WrongNumArgs(interp, 1, argv, "missing: command ...");
4419 return JIM_ERR;
4422 /* Jim_GetOpt_Debug(&goi); */
4423 r = Jim_GetOpt_Enum(&goi, target_cmds, &x);
4424 if (r != JIM_OK) {
4425 return r;
4428 switch (x) {
4429 default:
4430 Jim_Panic(goi.interp,"Why am I here?");
4431 return JIM_ERR;
4432 case TG_CMD_CURRENT:
4433 if (goi.argc != 0) {
4434 Jim_WrongNumArgs(goi.interp, 1, goi.argv, "Too many parameters");
4435 return JIM_ERR;
4437 Jim_SetResultString(goi.interp, get_current_target(cmd_ctx)->cmd_name, -1);
4438 return JIM_OK;
4439 case TG_CMD_TYPES:
4440 if (goi.argc != 0) {
4441 Jim_WrongNumArgs(goi.interp, 1, goi.argv, "Too many parameters");
4442 return JIM_ERR;
4444 Jim_SetResult(goi.interp, Jim_NewListObj(goi.interp, NULL, 0));
4445 for (x = 0 ; target_types[x] ; x++) {
4446 Jim_ListAppendElement(goi.interp,
4447 Jim_GetResult(goi.interp),
4448 Jim_NewStringObj(goi.interp, target_types[x]->name, -1));
4450 return JIM_OK;
4451 case TG_CMD_NAMES:
4452 if (goi.argc != 0) {
4453 Jim_WrongNumArgs(goi.interp, 1, goi.argv, "Too many parameters");
4454 return JIM_ERR;
4456 Jim_SetResult(goi.interp, Jim_NewListObj(goi.interp, NULL, 0));
4457 target = all_targets;
4458 while (target) {
4459 Jim_ListAppendElement(goi.interp,
4460 Jim_GetResult(goi.interp),
4461 Jim_NewStringObj(goi.interp, target->cmd_name, -1));
4462 target = target->next;
4464 return JIM_OK;
4465 case TG_CMD_CREATE:
4466 if (goi.argc < 3) {
4467 Jim_WrongNumArgs(goi.interp, goi.argc, goi.argv, "?name ... config options ...");
4468 return JIM_ERR;
4470 return target_create(&goi);
4471 break;
4472 case TG_CMD_NUMBER:
4473 /* It's OK to remove this mechanism sometime after August 2010 or so */
4474 LOG_WARNING("don't use numbers as target identifiers; use names");
4475 if (goi.argc != 1) {
4476 Jim_SetResult_sprintf(goi.interp, "expected: target number ?NUMBER?");
4477 return JIM_ERR;
4479 e = Jim_GetOpt_Wide(&goi, &w);
4480 if (e != JIM_OK) {
4481 return JIM_ERR;
4483 for (x = 0, target = all_targets; target; target = target->next, x++) {
4484 if (target->target_number == w)
4485 break;
4487 if (target == NULL) {
4488 Jim_SetResult_sprintf(goi.interp,
4489 "Target: number %d does not exist", (int)(w));
4490 return JIM_ERR;
4492 Jim_SetResultString(goi.interp, target->cmd_name, -1);
4493 return JIM_OK;
4494 case TG_CMD_COUNT:
4495 if (goi.argc != 0) {
4496 Jim_WrongNumArgs(goi.interp, 0, goi.argv, "<no parameters>");
4497 return JIM_ERR;
4499 for (x = 0, target = all_targets; target; target = target->next, x++)
4500 continue;
4501 Jim_SetResult(goi.interp, Jim_NewIntObj(goi.interp, x));
4502 return JIM_OK;
4505 return JIM_ERR;
4509 struct FastLoad
4511 uint32_t address;
4512 uint8_t *data;
4513 int length;
4517 static int fastload_num;
4518 static struct FastLoad *fastload;
4520 static void free_fastload(void)
4522 if (fastload != NULL)
4524 int i;
4525 for (i = 0; i < fastload_num; i++)
4527 if (fastload[i].data)
4528 free(fastload[i].data);
4530 free(fastload);
4531 fastload = NULL;
4538 COMMAND_HANDLER(handle_fast_load_image_command)
4540 uint8_t *buffer;
4541 size_t buf_cnt;
4542 uint32_t image_size;
4543 uint32_t min_address = 0;
4544 uint32_t max_address = 0xffffffff;
4545 int i;
4547 struct image image;
4549 int retval = CALL_COMMAND_HANDLER(parse_load_image_command_CMD_ARGV,
4550 &image, &min_address, &max_address);
4551 if (ERROR_OK != retval)
4552 return retval;
4554 struct duration bench;
4555 duration_start(&bench);
4557 if (image_open(&image, CMD_ARGV[0], (CMD_ARGC >= 3) ? CMD_ARGV[2] : NULL) != ERROR_OK)
4559 return ERROR_OK;
4562 image_size = 0x0;
4563 retval = ERROR_OK;
4564 fastload_num = image.num_sections;
4565 fastload = (struct FastLoad *)malloc(sizeof(struct FastLoad)*image.num_sections);
4566 if (fastload == NULL)
4568 image_close(&image);
4569 return ERROR_FAIL;
4571 memset(fastload, 0, sizeof(struct FastLoad)*image.num_sections);
4572 for (i = 0; i < image.num_sections; i++)
4574 buffer = malloc(image.sections[i].size);
4575 if (buffer == NULL)
4577 command_print(CMD_CTX, "error allocating buffer for section (%d bytes)",
4578 (int)(image.sections[i].size));
4579 break;
4582 if ((retval = image_read_section(&image, i, 0x0, image.sections[i].size, buffer, &buf_cnt)) != ERROR_OK)
4584 free(buffer);
4585 break;
4588 uint32_t offset = 0;
4589 uint32_t length = buf_cnt;
4592 /* DANGER!!! beware of unsigned comparision here!!! */
4594 if ((image.sections[i].base_address + buf_cnt >= min_address)&&
4595 (image.sections[i].base_address < max_address))
4597 if (image.sections[i].base_address < min_address)
4599 /* clip addresses below */
4600 offset += min_address-image.sections[i].base_address;
4601 length -= offset;
4604 if (image.sections[i].base_address + buf_cnt > max_address)
4606 length -= (image.sections[i].base_address + buf_cnt)-max_address;
4609 fastload[i].address = image.sections[i].base_address + offset;
4610 fastload[i].data = malloc(length);
4611 if (fastload[i].data == NULL)
4613 free(buffer);
4614 break;
4616 memcpy(fastload[i].data, buffer + offset, length);
4617 fastload[i].length = length;
4619 image_size += length;
4620 command_print(CMD_CTX, "%u bytes written at address 0x%8.8x",
4621 (unsigned int)length,
4622 ((unsigned int)(image.sections[i].base_address + offset)));
4625 free(buffer);
4628 if ((ERROR_OK == retval) && (duration_measure(&bench) == ERROR_OK))
4630 command_print(CMD_CTX, "Loaded %" PRIu32 " bytes "
4631 "in %fs (%0.3f kb/s)", image_size,
4632 duration_elapsed(&bench), duration_kbps(&bench, image_size));
4634 command_print(CMD_CTX,
4635 "WARNING: image has not been loaded to target!"
4636 "You can issue a 'fast_load' to finish loading.");
4639 image_close(&image);
4641 if (retval != ERROR_OK)
4643 free_fastload();
4646 return retval;
4649 COMMAND_HANDLER(handle_fast_load_command)
4651 if (CMD_ARGC > 0)
4652 return ERROR_COMMAND_SYNTAX_ERROR;
4653 if (fastload == NULL)
4655 LOG_ERROR("No image in memory");
4656 return ERROR_FAIL;
4658 int i;
4659 int ms = timeval_ms();
4660 int size = 0;
4661 int retval = ERROR_OK;
4662 for (i = 0; i < fastload_num;i++)
4664 struct target *target = get_current_target(CMD_CTX);
4665 command_print(CMD_CTX, "Write to 0x%08x, length 0x%08x",
4666 (unsigned int)(fastload[i].address),
4667 (unsigned int)(fastload[i].length));
4668 if (retval == ERROR_OK)
4670 retval = target_write_buffer(target, fastload[i].address, fastload[i].length, fastload[i].data);
4672 size += fastload[i].length;
4674 int after = timeval_ms();
4675 command_print(CMD_CTX, "Loaded image %f kBytes/s", (float)(size/1024.0)/((float)(after-ms)/1000.0));
4676 return retval;
4679 static int jim_mcrmrc(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
4681 struct command_context *context;
4682 struct target *target;
4683 int retval;
4685 context = Jim_GetAssocData(interp, "context");
4686 if (context == NULL) {
4687 LOG_ERROR("array2mem: no command context");
4688 return JIM_ERR;
4690 target = get_current_target(context);
4691 if (target == NULL) {
4692 LOG_ERROR("array2mem: no current target");
4693 return JIM_ERR;
4696 if ((argc < 6) || (argc > 7))
4698 return JIM_ERR;
4701 int cpnum;
4702 uint32_t op1;
4703 uint32_t op2;
4704 uint32_t CRn;
4705 uint32_t CRm;
4706 uint32_t value;
4708 int e;
4709 long l;
4710 e = Jim_GetLong(interp, argv[1], &l);
4711 if (e != JIM_OK) {
4712 return e;
4714 cpnum = l;
4716 e = Jim_GetLong(interp, argv[2], &l);
4717 if (e != JIM_OK) {
4718 return e;
4720 op1 = l;
4722 e = Jim_GetLong(interp, argv[3], &l);
4723 if (e != JIM_OK) {
4724 return e;
4726 CRn = l;
4728 e = Jim_GetLong(interp, argv[4], &l);
4729 if (e != JIM_OK) {
4730 return e;
4732 CRm = l;
4734 e = Jim_GetLong(interp, argv[5], &l);
4735 if (e != JIM_OK) {
4736 return e;
4738 op2 = l;
4740 value = 0;
4742 if (argc == 7)
4744 e = Jim_GetLong(interp, argv[6], &l);
4745 if (e != JIM_OK) {
4746 return e;
4748 value = l;
4750 retval = target_mcr(target, cpnum, op1, op2, CRn, CRm, value);
4751 if (retval != ERROR_OK)
4752 return JIM_ERR;
4753 } else
4755 retval = target_mrc(target, cpnum, op1, op2, CRn, CRm, &value);
4756 if (retval != ERROR_OK)
4757 return JIM_ERR;
4759 Jim_SetResult(interp, Jim_NewIntObj(interp, value));
4762 return JIM_OK;
4765 int target_register_commands(struct command_context *cmd_ctx)
4768 register_command(cmd_ctx, NULL, "targets",
4769 handle_targets_command, COMMAND_EXEC,
4770 "change current command line target (one parameter) "
4771 "or list targets (no parameters)");
4773 register_jim(cmd_ctx, "target", jim_target, "configure target");
4775 return ERROR_OK;
4778 int target_register_user_commands(struct command_context *cmd_ctx)
4780 int retval = ERROR_OK;
4781 if ((retval = target_request_register_commands(cmd_ctx)) != ERROR_OK)
4782 return retval;
4784 if ((retval = trace_register_commands(cmd_ctx)) != ERROR_OK)
4785 return retval;
4787 register_command(cmd_ctx, NULL, "profile",
4788 handle_profile_command, COMMAND_EXEC,
4789 "profiling samples the CPU PC");
4791 register_jim(cmd_ctx, "ocd_mem2array", jim_mem2array,
4792 "read memory and return as a TCL array for script processing "
4793 "<ARRAYNAME> <WIDTH = 32/16/8> <ADDRESS> <COUNT>");
4795 register_jim(cmd_ctx, "ocd_array2mem", jim_array2mem,
4796 "convert a TCL array to memory locations and write the values "
4797 "<ARRAYNAME> <WIDTH = 32/16/8> <ADDRESS> <COUNT>");
4799 register_command(cmd_ctx, NULL, "fast_load_image",
4800 handle_fast_load_image_command, COMMAND_ANY,
4801 "same CMD_ARGV as load_image, image stored in memory "
4802 "- mainly for profiling purposes");
4804 register_command(cmd_ctx, NULL, "fast_load",
4805 handle_fast_load_command, COMMAND_ANY,
4806 "loads active fast load image to current target "
4807 "- mainly for profiling purposes");
4809 /** @todo don't register virt2phys() unless target supports it */
4810 register_command(cmd_ctx, NULL, "virt2phys",
4811 handle_virt2phys_command, COMMAND_ANY,
4812 "translate a virtual address into a physical address");
4814 register_command(cmd_ctx, NULL, "reg",
4815 handle_reg_command, COMMAND_EXEC,
4816 "display or set a register");
4818 register_command(cmd_ctx, NULL, "poll",
4819 handle_poll_command, COMMAND_EXEC,
4820 "poll target state");
4821 register_command(cmd_ctx, NULL, "wait_halt",
4822 handle_wait_halt_command, COMMAND_EXEC,
4823 "wait for target halt [time (s)]");
4824 register_command(cmd_ctx, NULL, "halt",
4825 handle_halt_command, COMMAND_EXEC,
4826 "halt target");
4827 register_command(cmd_ctx, NULL, "resume",
4828 handle_resume_command, COMMAND_EXEC,
4829 "resume target [addr]");
4830 register_command(cmd_ctx, NULL, "reset",
4831 handle_reset_command, COMMAND_EXEC,
4832 "reset target [run | halt | init] - default is run");
4833 register_command(cmd_ctx, NULL, "soft_reset_halt",
4834 handle_soft_reset_halt_command, COMMAND_EXEC,
4835 "halt the target and do a soft reset");
4837 register_command(cmd_ctx, NULL, "step",
4838 handle_step_command, COMMAND_EXEC,
4839 "step one instruction from current PC or [addr]");
4841 register_command(cmd_ctx, NULL, "mdw",
4842 handle_md_command, COMMAND_EXEC,
4843 "display memory words [phys] <addr> [count]");
4844 register_command(cmd_ctx, NULL, "mdh",
4845 handle_md_command, COMMAND_EXEC,
4846 "display memory half-words [phys] <addr> [count]");
4847 register_command(cmd_ctx, NULL, "mdb",
4848 handle_md_command, COMMAND_EXEC,
4849 "display memory bytes [phys] <addr> [count]");
4851 register_command(cmd_ctx, NULL, "mww",
4852 handle_mw_command, COMMAND_EXEC,
4853 "write memory word [phys] <addr> <value> [count]");
4854 register_command(cmd_ctx, NULL, "mwh",
4855 handle_mw_command, COMMAND_EXEC,
4856 "write memory half-word [phys] <addr> <value> [count]");
4857 register_command(cmd_ctx, NULL, "mwb",
4858 handle_mw_command, COMMAND_EXEC,
4859 "write memory byte [phys] <addr> <value> [count]");
4861 register_command(cmd_ctx, NULL, "bp",
4862 handle_bp_command, COMMAND_EXEC,
4863 "list or set breakpoint [<address> <length> [hw]]");
4864 register_command(cmd_ctx, NULL, "rbp",
4865 handle_rbp_command, COMMAND_EXEC,
4866 "remove breakpoint <address>");
4868 register_command(cmd_ctx, NULL, "wp",
4869 handle_wp_command, COMMAND_EXEC,
4870 "list or set watchpoint "
4871 "[<address> <length> <r/w/a> [value] [mask]]");
4872 register_command(cmd_ctx, NULL, "rwp",
4873 handle_rwp_command, COMMAND_EXEC,
4874 "remove watchpoint <address>");
4876 register_command(cmd_ctx, NULL, "load_image",
4877 handle_load_image_command, COMMAND_EXEC,
4878 "load_image <file> <address> "
4879 "['bin'|'ihex'|'elf'|'s19'] [min_address] [max_length]");
4880 register_command(cmd_ctx, NULL, "dump_image",
4881 handle_dump_image_command, COMMAND_EXEC,
4882 "dump_image <file> <address> <size>");
4883 register_command(cmd_ctx, NULL, "verify_image",
4884 handle_verify_image_command, COMMAND_EXEC,
4885 "verify_image <file> [offset] [type]");
4886 register_command(cmd_ctx, NULL, "test_image",
4887 handle_test_image_command, COMMAND_EXEC,
4888 "test_image <file> [offset] [type]");
4890 return ERROR_OK;