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 handle_targets_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
48 static int handle_reg_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
49 static int handle_poll_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
50 static int handle_halt_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
51 static int handle_wait_halt_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
52 static int handle_reset_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
53 static int handle_soft_reset_halt_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
54 static int handle_resume_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
55 static int handle_step_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
56 static int handle_md_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
57 static int handle_mw_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
58 static int handle_load_image_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
59 static int handle_dump_image_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
60 static int handle_verify_image_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
61 static int handle_test_image_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
62 static int handle_bp_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
63 static int handle_rbp_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
64 static int handle_wp_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
65 static int handle_rwp_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
66 static int handle_virt2phys_command(command_context_t *cmd_ctx, char *cmd, char **args, int argc);
67 static int handle_profile_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
68 static int handle_fast_load_image_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
69 static int handle_fast_load_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
71 static int jim_array2mem(Jim_Interp *interp, int argc, Jim_Obj *const *argv);
72 static int jim_mcrmrc(Jim_Interp *interp, int argc, Jim_Obj *const *argv);
73 static int jim_mem2array(Jim_Interp *interp, int argc, Jim_Obj *const *argv);
74 static int jim_target(Jim_Interp *interp, int argc, Jim_Obj *const *argv);
76 static int target_array2mem(Jim_Interp *interp, target_t *target, int argc, Jim_Obj *const *argv);
77 static int target_mem2array(Jim_Interp *interp, target_t *target, int argc, Jim_Obj *const *argv);
80 extern target_type_t arm7tdmi_target;
81 extern target_type_t arm720t_target;
82 extern target_type_t arm9tdmi_target;
83 extern target_type_t arm920t_target;
84 extern target_type_t arm966e_target;
85 extern target_type_t arm926ejs_target;
86 extern target_type_t fa526_target;
87 extern target_type_t feroceon_target;
88 extern target_type_t dragonite_target;
89 extern target_type_t xscale_target;
90 extern target_type_t cortexm3_target;
91 extern target_type_t cortexa8_target;
92 extern target_type_t arm11_target;
93 extern target_type_t mips_m4k_target;
94 extern target_type_t avr_target;
96 target_type_t *target_types[] =
116 target_t *all_targets = NULL;
117 target_event_callback_t *target_event_callbacks = NULL;
118 target_timer_callback_t *target_timer_callbacks = NULL;
120 const Jim_Nvp nvp_assert[] = {
121 { .name = "assert", NVP_ASSERT },
122 { .name = "deassert", NVP_DEASSERT },
123 { .name = "T", NVP_ASSERT },
124 { .name = "F", NVP_DEASSERT },
125 { .name = "t", NVP_ASSERT },
126 { .name = "f", NVP_DEASSERT },
127 { .name = NULL, .value = -1 }
130 const Jim_Nvp nvp_error_target[] = {
131 { .value = ERROR_TARGET_INVALID, .name = "err-invalid" },
132 { .value = ERROR_TARGET_INIT_FAILED, .name = "err-init-failed" },
133 { .value = ERROR_TARGET_TIMEOUT, .name = "err-timeout" },
134 { .value = ERROR_TARGET_NOT_HALTED, .name = "err-not-halted" },
135 { .value = ERROR_TARGET_FAILURE, .name = "err-failure" },
136 { .value = ERROR_TARGET_UNALIGNED_ACCESS , .name = "err-unaligned-access" },
137 { .value = ERROR_TARGET_DATA_ABORT , .name = "err-data-abort" },
138 { .value = ERROR_TARGET_RESOURCE_NOT_AVAILABLE , .name = "err-resource-not-available" },
139 { .value = ERROR_TARGET_TRANSLATION_FAULT , .name = "err-translation-fault" },
140 { .value = ERROR_TARGET_NOT_RUNNING, .name = "err-not-running" },
141 { .value = ERROR_TARGET_NOT_EXAMINED, .name = "err-not-examined" },
142 { .value = -1, .name = NULL }
145 const char *target_strerror_safe(int err)
149 n = Jim_Nvp_value2name_simple(nvp_error_target, err);
150 if (n->name == NULL) {
157 static const Jim_Nvp nvp_target_event[] = {
158 { .value = TARGET_EVENT_OLD_gdb_program_config , .name = "old-gdb_program_config" },
159 { .value = TARGET_EVENT_OLD_pre_resume , .name = "old-pre_resume" },
161 { .value = TARGET_EVENT_GDB_HALT, .name = "gdb-halt" },
162 { .value = TARGET_EVENT_HALTED, .name = "halted" },
163 { .value = TARGET_EVENT_RESUMED, .name = "resumed" },
164 { .value = TARGET_EVENT_RESUME_START, .name = "resume-start" },
165 { .value = TARGET_EVENT_RESUME_END, .name = "resume-end" },
167 { .name = "gdb-start", .value = TARGET_EVENT_GDB_START },
168 { .name = "gdb-end", .value = TARGET_EVENT_GDB_END },
170 /* historical name */
172 { .value = TARGET_EVENT_RESET_START, .name = "reset-start" },
174 { .value = TARGET_EVENT_RESET_ASSERT_PRE, .name = "reset-assert-pre" },
175 { .value = TARGET_EVENT_RESET_ASSERT_POST, .name = "reset-assert-post" },
176 { .value = TARGET_EVENT_RESET_DEASSERT_PRE, .name = "reset-deassert-pre" },
177 { .value = TARGET_EVENT_RESET_DEASSERT_POST, .name = "reset-deassert-post" },
178 { .value = TARGET_EVENT_RESET_HALT_PRE, .name = "reset-halt-pre" },
179 { .value = TARGET_EVENT_RESET_HALT_POST, .name = "reset-halt-post" },
180 { .value = TARGET_EVENT_RESET_WAIT_PRE, .name = "reset-wait-pre" },
181 { .value = TARGET_EVENT_RESET_WAIT_POST, .name = "reset-wait-post" },
182 { .value = TARGET_EVENT_RESET_INIT , .name = "reset-init" },
183 { .value = TARGET_EVENT_RESET_END, .name = "reset-end" },
185 { .value = TARGET_EVENT_EXAMINE_START, .name = "examine-start" },
186 { .value = TARGET_EVENT_EXAMINE_END, .name = "examine-end" },
188 { .value = TARGET_EVENT_DEBUG_HALTED, .name = "debug-halted" },
189 { .value = TARGET_EVENT_DEBUG_RESUMED, .name = "debug-resumed" },
191 { .value = TARGET_EVENT_GDB_ATTACH, .name = "gdb-attach" },
192 { .value = TARGET_EVENT_GDB_DETACH, .name = "gdb-detach" },
194 { .value = TARGET_EVENT_GDB_FLASH_WRITE_START, .name = "gdb-flash-write-start" },
195 { .value = TARGET_EVENT_GDB_FLASH_WRITE_END , .name = "gdb-flash-write-end" },
197 { .value = TARGET_EVENT_GDB_FLASH_ERASE_START, .name = "gdb-flash-erase-start" },
198 { .value = TARGET_EVENT_GDB_FLASH_ERASE_END , .name = "gdb-flash-erase-end" },
200 { .value = TARGET_EVENT_RESUME_START, .name = "resume-start" },
201 { .value = TARGET_EVENT_RESUMED , .name = "resume-ok" },
202 { .value = TARGET_EVENT_RESUME_END , .name = "resume-end" },
204 { .name = NULL, .value = -1 }
207 const Jim_Nvp nvp_target_state[] = {
208 { .name = "unknown", .value = TARGET_UNKNOWN },
209 { .name = "running", .value = TARGET_RUNNING },
210 { .name = "halted", .value = TARGET_HALTED },
211 { .name = "reset", .value = TARGET_RESET },
212 { .name = "debug-running", .value = TARGET_DEBUG_RUNNING },
213 { .name = NULL, .value = -1 },
216 const Jim_Nvp nvp_target_debug_reason [] = {
217 { .name = "debug-request" , .value = DBG_REASON_DBGRQ },
218 { .name = "breakpoint" , .value = DBG_REASON_BREAKPOINT },
219 { .name = "watchpoint" , .value = DBG_REASON_WATCHPOINT },
220 { .name = "watchpoint-and-breakpoint", .value = DBG_REASON_WPTANDBKPT },
221 { .name = "single-step" , .value = DBG_REASON_SINGLESTEP },
222 { .name = "target-not-halted" , .value = DBG_REASON_NOTHALTED },
223 { .name = "undefined" , .value = DBG_REASON_UNDEFINED },
224 { .name = NULL, .value = -1 },
227 const Jim_Nvp nvp_target_endian[] = {
228 { .name = "big", .value = TARGET_BIG_ENDIAN },
229 { .name = "little", .value = TARGET_LITTLE_ENDIAN },
230 { .name = "be", .value = TARGET_BIG_ENDIAN },
231 { .name = "le", .value = TARGET_LITTLE_ENDIAN },
232 { .name = NULL, .value = -1 },
235 const Jim_Nvp nvp_reset_modes[] = {
236 { .name = "unknown", .value = RESET_UNKNOWN },
237 { .name = "run" , .value = RESET_RUN },
238 { .name = "halt" , .value = RESET_HALT },
239 { .name = "init" , .value = RESET_INIT },
240 { .name = NULL , .value = -1 },
244 target_state_name( target_t *t )
247 cp = Jim_Nvp_value2name_simple(nvp_target_state, t->state)->name;
249 LOG_ERROR("Invalid target state: %d", (int)(t->state));
250 cp = "(*BUG*unknown*BUG*)";
255 /* determine the number of the new target */
256 static int new_target_number(void)
261 /* number is 0 based */
265 if (x < t->target_number) {
266 x = t->target_number;
273 /* read a uint32_t from a buffer in target memory endianness */
274 uint32_t target_buffer_get_u32(target_t *target, const uint8_t *buffer)
276 if (target->endianness == TARGET_LITTLE_ENDIAN)
277 return le_to_h_u32(buffer);
279 return be_to_h_u32(buffer);
282 /* read a uint16_t from a buffer in target memory endianness */
283 uint16_t target_buffer_get_u16(target_t *target, const uint8_t *buffer)
285 if (target->endianness == TARGET_LITTLE_ENDIAN)
286 return le_to_h_u16(buffer);
288 return be_to_h_u16(buffer);
291 /* read a uint8_t from a buffer in target memory endianness */
292 uint8_t target_buffer_get_u8(target_t *target, const uint8_t *buffer)
294 return *buffer & 0x0ff;
297 /* write a uint32_t to a buffer in target memory endianness */
298 void target_buffer_set_u32(target_t *target, uint8_t *buffer, uint32_t value)
300 if (target->endianness == TARGET_LITTLE_ENDIAN)
301 h_u32_to_le(buffer, value);
303 h_u32_to_be(buffer, value);
306 /* write a uint16_t to a buffer in target memory endianness */
307 void target_buffer_set_u16(target_t *target, uint8_t *buffer, uint16_t value)
309 if (target->endianness == TARGET_LITTLE_ENDIAN)
310 h_u16_to_le(buffer, value);
312 h_u16_to_be(buffer, value);
315 /* write a uint8_t to a buffer in target memory endianness */
316 void target_buffer_set_u8(target_t *target, uint8_t *buffer, uint8_t value)
321 /* return a pointer to a configured target; id is name or number */
322 target_t *get_target(const char *id)
326 /* try as tcltarget name */
327 for (target = all_targets; target; target = target->next) {
328 if (target->cmd_name == NULL)
330 if (strcmp(id, target->cmd_name) == 0)
334 /* It's OK to remove this fallback sometime after August 2010 or so */
336 /* no match, try as number */
338 if (parse_uint(id, &num) != ERROR_OK)
341 for (target = all_targets; target; target = target->next) {
342 if (target->target_number == (int)num) {
343 LOG_WARNING("use '%s' as target identifier, not '%u'",
344 target->cmd_name, num);
352 /* returns a pointer to the n-th configured target */
353 static target_t *get_target_by_num(int num)
355 target_t *target = all_targets;
358 if (target->target_number == num) {
361 target = target->next;
367 target_t* get_current_target(command_context_t *cmd_ctx)
369 target_t *target = get_target_by_num(cmd_ctx->current_target);
373 LOG_ERROR("BUG: current_target out of bounds");
380 int target_poll(struct target_s *target)
384 /* We can't poll until after examine */
385 if (!target_was_examined(target))
387 /* Fail silently lest we pollute the log */
391 retval = target->type->poll(target);
392 if (retval != ERROR_OK)
395 if (target->halt_issued)
397 if (target->state == TARGET_HALTED)
399 target->halt_issued = false;
402 long long t = timeval_ms() - target->halt_issued_time;
405 target->halt_issued = false;
406 LOG_INFO("Halt timed out, wake up GDB.");
407 target_call_event_callbacks(target, TARGET_EVENT_GDB_HALT);
415 int target_halt(struct target_s *target)
418 /* We can't poll until after examine */
419 if (!target_was_examined(target))
421 LOG_ERROR("Target not examined yet");
425 retval = target->type->halt(target);
426 if (retval != ERROR_OK)
429 target->halt_issued = true;
430 target->halt_issued_time = timeval_ms();
435 int target_resume(struct target_s *target, int current, uint32_t address, int handle_breakpoints, int debug_execution)
439 /* We can't poll until after examine */
440 if (!target_was_examined(target))
442 LOG_ERROR("Target not examined yet");
446 /* note that resume *must* be asynchronous. The CPU can halt before we poll. The CPU can
447 * even halt at the current PC as a result of a software breakpoint being inserted by (a bug?)
450 if ((retval = target->type->resume(target, current, address, handle_breakpoints, debug_execution)) != ERROR_OK)
456 int target_process_reset(struct command_context_s *cmd_ctx, enum target_reset_mode reset_mode)
461 n = Jim_Nvp_value2name_simple(nvp_reset_modes, reset_mode);
462 if (n->name == NULL) {
463 LOG_ERROR("invalid reset mode");
467 /* disable polling during reset to make reset event scripts
468 * more predictable, i.e. dr/irscan & pathmove in events will
469 * not have JTAG operations injected into the middle of a sequence.
471 bool save_poll = jtag_poll_get_enabled();
473 jtag_poll_set_enabled(false);
475 sprintf(buf, "ocd_process_reset %s", n->name);
476 retval = Jim_Eval(interp, buf);
478 jtag_poll_set_enabled(save_poll);
480 if (retval != JIM_OK) {
481 Jim_PrintErrorMessage(interp);
485 /* We want any events to be processed before the prompt */
486 retval = target_call_timer_callbacks_now();
491 static int default_virt2phys(struct target_s *target, uint32_t virtual, uint32_t *physical)
497 static int default_mmu(struct target_s *target, int *enabled)
503 static int default_examine(struct target_s *target)
505 target_set_examined(target);
509 int target_examine_one(struct target_s *target)
511 return target->type->examine(target);
514 static int jtag_enable_callback(enum jtag_event event, void *priv)
516 target_t *target = priv;
518 if (event != JTAG_TAP_EVENT_ENABLE || !target->tap->enabled)
521 jtag_unregister_event_callback(jtag_enable_callback, target);
522 return target_examine_one(target);
526 /* Targets that correctly implement init + examine, i.e.
527 * no communication with target during init:
531 int target_examine(void)
533 int retval = ERROR_OK;
536 for (target = all_targets; target; target = target->next)
538 /* defer examination, but don't skip it */
539 if (!target->tap->enabled) {
540 jtag_register_event_callback(jtag_enable_callback,
544 if ((retval = target_examine_one(target)) != ERROR_OK)
549 const char *target_get_name(struct target_s *target)
551 return target->type->name;
554 static int target_write_memory_imp(struct target_s *target, uint32_t address, uint32_t size, uint32_t count, uint8_t *buffer)
556 if (!target_was_examined(target))
558 LOG_ERROR("Target not examined yet");
561 return target->type->write_memory_imp(target, address, size, count, buffer);
564 static int target_read_memory_imp(struct target_s *target, uint32_t address, uint32_t size, uint32_t count, uint8_t *buffer)
566 if (!target_was_examined(target))
568 LOG_ERROR("Target not examined yet");
571 return target->type->read_memory_imp(target, address, size, count, buffer);
574 static int target_soft_reset_halt_imp(struct target_s *target)
576 if (!target_was_examined(target))
578 LOG_ERROR("Target not examined yet");
581 if (!target->type->soft_reset_halt_imp) {
582 LOG_ERROR("Target %s does not support soft_reset_halt",
586 return target->type->soft_reset_halt_imp(target);
589 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)
591 if (!target_was_examined(target))
593 LOG_ERROR("Target not examined yet");
596 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);
599 int target_read_memory(struct target_s *target,
600 uint32_t address, uint32_t size, uint32_t count, uint8_t *buffer)
602 return target->type->read_memory(target, address, size, count, buffer);
605 int target_read_phys_memory(struct target_s *target,
606 uint32_t address, uint32_t size, uint32_t count, uint8_t *buffer)
608 return target->type->read_phys_memory(target, address, size, count, buffer);
611 int target_write_memory(struct target_s *target,
612 uint32_t address, uint32_t size, uint32_t count, uint8_t *buffer)
614 return target->type->write_memory(target, address, size, count, buffer);
617 int target_write_phys_memory(struct target_s *target,
618 uint32_t address, uint32_t size, uint32_t count, uint8_t *buffer)
620 return target->type->write_phys_memory(target, address, size, count, buffer);
623 int target_bulk_write_memory(struct target_s *target,
624 uint32_t address, uint32_t count, uint8_t *buffer)
626 return target->type->bulk_write_memory(target, address, count, buffer);
629 int target_add_breakpoint(struct target_s *target,
630 struct breakpoint_s *breakpoint)
632 return target->type->add_breakpoint(target, breakpoint);
634 int target_remove_breakpoint(struct target_s *target,
635 struct breakpoint_s *breakpoint)
637 return target->type->remove_breakpoint(target, breakpoint);
640 int target_add_watchpoint(struct target_s *target,
641 struct watchpoint_s *watchpoint)
643 return target->type->add_watchpoint(target, watchpoint);
645 int target_remove_watchpoint(struct target_s *target,
646 struct watchpoint_s *watchpoint)
648 return target->type->remove_watchpoint(target, watchpoint);
651 int target_get_gdb_reg_list(struct target_s *target,
652 struct reg_s **reg_list[], int *reg_list_size)
654 return target->type->get_gdb_reg_list(target, reg_list, reg_list_size);
656 int target_step(struct target_s *target,
657 int current, uint32_t address, int handle_breakpoints)
659 return target->type->step(target, current, address, handle_breakpoints);
663 int target_run_algorithm(struct target_s *target,
664 int num_mem_params, mem_param_t *mem_params,
665 int num_reg_params, reg_param_t *reg_param,
666 uint32_t entry_point, uint32_t exit_point,
667 int timeout_ms, void *arch_info)
669 return target->type->run_algorithm(target,
670 num_mem_params, mem_params, num_reg_params, reg_param,
671 entry_point, exit_point, timeout_ms, arch_info);
674 /// @returns @c true if the target has been examined.
675 bool target_was_examined(struct target_s *target)
677 return target->type->examined;
679 /// Sets the @c examined flag for the given target.
680 void target_set_examined(struct target_s *target)
682 target->type->examined = true;
684 // Reset the @c examined flag for the given target.
685 void target_reset_examined(struct target_s *target)
687 target->type->examined = false;
692 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)
694 LOG_ERROR("Not implemented");
698 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)
700 LOG_ERROR("Not implemented");
704 static int arm_cp_check(struct target_s *target, int cpnum, uint32_t op1, uint32_t op2, uint32_t CRn, uint32_t CRm)
707 if (!target_was_examined(target))
709 LOG_ERROR("Target not examined yet");
713 if ((cpnum <0) || (cpnum > 15))
715 LOG_ERROR("Illegal co-processor %d", cpnum);
722 int target_mrc(struct target_s *target, int cpnum, uint32_t op1, uint32_t op2, uint32_t CRn, uint32_t CRm, uint32_t *value)
726 retval = arm_cp_check(target, cpnum, op1, op2, CRn, CRm);
727 if (retval != ERROR_OK)
730 return target->type->mrc(target, cpnum, op1, op2, CRn, CRm, value);
733 int target_mcr(struct target_s *target, int cpnum, uint32_t op1, uint32_t op2, uint32_t CRn, uint32_t CRm, uint32_t value)
737 retval = arm_cp_check(target, cpnum, op1, op2, CRn, CRm);
738 if (retval != ERROR_OK)
741 return target->type->mcr(target, cpnum, op1, op2, CRn, CRm, value);
745 int target_init(struct command_context_s *cmd_ctx)
747 target_t *target = all_targets;
752 target_reset_examined(target);
753 if (target->type->examine == NULL)
755 target->type->examine = default_examine;
758 if ((retval = target->type->init_target(cmd_ctx, target)) != ERROR_OK)
760 LOG_ERROR("target '%s' init failed", target_get_name(target));
764 /* Set up default functions if none are provided by target */
765 if (target->type->virt2phys == NULL)
767 target->type->virt2phys = default_virt2phys;
770 if (target->type->read_phys_memory == NULL)
772 target->type->read_phys_memory = target->type->read_memory;
775 if (target->type->write_phys_memory == NULL)
777 target->type->write_phys_memory = target->type->write_memory;
780 if (target->type->mcr == NULL)
782 target->type->mcr = default_mcr;
785 if (target->type->mrc == NULL)
787 target->type->mrc = default_mrc;
791 /* a non-invasive way(in terms of patches) to add some code that
792 * runs before the type->write/read_memory implementation
794 target->type->write_memory_imp = target->type->write_memory;
795 target->type->write_memory = target_write_memory_imp;
796 target->type->read_memory_imp = target->type->read_memory;
797 target->type->read_memory = target_read_memory_imp;
798 target->type->soft_reset_halt_imp = target->type->soft_reset_halt;
799 target->type->soft_reset_halt = target_soft_reset_halt_imp;
800 target->type->run_algorithm_imp = target->type->run_algorithm;
801 target->type->run_algorithm = target_run_algorithm_imp;
803 if (target->type->mmu == NULL)
805 target->type->mmu = default_mmu;
807 target = target->next;
812 if ((retval = target_register_user_commands(cmd_ctx)) != ERROR_OK)
814 if ((retval = target_register_timer_callback(handle_target, 100, 1, NULL)) != ERROR_OK)
821 int target_register_event_callback(int (*callback)(struct target_s *target, enum target_event event, void *priv), void *priv)
823 target_event_callback_t **callbacks_p = &target_event_callbacks;
825 if (callback == NULL)
827 return ERROR_INVALID_ARGUMENTS;
832 while ((*callbacks_p)->next)
833 callbacks_p = &((*callbacks_p)->next);
834 callbacks_p = &((*callbacks_p)->next);
837 (*callbacks_p) = malloc(sizeof(target_event_callback_t));
838 (*callbacks_p)->callback = callback;
839 (*callbacks_p)->priv = priv;
840 (*callbacks_p)->next = NULL;
845 int target_register_timer_callback(int (*callback)(void *priv), int time_ms, int periodic, void *priv)
847 target_timer_callback_t **callbacks_p = &target_timer_callbacks;
850 if (callback == NULL)
852 return ERROR_INVALID_ARGUMENTS;
857 while ((*callbacks_p)->next)
858 callbacks_p = &((*callbacks_p)->next);
859 callbacks_p = &((*callbacks_p)->next);
862 (*callbacks_p) = malloc(sizeof(target_timer_callback_t));
863 (*callbacks_p)->callback = callback;
864 (*callbacks_p)->periodic = periodic;
865 (*callbacks_p)->time_ms = time_ms;
867 gettimeofday(&now, NULL);
868 (*callbacks_p)->when.tv_usec = now.tv_usec + (time_ms % 1000) * 1000;
869 time_ms -= (time_ms % 1000);
870 (*callbacks_p)->when.tv_sec = now.tv_sec + (time_ms / 1000);
871 if ((*callbacks_p)->when.tv_usec > 1000000)
873 (*callbacks_p)->when.tv_usec = (*callbacks_p)->when.tv_usec - 1000000;
874 (*callbacks_p)->when.tv_sec += 1;
877 (*callbacks_p)->priv = priv;
878 (*callbacks_p)->next = NULL;
883 int target_unregister_event_callback(int (*callback)(struct target_s *target, enum target_event event, void *priv), void *priv)
885 target_event_callback_t **p = &target_event_callbacks;
886 target_event_callback_t *c = target_event_callbacks;
888 if (callback == NULL)
890 return ERROR_INVALID_ARGUMENTS;
895 target_event_callback_t *next = c->next;
896 if ((c->callback == callback) && (c->priv == priv))
910 int target_unregister_timer_callback(int (*callback)(void *priv), void *priv)
912 target_timer_callback_t **p = &target_timer_callbacks;
913 target_timer_callback_t *c = target_timer_callbacks;
915 if (callback == NULL)
917 return ERROR_INVALID_ARGUMENTS;
922 target_timer_callback_t *next = c->next;
923 if ((c->callback == callback) && (c->priv == priv))
937 int target_call_event_callbacks(target_t *target, enum target_event event)
939 target_event_callback_t *callback = target_event_callbacks;
940 target_event_callback_t *next_callback;
942 if (event == TARGET_EVENT_HALTED)
944 /* execute early halted first */
945 target_call_event_callbacks(target, TARGET_EVENT_GDB_HALT);
948 LOG_DEBUG("target event %i (%s)",
950 Jim_Nvp_value2name_simple(nvp_target_event, event)->name);
952 target_handle_event(target, event);
956 next_callback = callback->next;
957 callback->callback(target, event, callback->priv);
958 callback = next_callback;
964 static int target_timer_callback_periodic_restart(
965 target_timer_callback_t *cb, struct timeval *now)
967 int time_ms = cb->time_ms;
968 cb->when.tv_usec = now->tv_usec + (time_ms % 1000) * 1000;
969 time_ms -= (time_ms % 1000);
970 cb->when.tv_sec = now->tv_sec + time_ms / 1000;
971 if (cb->when.tv_usec > 1000000)
973 cb->when.tv_usec = cb->when.tv_usec - 1000000;
974 cb->when.tv_sec += 1;
979 static int target_call_timer_callback(target_timer_callback_t *cb,
982 cb->callback(cb->priv);
985 return target_timer_callback_periodic_restart(cb, now);
987 return target_unregister_timer_callback(cb->callback, cb->priv);
990 static int target_call_timer_callbacks_check_time(int checktime)
995 gettimeofday(&now, NULL);
997 target_timer_callback_t *callback = target_timer_callbacks;
1000 // cleaning up may unregister and free this callback
1001 target_timer_callback_t *next_callback = callback->next;
1003 bool call_it = callback->callback &&
1004 ((!checktime && callback->periodic) ||
1005 now.tv_sec > callback->when.tv_sec ||
1006 (now.tv_sec == callback->when.tv_sec &&
1007 now.tv_usec >= callback->when.tv_usec));
1011 int retval = target_call_timer_callback(callback, &now);
1012 if (retval != ERROR_OK)
1016 callback = next_callback;
1022 int target_call_timer_callbacks(void)
1024 return target_call_timer_callbacks_check_time(1);
1027 /* invoke periodic callbacks immediately */
1028 int target_call_timer_callbacks_now(void)
1030 return target_call_timer_callbacks_check_time(0);
1033 int target_alloc_working_area(struct target_s *target, uint32_t size, working_area_t **area)
1035 working_area_t *c = target->working_areas;
1036 working_area_t *new_wa = NULL;
1038 /* Reevaluate working area address based on MMU state*/
1039 if (target->working_areas == NULL)
1043 retval = target->type->mmu(target, &enabled);
1044 if (retval != ERROR_OK)
1051 if (target->working_area_phys_spec)
1053 LOG_DEBUG("MMU disabled, using physical address for working memory 0x%08x", (unsigned)target->working_area_phys);
1054 target->working_area = target->working_area_phys;
1057 LOG_ERROR("No working memory available. Specify -work-area-phys to target.");
1058 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
1062 if (target->working_area_virt_spec)
1064 LOG_DEBUG("MMU enabled, using virtual address for working memory 0x%08x", (unsigned)target->working_area_virt);
1065 target->working_area = target->working_area_virt;
1068 LOG_ERROR("No working memory available. Specify -work-area-virt to target.");
1069 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
1074 /* only allocate multiples of 4 byte */
1077 LOG_ERROR("BUG: code tried to allocate unaligned number of bytes (0x%08x), padding", ((unsigned)(size)));
1078 size = (size + 3) & (~3);
1081 /* see if there's already a matching working area */
1084 if ((c->free) && (c->size == size))
1092 /* if not, allocate a new one */
1095 working_area_t **p = &target->working_areas;
1096 uint32_t first_free = target->working_area;
1097 uint32_t free_size = target->working_area_size;
1099 c = target->working_areas;
1102 first_free += c->size;
1103 free_size -= c->size;
1108 if (free_size < size)
1110 LOG_WARNING("not enough working area available(requested %u, free %u)",
1111 (unsigned)(size), (unsigned)(free_size));
1112 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
1115 LOG_DEBUG("allocated new working area at address 0x%08x", (unsigned)first_free);
1117 new_wa = malloc(sizeof(working_area_t));
1118 new_wa->next = NULL;
1119 new_wa->size = size;
1120 new_wa->address = first_free;
1122 if (target->backup_working_area)
1125 new_wa->backup = malloc(new_wa->size);
1126 if ((retval = target_read_memory(target, new_wa->address, 4, new_wa->size / 4, new_wa->backup)) != ERROR_OK)
1128 free(new_wa->backup);
1135 new_wa->backup = NULL;
1138 /* put new entry in list */
1142 /* mark as used, and return the new (reused) area */
1147 new_wa->user = area;
1152 int target_free_working_area_restore(struct target_s *target, working_area_t *area, int restore)
1157 if (restore && target->backup_working_area)
1160 if ((retval = target_write_memory(target, area->address, 4, area->size / 4, area->backup)) != ERROR_OK)
1166 /* mark user pointer invalid */
1173 int target_free_working_area(struct target_s *target, working_area_t *area)
1175 return target_free_working_area_restore(target, area, 1);
1178 /* free resources and restore memory, if restoring memory fails,
1179 * free up resources anyway
1181 void target_free_all_working_areas_restore(struct target_s *target, int restore)
1183 working_area_t *c = target->working_areas;
1187 working_area_t *next = c->next;
1188 target_free_working_area_restore(target, c, restore);
1198 target->working_areas = NULL;
1201 void target_free_all_working_areas(struct target_s *target)
1203 target_free_all_working_areas_restore(target, 1);
1206 int target_register_commands(struct command_context_s *cmd_ctx)
1209 register_command(cmd_ctx, NULL, "targets", handle_targets_command, COMMAND_EXEC, "change the current command line target (one parameter) or lists targets (with no parameter)");
1214 register_jim(cmd_ctx, "target", jim_target, "configure target");
1219 int target_arch_state(struct target_s *target)
1224 LOG_USER("No target has been configured");
1228 LOG_USER("target state: %s", target_state_name( target ));
1230 if (target->state != TARGET_HALTED)
1233 retval = target->type->arch_state(target);
1237 /* Single aligned words are guaranteed to use 16 or 32 bit access
1238 * mode respectively, otherwise data is handled as quickly as
1241 int target_write_buffer(struct target_s *target, uint32_t address, uint32_t size, uint8_t *buffer)
1244 LOG_DEBUG("writing buffer of %i byte at 0x%8.8x",
1245 (int)size, (unsigned)address);
1247 if (!target_was_examined(target))
1249 LOG_ERROR("Target not examined yet");
1257 if ((address + size - 1) < address)
1259 /* GDB can request this when e.g. PC is 0xfffffffc*/
1260 LOG_ERROR("address + size wrapped(0x%08x, 0x%08x)",
1266 if (((address % 2) == 0) && (size == 2))
1268 return target_write_memory(target, address, 2, 1, buffer);
1271 /* handle unaligned head bytes */
1274 uint32_t unaligned = 4 - (address % 4);
1276 if (unaligned > size)
1279 if ((retval = target_write_memory(target, address, 1, unaligned, buffer)) != ERROR_OK)
1282 buffer += unaligned;
1283 address += unaligned;
1287 /* handle aligned words */
1290 int aligned = size - (size % 4);
1292 /* use bulk writes above a certain limit. This may have to be changed */
1295 if ((retval = target->type->bulk_write_memory(target, address, aligned / 4, buffer)) != ERROR_OK)
1300 if ((retval = target_write_memory(target, address, 4, aligned / 4, buffer)) != ERROR_OK)
1309 /* handle tail writes of less than 4 bytes */
1312 if ((retval = target_write_memory(target, address, 1, size, buffer)) != ERROR_OK)
1319 /* Single aligned words are guaranteed to use 16 or 32 bit access
1320 * mode respectively, otherwise data is handled as quickly as
1323 int target_read_buffer(struct target_s *target, uint32_t address, uint32_t size, uint8_t *buffer)
1326 LOG_DEBUG("reading buffer of %i byte at 0x%8.8x",
1327 (int)size, (unsigned)address);
1329 if (!target_was_examined(target))
1331 LOG_ERROR("Target not examined yet");
1339 if ((address + size - 1) < address)
1341 /* GDB can request this when e.g. PC is 0xfffffffc*/
1342 LOG_ERROR("address + size wrapped(0x%08" PRIx32 ", 0x%08" PRIx32 ")",
1348 if (((address % 2) == 0) && (size == 2))
1350 return target_read_memory(target, address, 2, 1, buffer);
1353 /* handle unaligned head bytes */
1356 uint32_t unaligned = 4 - (address % 4);
1358 if (unaligned > size)
1361 if ((retval = target_read_memory(target, address, 1, unaligned, buffer)) != ERROR_OK)
1364 buffer += unaligned;
1365 address += unaligned;
1369 /* handle aligned words */
1372 int aligned = size - (size % 4);
1374 if ((retval = target_read_memory(target, address, 4, aligned / 4, buffer)) != ERROR_OK)
1382 /*prevent byte access when possible (avoid AHB access limitations in some cases)*/
1385 int aligned = size - (size%2);
1386 retval = target_read_memory(target, address, 2, aligned / 2, buffer);
1387 if (retval != ERROR_OK)
1394 /* handle tail writes of less than 4 bytes */
1397 if ((retval = target_read_memory(target, address, 1, size, buffer)) != ERROR_OK)
1404 int target_checksum_memory(struct target_s *target, uint32_t address, uint32_t size, uint32_t* crc)
1409 uint32_t checksum = 0;
1410 if (!target_was_examined(target))
1412 LOG_ERROR("Target not examined yet");
1416 if ((retval = target->type->checksum_memory(target, address,
1417 size, &checksum)) != ERROR_OK)
1419 buffer = malloc(size);
1422 LOG_ERROR("error allocating buffer for section (%d bytes)", (int)size);
1423 return ERROR_INVALID_ARGUMENTS;
1425 retval = target_read_buffer(target, address, size, buffer);
1426 if (retval != ERROR_OK)
1432 /* convert to target endianess */
1433 for (i = 0; i < (size/sizeof(uint32_t)); i++)
1435 uint32_t target_data;
1436 target_data = target_buffer_get_u32(target, &buffer[i*sizeof(uint32_t)]);
1437 target_buffer_set_u32(target, &buffer[i*sizeof(uint32_t)], target_data);
1440 retval = image_calculate_checksum(buffer, size, &checksum);
1449 int target_blank_check_memory(struct target_s *target, uint32_t address, uint32_t size, uint32_t* blank)
1452 if (!target_was_examined(target))
1454 LOG_ERROR("Target not examined yet");
1458 if (target->type->blank_check_memory == 0)
1459 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
1461 retval = target->type->blank_check_memory(target, address, size, blank);
1466 int target_read_u32(struct target_s *target, uint32_t address, uint32_t *value)
1468 uint8_t value_buf[4];
1469 if (!target_was_examined(target))
1471 LOG_ERROR("Target not examined yet");
1475 int retval = target_read_memory(target, address, 4, 1, value_buf);
1477 if (retval == ERROR_OK)
1479 *value = target_buffer_get_u32(target, value_buf);
1480 LOG_DEBUG("address: 0x%8.8" PRIx32 ", value: 0x%8.8" PRIx32 "",
1487 LOG_DEBUG("address: 0x%8.8" PRIx32 " failed",
1494 int target_read_u16(struct target_s *target, uint32_t address, uint16_t *value)
1496 uint8_t value_buf[2];
1497 if (!target_was_examined(target))
1499 LOG_ERROR("Target not examined yet");
1503 int retval = target_read_memory(target, address, 2, 1, value_buf);
1505 if (retval == ERROR_OK)
1507 *value = target_buffer_get_u16(target, value_buf);
1508 LOG_DEBUG("address: 0x%8.8" PRIx32 ", value: 0x%4.4x",
1515 LOG_DEBUG("address: 0x%8.8" PRIx32 " failed",
1522 int target_read_u8(struct target_s *target, uint32_t address, uint8_t *value)
1524 int retval = target_read_memory(target, address, 1, 1, value);
1525 if (!target_was_examined(target))
1527 LOG_ERROR("Target not examined yet");
1531 if (retval == ERROR_OK)
1533 LOG_DEBUG("address: 0x%8.8" PRIx32 ", value: 0x%2.2x",
1540 LOG_DEBUG("address: 0x%8.8" PRIx32 " failed",
1547 int target_write_u32(struct target_s *target, uint32_t address, uint32_t value)
1550 uint8_t value_buf[4];
1551 if (!target_was_examined(target))
1553 LOG_ERROR("Target not examined yet");
1557 LOG_DEBUG("address: 0x%8.8" PRIx32 ", value: 0x%8.8" PRIx32 "",
1561 target_buffer_set_u32(target, value_buf, value);
1562 if ((retval = target_write_memory(target, address, 4, 1, value_buf)) != ERROR_OK)
1564 LOG_DEBUG("failed: %i", retval);
1570 int target_write_u16(struct target_s *target, uint32_t address, uint16_t value)
1573 uint8_t value_buf[2];
1574 if (!target_was_examined(target))
1576 LOG_ERROR("Target not examined yet");
1580 LOG_DEBUG("address: 0x%8.8" PRIx32 ", value: 0x%8.8x",
1584 target_buffer_set_u16(target, value_buf, value);
1585 if ((retval = target_write_memory(target, address, 2, 1, value_buf)) != ERROR_OK)
1587 LOG_DEBUG("failed: %i", retval);
1593 int target_write_u8(struct target_s *target, uint32_t address, uint8_t value)
1596 if (!target_was_examined(target))
1598 LOG_ERROR("Target not examined yet");
1602 LOG_DEBUG("address: 0x%8.8" PRIx32 ", value: 0x%2.2x",
1605 if ((retval = target_write_memory(target, address, 1, 1, &value)) != ERROR_OK)
1607 LOG_DEBUG("failed: %i", retval);
1613 int target_register_user_commands(struct command_context_s *cmd_ctx)
1615 int retval = ERROR_OK;
1618 /* script procedures */
1619 register_command(cmd_ctx, NULL, "profile", handle_profile_command, COMMAND_EXEC, "profiling samples the CPU PC");
1620 register_jim(cmd_ctx, "ocd_mem2array", jim_mem2array, "read memory and return as a TCL array for script processing <ARRAYNAME> <WIDTH = 32/16/8> <ADDRESS> <COUNT>");
1621 register_jim(cmd_ctx, "ocd_array2mem", jim_array2mem, "convert a TCL array to memory locations and write the values <ARRAYNAME> <WIDTH = 32/16/8> <ADDRESS> <COUNT>");
1623 register_jim(cmd_ctx, "mrc", jim_mcrmrc, "read coprocessor <cpnum> <op1> <op2> <CRn> <CRm>");
1624 register_jim(cmd_ctx, "mcr", jim_mcrmrc, "write coprocessor <cpnum> <op1> <op2> <CRn> <CRm> <value>");
1626 register_command(cmd_ctx, NULL, "fast_load_image", handle_fast_load_image_command, COMMAND_ANY,
1627 "same args as load_image, image stored in memory - mainly for profiling purposes");
1629 register_command(cmd_ctx, NULL, "fast_load", handle_fast_load_command, COMMAND_ANY,
1630 "loads active fast load image to current target - mainly for profiling purposes");
1633 register_command(cmd_ctx, NULL, "virt2phys", handle_virt2phys_command, COMMAND_ANY, "translate a virtual address into a physical address");
1634 register_command(cmd_ctx, NULL, "reg", handle_reg_command, COMMAND_EXEC, "display or set a register");
1635 register_command(cmd_ctx, NULL, "poll", handle_poll_command, COMMAND_EXEC, "poll target state");
1636 register_command(cmd_ctx, NULL, "wait_halt", handle_wait_halt_command, COMMAND_EXEC, "wait for target halt [time (s)]");
1637 register_command(cmd_ctx, NULL, "halt", handle_halt_command, COMMAND_EXEC, "halt target");
1638 register_command(cmd_ctx, NULL, "resume", handle_resume_command, COMMAND_EXEC, "resume target [addr]");
1639 register_command(cmd_ctx, NULL, "step", handle_step_command, COMMAND_EXEC, "step one instruction from current PC or [addr]");
1640 register_command(cmd_ctx, NULL, "reset", handle_reset_command, COMMAND_EXEC, "reset target [run | halt | init] - default is run");
1641 register_command(cmd_ctx, NULL, "soft_reset_halt", handle_soft_reset_halt_command, COMMAND_EXEC, "halt the target and do a soft reset");
1643 register_command(cmd_ctx, NULL, "mdw", handle_md_command, COMMAND_EXEC, "display memory words [phys] <addr> [count]");
1644 register_command(cmd_ctx, NULL, "mdh", handle_md_command, COMMAND_EXEC, "display memory half-words [phys] <addr> [count]");
1645 register_command(cmd_ctx, NULL, "mdb", handle_md_command, COMMAND_EXEC, "display memory bytes [phys] <addr> [count]");
1647 register_command(cmd_ctx, NULL, "mww", handle_mw_command, COMMAND_EXEC, "write memory word [phys] <addr> <value> [count]");
1648 register_command(cmd_ctx, NULL, "mwh", handle_mw_command, COMMAND_EXEC, "write memory half-word [phys] <addr> <value> [count]");
1649 register_command(cmd_ctx, NULL, "mwb", handle_mw_command, COMMAND_EXEC, "write memory byte [phys] <addr> <value> [count]");
1651 register_command(cmd_ctx, NULL, "bp",
1652 handle_bp_command, COMMAND_EXEC,
1653 "list or set breakpoint [<address> <length> [hw]]");
1654 register_command(cmd_ctx, NULL, "rbp",
1655 handle_rbp_command, COMMAND_EXEC,
1656 "remove breakpoint <address>");
1657 register_command(cmd_ctx, NULL, "wp",
1658 handle_wp_command, COMMAND_EXEC,
1659 "list or set watchpoint "
1660 "[<address> <length> <r/w/a> [value] [mask]]");
1661 register_command(cmd_ctx, NULL, "rwp",
1662 handle_rwp_command, COMMAND_EXEC,
1663 "remove watchpoint <address>");
1665 register_command(cmd_ctx, NULL, "load_image", handle_load_image_command, COMMAND_EXEC, "load_image <file> <address> ['bin'|'ihex'|'elf'|'s19'] [min_address] [max_length]");
1666 register_command(cmd_ctx, NULL, "dump_image", handle_dump_image_command, COMMAND_EXEC, "dump_image <file> <address> <size>");
1667 register_command(cmd_ctx, NULL, "verify_image", handle_verify_image_command, COMMAND_EXEC, "verify_image <file> [offset] [type]");
1668 register_command(cmd_ctx, NULL, "test_image", handle_test_image_command, COMMAND_EXEC, "test_image <file> [offset] [type]");
1670 if ((retval = target_request_register_commands(cmd_ctx)) != ERROR_OK)
1672 if ((retval = trace_register_commands(cmd_ctx)) != ERROR_OK)
1678 static int handle_targets_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1680 target_t *target = all_targets;
1684 target = get_target(args[0]);
1685 if (target == NULL) {
1686 command_print(cmd_ctx,"Target: %s is unknown, try one of:\n", args[0]);
1689 if (!target->tap->enabled) {
1690 command_print(cmd_ctx,"Target: TAP %s is disabled, "
1691 "can't be the current target\n",
1692 target->tap->dotted_name);
1696 cmd_ctx->current_target = target->target_number;
1701 target = all_targets;
1702 command_print(cmd_ctx, " TargetName Type Endian TapName State ");
1703 command_print(cmd_ctx, "-- ------------------ ---------- ------ ------------------ ------------");
1709 if (target->tap->enabled)
1710 state = target_state_name( target );
1712 state = "tap-disabled";
1714 if (cmd_ctx->current_target == target->target_number)
1717 /* keep columns lined up to match the headers above */
1718 command_print(cmd_ctx, "%2d%c %-18s %-10s %-6s %-18s %s",
1719 target->target_number,
1722 target_get_name(target),
1723 Jim_Nvp_value2name_simple(nvp_target_endian,
1724 target->endianness)->name,
1725 target->tap->dotted_name,
1727 target = target->next;
1733 /* every 300ms we check for reset & powerdropout and issue a "reset halt" if so. */
1735 static int powerDropout;
1736 static int srstAsserted;
1738 static int runPowerRestore;
1739 static int runPowerDropout;
1740 static int runSrstAsserted;
1741 static int runSrstDeasserted;
1743 static int sense_handler(void)
1745 static int prevSrstAsserted = 0;
1746 static int prevPowerdropout = 0;
1749 if ((retval = jtag_power_dropout(&powerDropout)) != ERROR_OK)
1753 powerRestored = prevPowerdropout && !powerDropout;
1756 runPowerRestore = 1;
1759 long long current = timeval_ms();
1760 static long long lastPower = 0;
1761 int waitMore = lastPower + 2000 > current;
1762 if (powerDropout && !waitMore)
1764 runPowerDropout = 1;
1765 lastPower = current;
1768 if ((retval = jtag_srst_asserted(&srstAsserted)) != ERROR_OK)
1772 srstDeasserted = prevSrstAsserted && !srstAsserted;
1774 static long long lastSrst = 0;
1775 waitMore = lastSrst + 2000 > current;
1776 if (srstDeasserted && !waitMore)
1778 runSrstDeasserted = 1;
1782 if (!prevSrstAsserted && srstAsserted)
1784 runSrstAsserted = 1;
1787 prevSrstAsserted = srstAsserted;
1788 prevPowerdropout = powerDropout;
1790 if (srstDeasserted || powerRestored)
1792 /* Other than logging the event we can't do anything here.
1793 * Issuing a reset is a particularly bad idea as we might
1794 * be inside a reset already.
1801 static void target_call_event_callbacks_all(enum target_event e) {
1803 target = all_targets;
1805 target_call_event_callbacks(target, e);
1806 target = target->next;
1810 /* process target state changes */
1811 int handle_target(void *priv)
1813 int retval = ERROR_OK;
1815 /* we do not want to recurse here... */
1816 static int recursive = 0;
1821 /* danger! running these procedures can trigger srst assertions and power dropouts.
1822 * We need to avoid an infinite loop/recursion here and we do that by
1823 * clearing the flags after running these events.
1825 int did_something = 0;
1826 if (runSrstAsserted)
1828 target_call_event_callbacks_all(TARGET_EVENT_GDB_HALT);
1829 Jim_Eval(interp, "srst_asserted");
1832 if (runSrstDeasserted)
1834 Jim_Eval(interp, "srst_deasserted");
1837 if (runPowerDropout)
1839 target_call_event_callbacks_all(TARGET_EVENT_GDB_HALT);
1840 Jim_Eval(interp, "power_dropout");
1843 if (runPowerRestore)
1845 Jim_Eval(interp, "power_restore");
1851 /* clear detect flags */
1855 /* clear action flags */
1857 runSrstAsserted = 0;
1858 runSrstDeasserted = 0;
1859 runPowerRestore = 0;
1860 runPowerDropout = 0;
1865 /* Poll targets for state changes unless that's globally disabled.
1866 * Skip targets that are currently disabled.
1868 for (target_t *target = all_targets;
1869 is_jtag_poll_safe() && target;
1870 target = target->next)
1872 if (!target->tap->enabled)
1875 /* only poll target if we've got power and srst isn't asserted */
1876 if (!powerDropout && !srstAsserted)
1878 /* polling may fail silently until the target has been examined */
1879 if ((retval = target_poll(target)) != ERROR_OK)
1881 target_call_event_callbacks(target, TARGET_EVENT_GDB_HALT);
1890 static int handle_reg_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1899 target = get_current_target(cmd_ctx);
1901 /* list all available registers for the current target */
1904 reg_cache_t *cache = target->reg_cache;
1911 command_print(cmd_ctx, "===== %s", cache->name);
1913 for (i = 0, reg = cache->reg_list;
1914 i < cache->num_regs;
1915 i++, reg++, count++)
1917 /* only print cached values if they are valid */
1919 value = buf_to_str(reg->value,
1921 command_print(cmd_ctx,
1922 "(%i) %s (/%" PRIu32 "): 0x%s%s",
1930 command_print(cmd_ctx, "(%i) %s (/%" PRIu32 ")",
1935 cache = cache->next;
1941 /* access a single register by its ordinal number */
1942 if ((args[0][0] >= '0') && (args[0][0] <= '9'))
1945 int retval = parse_uint(args[0], &num);
1946 if (ERROR_OK != retval)
1947 return ERROR_COMMAND_SYNTAX_ERROR;
1949 reg_cache_t *cache = target->reg_cache;
1954 for (i = 0; i < cache->num_regs; i++)
1956 if (count++ == (int)num)
1958 reg = &cache->reg_list[i];
1964 cache = cache->next;
1969 command_print(cmd_ctx, "%i is out of bounds, the current target has only %i registers (0 - %i)", num, count, count - 1);
1972 } else /* access a single register by its name */
1974 reg = register_get_by_name(target->reg_cache, args[0], 1);
1978 command_print(cmd_ctx, "register %s not found in current target", args[0]);
1983 /* display a register */
1984 if ((argc == 1) || ((argc == 2) && !((args[1][0] >= '0') && (args[1][0] <= '9'))))
1986 if ((argc == 2) && (strcmp(args[1], "force") == 0))
1989 if (reg->valid == 0)
1991 reg_arch_type_t *arch_type = register_get_arch_type(reg->arch_type);
1992 arch_type->get(reg);
1994 value = buf_to_str(reg->value, reg->size, 16);
1995 command_print(cmd_ctx, "%s (/%i): 0x%s", reg->name, (int)(reg->size), value);
2000 /* set register value */
2003 uint8_t *buf = malloc(CEIL(reg->size, 8));
2004 str_to_buf(args[1], strlen(args[1]), buf, reg->size, 0);
2006 reg_arch_type_t *arch_type = register_get_arch_type(reg->arch_type);
2007 arch_type->set(reg, buf);
2009 value = buf_to_str(reg->value, reg->size, 16);
2010 command_print(cmd_ctx, "%s (/%i): 0x%s", reg->name, (int)(reg->size), value);
2018 command_print(cmd_ctx, "usage: reg <#|name> [value]");
2023 static int handle_poll_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2025 int retval = ERROR_OK;
2026 target_t *target = get_current_target(cmd_ctx);
2030 command_print(cmd_ctx, "background polling: %s",
2031 jtag_poll_get_enabled() ? "on" : "off");
2032 command_print(cmd_ctx, "TAP: %s (%s)",
2033 target->tap->dotted_name,
2034 target->tap->enabled ? "enabled" : "disabled");
2035 if (!target->tap->enabled)
2037 if ((retval = target_poll(target)) != ERROR_OK)
2039 if ((retval = target_arch_state(target)) != ERROR_OK)
2045 if (strcmp(args[0], "on") == 0)
2047 jtag_poll_set_enabled(true);
2049 else if (strcmp(args[0], "off") == 0)
2051 jtag_poll_set_enabled(false);
2055 command_print(cmd_ctx, "arg is \"on\" or \"off\"");
2059 return ERROR_COMMAND_SYNTAX_ERROR;
2065 static int handle_wait_halt_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2068 return ERROR_COMMAND_SYNTAX_ERROR;
2073 int retval = parse_uint(args[0], &ms);
2074 if (ERROR_OK != retval)
2076 command_print(cmd_ctx, "usage: %s [seconds]", cmd);
2077 return ERROR_COMMAND_SYNTAX_ERROR;
2079 // convert seconds (given) to milliseconds (needed)
2083 target_t *target = get_current_target(cmd_ctx);
2084 return target_wait_state(target, TARGET_HALTED, ms);
2087 /* wait for target state to change. The trick here is to have a low
2088 * latency for short waits and not to suck up all the CPU time
2091 * After 500ms, keep_alive() is invoked
2093 int target_wait_state(target_t *target, enum target_state state, int ms)
2096 long long then = 0, cur;
2101 if ((retval = target_poll(target)) != ERROR_OK)
2103 if (target->state == state)
2111 then = timeval_ms();
2112 LOG_DEBUG("waiting for target %s...",
2113 Jim_Nvp_value2name_simple(nvp_target_state,state)->name);
2121 if ((cur-then) > ms)
2123 LOG_ERROR("timed out while waiting for target %s",
2124 Jim_Nvp_value2name_simple(nvp_target_state,state)->name);
2132 static int handle_halt_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2136 target_t *target = get_current_target(cmd_ctx);
2137 int retval = target_halt(target);
2138 if (ERROR_OK != retval)
2144 retval = parse_uint(args[0], &wait);
2145 if (ERROR_OK != retval)
2146 return ERROR_COMMAND_SYNTAX_ERROR;
2151 return handle_wait_halt_command(cmd_ctx, cmd, args, argc);
2154 static int handle_soft_reset_halt_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2156 target_t *target = get_current_target(cmd_ctx);
2158 LOG_USER("requesting target halt and executing a soft reset");
2160 target->type->soft_reset_halt(target);
2165 static int handle_reset_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2168 return ERROR_COMMAND_SYNTAX_ERROR;
2170 enum target_reset_mode reset_mode = RESET_RUN;
2174 n = Jim_Nvp_name2value_simple(nvp_reset_modes, args[0]);
2175 if ((n->name == NULL) || (n->value == RESET_UNKNOWN)) {
2176 return ERROR_COMMAND_SYNTAX_ERROR;
2178 reset_mode = n->value;
2181 /* reset *all* targets */
2182 return target_process_reset(cmd_ctx, reset_mode);
2186 static int handle_resume_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2190 return ERROR_COMMAND_SYNTAX_ERROR;
2192 target_t *target = get_current_target(cmd_ctx);
2193 target_handle_event(target, TARGET_EVENT_OLD_pre_resume);
2195 /* with no args, resume from current pc, addr = 0,
2196 * with one arguments, addr = args[0],
2197 * handle breakpoints, not debugging */
2201 int retval = parse_u32(args[0], &addr);
2202 if (ERROR_OK != retval)
2207 return target_resume(target, current, addr, 1, 0);
2210 static int handle_step_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2213 return ERROR_COMMAND_SYNTAX_ERROR;
2217 /* with no args, step from current pc, addr = 0,
2218 * with one argument addr = args[0],
2219 * handle breakpoints, debugging */
2224 int retval = parse_u32(args[0], &addr);
2225 if (ERROR_OK != retval)
2230 target_t *target = get_current_target(cmd_ctx);
2232 return target->type->step(target, current_pc, addr, 1);
2235 static void handle_md_output(struct command_context_s *cmd_ctx,
2236 struct target_s *target, uint32_t address, unsigned size,
2237 unsigned count, const uint8_t *buffer)
2239 const unsigned line_bytecnt = 32;
2240 unsigned line_modulo = line_bytecnt / size;
2242 char output[line_bytecnt * 4 + 1];
2243 unsigned output_len = 0;
2245 const char *value_fmt;
2247 case 4: value_fmt = "%8.8x "; break;
2248 case 2: value_fmt = "%4.2x "; break;
2249 case 1: value_fmt = "%2.2x "; break;
2251 LOG_ERROR("invalid memory read size: %u", size);
2255 for (unsigned i = 0; i < count; i++)
2257 if (i % line_modulo == 0)
2259 output_len += snprintf(output + output_len,
2260 sizeof(output) - output_len,
2262 (unsigned)(address + (i*size)));
2266 const uint8_t *value_ptr = buffer + i * size;
2268 case 4: value = target_buffer_get_u32(target, value_ptr); break;
2269 case 2: value = target_buffer_get_u16(target, value_ptr); break;
2270 case 1: value = *value_ptr;
2272 output_len += snprintf(output + output_len,
2273 sizeof(output) - output_len,
2276 if ((i % line_modulo == line_modulo - 1) || (i == count - 1))
2278 command_print(cmd_ctx, "%s", output);
2284 static int handle_md_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2287 return ERROR_COMMAND_SYNTAX_ERROR;
2291 case 'w': size = 4; break;
2292 case 'h': size = 2; break;
2293 case 'b': size = 1; break;
2294 default: return ERROR_COMMAND_SYNTAX_ERROR;
2297 bool physical=strcmp(args[0], "phys")==0;
2298 int (*fn)(struct target_s *target,
2299 uint32_t address, uint32_t size, uint32_t count, uint8_t *buffer);
2304 fn=target_read_phys_memory;
2307 fn=target_read_memory;
2309 if ((argc < 1) || (argc > 2))
2311 return ERROR_COMMAND_SYNTAX_ERROR;
2314 int retval = parse_u32(args[0], &address);
2315 if (ERROR_OK != retval)
2321 retval = parse_uint(args[1], &count);
2322 if (ERROR_OK != retval)
2326 uint8_t *buffer = calloc(count, size);
2328 target_t *target = get_current_target(cmd_ctx);
2329 retval = fn(target, address, size, count, buffer);
2330 if (ERROR_OK == retval)
2331 handle_md_output(cmd_ctx, target, address, size, count, buffer);
2338 static int handle_mw_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2342 return ERROR_COMMAND_SYNTAX_ERROR;
2344 bool physical=strcmp(args[0], "phys")==0;
2345 int (*fn)(struct target_s *target,
2346 uint32_t address, uint32_t size, uint32_t count, uint8_t *buffer);
2351 fn=target_write_phys_memory;
2354 fn=target_write_memory;
2356 if ((argc < 2) || (argc > 3))
2357 return ERROR_COMMAND_SYNTAX_ERROR;
2360 int retval = parse_u32(args[0], &address);
2361 if (ERROR_OK != retval)
2365 retval = parse_u32(args[1], &value);
2366 if (ERROR_OK != retval)
2372 retval = parse_uint(args[2], &count);
2373 if (ERROR_OK != retval)
2377 target_t *target = get_current_target(cmd_ctx);
2379 uint8_t value_buf[4];
2384 target_buffer_set_u32(target, value_buf, value);
2388 target_buffer_set_u16(target, value_buf, value);
2392 value_buf[0] = value;
2395 return ERROR_COMMAND_SYNTAX_ERROR;
2397 for (unsigned i = 0; i < count; i++)
2400 address + i * wordsize, wordsize, 1, value_buf);
2401 if (ERROR_OK != retval)
2410 static int parse_load_image_command_args(char **args, int argc,
2411 image_t *image, uint32_t *min_address, uint32_t *max_address)
2413 if (argc < 1 || argc > 5)
2414 return ERROR_COMMAND_SYNTAX_ERROR;
2416 /* a base address isn't always necessary,
2417 * default to 0x0 (i.e. don't relocate) */
2421 int retval = parse_u32(args[1], &addr);
2422 if (ERROR_OK != retval)
2423 return ERROR_COMMAND_SYNTAX_ERROR;
2424 image->base_address = addr;
2425 image->base_address_set = 1;
2428 image->base_address_set = 0;
2430 image->start_address_set = 0;
2434 int retval = parse_u32(args[3], min_address);
2435 if (ERROR_OK != retval)
2436 return ERROR_COMMAND_SYNTAX_ERROR;
2440 int retval = parse_u32(args[4], max_address);
2441 if (ERROR_OK != retval)
2442 return ERROR_COMMAND_SYNTAX_ERROR;
2443 // use size (given) to find max (required)
2444 *max_address += *min_address;
2447 if (*min_address > *max_address)
2448 return ERROR_COMMAND_SYNTAX_ERROR;
2453 static int handle_load_image_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2457 uint32_t image_size;
2458 uint32_t min_address = 0;
2459 uint32_t max_address = 0xffffffff;
2465 duration_t duration;
2466 char *duration_text;
2468 int retval = parse_load_image_command_args(args, argc,
2469 &image, &min_address, &max_address);
2470 if (ERROR_OK != retval)
2473 target_t *target = get_current_target(cmd_ctx);
2474 duration_start_measure(&duration);
2476 if (image_open(&image, args[0], (argc >= 3) ? args[2] : NULL) != ERROR_OK)
2483 for (i = 0; i < image.num_sections; i++)
2485 buffer = malloc(image.sections[i].size);
2488 command_print(cmd_ctx,
2489 "error allocating buffer for section (%d bytes)",
2490 (int)(image.sections[i].size));
2494 if ((retval = image_read_section(&image, i, 0x0, image.sections[i].size, buffer, &buf_cnt)) != ERROR_OK)
2500 uint32_t offset = 0;
2501 uint32_t length = buf_cnt;
2503 /* DANGER!!! beware of unsigned comparision here!!! */
2505 if ((image.sections[i].base_address + buf_cnt >= min_address)&&
2506 (image.sections[i].base_address < max_address))
2508 if (image.sections[i].base_address < min_address)
2510 /* clip addresses below */
2511 offset += min_address-image.sections[i].base_address;
2515 if (image.sections[i].base_address + buf_cnt > max_address)
2517 length -= (image.sections[i].base_address + buf_cnt)-max_address;
2520 if ((retval = target_write_buffer(target, image.sections[i].base_address + offset, length, buffer + offset)) != ERROR_OK)
2525 image_size += length;
2526 command_print(cmd_ctx, "%u bytes written at address 0x%8.8" PRIx32 "",
2527 (unsigned int)length,
2528 image.sections[i].base_address + offset);
2534 if ((retvaltemp = duration_stop_measure(&duration, &duration_text)) != ERROR_OK)
2536 image_close(&image);
2540 if (retval == ERROR_OK)
2542 command_print(cmd_ctx, "downloaded %u byte in %s",
2543 (unsigned int)image_size,
2546 free(duration_text);
2548 image_close(&image);
2554 static int handle_dump_image_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2558 uint8_t buffer[560];
2561 duration_t duration;
2562 char *duration_text;
2564 target_t *target = get_current_target(cmd_ctx);
2568 command_print(cmd_ctx, "usage: dump_image <filename> <address> <size>");
2573 int retval = parse_u32(args[1], &address);
2574 if (ERROR_OK != retval)
2578 retval = parse_u32(args[2], &size);
2579 if (ERROR_OK != retval)
2582 if (fileio_open(&fileio, args[0], FILEIO_WRITE, FILEIO_BINARY) != ERROR_OK)
2587 duration_start_measure(&duration);
2591 uint32_t size_written;
2592 uint32_t this_run_size = (size > 560) ? 560 : size;
2594 retval = target_read_buffer(target, address, this_run_size, buffer);
2595 if (retval != ERROR_OK)
2600 retval = fileio_write(&fileio, this_run_size, buffer, &size_written);
2601 if (retval != ERROR_OK)
2606 size -= this_run_size;
2607 address += this_run_size;
2610 if ((retvaltemp = fileio_close(&fileio)) != ERROR_OK)
2613 if ((retvaltemp = duration_stop_measure(&duration, &duration_text)) != ERROR_OK)
2616 if (retval == ERROR_OK)
2618 command_print(cmd_ctx, "dumped %lld byte in %s",
2619 fileio.size, duration_text);
2620 free(duration_text);
2626 static int handle_verify_image_command_internal(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc, int verify)
2630 uint32_t image_size;
2632 int retval, retvaltemp;
2633 uint32_t checksum = 0;
2634 uint32_t mem_checksum = 0;
2638 duration_t duration;
2639 char *duration_text;
2641 target_t *target = get_current_target(cmd_ctx);
2645 return ERROR_COMMAND_SYNTAX_ERROR;
2650 LOG_ERROR("no target selected");
2654 duration_start_measure(&duration);
2659 retval = parse_u32(args[1], &addr);
2660 if (ERROR_OK != retval)
2661 return ERROR_COMMAND_SYNTAX_ERROR;
2662 image.base_address = addr;
2663 image.base_address_set = 1;
2667 image.base_address_set = 0;
2668 image.base_address = 0x0;
2671 image.start_address_set = 0;
2673 if ((retval = image_open(&image, args[0], (argc == 3) ? args[2] : NULL)) != ERROR_OK)
2680 for (i = 0; i < image.num_sections; i++)
2682 buffer = malloc(image.sections[i].size);
2685 command_print(cmd_ctx,
2686 "error allocating buffer for section (%d bytes)",
2687 (int)(image.sections[i].size));
2690 if ((retval = image_read_section(&image, i, 0x0, image.sections[i].size, buffer, &buf_cnt)) != ERROR_OK)
2698 /* calculate checksum of image */
2699 image_calculate_checksum(buffer, buf_cnt, &checksum);
2701 retval = target_checksum_memory(target, image.sections[i].base_address, buf_cnt, &mem_checksum);
2702 if (retval != ERROR_OK)
2708 if (checksum != mem_checksum)
2710 /* failed crc checksum, fall back to a binary compare */
2713 command_print(cmd_ctx, "checksum mismatch - attempting binary compare");
2715 data = (uint8_t*)malloc(buf_cnt);
2717 /* Can we use 32bit word accesses? */
2719 int count = buf_cnt;
2720 if ((count % 4) == 0)
2725 retval = target_read_memory(target, image.sections[i].base_address, size, count, data);
2726 if (retval == ERROR_OK)
2729 for (t = 0; t < buf_cnt; t++)
2731 if (data[t] != buffer[t])
2733 command_print(cmd_ctx,
2734 "Verify operation failed address 0x%08x. Was 0x%02x instead of 0x%02x\n",
2735 (unsigned)(t + image.sections[i].base_address),
2740 retval = ERROR_FAIL;
2754 command_print(cmd_ctx, "address 0x%08" PRIx32 " length 0x%08" PRIx32 "",
2755 image.sections[i].base_address,
2760 image_size += buf_cnt;
2764 if ((retvaltemp = duration_stop_measure(&duration, &duration_text)) != ERROR_OK)
2766 image_close(&image);
2770 if (retval == ERROR_OK)
2772 command_print(cmd_ctx, "verified %u bytes in %s",
2773 (unsigned int)image_size,
2776 free(duration_text);
2778 image_close(&image);
2783 static int handle_verify_image_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2785 return handle_verify_image_command_internal(cmd_ctx, cmd, args, argc, 1);
2788 static int handle_test_image_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2790 return handle_verify_image_command_internal(cmd_ctx, cmd, args, argc, 0);
2793 static int handle_bp_command_list(struct command_context_s *cmd_ctx)
2795 target_t *target = get_current_target(cmd_ctx);
2796 breakpoint_t *breakpoint = target->breakpoints;
2799 if (breakpoint->type == BKPT_SOFT)
2801 char* buf = buf_to_str(breakpoint->orig_instr,
2802 breakpoint->length, 16);
2803 command_print(cmd_ctx, "0x%8.8" PRIx32 ", 0x%x, %i, 0x%s",
2804 breakpoint->address,
2806 breakpoint->set, buf);
2811 command_print(cmd_ctx, "0x%8.8" PRIx32 ", 0x%x, %i",
2812 breakpoint->address,
2813 breakpoint->length, breakpoint->set);
2816 breakpoint = breakpoint->next;
2821 static int handle_bp_command_set(struct command_context_s *cmd_ctx,
2822 uint32_t addr, uint32_t length, int hw)
2824 target_t *target = get_current_target(cmd_ctx);
2825 int retval = breakpoint_add(target, addr, length, hw);
2826 if (ERROR_OK == retval)
2827 command_print(cmd_ctx, "breakpoint set at 0x%8.8" PRIx32 "", addr);
2829 LOG_ERROR("Failure setting breakpoint");
2833 static int handle_bp_command(struct command_context_s *cmd_ctx,
2834 char *cmd, char **args, int argc)
2837 return handle_bp_command_list(cmd_ctx);
2839 if (argc < 2 || argc > 3)
2841 command_print(cmd_ctx, "usage: bp <address> <length> ['hw']");
2842 return ERROR_COMMAND_SYNTAX_ERROR;
2846 int retval = parse_u32(args[0], &addr);
2847 if (ERROR_OK != retval)
2851 retval = parse_u32(args[1], &length);
2852 if (ERROR_OK != retval)
2858 if (strcmp(args[2], "hw") == 0)
2861 return ERROR_COMMAND_SYNTAX_ERROR;
2864 return handle_bp_command_set(cmd_ctx, addr, length, hw);
2867 static int handle_rbp_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2870 return ERROR_COMMAND_SYNTAX_ERROR;
2873 int retval = parse_u32(args[0], &addr);
2874 if (ERROR_OK != retval)
2877 target_t *target = get_current_target(cmd_ctx);
2878 breakpoint_remove(target, addr);
2883 static int handle_wp_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2885 target_t *target = get_current_target(cmd_ctx);
2889 watchpoint_t *watchpoint = target->watchpoints;
2893 command_print(cmd_ctx,
2894 "address: 0x%8.8" PRIx32 ", len: 0x%8.8x, r/w/a: %i, value: 0x%8.8" PRIx32 ", mask: 0x%8.8" PRIx32 "",
2895 watchpoint->address,
2897 (int)(watchpoint->rw),
2900 watchpoint = watchpoint->next;
2905 enum watchpoint_rw type = WPT_ACCESS;
2907 uint32_t length = 0;
2908 uint32_t data_value = 0x0;
2909 uint32_t data_mask = 0xffffffff;
2915 retval = parse_u32(args[4], &data_mask);
2916 if (ERROR_OK != retval)
2920 retval = parse_u32(args[3], &data_value);
2921 if (ERROR_OK != retval)
2937 LOG_ERROR("invalid watchpoint mode ('%c')", args[2][0]);
2938 return ERROR_COMMAND_SYNTAX_ERROR;
2942 retval = parse_u32(args[1], &length);
2943 if (ERROR_OK != retval)
2945 retval = parse_u32(args[0], &addr);
2946 if (ERROR_OK != retval)
2951 command_print(cmd_ctx, "usage: wp <address> <length> [r/w/a] [value] [mask]");
2952 return ERROR_COMMAND_SYNTAX_ERROR;
2955 retval = watchpoint_add(target, addr, length, type,
2956 data_value, data_mask);
2957 if (ERROR_OK != retval)
2958 LOG_ERROR("Failure setting watchpoints");
2963 static int handle_rwp_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
2966 return ERROR_COMMAND_SYNTAX_ERROR;
2969 int retval = parse_u32(args[0], &addr);
2970 if (ERROR_OK != retval)
2973 target_t *target = get_current_target(cmd_ctx);
2974 watchpoint_remove(target, addr);
2981 * Translate a virtual address to a physical address.
2983 * The low-level target implementation must have logged a detailed error
2984 * which is forwarded to telnet/GDB session.
2986 static int handle_virt2phys_command(command_context_t *cmd_ctx,
2987 char *cmd, char **args, int argc)
2990 return ERROR_COMMAND_SYNTAX_ERROR;
2993 int retval = parse_u32(args[0], &va);
2994 if (ERROR_OK != retval)
2998 target_t *target = get_current_target(cmd_ctx);
2999 retval = target->type->virt2phys(target, va, &pa);
3000 if (retval == ERROR_OK)
3001 command_print(cmd_ctx, "Physical address 0x%08" PRIx32 "", pa);
3006 static void writeData(FILE *f, const void *data, size_t len)
3008 size_t written = fwrite(data, 1, len, f);
3010 LOG_ERROR("failed to write %zu bytes: %s", len, strerror(errno));
3013 static void writeLong(FILE *f, int l)
3016 for (i = 0; i < 4; i++)
3018 char c = (l >> (i*8))&0xff;
3019 writeData(f, &c, 1);
3024 static void writeString(FILE *f, char *s)
3026 writeData(f, s, strlen(s));
3029 /* Dump a gmon.out histogram file. */
3030 static void writeGmon(uint32_t *samples, uint32_t sampleNum, char *filename)
3033 FILE *f = fopen(filename, "w");
3036 writeString(f, "gmon");
3037 writeLong(f, 0x00000001); /* Version */
3038 writeLong(f, 0); /* padding */
3039 writeLong(f, 0); /* padding */
3040 writeLong(f, 0); /* padding */
3042 uint8_t zero = 0; /* GMON_TAG_TIME_HIST */
3043 writeData(f, &zero, 1);
3045 /* figure out bucket size */
3046 uint32_t min = samples[0];
3047 uint32_t max = samples[0];
3048 for (i = 0; i < sampleNum; i++)
3050 if (min > samples[i])
3054 if (max < samples[i])
3060 int addressSpace = (max-min + 1);
3062 static const uint32_t maxBuckets = 256 * 1024; /* maximum buckets. */
3063 uint32_t length = addressSpace;
3064 if (length > maxBuckets)
3066 length = maxBuckets;
3068 int *buckets = malloc(sizeof(int)*length);
3069 if (buckets == NULL)
3074 memset(buckets, 0, sizeof(int)*length);
3075 for (i = 0; i < sampleNum;i++)
3077 uint32_t address = samples[i];
3078 long long a = address-min;
3079 long long b = length-1;
3080 long long c = addressSpace-1;
3081 int index = (a*b)/c; /* danger!!!! int32 overflows */
3085 /* append binary memory gmon.out &profile_hist_hdr ((char*)&profile_hist_hdr + sizeof(struct gmon_hist_hdr)) */
3086 writeLong(f, min); /* low_pc */
3087 writeLong(f, max); /* high_pc */
3088 writeLong(f, length); /* # of samples */
3089 writeLong(f, 64000000); /* 64MHz */
3090 writeString(f, "seconds");
3091 for (i = 0; i < (15-strlen("seconds")); i++)
3092 writeData(f, &zero, 1);
3093 writeString(f, "s");
3095 /*append binary memory gmon.out profile_hist_data (profile_hist_data + profile_hist_hdr.hist_size) */
3097 char *data = malloc(2*length);
3100 for (i = 0; i < length;i++)
3109 data[i*2 + 1]=(val >> 8)&0xff;
3112 writeData(f, data, length * 2);
3122 /* profiling samples the CPU PC as quickly as OpenOCD is able, which will be used as a random sampling of PC */
3123 static int handle_profile_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
3125 target_t *target = get_current_target(cmd_ctx);
3126 struct timeval timeout, now;
3128 gettimeofday(&timeout, NULL);
3131 return ERROR_COMMAND_SYNTAX_ERROR;
3134 int retval = parse_uint(args[0], &offset);
3135 if (ERROR_OK != retval)
3138 timeval_add_time(&timeout, offset, 0);
3140 command_print(cmd_ctx, "Starting profiling. Halting and resuming the target as often as we can...");
3142 static const int maxSample = 10000;
3143 uint32_t *samples = malloc(sizeof(uint32_t)*maxSample);
3144 if (samples == NULL)
3148 /* hopefully it is safe to cache! We want to stop/restart as quickly as possible. */
3149 reg_t *reg = register_get_by_name(target->reg_cache, "pc", 1);
3153 target_poll(target);
3154 if (target->state == TARGET_HALTED)
3156 uint32_t t=*((uint32_t *)reg->value);
3157 samples[numSamples++]=t;
3158 retval = target_resume(target, 1, 0, 0, 0); /* current pc, addr = 0, do not handle breakpoints, not debugging */
3159 target_poll(target);
3160 alive_sleep(10); /* sleep 10ms, i.e. <100 samples/second. */
3161 } else if (target->state == TARGET_RUNNING)
3163 /* We want to quickly sample the PC. */
3164 if ((retval = target_halt(target)) != ERROR_OK)
3171 command_print(cmd_ctx, "Target not halted or running");
3175 if (retval != ERROR_OK)
3180 gettimeofday(&now, NULL);
3181 if ((numSamples >= maxSample) || ((now.tv_sec >= timeout.tv_sec) && (now.tv_usec >= timeout.tv_usec)))
3183 command_print(cmd_ctx, "Profiling completed. %d samples.", numSamples);
3184 if ((retval = target_poll(target)) != ERROR_OK)
3189 if (target->state == TARGET_HALTED)
3191 target_resume(target, 1, 0, 0, 0); /* current pc, addr = 0, do not handle breakpoints, not debugging */
3193 if ((retval = target_poll(target)) != ERROR_OK)
3198 writeGmon(samples, numSamples, args[1]);
3199 command_print(cmd_ctx, "Wrote %s", args[1]);
3208 static int new_int_array_element(Jim_Interp * interp, const char *varname, int idx, uint32_t val)
3211 Jim_Obj *nameObjPtr, *valObjPtr;
3214 namebuf = alloc_printf("%s(%d)", varname, idx);
3218 nameObjPtr = Jim_NewStringObj(interp, namebuf, -1);
3219 valObjPtr = Jim_NewIntObj(interp, val);
3220 if (!nameObjPtr || !valObjPtr)
3226 Jim_IncrRefCount(nameObjPtr);
3227 Jim_IncrRefCount(valObjPtr);
3228 result = Jim_SetVariable(interp, nameObjPtr, valObjPtr);
3229 Jim_DecrRefCount(interp, nameObjPtr);
3230 Jim_DecrRefCount(interp, valObjPtr);
3232 /* printf("%s(%d) <= 0%08x\n", varname, idx, val); */
3236 static int jim_mem2array(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
3238 command_context_t *context;
3241 context = Jim_GetAssocData(interp, "context");
3242 if (context == NULL)
3244 LOG_ERROR("mem2array: no command context");
3247 target = get_current_target(context);
3250 LOG_ERROR("mem2array: no current target");
3254 return target_mem2array(interp, target, argc-1, argv + 1);
3257 static int target_mem2array(Jim_Interp *interp, target_t *target, int argc, Jim_Obj *const *argv)
3265 const char *varname;
3266 uint8_t buffer[4096];
3270 /* argv[1] = name of array to receive the data
3271 * argv[2] = desired width
3272 * argv[3] = memory address
3273 * argv[4] = count of times to read
3276 Jim_WrongNumArgs(interp, 1, argv, "varname width addr nelems");
3279 varname = Jim_GetString(argv[0], &len);
3280 /* given "foo" get space for worse case "foo(%d)" .. add 20 */
3282 e = Jim_GetLong(interp, argv[1], &l);
3288 e = Jim_GetLong(interp, argv[2], &l);
3293 e = Jim_GetLong(interp, argv[3], &l);
3309 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3310 Jim_AppendStrings(interp, Jim_GetResult(interp), "Invalid width param, must be 8/16/32", NULL);
3314 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3315 Jim_AppendStrings(interp, Jim_GetResult(interp), "mem2array: zero width read?", NULL);
3318 if ((addr + (len * width)) < addr) {
3319 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3320 Jim_AppendStrings(interp, Jim_GetResult(interp), "mem2array: addr + len - wraps to zero?", NULL);
3323 /* absurd transfer size? */
3325 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3326 Jim_AppendStrings(interp, Jim_GetResult(interp), "mem2array: absurd > 64K item request", NULL);
3331 ((width == 2) && ((addr & 1) == 0)) ||
3332 ((width == 4) && ((addr & 3) == 0))) {
3336 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3337 sprintf(buf, "mem2array address: 0x%08" PRIx32 " is not aligned for %" PRId32 " byte reads",
3340 Jim_AppendStrings(interp, Jim_GetResult(interp), buf , NULL);
3351 /* Slurp... in buffer size chunks */
3353 count = len; /* in objects.. */
3354 if (count > (sizeof(buffer)/width)) {
3355 count = (sizeof(buffer)/width);
3358 retval = target_read_memory(target, addr, width, count, buffer);
3359 if (retval != ERROR_OK) {
3361 LOG_ERROR("mem2array: Read @ 0x%08x, w=%d, cnt=%d, failed",
3365 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3366 Jim_AppendStrings(interp, Jim_GetResult(interp), "mem2array: cannot read memory", NULL);
3370 v = 0; /* shut up gcc */
3371 for (i = 0 ;i < count ;i++, n++) {
3374 v = target_buffer_get_u32(target, &buffer[i*width]);
3377 v = target_buffer_get_u16(target, &buffer[i*width]);
3380 v = buffer[i] & 0x0ff;
3383 new_int_array_element(interp, varname, n, v);
3389 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3394 static int get_int_array_element(Jim_Interp * interp, const char *varname, int idx, uint32_t *val)
3397 Jim_Obj *nameObjPtr, *valObjPtr;
3401 namebuf = alloc_printf("%s(%d)", varname, idx);
3405 nameObjPtr = Jim_NewStringObj(interp, namebuf, -1);
3412 Jim_IncrRefCount(nameObjPtr);
3413 valObjPtr = Jim_GetVariable(interp, nameObjPtr, JIM_ERRMSG);
3414 Jim_DecrRefCount(interp, nameObjPtr);
3416 if (valObjPtr == NULL)
3419 result = Jim_GetLong(interp, valObjPtr, &l);
3420 /* printf("%s(%d) => 0%08x\n", varname, idx, val); */
3425 static int jim_array2mem(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
3427 command_context_t *context;
3430 context = Jim_GetAssocData(interp, "context");
3431 if (context == NULL) {
3432 LOG_ERROR("array2mem: no command context");
3435 target = get_current_target(context);
3436 if (target == NULL) {
3437 LOG_ERROR("array2mem: no current target");
3441 return target_array2mem(interp,target, argc-1, argv + 1);
3443 static int target_array2mem(Jim_Interp *interp, target_t *target, int argc, Jim_Obj *const *argv)
3451 const char *varname;
3452 uint8_t buffer[4096];
3456 /* argv[1] = name of array to get the data
3457 * argv[2] = desired width
3458 * argv[3] = memory address
3459 * argv[4] = count to write
3462 Jim_WrongNumArgs(interp, 1, argv, "varname width addr nelems");
3465 varname = Jim_GetString(argv[0], &len);
3466 /* given "foo" get space for worse case "foo(%d)" .. add 20 */
3468 e = Jim_GetLong(interp, argv[1], &l);
3474 e = Jim_GetLong(interp, argv[2], &l);
3479 e = Jim_GetLong(interp, argv[3], &l);
3495 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3496 Jim_AppendStrings(interp, Jim_GetResult(interp), "Invalid width param, must be 8/16/32", NULL);
3500 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3501 Jim_AppendStrings(interp, Jim_GetResult(interp), "array2mem: zero width read?", NULL);
3504 if ((addr + (len * width)) < addr) {
3505 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3506 Jim_AppendStrings(interp, Jim_GetResult(interp), "array2mem: addr + len - wraps to zero?", NULL);
3509 /* absurd transfer size? */
3511 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3512 Jim_AppendStrings(interp, Jim_GetResult(interp), "array2mem: absurd > 64K item request", NULL);
3517 ((width == 2) && ((addr & 1) == 0)) ||
3518 ((width == 4) && ((addr & 3) == 0))) {
3522 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3523 sprintf(buf, "array2mem address: 0x%08x is not aligned for %d byte reads",
3526 Jim_AppendStrings(interp, Jim_GetResult(interp), buf , NULL);
3537 /* Slurp... in buffer size chunks */
3539 count = len; /* in objects.. */
3540 if (count > (sizeof(buffer)/width)) {
3541 count = (sizeof(buffer)/width);
3544 v = 0; /* shut up gcc */
3545 for (i = 0 ;i < count ;i++, n++) {
3546 get_int_array_element(interp, varname, n, &v);
3549 target_buffer_set_u32(target, &buffer[i*width], v);
3552 target_buffer_set_u16(target, &buffer[i*width], v);
3555 buffer[i] = v & 0x0ff;
3561 retval = target_write_memory(target, addr, width, count, buffer);
3562 if (retval != ERROR_OK) {
3564 LOG_ERROR("array2mem: Write @ 0x%08x, w=%d, cnt=%d, failed",
3568 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3569 Jim_AppendStrings(interp, Jim_GetResult(interp), "array2mem: cannot read memory", NULL);
3575 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3580 void target_all_handle_event(enum target_event e)
3584 LOG_DEBUG("**all*targets: event: %d, %s",
3586 Jim_Nvp_value2name_simple(nvp_target_event, e)->name);
3588 target = all_targets;
3590 target_handle_event(target, e);
3591 target = target->next;
3596 /* FIX? should we propagate errors here rather than printing them
3599 void target_handle_event(target_t *target, enum target_event e)
3601 target_event_action_t *teap;
3603 for (teap = target->event_action; teap != NULL; teap = teap->next) {
3604 if (teap->event == e) {
3605 LOG_DEBUG("target: (%d) %s (%s) event: %d (%s) action: %s",
3606 target->target_number,
3608 target_get_name(target),
3610 Jim_Nvp_value2name_simple(nvp_target_event, e)->name,
3611 Jim_GetString(teap->body, NULL));
3612 if (Jim_EvalObj(interp, teap->body) != JIM_OK)
3614 Jim_PrintErrorMessage(interp);
3620 enum target_cfg_param {
3623 TCFG_WORK_AREA_VIRT,
3624 TCFG_WORK_AREA_PHYS,
3625 TCFG_WORK_AREA_SIZE,
3626 TCFG_WORK_AREA_BACKUP,
3629 TCFG_CHAIN_POSITION,
3632 static Jim_Nvp nvp_config_opts[] = {
3633 { .name = "-type", .value = TCFG_TYPE },
3634 { .name = "-event", .value = TCFG_EVENT },
3635 { .name = "-work-area-virt", .value = TCFG_WORK_AREA_VIRT },
3636 { .name = "-work-area-phys", .value = TCFG_WORK_AREA_PHYS },
3637 { .name = "-work-area-size", .value = TCFG_WORK_AREA_SIZE },
3638 { .name = "-work-area-backup", .value = TCFG_WORK_AREA_BACKUP },
3639 { .name = "-endian" , .value = TCFG_ENDIAN },
3640 { .name = "-variant", .value = TCFG_VARIANT },
3641 { .name = "-chain-position", .value = TCFG_CHAIN_POSITION },
3643 { .name = NULL, .value = -1 }
3646 static int target_configure(Jim_GetOptInfo *goi, target_t *target)
3654 /* parse config or cget options ... */
3655 while (goi->argc > 0) {
3656 Jim_SetEmptyResult(goi->interp);
3657 /* Jim_GetOpt_Debug(goi); */
3659 if (target->type->target_jim_configure) {
3660 /* target defines a configure function */
3661 /* target gets first dibs on parameters */
3662 e = (*(target->type->target_jim_configure))(target, goi);
3671 /* otherwise we 'continue' below */
3673 e = Jim_GetOpt_Nvp(goi, nvp_config_opts, &n);
3675 Jim_GetOpt_NvpUnknown(goi, nvp_config_opts, 0);
3681 if (goi->isconfigure) {
3682 Jim_SetResult_sprintf(goi->interp, "not setable: %s", n->name);
3686 if (goi->argc != 0) {
3687 Jim_WrongNumArgs(goi->interp, goi->argc, goi->argv, "NO PARAMS");
3691 Jim_SetResultString(goi->interp, target_get_name(target), -1);
3695 if (goi->argc == 0) {
3696 Jim_WrongNumArgs(goi->interp, goi->argc, goi->argv, "-event ?event-name? ...");
3700 e = Jim_GetOpt_Nvp(goi, nvp_target_event, &n);
3702 Jim_GetOpt_NvpUnknown(goi, nvp_target_event, 1);
3706 if (goi->isconfigure) {
3707 if (goi->argc != 1) {
3708 Jim_WrongNumArgs(goi->interp, goi->argc, goi->argv, "-event ?event-name? ?EVENT-BODY?");
3712 if (goi->argc != 0) {
3713 Jim_WrongNumArgs(goi->interp, goi->argc, goi->argv, "-event ?event-name?");
3719 target_event_action_t *teap;
3721 teap = target->event_action;
3722 /* replace existing? */
3724 if (teap->event == (enum target_event)n->value) {
3730 if (goi->isconfigure) {
3731 bool replace = true;
3734 teap = calloc(1, sizeof(*teap));
3737 teap->event = n->value;
3738 Jim_GetOpt_Obj(goi, &o);
3740 Jim_DecrRefCount(interp, teap->body);
3742 teap->body = Jim_DuplicateObj(goi->interp, o);
3745 * Tcl/TK - "tk events" have a nice feature.
3746 * See the "BIND" command.
3747 * We should support that here.
3748 * You can specify %X and %Y in the event code.
3749 * The idea is: %T - target name.
3750 * The idea is: %N - target number
3751 * The idea is: %E - event name.
3753 Jim_IncrRefCount(teap->body);
3757 /* add to head of event list */
3758 teap->next = target->event_action;
3759 target->event_action = teap;
3761 Jim_SetEmptyResult(goi->interp);
3765 Jim_SetEmptyResult(goi->interp);
3767 Jim_SetResult(goi->interp, Jim_DuplicateObj(goi->interp, teap->body));
3774 case TCFG_WORK_AREA_VIRT:
3775 if (goi->isconfigure) {
3776 target_free_all_working_areas(target);
3777 e = Jim_GetOpt_Wide(goi, &w);
3781 target->working_area_virt = w;
3782 target->working_area_virt_spec = true;
3784 if (goi->argc != 0) {
3788 Jim_SetResult(interp, Jim_NewIntObj(goi->interp, target->working_area_virt));
3792 case TCFG_WORK_AREA_PHYS:
3793 if (goi->isconfigure) {
3794 target_free_all_working_areas(target);
3795 e = Jim_GetOpt_Wide(goi, &w);
3799 target->working_area_phys = w;
3800 target->working_area_phys_spec = true;
3802 if (goi->argc != 0) {
3806 Jim_SetResult(interp, Jim_NewIntObj(goi->interp, target->working_area_phys));
3810 case TCFG_WORK_AREA_SIZE:
3811 if (goi->isconfigure) {
3812 target_free_all_working_areas(target);
3813 e = Jim_GetOpt_Wide(goi, &w);
3817 target->working_area_size = w;
3819 if (goi->argc != 0) {
3823 Jim_SetResult(interp, Jim_NewIntObj(goi->interp, target->working_area_size));
3827 case TCFG_WORK_AREA_BACKUP:
3828 if (goi->isconfigure) {
3829 target_free_all_working_areas(target);
3830 e = Jim_GetOpt_Wide(goi, &w);
3834 /* make this exactly 1 or 0 */
3835 target->backup_working_area = (!!w);
3837 if (goi->argc != 0) {
3841 Jim_SetResult(interp, Jim_NewIntObj(goi->interp, target->backup_working_area));
3842 /* loop for more e*/
3846 if (goi->isconfigure) {
3847 e = Jim_GetOpt_Nvp(goi, nvp_target_endian, &n);
3849 Jim_GetOpt_NvpUnknown(goi, nvp_target_endian, 1);
3852 target->endianness = n->value;
3854 if (goi->argc != 0) {
3858 n = Jim_Nvp_value2name_simple(nvp_target_endian, target->endianness);
3859 if (n->name == NULL) {
3860 target->endianness = TARGET_LITTLE_ENDIAN;
3861 n = Jim_Nvp_value2name_simple(nvp_target_endian, target->endianness);
3863 Jim_SetResultString(goi->interp, n->name, -1);
3868 if (goi->isconfigure) {
3869 if (goi->argc < 1) {
3870 Jim_SetResult_sprintf(goi->interp,
3875 if (target->variant) {
3876 free((void *)(target->variant));
3878 e = Jim_GetOpt_String(goi, &cp, NULL);
3879 target->variant = strdup(cp);
3881 if (goi->argc != 0) {
3885 Jim_SetResultString(goi->interp, target->variant,-1);
3888 case TCFG_CHAIN_POSITION:
3889 if (goi->isconfigure) {
3892 target_free_all_working_areas(target);
3893 e = Jim_GetOpt_Obj(goi, &o);
3897 tap = jtag_tap_by_jim_obj(goi->interp, o);
3901 /* make this exactly 1 or 0 */
3904 if (goi->argc != 0) {
3908 Jim_SetResultString(interp, target->tap->dotted_name, -1);
3909 /* loop for more e*/
3912 } /* while (goi->argc) */
3915 /* done - we return */
3919 /** this is the 'tcl' handler for the target specific command */
3920 static int tcl_target_func(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
3925 uint8_t target_buf[32];
3928 struct command_context_s *cmd_ctx;
3935 TS_CMD_MWW, TS_CMD_MWH, TS_CMD_MWB,
3936 TS_CMD_MDW, TS_CMD_MDH, TS_CMD_MDB,
3937 TS_CMD_MRW, TS_CMD_MRH, TS_CMD_MRB,
3938 TS_CMD_MEM2ARRAY, TS_CMD_ARRAY2MEM,
3946 TS_CMD_INVOKE_EVENT,
3949 static const Jim_Nvp target_options[] = {
3950 { .name = "configure", .value = TS_CMD_CONFIGURE },
3951 { .name = "cget", .value = TS_CMD_CGET },
3952 { .name = "mww", .value = TS_CMD_MWW },
3953 { .name = "mwh", .value = TS_CMD_MWH },
3954 { .name = "mwb", .value = TS_CMD_MWB },
3955 { .name = "mdw", .value = TS_CMD_MDW },
3956 { .name = "mdh", .value = TS_CMD_MDH },
3957 { .name = "mdb", .value = TS_CMD_MDB },
3958 { .name = "mem2array", .value = TS_CMD_MEM2ARRAY },
3959 { .name = "array2mem", .value = TS_CMD_ARRAY2MEM },
3960 { .name = "eventlist", .value = TS_CMD_EVENTLIST },
3961 { .name = "curstate", .value = TS_CMD_CURSTATE },
3963 { .name = "arp_examine", .value = TS_CMD_EXAMINE },
3964 { .name = "arp_poll", .value = TS_CMD_POLL },
3965 { .name = "arp_reset", .value = TS_CMD_RESET },
3966 { .name = "arp_halt", .value = TS_CMD_HALT },
3967 { .name = "arp_waitstate", .value = TS_CMD_WAITSTATE },
3968 { .name = "invoke-event", .value = TS_CMD_INVOKE_EVENT },
3970 { .name = NULL, .value = -1 },
3973 /* go past the "command" */
3974 Jim_GetOpt_Setup(&goi, interp, argc-1, argv + 1);
3976 target = Jim_CmdPrivData(goi.interp);
3977 cmd_ctx = Jim_GetAssocData(goi.interp, "context");
3979 /* commands here are in an NVP table */
3980 e = Jim_GetOpt_Nvp(&goi, target_options, &n);
3982 Jim_GetOpt_NvpUnknown(&goi, target_options, 0);
3985 /* Assume blank result */
3986 Jim_SetEmptyResult(goi.interp);
3989 case TS_CMD_CONFIGURE:
3991 Jim_WrongNumArgs(goi.interp, goi.argc, goi.argv, "missing: -option VALUE ...");
3994 goi.isconfigure = 1;
3995 return target_configure(&goi, target);
3997 // some things take params
3999 Jim_WrongNumArgs(goi.interp, 0, goi.argv, "missing: ?-option?");
4002 goi.isconfigure = 0;
4003 return target_configure(&goi, target);
4011 * argv[3] = optional count.
4014 if ((goi.argc == 2) || (goi.argc == 3)) {
4018 Jim_SetResult_sprintf(goi.interp, "expected: %s ADDR DATA [COUNT]", n->name);
4022 e = Jim_GetOpt_Wide(&goi, &a);
4027 e = Jim_GetOpt_Wide(&goi, &b);
4031 if (goi.argc == 3) {
4032 e = Jim_GetOpt_Wide(&goi, &c);
4042 target_buffer_set_u32(target, target_buf, b);
4046 target_buffer_set_u16(target, target_buf, b);
4050 target_buffer_set_u8(target, target_buf, b);
4054 for (x = 0 ; x < c ; x++) {
4055 e = target_write_memory(target, a, b, 1, target_buf);
4056 if (e != ERROR_OK) {
4057 Jim_SetResult_sprintf(interp, "Error writing @ 0x%08x: %d\n", (int)(a), e);
4070 /* argv[0] = command
4072 * argv[2] = optional count
4074 if ((goi.argc == 2) || (goi.argc == 3)) {
4075 Jim_SetResult_sprintf(goi.interp, "expected: %s ADDR [COUNT]", n->name);
4078 e = Jim_GetOpt_Wide(&goi, &a);
4083 e = Jim_GetOpt_Wide(&goi, &c);
4090 b = 1; /* shut up gcc */
4103 /* convert to "bytes" */
4105 /* count is now in 'BYTES' */
4111 e = target_read_memory(target, a, b, y / b, target_buf);
4112 if (e != ERROR_OK) {
4113 Jim_SetResult_sprintf(interp, "error reading target @ 0x%08lx", (int)(a));
4117 Jim_fprintf(interp, interp->cookie_stdout, "0x%08x ", (int)(a));
4120 for (x = 0 ; (x < 16) && (x < y) ; x += 4) {
4121 z = target_buffer_get_u32(target, &(target_buf[ x * 4 ]));
4122 Jim_fprintf(interp, interp->cookie_stdout, "%08x ", (int)(z));
4124 for (; (x < 16) ; x += 4) {
4125 Jim_fprintf(interp, interp->cookie_stdout, " ");
4129 for (x = 0 ; (x < 16) && (x < y) ; x += 2) {
4130 z = target_buffer_get_u16(target, &(target_buf[ x * 2 ]));
4131 Jim_fprintf(interp, interp->cookie_stdout, "%04x ", (int)(z));
4133 for (; (x < 16) ; x += 2) {
4134 Jim_fprintf(interp, interp->cookie_stdout, " ");
4139 for (x = 0 ; (x < 16) && (x < y) ; x += 1) {
4140 z = target_buffer_get_u8(target, &(target_buf[ x * 4 ]));
4141 Jim_fprintf(interp, interp->cookie_stdout, "%02x ", (int)(z));
4143 for (; (x < 16) ; x += 1) {
4144 Jim_fprintf(interp, interp->cookie_stdout, " ");
4148 /* ascii-ify the bytes */
4149 for (x = 0 ; x < y ; x++) {
4150 if ((target_buf[x] >= 0x20) &&
4151 (target_buf[x] <= 0x7e)) {
4155 target_buf[x] = '.';
4160 target_buf[x] = ' ';
4165 /* print - with a newline */
4166 Jim_fprintf(interp, interp->cookie_stdout, "%s\n", target_buf);
4172 case TS_CMD_MEM2ARRAY:
4173 return target_mem2array(goi.interp, target, goi.argc, goi.argv);
4175 case TS_CMD_ARRAY2MEM:
4176 return target_array2mem(goi.interp, target, goi.argc, goi.argv);
4178 case TS_CMD_EXAMINE:
4180 Jim_WrongNumArgs(goi.interp, 2, argv, "[no parameters]");
4183 if (!target->tap->enabled)
4184 goto err_tap_disabled;
4185 e = target->type->examine(target);
4186 if (e != ERROR_OK) {
4187 Jim_SetResult_sprintf(interp, "examine-fails: %d", e);
4193 Jim_WrongNumArgs(goi.interp, 2, argv, "[no parameters]");
4196 if (!target->tap->enabled)
4197 goto err_tap_disabled;
4198 if (!(target_was_examined(target))) {
4199 e = ERROR_TARGET_NOT_EXAMINED;
4201 e = target->type->poll(target);
4203 if (e != ERROR_OK) {
4204 Jim_SetResult_sprintf(interp, "poll-fails: %d", e);
4211 if (goi.argc != 2) {
4212 Jim_WrongNumArgs(interp, 2, argv,
4213 "([tT]|[fF]|assert|deassert) BOOL");
4216 e = Jim_GetOpt_Nvp(&goi, nvp_assert, &n);
4218 Jim_GetOpt_NvpUnknown(&goi, nvp_assert, 1);
4221 /* the halt or not param */
4222 e = Jim_GetOpt_Wide(&goi, &a);
4226 if (!target->tap->enabled)
4227 goto err_tap_disabled;
4228 if (!target->type->assert_reset
4229 || !target->type->deassert_reset) {
4230 Jim_SetResult_sprintf(interp,
4231 "No target-specific reset for %s",
4235 /* determine if we should halt or not. */
4236 target->reset_halt = !!a;
4237 /* When this happens - all workareas are invalid. */
4238 target_free_all_working_areas_restore(target, 0);
4241 if (n->value == NVP_ASSERT) {
4242 e = target->type->assert_reset(target);
4244 e = target->type->deassert_reset(target);
4246 return (e == ERROR_OK) ? JIM_OK : JIM_ERR;
4249 Jim_WrongNumArgs(goi.interp, 0, argv, "halt [no parameters]");
4252 if (!target->tap->enabled)
4253 goto err_tap_disabled;
4254 e = target->type->halt(target);
4255 return (e == ERROR_OK) ? JIM_OK : JIM_ERR;
4256 case TS_CMD_WAITSTATE:
4257 /* params: <name> statename timeoutmsecs */
4258 if (goi.argc != 2) {
4259 Jim_SetResult_sprintf(goi.interp, "%s STATENAME TIMEOUTMSECS", n->name);
4262 e = Jim_GetOpt_Nvp(&goi, nvp_target_state, &n);
4264 Jim_GetOpt_NvpUnknown(&goi, nvp_target_state,1);
4267 e = Jim_GetOpt_Wide(&goi, &a);
4271 if (!target->tap->enabled)
4272 goto err_tap_disabled;
4273 e = target_wait_state(target, n->value, a);
4274 if (e != ERROR_OK) {
4275 Jim_SetResult_sprintf(goi.interp,
4276 "target: %s wait %s fails (%d) %s",
4279 e, target_strerror_safe(e));
4284 case TS_CMD_EVENTLIST:
4285 /* List for human, Events defined for this target.
4286 * scripts/programs should use 'name cget -event NAME'
4289 target_event_action_t *teap;
4290 teap = target->event_action;
4291 command_print(cmd_ctx, "Event actions for target (%d) %s\n",
4292 target->target_number,
4294 command_print(cmd_ctx, "%-25s | Body", "Event");
4295 command_print(cmd_ctx, "------------------------- | ----------------------------------------");
4297 command_print(cmd_ctx,
4299 Jim_Nvp_value2name_simple(nvp_target_event, teap->event)->name,
4300 Jim_GetString(teap->body, NULL));
4303 command_print(cmd_ctx, "***END***");
4306 case TS_CMD_CURSTATE:
4307 if (goi.argc != 0) {
4308 Jim_WrongNumArgs(goi.interp, 0, argv, "[no parameters]");
4311 Jim_SetResultString(goi.interp,
4312 target_state_name( target ),
4315 case TS_CMD_INVOKE_EVENT:
4316 if (goi.argc != 1) {
4317 Jim_SetResult_sprintf(goi.interp, "%s ?EVENTNAME?",n->name);
4320 e = Jim_GetOpt_Nvp(&goi, nvp_target_event, &n);
4322 Jim_GetOpt_NvpUnknown(&goi, nvp_target_event, 1);
4325 target_handle_event(target, n->value);
4331 Jim_SetResult_sprintf(interp, "[TAP is disabled]");
4335 static int target_create(Jim_GetOptInfo *goi)
4344 struct command_context_s *cmd_ctx;
4346 cmd_ctx = Jim_GetAssocData(goi->interp, "context");
4347 if (goi->argc < 3) {
4348 Jim_WrongNumArgs(goi->interp, 1, goi->argv, "?name? ?type? ..options...");
4353 Jim_GetOpt_Obj(goi, &new_cmd);
4354 /* does this command exist? */
4355 cmd = Jim_GetCommand(goi->interp, new_cmd, JIM_ERRMSG);
4357 cp = Jim_GetString(new_cmd, NULL);
4358 Jim_SetResult_sprintf(goi->interp, "Command/target: %s Exists", cp);
4363 e = Jim_GetOpt_String(goi, &cp2, NULL);
4365 /* now does target type exist */
4366 for (x = 0 ; target_types[x] ; x++) {
4367 if (0 == strcmp(cp, target_types[x]->name)) {
4372 if (target_types[x] == NULL) {
4373 Jim_SetResult_sprintf(goi->interp, "Unknown target type %s, try one of ", cp);
4374 for (x = 0 ; target_types[x] ; x++) {
4375 if (target_types[x + 1]) {
4376 Jim_AppendStrings(goi->interp,
4377 Jim_GetResult(goi->interp),
4378 target_types[x]->name,
4381 Jim_AppendStrings(goi->interp,
4382 Jim_GetResult(goi->interp),
4384 target_types[x]->name,NULL);
4391 target = calloc(1,sizeof(target_t));
4392 /* set target number */
4393 target->target_number = new_target_number();
4395 /* allocate memory for each unique target type */
4396 target->type = (target_type_t*)calloc(1,sizeof(target_type_t));
4398 memcpy(target->type, target_types[x], sizeof(target_type_t));
4400 /* will be set by "-endian" */
4401 target->endianness = TARGET_ENDIAN_UNKNOWN;
4403 target->working_area = 0x0;
4404 target->working_area_size = 0x0;
4405 target->working_areas = NULL;
4406 target->backup_working_area = 0;
4408 target->state = TARGET_UNKNOWN;
4409 target->debug_reason = DBG_REASON_UNDEFINED;
4410 target->reg_cache = NULL;
4411 target->breakpoints = NULL;
4412 target->watchpoints = NULL;
4413 target->next = NULL;
4414 target->arch_info = NULL;
4416 target->display = 1;
4418 target->halt_issued = false;
4420 /* initialize trace information */
4421 target->trace_info = malloc(sizeof(trace_t));
4422 target->trace_info->num_trace_points = 0;
4423 target->trace_info->trace_points_size = 0;
4424 target->trace_info->trace_points = NULL;
4425 target->trace_info->trace_history_size = 0;
4426 target->trace_info->trace_history = NULL;
4427 target->trace_info->trace_history_pos = 0;
4428 target->trace_info->trace_history_overflowed = 0;
4430 target->dbgmsg = NULL;
4431 target->dbg_msg_enabled = 0;
4433 target->endianness = TARGET_ENDIAN_UNKNOWN;
4435 /* Do the rest as "configure" options */
4436 goi->isconfigure = 1;
4437 e = target_configure(goi, target);
4439 if (target->tap == NULL)
4441 Jim_SetResultString(interp, "-chain-position required when creating target", -1);
4451 if (target->endianness == TARGET_ENDIAN_UNKNOWN) {
4452 /* default endian to little if not specified */
4453 target->endianness = TARGET_LITTLE_ENDIAN;
4456 /* incase variant is not set */
4457 if (!target->variant)
4458 target->variant = strdup("");
4460 /* create the target specific commands */
4461 if (target->type->register_commands) {
4462 (*(target->type->register_commands))(cmd_ctx);
4464 if (target->type->target_create) {
4465 (*(target->type->target_create))(target, goi->interp);
4468 /* append to end of list */
4471 tpp = &(all_targets);
4473 tpp = &((*tpp)->next);
4478 cp = Jim_GetString(new_cmd, NULL);
4479 target->cmd_name = strdup(cp);
4481 /* now - create the new target name command */
4482 e = Jim_CreateCommand(goi->interp,
4485 tcl_target_func, /* C function */
4486 target, /* private data */
4487 NULL); /* no del proc */
4492 static int jim_target(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
4496 struct command_context_s *cmd_ctx;
4500 /* TG = target generic */
4508 const char *target_cmds[] = {
4509 "create", "types", "names", "current", "number",
4511 NULL /* terminate */
4514 LOG_DEBUG("Target command params:");
4515 LOG_DEBUG("%s", Jim_Debug_ArgvString(interp, argc, argv));
4517 cmd_ctx = Jim_GetAssocData(interp, "context");
4519 Jim_GetOpt_Setup(&goi, interp, argc-1, argv + 1);
4521 if (goi.argc == 0) {
4522 Jim_WrongNumArgs(interp, 1, argv, "missing: command ...");
4526 /* Jim_GetOpt_Debug(&goi); */
4527 r = Jim_GetOpt_Enum(&goi, target_cmds, &x);
4534 Jim_Panic(goi.interp,"Why am I here?");
4536 case TG_CMD_CURRENT:
4537 if (goi.argc != 0) {
4538 Jim_WrongNumArgs(goi.interp, 1, goi.argv, "Too many parameters");
4541 Jim_SetResultString(goi.interp, get_current_target(cmd_ctx)->cmd_name, -1);
4544 if (goi.argc != 0) {
4545 Jim_WrongNumArgs(goi.interp, 1, goi.argv, "Too many parameters");
4548 Jim_SetResult(goi.interp, Jim_NewListObj(goi.interp, NULL, 0));
4549 for (x = 0 ; target_types[x] ; x++) {
4550 Jim_ListAppendElement(goi.interp,
4551 Jim_GetResult(goi.interp),
4552 Jim_NewStringObj(goi.interp, target_types[x]->name, -1));
4556 if (goi.argc != 0) {
4557 Jim_WrongNumArgs(goi.interp, 1, goi.argv, "Too many parameters");
4560 Jim_SetResult(goi.interp, Jim_NewListObj(goi.interp, NULL, 0));
4561 target = all_targets;
4563 Jim_ListAppendElement(goi.interp,
4564 Jim_GetResult(goi.interp),
4565 Jim_NewStringObj(goi.interp, target->cmd_name, -1));
4566 target = target->next;
4571 Jim_WrongNumArgs(goi.interp, goi.argc, goi.argv, "?name ... config options ...");
4574 return target_create(&goi);
4577 /* It's OK to remove this mechanism sometime after August 2010 or so */
4578 LOG_WARNING("don't use numbers as target identifiers; use names");
4579 if (goi.argc != 1) {
4580 Jim_SetResult_sprintf(goi.interp, "expected: target number ?NUMBER?");
4583 e = Jim_GetOpt_Wide(&goi, &w);
4587 for (x = 0, target = all_targets; target; target = target->next, x++) {
4588 if (target->target_number == w)
4591 if (target == NULL) {
4592 Jim_SetResult_sprintf(goi.interp,
4593 "Target: number %d does not exist", (int)(w));
4596 Jim_SetResultString(goi.interp, target->cmd_name, -1);
4599 if (goi.argc != 0) {
4600 Jim_WrongNumArgs(goi.interp, 0, goi.argv, "<no parameters>");
4603 for (x = 0, target = all_targets; target; target = target->next, x++)
4605 Jim_SetResult(goi.interp, Jim_NewIntObj(goi.interp, x));
4621 static int fastload_num;
4622 static struct FastLoad *fastload;
4624 static void free_fastload(void)
4626 if (fastload != NULL)
4629 for (i = 0; i < fastload_num; i++)
4631 if (fastload[i].data)
4632 free(fastload[i].data);
4642 static int handle_fast_load_image_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
4646 uint32_t image_size;
4647 uint32_t min_address = 0;
4648 uint32_t max_address = 0xffffffff;
4653 duration_t duration;
4654 char *duration_text;
4656 int retval = parse_load_image_command_args(args, argc,
4657 &image, &min_address, &max_address);
4658 if (ERROR_OK != retval)
4661 duration_start_measure(&duration);
4663 if (image_open(&image, args[0], (argc >= 3) ? args[2] : NULL) != ERROR_OK)
4670 fastload_num = image.num_sections;
4671 fastload = (struct FastLoad *)malloc(sizeof(struct FastLoad)*image.num_sections);
4672 if (fastload == NULL)
4674 image_close(&image);
4677 memset(fastload, 0, sizeof(struct FastLoad)*image.num_sections);
4678 for (i = 0; i < image.num_sections; i++)
4680 buffer = malloc(image.sections[i].size);
4683 command_print(cmd_ctx, "error allocating buffer for section (%d bytes)",
4684 (int)(image.sections[i].size));
4688 if ((retval = image_read_section(&image, i, 0x0, image.sections[i].size, buffer, &buf_cnt)) != ERROR_OK)
4694 uint32_t offset = 0;
4695 uint32_t length = buf_cnt;
4698 /* DANGER!!! beware of unsigned comparision here!!! */
4700 if ((image.sections[i].base_address + buf_cnt >= min_address)&&
4701 (image.sections[i].base_address < max_address))
4703 if (image.sections[i].base_address < min_address)
4705 /* clip addresses below */
4706 offset += min_address-image.sections[i].base_address;
4710 if (image.sections[i].base_address + buf_cnt > max_address)
4712 length -= (image.sections[i].base_address + buf_cnt)-max_address;
4715 fastload[i].address = image.sections[i].base_address + offset;
4716 fastload[i].data = malloc(length);
4717 if (fastload[i].data == NULL)
4722 memcpy(fastload[i].data, buffer + offset, length);
4723 fastload[i].length = length;
4725 image_size += length;
4726 command_print(cmd_ctx, "%u bytes written at address 0x%8.8x",
4727 (unsigned int)length,
4728 ((unsigned int)(image.sections[i].base_address + offset)));
4734 duration_stop_measure(&duration, &duration_text);
4735 if (retval == ERROR_OK)
4737 command_print(cmd_ctx, "Loaded %u bytes in %s", (unsigned int)image_size, duration_text);
4738 command_print(cmd_ctx, "NB!!! image has not been loaded to target, issue a subsequent 'fast_load' to do so.");
4740 free(duration_text);
4742 image_close(&image);
4744 if (retval != ERROR_OK)
4752 static int handle_fast_load_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
4755 return ERROR_COMMAND_SYNTAX_ERROR;
4756 if (fastload == NULL)
4758 LOG_ERROR("No image in memory");
4762 int ms = timeval_ms();
4764 int retval = ERROR_OK;
4765 for (i = 0; i < fastload_num;i++)
4767 target_t *target = get_current_target(cmd_ctx);
4768 command_print(cmd_ctx, "Write to 0x%08x, length 0x%08x",
4769 (unsigned int)(fastload[i].address),
4770 (unsigned int)(fastload[i].length));
4771 if (retval == ERROR_OK)
4773 retval = target_write_buffer(target, fastload[i].address, fastload[i].length, fastload[i].data);
4775 size += fastload[i].length;
4777 int after = timeval_ms();
4778 command_print(cmd_ctx, "Loaded image %f kBytes/s", (float)(size/1024.0)/((float)(after-ms)/1000.0));
4782 static int jim_mcrmrc(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
4784 command_context_t *context;
4788 context = Jim_GetAssocData(interp, "context");
4789 if (context == NULL) {
4790 LOG_ERROR("array2mem: no command context");
4793 target = get_current_target(context);
4794 if (target == NULL) {
4795 LOG_ERROR("array2mem: no current target");
4799 if ((argc < 6) || (argc > 7))
4813 e = Jim_GetLong(interp, argv[1], &l);
4819 e = Jim_GetLong(interp, argv[2], &l);
4825 e = Jim_GetLong(interp, argv[3], &l);
4831 e = Jim_GetLong(interp, argv[4], &l);
4837 e = Jim_GetLong(interp, argv[5], &l);
4847 e = Jim_GetLong(interp, argv[6], &l);
4853 retval = target_mcr(target, cpnum, op1, op2, CRn, CRm, value);
4854 if (retval != ERROR_OK)
4858 retval = target_mrc(target, cpnum, op1, op2, CRn, CRm, &value);
4859 if (retval != ERROR_OK)
4862 Jim_SetResult(interp, Jim_NewIntObj(interp, value));