target: create and use target_name()
[openocd/cmsis-dap.git] / src / target / target.c
blob3de9f2c6a3adbea8492b6d837eb62a3b9661490f
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;
68 extern struct target_type testee_target;
70 struct target_type *target_types[] =
72 &arm7tdmi_target,
73 &arm9tdmi_target,
74 &arm920t_target,
75 &arm720t_target,
76 &arm966e_target,
77 &arm926ejs_target,
78 &fa526_target,
79 &feroceon_target,
80 &dragonite_target,
81 &xscale_target,
82 &cortexm3_target,
83 &cortexa8_target,
84 &arm11_target,
85 &mips_m4k_target,
86 &avr_target,
87 &testee_target,
88 NULL,
91 struct target *all_targets = NULL;
92 struct target_event_callback *target_event_callbacks = NULL;
93 struct target_timer_callback *target_timer_callbacks = NULL;
95 const Jim_Nvp nvp_assert[] = {
96 { .name = "assert", NVP_ASSERT },
97 { .name = "deassert", NVP_DEASSERT },
98 { .name = "T", NVP_ASSERT },
99 { .name = "F", NVP_DEASSERT },
100 { .name = "t", NVP_ASSERT },
101 { .name = "f", NVP_DEASSERT },
102 { .name = NULL, .value = -1 }
105 const Jim_Nvp nvp_error_target[] = {
106 { .value = ERROR_TARGET_INVALID, .name = "err-invalid" },
107 { .value = ERROR_TARGET_INIT_FAILED, .name = "err-init-failed" },
108 { .value = ERROR_TARGET_TIMEOUT, .name = "err-timeout" },
109 { .value = ERROR_TARGET_NOT_HALTED, .name = "err-not-halted" },
110 { .value = ERROR_TARGET_FAILURE, .name = "err-failure" },
111 { .value = ERROR_TARGET_UNALIGNED_ACCESS , .name = "err-unaligned-access" },
112 { .value = ERROR_TARGET_DATA_ABORT , .name = "err-data-abort" },
113 { .value = ERROR_TARGET_RESOURCE_NOT_AVAILABLE , .name = "err-resource-not-available" },
114 { .value = ERROR_TARGET_TRANSLATION_FAULT , .name = "err-translation-fault" },
115 { .value = ERROR_TARGET_NOT_RUNNING, .name = "err-not-running" },
116 { .value = ERROR_TARGET_NOT_EXAMINED, .name = "err-not-examined" },
117 { .value = -1, .name = NULL }
120 const char *target_strerror_safe(int err)
122 const Jim_Nvp *n;
124 n = Jim_Nvp_value2name_simple(nvp_error_target, err);
125 if (n->name == NULL) {
126 return "unknown";
127 } else {
128 return n->name;
132 static const Jim_Nvp nvp_target_event[] = {
133 { .value = TARGET_EVENT_OLD_gdb_program_config , .name = "old-gdb_program_config" },
134 { .value = TARGET_EVENT_OLD_pre_resume , .name = "old-pre_resume" },
136 { .value = TARGET_EVENT_GDB_HALT, .name = "gdb-halt" },
137 { .value = TARGET_EVENT_HALTED, .name = "halted" },
138 { .value = TARGET_EVENT_RESUMED, .name = "resumed" },
139 { .value = TARGET_EVENT_RESUME_START, .name = "resume-start" },
140 { .value = TARGET_EVENT_RESUME_END, .name = "resume-end" },
142 { .name = "gdb-start", .value = TARGET_EVENT_GDB_START },
143 { .name = "gdb-end", .value = TARGET_EVENT_GDB_END },
145 /* historical name */
147 { .value = TARGET_EVENT_RESET_START, .name = "reset-start" },
149 { .value = TARGET_EVENT_RESET_ASSERT_PRE, .name = "reset-assert-pre" },
150 { .value = TARGET_EVENT_RESET_ASSERT_POST, .name = "reset-assert-post" },
151 { .value = TARGET_EVENT_RESET_DEASSERT_PRE, .name = "reset-deassert-pre" },
152 { .value = TARGET_EVENT_RESET_DEASSERT_POST, .name = "reset-deassert-post" },
153 { .value = TARGET_EVENT_RESET_HALT_PRE, .name = "reset-halt-pre" },
154 { .value = TARGET_EVENT_RESET_HALT_POST, .name = "reset-halt-post" },
155 { .value = TARGET_EVENT_RESET_WAIT_PRE, .name = "reset-wait-pre" },
156 { .value = TARGET_EVENT_RESET_WAIT_POST, .name = "reset-wait-post" },
157 { .value = TARGET_EVENT_RESET_INIT , .name = "reset-init" },
158 { .value = TARGET_EVENT_RESET_END, .name = "reset-end" },
160 { .value = TARGET_EVENT_EXAMINE_START, .name = "examine-start" },
161 { .value = TARGET_EVENT_EXAMINE_END, .name = "examine-end" },
163 { .value = TARGET_EVENT_DEBUG_HALTED, .name = "debug-halted" },
164 { .value = TARGET_EVENT_DEBUG_RESUMED, .name = "debug-resumed" },
166 { .value = TARGET_EVENT_GDB_ATTACH, .name = "gdb-attach" },
167 { .value = TARGET_EVENT_GDB_DETACH, .name = "gdb-detach" },
169 { .value = TARGET_EVENT_GDB_FLASH_WRITE_START, .name = "gdb-flash-write-start" },
170 { .value = TARGET_EVENT_GDB_FLASH_WRITE_END , .name = "gdb-flash-write-end" },
172 { .value = TARGET_EVENT_GDB_FLASH_ERASE_START, .name = "gdb-flash-erase-start" },
173 { .value = TARGET_EVENT_GDB_FLASH_ERASE_END , .name = "gdb-flash-erase-end" },
175 { .value = TARGET_EVENT_RESUME_START, .name = "resume-start" },
176 { .value = TARGET_EVENT_RESUMED , .name = "resume-ok" },
177 { .value = TARGET_EVENT_RESUME_END , .name = "resume-end" },
179 { .name = NULL, .value = -1 }
182 const Jim_Nvp nvp_target_state[] = {
183 { .name = "unknown", .value = TARGET_UNKNOWN },
184 { .name = "running", .value = TARGET_RUNNING },
185 { .name = "halted", .value = TARGET_HALTED },
186 { .name = "reset", .value = TARGET_RESET },
187 { .name = "debug-running", .value = TARGET_DEBUG_RUNNING },
188 { .name = NULL, .value = -1 },
191 const Jim_Nvp nvp_target_debug_reason [] = {
192 { .name = "debug-request" , .value = DBG_REASON_DBGRQ },
193 { .name = "breakpoint" , .value = DBG_REASON_BREAKPOINT },
194 { .name = "watchpoint" , .value = DBG_REASON_WATCHPOINT },
195 { .name = "watchpoint-and-breakpoint", .value = DBG_REASON_WPTANDBKPT },
196 { .name = "single-step" , .value = DBG_REASON_SINGLESTEP },
197 { .name = "target-not-halted" , .value = DBG_REASON_NOTHALTED },
198 { .name = "undefined" , .value = DBG_REASON_UNDEFINED },
199 { .name = NULL, .value = -1 },
202 const Jim_Nvp nvp_target_endian[] = {
203 { .name = "big", .value = TARGET_BIG_ENDIAN },
204 { .name = "little", .value = TARGET_LITTLE_ENDIAN },
205 { .name = "be", .value = TARGET_BIG_ENDIAN },
206 { .name = "le", .value = TARGET_LITTLE_ENDIAN },
207 { .name = NULL, .value = -1 },
210 const Jim_Nvp nvp_reset_modes[] = {
211 { .name = "unknown", .value = RESET_UNKNOWN },
212 { .name = "run" , .value = RESET_RUN },
213 { .name = "halt" , .value = RESET_HALT },
214 { .name = "init" , .value = RESET_INIT },
215 { .name = NULL , .value = -1 },
218 const char *
219 target_state_name( struct target *t )
221 const char *cp;
222 cp = Jim_Nvp_value2name_simple(nvp_target_state, t->state)->name;
223 if( !cp ){
224 LOG_ERROR("Invalid target state: %d", (int)(t->state));
225 cp = "(*BUG*unknown*BUG*)";
227 return cp;
230 /* determine the number of the new target */
231 static int new_target_number(void)
233 struct target *t;
234 int x;
236 /* number is 0 based */
237 x = -1;
238 t = all_targets;
239 while (t) {
240 if (x < t->target_number) {
241 x = t->target_number;
243 t = t->next;
245 return x + 1;
248 /* read a uint32_t from a buffer in target memory endianness */
249 uint32_t target_buffer_get_u32(struct target *target, const uint8_t *buffer)
251 if (target->endianness == TARGET_LITTLE_ENDIAN)
252 return le_to_h_u32(buffer);
253 else
254 return be_to_h_u32(buffer);
257 /* read a uint16_t from a buffer in target memory endianness */
258 uint16_t target_buffer_get_u16(struct target *target, const uint8_t *buffer)
260 if (target->endianness == TARGET_LITTLE_ENDIAN)
261 return le_to_h_u16(buffer);
262 else
263 return be_to_h_u16(buffer);
266 /* read a uint8_t from a buffer in target memory endianness */
267 uint8_t target_buffer_get_u8(struct target *target, const uint8_t *buffer)
269 return *buffer & 0x0ff;
272 /* write a uint32_t to a buffer in target memory endianness */
273 void target_buffer_set_u32(struct target *target, uint8_t *buffer, uint32_t value)
275 if (target->endianness == TARGET_LITTLE_ENDIAN)
276 h_u32_to_le(buffer, value);
277 else
278 h_u32_to_be(buffer, value);
281 /* write a uint16_t to a buffer in target memory endianness */
282 void target_buffer_set_u16(struct target *target, uint8_t *buffer, uint16_t value)
284 if (target->endianness == TARGET_LITTLE_ENDIAN)
285 h_u16_to_le(buffer, value);
286 else
287 h_u16_to_be(buffer, value);
290 /* write a uint8_t to a buffer in target memory endianness */
291 void target_buffer_set_u8(struct target *target, uint8_t *buffer, uint8_t value)
293 *buffer = value;
296 /* return a pointer to a configured target; id is name or number */
297 struct target *get_target(const char *id)
299 struct target *target;
301 /* try as tcltarget name */
302 for (target = all_targets; target; target = target->next) {
303 if (target->cmd_name == NULL)
304 continue;
305 if (strcmp(id, target->cmd_name) == 0)
306 return target;
309 /* It's OK to remove this fallback sometime after August 2010 or so */
311 /* no match, try as number */
312 unsigned num;
313 if (parse_uint(id, &num) != ERROR_OK)
314 return NULL;
316 for (target = all_targets; target; target = target->next) {
317 if (target->target_number == (int)num) {
318 LOG_WARNING("use '%s' as target identifier, not '%u'",
319 target->cmd_name, num);
320 return target;
324 return NULL;
327 /* returns a pointer to the n-th configured target */
328 static struct target *get_target_by_num(int num)
330 struct target *target = all_targets;
332 while (target) {
333 if (target->target_number == num) {
334 return target;
336 target = target->next;
339 return NULL;
342 struct target* get_current_target(struct command_context *cmd_ctx)
344 struct target *target = get_target_by_num(cmd_ctx->current_target);
346 if (target == NULL)
348 LOG_ERROR("BUG: current_target out of bounds");
349 exit(-1);
352 return target;
355 int target_poll(struct target *target)
357 int retval;
359 /* We can't poll until after examine */
360 if (!target_was_examined(target))
362 /* Fail silently lest we pollute the log */
363 return ERROR_FAIL;
366 retval = target->type->poll(target);
367 if (retval != ERROR_OK)
368 return retval;
370 if (target->halt_issued)
372 if (target->state == TARGET_HALTED)
374 target->halt_issued = false;
375 } else
377 long long t = timeval_ms() - target->halt_issued_time;
378 if (t>1000)
380 target->halt_issued = false;
381 LOG_INFO("Halt timed out, wake up GDB.");
382 target_call_event_callbacks(target, TARGET_EVENT_GDB_HALT);
387 return ERROR_OK;
390 int target_halt(struct target *target)
392 int retval;
393 /* We can't poll until after examine */
394 if (!target_was_examined(target))
396 LOG_ERROR("Target not examined yet");
397 return ERROR_FAIL;
400 retval = target->type->halt(target);
401 if (retval != ERROR_OK)
402 return retval;
404 target->halt_issued = true;
405 target->halt_issued_time = timeval_ms();
407 return ERROR_OK;
410 int target_resume(struct target *target, int current, uint32_t address, int handle_breakpoints, int debug_execution)
412 int retval;
414 /* We can't poll until after examine */
415 if (!target_was_examined(target))
417 LOG_ERROR("Target not examined yet");
418 return ERROR_FAIL;
421 /* note that resume *must* be asynchronous. The CPU can halt before we poll. The CPU can
422 * even halt at the current PC as a result of a software breakpoint being inserted by (a bug?)
423 * the application.
425 if ((retval = target->type->resume(target, current, address, handle_breakpoints, debug_execution)) != ERROR_OK)
426 return retval;
428 return retval;
431 int target_process_reset(struct command_context *cmd_ctx, enum target_reset_mode reset_mode)
433 char buf[100];
434 int retval;
435 Jim_Nvp *n;
436 n = Jim_Nvp_value2name_simple(nvp_reset_modes, reset_mode);
437 if (n->name == NULL) {
438 LOG_ERROR("invalid reset mode");
439 return ERROR_FAIL;
442 /* disable polling during reset to make reset event scripts
443 * more predictable, i.e. dr/irscan & pathmove in events will
444 * not have JTAG operations injected into the middle of a sequence.
446 bool save_poll = jtag_poll_get_enabled();
448 jtag_poll_set_enabled(false);
450 sprintf(buf, "ocd_process_reset %s", n->name);
451 retval = Jim_Eval(interp, buf);
453 jtag_poll_set_enabled(save_poll);
455 if (retval != JIM_OK) {
456 Jim_PrintErrorMessage(interp);
457 return ERROR_FAIL;
460 /* We want any events to be processed before the prompt */
461 retval = target_call_timer_callbacks_now();
463 return retval;
466 static int identity_virt2phys(struct target *target,
467 uint32_t virtual, uint32_t *physical)
469 *physical = virtual;
470 return ERROR_OK;
473 static int no_mmu(struct target *target, int *enabled)
475 *enabled = 0;
476 return ERROR_OK;
479 static int default_examine(struct target *target)
481 target_set_examined(target);
482 return ERROR_OK;
485 int target_examine_one(struct target *target)
487 return target->type->examine(target);
490 static int jtag_enable_callback(enum jtag_event event, void *priv)
492 struct target *target = priv;
494 if (event != JTAG_TAP_EVENT_ENABLE || !target->tap->enabled)
495 return ERROR_OK;
497 jtag_unregister_event_callback(jtag_enable_callback, target);
498 return target_examine_one(target);
502 /* Targets that correctly implement init + examine, i.e.
503 * no communication with target during init:
505 * XScale
507 int target_examine(void)
509 int retval = ERROR_OK;
510 struct target *target;
512 for (target = all_targets; target; target = target->next)
514 /* defer examination, but don't skip it */
515 if (!target->tap->enabled) {
516 jtag_register_event_callback(jtag_enable_callback,
517 target);
518 continue;
520 if ((retval = target_examine_one(target)) != ERROR_OK)
521 return retval;
523 return retval;
525 const char *target_type_name(struct target *target)
527 return target->type->name;
530 static int target_write_memory_imp(struct target *target, uint32_t address, uint32_t size, uint32_t count, uint8_t *buffer)
532 if (!target_was_examined(target))
534 LOG_ERROR("Target not examined yet");
535 return ERROR_FAIL;
537 return target->type->write_memory_imp(target, address, size, count, buffer);
540 static int target_read_memory_imp(struct target *target, uint32_t address, uint32_t size, uint32_t count, uint8_t *buffer)
542 if (!target_was_examined(target))
544 LOG_ERROR("Target not examined yet");
545 return ERROR_FAIL;
547 return target->type->read_memory_imp(target, address, size, count, buffer);
550 static int target_soft_reset_halt_imp(struct target *target)
552 if (!target_was_examined(target))
554 LOG_ERROR("Target not examined yet");
555 return ERROR_FAIL;
557 if (!target->type->soft_reset_halt_imp) {
558 LOG_ERROR("Target %s does not support soft_reset_halt",
559 target_name(target));
560 return ERROR_FAIL;
562 return target->type->soft_reset_halt_imp(target);
565 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)
567 if (!target_was_examined(target))
569 LOG_ERROR("Target not examined yet");
570 return ERROR_FAIL;
572 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);
575 int target_read_memory(struct target *target,
576 uint32_t address, uint32_t size, uint32_t count, uint8_t *buffer)
578 return target->type->read_memory(target, address, size, count, buffer);
581 int target_read_phys_memory(struct target *target,
582 uint32_t address, uint32_t size, uint32_t count, uint8_t *buffer)
584 return target->type->read_phys_memory(target, address, size, count, buffer);
587 int target_write_memory(struct target *target,
588 uint32_t address, uint32_t size, uint32_t count, uint8_t *buffer)
590 return target->type->write_memory(target, address, size, count, buffer);
593 int target_write_phys_memory(struct target *target,
594 uint32_t address, uint32_t size, uint32_t count, uint8_t *buffer)
596 return target->type->write_phys_memory(target, address, size, count, buffer);
599 int target_bulk_write_memory(struct target *target,
600 uint32_t address, uint32_t count, uint8_t *buffer)
602 return target->type->bulk_write_memory(target, address, count, buffer);
605 int target_add_breakpoint(struct target *target,
606 struct breakpoint *breakpoint)
608 return target->type->add_breakpoint(target, breakpoint);
610 int target_remove_breakpoint(struct target *target,
611 struct breakpoint *breakpoint)
613 return target->type->remove_breakpoint(target, breakpoint);
616 int target_add_watchpoint(struct target *target,
617 struct watchpoint *watchpoint)
619 return target->type->add_watchpoint(target, watchpoint);
621 int target_remove_watchpoint(struct target *target,
622 struct watchpoint *watchpoint)
624 return target->type->remove_watchpoint(target, watchpoint);
627 int target_get_gdb_reg_list(struct target *target,
628 struct reg **reg_list[], int *reg_list_size)
630 return target->type->get_gdb_reg_list(target, reg_list, reg_list_size);
632 int target_step(struct target *target,
633 int current, uint32_t address, int handle_breakpoints)
635 return target->type->step(target, current, address, handle_breakpoints);
639 int target_run_algorithm(struct target *target,
640 int num_mem_params, struct mem_param *mem_params,
641 int num_reg_params, struct reg_param *reg_param,
642 uint32_t entry_point, uint32_t exit_point,
643 int timeout_ms, void *arch_info)
645 return target->type->run_algorithm(target,
646 num_mem_params, mem_params, num_reg_params, reg_param,
647 entry_point, exit_point, timeout_ms, arch_info);
651 * Reset the @c examined flag for the given target.
652 * Pure paranoia -- targets are zeroed on allocation.
654 static void target_reset_examined(struct target *target)
656 target->examined = false;
661 static int default_mrc(struct target *target, int cpnum, uint32_t op1, uint32_t op2, uint32_t CRn, uint32_t CRm, uint32_t *value)
663 LOG_ERROR("Not implemented: %s", __func__);
664 return ERROR_FAIL;
667 static int default_mcr(struct target *target, int cpnum, uint32_t op1, uint32_t op2, uint32_t CRn, uint32_t CRm, uint32_t value)
669 LOG_ERROR("Not implemented: %s", __func__);
670 return ERROR_FAIL;
673 static int arm_cp_check(struct target *target, int cpnum, uint32_t op1, uint32_t op2, uint32_t CRn, uint32_t CRm)
675 /* basic check */
676 if (!target_was_examined(target))
678 LOG_ERROR("Target not examined yet");
679 return ERROR_FAIL;
682 if ((cpnum <0) || (cpnum > 15))
684 LOG_ERROR("Illegal co-processor %d", cpnum);
685 return ERROR_FAIL;
688 if (op1 > 7)
690 LOG_ERROR("Illegal op1");
691 return ERROR_FAIL;
694 if (op2 > 7)
696 LOG_ERROR("Illegal op2");
697 return ERROR_FAIL;
700 if (CRn > 15)
702 LOG_ERROR("Illegal CRn");
703 return ERROR_FAIL;
706 if (CRm > 15)
708 LOG_ERROR("Illegal CRm");
709 return ERROR_FAIL;
712 return ERROR_OK;
715 int target_mrc(struct target *target, int cpnum, uint32_t op1, uint32_t op2, uint32_t CRn, uint32_t CRm, uint32_t *value)
717 int retval;
719 retval = arm_cp_check(target, cpnum, op1, op2, CRn, CRm);
720 if (retval != ERROR_OK)
721 return retval;
723 return target->type->mrc(target, cpnum, op1, op2, CRn, CRm, value);
726 int target_mcr(struct target *target, int cpnum, uint32_t op1, uint32_t op2, uint32_t CRn, uint32_t CRm, uint32_t value)
728 int retval;
730 retval = arm_cp_check(target, cpnum, op1, op2, CRn, CRm);
731 if (retval != ERROR_OK)
732 return retval;
734 return target->type->mcr(target, cpnum, op1, op2, CRn, CRm, value);
737 static int
738 err_read_phys_memory(struct target *target, uint32_t address,
739 uint32_t size, uint32_t count, uint8_t *buffer)
741 LOG_ERROR("Not implemented: %s", __func__);
742 return ERROR_FAIL;
745 static int
746 err_write_phys_memory(struct target *target, uint32_t address,
747 uint32_t size, uint32_t count, uint8_t *buffer)
749 LOG_ERROR("Not implemented: %s", __func__);
750 return ERROR_FAIL;
753 int target_init(struct command_context *cmd_ctx)
755 struct target *target;
756 int retval;
758 for (target = all_targets; target; target = target->next) {
759 struct target_type *type = target->type;
761 target_reset_examined(target);
762 if (target->type->examine == NULL)
764 target->type->examine = default_examine;
767 if ((retval = target->type->init_target(cmd_ctx, target)) != ERROR_OK)
769 LOG_ERROR("target '%s' init failed", target_name(target));
770 return retval;
774 * @todo MCR/MRC are ARM-specific; don't require them in
775 * all targets, or for ARMs without coprocessors.
777 if (target->type->mcr == NULL)
779 target->type->mcr = default_mcr;
780 } else
782 const struct command_registration mcr_cmd = {
783 .name = "mcr",
784 .mode = COMMAND_EXEC,
785 .jim_handler = &jim_mcrmrc,
786 .help = "write coprocessor",
787 .usage = "<cpnum> <op1> <op2> <CRn> <CRm> <value>",
789 register_command(cmd_ctx, NULL, &mcr_cmd);
792 if (target->type->mrc == NULL)
794 target->type->mrc = default_mrc;
795 } else
797 const struct command_registration mrc_cmd = {
798 .name = "mrc",
799 .jim_handler = &jim_mcrmrc,
800 .help = "read coprocessor",
801 .usage = "<cpnum> <op1> <op2> <CRn> <CRm>",
803 register_command(cmd_ctx, NULL, &mrc_cmd);
808 * @todo get rid of those *memory_imp() methods, now that all
809 * callers are using target_*_memory() accessors ... and make
810 * sure the "physical" paths handle the same issues.
813 /* a non-invasive way(in terms of patches) to add some code that
814 * runs before the type->write/read_memory implementation
816 target->type->write_memory_imp = target->type->write_memory;
817 target->type->write_memory = target_write_memory_imp;
818 target->type->read_memory_imp = target->type->read_memory;
819 target->type->read_memory = target_read_memory_imp;
820 target->type->soft_reset_halt_imp = target->type->soft_reset_halt;
821 target->type->soft_reset_halt = target_soft_reset_halt_imp;
822 target->type->run_algorithm_imp = target->type->run_algorithm;
823 target->type->run_algorithm = target_run_algorithm_imp;
825 /* Sanity-check MMU support ... stub in what we must, to help
826 * implement it in stages, but warn if we need to do so.
828 if (type->mmu) {
829 if (type->write_phys_memory == NULL) {
830 LOG_ERROR("type '%s' is missing %s",
831 type->name,
832 "write_phys_memory");
833 type->write_phys_memory = err_write_phys_memory;
835 if (type->read_phys_memory == NULL) {
836 LOG_ERROR("type '%s' is missing %s",
837 type->name,
838 "read_phys_memory");
839 type->read_phys_memory = err_read_phys_memory;
841 if (type->virt2phys == NULL) {
842 LOG_ERROR("type '%s' is missing %s",
843 type->name,
844 "virt2phys");
845 type->virt2phys = identity_virt2phys;
848 /* Make sure no-MMU targets all behave the same: make no
849 * distinction between physical and virtual addresses, and
850 * ensure that virt2phys() is always an identity mapping.
852 } else {
853 if (type->write_phys_memory
854 || type->read_phys_memory
855 || type->virt2phys)
856 LOG_WARNING("type '%s' has broken MMU hooks",
857 type->name);
859 type->mmu = no_mmu;
860 type->write_phys_memory = type->write_memory;
861 type->read_phys_memory = type->read_memory;
862 type->virt2phys = identity_virt2phys;
866 if (all_targets)
868 if ((retval = target_register_user_commands(cmd_ctx)) != ERROR_OK)
869 return retval;
870 if ((retval = target_register_timer_callback(handle_target, 100, 1, NULL)) != ERROR_OK)
871 return retval;
874 return ERROR_OK;
877 int target_register_event_callback(int (*callback)(struct target *target, enum target_event event, void *priv), void *priv)
879 struct target_event_callback **callbacks_p = &target_event_callbacks;
881 if (callback == NULL)
883 return ERROR_INVALID_ARGUMENTS;
886 if (*callbacks_p)
888 while ((*callbacks_p)->next)
889 callbacks_p = &((*callbacks_p)->next);
890 callbacks_p = &((*callbacks_p)->next);
893 (*callbacks_p) = malloc(sizeof(struct target_event_callback));
894 (*callbacks_p)->callback = callback;
895 (*callbacks_p)->priv = priv;
896 (*callbacks_p)->next = NULL;
898 return ERROR_OK;
901 int target_register_timer_callback(int (*callback)(void *priv), int time_ms, int periodic, void *priv)
903 struct target_timer_callback **callbacks_p = &target_timer_callbacks;
904 struct timeval now;
906 if (callback == NULL)
908 return ERROR_INVALID_ARGUMENTS;
911 if (*callbacks_p)
913 while ((*callbacks_p)->next)
914 callbacks_p = &((*callbacks_p)->next);
915 callbacks_p = &((*callbacks_p)->next);
918 (*callbacks_p) = malloc(sizeof(struct target_timer_callback));
919 (*callbacks_p)->callback = callback;
920 (*callbacks_p)->periodic = periodic;
921 (*callbacks_p)->time_ms = time_ms;
923 gettimeofday(&now, NULL);
924 (*callbacks_p)->when.tv_usec = now.tv_usec + (time_ms % 1000) * 1000;
925 time_ms -= (time_ms % 1000);
926 (*callbacks_p)->when.tv_sec = now.tv_sec + (time_ms / 1000);
927 if ((*callbacks_p)->when.tv_usec > 1000000)
929 (*callbacks_p)->when.tv_usec = (*callbacks_p)->when.tv_usec - 1000000;
930 (*callbacks_p)->when.tv_sec += 1;
933 (*callbacks_p)->priv = priv;
934 (*callbacks_p)->next = NULL;
936 return ERROR_OK;
939 int target_unregister_event_callback(int (*callback)(struct target *target, enum target_event event, void *priv), void *priv)
941 struct target_event_callback **p = &target_event_callbacks;
942 struct target_event_callback *c = target_event_callbacks;
944 if (callback == NULL)
946 return ERROR_INVALID_ARGUMENTS;
949 while (c)
951 struct target_event_callback *next = c->next;
952 if ((c->callback == callback) && (c->priv == priv))
954 *p = next;
955 free(c);
956 return ERROR_OK;
958 else
959 p = &(c->next);
960 c = next;
963 return ERROR_OK;
966 int target_unregister_timer_callback(int (*callback)(void *priv), void *priv)
968 struct target_timer_callback **p = &target_timer_callbacks;
969 struct target_timer_callback *c = target_timer_callbacks;
971 if (callback == NULL)
973 return ERROR_INVALID_ARGUMENTS;
976 while (c)
978 struct target_timer_callback *next = c->next;
979 if ((c->callback == callback) && (c->priv == priv))
981 *p = next;
982 free(c);
983 return ERROR_OK;
985 else
986 p = &(c->next);
987 c = next;
990 return ERROR_OK;
993 int target_call_event_callbacks(struct target *target, enum target_event event)
995 struct target_event_callback *callback = target_event_callbacks;
996 struct target_event_callback *next_callback;
998 if (event == TARGET_EVENT_HALTED)
1000 /* execute early halted first */
1001 target_call_event_callbacks(target, TARGET_EVENT_GDB_HALT);
1004 LOG_DEBUG("target event %i (%s)",
1005 event,
1006 Jim_Nvp_value2name_simple(nvp_target_event, event)->name);
1008 target_handle_event(target, event);
1010 while (callback)
1012 next_callback = callback->next;
1013 callback->callback(target, event, callback->priv);
1014 callback = next_callback;
1017 return ERROR_OK;
1020 static int target_timer_callback_periodic_restart(
1021 struct target_timer_callback *cb, struct timeval *now)
1023 int time_ms = cb->time_ms;
1024 cb->when.tv_usec = now->tv_usec + (time_ms % 1000) * 1000;
1025 time_ms -= (time_ms % 1000);
1026 cb->when.tv_sec = now->tv_sec + time_ms / 1000;
1027 if (cb->when.tv_usec > 1000000)
1029 cb->when.tv_usec = cb->when.tv_usec - 1000000;
1030 cb->when.tv_sec += 1;
1032 return ERROR_OK;
1035 static int target_call_timer_callback(struct target_timer_callback *cb,
1036 struct timeval *now)
1038 cb->callback(cb->priv);
1040 if (cb->periodic)
1041 return target_timer_callback_periodic_restart(cb, now);
1043 return target_unregister_timer_callback(cb->callback, cb->priv);
1046 static int target_call_timer_callbacks_check_time(int checktime)
1048 keep_alive();
1050 struct timeval now;
1051 gettimeofday(&now, NULL);
1053 struct target_timer_callback *callback = target_timer_callbacks;
1054 while (callback)
1056 // cleaning up may unregister and free this callback
1057 struct target_timer_callback *next_callback = callback->next;
1059 bool call_it = callback->callback &&
1060 ((!checktime && callback->periodic) ||
1061 now.tv_sec > callback->when.tv_sec ||
1062 (now.tv_sec == callback->when.tv_sec &&
1063 now.tv_usec >= callback->when.tv_usec));
1065 if (call_it)
1067 int retval = target_call_timer_callback(callback, &now);
1068 if (retval != ERROR_OK)
1069 return retval;
1072 callback = next_callback;
1075 return ERROR_OK;
1078 int target_call_timer_callbacks(void)
1080 return target_call_timer_callbacks_check_time(1);
1083 /* invoke periodic callbacks immediately */
1084 int target_call_timer_callbacks_now(void)
1086 return target_call_timer_callbacks_check_time(0);
1089 int target_alloc_working_area(struct target *target, uint32_t size, struct working_area **area)
1091 struct working_area *c = target->working_areas;
1092 struct working_area *new_wa = NULL;
1094 /* Reevaluate working area address based on MMU state*/
1095 if (target->working_areas == NULL)
1097 int retval;
1098 int enabled;
1100 retval = target->type->mmu(target, &enabled);
1101 if (retval != ERROR_OK)
1103 return retval;
1106 if (!enabled) {
1107 if (target->working_area_phys_spec) {
1108 LOG_DEBUG("MMU disabled, using physical "
1109 "address for working memory 0x%08x",
1110 (unsigned)target->working_area_phys);
1111 target->working_area = target->working_area_phys;
1112 } else {
1113 LOG_ERROR("No working memory available. "
1114 "Specify -work-area-phys to target.");
1115 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
1117 } else {
1118 if (target->working_area_virt_spec) {
1119 LOG_DEBUG("MMU enabled, using virtual "
1120 "address for working memory 0x%08x",
1121 (unsigned)target->working_area_virt);
1122 target->working_area = target->working_area_virt;
1123 } else {
1124 LOG_ERROR("No working memory available. "
1125 "Specify -work-area-virt to target.");
1126 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
1131 /* only allocate multiples of 4 byte */
1132 if (size % 4)
1134 LOG_ERROR("BUG: code tried to allocate unaligned number of bytes (0x%08x), padding", ((unsigned)(size)));
1135 size = (size + 3) & (~3);
1138 /* see if there's already a matching working area */
1139 while (c)
1141 if ((c->free) && (c->size == size))
1143 new_wa = c;
1144 break;
1146 c = c->next;
1149 /* if not, allocate a new one */
1150 if (!new_wa)
1152 struct working_area **p = &target->working_areas;
1153 uint32_t first_free = target->working_area;
1154 uint32_t free_size = target->working_area_size;
1156 c = target->working_areas;
1157 while (c)
1159 first_free += c->size;
1160 free_size -= c->size;
1161 p = &c->next;
1162 c = c->next;
1165 if (free_size < size)
1167 LOG_WARNING("not enough working area available(requested %u, free %u)",
1168 (unsigned)(size), (unsigned)(free_size));
1169 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
1172 LOG_DEBUG("allocated new working area at address 0x%08x", (unsigned)first_free);
1174 new_wa = malloc(sizeof(struct working_area));
1175 new_wa->next = NULL;
1176 new_wa->size = size;
1177 new_wa->address = first_free;
1179 if (target->backup_working_area)
1181 int retval;
1182 new_wa->backup = malloc(new_wa->size);
1183 if ((retval = target_read_memory(target, new_wa->address, 4, new_wa->size / 4, new_wa->backup)) != ERROR_OK)
1185 free(new_wa->backup);
1186 free(new_wa);
1187 return retval;
1190 else
1192 new_wa->backup = NULL;
1195 /* put new entry in list */
1196 *p = new_wa;
1199 /* mark as used, and return the new (reused) area */
1200 new_wa->free = 0;
1201 *area = new_wa;
1203 /* user pointer */
1204 new_wa->user = area;
1206 return ERROR_OK;
1209 int target_free_working_area_restore(struct target *target, struct working_area *area, int restore)
1211 if (area->free)
1212 return ERROR_OK;
1214 if (restore && target->backup_working_area)
1216 int retval;
1217 if ((retval = target_write_memory(target, area->address, 4, area->size / 4, area->backup)) != ERROR_OK)
1218 return retval;
1221 area->free = 1;
1223 /* mark user pointer invalid */
1224 *area->user = NULL;
1225 area->user = NULL;
1227 return ERROR_OK;
1230 int target_free_working_area(struct target *target, struct working_area *area)
1232 return target_free_working_area_restore(target, area, 1);
1235 /* free resources and restore memory, if restoring memory fails,
1236 * free up resources anyway
1238 void target_free_all_working_areas_restore(struct target *target, int restore)
1240 struct working_area *c = target->working_areas;
1242 while (c)
1244 struct working_area *next = c->next;
1245 target_free_working_area_restore(target, c, restore);
1247 if (c->backup)
1248 free(c->backup);
1250 free(c);
1252 c = next;
1255 target->working_areas = NULL;
1258 void target_free_all_working_areas(struct target *target)
1260 target_free_all_working_areas_restore(target, 1);
1263 int target_arch_state(struct target *target)
1265 int retval;
1266 if (target == NULL)
1268 LOG_USER("No target has been configured");
1269 return ERROR_OK;
1272 LOG_USER("target state: %s", target_state_name( target ));
1274 if (target->state != TARGET_HALTED)
1275 return ERROR_OK;
1277 retval = target->type->arch_state(target);
1278 return retval;
1281 /* Single aligned words are guaranteed to use 16 or 32 bit access
1282 * mode respectively, otherwise data is handled as quickly as
1283 * possible
1285 int target_write_buffer(struct target *target, uint32_t address, uint32_t size, uint8_t *buffer)
1287 int retval;
1288 LOG_DEBUG("writing buffer of %i byte at 0x%8.8x",
1289 (int)size, (unsigned)address);
1291 if (!target_was_examined(target))
1293 LOG_ERROR("Target not examined yet");
1294 return ERROR_FAIL;
1297 if (size == 0) {
1298 return ERROR_OK;
1301 if ((address + size - 1) < address)
1303 /* GDB can request this when e.g. PC is 0xfffffffc*/
1304 LOG_ERROR("address + size wrapped(0x%08x, 0x%08x)",
1305 (unsigned)address,
1306 (unsigned)size);
1307 return ERROR_FAIL;
1310 if (((address % 2) == 0) && (size == 2))
1312 return target_write_memory(target, address, 2, 1, buffer);
1315 /* handle unaligned head bytes */
1316 if (address % 4)
1318 uint32_t unaligned = 4 - (address % 4);
1320 if (unaligned > size)
1321 unaligned = size;
1323 if ((retval = target_write_memory(target, address, 1, unaligned, buffer)) != ERROR_OK)
1324 return retval;
1326 buffer += unaligned;
1327 address += unaligned;
1328 size -= unaligned;
1331 /* handle aligned words */
1332 if (size >= 4)
1334 int aligned = size - (size % 4);
1336 /* use bulk writes above a certain limit. This may have to be changed */
1337 if (aligned > 128)
1339 if ((retval = target->type->bulk_write_memory(target, address, aligned / 4, buffer)) != ERROR_OK)
1340 return retval;
1342 else
1344 if ((retval = target_write_memory(target, address, 4, aligned / 4, buffer)) != ERROR_OK)
1345 return retval;
1348 buffer += aligned;
1349 address += aligned;
1350 size -= aligned;
1353 /* handle tail writes of less than 4 bytes */
1354 if (size > 0)
1356 if ((retval = target_write_memory(target, address, 1, size, buffer)) != ERROR_OK)
1357 return retval;
1360 return ERROR_OK;
1363 /* Single aligned words are guaranteed to use 16 or 32 bit access
1364 * mode respectively, otherwise data is handled as quickly as
1365 * possible
1367 int target_read_buffer(struct target *target, uint32_t address, uint32_t size, uint8_t *buffer)
1369 int retval;
1370 LOG_DEBUG("reading buffer of %i byte at 0x%8.8x",
1371 (int)size, (unsigned)address);
1373 if (!target_was_examined(target))
1375 LOG_ERROR("Target not examined yet");
1376 return ERROR_FAIL;
1379 if (size == 0) {
1380 return ERROR_OK;
1383 if ((address + size - 1) < address)
1385 /* GDB can request this when e.g. PC is 0xfffffffc*/
1386 LOG_ERROR("address + size wrapped(0x%08" PRIx32 ", 0x%08" PRIx32 ")",
1387 address,
1388 size);
1389 return ERROR_FAIL;
1392 if (((address % 2) == 0) && (size == 2))
1394 return target_read_memory(target, address, 2, 1, buffer);
1397 /* handle unaligned head bytes */
1398 if (address % 4)
1400 uint32_t unaligned = 4 - (address % 4);
1402 if (unaligned > size)
1403 unaligned = size;
1405 if ((retval = target_read_memory(target, address, 1, unaligned, buffer)) != ERROR_OK)
1406 return retval;
1408 buffer += unaligned;
1409 address += unaligned;
1410 size -= unaligned;
1413 /* handle aligned words */
1414 if (size >= 4)
1416 int aligned = size - (size % 4);
1418 if ((retval = target_read_memory(target, address, 4, aligned / 4, buffer)) != ERROR_OK)
1419 return retval;
1421 buffer += aligned;
1422 address += aligned;
1423 size -= aligned;
1426 /*prevent byte access when possible (avoid AHB access limitations in some cases)*/
1427 if(size >=2)
1429 int aligned = size - (size%2);
1430 retval = target_read_memory(target, address, 2, aligned / 2, buffer);
1431 if (retval != ERROR_OK)
1432 return retval;
1434 buffer += aligned;
1435 address += aligned;
1436 size -= aligned;
1438 /* handle tail writes of less than 4 bytes */
1439 if (size > 0)
1441 if ((retval = target_read_memory(target, address, 1, size, buffer)) != ERROR_OK)
1442 return retval;
1445 return ERROR_OK;
1448 int target_checksum_memory(struct target *target, uint32_t address, uint32_t size, uint32_t* crc)
1450 uint8_t *buffer;
1451 int retval;
1452 uint32_t i;
1453 uint32_t checksum = 0;
1454 if (!target_was_examined(target))
1456 LOG_ERROR("Target not examined yet");
1457 return ERROR_FAIL;
1460 if ((retval = target->type->checksum_memory(target, address,
1461 size, &checksum)) != ERROR_OK)
1463 buffer = malloc(size);
1464 if (buffer == NULL)
1466 LOG_ERROR("error allocating buffer for section (%d bytes)", (int)size);
1467 return ERROR_INVALID_ARGUMENTS;
1469 retval = target_read_buffer(target, address, size, buffer);
1470 if (retval != ERROR_OK)
1472 free(buffer);
1473 return retval;
1476 /* convert to target endianess */
1477 for (i = 0; i < (size/sizeof(uint32_t)); i++)
1479 uint32_t target_data;
1480 target_data = target_buffer_get_u32(target, &buffer[i*sizeof(uint32_t)]);
1481 target_buffer_set_u32(target, &buffer[i*sizeof(uint32_t)], target_data);
1484 retval = image_calculate_checksum(buffer, size, &checksum);
1485 free(buffer);
1488 *crc = checksum;
1490 return retval;
1493 int target_blank_check_memory(struct target *target, uint32_t address, uint32_t size, uint32_t* blank)
1495 int retval;
1496 if (!target_was_examined(target))
1498 LOG_ERROR("Target not examined yet");
1499 return ERROR_FAIL;
1502 if (target->type->blank_check_memory == 0)
1503 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
1505 retval = target->type->blank_check_memory(target, address, size, blank);
1507 return retval;
1510 int target_read_u32(struct target *target, uint32_t address, uint32_t *value)
1512 uint8_t value_buf[4];
1513 if (!target_was_examined(target))
1515 LOG_ERROR("Target not examined yet");
1516 return ERROR_FAIL;
1519 int retval = target_read_memory(target, address, 4, 1, value_buf);
1521 if (retval == ERROR_OK)
1523 *value = target_buffer_get_u32(target, value_buf);
1524 LOG_DEBUG("address: 0x%8.8" PRIx32 ", value: 0x%8.8" PRIx32 "",
1525 address,
1526 *value);
1528 else
1530 *value = 0x0;
1531 LOG_DEBUG("address: 0x%8.8" PRIx32 " failed",
1532 address);
1535 return retval;
1538 int target_read_u16(struct target *target, uint32_t address, uint16_t *value)
1540 uint8_t value_buf[2];
1541 if (!target_was_examined(target))
1543 LOG_ERROR("Target not examined yet");
1544 return ERROR_FAIL;
1547 int retval = target_read_memory(target, address, 2, 1, value_buf);
1549 if (retval == ERROR_OK)
1551 *value = target_buffer_get_u16(target, value_buf);
1552 LOG_DEBUG("address: 0x%8.8" PRIx32 ", value: 0x%4.4x",
1553 address,
1554 *value);
1556 else
1558 *value = 0x0;
1559 LOG_DEBUG("address: 0x%8.8" PRIx32 " failed",
1560 address);
1563 return retval;
1566 int target_read_u8(struct target *target, uint32_t address, uint8_t *value)
1568 int retval = target_read_memory(target, address, 1, 1, value);
1569 if (!target_was_examined(target))
1571 LOG_ERROR("Target not examined yet");
1572 return ERROR_FAIL;
1575 if (retval == ERROR_OK)
1577 LOG_DEBUG("address: 0x%8.8" PRIx32 ", value: 0x%2.2x",
1578 address,
1579 *value);
1581 else
1583 *value = 0x0;
1584 LOG_DEBUG("address: 0x%8.8" PRIx32 " failed",
1585 address);
1588 return retval;
1591 int target_write_u32(struct target *target, uint32_t address, uint32_t value)
1593 int retval;
1594 uint8_t value_buf[4];
1595 if (!target_was_examined(target))
1597 LOG_ERROR("Target not examined yet");
1598 return ERROR_FAIL;
1601 LOG_DEBUG("address: 0x%8.8" PRIx32 ", value: 0x%8.8" PRIx32 "",
1602 address,
1603 value);
1605 target_buffer_set_u32(target, value_buf, value);
1606 if ((retval = target_write_memory(target, address, 4, 1, value_buf)) != ERROR_OK)
1608 LOG_DEBUG("failed: %i", retval);
1611 return retval;
1614 int target_write_u16(struct target *target, uint32_t address, uint16_t value)
1616 int retval;
1617 uint8_t value_buf[2];
1618 if (!target_was_examined(target))
1620 LOG_ERROR("Target not examined yet");
1621 return ERROR_FAIL;
1624 LOG_DEBUG("address: 0x%8.8" PRIx32 ", value: 0x%8.8x",
1625 address,
1626 value);
1628 target_buffer_set_u16(target, value_buf, value);
1629 if ((retval = target_write_memory(target, address, 2, 1, value_buf)) != ERROR_OK)
1631 LOG_DEBUG("failed: %i", retval);
1634 return retval;
1637 int target_write_u8(struct target *target, uint32_t address, uint8_t value)
1639 int retval;
1640 if (!target_was_examined(target))
1642 LOG_ERROR("Target not examined yet");
1643 return ERROR_FAIL;
1646 LOG_DEBUG("address: 0x%8.8" PRIx32 ", value: 0x%2.2x",
1647 address, value);
1649 if ((retval = target_write_memory(target, address, 1, 1, &value)) != ERROR_OK)
1651 LOG_DEBUG("failed: %i", retval);
1654 return retval;
1657 COMMAND_HANDLER(handle_targets_command)
1659 struct target *target = all_targets;
1661 if (CMD_ARGC == 1)
1663 target = get_target(CMD_ARGV[0]);
1664 if (target == NULL) {
1665 command_print(CMD_CTX,"Target: %s is unknown, try one of:\n", CMD_ARGV[0]);
1666 goto DumpTargets;
1668 if (!target->tap->enabled) {
1669 command_print(CMD_CTX,"Target: TAP %s is disabled, "
1670 "can't be the current target\n",
1671 target->tap->dotted_name);
1672 return ERROR_FAIL;
1675 CMD_CTX->current_target = target->target_number;
1676 return ERROR_OK;
1678 DumpTargets:
1680 target = all_targets;
1681 command_print(CMD_CTX, " TargetName Type Endian TapName State ");
1682 command_print(CMD_CTX, "-- ------------------ ---------- ------ ------------------ ------------");
1683 while (target)
1685 const char *state;
1686 char marker = ' ';
1688 if (target->tap->enabled)
1689 state = target_state_name( target );
1690 else
1691 state = "tap-disabled";
1693 if (CMD_CTX->current_target == target->target_number)
1694 marker = '*';
1696 /* keep columns lined up to match the headers above */
1697 command_print(CMD_CTX, "%2d%c %-18s %-10s %-6s %-18s %s",
1698 target->target_number,
1699 marker,
1700 target_name(target),
1701 target_type_name(target),
1702 Jim_Nvp_value2name_simple(nvp_target_endian,
1703 target->endianness)->name,
1704 target->tap->dotted_name,
1705 state);
1706 target = target->next;
1709 return ERROR_OK;
1712 /* every 300ms we check for reset & powerdropout and issue a "reset halt" if so. */
1714 static int powerDropout;
1715 static int srstAsserted;
1717 static int runPowerRestore;
1718 static int runPowerDropout;
1719 static int runSrstAsserted;
1720 static int runSrstDeasserted;
1722 static int sense_handler(void)
1724 static int prevSrstAsserted = 0;
1725 static int prevPowerdropout = 0;
1727 int retval;
1728 if ((retval = jtag_power_dropout(&powerDropout)) != ERROR_OK)
1729 return retval;
1731 int powerRestored;
1732 powerRestored = prevPowerdropout && !powerDropout;
1733 if (powerRestored)
1735 runPowerRestore = 1;
1738 long long current = timeval_ms();
1739 static long long lastPower = 0;
1740 int waitMore = lastPower + 2000 > current;
1741 if (powerDropout && !waitMore)
1743 runPowerDropout = 1;
1744 lastPower = current;
1747 if ((retval = jtag_srst_asserted(&srstAsserted)) != ERROR_OK)
1748 return retval;
1750 int srstDeasserted;
1751 srstDeasserted = prevSrstAsserted && !srstAsserted;
1753 static long long lastSrst = 0;
1754 waitMore = lastSrst + 2000 > current;
1755 if (srstDeasserted && !waitMore)
1757 runSrstDeasserted = 1;
1758 lastSrst = current;
1761 if (!prevSrstAsserted && srstAsserted)
1763 runSrstAsserted = 1;
1766 prevSrstAsserted = srstAsserted;
1767 prevPowerdropout = powerDropout;
1769 if (srstDeasserted || powerRestored)
1771 /* Other than logging the event we can't do anything here.
1772 * Issuing a reset is a particularly bad idea as we might
1773 * be inside a reset already.
1777 return ERROR_OK;
1780 static void target_call_event_callbacks_all(enum target_event e) {
1781 struct target *target;
1782 target = all_targets;
1783 while (target) {
1784 target_call_event_callbacks(target, e);
1785 target = target->next;
1789 /* process target state changes */
1790 int handle_target(void *priv)
1792 int retval = ERROR_OK;
1794 /* we do not want to recurse here... */
1795 static int recursive = 0;
1796 if (! recursive)
1798 recursive = 1;
1799 sense_handler();
1800 /* danger! running these procedures can trigger srst assertions and power dropouts.
1801 * We need to avoid an infinite loop/recursion here and we do that by
1802 * clearing the flags after running these events.
1804 int did_something = 0;
1805 if (runSrstAsserted)
1807 target_call_event_callbacks_all(TARGET_EVENT_GDB_HALT);
1808 Jim_Eval(interp, "srst_asserted");
1809 did_something = 1;
1811 if (runSrstDeasserted)
1813 Jim_Eval(interp, "srst_deasserted");
1814 did_something = 1;
1816 if (runPowerDropout)
1818 target_call_event_callbacks_all(TARGET_EVENT_GDB_HALT);
1819 Jim_Eval(interp, "power_dropout");
1820 did_something = 1;
1822 if (runPowerRestore)
1824 Jim_Eval(interp, "power_restore");
1825 did_something = 1;
1828 if (did_something)
1830 /* clear detect flags */
1831 sense_handler();
1834 /* clear action flags */
1836 runSrstAsserted = 0;
1837 runSrstDeasserted = 0;
1838 runPowerRestore = 0;
1839 runPowerDropout = 0;
1841 recursive = 0;
1844 /* Poll targets for state changes unless that's globally disabled.
1845 * Skip targets that are currently disabled.
1847 for (struct target *target = all_targets;
1848 is_jtag_poll_safe() && target;
1849 target = target->next)
1851 if (!target->tap->enabled)
1852 continue;
1854 /* only poll target if we've got power and srst isn't asserted */
1855 if (!powerDropout && !srstAsserted)
1857 /* polling may fail silently until the target has been examined */
1858 if ((retval = target_poll(target)) != ERROR_OK)
1860 target_call_event_callbacks(target, TARGET_EVENT_GDB_HALT);
1861 return retval;
1866 return retval;
1869 COMMAND_HANDLER(handle_reg_command)
1871 struct target *target;
1872 struct reg *reg = NULL;
1873 unsigned count = 0;
1874 char *value;
1876 LOG_DEBUG("-");
1878 target = get_current_target(CMD_CTX);
1880 /* list all available registers for the current target */
1881 if (CMD_ARGC == 0)
1883 struct reg_cache *cache = target->reg_cache;
1885 count = 0;
1886 while (cache)
1888 unsigned i;
1890 command_print(CMD_CTX, "===== %s", cache->name);
1892 for (i = 0, reg = cache->reg_list;
1893 i < cache->num_regs;
1894 i++, reg++, count++)
1896 /* only print cached values if they are valid */
1897 if (reg->valid) {
1898 value = buf_to_str(reg->value,
1899 reg->size, 16);
1900 command_print(CMD_CTX,
1901 "(%i) %s (/%" PRIu32 "): 0x%s%s",
1902 count, reg->name,
1903 reg->size, value,
1904 reg->dirty
1905 ? " (dirty)"
1906 : "");
1907 free(value);
1908 } else {
1909 command_print(CMD_CTX, "(%i) %s (/%" PRIu32 ")",
1910 count, reg->name,
1911 reg->size) ;
1914 cache = cache->next;
1917 return ERROR_OK;
1920 /* access a single register by its ordinal number */
1921 if ((CMD_ARGV[0][0] >= '0') && (CMD_ARGV[0][0] <= '9'))
1923 unsigned num;
1924 COMMAND_PARSE_NUMBER(uint, CMD_ARGV[0], num);
1926 struct reg_cache *cache = target->reg_cache;
1927 count = 0;
1928 while (cache)
1930 unsigned i;
1931 for (i = 0; i < cache->num_regs; i++)
1933 if (count++ == num)
1935 reg = &cache->reg_list[i];
1936 break;
1939 if (reg)
1940 break;
1941 cache = cache->next;
1944 if (!reg)
1946 command_print(CMD_CTX, "%i is out of bounds, the current target has only %i registers (0 - %i)", num, count, count - 1);
1947 return ERROR_OK;
1949 } else /* access a single register by its name */
1951 reg = register_get_by_name(target->reg_cache, CMD_ARGV[0], 1);
1953 if (!reg)
1955 command_print(CMD_CTX, "register %s not found in current target", CMD_ARGV[0]);
1956 return ERROR_OK;
1960 /* display a register */
1961 if ((CMD_ARGC == 1) || ((CMD_ARGC == 2) && !((CMD_ARGV[1][0] >= '0') && (CMD_ARGV[1][0] <= '9'))))
1963 if ((CMD_ARGC == 2) && (strcmp(CMD_ARGV[1], "force") == 0))
1964 reg->valid = 0;
1966 if (reg->valid == 0)
1968 reg->type->get(reg);
1970 value = buf_to_str(reg->value, reg->size, 16);
1971 command_print(CMD_CTX, "%s (/%i): 0x%s", reg->name, (int)(reg->size), value);
1972 free(value);
1973 return ERROR_OK;
1976 /* set register value */
1977 if (CMD_ARGC == 2)
1979 uint8_t *buf = malloc(DIV_ROUND_UP(reg->size, 8));
1980 str_to_buf(CMD_ARGV[1], strlen(CMD_ARGV[1]), buf, reg->size, 0);
1982 reg->type->set(reg, buf);
1984 value = buf_to_str(reg->value, reg->size, 16);
1985 command_print(CMD_CTX, "%s (/%i): 0x%s", reg->name, (int)(reg->size), value);
1986 free(value);
1988 free(buf);
1990 return ERROR_OK;
1993 command_print(CMD_CTX, "usage: reg <#|name> [value]");
1995 return ERROR_OK;
1998 COMMAND_HANDLER(handle_poll_command)
2000 int retval = ERROR_OK;
2001 struct target *target = get_current_target(CMD_CTX);
2003 if (CMD_ARGC == 0)
2005 command_print(CMD_CTX, "background polling: %s",
2006 jtag_poll_get_enabled() ? "on" : "off");
2007 command_print(CMD_CTX, "TAP: %s (%s)",
2008 target->tap->dotted_name,
2009 target->tap->enabled ? "enabled" : "disabled");
2010 if (!target->tap->enabled)
2011 return ERROR_OK;
2012 if ((retval = target_poll(target)) != ERROR_OK)
2013 return retval;
2014 if ((retval = target_arch_state(target)) != ERROR_OK)
2015 return retval;
2017 else if (CMD_ARGC == 1)
2019 bool enable;
2020 COMMAND_PARSE_ON_OFF(CMD_ARGV[0], enable);
2021 jtag_poll_set_enabled(enable);
2023 else
2025 return ERROR_COMMAND_SYNTAX_ERROR;
2028 return retval;
2031 COMMAND_HANDLER(handle_wait_halt_command)
2033 if (CMD_ARGC > 1)
2034 return ERROR_COMMAND_SYNTAX_ERROR;
2036 unsigned ms = 5000;
2037 if (1 == CMD_ARGC)
2039 int retval = parse_uint(CMD_ARGV[0], &ms);
2040 if (ERROR_OK != retval)
2042 command_print(CMD_CTX, "usage: %s [seconds]", CMD_NAME);
2043 return ERROR_COMMAND_SYNTAX_ERROR;
2045 // convert seconds (given) to milliseconds (needed)
2046 ms *= 1000;
2049 struct target *target = get_current_target(CMD_CTX);
2050 return target_wait_state(target, TARGET_HALTED, ms);
2053 /* wait for target state to change. The trick here is to have a low
2054 * latency for short waits and not to suck up all the CPU time
2055 * on longer waits.
2057 * After 500ms, keep_alive() is invoked
2059 int target_wait_state(struct target *target, enum target_state state, int ms)
2061 int retval;
2062 long long then = 0, cur;
2063 int once = 1;
2065 for (;;)
2067 if ((retval = target_poll(target)) != ERROR_OK)
2068 return retval;
2069 if (target->state == state)
2071 break;
2073 cur = timeval_ms();
2074 if (once)
2076 once = 0;
2077 then = timeval_ms();
2078 LOG_DEBUG("waiting for target %s...",
2079 Jim_Nvp_value2name_simple(nvp_target_state,state)->name);
2082 if (cur-then > 500)
2084 keep_alive();
2087 if ((cur-then) > ms)
2089 LOG_ERROR("timed out while waiting for target %s",
2090 Jim_Nvp_value2name_simple(nvp_target_state,state)->name);
2091 return ERROR_FAIL;
2095 return ERROR_OK;
2098 COMMAND_HANDLER(handle_halt_command)
2100 LOG_DEBUG("-");
2102 struct target *target = get_current_target(CMD_CTX);
2103 int retval = target_halt(target);
2104 if (ERROR_OK != retval)
2105 return retval;
2107 if (CMD_ARGC == 1)
2109 unsigned wait;
2110 retval = parse_uint(CMD_ARGV[0], &wait);
2111 if (ERROR_OK != retval)
2112 return ERROR_COMMAND_SYNTAX_ERROR;
2113 if (!wait)
2114 return ERROR_OK;
2117 return CALL_COMMAND_HANDLER(handle_wait_halt_command);
2120 COMMAND_HANDLER(handle_soft_reset_halt_command)
2122 struct target *target = get_current_target(CMD_CTX);
2124 LOG_USER("requesting target halt and executing a soft reset");
2126 target->type->soft_reset_halt(target);
2128 return ERROR_OK;
2131 COMMAND_HANDLER(handle_reset_command)
2133 if (CMD_ARGC > 1)
2134 return ERROR_COMMAND_SYNTAX_ERROR;
2136 enum target_reset_mode reset_mode = RESET_RUN;
2137 if (CMD_ARGC == 1)
2139 const Jim_Nvp *n;
2140 n = Jim_Nvp_name2value_simple(nvp_reset_modes, CMD_ARGV[0]);
2141 if ((n->name == NULL) || (n->value == RESET_UNKNOWN)) {
2142 return ERROR_COMMAND_SYNTAX_ERROR;
2144 reset_mode = n->value;
2147 /* reset *all* targets */
2148 return target_process_reset(CMD_CTX, reset_mode);
2152 COMMAND_HANDLER(handle_resume_command)
2154 int current = 1;
2155 if (CMD_ARGC > 1)
2156 return ERROR_COMMAND_SYNTAX_ERROR;
2158 struct target *target = get_current_target(CMD_CTX);
2159 target_handle_event(target, TARGET_EVENT_OLD_pre_resume);
2161 /* with no CMD_ARGV, resume from current pc, addr = 0,
2162 * with one arguments, addr = CMD_ARGV[0],
2163 * handle breakpoints, not debugging */
2164 uint32_t addr = 0;
2165 if (CMD_ARGC == 1)
2167 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], addr);
2168 current = 0;
2171 return target_resume(target, current, addr, 1, 0);
2174 COMMAND_HANDLER(handle_step_command)
2176 if (CMD_ARGC > 1)
2177 return ERROR_COMMAND_SYNTAX_ERROR;
2179 LOG_DEBUG("-");
2181 /* with no CMD_ARGV, step from current pc, addr = 0,
2182 * with one argument addr = CMD_ARGV[0],
2183 * handle breakpoints, debugging */
2184 uint32_t addr = 0;
2185 int current_pc = 1;
2186 if (CMD_ARGC == 1)
2188 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], addr);
2189 current_pc = 0;
2192 struct target *target = get_current_target(CMD_CTX);
2194 return target->type->step(target, current_pc, addr, 1);
2197 static void handle_md_output(struct command_context *cmd_ctx,
2198 struct target *target, uint32_t address, unsigned size,
2199 unsigned count, const uint8_t *buffer)
2201 const unsigned line_bytecnt = 32;
2202 unsigned line_modulo = line_bytecnt / size;
2204 char output[line_bytecnt * 4 + 1];
2205 unsigned output_len = 0;
2207 const char *value_fmt;
2208 switch (size) {
2209 case 4: value_fmt = "%8.8x "; break;
2210 case 2: value_fmt = "%4.2x "; break;
2211 case 1: value_fmt = "%2.2x "; break;
2212 default:
2213 LOG_ERROR("invalid memory read size: %u", size);
2214 exit(-1);
2217 for (unsigned i = 0; i < count; i++)
2219 if (i % line_modulo == 0)
2221 output_len += snprintf(output + output_len,
2222 sizeof(output) - output_len,
2223 "0x%8.8x: ",
2224 (unsigned)(address + (i*size)));
2227 uint32_t value = 0;
2228 const uint8_t *value_ptr = buffer + i * size;
2229 switch (size) {
2230 case 4: value = target_buffer_get_u32(target, value_ptr); break;
2231 case 2: value = target_buffer_get_u16(target, value_ptr); break;
2232 case 1: value = *value_ptr;
2234 output_len += snprintf(output + output_len,
2235 sizeof(output) - output_len,
2236 value_fmt, value);
2238 if ((i % line_modulo == line_modulo - 1) || (i == count - 1))
2240 command_print(cmd_ctx, "%s", output);
2241 output_len = 0;
2246 COMMAND_HANDLER(handle_md_command)
2248 if (CMD_ARGC < 1)
2249 return ERROR_COMMAND_SYNTAX_ERROR;
2251 unsigned size = 0;
2252 switch (CMD_NAME[2]) {
2253 case 'w': size = 4; break;
2254 case 'h': size = 2; break;
2255 case 'b': size = 1; break;
2256 default: return ERROR_COMMAND_SYNTAX_ERROR;
2259 bool physical=strcmp(CMD_ARGV[0], "phys")==0;
2260 int (*fn)(struct target *target,
2261 uint32_t address, uint32_t size, uint32_t count, uint8_t *buffer);
2262 if (physical)
2264 CMD_ARGC--;
2265 CMD_ARGV++;
2266 fn=target_read_phys_memory;
2267 } else
2269 fn=target_read_memory;
2271 if ((CMD_ARGC < 1) || (CMD_ARGC > 2))
2273 return ERROR_COMMAND_SYNTAX_ERROR;
2276 uint32_t address;
2277 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], address);
2279 unsigned count = 1;
2280 if (CMD_ARGC == 2)
2281 COMMAND_PARSE_NUMBER(uint, CMD_ARGV[1], count);
2283 uint8_t *buffer = calloc(count, size);
2285 struct target *target = get_current_target(CMD_CTX);
2286 int retval = fn(target, address, size, count, buffer);
2287 if (ERROR_OK == retval)
2288 handle_md_output(CMD_CTX, target, address, size, count, buffer);
2290 free(buffer);
2292 return retval;
2295 COMMAND_HANDLER(handle_mw_command)
2297 if (CMD_ARGC < 2)
2299 return ERROR_COMMAND_SYNTAX_ERROR;
2301 bool physical=strcmp(CMD_ARGV[0], "phys")==0;
2302 int (*fn)(struct target *target,
2303 uint32_t address, uint32_t size, uint32_t count, uint8_t *buffer);
2304 if (physical)
2306 CMD_ARGC--;
2307 CMD_ARGV++;
2308 fn=target_write_phys_memory;
2309 } else
2311 fn=target_write_memory;
2313 if ((CMD_ARGC < 2) || (CMD_ARGC > 3))
2314 return ERROR_COMMAND_SYNTAX_ERROR;
2316 uint32_t address;
2317 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], address);
2319 uint32_t value;
2320 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], value);
2322 unsigned count = 1;
2323 if (CMD_ARGC == 3)
2324 COMMAND_PARSE_NUMBER(uint, CMD_ARGV[2], count);
2326 struct target *target = get_current_target(CMD_CTX);
2327 unsigned wordsize;
2328 uint8_t value_buf[4];
2329 switch (CMD_NAME[2])
2331 case 'w':
2332 wordsize = 4;
2333 target_buffer_set_u32(target, value_buf, value);
2334 break;
2335 case 'h':
2336 wordsize = 2;
2337 target_buffer_set_u16(target, value_buf, value);
2338 break;
2339 case 'b':
2340 wordsize = 1;
2341 value_buf[0] = value;
2342 break;
2343 default:
2344 return ERROR_COMMAND_SYNTAX_ERROR;
2346 for (unsigned i = 0; i < count; i++)
2348 int retval = fn(target,
2349 address + i * wordsize, wordsize, 1, value_buf);
2350 if (ERROR_OK != retval)
2351 return retval;
2352 keep_alive();
2355 return ERROR_OK;
2359 static COMMAND_HELPER(parse_load_image_command_CMD_ARGV, struct image *image,
2360 uint32_t *min_address, uint32_t *max_address)
2362 if (CMD_ARGC < 1 || CMD_ARGC > 5)
2363 return ERROR_COMMAND_SYNTAX_ERROR;
2365 /* a base address isn't always necessary,
2366 * default to 0x0 (i.e. don't relocate) */
2367 if (CMD_ARGC >= 2)
2369 uint32_t addr;
2370 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], addr);
2371 image->base_address = addr;
2372 image->base_address_set = 1;
2374 else
2375 image->base_address_set = 0;
2377 image->start_address_set = 0;
2379 if (CMD_ARGC >= 4)
2381 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[3], *min_address);
2383 if (CMD_ARGC == 5)
2385 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[4], *max_address);
2386 // use size (given) to find max (required)
2387 *max_address += *min_address;
2390 if (*min_address > *max_address)
2391 return ERROR_COMMAND_SYNTAX_ERROR;
2393 return ERROR_OK;
2396 COMMAND_HANDLER(handle_load_image_command)
2398 uint8_t *buffer;
2399 size_t buf_cnt;
2400 uint32_t image_size;
2401 uint32_t min_address = 0;
2402 uint32_t max_address = 0xffffffff;
2403 int i;
2404 struct image image;
2406 int retval = CALL_COMMAND_HANDLER(parse_load_image_command_CMD_ARGV,
2407 &image, &min_address, &max_address);
2408 if (ERROR_OK != retval)
2409 return retval;
2411 struct target *target = get_current_target(CMD_CTX);
2413 struct duration bench;
2414 duration_start(&bench);
2416 if (image_open(&image, CMD_ARGV[0], (CMD_ARGC >= 3) ? CMD_ARGV[2] : NULL) != ERROR_OK)
2418 return ERROR_OK;
2421 image_size = 0x0;
2422 retval = ERROR_OK;
2423 for (i = 0; i < image.num_sections; i++)
2425 buffer = malloc(image.sections[i].size);
2426 if (buffer == NULL)
2428 command_print(CMD_CTX,
2429 "error allocating buffer for section (%d bytes)",
2430 (int)(image.sections[i].size));
2431 break;
2434 if ((retval = image_read_section(&image, i, 0x0, image.sections[i].size, buffer, &buf_cnt)) != ERROR_OK)
2436 free(buffer);
2437 break;
2440 uint32_t offset = 0;
2441 uint32_t length = buf_cnt;
2443 /* DANGER!!! beware of unsigned comparision here!!! */
2445 if ((image.sections[i].base_address + buf_cnt >= min_address)&&
2446 (image.sections[i].base_address < max_address))
2448 if (image.sections[i].base_address < min_address)
2450 /* clip addresses below */
2451 offset += min_address-image.sections[i].base_address;
2452 length -= offset;
2455 if (image.sections[i].base_address + buf_cnt > max_address)
2457 length -= (image.sections[i].base_address + buf_cnt)-max_address;
2460 if ((retval = target_write_buffer(target, image.sections[i].base_address + offset, length, buffer + offset)) != ERROR_OK)
2462 free(buffer);
2463 break;
2465 image_size += length;
2466 command_print(CMD_CTX, "%u bytes written at address 0x%8.8" PRIx32 "",
2467 (unsigned int)length,
2468 image.sections[i].base_address + offset);
2471 free(buffer);
2474 if ((ERROR_OK == retval) && (duration_measure(&bench) == ERROR_OK))
2476 command_print(CMD_CTX, "downloaded %" PRIu32 " bytes "
2477 "in %fs (%0.3f kb/s)", image_size,
2478 duration_elapsed(&bench), duration_kbps(&bench, image_size));
2481 image_close(&image);
2483 return retval;
2487 COMMAND_HANDLER(handle_dump_image_command)
2489 struct fileio fileio;
2491 uint8_t buffer[560];
2492 int retvaltemp;
2495 struct target *target = get_current_target(CMD_CTX);
2497 if (CMD_ARGC != 3)
2499 command_print(CMD_CTX, "usage: dump_image <filename> <address> <size>");
2500 return ERROR_OK;
2503 uint32_t address;
2504 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], address);
2505 uint32_t size;
2506 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[2], size);
2508 if (fileio_open(&fileio, CMD_ARGV[0], FILEIO_WRITE, FILEIO_BINARY) != ERROR_OK)
2510 return ERROR_OK;
2513 struct duration bench;
2514 duration_start(&bench);
2516 int retval = ERROR_OK;
2517 while (size > 0)
2519 size_t size_written;
2520 uint32_t this_run_size = (size > 560) ? 560 : size;
2521 retval = target_read_buffer(target, address, this_run_size, buffer);
2522 if (retval != ERROR_OK)
2524 break;
2527 retval = fileio_write(&fileio, this_run_size, buffer, &size_written);
2528 if (retval != ERROR_OK)
2530 break;
2533 size -= this_run_size;
2534 address += this_run_size;
2537 if ((retvaltemp = fileio_close(&fileio)) != ERROR_OK)
2538 return retvaltemp;
2540 if ((ERROR_OK == retval) && (duration_measure(&bench) == ERROR_OK))
2542 command_print(CMD_CTX,
2543 "dumped %zu bytes in %fs (%0.3f kb/s)", fileio.size,
2544 duration_elapsed(&bench), duration_kbps(&bench, fileio.size));
2547 return retval;
2550 static COMMAND_HELPER(handle_verify_image_command_internal, int verify)
2552 uint8_t *buffer;
2553 size_t buf_cnt;
2554 uint32_t image_size;
2555 int i;
2556 int retval;
2557 uint32_t checksum = 0;
2558 uint32_t mem_checksum = 0;
2560 struct image image;
2562 struct target *target = get_current_target(CMD_CTX);
2564 if (CMD_ARGC < 1)
2566 return ERROR_COMMAND_SYNTAX_ERROR;
2569 if (!target)
2571 LOG_ERROR("no target selected");
2572 return ERROR_FAIL;
2575 struct duration bench;
2576 duration_start(&bench);
2578 if (CMD_ARGC >= 2)
2580 uint32_t addr;
2581 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], addr);
2582 image.base_address = addr;
2583 image.base_address_set = 1;
2585 else
2587 image.base_address_set = 0;
2588 image.base_address = 0x0;
2591 image.start_address_set = 0;
2593 if ((retval = image_open(&image, CMD_ARGV[0], (CMD_ARGC == 3) ? CMD_ARGV[2] : NULL)) != ERROR_OK)
2595 return retval;
2598 image_size = 0x0;
2599 retval = ERROR_OK;
2600 for (i = 0; i < image.num_sections; i++)
2602 buffer = malloc(image.sections[i].size);
2603 if (buffer == NULL)
2605 command_print(CMD_CTX,
2606 "error allocating buffer for section (%d bytes)",
2607 (int)(image.sections[i].size));
2608 break;
2610 if ((retval = image_read_section(&image, i, 0x0, image.sections[i].size, buffer, &buf_cnt)) != ERROR_OK)
2612 free(buffer);
2613 break;
2616 if (verify)
2618 /* calculate checksum of image */
2619 image_calculate_checksum(buffer, buf_cnt, &checksum);
2621 retval = target_checksum_memory(target, image.sections[i].base_address, buf_cnt, &mem_checksum);
2622 if (retval != ERROR_OK)
2624 free(buffer);
2625 break;
2628 if (checksum != mem_checksum)
2630 /* failed crc checksum, fall back to a binary compare */
2631 uint8_t *data;
2633 command_print(CMD_CTX, "checksum mismatch - attempting binary compare");
2635 data = (uint8_t*)malloc(buf_cnt);
2637 /* Can we use 32bit word accesses? */
2638 int size = 1;
2639 int count = buf_cnt;
2640 if ((count % 4) == 0)
2642 size *= 4;
2643 count /= 4;
2645 retval = target_read_memory(target, image.sections[i].base_address, size, count, data);
2646 if (retval == ERROR_OK)
2648 uint32_t t;
2649 for (t = 0; t < buf_cnt; t++)
2651 if (data[t] != buffer[t])
2653 command_print(CMD_CTX,
2654 "Verify operation failed address 0x%08x. Was 0x%02x instead of 0x%02x\n",
2655 (unsigned)(t + image.sections[i].base_address),
2656 data[t],
2657 buffer[t]);
2658 free(data);
2659 free(buffer);
2660 retval = ERROR_FAIL;
2661 goto done;
2663 if ((t%16384) == 0)
2665 keep_alive();
2670 free(data);
2672 } else
2674 command_print(CMD_CTX, "address 0x%08" PRIx32 " length 0x%08zx",
2675 image.sections[i].base_address,
2676 buf_cnt);
2679 free(buffer);
2680 image_size += buf_cnt;
2682 done:
2683 if ((ERROR_OK == retval) && (duration_measure(&bench) == ERROR_OK))
2685 command_print(CMD_CTX, "verified %" PRIu32 " bytes "
2686 "in %fs (%0.3f kb/s)", image_size,
2687 duration_elapsed(&bench), duration_kbps(&bench, image_size));
2690 image_close(&image);
2692 return retval;
2695 COMMAND_HANDLER(handle_verify_image_command)
2697 return CALL_COMMAND_HANDLER(handle_verify_image_command_internal, 1);
2700 COMMAND_HANDLER(handle_test_image_command)
2702 return CALL_COMMAND_HANDLER(handle_verify_image_command_internal, 0);
2705 static int handle_bp_command_list(struct command_context *cmd_ctx)
2707 struct target *target = get_current_target(cmd_ctx);
2708 struct breakpoint *breakpoint = target->breakpoints;
2709 while (breakpoint)
2711 if (breakpoint->type == BKPT_SOFT)
2713 char* buf = buf_to_str(breakpoint->orig_instr,
2714 breakpoint->length, 16);
2715 command_print(cmd_ctx, "0x%8.8" PRIx32 ", 0x%x, %i, 0x%s",
2716 breakpoint->address,
2717 breakpoint->length,
2718 breakpoint->set, buf);
2719 free(buf);
2721 else
2723 command_print(cmd_ctx, "0x%8.8" PRIx32 ", 0x%x, %i",
2724 breakpoint->address,
2725 breakpoint->length, breakpoint->set);
2728 breakpoint = breakpoint->next;
2730 return ERROR_OK;
2733 static int handle_bp_command_set(struct command_context *cmd_ctx,
2734 uint32_t addr, uint32_t length, int hw)
2736 struct target *target = get_current_target(cmd_ctx);
2737 int retval = breakpoint_add(target, addr, length, hw);
2738 if (ERROR_OK == retval)
2739 command_print(cmd_ctx, "breakpoint set at 0x%8.8" PRIx32 "", addr);
2740 else
2741 LOG_ERROR("Failure setting breakpoint");
2742 return retval;
2745 COMMAND_HANDLER(handle_bp_command)
2747 if (CMD_ARGC == 0)
2748 return handle_bp_command_list(CMD_CTX);
2750 if (CMD_ARGC < 2 || CMD_ARGC > 3)
2752 command_print(CMD_CTX, "usage: bp <address> <length> ['hw']");
2753 return ERROR_COMMAND_SYNTAX_ERROR;
2756 uint32_t addr;
2757 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], addr);
2758 uint32_t length;
2759 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], length);
2761 int hw = BKPT_SOFT;
2762 if (CMD_ARGC == 3)
2764 if (strcmp(CMD_ARGV[2], "hw") == 0)
2765 hw = BKPT_HARD;
2766 else
2767 return ERROR_COMMAND_SYNTAX_ERROR;
2770 return handle_bp_command_set(CMD_CTX, addr, length, hw);
2773 COMMAND_HANDLER(handle_rbp_command)
2775 if (CMD_ARGC != 1)
2776 return ERROR_COMMAND_SYNTAX_ERROR;
2778 uint32_t addr;
2779 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], addr);
2781 struct target *target = get_current_target(CMD_CTX);
2782 breakpoint_remove(target, addr);
2784 return ERROR_OK;
2787 COMMAND_HANDLER(handle_wp_command)
2789 struct target *target = get_current_target(CMD_CTX);
2791 if (CMD_ARGC == 0)
2793 struct watchpoint *watchpoint = target->watchpoints;
2795 while (watchpoint)
2797 command_print(CMD_CTX, "address: 0x%8.8" PRIx32
2798 ", len: 0x%8.8" PRIx32
2799 ", r/w/a: %i, value: 0x%8.8" PRIx32
2800 ", mask: 0x%8.8" PRIx32,
2801 watchpoint->address,
2802 watchpoint->length,
2803 (int)watchpoint->rw,
2804 watchpoint->value,
2805 watchpoint->mask);
2806 watchpoint = watchpoint->next;
2808 return ERROR_OK;
2811 enum watchpoint_rw type = WPT_ACCESS;
2812 uint32_t addr = 0;
2813 uint32_t length = 0;
2814 uint32_t data_value = 0x0;
2815 uint32_t data_mask = 0xffffffff;
2817 switch (CMD_ARGC)
2819 case 5:
2820 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[4], data_mask);
2821 // fall through
2822 case 4:
2823 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[3], data_value);
2824 // fall through
2825 case 3:
2826 switch (CMD_ARGV[2][0])
2828 case 'r':
2829 type = WPT_READ;
2830 break;
2831 case 'w':
2832 type = WPT_WRITE;
2833 break;
2834 case 'a':
2835 type = WPT_ACCESS;
2836 break;
2837 default:
2838 LOG_ERROR("invalid watchpoint mode ('%c')", CMD_ARGV[2][0]);
2839 return ERROR_COMMAND_SYNTAX_ERROR;
2841 // fall through
2842 case 2:
2843 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], length);
2844 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], addr);
2845 break;
2847 default:
2848 command_print(CMD_CTX, "usage: wp [address length "
2849 "[(r|w|a) [value [mask]]]]");
2850 return ERROR_COMMAND_SYNTAX_ERROR;
2853 int retval = watchpoint_add(target, addr, length, type,
2854 data_value, data_mask);
2855 if (ERROR_OK != retval)
2856 LOG_ERROR("Failure setting watchpoints");
2858 return retval;
2861 COMMAND_HANDLER(handle_rwp_command)
2863 if (CMD_ARGC != 1)
2864 return ERROR_COMMAND_SYNTAX_ERROR;
2866 uint32_t addr;
2867 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], addr);
2869 struct target *target = get_current_target(CMD_CTX);
2870 watchpoint_remove(target, addr);
2872 return ERROR_OK;
2877 * Translate a virtual address to a physical address.
2879 * The low-level target implementation must have logged a detailed error
2880 * which is forwarded to telnet/GDB session.
2882 COMMAND_HANDLER(handle_virt2phys_command)
2884 if (CMD_ARGC != 1)
2885 return ERROR_COMMAND_SYNTAX_ERROR;
2887 uint32_t va;
2888 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], va);
2889 uint32_t pa;
2891 struct target *target = get_current_target(CMD_CTX);
2892 int retval = target->type->virt2phys(target, va, &pa);
2893 if (retval == ERROR_OK)
2894 command_print(CMD_CTX, "Physical address 0x%08" PRIx32 "", pa);
2896 return retval;
2899 static void writeData(FILE *f, const void *data, size_t len)
2901 size_t written = fwrite(data, 1, len, f);
2902 if (written != len)
2903 LOG_ERROR("failed to write %zu bytes: %s", len, strerror(errno));
2906 static void writeLong(FILE *f, int l)
2908 int i;
2909 for (i = 0; i < 4; i++)
2911 char c = (l >> (i*8))&0xff;
2912 writeData(f, &c, 1);
2917 static void writeString(FILE *f, char *s)
2919 writeData(f, s, strlen(s));
2922 /* Dump a gmon.out histogram file. */
2923 static void writeGmon(uint32_t *samples, uint32_t sampleNum, const char *filename)
2925 uint32_t i;
2926 FILE *f = fopen(filename, "w");
2927 if (f == NULL)
2928 return;
2929 writeString(f, "gmon");
2930 writeLong(f, 0x00000001); /* Version */
2931 writeLong(f, 0); /* padding */
2932 writeLong(f, 0); /* padding */
2933 writeLong(f, 0); /* padding */
2935 uint8_t zero = 0; /* GMON_TAG_TIME_HIST */
2936 writeData(f, &zero, 1);
2938 /* figure out bucket size */
2939 uint32_t min = samples[0];
2940 uint32_t max = samples[0];
2941 for (i = 0; i < sampleNum; i++)
2943 if (min > samples[i])
2945 min = samples[i];
2947 if (max < samples[i])
2949 max = samples[i];
2953 int addressSpace = (max-min + 1);
2955 static const uint32_t maxBuckets = 256 * 1024; /* maximum buckets. */
2956 uint32_t length = addressSpace;
2957 if (length > maxBuckets)
2959 length = maxBuckets;
2961 int *buckets = malloc(sizeof(int)*length);
2962 if (buckets == NULL)
2964 fclose(f);
2965 return;
2967 memset(buckets, 0, sizeof(int)*length);
2968 for (i = 0; i < sampleNum;i++)
2970 uint32_t address = samples[i];
2971 long long a = address-min;
2972 long long b = length-1;
2973 long long c = addressSpace-1;
2974 int index = (a*b)/c; /* danger!!!! int32 overflows */
2975 buckets[index]++;
2978 /* append binary memory gmon.out &profile_hist_hdr ((char*)&profile_hist_hdr + sizeof(struct gmon_hist_hdr)) */
2979 writeLong(f, min); /* low_pc */
2980 writeLong(f, max); /* high_pc */
2981 writeLong(f, length); /* # of samples */
2982 writeLong(f, 64000000); /* 64MHz */
2983 writeString(f, "seconds");
2984 for (i = 0; i < (15-strlen("seconds")); i++)
2985 writeData(f, &zero, 1);
2986 writeString(f, "s");
2988 /*append binary memory gmon.out profile_hist_data (profile_hist_data + profile_hist_hdr.hist_size) */
2990 char *data = malloc(2*length);
2991 if (data != NULL)
2993 for (i = 0; i < length;i++)
2995 int val;
2996 val = buckets[i];
2997 if (val > 65535)
2999 val = 65535;
3001 data[i*2]=val&0xff;
3002 data[i*2 + 1]=(val >> 8)&0xff;
3004 free(buckets);
3005 writeData(f, data, length * 2);
3006 free(data);
3007 } else
3009 free(buckets);
3012 fclose(f);
3015 /* profiling samples the CPU PC as quickly as OpenOCD is able, which will be used as a random sampling of PC */
3016 COMMAND_HANDLER(handle_profile_command)
3018 struct target *target = get_current_target(CMD_CTX);
3019 struct timeval timeout, now;
3021 gettimeofday(&timeout, NULL);
3022 if (CMD_ARGC != 2)
3024 return ERROR_COMMAND_SYNTAX_ERROR;
3026 unsigned offset;
3027 COMMAND_PARSE_NUMBER(uint, CMD_ARGV[0], offset);
3029 timeval_add_time(&timeout, offset, 0);
3031 command_print(CMD_CTX, "Starting profiling. Halting and resuming the target as often as we can...");
3033 static const int maxSample = 10000;
3034 uint32_t *samples = malloc(sizeof(uint32_t)*maxSample);
3035 if (samples == NULL)
3036 return ERROR_OK;
3038 int numSamples = 0;
3039 /* hopefully it is safe to cache! We want to stop/restart as quickly as possible. */
3040 struct reg *reg = register_get_by_name(target->reg_cache, "pc", 1);
3042 for (;;)
3044 int retval;
3045 target_poll(target);
3046 if (target->state == TARGET_HALTED)
3048 uint32_t t=*((uint32_t *)reg->value);
3049 samples[numSamples++]=t;
3050 retval = target_resume(target, 1, 0, 0, 0); /* current pc, addr = 0, do not handle breakpoints, not debugging */
3051 target_poll(target);
3052 alive_sleep(10); /* sleep 10ms, i.e. <100 samples/second. */
3053 } else if (target->state == TARGET_RUNNING)
3055 /* We want to quickly sample the PC. */
3056 if ((retval = target_halt(target)) != ERROR_OK)
3058 free(samples);
3059 return retval;
3061 } else
3063 command_print(CMD_CTX, "Target not halted or running");
3064 retval = ERROR_OK;
3065 break;
3067 if (retval != ERROR_OK)
3069 break;
3072 gettimeofday(&now, NULL);
3073 if ((numSamples >= maxSample) || ((now.tv_sec >= timeout.tv_sec) && (now.tv_usec >= timeout.tv_usec)))
3075 command_print(CMD_CTX, "Profiling completed. %d samples.", numSamples);
3076 if ((retval = target_poll(target)) != ERROR_OK)
3078 free(samples);
3079 return retval;
3081 if (target->state == TARGET_HALTED)
3083 target_resume(target, 1, 0, 0, 0); /* current pc, addr = 0, do not handle breakpoints, not debugging */
3085 if ((retval = target_poll(target)) != ERROR_OK)
3087 free(samples);
3088 return retval;
3090 writeGmon(samples, numSamples, CMD_ARGV[1]);
3091 command_print(CMD_CTX, "Wrote %s", CMD_ARGV[1]);
3092 break;
3095 free(samples);
3097 return ERROR_OK;
3100 static int new_int_array_element(Jim_Interp * interp, const char *varname, int idx, uint32_t val)
3102 char *namebuf;
3103 Jim_Obj *nameObjPtr, *valObjPtr;
3104 int result;
3106 namebuf = alloc_printf("%s(%d)", varname, idx);
3107 if (!namebuf)
3108 return JIM_ERR;
3110 nameObjPtr = Jim_NewStringObj(interp, namebuf, -1);
3111 valObjPtr = Jim_NewIntObj(interp, val);
3112 if (!nameObjPtr || !valObjPtr)
3114 free(namebuf);
3115 return JIM_ERR;
3118 Jim_IncrRefCount(nameObjPtr);
3119 Jim_IncrRefCount(valObjPtr);
3120 result = Jim_SetVariable(interp, nameObjPtr, valObjPtr);
3121 Jim_DecrRefCount(interp, nameObjPtr);
3122 Jim_DecrRefCount(interp, valObjPtr);
3123 free(namebuf);
3124 /* printf("%s(%d) <= 0%08x\n", varname, idx, val); */
3125 return result;
3128 static int jim_mem2array(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
3130 struct command_context *context;
3131 struct target *target;
3133 context = Jim_GetAssocData(interp, "context");
3134 if (context == NULL)
3136 LOG_ERROR("mem2array: no command context");
3137 return JIM_ERR;
3139 target = get_current_target(context);
3140 if (target == NULL)
3142 LOG_ERROR("mem2array: no current target");
3143 return JIM_ERR;
3146 return target_mem2array(interp, target, argc-1, argv + 1);
3149 static int target_mem2array(Jim_Interp *interp, struct target *target, int argc, Jim_Obj *const *argv)
3151 long l;
3152 uint32_t width;
3153 int len;
3154 uint32_t addr;
3155 uint32_t count;
3156 uint32_t v;
3157 const char *varname;
3158 int n, e, retval;
3159 uint32_t i;
3161 /* argv[1] = name of array to receive the data
3162 * argv[2] = desired width
3163 * argv[3] = memory address
3164 * argv[4] = count of times to read
3166 if (argc != 4) {
3167 Jim_WrongNumArgs(interp, 1, argv, "varname width addr nelems");
3168 return JIM_ERR;
3170 varname = Jim_GetString(argv[0], &len);
3171 /* given "foo" get space for worse case "foo(%d)" .. add 20 */
3173 e = Jim_GetLong(interp, argv[1], &l);
3174 width = l;
3175 if (e != JIM_OK) {
3176 return e;
3179 e = Jim_GetLong(interp, argv[2], &l);
3180 addr = l;
3181 if (e != JIM_OK) {
3182 return e;
3184 e = Jim_GetLong(interp, argv[3], &l);
3185 len = l;
3186 if (e != JIM_OK) {
3187 return e;
3189 switch (width) {
3190 case 8:
3191 width = 1;
3192 break;
3193 case 16:
3194 width = 2;
3195 break;
3196 case 32:
3197 width = 4;
3198 break;
3199 default:
3200 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3201 Jim_AppendStrings(interp, Jim_GetResult(interp), "Invalid width param, must be 8/16/32", NULL);
3202 return JIM_ERR;
3204 if (len == 0) {
3205 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3206 Jim_AppendStrings(interp, Jim_GetResult(interp), "mem2array: zero width read?", NULL);
3207 return JIM_ERR;
3209 if ((addr + (len * width)) < addr) {
3210 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3211 Jim_AppendStrings(interp, Jim_GetResult(interp), "mem2array: addr + len - wraps to zero?", NULL);
3212 return JIM_ERR;
3214 /* absurd transfer size? */
3215 if (len > 65536) {
3216 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3217 Jim_AppendStrings(interp, Jim_GetResult(interp), "mem2array: absurd > 64K item request", NULL);
3218 return JIM_ERR;
3221 if ((width == 1) ||
3222 ((width == 2) && ((addr & 1) == 0)) ||
3223 ((width == 4) && ((addr & 3) == 0))) {
3224 /* all is well */
3225 } else {
3226 char buf[100];
3227 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3228 sprintf(buf, "mem2array address: 0x%08" PRIx32 " is not aligned for %" PRId32 " byte reads",
3229 addr,
3230 width);
3231 Jim_AppendStrings(interp, Jim_GetResult(interp), buf , NULL);
3232 return JIM_ERR;
3235 /* Transfer loop */
3237 /* index counter */
3238 n = 0;
3240 size_t buffersize = 4096;
3241 uint8_t *buffer = malloc(buffersize);
3242 if (buffer == NULL)
3243 return JIM_ERR;
3245 /* assume ok */
3246 e = JIM_OK;
3247 while (len) {
3248 /* Slurp... in buffer size chunks */
3250 count = len; /* in objects.. */
3251 if (count > (buffersize/width)) {
3252 count = (buffersize/width);
3255 retval = target_read_memory(target, addr, width, count, buffer);
3256 if (retval != ERROR_OK) {
3257 /* BOO !*/
3258 LOG_ERROR("mem2array: Read @ 0x%08x, w=%d, cnt=%d, failed",
3259 (unsigned int)addr,
3260 (int)width,
3261 (int)count);
3262 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3263 Jim_AppendStrings(interp, Jim_GetResult(interp), "mem2array: cannot read memory", NULL);
3264 e = JIM_ERR;
3265 len = 0;
3266 } else {
3267 v = 0; /* shut up gcc */
3268 for (i = 0 ;i < count ;i++, n++) {
3269 switch (width) {
3270 case 4:
3271 v = target_buffer_get_u32(target, &buffer[i*width]);
3272 break;
3273 case 2:
3274 v = target_buffer_get_u16(target, &buffer[i*width]);
3275 break;
3276 case 1:
3277 v = buffer[i] & 0x0ff;
3278 break;
3280 new_int_array_element(interp, varname, n, v);
3282 len -= count;
3286 free(buffer);
3288 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3290 return JIM_OK;
3293 static int get_int_array_element(Jim_Interp * interp, const char *varname, int idx, uint32_t *val)
3295 char *namebuf;
3296 Jim_Obj *nameObjPtr, *valObjPtr;
3297 int result;
3298 long l;
3300 namebuf = alloc_printf("%s(%d)", varname, idx);
3301 if (!namebuf)
3302 return JIM_ERR;
3304 nameObjPtr = Jim_NewStringObj(interp, namebuf, -1);
3305 if (!nameObjPtr)
3307 free(namebuf);
3308 return JIM_ERR;
3311 Jim_IncrRefCount(nameObjPtr);
3312 valObjPtr = Jim_GetVariable(interp, nameObjPtr, JIM_ERRMSG);
3313 Jim_DecrRefCount(interp, nameObjPtr);
3314 free(namebuf);
3315 if (valObjPtr == NULL)
3316 return JIM_ERR;
3318 result = Jim_GetLong(interp, valObjPtr, &l);
3319 /* printf("%s(%d) => 0%08x\n", varname, idx, val); */
3320 *val = l;
3321 return result;
3324 static int jim_array2mem(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
3326 struct command_context *context;
3327 struct target *target;
3329 context = Jim_GetAssocData(interp, "context");
3330 if (context == NULL) {
3331 LOG_ERROR("array2mem: no command context");
3332 return JIM_ERR;
3334 target = get_current_target(context);
3335 if (target == NULL) {
3336 LOG_ERROR("array2mem: no current target");
3337 return JIM_ERR;
3340 return target_array2mem(interp,target, argc-1, argv + 1);
3342 static int target_array2mem(Jim_Interp *interp, struct target *target, int argc, Jim_Obj *const *argv)
3344 long l;
3345 uint32_t width;
3346 int len;
3347 uint32_t addr;
3348 uint32_t count;
3349 uint32_t v;
3350 const char *varname;
3351 int n, e, retval;
3352 uint32_t i;
3354 /* argv[1] = name of array to get the data
3355 * argv[2] = desired width
3356 * argv[3] = memory address
3357 * argv[4] = count to write
3359 if (argc != 4) {
3360 Jim_WrongNumArgs(interp, 1, argv, "varname width addr nelems");
3361 return JIM_ERR;
3363 varname = Jim_GetString(argv[0], &len);
3364 /* given "foo" get space for worse case "foo(%d)" .. add 20 */
3366 e = Jim_GetLong(interp, argv[1], &l);
3367 width = l;
3368 if (e != JIM_OK) {
3369 return e;
3372 e = Jim_GetLong(interp, argv[2], &l);
3373 addr = l;
3374 if (e != JIM_OK) {
3375 return e;
3377 e = Jim_GetLong(interp, argv[3], &l);
3378 len = l;
3379 if (e != JIM_OK) {
3380 return e;
3382 switch (width) {
3383 case 8:
3384 width = 1;
3385 break;
3386 case 16:
3387 width = 2;
3388 break;
3389 case 32:
3390 width = 4;
3391 break;
3392 default:
3393 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3394 Jim_AppendStrings(interp, Jim_GetResult(interp), "Invalid width param, must be 8/16/32", NULL);
3395 return JIM_ERR;
3397 if (len == 0) {
3398 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3399 Jim_AppendStrings(interp, Jim_GetResult(interp), "array2mem: zero width read?", NULL);
3400 return JIM_ERR;
3402 if ((addr + (len * width)) < addr) {
3403 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3404 Jim_AppendStrings(interp, Jim_GetResult(interp), "array2mem: addr + len - wraps to zero?", NULL);
3405 return JIM_ERR;
3407 /* absurd transfer size? */
3408 if (len > 65536) {
3409 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3410 Jim_AppendStrings(interp, Jim_GetResult(interp), "array2mem: absurd > 64K item request", NULL);
3411 return JIM_ERR;
3414 if ((width == 1) ||
3415 ((width == 2) && ((addr & 1) == 0)) ||
3416 ((width == 4) && ((addr & 3) == 0))) {
3417 /* all is well */
3418 } else {
3419 char buf[100];
3420 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3421 sprintf(buf, "array2mem address: 0x%08x is not aligned for %d byte reads",
3422 (unsigned int)addr,
3423 (int)width);
3424 Jim_AppendStrings(interp, Jim_GetResult(interp), buf , NULL);
3425 return JIM_ERR;
3428 /* Transfer loop */
3430 /* index counter */
3431 n = 0;
3432 /* assume ok */
3433 e = JIM_OK;
3435 size_t buffersize = 4096;
3436 uint8_t *buffer = malloc(buffersize);
3437 if (buffer == NULL)
3438 return JIM_ERR;
3440 while (len) {
3441 /* Slurp... in buffer size chunks */
3443 count = len; /* in objects.. */
3444 if (count > (buffersize/width)) {
3445 count = (buffersize/width);
3448 v = 0; /* shut up gcc */
3449 for (i = 0 ;i < count ;i++, n++) {
3450 get_int_array_element(interp, varname, n, &v);
3451 switch (width) {
3452 case 4:
3453 target_buffer_set_u32(target, &buffer[i*width], v);
3454 break;
3455 case 2:
3456 target_buffer_set_u16(target, &buffer[i*width], v);
3457 break;
3458 case 1:
3459 buffer[i] = v & 0x0ff;
3460 break;
3463 len -= count;
3465 retval = target_write_memory(target, addr, width, count, buffer);
3466 if (retval != ERROR_OK) {
3467 /* BOO !*/
3468 LOG_ERROR("array2mem: Write @ 0x%08x, w=%d, cnt=%d, failed",
3469 (unsigned int)addr,
3470 (int)width,
3471 (int)count);
3472 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3473 Jim_AppendStrings(interp, Jim_GetResult(interp), "array2mem: cannot read memory", NULL);
3474 e = JIM_ERR;
3475 len = 0;
3479 free(buffer);
3481 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3483 return JIM_OK;
3486 void target_all_handle_event(enum target_event e)
3488 struct target *target;
3490 LOG_DEBUG("**all*targets: event: %d, %s",
3491 (int)e,
3492 Jim_Nvp_value2name_simple(nvp_target_event, e)->name);
3494 target = all_targets;
3495 while (target) {
3496 target_handle_event(target, e);
3497 target = target->next;
3502 /* FIX? should we propagate errors here rather than printing them
3503 * and continuing?
3505 void target_handle_event(struct target *target, enum target_event e)
3507 struct target_event_action *teap;
3509 for (teap = target->event_action; teap != NULL; teap = teap->next) {
3510 if (teap->event == e) {
3511 LOG_DEBUG("target: (%d) %s (%s) event: %d (%s) action: %s",
3512 target->target_number,
3513 target_name(target),
3514 target_type_name(target),
3516 Jim_Nvp_value2name_simple(nvp_target_event, e)->name,
3517 Jim_GetString(teap->body, NULL));
3518 if (Jim_EvalObj(interp, teap->body) != JIM_OK)
3520 Jim_PrintErrorMessage(interp);
3526 enum target_cfg_param {
3527 TCFG_TYPE,
3528 TCFG_EVENT,
3529 TCFG_WORK_AREA_VIRT,
3530 TCFG_WORK_AREA_PHYS,
3531 TCFG_WORK_AREA_SIZE,
3532 TCFG_WORK_AREA_BACKUP,
3533 TCFG_ENDIAN,
3534 TCFG_VARIANT,
3535 TCFG_CHAIN_POSITION,
3538 static Jim_Nvp nvp_config_opts[] = {
3539 { .name = "-type", .value = TCFG_TYPE },
3540 { .name = "-event", .value = TCFG_EVENT },
3541 { .name = "-work-area-virt", .value = TCFG_WORK_AREA_VIRT },
3542 { .name = "-work-area-phys", .value = TCFG_WORK_AREA_PHYS },
3543 { .name = "-work-area-size", .value = TCFG_WORK_AREA_SIZE },
3544 { .name = "-work-area-backup", .value = TCFG_WORK_AREA_BACKUP },
3545 { .name = "-endian" , .value = TCFG_ENDIAN },
3546 { .name = "-variant", .value = TCFG_VARIANT },
3547 { .name = "-chain-position", .value = TCFG_CHAIN_POSITION },
3549 { .name = NULL, .value = -1 }
3552 static int target_configure(Jim_GetOptInfo *goi, struct target *target)
3554 Jim_Nvp *n;
3555 Jim_Obj *o;
3556 jim_wide w;
3557 char *cp;
3558 int e;
3560 /* parse config or cget options ... */
3561 while (goi->argc > 0) {
3562 Jim_SetEmptyResult(goi->interp);
3563 /* Jim_GetOpt_Debug(goi); */
3565 if (target->type->target_jim_configure) {
3566 /* target defines a configure function */
3567 /* target gets first dibs on parameters */
3568 e = (*(target->type->target_jim_configure))(target, goi);
3569 if (e == JIM_OK) {
3570 /* more? */
3571 continue;
3573 if (e == JIM_ERR) {
3574 /* An error */
3575 return e;
3577 /* otherwise we 'continue' below */
3579 e = Jim_GetOpt_Nvp(goi, nvp_config_opts, &n);
3580 if (e != JIM_OK) {
3581 Jim_GetOpt_NvpUnknown(goi, nvp_config_opts, 0);
3582 return e;
3584 switch (n->value) {
3585 case TCFG_TYPE:
3586 /* not setable */
3587 if (goi->isconfigure) {
3588 Jim_SetResult_sprintf(goi->interp,
3589 "not settable: %s", n->name);
3590 return JIM_ERR;
3591 } else {
3592 no_params:
3593 if (goi->argc != 0) {
3594 Jim_WrongNumArgs(goi->interp,
3595 goi->argc, goi->argv,
3596 "NO PARAMS");
3597 return JIM_ERR;
3600 Jim_SetResultString(goi->interp,
3601 target_type_name(target), -1);
3602 /* loop for more */
3603 break;
3604 case TCFG_EVENT:
3605 if (goi->argc == 0) {
3606 Jim_WrongNumArgs(goi->interp, goi->argc, goi->argv, "-event ?event-name? ...");
3607 return JIM_ERR;
3610 e = Jim_GetOpt_Nvp(goi, nvp_target_event, &n);
3611 if (e != JIM_OK) {
3612 Jim_GetOpt_NvpUnknown(goi, nvp_target_event, 1);
3613 return e;
3616 if (goi->isconfigure) {
3617 if (goi->argc != 1) {
3618 Jim_WrongNumArgs(goi->interp, goi->argc, goi->argv, "-event ?event-name? ?EVENT-BODY?");
3619 return JIM_ERR;
3621 } else {
3622 if (goi->argc != 0) {
3623 Jim_WrongNumArgs(goi->interp, goi->argc, goi->argv, "-event ?event-name?");
3624 return JIM_ERR;
3629 struct target_event_action *teap;
3631 teap = target->event_action;
3632 /* replace existing? */
3633 while (teap) {
3634 if (teap->event == (enum target_event)n->value) {
3635 break;
3637 teap = teap->next;
3640 if (goi->isconfigure) {
3641 bool replace = true;
3642 if (teap == NULL) {
3643 /* create new */
3644 teap = calloc(1, sizeof(*teap));
3645 replace = false;
3647 teap->event = n->value;
3648 Jim_GetOpt_Obj(goi, &o);
3649 if (teap->body) {
3650 Jim_DecrRefCount(interp, teap->body);
3652 teap->body = Jim_DuplicateObj(goi->interp, o);
3654 * FIXME:
3655 * Tcl/TK - "tk events" have a nice feature.
3656 * See the "BIND" command.
3657 * We should support that here.
3658 * You can specify %X and %Y in the event code.
3659 * The idea is: %T - target name.
3660 * The idea is: %N - target number
3661 * The idea is: %E - event name.
3663 Jim_IncrRefCount(teap->body);
3665 if (!replace)
3667 /* add to head of event list */
3668 teap->next = target->event_action;
3669 target->event_action = teap;
3671 Jim_SetEmptyResult(goi->interp);
3672 } else {
3673 /* get */
3674 if (teap == NULL) {
3675 Jim_SetEmptyResult(goi->interp);
3676 } else {
3677 Jim_SetResult(goi->interp, Jim_DuplicateObj(goi->interp, teap->body));
3681 /* loop for more */
3682 break;
3684 case TCFG_WORK_AREA_VIRT:
3685 if (goi->isconfigure) {
3686 target_free_all_working_areas(target);
3687 e = Jim_GetOpt_Wide(goi, &w);
3688 if (e != JIM_OK) {
3689 return e;
3691 target->working_area_virt = w;
3692 target->working_area_virt_spec = true;
3693 } else {
3694 if (goi->argc != 0) {
3695 goto no_params;
3698 Jim_SetResult(interp, Jim_NewIntObj(goi->interp, target->working_area_virt));
3699 /* loop for more */
3700 break;
3702 case TCFG_WORK_AREA_PHYS:
3703 if (goi->isconfigure) {
3704 target_free_all_working_areas(target);
3705 e = Jim_GetOpt_Wide(goi, &w);
3706 if (e != JIM_OK) {
3707 return e;
3709 target->working_area_phys = w;
3710 target->working_area_phys_spec = true;
3711 } else {
3712 if (goi->argc != 0) {
3713 goto no_params;
3716 Jim_SetResult(interp, Jim_NewIntObj(goi->interp, target->working_area_phys));
3717 /* loop for more */
3718 break;
3720 case TCFG_WORK_AREA_SIZE:
3721 if (goi->isconfigure) {
3722 target_free_all_working_areas(target);
3723 e = Jim_GetOpt_Wide(goi, &w);
3724 if (e != JIM_OK) {
3725 return e;
3727 target->working_area_size = w;
3728 } else {
3729 if (goi->argc != 0) {
3730 goto no_params;
3733 Jim_SetResult(interp, Jim_NewIntObj(goi->interp, target->working_area_size));
3734 /* loop for more */
3735 break;
3737 case TCFG_WORK_AREA_BACKUP:
3738 if (goi->isconfigure) {
3739 target_free_all_working_areas(target);
3740 e = Jim_GetOpt_Wide(goi, &w);
3741 if (e != JIM_OK) {
3742 return e;
3744 /* make this exactly 1 or 0 */
3745 target->backup_working_area = (!!w);
3746 } else {
3747 if (goi->argc != 0) {
3748 goto no_params;
3751 Jim_SetResult(interp, Jim_NewIntObj(goi->interp, target->backup_working_area));
3752 /* loop for more e*/
3753 break;
3755 case TCFG_ENDIAN:
3756 if (goi->isconfigure) {
3757 e = Jim_GetOpt_Nvp(goi, nvp_target_endian, &n);
3758 if (e != JIM_OK) {
3759 Jim_GetOpt_NvpUnknown(goi, nvp_target_endian, 1);
3760 return e;
3762 target->endianness = n->value;
3763 } else {
3764 if (goi->argc != 0) {
3765 goto no_params;
3768 n = Jim_Nvp_value2name_simple(nvp_target_endian, target->endianness);
3769 if (n->name == NULL) {
3770 target->endianness = TARGET_LITTLE_ENDIAN;
3771 n = Jim_Nvp_value2name_simple(nvp_target_endian, target->endianness);
3773 Jim_SetResultString(goi->interp, n->name, -1);
3774 /* loop for more */
3775 break;
3777 case TCFG_VARIANT:
3778 if (goi->isconfigure) {
3779 if (goi->argc < 1) {
3780 Jim_SetResult_sprintf(goi->interp,
3781 "%s ?STRING?",
3782 n->name);
3783 return JIM_ERR;
3785 if (target->variant) {
3786 free((void *)(target->variant));
3788 e = Jim_GetOpt_String(goi, &cp, NULL);
3789 target->variant = strdup(cp);
3790 } else {
3791 if (goi->argc != 0) {
3792 goto no_params;
3795 Jim_SetResultString(goi->interp, target->variant,-1);
3796 /* loop for more */
3797 break;
3798 case TCFG_CHAIN_POSITION:
3799 if (goi->isconfigure) {
3800 Jim_Obj *o;
3801 struct jtag_tap *tap;
3802 target_free_all_working_areas(target);
3803 e = Jim_GetOpt_Obj(goi, &o);
3804 if (e != JIM_OK) {
3805 return e;
3807 tap = jtag_tap_by_jim_obj(goi->interp, o);
3808 if (tap == NULL) {
3809 return JIM_ERR;
3811 /* make this exactly 1 or 0 */
3812 target->tap = tap;
3813 } else {
3814 if (goi->argc != 0) {
3815 goto no_params;
3818 Jim_SetResultString(interp, target->tap->dotted_name, -1);
3819 /* loop for more e*/
3820 break;
3822 } /* while (goi->argc) */
3825 /* done - we return */
3826 return JIM_OK;
3829 /** this is the 'tcl' handler for the target specific command */
3830 static int tcl_target_func(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
3832 Jim_GetOptInfo goi;
3833 jim_wide a,b,c;
3834 int x,y,z;
3835 uint8_t target_buf[32];
3836 Jim_Nvp *n;
3837 struct target *target;
3838 struct command_context *cmd_ctx;
3839 int e;
3841 enum {
3842 TS_CMD_CONFIGURE,
3843 TS_CMD_CGET,
3845 TS_CMD_MWW, TS_CMD_MWH, TS_CMD_MWB,
3846 TS_CMD_MDW, TS_CMD_MDH, TS_CMD_MDB,
3847 TS_CMD_MRW, TS_CMD_MRH, TS_CMD_MRB,
3848 TS_CMD_MEM2ARRAY, TS_CMD_ARRAY2MEM,
3849 TS_CMD_EXAMINE,
3850 TS_CMD_POLL,
3851 TS_CMD_RESET,
3852 TS_CMD_HALT,
3853 TS_CMD_WAITSTATE,
3854 TS_CMD_EVENTLIST,
3855 TS_CMD_CURSTATE,
3856 TS_CMD_INVOKE_EVENT,
3859 static const Jim_Nvp target_options[] = {
3860 { .name = "configure", .value = TS_CMD_CONFIGURE },
3861 { .name = "cget", .value = TS_CMD_CGET },
3862 { .name = "mww", .value = TS_CMD_MWW },
3863 { .name = "mwh", .value = TS_CMD_MWH },
3864 { .name = "mwb", .value = TS_CMD_MWB },
3865 { .name = "mdw", .value = TS_CMD_MDW },
3866 { .name = "mdh", .value = TS_CMD_MDH },
3867 { .name = "mdb", .value = TS_CMD_MDB },
3868 { .name = "mem2array", .value = TS_CMD_MEM2ARRAY },
3869 { .name = "array2mem", .value = TS_CMD_ARRAY2MEM },
3870 { .name = "eventlist", .value = TS_CMD_EVENTLIST },
3871 { .name = "curstate", .value = TS_CMD_CURSTATE },
3873 { .name = "arp_examine", .value = TS_CMD_EXAMINE },
3874 { .name = "arp_poll", .value = TS_CMD_POLL },
3875 { .name = "arp_reset", .value = TS_CMD_RESET },
3876 { .name = "arp_halt", .value = TS_CMD_HALT },
3877 { .name = "arp_waitstate", .value = TS_CMD_WAITSTATE },
3878 { .name = "invoke-event", .value = TS_CMD_INVOKE_EVENT },
3880 { .name = NULL, .value = -1 },
3883 /* go past the "command" */
3884 Jim_GetOpt_Setup(&goi, interp, argc-1, argv + 1);
3886 target = Jim_CmdPrivData(goi.interp);
3887 cmd_ctx = Jim_GetAssocData(goi.interp, "context");
3889 /* commands here are in an NVP table */
3890 e = Jim_GetOpt_Nvp(&goi, target_options, &n);
3891 if (e != JIM_OK) {
3892 Jim_GetOpt_NvpUnknown(&goi, target_options, 0);
3893 return e;
3895 /* Assume blank result */
3896 Jim_SetEmptyResult(goi.interp);
3898 switch (n->value) {
3899 case TS_CMD_CONFIGURE:
3900 if (goi.argc < 2) {
3901 Jim_WrongNumArgs(goi.interp, goi.argc, goi.argv, "missing: -option VALUE ...");
3902 return JIM_ERR;
3904 goi.isconfigure = 1;
3905 return target_configure(&goi, target);
3906 case TS_CMD_CGET:
3907 // some things take params
3908 if (goi.argc < 1) {
3909 Jim_WrongNumArgs(goi.interp, 0, goi.argv, "missing: ?-option?");
3910 return JIM_ERR;
3912 goi.isconfigure = 0;
3913 return target_configure(&goi, target);
3914 break;
3915 case TS_CMD_MWW:
3916 case TS_CMD_MWH:
3917 case TS_CMD_MWB:
3918 /* argv[0] = cmd
3919 * argv[1] = address
3920 * argv[2] = data
3921 * argv[3] = optional count.
3924 if ((goi.argc == 2) || (goi.argc == 3)) {
3925 /* all is well */
3926 } else {
3927 mwx_error:
3928 Jim_SetResult_sprintf(goi.interp, "expected: %s ADDR DATA [COUNT]", n->name);
3929 return JIM_ERR;
3932 e = Jim_GetOpt_Wide(&goi, &a);
3933 if (e != JIM_OK) {
3934 goto mwx_error;
3937 e = Jim_GetOpt_Wide(&goi, &b);
3938 if (e != JIM_OK) {
3939 goto mwx_error;
3941 if (goi.argc == 3) {
3942 e = Jim_GetOpt_Wide(&goi, &c);
3943 if (e != JIM_OK) {
3944 goto mwx_error;
3946 } else {
3947 c = 1;
3950 switch (n->value) {
3951 case TS_CMD_MWW:
3952 target_buffer_set_u32(target, target_buf, b);
3953 b = 4;
3954 break;
3955 case TS_CMD_MWH:
3956 target_buffer_set_u16(target, target_buf, b);
3957 b = 2;
3958 break;
3959 case TS_CMD_MWB:
3960 target_buffer_set_u8(target, target_buf, b);
3961 b = 1;
3962 break;
3964 for (x = 0 ; x < c ; x++) {
3965 e = target_write_memory(target, a, b, 1, target_buf);
3966 if (e != ERROR_OK) {
3967 Jim_SetResult_sprintf(interp, "Error writing @ 0x%08x: %d\n", (int)(a), e);
3968 return JIM_ERR;
3970 /* b = width */
3971 a = a + b;
3973 return JIM_OK;
3974 break;
3976 /* display */
3977 case TS_CMD_MDW:
3978 case TS_CMD_MDH:
3979 case TS_CMD_MDB:
3980 /* argv[0] = command
3981 * argv[1] = address
3982 * argv[2] = optional count
3984 if ((goi.argc == 2) || (goi.argc == 3)) {
3985 Jim_SetResult_sprintf(goi.interp, "expected: %s ADDR [COUNT]", n->name);
3986 return JIM_ERR;
3988 e = Jim_GetOpt_Wide(&goi, &a);
3989 if (e != JIM_OK) {
3990 return JIM_ERR;
3992 if (goi.argc) {
3993 e = Jim_GetOpt_Wide(&goi, &c);
3994 if (e != JIM_OK) {
3995 return JIM_ERR;
3997 } else {
3998 c = 1;
4000 b = 1; /* shut up gcc */
4001 switch (n->value) {
4002 case TS_CMD_MDW:
4003 b = 4;
4004 break;
4005 case TS_CMD_MDH:
4006 b = 2;
4007 break;
4008 case TS_CMD_MDB:
4009 b = 1;
4010 break;
4013 /* convert to "bytes" */
4014 c = c * b;
4015 /* count is now in 'BYTES' */
4016 while (c > 0) {
4017 y = c;
4018 if (y > 16) {
4019 y = 16;
4021 e = target_read_memory(target, a, b, y / b, target_buf);
4022 if (e != ERROR_OK) {
4023 Jim_SetResult_sprintf(interp, "error reading target @ 0x%08lx", (int)(a));
4024 return JIM_ERR;
4027 Jim_fprintf(interp, interp->cookie_stdout, "0x%08x ", (int)(a));
4028 switch (b) {
4029 case 4:
4030 for (x = 0 ; (x < 16) && (x < y) ; x += 4) {
4031 z = target_buffer_get_u32(target, &(target_buf[ x * 4 ]));
4032 Jim_fprintf(interp, interp->cookie_stdout, "%08x ", (int)(z));
4034 for (; (x < 16) ; x += 4) {
4035 Jim_fprintf(interp, interp->cookie_stdout, " ");
4037 break;
4038 case 2:
4039 for (x = 0 ; (x < 16) && (x < y) ; x += 2) {
4040 z = target_buffer_get_u16(target, &(target_buf[ x * 2 ]));
4041 Jim_fprintf(interp, interp->cookie_stdout, "%04x ", (int)(z));
4043 for (; (x < 16) ; x += 2) {
4044 Jim_fprintf(interp, interp->cookie_stdout, " ");
4046 break;
4047 case 1:
4048 default:
4049 for (x = 0 ; (x < 16) && (x < y) ; x += 1) {
4050 z = target_buffer_get_u8(target, &(target_buf[ x * 4 ]));
4051 Jim_fprintf(interp, interp->cookie_stdout, "%02x ", (int)(z));
4053 for (; (x < 16) ; x += 1) {
4054 Jim_fprintf(interp, interp->cookie_stdout, " ");
4056 break;
4058 /* ascii-ify the bytes */
4059 for (x = 0 ; x < y ; x++) {
4060 if ((target_buf[x] >= 0x20) &&
4061 (target_buf[x] <= 0x7e)) {
4062 /* good */
4063 } else {
4064 /* smack it */
4065 target_buf[x] = '.';
4068 /* space pad */
4069 while (x < 16) {
4070 target_buf[x] = ' ';
4071 x++;
4073 /* terminate */
4074 target_buf[16] = 0;
4075 /* print - with a newline */
4076 Jim_fprintf(interp, interp->cookie_stdout, "%s\n", target_buf);
4077 /* NEXT... */
4078 c -= 16;
4079 a += 16;
4081 return JIM_OK;
4082 case TS_CMD_MEM2ARRAY:
4083 return target_mem2array(goi.interp, target, goi.argc, goi.argv);
4084 break;
4085 case TS_CMD_ARRAY2MEM:
4086 return target_array2mem(goi.interp, target, goi.argc, goi.argv);
4087 break;
4088 case TS_CMD_EXAMINE:
4089 if (goi.argc) {
4090 Jim_WrongNumArgs(goi.interp, 2, argv, "[no parameters]");
4091 return JIM_ERR;
4093 if (!target->tap->enabled)
4094 goto err_tap_disabled;
4095 e = target->type->examine(target);
4096 if (e != ERROR_OK) {
4097 Jim_SetResult_sprintf(interp, "examine-fails: %d", e);
4098 return JIM_ERR;
4100 return JIM_OK;
4101 case TS_CMD_POLL:
4102 if (goi.argc) {
4103 Jim_WrongNumArgs(goi.interp, 2, argv, "[no parameters]");
4104 return JIM_ERR;
4106 if (!target->tap->enabled)
4107 goto err_tap_disabled;
4108 if (!(target_was_examined(target))) {
4109 e = ERROR_TARGET_NOT_EXAMINED;
4110 } else {
4111 e = target->type->poll(target);
4113 if (e != ERROR_OK) {
4114 Jim_SetResult_sprintf(interp, "poll-fails: %d", e);
4115 return JIM_ERR;
4116 } else {
4117 return JIM_OK;
4119 break;
4120 case TS_CMD_RESET:
4121 if (goi.argc != 2) {
4122 Jim_WrongNumArgs(interp, 2, argv,
4123 "([tT]|[fF]|assert|deassert) BOOL");
4124 return JIM_ERR;
4126 e = Jim_GetOpt_Nvp(&goi, nvp_assert, &n);
4127 if (e != JIM_OK) {
4128 Jim_GetOpt_NvpUnknown(&goi, nvp_assert, 1);
4129 return e;
4131 /* the halt or not param */
4132 e = Jim_GetOpt_Wide(&goi, &a);
4133 if (e != JIM_OK) {
4134 return e;
4136 if (!target->tap->enabled)
4137 goto err_tap_disabled;
4138 if (!target->type->assert_reset
4139 || !target->type->deassert_reset) {
4140 Jim_SetResult_sprintf(interp,
4141 "No target-specific reset for %s",
4142 target_name(target));
4143 return JIM_ERR;
4145 /* determine if we should halt or not. */
4146 target->reset_halt = !!a;
4147 /* When this happens - all workareas are invalid. */
4148 target_free_all_working_areas_restore(target, 0);
4150 /* do the assert */
4151 if (n->value == NVP_ASSERT) {
4152 e = target->type->assert_reset(target);
4153 } else {
4154 e = target->type->deassert_reset(target);
4156 return (e == ERROR_OK) ? JIM_OK : JIM_ERR;
4157 case TS_CMD_HALT:
4158 if (goi.argc) {
4159 Jim_WrongNumArgs(goi.interp, 0, argv, "halt [no parameters]");
4160 return JIM_ERR;
4162 if (!target->tap->enabled)
4163 goto err_tap_disabled;
4164 e = target->type->halt(target);
4165 return (e == ERROR_OK) ? JIM_OK : JIM_ERR;
4166 case TS_CMD_WAITSTATE:
4167 /* params: <name> statename timeoutmsecs */
4168 if (goi.argc != 2) {
4169 Jim_SetResult_sprintf(goi.interp, "%s STATENAME TIMEOUTMSECS", n->name);
4170 return JIM_ERR;
4172 e = Jim_GetOpt_Nvp(&goi, nvp_target_state, &n);
4173 if (e != JIM_OK) {
4174 Jim_GetOpt_NvpUnknown(&goi, nvp_target_state,1);
4175 return e;
4177 e = Jim_GetOpt_Wide(&goi, &a);
4178 if (e != JIM_OK) {
4179 return e;
4181 if (!target->tap->enabled)
4182 goto err_tap_disabled;
4183 e = target_wait_state(target, n->value, a);
4184 if (e != ERROR_OK) {
4185 Jim_SetResult_sprintf(goi.interp,
4186 "target: %s wait %s fails (%d) %s",
4187 target_name(target), n->name,
4188 e, target_strerror_safe(e));
4189 return JIM_ERR;
4190 } else {
4191 return JIM_OK;
4193 case TS_CMD_EVENTLIST:
4194 /* List for human, Events defined for this target.
4195 * scripts/programs should use 'name cget -event NAME'
4198 struct target_event_action *teap;
4199 teap = target->event_action;
4200 command_print(cmd_ctx,
4201 "Event actions for target (%d) %s\n",
4202 target->target_number,
4203 target_name(target));
4204 command_print(cmd_ctx, "%-25s | Body", "Event");
4205 command_print(cmd_ctx, "------------------------- | ----------------------------------------");
4206 while (teap) {
4207 command_print(cmd_ctx,
4208 "%-25s | %s",
4209 Jim_Nvp_value2name_simple(nvp_target_event, teap->event)->name,
4210 Jim_GetString(teap->body, NULL));
4211 teap = teap->next;
4213 command_print(cmd_ctx, "***END***");
4214 return JIM_OK;
4216 case TS_CMD_CURSTATE:
4217 if (goi.argc != 0) {
4218 Jim_WrongNumArgs(goi.interp, 0, argv, "[no parameters]");
4219 return JIM_ERR;
4221 Jim_SetResultString(goi.interp,
4222 target_state_name( target ),
4223 -1);
4224 return JIM_OK;
4225 case TS_CMD_INVOKE_EVENT:
4226 if (goi.argc != 1) {
4227 Jim_SetResult_sprintf(goi.interp, "%s ?EVENTNAME?",n->name);
4228 return JIM_ERR;
4230 e = Jim_GetOpt_Nvp(&goi, nvp_target_event, &n);
4231 if (e != JIM_OK) {
4232 Jim_GetOpt_NvpUnknown(&goi, nvp_target_event, 1);
4233 return e;
4235 target_handle_event(target, n->value);
4236 return JIM_OK;
4238 return JIM_ERR;
4240 err_tap_disabled:
4241 Jim_SetResult_sprintf(interp, "[TAP is disabled]");
4242 return JIM_ERR;
4245 static int target_create(Jim_GetOptInfo *goi)
4247 Jim_Obj *new_cmd;
4248 Jim_Cmd *cmd;
4249 const char *cp;
4250 char *cp2;
4251 int e;
4252 int x;
4253 struct target *target;
4254 struct command_context *cmd_ctx;
4256 cmd_ctx = Jim_GetAssocData(goi->interp, "context");
4257 if (goi->argc < 3) {
4258 Jim_WrongNumArgs(goi->interp, 1, goi->argv, "?name? ?type? ..options...");
4259 return JIM_ERR;
4262 /* COMMAND */
4263 Jim_GetOpt_Obj(goi, &new_cmd);
4264 /* does this command exist? */
4265 cmd = Jim_GetCommand(goi->interp, new_cmd, JIM_ERRMSG);
4266 if (cmd) {
4267 cp = Jim_GetString(new_cmd, NULL);
4268 Jim_SetResult_sprintf(goi->interp, "Command/target: %s Exists", cp);
4269 return JIM_ERR;
4272 /* TYPE */
4273 e = Jim_GetOpt_String(goi, &cp2, NULL);
4274 cp = cp2;
4275 /* now does target type exist */
4276 for (x = 0 ; target_types[x] ; x++) {
4277 if (0 == strcmp(cp, target_types[x]->name)) {
4278 /* found */
4279 break;
4282 if (target_types[x] == NULL) {
4283 Jim_SetResult_sprintf(goi->interp, "Unknown target type %s, try one of ", cp);
4284 for (x = 0 ; target_types[x] ; x++) {
4285 if (target_types[x + 1]) {
4286 Jim_AppendStrings(goi->interp,
4287 Jim_GetResult(goi->interp),
4288 target_types[x]->name,
4289 ", ", NULL);
4290 } else {
4291 Jim_AppendStrings(goi->interp,
4292 Jim_GetResult(goi->interp),
4293 " or ",
4294 target_types[x]->name,NULL);
4297 return JIM_ERR;
4300 /* Create it */
4301 target = calloc(1,sizeof(struct target));
4302 /* set target number */
4303 target->target_number = new_target_number();
4305 /* allocate memory for each unique target type */
4306 target->type = (struct target_type*)calloc(1,sizeof(struct target_type));
4308 memcpy(target->type, target_types[x], sizeof(struct target_type));
4310 /* will be set by "-endian" */
4311 target->endianness = TARGET_ENDIAN_UNKNOWN;
4313 target->working_area = 0x0;
4314 target->working_area_size = 0x0;
4315 target->working_areas = NULL;
4316 target->backup_working_area = 0;
4318 target->state = TARGET_UNKNOWN;
4319 target->debug_reason = DBG_REASON_UNDEFINED;
4320 target->reg_cache = NULL;
4321 target->breakpoints = NULL;
4322 target->watchpoints = NULL;
4323 target->next = NULL;
4324 target->arch_info = NULL;
4326 target->display = 1;
4328 target->halt_issued = false;
4330 /* initialize trace information */
4331 target->trace_info = malloc(sizeof(struct trace));
4332 target->trace_info->num_trace_points = 0;
4333 target->trace_info->trace_points_size = 0;
4334 target->trace_info->trace_points = NULL;
4335 target->trace_info->trace_history_size = 0;
4336 target->trace_info->trace_history = NULL;
4337 target->trace_info->trace_history_pos = 0;
4338 target->trace_info->trace_history_overflowed = 0;
4340 target->dbgmsg = NULL;
4341 target->dbg_msg_enabled = 0;
4343 target->endianness = TARGET_ENDIAN_UNKNOWN;
4345 /* Do the rest as "configure" options */
4346 goi->isconfigure = 1;
4347 e = target_configure(goi, target);
4349 if (target->tap == NULL)
4351 Jim_SetResultString(interp, "-chain-position required when creating target", -1);
4352 e = JIM_ERR;
4355 if (e != JIM_OK) {
4356 free(target->type);
4357 free(target);
4358 return e;
4361 if (target->endianness == TARGET_ENDIAN_UNKNOWN) {
4362 /* default endian to little if not specified */
4363 target->endianness = TARGET_LITTLE_ENDIAN;
4366 /* incase variant is not set */
4367 if (!target->variant)
4368 target->variant = strdup("");
4370 cp = Jim_GetString(new_cmd, NULL);
4371 target->cmd_name = strdup(cp);
4373 /* create the target specific commands */
4374 if (target->type->commands) {
4375 e = register_commands(cmd_ctx, NULL, target->type->commands);
4376 if (ERROR_OK != e)
4377 LOG_ERROR("unable to register '%s' commands", cp);
4379 if (target->type->target_create) {
4380 (*(target->type->target_create))(target, goi->interp);
4383 /* append to end of list */
4385 struct target **tpp;
4386 tpp = &(all_targets);
4387 while (*tpp) {
4388 tpp = &((*tpp)->next);
4390 *tpp = target;
4393 /* now - create the new target name command */
4394 const struct command_registration target_command = {
4395 .name = cp,
4396 .jim_handler = &tcl_target_func,
4397 .jim_handler_data = target,
4398 .help = "target command group",
4400 struct command *c = register_command(cmd_ctx, NULL, &target_command);
4401 return (NULL != c) ? ERROR_OK : ERROR_FAIL;
4404 static int jim_target(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
4406 int x,r,e;
4407 jim_wide w;
4408 struct command_context *cmd_ctx;
4409 struct target *target;
4410 Jim_GetOptInfo goi;
4411 enum tcmd {
4412 /* TG = target generic */
4413 TG_CMD_CREATE,
4414 TG_CMD_TYPES,
4415 TG_CMD_NAMES,
4416 TG_CMD_CURRENT,
4417 TG_CMD_NUMBER,
4418 TG_CMD_COUNT,
4420 const char *target_cmds[] = {
4421 "create", "types", "names", "current", "number",
4422 "count",
4423 NULL /* terminate */
4426 LOG_DEBUG("Target command params:");
4427 LOG_DEBUG("%s", Jim_Debug_ArgvString(interp, argc, argv));
4429 cmd_ctx = Jim_GetAssocData(interp, "context");
4431 Jim_GetOpt_Setup(&goi, interp, argc-1, argv + 1);
4433 if (goi.argc == 0) {
4434 Jim_WrongNumArgs(interp, 1, argv, "missing: command ...");
4435 return JIM_ERR;
4438 /* Jim_GetOpt_Debug(&goi); */
4439 r = Jim_GetOpt_Enum(&goi, target_cmds, &x);
4440 if (r != JIM_OK) {
4441 return r;
4444 switch (x) {
4445 default:
4446 Jim_Panic(goi.interp,"Why am I here?");
4447 return JIM_ERR;
4448 case TG_CMD_CURRENT:
4449 if (goi.argc != 0) {
4450 Jim_WrongNumArgs(goi.interp, 1, goi.argv, "Too many parameters");
4451 return JIM_ERR;
4453 Jim_SetResultString(goi.interp,
4454 target_name(get_current_target(cmd_ctx)),
4455 -1);
4456 return JIM_OK;
4457 case TG_CMD_TYPES:
4458 if (goi.argc != 0) {
4459 Jim_WrongNumArgs(goi.interp, 1, goi.argv, "Too many parameters");
4460 return JIM_ERR;
4462 Jim_SetResult(goi.interp, Jim_NewListObj(goi.interp, NULL, 0));
4463 for (x = 0 ; target_types[x] ; x++) {
4464 Jim_ListAppendElement(goi.interp,
4465 Jim_GetResult(goi.interp),
4466 Jim_NewStringObj(goi.interp, target_types[x]->name, -1));
4468 return JIM_OK;
4469 case TG_CMD_NAMES:
4470 if (goi.argc != 0) {
4471 Jim_WrongNumArgs(goi.interp, 1, goi.argv, "Too many parameters");
4472 return JIM_ERR;
4474 Jim_SetResult(goi.interp, Jim_NewListObj(goi.interp, NULL, 0));
4475 target = all_targets;
4476 while (target) {
4477 Jim_ListAppendElement(goi.interp,
4478 Jim_GetResult(goi.interp),
4479 Jim_NewStringObj(goi.interp,
4480 target_name(target), -1));
4481 target = target->next;
4483 return JIM_OK;
4484 case TG_CMD_CREATE:
4485 if (goi.argc < 3) {
4486 Jim_WrongNumArgs(goi.interp, goi.argc, goi.argv, "?name ... config options ...");
4487 return JIM_ERR;
4489 return target_create(&goi);
4490 break;
4491 case TG_CMD_NUMBER:
4492 /* It's OK to remove this mechanism sometime after August 2010 or so */
4493 LOG_WARNING("don't use numbers as target identifiers; use names");
4494 if (goi.argc != 1) {
4495 Jim_SetResult_sprintf(goi.interp, "expected: target number ?NUMBER?");
4496 return JIM_ERR;
4498 e = Jim_GetOpt_Wide(&goi, &w);
4499 if (e != JIM_OK) {
4500 return JIM_ERR;
4502 for (x = 0, target = all_targets; target; target = target->next, x++) {
4503 if (target->target_number == w)
4504 break;
4506 if (target == NULL) {
4507 Jim_SetResult_sprintf(goi.interp,
4508 "Target: number %d does not exist", (int)(w));
4509 return JIM_ERR;
4511 Jim_SetResultString(goi.interp, target_name(target), -1);
4512 return JIM_OK;
4513 case TG_CMD_COUNT:
4514 if (goi.argc != 0) {
4515 Jim_WrongNumArgs(goi.interp, 0, goi.argv, "<no parameters>");
4516 return JIM_ERR;
4518 for (x = 0, target = all_targets; target; target = target->next, x++)
4519 continue;
4520 Jim_SetResult(goi.interp, Jim_NewIntObj(goi.interp, x));
4521 return JIM_OK;
4524 return JIM_ERR;
4528 struct FastLoad
4530 uint32_t address;
4531 uint8_t *data;
4532 int length;
4536 static int fastload_num;
4537 static struct FastLoad *fastload;
4539 static void free_fastload(void)
4541 if (fastload != NULL)
4543 int i;
4544 for (i = 0; i < fastload_num; i++)
4546 if (fastload[i].data)
4547 free(fastload[i].data);
4549 free(fastload);
4550 fastload = NULL;
4557 COMMAND_HANDLER(handle_fast_load_image_command)
4559 uint8_t *buffer;
4560 size_t buf_cnt;
4561 uint32_t image_size;
4562 uint32_t min_address = 0;
4563 uint32_t max_address = 0xffffffff;
4564 int i;
4566 struct image image;
4568 int retval = CALL_COMMAND_HANDLER(parse_load_image_command_CMD_ARGV,
4569 &image, &min_address, &max_address);
4570 if (ERROR_OK != retval)
4571 return retval;
4573 struct duration bench;
4574 duration_start(&bench);
4576 if (image_open(&image, CMD_ARGV[0], (CMD_ARGC >= 3) ? CMD_ARGV[2] : NULL) != ERROR_OK)
4578 return ERROR_OK;
4581 image_size = 0x0;
4582 retval = ERROR_OK;
4583 fastload_num = image.num_sections;
4584 fastload = (struct FastLoad *)malloc(sizeof(struct FastLoad)*image.num_sections);
4585 if (fastload == NULL)
4587 image_close(&image);
4588 return ERROR_FAIL;
4590 memset(fastload, 0, sizeof(struct FastLoad)*image.num_sections);
4591 for (i = 0; i < image.num_sections; i++)
4593 buffer = malloc(image.sections[i].size);
4594 if (buffer == NULL)
4596 command_print(CMD_CTX, "error allocating buffer for section (%d bytes)",
4597 (int)(image.sections[i].size));
4598 break;
4601 if ((retval = image_read_section(&image, i, 0x0, image.sections[i].size, buffer, &buf_cnt)) != ERROR_OK)
4603 free(buffer);
4604 break;
4607 uint32_t offset = 0;
4608 uint32_t length = buf_cnt;
4611 /* DANGER!!! beware of unsigned comparision here!!! */
4613 if ((image.sections[i].base_address + buf_cnt >= min_address)&&
4614 (image.sections[i].base_address < max_address))
4616 if (image.sections[i].base_address < min_address)
4618 /* clip addresses below */
4619 offset += min_address-image.sections[i].base_address;
4620 length -= offset;
4623 if (image.sections[i].base_address + buf_cnt > max_address)
4625 length -= (image.sections[i].base_address + buf_cnt)-max_address;
4628 fastload[i].address = image.sections[i].base_address + offset;
4629 fastload[i].data = malloc(length);
4630 if (fastload[i].data == NULL)
4632 free(buffer);
4633 break;
4635 memcpy(fastload[i].data, buffer + offset, length);
4636 fastload[i].length = length;
4638 image_size += length;
4639 command_print(CMD_CTX, "%u bytes written at address 0x%8.8x",
4640 (unsigned int)length,
4641 ((unsigned int)(image.sections[i].base_address + offset)));
4644 free(buffer);
4647 if ((ERROR_OK == retval) && (duration_measure(&bench) == ERROR_OK))
4649 command_print(CMD_CTX, "Loaded %" PRIu32 " bytes "
4650 "in %fs (%0.3f kb/s)", image_size,
4651 duration_elapsed(&bench), duration_kbps(&bench, image_size));
4653 command_print(CMD_CTX,
4654 "WARNING: image has not been loaded to target!"
4655 "You can issue a 'fast_load' to finish loading.");
4658 image_close(&image);
4660 if (retval != ERROR_OK)
4662 free_fastload();
4665 return retval;
4668 COMMAND_HANDLER(handle_fast_load_command)
4670 if (CMD_ARGC > 0)
4671 return ERROR_COMMAND_SYNTAX_ERROR;
4672 if (fastload == NULL)
4674 LOG_ERROR("No image in memory");
4675 return ERROR_FAIL;
4677 int i;
4678 int ms = timeval_ms();
4679 int size = 0;
4680 int retval = ERROR_OK;
4681 for (i = 0; i < fastload_num;i++)
4683 struct target *target = get_current_target(CMD_CTX);
4684 command_print(CMD_CTX, "Write to 0x%08x, length 0x%08x",
4685 (unsigned int)(fastload[i].address),
4686 (unsigned int)(fastload[i].length));
4687 if (retval == ERROR_OK)
4689 retval = target_write_buffer(target, fastload[i].address, fastload[i].length, fastload[i].data);
4691 size += fastload[i].length;
4693 int after = timeval_ms();
4694 command_print(CMD_CTX, "Loaded image %f kBytes/s", (float)(size/1024.0)/((float)(after-ms)/1000.0));
4695 return retval;
4698 static int jim_mcrmrc(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
4700 struct command_context *context;
4701 struct target *target;
4702 int retval;
4704 context = Jim_GetAssocData(interp, "context");
4705 if (context == NULL) {
4706 LOG_ERROR("array2mem: no command context");
4707 return JIM_ERR;
4709 target = get_current_target(context);
4710 if (target == NULL) {
4711 LOG_ERROR("array2mem: no current target");
4712 return JIM_ERR;
4715 if ((argc < 6) || (argc > 7))
4717 return JIM_ERR;
4720 int cpnum;
4721 uint32_t op1;
4722 uint32_t op2;
4723 uint32_t CRn;
4724 uint32_t CRm;
4725 uint32_t value;
4727 int e;
4728 long l;
4729 e = Jim_GetLong(interp, argv[1], &l);
4730 if (e != JIM_OK) {
4731 return e;
4733 cpnum = l;
4735 e = Jim_GetLong(interp, argv[2], &l);
4736 if (e != JIM_OK) {
4737 return e;
4739 op1 = l;
4741 e = Jim_GetLong(interp, argv[3], &l);
4742 if (e != JIM_OK) {
4743 return e;
4745 CRn = l;
4747 e = Jim_GetLong(interp, argv[4], &l);
4748 if (e != JIM_OK) {
4749 return e;
4751 CRm = l;
4753 e = Jim_GetLong(interp, argv[5], &l);
4754 if (e != JIM_OK) {
4755 return e;
4757 op2 = l;
4759 value = 0;
4761 if (argc == 7)
4763 e = Jim_GetLong(interp, argv[6], &l);
4764 if (e != JIM_OK) {
4765 return e;
4767 value = l;
4769 retval = target_mcr(target, cpnum, op1, op2, CRn, CRm, value);
4770 if (retval != ERROR_OK)
4771 return JIM_ERR;
4772 } else
4774 retval = target_mrc(target, cpnum, op1, op2, CRn, CRm, &value);
4775 if (retval != ERROR_OK)
4776 return JIM_ERR;
4778 Jim_SetResult(interp, Jim_NewIntObj(interp, value));
4781 return JIM_OK;
4784 static const struct command_registration target_command_handlers[] = {
4786 .name = "targets",
4787 .handler = &handle_targets_command,
4788 .mode = COMMAND_ANY,
4789 .help = "change current command line target (one parameter) "
4790 "or list targets (no parameters)",
4791 .usage = "[<new_current_target>]",
4794 .name = "target",
4795 .mode = COMMAND_CONFIG,
4796 .jim_handler = &jim_target,
4797 .help = "configure target",
4799 COMMAND_REGISTRATION_DONE
4802 int target_register_commands(struct command_context *cmd_ctx)
4804 return register_commands(cmd_ctx, NULL, target_command_handlers);
4807 static const struct command_registration target_exec_command_handlers[] = {
4809 .name = "fast_load_image",
4810 .handler = &handle_fast_load_image_command,
4811 .mode = COMMAND_ANY,
4812 .help = "Load image into memory, mainly for profiling purposes",
4813 .usage = "<file> <address> ['bin'|'ihex'|'elf'|'s19'] "
4814 "[min_address] [max_length]",
4817 .name = "fast_load",
4818 .handler = &handle_fast_load_command,
4819 .mode = COMMAND_ANY,
4820 .help = "loads active fast load image to current target "
4821 "- mainly for profiling purposes",
4824 .name = "profile",
4825 .handler = &handle_profile_command,
4826 .mode = COMMAND_EXEC,
4827 .help = "profiling samples the CPU PC",
4829 /** @todo don't register virt2phys() unless target supports it */
4831 .name = "virt2phys",
4832 .handler = &handle_virt2phys_command,
4833 .mode = COMMAND_ANY,
4834 .help = "translate a virtual address into a physical address",
4838 .name = "reg",
4839 .handler = &handle_reg_command,
4840 .mode = COMMAND_EXEC,
4841 .help = "display or set a register",
4845 .name = "poll",
4846 .handler = &handle_poll_command,
4847 .mode = COMMAND_EXEC,
4848 .help = "poll target state",
4851 .name = "wait_halt",
4852 .handler = &handle_wait_halt_command,
4853 .mode = COMMAND_EXEC,
4854 .help = "wait for target halt",
4855 .usage = "[time (s)]",
4858 .name = "halt",
4859 .handler = &handle_halt_command,
4860 .mode = COMMAND_EXEC,
4861 .help = "halt target",
4864 .name = "resume",
4865 .handler = &handle_resume_command,
4866 .mode = COMMAND_EXEC,
4867 .help = "resume target",
4868 .usage = "[<address>]",
4871 .name = "reset",
4872 .handler = &handle_reset_command,
4873 .mode = COMMAND_EXEC,
4874 .usage = "[run|halt|init]",
4875 .help = "Reset all targets into the specified mode."
4876 "Default reset mode is run, if not given.",
4879 .name = "soft_reset_halt",
4880 .handler = &handle_soft_reset_halt_command,
4881 .mode = COMMAND_EXEC,
4882 .help = "halt the target and do a soft reset",
4886 .name = "step",
4887 .handler = &handle_step_command,
4888 .mode = COMMAND_EXEC,
4889 .help = "step one instruction from current PC or [addr]",
4890 .usage = "[<address>]",
4894 .name = "mdw",
4895 .handler = &handle_md_command,
4896 .mode = COMMAND_EXEC,
4897 .help = "display memory words",
4898 .usage = "[phys] <addr> [count]",
4901 .name = "mdh",
4902 .handler = &handle_md_command,
4903 .mode = COMMAND_EXEC,
4904 .help = "display memory half-words",
4905 .usage = "[phys] <addr> [count]",
4908 .name = "mdb",
4909 .handler = &handle_md_command,
4910 .mode = COMMAND_EXEC,
4911 .help = "display memory bytes",
4912 .usage = "[phys] <addr> [count]",
4916 .name = "mww",
4917 .handler = &handle_mw_command,
4918 .mode = COMMAND_EXEC,
4919 .help = "write memory word",
4920 .usage = "[phys] <addr> <value> [count]",
4923 .name = "mwh",
4924 .handler = &handle_mw_command,
4925 .mode = COMMAND_EXEC,
4926 .help = "write memory half-word",
4927 .usage = "[phys] <addr> <value> [count]",
4930 .name = "mwb",
4931 .handler = &handle_mw_command,
4932 .mode = COMMAND_EXEC,
4933 .help = "write memory byte",
4934 .usage = "[phys] <addr> <value> [count]",
4938 .name = "bp",
4939 .handler = &handle_bp_command,
4940 .mode = COMMAND_EXEC,
4941 .help = "list or set breakpoint",
4942 .usage = "[<address> <length> [hw]]",
4945 .name = "rbp",
4946 .handler = &handle_rbp_command,
4947 .mode = COMMAND_EXEC,
4948 .help = "remove breakpoint",
4949 .usage = "<address>",
4953 .name = "wp",
4954 .handler = &handle_wp_command,
4955 .mode = COMMAND_EXEC,
4956 .help = "list or set watchpoint",
4957 .usage = "[<address> <length> <r/w/a> [value] [mask]]",
4960 .name = "rwp",
4961 .handler = &handle_rwp_command,
4962 .mode = COMMAND_EXEC,
4963 .help = "remove watchpoint",
4964 .usage = "<address>",
4968 .name = "load_image",
4969 .handler = &handle_load_image_command,
4970 .mode = COMMAND_EXEC,
4971 .usage = "<file> <address> ['bin'|'ihex'|'elf'|'s19'] "
4972 "[min_address] [max_length]",
4975 .name = "dump_image",
4976 .handler = &handle_dump_image_command,
4977 .mode = COMMAND_EXEC,
4978 .usage = "<file> <address> <size>",
4981 .name = "verify_image",
4982 .handler = &handle_verify_image_command,
4983 .mode = COMMAND_EXEC,
4984 .usage = "<file> [offset] [type]",
4987 .name = "test_image",
4988 .handler = &handle_test_image_command,
4989 .mode = COMMAND_EXEC,
4990 .usage = "<file> [offset] [type]",
4993 .name = "ocd_mem2array",
4994 .mode = COMMAND_EXEC,
4995 .jim_handler = &jim_mem2array,
4996 .help = "read memory and return as a TCL array "
4997 "for script processing",
4998 .usage = "<arrayname> <width=32|16|8> <address> <count>",
5001 .name = "ocd_array2mem",
5002 .mode = COMMAND_EXEC,
5003 .jim_handler = &jim_array2mem,
5004 .help = "convert a TCL array to memory locations "
5005 "and write the values",
5006 .usage = "<arrayname> <width=32|16|8> <address> <count>",
5008 COMMAND_REGISTRATION_DONE
5010 int target_register_user_commands(struct command_context *cmd_ctx)
5012 int retval = ERROR_OK;
5013 if ((retval = target_request_register_commands(cmd_ctx)) != ERROR_OK)
5014 return retval;
5016 if ((retval = trace_register_commands(cmd_ctx)) != ERROR_OK)
5017 return retval;
5020 return register_commands(cmd_ctx, NULL, target_exec_command_handlers);