target: don't implicitly include "breakpoint.h"
[openocd/ztw.git] / src / target / target.c
blobf5a092a707ed1b5e764a9531a72b17524abbf6f7
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 (argc == 1)
1653 target = get_target(args[0]);
1654 if (target == NULL) {
1655 command_print(cmd_ctx,"Target: %s is unknown, try one of:\n", args[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 int 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 (argc == 0)
1873 struct reg_cache *cache = target->reg_cache;
1875 count = 0;
1876 while (cache)
1878 int 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 ((args[0][0] >= '0') && (args[0][0] <= '9'))
1913 unsigned num;
1914 COMMAND_PARSE_NUMBER(uint, args[0], num);
1916 struct reg_cache *cache = target->reg_cache;
1917 count = 0;
1918 while (cache)
1920 int i;
1921 for (i = 0; i < cache->num_regs; i++)
1923 if (count++ == (int)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, args[0], 1);
1943 if (!reg)
1945 command_print(cmd_ctx, "register %s not found in current target", args[0]);
1946 return ERROR_OK;
1950 /* display a register */
1951 if ((argc == 1) || ((argc == 2) && !((args[1][0] >= '0') && (args[1][0] <= '9'))))
1953 if ((argc == 2) && (strcmp(args[1], "force") == 0))
1954 reg->valid = 0;
1956 if (reg->valid == 0)
1958 struct reg_arch_type *arch_type = register_get_arch_type(reg->arch_type);
1959 arch_type->get(reg);
1961 value = buf_to_str(reg->value, reg->size, 16);
1962 command_print(cmd_ctx, "%s (/%i): 0x%s", reg->name, (int)(reg->size), value);
1963 free(value);
1964 return ERROR_OK;
1967 /* set register value */
1968 if (argc == 2)
1970 uint8_t *buf = malloc(CEIL(reg->size, 8));
1971 str_to_buf(args[1], strlen(args[1]), buf, reg->size, 0);
1973 struct reg_arch_type *arch_type = register_get_arch_type(reg->arch_type);
1974 arch_type->set(reg, buf);
1976 value = buf_to_str(reg->value, reg->size, 16);
1977 command_print(cmd_ctx, "%s (/%i): 0x%s", reg->name, (int)(reg->size), value);
1978 free(value);
1980 free(buf);
1982 return ERROR_OK;
1985 command_print(cmd_ctx, "usage: reg <#|name> [value]");
1987 return ERROR_OK;
1990 COMMAND_HANDLER(handle_poll_command)
1992 int retval = ERROR_OK;
1993 struct target *target = get_current_target(cmd_ctx);
1995 if (argc == 0)
1997 command_print(cmd_ctx, "background polling: %s",
1998 jtag_poll_get_enabled() ? "on" : "off");
1999 command_print(cmd_ctx, "TAP: %s (%s)",
2000 target->tap->dotted_name,
2001 target->tap->enabled ? "enabled" : "disabled");
2002 if (!target->tap->enabled)
2003 return ERROR_OK;
2004 if ((retval = target_poll(target)) != ERROR_OK)
2005 return retval;
2006 if ((retval = target_arch_state(target)) != ERROR_OK)
2007 return retval;
2010 else if (argc == 1)
2012 if (strcmp(args[0], "on") == 0)
2014 jtag_poll_set_enabled(true);
2016 else if (strcmp(args[0], "off") == 0)
2018 jtag_poll_set_enabled(false);
2020 else
2022 command_print(cmd_ctx, "arg is \"on\" or \"off\"");
2024 } else
2026 return ERROR_COMMAND_SYNTAX_ERROR;
2029 return retval;
2032 COMMAND_HANDLER(handle_wait_halt_command)
2034 if (argc > 1)
2035 return ERROR_COMMAND_SYNTAX_ERROR;
2037 unsigned ms = 5000;
2038 if (1 == argc)
2040 int retval = parse_uint(args[0], &ms);
2041 if (ERROR_OK != retval)
2043 command_print(cmd_ctx, "usage: %s [seconds]", CMD_NAME);
2044 return ERROR_COMMAND_SYNTAX_ERROR;
2046 // convert seconds (given) to milliseconds (needed)
2047 ms *= 1000;
2050 struct target *target = get_current_target(cmd_ctx);
2051 return target_wait_state(target, TARGET_HALTED, ms);
2054 /* wait for target state to change. The trick here is to have a low
2055 * latency for short waits and not to suck up all the CPU time
2056 * on longer waits.
2058 * After 500ms, keep_alive() is invoked
2060 int target_wait_state(struct target *target, enum target_state state, int ms)
2062 int retval;
2063 long long then = 0, cur;
2064 int once = 1;
2066 for (;;)
2068 if ((retval = target_poll(target)) != ERROR_OK)
2069 return retval;
2070 if (target->state == state)
2072 break;
2074 cur = timeval_ms();
2075 if (once)
2077 once = 0;
2078 then = timeval_ms();
2079 LOG_DEBUG("waiting for target %s...",
2080 Jim_Nvp_value2name_simple(nvp_target_state,state)->name);
2083 if (cur-then > 500)
2085 keep_alive();
2088 if ((cur-then) > ms)
2090 LOG_ERROR("timed out while waiting for target %s",
2091 Jim_Nvp_value2name_simple(nvp_target_state,state)->name);
2092 return ERROR_FAIL;
2096 return ERROR_OK;
2099 COMMAND_HANDLER(handle_halt_command)
2101 LOG_DEBUG("-");
2103 struct target *target = get_current_target(cmd_ctx);
2104 int retval = target_halt(target);
2105 if (ERROR_OK != retval)
2106 return retval;
2108 if (argc == 1)
2110 unsigned wait;
2111 retval = parse_uint(args[0], &wait);
2112 if (ERROR_OK != retval)
2113 return ERROR_COMMAND_SYNTAX_ERROR;
2114 if (!wait)
2115 return ERROR_OK;
2118 return CALL_COMMAND_HANDLER(handle_wait_halt_command);
2121 COMMAND_HANDLER(handle_soft_reset_halt_command)
2123 struct target *target = get_current_target(cmd_ctx);
2125 LOG_USER("requesting target halt and executing a soft reset");
2127 target->type->soft_reset_halt(target);
2129 return ERROR_OK;
2132 COMMAND_HANDLER(handle_reset_command)
2134 if (argc > 1)
2135 return ERROR_COMMAND_SYNTAX_ERROR;
2137 enum target_reset_mode reset_mode = RESET_RUN;
2138 if (argc == 1)
2140 const Jim_Nvp *n;
2141 n = Jim_Nvp_name2value_simple(nvp_reset_modes, args[0]);
2142 if ((n->name == NULL) || (n->value == RESET_UNKNOWN)) {
2143 return ERROR_COMMAND_SYNTAX_ERROR;
2145 reset_mode = n->value;
2148 /* reset *all* targets */
2149 return target_process_reset(cmd_ctx, reset_mode);
2153 COMMAND_HANDLER(handle_resume_command)
2155 int current = 1;
2156 if (argc > 1)
2157 return ERROR_COMMAND_SYNTAX_ERROR;
2159 struct target *target = get_current_target(cmd_ctx);
2160 target_handle_event(target, TARGET_EVENT_OLD_pre_resume);
2162 /* with no args, resume from current pc, addr = 0,
2163 * with one arguments, addr = args[0],
2164 * handle breakpoints, not debugging */
2165 uint32_t addr = 0;
2166 if (argc == 1)
2168 COMMAND_PARSE_NUMBER(u32, args[0], addr);
2169 current = 0;
2172 return target_resume(target, current, addr, 1, 0);
2175 COMMAND_HANDLER(handle_step_command)
2177 if (argc > 1)
2178 return ERROR_COMMAND_SYNTAX_ERROR;
2180 LOG_DEBUG("-");
2182 /* with no args, step from current pc, addr = 0,
2183 * with one argument addr = args[0],
2184 * handle breakpoints, debugging */
2185 uint32_t addr = 0;
2186 int current_pc = 1;
2187 if (argc == 1)
2189 COMMAND_PARSE_NUMBER(u32, args[0], addr);
2190 current_pc = 0;
2193 struct target *target = get_current_target(cmd_ctx);
2195 return target->type->step(target, current_pc, addr, 1);
2198 static void handle_md_output(struct command_context *cmd_ctx,
2199 struct target *target, uint32_t address, unsigned size,
2200 unsigned count, const uint8_t *buffer)
2202 const unsigned line_bytecnt = 32;
2203 unsigned line_modulo = line_bytecnt / size;
2205 char output[line_bytecnt * 4 + 1];
2206 unsigned output_len = 0;
2208 const char *value_fmt;
2209 switch (size) {
2210 case 4: value_fmt = "%8.8x "; break;
2211 case 2: value_fmt = "%4.2x "; break;
2212 case 1: value_fmt = "%2.2x "; break;
2213 default:
2214 LOG_ERROR("invalid memory read size: %u", size);
2215 exit(-1);
2218 for (unsigned i = 0; i < count; i++)
2220 if (i % line_modulo == 0)
2222 output_len += snprintf(output + output_len,
2223 sizeof(output) - output_len,
2224 "0x%8.8x: ",
2225 (unsigned)(address + (i*size)));
2228 uint32_t value = 0;
2229 const uint8_t *value_ptr = buffer + i * size;
2230 switch (size) {
2231 case 4: value = target_buffer_get_u32(target, value_ptr); break;
2232 case 2: value = target_buffer_get_u16(target, value_ptr); break;
2233 case 1: value = *value_ptr;
2235 output_len += snprintf(output + output_len,
2236 sizeof(output) - output_len,
2237 value_fmt, value);
2239 if ((i % line_modulo == line_modulo - 1) || (i == count - 1))
2241 command_print(cmd_ctx, "%s", output);
2242 output_len = 0;
2247 COMMAND_HANDLER(handle_md_command)
2249 if (argc < 1)
2250 return ERROR_COMMAND_SYNTAX_ERROR;
2252 unsigned size = 0;
2253 const char *cmd_name = CMD_NAME;
2254 switch (cmd_name[6]) {
2255 case 'w': size = 4; break;
2256 case 'h': size = 2; break;
2257 case 'b': size = 1; break;
2258 default: return ERROR_COMMAND_SYNTAX_ERROR;
2261 bool physical=strcmp(args[0], "phys")==0;
2262 int (*fn)(struct target *target,
2263 uint32_t address, uint32_t size, uint32_t count, uint8_t *buffer);
2264 if (physical)
2266 argc--;
2267 args++;
2268 fn=target_read_phys_memory;
2269 } else
2271 fn=target_read_memory;
2273 if ((argc < 1) || (argc > 2))
2275 return ERROR_COMMAND_SYNTAX_ERROR;
2278 uint32_t address;
2279 COMMAND_PARSE_NUMBER(u32, args[0], address);
2281 unsigned count = 1;
2282 if (argc == 2)
2283 COMMAND_PARSE_NUMBER(uint, args[1], count);
2285 uint8_t *buffer = calloc(count, size);
2287 struct target *target = get_current_target(cmd_ctx);
2288 int retval = fn(target, address, size, count, buffer);
2289 if (ERROR_OK == retval)
2290 handle_md_output(cmd_ctx, target, address, size, count, buffer);
2292 free(buffer);
2294 return retval;
2297 COMMAND_HANDLER(handle_mw_command)
2299 if (argc < 2)
2301 return ERROR_COMMAND_SYNTAX_ERROR;
2303 bool physical=strcmp(args[0], "phys")==0;
2304 int (*fn)(struct target *target,
2305 uint32_t address, uint32_t size, uint32_t count, uint8_t *buffer);
2306 const char *cmd_name = CMD_NAME;
2307 if (physical)
2309 argc--;
2310 args++;
2311 fn=target_write_phys_memory;
2312 } else
2314 fn=target_write_memory;
2316 if ((argc < 2) || (argc > 3))
2317 return ERROR_COMMAND_SYNTAX_ERROR;
2319 uint32_t address;
2320 COMMAND_PARSE_NUMBER(u32, args[0], address);
2322 uint32_t value;
2323 COMMAND_PARSE_NUMBER(u32, args[1], value);
2325 unsigned count = 1;
2326 if (argc == 3)
2327 COMMAND_PARSE_NUMBER(uint, args[2], count);
2329 struct target *target = get_current_target(cmd_ctx);
2330 unsigned wordsize;
2331 uint8_t value_buf[4];
2332 switch (cmd_name[6])
2334 case 'w':
2335 wordsize = 4;
2336 target_buffer_set_u32(target, value_buf, value);
2337 break;
2338 case 'h':
2339 wordsize = 2;
2340 target_buffer_set_u16(target, value_buf, value);
2341 break;
2342 case 'b':
2343 wordsize = 1;
2344 value_buf[0] = value;
2345 break;
2346 default:
2347 return ERROR_COMMAND_SYNTAX_ERROR;
2349 for (unsigned i = 0; i < count; i++)
2351 int retval = fn(target,
2352 address + i * wordsize, wordsize, 1, value_buf);
2353 if (ERROR_OK != retval)
2354 return retval;
2355 keep_alive();
2358 return ERROR_OK;
2362 static COMMAND_HELPER(parse_load_image_command_args, struct image *image,
2363 uint32_t *min_address, uint32_t *max_address)
2365 if (argc < 1 || argc > 5)
2366 return ERROR_COMMAND_SYNTAX_ERROR;
2368 /* a base address isn't always necessary,
2369 * default to 0x0 (i.e. don't relocate) */
2370 if (argc >= 2)
2372 uint32_t addr;
2373 COMMAND_PARSE_NUMBER(u32, args[1], addr);
2374 image->base_address = addr;
2375 image->base_address_set = 1;
2377 else
2378 image->base_address_set = 0;
2380 image->start_address_set = 0;
2382 if (argc >= 4)
2384 COMMAND_PARSE_NUMBER(u32, args[3], *min_address);
2386 if (argc == 5)
2388 COMMAND_PARSE_NUMBER(u32, args[4], *max_address);
2389 // use size (given) to find max (required)
2390 *max_address += *min_address;
2393 if (*min_address > *max_address)
2394 return ERROR_COMMAND_SYNTAX_ERROR;
2396 return ERROR_OK;
2399 COMMAND_HANDLER(handle_load_image_command)
2401 uint8_t *buffer;
2402 uint32_t buf_cnt;
2403 uint32_t image_size;
2404 uint32_t min_address = 0;
2405 uint32_t max_address = 0xffffffff;
2406 int i;
2407 struct image image;
2409 int retval = CALL_COMMAND_HANDLER(parse_load_image_command_args,
2410 &image, &min_address, &max_address);
2411 if (ERROR_OK != retval)
2412 return retval;
2414 struct target *target = get_current_target(cmd_ctx);
2416 struct duration bench;
2417 duration_start(&bench);
2419 if (image_open(&image, args[0], (argc >= 3) ? args[2] : NULL) != ERROR_OK)
2421 return ERROR_OK;
2424 image_size = 0x0;
2425 retval = ERROR_OK;
2426 for (i = 0; i < image.num_sections; i++)
2428 buffer = malloc(image.sections[i].size);
2429 if (buffer == NULL)
2431 command_print(cmd_ctx,
2432 "error allocating buffer for section (%d bytes)",
2433 (int)(image.sections[i].size));
2434 break;
2437 if ((retval = image_read_section(&image, i, 0x0, image.sections[i].size, buffer, &buf_cnt)) != ERROR_OK)
2439 free(buffer);
2440 break;
2443 uint32_t offset = 0;
2444 uint32_t length = buf_cnt;
2446 /* DANGER!!! beware of unsigned comparision here!!! */
2448 if ((image.sections[i].base_address + buf_cnt >= min_address)&&
2449 (image.sections[i].base_address < max_address))
2451 if (image.sections[i].base_address < min_address)
2453 /* clip addresses below */
2454 offset += min_address-image.sections[i].base_address;
2455 length -= offset;
2458 if (image.sections[i].base_address + buf_cnt > max_address)
2460 length -= (image.sections[i].base_address + buf_cnt)-max_address;
2463 if ((retval = target_write_buffer(target, image.sections[i].base_address + offset, length, buffer + offset)) != ERROR_OK)
2465 free(buffer);
2466 break;
2468 image_size += length;
2469 command_print(cmd_ctx, "%u bytes written at address 0x%8.8" PRIx32 "",
2470 (unsigned int)length,
2471 image.sections[i].base_address + offset);
2474 free(buffer);
2477 if ((ERROR_OK == retval) && (duration_measure(&bench) == ERROR_OK))
2479 command_print(cmd_ctx, "downloaded %" PRIu32 " bytes "
2480 "in %fs (%0.3f kb/s)", image_size,
2481 duration_elapsed(&bench), duration_kbps(&bench, image_size));
2484 image_close(&image);
2486 return retval;
2490 COMMAND_HANDLER(handle_dump_image_command)
2492 struct fileio fileio;
2494 uint8_t buffer[560];
2495 int retvaltemp;
2498 struct target *target = get_current_target(cmd_ctx);
2500 if (argc != 3)
2502 command_print(cmd_ctx, "usage: dump_image <filename> <address> <size>");
2503 return ERROR_OK;
2506 uint32_t address;
2507 COMMAND_PARSE_NUMBER(u32, args[1], address);
2508 uint32_t size;
2509 COMMAND_PARSE_NUMBER(u32, args[2], size);
2511 if (fileio_open(&fileio, args[0], FILEIO_WRITE, FILEIO_BINARY) != ERROR_OK)
2513 return ERROR_OK;
2516 struct duration bench;
2517 duration_start(&bench);
2519 int retval = ERROR_OK;
2520 while (size > 0)
2522 uint32_t size_written;
2523 uint32_t this_run_size = (size > 560) ? 560 : size;
2524 retval = target_read_buffer(target, address, this_run_size, buffer);
2525 if (retval != ERROR_OK)
2527 break;
2530 retval = fileio_write(&fileio, this_run_size, buffer, &size_written);
2531 if (retval != ERROR_OK)
2533 break;
2536 size -= this_run_size;
2537 address += this_run_size;
2540 if ((retvaltemp = fileio_close(&fileio)) != ERROR_OK)
2541 return retvaltemp;
2543 if ((ERROR_OK == retval) && (duration_measure(&bench) == ERROR_OK))
2545 command_print(cmd_ctx,
2546 "dumped %lld bytes in %fs (%0.3f kb/s)", fileio.size,
2547 duration_elapsed(&bench), duration_kbps(&bench, fileio.size));
2550 return retval;
2553 static COMMAND_HELPER(handle_verify_image_command_internal, int verify)
2555 uint8_t *buffer;
2556 uint32_t buf_cnt;
2557 uint32_t image_size;
2558 int i;
2559 int retval;
2560 uint32_t checksum = 0;
2561 uint32_t mem_checksum = 0;
2563 struct image image;
2565 struct target *target = get_current_target(cmd_ctx);
2567 if (argc < 1)
2569 return ERROR_COMMAND_SYNTAX_ERROR;
2572 if (!target)
2574 LOG_ERROR("no target selected");
2575 return ERROR_FAIL;
2578 struct duration bench;
2579 duration_start(&bench);
2581 if (argc >= 2)
2583 uint32_t addr;
2584 COMMAND_PARSE_NUMBER(u32, args[1], addr);
2585 image.base_address = addr;
2586 image.base_address_set = 1;
2588 else
2590 image.base_address_set = 0;
2591 image.base_address = 0x0;
2594 image.start_address_set = 0;
2596 if ((retval = image_open(&image, args[0], (argc == 3) ? args[2] : NULL)) != ERROR_OK)
2598 return retval;
2601 image_size = 0x0;
2602 retval = ERROR_OK;
2603 for (i = 0; i < image.num_sections; i++)
2605 buffer = malloc(image.sections[i].size);
2606 if (buffer == NULL)
2608 command_print(cmd_ctx,
2609 "error allocating buffer for section (%d bytes)",
2610 (int)(image.sections[i].size));
2611 break;
2613 if ((retval = image_read_section(&image, i, 0x0, image.sections[i].size, buffer, &buf_cnt)) != ERROR_OK)
2615 free(buffer);
2616 break;
2619 if (verify)
2621 /* calculate checksum of image */
2622 image_calculate_checksum(buffer, buf_cnt, &checksum);
2624 retval = target_checksum_memory(target, image.sections[i].base_address, buf_cnt, &mem_checksum);
2625 if (retval != ERROR_OK)
2627 free(buffer);
2628 break;
2631 if (checksum != mem_checksum)
2633 /* failed crc checksum, fall back to a binary compare */
2634 uint8_t *data;
2636 command_print(cmd_ctx, "checksum mismatch - attempting binary compare");
2638 data = (uint8_t*)malloc(buf_cnt);
2640 /* Can we use 32bit word accesses? */
2641 int size = 1;
2642 int count = buf_cnt;
2643 if ((count % 4) == 0)
2645 size *= 4;
2646 count /= 4;
2648 retval = target_read_memory(target, image.sections[i].base_address, size, count, data);
2649 if (retval == ERROR_OK)
2651 uint32_t t;
2652 for (t = 0; t < buf_cnt; t++)
2654 if (data[t] != buffer[t])
2656 command_print(cmd_ctx,
2657 "Verify operation failed address 0x%08x. Was 0x%02x instead of 0x%02x\n",
2658 (unsigned)(t + image.sections[i].base_address),
2659 data[t],
2660 buffer[t]);
2661 free(data);
2662 free(buffer);
2663 retval = ERROR_FAIL;
2664 goto done;
2666 if ((t%16384) == 0)
2668 keep_alive();
2673 free(data);
2675 } else
2677 command_print(cmd_ctx, "address 0x%08" PRIx32 " length 0x%08" PRIx32 "",
2678 image.sections[i].base_address,
2679 buf_cnt);
2682 free(buffer);
2683 image_size += buf_cnt;
2685 done:
2686 if ((ERROR_OK == retval) && (duration_measure(&bench) == ERROR_OK))
2688 command_print(cmd_ctx, "verified %" PRIu32 " bytes "
2689 "in %fs (%0.3f kb/s)", image_size,
2690 duration_elapsed(&bench), duration_kbps(&bench, image_size));
2693 image_close(&image);
2695 return retval;
2698 COMMAND_HANDLER(handle_verify_image_command)
2700 return CALL_COMMAND_HANDLER(handle_verify_image_command_internal, 1);
2703 COMMAND_HANDLER(handle_test_image_command)
2705 return CALL_COMMAND_HANDLER(handle_verify_image_command_internal, 0);
2708 static int handle_bp_command_list(struct command_context *cmd_ctx)
2710 struct target *target = get_current_target(cmd_ctx);
2711 struct breakpoint *breakpoint = target->breakpoints;
2712 while (breakpoint)
2714 if (breakpoint->type == BKPT_SOFT)
2716 char* buf = buf_to_str(breakpoint->orig_instr,
2717 breakpoint->length, 16);
2718 command_print(cmd_ctx, "0x%8.8" PRIx32 ", 0x%x, %i, 0x%s",
2719 breakpoint->address,
2720 breakpoint->length,
2721 breakpoint->set, buf);
2722 free(buf);
2724 else
2726 command_print(cmd_ctx, "0x%8.8" PRIx32 ", 0x%x, %i",
2727 breakpoint->address,
2728 breakpoint->length, breakpoint->set);
2731 breakpoint = breakpoint->next;
2733 return ERROR_OK;
2736 static int handle_bp_command_set(struct command_context *cmd_ctx,
2737 uint32_t addr, uint32_t length, int hw)
2739 struct target *target = get_current_target(cmd_ctx);
2740 int retval = breakpoint_add(target, addr, length, hw);
2741 if (ERROR_OK == retval)
2742 command_print(cmd_ctx, "breakpoint set at 0x%8.8" PRIx32 "", addr);
2743 else
2744 LOG_ERROR("Failure setting breakpoint");
2745 return retval;
2748 COMMAND_HANDLER(handle_bp_command)
2750 if (argc == 0)
2751 return handle_bp_command_list(cmd_ctx);
2753 if (argc < 2 || argc > 3)
2755 command_print(cmd_ctx, "usage: bp <address> <length> ['hw']");
2756 return ERROR_COMMAND_SYNTAX_ERROR;
2759 uint32_t addr;
2760 COMMAND_PARSE_NUMBER(u32, args[0], addr);
2761 uint32_t length;
2762 COMMAND_PARSE_NUMBER(u32, args[1], length);
2764 int hw = BKPT_SOFT;
2765 if (argc == 3)
2767 if (strcmp(args[2], "hw") == 0)
2768 hw = BKPT_HARD;
2769 else
2770 return ERROR_COMMAND_SYNTAX_ERROR;
2773 return handle_bp_command_set(cmd_ctx, addr, length, hw);
2776 COMMAND_HANDLER(handle_rbp_command)
2778 if (argc != 1)
2779 return ERROR_COMMAND_SYNTAX_ERROR;
2781 uint32_t addr;
2782 COMMAND_PARSE_NUMBER(u32, args[0], addr);
2784 struct target *target = get_current_target(cmd_ctx);
2785 breakpoint_remove(target, addr);
2787 return ERROR_OK;
2790 COMMAND_HANDLER(handle_wp_command)
2792 struct target *target = get_current_target(cmd_ctx);
2794 if (argc == 0)
2796 struct watchpoint *watchpoint = target->watchpoints;
2798 while (watchpoint)
2800 command_print(cmd_ctx, "address: 0x%8.8" PRIx32
2801 ", len: 0x%8.8" PRIx32
2802 ", r/w/a: %i, value: 0x%8.8" PRIx32
2803 ", mask: 0x%8.8" PRIx32,
2804 watchpoint->address,
2805 watchpoint->length,
2806 (int)watchpoint->rw,
2807 watchpoint->value,
2808 watchpoint->mask);
2809 watchpoint = watchpoint->next;
2811 return ERROR_OK;
2814 enum watchpoint_rw type = WPT_ACCESS;
2815 uint32_t addr = 0;
2816 uint32_t length = 0;
2817 uint32_t data_value = 0x0;
2818 uint32_t data_mask = 0xffffffff;
2820 switch (argc)
2822 case 5:
2823 COMMAND_PARSE_NUMBER(u32, args[4], data_mask);
2824 // fall through
2825 case 4:
2826 COMMAND_PARSE_NUMBER(u32, args[3], data_value);
2827 // fall through
2828 case 3:
2829 switch (args[2][0])
2831 case 'r':
2832 type = WPT_READ;
2833 break;
2834 case 'w':
2835 type = WPT_WRITE;
2836 break;
2837 case 'a':
2838 type = WPT_ACCESS;
2839 break;
2840 default:
2841 LOG_ERROR("invalid watchpoint mode ('%c')", args[2][0]);
2842 return ERROR_COMMAND_SYNTAX_ERROR;
2844 // fall through
2845 case 2:
2846 COMMAND_PARSE_NUMBER(u32, args[1], length);
2847 COMMAND_PARSE_NUMBER(u32, args[0], addr);
2848 break;
2850 default:
2851 command_print(cmd_ctx, "usage: wp [address length "
2852 "[(r|w|a) [value [mask]]]]");
2853 return ERROR_COMMAND_SYNTAX_ERROR;
2856 int retval = watchpoint_add(target, addr, length, type,
2857 data_value, data_mask);
2858 if (ERROR_OK != retval)
2859 LOG_ERROR("Failure setting watchpoints");
2861 return retval;
2864 COMMAND_HANDLER(handle_rwp_command)
2866 if (argc != 1)
2867 return ERROR_COMMAND_SYNTAX_ERROR;
2869 uint32_t addr;
2870 COMMAND_PARSE_NUMBER(u32, args[0], addr);
2872 struct target *target = get_current_target(cmd_ctx);
2873 watchpoint_remove(target, addr);
2875 return ERROR_OK;
2880 * Translate a virtual address to a physical address.
2882 * The low-level target implementation must have logged a detailed error
2883 * which is forwarded to telnet/GDB session.
2885 COMMAND_HANDLER(handle_virt2phys_command)
2887 if (argc != 1)
2888 return ERROR_COMMAND_SYNTAX_ERROR;
2890 uint32_t va;
2891 COMMAND_PARSE_NUMBER(u32, args[0], va);
2892 uint32_t pa;
2894 struct target *target = get_current_target(cmd_ctx);
2895 int retval = target->type->virt2phys(target, va, &pa);
2896 if (retval == ERROR_OK)
2897 command_print(cmd_ctx, "Physical address 0x%08" PRIx32 "", pa);
2899 return retval;
2902 static void writeData(FILE *f, const void *data, size_t len)
2904 size_t written = fwrite(data, 1, len, f);
2905 if (written != len)
2906 LOG_ERROR("failed to write %zu bytes: %s", len, strerror(errno));
2909 static void writeLong(FILE *f, int l)
2911 int i;
2912 for (i = 0; i < 4; i++)
2914 char c = (l >> (i*8))&0xff;
2915 writeData(f, &c, 1);
2920 static void writeString(FILE *f, char *s)
2922 writeData(f, s, strlen(s));
2925 /* Dump a gmon.out histogram file. */
2926 static void writeGmon(uint32_t *samples, uint32_t sampleNum, const char *filename)
2928 uint32_t i;
2929 FILE *f = fopen(filename, "w");
2930 if (f == NULL)
2931 return;
2932 writeString(f, "gmon");
2933 writeLong(f, 0x00000001); /* Version */
2934 writeLong(f, 0); /* padding */
2935 writeLong(f, 0); /* padding */
2936 writeLong(f, 0); /* padding */
2938 uint8_t zero = 0; /* GMON_TAG_TIME_HIST */
2939 writeData(f, &zero, 1);
2941 /* figure out bucket size */
2942 uint32_t min = samples[0];
2943 uint32_t max = samples[0];
2944 for (i = 0; i < sampleNum; i++)
2946 if (min > samples[i])
2948 min = samples[i];
2950 if (max < samples[i])
2952 max = samples[i];
2956 int addressSpace = (max-min + 1);
2958 static const uint32_t maxBuckets = 256 * 1024; /* maximum buckets. */
2959 uint32_t length = addressSpace;
2960 if (length > maxBuckets)
2962 length = maxBuckets;
2964 int *buckets = malloc(sizeof(int)*length);
2965 if (buckets == NULL)
2967 fclose(f);
2968 return;
2970 memset(buckets, 0, sizeof(int)*length);
2971 for (i = 0; i < sampleNum;i++)
2973 uint32_t address = samples[i];
2974 long long a = address-min;
2975 long long b = length-1;
2976 long long c = addressSpace-1;
2977 int index = (a*b)/c; /* danger!!!! int32 overflows */
2978 buckets[index]++;
2981 /* append binary memory gmon.out &profile_hist_hdr ((char*)&profile_hist_hdr + sizeof(struct gmon_hist_hdr)) */
2982 writeLong(f, min); /* low_pc */
2983 writeLong(f, max); /* high_pc */
2984 writeLong(f, length); /* # of samples */
2985 writeLong(f, 64000000); /* 64MHz */
2986 writeString(f, "seconds");
2987 for (i = 0; i < (15-strlen("seconds")); i++)
2988 writeData(f, &zero, 1);
2989 writeString(f, "s");
2991 /*append binary memory gmon.out profile_hist_data (profile_hist_data + profile_hist_hdr.hist_size) */
2993 char *data = malloc(2*length);
2994 if (data != NULL)
2996 for (i = 0; i < length;i++)
2998 int val;
2999 val = buckets[i];
3000 if (val > 65535)
3002 val = 65535;
3004 data[i*2]=val&0xff;
3005 data[i*2 + 1]=(val >> 8)&0xff;
3007 free(buckets);
3008 writeData(f, data, length * 2);
3009 free(data);
3010 } else
3012 free(buckets);
3015 fclose(f);
3018 /* profiling samples the CPU PC as quickly as OpenOCD is able, which will be used as a random sampling of PC */
3019 COMMAND_HANDLER(handle_profile_command)
3021 struct target *target = get_current_target(cmd_ctx);
3022 struct timeval timeout, now;
3024 gettimeofday(&timeout, NULL);
3025 if (argc != 2)
3027 return ERROR_COMMAND_SYNTAX_ERROR;
3029 unsigned offset;
3030 COMMAND_PARSE_NUMBER(uint, args[0], offset);
3032 timeval_add_time(&timeout, offset, 0);
3034 command_print(cmd_ctx, "Starting profiling. Halting and resuming the target as often as we can...");
3036 static const int maxSample = 10000;
3037 uint32_t *samples = malloc(sizeof(uint32_t)*maxSample);
3038 if (samples == NULL)
3039 return ERROR_OK;
3041 int numSamples = 0;
3042 /* hopefully it is safe to cache! We want to stop/restart as quickly as possible. */
3043 struct reg *reg = register_get_by_name(target->reg_cache, "pc", 1);
3045 for (;;)
3047 int retval;
3048 target_poll(target);
3049 if (target->state == TARGET_HALTED)
3051 uint32_t t=*((uint32_t *)reg->value);
3052 samples[numSamples++]=t;
3053 retval = target_resume(target, 1, 0, 0, 0); /* current pc, addr = 0, do not handle breakpoints, not debugging */
3054 target_poll(target);
3055 alive_sleep(10); /* sleep 10ms, i.e. <100 samples/second. */
3056 } else if (target->state == TARGET_RUNNING)
3058 /* We want to quickly sample the PC. */
3059 if ((retval = target_halt(target)) != ERROR_OK)
3061 free(samples);
3062 return retval;
3064 } else
3066 command_print(cmd_ctx, "Target not halted or running");
3067 retval = ERROR_OK;
3068 break;
3070 if (retval != ERROR_OK)
3072 break;
3075 gettimeofday(&now, NULL);
3076 if ((numSamples >= maxSample) || ((now.tv_sec >= timeout.tv_sec) && (now.tv_usec >= timeout.tv_usec)))
3078 command_print(cmd_ctx, "Profiling completed. %d samples.", numSamples);
3079 if ((retval = target_poll(target)) != ERROR_OK)
3081 free(samples);
3082 return retval;
3084 if (target->state == TARGET_HALTED)
3086 target_resume(target, 1, 0, 0, 0); /* current pc, addr = 0, do not handle breakpoints, not debugging */
3088 if ((retval = target_poll(target)) != ERROR_OK)
3090 free(samples);
3091 return retval;
3093 writeGmon(samples, numSamples, args[1]);
3094 command_print(cmd_ctx, "Wrote %s", args[1]);
3095 break;
3098 free(samples);
3100 return ERROR_OK;
3103 static int new_int_array_element(Jim_Interp * interp, const char *varname, int idx, uint32_t val)
3105 char *namebuf;
3106 Jim_Obj *nameObjPtr, *valObjPtr;
3107 int result;
3109 namebuf = alloc_printf("%s(%d)", varname, idx);
3110 if (!namebuf)
3111 return JIM_ERR;
3113 nameObjPtr = Jim_NewStringObj(interp, namebuf, -1);
3114 valObjPtr = Jim_NewIntObj(interp, val);
3115 if (!nameObjPtr || !valObjPtr)
3117 free(namebuf);
3118 return JIM_ERR;
3121 Jim_IncrRefCount(nameObjPtr);
3122 Jim_IncrRefCount(valObjPtr);
3123 result = Jim_SetVariable(interp, nameObjPtr, valObjPtr);
3124 Jim_DecrRefCount(interp, nameObjPtr);
3125 Jim_DecrRefCount(interp, valObjPtr);
3126 free(namebuf);
3127 /* printf("%s(%d) <= 0%08x\n", varname, idx, val); */
3128 return result;
3131 static int jim_mem2array(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
3133 struct command_context *context;
3134 struct target *target;
3136 context = Jim_GetAssocData(interp, "context");
3137 if (context == NULL)
3139 LOG_ERROR("mem2array: no command context");
3140 return JIM_ERR;
3142 target = get_current_target(context);
3143 if (target == NULL)
3145 LOG_ERROR("mem2array: no current target");
3146 return JIM_ERR;
3149 return target_mem2array(interp, target, argc-1, argv + 1);
3152 static int target_mem2array(Jim_Interp *interp, struct target *target, int argc, Jim_Obj *const *argv)
3154 long l;
3155 uint32_t width;
3156 int len;
3157 uint32_t addr;
3158 uint32_t count;
3159 uint32_t v;
3160 const char *varname;
3161 uint8_t buffer[4096];
3162 int n, e, retval;
3163 uint32_t i;
3165 /* argv[1] = name of array to receive the data
3166 * argv[2] = desired width
3167 * argv[3] = memory address
3168 * argv[4] = count of times to read
3170 if (argc != 4) {
3171 Jim_WrongNumArgs(interp, 1, argv, "varname width addr nelems");
3172 return JIM_ERR;
3174 varname = Jim_GetString(argv[0], &len);
3175 /* given "foo" get space for worse case "foo(%d)" .. add 20 */
3177 e = Jim_GetLong(interp, argv[1], &l);
3178 width = l;
3179 if (e != JIM_OK) {
3180 return e;
3183 e = Jim_GetLong(interp, argv[2], &l);
3184 addr = l;
3185 if (e != JIM_OK) {
3186 return e;
3188 e = Jim_GetLong(interp, argv[3], &l);
3189 len = l;
3190 if (e != JIM_OK) {
3191 return e;
3193 switch (width) {
3194 case 8:
3195 width = 1;
3196 break;
3197 case 16:
3198 width = 2;
3199 break;
3200 case 32:
3201 width = 4;
3202 break;
3203 default:
3204 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3205 Jim_AppendStrings(interp, Jim_GetResult(interp), "Invalid width param, must be 8/16/32", NULL);
3206 return JIM_ERR;
3208 if (len == 0) {
3209 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3210 Jim_AppendStrings(interp, Jim_GetResult(interp), "mem2array: zero width read?", NULL);
3211 return JIM_ERR;
3213 if ((addr + (len * width)) < addr) {
3214 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3215 Jim_AppendStrings(interp, Jim_GetResult(interp), "mem2array: addr + len - wraps to zero?", NULL);
3216 return JIM_ERR;
3218 /* absurd transfer size? */
3219 if (len > 65536) {
3220 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3221 Jim_AppendStrings(interp, Jim_GetResult(interp), "mem2array: absurd > 64K item request", NULL);
3222 return JIM_ERR;
3225 if ((width == 1) ||
3226 ((width == 2) && ((addr & 1) == 0)) ||
3227 ((width == 4) && ((addr & 3) == 0))) {
3228 /* all is well */
3229 } else {
3230 char buf[100];
3231 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3232 sprintf(buf, "mem2array address: 0x%08" PRIx32 " is not aligned for %" PRId32 " byte reads",
3233 addr,
3234 width);
3235 Jim_AppendStrings(interp, Jim_GetResult(interp), buf , NULL);
3236 return JIM_ERR;
3239 /* Transfer loop */
3241 /* index counter */
3242 n = 0;
3243 /* assume ok */
3244 e = JIM_OK;
3245 while (len) {
3246 /* Slurp... in buffer size chunks */
3248 count = len; /* in objects.. */
3249 if (count > (sizeof(buffer)/width)) {
3250 count = (sizeof(buffer)/width);
3253 retval = target_read_memory(target, addr, width, count, buffer);
3254 if (retval != ERROR_OK) {
3255 /* BOO !*/
3256 LOG_ERROR("mem2array: Read @ 0x%08x, w=%d, cnt=%d, failed",
3257 (unsigned int)addr,
3258 (int)width,
3259 (int)count);
3260 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3261 Jim_AppendStrings(interp, Jim_GetResult(interp), "mem2array: cannot read memory", NULL);
3262 e = JIM_ERR;
3263 len = 0;
3264 } else {
3265 v = 0; /* shut up gcc */
3266 for (i = 0 ;i < count ;i++, n++) {
3267 switch (width) {
3268 case 4:
3269 v = target_buffer_get_u32(target, &buffer[i*width]);
3270 break;
3271 case 2:
3272 v = target_buffer_get_u16(target, &buffer[i*width]);
3273 break;
3274 case 1:
3275 v = buffer[i] & 0x0ff;
3276 break;
3278 new_int_array_element(interp, varname, n, v);
3280 len -= count;
3284 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3286 return JIM_OK;
3289 static int get_int_array_element(Jim_Interp * interp, const char *varname, int idx, uint32_t *val)
3291 char *namebuf;
3292 Jim_Obj *nameObjPtr, *valObjPtr;
3293 int result;
3294 long l;
3296 namebuf = alloc_printf("%s(%d)", varname, idx);
3297 if (!namebuf)
3298 return JIM_ERR;
3300 nameObjPtr = Jim_NewStringObj(interp, namebuf, -1);
3301 if (!nameObjPtr)
3303 free(namebuf);
3304 return JIM_ERR;
3307 Jim_IncrRefCount(nameObjPtr);
3308 valObjPtr = Jim_GetVariable(interp, nameObjPtr, JIM_ERRMSG);
3309 Jim_DecrRefCount(interp, nameObjPtr);
3310 free(namebuf);
3311 if (valObjPtr == NULL)
3312 return JIM_ERR;
3314 result = Jim_GetLong(interp, valObjPtr, &l);
3315 /* printf("%s(%d) => 0%08x\n", varname, idx, val); */
3316 *val = l;
3317 return result;
3320 static int jim_array2mem(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
3322 struct command_context *context;
3323 struct target *target;
3325 context = Jim_GetAssocData(interp, "context");
3326 if (context == NULL) {
3327 LOG_ERROR("array2mem: no command context");
3328 return JIM_ERR;
3330 target = get_current_target(context);
3331 if (target == NULL) {
3332 LOG_ERROR("array2mem: no current target");
3333 return JIM_ERR;
3336 return target_array2mem(interp,target, argc-1, argv + 1);
3338 static int target_array2mem(Jim_Interp *interp, struct target *target, int argc, Jim_Obj *const *argv)
3340 long l;
3341 uint32_t width;
3342 int len;
3343 uint32_t addr;
3344 uint32_t count;
3345 uint32_t v;
3346 const char *varname;
3347 uint8_t buffer[4096];
3348 int n, e, retval;
3349 uint32_t i;
3351 /* argv[1] = name of array to get the data
3352 * argv[2] = desired width
3353 * argv[3] = memory address
3354 * argv[4] = count to write
3356 if (argc != 4) {
3357 Jim_WrongNumArgs(interp, 1, argv, "varname width addr nelems");
3358 return JIM_ERR;
3360 varname = Jim_GetString(argv[0], &len);
3361 /* given "foo" get space for worse case "foo(%d)" .. add 20 */
3363 e = Jim_GetLong(interp, argv[1], &l);
3364 width = l;
3365 if (e != JIM_OK) {
3366 return e;
3369 e = Jim_GetLong(interp, argv[2], &l);
3370 addr = l;
3371 if (e != JIM_OK) {
3372 return e;
3374 e = Jim_GetLong(interp, argv[3], &l);
3375 len = l;
3376 if (e != JIM_OK) {
3377 return e;
3379 switch (width) {
3380 case 8:
3381 width = 1;
3382 break;
3383 case 16:
3384 width = 2;
3385 break;
3386 case 32:
3387 width = 4;
3388 break;
3389 default:
3390 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3391 Jim_AppendStrings(interp, Jim_GetResult(interp), "Invalid width param, must be 8/16/32", NULL);
3392 return JIM_ERR;
3394 if (len == 0) {
3395 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3396 Jim_AppendStrings(interp, Jim_GetResult(interp), "array2mem: zero width read?", NULL);
3397 return JIM_ERR;
3399 if ((addr + (len * width)) < addr) {
3400 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3401 Jim_AppendStrings(interp, Jim_GetResult(interp), "array2mem: addr + len - wraps to zero?", NULL);
3402 return JIM_ERR;
3404 /* absurd transfer size? */
3405 if (len > 65536) {
3406 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3407 Jim_AppendStrings(interp, Jim_GetResult(interp), "array2mem: absurd > 64K item request", NULL);
3408 return JIM_ERR;
3411 if ((width == 1) ||
3412 ((width == 2) && ((addr & 1) == 0)) ||
3413 ((width == 4) && ((addr & 3) == 0))) {
3414 /* all is well */
3415 } else {
3416 char buf[100];
3417 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3418 sprintf(buf, "array2mem address: 0x%08x is not aligned for %d byte reads",
3419 (unsigned int)addr,
3420 (int)width);
3421 Jim_AppendStrings(interp, Jim_GetResult(interp), buf , NULL);
3422 return JIM_ERR;
3425 /* Transfer loop */
3427 /* index counter */
3428 n = 0;
3429 /* assume ok */
3430 e = JIM_OK;
3431 while (len) {
3432 /* Slurp... in buffer size chunks */
3434 count = len; /* in objects.. */
3435 if (count > (sizeof(buffer)/width)) {
3436 count = (sizeof(buffer)/width);
3439 v = 0; /* shut up gcc */
3440 for (i = 0 ;i < count ;i++, n++) {
3441 get_int_array_element(interp, varname, n, &v);
3442 switch (width) {
3443 case 4:
3444 target_buffer_set_u32(target, &buffer[i*width], v);
3445 break;
3446 case 2:
3447 target_buffer_set_u16(target, &buffer[i*width], v);
3448 break;
3449 case 1:
3450 buffer[i] = v & 0x0ff;
3451 break;
3454 len -= count;
3456 retval = target_write_memory(target, addr, width, count, buffer);
3457 if (retval != ERROR_OK) {
3458 /* BOO !*/
3459 LOG_ERROR("array2mem: Write @ 0x%08x, w=%d, cnt=%d, failed",
3460 (unsigned int)addr,
3461 (int)width,
3462 (int)count);
3463 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3464 Jim_AppendStrings(interp, Jim_GetResult(interp), "array2mem: cannot read memory", NULL);
3465 e = JIM_ERR;
3466 len = 0;
3470 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3472 return JIM_OK;
3475 void target_all_handle_event(enum target_event e)
3477 struct target *target;
3479 LOG_DEBUG("**all*targets: event: %d, %s",
3480 (int)e,
3481 Jim_Nvp_value2name_simple(nvp_target_event, e)->name);
3483 target = all_targets;
3484 while (target) {
3485 target_handle_event(target, e);
3486 target = target->next;
3491 /* FIX? should we propagate errors here rather than printing them
3492 * and continuing?
3494 void target_handle_event(struct target *target, enum target_event e)
3496 struct target_event_action *teap;
3498 for (teap = target->event_action; teap != NULL; teap = teap->next) {
3499 if (teap->event == e) {
3500 LOG_DEBUG("target: (%d) %s (%s) event: %d (%s) action: %s",
3501 target->target_number,
3502 target->cmd_name,
3503 target_get_name(target),
3505 Jim_Nvp_value2name_simple(nvp_target_event, e)->name,
3506 Jim_GetString(teap->body, NULL));
3507 if (Jim_EvalObj(interp, teap->body) != JIM_OK)
3509 Jim_PrintErrorMessage(interp);
3515 enum target_cfg_param {
3516 TCFG_TYPE,
3517 TCFG_EVENT,
3518 TCFG_WORK_AREA_VIRT,
3519 TCFG_WORK_AREA_PHYS,
3520 TCFG_WORK_AREA_SIZE,
3521 TCFG_WORK_AREA_BACKUP,
3522 TCFG_ENDIAN,
3523 TCFG_VARIANT,
3524 TCFG_CHAIN_POSITION,
3527 static Jim_Nvp nvp_config_opts[] = {
3528 { .name = "-type", .value = TCFG_TYPE },
3529 { .name = "-event", .value = TCFG_EVENT },
3530 { .name = "-work-area-virt", .value = TCFG_WORK_AREA_VIRT },
3531 { .name = "-work-area-phys", .value = TCFG_WORK_AREA_PHYS },
3532 { .name = "-work-area-size", .value = TCFG_WORK_AREA_SIZE },
3533 { .name = "-work-area-backup", .value = TCFG_WORK_AREA_BACKUP },
3534 { .name = "-endian" , .value = TCFG_ENDIAN },
3535 { .name = "-variant", .value = TCFG_VARIANT },
3536 { .name = "-chain-position", .value = TCFG_CHAIN_POSITION },
3538 { .name = NULL, .value = -1 }
3541 static int target_configure(Jim_GetOptInfo *goi, struct target *target)
3543 Jim_Nvp *n;
3544 Jim_Obj *o;
3545 jim_wide w;
3546 char *cp;
3547 int e;
3549 /* parse config or cget options ... */
3550 while (goi->argc > 0) {
3551 Jim_SetEmptyResult(goi->interp);
3552 /* Jim_GetOpt_Debug(goi); */
3554 if (target->type->target_jim_configure) {
3555 /* target defines a configure function */
3556 /* target gets first dibs on parameters */
3557 e = (*(target->type->target_jim_configure))(target, goi);
3558 if (e == JIM_OK) {
3559 /* more? */
3560 continue;
3562 if (e == JIM_ERR) {
3563 /* An error */
3564 return e;
3566 /* otherwise we 'continue' below */
3568 e = Jim_GetOpt_Nvp(goi, nvp_config_opts, &n);
3569 if (e != JIM_OK) {
3570 Jim_GetOpt_NvpUnknown(goi, nvp_config_opts, 0);
3571 return e;
3573 switch (n->value) {
3574 case TCFG_TYPE:
3575 /* not setable */
3576 if (goi->isconfigure) {
3577 Jim_SetResult_sprintf(goi->interp, "not setable: %s", n->name);
3578 return JIM_ERR;
3579 } else {
3580 no_params:
3581 if (goi->argc != 0) {
3582 Jim_WrongNumArgs(goi->interp, goi->argc, goi->argv, "NO PARAMS");
3583 return JIM_ERR;
3586 Jim_SetResultString(goi->interp, target_get_name(target), -1);
3587 /* loop for more */
3588 break;
3589 case TCFG_EVENT:
3590 if (goi->argc == 0) {
3591 Jim_WrongNumArgs(goi->interp, goi->argc, goi->argv, "-event ?event-name? ...");
3592 return JIM_ERR;
3595 e = Jim_GetOpt_Nvp(goi, nvp_target_event, &n);
3596 if (e != JIM_OK) {
3597 Jim_GetOpt_NvpUnknown(goi, nvp_target_event, 1);
3598 return e;
3601 if (goi->isconfigure) {
3602 if (goi->argc != 1) {
3603 Jim_WrongNumArgs(goi->interp, goi->argc, goi->argv, "-event ?event-name? ?EVENT-BODY?");
3604 return JIM_ERR;
3606 } else {
3607 if (goi->argc != 0) {
3608 Jim_WrongNumArgs(goi->interp, goi->argc, goi->argv, "-event ?event-name?");
3609 return JIM_ERR;
3614 struct target_event_action *teap;
3616 teap = target->event_action;
3617 /* replace existing? */
3618 while (teap) {
3619 if (teap->event == (enum target_event)n->value) {
3620 break;
3622 teap = teap->next;
3625 if (goi->isconfigure) {
3626 bool replace = true;
3627 if (teap == NULL) {
3628 /* create new */
3629 teap = calloc(1, sizeof(*teap));
3630 replace = false;
3632 teap->event = n->value;
3633 Jim_GetOpt_Obj(goi, &o);
3634 if (teap->body) {
3635 Jim_DecrRefCount(interp, teap->body);
3637 teap->body = Jim_DuplicateObj(goi->interp, o);
3639 * FIXME:
3640 * Tcl/TK - "tk events" have a nice feature.
3641 * See the "BIND" command.
3642 * We should support that here.
3643 * You can specify %X and %Y in the event code.
3644 * The idea is: %T - target name.
3645 * The idea is: %N - target number
3646 * The idea is: %E - event name.
3648 Jim_IncrRefCount(teap->body);
3650 if (!replace)
3652 /* add to head of event list */
3653 teap->next = target->event_action;
3654 target->event_action = teap;
3656 Jim_SetEmptyResult(goi->interp);
3657 } else {
3658 /* get */
3659 if (teap == NULL) {
3660 Jim_SetEmptyResult(goi->interp);
3661 } else {
3662 Jim_SetResult(goi->interp, Jim_DuplicateObj(goi->interp, teap->body));
3666 /* loop for more */
3667 break;
3669 case TCFG_WORK_AREA_VIRT:
3670 if (goi->isconfigure) {
3671 target_free_all_working_areas(target);
3672 e = Jim_GetOpt_Wide(goi, &w);
3673 if (e != JIM_OK) {
3674 return e;
3676 target->working_area_virt = w;
3677 target->working_area_virt_spec = true;
3678 } else {
3679 if (goi->argc != 0) {
3680 goto no_params;
3683 Jim_SetResult(interp, Jim_NewIntObj(goi->interp, target->working_area_virt));
3684 /* loop for more */
3685 break;
3687 case TCFG_WORK_AREA_PHYS:
3688 if (goi->isconfigure) {
3689 target_free_all_working_areas(target);
3690 e = Jim_GetOpt_Wide(goi, &w);
3691 if (e != JIM_OK) {
3692 return e;
3694 target->working_area_phys = w;
3695 target->working_area_phys_spec = true;
3696 } else {
3697 if (goi->argc != 0) {
3698 goto no_params;
3701 Jim_SetResult(interp, Jim_NewIntObj(goi->interp, target->working_area_phys));
3702 /* loop for more */
3703 break;
3705 case TCFG_WORK_AREA_SIZE:
3706 if (goi->isconfigure) {
3707 target_free_all_working_areas(target);
3708 e = Jim_GetOpt_Wide(goi, &w);
3709 if (e != JIM_OK) {
3710 return e;
3712 target->working_area_size = w;
3713 } else {
3714 if (goi->argc != 0) {
3715 goto no_params;
3718 Jim_SetResult(interp, Jim_NewIntObj(goi->interp, target->working_area_size));
3719 /* loop for more */
3720 break;
3722 case TCFG_WORK_AREA_BACKUP:
3723 if (goi->isconfigure) {
3724 target_free_all_working_areas(target);
3725 e = Jim_GetOpt_Wide(goi, &w);
3726 if (e != JIM_OK) {
3727 return e;
3729 /* make this exactly 1 or 0 */
3730 target->backup_working_area = (!!w);
3731 } else {
3732 if (goi->argc != 0) {
3733 goto no_params;
3736 Jim_SetResult(interp, Jim_NewIntObj(goi->interp, target->backup_working_area));
3737 /* loop for more e*/
3738 break;
3740 case TCFG_ENDIAN:
3741 if (goi->isconfigure) {
3742 e = Jim_GetOpt_Nvp(goi, nvp_target_endian, &n);
3743 if (e != JIM_OK) {
3744 Jim_GetOpt_NvpUnknown(goi, nvp_target_endian, 1);
3745 return e;
3747 target->endianness = n->value;
3748 } else {
3749 if (goi->argc != 0) {
3750 goto no_params;
3753 n = Jim_Nvp_value2name_simple(nvp_target_endian, target->endianness);
3754 if (n->name == NULL) {
3755 target->endianness = TARGET_LITTLE_ENDIAN;
3756 n = Jim_Nvp_value2name_simple(nvp_target_endian, target->endianness);
3758 Jim_SetResultString(goi->interp, n->name, -1);
3759 /* loop for more */
3760 break;
3762 case TCFG_VARIANT:
3763 if (goi->isconfigure) {
3764 if (goi->argc < 1) {
3765 Jim_SetResult_sprintf(goi->interp,
3766 "%s ?STRING?",
3767 n->name);
3768 return JIM_ERR;
3770 if (target->variant) {
3771 free((void *)(target->variant));
3773 e = Jim_GetOpt_String(goi, &cp, NULL);
3774 target->variant = strdup(cp);
3775 } else {
3776 if (goi->argc != 0) {
3777 goto no_params;
3780 Jim_SetResultString(goi->interp, target->variant,-1);
3781 /* loop for more */
3782 break;
3783 case TCFG_CHAIN_POSITION:
3784 if (goi->isconfigure) {
3785 Jim_Obj *o;
3786 struct jtag_tap *tap;
3787 target_free_all_working_areas(target);
3788 e = Jim_GetOpt_Obj(goi, &o);
3789 if (e != JIM_OK) {
3790 return e;
3792 tap = jtag_tap_by_jim_obj(goi->interp, o);
3793 if (tap == NULL) {
3794 return JIM_ERR;
3796 /* make this exactly 1 or 0 */
3797 target->tap = tap;
3798 } else {
3799 if (goi->argc != 0) {
3800 goto no_params;
3803 Jim_SetResultString(interp, target->tap->dotted_name, -1);
3804 /* loop for more e*/
3805 break;
3807 } /* while (goi->argc) */
3810 /* done - we return */
3811 return JIM_OK;
3814 /** this is the 'tcl' handler for the target specific command */
3815 static int tcl_target_func(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
3817 Jim_GetOptInfo goi;
3818 jim_wide a,b,c;
3819 int x,y,z;
3820 uint8_t target_buf[32];
3821 Jim_Nvp *n;
3822 struct target *target;
3823 struct command_context *cmd_ctx;
3824 int e;
3826 enum {
3827 TS_CMD_CONFIGURE,
3828 TS_CMD_CGET,
3830 TS_CMD_MWW, TS_CMD_MWH, TS_CMD_MWB,
3831 TS_CMD_MDW, TS_CMD_MDH, TS_CMD_MDB,
3832 TS_CMD_MRW, TS_CMD_MRH, TS_CMD_MRB,
3833 TS_CMD_MEM2ARRAY, TS_CMD_ARRAY2MEM,
3834 TS_CMD_EXAMINE,
3835 TS_CMD_POLL,
3836 TS_CMD_RESET,
3837 TS_CMD_HALT,
3838 TS_CMD_WAITSTATE,
3839 TS_CMD_EVENTLIST,
3840 TS_CMD_CURSTATE,
3841 TS_CMD_INVOKE_EVENT,
3844 static const Jim_Nvp target_options[] = {
3845 { .name = "configure", .value = TS_CMD_CONFIGURE },
3846 { .name = "cget", .value = TS_CMD_CGET },
3847 { .name = "mww", .value = TS_CMD_MWW },
3848 { .name = "mwh", .value = TS_CMD_MWH },
3849 { .name = "mwb", .value = TS_CMD_MWB },
3850 { .name = "mdw", .value = TS_CMD_MDW },
3851 { .name = "mdh", .value = TS_CMD_MDH },
3852 { .name = "mdb", .value = TS_CMD_MDB },
3853 { .name = "mem2array", .value = TS_CMD_MEM2ARRAY },
3854 { .name = "array2mem", .value = TS_CMD_ARRAY2MEM },
3855 { .name = "eventlist", .value = TS_CMD_EVENTLIST },
3856 { .name = "curstate", .value = TS_CMD_CURSTATE },
3858 { .name = "arp_examine", .value = TS_CMD_EXAMINE },
3859 { .name = "arp_poll", .value = TS_CMD_POLL },
3860 { .name = "arp_reset", .value = TS_CMD_RESET },
3861 { .name = "arp_halt", .value = TS_CMD_HALT },
3862 { .name = "arp_waitstate", .value = TS_CMD_WAITSTATE },
3863 { .name = "invoke-event", .value = TS_CMD_INVOKE_EVENT },
3865 { .name = NULL, .value = -1 },
3868 /* go past the "command" */
3869 Jim_GetOpt_Setup(&goi, interp, argc-1, argv + 1);
3871 target = Jim_CmdPrivData(goi.interp);
3872 cmd_ctx = Jim_GetAssocData(goi.interp, "context");
3874 /* commands here are in an NVP table */
3875 e = Jim_GetOpt_Nvp(&goi, target_options, &n);
3876 if (e != JIM_OK) {
3877 Jim_GetOpt_NvpUnknown(&goi, target_options, 0);
3878 return e;
3880 /* Assume blank result */
3881 Jim_SetEmptyResult(goi.interp);
3883 switch (n->value) {
3884 case TS_CMD_CONFIGURE:
3885 if (goi.argc < 2) {
3886 Jim_WrongNumArgs(goi.interp, goi.argc, goi.argv, "missing: -option VALUE ...");
3887 return JIM_ERR;
3889 goi.isconfigure = 1;
3890 return target_configure(&goi, target);
3891 case TS_CMD_CGET:
3892 // some things take params
3893 if (goi.argc < 1) {
3894 Jim_WrongNumArgs(goi.interp, 0, goi.argv, "missing: ?-option?");
3895 return JIM_ERR;
3897 goi.isconfigure = 0;
3898 return target_configure(&goi, target);
3899 break;
3900 case TS_CMD_MWW:
3901 case TS_CMD_MWH:
3902 case TS_CMD_MWB:
3903 /* argv[0] = cmd
3904 * argv[1] = address
3905 * argv[2] = data
3906 * argv[3] = optional count.
3909 if ((goi.argc == 2) || (goi.argc == 3)) {
3910 /* all is well */
3911 } else {
3912 mwx_error:
3913 Jim_SetResult_sprintf(goi.interp, "expected: %s ADDR DATA [COUNT]", n->name);
3914 return JIM_ERR;
3917 e = Jim_GetOpt_Wide(&goi, &a);
3918 if (e != JIM_OK) {
3919 goto mwx_error;
3922 e = Jim_GetOpt_Wide(&goi, &b);
3923 if (e != JIM_OK) {
3924 goto mwx_error;
3926 if (goi.argc == 3) {
3927 e = Jim_GetOpt_Wide(&goi, &c);
3928 if (e != JIM_OK) {
3929 goto mwx_error;
3931 } else {
3932 c = 1;
3935 switch (n->value) {
3936 case TS_CMD_MWW:
3937 target_buffer_set_u32(target, target_buf, b);
3938 b = 4;
3939 break;
3940 case TS_CMD_MWH:
3941 target_buffer_set_u16(target, target_buf, b);
3942 b = 2;
3943 break;
3944 case TS_CMD_MWB:
3945 target_buffer_set_u8(target, target_buf, b);
3946 b = 1;
3947 break;
3949 for (x = 0 ; x < c ; x++) {
3950 e = target_write_memory(target, a, b, 1, target_buf);
3951 if (e != ERROR_OK) {
3952 Jim_SetResult_sprintf(interp, "Error writing @ 0x%08x: %d\n", (int)(a), e);
3953 return JIM_ERR;
3955 /* b = width */
3956 a = a + b;
3958 return JIM_OK;
3959 break;
3961 /* display */
3962 case TS_CMD_MDW:
3963 case TS_CMD_MDH:
3964 case TS_CMD_MDB:
3965 /* argv[0] = command
3966 * argv[1] = address
3967 * argv[2] = optional count
3969 if ((goi.argc == 2) || (goi.argc == 3)) {
3970 Jim_SetResult_sprintf(goi.interp, "expected: %s ADDR [COUNT]", n->name);
3971 return JIM_ERR;
3973 e = Jim_GetOpt_Wide(&goi, &a);
3974 if (e != JIM_OK) {
3975 return JIM_ERR;
3977 if (goi.argc) {
3978 e = Jim_GetOpt_Wide(&goi, &c);
3979 if (e != JIM_OK) {
3980 return JIM_ERR;
3982 } else {
3983 c = 1;
3985 b = 1; /* shut up gcc */
3986 switch (n->value) {
3987 case TS_CMD_MDW:
3988 b = 4;
3989 break;
3990 case TS_CMD_MDH:
3991 b = 2;
3992 break;
3993 case TS_CMD_MDB:
3994 b = 1;
3995 break;
3998 /* convert to "bytes" */
3999 c = c * b;
4000 /* count is now in 'BYTES' */
4001 while (c > 0) {
4002 y = c;
4003 if (y > 16) {
4004 y = 16;
4006 e = target_read_memory(target, a, b, y / b, target_buf);
4007 if (e != ERROR_OK) {
4008 Jim_SetResult_sprintf(interp, "error reading target @ 0x%08lx", (int)(a));
4009 return JIM_ERR;
4012 Jim_fprintf(interp, interp->cookie_stdout, "0x%08x ", (int)(a));
4013 switch (b) {
4014 case 4:
4015 for (x = 0 ; (x < 16) && (x < y) ; x += 4) {
4016 z = target_buffer_get_u32(target, &(target_buf[ x * 4 ]));
4017 Jim_fprintf(interp, interp->cookie_stdout, "%08x ", (int)(z));
4019 for (; (x < 16) ; x += 4) {
4020 Jim_fprintf(interp, interp->cookie_stdout, " ");
4022 break;
4023 case 2:
4024 for (x = 0 ; (x < 16) && (x < y) ; x += 2) {
4025 z = target_buffer_get_u16(target, &(target_buf[ x * 2 ]));
4026 Jim_fprintf(interp, interp->cookie_stdout, "%04x ", (int)(z));
4028 for (; (x < 16) ; x += 2) {
4029 Jim_fprintf(interp, interp->cookie_stdout, " ");
4031 break;
4032 case 1:
4033 default:
4034 for (x = 0 ; (x < 16) && (x < y) ; x += 1) {
4035 z = target_buffer_get_u8(target, &(target_buf[ x * 4 ]));
4036 Jim_fprintf(interp, interp->cookie_stdout, "%02x ", (int)(z));
4038 for (; (x < 16) ; x += 1) {
4039 Jim_fprintf(interp, interp->cookie_stdout, " ");
4041 break;
4043 /* ascii-ify the bytes */
4044 for (x = 0 ; x < y ; x++) {
4045 if ((target_buf[x] >= 0x20) &&
4046 (target_buf[x] <= 0x7e)) {
4047 /* good */
4048 } else {
4049 /* smack it */
4050 target_buf[x] = '.';
4053 /* space pad */
4054 while (x < 16) {
4055 target_buf[x] = ' ';
4056 x++;
4058 /* terminate */
4059 target_buf[16] = 0;
4060 /* print - with a newline */
4061 Jim_fprintf(interp, interp->cookie_stdout, "%s\n", target_buf);
4062 /* NEXT... */
4063 c -= 16;
4064 a += 16;
4066 return JIM_OK;
4067 case TS_CMD_MEM2ARRAY:
4068 return target_mem2array(goi.interp, target, goi.argc, goi.argv);
4069 break;
4070 case TS_CMD_ARRAY2MEM:
4071 return target_array2mem(goi.interp, target, goi.argc, goi.argv);
4072 break;
4073 case TS_CMD_EXAMINE:
4074 if (goi.argc) {
4075 Jim_WrongNumArgs(goi.interp, 2, argv, "[no parameters]");
4076 return JIM_ERR;
4078 if (!target->tap->enabled)
4079 goto err_tap_disabled;
4080 e = target->type->examine(target);
4081 if (e != ERROR_OK) {
4082 Jim_SetResult_sprintf(interp, "examine-fails: %d", e);
4083 return JIM_ERR;
4085 return JIM_OK;
4086 case TS_CMD_POLL:
4087 if (goi.argc) {
4088 Jim_WrongNumArgs(goi.interp, 2, argv, "[no parameters]");
4089 return JIM_ERR;
4091 if (!target->tap->enabled)
4092 goto err_tap_disabled;
4093 if (!(target_was_examined(target))) {
4094 e = ERROR_TARGET_NOT_EXAMINED;
4095 } else {
4096 e = target->type->poll(target);
4098 if (e != ERROR_OK) {
4099 Jim_SetResult_sprintf(interp, "poll-fails: %d", e);
4100 return JIM_ERR;
4101 } else {
4102 return JIM_OK;
4104 break;
4105 case TS_CMD_RESET:
4106 if (goi.argc != 2) {
4107 Jim_WrongNumArgs(interp, 2, argv,
4108 "([tT]|[fF]|assert|deassert) BOOL");
4109 return JIM_ERR;
4111 e = Jim_GetOpt_Nvp(&goi, nvp_assert, &n);
4112 if (e != JIM_OK) {
4113 Jim_GetOpt_NvpUnknown(&goi, nvp_assert, 1);
4114 return e;
4116 /* the halt or not param */
4117 e = Jim_GetOpt_Wide(&goi, &a);
4118 if (e != JIM_OK) {
4119 return e;
4121 if (!target->tap->enabled)
4122 goto err_tap_disabled;
4123 if (!target->type->assert_reset
4124 || !target->type->deassert_reset) {
4125 Jim_SetResult_sprintf(interp,
4126 "No target-specific reset for %s",
4127 target->cmd_name);
4128 return JIM_ERR;
4130 /* determine if we should halt or not. */
4131 target->reset_halt = !!a;
4132 /* When this happens - all workareas are invalid. */
4133 target_free_all_working_areas_restore(target, 0);
4135 /* do the assert */
4136 if (n->value == NVP_ASSERT) {
4137 e = target->type->assert_reset(target);
4138 } else {
4139 e = target->type->deassert_reset(target);
4141 return (e == ERROR_OK) ? JIM_OK : JIM_ERR;
4142 case TS_CMD_HALT:
4143 if (goi.argc) {
4144 Jim_WrongNumArgs(goi.interp, 0, argv, "halt [no parameters]");
4145 return JIM_ERR;
4147 if (!target->tap->enabled)
4148 goto err_tap_disabled;
4149 e = target->type->halt(target);
4150 return (e == ERROR_OK) ? JIM_OK : JIM_ERR;
4151 case TS_CMD_WAITSTATE:
4152 /* params: <name> statename timeoutmsecs */
4153 if (goi.argc != 2) {
4154 Jim_SetResult_sprintf(goi.interp, "%s STATENAME TIMEOUTMSECS", n->name);
4155 return JIM_ERR;
4157 e = Jim_GetOpt_Nvp(&goi, nvp_target_state, &n);
4158 if (e != JIM_OK) {
4159 Jim_GetOpt_NvpUnknown(&goi, nvp_target_state,1);
4160 return e;
4162 e = Jim_GetOpt_Wide(&goi, &a);
4163 if (e != JIM_OK) {
4164 return e;
4166 if (!target->tap->enabled)
4167 goto err_tap_disabled;
4168 e = target_wait_state(target, n->value, a);
4169 if (e != ERROR_OK) {
4170 Jim_SetResult_sprintf(goi.interp,
4171 "target: %s wait %s fails (%d) %s",
4172 target->cmd_name,
4173 n->name,
4174 e, target_strerror_safe(e));
4175 return JIM_ERR;
4176 } else {
4177 return JIM_OK;
4179 case TS_CMD_EVENTLIST:
4180 /* List for human, Events defined for this target.
4181 * scripts/programs should use 'name cget -event NAME'
4184 struct target_event_action *teap;
4185 teap = target->event_action;
4186 command_print(cmd_ctx, "Event actions for target (%d) %s\n",
4187 target->target_number,
4188 target->cmd_name);
4189 command_print(cmd_ctx, "%-25s | Body", "Event");
4190 command_print(cmd_ctx, "------------------------- | ----------------------------------------");
4191 while (teap) {
4192 command_print(cmd_ctx,
4193 "%-25s | %s",
4194 Jim_Nvp_value2name_simple(nvp_target_event, teap->event)->name,
4195 Jim_GetString(teap->body, NULL));
4196 teap = teap->next;
4198 command_print(cmd_ctx, "***END***");
4199 return JIM_OK;
4201 case TS_CMD_CURSTATE:
4202 if (goi.argc != 0) {
4203 Jim_WrongNumArgs(goi.interp, 0, argv, "[no parameters]");
4204 return JIM_ERR;
4206 Jim_SetResultString(goi.interp,
4207 target_state_name( target ),
4208 -1);
4209 return JIM_OK;
4210 case TS_CMD_INVOKE_EVENT:
4211 if (goi.argc != 1) {
4212 Jim_SetResult_sprintf(goi.interp, "%s ?EVENTNAME?",n->name);
4213 return JIM_ERR;
4215 e = Jim_GetOpt_Nvp(&goi, nvp_target_event, &n);
4216 if (e != JIM_OK) {
4217 Jim_GetOpt_NvpUnknown(&goi, nvp_target_event, 1);
4218 return e;
4220 target_handle_event(target, n->value);
4221 return JIM_OK;
4223 return JIM_ERR;
4225 err_tap_disabled:
4226 Jim_SetResult_sprintf(interp, "[TAP is disabled]");
4227 return JIM_ERR;
4230 static int target_create(Jim_GetOptInfo *goi)
4232 Jim_Obj *new_cmd;
4233 Jim_Cmd *cmd;
4234 const char *cp;
4235 char *cp2;
4236 int e;
4237 int x;
4238 struct target *target;
4239 struct command_context *cmd_ctx;
4241 cmd_ctx = Jim_GetAssocData(goi->interp, "context");
4242 if (goi->argc < 3) {
4243 Jim_WrongNumArgs(goi->interp, 1, goi->argv, "?name? ?type? ..options...");
4244 return JIM_ERR;
4247 /* COMMAND */
4248 Jim_GetOpt_Obj(goi, &new_cmd);
4249 /* does this command exist? */
4250 cmd = Jim_GetCommand(goi->interp, new_cmd, JIM_ERRMSG);
4251 if (cmd) {
4252 cp = Jim_GetString(new_cmd, NULL);
4253 Jim_SetResult_sprintf(goi->interp, "Command/target: %s Exists", cp);
4254 return JIM_ERR;
4257 /* TYPE */
4258 e = Jim_GetOpt_String(goi, &cp2, NULL);
4259 cp = cp2;
4260 /* now does target type exist */
4261 for (x = 0 ; target_types[x] ; x++) {
4262 if (0 == strcmp(cp, target_types[x]->name)) {
4263 /* found */
4264 break;
4267 if (target_types[x] == NULL) {
4268 Jim_SetResult_sprintf(goi->interp, "Unknown target type %s, try one of ", cp);
4269 for (x = 0 ; target_types[x] ; x++) {
4270 if (target_types[x + 1]) {
4271 Jim_AppendStrings(goi->interp,
4272 Jim_GetResult(goi->interp),
4273 target_types[x]->name,
4274 ", ", NULL);
4275 } else {
4276 Jim_AppendStrings(goi->interp,
4277 Jim_GetResult(goi->interp),
4278 " or ",
4279 target_types[x]->name,NULL);
4282 return JIM_ERR;
4285 /* Create it */
4286 target = calloc(1,sizeof(struct target));
4287 /* set target number */
4288 target->target_number = new_target_number();
4290 /* allocate memory for each unique target type */
4291 target->type = (struct target_type*)calloc(1,sizeof(struct target_type));
4293 memcpy(target->type, target_types[x], sizeof(struct target_type));
4295 /* will be set by "-endian" */
4296 target->endianness = TARGET_ENDIAN_UNKNOWN;
4298 target->working_area = 0x0;
4299 target->working_area_size = 0x0;
4300 target->working_areas = NULL;
4301 target->backup_working_area = 0;
4303 target->state = TARGET_UNKNOWN;
4304 target->debug_reason = DBG_REASON_UNDEFINED;
4305 target->reg_cache = NULL;
4306 target->breakpoints = NULL;
4307 target->watchpoints = NULL;
4308 target->next = NULL;
4309 target->arch_info = NULL;
4311 target->display = 1;
4313 target->halt_issued = false;
4315 /* initialize trace information */
4316 target->trace_info = malloc(sizeof(struct trace));
4317 target->trace_info->num_trace_points = 0;
4318 target->trace_info->trace_points_size = 0;
4319 target->trace_info->trace_points = NULL;
4320 target->trace_info->trace_history_size = 0;
4321 target->trace_info->trace_history = NULL;
4322 target->trace_info->trace_history_pos = 0;
4323 target->trace_info->trace_history_overflowed = 0;
4325 target->dbgmsg = NULL;
4326 target->dbg_msg_enabled = 0;
4328 target->endianness = TARGET_ENDIAN_UNKNOWN;
4330 /* Do the rest as "configure" options */
4331 goi->isconfigure = 1;
4332 e = target_configure(goi, target);
4334 if (target->tap == NULL)
4336 Jim_SetResultString(interp, "-chain-position required when creating target", -1);
4337 e = JIM_ERR;
4340 if (e != JIM_OK) {
4341 free(target->type);
4342 free(target);
4343 return e;
4346 if (target->endianness == TARGET_ENDIAN_UNKNOWN) {
4347 /* default endian to little if not specified */
4348 target->endianness = TARGET_LITTLE_ENDIAN;
4351 /* incase variant is not set */
4352 if (!target->variant)
4353 target->variant = strdup("");
4355 /* create the target specific commands */
4356 if (target->type->register_commands) {
4357 (*(target->type->register_commands))(cmd_ctx);
4359 if (target->type->target_create) {
4360 (*(target->type->target_create))(target, goi->interp);
4363 /* append to end of list */
4365 struct target **tpp;
4366 tpp = &(all_targets);
4367 while (*tpp) {
4368 tpp = &((*tpp)->next);
4370 *tpp = target;
4373 cp = Jim_GetString(new_cmd, NULL);
4374 target->cmd_name = strdup(cp);
4376 /* now - create the new target name command */
4377 e = Jim_CreateCommand(goi->interp,
4378 /* name */
4380 tcl_target_func, /* C function */
4381 target, /* private data */
4382 NULL); /* no del proc */
4384 return e;
4387 static int jim_target(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
4389 int x,r,e;
4390 jim_wide w;
4391 struct command_context *cmd_ctx;
4392 struct target *target;
4393 Jim_GetOptInfo goi;
4394 enum tcmd {
4395 /* TG = target generic */
4396 TG_CMD_CREATE,
4397 TG_CMD_TYPES,
4398 TG_CMD_NAMES,
4399 TG_CMD_CURRENT,
4400 TG_CMD_NUMBER,
4401 TG_CMD_COUNT,
4403 const char *target_cmds[] = {
4404 "create", "types", "names", "current", "number",
4405 "count",
4406 NULL /* terminate */
4409 LOG_DEBUG("Target command params:");
4410 LOG_DEBUG("%s", Jim_Debug_ArgvString(interp, argc, argv));
4412 cmd_ctx = Jim_GetAssocData(interp, "context");
4414 Jim_GetOpt_Setup(&goi, interp, argc-1, argv + 1);
4416 if (goi.argc == 0) {
4417 Jim_WrongNumArgs(interp, 1, argv, "missing: command ...");
4418 return JIM_ERR;
4421 /* Jim_GetOpt_Debug(&goi); */
4422 r = Jim_GetOpt_Enum(&goi, target_cmds, &x);
4423 if (r != JIM_OK) {
4424 return r;
4427 switch (x) {
4428 default:
4429 Jim_Panic(goi.interp,"Why am I here?");
4430 return JIM_ERR;
4431 case TG_CMD_CURRENT:
4432 if (goi.argc != 0) {
4433 Jim_WrongNumArgs(goi.interp, 1, goi.argv, "Too many parameters");
4434 return JIM_ERR;
4436 Jim_SetResultString(goi.interp, get_current_target(cmd_ctx)->cmd_name, -1);
4437 return JIM_OK;
4438 case TG_CMD_TYPES:
4439 if (goi.argc != 0) {
4440 Jim_WrongNumArgs(goi.interp, 1, goi.argv, "Too many parameters");
4441 return JIM_ERR;
4443 Jim_SetResult(goi.interp, Jim_NewListObj(goi.interp, NULL, 0));
4444 for (x = 0 ; target_types[x] ; x++) {
4445 Jim_ListAppendElement(goi.interp,
4446 Jim_GetResult(goi.interp),
4447 Jim_NewStringObj(goi.interp, target_types[x]->name, -1));
4449 return JIM_OK;
4450 case TG_CMD_NAMES:
4451 if (goi.argc != 0) {
4452 Jim_WrongNumArgs(goi.interp, 1, goi.argv, "Too many parameters");
4453 return JIM_ERR;
4455 Jim_SetResult(goi.interp, Jim_NewListObj(goi.interp, NULL, 0));
4456 target = all_targets;
4457 while (target) {
4458 Jim_ListAppendElement(goi.interp,
4459 Jim_GetResult(goi.interp),
4460 Jim_NewStringObj(goi.interp, target->cmd_name, -1));
4461 target = target->next;
4463 return JIM_OK;
4464 case TG_CMD_CREATE:
4465 if (goi.argc < 3) {
4466 Jim_WrongNumArgs(goi.interp, goi.argc, goi.argv, "?name ... config options ...");
4467 return JIM_ERR;
4469 return target_create(&goi);
4470 break;
4471 case TG_CMD_NUMBER:
4472 /* It's OK to remove this mechanism sometime after August 2010 or so */
4473 LOG_WARNING("don't use numbers as target identifiers; use names");
4474 if (goi.argc != 1) {
4475 Jim_SetResult_sprintf(goi.interp, "expected: target number ?NUMBER?");
4476 return JIM_ERR;
4478 e = Jim_GetOpt_Wide(&goi, &w);
4479 if (e != JIM_OK) {
4480 return JIM_ERR;
4482 for (x = 0, target = all_targets; target; target = target->next, x++) {
4483 if (target->target_number == w)
4484 break;
4486 if (target == NULL) {
4487 Jim_SetResult_sprintf(goi.interp,
4488 "Target: number %d does not exist", (int)(w));
4489 return JIM_ERR;
4491 Jim_SetResultString(goi.interp, target->cmd_name, -1);
4492 return JIM_OK;
4493 case TG_CMD_COUNT:
4494 if (goi.argc != 0) {
4495 Jim_WrongNumArgs(goi.interp, 0, goi.argv, "<no parameters>");
4496 return JIM_ERR;
4498 for (x = 0, target = all_targets; target; target = target->next, x++)
4499 continue;
4500 Jim_SetResult(goi.interp, Jim_NewIntObj(goi.interp, x));
4501 return JIM_OK;
4504 return JIM_ERR;
4508 struct FastLoad
4510 uint32_t address;
4511 uint8_t *data;
4512 int length;
4516 static int fastload_num;
4517 static struct FastLoad *fastload;
4519 static void free_fastload(void)
4521 if (fastload != NULL)
4523 int i;
4524 for (i = 0; i < fastload_num; i++)
4526 if (fastload[i].data)
4527 free(fastload[i].data);
4529 free(fastload);
4530 fastload = NULL;
4537 COMMAND_HANDLER(handle_fast_load_image_command)
4539 uint8_t *buffer;
4540 uint32_t buf_cnt;
4541 uint32_t image_size;
4542 uint32_t min_address = 0;
4543 uint32_t max_address = 0xffffffff;
4544 int i;
4546 struct image image;
4548 int retval = CALL_COMMAND_HANDLER(parse_load_image_command_args,
4549 &image, &min_address, &max_address);
4550 if (ERROR_OK != retval)
4551 return retval;
4553 struct duration bench;
4554 duration_start(&bench);
4556 if (image_open(&image, args[0], (argc >= 3) ? args[2] : NULL) != ERROR_OK)
4558 return ERROR_OK;
4561 image_size = 0x0;
4562 retval = ERROR_OK;
4563 fastload_num = image.num_sections;
4564 fastload = (struct FastLoad *)malloc(sizeof(struct FastLoad)*image.num_sections);
4565 if (fastload == NULL)
4567 image_close(&image);
4568 return ERROR_FAIL;
4570 memset(fastload, 0, sizeof(struct FastLoad)*image.num_sections);
4571 for (i = 0; i < image.num_sections; i++)
4573 buffer = malloc(image.sections[i].size);
4574 if (buffer == NULL)
4576 command_print(cmd_ctx, "error allocating buffer for section (%d bytes)",
4577 (int)(image.sections[i].size));
4578 break;
4581 if ((retval = image_read_section(&image, i, 0x0, image.sections[i].size, buffer, &buf_cnt)) != ERROR_OK)
4583 free(buffer);
4584 break;
4587 uint32_t offset = 0;
4588 uint32_t length = buf_cnt;
4591 /* DANGER!!! beware of unsigned comparision here!!! */
4593 if ((image.sections[i].base_address + buf_cnt >= min_address)&&
4594 (image.sections[i].base_address < max_address))
4596 if (image.sections[i].base_address < min_address)
4598 /* clip addresses below */
4599 offset += min_address-image.sections[i].base_address;
4600 length -= offset;
4603 if (image.sections[i].base_address + buf_cnt > max_address)
4605 length -= (image.sections[i].base_address + buf_cnt)-max_address;
4608 fastload[i].address = image.sections[i].base_address + offset;
4609 fastload[i].data = malloc(length);
4610 if (fastload[i].data == NULL)
4612 free(buffer);
4613 break;
4615 memcpy(fastload[i].data, buffer + offset, length);
4616 fastload[i].length = length;
4618 image_size += length;
4619 command_print(cmd_ctx, "%u bytes written at address 0x%8.8x",
4620 (unsigned int)length,
4621 ((unsigned int)(image.sections[i].base_address + offset)));
4624 free(buffer);
4627 if ((ERROR_OK == retval) && (duration_measure(&bench) == ERROR_OK))
4629 command_print(cmd_ctx, "Loaded %" PRIu32 " bytes "
4630 "in %fs (%0.3f kb/s)", image_size,
4631 duration_elapsed(&bench), duration_kbps(&bench, image_size));
4633 command_print(cmd_ctx,
4634 "WARNING: image has not been loaded to target!"
4635 "You can issue a 'fast_load' to finish loading.");
4638 image_close(&image);
4640 if (retval != ERROR_OK)
4642 free_fastload();
4645 return retval;
4648 COMMAND_HANDLER(handle_fast_load_command)
4650 if (argc > 0)
4651 return ERROR_COMMAND_SYNTAX_ERROR;
4652 if (fastload == NULL)
4654 LOG_ERROR("No image in memory");
4655 return ERROR_FAIL;
4657 int i;
4658 int ms = timeval_ms();
4659 int size = 0;
4660 int retval = ERROR_OK;
4661 for (i = 0; i < fastload_num;i++)
4663 struct target *target = get_current_target(cmd_ctx);
4664 command_print(cmd_ctx, "Write to 0x%08x, length 0x%08x",
4665 (unsigned int)(fastload[i].address),
4666 (unsigned int)(fastload[i].length));
4667 if (retval == ERROR_OK)
4669 retval = target_write_buffer(target, fastload[i].address, fastload[i].length, fastload[i].data);
4671 size += fastload[i].length;
4673 int after = timeval_ms();
4674 command_print(cmd_ctx, "Loaded image %f kBytes/s", (float)(size/1024.0)/((float)(after-ms)/1000.0));
4675 return retval;
4678 static int jim_mcrmrc(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
4680 struct command_context *context;
4681 struct target *target;
4682 int retval;
4684 context = Jim_GetAssocData(interp, "context");
4685 if (context == NULL) {
4686 LOG_ERROR("array2mem: no command context");
4687 return JIM_ERR;
4689 target = get_current_target(context);
4690 if (target == NULL) {
4691 LOG_ERROR("array2mem: no current target");
4692 return JIM_ERR;
4695 if ((argc < 6) || (argc > 7))
4697 return JIM_ERR;
4700 int cpnum;
4701 uint32_t op1;
4702 uint32_t op2;
4703 uint32_t CRn;
4704 uint32_t CRm;
4705 uint32_t value;
4707 int e;
4708 long l;
4709 e = Jim_GetLong(interp, argv[1], &l);
4710 if (e != JIM_OK) {
4711 return e;
4713 cpnum = l;
4715 e = Jim_GetLong(interp, argv[2], &l);
4716 if (e != JIM_OK) {
4717 return e;
4719 op1 = l;
4721 e = Jim_GetLong(interp, argv[3], &l);
4722 if (e != JIM_OK) {
4723 return e;
4725 CRn = l;
4727 e = Jim_GetLong(interp, argv[4], &l);
4728 if (e != JIM_OK) {
4729 return e;
4731 CRm = l;
4733 e = Jim_GetLong(interp, argv[5], &l);
4734 if (e != JIM_OK) {
4735 return e;
4737 op2 = l;
4739 value = 0;
4741 if (argc == 7)
4743 e = Jim_GetLong(interp, argv[6], &l);
4744 if (e != JIM_OK) {
4745 return e;
4747 value = l;
4749 retval = target_mcr(target, cpnum, op1, op2, CRn, CRm, value);
4750 if (retval != ERROR_OK)
4751 return JIM_ERR;
4752 } else
4754 retval = target_mrc(target, cpnum, op1, op2, CRn, CRm, &value);
4755 if (retval != ERROR_OK)
4756 return JIM_ERR;
4758 Jim_SetResult(interp, Jim_NewIntObj(interp, value));
4761 return JIM_OK;
4764 int target_register_commands(struct command_context *cmd_ctx)
4767 register_command(cmd_ctx, NULL, "targets",
4768 handle_targets_command, COMMAND_EXEC,
4769 "change current command line target (one parameter) "
4770 "or list targets (no parameters)");
4772 register_jim(cmd_ctx, "target", jim_target, "configure target");
4774 return ERROR_OK;
4777 int target_register_user_commands(struct command_context *cmd_ctx)
4779 int retval = ERROR_OK;
4780 if ((retval = target_request_register_commands(cmd_ctx)) != ERROR_OK)
4781 return retval;
4783 if ((retval = trace_register_commands(cmd_ctx)) != ERROR_OK)
4784 return retval;
4786 register_command(cmd_ctx, NULL, "profile",
4787 handle_profile_command, COMMAND_EXEC,
4788 "profiling samples the CPU PC");
4790 register_jim(cmd_ctx, "ocd_mem2array", jim_mem2array,
4791 "read memory and return as a TCL array for script processing "
4792 "<ARRAYNAME> <WIDTH = 32/16/8> <ADDRESS> <COUNT>");
4794 register_jim(cmd_ctx, "ocd_array2mem", jim_array2mem,
4795 "convert a TCL array to memory locations and write the values "
4796 "<ARRAYNAME> <WIDTH = 32/16/8> <ADDRESS> <COUNT>");
4798 register_command(cmd_ctx, NULL, "fast_load_image",
4799 handle_fast_load_image_command, COMMAND_ANY,
4800 "same args as load_image, image stored in memory "
4801 "- mainly for profiling purposes");
4803 register_command(cmd_ctx, NULL, "fast_load",
4804 handle_fast_load_command, COMMAND_ANY,
4805 "loads active fast load image to current target "
4806 "- mainly for profiling purposes");
4808 /** @todo don't register virt2phys() unless target supports it */
4809 register_command(cmd_ctx, NULL, "virt2phys",
4810 handle_virt2phys_command, COMMAND_ANY,
4811 "translate a virtual address into a physical address");
4813 register_command(cmd_ctx, NULL, "reg",
4814 handle_reg_command, COMMAND_EXEC,
4815 "display or set a register");
4817 register_command(cmd_ctx, NULL, "poll",
4818 handle_poll_command, COMMAND_EXEC,
4819 "poll target state");
4820 register_command(cmd_ctx, NULL, "wait_halt",
4821 handle_wait_halt_command, COMMAND_EXEC,
4822 "wait for target halt [time (s)]");
4823 register_command(cmd_ctx, NULL, "halt",
4824 handle_halt_command, COMMAND_EXEC,
4825 "halt target");
4826 register_command(cmd_ctx, NULL, "resume",
4827 handle_resume_command, COMMAND_EXEC,
4828 "resume target [addr]");
4829 register_command(cmd_ctx, NULL, "reset",
4830 handle_reset_command, COMMAND_EXEC,
4831 "reset target [run | halt | init] - default is run");
4832 register_command(cmd_ctx, NULL, "soft_reset_halt",
4833 handle_soft_reset_halt_command, COMMAND_EXEC,
4834 "halt the target and do a soft reset");
4836 register_command(cmd_ctx, NULL, "step",
4837 handle_step_command, COMMAND_EXEC,
4838 "step one instruction from current PC or [addr]");
4840 register_command(cmd_ctx, NULL, "mdw",
4841 handle_md_command, COMMAND_EXEC,
4842 "display memory words [phys] <addr> [count]");
4843 register_command(cmd_ctx, NULL, "mdh",
4844 handle_md_command, COMMAND_EXEC,
4845 "display memory half-words [phys] <addr> [count]");
4846 register_command(cmd_ctx, NULL, "mdb",
4847 handle_md_command, COMMAND_EXEC,
4848 "display memory bytes [phys] <addr> [count]");
4850 register_command(cmd_ctx, NULL, "mww",
4851 handle_mw_command, COMMAND_EXEC,
4852 "write memory word [phys] <addr> <value> [count]");
4853 register_command(cmd_ctx, NULL, "mwh",
4854 handle_mw_command, COMMAND_EXEC,
4855 "write memory half-word [phys] <addr> <value> [count]");
4856 register_command(cmd_ctx, NULL, "mwb",
4857 handle_mw_command, COMMAND_EXEC,
4858 "write memory byte [phys] <addr> <value> [count]");
4860 register_command(cmd_ctx, NULL, "bp",
4861 handle_bp_command, COMMAND_EXEC,
4862 "list or set breakpoint [<address> <length> [hw]]");
4863 register_command(cmd_ctx, NULL, "rbp",
4864 handle_rbp_command, COMMAND_EXEC,
4865 "remove breakpoint <address>");
4867 register_command(cmd_ctx, NULL, "wp",
4868 handle_wp_command, COMMAND_EXEC,
4869 "list or set watchpoint "
4870 "[<address> <length> <r/w/a> [value] [mask]]");
4871 register_command(cmd_ctx, NULL, "rwp",
4872 handle_rwp_command, COMMAND_EXEC,
4873 "remove watchpoint <address>");
4875 register_command(cmd_ctx, NULL, "load_image",
4876 handle_load_image_command, COMMAND_EXEC,
4877 "load_image <file> <address> "
4878 "['bin'|'ihex'|'elf'|'s19'] [min_address] [max_length]");
4879 register_command(cmd_ctx, NULL, "dump_image",
4880 handle_dump_image_command, COMMAND_EXEC,
4881 "dump_image <file> <address> <size>");
4882 register_command(cmd_ctx, NULL, "verify_image",
4883 handle_verify_image_command, COMMAND_EXEC,
4884 "verify_image <file> [offset] [type]");
4885 register_command(cmd_ctx, NULL, "test_image",
4886 handle_test_image_command, COMMAND_EXEC,
4887 "test_image <file> [offset] [type]");
4889 return ERROR_OK;