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;
69 struct target_type *target_types[] =
89 struct target *all_targets = NULL;
90 struct target_event_callback *target_event_callbacks = NULL;
91 struct target_timer_callback *target_timer_callbacks = NULL;
93 const Jim_Nvp nvp_assert[] = {
94 { .name = "assert", NVP_ASSERT },
95 { .name = "deassert", NVP_DEASSERT },
96 { .name = "T", NVP_ASSERT },
97 { .name = "F", NVP_DEASSERT },
98 { .name = "t", NVP_ASSERT },
99 { .name = "f", NVP_DEASSERT },
100 { .name = NULL, .value = -1 }
103 const Jim_Nvp nvp_error_target[] = {
104 { .value = ERROR_TARGET_INVALID, .name = "err-invalid" },
105 { .value = ERROR_TARGET_INIT_FAILED, .name = "err-init-failed" },
106 { .value = ERROR_TARGET_TIMEOUT, .name = "err-timeout" },
107 { .value = ERROR_TARGET_NOT_HALTED, .name = "err-not-halted" },
108 { .value = ERROR_TARGET_FAILURE, .name = "err-failure" },
109 { .value = ERROR_TARGET_UNALIGNED_ACCESS , .name = "err-unaligned-access" },
110 { .value = ERROR_TARGET_DATA_ABORT , .name = "err-data-abort" },
111 { .value = ERROR_TARGET_RESOURCE_NOT_AVAILABLE , .name = "err-resource-not-available" },
112 { .value = ERROR_TARGET_TRANSLATION_FAULT , .name = "err-translation-fault" },
113 { .value = ERROR_TARGET_NOT_RUNNING, .name = "err-not-running" },
114 { .value = ERROR_TARGET_NOT_EXAMINED, .name = "err-not-examined" },
115 { .value = -1, .name = NULL }
118 const char *target_strerror_safe(int err)
122 n = Jim_Nvp_value2name_simple(nvp_error_target, err);
123 if (n->name == NULL) {
130 static const Jim_Nvp nvp_target_event[] = {
131 { .value = TARGET_EVENT_OLD_gdb_program_config , .name = "old-gdb_program_config" },
132 { .value = TARGET_EVENT_OLD_pre_resume , .name = "old-pre_resume" },
134 { .value = TARGET_EVENT_GDB_HALT, .name = "gdb-halt" },
135 { .value = TARGET_EVENT_HALTED, .name = "halted" },
136 { .value = TARGET_EVENT_RESUMED, .name = "resumed" },
137 { .value = TARGET_EVENT_RESUME_START, .name = "resume-start" },
138 { .value = TARGET_EVENT_RESUME_END, .name = "resume-end" },
140 { .name = "gdb-start", .value = TARGET_EVENT_GDB_START },
141 { .name = "gdb-end", .value = TARGET_EVENT_GDB_END },
143 /* historical name */
145 { .value = TARGET_EVENT_RESET_START, .name = "reset-start" },
147 { .value = TARGET_EVENT_RESET_ASSERT_PRE, .name = "reset-assert-pre" },
148 { .value = TARGET_EVENT_RESET_ASSERT_POST, .name = "reset-assert-post" },
149 { .value = TARGET_EVENT_RESET_DEASSERT_PRE, .name = "reset-deassert-pre" },
150 { .value = TARGET_EVENT_RESET_DEASSERT_POST, .name = "reset-deassert-post" },
151 { .value = TARGET_EVENT_RESET_HALT_PRE, .name = "reset-halt-pre" },
152 { .value = TARGET_EVENT_RESET_HALT_POST, .name = "reset-halt-post" },
153 { .value = TARGET_EVENT_RESET_WAIT_PRE, .name = "reset-wait-pre" },
154 { .value = TARGET_EVENT_RESET_WAIT_POST, .name = "reset-wait-post" },
155 { .value = TARGET_EVENT_RESET_INIT , .name = "reset-init" },
156 { .value = TARGET_EVENT_RESET_END, .name = "reset-end" },
158 { .value = TARGET_EVENT_EXAMINE_START, .name = "examine-start" },
159 { .value = TARGET_EVENT_EXAMINE_END, .name = "examine-end" },
161 { .value = TARGET_EVENT_DEBUG_HALTED, .name = "debug-halted" },
162 { .value = TARGET_EVENT_DEBUG_RESUMED, .name = "debug-resumed" },
164 { .value = TARGET_EVENT_GDB_ATTACH, .name = "gdb-attach" },
165 { .value = TARGET_EVENT_GDB_DETACH, .name = "gdb-detach" },
167 { .value = TARGET_EVENT_GDB_FLASH_WRITE_START, .name = "gdb-flash-write-start" },
168 { .value = TARGET_EVENT_GDB_FLASH_WRITE_END , .name = "gdb-flash-write-end" },
170 { .value = TARGET_EVENT_GDB_FLASH_ERASE_START, .name = "gdb-flash-erase-start" },
171 { .value = TARGET_EVENT_GDB_FLASH_ERASE_END , .name = "gdb-flash-erase-end" },
173 { .value = TARGET_EVENT_RESUME_START, .name = "resume-start" },
174 { .value = TARGET_EVENT_RESUMED , .name = "resume-ok" },
175 { .value = TARGET_EVENT_RESUME_END , .name = "resume-end" },
177 { .name = NULL, .value = -1 }
180 const Jim_Nvp nvp_target_state[] = {
181 { .name = "unknown", .value = TARGET_UNKNOWN },
182 { .name = "running", .value = TARGET_RUNNING },
183 { .name = "halted", .value = TARGET_HALTED },
184 { .name = "reset", .value = TARGET_RESET },
185 { .name = "debug-running", .value = TARGET_DEBUG_RUNNING },
186 { .name = NULL, .value = -1 },
189 const Jim_Nvp nvp_target_debug_reason [] = {
190 { .name = "debug-request" , .value = DBG_REASON_DBGRQ },
191 { .name = "breakpoint" , .value = DBG_REASON_BREAKPOINT },
192 { .name = "watchpoint" , .value = DBG_REASON_WATCHPOINT },
193 { .name = "watchpoint-and-breakpoint", .value = DBG_REASON_WPTANDBKPT },
194 { .name = "single-step" , .value = DBG_REASON_SINGLESTEP },
195 { .name = "target-not-halted" , .value = DBG_REASON_NOTHALTED },
196 { .name = "undefined" , .value = DBG_REASON_UNDEFINED },
197 { .name = NULL, .value = -1 },
200 const Jim_Nvp nvp_target_endian[] = {
201 { .name = "big", .value = TARGET_BIG_ENDIAN },
202 { .name = "little", .value = TARGET_LITTLE_ENDIAN },
203 { .name = "be", .value = TARGET_BIG_ENDIAN },
204 { .name = "le", .value = TARGET_LITTLE_ENDIAN },
205 { .name = NULL, .value = -1 },
208 const Jim_Nvp nvp_reset_modes[] = {
209 { .name = "unknown", .value = RESET_UNKNOWN },
210 { .name = "run" , .value = RESET_RUN },
211 { .name = "halt" , .value = RESET_HALT },
212 { .name = "init" , .value = RESET_INIT },
213 { .name = NULL , .value = -1 },
217 target_state_name( struct target *t )
220 cp = Jim_Nvp_value2name_simple(nvp_target_state, t->state)->name;
222 LOG_ERROR("Invalid target state: %d", (int)(t->state));
223 cp = "(*BUG*unknown*BUG*)";
228 /* determine the number of the new target */
229 static int new_target_number(void)
234 /* number is 0 based */
238 if (x < t->target_number) {
239 x = t->target_number;
246 /* read a uint32_t from a buffer in target memory endianness */
247 uint32_t target_buffer_get_u32(struct target *target, const uint8_t *buffer)
249 if (target->endianness == TARGET_LITTLE_ENDIAN)
250 return le_to_h_u32(buffer);
252 return be_to_h_u32(buffer);
255 /* read a uint16_t from a buffer in target memory endianness */
256 uint16_t target_buffer_get_u16(struct target *target, const uint8_t *buffer)
258 if (target->endianness == TARGET_LITTLE_ENDIAN)
259 return le_to_h_u16(buffer);
261 return be_to_h_u16(buffer);
264 /* read a uint8_t from a buffer in target memory endianness */
265 uint8_t target_buffer_get_u8(struct target *target, const uint8_t *buffer)
267 return *buffer & 0x0ff;
270 /* write a uint32_t to a buffer in target memory endianness */
271 void target_buffer_set_u32(struct target *target, uint8_t *buffer, uint32_t value)
273 if (target->endianness == TARGET_LITTLE_ENDIAN)
274 h_u32_to_le(buffer, value);
276 h_u32_to_be(buffer, value);
279 /* write a uint16_t to a buffer in target memory endianness */
280 void target_buffer_set_u16(struct target *target, uint8_t *buffer, uint16_t value)
282 if (target->endianness == TARGET_LITTLE_ENDIAN)
283 h_u16_to_le(buffer, value);
285 h_u16_to_be(buffer, value);
288 /* write a uint8_t to a buffer in target memory endianness */
289 void target_buffer_set_u8(struct target *target, uint8_t *buffer, uint8_t value)
294 /* return a pointer to a configured target; id is name or number */
295 struct target *get_target(const char *id)
297 struct target *target;
299 /* try as tcltarget name */
300 for (target = all_targets; target; target = target->next) {
301 if (target->cmd_name == NULL)
303 if (strcmp(id, target->cmd_name) == 0)
307 /* It's OK to remove this fallback sometime after August 2010 or so */
309 /* no match, try as number */
311 if (parse_uint(id, &num) != ERROR_OK)
314 for (target = all_targets; target; target = target->next) {
315 if (target->target_number == (int)num) {
316 LOG_WARNING("use '%s' as target identifier, not '%u'",
317 target->cmd_name, num);
325 /* returns a pointer to the n-th configured target */
326 static struct target *get_target_by_num(int num)
328 struct target *target = all_targets;
331 if (target->target_number == num) {
334 target = target->next;
340 struct target* get_current_target(struct command_context *cmd_ctx)
342 struct target *target = get_target_by_num(cmd_ctx->current_target);
346 LOG_ERROR("BUG: current_target out of bounds");
353 int target_poll(struct target *target)
357 /* We can't poll until after examine */
358 if (!target_was_examined(target))
360 /* Fail silently lest we pollute the log */
364 retval = target->type->poll(target);
365 if (retval != ERROR_OK)
368 if (target->halt_issued)
370 if (target->state == TARGET_HALTED)
372 target->halt_issued = false;
375 long long t = timeval_ms() - target->halt_issued_time;
378 target->halt_issued = false;
379 LOG_INFO("Halt timed out, wake up GDB.");
380 target_call_event_callbacks(target, TARGET_EVENT_GDB_HALT);
388 int target_halt(struct target *target)
391 /* We can't poll until after examine */
392 if (!target_was_examined(target))
394 LOG_ERROR("Target not examined yet");
398 retval = target->type->halt(target);
399 if (retval != ERROR_OK)
402 target->halt_issued = true;
403 target->halt_issued_time = timeval_ms();
408 int target_resume(struct target *target, int current, uint32_t address, int handle_breakpoints, int debug_execution)
412 /* We can't poll until after examine */
413 if (!target_was_examined(target))
415 LOG_ERROR("Target not examined yet");
419 /* note that resume *must* be asynchronous. The CPU can halt before we poll. The CPU can
420 * even halt at the current PC as a result of a software breakpoint being inserted by (a bug?)
423 if ((retval = target->type->resume(target, current, address, handle_breakpoints, debug_execution)) != ERROR_OK)
429 int target_process_reset(struct command_context *cmd_ctx, enum target_reset_mode reset_mode)
434 n = Jim_Nvp_value2name_simple(nvp_reset_modes, reset_mode);
435 if (n->name == NULL) {
436 LOG_ERROR("invalid reset mode");
440 /* disable polling during reset to make reset event scripts
441 * more predictable, i.e. dr/irscan & pathmove in events will
442 * not have JTAG operations injected into the middle of a sequence.
444 bool save_poll = jtag_poll_get_enabled();
446 jtag_poll_set_enabled(false);
448 sprintf(buf, "ocd_process_reset %s", n->name);
449 retval = Jim_Eval(interp, buf);
451 jtag_poll_set_enabled(save_poll);
453 if (retval != JIM_OK) {
454 Jim_PrintErrorMessage(interp);
458 /* We want any events to be processed before the prompt */
459 retval = target_call_timer_callbacks_now();
464 static int identity_virt2phys(struct target *target,
465 uint32_t virtual, uint32_t *physical)
471 static int no_mmu(struct target *target, int *enabled)
477 static int default_examine(struct target *target)
479 target_set_examined(target);
483 int target_examine_one(struct target *target)
485 return target->type->examine(target);
488 static int jtag_enable_callback(enum jtag_event event, void *priv)
490 struct target *target = priv;
492 if (event != JTAG_TAP_EVENT_ENABLE || !target->tap->enabled)
495 jtag_unregister_event_callback(jtag_enable_callback, target);
496 return target_examine_one(target);
500 /* Targets that correctly implement init + examine, i.e.
501 * no communication with target during init:
505 int target_examine(void)
507 int retval = ERROR_OK;
508 struct target *target;
510 for (target = all_targets; target; target = target->next)
512 /* defer examination, but don't skip it */
513 if (!target->tap->enabled) {
514 jtag_register_event_callback(jtag_enable_callback,
518 if ((retval = target_examine_one(target)) != ERROR_OK)
523 const char *target_get_name(struct target *target)
525 return target->type->name;
528 static int target_write_memory_imp(struct target *target, uint32_t address, uint32_t size, uint32_t count, uint8_t *buffer)
530 if (!target_was_examined(target))
532 LOG_ERROR("Target not examined yet");
535 return target->type->write_memory_imp(target, address, size, count, buffer);
538 static int target_read_memory_imp(struct target *target, uint32_t address, uint32_t size, uint32_t count, uint8_t *buffer)
540 if (!target_was_examined(target))
542 LOG_ERROR("Target not examined yet");
545 return target->type->read_memory_imp(target, address, size, count, buffer);
548 static int target_soft_reset_halt_imp(struct target *target)
550 if (!target_was_examined(target))
552 LOG_ERROR("Target not examined yet");
555 if (!target->type->soft_reset_halt_imp) {
556 LOG_ERROR("Target %s does not support soft_reset_halt",
560 return target->type->soft_reset_halt_imp(target);
563 static int target_run_algorithm_imp(struct target *target, int num_mem_params, struct mem_param *mem_params, int num_reg_params, struct reg_param *reg_param, uint32_t entry_point, uint32_t exit_point, int timeout_ms, void *arch_info)
565 if (!target_was_examined(target))
567 LOG_ERROR("Target not examined yet");
570 return target->type->run_algorithm_imp(target, num_mem_params, mem_params, num_reg_params, reg_param, entry_point, exit_point, timeout_ms, arch_info);
573 int target_read_memory(struct target *target,
574 uint32_t address, uint32_t size, uint32_t count, uint8_t *buffer)
576 return target->type->read_memory(target, address, size, count, buffer);
579 int target_read_phys_memory(struct target *target,
580 uint32_t address, uint32_t size, uint32_t count, uint8_t *buffer)
582 return target->type->read_phys_memory(target, address, size, count, buffer);
585 int target_write_memory(struct target *target,
586 uint32_t address, uint32_t size, uint32_t count, uint8_t *buffer)
588 return target->type->write_memory(target, address, size, count, buffer);
591 int target_write_phys_memory(struct target *target,
592 uint32_t address, uint32_t size, uint32_t count, uint8_t *buffer)
594 return target->type->write_phys_memory(target, address, size, count, buffer);
597 int target_bulk_write_memory(struct target *target,
598 uint32_t address, uint32_t count, uint8_t *buffer)
600 return target->type->bulk_write_memory(target, address, count, buffer);
603 int target_add_breakpoint(struct target *target,
604 struct breakpoint *breakpoint)
606 return target->type->add_breakpoint(target, breakpoint);
608 int target_remove_breakpoint(struct target *target,
609 struct breakpoint *breakpoint)
611 return target->type->remove_breakpoint(target, breakpoint);
614 int target_add_watchpoint(struct target *target,
615 struct watchpoint *watchpoint)
617 return target->type->add_watchpoint(target, watchpoint);
619 int target_remove_watchpoint(struct target *target,
620 struct watchpoint *watchpoint)
622 return target->type->remove_watchpoint(target, watchpoint);
625 int target_get_gdb_reg_list(struct target *target,
626 struct reg **reg_list[], int *reg_list_size)
628 return target->type->get_gdb_reg_list(target, reg_list, reg_list_size);
630 int target_step(struct target *target,
631 int current, uint32_t address, int handle_breakpoints)
633 return target->type->step(target, current, address, handle_breakpoints);
637 int target_run_algorithm(struct target *target,
638 int num_mem_params, struct mem_param *mem_params,
639 int num_reg_params, struct reg_param *reg_param,
640 uint32_t entry_point, uint32_t exit_point,
641 int timeout_ms, void *arch_info)
643 return target->type->run_algorithm(target,
644 num_mem_params, mem_params, num_reg_params, reg_param,
645 entry_point, exit_point, timeout_ms, arch_info);
649 * Reset the @c examined flag for the given target.
650 * Pure paranoia -- targets are zeroed on allocation.
652 static void target_reset_examined(struct target *target)
654 target->examined = false;
659 static int default_mrc(struct target *target, int cpnum, uint32_t op1, uint32_t op2, uint32_t CRn, uint32_t CRm, uint32_t *value)
661 LOG_ERROR("Not implemented: %s", __func__);
665 static int default_mcr(struct target *target, int cpnum, uint32_t op1, uint32_t op2, uint32_t CRn, uint32_t CRm, uint32_t value)
667 LOG_ERROR("Not implemented: %s", __func__);
671 static int arm_cp_check(struct target *target, int cpnum, uint32_t op1, uint32_t op2, uint32_t CRn, uint32_t CRm)
674 if (!target_was_examined(target))
676 LOG_ERROR("Target not examined yet");
680 if ((cpnum <0) || (cpnum > 15))
682 LOG_ERROR("Illegal co-processor %d", cpnum);
688 LOG_ERROR("Illegal op1");
694 LOG_ERROR("Illegal op2");
700 LOG_ERROR("Illegal CRn");
706 LOG_ERROR("Illegal CRm");
713 int target_mrc(struct target *target, int cpnum, uint32_t op1, uint32_t op2, uint32_t CRn, uint32_t CRm, uint32_t *value)
717 retval = arm_cp_check(target, cpnum, op1, op2, CRn, CRm);
718 if (retval != ERROR_OK)
721 return target->type->mrc(target, cpnum, op1, op2, CRn, CRm, value);
724 int target_mcr(struct target *target, int cpnum, uint32_t op1, uint32_t op2, uint32_t CRn, uint32_t CRm, uint32_t value)
728 retval = arm_cp_check(target, cpnum, op1, op2, CRn, CRm);
729 if (retval != ERROR_OK)
732 return target->type->mcr(target, cpnum, op1, op2, CRn, CRm, value);
736 err_read_phys_memory(struct target *target, uint32_t address,
737 uint32_t size, uint32_t count, uint8_t *buffer)
739 LOG_ERROR("Not implemented: %s", __func__);
744 err_write_phys_memory(struct target *target, uint32_t address,
745 uint32_t size, uint32_t count, uint8_t *buffer)
747 LOG_ERROR("Not implemented: %s", __func__);
751 int target_init(struct command_context *cmd_ctx)
753 struct target *target;
756 for (target = all_targets; target; target = target->next) {
757 struct target_type *type = target->type;
759 target_reset_examined(target);
760 if (target->type->examine == NULL)
762 target->type->examine = default_examine;
765 if ((retval = target->type->init_target(cmd_ctx, target)) != ERROR_OK)
767 LOG_ERROR("target '%s' init failed", target_get_name(target));
772 * @todo MCR/MRC are ARM-specific; don't require them in
773 * all targets, or for ARMs without coprocessors.
775 if (target->type->mcr == NULL)
777 target->type->mcr = default_mcr;
780 const struct command_registration mcr_cmd = {
782 .mode = COMMAND_EXEC,
783 .jim_handler = &jim_mcrmrc,
784 .help = "write coprocessor",
785 .usage = "<cpnum> <op1> <op2> <CRn> <CRm> <value>",
787 register_command(cmd_ctx, NULL, &mcr_cmd);
790 if (target->type->mrc == NULL)
792 target->type->mrc = default_mrc;
795 const struct command_registration mrc_cmd = {
797 .jim_handler = &jim_mcrmrc,
798 .help = "read coprocessor",
799 .usage = "<cpnum> <op1> <op2> <CRn> <CRm>",
801 register_command(cmd_ctx, NULL, &mrc_cmd);
806 * @todo get rid of those *memory_imp() methods, now that all
807 * callers are using target_*_memory() accessors ... and make
808 * sure the "physical" paths handle the same issues.
811 /* a non-invasive way(in terms of patches) to add some code that
812 * runs before the type->write/read_memory implementation
814 target->type->write_memory_imp = target->type->write_memory;
815 target->type->write_memory = target_write_memory_imp;
816 target->type->read_memory_imp = target->type->read_memory;
817 target->type->read_memory = target_read_memory_imp;
818 target->type->soft_reset_halt_imp = target->type->soft_reset_halt;
819 target->type->soft_reset_halt = target_soft_reset_halt_imp;
820 target->type->run_algorithm_imp = target->type->run_algorithm;
821 target->type->run_algorithm = target_run_algorithm_imp;
823 /* Sanity-check MMU support ... stub in what we must, to help
824 * implement it in stages, but warn if we need to do so.
827 if (type->write_phys_memory == NULL) {
828 LOG_ERROR("type '%s' is missing %s",
830 "write_phys_memory");
831 type->write_phys_memory = err_write_phys_memory;
833 if (type->read_phys_memory == NULL) {
834 LOG_ERROR("type '%s' is missing %s",
837 type->read_phys_memory = err_read_phys_memory;
839 if (type->virt2phys == NULL) {
840 LOG_ERROR("type '%s' is missing %s",
843 type->virt2phys = identity_virt2phys;
846 /* Make sure no-MMU targets all behave the same: make no
847 * distinction between physical and virtual addresses, and
848 * ensure that virt2phys() is always an identity mapping.
851 if (type->write_phys_memory
852 || type->read_phys_memory
854 LOG_WARNING("type '%s' has broken MMU hooks",
858 type->write_phys_memory = type->write_memory;
859 type->read_phys_memory = type->read_memory;
860 type->virt2phys = identity_virt2phys;
866 if ((retval = target_register_user_commands(cmd_ctx)) != ERROR_OK)
868 if ((retval = target_register_timer_callback(handle_target, 100, 1, NULL)) != ERROR_OK)
875 int target_register_event_callback(int (*callback)(struct target *target, enum target_event event, void *priv), void *priv)
877 struct target_event_callback **callbacks_p = &target_event_callbacks;
879 if (callback == NULL)
881 return ERROR_INVALID_ARGUMENTS;
886 while ((*callbacks_p)->next)
887 callbacks_p = &((*callbacks_p)->next);
888 callbacks_p = &((*callbacks_p)->next);
891 (*callbacks_p) = malloc(sizeof(struct target_event_callback));
892 (*callbacks_p)->callback = callback;
893 (*callbacks_p)->priv = priv;
894 (*callbacks_p)->next = NULL;
899 int target_register_timer_callback(int (*callback)(void *priv), int time_ms, int periodic, void *priv)
901 struct target_timer_callback **callbacks_p = &target_timer_callbacks;
904 if (callback == NULL)
906 return ERROR_INVALID_ARGUMENTS;
911 while ((*callbacks_p)->next)
912 callbacks_p = &((*callbacks_p)->next);
913 callbacks_p = &((*callbacks_p)->next);
916 (*callbacks_p) = malloc(sizeof(struct target_timer_callback));
917 (*callbacks_p)->callback = callback;
918 (*callbacks_p)->periodic = periodic;
919 (*callbacks_p)->time_ms = time_ms;
921 gettimeofday(&now, NULL);
922 (*callbacks_p)->when.tv_usec = now.tv_usec + (time_ms % 1000) * 1000;
923 time_ms -= (time_ms % 1000);
924 (*callbacks_p)->when.tv_sec = now.tv_sec + (time_ms / 1000);
925 if ((*callbacks_p)->when.tv_usec > 1000000)
927 (*callbacks_p)->when.tv_usec = (*callbacks_p)->when.tv_usec - 1000000;
928 (*callbacks_p)->when.tv_sec += 1;
931 (*callbacks_p)->priv = priv;
932 (*callbacks_p)->next = NULL;
937 int target_unregister_event_callback(int (*callback)(struct target *target, enum target_event event, void *priv), void *priv)
939 struct target_event_callback **p = &target_event_callbacks;
940 struct target_event_callback *c = target_event_callbacks;
942 if (callback == NULL)
944 return ERROR_INVALID_ARGUMENTS;
949 struct target_event_callback *next = c->next;
950 if ((c->callback == callback) && (c->priv == priv))
964 int target_unregister_timer_callback(int (*callback)(void *priv), void *priv)
966 struct target_timer_callback **p = &target_timer_callbacks;
967 struct target_timer_callback *c = target_timer_callbacks;
969 if (callback == NULL)
971 return ERROR_INVALID_ARGUMENTS;
976 struct target_timer_callback *next = c->next;
977 if ((c->callback == callback) && (c->priv == priv))
991 int target_call_event_callbacks(struct target *target, enum target_event event)
993 struct target_event_callback *callback = target_event_callbacks;
994 struct target_event_callback *next_callback;
996 if (event == TARGET_EVENT_HALTED)
998 /* execute early halted first */
999 target_call_event_callbacks(target, TARGET_EVENT_GDB_HALT);
1002 LOG_DEBUG("target event %i (%s)",
1004 Jim_Nvp_value2name_simple(nvp_target_event, event)->name);
1006 target_handle_event(target, event);
1010 next_callback = callback->next;
1011 callback->callback(target, event, callback->priv);
1012 callback = next_callback;
1018 static int target_timer_callback_periodic_restart(
1019 struct target_timer_callback *cb, struct timeval *now)
1021 int time_ms = cb->time_ms;
1022 cb->when.tv_usec = now->tv_usec + (time_ms % 1000) * 1000;
1023 time_ms -= (time_ms % 1000);
1024 cb->when.tv_sec = now->tv_sec + time_ms / 1000;
1025 if (cb->when.tv_usec > 1000000)
1027 cb->when.tv_usec = cb->when.tv_usec - 1000000;
1028 cb->when.tv_sec += 1;
1033 static int target_call_timer_callback(struct target_timer_callback *cb,
1034 struct timeval *now)
1036 cb->callback(cb->priv);
1039 return target_timer_callback_periodic_restart(cb, now);
1041 return target_unregister_timer_callback(cb->callback, cb->priv);
1044 static int target_call_timer_callbacks_check_time(int checktime)
1049 gettimeofday(&now, NULL);
1051 struct target_timer_callback *callback = target_timer_callbacks;
1054 // cleaning up may unregister and free this callback
1055 struct target_timer_callback *next_callback = callback->next;
1057 bool call_it = callback->callback &&
1058 ((!checktime && callback->periodic) ||
1059 now.tv_sec > callback->when.tv_sec ||
1060 (now.tv_sec == callback->when.tv_sec &&
1061 now.tv_usec >= callback->when.tv_usec));
1065 int retval = target_call_timer_callback(callback, &now);
1066 if (retval != ERROR_OK)
1070 callback = next_callback;
1076 int target_call_timer_callbacks(void)
1078 return target_call_timer_callbacks_check_time(1);
1081 /* invoke periodic callbacks immediately */
1082 int target_call_timer_callbacks_now(void)
1084 return target_call_timer_callbacks_check_time(0);
1087 int target_alloc_working_area(struct target *target, uint32_t size, struct working_area **area)
1089 struct working_area *c = target->working_areas;
1090 struct working_area *new_wa = NULL;
1092 /* Reevaluate working area address based on MMU state*/
1093 if (target->working_areas == NULL)
1098 retval = target->type->mmu(target, &enabled);
1099 if (retval != ERROR_OK)
1105 if (target->working_area_phys_spec) {
1106 LOG_DEBUG("MMU disabled, using physical "
1107 "address for working memory 0x%08x",
1108 (unsigned)target->working_area_phys);
1109 target->working_area = target->working_area_phys;
1111 LOG_ERROR("No working memory available. "
1112 "Specify -work-area-phys to target.");
1113 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
1116 if (target->working_area_virt_spec) {
1117 LOG_DEBUG("MMU enabled, using virtual "
1118 "address for working memory 0x%08x",
1119 (unsigned)target->working_area_virt);
1120 target->working_area = target->working_area_virt;
1122 LOG_ERROR("No working memory available. "
1123 "Specify -work-area-virt to target.");
1124 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
1129 /* only allocate multiples of 4 byte */
1132 LOG_ERROR("BUG: code tried to allocate unaligned number of bytes (0x%08x), padding", ((unsigned)(size)));
1133 size = (size + 3) & (~3);
1136 /* see if there's already a matching working area */
1139 if ((c->free) && (c->size == size))
1147 /* if not, allocate a new one */
1150 struct working_area **p = &target->working_areas;
1151 uint32_t first_free = target->working_area;
1152 uint32_t free_size = target->working_area_size;
1154 c = target->working_areas;
1157 first_free += c->size;
1158 free_size -= c->size;
1163 if (free_size < size)
1165 LOG_WARNING("not enough working area available(requested %u, free %u)",
1166 (unsigned)(size), (unsigned)(free_size));
1167 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
1170 LOG_DEBUG("allocated new working area at address 0x%08x", (unsigned)first_free);
1172 new_wa = malloc(sizeof(struct working_area));
1173 new_wa->next = NULL;
1174 new_wa->size = size;
1175 new_wa->address = first_free;
1177 if (target->backup_working_area)
1180 new_wa->backup = malloc(new_wa->size);
1181 if ((retval = target_read_memory(target, new_wa->address, 4, new_wa->size / 4, new_wa->backup)) != ERROR_OK)
1183 free(new_wa->backup);
1190 new_wa->backup = NULL;
1193 /* put new entry in list */
1197 /* mark as used, and return the new (reused) area */
1202 new_wa->user = area;
1207 int target_free_working_area_restore(struct target *target, struct working_area *area, int restore)
1212 if (restore && target->backup_working_area)
1215 if ((retval = target_write_memory(target, area->address, 4, area->size / 4, area->backup)) != ERROR_OK)
1221 /* mark user pointer invalid */
1228 int target_free_working_area(struct target *target, struct working_area *area)
1230 return target_free_working_area_restore(target, area, 1);
1233 /* free resources and restore memory, if restoring memory fails,
1234 * free up resources anyway
1236 void target_free_all_working_areas_restore(struct target *target, int restore)
1238 struct working_area *c = target->working_areas;
1242 struct working_area *next = c->next;
1243 target_free_working_area_restore(target, c, restore);
1253 target->working_areas = NULL;
1256 void target_free_all_working_areas(struct target *target)
1258 target_free_all_working_areas_restore(target, 1);
1261 int target_arch_state(struct target *target)
1266 LOG_USER("No target has been configured");
1270 LOG_USER("target state: %s", target_state_name( target ));
1272 if (target->state != TARGET_HALTED)
1275 retval = target->type->arch_state(target);
1279 /* Single aligned words are guaranteed to use 16 or 32 bit access
1280 * mode respectively, otherwise data is handled as quickly as
1283 int target_write_buffer(struct target *target, uint32_t address, uint32_t size, uint8_t *buffer)
1286 LOG_DEBUG("writing buffer of %i byte at 0x%8.8x",
1287 (int)size, (unsigned)address);
1289 if (!target_was_examined(target))
1291 LOG_ERROR("Target not examined yet");
1299 if ((address + size - 1) < address)
1301 /* GDB can request this when e.g. PC is 0xfffffffc*/
1302 LOG_ERROR("address + size wrapped(0x%08x, 0x%08x)",
1308 if (((address % 2) == 0) && (size == 2))
1310 return target_write_memory(target, address, 2, 1, buffer);
1313 /* handle unaligned head bytes */
1316 uint32_t unaligned = 4 - (address % 4);
1318 if (unaligned > size)
1321 if ((retval = target_write_memory(target, address, 1, unaligned, buffer)) != ERROR_OK)
1324 buffer += unaligned;
1325 address += unaligned;
1329 /* handle aligned words */
1332 int aligned = size - (size % 4);
1334 /* use bulk writes above a certain limit. This may have to be changed */
1337 if ((retval = target->type->bulk_write_memory(target, address, aligned / 4, buffer)) != ERROR_OK)
1342 if ((retval = target_write_memory(target, address, 4, aligned / 4, buffer)) != ERROR_OK)
1351 /* handle tail writes of less than 4 bytes */
1354 if ((retval = target_write_memory(target, address, 1, size, buffer)) != ERROR_OK)
1361 /* Single aligned words are guaranteed to use 16 or 32 bit access
1362 * mode respectively, otherwise data is handled as quickly as
1365 int target_read_buffer(struct target *target, uint32_t address, uint32_t size, uint8_t *buffer)
1368 LOG_DEBUG("reading buffer of %i byte at 0x%8.8x",
1369 (int)size, (unsigned)address);
1371 if (!target_was_examined(target))
1373 LOG_ERROR("Target not examined yet");
1381 if ((address + size - 1) < address)
1383 /* GDB can request this when e.g. PC is 0xfffffffc*/
1384 LOG_ERROR("address + size wrapped(0x%08" PRIx32 ", 0x%08" PRIx32 ")",
1390 if (((address % 2) == 0) && (size == 2))
1392 return target_read_memory(target, address, 2, 1, buffer);
1395 /* handle unaligned head bytes */
1398 uint32_t unaligned = 4 - (address % 4);
1400 if (unaligned > size)
1403 if ((retval = target_read_memory(target, address, 1, unaligned, buffer)) != ERROR_OK)
1406 buffer += unaligned;
1407 address += unaligned;
1411 /* handle aligned words */
1414 int aligned = size - (size % 4);
1416 if ((retval = target_read_memory(target, address, 4, aligned / 4, buffer)) != ERROR_OK)
1424 /*prevent byte access when possible (avoid AHB access limitations in some cases)*/
1427 int aligned = size - (size%2);
1428 retval = target_read_memory(target, address, 2, aligned / 2, buffer);
1429 if (retval != ERROR_OK)
1436 /* handle tail writes of less than 4 bytes */
1439 if ((retval = target_read_memory(target, address, 1, size, buffer)) != ERROR_OK)
1446 int target_checksum_memory(struct target *target, uint32_t address, uint32_t size, uint32_t* crc)
1451 uint32_t checksum = 0;
1452 if (!target_was_examined(target))
1454 LOG_ERROR("Target not examined yet");
1458 if ((retval = target->type->checksum_memory(target, address,
1459 size, &checksum)) != ERROR_OK)
1461 buffer = malloc(size);
1464 LOG_ERROR("error allocating buffer for section (%d bytes)", (int)size);
1465 return ERROR_INVALID_ARGUMENTS;
1467 retval = target_read_buffer(target, address, size, buffer);
1468 if (retval != ERROR_OK)
1474 /* convert to target endianess */
1475 for (i = 0; i < (size/sizeof(uint32_t)); i++)
1477 uint32_t target_data;
1478 target_data = target_buffer_get_u32(target, &buffer[i*sizeof(uint32_t)]);
1479 target_buffer_set_u32(target, &buffer[i*sizeof(uint32_t)], target_data);
1482 retval = image_calculate_checksum(buffer, size, &checksum);
1491 int target_blank_check_memory(struct target *target, uint32_t address, uint32_t size, uint32_t* blank)
1494 if (!target_was_examined(target))
1496 LOG_ERROR("Target not examined yet");
1500 if (target->type->blank_check_memory == 0)
1501 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
1503 retval = target->type->blank_check_memory(target, address, size, blank);
1508 int target_read_u32(struct target *target, uint32_t address, uint32_t *value)
1510 uint8_t value_buf[4];
1511 if (!target_was_examined(target))
1513 LOG_ERROR("Target not examined yet");
1517 int retval = target_read_memory(target, address, 4, 1, value_buf);
1519 if (retval == ERROR_OK)
1521 *value = target_buffer_get_u32(target, value_buf);
1522 LOG_DEBUG("address: 0x%8.8" PRIx32 ", value: 0x%8.8" PRIx32 "",
1529 LOG_DEBUG("address: 0x%8.8" PRIx32 " failed",
1536 int target_read_u16(struct target *target, uint32_t address, uint16_t *value)
1538 uint8_t value_buf[2];
1539 if (!target_was_examined(target))
1541 LOG_ERROR("Target not examined yet");
1545 int retval = target_read_memory(target, address, 2, 1, value_buf);
1547 if (retval == ERROR_OK)
1549 *value = target_buffer_get_u16(target, value_buf);
1550 LOG_DEBUG("address: 0x%8.8" PRIx32 ", value: 0x%4.4x",
1557 LOG_DEBUG("address: 0x%8.8" PRIx32 " failed",
1564 int target_read_u8(struct target *target, uint32_t address, uint8_t *value)
1566 int retval = target_read_memory(target, address, 1, 1, value);
1567 if (!target_was_examined(target))
1569 LOG_ERROR("Target not examined yet");
1573 if (retval == ERROR_OK)
1575 LOG_DEBUG("address: 0x%8.8" PRIx32 ", value: 0x%2.2x",
1582 LOG_DEBUG("address: 0x%8.8" PRIx32 " failed",
1589 int target_write_u32(struct target *target, uint32_t address, uint32_t value)
1592 uint8_t value_buf[4];
1593 if (!target_was_examined(target))
1595 LOG_ERROR("Target not examined yet");
1599 LOG_DEBUG("address: 0x%8.8" PRIx32 ", value: 0x%8.8" PRIx32 "",
1603 target_buffer_set_u32(target, value_buf, value);
1604 if ((retval = target_write_memory(target, address, 4, 1, value_buf)) != ERROR_OK)
1606 LOG_DEBUG("failed: %i", retval);
1612 int target_write_u16(struct target *target, uint32_t address, uint16_t value)
1615 uint8_t value_buf[2];
1616 if (!target_was_examined(target))
1618 LOG_ERROR("Target not examined yet");
1622 LOG_DEBUG("address: 0x%8.8" PRIx32 ", value: 0x%8.8x",
1626 target_buffer_set_u16(target, value_buf, value);
1627 if ((retval = target_write_memory(target, address, 2, 1, value_buf)) != ERROR_OK)
1629 LOG_DEBUG("failed: %i", retval);
1635 int target_write_u8(struct target *target, uint32_t address, uint8_t value)
1638 if (!target_was_examined(target))
1640 LOG_ERROR("Target not examined yet");
1644 LOG_DEBUG("address: 0x%8.8" PRIx32 ", value: 0x%2.2x",
1647 if ((retval = target_write_memory(target, address, 1, 1, &value)) != ERROR_OK)
1649 LOG_DEBUG("failed: %i", retval);
1655 COMMAND_HANDLER(handle_targets_command)
1657 struct target *target = all_targets;
1661 target = get_target(CMD_ARGV[0]);
1662 if (target == NULL) {
1663 command_print(CMD_CTX,"Target: %s is unknown, try one of:\n", CMD_ARGV[0]);
1666 if (!target->tap->enabled) {
1667 command_print(CMD_CTX,"Target: TAP %s is disabled, "
1668 "can't be the current target\n",
1669 target->tap->dotted_name);
1673 CMD_CTX->current_target = target->target_number;
1678 target = all_targets;
1679 command_print(CMD_CTX, " TargetName Type Endian TapName State ");
1680 command_print(CMD_CTX, "-- ------------------ ---------- ------ ------------------ ------------");
1686 if (target->tap->enabled)
1687 state = target_state_name( target );
1689 state = "tap-disabled";
1691 if (CMD_CTX->current_target == target->target_number)
1694 /* keep columns lined up to match the headers above */
1695 command_print(CMD_CTX, "%2d%c %-18s %-10s %-6s %-18s %s",
1696 target->target_number,
1699 target_get_name(target),
1700 Jim_Nvp_value2name_simple(nvp_target_endian,
1701 target->endianness)->name,
1702 target->tap->dotted_name,
1704 target = target->next;
1710 /* every 300ms we check for reset & powerdropout and issue a "reset halt" if so. */
1712 static int powerDropout;
1713 static int srstAsserted;
1715 static int runPowerRestore;
1716 static int runPowerDropout;
1717 static int runSrstAsserted;
1718 static int runSrstDeasserted;
1720 static int sense_handler(void)
1722 static int prevSrstAsserted = 0;
1723 static int prevPowerdropout = 0;
1726 if ((retval = jtag_power_dropout(&powerDropout)) != ERROR_OK)
1730 powerRestored = prevPowerdropout && !powerDropout;
1733 runPowerRestore = 1;
1736 long long current = timeval_ms();
1737 static long long lastPower = 0;
1738 int waitMore = lastPower + 2000 > current;
1739 if (powerDropout && !waitMore)
1741 runPowerDropout = 1;
1742 lastPower = current;
1745 if ((retval = jtag_srst_asserted(&srstAsserted)) != ERROR_OK)
1749 srstDeasserted = prevSrstAsserted && !srstAsserted;
1751 static long long lastSrst = 0;
1752 waitMore = lastSrst + 2000 > current;
1753 if (srstDeasserted && !waitMore)
1755 runSrstDeasserted = 1;
1759 if (!prevSrstAsserted && srstAsserted)
1761 runSrstAsserted = 1;
1764 prevSrstAsserted = srstAsserted;
1765 prevPowerdropout = powerDropout;
1767 if (srstDeasserted || powerRestored)
1769 /* Other than logging the event we can't do anything here.
1770 * Issuing a reset is a particularly bad idea as we might
1771 * be inside a reset already.
1778 static void target_call_event_callbacks_all(enum target_event e) {
1779 struct target *target;
1780 target = all_targets;
1782 target_call_event_callbacks(target, e);
1783 target = target->next;
1787 /* process target state changes */
1788 int handle_target(void *priv)
1790 int retval = ERROR_OK;
1792 /* we do not want to recurse here... */
1793 static int recursive = 0;
1798 /* danger! running these procedures can trigger srst assertions and power dropouts.
1799 * We need to avoid an infinite loop/recursion here and we do that by
1800 * clearing the flags after running these events.
1802 int did_something = 0;
1803 if (runSrstAsserted)
1805 target_call_event_callbacks_all(TARGET_EVENT_GDB_HALT);
1806 Jim_Eval(interp, "srst_asserted");
1809 if (runSrstDeasserted)
1811 Jim_Eval(interp, "srst_deasserted");
1814 if (runPowerDropout)
1816 target_call_event_callbacks_all(TARGET_EVENT_GDB_HALT);
1817 Jim_Eval(interp, "power_dropout");
1820 if (runPowerRestore)
1822 Jim_Eval(interp, "power_restore");
1828 /* clear detect flags */
1832 /* clear action flags */
1834 runSrstAsserted = 0;
1835 runSrstDeasserted = 0;
1836 runPowerRestore = 0;
1837 runPowerDropout = 0;
1842 /* Poll targets for state changes unless that's globally disabled.
1843 * Skip targets that are currently disabled.
1845 for (struct target *target = all_targets;
1846 is_jtag_poll_safe() && target;
1847 target = target->next)
1849 if (!target->tap->enabled)
1852 /* only poll target if we've got power and srst isn't asserted */
1853 if (!powerDropout && !srstAsserted)
1855 /* polling may fail silently until the target has been examined */
1856 if ((retval = target_poll(target)) != ERROR_OK)
1858 target_call_event_callbacks(target, TARGET_EVENT_GDB_HALT);
1867 COMMAND_HANDLER(handle_reg_command)
1869 struct target *target;
1870 struct reg *reg = NULL;
1876 target = get_current_target(CMD_CTX);
1878 /* list all available registers for the current target */
1881 struct reg_cache *cache = target->reg_cache;
1888 command_print(CMD_CTX, "===== %s", cache->name);
1890 for (i = 0, reg = cache->reg_list;
1891 i < cache->num_regs;
1892 i++, reg++, count++)
1894 /* only print cached values if they are valid */
1896 value = buf_to_str(reg->value,
1898 command_print(CMD_CTX,
1899 "(%i) %s (/%" PRIu32 "): 0x%s%s",
1907 command_print(CMD_CTX, "(%i) %s (/%" PRIu32 ")",
1912 cache = cache->next;
1918 /* access a single register by its ordinal number */
1919 if ((CMD_ARGV[0][0] >= '0') && (CMD_ARGV[0][0] <= '9'))
1922 COMMAND_PARSE_NUMBER(uint, CMD_ARGV[0], num);
1924 struct reg_cache *cache = target->reg_cache;
1929 for (i = 0; i < cache->num_regs; i++)
1933 reg = &cache->reg_list[i];
1939 cache = cache->next;
1944 command_print(CMD_CTX, "%i is out of bounds, the current target has only %i registers (0 - %i)", num, count, count - 1);
1947 } else /* access a single register by its name */
1949 reg = register_get_by_name(target->reg_cache, CMD_ARGV[0], 1);
1953 command_print(CMD_CTX, "register %s not found in current target", CMD_ARGV[0]);
1958 /* display a register */
1959 if ((CMD_ARGC == 1) || ((CMD_ARGC == 2) && !((CMD_ARGV[1][0] >= '0') && (CMD_ARGV[1][0] <= '9'))))
1961 if ((CMD_ARGC == 2) && (strcmp(CMD_ARGV[1], "force") == 0))
1964 if (reg->valid == 0)
1966 reg->type->get(reg);
1968 value = buf_to_str(reg->value, reg->size, 16);
1969 command_print(CMD_CTX, "%s (/%i): 0x%s", reg->name, (int)(reg->size), value);
1974 /* set register value */
1977 uint8_t *buf = malloc(DIV_ROUND_UP(reg->size, 8));
1978 str_to_buf(CMD_ARGV[1], strlen(CMD_ARGV[1]), buf, reg->size, 0);
1980 reg->type->set(reg, buf);
1982 value = buf_to_str(reg->value, reg->size, 16);
1983 command_print(CMD_CTX, "%s (/%i): 0x%s", reg->name, (int)(reg->size), value);
1991 command_print(CMD_CTX, "usage: reg <#|name> [value]");
1996 COMMAND_HANDLER(handle_poll_command)
1998 int retval = ERROR_OK;
1999 struct target *target = get_current_target(CMD_CTX);
2003 command_print(CMD_CTX, "background polling: %s",
2004 jtag_poll_get_enabled() ? "on" : "off");
2005 command_print(CMD_CTX, "TAP: %s (%s)",
2006 target->tap->dotted_name,
2007 target->tap->enabled ? "enabled" : "disabled");
2008 if (!target->tap->enabled)
2010 if ((retval = target_poll(target)) != ERROR_OK)
2012 if ((retval = target_arch_state(target)) != ERROR_OK)
2015 else if (CMD_ARGC == 1)
2018 COMMAND_PARSE_ON_OFF(CMD_ARGV[0], enable);
2019 jtag_poll_set_enabled(enable);
2023 return ERROR_COMMAND_SYNTAX_ERROR;
2029 COMMAND_HANDLER(handle_wait_halt_command)
2032 return ERROR_COMMAND_SYNTAX_ERROR;
2037 int retval = parse_uint(CMD_ARGV[0], &ms);
2038 if (ERROR_OK != retval)
2040 command_print(CMD_CTX, "usage: %s [seconds]", CMD_NAME);
2041 return ERROR_COMMAND_SYNTAX_ERROR;
2043 // convert seconds (given) to milliseconds (needed)
2047 struct target *target = get_current_target(CMD_CTX);
2048 return target_wait_state(target, TARGET_HALTED, ms);
2051 /* wait for target state to change. The trick here is to have a low
2052 * latency for short waits and not to suck up all the CPU time
2055 * After 500ms, keep_alive() is invoked
2057 int target_wait_state(struct target *target, enum target_state state, int ms)
2060 long long then = 0, cur;
2065 if ((retval = target_poll(target)) != ERROR_OK)
2067 if (target->state == state)
2075 then = timeval_ms();
2076 LOG_DEBUG("waiting for target %s...",
2077 Jim_Nvp_value2name_simple(nvp_target_state,state)->name);
2085 if ((cur-then) > ms)
2087 LOG_ERROR("timed out while waiting for target %s",
2088 Jim_Nvp_value2name_simple(nvp_target_state,state)->name);
2096 COMMAND_HANDLER(handle_halt_command)
2100 struct target *target = get_current_target(CMD_CTX);
2101 int retval = target_halt(target);
2102 if (ERROR_OK != retval)
2108 retval = parse_uint(CMD_ARGV[0], &wait);
2109 if (ERROR_OK != retval)
2110 return ERROR_COMMAND_SYNTAX_ERROR;
2115 return CALL_COMMAND_HANDLER(handle_wait_halt_command);
2118 COMMAND_HANDLER(handle_soft_reset_halt_command)
2120 struct target *target = get_current_target(CMD_CTX);
2122 LOG_USER("requesting target halt and executing a soft reset");
2124 target->type->soft_reset_halt(target);
2129 COMMAND_HANDLER(handle_reset_command)
2132 return ERROR_COMMAND_SYNTAX_ERROR;
2134 enum target_reset_mode reset_mode = RESET_RUN;
2138 n = Jim_Nvp_name2value_simple(nvp_reset_modes, CMD_ARGV[0]);
2139 if ((n->name == NULL) || (n->value == RESET_UNKNOWN)) {
2140 return ERROR_COMMAND_SYNTAX_ERROR;
2142 reset_mode = n->value;
2145 /* reset *all* targets */
2146 return target_process_reset(CMD_CTX, reset_mode);
2150 COMMAND_HANDLER(handle_resume_command)
2154 return ERROR_COMMAND_SYNTAX_ERROR;
2156 struct target *target = get_current_target(CMD_CTX);
2157 target_handle_event(target, TARGET_EVENT_OLD_pre_resume);
2159 /* with no CMD_ARGV, resume from current pc, addr = 0,
2160 * with one arguments, addr = CMD_ARGV[0],
2161 * handle breakpoints, not debugging */
2165 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], addr);
2169 return target_resume(target, current, addr, 1, 0);
2172 COMMAND_HANDLER(handle_step_command)
2175 return ERROR_COMMAND_SYNTAX_ERROR;
2179 /* with no CMD_ARGV, step from current pc, addr = 0,
2180 * with one argument addr = CMD_ARGV[0],
2181 * handle breakpoints, debugging */
2186 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], addr);
2190 struct target *target = get_current_target(CMD_CTX);
2192 return target->type->step(target, current_pc, addr, 1);
2195 static void handle_md_output(struct command_context *cmd_ctx,
2196 struct target *target, uint32_t address, unsigned size,
2197 unsigned count, const uint8_t *buffer)
2199 const unsigned line_bytecnt = 32;
2200 unsigned line_modulo = line_bytecnt / size;
2202 char output[line_bytecnt * 4 + 1];
2203 unsigned output_len = 0;
2205 const char *value_fmt;
2207 case 4: value_fmt = "%8.8x "; break;
2208 case 2: value_fmt = "%4.2x "; break;
2209 case 1: value_fmt = "%2.2x "; break;
2211 LOG_ERROR("invalid memory read size: %u", size);
2215 for (unsigned i = 0; i < count; i++)
2217 if (i % line_modulo == 0)
2219 output_len += snprintf(output + output_len,
2220 sizeof(output) - output_len,
2222 (unsigned)(address + (i*size)));
2226 const uint8_t *value_ptr = buffer + i * size;
2228 case 4: value = target_buffer_get_u32(target, value_ptr); break;
2229 case 2: value = target_buffer_get_u16(target, value_ptr); break;
2230 case 1: value = *value_ptr;
2232 output_len += snprintf(output + output_len,
2233 sizeof(output) - output_len,
2236 if ((i % line_modulo == line_modulo - 1) || (i == count - 1))
2238 command_print(cmd_ctx, "%s", output);
2244 COMMAND_HANDLER(handle_md_command)
2247 return ERROR_COMMAND_SYNTAX_ERROR;
2250 switch (CMD_NAME[2]) {
2251 case 'w': size = 4; break;
2252 case 'h': size = 2; break;
2253 case 'b': size = 1; break;
2254 default: return ERROR_COMMAND_SYNTAX_ERROR;
2257 bool physical=strcmp(CMD_ARGV[0], "phys")==0;
2258 int (*fn)(struct target *target,
2259 uint32_t address, uint32_t size, uint32_t count, uint8_t *buffer);
2264 fn=target_read_phys_memory;
2267 fn=target_read_memory;
2269 if ((CMD_ARGC < 1) || (CMD_ARGC > 2))
2271 return ERROR_COMMAND_SYNTAX_ERROR;
2275 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], address);
2279 COMMAND_PARSE_NUMBER(uint, CMD_ARGV[1], count);
2281 uint8_t *buffer = calloc(count, size);
2283 struct target *target = get_current_target(CMD_CTX);
2284 int retval = fn(target, address, size, count, buffer);
2285 if (ERROR_OK == retval)
2286 handle_md_output(CMD_CTX, target, address, size, count, buffer);
2293 COMMAND_HANDLER(handle_mw_command)
2297 return ERROR_COMMAND_SYNTAX_ERROR;
2299 bool physical=strcmp(CMD_ARGV[0], "phys")==0;
2300 int (*fn)(struct target *target,
2301 uint32_t address, uint32_t size, uint32_t count, uint8_t *buffer);
2306 fn=target_write_phys_memory;
2309 fn=target_write_memory;
2311 if ((CMD_ARGC < 2) || (CMD_ARGC > 3))
2312 return ERROR_COMMAND_SYNTAX_ERROR;
2315 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], address);
2318 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], value);
2322 COMMAND_PARSE_NUMBER(uint, CMD_ARGV[2], count);
2324 struct target *target = get_current_target(CMD_CTX);
2326 uint8_t value_buf[4];
2327 switch (CMD_NAME[2])
2331 target_buffer_set_u32(target, value_buf, value);
2335 target_buffer_set_u16(target, value_buf, value);
2339 value_buf[0] = value;
2342 return ERROR_COMMAND_SYNTAX_ERROR;
2344 for (unsigned i = 0; i < count; i++)
2346 int retval = fn(target,
2347 address + i * wordsize, wordsize, 1, value_buf);
2348 if (ERROR_OK != retval)
2357 static COMMAND_HELPER(parse_load_image_command_CMD_ARGV, struct image *image,
2358 uint32_t *min_address, uint32_t *max_address)
2360 if (CMD_ARGC < 1 || CMD_ARGC > 5)
2361 return ERROR_COMMAND_SYNTAX_ERROR;
2363 /* a base address isn't always necessary,
2364 * default to 0x0 (i.e. don't relocate) */
2368 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], addr);
2369 image->base_address = addr;
2370 image->base_address_set = 1;
2373 image->base_address_set = 0;
2375 image->start_address_set = 0;
2379 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[3], *min_address);
2383 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[4], *max_address);
2384 // use size (given) to find max (required)
2385 *max_address += *min_address;
2388 if (*min_address > *max_address)
2389 return ERROR_COMMAND_SYNTAX_ERROR;
2394 COMMAND_HANDLER(handle_load_image_command)
2398 uint32_t image_size;
2399 uint32_t min_address = 0;
2400 uint32_t max_address = 0xffffffff;
2404 int retval = CALL_COMMAND_HANDLER(parse_load_image_command_CMD_ARGV,
2405 &image, &min_address, &max_address);
2406 if (ERROR_OK != retval)
2409 struct target *target = get_current_target(CMD_CTX);
2411 struct duration bench;
2412 duration_start(&bench);
2414 if (image_open(&image, CMD_ARGV[0], (CMD_ARGC >= 3) ? CMD_ARGV[2] : NULL) != ERROR_OK)
2421 for (i = 0; i < image.num_sections; i++)
2423 buffer = malloc(image.sections[i].size);
2426 command_print(CMD_CTX,
2427 "error allocating buffer for section (%d bytes)",
2428 (int)(image.sections[i].size));
2432 if ((retval = image_read_section(&image, i, 0x0, image.sections[i].size, buffer, &buf_cnt)) != ERROR_OK)
2438 uint32_t offset = 0;
2439 uint32_t length = buf_cnt;
2441 /* DANGER!!! beware of unsigned comparision here!!! */
2443 if ((image.sections[i].base_address + buf_cnt >= min_address)&&
2444 (image.sections[i].base_address < max_address))
2446 if (image.sections[i].base_address < min_address)
2448 /* clip addresses below */
2449 offset += min_address-image.sections[i].base_address;
2453 if (image.sections[i].base_address + buf_cnt > max_address)
2455 length -= (image.sections[i].base_address + buf_cnt)-max_address;
2458 if ((retval = target_write_buffer(target, image.sections[i].base_address + offset, length, buffer + offset)) != ERROR_OK)
2463 image_size += length;
2464 command_print(CMD_CTX, "%u bytes written at address 0x%8.8" PRIx32 "",
2465 (unsigned int)length,
2466 image.sections[i].base_address + offset);
2472 if ((ERROR_OK == retval) && (duration_measure(&bench) == ERROR_OK))
2474 command_print(CMD_CTX, "downloaded %" PRIu32 " bytes "
2475 "in %fs (%0.3f kb/s)", image_size,
2476 duration_elapsed(&bench), duration_kbps(&bench, image_size));
2479 image_close(&image);
2485 COMMAND_HANDLER(handle_dump_image_command)
2487 struct fileio fileio;
2489 uint8_t buffer[560];
2493 struct target *target = get_current_target(CMD_CTX);
2497 command_print(CMD_CTX, "usage: dump_image <filename> <address> <size>");
2502 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], address);
2504 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[2], size);
2506 if (fileio_open(&fileio, CMD_ARGV[0], FILEIO_WRITE, FILEIO_BINARY) != ERROR_OK)
2511 struct duration bench;
2512 duration_start(&bench);
2514 int retval = ERROR_OK;
2517 size_t size_written;
2518 uint32_t this_run_size = (size > 560) ? 560 : size;
2519 retval = target_read_buffer(target, address, this_run_size, buffer);
2520 if (retval != ERROR_OK)
2525 retval = fileio_write(&fileio, this_run_size, buffer, &size_written);
2526 if (retval != ERROR_OK)
2531 size -= this_run_size;
2532 address += this_run_size;
2535 if ((retvaltemp = fileio_close(&fileio)) != ERROR_OK)
2538 if ((ERROR_OK == retval) && (duration_measure(&bench) == ERROR_OK))
2540 command_print(CMD_CTX,
2541 "dumped %zu bytes in %fs (%0.3f kb/s)", fileio.size,
2542 duration_elapsed(&bench), duration_kbps(&bench, fileio.size));
2548 static COMMAND_HELPER(handle_verify_image_command_internal, int verify)
2552 uint32_t image_size;
2555 uint32_t checksum = 0;
2556 uint32_t mem_checksum = 0;
2560 struct target *target = get_current_target(CMD_CTX);
2564 return ERROR_COMMAND_SYNTAX_ERROR;
2569 LOG_ERROR("no target selected");
2573 struct duration bench;
2574 duration_start(&bench);
2579 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], addr);
2580 image.base_address = addr;
2581 image.base_address_set = 1;
2585 image.base_address_set = 0;
2586 image.base_address = 0x0;
2589 image.start_address_set = 0;
2591 if ((retval = image_open(&image, CMD_ARGV[0], (CMD_ARGC == 3) ? CMD_ARGV[2] : NULL)) != ERROR_OK)
2598 for (i = 0; i < image.num_sections; i++)
2600 buffer = malloc(image.sections[i].size);
2603 command_print(CMD_CTX,
2604 "error allocating buffer for section (%d bytes)",
2605 (int)(image.sections[i].size));
2608 if ((retval = image_read_section(&image, i, 0x0, image.sections[i].size, buffer, &buf_cnt)) != ERROR_OK)
2616 /* calculate checksum of image */
2617 image_calculate_checksum(buffer, buf_cnt, &checksum);
2619 retval = target_checksum_memory(target, image.sections[i].base_address, buf_cnt, &mem_checksum);
2620 if (retval != ERROR_OK)
2626 if (checksum != mem_checksum)
2628 /* failed crc checksum, fall back to a binary compare */
2631 command_print(CMD_CTX, "checksum mismatch - attempting binary compare");
2633 data = (uint8_t*)malloc(buf_cnt);
2635 /* Can we use 32bit word accesses? */
2637 int count = buf_cnt;
2638 if ((count % 4) == 0)
2643 retval = target_read_memory(target, image.sections[i].base_address, size, count, data);
2644 if (retval == ERROR_OK)
2647 for (t = 0; t < buf_cnt; t++)
2649 if (data[t] != buffer[t])
2651 command_print(CMD_CTX,
2652 "Verify operation failed address 0x%08x. Was 0x%02x instead of 0x%02x\n",
2653 (unsigned)(t + image.sections[i].base_address),
2658 retval = ERROR_FAIL;
2672 command_print(CMD_CTX, "address 0x%08" PRIx32 " length 0x%08zx",
2673 image.sections[i].base_address,
2678 image_size += buf_cnt;
2681 if ((ERROR_OK == retval) && (duration_measure(&bench) == ERROR_OK))
2683 command_print(CMD_CTX, "verified %" PRIu32 " bytes "
2684 "in %fs (%0.3f kb/s)", image_size,
2685 duration_elapsed(&bench), duration_kbps(&bench, image_size));
2688 image_close(&image);
2693 COMMAND_HANDLER(handle_verify_image_command)
2695 return CALL_COMMAND_HANDLER(handle_verify_image_command_internal, 1);
2698 COMMAND_HANDLER(handle_test_image_command)
2700 return CALL_COMMAND_HANDLER(handle_verify_image_command_internal, 0);
2703 static int handle_bp_command_list(struct command_context *cmd_ctx)
2705 struct target *target = get_current_target(cmd_ctx);
2706 struct breakpoint *breakpoint = target->breakpoints;
2709 if (breakpoint->type == BKPT_SOFT)
2711 char* buf = buf_to_str(breakpoint->orig_instr,
2712 breakpoint->length, 16);
2713 command_print(cmd_ctx, "0x%8.8" PRIx32 ", 0x%x, %i, 0x%s",
2714 breakpoint->address,
2716 breakpoint->set, buf);
2721 command_print(cmd_ctx, "0x%8.8" PRIx32 ", 0x%x, %i",
2722 breakpoint->address,
2723 breakpoint->length, breakpoint->set);
2726 breakpoint = breakpoint->next;
2731 static int handle_bp_command_set(struct command_context *cmd_ctx,
2732 uint32_t addr, uint32_t length, int hw)
2734 struct target *target = get_current_target(cmd_ctx);
2735 int retval = breakpoint_add(target, addr, length, hw);
2736 if (ERROR_OK == retval)
2737 command_print(cmd_ctx, "breakpoint set at 0x%8.8" PRIx32 "", addr);
2739 LOG_ERROR("Failure setting breakpoint");
2743 COMMAND_HANDLER(handle_bp_command)
2746 return handle_bp_command_list(CMD_CTX);
2748 if (CMD_ARGC < 2 || CMD_ARGC > 3)
2750 command_print(CMD_CTX, "usage: bp <address> <length> ['hw']");
2751 return ERROR_COMMAND_SYNTAX_ERROR;
2755 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], addr);
2757 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], length);
2762 if (strcmp(CMD_ARGV[2], "hw") == 0)
2765 return ERROR_COMMAND_SYNTAX_ERROR;
2768 return handle_bp_command_set(CMD_CTX, addr, length, hw);
2771 COMMAND_HANDLER(handle_rbp_command)
2774 return ERROR_COMMAND_SYNTAX_ERROR;
2777 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], addr);
2779 struct target *target = get_current_target(CMD_CTX);
2780 breakpoint_remove(target, addr);
2785 COMMAND_HANDLER(handle_wp_command)
2787 struct target *target = get_current_target(CMD_CTX);
2791 struct watchpoint *watchpoint = target->watchpoints;
2795 command_print(CMD_CTX, "address: 0x%8.8" PRIx32
2796 ", len: 0x%8.8" PRIx32
2797 ", r/w/a: %i, value: 0x%8.8" PRIx32
2798 ", mask: 0x%8.8" PRIx32,
2799 watchpoint->address,
2801 (int)watchpoint->rw,
2804 watchpoint = watchpoint->next;
2809 enum watchpoint_rw type = WPT_ACCESS;
2811 uint32_t length = 0;
2812 uint32_t data_value = 0x0;
2813 uint32_t data_mask = 0xffffffff;
2818 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[4], data_mask);
2821 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[3], data_value);
2824 switch (CMD_ARGV[2][0])
2836 LOG_ERROR("invalid watchpoint mode ('%c')", CMD_ARGV[2][0]);
2837 return ERROR_COMMAND_SYNTAX_ERROR;
2841 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], length);
2842 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], addr);
2846 command_print(CMD_CTX, "usage: wp [address length "
2847 "[(r|w|a) [value [mask]]]]");
2848 return ERROR_COMMAND_SYNTAX_ERROR;
2851 int retval = watchpoint_add(target, addr, length, type,
2852 data_value, data_mask);
2853 if (ERROR_OK != retval)
2854 LOG_ERROR("Failure setting watchpoints");
2859 COMMAND_HANDLER(handle_rwp_command)
2862 return ERROR_COMMAND_SYNTAX_ERROR;
2865 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], addr);
2867 struct target *target = get_current_target(CMD_CTX);
2868 watchpoint_remove(target, addr);
2875 * Translate a virtual address to a physical address.
2877 * The low-level target implementation must have logged a detailed error
2878 * which is forwarded to telnet/GDB session.
2880 COMMAND_HANDLER(handle_virt2phys_command)
2883 return ERROR_COMMAND_SYNTAX_ERROR;
2886 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], va);
2889 struct target *target = get_current_target(CMD_CTX);
2890 int retval = target->type->virt2phys(target, va, &pa);
2891 if (retval == ERROR_OK)
2892 command_print(CMD_CTX, "Physical address 0x%08" PRIx32 "", pa);
2897 static void writeData(FILE *f, const void *data, size_t len)
2899 size_t written = fwrite(data, 1, len, f);
2901 LOG_ERROR("failed to write %zu bytes: %s", len, strerror(errno));
2904 static void writeLong(FILE *f, int l)
2907 for (i = 0; i < 4; i++)
2909 char c = (l >> (i*8))&0xff;
2910 writeData(f, &c, 1);
2915 static void writeString(FILE *f, char *s)
2917 writeData(f, s, strlen(s));
2920 /* Dump a gmon.out histogram file. */
2921 static void writeGmon(uint32_t *samples, uint32_t sampleNum, const char *filename)
2924 FILE *f = fopen(filename, "w");
2927 writeString(f, "gmon");
2928 writeLong(f, 0x00000001); /* Version */
2929 writeLong(f, 0); /* padding */
2930 writeLong(f, 0); /* padding */
2931 writeLong(f, 0); /* padding */
2933 uint8_t zero = 0; /* GMON_TAG_TIME_HIST */
2934 writeData(f, &zero, 1);
2936 /* figure out bucket size */
2937 uint32_t min = samples[0];
2938 uint32_t max = samples[0];
2939 for (i = 0; i < sampleNum; i++)
2941 if (min > samples[i])
2945 if (max < samples[i])
2951 int addressSpace = (max-min + 1);
2953 static const uint32_t maxBuckets = 256 * 1024; /* maximum buckets. */
2954 uint32_t length = addressSpace;
2955 if (length > maxBuckets)
2957 length = maxBuckets;
2959 int *buckets = malloc(sizeof(int)*length);
2960 if (buckets == NULL)
2965 memset(buckets, 0, sizeof(int)*length);
2966 for (i = 0; i < sampleNum;i++)
2968 uint32_t address = samples[i];
2969 long long a = address-min;
2970 long long b = length-1;
2971 long long c = addressSpace-1;
2972 int index = (a*b)/c; /* danger!!!! int32 overflows */
2976 /* append binary memory gmon.out &profile_hist_hdr ((char*)&profile_hist_hdr + sizeof(struct gmon_hist_hdr)) */
2977 writeLong(f, min); /* low_pc */
2978 writeLong(f, max); /* high_pc */
2979 writeLong(f, length); /* # of samples */
2980 writeLong(f, 64000000); /* 64MHz */
2981 writeString(f, "seconds");
2982 for (i = 0; i < (15-strlen("seconds")); i++)
2983 writeData(f, &zero, 1);
2984 writeString(f, "s");
2986 /*append binary memory gmon.out profile_hist_data (profile_hist_data + profile_hist_hdr.hist_size) */
2988 char *data = malloc(2*length);
2991 for (i = 0; i < length;i++)
3000 data[i*2 + 1]=(val >> 8)&0xff;
3003 writeData(f, data, length * 2);
3013 /* profiling samples the CPU PC as quickly as OpenOCD is able, which will be used as a random sampling of PC */
3014 COMMAND_HANDLER(handle_profile_command)
3016 struct target *target = get_current_target(CMD_CTX);
3017 struct timeval timeout, now;
3019 gettimeofday(&timeout, NULL);
3022 return ERROR_COMMAND_SYNTAX_ERROR;
3025 COMMAND_PARSE_NUMBER(uint, CMD_ARGV[0], offset);
3027 timeval_add_time(&timeout, offset, 0);
3029 command_print(CMD_CTX, "Starting profiling. Halting and resuming the target as often as we can...");
3031 static const int maxSample = 10000;
3032 uint32_t *samples = malloc(sizeof(uint32_t)*maxSample);
3033 if (samples == NULL)
3037 /* hopefully it is safe to cache! We want to stop/restart as quickly as possible. */
3038 struct reg *reg = register_get_by_name(target->reg_cache, "pc", 1);
3043 target_poll(target);
3044 if (target->state == TARGET_HALTED)
3046 uint32_t t=*((uint32_t *)reg->value);
3047 samples[numSamples++]=t;
3048 retval = target_resume(target, 1, 0, 0, 0); /* current pc, addr = 0, do not handle breakpoints, not debugging */
3049 target_poll(target);
3050 alive_sleep(10); /* sleep 10ms, i.e. <100 samples/second. */
3051 } else if (target->state == TARGET_RUNNING)
3053 /* We want to quickly sample the PC. */
3054 if ((retval = target_halt(target)) != ERROR_OK)
3061 command_print(CMD_CTX, "Target not halted or running");
3065 if (retval != ERROR_OK)
3070 gettimeofday(&now, NULL);
3071 if ((numSamples >= maxSample) || ((now.tv_sec >= timeout.tv_sec) && (now.tv_usec >= timeout.tv_usec)))
3073 command_print(CMD_CTX, "Profiling completed. %d samples.", numSamples);
3074 if ((retval = target_poll(target)) != ERROR_OK)
3079 if (target->state == TARGET_HALTED)
3081 target_resume(target, 1, 0, 0, 0); /* current pc, addr = 0, do not handle breakpoints, not debugging */
3083 if ((retval = target_poll(target)) != ERROR_OK)
3088 writeGmon(samples, numSamples, CMD_ARGV[1]);
3089 command_print(CMD_CTX, "Wrote %s", CMD_ARGV[1]);
3098 static int new_int_array_element(Jim_Interp * interp, const char *varname, int idx, uint32_t val)
3101 Jim_Obj *nameObjPtr, *valObjPtr;
3104 namebuf = alloc_printf("%s(%d)", varname, idx);
3108 nameObjPtr = Jim_NewStringObj(interp, namebuf, -1);
3109 valObjPtr = Jim_NewIntObj(interp, val);
3110 if (!nameObjPtr || !valObjPtr)
3116 Jim_IncrRefCount(nameObjPtr);
3117 Jim_IncrRefCount(valObjPtr);
3118 result = Jim_SetVariable(interp, nameObjPtr, valObjPtr);
3119 Jim_DecrRefCount(interp, nameObjPtr);
3120 Jim_DecrRefCount(interp, valObjPtr);
3122 /* printf("%s(%d) <= 0%08x\n", varname, idx, val); */
3126 static int jim_mem2array(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
3128 struct command_context *context;
3129 struct target *target;
3131 context = Jim_GetAssocData(interp, "context");
3132 if (context == NULL)
3134 LOG_ERROR("mem2array: no command context");
3137 target = get_current_target(context);
3140 LOG_ERROR("mem2array: no current target");
3144 return target_mem2array(interp, target, argc-1, argv + 1);
3147 static int target_mem2array(Jim_Interp *interp, struct target *target, int argc, Jim_Obj *const *argv)
3155 const char *varname;
3159 /* argv[1] = name of array to receive the data
3160 * argv[2] = desired width
3161 * argv[3] = memory address
3162 * argv[4] = count of times to read
3165 Jim_WrongNumArgs(interp, 1, argv, "varname width addr nelems");
3168 varname = Jim_GetString(argv[0], &len);
3169 /* given "foo" get space for worse case "foo(%d)" .. add 20 */
3171 e = Jim_GetLong(interp, argv[1], &l);
3177 e = Jim_GetLong(interp, argv[2], &l);
3182 e = Jim_GetLong(interp, argv[3], &l);
3198 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3199 Jim_AppendStrings(interp, Jim_GetResult(interp), "Invalid width param, must be 8/16/32", NULL);
3203 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3204 Jim_AppendStrings(interp, Jim_GetResult(interp), "mem2array: zero width read?", NULL);
3207 if ((addr + (len * width)) < addr) {
3208 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3209 Jim_AppendStrings(interp, Jim_GetResult(interp), "mem2array: addr + len - wraps to zero?", NULL);
3212 /* absurd transfer size? */
3214 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3215 Jim_AppendStrings(interp, Jim_GetResult(interp), "mem2array: absurd > 64K item request", NULL);
3220 ((width == 2) && ((addr & 1) == 0)) ||
3221 ((width == 4) && ((addr & 3) == 0))) {
3225 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3226 sprintf(buf, "mem2array address: 0x%08" PRIx32 " is not aligned for %" PRId32 " byte reads",
3229 Jim_AppendStrings(interp, Jim_GetResult(interp), buf , NULL);
3238 size_t buffersize = 4096;
3239 uint8_t *buffer = malloc(buffersize);
3246 /* Slurp... in buffer size chunks */
3248 count = len; /* in objects.. */
3249 if (count > (buffersize/width)) {
3250 count = (buffersize/width);
3253 retval = target_read_memory(target, addr, width, count, buffer);
3254 if (retval != ERROR_OK) {
3256 LOG_ERROR("mem2array: Read @ 0x%08x, w=%d, cnt=%d, failed",
3260 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3261 Jim_AppendStrings(interp, Jim_GetResult(interp), "mem2array: cannot read memory", NULL);
3265 v = 0; /* shut up gcc */
3266 for (i = 0 ;i < count ;i++, n++) {
3269 v = target_buffer_get_u32(target, &buffer[i*width]);
3272 v = target_buffer_get_u16(target, &buffer[i*width]);
3275 v = buffer[i] & 0x0ff;
3278 new_int_array_element(interp, varname, n, v);
3286 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3291 static int get_int_array_element(Jim_Interp * interp, const char *varname, int idx, uint32_t *val)
3294 Jim_Obj *nameObjPtr, *valObjPtr;
3298 namebuf = alloc_printf("%s(%d)", varname, idx);
3302 nameObjPtr = Jim_NewStringObj(interp, namebuf, -1);
3309 Jim_IncrRefCount(nameObjPtr);
3310 valObjPtr = Jim_GetVariable(interp, nameObjPtr, JIM_ERRMSG);
3311 Jim_DecrRefCount(interp, nameObjPtr);
3313 if (valObjPtr == NULL)
3316 result = Jim_GetLong(interp, valObjPtr, &l);
3317 /* printf("%s(%d) => 0%08x\n", varname, idx, val); */
3322 static int jim_array2mem(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
3324 struct command_context *context;
3325 struct target *target;
3327 context = Jim_GetAssocData(interp, "context");
3328 if (context == NULL) {
3329 LOG_ERROR("array2mem: no command context");
3332 target = get_current_target(context);
3333 if (target == NULL) {
3334 LOG_ERROR("array2mem: no current target");
3338 return target_array2mem(interp,target, argc-1, argv + 1);
3340 static int target_array2mem(Jim_Interp *interp, struct target *target, int argc, Jim_Obj *const *argv)
3348 const char *varname;
3352 /* argv[1] = name of array to get the data
3353 * argv[2] = desired width
3354 * argv[3] = memory address
3355 * argv[4] = count to write
3358 Jim_WrongNumArgs(interp, 1, argv, "varname width addr nelems");
3361 varname = Jim_GetString(argv[0], &len);
3362 /* given "foo" get space for worse case "foo(%d)" .. add 20 */
3364 e = Jim_GetLong(interp, argv[1], &l);
3370 e = Jim_GetLong(interp, argv[2], &l);
3375 e = Jim_GetLong(interp, argv[3], &l);
3391 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3392 Jim_AppendStrings(interp, Jim_GetResult(interp), "Invalid width param, must be 8/16/32", NULL);
3396 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3397 Jim_AppendStrings(interp, Jim_GetResult(interp), "array2mem: zero width read?", NULL);
3400 if ((addr + (len * width)) < addr) {
3401 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3402 Jim_AppendStrings(interp, Jim_GetResult(interp), "array2mem: addr + len - wraps to zero?", NULL);
3405 /* absurd transfer size? */
3407 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3408 Jim_AppendStrings(interp, Jim_GetResult(interp), "array2mem: absurd > 64K item request", NULL);
3413 ((width == 2) && ((addr & 1) == 0)) ||
3414 ((width == 4) && ((addr & 3) == 0))) {
3418 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3419 sprintf(buf, "array2mem address: 0x%08x is not aligned for %d byte reads",
3422 Jim_AppendStrings(interp, Jim_GetResult(interp), buf , NULL);
3433 size_t buffersize = 4096;
3434 uint8_t *buffer = malloc(buffersize);
3439 /* Slurp... in buffer size chunks */
3441 count = len; /* in objects.. */
3442 if (count > (buffersize/width)) {
3443 count = (buffersize/width);
3446 v = 0; /* shut up gcc */
3447 for (i = 0 ;i < count ;i++, n++) {
3448 get_int_array_element(interp, varname, n, &v);
3451 target_buffer_set_u32(target, &buffer[i*width], v);
3454 target_buffer_set_u16(target, &buffer[i*width], v);
3457 buffer[i] = v & 0x0ff;
3463 retval = target_write_memory(target, addr, width, count, buffer);
3464 if (retval != ERROR_OK) {
3466 LOG_ERROR("array2mem: Write @ 0x%08x, w=%d, cnt=%d, failed",
3470 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3471 Jim_AppendStrings(interp, Jim_GetResult(interp), "array2mem: cannot read memory", NULL);
3479 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3484 void target_all_handle_event(enum target_event e)
3486 struct target *target;
3488 LOG_DEBUG("**all*targets: event: %d, %s",
3490 Jim_Nvp_value2name_simple(nvp_target_event, e)->name);
3492 target = all_targets;
3494 target_handle_event(target, e);
3495 target = target->next;
3500 /* FIX? should we propagate errors here rather than printing them
3503 void target_handle_event(struct target *target, enum target_event e)
3505 struct target_event_action *teap;
3507 for (teap = target->event_action; teap != NULL; teap = teap->next) {
3508 if (teap->event == e) {
3509 LOG_DEBUG("target: (%d) %s (%s) event: %d (%s) action: %s",
3510 target->target_number,
3512 target_get_name(target),
3514 Jim_Nvp_value2name_simple(nvp_target_event, e)->name,
3515 Jim_GetString(teap->body, NULL));
3516 if (Jim_EvalObj(interp, teap->body) != JIM_OK)
3518 Jim_PrintErrorMessage(interp);
3524 enum target_cfg_param {
3527 TCFG_WORK_AREA_VIRT,
3528 TCFG_WORK_AREA_PHYS,
3529 TCFG_WORK_AREA_SIZE,
3530 TCFG_WORK_AREA_BACKUP,
3533 TCFG_CHAIN_POSITION,
3536 static Jim_Nvp nvp_config_opts[] = {
3537 { .name = "-type", .value = TCFG_TYPE },
3538 { .name = "-event", .value = TCFG_EVENT },
3539 { .name = "-work-area-virt", .value = TCFG_WORK_AREA_VIRT },
3540 { .name = "-work-area-phys", .value = TCFG_WORK_AREA_PHYS },
3541 { .name = "-work-area-size", .value = TCFG_WORK_AREA_SIZE },
3542 { .name = "-work-area-backup", .value = TCFG_WORK_AREA_BACKUP },
3543 { .name = "-endian" , .value = TCFG_ENDIAN },
3544 { .name = "-variant", .value = TCFG_VARIANT },
3545 { .name = "-chain-position", .value = TCFG_CHAIN_POSITION },
3547 { .name = NULL, .value = -1 }
3550 static int target_configure(Jim_GetOptInfo *goi, struct target *target)
3558 /* parse config or cget options ... */
3559 while (goi->argc > 0) {
3560 Jim_SetEmptyResult(goi->interp);
3561 /* Jim_GetOpt_Debug(goi); */
3563 if (target->type->target_jim_configure) {
3564 /* target defines a configure function */
3565 /* target gets first dibs on parameters */
3566 e = (*(target->type->target_jim_configure))(target, goi);
3575 /* otherwise we 'continue' below */
3577 e = Jim_GetOpt_Nvp(goi, nvp_config_opts, &n);
3579 Jim_GetOpt_NvpUnknown(goi, nvp_config_opts, 0);
3585 if (goi->isconfigure) {
3586 Jim_SetResult_sprintf(goi->interp, "not setable: %s", n->name);
3590 if (goi->argc != 0) {
3591 Jim_WrongNumArgs(goi->interp, goi->argc, goi->argv, "NO PARAMS");
3595 Jim_SetResultString(goi->interp, target_get_name(target), -1);
3599 if (goi->argc == 0) {
3600 Jim_WrongNumArgs(goi->interp, goi->argc, goi->argv, "-event ?event-name? ...");
3604 e = Jim_GetOpt_Nvp(goi, nvp_target_event, &n);
3606 Jim_GetOpt_NvpUnknown(goi, nvp_target_event, 1);
3610 if (goi->isconfigure) {
3611 if (goi->argc != 1) {
3612 Jim_WrongNumArgs(goi->interp, goi->argc, goi->argv, "-event ?event-name? ?EVENT-BODY?");
3616 if (goi->argc != 0) {
3617 Jim_WrongNumArgs(goi->interp, goi->argc, goi->argv, "-event ?event-name?");
3623 struct target_event_action *teap;
3625 teap = target->event_action;
3626 /* replace existing? */
3628 if (teap->event == (enum target_event)n->value) {
3634 if (goi->isconfigure) {
3635 bool replace = true;
3638 teap = calloc(1, sizeof(*teap));
3641 teap->event = n->value;
3642 Jim_GetOpt_Obj(goi, &o);
3644 Jim_DecrRefCount(interp, teap->body);
3646 teap->body = Jim_DuplicateObj(goi->interp, o);
3649 * Tcl/TK - "tk events" have a nice feature.
3650 * See the "BIND" command.
3651 * We should support that here.
3652 * You can specify %X and %Y in the event code.
3653 * The idea is: %T - target name.
3654 * The idea is: %N - target number
3655 * The idea is: %E - event name.
3657 Jim_IncrRefCount(teap->body);
3661 /* add to head of event list */
3662 teap->next = target->event_action;
3663 target->event_action = teap;
3665 Jim_SetEmptyResult(goi->interp);
3669 Jim_SetEmptyResult(goi->interp);
3671 Jim_SetResult(goi->interp, Jim_DuplicateObj(goi->interp, teap->body));
3678 case TCFG_WORK_AREA_VIRT:
3679 if (goi->isconfigure) {
3680 target_free_all_working_areas(target);
3681 e = Jim_GetOpt_Wide(goi, &w);
3685 target->working_area_virt = w;
3686 target->working_area_virt_spec = true;
3688 if (goi->argc != 0) {
3692 Jim_SetResult(interp, Jim_NewIntObj(goi->interp, target->working_area_virt));
3696 case TCFG_WORK_AREA_PHYS:
3697 if (goi->isconfigure) {
3698 target_free_all_working_areas(target);
3699 e = Jim_GetOpt_Wide(goi, &w);
3703 target->working_area_phys = w;
3704 target->working_area_phys_spec = true;
3706 if (goi->argc != 0) {
3710 Jim_SetResult(interp, Jim_NewIntObj(goi->interp, target->working_area_phys));
3714 case TCFG_WORK_AREA_SIZE:
3715 if (goi->isconfigure) {
3716 target_free_all_working_areas(target);
3717 e = Jim_GetOpt_Wide(goi, &w);
3721 target->working_area_size = w;
3723 if (goi->argc != 0) {
3727 Jim_SetResult(interp, Jim_NewIntObj(goi->interp, target->working_area_size));
3731 case TCFG_WORK_AREA_BACKUP:
3732 if (goi->isconfigure) {
3733 target_free_all_working_areas(target);
3734 e = Jim_GetOpt_Wide(goi, &w);
3738 /* make this exactly 1 or 0 */
3739 target->backup_working_area = (!!w);
3741 if (goi->argc != 0) {
3745 Jim_SetResult(interp, Jim_NewIntObj(goi->interp, target->backup_working_area));
3746 /* loop for more e*/
3750 if (goi->isconfigure) {
3751 e = Jim_GetOpt_Nvp(goi, nvp_target_endian, &n);
3753 Jim_GetOpt_NvpUnknown(goi, nvp_target_endian, 1);
3756 target->endianness = n->value;
3758 if (goi->argc != 0) {
3762 n = Jim_Nvp_value2name_simple(nvp_target_endian, target->endianness);
3763 if (n->name == NULL) {
3764 target->endianness = TARGET_LITTLE_ENDIAN;
3765 n = Jim_Nvp_value2name_simple(nvp_target_endian, target->endianness);
3767 Jim_SetResultString(goi->interp, n->name, -1);
3772 if (goi->isconfigure) {
3773 if (goi->argc < 1) {
3774 Jim_SetResult_sprintf(goi->interp,
3779 if (target->variant) {
3780 free((void *)(target->variant));
3782 e = Jim_GetOpt_String(goi, &cp, NULL);
3783 target->variant = strdup(cp);
3785 if (goi->argc != 0) {
3789 Jim_SetResultString(goi->interp, target->variant,-1);
3792 case TCFG_CHAIN_POSITION:
3793 if (goi->isconfigure) {
3795 struct jtag_tap *tap;
3796 target_free_all_working_areas(target);
3797 e = Jim_GetOpt_Obj(goi, &o);
3801 tap = jtag_tap_by_jim_obj(goi->interp, o);
3805 /* make this exactly 1 or 0 */
3808 if (goi->argc != 0) {
3812 Jim_SetResultString(interp, target->tap->dotted_name, -1);
3813 /* loop for more e*/
3816 } /* while (goi->argc) */
3819 /* done - we return */
3823 /** this is the 'tcl' handler for the target specific command */
3824 static int tcl_target_func(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
3829 uint8_t target_buf[32];
3831 struct target *target;
3832 struct command_context *cmd_ctx;
3839 TS_CMD_MWW, TS_CMD_MWH, TS_CMD_MWB,
3840 TS_CMD_MDW, TS_CMD_MDH, TS_CMD_MDB,
3841 TS_CMD_MRW, TS_CMD_MRH, TS_CMD_MRB,
3842 TS_CMD_MEM2ARRAY, TS_CMD_ARRAY2MEM,
3850 TS_CMD_INVOKE_EVENT,
3853 static const Jim_Nvp target_options[] = {
3854 { .name = "configure", .value = TS_CMD_CONFIGURE },
3855 { .name = "cget", .value = TS_CMD_CGET },
3856 { .name = "mww", .value = TS_CMD_MWW },
3857 { .name = "mwh", .value = TS_CMD_MWH },
3858 { .name = "mwb", .value = TS_CMD_MWB },
3859 { .name = "mdw", .value = TS_CMD_MDW },
3860 { .name = "mdh", .value = TS_CMD_MDH },
3861 { .name = "mdb", .value = TS_CMD_MDB },
3862 { .name = "mem2array", .value = TS_CMD_MEM2ARRAY },
3863 { .name = "array2mem", .value = TS_CMD_ARRAY2MEM },
3864 { .name = "eventlist", .value = TS_CMD_EVENTLIST },
3865 { .name = "curstate", .value = TS_CMD_CURSTATE },
3867 { .name = "arp_examine", .value = TS_CMD_EXAMINE },
3868 { .name = "arp_poll", .value = TS_CMD_POLL },
3869 { .name = "arp_reset", .value = TS_CMD_RESET },
3870 { .name = "arp_halt", .value = TS_CMD_HALT },
3871 { .name = "arp_waitstate", .value = TS_CMD_WAITSTATE },
3872 { .name = "invoke-event", .value = TS_CMD_INVOKE_EVENT },
3874 { .name = NULL, .value = -1 },
3877 /* go past the "command" */
3878 Jim_GetOpt_Setup(&goi, interp, argc-1, argv + 1);
3880 target = Jim_CmdPrivData(goi.interp);
3881 cmd_ctx = Jim_GetAssocData(goi.interp, "context");
3883 /* commands here are in an NVP table */
3884 e = Jim_GetOpt_Nvp(&goi, target_options, &n);
3886 Jim_GetOpt_NvpUnknown(&goi, target_options, 0);
3889 /* Assume blank result */
3890 Jim_SetEmptyResult(goi.interp);
3893 case TS_CMD_CONFIGURE:
3895 Jim_WrongNumArgs(goi.interp, goi.argc, goi.argv, "missing: -option VALUE ...");
3898 goi.isconfigure = 1;
3899 return target_configure(&goi, target);
3901 // some things take params
3903 Jim_WrongNumArgs(goi.interp, 0, goi.argv, "missing: ?-option?");
3906 goi.isconfigure = 0;
3907 return target_configure(&goi, target);
3915 * argv[3] = optional count.
3918 if ((goi.argc == 2) || (goi.argc == 3)) {
3922 Jim_SetResult_sprintf(goi.interp, "expected: %s ADDR DATA [COUNT]", n->name);
3926 e = Jim_GetOpt_Wide(&goi, &a);
3931 e = Jim_GetOpt_Wide(&goi, &b);
3935 if (goi.argc == 3) {
3936 e = Jim_GetOpt_Wide(&goi, &c);
3946 target_buffer_set_u32(target, target_buf, b);
3950 target_buffer_set_u16(target, target_buf, b);
3954 target_buffer_set_u8(target, target_buf, b);
3958 for (x = 0 ; x < c ; x++) {
3959 e = target_write_memory(target, a, b, 1, target_buf);
3960 if (e != ERROR_OK) {
3961 Jim_SetResult_sprintf(interp, "Error writing @ 0x%08x: %d\n", (int)(a), e);
3974 /* argv[0] = command
3976 * argv[2] = optional count
3978 if ((goi.argc == 2) || (goi.argc == 3)) {
3979 Jim_SetResult_sprintf(goi.interp, "expected: %s ADDR [COUNT]", n->name);
3982 e = Jim_GetOpt_Wide(&goi, &a);
3987 e = Jim_GetOpt_Wide(&goi, &c);
3994 b = 1; /* shut up gcc */
4007 /* convert to "bytes" */
4009 /* count is now in 'BYTES' */
4015 e = target_read_memory(target, a, b, y / b, target_buf);
4016 if (e != ERROR_OK) {
4017 Jim_SetResult_sprintf(interp, "error reading target @ 0x%08lx", (int)(a));
4021 Jim_fprintf(interp, interp->cookie_stdout, "0x%08x ", (int)(a));
4024 for (x = 0 ; (x < 16) && (x < y) ; x += 4) {
4025 z = target_buffer_get_u32(target, &(target_buf[ x * 4 ]));
4026 Jim_fprintf(interp, interp->cookie_stdout, "%08x ", (int)(z));
4028 for (; (x < 16) ; x += 4) {
4029 Jim_fprintf(interp, interp->cookie_stdout, " ");
4033 for (x = 0 ; (x < 16) && (x < y) ; x += 2) {
4034 z = target_buffer_get_u16(target, &(target_buf[ x * 2 ]));
4035 Jim_fprintf(interp, interp->cookie_stdout, "%04x ", (int)(z));
4037 for (; (x < 16) ; x += 2) {
4038 Jim_fprintf(interp, interp->cookie_stdout, " ");
4043 for (x = 0 ; (x < 16) && (x < y) ; x += 1) {
4044 z = target_buffer_get_u8(target, &(target_buf[ x * 4 ]));
4045 Jim_fprintf(interp, interp->cookie_stdout, "%02x ", (int)(z));
4047 for (; (x < 16) ; x += 1) {
4048 Jim_fprintf(interp, interp->cookie_stdout, " ");
4052 /* ascii-ify the bytes */
4053 for (x = 0 ; x < y ; x++) {
4054 if ((target_buf[x] >= 0x20) &&
4055 (target_buf[x] <= 0x7e)) {
4059 target_buf[x] = '.';
4064 target_buf[x] = ' ';
4069 /* print - with a newline */
4070 Jim_fprintf(interp, interp->cookie_stdout, "%s\n", target_buf);
4076 case TS_CMD_MEM2ARRAY:
4077 return target_mem2array(goi.interp, target, goi.argc, goi.argv);
4079 case TS_CMD_ARRAY2MEM:
4080 return target_array2mem(goi.interp, target, goi.argc, goi.argv);
4082 case TS_CMD_EXAMINE:
4084 Jim_WrongNumArgs(goi.interp, 2, argv, "[no parameters]");
4087 if (!target->tap->enabled)
4088 goto err_tap_disabled;
4089 e = target->type->examine(target);
4090 if (e != ERROR_OK) {
4091 Jim_SetResult_sprintf(interp, "examine-fails: %d", e);
4097 Jim_WrongNumArgs(goi.interp, 2, argv, "[no parameters]");
4100 if (!target->tap->enabled)
4101 goto err_tap_disabled;
4102 if (!(target_was_examined(target))) {
4103 e = ERROR_TARGET_NOT_EXAMINED;
4105 e = target->type->poll(target);
4107 if (e != ERROR_OK) {
4108 Jim_SetResult_sprintf(interp, "poll-fails: %d", e);
4115 if (goi.argc != 2) {
4116 Jim_WrongNumArgs(interp, 2, argv,
4117 "([tT]|[fF]|assert|deassert) BOOL");
4120 e = Jim_GetOpt_Nvp(&goi, nvp_assert, &n);
4122 Jim_GetOpt_NvpUnknown(&goi, nvp_assert, 1);
4125 /* the halt or not param */
4126 e = Jim_GetOpt_Wide(&goi, &a);
4130 if (!target->tap->enabled)
4131 goto err_tap_disabled;
4132 if (!target->type->assert_reset
4133 || !target->type->deassert_reset) {
4134 Jim_SetResult_sprintf(interp,
4135 "No target-specific reset for %s",
4139 /* determine if we should halt or not. */
4140 target->reset_halt = !!a;
4141 /* When this happens - all workareas are invalid. */
4142 target_free_all_working_areas_restore(target, 0);
4145 if (n->value == NVP_ASSERT) {
4146 e = target->type->assert_reset(target);
4148 e = target->type->deassert_reset(target);
4150 return (e == ERROR_OK) ? JIM_OK : JIM_ERR;
4153 Jim_WrongNumArgs(goi.interp, 0, argv, "halt [no parameters]");
4156 if (!target->tap->enabled)
4157 goto err_tap_disabled;
4158 e = target->type->halt(target);
4159 return (e == ERROR_OK) ? JIM_OK : JIM_ERR;
4160 case TS_CMD_WAITSTATE:
4161 /* params: <name> statename timeoutmsecs */
4162 if (goi.argc != 2) {
4163 Jim_SetResult_sprintf(goi.interp, "%s STATENAME TIMEOUTMSECS", n->name);
4166 e = Jim_GetOpt_Nvp(&goi, nvp_target_state, &n);
4168 Jim_GetOpt_NvpUnknown(&goi, nvp_target_state,1);
4171 e = Jim_GetOpt_Wide(&goi, &a);
4175 if (!target->tap->enabled)
4176 goto err_tap_disabled;
4177 e = target_wait_state(target, n->value, a);
4178 if (e != ERROR_OK) {
4179 Jim_SetResult_sprintf(goi.interp,
4180 "target: %s wait %s fails (%d) %s",
4183 e, target_strerror_safe(e));
4188 case TS_CMD_EVENTLIST:
4189 /* List for human, Events defined for this target.
4190 * scripts/programs should use 'name cget -event NAME'
4193 struct target_event_action *teap;
4194 teap = target->event_action;
4195 command_print(cmd_ctx, "Event actions for target (%d) %s\n",
4196 target->target_number,
4198 command_print(cmd_ctx, "%-25s | Body", "Event");
4199 command_print(cmd_ctx, "------------------------- | ----------------------------------------");
4201 command_print(cmd_ctx,
4203 Jim_Nvp_value2name_simple(nvp_target_event, teap->event)->name,
4204 Jim_GetString(teap->body, NULL));
4207 command_print(cmd_ctx, "***END***");
4210 case TS_CMD_CURSTATE:
4211 if (goi.argc != 0) {
4212 Jim_WrongNumArgs(goi.interp, 0, argv, "[no parameters]");
4215 Jim_SetResultString(goi.interp,
4216 target_state_name( target ),
4219 case TS_CMD_INVOKE_EVENT:
4220 if (goi.argc != 1) {
4221 Jim_SetResult_sprintf(goi.interp, "%s ?EVENTNAME?",n->name);
4224 e = Jim_GetOpt_Nvp(&goi, nvp_target_event, &n);
4226 Jim_GetOpt_NvpUnknown(&goi, nvp_target_event, 1);
4229 target_handle_event(target, n->value);
4235 Jim_SetResult_sprintf(interp, "[TAP is disabled]");
4239 static int target_create(Jim_GetOptInfo *goi)
4247 struct target *target;
4248 struct command_context *cmd_ctx;
4250 cmd_ctx = Jim_GetAssocData(goi->interp, "context");
4251 if (goi->argc < 3) {
4252 Jim_WrongNumArgs(goi->interp, 1, goi->argv, "?name? ?type? ..options...");
4257 Jim_GetOpt_Obj(goi, &new_cmd);
4258 /* does this command exist? */
4259 cmd = Jim_GetCommand(goi->interp, new_cmd, JIM_ERRMSG);
4261 cp = Jim_GetString(new_cmd, NULL);
4262 Jim_SetResult_sprintf(goi->interp, "Command/target: %s Exists", cp);
4267 e = Jim_GetOpt_String(goi, &cp2, NULL);
4269 /* now does target type exist */
4270 for (x = 0 ; target_types[x] ; x++) {
4271 if (0 == strcmp(cp, target_types[x]->name)) {
4276 if (target_types[x] == NULL) {
4277 Jim_SetResult_sprintf(goi->interp, "Unknown target type %s, try one of ", cp);
4278 for (x = 0 ; target_types[x] ; x++) {
4279 if (target_types[x + 1]) {
4280 Jim_AppendStrings(goi->interp,
4281 Jim_GetResult(goi->interp),
4282 target_types[x]->name,
4285 Jim_AppendStrings(goi->interp,
4286 Jim_GetResult(goi->interp),
4288 target_types[x]->name,NULL);
4295 target = calloc(1,sizeof(struct target));
4296 /* set target number */
4297 target->target_number = new_target_number();
4299 /* allocate memory for each unique target type */
4300 target->type = (struct target_type*)calloc(1,sizeof(struct target_type));
4302 memcpy(target->type, target_types[x], sizeof(struct target_type));
4304 /* will be set by "-endian" */
4305 target->endianness = TARGET_ENDIAN_UNKNOWN;
4307 target->working_area = 0x0;
4308 target->working_area_size = 0x0;
4309 target->working_areas = NULL;
4310 target->backup_working_area = 0;
4312 target->state = TARGET_UNKNOWN;
4313 target->debug_reason = DBG_REASON_UNDEFINED;
4314 target->reg_cache = NULL;
4315 target->breakpoints = NULL;
4316 target->watchpoints = NULL;
4317 target->next = NULL;
4318 target->arch_info = NULL;
4320 target->display = 1;
4322 target->halt_issued = false;
4324 /* initialize trace information */
4325 target->trace_info = malloc(sizeof(struct trace));
4326 target->trace_info->num_trace_points = 0;
4327 target->trace_info->trace_points_size = 0;
4328 target->trace_info->trace_points = NULL;
4329 target->trace_info->trace_history_size = 0;
4330 target->trace_info->trace_history = NULL;
4331 target->trace_info->trace_history_pos = 0;
4332 target->trace_info->trace_history_overflowed = 0;
4334 target->dbgmsg = NULL;
4335 target->dbg_msg_enabled = 0;
4337 target->endianness = TARGET_ENDIAN_UNKNOWN;
4339 /* Do the rest as "configure" options */
4340 goi->isconfigure = 1;
4341 e = target_configure(goi, target);
4343 if (target->tap == NULL)
4345 Jim_SetResultString(interp, "-chain-position required when creating target", -1);
4355 if (target->endianness == TARGET_ENDIAN_UNKNOWN) {
4356 /* default endian to little if not specified */
4357 target->endianness = TARGET_LITTLE_ENDIAN;
4360 /* incase variant is not set */
4361 if (!target->variant)
4362 target->variant = strdup("");
4364 cp = Jim_GetString(new_cmd, NULL);
4365 target->cmd_name = strdup(cp);
4367 /* create the target specific commands */
4368 if (target->type->commands) {
4369 e = register_commands(cmd_ctx, NULL, target->type->commands);
4371 LOG_ERROR("unable to register '%s' commands", cp);
4373 if (target->type->target_create) {
4374 (*(target->type->target_create))(target, goi->interp);
4377 /* append to end of list */
4379 struct target **tpp;
4380 tpp = &(all_targets);
4382 tpp = &((*tpp)->next);
4387 /* now - create the new target name command */
4388 const struct command_registration target_command = {
4390 .jim_handler = &tcl_target_func,
4391 .jim_handler_data = target,
4392 .help = "target command group",
4394 struct command *c = register_command(cmd_ctx, NULL, &target_command);
4395 return (NULL != c) ? ERROR_OK : ERROR_FAIL;
4398 static int jim_target(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
4402 struct command_context *cmd_ctx;
4403 struct target *target;
4406 /* TG = target generic */
4414 const char *target_cmds[] = {
4415 "create", "types", "names", "current", "number",
4417 NULL /* terminate */
4420 LOG_DEBUG("Target command params:");
4421 LOG_DEBUG("%s", Jim_Debug_ArgvString(interp, argc, argv));
4423 cmd_ctx = Jim_GetAssocData(interp, "context");
4425 Jim_GetOpt_Setup(&goi, interp, argc-1, argv + 1);
4427 if (goi.argc == 0) {
4428 Jim_WrongNumArgs(interp, 1, argv, "missing: command ...");
4432 /* Jim_GetOpt_Debug(&goi); */
4433 r = Jim_GetOpt_Enum(&goi, target_cmds, &x);
4440 Jim_Panic(goi.interp,"Why am I here?");
4442 case TG_CMD_CURRENT:
4443 if (goi.argc != 0) {
4444 Jim_WrongNumArgs(goi.interp, 1, goi.argv, "Too many parameters");
4447 Jim_SetResultString(goi.interp, get_current_target(cmd_ctx)->cmd_name, -1);
4450 if (goi.argc != 0) {
4451 Jim_WrongNumArgs(goi.interp, 1, goi.argv, "Too many parameters");
4454 Jim_SetResult(goi.interp, Jim_NewListObj(goi.interp, NULL, 0));
4455 for (x = 0 ; target_types[x] ; x++) {
4456 Jim_ListAppendElement(goi.interp,
4457 Jim_GetResult(goi.interp),
4458 Jim_NewStringObj(goi.interp, target_types[x]->name, -1));
4462 if (goi.argc != 0) {
4463 Jim_WrongNumArgs(goi.interp, 1, goi.argv, "Too many parameters");
4466 Jim_SetResult(goi.interp, Jim_NewListObj(goi.interp, NULL, 0));
4467 target = all_targets;
4469 Jim_ListAppendElement(goi.interp,
4470 Jim_GetResult(goi.interp),
4471 Jim_NewStringObj(goi.interp, target->cmd_name, -1));
4472 target = target->next;
4477 Jim_WrongNumArgs(goi.interp, goi.argc, goi.argv, "?name ... config options ...");
4480 return target_create(&goi);
4483 /* It's OK to remove this mechanism sometime after August 2010 or so */
4484 LOG_WARNING("don't use numbers as target identifiers; use names");
4485 if (goi.argc != 1) {
4486 Jim_SetResult_sprintf(goi.interp, "expected: target number ?NUMBER?");
4489 e = Jim_GetOpt_Wide(&goi, &w);
4493 for (x = 0, target = all_targets; target; target = target->next, x++) {
4494 if (target->target_number == w)
4497 if (target == NULL) {
4498 Jim_SetResult_sprintf(goi.interp,
4499 "Target: number %d does not exist", (int)(w));
4502 Jim_SetResultString(goi.interp, target->cmd_name, -1);
4505 if (goi.argc != 0) {
4506 Jim_WrongNumArgs(goi.interp, 0, goi.argv, "<no parameters>");
4509 for (x = 0, target = all_targets; target; target = target->next, x++)
4511 Jim_SetResult(goi.interp, Jim_NewIntObj(goi.interp, x));
4527 static int fastload_num;
4528 static struct FastLoad *fastload;
4530 static void free_fastload(void)
4532 if (fastload != NULL)
4535 for (i = 0; i < fastload_num; i++)
4537 if (fastload[i].data)
4538 free(fastload[i].data);
4548 COMMAND_HANDLER(handle_fast_load_image_command)
4552 uint32_t image_size;
4553 uint32_t min_address = 0;
4554 uint32_t max_address = 0xffffffff;
4559 int retval = CALL_COMMAND_HANDLER(parse_load_image_command_CMD_ARGV,
4560 &image, &min_address, &max_address);
4561 if (ERROR_OK != retval)
4564 struct duration bench;
4565 duration_start(&bench);
4567 if (image_open(&image, CMD_ARGV[0], (CMD_ARGC >= 3) ? CMD_ARGV[2] : NULL) != ERROR_OK)
4574 fastload_num = image.num_sections;
4575 fastload = (struct FastLoad *)malloc(sizeof(struct FastLoad)*image.num_sections);
4576 if (fastload == NULL)
4578 image_close(&image);
4581 memset(fastload, 0, sizeof(struct FastLoad)*image.num_sections);
4582 for (i = 0; i < image.num_sections; i++)
4584 buffer = malloc(image.sections[i].size);
4587 command_print(CMD_CTX, "error allocating buffer for section (%d bytes)",
4588 (int)(image.sections[i].size));
4592 if ((retval = image_read_section(&image, i, 0x0, image.sections[i].size, buffer, &buf_cnt)) != ERROR_OK)
4598 uint32_t offset = 0;
4599 uint32_t length = buf_cnt;
4602 /* DANGER!!! beware of unsigned comparision here!!! */
4604 if ((image.sections[i].base_address + buf_cnt >= min_address)&&
4605 (image.sections[i].base_address < max_address))
4607 if (image.sections[i].base_address < min_address)
4609 /* clip addresses below */
4610 offset += min_address-image.sections[i].base_address;
4614 if (image.sections[i].base_address + buf_cnt > max_address)
4616 length -= (image.sections[i].base_address + buf_cnt)-max_address;
4619 fastload[i].address = image.sections[i].base_address + offset;
4620 fastload[i].data = malloc(length);
4621 if (fastload[i].data == NULL)
4626 memcpy(fastload[i].data, buffer + offset, length);
4627 fastload[i].length = length;
4629 image_size += length;
4630 command_print(CMD_CTX, "%u bytes written at address 0x%8.8x",
4631 (unsigned int)length,
4632 ((unsigned int)(image.sections[i].base_address + offset)));
4638 if ((ERROR_OK == retval) && (duration_measure(&bench) == ERROR_OK))
4640 command_print(CMD_CTX, "Loaded %" PRIu32 " bytes "
4641 "in %fs (%0.3f kb/s)", image_size,
4642 duration_elapsed(&bench), duration_kbps(&bench, image_size));
4644 command_print(CMD_CTX,
4645 "WARNING: image has not been loaded to target!"
4646 "You can issue a 'fast_load' to finish loading.");
4649 image_close(&image);
4651 if (retval != ERROR_OK)
4659 COMMAND_HANDLER(handle_fast_load_command)
4662 return ERROR_COMMAND_SYNTAX_ERROR;
4663 if (fastload == NULL)
4665 LOG_ERROR("No image in memory");
4669 int ms = timeval_ms();
4671 int retval = ERROR_OK;
4672 for (i = 0; i < fastload_num;i++)
4674 struct target *target = get_current_target(CMD_CTX);
4675 command_print(CMD_CTX, "Write to 0x%08x, length 0x%08x",
4676 (unsigned int)(fastload[i].address),
4677 (unsigned int)(fastload[i].length));
4678 if (retval == ERROR_OK)
4680 retval = target_write_buffer(target, fastload[i].address, fastload[i].length, fastload[i].data);
4682 size += fastload[i].length;
4684 int after = timeval_ms();
4685 command_print(CMD_CTX, "Loaded image %f kBytes/s", (float)(size/1024.0)/((float)(after-ms)/1000.0));
4689 static int jim_mcrmrc(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
4691 struct command_context *context;
4692 struct target *target;
4695 context = Jim_GetAssocData(interp, "context");
4696 if (context == NULL) {
4697 LOG_ERROR("array2mem: no command context");
4700 target = get_current_target(context);
4701 if (target == NULL) {
4702 LOG_ERROR("array2mem: no current target");
4706 if ((argc < 6) || (argc > 7))
4720 e = Jim_GetLong(interp, argv[1], &l);
4726 e = Jim_GetLong(interp, argv[2], &l);
4732 e = Jim_GetLong(interp, argv[3], &l);
4738 e = Jim_GetLong(interp, argv[4], &l);
4744 e = Jim_GetLong(interp, argv[5], &l);
4754 e = Jim_GetLong(interp, argv[6], &l);
4760 retval = target_mcr(target, cpnum, op1, op2, CRn, CRm, value);
4761 if (retval != ERROR_OK)
4765 retval = target_mrc(target, cpnum, op1, op2, CRn, CRm, &value);
4766 if (retval != ERROR_OK)
4769 Jim_SetResult(interp, Jim_NewIntObj(interp, value));
4775 static const struct command_registration target_command_handlers[] = {
4778 .handler = &handle_targets_command,
4779 .mode = COMMAND_ANY,
4780 .help = "change current command line target (one parameter) "
4781 "or list targets (no parameters)",
4782 .usage = "[<new_current_target>]",
4786 .mode = COMMAND_CONFIG,
4787 .jim_handler = &jim_target,
4788 .help = "configure target",
4790 COMMAND_REGISTRATION_DONE
4793 int target_register_commands(struct command_context *cmd_ctx)
4795 return register_commands(cmd_ctx, NULL, target_command_handlers);
4798 static const struct command_registration target_exec_command_handlers[] = {
4800 .name = "fast_load_image",
4801 .handler = &handle_fast_load_image_command,
4802 .mode = COMMAND_ANY,
4803 .help = "Load image into memory, mainly for profiling purposes",
4804 .usage = "<file> <address> ['bin'|'ihex'|'elf'|'s19'] "
4805 "[min_address] [max_length]",
4808 .name = "fast_load",
4809 .handler = &handle_fast_load_command,
4810 .mode = COMMAND_ANY,
4811 .help = "loads active fast load image to current target "
4812 "- mainly for profiling purposes",
4816 .handler = &handle_profile_command,
4817 .mode = COMMAND_EXEC,
4818 .help = "profiling samples the CPU PC",
4820 /** @todo don't register virt2phys() unless target supports it */
4822 .name = "virt2phys",
4823 .handler = &handle_virt2phys_command,
4824 .mode = COMMAND_ANY,
4825 .help = "translate a virtual address into a physical address",
4830 .handler = &handle_reg_command,
4831 .mode = COMMAND_EXEC,
4832 .help = "display or set a register",
4837 .handler = &handle_poll_command,
4838 .mode = COMMAND_EXEC,
4839 .help = "poll target state",
4842 .name = "wait_halt",
4843 .handler = &handle_wait_halt_command,
4844 .mode = COMMAND_EXEC,
4845 .help = "wait for target halt",
4846 .usage = "[time (s)]",
4850 .handler = &handle_halt_command,
4851 .mode = COMMAND_EXEC,
4852 .help = "halt target",
4856 .handler = &handle_resume_command,
4857 .mode = COMMAND_EXEC,
4858 .help = "resume target",
4859 .usage = "[<address>]",
4863 .handler = &handle_reset_command,
4864 .mode = COMMAND_EXEC,
4865 .usage = "[run|halt|init]",
4866 .help = "Reset all targets into the specified mode."
4867 "Default reset mode is run, if not given.",
4870 .name = "soft_reset_halt",
4871 .handler = &handle_soft_reset_halt_command,
4872 .mode = COMMAND_EXEC,
4873 .help = "halt the target and do a soft reset",
4878 .handler = &handle_step_command,
4879 .mode = COMMAND_EXEC,
4880 .help = "step one instruction from current PC or [addr]",
4881 .usage = "[<address>]",
4886 .handler = &handle_md_command,
4887 .mode = COMMAND_EXEC,
4888 .help = "display memory words",
4889 .usage = "[phys] <addr> [count]",
4893 .handler = &handle_md_command,
4894 .mode = COMMAND_EXEC,
4895 .help = "display memory half-words",
4896 .usage = "[phys] <addr> [count]",
4900 .handler = &handle_md_command,
4901 .mode = COMMAND_EXEC,
4902 .help = "display memory bytes",
4903 .usage = "[phys] <addr> [count]",
4908 .handler = &handle_mw_command,
4909 .mode = COMMAND_EXEC,
4910 .help = "write memory word",
4911 .usage = "[phys] <addr> <value> [count]",
4915 .handler = &handle_mw_command,
4916 .mode = COMMAND_EXEC,
4917 .help = "write memory half-word",
4918 .usage = "[phys] <addr> <value> [count]",
4922 .handler = &handle_mw_command,
4923 .mode = COMMAND_EXEC,
4924 .help = "write memory byte",
4925 .usage = "[phys] <addr> <value> [count]",
4930 .handler = &handle_bp_command,
4931 .mode = COMMAND_EXEC,
4932 .help = "list or set breakpoint",
4933 .usage = "[<address> <length> [hw]]",
4937 .handler = &handle_rbp_command,
4938 .mode = COMMAND_EXEC,
4939 .help = "remove breakpoint",
4940 .usage = "<address>",
4945 .handler = &handle_wp_command,
4946 .mode = COMMAND_EXEC,
4947 .help = "list or set watchpoint",
4948 .usage = "[<address> <length> <r/w/a> [value] [mask]]",
4952 .handler = &handle_rwp_command,
4953 .mode = COMMAND_EXEC,
4954 .help = "remove watchpoint",
4955 .usage = "<address>",
4959 .name = "load_image",
4960 .handler = &handle_load_image_command,
4961 .mode = COMMAND_EXEC,
4962 .usage = "<file> <address> ['bin'|'ihex'|'elf'|'s19'] "
4963 "[min_address] [max_length]",
4966 .name = "dump_image",
4967 .handler = &handle_dump_image_command,
4968 .mode = COMMAND_EXEC,
4969 .usage = "<file> <address> <size>",
4972 .name = "verify_image",
4973 .handler = &handle_verify_image_command,
4974 .mode = COMMAND_EXEC,
4975 .usage = "<file> [offset] [type]",
4978 .name = "test_image",
4979 .handler = &handle_test_image_command,
4980 .mode = COMMAND_EXEC,
4981 .usage = "<file> [offset] [type]",
4984 .name = "ocd_mem2array",
4985 .mode = COMMAND_EXEC,
4986 .jim_handler = &jim_mem2array,
4987 .help = "read memory and return as a TCL array "
4988 "for script processing",
4989 .usage = "<arrayname> <width=32|16|8> <address> <count>",
4992 .name = "ocd_array2mem",
4993 .mode = COMMAND_EXEC,
4994 .jim_handler = &jim_array2mem,
4995 .help = "convert a TCL array to memory locations "
4996 "and write the values",
4997 .usage = "<arrayname> <width=32|16|8> <address> <count>",
4999 COMMAND_REGISTRATION_DONE
5001 int target_register_user_commands(struct command_context *cmd_ctx)
5003 int retval = ERROR_OK;
5004 if ((retval = target_request_register_commands(cmd_ctx)) != ERROR_OK)
5007 if ((retval = trace_register_commands(cmd_ctx)) != ERROR_OK)
5011 return register_commands(cmd_ctx, NULL, target_exec_command_handlers);