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 "time_support.h"
46 static int jim_mcrmrc(Jim_Interp *interp, int argc, Jim_Obj *const *argv);
48 static int target_array2mem(Jim_Interp *interp, target_t *target, int argc, Jim_Obj *const *argv);
49 static int target_mem2array(Jim_Interp *interp, target_t *target, int argc, Jim_Obj *const *argv);
52 extern target_type_t arm7tdmi_target;
53 extern target_type_t arm720t_target;
54 extern target_type_t arm9tdmi_target;
55 extern target_type_t arm920t_target;
56 extern target_type_t arm966e_target;
57 extern target_type_t arm926ejs_target;
58 extern target_type_t fa526_target;
59 extern target_type_t feroceon_target;
60 extern target_type_t dragonite_target;
61 extern target_type_t xscale_target;
62 extern target_type_t cortexm3_target;
63 extern target_type_t cortexa8_target;
64 extern target_type_t arm11_target;
65 extern target_type_t mips_m4k_target;
66 extern target_type_t avr_target;
68 target_type_t *target_types[] =
88 target_t *all_targets = NULL;
89 target_event_callback_t *target_event_callbacks = NULL;
90 target_timer_callback_t *target_timer_callbacks = NULL;
92 const Jim_Nvp nvp_assert[] = {
93 { .name = "assert", NVP_ASSERT },
94 { .name = "deassert", NVP_DEASSERT },
95 { .name = "T", NVP_ASSERT },
96 { .name = "F", NVP_DEASSERT },
97 { .name = "t", NVP_ASSERT },
98 { .name = "f", NVP_DEASSERT },
99 { .name = NULL, .value = -1 }
102 const Jim_Nvp nvp_error_target[] = {
103 { .value = ERROR_TARGET_INVALID, .name = "err-invalid" },
104 { .value = ERROR_TARGET_INIT_FAILED, .name = "err-init-failed" },
105 { .value = ERROR_TARGET_TIMEOUT, .name = "err-timeout" },
106 { .value = ERROR_TARGET_NOT_HALTED, .name = "err-not-halted" },
107 { .value = ERROR_TARGET_FAILURE, .name = "err-failure" },
108 { .value = ERROR_TARGET_UNALIGNED_ACCESS , .name = "err-unaligned-access" },
109 { .value = ERROR_TARGET_DATA_ABORT , .name = "err-data-abort" },
110 { .value = ERROR_TARGET_RESOURCE_NOT_AVAILABLE , .name = "err-resource-not-available" },
111 { .value = ERROR_TARGET_TRANSLATION_FAULT , .name = "err-translation-fault" },
112 { .value = ERROR_TARGET_NOT_RUNNING, .name = "err-not-running" },
113 { .value = ERROR_TARGET_NOT_EXAMINED, .name = "err-not-examined" },
114 { .value = -1, .name = NULL }
117 const char *target_strerror_safe(int err)
121 n = Jim_Nvp_value2name_simple(nvp_error_target, err);
122 if (n->name == NULL) {
129 static const Jim_Nvp nvp_target_event[] = {
130 { .value = TARGET_EVENT_OLD_gdb_program_config , .name = "old-gdb_program_config" },
131 { .value = TARGET_EVENT_OLD_pre_resume , .name = "old-pre_resume" },
133 { .value = TARGET_EVENT_GDB_HALT, .name = "gdb-halt" },
134 { .value = TARGET_EVENT_HALTED, .name = "halted" },
135 { .value = TARGET_EVENT_RESUMED, .name = "resumed" },
136 { .value = TARGET_EVENT_RESUME_START, .name = "resume-start" },
137 { .value = TARGET_EVENT_RESUME_END, .name = "resume-end" },
139 { .name = "gdb-start", .value = TARGET_EVENT_GDB_START },
140 { .name = "gdb-end", .value = TARGET_EVENT_GDB_END },
142 /* historical name */
144 { .value = TARGET_EVENT_RESET_START, .name = "reset-start" },
146 { .value = TARGET_EVENT_RESET_ASSERT_PRE, .name = "reset-assert-pre" },
147 { .value = TARGET_EVENT_RESET_ASSERT_POST, .name = "reset-assert-post" },
148 { .value = TARGET_EVENT_RESET_DEASSERT_PRE, .name = "reset-deassert-pre" },
149 { .value = TARGET_EVENT_RESET_DEASSERT_POST, .name = "reset-deassert-post" },
150 { .value = TARGET_EVENT_RESET_HALT_PRE, .name = "reset-halt-pre" },
151 { .value = TARGET_EVENT_RESET_HALT_POST, .name = "reset-halt-post" },
152 { .value = TARGET_EVENT_RESET_WAIT_PRE, .name = "reset-wait-pre" },
153 { .value = TARGET_EVENT_RESET_WAIT_POST, .name = "reset-wait-post" },
154 { .value = TARGET_EVENT_RESET_INIT , .name = "reset-init" },
155 { .value = TARGET_EVENT_RESET_END, .name = "reset-end" },
157 { .value = TARGET_EVENT_EXAMINE_START, .name = "examine-start" },
158 { .value = TARGET_EVENT_EXAMINE_END, .name = "examine-end" },
160 { .value = TARGET_EVENT_DEBUG_HALTED, .name = "debug-halted" },
161 { .value = TARGET_EVENT_DEBUG_RESUMED, .name = "debug-resumed" },
163 { .value = TARGET_EVENT_GDB_ATTACH, .name = "gdb-attach" },
164 { .value = TARGET_EVENT_GDB_DETACH, .name = "gdb-detach" },
166 { .value = TARGET_EVENT_GDB_FLASH_WRITE_START, .name = "gdb-flash-write-start" },
167 { .value = TARGET_EVENT_GDB_FLASH_WRITE_END , .name = "gdb-flash-write-end" },
169 { .value = TARGET_EVENT_GDB_FLASH_ERASE_START, .name = "gdb-flash-erase-start" },
170 { .value = TARGET_EVENT_GDB_FLASH_ERASE_END , .name = "gdb-flash-erase-end" },
172 { .value = TARGET_EVENT_RESUME_START, .name = "resume-start" },
173 { .value = TARGET_EVENT_RESUMED , .name = "resume-ok" },
174 { .value = TARGET_EVENT_RESUME_END , .name = "resume-end" },
176 { .name = NULL, .value = -1 }
179 const Jim_Nvp nvp_target_state[] = {
180 { .name = "unknown", .value = TARGET_UNKNOWN },
181 { .name = "running", .value = TARGET_RUNNING },
182 { .name = "halted", .value = TARGET_HALTED },
183 { .name = "reset", .value = TARGET_RESET },
184 { .name = "debug-running", .value = TARGET_DEBUG_RUNNING },
185 { .name = NULL, .value = -1 },
188 const Jim_Nvp nvp_target_debug_reason [] = {
189 { .name = "debug-request" , .value = DBG_REASON_DBGRQ },
190 { .name = "breakpoint" , .value = DBG_REASON_BREAKPOINT },
191 { .name = "watchpoint" , .value = DBG_REASON_WATCHPOINT },
192 { .name = "watchpoint-and-breakpoint", .value = DBG_REASON_WPTANDBKPT },
193 { .name = "single-step" , .value = DBG_REASON_SINGLESTEP },
194 { .name = "target-not-halted" , .value = DBG_REASON_NOTHALTED },
195 { .name = "undefined" , .value = DBG_REASON_UNDEFINED },
196 { .name = NULL, .value = -1 },
199 const Jim_Nvp nvp_target_endian[] = {
200 { .name = "big", .value = TARGET_BIG_ENDIAN },
201 { .name = "little", .value = TARGET_LITTLE_ENDIAN },
202 { .name = "be", .value = TARGET_BIG_ENDIAN },
203 { .name = "le", .value = TARGET_LITTLE_ENDIAN },
204 { .name = NULL, .value = -1 },
207 const Jim_Nvp nvp_reset_modes[] = {
208 { .name = "unknown", .value = RESET_UNKNOWN },
209 { .name = "run" , .value = RESET_RUN },
210 { .name = "halt" , .value = RESET_HALT },
211 { .name = "init" , .value = RESET_INIT },
212 { .name = NULL , .value = -1 },
216 target_state_name( target_t *t )
219 cp = Jim_Nvp_value2name_simple(nvp_target_state, t->state)->name;
221 LOG_ERROR("Invalid target state: %d", (int)(t->state));
222 cp = "(*BUG*unknown*BUG*)";
227 /* determine the number of the new target */
228 static int new_target_number(void)
233 /* number is 0 based */
237 if (x < t->target_number) {
238 x = t->target_number;
245 /* read a uint32_t from a buffer in target memory endianness */
246 uint32_t target_buffer_get_u32(target_t *target, const uint8_t *buffer)
248 if (target->endianness == TARGET_LITTLE_ENDIAN)
249 return le_to_h_u32(buffer);
251 return be_to_h_u32(buffer);
254 /* read a uint16_t from a buffer in target memory endianness */
255 uint16_t target_buffer_get_u16(target_t *target, const uint8_t *buffer)
257 if (target->endianness == TARGET_LITTLE_ENDIAN)
258 return le_to_h_u16(buffer);
260 return be_to_h_u16(buffer);
263 /* read a uint8_t from a buffer in target memory endianness */
264 uint8_t target_buffer_get_u8(target_t *target, const uint8_t *buffer)
266 return *buffer & 0x0ff;
269 /* write a uint32_t to a buffer in target memory endianness */
270 void target_buffer_set_u32(target_t *target, uint8_t *buffer, uint32_t value)
272 if (target->endianness == TARGET_LITTLE_ENDIAN)
273 h_u32_to_le(buffer, value);
275 h_u32_to_be(buffer, value);
278 /* write a uint16_t to a buffer in target memory endianness */
279 void target_buffer_set_u16(target_t *target, uint8_t *buffer, uint16_t value)
281 if (target->endianness == TARGET_LITTLE_ENDIAN)
282 h_u16_to_le(buffer, value);
284 h_u16_to_be(buffer, value);
287 /* write a uint8_t to a buffer in target memory endianness */
288 void target_buffer_set_u8(target_t *target, uint8_t *buffer, uint8_t value)
293 /* return a pointer to a configured target; id is name or number */
294 target_t *get_target(const char *id)
298 /* try as tcltarget name */
299 for (target = all_targets; target; target = target->next) {
300 if (target->cmd_name == NULL)
302 if (strcmp(id, target->cmd_name) == 0)
306 /* It's OK to remove this fallback sometime after August 2010 or so */
308 /* no match, try as number */
310 if (parse_uint(id, &num) != ERROR_OK)
313 for (target = all_targets; target; target = target->next) {
314 if (target->target_number == (int)num) {
315 LOG_WARNING("use '%s' as target identifier, not '%u'",
316 target->cmd_name, num);
324 /* returns a pointer to the n-th configured target */
325 static target_t *get_target_by_num(int num)
327 target_t *target = all_targets;
330 if (target->target_number == num) {
333 target = target->next;
339 target_t* get_current_target(command_context_t *cmd_ctx)
341 target_t *target = get_target_by_num(cmd_ctx->current_target);
345 LOG_ERROR("BUG: current_target out of bounds");
352 int target_poll(struct target_s *target)
356 /* We can't poll until after examine */
357 if (!target_was_examined(target))
359 /* Fail silently lest we pollute the log */
363 retval = target->type->poll(target);
364 if (retval != ERROR_OK)
367 if (target->halt_issued)
369 if (target->state == TARGET_HALTED)
371 target->halt_issued = false;
374 long long t = timeval_ms() - target->halt_issued_time;
377 target->halt_issued = false;
378 LOG_INFO("Halt timed out, wake up GDB.");
379 target_call_event_callbacks(target, TARGET_EVENT_GDB_HALT);
387 int target_halt(struct target_s *target)
390 /* We can't poll until after examine */
391 if (!target_was_examined(target))
393 LOG_ERROR("Target not examined yet");
397 retval = target->type->halt(target);
398 if (retval != ERROR_OK)
401 target->halt_issued = true;
402 target->halt_issued_time = timeval_ms();
407 int target_resume(struct target_s *target, int current, uint32_t address, int handle_breakpoints, int debug_execution)
411 /* We can't poll until after examine */
412 if (!target_was_examined(target))
414 LOG_ERROR("Target not examined yet");
418 /* note that resume *must* be asynchronous. The CPU can halt before we poll. The CPU can
419 * even halt at the current PC as a result of a software breakpoint being inserted by (a bug?)
422 if ((retval = target->type->resume(target, current, address, handle_breakpoints, debug_execution)) != ERROR_OK)
428 int target_process_reset(struct command_context_s *cmd_ctx, enum target_reset_mode reset_mode)
433 n = Jim_Nvp_value2name_simple(nvp_reset_modes, reset_mode);
434 if (n->name == NULL) {
435 LOG_ERROR("invalid reset mode");
439 /* disable polling during reset to make reset event scripts
440 * more predictable, i.e. dr/irscan & pathmove in events will
441 * not have JTAG operations injected into the middle of a sequence.
443 bool save_poll = jtag_poll_get_enabled();
445 jtag_poll_set_enabled(false);
447 sprintf(buf, "ocd_process_reset %s", n->name);
448 retval = Jim_Eval(interp, buf);
450 jtag_poll_set_enabled(save_poll);
452 if (retval != JIM_OK) {
453 Jim_PrintErrorMessage(interp);
457 /* We want any events to be processed before the prompt */
458 retval = target_call_timer_callbacks_now();
463 static int identity_virt2phys(struct target_s *target,
464 uint32_t virtual, uint32_t *physical)
470 static int no_mmu(struct target_s *target, int *enabled)
476 static int default_examine(struct target_s *target)
478 target_set_examined(target);
482 int target_examine_one(struct target_s *target)
484 return target->type->examine(target);
487 static int jtag_enable_callback(enum jtag_event event, void *priv)
489 target_t *target = priv;
491 if (event != JTAG_TAP_EVENT_ENABLE || !target->tap->enabled)
494 jtag_unregister_event_callback(jtag_enable_callback, target);
495 return target_examine_one(target);
499 /* Targets that correctly implement init + examine, i.e.
500 * no communication with target during init:
504 int target_examine(void)
506 int retval = ERROR_OK;
509 for (target = all_targets; target; target = target->next)
511 /* defer examination, but don't skip it */
512 if (!target->tap->enabled) {
513 jtag_register_event_callback(jtag_enable_callback,
517 if ((retval = target_examine_one(target)) != ERROR_OK)
522 const char *target_get_name(struct target_s *target)
524 return target->type->name;
527 static int target_write_memory_imp(struct target_s *target, uint32_t address, uint32_t size, uint32_t count, uint8_t *buffer)
529 if (!target_was_examined(target))
531 LOG_ERROR("Target not examined yet");
534 return target->type->write_memory_imp(target, address, size, count, buffer);
537 static int target_read_memory_imp(struct target_s *target, uint32_t address, uint32_t size, uint32_t count, uint8_t *buffer)
539 if (!target_was_examined(target))
541 LOG_ERROR("Target not examined yet");
544 return target->type->read_memory_imp(target, address, size, count, buffer);
547 static int target_soft_reset_halt_imp(struct target_s *target)
549 if (!target_was_examined(target))
551 LOG_ERROR("Target not examined yet");
554 if (!target->type->soft_reset_halt_imp) {
555 LOG_ERROR("Target %s does not support soft_reset_halt",
559 return target->type->soft_reset_halt_imp(target);
562 static int target_run_algorithm_imp(struct target_s *target, int num_mem_params, mem_param_t *mem_params, int num_reg_params, reg_param_t *reg_param, uint32_t entry_point, uint32_t exit_point, int timeout_ms, void *arch_info)
564 if (!target_was_examined(target))
566 LOG_ERROR("Target not examined yet");
569 return target->type->run_algorithm_imp(target, num_mem_params, mem_params, num_reg_params, reg_param, entry_point, exit_point, timeout_ms, arch_info);
572 int target_read_memory(struct target_s *target,
573 uint32_t address, uint32_t size, uint32_t count, uint8_t *buffer)
575 return target->type->read_memory(target, address, size, count, buffer);
578 int target_read_phys_memory(struct target_s *target,
579 uint32_t address, uint32_t size, uint32_t count, uint8_t *buffer)
581 return target->type->read_phys_memory(target, address, size, count, buffer);
584 int target_write_memory(struct target_s *target,
585 uint32_t address, uint32_t size, uint32_t count, uint8_t *buffer)
587 return target->type->write_memory(target, address, size, count, buffer);
590 int target_write_phys_memory(struct target_s *target,
591 uint32_t address, uint32_t size, uint32_t count, uint8_t *buffer)
593 return target->type->write_phys_memory(target, address, size, count, buffer);
596 int target_bulk_write_memory(struct target_s *target,
597 uint32_t address, uint32_t count, uint8_t *buffer)
599 return target->type->bulk_write_memory(target, address, count, buffer);
602 int target_add_breakpoint(struct target_s *target,
603 struct breakpoint_s *breakpoint)
605 return target->type->add_breakpoint(target, breakpoint);
607 int target_remove_breakpoint(struct target_s *target,
608 struct breakpoint_s *breakpoint)
610 return target->type->remove_breakpoint(target, breakpoint);
613 int target_add_watchpoint(struct target_s *target,
614 struct watchpoint_s *watchpoint)
616 return target->type->add_watchpoint(target, watchpoint);
618 int target_remove_watchpoint(struct target_s *target,
619 struct watchpoint_s *watchpoint)
621 return target->type->remove_watchpoint(target, watchpoint);
624 int target_get_gdb_reg_list(struct target_s *target,
625 struct reg_s **reg_list[], int *reg_list_size)
627 return target->type->get_gdb_reg_list(target, reg_list, reg_list_size);
629 int target_step(struct target_s *target,
630 int current, uint32_t address, int handle_breakpoints)
632 return target->type->step(target, current, address, handle_breakpoints);
636 int target_run_algorithm(struct target_s *target,
637 int num_mem_params, mem_param_t *mem_params,
638 int num_reg_params, reg_param_t *reg_param,
639 uint32_t entry_point, uint32_t exit_point,
640 int timeout_ms, void *arch_info)
642 return target->type->run_algorithm(target,
643 num_mem_params, mem_params, num_reg_params, reg_param,
644 entry_point, exit_point, timeout_ms, arch_info);
647 /// @returns @c true if the target has been examined.
648 bool target_was_examined(struct target_s *target)
650 return target->type->examined;
652 /// Sets the @c examined flag for the given target.
653 void target_set_examined(struct target_s *target)
655 target->type->examined = true;
657 // Reset the @c examined flag for the given target.
658 void target_reset_examined(struct target_s *target)
660 target->type->examined = false;
665 static int default_mrc(struct target_s *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 default_mcr(struct target_s *target, int cpnum, uint32_t op1, uint32_t op2, uint32_t CRn, uint32_t CRm, uint32_t value)
673 LOG_ERROR("Not implemented: %s", __func__);
677 static int arm_cp_check(struct target_s *target, int cpnum, uint32_t op1, uint32_t op2, uint32_t CRn, uint32_t CRm)
680 if (!target_was_examined(target))
682 LOG_ERROR("Target not examined yet");
686 if ((cpnum <0) || (cpnum > 15))
688 LOG_ERROR("Illegal co-processor %d", cpnum);
694 LOG_ERROR("Illegal op1");
700 LOG_ERROR("Illegal op2");
706 LOG_ERROR("Illegal CRn");
712 LOG_ERROR("Illegal CRm");
719 int target_mrc(struct target_s *target, int cpnum, uint32_t op1, uint32_t op2, uint32_t CRn, uint32_t CRm, uint32_t *value)
723 retval = arm_cp_check(target, cpnum, op1, op2, CRn, CRm);
724 if (retval != ERROR_OK)
727 return target->type->mrc(target, cpnum, op1, op2, CRn, CRm, value);
730 int target_mcr(struct target_s *target, int cpnum, uint32_t op1, uint32_t op2, uint32_t CRn, uint32_t CRm, uint32_t value)
734 retval = arm_cp_check(target, cpnum, op1, op2, CRn, CRm);
735 if (retval != ERROR_OK)
738 return target->type->mcr(target, cpnum, op1, op2, CRn, CRm, value);
742 err_read_phys_memory(struct target_s *target, uint32_t address,
743 uint32_t size, uint32_t count, uint8_t *buffer)
745 LOG_ERROR("Not implemented: %s", __func__);
750 err_write_phys_memory(struct target_s *target, uint32_t address,
751 uint32_t size, uint32_t count, uint8_t *buffer)
753 LOG_ERROR("Not implemented: %s", __func__);
757 int target_init(struct command_context_s *cmd_ctx)
759 target_t *target = all_targets;
764 target_reset_examined(target);
765 if (target->type->examine == NULL)
767 target->type->examine = default_examine;
770 if ((retval = target->type->init_target(cmd_ctx, target)) != ERROR_OK)
772 LOG_ERROR("target '%s' init failed", target_get_name(target));
776 /* Set up default functions if none are provided by target */
777 if (target->type->virt2phys == NULL)
779 target->type->virt2phys = identity_virt2phys;
782 if (target->type->read_phys_memory == NULL)
784 target->type->read_phys_memory = err_read_phys_memory;
787 if (target->type->write_phys_memory == NULL)
789 target->type->write_phys_memory = err_write_phys_memory;
793 * @todo MCR/MRC are ARM-specific; don't require them in
794 * all targets, or for ARMs without coprocessors.
796 if (target->type->mcr == NULL)
798 target->type->mcr = default_mcr;
801 /* FIX! multiple targets will generally register global commands
802 * multiple times. Only register this one if *one* of the
803 * targets need the command. Hmm... make it a command on the
804 * Jim Tcl target object?
806 register_jim(cmd_ctx, "mcr", jim_mcrmrc, "write coprocessor <cpnum> <op1> <op2> <CRn> <CRm> <value>");
809 if (target->type->mrc == NULL)
811 target->type->mrc = default_mrc;
814 register_jim(cmd_ctx, "mrc", jim_mcrmrc, "read coprocessor <cpnum> <op1> <op2> <CRn> <CRm>");
819 * @todo get rid of those *memory_imp() methods, now that all
820 * callers are using target_*_memory() accessors ... and make
821 * sure the "physical" paths handle the same issues.
824 /* a non-invasive way(in terms of patches) to add some code that
825 * runs before the type->write/read_memory implementation
827 target->type->write_memory_imp = target->type->write_memory;
828 target->type->write_memory = target_write_memory_imp;
829 target->type->read_memory_imp = target->type->read_memory;
830 target->type->read_memory = target_read_memory_imp;
831 target->type->soft_reset_halt_imp = target->type->soft_reset_halt;
832 target->type->soft_reset_halt = target_soft_reset_halt_imp;
833 target->type->run_algorithm_imp = target->type->run_algorithm;
834 target->type->run_algorithm = target_run_algorithm_imp;
836 if (target->type->mmu == NULL)
838 target->type->mmu = no_mmu;
840 target = target->next;
845 if ((retval = target_register_user_commands(cmd_ctx)) != ERROR_OK)
847 if ((retval = target_register_timer_callback(handle_target, 100, 1, NULL)) != ERROR_OK)
854 int target_register_event_callback(int (*callback)(struct target_s *target, enum target_event event, void *priv), void *priv)
856 target_event_callback_t **callbacks_p = &target_event_callbacks;
858 if (callback == NULL)
860 return ERROR_INVALID_ARGUMENTS;
865 while ((*callbacks_p)->next)
866 callbacks_p = &((*callbacks_p)->next);
867 callbacks_p = &((*callbacks_p)->next);
870 (*callbacks_p) = malloc(sizeof(target_event_callback_t));
871 (*callbacks_p)->callback = callback;
872 (*callbacks_p)->priv = priv;
873 (*callbacks_p)->next = NULL;
878 int target_register_timer_callback(int (*callback)(void *priv), int time_ms, int periodic, void *priv)
880 target_timer_callback_t **callbacks_p = &target_timer_callbacks;
883 if (callback == NULL)
885 return ERROR_INVALID_ARGUMENTS;
890 while ((*callbacks_p)->next)
891 callbacks_p = &((*callbacks_p)->next);
892 callbacks_p = &((*callbacks_p)->next);
895 (*callbacks_p) = malloc(sizeof(target_timer_callback_t));
896 (*callbacks_p)->callback = callback;
897 (*callbacks_p)->periodic = periodic;
898 (*callbacks_p)->time_ms = time_ms;
900 gettimeofday(&now, NULL);
901 (*callbacks_p)->when.tv_usec = now.tv_usec + (time_ms % 1000) * 1000;
902 time_ms -= (time_ms % 1000);
903 (*callbacks_p)->when.tv_sec = now.tv_sec + (time_ms / 1000);
904 if ((*callbacks_p)->when.tv_usec > 1000000)
906 (*callbacks_p)->when.tv_usec = (*callbacks_p)->when.tv_usec - 1000000;
907 (*callbacks_p)->when.tv_sec += 1;
910 (*callbacks_p)->priv = priv;
911 (*callbacks_p)->next = NULL;
916 int target_unregister_event_callback(int (*callback)(struct target_s *target, enum target_event event, void *priv), void *priv)
918 target_event_callback_t **p = &target_event_callbacks;
919 target_event_callback_t *c = target_event_callbacks;
921 if (callback == NULL)
923 return ERROR_INVALID_ARGUMENTS;
928 target_event_callback_t *next = c->next;
929 if ((c->callback == callback) && (c->priv == priv))
943 int target_unregister_timer_callback(int (*callback)(void *priv), void *priv)
945 target_timer_callback_t **p = &target_timer_callbacks;
946 target_timer_callback_t *c = target_timer_callbacks;
948 if (callback == NULL)
950 return ERROR_INVALID_ARGUMENTS;
955 target_timer_callback_t *next = c->next;
956 if ((c->callback == callback) && (c->priv == priv))
970 int target_call_event_callbacks(target_t *target, enum target_event event)
972 target_event_callback_t *callback = target_event_callbacks;
973 target_event_callback_t *next_callback;
975 if (event == TARGET_EVENT_HALTED)
977 /* execute early halted first */
978 target_call_event_callbacks(target, TARGET_EVENT_GDB_HALT);
981 LOG_DEBUG("target event %i (%s)",
983 Jim_Nvp_value2name_simple(nvp_target_event, event)->name);
985 target_handle_event(target, event);
989 next_callback = callback->next;
990 callback->callback(target, event, callback->priv);
991 callback = next_callback;
997 static int target_timer_callback_periodic_restart(
998 target_timer_callback_t *cb, struct timeval *now)
1000 int time_ms = cb->time_ms;
1001 cb->when.tv_usec = now->tv_usec + (time_ms % 1000) * 1000;
1002 time_ms -= (time_ms % 1000);
1003 cb->when.tv_sec = now->tv_sec + time_ms / 1000;
1004 if (cb->when.tv_usec > 1000000)
1006 cb->when.tv_usec = cb->when.tv_usec - 1000000;
1007 cb->when.tv_sec += 1;
1012 static int target_call_timer_callback(target_timer_callback_t *cb,
1013 struct timeval *now)
1015 cb->callback(cb->priv);
1018 return target_timer_callback_periodic_restart(cb, now);
1020 return target_unregister_timer_callback(cb->callback, cb->priv);
1023 static int target_call_timer_callbacks_check_time(int checktime)
1028 gettimeofday(&now, NULL);
1030 target_timer_callback_t *callback = target_timer_callbacks;
1033 // cleaning up may unregister and free this callback
1034 target_timer_callback_t *next_callback = callback->next;
1036 bool call_it = callback->callback &&
1037 ((!checktime && callback->periodic) ||
1038 now.tv_sec > callback->when.tv_sec ||
1039 (now.tv_sec == callback->when.tv_sec &&
1040 now.tv_usec >= callback->when.tv_usec));
1044 int retval = target_call_timer_callback(callback, &now);
1045 if (retval != ERROR_OK)
1049 callback = next_callback;
1055 int target_call_timer_callbacks(void)
1057 return target_call_timer_callbacks_check_time(1);
1060 /* invoke periodic callbacks immediately */
1061 int target_call_timer_callbacks_now(void)
1063 return target_call_timer_callbacks_check_time(0);
1066 int target_alloc_working_area(struct target_s *target, uint32_t size, working_area_t **area)
1068 working_area_t *c = target->working_areas;
1069 working_area_t *new_wa = NULL;
1071 /* Reevaluate working area address based on MMU state*/
1072 if (target->working_areas == NULL)
1077 retval = target->type->mmu(target, &enabled);
1078 if (retval != ERROR_OK)
1084 if (target->working_area_phys_spec) {
1085 LOG_DEBUG("MMU disabled, using physical "
1086 "address for working memory 0x%08x",
1087 (unsigned)target->working_area_phys);
1088 target->working_area = target->working_area_phys;
1090 LOG_ERROR("No working memory available. "
1091 "Specify -work-area-phys to target.");
1092 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
1095 if (target->working_area_virt_spec) {
1096 LOG_DEBUG("MMU enabled, using virtual "
1097 "address for working memory 0x%08x",
1098 (unsigned)target->working_area_virt);
1099 target->working_area = target->working_area_virt;
1101 LOG_ERROR("No working memory available. "
1102 "Specify -work-area-virt to target.");
1103 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
1108 /* only allocate multiples of 4 byte */
1111 LOG_ERROR("BUG: code tried to allocate unaligned number of bytes (0x%08x), padding", ((unsigned)(size)));
1112 size = (size + 3) & (~3);
1115 /* see if there's already a matching working area */
1118 if ((c->free) && (c->size == size))
1126 /* if not, allocate a new one */
1129 working_area_t **p = &target->working_areas;
1130 uint32_t first_free = target->working_area;
1131 uint32_t free_size = target->working_area_size;
1133 c = target->working_areas;
1136 first_free += c->size;
1137 free_size -= c->size;
1142 if (free_size < size)
1144 LOG_WARNING("not enough working area available(requested %u, free %u)",
1145 (unsigned)(size), (unsigned)(free_size));
1146 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
1149 LOG_DEBUG("allocated new working area at address 0x%08x", (unsigned)first_free);
1151 new_wa = malloc(sizeof(working_area_t));
1152 new_wa->next = NULL;
1153 new_wa->size = size;
1154 new_wa->address = first_free;
1156 if (target->backup_working_area)
1159 new_wa->backup = malloc(new_wa->size);
1160 if ((retval = target_read_memory(target, new_wa->address, 4, new_wa->size / 4, new_wa->backup)) != ERROR_OK)
1162 free(new_wa->backup);
1169 new_wa->backup = NULL;
1172 /* put new entry in list */
1176 /* mark as used, and return the new (reused) area */
1181 new_wa->user = area;
1186 int target_free_working_area_restore(struct target_s *target, working_area_t *area, int restore)
1191 if (restore && target->backup_working_area)
1194 if ((retval = target_write_memory(target, area->address, 4, area->size / 4, area->backup)) != ERROR_OK)
1200 /* mark user pointer invalid */
1207 int target_free_working_area(struct target_s *target, working_area_t *area)
1209 return target_free_working_area_restore(target, area, 1);
1212 /* free resources and restore memory, if restoring memory fails,
1213 * free up resources anyway
1215 void target_free_all_working_areas_restore(struct target_s *target, int restore)
1217 working_area_t *c = target->working_areas;
1221 working_area_t *next = c->next;
1222 target_free_working_area_restore(target, c, restore);
1232 target->working_areas = NULL;
1235 void target_free_all_working_areas(struct target_s *target)
1237 target_free_all_working_areas_restore(target, 1);
1240 int target_arch_state(struct target_s *target)
1245 LOG_USER("No target has been configured");
1249 LOG_USER("target state: %s", target_state_name( target ));
1251 if (target->state != TARGET_HALTED)
1254 retval = target->type->arch_state(target);
1258 /* Single aligned words are guaranteed to use 16 or 32 bit access
1259 * mode respectively, otherwise data is handled as quickly as
1262 int target_write_buffer(struct target_s *target, uint32_t address, uint32_t size, uint8_t *buffer)
1265 LOG_DEBUG("writing buffer of %i byte at 0x%8.8x",
1266 (int)size, (unsigned)address);
1268 if (!target_was_examined(target))
1270 LOG_ERROR("Target not examined yet");
1278 if ((address + size - 1) < address)
1280 /* GDB can request this when e.g. PC is 0xfffffffc*/
1281 LOG_ERROR("address + size wrapped(0x%08x, 0x%08x)",
1287 if (((address % 2) == 0) && (size == 2))
1289 return target_write_memory(target, address, 2, 1, buffer);
1292 /* handle unaligned head bytes */
1295 uint32_t unaligned = 4 - (address % 4);
1297 if (unaligned > size)
1300 if ((retval = target_write_memory(target, address, 1, unaligned, buffer)) != ERROR_OK)
1303 buffer += unaligned;
1304 address += unaligned;
1308 /* handle aligned words */
1311 int aligned = size - (size % 4);
1313 /* use bulk writes above a certain limit. This may have to be changed */
1316 if ((retval = target->type->bulk_write_memory(target, address, aligned / 4, buffer)) != ERROR_OK)
1321 if ((retval = target_write_memory(target, address, 4, aligned / 4, buffer)) != ERROR_OK)
1330 /* handle tail writes of less than 4 bytes */
1333 if ((retval = target_write_memory(target, address, 1, size, buffer)) != ERROR_OK)
1340 /* Single aligned words are guaranteed to use 16 or 32 bit access
1341 * mode respectively, otherwise data is handled as quickly as
1344 int target_read_buffer(struct target_s *target, uint32_t address, uint32_t size, uint8_t *buffer)
1347 LOG_DEBUG("reading buffer of %i byte at 0x%8.8x",
1348 (int)size, (unsigned)address);
1350 if (!target_was_examined(target))
1352 LOG_ERROR("Target not examined yet");
1360 if ((address + size - 1) < address)
1362 /* GDB can request this when e.g. PC is 0xfffffffc*/
1363 LOG_ERROR("address + size wrapped(0x%08" PRIx32 ", 0x%08" PRIx32 ")",
1369 if (((address % 2) == 0) && (size == 2))
1371 return target_read_memory(target, address, 2, 1, buffer);
1374 /* handle unaligned head bytes */
1377 uint32_t unaligned = 4 - (address % 4);
1379 if (unaligned > size)
1382 if ((retval = target_read_memory(target, address, 1, unaligned, buffer)) != ERROR_OK)
1385 buffer += unaligned;
1386 address += unaligned;
1390 /* handle aligned words */
1393 int aligned = size - (size % 4);
1395 if ((retval = target_read_memory(target, address, 4, aligned / 4, buffer)) != ERROR_OK)
1403 /*prevent byte access when possible (avoid AHB access limitations in some cases)*/
1406 int aligned = size - (size%2);
1407 retval = target_read_memory(target, address, 2, aligned / 2, buffer);
1408 if (retval != ERROR_OK)
1415 /* handle tail writes of less than 4 bytes */
1418 if ((retval = target_read_memory(target, address, 1, size, buffer)) != ERROR_OK)
1425 int target_checksum_memory(struct target_s *target, uint32_t address, uint32_t size, uint32_t* crc)
1430 uint32_t checksum = 0;
1431 if (!target_was_examined(target))
1433 LOG_ERROR("Target not examined yet");
1437 if ((retval = target->type->checksum_memory(target, address,
1438 size, &checksum)) != ERROR_OK)
1440 buffer = malloc(size);
1443 LOG_ERROR("error allocating buffer for section (%d bytes)", (int)size);
1444 return ERROR_INVALID_ARGUMENTS;
1446 retval = target_read_buffer(target, address, size, buffer);
1447 if (retval != ERROR_OK)
1453 /* convert to target endianess */
1454 for (i = 0; i < (size/sizeof(uint32_t)); i++)
1456 uint32_t target_data;
1457 target_data = target_buffer_get_u32(target, &buffer[i*sizeof(uint32_t)]);
1458 target_buffer_set_u32(target, &buffer[i*sizeof(uint32_t)], target_data);
1461 retval = image_calculate_checksum(buffer, size, &checksum);
1470 int target_blank_check_memory(struct target_s *target, uint32_t address, uint32_t size, uint32_t* blank)
1473 if (!target_was_examined(target))
1475 LOG_ERROR("Target not examined yet");
1479 if (target->type->blank_check_memory == 0)
1480 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
1482 retval = target->type->blank_check_memory(target, address, size, blank);
1487 int target_read_u32(struct target_s *target, uint32_t address, uint32_t *value)
1489 uint8_t value_buf[4];
1490 if (!target_was_examined(target))
1492 LOG_ERROR("Target not examined yet");
1496 int retval = target_read_memory(target, address, 4, 1, value_buf);
1498 if (retval == ERROR_OK)
1500 *value = target_buffer_get_u32(target, value_buf);
1501 LOG_DEBUG("address: 0x%8.8" PRIx32 ", value: 0x%8.8" PRIx32 "",
1508 LOG_DEBUG("address: 0x%8.8" PRIx32 " failed",
1515 int target_read_u16(struct target_s *target, uint32_t address, uint16_t *value)
1517 uint8_t value_buf[2];
1518 if (!target_was_examined(target))
1520 LOG_ERROR("Target not examined yet");
1524 int retval = target_read_memory(target, address, 2, 1, value_buf);
1526 if (retval == ERROR_OK)
1528 *value = target_buffer_get_u16(target, value_buf);
1529 LOG_DEBUG("address: 0x%8.8" PRIx32 ", value: 0x%4.4x",
1536 LOG_DEBUG("address: 0x%8.8" PRIx32 " failed",
1543 int target_read_u8(struct target_s *target, uint32_t address, uint8_t *value)
1545 int retval = target_read_memory(target, address, 1, 1, value);
1546 if (!target_was_examined(target))
1548 LOG_ERROR("Target not examined yet");
1552 if (retval == ERROR_OK)
1554 LOG_DEBUG("address: 0x%8.8" PRIx32 ", value: 0x%2.2x",
1561 LOG_DEBUG("address: 0x%8.8" PRIx32 " failed",
1568 int target_write_u32(struct target_s *target, uint32_t address, uint32_t value)
1571 uint8_t value_buf[4];
1572 if (!target_was_examined(target))
1574 LOG_ERROR("Target not examined yet");
1578 LOG_DEBUG("address: 0x%8.8" PRIx32 ", value: 0x%8.8" PRIx32 "",
1582 target_buffer_set_u32(target, value_buf, value);
1583 if ((retval = target_write_memory(target, address, 4, 1, value_buf)) != ERROR_OK)
1585 LOG_DEBUG("failed: %i", retval);
1591 int target_write_u16(struct target_s *target, uint32_t address, uint16_t value)
1594 uint8_t value_buf[2];
1595 if (!target_was_examined(target))
1597 LOG_ERROR("Target not examined yet");
1601 LOG_DEBUG("address: 0x%8.8" PRIx32 ", value: 0x%8.8x",
1605 target_buffer_set_u16(target, value_buf, value);
1606 if ((retval = target_write_memory(target, address, 2, 1, value_buf)) != ERROR_OK)
1608 LOG_DEBUG("failed: %i", retval);
1614 int target_write_u8(struct target_s *target, uint32_t address, uint8_t value)
1617 if (!target_was_examined(target))
1619 LOG_ERROR("Target not examined yet");
1623 LOG_DEBUG("address: 0x%8.8" PRIx32 ", value: 0x%2.2x",
1626 if ((retval = target_write_memory(target, address, 1, 1, &value)) != ERROR_OK)
1628 LOG_DEBUG("failed: %i", retval);
1634 static int handle_targets_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1636 target_t *target = all_targets;
1640 target = get_target(args[0]);
1641 if (target == NULL) {
1642 command_print(cmd_ctx,"Target: %s is unknown, try one of:\n", args[0]);
1645 if (!target->tap->enabled) {
1646 command_print(cmd_ctx,"Target: TAP %s is disabled, "
1647 "can't be the current target\n",
1648 target->tap->dotted_name);
1652 cmd_ctx->current_target = target->target_number;
1657 target = all_targets;
1658 command_print(cmd_ctx, " TargetName Type Endian TapName State ");
1659 command_print(cmd_ctx, "-- ------------------ ---------- ------ ------------------ ------------");
1665 if (target->tap->enabled)
1666 state = target_state_name( target );
1668 state = "tap-disabled";
1670 if (cmd_ctx->current_target == target->target_number)
1673 /* keep columns lined up to match the headers above */
1674 command_print(cmd_ctx, "%2d%c %-18s %-10s %-6s %-18s %s",
1675 target->target_number,
1678 target_get_name(target),
1679 Jim_Nvp_value2name_simple(nvp_target_endian,
1680 target->endianness)->name,
1681 target->tap->dotted_name,
1683 target = target->next;
1689 /* every 300ms we check for reset & powerdropout and issue a "reset halt" if so. */
1691 static int powerDropout;
1692 static int srstAsserted;
1694 static int runPowerRestore;
1695 static int runPowerDropout;
1696 static int runSrstAsserted;
1697 static int runSrstDeasserted;
1699 static int sense_handler(void)
1701 static int prevSrstAsserted = 0;
1702 static int prevPowerdropout = 0;
1705 if ((retval = jtag_power_dropout(&powerDropout)) != ERROR_OK)
1709 powerRestored = prevPowerdropout && !powerDropout;
1712 runPowerRestore = 1;
1715 long long current = timeval_ms();
1716 static long long lastPower = 0;
1717 int waitMore = lastPower + 2000 > current;
1718 if (powerDropout && !waitMore)
1720 runPowerDropout = 1;
1721 lastPower = current;
1724 if ((retval = jtag_srst_asserted(&srstAsserted)) != ERROR_OK)
1728 srstDeasserted = prevSrstAsserted && !srstAsserted;
1730 static long long lastSrst = 0;
1731 waitMore = lastSrst + 2000 > current;
1732 if (srstDeasserted && !waitMore)
1734 runSrstDeasserted = 1;
1738 if (!prevSrstAsserted && srstAsserted)
1740 runSrstAsserted = 1;
1743 prevSrstAsserted = srstAsserted;
1744 prevPowerdropout = powerDropout;
1746 if (srstDeasserted || powerRestored)
1748 /* Other than logging the event we can't do anything here.
1749 * Issuing a reset is a particularly bad idea as we might
1750 * be inside a reset already.
1757 static void target_call_event_callbacks_all(enum target_event e) {
1759 target = all_targets;
1761 target_call_event_callbacks(target, e);
1762 target = target->next;
1766 /* process target state changes */
1767 int handle_target(void *priv)
1769 int retval = ERROR_OK;
1771 /* we do not want to recurse here... */
1772 static int recursive = 0;
1777 /* danger! running these procedures can trigger srst assertions and power dropouts.
1778 * We need to avoid an infinite loop/recursion here and we do that by
1779 * clearing the flags after running these events.
1781 int did_something = 0;
1782 if (runSrstAsserted)
1784 target_call_event_callbacks_all(TARGET_EVENT_GDB_HALT);
1785 Jim_Eval(interp, "srst_asserted");
1788 if (runSrstDeasserted)
1790 Jim_Eval(interp, "srst_deasserted");
1793 if (runPowerDropout)
1795 target_call_event_callbacks_all(TARGET_EVENT_GDB_HALT);
1796 Jim_Eval(interp, "power_dropout");
1799 if (runPowerRestore)
1801 Jim_Eval(interp, "power_restore");
1807 /* clear detect flags */
1811 /* clear action flags */
1813 runSrstAsserted = 0;
1814 runSrstDeasserted = 0;
1815 runPowerRestore = 0;
1816 runPowerDropout = 0;
1821 /* Poll targets for state changes unless that's globally disabled.
1822 * Skip targets that are currently disabled.
1824 for (target_t *target = all_targets;
1825 is_jtag_poll_safe() && target;
1826 target = target->next)
1828 if (!target->tap->enabled)
1831 /* only poll target if we've got power and srst isn't asserted */
1832 if (!powerDropout && !srstAsserted)
1834 /* polling may fail silently until the target has been examined */
1835 if ((retval = target_poll(target)) != ERROR_OK)
1837 target_call_event_callbacks(target, TARGET_EVENT_GDB_HALT);
1846 static int handle_reg_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1855 target = get_current_target(cmd_ctx);
1857 /* list all available registers for the current target */
1860 reg_cache_t *cache = target->reg_cache;
1867 command_print(cmd_ctx, "===== %s", cache->name);
1869 for (i = 0, reg = cache->reg_list;
1870 i < cache->num_regs;
1871 i++, reg++, count++)
1873 /* only print cached values if they are valid */
1875 value = buf_to_str(reg->value,
1877 command_print(cmd_ctx,
1878 "(%i) %s (/%" PRIu32 "): 0x%s%s",
1886 command_print(cmd_ctx, "(%i) %s (/%" PRIu32 ")",
1891 cache = cache->next;
1897 /* access a single register by its ordinal number */
1898 if ((args[0][0] >= '0') && (args[0][0] <= '9'))
1901 COMMAND_PARSE_NUMBER(uint, args[0], num);
1903 reg_cache_t *cache = target->reg_cache;
1908 for (i = 0; i < cache->num_regs; i++)
1910 if (count++ == (int)num)
1912 reg = &cache->reg_list[i];
1918 cache = cache->next;
1923 command_print(cmd_ctx, "%i is out of bounds, the current target has only %i registers (0 - %i)", num, count, count - 1);
1926 } else /* access a single register by its name */
1928 reg = register_get_by_name(target->reg_cache, args[0], 1);
1932 command_print(cmd_ctx, "register %s not found in current target", args[0]);
1937 /* display a register */
1938 if ((argc == 1) || ((argc == 2) && !((args[1][0] >= '0') && (args[1][0] <= '9'))))
1940 if ((argc == 2) && (strcmp(args[1], "force") == 0))
1943 if (reg->valid == 0)
1945 reg_arch_type_t *arch_type = register_get_arch_type(reg->arch_type);
1946 arch_type->get(reg);
1948 value = buf_to_str(reg->value, reg->size, 16);
1949 command_print(cmd_ctx, "%s (/%i): 0x%s", reg->name, (int)(reg->size), value);
1954 /* set register value */
1957 uint8_t *buf = malloc(CEIL(reg->size, 8));
1958 str_to_buf(args[1], strlen(args[1]), buf, reg->size, 0);
1960 reg_arch_type_t *arch_type = register_get_arch_type(reg->arch_type);
1961 arch_type->set(reg, buf);
1963 value = buf_to_str(reg->value, reg->size, 16);
1964 command_print(cmd_ctx, "%s (/%i): 0x%s", reg->name, (int)(reg->size), value);
1972 command_print(cmd_ctx, "usage: reg <#|name> [value]");
1977 static int handle_poll_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1979 int retval = ERROR_OK;
1980 target_t *target = get_current_target(cmd_ctx);
1984 command_print(cmd_ctx, "background polling: %s",
1985 jtag_poll_get_enabled() ? "on" : "off");
1986 command_print(cmd_ctx, "TAP: %s (%s)",
1987 target->tap->dotted_name,
1988 target->tap->enabled ? "enabled" : "disabled");
1989 if (!target->tap->enabled)
1991 if ((retval = target_poll(target)) != ERROR_OK)
1993 if ((retval = target_arch_state(target)) != ERROR_OK)
1999 if (strcmp(args[0], "on") == 0)
2001 jtag_poll_set_enabled(true);
2003 else if (strcmp(args[0], "off") == 0)
2005 jtag_poll_set_enabled(false);
2009 command_print(cmd_ctx, "arg is \"on\" or \"off\"");
2013 return ERROR_COMMAND_SYNTAX_ERROR;
2019 static int handle_wait_halt_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2022 return ERROR_COMMAND_SYNTAX_ERROR;
2027 int retval = parse_uint(args[0], &ms);
2028 if (ERROR_OK != retval)
2030 command_print(cmd_ctx, "usage: %s [seconds]", cmd);
2031 return ERROR_COMMAND_SYNTAX_ERROR;
2033 // convert seconds (given) to milliseconds (needed)
2037 target_t *target = get_current_target(cmd_ctx);
2038 return target_wait_state(target, TARGET_HALTED, ms);
2041 /* wait for target state to change. The trick here is to have a low
2042 * latency for short waits and not to suck up all the CPU time
2045 * After 500ms, keep_alive() is invoked
2047 int target_wait_state(target_t *target, enum target_state state, int ms)
2050 long long then = 0, cur;
2055 if ((retval = target_poll(target)) != ERROR_OK)
2057 if (target->state == state)
2065 then = timeval_ms();
2066 LOG_DEBUG("waiting for target %s...",
2067 Jim_Nvp_value2name_simple(nvp_target_state,state)->name);
2075 if ((cur-then) > ms)
2077 LOG_ERROR("timed out while waiting for target %s",
2078 Jim_Nvp_value2name_simple(nvp_target_state,state)->name);
2086 static int handle_halt_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2090 target_t *target = get_current_target(cmd_ctx);
2091 int retval = target_halt(target);
2092 if (ERROR_OK != retval)
2098 retval = parse_uint(args[0], &wait);
2099 if (ERROR_OK != retval)
2100 return ERROR_COMMAND_SYNTAX_ERROR;
2105 return handle_wait_halt_command(cmd_ctx, cmd, args, argc);
2108 static int handle_soft_reset_halt_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2110 target_t *target = get_current_target(cmd_ctx);
2112 LOG_USER("requesting target halt and executing a soft reset");
2114 target->type->soft_reset_halt(target);
2119 static int handle_reset_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2122 return ERROR_COMMAND_SYNTAX_ERROR;
2124 enum target_reset_mode reset_mode = RESET_RUN;
2128 n = Jim_Nvp_name2value_simple(nvp_reset_modes, args[0]);
2129 if ((n->name == NULL) || (n->value == RESET_UNKNOWN)) {
2130 return ERROR_COMMAND_SYNTAX_ERROR;
2132 reset_mode = n->value;
2135 /* reset *all* targets */
2136 return target_process_reset(cmd_ctx, reset_mode);
2140 static int handle_resume_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2144 return ERROR_COMMAND_SYNTAX_ERROR;
2146 target_t *target = get_current_target(cmd_ctx);
2147 target_handle_event(target, TARGET_EVENT_OLD_pre_resume);
2149 /* with no args, resume from current pc, addr = 0,
2150 * with one arguments, addr = args[0],
2151 * handle breakpoints, not debugging */
2155 COMMAND_PARSE_NUMBER(u32, args[0], addr);
2159 return target_resume(target, current, addr, 1, 0);
2162 static int handle_step_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2165 return ERROR_COMMAND_SYNTAX_ERROR;
2169 /* with no args, step from current pc, addr = 0,
2170 * with one argument addr = args[0],
2171 * handle breakpoints, debugging */
2176 COMMAND_PARSE_NUMBER(u32, args[0], addr);
2180 target_t *target = get_current_target(cmd_ctx);
2182 return target->type->step(target, current_pc, addr, 1);
2185 static void handle_md_output(struct command_context_s *cmd_ctx,
2186 struct target_s *target, uint32_t address, unsigned size,
2187 unsigned count, const uint8_t *buffer)
2189 const unsigned line_bytecnt = 32;
2190 unsigned line_modulo = line_bytecnt / size;
2192 char output[line_bytecnt * 4 + 1];
2193 unsigned output_len = 0;
2195 const char *value_fmt;
2197 case 4: value_fmt = "%8.8x "; break;
2198 case 2: value_fmt = "%4.2x "; break;
2199 case 1: value_fmt = "%2.2x "; break;
2201 LOG_ERROR("invalid memory read size: %u", size);
2205 for (unsigned i = 0; i < count; i++)
2207 if (i % line_modulo == 0)
2209 output_len += snprintf(output + output_len,
2210 sizeof(output) - output_len,
2212 (unsigned)(address + (i*size)));
2216 const uint8_t *value_ptr = buffer + i * size;
2218 case 4: value = target_buffer_get_u32(target, value_ptr); break;
2219 case 2: value = target_buffer_get_u16(target, value_ptr); break;
2220 case 1: value = *value_ptr;
2222 output_len += snprintf(output + output_len,
2223 sizeof(output) - output_len,
2226 if ((i % line_modulo == line_modulo - 1) || (i == count - 1))
2228 command_print(cmd_ctx, "%s", output);
2234 static int handle_md_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2237 return ERROR_COMMAND_SYNTAX_ERROR;
2241 case 'w': size = 4; break;
2242 case 'h': size = 2; break;
2243 case 'b': size = 1; break;
2244 default: return ERROR_COMMAND_SYNTAX_ERROR;
2247 bool physical=strcmp(args[0], "phys")==0;
2248 int (*fn)(struct target_s *target,
2249 uint32_t address, uint32_t size, uint32_t count, uint8_t *buffer);
2254 fn=target_read_phys_memory;
2257 fn=target_read_memory;
2259 if ((argc < 1) || (argc > 2))
2261 return ERROR_COMMAND_SYNTAX_ERROR;
2265 COMMAND_PARSE_NUMBER(u32, args[0], address);
2269 COMMAND_PARSE_NUMBER(uint, args[1], count);
2271 uint8_t *buffer = calloc(count, size);
2273 target_t *target = get_current_target(cmd_ctx);
2274 int retval = fn(target, address, size, count, buffer);
2275 if (ERROR_OK == retval)
2276 handle_md_output(cmd_ctx, target, address, size, count, buffer);
2283 static int handle_mw_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2287 return ERROR_COMMAND_SYNTAX_ERROR;
2289 bool physical=strcmp(args[0], "phys")==0;
2290 int (*fn)(struct target_s *target,
2291 uint32_t address, uint32_t size, uint32_t count, uint8_t *buffer);
2296 fn=target_write_phys_memory;
2299 fn=target_write_memory;
2301 if ((argc < 2) || (argc > 3))
2302 return ERROR_COMMAND_SYNTAX_ERROR;
2305 COMMAND_PARSE_NUMBER(u32, args[0], address);
2308 COMMAND_PARSE_NUMBER(u32, args[1], value);
2312 COMMAND_PARSE_NUMBER(uint, args[2], count);
2314 target_t *target = get_current_target(cmd_ctx);
2316 uint8_t value_buf[4];
2321 target_buffer_set_u32(target, value_buf, value);
2325 target_buffer_set_u16(target, value_buf, value);
2329 value_buf[0] = value;
2332 return ERROR_COMMAND_SYNTAX_ERROR;
2334 for (unsigned i = 0; i < count; i++)
2336 int retval = fn(target,
2337 address + i * wordsize, wordsize, 1, value_buf);
2338 if (ERROR_OK != retval)
2347 static int parse_load_image_command_args(struct command_context_s *cmd_ctx,
2348 char **args, int argc, image_t *image,
2349 uint32_t *min_address, uint32_t *max_address)
2351 if (argc < 1 || argc > 5)
2352 return ERROR_COMMAND_SYNTAX_ERROR;
2354 /* a base address isn't always necessary,
2355 * default to 0x0 (i.e. don't relocate) */
2359 COMMAND_PARSE_NUMBER(u32, args[1], addr);
2360 image->base_address = addr;
2361 image->base_address_set = 1;
2364 image->base_address_set = 0;
2366 image->start_address_set = 0;
2370 COMMAND_PARSE_NUMBER(u32, args[3], *min_address);
2374 COMMAND_PARSE_NUMBER(u32, args[4], *max_address);
2375 // use size (given) to find max (required)
2376 *max_address += *min_address;
2379 if (*min_address > *max_address)
2380 return ERROR_COMMAND_SYNTAX_ERROR;
2385 static int handle_load_image_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2389 uint32_t image_size;
2390 uint32_t min_address = 0;
2391 uint32_t max_address = 0xffffffff;
2395 int retval = parse_load_image_command_args(cmd_ctx, args, argc,
2396 &image, &min_address, &max_address);
2397 if (ERROR_OK != retval)
2400 target_t *target = get_current_target(cmd_ctx);
2402 struct duration bench;
2403 duration_start(&bench);
2405 if (image_open(&image, args[0], (argc >= 3) ? args[2] : NULL) != ERROR_OK)
2412 for (i = 0; i < image.num_sections; i++)
2414 buffer = malloc(image.sections[i].size);
2417 command_print(cmd_ctx,
2418 "error allocating buffer for section (%d bytes)",
2419 (int)(image.sections[i].size));
2423 if ((retval = image_read_section(&image, i, 0x0, image.sections[i].size, buffer, &buf_cnt)) != ERROR_OK)
2429 uint32_t offset = 0;
2430 uint32_t length = buf_cnt;
2432 /* DANGER!!! beware of unsigned comparision here!!! */
2434 if ((image.sections[i].base_address + buf_cnt >= min_address)&&
2435 (image.sections[i].base_address < max_address))
2437 if (image.sections[i].base_address < min_address)
2439 /* clip addresses below */
2440 offset += min_address-image.sections[i].base_address;
2444 if (image.sections[i].base_address + buf_cnt > max_address)
2446 length -= (image.sections[i].base_address + buf_cnt)-max_address;
2449 if ((retval = target_write_buffer(target, image.sections[i].base_address + offset, length, buffer + offset)) != ERROR_OK)
2454 image_size += length;
2455 command_print(cmd_ctx, "%u bytes written at address 0x%8.8" PRIx32 "",
2456 (unsigned int)length,
2457 image.sections[i].base_address + offset);
2463 if ((ERROR_OK == retval) && (duration_measure(&bench) == ERROR_OK))
2465 command_print(cmd_ctx, "downloaded %" PRIu32 " bytes "
2466 "in %fs (%0.3f kb/s)", image_size,
2467 duration_elapsed(&bench), duration_kbps(&bench, image_size));
2470 image_close(&image);
2476 static int handle_dump_image_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2480 uint8_t buffer[560];
2484 target_t *target = get_current_target(cmd_ctx);
2488 command_print(cmd_ctx, "usage: dump_image <filename> <address> <size>");
2493 COMMAND_PARSE_NUMBER(u32, args[1], address);
2495 COMMAND_PARSE_NUMBER(u32, args[2], size);
2497 if (fileio_open(&fileio, args[0], FILEIO_WRITE, FILEIO_BINARY) != ERROR_OK)
2502 struct duration bench;
2503 duration_start(&bench);
2505 int retval = ERROR_OK;
2508 uint32_t size_written;
2509 uint32_t this_run_size = (size > 560) ? 560 : size;
2510 retval = target_read_buffer(target, address, this_run_size, buffer);
2511 if (retval != ERROR_OK)
2516 retval = fileio_write(&fileio, this_run_size, buffer, &size_written);
2517 if (retval != ERROR_OK)
2522 size -= this_run_size;
2523 address += this_run_size;
2526 if ((retvaltemp = fileio_close(&fileio)) != ERROR_OK)
2529 if ((ERROR_OK == retval) && (duration_measure(&bench) == ERROR_OK))
2531 command_print(cmd_ctx,
2532 "dumped %lld bytes in %fs (%0.3f kb/s)", fileio.size,
2533 duration_elapsed(&bench), duration_kbps(&bench, fileio.size));
2539 static int handle_verify_image_command_internal(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc, int verify)
2543 uint32_t image_size;
2546 uint32_t checksum = 0;
2547 uint32_t mem_checksum = 0;
2551 target_t *target = get_current_target(cmd_ctx);
2555 return ERROR_COMMAND_SYNTAX_ERROR;
2560 LOG_ERROR("no target selected");
2564 struct duration bench;
2565 duration_start(&bench);
2570 COMMAND_PARSE_NUMBER(u32, args[1], addr);
2571 image.base_address = addr;
2572 image.base_address_set = 1;
2576 image.base_address_set = 0;
2577 image.base_address = 0x0;
2580 image.start_address_set = 0;
2582 if ((retval = image_open(&image, args[0], (argc == 3) ? args[2] : NULL)) != ERROR_OK)
2589 for (i = 0; i < image.num_sections; i++)
2591 buffer = malloc(image.sections[i].size);
2594 command_print(cmd_ctx,
2595 "error allocating buffer for section (%d bytes)",
2596 (int)(image.sections[i].size));
2599 if ((retval = image_read_section(&image, i, 0x0, image.sections[i].size, buffer, &buf_cnt)) != ERROR_OK)
2607 /* calculate checksum of image */
2608 image_calculate_checksum(buffer, buf_cnt, &checksum);
2610 retval = target_checksum_memory(target, image.sections[i].base_address, buf_cnt, &mem_checksum);
2611 if (retval != ERROR_OK)
2617 if (checksum != mem_checksum)
2619 /* failed crc checksum, fall back to a binary compare */
2622 command_print(cmd_ctx, "checksum mismatch - attempting binary compare");
2624 data = (uint8_t*)malloc(buf_cnt);
2626 /* Can we use 32bit word accesses? */
2628 int count = buf_cnt;
2629 if ((count % 4) == 0)
2634 retval = target_read_memory(target, image.sections[i].base_address, size, count, data);
2635 if (retval == ERROR_OK)
2638 for (t = 0; t < buf_cnt; t++)
2640 if (data[t] != buffer[t])
2642 command_print(cmd_ctx,
2643 "Verify operation failed address 0x%08x. Was 0x%02x instead of 0x%02x\n",
2644 (unsigned)(t + image.sections[i].base_address),
2649 retval = ERROR_FAIL;
2663 command_print(cmd_ctx, "address 0x%08" PRIx32 " length 0x%08" PRIx32 "",
2664 image.sections[i].base_address,
2669 image_size += buf_cnt;
2672 if ((ERROR_OK == retval) && (duration_measure(&bench) == ERROR_OK))
2674 command_print(cmd_ctx, "verified %" PRIu32 " bytes "
2675 "in %fs (%0.3f kb/s)", image_size,
2676 duration_elapsed(&bench), duration_kbps(&bench, image_size));
2679 image_close(&image);
2684 static int handle_verify_image_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2686 return handle_verify_image_command_internal(cmd_ctx, cmd, args, argc, 1);
2689 static int handle_test_image_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2691 return handle_verify_image_command_internal(cmd_ctx, cmd, args, argc, 0);
2694 static int handle_bp_command_list(struct command_context_s *cmd_ctx)
2696 target_t *target = get_current_target(cmd_ctx);
2697 breakpoint_t *breakpoint = target->breakpoints;
2700 if (breakpoint->type == BKPT_SOFT)
2702 char* buf = buf_to_str(breakpoint->orig_instr,
2703 breakpoint->length, 16);
2704 command_print(cmd_ctx, "0x%8.8" PRIx32 ", 0x%x, %i, 0x%s",
2705 breakpoint->address,
2707 breakpoint->set, buf);
2712 command_print(cmd_ctx, "0x%8.8" PRIx32 ", 0x%x, %i",
2713 breakpoint->address,
2714 breakpoint->length, breakpoint->set);
2717 breakpoint = breakpoint->next;
2722 static int handle_bp_command_set(struct command_context_s *cmd_ctx,
2723 uint32_t addr, uint32_t length, int hw)
2725 target_t *target = get_current_target(cmd_ctx);
2726 int retval = breakpoint_add(target, addr, length, hw);
2727 if (ERROR_OK == retval)
2728 command_print(cmd_ctx, "breakpoint set at 0x%8.8" PRIx32 "", addr);
2730 LOG_ERROR("Failure setting breakpoint");
2734 static int handle_bp_command(struct command_context_s *cmd_ctx,
2735 char *cmd, char **args, int argc)
2738 return handle_bp_command_list(cmd_ctx);
2740 if (argc < 2 || argc > 3)
2742 command_print(cmd_ctx, "usage: bp <address> <length> ['hw']");
2743 return ERROR_COMMAND_SYNTAX_ERROR;
2747 COMMAND_PARSE_NUMBER(u32, args[0], addr);
2749 COMMAND_PARSE_NUMBER(u32, args[1], length);
2754 if (strcmp(args[2], "hw") == 0)
2757 return ERROR_COMMAND_SYNTAX_ERROR;
2760 return handle_bp_command_set(cmd_ctx, addr, length, hw);
2763 static int handle_rbp_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2766 return ERROR_COMMAND_SYNTAX_ERROR;
2769 COMMAND_PARSE_NUMBER(u32, args[0], addr);
2771 target_t *target = get_current_target(cmd_ctx);
2772 breakpoint_remove(target, addr);
2777 static int handle_wp_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2779 target_t *target = get_current_target(cmd_ctx);
2783 watchpoint_t *watchpoint = target->watchpoints;
2787 command_print(cmd_ctx,
2788 "address: 0x%8.8" PRIx32 ", len: 0x%8.8x, r/w/a: %i, value: 0x%8.8" PRIx32 ", mask: 0x%8.8" PRIx32 "",
2789 watchpoint->address,
2791 (int)(watchpoint->rw),
2794 watchpoint = watchpoint->next;
2799 enum watchpoint_rw type = WPT_ACCESS;
2801 uint32_t length = 0;
2802 uint32_t data_value = 0x0;
2803 uint32_t data_mask = 0xffffffff;
2808 COMMAND_PARSE_NUMBER(u32, args[4], data_mask);
2811 COMMAND_PARSE_NUMBER(u32, args[3], data_value);
2826 LOG_ERROR("invalid watchpoint mode ('%c')", args[2][0]);
2827 return ERROR_COMMAND_SYNTAX_ERROR;
2831 COMMAND_PARSE_NUMBER(u32, args[1], length);
2832 COMMAND_PARSE_NUMBER(u32, args[0], addr);
2836 command_print(cmd_ctx, "usage: wp [address length "
2837 "[(r|w|a) [value [mask]]]]");
2838 return ERROR_COMMAND_SYNTAX_ERROR;
2841 int retval = watchpoint_add(target, addr, length, type,
2842 data_value, data_mask);
2843 if (ERROR_OK != retval)
2844 LOG_ERROR("Failure setting watchpoints");
2849 static int handle_rwp_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2852 return ERROR_COMMAND_SYNTAX_ERROR;
2855 COMMAND_PARSE_NUMBER(u32, args[0], addr);
2857 target_t *target = get_current_target(cmd_ctx);
2858 watchpoint_remove(target, addr);
2865 * Translate a virtual address to a physical address.
2867 * The low-level target implementation must have logged a detailed error
2868 * which is forwarded to telnet/GDB session.
2870 static int handle_virt2phys_command(command_context_t *cmd_ctx,
2871 char *cmd, char **args, int argc)
2874 return ERROR_COMMAND_SYNTAX_ERROR;
2877 COMMAND_PARSE_NUMBER(u32, args[0], va);
2880 target_t *target = get_current_target(cmd_ctx);
2881 int retval = target->type->virt2phys(target, va, &pa);
2882 if (retval == ERROR_OK)
2883 command_print(cmd_ctx, "Physical address 0x%08" PRIx32 "", pa);
2888 static void writeData(FILE *f, const void *data, size_t len)
2890 size_t written = fwrite(data, 1, len, f);
2892 LOG_ERROR("failed to write %zu bytes: %s", len, strerror(errno));
2895 static void writeLong(FILE *f, int l)
2898 for (i = 0; i < 4; i++)
2900 char c = (l >> (i*8))&0xff;
2901 writeData(f, &c, 1);
2906 static void writeString(FILE *f, char *s)
2908 writeData(f, s, strlen(s));
2911 /* Dump a gmon.out histogram file. */
2912 static void writeGmon(uint32_t *samples, uint32_t sampleNum, char *filename)
2915 FILE *f = fopen(filename, "w");
2918 writeString(f, "gmon");
2919 writeLong(f, 0x00000001); /* Version */
2920 writeLong(f, 0); /* padding */
2921 writeLong(f, 0); /* padding */
2922 writeLong(f, 0); /* padding */
2924 uint8_t zero = 0; /* GMON_TAG_TIME_HIST */
2925 writeData(f, &zero, 1);
2927 /* figure out bucket size */
2928 uint32_t min = samples[0];
2929 uint32_t max = samples[0];
2930 for (i = 0; i < sampleNum; i++)
2932 if (min > samples[i])
2936 if (max < samples[i])
2942 int addressSpace = (max-min + 1);
2944 static const uint32_t maxBuckets = 256 * 1024; /* maximum buckets. */
2945 uint32_t length = addressSpace;
2946 if (length > maxBuckets)
2948 length = maxBuckets;
2950 int *buckets = malloc(sizeof(int)*length);
2951 if (buckets == NULL)
2956 memset(buckets, 0, sizeof(int)*length);
2957 for (i = 0; i < sampleNum;i++)
2959 uint32_t address = samples[i];
2960 long long a = address-min;
2961 long long b = length-1;
2962 long long c = addressSpace-1;
2963 int index = (a*b)/c; /* danger!!!! int32 overflows */
2967 /* append binary memory gmon.out &profile_hist_hdr ((char*)&profile_hist_hdr + sizeof(struct gmon_hist_hdr)) */
2968 writeLong(f, min); /* low_pc */
2969 writeLong(f, max); /* high_pc */
2970 writeLong(f, length); /* # of samples */
2971 writeLong(f, 64000000); /* 64MHz */
2972 writeString(f, "seconds");
2973 for (i = 0; i < (15-strlen("seconds")); i++)
2974 writeData(f, &zero, 1);
2975 writeString(f, "s");
2977 /*append binary memory gmon.out profile_hist_data (profile_hist_data + profile_hist_hdr.hist_size) */
2979 char *data = malloc(2*length);
2982 for (i = 0; i < length;i++)
2991 data[i*2 + 1]=(val >> 8)&0xff;
2994 writeData(f, data, length * 2);
3004 /* profiling samples the CPU PC as quickly as OpenOCD is able, which will be used as a random sampling of PC */
3005 static int handle_profile_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
3007 target_t *target = get_current_target(cmd_ctx);
3008 struct timeval timeout, now;
3010 gettimeofday(&timeout, NULL);
3013 return ERROR_COMMAND_SYNTAX_ERROR;
3016 COMMAND_PARSE_NUMBER(uint, args[0], offset);
3018 timeval_add_time(&timeout, offset, 0);
3020 command_print(cmd_ctx, "Starting profiling. Halting and resuming the target as often as we can...");
3022 static const int maxSample = 10000;
3023 uint32_t *samples = malloc(sizeof(uint32_t)*maxSample);
3024 if (samples == NULL)
3028 /* hopefully it is safe to cache! We want to stop/restart as quickly as possible. */
3029 reg_t *reg = register_get_by_name(target->reg_cache, "pc", 1);
3034 target_poll(target);
3035 if (target->state == TARGET_HALTED)
3037 uint32_t t=*((uint32_t *)reg->value);
3038 samples[numSamples++]=t;
3039 retval = target_resume(target, 1, 0, 0, 0); /* current pc, addr = 0, do not handle breakpoints, not debugging */
3040 target_poll(target);
3041 alive_sleep(10); /* sleep 10ms, i.e. <100 samples/second. */
3042 } else if (target->state == TARGET_RUNNING)
3044 /* We want to quickly sample the PC. */
3045 if ((retval = target_halt(target)) != ERROR_OK)
3052 command_print(cmd_ctx, "Target not halted or running");
3056 if (retval != ERROR_OK)
3061 gettimeofday(&now, NULL);
3062 if ((numSamples >= maxSample) || ((now.tv_sec >= timeout.tv_sec) && (now.tv_usec >= timeout.tv_usec)))
3064 command_print(cmd_ctx, "Profiling completed. %d samples.", numSamples);
3065 if ((retval = target_poll(target)) != ERROR_OK)
3070 if (target->state == TARGET_HALTED)
3072 target_resume(target, 1, 0, 0, 0); /* current pc, addr = 0, do not handle breakpoints, not debugging */
3074 if ((retval = target_poll(target)) != ERROR_OK)
3079 writeGmon(samples, numSamples, args[1]);
3080 command_print(cmd_ctx, "Wrote %s", args[1]);
3089 static int new_int_array_element(Jim_Interp * interp, const char *varname, int idx, uint32_t val)
3092 Jim_Obj *nameObjPtr, *valObjPtr;
3095 namebuf = alloc_printf("%s(%d)", varname, idx);
3099 nameObjPtr = Jim_NewStringObj(interp, namebuf, -1);
3100 valObjPtr = Jim_NewIntObj(interp, val);
3101 if (!nameObjPtr || !valObjPtr)
3107 Jim_IncrRefCount(nameObjPtr);
3108 Jim_IncrRefCount(valObjPtr);
3109 result = Jim_SetVariable(interp, nameObjPtr, valObjPtr);
3110 Jim_DecrRefCount(interp, nameObjPtr);
3111 Jim_DecrRefCount(interp, valObjPtr);
3113 /* printf("%s(%d) <= 0%08x\n", varname, idx, val); */
3117 static int jim_mem2array(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
3119 command_context_t *context;
3122 context = Jim_GetAssocData(interp, "context");
3123 if (context == NULL)
3125 LOG_ERROR("mem2array: no command context");
3128 target = get_current_target(context);
3131 LOG_ERROR("mem2array: no current target");
3135 return target_mem2array(interp, target, argc-1, argv + 1);
3138 static int target_mem2array(Jim_Interp *interp, target_t *target, int argc, Jim_Obj *const *argv)
3146 const char *varname;
3147 uint8_t buffer[4096];
3151 /* argv[1] = name of array to receive the data
3152 * argv[2] = desired width
3153 * argv[3] = memory address
3154 * argv[4] = count of times to read
3157 Jim_WrongNumArgs(interp, 1, argv, "varname width addr nelems");
3160 varname = Jim_GetString(argv[0], &len);
3161 /* given "foo" get space for worse case "foo(%d)" .. add 20 */
3163 e = Jim_GetLong(interp, argv[1], &l);
3169 e = Jim_GetLong(interp, argv[2], &l);
3174 e = Jim_GetLong(interp, argv[3], &l);
3190 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3191 Jim_AppendStrings(interp, Jim_GetResult(interp), "Invalid width param, must be 8/16/32", NULL);
3195 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3196 Jim_AppendStrings(interp, Jim_GetResult(interp), "mem2array: zero width read?", NULL);
3199 if ((addr + (len * width)) < addr) {
3200 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3201 Jim_AppendStrings(interp, Jim_GetResult(interp), "mem2array: addr + len - wraps to zero?", NULL);
3204 /* absurd transfer size? */
3206 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3207 Jim_AppendStrings(interp, Jim_GetResult(interp), "mem2array: absurd > 64K item request", NULL);
3212 ((width == 2) && ((addr & 1) == 0)) ||
3213 ((width == 4) && ((addr & 3) == 0))) {
3217 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3218 sprintf(buf, "mem2array address: 0x%08" PRIx32 " is not aligned for %" PRId32 " byte reads",
3221 Jim_AppendStrings(interp, Jim_GetResult(interp), buf , NULL);
3232 /* Slurp... in buffer size chunks */
3234 count = len; /* in objects.. */
3235 if (count > (sizeof(buffer)/width)) {
3236 count = (sizeof(buffer)/width);
3239 retval = target_read_memory(target, addr, width, count, buffer);
3240 if (retval != ERROR_OK) {
3242 LOG_ERROR("mem2array: Read @ 0x%08x, w=%d, cnt=%d, failed",
3246 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3247 Jim_AppendStrings(interp, Jim_GetResult(interp), "mem2array: cannot read memory", NULL);
3251 v = 0; /* shut up gcc */
3252 for (i = 0 ;i < count ;i++, n++) {
3255 v = target_buffer_get_u32(target, &buffer[i*width]);
3258 v = target_buffer_get_u16(target, &buffer[i*width]);
3261 v = buffer[i] & 0x0ff;
3264 new_int_array_element(interp, varname, n, v);
3270 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3275 static int get_int_array_element(Jim_Interp * interp, const char *varname, int idx, uint32_t *val)
3278 Jim_Obj *nameObjPtr, *valObjPtr;
3282 namebuf = alloc_printf("%s(%d)", varname, idx);
3286 nameObjPtr = Jim_NewStringObj(interp, namebuf, -1);
3293 Jim_IncrRefCount(nameObjPtr);
3294 valObjPtr = Jim_GetVariable(interp, nameObjPtr, JIM_ERRMSG);
3295 Jim_DecrRefCount(interp, nameObjPtr);
3297 if (valObjPtr == NULL)
3300 result = Jim_GetLong(interp, valObjPtr, &l);
3301 /* printf("%s(%d) => 0%08x\n", varname, idx, val); */
3306 static int jim_array2mem(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
3308 command_context_t *context;
3311 context = Jim_GetAssocData(interp, "context");
3312 if (context == NULL) {
3313 LOG_ERROR("array2mem: no command context");
3316 target = get_current_target(context);
3317 if (target == NULL) {
3318 LOG_ERROR("array2mem: no current target");
3322 return target_array2mem(interp,target, argc-1, argv + 1);
3324 static int target_array2mem(Jim_Interp *interp, target_t *target, int argc, Jim_Obj *const *argv)
3332 const char *varname;
3333 uint8_t buffer[4096];
3337 /* argv[1] = name of array to get the data
3338 * argv[2] = desired width
3339 * argv[3] = memory address
3340 * argv[4] = count to write
3343 Jim_WrongNumArgs(interp, 1, argv, "varname width addr nelems");
3346 varname = Jim_GetString(argv[0], &len);
3347 /* given "foo" get space for worse case "foo(%d)" .. add 20 */
3349 e = Jim_GetLong(interp, argv[1], &l);
3355 e = Jim_GetLong(interp, argv[2], &l);
3360 e = Jim_GetLong(interp, argv[3], &l);
3376 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3377 Jim_AppendStrings(interp, Jim_GetResult(interp), "Invalid width param, must be 8/16/32", NULL);
3381 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3382 Jim_AppendStrings(interp, Jim_GetResult(interp), "array2mem: zero width read?", NULL);
3385 if ((addr + (len * width)) < addr) {
3386 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3387 Jim_AppendStrings(interp, Jim_GetResult(interp), "array2mem: addr + len - wraps to zero?", NULL);
3390 /* absurd transfer size? */
3392 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3393 Jim_AppendStrings(interp, Jim_GetResult(interp), "array2mem: absurd > 64K item request", NULL);
3398 ((width == 2) && ((addr & 1) == 0)) ||
3399 ((width == 4) && ((addr & 3) == 0))) {
3403 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3404 sprintf(buf, "array2mem address: 0x%08x is not aligned for %d byte reads",
3407 Jim_AppendStrings(interp, Jim_GetResult(interp), buf , NULL);
3418 /* Slurp... in buffer size chunks */
3420 count = len; /* in objects.. */
3421 if (count > (sizeof(buffer)/width)) {
3422 count = (sizeof(buffer)/width);
3425 v = 0; /* shut up gcc */
3426 for (i = 0 ;i < count ;i++, n++) {
3427 get_int_array_element(interp, varname, n, &v);
3430 target_buffer_set_u32(target, &buffer[i*width], v);
3433 target_buffer_set_u16(target, &buffer[i*width], v);
3436 buffer[i] = v & 0x0ff;
3442 retval = target_write_memory(target, addr, width, count, buffer);
3443 if (retval != ERROR_OK) {
3445 LOG_ERROR("array2mem: Write @ 0x%08x, w=%d, cnt=%d, failed",
3449 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3450 Jim_AppendStrings(interp, Jim_GetResult(interp), "array2mem: cannot read memory", NULL);
3456 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3461 void target_all_handle_event(enum target_event e)
3465 LOG_DEBUG("**all*targets: event: %d, %s",
3467 Jim_Nvp_value2name_simple(nvp_target_event, e)->name);
3469 target = all_targets;
3471 target_handle_event(target, e);
3472 target = target->next;
3477 /* FIX? should we propagate errors here rather than printing them
3480 void target_handle_event(target_t *target, enum target_event e)
3482 target_event_action_t *teap;
3484 for (teap = target->event_action; teap != NULL; teap = teap->next) {
3485 if (teap->event == e) {
3486 LOG_DEBUG("target: (%d) %s (%s) event: %d (%s) action: %s",
3487 target->target_number,
3489 target_get_name(target),
3491 Jim_Nvp_value2name_simple(nvp_target_event, e)->name,
3492 Jim_GetString(teap->body, NULL));
3493 if (Jim_EvalObj(interp, teap->body) != JIM_OK)
3495 Jim_PrintErrorMessage(interp);
3501 enum target_cfg_param {
3504 TCFG_WORK_AREA_VIRT,
3505 TCFG_WORK_AREA_PHYS,
3506 TCFG_WORK_AREA_SIZE,
3507 TCFG_WORK_AREA_BACKUP,
3510 TCFG_CHAIN_POSITION,
3513 static Jim_Nvp nvp_config_opts[] = {
3514 { .name = "-type", .value = TCFG_TYPE },
3515 { .name = "-event", .value = TCFG_EVENT },
3516 { .name = "-work-area-virt", .value = TCFG_WORK_AREA_VIRT },
3517 { .name = "-work-area-phys", .value = TCFG_WORK_AREA_PHYS },
3518 { .name = "-work-area-size", .value = TCFG_WORK_AREA_SIZE },
3519 { .name = "-work-area-backup", .value = TCFG_WORK_AREA_BACKUP },
3520 { .name = "-endian" , .value = TCFG_ENDIAN },
3521 { .name = "-variant", .value = TCFG_VARIANT },
3522 { .name = "-chain-position", .value = TCFG_CHAIN_POSITION },
3524 { .name = NULL, .value = -1 }
3527 static int target_configure(Jim_GetOptInfo *goi, target_t *target)
3535 /* parse config or cget options ... */
3536 while (goi->argc > 0) {
3537 Jim_SetEmptyResult(goi->interp);
3538 /* Jim_GetOpt_Debug(goi); */
3540 if (target->type->target_jim_configure) {
3541 /* target defines a configure function */
3542 /* target gets first dibs on parameters */
3543 e = (*(target->type->target_jim_configure))(target, goi);
3552 /* otherwise we 'continue' below */
3554 e = Jim_GetOpt_Nvp(goi, nvp_config_opts, &n);
3556 Jim_GetOpt_NvpUnknown(goi, nvp_config_opts, 0);
3562 if (goi->isconfigure) {
3563 Jim_SetResult_sprintf(goi->interp, "not setable: %s", n->name);
3567 if (goi->argc != 0) {
3568 Jim_WrongNumArgs(goi->interp, goi->argc, goi->argv, "NO PARAMS");
3572 Jim_SetResultString(goi->interp, target_get_name(target), -1);
3576 if (goi->argc == 0) {
3577 Jim_WrongNumArgs(goi->interp, goi->argc, goi->argv, "-event ?event-name? ...");
3581 e = Jim_GetOpt_Nvp(goi, nvp_target_event, &n);
3583 Jim_GetOpt_NvpUnknown(goi, nvp_target_event, 1);
3587 if (goi->isconfigure) {
3588 if (goi->argc != 1) {
3589 Jim_WrongNumArgs(goi->interp, goi->argc, goi->argv, "-event ?event-name? ?EVENT-BODY?");
3593 if (goi->argc != 0) {
3594 Jim_WrongNumArgs(goi->interp, goi->argc, goi->argv, "-event ?event-name?");
3600 target_event_action_t *teap;
3602 teap = target->event_action;
3603 /* replace existing? */
3605 if (teap->event == (enum target_event)n->value) {
3611 if (goi->isconfigure) {
3612 bool replace = true;
3615 teap = calloc(1, sizeof(*teap));
3618 teap->event = n->value;
3619 Jim_GetOpt_Obj(goi, &o);
3621 Jim_DecrRefCount(interp, teap->body);
3623 teap->body = Jim_DuplicateObj(goi->interp, o);
3626 * Tcl/TK - "tk events" have a nice feature.
3627 * See the "BIND" command.
3628 * We should support that here.
3629 * You can specify %X and %Y in the event code.
3630 * The idea is: %T - target name.
3631 * The idea is: %N - target number
3632 * The idea is: %E - event name.
3634 Jim_IncrRefCount(teap->body);
3638 /* add to head of event list */
3639 teap->next = target->event_action;
3640 target->event_action = teap;
3642 Jim_SetEmptyResult(goi->interp);
3646 Jim_SetEmptyResult(goi->interp);
3648 Jim_SetResult(goi->interp, Jim_DuplicateObj(goi->interp, teap->body));
3655 case TCFG_WORK_AREA_VIRT:
3656 if (goi->isconfigure) {
3657 target_free_all_working_areas(target);
3658 e = Jim_GetOpt_Wide(goi, &w);
3662 target->working_area_virt = w;
3663 target->working_area_virt_spec = true;
3665 if (goi->argc != 0) {
3669 Jim_SetResult(interp, Jim_NewIntObj(goi->interp, target->working_area_virt));
3673 case TCFG_WORK_AREA_PHYS:
3674 if (goi->isconfigure) {
3675 target_free_all_working_areas(target);
3676 e = Jim_GetOpt_Wide(goi, &w);
3680 target->working_area_phys = w;
3681 target->working_area_phys_spec = true;
3683 if (goi->argc != 0) {
3687 Jim_SetResult(interp, Jim_NewIntObj(goi->interp, target->working_area_phys));
3691 case TCFG_WORK_AREA_SIZE:
3692 if (goi->isconfigure) {
3693 target_free_all_working_areas(target);
3694 e = Jim_GetOpt_Wide(goi, &w);
3698 target->working_area_size = w;
3700 if (goi->argc != 0) {
3704 Jim_SetResult(interp, Jim_NewIntObj(goi->interp, target->working_area_size));
3708 case TCFG_WORK_AREA_BACKUP:
3709 if (goi->isconfigure) {
3710 target_free_all_working_areas(target);
3711 e = Jim_GetOpt_Wide(goi, &w);
3715 /* make this exactly 1 or 0 */
3716 target->backup_working_area = (!!w);
3718 if (goi->argc != 0) {
3722 Jim_SetResult(interp, Jim_NewIntObj(goi->interp, target->backup_working_area));
3723 /* loop for more e*/
3727 if (goi->isconfigure) {
3728 e = Jim_GetOpt_Nvp(goi, nvp_target_endian, &n);
3730 Jim_GetOpt_NvpUnknown(goi, nvp_target_endian, 1);
3733 target->endianness = n->value;
3735 if (goi->argc != 0) {
3739 n = Jim_Nvp_value2name_simple(nvp_target_endian, target->endianness);
3740 if (n->name == NULL) {
3741 target->endianness = TARGET_LITTLE_ENDIAN;
3742 n = Jim_Nvp_value2name_simple(nvp_target_endian, target->endianness);
3744 Jim_SetResultString(goi->interp, n->name, -1);
3749 if (goi->isconfigure) {
3750 if (goi->argc < 1) {
3751 Jim_SetResult_sprintf(goi->interp,
3756 if (target->variant) {
3757 free((void *)(target->variant));
3759 e = Jim_GetOpt_String(goi, &cp, NULL);
3760 target->variant = strdup(cp);
3762 if (goi->argc != 0) {
3766 Jim_SetResultString(goi->interp, target->variant,-1);
3769 case TCFG_CHAIN_POSITION:
3770 if (goi->isconfigure) {
3773 target_free_all_working_areas(target);
3774 e = Jim_GetOpt_Obj(goi, &o);
3778 tap = jtag_tap_by_jim_obj(goi->interp, o);
3782 /* make this exactly 1 or 0 */
3785 if (goi->argc != 0) {
3789 Jim_SetResultString(interp, target->tap->dotted_name, -1);
3790 /* loop for more e*/
3793 } /* while (goi->argc) */
3796 /* done - we return */
3800 /** this is the 'tcl' handler for the target specific command */
3801 static int tcl_target_func(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
3806 uint8_t target_buf[32];
3809 struct command_context_s *cmd_ctx;
3816 TS_CMD_MWW, TS_CMD_MWH, TS_CMD_MWB,
3817 TS_CMD_MDW, TS_CMD_MDH, TS_CMD_MDB,
3818 TS_CMD_MRW, TS_CMD_MRH, TS_CMD_MRB,
3819 TS_CMD_MEM2ARRAY, TS_CMD_ARRAY2MEM,
3827 TS_CMD_INVOKE_EVENT,
3830 static const Jim_Nvp target_options[] = {
3831 { .name = "configure", .value = TS_CMD_CONFIGURE },
3832 { .name = "cget", .value = TS_CMD_CGET },
3833 { .name = "mww", .value = TS_CMD_MWW },
3834 { .name = "mwh", .value = TS_CMD_MWH },
3835 { .name = "mwb", .value = TS_CMD_MWB },
3836 { .name = "mdw", .value = TS_CMD_MDW },
3837 { .name = "mdh", .value = TS_CMD_MDH },
3838 { .name = "mdb", .value = TS_CMD_MDB },
3839 { .name = "mem2array", .value = TS_CMD_MEM2ARRAY },
3840 { .name = "array2mem", .value = TS_CMD_ARRAY2MEM },
3841 { .name = "eventlist", .value = TS_CMD_EVENTLIST },
3842 { .name = "curstate", .value = TS_CMD_CURSTATE },
3844 { .name = "arp_examine", .value = TS_CMD_EXAMINE },
3845 { .name = "arp_poll", .value = TS_CMD_POLL },
3846 { .name = "arp_reset", .value = TS_CMD_RESET },
3847 { .name = "arp_halt", .value = TS_CMD_HALT },
3848 { .name = "arp_waitstate", .value = TS_CMD_WAITSTATE },
3849 { .name = "invoke-event", .value = TS_CMD_INVOKE_EVENT },
3851 { .name = NULL, .value = -1 },
3854 /* go past the "command" */
3855 Jim_GetOpt_Setup(&goi, interp, argc-1, argv + 1);
3857 target = Jim_CmdPrivData(goi.interp);
3858 cmd_ctx = Jim_GetAssocData(goi.interp, "context");
3860 /* commands here are in an NVP table */
3861 e = Jim_GetOpt_Nvp(&goi, target_options, &n);
3863 Jim_GetOpt_NvpUnknown(&goi, target_options, 0);
3866 /* Assume blank result */
3867 Jim_SetEmptyResult(goi.interp);
3870 case TS_CMD_CONFIGURE:
3872 Jim_WrongNumArgs(goi.interp, goi.argc, goi.argv, "missing: -option VALUE ...");
3875 goi.isconfigure = 1;
3876 return target_configure(&goi, target);
3878 // some things take params
3880 Jim_WrongNumArgs(goi.interp, 0, goi.argv, "missing: ?-option?");
3883 goi.isconfigure = 0;
3884 return target_configure(&goi, target);
3892 * argv[3] = optional count.
3895 if ((goi.argc == 2) || (goi.argc == 3)) {
3899 Jim_SetResult_sprintf(goi.interp, "expected: %s ADDR DATA [COUNT]", n->name);
3903 e = Jim_GetOpt_Wide(&goi, &a);
3908 e = Jim_GetOpt_Wide(&goi, &b);
3912 if (goi.argc == 3) {
3913 e = Jim_GetOpt_Wide(&goi, &c);
3923 target_buffer_set_u32(target, target_buf, b);
3927 target_buffer_set_u16(target, target_buf, b);
3931 target_buffer_set_u8(target, target_buf, b);
3935 for (x = 0 ; x < c ; x++) {
3936 e = target_write_memory(target, a, b, 1, target_buf);
3937 if (e != ERROR_OK) {
3938 Jim_SetResult_sprintf(interp, "Error writing @ 0x%08x: %d\n", (int)(a), e);
3951 /* argv[0] = command
3953 * argv[2] = optional count
3955 if ((goi.argc == 2) || (goi.argc == 3)) {
3956 Jim_SetResult_sprintf(goi.interp, "expected: %s ADDR [COUNT]", n->name);
3959 e = Jim_GetOpt_Wide(&goi, &a);
3964 e = Jim_GetOpt_Wide(&goi, &c);
3971 b = 1; /* shut up gcc */
3984 /* convert to "bytes" */
3986 /* count is now in 'BYTES' */
3992 e = target_read_memory(target, a, b, y / b, target_buf);
3993 if (e != ERROR_OK) {
3994 Jim_SetResult_sprintf(interp, "error reading target @ 0x%08lx", (int)(a));
3998 Jim_fprintf(interp, interp->cookie_stdout, "0x%08x ", (int)(a));
4001 for (x = 0 ; (x < 16) && (x < y) ; x += 4) {
4002 z = target_buffer_get_u32(target, &(target_buf[ x * 4 ]));
4003 Jim_fprintf(interp, interp->cookie_stdout, "%08x ", (int)(z));
4005 for (; (x < 16) ; x += 4) {
4006 Jim_fprintf(interp, interp->cookie_stdout, " ");
4010 for (x = 0 ; (x < 16) && (x < y) ; x += 2) {
4011 z = target_buffer_get_u16(target, &(target_buf[ x * 2 ]));
4012 Jim_fprintf(interp, interp->cookie_stdout, "%04x ", (int)(z));
4014 for (; (x < 16) ; x += 2) {
4015 Jim_fprintf(interp, interp->cookie_stdout, " ");
4020 for (x = 0 ; (x < 16) && (x < y) ; x += 1) {
4021 z = target_buffer_get_u8(target, &(target_buf[ x * 4 ]));
4022 Jim_fprintf(interp, interp->cookie_stdout, "%02x ", (int)(z));
4024 for (; (x < 16) ; x += 1) {
4025 Jim_fprintf(interp, interp->cookie_stdout, " ");
4029 /* ascii-ify the bytes */
4030 for (x = 0 ; x < y ; x++) {
4031 if ((target_buf[x] >= 0x20) &&
4032 (target_buf[x] <= 0x7e)) {
4036 target_buf[x] = '.';
4041 target_buf[x] = ' ';
4046 /* print - with a newline */
4047 Jim_fprintf(interp, interp->cookie_stdout, "%s\n", target_buf);
4053 case TS_CMD_MEM2ARRAY:
4054 return target_mem2array(goi.interp, target, goi.argc, goi.argv);
4056 case TS_CMD_ARRAY2MEM:
4057 return target_array2mem(goi.interp, target, goi.argc, goi.argv);
4059 case TS_CMD_EXAMINE:
4061 Jim_WrongNumArgs(goi.interp, 2, argv, "[no parameters]");
4064 if (!target->tap->enabled)
4065 goto err_tap_disabled;
4066 e = target->type->examine(target);
4067 if (e != ERROR_OK) {
4068 Jim_SetResult_sprintf(interp, "examine-fails: %d", e);
4074 Jim_WrongNumArgs(goi.interp, 2, argv, "[no parameters]");
4077 if (!target->tap->enabled)
4078 goto err_tap_disabled;
4079 if (!(target_was_examined(target))) {
4080 e = ERROR_TARGET_NOT_EXAMINED;
4082 e = target->type->poll(target);
4084 if (e != ERROR_OK) {
4085 Jim_SetResult_sprintf(interp, "poll-fails: %d", e);
4092 if (goi.argc != 2) {
4093 Jim_WrongNumArgs(interp, 2, argv,
4094 "([tT]|[fF]|assert|deassert) BOOL");
4097 e = Jim_GetOpt_Nvp(&goi, nvp_assert, &n);
4099 Jim_GetOpt_NvpUnknown(&goi, nvp_assert, 1);
4102 /* the halt or not param */
4103 e = Jim_GetOpt_Wide(&goi, &a);
4107 if (!target->tap->enabled)
4108 goto err_tap_disabled;
4109 if (!target->type->assert_reset
4110 || !target->type->deassert_reset) {
4111 Jim_SetResult_sprintf(interp,
4112 "No target-specific reset for %s",
4116 /* determine if we should halt or not. */
4117 target->reset_halt = !!a;
4118 /* When this happens - all workareas are invalid. */
4119 target_free_all_working_areas_restore(target, 0);
4122 if (n->value == NVP_ASSERT) {
4123 e = target->type->assert_reset(target);
4125 e = target->type->deassert_reset(target);
4127 return (e == ERROR_OK) ? JIM_OK : JIM_ERR;
4130 Jim_WrongNumArgs(goi.interp, 0, argv, "halt [no parameters]");
4133 if (!target->tap->enabled)
4134 goto err_tap_disabled;
4135 e = target->type->halt(target);
4136 return (e == ERROR_OK) ? JIM_OK : JIM_ERR;
4137 case TS_CMD_WAITSTATE:
4138 /* params: <name> statename timeoutmsecs */
4139 if (goi.argc != 2) {
4140 Jim_SetResult_sprintf(goi.interp, "%s STATENAME TIMEOUTMSECS", n->name);
4143 e = Jim_GetOpt_Nvp(&goi, nvp_target_state, &n);
4145 Jim_GetOpt_NvpUnknown(&goi, nvp_target_state,1);
4148 e = Jim_GetOpt_Wide(&goi, &a);
4152 if (!target->tap->enabled)
4153 goto err_tap_disabled;
4154 e = target_wait_state(target, n->value, a);
4155 if (e != ERROR_OK) {
4156 Jim_SetResult_sprintf(goi.interp,
4157 "target: %s wait %s fails (%d) %s",
4160 e, target_strerror_safe(e));
4165 case TS_CMD_EVENTLIST:
4166 /* List for human, Events defined for this target.
4167 * scripts/programs should use 'name cget -event NAME'
4170 target_event_action_t *teap;
4171 teap = target->event_action;
4172 command_print(cmd_ctx, "Event actions for target (%d) %s\n",
4173 target->target_number,
4175 command_print(cmd_ctx, "%-25s | Body", "Event");
4176 command_print(cmd_ctx, "------------------------- | ----------------------------------------");
4178 command_print(cmd_ctx,
4180 Jim_Nvp_value2name_simple(nvp_target_event, teap->event)->name,
4181 Jim_GetString(teap->body, NULL));
4184 command_print(cmd_ctx, "***END***");
4187 case TS_CMD_CURSTATE:
4188 if (goi.argc != 0) {
4189 Jim_WrongNumArgs(goi.interp, 0, argv, "[no parameters]");
4192 Jim_SetResultString(goi.interp,
4193 target_state_name( target ),
4196 case TS_CMD_INVOKE_EVENT:
4197 if (goi.argc != 1) {
4198 Jim_SetResult_sprintf(goi.interp, "%s ?EVENTNAME?",n->name);
4201 e = Jim_GetOpt_Nvp(&goi, nvp_target_event, &n);
4203 Jim_GetOpt_NvpUnknown(&goi, nvp_target_event, 1);
4206 target_handle_event(target, n->value);
4212 Jim_SetResult_sprintf(interp, "[TAP is disabled]");
4216 static int target_create(Jim_GetOptInfo *goi)
4225 struct command_context_s *cmd_ctx;
4227 cmd_ctx = Jim_GetAssocData(goi->interp, "context");
4228 if (goi->argc < 3) {
4229 Jim_WrongNumArgs(goi->interp, 1, goi->argv, "?name? ?type? ..options...");
4234 Jim_GetOpt_Obj(goi, &new_cmd);
4235 /* does this command exist? */
4236 cmd = Jim_GetCommand(goi->interp, new_cmd, JIM_ERRMSG);
4238 cp = Jim_GetString(new_cmd, NULL);
4239 Jim_SetResult_sprintf(goi->interp, "Command/target: %s Exists", cp);
4244 e = Jim_GetOpt_String(goi, &cp2, NULL);
4246 /* now does target type exist */
4247 for (x = 0 ; target_types[x] ; x++) {
4248 if (0 == strcmp(cp, target_types[x]->name)) {
4253 if (target_types[x] == NULL) {
4254 Jim_SetResult_sprintf(goi->interp, "Unknown target type %s, try one of ", cp);
4255 for (x = 0 ; target_types[x] ; x++) {
4256 if (target_types[x + 1]) {
4257 Jim_AppendStrings(goi->interp,
4258 Jim_GetResult(goi->interp),
4259 target_types[x]->name,
4262 Jim_AppendStrings(goi->interp,
4263 Jim_GetResult(goi->interp),
4265 target_types[x]->name,NULL);
4272 target = calloc(1,sizeof(target_t));
4273 /* set target number */
4274 target->target_number = new_target_number();
4276 /* allocate memory for each unique target type */
4277 target->type = (target_type_t*)calloc(1,sizeof(target_type_t));
4279 memcpy(target->type, target_types[x], sizeof(target_type_t));
4281 /* will be set by "-endian" */
4282 target->endianness = TARGET_ENDIAN_UNKNOWN;
4284 target->working_area = 0x0;
4285 target->working_area_size = 0x0;
4286 target->working_areas = NULL;
4287 target->backup_working_area = 0;
4289 target->state = TARGET_UNKNOWN;
4290 target->debug_reason = DBG_REASON_UNDEFINED;
4291 target->reg_cache = NULL;
4292 target->breakpoints = NULL;
4293 target->watchpoints = NULL;
4294 target->next = NULL;
4295 target->arch_info = NULL;
4297 target->display = 1;
4299 target->halt_issued = false;
4301 /* initialize trace information */
4302 target->trace_info = malloc(sizeof(trace_t));
4303 target->trace_info->num_trace_points = 0;
4304 target->trace_info->trace_points_size = 0;
4305 target->trace_info->trace_points = NULL;
4306 target->trace_info->trace_history_size = 0;
4307 target->trace_info->trace_history = NULL;
4308 target->trace_info->trace_history_pos = 0;
4309 target->trace_info->trace_history_overflowed = 0;
4311 target->dbgmsg = NULL;
4312 target->dbg_msg_enabled = 0;
4314 target->endianness = TARGET_ENDIAN_UNKNOWN;
4316 /* Do the rest as "configure" options */
4317 goi->isconfigure = 1;
4318 e = target_configure(goi, target);
4320 if (target->tap == NULL)
4322 Jim_SetResultString(interp, "-chain-position required when creating target", -1);
4332 if (target->endianness == TARGET_ENDIAN_UNKNOWN) {
4333 /* default endian to little if not specified */
4334 target->endianness = TARGET_LITTLE_ENDIAN;
4337 /* incase variant is not set */
4338 if (!target->variant)
4339 target->variant = strdup("");
4341 /* create the target specific commands */
4342 if (target->type->register_commands) {
4343 (*(target->type->register_commands))(cmd_ctx);
4345 if (target->type->target_create) {
4346 (*(target->type->target_create))(target, goi->interp);
4349 /* append to end of list */
4352 tpp = &(all_targets);
4354 tpp = &((*tpp)->next);
4359 cp = Jim_GetString(new_cmd, NULL);
4360 target->cmd_name = strdup(cp);
4362 /* now - create the new target name command */
4363 e = Jim_CreateCommand(goi->interp,
4366 tcl_target_func, /* C function */
4367 target, /* private data */
4368 NULL); /* no del proc */
4373 static int jim_target(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
4377 struct command_context_s *cmd_ctx;
4381 /* TG = target generic */
4389 const char *target_cmds[] = {
4390 "create", "types", "names", "current", "number",
4392 NULL /* terminate */
4395 LOG_DEBUG("Target command params:");
4396 LOG_DEBUG("%s", Jim_Debug_ArgvString(interp, argc, argv));
4398 cmd_ctx = Jim_GetAssocData(interp, "context");
4400 Jim_GetOpt_Setup(&goi, interp, argc-1, argv + 1);
4402 if (goi.argc == 0) {
4403 Jim_WrongNumArgs(interp, 1, argv, "missing: command ...");
4407 /* Jim_GetOpt_Debug(&goi); */
4408 r = Jim_GetOpt_Enum(&goi, target_cmds, &x);
4415 Jim_Panic(goi.interp,"Why am I here?");
4417 case TG_CMD_CURRENT:
4418 if (goi.argc != 0) {
4419 Jim_WrongNumArgs(goi.interp, 1, goi.argv, "Too many parameters");
4422 Jim_SetResultString(goi.interp, get_current_target(cmd_ctx)->cmd_name, -1);
4425 if (goi.argc != 0) {
4426 Jim_WrongNumArgs(goi.interp, 1, goi.argv, "Too many parameters");
4429 Jim_SetResult(goi.interp, Jim_NewListObj(goi.interp, NULL, 0));
4430 for (x = 0 ; target_types[x] ; x++) {
4431 Jim_ListAppendElement(goi.interp,
4432 Jim_GetResult(goi.interp),
4433 Jim_NewStringObj(goi.interp, target_types[x]->name, -1));
4437 if (goi.argc != 0) {
4438 Jim_WrongNumArgs(goi.interp, 1, goi.argv, "Too many parameters");
4441 Jim_SetResult(goi.interp, Jim_NewListObj(goi.interp, NULL, 0));
4442 target = all_targets;
4444 Jim_ListAppendElement(goi.interp,
4445 Jim_GetResult(goi.interp),
4446 Jim_NewStringObj(goi.interp, target->cmd_name, -1));
4447 target = target->next;
4452 Jim_WrongNumArgs(goi.interp, goi.argc, goi.argv, "?name ... config options ...");
4455 return target_create(&goi);
4458 /* It's OK to remove this mechanism sometime after August 2010 or so */
4459 LOG_WARNING("don't use numbers as target identifiers; use names");
4460 if (goi.argc != 1) {
4461 Jim_SetResult_sprintf(goi.interp, "expected: target number ?NUMBER?");
4464 e = Jim_GetOpt_Wide(&goi, &w);
4468 for (x = 0, target = all_targets; target; target = target->next, x++) {
4469 if (target->target_number == w)
4472 if (target == NULL) {
4473 Jim_SetResult_sprintf(goi.interp,
4474 "Target: number %d does not exist", (int)(w));
4477 Jim_SetResultString(goi.interp, target->cmd_name, -1);
4480 if (goi.argc != 0) {
4481 Jim_WrongNumArgs(goi.interp, 0, goi.argv, "<no parameters>");
4484 for (x = 0, target = all_targets; target; target = target->next, x++)
4486 Jim_SetResult(goi.interp, Jim_NewIntObj(goi.interp, x));
4502 static int fastload_num;
4503 static struct FastLoad *fastload;
4505 static void free_fastload(void)
4507 if (fastload != NULL)
4510 for (i = 0; i < fastload_num; i++)
4512 if (fastload[i].data)
4513 free(fastload[i].data);
4523 static int handle_fast_load_image_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
4527 uint32_t image_size;
4528 uint32_t min_address = 0;
4529 uint32_t max_address = 0xffffffff;
4534 int retval = parse_load_image_command_args(cmd_ctx, args, argc,
4535 &image, &min_address, &max_address);
4536 if (ERROR_OK != retval)
4539 struct duration bench;
4540 duration_start(&bench);
4542 if (image_open(&image, args[0], (argc >= 3) ? args[2] : NULL) != ERROR_OK)
4549 fastload_num = image.num_sections;
4550 fastload = (struct FastLoad *)malloc(sizeof(struct FastLoad)*image.num_sections);
4551 if (fastload == NULL)
4553 image_close(&image);
4556 memset(fastload, 0, sizeof(struct FastLoad)*image.num_sections);
4557 for (i = 0; i < image.num_sections; i++)
4559 buffer = malloc(image.sections[i].size);
4562 command_print(cmd_ctx, "error allocating buffer for section (%d bytes)",
4563 (int)(image.sections[i].size));
4567 if ((retval = image_read_section(&image, i, 0x0, image.sections[i].size, buffer, &buf_cnt)) != ERROR_OK)
4573 uint32_t offset = 0;
4574 uint32_t length = buf_cnt;
4577 /* DANGER!!! beware of unsigned comparision here!!! */
4579 if ((image.sections[i].base_address + buf_cnt >= min_address)&&
4580 (image.sections[i].base_address < max_address))
4582 if (image.sections[i].base_address < min_address)
4584 /* clip addresses below */
4585 offset += min_address-image.sections[i].base_address;
4589 if (image.sections[i].base_address + buf_cnt > max_address)
4591 length -= (image.sections[i].base_address + buf_cnt)-max_address;
4594 fastload[i].address = image.sections[i].base_address + offset;
4595 fastload[i].data = malloc(length);
4596 if (fastload[i].data == NULL)
4601 memcpy(fastload[i].data, buffer + offset, length);
4602 fastload[i].length = length;
4604 image_size += length;
4605 command_print(cmd_ctx, "%u bytes written at address 0x%8.8x",
4606 (unsigned int)length,
4607 ((unsigned int)(image.sections[i].base_address + offset)));
4613 if ((ERROR_OK == retval) && (duration_measure(&bench) == ERROR_OK))
4615 command_print(cmd_ctx, "Loaded %" PRIu32 " bytes "
4616 "in %fs (%0.3f kb/s)", image_size,
4617 duration_elapsed(&bench), duration_kbps(&bench, image_size));
4619 command_print(cmd_ctx,
4620 "WARNING: image has not been loaded to target!"
4621 "You can issue a 'fast_load' to finish loading.");
4624 image_close(&image);
4626 if (retval != ERROR_OK)
4634 static int handle_fast_load_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
4637 return ERROR_COMMAND_SYNTAX_ERROR;
4638 if (fastload == NULL)
4640 LOG_ERROR("No image in memory");
4644 int ms = timeval_ms();
4646 int retval = ERROR_OK;
4647 for (i = 0; i < fastload_num;i++)
4649 target_t *target = get_current_target(cmd_ctx);
4650 command_print(cmd_ctx, "Write to 0x%08x, length 0x%08x",
4651 (unsigned int)(fastload[i].address),
4652 (unsigned int)(fastload[i].length));
4653 if (retval == ERROR_OK)
4655 retval = target_write_buffer(target, fastload[i].address, fastload[i].length, fastload[i].data);
4657 size += fastload[i].length;
4659 int after = timeval_ms();
4660 command_print(cmd_ctx, "Loaded image %f kBytes/s", (float)(size/1024.0)/((float)(after-ms)/1000.0));
4664 static int jim_mcrmrc(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
4666 command_context_t *context;
4670 context = Jim_GetAssocData(interp, "context");
4671 if (context == NULL) {
4672 LOG_ERROR("array2mem: no command context");
4675 target = get_current_target(context);
4676 if (target == NULL) {
4677 LOG_ERROR("array2mem: no current target");
4681 if ((argc < 6) || (argc > 7))
4695 e = Jim_GetLong(interp, argv[1], &l);
4701 e = Jim_GetLong(interp, argv[2], &l);
4707 e = Jim_GetLong(interp, argv[3], &l);
4713 e = Jim_GetLong(interp, argv[4], &l);
4719 e = Jim_GetLong(interp, argv[5], &l);
4729 e = Jim_GetLong(interp, argv[6], &l);
4735 retval = target_mcr(target, cpnum, op1, op2, CRn, CRm, value);
4736 if (retval != ERROR_OK)
4740 retval = target_mrc(target, cpnum, op1, op2, CRn, CRm, &value);
4741 if (retval != ERROR_OK)
4744 Jim_SetResult(interp, Jim_NewIntObj(interp, value));
4750 int target_register_commands(struct command_context_s *cmd_ctx)
4753 register_command(cmd_ctx, NULL, "targets",
4754 handle_targets_command, COMMAND_EXEC,
4755 "change current command line target (one parameter) "
4756 "or list targets (no parameters)");
4758 register_jim(cmd_ctx, "target", jim_target, "configure target");
4763 int target_register_user_commands(struct command_context_s *cmd_ctx)
4765 int retval = ERROR_OK;
4766 if ((retval = target_request_register_commands(cmd_ctx)) != ERROR_OK)
4769 if ((retval = trace_register_commands(cmd_ctx)) != ERROR_OK)
4772 register_command(cmd_ctx, NULL, "profile",
4773 handle_profile_command, COMMAND_EXEC,
4774 "profiling samples the CPU PC");
4776 register_jim(cmd_ctx, "ocd_mem2array", jim_mem2array,
4777 "read memory and return as a TCL array for script processing "
4778 "<ARRAYNAME> <WIDTH = 32/16/8> <ADDRESS> <COUNT>");
4780 register_jim(cmd_ctx, "ocd_array2mem", jim_array2mem,
4781 "convert a TCL array to memory locations and write the values "
4782 "<ARRAYNAME> <WIDTH = 32/16/8> <ADDRESS> <COUNT>");
4784 register_command(cmd_ctx, NULL, "fast_load_image",
4785 handle_fast_load_image_command, COMMAND_ANY,
4786 "same args as load_image, image stored in memory "
4787 "- mainly for profiling purposes");
4789 register_command(cmd_ctx, NULL, "fast_load",
4790 handle_fast_load_command, COMMAND_ANY,
4791 "loads active fast load image to current target "
4792 "- mainly for profiling purposes");
4794 /** @todo don't register virt2phys() unless target supports it */
4795 register_command(cmd_ctx, NULL, "virt2phys",
4796 handle_virt2phys_command, COMMAND_ANY,
4797 "translate a virtual address into a physical address");
4799 register_command(cmd_ctx, NULL, "reg",
4800 handle_reg_command, COMMAND_EXEC,
4801 "display or set a register");
4803 register_command(cmd_ctx, NULL, "poll",
4804 handle_poll_command, COMMAND_EXEC,
4805 "poll target state");
4806 register_command(cmd_ctx, NULL, "wait_halt",
4807 handle_wait_halt_command, COMMAND_EXEC,
4808 "wait for target halt [time (s)]");
4809 register_command(cmd_ctx, NULL, "halt",
4810 handle_halt_command, COMMAND_EXEC,
4812 register_command(cmd_ctx, NULL, "resume",
4813 handle_resume_command, COMMAND_EXEC,
4814 "resume target [addr]");
4815 register_command(cmd_ctx, NULL, "reset",
4816 handle_reset_command, COMMAND_EXEC,
4817 "reset target [run | halt | init] - default is run");
4818 register_command(cmd_ctx, NULL, "soft_reset_halt",
4819 handle_soft_reset_halt_command, COMMAND_EXEC,
4820 "halt the target and do a soft reset");
4822 register_command(cmd_ctx, NULL, "step",
4823 handle_step_command, COMMAND_EXEC,
4824 "step one instruction from current PC or [addr]");
4826 register_command(cmd_ctx, NULL, "mdw",
4827 handle_md_command, COMMAND_EXEC,
4828 "display memory words [phys] <addr> [count]");
4829 register_command(cmd_ctx, NULL, "mdh",
4830 handle_md_command, COMMAND_EXEC,
4831 "display memory half-words [phys] <addr> [count]");
4832 register_command(cmd_ctx, NULL, "mdb",
4833 handle_md_command, COMMAND_EXEC,
4834 "display memory bytes [phys] <addr> [count]");
4836 register_command(cmd_ctx, NULL, "mww",
4837 handle_mw_command, COMMAND_EXEC,
4838 "write memory word [phys] <addr> <value> [count]");
4839 register_command(cmd_ctx, NULL, "mwh",
4840 handle_mw_command, COMMAND_EXEC,
4841 "write memory half-word [phys] <addr> <value> [count]");
4842 register_command(cmd_ctx, NULL, "mwb",
4843 handle_mw_command, COMMAND_EXEC,
4844 "write memory byte [phys] <addr> <value> [count]");
4846 register_command(cmd_ctx, NULL, "bp",
4847 handle_bp_command, COMMAND_EXEC,
4848 "list or set breakpoint [<address> <length> [hw]]");
4849 register_command(cmd_ctx, NULL, "rbp",
4850 handle_rbp_command, COMMAND_EXEC,
4851 "remove breakpoint <address>");
4853 register_command(cmd_ctx, NULL, "wp",
4854 handle_wp_command, COMMAND_EXEC,
4855 "list or set watchpoint "
4856 "[<address> <length> <r/w/a> [value] [mask]]");
4857 register_command(cmd_ctx, NULL, "rwp",
4858 handle_rwp_command, COMMAND_EXEC,
4859 "remove watchpoint <address>");
4861 register_command(cmd_ctx, NULL, "load_image",
4862 handle_load_image_command, COMMAND_EXEC,
4863 "load_image <file> <address> "
4864 "['bin'|'ihex'|'elf'|'s19'] [min_address] [max_length]");
4865 register_command(cmd_ctx, NULL, "dump_image",
4866 handle_dump_image_command, COMMAND_EXEC,
4867 "dump_image <file> <address> <size>");
4868 register_command(cmd_ctx, NULL, "verify_image",
4869 handle_verify_image_command, COMMAND_EXEC,
4870 "verify_image <file> [offset] [type]");
4871 register_command(cmd_ctx, NULL, "test_image",
4872 handle_test_image_command, COMMAND_EXEC,
4873 "test_image <file> [offset] [type]");