do not extern 'interp' from command.c
[openocd/ztw.git] / src / target / target.c
blob31734b8f1bb30db61142ebfb1009b43f87118068
1 /***************************************************************************
2 * Copyright (C) 2005 by Dominic Rath *
3 * Dominic.Rath@gmx.de *
4 * *
5 * Copyright (C) 2007-2009 Øyvind Harboe *
6 * oyvind.harboe@zylin.com *
7 * *
8 * Copyright (C) 2008, Duane Ellis *
9 * openocd@duaneeellis.com *
10 * *
11 * Copyright (C) 2008 by Spencer Oliver *
12 * spen@spen-soft.co.uk *
13 * *
14 * Copyright (C) 2008 by Rick Altherr *
15 * kc8apf@kc8apf.net> *
16 * *
17 * This program is free software; you can redistribute it and/or modify *
18 * it under the terms of the GNU General Public License as published by *
19 * the Free Software Foundation; either version 2 of the License, or *
20 * (at your option) any later version. *
21 * *
22 * This program is distributed in the hope that it will be useful, *
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
25 * GNU General Public License for more details. *
26 * *
27 * You should have received a copy of the GNU General Public License *
28 * along with this program; if not, write to the *
29 * Free Software Foundation, Inc., *
30 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
31 ***************************************************************************/
32 #ifdef HAVE_CONFIG_H
33 #include "config.h"
34 #endif
36 #include "target.h"
37 #include "target_type.h"
38 #include "target_request.h"
39 #include "breakpoints.h"
40 #include "time_support.h"
41 #include "register.h"
42 #include "trace.h"
43 #include "image.h"
44 #include "jtag.h"
47 static int jim_mcrmrc(Jim_Interp *interp, int argc, Jim_Obj *const *argv);
49 static int target_array2mem(Jim_Interp *interp, struct target *target, int argc, Jim_Obj *const *argv);
50 static int target_mem2array(Jim_Interp *interp, struct target *target, int argc, Jim_Obj *const *argv);
52 /* targets */
53 extern struct target_type arm7tdmi_target;
54 extern struct target_type arm720t_target;
55 extern struct target_type arm9tdmi_target;
56 extern struct target_type arm920t_target;
57 extern struct target_type arm966e_target;
58 extern struct target_type arm926ejs_target;
59 extern struct target_type fa526_target;
60 extern struct target_type feroceon_target;
61 extern struct target_type dragonite_target;
62 extern struct target_type xscale_target;
63 extern struct target_type cortexm3_target;
64 extern struct target_type cortexa8_target;
65 extern struct target_type arm11_target;
66 extern struct target_type mips_m4k_target;
67 extern struct target_type avr_target;
68 extern struct target_type testee_target;
70 struct target_type *target_types[] =
72 &arm7tdmi_target,
73 &arm9tdmi_target,
74 &arm920t_target,
75 &arm720t_target,
76 &arm966e_target,
77 &arm926ejs_target,
78 &fa526_target,
79 &feroceon_target,
80 &dragonite_target,
81 &xscale_target,
82 &cortexm3_target,
83 &cortexa8_target,
84 &arm11_target,
85 &mips_m4k_target,
86 &avr_target,
87 &testee_target,
88 NULL,
91 struct target *all_targets = NULL;
92 struct target_event_callback *target_event_callbacks = NULL;
93 struct target_timer_callback *target_timer_callbacks = NULL;
95 const Jim_Nvp nvp_assert[] = {
96 { .name = "assert", NVP_ASSERT },
97 { .name = "deassert", NVP_DEASSERT },
98 { .name = "T", NVP_ASSERT },
99 { .name = "F", NVP_DEASSERT },
100 { .name = "t", NVP_ASSERT },
101 { .name = "f", NVP_DEASSERT },
102 { .name = NULL, .value = -1 }
105 const Jim_Nvp nvp_error_target[] = {
106 { .value = ERROR_TARGET_INVALID, .name = "err-invalid" },
107 { .value = ERROR_TARGET_INIT_FAILED, .name = "err-init-failed" },
108 { .value = ERROR_TARGET_TIMEOUT, .name = "err-timeout" },
109 { .value = ERROR_TARGET_NOT_HALTED, .name = "err-not-halted" },
110 { .value = ERROR_TARGET_FAILURE, .name = "err-failure" },
111 { .value = ERROR_TARGET_UNALIGNED_ACCESS , .name = "err-unaligned-access" },
112 { .value = ERROR_TARGET_DATA_ABORT , .name = "err-data-abort" },
113 { .value = ERROR_TARGET_RESOURCE_NOT_AVAILABLE , .name = "err-resource-not-available" },
114 { .value = ERROR_TARGET_TRANSLATION_FAULT , .name = "err-translation-fault" },
115 { .value = ERROR_TARGET_NOT_RUNNING, .name = "err-not-running" },
116 { .value = ERROR_TARGET_NOT_EXAMINED, .name = "err-not-examined" },
117 { .value = -1, .name = NULL }
120 const char *target_strerror_safe(int err)
122 const Jim_Nvp *n;
124 n = Jim_Nvp_value2name_simple(nvp_error_target, err);
125 if (n->name == NULL) {
126 return "unknown";
127 } else {
128 return n->name;
132 static const Jim_Nvp nvp_target_event[] = {
133 { .value = TARGET_EVENT_OLD_gdb_program_config , .name = "old-gdb_program_config" },
134 { .value = TARGET_EVENT_OLD_pre_resume , .name = "old-pre_resume" },
136 { .value = TARGET_EVENT_GDB_HALT, .name = "gdb-halt" },
137 { .value = TARGET_EVENT_HALTED, .name = "halted" },
138 { .value = TARGET_EVENT_RESUMED, .name = "resumed" },
139 { .value = TARGET_EVENT_RESUME_START, .name = "resume-start" },
140 { .value = TARGET_EVENT_RESUME_END, .name = "resume-end" },
142 { .name = "gdb-start", .value = TARGET_EVENT_GDB_START },
143 { .name = "gdb-end", .value = TARGET_EVENT_GDB_END },
145 /* historical name */
147 { .value = TARGET_EVENT_RESET_START, .name = "reset-start" },
149 { .value = TARGET_EVENT_RESET_ASSERT_PRE, .name = "reset-assert-pre" },
150 { .value = TARGET_EVENT_RESET_ASSERT, .name = "reset-assert" },
151 { .value = TARGET_EVENT_RESET_ASSERT_POST, .name = "reset-assert-post" },
152 { .value = TARGET_EVENT_RESET_DEASSERT_PRE, .name = "reset-deassert-pre" },
153 { .value = TARGET_EVENT_RESET_DEASSERT_POST, .name = "reset-deassert-post" },
154 { .value = TARGET_EVENT_RESET_HALT_PRE, .name = "reset-halt-pre" },
155 { .value = TARGET_EVENT_RESET_HALT_POST, .name = "reset-halt-post" },
156 { .value = TARGET_EVENT_RESET_WAIT_PRE, .name = "reset-wait-pre" },
157 { .value = TARGET_EVENT_RESET_WAIT_POST, .name = "reset-wait-post" },
158 { .value = TARGET_EVENT_RESET_INIT, .name = "reset-init" },
159 { .value = TARGET_EVENT_RESET_END, .name = "reset-end" },
161 { .value = TARGET_EVENT_EXAMINE_START, .name = "examine-start" },
162 { .value = TARGET_EVENT_EXAMINE_END, .name = "examine-end" },
164 { .value = TARGET_EVENT_DEBUG_HALTED, .name = "debug-halted" },
165 { .value = TARGET_EVENT_DEBUG_RESUMED, .name = "debug-resumed" },
167 { .value = TARGET_EVENT_GDB_ATTACH, .name = "gdb-attach" },
168 { .value = TARGET_EVENT_GDB_DETACH, .name = "gdb-detach" },
170 { .value = TARGET_EVENT_GDB_FLASH_WRITE_START, .name = "gdb-flash-write-start" },
171 { .value = TARGET_EVENT_GDB_FLASH_WRITE_END , .name = "gdb-flash-write-end" },
173 { .value = TARGET_EVENT_GDB_FLASH_ERASE_START, .name = "gdb-flash-erase-start" },
174 { .value = TARGET_EVENT_GDB_FLASH_ERASE_END , .name = "gdb-flash-erase-end" },
176 { .value = TARGET_EVENT_RESUME_START, .name = "resume-start" },
177 { .value = TARGET_EVENT_RESUMED , .name = "resume-ok" },
178 { .value = TARGET_EVENT_RESUME_END , .name = "resume-end" },
180 { .name = NULL, .value = -1 }
183 const Jim_Nvp nvp_target_state[] = {
184 { .name = "unknown", .value = TARGET_UNKNOWN },
185 { .name = "running", .value = TARGET_RUNNING },
186 { .name = "halted", .value = TARGET_HALTED },
187 { .name = "reset", .value = TARGET_RESET },
188 { .name = "debug-running", .value = TARGET_DEBUG_RUNNING },
189 { .name = NULL, .value = -1 },
192 const Jim_Nvp nvp_target_debug_reason [] = {
193 { .name = "debug-request" , .value = DBG_REASON_DBGRQ },
194 { .name = "breakpoint" , .value = DBG_REASON_BREAKPOINT },
195 { .name = "watchpoint" , .value = DBG_REASON_WATCHPOINT },
196 { .name = "watchpoint-and-breakpoint", .value = DBG_REASON_WPTANDBKPT },
197 { .name = "single-step" , .value = DBG_REASON_SINGLESTEP },
198 { .name = "target-not-halted" , .value = DBG_REASON_NOTHALTED },
199 { .name = "undefined" , .value = DBG_REASON_UNDEFINED },
200 { .name = NULL, .value = -1 },
203 const Jim_Nvp nvp_target_endian[] = {
204 { .name = "big", .value = TARGET_BIG_ENDIAN },
205 { .name = "little", .value = TARGET_LITTLE_ENDIAN },
206 { .name = "be", .value = TARGET_BIG_ENDIAN },
207 { .name = "le", .value = TARGET_LITTLE_ENDIAN },
208 { .name = NULL, .value = -1 },
211 const Jim_Nvp nvp_reset_modes[] = {
212 { .name = "unknown", .value = RESET_UNKNOWN },
213 { .name = "run" , .value = RESET_RUN },
214 { .name = "halt" , .value = RESET_HALT },
215 { .name = "init" , .value = RESET_INIT },
216 { .name = NULL , .value = -1 },
219 const char *
220 target_state_name( struct target *t )
222 const char *cp;
223 cp = Jim_Nvp_value2name_simple(nvp_target_state, t->state)->name;
224 if( !cp ){
225 LOG_ERROR("Invalid target state: %d", (int)(t->state));
226 cp = "(*BUG*unknown*BUG*)";
228 return cp;
231 /* determine the number of the new target */
232 static int new_target_number(void)
234 struct target *t;
235 int x;
237 /* number is 0 based */
238 x = -1;
239 t = all_targets;
240 while (t) {
241 if (x < t->target_number) {
242 x = t->target_number;
244 t = t->next;
246 return x + 1;
249 /* read a uint32_t from a buffer in target memory endianness */
250 uint32_t target_buffer_get_u32(struct target *target, const uint8_t *buffer)
252 if (target->endianness == TARGET_LITTLE_ENDIAN)
253 return le_to_h_u32(buffer);
254 else
255 return be_to_h_u32(buffer);
258 /* read a uint16_t from a buffer in target memory endianness */
259 uint16_t target_buffer_get_u16(struct target *target, const uint8_t *buffer)
261 if (target->endianness == TARGET_LITTLE_ENDIAN)
262 return le_to_h_u16(buffer);
263 else
264 return be_to_h_u16(buffer);
267 /* read a uint8_t from a buffer in target memory endianness */
268 uint8_t target_buffer_get_u8(struct target *target, const uint8_t *buffer)
270 return *buffer & 0x0ff;
273 /* write a uint32_t to a buffer in target memory endianness */
274 void target_buffer_set_u32(struct target *target, uint8_t *buffer, uint32_t value)
276 if (target->endianness == TARGET_LITTLE_ENDIAN)
277 h_u32_to_le(buffer, value);
278 else
279 h_u32_to_be(buffer, value);
282 /* write a uint16_t to a buffer in target memory endianness */
283 void target_buffer_set_u16(struct target *target, uint8_t *buffer, uint16_t value)
285 if (target->endianness == TARGET_LITTLE_ENDIAN)
286 h_u16_to_le(buffer, value);
287 else
288 h_u16_to_be(buffer, value);
291 /* write a uint8_t to a buffer in target memory endianness */
292 void target_buffer_set_u8(struct target *target, uint8_t *buffer, uint8_t value)
294 *buffer = value;
297 /* return a pointer to a configured target; id is name or number */
298 struct target *get_target(const char *id)
300 struct target *target;
302 /* try as tcltarget name */
303 for (target = all_targets; target; target = target->next) {
304 if (target->cmd_name == NULL)
305 continue;
306 if (strcmp(id, target->cmd_name) == 0)
307 return target;
310 /* It's OK to remove this fallback sometime after August 2010 or so */
312 /* no match, try as number */
313 unsigned num;
314 if (parse_uint(id, &num) != ERROR_OK)
315 return NULL;
317 for (target = all_targets; target; target = target->next) {
318 if (target->target_number == (int)num) {
319 LOG_WARNING("use '%s' as target identifier, not '%u'",
320 target->cmd_name, num);
321 return target;
325 return NULL;
328 /* returns a pointer to the n-th configured target */
329 static struct target *get_target_by_num(int num)
331 struct target *target = all_targets;
333 while (target) {
334 if (target->target_number == num) {
335 return target;
337 target = target->next;
340 return NULL;
343 struct target* get_current_target(struct command_context *cmd_ctx)
345 struct target *target = get_target_by_num(cmd_ctx->current_target);
347 if (target == NULL)
349 LOG_ERROR("BUG: current_target out of bounds");
350 exit(-1);
353 return target;
356 int target_poll(struct target *target)
358 int retval;
360 /* We can't poll until after examine */
361 if (!target_was_examined(target))
363 /* Fail silently lest we pollute the log */
364 return ERROR_FAIL;
367 retval = target->type->poll(target);
368 if (retval != ERROR_OK)
369 return retval;
371 if (target->halt_issued)
373 if (target->state == TARGET_HALTED)
375 target->halt_issued = false;
376 } else
378 long long t = timeval_ms() - target->halt_issued_time;
379 if (t>1000)
381 target->halt_issued = false;
382 LOG_INFO("Halt timed out, wake up GDB.");
383 target_call_event_callbacks(target, TARGET_EVENT_GDB_HALT);
388 return ERROR_OK;
391 int target_halt(struct target *target)
393 int retval;
394 /* We can't poll until after examine */
395 if (!target_was_examined(target))
397 LOG_ERROR("Target not examined yet");
398 return ERROR_FAIL;
401 retval = target->type->halt(target);
402 if (retval != ERROR_OK)
403 return retval;
405 target->halt_issued = true;
406 target->halt_issued_time = timeval_ms();
408 return ERROR_OK;
411 int target_resume(struct target *target, int current, uint32_t address, int handle_breakpoints, int debug_execution)
413 int retval;
415 /* We can't poll until after examine */
416 if (!target_was_examined(target))
418 LOG_ERROR("Target not examined yet");
419 return ERROR_FAIL;
422 /* note that resume *must* be asynchronous. The CPU can halt before we poll. The CPU can
423 * even halt at the current PC as a result of a software breakpoint being inserted by (a bug?)
424 * the application.
426 if ((retval = target->type->resume(target, current, address, handle_breakpoints, debug_execution)) != ERROR_OK)
427 return retval;
429 return retval;
432 int target_process_reset(struct command_context *cmd_ctx, enum target_reset_mode reset_mode)
434 char buf[100];
435 int retval;
436 Jim_Nvp *n;
437 n = Jim_Nvp_value2name_simple(nvp_reset_modes, reset_mode);
438 if (n->name == NULL) {
439 LOG_ERROR("invalid reset mode");
440 return ERROR_FAIL;
443 /* disable polling during reset to make reset event scripts
444 * more predictable, i.e. dr/irscan & pathmove in events will
445 * not have JTAG operations injected into the middle of a sequence.
447 bool save_poll = jtag_poll_get_enabled();
449 jtag_poll_set_enabled(false);
451 sprintf(buf, "ocd_process_reset %s", n->name);
452 retval = Jim_Eval(cmd_ctx->interp, buf);
454 jtag_poll_set_enabled(save_poll);
456 if (retval != JIM_OK) {
457 Jim_PrintErrorMessage(cmd_ctx->interp);
458 return ERROR_FAIL;
461 /* We want any events to be processed before the prompt */
462 retval = target_call_timer_callbacks_now();
464 return retval;
467 static int identity_virt2phys(struct target *target,
468 uint32_t virtual, uint32_t *physical)
470 *physical = virtual;
471 return ERROR_OK;
474 static int no_mmu(struct target *target, int *enabled)
476 *enabled = 0;
477 return ERROR_OK;
480 static int default_examine(struct target *target)
482 target_set_examined(target);
483 return ERROR_OK;
486 int target_examine_one(struct target *target)
488 return target->type->examine(target);
491 static int jtag_enable_callback(enum jtag_event event, void *priv)
493 struct target *target = priv;
495 if (event != JTAG_TAP_EVENT_ENABLE || !target->tap->enabled)
496 return ERROR_OK;
498 jtag_unregister_event_callback(jtag_enable_callback, target);
499 return target_examine_one(target);
503 /* Targets that correctly implement init + examine, i.e.
504 * no communication with target during init:
506 * XScale
508 int target_examine(void)
510 int retval = ERROR_OK;
511 struct target *target;
513 for (target = all_targets; target; target = target->next)
515 /* defer examination, but don't skip it */
516 if (!target->tap->enabled) {
517 jtag_register_event_callback(jtag_enable_callback,
518 target);
519 continue;
521 if ((retval = target_examine_one(target)) != ERROR_OK)
522 return retval;
524 return retval;
526 const char *target_type_name(struct target *target)
528 return target->type->name;
531 static int target_write_memory_imp(struct target *target, uint32_t address, uint32_t size, uint32_t count, uint8_t *buffer)
533 if (!target_was_examined(target))
535 LOG_ERROR("Target not examined yet");
536 return ERROR_FAIL;
538 return target->type->write_memory_imp(target, address, size, count, buffer);
541 static int target_read_memory_imp(struct target *target, uint32_t address, uint32_t size, uint32_t count, uint8_t *buffer)
543 if (!target_was_examined(target))
545 LOG_ERROR("Target not examined yet");
546 return ERROR_FAIL;
548 return target->type->read_memory_imp(target, address, size, count, buffer);
551 static int target_soft_reset_halt_imp(struct target *target)
553 if (!target_was_examined(target))
555 LOG_ERROR("Target not examined yet");
556 return ERROR_FAIL;
558 if (!target->type->soft_reset_halt_imp) {
559 LOG_ERROR("Target %s does not support soft_reset_halt",
560 target_name(target));
561 return ERROR_FAIL;
563 return target->type->soft_reset_halt_imp(target);
566 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)
568 if (!target_was_examined(target))
570 LOG_ERROR("Target not examined yet");
571 return ERROR_FAIL;
573 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);
576 int target_read_memory(struct target *target,
577 uint32_t address, uint32_t size, uint32_t count, uint8_t *buffer)
579 return target->type->read_memory(target, address, size, count, buffer);
582 int target_read_phys_memory(struct target *target,
583 uint32_t address, uint32_t size, uint32_t count, uint8_t *buffer)
585 return target->type->read_phys_memory(target, address, size, count, buffer);
588 int target_write_memory(struct target *target,
589 uint32_t address, uint32_t size, uint32_t count, uint8_t *buffer)
591 return target->type->write_memory(target, address, size, count, buffer);
594 int target_write_phys_memory(struct target *target,
595 uint32_t address, uint32_t size, uint32_t count, uint8_t *buffer)
597 return target->type->write_phys_memory(target, address, size, count, buffer);
600 int target_bulk_write_memory(struct target *target,
601 uint32_t address, uint32_t count, uint8_t *buffer)
603 return target->type->bulk_write_memory(target, address, count, buffer);
606 int target_add_breakpoint(struct target *target,
607 struct breakpoint *breakpoint)
609 if (target->state != TARGET_HALTED) {
610 LOG_WARNING("target %s is not halted", target->cmd_name);
611 return ERROR_TARGET_NOT_HALTED;
613 return target->type->add_breakpoint(target, breakpoint);
615 int target_remove_breakpoint(struct target *target,
616 struct breakpoint *breakpoint)
618 return target->type->remove_breakpoint(target, breakpoint);
621 int target_add_watchpoint(struct target *target,
622 struct watchpoint *watchpoint)
624 if (target->state != TARGET_HALTED) {
625 LOG_WARNING("target %s is not halted", target->cmd_name);
626 return ERROR_TARGET_NOT_HALTED;
628 return target->type->add_watchpoint(target, watchpoint);
630 int target_remove_watchpoint(struct target *target,
631 struct watchpoint *watchpoint)
633 return target->type->remove_watchpoint(target, watchpoint);
636 int target_get_gdb_reg_list(struct target *target,
637 struct reg **reg_list[], int *reg_list_size)
639 return target->type->get_gdb_reg_list(target, reg_list, reg_list_size);
641 int target_step(struct target *target,
642 int current, uint32_t address, int handle_breakpoints)
644 return target->type->step(target, current, address, handle_breakpoints);
648 int target_run_algorithm(struct target *target,
649 int num_mem_params, struct mem_param *mem_params,
650 int num_reg_params, struct reg_param *reg_param,
651 uint32_t entry_point, uint32_t exit_point,
652 int timeout_ms, void *arch_info)
654 return target->type->run_algorithm(target,
655 num_mem_params, mem_params, num_reg_params, reg_param,
656 entry_point, exit_point, timeout_ms, arch_info);
660 * Reset the @c examined flag for the given target.
661 * Pure paranoia -- targets are zeroed on allocation.
663 static void target_reset_examined(struct target *target)
665 target->examined = false;
670 static int default_mrc(struct target *target, int cpnum, uint32_t op1, uint32_t op2, uint32_t CRn, uint32_t CRm, uint32_t *value)
672 LOG_ERROR("Not implemented: %s", __func__);
673 return ERROR_FAIL;
676 static int default_mcr(struct target *target, int cpnum, uint32_t op1, uint32_t op2, uint32_t CRn, uint32_t CRm, uint32_t value)
678 LOG_ERROR("Not implemented: %s", __func__);
679 return ERROR_FAIL;
682 static int arm_cp_check(struct target *target, int cpnum, uint32_t op1, uint32_t op2, uint32_t CRn, uint32_t CRm)
684 /* basic check */
685 if (!target_was_examined(target))
687 LOG_ERROR("Target not examined yet");
688 return ERROR_FAIL;
691 if ((cpnum <0) || (cpnum > 15))
693 LOG_ERROR("Illegal co-processor %d", cpnum);
694 return ERROR_FAIL;
697 if (op1 > 7)
699 LOG_ERROR("Illegal op1");
700 return ERROR_FAIL;
703 if (op2 > 7)
705 LOG_ERROR("Illegal op2");
706 return ERROR_FAIL;
709 if (CRn > 15)
711 LOG_ERROR("Illegal CRn");
712 return ERROR_FAIL;
715 if (CRm > 15)
717 LOG_ERROR("Illegal CRm");
718 return ERROR_FAIL;
721 return ERROR_OK;
724 int target_mrc(struct target *target, int cpnum, uint32_t op1, uint32_t op2, uint32_t CRn, uint32_t CRm, uint32_t *value)
726 int retval;
728 retval = arm_cp_check(target, cpnum, op1, op2, CRn, CRm);
729 if (retval != ERROR_OK)
730 return retval;
732 return target->type->mrc(target, cpnum, op1, op2, CRn, CRm, value);
735 int target_mcr(struct target *target, int cpnum, uint32_t op1, uint32_t op2, uint32_t CRn, uint32_t CRm, uint32_t value)
737 int retval;
739 retval = arm_cp_check(target, cpnum, op1, op2, CRn, CRm);
740 if (retval != ERROR_OK)
741 return retval;
743 return target->type->mcr(target, cpnum, op1, op2, CRn, CRm, value);
746 static int
747 err_read_phys_memory(struct target *target, uint32_t address,
748 uint32_t size, uint32_t count, uint8_t *buffer)
750 LOG_ERROR("Not implemented: %s", __func__);
751 return ERROR_FAIL;
754 static int
755 err_write_phys_memory(struct target *target, uint32_t address,
756 uint32_t size, uint32_t count, uint8_t *buffer)
758 LOG_ERROR("Not implemented: %s", __func__);
759 return ERROR_FAIL;
762 static int handle_target(void *priv);
764 int target_init(struct command_context *cmd_ctx)
766 struct target *target;
767 int retval;
769 for (target = all_targets; target; target = target->next) {
770 struct target_type *type = target->type;
772 target_reset_examined(target);
773 if (target->type->examine == NULL)
775 target->type->examine = default_examine;
778 if ((retval = target->type->init_target(cmd_ctx, target)) != ERROR_OK)
780 LOG_ERROR("target '%s' init failed", target_name(target));
781 return retval;
785 * @todo MCR/MRC are ARM-specific; don't require them in
786 * all targets, or for ARMs without coprocessors.
788 if (target->type->mcr == NULL)
790 target->type->mcr = default_mcr;
791 } else
793 const struct command_registration mcr_cmd = {
794 .name = "mcr",
795 .mode = COMMAND_EXEC,
796 .jim_handler = &jim_mcrmrc,
797 .help = "write coprocessor",
798 .usage = "<cpnum> <op1> <op2> <CRn> <CRm> <value>",
800 register_command(cmd_ctx, NULL, &mcr_cmd);
803 if (target->type->mrc == NULL)
805 target->type->mrc = default_mrc;
806 } else
808 const struct command_registration mrc_cmd = {
809 .name = "mrc",
810 .jim_handler = &jim_mcrmrc,
811 .help = "read coprocessor",
812 .usage = "<cpnum> <op1> <op2> <CRn> <CRm>",
814 register_command(cmd_ctx, NULL, &mrc_cmd);
819 * @todo get rid of those *memory_imp() methods, now that all
820 * callers are using target_*_memory() accessors ... and make
821 * sure the "physical" paths handle the same issues.
824 /* a non-invasive way(in terms of patches) to add some code that
825 * runs before the type->write/read_memory implementation
827 target->type->write_memory_imp = target->type->write_memory;
828 target->type->write_memory = target_write_memory_imp;
829 target->type->read_memory_imp = target->type->read_memory;
830 target->type->read_memory = target_read_memory_imp;
831 target->type->soft_reset_halt_imp = target->type->soft_reset_halt;
832 target->type->soft_reset_halt = target_soft_reset_halt_imp;
833 target->type->run_algorithm_imp = target->type->run_algorithm;
834 target->type->run_algorithm = target_run_algorithm_imp;
836 /* Sanity-check MMU support ... stub in what we must, to help
837 * implement it in stages, but warn if we need to do so.
839 if (type->mmu) {
840 if (type->write_phys_memory == NULL) {
841 LOG_ERROR("type '%s' is missing %s",
842 type->name,
843 "write_phys_memory");
844 type->write_phys_memory = err_write_phys_memory;
846 if (type->read_phys_memory == NULL) {
847 LOG_ERROR("type '%s' is missing %s",
848 type->name,
849 "read_phys_memory");
850 type->read_phys_memory = err_read_phys_memory;
852 if (type->virt2phys == NULL) {
853 LOG_ERROR("type '%s' is missing %s",
854 type->name,
855 "virt2phys");
856 type->virt2phys = identity_virt2phys;
859 /* Make sure no-MMU targets all behave the same: make no
860 * distinction between physical and virtual addresses, and
861 * ensure that virt2phys() is always an identity mapping.
863 } else {
864 if (type->write_phys_memory
865 || type->read_phys_memory
866 || type->virt2phys)
867 LOG_WARNING("type '%s' has broken MMU hooks",
868 type->name);
870 type->mmu = no_mmu;
871 type->write_phys_memory = type->write_memory;
872 type->read_phys_memory = type->read_memory;
873 type->virt2phys = identity_virt2phys;
877 if (all_targets)
879 if ((retval = target_register_user_commands(cmd_ctx)) != ERROR_OK)
880 return retval;
881 if ((retval = target_register_timer_callback(&handle_target, 100, 1, cmd_ctx->interp)) != ERROR_OK)
882 return retval;
885 return ERROR_OK;
888 int target_register_event_callback(int (*callback)(struct target *target, enum target_event event, void *priv), void *priv)
890 struct target_event_callback **callbacks_p = &target_event_callbacks;
892 if (callback == NULL)
894 return ERROR_INVALID_ARGUMENTS;
897 if (*callbacks_p)
899 while ((*callbacks_p)->next)
900 callbacks_p = &((*callbacks_p)->next);
901 callbacks_p = &((*callbacks_p)->next);
904 (*callbacks_p) = malloc(sizeof(struct target_event_callback));
905 (*callbacks_p)->callback = callback;
906 (*callbacks_p)->priv = priv;
907 (*callbacks_p)->next = NULL;
909 return ERROR_OK;
912 int target_register_timer_callback(int (*callback)(void *priv), int time_ms, int periodic, void *priv)
914 struct target_timer_callback **callbacks_p = &target_timer_callbacks;
915 struct timeval now;
917 if (callback == NULL)
919 return ERROR_INVALID_ARGUMENTS;
922 if (*callbacks_p)
924 while ((*callbacks_p)->next)
925 callbacks_p = &((*callbacks_p)->next);
926 callbacks_p = &((*callbacks_p)->next);
929 (*callbacks_p) = malloc(sizeof(struct target_timer_callback));
930 (*callbacks_p)->callback = callback;
931 (*callbacks_p)->periodic = periodic;
932 (*callbacks_p)->time_ms = time_ms;
934 gettimeofday(&now, NULL);
935 (*callbacks_p)->when.tv_usec = now.tv_usec + (time_ms % 1000) * 1000;
936 time_ms -= (time_ms % 1000);
937 (*callbacks_p)->when.tv_sec = now.tv_sec + (time_ms / 1000);
938 if ((*callbacks_p)->when.tv_usec > 1000000)
940 (*callbacks_p)->when.tv_usec = (*callbacks_p)->when.tv_usec - 1000000;
941 (*callbacks_p)->when.tv_sec += 1;
944 (*callbacks_p)->priv = priv;
945 (*callbacks_p)->next = NULL;
947 return ERROR_OK;
950 int target_unregister_event_callback(int (*callback)(struct target *target, enum target_event event, void *priv), void *priv)
952 struct target_event_callback **p = &target_event_callbacks;
953 struct target_event_callback *c = target_event_callbacks;
955 if (callback == NULL)
957 return ERROR_INVALID_ARGUMENTS;
960 while (c)
962 struct target_event_callback *next = c->next;
963 if ((c->callback == callback) && (c->priv == priv))
965 *p = next;
966 free(c);
967 return ERROR_OK;
969 else
970 p = &(c->next);
971 c = next;
974 return ERROR_OK;
977 int target_unregister_timer_callback(int (*callback)(void *priv), void *priv)
979 struct target_timer_callback **p = &target_timer_callbacks;
980 struct target_timer_callback *c = target_timer_callbacks;
982 if (callback == NULL)
984 return ERROR_INVALID_ARGUMENTS;
987 while (c)
989 struct target_timer_callback *next = c->next;
990 if ((c->callback == callback) && (c->priv == priv))
992 *p = next;
993 free(c);
994 return ERROR_OK;
996 else
997 p = &(c->next);
998 c = next;
1001 return ERROR_OK;
1004 int target_call_event_callbacks(struct target *target, enum target_event event)
1006 struct target_event_callback *callback = target_event_callbacks;
1007 struct target_event_callback *next_callback;
1009 if (event == TARGET_EVENT_HALTED)
1011 /* execute early halted first */
1012 target_call_event_callbacks(target, TARGET_EVENT_GDB_HALT);
1015 LOG_DEBUG("target event %i (%s)",
1016 event,
1017 Jim_Nvp_value2name_simple(nvp_target_event, event)->name);
1019 target_handle_event(target, event);
1021 while (callback)
1023 next_callback = callback->next;
1024 callback->callback(target, event, callback->priv);
1025 callback = next_callback;
1028 return ERROR_OK;
1031 static int target_timer_callback_periodic_restart(
1032 struct target_timer_callback *cb, struct timeval *now)
1034 int time_ms = cb->time_ms;
1035 cb->when.tv_usec = now->tv_usec + (time_ms % 1000) * 1000;
1036 time_ms -= (time_ms % 1000);
1037 cb->when.tv_sec = now->tv_sec + time_ms / 1000;
1038 if (cb->when.tv_usec > 1000000)
1040 cb->when.tv_usec = cb->when.tv_usec - 1000000;
1041 cb->when.tv_sec += 1;
1043 return ERROR_OK;
1046 static int target_call_timer_callback(struct target_timer_callback *cb,
1047 struct timeval *now)
1049 cb->callback(cb->priv);
1051 if (cb->periodic)
1052 return target_timer_callback_periodic_restart(cb, now);
1054 return target_unregister_timer_callback(cb->callback, cb->priv);
1057 static int target_call_timer_callbacks_check_time(int checktime)
1059 keep_alive();
1061 struct timeval now;
1062 gettimeofday(&now, NULL);
1064 struct target_timer_callback *callback = target_timer_callbacks;
1065 while (callback)
1067 // cleaning up may unregister and free this callback
1068 struct target_timer_callback *next_callback = callback->next;
1070 bool call_it = callback->callback &&
1071 ((!checktime && callback->periodic) ||
1072 now.tv_sec > callback->when.tv_sec ||
1073 (now.tv_sec == callback->when.tv_sec &&
1074 now.tv_usec >= callback->when.tv_usec));
1076 if (call_it)
1078 int retval = target_call_timer_callback(callback, &now);
1079 if (retval != ERROR_OK)
1080 return retval;
1083 callback = next_callback;
1086 return ERROR_OK;
1089 int target_call_timer_callbacks(void)
1091 return target_call_timer_callbacks_check_time(1);
1094 /* invoke periodic callbacks immediately */
1095 int target_call_timer_callbacks_now(void)
1097 return target_call_timer_callbacks_check_time(0);
1100 int target_alloc_working_area(struct target *target, uint32_t size, struct working_area **area)
1102 struct working_area *c = target->working_areas;
1103 struct working_area *new_wa = NULL;
1105 /* Reevaluate working area address based on MMU state*/
1106 if (target->working_areas == NULL)
1108 int retval;
1109 int enabled;
1111 retval = target->type->mmu(target, &enabled);
1112 if (retval != ERROR_OK)
1114 return retval;
1117 if (!enabled) {
1118 if (target->working_area_phys_spec) {
1119 LOG_DEBUG("MMU disabled, using physical "
1120 "address for working memory 0x%08x",
1121 (unsigned)target->working_area_phys);
1122 target->working_area = target->working_area_phys;
1123 } else {
1124 LOG_ERROR("No working memory available. "
1125 "Specify -work-area-phys to target.");
1126 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
1128 } else {
1129 if (target->working_area_virt_spec) {
1130 LOG_DEBUG("MMU enabled, using virtual "
1131 "address for working memory 0x%08x",
1132 (unsigned)target->working_area_virt);
1133 target->working_area = target->working_area_virt;
1134 } else {
1135 LOG_ERROR("No working memory available. "
1136 "Specify -work-area-virt to target.");
1137 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
1142 /* only allocate multiples of 4 byte */
1143 if (size % 4)
1145 LOG_ERROR("BUG: code tried to allocate unaligned number of bytes (0x%08x), padding", ((unsigned)(size)));
1146 size = (size + 3) & (~3);
1149 /* see if there's already a matching working area */
1150 while (c)
1152 if ((c->free) && (c->size == size))
1154 new_wa = c;
1155 break;
1157 c = c->next;
1160 /* if not, allocate a new one */
1161 if (!new_wa)
1163 struct working_area **p = &target->working_areas;
1164 uint32_t first_free = target->working_area;
1165 uint32_t free_size = target->working_area_size;
1167 c = target->working_areas;
1168 while (c)
1170 first_free += c->size;
1171 free_size -= c->size;
1172 p = &c->next;
1173 c = c->next;
1176 if (free_size < size)
1178 LOG_WARNING("not enough working area available(requested %u, free %u)",
1179 (unsigned)(size), (unsigned)(free_size));
1180 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
1183 LOG_DEBUG("allocated new working area at address 0x%08x", (unsigned)first_free);
1185 new_wa = malloc(sizeof(struct working_area));
1186 new_wa->next = NULL;
1187 new_wa->size = size;
1188 new_wa->address = first_free;
1190 if (target->backup_working_area)
1192 int retval;
1193 new_wa->backup = malloc(new_wa->size);
1194 if ((retval = target_read_memory(target, new_wa->address, 4, new_wa->size / 4, new_wa->backup)) != ERROR_OK)
1196 free(new_wa->backup);
1197 free(new_wa);
1198 return retval;
1201 else
1203 new_wa->backup = NULL;
1206 /* put new entry in list */
1207 *p = new_wa;
1210 /* mark as used, and return the new (reused) area */
1211 new_wa->free = 0;
1212 *area = new_wa;
1214 /* user pointer */
1215 new_wa->user = area;
1217 return ERROR_OK;
1220 int target_free_working_area_restore(struct target *target, struct working_area *area, int restore)
1222 if (area->free)
1223 return ERROR_OK;
1225 if (restore && target->backup_working_area)
1227 int retval;
1228 if ((retval = target_write_memory(target, area->address, 4, area->size / 4, area->backup)) != ERROR_OK)
1229 return retval;
1232 area->free = 1;
1234 /* mark user pointer invalid */
1235 *area->user = NULL;
1236 area->user = NULL;
1238 return ERROR_OK;
1241 int target_free_working_area(struct target *target, struct working_area *area)
1243 return target_free_working_area_restore(target, area, 1);
1246 /* free resources and restore memory, if restoring memory fails,
1247 * free up resources anyway
1249 void target_free_all_working_areas_restore(struct target *target, int restore)
1251 struct working_area *c = target->working_areas;
1253 while (c)
1255 struct working_area *next = c->next;
1256 target_free_working_area_restore(target, c, restore);
1258 if (c->backup)
1259 free(c->backup);
1261 free(c);
1263 c = next;
1266 target->working_areas = NULL;
1269 void target_free_all_working_areas(struct target *target)
1271 target_free_all_working_areas_restore(target, 1);
1274 int target_arch_state(struct target *target)
1276 int retval;
1277 if (target == NULL)
1279 LOG_USER("No target has been configured");
1280 return ERROR_OK;
1283 LOG_USER("target state: %s", target_state_name( target ));
1285 if (target->state != TARGET_HALTED)
1286 return ERROR_OK;
1288 retval = target->type->arch_state(target);
1289 return retval;
1292 /* Single aligned words are guaranteed to use 16 or 32 bit access
1293 * mode respectively, otherwise data is handled as quickly as
1294 * possible
1296 int target_write_buffer(struct target *target, uint32_t address, uint32_t size, uint8_t *buffer)
1298 int retval;
1299 LOG_DEBUG("writing buffer of %i byte at 0x%8.8x",
1300 (int)size, (unsigned)address);
1302 if (!target_was_examined(target))
1304 LOG_ERROR("Target not examined yet");
1305 return ERROR_FAIL;
1308 if (size == 0) {
1309 return ERROR_OK;
1312 if ((address + size - 1) < address)
1314 /* GDB can request this when e.g. PC is 0xfffffffc*/
1315 LOG_ERROR("address + size wrapped(0x%08x, 0x%08x)",
1316 (unsigned)address,
1317 (unsigned)size);
1318 return ERROR_FAIL;
1321 if (((address % 2) == 0) && (size == 2))
1323 return target_write_memory(target, address, 2, 1, buffer);
1326 /* handle unaligned head bytes */
1327 if (address % 4)
1329 uint32_t unaligned = 4 - (address % 4);
1331 if (unaligned > size)
1332 unaligned = size;
1334 if ((retval = target_write_memory(target, address, 1, unaligned, buffer)) != ERROR_OK)
1335 return retval;
1337 buffer += unaligned;
1338 address += unaligned;
1339 size -= unaligned;
1342 /* handle aligned words */
1343 if (size >= 4)
1345 int aligned = size - (size % 4);
1347 /* use bulk writes above a certain limit. This may have to be changed */
1348 if (aligned > 128)
1350 if ((retval = target->type->bulk_write_memory(target, address, aligned / 4, buffer)) != ERROR_OK)
1351 return retval;
1353 else
1355 if ((retval = target_write_memory(target, address, 4, aligned / 4, buffer)) != ERROR_OK)
1356 return retval;
1359 buffer += aligned;
1360 address += aligned;
1361 size -= aligned;
1364 /* handle tail writes of less than 4 bytes */
1365 if (size > 0)
1367 if ((retval = target_write_memory(target, address, 1, size, buffer)) != ERROR_OK)
1368 return retval;
1371 return ERROR_OK;
1374 /* Single aligned words are guaranteed to use 16 or 32 bit access
1375 * mode respectively, otherwise data is handled as quickly as
1376 * possible
1378 int target_read_buffer(struct target *target, uint32_t address, uint32_t size, uint8_t *buffer)
1380 int retval;
1381 LOG_DEBUG("reading buffer of %i byte at 0x%8.8x",
1382 (int)size, (unsigned)address);
1384 if (!target_was_examined(target))
1386 LOG_ERROR("Target not examined yet");
1387 return ERROR_FAIL;
1390 if (size == 0) {
1391 return ERROR_OK;
1394 if ((address + size - 1) < address)
1396 /* GDB can request this when e.g. PC is 0xfffffffc*/
1397 LOG_ERROR("address + size wrapped(0x%08" PRIx32 ", 0x%08" PRIx32 ")",
1398 address,
1399 size);
1400 return ERROR_FAIL;
1403 if (((address % 2) == 0) && (size == 2))
1405 return target_read_memory(target, address, 2, 1, buffer);
1408 /* handle unaligned head bytes */
1409 if (address % 4)
1411 uint32_t unaligned = 4 - (address % 4);
1413 if (unaligned > size)
1414 unaligned = size;
1416 if ((retval = target_read_memory(target, address, 1, unaligned, buffer)) != ERROR_OK)
1417 return retval;
1419 buffer += unaligned;
1420 address += unaligned;
1421 size -= unaligned;
1424 /* handle aligned words */
1425 if (size >= 4)
1427 int aligned = size - (size % 4);
1429 if ((retval = target_read_memory(target, address, 4, aligned / 4, buffer)) != ERROR_OK)
1430 return retval;
1432 buffer += aligned;
1433 address += aligned;
1434 size -= aligned;
1437 /*prevent byte access when possible (avoid AHB access limitations in some cases)*/
1438 if(size >=2)
1440 int aligned = size - (size%2);
1441 retval = target_read_memory(target, address, 2, aligned / 2, buffer);
1442 if (retval != ERROR_OK)
1443 return retval;
1445 buffer += aligned;
1446 address += aligned;
1447 size -= aligned;
1449 /* handle tail writes of less than 4 bytes */
1450 if (size > 0)
1452 if ((retval = target_read_memory(target, address, 1, size, buffer)) != ERROR_OK)
1453 return retval;
1456 return ERROR_OK;
1459 int target_checksum_memory(struct target *target, uint32_t address, uint32_t size, uint32_t* crc)
1461 uint8_t *buffer;
1462 int retval;
1463 uint32_t i;
1464 uint32_t checksum = 0;
1465 if (!target_was_examined(target))
1467 LOG_ERROR("Target not examined yet");
1468 return ERROR_FAIL;
1471 if ((retval = target->type->checksum_memory(target, address,
1472 size, &checksum)) != ERROR_OK)
1474 buffer = malloc(size);
1475 if (buffer == NULL)
1477 LOG_ERROR("error allocating buffer for section (%d bytes)", (int)size);
1478 return ERROR_INVALID_ARGUMENTS;
1480 retval = target_read_buffer(target, address, size, buffer);
1481 if (retval != ERROR_OK)
1483 free(buffer);
1484 return retval;
1487 /* convert to target endianess */
1488 for (i = 0; i < (size/sizeof(uint32_t)); i++)
1490 uint32_t target_data;
1491 target_data = target_buffer_get_u32(target, &buffer[i*sizeof(uint32_t)]);
1492 target_buffer_set_u32(target, &buffer[i*sizeof(uint32_t)], target_data);
1495 retval = image_calculate_checksum(buffer, size, &checksum);
1496 free(buffer);
1499 *crc = checksum;
1501 return retval;
1504 int target_blank_check_memory(struct target *target, uint32_t address, uint32_t size, uint32_t* blank)
1506 int retval;
1507 if (!target_was_examined(target))
1509 LOG_ERROR("Target not examined yet");
1510 return ERROR_FAIL;
1513 if (target->type->blank_check_memory == 0)
1514 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
1516 retval = target->type->blank_check_memory(target, address, size, blank);
1518 return retval;
1521 int target_read_u32(struct target *target, uint32_t address, uint32_t *value)
1523 uint8_t value_buf[4];
1524 if (!target_was_examined(target))
1526 LOG_ERROR("Target not examined yet");
1527 return ERROR_FAIL;
1530 int retval = target_read_memory(target, address, 4, 1, value_buf);
1532 if (retval == ERROR_OK)
1534 *value = target_buffer_get_u32(target, value_buf);
1535 LOG_DEBUG("address: 0x%8.8" PRIx32 ", value: 0x%8.8" PRIx32 "",
1536 address,
1537 *value);
1539 else
1541 *value = 0x0;
1542 LOG_DEBUG("address: 0x%8.8" PRIx32 " failed",
1543 address);
1546 return retval;
1549 int target_read_u16(struct target *target, uint32_t address, uint16_t *value)
1551 uint8_t value_buf[2];
1552 if (!target_was_examined(target))
1554 LOG_ERROR("Target not examined yet");
1555 return ERROR_FAIL;
1558 int retval = target_read_memory(target, address, 2, 1, value_buf);
1560 if (retval == ERROR_OK)
1562 *value = target_buffer_get_u16(target, value_buf);
1563 LOG_DEBUG("address: 0x%8.8" PRIx32 ", value: 0x%4.4x",
1564 address,
1565 *value);
1567 else
1569 *value = 0x0;
1570 LOG_DEBUG("address: 0x%8.8" PRIx32 " failed",
1571 address);
1574 return retval;
1577 int target_read_u8(struct target *target, uint32_t address, uint8_t *value)
1579 int retval = target_read_memory(target, address, 1, 1, value);
1580 if (!target_was_examined(target))
1582 LOG_ERROR("Target not examined yet");
1583 return ERROR_FAIL;
1586 if (retval == ERROR_OK)
1588 LOG_DEBUG("address: 0x%8.8" PRIx32 ", value: 0x%2.2x",
1589 address,
1590 *value);
1592 else
1594 *value = 0x0;
1595 LOG_DEBUG("address: 0x%8.8" PRIx32 " failed",
1596 address);
1599 return retval;
1602 int target_write_u32(struct target *target, uint32_t address, uint32_t value)
1604 int retval;
1605 uint8_t value_buf[4];
1606 if (!target_was_examined(target))
1608 LOG_ERROR("Target not examined yet");
1609 return ERROR_FAIL;
1612 LOG_DEBUG("address: 0x%8.8" PRIx32 ", value: 0x%8.8" PRIx32 "",
1613 address,
1614 value);
1616 target_buffer_set_u32(target, value_buf, value);
1617 if ((retval = target_write_memory(target, address, 4, 1, value_buf)) != ERROR_OK)
1619 LOG_DEBUG("failed: %i", retval);
1622 return retval;
1625 int target_write_u16(struct target *target, uint32_t address, uint16_t value)
1627 int retval;
1628 uint8_t value_buf[2];
1629 if (!target_was_examined(target))
1631 LOG_ERROR("Target not examined yet");
1632 return ERROR_FAIL;
1635 LOG_DEBUG("address: 0x%8.8" PRIx32 ", value: 0x%8.8x",
1636 address,
1637 value);
1639 target_buffer_set_u16(target, value_buf, value);
1640 if ((retval = target_write_memory(target, address, 2, 1, value_buf)) != ERROR_OK)
1642 LOG_DEBUG("failed: %i", retval);
1645 return retval;
1648 int target_write_u8(struct target *target, uint32_t address, uint8_t value)
1650 int retval;
1651 if (!target_was_examined(target))
1653 LOG_ERROR("Target not examined yet");
1654 return ERROR_FAIL;
1657 LOG_DEBUG("address: 0x%8.8" PRIx32 ", value: 0x%2.2x",
1658 address, value);
1660 if ((retval = target_write_memory(target, address, 1, 1, &value)) != ERROR_OK)
1662 LOG_DEBUG("failed: %i", retval);
1665 return retval;
1668 COMMAND_HANDLER(handle_targets_command)
1670 struct target *target = all_targets;
1672 if (CMD_ARGC == 1)
1674 target = get_target(CMD_ARGV[0]);
1675 if (target == NULL) {
1676 command_print(CMD_CTX,"Target: %s is unknown, try one of:\n", CMD_ARGV[0]);
1677 goto DumpTargets;
1679 if (!target->tap->enabled) {
1680 command_print(CMD_CTX,"Target: TAP %s is disabled, "
1681 "can't be the current target\n",
1682 target->tap->dotted_name);
1683 return ERROR_FAIL;
1686 CMD_CTX->current_target = target->target_number;
1687 return ERROR_OK;
1689 DumpTargets:
1691 target = all_targets;
1692 command_print(CMD_CTX, " TargetName Type Endian TapName State ");
1693 command_print(CMD_CTX, "-- ------------------ ---------- ------ ------------------ ------------");
1694 while (target)
1696 const char *state;
1697 char marker = ' ';
1699 if (target->tap->enabled)
1700 state = target_state_name( target );
1701 else
1702 state = "tap-disabled";
1704 if (CMD_CTX->current_target == target->target_number)
1705 marker = '*';
1707 /* keep columns lined up to match the headers above */
1708 command_print(CMD_CTX, "%2d%c %-18s %-10s %-6s %-18s %s",
1709 target->target_number,
1710 marker,
1711 target_name(target),
1712 target_type_name(target),
1713 Jim_Nvp_value2name_simple(nvp_target_endian,
1714 target->endianness)->name,
1715 target->tap->dotted_name,
1716 state);
1717 target = target->next;
1720 return ERROR_OK;
1723 /* every 300ms we check for reset & powerdropout and issue a "reset halt" if so. */
1725 static int powerDropout;
1726 static int srstAsserted;
1728 static int runPowerRestore;
1729 static int runPowerDropout;
1730 static int runSrstAsserted;
1731 static int runSrstDeasserted;
1733 static int sense_handler(void)
1735 static int prevSrstAsserted = 0;
1736 static int prevPowerdropout = 0;
1738 int retval;
1739 if ((retval = jtag_power_dropout(&powerDropout)) != ERROR_OK)
1740 return retval;
1742 int powerRestored;
1743 powerRestored = prevPowerdropout && !powerDropout;
1744 if (powerRestored)
1746 runPowerRestore = 1;
1749 long long current = timeval_ms();
1750 static long long lastPower = 0;
1751 int waitMore = lastPower + 2000 > current;
1752 if (powerDropout && !waitMore)
1754 runPowerDropout = 1;
1755 lastPower = current;
1758 if ((retval = jtag_srst_asserted(&srstAsserted)) != ERROR_OK)
1759 return retval;
1761 int srstDeasserted;
1762 srstDeasserted = prevSrstAsserted && !srstAsserted;
1764 static long long lastSrst = 0;
1765 waitMore = lastSrst + 2000 > current;
1766 if (srstDeasserted && !waitMore)
1768 runSrstDeasserted = 1;
1769 lastSrst = current;
1772 if (!prevSrstAsserted && srstAsserted)
1774 runSrstAsserted = 1;
1777 prevSrstAsserted = srstAsserted;
1778 prevPowerdropout = powerDropout;
1780 if (srstDeasserted || powerRestored)
1782 /* Other than logging the event we can't do anything here.
1783 * Issuing a reset is a particularly bad idea as we might
1784 * be inside a reset already.
1788 return ERROR_OK;
1791 static void target_call_event_callbacks_all(enum target_event e) {
1792 struct target *target;
1793 target = all_targets;
1794 while (target) {
1795 target_call_event_callbacks(target, e);
1796 target = target->next;
1800 /* process target state changes */
1801 static int handle_target(void *priv)
1803 Jim_Interp *interp = (Jim_Interp *)priv;
1804 int retval = ERROR_OK;
1806 /* we do not want to recurse here... */
1807 static int recursive = 0;
1808 if (! recursive)
1810 recursive = 1;
1811 sense_handler();
1812 /* danger! running these procedures can trigger srst assertions and power dropouts.
1813 * We need to avoid an infinite loop/recursion here and we do that by
1814 * clearing the flags after running these events.
1816 int did_something = 0;
1817 if (runSrstAsserted)
1819 target_call_event_callbacks_all(TARGET_EVENT_GDB_HALT);
1820 Jim_Eval(interp, "srst_asserted");
1821 did_something = 1;
1823 if (runSrstDeasserted)
1825 Jim_Eval(interp, "srst_deasserted");
1826 did_something = 1;
1828 if (runPowerDropout)
1830 target_call_event_callbacks_all(TARGET_EVENT_GDB_HALT);
1831 Jim_Eval(interp, "power_dropout");
1832 did_something = 1;
1834 if (runPowerRestore)
1836 Jim_Eval(interp, "power_restore");
1837 did_something = 1;
1840 if (did_something)
1842 /* clear detect flags */
1843 sense_handler();
1846 /* clear action flags */
1848 runSrstAsserted = 0;
1849 runSrstDeasserted = 0;
1850 runPowerRestore = 0;
1851 runPowerDropout = 0;
1853 recursive = 0;
1856 /* Poll targets for state changes unless that's globally disabled.
1857 * Skip targets that are currently disabled.
1859 for (struct target *target = all_targets;
1860 is_jtag_poll_safe() && target;
1861 target = target->next)
1863 if (!target->tap->enabled)
1864 continue;
1866 /* only poll target if we've got power and srst isn't asserted */
1867 if (!powerDropout && !srstAsserted)
1869 /* polling may fail silently until the target has been examined */
1870 if ((retval = target_poll(target)) != ERROR_OK)
1872 target_call_event_callbacks(target, TARGET_EVENT_GDB_HALT);
1873 return retval;
1878 return retval;
1881 COMMAND_HANDLER(handle_reg_command)
1883 struct target *target;
1884 struct reg *reg = NULL;
1885 unsigned count = 0;
1886 char *value;
1888 LOG_DEBUG("-");
1890 target = get_current_target(CMD_CTX);
1892 /* list all available registers for the current target */
1893 if (CMD_ARGC == 0)
1895 struct reg_cache *cache = target->reg_cache;
1897 count = 0;
1898 while (cache)
1900 unsigned i;
1902 command_print(CMD_CTX, "===== %s", cache->name);
1904 for (i = 0, reg = cache->reg_list;
1905 i < cache->num_regs;
1906 i++, reg++, count++)
1908 /* only print cached values if they are valid */
1909 if (reg->valid) {
1910 value = buf_to_str(reg->value,
1911 reg->size, 16);
1912 command_print(CMD_CTX,
1913 "(%i) %s (/%" PRIu32 "): 0x%s%s",
1914 count, reg->name,
1915 reg->size, value,
1916 reg->dirty
1917 ? " (dirty)"
1918 : "");
1919 free(value);
1920 } else {
1921 command_print(CMD_CTX, "(%i) %s (/%" PRIu32 ")",
1922 count, reg->name,
1923 reg->size) ;
1926 cache = cache->next;
1929 return ERROR_OK;
1932 /* access a single register by its ordinal number */
1933 if ((CMD_ARGV[0][0] >= '0') && (CMD_ARGV[0][0] <= '9'))
1935 unsigned num;
1936 COMMAND_PARSE_NUMBER(uint, CMD_ARGV[0], num);
1938 struct reg_cache *cache = target->reg_cache;
1939 count = 0;
1940 while (cache)
1942 unsigned i;
1943 for (i = 0; i < cache->num_regs; i++)
1945 if (count++ == num)
1947 reg = &cache->reg_list[i];
1948 break;
1951 if (reg)
1952 break;
1953 cache = cache->next;
1956 if (!reg)
1958 command_print(CMD_CTX, "%i is out of bounds, the current target has only %i registers (0 - %i)", num, count, count - 1);
1959 return ERROR_OK;
1961 } else /* access a single register by its name */
1963 reg = register_get_by_name(target->reg_cache, CMD_ARGV[0], 1);
1965 if (!reg)
1967 command_print(CMD_CTX, "register %s not found in current target", CMD_ARGV[0]);
1968 return ERROR_OK;
1972 /* display a register */
1973 if ((CMD_ARGC == 1) || ((CMD_ARGC == 2) && !((CMD_ARGV[1][0] >= '0') && (CMD_ARGV[1][0] <= '9'))))
1975 if ((CMD_ARGC == 2) && (strcmp(CMD_ARGV[1], "force") == 0))
1976 reg->valid = 0;
1978 if (reg->valid == 0)
1980 reg->type->get(reg);
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);
1985 return ERROR_OK;
1988 /* set register value */
1989 if (CMD_ARGC == 2)
1991 uint8_t *buf = malloc(DIV_ROUND_UP(reg->size, 8));
1992 str_to_buf(CMD_ARGV[1], strlen(CMD_ARGV[1]), buf, reg->size, 0);
1994 reg->type->set(reg, buf);
1996 value = buf_to_str(reg->value, reg->size, 16);
1997 command_print(CMD_CTX, "%s (/%i): 0x%s", reg->name, (int)(reg->size), value);
1998 free(value);
2000 free(buf);
2002 return ERROR_OK;
2005 command_print(CMD_CTX, "usage: reg <#|name> [value]");
2007 return ERROR_OK;
2010 COMMAND_HANDLER(handle_poll_command)
2012 int retval = ERROR_OK;
2013 struct target *target = get_current_target(CMD_CTX);
2015 if (CMD_ARGC == 0)
2017 command_print(CMD_CTX, "background polling: %s",
2018 jtag_poll_get_enabled() ? "on" : "off");
2019 command_print(CMD_CTX, "TAP: %s (%s)",
2020 target->tap->dotted_name,
2021 target->tap->enabled ? "enabled" : "disabled");
2022 if (!target->tap->enabled)
2023 return ERROR_OK;
2024 if ((retval = target_poll(target)) != ERROR_OK)
2025 return retval;
2026 if ((retval = target_arch_state(target)) != ERROR_OK)
2027 return retval;
2029 else if (CMD_ARGC == 1)
2031 bool enable;
2032 COMMAND_PARSE_ON_OFF(CMD_ARGV[0], enable);
2033 jtag_poll_set_enabled(enable);
2035 else
2037 return ERROR_COMMAND_SYNTAX_ERROR;
2040 return retval;
2043 COMMAND_HANDLER(handle_wait_halt_command)
2045 if (CMD_ARGC > 1)
2046 return ERROR_COMMAND_SYNTAX_ERROR;
2048 unsigned ms = 5000;
2049 if (1 == CMD_ARGC)
2051 int retval = parse_uint(CMD_ARGV[0], &ms);
2052 if (ERROR_OK != retval)
2054 command_print(CMD_CTX, "usage: %s [seconds]", CMD_NAME);
2055 return ERROR_COMMAND_SYNTAX_ERROR;
2057 // convert seconds (given) to milliseconds (needed)
2058 ms *= 1000;
2061 struct target *target = get_current_target(CMD_CTX);
2062 return target_wait_state(target, TARGET_HALTED, ms);
2065 /* wait for target state to change. The trick here is to have a low
2066 * latency for short waits and not to suck up all the CPU time
2067 * on longer waits.
2069 * After 500ms, keep_alive() is invoked
2071 int target_wait_state(struct target *target, enum target_state state, int ms)
2073 int retval;
2074 long long then = 0, cur;
2075 int once = 1;
2077 for (;;)
2079 if ((retval = target_poll(target)) != ERROR_OK)
2080 return retval;
2081 if (target->state == state)
2083 break;
2085 cur = timeval_ms();
2086 if (once)
2088 once = 0;
2089 then = timeval_ms();
2090 LOG_DEBUG("waiting for target %s...",
2091 Jim_Nvp_value2name_simple(nvp_target_state,state)->name);
2094 if (cur-then > 500)
2096 keep_alive();
2099 if ((cur-then) > ms)
2101 LOG_ERROR("timed out while waiting for target %s",
2102 Jim_Nvp_value2name_simple(nvp_target_state,state)->name);
2103 return ERROR_FAIL;
2107 return ERROR_OK;
2110 COMMAND_HANDLER(handle_halt_command)
2112 LOG_DEBUG("-");
2114 struct target *target = get_current_target(CMD_CTX);
2115 int retval = target_halt(target);
2116 if (ERROR_OK != retval)
2117 return retval;
2119 if (CMD_ARGC == 1)
2121 unsigned wait;
2122 retval = parse_uint(CMD_ARGV[0], &wait);
2123 if (ERROR_OK != retval)
2124 return ERROR_COMMAND_SYNTAX_ERROR;
2125 if (!wait)
2126 return ERROR_OK;
2129 return CALL_COMMAND_HANDLER(handle_wait_halt_command);
2132 COMMAND_HANDLER(handle_soft_reset_halt_command)
2134 struct target *target = get_current_target(CMD_CTX);
2136 LOG_USER("requesting target halt and executing a soft reset");
2138 target->type->soft_reset_halt(target);
2140 return ERROR_OK;
2143 COMMAND_HANDLER(handle_reset_command)
2145 if (CMD_ARGC > 1)
2146 return ERROR_COMMAND_SYNTAX_ERROR;
2148 enum target_reset_mode reset_mode = RESET_RUN;
2149 if (CMD_ARGC == 1)
2151 const Jim_Nvp *n;
2152 n = Jim_Nvp_name2value_simple(nvp_reset_modes, CMD_ARGV[0]);
2153 if ((n->name == NULL) || (n->value == RESET_UNKNOWN)) {
2154 return ERROR_COMMAND_SYNTAX_ERROR;
2156 reset_mode = n->value;
2159 /* reset *all* targets */
2160 return target_process_reset(CMD_CTX, reset_mode);
2164 COMMAND_HANDLER(handle_resume_command)
2166 int current = 1;
2167 if (CMD_ARGC > 1)
2168 return ERROR_COMMAND_SYNTAX_ERROR;
2170 struct target *target = get_current_target(CMD_CTX);
2171 target_handle_event(target, TARGET_EVENT_OLD_pre_resume);
2173 /* with no CMD_ARGV, resume from current pc, addr = 0,
2174 * with one arguments, addr = CMD_ARGV[0],
2175 * handle breakpoints, not debugging */
2176 uint32_t addr = 0;
2177 if (CMD_ARGC == 1)
2179 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], addr);
2180 current = 0;
2183 return target_resume(target, current, addr, 1, 0);
2186 COMMAND_HANDLER(handle_step_command)
2188 if (CMD_ARGC > 1)
2189 return ERROR_COMMAND_SYNTAX_ERROR;
2191 LOG_DEBUG("-");
2193 /* with no CMD_ARGV, step from current pc, addr = 0,
2194 * with one argument addr = CMD_ARGV[0],
2195 * handle breakpoints, debugging */
2196 uint32_t addr = 0;
2197 int current_pc = 1;
2198 if (CMD_ARGC == 1)
2200 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], addr);
2201 current_pc = 0;
2204 struct target *target = get_current_target(CMD_CTX);
2206 return target->type->step(target, current_pc, addr, 1);
2209 static void handle_md_output(struct command_context *cmd_ctx,
2210 struct target *target, uint32_t address, unsigned size,
2211 unsigned count, const uint8_t *buffer)
2213 const unsigned line_bytecnt = 32;
2214 unsigned line_modulo = line_bytecnt / size;
2216 char output[line_bytecnt * 4 + 1];
2217 unsigned output_len = 0;
2219 const char *value_fmt;
2220 switch (size) {
2221 case 4: value_fmt = "%8.8x "; break;
2222 case 2: value_fmt = "%4.2x "; break;
2223 case 1: value_fmt = "%2.2x "; break;
2224 default:
2225 LOG_ERROR("invalid memory read size: %u", size);
2226 exit(-1);
2229 for (unsigned i = 0; i < count; i++)
2231 if (i % line_modulo == 0)
2233 output_len += snprintf(output + output_len,
2234 sizeof(output) - output_len,
2235 "0x%8.8x: ",
2236 (unsigned)(address + (i*size)));
2239 uint32_t value = 0;
2240 const uint8_t *value_ptr = buffer + i * size;
2241 switch (size) {
2242 case 4: value = target_buffer_get_u32(target, value_ptr); break;
2243 case 2: value = target_buffer_get_u16(target, value_ptr); break;
2244 case 1: value = *value_ptr;
2246 output_len += snprintf(output + output_len,
2247 sizeof(output) - output_len,
2248 value_fmt, value);
2250 if ((i % line_modulo == line_modulo - 1) || (i == count - 1))
2252 command_print(cmd_ctx, "%s", output);
2253 output_len = 0;
2258 COMMAND_HANDLER(handle_md_command)
2260 if (CMD_ARGC < 1)
2261 return ERROR_COMMAND_SYNTAX_ERROR;
2263 unsigned size = 0;
2264 switch (CMD_NAME[2]) {
2265 case 'w': size = 4; break;
2266 case 'h': size = 2; break;
2267 case 'b': size = 1; break;
2268 default: return ERROR_COMMAND_SYNTAX_ERROR;
2271 bool physical=strcmp(CMD_ARGV[0], "phys")==0;
2272 int (*fn)(struct target *target,
2273 uint32_t address, uint32_t size, uint32_t count, uint8_t *buffer);
2274 if (physical)
2276 CMD_ARGC--;
2277 CMD_ARGV++;
2278 fn=target_read_phys_memory;
2279 } else
2281 fn=target_read_memory;
2283 if ((CMD_ARGC < 1) || (CMD_ARGC > 2))
2285 return ERROR_COMMAND_SYNTAX_ERROR;
2288 uint32_t address;
2289 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], address);
2291 unsigned count = 1;
2292 if (CMD_ARGC == 2)
2293 COMMAND_PARSE_NUMBER(uint, CMD_ARGV[1], count);
2295 uint8_t *buffer = calloc(count, size);
2297 struct target *target = get_current_target(CMD_CTX);
2298 int retval = fn(target, address, size, count, buffer);
2299 if (ERROR_OK == retval)
2300 handle_md_output(CMD_CTX, target, address, size, count, buffer);
2302 free(buffer);
2304 return retval;
2307 COMMAND_HANDLER(handle_mw_command)
2309 if (CMD_ARGC < 2)
2311 return ERROR_COMMAND_SYNTAX_ERROR;
2313 bool physical=strcmp(CMD_ARGV[0], "phys")==0;
2314 int (*fn)(struct target *target,
2315 uint32_t address, uint32_t size, uint32_t count, uint8_t *buffer);
2316 if (physical)
2318 CMD_ARGC--;
2319 CMD_ARGV++;
2320 fn=target_write_phys_memory;
2321 } else
2323 fn=target_write_memory;
2325 if ((CMD_ARGC < 2) || (CMD_ARGC > 3))
2326 return ERROR_COMMAND_SYNTAX_ERROR;
2328 uint32_t address;
2329 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], address);
2331 uint32_t value;
2332 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], value);
2334 unsigned count = 1;
2335 if (CMD_ARGC == 3)
2336 COMMAND_PARSE_NUMBER(uint, CMD_ARGV[2], count);
2338 struct target *target = get_current_target(CMD_CTX);
2339 unsigned wordsize;
2340 uint8_t value_buf[4];
2341 switch (CMD_NAME[2])
2343 case 'w':
2344 wordsize = 4;
2345 target_buffer_set_u32(target, value_buf, value);
2346 break;
2347 case 'h':
2348 wordsize = 2;
2349 target_buffer_set_u16(target, value_buf, value);
2350 break;
2351 case 'b':
2352 wordsize = 1;
2353 value_buf[0] = value;
2354 break;
2355 default:
2356 return ERROR_COMMAND_SYNTAX_ERROR;
2358 for (unsigned i = 0; i < count; i++)
2360 int retval = fn(target,
2361 address + i * wordsize, wordsize, 1, value_buf);
2362 if (ERROR_OK != retval)
2363 return retval;
2364 keep_alive();
2367 return ERROR_OK;
2371 static COMMAND_HELPER(parse_load_image_command_CMD_ARGV, struct image *image,
2372 uint32_t *min_address, uint32_t *max_address)
2374 if (CMD_ARGC < 1 || CMD_ARGC > 5)
2375 return ERROR_COMMAND_SYNTAX_ERROR;
2377 /* a base address isn't always necessary,
2378 * default to 0x0 (i.e. don't relocate) */
2379 if (CMD_ARGC >= 2)
2381 uint32_t addr;
2382 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], addr);
2383 image->base_address = addr;
2384 image->base_address_set = 1;
2386 else
2387 image->base_address_set = 0;
2389 image->start_address_set = 0;
2391 if (CMD_ARGC >= 4)
2393 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[3], *min_address);
2395 if (CMD_ARGC == 5)
2397 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[4], *max_address);
2398 // use size (given) to find max (required)
2399 *max_address += *min_address;
2402 if (*min_address > *max_address)
2403 return ERROR_COMMAND_SYNTAX_ERROR;
2405 return ERROR_OK;
2408 COMMAND_HANDLER(handle_load_image_command)
2410 uint8_t *buffer;
2411 size_t buf_cnt;
2412 uint32_t image_size;
2413 uint32_t min_address = 0;
2414 uint32_t max_address = 0xffffffff;
2415 int i;
2416 struct image image;
2418 int retval = CALL_COMMAND_HANDLER(parse_load_image_command_CMD_ARGV,
2419 &image, &min_address, &max_address);
2420 if (ERROR_OK != retval)
2421 return retval;
2423 struct target *target = get_current_target(CMD_CTX);
2425 struct duration bench;
2426 duration_start(&bench);
2428 if (image_open(&image, CMD_ARGV[0], (CMD_ARGC >= 3) ? CMD_ARGV[2] : NULL) != ERROR_OK)
2430 return ERROR_OK;
2433 image_size = 0x0;
2434 retval = ERROR_OK;
2435 for (i = 0; i < image.num_sections; i++)
2437 buffer = malloc(image.sections[i].size);
2438 if (buffer == NULL)
2440 command_print(CMD_CTX,
2441 "error allocating buffer for section (%d bytes)",
2442 (int)(image.sections[i].size));
2443 break;
2446 if ((retval = image_read_section(&image, i, 0x0, image.sections[i].size, buffer, &buf_cnt)) != ERROR_OK)
2448 free(buffer);
2449 break;
2452 uint32_t offset = 0;
2453 uint32_t length = buf_cnt;
2455 /* DANGER!!! beware of unsigned comparision here!!! */
2457 if ((image.sections[i].base_address + buf_cnt >= min_address)&&
2458 (image.sections[i].base_address < max_address))
2460 if (image.sections[i].base_address < min_address)
2462 /* clip addresses below */
2463 offset += min_address-image.sections[i].base_address;
2464 length -= offset;
2467 if (image.sections[i].base_address + buf_cnt > max_address)
2469 length -= (image.sections[i].base_address + buf_cnt)-max_address;
2472 if ((retval = target_write_buffer(target, image.sections[i].base_address + offset, length, buffer + offset)) != ERROR_OK)
2474 free(buffer);
2475 break;
2477 image_size += length;
2478 command_print(CMD_CTX, "%u bytes written at address 0x%8.8" PRIx32 "",
2479 (unsigned int)length,
2480 image.sections[i].base_address + offset);
2483 free(buffer);
2486 if ((ERROR_OK == retval) && (duration_measure(&bench) == ERROR_OK))
2488 command_print(CMD_CTX, "downloaded %" PRIu32 " bytes "
2489 "in %fs (%0.3f kb/s)", image_size,
2490 duration_elapsed(&bench), duration_kbps(&bench, image_size));
2493 image_close(&image);
2495 return retval;
2499 COMMAND_HANDLER(handle_dump_image_command)
2501 struct fileio fileio;
2503 uint8_t buffer[560];
2504 int retvaltemp;
2507 struct target *target = get_current_target(CMD_CTX);
2509 if (CMD_ARGC != 3)
2511 command_print(CMD_CTX, "usage: dump_image <filename> <address> <size>");
2512 return ERROR_OK;
2515 uint32_t address;
2516 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], address);
2517 uint32_t size;
2518 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[2], size);
2520 if (fileio_open(&fileio, CMD_ARGV[0], FILEIO_WRITE, FILEIO_BINARY) != ERROR_OK)
2522 return ERROR_OK;
2525 struct duration bench;
2526 duration_start(&bench);
2528 int retval = ERROR_OK;
2529 while (size > 0)
2531 size_t size_written;
2532 uint32_t this_run_size = (size > 560) ? 560 : size;
2533 retval = target_read_buffer(target, address, this_run_size, buffer);
2534 if (retval != ERROR_OK)
2536 break;
2539 retval = fileio_write(&fileio, this_run_size, buffer, &size_written);
2540 if (retval != ERROR_OK)
2542 break;
2545 size -= this_run_size;
2546 address += this_run_size;
2549 if ((retvaltemp = fileio_close(&fileio)) != ERROR_OK)
2550 return retvaltemp;
2552 if ((ERROR_OK == retval) && (duration_measure(&bench) == ERROR_OK))
2554 command_print(CMD_CTX,
2555 "dumped %zu bytes in %fs (%0.3f kb/s)", fileio.size,
2556 duration_elapsed(&bench), duration_kbps(&bench, fileio.size));
2559 return retval;
2562 static COMMAND_HELPER(handle_verify_image_command_internal, int verify)
2564 uint8_t *buffer;
2565 size_t buf_cnt;
2566 uint32_t image_size;
2567 int i;
2568 int retval;
2569 uint32_t checksum = 0;
2570 uint32_t mem_checksum = 0;
2572 struct image image;
2574 struct target *target = get_current_target(CMD_CTX);
2576 if (CMD_ARGC < 1)
2578 return ERROR_COMMAND_SYNTAX_ERROR;
2581 if (!target)
2583 LOG_ERROR("no target selected");
2584 return ERROR_FAIL;
2587 struct duration bench;
2588 duration_start(&bench);
2590 if (CMD_ARGC >= 2)
2592 uint32_t addr;
2593 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], addr);
2594 image.base_address = addr;
2595 image.base_address_set = 1;
2597 else
2599 image.base_address_set = 0;
2600 image.base_address = 0x0;
2603 image.start_address_set = 0;
2605 if ((retval = image_open(&image, CMD_ARGV[0], (CMD_ARGC == 3) ? CMD_ARGV[2] : NULL)) != ERROR_OK)
2607 return retval;
2610 image_size = 0x0;
2611 retval = ERROR_OK;
2612 for (i = 0; i < image.num_sections; i++)
2614 buffer = malloc(image.sections[i].size);
2615 if (buffer == NULL)
2617 command_print(CMD_CTX,
2618 "error allocating buffer for section (%d bytes)",
2619 (int)(image.sections[i].size));
2620 break;
2622 if ((retval = image_read_section(&image, i, 0x0, image.sections[i].size, buffer, &buf_cnt)) != ERROR_OK)
2624 free(buffer);
2625 break;
2628 if (verify)
2630 /* calculate checksum of image */
2631 image_calculate_checksum(buffer, buf_cnt, &checksum);
2633 retval = target_checksum_memory(target, image.sections[i].base_address, buf_cnt, &mem_checksum);
2634 if (retval != ERROR_OK)
2636 free(buffer);
2637 break;
2640 if (checksum != mem_checksum)
2642 /* failed crc checksum, fall back to a binary compare */
2643 uint8_t *data;
2645 command_print(CMD_CTX, "checksum mismatch - attempting binary compare");
2647 data = (uint8_t*)malloc(buf_cnt);
2649 /* Can we use 32bit word accesses? */
2650 int size = 1;
2651 int count = buf_cnt;
2652 if ((count % 4) == 0)
2654 size *= 4;
2655 count /= 4;
2657 retval = target_read_memory(target, image.sections[i].base_address, size, count, data);
2658 if (retval == ERROR_OK)
2660 uint32_t t;
2661 for (t = 0; t < buf_cnt; t++)
2663 if (data[t] != buffer[t])
2665 command_print(CMD_CTX,
2666 "Verify operation failed address 0x%08x. Was 0x%02x instead of 0x%02x\n",
2667 (unsigned)(t + image.sections[i].base_address),
2668 data[t],
2669 buffer[t]);
2670 free(data);
2671 free(buffer);
2672 retval = ERROR_FAIL;
2673 goto done;
2675 if ((t%16384) == 0)
2677 keep_alive();
2682 free(data);
2684 } else
2686 command_print(CMD_CTX, "address 0x%08" PRIx32 " length 0x%08zx",
2687 image.sections[i].base_address,
2688 buf_cnt);
2691 free(buffer);
2692 image_size += buf_cnt;
2694 done:
2695 if ((ERROR_OK == retval) && (duration_measure(&bench) == ERROR_OK))
2697 command_print(CMD_CTX, "verified %" PRIu32 " bytes "
2698 "in %fs (%0.3f kb/s)", image_size,
2699 duration_elapsed(&bench), duration_kbps(&bench, image_size));
2702 image_close(&image);
2704 return retval;
2707 COMMAND_HANDLER(handle_verify_image_command)
2709 return CALL_COMMAND_HANDLER(handle_verify_image_command_internal, 1);
2712 COMMAND_HANDLER(handle_test_image_command)
2714 return CALL_COMMAND_HANDLER(handle_verify_image_command_internal, 0);
2717 static int handle_bp_command_list(struct command_context *cmd_ctx)
2719 struct target *target = get_current_target(cmd_ctx);
2720 struct breakpoint *breakpoint = target->breakpoints;
2721 while (breakpoint)
2723 if (breakpoint->type == BKPT_SOFT)
2725 char* buf = buf_to_str(breakpoint->orig_instr,
2726 breakpoint->length, 16);
2727 command_print(cmd_ctx, "0x%8.8" PRIx32 ", 0x%x, %i, 0x%s",
2728 breakpoint->address,
2729 breakpoint->length,
2730 breakpoint->set, buf);
2731 free(buf);
2733 else
2735 command_print(cmd_ctx, "0x%8.8" PRIx32 ", 0x%x, %i",
2736 breakpoint->address,
2737 breakpoint->length, breakpoint->set);
2740 breakpoint = breakpoint->next;
2742 return ERROR_OK;
2745 static int handle_bp_command_set(struct command_context *cmd_ctx,
2746 uint32_t addr, uint32_t length, int hw)
2748 struct target *target = get_current_target(cmd_ctx);
2749 int retval = breakpoint_add(target, addr, length, hw);
2750 if (ERROR_OK == retval)
2751 command_print(cmd_ctx, "breakpoint set at 0x%8.8" PRIx32 "", addr);
2752 else
2753 LOG_ERROR("Failure setting breakpoint");
2754 return retval;
2757 COMMAND_HANDLER(handle_bp_command)
2759 if (CMD_ARGC == 0)
2760 return handle_bp_command_list(CMD_CTX);
2762 if (CMD_ARGC < 2 || CMD_ARGC > 3)
2764 command_print(CMD_CTX, "usage: bp <address> <length> ['hw']");
2765 return ERROR_COMMAND_SYNTAX_ERROR;
2768 uint32_t addr;
2769 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], addr);
2770 uint32_t length;
2771 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], length);
2773 int hw = BKPT_SOFT;
2774 if (CMD_ARGC == 3)
2776 if (strcmp(CMD_ARGV[2], "hw") == 0)
2777 hw = BKPT_HARD;
2778 else
2779 return ERROR_COMMAND_SYNTAX_ERROR;
2782 return handle_bp_command_set(CMD_CTX, addr, length, hw);
2785 COMMAND_HANDLER(handle_rbp_command)
2787 if (CMD_ARGC != 1)
2788 return ERROR_COMMAND_SYNTAX_ERROR;
2790 uint32_t addr;
2791 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], addr);
2793 struct target *target = get_current_target(CMD_CTX);
2794 breakpoint_remove(target, addr);
2796 return ERROR_OK;
2799 COMMAND_HANDLER(handle_wp_command)
2801 struct target *target = get_current_target(CMD_CTX);
2803 if (CMD_ARGC == 0)
2805 struct watchpoint *watchpoint = target->watchpoints;
2807 while (watchpoint)
2809 command_print(CMD_CTX, "address: 0x%8.8" PRIx32
2810 ", len: 0x%8.8" PRIx32
2811 ", r/w/a: %i, value: 0x%8.8" PRIx32
2812 ", mask: 0x%8.8" PRIx32,
2813 watchpoint->address,
2814 watchpoint->length,
2815 (int)watchpoint->rw,
2816 watchpoint->value,
2817 watchpoint->mask);
2818 watchpoint = watchpoint->next;
2820 return ERROR_OK;
2823 enum watchpoint_rw type = WPT_ACCESS;
2824 uint32_t addr = 0;
2825 uint32_t length = 0;
2826 uint32_t data_value = 0x0;
2827 uint32_t data_mask = 0xffffffff;
2829 switch (CMD_ARGC)
2831 case 5:
2832 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[4], data_mask);
2833 // fall through
2834 case 4:
2835 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[3], data_value);
2836 // fall through
2837 case 3:
2838 switch (CMD_ARGV[2][0])
2840 case 'r':
2841 type = WPT_READ;
2842 break;
2843 case 'w':
2844 type = WPT_WRITE;
2845 break;
2846 case 'a':
2847 type = WPT_ACCESS;
2848 break;
2849 default:
2850 LOG_ERROR("invalid watchpoint mode ('%c')", CMD_ARGV[2][0]);
2851 return ERROR_COMMAND_SYNTAX_ERROR;
2853 // fall through
2854 case 2:
2855 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], length);
2856 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], addr);
2857 break;
2859 default:
2860 command_print(CMD_CTX, "usage: wp [address length "
2861 "[(r|w|a) [value [mask]]]]");
2862 return ERROR_COMMAND_SYNTAX_ERROR;
2865 int retval = watchpoint_add(target, addr, length, type,
2866 data_value, data_mask);
2867 if (ERROR_OK != retval)
2868 LOG_ERROR("Failure setting watchpoints");
2870 return retval;
2873 COMMAND_HANDLER(handle_rwp_command)
2875 if (CMD_ARGC != 1)
2876 return ERROR_COMMAND_SYNTAX_ERROR;
2878 uint32_t addr;
2879 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], addr);
2881 struct target *target = get_current_target(CMD_CTX);
2882 watchpoint_remove(target, addr);
2884 return ERROR_OK;
2889 * Translate a virtual address to a physical address.
2891 * The low-level target implementation must have logged a detailed error
2892 * which is forwarded to telnet/GDB session.
2894 COMMAND_HANDLER(handle_virt2phys_command)
2896 if (CMD_ARGC != 1)
2897 return ERROR_COMMAND_SYNTAX_ERROR;
2899 uint32_t va;
2900 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], va);
2901 uint32_t pa;
2903 struct target *target = get_current_target(CMD_CTX);
2904 int retval = target->type->virt2phys(target, va, &pa);
2905 if (retval == ERROR_OK)
2906 command_print(CMD_CTX, "Physical address 0x%08" PRIx32 "", pa);
2908 return retval;
2911 static void writeData(FILE *f, const void *data, size_t len)
2913 size_t written = fwrite(data, 1, len, f);
2914 if (written != len)
2915 LOG_ERROR("failed to write %zu bytes: %s", len, strerror(errno));
2918 static void writeLong(FILE *f, int l)
2920 int i;
2921 for (i = 0; i < 4; i++)
2923 char c = (l >> (i*8))&0xff;
2924 writeData(f, &c, 1);
2929 static void writeString(FILE *f, char *s)
2931 writeData(f, s, strlen(s));
2934 /* Dump a gmon.out histogram file. */
2935 static void writeGmon(uint32_t *samples, uint32_t sampleNum, const char *filename)
2937 uint32_t i;
2938 FILE *f = fopen(filename, "w");
2939 if (f == NULL)
2940 return;
2941 writeString(f, "gmon");
2942 writeLong(f, 0x00000001); /* Version */
2943 writeLong(f, 0); /* padding */
2944 writeLong(f, 0); /* padding */
2945 writeLong(f, 0); /* padding */
2947 uint8_t zero = 0; /* GMON_TAG_TIME_HIST */
2948 writeData(f, &zero, 1);
2950 /* figure out bucket size */
2951 uint32_t min = samples[0];
2952 uint32_t max = samples[0];
2953 for (i = 0; i < sampleNum; i++)
2955 if (min > samples[i])
2957 min = samples[i];
2959 if (max < samples[i])
2961 max = samples[i];
2965 int addressSpace = (max-min + 1);
2967 static const uint32_t maxBuckets = 256 * 1024; /* maximum buckets. */
2968 uint32_t length = addressSpace;
2969 if (length > maxBuckets)
2971 length = maxBuckets;
2973 int *buckets = malloc(sizeof(int)*length);
2974 if (buckets == NULL)
2976 fclose(f);
2977 return;
2979 memset(buckets, 0, sizeof(int)*length);
2980 for (i = 0; i < sampleNum;i++)
2982 uint32_t address = samples[i];
2983 long long a = address-min;
2984 long long b = length-1;
2985 long long c = addressSpace-1;
2986 int index = (a*b)/c; /* danger!!!! int32 overflows */
2987 buckets[index]++;
2990 /* append binary memory gmon.out &profile_hist_hdr ((char*)&profile_hist_hdr + sizeof(struct gmon_hist_hdr)) */
2991 writeLong(f, min); /* low_pc */
2992 writeLong(f, max); /* high_pc */
2993 writeLong(f, length); /* # of samples */
2994 writeLong(f, 64000000); /* 64MHz */
2995 writeString(f, "seconds");
2996 for (i = 0; i < (15-strlen("seconds")); i++)
2997 writeData(f, &zero, 1);
2998 writeString(f, "s");
3000 /*append binary memory gmon.out profile_hist_data (profile_hist_data + profile_hist_hdr.hist_size) */
3002 char *data = malloc(2*length);
3003 if (data != NULL)
3005 for (i = 0; i < length;i++)
3007 int val;
3008 val = buckets[i];
3009 if (val > 65535)
3011 val = 65535;
3013 data[i*2]=val&0xff;
3014 data[i*2 + 1]=(val >> 8)&0xff;
3016 free(buckets);
3017 writeData(f, data, length * 2);
3018 free(data);
3019 } else
3021 free(buckets);
3024 fclose(f);
3027 /* profiling samples the CPU PC as quickly as OpenOCD is able, which will be used as a random sampling of PC */
3028 COMMAND_HANDLER(handle_profile_command)
3030 struct target *target = get_current_target(CMD_CTX);
3031 struct timeval timeout, now;
3033 gettimeofday(&timeout, NULL);
3034 if (CMD_ARGC != 2)
3036 return ERROR_COMMAND_SYNTAX_ERROR;
3038 unsigned offset;
3039 COMMAND_PARSE_NUMBER(uint, CMD_ARGV[0], offset);
3041 timeval_add_time(&timeout, offset, 0);
3043 command_print(CMD_CTX, "Starting profiling. Halting and resuming the target as often as we can...");
3045 static const int maxSample = 10000;
3046 uint32_t *samples = malloc(sizeof(uint32_t)*maxSample);
3047 if (samples == NULL)
3048 return ERROR_OK;
3050 int numSamples = 0;
3051 /* hopefully it is safe to cache! We want to stop/restart as quickly as possible. */
3052 struct reg *reg = register_get_by_name(target->reg_cache, "pc", 1);
3054 for (;;)
3056 int retval;
3057 target_poll(target);
3058 if (target->state == TARGET_HALTED)
3060 uint32_t t=*((uint32_t *)reg->value);
3061 samples[numSamples++]=t;
3062 retval = target_resume(target, 1, 0, 0, 0); /* current pc, addr = 0, do not handle breakpoints, not debugging */
3063 target_poll(target);
3064 alive_sleep(10); /* sleep 10ms, i.e. <100 samples/second. */
3065 } else if (target->state == TARGET_RUNNING)
3067 /* We want to quickly sample the PC. */
3068 if ((retval = target_halt(target)) != ERROR_OK)
3070 free(samples);
3071 return retval;
3073 } else
3075 command_print(CMD_CTX, "Target not halted or running");
3076 retval = ERROR_OK;
3077 break;
3079 if (retval != ERROR_OK)
3081 break;
3084 gettimeofday(&now, NULL);
3085 if ((numSamples >= maxSample) || ((now.tv_sec >= timeout.tv_sec) && (now.tv_usec >= timeout.tv_usec)))
3087 command_print(CMD_CTX, "Profiling completed. %d samples.", numSamples);
3088 if ((retval = target_poll(target)) != ERROR_OK)
3090 free(samples);
3091 return retval;
3093 if (target->state == TARGET_HALTED)
3095 target_resume(target, 1, 0, 0, 0); /* current pc, addr = 0, do not handle breakpoints, not debugging */
3097 if ((retval = target_poll(target)) != ERROR_OK)
3099 free(samples);
3100 return retval;
3102 writeGmon(samples, numSamples, CMD_ARGV[1]);
3103 command_print(CMD_CTX, "Wrote %s", CMD_ARGV[1]);
3104 break;
3107 free(samples);
3109 return ERROR_OK;
3112 static int new_int_array_element(Jim_Interp * interp, const char *varname, int idx, uint32_t val)
3114 char *namebuf;
3115 Jim_Obj *nameObjPtr, *valObjPtr;
3116 int result;
3118 namebuf = alloc_printf("%s(%d)", varname, idx);
3119 if (!namebuf)
3120 return JIM_ERR;
3122 nameObjPtr = Jim_NewStringObj(interp, namebuf, -1);
3123 valObjPtr = Jim_NewIntObj(interp, val);
3124 if (!nameObjPtr || !valObjPtr)
3126 free(namebuf);
3127 return JIM_ERR;
3130 Jim_IncrRefCount(nameObjPtr);
3131 Jim_IncrRefCount(valObjPtr);
3132 result = Jim_SetVariable(interp, nameObjPtr, valObjPtr);
3133 Jim_DecrRefCount(interp, nameObjPtr);
3134 Jim_DecrRefCount(interp, valObjPtr);
3135 free(namebuf);
3136 /* printf("%s(%d) <= 0%08x\n", varname, idx, val); */
3137 return result;
3140 static int jim_mem2array(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
3142 struct command_context *context;
3143 struct target *target;
3145 context = Jim_GetAssocData(interp, "context");
3146 if (context == NULL)
3148 LOG_ERROR("mem2array: no command context");
3149 return JIM_ERR;
3151 target = get_current_target(context);
3152 if (target == NULL)
3154 LOG_ERROR("mem2array: no current target");
3155 return JIM_ERR;
3158 return target_mem2array(interp, target, argc-1, argv + 1);
3161 static int target_mem2array(Jim_Interp *interp, struct target *target, int argc, Jim_Obj *const *argv)
3163 long l;
3164 uint32_t width;
3165 int len;
3166 uint32_t addr;
3167 uint32_t count;
3168 uint32_t v;
3169 const char *varname;
3170 int n, e, retval;
3171 uint32_t i;
3173 /* argv[1] = name of array to receive the data
3174 * argv[2] = desired width
3175 * argv[3] = memory address
3176 * argv[4] = count of times to read
3178 if (argc != 4) {
3179 Jim_WrongNumArgs(interp, 1, argv, "varname width addr nelems");
3180 return JIM_ERR;
3182 varname = Jim_GetString(argv[0], &len);
3183 /* given "foo" get space for worse case "foo(%d)" .. add 20 */
3185 e = Jim_GetLong(interp, argv[1], &l);
3186 width = l;
3187 if (e != JIM_OK) {
3188 return e;
3191 e = Jim_GetLong(interp, argv[2], &l);
3192 addr = l;
3193 if (e != JIM_OK) {
3194 return e;
3196 e = Jim_GetLong(interp, argv[3], &l);
3197 len = l;
3198 if (e != JIM_OK) {
3199 return e;
3201 switch (width) {
3202 case 8:
3203 width = 1;
3204 break;
3205 case 16:
3206 width = 2;
3207 break;
3208 case 32:
3209 width = 4;
3210 break;
3211 default:
3212 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3213 Jim_AppendStrings(interp, Jim_GetResult(interp), "Invalid width param, must be 8/16/32", NULL);
3214 return JIM_ERR;
3216 if (len == 0) {
3217 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3218 Jim_AppendStrings(interp, Jim_GetResult(interp), "mem2array: zero width read?", NULL);
3219 return JIM_ERR;
3221 if ((addr + (len * width)) < addr) {
3222 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3223 Jim_AppendStrings(interp, Jim_GetResult(interp), "mem2array: addr + len - wraps to zero?", NULL);
3224 return JIM_ERR;
3226 /* absurd transfer size? */
3227 if (len > 65536) {
3228 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3229 Jim_AppendStrings(interp, Jim_GetResult(interp), "mem2array: absurd > 64K item request", NULL);
3230 return JIM_ERR;
3233 if ((width == 1) ||
3234 ((width == 2) && ((addr & 1) == 0)) ||
3235 ((width == 4) && ((addr & 3) == 0))) {
3236 /* all is well */
3237 } else {
3238 char buf[100];
3239 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3240 sprintf(buf, "mem2array address: 0x%08" PRIx32 " is not aligned for %" PRId32 " byte reads",
3241 addr,
3242 width);
3243 Jim_AppendStrings(interp, Jim_GetResult(interp), buf , NULL);
3244 return JIM_ERR;
3247 /* Transfer loop */
3249 /* index counter */
3250 n = 0;
3252 size_t buffersize = 4096;
3253 uint8_t *buffer = malloc(buffersize);
3254 if (buffer == NULL)
3255 return JIM_ERR;
3257 /* assume ok */
3258 e = JIM_OK;
3259 while (len) {
3260 /* Slurp... in buffer size chunks */
3262 count = len; /* in objects.. */
3263 if (count > (buffersize/width)) {
3264 count = (buffersize/width);
3267 retval = target_read_memory(target, addr, width, count, buffer);
3268 if (retval != ERROR_OK) {
3269 /* BOO !*/
3270 LOG_ERROR("mem2array: Read @ 0x%08x, w=%d, cnt=%d, failed",
3271 (unsigned int)addr,
3272 (int)width,
3273 (int)count);
3274 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3275 Jim_AppendStrings(interp, Jim_GetResult(interp), "mem2array: cannot read memory", NULL);
3276 e = JIM_ERR;
3277 len = 0;
3278 } else {
3279 v = 0; /* shut up gcc */
3280 for (i = 0 ;i < count ;i++, n++) {
3281 switch (width) {
3282 case 4:
3283 v = target_buffer_get_u32(target, &buffer[i*width]);
3284 break;
3285 case 2:
3286 v = target_buffer_get_u16(target, &buffer[i*width]);
3287 break;
3288 case 1:
3289 v = buffer[i] & 0x0ff;
3290 break;
3292 new_int_array_element(interp, varname, n, v);
3294 len -= count;
3298 free(buffer);
3300 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3302 return JIM_OK;
3305 static int get_int_array_element(Jim_Interp * interp, const char *varname, int idx, uint32_t *val)
3307 char *namebuf;
3308 Jim_Obj *nameObjPtr, *valObjPtr;
3309 int result;
3310 long l;
3312 namebuf = alloc_printf("%s(%d)", varname, idx);
3313 if (!namebuf)
3314 return JIM_ERR;
3316 nameObjPtr = Jim_NewStringObj(interp, namebuf, -1);
3317 if (!nameObjPtr)
3319 free(namebuf);
3320 return JIM_ERR;
3323 Jim_IncrRefCount(nameObjPtr);
3324 valObjPtr = Jim_GetVariable(interp, nameObjPtr, JIM_ERRMSG);
3325 Jim_DecrRefCount(interp, nameObjPtr);
3326 free(namebuf);
3327 if (valObjPtr == NULL)
3328 return JIM_ERR;
3330 result = Jim_GetLong(interp, valObjPtr, &l);
3331 /* printf("%s(%d) => 0%08x\n", varname, idx, val); */
3332 *val = l;
3333 return result;
3336 static int jim_array2mem(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
3338 struct command_context *context;
3339 struct target *target;
3341 context = Jim_GetAssocData(interp, "context");
3342 if (context == NULL) {
3343 LOG_ERROR("array2mem: no command context");
3344 return JIM_ERR;
3346 target = get_current_target(context);
3347 if (target == NULL) {
3348 LOG_ERROR("array2mem: no current target");
3349 return JIM_ERR;
3352 return target_array2mem(interp,target, argc-1, argv + 1);
3354 static int target_array2mem(Jim_Interp *interp, struct target *target, int argc, Jim_Obj *const *argv)
3356 long l;
3357 uint32_t width;
3358 int len;
3359 uint32_t addr;
3360 uint32_t count;
3361 uint32_t v;
3362 const char *varname;
3363 int n, e, retval;
3364 uint32_t i;
3366 /* argv[1] = name of array to get the data
3367 * argv[2] = desired width
3368 * argv[3] = memory address
3369 * argv[4] = count to write
3371 if (argc != 4) {
3372 Jim_WrongNumArgs(interp, 0, argv, "varname width addr nelems");
3373 return JIM_ERR;
3375 varname = Jim_GetString(argv[0], &len);
3376 /* given "foo" get space for worse case "foo(%d)" .. add 20 */
3378 e = Jim_GetLong(interp, argv[1], &l);
3379 width = l;
3380 if (e != JIM_OK) {
3381 return e;
3384 e = Jim_GetLong(interp, argv[2], &l);
3385 addr = l;
3386 if (e != JIM_OK) {
3387 return e;
3389 e = Jim_GetLong(interp, argv[3], &l);
3390 len = l;
3391 if (e != JIM_OK) {
3392 return e;
3394 switch (width) {
3395 case 8:
3396 width = 1;
3397 break;
3398 case 16:
3399 width = 2;
3400 break;
3401 case 32:
3402 width = 4;
3403 break;
3404 default:
3405 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3406 Jim_AppendStrings(interp, Jim_GetResult(interp), "Invalid width param, must be 8/16/32", NULL);
3407 return JIM_ERR;
3409 if (len == 0) {
3410 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3411 Jim_AppendStrings(interp, Jim_GetResult(interp), "array2mem: zero width read?", NULL);
3412 return JIM_ERR;
3414 if ((addr + (len * width)) < addr) {
3415 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3416 Jim_AppendStrings(interp, Jim_GetResult(interp), "array2mem: addr + len - wraps to zero?", NULL);
3417 return JIM_ERR;
3419 /* absurd transfer size? */
3420 if (len > 65536) {
3421 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3422 Jim_AppendStrings(interp, Jim_GetResult(interp), "array2mem: absurd > 64K item request", NULL);
3423 return JIM_ERR;
3426 if ((width == 1) ||
3427 ((width == 2) && ((addr & 1) == 0)) ||
3428 ((width == 4) && ((addr & 3) == 0))) {
3429 /* all is well */
3430 } else {
3431 char buf[100];
3432 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3433 sprintf(buf, "array2mem address: 0x%08x is not aligned for %d byte reads",
3434 (unsigned int)addr,
3435 (int)width);
3436 Jim_AppendStrings(interp, Jim_GetResult(interp), buf , NULL);
3437 return JIM_ERR;
3440 /* Transfer loop */
3442 /* index counter */
3443 n = 0;
3444 /* assume ok */
3445 e = JIM_OK;
3447 size_t buffersize = 4096;
3448 uint8_t *buffer = malloc(buffersize);
3449 if (buffer == NULL)
3450 return JIM_ERR;
3452 while (len) {
3453 /* Slurp... in buffer size chunks */
3455 count = len; /* in objects.. */
3456 if (count > (buffersize/width)) {
3457 count = (buffersize/width);
3460 v = 0; /* shut up gcc */
3461 for (i = 0 ;i < count ;i++, n++) {
3462 get_int_array_element(interp, varname, n, &v);
3463 switch (width) {
3464 case 4:
3465 target_buffer_set_u32(target, &buffer[i*width], v);
3466 break;
3467 case 2:
3468 target_buffer_set_u16(target, &buffer[i*width], v);
3469 break;
3470 case 1:
3471 buffer[i] = v & 0x0ff;
3472 break;
3475 len -= count;
3477 retval = target_write_memory(target, addr, width, count, buffer);
3478 if (retval != ERROR_OK) {
3479 /* BOO !*/
3480 LOG_ERROR("array2mem: Write @ 0x%08x, w=%d, cnt=%d, failed",
3481 (unsigned int)addr,
3482 (int)width,
3483 (int)count);
3484 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3485 Jim_AppendStrings(interp, Jim_GetResult(interp), "array2mem: cannot read memory", NULL);
3486 e = JIM_ERR;
3487 len = 0;
3491 free(buffer);
3493 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3495 return JIM_OK;
3498 void target_all_handle_event(enum target_event e)
3500 struct target *target;
3502 LOG_DEBUG("**all*targets: event: %d, %s",
3503 (int)e,
3504 Jim_Nvp_value2name_simple(nvp_target_event, e)->name);
3506 target = all_targets;
3507 while (target) {
3508 target_handle_event(target, e);
3509 target = target->next;
3514 /* FIX? should we propagate errors here rather than printing them
3515 * and continuing?
3517 void target_handle_event(struct target *target, enum target_event e)
3519 struct target_event_action *teap;
3521 for (teap = target->event_action; teap != NULL; teap = teap->next) {
3522 if (teap->event == e) {
3523 LOG_DEBUG("target: (%d) %s (%s) event: %d (%s) action: %s",
3524 target->target_number,
3525 target_name(target),
3526 target_type_name(target),
3528 Jim_Nvp_value2name_simple(nvp_target_event, e)->name,
3529 Jim_GetString(teap->body, NULL));
3530 if (Jim_EvalObj(teap->interp, teap->body) != JIM_OK)
3532 Jim_PrintErrorMessage(teap->interp);
3539 * Returns true only if the target has a handler for the specified event.
3541 bool target_has_event_action(struct target *target, enum target_event event)
3543 struct target_event_action *teap;
3545 for (teap = target->event_action; teap != NULL; teap = teap->next) {
3546 if (teap->event == event)
3547 return true;
3549 return false;
3552 enum target_cfg_param {
3553 TCFG_TYPE,
3554 TCFG_EVENT,
3555 TCFG_WORK_AREA_VIRT,
3556 TCFG_WORK_AREA_PHYS,
3557 TCFG_WORK_AREA_SIZE,
3558 TCFG_WORK_AREA_BACKUP,
3559 TCFG_ENDIAN,
3560 TCFG_VARIANT,
3561 TCFG_CHAIN_POSITION,
3564 static Jim_Nvp nvp_config_opts[] = {
3565 { .name = "-type", .value = TCFG_TYPE },
3566 { .name = "-event", .value = TCFG_EVENT },
3567 { .name = "-work-area-virt", .value = TCFG_WORK_AREA_VIRT },
3568 { .name = "-work-area-phys", .value = TCFG_WORK_AREA_PHYS },
3569 { .name = "-work-area-size", .value = TCFG_WORK_AREA_SIZE },
3570 { .name = "-work-area-backup", .value = TCFG_WORK_AREA_BACKUP },
3571 { .name = "-endian" , .value = TCFG_ENDIAN },
3572 { .name = "-variant", .value = TCFG_VARIANT },
3573 { .name = "-chain-position", .value = TCFG_CHAIN_POSITION },
3575 { .name = NULL, .value = -1 }
3578 static int target_configure(Jim_GetOptInfo *goi, struct target *target)
3580 Jim_Nvp *n;
3581 Jim_Obj *o;
3582 jim_wide w;
3583 char *cp;
3584 int e;
3586 /* parse config or cget options ... */
3587 while (goi->argc > 0) {
3588 Jim_SetEmptyResult(goi->interp);
3589 /* Jim_GetOpt_Debug(goi); */
3591 if (target->type->target_jim_configure) {
3592 /* target defines a configure function */
3593 /* target gets first dibs on parameters */
3594 e = (*(target->type->target_jim_configure))(target, goi);
3595 if (e == JIM_OK) {
3596 /* more? */
3597 continue;
3599 if (e == JIM_ERR) {
3600 /* An error */
3601 return e;
3603 /* otherwise we 'continue' below */
3605 e = Jim_GetOpt_Nvp(goi, nvp_config_opts, &n);
3606 if (e != JIM_OK) {
3607 Jim_GetOpt_NvpUnknown(goi, nvp_config_opts, 0);
3608 return e;
3610 switch (n->value) {
3611 case TCFG_TYPE:
3612 /* not setable */
3613 if (goi->isconfigure) {
3614 Jim_SetResult_sprintf(goi->interp,
3615 "not settable: %s", n->name);
3616 return JIM_ERR;
3617 } else {
3618 no_params:
3619 if (goi->argc != 0) {
3620 Jim_WrongNumArgs(goi->interp,
3621 goi->argc, goi->argv,
3622 "NO PARAMS");
3623 return JIM_ERR;
3626 Jim_SetResultString(goi->interp,
3627 target_type_name(target), -1);
3628 /* loop for more */
3629 break;
3630 case TCFG_EVENT:
3631 if (goi->argc == 0) {
3632 Jim_WrongNumArgs(goi->interp, goi->argc, goi->argv, "-event ?event-name? ...");
3633 return JIM_ERR;
3636 e = Jim_GetOpt_Nvp(goi, nvp_target_event, &n);
3637 if (e != JIM_OK) {
3638 Jim_GetOpt_NvpUnknown(goi, nvp_target_event, 1);
3639 return e;
3642 if (goi->isconfigure) {
3643 if (goi->argc != 1) {
3644 Jim_WrongNumArgs(goi->interp, goi->argc, goi->argv, "-event ?event-name? ?EVENT-BODY?");
3645 return JIM_ERR;
3647 } else {
3648 if (goi->argc != 0) {
3649 Jim_WrongNumArgs(goi->interp, goi->argc, goi->argv, "-event ?event-name?");
3650 return JIM_ERR;
3655 struct target_event_action *teap;
3657 teap = target->event_action;
3658 /* replace existing? */
3659 while (teap) {
3660 if (teap->event == (enum target_event)n->value) {
3661 break;
3663 teap = teap->next;
3666 if (goi->isconfigure) {
3667 bool replace = true;
3668 if (teap == NULL) {
3669 /* create new */
3670 teap = calloc(1, sizeof(*teap));
3671 replace = false;
3673 teap->event = n->value;
3674 teap->interp = goi->interp;
3675 Jim_GetOpt_Obj(goi, &o);
3676 if (teap->body) {
3677 Jim_DecrRefCount(teap->interp, teap->body);
3679 teap->body = Jim_DuplicateObj(goi->interp, o);
3681 * FIXME:
3682 * Tcl/TK - "tk events" have a nice feature.
3683 * See the "BIND" command.
3684 * We should support that here.
3685 * You can specify %X and %Y in the event code.
3686 * The idea is: %T - target name.
3687 * The idea is: %N - target number
3688 * The idea is: %E - event name.
3690 Jim_IncrRefCount(teap->body);
3692 if (!replace)
3694 /* add to head of event list */
3695 teap->next = target->event_action;
3696 target->event_action = teap;
3698 Jim_SetEmptyResult(goi->interp);
3699 } else {
3700 /* get */
3701 if (teap == NULL) {
3702 Jim_SetEmptyResult(goi->interp);
3703 } else {
3704 Jim_SetResult(goi->interp, Jim_DuplicateObj(goi->interp, teap->body));
3708 /* loop for more */
3709 break;
3711 case TCFG_WORK_AREA_VIRT:
3712 if (goi->isconfigure) {
3713 target_free_all_working_areas(target);
3714 e = Jim_GetOpt_Wide(goi, &w);
3715 if (e != JIM_OK) {
3716 return e;
3718 target->working_area_virt = w;
3719 target->working_area_virt_spec = true;
3720 } else {
3721 if (goi->argc != 0) {
3722 goto no_params;
3725 Jim_SetResult(goi->interp, Jim_NewIntObj(goi->interp, target->working_area_virt));
3726 /* loop for more */
3727 break;
3729 case TCFG_WORK_AREA_PHYS:
3730 if (goi->isconfigure) {
3731 target_free_all_working_areas(target);
3732 e = Jim_GetOpt_Wide(goi, &w);
3733 if (e != JIM_OK) {
3734 return e;
3736 target->working_area_phys = w;
3737 target->working_area_phys_spec = true;
3738 } else {
3739 if (goi->argc != 0) {
3740 goto no_params;
3743 Jim_SetResult(goi->interp, Jim_NewIntObj(goi->interp, target->working_area_phys));
3744 /* loop for more */
3745 break;
3747 case TCFG_WORK_AREA_SIZE:
3748 if (goi->isconfigure) {
3749 target_free_all_working_areas(target);
3750 e = Jim_GetOpt_Wide(goi, &w);
3751 if (e != JIM_OK) {
3752 return e;
3754 target->working_area_size = w;
3755 } else {
3756 if (goi->argc != 0) {
3757 goto no_params;
3760 Jim_SetResult(goi->interp, Jim_NewIntObj(goi->interp, target->working_area_size));
3761 /* loop for more */
3762 break;
3764 case TCFG_WORK_AREA_BACKUP:
3765 if (goi->isconfigure) {
3766 target_free_all_working_areas(target);
3767 e = Jim_GetOpt_Wide(goi, &w);
3768 if (e != JIM_OK) {
3769 return e;
3771 /* make this exactly 1 or 0 */
3772 target->backup_working_area = (!!w);
3773 } else {
3774 if (goi->argc != 0) {
3775 goto no_params;
3778 Jim_SetResult(goi->interp, Jim_NewIntObj(goi->interp, target->backup_working_area));
3779 /* loop for more e*/
3780 break;
3782 case TCFG_ENDIAN:
3783 if (goi->isconfigure) {
3784 e = Jim_GetOpt_Nvp(goi, nvp_target_endian, &n);
3785 if (e != JIM_OK) {
3786 Jim_GetOpt_NvpUnknown(goi, nvp_target_endian, 1);
3787 return e;
3789 target->endianness = n->value;
3790 } else {
3791 if (goi->argc != 0) {
3792 goto no_params;
3795 n = Jim_Nvp_value2name_simple(nvp_target_endian, target->endianness);
3796 if (n->name == NULL) {
3797 target->endianness = TARGET_LITTLE_ENDIAN;
3798 n = Jim_Nvp_value2name_simple(nvp_target_endian, target->endianness);
3800 Jim_SetResultString(goi->interp, n->name, -1);
3801 /* loop for more */
3802 break;
3804 case TCFG_VARIANT:
3805 if (goi->isconfigure) {
3806 if (goi->argc < 1) {
3807 Jim_SetResult_sprintf(goi->interp,
3808 "%s ?STRING?",
3809 n->name);
3810 return JIM_ERR;
3812 if (target->variant) {
3813 free((void *)(target->variant));
3815 e = Jim_GetOpt_String(goi, &cp, NULL);
3816 target->variant = strdup(cp);
3817 } else {
3818 if (goi->argc != 0) {
3819 goto no_params;
3822 Jim_SetResultString(goi->interp, target->variant,-1);
3823 /* loop for more */
3824 break;
3825 case TCFG_CHAIN_POSITION:
3826 if (goi->isconfigure) {
3827 Jim_Obj *o;
3828 struct jtag_tap *tap;
3829 target_free_all_working_areas(target);
3830 e = Jim_GetOpt_Obj(goi, &o);
3831 if (e != JIM_OK) {
3832 return e;
3834 tap = jtag_tap_by_jim_obj(goi->interp, o);
3835 if (tap == NULL) {
3836 return JIM_ERR;
3838 /* make this exactly 1 or 0 */
3839 target->tap = tap;
3840 } else {
3841 if (goi->argc != 0) {
3842 goto no_params;
3845 Jim_SetResultString(goi->interp, target->tap->dotted_name, -1);
3846 /* loop for more e*/
3847 break;
3849 } /* while (goi->argc) */
3852 /* done - we return */
3853 return JIM_OK;
3856 static int jim_target_configure(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
3858 Jim_GetOptInfo goi;
3859 Jim_GetOpt_Setup(&goi, interp, argc - 1, argv + 1);
3860 goi.isconfigure = strcmp(Jim_GetString(argv[0], NULL), "configure") == 0;
3861 int need_args = 1 + goi.isconfigure;
3862 if (goi.argc < need_args)
3864 Jim_WrongNumArgs(goi.interp, goi.argc, goi.argv,
3865 goi.isconfigure
3866 ? "missing: -option VALUE ..."
3867 : "missing: -option ...");
3868 return JIM_ERR;
3870 struct target *target = Jim_CmdPrivData(goi.interp);
3871 return target_configure(&goi, target);
3874 static int jim_target_mw(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
3876 const char *cmd_name = Jim_GetString(argv[0], NULL);
3878 Jim_GetOptInfo goi;
3879 Jim_GetOpt_Setup(&goi, interp, argc - 1, argv + 1);
3881 if (goi.argc != 2 && goi.argc != 3)
3883 Jim_SetResult_sprintf(goi.interp,
3884 "usage: %s <address> <data> [<count>]", cmd_name);
3885 return JIM_ERR;
3888 jim_wide a;
3889 int e = Jim_GetOpt_Wide(&goi, &a);
3890 if (e != JIM_OK)
3891 return e;
3893 jim_wide b;
3894 e = Jim_GetOpt_Wide(&goi, &b);
3895 if (e != JIM_OK)
3896 return e;
3898 jim_wide c = 1;
3899 if (goi.argc == 3)
3901 e = Jim_GetOpt_Wide(&goi, &c);
3902 if (e != JIM_OK)
3903 return e;
3906 struct target *target = Jim_CmdPrivData(goi.interp);
3907 uint8_t target_buf[32];
3908 if (strcasecmp(cmd_name, "mww") == 0) {
3909 target_buffer_set_u32(target, target_buf, b);
3910 b = 4;
3912 else if (strcasecmp(cmd_name, "mwh") == 0) {
3913 target_buffer_set_u16(target, target_buf, b);
3914 b = 2;
3916 else if (strcasecmp(cmd_name, "mwb") == 0) {
3917 target_buffer_set_u8(target, target_buf, b);
3918 b = 1;
3919 } else {
3920 LOG_ERROR("command '%s' unknown: ", cmd_name);
3921 return JIM_ERR;
3924 for (jim_wide x = 0; x < c; x++)
3926 e = target_write_memory(target, a, b, 1, target_buf);
3927 if (e != ERROR_OK)
3929 Jim_SetResult_sprintf(interp,
3930 "Error writing @ 0x%08x: %d\n", (int)(a), e);
3931 return JIM_ERR;
3933 /* b = width */
3934 a = a + b;
3936 return JIM_OK;
3939 static int jim_target_md(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
3941 const char *cmd_name = Jim_GetString(argv[0], NULL);
3943 Jim_GetOptInfo goi;
3944 Jim_GetOpt_Setup(&goi, interp, argc - 1, argv + 1);
3946 if ((goi.argc == 2) || (goi.argc == 3))
3948 Jim_SetResult_sprintf(goi.interp,
3949 "usage: %s <address> [<count>]", cmd_name);
3950 return JIM_ERR;
3953 jim_wide a;
3954 int e = Jim_GetOpt_Wide(&goi, &a);
3955 if (e != JIM_OK) {
3956 return JIM_ERR;
3958 jim_wide c;
3959 if (goi.argc) {
3960 e = Jim_GetOpt_Wide(&goi, &c);
3961 if (e != JIM_OK) {
3962 return JIM_ERR;
3964 } else {
3965 c = 1;
3967 jim_wide b = 1; /* shut up gcc */
3968 if (strcasecmp(cmd_name, "mdw") == 0)
3969 b = 4;
3970 else if (strcasecmp(cmd_name, "mdh") == 0)
3971 b = 2;
3972 else if (strcasecmp(cmd_name, "mdb") == 0)
3973 b = 1;
3974 else {
3975 LOG_ERROR("command '%s' unknown: ", cmd_name);
3976 return JIM_ERR;
3979 /* convert count to "bytes" */
3980 c = c * b;
3982 struct target *target = Jim_CmdPrivData(goi.interp);
3983 uint8_t target_buf[32];
3984 jim_wide x, y, z;
3985 while (c > 0) {
3986 y = c;
3987 if (y > 16) {
3988 y = 16;
3990 e = target_read_memory(target, a, b, y / b, target_buf);
3991 if (e != ERROR_OK) {
3992 Jim_SetResult_sprintf(interp, "error reading target @ 0x%08lx", (int)(a));
3993 return JIM_ERR;
3996 Jim_fprintf(interp, interp->cookie_stdout, "0x%08x ", (int)(a));
3997 switch (b) {
3998 case 4:
3999 for (x = 0; x < 16 && x < y; x += 4)
4001 z = target_buffer_get_u32(target, &(target_buf[ x * 4 ]));
4002 Jim_fprintf(interp, interp->cookie_stdout, "%08x ", (int)(z));
4004 for (; (x < 16) ; x += 4) {
4005 Jim_fprintf(interp, interp->cookie_stdout, " ");
4007 break;
4008 case 2:
4009 for (x = 0; x < 16 && x < y; x += 2)
4011 z = target_buffer_get_u16(target, &(target_buf[ x * 2 ]));
4012 Jim_fprintf(interp, interp->cookie_stdout, "%04x ", (int)(z));
4014 for (; (x < 16) ; x += 2) {
4015 Jim_fprintf(interp, interp->cookie_stdout, " ");
4017 break;
4018 case 1:
4019 default:
4020 for (x = 0 ; (x < 16) && (x < y) ; x += 1) {
4021 z = target_buffer_get_u8(target, &(target_buf[ x * 4 ]));
4022 Jim_fprintf(interp, interp->cookie_stdout, "%02x ", (int)(z));
4024 for (; (x < 16) ; x += 1) {
4025 Jim_fprintf(interp, interp->cookie_stdout, " ");
4027 break;
4029 /* ascii-ify the bytes */
4030 for (x = 0 ; x < y ; x++) {
4031 if ((target_buf[x] >= 0x20) &&
4032 (target_buf[x] <= 0x7e)) {
4033 /* good */
4034 } else {
4035 /* smack it */
4036 target_buf[x] = '.';
4039 /* space pad */
4040 while (x < 16) {
4041 target_buf[x] = ' ';
4042 x++;
4044 /* terminate */
4045 target_buf[16] = 0;
4046 /* print - with a newline */
4047 Jim_fprintf(interp, interp->cookie_stdout, "%s\n", target_buf);
4048 /* NEXT... */
4049 c -= 16;
4050 a += 16;
4052 return JIM_OK;
4055 static int jim_target_mem2array(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
4057 struct target *target = Jim_CmdPrivData(interp);
4058 return target_mem2array(interp, target, argc - 1, argv + 1);
4061 static int jim_target_array2mem(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
4063 struct target *target = Jim_CmdPrivData(interp);
4064 return target_array2mem(interp, target, argc - 1, argv + 1);
4067 static int jim_target_tap_disabled(Jim_Interp *interp)
4069 Jim_SetResult_sprintf(interp, "[TAP is disabled]");
4070 return JIM_ERR;
4073 static int jim_target_examine(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
4075 if (argc != 1)
4077 Jim_WrongNumArgs(interp, 1, argv, "[no parameters]");
4078 return JIM_ERR;
4080 struct target *target = Jim_CmdPrivData(interp);
4081 if (!target->tap->enabled)
4082 return jim_target_tap_disabled(interp);
4084 int e = target->type->examine(target);
4085 if (e != ERROR_OK)
4087 Jim_SetResult_sprintf(interp, "examine-fails: %d", e);
4088 return JIM_ERR;
4090 return JIM_OK;
4093 static int jim_target_poll(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
4095 if (argc != 1)
4097 Jim_WrongNumArgs(interp, 1, argv, "[no parameters]");
4098 return JIM_ERR;
4100 struct target *target = Jim_CmdPrivData(interp);
4101 if (!target->tap->enabled)
4102 return jim_target_tap_disabled(interp);
4104 int e;
4105 if (!(target_was_examined(target))) {
4106 e = ERROR_TARGET_NOT_EXAMINED;
4107 } else {
4108 e = target->type->poll(target);
4110 if (e != ERROR_OK)
4112 Jim_SetResult_sprintf(interp, "poll-fails: %d", e);
4113 return JIM_ERR;
4115 return JIM_OK;
4118 static int jim_target_reset(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
4120 Jim_GetOptInfo goi;
4121 Jim_GetOpt_Setup(&goi, interp, argc - 1, argv + 1);
4123 if (goi.argc != 2)
4125 Jim_WrongNumArgs(interp, 0, argv,
4126 "([tT]|[fF]|assert|deassert) BOOL");
4127 return JIM_ERR;
4130 Jim_Nvp *n;
4131 int e = Jim_GetOpt_Nvp(&goi, nvp_assert, &n);
4132 if (e != JIM_OK)
4134 Jim_GetOpt_NvpUnknown(&goi, nvp_assert, 1);
4135 return e;
4137 /* the halt or not param */
4138 jim_wide a;
4139 e = Jim_GetOpt_Wide(&goi, &a);
4140 if (e != JIM_OK)
4141 return e;
4143 struct target *target = Jim_CmdPrivData(goi.interp);
4144 if (!target->tap->enabled)
4145 return jim_target_tap_disabled(interp);
4146 if (!target->type->assert_reset || !target->type->deassert_reset)
4148 Jim_SetResult_sprintf(interp,
4149 "No target-specific reset for %s",
4150 target_name(target));
4151 return JIM_ERR;
4153 /* determine if we should halt or not. */
4154 target->reset_halt = !!a;
4155 /* When this happens - all workareas are invalid. */
4156 target_free_all_working_areas_restore(target, 0);
4158 /* do the assert */
4159 if (n->value == NVP_ASSERT) {
4160 e = target->type->assert_reset(target);
4161 } else {
4162 e = target->type->deassert_reset(target);
4164 return (e == ERROR_OK) ? JIM_OK : JIM_ERR;
4167 static int jim_target_halt(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
4169 if (argc != 1) {
4170 Jim_WrongNumArgs(interp, 1, argv, "[no parameters]");
4171 return JIM_ERR;
4173 struct target *target = Jim_CmdPrivData(interp);
4174 if (!target->tap->enabled)
4175 return jim_target_tap_disabled(interp);
4176 int e = target->type->halt(target);
4177 return (e == ERROR_OK) ? JIM_OK : JIM_ERR;
4180 static int jim_target_wait_state(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
4182 Jim_GetOptInfo goi;
4183 Jim_GetOpt_Setup(&goi, interp, argc - 1, argv + 1);
4185 /* params: <name> statename timeoutmsecs */
4186 if (goi.argc != 2)
4188 const char *cmd_name = Jim_GetString(argv[0], NULL);
4189 Jim_SetResult_sprintf(goi.interp,
4190 "%s <state_name> <timeout_in_msec>", cmd_name);
4191 return JIM_ERR;
4194 Jim_Nvp *n;
4195 int e = Jim_GetOpt_Nvp(&goi, nvp_target_state, &n);
4196 if (e != JIM_OK) {
4197 Jim_GetOpt_NvpUnknown(&goi, nvp_target_state,1);
4198 return e;
4200 jim_wide a;
4201 e = Jim_GetOpt_Wide(&goi, &a);
4202 if (e != JIM_OK) {
4203 return e;
4205 struct target *target = Jim_CmdPrivData(interp);
4206 if (!target->tap->enabled)
4207 return jim_target_tap_disabled(interp);
4209 e = target_wait_state(target, n->value, a);
4210 if (e != ERROR_OK)
4212 Jim_SetResult_sprintf(goi.interp,
4213 "target: %s wait %s fails (%d) %s",
4214 target_name(target), n->name,
4215 e, target_strerror_safe(e));
4216 return JIM_ERR;
4218 return JIM_OK;
4220 /* List for human, Events defined for this target.
4221 * scripts/programs should use 'name cget -event NAME'
4223 static int jim_target_event_list(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
4225 struct command_context *cmd_ctx = Jim_GetAssocData(interp, "context");
4226 struct target *target = Jim_CmdPrivData(interp);
4227 struct target_event_action *teap = target->event_action;
4228 command_print(cmd_ctx, "Event actions for target (%d) %s\n",
4229 target->target_number,
4230 target_name(target));
4231 command_print(cmd_ctx, "%-25s | Body", "Event");
4232 command_print(cmd_ctx, "------------------------- | "
4233 "----------------------------------------");
4234 while (teap)
4236 Jim_Nvp *opt = Jim_Nvp_value2name_simple(nvp_target_event, teap->event);
4237 command_print(cmd_ctx, "%-25s | %s",
4238 opt->name, Jim_GetString(teap->body, NULL));
4239 teap = teap->next;
4241 command_print(cmd_ctx, "***END***");
4242 return JIM_OK;
4244 static int jim_target_current_state(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
4246 if (argc != 1)
4248 Jim_WrongNumArgs(interp, 1, argv, "[no parameters]");
4249 return JIM_ERR;
4251 struct target *target = Jim_CmdPrivData(interp);
4252 Jim_SetResultString(interp, target_state_name(target), -1);
4253 return JIM_OK;
4255 static int jim_target_invoke_event(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
4257 Jim_GetOptInfo goi;
4258 Jim_GetOpt_Setup(&goi, interp, argc - 1, argv + 1);
4259 if (goi.argc != 1)
4261 const char *cmd_name = Jim_GetString(argv[0], NULL);
4262 Jim_SetResult_sprintf(goi.interp, "%s <eventname>", cmd_name);
4263 return JIM_ERR;
4265 Jim_Nvp *n;
4266 int e = Jim_GetOpt_Nvp(&goi, nvp_target_event, &n);
4267 if (e != JIM_OK)
4269 Jim_GetOpt_NvpUnknown(&goi, nvp_target_event, 1);
4270 return e;
4272 struct target *target = Jim_CmdPrivData(interp);
4273 target_handle_event(target, n->value);
4274 return JIM_OK;
4277 static const struct command_registration target_instance_command_handlers[] = {
4279 .name = "configure",
4280 .mode = COMMAND_CONFIG,
4281 .jim_handler = &jim_target_configure,
4282 .usage = "[<target_options> ...]",
4283 .help = "configure a new target for use",
4286 .name = "cget",
4287 .mode = COMMAND_ANY,
4288 .jim_handler = &jim_target_configure,
4289 .usage = "<target_type> [<target_options> ...]",
4290 .help = "configure a new target for use",
4293 .name = "mww",
4294 .mode = COMMAND_EXEC,
4295 .jim_handler = &jim_target_mw,
4296 .usage = "<address> <data> [<count>]",
4297 .help = "Write 32-bit word(s) to target memory",
4300 .name = "mwh",
4301 .mode = COMMAND_EXEC,
4302 .jim_handler = &jim_target_mw,
4303 .usage = "<address> <data> [<count>]",
4304 .help = "Write 16-bit half-word(s) to target memory",
4307 .name = "mwb",
4308 .mode = COMMAND_EXEC,
4309 .jim_handler = &jim_target_mw,
4310 .usage = "<address> <data> [<count>]",
4311 .help = "Write byte(s) to target memory",
4314 .name = "mdw",
4315 .mode = COMMAND_EXEC,
4316 .jim_handler = &jim_target_md,
4317 .usage = "<address> [<count>]",
4318 .help = "Display target memory as 32-bit words",
4321 .name = "mdh",
4322 .mode = COMMAND_EXEC,
4323 .jim_handler = &jim_target_md,
4324 .usage = "<address> [<count>]",
4325 .help = "Display target memory as 16-bit half-words",
4328 .name = "mdb",
4329 .mode = COMMAND_EXEC,
4330 .jim_handler = &jim_target_md,
4331 .usage = "<address> [<count>]",
4332 .help = "Display target memory as 8-bit bytes",
4335 .name = "array2mem",
4336 .mode = COMMAND_EXEC,
4337 .jim_handler = &jim_target_array2mem,
4340 .name = "mem2array",
4341 .mode = COMMAND_EXEC,
4342 .jim_handler = &jim_target_mem2array,
4345 .name = "eventlist",
4346 .mode = COMMAND_EXEC,
4347 .jim_handler = &jim_target_event_list,
4350 .name = "curstate",
4351 .mode = COMMAND_EXEC,
4352 .jim_handler = &jim_target_current_state,
4355 .name = "arp_examine",
4356 .mode = COMMAND_EXEC,
4357 .jim_handler = &jim_target_examine,
4360 .name = "arp_poll",
4361 .mode = COMMAND_EXEC,
4362 .jim_handler = &jim_target_poll,
4365 .name = "arp_reset",
4366 .mode = COMMAND_EXEC,
4367 .jim_handler = &jim_target_reset,
4370 .name = "arp_halt",
4371 .mode = COMMAND_EXEC,
4372 .jim_handler = &jim_target_halt,
4375 .name = "arp_waitstate",
4376 .mode = COMMAND_EXEC,
4377 .jim_handler = &jim_target_wait_state,
4380 .name = "invoke-event",
4381 .mode = COMMAND_EXEC,
4382 .jim_handler = &jim_target_invoke_event,
4384 COMMAND_REGISTRATION_DONE
4387 static int target_create(Jim_GetOptInfo *goi)
4389 Jim_Obj *new_cmd;
4390 Jim_Cmd *cmd;
4391 const char *cp;
4392 char *cp2;
4393 int e;
4394 int x;
4395 struct target *target;
4396 struct command_context *cmd_ctx;
4398 cmd_ctx = Jim_GetAssocData(goi->interp, "context");
4399 if (goi->argc < 3) {
4400 Jim_WrongNumArgs(goi->interp, 1, goi->argv, "?name? ?type? ..options...");
4401 return JIM_ERR;
4404 /* COMMAND */
4405 Jim_GetOpt_Obj(goi, &new_cmd);
4406 /* does this command exist? */
4407 cmd = Jim_GetCommand(goi->interp, new_cmd, JIM_ERRMSG);
4408 if (cmd) {
4409 cp = Jim_GetString(new_cmd, NULL);
4410 Jim_SetResult_sprintf(goi->interp, "Command/target: %s Exists", cp);
4411 return JIM_ERR;
4414 /* TYPE */
4415 e = Jim_GetOpt_String(goi, &cp2, NULL);
4416 cp = cp2;
4417 /* now does target type exist */
4418 for (x = 0 ; target_types[x] ; x++) {
4419 if (0 == strcmp(cp, target_types[x]->name)) {
4420 /* found */
4421 break;
4424 if (target_types[x] == NULL) {
4425 Jim_SetResult_sprintf(goi->interp, "Unknown target type %s, try one of ", cp);
4426 for (x = 0 ; target_types[x] ; x++) {
4427 if (target_types[x + 1]) {
4428 Jim_AppendStrings(goi->interp,
4429 Jim_GetResult(goi->interp),
4430 target_types[x]->name,
4431 ", ", NULL);
4432 } else {
4433 Jim_AppendStrings(goi->interp,
4434 Jim_GetResult(goi->interp),
4435 " or ",
4436 target_types[x]->name,NULL);
4439 return JIM_ERR;
4442 /* Create it */
4443 target = calloc(1,sizeof(struct target));
4444 /* set target number */
4445 target->target_number = new_target_number();
4447 /* allocate memory for each unique target type */
4448 target->type = (struct target_type*)calloc(1,sizeof(struct target_type));
4450 memcpy(target->type, target_types[x], sizeof(struct target_type));
4452 /* will be set by "-endian" */
4453 target->endianness = TARGET_ENDIAN_UNKNOWN;
4455 target->working_area = 0x0;
4456 target->working_area_size = 0x0;
4457 target->working_areas = NULL;
4458 target->backup_working_area = 0;
4460 target->state = TARGET_UNKNOWN;
4461 target->debug_reason = DBG_REASON_UNDEFINED;
4462 target->reg_cache = NULL;
4463 target->breakpoints = NULL;
4464 target->watchpoints = NULL;
4465 target->next = NULL;
4466 target->arch_info = NULL;
4468 target->display = 1;
4470 target->halt_issued = false;
4472 /* initialize trace information */
4473 target->trace_info = malloc(sizeof(struct trace));
4474 target->trace_info->num_trace_points = 0;
4475 target->trace_info->trace_points_size = 0;
4476 target->trace_info->trace_points = NULL;
4477 target->trace_info->trace_history_size = 0;
4478 target->trace_info->trace_history = NULL;
4479 target->trace_info->trace_history_pos = 0;
4480 target->trace_info->trace_history_overflowed = 0;
4482 target->dbgmsg = NULL;
4483 target->dbg_msg_enabled = 0;
4485 target->endianness = TARGET_ENDIAN_UNKNOWN;
4487 /* Do the rest as "configure" options */
4488 goi->isconfigure = 1;
4489 e = target_configure(goi, target);
4491 if (target->tap == NULL)
4493 Jim_SetResultString(goi->interp, "-chain-position required when creating target", -1);
4494 e = JIM_ERR;
4497 if (e != JIM_OK) {
4498 free(target->type);
4499 free(target);
4500 return e;
4503 if (target->endianness == TARGET_ENDIAN_UNKNOWN) {
4504 /* default endian to little if not specified */
4505 target->endianness = TARGET_LITTLE_ENDIAN;
4508 /* incase variant is not set */
4509 if (!target->variant)
4510 target->variant = strdup("");
4512 cp = Jim_GetString(new_cmd, NULL);
4513 target->cmd_name = strdup(cp);
4515 /* create the target specific commands */
4516 if (target->type->commands) {
4517 e = register_commands(cmd_ctx, NULL, target->type->commands);
4518 if (ERROR_OK != e)
4519 LOG_ERROR("unable to register '%s' commands", cp);
4521 if (target->type->target_create) {
4522 (*(target->type->target_create))(target, goi->interp);
4525 /* append to end of list */
4527 struct target **tpp;
4528 tpp = &(all_targets);
4529 while (*tpp) {
4530 tpp = &((*tpp)->next);
4532 *tpp = target;
4535 /* now - create the new target name command */
4536 const const struct command_registration target_subcommands[] = {
4538 .chain = target_instance_command_handlers,
4541 .chain = target->type->commands,
4543 COMMAND_REGISTRATION_DONE
4545 const const struct command_registration target_commands[] = {
4547 .name = cp,
4548 .mode = COMMAND_ANY,
4549 .help = "target command group",
4550 .chain = target_subcommands,
4552 COMMAND_REGISTRATION_DONE
4554 e = register_commands(cmd_ctx, NULL, target_commands);
4555 if (ERROR_OK != e)
4556 return JIM_ERR;
4558 struct command *c = command_find_in_context(cmd_ctx, cp);
4559 assert(c);
4560 command_set_handler_data(c, target);
4562 return (ERROR_OK == e) ? JIM_OK : JIM_ERR;
4565 static int jim_target_current(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
4567 if (argc != 1)
4569 Jim_WrongNumArgs(interp, 1, argv, "Too many parameters");
4570 return JIM_ERR;
4572 struct command_context *cmd_ctx = Jim_GetAssocData(interp, "context");
4573 Jim_SetResultString(interp, get_current_target(cmd_ctx)->cmd_name, -1);
4574 return JIM_OK;
4577 static int jim_target_types(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
4579 if (argc != 1)
4581 Jim_WrongNumArgs(interp, 1, argv, "Too many parameters");
4582 return JIM_ERR;
4584 Jim_SetResult(interp, Jim_NewListObj(interp, NULL, 0));
4585 for (unsigned x = 0; NULL != target_types[x]; x++)
4587 Jim_ListAppendElement(interp, Jim_GetResult(interp),
4588 Jim_NewStringObj(interp, target_types[x]->name, -1));
4590 return JIM_OK;
4593 static int jim_target_names(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
4595 if (argc != 1)
4597 Jim_WrongNumArgs(interp, 1, argv, "Too many parameters");
4598 return JIM_ERR;
4600 Jim_SetResult(interp, Jim_NewListObj(interp, NULL, 0));
4601 struct target *target = all_targets;
4602 while (target)
4604 Jim_ListAppendElement(interp, Jim_GetResult(interp),
4605 Jim_NewStringObj(interp, target_name(target), -1));
4606 target = target->next;
4608 return JIM_OK;
4611 static int jim_target_create(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
4613 Jim_GetOptInfo goi;
4614 Jim_GetOpt_Setup(&goi, interp, argc - 1, argv + 1);
4615 if (goi.argc < 3)
4617 Jim_WrongNumArgs(goi.interp, goi.argc, goi.argv,
4618 "<name> <target_type> [<target_options> ...]");
4619 return JIM_ERR;
4621 return target_create(&goi);
4624 static int jim_target_number(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
4626 Jim_GetOptInfo goi;
4627 Jim_GetOpt_Setup(&goi, interp, argc - 1, argv + 1);
4629 /* It's OK to remove this mechanism sometime after August 2010 or so */
4630 LOG_WARNING("don't use numbers as target identifiers; use names");
4631 if (goi.argc != 1)
4633 Jim_SetResult_sprintf(goi.interp, "usage: target number <number>");
4634 return JIM_ERR;
4636 jim_wide w;
4637 int e = Jim_GetOpt_Wide(&goi, &w);
4638 if (e != JIM_OK)
4639 return JIM_ERR;
4641 struct target *target;
4642 for (target = all_targets; NULL != target; target = target->next)
4644 if (target->target_number != w)
4645 continue;
4647 Jim_SetResultString(goi.interp, target_name(target), -1);
4648 return JIM_OK;
4650 Jim_SetResult_sprintf(goi.interp,
4651 "Target: number %d does not exist", (int)(w));
4652 return JIM_ERR;
4655 static int jim_target_count(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
4657 if (argc != 1)
4659 Jim_WrongNumArgs(interp, 1, argv, "<no parameters>");
4660 return JIM_ERR;
4662 unsigned count = 0;
4663 struct target *target = all_targets;
4664 while (NULL != target)
4666 target = target->next;
4667 count++;
4669 Jim_SetResult(interp, Jim_NewIntObj(interp, count));
4670 return JIM_OK;
4673 static const struct command_registration target_subcommand_handlers[] = {
4675 .name = "create",
4676 .mode = COMMAND_ANY,
4677 .jim_handler = &jim_target_create,
4678 .usage = "<name> <type> ...",
4679 .help = "Returns the currently selected target",
4682 .name = "current",
4683 .mode = COMMAND_ANY,
4684 .jim_handler = &jim_target_current,
4685 .help = "Returns the currently selected target",
4688 .name = "types",
4689 .mode = COMMAND_ANY,
4690 .jim_handler = &jim_target_types,
4691 .help = "Returns the available target types as a list of strings",
4694 .name = "names",
4695 .mode = COMMAND_ANY,
4696 .jim_handler = &jim_target_names,
4697 .help = "Returns the names of all targets as a list of strings",
4700 .name = "number",
4701 .mode = COMMAND_ANY,
4702 .jim_handler = &jim_target_number,
4703 .usage = "<number>",
4704 .help = "Returns the name of target <n>",
4707 .name = "count",
4708 .mode = COMMAND_ANY,
4709 .jim_handler = &jim_target_count,
4710 .help = "Returns the number of targets as an integer",
4712 COMMAND_REGISTRATION_DONE
4716 struct FastLoad
4718 uint32_t address;
4719 uint8_t *data;
4720 int length;
4724 static int fastload_num;
4725 static struct FastLoad *fastload;
4727 static void free_fastload(void)
4729 if (fastload != NULL)
4731 int i;
4732 for (i = 0; i < fastload_num; i++)
4734 if (fastload[i].data)
4735 free(fastload[i].data);
4737 free(fastload);
4738 fastload = NULL;
4745 COMMAND_HANDLER(handle_fast_load_image_command)
4747 uint8_t *buffer;
4748 size_t buf_cnt;
4749 uint32_t image_size;
4750 uint32_t min_address = 0;
4751 uint32_t max_address = 0xffffffff;
4752 int i;
4754 struct image image;
4756 int retval = CALL_COMMAND_HANDLER(parse_load_image_command_CMD_ARGV,
4757 &image, &min_address, &max_address);
4758 if (ERROR_OK != retval)
4759 return retval;
4761 struct duration bench;
4762 duration_start(&bench);
4764 if (image_open(&image, CMD_ARGV[0], (CMD_ARGC >= 3) ? CMD_ARGV[2] : NULL) != ERROR_OK)
4766 return ERROR_OK;
4769 image_size = 0x0;
4770 retval = ERROR_OK;
4771 fastload_num = image.num_sections;
4772 fastload = (struct FastLoad *)malloc(sizeof(struct FastLoad)*image.num_sections);
4773 if (fastload == NULL)
4775 image_close(&image);
4776 return ERROR_FAIL;
4778 memset(fastload, 0, sizeof(struct FastLoad)*image.num_sections);
4779 for (i = 0; i < image.num_sections; i++)
4781 buffer = malloc(image.sections[i].size);
4782 if (buffer == NULL)
4784 command_print(CMD_CTX, "error allocating buffer for section (%d bytes)",
4785 (int)(image.sections[i].size));
4786 break;
4789 if ((retval = image_read_section(&image, i, 0x0, image.sections[i].size, buffer, &buf_cnt)) != ERROR_OK)
4791 free(buffer);
4792 break;
4795 uint32_t offset = 0;
4796 uint32_t length = buf_cnt;
4799 /* DANGER!!! beware of unsigned comparision here!!! */
4801 if ((image.sections[i].base_address + buf_cnt >= min_address)&&
4802 (image.sections[i].base_address < max_address))
4804 if (image.sections[i].base_address < min_address)
4806 /* clip addresses below */
4807 offset += min_address-image.sections[i].base_address;
4808 length -= offset;
4811 if (image.sections[i].base_address + buf_cnt > max_address)
4813 length -= (image.sections[i].base_address + buf_cnt)-max_address;
4816 fastload[i].address = image.sections[i].base_address + offset;
4817 fastload[i].data = malloc(length);
4818 if (fastload[i].data == NULL)
4820 free(buffer);
4821 break;
4823 memcpy(fastload[i].data, buffer + offset, length);
4824 fastload[i].length = length;
4826 image_size += length;
4827 command_print(CMD_CTX, "%u bytes written at address 0x%8.8x",
4828 (unsigned int)length,
4829 ((unsigned int)(image.sections[i].base_address + offset)));
4832 free(buffer);
4835 if ((ERROR_OK == retval) && (duration_measure(&bench) == ERROR_OK))
4837 command_print(CMD_CTX, "Loaded %" PRIu32 " bytes "
4838 "in %fs (%0.3f kb/s)", image_size,
4839 duration_elapsed(&bench), duration_kbps(&bench, image_size));
4841 command_print(CMD_CTX,
4842 "WARNING: image has not been loaded to target!"
4843 "You can issue a 'fast_load' to finish loading.");
4846 image_close(&image);
4848 if (retval != ERROR_OK)
4850 free_fastload();
4853 return retval;
4856 COMMAND_HANDLER(handle_fast_load_command)
4858 if (CMD_ARGC > 0)
4859 return ERROR_COMMAND_SYNTAX_ERROR;
4860 if (fastload == NULL)
4862 LOG_ERROR("No image in memory");
4863 return ERROR_FAIL;
4865 int i;
4866 int ms = timeval_ms();
4867 int size = 0;
4868 int retval = ERROR_OK;
4869 for (i = 0; i < fastload_num;i++)
4871 struct target *target = get_current_target(CMD_CTX);
4872 command_print(CMD_CTX, "Write to 0x%08x, length 0x%08x",
4873 (unsigned int)(fastload[i].address),
4874 (unsigned int)(fastload[i].length));
4875 if (retval == ERROR_OK)
4877 retval = target_write_buffer(target, fastload[i].address, fastload[i].length, fastload[i].data);
4879 size += fastload[i].length;
4881 int after = timeval_ms();
4882 command_print(CMD_CTX, "Loaded image %f kBytes/s", (float)(size/1024.0)/((float)(after-ms)/1000.0));
4883 return retval;
4886 static int jim_mcrmrc(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
4888 struct command_context *context;
4889 struct target *target;
4890 int retval;
4892 context = Jim_GetAssocData(interp, "context");
4893 if (context == NULL) {
4894 LOG_ERROR("array2mem: no command context");
4895 return JIM_ERR;
4897 target = get_current_target(context);
4898 if (target == NULL) {
4899 LOG_ERROR("array2mem: no current target");
4900 return JIM_ERR;
4903 if ((argc < 6) || (argc > 7))
4905 return JIM_ERR;
4908 int cpnum;
4909 uint32_t op1;
4910 uint32_t op2;
4911 uint32_t CRn;
4912 uint32_t CRm;
4913 uint32_t value;
4915 int e;
4916 long l;
4917 e = Jim_GetLong(interp, argv[1], &l);
4918 if (e != JIM_OK) {
4919 return e;
4921 cpnum = l;
4923 e = Jim_GetLong(interp, argv[2], &l);
4924 if (e != JIM_OK) {
4925 return e;
4927 op1 = l;
4929 e = Jim_GetLong(interp, argv[3], &l);
4930 if (e != JIM_OK) {
4931 return e;
4933 CRn = l;
4935 e = Jim_GetLong(interp, argv[4], &l);
4936 if (e != JIM_OK) {
4937 return e;
4939 CRm = l;
4941 e = Jim_GetLong(interp, argv[5], &l);
4942 if (e != JIM_OK) {
4943 return e;
4945 op2 = l;
4947 value = 0;
4949 if (argc == 7)
4951 e = Jim_GetLong(interp, argv[6], &l);
4952 if (e != JIM_OK) {
4953 return e;
4955 value = l;
4957 retval = target_mcr(target, cpnum, op1, op2, CRn, CRm, value);
4958 if (retval != ERROR_OK)
4959 return JIM_ERR;
4960 } else
4962 retval = target_mrc(target, cpnum, op1, op2, CRn, CRm, &value);
4963 if (retval != ERROR_OK)
4964 return JIM_ERR;
4966 Jim_SetResult(interp, Jim_NewIntObj(interp, value));
4969 return JIM_OK;
4972 static const struct command_registration target_command_handlers[] = {
4974 .name = "targets",
4975 .handler = &handle_targets_command,
4976 .mode = COMMAND_ANY,
4977 .help = "change current command line target (one parameter) "
4978 "or list targets (no parameters)",
4979 .usage = "[<new_current_target>]",
4982 .name = "target",
4983 .mode = COMMAND_CONFIG,
4984 .help = "configure target",
4986 .chain = target_subcommand_handlers,
4988 COMMAND_REGISTRATION_DONE
4991 int target_register_commands(struct command_context *cmd_ctx)
4993 return register_commands(cmd_ctx, NULL, target_command_handlers);
4996 static const struct command_registration target_exec_command_handlers[] = {
4998 .name = "fast_load_image",
4999 .handler = &handle_fast_load_image_command,
5000 .mode = COMMAND_ANY,
5001 .help = "Load image into memory, mainly for profiling purposes",
5002 .usage = "<file> <address> ['bin'|'ihex'|'elf'|'s19'] "
5003 "[min_address] [max_length]",
5006 .name = "fast_load",
5007 .handler = &handle_fast_load_command,
5008 .mode = COMMAND_ANY,
5009 .help = "loads active fast load image to current target "
5010 "- mainly for profiling purposes",
5013 .name = "profile",
5014 .handler = &handle_profile_command,
5015 .mode = COMMAND_EXEC,
5016 .help = "profiling samples the CPU PC",
5018 /** @todo don't register virt2phys() unless target supports it */
5020 .name = "virt2phys",
5021 .handler = &handle_virt2phys_command,
5022 .mode = COMMAND_ANY,
5023 .help = "translate a virtual address into a physical address",
5027 .name = "reg",
5028 .handler = &handle_reg_command,
5029 .mode = COMMAND_EXEC,
5030 .help = "display or set a register",
5034 .name = "poll",
5035 .handler = &handle_poll_command,
5036 .mode = COMMAND_EXEC,
5037 .help = "poll target state",
5040 .name = "wait_halt",
5041 .handler = &handle_wait_halt_command,
5042 .mode = COMMAND_EXEC,
5043 .help = "wait for target halt",
5044 .usage = "[time (s)]",
5047 .name = "halt",
5048 .handler = &handle_halt_command,
5049 .mode = COMMAND_EXEC,
5050 .help = "halt target",
5053 .name = "resume",
5054 .handler = &handle_resume_command,
5055 .mode = COMMAND_EXEC,
5056 .help = "resume target",
5057 .usage = "[<address>]",
5060 .name = "reset",
5061 .handler = &handle_reset_command,
5062 .mode = COMMAND_EXEC,
5063 .usage = "[run|halt|init]",
5064 .help = "Reset all targets into the specified mode."
5065 "Default reset mode is run, if not given.",
5068 .name = "soft_reset_halt",
5069 .handler = &handle_soft_reset_halt_command,
5070 .mode = COMMAND_EXEC,
5071 .help = "halt the target and do a soft reset",
5075 .name = "step",
5076 .handler = &handle_step_command,
5077 .mode = COMMAND_EXEC,
5078 .help = "step one instruction from current PC or [addr]",
5079 .usage = "[<address>]",
5083 .name = "mdw",
5084 .handler = &handle_md_command,
5085 .mode = COMMAND_EXEC,
5086 .help = "display memory words",
5087 .usage = "[phys] <addr> [count]",
5090 .name = "mdh",
5091 .handler = &handle_md_command,
5092 .mode = COMMAND_EXEC,
5093 .help = "display memory half-words",
5094 .usage = "[phys] <addr> [count]",
5097 .name = "mdb",
5098 .handler = &handle_md_command,
5099 .mode = COMMAND_EXEC,
5100 .help = "display memory bytes",
5101 .usage = "[phys] <addr> [count]",
5105 .name = "mww",
5106 .handler = &handle_mw_command,
5107 .mode = COMMAND_EXEC,
5108 .help = "write memory word",
5109 .usage = "[phys] <addr> <value> [count]",
5112 .name = "mwh",
5113 .handler = &handle_mw_command,
5114 .mode = COMMAND_EXEC,
5115 .help = "write memory half-word",
5116 .usage = "[phys] <addr> <value> [count]",
5119 .name = "mwb",
5120 .handler = &handle_mw_command,
5121 .mode = COMMAND_EXEC,
5122 .help = "write memory byte",
5123 .usage = "[phys] <addr> <value> [count]",
5127 .name = "bp",
5128 .handler = &handle_bp_command,
5129 .mode = COMMAND_EXEC,
5130 .help = "list or set breakpoint",
5131 .usage = "[<address> <length> [hw]]",
5134 .name = "rbp",
5135 .handler = &handle_rbp_command,
5136 .mode = COMMAND_EXEC,
5137 .help = "remove breakpoint",
5138 .usage = "<address>",
5142 .name = "wp",
5143 .handler = &handle_wp_command,
5144 .mode = COMMAND_EXEC,
5145 .help = "list or set watchpoint",
5146 .usage = "[<address> <length> <r/w/a> [value] [mask]]",
5149 .name = "rwp",
5150 .handler = &handle_rwp_command,
5151 .mode = COMMAND_EXEC,
5152 .help = "remove watchpoint",
5153 .usage = "<address>",
5157 .name = "load_image",
5158 .handler = &handle_load_image_command,
5159 .mode = COMMAND_EXEC,
5160 .usage = "<file> <address> ['bin'|'ihex'|'elf'|'s19'] "
5161 "[min_address] [max_length]",
5164 .name = "dump_image",
5165 .handler = &handle_dump_image_command,
5166 .mode = COMMAND_EXEC,
5167 .usage = "<file> <address> <size>",
5170 .name = "verify_image",
5171 .handler = &handle_verify_image_command,
5172 .mode = COMMAND_EXEC,
5173 .usage = "<file> [offset] [type]",
5176 .name = "test_image",
5177 .handler = &handle_test_image_command,
5178 .mode = COMMAND_EXEC,
5179 .usage = "<file> [offset] [type]",
5182 .name = "ocd_mem2array",
5183 .mode = COMMAND_EXEC,
5184 .jim_handler = &jim_mem2array,
5185 .help = "read memory and return as a TCL array "
5186 "for script processing",
5187 .usage = "<arrayname> <width=32|16|8> <address> <count>",
5190 .name = "ocd_array2mem",
5191 .mode = COMMAND_EXEC,
5192 .jim_handler = &jim_array2mem,
5193 .help = "convert a TCL array to memory locations "
5194 "and write the values",
5195 .usage = "<arrayname> <width=32|16|8> <address> <count>",
5197 COMMAND_REGISTRATION_DONE
5199 int target_register_user_commands(struct command_context *cmd_ctx)
5201 int retval = ERROR_OK;
5202 if ((retval = target_request_register_commands(cmd_ctx)) != ERROR_OK)
5203 return retval;
5205 if ((retval = trace_register_commands(cmd_ctx)) != ERROR_OK)
5206 return retval;
5209 return register_commands(cmd_ctx, NULL, target_exec_command_handlers);