Invalid command syntax errors with MWW.
[openocd.git] / src / target / target.c
blob6ecf16a1e887750fb08a35b3c1a62a0744d94612
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 "time_support.h"
40 #include "register.h"
41 #include "trace.h"
42 #include "image.h"
43 #include "jtag.h"
46 static int jim_mcrmrc(Jim_Interp *interp, int argc, Jim_Obj *const *argv);
48 static int target_array2mem(Jim_Interp *interp, struct target *target, int argc, Jim_Obj *const *argv);
49 static int target_mem2array(Jim_Interp *interp, struct target *target, int argc, Jim_Obj *const *argv);
51 /* targets */
52 extern struct target_type arm7tdmi_target;
53 extern struct target_type arm720t_target;
54 extern struct target_type arm9tdmi_target;
55 extern struct target_type arm920t_target;
56 extern struct target_type arm966e_target;
57 extern struct target_type arm926ejs_target;
58 extern struct target_type fa526_target;
59 extern struct target_type feroceon_target;
60 extern struct target_type dragonite_target;
61 extern struct target_type xscale_target;
62 extern struct target_type cortexm3_target;
63 extern struct target_type cortexa8_target;
64 extern struct target_type arm11_target;
65 extern struct target_type mips_m4k_target;
66 extern struct target_type avr_target;
68 struct target_type *target_types[] =
70 &arm7tdmi_target,
71 &arm9tdmi_target,
72 &arm920t_target,
73 &arm720t_target,
74 &arm966e_target,
75 &arm926ejs_target,
76 &fa526_target,
77 &feroceon_target,
78 &dragonite_target,
79 &xscale_target,
80 &cortexm3_target,
81 &cortexa8_target,
82 &arm11_target,
83 &mips_m4k_target,
84 &avr_target,
85 NULL,
88 struct target *all_targets = NULL;
89 struct target_event_callback *target_event_callbacks = NULL;
90 struct target_timer_callback *target_timer_callbacks = NULL;
92 const Jim_Nvp nvp_assert[] = {
93 { .name = "assert", NVP_ASSERT },
94 { .name = "deassert", NVP_DEASSERT },
95 { .name = "T", NVP_ASSERT },
96 { .name = "F", NVP_DEASSERT },
97 { .name = "t", NVP_ASSERT },
98 { .name = "f", NVP_DEASSERT },
99 { .name = NULL, .value = -1 }
102 const Jim_Nvp nvp_error_target[] = {
103 { .value = ERROR_TARGET_INVALID, .name = "err-invalid" },
104 { .value = ERROR_TARGET_INIT_FAILED, .name = "err-init-failed" },
105 { .value = ERROR_TARGET_TIMEOUT, .name = "err-timeout" },
106 { .value = ERROR_TARGET_NOT_HALTED, .name = "err-not-halted" },
107 { .value = ERROR_TARGET_FAILURE, .name = "err-failure" },
108 { .value = ERROR_TARGET_UNALIGNED_ACCESS , .name = "err-unaligned-access" },
109 { .value = ERROR_TARGET_DATA_ABORT , .name = "err-data-abort" },
110 { .value = ERROR_TARGET_RESOURCE_NOT_AVAILABLE , .name = "err-resource-not-available" },
111 { .value = ERROR_TARGET_TRANSLATION_FAULT , .name = "err-translation-fault" },
112 { .value = ERROR_TARGET_NOT_RUNNING, .name = "err-not-running" },
113 { .value = ERROR_TARGET_NOT_EXAMINED, .name = "err-not-examined" },
114 { .value = -1, .name = NULL }
117 const char *target_strerror_safe(int err)
119 const Jim_Nvp *n;
121 n = Jim_Nvp_value2name_simple(nvp_error_target, err);
122 if (n->name == NULL) {
123 return "unknown";
124 } else {
125 return n->name;
129 static const Jim_Nvp nvp_target_event[] = {
130 { .value = TARGET_EVENT_OLD_gdb_program_config , .name = "old-gdb_program_config" },
131 { .value = TARGET_EVENT_OLD_pre_resume , .name = "old-pre_resume" },
133 { .value = TARGET_EVENT_GDB_HALT, .name = "gdb-halt" },
134 { .value = TARGET_EVENT_HALTED, .name = "halted" },
135 { .value = TARGET_EVENT_RESUMED, .name = "resumed" },
136 { .value = TARGET_EVENT_RESUME_START, .name = "resume-start" },
137 { .value = TARGET_EVENT_RESUME_END, .name = "resume-end" },
139 { .name = "gdb-start", .value = TARGET_EVENT_GDB_START },
140 { .name = "gdb-end", .value = TARGET_EVENT_GDB_END },
142 /* historical name */
144 { .value = TARGET_EVENT_RESET_START, .name = "reset-start" },
146 { .value = TARGET_EVENT_RESET_ASSERT_PRE, .name = "reset-assert-pre" },
147 { .value = TARGET_EVENT_RESET_ASSERT_POST, .name = "reset-assert-post" },
148 { .value = TARGET_EVENT_RESET_DEASSERT_PRE, .name = "reset-deassert-pre" },
149 { .value = TARGET_EVENT_RESET_DEASSERT_POST, .name = "reset-deassert-post" },
150 { .value = TARGET_EVENT_RESET_HALT_PRE, .name = "reset-halt-pre" },
151 { .value = TARGET_EVENT_RESET_HALT_POST, .name = "reset-halt-post" },
152 { .value = TARGET_EVENT_RESET_WAIT_PRE, .name = "reset-wait-pre" },
153 { .value = TARGET_EVENT_RESET_WAIT_POST, .name = "reset-wait-post" },
154 { .value = TARGET_EVENT_RESET_INIT , .name = "reset-init" },
155 { .value = TARGET_EVENT_RESET_END, .name = "reset-end" },
157 { .value = TARGET_EVENT_EXAMINE_START, .name = "examine-start" },
158 { .value = TARGET_EVENT_EXAMINE_END, .name = "examine-end" },
160 { .value = TARGET_EVENT_DEBUG_HALTED, .name = "debug-halted" },
161 { .value = TARGET_EVENT_DEBUG_RESUMED, .name = "debug-resumed" },
163 { .value = TARGET_EVENT_GDB_ATTACH, .name = "gdb-attach" },
164 { .value = TARGET_EVENT_GDB_DETACH, .name = "gdb-detach" },
166 { .value = TARGET_EVENT_GDB_FLASH_WRITE_START, .name = "gdb-flash-write-start" },
167 { .value = TARGET_EVENT_GDB_FLASH_WRITE_END , .name = "gdb-flash-write-end" },
169 { .value = TARGET_EVENT_GDB_FLASH_ERASE_START, .name = "gdb-flash-erase-start" },
170 { .value = TARGET_EVENT_GDB_FLASH_ERASE_END , .name = "gdb-flash-erase-end" },
172 { .value = TARGET_EVENT_RESUME_START, .name = "resume-start" },
173 { .value = TARGET_EVENT_RESUMED , .name = "resume-ok" },
174 { .value = TARGET_EVENT_RESUME_END , .name = "resume-end" },
176 { .name = NULL, .value = -1 }
179 const Jim_Nvp nvp_target_state[] = {
180 { .name = "unknown", .value = TARGET_UNKNOWN },
181 { .name = "running", .value = TARGET_RUNNING },
182 { .name = "halted", .value = TARGET_HALTED },
183 { .name = "reset", .value = TARGET_RESET },
184 { .name = "debug-running", .value = TARGET_DEBUG_RUNNING },
185 { .name = NULL, .value = -1 },
188 const Jim_Nvp nvp_target_debug_reason [] = {
189 { .name = "debug-request" , .value = DBG_REASON_DBGRQ },
190 { .name = "breakpoint" , .value = DBG_REASON_BREAKPOINT },
191 { .name = "watchpoint" , .value = DBG_REASON_WATCHPOINT },
192 { .name = "watchpoint-and-breakpoint", .value = DBG_REASON_WPTANDBKPT },
193 { .name = "single-step" , .value = DBG_REASON_SINGLESTEP },
194 { .name = "target-not-halted" , .value = DBG_REASON_NOTHALTED },
195 { .name = "undefined" , .value = DBG_REASON_UNDEFINED },
196 { .name = NULL, .value = -1 },
199 const Jim_Nvp nvp_target_endian[] = {
200 { .name = "big", .value = TARGET_BIG_ENDIAN },
201 { .name = "little", .value = TARGET_LITTLE_ENDIAN },
202 { .name = "be", .value = TARGET_BIG_ENDIAN },
203 { .name = "le", .value = TARGET_LITTLE_ENDIAN },
204 { .name = NULL, .value = -1 },
207 const Jim_Nvp nvp_reset_modes[] = {
208 { .name = "unknown", .value = RESET_UNKNOWN },
209 { .name = "run" , .value = RESET_RUN },
210 { .name = "halt" , .value = RESET_HALT },
211 { .name = "init" , .value = RESET_INIT },
212 { .name = NULL , .value = -1 },
215 const char *
216 target_state_name( struct target *t )
218 const char *cp;
219 cp = Jim_Nvp_value2name_simple(nvp_target_state, t->state)->name;
220 if( !cp ){
221 LOG_ERROR("Invalid target state: %d", (int)(t->state));
222 cp = "(*BUG*unknown*BUG*)";
224 return cp;
227 /* determine the number of the new target */
228 static int new_target_number(void)
230 struct target *t;
231 int x;
233 /* number is 0 based */
234 x = -1;
235 t = all_targets;
236 while (t) {
237 if (x < t->target_number) {
238 x = t->target_number;
240 t = t->next;
242 return x + 1;
245 /* read a uint32_t from a buffer in target memory endianness */
246 uint32_t target_buffer_get_u32(struct target *target, const uint8_t *buffer)
248 if (target->endianness == TARGET_LITTLE_ENDIAN)
249 return le_to_h_u32(buffer);
250 else
251 return be_to_h_u32(buffer);
254 /* read a uint16_t from a buffer in target memory endianness */
255 uint16_t target_buffer_get_u16(struct target *target, const uint8_t *buffer)
257 if (target->endianness == TARGET_LITTLE_ENDIAN)
258 return le_to_h_u16(buffer);
259 else
260 return be_to_h_u16(buffer);
263 /* read a uint8_t from a buffer in target memory endianness */
264 uint8_t target_buffer_get_u8(struct target *target, const uint8_t *buffer)
266 return *buffer & 0x0ff;
269 /* write a uint32_t to a buffer in target memory endianness */
270 void target_buffer_set_u32(struct target *target, uint8_t *buffer, uint32_t value)
272 if (target->endianness == TARGET_LITTLE_ENDIAN)
273 h_u32_to_le(buffer, value);
274 else
275 h_u32_to_be(buffer, value);
278 /* write a uint16_t to a buffer in target memory endianness */
279 void target_buffer_set_u16(struct target *target, uint8_t *buffer, uint16_t value)
281 if (target->endianness == TARGET_LITTLE_ENDIAN)
282 h_u16_to_le(buffer, value);
283 else
284 h_u16_to_be(buffer, value);
287 /* write a uint8_t to a buffer in target memory endianness */
288 void target_buffer_set_u8(struct target *target, uint8_t *buffer, uint8_t value)
290 *buffer = value;
293 /* return a pointer to a configured target; id is name or number */
294 struct target *get_target(const char *id)
296 struct target *target;
298 /* try as tcltarget name */
299 for (target = all_targets; target; target = target->next) {
300 if (target->cmd_name == NULL)
301 continue;
302 if (strcmp(id, target->cmd_name) == 0)
303 return target;
306 /* It's OK to remove this fallback sometime after August 2010 or so */
308 /* no match, try as number */
309 unsigned num;
310 if (parse_uint(id, &num) != ERROR_OK)
311 return NULL;
313 for (target = all_targets; target; target = target->next) {
314 if (target->target_number == (int)num) {
315 LOG_WARNING("use '%s' as target identifier, not '%u'",
316 target->cmd_name, num);
317 return target;
321 return NULL;
324 /* returns a pointer to the n-th configured target */
325 static struct target *get_target_by_num(int num)
327 struct target *target = all_targets;
329 while (target) {
330 if (target->target_number == num) {
331 return target;
333 target = target->next;
336 return NULL;
339 struct target* get_current_target(struct command_context *cmd_ctx)
341 struct target *target = get_target_by_num(cmd_ctx->current_target);
343 if (target == NULL)
345 LOG_ERROR("BUG: current_target out of bounds");
346 exit(-1);
349 return target;
352 int target_poll(struct target *target)
354 int retval;
356 /* We can't poll until after examine */
357 if (!target_was_examined(target))
359 /* Fail silently lest we pollute the log */
360 return ERROR_FAIL;
363 retval = target->type->poll(target);
364 if (retval != ERROR_OK)
365 return retval;
367 if (target->halt_issued)
369 if (target->state == TARGET_HALTED)
371 target->halt_issued = false;
372 } else
374 long long t = timeval_ms() - target->halt_issued_time;
375 if (t>1000)
377 target->halt_issued = false;
378 LOG_INFO("Halt timed out, wake up GDB.");
379 target_call_event_callbacks(target, TARGET_EVENT_GDB_HALT);
384 return ERROR_OK;
387 int target_halt(struct target *target)
389 int retval;
390 /* We can't poll until after examine */
391 if (!target_was_examined(target))
393 LOG_ERROR("Target not examined yet");
394 return ERROR_FAIL;
397 retval = target->type->halt(target);
398 if (retval != ERROR_OK)
399 return retval;
401 target->halt_issued = true;
402 target->halt_issued_time = timeval_ms();
404 return ERROR_OK;
407 int target_resume(struct target *target, int current, uint32_t address, int handle_breakpoints, int debug_execution)
409 int retval;
411 /* We can't poll until after examine */
412 if (!target_was_examined(target))
414 LOG_ERROR("Target not examined yet");
415 return ERROR_FAIL;
418 /* note that resume *must* be asynchronous. The CPU can halt before we poll. The CPU can
419 * even halt at the current PC as a result of a software breakpoint being inserted by (a bug?)
420 * the application.
422 if ((retval = target->type->resume(target, current, address, handle_breakpoints, debug_execution)) != ERROR_OK)
423 return retval;
425 return retval;
428 int target_process_reset(struct command_context *cmd_ctx, enum target_reset_mode reset_mode)
430 char buf[100];
431 int retval;
432 Jim_Nvp *n;
433 n = Jim_Nvp_value2name_simple(nvp_reset_modes, reset_mode);
434 if (n->name == NULL) {
435 LOG_ERROR("invalid reset mode");
436 return ERROR_FAIL;
439 /* disable polling during reset to make reset event scripts
440 * more predictable, i.e. dr/irscan & pathmove in events will
441 * not have JTAG operations injected into the middle of a sequence.
443 bool save_poll = jtag_poll_get_enabled();
445 jtag_poll_set_enabled(false);
447 sprintf(buf, "ocd_process_reset %s", n->name);
448 retval = Jim_Eval(interp, buf);
450 jtag_poll_set_enabled(save_poll);
452 if (retval != JIM_OK) {
453 Jim_PrintErrorMessage(interp);
454 return ERROR_FAIL;
457 /* We want any events to be processed before the prompt */
458 retval = target_call_timer_callbacks_now();
460 return retval;
463 static int identity_virt2phys(struct target *target,
464 uint32_t virtual, uint32_t *physical)
466 *physical = virtual;
467 return ERROR_OK;
470 static int no_mmu(struct target *target, int *enabled)
472 *enabled = 0;
473 return ERROR_OK;
476 static int default_examine(struct target *target)
478 target_set_examined(target);
479 return ERROR_OK;
482 int target_examine_one(struct target *target)
484 return target->type->examine(target);
487 static int jtag_enable_callback(enum jtag_event event, void *priv)
489 struct target *target = priv;
491 if (event != JTAG_TAP_EVENT_ENABLE || !target->tap->enabled)
492 return ERROR_OK;
494 jtag_unregister_event_callback(jtag_enable_callback, target);
495 return target_examine_one(target);
499 /* Targets that correctly implement init + examine, i.e.
500 * no communication with target during init:
502 * XScale
504 int target_examine(void)
506 int retval = ERROR_OK;
507 struct target *target;
509 for (target = all_targets; target; target = target->next)
511 /* defer examination, but don't skip it */
512 if (!target->tap->enabled) {
513 jtag_register_event_callback(jtag_enable_callback,
514 target);
515 continue;
517 if ((retval = target_examine_one(target)) != ERROR_OK)
518 return retval;
520 return retval;
522 const char *target_get_name(struct target *target)
524 return target->type->name;
527 static int target_write_memory_imp(struct target *target, uint32_t address, uint32_t size, uint32_t count, uint8_t *buffer)
529 if (!target_was_examined(target))
531 LOG_ERROR("Target not examined yet");
532 return ERROR_FAIL;
534 return target->type->write_memory_imp(target, address, size, count, buffer);
537 static int target_read_memory_imp(struct target *target, uint32_t address, uint32_t size, uint32_t count, uint8_t *buffer)
539 if (!target_was_examined(target))
541 LOG_ERROR("Target not examined yet");
542 return ERROR_FAIL;
544 return target->type->read_memory_imp(target, address, size, count, buffer);
547 static int target_soft_reset_halt_imp(struct target *target)
549 if (!target_was_examined(target))
551 LOG_ERROR("Target not examined yet");
552 return ERROR_FAIL;
554 if (!target->type->soft_reset_halt_imp) {
555 LOG_ERROR("Target %s does not support soft_reset_halt",
556 target->cmd_name);
557 return ERROR_FAIL;
559 return target->type->soft_reset_halt_imp(target);
562 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)
564 if (!target_was_examined(target))
566 LOG_ERROR("Target not examined yet");
567 return ERROR_FAIL;
569 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);
572 int target_read_memory(struct target *target,
573 uint32_t address, uint32_t size, uint32_t count, uint8_t *buffer)
575 return target->type->read_memory(target, address, size, count, buffer);
578 int target_read_phys_memory(struct target *target,
579 uint32_t address, uint32_t size, uint32_t count, uint8_t *buffer)
581 return target->type->read_phys_memory(target, address, size, count, buffer);
584 int target_write_memory(struct target *target,
585 uint32_t address, uint32_t size, uint32_t count, uint8_t *buffer)
587 return target->type->write_memory(target, address, size, count, buffer);
590 int target_write_phys_memory(struct target *target,
591 uint32_t address, uint32_t size, uint32_t count, uint8_t *buffer)
593 return target->type->write_phys_memory(target, address, size, count, buffer);
596 int target_bulk_write_memory(struct target *target,
597 uint32_t address, uint32_t count, uint8_t *buffer)
599 return target->type->bulk_write_memory(target, address, count, buffer);
602 int target_add_breakpoint(struct target *target,
603 struct breakpoint *breakpoint)
605 return target->type->add_breakpoint(target, breakpoint);
607 int target_remove_breakpoint(struct target *target,
608 struct breakpoint *breakpoint)
610 return target->type->remove_breakpoint(target, breakpoint);
613 int target_add_watchpoint(struct target *target,
614 struct watchpoint *watchpoint)
616 return target->type->add_watchpoint(target, watchpoint);
618 int target_remove_watchpoint(struct target *target,
619 struct watchpoint *watchpoint)
621 return target->type->remove_watchpoint(target, watchpoint);
624 int target_get_gdb_reg_list(struct target *target,
625 struct reg **reg_list[], int *reg_list_size)
627 return target->type->get_gdb_reg_list(target, reg_list, reg_list_size);
629 int target_step(struct target *target,
630 int current, uint32_t address, int handle_breakpoints)
632 return target->type->step(target, current, address, handle_breakpoints);
636 int target_run_algorithm(struct target *target,
637 int num_mem_params, struct mem_param *mem_params,
638 int num_reg_params, struct reg_param *reg_param,
639 uint32_t entry_point, uint32_t exit_point,
640 int timeout_ms, void *arch_info)
642 return target->type->run_algorithm(target,
643 num_mem_params, mem_params, num_reg_params, reg_param,
644 entry_point, exit_point, timeout_ms, arch_info);
647 /// @returns @c true if the target has been examined.
648 bool target_was_examined(struct target *target)
650 return target->type->examined;
652 /// Sets the @c examined flag for the given target.
653 void target_set_examined(struct target *target)
655 target->type->examined = true;
657 // Reset the @c examined flag for the given target.
658 void target_reset_examined(struct target *target)
660 target->type->examined = false;
665 static int default_mrc(struct target *target, int cpnum, uint32_t op1, uint32_t op2, uint32_t CRn, uint32_t CRm, uint32_t *value)
667 LOG_ERROR("Not implemented: %s", __func__);
668 return ERROR_FAIL;
671 static int default_mcr(struct target *target, int cpnum, uint32_t op1, uint32_t op2, uint32_t CRn, uint32_t CRm, uint32_t value)
673 LOG_ERROR("Not implemented: %s", __func__);
674 return ERROR_FAIL;
677 static int arm_cp_check(struct target *target, int cpnum, uint32_t op1, uint32_t op2, uint32_t CRn, uint32_t CRm)
679 /* basic check */
680 if (!target_was_examined(target))
682 LOG_ERROR("Target not examined yet");
683 return ERROR_FAIL;
686 if ((cpnum <0) || (cpnum > 15))
688 LOG_ERROR("Illegal co-processor %d", cpnum);
689 return ERROR_FAIL;
692 if (op1 > 7)
694 LOG_ERROR("Illegal op1");
695 return ERROR_FAIL;
698 if (op2 > 7)
700 LOG_ERROR("Illegal op2");
701 return ERROR_FAIL;
704 if (CRn > 15)
706 LOG_ERROR("Illegal CRn");
707 return ERROR_FAIL;
710 if (CRm > 15)
712 LOG_ERROR("Illegal CRm");
713 return ERROR_FAIL;
716 return ERROR_OK;
719 int target_mrc(struct target *target, int cpnum, uint32_t op1, uint32_t op2, uint32_t CRn, uint32_t CRm, uint32_t *value)
721 int retval;
723 retval = arm_cp_check(target, cpnum, op1, op2, CRn, CRm);
724 if (retval != ERROR_OK)
725 return retval;
727 return target->type->mrc(target, cpnum, op1, op2, CRn, CRm, value);
730 int target_mcr(struct target *target, int cpnum, uint32_t op1, uint32_t op2, uint32_t CRn, uint32_t CRm, uint32_t value)
732 int retval;
734 retval = arm_cp_check(target, cpnum, op1, op2, CRn, CRm);
735 if (retval != ERROR_OK)
736 return retval;
738 return target->type->mcr(target, cpnum, op1, op2, CRn, CRm, value);
741 static int
742 err_read_phys_memory(struct target *target, uint32_t address,
743 uint32_t size, uint32_t count, uint8_t *buffer)
745 LOG_ERROR("Not implemented: %s", __func__);
746 return ERROR_FAIL;
749 static int
750 err_write_phys_memory(struct target *target, uint32_t address,
751 uint32_t size, uint32_t count, uint8_t *buffer)
753 LOG_ERROR("Not implemented: %s", __func__);
754 return ERROR_FAIL;
757 int target_init(struct command_context *cmd_ctx)
759 struct target *target;
760 int retval;
762 for (target = all_targets; target; target = target->next) {
763 struct target_type *type = target->type;
765 target_reset_examined(target);
766 if (target->type->examine == NULL)
768 target->type->examine = default_examine;
771 if ((retval = target->type->init_target(cmd_ctx, target)) != ERROR_OK)
773 LOG_ERROR("target '%s' init failed", target_get_name(target));
774 return retval;
778 * @todo MCR/MRC are ARM-specific; don't require them in
779 * all targets, or for ARMs without coprocessors.
781 if (target->type->mcr == NULL)
783 target->type->mcr = default_mcr;
784 } else
786 /* FIX! multiple targets will generally register global commands
787 * multiple times. Only register this one if *one* of the
788 * targets need the command. Hmm... make it a command on the
789 * Jim Tcl target object?
791 register_jim(cmd_ctx, "mcr", jim_mcrmrc, "write coprocessor <cpnum> <op1> <op2> <CRn> <CRm> <value>");
794 if (target->type->mrc == NULL)
796 target->type->mrc = default_mrc;
797 } else
799 register_jim(cmd_ctx, "mrc", jim_mcrmrc, "read coprocessor <cpnum> <op1> <op2> <CRn> <CRm>");
804 * @todo get rid of those *memory_imp() methods, now that all
805 * callers are using target_*_memory() accessors ... and make
806 * sure the "physical" paths handle the same issues.
809 /* a non-invasive way(in terms of patches) to add some code that
810 * runs before the type->write/read_memory implementation
812 target->type->write_memory_imp = target->type->write_memory;
813 target->type->write_memory = target_write_memory_imp;
814 target->type->read_memory_imp = target->type->read_memory;
815 target->type->read_memory = target_read_memory_imp;
816 target->type->soft_reset_halt_imp = target->type->soft_reset_halt;
817 target->type->soft_reset_halt = target_soft_reset_halt_imp;
818 target->type->run_algorithm_imp = target->type->run_algorithm;
819 target->type->run_algorithm = target_run_algorithm_imp;
821 /* Sanity-check MMU support ... stub in what we must, to help
822 * implement it in stages, but warn if we need to do so.
824 if (type->mmu) {
825 if (type->write_phys_memory == NULL) {
826 LOG_ERROR("type '%s' is missing %s",
827 type->name,
828 "write_phys_memory");
829 type->write_phys_memory = err_write_phys_memory;
831 if (type->read_phys_memory == NULL) {
832 LOG_ERROR("type '%s' is missing %s",
833 type->name,
834 "read_phys_memory");
835 type->read_phys_memory = err_read_phys_memory;
837 if (type->virt2phys == NULL) {
838 LOG_ERROR("type '%s' is missing %s",
839 type->name,
840 "virt2phys");
841 type->virt2phys = identity_virt2phys;
844 /* Make sure no-MMU targets all behave the same: make no
845 * distinction between physical and virtual addresses, and
846 * ensure that virt2phys() is always an identity mapping.
848 } else {
849 if (type->write_phys_memory
850 || type->read_phys_memory
851 || type->virt2phys)
852 LOG_WARNING("type '%s' has broken MMU hooks",
853 type->name);
855 type->mmu = no_mmu;
856 type->write_phys_memory = type->write_memory;
857 type->read_phys_memory = type->read_memory;
858 type->virt2phys = identity_virt2phys;
862 if (all_targets)
864 if ((retval = target_register_user_commands(cmd_ctx)) != ERROR_OK)
865 return retval;
866 if ((retval = target_register_timer_callback(handle_target, 100, 1, NULL)) != ERROR_OK)
867 return retval;
870 return ERROR_OK;
873 int target_register_event_callback(int (*callback)(struct target *target, enum target_event event, void *priv), void *priv)
875 struct target_event_callback **callbacks_p = &target_event_callbacks;
877 if (callback == NULL)
879 return ERROR_INVALID_ARGUMENTS;
882 if (*callbacks_p)
884 while ((*callbacks_p)->next)
885 callbacks_p = &((*callbacks_p)->next);
886 callbacks_p = &((*callbacks_p)->next);
889 (*callbacks_p) = malloc(sizeof(struct target_event_callback));
890 (*callbacks_p)->callback = callback;
891 (*callbacks_p)->priv = priv;
892 (*callbacks_p)->next = NULL;
894 return ERROR_OK;
897 int target_register_timer_callback(int (*callback)(void *priv), int time_ms, int periodic, void *priv)
899 struct target_timer_callback **callbacks_p = &target_timer_callbacks;
900 struct timeval now;
902 if (callback == NULL)
904 return ERROR_INVALID_ARGUMENTS;
907 if (*callbacks_p)
909 while ((*callbacks_p)->next)
910 callbacks_p = &((*callbacks_p)->next);
911 callbacks_p = &((*callbacks_p)->next);
914 (*callbacks_p) = malloc(sizeof(struct target_timer_callback));
915 (*callbacks_p)->callback = callback;
916 (*callbacks_p)->periodic = periodic;
917 (*callbacks_p)->time_ms = time_ms;
919 gettimeofday(&now, NULL);
920 (*callbacks_p)->when.tv_usec = now.tv_usec + (time_ms % 1000) * 1000;
921 time_ms -= (time_ms % 1000);
922 (*callbacks_p)->when.tv_sec = now.tv_sec + (time_ms / 1000);
923 if ((*callbacks_p)->when.tv_usec > 1000000)
925 (*callbacks_p)->when.tv_usec = (*callbacks_p)->when.tv_usec - 1000000;
926 (*callbacks_p)->when.tv_sec += 1;
929 (*callbacks_p)->priv = priv;
930 (*callbacks_p)->next = NULL;
932 return ERROR_OK;
935 int target_unregister_event_callback(int (*callback)(struct target *target, enum target_event event, void *priv), void *priv)
937 struct target_event_callback **p = &target_event_callbacks;
938 struct target_event_callback *c = target_event_callbacks;
940 if (callback == NULL)
942 return ERROR_INVALID_ARGUMENTS;
945 while (c)
947 struct target_event_callback *next = c->next;
948 if ((c->callback == callback) && (c->priv == priv))
950 *p = next;
951 free(c);
952 return ERROR_OK;
954 else
955 p = &(c->next);
956 c = next;
959 return ERROR_OK;
962 int target_unregister_timer_callback(int (*callback)(void *priv), void *priv)
964 struct target_timer_callback **p = &target_timer_callbacks;
965 struct target_timer_callback *c = target_timer_callbacks;
967 if (callback == NULL)
969 return ERROR_INVALID_ARGUMENTS;
972 while (c)
974 struct target_timer_callback *next = c->next;
975 if ((c->callback == callback) && (c->priv == priv))
977 *p = next;
978 free(c);
979 return ERROR_OK;
981 else
982 p = &(c->next);
983 c = next;
986 return ERROR_OK;
989 int target_call_event_callbacks(struct target *target, enum target_event event)
991 struct target_event_callback *callback = target_event_callbacks;
992 struct target_event_callback *next_callback;
994 if (event == TARGET_EVENT_HALTED)
996 /* execute early halted first */
997 target_call_event_callbacks(target, TARGET_EVENT_GDB_HALT);
1000 LOG_DEBUG("target event %i (%s)",
1001 event,
1002 Jim_Nvp_value2name_simple(nvp_target_event, event)->name);
1004 target_handle_event(target, event);
1006 while (callback)
1008 next_callback = callback->next;
1009 callback->callback(target, event, callback->priv);
1010 callback = next_callback;
1013 return ERROR_OK;
1016 static int target_timer_callback_periodic_restart(
1017 struct target_timer_callback *cb, struct timeval *now)
1019 int time_ms = cb->time_ms;
1020 cb->when.tv_usec = now->tv_usec + (time_ms % 1000) * 1000;
1021 time_ms -= (time_ms % 1000);
1022 cb->when.tv_sec = now->tv_sec + time_ms / 1000;
1023 if (cb->when.tv_usec > 1000000)
1025 cb->when.tv_usec = cb->when.tv_usec - 1000000;
1026 cb->when.tv_sec += 1;
1028 return ERROR_OK;
1031 static int target_call_timer_callback(struct target_timer_callback *cb,
1032 struct timeval *now)
1034 cb->callback(cb->priv);
1036 if (cb->periodic)
1037 return target_timer_callback_periodic_restart(cb, now);
1039 return target_unregister_timer_callback(cb->callback, cb->priv);
1042 static int target_call_timer_callbacks_check_time(int checktime)
1044 keep_alive();
1046 struct timeval now;
1047 gettimeofday(&now, NULL);
1049 struct target_timer_callback *callback = target_timer_callbacks;
1050 while (callback)
1052 // cleaning up may unregister and free this callback
1053 struct target_timer_callback *next_callback = callback->next;
1055 bool call_it = callback->callback &&
1056 ((!checktime && callback->periodic) ||
1057 now.tv_sec > callback->when.tv_sec ||
1058 (now.tv_sec == callback->when.tv_sec &&
1059 now.tv_usec >= callback->when.tv_usec));
1061 if (call_it)
1063 int retval = target_call_timer_callback(callback, &now);
1064 if (retval != ERROR_OK)
1065 return retval;
1068 callback = next_callback;
1071 return ERROR_OK;
1074 int target_call_timer_callbacks(void)
1076 return target_call_timer_callbacks_check_time(1);
1079 /* invoke periodic callbacks immediately */
1080 int target_call_timer_callbacks_now(void)
1082 return target_call_timer_callbacks_check_time(0);
1085 int target_alloc_working_area(struct target *target, uint32_t size, struct working_area **area)
1087 struct working_area *c = target->working_areas;
1088 struct working_area *new_wa = NULL;
1090 /* Reevaluate working area address based on MMU state*/
1091 if (target->working_areas == NULL)
1093 int retval;
1094 int enabled;
1096 retval = target->type->mmu(target, &enabled);
1097 if (retval != ERROR_OK)
1099 return retval;
1102 if (!enabled) {
1103 if (target->working_area_phys_spec) {
1104 LOG_DEBUG("MMU disabled, using physical "
1105 "address for working memory 0x%08x",
1106 (unsigned)target->working_area_phys);
1107 target->working_area = target->working_area_phys;
1108 } else {
1109 LOG_ERROR("No working memory available. "
1110 "Specify -work-area-phys to target.");
1111 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
1113 } else {
1114 if (target->working_area_virt_spec) {
1115 LOG_DEBUG("MMU enabled, using virtual "
1116 "address for working memory 0x%08x",
1117 (unsigned)target->working_area_virt);
1118 target->working_area = target->working_area_virt;
1119 } else {
1120 LOG_ERROR("No working memory available. "
1121 "Specify -work-area-virt to target.");
1122 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
1127 /* only allocate multiples of 4 byte */
1128 if (size % 4)
1130 LOG_ERROR("BUG: code tried to allocate unaligned number of bytes (0x%08x), padding", ((unsigned)(size)));
1131 size = (size + 3) & (~3);
1134 /* see if there's already a matching working area */
1135 while (c)
1137 if ((c->free) && (c->size == size))
1139 new_wa = c;
1140 break;
1142 c = c->next;
1145 /* if not, allocate a new one */
1146 if (!new_wa)
1148 struct working_area **p = &target->working_areas;
1149 uint32_t first_free = target->working_area;
1150 uint32_t free_size = target->working_area_size;
1152 c = target->working_areas;
1153 while (c)
1155 first_free += c->size;
1156 free_size -= c->size;
1157 p = &c->next;
1158 c = c->next;
1161 if (free_size < size)
1163 LOG_WARNING("not enough working area available(requested %u, free %u)",
1164 (unsigned)(size), (unsigned)(free_size));
1165 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
1168 LOG_DEBUG("allocated new working area at address 0x%08x", (unsigned)first_free);
1170 new_wa = malloc(sizeof(struct working_area));
1171 new_wa->next = NULL;
1172 new_wa->size = size;
1173 new_wa->address = first_free;
1175 if (target->backup_working_area)
1177 int retval;
1178 new_wa->backup = malloc(new_wa->size);
1179 if ((retval = target_read_memory(target, new_wa->address, 4, new_wa->size / 4, new_wa->backup)) != ERROR_OK)
1181 free(new_wa->backup);
1182 free(new_wa);
1183 return retval;
1186 else
1188 new_wa->backup = NULL;
1191 /* put new entry in list */
1192 *p = new_wa;
1195 /* mark as used, and return the new (reused) area */
1196 new_wa->free = 0;
1197 *area = new_wa;
1199 /* user pointer */
1200 new_wa->user = area;
1202 return ERROR_OK;
1205 int target_free_working_area_restore(struct target *target, struct working_area *area, int restore)
1207 if (area->free)
1208 return ERROR_OK;
1210 if (restore && target->backup_working_area)
1212 int retval;
1213 if ((retval = target_write_memory(target, area->address, 4, area->size / 4, area->backup)) != ERROR_OK)
1214 return retval;
1217 area->free = 1;
1219 /* mark user pointer invalid */
1220 *area->user = NULL;
1221 area->user = NULL;
1223 return ERROR_OK;
1226 int target_free_working_area(struct target *target, struct working_area *area)
1228 return target_free_working_area_restore(target, area, 1);
1231 /* free resources and restore memory, if restoring memory fails,
1232 * free up resources anyway
1234 void target_free_all_working_areas_restore(struct target *target, int restore)
1236 struct working_area *c = target->working_areas;
1238 while (c)
1240 struct working_area *next = c->next;
1241 target_free_working_area_restore(target, c, restore);
1243 if (c->backup)
1244 free(c->backup);
1246 free(c);
1248 c = next;
1251 target->working_areas = NULL;
1254 void target_free_all_working_areas(struct target *target)
1256 target_free_all_working_areas_restore(target, 1);
1259 int target_arch_state(struct target *target)
1261 int retval;
1262 if (target == NULL)
1264 LOG_USER("No target has been configured");
1265 return ERROR_OK;
1268 LOG_USER("target state: %s", target_state_name( target ));
1270 if (target->state != TARGET_HALTED)
1271 return ERROR_OK;
1273 retval = target->type->arch_state(target);
1274 return retval;
1277 /* Single aligned words are guaranteed to use 16 or 32 bit access
1278 * mode respectively, otherwise data is handled as quickly as
1279 * possible
1281 int target_write_buffer(struct target *target, uint32_t address, uint32_t size, uint8_t *buffer)
1283 int retval;
1284 LOG_DEBUG("writing buffer of %i byte at 0x%8.8x",
1285 (int)size, (unsigned)address);
1287 if (!target_was_examined(target))
1289 LOG_ERROR("Target not examined yet");
1290 return ERROR_FAIL;
1293 if (size == 0) {
1294 return ERROR_OK;
1297 if ((address + size - 1) < address)
1299 /* GDB can request this when e.g. PC is 0xfffffffc*/
1300 LOG_ERROR("address + size wrapped(0x%08x, 0x%08x)",
1301 (unsigned)address,
1302 (unsigned)size);
1303 return ERROR_FAIL;
1306 if (((address % 2) == 0) && (size == 2))
1308 return target_write_memory(target, address, 2, 1, buffer);
1311 /* handle unaligned head bytes */
1312 if (address % 4)
1314 uint32_t unaligned = 4 - (address % 4);
1316 if (unaligned > size)
1317 unaligned = size;
1319 if ((retval = target_write_memory(target, address, 1, unaligned, buffer)) != ERROR_OK)
1320 return retval;
1322 buffer += unaligned;
1323 address += unaligned;
1324 size -= unaligned;
1327 /* handle aligned words */
1328 if (size >= 4)
1330 int aligned = size - (size % 4);
1332 /* use bulk writes above a certain limit. This may have to be changed */
1333 if (aligned > 128)
1335 if ((retval = target->type->bulk_write_memory(target, address, aligned / 4, buffer)) != ERROR_OK)
1336 return retval;
1338 else
1340 if ((retval = target_write_memory(target, address, 4, aligned / 4, buffer)) != ERROR_OK)
1341 return retval;
1344 buffer += aligned;
1345 address += aligned;
1346 size -= aligned;
1349 /* handle tail writes of less than 4 bytes */
1350 if (size > 0)
1352 if ((retval = target_write_memory(target, address, 1, size, buffer)) != ERROR_OK)
1353 return retval;
1356 return ERROR_OK;
1359 /* Single aligned words are guaranteed to use 16 or 32 bit access
1360 * mode respectively, otherwise data is handled as quickly as
1361 * possible
1363 int target_read_buffer(struct target *target, uint32_t address, uint32_t size, uint8_t *buffer)
1365 int retval;
1366 LOG_DEBUG("reading buffer of %i byte at 0x%8.8x",
1367 (int)size, (unsigned)address);
1369 if (!target_was_examined(target))
1371 LOG_ERROR("Target not examined yet");
1372 return ERROR_FAIL;
1375 if (size == 0) {
1376 return ERROR_OK;
1379 if ((address + size - 1) < address)
1381 /* GDB can request this when e.g. PC is 0xfffffffc*/
1382 LOG_ERROR("address + size wrapped(0x%08" PRIx32 ", 0x%08" PRIx32 ")",
1383 address,
1384 size);
1385 return ERROR_FAIL;
1388 if (((address % 2) == 0) && (size == 2))
1390 return target_read_memory(target, address, 2, 1, buffer);
1393 /* handle unaligned head bytes */
1394 if (address % 4)
1396 uint32_t unaligned = 4 - (address % 4);
1398 if (unaligned > size)
1399 unaligned = size;
1401 if ((retval = target_read_memory(target, address, 1, unaligned, buffer)) != ERROR_OK)
1402 return retval;
1404 buffer += unaligned;
1405 address += unaligned;
1406 size -= unaligned;
1409 /* handle aligned words */
1410 if (size >= 4)
1412 int aligned = size - (size % 4);
1414 if ((retval = target_read_memory(target, address, 4, aligned / 4, buffer)) != ERROR_OK)
1415 return retval;
1417 buffer += aligned;
1418 address += aligned;
1419 size -= aligned;
1422 /*prevent byte access when possible (avoid AHB access limitations in some cases)*/
1423 if(size >=2)
1425 int aligned = size - (size%2);
1426 retval = target_read_memory(target, address, 2, aligned / 2, buffer);
1427 if (retval != ERROR_OK)
1428 return retval;
1430 buffer += aligned;
1431 address += aligned;
1432 size -= aligned;
1434 /* handle tail writes of less than 4 bytes */
1435 if (size > 0)
1437 if ((retval = target_read_memory(target, address, 1, size, buffer)) != ERROR_OK)
1438 return retval;
1441 return ERROR_OK;
1444 int target_checksum_memory(struct target *target, uint32_t address, uint32_t size, uint32_t* crc)
1446 uint8_t *buffer;
1447 int retval;
1448 uint32_t i;
1449 uint32_t checksum = 0;
1450 if (!target_was_examined(target))
1452 LOG_ERROR("Target not examined yet");
1453 return ERROR_FAIL;
1456 if ((retval = target->type->checksum_memory(target, address,
1457 size, &checksum)) != ERROR_OK)
1459 buffer = malloc(size);
1460 if (buffer == NULL)
1462 LOG_ERROR("error allocating buffer for section (%d bytes)", (int)size);
1463 return ERROR_INVALID_ARGUMENTS;
1465 retval = target_read_buffer(target, address, size, buffer);
1466 if (retval != ERROR_OK)
1468 free(buffer);
1469 return retval;
1472 /* convert to target endianess */
1473 for (i = 0; i < (size/sizeof(uint32_t)); i++)
1475 uint32_t target_data;
1476 target_data = target_buffer_get_u32(target, &buffer[i*sizeof(uint32_t)]);
1477 target_buffer_set_u32(target, &buffer[i*sizeof(uint32_t)], target_data);
1480 retval = image_calculate_checksum(buffer, size, &checksum);
1481 free(buffer);
1484 *crc = checksum;
1486 return retval;
1489 int target_blank_check_memory(struct target *target, uint32_t address, uint32_t size, uint32_t* blank)
1491 int retval;
1492 if (!target_was_examined(target))
1494 LOG_ERROR("Target not examined yet");
1495 return ERROR_FAIL;
1498 if (target->type->blank_check_memory == 0)
1499 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
1501 retval = target->type->blank_check_memory(target, address, size, blank);
1503 return retval;
1506 int target_read_u32(struct target *target, uint32_t address, uint32_t *value)
1508 uint8_t value_buf[4];
1509 if (!target_was_examined(target))
1511 LOG_ERROR("Target not examined yet");
1512 return ERROR_FAIL;
1515 int retval = target_read_memory(target, address, 4, 1, value_buf);
1517 if (retval == ERROR_OK)
1519 *value = target_buffer_get_u32(target, value_buf);
1520 LOG_DEBUG("address: 0x%8.8" PRIx32 ", value: 0x%8.8" PRIx32 "",
1521 address,
1522 *value);
1524 else
1526 *value = 0x0;
1527 LOG_DEBUG("address: 0x%8.8" PRIx32 " failed",
1528 address);
1531 return retval;
1534 int target_read_u16(struct target *target, uint32_t address, uint16_t *value)
1536 uint8_t value_buf[2];
1537 if (!target_was_examined(target))
1539 LOG_ERROR("Target not examined yet");
1540 return ERROR_FAIL;
1543 int retval = target_read_memory(target, address, 2, 1, value_buf);
1545 if (retval == ERROR_OK)
1547 *value = target_buffer_get_u16(target, value_buf);
1548 LOG_DEBUG("address: 0x%8.8" PRIx32 ", value: 0x%4.4x",
1549 address,
1550 *value);
1552 else
1554 *value = 0x0;
1555 LOG_DEBUG("address: 0x%8.8" PRIx32 " failed",
1556 address);
1559 return retval;
1562 int target_read_u8(struct target *target, uint32_t address, uint8_t *value)
1564 int retval = target_read_memory(target, address, 1, 1, value);
1565 if (!target_was_examined(target))
1567 LOG_ERROR("Target not examined yet");
1568 return ERROR_FAIL;
1571 if (retval == ERROR_OK)
1573 LOG_DEBUG("address: 0x%8.8" PRIx32 ", value: 0x%2.2x",
1574 address,
1575 *value);
1577 else
1579 *value = 0x0;
1580 LOG_DEBUG("address: 0x%8.8" PRIx32 " failed",
1581 address);
1584 return retval;
1587 int target_write_u32(struct target *target, uint32_t address, uint32_t value)
1589 int retval;
1590 uint8_t value_buf[4];
1591 if (!target_was_examined(target))
1593 LOG_ERROR("Target not examined yet");
1594 return ERROR_FAIL;
1597 LOG_DEBUG("address: 0x%8.8" PRIx32 ", value: 0x%8.8" PRIx32 "",
1598 address,
1599 value);
1601 target_buffer_set_u32(target, value_buf, value);
1602 if ((retval = target_write_memory(target, address, 4, 1, value_buf)) != ERROR_OK)
1604 LOG_DEBUG("failed: %i", retval);
1607 return retval;
1610 int target_write_u16(struct target *target, uint32_t address, uint16_t value)
1612 int retval;
1613 uint8_t value_buf[2];
1614 if (!target_was_examined(target))
1616 LOG_ERROR("Target not examined yet");
1617 return ERROR_FAIL;
1620 LOG_DEBUG("address: 0x%8.8" PRIx32 ", value: 0x%8.8x",
1621 address,
1622 value);
1624 target_buffer_set_u16(target, value_buf, value);
1625 if ((retval = target_write_memory(target, address, 2, 1, value_buf)) != ERROR_OK)
1627 LOG_DEBUG("failed: %i", retval);
1630 return retval;
1633 int target_write_u8(struct target *target, uint32_t address, uint8_t value)
1635 int retval;
1636 if (!target_was_examined(target))
1638 LOG_ERROR("Target not examined yet");
1639 return ERROR_FAIL;
1642 LOG_DEBUG("address: 0x%8.8" PRIx32 ", value: 0x%2.2x",
1643 address, value);
1645 if ((retval = target_write_memory(target, address, 1, 1, &value)) != ERROR_OK)
1647 LOG_DEBUG("failed: %i", retval);
1650 return retval;
1653 COMMAND_HANDLER(handle_targets_command)
1655 struct target *target = all_targets;
1657 if (argc == 1)
1659 target = get_target(args[0]);
1660 if (target == NULL) {
1661 command_print(cmd_ctx,"Target: %s is unknown, try one of:\n", args[0]);
1662 goto DumpTargets;
1664 if (!target->tap->enabled) {
1665 command_print(cmd_ctx,"Target: TAP %s is disabled, "
1666 "can't be the current target\n",
1667 target->tap->dotted_name);
1668 return ERROR_FAIL;
1671 cmd_ctx->current_target = target->target_number;
1672 return ERROR_OK;
1674 DumpTargets:
1676 target = all_targets;
1677 command_print(cmd_ctx, " TargetName Type Endian TapName State ");
1678 command_print(cmd_ctx, "-- ------------------ ---------- ------ ------------------ ------------");
1679 while (target)
1681 const char *state;
1682 char marker = ' ';
1684 if (target->tap->enabled)
1685 state = target_state_name( target );
1686 else
1687 state = "tap-disabled";
1689 if (cmd_ctx->current_target == target->target_number)
1690 marker = '*';
1692 /* keep columns lined up to match the headers above */
1693 command_print(cmd_ctx, "%2d%c %-18s %-10s %-6s %-18s %s",
1694 target->target_number,
1695 marker,
1696 target->cmd_name,
1697 target_get_name(target),
1698 Jim_Nvp_value2name_simple(nvp_target_endian,
1699 target->endianness)->name,
1700 target->tap->dotted_name,
1701 state);
1702 target = target->next;
1705 return ERROR_OK;
1708 /* every 300ms we check for reset & powerdropout and issue a "reset halt" if so. */
1710 static int powerDropout;
1711 static int srstAsserted;
1713 static int runPowerRestore;
1714 static int runPowerDropout;
1715 static int runSrstAsserted;
1716 static int runSrstDeasserted;
1718 static int sense_handler(void)
1720 static int prevSrstAsserted = 0;
1721 static int prevPowerdropout = 0;
1723 int retval;
1724 if ((retval = jtag_power_dropout(&powerDropout)) != ERROR_OK)
1725 return retval;
1727 int powerRestored;
1728 powerRestored = prevPowerdropout && !powerDropout;
1729 if (powerRestored)
1731 runPowerRestore = 1;
1734 long long current = timeval_ms();
1735 static long long lastPower = 0;
1736 int waitMore = lastPower + 2000 > current;
1737 if (powerDropout && !waitMore)
1739 runPowerDropout = 1;
1740 lastPower = current;
1743 if ((retval = jtag_srst_asserted(&srstAsserted)) != ERROR_OK)
1744 return retval;
1746 int srstDeasserted;
1747 srstDeasserted = prevSrstAsserted && !srstAsserted;
1749 static long long lastSrst = 0;
1750 waitMore = lastSrst + 2000 > current;
1751 if (srstDeasserted && !waitMore)
1753 runSrstDeasserted = 1;
1754 lastSrst = current;
1757 if (!prevSrstAsserted && srstAsserted)
1759 runSrstAsserted = 1;
1762 prevSrstAsserted = srstAsserted;
1763 prevPowerdropout = powerDropout;
1765 if (srstDeasserted || powerRestored)
1767 /* Other than logging the event we can't do anything here.
1768 * Issuing a reset is a particularly bad idea as we might
1769 * be inside a reset already.
1773 return ERROR_OK;
1776 static void target_call_event_callbacks_all(enum target_event e) {
1777 struct target *target;
1778 target = all_targets;
1779 while (target) {
1780 target_call_event_callbacks(target, e);
1781 target = target->next;
1785 /* process target state changes */
1786 int handle_target(void *priv)
1788 int retval = ERROR_OK;
1790 /* we do not want to recurse here... */
1791 static int recursive = 0;
1792 if (! recursive)
1794 recursive = 1;
1795 sense_handler();
1796 /* danger! running these procedures can trigger srst assertions and power dropouts.
1797 * We need to avoid an infinite loop/recursion here and we do that by
1798 * clearing the flags after running these events.
1800 int did_something = 0;
1801 if (runSrstAsserted)
1803 target_call_event_callbacks_all(TARGET_EVENT_GDB_HALT);
1804 Jim_Eval(interp, "srst_asserted");
1805 did_something = 1;
1807 if (runSrstDeasserted)
1809 Jim_Eval(interp, "srst_deasserted");
1810 did_something = 1;
1812 if (runPowerDropout)
1814 target_call_event_callbacks_all(TARGET_EVENT_GDB_HALT);
1815 Jim_Eval(interp, "power_dropout");
1816 did_something = 1;
1818 if (runPowerRestore)
1820 Jim_Eval(interp, "power_restore");
1821 did_something = 1;
1824 if (did_something)
1826 /* clear detect flags */
1827 sense_handler();
1830 /* clear action flags */
1832 runSrstAsserted = 0;
1833 runSrstDeasserted = 0;
1834 runPowerRestore = 0;
1835 runPowerDropout = 0;
1837 recursive = 0;
1840 /* Poll targets for state changes unless that's globally disabled.
1841 * Skip targets that are currently disabled.
1843 for (struct target *target = all_targets;
1844 is_jtag_poll_safe() && target;
1845 target = target->next)
1847 if (!target->tap->enabled)
1848 continue;
1850 /* only poll target if we've got power and srst isn't asserted */
1851 if (!powerDropout && !srstAsserted)
1853 /* polling may fail silently until the target has been examined */
1854 if ((retval = target_poll(target)) != ERROR_OK)
1856 target_call_event_callbacks(target, TARGET_EVENT_GDB_HALT);
1857 return retval;
1862 return retval;
1865 COMMAND_HANDLER(handle_reg_command)
1867 struct target *target;
1868 struct reg *reg = NULL;
1869 int count = 0;
1870 char *value;
1872 LOG_DEBUG("-");
1874 target = get_current_target(cmd_ctx);
1876 /* list all available registers for the current target */
1877 if (argc == 0)
1879 struct reg_cache *cache = target->reg_cache;
1881 count = 0;
1882 while (cache)
1884 int i;
1886 command_print(cmd_ctx, "===== %s", cache->name);
1888 for (i = 0, reg = cache->reg_list;
1889 i < cache->num_regs;
1890 i++, reg++, count++)
1892 /* only print cached values if they are valid */
1893 if (reg->valid) {
1894 value = buf_to_str(reg->value,
1895 reg->size, 16);
1896 command_print(cmd_ctx,
1897 "(%i) %s (/%" PRIu32 "): 0x%s%s",
1898 count, reg->name,
1899 reg->size, value,
1900 reg->dirty
1901 ? " (dirty)"
1902 : "");
1903 free(value);
1904 } else {
1905 command_print(cmd_ctx, "(%i) %s (/%" PRIu32 ")",
1906 count, reg->name,
1907 reg->size) ;
1910 cache = cache->next;
1913 return ERROR_OK;
1916 /* access a single register by its ordinal number */
1917 if ((args[0][0] >= '0') && (args[0][0] <= '9'))
1919 unsigned num;
1920 COMMAND_PARSE_NUMBER(uint, args[0], num);
1922 struct reg_cache *cache = target->reg_cache;
1923 count = 0;
1924 while (cache)
1926 int i;
1927 for (i = 0; i < cache->num_regs; i++)
1929 if (count++ == (int)num)
1931 reg = &cache->reg_list[i];
1932 break;
1935 if (reg)
1936 break;
1937 cache = cache->next;
1940 if (!reg)
1942 command_print(cmd_ctx, "%i is out of bounds, the current target has only %i registers (0 - %i)", num, count, count - 1);
1943 return ERROR_OK;
1945 } else /* access a single register by its name */
1947 reg = register_get_by_name(target->reg_cache, args[0], 1);
1949 if (!reg)
1951 command_print(cmd_ctx, "register %s not found in current target", args[0]);
1952 return ERROR_OK;
1956 /* display a register */
1957 if ((argc == 1) || ((argc == 2) && !((args[1][0] >= '0') && (args[1][0] <= '9'))))
1959 if ((argc == 2) && (strcmp(args[1], "force") == 0))
1960 reg->valid = 0;
1962 if (reg->valid == 0)
1964 struct reg_arch_type *arch_type = register_get_arch_type(reg->arch_type);
1965 arch_type->get(reg);
1967 value = buf_to_str(reg->value, reg->size, 16);
1968 command_print(cmd_ctx, "%s (/%i): 0x%s", reg->name, (int)(reg->size), value);
1969 free(value);
1970 return ERROR_OK;
1973 /* set register value */
1974 if (argc == 2)
1976 uint8_t *buf = malloc(CEIL(reg->size, 8));
1977 str_to_buf(args[1], strlen(args[1]), buf, reg->size, 0);
1979 struct reg_arch_type *arch_type = register_get_arch_type(reg->arch_type);
1980 arch_type->set(reg, buf);
1982 value = buf_to_str(reg->value, reg->size, 16);
1983 command_print(cmd_ctx, "%s (/%i): 0x%s", reg->name, (int)(reg->size), value);
1984 free(value);
1986 free(buf);
1988 return ERROR_OK;
1991 command_print(cmd_ctx, "usage: reg <#|name> [value]");
1993 return ERROR_OK;
1996 COMMAND_HANDLER(handle_poll_command)
1998 int retval = ERROR_OK;
1999 struct target *target = get_current_target(cmd_ctx);
2001 if (argc == 0)
2003 command_print(cmd_ctx, "background polling: %s",
2004 jtag_poll_get_enabled() ? "on" : "off");
2005 command_print(cmd_ctx, "TAP: %s (%s)",
2006 target->tap->dotted_name,
2007 target->tap->enabled ? "enabled" : "disabled");
2008 if (!target->tap->enabled)
2009 return ERROR_OK;
2010 if ((retval = target_poll(target)) != ERROR_OK)
2011 return retval;
2012 if ((retval = target_arch_state(target)) != ERROR_OK)
2013 return retval;
2016 else if (argc == 1)
2018 if (strcmp(args[0], "on") == 0)
2020 jtag_poll_set_enabled(true);
2022 else if (strcmp(args[0], "off") == 0)
2024 jtag_poll_set_enabled(false);
2026 else
2028 command_print(cmd_ctx, "arg is \"on\" or \"off\"");
2030 } else
2032 return ERROR_COMMAND_SYNTAX_ERROR;
2035 return retval;
2038 COMMAND_HANDLER(handle_wait_halt_command)
2040 if (argc > 1)
2041 return ERROR_COMMAND_SYNTAX_ERROR;
2043 unsigned ms = 5000;
2044 if (1 == argc)
2046 int retval = parse_uint(args[0], &ms);
2047 if (ERROR_OK != retval)
2049 command_print(cmd_ctx, "usage: %s [seconds]", CMD_NAME);
2050 return ERROR_COMMAND_SYNTAX_ERROR;
2052 // convert seconds (given) to milliseconds (needed)
2053 ms *= 1000;
2056 struct target *target = get_current_target(cmd_ctx);
2057 return target_wait_state(target, TARGET_HALTED, ms);
2060 /* wait for target state to change. The trick here is to have a low
2061 * latency for short waits and not to suck up all the CPU time
2062 * on longer waits.
2064 * After 500ms, keep_alive() is invoked
2066 int target_wait_state(struct target *target, enum target_state state, int ms)
2068 int retval;
2069 long long then = 0, cur;
2070 int once = 1;
2072 for (;;)
2074 if ((retval = target_poll(target)) != ERROR_OK)
2075 return retval;
2076 if (target->state == state)
2078 break;
2080 cur = timeval_ms();
2081 if (once)
2083 once = 0;
2084 then = timeval_ms();
2085 LOG_DEBUG("waiting for target %s...",
2086 Jim_Nvp_value2name_simple(nvp_target_state,state)->name);
2089 if (cur-then > 500)
2091 keep_alive();
2094 if ((cur-then) > ms)
2096 LOG_ERROR("timed out while waiting for target %s",
2097 Jim_Nvp_value2name_simple(nvp_target_state,state)->name);
2098 return ERROR_FAIL;
2102 return ERROR_OK;
2105 COMMAND_HANDLER(handle_halt_command)
2107 LOG_DEBUG("-");
2109 struct target *target = get_current_target(cmd_ctx);
2110 int retval = target_halt(target);
2111 if (ERROR_OK != retval)
2112 return retval;
2114 if (argc == 1)
2116 unsigned wait;
2117 retval = parse_uint(args[0], &wait);
2118 if (ERROR_OK != retval)
2119 return ERROR_COMMAND_SYNTAX_ERROR;
2120 if (!wait)
2121 return ERROR_OK;
2124 return CALL_COMMAND_HANDLER(handle_wait_halt_command);
2127 COMMAND_HANDLER(handle_soft_reset_halt_command)
2129 struct target *target = get_current_target(cmd_ctx);
2131 LOG_USER("requesting target halt and executing a soft reset");
2133 target->type->soft_reset_halt(target);
2135 return ERROR_OK;
2138 COMMAND_HANDLER(handle_reset_command)
2140 if (argc > 1)
2141 return ERROR_COMMAND_SYNTAX_ERROR;
2143 enum target_reset_mode reset_mode = RESET_RUN;
2144 if (argc == 1)
2146 const Jim_Nvp *n;
2147 n = Jim_Nvp_name2value_simple(nvp_reset_modes, args[0]);
2148 if ((n->name == NULL) || (n->value == RESET_UNKNOWN)) {
2149 return ERROR_COMMAND_SYNTAX_ERROR;
2151 reset_mode = n->value;
2154 /* reset *all* targets */
2155 return target_process_reset(cmd_ctx, reset_mode);
2159 COMMAND_HANDLER(handle_resume_command)
2161 int current = 1;
2162 if (argc > 1)
2163 return ERROR_COMMAND_SYNTAX_ERROR;
2165 struct target *target = get_current_target(cmd_ctx);
2166 target_handle_event(target, TARGET_EVENT_OLD_pre_resume);
2168 /* with no args, resume from current pc, addr = 0,
2169 * with one arguments, addr = args[0],
2170 * handle breakpoints, not debugging */
2171 uint32_t addr = 0;
2172 if (argc == 1)
2174 COMMAND_PARSE_NUMBER(u32, args[0], addr);
2175 current = 0;
2178 return target_resume(target, current, addr, 1, 0);
2181 COMMAND_HANDLER(handle_step_command)
2183 if (argc > 1)
2184 return ERROR_COMMAND_SYNTAX_ERROR;
2186 LOG_DEBUG("-");
2188 /* with no args, step from current pc, addr = 0,
2189 * with one argument addr = args[0],
2190 * handle breakpoints, debugging */
2191 uint32_t addr = 0;
2192 int current_pc = 1;
2193 if (argc == 1)
2195 COMMAND_PARSE_NUMBER(u32, args[0], addr);
2196 current_pc = 0;
2199 struct target *target = get_current_target(cmd_ctx);
2201 return target->type->step(target, current_pc, addr, 1);
2204 static void handle_md_output(struct command_context *cmd_ctx,
2205 struct target *target, uint32_t address, unsigned size,
2206 unsigned count, const uint8_t *buffer)
2208 const unsigned line_bytecnt = 32;
2209 unsigned line_modulo = line_bytecnt / size;
2211 char output[line_bytecnt * 4 + 1];
2212 unsigned output_len = 0;
2214 const char *value_fmt;
2215 switch (size) {
2216 case 4: value_fmt = "%8.8x "; break;
2217 case 2: value_fmt = "%4.2x "; break;
2218 case 1: value_fmt = "%2.2x "; break;
2219 default:
2220 LOG_ERROR("invalid memory read size: %u", size);
2221 exit(-1);
2224 for (unsigned i = 0; i < count; i++)
2226 if (i % line_modulo == 0)
2228 output_len += snprintf(output + output_len,
2229 sizeof(output) - output_len,
2230 "0x%8.8x: ",
2231 (unsigned)(address + (i*size)));
2234 uint32_t value = 0;
2235 const uint8_t *value_ptr = buffer + i * size;
2236 switch (size) {
2237 case 4: value = target_buffer_get_u32(target, value_ptr); break;
2238 case 2: value = target_buffer_get_u16(target, value_ptr); break;
2239 case 1: value = *value_ptr;
2241 output_len += snprintf(output + output_len,
2242 sizeof(output) - output_len,
2243 value_fmt, value);
2245 if ((i % line_modulo == line_modulo - 1) || (i == count - 1))
2247 command_print(cmd_ctx, "%s", output);
2248 output_len = 0;
2253 COMMAND_HANDLER(handle_md_command)
2255 if (argc < 1)
2256 return ERROR_COMMAND_SYNTAX_ERROR;
2258 unsigned size = 0;
2259 switch (CMD_NAME[2]) {
2260 case 'w': size = 4; break;
2261 case 'h': size = 2; break;
2262 case 'b': size = 1; break;
2263 default: return ERROR_COMMAND_SYNTAX_ERROR;
2266 bool physical=strcmp(args[0], "phys")==0;
2267 int (*fn)(struct target *target,
2268 uint32_t address, uint32_t size, uint32_t count, uint8_t *buffer);
2269 if (physical)
2271 argc--;
2272 args++;
2273 fn=target_read_phys_memory;
2274 } else
2276 fn=target_read_memory;
2278 if ((argc < 1) || (argc > 2))
2280 return ERROR_COMMAND_SYNTAX_ERROR;
2283 uint32_t address;
2284 COMMAND_PARSE_NUMBER(u32, args[0], address);
2286 unsigned count = 1;
2287 if (argc == 2)
2288 COMMAND_PARSE_NUMBER(uint, args[1], count);
2290 uint8_t *buffer = calloc(count, size);
2292 struct target *target = get_current_target(cmd_ctx);
2293 int retval = fn(target, address, size, count, buffer);
2294 if (ERROR_OK == retval)
2295 handle_md_output(cmd_ctx, target, address, size, count, buffer);
2297 free(buffer);
2299 return retval;
2302 COMMAND_HANDLER(handle_mw_command)
2304 if (argc < 2)
2306 return ERROR_COMMAND_SYNTAX_ERROR;
2308 bool physical=strcmp(args[0], "phys")==0;
2309 int (*fn)(struct target *target,
2310 uint32_t address, uint32_t size, uint32_t count, uint8_t *buffer);
2311 const char *cmd_name = CMD_NAME;
2312 if (physical)
2314 argc--;
2315 args++;
2316 fn=target_write_phys_memory;
2317 } else
2319 fn=target_write_memory;
2321 if ((argc < 2) || (argc > 3))
2322 return ERROR_COMMAND_SYNTAX_ERROR;
2324 uint32_t address;
2325 COMMAND_PARSE_NUMBER(u32, args[0], address);
2327 uint32_t value;
2328 COMMAND_PARSE_NUMBER(u32, args[1], value);
2330 unsigned count = 1;
2331 if (argc == 3)
2332 COMMAND_PARSE_NUMBER(uint, args[2], count);
2334 struct target *target = get_current_target(cmd_ctx);
2335 unsigned wordsize;
2336 uint8_t value_buf[4];
2337 switch (cmd_name[6])
2339 case 'w':
2340 wordsize = 4;
2341 target_buffer_set_u32(target, value_buf, value);
2342 break;
2343 case 'h':
2344 wordsize = 2;
2345 target_buffer_set_u16(target, value_buf, value);
2346 break;
2347 case 'b':
2348 wordsize = 1;
2349 value_buf[0] = value;
2350 break;
2351 default:
2352 return ERROR_COMMAND_SYNTAX_ERROR;
2354 for (unsigned i = 0; i < count; i++)
2356 int retval = fn(target,
2357 address + i * wordsize, wordsize, 1, value_buf);
2358 if (ERROR_OK != retval)
2359 return retval;
2360 keep_alive();
2363 return ERROR_OK;
2367 static COMMAND_HELPER(parse_load_image_command_args, struct image *image,
2368 uint32_t *min_address, uint32_t *max_address)
2370 if (argc < 1 || argc > 5)
2371 return ERROR_COMMAND_SYNTAX_ERROR;
2373 /* a base address isn't always necessary,
2374 * default to 0x0 (i.e. don't relocate) */
2375 if (argc >= 2)
2377 uint32_t addr;
2378 COMMAND_PARSE_NUMBER(u32, args[1], addr);
2379 image->base_address = addr;
2380 image->base_address_set = 1;
2382 else
2383 image->base_address_set = 0;
2385 image->start_address_set = 0;
2387 if (argc >= 4)
2389 COMMAND_PARSE_NUMBER(u32, args[3], *min_address);
2391 if (argc == 5)
2393 COMMAND_PARSE_NUMBER(u32, args[4], *max_address);
2394 // use size (given) to find max (required)
2395 *max_address += *min_address;
2398 if (*min_address > *max_address)
2399 return ERROR_COMMAND_SYNTAX_ERROR;
2401 return ERROR_OK;
2404 COMMAND_HANDLER(handle_load_image_command)
2406 uint8_t *buffer;
2407 uint32_t buf_cnt;
2408 uint32_t image_size;
2409 uint32_t min_address = 0;
2410 uint32_t max_address = 0xffffffff;
2411 int i;
2412 struct image image;
2414 int retval = CALL_COMMAND_HANDLER(parse_load_image_command_args,
2415 &image, &min_address, &max_address);
2416 if (ERROR_OK != retval)
2417 return retval;
2419 struct target *target = get_current_target(cmd_ctx);
2421 struct duration bench;
2422 duration_start(&bench);
2424 if (image_open(&image, args[0], (argc >= 3) ? args[2] : NULL) != ERROR_OK)
2426 return ERROR_OK;
2429 image_size = 0x0;
2430 retval = ERROR_OK;
2431 for (i = 0; i < image.num_sections; i++)
2433 buffer = malloc(image.sections[i].size);
2434 if (buffer == NULL)
2436 command_print(cmd_ctx,
2437 "error allocating buffer for section (%d bytes)",
2438 (int)(image.sections[i].size));
2439 break;
2442 if ((retval = image_read_section(&image, i, 0x0, image.sections[i].size, buffer, &buf_cnt)) != ERROR_OK)
2444 free(buffer);
2445 break;
2448 uint32_t offset = 0;
2449 uint32_t length = buf_cnt;
2451 /* DANGER!!! beware of unsigned comparision here!!! */
2453 if ((image.sections[i].base_address + buf_cnt >= min_address)&&
2454 (image.sections[i].base_address < max_address))
2456 if (image.sections[i].base_address < min_address)
2458 /* clip addresses below */
2459 offset += min_address-image.sections[i].base_address;
2460 length -= offset;
2463 if (image.sections[i].base_address + buf_cnt > max_address)
2465 length -= (image.sections[i].base_address + buf_cnt)-max_address;
2468 if ((retval = target_write_buffer(target, image.sections[i].base_address + offset, length, buffer + offset)) != ERROR_OK)
2470 free(buffer);
2471 break;
2473 image_size += length;
2474 command_print(cmd_ctx, "%u bytes written at address 0x%8.8" PRIx32 "",
2475 (unsigned int)length,
2476 image.sections[i].base_address + offset);
2479 free(buffer);
2482 if ((ERROR_OK == retval) && (duration_measure(&bench) == ERROR_OK))
2484 command_print(cmd_ctx, "downloaded %" PRIu32 " bytes "
2485 "in %fs (%0.3f kb/s)", image_size,
2486 duration_elapsed(&bench), duration_kbps(&bench, image_size));
2489 image_close(&image);
2491 return retval;
2495 COMMAND_HANDLER(handle_dump_image_command)
2497 struct fileio fileio;
2499 uint8_t buffer[560];
2500 int retvaltemp;
2503 struct target *target = get_current_target(cmd_ctx);
2505 if (argc != 3)
2507 command_print(cmd_ctx, "usage: dump_image <filename> <address> <size>");
2508 return ERROR_OK;
2511 uint32_t address;
2512 COMMAND_PARSE_NUMBER(u32, args[1], address);
2513 uint32_t size;
2514 COMMAND_PARSE_NUMBER(u32, args[2], size);
2516 if (fileio_open(&fileio, args[0], FILEIO_WRITE, FILEIO_BINARY) != ERROR_OK)
2518 return ERROR_OK;
2521 struct duration bench;
2522 duration_start(&bench);
2524 int retval = ERROR_OK;
2525 while (size > 0)
2527 uint32_t size_written;
2528 uint32_t this_run_size = (size > 560) ? 560 : size;
2529 retval = target_read_buffer(target, address, this_run_size, buffer);
2530 if (retval != ERROR_OK)
2532 break;
2535 retval = fileio_write(&fileio, this_run_size, buffer, &size_written);
2536 if (retval != ERROR_OK)
2538 break;
2541 size -= this_run_size;
2542 address += this_run_size;
2545 if ((retvaltemp = fileio_close(&fileio)) != ERROR_OK)
2546 return retvaltemp;
2548 if ((ERROR_OK == retval) && (duration_measure(&bench) == ERROR_OK))
2550 command_print(cmd_ctx,
2551 "dumped %lld bytes in %fs (%0.3f kb/s)", fileio.size,
2552 duration_elapsed(&bench), duration_kbps(&bench, fileio.size));
2555 return retval;
2558 static COMMAND_HELPER(handle_verify_image_command_internal, int verify)
2560 uint8_t *buffer;
2561 uint32_t buf_cnt;
2562 uint32_t image_size;
2563 int i;
2564 int retval;
2565 uint32_t checksum = 0;
2566 uint32_t mem_checksum = 0;
2568 struct image image;
2570 struct target *target = get_current_target(cmd_ctx);
2572 if (argc < 1)
2574 return ERROR_COMMAND_SYNTAX_ERROR;
2577 if (!target)
2579 LOG_ERROR("no target selected");
2580 return ERROR_FAIL;
2583 struct duration bench;
2584 duration_start(&bench);
2586 if (argc >= 2)
2588 uint32_t addr;
2589 COMMAND_PARSE_NUMBER(u32, args[1], addr);
2590 image.base_address = addr;
2591 image.base_address_set = 1;
2593 else
2595 image.base_address_set = 0;
2596 image.base_address = 0x0;
2599 image.start_address_set = 0;
2601 if ((retval = image_open(&image, args[0], (argc == 3) ? args[2] : NULL)) != ERROR_OK)
2603 return retval;
2606 image_size = 0x0;
2607 retval = ERROR_OK;
2608 for (i = 0; i < image.num_sections; i++)
2610 buffer = malloc(image.sections[i].size);
2611 if (buffer == NULL)
2613 command_print(cmd_ctx,
2614 "error allocating buffer for section (%d bytes)",
2615 (int)(image.sections[i].size));
2616 break;
2618 if ((retval = image_read_section(&image, i, 0x0, image.sections[i].size, buffer, &buf_cnt)) != ERROR_OK)
2620 free(buffer);
2621 break;
2624 if (verify)
2626 /* calculate checksum of image */
2627 image_calculate_checksum(buffer, buf_cnt, &checksum);
2629 retval = target_checksum_memory(target, image.sections[i].base_address, buf_cnt, &mem_checksum);
2630 if (retval != ERROR_OK)
2632 free(buffer);
2633 break;
2636 if (checksum != mem_checksum)
2638 /* failed crc checksum, fall back to a binary compare */
2639 uint8_t *data;
2641 command_print(cmd_ctx, "checksum mismatch - attempting binary compare");
2643 data = (uint8_t*)malloc(buf_cnt);
2645 /* Can we use 32bit word accesses? */
2646 int size = 1;
2647 int count = buf_cnt;
2648 if ((count % 4) == 0)
2650 size *= 4;
2651 count /= 4;
2653 retval = target_read_memory(target, image.sections[i].base_address, size, count, data);
2654 if (retval == ERROR_OK)
2656 uint32_t t;
2657 for (t = 0; t < buf_cnt; t++)
2659 if (data[t] != buffer[t])
2661 command_print(cmd_ctx,
2662 "Verify operation failed address 0x%08x. Was 0x%02x instead of 0x%02x\n",
2663 (unsigned)(t + image.sections[i].base_address),
2664 data[t],
2665 buffer[t]);
2666 free(data);
2667 free(buffer);
2668 retval = ERROR_FAIL;
2669 goto done;
2671 if ((t%16384) == 0)
2673 keep_alive();
2678 free(data);
2680 } else
2682 command_print(cmd_ctx, "address 0x%08" PRIx32 " length 0x%08" PRIx32 "",
2683 image.sections[i].base_address,
2684 buf_cnt);
2687 free(buffer);
2688 image_size += buf_cnt;
2690 done:
2691 if ((ERROR_OK == retval) && (duration_measure(&bench) == ERROR_OK))
2693 command_print(cmd_ctx, "verified %" PRIu32 " bytes "
2694 "in %fs (%0.3f kb/s)", image_size,
2695 duration_elapsed(&bench), duration_kbps(&bench, image_size));
2698 image_close(&image);
2700 return retval;
2703 COMMAND_HANDLER(handle_verify_image_command)
2705 return CALL_COMMAND_HANDLER(handle_verify_image_command_internal, 1);
2708 COMMAND_HANDLER(handle_test_image_command)
2710 return CALL_COMMAND_HANDLER(handle_verify_image_command_internal, 0);
2713 static int handle_bp_command_list(struct command_context *cmd_ctx)
2715 struct target *target = get_current_target(cmd_ctx);
2716 struct breakpoint *breakpoint = target->breakpoints;
2717 while (breakpoint)
2719 if (breakpoint->type == BKPT_SOFT)
2721 char* buf = buf_to_str(breakpoint->orig_instr,
2722 breakpoint->length, 16);
2723 command_print(cmd_ctx, "0x%8.8" PRIx32 ", 0x%x, %i, 0x%s",
2724 breakpoint->address,
2725 breakpoint->length,
2726 breakpoint->set, buf);
2727 free(buf);
2729 else
2731 command_print(cmd_ctx, "0x%8.8" PRIx32 ", 0x%x, %i",
2732 breakpoint->address,
2733 breakpoint->length, breakpoint->set);
2736 breakpoint = breakpoint->next;
2738 return ERROR_OK;
2741 static int handle_bp_command_set(struct command_context *cmd_ctx,
2742 uint32_t addr, uint32_t length, int hw)
2744 struct target *target = get_current_target(cmd_ctx);
2745 int retval = breakpoint_add(target, addr, length, hw);
2746 if (ERROR_OK == retval)
2747 command_print(cmd_ctx, "breakpoint set at 0x%8.8" PRIx32 "", addr);
2748 else
2749 LOG_ERROR("Failure setting breakpoint");
2750 return retval;
2753 COMMAND_HANDLER(handle_bp_command)
2755 if (argc == 0)
2756 return handle_bp_command_list(cmd_ctx);
2758 if (argc < 2 || argc > 3)
2760 command_print(cmd_ctx, "usage: bp <address> <length> ['hw']");
2761 return ERROR_COMMAND_SYNTAX_ERROR;
2764 uint32_t addr;
2765 COMMAND_PARSE_NUMBER(u32, args[0], addr);
2766 uint32_t length;
2767 COMMAND_PARSE_NUMBER(u32, args[1], length);
2769 int hw = BKPT_SOFT;
2770 if (argc == 3)
2772 if (strcmp(args[2], "hw") == 0)
2773 hw = BKPT_HARD;
2774 else
2775 return ERROR_COMMAND_SYNTAX_ERROR;
2778 return handle_bp_command_set(cmd_ctx, addr, length, hw);
2781 COMMAND_HANDLER(handle_rbp_command)
2783 if (argc != 1)
2784 return ERROR_COMMAND_SYNTAX_ERROR;
2786 uint32_t addr;
2787 COMMAND_PARSE_NUMBER(u32, args[0], addr);
2789 struct target *target = get_current_target(cmd_ctx);
2790 breakpoint_remove(target, addr);
2792 return ERROR_OK;
2795 COMMAND_HANDLER(handle_wp_command)
2797 struct target *target = get_current_target(cmd_ctx);
2799 if (argc == 0)
2801 struct watchpoint *watchpoint = target->watchpoints;
2803 while (watchpoint)
2805 command_print(cmd_ctx, "address: 0x%8.8" PRIx32
2806 ", len: 0x%8.8" PRIx32
2807 ", r/w/a: %i, value: 0x%8.8" PRIx32
2808 ", mask: 0x%8.8" PRIx32,
2809 watchpoint->address,
2810 watchpoint->length,
2811 (int)watchpoint->rw,
2812 watchpoint->value,
2813 watchpoint->mask);
2814 watchpoint = watchpoint->next;
2816 return ERROR_OK;
2819 enum watchpoint_rw type = WPT_ACCESS;
2820 uint32_t addr = 0;
2821 uint32_t length = 0;
2822 uint32_t data_value = 0x0;
2823 uint32_t data_mask = 0xffffffff;
2825 switch (argc)
2827 case 5:
2828 COMMAND_PARSE_NUMBER(u32, args[4], data_mask);
2829 // fall through
2830 case 4:
2831 COMMAND_PARSE_NUMBER(u32, args[3], data_value);
2832 // fall through
2833 case 3:
2834 switch (args[2][0])
2836 case 'r':
2837 type = WPT_READ;
2838 break;
2839 case 'w':
2840 type = WPT_WRITE;
2841 break;
2842 case 'a':
2843 type = WPT_ACCESS;
2844 break;
2845 default:
2846 LOG_ERROR("invalid watchpoint mode ('%c')", args[2][0]);
2847 return ERROR_COMMAND_SYNTAX_ERROR;
2849 // fall through
2850 case 2:
2851 COMMAND_PARSE_NUMBER(u32, args[1], length);
2852 COMMAND_PARSE_NUMBER(u32, args[0], addr);
2853 break;
2855 default:
2856 command_print(cmd_ctx, "usage: wp [address length "
2857 "[(r|w|a) [value [mask]]]]");
2858 return ERROR_COMMAND_SYNTAX_ERROR;
2861 int retval = watchpoint_add(target, addr, length, type,
2862 data_value, data_mask);
2863 if (ERROR_OK != retval)
2864 LOG_ERROR("Failure setting watchpoints");
2866 return retval;
2869 COMMAND_HANDLER(handle_rwp_command)
2871 if (argc != 1)
2872 return ERROR_COMMAND_SYNTAX_ERROR;
2874 uint32_t addr;
2875 COMMAND_PARSE_NUMBER(u32, args[0], addr);
2877 struct target *target = get_current_target(cmd_ctx);
2878 watchpoint_remove(target, addr);
2880 return ERROR_OK;
2885 * Translate a virtual address to a physical address.
2887 * The low-level target implementation must have logged a detailed error
2888 * which is forwarded to telnet/GDB session.
2890 COMMAND_HANDLER(handle_virt2phys_command)
2892 if (argc != 1)
2893 return ERROR_COMMAND_SYNTAX_ERROR;
2895 uint32_t va;
2896 COMMAND_PARSE_NUMBER(u32, args[0], va);
2897 uint32_t pa;
2899 struct target *target = get_current_target(cmd_ctx);
2900 int retval = target->type->virt2phys(target, va, &pa);
2901 if (retval == ERROR_OK)
2902 command_print(cmd_ctx, "Physical address 0x%08" PRIx32 "", pa);
2904 return retval;
2907 static void writeData(FILE *f, const void *data, size_t len)
2909 size_t written = fwrite(data, 1, len, f);
2910 if (written != len)
2911 LOG_ERROR("failed to write %zu bytes: %s", len, strerror(errno));
2914 static void writeLong(FILE *f, int l)
2916 int i;
2917 for (i = 0; i < 4; i++)
2919 char c = (l >> (i*8))&0xff;
2920 writeData(f, &c, 1);
2925 static void writeString(FILE *f, char *s)
2927 writeData(f, s, strlen(s));
2930 /* Dump a gmon.out histogram file. */
2931 static void writeGmon(uint32_t *samples, uint32_t sampleNum, const char *filename)
2933 uint32_t i;
2934 FILE *f = fopen(filename, "w");
2935 if (f == NULL)
2936 return;
2937 writeString(f, "gmon");
2938 writeLong(f, 0x00000001); /* Version */
2939 writeLong(f, 0); /* padding */
2940 writeLong(f, 0); /* padding */
2941 writeLong(f, 0); /* padding */
2943 uint8_t zero = 0; /* GMON_TAG_TIME_HIST */
2944 writeData(f, &zero, 1);
2946 /* figure out bucket size */
2947 uint32_t min = samples[0];
2948 uint32_t max = samples[0];
2949 for (i = 0; i < sampleNum; i++)
2951 if (min > samples[i])
2953 min = samples[i];
2955 if (max < samples[i])
2957 max = samples[i];
2961 int addressSpace = (max-min + 1);
2963 static const uint32_t maxBuckets = 256 * 1024; /* maximum buckets. */
2964 uint32_t length = addressSpace;
2965 if (length > maxBuckets)
2967 length = maxBuckets;
2969 int *buckets = malloc(sizeof(int)*length);
2970 if (buckets == NULL)
2972 fclose(f);
2973 return;
2975 memset(buckets, 0, sizeof(int)*length);
2976 for (i = 0; i < sampleNum;i++)
2978 uint32_t address = samples[i];
2979 long long a = address-min;
2980 long long b = length-1;
2981 long long c = addressSpace-1;
2982 int index = (a*b)/c; /* danger!!!! int32 overflows */
2983 buckets[index]++;
2986 /* append binary memory gmon.out &profile_hist_hdr ((char*)&profile_hist_hdr + sizeof(struct gmon_hist_hdr)) */
2987 writeLong(f, min); /* low_pc */
2988 writeLong(f, max); /* high_pc */
2989 writeLong(f, length); /* # of samples */
2990 writeLong(f, 64000000); /* 64MHz */
2991 writeString(f, "seconds");
2992 for (i = 0; i < (15-strlen("seconds")); i++)
2993 writeData(f, &zero, 1);
2994 writeString(f, "s");
2996 /*append binary memory gmon.out profile_hist_data (profile_hist_data + profile_hist_hdr.hist_size) */
2998 char *data = malloc(2*length);
2999 if (data != NULL)
3001 for (i = 0; i < length;i++)
3003 int val;
3004 val = buckets[i];
3005 if (val > 65535)
3007 val = 65535;
3009 data[i*2]=val&0xff;
3010 data[i*2 + 1]=(val >> 8)&0xff;
3012 free(buckets);
3013 writeData(f, data, length * 2);
3014 free(data);
3015 } else
3017 free(buckets);
3020 fclose(f);
3023 /* profiling samples the CPU PC as quickly as OpenOCD is able, which will be used as a random sampling of PC */
3024 COMMAND_HANDLER(handle_profile_command)
3026 struct target *target = get_current_target(cmd_ctx);
3027 struct timeval timeout, now;
3029 gettimeofday(&timeout, NULL);
3030 if (argc != 2)
3032 return ERROR_COMMAND_SYNTAX_ERROR;
3034 unsigned offset;
3035 COMMAND_PARSE_NUMBER(uint, args[0], offset);
3037 timeval_add_time(&timeout, offset, 0);
3039 command_print(cmd_ctx, "Starting profiling. Halting and resuming the target as often as we can...");
3041 static const int maxSample = 10000;
3042 uint32_t *samples = malloc(sizeof(uint32_t)*maxSample);
3043 if (samples == NULL)
3044 return ERROR_OK;
3046 int numSamples = 0;
3047 /* hopefully it is safe to cache! We want to stop/restart as quickly as possible. */
3048 struct reg *reg = register_get_by_name(target->reg_cache, "pc", 1);
3050 for (;;)
3052 int retval;
3053 target_poll(target);
3054 if (target->state == TARGET_HALTED)
3056 uint32_t t=*((uint32_t *)reg->value);
3057 samples[numSamples++]=t;
3058 retval = target_resume(target, 1, 0, 0, 0); /* current pc, addr = 0, do not handle breakpoints, not debugging */
3059 target_poll(target);
3060 alive_sleep(10); /* sleep 10ms, i.e. <100 samples/second. */
3061 } else if (target->state == TARGET_RUNNING)
3063 /* We want to quickly sample the PC. */
3064 if ((retval = target_halt(target)) != ERROR_OK)
3066 free(samples);
3067 return retval;
3069 } else
3071 command_print(cmd_ctx, "Target not halted or running");
3072 retval = ERROR_OK;
3073 break;
3075 if (retval != ERROR_OK)
3077 break;
3080 gettimeofday(&now, NULL);
3081 if ((numSamples >= maxSample) || ((now.tv_sec >= timeout.tv_sec) && (now.tv_usec >= timeout.tv_usec)))
3083 command_print(cmd_ctx, "Profiling completed. %d samples.", numSamples);
3084 if ((retval = target_poll(target)) != ERROR_OK)
3086 free(samples);
3087 return retval;
3089 if (target->state == TARGET_HALTED)
3091 target_resume(target, 1, 0, 0, 0); /* current pc, addr = 0, do not handle breakpoints, not debugging */
3093 if ((retval = target_poll(target)) != ERROR_OK)
3095 free(samples);
3096 return retval;
3098 writeGmon(samples, numSamples, args[1]);
3099 command_print(cmd_ctx, "Wrote %s", args[1]);
3100 break;
3103 free(samples);
3105 return ERROR_OK;
3108 static int new_int_array_element(Jim_Interp * interp, const char *varname, int idx, uint32_t val)
3110 char *namebuf;
3111 Jim_Obj *nameObjPtr, *valObjPtr;
3112 int result;
3114 namebuf = alloc_printf("%s(%d)", varname, idx);
3115 if (!namebuf)
3116 return JIM_ERR;
3118 nameObjPtr = Jim_NewStringObj(interp, namebuf, -1);
3119 valObjPtr = Jim_NewIntObj(interp, val);
3120 if (!nameObjPtr || !valObjPtr)
3122 free(namebuf);
3123 return JIM_ERR;
3126 Jim_IncrRefCount(nameObjPtr);
3127 Jim_IncrRefCount(valObjPtr);
3128 result = Jim_SetVariable(interp, nameObjPtr, valObjPtr);
3129 Jim_DecrRefCount(interp, nameObjPtr);
3130 Jim_DecrRefCount(interp, valObjPtr);
3131 free(namebuf);
3132 /* printf("%s(%d) <= 0%08x\n", varname, idx, val); */
3133 return result;
3136 static int jim_mem2array(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
3138 struct command_context *context;
3139 struct target *target;
3141 context = Jim_GetAssocData(interp, "context");
3142 if (context == NULL)
3144 LOG_ERROR("mem2array: no command context");
3145 return JIM_ERR;
3147 target = get_current_target(context);
3148 if (target == NULL)
3150 LOG_ERROR("mem2array: no current target");
3151 return JIM_ERR;
3154 return target_mem2array(interp, target, argc-1, argv + 1);
3157 static int target_mem2array(Jim_Interp *interp, struct target *target, int argc, Jim_Obj *const *argv)
3159 long l;
3160 uint32_t width;
3161 int len;
3162 uint32_t addr;
3163 uint32_t count;
3164 uint32_t v;
3165 const char *varname;
3166 uint8_t buffer[4096];
3167 int n, e, retval;
3168 uint32_t i;
3170 /* argv[1] = name of array to receive the data
3171 * argv[2] = desired width
3172 * argv[3] = memory address
3173 * argv[4] = count of times to read
3175 if (argc != 4) {
3176 Jim_WrongNumArgs(interp, 1, argv, "varname width addr nelems");
3177 return JIM_ERR;
3179 varname = Jim_GetString(argv[0], &len);
3180 /* given "foo" get space for worse case "foo(%d)" .. add 20 */
3182 e = Jim_GetLong(interp, argv[1], &l);
3183 width = l;
3184 if (e != JIM_OK) {
3185 return e;
3188 e = Jim_GetLong(interp, argv[2], &l);
3189 addr = l;
3190 if (e != JIM_OK) {
3191 return e;
3193 e = Jim_GetLong(interp, argv[3], &l);
3194 len = l;
3195 if (e != JIM_OK) {
3196 return e;
3198 switch (width) {
3199 case 8:
3200 width = 1;
3201 break;
3202 case 16:
3203 width = 2;
3204 break;
3205 case 32:
3206 width = 4;
3207 break;
3208 default:
3209 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3210 Jim_AppendStrings(interp, Jim_GetResult(interp), "Invalid width param, must be 8/16/32", NULL);
3211 return JIM_ERR;
3213 if (len == 0) {
3214 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3215 Jim_AppendStrings(interp, Jim_GetResult(interp), "mem2array: zero width read?", NULL);
3216 return JIM_ERR;
3218 if ((addr + (len * width)) < addr) {
3219 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3220 Jim_AppendStrings(interp, Jim_GetResult(interp), "mem2array: addr + len - wraps to zero?", NULL);
3221 return JIM_ERR;
3223 /* absurd transfer size? */
3224 if (len > 65536) {
3225 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3226 Jim_AppendStrings(interp, Jim_GetResult(interp), "mem2array: absurd > 64K item request", NULL);
3227 return JIM_ERR;
3230 if ((width == 1) ||
3231 ((width == 2) && ((addr & 1) == 0)) ||
3232 ((width == 4) && ((addr & 3) == 0))) {
3233 /* all is well */
3234 } else {
3235 char buf[100];
3236 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3237 sprintf(buf, "mem2array address: 0x%08" PRIx32 " is not aligned for %" PRId32 " byte reads",
3238 addr,
3239 width);
3240 Jim_AppendStrings(interp, Jim_GetResult(interp), buf , NULL);
3241 return JIM_ERR;
3244 /* Transfer loop */
3246 /* index counter */
3247 n = 0;
3248 /* assume ok */
3249 e = JIM_OK;
3250 while (len) {
3251 /* Slurp... in buffer size chunks */
3253 count = len; /* in objects.. */
3254 if (count > (sizeof(buffer)/width)) {
3255 count = (sizeof(buffer)/width);
3258 retval = target_read_memory(target, addr, width, count, buffer);
3259 if (retval != ERROR_OK) {
3260 /* BOO !*/
3261 LOG_ERROR("mem2array: Read @ 0x%08x, w=%d, cnt=%d, failed",
3262 (unsigned int)addr,
3263 (int)width,
3264 (int)count);
3265 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3266 Jim_AppendStrings(interp, Jim_GetResult(interp), "mem2array: cannot read memory", NULL);
3267 e = JIM_ERR;
3268 len = 0;
3269 } else {
3270 v = 0; /* shut up gcc */
3271 for (i = 0 ;i < count ;i++, n++) {
3272 switch (width) {
3273 case 4:
3274 v = target_buffer_get_u32(target, &buffer[i*width]);
3275 break;
3276 case 2:
3277 v = target_buffer_get_u16(target, &buffer[i*width]);
3278 break;
3279 case 1:
3280 v = buffer[i] & 0x0ff;
3281 break;
3283 new_int_array_element(interp, varname, n, v);
3285 len -= count;
3289 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3291 return JIM_OK;
3294 static int get_int_array_element(Jim_Interp * interp, const char *varname, int idx, uint32_t *val)
3296 char *namebuf;
3297 Jim_Obj *nameObjPtr, *valObjPtr;
3298 int result;
3299 long l;
3301 namebuf = alloc_printf("%s(%d)", varname, idx);
3302 if (!namebuf)
3303 return JIM_ERR;
3305 nameObjPtr = Jim_NewStringObj(interp, namebuf, -1);
3306 if (!nameObjPtr)
3308 free(namebuf);
3309 return JIM_ERR;
3312 Jim_IncrRefCount(nameObjPtr);
3313 valObjPtr = Jim_GetVariable(interp, nameObjPtr, JIM_ERRMSG);
3314 Jim_DecrRefCount(interp, nameObjPtr);
3315 free(namebuf);
3316 if (valObjPtr == NULL)
3317 return JIM_ERR;
3319 result = Jim_GetLong(interp, valObjPtr, &l);
3320 /* printf("%s(%d) => 0%08x\n", varname, idx, val); */
3321 *val = l;
3322 return result;
3325 static int jim_array2mem(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
3327 struct command_context *context;
3328 struct target *target;
3330 context = Jim_GetAssocData(interp, "context");
3331 if (context == NULL) {
3332 LOG_ERROR("array2mem: no command context");
3333 return JIM_ERR;
3335 target = get_current_target(context);
3336 if (target == NULL) {
3337 LOG_ERROR("array2mem: no current target");
3338 return JIM_ERR;
3341 return target_array2mem(interp,target, argc-1, argv + 1);
3343 static int target_array2mem(Jim_Interp *interp, struct target *target, int argc, Jim_Obj *const *argv)
3345 long l;
3346 uint32_t width;
3347 int len;
3348 uint32_t addr;
3349 uint32_t count;
3350 uint32_t v;
3351 const char *varname;
3352 uint8_t buffer[4096];
3353 int n, e, retval;
3354 uint32_t i;
3356 /* argv[1] = name of array to get the data
3357 * argv[2] = desired width
3358 * argv[3] = memory address
3359 * argv[4] = count to write
3361 if (argc != 4) {
3362 Jim_WrongNumArgs(interp, 1, argv, "varname width addr nelems");
3363 return JIM_ERR;
3365 varname = Jim_GetString(argv[0], &len);
3366 /* given "foo" get space for worse case "foo(%d)" .. add 20 */
3368 e = Jim_GetLong(interp, argv[1], &l);
3369 width = l;
3370 if (e != JIM_OK) {
3371 return e;
3374 e = Jim_GetLong(interp, argv[2], &l);
3375 addr = l;
3376 if (e != JIM_OK) {
3377 return e;
3379 e = Jim_GetLong(interp, argv[3], &l);
3380 len = l;
3381 if (e != JIM_OK) {
3382 return e;
3384 switch (width) {
3385 case 8:
3386 width = 1;
3387 break;
3388 case 16:
3389 width = 2;
3390 break;
3391 case 32:
3392 width = 4;
3393 break;
3394 default:
3395 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3396 Jim_AppendStrings(interp, Jim_GetResult(interp), "Invalid width param, must be 8/16/32", NULL);
3397 return JIM_ERR;
3399 if (len == 0) {
3400 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3401 Jim_AppendStrings(interp, Jim_GetResult(interp), "array2mem: zero width read?", NULL);
3402 return JIM_ERR;
3404 if ((addr + (len * width)) < addr) {
3405 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3406 Jim_AppendStrings(interp, Jim_GetResult(interp), "array2mem: addr + len - wraps to zero?", NULL);
3407 return JIM_ERR;
3409 /* absurd transfer size? */
3410 if (len > 65536) {
3411 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3412 Jim_AppendStrings(interp, Jim_GetResult(interp), "array2mem: absurd > 64K item request", NULL);
3413 return JIM_ERR;
3416 if ((width == 1) ||
3417 ((width == 2) && ((addr & 1) == 0)) ||
3418 ((width == 4) && ((addr & 3) == 0))) {
3419 /* all is well */
3420 } else {
3421 char buf[100];
3422 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3423 sprintf(buf, "array2mem address: 0x%08x is not aligned for %d byte reads",
3424 (unsigned int)addr,
3425 (int)width);
3426 Jim_AppendStrings(interp, Jim_GetResult(interp), buf , NULL);
3427 return JIM_ERR;
3430 /* Transfer loop */
3432 /* index counter */
3433 n = 0;
3434 /* assume ok */
3435 e = JIM_OK;
3436 while (len) {
3437 /* Slurp... in buffer size chunks */
3439 count = len; /* in objects.. */
3440 if (count > (sizeof(buffer)/width)) {
3441 count = (sizeof(buffer)/width);
3444 v = 0; /* shut up gcc */
3445 for (i = 0 ;i < count ;i++, n++) {
3446 get_int_array_element(interp, varname, n, &v);
3447 switch (width) {
3448 case 4:
3449 target_buffer_set_u32(target, &buffer[i*width], v);
3450 break;
3451 case 2:
3452 target_buffer_set_u16(target, &buffer[i*width], v);
3453 break;
3454 case 1:
3455 buffer[i] = v & 0x0ff;
3456 break;
3459 len -= count;
3461 retval = target_write_memory(target, addr, width, count, buffer);
3462 if (retval != ERROR_OK) {
3463 /* BOO !*/
3464 LOG_ERROR("array2mem: Write @ 0x%08x, w=%d, cnt=%d, failed",
3465 (unsigned int)addr,
3466 (int)width,
3467 (int)count);
3468 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3469 Jim_AppendStrings(interp, Jim_GetResult(interp), "array2mem: cannot read memory", NULL);
3470 e = JIM_ERR;
3471 len = 0;
3475 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3477 return JIM_OK;
3480 void target_all_handle_event(enum target_event e)
3482 struct target *target;
3484 LOG_DEBUG("**all*targets: event: %d, %s",
3485 (int)e,
3486 Jim_Nvp_value2name_simple(nvp_target_event, e)->name);
3488 target = all_targets;
3489 while (target) {
3490 target_handle_event(target, e);
3491 target = target->next;
3496 /* FIX? should we propagate errors here rather than printing them
3497 * and continuing?
3499 void target_handle_event(struct target *target, enum target_event e)
3501 struct target_event_action *teap;
3503 for (teap = target->event_action; teap != NULL; teap = teap->next) {
3504 if (teap->event == e) {
3505 LOG_DEBUG("target: (%d) %s (%s) event: %d (%s) action: %s",
3506 target->target_number,
3507 target->cmd_name,
3508 target_get_name(target),
3510 Jim_Nvp_value2name_simple(nvp_target_event, e)->name,
3511 Jim_GetString(teap->body, NULL));
3512 if (Jim_EvalObj(interp, teap->body) != JIM_OK)
3514 Jim_PrintErrorMessage(interp);
3520 enum target_cfg_param {
3521 TCFG_TYPE,
3522 TCFG_EVENT,
3523 TCFG_WORK_AREA_VIRT,
3524 TCFG_WORK_AREA_PHYS,
3525 TCFG_WORK_AREA_SIZE,
3526 TCFG_WORK_AREA_BACKUP,
3527 TCFG_ENDIAN,
3528 TCFG_VARIANT,
3529 TCFG_CHAIN_POSITION,
3532 static Jim_Nvp nvp_config_opts[] = {
3533 { .name = "-type", .value = TCFG_TYPE },
3534 { .name = "-event", .value = TCFG_EVENT },
3535 { .name = "-work-area-virt", .value = TCFG_WORK_AREA_VIRT },
3536 { .name = "-work-area-phys", .value = TCFG_WORK_AREA_PHYS },
3537 { .name = "-work-area-size", .value = TCFG_WORK_AREA_SIZE },
3538 { .name = "-work-area-backup", .value = TCFG_WORK_AREA_BACKUP },
3539 { .name = "-endian" , .value = TCFG_ENDIAN },
3540 { .name = "-variant", .value = TCFG_VARIANT },
3541 { .name = "-chain-position", .value = TCFG_CHAIN_POSITION },
3543 { .name = NULL, .value = -1 }
3546 static int target_configure(Jim_GetOptInfo *goi, struct target *target)
3548 Jim_Nvp *n;
3549 Jim_Obj *o;
3550 jim_wide w;
3551 char *cp;
3552 int e;
3554 /* parse config or cget options ... */
3555 while (goi->argc > 0) {
3556 Jim_SetEmptyResult(goi->interp);
3557 /* Jim_GetOpt_Debug(goi); */
3559 if (target->type->target_jim_configure) {
3560 /* target defines a configure function */
3561 /* target gets first dibs on parameters */
3562 e = (*(target->type->target_jim_configure))(target, goi);
3563 if (e == JIM_OK) {
3564 /* more? */
3565 continue;
3567 if (e == JIM_ERR) {
3568 /* An error */
3569 return e;
3571 /* otherwise we 'continue' below */
3573 e = Jim_GetOpt_Nvp(goi, nvp_config_opts, &n);
3574 if (e != JIM_OK) {
3575 Jim_GetOpt_NvpUnknown(goi, nvp_config_opts, 0);
3576 return e;
3578 switch (n->value) {
3579 case TCFG_TYPE:
3580 /* not setable */
3581 if (goi->isconfigure) {
3582 Jim_SetResult_sprintf(goi->interp, "not setable: %s", n->name);
3583 return JIM_ERR;
3584 } else {
3585 no_params:
3586 if (goi->argc != 0) {
3587 Jim_WrongNumArgs(goi->interp, goi->argc, goi->argv, "NO PARAMS");
3588 return JIM_ERR;
3591 Jim_SetResultString(goi->interp, target_get_name(target), -1);
3592 /* loop for more */
3593 break;
3594 case TCFG_EVENT:
3595 if (goi->argc == 0) {
3596 Jim_WrongNumArgs(goi->interp, goi->argc, goi->argv, "-event ?event-name? ...");
3597 return JIM_ERR;
3600 e = Jim_GetOpt_Nvp(goi, nvp_target_event, &n);
3601 if (e != JIM_OK) {
3602 Jim_GetOpt_NvpUnknown(goi, nvp_target_event, 1);
3603 return e;
3606 if (goi->isconfigure) {
3607 if (goi->argc != 1) {
3608 Jim_WrongNumArgs(goi->interp, goi->argc, goi->argv, "-event ?event-name? ?EVENT-BODY?");
3609 return JIM_ERR;
3611 } else {
3612 if (goi->argc != 0) {
3613 Jim_WrongNumArgs(goi->interp, goi->argc, goi->argv, "-event ?event-name?");
3614 return JIM_ERR;
3619 struct target_event_action *teap;
3621 teap = target->event_action;
3622 /* replace existing? */
3623 while (teap) {
3624 if (teap->event == (enum target_event)n->value) {
3625 break;
3627 teap = teap->next;
3630 if (goi->isconfigure) {
3631 bool replace = true;
3632 if (teap == NULL) {
3633 /* create new */
3634 teap = calloc(1, sizeof(*teap));
3635 replace = false;
3637 teap->event = n->value;
3638 Jim_GetOpt_Obj(goi, &o);
3639 if (teap->body) {
3640 Jim_DecrRefCount(interp, teap->body);
3642 teap->body = Jim_DuplicateObj(goi->interp, o);
3644 * FIXME:
3645 * Tcl/TK - "tk events" have a nice feature.
3646 * See the "BIND" command.
3647 * We should support that here.
3648 * You can specify %X and %Y in the event code.
3649 * The idea is: %T - target name.
3650 * The idea is: %N - target number
3651 * The idea is: %E - event name.
3653 Jim_IncrRefCount(teap->body);
3655 if (!replace)
3657 /* add to head of event list */
3658 teap->next = target->event_action;
3659 target->event_action = teap;
3661 Jim_SetEmptyResult(goi->interp);
3662 } else {
3663 /* get */
3664 if (teap == NULL) {
3665 Jim_SetEmptyResult(goi->interp);
3666 } else {
3667 Jim_SetResult(goi->interp, Jim_DuplicateObj(goi->interp, teap->body));
3671 /* loop for more */
3672 break;
3674 case TCFG_WORK_AREA_VIRT:
3675 if (goi->isconfigure) {
3676 target_free_all_working_areas(target);
3677 e = Jim_GetOpt_Wide(goi, &w);
3678 if (e != JIM_OK) {
3679 return e;
3681 target->working_area_virt = w;
3682 target->working_area_virt_spec = true;
3683 } else {
3684 if (goi->argc != 0) {
3685 goto no_params;
3688 Jim_SetResult(interp, Jim_NewIntObj(goi->interp, target->working_area_virt));
3689 /* loop for more */
3690 break;
3692 case TCFG_WORK_AREA_PHYS:
3693 if (goi->isconfigure) {
3694 target_free_all_working_areas(target);
3695 e = Jim_GetOpt_Wide(goi, &w);
3696 if (e != JIM_OK) {
3697 return e;
3699 target->working_area_phys = w;
3700 target->working_area_phys_spec = true;
3701 } else {
3702 if (goi->argc != 0) {
3703 goto no_params;
3706 Jim_SetResult(interp, Jim_NewIntObj(goi->interp, target->working_area_phys));
3707 /* loop for more */
3708 break;
3710 case TCFG_WORK_AREA_SIZE:
3711 if (goi->isconfigure) {
3712 target_free_all_working_areas(target);
3713 e = Jim_GetOpt_Wide(goi, &w);
3714 if (e != JIM_OK) {
3715 return e;
3717 target->working_area_size = w;
3718 } else {
3719 if (goi->argc != 0) {
3720 goto no_params;
3723 Jim_SetResult(interp, Jim_NewIntObj(goi->interp, target->working_area_size));
3724 /* loop for more */
3725 break;
3727 case TCFG_WORK_AREA_BACKUP:
3728 if (goi->isconfigure) {
3729 target_free_all_working_areas(target);
3730 e = Jim_GetOpt_Wide(goi, &w);
3731 if (e != JIM_OK) {
3732 return e;
3734 /* make this exactly 1 or 0 */
3735 target->backup_working_area = (!!w);
3736 } else {
3737 if (goi->argc != 0) {
3738 goto no_params;
3741 Jim_SetResult(interp, Jim_NewIntObj(goi->interp, target->backup_working_area));
3742 /* loop for more e*/
3743 break;
3745 case TCFG_ENDIAN:
3746 if (goi->isconfigure) {
3747 e = Jim_GetOpt_Nvp(goi, nvp_target_endian, &n);
3748 if (e != JIM_OK) {
3749 Jim_GetOpt_NvpUnknown(goi, nvp_target_endian, 1);
3750 return e;
3752 target->endianness = n->value;
3753 } else {
3754 if (goi->argc != 0) {
3755 goto no_params;
3758 n = Jim_Nvp_value2name_simple(nvp_target_endian, target->endianness);
3759 if (n->name == NULL) {
3760 target->endianness = TARGET_LITTLE_ENDIAN;
3761 n = Jim_Nvp_value2name_simple(nvp_target_endian, target->endianness);
3763 Jim_SetResultString(goi->interp, n->name, -1);
3764 /* loop for more */
3765 break;
3767 case TCFG_VARIANT:
3768 if (goi->isconfigure) {
3769 if (goi->argc < 1) {
3770 Jim_SetResult_sprintf(goi->interp,
3771 "%s ?STRING?",
3772 n->name);
3773 return JIM_ERR;
3775 if (target->variant) {
3776 free((void *)(target->variant));
3778 e = Jim_GetOpt_String(goi, &cp, NULL);
3779 target->variant = strdup(cp);
3780 } else {
3781 if (goi->argc != 0) {
3782 goto no_params;
3785 Jim_SetResultString(goi->interp, target->variant,-1);
3786 /* loop for more */
3787 break;
3788 case TCFG_CHAIN_POSITION:
3789 if (goi->isconfigure) {
3790 Jim_Obj *o;
3791 struct jtag_tap *tap;
3792 target_free_all_working_areas(target);
3793 e = Jim_GetOpt_Obj(goi, &o);
3794 if (e != JIM_OK) {
3795 return e;
3797 tap = jtag_tap_by_jim_obj(goi->interp, o);
3798 if (tap == NULL) {
3799 return JIM_ERR;
3801 /* make this exactly 1 or 0 */
3802 target->tap = tap;
3803 } else {
3804 if (goi->argc != 0) {
3805 goto no_params;
3808 Jim_SetResultString(interp, target->tap->dotted_name, -1);
3809 /* loop for more e*/
3810 break;
3812 } /* while (goi->argc) */
3815 /* done - we return */
3816 return JIM_OK;
3819 /** this is the 'tcl' handler for the target specific command */
3820 static int tcl_target_func(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
3822 Jim_GetOptInfo goi;
3823 jim_wide a,b,c;
3824 int x,y,z;
3825 uint8_t target_buf[32];
3826 Jim_Nvp *n;
3827 struct target *target;
3828 struct command_context *cmd_ctx;
3829 int e;
3831 enum {
3832 TS_CMD_CONFIGURE,
3833 TS_CMD_CGET,
3835 TS_CMD_MWW, TS_CMD_MWH, TS_CMD_MWB,
3836 TS_CMD_MDW, TS_CMD_MDH, TS_CMD_MDB,
3837 TS_CMD_MRW, TS_CMD_MRH, TS_CMD_MRB,
3838 TS_CMD_MEM2ARRAY, TS_CMD_ARRAY2MEM,
3839 TS_CMD_EXAMINE,
3840 TS_CMD_POLL,
3841 TS_CMD_RESET,
3842 TS_CMD_HALT,
3843 TS_CMD_WAITSTATE,
3844 TS_CMD_EVENTLIST,
3845 TS_CMD_CURSTATE,
3846 TS_CMD_INVOKE_EVENT,
3849 static const Jim_Nvp target_options[] = {
3850 { .name = "configure", .value = TS_CMD_CONFIGURE },
3851 { .name = "cget", .value = TS_CMD_CGET },
3852 { .name = "mww", .value = TS_CMD_MWW },
3853 { .name = "mwh", .value = TS_CMD_MWH },
3854 { .name = "mwb", .value = TS_CMD_MWB },
3855 { .name = "mdw", .value = TS_CMD_MDW },
3856 { .name = "mdh", .value = TS_CMD_MDH },
3857 { .name = "mdb", .value = TS_CMD_MDB },
3858 { .name = "mem2array", .value = TS_CMD_MEM2ARRAY },
3859 { .name = "array2mem", .value = TS_CMD_ARRAY2MEM },
3860 { .name = "eventlist", .value = TS_CMD_EVENTLIST },
3861 { .name = "curstate", .value = TS_CMD_CURSTATE },
3863 { .name = "arp_examine", .value = TS_CMD_EXAMINE },
3864 { .name = "arp_poll", .value = TS_CMD_POLL },
3865 { .name = "arp_reset", .value = TS_CMD_RESET },
3866 { .name = "arp_halt", .value = TS_CMD_HALT },
3867 { .name = "arp_waitstate", .value = TS_CMD_WAITSTATE },
3868 { .name = "invoke-event", .value = TS_CMD_INVOKE_EVENT },
3870 { .name = NULL, .value = -1 },
3873 /* go past the "command" */
3874 Jim_GetOpt_Setup(&goi, interp, argc-1, argv + 1);
3876 target = Jim_CmdPrivData(goi.interp);
3877 cmd_ctx = Jim_GetAssocData(goi.interp, "context");
3879 /* commands here are in an NVP table */
3880 e = Jim_GetOpt_Nvp(&goi, target_options, &n);
3881 if (e != JIM_OK) {
3882 Jim_GetOpt_NvpUnknown(&goi, target_options, 0);
3883 return e;
3885 /* Assume blank result */
3886 Jim_SetEmptyResult(goi.interp);
3888 switch (n->value) {
3889 case TS_CMD_CONFIGURE:
3890 if (goi.argc < 2) {
3891 Jim_WrongNumArgs(goi.interp, goi.argc, goi.argv, "missing: -option VALUE ...");
3892 return JIM_ERR;
3894 goi.isconfigure = 1;
3895 return target_configure(&goi, target);
3896 case TS_CMD_CGET:
3897 // some things take params
3898 if (goi.argc < 1) {
3899 Jim_WrongNumArgs(goi.interp, 0, goi.argv, "missing: ?-option?");
3900 return JIM_ERR;
3902 goi.isconfigure = 0;
3903 return target_configure(&goi, target);
3904 break;
3905 case TS_CMD_MWW:
3906 case TS_CMD_MWH:
3907 case TS_CMD_MWB:
3908 /* argv[0] = cmd
3909 * argv[1] = address
3910 * argv[2] = data
3911 * argv[3] = optional count.
3914 if ((goi.argc == 2) || (goi.argc == 3)) {
3915 /* all is well */
3916 } else {
3917 mwx_error:
3918 Jim_SetResult_sprintf(goi.interp, "expected: %s ADDR DATA [COUNT]", n->name);
3919 return JIM_ERR;
3922 e = Jim_GetOpt_Wide(&goi, &a);
3923 if (e != JIM_OK) {
3924 goto mwx_error;
3927 e = Jim_GetOpt_Wide(&goi, &b);
3928 if (e != JIM_OK) {
3929 goto mwx_error;
3931 if (goi.argc == 3) {
3932 e = Jim_GetOpt_Wide(&goi, &c);
3933 if (e != JIM_OK) {
3934 goto mwx_error;
3936 } else {
3937 c = 1;
3940 switch (n->value) {
3941 case TS_CMD_MWW:
3942 target_buffer_set_u32(target, target_buf, b);
3943 b = 4;
3944 break;
3945 case TS_CMD_MWH:
3946 target_buffer_set_u16(target, target_buf, b);
3947 b = 2;
3948 break;
3949 case TS_CMD_MWB:
3950 target_buffer_set_u8(target, target_buf, b);
3951 b = 1;
3952 break;
3954 for (x = 0 ; x < c ; x++) {
3955 e = target_write_memory(target, a, b, 1, target_buf);
3956 if (e != ERROR_OK) {
3957 Jim_SetResult_sprintf(interp, "Error writing @ 0x%08x: %d\n", (int)(a), e);
3958 return JIM_ERR;
3960 /* b = width */
3961 a = a + b;
3963 return JIM_OK;
3964 break;
3966 /* display */
3967 case TS_CMD_MDW:
3968 case TS_CMD_MDH:
3969 case TS_CMD_MDB:
3970 /* argv[0] = command
3971 * argv[1] = address
3972 * argv[2] = optional count
3974 if ((goi.argc == 2) || (goi.argc == 3)) {
3975 Jim_SetResult_sprintf(goi.interp, "expected: %s ADDR [COUNT]", n->name);
3976 return JIM_ERR;
3978 e = Jim_GetOpt_Wide(&goi, &a);
3979 if (e != JIM_OK) {
3980 return JIM_ERR;
3982 if (goi.argc) {
3983 e = Jim_GetOpt_Wide(&goi, &c);
3984 if (e != JIM_OK) {
3985 return JIM_ERR;
3987 } else {
3988 c = 1;
3990 b = 1; /* shut up gcc */
3991 switch (n->value) {
3992 case TS_CMD_MDW:
3993 b = 4;
3994 break;
3995 case TS_CMD_MDH:
3996 b = 2;
3997 break;
3998 case TS_CMD_MDB:
3999 b = 1;
4000 break;
4003 /* convert to "bytes" */
4004 c = c * b;
4005 /* count is now in 'BYTES' */
4006 while (c > 0) {
4007 y = c;
4008 if (y > 16) {
4009 y = 16;
4011 e = target_read_memory(target, a, b, y / b, target_buf);
4012 if (e != ERROR_OK) {
4013 Jim_SetResult_sprintf(interp, "error reading target @ 0x%08lx", (int)(a));
4014 return JIM_ERR;
4017 Jim_fprintf(interp, interp->cookie_stdout, "0x%08x ", (int)(a));
4018 switch (b) {
4019 case 4:
4020 for (x = 0 ; (x < 16) && (x < y) ; x += 4) {
4021 z = target_buffer_get_u32(target, &(target_buf[ x * 4 ]));
4022 Jim_fprintf(interp, interp->cookie_stdout, "%08x ", (int)(z));
4024 for (; (x < 16) ; x += 4) {
4025 Jim_fprintf(interp, interp->cookie_stdout, " ");
4027 break;
4028 case 2:
4029 for (x = 0 ; (x < 16) && (x < y) ; x += 2) {
4030 z = target_buffer_get_u16(target, &(target_buf[ x * 2 ]));
4031 Jim_fprintf(interp, interp->cookie_stdout, "%04x ", (int)(z));
4033 for (; (x < 16) ; x += 2) {
4034 Jim_fprintf(interp, interp->cookie_stdout, " ");
4036 break;
4037 case 1:
4038 default:
4039 for (x = 0 ; (x < 16) && (x < y) ; x += 1) {
4040 z = target_buffer_get_u8(target, &(target_buf[ x * 4 ]));
4041 Jim_fprintf(interp, interp->cookie_stdout, "%02x ", (int)(z));
4043 for (; (x < 16) ; x += 1) {
4044 Jim_fprintf(interp, interp->cookie_stdout, " ");
4046 break;
4048 /* ascii-ify the bytes */
4049 for (x = 0 ; x < y ; x++) {
4050 if ((target_buf[x] >= 0x20) &&
4051 (target_buf[x] <= 0x7e)) {
4052 /* good */
4053 } else {
4054 /* smack it */
4055 target_buf[x] = '.';
4058 /* space pad */
4059 while (x < 16) {
4060 target_buf[x] = ' ';
4061 x++;
4063 /* terminate */
4064 target_buf[16] = 0;
4065 /* print - with a newline */
4066 Jim_fprintf(interp, interp->cookie_stdout, "%s\n", target_buf);
4067 /* NEXT... */
4068 c -= 16;
4069 a += 16;
4071 return JIM_OK;
4072 case TS_CMD_MEM2ARRAY:
4073 return target_mem2array(goi.interp, target, goi.argc, goi.argv);
4074 break;
4075 case TS_CMD_ARRAY2MEM:
4076 return target_array2mem(goi.interp, target, goi.argc, goi.argv);
4077 break;
4078 case TS_CMD_EXAMINE:
4079 if (goi.argc) {
4080 Jim_WrongNumArgs(goi.interp, 2, argv, "[no parameters]");
4081 return JIM_ERR;
4083 if (!target->tap->enabled)
4084 goto err_tap_disabled;
4085 e = target->type->examine(target);
4086 if (e != ERROR_OK) {
4087 Jim_SetResult_sprintf(interp, "examine-fails: %d", e);
4088 return JIM_ERR;
4090 return JIM_OK;
4091 case TS_CMD_POLL:
4092 if (goi.argc) {
4093 Jim_WrongNumArgs(goi.interp, 2, argv, "[no parameters]");
4094 return JIM_ERR;
4096 if (!target->tap->enabled)
4097 goto err_tap_disabled;
4098 if (!(target_was_examined(target))) {
4099 e = ERROR_TARGET_NOT_EXAMINED;
4100 } else {
4101 e = target->type->poll(target);
4103 if (e != ERROR_OK) {
4104 Jim_SetResult_sprintf(interp, "poll-fails: %d", e);
4105 return JIM_ERR;
4106 } else {
4107 return JIM_OK;
4109 break;
4110 case TS_CMD_RESET:
4111 if (goi.argc != 2) {
4112 Jim_WrongNumArgs(interp, 2, argv,
4113 "([tT]|[fF]|assert|deassert) BOOL");
4114 return JIM_ERR;
4116 e = Jim_GetOpt_Nvp(&goi, nvp_assert, &n);
4117 if (e != JIM_OK) {
4118 Jim_GetOpt_NvpUnknown(&goi, nvp_assert, 1);
4119 return e;
4121 /* the halt or not param */
4122 e = Jim_GetOpt_Wide(&goi, &a);
4123 if (e != JIM_OK) {
4124 return e;
4126 if (!target->tap->enabled)
4127 goto err_tap_disabled;
4128 if (!target->type->assert_reset
4129 || !target->type->deassert_reset) {
4130 Jim_SetResult_sprintf(interp,
4131 "No target-specific reset for %s",
4132 target->cmd_name);
4133 return JIM_ERR;
4135 /* determine if we should halt or not. */
4136 target->reset_halt = !!a;
4137 /* When this happens - all workareas are invalid. */
4138 target_free_all_working_areas_restore(target, 0);
4140 /* do the assert */
4141 if (n->value == NVP_ASSERT) {
4142 e = target->type->assert_reset(target);
4143 } else {
4144 e = target->type->deassert_reset(target);
4146 return (e == ERROR_OK) ? JIM_OK : JIM_ERR;
4147 case TS_CMD_HALT:
4148 if (goi.argc) {
4149 Jim_WrongNumArgs(goi.interp, 0, argv, "halt [no parameters]");
4150 return JIM_ERR;
4152 if (!target->tap->enabled)
4153 goto err_tap_disabled;
4154 e = target->type->halt(target);
4155 return (e == ERROR_OK) ? JIM_OK : JIM_ERR;
4156 case TS_CMD_WAITSTATE:
4157 /* params: <name> statename timeoutmsecs */
4158 if (goi.argc != 2) {
4159 Jim_SetResult_sprintf(goi.interp, "%s STATENAME TIMEOUTMSECS", n->name);
4160 return JIM_ERR;
4162 e = Jim_GetOpt_Nvp(&goi, nvp_target_state, &n);
4163 if (e != JIM_OK) {
4164 Jim_GetOpt_NvpUnknown(&goi, nvp_target_state,1);
4165 return e;
4167 e = Jim_GetOpt_Wide(&goi, &a);
4168 if (e != JIM_OK) {
4169 return e;
4171 if (!target->tap->enabled)
4172 goto err_tap_disabled;
4173 e = target_wait_state(target, n->value, a);
4174 if (e != ERROR_OK) {
4175 Jim_SetResult_sprintf(goi.interp,
4176 "target: %s wait %s fails (%d) %s",
4177 target->cmd_name,
4178 n->name,
4179 e, target_strerror_safe(e));
4180 return JIM_ERR;
4181 } else {
4182 return JIM_OK;
4184 case TS_CMD_EVENTLIST:
4185 /* List for human, Events defined for this target.
4186 * scripts/programs should use 'name cget -event NAME'
4189 struct target_event_action *teap;
4190 teap = target->event_action;
4191 command_print(cmd_ctx, "Event actions for target (%d) %s\n",
4192 target->target_number,
4193 target->cmd_name);
4194 command_print(cmd_ctx, "%-25s | Body", "Event");
4195 command_print(cmd_ctx, "------------------------- | ----------------------------------------");
4196 while (teap) {
4197 command_print(cmd_ctx,
4198 "%-25s | %s",
4199 Jim_Nvp_value2name_simple(nvp_target_event, teap->event)->name,
4200 Jim_GetString(teap->body, NULL));
4201 teap = teap->next;
4203 command_print(cmd_ctx, "***END***");
4204 return JIM_OK;
4206 case TS_CMD_CURSTATE:
4207 if (goi.argc != 0) {
4208 Jim_WrongNumArgs(goi.interp, 0, argv, "[no parameters]");
4209 return JIM_ERR;
4211 Jim_SetResultString(goi.interp,
4212 target_state_name( target ),
4213 -1);
4214 return JIM_OK;
4215 case TS_CMD_INVOKE_EVENT:
4216 if (goi.argc != 1) {
4217 Jim_SetResult_sprintf(goi.interp, "%s ?EVENTNAME?",n->name);
4218 return JIM_ERR;
4220 e = Jim_GetOpt_Nvp(&goi, nvp_target_event, &n);
4221 if (e != JIM_OK) {
4222 Jim_GetOpt_NvpUnknown(&goi, nvp_target_event, 1);
4223 return e;
4225 target_handle_event(target, n->value);
4226 return JIM_OK;
4228 return JIM_ERR;
4230 err_tap_disabled:
4231 Jim_SetResult_sprintf(interp, "[TAP is disabled]");
4232 return JIM_ERR;
4235 static int target_create(Jim_GetOptInfo *goi)
4237 Jim_Obj *new_cmd;
4238 Jim_Cmd *cmd;
4239 const char *cp;
4240 char *cp2;
4241 int e;
4242 int x;
4243 struct target *target;
4244 struct command_context *cmd_ctx;
4246 cmd_ctx = Jim_GetAssocData(goi->interp, "context");
4247 if (goi->argc < 3) {
4248 Jim_WrongNumArgs(goi->interp, 1, goi->argv, "?name? ?type? ..options...");
4249 return JIM_ERR;
4252 /* COMMAND */
4253 Jim_GetOpt_Obj(goi, &new_cmd);
4254 /* does this command exist? */
4255 cmd = Jim_GetCommand(goi->interp, new_cmd, JIM_ERRMSG);
4256 if (cmd) {
4257 cp = Jim_GetString(new_cmd, NULL);
4258 Jim_SetResult_sprintf(goi->interp, "Command/target: %s Exists", cp);
4259 return JIM_ERR;
4262 /* TYPE */
4263 e = Jim_GetOpt_String(goi, &cp2, NULL);
4264 cp = cp2;
4265 /* now does target type exist */
4266 for (x = 0 ; target_types[x] ; x++) {
4267 if (0 == strcmp(cp, target_types[x]->name)) {
4268 /* found */
4269 break;
4272 if (target_types[x] == NULL) {
4273 Jim_SetResult_sprintf(goi->interp, "Unknown target type %s, try one of ", cp);
4274 for (x = 0 ; target_types[x] ; x++) {
4275 if (target_types[x + 1]) {
4276 Jim_AppendStrings(goi->interp,
4277 Jim_GetResult(goi->interp),
4278 target_types[x]->name,
4279 ", ", NULL);
4280 } else {
4281 Jim_AppendStrings(goi->interp,
4282 Jim_GetResult(goi->interp),
4283 " or ",
4284 target_types[x]->name,NULL);
4287 return JIM_ERR;
4290 /* Create it */
4291 target = calloc(1,sizeof(struct target));
4292 /* set target number */
4293 target->target_number = new_target_number();
4295 /* allocate memory for each unique target type */
4296 target->type = (struct target_type*)calloc(1,sizeof(struct target_type));
4298 memcpy(target->type, target_types[x], sizeof(struct target_type));
4300 /* will be set by "-endian" */
4301 target->endianness = TARGET_ENDIAN_UNKNOWN;
4303 target->working_area = 0x0;
4304 target->working_area_size = 0x0;
4305 target->working_areas = NULL;
4306 target->backup_working_area = 0;
4308 target->state = TARGET_UNKNOWN;
4309 target->debug_reason = DBG_REASON_UNDEFINED;
4310 target->reg_cache = NULL;
4311 target->breakpoints = NULL;
4312 target->watchpoints = NULL;
4313 target->next = NULL;
4314 target->arch_info = NULL;
4316 target->display = 1;
4318 target->halt_issued = false;
4320 /* initialize trace information */
4321 target->trace_info = malloc(sizeof(struct trace));
4322 target->trace_info->num_trace_points = 0;
4323 target->trace_info->trace_points_size = 0;
4324 target->trace_info->trace_points = NULL;
4325 target->trace_info->trace_history_size = 0;
4326 target->trace_info->trace_history = NULL;
4327 target->trace_info->trace_history_pos = 0;
4328 target->trace_info->trace_history_overflowed = 0;
4330 target->dbgmsg = NULL;
4331 target->dbg_msg_enabled = 0;
4333 target->endianness = TARGET_ENDIAN_UNKNOWN;
4335 /* Do the rest as "configure" options */
4336 goi->isconfigure = 1;
4337 e = target_configure(goi, target);
4339 if (target->tap == NULL)
4341 Jim_SetResultString(interp, "-chain-position required when creating target", -1);
4342 e = JIM_ERR;
4345 if (e != JIM_OK) {
4346 free(target->type);
4347 free(target);
4348 return e;
4351 if (target->endianness == TARGET_ENDIAN_UNKNOWN) {
4352 /* default endian to little if not specified */
4353 target->endianness = TARGET_LITTLE_ENDIAN;
4356 /* incase variant is not set */
4357 if (!target->variant)
4358 target->variant = strdup("");
4360 /* create the target specific commands */
4361 if (target->type->register_commands) {
4362 (*(target->type->register_commands))(cmd_ctx);
4364 if (target->type->target_create) {
4365 (*(target->type->target_create))(target, goi->interp);
4368 /* append to end of list */
4370 struct target **tpp;
4371 tpp = &(all_targets);
4372 while (*tpp) {
4373 tpp = &((*tpp)->next);
4375 *tpp = target;
4378 cp = Jim_GetString(new_cmd, NULL);
4379 target->cmd_name = strdup(cp);
4381 /* now - create the new target name command */
4382 e = Jim_CreateCommand(goi->interp,
4383 /* name */
4385 tcl_target_func, /* C function */
4386 target, /* private data */
4387 NULL); /* no del proc */
4389 return e;
4392 static int jim_target(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
4394 int x,r,e;
4395 jim_wide w;
4396 struct command_context *cmd_ctx;
4397 struct target *target;
4398 Jim_GetOptInfo goi;
4399 enum tcmd {
4400 /* TG = target generic */
4401 TG_CMD_CREATE,
4402 TG_CMD_TYPES,
4403 TG_CMD_NAMES,
4404 TG_CMD_CURRENT,
4405 TG_CMD_NUMBER,
4406 TG_CMD_COUNT,
4408 const char *target_cmds[] = {
4409 "create", "types", "names", "current", "number",
4410 "count",
4411 NULL /* terminate */
4414 LOG_DEBUG("Target command params:");
4415 LOG_DEBUG("%s", Jim_Debug_ArgvString(interp, argc, argv));
4417 cmd_ctx = Jim_GetAssocData(interp, "context");
4419 Jim_GetOpt_Setup(&goi, interp, argc-1, argv + 1);
4421 if (goi.argc == 0) {
4422 Jim_WrongNumArgs(interp, 1, argv, "missing: command ...");
4423 return JIM_ERR;
4426 /* Jim_GetOpt_Debug(&goi); */
4427 r = Jim_GetOpt_Enum(&goi, target_cmds, &x);
4428 if (r != JIM_OK) {
4429 return r;
4432 switch (x) {
4433 default:
4434 Jim_Panic(goi.interp,"Why am I here?");
4435 return JIM_ERR;
4436 case TG_CMD_CURRENT:
4437 if (goi.argc != 0) {
4438 Jim_WrongNumArgs(goi.interp, 1, goi.argv, "Too many parameters");
4439 return JIM_ERR;
4441 Jim_SetResultString(goi.interp, get_current_target(cmd_ctx)->cmd_name, -1);
4442 return JIM_OK;
4443 case TG_CMD_TYPES:
4444 if (goi.argc != 0) {
4445 Jim_WrongNumArgs(goi.interp, 1, goi.argv, "Too many parameters");
4446 return JIM_ERR;
4448 Jim_SetResult(goi.interp, Jim_NewListObj(goi.interp, NULL, 0));
4449 for (x = 0 ; target_types[x] ; x++) {
4450 Jim_ListAppendElement(goi.interp,
4451 Jim_GetResult(goi.interp),
4452 Jim_NewStringObj(goi.interp, target_types[x]->name, -1));
4454 return JIM_OK;
4455 case TG_CMD_NAMES:
4456 if (goi.argc != 0) {
4457 Jim_WrongNumArgs(goi.interp, 1, goi.argv, "Too many parameters");
4458 return JIM_ERR;
4460 Jim_SetResult(goi.interp, Jim_NewListObj(goi.interp, NULL, 0));
4461 target = all_targets;
4462 while (target) {
4463 Jim_ListAppendElement(goi.interp,
4464 Jim_GetResult(goi.interp),
4465 Jim_NewStringObj(goi.interp, target->cmd_name, -1));
4466 target = target->next;
4468 return JIM_OK;
4469 case TG_CMD_CREATE:
4470 if (goi.argc < 3) {
4471 Jim_WrongNumArgs(goi.interp, goi.argc, goi.argv, "?name ... config options ...");
4472 return JIM_ERR;
4474 return target_create(&goi);
4475 break;
4476 case TG_CMD_NUMBER:
4477 /* It's OK to remove this mechanism sometime after August 2010 or so */
4478 LOG_WARNING("don't use numbers as target identifiers; use names");
4479 if (goi.argc != 1) {
4480 Jim_SetResult_sprintf(goi.interp, "expected: target number ?NUMBER?");
4481 return JIM_ERR;
4483 e = Jim_GetOpt_Wide(&goi, &w);
4484 if (e != JIM_OK) {
4485 return JIM_ERR;
4487 for (x = 0, target = all_targets; target; target = target->next, x++) {
4488 if (target->target_number == w)
4489 break;
4491 if (target == NULL) {
4492 Jim_SetResult_sprintf(goi.interp,
4493 "Target: number %d does not exist", (int)(w));
4494 return JIM_ERR;
4496 Jim_SetResultString(goi.interp, target->cmd_name, -1);
4497 return JIM_OK;
4498 case TG_CMD_COUNT:
4499 if (goi.argc != 0) {
4500 Jim_WrongNumArgs(goi.interp, 0, goi.argv, "<no parameters>");
4501 return JIM_ERR;
4503 for (x = 0, target = all_targets; target; target = target->next, x++)
4504 continue;
4505 Jim_SetResult(goi.interp, Jim_NewIntObj(goi.interp, x));
4506 return JIM_OK;
4509 return JIM_ERR;
4513 struct FastLoad
4515 uint32_t address;
4516 uint8_t *data;
4517 int length;
4521 static int fastload_num;
4522 static struct FastLoad *fastload;
4524 static void free_fastload(void)
4526 if (fastload != NULL)
4528 int i;
4529 for (i = 0; i < fastload_num; i++)
4531 if (fastload[i].data)
4532 free(fastload[i].data);
4534 free(fastload);
4535 fastload = NULL;
4542 COMMAND_HANDLER(handle_fast_load_image_command)
4544 uint8_t *buffer;
4545 uint32_t buf_cnt;
4546 uint32_t image_size;
4547 uint32_t min_address = 0;
4548 uint32_t max_address = 0xffffffff;
4549 int i;
4551 struct image image;
4553 int retval = CALL_COMMAND_HANDLER(parse_load_image_command_args,
4554 &image, &min_address, &max_address);
4555 if (ERROR_OK != retval)
4556 return retval;
4558 struct duration bench;
4559 duration_start(&bench);
4561 if (image_open(&image, args[0], (argc >= 3) ? args[2] : NULL) != ERROR_OK)
4563 return ERROR_OK;
4566 image_size = 0x0;
4567 retval = ERROR_OK;
4568 fastload_num = image.num_sections;
4569 fastload = (struct FastLoad *)malloc(sizeof(struct FastLoad)*image.num_sections);
4570 if (fastload == NULL)
4572 image_close(&image);
4573 return ERROR_FAIL;
4575 memset(fastload, 0, sizeof(struct FastLoad)*image.num_sections);
4576 for (i = 0; i < image.num_sections; i++)
4578 buffer = malloc(image.sections[i].size);
4579 if (buffer == NULL)
4581 command_print(cmd_ctx, "error allocating buffer for section (%d bytes)",
4582 (int)(image.sections[i].size));
4583 break;
4586 if ((retval = image_read_section(&image, i, 0x0, image.sections[i].size, buffer, &buf_cnt)) != ERROR_OK)
4588 free(buffer);
4589 break;
4592 uint32_t offset = 0;
4593 uint32_t length = buf_cnt;
4596 /* DANGER!!! beware of unsigned comparision here!!! */
4598 if ((image.sections[i].base_address + buf_cnt >= min_address)&&
4599 (image.sections[i].base_address < max_address))
4601 if (image.sections[i].base_address < min_address)
4603 /* clip addresses below */
4604 offset += min_address-image.sections[i].base_address;
4605 length -= offset;
4608 if (image.sections[i].base_address + buf_cnt > max_address)
4610 length -= (image.sections[i].base_address + buf_cnt)-max_address;
4613 fastload[i].address = image.sections[i].base_address + offset;
4614 fastload[i].data = malloc(length);
4615 if (fastload[i].data == NULL)
4617 free(buffer);
4618 break;
4620 memcpy(fastload[i].data, buffer + offset, length);
4621 fastload[i].length = length;
4623 image_size += length;
4624 command_print(cmd_ctx, "%u bytes written at address 0x%8.8x",
4625 (unsigned int)length,
4626 ((unsigned int)(image.sections[i].base_address + offset)));
4629 free(buffer);
4632 if ((ERROR_OK == retval) && (duration_measure(&bench) == ERROR_OK))
4634 command_print(cmd_ctx, "Loaded %" PRIu32 " bytes "
4635 "in %fs (%0.3f kb/s)", image_size,
4636 duration_elapsed(&bench), duration_kbps(&bench, image_size));
4638 command_print(cmd_ctx,
4639 "WARNING: image has not been loaded to target!"
4640 "You can issue a 'fast_load' to finish loading.");
4643 image_close(&image);
4645 if (retval != ERROR_OK)
4647 free_fastload();
4650 return retval;
4653 COMMAND_HANDLER(handle_fast_load_command)
4655 if (argc > 0)
4656 return ERROR_COMMAND_SYNTAX_ERROR;
4657 if (fastload == NULL)
4659 LOG_ERROR("No image in memory");
4660 return ERROR_FAIL;
4662 int i;
4663 int ms = timeval_ms();
4664 int size = 0;
4665 int retval = ERROR_OK;
4666 for (i = 0; i < fastload_num;i++)
4668 struct target *target = get_current_target(cmd_ctx);
4669 command_print(cmd_ctx, "Write to 0x%08x, length 0x%08x",
4670 (unsigned int)(fastload[i].address),
4671 (unsigned int)(fastload[i].length));
4672 if (retval == ERROR_OK)
4674 retval = target_write_buffer(target, fastload[i].address, fastload[i].length, fastload[i].data);
4676 size += fastload[i].length;
4678 int after = timeval_ms();
4679 command_print(cmd_ctx, "Loaded image %f kBytes/s", (float)(size/1024.0)/((float)(after-ms)/1000.0));
4680 return retval;
4683 static int jim_mcrmrc(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
4685 struct command_context *context;
4686 struct target *target;
4687 int retval;
4689 context = Jim_GetAssocData(interp, "context");
4690 if (context == NULL) {
4691 LOG_ERROR("array2mem: no command context");
4692 return JIM_ERR;
4694 target = get_current_target(context);
4695 if (target == NULL) {
4696 LOG_ERROR("array2mem: no current target");
4697 return JIM_ERR;
4700 if ((argc < 6) || (argc > 7))
4702 return JIM_ERR;
4705 int cpnum;
4706 uint32_t op1;
4707 uint32_t op2;
4708 uint32_t CRn;
4709 uint32_t CRm;
4710 uint32_t value;
4712 int e;
4713 long l;
4714 e = Jim_GetLong(interp, argv[1], &l);
4715 if (e != JIM_OK) {
4716 return e;
4718 cpnum = l;
4720 e = Jim_GetLong(interp, argv[2], &l);
4721 if (e != JIM_OK) {
4722 return e;
4724 op1 = l;
4726 e = Jim_GetLong(interp, argv[3], &l);
4727 if (e != JIM_OK) {
4728 return e;
4730 CRn = l;
4732 e = Jim_GetLong(interp, argv[4], &l);
4733 if (e != JIM_OK) {
4734 return e;
4736 CRm = l;
4738 e = Jim_GetLong(interp, argv[5], &l);
4739 if (e != JIM_OK) {
4740 return e;
4742 op2 = l;
4744 value = 0;
4746 if (argc == 7)
4748 e = Jim_GetLong(interp, argv[6], &l);
4749 if (e != JIM_OK) {
4750 return e;
4752 value = l;
4754 retval = target_mcr(target, cpnum, op1, op2, CRn, CRm, value);
4755 if (retval != ERROR_OK)
4756 return JIM_ERR;
4757 } else
4759 retval = target_mrc(target, cpnum, op1, op2, CRn, CRm, &value);
4760 if (retval != ERROR_OK)
4761 return JIM_ERR;
4763 Jim_SetResult(interp, Jim_NewIntObj(interp, value));
4766 return JIM_OK;
4769 int target_register_commands(struct command_context *cmd_ctx)
4772 register_command(cmd_ctx, NULL, "targets",
4773 handle_targets_command, COMMAND_EXEC,
4774 "change current command line target (one parameter) "
4775 "or list targets (no parameters)");
4777 register_jim(cmd_ctx, "target", jim_target, "configure target");
4779 return ERROR_OK;
4782 int target_register_user_commands(struct command_context *cmd_ctx)
4784 int retval = ERROR_OK;
4785 if ((retval = target_request_register_commands(cmd_ctx)) != ERROR_OK)
4786 return retval;
4788 if ((retval = trace_register_commands(cmd_ctx)) != ERROR_OK)
4789 return retval;
4791 register_command(cmd_ctx, NULL, "profile",
4792 handle_profile_command, COMMAND_EXEC,
4793 "profiling samples the CPU PC");
4795 register_jim(cmd_ctx, "ocd_mem2array", jim_mem2array,
4796 "read memory and return as a TCL array for script processing "
4797 "<ARRAYNAME> <WIDTH = 32/16/8> <ADDRESS> <COUNT>");
4799 register_jim(cmd_ctx, "ocd_array2mem", jim_array2mem,
4800 "convert a TCL array to memory locations and write the values "
4801 "<ARRAYNAME> <WIDTH = 32/16/8> <ADDRESS> <COUNT>");
4803 register_command(cmd_ctx, NULL, "fast_load_image",
4804 handle_fast_load_image_command, COMMAND_ANY,
4805 "same args as load_image, image stored in memory "
4806 "- mainly for profiling purposes");
4808 register_command(cmd_ctx, NULL, "fast_load",
4809 handle_fast_load_command, COMMAND_ANY,
4810 "loads active fast load image to current target "
4811 "- mainly for profiling purposes");
4813 /** @todo don't register virt2phys() unless target supports it */
4814 register_command(cmd_ctx, NULL, "virt2phys",
4815 handle_virt2phys_command, COMMAND_ANY,
4816 "translate a virtual address into a physical address");
4818 register_command(cmd_ctx, NULL, "reg",
4819 handle_reg_command, COMMAND_EXEC,
4820 "display or set a register");
4822 register_command(cmd_ctx, NULL, "poll",
4823 handle_poll_command, COMMAND_EXEC,
4824 "poll target state");
4825 register_command(cmd_ctx, NULL, "wait_halt",
4826 handle_wait_halt_command, COMMAND_EXEC,
4827 "wait for target halt [time (s)]");
4828 register_command(cmd_ctx, NULL, "halt",
4829 handle_halt_command, COMMAND_EXEC,
4830 "halt target");
4831 register_command(cmd_ctx, NULL, "resume",
4832 handle_resume_command, COMMAND_EXEC,
4833 "resume target [addr]");
4834 register_command(cmd_ctx, NULL, "reset",
4835 handle_reset_command, COMMAND_EXEC,
4836 "reset target [run | halt | init] - default is run");
4837 register_command(cmd_ctx, NULL, "soft_reset_halt",
4838 handle_soft_reset_halt_command, COMMAND_EXEC,
4839 "halt the target and do a soft reset");
4841 register_command(cmd_ctx, NULL, "step",
4842 handle_step_command, COMMAND_EXEC,
4843 "step one instruction from current PC or [addr]");
4845 register_command(cmd_ctx, NULL, "mdw",
4846 handle_md_command, COMMAND_EXEC,
4847 "display memory words [phys] <addr> [count]");
4848 register_command(cmd_ctx, NULL, "mdh",
4849 handle_md_command, COMMAND_EXEC,
4850 "display memory half-words [phys] <addr> [count]");
4851 register_command(cmd_ctx, NULL, "mdb",
4852 handle_md_command, COMMAND_EXEC,
4853 "display memory bytes [phys] <addr> [count]");
4855 register_command(cmd_ctx, NULL, "mww",
4856 handle_mw_command, COMMAND_EXEC,
4857 "write memory word [phys] <addr> <value> [count]");
4858 register_command(cmd_ctx, NULL, "mwh",
4859 handle_mw_command, COMMAND_EXEC,
4860 "write memory half-word [phys] <addr> <value> [count]");
4861 register_command(cmd_ctx, NULL, "mwb",
4862 handle_mw_command, COMMAND_EXEC,
4863 "write memory byte [phys] <addr> <value> [count]");
4865 register_command(cmd_ctx, NULL, "bp",
4866 handle_bp_command, COMMAND_EXEC,
4867 "list or set breakpoint [<address> <length> [hw]]");
4868 register_command(cmd_ctx, NULL, "rbp",
4869 handle_rbp_command, COMMAND_EXEC,
4870 "remove breakpoint <address>");
4872 register_command(cmd_ctx, NULL, "wp",
4873 handle_wp_command, COMMAND_EXEC,
4874 "list or set watchpoint "
4875 "[<address> <length> <r/w/a> [value] [mask]]");
4876 register_command(cmd_ctx, NULL, "rwp",
4877 handle_rwp_command, COMMAND_EXEC,
4878 "remove watchpoint <address>");
4880 register_command(cmd_ctx, NULL, "load_image",
4881 handle_load_image_command, COMMAND_EXEC,
4882 "load_image <file> <address> "
4883 "['bin'|'ihex'|'elf'|'s19'] [min_address] [max_length]");
4884 register_command(cmd_ctx, NULL, "dump_image",
4885 handle_dump_image_command, COMMAND_EXEC,
4886 "dump_image <file> <address> <size>");
4887 register_command(cmd_ctx, NULL, "verify_image",
4888 handle_verify_image_command, COMMAND_EXEC,
4889 "verify_image <file> [offset] [type]");
4890 register_command(cmd_ctx, NULL, "test_image",
4891 handle_test_image_command, COMMAND_EXEC,
4892 "test_image <file> [offset] [type]");
4894 return ERROR_OK;