1 /***************************************************************************
2 * Copyright (C) 2005 by Dominic Rath *
3 * Dominic.Rath@gmx.de *
5 * Copyright (C) 2007-2009 Øyvind Harboe *
6 * oyvind.harboe@zylin.com *
8 * Copyright (C) 2008, Duane Ellis *
9 * openocd@duaneeellis.com *
11 * Copyright (C) 2008 by Spencer Oliver *
12 * spen@spen-soft.co.uk *
14 * Copyright (C) 2008 by Rick Altherr *
15 * kc8apf@kc8apf.net> *
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. *
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. *
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 ***************************************************************************/
37 #include "target_type.h"
38 #include "target_request.h"
39 #include "breakpoints.h"
40 #include "time_support.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);
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[] =
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)
124 n = Jim_Nvp_value2name_simple(nvp_error_target, err);
125 if (n->name == NULL) {
132 static const Jim_Nvp nvp_target_event[] = {
133 { .value = TARGET_EVENT_OLD_gdb_program_config , .name = "old-gdb_program_config" },
134 { .value = TARGET_EVENT_OLD_pre_resume , .name = "old-pre_resume" },
136 { .value = TARGET_EVENT_GDB_HALT, .name = "gdb-halt" },
137 { .value = TARGET_EVENT_HALTED, .name = "halted" },
138 { .value = TARGET_EVENT_RESUMED, .name = "resumed" },
139 { .value = TARGET_EVENT_RESUME_START, .name = "resume-start" },
140 { .value = TARGET_EVENT_RESUME_END, .name = "resume-end" },
142 { .name = "gdb-start", .value = TARGET_EVENT_GDB_START },
143 { .name = "gdb-end", .value = TARGET_EVENT_GDB_END },
145 /* historical name */
147 { .value = TARGET_EVENT_RESET_START, .name = "reset-start" },
149 { .value = TARGET_EVENT_RESET_ASSERT_PRE, .name = "reset-assert-pre" },
150 { .value = TARGET_EVENT_RESET_ASSERT_POST, .name = "reset-assert-post" },
151 { .value = TARGET_EVENT_RESET_DEASSERT_PRE, .name = "reset-deassert-pre" },
152 { .value = TARGET_EVENT_RESET_DEASSERT_POST, .name = "reset-deassert-post" },
153 { .value = TARGET_EVENT_RESET_HALT_PRE, .name = "reset-halt-pre" },
154 { .value = TARGET_EVENT_RESET_HALT_POST, .name = "reset-halt-post" },
155 { .value = TARGET_EVENT_RESET_WAIT_PRE, .name = "reset-wait-pre" },
156 { .value = TARGET_EVENT_RESET_WAIT_POST, .name = "reset-wait-post" },
157 { .value = TARGET_EVENT_RESET_INIT , .name = "reset-init" },
158 { .value = TARGET_EVENT_RESET_END, .name = "reset-end" },
160 { .value = TARGET_EVENT_EXAMINE_START, .name = "examine-start" },
161 { .value = TARGET_EVENT_EXAMINE_END, .name = "examine-end" },
163 { .value = TARGET_EVENT_DEBUG_HALTED, .name = "debug-halted" },
164 { .value = TARGET_EVENT_DEBUG_RESUMED, .name = "debug-resumed" },
166 { .value = TARGET_EVENT_GDB_ATTACH, .name = "gdb-attach" },
167 { .value = TARGET_EVENT_GDB_DETACH, .name = "gdb-detach" },
169 { .value = TARGET_EVENT_GDB_FLASH_WRITE_START, .name = "gdb-flash-write-start" },
170 { .value = TARGET_EVENT_GDB_FLASH_WRITE_END , .name = "gdb-flash-write-end" },
172 { .value = TARGET_EVENT_GDB_FLASH_ERASE_START, .name = "gdb-flash-erase-start" },
173 { .value = TARGET_EVENT_GDB_FLASH_ERASE_END , .name = "gdb-flash-erase-end" },
175 { .value = TARGET_EVENT_RESUME_START, .name = "resume-start" },
176 { .value = TARGET_EVENT_RESUMED , .name = "resume-ok" },
177 { .value = TARGET_EVENT_RESUME_END , .name = "resume-end" },
179 { .name = NULL, .value = -1 }
182 const Jim_Nvp nvp_target_state[] = {
183 { .name = "unknown", .value = TARGET_UNKNOWN },
184 { .name = "running", .value = TARGET_RUNNING },
185 { .name = "halted", .value = TARGET_HALTED },
186 { .name = "reset", .value = TARGET_RESET },
187 { .name = "debug-running", .value = TARGET_DEBUG_RUNNING },
188 { .name = NULL, .value = -1 },
191 const Jim_Nvp nvp_target_debug_reason [] = {
192 { .name = "debug-request" , .value = DBG_REASON_DBGRQ },
193 { .name = "breakpoint" , .value = DBG_REASON_BREAKPOINT },
194 { .name = "watchpoint" , .value = DBG_REASON_WATCHPOINT },
195 { .name = "watchpoint-and-breakpoint", .value = DBG_REASON_WPTANDBKPT },
196 { .name = "single-step" , .value = DBG_REASON_SINGLESTEP },
197 { .name = "target-not-halted" , .value = DBG_REASON_NOTHALTED },
198 { .name = "undefined" , .value = DBG_REASON_UNDEFINED },
199 { .name = NULL, .value = -1 },
202 const Jim_Nvp nvp_target_endian[] = {
203 { .name = "big", .value = TARGET_BIG_ENDIAN },
204 { .name = "little", .value = TARGET_LITTLE_ENDIAN },
205 { .name = "be", .value = TARGET_BIG_ENDIAN },
206 { .name = "le", .value = TARGET_LITTLE_ENDIAN },
207 { .name = NULL, .value = -1 },
210 const Jim_Nvp nvp_reset_modes[] = {
211 { .name = "unknown", .value = RESET_UNKNOWN },
212 { .name = "run" , .value = RESET_RUN },
213 { .name = "halt" , .value = RESET_HALT },
214 { .name = "init" , .value = RESET_INIT },
215 { .name = NULL , .value = -1 },
219 target_state_name( struct target *t )
222 cp = Jim_Nvp_value2name_simple(nvp_target_state, t->state)->name;
224 LOG_ERROR("Invalid target state: %d", (int)(t->state));
225 cp = "(*BUG*unknown*BUG*)";
230 /* determine the number of the new target */
231 static int new_target_number(void)
236 /* number is 0 based */
240 if (x < t->target_number) {
241 x = t->target_number;
248 /* read a uint32_t from a buffer in target memory endianness */
249 uint32_t target_buffer_get_u32(struct target *target, const uint8_t *buffer)
251 if (target->endianness == TARGET_LITTLE_ENDIAN)
252 return le_to_h_u32(buffer);
254 return be_to_h_u32(buffer);
257 /* read a uint16_t from a buffer in target memory endianness */
258 uint16_t target_buffer_get_u16(struct target *target, const uint8_t *buffer)
260 if (target->endianness == TARGET_LITTLE_ENDIAN)
261 return le_to_h_u16(buffer);
263 return be_to_h_u16(buffer);
266 /* read a uint8_t from a buffer in target memory endianness */
267 uint8_t target_buffer_get_u8(struct target *target, const uint8_t *buffer)
269 return *buffer & 0x0ff;
272 /* write a uint32_t to a buffer in target memory endianness */
273 void target_buffer_set_u32(struct target *target, uint8_t *buffer, uint32_t value)
275 if (target->endianness == TARGET_LITTLE_ENDIAN)
276 h_u32_to_le(buffer, value);
278 h_u32_to_be(buffer, value);
281 /* write a uint16_t to a buffer in target memory endianness */
282 void target_buffer_set_u16(struct target *target, uint8_t *buffer, uint16_t value)
284 if (target->endianness == TARGET_LITTLE_ENDIAN)
285 h_u16_to_le(buffer, value);
287 h_u16_to_be(buffer, value);
290 /* write a uint8_t to a buffer in target memory endianness */
291 void target_buffer_set_u8(struct target *target, uint8_t *buffer, uint8_t value)
296 /* return a pointer to a configured target; id is name or number */
297 struct target *get_target(const char *id)
299 struct target *target;
301 /* try as tcltarget name */
302 for (target = all_targets; target; target = target->next) {
303 if (target->cmd_name == NULL)
305 if (strcmp(id, target->cmd_name) == 0)
309 /* It's OK to remove this fallback sometime after August 2010 or so */
311 /* no match, try as number */
313 if (parse_uint(id, &num) != ERROR_OK)
316 for (target = all_targets; target; target = target->next) {
317 if (target->target_number == (int)num) {
318 LOG_WARNING("use '%s' as target identifier, not '%u'",
319 target->cmd_name, num);
327 /* returns a pointer to the n-th configured target */
328 static struct target *get_target_by_num(int num)
330 struct target *target = all_targets;
333 if (target->target_number == num) {
336 target = target->next;
342 struct target* get_current_target(struct command_context *cmd_ctx)
344 struct target *target = get_target_by_num(cmd_ctx->current_target);
348 LOG_ERROR("BUG: current_target out of bounds");
355 int target_poll(struct target *target)
359 /* We can't poll until after examine */
360 if (!target_was_examined(target))
362 /* Fail silently lest we pollute the log */
366 retval = target->type->poll(target);
367 if (retval != ERROR_OK)
370 if (target->halt_issued)
372 if (target->state == TARGET_HALTED)
374 target->halt_issued = false;
377 long long t = timeval_ms() - target->halt_issued_time;
380 target->halt_issued = false;
381 LOG_INFO("Halt timed out, wake up GDB.");
382 target_call_event_callbacks(target, TARGET_EVENT_GDB_HALT);
390 int target_halt(struct target *target)
393 /* We can't poll until after examine */
394 if (!target_was_examined(target))
396 LOG_ERROR("Target not examined yet");
400 retval = target->type->halt(target);
401 if (retval != ERROR_OK)
404 target->halt_issued = true;
405 target->halt_issued_time = timeval_ms();
410 int target_resume(struct target *target, int current, uint32_t address, int handle_breakpoints, int debug_execution)
414 /* We can't poll until after examine */
415 if (!target_was_examined(target))
417 LOG_ERROR("Target not examined yet");
421 /* note that resume *must* be asynchronous. The CPU can halt before we poll. The CPU can
422 * even halt at the current PC as a result of a software breakpoint being inserted by (a bug?)
425 if ((retval = target->type->resume(target, current, address, handle_breakpoints, debug_execution)) != ERROR_OK)
431 int target_process_reset(struct command_context *cmd_ctx, enum target_reset_mode reset_mode)
436 n = Jim_Nvp_value2name_simple(nvp_reset_modes, reset_mode);
437 if (n->name == NULL) {
438 LOG_ERROR("invalid reset mode");
442 /* disable polling during reset to make reset event scripts
443 * more predictable, i.e. dr/irscan & pathmove in events will
444 * not have JTAG operations injected into the middle of a sequence.
446 bool save_poll = jtag_poll_get_enabled();
448 jtag_poll_set_enabled(false);
450 sprintf(buf, "ocd_process_reset %s", n->name);
451 retval = Jim_Eval(interp, buf);
453 jtag_poll_set_enabled(save_poll);
455 if (retval != JIM_OK) {
456 Jim_PrintErrorMessage(interp);
460 /* We want any events to be processed before the prompt */
461 retval = target_call_timer_callbacks_now();
466 static int identity_virt2phys(struct target *target,
467 uint32_t virtual, uint32_t *physical)
473 static int no_mmu(struct target *target, int *enabled)
479 static int default_examine(struct target *target)
481 target_set_examined(target);
485 int target_examine_one(struct target *target)
487 return target->type->examine(target);
490 static int jtag_enable_callback(enum jtag_event event, void *priv)
492 struct target *target = priv;
494 if (event != JTAG_TAP_EVENT_ENABLE || !target->tap->enabled)
497 jtag_unregister_event_callback(jtag_enable_callback, target);
498 return target_examine_one(target);
502 /* Targets that correctly implement init + examine, i.e.
503 * no communication with target during init:
507 int target_examine(void)
509 int retval = ERROR_OK;
510 struct target *target;
512 for (target = all_targets; target; target = target->next)
514 /* defer examination, but don't skip it */
515 if (!target->tap->enabled) {
516 jtag_register_event_callback(jtag_enable_callback,
520 if ((retval = target_examine_one(target)) != ERROR_OK)
525 const char *target_type_name(struct target *target)
527 return target->type->name;
530 static int target_write_memory_imp(struct target *target, uint32_t address, uint32_t size, uint32_t count, uint8_t *buffer)
532 if (!target_was_examined(target))
534 LOG_ERROR("Target not examined yet");
537 return target->type->write_memory_imp(target, address, size, count, buffer);
540 static int target_read_memory_imp(struct target *target, uint32_t address, uint32_t size, uint32_t count, uint8_t *buffer)
542 if (!target_was_examined(target))
544 LOG_ERROR("Target not examined yet");
547 return target->type->read_memory_imp(target, address, size, count, buffer);
550 static int target_soft_reset_halt_imp(struct target *target)
552 if (!target_was_examined(target))
554 LOG_ERROR("Target not examined yet");
557 if (!target->type->soft_reset_halt_imp) {
558 LOG_ERROR("Target %s does not support soft_reset_halt",
562 return target->type->soft_reset_halt_imp(target);
565 static int target_run_algorithm_imp(struct target *target, int num_mem_params, struct mem_param *mem_params, int num_reg_params, struct reg_param *reg_param, uint32_t entry_point, uint32_t exit_point, int timeout_ms, void *arch_info)
567 if (!target_was_examined(target))
569 LOG_ERROR("Target not examined yet");
572 return target->type->run_algorithm_imp(target, num_mem_params, mem_params, num_reg_params, reg_param, entry_point, exit_point, timeout_ms, arch_info);
575 int target_read_memory(struct target *target,
576 uint32_t address, uint32_t size, uint32_t count, uint8_t *buffer)
578 return target->type->read_memory(target, address, size, count, buffer);
581 int target_read_phys_memory(struct target *target,
582 uint32_t address, uint32_t size, uint32_t count, uint8_t *buffer)
584 return target->type->read_phys_memory(target, address, size, count, buffer);
587 int target_write_memory(struct target *target,
588 uint32_t address, uint32_t size, uint32_t count, uint8_t *buffer)
590 return target->type->write_memory(target, address, size, count, buffer);
593 int target_write_phys_memory(struct target *target,
594 uint32_t address, uint32_t size, uint32_t count, uint8_t *buffer)
596 return target->type->write_phys_memory(target, address, size, count, buffer);
599 int target_bulk_write_memory(struct target *target,
600 uint32_t address, uint32_t count, uint8_t *buffer)
602 return target->type->bulk_write_memory(target, address, count, buffer);
605 int target_add_breakpoint(struct target *target,
606 struct breakpoint *breakpoint)
608 return target->type->add_breakpoint(target, breakpoint);
610 int target_remove_breakpoint(struct target *target,
611 struct breakpoint *breakpoint)
613 return target->type->remove_breakpoint(target, breakpoint);
616 int target_add_watchpoint(struct target *target,
617 struct watchpoint *watchpoint)
619 return target->type->add_watchpoint(target, watchpoint);
621 int target_remove_watchpoint(struct target *target,
622 struct watchpoint *watchpoint)
624 return target->type->remove_watchpoint(target, watchpoint);
627 int target_get_gdb_reg_list(struct target *target,
628 struct reg **reg_list[], int *reg_list_size)
630 return target->type->get_gdb_reg_list(target, reg_list, reg_list_size);
632 int target_step(struct target *target,
633 int current, uint32_t address, int handle_breakpoints)
635 return target->type->step(target, current, address, handle_breakpoints);
639 int target_run_algorithm(struct target *target,
640 int num_mem_params, struct mem_param *mem_params,
641 int num_reg_params, struct reg_param *reg_param,
642 uint32_t entry_point, uint32_t exit_point,
643 int timeout_ms, void *arch_info)
645 return target->type->run_algorithm(target,
646 num_mem_params, mem_params, num_reg_params, reg_param,
647 entry_point, exit_point, timeout_ms, arch_info);
651 * Reset the @c examined flag for the given target.
652 * Pure paranoia -- targets are zeroed on allocation.
654 static void target_reset_examined(struct target *target)
656 target->examined = false;
661 static int default_mrc(struct target *target, int cpnum, uint32_t op1, uint32_t op2, uint32_t CRn, uint32_t CRm, uint32_t *value)
663 LOG_ERROR("Not implemented: %s", __func__);
667 static int default_mcr(struct target *target, int cpnum, uint32_t op1, uint32_t op2, uint32_t CRn, uint32_t CRm, uint32_t value)
669 LOG_ERROR("Not implemented: %s", __func__);
673 static int arm_cp_check(struct target *target, int cpnum, uint32_t op1, uint32_t op2, uint32_t CRn, uint32_t CRm)
676 if (!target_was_examined(target))
678 LOG_ERROR("Target not examined yet");
682 if ((cpnum <0) || (cpnum > 15))
684 LOG_ERROR("Illegal co-processor %d", cpnum);
690 LOG_ERROR("Illegal op1");
696 LOG_ERROR("Illegal op2");
702 LOG_ERROR("Illegal CRn");
708 LOG_ERROR("Illegal CRm");
715 int target_mrc(struct target *target, int cpnum, uint32_t op1, uint32_t op2, uint32_t CRn, uint32_t CRm, uint32_t *value)
719 retval = arm_cp_check(target, cpnum, op1, op2, CRn, CRm);
720 if (retval != ERROR_OK)
723 return target->type->mrc(target, cpnum, op1, op2, CRn, CRm, value);
726 int target_mcr(struct target *target, int cpnum, uint32_t op1, uint32_t op2, uint32_t CRn, uint32_t CRm, uint32_t value)
730 retval = arm_cp_check(target, cpnum, op1, op2, CRn, CRm);
731 if (retval != ERROR_OK)
734 return target->type->mcr(target, cpnum, op1, op2, CRn, CRm, value);
738 err_read_phys_memory(struct target *target, uint32_t address,
739 uint32_t size, uint32_t count, uint8_t *buffer)
741 LOG_ERROR("Not implemented: %s", __func__);
746 err_write_phys_memory(struct target *target, uint32_t address,
747 uint32_t size, uint32_t count, uint8_t *buffer)
749 LOG_ERROR("Not implemented: %s", __func__);
753 int target_init(struct command_context *cmd_ctx)
755 struct target *target;
758 for (target = all_targets; target; target = target->next) {
759 struct target_type *type = target->type;
761 target_reset_examined(target);
762 if (target->type->examine == NULL)
764 target->type->examine = default_examine;
767 if ((retval = target->type->init_target(cmd_ctx, target)) != ERROR_OK)
769 LOG_ERROR("target '%s' init failed", target_type_name(target));
774 * @todo MCR/MRC are ARM-specific; don't require them in
775 * all targets, or for ARMs without coprocessors.
777 if (target->type->mcr == NULL)
779 target->type->mcr = default_mcr;
782 const struct command_registration mcr_cmd = {
784 .mode = COMMAND_EXEC,
785 .jim_handler = &jim_mcrmrc,
786 .help = "write coprocessor",
787 .usage = "<cpnum> <op1> <op2> <CRn> <CRm> <value>",
789 register_command(cmd_ctx, NULL, &mcr_cmd);
792 if (target->type->mrc == NULL)
794 target->type->mrc = default_mrc;
797 const struct command_registration mrc_cmd = {
799 .jim_handler = &jim_mcrmrc,
800 .help = "read coprocessor",
801 .usage = "<cpnum> <op1> <op2> <CRn> <CRm>",
803 register_command(cmd_ctx, NULL, &mrc_cmd);
808 * @todo get rid of those *memory_imp() methods, now that all
809 * callers are using target_*_memory() accessors ... and make
810 * sure the "physical" paths handle the same issues.
813 /* a non-invasive way(in terms of patches) to add some code that
814 * runs before the type->write/read_memory implementation
816 target->type->write_memory_imp = target->type->write_memory;
817 target->type->write_memory = target_write_memory_imp;
818 target->type->read_memory_imp = target->type->read_memory;
819 target->type->read_memory = target_read_memory_imp;
820 target->type->soft_reset_halt_imp = target->type->soft_reset_halt;
821 target->type->soft_reset_halt = target_soft_reset_halt_imp;
822 target->type->run_algorithm_imp = target->type->run_algorithm;
823 target->type->run_algorithm = target_run_algorithm_imp;
825 /* Sanity-check MMU support ... stub in what we must, to help
826 * implement it in stages, but warn if we need to do so.
829 if (type->write_phys_memory == NULL) {
830 LOG_ERROR("type '%s' is missing %s",
832 "write_phys_memory");
833 type->write_phys_memory = err_write_phys_memory;
835 if (type->read_phys_memory == NULL) {
836 LOG_ERROR("type '%s' is missing %s",
839 type->read_phys_memory = err_read_phys_memory;
841 if (type->virt2phys == NULL) {
842 LOG_ERROR("type '%s' is missing %s",
845 type->virt2phys = identity_virt2phys;
848 /* Make sure no-MMU targets all behave the same: make no
849 * distinction between physical and virtual addresses, and
850 * ensure that virt2phys() is always an identity mapping.
853 if (type->write_phys_memory
854 || type->read_phys_memory
856 LOG_WARNING("type '%s' has broken MMU hooks",
860 type->write_phys_memory = type->write_memory;
861 type->read_phys_memory = type->read_memory;
862 type->virt2phys = identity_virt2phys;
868 if ((retval = target_register_user_commands(cmd_ctx)) != ERROR_OK)
870 if ((retval = target_register_timer_callback(handle_target, 100, 1, NULL)) != ERROR_OK)
877 int target_register_event_callback(int (*callback)(struct target *target, enum target_event event, void *priv), void *priv)
879 struct target_event_callback **callbacks_p = &target_event_callbacks;
881 if (callback == NULL)
883 return ERROR_INVALID_ARGUMENTS;
888 while ((*callbacks_p)->next)
889 callbacks_p = &((*callbacks_p)->next);
890 callbacks_p = &((*callbacks_p)->next);
893 (*callbacks_p) = malloc(sizeof(struct target_event_callback));
894 (*callbacks_p)->callback = callback;
895 (*callbacks_p)->priv = priv;
896 (*callbacks_p)->next = NULL;
901 int target_register_timer_callback(int (*callback)(void *priv), int time_ms, int periodic, void *priv)
903 struct target_timer_callback **callbacks_p = &target_timer_callbacks;
906 if (callback == NULL)
908 return ERROR_INVALID_ARGUMENTS;
913 while ((*callbacks_p)->next)
914 callbacks_p = &((*callbacks_p)->next);
915 callbacks_p = &((*callbacks_p)->next);
918 (*callbacks_p) = malloc(sizeof(struct target_timer_callback));
919 (*callbacks_p)->callback = callback;
920 (*callbacks_p)->periodic = periodic;
921 (*callbacks_p)->time_ms = time_ms;
923 gettimeofday(&now, NULL);
924 (*callbacks_p)->when.tv_usec = now.tv_usec + (time_ms % 1000) * 1000;
925 time_ms -= (time_ms % 1000);
926 (*callbacks_p)->when.tv_sec = now.tv_sec + (time_ms / 1000);
927 if ((*callbacks_p)->when.tv_usec > 1000000)
929 (*callbacks_p)->when.tv_usec = (*callbacks_p)->when.tv_usec - 1000000;
930 (*callbacks_p)->when.tv_sec += 1;
933 (*callbacks_p)->priv = priv;
934 (*callbacks_p)->next = NULL;
939 int target_unregister_event_callback(int (*callback)(struct target *target, enum target_event event, void *priv), void *priv)
941 struct target_event_callback **p = &target_event_callbacks;
942 struct target_event_callback *c = target_event_callbacks;
944 if (callback == NULL)
946 return ERROR_INVALID_ARGUMENTS;
951 struct target_event_callback *next = c->next;
952 if ((c->callback == callback) && (c->priv == priv))
966 int target_unregister_timer_callback(int (*callback)(void *priv), void *priv)
968 struct target_timer_callback **p = &target_timer_callbacks;
969 struct target_timer_callback *c = target_timer_callbacks;
971 if (callback == NULL)
973 return ERROR_INVALID_ARGUMENTS;
978 struct target_timer_callback *next = c->next;
979 if ((c->callback == callback) && (c->priv == priv))
993 int target_call_event_callbacks(struct target *target, enum target_event event)
995 struct target_event_callback *callback = target_event_callbacks;
996 struct target_event_callback *next_callback;
998 if (event == TARGET_EVENT_HALTED)
1000 /* execute early halted first */
1001 target_call_event_callbacks(target, TARGET_EVENT_GDB_HALT);
1004 LOG_DEBUG("target event %i (%s)",
1006 Jim_Nvp_value2name_simple(nvp_target_event, event)->name);
1008 target_handle_event(target, event);
1012 next_callback = callback->next;
1013 callback->callback(target, event, callback->priv);
1014 callback = next_callback;
1020 static int target_timer_callback_periodic_restart(
1021 struct target_timer_callback *cb, struct timeval *now)
1023 int time_ms = cb->time_ms;
1024 cb->when.tv_usec = now->tv_usec + (time_ms % 1000) * 1000;
1025 time_ms -= (time_ms % 1000);
1026 cb->when.tv_sec = now->tv_sec + time_ms / 1000;
1027 if (cb->when.tv_usec > 1000000)
1029 cb->when.tv_usec = cb->when.tv_usec - 1000000;
1030 cb->when.tv_sec += 1;
1035 static int target_call_timer_callback(struct target_timer_callback *cb,
1036 struct timeval *now)
1038 cb->callback(cb->priv);
1041 return target_timer_callback_periodic_restart(cb, now);
1043 return target_unregister_timer_callback(cb->callback, cb->priv);
1046 static int target_call_timer_callbacks_check_time(int checktime)
1051 gettimeofday(&now, NULL);
1053 struct target_timer_callback *callback = target_timer_callbacks;
1056 // cleaning up may unregister and free this callback
1057 struct target_timer_callback *next_callback = callback->next;
1059 bool call_it = callback->callback &&
1060 ((!checktime && callback->periodic) ||
1061 now.tv_sec > callback->when.tv_sec ||
1062 (now.tv_sec == callback->when.tv_sec &&
1063 now.tv_usec >= callback->when.tv_usec));
1067 int retval = target_call_timer_callback(callback, &now);
1068 if (retval != ERROR_OK)
1072 callback = next_callback;
1078 int target_call_timer_callbacks(void)
1080 return target_call_timer_callbacks_check_time(1);
1083 /* invoke periodic callbacks immediately */
1084 int target_call_timer_callbacks_now(void)
1086 return target_call_timer_callbacks_check_time(0);
1089 int target_alloc_working_area(struct target *target, uint32_t size, struct working_area **area)
1091 struct working_area *c = target->working_areas;
1092 struct working_area *new_wa = NULL;
1094 /* Reevaluate working area address based on MMU state*/
1095 if (target->working_areas == NULL)
1100 retval = target->type->mmu(target, &enabled);
1101 if (retval != ERROR_OK)
1107 if (target->working_area_phys_spec) {
1108 LOG_DEBUG("MMU disabled, using physical "
1109 "address for working memory 0x%08x",
1110 (unsigned)target->working_area_phys);
1111 target->working_area = target->working_area_phys;
1113 LOG_ERROR("No working memory available. "
1114 "Specify -work-area-phys to target.");
1115 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
1118 if (target->working_area_virt_spec) {
1119 LOG_DEBUG("MMU enabled, using virtual "
1120 "address for working memory 0x%08x",
1121 (unsigned)target->working_area_virt);
1122 target->working_area = target->working_area_virt;
1124 LOG_ERROR("No working memory available. "
1125 "Specify -work-area-virt to target.");
1126 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
1131 /* only allocate multiples of 4 byte */
1134 LOG_ERROR("BUG: code tried to allocate unaligned number of bytes (0x%08x), padding", ((unsigned)(size)));
1135 size = (size + 3) & (~3);
1138 /* see if there's already a matching working area */
1141 if ((c->free) && (c->size == size))
1149 /* if not, allocate a new one */
1152 struct working_area **p = &target->working_areas;
1153 uint32_t first_free = target->working_area;
1154 uint32_t free_size = target->working_area_size;
1156 c = target->working_areas;
1159 first_free += c->size;
1160 free_size -= c->size;
1165 if (free_size < size)
1167 LOG_WARNING("not enough working area available(requested %u, free %u)",
1168 (unsigned)(size), (unsigned)(free_size));
1169 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
1172 LOG_DEBUG("allocated new working area at address 0x%08x", (unsigned)first_free);
1174 new_wa = malloc(sizeof(struct working_area));
1175 new_wa->next = NULL;
1176 new_wa->size = size;
1177 new_wa->address = first_free;
1179 if (target->backup_working_area)
1182 new_wa->backup = malloc(new_wa->size);
1183 if ((retval = target_read_memory(target, new_wa->address, 4, new_wa->size / 4, new_wa->backup)) != ERROR_OK)
1185 free(new_wa->backup);
1192 new_wa->backup = NULL;
1195 /* put new entry in list */
1199 /* mark as used, and return the new (reused) area */
1204 new_wa->user = area;
1209 int target_free_working_area_restore(struct target *target, struct working_area *area, int restore)
1214 if (restore && target->backup_working_area)
1217 if ((retval = target_write_memory(target, area->address, 4, area->size / 4, area->backup)) != ERROR_OK)
1223 /* mark user pointer invalid */
1230 int target_free_working_area(struct target *target, struct working_area *area)
1232 return target_free_working_area_restore(target, area, 1);
1235 /* free resources and restore memory, if restoring memory fails,
1236 * free up resources anyway
1238 void target_free_all_working_areas_restore(struct target *target, int restore)
1240 struct working_area *c = target->working_areas;
1244 struct working_area *next = c->next;
1245 target_free_working_area_restore(target, c, restore);
1255 target->working_areas = NULL;
1258 void target_free_all_working_areas(struct target *target)
1260 target_free_all_working_areas_restore(target, 1);
1263 int target_arch_state(struct target *target)
1268 LOG_USER("No target has been configured");
1272 LOG_USER("target state: %s", target_state_name( target ));
1274 if (target->state != TARGET_HALTED)
1277 retval = target->type->arch_state(target);
1281 /* Single aligned words are guaranteed to use 16 or 32 bit access
1282 * mode respectively, otherwise data is handled as quickly as
1285 int target_write_buffer(struct target *target, uint32_t address, uint32_t size, uint8_t *buffer)
1288 LOG_DEBUG("writing buffer of %i byte at 0x%8.8x",
1289 (int)size, (unsigned)address);
1291 if (!target_was_examined(target))
1293 LOG_ERROR("Target not examined yet");
1301 if ((address + size - 1) < address)
1303 /* GDB can request this when e.g. PC is 0xfffffffc*/
1304 LOG_ERROR("address + size wrapped(0x%08x, 0x%08x)",
1310 if (((address % 2) == 0) && (size == 2))
1312 return target_write_memory(target, address, 2, 1, buffer);
1315 /* handle unaligned head bytes */
1318 uint32_t unaligned = 4 - (address % 4);
1320 if (unaligned > size)
1323 if ((retval = target_write_memory(target, address, 1, unaligned, buffer)) != ERROR_OK)
1326 buffer += unaligned;
1327 address += unaligned;
1331 /* handle aligned words */
1334 int aligned = size - (size % 4);
1336 /* use bulk writes above a certain limit. This may have to be changed */
1339 if ((retval = target->type->bulk_write_memory(target, address, aligned / 4, buffer)) != ERROR_OK)
1344 if ((retval = target_write_memory(target, address, 4, aligned / 4, buffer)) != ERROR_OK)
1353 /* handle tail writes of less than 4 bytes */
1356 if ((retval = target_write_memory(target, address, 1, size, buffer)) != ERROR_OK)
1363 /* Single aligned words are guaranteed to use 16 or 32 bit access
1364 * mode respectively, otherwise data is handled as quickly as
1367 int target_read_buffer(struct target *target, uint32_t address, uint32_t size, uint8_t *buffer)
1370 LOG_DEBUG("reading buffer of %i byte at 0x%8.8x",
1371 (int)size, (unsigned)address);
1373 if (!target_was_examined(target))
1375 LOG_ERROR("Target not examined yet");
1383 if ((address + size - 1) < address)
1385 /* GDB can request this when e.g. PC is 0xfffffffc*/
1386 LOG_ERROR("address + size wrapped(0x%08" PRIx32 ", 0x%08" PRIx32 ")",
1392 if (((address % 2) == 0) && (size == 2))
1394 return target_read_memory(target, address, 2, 1, buffer);
1397 /* handle unaligned head bytes */
1400 uint32_t unaligned = 4 - (address % 4);
1402 if (unaligned > size)
1405 if ((retval = target_read_memory(target, address, 1, unaligned, buffer)) != ERROR_OK)
1408 buffer += unaligned;
1409 address += unaligned;
1413 /* handle aligned words */
1416 int aligned = size - (size % 4);
1418 if ((retval = target_read_memory(target, address, 4, aligned / 4, buffer)) != ERROR_OK)
1426 /*prevent byte access when possible (avoid AHB access limitations in some cases)*/
1429 int aligned = size - (size%2);
1430 retval = target_read_memory(target, address, 2, aligned / 2, buffer);
1431 if (retval != ERROR_OK)
1438 /* handle tail writes of less than 4 bytes */
1441 if ((retval = target_read_memory(target, address, 1, size, buffer)) != ERROR_OK)
1448 int target_checksum_memory(struct target *target, uint32_t address, uint32_t size, uint32_t* crc)
1453 uint32_t checksum = 0;
1454 if (!target_was_examined(target))
1456 LOG_ERROR("Target not examined yet");
1460 if ((retval = target->type->checksum_memory(target, address,
1461 size, &checksum)) != ERROR_OK)
1463 buffer = malloc(size);
1466 LOG_ERROR("error allocating buffer for section (%d bytes)", (int)size);
1467 return ERROR_INVALID_ARGUMENTS;
1469 retval = target_read_buffer(target, address, size, buffer);
1470 if (retval != ERROR_OK)
1476 /* convert to target endianess */
1477 for (i = 0; i < (size/sizeof(uint32_t)); i++)
1479 uint32_t target_data;
1480 target_data = target_buffer_get_u32(target, &buffer[i*sizeof(uint32_t)]);
1481 target_buffer_set_u32(target, &buffer[i*sizeof(uint32_t)], target_data);
1484 retval = image_calculate_checksum(buffer, size, &checksum);
1493 int target_blank_check_memory(struct target *target, uint32_t address, uint32_t size, uint32_t* blank)
1496 if (!target_was_examined(target))
1498 LOG_ERROR("Target not examined yet");
1502 if (target->type->blank_check_memory == 0)
1503 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
1505 retval = target->type->blank_check_memory(target, address, size, blank);
1510 int target_read_u32(struct target *target, uint32_t address, uint32_t *value)
1512 uint8_t value_buf[4];
1513 if (!target_was_examined(target))
1515 LOG_ERROR("Target not examined yet");
1519 int retval = target_read_memory(target, address, 4, 1, value_buf);
1521 if (retval == ERROR_OK)
1523 *value = target_buffer_get_u32(target, value_buf);
1524 LOG_DEBUG("address: 0x%8.8" PRIx32 ", value: 0x%8.8" PRIx32 "",
1531 LOG_DEBUG("address: 0x%8.8" PRIx32 " failed",
1538 int target_read_u16(struct target *target, uint32_t address, uint16_t *value)
1540 uint8_t value_buf[2];
1541 if (!target_was_examined(target))
1543 LOG_ERROR("Target not examined yet");
1547 int retval = target_read_memory(target, address, 2, 1, value_buf);
1549 if (retval == ERROR_OK)
1551 *value = target_buffer_get_u16(target, value_buf);
1552 LOG_DEBUG("address: 0x%8.8" PRIx32 ", value: 0x%4.4x",
1559 LOG_DEBUG("address: 0x%8.8" PRIx32 " failed",
1566 int target_read_u8(struct target *target, uint32_t address, uint8_t *value)
1568 int retval = target_read_memory(target, address, 1, 1, value);
1569 if (!target_was_examined(target))
1571 LOG_ERROR("Target not examined yet");
1575 if (retval == ERROR_OK)
1577 LOG_DEBUG("address: 0x%8.8" PRIx32 ", value: 0x%2.2x",
1584 LOG_DEBUG("address: 0x%8.8" PRIx32 " failed",
1591 int target_write_u32(struct target *target, uint32_t address, uint32_t value)
1594 uint8_t value_buf[4];
1595 if (!target_was_examined(target))
1597 LOG_ERROR("Target not examined yet");
1601 LOG_DEBUG("address: 0x%8.8" PRIx32 ", value: 0x%8.8" PRIx32 "",
1605 target_buffer_set_u32(target, value_buf, value);
1606 if ((retval = target_write_memory(target, address, 4, 1, value_buf)) != ERROR_OK)
1608 LOG_DEBUG("failed: %i", retval);
1614 int target_write_u16(struct target *target, uint32_t address, uint16_t value)
1617 uint8_t value_buf[2];
1618 if (!target_was_examined(target))
1620 LOG_ERROR("Target not examined yet");
1624 LOG_DEBUG("address: 0x%8.8" PRIx32 ", value: 0x%8.8x",
1628 target_buffer_set_u16(target, value_buf, value);
1629 if ((retval = target_write_memory(target, address, 2, 1, value_buf)) != ERROR_OK)
1631 LOG_DEBUG("failed: %i", retval);
1637 int target_write_u8(struct target *target, uint32_t address, uint8_t value)
1640 if (!target_was_examined(target))
1642 LOG_ERROR("Target not examined yet");
1646 LOG_DEBUG("address: 0x%8.8" PRIx32 ", value: 0x%2.2x",
1649 if ((retval = target_write_memory(target, address, 1, 1, &value)) != ERROR_OK)
1651 LOG_DEBUG("failed: %i", retval);
1657 COMMAND_HANDLER(handle_targets_command)
1659 struct target *target = all_targets;
1663 target = get_target(CMD_ARGV[0]);
1664 if (target == NULL) {
1665 command_print(CMD_CTX,"Target: %s is unknown, try one of:\n", CMD_ARGV[0]);
1668 if (!target->tap->enabled) {
1669 command_print(CMD_CTX,"Target: TAP %s is disabled, "
1670 "can't be the current target\n",
1671 target->tap->dotted_name);
1675 CMD_CTX->current_target = target->target_number;
1680 target = all_targets;
1681 command_print(CMD_CTX, " TargetName Type Endian TapName State ");
1682 command_print(CMD_CTX, "-- ------------------ ---------- ------ ------------------ ------------");
1688 if (target->tap->enabled)
1689 state = target_state_name( target );
1691 state = "tap-disabled";
1693 if (CMD_CTX->current_target == target->target_number)
1696 /* keep columns lined up to match the headers above */
1697 command_print(CMD_CTX, "%2d%c %-18s %-10s %-6s %-18s %s",
1698 target->target_number,
1701 target_type_name(target),
1702 Jim_Nvp_value2name_simple(nvp_target_endian,
1703 target->endianness)->name,
1704 target->tap->dotted_name,
1706 target = target->next;
1712 /* every 300ms we check for reset & powerdropout and issue a "reset halt" if so. */
1714 static int powerDropout;
1715 static int srstAsserted;
1717 static int runPowerRestore;
1718 static int runPowerDropout;
1719 static int runSrstAsserted;
1720 static int runSrstDeasserted;
1722 static int sense_handler(void)
1724 static int prevSrstAsserted = 0;
1725 static int prevPowerdropout = 0;
1728 if ((retval = jtag_power_dropout(&powerDropout)) != ERROR_OK)
1732 powerRestored = prevPowerdropout && !powerDropout;
1735 runPowerRestore = 1;
1738 long long current = timeval_ms();
1739 static long long lastPower = 0;
1740 int waitMore = lastPower + 2000 > current;
1741 if (powerDropout && !waitMore)
1743 runPowerDropout = 1;
1744 lastPower = current;
1747 if ((retval = jtag_srst_asserted(&srstAsserted)) != ERROR_OK)
1751 srstDeasserted = prevSrstAsserted && !srstAsserted;
1753 static long long lastSrst = 0;
1754 waitMore = lastSrst + 2000 > current;
1755 if (srstDeasserted && !waitMore)
1757 runSrstDeasserted = 1;
1761 if (!prevSrstAsserted && srstAsserted)
1763 runSrstAsserted = 1;
1766 prevSrstAsserted = srstAsserted;
1767 prevPowerdropout = powerDropout;
1769 if (srstDeasserted || powerRestored)
1771 /* Other than logging the event we can't do anything here.
1772 * Issuing a reset is a particularly bad idea as we might
1773 * be inside a reset already.
1780 static void target_call_event_callbacks_all(enum target_event e) {
1781 struct target *target;
1782 target = all_targets;
1784 target_call_event_callbacks(target, e);
1785 target = target->next;
1789 /* process target state changes */
1790 int handle_target(void *priv)
1792 int retval = ERROR_OK;
1794 /* we do not want to recurse here... */
1795 static int recursive = 0;
1800 /* danger! running these procedures can trigger srst assertions and power dropouts.
1801 * We need to avoid an infinite loop/recursion here and we do that by
1802 * clearing the flags after running these events.
1804 int did_something = 0;
1805 if (runSrstAsserted)
1807 target_call_event_callbacks_all(TARGET_EVENT_GDB_HALT);
1808 Jim_Eval(interp, "srst_asserted");
1811 if (runSrstDeasserted)
1813 Jim_Eval(interp, "srst_deasserted");
1816 if (runPowerDropout)
1818 target_call_event_callbacks_all(TARGET_EVENT_GDB_HALT);
1819 Jim_Eval(interp, "power_dropout");
1822 if (runPowerRestore)
1824 Jim_Eval(interp, "power_restore");
1830 /* clear detect flags */
1834 /* clear action flags */
1836 runSrstAsserted = 0;
1837 runSrstDeasserted = 0;
1838 runPowerRestore = 0;
1839 runPowerDropout = 0;
1844 /* Poll targets for state changes unless that's globally disabled.
1845 * Skip targets that are currently disabled.
1847 for (struct target *target = all_targets;
1848 is_jtag_poll_safe() && target;
1849 target = target->next)
1851 if (!target->tap->enabled)
1854 /* only poll target if we've got power and srst isn't asserted */
1855 if (!powerDropout && !srstAsserted)
1857 /* polling may fail silently until the target has been examined */
1858 if ((retval = target_poll(target)) != ERROR_OK)
1860 target_call_event_callbacks(target, TARGET_EVENT_GDB_HALT);
1869 COMMAND_HANDLER(handle_reg_command)
1871 struct target *target;
1872 struct reg *reg = NULL;
1878 target = get_current_target(CMD_CTX);
1880 /* list all available registers for the current target */
1883 struct reg_cache *cache = target->reg_cache;
1890 command_print(CMD_CTX, "===== %s", cache->name);
1892 for (i = 0, reg = cache->reg_list;
1893 i < cache->num_regs;
1894 i++, reg++, count++)
1896 /* only print cached values if they are valid */
1898 value = buf_to_str(reg->value,
1900 command_print(CMD_CTX,
1901 "(%i) %s (/%" PRIu32 "): 0x%s%s",
1909 command_print(CMD_CTX, "(%i) %s (/%" PRIu32 ")",
1914 cache = cache->next;
1920 /* access a single register by its ordinal number */
1921 if ((CMD_ARGV[0][0] >= '0') && (CMD_ARGV[0][0] <= '9'))
1924 COMMAND_PARSE_NUMBER(uint, CMD_ARGV[0], num);
1926 struct reg_cache *cache = target->reg_cache;
1931 for (i = 0; i < cache->num_regs; i++)
1935 reg = &cache->reg_list[i];
1941 cache = cache->next;
1946 command_print(CMD_CTX, "%i is out of bounds, the current target has only %i registers (0 - %i)", num, count, count - 1);
1949 } else /* access a single register by its name */
1951 reg = register_get_by_name(target->reg_cache, CMD_ARGV[0], 1);
1955 command_print(CMD_CTX, "register %s not found in current target", CMD_ARGV[0]);
1960 /* display a register */
1961 if ((CMD_ARGC == 1) || ((CMD_ARGC == 2) && !((CMD_ARGV[1][0] >= '0') && (CMD_ARGV[1][0] <= '9'))))
1963 if ((CMD_ARGC == 2) && (strcmp(CMD_ARGV[1], "force") == 0))
1966 if (reg->valid == 0)
1968 reg->type->get(reg);
1970 value = buf_to_str(reg->value, reg->size, 16);
1971 command_print(CMD_CTX, "%s (/%i): 0x%s", reg->name, (int)(reg->size), value);
1976 /* set register value */
1979 uint8_t *buf = malloc(DIV_ROUND_UP(reg->size, 8));
1980 str_to_buf(CMD_ARGV[1], strlen(CMD_ARGV[1]), buf, reg->size, 0);
1982 reg->type->set(reg, buf);
1984 value = buf_to_str(reg->value, reg->size, 16);
1985 command_print(CMD_CTX, "%s (/%i): 0x%s", reg->name, (int)(reg->size), value);
1993 command_print(CMD_CTX, "usage: reg <#|name> [value]");
1998 COMMAND_HANDLER(handle_poll_command)
2000 int retval = ERROR_OK;
2001 struct target *target = get_current_target(CMD_CTX);
2005 command_print(CMD_CTX, "background polling: %s",
2006 jtag_poll_get_enabled() ? "on" : "off");
2007 command_print(CMD_CTX, "TAP: %s (%s)",
2008 target->tap->dotted_name,
2009 target->tap->enabled ? "enabled" : "disabled");
2010 if (!target->tap->enabled)
2012 if ((retval = target_poll(target)) != ERROR_OK)
2014 if ((retval = target_arch_state(target)) != ERROR_OK)
2017 else if (CMD_ARGC == 1)
2020 COMMAND_PARSE_ON_OFF(CMD_ARGV[0], enable);
2021 jtag_poll_set_enabled(enable);
2025 return ERROR_COMMAND_SYNTAX_ERROR;
2031 COMMAND_HANDLER(handle_wait_halt_command)
2034 return ERROR_COMMAND_SYNTAX_ERROR;
2039 int retval = parse_uint(CMD_ARGV[0], &ms);
2040 if (ERROR_OK != retval)
2042 command_print(CMD_CTX, "usage: %s [seconds]", CMD_NAME);
2043 return ERROR_COMMAND_SYNTAX_ERROR;
2045 // convert seconds (given) to milliseconds (needed)
2049 struct target *target = get_current_target(CMD_CTX);
2050 return target_wait_state(target, TARGET_HALTED, ms);
2053 /* wait for target state to change. The trick here is to have a low
2054 * latency for short waits and not to suck up all the CPU time
2057 * After 500ms, keep_alive() is invoked
2059 int target_wait_state(struct target *target, enum target_state state, int ms)
2062 long long then = 0, cur;
2067 if ((retval = target_poll(target)) != ERROR_OK)
2069 if (target->state == state)
2077 then = timeval_ms();
2078 LOG_DEBUG("waiting for target %s...",
2079 Jim_Nvp_value2name_simple(nvp_target_state,state)->name);
2087 if ((cur-then) > ms)
2089 LOG_ERROR("timed out while waiting for target %s",
2090 Jim_Nvp_value2name_simple(nvp_target_state,state)->name);
2098 COMMAND_HANDLER(handle_halt_command)
2102 struct target *target = get_current_target(CMD_CTX);
2103 int retval = target_halt(target);
2104 if (ERROR_OK != retval)
2110 retval = parse_uint(CMD_ARGV[0], &wait);
2111 if (ERROR_OK != retval)
2112 return ERROR_COMMAND_SYNTAX_ERROR;
2117 return CALL_COMMAND_HANDLER(handle_wait_halt_command);
2120 COMMAND_HANDLER(handle_soft_reset_halt_command)
2122 struct target *target = get_current_target(CMD_CTX);
2124 LOG_USER("requesting target halt and executing a soft reset");
2126 target->type->soft_reset_halt(target);
2131 COMMAND_HANDLER(handle_reset_command)
2134 return ERROR_COMMAND_SYNTAX_ERROR;
2136 enum target_reset_mode reset_mode = RESET_RUN;
2140 n = Jim_Nvp_name2value_simple(nvp_reset_modes, CMD_ARGV[0]);
2141 if ((n->name == NULL) || (n->value == RESET_UNKNOWN)) {
2142 return ERROR_COMMAND_SYNTAX_ERROR;
2144 reset_mode = n->value;
2147 /* reset *all* targets */
2148 return target_process_reset(CMD_CTX, reset_mode);
2152 COMMAND_HANDLER(handle_resume_command)
2156 return ERROR_COMMAND_SYNTAX_ERROR;
2158 struct target *target = get_current_target(CMD_CTX);
2159 target_handle_event(target, TARGET_EVENT_OLD_pre_resume);
2161 /* with no CMD_ARGV, resume from current pc, addr = 0,
2162 * with one arguments, addr = CMD_ARGV[0],
2163 * handle breakpoints, not debugging */
2167 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], addr);
2171 return target_resume(target, current, addr, 1, 0);
2174 COMMAND_HANDLER(handle_step_command)
2177 return ERROR_COMMAND_SYNTAX_ERROR;
2181 /* with no CMD_ARGV, step from current pc, addr = 0,
2182 * with one argument addr = CMD_ARGV[0],
2183 * handle breakpoints, debugging */
2188 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], addr);
2192 struct target *target = get_current_target(CMD_CTX);
2194 return target->type->step(target, current_pc, addr, 1);
2197 static void handle_md_output(struct command_context *cmd_ctx,
2198 struct target *target, uint32_t address, unsigned size,
2199 unsigned count, const uint8_t *buffer)
2201 const unsigned line_bytecnt = 32;
2202 unsigned line_modulo = line_bytecnt / size;
2204 char output[line_bytecnt * 4 + 1];
2205 unsigned output_len = 0;
2207 const char *value_fmt;
2209 case 4: value_fmt = "%8.8x "; break;
2210 case 2: value_fmt = "%4.2x "; break;
2211 case 1: value_fmt = "%2.2x "; break;
2213 LOG_ERROR("invalid memory read size: %u", size);
2217 for (unsigned i = 0; i < count; i++)
2219 if (i % line_modulo == 0)
2221 output_len += snprintf(output + output_len,
2222 sizeof(output) - output_len,
2224 (unsigned)(address + (i*size)));
2228 const uint8_t *value_ptr = buffer + i * size;
2230 case 4: value = target_buffer_get_u32(target, value_ptr); break;
2231 case 2: value = target_buffer_get_u16(target, value_ptr); break;
2232 case 1: value = *value_ptr;
2234 output_len += snprintf(output + output_len,
2235 sizeof(output) - output_len,
2238 if ((i % line_modulo == line_modulo - 1) || (i == count - 1))
2240 command_print(cmd_ctx, "%s", output);
2246 COMMAND_HANDLER(handle_md_command)
2249 return ERROR_COMMAND_SYNTAX_ERROR;
2252 switch (CMD_NAME[2]) {
2253 case 'w': size = 4; break;
2254 case 'h': size = 2; break;
2255 case 'b': size = 1; break;
2256 default: return ERROR_COMMAND_SYNTAX_ERROR;
2259 bool physical=strcmp(CMD_ARGV[0], "phys")==0;
2260 int (*fn)(struct target *target,
2261 uint32_t address, uint32_t size, uint32_t count, uint8_t *buffer);
2266 fn=target_read_phys_memory;
2269 fn=target_read_memory;
2271 if ((CMD_ARGC < 1) || (CMD_ARGC > 2))
2273 return ERROR_COMMAND_SYNTAX_ERROR;
2277 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], address);
2281 COMMAND_PARSE_NUMBER(uint, CMD_ARGV[1], count);
2283 uint8_t *buffer = calloc(count, size);
2285 struct target *target = get_current_target(CMD_CTX);
2286 int retval = fn(target, address, size, count, buffer);
2287 if (ERROR_OK == retval)
2288 handle_md_output(CMD_CTX, target, address, size, count, buffer);
2295 COMMAND_HANDLER(handle_mw_command)
2299 return ERROR_COMMAND_SYNTAX_ERROR;
2301 bool physical=strcmp(CMD_ARGV[0], "phys")==0;
2302 int (*fn)(struct target *target,
2303 uint32_t address, uint32_t size, uint32_t count, uint8_t *buffer);
2308 fn=target_write_phys_memory;
2311 fn=target_write_memory;
2313 if ((CMD_ARGC < 2) || (CMD_ARGC > 3))
2314 return ERROR_COMMAND_SYNTAX_ERROR;
2317 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], address);
2320 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], value);
2324 COMMAND_PARSE_NUMBER(uint, CMD_ARGV[2], count);
2326 struct target *target = get_current_target(CMD_CTX);
2328 uint8_t value_buf[4];
2329 switch (CMD_NAME[2])
2333 target_buffer_set_u32(target, value_buf, value);
2337 target_buffer_set_u16(target, value_buf, value);
2341 value_buf[0] = value;
2344 return ERROR_COMMAND_SYNTAX_ERROR;
2346 for (unsigned i = 0; i < count; i++)
2348 int retval = fn(target,
2349 address + i * wordsize, wordsize, 1, value_buf);
2350 if (ERROR_OK != retval)
2359 static COMMAND_HELPER(parse_load_image_command_CMD_ARGV, struct image *image,
2360 uint32_t *min_address, uint32_t *max_address)
2362 if (CMD_ARGC < 1 || CMD_ARGC > 5)
2363 return ERROR_COMMAND_SYNTAX_ERROR;
2365 /* a base address isn't always necessary,
2366 * default to 0x0 (i.e. don't relocate) */
2370 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], addr);
2371 image->base_address = addr;
2372 image->base_address_set = 1;
2375 image->base_address_set = 0;
2377 image->start_address_set = 0;
2381 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[3], *min_address);
2385 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[4], *max_address);
2386 // use size (given) to find max (required)
2387 *max_address += *min_address;
2390 if (*min_address > *max_address)
2391 return ERROR_COMMAND_SYNTAX_ERROR;
2396 COMMAND_HANDLER(handle_load_image_command)
2400 uint32_t image_size;
2401 uint32_t min_address = 0;
2402 uint32_t max_address = 0xffffffff;
2406 int retval = CALL_COMMAND_HANDLER(parse_load_image_command_CMD_ARGV,
2407 &image, &min_address, &max_address);
2408 if (ERROR_OK != retval)
2411 struct target *target = get_current_target(CMD_CTX);
2413 struct duration bench;
2414 duration_start(&bench);
2416 if (image_open(&image, CMD_ARGV[0], (CMD_ARGC >= 3) ? CMD_ARGV[2] : NULL) != ERROR_OK)
2423 for (i = 0; i < image.num_sections; i++)
2425 buffer = malloc(image.sections[i].size);
2428 command_print(CMD_CTX,
2429 "error allocating buffer for section (%d bytes)",
2430 (int)(image.sections[i].size));
2434 if ((retval = image_read_section(&image, i, 0x0, image.sections[i].size, buffer, &buf_cnt)) != ERROR_OK)
2440 uint32_t offset = 0;
2441 uint32_t length = buf_cnt;
2443 /* DANGER!!! beware of unsigned comparision here!!! */
2445 if ((image.sections[i].base_address + buf_cnt >= min_address)&&
2446 (image.sections[i].base_address < max_address))
2448 if (image.sections[i].base_address < min_address)
2450 /* clip addresses below */
2451 offset += min_address-image.sections[i].base_address;
2455 if (image.sections[i].base_address + buf_cnt > max_address)
2457 length -= (image.sections[i].base_address + buf_cnt)-max_address;
2460 if ((retval = target_write_buffer(target, image.sections[i].base_address + offset, length, buffer + offset)) != ERROR_OK)
2465 image_size += length;
2466 command_print(CMD_CTX, "%u bytes written at address 0x%8.8" PRIx32 "",
2467 (unsigned int)length,
2468 image.sections[i].base_address + offset);
2474 if ((ERROR_OK == retval) && (duration_measure(&bench) == ERROR_OK))
2476 command_print(CMD_CTX, "downloaded %" PRIu32 " bytes "
2477 "in %fs (%0.3f kb/s)", image_size,
2478 duration_elapsed(&bench), duration_kbps(&bench, image_size));
2481 image_close(&image);
2487 COMMAND_HANDLER(handle_dump_image_command)
2489 struct fileio fileio;
2491 uint8_t buffer[560];
2495 struct target *target = get_current_target(CMD_CTX);
2499 command_print(CMD_CTX, "usage: dump_image <filename> <address> <size>");
2504 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], address);
2506 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[2], size);
2508 if (fileio_open(&fileio, CMD_ARGV[0], FILEIO_WRITE, FILEIO_BINARY) != ERROR_OK)
2513 struct duration bench;
2514 duration_start(&bench);
2516 int retval = ERROR_OK;
2519 size_t size_written;
2520 uint32_t this_run_size = (size > 560) ? 560 : size;
2521 retval = target_read_buffer(target, address, this_run_size, buffer);
2522 if (retval != ERROR_OK)
2527 retval = fileio_write(&fileio, this_run_size, buffer, &size_written);
2528 if (retval != ERROR_OK)
2533 size -= this_run_size;
2534 address += this_run_size;
2537 if ((retvaltemp = fileio_close(&fileio)) != ERROR_OK)
2540 if ((ERROR_OK == retval) && (duration_measure(&bench) == ERROR_OK))
2542 command_print(CMD_CTX,
2543 "dumped %zu bytes in %fs (%0.3f kb/s)", fileio.size,
2544 duration_elapsed(&bench), duration_kbps(&bench, fileio.size));
2550 static COMMAND_HELPER(handle_verify_image_command_internal, int verify)
2554 uint32_t image_size;
2557 uint32_t checksum = 0;
2558 uint32_t mem_checksum = 0;
2562 struct target *target = get_current_target(CMD_CTX);
2566 return ERROR_COMMAND_SYNTAX_ERROR;
2571 LOG_ERROR("no target selected");
2575 struct duration bench;
2576 duration_start(&bench);
2581 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], addr);
2582 image.base_address = addr;
2583 image.base_address_set = 1;
2587 image.base_address_set = 0;
2588 image.base_address = 0x0;
2591 image.start_address_set = 0;
2593 if ((retval = image_open(&image, CMD_ARGV[0], (CMD_ARGC == 3) ? CMD_ARGV[2] : NULL)) != ERROR_OK)
2600 for (i = 0; i < image.num_sections; i++)
2602 buffer = malloc(image.sections[i].size);
2605 command_print(CMD_CTX,
2606 "error allocating buffer for section (%d bytes)",
2607 (int)(image.sections[i].size));
2610 if ((retval = image_read_section(&image, i, 0x0, image.sections[i].size, buffer, &buf_cnt)) != ERROR_OK)
2618 /* calculate checksum of image */
2619 image_calculate_checksum(buffer, buf_cnt, &checksum);
2621 retval = target_checksum_memory(target, image.sections[i].base_address, buf_cnt, &mem_checksum);
2622 if (retval != ERROR_OK)
2628 if (checksum != mem_checksum)
2630 /* failed crc checksum, fall back to a binary compare */
2633 command_print(CMD_CTX, "checksum mismatch - attempting binary compare");
2635 data = (uint8_t*)malloc(buf_cnt);
2637 /* Can we use 32bit word accesses? */
2639 int count = buf_cnt;
2640 if ((count % 4) == 0)
2645 retval = target_read_memory(target, image.sections[i].base_address, size, count, data);
2646 if (retval == ERROR_OK)
2649 for (t = 0; t < buf_cnt; t++)
2651 if (data[t] != buffer[t])
2653 command_print(CMD_CTX,
2654 "Verify operation failed address 0x%08x. Was 0x%02x instead of 0x%02x\n",
2655 (unsigned)(t + image.sections[i].base_address),
2660 retval = ERROR_FAIL;
2674 command_print(CMD_CTX, "address 0x%08" PRIx32 " length 0x%08zx",
2675 image.sections[i].base_address,
2680 image_size += buf_cnt;
2683 if ((ERROR_OK == retval) && (duration_measure(&bench) == ERROR_OK))
2685 command_print(CMD_CTX, "verified %" PRIu32 " bytes "
2686 "in %fs (%0.3f kb/s)", image_size,
2687 duration_elapsed(&bench), duration_kbps(&bench, image_size));
2690 image_close(&image);
2695 COMMAND_HANDLER(handle_verify_image_command)
2697 return CALL_COMMAND_HANDLER(handle_verify_image_command_internal, 1);
2700 COMMAND_HANDLER(handle_test_image_command)
2702 return CALL_COMMAND_HANDLER(handle_verify_image_command_internal, 0);
2705 static int handle_bp_command_list(struct command_context *cmd_ctx)
2707 struct target *target = get_current_target(cmd_ctx);
2708 struct breakpoint *breakpoint = target->breakpoints;
2711 if (breakpoint->type == BKPT_SOFT)
2713 char* buf = buf_to_str(breakpoint->orig_instr,
2714 breakpoint->length, 16);
2715 command_print(cmd_ctx, "0x%8.8" PRIx32 ", 0x%x, %i, 0x%s",
2716 breakpoint->address,
2718 breakpoint->set, buf);
2723 command_print(cmd_ctx, "0x%8.8" PRIx32 ", 0x%x, %i",
2724 breakpoint->address,
2725 breakpoint->length, breakpoint->set);
2728 breakpoint = breakpoint->next;
2733 static int handle_bp_command_set(struct command_context *cmd_ctx,
2734 uint32_t addr, uint32_t length, int hw)
2736 struct target *target = get_current_target(cmd_ctx);
2737 int retval = breakpoint_add(target, addr, length, hw);
2738 if (ERROR_OK == retval)
2739 command_print(cmd_ctx, "breakpoint set at 0x%8.8" PRIx32 "", addr);
2741 LOG_ERROR("Failure setting breakpoint");
2745 COMMAND_HANDLER(handle_bp_command)
2748 return handle_bp_command_list(CMD_CTX);
2750 if (CMD_ARGC < 2 || CMD_ARGC > 3)
2752 command_print(CMD_CTX, "usage: bp <address> <length> ['hw']");
2753 return ERROR_COMMAND_SYNTAX_ERROR;
2757 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], addr);
2759 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], length);
2764 if (strcmp(CMD_ARGV[2], "hw") == 0)
2767 return ERROR_COMMAND_SYNTAX_ERROR;
2770 return handle_bp_command_set(CMD_CTX, addr, length, hw);
2773 COMMAND_HANDLER(handle_rbp_command)
2776 return ERROR_COMMAND_SYNTAX_ERROR;
2779 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], addr);
2781 struct target *target = get_current_target(CMD_CTX);
2782 breakpoint_remove(target, addr);
2787 COMMAND_HANDLER(handle_wp_command)
2789 struct target *target = get_current_target(CMD_CTX);
2793 struct watchpoint *watchpoint = target->watchpoints;
2797 command_print(CMD_CTX, "address: 0x%8.8" PRIx32
2798 ", len: 0x%8.8" PRIx32
2799 ", r/w/a: %i, value: 0x%8.8" PRIx32
2800 ", mask: 0x%8.8" PRIx32,
2801 watchpoint->address,
2803 (int)watchpoint->rw,
2806 watchpoint = watchpoint->next;
2811 enum watchpoint_rw type = WPT_ACCESS;
2813 uint32_t length = 0;
2814 uint32_t data_value = 0x0;
2815 uint32_t data_mask = 0xffffffff;
2820 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[4], data_mask);
2823 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[3], data_value);
2826 switch (CMD_ARGV[2][0])
2838 LOG_ERROR("invalid watchpoint mode ('%c')", CMD_ARGV[2][0]);
2839 return ERROR_COMMAND_SYNTAX_ERROR;
2843 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], length);
2844 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], addr);
2848 command_print(CMD_CTX, "usage: wp [address length "
2849 "[(r|w|a) [value [mask]]]]");
2850 return ERROR_COMMAND_SYNTAX_ERROR;
2853 int retval = watchpoint_add(target, addr, length, type,
2854 data_value, data_mask);
2855 if (ERROR_OK != retval)
2856 LOG_ERROR("Failure setting watchpoints");
2861 COMMAND_HANDLER(handle_rwp_command)
2864 return ERROR_COMMAND_SYNTAX_ERROR;
2867 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], addr);
2869 struct target *target = get_current_target(CMD_CTX);
2870 watchpoint_remove(target, addr);
2877 * Translate a virtual address to a physical address.
2879 * The low-level target implementation must have logged a detailed error
2880 * which is forwarded to telnet/GDB session.
2882 COMMAND_HANDLER(handle_virt2phys_command)
2885 return ERROR_COMMAND_SYNTAX_ERROR;
2888 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], va);
2891 struct target *target = get_current_target(CMD_CTX);
2892 int retval = target->type->virt2phys(target, va, &pa);
2893 if (retval == ERROR_OK)
2894 command_print(CMD_CTX, "Physical address 0x%08" PRIx32 "", pa);
2899 static void writeData(FILE *f, const void *data, size_t len)
2901 size_t written = fwrite(data, 1, len, f);
2903 LOG_ERROR("failed to write %zu bytes: %s", len, strerror(errno));
2906 static void writeLong(FILE *f, int l)
2909 for (i = 0; i < 4; i++)
2911 char c = (l >> (i*8))&0xff;
2912 writeData(f, &c, 1);
2917 static void writeString(FILE *f, char *s)
2919 writeData(f, s, strlen(s));
2922 /* Dump a gmon.out histogram file. */
2923 static void writeGmon(uint32_t *samples, uint32_t sampleNum, const char *filename)
2926 FILE *f = fopen(filename, "w");
2929 writeString(f, "gmon");
2930 writeLong(f, 0x00000001); /* Version */
2931 writeLong(f, 0); /* padding */
2932 writeLong(f, 0); /* padding */
2933 writeLong(f, 0); /* padding */
2935 uint8_t zero = 0; /* GMON_TAG_TIME_HIST */
2936 writeData(f, &zero, 1);
2938 /* figure out bucket size */
2939 uint32_t min = samples[0];
2940 uint32_t max = samples[0];
2941 for (i = 0; i < sampleNum; i++)
2943 if (min > samples[i])
2947 if (max < samples[i])
2953 int addressSpace = (max-min + 1);
2955 static const uint32_t maxBuckets = 256 * 1024; /* maximum buckets. */
2956 uint32_t length = addressSpace;
2957 if (length > maxBuckets)
2959 length = maxBuckets;
2961 int *buckets = malloc(sizeof(int)*length);
2962 if (buckets == NULL)
2967 memset(buckets, 0, sizeof(int)*length);
2968 for (i = 0; i < sampleNum;i++)
2970 uint32_t address = samples[i];
2971 long long a = address-min;
2972 long long b = length-1;
2973 long long c = addressSpace-1;
2974 int index = (a*b)/c; /* danger!!!! int32 overflows */
2978 /* append binary memory gmon.out &profile_hist_hdr ((char*)&profile_hist_hdr + sizeof(struct gmon_hist_hdr)) */
2979 writeLong(f, min); /* low_pc */
2980 writeLong(f, max); /* high_pc */
2981 writeLong(f, length); /* # of samples */
2982 writeLong(f, 64000000); /* 64MHz */
2983 writeString(f, "seconds");
2984 for (i = 0; i < (15-strlen("seconds")); i++)
2985 writeData(f, &zero, 1);
2986 writeString(f, "s");
2988 /*append binary memory gmon.out profile_hist_data (profile_hist_data + profile_hist_hdr.hist_size) */
2990 char *data = malloc(2*length);
2993 for (i = 0; i < length;i++)
3002 data[i*2 + 1]=(val >> 8)&0xff;
3005 writeData(f, data, length * 2);
3015 /* profiling samples the CPU PC as quickly as OpenOCD is able, which will be used as a random sampling of PC */
3016 COMMAND_HANDLER(handle_profile_command)
3018 struct target *target = get_current_target(CMD_CTX);
3019 struct timeval timeout, now;
3021 gettimeofday(&timeout, NULL);
3024 return ERROR_COMMAND_SYNTAX_ERROR;
3027 COMMAND_PARSE_NUMBER(uint, CMD_ARGV[0], offset);
3029 timeval_add_time(&timeout, offset, 0);
3031 command_print(CMD_CTX, "Starting profiling. Halting and resuming the target as often as we can...");
3033 static const int maxSample = 10000;
3034 uint32_t *samples = malloc(sizeof(uint32_t)*maxSample);
3035 if (samples == NULL)
3039 /* hopefully it is safe to cache! We want to stop/restart as quickly as possible. */
3040 struct reg *reg = register_get_by_name(target->reg_cache, "pc", 1);
3045 target_poll(target);
3046 if (target->state == TARGET_HALTED)
3048 uint32_t t=*((uint32_t *)reg->value);
3049 samples[numSamples++]=t;
3050 retval = target_resume(target, 1, 0, 0, 0); /* current pc, addr = 0, do not handle breakpoints, not debugging */
3051 target_poll(target);
3052 alive_sleep(10); /* sleep 10ms, i.e. <100 samples/second. */
3053 } else if (target->state == TARGET_RUNNING)
3055 /* We want to quickly sample the PC. */
3056 if ((retval = target_halt(target)) != ERROR_OK)
3063 command_print(CMD_CTX, "Target not halted or running");
3067 if (retval != ERROR_OK)
3072 gettimeofday(&now, NULL);
3073 if ((numSamples >= maxSample) || ((now.tv_sec >= timeout.tv_sec) && (now.tv_usec >= timeout.tv_usec)))
3075 command_print(CMD_CTX, "Profiling completed. %d samples.", numSamples);
3076 if ((retval = target_poll(target)) != ERROR_OK)
3081 if (target->state == TARGET_HALTED)
3083 target_resume(target, 1, 0, 0, 0); /* current pc, addr = 0, do not handle breakpoints, not debugging */
3085 if ((retval = target_poll(target)) != ERROR_OK)
3090 writeGmon(samples, numSamples, CMD_ARGV[1]);
3091 command_print(CMD_CTX, "Wrote %s", CMD_ARGV[1]);
3100 static int new_int_array_element(Jim_Interp * interp, const char *varname, int idx, uint32_t val)
3103 Jim_Obj *nameObjPtr, *valObjPtr;
3106 namebuf = alloc_printf("%s(%d)", varname, idx);
3110 nameObjPtr = Jim_NewStringObj(interp, namebuf, -1);
3111 valObjPtr = Jim_NewIntObj(interp, val);
3112 if (!nameObjPtr || !valObjPtr)
3118 Jim_IncrRefCount(nameObjPtr);
3119 Jim_IncrRefCount(valObjPtr);
3120 result = Jim_SetVariable(interp, nameObjPtr, valObjPtr);
3121 Jim_DecrRefCount(interp, nameObjPtr);
3122 Jim_DecrRefCount(interp, valObjPtr);
3124 /* printf("%s(%d) <= 0%08x\n", varname, idx, val); */
3128 static int jim_mem2array(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
3130 struct command_context *context;
3131 struct target *target;
3133 context = Jim_GetAssocData(interp, "context");
3134 if (context == NULL)
3136 LOG_ERROR("mem2array: no command context");
3139 target = get_current_target(context);
3142 LOG_ERROR("mem2array: no current target");
3146 return target_mem2array(interp, target, argc-1, argv + 1);
3149 static int target_mem2array(Jim_Interp *interp, struct target *target, int argc, Jim_Obj *const *argv)
3157 const char *varname;
3161 /* argv[1] = name of array to receive the data
3162 * argv[2] = desired width
3163 * argv[3] = memory address
3164 * argv[4] = count of times to read
3167 Jim_WrongNumArgs(interp, 1, argv, "varname width addr nelems");
3170 varname = Jim_GetString(argv[0], &len);
3171 /* given "foo" get space for worse case "foo(%d)" .. add 20 */
3173 e = Jim_GetLong(interp, argv[1], &l);
3179 e = Jim_GetLong(interp, argv[2], &l);
3184 e = Jim_GetLong(interp, argv[3], &l);
3200 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3201 Jim_AppendStrings(interp, Jim_GetResult(interp), "Invalid width param, must be 8/16/32", NULL);
3205 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3206 Jim_AppendStrings(interp, Jim_GetResult(interp), "mem2array: zero width read?", NULL);
3209 if ((addr + (len * width)) < addr) {
3210 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3211 Jim_AppendStrings(interp, Jim_GetResult(interp), "mem2array: addr + len - wraps to zero?", NULL);
3214 /* absurd transfer size? */
3216 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3217 Jim_AppendStrings(interp, Jim_GetResult(interp), "mem2array: absurd > 64K item request", NULL);
3222 ((width == 2) && ((addr & 1) == 0)) ||
3223 ((width == 4) && ((addr & 3) == 0))) {
3227 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3228 sprintf(buf, "mem2array address: 0x%08" PRIx32 " is not aligned for %" PRId32 " byte reads",
3231 Jim_AppendStrings(interp, Jim_GetResult(interp), buf , NULL);
3240 size_t buffersize = 4096;
3241 uint8_t *buffer = malloc(buffersize);
3248 /* Slurp... in buffer size chunks */
3250 count = len; /* in objects.. */
3251 if (count > (buffersize/width)) {
3252 count = (buffersize/width);
3255 retval = target_read_memory(target, addr, width, count, buffer);
3256 if (retval != ERROR_OK) {
3258 LOG_ERROR("mem2array: Read @ 0x%08x, w=%d, cnt=%d, failed",
3262 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3263 Jim_AppendStrings(interp, Jim_GetResult(interp), "mem2array: cannot read memory", NULL);
3267 v = 0; /* shut up gcc */
3268 for (i = 0 ;i < count ;i++, n++) {
3271 v = target_buffer_get_u32(target, &buffer[i*width]);
3274 v = target_buffer_get_u16(target, &buffer[i*width]);
3277 v = buffer[i] & 0x0ff;
3280 new_int_array_element(interp, varname, n, v);
3288 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3293 static int get_int_array_element(Jim_Interp * interp, const char *varname, int idx, uint32_t *val)
3296 Jim_Obj *nameObjPtr, *valObjPtr;
3300 namebuf = alloc_printf("%s(%d)", varname, idx);
3304 nameObjPtr = Jim_NewStringObj(interp, namebuf, -1);
3311 Jim_IncrRefCount(nameObjPtr);
3312 valObjPtr = Jim_GetVariable(interp, nameObjPtr, JIM_ERRMSG);
3313 Jim_DecrRefCount(interp, nameObjPtr);
3315 if (valObjPtr == NULL)
3318 result = Jim_GetLong(interp, valObjPtr, &l);
3319 /* printf("%s(%d) => 0%08x\n", varname, idx, val); */
3324 static int jim_array2mem(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
3326 struct command_context *context;
3327 struct target *target;
3329 context = Jim_GetAssocData(interp, "context");
3330 if (context == NULL) {
3331 LOG_ERROR("array2mem: no command context");
3334 target = get_current_target(context);
3335 if (target == NULL) {
3336 LOG_ERROR("array2mem: no current target");
3340 return target_array2mem(interp,target, argc-1, argv + 1);
3342 static int target_array2mem(Jim_Interp *interp, struct target *target, int argc, Jim_Obj *const *argv)
3350 const char *varname;
3354 /* argv[1] = name of array to get the data
3355 * argv[2] = desired width
3356 * argv[3] = memory address
3357 * argv[4] = count to write
3360 Jim_WrongNumArgs(interp, 1, argv, "varname width addr nelems");
3363 varname = Jim_GetString(argv[0], &len);
3364 /* given "foo" get space for worse case "foo(%d)" .. add 20 */
3366 e = Jim_GetLong(interp, argv[1], &l);
3372 e = Jim_GetLong(interp, argv[2], &l);
3377 e = Jim_GetLong(interp, argv[3], &l);
3393 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3394 Jim_AppendStrings(interp, Jim_GetResult(interp), "Invalid width param, must be 8/16/32", NULL);
3398 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3399 Jim_AppendStrings(interp, Jim_GetResult(interp), "array2mem: zero width read?", NULL);
3402 if ((addr + (len * width)) < addr) {
3403 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3404 Jim_AppendStrings(interp, Jim_GetResult(interp), "array2mem: addr + len - wraps to zero?", NULL);
3407 /* absurd transfer size? */
3409 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3410 Jim_AppendStrings(interp, Jim_GetResult(interp), "array2mem: absurd > 64K item request", NULL);
3415 ((width == 2) && ((addr & 1) == 0)) ||
3416 ((width == 4) && ((addr & 3) == 0))) {
3420 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3421 sprintf(buf, "array2mem address: 0x%08x is not aligned for %d byte reads",
3424 Jim_AppendStrings(interp, Jim_GetResult(interp), buf , NULL);
3435 size_t buffersize = 4096;
3436 uint8_t *buffer = malloc(buffersize);
3441 /* Slurp... in buffer size chunks */
3443 count = len; /* in objects.. */
3444 if (count > (buffersize/width)) {
3445 count = (buffersize/width);
3448 v = 0; /* shut up gcc */
3449 for (i = 0 ;i < count ;i++, n++) {
3450 get_int_array_element(interp, varname, n, &v);
3453 target_buffer_set_u32(target, &buffer[i*width], v);
3456 target_buffer_set_u16(target, &buffer[i*width], v);
3459 buffer[i] = v & 0x0ff;
3465 retval = target_write_memory(target, addr, width, count, buffer);
3466 if (retval != ERROR_OK) {
3468 LOG_ERROR("array2mem: Write @ 0x%08x, w=%d, cnt=%d, failed",
3472 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3473 Jim_AppendStrings(interp, Jim_GetResult(interp), "array2mem: cannot read memory", NULL);
3481 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3486 void target_all_handle_event(enum target_event e)
3488 struct target *target;
3490 LOG_DEBUG("**all*targets: event: %d, %s",
3492 Jim_Nvp_value2name_simple(nvp_target_event, e)->name);
3494 target = all_targets;
3496 target_handle_event(target, e);
3497 target = target->next;
3502 /* FIX? should we propagate errors here rather than printing them
3505 void target_handle_event(struct target *target, enum target_event e)
3507 struct target_event_action *teap;
3509 for (teap = target->event_action; teap != NULL; teap = teap->next) {
3510 if (teap->event == e) {
3511 LOG_DEBUG("target: (%d) %s (%s) event: %d (%s) action: %s",
3512 target->target_number,
3514 target_type_name(target),
3516 Jim_Nvp_value2name_simple(nvp_target_event, e)->name,
3517 Jim_GetString(teap->body, NULL));
3518 if (Jim_EvalObj(interp, teap->body) != JIM_OK)
3520 Jim_PrintErrorMessage(interp);
3526 enum target_cfg_param {
3529 TCFG_WORK_AREA_VIRT,
3530 TCFG_WORK_AREA_PHYS,
3531 TCFG_WORK_AREA_SIZE,
3532 TCFG_WORK_AREA_BACKUP,
3535 TCFG_CHAIN_POSITION,
3538 static Jim_Nvp nvp_config_opts[] = {
3539 { .name = "-type", .value = TCFG_TYPE },
3540 { .name = "-event", .value = TCFG_EVENT },
3541 { .name = "-work-area-virt", .value = TCFG_WORK_AREA_VIRT },
3542 { .name = "-work-area-phys", .value = TCFG_WORK_AREA_PHYS },
3543 { .name = "-work-area-size", .value = TCFG_WORK_AREA_SIZE },
3544 { .name = "-work-area-backup", .value = TCFG_WORK_AREA_BACKUP },
3545 { .name = "-endian" , .value = TCFG_ENDIAN },
3546 { .name = "-variant", .value = TCFG_VARIANT },
3547 { .name = "-chain-position", .value = TCFG_CHAIN_POSITION },
3549 { .name = NULL, .value = -1 }
3552 static int target_configure(Jim_GetOptInfo *goi, struct target *target)
3560 /* parse config or cget options ... */
3561 while (goi->argc > 0) {
3562 Jim_SetEmptyResult(goi->interp);
3563 /* Jim_GetOpt_Debug(goi); */
3565 if (target->type->target_jim_configure) {
3566 /* target defines a configure function */
3567 /* target gets first dibs on parameters */
3568 e = (*(target->type->target_jim_configure))(target, goi);
3577 /* otherwise we 'continue' below */
3579 e = Jim_GetOpt_Nvp(goi, nvp_config_opts, &n);
3581 Jim_GetOpt_NvpUnknown(goi, nvp_config_opts, 0);
3587 if (goi->isconfigure) {
3588 Jim_SetResult_sprintf(goi->interp,
3589 "not settable: %s", n->name);
3593 if (goi->argc != 0) {
3594 Jim_WrongNumArgs(goi->interp,
3595 goi->argc, goi->argv,
3600 Jim_SetResultString(goi->interp,
3601 target_type_name(target), -1);
3605 if (goi->argc == 0) {
3606 Jim_WrongNumArgs(goi->interp, goi->argc, goi->argv, "-event ?event-name? ...");
3610 e = Jim_GetOpt_Nvp(goi, nvp_target_event, &n);
3612 Jim_GetOpt_NvpUnknown(goi, nvp_target_event, 1);
3616 if (goi->isconfigure) {
3617 if (goi->argc != 1) {
3618 Jim_WrongNumArgs(goi->interp, goi->argc, goi->argv, "-event ?event-name? ?EVENT-BODY?");
3622 if (goi->argc != 0) {
3623 Jim_WrongNumArgs(goi->interp, goi->argc, goi->argv, "-event ?event-name?");
3629 struct target_event_action *teap;
3631 teap = target->event_action;
3632 /* replace existing? */
3634 if (teap->event == (enum target_event)n->value) {
3640 if (goi->isconfigure) {
3641 bool replace = true;
3644 teap = calloc(1, sizeof(*teap));
3647 teap->event = n->value;
3648 Jim_GetOpt_Obj(goi, &o);
3650 Jim_DecrRefCount(interp, teap->body);
3652 teap->body = Jim_DuplicateObj(goi->interp, o);
3655 * Tcl/TK - "tk events" have a nice feature.
3656 * See the "BIND" command.
3657 * We should support that here.
3658 * You can specify %X and %Y in the event code.
3659 * The idea is: %T - target name.
3660 * The idea is: %N - target number
3661 * The idea is: %E - event name.
3663 Jim_IncrRefCount(teap->body);
3667 /* add to head of event list */
3668 teap->next = target->event_action;
3669 target->event_action = teap;
3671 Jim_SetEmptyResult(goi->interp);
3675 Jim_SetEmptyResult(goi->interp);
3677 Jim_SetResult(goi->interp, Jim_DuplicateObj(goi->interp, teap->body));
3684 case TCFG_WORK_AREA_VIRT:
3685 if (goi->isconfigure) {
3686 target_free_all_working_areas(target);
3687 e = Jim_GetOpt_Wide(goi, &w);
3691 target->working_area_virt = w;
3692 target->working_area_virt_spec = true;
3694 if (goi->argc != 0) {
3698 Jim_SetResult(interp, Jim_NewIntObj(goi->interp, target->working_area_virt));
3702 case TCFG_WORK_AREA_PHYS:
3703 if (goi->isconfigure) {
3704 target_free_all_working_areas(target);
3705 e = Jim_GetOpt_Wide(goi, &w);
3709 target->working_area_phys = w;
3710 target->working_area_phys_spec = true;
3712 if (goi->argc != 0) {
3716 Jim_SetResult(interp, Jim_NewIntObj(goi->interp, target->working_area_phys));
3720 case TCFG_WORK_AREA_SIZE:
3721 if (goi->isconfigure) {
3722 target_free_all_working_areas(target);
3723 e = Jim_GetOpt_Wide(goi, &w);
3727 target->working_area_size = w;
3729 if (goi->argc != 0) {
3733 Jim_SetResult(interp, Jim_NewIntObj(goi->interp, target->working_area_size));
3737 case TCFG_WORK_AREA_BACKUP:
3738 if (goi->isconfigure) {
3739 target_free_all_working_areas(target);
3740 e = Jim_GetOpt_Wide(goi, &w);
3744 /* make this exactly 1 or 0 */
3745 target->backup_working_area = (!!w);
3747 if (goi->argc != 0) {
3751 Jim_SetResult(interp, Jim_NewIntObj(goi->interp, target->backup_working_area));
3752 /* loop for more e*/
3756 if (goi->isconfigure) {
3757 e = Jim_GetOpt_Nvp(goi, nvp_target_endian, &n);
3759 Jim_GetOpt_NvpUnknown(goi, nvp_target_endian, 1);
3762 target->endianness = n->value;
3764 if (goi->argc != 0) {
3768 n = Jim_Nvp_value2name_simple(nvp_target_endian, target->endianness);
3769 if (n->name == NULL) {
3770 target->endianness = TARGET_LITTLE_ENDIAN;
3771 n = Jim_Nvp_value2name_simple(nvp_target_endian, target->endianness);
3773 Jim_SetResultString(goi->interp, n->name, -1);
3778 if (goi->isconfigure) {
3779 if (goi->argc < 1) {
3780 Jim_SetResult_sprintf(goi->interp,
3785 if (target->variant) {
3786 free((void *)(target->variant));
3788 e = Jim_GetOpt_String(goi, &cp, NULL);
3789 target->variant = strdup(cp);
3791 if (goi->argc != 0) {
3795 Jim_SetResultString(goi->interp, target->variant,-1);
3798 case TCFG_CHAIN_POSITION:
3799 if (goi->isconfigure) {
3801 struct jtag_tap *tap;
3802 target_free_all_working_areas(target);
3803 e = Jim_GetOpt_Obj(goi, &o);
3807 tap = jtag_tap_by_jim_obj(goi->interp, o);
3811 /* make this exactly 1 or 0 */
3814 if (goi->argc != 0) {
3818 Jim_SetResultString(interp, target->tap->dotted_name, -1);
3819 /* loop for more e*/
3822 } /* while (goi->argc) */
3825 /* done - we return */
3829 /** this is the 'tcl' handler for the target specific command */
3830 static int tcl_target_func(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
3835 uint8_t target_buf[32];
3837 struct target *target;
3838 struct command_context *cmd_ctx;
3845 TS_CMD_MWW, TS_CMD_MWH, TS_CMD_MWB,
3846 TS_CMD_MDW, TS_CMD_MDH, TS_CMD_MDB,
3847 TS_CMD_MRW, TS_CMD_MRH, TS_CMD_MRB,
3848 TS_CMD_MEM2ARRAY, TS_CMD_ARRAY2MEM,
3856 TS_CMD_INVOKE_EVENT,
3859 static const Jim_Nvp target_options[] = {
3860 { .name = "configure", .value = TS_CMD_CONFIGURE },
3861 { .name = "cget", .value = TS_CMD_CGET },
3862 { .name = "mww", .value = TS_CMD_MWW },
3863 { .name = "mwh", .value = TS_CMD_MWH },
3864 { .name = "mwb", .value = TS_CMD_MWB },
3865 { .name = "mdw", .value = TS_CMD_MDW },
3866 { .name = "mdh", .value = TS_CMD_MDH },
3867 { .name = "mdb", .value = TS_CMD_MDB },
3868 { .name = "mem2array", .value = TS_CMD_MEM2ARRAY },
3869 { .name = "array2mem", .value = TS_CMD_ARRAY2MEM },
3870 { .name = "eventlist", .value = TS_CMD_EVENTLIST },
3871 { .name = "curstate", .value = TS_CMD_CURSTATE },
3873 { .name = "arp_examine", .value = TS_CMD_EXAMINE },
3874 { .name = "arp_poll", .value = TS_CMD_POLL },
3875 { .name = "arp_reset", .value = TS_CMD_RESET },
3876 { .name = "arp_halt", .value = TS_CMD_HALT },
3877 { .name = "arp_waitstate", .value = TS_CMD_WAITSTATE },
3878 { .name = "invoke-event", .value = TS_CMD_INVOKE_EVENT },
3880 { .name = NULL, .value = -1 },
3883 /* go past the "command" */
3884 Jim_GetOpt_Setup(&goi, interp, argc-1, argv + 1);
3886 target = Jim_CmdPrivData(goi.interp);
3887 cmd_ctx = Jim_GetAssocData(goi.interp, "context");
3889 /* commands here are in an NVP table */
3890 e = Jim_GetOpt_Nvp(&goi, target_options, &n);
3892 Jim_GetOpt_NvpUnknown(&goi, target_options, 0);
3895 /* Assume blank result */
3896 Jim_SetEmptyResult(goi.interp);
3899 case TS_CMD_CONFIGURE:
3901 Jim_WrongNumArgs(goi.interp, goi.argc, goi.argv, "missing: -option VALUE ...");
3904 goi.isconfigure = 1;
3905 return target_configure(&goi, target);
3907 // some things take params
3909 Jim_WrongNumArgs(goi.interp, 0, goi.argv, "missing: ?-option?");
3912 goi.isconfigure = 0;
3913 return target_configure(&goi, target);
3921 * argv[3] = optional count.
3924 if ((goi.argc == 2) || (goi.argc == 3)) {
3928 Jim_SetResult_sprintf(goi.interp, "expected: %s ADDR DATA [COUNT]", n->name);
3932 e = Jim_GetOpt_Wide(&goi, &a);
3937 e = Jim_GetOpt_Wide(&goi, &b);
3941 if (goi.argc == 3) {
3942 e = Jim_GetOpt_Wide(&goi, &c);
3952 target_buffer_set_u32(target, target_buf, b);
3956 target_buffer_set_u16(target, target_buf, b);
3960 target_buffer_set_u8(target, target_buf, b);
3964 for (x = 0 ; x < c ; x++) {
3965 e = target_write_memory(target, a, b, 1, target_buf);
3966 if (e != ERROR_OK) {
3967 Jim_SetResult_sprintf(interp, "Error writing @ 0x%08x: %d\n", (int)(a), e);
3980 /* argv[0] = command
3982 * argv[2] = optional count
3984 if ((goi.argc == 2) || (goi.argc == 3)) {
3985 Jim_SetResult_sprintf(goi.interp, "expected: %s ADDR [COUNT]", n->name);
3988 e = Jim_GetOpt_Wide(&goi, &a);
3993 e = Jim_GetOpt_Wide(&goi, &c);
4000 b = 1; /* shut up gcc */
4013 /* convert to "bytes" */
4015 /* count is now in 'BYTES' */
4021 e = target_read_memory(target, a, b, y / b, target_buf);
4022 if (e != ERROR_OK) {
4023 Jim_SetResult_sprintf(interp, "error reading target @ 0x%08lx", (int)(a));
4027 Jim_fprintf(interp, interp->cookie_stdout, "0x%08x ", (int)(a));
4030 for (x = 0 ; (x < 16) && (x < y) ; x += 4) {
4031 z = target_buffer_get_u32(target, &(target_buf[ x * 4 ]));
4032 Jim_fprintf(interp, interp->cookie_stdout, "%08x ", (int)(z));
4034 for (; (x < 16) ; x += 4) {
4035 Jim_fprintf(interp, interp->cookie_stdout, " ");
4039 for (x = 0 ; (x < 16) && (x < y) ; x += 2) {
4040 z = target_buffer_get_u16(target, &(target_buf[ x * 2 ]));
4041 Jim_fprintf(interp, interp->cookie_stdout, "%04x ", (int)(z));
4043 for (; (x < 16) ; x += 2) {
4044 Jim_fprintf(interp, interp->cookie_stdout, " ");
4049 for (x = 0 ; (x < 16) && (x < y) ; x += 1) {
4050 z = target_buffer_get_u8(target, &(target_buf[ x * 4 ]));
4051 Jim_fprintf(interp, interp->cookie_stdout, "%02x ", (int)(z));
4053 for (; (x < 16) ; x += 1) {
4054 Jim_fprintf(interp, interp->cookie_stdout, " ");
4058 /* ascii-ify the bytes */
4059 for (x = 0 ; x < y ; x++) {
4060 if ((target_buf[x] >= 0x20) &&
4061 (target_buf[x] <= 0x7e)) {
4065 target_buf[x] = '.';
4070 target_buf[x] = ' ';
4075 /* print - with a newline */
4076 Jim_fprintf(interp, interp->cookie_stdout, "%s\n", target_buf);
4082 case TS_CMD_MEM2ARRAY:
4083 return target_mem2array(goi.interp, target, goi.argc, goi.argv);
4085 case TS_CMD_ARRAY2MEM:
4086 return target_array2mem(goi.interp, target, goi.argc, goi.argv);
4088 case TS_CMD_EXAMINE:
4090 Jim_WrongNumArgs(goi.interp, 2, argv, "[no parameters]");
4093 if (!target->tap->enabled)
4094 goto err_tap_disabled;
4095 e = target->type->examine(target);
4096 if (e != ERROR_OK) {
4097 Jim_SetResult_sprintf(interp, "examine-fails: %d", e);
4103 Jim_WrongNumArgs(goi.interp, 2, argv, "[no parameters]");
4106 if (!target->tap->enabled)
4107 goto err_tap_disabled;
4108 if (!(target_was_examined(target))) {
4109 e = ERROR_TARGET_NOT_EXAMINED;
4111 e = target->type->poll(target);
4113 if (e != ERROR_OK) {
4114 Jim_SetResult_sprintf(interp, "poll-fails: %d", e);
4121 if (goi.argc != 2) {
4122 Jim_WrongNumArgs(interp, 2, argv,
4123 "([tT]|[fF]|assert|deassert) BOOL");
4126 e = Jim_GetOpt_Nvp(&goi, nvp_assert, &n);
4128 Jim_GetOpt_NvpUnknown(&goi, nvp_assert, 1);
4131 /* the halt or not param */
4132 e = Jim_GetOpt_Wide(&goi, &a);
4136 if (!target->tap->enabled)
4137 goto err_tap_disabled;
4138 if (!target->type->assert_reset
4139 || !target->type->deassert_reset) {
4140 Jim_SetResult_sprintf(interp,
4141 "No target-specific reset for %s",
4145 /* determine if we should halt or not. */
4146 target->reset_halt = !!a;
4147 /* When this happens - all workareas are invalid. */
4148 target_free_all_working_areas_restore(target, 0);
4151 if (n->value == NVP_ASSERT) {
4152 e = target->type->assert_reset(target);
4154 e = target->type->deassert_reset(target);
4156 return (e == ERROR_OK) ? JIM_OK : JIM_ERR;
4159 Jim_WrongNumArgs(goi.interp, 0, argv, "halt [no parameters]");
4162 if (!target->tap->enabled)
4163 goto err_tap_disabled;
4164 e = target->type->halt(target);
4165 return (e == ERROR_OK) ? JIM_OK : JIM_ERR;
4166 case TS_CMD_WAITSTATE:
4167 /* params: <name> statename timeoutmsecs */
4168 if (goi.argc != 2) {
4169 Jim_SetResult_sprintf(goi.interp, "%s STATENAME TIMEOUTMSECS", n->name);
4172 e = Jim_GetOpt_Nvp(&goi, nvp_target_state, &n);
4174 Jim_GetOpt_NvpUnknown(&goi, nvp_target_state,1);
4177 e = Jim_GetOpt_Wide(&goi, &a);
4181 if (!target->tap->enabled)
4182 goto err_tap_disabled;
4183 e = target_wait_state(target, n->value, a);
4184 if (e != ERROR_OK) {
4185 Jim_SetResult_sprintf(goi.interp,
4186 "target: %s wait %s fails (%d) %s",
4189 e, target_strerror_safe(e));
4194 case TS_CMD_EVENTLIST:
4195 /* List for human, Events defined for this target.
4196 * scripts/programs should use 'name cget -event NAME'
4199 struct target_event_action *teap;
4200 teap = target->event_action;
4201 command_print(cmd_ctx, "Event actions for target (%d) %s\n",
4202 target->target_number,
4204 command_print(cmd_ctx, "%-25s | Body", "Event");
4205 command_print(cmd_ctx, "------------------------- | ----------------------------------------");
4207 command_print(cmd_ctx,
4209 Jim_Nvp_value2name_simple(nvp_target_event, teap->event)->name,
4210 Jim_GetString(teap->body, NULL));
4213 command_print(cmd_ctx, "***END***");
4216 case TS_CMD_CURSTATE:
4217 if (goi.argc != 0) {
4218 Jim_WrongNumArgs(goi.interp, 0, argv, "[no parameters]");
4221 Jim_SetResultString(goi.interp,
4222 target_state_name( target ),
4225 case TS_CMD_INVOKE_EVENT:
4226 if (goi.argc != 1) {
4227 Jim_SetResult_sprintf(goi.interp, "%s ?EVENTNAME?",n->name);
4230 e = Jim_GetOpt_Nvp(&goi, nvp_target_event, &n);
4232 Jim_GetOpt_NvpUnknown(&goi, nvp_target_event, 1);
4235 target_handle_event(target, n->value);
4241 Jim_SetResult_sprintf(interp, "[TAP is disabled]");
4245 static int target_create(Jim_GetOptInfo *goi)
4253 struct target *target;
4254 struct command_context *cmd_ctx;
4256 cmd_ctx = Jim_GetAssocData(goi->interp, "context");
4257 if (goi->argc < 3) {
4258 Jim_WrongNumArgs(goi->interp, 1, goi->argv, "?name? ?type? ..options...");
4263 Jim_GetOpt_Obj(goi, &new_cmd);
4264 /* does this command exist? */
4265 cmd = Jim_GetCommand(goi->interp, new_cmd, JIM_ERRMSG);
4267 cp = Jim_GetString(new_cmd, NULL);
4268 Jim_SetResult_sprintf(goi->interp, "Command/target: %s Exists", cp);
4273 e = Jim_GetOpt_String(goi, &cp2, NULL);
4275 /* now does target type exist */
4276 for (x = 0 ; target_types[x] ; x++) {
4277 if (0 == strcmp(cp, target_types[x]->name)) {
4282 if (target_types[x] == NULL) {
4283 Jim_SetResult_sprintf(goi->interp, "Unknown target type %s, try one of ", cp);
4284 for (x = 0 ; target_types[x] ; x++) {
4285 if (target_types[x + 1]) {
4286 Jim_AppendStrings(goi->interp,
4287 Jim_GetResult(goi->interp),
4288 target_types[x]->name,
4291 Jim_AppendStrings(goi->interp,
4292 Jim_GetResult(goi->interp),
4294 target_types[x]->name,NULL);
4301 target = calloc(1,sizeof(struct target));
4302 /* set target number */
4303 target->target_number = new_target_number();
4305 /* allocate memory for each unique target type */
4306 target->type = (struct target_type*)calloc(1,sizeof(struct target_type));
4308 memcpy(target->type, target_types[x], sizeof(struct target_type));
4310 /* will be set by "-endian" */
4311 target->endianness = TARGET_ENDIAN_UNKNOWN;
4313 target->working_area = 0x0;
4314 target->working_area_size = 0x0;
4315 target->working_areas = NULL;
4316 target->backup_working_area = 0;
4318 target->state = TARGET_UNKNOWN;
4319 target->debug_reason = DBG_REASON_UNDEFINED;
4320 target->reg_cache = NULL;
4321 target->breakpoints = NULL;
4322 target->watchpoints = NULL;
4323 target->next = NULL;
4324 target->arch_info = NULL;
4326 target->display = 1;
4328 target->halt_issued = false;
4330 /* initialize trace information */
4331 target->trace_info = malloc(sizeof(struct trace));
4332 target->trace_info->num_trace_points = 0;
4333 target->trace_info->trace_points_size = 0;
4334 target->trace_info->trace_points = NULL;
4335 target->trace_info->trace_history_size = 0;
4336 target->trace_info->trace_history = NULL;
4337 target->trace_info->trace_history_pos = 0;
4338 target->trace_info->trace_history_overflowed = 0;
4340 target->dbgmsg = NULL;
4341 target->dbg_msg_enabled = 0;
4343 target->endianness = TARGET_ENDIAN_UNKNOWN;
4345 /* Do the rest as "configure" options */
4346 goi->isconfigure = 1;
4347 e = target_configure(goi, target);
4349 if (target->tap == NULL)
4351 Jim_SetResultString(interp, "-chain-position required when creating target", -1);
4361 if (target->endianness == TARGET_ENDIAN_UNKNOWN) {
4362 /* default endian to little if not specified */
4363 target->endianness = TARGET_LITTLE_ENDIAN;
4366 /* incase variant is not set */
4367 if (!target->variant)
4368 target->variant = strdup("");
4370 cp = Jim_GetString(new_cmd, NULL);
4371 target->cmd_name = strdup(cp);
4373 /* create the target specific commands */
4374 if (target->type->commands) {
4375 e = register_commands(cmd_ctx, NULL, target->type->commands);
4377 LOG_ERROR("unable to register '%s' commands", cp);
4379 if (target->type->target_create) {
4380 (*(target->type->target_create))(target, goi->interp);
4383 /* append to end of list */
4385 struct target **tpp;
4386 tpp = &(all_targets);
4388 tpp = &((*tpp)->next);
4393 /* now - create the new target name command */
4394 const struct command_registration target_command = {
4396 .jim_handler = &tcl_target_func,
4397 .jim_handler_data = target,
4398 .help = "target command group",
4400 struct command *c = register_command(cmd_ctx, NULL, &target_command);
4401 return (NULL != c) ? ERROR_OK : ERROR_FAIL;
4404 static int jim_target(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
4408 struct command_context *cmd_ctx;
4409 struct target *target;
4412 /* TG = target generic */
4420 const char *target_cmds[] = {
4421 "create", "types", "names", "current", "number",
4423 NULL /* terminate */
4426 LOG_DEBUG("Target command params:");
4427 LOG_DEBUG("%s", Jim_Debug_ArgvString(interp, argc, argv));
4429 cmd_ctx = Jim_GetAssocData(interp, "context");
4431 Jim_GetOpt_Setup(&goi, interp, argc-1, argv + 1);
4433 if (goi.argc == 0) {
4434 Jim_WrongNumArgs(interp, 1, argv, "missing: command ...");
4438 /* Jim_GetOpt_Debug(&goi); */
4439 r = Jim_GetOpt_Enum(&goi, target_cmds, &x);
4446 Jim_Panic(goi.interp,"Why am I here?");
4448 case TG_CMD_CURRENT:
4449 if (goi.argc != 0) {
4450 Jim_WrongNumArgs(goi.interp, 1, goi.argv, "Too many parameters");
4453 Jim_SetResultString(goi.interp, get_current_target(cmd_ctx)->cmd_name, -1);
4456 if (goi.argc != 0) {
4457 Jim_WrongNumArgs(goi.interp, 1, goi.argv, "Too many parameters");
4460 Jim_SetResult(goi.interp, Jim_NewListObj(goi.interp, NULL, 0));
4461 for (x = 0 ; target_types[x] ; x++) {
4462 Jim_ListAppendElement(goi.interp,
4463 Jim_GetResult(goi.interp),
4464 Jim_NewStringObj(goi.interp, target_types[x]->name, -1));
4468 if (goi.argc != 0) {
4469 Jim_WrongNumArgs(goi.interp, 1, goi.argv, "Too many parameters");
4472 Jim_SetResult(goi.interp, Jim_NewListObj(goi.interp, NULL, 0));
4473 target = all_targets;
4475 Jim_ListAppendElement(goi.interp,
4476 Jim_GetResult(goi.interp),
4477 Jim_NewStringObj(goi.interp, target->cmd_name, -1));
4478 target = target->next;
4483 Jim_WrongNumArgs(goi.interp, goi.argc, goi.argv, "?name ... config options ...");
4486 return target_create(&goi);
4489 /* It's OK to remove this mechanism sometime after August 2010 or so */
4490 LOG_WARNING("don't use numbers as target identifiers; use names");
4491 if (goi.argc != 1) {
4492 Jim_SetResult_sprintf(goi.interp, "expected: target number ?NUMBER?");
4495 e = Jim_GetOpt_Wide(&goi, &w);
4499 for (x = 0, target = all_targets; target; target = target->next, x++) {
4500 if (target->target_number == w)
4503 if (target == NULL) {
4504 Jim_SetResult_sprintf(goi.interp,
4505 "Target: number %d does not exist", (int)(w));
4508 Jim_SetResultString(goi.interp, target->cmd_name, -1);
4511 if (goi.argc != 0) {
4512 Jim_WrongNumArgs(goi.interp, 0, goi.argv, "<no parameters>");
4515 for (x = 0, target = all_targets; target; target = target->next, x++)
4517 Jim_SetResult(goi.interp, Jim_NewIntObj(goi.interp, x));
4533 static int fastload_num;
4534 static struct FastLoad *fastload;
4536 static void free_fastload(void)
4538 if (fastload != NULL)
4541 for (i = 0; i < fastload_num; i++)
4543 if (fastload[i].data)
4544 free(fastload[i].data);
4554 COMMAND_HANDLER(handle_fast_load_image_command)
4558 uint32_t image_size;
4559 uint32_t min_address = 0;
4560 uint32_t max_address = 0xffffffff;
4565 int retval = CALL_COMMAND_HANDLER(parse_load_image_command_CMD_ARGV,
4566 &image, &min_address, &max_address);
4567 if (ERROR_OK != retval)
4570 struct duration bench;
4571 duration_start(&bench);
4573 if (image_open(&image, CMD_ARGV[0], (CMD_ARGC >= 3) ? CMD_ARGV[2] : NULL) != ERROR_OK)
4580 fastload_num = image.num_sections;
4581 fastload = (struct FastLoad *)malloc(sizeof(struct FastLoad)*image.num_sections);
4582 if (fastload == NULL)
4584 image_close(&image);
4587 memset(fastload, 0, sizeof(struct FastLoad)*image.num_sections);
4588 for (i = 0; i < image.num_sections; i++)
4590 buffer = malloc(image.sections[i].size);
4593 command_print(CMD_CTX, "error allocating buffer for section (%d bytes)",
4594 (int)(image.sections[i].size));
4598 if ((retval = image_read_section(&image, i, 0x0, image.sections[i].size, buffer, &buf_cnt)) != ERROR_OK)
4604 uint32_t offset = 0;
4605 uint32_t length = buf_cnt;
4608 /* DANGER!!! beware of unsigned comparision here!!! */
4610 if ((image.sections[i].base_address + buf_cnt >= min_address)&&
4611 (image.sections[i].base_address < max_address))
4613 if (image.sections[i].base_address < min_address)
4615 /* clip addresses below */
4616 offset += min_address-image.sections[i].base_address;
4620 if (image.sections[i].base_address + buf_cnt > max_address)
4622 length -= (image.sections[i].base_address + buf_cnt)-max_address;
4625 fastload[i].address = image.sections[i].base_address + offset;
4626 fastload[i].data = malloc(length);
4627 if (fastload[i].data == NULL)
4632 memcpy(fastload[i].data, buffer + offset, length);
4633 fastload[i].length = length;
4635 image_size += length;
4636 command_print(CMD_CTX, "%u bytes written at address 0x%8.8x",
4637 (unsigned int)length,
4638 ((unsigned int)(image.sections[i].base_address + offset)));
4644 if ((ERROR_OK == retval) && (duration_measure(&bench) == ERROR_OK))
4646 command_print(CMD_CTX, "Loaded %" PRIu32 " bytes "
4647 "in %fs (%0.3f kb/s)", image_size,
4648 duration_elapsed(&bench), duration_kbps(&bench, image_size));
4650 command_print(CMD_CTX,
4651 "WARNING: image has not been loaded to target!"
4652 "You can issue a 'fast_load' to finish loading.");
4655 image_close(&image);
4657 if (retval != ERROR_OK)
4665 COMMAND_HANDLER(handle_fast_load_command)
4668 return ERROR_COMMAND_SYNTAX_ERROR;
4669 if (fastload == NULL)
4671 LOG_ERROR("No image in memory");
4675 int ms = timeval_ms();
4677 int retval = ERROR_OK;
4678 for (i = 0; i < fastload_num;i++)
4680 struct target *target = get_current_target(CMD_CTX);
4681 command_print(CMD_CTX, "Write to 0x%08x, length 0x%08x",
4682 (unsigned int)(fastload[i].address),
4683 (unsigned int)(fastload[i].length));
4684 if (retval == ERROR_OK)
4686 retval = target_write_buffer(target, fastload[i].address, fastload[i].length, fastload[i].data);
4688 size += fastload[i].length;
4690 int after = timeval_ms();
4691 command_print(CMD_CTX, "Loaded image %f kBytes/s", (float)(size/1024.0)/((float)(after-ms)/1000.0));
4695 static int jim_mcrmrc(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
4697 struct command_context *context;
4698 struct target *target;
4701 context = Jim_GetAssocData(interp, "context");
4702 if (context == NULL) {
4703 LOG_ERROR("array2mem: no command context");
4706 target = get_current_target(context);
4707 if (target == NULL) {
4708 LOG_ERROR("array2mem: no current target");
4712 if ((argc < 6) || (argc > 7))
4726 e = Jim_GetLong(interp, argv[1], &l);
4732 e = Jim_GetLong(interp, argv[2], &l);
4738 e = Jim_GetLong(interp, argv[3], &l);
4744 e = Jim_GetLong(interp, argv[4], &l);
4750 e = Jim_GetLong(interp, argv[5], &l);
4760 e = Jim_GetLong(interp, argv[6], &l);
4766 retval = target_mcr(target, cpnum, op1, op2, CRn, CRm, value);
4767 if (retval != ERROR_OK)
4771 retval = target_mrc(target, cpnum, op1, op2, CRn, CRm, &value);
4772 if (retval != ERROR_OK)
4775 Jim_SetResult(interp, Jim_NewIntObj(interp, value));
4781 static const struct command_registration target_command_handlers[] = {
4784 .handler = &handle_targets_command,
4785 .mode = COMMAND_ANY,
4786 .help = "change current command line target (one parameter) "
4787 "or list targets (no parameters)",
4788 .usage = "[<new_current_target>]",
4792 .mode = COMMAND_CONFIG,
4793 .jim_handler = &jim_target,
4794 .help = "configure target",
4796 COMMAND_REGISTRATION_DONE
4799 int target_register_commands(struct command_context *cmd_ctx)
4801 return register_commands(cmd_ctx, NULL, target_command_handlers);
4804 static const struct command_registration target_exec_command_handlers[] = {
4806 .name = "fast_load_image",
4807 .handler = &handle_fast_load_image_command,
4808 .mode = COMMAND_ANY,
4809 .help = "Load image into memory, mainly for profiling purposes",
4810 .usage = "<file> <address> ['bin'|'ihex'|'elf'|'s19'] "
4811 "[min_address] [max_length]",
4814 .name = "fast_load",
4815 .handler = &handle_fast_load_command,
4816 .mode = COMMAND_ANY,
4817 .help = "loads active fast load image to current target "
4818 "- mainly for profiling purposes",
4822 .handler = &handle_profile_command,
4823 .mode = COMMAND_EXEC,
4824 .help = "profiling samples the CPU PC",
4826 /** @todo don't register virt2phys() unless target supports it */
4828 .name = "virt2phys",
4829 .handler = &handle_virt2phys_command,
4830 .mode = COMMAND_ANY,
4831 .help = "translate a virtual address into a physical address",
4836 .handler = &handle_reg_command,
4837 .mode = COMMAND_EXEC,
4838 .help = "display or set a register",
4843 .handler = &handle_poll_command,
4844 .mode = COMMAND_EXEC,
4845 .help = "poll target state",
4848 .name = "wait_halt",
4849 .handler = &handle_wait_halt_command,
4850 .mode = COMMAND_EXEC,
4851 .help = "wait for target halt",
4852 .usage = "[time (s)]",
4856 .handler = &handle_halt_command,
4857 .mode = COMMAND_EXEC,
4858 .help = "halt target",
4862 .handler = &handle_resume_command,
4863 .mode = COMMAND_EXEC,
4864 .help = "resume target",
4865 .usage = "[<address>]",
4869 .handler = &handle_reset_command,
4870 .mode = COMMAND_EXEC,
4871 .usage = "[run|halt|init]",
4872 .help = "Reset all targets into the specified mode."
4873 "Default reset mode is run, if not given.",
4876 .name = "soft_reset_halt",
4877 .handler = &handle_soft_reset_halt_command,
4878 .mode = COMMAND_EXEC,
4879 .help = "halt the target and do a soft reset",
4884 .handler = &handle_step_command,
4885 .mode = COMMAND_EXEC,
4886 .help = "step one instruction from current PC or [addr]",
4887 .usage = "[<address>]",
4892 .handler = &handle_md_command,
4893 .mode = COMMAND_EXEC,
4894 .help = "display memory words",
4895 .usage = "[phys] <addr> [count]",
4899 .handler = &handle_md_command,
4900 .mode = COMMAND_EXEC,
4901 .help = "display memory half-words",
4902 .usage = "[phys] <addr> [count]",
4906 .handler = &handle_md_command,
4907 .mode = COMMAND_EXEC,
4908 .help = "display memory bytes",
4909 .usage = "[phys] <addr> [count]",
4914 .handler = &handle_mw_command,
4915 .mode = COMMAND_EXEC,
4916 .help = "write memory word",
4917 .usage = "[phys] <addr> <value> [count]",
4921 .handler = &handle_mw_command,
4922 .mode = COMMAND_EXEC,
4923 .help = "write memory half-word",
4924 .usage = "[phys] <addr> <value> [count]",
4928 .handler = &handle_mw_command,
4929 .mode = COMMAND_EXEC,
4930 .help = "write memory byte",
4931 .usage = "[phys] <addr> <value> [count]",
4936 .handler = &handle_bp_command,
4937 .mode = COMMAND_EXEC,
4938 .help = "list or set breakpoint",
4939 .usage = "[<address> <length> [hw]]",
4943 .handler = &handle_rbp_command,
4944 .mode = COMMAND_EXEC,
4945 .help = "remove breakpoint",
4946 .usage = "<address>",
4951 .handler = &handle_wp_command,
4952 .mode = COMMAND_EXEC,
4953 .help = "list or set watchpoint",
4954 .usage = "[<address> <length> <r/w/a> [value] [mask]]",
4958 .handler = &handle_rwp_command,
4959 .mode = COMMAND_EXEC,
4960 .help = "remove watchpoint",
4961 .usage = "<address>",
4965 .name = "load_image",
4966 .handler = &handle_load_image_command,
4967 .mode = COMMAND_EXEC,
4968 .usage = "<file> <address> ['bin'|'ihex'|'elf'|'s19'] "
4969 "[min_address] [max_length]",
4972 .name = "dump_image",
4973 .handler = &handle_dump_image_command,
4974 .mode = COMMAND_EXEC,
4975 .usage = "<file> <address> <size>",
4978 .name = "verify_image",
4979 .handler = &handle_verify_image_command,
4980 .mode = COMMAND_EXEC,
4981 .usage = "<file> [offset] [type]",
4984 .name = "test_image",
4985 .handler = &handle_test_image_command,
4986 .mode = COMMAND_EXEC,
4987 .usage = "<file> [offset] [type]",
4990 .name = "ocd_mem2array",
4991 .mode = COMMAND_EXEC,
4992 .jim_handler = &jim_mem2array,
4993 .help = "read memory and return as a TCL array "
4994 "for script processing",
4995 .usage = "<arrayname> <width=32|16|8> <address> <count>",
4998 .name = "ocd_array2mem",
4999 .mode = COMMAND_EXEC,
5000 .jim_handler = &jim_array2mem,
5001 .help = "convert a TCL array to memory locations "
5002 "and write the values",
5003 .usage = "<arrayname> <width=32|16|8> <address> <count>",
5005 COMMAND_REGISTRATION_DONE
5007 int target_register_user_commands(struct command_context *cmd_ctx)
5009 int retval = ERROR_OK;
5010 if ((retval = target_request_register_commands(cmd_ctx)) != ERROR_OK)
5013 if ((retval = trace_register_commands(cmd_ctx)) != ERROR_OK)
5017 return register_commands(cmd_ctx, NULL, target_exec_command_handlers);